Skip to content

Commit

Permalink
chore(all): use destructuring in conformance
Browse files Browse the repository at this point in the history
  • Loading branch information
mayeul-zama committed Aug 26, 2024
1 parent 35a9c32 commit 11a8f97
Show file tree
Hide file tree
Showing 22 changed files with 230 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,25 @@ impl<Scalar: UnsignedInteger> ParameterSetConformant
&self,
lwe_ct_parameters: &GlweCiphertextConformanceParameters<Scalar>,
) -> bool {
let log_modulus = self.packed_integers.log_modulus.0;
let Self {
packed_integers,
glwe_dimension,
polynomial_size,
bodies_count,
uncompressed_ciphertext_modulus,
} = self;
let log_modulus = packed_integers.log_modulus.0;

let number_bits_to_unpack =
(self.glwe_dimension.0 * self.polynomial_size.0 + self.bodies_count.0) * log_modulus;
(glwe_dimension.0 * polynomial_size.0 + bodies_count.0) * log_modulus;

let len = number_bits_to_unpack.div_ceil(Scalar::BITS);

self.packed_integers.packed_coeffs.len() == len
&& self.glwe_dimension == lwe_ct_parameters.glwe_dim
&& self.polynomial_size == lwe_ct_parameters.polynomial_size
packed_integers.packed_coeffs.len() == len
&& *glwe_dimension == lwe_ct_parameters.glwe_dim
&& *polynomial_size == lwe_ct_parameters.polynomial_size
&& lwe_ct_parameters.ct_modulus.is_power_of_two()
&& self.uncompressed_ciphertext_modulus == lwe_ct_parameters.ct_modulus
&& *uncompressed_ciphertext_modulus == lwe_ct_parameters.ct_modulus
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,22 @@ impl<Scalar: UnsignedInteger> ParameterSetConformant
type ParameterSet = LweCiphertextParameters<Scalar>;

fn is_conformant(&self, lwe_ct_parameters: &LweCiphertextParameters<Scalar>) -> bool {
let lwe_size = self.lwe_dimension.to_lwe_size().0;
let Self {
packed_integers,
lwe_dimension,
uncompressed_ciphertext_modulus,
} = self;

let lwe_size = lwe_dimension.to_lwe_size().0;

let number_bits_to_pack = lwe_size * self.packed_integers.log_modulus.0;
let number_bits_to_pack = lwe_size * packed_integers.log_modulus.0;

let len = number_bits_to_pack.div_ceil(Scalar::BITS);

self.packed_integers.packed_coeffs.len() == len
&& self.lwe_dimension == lwe_ct_parameters.lwe_dim
packed_integers.packed_coeffs.len() == len
&& *lwe_dimension == lwe_ct_parameters.lwe_dim
&& lwe_ct_parameters.ct_modulus.is_power_of_two()
&& self.uncompressed_ciphertext_modulus == lwe_ct_parameters.ct_modulus
&& *uncompressed_ciphertext_modulus == lwe_ct_parameters.ct_modulus
&& matches!(
lwe_ct_parameters.ms_decompression_method,
MsDecompressionType::ClassicPbs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,24 +403,33 @@ impl MultiBitModulusSwitchedCt for FromCompressionMultiBitModulusSwitchedCt {
impl<Scalar: UnsignedInteger + CastInto<usize> + CastFrom<usize>> ParameterSetConformant
for CompressedModulusSwitchedMultiBitLweCiphertext<Scalar>
{
type ParameterSet = LweCiphertextParameters<u64>;
type ParameterSet = LweCiphertextParameters<Scalar>;

fn is_conformant(&self, lwe_ct_parameters: &LweCiphertextParameters<u64>) -> bool {
let lwe_dim = self.lwe_dimension.0;

let number_mask_bits_to_pack = lwe_dim * self.packed_mask.log_modulus.0;
fn is_conformant(&self, lwe_ct_parameters: &LweCiphertextParameters<Scalar>) -> bool {
let Self {
body,
packed_mask,
packed_diffs,
lwe_dimension,
uncompressed_ciphertext_modulus,
grouping_factor,
} = self;

let len = number_mask_bits_to_pack.div_ceil(Scalar::BITS);
let lwe_dim = lwe_dimension.0;

self.body >> self.packed_mask.log_modulus.0 == 0
&& self.packed_mask.packed_coeffs.len() == len
&& self.lwe_dimension == lwe_ct_parameters.lwe_dim
body >> packed_mask.log_modulus.0 == 0
&& packed_mask.is_conformant(&lwe_dim)
&& packed_diffs
.as_ref()
.map_or(true, |packed_diffs| packed_diffs.is_conformant(&lwe_dim))
&& *lwe_dimension == lwe_ct_parameters.lwe_dim
&& lwe_ct_parameters.ct_modulus.is_power_of_two()
&& match lwe_ct_parameters.ms_decompression_method {
MsDecompressionType::ClassicPbs => false,
MsDecompressionType::MultiBitPbs(expected_gouping_factor) => {
expected_gouping_factor.0 == self.grouping_factor.0
expected_gouping_factor.0 == grouping_factor.0
}
}
&& *uncompressed_ciphertext_modulus == lwe_ct_parameters.ct_modulus
}
}
16 changes: 13 additions & 3 deletions tfhe/src/core_crypto/entities/glwe_ciphertext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,9 +643,19 @@ where
&self,
glwe_ct_parameters: &GlweCiphertextConformanceParameters<C::Element>,
) -> bool {
let Self {
data,
polynomial_size,
ciphertext_modulus,
} = self;

check_encrypted_content_respects_mod(self, glwe_ct_parameters.ct_modulus)
&& self.glwe_size() == glwe_ct_parameters.glwe_dim.to_glwe_size()
&& self.polynomial_size() == glwe_ct_parameters.polynomial_size
&& self.ciphertext_modulus() == glwe_ct_parameters.ct_modulus
&& data.container_len()
== glwe_ciphertext_size(
glwe_ct_parameters.glwe_dim.to_glwe_size(),
glwe_ct_parameters.polynomial_size,
)
&& *polynomial_size == glwe_ct_parameters.polynomial_size
&& *ciphertext_modulus == glwe_ct_parameters.ct_modulus
}
}
9 changes: 7 additions & 2 deletions tfhe/src/core_crypto/entities/lwe_ciphertext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,9 +761,14 @@ where
type ParameterSet = LweCiphertextParameters<C::Element>;

fn is_conformant(&self, lwe_ct_parameters: &LweCiphertextParameters<C::Element>) -> bool {
check_encrypted_content_respects_mod(self, lwe_ct_parameters.ct_modulus)
let Self {
data,
ciphertext_modulus,
} = self;

check_encrypted_content_respects_mod(data, lwe_ct_parameters.ct_modulus)
&& self.lwe_size() == lwe_ct_parameters.lwe_dim.to_lwe_size()
&& self.ciphertext_modulus() == lwe_ct_parameters.ct_modulus
&& *ciphertext_modulus == lwe_ct_parameters.ct_modulus
}
}

Expand Down
19 changes: 13 additions & 6 deletions tfhe/src/core_crypto/entities/lwe_compact_ciphertext_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,17 +343,24 @@ impl<T: UnsignedInteger> ParameterSetConformant for LweCompactCiphertextListOwne
type ParameterSet = LweCiphertextListParameters<T>;

fn is_conformant(&self, param: &LweCiphertextListParameters<T>) -> bool {
let Self {
data,
lwe_size,
lwe_ciphertext_count,
ciphertext_modulus,
} = self;

param
.lwe_ciphertext_count_constraint
.is_valid(self.lwe_ciphertext_count.0)
&& self.data.len()
.is_valid(lwe_ciphertext_count.0)
&& data.len()
== lwe_compact_ciphertext_list_size(
self.lwe_size.to_lwe_dimension(),
self.lwe_ciphertext_count,
lwe_size.to_lwe_dimension(),
*lwe_ciphertext_count,
)
&& check_encrypted_content_respects_mod(self, param.ct_modulus)
&& self.lwe_size == param.lwe_dim.to_lwe_size()
&& self.ciphertext_modulus == param.ct_modulus
&& *lwe_size == param.lwe_dim.to_lwe_size()
&& *ciphertext_modulus == param.ct_modulus
}
}

Expand Down
19 changes: 19 additions & 0 deletions tfhe/src/core_crypto/entities/packed_integers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use tfhe_versionable::Versionize;

use crate::conformance::ParameterSetConformant;
use crate::core_crypto::backward_compatibility::entities::packed_integers::PackedIntegersVersions;
use crate::core_crypto::prelude::*;

Expand Down Expand Up @@ -166,3 +167,21 @@ impl<Scalar: UnsignedInteger> PackedIntegers<Scalar> {
})
}
}

impl<Scalar: UnsignedInteger> ParameterSetConformant for PackedIntegers<Scalar> {
type ParameterSet = usize;

fn is_conformant(&self, len: &usize) -> bool {
let Self {
packed_coeffs,
log_modulus,
initial_len,
} = self;

let number_packed_bits = *len * log_modulus.0;

let packed_len = number_packed_bits.div_ceil(Scalar::BITS);

*len == *initial_len && packed_coeffs.len() == packed_len
}
}
13 changes: 10 additions & 3 deletions tfhe/src/core_crypto/entities/seeded_lwe_ciphertext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ impl<T: UnsignedInteger> ParameterSetConformant for SeededLweCiphertext<T> {
type ParameterSet = LweCiphertextParameters<T>;

fn is_conformant(&self, lwe_ct_parameters: &LweCiphertextParameters<T>) -> bool {
let Self {
data,
lwe_size,
compression_seed: _,
ciphertext_modulus,
} = self;

check_encrypted_content_respects_mod::<T, &[T]>(
&std::slice::from_ref(self.get_body().data),
&std::slice::from_ref(data),
lwe_ct_parameters.ct_modulus,
) && self.lwe_size == lwe_ct_parameters.lwe_dim.to_lwe_size()
&& self.ciphertext_modulus() == lwe_ct_parameters.ct_modulus
) && *lwe_size == lwe_ct_parameters.lwe_dim.to_lwe_size()
&& *ciphertext_modulus == lwe_ct_parameters.ct_modulus
}
}

Expand Down
6 changes: 5 additions & 1 deletion tfhe/src/high_level_api/booleans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ impl ParameterSetConformant for FheBool {
type ParameterSet = FheBoolConformanceParams;

fn is_conformant(&self, params: &FheBoolConformanceParams) -> bool {
self.ciphertext.on_cpu().0.is_conformant(&params.0)
let Self { ciphertext } = self;

let BooleanBlock(block) = &*ciphertext.on_cpu();

block.is_conformant(&params.0)
}
}

Expand Down
4 changes: 3 additions & 1 deletion tfhe/src/high_level_api/compact_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ impl ParameterSetConformant for CompactCiphertextList {
type ParameterSet = CompactCiphertextListConformanceParams;

fn is_conformant(&self, parameter_set: &Self::ParameterSet) -> bool {
self.0.is_conformant(parameter_set)
let Self(list) = self;

list.is_conformant(parameter_set)
}
}

Expand Down
4 changes: 3 additions & 1 deletion tfhe/src/high_level_api/integers/signed/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ impl<Id: FheIntId> ParameterSetConformant for FheInt<Id> {
type ParameterSet = FheIntConformanceParams<Id>;

fn is_conformant(&self, params: &FheIntConformanceParams<Id>) -> bool {
self.ciphertext.on_cpu().is_conformant(&params.params)
let Self { ciphertext, id: _ } = self;

ciphertext.on_cpu().is_conformant(&params.params)
}
}

Expand Down
4 changes: 3 additions & 1 deletion tfhe/src/high_level_api/integers/signed/compressed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ impl<Id: FheIntId> ParameterSetConformant for CompressedFheInt<Id> {
type ParameterSet = FheIntConformanceParams<Id>;

fn is_conformant(&self, params: &FheIntConformanceParams<Id>) -> bool {
self.ciphertext.is_conformant(&params.params)
let Self { ciphertext, id: _ } = self;

ciphertext.is_conformant(&params.params)
}
}

Expand Down
4 changes: 3 additions & 1 deletion tfhe/src/high_level_api/integers/unsigned/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ impl<Id: FheUintId> ParameterSetConformant for FheUint<Id> {
type ParameterSet = FheUintConformanceParams<Id>;

fn is_conformant(&self, params: &FheUintConformanceParams<Id>) -> bool {
self.ciphertext.on_cpu().is_conformant(&params.params)
let Self { ciphertext, id: _ } = self;

ciphertext.on_cpu().is_conformant(&params.params)
}
}

Expand Down
4 changes: 3 additions & 1 deletion tfhe/src/high_level_api/integers/unsigned/compressed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ impl<Id: FheUintId> ParameterSetConformant for CompressedFheUint<Id> {
type ParameterSet = FheUintConformanceParams<Id>;

fn is_conformant(&self, params: &FheUintConformanceParams<Id>) -> bool {
self.ciphertext.is_conformant(&params.params)
let Self { ciphertext, id: _ } = self;

ciphertext.is_conformant(&params.params)
}
}

Expand Down
14 changes: 8 additions & 6 deletions tfhe/src/integer/ciphertext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ impl<T: ParameterSetConformant<ParameterSet = CiphertextConformanceParams>> Para
type ParameterSet = RadixCiphertextConformanceParams;

fn is_conformant(&self, params: &RadixCiphertextConformanceParams) -> bool {
self.blocks.len() == params.num_blocks_per_integer
&& self
.blocks
let Self { blocks } = self;

blocks.len() == params.num_blocks_per_integer
&& blocks
.iter()
.all(|block| block.is_conformant(&params.shortint_params))
}
Expand Down Expand Up @@ -137,9 +138,10 @@ impl<T: ParameterSetConformant<ParameterSet = CiphertextConformanceParams>> Para
type ParameterSet = RadixCiphertextConformanceParams;

fn is_conformant(&self, params: &RadixCiphertextConformanceParams) -> bool {
self.blocks.len() == params.num_blocks_per_integer
&& self
.blocks
let Self { blocks } = self;

blocks.len() == params.num_blocks_per_integer
&& blocks
.iter()
.all(|block| block.is_conformant(&params.shortint_params))
}
Expand Down
10 changes: 7 additions & 3 deletions tfhe/src/integer/ciphertext/compact_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,9 @@ impl ParameterSetConformant for CompactCiphertextList {
type ParameterSet = CompactCiphertextListConformanceParams;

fn is_conformant(&self, params: &CompactCiphertextListConformanceParams) -> bool {
if !params.num_elements_constraint.is_valid(self.info.len()) {
let Self { ct_list: _, info } = self;

if !params.num_elements_constraint.is_valid(info.len()) {
return false;
}

Expand Down Expand Up @@ -520,7 +522,9 @@ impl CompactCiphertextList {
&self,
shortint_params: CiphertextConformanceParams,
) -> bool {
let mut num_blocks: usize = self.info.iter().copied().map(DataKind::num_blocks).sum();
let Self { ct_list, info } = self;

let mut num_blocks: usize = info.iter().copied().map(DataKind::num_blocks).sum();
// This expects packing, halve the number of blocks with enough capacity
if shortint_params.degree.get()
== (shortint_params.message_modulus.0 * shortint_params.carry_modulus.0) - 1
Expand All @@ -529,7 +533,7 @@ impl CompactCiphertextList {
}
let shortint_list_params = shortint_params
.to_ct_list_conformance_parameters(ListSizeConstraint::exact_size(num_blocks));
self.ct_list.is_conformant(&shortint_list_params)
ct_list.is_conformant(&shortint_list_params)
}
}

Expand Down
Loading

0 comments on commit 11a8f97

Please sign in to comment.