-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add SyncCompute API skeleton in executor
Put common definitions in the common.proto file. Some rework of key hanlding that will be used in the executor.
- Loading branch information
1 parent
bed330f
commit 29430b7
Showing
21 changed files
with
378 additions
and
97 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use std::{env, path::PathBuf}; | ||
|
||
fn main() { | ||
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); | ||
tonic_build::configure() | ||
.file_descriptor_set_path(out_dir.join("executor_descriptor.bin")) | ||
.compile(&["../../proto/executor.proto"], &["../../proto"]) | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use clap::Parser; | ||
|
||
#[derive(Parser, Debug, Clone)] | ||
#[command(version, about, long_about = None)] | ||
pub struct Args { | ||
#[arg(long, default_value_t = 4)] | ||
pub tokio_threads: usize, | ||
|
||
#[arg(long, default_value_t = 8)] | ||
pub fhe_compute_threads: usize, | ||
|
||
#[arg(long, default_value = "127.0.0.1:50051")] | ||
pub server_addr: String, | ||
} | ||
|
||
pub fn parse_args() -> Args { | ||
Args::parse() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod cli; | ||
pub mod server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
use std::error::Error; | ||
|
||
mod cli; | ||
mod server; | ||
|
||
fn main() -> Result<(), Box<dyn Error>> { | ||
let args = cli::parse_args(); | ||
server::start(&args)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::error::Error; | ||
|
||
use executor::{ | ||
fhevm_executor_server::{FhevmExecutor, FhevmExecutorServer}, | ||
SyncComputeRequest, SyncComputeResponse, | ||
}; | ||
use tonic::{transport::Server, Request, Response}; | ||
|
||
mod common { | ||
tonic::include_proto!("fhevm.common"); | ||
} | ||
|
||
pub mod executor { | ||
tonic::include_proto!("fhevm.executor"); | ||
} | ||
|
||
pub fn start(args: &crate::cli::Args) -> Result<(), Box<dyn Error>> { | ||
let runtime = tokio::runtime::Builder::new_multi_thread() | ||
.worker_threads(args.tokio_threads) | ||
.max_blocking_threads(args.fhe_compute_threads) | ||
.enable_all() | ||
.build()?; | ||
|
||
let executor = FhevmExecutorService::default(); | ||
let addr = args.server_addr.parse().expect("server address"); | ||
|
||
runtime.block_on(async { | ||
Server::builder() | ||
.add_service(FhevmExecutorServer::new(executor)) | ||
.serve(addr) | ||
.await?; | ||
Ok::<(), Box<dyn Error>>(()) | ||
})?; | ||
Ok(()) | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct FhevmExecutorService {} | ||
|
||
#[tonic::async_trait] | ||
impl FhevmExecutor for FhevmExecutorService { | ||
async fn sync_compute( | ||
&self, | ||
req: Request<SyncComputeRequest>, | ||
) -> Result<Response<SyncComputeResponse>, tonic::Status> { | ||
Ok(Response::new(SyncComputeResponse::default())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use executor::server::executor::{fhevm_executor_client::FhevmExecutorClient, SyncComputeRequest}; | ||
use utils::TestInstance; | ||
|
||
mod utils; | ||
|
||
#[tokio::test] | ||
async fn compute_on_ciphertexts() -> Result<(), Box<dyn std::error::Error>> { | ||
let test_instance = TestInstance::new(); | ||
let mut client = FhevmExecutorClient::connect(test_instance.server_addr).await?; | ||
let resp = client.sync_compute(SyncComputeRequest::default()).await?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use clap::Parser; | ||
use executor::{cli::Args, server}; | ||
use fhevm_engine_common::keys::{FhevmKeys, SerializedFhevmKeys}; | ||
|
||
pub struct TestInstance { | ||
pub keys: FhevmKeys, | ||
pub server_addr: String, | ||
} | ||
|
||
impl TestInstance { | ||
pub fn new() -> Self { | ||
// Get defaults by parsing a cmd line without any arguments. | ||
let args = Args::parse_from(&["test"]); | ||
|
||
let instance = TestInstance { | ||
keys: SerializedFhevmKeys::load_from_disk().into(), | ||
server_addr: format!("http://{}", args.server_addr), | ||
}; | ||
|
||
std::thread::spawn(move || server::start(&args).expect("start server")); | ||
|
||
// TODO: a hacky way to wait for the server to start | ||
std::thread::sleep(std::time::Duration::from_millis(150)); | ||
|
||
instance | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,7 @@ | ||
use fhevm_engine_common::generate_fhe_keys; | ||
use fhevm_engine_common::keys::{FhevmKeys, SerializedFhevmKeys}; | ||
|
||
fn main() { | ||
let output_dir = "fhevm-keys"; | ||
println!("Generating keys..."); | ||
let keys = generate_fhe_keys(); | ||
println!("Creating directory {output_dir}"); | ||
std::fs::create_dir_all(output_dir).unwrap(); | ||
println!("Creating file {output_dir}/cks"); | ||
std::fs::write(format!("{output_dir}/cks"), keys.client_key).unwrap(); | ||
println!("Creating file {output_dir}/pks"); | ||
std::fs::write(format!("{output_dir}/pks"), keys.compact_public_key).unwrap(); | ||
println!("Creating file {output_dir}/sks"); | ||
std::fs::write(format!("{output_dir}/sks"), keys.server_key).unwrap(); | ||
let keys = FhevmKeys::new(); | ||
let ser_keys: SerializedFhevmKeys = keys.into(); | ||
ser_keys.save_to_disk(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use std::fs::read; | ||
|
||
use tfhe::{ | ||
generate_keys, | ||
shortint::parameters::{ | ||
list_compression::COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, | ||
PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, | ||
}, | ||
ClientKey, CompactPublicKey, ConfigBuilder, ServerKey, | ||
}; | ||
|
||
pub struct FhevmKeys { | ||
pub server_key: ServerKey, | ||
pub client_key: Option<ClientKey>, | ||
pub compact_public_key: Option<CompactPublicKey>, | ||
} | ||
|
||
pub struct SerializedFhevmKeys { | ||
pub server_key: Vec<u8>, | ||
pub client_key: Option<Vec<u8>>, | ||
pub compact_public_key: Option<Vec<u8>>, | ||
} | ||
|
||
impl FhevmKeys { | ||
pub fn new() -> Self { | ||
println!("Generating keys..."); | ||
let config = | ||
ConfigBuilder::with_custom_parameters(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64) | ||
.enable_compression(COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64) | ||
.build(); | ||
let (client_key, server_key) = generate_keys(config); | ||
let compact_public_key = CompactPublicKey::new(&client_key); | ||
FhevmKeys { | ||
server_key, | ||
client_key: Some(client_key), | ||
compact_public_key: Some(compact_public_key), | ||
} | ||
} | ||
} | ||
|
||
impl SerializedFhevmKeys { | ||
const DIRECTORY: &'static str = "fhevm-keys"; | ||
const SKS: &'static str = "fhevm-keys/sks"; | ||
const CKS: &'static str = "fhevm-keys/cks"; | ||
const PKS: &'static str = "fhevm-keys/pks"; | ||
|
||
pub fn save_to_disk(self) { | ||
println!("Creating directory {}", Self::DIRECTORY); | ||
std::fs::create_dir_all(Self::DIRECTORY).expect("create keys directory"); | ||
|
||
println!("Creating file {}", Self::SKS); | ||
std::fs::write(format!("{}", Self::SKS), self.server_key).expect("write sks"); | ||
|
||
if self.client_key.is_some() { | ||
println!("Creating file {}", Self::CKS); | ||
std::fs::write(format!("{}", Self::CKS), self.client_key.unwrap()).expect("write cks"); | ||
} | ||
|
||
if self.compact_public_key.is_some() { | ||
println!("Creating file {}", Self::PKS); | ||
std::fs::write(format!("{}", Self::PKS), self.compact_public_key.unwrap()) | ||
.expect("write pks"); | ||
} | ||
} | ||
|
||
pub fn load_from_disk() -> Self { | ||
let server_key = read(Self::SKS).expect("read server key"); | ||
let client_key = read(Self::CKS); | ||
let compact_public_key = read(Self::PKS); | ||
SerializedFhevmKeys { | ||
server_key, | ||
client_key: client_key.ok(), | ||
compact_public_key: compact_public_key.ok(), | ||
} | ||
} | ||
} | ||
|
||
impl From<FhevmKeys> for SerializedFhevmKeys { | ||
fn from(f: FhevmKeys) -> Self { | ||
SerializedFhevmKeys { | ||
server_key: bincode::serialize(&f.server_key).expect("serialize server key"), | ||
client_key: f | ||
.client_key | ||
.map(|c| bincode::serialize(&c).expect("serialize client key")), | ||
compact_public_key: f | ||
.compact_public_key | ||
.map(|p| bincode::serialize(&p).expect("serialize compact public key")), | ||
} | ||
} | ||
} | ||
|
||
impl From<SerializedFhevmKeys> for FhevmKeys { | ||
fn from(f: SerializedFhevmKeys) -> Self { | ||
FhevmKeys { | ||
server_key: bincode::deserialize(&f.server_key).expect("deserialize server key"), | ||
client_key: f | ||
.client_key | ||
.map(|c| bincode::deserialize(&c).expect("deserialize client key")), | ||
compact_public_key: f | ||
.compact_public_key | ||
.map(|p| bincode::deserialize(&p).expect("deserialize compact public key")), | ||
} | ||
} | ||
} |
Oops, something went wrong.