From e121038484a5b1ae2fa67e66b552816fd543b30a Mon Sep 17 00:00:00 2001 From: "Mayeul@Zama" Date: Wed, 27 Sep 2023 13:44:39 +0200 Subject: [PATCH] add serializtion test --- tfhe/src/safe_serialization.rs | 39 +++++++++++++++++++++++++++++ tfhe/src/shortint/ciphertext/mod.rs | 4 +++ 2 files changed, 43 insertions(+) diff --git a/tfhe/src/safe_serialization.rs b/tfhe/src/safe_serialization.rs index d226863a83..039e78cd96 100644 --- a/tfhe/src/safe_serialization.rs +++ b/tfhe/src/safe_serialization.rs @@ -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::( + 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); + } +} diff --git a/tfhe/src/shortint/ciphertext/mod.rs b/tfhe/src/shortint/ciphertext/mod.rs index 0269325a0e..16abdfe5f1 100644 --- a/tfhe/src/shortint/ciphertext/mod.rs +++ b/tfhe/src/shortint/ciphertext/mod.rs @@ -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;