From fd554e757566c7afec0b6d479f23846ba7f20dd0 Mon Sep 17 00:00:00 2001 From: "Mayeul@Zama" <69792125+mayeul-zama@users.noreply.github.com> Date: Fri, 6 Dec 2024 14:39:05 +0100 Subject: [PATCH] fix(shortint): add ciphertext_modulus_after_packing_ks to compression parameters --- tfhe/src/integer/gpu/client_key/radix.rs | 2 +- .../parameters/list_compression.rs | 57 ++++++++++++++++++- .../compressed_server_keys.rs | 2 +- .../shortint/list_compression/compression.rs | 17 ++++-- .../shortint/list_compression/server_keys.rs | 7 ++- .../shortint/parameters/list_compression.rs | 3 + 6 files changed, 77 insertions(+), 11 deletions(-) diff --git a/tfhe/src/integer/gpu/client_key/radix.rs b/tfhe/src/integer/gpu/client_key/radix.rs index c9c1f1ca4a..702744b1b3 100644 --- a/tfhe/src/integer/gpu/client_key/radix.rs +++ b/tfhe/src/integer/gpu/client_key/radix.rs @@ -43,7 +43,7 @@ impl RadixClientKey { params.packing_ks_base_log, params.packing_ks_level, params.packing_ks_key_noise_distribution, - self.parameters().ciphertext_modulus(), + params.ciphertext_modulus_after_packing_ks, &mut engine.encryption_generator, ) }); diff --git a/tfhe/src/shortint/backward_compatibility/parameters/list_compression.rs b/tfhe/src/shortint/backward_compatibility/parameters/list_compression.rs index 0c50de9225..06992a42de 100644 --- a/tfhe/src/shortint/backward_compatibility/parameters/list_compression.rs +++ b/tfhe/src/shortint/backward_compatibility/parameters/list_compression.rs @@ -1,8 +1,59 @@ -use tfhe_versionable::VersionsDispatch; - use super::parameters::list_compression::CompressionParameters; +use super::CiphertextModulus; +use crate::core_crypto::prelude::{CiphertextModulusLog, LweCiphertextCount}; +use crate::shortint::parameters::{ + DecompositionBaseLog, DecompositionLevelCount, DynamicDistribution, GlweDimension, + PolynomialSize, +}; +use std::convert::Infallible; +use tfhe_versionable::{Upgrade, Version, VersionsDispatch}; + +#[derive(Version)] +pub struct CompressionParametersV0 { + pub br_level: DecompositionLevelCount, + pub br_base_log: DecompositionBaseLog, + pub packing_ks_level: DecompositionLevelCount, + pub packing_ks_base_log: DecompositionBaseLog, + pub packing_ks_polynomial_size: PolynomialSize, + pub packing_ks_glwe_dimension: GlweDimension, + pub lwe_per_glwe: LweCiphertextCount, + pub storage_log_modulus: CiphertextModulusLog, + pub packing_ks_key_noise_distribution: DynamicDistribution, +} + +impl Upgrade for CompressionParametersV0 { + type Error = Infallible; + + fn upgrade(self) -> Result { + let Self { + br_level, + br_base_log, + packing_ks_level, + packing_ks_base_log, + packing_ks_polynomial_size, + packing_ks_glwe_dimension, + lwe_per_glwe, + storage_log_modulus, + packing_ks_key_noise_distribution, + } = self; + + Ok(CompressionParameters { + br_level, + br_base_log, + packing_ks_level, + packing_ks_base_log, + packing_ks_polynomial_size, + packing_ks_glwe_dimension, + lwe_per_glwe, + storage_log_modulus, + packing_ks_key_noise_distribution, + ciphertext_modulus_after_packing_ks: CiphertextModulus::new_native(), + }) + } +} #[derive(VersionsDispatch)] pub enum CompressionParametersVersions { - V0(CompressionParameters), + V0(CompressionParametersV0), + V1(CompressionParameters), } diff --git a/tfhe/src/shortint/list_compression/compressed_server_keys.rs b/tfhe/src/shortint/list_compression/compressed_server_keys.rs index f93770827c..41a059f131 100644 --- a/tfhe/src/shortint/list_compression/compressed_server_keys.rs +++ b/tfhe/src/shortint/list_compression/compressed_server_keys.rs @@ -103,7 +103,7 @@ impl ClientKey { params.packing_ks_base_log, params.packing_ks_level, params.packing_ks_key_noise_distribution, - self.parameters.ciphertext_modulus(), + params.ciphertext_modulus_after_packing_ks, &mut engine.seeder, ) }); diff --git a/tfhe/src/shortint/list_compression/compression.rs b/tfhe/src/shortint/list_compression/compression.rs index 114bb570c9..2c3a4697e1 100644 --- a/tfhe/src/shortint/list_compression/compression.rs +++ b/tfhe/src/shortint/list_compression/compression.rs @@ -25,7 +25,9 @@ impl CompressionKey { let lwe_pksk = &self.packing_key_switching_key; let polynomial_size = lwe_pksk.output_polynomial_size(); - let ciphertext_modulus = lwe_pksk.ciphertext_modulus(); + + let out_ciphertext_modulus = lwe_pksk.ciphertext_modulus(); + let glwe_size = lwe_pksk.output_glwe_size(); let lwe_size = lwe_pksk.input_key_lwe_dimension().to_lwe_size(); @@ -43,6 +45,7 @@ impl CompressionKey { let message_modulus = first_ct.message_modulus; let carry_modulus = first_ct.carry_modulus; let pbs_order = first_ct.pbs_order; + let in_ciphertext_modulus = first_ct.ct.ciphertext_modulus(); assert!( message_modulus.0 <= carry_modulus.0, @@ -86,6 +89,12 @@ impl CompressionKey { "All ciphertexts do not have the same pbs order" ); + assert_eq!( + in_ciphertext_modulus, + ct.ct.ciphertext_modulus(), + "All ciphertexts do not have the same ciphertext modulus" + ); + let mut ct = ct.clone(); let max_noise_level = MaxNoiseLevel::new((ct.noise_level() * message_modulus.0).get()); @@ -94,12 +103,12 @@ impl CompressionKey { list.extend(ct.ct.as_ref()); } - let list = LweCiphertextList::from_container(list, lwe_size, ciphertext_modulus); + let list = LweCiphertextList::from_container(list, lwe_size, in_ciphertext_modulus); let bodies_count = LweCiphertextCount(ct_list.len()); let mut out = - GlweCiphertext::new(0, glwe_size, polynomial_size, ciphertext_modulus); + GlweCiphertext::new(0, glwe_size, polynomial_size, out_ciphertext_modulus); par_keyswitch_lwe_ciphertext_list_and_pack_in_glwe_ciphertext( lwe_pksk, &list, &mut out, @@ -120,7 +129,7 @@ impl CompressionKey { pbs_order, lwe_per_glwe, count, - ciphertext_modulus, + ciphertext_modulus: out_ciphertext_modulus, } } } diff --git a/tfhe/src/shortint/list_compression/server_keys.rs b/tfhe/src/shortint/list_compression/server_keys.rs index 44d15ad500..9fba686673 100644 --- a/tfhe/src/shortint/list_compression/server_keys.rs +++ b/tfhe/src/shortint/list_compression/server_keys.rs @@ -65,7 +65,7 @@ impl ClientKey { params.packing_ks_base_log, params.packing_ks_level, params.packing_ks_key_noise_distribution, - self.parameters.ciphertext_modulus(), + params.ciphertext_modulus_after_packing_ks, &mut engine.encryption_generator, ) }); @@ -116,6 +116,7 @@ pub struct CompressionConformanceParameters { pub packing_ks_base_log: DecompositionBaseLog, pub packing_ks_polynomial_size: PolynomialSize, pub packing_ks_glwe_dimension: GlweDimension, + pub ciphertext_modulus_after_packing_ks: CiphertextModulus, pub lwe_per_glwe: LweCiphertextCount, pub storage_log_modulus: CiphertextModulusLog, pub uncompressed_polynomial_size: PolynomialSize, @@ -132,6 +133,8 @@ impl From<(PBSParameters, CompressionParameters)> for CompressionConformancePara packing_ks_base_log: compression_params.packing_ks_base_log, packing_ks_polynomial_size: compression_params.packing_ks_polynomial_size, packing_ks_glwe_dimension: compression_params.packing_ks_glwe_dimension, + ciphertext_modulus_after_packing_ks: compression_params + .ciphertext_modulus_after_packing_ks, lwe_per_glwe: compression_params.lwe_per_glwe, storage_log_modulus: compression_params.storage_log_modulus, uncompressed_polynomial_size: pbs_params.polynomial_size(), @@ -159,7 +162,7 @@ impl ParameterSetConformant for CompressionKey { .to_equivalent_lwe_dimension(parameter_set.uncompressed_polynomial_size), output_glwe_size: parameter_set.packing_ks_glwe_dimension.to_glwe_size(), output_polynomial_size: parameter_set.packing_ks_polynomial_size, - ciphertext_modulus: parameter_set.cipherext_modulus, + ciphertext_modulus: parameter_set.ciphertext_modulus_after_packing_ks, }; packing_key_switching_key.is_conformant(¶ms) diff --git a/tfhe/src/shortint/parameters/list_compression.rs b/tfhe/src/shortint/parameters/list_compression.rs index 6e34507b11..d575009028 100644 --- a/tfhe/src/shortint/parameters/list_compression.rs +++ b/tfhe/src/shortint/parameters/list_compression.rs @@ -1,5 +1,6 @@ use tfhe_versionable::Versionize; +use super::CiphertextModulus; use crate::core_crypto::prelude::{CiphertextModulusLog, LweCiphertextCount}; use crate::shortint::backward_compatibility::parameters::list_compression::CompressionParametersVersions; use crate::shortint::parameters::{ @@ -20,6 +21,7 @@ pub struct CompressionParameters { pub lwe_per_glwe: LweCiphertextCount, pub storage_log_modulus: CiphertextModulusLog, pub packing_ks_key_noise_distribution: DynamicDistribution, + pub ciphertext_modulus_after_packing_ks: CiphertextModulus, } pub const COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64: CompressionParameters = @@ -36,4 +38,5 @@ pub const V0_11_COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64: CompressionPa lwe_per_glwe: LweCiphertextCount(256), storage_log_modulus: CiphertextModulusLog(12), packing_ks_key_noise_distribution: DynamicDistribution::new_t_uniform(43), + ciphertext_modulus_after_packing_ks: CiphertextModulus::new_native(), };