Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add data for the CompactPkeCrs #29

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 83 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ tfhe_0_10 = { version = "0.10", features = [
"zk-pok",
"experimental-force_fft_algo_dif4",
], package = "tfhe", optional = true }
tfhe_0_11 = { version = "0.11", features = [
"boolean",
"integer",
"shortint",
"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" }

# 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" }

# other deps
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -47,6 +56,8 @@ default = ["generate"]
generate = [
"dep:tfhe_0_8",
"dep:tfhe_0_10",
"dep:tfhe_0_11",
"dep:tfhe-versionable",
"dep:tfhe_0_11-versionable",
]
load = ["dep:semver"]
3 changes: 3 additions & 0 deletions data/0_11/high_level_api/zk_pke_crs.bcode
Git LFS file not shown
3 changes: 3 additions & 0 deletions data/0_11/high_level_api/zk_pke_crs.cbor
Git LFS file not shown
13 changes: 13 additions & 0 deletions data/high_level_api.ron
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,17 @@
compressed: false,
)),
),
(
tfhe_version_min: "0.11",
tfhe_module: "high_level_api",
metadata: ZkPkePublicParams((
test_filename: "zk_pke_crs",
lwe_dimension: 2048,
max_num_cleartext: 16,
noise_bound: 46,
ciphertext_modulus: 18446744073709551616,
plaintext_modulus: 32,
padding_bit_count: 1,
)),
),
]
137 changes: 137 additions & 0 deletions src/data_0_11.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
use crate::generate::{
store_versioned_test_tfhe_011, TfhersVersion, PRNG_SEED, VALID_TEST_PARAMS_TUNIFORM,
};
use crate::{
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::{
boolean::engine::BooleanEngine,
core_crypto::commons::generators::DeterministicSeeder,
core_crypto::commons::math::random::ActivatedRandomGenerator,
shortint::engine::ShortintEngine,
shortint::parameters::{
CarryModulus, CiphertextModulus, ClassicPBSParameters, DecompositionBaseLog,
DecompositionLevelCount, DynamicDistribution, EncryptionKeyChoice, GlweDimension,
LweDimension, MaxNoiseLevel, MessageModulus, PBSParameters, PolynomialSize, StandardDev,
},
Seed,
};

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

impl From<TestDistribution> for DynamicDistribution<u64> {
fn from(value: TestDistribution) -> Self {
match value {
TestDistribution::Gaussian { stddev } => {
DynamicDistribution::new_gaussian_from_std_dev(StandardDev(stddev))
}
TestDistribution::TUniform { bound_log2 } => {
DynamicDistribution::new_t_uniform(bound_log2)
}
}
}
}

impl From<TestParameterSet> for ClassicPBSParameters {
fn from(value: TestParameterSet) -> Self {
ClassicPBSParameters {
lwe_dimension: LweDimension(value.lwe_dimension),
glwe_dimension: GlweDimension(value.glwe_dimension),
polynomial_size: PolynomialSize(value.polynomial_size),
lwe_noise_distribution: value.lwe_noise_distribution.into(),
glwe_noise_distribution: value.glwe_noise_distribution.into(),
pbs_base_log: DecompositionBaseLog(value.pbs_base_log),
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),
log2_p_fail: value.log2_p_fail,
ciphertext_modulus: CiphertextModulus::try_new(value.ciphertext_modulus).unwrap(),
encryption_key_choice: {
match &*value.encryption_key_choice {
"big" => EncryptionKeyChoice::Big,
"small" => EncryptionKeyChoice::Small,
_ => panic!("Invalid encryption key choice"),
}
},
}
}
}

impl From<TestParameterSet> for PBSParameters {
fn from(value: TestParameterSet) -> Self {
let tmp: ClassicPBSParameters = value.into();
tmp.into()
}
}

// 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"),
lwe_dimension: VALID_TEST_PARAMS_TUNIFORM.polynomial_size
* VALID_TEST_PARAMS_TUNIFORM.glwe_dimension, // Lwe dimension of the "big" key is glwe dimension * polynomial size
max_num_cleartext: 16,
noise_bound: match VALID_TEST_PARAMS_TUNIFORM.lwe_noise_distribution {
TestDistribution::Gaussian { .. } => unreachable!(),
TestDistribution::TUniform { bound_log2 } => bound_log2 as usize,
},
ciphertext_modulus: VALID_TEST_PARAMS_TUNIFORM.ciphertext_modulus,
plaintext_modulus: VALID_TEST_PARAMS_TUNIFORM.message_modulus
* VALID_TEST_PARAMS_TUNIFORM.carry_modulus
* 2, // *2 for padding bit
padding_bit_count: 1,
};

pub struct V0_11;

impl TfhersVersion for V0_11 {
const VERSION_NUMBER: &'static str = "0.11";

fn seed_prng(seed: u128) {
let mut seeder = DeterministicSeeder::<ActivatedRandomGenerator>::new(Seed(seed));
let shortint_engine = ShortintEngine::new_from_seeder(&mut seeder);
ShortintEngine::with_thread_local_mut(|local_engine| {
let _ = std::mem::replace(local_engine, shortint_engine);
});

let boolean_engine = BooleanEngine::new_from_seeder(&mut seeder);
BooleanEngine::replace_thread_local(boolean_engine);
}

fn gen_shortint_data() -> Vec<TestMetadata> {
Vec::new()
}

fn gen_hl_data() -> Vec<TestMetadata> {
let dir = Self::data_dir().join(HL_MODULE_NAME);
create_dir_all(&dir).unwrap();

let mut zk_rng: RandomGenerator<ActivatedRandomGenerator> =
RandomGenerator::new(Seed(PRNG_SEED));

let crs = CompactPkeCrs::new(
LweDimension(ZK_PKE_CRS_TEST.lwe_dimension),
ZK_PKE_CRS_TEST.max_num_cleartext,
TUniform::<u64>::new(ZK_PKE_CRS_TEST.noise_bound as u32),
CiphertextModulus::new(ZK_PKE_CRS_TEST.ciphertext_modulus),
ZK_PKE_CRS_TEST.plaintext_modulus as u64,
ZkMSBZeroPaddingBitCount(ZK_PKE_CRS_TEST.padding_bit_count as u64),
&mut zk_rng,
)
.unwrap();

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

vec![TestMetadata::ZkPkePublicParams(ZK_PKE_CRS_TEST)]
}
}
2 changes: 2 additions & 0 deletions src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{

use bincode::Options;
use serde::Serialize;
use tfhe_0_11_versionable::Versionize as VersionizeTfhe011;
use tfhe_versionable::Versionize as VersionizeTfhe010;
use tfhe_versionable::Versionize as VersionizeTfhe08;

Expand Down Expand Up @@ -163,6 +164,7 @@ macro_rules! define_store_versioned_test_fn {
}
define_store_versioned_test_fn!(store_versioned_test_tfhe_08, VersionizeTfhe08);
define_store_versioned_test_fn!(store_versioned_test_tfhe_010, VersionizeTfhe010);
define_store_versioned_test_fn!(store_versioned_test_tfhe_011, VersionizeTfhe011);

/// Stores the auxiliary data in `dir`, encoded in cbor, using the right tfhe-versionable version
macro_rules! define_store_versioned_auxiliary_fn {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use serde::{Deserialize, Serialize};
#[cfg(feature = "generate")]
pub mod data_0_10;
#[cfg(feature = "generate")]
pub mod data_0_11;
#[cfg(feature = "generate")]
pub mod data_0_8;
#[cfg(feature = "generate")]
pub mod generate;
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fs::remove_dir_all;
use tfhe_backward_compat_data::{
data_0_10::V0_10,
data_0_11::V0_11,
data_0_8::V0_8,
data_dir,
generate::{store_metadata, TfhersVersion, PRNG_SEED},
Expand Down Expand Up @@ -39,6 +40,7 @@ fn main() {

let mut testcases = gen_all_data::<V0_8>();
testcases.extend(gen_all_data::<V0_10>());
testcases.extend(gen_all_data::<V0_11>());

let shortint_testcases: Vec<Testcase> = testcases
.iter()
Expand Down