chore(zk): add bench zk v1 vs v2

This commit is contained in:
Nicolas Sarlin
2025-01-07 10:25:41 +01:00
committed by Nicolas Sarlin
parent cd5b3c61eb
commit 4c8d55f32b
3 changed files with 77 additions and 25 deletions

View File

@@ -14,8 +14,14 @@ use tfhe::integer::parameters::IntegerCompactCiphertextListExpansionMode;
use tfhe::integer::{ClientKey, CompactPrivateKey, CompactPublicKey, ServerKey}; use tfhe::integer::{ClientKey, CompactPrivateKey, CompactPublicKey, ServerKey};
use tfhe::keycache::NamedParam; 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::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::compact_public_key_only::p_fail_2_minus_64::ks_pbs::{
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_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::shortint::parameters::PBSParameters;
use tfhe::zk::{CompactPkeCrs, ZkComputeLoad}; use tfhe::zk::{CompactPkeCrs, ZkComputeLoad};
use utilities::{write_to_json, OperatorType}; use utilities::{write_to_json, OperatorType};
@@ -33,11 +39,18 @@ fn pke_zk_proof(c: &mut Criterion) {
.sample_size(15) .sample_size(15)
.measurement_time(std::time::Duration::from_secs(60)); .measurement_time(std::time::Duration::from_secs(60));
for (param_pke, _param_casting, param_fhe) in [( 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, V0_11_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
PARAM_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_fhe.name();
let param_name = param_name.as_str(); let param_name = param_name.as_str();
let cks = ClientKey::new(param_fhe); let cks = ClientKey::new(param_fhe);
@@ -53,6 +66,8 @@ fn pke_zk_proof(c: &mut Criterion) {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
metadata.fill_with(|| rng.gen()); metadata.fill_with(|| rng.gen());
let zk_vers = param_pke.zk_scheme;
for bits in [64usize, 640, 1280, 4096] { for bits in [64usize, 640, 1280, 4096] {
assert_eq!(bits % 64, 0); assert_eq!(bits % 64, 0);
// Packing, so we take the message and carry modulus to compute our block count // 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() { match BENCH_TYPE.get().unwrap() {
BenchmarkType::Latency => { BenchmarkType::Latency => {
bench_id = bench_id = format!(
format!("{bench_name}::{param_name}_{bits}_bits_packed_{zk_load}"); "{bench_name}::{param_name}_{bits}_bits_packed_{zk_load}_ZK{zk_vers:?}"
);
bench_group.bench_function(&bench_id, |b| { bench_group.bench_function(&bench_id, |b| {
let input_msg = rng.gen::<u64>(); let input_msg = rng.gen::<u64>();
let messages = vec![input_msg; fhe_uint_count]; 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_group.throughput(Throughput::Elements(elements));
bench_id = format!( 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| { bench_group.bench_function(&bench_id, |b| {
let messages = (0..elements) let messages = (0..elements)
@@ -155,11 +171,18 @@ fn pke_zk_verify(c: &mut Criterion, results_file: &Path) {
.open(results_file) .open(results_file)
.expect("cannot open results file"); .expect("cannot open results file");
for (param_pke, param_casting, param_fhe) in [( 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, V0_11_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
PARAM_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_fhe.name();
let param_name = param_name.as_str(); let param_name = param_name.as_str();
let cks = ClientKey::new(param_fhe); 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(); let mut rng = rand::thread_rng();
metadata.fill_with(|| rng.gen()); metadata.fill_with(|| rng.gen());
let zk_vers = param_pke.zk_scheme;
for bits in [64usize, 640, 1280, 4096] { for bits in [64usize, 640, 1280, 4096] {
assert_eq!(bits % 64, 0); assert_eq!(bits % 64, 0);
// Packing, so we take the message and carry modulus to compute our block count // 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()); 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_result(&mut file, &test_name, crs_data.len());
write_to_json::<u64, _>( write_to_json::<u64, _>(
@@ -223,10 +248,11 @@ fn pke_zk_verify(c: &mut Criterion, results_file: &Path) {
match BENCH_TYPE.get().unwrap() { match BENCH_TYPE.get().unwrap() {
BenchmarkType::Latency => { BenchmarkType::Latency => {
bench_id_verify = bench_id_verify = format!(
format!("{bench_name}::{param_name}_{bits}_bits_packed_{zk_load}"); "{bench_name}::{param_name}_{bits}_bits_packed_{zk_load}_ZK{zk_vers:?}"
);
bench_id_verify_and_expand = format!( 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::<u64>(); let input_msg = rng.gen::<u64>();
@@ -246,7 +272,7 @@ fn pke_zk_verify(c: &mut Criterion, results_file: &Path) {
); );
let test_name = format!( 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( write_result(
@@ -268,7 +294,7 @@ fn pke_zk_verify(c: &mut Criterion, results_file: &Path) {
println!("proof size: {}", ct1.proof_size()); println!("proof size: {}", ct1.proof_size());
let test_name = 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_result(&mut file, &test_name, proof_size);
write_to_json::<u64, _>( write_to_json::<u64, _>(
@@ -308,10 +334,10 @@ fn pke_zk_verify(c: &mut Criterion, results_file: &Path) {
bench_group.throughput(Throughput::Elements(elements)); bench_group.throughput(Throughput::Elements(elements));
bench_id_verify = format!( 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_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})... "); println!("Generating proven ciphertexts list ({zk_load})... ");

View File

@@ -4,8 +4,14 @@ use crate::core_crypto::commons::math::random::Seed;
use crate::core_crypto::prelude::DefaultRandomGenerator; use crate::core_crypto::prelude::DefaultRandomGenerator;
use crate::js_on_wasm_api::js_high_level_api::into_js_error; use crate::js_on_wasm_api::js_high_level_api::into_js_error;
use crate::shortint::parameters::classic::compact_pk::*; 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::compact_public_key_only::p_fail_2_minus_64::ks_pbs::{
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_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 crate::shortint::parameters::*;
use std::panic::set_hook; use std::panic::set_hook;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
@@ -216,6 +222,7 @@ pub struct ShortintNoiseDistribution(
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
pub enum ShortintCompactPublicKeyEncryptionParametersName { 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,
SHORTINT_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64_ZKV1,
} }
#[wasm_bindgen] #[wasm_bindgen]
@@ -227,6 +234,10 @@ impl ShortintCompactPublicKeyEncryptionParameters {
ShortintCompactPublicKeyEncryptionParametersName::SHORTINT_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64 => Self { 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, 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, 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,
} }
} }
} }

View File

@@ -645,6 +645,7 @@ async function compressedServerKeyBenchMessage2Carry2() {
async function compactPublicKeyZeroKnowledgeBench() { async function compactPublicKeyZeroKnowledgeBench() {
let params_to_bench = [ let params_to_bench = [
{ {
zk_scheme: "ZKV2",
name: shortint_params_name( name: shortint_params_name(
ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, 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, 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 = {}; let bench_results = {};
@@ -718,6 +731,8 @@ async function compactPublicKeyZeroKnowledgeBench() {
const mean = timing / bench_loops; const mean = timing / bench_loops;
const common_bench_str = const common_bench_str =
"compact_fhe_uint_proven_encryption_" + "compact_fhe_uint_proven_encryption_" +
params.zk_scheme +
"_" +
encrypt_count * 64 + encrypt_count * 64 +
"_bits_packed_" + "_bits_packed_" +
load_to_str[loadChoice]; load_to_str[loadChoice];