diff --git a/Cargo.lock b/Cargo.lock index 31e2881..1a39e28 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -45,9 +45,9 @@ dependencies = [ [[package]] name = "bitflags" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" dependencies = [ "serde", ] @@ -112,9 +112,9 @@ dependencies = [ [[package]] name = "concrete-csprng" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c7080e711c39c3dda834604f7c31f4634a38b8dddeb9f24db5d7801751415ad" +checksum = "90518357249582c16a6b64d7410243dfb3109d5bf0ad1665c058c9a59f2fc4cc" dependencies = [ "aes", "libc", @@ -217,9 +217,9 @@ dependencies = [ [[package]] name = "either" -version = "1.12.0" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "generic-array" @@ -414,18 +414,18 @@ checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" [[package]] name = "serde" -version = "1.0.203" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.203" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", @@ -466,9 +466,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.68" +version = "2.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "901fa70d88b9d6c98022e23b4136f9f3e54e4662c3bc1bd1d84a42a9a0f0c1e9" +checksum = "b146dcf730474b4bcd16c311627b31ede9ab149045db4d6088b3becaea046462" dependencies = [ "proc-macro2", "quote", @@ -493,14 +493,14 @@ dependencies = [ "rand_core", "rayon", "serde", - "tfhe-versionable", + "tfhe-versionable 0.1.0", ] [[package]] name = "tfhe" -version = "0.7.0" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dafa7eb0c9fadc8f2e26c83c2c2164e5b646e4a789952dc88d26daa53ae2b7d0" +checksum = "0623aaa0dc7836f4f7aa96602ef9c6a55f87cbcb962a94ed7a9e6d00daa32cfb" dependencies = [ "aligned-vec", "bincode", @@ -516,7 +516,7 @@ dependencies = [ "rayon", "serde", "sha3", - "tfhe-versionable", + "tfhe-versionable 0.2.0", ] [[package]] @@ -530,8 +530,9 @@ dependencies = [ "serde", "strum", "tfhe 0.6.4", - "tfhe 0.7.0", - "tfhe-versionable", + "tfhe 0.7.2", + "tfhe-versionable 0.1.0", + "tfhe-versionable 0.2.0", ] [[package]] @@ -543,7 +544,19 @@ dependencies = [ "aligned-vec", "num-complex", "serde", - "tfhe-versionable-derive", + "tfhe-versionable-derive 0.1.0", +] + +[[package]] +name = "tfhe-versionable" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bcae0d989f0eb92d5faae77dac5705ff1d9b82c59a38b36fd17edd6509c449c" +dependencies = [ + "aligned-vec", + "num-complex", + "serde", + "tfhe-versionable-derive 0.2.0", ] [[package]] @@ -557,6 +570,17 @@ dependencies = [ "syn", ] +[[package]] +name = "tfhe-versionable-derive" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2049a82376dc42df29b91961a3f58e7923270c976d9c762a43fd55da51d18c5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "typenum" version = "1.17.0" diff --git a/Cargo.toml b/Cargo.toml index e908759..445307b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,8 @@ edition = "2021" tfhe_0_6 = { version = "0.6", features = ["boolean", "integer", "shortint", "x86_64-unix", "experimental-force_fft_algo_dif4"], package = "tfhe", optional = true } tfhe_0_7 = { version = "0.7", features = ["boolean", "integer", "shortint", "x86_64-unix", "experimental-force_fft_algo_dif4"], package = "tfhe", optional = true } -tfhe-versionable = { version = "0.1", optional = true } +tfhe-versionable_0_1 = { version = "0.1", optional = true, package = "tfhe-versionable" } +tfhe-versionable_0_2 = { version = "0.2", optional = true, package = "tfhe-versionable" } # other deps serde = { version = "1.0", features = ["derive"] } @@ -29,5 +30,5 @@ required-features = ["generate"] [features] default = ["generate"] -generate = ["dep:tfhe_0_6", "dep:tfhe_0_7", "dep:tfhe-versionable"] +generate = ["dep:tfhe_0_6", "dep:tfhe_0_7", "dep:tfhe-versionable_0_1", "dep:tfhe-versionable_0_2"] load = ["dep:semver"] diff --git a/README.md b/README.md index 7fb2e71..99f92da 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # tfhe-rs backwards compatibility test corpus +TEST This repo contains various messages from [TFHE-rs](https://github.com/zama-ai/tfhe-rs) that have been versioned and serialized. The goal is to detect in TFHE-rs CI when the version of a type should be updated because a breaking change has been added. @@ -26,7 +27,7 @@ Any commits to `main` should be backported to the latest version branch. To re-generate the data, run the binary target for this project: `cargo run --release`. The prng is seeded with a fixed seed, so the data should be identical. # Adding a test for an existing type -To add a new test for a type that is already tested, you need to create a const global variable with the metadata for that test. The type of metadata depends on the type being tested (for example, the metadata for a test of the `ClientKey' from the `high_level_api' is `HlClientKEy'). Then go to the `data_vvv.rs` file (where "vvv" is the TFHE-rs version of the tested data) and update the `gen_xxx_data` method (where "xxx" is the API layer of your test (hl, shortint, integer,...)). In this method, create the object you want to test and serialize it using the `store_versioned_test` method. Add the metadata of your test to the vector returned by this method. +To add a new test for a type that is already tested, you need to create a const global variable with the metadata for that test. The type of metadata depends on the type being tested (for example, the metadata for a test of the `ClientKey' from the `high_level_api' is `HlClientKEy'). Then go to the `data_vvv.rs` file (where "vvv" is the TFHE-rs version of the tested data) and update the `gen_xxx_data` method (where "xxx" is the API layer of your test (hl, shortint, integer,...)). In this method, create the object you want to test and serialize it using the `store_versioned_test` macro. Add the metadata of your test to the vector returned by this method. The test will be automatically selected when you run TFHE-rs `make test_backward_compatibility`. @@ -55,7 +56,7 @@ impl TfhersVersion for V0_6 { let ct1 = fheint8::encrypt(HL_CT1_TEST.clear_value, &hl_client_key); // 3. Store it - store_versioned_test(&ct1, &dir, &HL_CT1_TEST.test_filename); + store_versioned_test!(&ct1, &dir, &HL_CT1_TEST.test_filename); // 4. Return the metadata vec![ diff --git a/data/0_7/high_level_api/client_key.cbor b/data/0_7/high_level_api/client_key.cbor new file mode 100644 index 0000000..5db721a --- /dev/null +++ b/data/0_7/high_level_api/client_key.cbor @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fa27dc2ea178bc9a55d7f6c5b3a6140c13aa85eece2049d6798d4f4bd0aa9dc5 +size 5075 diff --git a/data/0_7/high_level_api/client_key.cbor.bcode b/data/0_7/high_level_api/client_key.cbor.bcode deleted file mode 100644 index 4621873..0000000 --- a/data/0_7/high_level_api/client_key.cbor.bcode +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:0ebefafaff7add36d2df48083e77e2a8dc5f242f50ce6cfbab9bb19111cb88bf -size 22771 diff --git a/data/0_7/high_level_api/client_key.cbor.cbor b/data/0_7/high_level_api/client_key.cbor.cbor deleted file mode 100644 index 8ce1d1d..0000000 --- a/data/0_7/high_level_api/client_key.cbor.cbor +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:23f58e48c01d0f965a1b4b0c19be82d50cb732c6fac6bb6dbfbabe60529ff4fc -size 3457 diff --git a/data/0_7/high_level_api/hl_compressed_heterogeneous_list.bcode b/data/0_7/high_level_api/hl_compressed_heterogeneous_list.bcode new file mode 100644 index 0000000..3cc0079 --- /dev/null +++ b/data/0_7/high_level_api/hl_compressed_heterogeneous_list.bcode @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:465b15b89c3161a1dbcebd7f689b5f87f969f15b259c86925ca67d02045f6946 +size 2036 diff --git a/data/0_7/high_level_api/hl_compressed_heterogeneous_list.cbor b/data/0_7/high_level_api/hl_compressed_heterogeneous_list.cbor new file mode 100644 index 0000000..878d697 --- /dev/null +++ b/data/0_7/high_level_api/hl_compressed_heterogeneous_list.cbor @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ec9092e4435c434104c518bfc72563b3ec1e2c234d1fb5de94d1a3884d23124e +size 2469 diff --git a/data/0_7/high_level_api/hl_heterogeneous_list.bcode b/data/0_7/high_level_api/hl_heterogeneous_list.bcode index 084ff4f..d8d6d7b 100644 --- a/data/0_7/high_level_api/hl_heterogeneous_list.bcode +++ b/data/0_7/high_level_api/hl_heterogeneous_list.bcode @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:78db6b0a593adebb219ec1da31238f38d61425e9a5392aeea7a5143f5ab7f0bc -size 16980 +oid sha256:f60cf426e28d96ced5e76157c5ce42d07c1889f542e21b47de33792d3346c5be +size 16660 diff --git a/data/0_7/high_level_api/hl_heterogeneous_list.cbor b/data/0_7/high_level_api/hl_heterogeneous_list.cbor index 8e5ac34..b19fbc6 100644 --- a/data/0_7/high_level_api/hl_heterogeneous_list.cbor +++ b/data/0_7/high_level_api/hl_heterogeneous_list.cbor @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:0f12c274869b4091cfea079bc236cdc0915560f34972d4492cee2d3a1b501f91 -size 19196 +oid sha256:22ff1f7b7132bd01185b5e11bf68b8275068e82d061c2125f71112868ee42b19 +size 18834 diff --git a/data/0_7/high_level_api/hl_packed_heterogeneous_list.bcode b/data/0_7/high_level_api/hl_packed_heterogeneous_list.bcode index 13e54d1..8ac35c3 100644 --- a/data/0_7/high_level_api/hl_packed_heterogeneous_list.bcode +++ b/data/0_7/high_level_api/hl_packed_heterogeneous_list.bcode @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:e43e2385b0c6c05bf15280d9689eeab58b62258effead9f872db894715d03611 -size 16780 +oid sha256:372afc2ab7c0fbd458f4132a52f9e618896a1700d8d01f5f89da8ce2e784835f +size 16620 diff --git a/data/0_7/high_level_api/hl_packed_heterogeneous_list.cbor b/data/0_7/high_level_api/hl_packed_heterogeneous_list.cbor index d569d09..e957f31 100644 --- a/data/0_7/high_level_api/hl_packed_heterogeneous_list.cbor +++ b/data/0_7/high_level_api/hl_packed_heterogeneous_list.cbor @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ccc6acc6e7492a4f9fd21af4fcd1984a76ede20d0156aa6ab181f077c98f2aba -size 18971 +oid sha256:9001e41fe1d8053fad71cace70963aee0eb09358ce1497f5d9c6a8259d0433eb +size 18789 diff --git a/data/high_level_api.ron b/data/high_level_api.ron index d965dcc..e3440b3 100644 --- a/data/high_level_api.ron +++ b/data/high_level_api.ron @@ -326,4 +326,25 @@ ], )), ), + ( + tfhe_version_min: "0.7", + tfhe_module: "high_level_api", + metadata: HlHeterogeneousCiphertextList(( + test_filename: "hl_compressed_heterogeneous_list", + key_filename: "client_key.cbor", + compressed: true, + clear_values: [ + 17, + 18446744073709551604, + 0, + 1, + ], + data_kinds: [ + Unsigned, + Signed, + Bool, + Bool, + ], + )), + ), ] \ No newline at end of file diff --git a/src/data_0_6.rs b/src/data_0_6.rs index fe61ba1..6d0b28d 100644 --- a/src/data_0_6.rs +++ b/src/data_0_6.rs @@ -24,16 +24,22 @@ use tfhe_0_6::{ CompressedFheBool, CompressedFheInt8, CompressedFheUint8, CompressedPublicKey, CompressedServerKey, ConfigBuilder, FheBool, FheInt8, FheUint8, PublicKey, }; -use tfhe_versionable::Versionize; +use tfhe_versionable_0_1::Versionize; use crate::{ - generate::{save_cbor, store_versioned_test, TfhersVersion, VALID_TEST_PARAMS}, + generate::{save_cbor, store_versioned_test_01, TfhersVersion, VALID_TEST_PARAMS}, HlBoolCiphertextListTest, HlBoolCiphertextTest, HlCiphertextListTest, HlCiphertextTest, HlClientKeyTest, HlPublicKeyTest, HlServerKeyTest, HlSignedCiphertextListTest, HlSignedCiphertextTest, ShortintCiphertextTest, ShortintClientKeyTest, TestMetadata, TestParameterSet, HL_MODULE_NAME, SHORTINT_MODULE_NAME, }; +macro_rules! store_versioned_test { + ($msg:expr, $dir:expr, $test_filename:expr $(,)? ) => { + store_versioned_test_01($msg, $dir, $test_filename) + }; +} + impl From for ClassicPBSParameters { fn from(value: TestParameterSet) -> Self { ClassicPBSParameters { @@ -295,7 +301,7 @@ impl TfhersVersion for V0_6 { // generate a client key let shortint_client_key = shortint::ClientKey::new(SHORTINT_CLIENTKEY_TEST.parameters); - store_versioned_test( + store_versioned_test!( &shortint_client_key, &dir, &SHORTINT_CLIENTKEY_TEST.test_filename, @@ -306,8 +312,8 @@ impl TfhersVersion for V0_6 { let ct2 = shortint_client_key.encrypt(SHORTINT_CT2_TEST.clear_value); // Serialize them - store_versioned_test(&ct1, &dir, &SHORTINT_CT1_TEST.test_filename); - store_versioned_test(&ct2, &dir, &SHORTINT_CT2_TEST.test_filename); + store_versioned_test!(&ct1, &dir, &SHORTINT_CT1_TEST.test_filename); + store_versioned_test!(&ct2, &dir, &SHORTINT_CT2_TEST.test_filename); vec![ TestMetadata::ShortintClientKey(SHORTINT_CLIENTKEY_TEST), @@ -339,31 +345,31 @@ impl TfhersVersion for V0_6 { let compact_pub_key = CompactPublicKey::new(&hl_client_key); let compressed_compact_pub_key = CompressedCompactPublicKey::new(&hl_client_key); - store_versioned_test(&hl_client_key, &dir, &HL_CLIENTKEY_TEST.test_filename); + store_versioned_test!(&hl_client_key, &dir, &HL_CLIENTKEY_TEST.test_filename); - store_versioned_test(&hl_server_key, &dir, &HL_SERVERKEY_TEST.test_filename); - store_versioned_test( + store_versioned_test!(&hl_server_key, &dir, &HL_SERVERKEY_TEST.test_filename); + store_versioned_test!( &compressed_server_key, &dir, &HL_COMPRESSED_SERVERKEY_TEST.test_filename, ); - store_versioned_test(&pub_key, &dir, &HL_PUBKEY_TEST.test_filename); + store_versioned_test!(&pub_key, &dir, &HL_PUBKEY_TEST.test_filename); save_cbor( &client_key_for_pk.versionize(), dir.join(&*HL_PUBKEY_TEST.client_key_filename), ); - store_versioned_test( + store_versioned_test!( &compressed_pub_key, &dir, &HL_COMPRESSED_PUBKEY_TEST.test_filename, ); - store_versioned_test( + store_versioned_test!( &compact_pub_key, &dir, &HL_COMPACT_PUBKEY_TEST.test_filename, ); - store_versioned_test( + store_versioned_test!( &compressed_compact_pub_key, &dir, &HL_COMPRESSED_COMPACT_PUBKEY_TEST.test_filename, @@ -422,54 +428,54 @@ impl TfhersVersion for V0_6 { CompactFheBoolList::encrypt(&HL_BOOL_LIST_TEST.clear_values, &compact_pub_key); // Serialize them - store_versioned_test(&ct1, &dir, &HL_CT1_TEST.test_filename); - store_versioned_test(&ct2, &dir, &HL_CT2_TEST.test_filename); - store_versioned_test( + store_versioned_test!(&ct1, &dir, &HL_CT1_TEST.test_filename); + store_versioned_test!(&ct2, &dir, &HL_CT2_TEST.test_filename); + store_versioned_test!( &compressed_ct1, &dir, &HL_COMPRESSED_SEEDED_CT_TEST.test_filename, ); - store_versioned_test( + store_versioned_test!( &compressed_ct2, &dir, &HL_COMPRESSED_CT_MODSWITCHED_TEST.test_filename, ); - store_versioned_test(&compact_ct, &dir, &HL_COMPACT_CT_TEST.test_filename); - store_versioned_test(&ct_list, &dir, &HL_CT_LIST_TEST.test_filename); + store_versioned_test!(&compact_ct, &dir, &HL_COMPACT_CT_TEST.test_filename); + store_versioned_test!(&ct_list, &dir, &HL_CT_LIST_TEST.test_filename); - store_versioned_test(&ct1_signed, &dir, &HL_SIGNED_CT1_TEST.test_filename); - store_versioned_test(&ct2_signed, &dir, &HL_SIGNED_CT2_TEST.test_filename); - store_versioned_test( + store_versioned_test!(&ct1_signed, &dir, &HL_SIGNED_CT1_TEST.test_filename); + store_versioned_test!(&ct2_signed, &dir, &HL_SIGNED_CT2_TEST.test_filename); + store_versioned_test!( &compressed_ct1_signed, &dir, &HL_SIGNED_COMPRESSED_SEEDED_CT_TEST.test_filename, ); - store_versioned_test( + store_versioned_test!( &compressed_ct2_signed, &dir, &HL_SIGNED_COMPRESSED_CT_MODSWITCHED_TEST.test_filename, ); - store_versioned_test( + store_versioned_test!( &compact_ct_signed, &dir, &HL_SIGNED_COMPACT_CT_TEST.test_filename, ); - store_versioned_test(&ct_list_signed, &dir, &HL_SIGNED_CT_LIST_TEST.test_filename); + store_versioned_test!(&ct_list_signed, &dir, &HL_SIGNED_CT_LIST_TEST.test_filename); - store_versioned_test(&bool1, &dir, &HL_BOOL1_TEST.test_filename); - store_versioned_test(&bool2, &dir, &HL_BOOL2_TEST.test_filename); - store_versioned_test( + store_versioned_test!(&bool1, &dir, &HL_BOOL1_TEST.test_filename); + store_versioned_test!(&bool2, &dir, &HL_BOOL2_TEST.test_filename); + store_versioned_test!( &compressed_bool1, &dir, &HL_COMPRESSED_BOOL_SEEDED_TEST.test_filename, ); - store_versioned_test( + store_versioned_test!( &compressed_bool2, &dir, &HL_COMPRESSED_BOOL_MODSWITCHED_TEST.test_filename, ); - store_versioned_test(&compact_bool, &dir, &HL_COMPACT_BOOL_TEST.test_filename); - store_versioned_test(&bool_list, &dir, &HL_BOOL_LIST_TEST.test_filename); + store_versioned_test!(&compact_bool, &dir, &HL_COMPACT_BOOL_TEST.test_filename); + store_versioned_test!(&bool_list, &dir, &HL_BOOL_LIST_TEST.test_filename); vec![ TestMetadata::HlClientKey(HL_CLIENTKEY_TEST), diff --git a/src/data_0_7.rs b/src/data_0_7.rs index 4e95689..4476902 100644 --- a/src/data_0_7.rs +++ b/src/data_0_7.rs @@ -5,23 +5,41 @@ use tfhe_0_7::{ core_crypto::commons::{ generators::DeterministicSeeder, math::random::ActivatedRandomGenerator, }, + generate_keys, + prelude::FheEncrypt, + set_server_key, shortint::{ engine::ShortintEngine, parameters::{ DecompositionBaseLog, DecompositionLevelCount, DynamicDistribution, GlweDimension, - LweDimension, PolynomialSize, StandardDev, + LweDimension, PolynomialSize, StandardDev, COMP_PARAM_MESSAGE_2_CARRY_2, }, CarryModulus, CiphertextModulus, ClassicPBSParameters, EncryptionKeyChoice, MaxNoiseLevel, MessageModulus, PBSParameters, }, - ClientKey, CompactCiphertextList, CompactPublicKey, Seed, + CompactCiphertextList, CompactPublicKey, CompressedCiphertextListBuilder, FheBool, FheInt8, + FheUint8, Seed, }; use crate::{ - generate::{store_versioned_test, TfhersVersion, VALID_TEST_PARAMS}, + generate::{ + store_versioned_auxiliary_02, store_versioned_test_02, TfhersVersion, VALID_TEST_PARAMS, + }, DataKind, HlHeterogeneousCiphertextListTest, TestMetadata, TestParameterSet, HL_MODULE_NAME, }; +macro_rules! store_versioned_test { + ($msg:expr, $dir:expr, $test_filename:expr $(,)? ) => { + store_versioned_test_02($msg, $dir, $test_filename) + }; +} + +macro_rules! store_versioned_auxiliary { + ($msg:expr, $dir:expr, $test_filename:expr $(,)? ) => { + store_versioned_auxiliary_02($msg, $dir, $test_filename) + }; +} + impl From for ClassicPBSParameters { fn from(value: TestParameterSet) -> Self { ClassicPBSParameters { @@ -61,20 +79,6 @@ impl From for PBSParameters { } } -const HL_PACKED_COMPACTLIST_TEST: HlHeterogeneousCiphertextListTest = - HlHeterogeneousCiphertextListTest { - test_filename: Cow::Borrowed("hl_packed_heterogeneous_list"), - key_filename: Cow::Borrowed("client_key.cbor"), - 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, - }; - const HL_COMPACTLIST_TEST: HlHeterogeneousCiphertextListTest = HlHeterogeneousCiphertextListTest { test_filename: Cow::Borrowed("hl_heterogeneous_list"), key_filename: Cow::Borrowed("client_key.cbor"), @@ -88,6 +92,29 @@ const HL_COMPACTLIST_TEST: HlHeterogeneousCiphertextListTest = HlHeterogeneousCi compressed: false, }; +const HL_PACKED_COMPACTLIST_TEST: HlHeterogeneousCiphertextListTest = + HlHeterogeneousCiphertextListTest { + test_filename: Cow::Borrowed("hl_packed_heterogeneous_list"), + key_filename: Cow::Borrowed("client_key.cbor"), + clear_values: HL_COMPACTLIST_TEST.clear_values, + data_kinds: HL_COMPACTLIST_TEST.data_kinds, + compressed: false, + }; + +const HL_COMPRESSED_LIST_TEST: HlHeterogeneousCiphertextListTest = + HlHeterogeneousCiphertextListTest { + test_filename: Cow::Borrowed("hl_compressed_heterogeneous_list"), + key_filename: Cow::Borrowed("client_key.cbor"), + 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: true, + }; + pub struct V0_7; impl TfhersVersion for V0_7 { @@ -113,33 +140,65 @@ impl TfhersVersion for V0_7 { create_dir_all(&dir).unwrap(); // Generate a compact public key needed to create a compact list - let config = - tfhe_0_7::ConfigBuilder::with_custom_parameters(VALID_TEST_PARAMS, None).build(); - let hl_client_key = ClientKey::generate(config); + let config = tfhe_0_7::ConfigBuilder::with_custom_parameters(VALID_TEST_PARAMS, None) + .enable_compression(COMP_PARAM_MESSAGE_2_CARRY_2) + .build(); + let (hl_client_key, hl_server_key) = generate_keys(config); + + set_server_key(hl_server_key); + let compact_pub_key = CompactPublicKey::new(&hl_client_key); // Store the associated client key to be able to decrypt the ciphertexts in the list - store_versioned_test(&hl_client_key, &dir, &HL_COMPACTLIST_TEST.key_filename); + store_versioned_auxiliary!(&hl_client_key, &dir, &HL_COMPACTLIST_TEST.key_filename); + // Generate heterogeneous list data let mut compact_builder = CompactCiphertextList::builder(&compact_pub_key); compact_builder - .push(17u32) - .push(-1i64) - .push(false) - .push(true); + .push(HL_COMPACTLIST_TEST.clear_values[0] as u8) + .push(HL_COMPACTLIST_TEST.clear_values[1] as i8) + .push(HL_COMPACTLIST_TEST.clear_values[2] != 0) + .push(HL_COMPACTLIST_TEST.clear_values[3] != 0); + let compact_list_packed = compact_builder.build_packed(); let compact_list = compact_builder.build(); - store_versioned_test( + let mut compressed_builder = CompressedCiphertextListBuilder::new(); + compressed_builder + .push(FheUint8::encrypt( + HL_COMPRESSED_LIST_TEST.clear_values[0] as u8, + &hl_client_key, + )) + .push(FheInt8::encrypt( + HL_COMPRESSED_LIST_TEST.clear_values[1] as i8, + &hl_client_key, + )) + .push(FheBool::encrypt( + HL_COMPRESSED_LIST_TEST.clear_values[2] != 0, + &hl_client_key, + )) + .push(FheBool::encrypt( + HL_COMPRESSED_LIST_TEST.clear_values[3] != 0, + &hl_client_key, + )); + let compressed_list = compressed_builder.build().unwrap(); + + store_versioned_test!( &compact_list_packed, &dir, &HL_PACKED_COMPACTLIST_TEST.test_filename, ); - store_versioned_test(&compact_list, &dir, &HL_COMPACTLIST_TEST.test_filename); + store_versioned_test!(&compact_list, &dir, &HL_COMPACTLIST_TEST.test_filename); + store_versioned_test!( + &compressed_list, + &dir, + &HL_COMPRESSED_LIST_TEST.test_filename, + ); vec![ TestMetadata::HlHeterogeneousCiphertextList(HL_PACKED_COMPACTLIST_TEST), TestMetadata::HlHeterogeneousCiphertextList(HL_COMPACTLIST_TEST), + TestMetadata::HlHeterogeneousCiphertextList(HL_COMPRESSED_LIST_TEST), ] } } diff --git a/src/generate.rs b/src/generate.rs index cdcbe4e..8916ed6 100644 --- a/src/generate.rs +++ b/src/generate.rs @@ -6,7 +6,8 @@ use std::{ use bincode::Options; use serde::Serialize; -use tfhe_versionable::Versionize; +use tfhe_versionable_0_1::Versionize as Versionize01; +use tfhe_versionable_0_2::Versionize as Versionize02; use crate::{data_dir, dir_for_version, TestMetadata, TestParameterSet}; @@ -59,22 +60,47 @@ pub fn save_bcode>(msg: &Data, path: P) { options.serialize_into(&mut file, msg).unwrap(); } -/// Stores the test data in `dir`, encoded in both cbor and bincode -pub fn store_versioned_test>( - msg: &Data, - dir: P, - test_filename: &str, -) { - let versioned = msg.versionize(); - - // Store in cbor - let filename_cbor = format!("{}.cbor", test_filename); - save_cbor(&versioned, dir.as_ref().join(filename_cbor)); - - // Store in bincode - let filename_bincode = format!("{}.bcode", test_filename); - save_bcode(&versioned, dir.as_ref().join(filename_bincode)); +/// Stores the test data in `dir`, encoded in both cbor and bincode, using the right tfhe-versionable version +macro_rules! define_store_versioned_test_fn { + ($fn_name:ident, $versionize_trait:ident) => { + pub fn $fn_name>( + msg: &Data, + dir: P, + test_filename: &str, + ) { + let versioned = msg.versionize(); + + // Store in cbor + let filename_cbor = format!("{}.cbor", test_filename); + save_cbor(&versioned, dir.as_ref().join(filename_cbor)); + + // Store in bincode + let filename_bincode = format!("{}.bcode", test_filename); + save_bcode(&versioned, dir.as_ref().join(filename_bincode)); + } + }; +} +define_store_versioned_test_fn!(store_versioned_test_01, Versionize01); +define_store_versioned_test_fn!(store_versioned_test_02, Versionize02); + +/// Stores the auxiliary data in `dir`, encoded in cbor, using the right tfhe-versionable version +macro_rules! define_store_versioned_auxiliary_fn { + ($fn_name:ident, $versionize_trait:ident) => { + pub fn $fn_name>( + msg: &Data, + dir: P, + test_filename: &str, + ) { + let versioned = msg.versionize(); + + // Store in cbor + let filename_cbor = format!("{}", test_filename); + save_cbor(&versioned, dir.as_ref().join(filename_cbor)); + } + }; } +define_store_versioned_auxiliary_fn!(store_versioned_auxiliary_01, Versionize01); +define_store_versioned_auxiliary_fn!(store_versioned_auxiliary_02, Versionize02); pub fn store_metadata>(value: &Meta, path: P) { let serialized = ron::ser::to_string_pretty(value, ron::ser::PrettyConfig::default()).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 921ecb9..3092735 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ use std::{ }; #[cfg(feature = "load")] -use semver::{Version, VersionReq}; +use semver::{Prerelease, Version, VersionReq}; #[cfg(feature = "load")] use std::fmt::Display; use strum::Display; @@ -269,6 +269,7 @@ impl TestType for HlBoolCiphertextTest { } } +/// Legacy compact unsigned ciphertext lists (deprected in 0.7) #[derive(Serialize, Deserialize, Clone, Debug)] pub struct HlCiphertextListTest { pub test_filename: Cow<'static, str>, @@ -290,6 +291,7 @@ impl TestType for HlCiphertextListTest { } } +/// Legacy compact signed ciphertext lists (deprected in 0.7) #[derive(Serialize, Deserialize, Clone, Debug)] pub struct HlSignedCiphertextListTest { pub test_filename: Cow<'static, str>, @@ -311,6 +313,7 @@ impl TestType for HlSignedCiphertextListTest { } } +/// Legacy compact bool ciphertext lists (deprected in 0.7) #[derive(Serialize, Deserialize, Clone, Debug)] pub struct HlBoolCiphertextListTest { pub test_filename: Cow<'static, str>, @@ -332,7 +335,7 @@ impl TestType for HlBoolCiphertextListTest { } } -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Clone, Copy, Debug)] pub enum DataKind { Bool, Signed, @@ -391,7 +394,10 @@ pub struct Testcase { #[cfg(feature = "load")] impl Testcase { pub fn is_valid_for_version(&self, version: &str) -> bool { - let tfhe_version = Version::parse(version).unwrap(); + let mut tfhe_version = Version::parse(version).unwrap(); + + // Removes the pre-release tag because matches will always return + tfhe_version.pre = Prerelease::EMPTY; let req = format!(">={}", self.tfhe_version_min); let min_version = VersionReq::parse(&req).unwrap();