Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update to tfhe-rs 0.8.0 #46

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions fhevm-engine/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions fhevm-engine/coprocessor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ version = "0.1.0"
edition = "2021"

[target.'cfg(target_arch = "x86_64")'.dependencies]
tfhe = { version = "0.8.0-alpha.9", features = ["boolean", "shortint", "integer", "x86_64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] }
tfhe = { version = "0.8.0", 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.9", features = ["boolean", "shortint", "integer", "aarch64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] }
tfhe = { version = "0.8.0", features = ["boolean", "shortint", "integer", "aarch64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] }

[dependencies]
# Common dependencies
Expand Down
15 changes: 6 additions & 9 deletions fhevm-engine/coprocessor/src/db_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str::FromStr;

use crate::server::GrpcTracer;
use crate::types::{CoprocessorError, TfheTenantKeys};
use fhevm_engine_common::utils::{safe_deserialize_versioned, safe_deserialize_versioned_sks};
use fhevm_engine_common::utils::safe_deserialize_key;
use opentelemetry::trace::Span;
use opentelemetry::KeyValue;
use sqlx::{query, Postgres};
Expand Down Expand Up @@ -158,16 +158,13 @@ where
.await?;

for key in keys {
let sks: tfhe::ServerKey = safe_deserialize_versioned_sks(&key.sks_key)
let sks: tfhe::ServerKey = safe_deserialize_key(&key.sks_key)
.expect("We can't deserialize our own validated sks key");
let pks: tfhe::CompactPublicKey = safe_deserialize_versioned(&key.pks_key)
let pks: tfhe::CompactPublicKey = safe_deserialize_key(&key.pks_key)
.expect("We can't deserialize our own validated pks key");
let public_params = <tfhe::zk::CompactPkePublicParams as tfhe::zk::CanonicalDeserialize>::deserialize_with_mode(
&*key.public_params,
tfhe::zk::Compress::No,
tfhe::zk::Validate::No,
)
.expect("We can't deserialize our own validated public params");
let public_params: tfhe::zk::CompactPkePublicParams =
safe_deserialize_key(&key.public_params)
.expect("We can't deserialize our own validated public params");
res.push(TfheTenantKeys {
tenant_id: key.tenant_id,
sks,
Expand Down
4 changes: 2 additions & 2 deletions fhevm-engine/coprocessor/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use fhevm_engine_common::tfhe_ops::{
try_expand_ciphertext_list, validate_fhe_type,
};
use fhevm_engine_common::types::{FhevmError, SupportedFheCiphertexts, SupportedFheOperations};
use fhevm_engine_common::utils::safe_deserialize_versioned_sks;
use fhevm_engine_common::utils::safe_deserialize_key;
use lazy_static::lazy_static;
use opentelemetry::global::{BoxedSpan, BoxedTracer};
use opentelemetry::trace::{Span, TraceContextExt, Tracer};
Expand Down Expand Up @@ -775,7 +775,7 @@ impl CoprocessorService {
let mut outer_span = tracer.child_span("blocking_trivial_encrypt");
let out_cts = tokio::task::spawn_blocking(move || {
let mut span = inner_tracer.child_span("deserialize_and_set_sks");
let server_key: tfhe::ServerKey = safe_deserialize_versioned_sks(&sks.sks_key).unwrap();
let server_key: tfhe::ServerKey = safe_deserialize_key(&sks.sks_key).unwrap();
tfhe::set_server_key(server_key);
span.end();

Expand Down
36 changes: 18 additions & 18 deletions fhevm-engine/coprocessor/src/tests/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,17 @@ async fn test_fhe_random_basic() -> Result<(), Box<dyn std::error::Error>> {
let resp = decrypt_ciphertexts(&pool, 1, decrypt_request).await?;
let expected: Vec<DecryptionResult> = vec![
DecryptionResult { value: "false".to_string(), output_type: 0 },
DecryptionResult { value: "0".to_string(), output_type: 1 },
DecryptionResult { value: "208".to_string(), output_type: 2 },
DecryptionResult { value: "18384".to_string(), output_type: 3 },
DecryptionResult { value: "546654160".to_string(), output_type: 4 },
DecryptionResult { value: "5309702218429319120".to_string(), output_type: 5 },
DecryptionResult { value: "259532418979733305351113308189425485776".to_string(), output_type: 6 },
DecryptionResult { value: "1267867068236038429610894726805758983204846782416".to_string(), output_type: 7 },
DecryptionResult { value: "30164121005611094063260660418297722452727051469520960924993268496981611071440".to_string(), output_type: 8 },
DecryptionResult { value: "6213722850039064669433671652647529255789532115960532372768030528069295575413451947625020803543441293820836550947188152513313074353655122817702324075841488".to_string(), output_type: 9 },
DecryptionResult { value: "135596181018050014151440026328436435096551725053732154262022484620871840987960807275950086972107187870322269980270968306992213801802722113379074637321577464212174225858453662873604986351675422583583022204649943087340917217757907768317732046913567629376792967659037503980904999476225028235881045286170343589840".to_string(), output_type: 10 },
DecryptionResult { value: "20926228578992516717417088826312477563346530647238102649041733547761442048502490725118956138164779152604543876429923941857666356145969053143096763294553340917748520683808183210744459554376313461218407555416764225017989632233017323561671951549366342206122891759465623534776122756268283899411003375412445896653512731607929423488100112560489030070895391125926875840684946772723738520663564318353443151393937189264351070849901049341958499762590673504671858266930627927728298335449590844376609353638317270493141629796367937654152469055194322006100677265326779422590457512219671735471500255183382079593262797753352340195280".to_string(), output_type: 11 }
DecryptionResult { value: "12".to_string(), output_type: 1 },
DecryptionResult { value: "28".to_string(), output_type: 2 },
DecryptionResult { value: "11036".to_string(), output_type: 3 },
DecryptionResult { value: "4210699036".to_string(), output_type: 4 },
DecryptionResult { value: "16344510723587189532".to_string(), output_type: 5 },
DecryptionResult { value: "136613649354833719176509443219471870748".to_string(), output_type: 6 },
DecryptionResult { value: "591363665230987812468666261651371579213130378012".to_string(), output_type: 7 },
DecryptionResult { value: "36975974909332520374266082483230454203705533525553393508422531302674631633692".to_string(), output_type: 8 },
DecryptionResult { value: "3326473935649069820950190113974858702563126997842506184772364084379288002799388440475644142369974102118684705625192196569242540629776740820021874404240156".to_string(), output_type: 9 },
DecryptionResult { value: "67228519144235361528163080870023642189372018339038974888803233372513746305170161682213639048818750609438947852635103936657279245767388355369360459437058019384020620546105706709782910022926513415199095325084179587796282182018635825899937073512226837931796770579317989122097448831219127297643296517567830174492".to_string(), output_type: 10 },
DecryptionResult { value: "26920374933019397363919102461740542820117613763813132144807740772483479765175833446829138381880546503235854012422168933572520304055369469954014214199406279316867905690284304295806440063864584911726946311557319966680736114033979639916024969840755036047902712883035150478290439430414911346811818704787544188469137115163021399536700621345967863285233994144754988274944685028835593424108169617615600872145086287438196489215604320879747992340881217564567463858602347322566997197490357100709844912206375614462243644572582725720254270613345104375017605321216618774963056885955516133520765131507335300883235207604800916695836".to_string(), output_type: 11 }
];

println!("results: {:#?}", resp);
Expand Down Expand Up @@ -188,13 +188,13 @@ async fn test_fhe_random_bounded() -> Result<(), Box<dyn std::error::Error>> {
let results = [
"false",
"0",
"80",
"2000",
"546654160",
"698016200001931216",
"4320643789029457753582352615599327184",
"171740840237861240958131102268546718462897375184",
"1216098696282045207367914166125745489409555303110819915128872495003328661456",
"28",
"11036",
"989473564",
"2509452668305025820",
"51543057624599103310665791361529817884",
"225988255898262082917745053472300824299147242268",
"8027952600003471518373336231058477240388037359143252498558135300696349223708",
];

for (idx, the_type) in supported_types().iter().enumerate() {
Expand Down
7 changes: 3 additions & 4 deletions fhevm-engine/coprocessor/src/tests/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::cli::Args;
use fhevm_engine_common::tfhe_ops::current_ciphertext_version;
use fhevm_engine_common::types::SupportedFheCiphertexts;
use fhevm_engine_common::utils::{safe_deserialize_versioned, safe_deserialize_versioned_sks};
use fhevm_engine_common::utils::{safe_deserialize, safe_deserialize_key};
use rand::Rng;
use std::collections::BTreeMap;
use std::sync::atomic::{AtomicU16, Ordering};
Expand Down Expand Up @@ -287,9 +287,8 @@ pub async fn decrypt_ciphertexts(
let keys = keys.pop().unwrap();

let mut values = tokio::task::spawn_blocking(move || {
let client_key: tfhe::ClientKey =
safe_deserialize_versioned(&keys.cks_key.clone().unwrap()).unwrap();
let sks: tfhe::ServerKey = safe_deserialize_versioned_sks(&keys.sks_key).unwrap();
let client_key: tfhe::ClientKey = safe_deserialize(&keys.cks_key.clone().unwrap()).unwrap();
let sks: tfhe::ServerKey = safe_deserialize_key(&keys.sks_key).unwrap();
tfhe::set_server_key(sks);

let mut decrypted: Vec<(Vec<u8>, DecryptionResult)> = Vec::with_capacity(cts.len());
Expand Down
4 changes: 2 additions & 2 deletions fhevm-engine/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ version = "0.1.0"
edition = "2021"

[target.'cfg(target_arch = "x86_64")'.dependencies]
tfhe = { version = "0.8.0-alpha.9", features = ["boolean", "shortint", "integer", "x86_64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] }
tfhe = { version = "0.8.0", 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.9", features = ["boolean", "shortint", "integer", "aarch64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] }
tfhe = { version = "0.8.0", features = ["boolean", "shortint", "integer", "aarch64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] }

[dependencies]
clap.workspace = true
Expand Down
1 change: 1 addition & 0 deletions fhevm-engine/executor/tests/sync_compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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::prelude::CiphertextList;
use tfhe::zk::ZkComputeLoad;
use tfhe::ProvenCompactCiphertextList;
use utils::get_test;
Expand Down
8 changes: 4 additions & 4 deletions fhevm-engine/fhevm-engine-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ version = "0.1.0"
edition = "2021"

[target.'cfg(target_arch = "x86_64")'.dependencies]
tfhe = { version = "0.8.0-alpha.9", features = ["boolean", "shortint", "integer", "x86_64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] }
tfhe = { version = "0.8.0", 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.9", features = ["boolean", "shortint", "integer", "aarch64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] }
tfhe = { version = "0.8.0", features = ["boolean", "shortint", "integer", "aarch64-unix", "zk-pok", "experimental-force_fft_algo_dif4"] }

[dependencies]
sha3.workspace = true
anyhow.workspace = true
bincode.workspace = true
serde.workspace = true
strum = { version = "0.26", features = ["derive"] }
bincode = "1.3.3"
hex = "0.4"
bigdecimal = "0.4.5"
rand_chacha = "0.3.1"
rand = "0.8.5"
serde.workspace = true

[[bin]]
name = "generate-keys"
Expand Down
34 changes: 11 additions & 23 deletions fhevm-engine/fhevm-engine-common/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ use tfhe::{
list_compression::COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
},
zk::{
CanonicalDeserialize, CanonicalSerialize, CompactPkeCrs, CompactPkePublicParams, Compress,
Validate,
},
zk::{CompactPkeCrs, CompactPkePublicParams},
ClientKey, CompactPublicKey, ConfigBuilder, ServerKey,
};

use crate::utils::{safe_deserialize_versioned, safe_deserialize_versioned_sks, safe_serialize_versioned, safe_serialize_versioned_sks};
use crate::utils::{safe_deserialize_key, safe_serialize_key};

pub const MAX_BITS_TO_PROVE: usize = 2048;

Expand Down Expand Up @@ -97,35 +94,26 @@ impl SerializedFhevmKeys {

impl From<FhevmKeys> for SerializedFhevmKeys {
fn from(f: FhevmKeys) -> Self {
let mut public_params = vec![];
f.public_params
.serialize_with_mode(&mut public_params, Compress::No)
.expect("serialize public params");
SerializedFhevmKeys {
server_key: safe_serialize_versioned_sks(&f.server_key),
client_key: f.client_key.map(|c| safe_serialize_versioned(&c)),
compact_public_key: safe_serialize_versioned(&f.compact_public_key),
public_params,
server_key: safe_serialize_key(&f.server_key),
client_key: f.client_key.map(|c| safe_serialize_key(&c)),
compact_public_key: safe_serialize_key(&f.compact_public_key),
public_params: safe_serialize_key(&f.public_params),
}
}
}

impl From<SerializedFhevmKeys> for FhevmKeys {
fn from(f: SerializedFhevmKeys) -> Self {
let public_params = CompactPkePublicParams::deserialize_with_mode(
&*f.public_params,
Compress::No,
Validate::No,
)
.expect("deserialize public params");
FhevmKeys {
server_key: safe_deserialize_versioned_sks(&f.server_key).expect("deserialize server key"),
server_key: safe_deserialize_key(&f.server_key).expect("deserialize server key"),
client_key: f
.client_key
.map(|c| safe_deserialize_versioned(&c).expect("deserialize client key")),
compact_public_key: safe_deserialize_versioned(&f.compact_public_key)
.map(|c| safe_deserialize_key(&c).expect("deserialize client key")),
compact_public_key: safe_deserialize_key(&f.compact_public_key)
.expect("deserialize compact public key"),
public_params,
public_params: safe_deserialize_key(&f.public_params)
.expect("deserialize public params"),
}
}
}
Loading