Skip to content

Commit

Permalink
chore(backward): adds a test for proven list versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarlin-zama committed Sep 27, 2024
1 parent 154a10f commit 2b7e75a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tfhe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
] }

Expand Down
59 changes: 56 additions & 3 deletions tfhe/tests/backward_compatibility/high_level_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ 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,
};
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;

Expand Down Expand Up @@ -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<TestSuccess, TestFailure> {
#[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,
Expand All @@ -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))
Expand Down Expand Up @@ -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())
Expand Down
24 changes: 16 additions & 8 deletions tfhe/tests/backward_compatibility/shortint.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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),
Expand All @@ -52,6 +49,17 @@ pub fn load_params(test_params: &TestParameterSet) -> ClassicPBSParameters {
}
}

fn convert_distribution(value: &TestDistribution) -> DynamicDistribution<u64> {
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)))
}
Expand Down

0 comments on commit 2b7e75a

Please sign in to comment.