chore(cpu): separate GlevCt

This commit is contained in:
Mayeul@Zama
2023-03-10 15:33:45 +01:00
committed by mayeul-zama
parent a2b143f409
commit 78e2a6e1b3
3 changed files with 76 additions and 72 deletions

View File

@@ -0,0 +1,74 @@
use super::*;
use crate::implementation::{Container, ContainerMut, Split};
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[readonly::make]
pub struct GlevCiphertext<C: Container> {
pub data: C,
pub glwe_params: GlweParams,
pub ciphertext_count: usize,
}
impl<C: Container> GlevCiphertext<C> {
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<Item = GlweCiphertext<C>>
where
C: Split,
{
self.data
.split_into(self.ciphertext_count)
.map(move |slice| GlweCiphertext::new(slice, self.glwe_params))
}
}

View File

@@ -51,4 +51,5 @@ mod csprng;
pub use csprng::*;
pub mod ciphertext_list;
pub mod glev_ciphertext;
pub mod polynomial_list;

View File

@@ -1,3 +1,4 @@
use super::glev_ciphertext::GlevCiphertext;
use super::*;
use crate::implementation::{Container, ContainerMut, Split};
@@ -10,14 +11,6 @@ pub struct PackingKeyswitchKey<C: Container> {
pub decomp_params: DecompParams,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[readonly::make]
pub struct GlevCiphertext<C: Container> {
pub data: C,
pub glwe_params: GlweParams,
pub ciphertext_count: usize,
}
impl<C: Container> PackingKeyswitchKey<C> {
pub fn data_len(
glwe_params: GlweParams,
@@ -105,67 +98,3 @@ impl<C: Container> PackingKeyswitchKey<C> {
})
}
}
impl<C: Container> GlevCiphertext<C> {
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<Item = GlweCiphertext<C>>
where
C: Split,
{
self.data
.split_into(self.ciphertext_count)
.map(move |slice| GlweCiphertext::new(slice, self.glwe_params))
}
}