chore(backward): add backward compat tests for rerand

This commit is contained in:
Nicolas Sarlin
2025-09-16 14:01:38 +02:00
committed by Nicolas Sarlin
parent 1b1e6a7068
commit 70e1828c58
19 changed files with 282 additions and 48 deletions

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:0ef52bb82a792d2530c7e46f80dccba6a9f5c6d7b94d5f87bdd4637ceeed8197
size 3338

View File

@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:11975834f2209a223da64547d50547f8f01798a5110388f5384c8631bdd82b19
size 82427
oid sha256:8e58e8549f1ab58bed9f77ec936b995cb0238f43b813dbc64651b96aab6d9452
size 82428

View File

@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:db5483f4fcf34659639acaaa25e755ded39794f762bba8ff18ef462073747435
size 7336
oid sha256:d422e48c8327dfe2cdada7da850b12aacc81d198b408e650e8dfcab3ec164487
size 7370

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:a29e1f621ff00d68c39860be58660c3359801cdd6cdd55c09fffd80c4b86ba2f
size 747

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:1d1f9127b621084669da234edfb15d6ec0a9811b4a11920f214f6a70710bcefd
size 856624

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:827caa582a73af08043012732444f4ea89eaa445095b4cfc0b8dfb2a193f504d
size 972494

View File

@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:4acc2ef58e90faae1d8e0f5ebc0db83740f08e6b9be7cdc1dbe0034c37ee2102
size 15614959
oid sha256:523fb601cb28251fce9fbf4a784b890336bda2cfd1b3db64c996cb89fc38f212
size 15614960

View File

@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:f2b6cd9d2170c7782b525a01dc8021c3b55d11d67f66215bbf16cc4e84b98b22
size 17632539
oid sha256:147dd3b499ce1385aeba3d54e60b12eff302ddbd98d3b218cb9972a0321251f2
size 17632589

View File

@@ -357,6 +357,7 @@
metadata: HlServerKey((
test_filename: "compressed_server_key",
client_key_filename: "client_key.cbor",
rerand_cpk_filename: None,
compressed: true,
)),
),
@@ -366,6 +367,7 @@
metadata: HlServerKey((
test_filename: "server_key_with_compression",
client_key_filename: "client_key.cbor",
rerand_cpk_filename: None,
compressed: false,
)),
),
@@ -448,6 +450,7 @@
metadata: HlServerKey((
test_filename: "server_key_ms_noise_reduction",
client_key_filename: "client_key_ms_noise_reduction.cbor",
rerand_cpk_filename: None,
compressed: false,
)),
),
@@ -504,6 +507,7 @@
metadata: HlServerKey((
test_filename: "server_key_with_noise_squashing",
client_key_filename: "client_key_with_noise_squashing",
rerand_cpk_filename: None,
compressed: false,
)),
),
@@ -513,6 +517,7 @@
metadata: HlServerKey((
test_filename: "server_key_with_noise_squashing_compressed",
client_key_filename: "client_key_with_noise_squashing",
rerand_cpk_filename: None,
compressed: true,
)),
),
@@ -613,6 +618,7 @@
metadata: HlServerKey((
test_filename: "server_key_ms_mean_compensation",
client_key_filename: "client_key_ms_mean_compensation.cbor",
rerand_cpk_filename: None,
compressed: false,
)),
),
@@ -673,6 +679,7 @@
metadata: HlServerKey((
test_filename: "server_key_with_noise_squashing",
client_key_filename: "client_key_with_noise_squashing",
rerand_cpk_filename: None,
compressed: false,
)),
),
@@ -685,4 +692,14 @@
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,
)),
),
]

View File

@@ -165,12 +165,14 @@ const HL_CLIENTKEY_TEST: HlClientKeyTest = HlClientKeyTest {
const HL_COMPRESSED_SERVERKEY_TEST: HlServerKeyTest = HlServerKeyTest {
test_filename: Cow::Borrowed("compressed_server_key"),
client_key_filename: Cow::Borrowed("client_key.cbor"),
rerand_cpk_filename: None,
compressed: true,
};
const HL_SERVERKEY_WITH_COMPRESSION_TEST: HlServerKeyTest = HlServerKeyTest {
test_filename: Cow::Borrowed("server_key_with_compression"),
client_key_filename: Cow::Borrowed("client_key.cbor"),
rerand_cpk_filename: None,
compressed: false,
};

View File

@@ -171,6 +171,7 @@ const HL_CLIENTKEY_MS_NOISE_REDUCTION_TEST: HlClientKeyTest = HlClientKeyTest {
const HL_SERVERKEY_MS_NOISE_REDUCTION_TEST: HlServerKeyTest = HlServerKeyTest {
test_filename: Cow::Borrowed("server_key_ms_noise_reduction"),
client_key_filename: Cow::Borrowed("client_key_ms_noise_reduction.cbor"),
rerand_cpk_filename: None,
compressed: false,
};

View File

@@ -207,12 +207,14 @@ const HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST: HlClientKeyTest = HlClientKeyTest
const HL_SERVERKEY_MS_NOISE_REDUCTION_TEST: HlServerKeyTest = HlServerKeyTest {
test_filename: Cow::Borrowed("server_key_with_noise_squashing"),
client_key_filename: HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST.test_filename,
rerand_cpk_filename: None,
compressed: false,
};
const HL_SERVERKEY_MS_NOISE_REDUCTION_COMPRESSED_TEST: HlServerKeyTest = HlServerKeyTest {
test_filename: Cow::Borrowed("server_key_with_noise_squashing_compressed"),
client_key_filename: HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST.test_filename,
rerand_cpk_filename: None,
compressed: true,
};

View File

@@ -14,7 +14,7 @@ use std::fs::create_dir_all;
use crate::generate::{
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;
@@ -310,6 +310,7 @@ const HL_CLIENTKEY_MS_MEAN_COMPENSATION: HlClientKeyTest = HlClientKeyTest {
const HL_SERVERKEY_MS_MEAN_COMPENSATION: HlServerKeyTest = HlServerKeyTest {
test_filename: Cow::Borrowed("server_key_ms_mean_compensation"),
client_key_filename: Cow::Borrowed("client_key_ms_mean_compensation.cbor"),
rerand_cpk_filename: None,
compressed: false,
};
@@ -449,7 +450,7 @@ impl TfhersVersion for V1_3 {
.enable_noise_squashing(
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();
let hl_client_key = ClientKey::generate(config);
let hl_server_key = ServerKey::new(&hl_client_key);

View File

@@ -1,10 +1,13 @@
use crate::generate::{
store_versioned_test_tfhe_1_4, TfhersVersion,
INSECURE_SMALL_TEST_NOISE_SQUASHING_PARAMS_MULTI_BIT, INSECURE_SMALL_TEST_PARAMS_MULTI_BIT,
store_versioned_auxiliary_tfhe_1_4, store_versioned_test_tfhe_1_4, TfhersVersion,
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::{
HlClientKeyTest, HlServerKeyTest, HlSquashedNoiseUnsignedCiphertextTest,
TestClassicParameterSet, TestDistribution, TestMetadata, TestModulusSwitchNoiseReductionParams,
TestClassicParameterSet, TestCompactPublicKeyEncryptionParameters, TestDistribution,
TestKeySwitchingParams, TestMetadata, TestModulusSwitchNoiseReductionParams,
TestModulusSwitchType, TestMultiBitParameterSet, TestNoiseSquashingParamsMultiBit,
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::parameters::noise_squashing::NoiseSquashingMultiBitParameters;
use tfhe_1_4::shortint::parameters::{
CarryModulus, CiphertextModulus, CoreCiphertextModulus, DecompositionBaseLog,
CarryModulus, CiphertextModulus, CompactCiphertextListExpansionKind,
CompactPublicKeyEncryptionParameters, CoreCiphertextModulus, DecompositionBaseLog,
DecompositionLevelCount, DynamicDistribution, EncryptionKeyChoice, GlweDimension,
LweBskGroupingFactor, LweCiphertextCount, LweDimension, MaxNoiseLevel, MessageModulus,
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::{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 {
($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> {
fn from(value: TestDistribution) -> Self {
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 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 {
test_filename: Cow::Borrowed("server_key_with_noise_squashing"),
client_key_filename: TEST_FILENAME,
rerand_cpk_filename: None,
compressed: false,
};
@@ -240,6 +290,13 @@ const HL_SQUASHED_NOISE_UNSIGNED_CIPHERTEXT_TEST: HlSquashedNoiseUnsignedCiphert
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;
impl TfhersVersion for V1_4 {
@@ -264,34 +321,74 @@ impl TfhersVersion for V1_4 {
let dir = Self::data_dir().join(HL_MODULE_NAME);
create_dir_all(&dir).unwrap();
let config = tfhe_1_4::ConfigBuilder::with_custom_parameters(
HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST.parameters,
)
.enable_noise_squashing(INSECURE_SMALL_TEST_NOISE_SQUASHING_PARAMS_MULTI_BIT.into())
.build();
let hl_client_key = ClientKey::generate(config);
let hl_server_key = ServerKey::new(&hl_client_key);
set_server_key(hl_server_key.clone());
// Test noise squahsing multibit
{
let config = ConfigBuilder::with_custom_parameters(
HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST.parameters,
)
.enable_noise_squashing(INSECURE_SMALL_TEST_NOISE_SQUASHING_PARAMS_MULTI_BIT.into())
.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 input = FheUint32::encrypt(
HL_SQUASHED_NOISE_UNSIGNED_CIPHERTEXT_TEST.clear_value as u32,
&hl_client_key,
);
let input = FheUint32::encrypt(
HL_SQUASHED_NOISE_UNSIGNED_CIPHERTEXT_TEST.clear_value as u32,
&hl_client_key,
);
let ns = input.squash_noise().unwrap();
let ns = input.squash_noise().unwrap();
store_versioned_test!(
&hl_client_key,
&dir,
&HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST.test_filename
);
store_versioned_test!(&hl_server_key, &dir, &HL_SERVERKEY_TEST.test_filename,);
store_versioned_test!(
&hl_client_key,
&dir,
&HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST.test_filename
);
store_versioned_test!(&hl_server_key, &dir, &HL_SERVERKEY_TEST.test_filename,);
store_versioned_test!(
&ns,
&dir,
&HL_SQUASHED_NOISE_UNSIGNED_CIPHERTEXT_TEST.test_filename,
);
store_versioned_test!(
&ns,
&dir,
&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![
TestMetadata::HlClientKey(HL_CLIENTKEY_WITH_NOISE_SQUASHING_TEST),
@@ -299,6 +396,7 @@ impl TfhersVersion for V1_4 {
TestMetadata::HlSquashedNoiseUnsignedCiphertext(
HL_SQUASHED_NOISE_UNSIGNED_CIPHERTEXT_TEST,
),
TestMetadata::HlServerKey(HL_SERVERKEY_RERAND_TEST),
]
}
}

View File

@@ -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 crate::{
data_dir, dir_for_version, TestClassicParameterSet, TestCompressionParameterSet,
TestDistribution, TestMetadata, TestModulusSwitchNoiseReductionParams, TestModulusSwitchType,
TestMultiBitParameterSet, TestNoiseSquashingCompressionParameters, TestNoiseSquashingParams,
data_dir, dir_for_version, TestClassicParameterSet, TestCompactPublicKeyEncryptionParameters,
TestCompressionParameterSet, TestDistribution, TestKeySwitchingParams, TestMetadata,
TestModulusSwitchNoiseReductionParams, TestModulusSwitchType, TestMultiBitParameterSet,
TestNoiseSquashingCompressionParameters, TestNoiseSquashingParams,
TestNoiseSquashingParamsMultiBit, TestParameterSet,
};
@@ -181,7 +182,7 @@ pub const INSECURE_SMALL_TEST_NOISE_SQUASHING_PARAMS_MS_NOISE_REDUCTION: TestNoi
ciphertext_modulus: 0,
};
pub const TEST_PRAMS_NOISE_SQUASHING_COMPRESSION: TestNoiseSquashingCompressionParameters =
pub const TEST_PARAMS_NOISE_SQUASHING_COMPRESSION: TestNoiseSquashingCompressionParameters =
TestNoiseSquashingCompressionParameters {
packing_ks_level: 1,
packing_ks_base_log: 61,
@@ -242,6 +243,30 @@ pub const INVALID_TEST_PARAMS: TestClassicParameterSet = TestClassicParameterSet
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) {
let path = path.as_ref();
if path.exists() {

View File

@@ -159,6 +159,26 @@ pub enum TestDistribution {
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 {
let mut path = data_dir.as_ref().to_path_buf();
path.push(version.replace('.', "_"));
@@ -349,6 +369,7 @@ impl TestType for HlClientKeyTest {
pub struct HlServerKeyTest {
pub test_filename: Cow<'static, str>,
pub client_key_filename: Cow<'static, str>,
pub rerand_cpk_filename: Option<Cow<'static, str>>,
pub compressed: bool,
}