From 9a3c5a531bbe2281258a95e7dc6b3045447fa1c0 Mon Sep 17 00:00:00 2001 From: Nicolas Sarlin Date: Wed, 15 Oct 2025 10:37:26 +0200 Subject: [PATCH] wip: show instantiated template for 1.5 --- tfhe/Cargo.toml | 2 +- .../crates/generate_1_5/Cargo.toml | 30 ++++++ .../crates/generate_1_5/src/lib.rs | 102 ++++++++++++++++++ .../crates/generate_1_5/src/main.rs | 36 +++++++ .../crates/generate_1_5/src/utils.rs | 35 ++++++ 5 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 utils/tfhe-backward-compat-data/crates/generate_1_5/Cargo.toml create mode 100644 utils/tfhe-backward-compat-data/crates/generate_1_5/src/lib.rs create mode 100644 utils/tfhe-backward-compat-data/crates/generate_1_5/src/main.rs create mode 100644 utils/tfhe-backward-compat-data/crates/generate_1_5/src/utils.rs diff --git a/tfhe/Cargo.toml b/tfhe/Cargo.toml index a75cfbf91..494df5641 100644 --- a/tfhe/Cargo.toml +++ b/tfhe/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tfhe" -version = "1.4.0" +version = "1.5.0" edition = "2021" readme = "../README.md" keywords = ["fully", "homomorphic", "encryption", "fhe", "cryptography"] diff --git a/utils/tfhe-backward-compat-data/crates/generate_1_5/Cargo.toml b/utils/tfhe-backward-compat-data/crates/generate_1_5/Cargo.toml new file mode 100644 index 000000000..abdb7f0d1 --- /dev/null +++ b/utils/tfhe-backward-compat-data/crates/generate_1_5/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "generate_1_5" +edition = "2024" +license.workspace = true +version.workspace = true + +[dependencies] +clap.workspace = true + +# TFHE-rs +tfhe = { features = [ + "boolean", + "integer", + "shortint", + "zk-pok", + "experimental-force_fft_algo_dif4", +], path = "../../../../tfhe" } +tfhe-versionable = { path = "../../../tfhe-versionable" } + +# Uncomment this and remove the lines above once the current tfhe-rs version has been released +# tfhe = { git = "https://github.com/zama-ai/tfhe-rs.git", tag = "tfhe-rs-1.5.0", features = [ +# "boolean", +# "integer", +# "shortint", +# "zk-pok", +# "experimental-force_fft_algo_dif4", +# ] } +# tfhe-versionable = { git = "https://github.com/zama-ai/tfhe-rs.git", tag = "tfhe-rs-1.5.0" } + +tfhe-backward-compat-data = { path = "../.." } diff --git a/utils/tfhe-backward-compat-data/crates/generate_1_5/src/lib.rs b/utils/tfhe-backward-compat-data/crates/generate_1_5/src/lib.rs new file mode 100644 index 000000000..a454703da --- /dev/null +++ b/utils/tfhe-backward-compat-data/crates/generate_1_5/src/lib.rs @@ -0,0 +1,102 @@ +mod utils; +use utils::*; + +use std::fs::create_dir_all; +use std::path::Path; + +use tfhe_backward_compat_data::generate::*; +use tfhe_backward_compat_data::*; + +// To complete this file, you can use examples in the data generation crates for +// other versions. + +// Here you can add constants that defines the metadata for your data tests. +// The metadata should use one of the types inside the `TestMetadata` enum in +// `tfhe-backward-compat-data/src/lib.rs`. +// Feel free to add a new variant if none of the existing one cover your needs. +// +// Example: +// const SHORTINT_CLIENT_KEY_FILENAME: &str = "client_key"; +// +// const SHORTINT_CLIENTKEY_TEST: ShortintClientKeyTest = ShortintClientKeyTest { +// test_filename: Cow::Borrowed(SHORTINT_CLIENT_KEY_FILENAME), +// parameters: VALID_TEST_PARAMS, +// }; +// const SHORTINT_CT_TEST: ShortintCiphertextTest = ShortintCiphertextTest { +// test_filename: Cow::Borrowed("ct"), +// key_filename: Cow::Borrowed(SHORTINT_CLIENT_KEY_FILENAME), +// clear_value: 0, +// }; + +pub struct V1_5; + +impl TfhersVersion for V1_5 { + const VERSION_NUMBER: &'static str = "1.5"; + + fn seed_prng(seed: u128) { + // Include here the code required to seed the prng for this version of TFHE-rs. + // This might require: seeding shortint and boolean engines + // + // Example: + // let mut seeder = DeterministicSeeder::::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); + todo!() + } + + // You now need to generate the data for the shortint and hl layer. This means: + // - Create the TFHE-rs objects you want to test from the metadata defined above + // - Store them using `store_versioned_test` + // - If the test needs some auxiliary data (such as a client key), store it with + // `store_versioned_auxiliary` + // - Returns all the metadata in a Vec + // + // Example: + // fn gen_shortint_data>(base_data_dir: P) -> Vec { + // let dir = Self::data_dir(base_data_dir).join(SHORTINT_MODULE_NAME); + // create_dir_all(&dir).unwrap(); + // + // // generate a client key + // let shortint_client_key = + // shortint::ClientKey::new(SHORTINT_CLIENTKEY_TEST.parameters.convert()); + // + // store_versioned_test( + // &shortint_client_key, + // &dir, + // &SHORTINT_CLIENTKEY_TEST.test_filename, + // ); + // + // // generate a ciphertext + // let ct = shortint_client_key.encrypt(SHORTINT_CT_TEST.clear_value); + // + // // Serialize it + // store_versioned_test(&ct, &dir, &SHORTINT_CT_TEST.test_filename); + // + // vec![ + // TestMetadata::ShortintClientKey(SHORTINT_CLIENTKEY_TEST), + // TestMetadata::ShortintCiphertext(SHORTINT_CT_TEST), + // ] + // } + fn gen_shortint_data>(base_data_dir: P) -> Vec { + // Remove this if you do not generate shortint data for this version + let dir = Self::data_dir(base_data_dir).join(SHORTINT_MODULE_NAME); + create_dir_all(&dir).unwrap(); + + // Add shortint data here + Vec::new() + } + + fn gen_hl_data>(base_data_dir: P) -> Vec { + // Remove this if you do not generate hl data for this version + let dir = Self::data_dir(base_data_dir).join(HL_MODULE_NAME); + create_dir_all(&dir).unwrap(); + + // Add hl data here + Vec::new() + } +} diff --git a/utils/tfhe-backward-compat-data/crates/generate_1_5/src/main.rs b/utils/tfhe-backward-compat-data/crates/generate_1_5/src/main.rs new file mode 100644 index 000000000..313020f09 --- /dev/null +++ b/utils/tfhe-backward-compat-data/crates/generate_1_5/src/main.rs @@ -0,0 +1,36 @@ +use std::fs::remove_dir_all; +use std::path::PathBuf; + +use clap::Parser; +use generate_1_5::V1_5; +use tfhe_backward_compat_data::dir_for_version; +use tfhe_backward_compat_data::generate::{ + display_metadata, gen_all_data, update_metadata_for_version, +}; + +#[derive(Parser, Debug)] +struct Args { + /// The path where the backward data should be stored + #[arg(long)] + data_path: PathBuf, + + /// Output metadata to stdout instead of writing them to the ron file + #[arg(long, action)] + stdout: bool, +} + +fn main() { + let args = Args::parse(); + + let version_dir = dir_for_version(&args.data_path, "1.5"); + // Ignore if directory does not exist + let _ = remove_dir_all(&version_dir); + + let data = gen_all_data::(&args.data_path); + + if args.stdout { + display_metadata(&data); + } else { + update_metadata_for_version(data, args.data_path); + } +} diff --git a/utils/tfhe-backward-compat-data/crates/generate_1_5/src/utils.rs b/utils/tfhe-backward-compat-data/crates/generate_1_5/src/utils.rs new file mode 100644 index 000000000..bf934a602 --- /dev/null +++ b/utils/tfhe-backward-compat-data/crates/generate_1_5/src/utils.rs @@ -0,0 +1,35 @@ +use std::path::Path; + +use tfhe_versionable::Versionize; + +use tfhe_backward_compat_data::generate::*; +use tfhe_backward_compat_data::*; + +pub(crate) fn store_versioned_test>( + msg: &Data, + dir: P, + test_filename: &str, +) { + generic_store_versioned_test(Versionize::versionize, msg, dir, test_filename) +} + +#[allow(dead_code)] +pub(crate) fn store_versioned_auxiliary>( + msg: &Data, + dir: P, + test_filename: &str, +) { + generic_store_versioned_auxiliary(Versionize::versionize, msg, dir, test_filename) +} + +/// This trait allows to convert version independent parameters types defined in +/// `tfhe-backward-compat-data` to the equivalent TFHE-rs parameters for this version. +/// +/// This is similar to `Into` but allows to circumvent the orphan rule. +pub(crate) trait ConvertParams { + fn convert(self) -> TfheRsParams; +} + +// Add here the impl of ConvertParams for the TestXXXParameterSet that you need. +// You can start by simply copying the implementations of this trait from the crate for +// the previous version, and then eventually fix parameter types that have been updated.