Skip to content

Commit dcb6049

Browse files
committed
chore: backward data test for CompressedSquashedNoiseCiphertextList
1 parent 7203cc3 commit dcb6049

File tree

5 files changed

+99
-12
lines changed

5 files changed

+99
-12
lines changed

tests/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ tfhe-versionable = { path = "../utils/tfhe-versionable" }
1010
tfhe-backward-compat-data = { git = "https://github.com/zama-ai/tfhe-backward-compat-data.git", branch = "v0.8", default-features = false, features = [
1111
"load",
1212
] }
13-
ron = "0.8"
1413
cargo_toml = "0.22"
1514

1615

tests/backward_compatibility/high_level_api.rs

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
use super::shortint::load_params;
22
use crate::{load_and_unversionize, TestedModule};
33
use std::path::Path;
4+
#[cfg(feature = "zk-pok")]
45
use tfhe::integer::parameters::DynamicDistribution;
56
use tfhe::prelude::{CiphertextList, FheDecrypt, FheEncrypt, ParameterSetConformant};
7+
#[cfg(feature = "zk-pok")]
68
use tfhe::shortint::parameters::{
79
CompactCiphertextListExpansionKind, CompactPublicKeyEncryptionParameters,
810
};
11+
#[cfg(feature = "zk-pok")]
912
use tfhe::shortint::prelude::LweDimension;
1013
use tfhe::shortint::{
1114
AtomicPatternParameters, CarryModulus, CiphertextModulus, MessageModulus, PBSParameters,
1215
};
1316
#[cfg(feature = "zk-pok")]
14-
use tfhe::zk::CompactPkeCrs;
15-
#[cfg(feature = "zk-pok")]
16-
use tfhe::zk::CompactPkeCrsConformanceParams;
17+
use tfhe::zk::{CompactPkeCrs, CompactPkeCrsConformanceParams};
1718
use tfhe::{
1819
set_server_key, ClientKey, CompactCiphertextList, CompressedCiphertextList,
1920
CompressedCompactPublicKey, CompressedFheBool, CompressedFheInt8, CompressedFheUint8,
20-
CompressedPublicKey, CompressedServerKey, FheBool, FheInt8, FheUint8, SquashedNoiseFheBool,
21-
SquashedNoiseFheInt, SquashedNoiseFheUint,
21+
CompressedPublicKey, CompressedServerKey, CompressedSquashedNoiseCiphertextList, FheBool,
22+
FheInt8, FheUint8, SquashedNoiseFheBool, SquashedNoiseFheInt, SquashedNoiseFheUint,
2223
};
2324
#[cfg(feature = "zk-pok")]
2425
use tfhe::{CompactPublicKey, ProvenCompactCiphertextList};
@@ -27,10 +28,10 @@ use tfhe_backward_compat_data::load::{
2728
};
2829
use tfhe_backward_compat_data::{
2930
DataKind, HlBoolCiphertextTest, HlCiphertextTest, HlClientKeyTest,
30-
HlHeterogeneousCiphertextListTest, HlPublicKeyTest, HlServerKeyTest, HlSignedCiphertextTest,
31-
HlSquashedNoiseBoolCiphertextTest, HlSquashedNoiseSignedCiphertextTest,
32-
HlSquashedNoiseUnsignedCiphertextTest, TestMetadata, TestParameterSet, TestType, Testcase,
33-
ZkPkePublicParamsTest,
31+
HlCompressedSquashedNoiseCiphertextListTest, HlHeterogeneousCiphertextListTest,
32+
HlPublicKeyTest, HlServerKeyTest, HlSignedCiphertextTest, HlSquashedNoiseBoolCiphertextTest,
33+
HlSquashedNoiseSignedCiphertextTest, HlSquashedNoiseUnsignedCiphertextTest, TestMetadata,
34+
TestParameterSet, TestType, Testcase, ZkPkePublicParamsTest,
3435
};
3536
use tfhe_versionable::Unversionize;
3637

@@ -495,6 +496,67 @@ pub fn test_hl_squashed_noise_bool_ciphertext(
495496
}
496497
}
497498

499+
/// Test HL compressed squashed noise ciphertext list:
500+
/// loads the ciphertext list and compare the decrypted value to the one in the
501+
/// metadata.
502+
pub fn test_hl_compressed_squashed_noise_ciphertext_list(
503+
dir: &Path,
504+
test: &HlCompressedSquashedNoiseCiphertextListTest,
505+
format: DataFormat,
506+
) -> Result<TestSuccess, TestFailure> {
507+
let key_file = dir.join(&*test.key_filename);
508+
let key = ClientKey::unversionize(
509+
load_versioned_auxiliary(key_file).map_err(|e| test.failure(e, format))?,
510+
)
511+
.map_err(|e| test.failure(format!("Failed to load key file: {e}"), format))?;
512+
513+
let list: CompressedSquashedNoiseCiphertextList = load_and_unversionize(dir, test, format)
514+
.map_err(|e| test.failure(format!("Failed to load list file: {e}"), format))?;
515+
516+
if list.len() != test.clear_values.len() || list.len() != test.data_kinds.len() {
517+
return Err(test.failure(
518+
format!(
519+
"Invalid len for the compressed list, expected {} elements, got {}",
520+
test.clear_values.len(),
521+
list.len()
522+
),
523+
format,
524+
));
525+
}
526+
527+
for i in 0..list.len() {
528+
let decrypted = match test.data_kinds[i] {
529+
DataKind::Unsigned => {
530+
let ct: SquashedNoiseFheUint = list.get(i).unwrap().unwrap();
531+
let clear: u64 = ct.decrypt(&key);
532+
clear
533+
}
534+
DataKind::Signed => {
535+
let ct: SquashedNoiseFheInt = list.get(i).unwrap().unwrap();
536+
let clear: i64 = ct.decrypt(&key);
537+
clear as u64
538+
}
539+
DataKind::Bool => {
540+
let ct: SquashedNoiseFheBool = list.get(i).unwrap().unwrap();
541+
let clear: bool = ct.decrypt(&key);
542+
clear as u64
543+
}
544+
};
545+
546+
let expected = test.clear_values[i];
547+
if decrypted != expected {
548+
return Err(test.failure(
549+
format!(
550+
"Invalid decryption at index {i}:\n Expected :{expected:?} Got: {decrypted:?}",
551+
),
552+
format,
553+
));
554+
}
555+
}
556+
557+
Ok(test.success(format))
558+
}
559+
498560
pub struct Hl;
499561

500562
impl TestedModule for Hl {
@@ -540,6 +602,10 @@ impl TestedModule for Hl {
540602
TestMetadata::HlSquashedNoiseBoolCiphertext(test) => {
541603
test_hl_squashed_noise_bool_ciphertext(test_dir.as_ref(), test, format).into()
542604
}
605+
TestMetadata::HlCompressedSquashedNoiseCiphertextList(test) => {
606+
test_hl_compressed_squashed_noise_ciphertext_list(test_dir.as_ref(), test, format)
607+
.into()
608+
}
543609
_ => {
544610
println!("WARNING: missing test: {:?}", testcase.metadata);
545611
TestResult::Skipped(testcase.skip())

tfhe/src/high_level_api/compressed_noise_squashed_ciphertext_list.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ impl InnerCompressedSquashedNoiseCiphertextList {
118118
}
119119
}
120120

121+
/// Compressed ciphertext list for squashed noise ciphertext
122+
///
123+
/// This list supports
124+
///
125+
/// * [SquashedNoiseFheUint]
126+
/// * [SquashedNoiseFheInt]
127+
/// * [SquashedNoiseFheBool]
128+
///
129+
/// Use the [CompressedSquashedNoiseCiphertextListBuilder] struct to
130+
/// build a list.
131+
///
132+
/// This requires the server key to have noise-squashing compression keys,
133+
/// which is enabled by calling [crate::ConfigBuilder::enable_noise_squashing_compression]
121134
#[derive(Serialize, Deserialize, Versionize)]
122135
#[versionize(CompressedSquashedNoiseCiphertextListVersions)]
123136
pub struct CompressedSquashedNoiseCiphertextList {
@@ -290,6 +303,10 @@ impl HlSquashedNoiseCompressible for SquashedNoiseFheInt {
290303
}
291304
}
292305

306+
/// Builder to create [CompressedSquashedNoiseCiphertextList]
307+
///
308+
/// Use [push](Self::push) to add squashed noise ciphertext to the list,
309+
/// then call [build](Self::build) to build the list.
293310
pub struct CompressedSquashedNoiseCiphertextListBuilder {
294311
inner: Vec<(private::SquashedNoiseToBeCompressed, DataKind)>,
295312
}

tfhe/src/high_level_api/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ impl ConfigBuilder {
6767
self
6868
}
6969

70+
/// Enable the generation of keys needed for [crate::CompressedSquashedNoiseCiphertextList]
71+
///
72+
/// # Note
73+
///
74+
/// This requires nose squashing to be enabled first via [Self::enable_noise_squashing]
7075
pub fn enable_noise_squashing_compression(
7176
mut self,
7277
compression_parameters: NoiseSquashingCompressionParameters,

tfhe/src/high_level_api/keys/inner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ impl ParameterSetConformant for IntegerServerKey {
728728
parameter_set.noise_squashing_compression_param.as_ref(),
729729
noise_squashing_compression_key.as_ref(),
730730
) {
731-
(None, None, None) | (Some(_), None, None) => true,
731+
(None | Some(_), None, None) => true,
732732
(
733733
Some(noise_squashing_parameters),
734734
Some(noise_squashing_compression_param),
@@ -818,7 +818,7 @@ impl ParameterSetConformant for IntegerCompressedServerKey {
818818
parameter_set.noise_squashing_compression_param.as_ref(),
819819
noise_squashing_compression_key.as_ref(),
820820
) {
821-
(None, None, None) | (Some(_), None, None) => true,
821+
(None | Some(_), None, None) => true,
822822
(
823823
Some(noise_squashing_parameters),
824824
Some(noise_squashing_compression_param),

0 commit comments

Comments
 (0)