diff --git a/backends/concrete-cpu/src/implementation/types/keyswitch_key.rs b/backends/concrete-cpu/src/implementation/types/keyswitch_key.rs index 49f670f57..1330b87af 100644 --- a/backends/concrete-cpu/src/implementation/types/keyswitch_key.rs +++ b/backends/concrete-cpu/src/implementation/types/keyswitch_key.rs @@ -1,3 +1,4 @@ +use super::lev_ciphertext::LevCiphertext; use super::*; use crate::implementation::{Container, ContainerMut, Split}; @@ -10,14 +11,6 @@ pub struct LweKeyswitchKey { pub decomp_params: DecompParams, } -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -#[readonly::make] -pub struct LevCiphertext { - pub data: C, - pub lwe_dimension: usize, - pub ciphertext_count: usize, -} - impl LweKeyswitchKey { pub fn data_len( output_dimension: usize, @@ -102,66 +95,3 @@ impl LweKeyswitchKey { }) } } - -impl LevCiphertext { - pub fn data_len(lwe_dimension: usize, ciphertext_count: usize) -> usize { - ciphertext_count * (lwe_dimension + 1) - } - - pub fn new(data: C, lwe_dimension: usize, ciphertext_count: usize) -> Self { - debug_assert_eq!(data.len(), Self::data_len(lwe_dimension, ciphertext_count)); - - Self { - data, - lwe_dimension, - ciphertext_count, - } - } - - pub unsafe fn from_raw_parts( - data: C::Pointer, - lwe_dimension: usize, - ciphertext_count: usize, - ) -> Self - where - C: Split, - { - Self { - data: C::from_raw_parts(data, Self::data_len(lwe_dimension, ciphertext_count)), - lwe_dimension, - ciphertext_count, - } - } - - pub fn as_view(&self) -> LevCiphertext<&[C::Item]> { - LevCiphertext { - data: self.data.as_ref(), - lwe_dimension: self.lwe_dimension, - ciphertext_count: self.ciphertext_count, - } - } - - pub fn as_mut_view(&mut self) -> LevCiphertext<&mut [C::Item]> - where - C: ContainerMut, - { - LevCiphertext { - data: self.data.as_mut(), - lwe_dimension: self.lwe_dimension, - ciphertext_count: self.ciphertext_count, - } - } - - pub fn into_data(self) -> C { - self.data - } - - pub fn into_ciphertext_iter(self) -> impl DoubleEndedIterator> - where - C: Split, - { - self.data - .split_into(self.ciphertext_count) - .map(move |slice| LweCiphertext::new(slice, self.lwe_dimension)) - } -} diff --git a/backends/concrete-cpu/src/implementation/types/lev_ciphertext.rs b/backends/concrete-cpu/src/implementation/types/lev_ciphertext.rs new file mode 100644 index 000000000..087ca60ec --- /dev/null +++ b/backends/concrete-cpu/src/implementation/types/lev_ciphertext.rs @@ -0,0 +1,73 @@ +use super::*; +use crate::implementation::{Container, ContainerMut, Split}; + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[readonly::make] +pub struct LevCiphertext { + pub data: C, + pub lwe_dimension: usize, + pub ciphertext_count: usize, +} + +impl LevCiphertext { + pub fn data_len(lwe_dimension: usize, ciphertext_count: usize) -> usize { + ciphertext_count * (lwe_dimension + 1) + } + + pub fn new(data: C, lwe_dimension: usize, ciphertext_count: usize) -> Self { + debug_assert_eq!(data.len(), Self::data_len(lwe_dimension, ciphertext_count)); + + Self { + data, + lwe_dimension, + ciphertext_count, + } + } + + pub unsafe fn from_raw_parts( + data: C::Pointer, + lwe_dimension: usize, + ciphertext_count: usize, + ) -> Self + where + C: Split, + { + Self { + data: C::from_raw_parts(data, Self::data_len(lwe_dimension, ciphertext_count)), + lwe_dimension, + ciphertext_count, + } + } + + pub fn as_view(&self) -> LevCiphertext<&[C::Item]> { + LevCiphertext { + data: self.data.as_ref(), + lwe_dimension: self.lwe_dimension, + ciphertext_count: self.ciphertext_count, + } + } + + pub fn as_mut_view(&mut self) -> LevCiphertext<&mut [C::Item]> + where + C: ContainerMut, + { + LevCiphertext { + data: self.data.as_mut(), + lwe_dimension: self.lwe_dimension, + ciphertext_count: self.ciphertext_count, + } + } + + pub fn into_data(self) -> C { + self.data + } + + pub fn into_ciphertext_iter(self) -> impl DoubleEndedIterator> + where + C: Split, + { + self.data + .split_into(self.ciphertext_count) + .map(move |slice| LweCiphertext::new(slice, self.lwe_dimension)) + } +} diff --git a/backends/concrete-cpu/src/implementation/types/mod.rs b/backends/concrete-cpu/src/implementation/types/mod.rs index c9821a2c5..d531f24df 100644 --- a/backends/concrete-cpu/src/implementation/types/mod.rs +++ b/backends/concrete-cpu/src/implementation/types/mod.rs @@ -52,5 +52,6 @@ pub use csprng::*; pub mod ciphertext_list; pub mod glev_ciphertext; +pub mod lev_ciphertext; pub mod polynomial; pub mod polynomial_list;