From 2353e85fca03f59fec79d987bead30fffbbb644b Mon Sep 17 00:00:00 2001 From: Nicolas Sarlin Date: Tue, 7 Jan 2025 10:25:41 +0100 Subject: [PATCH] chore(zk): add bench zk v1 vs v2 --- tfhe/benches/integer/zk_pke.rs | 72 ++++++++++++++++++-------- tfhe/src/js_on_wasm_api/shortint.rs | 15 +++++- tfhe/web_wasm_parallel_tests/worker.js | 15 ++++++ 3 files changed, 77 insertions(+), 25 deletions(-) diff --git a/tfhe/benches/integer/zk_pke.rs b/tfhe/benches/integer/zk_pke.rs index 8d789caf1f..18daf9cbbb 100644 --- a/tfhe/benches/integer/zk_pke.rs +++ b/tfhe/benches/integer/zk_pke.rs @@ -14,8 +14,14 @@ use tfhe::integer::parameters::IntegerCompactCiphertextListExpansionMode; use tfhe::integer::{ClientKey, CompactPrivateKey, CompactPublicKey, ServerKey}; use tfhe::keycache::NamedParam; use tfhe::shortint::parameters::classic::tuniform::p_fail_2_minus_64::ks_pbs::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64; -use tfhe::shortint::parameters::compact_public_key_only::p_fail_2_minus_64::ks_pbs::V0_11_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64; -use tfhe::shortint::parameters::key_switching::p_fail_2_minus_64::ks_pbs::V0_11_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64; +use tfhe::shortint::parameters::compact_public_key_only::p_fail_2_minus_64::ks_pbs::{ + V0_11_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + V0_11_PARAM_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64_ZKV1, +}; +use tfhe::shortint::parameters::key_switching::p_fail_2_minus_64::ks_pbs::{ + V0_11_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + V0_11_PARAM_KEYSWITCH_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64_ZKV1, +}; use tfhe::shortint::parameters::PBSParameters; use tfhe::zk::{CompactPkeCrs, ZkComputeLoad}; use utilities::{write_to_json, OperatorType}; @@ -33,11 +39,18 @@ fn pke_zk_proof(c: &mut Criterion) { .sample_size(15) .measurement_time(std::time::Duration::from_secs(60)); - for (param_pke, _param_casting, param_fhe) in [( - V0_11_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, - V0_11_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, - )] { + for (param_pke, _param_casting, param_fhe) in [ + ( + V0_11_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + V0_11_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + ), + ( + V0_11_PARAM_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64_ZKV1, + V0_11_PARAM_KEYSWITCH_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64_ZKV1, + PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + ), + ] { let param_name = param_fhe.name(); let param_name = param_name.as_str(); let cks = ClientKey::new(param_fhe); @@ -53,6 +66,8 @@ fn pke_zk_proof(c: &mut Criterion) { let mut rng = rand::thread_rng(); metadata.fill_with(|| rng.gen()); + let zk_vers = param_pke.zk_scheme; + for bits in [64usize, 640, 1280, 4096] { assert_eq!(bits % 64, 0); // Packing, so we take the message and carry modulus to compute our block count @@ -81,8 +96,9 @@ fn pke_zk_proof(c: &mut Criterion) { match BENCH_TYPE.get().unwrap() { BenchmarkType::Latency => { - bench_id = - format!("{bench_name}::{param_name}_{bits}_bits_packed_{zk_load}"); + bench_id = format!( + "{bench_name}::{param_name}_{bits}_bits_packed_{zk_load}_ZK{zk_vers:?}" + ); bench_group.bench_function(&bench_id, |b| { let input_msg = rng.gen::(); let messages = vec![input_msg; fhe_uint_count]; @@ -100,7 +116,7 @@ fn pke_zk_proof(c: &mut Criterion) { bench_group.throughput(Throughput::Elements(elements)); bench_id = format!( - "{bench_name}::throughput::{param_name}_{bits}_bits_packed_{zk_load}" + "{bench_name}::throughput::{param_name}_{bits}_bits_packed_{zk_load}_ZK{zk_vers:?}" ); bench_group.bench_function(&bench_id, |b| { let messages = (0..elements) @@ -155,11 +171,18 @@ fn pke_zk_verify(c: &mut Criterion, results_file: &Path) { .open(results_file) .expect("cannot open results file"); - for (param_pke, param_casting, param_fhe) in [( - V0_11_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, - V0_11_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, - )] { + for (param_pke, param_casting, param_fhe) in [ + ( + V0_11_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + V0_11_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + ), + ( + V0_11_PARAM_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64_ZKV1, + V0_11_PARAM_KEYSWITCH_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64_ZKV1, + PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + ), + ] { let param_name = param_fhe.name(); let param_name = param_name.as_str(); let cks = ClientKey::new(param_fhe); @@ -174,6 +197,8 @@ fn pke_zk_verify(c: &mut Criterion, results_file: &Path) { let mut rng = rand::thread_rng(); metadata.fill_with(|| rng.gen()); + let zk_vers = param_pke.zk_scheme; + for bits in [64usize, 640, 1280, 4096] { assert_eq!(bits % 64, 0); // Packing, so we take the message and carry modulus to compute our block count @@ -199,7 +224,7 @@ fn pke_zk_verify(c: &mut Criterion, results_file: &Path) { println!("CRS size: {}", crs_data.len()); - let test_name = format!("zk::crs_sizes::{param_name}_{bits}_bits_packed"); + let test_name = format!("zk::crs_sizes::{param_name}_{bits}_bits_packed_ZK{zk_vers:?}"); write_result(&mut file, &test_name, crs_data.len()); write_to_json::( @@ -223,10 +248,11 @@ fn pke_zk_verify(c: &mut Criterion, results_file: &Path) { match BENCH_TYPE.get().unwrap() { BenchmarkType::Latency => { - bench_id_verify = - format!("{bench_name}::{param_name}_{bits}_bits_packed_{zk_load}"); + bench_id_verify = format!( + "{bench_name}::{param_name}_{bits}_bits_packed_{zk_load}_ZK{zk_vers:?}" + ); bench_id_verify_and_expand = format!( - "{bench_name}_and_expand::{param_name}_{bits}_bits_packed_{zk_load}" + "{bench_name}_and_expand::{param_name}_{bits}_bits_packed_{zk_load}_ZK{zk_vers:?}" ); let input_msg = rng.gen::(); @@ -246,7 +272,7 @@ fn pke_zk_verify(c: &mut Criterion, results_file: &Path) { ); let test_name = format!( - "zk::proven_list_size::{param_name}_{bits}_bits_packed_{zk_load}" + "zk::proven_list_size::{param_name}_{bits}_bits_packed_{zk_load}_ZK{zk_vers:?}" ); write_result( @@ -268,7 +294,7 @@ fn pke_zk_verify(c: &mut Criterion, results_file: &Path) { println!("proof size: {}", ct1.proof_size()); let test_name = - format!("zk::proof_sizes::{param_name}_{bits}_bits_packed_{zk_load}"); + format!("zk::proof_sizes::{param_name}_{bits}_bits_packed_{zk_load}_ZK{zk_vers:?}"); write_result(&mut file, &test_name, proof_size); write_to_json::( @@ -308,10 +334,10 @@ fn pke_zk_verify(c: &mut Criterion, results_file: &Path) { bench_group.throughput(Throughput::Elements(elements)); bench_id_verify = format!( - "{bench_name}::throughput::{param_name}_{bits}_bits_packed_{zk_load}" + "{bench_name}::throughput::{param_name}_{bits}_bits_packed_{zk_load}_ZK{zk_vers:?}" ); bench_id_verify_and_expand = format!( - "{bench_name}_and_expand::{param_name}_{bits}_bits_packed_{zk_load}" + "{bench_name}_and_expand::{param_name}_{bits}_bits_packed_{zk_load}_ZK{zk_vers:?}" ); println!("Generating proven ciphertexts list ({zk_load})... "); diff --git a/tfhe/src/js_on_wasm_api/shortint.rs b/tfhe/src/js_on_wasm_api/shortint.rs index 22da007bce..6ecff4939e 100644 --- a/tfhe/src/js_on_wasm_api/shortint.rs +++ b/tfhe/src/js_on_wasm_api/shortint.rs @@ -4,8 +4,14 @@ use crate::core_crypto::commons::math::random::Seed; use crate::core_crypto::prelude::DefaultRandomGenerator; use crate::js_on_wasm_api::js_high_level_api::into_js_error; use crate::shortint::parameters::classic::compact_pk::*; -use crate::shortint::parameters::compact_public_key_only::p_fail_2_minus_64::ks_pbs::V0_11_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64; -use crate::shortint::parameters::key_switching::p_fail_2_minus_64::ks_pbs::V0_11_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64; +use crate::shortint::parameters::compact_public_key_only::p_fail_2_minus_64::ks_pbs::{ + V0_11_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + V0_11_PARAM_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64_ZKV1, +}; +use crate::shortint::parameters::key_switching::p_fail_2_minus_64::ks_pbs::{ + V0_11_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + V0_11_PARAM_KEYSWITCH_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64_ZKV1, +}; use crate::shortint::parameters::*; use std::panic::set_hook; use wasm_bindgen::prelude::*; @@ -216,6 +222,7 @@ pub struct ShortintNoiseDistribution( #[allow(non_camel_case_types)] pub enum ShortintCompactPublicKeyEncryptionParametersName { SHORTINT_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + SHORTINT_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64_ZKV1, } #[wasm_bindgen] @@ -227,6 +234,10 @@ impl ShortintCompactPublicKeyEncryptionParameters { ShortintCompactPublicKeyEncryptionParametersName::SHORTINT_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64 => Self { compact_pke_params: V0_11_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, casting_parameters: V0_11_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + }, + ShortintCompactPublicKeyEncryptionParametersName::SHORTINT_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64_ZKV1 => Self { + compact_pke_params: V0_11_PARAM_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64_ZKV1, + casting_parameters: V0_11_PARAM_KEYSWITCH_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64_ZKV1, } } } diff --git a/tfhe/web_wasm_parallel_tests/worker.js b/tfhe/web_wasm_parallel_tests/worker.js index 05c2fe6766..d0712603a4 100644 --- a/tfhe/web_wasm_parallel_tests/worker.js +++ b/tfhe/web_wasm_parallel_tests/worker.js @@ -645,6 +645,7 @@ async function compressedServerKeyBenchMessage2Carry2() { async function compactPublicKeyZeroKnowledgeBench() { let params_to_bench = [ { + zk_scheme: "ZKV2", name: shortint_params_name( ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, ), @@ -655,6 +656,18 @@ async function compactPublicKeyZeroKnowledgeBench() { ShortintCompactPublicKeyEncryptionParametersName.SHORTINT_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, ), }, + { + zk_scheme: "ZKV1", + name: shortint_params_name( + ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + ), + block_params: new ShortintParameters( + ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + ), + casting_params: new ShortintCompactPublicKeyEncryptionParameters( + ShortintCompactPublicKeyEncryptionParametersName.SHORTINT_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64_ZKV1, + ), + }, ]; let bench_results = {}; @@ -718,6 +731,8 @@ async function compactPublicKeyZeroKnowledgeBench() { const mean = timing / bench_loops; const common_bench_str = "compact_fhe_uint_proven_encryption_" + + params.zk_scheme + + "_" + encrypt_count * 64 + "_bits_packed_" + load_to_str[loadChoice];