From 78e2a6e1b3bd5fd8d5b59ac2b4fab84789e012ad Mon Sep 17 00:00:00 2001 From: "Mayeul@Zama" Date: Fri, 10 Mar 2023 15:33:45 +0100 Subject: [PATCH] chore(cpu): separate GlevCt --- .../implementation/types/glev_ciphertext.rs | 74 +++++++++++++++++++ .../src/implementation/types/mod.rs | 1 + .../types/packing_keyswitch_key.rs | 73 +----------------- 3 files changed, 76 insertions(+), 72 deletions(-) create mode 100644 backends/concrete-cpu/src/implementation/types/glev_ciphertext.rs diff --git a/backends/concrete-cpu/src/implementation/types/glev_ciphertext.rs b/backends/concrete-cpu/src/implementation/types/glev_ciphertext.rs new file mode 100644 index 000000000..f5836fbcf --- /dev/null +++ b/backends/concrete-cpu/src/implementation/types/glev_ciphertext.rs @@ -0,0 +1,74 @@ +use super::*; +use crate::implementation::{Container, ContainerMut, Split}; + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[readonly::make] +pub struct GlevCiphertext { + pub data: C, + pub glwe_params: GlweParams, + pub ciphertext_count: usize, +} + +impl GlevCiphertext { + pub fn data_len(glwe_params: GlweParams, ciphertext_count: usize) -> usize { + glwe_params.polynomial_size * (glwe_params.dimension + 1) * ciphertext_count + } + + pub fn new(data: C, glwe_params: GlweParams, ciphertext_count: usize) -> Self { + debug_assert_eq!(data.len(), Self::data_len(glwe_params, ciphertext_count)); + + Self { + data, + glwe_params, + ciphertext_count, + } + } + + pub unsafe fn from_raw_parts( + data: C::Pointer, + glwe_params: GlweParams, + ciphertext_count: usize, + ) -> Self + where + C: Split, + { + let data = C::from_raw_parts(data, Self::data_len(glwe_params, ciphertext_count)); + Self { + data, + glwe_params, + ciphertext_count, + } + } + + pub fn as_view(&self) -> GlevCiphertext<&[C::Item]> { + GlevCiphertext { + data: self.data.as_ref(), + glwe_params: self.glwe_params, + ciphertext_count: self.ciphertext_count, + } + } + + pub fn as_mut_view(&mut self) -> GlevCiphertext<&mut [C::Item]> + where + C: ContainerMut, + { + GlevCiphertext { + data: self.data.as_mut(), + glwe_params: self.glwe_params, + 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| GlweCiphertext::new(slice, self.glwe_params)) + } +} diff --git a/backends/concrete-cpu/src/implementation/types/mod.rs b/backends/concrete-cpu/src/implementation/types/mod.rs index a77a47c9f..cc7339d7b 100644 --- a/backends/concrete-cpu/src/implementation/types/mod.rs +++ b/backends/concrete-cpu/src/implementation/types/mod.rs @@ -51,4 +51,5 @@ mod csprng; pub use csprng::*; pub mod ciphertext_list; +pub mod glev_ciphertext; pub mod polynomial_list; diff --git a/backends/concrete-cpu/src/implementation/types/packing_keyswitch_key.rs b/backends/concrete-cpu/src/implementation/types/packing_keyswitch_key.rs index 1d96cfaf3..2e783eb3e 100644 --- a/backends/concrete-cpu/src/implementation/types/packing_keyswitch_key.rs +++ b/backends/concrete-cpu/src/implementation/types/packing_keyswitch_key.rs @@ -1,3 +1,4 @@ +use super::glev_ciphertext::GlevCiphertext; use super::*; use crate::implementation::{Container, ContainerMut, Split}; @@ -10,14 +11,6 @@ pub struct PackingKeyswitchKey { pub decomp_params: DecompParams, } -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -#[readonly::make] -pub struct GlevCiphertext { - pub data: C, - pub glwe_params: GlweParams, - pub ciphertext_count: usize, -} - impl PackingKeyswitchKey { pub fn data_len( glwe_params: GlweParams, @@ -105,67 +98,3 @@ impl PackingKeyswitchKey { }) } } - -impl GlevCiphertext { - pub fn data_len(glwe_params: GlweParams, ciphertext_count: usize) -> usize { - glwe_params.polynomial_size * (glwe_params.dimension + 1) * ciphertext_count - } - - pub fn new(data: C, glwe_params: GlweParams, ciphertext_count: usize) -> Self { - debug_assert_eq!(data.len(), Self::data_len(glwe_params, ciphertext_count)); - - Self { - data, - glwe_params, - ciphertext_count, - } - } - - pub unsafe fn from_raw_parts( - data: C::Pointer, - glwe_params: GlweParams, - ciphertext_count: usize, - ) -> Self - where - C: Split, - { - let data = C::from_raw_parts(data, Self::data_len(glwe_params, ciphertext_count)); - Self { - data, - glwe_params, - ciphertext_count, - } - } - - pub fn as_view(&self) -> GlevCiphertext<&[C::Item]> { - GlevCiphertext { - data: self.data.as_ref(), - glwe_params: self.glwe_params, - ciphertext_count: self.ciphertext_count, - } - } - - pub fn as_mut_view(&mut self) -> GlevCiphertext<&mut [C::Item]> - where - C: ContainerMut, - { - GlevCiphertext { - data: self.data.as_mut(), - glwe_params: self.glwe_params, - 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| GlweCiphertext::new(slice, self.glwe_params)) - } -}