Skip to content

Commit

Permalink
fix(safe_ser): aliases in named for renamed types deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarlin-zama committed Jan 17, 2025
1 parent a882262 commit f4a8991
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
4 changes: 4 additions & 0 deletions tfhe/src/named.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
pub trait Named {
/// Default name for the type
const NAME: &'static str;
/// Aliases that should also be accepted for backward compatibility when checking the name of
/// values of this type
const BACKWARD_COMPATIBILITY_ALIASES: &'static [&'static str] = &[];
}
52 changes: 50 additions & 2 deletions tfhe/src/safe_serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ Please use the versioned serialization mode for backward compatibility.",
}
}

if self.name != T::NAME {
if self.name != T::NAME
&& T::BACKWARD_COMPATIBILITY_ALIASES
.iter()
.all(|alias| self.name != *alias)
{
return Err(format!(
"On deserialization, expected type {}, got type {}",
T::NAME,
Expand Down Expand Up @@ -494,13 +498,17 @@ pub fn safe_deserialize_conformant<

#[cfg(all(test, feature = "shortint"))]
mod test_shortint {
use crate::safe_serialization::{DeserializationConfig, SerializationConfig};
use tfhe_versionable::Versionize;

use crate::named::Named;
use crate::shortint::parameters::{
PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
V0_11_PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M64,
};
use crate::shortint::{gen_keys, Ciphertext};

use super::*;

#[test]
fn safe_deserialization_ct_unversioned() {
let (ck, _sk) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
Expand Down Expand Up @@ -626,6 +634,46 @@ mod test_shortint {
let dec = ck.decrypt(&ct2);
assert_eq!(msg, dec);
}

#[test]
fn safe_deserialization_named() {
#[derive(Serialize, Deserialize, Versionize)]
#[repr(transparent)]
struct Foo(u64);

impl Named for Foo {
const NAME: &'static str = "Foo";
}

#[derive(Deserialize, Versionize)]
#[repr(transparent)]
struct Bar(u64);

impl Named for Bar {
const NAME: &'static str = "Bar";

const BACKWARD_COMPATIBILITY_ALIASES: &'static [&'static str] = &["Foo"];
}

#[derive(Deserialize, Versionize)]
#[repr(transparent)]
struct Baz(u64);

impl Named for Baz {
const NAME: &'static str = "Baz";
}

let foo = Foo(3);
let mut foo_ser = Vec::new();
safe_serialize(&foo, &mut foo_ser, 0x1000).unwrap();

let foo_deser: Foo = safe_deserialize(foo_ser.as_slice(), 0x1000).unwrap();
let bar_deser: Bar = safe_deserialize(foo_ser.as_slice(), 0x1000).unwrap();

assert_eq!(foo_deser.0, bar_deser.0);

assert!(safe_deserialize::<Baz>(foo_ser.as_slice(), 0x1000).is_err());
}
}

#[cfg(all(test, feature = "integer"))]
Expand Down
2 changes: 2 additions & 0 deletions tfhe/src/zk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ pub enum CompactPkeCrs {

impl Named for CompactPkeCrs {
const NAME: &'static str = "zk::CompactPkeCrs";

const BACKWARD_COMPATIBILITY_ALIASES: &'static [&'static str] = &["zk::CompactPkePublicParams"];
}

impl From<ZkCompactPkeV1PublicParams> for CompactPkeCrs {
Expand Down

0 comments on commit f4a8991

Please sign in to comment.