Skip to content

Commit

Permalink
add serializtion test
Browse files Browse the repository at this point in the history
  • Loading branch information
mayeul-zama committed Sep 28, 2023
1 parent 65ee604 commit e121038
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tfhe/src/safe_serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,42 @@ where
safe_deserialize_conformant(reader, T::serialized_size(parameter), parameter)
}

#[cfg(all(test, feature = "shortint"))]
mod test {

use crate::safe_serialization::{safe_deserialize_conformant, safe_serialize};
use crate::shortint::parameters::{
PARAM_MESSAGE_2_CARRY_2_KS_PBS, PARAM_MESSAGE_3_CARRY_3_KS_PBS,
};
use crate::shortint::{gen_keys, Ciphertext, PBSParameters};

#[test]
fn safe_ser_ct() {
let (ck, _sk) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS);

let msg = 2_u64;

let ct = ck.encrypt(msg);

let mut buffer = vec![];

safe_serialize(&ct, &mut buffer, 1 << 40).unwrap();

assert!(safe_deserialize_conformant::<Ciphertext>(
buffer.as_slice(),
1 << 40,
&PBSParameters::PBS(PARAM_MESSAGE_3_CARRY_3_KS_PBS).to_shortint_conformance_param(),
)
.is_err());

let ct2 = safe_deserialize_conformant(
buffer.as_slice(),
1 << 40,
&PBSParameters::PBS(PARAM_MESSAGE_2_CARRY_2_KS_PBS).to_shortint_conformance_param(),
)
.unwrap();

let dec = ck.decrypt(&ct2);
assert_eq!(msg, dec);
}
}
4 changes: 4 additions & 0 deletions tfhe/src/shortint/ciphertext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ pub struct Ciphertext {
pub pbs_order: PBSOrder,
}

impl crate::Named for Ciphertext {
const NAME: &'static str = "shortint::Ciphertext";
}

impl ParameterSetConformant for Ciphertext {
type ParameterSet = CiphertextConformanceParams;

Expand Down

0 comments on commit e121038

Please sign in to comment.