mirror of
https://github.com/zama-ai/tfhe-rs.git
synced 2026-01-09 22:57:59 -05:00
chore(backward): add backward compat tests for rerand
This commit is contained in:
committed by
Nicolas Sarlin
parent
1b1e6a7068
commit
70e1828c58
@@ -10,6 +10,7 @@ tfhe-versionable = { path = "../utils/tfhe-versionable" }
|
|||||||
tfhe-backward-compat-data = { path = "../utils/tfhe-backward-compat-data", default-features = false, features = [
|
tfhe-backward-compat-data = { path = "../utils/tfhe-backward-compat-data", default-features = false, features = [
|
||||||
"load",
|
"load",
|
||||||
] }
|
] }
|
||||||
|
rand = { workspace = true }
|
||||||
cargo_toml = "0.22"
|
cargo_toml = "0.22"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ use crate::{load_and_unversionize, TestedModule};
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
#[cfg(feature = "zk-pok")]
|
#[cfg(feature = "zk-pok")]
|
||||||
use tfhe::integer::parameters::DynamicDistribution;
|
use tfhe::integer::parameters::DynamicDistribution;
|
||||||
use tfhe::prelude::{CiphertextList, FheDecrypt, FheEncrypt, ParameterSetConformant, SquashNoise};
|
use tfhe::prelude::{
|
||||||
|
CiphertextList, FheDecrypt, FheEncrypt, ParameterSetConformant, ReRandomize, SquashNoise,
|
||||||
|
};
|
||||||
#[cfg(feature = "zk-pok")]
|
#[cfg(feature = "zk-pok")]
|
||||||
use tfhe::shortint::parameters::{
|
use tfhe::shortint::parameters::{
|
||||||
CompactCiphertextListExpansionKind, CompactPublicKeyEncryptionParameters,
|
CompactCiphertextListExpansionKind, CompactPublicKeyEncryptionParameters,
|
||||||
@@ -17,7 +19,8 @@ use tfhe::{
|
|||||||
set_server_key, ClientKey, CompactCiphertextList, CompressedCiphertextList,
|
set_server_key, ClientKey, CompactCiphertextList, CompressedCiphertextList,
|
||||||
CompressedCompactPublicKey, CompressedFheBool, CompressedFheInt8, CompressedFheUint8,
|
CompressedCompactPublicKey, CompressedFheBool, CompressedFheInt8, CompressedFheUint8,
|
||||||
CompressedPublicKey, CompressedServerKey, CompressedSquashedNoiseCiphertextList, FheBool,
|
CompressedPublicKey, CompressedServerKey, CompressedSquashedNoiseCiphertextList, FheBool,
|
||||||
FheInt8, FheUint8, SquashedNoiseFheBool, SquashedNoiseFheInt, SquashedNoiseFheUint,
|
FheInt8, FheUint8, ReRandomizationContext, SquashedNoiseFheBool, SquashedNoiseFheInt,
|
||||||
|
SquashedNoiseFheUint,
|
||||||
};
|
};
|
||||||
#[cfg(feature = "zk-pok")]
|
#[cfg(feature = "zk-pok")]
|
||||||
use tfhe::{CompactPublicKey, ProvenCompactCiphertextList};
|
use tfhe::{CompactPublicKey, ProvenCompactCiphertextList};
|
||||||
@@ -347,6 +350,7 @@ pub fn test_hl_pubkey(
|
|||||||
};
|
};
|
||||||
FheUint8::encrypt(value, &public_key)
|
FheUint8::encrypt(value, &public_key)
|
||||||
};
|
};
|
||||||
|
|
||||||
let decrypted: u8 = encrypted.decrypt(&client_key);
|
let decrypted: u8 = encrypted.decrypt(&client_key);
|
||||||
|
|
||||||
if decrypted != value {
|
if decrypted != value {
|
||||||
@@ -375,7 +379,7 @@ pub fn test_hl_serverkey(
|
|||||||
.map_err(|e| test.failure(e, format))?;
|
.map_err(|e| test.failure(e, format))?;
|
||||||
|
|
||||||
let v1 = 73u8;
|
let v1 = 73u8;
|
||||||
let ct1 = FheUint8::encrypt(v1, &client_key);
|
let mut ct1 = FheUint8::encrypt(v1, &client_key);
|
||||||
let v2 = 102u8;
|
let v2 = 102u8;
|
||||||
let ct2 = FheUint8::encrypt(v2, &client_key);
|
let ct2 = FheUint8::encrypt(v2, &client_key);
|
||||||
|
|
||||||
@@ -386,7 +390,8 @@ pub fn test_hl_serverkey(
|
|||||||
load_and_unversionize(dir, test, format)?
|
load_and_unversionize(dir, test, format)?
|
||||||
};
|
};
|
||||||
|
|
||||||
let has_noise_squashing = key.noise_squashing_key().is_some();
|
let has_noise_squashing = key.supports_noise_squashing();
|
||||||
|
let has_rerand = key.supports_ciphertext_re_randomization();
|
||||||
set_server_key(key);
|
set_server_key(key);
|
||||||
|
|
||||||
if has_noise_squashing {
|
if has_noise_squashing {
|
||||||
@@ -402,6 +407,47 @@ pub fn test_hl_serverkey(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(rerand_cpk_filename) = test.rerand_cpk_filename.as_ref() {
|
||||||
|
if has_rerand {
|
||||||
|
let rerand_cpk_file = dir.join(rerand_cpk_filename.to_string());
|
||||||
|
let public_key = CompressedCompactPublicKey::unversionize(
|
||||||
|
load_versioned_auxiliary(rerand_cpk_file).map_err(|e| test.failure(e, format))?,
|
||||||
|
)
|
||||||
|
.map_err(|e| test.failure(e, format))?
|
||||||
|
.decompress();
|
||||||
|
|
||||||
|
let nonce: [u8; 256 / 8] = rand::random();
|
||||||
|
let mut re_rand_context = ReRandomizationContext::new(
|
||||||
|
*b"TFHE_Rrd",
|
||||||
|
[b"FheUint8".as_slice(), nonce.as_slice()],
|
||||||
|
*b"TFHE_Enc",
|
||||||
|
);
|
||||||
|
|
||||||
|
re_rand_context.add_ciphertext(&ct1);
|
||||||
|
let mut seed_gen = re_rand_context.finalize();
|
||||||
|
|
||||||
|
ct1.re_randomize(&public_key, seed_gen.next_seed().unwrap())
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
#[allow(clippy::eq_op)]
|
||||||
|
let rrd = &ct1 & &ct1;
|
||||||
|
let res: u8 = rrd.decrypt(&client_key);
|
||||||
|
if res != v1 {
|
||||||
|
return Err(test.failure(
|
||||||
|
format!(
|
||||||
|
"Invalid result for rerand using loaded server key, expected {v1} got {res}",
|
||||||
|
),
|
||||||
|
format,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(test.failure(
|
||||||
|
"Test requires rerand key but server key does not have it".to_string(),
|
||||||
|
format,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let ct_sum = ct1 + ct2;
|
let ct_sum = ct1 + ct2;
|
||||||
let sum: u8 = ct_sum.decrypt(&client_key);
|
let sum: u8 = ct_sum.decrypt(&client_key);
|
||||||
|
|
||||||
|
|||||||
@@ -141,12 +141,20 @@ impl ServerKey {
|
|||||||
self.key.re_randomization_cpk_casting_key()
|
self.key.re_randomization_cpk_casting_key()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn supports_ciphertext_re_randomization(&self) -> bool {
|
||||||
|
self.re_randomization_cpk_casting_key().is_some()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn noise_squashing_key(
|
pub fn noise_squashing_key(
|
||||||
&self,
|
&self,
|
||||||
) -> Option<&crate::integer::noise_squashing::NoiseSquashingKey> {
|
) -> Option<&crate::integer::noise_squashing::NoiseSquashingKey> {
|
||||||
self.key.noise_squashing_key.as_ref()
|
self.key.noise_squashing_key.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn supports_noise_squashing(&self) -> bool {
|
||||||
|
self.noise_squashing_key().is_some()
|
||||||
|
}
|
||||||
|
|
||||||
pub(in crate::high_level_api) fn message_modulus(&self) -> MessageModulus {
|
pub(in crate::high_level_api) fn message_modulus(&self) -> MessageModulus {
|
||||||
self.key.message_modulus()
|
self.key.message_modulus()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:0ef52bb82a792d2530c7e46f80dccba6a9f5c6d7b94d5f87bdd4637ceeed8197
|
||||||
|
size 3338
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
version https://git-lfs.github.com/spec/v1
|
version https://git-lfs.github.com/spec/v1
|
||||||
oid sha256:11975834f2209a223da64547d50547f8f01798a5110388f5384c8631bdd82b19
|
oid sha256:8e58e8549f1ab58bed9f77ec936b995cb0238f43b813dbc64651b96aab6d9452
|
||||||
size 82427
|
size 82428
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
version https://git-lfs.github.com/spec/v1
|
version https://git-lfs.github.com/spec/v1
|
||||||
oid sha256:db5483f4fcf34659639acaaa25e755ded39794f762bba8ff18ef462073747435
|
oid sha256:d422e48c8327dfe2cdada7da850b12aacc81d198b408e650e8dfcab3ec164487
|
||||||
size 7336
|
size 7370
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:a29e1f621ff00d68c39860be58660c3359801cdd6cdd55c09fffd80c4b86ba2f
|
||||||
|
size 747
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:1d1f9127b621084669da234edfb15d6ec0a9811b4a11920f214f6a70710bcefd
|
||||||
|
size 856624
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:827caa582a73af08043012732444f4ea89eaa445095b4cfc0b8dfb2a193f504d
|
||||||
|
size 972494
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
version https://git-lfs.github.com/spec/v1
|
version https://git-lfs.github.com/spec/v1
|
||||||
oid sha256:4acc2ef58e90faae1d8e0f5ebc0db83740f08e6b9be7cdc1dbe0034c37ee2102
|
oid sha256:523fb601cb28251fce9fbf4a784b890336bda2cfd1b3db64c996cb89fc38f212
|
||||||
size 15614959
|
size 15614960
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
version https://git-lfs.github.com/spec/v1
|
version https://git-lfs.github.com/spec/v1
|
||||||
oid sha256:f2b6cd9d2170c7782b525a01dc8021c3b55d11d67f66215bbf16cc4e84b98b22
|
oid sha256:147dd3b499ce1385aeba3d54e60b12eff302ddbd98d3b218cb9972a0321251f2
|
||||||
size 17632539
|
size 17632589
|
||||||
|
|||||||
@@ -357,6 +357,7 @@
|
|||||||
metadata: HlServerKey((
|
metadata: HlServerKey((
|
||||||
test_filename: "compressed_server_key",
|
test_filename: "compressed_server_key",
|
||||||
client_key_filename: "client_key.cbor",
|
client_key_filename: "client_key.cbor",
|
||||||
|
rerand_cpk_filename: None,
|
||||||
compressed: true,
|
compressed: true,
|
||||||
)),
|
)),
|
||||||
),
|
),
|
||||||
@@ -366,6 +367,7 @@
|
|||||||
metadata: HlServerKey((
|
metadata: HlServerKey((
|
||||||
test_filename: "server_key_with_compression",
|
test_filename: "server_key_with_compression",
|
||||||
client_key_filename: "client_key.cbor",
|
client_key_filename: "client_key.cbor",
|
||||||
|
rerand_cpk_filename: None,
|
||||||
compressed: false,
|
compressed: false,
|
||||||
)),
|
)),
|
||||||
),
|
),
|
||||||
@@ -448,6 +450,7 @@
|
|||||||
metadata: HlServerKey((
|
metadata: HlServerKey((
|
||||||
test_filename: "server_key_ms_noise_reduction",
|
test_filename: "server_key_ms_noise_reduction",
|
||||||
client_key_filename: "client_key_ms_noise_reduction.cbor",
|
client_key_filename: "client_key_ms_noise_reduction.cbor",
|
||||||
|
rerand_cpk_filename: None,
|
||||||
compressed: false,
|
compressed: false,
|
||||||
)),
|
)),
|
||||||
),
|
),
|
||||||
@@ -504,6 +507,7 @@
|
|||||||
metadata: HlServerKey((
|
metadata: HlServerKey((
|
||||||
test_filename: "server_key_with_noise_squashing",
|
test_filename: "server_key_with_noise_squashing",
|
||||||
client_key_filename: "client_key_with_noise_squashing",
|
client_key_filename: "client_key_with_noise_squashing",
|
||||||
|
rerand_cpk_filename: None,
|
||||||
compressed: false,
|
compressed: false,
|
||||||
)),
|
)),
|
||||||
),
|
),
|
||||||
@@ -513,6 +517,7 @@
|
|||||||
metadata: HlServerKey((
|
metadata: HlServerKey((
|
||||||
test_filename: "server_key_with_noise_squashing_compressed",
|
test_filename: "server_key_with_noise_squashing_compressed",
|
||||||
client_key_filename: "client_key_with_noise_squashing",
|
client_key_filename: "client_key_with_noise_squashing",
|
||||||
|
rerand_cpk_filename: None,
|
||||||
compressed: true,
|
compressed: true,
|
||||||
)),
|
)),
|
||||||
),
|
),
|
||||||
@@ -613,6 +618,7 @@
|
|||||||
metadata: HlServerKey((
|
metadata: HlServerKey((
|
||||||
test_filename: "server_key_ms_mean_compensation",
|
test_filename: "server_key_ms_mean_compensation",
|
||||||
client_key_filename: "client_key_ms_mean_compensation.cbor",
|
client_key_filename: "client_key_ms_mean_compensation.cbor",
|
||||||
|
rerand_cpk_filename: None,
|
||||||
compressed: false,
|
compressed: false,
|
||||||
)),
|
)),
|
||||||
),
|
),
|
||||||
@@ -673,6 +679,7 @@
|
|||||||
metadata: HlServerKey((
|
metadata: HlServerKey((
|
||||||
test_filename: "server_key_with_noise_squashing",
|
test_filename: "server_key_with_noise_squashing",
|
||||||
client_key_filename: "client_key_with_noise_squashing",
|
client_key_filename: "client_key_with_noise_squashing",
|
||||||
|
rerand_cpk_filename: None,
|
||||||
compressed: false,
|
compressed: false,
|
||||||
)),
|
)),
|
||||||
),
|
),
|
||||||
@@ -685,4 +692,14 @@
|
|||||||
clear_value: 42,
|
clear_value: 42,
|
||||||
)),
|
)),
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
tfhe_version_min: "1.4",
|
||||||
|
tfhe_module: "high_level_api",
|
||||||
|
metadata: HlServerKey((
|
||||||
|
test_filename: "server_key_for_rerand",
|
||||||
|
client_key_filename: "client_key_for_rerand",
|
||||||
|
rerand_cpk_filename: Some("cpk_for_rerand"),
|
||||||
|
compressed: false,
|
||||||
|
)),
|
||||||
|
),
|
||||||
]
|
]
|
||||||
@@ -165,12 +165,14 @@ const HL_CLIENTKEY_TEST: HlClientKeyTest = HlClientKeyTest {
|
|||||||
const HL_COMPRESSED_SERVERKEY_TEST: HlServerKeyTest = HlServerKeyTest {
|
const HL_COMPRESSED_SERVERKEY_TEST: HlServerKeyTest = HlServerKeyTest {
|
||||||
test_filename: Cow::Borrowed("compressed_server_key"),
|
test_filename: Cow::Borrowed("compressed_server_key"),
|
||||||
client_key_filename: Cow::Borrowed("client_key.cbor"),
|
client_key_filename: Cow::Borrowed("client_key.cbor"),
|
||||||
|
rerand_cpk_filename: None,
|
||||||
compressed: true,
|
compressed: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
const HL_SERVERKEY_WITH_COMPRESSION_TEST: HlServerKeyTest = HlServerKeyTest {
|
const HL_SERVERKEY_WITH_COMPRESSION_TEST: HlServerKeyTest = HlServerKeyTest {
|
||||||
test_filename: Cow::Borrowed("server_key_with_compression"),
|
test_filename: Cow::Borrowed("server_key_with_compression"),
|
||||||
client_key_filename: Cow::Borrowed("client_key.cbor"),
|
client_key_filename: Cow::Borrowed("client_key.cbor"),
|
||||||
|
rerand_cpk_filename: None,
|
||||||
compressed: false,
|
compressed: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -171,6 +171,7 @@ const HL_CLIENTKEY_MS_NOISE_REDUCTION_TEST: HlClientKeyTest = HlClientKeyTest {
|
|||||||
const HL_SERVERKEY_MS_NOISE_REDUCTION_TEST: HlServerKeyTest = HlServerKeyTest {
|
const HL_SERVERKEY_MS_NOISE_REDUCTION_TEST: HlServerKeyTest = HlServerKeyTest {
|
||||||
test_filename: Cow::Borrowed("server_key_ms_noise_reduction"),
|
test_filename: Cow::Borrowed("server_key_ms_noise_reduction"),
|
||||||
client_key_filename: Cow::Borrowed("client_key_ms_noise_reduction.cbor"),
|
client_key_filename: Cow::Borrowed("client_key_ms_noise_reduction.cbor"),
|
||||||
|
rerand_cpk_filename: None,
|
||||||
compressed: false,
|
compressed: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -207,12 +207,14 @@ const HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST: HlClientKeyTest = HlClientKeyTest
|
|||||||
const HL_SERVERKEY_MS_NOISE_REDUCTION_TEST: HlServerKeyTest = HlServerKeyTest {
|
const HL_SERVERKEY_MS_NOISE_REDUCTION_TEST: HlServerKeyTest = HlServerKeyTest {
|
||||||
test_filename: Cow::Borrowed("server_key_with_noise_squashing"),
|
test_filename: Cow::Borrowed("server_key_with_noise_squashing"),
|
||||||
client_key_filename: HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST.test_filename,
|
client_key_filename: HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST.test_filename,
|
||||||
|
rerand_cpk_filename: None,
|
||||||
compressed: false,
|
compressed: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const HL_SERVERKEY_MS_NOISE_REDUCTION_COMPRESSED_TEST: HlServerKeyTest = HlServerKeyTest {
|
const HL_SERVERKEY_MS_NOISE_REDUCTION_COMPRESSED_TEST: HlServerKeyTest = HlServerKeyTest {
|
||||||
test_filename: Cow::Borrowed("server_key_with_noise_squashing_compressed"),
|
test_filename: Cow::Borrowed("server_key_with_noise_squashing_compressed"),
|
||||||
client_key_filename: HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST.test_filename,
|
client_key_filename: HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST.test_filename,
|
||||||
|
rerand_cpk_filename: None,
|
||||||
compressed: true,
|
compressed: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ use std::fs::create_dir_all;
|
|||||||
|
|
||||||
use crate::generate::{
|
use crate::generate::{
|
||||||
INSECURE_SMALL_TEST_NOISE_SQUASHING_PARAMS_MS_NOISE_REDUCTION,
|
INSECURE_SMALL_TEST_NOISE_SQUASHING_PARAMS_MS_NOISE_REDUCTION,
|
||||||
INSECURE_SMALL_TEST_PARAMS_MS_NOISE_REDUCTION, TEST_PRAMS_NOISE_SQUASHING_COMPRESSION,
|
INSECURE_SMALL_TEST_PARAMS_MS_NOISE_REDUCTION, TEST_PARAMS_NOISE_SQUASHING_COMPRESSION,
|
||||||
};
|
};
|
||||||
|
|
||||||
use tfhe_1_3::boolean::engine::BooleanEngine;
|
use tfhe_1_3::boolean::engine::BooleanEngine;
|
||||||
@@ -310,6 +310,7 @@ const HL_CLIENTKEY_MS_MEAN_COMPENSATION: HlClientKeyTest = HlClientKeyTest {
|
|||||||
const HL_SERVERKEY_MS_MEAN_COMPENSATION: HlServerKeyTest = HlServerKeyTest {
|
const HL_SERVERKEY_MS_MEAN_COMPENSATION: HlServerKeyTest = HlServerKeyTest {
|
||||||
test_filename: Cow::Borrowed("server_key_ms_mean_compensation"),
|
test_filename: Cow::Borrowed("server_key_ms_mean_compensation"),
|
||||||
client_key_filename: Cow::Borrowed("client_key_ms_mean_compensation.cbor"),
|
client_key_filename: Cow::Borrowed("client_key_ms_mean_compensation.cbor"),
|
||||||
|
rerand_cpk_filename: None,
|
||||||
compressed: false,
|
compressed: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -449,7 +450,7 @@ impl TfhersVersion for V1_3 {
|
|||||||
.enable_noise_squashing(
|
.enable_noise_squashing(
|
||||||
INSECURE_SMALL_TEST_NOISE_SQUASHING_PARAMS_MS_NOISE_REDUCTION.into(),
|
INSECURE_SMALL_TEST_NOISE_SQUASHING_PARAMS_MS_NOISE_REDUCTION.into(),
|
||||||
)
|
)
|
||||||
.enable_noise_squashing_compression(TEST_PRAMS_NOISE_SQUASHING_COMPRESSION.into())
|
.enable_noise_squashing_compression(TEST_PARAMS_NOISE_SQUASHING_COMPRESSION.into())
|
||||||
.build();
|
.build();
|
||||||
let hl_client_key = ClientKey::generate(config);
|
let hl_client_key = ClientKey::generate(config);
|
||||||
let hl_server_key = ServerKey::new(&hl_client_key);
|
let hl_server_key = ServerKey::new(&hl_client_key);
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
use crate::generate::{
|
use crate::generate::{
|
||||||
store_versioned_test_tfhe_1_4, TfhersVersion,
|
store_versioned_auxiliary_tfhe_1_4, store_versioned_test_tfhe_1_4, TfhersVersion,
|
||||||
INSECURE_SMALL_TEST_NOISE_SQUASHING_PARAMS_MULTI_BIT, INSECURE_SMALL_TEST_PARAMS_MULTI_BIT,
|
INSECURE_DEDICATED_CPK_TEST_PARAMS, INSECURE_SMALL_TEST_NOISE_SQUASHING_PARAMS_MULTI_BIT,
|
||||||
|
INSECURE_SMALL_TEST_PARAMS_MS_MEAN_COMPENSATION, INSECURE_SMALL_TEST_PARAMS_MULTI_BIT,
|
||||||
|
KS_TO_BIG_TEST_PARAMS, KS_TO_SMALL_TEST_PARAMS,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
HlClientKeyTest, HlServerKeyTest, HlSquashedNoiseUnsignedCiphertextTest,
|
HlClientKeyTest, HlServerKeyTest, HlSquashedNoiseUnsignedCiphertextTest,
|
||||||
TestClassicParameterSet, TestDistribution, TestMetadata, TestModulusSwitchNoiseReductionParams,
|
TestClassicParameterSet, TestCompactPublicKeyEncryptionParameters, TestDistribution,
|
||||||
|
TestKeySwitchingParams, TestMetadata, TestModulusSwitchNoiseReductionParams,
|
||||||
TestModulusSwitchType, TestMultiBitParameterSet, TestNoiseSquashingParamsMultiBit,
|
TestModulusSwitchType, TestMultiBitParameterSet, TestNoiseSquashingParamsMultiBit,
|
||||||
TestParameterSet, HL_MODULE_NAME,
|
TestParameterSet, HL_MODULE_NAME,
|
||||||
};
|
};
|
||||||
@@ -19,14 +22,19 @@ use tfhe_1_4::prelude::*;
|
|||||||
use tfhe_1_4::shortint::engine::ShortintEngine;
|
use tfhe_1_4::shortint::engine::ShortintEngine;
|
||||||
use tfhe_1_4::shortint::parameters::noise_squashing::NoiseSquashingMultiBitParameters;
|
use tfhe_1_4::shortint::parameters::noise_squashing::NoiseSquashingMultiBitParameters;
|
||||||
use tfhe_1_4::shortint::parameters::{
|
use tfhe_1_4::shortint::parameters::{
|
||||||
CarryModulus, CiphertextModulus, CoreCiphertextModulus, DecompositionBaseLog,
|
CarryModulus, CiphertextModulus, CompactCiphertextListExpansionKind,
|
||||||
|
CompactPublicKeyEncryptionParameters, CoreCiphertextModulus, DecompositionBaseLog,
|
||||||
DecompositionLevelCount, DynamicDistribution, EncryptionKeyChoice, GlweDimension,
|
DecompositionLevelCount, DynamicDistribution, EncryptionKeyChoice, GlweDimension,
|
||||||
LweBskGroupingFactor, LweCiphertextCount, LweDimension, MaxNoiseLevel, MessageModulus,
|
LweBskGroupingFactor, LweCiphertextCount, LweDimension, MaxNoiseLevel, MessageModulus,
|
||||||
ModulusSwitchNoiseReductionParams, ModulusSwitchType, NoiseEstimationMeasureBound,
|
ModulusSwitchNoiseReductionParams, ModulusSwitchType, NoiseEstimationMeasureBound,
|
||||||
NoiseSquashingParameters, PolynomialSize, RSigmaFactor, StandardDev, Variance,
|
NoiseSquashingParameters, PolynomialSize, RSigmaFactor, ShortintKeySwitchingParameters,
|
||||||
|
StandardDev, SupportedCompactPkeZkScheme, Variance,
|
||||||
};
|
};
|
||||||
use tfhe_1_4::shortint::{AtomicPatternParameters, ClassicPBSParameters, MultiBitPBSParameters};
|
use tfhe_1_4::shortint::{AtomicPatternParameters, ClassicPBSParameters, MultiBitPBSParameters};
|
||||||
use tfhe_1_4::{set_server_key, ClientKey, FheUint32, Seed, ServerKey};
|
use tfhe_1_4::{
|
||||||
|
set_server_key, ClientKey, CompressedCompactPublicKey, ConfigBuilder, FheUint32, Seed,
|
||||||
|
ServerKey,
|
||||||
|
};
|
||||||
|
|
||||||
macro_rules! store_versioned_test {
|
macro_rules! store_versioned_test {
|
||||||
($msg:expr, $dir:expr, $test_filename:expr $(,)? ) => {
|
($msg:expr, $dir:expr, $test_filename:expr $(,)? ) => {
|
||||||
@@ -34,6 +42,12 @@ macro_rules! store_versioned_test {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! store_versioned_auxiliary {
|
||||||
|
($msg:expr, $dir:expr, $test_filename:expr $(,)? ) => {
|
||||||
|
store_versioned_auxiliary_tfhe_1_4($msg, $dir, $test_filename)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
impl From<TestDistribution> for DynamicDistribution<u64> {
|
impl From<TestDistribution> for DynamicDistribution<u64> {
|
||||||
fn from(value: TestDistribution) -> Self {
|
fn from(value: TestDistribution) -> Self {
|
||||||
match value {
|
match value {
|
||||||
@@ -220,6 +234,41 @@ impl From<TestNoiseSquashingParamsMultiBit> for NoiseSquashingParameters {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<TestKeySwitchingParams> for ShortintKeySwitchingParameters {
|
||||||
|
fn from(value: TestKeySwitchingParams) -> Self {
|
||||||
|
Self {
|
||||||
|
ks_level: DecompositionLevelCount(value.ks_level),
|
||||||
|
ks_base_log: DecompositionBaseLog(value.ks_base_log),
|
||||||
|
destination_key: match &*value.destination_key {
|
||||||
|
"big" => EncryptionKeyChoice::Big,
|
||||||
|
"small" => EncryptionKeyChoice::Small,
|
||||||
|
_ => panic!("Invalid encryption key choice"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<TestCompactPublicKeyEncryptionParameters> for CompactPublicKeyEncryptionParameters {
|
||||||
|
fn from(value: TestCompactPublicKeyEncryptionParameters) -> Self {
|
||||||
|
Self {
|
||||||
|
encryption_lwe_dimension: LweDimension(value.encryption_lwe_dimension),
|
||||||
|
encryption_noise_distribution: value.encryption_noise_distribution.into(),
|
||||||
|
message_modulus: MessageModulus(value.message_modulus as u64),
|
||||||
|
carry_modulus: CarryModulus(value.carry_modulus as u64),
|
||||||
|
ciphertext_modulus: CoreCiphertextModulus::try_new(value.ciphertext_modulus).unwrap(),
|
||||||
|
expansion_kind: match &*value.expansion_kind {
|
||||||
|
"requires_casting" => CompactCiphertextListExpansionKind::RequiresCasting,
|
||||||
|
_ => panic!("Invalid expansion kind"),
|
||||||
|
},
|
||||||
|
zk_scheme: match &*value.zk_scheme {
|
||||||
|
"zkv1" => SupportedCompactPkeZkScheme::V1,
|
||||||
|
"zkv2" => SupportedCompactPkeZkScheme::V2,
|
||||||
|
_ => panic!("Invalid zk scheme"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const TEST_FILENAME: Cow<'static, str> = Cow::Borrowed("client_key_with_noise_squashing");
|
const TEST_FILENAME: Cow<'static, str> = Cow::Borrowed("client_key_with_noise_squashing");
|
||||||
|
|
||||||
const HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST: HlClientKeyTest = HlClientKeyTest {
|
const HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST: HlClientKeyTest = HlClientKeyTest {
|
||||||
@@ -230,6 +279,7 @@ const HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST: HlClientKeyTest = HlClientKeyTest
|
|||||||
const HL_SERVERKEY_TEST: HlServerKeyTest = HlServerKeyTest {
|
const HL_SERVERKEY_TEST: HlServerKeyTest = HlServerKeyTest {
|
||||||
test_filename: Cow::Borrowed("server_key_with_noise_squashing"),
|
test_filename: Cow::Borrowed("server_key_with_noise_squashing"),
|
||||||
client_key_filename: TEST_FILENAME,
|
client_key_filename: TEST_FILENAME,
|
||||||
|
rerand_cpk_filename: None,
|
||||||
compressed: false,
|
compressed: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -240,6 +290,13 @@ const HL_SQUASHED_NOISE_UNSIGNED_CIPHERTEXT_TEST: HlSquashedNoiseUnsignedCiphert
|
|||||||
clear_value: 42,
|
clear_value: 42,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const HL_SERVERKEY_RERAND_TEST: HlServerKeyTest = HlServerKeyTest {
|
||||||
|
test_filename: Cow::Borrowed("server_key_for_rerand"),
|
||||||
|
client_key_filename: Cow::Borrowed("client_key_for_rerand"),
|
||||||
|
rerand_cpk_filename: Some(Cow::Borrowed("cpk_for_rerand")),
|
||||||
|
compressed: false,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct V1_4;
|
pub struct V1_4;
|
||||||
|
|
||||||
impl TfhersVersion for V1_4 {
|
impl TfhersVersion for V1_4 {
|
||||||
@@ -264,34 +321,74 @@ impl TfhersVersion for V1_4 {
|
|||||||
let dir = Self::data_dir().join(HL_MODULE_NAME);
|
let dir = Self::data_dir().join(HL_MODULE_NAME);
|
||||||
create_dir_all(&dir).unwrap();
|
create_dir_all(&dir).unwrap();
|
||||||
|
|
||||||
let config = tfhe_1_4::ConfigBuilder::with_custom_parameters(
|
// Test noise squahsing multibit
|
||||||
HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST.parameters,
|
{
|
||||||
)
|
let config = ConfigBuilder::with_custom_parameters(
|
||||||
.enable_noise_squashing(INSECURE_SMALL_TEST_NOISE_SQUASHING_PARAMS_MULTI_BIT.into())
|
HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST.parameters,
|
||||||
.build();
|
)
|
||||||
let hl_client_key = ClientKey::generate(config);
|
.enable_noise_squashing(INSECURE_SMALL_TEST_NOISE_SQUASHING_PARAMS_MULTI_BIT.into())
|
||||||
let hl_server_key = ServerKey::new(&hl_client_key);
|
.build();
|
||||||
set_server_key(hl_server_key.clone());
|
let hl_client_key = ClientKey::generate(config);
|
||||||
|
let hl_server_key = ServerKey::new(&hl_client_key);
|
||||||
|
set_server_key(hl_server_key.clone());
|
||||||
|
|
||||||
let input = FheUint32::encrypt(
|
let input = FheUint32::encrypt(
|
||||||
HL_SQUASHED_NOISE_UNSIGNED_CIPHERTEXT_TEST.clear_value as u32,
|
HL_SQUASHED_NOISE_UNSIGNED_CIPHERTEXT_TEST.clear_value as u32,
|
||||||
&hl_client_key,
|
&hl_client_key,
|
||||||
);
|
);
|
||||||
|
|
||||||
let ns = input.squash_noise().unwrap();
|
let ns = input.squash_noise().unwrap();
|
||||||
|
|
||||||
store_versioned_test!(
|
store_versioned_test!(
|
||||||
&hl_client_key,
|
&hl_client_key,
|
||||||
&dir,
|
&dir,
|
||||||
&HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST.test_filename
|
&HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST.test_filename
|
||||||
);
|
);
|
||||||
store_versioned_test!(&hl_server_key, &dir, &HL_SERVERKEY_TEST.test_filename,);
|
store_versioned_test!(&hl_server_key, &dir, &HL_SERVERKEY_TEST.test_filename,);
|
||||||
|
|
||||||
store_versioned_test!(
|
store_versioned_test!(
|
||||||
&ns,
|
&ns,
|
||||||
&dir,
|
&dir,
|
||||||
&HL_SQUASHED_NOISE_UNSIGNED_CIPHERTEXT_TEST.test_filename,
|
&HL_SQUASHED_NOISE_UNSIGNED_CIPHERTEXT_TEST.test_filename,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test re-randomization
|
||||||
|
{
|
||||||
|
let params = INSECURE_SMALL_TEST_PARAMS_MS_MEAN_COMPENSATION;
|
||||||
|
let cpk_params = (
|
||||||
|
INSECURE_DEDICATED_CPK_TEST_PARAMS.into(),
|
||||||
|
KS_TO_SMALL_TEST_PARAMS.into(),
|
||||||
|
);
|
||||||
|
let re_rand_ks_params = KS_TO_BIG_TEST_PARAMS;
|
||||||
|
|
||||||
|
let config = ConfigBuilder::with_custom_parameters(params)
|
||||||
|
.use_dedicated_compact_public_key_parameters(cpk_params)
|
||||||
|
.enable_ciphertext_re_randomization(re_rand_ks_params.into())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let hl_client_key = ClientKey::generate(config);
|
||||||
|
let hl_server_key = ServerKey::new(&hl_client_key);
|
||||||
|
let hl_public_key = CompressedCompactPublicKey::new(&hl_client_key);
|
||||||
|
|
||||||
|
store_versioned_auxiliary!(
|
||||||
|
&hl_client_key,
|
||||||
|
&dir,
|
||||||
|
&HL_SERVERKEY_RERAND_TEST.client_key_filename
|
||||||
|
);
|
||||||
|
|
||||||
|
store_versioned_auxiliary!(
|
||||||
|
&hl_public_key,
|
||||||
|
&dir,
|
||||||
|
&HL_SERVERKEY_RERAND_TEST.rerand_cpk_filename.unwrap()
|
||||||
|
);
|
||||||
|
|
||||||
|
store_versioned_test!(
|
||||||
|
&hl_server_key,
|
||||||
|
&dir,
|
||||||
|
&HL_SERVERKEY_RERAND_TEST.test_filename,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
vec![
|
vec![
|
||||||
TestMetadata::HlClientKey(HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST),
|
TestMetadata::HlClientKey(HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST),
|
||||||
@@ -299,6 +396,7 @@ impl TfhersVersion for V1_4 {
|
|||||||
TestMetadata::HlSquashedNoiseUnsignedCiphertext(
|
TestMetadata::HlSquashedNoiseUnsignedCiphertext(
|
||||||
HL_SQUASHED_NOISE_UNSIGNED_CIPHERTEXT_TEST,
|
HL_SQUASHED_NOISE_UNSIGNED_CIPHERTEXT_TEST,
|
||||||
),
|
),
|
||||||
|
TestMetadata::HlServerKey(HL_SERVERKEY_RERAND_TEST),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,9 +12,10 @@ use tfhe_1_4_versionable::Versionize as VersionizeTfhe_1_4;
|
|||||||
use tfhe_versionable::{Versionize as VersionizeTfhe_0_10, Versionize as VersionizeTfhe_0_8};
|
use tfhe_versionable::{Versionize as VersionizeTfhe_0_10, Versionize as VersionizeTfhe_0_8};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
data_dir, dir_for_version, TestClassicParameterSet, TestCompressionParameterSet,
|
data_dir, dir_for_version, TestClassicParameterSet, TestCompactPublicKeyEncryptionParameters,
|
||||||
TestDistribution, TestMetadata, TestModulusSwitchNoiseReductionParams, TestModulusSwitchType,
|
TestCompressionParameterSet, TestDistribution, TestKeySwitchingParams, TestMetadata,
|
||||||
TestMultiBitParameterSet, TestNoiseSquashingCompressionParameters, TestNoiseSquashingParams,
|
TestModulusSwitchNoiseReductionParams, TestModulusSwitchType, TestMultiBitParameterSet,
|
||||||
|
TestNoiseSquashingCompressionParameters, TestNoiseSquashingParams,
|
||||||
TestNoiseSquashingParamsMultiBit, TestParameterSet,
|
TestNoiseSquashingParamsMultiBit, TestParameterSet,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -181,7 +182,7 @@ pub const INSECURE_SMALL_TEST_NOISE_SQUASHING_PARAMS_MS_NOISE_REDUCTION: TestNoi
|
|||||||
ciphertext_modulus: 0,
|
ciphertext_modulus: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const TEST_PRAMS_NOISE_SQUASHING_COMPRESSION: TestNoiseSquashingCompressionParameters =
|
pub const TEST_PARAMS_NOISE_SQUASHING_COMPRESSION: TestNoiseSquashingCompressionParameters =
|
||||||
TestNoiseSquashingCompressionParameters {
|
TestNoiseSquashingCompressionParameters {
|
||||||
packing_ks_level: 1,
|
packing_ks_level: 1,
|
||||||
packing_ks_base_log: 61,
|
packing_ks_base_log: 61,
|
||||||
@@ -242,6 +243,30 @@ pub const INVALID_TEST_PARAMS: TestClassicParameterSet = TestClassicParameterSet
|
|||||||
modulus_switch_noise_reduction_params: TestModulusSwitchType::Standard,
|
modulus_switch_noise_reduction_params: TestModulusSwitchType::Standard,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const KS_TO_SMALL_TEST_PARAMS: TestKeySwitchingParams = TestKeySwitchingParams {
|
||||||
|
ks_level: 5,
|
||||||
|
ks_base_log: 3,
|
||||||
|
destination_key: Cow::Borrowed("small"),
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Parameters used to create a rerand key
|
||||||
|
pub const KS_TO_BIG_TEST_PARAMS: TestKeySwitchingParams = TestKeySwitchingParams {
|
||||||
|
ks_level: 1,
|
||||||
|
ks_base_log: 27,
|
||||||
|
destination_key: Cow::Borrowed("big"),
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const INSECURE_DEDICATED_CPK_TEST_PARAMS: TestCompactPublicKeyEncryptionParameters =
|
||||||
|
TestCompactPublicKeyEncryptionParameters {
|
||||||
|
encryption_lwe_dimension: 32,
|
||||||
|
encryption_noise_distribution: TestDistribution::TUniform { bound_log2: 42 },
|
||||||
|
message_modulus: 4,
|
||||||
|
carry_modulus: 4,
|
||||||
|
ciphertext_modulus: 1 << 64,
|
||||||
|
expansion_kind: Cow::Borrowed("requires_casting"),
|
||||||
|
zk_scheme: Cow::Borrowed("zkv2"),
|
||||||
|
};
|
||||||
|
|
||||||
pub fn save_cbor<Data: Serialize, P: AsRef<Path>>(msg: &Data, path: P) {
|
pub fn save_cbor<Data: Serialize, P: AsRef<Path>>(msg: &Data, path: P) {
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
if path.exists() {
|
if path.exists() {
|
||||||
|
|||||||
@@ -159,6 +159,26 @@ pub enum TestDistribution {
|
|||||||
TUniform { bound_log2: u32 },
|
TUniform { bound_log2: u32 },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Re-definition of keyswitch parameters
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
pub struct TestKeySwitchingParams {
|
||||||
|
pub ks_level: usize,
|
||||||
|
pub ks_base_log: usize,
|
||||||
|
pub destination_key: Cow<'static, str>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Re-definition of cpk parameters
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
pub struct TestCompactPublicKeyEncryptionParameters {
|
||||||
|
pub encryption_lwe_dimension: usize,
|
||||||
|
pub encryption_noise_distribution: TestDistribution,
|
||||||
|
pub message_modulus: usize,
|
||||||
|
pub carry_modulus: usize,
|
||||||
|
pub ciphertext_modulus: u128,
|
||||||
|
pub expansion_kind: Cow<'static, str>,
|
||||||
|
pub zk_scheme: Cow<'static, str>,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn dir_for_version<P: AsRef<Path>>(data_dir: P, version: &str) -> PathBuf {
|
pub fn dir_for_version<P: AsRef<Path>>(data_dir: P, version: &str) -> PathBuf {
|
||||||
let mut path = data_dir.as_ref().to_path_buf();
|
let mut path = data_dir.as_ref().to_path_buf();
|
||||||
path.push(version.replace('.', "_"));
|
path.push(version.replace('.', "_"));
|
||||||
@@ -349,6 +369,7 @@ impl TestType for HlClientKeyTest {
|
|||||||
pub struct HlServerKeyTest {
|
pub struct HlServerKeyTest {
|
||||||
pub test_filename: Cow<'static, str>,
|
pub test_filename: Cow<'static, str>,
|
||||||
pub client_key_filename: Cow<'static, str>,
|
pub client_key_filename: Cow<'static, str>,
|
||||||
|
pub rerand_cpk_filename: Option<Cow<'static, str>>,
|
||||||
pub compressed: bool,
|
pub compressed: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user