From c0170065b2201e57b84a8588ff0c8210c267d113 Mon Sep 17 00:00:00 2001 From: Arthur Meyre Date: Wed, 4 Sep 2024 10:30:48 +0200 Subject: [PATCH] chore(data)!: breaking data changes for future compatibility - invert the LweKeyswitchKey level order and propagate change - remove dependency on unsupported wopbs keys for the HL keys --- .../cuda/src/crypto/keyswitch.cuh | 6 +- .../core_crypto/algorithms/lwe_keyswitch.rs | 13 +- .../lwe_keyswitch_key_generation.rs | 3 - .../algorithms/lwe_packing_keyswitch.rs | 2 +- .../lwe_packing_keyswitch_key_generation.rs | 2 - .../entities/lwe_keyswitch_key.rs | 52 +++++++- .../entities/lwe_packing_keyswitch_key.rs | 55 +++++++- .../entities/seeded_lwe_keyswitch_key.rs | 22 ++- .../seeded_lwe_packing_keyswitch_key.rs | 22 ++- .../traits/contiguous_entity_container.rs | 11 ++ .../algorithms/lwe_shrinking_keyswitch.rs | 3 +- .../backward_compatibility/keys.rs | 126 +++++++----------- .../key_switching_key.rs | 38 +++++- .../list_compression.rs | 20 ++- .../src/integer/backward_compatibility/mod.rs | 1 - .../backward_compatibility/server_key/mod.rs | 20 ++- .../backward_compatibility/wopbs/mod.rs | 8 -- tfhe/src/integer/mod.rs | 2 - tfhe/src/integer/wopbs/mod.rs | 6 +- .../key_switching_key.rs | 38 +++++- .../list_compression.rs | 20 ++- .../shortint/backward_compatibility/mod.rs | 1 - .../backward_compatibility/server_key/mod.rs | 20 ++- .../backward_compatibility/wopbs/mod.rs | 8 -- tfhe/src/shortint/wopbs/mod.rs | 6 +- 25 files changed, 360 insertions(+), 145 deletions(-) delete mode 100644 tfhe/src/integer/backward_compatibility/wopbs/mod.rs delete mode 100644 tfhe/src/shortint/backward_compatibility/wopbs/mod.rs diff --git a/backends/tfhe-cuda-backend/cuda/src/crypto/keyswitch.cuh b/backends/tfhe-cuda-backend/cuda/src/crypto/keyswitch.cuh index f84fee7030..3e6c87648e 100644 --- a/backends/tfhe-cuda-backend/cuda/src/crypto/keyswitch.cuh +++ b/backends/tfhe-cuda-backend/cuda/src/crypto/keyswitch.cuh @@ -75,7 +75,8 @@ keyswitch(Torus *lwe_array_out, const Torus *__restrict__ lwe_output_indexes, level_count); Torus state = a_i >> (sizeof(Torus) * 8 - base_log * level_count); - for (int j = 0; j < level_count; j++) { + for (int j = level_count - 1; j >= 0; j--) { + // Levels are stored in reverse order auto ksk_block = get_ith_block(ksk, i, j, lwe_dimension_out, level_count); Torus decomposed = decompose_one(state, mask_mod_b, base_log); @@ -208,7 +209,8 @@ __device__ void packing_keyswitch_lwe_ciphertext_into_glwe_ciphertext( // block of key for current lwe coefficient (cur_input_lwe[i]) auto ksk_block = &fp_ksk[i * ksk_block_size]; - for (int j = 0; j < level_count; j++) { + for (int j = level_count - 1; j >= 0; j--) { + // Levels are stored in reverse order auto ksk_glwe = &ksk_block[j * glwe_size * polynomial_size]; // Iterate through each level and multiply by the ksk piece auto ksk_glwe_chunk = &ksk_glwe[poly_id * coef_per_block]; diff --git a/tfhe/src/core_crypto/algorithms/lwe_keyswitch.rs b/tfhe/src/core_crypto/algorithms/lwe_keyswitch.rs index e16c29ebb7..2ebd836094 100644 --- a/tfhe/src/core_crypto/algorithms/lwe_keyswitch.rs +++ b/tfhe/src/core_crypto/algorithms/lwe_keyswitch.rs @@ -216,7 +216,8 @@ pub fn keyswitch_lwe_ciphertext_native_mod_compatible +where + C::Element: UnsignedInteger, +{ + data: C, + decomp_base_log: DecompositionBaseLog, + decomp_level_count: DecompositionLevelCount, + output_lwe_size: LweSize, + ciphertext_modulus: CiphertextModulus, +} + +impl> Upgrade> + for LweKeyswitchKeyV0 +{ + type Error = std::convert::Infallible; + + fn upgrade(self) -> Result, Self::Error> { + let Self { + data, + decomp_base_log, + decomp_level_count, + output_lwe_size, + ciphertext_modulus, + } = self; + let mut new_ksk = LweKeyswitchKey::from_container( + data, + decomp_base_log, + decomp_level_count, + output_lwe_size, + ciphertext_modulus, + ); + + // Invert levels + for mut ksk_block in new_ksk.iter_mut() { + ksk_block.reverse(); + } + + Ok(new_ksk) + } +} #[derive(VersionsDispatch)] pub enum LweKeyswitchKeyVersions where C::Element: UnsignedInteger, { - V0(LweKeyswitchKey), + V0(LweKeyswitchKeyV0), + V1(LweKeyswitchKey), } diff --git a/tfhe/src/core_crypto/backward_compatibility/entities/lwe_packing_keyswitch_key.rs b/tfhe/src/core_crypto/backward_compatibility/entities/lwe_packing_keyswitch_key.rs index 814a11fd7a..1c5ad9958a 100644 --- a/tfhe/src/core_crypto/backward_compatibility/entities/lwe_packing_keyswitch_key.rs +++ b/tfhe/src/core_crypto/backward_compatibility/entities/lwe_packing_keyswitch_key.rs @@ -1,11 +1,60 @@ -use tfhe_versionable::VersionsDispatch; +use tfhe_versionable::{Upgrade, Version, VersionsDispatch}; -use crate::core_crypto::prelude::{Container, LwePackingKeyswitchKey, UnsignedInteger}; +use crate::core_crypto::prelude::{ + CiphertextModulus, Container, ContainerMut, ContiguousEntityContainerMut, DecompositionBaseLog, + DecompositionLevelCount, GlweSize, LwePackingKeyswitchKey, PolynomialSize, UnsignedInteger, +}; + +#[derive(Version)] +pub struct LwePackingKeyswitchKeyV0 +where + C::Element: UnsignedInteger, +{ + data: C, + decomp_base_log: DecompositionBaseLog, + decomp_level_count: DecompositionLevelCount, + output_glwe_size: GlweSize, + output_polynomial_size: PolynomialSize, + ciphertext_modulus: CiphertextModulus, +} + +impl> Upgrade> + for LwePackingKeyswitchKeyV0 +{ + type Error = std::convert::Infallible; + + fn upgrade(self) -> Result, Self::Error> { + let Self { + data, + decomp_base_log, + decomp_level_count, + output_glwe_size, + output_polynomial_size, + ciphertext_modulus, + } = self; + let mut new_pksk = LwePackingKeyswitchKey::from_container( + data, + decomp_base_log, + decomp_level_count, + output_glwe_size, + output_polynomial_size, + ciphertext_modulus, + ); + + // Invert levels + for mut pksk_block in new_pksk.iter_mut() { + pksk_block.reverse(); + } + + Ok(new_pksk) + } +} #[derive(VersionsDispatch)] pub enum LwePackingKeyswitchKeyVersions where C::Element: UnsignedInteger, { - V0(LwePackingKeyswitchKey), + V0(LwePackingKeyswitchKeyV0), + V1(LwePackingKeyswitchKey), } diff --git a/tfhe/src/core_crypto/backward_compatibility/entities/seeded_lwe_keyswitch_key.rs b/tfhe/src/core_crypto/backward_compatibility/entities/seeded_lwe_keyswitch_key.rs index 822431c51f..6c629c31ae 100644 --- a/tfhe/src/core_crypto/backward_compatibility/entities/seeded_lwe_keyswitch_key.rs +++ b/tfhe/src/core_crypto/backward_compatibility/entities/seeded_lwe_keyswitch_key.rs @@ -1,11 +1,29 @@ -use tfhe_versionable::VersionsDispatch; +use tfhe_versionable::{Upgrade, Version, VersionsDispatch}; use crate::core_crypto::prelude::{Container, SeededLweKeyswitchKey, UnsignedInteger}; +#[derive(Version)] +pub struct UnsupportedSeededLweKeyswitchKeyV0; + +impl> Upgrade> + for UnsupportedSeededLweKeyswitchKeyV0 +{ + type Error = crate::Error; + + fn upgrade(self) -> Result, Self::Error> { + Err(crate::Error::new( + "Unable to load SeededLweKeyswitchKey, \ + this format is UnsupportedSeededLweKeyswitchKeyV0 by this TFHE-rs version." + .to_string(), + )) + } +} + #[derive(VersionsDispatch)] pub enum SeededLweKeyswitchKeyVersions where C::Element: UnsignedInteger, { - V0(SeededLweKeyswitchKey), + V0(UnsupportedSeededLweKeyswitchKeyV0), + V1(SeededLweKeyswitchKey), } diff --git a/tfhe/src/core_crypto/backward_compatibility/entities/seeded_lwe_packing_keyswitch_key.rs b/tfhe/src/core_crypto/backward_compatibility/entities/seeded_lwe_packing_keyswitch_key.rs index cade040ee0..c74c492a9c 100644 --- a/tfhe/src/core_crypto/backward_compatibility/entities/seeded_lwe_packing_keyswitch_key.rs +++ b/tfhe/src/core_crypto/backward_compatibility/entities/seeded_lwe_packing_keyswitch_key.rs @@ -1,11 +1,29 @@ -use tfhe_versionable::VersionsDispatch; +use tfhe_versionable::{Upgrade, Version, VersionsDispatch}; use crate::core_crypto::prelude::{Container, SeededLwePackingKeyswitchKey, UnsignedInteger}; +#[derive(Version)] +pub struct UnsupportedSeededLwePackingKeyswitchKeyV0; + +impl> + Upgrade> for UnsupportedSeededLwePackingKeyswitchKeyV0 +{ + type Error = crate::Error; + + fn upgrade(self) -> Result, Self::Error> { + Err(crate::Error::new( + "Unable to load SeededLwePackingKeyswitchKey, \ + this format is unsupported by this TFHE-rs version." + .to_string(), + )) + } +} + #[derive(VersionsDispatch)] pub enum SeededLwePackingKeyswitchKeyVersions where C::Element: UnsignedInteger, { - V0(SeededLwePackingKeyswitchKey), + V0(UnsupportedSeededLwePackingKeyswitchKeyV0), + V1(SeededLwePackingKeyswitchKey), } diff --git a/tfhe/src/core_crypto/commons/traits/contiguous_entity_container.rs b/tfhe/src/core_crypto/commons/traits/contiguous_entity_container.rs index 93b5697a7a..7f34cfe47e 100644 --- a/tfhe/src/core_crypto/commons/traits/contiguous_entity_container.rs +++ b/tfhe/src/core_crypto/commons/traits/contiguous_entity_container.rs @@ -433,6 +433,17 @@ pub trait ContiguousEntityContainerMut: ContiguousEntityContainer + AsMut<[Self: .map(|(elt, meta)| Self::SelfMutView::<'_>::create_from(elt, meta)) } + fn reverse(&mut self) { + let entity_view_pod_size = self.get_entity_view_pod_size(); + let container = self.as_mut(); + + container.reverse(); + + for entity_slot in self.as_mut().chunks_exact_mut(entity_view_pod_size) { + entity_slot.reverse(); + } + } + fn par_iter_mut<'this>( &'this mut self, ) -> ParallelChunksExactWrappingLendingIteratorMut< diff --git a/tfhe/src/core_crypto/experimental/algorithms/lwe_shrinking_keyswitch.rs b/tfhe/src/core_crypto/experimental/algorithms/lwe_shrinking_keyswitch.rs index a91657f3de..04cc741054 100644 --- a/tfhe/src/core_crypto/experimental/algorithms/lwe_shrinking_keyswitch.rs +++ b/tfhe/src/core_crypto/experimental/algorithms/lwe_shrinking_keyswitch.rs @@ -174,7 +174,8 @@ pub fn shrinking_keyswitch_lwe_ciphertext, -} +impl Upgrade for UnsupportedIntegerClientKeyV0 { + type Error = crate::Error; -#[derive(Version)] -pub(crate) struct IntegerClientKeyV1 { - pub(crate) key: crate::integer::ClientKey, - pub(crate) wopbs_block_parameters: Option, - pub(crate) dedicated_compact_private_key: Option, - pub(crate) compression_key: Option, -} - -impl Upgrade for IntegerClientKeyV0 { - type Error = Infallible; - - fn upgrade(self) -> Result { - Ok(IntegerClientKeyV1 { - key: self.key, - wopbs_block_parameters: self.wopbs_block_parameters, - dedicated_compact_private_key: None, - compression_key: None, - }) + fn upgrade(self) -> Result { + Err(crate::Error::new( + "Unable to load data this format is unsupported by this TFHE-rs version.".to_string(), + )) } } -impl Upgrade for IntegerClientKeyV1 { - type Error = Infallible; +impl Upgrade for UnsupportedIntegerClientKeyV1 { + type Error = crate::Error; fn upgrade(self) -> Result { - Ok(IntegerClientKeyV2 { - key: self.key, - dedicated_compact_private_key: self.dedicated_compact_private_key, - compression_key: self.compression_key, - }) + Err(crate::Error::new( + "Unable to load IntegerClientKey, \ + this format is unsupported by this TFHE-rs version." + .to_string(), + )) } } @@ -247,52 +235,39 @@ impl Upgrade for IntegerClientKeyV2 { #[derive(VersionsDispatch)] #[allow(unused)] pub(crate) enum IntegerClientKeyVersions { - V0(IntegerClientKeyV0), - V1(IntegerClientKeyV1), + V0(UnsupportedIntegerClientKeyV0), + V1(UnsupportedIntegerClientKeyV1), V2(IntegerClientKeyV2), V3(IntegerClientKey), } #[derive(Version)] -pub struct IntegerServerKeyV0 { - pub(crate) key: crate::integer::ServerKey, - pub(crate) wopbs_key: Option, -} +pub struct UnsupportedIntegerServerKeyV0; #[derive(Version)] -pub struct IntegerServerKeyV1 { - pub(crate) key: crate::integer::ServerKey, - pub(crate) wopbs_key: Option, - pub(crate) cpk_key_switching_key_material: - Option, - pub(crate) compression_key: Option, - pub(crate) decompression_key: Option, -} +pub struct UnsupportedIntegerServerKeyV1; -impl Upgrade for IntegerServerKeyV0 { - type Error = Infallible; +impl Upgrade for UnsupportedIntegerServerKeyV0 { + type Error = crate::Error; - fn upgrade(self) -> Result { - Ok(IntegerServerKeyV1 { - key: self.key, - wopbs_key: self.wopbs_key, - cpk_key_switching_key_material: None, - compression_key: None, - decompression_key: None, - }) + fn upgrade(self) -> Result { + Err(crate::Error::new( + "Unable to load IntegerServerKey, \ + this format is unsupported by this TFHE-rs version." + .to_string(), + )) } } -impl Upgrade for IntegerServerKeyV1 { - type Error = Infallible; +impl Upgrade for UnsupportedIntegerServerKeyV1 { + type Error = crate::Error; fn upgrade(self) -> Result { - Ok(IntegerServerKeyV2 { - key: self.key, - cpk_key_switching_key_material: self.cpk_key_switching_key_material, - compression_key: self.compression_key, - decompression_key: self.decompression_key, - }) + Err(crate::Error::new( + "Unable to load IntegerServerKey, \ + this format is unsupported by this TFHE-rs version." + .to_string(), + )) } } @@ -324,33 +299,30 @@ impl Upgrade for IntegerServerKeyV2 { #[derive(VersionsDispatch)] pub enum IntegerServerKeyVersions { - V0(IntegerServerKeyV0), - V1(IntegerServerKeyV1), + V0(UnsupportedIntegerServerKeyV0), + V1(UnsupportedIntegerServerKeyV1), V2(IntegerServerKeyV2), V3(IntegerServerKey), } #[derive(Version)] -pub struct IntegerCompressedServerKeyV0 { - pub(crate) key: crate::integer::CompressedServerKey, -} +pub struct UnsupportedIntegerCompressedServerKeyV0; -impl Upgrade for IntegerCompressedServerKeyV0 { - type Error = Infallible; +impl Upgrade for UnsupportedIntegerCompressedServerKeyV0 { + type Error = crate::Error; fn upgrade(self) -> Result { - Ok(IntegerCompressedServerKey { - key: self.key, - cpk_key_switching_key_material: None, - compression_key: None, - decompression_key: None, - }) + Err(crate::Error::new( + "Unable to load IntegerCompressedServerKey, \ + this format is unsupported by this TFHE-rs version." + .to_string(), + )) } } #[derive(VersionsDispatch)] pub enum IntegerCompressedServerKeyVersions { - V0(IntegerCompressedServerKeyV0), + V0(UnsupportedIntegerCompressedServerKeyV0), V1(IntegerCompressedServerKey), } diff --git a/tfhe/src/integer/backward_compatibility/key_switching_key.rs b/tfhe/src/integer/backward_compatibility/key_switching_key.rs index 646d298228..dfed143fe9 100644 --- a/tfhe/src/integer/backward_compatibility/key_switching_key.rs +++ b/tfhe/src/integer/backward_compatibility/key_switching_key.rs @@ -1,4 +1,4 @@ -use tfhe_versionable::VersionsDispatch; +use tfhe_versionable::{Upgrade, Version, VersionsDispatch}; use crate::integer::key_switching_key::{ CompressedKeySwitchingKey, CompressedKeySwitchingKeyMaterial, KeySwitchingKey, @@ -15,12 +15,44 @@ pub enum KeySwitchingKeyVersions { V0(KeySwitchingKey), } +#[derive(Version)] +pub struct UnsupportedCompressedKeySwitchingKeyMaterialV0; + +impl Upgrade for UnsupportedCompressedKeySwitchingKeyMaterialV0 { + type Error = crate::Error; + + fn upgrade(self) -> Result { + Err(crate::Error::new( + "Unable to load CompressedKeySwitchingKeyMaterial, \ + this format is unsupported by this TFHE-rs version." + .to_string(), + )) + } +} + #[derive(VersionsDispatch)] pub enum CompressedKeySwitchingKeyMaterialVersions { - V0(CompressedKeySwitchingKeyMaterial), + V0(UnsupportedCompressedKeySwitchingKeyMaterialV0), + V1(CompressedKeySwitchingKeyMaterial), +} + +#[derive(Version)] +pub struct UnsupportedCompressedKeySwitchingKeyV0; + +impl Upgrade for UnsupportedCompressedKeySwitchingKeyV0 { + type Error = crate::Error; + + fn upgrade(self) -> Result { + Err(crate::Error::new( + "Unable to load CompressedKeySwitchingKey, \ + this format is unsupported by this TFHE-rs version." + .to_string(), + )) + } } #[derive(VersionsDispatch)] pub enum CompressedKeySwitchingKeyVersions { - V0(CompressedKeySwitchingKey), + V0(UnsupportedCompressedKeySwitchingKeyV0), + V1(CompressedKeySwitchingKey), } diff --git a/tfhe/src/integer/backward_compatibility/list_compression.rs b/tfhe/src/integer/backward_compatibility/list_compression.rs index 8ddc63a5d1..b73e8bcfa4 100644 --- a/tfhe/src/integer/backward_compatibility/list_compression.rs +++ b/tfhe/src/integer/backward_compatibility/list_compression.rs @@ -2,7 +2,7 @@ use crate::integer::compression_keys::{ CompressedCompressionKey, CompressedDecompressionKey, CompressionKey, CompressionPrivateKeys, DecompressionKey, }; -use tfhe_versionable::VersionsDispatch; +use tfhe_versionable::{Upgrade, Version, VersionsDispatch}; #[derive(VersionsDispatch)] pub enum CompressionKeyVersions { @@ -14,9 +14,25 @@ pub enum DecompressionKeyVersions { V0(DecompressionKey), } +#[derive(Version)] +pub struct UnsupportedCompressedCompressionKeyV0; + +impl Upgrade for UnsupportedCompressedCompressionKeyV0 { + type Error = crate::Error; + + fn upgrade(self) -> Result { + Err(crate::Error::new( + "Unable to load CompressedCompressionKey, \ + this format is unsupported by this TFHE-rs version." + .to_string(), + )) + } +} + #[derive(VersionsDispatch)] pub enum CompressedCompressionKeyVersions { - V0(CompressedCompressionKey), + V0(UnsupportedCompressedCompressionKeyV0), + V1(CompressedCompressionKey), } #[derive(VersionsDispatch)] diff --git a/tfhe/src/integer/backward_compatibility/mod.rs b/tfhe/src/integer/backward_compatibility/mod.rs index fba2e6ebee..293c904141 100644 --- a/tfhe/src/integer/backward_compatibility/mod.rs +++ b/tfhe/src/integer/backward_compatibility/mod.rs @@ -6,4 +6,3 @@ pub mod key_switching_key; pub mod list_compression; pub mod public_key; pub mod server_key; -pub mod wopbs; diff --git a/tfhe/src/integer/backward_compatibility/server_key/mod.rs b/tfhe/src/integer/backward_compatibility/server_key/mod.rs index 00e31961ea..219d5ad253 100644 --- a/tfhe/src/integer/backward_compatibility/server_key/mod.rs +++ b/tfhe/src/integer/backward_compatibility/server_key/mod.rs @@ -1,4 +1,4 @@ -use tfhe_versionable::VersionsDispatch; +use tfhe_versionable::{Upgrade, Version, VersionsDispatch}; use crate::integer::{CompressedServerKey, ServerKey}; @@ -7,7 +7,23 @@ pub enum ServerKeyVersions { V0(ServerKey), } +#[derive(Version)] +pub struct UnsupportedCompressedServerKeyV0; + +impl Upgrade for UnsupportedCompressedServerKeyV0 { + type Error = crate::Error; + + fn upgrade(self) -> Result { + Err(crate::Error::new( + "Unable to load CompressedServerKey, \ + this format is unsupported by this TFHE-rs version." + .to_string(), + )) + } +} + #[derive(VersionsDispatch)] pub enum CompressedServerKeyVersions { - V0(CompressedServerKey), + V0(UnsupportedCompressedServerKeyV0), + V1(CompressedServerKey), } diff --git a/tfhe/src/integer/backward_compatibility/wopbs/mod.rs b/tfhe/src/integer/backward_compatibility/wopbs/mod.rs deleted file mode 100644 index ddc0f350c7..0000000000 --- a/tfhe/src/integer/backward_compatibility/wopbs/mod.rs +++ /dev/null @@ -1,8 +0,0 @@ -use tfhe_versionable::VersionsDispatch; - -use crate::integer::wopbs::WopbsKey; - -#[derive(VersionsDispatch)] -pub enum WopbsKeyVersions { - V0(WopbsKey), -} diff --git a/tfhe/src/integer/mod.rs b/tfhe/src/integer/mod.rs index 03ae281b20..b83ed0d417 100755 --- a/tfhe/src/integer/mod.rs +++ b/tfhe/src/integer/mod.rs @@ -65,8 +65,6 @@ pub mod public_key; pub mod server_key; #[cfg(feature = "experimental")] pub mod wopbs; -#[cfg(not(feature = "experimental"))] -pub(crate) mod wopbs; #[cfg(feature = "gpu")] pub mod gpu; diff --git a/tfhe/src/integer/wopbs/mod.rs b/tfhe/src/integer/wopbs/mod.rs index a9f468fd7d..a3fee243c8 100644 --- a/tfhe/src/integer/wopbs/mod.rs +++ b/tfhe/src/integer/wopbs/mod.rs @@ -6,13 +6,9 @@ #[cfg(all(test, feature = "experimental"))] mod test; -use super::backward_compatibility::wopbs::WopbsKeyVersions; - use serde::{Deserialize, Serialize}; -use tfhe_versionable::Versionize; -#[derive(Clone, Serialize, Deserialize, Versionize)] -#[versionize(WopbsKeyVersions)] +#[derive(Clone, Serialize, Deserialize)] pub struct WopbsKey { wopbs_key: crate::shortint::wopbs::WopbsKey, } diff --git a/tfhe/src/shortint/backward_compatibility/key_switching_key.rs b/tfhe/src/shortint/backward_compatibility/key_switching_key.rs index 7686a21153..623f31b077 100644 --- a/tfhe/src/shortint/backward_compatibility/key_switching_key.rs +++ b/tfhe/src/shortint/backward_compatibility/key_switching_key.rs @@ -1,10 +1,13 @@ -use tfhe_versionable::VersionsDispatch; +use tfhe_versionable::{Upgrade, Version, VersionsDispatch}; use crate::shortint::key_switching_key::{ CompressedKeySwitchingKeyMaterial, KeySwitchingKeyMaterial, }; use crate::shortint::{CompressedKeySwitchingKey, KeySwitchingKey}; +#[derive(Version)] +pub struct UnsupportedCompressedKeySwitchingKeyMaterialV0; + #[derive(VersionsDispatch)] pub enum KeySwitchingKeyMaterialVersions { V0(KeySwitchingKeyMaterial), @@ -15,12 +18,41 @@ pub enum KeySwitchingKeyVersions { V0(KeySwitchingKey), } +impl Upgrade for UnsupportedCompressedKeySwitchingKeyMaterialV0 { + type Error = crate::Error; + + fn upgrade(self) -> Result { + Err(crate::Error::new( + "Unable to load CompressedKeySwitchingKeyMaterial, \ + this format is unsupported by this TFHE-rs version." + .to_string(), + )) + } +} + #[derive(VersionsDispatch)] pub enum CompressedKeySwitchingKeyMaterialVersions { - V0(CompressedKeySwitchingKeyMaterial), + V0(UnsupportedCompressedKeySwitchingKeyMaterialV0), + V1(CompressedKeySwitchingKeyMaterial), +} + +#[derive(Version)] +pub struct UnsupportedCompressedKeySwitchingKeyV0; + +impl Upgrade for UnsupportedCompressedKeySwitchingKeyV0 { + type Error = crate::Error; + + fn upgrade(self) -> Result { + Err(crate::Error::new( + "Unable to load CompressedKeySwitchingKey, \ + this format is unsupported by this TFHE-rs version." + .to_string(), + )) + } } #[derive(VersionsDispatch)] pub enum CompressedKeySwitchingKeyVersions { - V0(CompressedKeySwitchingKey), + V0(UnsupportedCompressedKeySwitchingKeyV0), + V1(CompressedKeySwitchingKey), } diff --git a/tfhe/src/shortint/backward_compatibility/list_compression.rs b/tfhe/src/shortint/backward_compatibility/list_compression.rs index 5f2a129d57..a5959a16c3 100644 --- a/tfhe/src/shortint/backward_compatibility/list_compression.rs +++ b/tfhe/src/shortint/backward_compatibility/list_compression.rs @@ -1,4 +1,4 @@ -use tfhe_versionable::VersionsDispatch; +use tfhe_versionable::{Upgrade, Version, VersionsDispatch}; use crate::shortint::list_compression::{ CompressedCompressionKey, CompressedDecompressionKey, CompressionKey, CompressionPrivateKeys, @@ -15,9 +15,25 @@ pub enum DecompressionKeyVersions { V0(DecompressionKey), } +#[derive(Version)] +pub struct UnsupportedCompressedCompressionKeyV0; + +impl Upgrade for UnsupportedCompressedCompressionKeyV0 { + type Error = crate::Error; + + fn upgrade(self) -> Result { + Err(crate::Error::new( + "Unable to load CompressedCompressionKey, \ + this format is unsupported by this TFHE-rs version." + .to_string(), + )) + } +} + #[derive(VersionsDispatch)] pub enum CompressedCompressionKeyVersions { - V0(CompressedCompressionKey), + V0(UnsupportedCompressedCompressionKeyV0), + V1(CompressedCompressionKey), } #[derive(VersionsDispatch)] diff --git a/tfhe/src/shortint/backward_compatibility/mod.rs b/tfhe/src/shortint/backward_compatibility/mod.rs index 15e371dbbf..7fd49a6fe6 100644 --- a/tfhe/src/shortint/backward_compatibility/mod.rs +++ b/tfhe/src/shortint/backward_compatibility/mod.rs @@ -7,4 +7,3 @@ pub mod list_compression; pub mod parameters; pub mod public_key; pub mod server_key; -pub mod wopbs; diff --git a/tfhe/src/shortint/backward_compatibility/server_key/mod.rs b/tfhe/src/shortint/backward_compatibility/server_key/mod.rs index 4bd9ddd908..6d1d295ade 100644 --- a/tfhe/src/shortint/backward_compatibility/server_key/mod.rs +++ b/tfhe/src/shortint/backward_compatibility/server_key/mod.rs @@ -1,4 +1,4 @@ -use tfhe_versionable::VersionsDispatch; +use tfhe_versionable::{Upgrade, Version, VersionsDispatch}; use crate::core_crypto::prelude::Container; use crate::shortint::server_key::*; @@ -18,7 +18,23 @@ pub enum ShortintCompressedBootstrappingKeyVersions { V0(ShortintCompressedBootstrappingKey), } +#[derive(Version)] +pub struct UnsupportedCompressedServerKeyV0; + +impl Upgrade for UnsupportedCompressedServerKeyV0 { + type Error = crate::Error; + + fn upgrade(self) -> Result { + Err(crate::Error::new( + "Unable to load CompressedServerKey, \ + this format is unsupported by this TFHE-rs version." + .to_string(), + )) + } +} + #[derive(VersionsDispatch)] pub enum CompressedServerKeyVersions { - V0(CompressedServerKey), + V0(UnsupportedCompressedServerKeyV0), + V1(CompressedServerKey), } diff --git a/tfhe/src/shortint/backward_compatibility/wopbs/mod.rs b/tfhe/src/shortint/backward_compatibility/wopbs/mod.rs deleted file mode 100644 index 8426b1da1b..0000000000 --- a/tfhe/src/shortint/backward_compatibility/wopbs/mod.rs +++ /dev/null @@ -1,8 +0,0 @@ -use tfhe_versionable::VersionsDispatch; - -use crate::shortint::wopbs::WopbsKey; - -#[derive(VersionsDispatch)] -pub enum WopbsKeyVersions { - V0(WopbsKey), -} diff --git a/tfhe/src/shortint/wopbs/mod.rs b/tfhe/src/shortint/wopbs/mod.rs index dbcfea1e45..8ba6fcceba 100644 --- a/tfhe/src/shortint/wopbs/mod.rs +++ b/tfhe/src/shortint/wopbs/mod.rs @@ -11,16 +11,12 @@ use crate::core_crypto::entities::*; use crate::shortint::{ServerKey, WopbsParameters}; use serde::{Deserialize, Serialize}; -use tfhe_versionable::Versionize; - -use super::backward_compatibility::wopbs::WopbsKeyVersions; #[cfg(all(test, feature = "experimental"))] mod test; // Struct for WoPBS based on the private functional packing keyswitch. -#[derive(Clone, Debug, Serialize, Deserialize, Versionize)] -#[versionize(WopbsKeyVersions)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct WopbsKey { //Key for the private functional keyswitch pub wopbs_server_key: ServerKey,