diff --git a/fhevm-engine/Cargo.lock b/fhevm-engine/Cargo.lock index e437a5c6..f3562b44 100644 --- a/fhevm-engine/Cargo.lock +++ b/fhevm-engine/Cargo.lock @@ -1966,6 +1966,7 @@ dependencies = [ "hex", "rand", "rand_chacha", + "serde", "sha3", "strum", "tfhe", @@ -4078,18 +4079,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.208" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cff085d2cb684faa248efb494c39b68e522822ac0de72ccf08109abde717cfb2" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.208" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24008e81ff7613ed8e5ba0cfaf24e2c2f1e5b8a0495711e44fcd4882fca62bcf" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", @@ -4658,9 +4659,9 @@ dependencies = [ [[package]] name = "tfhe" -version = "0.8.0-alpha.6" +version = "0.8.0-alpha.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c78dd2a7467f4660e5c207b4a5c02a586dc41d866b6767e7d9bc210f8dc6a2f" +checksum = "b3a04027e69f5723727b8092c7d30e36701f97b298d180adc3dd04b92bfea1cb" dependencies = [ "aligned-vec", "bincode", diff --git a/fhevm-engine/Cargo.toml b/fhevm-engine/Cargo.toml index e45e8e6c..91b47964 100644 --- a/fhevm-engine/Cargo.toml +++ b/fhevm-engine/Cargo.toml @@ -11,6 +11,7 @@ bincode = "1.3.3" sha3 = "0.10.8" anyhow = "1.0.86" daggy = "0.8.0" +serde = "1.0.210" [profile.dev.package.tfhe] overflow-checks = false diff --git a/fhevm-engine/coprocessor/Cargo.toml b/fhevm-engine/coprocessor/Cargo.toml index 9dc62f75..a14df9ba 100644 --- a/fhevm-engine/coprocessor/Cargo.toml +++ b/fhevm-engine/coprocessor/Cargo.toml @@ -4,9 +4,9 @@ version = "0.1.0" edition = "2021" [target.'cfg(target_arch = "x86_64")'.dependencies] -tfhe = { version = "0.8.0-alpha.6", features = ["boolean", "shortint", "integer", "x86_64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] } +tfhe = { version = "0.8.0-alpha.8", features = ["boolean", "shortint", "integer", "x86_64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] } [target.'cfg(target_arch = "aarch64")'.dependencies] -tfhe = { version = "0.8.0-alpha.6", features = ["boolean", "shortint", "integer", "aarch64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] } +tfhe = { version = "0.8.0-alpha.8", features = ["boolean", "shortint", "integer", "aarch64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] } [dependencies] # Common dependencies diff --git a/fhevm-engine/coprocessor/src/tests/errors.rs b/fhevm-engine/coprocessor/src/tests/errors.rs index b0463b72..89e8a263 100644 --- a/fhevm-engine/coprocessor/src/tests/errors.rs +++ b/fhevm-engine/coprocessor/src/tests/errors.rs @@ -15,6 +15,7 @@ use crate::{ utils::{default_api_key, default_tenant_id, setup_test_app}, }, }; +use fhevm_engine_common::utils::safe_serialize; use tonic::metadata::MetadataValue; #[tokio::test] @@ -47,7 +48,7 @@ async fn test_coprocessor_input_errors() -> Result<(), Box Result<(), Box Result<(), Box Result<(), Box Result<(), Box> { .build_with_proof_packed(&keys.public_params, &[], tfhe::zk::ZkComputeLoad::Proof) .unwrap(); - let serialized = bincode::serialize(&the_list).unwrap(); + let serialized = safe_serialize(&the_list); println!("Encrypting inputs..."); let mut input_request = tonic::Request::new(InputUploadBatch { @@ -144,7 +145,7 @@ async fn custom_insert_inputs() -> Result<(), Box> { .build_with_proof_packed(&keys.public_params, &[], tfhe::zk::ZkComputeLoad::Proof) .unwrap(); - let serialized = bincode::serialize(&the_list).unwrap(); + let serialized = safe_serialize(&the_list); println!("Encrypting inputs..."); let mut input_request = tonic::Request::new(InputUploadBatch { diff --git a/fhevm-engine/coprocessor/src/tests/random.rs b/fhevm-engine/coprocessor/src/tests/random.rs index f4345cc4..cd6450af 100644 --- a/fhevm-engine/coprocessor/src/tests/random.rs +++ b/fhevm-engine/coprocessor/src/tests/random.rs @@ -117,17 +117,17 @@ async fn test_fhe_random_basic() -> Result<(), Box> { let resp = decrypt_ciphertexts(&pool, 1, decrypt_request).await?; let expected: Vec = vec![ DecryptionResult { value: "true".to_string(), output_type: 0 }, - DecryptionResult { value: "15".to_string(), output_type: 1 }, - DecryptionResult { value: "255".to_string(), output_type: 2 }, - DecryptionResult { value: "35839".to_string(), output_type: 3 }, - DecryptionResult { value: "3850341375".to_string(), output_type: 4 }, - DecryptionResult { value: "17749940961552600063".to_string(), output_type: 5 }, - DecryptionResult { value: "338317369763394761689683669875179883519".to_string(), output_type: 6 }, - DecryptionResult { value: "728577578444838901693251259047493597479225494527".to_string(), output_type: 7 }, - DecryptionResult { value: "74507219184508265163276797316037056014989661721330011251665343119622538234879".to_string(), output_type: 8 }, - DecryptionResult { value: "2504114756871049901977831169567626719242268143006537293974082655527485165166706069255105841561002259297707106091992898062733977791194469887698664398228479".to_string(), output_type: 9 }, - DecryptionResult { value: "161403477149019279849094421471438276759470342665592799183135241729901366432142182887626249781582277693598739699657901471347278400933758701565974722374779502889670960728481539900065496353261065895631087051001039006688379936272227778512119687343404647253114276811856825699481770620842978083170204991519490804735".to_string(), output_type: 10 }, - DecryptionResult { value: "25936938232639367084138738776796005240569894972685748071387345538441824752721115183420331032123662095068585061486170621701914537473863580716907485015792656836012274813782383618707951652014429130281418579493619200757130996710486367616379470398907811991574381901820339230001839609009629272311033481447460170915425494881184165217672126153498808951018389035017882126611344150334135611931908252734913533251704739949869925607308666786131997107573881460338809242245923254917133620850600852429219039727909517483206781101690821959886535327935681900259567708853284514443037134192173440669143533573266158029589611848512003083263".to_string(), output_type: 11 } + DecryptionResult { value: "6".to_string(), output_type: 1 }, + DecryptionResult { value: "6".to_string(), output_type: 2 }, + DecryptionResult { value: "23046".to_string(), output_type: 3 }, + DecryptionResult { value: "2257672710".to_string(), output_type: 4 }, + DecryptionResult { value: "12138718414261803526".to_string(), output_type: 5 }, + DecryptionResult { value: "130536719590611940049803920731387550214".to_string(), output_type: 6 }, + DecryptionResult { value: "971176705489787087023559718483701127113677560326".to_string(), output_type: 7 }, + DecryptionResult { value: "62210255757460412253332620363065848989112923584999887570035464828426661222918".to_string(), output_type: 8 }, + DecryptionResult { value: "167958935840398111366003661819132943572579228212385323643009044778284654758971531763634195717060767316412295162146605242695852136468800900790045270694406".to_string(), output_type: 9 }, + DecryptionResult { value: "127460563385689404084570635453516642982330737396307363709535669246693726363369279326274116849562765049033667934125131507607869225026009107310544028242879211116101076829363291657387574479716476869613221980036198477470920343187777849916436388023322996436007563319615378730113313056846971613305517149919649028614".to_string(), output_type: 10 }, + DecryptionResult { value: "29687326363179539154232170826093317060572491263948154715413122357200687474061448043555291795321984983113829977114301561317315809196828773909981565653610082891472340553741585442577497506409472143098823132371629384036451019214072899732235656145602725111017828708028912154841404994944466545632048686969494346234325709069045453046020648098209481065154942201598888424765642988091655940417557742117518483932517015160272576663001732809302519121630949039706341063098676812339442637939392896074884484156187775746589025164758187166306751922076107008755031211360389068550389609734783888124482836062055425119177200121882346609158".to_string(), output_type: 11 } ]; println!("results: {:#?}", resp); @@ -187,14 +187,14 @@ async fn test_fhe_random_bounded() -> Result<(), Box> { ]; let results = [ "true", - "3", - "127", - "3071", - "629115903", - "3914882906270436351", - "83105594572690914092152714301353724927", - "363202169112113172142330050868422842565242358783", - "16611174565850167451491304811693102088354669388509729231936551115665973414911", + "2", + "6", + "6662", + "110189062", + "2915346377407027718", + "45466127860377324183960268873445497350", + "240425886824335627921717302125559617285711288838", + "4314211138802314541547127858721895062477931252179605550306672824470096402950", ]; for (idx, the_type) in supported_types().iter().enumerate() { diff --git a/fhevm-engine/executor/Cargo.toml b/fhevm-engine/executor/Cargo.toml index a8fa51c9..dbb58c17 100644 --- a/fhevm-engine/executor/Cargo.toml +++ b/fhevm-engine/executor/Cargo.toml @@ -4,9 +4,9 @@ version = "0.1.0" edition = "2021" [target.'cfg(target_arch = "x86_64")'.dependencies] -tfhe = { version = "0.8.0-alpha.6", features = ["boolean", "shortint", "integer", "x86_64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] } +tfhe = { version = "0.8.0-alpha.8", features = ["boolean", "shortint", "integer", "x86_64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] } [target.'cfg(target_arch = "aarch64")'.dependencies] -tfhe = { version = "0.8.0-alpha.6", features = ["boolean", "shortint", "integer", "aarch64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] } +tfhe = { version = "0.8.0-alpha.8", features = ["boolean", "shortint", "integer", "aarch64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] } [dependencies] clap.workspace = true diff --git a/fhevm-engine/executor/tests/sync_compute.rs b/fhevm-engine/executor/tests/sync_compute.rs index 9b8915b6..f3e2a166 100644 --- a/fhevm-engine/executor/tests/sync_compute.rs +++ b/fhevm-engine/executor/tests/sync_compute.rs @@ -7,6 +7,7 @@ use executor::server::executor::{ use executor::server::executor::{sync_input::Input, SyncInput}; use executor::server::SyncComputeError; use fhevm_engine_common::types::{SupportedFheCiphertexts, HANDLE_LEN}; +use fhevm_engine_common::utils::safe_serialize; use tfhe::zk::ZkComputeLoad; use tfhe::ProvenCompactCiphertextList; use utils::get_test; @@ -21,13 +22,12 @@ async fn get_input_ciphertext() { .await .unwrap(); let mut builder = ProvenCompactCiphertextList::builder(&test.keys.compact_public_key); - let list = bincode::serialize( + let list = safe_serialize( &builder .push(10_u8) .build_with_proof_packed(&test.keys.public_params, &[], ZkComputeLoad::Proof) .unwrap(), - ) - .unwrap(); + ); // TODO: tests for all types and avoiding passing in 2 as an identifier for FheUint8. let input_handle = test.input_handle(&list, 0, 2); let sync_input = SyncInput { @@ -136,13 +136,12 @@ async fn compute_on_compact_and_serialized_ciphertexts() { .await .unwrap(); let mut builder_input = ProvenCompactCiphertextList::builder(&test.keys.compact_public_key); - let compact_list = bincode::serialize( + let compact_list = safe_serialize( &builder_input .push(10_u16) .build_with_proof_packed(&test.keys.public_params, &[], ZkComputeLoad::Proof) .unwrap(), - ) - .unwrap(); + ); let mut builder_cts = ProvenCompactCiphertextList::builder(&test.keys.compact_public_key); let list = builder_cts .push(11_u16) diff --git a/fhevm-engine/fhevm-engine-common/Cargo.toml b/fhevm-engine/fhevm-engine-common/Cargo.toml index 1458b2de..bde1fb0d 100644 --- a/fhevm-engine/fhevm-engine-common/Cargo.toml +++ b/fhevm-engine/fhevm-engine-common/Cargo.toml @@ -4,9 +4,9 @@ version = "0.1.0" edition = "2021" [target.'cfg(target_arch = "x86_64")'.dependencies] -tfhe = { version = "0.8.0-alpha.6", features = ["boolean", "shortint", "integer", "x86_64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] } +tfhe = { version = "0.8.0-alpha.8", features = ["boolean", "shortint", "integer", "x86_64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] } [target.'cfg(target_arch = "aarch64")'.dependencies] -tfhe = { version = "0.8.0-alpha.6", features = ["boolean", "shortint", "integer", "aarch64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] } +tfhe = { version = "0.8.0-alpha.8", features = ["boolean", "shortint", "integer", "aarch64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] } [dependencies] sha3.workspace = true @@ -17,6 +17,7 @@ hex = "0.4" bigdecimal = "0.4.5" rand_chacha = "0.3.1" rand = "0.8.5" +serde.workspace = true [[bin]] name = "generate-keys" diff --git a/fhevm-engine/fhevm-engine-common/src/lib.rs b/fhevm-engine/fhevm-engine-common/src/lib.rs index a8a4b244..b4e70476 100644 --- a/fhevm-engine/fhevm-engine-common/src/lib.rs +++ b/fhevm-engine/fhevm-engine-common/src/lib.rs @@ -1,3 +1,4 @@ pub mod keys; pub mod tfhe_ops; pub mod types; +pub mod utils; diff --git a/fhevm-engine/fhevm-engine-common/src/tfhe_ops.rs b/fhevm-engine/fhevm-engine-common/src/tfhe_ops.rs index d9b1d08c..f28c2542 100644 --- a/fhevm-engine/fhevm-engine-common/src/tfhe_ops.rs +++ b/fhevm-engine/fhevm-engine-common/src/tfhe_ops.rs @@ -1,5 +1,9 @@ -use crate::types::{ - is_ebytes_type, FheOperationType, FhevmError, SupportedFheCiphertexts, SupportedFheOperations, +use crate::{ + types::{ + is_ebytes_type, FheOperationType, FhevmError, SupportedFheCiphertexts, + SupportedFheOperations, + }, + utils::safe_deserialize, }; use tfhe::{ integer::{bigint::StaticUnsignedBigInt, U256}, @@ -7,8 +11,8 @@ use tfhe::{ CastInto, FheEq, FheMax, FheMin, FheOrd, FheTryTrivialEncrypt, IfThenElse, RotateLeft, RotateRight, }, - FheBool, FheUint1024, FheUint128, FheUint16, FheUint160, FheUint2048, FheUint256, FheUint32, - FheUint4, FheUint512, FheUint64, FheUint8, Seed, + FheBool, FheUint1024, FheUint128, FheUint16, FheUint160, FheUint2, FheUint2048, FheUint256, + FheUint32, FheUint4, FheUint512, FheUint64, FheUint8, Seed, }; pub fn deserialize_fhe_ciphertext( @@ -17,63 +21,51 @@ pub fn deserialize_fhe_ciphertext( ) -> Result { match input_type { 0 => { - let v: tfhe::FheBool = bincode::deserialize(input_bytes) - .map_err(|e| FhevmError::DeserializationError(e))?; + let v: tfhe::FheBool = safe_deserialize(input_bytes)?; Ok(SupportedFheCiphertexts::FheBool(v)) } 1 => { - let v: tfhe::FheUint4 = bincode::deserialize(input_bytes) - .map_err(|e| FhevmError::DeserializationError(e))?; + let v: tfhe::FheUint4 = safe_deserialize(input_bytes)?; Ok(SupportedFheCiphertexts::FheUint4(v)) } 2 => { - let v: tfhe::FheUint8 = bincode::deserialize(input_bytes) - .map_err(|e| FhevmError::DeserializationError(e))?; + let v: tfhe::FheUint8 = safe_deserialize(input_bytes)?; Ok(SupportedFheCiphertexts::FheUint8(v)) } 3 => { - let v: tfhe::FheUint16 = bincode::deserialize(input_bytes) - .map_err(|e| FhevmError::DeserializationError(e))?; + let v: tfhe::FheUint16 = safe_deserialize(input_bytes)?; Ok(SupportedFheCiphertexts::FheUint16(v)) } 4 => { - let v: tfhe::FheUint32 = bincode::deserialize(input_bytes) - .map_err(|e| FhevmError::DeserializationError(e))?; + let v: tfhe::FheUint32 = safe_deserialize(input_bytes)?; Ok(SupportedFheCiphertexts::FheUint32(v)) } 5 => { - let v: tfhe::FheUint64 = bincode::deserialize(input_bytes) - .map_err(|e| FhevmError::DeserializationError(e))?; + let v: tfhe::FheUint64 = safe_deserialize(input_bytes)?; Ok(SupportedFheCiphertexts::FheUint64(v)) } 6 => { - let v: tfhe::FheUint128 = bincode::deserialize(input_bytes) - .map_err(|e| FhevmError::DeserializationError(e))?; + let v: tfhe::FheUint128 = safe_deserialize(input_bytes)?; Ok(SupportedFheCiphertexts::FheUint128(v)) } 7 => { - let v: tfhe::FheUint160 = bincode::deserialize(input_bytes) - .map_err(|e| FhevmError::DeserializationError(e))?; + let v: tfhe::FheUint160 = safe_deserialize(input_bytes)?; Ok(SupportedFheCiphertexts::FheUint160(v)) } 8 => { - let v: tfhe::FheUint256 = bincode::deserialize(input_bytes) - .map_err(|e| FhevmError::DeserializationError(e))?; + let v: tfhe::FheUint256 = safe_deserialize(input_bytes)?; Ok(SupportedFheCiphertexts::FheUint256(v)) } 9 => { - let v: tfhe::FheUint512 = bincode::deserialize(input_bytes) - .map_err(|e| FhevmError::DeserializationError(e))?; + let v: tfhe::FheUint512 = safe_deserialize(input_bytes)?; Ok(SupportedFheCiphertexts::FheBytes64(v)) } 10 => { - let v: tfhe::FheUint1024 = bincode::deserialize(input_bytes) - .map_err(|e| FhevmError::DeserializationError(e))?; + let v: tfhe::FheUint1024 = safe_deserialize(input_bytes)?; Ok(SupportedFheCiphertexts::FheBytes128(v)) } 11 => { - let v: tfhe::FheUint2048 = bincode::deserialize(input_bytes) - .map_err(|e| FhevmError::DeserializationError(e))?; + let v: tfhe::FheUint2048 = safe_deserialize(input_bytes)?; Ok(SupportedFheCiphertexts::FheBytes256(v)) } _ => { @@ -239,11 +231,7 @@ pub fn try_expand_ciphertext_list( ) -> Result, FhevmError> { let mut res = Vec::new(); - let the_list: tfhe::ProvenCompactCiphertextList = bincode::deserialize(input_ciphertext) - .map_err(|e| { - let err: Box<(dyn std::error::Error + Send + Sync)> = e; - FhevmError::DeserializationError(err) - })?; + let the_list: tfhe::ProvenCompactCiphertextList = safe_deserialize(input_ciphertext)?; // TODO: we can do better and avoid cloning tfhe::set_server_key(server_key.clone()); @@ -3209,7 +3197,7 @@ pub fn generate_random_number( let subtract_from = 255; match the_type { 0 => { - let num = FheUint8::generate_oblivious_pseudo_random_bounded(Seed(seed), 1); + let num = FheUint2::generate_oblivious_pseudo_random_bounded(Seed(seed), 1); SupportedFheCiphertexts::FheBool(num.gt(0)) } 1 => { diff --git a/fhevm-engine/fhevm-engine-common/src/types.rs b/fhevm-engine/fhevm-engine-common/src/types.rs index 2f6637ca..f368c774 100644 --- a/fhevm-engine/fhevm-engine-common/src/types.rs +++ b/fhevm-engine/fhevm-engine-common/src/types.rs @@ -5,6 +5,8 @@ use tfhe::integer::U256; use tfhe::prelude::FheDecrypt; use tfhe::{CompressedCiphertextList, CompressedCiphertextListBuilder}; +use crate::utils::{safe_deserialize, safe_serialize}; + #[derive(Debug)] pub enum FhevmError { UnknownFheOperation(i32), @@ -233,7 +235,7 @@ impl std::fmt::Display for FhevmError { upper_bound_value, } => { write!(f, "rand bounded operation cannot receive zero as upper bound {fhe_operation} ({fhe_operation_name}) received: {}", upper_bound_value) - }, + } Self::RandOperationInputsMustAllBeScalar { fhe_operation, fhe_operation_name, @@ -315,18 +317,18 @@ impl SupportedFheCiphertexts { pub fn serialize(&self) -> (i16, Vec) { let type_num = self.type_num(); match self { - SupportedFheCiphertexts::FheBool(v) => (type_num, bincode::serialize(v).unwrap()), - SupportedFheCiphertexts::FheUint4(v) => (type_num, bincode::serialize(v).unwrap()), - SupportedFheCiphertexts::FheUint8(v) => (type_num, bincode::serialize(v).unwrap()), - SupportedFheCiphertexts::FheUint16(v) => (type_num, bincode::serialize(v).unwrap()), - SupportedFheCiphertexts::FheUint32(v) => (type_num, bincode::serialize(v).unwrap()), - SupportedFheCiphertexts::FheUint64(v) => (type_num, bincode::serialize(v).unwrap()), - SupportedFheCiphertexts::FheUint128(v) => (type_num, bincode::serialize(v).unwrap()), - SupportedFheCiphertexts::FheUint160(v) => (type_num, bincode::serialize(v).unwrap()), - SupportedFheCiphertexts::FheUint256(v) => (type_num, bincode::serialize(v).unwrap()), - SupportedFheCiphertexts::FheBytes64(v) => (type_num, bincode::serialize(v).unwrap()), - SupportedFheCiphertexts::FheBytes128(v) => (type_num, bincode::serialize(v).unwrap()), - SupportedFheCiphertexts::FheBytes256(v) => (type_num, bincode::serialize(v).unwrap()), + SupportedFheCiphertexts::FheBool(v) => (type_num, safe_serialize(v)), + SupportedFheCiphertexts::FheUint4(v) => (type_num, safe_serialize(v)), + SupportedFheCiphertexts::FheUint8(v) => (type_num, safe_serialize(v)), + SupportedFheCiphertexts::FheUint16(v) => (type_num, safe_serialize(v)), + SupportedFheCiphertexts::FheUint32(v) => (type_num, safe_serialize(v)), + SupportedFheCiphertexts::FheUint64(v) => (type_num, safe_serialize(v)), + SupportedFheCiphertexts::FheUint128(v) => (type_num, safe_serialize(v)), + SupportedFheCiphertexts::FheUint160(v) => (type_num, safe_serialize(v)), + SupportedFheCiphertexts::FheUint256(v) => (type_num, safe_serialize(v)), + SupportedFheCiphertexts::FheBytes64(v) => (type_num, safe_serialize(v)), + SupportedFheCiphertexts::FheBytes128(v) => (type_num, safe_serialize(v)), + SupportedFheCiphertexts::FheBytes256(v) => (type_num, safe_serialize(v)), SupportedFheCiphertexts::Scalar(_) => { panic!("we should never need to serialize scalar") } @@ -374,38 +376,38 @@ impl SupportedFheCiphertexts { } SupportedFheCiphertexts::FheUint128(v) => { FheDecrypt::::decrypt(v, client_key).to_string() - }, + } SupportedFheCiphertexts::FheUint160(v) => { let dec = FheDecrypt::::decrypt(v, client_key); let mut slice: [u8; 32] = [0; 32]; dec.copy_to_be_byte_slice(&mut slice); - let final_slice = &slice[slice.len()-20..]; + let final_slice = &slice[slice.len() - 20..]; BigInt::from_bytes_be(bigdecimal::num_bigint::Sign::Plus, &final_slice).to_string() - }, + } SupportedFheCiphertexts::FheUint256(v) => { let dec = FheDecrypt::::decrypt(v, client_key); let mut slice: [u8; 32] = [0; 32]; dec.copy_to_be_byte_slice(&mut slice); BigInt::from_bytes_be(bigdecimal::num_bigint::Sign::Plus, &slice).to_string() - }, + } SupportedFheCiphertexts::FheBytes64(v) => { let dec = FheDecrypt::>::decrypt(v, client_key); let mut slice: [u8; 64] = [0; 64]; dec.copy_to_be_byte_slice(&mut slice); BigInt::from_bytes_be(bigdecimal::num_bigint::Sign::Plus, &slice).to_string() - }, + } SupportedFheCiphertexts::FheBytes128(v) => { let dec = FheDecrypt::>::decrypt(v, client_key); let mut slice: [u8; 128] = [0; 128]; dec.copy_to_be_byte_slice(&mut slice); BigInt::from_bytes_be(bigdecimal::num_bigint::Sign::Plus, &slice).to_string() - }, + } SupportedFheCiphertexts::FheBytes256(v) => { let dec = FheDecrypt::>::decrypt(v, client_key); let mut slice: [u8; 256] = [0; 256]; dec.copy_to_be_byte_slice(&mut slice); BigInt::from_bytes_be(bigdecimal::num_bigint::Sign::Plus, &slice).to_string() - }, + } SupportedFheCiphertexts::Scalar(v) => { let (l, h) = v.to_low_high_u128(); format!("{l}{h}") @@ -434,11 +436,11 @@ impl SupportedFheCiphertexts { } }; let list = builder.build().expect("ciphertext compression"); - bincode::serialize(&list).expect("compressed list serialization") + safe_serialize(&list) } pub fn decompress(ct_type: i16, list: &[u8]) -> Result { - let list: CompressedCiphertextList = bincode::deserialize(list)?; + let list: CompressedCiphertextList = safe_deserialize(list)?; match ct_type { 0 => Ok(SupportedFheCiphertexts::FheBool( list.get(0)?.ok_or(FhevmError::MissingTfheRsData)?, @@ -528,9 +530,7 @@ impl SupportedFheOperations { SupportedFheOperations::FheIfThenElse | SupportedFheOperations::FheCast | SupportedFheOperations::FheRand - | SupportedFheOperations::FheRandBounded => { - FheOperationType::Other - } + | SupportedFheOperations::FheRandBounded => FheOperationType::Other, SupportedFheOperations::FheGetInputCiphertext => FheOperationType::Other, } } @@ -549,8 +549,7 @@ impl SupportedFheOperations { pub fn is_random(&self) -> bool { match self { - SupportedFheOperations::FheRand - | SupportedFheOperations::FheRandBounded => true, + SupportedFheOperations::FheRand | SupportedFheOperations::FheRandBounded => true, _ => false, } } @@ -584,9 +583,7 @@ impl SupportedFheOperations { | SupportedFheOperations::FheNot | SupportedFheOperations::FheNeg | SupportedFheOperations::FheIfThenElse - | SupportedFheOperations::FheCast => { - true - }, + | SupportedFheOperations::FheCast => true, SupportedFheOperations::FheAdd | SupportedFheOperations::FheSub | SupportedFheOperations::FheMul @@ -594,9 +591,7 @@ impl SupportedFheOperations { | SupportedFheOperations::FheRem | SupportedFheOperations::FheRand | SupportedFheOperations::FheRandBounded - | SupportedFheOperations::FheGetInputCiphertext => { - false - }, + | SupportedFheOperations::FheGetInputCiphertext => false, } } } @@ -679,4 +674,4 @@ pub fn get_ct_type(handle: &[u8]) -> Result { pub fn is_ebytes_type(inp: i16) -> bool { inp >= 9 && inp <= 11 -} \ No newline at end of file +} diff --git a/fhevm-engine/fhevm-engine-common/src/utils.rs b/fhevm-engine/fhevm-engine-common/src/utils.rs new file mode 100644 index 00000000..54a30f5c --- /dev/null +++ b/fhevm-engine/fhevm-engine-common/src/utils.rs @@ -0,0 +1,17 @@ +use serde::{de::DeserializeOwned, Serialize}; +use tfhe::named::Named; + +use crate::types::FhevmError; + +pub const SAFE_SER_DESER_LIMIT: u64 = 1024 * 1024 * 512; + +pub fn safe_serialize(object: &T) -> Vec { + let mut out = vec![]; + tfhe::safe_serialize(object, &mut out, SAFE_SER_DESER_LIMIT).expect("safe serialize succeeds"); + out +} + +pub fn safe_deserialize(input: &[u8]) -> Result { + tfhe::safe_deserialization::safe_deserialize(input, SAFE_SER_DESER_LIMIT) + .map_err(|e| FhevmError::DeserializationError(e.into())) +} diff --git a/fhevm-engine/fhevm-keys/cks b/fhevm-engine/fhevm-keys/cks index 36d78b1d..5eae31f0 100644 --- a/fhevm-engine/fhevm-keys/cks +++ b/fhevm-engine/fhevm-keys/cks @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d006cc95d39b68c4a248097e68c713353e7522923621b47fee1c20c5b55cf7f8 +oid sha256:4e433d085c604f438d54d0228246d09423e576bd63a85307566a8710244eae01 size 33982 diff --git a/fhevm-engine/fhevm-keys/pks b/fhevm-engine/fhevm-keys/pks index ea8e611d..c6914f32 100644 --- a/fhevm-engine/fhevm-keys/pks +++ b/fhevm-engine/fhevm-keys/pks @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4692b9c824ac924dc3e089692daabb98baf89efc85c8f47697aba3208d8fc6fe +oid sha256:511703808ed8e1135934262e89662a598035b414ee7d6128f902cfb83efe72fe size 32880 diff --git a/fhevm-engine/fhevm-keys/pp b/fhevm-engine/fhevm-keys/pp index c2a683b0..9601e1f7 100644 --- a/fhevm-engine/fhevm-keys/pp +++ b/fhevm-engine/fhevm-keys/pp @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:1e43319038374cec08ba58d4f5dcb5f661949b8777c80d26ad133bf0323b3657 +oid sha256:d7a596b243a8d0bf1142b1dd0d37d07e0520ed6481684c495c9478eee2215522 size 37124128 diff --git a/fhevm-engine/fhevm-keys/sks b/fhevm-engine/fhevm-keys/sks index be33d3be..70f2b705 100644 --- a/fhevm-engine/fhevm-keys/sks +++ b/fhevm-engine/fhevm-keys/sks @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:076ae14603e5b3bae9443384d29793e9184d43f6fa2ef5c13785bbb281254b85 +oid sha256:f414b6f2e57d8a2112687d4a34513e1510660390981c3ff793dd9e142fc64b0f size 265162799