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 22, 2024
1 parent 358bcc9 commit 88239f0
Show file tree
Hide file tree
Showing 18 changed files with 164 additions and 71 deletions.
4 changes: 4 additions & 0 deletions tfhe/src/core_crypto/commons/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ impl GlweSize {
pub fn to_glwe_dimension(&self) -> GlweDimension {
GlweDimension(self.0 - 1)
}

pub const fn to_glwe_ct_size(self, poly_size: PolynomialSize) -> usize {
self.0 * poly_size.0
}
}

/// The number of polynomials of a GLWE mask, or the size of a GLWE secret key.
Expand Down
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_ct_parameters
.glwe_dim
.to_glwe_size()
.to_glwe_ct_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 LweCompactCiphertextListOwned {
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
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ impl ParameterSetConformant for CompressedModulusSwitchedRadixCiphertext {
type ParameterSet = RadixCiphertextConformanceParams;

fn is_conformant(&self, params: &RadixCiphertextConformanceParams) -> bool {
self.0.is_conformant(params)
let Self(ct) = self;

ct.is_conformant(params)
}
}

Expand Down Expand Up @@ -86,7 +88,9 @@ impl ParameterSetConformant for CompressedModulusSwitchedSignedRadixCiphertext {
type ParameterSet = RadixCiphertextConformanceParams;

fn is_conformant(&self, params: &RadixCiphertextConformanceParams) -> bool {
self.0.is_conformant(params)
let Self(ct) = self;

ct.is_conformant(params)
}
}

Expand All @@ -101,6 +105,11 @@ impl ParameterSetConformant for CompressedModulusSwitchedRadixCiphertextGeneric
type ParameterSet = RadixCiphertextConformanceParams;

fn is_conformant(&self, params: &RadixCiphertextConformanceParams) -> bool {
let Self {
paired_blocks,
last_block,
} = self;

let mut shortint_params = params.shortint_params;

shortint_params.degree = Degree::new(
Expand All @@ -111,15 +120,14 @@ impl ParameterSetConformant for CompressedModulusSwitchedRadixCiphertextGeneric
.get(),
);

let paired_blocks_len_ok = self.paired_blocks.len() == params.num_blocks_per_integer / 2;
let paired_blocks_len_ok = paired_blocks.len() == params.num_blocks_per_integer / 2;

let paired_blocks_ok = self
.paired_blocks
let paired_blocks_ok = paired_blocks
.iter()
.all(|block| block.is_conformant(&shortint_params));

let last_item_ok = if params.num_blocks_per_integer % 2 == 1 {
self.last_block.as_ref().map_or(false, |last_block| {
last_block.as_ref().map_or(false, |last_block| {
last_block.is_conformant(&params.shortint_params)
})
} else {
Expand Down
2 changes: 2 additions & 0 deletions tfhe/src/shortint/ciphertext/compact_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl ParameterSetConformant for CompactCiphertextList {
expansion_kind,
noise_level,
} = self;

let CiphertextListConformanceParams {
ct_list_params,
message_modulus: param_message_modulus,
Expand All @@ -47,6 +48,7 @@ impl ParameterSetConformant for CompactCiphertextList {
noise_level: param_noise_level,
expansion_kind: param_expansion_kind,
} = param;

ct_list.is_conformant(ct_list_params)
&& *message_modulus == *param_message_modulus
&& *carry_modulus == *param_carry_modulus
Expand Down
21 changes: 15 additions & 6 deletions tfhe/src/shortint/ciphertext/compressed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,21 @@ impl ParameterSetConformant for CompressedCiphertext {
type ParameterSet = CiphertextConformanceParams;

fn is_conformant(&self, param: &CiphertextConformanceParams) -> bool {
self.ct.is_conformant(&param.ct_params)
&& self.message_modulus == param.message_modulus
&& self.carry_modulus == param.carry_modulus
&& self.pbs_order == param.pbs_order
&& self.degree == param.degree
&& self.noise_level == param.noise_level
let Self {
ct,
degree,
message_modulus,
carry_modulus,
pbs_order,
noise_level,
} = self;

ct.is_conformant(&param.ct_params)
&& *message_modulus == param.message_modulus
&& *carry_modulus == param.carry_modulus
&& *pbs_order == param.pbs_order
&& *degree == param.degree
&& *noise_level == param.noise_level
}
}

Expand Down
55 changes: 31 additions & 24 deletions tfhe/src/shortint/ciphertext/compressed_ciphertext_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,44 @@ impl ParameterSetConformant for CompressedCiphertextList {
type ParameterSet = CompressedCiphertextConformanceParams;

fn is_conformant(&self, params: &CompressedCiphertextConformanceParams) -> bool {
let len = self.modulus_switched_glwe_ciphertext_list.len();
let Self {
modulus_switched_glwe_ciphertext_list,
ciphertext_modulus,
message_modulus,
carry_modulus,
pbs_order,
lwe_per_glwe,
count,
} = self;

let len = modulus_switched_glwe_ciphertext_list.len();

if len == 0 {
return true;
}

let count_is_ok = self.modulus_switched_glwe_ciphertext_list[..len - 1]
.iter()
.all(|a| a.bodies_count() == params.lwe_per_glwe)
&& self
.modulus_switched_glwe_ciphertext_list
.last()
.unwrap()
.bodies_count()
.0
<= params.lwe_per_glwe.0;
let last_body_count = modulus_switched_glwe_ciphertext_list
.last()
.unwrap()
.bodies_count()
.0;

let count_is_ok = count.0.div_ceil(lwe_per_glwe.0) == len
&& modulus_switched_glwe_ciphertext_list[..len - 1]
.iter()
.all(|a| a.bodies_count() == params.lwe_per_glwe)
&& last_body_count <= params.lwe_per_glwe.0
&& (len - 1) * params.lwe_per_glwe.0 + last_body_count == count.0;

count_is_ok
&& self
.modulus_switched_glwe_ciphertext_list
&& modulus_switched_glwe_ciphertext_list
.iter()
.all(|glwe| {
glwe.glwe_dimension() == params.ct_params.glwe_dim
&& glwe.polynomial_size() == params.ct_params.polynomial_size
&& glwe.uncompressed_ciphertext_modulus() == params.ct_params.ct_modulus
})
&& self.lwe_per_glwe.0 <= params.ct_params.polynomial_size.0
&& self.lwe_per_glwe == params.lwe_per_glwe
&& self.ciphertext_modulus == params.ct_params.ct_modulus
&& self.message_modulus == params.message_modulus
&& self.carry_modulus == params.carry_modulus
&& self.pbs_order == params.pbs_order
.all(|glwe| glwe.is_conformant(&params.ct_params))
&& lwe_per_glwe.0 <= params.ct_params.polynomial_size.0
&& *lwe_per_glwe == params.lwe_per_glwe
&& *ciphertext_modulus == params.ct_params.ct_modulus
&& *message_modulus == params.message_modulus
&& *carry_modulus == params.carry_modulus
&& *pbs_order == params.pbs_order
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,19 @@ impl ParameterSetConformant for CompressedModulusSwitchedCiphertext {
type ParameterSet = CiphertextConformanceParams;

fn is_conformant(&self, param: &CiphertextConformanceParams) -> bool {
self.compressed_modulus_switched_lwe_ciphertext
.is_conformant(&param.ct_params)
&& self.message_modulus == param.message_modulus
&& self.carry_modulus == param.carry_modulus
&& self.pbs_order == param.pbs_order
&& self.degree == param.degree
let Self {
compressed_modulus_switched_lwe_ciphertext,
degree,
message_modulus,
carry_modulus,
pbs_order,
} = self;

compressed_modulus_switched_lwe_ciphertext.is_conformant(&param.ct_params)
&& *message_modulus == param.message_modulus
&& *carry_modulus == param.carry_modulus
&& *pbs_order == param.pbs_order
&& *degree == param.degree
}
}

Expand Down
Loading

0 comments on commit 88239f0

Please sign in to comment.