Skip to content

Commit

Permalink
fix: correct tfhe params for deser of compact lists
Browse files Browse the repository at this point in the history
  • Loading branch information
dartdart26 committed Nov 6, 2024
1 parent b10f4f8 commit eef648a
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 131 deletions.
48 changes: 24 additions & 24 deletions fhevm-engine/coprocessor/src/tests/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,18 @@ async fn test_fhe_random_basic() -> Result<(), Box<dyn std::error::Error>> {
let decrypt_request = output_handles.clone();
let resp = decrypt_ciphertexts(&pool, 1, decrypt_request).await?;
let expected: Vec<DecryptionResult> = vec![
DecryptionResult { value: "true".to_string(), output_type: 0 },
DecryptionResult { value: "14".to_string(), output_type: 1 },
DecryptionResult { value: "222".to_string(), output_type: 2 },
DecryptionResult { value: "16606".to_string(), output_type: 3 },
DecryptionResult { value: "988233950".to_string(), output_type: 4 },
DecryptionResult { value: "17726426742889332958".to_string(), output_type: 5 },
DecryptionResult { value: "95096831748625519397318297902939324638".to_string(), output_type: 6 },
DecryptionResult { value: "1399147239934657696393352535269648034851377856734".to_string(), output_type: 7 },
DecryptionResult { value: "48969777095037584338767432630635603497351897034565050559046641215867078525150".to_string(), output_type: 8 },
DecryptionResult { value: "5672133879802708562062769460366654319040907404569669369250878712726641601293078201378408576635102952424343507606900013668664900968766534158389316556374238".to_string(), output_type: 9 },
DecryptionResult { value: "114410226683647796805289418347157165421556177542313075040261034050349283333746133758044391125907486214639472868006915368703395208956207333386492400441169537541732419752589713196090985414300923196056906582334036871687847581632894669188198798108581443774738327425973554046487690044338027161262063834091508678878".to_string(), output_type: 10 },
DecryptionResult { value: "25467131467221889352447766632386825920162086306849479079474003300521331308946323592350894436520833075335200921129880946181368851703118432881146206394209809079609342138464350013730332957427806799788734382905676196753813118791559928400163520766297381306834422421839448600811564925727337403620797889852511246847304334517070464078488035367296596574161807086540525650162518315873919952617981067113303814800084140798312055931333854875147039478663345671463906729533961660604550647954937369209494370427528366578139283958113608230658940368557430929555840901268260704720518922861406075936793090973737181775476689405198209597662".to_string(), output_type: 11 }
DecryptionResult { value: "false".to_string(), output_type: 0 },
DecryptionResult { value: "4".to_string(), output_type: 1 },
DecryptionResult { value: "148".to_string(), output_type: 2 },
DecryptionResult { value: "39060".to_string(), output_type: 3 },
DecryptionResult { value: "3116406932".to_string(), output_type: 4 },
DecryptionResult { value: "16744062003802314900".to_string(), output_type: 5 },
DecryptionResult { value: "44953030667788789807801163478489667732".to_string(), output_type: 6 },
DecryptionResult { value: "699788072547873193368126953957374377199048628372".to_string(), output_type: 7 },
DecryptionResult { value: "18290830408818269326783511394560190581841753203314111201154159834111735273620".to_string(), output_type: 8 },
DecryptionResult { value: "1769040210829320620035371975763208287159504590317173624997132562186133182915910315329508293705545243792383240992549293826572438247391861630912169190135956".to_string(), output_type: 9 },
DecryptionResult { value: "22989541611278360860317607085321234262476959622429178478308114566779950875293498998603742120438063467358496867879955578556491855528439833101173555335758726877695751949613732825582850312467922902141927670800114686022716185629565391201179999715726886895662862992307149483228306419275997167463869911273658423444".to_string(), output_type: 10 },
DecryptionResult { value: "11416014595749681729513000062974489717332577819997864686142230359454232238264792773250893798270837445400356607589296759837330430218211296049670295480026281165659278432024204167404453215564579545011238956721728258299506154112994566209651361789234724005154439702784336410251188832231630832913285047561498057692981059809933095267618636935634867995375450484916706150926926865716366559640611873269923085322552883512574825770541008027885062573259361484934666788450768469315411342918093486180912626492755165617295077344605564323989831779802237230520718702699705619375496850673508054983765355058977658387840597943659895232660".to_string(), output_type: 11 }
];

println!("results: {:#?}", resp);
Expand Down Expand Up @@ -189,18 +189,18 @@ async fn test_fhe_random_bounded() -> Result<(), Box<dyn std::error::Error>> {
"8079251517827751825178719172167487990111025667428871008032586356881163784716972723299300352880728365922179490230474504873529889787622730273772038096612070780157719341825249022937549437597413026699014409596016892069198054660654939040459523584619042617645411463009076260721893972885266452151888099780982596380478583347417085605171243696641142373714044008831580514519451414832756548177115078537564648216044279181485900929615464339399587788075411476100924403308321807806781421177705052431289275431732830867419635645164174483761499317088249659553881291597359333885900533858307401161329619651238037048388963402764899057664",
];
let results = [
"true",
"2",
"94",
"222",
"988233950",
"3891368687607169246",
"10026240018390903531474646044997271774",
"303021011936480507740588910732435770109428449502",
"20021754785708535482874686378463626534034400868154909549182245213888796115166",
"2320181897317059287169263210815192787171065949421571024819988351796200593774691457177940002093377095501835543060278500955226430265779891671780904304853214",
"24525569940532001418824158807705928740657328595197746403545993471482945430995652191690152464703718204079415928071218689874000324548999022140068685121432475352848473040156970557939875613677876136330365106291533987268772240461663228451242242838167825193063072083680404926514067075098169008844385669279396610270",
"1229376913738633876911609115884361949829009304562866055376244229877839954795405422452993377878647977568662450438457431560779182340250242059830092104373596739136184112988602944917684644635567719691691154117625520546218954809595111278784950012440253453898188032812219818645883007071538047165133590509563457705868584474819207262974304277373169453019675060045784106604164071375650308086635831500609870151951303253854353142487461856948276114437111243161133519608996237184206384421822211915626544132329873975880377022621084779374442417292681950894197026476182703062817321286483872452804232020023070630309799196903512424670",
"false",
"0",
"84",
"6292",
"968923284",
"2909003948520151188",
"44953030667788789807801163478489667732",
"334412663215147463817205745778303622285065492628",
"18290830408818269326783511394560190581841753203314111201154159834111735273620",
"1769040210829320620035371975763208287159504590317173624997132562186133182915910315329508293705545243792383240992549293826572438247391861630912169190135956",
"22989541611278360860317607085321234262476959622429178478308114566779950875293498998603742120438063467358496867879955578556491855528439833101173555335758726877695751949613732825582850312467922902141927670800114686022716185629565391201179999715726886895662862992307149483228306419275997167463869911273658423444",
"3336763077921929904334280890807001727221552152568993678109644002573068453547820049951593445390109079478177117358822254963800540430588565775898257383414210385501559090198955144466903777967166518312224547125711366230308099452339627169191838204615681387509028239775260149529294859346364380761396947780515461312502476462516009662447393238993725621661406476085125636407475450883610011463496794732358437106508604331088924840925543688485474785183950008833742385142446661508629921740388433749623351061022334749875441699441389840228332462713987570966837411102346285489596316815200653822435735407739621339451634540894996174996",
];

for (idx, the_type) in supported_types().iter().enumerate() {
Expand Down
25 changes: 20 additions & 5 deletions fhevm-engine/fhevm-engine-common/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +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,
CompressionParameters, PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
CompactPublicKeyEncryptionParameters, CompressionParameters,
ShortintKeySwitchingParameters, PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
},
ClassicPBSParameters,
},
zk::{CompactPkeCrs, CompactPkePublicParams},
ClientKey, CompactPublicKey, ConfigBuilder, ServerKey,
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_COMPRESSION_PARAMS: CompressionParameters =
COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
pub const TFHE_COMPACT_PK_ENCRYPTION_PARAMS: CompactPublicKeyEncryptionParameters =
PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
pub const TFHE_KS_PARAMS: ShortintKeySwitchingParameters =
PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;

pub const MAX_BITS_TO_PROVE: usize = 2048;

Expand All @@ -38,9 +45,7 @@ pub struct SerializedFhevmKeys {
impl FhevmKeys {
pub fn new() -> Self {
println!("Generating keys...");
let config = ConfigBuilder::with_custom_parameters(TFHE_PARAMS)
.enable_compression(TFHE_COMPRESSION_PARAMS)
.build();
let config = Self::new_config();
let (client_key, server_key) = generate_keys(config);
let compact_public_key = CompactPublicKey::new(&client_key);
let crs = CompactPkeCrs::from_config(config, MAX_BITS_TO_PROVE).expect("CRS creation");
Expand All @@ -52,6 +57,16 @@ impl FhevmKeys {
}
}

pub fn new_config() -> Config {
ConfigBuilder::with_custom_parameters(TFHE_PARAMS)
.enable_compression(TFHE_COMPRESSION_PARAMS)
.use_dedicated_compact_public_key_parameters((
TFHE_COMPACT_PK_ENCRYPTION_PARAMS,
TFHE_KS_PARAMS,
))
.build()
}

pub fn set_server_key_for_current_thread(&self) {
set_server_key(self.server_key.clone());
}
Expand Down
Loading

0 comments on commit eef648a

Please sign in to comment.