Skip to content

Commit

Permalink
chore(hl): add data tests for heterogeneous lists
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarlin-zama committed Jul 23, 2024
1 parent 58344fd commit d77a66c
Showing 1 changed file with 136 additions and 8 deletions.
144 changes: 136 additions & 8 deletions tfhe/tests/backward_compatibility/high_level_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ use tfhe::backward_compatibility::booleans::{CompactFheBool, CompactFheBoolList}
use tfhe::backward_compatibility::integers::{
CompactFheInt8, CompactFheInt8List, CompactFheUint8, CompactFheUint8List,
};

use tfhe::prelude::{FheDecrypt, FheEncrypt};
use tfhe::shortint::PBSParameters;
use tfhe::{
set_server_key, ClientKey, CompactCiphertextList, CompressedCompactPublicKey,
CompressedFheBool, CompressedFheInt8, CompressedFheUint8, CompressedPublicKey,
CompressedServerKey, FheUint8,
set_server_key, ClientKey, CompactCiphertextList, CompressedCiphertextList,
CompressedCompactPublicKey, CompressedFheBool, CompressedFheInt8, CompressedFheUint8,
CompressedPublicKey, CompressedServerKey, FheBool, FheInt8, FheUint8,
};
use tfhe_backward_compat_data::load::{
load_versioned_auxiliary, DataFormat, TestFailure, TestResult, TestSuccess,
};
use tfhe_backward_compat_data::{
HlBoolCiphertextListTest, HlBoolCiphertextTest, HlCiphertextListTest, HlCiphertextTest,
HlClientKeyTest, HlPublicKeyTest, HlServerKeyTest, HlSignedCiphertextListTest,
HlSignedCiphertextTest, TestMetadata, TestParameterSet, TestType, Testcase,
DataKind, HlBoolCiphertextListTest, HlBoolCiphertextTest, HlCiphertextListTest,
HlCiphertextTest, HlClientKeyTest, HlHeterogeneousCiphertextListTest, HlPublicKeyTest,
HlServerKeyTest, HlSignedCiphertextListTest, HlSignedCiphertextTest, TestMetadata,
TestParameterSet, TestType, Testcase,
};
use tfhe_versionable::Unversionize;

Expand Down Expand Up @@ -257,6 +259,129 @@ pub fn test_hl_bool_ciphertext_list(
}
}

/// Test HL ciphertext list: loads the ciphertext list and compare the decrypted values to the ones
/// in the metadata.
pub fn test_hl_heterogeneous_ciphertext_list(
dir: &Path,
test: &HlHeterogeneousCiphertextListTest,
format: DataFormat,
) -> Result<TestSuccess, TestFailure> {
let key_file = dir.join(&*test.key_filename);
let key = ClientKey::unversionize(
load_versioned_auxiliary(key_file).map_err(|e| test.failure(e, format))?,
)
.map_err(|e| test.failure(e, format))?;

let server_key = key.generate_server_key();
set_server_key(server_key);

if test.compressed {
test_hl_heterogeneous_ciphertext_list_compressed(
load_and_unversionize(dir, test, format)?,
&key,
test,
)
} else {
test_hl_heterogeneous_ciphertext_list_compact(
load_and_unversionize(dir, test, format)?,
&key,
test,
)
}
.map(|_| test.success(format))
.map_err(|msg| test.failure(msg, format))
}

pub fn test_hl_heterogeneous_ciphertext_list_compact(
list: CompactCiphertextList,
key: &ClientKey,
test: &HlHeterogeneousCiphertextListTest,
) -> Result<(), String> {
let ct_list = list.expand().unwrap();

for idx in 0..(ct_list.len()) {
match test.data_kinds[idx] {
DataKind::Bool => {
let ct: FheBool = ct_list.get(idx).unwrap().unwrap();
let clear = ct.decrypt(key);
if clear != (test.clear_values[idx] != 0) {
return Err(format!(
"Invalid decrypted cleartext:\n Expected :\n{:?}\nGot:\n{:?}",
clear, test.clear_values[idx]
));
}
}
DataKind::Signed => {
let ct: FheInt8 = ct_list.get(idx).unwrap().unwrap();
let clear: i8 = ct.decrypt(key);
if clear != test.clear_values[idx] as i8 {
return Err(format!(
"Invalid decrypted cleartext:\n Expected :\n{:?}\nGot:\n{:?}",
clear,
(test.clear_values[idx] as i8)
));
}
}
DataKind::Unsigned => {
let ct: FheUint8 = ct_list.get(idx).unwrap().unwrap();
let clear: u8 = ct.decrypt(key);
if clear != test.clear_values[idx] as u8 {
return Err(format!(
"Invalid decrypted cleartext:\n Expected :\n{:?}\nGot:\n{:?}",
clear, test.clear_values[idx]
));
}
}
};
}
Ok(())
}

pub fn test_hl_heterogeneous_ciphertext_list_compressed(
list: CompressedCiphertextList,
key: &ClientKey,
test: &HlHeterogeneousCiphertextListTest,
) -> Result<(), String> {
let ct_list = list;

for idx in 0..(ct_list.len()) {
match test.data_kinds[idx] {
DataKind::Bool => {
let ct: FheBool = ct_list.get(idx).unwrap().unwrap();
let clear = ct.decrypt(key);
if clear != (test.clear_values[idx] != 0) {
return Err(format!(
"Invalid decrypted cleartext:\n Expected :\n{:?}\nGot:\n{:?}",
clear, test.clear_values[idx]
));
}
}
DataKind::Signed => {
let ct: FheInt8 = ct_list.get(idx).unwrap().unwrap();
let clear: i8 = ct.decrypt(key);
if clear != test.clear_values[idx] as i8 {
return Err(format!(
"Invalid decrypted cleartext:\n Expected :\n{:?}\nGot:\n{:?}",
clear,
(test.clear_values[idx] as i8)
));
}
}
DataKind::Unsigned => {
let ct: FheUint8 = ct_list.get(idx).unwrap().unwrap();
let clear: u8 = ct.decrypt(key);
if clear != test.clear_values[idx] as u8 {
return Err(format!(
"Invalid decrypted cleartext:\n Expected :\n{:?}\nGot:\n{:?}",
clear, test.clear_values[idx]
));
}
}
};
}
Ok(())
}

/// Test HL client key: loads the key and checks the parameters using the values stored in
/// the test metadata.
pub fn test_hl_clientkey(
Expand Down Expand Up @@ -333,8 +458,8 @@ pub fn test_hl_pubkey(
}
}

/// Test HL server key: encrypt to values with a client key, add them using the server key and check
/// that the decrypted sum is valid.
/// Test HL server key: encrypt two values with a client key, add them using the server key and
/// check that the decrypted sum is valid.
pub fn test_hl_serverkey(
dir: &Path,
test: &HlServerKeyTest,
Expand Down Expand Up @@ -406,6 +531,9 @@ impl TestedModule for Hl {
TestMetadata::HlSignedCiphertextList(test) => {
test_hl_signed_ciphertext_list(test_dir.as_ref(), test, format).into()
}
TestMetadata::HlHeterogeneousCiphertextList(test) => {
test_hl_heterogeneous_ciphertext_list(test_dir.as_ref(), test, format).into()
}
TestMetadata::HlClientKey(test) => {
test_hl_clientkey(test_dir.as_ref(), test, format).into()
}
Expand Down

0 comments on commit d77a66c

Please sign in to comment.