Skip to content

Commit

Permalink
feat: add data for proven list with zk v2
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarlin-zama authored and zama-bot committed Dec 16, 2024
1 parent 193e7dd commit e2f3c25
Show file tree
Hide file tree
Showing 11 changed files with 427 additions and 92 deletions.
369 changes: 290 additions & 79 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ tfhe_0_11 = { version = "0.11", features = [
"x86_64-unix",
"zk-pok",
"experimental-force_fft_algo_dif4",
], package = "tfhe", optional = true, git = "https://github.com/zama-ai/tfhe-rs/", branch = "ns/rename_pp_crs" }
], package = "tfhe", optional = true, git = "https://github.com/zama-ai/tfhe-rs/", branch = "ns/plug_zk_v2" }

# TFHE-rs 0.8 and 0.10 use the same version of versionable
tfhe-versionable = { version = "0.3.2", optional = true }
tfhe_0_11-versionable = { version = "0.3.2", optional = true, package = "tfhe-versionable", git = "https://github.com/zama-ai/tfhe-rs/", branch = "ns/rename_pp_crs" }
tfhe_0_11-versionable = { version = "0.3.2", optional = true, package = "tfhe-versionable", git = "https://github.com/zama-ai/tfhe-rs/", branch = "ns/plug_zk_v2" }

# other deps
serde = { version = "1.0", features = ["derive"] }
Expand Down
3 changes: 3 additions & 0 deletions data/0_11/high_level_api/client_key.cbor
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
3 changes: 3 additions & 0 deletions data/0_11/high_level_api/public_key.cbor
Git LFS file not shown
4 changes: 2 additions & 2 deletions data/0_11/high_level_api/zk_pke_crs.bcode
Git LFS file not shown
4 changes: 2 additions & 2 deletions data/0_11/high_level_api/zk_pke_crs.cbor
Git LFS file not shown
26 changes: 26 additions & 0 deletions data/high_level_api.ron
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,30 @@
padding_bit_count: 1,
)),
),
(
tfhe_version_min: "0.11",
tfhe_module: "high_level_api",
metadata: HlHeterogeneousCiphertextList((
test_filename: "hl_proven_heterogeneous_list_zkv2",
key_filename: "client_key",
compressed: false,
proof_info: Some((
public_key_filename: "public_key",
params_filename: "zk_pke_crs",
metadata: "2vdrawkcab",
)),
clear_values: [
17,
18446744073709551604,
0,
1,
],
data_kinds: [
Unsigned,
Signed,
Bool,
Bool,
],
)),
),
]
99 changes: 92 additions & 7 deletions src/data_0_11.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::generate::{
store_versioned_test_tfhe_011, TfhersVersion, PRNG_SEED, VALID_TEST_PARAMS_TUNIFORM,
store_versioned_auxiliary_tfhe_011, store_versioned_test_tfhe_011, TfhersVersion, PRNG_SEED,
VALID_TEST_PARAMS_TUNIFORM,
};
use crate::{
DataKind, HlClientKeyTest, HlHeterogeneousCiphertextListTest, PkeZkProofAuxilliaryInfo,
TestDistribution, TestMetadata, TestParameterSet, ZkPkePublicParamsTest, HL_MODULE_NAME,
};
use std::{borrow::Cow, fs::create_dir_all};
use tfhe_0_11::core_crypto::commons::math::random::RandomGenerator;
use tfhe_0_11::core_crypto::prelude::TUniform;
use tfhe_0_11::zk::{CompactPkeCrs, ZkMSBZeroPaddingBitCount};
use tfhe_0_11::zk::{CompactPkeCrs, ZkComputeLoad, ZkMSBZeroPaddingBitCount};
use tfhe_0_11::{
boolean::engine::BooleanEngine,
core_crypto::commons::generators::DeterministicSeeder,
Expand All @@ -18,15 +20,22 @@ use tfhe_0_11::{
DecompositionLevelCount, DynamicDistribution, EncryptionKeyChoice, GlweDimension,
LweDimension, MaxNoiseLevel, MessageModulus, PBSParameters, PolynomialSize, StandardDev,
},
Seed,
ClientKey, Seed,
};
use tfhe_0_11::{set_server_key, CompactPublicKey, ProvenCompactCiphertextList, ServerKey};

macro_rules! store_versioned_test {
($msg:expr, $dir:expr, $test_filename:expr $(,)? ) => {
store_versioned_test_tfhe_011($msg, $dir, $test_filename)
};
}

macro_rules! store_versioned_auxiliary {
($msg:expr, $dir:expr, $test_filename:expr $(,)? ) => {
store_versioned_auxiliary_tfhe_011($msg, $dir, $test_filename)
};
}

impl From<TestDistribution> for DynamicDistribution<u64> {
fn from(value: TestDistribution) -> Self {
match value {
Expand All @@ -52,9 +61,9 @@ impl From<TestParameterSet> for ClassicPBSParameters {
pbs_level: DecompositionLevelCount(value.pbs_level),
ks_base_log: DecompositionBaseLog(value.ks_base_log),
ks_level: DecompositionLevelCount(value.ks_level),
message_modulus: MessageModulus(value.message_modulus),
carry_modulus: CarryModulus(value.carry_modulus),
max_noise_level: MaxNoiseLevel::new(value.max_noise_level),
message_modulus: MessageModulus(value.message_modulus as u64),
carry_modulus: CarryModulus(value.carry_modulus as u64),
max_noise_level: MaxNoiseLevel::new(value.max_noise_level as u64),
log2_p_fail: value.log2_p_fail,
ciphertext_modulus: CiphertextModulus::try_new(value.ciphertext_modulus).unwrap(),
encryption_key_choice: {
Expand All @@ -75,6 +84,11 @@ impl From<TestParameterSet> for PBSParameters {
}
}

const HL_CLIENTKEY_TEST: HlClientKeyTest = HlClientKeyTest {
test_filename: Cow::Borrowed("client_key"),
parameters: VALID_TEST_PARAMS_TUNIFORM,
};

// The CRS is structurally equivalent to the public params type so we reuse the test
const ZK_PKE_CRS_TEST: ZkPkePublicParamsTest = ZkPkePublicParamsTest {
test_filename: Cow::Borrowed("zk_pke_crs"),
Expand All @@ -92,6 +106,25 @@ const ZK_PKE_CRS_TEST: ZkPkePublicParamsTest = ZkPkePublicParamsTest {
padding_bit_count: 1,
};

const HL_PROVEN_COMPACTLIST_TEST_ZKV2: HlHeterogeneousCiphertextListTest =
HlHeterogeneousCiphertextListTest {
test_filename: Cow::Borrowed("hl_proven_heterogeneous_list_zkv2"),
key_filename: HL_CLIENTKEY_TEST.test_filename,
clear_values: Cow::Borrowed(&[17u8 as u64, -12i8 as u64, false as u64, true as u64]),
data_kinds: Cow::Borrowed(&[
DataKind::Unsigned,
DataKind::Signed,
DataKind::Bool,
DataKind::Bool,
]),
compressed: false,
proof_info: Some(PkeZkProofAuxilliaryInfo {
public_key_filename: Cow::Borrowed("public_key"),
params_filename: Cow::Borrowed("zk_pke_crs"),
metadata: Cow::Borrowed("2vdrawkcab"),
}),
};

pub struct V0_11;

impl TfhersVersion for V0_11 {
Expand Down Expand Up @@ -119,6 +152,14 @@ impl TfhersVersion for V0_11 {
let mut zk_rng: RandomGenerator<ActivatedRandomGenerator> =
RandomGenerator::new(Seed(PRNG_SEED));

// Generate a compact public key needed to create a compact list
let config =
tfhe_0_11::ConfigBuilder::with_custom_parameters(VALID_TEST_PARAMS_TUNIFORM).build();
let hl_client_key = ClientKey::generate(config);
let hl_server_key = ServerKey::new(&hl_client_key);
set_server_key(hl_server_key.clone());
let compact_pub_key = CompactPublicKey::new(&hl_client_key);

let crs = CompactPkeCrs::new(
LweDimension(ZK_PKE_CRS_TEST.lwe_dimension),
ZK_PKE_CRS_TEST.max_num_cleartext,
Expand All @@ -132,6 +173,50 @@ impl TfhersVersion for V0_11 {

store_versioned_test!(&crs, &dir, &ZK_PKE_CRS_TEST.test_filename,);

vec![TestMetadata::ZkPkePublicParams(ZK_PKE_CRS_TEST)]
// Store the associated client key to be able to decrypt the ciphertexts in the list
store_versioned_auxiliary!(
&hl_client_key,
&dir,
&HL_PROVEN_COMPACTLIST_TEST_ZKV2.key_filename
);

store_versioned_auxiliary!(
&compact_pub_key,
&dir,
&HL_PROVEN_COMPACTLIST_TEST_ZKV2
.proof_info
.unwrap()
.public_key_filename
);

let mut proven_builder = ProvenCompactCiphertextList::builder(&compact_pub_key);
proven_builder
.push(HL_PROVEN_COMPACTLIST_TEST_ZKV2.clear_values[0] as u8)
.push(HL_PROVEN_COMPACTLIST_TEST_ZKV2.clear_values[1] as i8)
.push(HL_PROVEN_COMPACTLIST_TEST_ZKV2.clear_values[2] != 0)
.push(HL_PROVEN_COMPACTLIST_TEST_ZKV2.clear_values[3] != 0);

let proven_list_packed = proven_builder
.build_with_proof_packed(
&crs,
HL_PROVEN_COMPACTLIST_TEST_ZKV2
.proof_info
.unwrap()
.metadata
.as_bytes(),
ZkComputeLoad::Proof,
)
.unwrap();

store_versioned_test!(
&proven_list_packed,
&dir,
&HL_PROVEN_COMPACTLIST_TEST_ZKV2.test_filename,
);

vec![
TestMetadata::ZkPkePublicParams(ZK_PKE_CRS_TEST),
TestMetadata::HlHeterogeneousCiphertextList(HL_PROVEN_COMPACTLIST_TEST_ZKV2),
]
}
}
1 change: 1 addition & 0 deletions src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ macro_rules! define_store_versioned_auxiliary_fn {
}
define_store_versioned_auxiliary_fn!(store_versioned_auxiliary_tfhe_08, VersionizeTfhe08);
define_store_versioned_auxiliary_fn!(store_versioned_auxiliary_tfhe_010, VersionizeTfhe010);
define_store_versioned_auxiliary_fn!(store_versioned_auxiliary_tfhe_011, VersionizeTfhe011);

pub fn store_metadata<Meta: Serialize, P: AsRef<Path>>(value: &Meta, path: P) {
let serialized = ron::ser::to_string_pretty(value, ron::ser::PrettyConfig::default()).unwrap();
Expand Down

0 comments on commit e2f3c25

Please sign in to comment.