Skip to content

Commit

Permalink
fix(hlapi): fix Client/Server Key versionning
Browse files Browse the repository at this point in the history
  • Loading branch information
mayeul-zama committed Aug 2, 2024
1 parent 3ba61c0 commit 0e71ca6
Showing 1 changed file with 57 additions and 11 deletions.
68 changes: 57 additions & 11 deletions tfhe/src/high_level_api/backward_compatibility/keys.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::high_level_api::keys::*;
use crate::shortint::list_compression::{CompressionKey, CompressionPrivateKeys, DecompressionKey};
use std::convert::Infallible;
use std::sync::Arc;

use tfhe_versionable::{Upgrade, Version, VersionsDispatch};

use crate::high_level_api::keys::*;

#[derive(VersionsDispatch)]
pub enum ClientKeyVersions {
V0(ClientKey),
Expand Down Expand Up @@ -70,23 +69,45 @@ pub(crate) struct IntegerClientKeyV0 {
pub(crate) wopbs_block_parameters: Option<crate::shortint::WopbsParameters>,
}

impl Upgrade<IntegerClientKey> for IntegerClientKeyV0 {
#[derive(Version)]
pub(crate) struct IntegerClientKeyV1 {
pub(crate) key: crate::integer::ClientKey,
pub(crate) wopbs_block_parameters: Option<crate::shortint::WopbsParameters>,
pub(crate) dedicated_compact_private_key: Option<CompactPrivateKey>,
pub(crate) compression_key: Option<CompressionPrivateKeys>,
}

impl Upgrade<IntegerClientKeyV1> for IntegerClientKeyV0 {
type Error = Infallible;

fn upgrade(self) -> Result<IntegerClientKey, Self::Error> {
Ok(IntegerClientKey {
fn upgrade(self) -> Result<IntegerClientKeyV1, Self::Error> {
Ok(IntegerClientKeyV1 {
key: self.key,
wopbs_block_parameters: self.wopbs_block_parameters,
dedicated_compact_private_key: None,
compression_key: None,
})
}
}

impl Upgrade<IntegerClientKey> for IntegerClientKeyV1 {
type Error = Infallible;

fn upgrade(self) -> Result<IntegerClientKey, Self::Error> {
Ok(IntegerClientKey {
key: self.key,
dedicated_compact_private_key: self.dedicated_compact_private_key,
compression_key: self.compression_key,
})
}
}

#[derive(VersionsDispatch)]
#[allow(unused)]
pub(crate) enum IntegerClientKeyVersions {
V0(IntegerClientKeyV0),
V1(IntegerClientKey),
V1(IntegerClientKeyV1),
V2(IntegerClientKey),
}

#[derive(Version)]
Expand All @@ -95,23 +116,48 @@ pub struct IntegerServerKeyV0 {
pub(crate) wopbs_key: Option<crate::integer::wopbs::WopbsKey>,
}

impl Upgrade<IntegerServerKey> for IntegerServerKeyV0 {
#[derive(Version)]
pub struct IntegerServerKeyV1 {
pub(crate) key: crate::integer::ServerKey,
pub(crate) wopbs_key: Option<crate::integer::wopbs::WopbsKey>,
pub(crate) cpk_key_switching_key_material:
Option<crate::integer::key_switching_key::KeySwitchingKeyMaterial>,
pub(crate) compression_key: Option<CompressionKey>,
pub(crate) decompression_key: Option<DecompressionKey>,
}

impl Upgrade<IntegerServerKeyV1> for IntegerServerKeyV0 {
type Error = Infallible;

fn upgrade(self) -> Result<IntegerServerKey, Self::Error> {
Ok(IntegerServerKey {
fn upgrade(self) -> Result<IntegerServerKeyV1, Self::Error> {
Ok(IntegerServerKeyV1 {
key: self.key,
wopbs_key: self.wopbs_key,
cpk_key_switching_key_material: None,
compression_key: None,
decompression_key: None,
})
}
}

impl Upgrade<IntegerServerKey> for IntegerServerKeyV1 {
type Error = Infallible;

fn upgrade(self) -> Result<IntegerServerKey, Self::Error> {
Ok(IntegerServerKey {
key: self.key,
cpk_key_switching_key_material: self.cpk_key_switching_key_material,
compression_key: self.compression_key,
decompression_key: self.decompression_key,
})
}
}

#[derive(VersionsDispatch)]
pub enum IntegerServerKeyVersions {
V0(IntegerServerKeyV0),
V1(IntegerServerKey),
V1(IntegerServerKeyV1),
V2(IntegerServerKey),
}

#[derive(Version)]
Expand Down

0 comments on commit 0e71ca6

Please sign in to comment.