diff --git a/backends/concrete-cpu/src/implementation/encrypt/mod.rs b/backends/concrete-cpu/src/implementation/encrypt/mod.rs index 635a6f005..7774e9a88 100644 --- a/backends/concrete-cpu/src/implementation/encrypt/mod.rs +++ b/backends/concrete-cpu/src/implementation/encrypt/mod.rs @@ -505,9 +505,7 @@ impl GlweSecretKey<&[u64]> { let mask = mask.as_view(); - for idx in 0..mask.glwe_params.dimension { - let poly = mask.get_polynomial(idx); - let bin_poly = self.get_polynomial(idx); + for (poly, bin_poly) in zip_eq(mask.iter_polynomial(), self.iter()) { let body = body.as_mut_view(); update_with_wrapping_add_mul(body, poly, bin_poly) } @@ -517,9 +515,7 @@ impl GlweSecretKey<&[u64]> { let mask = mask.as_view(); out.copy_from_slice(body.into_data()); - for idx in 0..mask.glwe_params.dimension { - let poly = mask.get_polynomial(idx); - let bin_poly = self.get_polynomial(idx); + for (poly, bin_poly) in zip_eq(mask.iter_polynomial(), self.iter()) { update_with_wrapping_sub_mul( Polynomial::new(out, encrypted.glwe_params.polynomial_size), poly, @@ -616,9 +612,7 @@ impl GlweSecretKey<&[u64]> { let (mask, mut body) = encrypted.into_mask_and_body(); let mask = mask.as_view(); - for idx in 0..mask.glwe_params.dimension { - let poly = mask.get_polynomial(idx); - let bin_poly = self.get_polynomial(idx); + for (poly, bin_poly) in zip_eq(mask.iter_polynomial(), self.iter()) { update_with_wrapping_add_mul(body.as_mut_view(), poly, bin_poly) } } diff --git a/backends/concrete-cpu/src/implementation/types/glwe_ciphertext.rs b/backends/concrete-cpu/src/implementation/types/glwe_ciphertext.rs index 256b13036..60fade55c 100644 --- a/backends/concrete-cpu/src/implementation/types/glwe_ciphertext.rs +++ b/backends/concrete-cpu/src/implementation/types/glwe_ciphertext.rs @@ -1,8 +1,7 @@ -use crate::implementation::{zip_eq, Container, ContainerMut, Split}; - use super::polynomial::Polynomial; use super::polynomial_list::PolynomialList; use super::GlweParams; +use crate::implementation::{zip_eq, Container, ContainerMut, Split}; #[derive(Copy, Clone, Debug, PartialEq, Eq)] #[readonly::make] @@ -11,13 +10,6 @@ pub struct GlweCiphertext { pub glwe_params: GlweParams, } -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -#[readonly::make] -pub struct GlweMask { - pub data: C, - pub glwe_params: GlweParams, -} - impl GlweCiphertext { pub fn data_len(glwe_params: GlweParams) -> usize { glwe_params.polynomial_size * (glwe_params.dimension + 1) @@ -67,7 +59,7 @@ impl GlweCiphertext { self.data } - pub fn into_mask_and_body(self) -> (GlweMask, Polynomial) + pub fn into_mask_and_body(self) -> (PolynomialList, Polynomial) where C: Split, { @@ -76,10 +68,11 @@ impl GlweCiphertext { .split_at(self.glwe_params.polynomial_size * self.glwe_params.dimension); ( - GlweMask { - data: mask, - glwe_params: self.glwe_params, - }, + PolynomialList::new( + mask, + self.glwe_params.polynomial_size, + self.glwe_params.dimension, + ), Polynomial::new(body, self.glwe_params.polynomial_size), ) } @@ -102,56 +95,3 @@ impl GlweCiphertext<&mut [u64]> { } } } - -impl GlweMask { - pub fn data_len(glwe_params: GlweParams) -> usize { - glwe_params.polynomial_size * glwe_params.dimension - } - - pub fn new(data: C, glwe_params: GlweParams) -> Self { - debug_assert_eq!(data.len(), Self::data_len(glwe_params)); - Self { data, glwe_params } - } - - pub unsafe fn from_raw_parts(data: C::Pointer, glwe_params: GlweParams) -> Self - where - C: Split, - { - let data = C::from_raw_parts(data, Self::data_len(glwe_params)); - Self { data, glwe_params } - } - - pub fn as_view(&self) -> GlweMask<&[C::Item]> { - GlweMask { - data: self.data.as_ref(), - glwe_params: self.glwe_params, - } - } - - pub fn as_mut_view(&mut self) -> GlweMask<&mut [C::Item]> - where - C: ContainerMut, - { - GlweMask { - data: self.data.as_mut(), - glwe_params: self.glwe_params, - } - } - - pub fn into_data(self) -> C { - self.data - } - - pub fn get_polynomial(self, idx: usize) -> Polynomial - where - C: Split, - { - Polynomial::new( - self.data.chunk( - idx * self.glwe_params.polynomial_size, - (idx + 1) * self.glwe_params.polynomial_size, - ), - self.glwe_params.polynomial_size, - ) - } -} diff --git a/backends/concrete-cpu/src/implementation/types/polynomial_list.rs b/backends/concrete-cpu/src/implementation/types/polynomial_list.rs index 6188d6442..26c25dffb 100644 --- a/backends/concrete-cpu/src/implementation/types/polynomial_list.rs +++ b/backends/concrete-cpu/src/implementation/types/polynomial_list.rs @@ -22,6 +22,10 @@ impl PolynomialList { fn container_len(&self) -> usize { self.data.len() } + + pub fn into_data(self) -> C { + self.data + } } impl PolynomialList<&[u64]> { @@ -48,6 +52,13 @@ impl PolynomialList<&[u64]> { count, }) } + pub fn as_view(&self) -> PolynomialList<&[u64]> { + PolynomialList { + data: self.data, + count: self.count, + polynomial_size: self.polynomial_size, + } + } } impl PolynomialList<&mut [u64]> { @@ -58,4 +69,20 @@ impl PolynomialList<&mut [u64]> { .chunks_exact_mut(self.polynomial_size) .map(|a| Polynomial::new(a, self.polynomial_size)) } + + pub fn as_mut_view(&mut self) -> PolynomialList<&mut [u64]> { + PolynomialList { + data: self.data, + count: self.count, + polynomial_size: self.polynomial_size, + } + } + + pub fn as_view(&self) -> PolynomialList<&[u64]> { + PolynomialList { + data: self.data, + count: self.count, + polynomial_size: self.polynomial_size, + } + } } diff --git a/backends/concrete-cpu/src/implementation/wop.rs b/backends/concrete-cpu/src/implementation/wop.rs index ddf57b5a6..349b9b663 100644 --- a/backends/concrete-cpu/src/implementation/wop.rs +++ b/backends/concrete-cpu/src/implementation/wop.rs @@ -397,6 +397,10 @@ impl GlweCiphertextList { } } + pub fn into_data(self) -> C { + self.data + } + pub fn as_view(&self) -> GlweCiphertextListView<'_, C::Item> { GlweCiphertextListView { data: self.data.as_ref(),