From 2b7e75a2eca705676b896e5d2abb8ba84bb23b2a Mon Sep 17 00:00:00 2001 From: Nicolas Sarlin Date: Wed, 11 Sep 2024 14:33:08 +0200 Subject: [PATCH] chore(backward): adds a test for proven list versioning --- Makefile | 2 +- tfhe/Cargo.toml | 2 +- .../backward_compatibility/high_level_api.rs | 59 ++++++++++++++++++- tfhe/tests/backward_compatibility/shortint.rs | 24 +++++--- 4 files changed, 74 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index a81bf442b8..bd584fdbfe 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ BENCH_OP_FLAVOR?=DEFAULT NODE_VERSION=22.6 FORWARD_COMPAT?=OFF BACKWARD_COMPAT_DATA_URL=https://github.com/zama-ai/tfhe-backward-compat-data.git -BACKWARD_COMPAT_DATA_BRANCH?=v0.1 +BACKWARD_COMPAT_DATA_BRANCH?=v0.2 BACKWARD_COMPAT_DATA_PROJECT=tfhe-backward-compat-data BACKWARD_COMPAT_DATA_DIR=$(BACKWARD_COMPAT_DATA_PROJECT) TFHE_SPEC:=tfhe diff --git a/tfhe/Cargo.toml b/tfhe/Cargo.toml index b60e3f4bc3..c64a8f3406 100644 --- a/tfhe/Cargo.toml +++ b/tfhe/Cargo.toml @@ -46,7 +46,7 @@ hex = "0.4.3" # End regex-engine deps # Used for backward compatibility test metadata ron = "0.8" -tfhe-backward-compat-data = { git = "https://github.com/zama-ai/tfhe-backward-compat-data.git", branch = "v0.1", default-features = false, features = [ +tfhe-backward-compat-data = { git = "https://github.com/zama-ai/tfhe-backward-compat-data.git", branch = "v0.2", default-features = false, features = [ "load", ] } diff --git a/tfhe/tests/backward_compatibility/high_level_api.rs b/tfhe/tests/backward_compatibility/high_level_api.rs index 5422426e74..bf274b04e8 100644 --- a/tfhe/tests/backward_compatibility/high_level_api.rs +++ b/tfhe/tests/backward_compatibility/high_level_api.rs @@ -8,11 +8,15 @@ use tfhe::backward_compatibility::integers::{ use tfhe::prelude::{CiphertextList, FheDecrypt, FheEncrypt}; use tfhe::shortint::PBSParameters; +#[cfg(feature = "zk-pok")] +use tfhe::zk::CompactPkePublicParams; use tfhe::{ set_server_key, ClientKey, CompactCiphertextList, CompressedCiphertextList, CompressedCompactPublicKey, CompressedFheBool, CompressedFheInt8, CompressedFheUint8, CompressedPublicKey, CompressedServerKey, FheBool, FheInt8, FheUint8, }; +#[cfg(feature = "zk-pok")] +use tfhe::{CompactPublicKey, ProvenCompactCiphertextList}; use tfhe_backward_compat_data::load::{ load_versioned_auxiliary, DataFormat, TestFailure, TestResult, TestSuccess, }; @@ -20,7 +24,7 @@ use tfhe_backward_compat_data::{ DataKind, HlBoolCiphertextListTest, HlBoolCiphertextTest, HlCiphertextListTest, HlCiphertextTest, HlClientKeyTest, HlHeterogeneousCiphertextListTest, HlPublicKeyTest, HlServerKeyTest, HlSignedCiphertextListTest, HlSignedCiphertextTest, TestMetadata, - TestParameterSet, TestType, Testcase, + TestParameterSet, TestType, Testcase, ZkPkePublicParamsTest, }; use tfhe_versionable::Unversionize; @@ -259,9 +263,23 @@ pub fn test_hl_bool_ciphertext_list( } } +/// Test Zk Public params +pub fn test_zk_params( + dir: &Path, + test: &ZkPkePublicParamsTest, + format: DataFormat, +) -> Result { + #[cfg(feature = "zk-pok")] + let _loaded_params: CompactPkePublicParams = load_and_unversionize(dir, test, format)?; + + #[cfg(not(feature = "zk-pok"))] + let _ = dir; + + Ok(test.success(format)) +} + /// Test HL ciphertext list: loads the ciphertext list and compare the decrypted values to the ones /// in the metadata. - pub fn test_hl_heterogeneous_ciphertext_list( dir: &Path, test: &HlHeterogeneousCiphertextListTest, @@ -279,9 +297,41 @@ pub fn test_hl_heterogeneous_ciphertext_list( if test.compressed { let list: CompressedCiphertextList = load_and_unversionize(dir, test, format)?; test_hl_heterogeneous_ciphertext_list_elements(list, &key, test) + } else if let Some(zk_info) = &test.proof_info { + #[cfg(feature = "zk-pok")] + { + let crs_file = dir.join(&*zk_info.params_filename); + let crs = CompactPkePublicParams::unversionize( + load_versioned_auxiliary(crs_file).map_err(|e| test.failure(e, format))?, + ) + .map_err(|e| test.failure(e, format))?; + + let pubkey_file = dir.join(&*zk_info.public_key_filename); + let pubkey = CompactPublicKey::unversionize( + load_versioned_auxiliary(pubkey_file).map_err(|e| test.failure(e, format))?, + ) + .map_err(|e| test.failure(e, format))?; + + let list: ProvenCompactCiphertextList = load_and_unversionize(dir, test, format)?; + test_hl_heterogeneous_ciphertext_list_elements( + list.verify_and_expand(&crs, &pubkey, zk_info.metadata.as_bytes()) + .map_err(|msg| test.failure(msg, format))?, + &key, + test, + ) + } + #[cfg(not(feature = "zk-pok"))] + { + let _ = zk_info; + Ok(()) + } } else { let list: CompactCiphertextList = load_and_unversionize(dir, test, format)?; - test_hl_heterogeneous_ciphertext_list_elements(list.expand().unwrap(), &key, test) + test_hl_heterogeneous_ciphertext_list_elements( + list.expand().map_err(|msg| test.failure(msg, format))?, + &key, + test, + ) } .map(|_| test.success(format)) .map_err(|msg| test.failure(msg, format)) @@ -491,6 +541,9 @@ impl TestedModule for Hl { TestMetadata::HlServerKey(test) => { test_hl_serverkey(test_dir.as_ref(), test, format).into() } + TestMetadata::ZkPkePublicParams(test) => { + test_zk_params(test_dir.as_ref(), test, format).into() + } _ => { println!("WARNING: missing test: {:?}", testcase.metadata); TestResult::Skipped(testcase.skip()) diff --git a/tfhe/tests/backward_compatibility/shortint.rs b/tfhe/tests/backward_compatibility/shortint.rs index 08b57e9874..c1ae1d3aab 100644 --- a/tfhe/tests/backward_compatibility/shortint.rs +++ b/tfhe/tests/backward_compatibility/shortint.rs @@ -1,11 +1,12 @@ use std::path::Path; +use tfhe::core_crypto::prelude::TUniform; use tfhe_backward_compat_data::load::{ load_versioned_auxiliary, DataFormat, TestFailure, TestResult, TestSuccess, }; use tfhe_backward_compat_data::{ - ShortintCiphertextTest, ShortintClientKeyTest, TestMetadata, TestParameterSet, TestType, - Testcase, + ShortintCiphertextTest, ShortintClientKeyTest, TestDistribution, TestMetadata, + TestParameterSet, TestType, Testcase, }; use tfhe::shortint::parameters::{ @@ -27,12 +28,8 @@ pub fn load_params(test_params: &TestParameterSet) -> ClassicPBSParameters { lwe_dimension: LweDimension(test_params.lwe_dimension), glwe_dimension: GlweDimension(test_params.glwe_dimension), polynomial_size: PolynomialSize(test_params.polynomial_size), - lwe_noise_distribution: DynamicDistribution::new_gaussian_from_std_dev(StandardDev( - test_params.lwe_noise_gaussian_stddev, - )), - glwe_noise_distribution: DynamicDistribution::new_gaussian_from_std_dev(StandardDev( - test_params.glwe_noise_gaussian_stddev, - )), + lwe_noise_distribution: convert_distribution(&test_params.lwe_noise_distribution), + glwe_noise_distribution: convert_distribution(&test_params.glwe_noise_distribution), pbs_base_log: DecompositionBaseLog(test_params.pbs_base_log), pbs_level: DecompositionLevelCount(test_params.pbs_level), ks_base_log: DecompositionBaseLog(test_params.ks_base_log), @@ -52,6 +49,17 @@ pub fn load_params(test_params: &TestParameterSet) -> ClassicPBSParameters { } } +fn convert_distribution(value: &TestDistribution) -> DynamicDistribution { + match value { + TestDistribution::Gaussian { stddev } => { + DynamicDistribution::new_gaussian_from_std_dev(StandardDev(*stddev)) + } + TestDistribution::TUniform { bound_log2 } => { + DynamicDistribution::TUniform(TUniform::new(*bound_log2)) + } + } +} + fn load_shortint_params(test_params: &TestParameterSet) -> ShortintParameterSet { ShortintParameterSet::new_pbs_param_set(PBSParameters::PBS(load_params(test_params))) }