Skip to content

Commit

Permalink
Merge branch 'main' into georgi/sns-keys-from-db
Browse files Browse the repository at this point in the history
  • Loading branch information
goshawk-3 authored Feb 19, 2025
2 parents 001d9df + b826fbf commit 64190c0
Show file tree
Hide file tree
Showing 19 changed files with 298 additions and 220 deletions.
349 changes: 217 additions & 132 deletions fhevm-engine/Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion fhevm-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rayon = "1.10.0"
serde = "1.0.210"
sha3 = "0.10.8"
sqlx = { version = "0.8.3", features = ["runtime-tokio", "tls-rustls", "time", "postgres", "uuid"] }
tfhe = { version = "0.10.0", features = ["boolean", "shortint", "integer", "zk-pok", "experimental-force_fft_algo_dif4"] }
tfhe = { version = "1.0.0", features = ["boolean", "shortint", "integer", "zk-pok", "experimental-force_fft_algo_dif4"] }
tokio = { version = "1.38.0", features = ["full"] }
tokio-util = "0.7"
tonic = { version = "0.12", features = ["server"] }
Expand Down
7 changes: 1 addition & 6 deletions fhevm-engine/coprocessor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rand = { workspace = true }
rayon = { workspace = true }
sha3 = { workspace = true }
sqlx = { workspace = true }
tfhe = { workspace = true }
tokio = { workspace = true }
tokio-util = { workspace = true }
tonic = { workspace = true }
Expand Down Expand Up @@ -45,12 +46,6 @@ tonic-web = "0.12"
fhevm-engine-common = { path = "../fhevm-engine-common" }
scheduler = { path = "../scheduler" }

# arch-specific dependencies
[target.'cfg(target_arch = "x86_64")'.dependencies]
tfhe = { workspace = true, features = ["x86_64-unix"] }
[target.'cfg(target_arch = "aarch64")'.dependencies]
tfhe = { workspace = true, features = ["aarch64-unix"] }

[features]
nightly-avx512 = ["tfhe/nightly-avx512"]

Expand Down
4 changes: 2 additions & 2 deletions fhevm-engine/coprocessor/src/db_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct FetchTenantKeyResult {
pub verifying_contract_address: String,
pub acl_contract_address: String,
pub server_key: tfhe::ServerKey,
pub public_params: Arc<tfhe::zk::CompactPkePublicParams>,
pub public_params: Arc<tfhe::zk::CompactPkeCrs>,
}

/// Returns chain id and verifying contract address for EIP712 signature and tfhe server key
Expand Down Expand Up @@ -118,7 +118,7 @@ where
.expect("We can't deserialize our own validated sks 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 =
let public_params: tfhe::zk::CompactPkeCrs =
safe_deserialize_key(&key.public_params)
.expect("We can't deserialize our own validated public params");
res.push(TfheTenantKeys {
Expand Down
23 changes: 21 additions & 2 deletions fhevm-engine/coprocessor/src/tests/operators_from_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ use fhevm_listener::contracts::TfheContract::TfheContractEvents;
use fhevm_listener::database::tfhe_event_propagate::{Database as ListenerDatabase, Handle, ToType};


use crate::tests::operators::{generate_binary_test_cases, generate_unary_test_cases, supported_types};
use crate::tests::operators::{generate_binary_test_cases, generate_unary_test_cases};
use crate::tests::utils::{default_api_key, setup_test_app, TestInstance};
use crate::tests::utils::{decrypt_ciphertexts, wait_until_all_ciphertexts_computed};

use crate::tests::operators::BinaryOperatorTestCase;
use crate::tests::operators::UnaryOperatorTestCase;

pub fn supported_types() -> &'static [i32] {
&[
0, // bool
8, // 256 bit
9, // ebytes 64
]
}

fn tfhe_event(data: TfheContractEvents) -> Log<TfheContractEvents> {
let address = "0x0000000000000000000000000000000000000000".parse().unwrap();
Log::<TfheContractEvents>{address, data}
Expand Down Expand Up @@ -105,7 +113,11 @@ async fn test_fhe_binary_operands_events() -> Result<(), Box<dyn std::error::Err
.connect(app.db_url())
.await?;
let mut listener_event_to_db = listener_event_to_db(&app).await;
let mut cases = vec![];
for op in generate_binary_test_cases() {
if !supported_types().contains(&op.input_types) {
continue;
}
let support_bytes = match S::try_from(op.operand).unwrap() {
S::FheEq | S::FheNe => true,
_ => false
Expand Down Expand Up @@ -141,8 +153,12 @@ async fn test_fhe_binary_operands_events() -> Result<(), Box<dyn std::error::Err
eprintln!("op_event: {:?}", &op_event);
listener_event_to_db.insert_tfhe_event(&tfhe_event(op_event)).await?;
listener_event_to_db.notify_scheduler().await;
wait_until_all_ciphertexts_computed(&app).await?;

cases.push((op, output_handle));
}

wait_until_all_ciphertexts_computed(&app).await?;
for (op, output_handle) in cases {
let decrypt_request = vec![output_handle.to_be_bytes_vec()];
let resp = decrypt_ciphertexts(&pool, 1, decrypt_request).await?;
let decr_response = &resp[0];
Expand Down Expand Up @@ -195,6 +211,9 @@ async fn test_fhe_unary_operands_events() -> Result<(), Box<dyn std::error::Erro
let mut listener_event_to_db = listener_event_to_db(&app).await;

for op in &ops {
if !supported_types().contains(&op.operand_types) {
continue;
}
let input_handle = next_handle();
let output_handle = next_handle();

Expand Down
46 changes: 24 additions & 22 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: "true".to_string(), output_type: 0 },
DecryptionResult { value: "2".to_string(), output_type: 1 },
DecryptionResult { value: "178".to_string(), output_type: 2 },
DecryptionResult { value: "14770".to_string(), output_type: 3 },
DecryptionResult { value: "3233298866".to_string(), output_type: 4 },
DecryptionResult { value: "14612480407908137394".to_string(), output_type: 5 },
DecryptionResult { value: "237341639434117301949087796196011424178".to_string(), output_type: 6 },
DecryptionResult { value: "963123578337087940297602368436260272041228777906".to_string(), output_type: 7 },
DecryptionResult { value: "4957159431224693793635339084646955011298500432818337630852092398857805642162".to_string(), output_type: 8 },
DecryptionResult { value: "4273313508171339583832544762733351218893742780063352150052771784030182678151261952648007688185758228439634451766592375629798049346674851354419119309470130".to_string(), output_type: 9 },
DecryptionResult { value: "147710808730159615890939873041029966358705523918476796360551473211224263374716311378642610380445720764139714191331771559563342278307452243881320765758551233150238014060664160213992760833678010280572160460895656022873333070898090481975964124185236046652639979678922115388738351590210798742932473914295259249074".to_string(), output_type: 10 },
DecryptionResult { value: "16296031029837480926749218699301227055776806083009811119047674979647284833689081592903436049320655412031510959925234064816165310905500258211391504853839153749946669070701725946853107869887627621985486701964105738157103898322739684866199567823597098153332582435696344805510671844237519839878108950797382621828675803356788564480413402412760527858644546400119874052868832987355781072938125917127259446283139261251620742940397422094439406595073078630085605956483312381099073522390688035728162740142810788566937684999310535339800060223096345147502113221086953295432501391327216431698138260598828856305250139847746494413234".to_string(), output_type: 11 }
DecryptionResult { value: "10".to_string(), output_type: 1 },
DecryptionResult { value: "74".to_string(), output_type: 2 },
DecryptionResult { value: "20042".to_string(), output_type: 3 },
DecryptionResult { value: "2317110858".to_string(), output_type: 4 },
DecryptionResult { value: "3781517732040429130".to_string(), output_type: 5 },
DecryptionResult { value: "173067494276272686594848549919321247306".to_string(), output_type: 6 },
DecryptionResult { value: "1094515384870572566830339459490292190735847149130".to_string(), output_type: 7 },
DecryptionResult { value: "708862794572017520787824189501889310665101791528313059693660933975816556106".to_string(), output_type: 8 },
DecryptionResult { value: "12730235082833568380334594754488662004233832431992980449804758406069160864275028020962155601245523679819346268699252919365791199191241634613862853049601610".to_string(), output_type: 9 },
DecryptionResult { value: "108080741562249556931009330483536710085758122079454929899567131003800646884284657303009577197577435564912115080988607041335379236995946564760399287610685200564494202954787898550157622716115053586508022488999714877512770859433667427323545700100098005683083248085496846062049536709006011184102093099103655054922".to_string(), output_type: 10 },
DecryptionResult { value: "20861508589877342907727084703622550598935352998746120842950395081753632435267183114988504993162213367166862517403829273790814314107740319854089900370739997411799274100417555415192972122100470155923419604034291807093541986108659977790911517837161376588397655175961399498725202642377614760931166783035115963547101181507813456254749873668327253075828261674997156280447164928833111149986197323155994656673039741155124831297907623530345138812274608251091216970172997278005915715893955210806849703477333174085073707097135005438554387123608323887293974874745441977610516712982266319433597579122608490187852748993192739491402".to_string(), output_type: 11 }
];

println!("results: {:#?}", resp);
Expand Down Expand Up @@ -191,16 +191,16 @@ async fn test_fhe_random_bounded() -> Result<(), Box<dyn std::error::Error>> {
let results = [
"true",
"2",
"114",
"14770",
"12073394",
"777422352625973682",
"67200455973648070217400492480127318450",
"232372759671636481195759952078118762213262506418",
"4957159431224693793635339084646955011298500432818337630852092398857805642162",
"921361525685690308939038513181889687023901324915253805621881423099741670632875208447539113644032371517126487219970862916359578643688208867810707057949106",
"12883823615485922811241983731853111337357250497803803405478912342924756520590589029111252388640068748299628781428226541319249951696639777011685192778945639866912093992015046256766096132743439690982348246831901696244720059141243320870529291279615618780127096665482391708777917136351011514305956667077091146162",
"137527994181977276391780354966251075554754748152069102982502265884957264255136146304835343559198680187151979464285055069105531330254797663847428660615012189631230387051227900978008994692801568587457882772071954018707789001429806785280520654359012918041759509678192284066883898466986935574332751235417429067718636661954393270070915019478243111216458382456713023829930157690267976583895760052130149851050702888648941081166493415640231018922255677883757149866668765485510680035277930865584189279345126832098413708982186372277061588919845828394350637892234627660700323610601629375479021296352782208472213042216696297906",
"10",
"3658",
"169627210",
"3781517732040429130",
"2926310815803454863161246203437141578",
"363764566205121107728497043132150680907880877642",
"708862794572017520787824189501889310665101791528313059693660933975816556106",
"2674379135376620555654076005834277408624308066548685416512087323277837841719867788360749877620346109051822375059388381225475787082281707154037616295038538",
"18196084819133761544544070944085473404859273132339601262852090424934308981534175736655338536373667554352058141052910362505984352588738253513975572290948138375610256242355155912006512915492006526781481012957211993093695518262435986586589144829684387101407992743203696942075913739766153031684414934291542986314",
"4703005554221839257369646359287574618713301663888378826885222367991304865833237668389904287400756635322503536942880264043754534532494859306545824177515855851483835416767057369317873246905644102525390784842258022955145876787350099709992470667923291353106832249943246977281414696607081856627390583473150770786144014812979285044407386275044968328400173657333995251408262099167598053631967166080865360240951182792153029438676694851545963236123785298889368163556353662392352873538545105944271152613867512350234435806806656471031388489431824568186212291550723309838715645265651517110938339820132416091074822187662941376074",
];

for (idx, the_type) in supported_types().iter().enumerate() {
Expand Down Expand Up @@ -247,7 +247,9 @@ async fn test_fhe_random_bounded() -> Result<(), Box<dyn std::error::Error>> {
assert_eq!(resp[idx].value, results[idx]);
// skip boolean bounds check
if resp[idx].output_type > 0 {
assert!(BigInt::from_str(bounds[idx]).unwrap().gt(&BigInt::from_str(&resp[idx].value).unwrap()));
assert!(BigInt::from_str(bounds[idx])
.unwrap()
.gt(&BigInt::from_str(&resp[idx].value).unwrap()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion fhevm-engine/coprocessor/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,5 @@ pub struct TfheTenantKeys {
#[allow(dead_code)]
pub pks: tfhe::CompactPublicKey,
#[allow(dead_code)]
pub public_params: Arc<tfhe::zk::CompactPkePublicParams>,
pub public_params: Arc<tfhe::zk::CompactPkeCrs>,
}
7 changes: 1 addition & 6 deletions fhevm-engine/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@ daggy = { workspace = true }
prost = { workspace = true }
rayon = { workspace = true }
sha3 = { workspace = true }
tfhe = { workspace = true }
tokio = { workspace = true }
tonic = { workspace = true }

# local dependencies
fhevm-engine-common = { path = "../fhevm-engine-common" }
scheduler = { path = "../scheduler" }

# arch-specific dependencies
[target.'cfg(target_arch = "x86_64")'.dependencies]
tfhe = { workspace = true, features = ["x86_64-unix"] }
[target.'cfg(target_arch = "aarch64")'.dependencies]
tfhe = { workspace = true, features = ["aarch64-unix"] }

[features]
nightly-avx512 = ["tfhe/nightly-avx512"]

Expand Down
4 changes: 2 additions & 2 deletions fhevm-engine/executor/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use fhevm_engine_common::{
};
use sha3::{Digest, Keccak256};
use std::{cell::Cell, collections::HashMap};
use tfhe::{set_server_key, zk::CompactPkePublicParams};
use tfhe::{set_server_key, zk::CompactPkeCrs};
use tokio::task::spawn_blocking;
use tonic::{transport::Server, Code, Request, Response, Status};

Expand Down Expand Up @@ -184,7 +184,7 @@ impl FhevmExecutorService {
fn expand_compact_lists(
lists: &Vec<Vec<u8>>,
state: &mut ComputationState,
public_params: &CompactPkePublicParams,
public_params: &CompactPkeCrs,
) -> Result<(), FhevmError> {
for list in lists {
let cts = try_expand_ciphertext_list(&list, &public_params)?;
Expand Down
6 changes: 0 additions & 6 deletions fhevm-engine/fhevm-engine-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ paste = "1.0.15"
rand_chacha = "0.3.1"
strum = { version = "0.26", features = ["derive"] }

# arch-specific dependencies
[target.'cfg(target_arch = "x86_64")'.dependencies]
tfhe = { workspace = true, features = ["x86_64-unix"] }
[target.'cfg(target_arch = "aarch64")'.dependencies]
tfhe = { workspace = true, features = ["aarch64-unix"] }

[features]
nightly-avx512 = ["tfhe/nightly-avx512"]

Expand Down
22 changes: 11 additions & 11 deletions fhevm-engine/fhevm-engine-common/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ use tfhe::{
generate_keys, set_server_key,
shortint::{
parameters::{
compact_public_key_only::p_fail_2_minus_64::ks_pbs::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
key_switching::p_fail_2_minus_64::ks_pbs::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
list_compression::COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
v1_0::compact_public_key_only::p_fail_2_minus_128::ks_pbs::V1_0_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
v1_0::key_switching::p_fail_2_minus_128::ks_pbs::V1_0_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
v1_0::list_compression::V1_0_COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
CompactPublicKeyEncryptionParameters, CompressionParameters,
ShortintKeySwitchingParameters, PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
ShortintKeySwitchingParameters, PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
},
ClassicPBSParameters,
},
zk::{CompactPkeCrs, CompactPkePublicParams},
zk::CompactPkeCrs,
ClientKey, CompactPublicKey, Config, ConfigBuilder, ServerKey,
};

use crate::utils::{safe_deserialize_key, safe_serialize_key};

pub const TFHE_PARAMS: ClassicPBSParameters = PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
pub const TFHE_PARAMS: ClassicPBSParameters = PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
pub const TFHE_COMPRESSION_PARAMS: CompressionParameters =
COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
V1_0_COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
pub const TFHE_COMPACT_PK_ENCRYPTION_PARAMS: CompactPublicKeyEncryptionParameters =
PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
V1_0_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
pub const TFHE_KS_PARAMS: ShortintKeySwitchingParameters =
PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
V1_0_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;

pub const MAX_BITS_TO_PROVE: usize = 2048;

Expand All @@ -33,7 +33,7 @@ pub struct FhevmKeys {
pub server_key: ServerKey,
pub client_key: Option<ClientKey>,
pub compact_public_key: CompactPublicKey,
pub public_params: Arc<CompactPkePublicParams>,
pub public_params: Arc<CompactPkeCrs>,
}

pub struct SerializedFhevmKeys {
Expand All @@ -54,7 +54,7 @@ impl FhevmKeys {
server_key,
client_key: Some(client_key),
compact_public_key,
public_params: Arc::new(crs.public_params().clone()),
public_params: Arc::new(crs.clone()),
}
}

Expand Down
4 changes: 2 additions & 2 deletions fhevm-engine/fhevm-engine-common/src/tfhe_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use tfhe::{
CastInto, CiphertextList, FheEq, FheMax, FheMin, FheOrd, FheTryTrivialEncrypt, IfThenElse,
RotateLeft, RotateRight,
},
zk::CompactPkePublicParams,
zk::CompactPkeCrs,
FheBool, FheUint1024, FheUint128, FheUint16, FheUint160, FheUint2, FheUint2048, FheUint256,
FheUint32, FheUint4, FheUint512, FheUint64, FheUint8, Seed,
};
Expand Down Expand Up @@ -271,7 +271,7 @@ pub fn current_ciphertext_version() -> i16 {

pub fn try_expand_ciphertext_list(
input_ciphertext: &[u8],
public_params: &CompactPkePublicParams,
public_params: &CompactPkeCrs,
) -> Result<Vec<SupportedFheCiphertexts>, FhevmError> {
let mut res = Vec::new();

Expand Down
4 changes: 2 additions & 2 deletions fhevm-engine/fhevm-keys/cks
Git LFS file not shown
4 changes: 2 additions & 2 deletions fhevm-engine/fhevm-keys/pks
Git LFS file not shown
4 changes: 2 additions & 2 deletions fhevm-engine/fhevm-keys/pp
Git LFS file not shown
4 changes: 2 additions & 2 deletions fhevm-engine/fhevm-keys/sks
Git LFS file not shown
8 changes: 1 addition & 7 deletions fhevm-engine/scheduler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@ edition = "2021"
anyhow = { workspace = true }
daggy = { workspace = true }
rayon = { workspace = true }
tfhe = { workspace = true }
tokio = { workspace = true }

# local dependencies
fhevm-engine-common = { path = "../fhevm-engine-common" }

# arch-specific dependencies
[target.'cfg(target_arch = "x86_64")'.dependencies]
tfhe = { workspace = true, features = ["x86_64-unix"] }
[target.'cfg(target_arch = "aarch64")'.dependencies]
tfhe = { workspace = true, features = ["aarch64-unix"] }

[features]
nightly-avx512 = ["tfhe/nightly-avx512"]

Loading

0 comments on commit 64190c0

Please sign in to comment.