diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 91f1856..f9549a5 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -25,7 +25,7 @@ impl GLWESwitchingKeyCompressed> { { (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) + ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into()) - + GLWESecretPrepared::alloc_bytes_with(module, infos.rank_out()) + + GLWESecretPrepared::alloc_bytes(module, infos.rank_out()) } } diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index ce10a51..5bea8d0 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -22,7 +22,7 @@ impl GLWESwitchingKey> { { (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) + ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into()) - + GLWESecretPrepared::alloc_bytes(module, &infos.glwe_layout()) + + GLWESecretPrepared::alloc_bytes_from_infos(module, &infos.glwe_layout()) } pub fn encrypt_pk_scratch_space(module: &Module, _infos: &A) -> usize diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 50a646a..330ca0c 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -23,7 +23,7 @@ impl TensorKey> { Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes, { - GLWESecretPrepared::alloc_bytes_with(module, infos.rank_out()) + GLWESecretPrepared::alloc_bytes(module, infos.rank_out()) + module.vec_znx_dft_alloc_bytes(infos.rank_out().into(), 1) + module.vec_znx_big_alloc_bytes(1, 1) + module.vec_znx_dft_alloc_bytes(1, 1) diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index aee218d..ebbd0f1 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -22,7 +22,7 @@ impl GLWEToLWESwitchingKey> { A: GGLWEInfos, Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - GLWESecretPrepared::alloc_bytes_with(module, infos.rank_in()) + GLWESecretPrepared::alloc_bytes(module, infos.rank_in()) + (GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) | GLWESecret::alloc_bytes_with(infos.n(), infos.rank_in())) } diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 4c8b866..4408e6e 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -39,7 +39,7 @@ impl LWESwitchingKey> { "rank_out > 1 is not supported for LWESwitchingKey" ); GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1)) - + GLWESecretPrepared::alloc_bytes_with(module, Rank(1)) + + GLWESecretPrepared::alloc_bytes(module, Rank(1)) + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) } } diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe_ct.rs index 576774d..93cb829 100644 --- a/poulpy-core/src/layouts/compressed/glwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/glwe_ct.rs @@ -6,9 +6,7 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{ - Base2K, BuildError, Degree, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::Decompress, -}; +use crate::layouts::{Base2K, Degree, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::Decompress}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -100,101 +98,6 @@ impl GLWECiphertextCompressed> { } } -pub struct GLWECiphertextCompressedBuilder { - data: Option>, - base2k: Option, - k: Option, - rank: Option, -} - -impl GLWECiphertextCompressed { - #[inline] - pub fn builder() -> GLWECiphertextCompressedBuilder { - GLWECiphertextCompressedBuilder { - data: None, - base2k: None, - k: None, - rank: None, - } - } -} - -impl GLWECiphertextCompressedBuilder> { - #[inline] - pub fn layout(mut self, layout: &A) -> Self - where - A: GLWEInfos, - { - self.data = Some(VecZnx::alloc(layout.n().into(), 1, layout.size())); - self.base2k = Some(layout.base2k()); - self.k = Some(layout.k()); - self.rank = Some(layout.rank()); - self - } -} - -impl GLWECiphertextCompressedBuilder { - #[inline] - pub fn data(mut self, data: VecZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - #[inline] - pub fn rank(mut self, rank: Rank) -> Self { - self.rank = Some(rank); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VecZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - let rank: Rank = self.rank.ok_or(BuildError::MissingK)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if rank == 0_u32 { - return Err(BuildError::ZeroRank); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() != 1 { - return Err(BuildError::VecZnxColsNotOne); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GLWECiphertextCompressed { - data, - base2k, - k, - rank, - seed: [0u8; 32], - }) - } -} - impl ReaderFrom for GLWECiphertextCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); @@ -282,13 +185,13 @@ pub trait GLWECiphertextCompressedToRef { impl GLWECiphertextCompressedToRef for GLWECiphertextCompressed { fn to_ref(&self) -> GLWECiphertextCompressed<&[u8]> { - GLWECiphertextCompressed::builder() - .k(self.k()) - .base2k(self.base2k()) - .rank(self.rank()) - .data(self.data.to_ref()) - .build() - .unwrap() + GLWECiphertextCompressed { + seed: self.seed.clone(), + base2k: self.base2k, + k: self.k, + rank: self.rank, + data: self.data.to_ref(), + } } } @@ -298,12 +201,12 @@ pub trait GLWECiphertextCompressedToMut { impl GLWECiphertextCompressedToMut for GLWECiphertextCompressed { fn to_mut(&mut self) -> GLWECiphertextCompressed<&mut [u8]> { - GLWECiphertextCompressed::builder() - .k(self.k()) - .base2k(self.base2k()) - .rank(self.rank()) - .data(self.data.to_mut()) - .build() - .unwrap() + GLWECiphertextCompressed { + seed: self.seed.clone(), + base2k: self.base2k, + k: self.k, + rank: self.rank, + data: self.data.to_mut(), + } } } diff --git a/poulpy-core/src/layouts/gglwe_ct.rs b/poulpy-core/src/layouts/gglwe_ct.rs index 8546499..509e46e 100644 --- a/poulpy-core/src/layouts/gglwe_ct.rs +++ b/poulpy-core/src/layouts/gglwe_ct.rs @@ -3,7 +3,7 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, BuildError, Degree, Dnum, Dsize, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -132,107 +132,6 @@ impl GGLWEInfos for GGLWE { } } -pub struct GGLWECiphertextBuilder { - data: Option>, - base2k: Option, - k: Option, - dsize: Option, -} - -impl GGLWE { - #[inline] - pub fn builder() -> GGLWECiphertextBuilder { - GGLWECiphertextBuilder { - data: None, - base2k: None, - k: None, - dsize: None, - } - } -} - -impl GGLWECiphertextBuilder> { - #[inline] - pub fn layout(mut self, infos: &A) -> Self - where - A: GGLWEInfos, - { - self.data = Some(MatZnx::alloc( - infos.n().into(), - infos.dnum().into(), - infos.rank_in().into(), - (infos.rank_out() + 1).into(), - infos.size(), - )); - self.base2k = Some(infos.base2k()); - self.k = Some(infos.k()); - self.dsize = Some(infos.dsize()); - self - } -} - -impl GGLWECiphertextBuilder { - #[inline] - pub fn data(mut self, data: MatZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - #[inline] - pub fn dsize(mut self, dsize: Dsize) -> Self { - self.dsize = Some(dsize); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: MatZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - let dsize: Dsize = self.dsize.ok_or(BuildError::MissingDigits)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if dsize == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GGLWE { - data, - base2k, - k, - dsize, - }) - } -} - impl GGLWE { pub fn data(&self) -> &MatZnx { &self.data @@ -272,23 +171,21 @@ impl fmt::Display for GGLWE { impl GGLWE { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { - GLWECiphertext::builder() - .data(self.data.at(row, col)) - .base2k(self.base2k()) - .k(self.k()) - .build() - .unwrap() + GLWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.at(row, col), + } } } impl GGLWE { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext::builder() - .base2k(self.base2k()) - .k(self.k()) - .data(self.data.at_mut(row, col)) - .build() - .unwrap() + GLWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.at_mut(row, col), + } } } diff --git a/poulpy-core/src/layouts/ggsw_ct.rs b/poulpy-core/src/layouts/ggsw_ct.rs index 2d8b271..b1fa240 100644 --- a/poulpy-core/src/layouts/ggsw_ct.rs +++ b/poulpy-core/src/layouts/ggsw_ct.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use std::fmt; -use crate::layouts::{Base2K, BuildError, Degree, Dnum, Dsize, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision}; pub trait GGSWInfos where @@ -105,122 +105,6 @@ impl GGSWInfos for GGSW { } } -pub struct GGSWCiphertextBuilder { - data: Option>, - base2k: Option, - k: Option, - dsize: Option, -} - -impl GGSW { - #[inline] - pub fn builder() -> GGSWCiphertextBuilder { - GGSWCiphertextBuilder { - data: None, - base2k: None, - k: None, - dsize: None, - } - } -} - -impl GGSWCiphertextBuilder> { - #[inline] - pub fn layout(mut self, infos: &A) -> Self - where - A: GGSWInfos, - { - debug_assert!( - infos.size() as u32 > infos.dsize().0, - "invalid ggsw: ceil(k/base2k): {} <= dsize: {}", - infos.size(), - infos.dsize() - ); - - assert!( - infos.dnum().0 * infos.dsize().0 <= infos.size() as u32, - "invalid ggsw: dnum: {} * dsize:{} > ceil(k/base2k): {}", - infos.dnum(), - infos.dsize(), - infos.size(), - ); - - self.data = Some(MatZnx::alloc( - infos.n().into(), - infos.dnum().into(), - (infos.rank() + 1).into(), - (infos.rank() + 1).into(), - infos.size(), - )); - self.base2k = Some(infos.base2k()); - self.k = Some(infos.k()); - self.dsize = Some(infos.dsize()); - self - } -} - -impl GGSWCiphertextBuilder { - #[inline] - pub fn data(mut self, data: MatZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - #[inline] - pub fn dsize(mut self, dsize: Dsize) -> Self { - self.dsize = Some(dsize); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: MatZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - let dsize: Dsize = self.dsize.ok_or(BuildError::MissingDigits)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if dsize == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GGSW { - data, - base2k, - k, - dsize, - }) - } -} - impl fmt::Debug for GGSW { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.data) @@ -248,23 +132,21 @@ impl FillUniform for GGSW { impl GGSW { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { - GLWECiphertext::builder() - .data(self.data.at(row, col)) - .base2k(self.base2k()) - .k(self.k()) - .build() - .unwrap() + GLWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.at(row, col), + } } } impl GGSW { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext::builder() - .base2k(self.base2k()) - .k(self.k()) - .data(self.data.at_mut(row, col)) - .build() - .unwrap() + GLWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.at_mut(row, col), + } } } diff --git a/poulpy-core/src/layouts/glwe_ct.rs b/poulpy-core/src/layouts/glwe_ct.rs index c8cfa4f..7776662 100644 --- a/poulpy-core/src/layouts/glwe_ct.rs +++ b/poulpy-core/src/layouts/glwe_ct.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, BuildError, Degree, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, LWEInfos, Rank, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -86,86 +86,6 @@ impl GLWECiphertext { } } -pub struct GLWECiphertextBuilder { - data: Option>, - base2k: Option, - k: Option, -} - -impl GLWECiphertext { - #[inline] - pub fn builder() -> GLWECiphertextBuilder { - GLWECiphertextBuilder { - data: None, - base2k: None, - k: None, - } - } -} - -impl GLWECiphertextBuilder> { - #[inline] - pub fn layout(mut self, layout: &A) -> Self - where - A: GLWEInfos, - { - self.data = Some(VecZnx::alloc( - layout.n().into(), - (layout.rank() + 1).into(), - layout.size(), - )); - self.base2k = Some(layout.base2k()); - self.k = Some(layout.k()); - self - } -} - -impl GLWECiphertextBuilder { - #[inline] - pub fn data(mut self, data: VecZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VecZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GLWECiphertext { data, base2k, k }) - } -} - impl LWEInfos for GLWECiphertext { fn base2k(&self) -> Base2K { self.base2k diff --git a/poulpy-core/src/layouts/glwe_pk.rs b/poulpy-core/src/layouts/glwe_pk.rs index 6e947f1..b270b33 100644 --- a/poulpy-core/src/layouts/glwe_pk.rs +++ b/poulpy-core/src/layouts/glwe_pk.rs @@ -2,7 +2,7 @@ use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, VecZnxToMu use crate::{ dist::Distribution, - layouts::{Base2K, BuildError, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -22,6 +22,16 @@ pub struct GLWEPublicKeyLayout { pub rank: Rank, } +pub trait GetDist { + fn get_dist(&self) -> Distribution; +} + +impl GetDist for GLWEPublicKey { + fn get_dist(&self) -> Distribution { + self.dist + } +} + impl LWEInfos for GLWEPublicKey { fn base2k(&self) -> Base2K { self.base2k @@ -70,91 +80,6 @@ impl GLWEInfos for GLWEPublicKeyLayout { } } -pub struct GLWEPublicKeyBuilder { - data: Option>, - base2k: Option, - k: Option, -} - -impl GLWEPublicKey { - #[inline] - pub fn builder() -> GLWEPublicKeyBuilder { - GLWEPublicKeyBuilder { - data: None, - base2k: None, - k: None, - } - } -} - -impl GLWEPublicKeyBuilder> { - #[inline] - pub fn layout(mut self, layout: &A) -> Self - where - A: GLWEInfos, - { - self.data = Some(VecZnx::alloc( - layout.n().into(), - (layout.rank() + 1).into(), - layout.size(), - )); - self.base2k = Some(layout.base2k()); - self.k = Some(layout.k()); - self - } -} - -impl GLWEPublicKeyBuilder { - #[inline] - pub fn data(mut self, data: VecZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VecZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GLWEPublicKey { - data, - base2k, - k, - dist: Distribution::NONE, - }) - } -} - impl GLWEPublicKey> { pub fn alloc(infos: &A) -> Self where diff --git a/poulpy-core/src/layouts/glwe_pt.rs b/poulpy-core/src/layouts/glwe_pt.rs index c32f229..dfd4b8d 100644 --- a/poulpy-core/src/layouts/glwe_pt.rs +++ b/poulpy-core/src/layouts/glwe_pt.rs @@ -3,8 +3,8 @@ use std::fmt; use poulpy_hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; use crate::layouts::{ - Base2K, BuildError, Degree, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, GLWELayoutSet, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, GLWELayoutSet, LWEInfos, Rank, + TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -74,69 +74,6 @@ impl GLWEInfos for GLWEPlaintext { } } -pub struct GLWEPlaintextBuilder { - data: Option>, - base2k: Option, - k: Option, -} - -impl GLWEPlaintext { - #[inline] - pub fn builder() -> GLWEPlaintextBuilder { - GLWEPlaintextBuilder { - data: None, - base2k: None, - k: None, - } - } -} - -impl GLWEPlaintextBuilder { - #[inline] - pub fn data(mut self, data: VecZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VecZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - - if base2k.0 == 0 { - return Err(BuildError::ZeroBase2K); - } - - if k.0 == 0 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() != 1 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GLWEPlaintext { data, base2k, k }) - } -} - impl fmt::Display for GLWEPlaintext { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( @@ -181,23 +118,21 @@ impl GLWEPlaintext> { impl GLWECiphertextToRef for GLWEPlaintext { fn to_ref(&self) -> GLWECiphertext<&[u8]> { - GLWECiphertext::builder() - .data(self.data.to_ref()) - .k(self.k()) - .base2k(self.base2k()) - .build() - .unwrap() + GLWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.to_ref(), + } } } impl GLWECiphertextToMut for GLWEPlaintext { fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext::builder() - .k(self.k()) - .base2k(self.base2k()) - .data(self.data.to_mut()) - .build() - .unwrap() + GLWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.to_mut(), + } } } diff --git a/poulpy-core/src/layouts/lwe_ct.rs b/poulpy-core/src/layouts/lwe_ct.rs index ce5b13a..8c73a4d 100644 --- a/poulpy-core/src/layouts/lwe_ct.rs +++ b/poulpy-core/src/layouts/lwe_ct.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, BuildError, Degree, TorusPrecision}; +use crate::layouts::{Base2K, Degree, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; pub trait LWEInfos { @@ -153,82 +153,6 @@ impl LWECiphertext> { } } -impl LWECiphertextBuilder> { - #[inline] - pub fn layout(mut self, layout: A) -> Self - where - A: LWEInfos, - { - self.data = Some(Zn::alloc((layout.n() + 1).into(), 1, layout.size())); - self.base2k = Some(layout.base2k()); - self.k = Some(layout.k()); - self - } -} - -pub struct LWECiphertextBuilder { - data: Option>, - base2k: Option, - k: Option, -} - -impl LWECiphertext { - #[inline] - pub fn builder() -> LWECiphertextBuilder { - LWECiphertextBuilder { - data: None, - base2k: None, - k: None, - } - } -} - -impl LWECiphertextBuilder { - #[inline] - pub fn data(mut self, data: Zn) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: Zn = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - - if base2k.0 == 0 { - return Err(BuildError::ZeroBase2K); - } - - if k.0 == 0 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(LWECiphertext { data, base2k, k }) - } -} - pub trait LWECiphertextToRef { fn to_ref(&self) -> LWECiphertext<&[u8]>; } diff --git a/poulpy-core/src/layouts/mod.rs b/poulpy-core/src/layouts/mod.rs index 775fb6f..1d8e30d 100644 --- a/poulpy-core/src/layouts/mod.rs +++ b/poulpy-core/src/layouts/mod.rs @@ -33,21 +33,16 @@ pub use lwe_pt::*; pub use lwe_sk::*; pub use lwe_to_glwe_ksk::*; -#[derive(Debug)] -pub enum BuildError { - MissingData, - MissingBase2K, - MissingK, - MissingDigits, - ZeroDegree, - NonPowerOfTwoDegree, - ZeroBase2K, - ZeroTorusPrecision, - ZeroCols, - ZeroLimbs, - ZeroRank, - ZeroDigits, - VecZnxColsNotOne, +use poulpy_hal::layouts::{Backend, Module}; + +pub trait GetDegree { + fn n(&self) -> Degree; +} + +impl GetDegree for Module { + fn n(&self) -> Degree { + Self::n(&self).into() + } } /// Newtype over `u32` with arithmetic and comparisons against same type and `u32`. diff --git a/poulpy-core/src/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/gglwe_atk.rs index 32dd177..e590c21 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_atk.rs @@ -3,9 +3,8 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::{ - GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepareTmpBytes, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, - GLWESwitchingKeyPreparedAllocBytes, GLWESwitchingKeyPreparedAllocBytesFromInfos, GLWESwitchingKeyPreparedAllocFromInfos, - GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPreparedToRef, }, }; @@ -77,20 +76,9 @@ impl GGLWEInfos for AutomorphismKeyPrepared { } } -pub trait AutomorphismKeyPreparedAlloc { - fn automorphism_key_prepared_alloc( - &self, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> AutomorphismKeyPrepared, B>; -} - -impl AutomorphismKeyPreparedAlloc for Module +pub trait AutomorphismKeyPreparedAlloc where - Module: GLWESwitchingKeyPreparedAlloc, + Self: GLWESwitchingKeyPreparedAlloc, { fn automorphism_key_prepared_alloc( &self, @@ -105,18 +93,7 @@ where p: 0, } } -} -pub trait AutomorphismKeyPreparedAllocFromInfos { - fn automorphism_key_prepared_alloc_from_infos(&self, infos: &A) -> AutomorphismKeyPrepared, B> - where - A: GGLWEInfos; -} - -impl AutomorphismKeyPreparedAllocFromInfos for Module -where - Module: GLWESwitchingKeyPreparedAllocFromInfos, -{ fn automorphism_key_prepared_alloc_from_infos(&self, infos: &A) -> AutomorphismKeyPrepared, B> where A: GGLWEInfos, @@ -126,28 +103,15 @@ where infos.rank_out(), "rank_in != rank_out is not supported for AutomorphismKeyPrepared" ); - AutomorphismKeyPrepared { - key: self.glwe_switching_key_prepared_alloc_from_infos(infos), - p: 0, - } + self.automorphism_key_prepared_alloc( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) } -} -pub trait AutomorphismKeyPreparedAllocBytes { - fn automorphism_key_prepared_alloc_bytes( - &self, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize; -} - -impl AutomorphismKeyPreparedAllocBytes for Module -where - Module: GLWESwitchingKeyPreparedAllocBytes, -{ fn automorphism_key_prepared_alloc_bytes( &self, base2k: Base2K, @@ -158,18 +122,7 @@ where ) -> usize { self.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank, rank, dnum, dsize) } -} -pub trait AutomorphismKeyPreparedAllocBytesFromInfos { - fn automorphism_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos; -} - -impl AutomorphismKeyPreparedAllocBytesFromInfos for Module -where - Module: GLWESwitchingKeyPreparedAllocBytesFromInfos, -{ fn automorphism_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, @@ -179,15 +132,23 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" ); - self.glwe_switching_key_prepared_alloc_bytes_from_infos(infos) + self.automorphism_key_prepared_alloc_bytes( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) } } +impl AutomorphismKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} + impl AutomorphismKeyPrepared, B> { pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where A: GGLWEInfos, - Module: AutomorphismKeyPreparedAllocFromInfos, + Module: AutomorphismKeyPreparedAlloc, { module.automorphism_key_prepared_alloc_from_infos(infos) } @@ -202,28 +163,22 @@ impl AutomorphismKeyPrepared, B> { pub fn alloc_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: AutomorphismKeyPreparedAllocBytesFromInfos, + Module: AutomorphismKeyPreparedAlloc, { module.automorphism_key_prepared_alloc_bytes_from_infos(infos) } pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where - Module: AutomorphismKeyPreparedAllocBytes, + Module: AutomorphismKeyPreparedAlloc, { module.automorphism_key_prepared_alloc_bytes(base2k, k, rank, dnum, dsize) } } -pub trait AutomorphismKeyPrepareTmpBytes { - fn automorphism_key_prepare_tmp_bytes(&self, infos: &A) -> usize - where - A: GGLWEInfos; -} - -impl AutomorphismKeyPrepareTmpBytes for Module +pub trait AutomorphismKeyPrepare where - Module: GLWESwitchingKeyPrepareTmpBytes, + Self: GLWESwitchingKeyPrepare, { fn automorphism_key_prepare_tmp_bytes(&self, infos: &A) -> usize where @@ -231,28 +186,7 @@ where { self.glwe_switching_key_prepare_tmp_bytes(infos) } -} -impl AutomorphismKeyPrepared { - pub fn prepare_tmp_bytes(&self, module: &Module) -> usize - where - Module: AutomorphismKeyPrepareTmpBytes, - { - module.automorphism_key_prepare_tmp_bytes(self) - } -} - -pub trait AutomorphismKeyPrepare { - fn automorphism_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: AutomorphismKeyPreparedToMut + SetAutomorphismGaloisElement, - O: AutomorphismKeyToRef + GetAutomorphismGaloisElement; -} - -impl AutomorphismKeyPrepare for Module -where - Module: GLWESwitchingKeyPrepare, -{ fn automorphism_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: AutomorphismKeyPreparedToMut + SetAutomorphismGaloisElement, @@ -263,6 +197,27 @@ where } } +impl AutomorphismKeyPrepare for Module where Module: GLWESwitchingKeyPrepare {} + +impl AutomorphismKeyPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &Module) -> usize + where + Module: GLWESwitchingKeyPrepare, + { + module.automorphism_key_prepare_tmp_bytes(self) + } +} + +impl AutomorphismKeyPrepared { + pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + where + O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, + Module: AutomorphismKeyPrepare, + { + module.automorphism_key_prepare(self, other, scratch); + } +} + pub trait AutomorphismKeyPreparedToMut { fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ct.rs b/poulpy-core/src/layouts/prepared/gglwe_ct.rs index 077ad7c..6060e80 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ct.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ct.rs @@ -1,11 +1,10 @@ use poulpy_hal::{ api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare, VmpPrepareTmpBytes}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToMut, VmpPMatToRef, ZnxInfos}, - oep::VmpPMatAllocBytesImpl, }; use crate::layouts::{ - Base2K, BuildError, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToRef, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq)] @@ -58,123 +57,9 @@ impl GGLWEInfos for GGLWEPrepared { } } -pub struct GGLWEPreparedBuilder { - data: Option>, - base2k: Option, - k: Option, - dsize: Option, -} - -impl GGLWEPrepared { - #[inline] - pub fn builder() -> GGLWEPreparedBuilder { - GGLWEPreparedBuilder { - data: None, - base2k: None, - k: None, - dsize: None, - } - } -} - -impl GGLWEPreparedBuilder, B> { - #[inline] - pub fn layout(mut self, infos: &A) -> Self - where - A: GGLWEInfos, - B: VmpPMatAllocBytesImpl, - { - self.data = Some(VmpPMat::alloc( - infos.n().into(), - infos.dnum().into(), - infos.rank_in().into(), - (infos.rank_out() + 1).into(), - infos.size(), - )); - self.base2k = Some(infos.base2k()); - self.k = Some(infos.k()); - self.dsize = Some(infos.dsize()); - self - } -} - -impl GGLWEPreparedBuilder { - #[inline] - pub fn data(mut self, data: VmpPMat) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - #[inline] - pub fn dsize(mut self, dsize: Dsize) -> Self { - self.dsize = Some(dsize); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VmpPMat = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - let dsize: Dsize = self.dsize.ok_or(BuildError::MissingDigits)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if dsize == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GGLWEPrepared { - data, - base2k, - k, - dsize, - }) - } -} - -pub trait GGLWEPreparedAlloc { - fn gglwe_prepared_alloc( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> GGLWEPrepared, B>; -} - -impl GGLWEPreparedAlloc for Module +pub trait GGLWEPreparedAlloc where - Module: VmpPMatAlloc, + Self: GetDegree + VmpPMatAlloc + VmpPMatAllocBytes, { fn gglwe_prepared_alloc( &self, @@ -206,23 +91,12 @@ where dsize, } } -} -pub trait GGLWEPreparedAllocFromInfos { - fn gglwe_prepared_alloc_from_infos(&self, infos: &A) -> GGLWEPrepared, B> - where - A: GGLWEInfos; -} - -impl GGLWEPreparedAllocFromInfos for Module -where - Module: GGLWEPreparedAlloc, -{ fn gglwe_prepared_alloc_from_infos(&self, infos: &A) -> GGLWEPrepared, B> where A: GGLWEInfos, { - assert_eq!(self.n() as u32, infos.n(), "module.n() != infos.n()"); + assert_eq!(self.n(), infos.n()); self.gglwe_prepared_alloc( infos.base2k(), infos.k(), @@ -232,24 +106,7 @@ where infos.dsize(), ) } -} -pub trait GGLWEPreparedAllocBytes { - fn gglwe_prepared_alloc_bytes( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize; -} - -impl GGLWEPreparedAllocBytes for Module -where - Module: VmpPMatAllocBytes, -{ fn gglwe_prepared_alloc_bytes( &self, base2k: Base2K, @@ -275,23 +132,12 @@ where self.vmp_pmat_alloc_bytes(dnum.into(), rank_in.into(), (rank_out + 1).into(), size) } -} -pub trait GGLWEPreparedAllocBytesFromInfos { - fn gglwe_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos; -} - -impl GGLWEPreparedAllocBytesFromInfos for Module -where - Module: GGLWEPreparedAllocBytes, -{ fn gglwe_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - assert_eq!(self.n() as u32, infos.n(), "module.n() != infos.n()"); + assert_eq!(self.n(), infos.n()); self.gglwe_prepared_alloc_bytes( infos.base2k(), infos.k(), @@ -303,11 +149,15 @@ where } } -impl GGLWEPrepared, B> { +impl GGLWEPreparedAlloc for Module where Module: GetDegree + VmpPMatAlloc + VmpPMatAllocBytes {} + +impl GGLWEPrepared, B> +where + Module: GGLWEPreparedAlloc, +{ pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where A: GGLWEInfos, - Module: GGLWEPreparedAllocFromInfos, { module.gglwe_prepared_alloc_from_infos(infos) } @@ -320,17 +170,13 @@ impl GGLWEPrepared, B> { rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self - where - Module: GGLWEPreparedAlloc, - { + ) -> Self { module.gglwe_prepared_alloc(base2k, k, rank_in, rank_out, dnum, dsize) } pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: GGLWEPreparedAllocBytesFromInfos, { module.gglwe_prepared_alloc_bytes_from_infos(infos) } @@ -343,23 +189,14 @@ impl GGLWEPrepared, B> { rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> usize - where - Module: GGLWEPreparedAllocBytes, - { + ) -> usize { module.gglwe_prepared_alloc_bytes(base2k, k, rank_in, rank_out, dnum, dsize) } } -pub trait GGLWEPrepareTmpBytes { - fn gglwe_prepare_tmp_bytes(&self, infos: &A) -> usize - where - A: GGLWEInfos; -} - -impl GGLWEPrepareTmpBytes for Module +pub trait GGLWEPrepare where - Module: VmpPrepareTmpBytes, + Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare, { fn gglwe_prepare_tmp_bytes(&self, infos: &A) -> usize where @@ -372,28 +209,7 @@ where infos.size(), ) } -} -impl GGLWEPrepared, B> { - pub fn prepare_tmp_bytes(&self, module: &Module) -> usize - where - Module: GGLWEPrepareTmpBytes, - { - module.gglwe_prepare_tmp_bytes(self) - } -} - -pub trait GGLWEPrepare { - fn gglwe_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: GGLWEPreparedToMut, - O: GGLWEToRef; -} - -impl GGLWEPrepare for Module -where - Module: VmpPrepare, -{ fn gglwe_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GGLWEPreparedToMut, @@ -402,8 +218,8 @@ where let mut res: GGLWEPrepared<&mut [u8], B> = res.to_mut(); let other: GGLWE<&[u8]> = other.to_ref(); - assert_eq!(res.n(), self.n() as u32); - assert_eq!(other.n(), self.n() as u32); + assert_eq!(res.n(), self.n()); + assert_eq!(other.n(), self.n()); assert_eq!(res.base2k, other.base2k); assert_eq!(res.k, other.k); assert_eq!(res.dsize, other.dsize); @@ -412,11 +228,13 @@ where } } +impl GGLWEPrepare for Module where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare {} + impl GGLWEPrepared where Module: GGLWEPrepare, { - fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) where O: GGLWEToRef, { @@ -424,6 +242,15 @@ where } } +impl GGLWEPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &Module) -> usize + where + Module: GGLWEPrepare, + { + module.gglwe_prepare_tmp_bytes(self) + } +} + pub trait GGLWEPreparedToMut { fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs index 7f3aa97..6d1e443 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs @@ -3,10 +3,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToRef, GLWESwtichingKeyGetMetaData, LWEInfos, Rank, TorusPrecision, - prepared::{ - GGLWEPrepare, GGLWEPrepareTmpBytes, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedAllocBytes, - GGLWEPreparedAllocBytesFromInfos, GGLWEPreparedAllocFromInfos, GGLWEPreparedToMut, GGLWEPreparedToRef, - }, + prepared::{GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef}, }; #[derive(PartialEq, Eq)] @@ -78,21 +75,9 @@ impl GGLWEInfos for GLWESwitchingKeyPrepared { } } -pub trait GLWESwitchingKeyPreparedAlloc { - fn glwe_switching_key_prepared_alloc( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> GLWESwitchingKeyPrepared, B>; -} - -impl GLWESwitchingKeyPreparedAlloc for Module +pub trait GLWESwitchingKeyPreparedAlloc where - Module: GGLWEPreparedAlloc, + Self: GGLWEPreparedAlloc, { fn glwe_switching_key_prepared_alloc( &self, @@ -109,46 +94,21 @@ where sk_out_n: 0, } } -} -pub trait GLWESwitchingKeyPreparedAllocFromInfos { - fn glwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> GLWESwitchingKeyPrepared, B> - where - A: GGLWEInfos; -} - -impl GLWESwitchingKeyPreparedAllocFromInfos for Module -where - Module: GGLWEPreparedAllocFromInfos, -{ fn glwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> GLWESwitchingKeyPrepared, B> where A: GGLWEInfos, { - GLWESwitchingKeyPrepared::, B> { - key: self.gglwe_prepared_alloc_from_infos(infos), - sk_in_n: 0, - sk_out_n: 0, - } + self.glwe_switching_key_prepared_alloc( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) } -} -pub trait GLWESwitchingKeyPreparedAllocBytes { - fn glwe_switching_key_prepared_alloc_bytes( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize; -} - -impl GLWESwitchingKeyPreparedAllocBytes for Module -where - Module: GGLWEPreparedAllocBytes, -{ fn glwe_switching_key_prepared_alloc_bytes( &self, base2k: Base2K, @@ -160,31 +120,31 @@ where ) -> usize { self.gglwe_prepared_alloc_bytes(base2k, k, rank_in, rank_out, dnum, dsize) } -} -pub trait GLWESwitchingKeyPreparedAllocBytesFromInfos { - fn glwe_switching_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos; -} - -impl GLWESwitchingKeyPreparedAllocBytesFromInfos for Module -where - Module: GGLWEPreparedAllocBytesFromInfos, -{ fn glwe_switching_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.gglwe_prepared_alloc_bytes_from_infos(infos) + self.glwe_switching_key_prepared_alloc_bytes( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) } } -impl GLWESwitchingKeyPrepared, B> { +impl GLWESwitchingKeyPreparedAlloc for Module where Module: GGLWEPreparedAlloc {} + +impl GLWESwitchingKeyPrepared, B> +where + Module: GLWESwitchingKeyPreparedAlloc, +{ pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where A: GGLWEInfos, - Module: GLWESwitchingKeyPreparedAllocFromInfos, { module.glwe_switching_key_prepared_alloc_from_infos(infos) } @@ -197,17 +157,13 @@ impl GLWESwitchingKeyPrepared, B> { rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self - where - Module: GLWESwitchingKeyPreparedAlloc, - { + ) -> Self { module.glwe_switching_key_prepared_alloc(base2k, k, rank_in, rank_out, dnum, dsize) } pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: GLWESwitchingKeyPreparedAllocBytesFromInfos, { module.glwe_switching_key_prepared_alloc_bytes_from_infos(infos) } @@ -220,23 +176,14 @@ impl GLWESwitchingKeyPrepared, B> { rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> usize - where - Module: GLWESwitchingKeyPreparedAllocBytes, - { + ) -> usize { module.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank_in, rank_out, dnum, dsize) } } -pub trait GLWESwitchingKeyPrepareTmpBytes { - fn glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) -> usize - where - A: GGLWEInfos; -} - -impl GLWESwitchingKeyPrepareTmpBytes for Module +pub trait GLWESwitchingKeyPrepare where - Module: GGLWEPrepareTmpBytes, + Self: GGLWEPrepare, { fn glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) -> usize where @@ -244,28 +191,7 @@ where { self.gglwe_prepare_tmp_bytes(infos) } -} -impl GLWESwitchingKeyPrepared, B> -where - Module: GGLWEPrepareTmpBytes, -{ - pub fn prepare_tmp_bytes(&self, module: &Module) -> usize { - module.gglwe_prepare_tmp_bytes(self) - } -} - -pub trait GLWESwitchingKeyPrepare { - fn glwe_switching_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeySetMetaData, - O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData; -} - -impl GLWESwitchingKeyPrepare for Module -where - Module: GGLWEPrepare, -{ fn glwe_switching_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeySetMetaData, @@ -277,6 +203,8 @@ where } } +impl GLWESwitchingKeyPrepare for Module where Self: GGLWEPrepare {} + impl GLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) where @@ -287,6 +215,15 @@ impl GLWESwitchingKeyPrepared { } } +impl GLWESwitchingKeyPrepared, B> +where + Module: GLWESwitchingKeyPrepare, +{ + pub fn prepare_tmp_bytes(&self, module: &Module) -> usize { + module.gglwe_prepare_tmp_bytes(self) + } +} + pub trait GLWESwitchingKeyPreparedToMut { fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs index c6cfc90..b4cd56d 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs @@ -1,13 +1,9 @@ -use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision, - compressed::TensorKeyCompressedToMut, prepared::{ - GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepareTmpBytes, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, }, }; @@ -59,19 +55,36 @@ impl GGLWEInfos for TensorKeyPrepared { } } -impl TensorKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self +pub trait TensorKeyPreparedAlloc +where + Self: GLWESwitchingKeyPreparedAlloc, +{ + fn tensor_key_prepared_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + dnum: Dnum, + dsize: Dsize, + rank: Rank, + ) -> TensorKeyPrepared, B> { + let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); + TensorKeyPrepared { + keys: (0..pairs) + .map(|_| self.glwe_switching_key_prepared_alloc(base2k, k, Rank(1), rank, dnum, dsize)) + .collect(), + } + } + + fn tensor_key_prepared_alloc_from_infos(&self, infos: &A) -> TensorKeyPrepared, B> where A: GGLWEInfos, - Module: VmpPMatAlloc, { assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKeyPrepared" ); - Self::alloc_with( - module, + self.tensor_key_prepared_alloc( infos.base2k(), infos.k(), infos.dnum(), @@ -80,56 +93,58 @@ impl TensorKeyPrepared, B> { ) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self + fn tensor_key_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; + pairs * self.glwe_switching_key_prepared_alloc_bytes(base2k, k, Rank(1), rank, dnum, dsize) + } + + fn tensor_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize where - Module: VmpPMatAlloc, + A: GGLWEInfos, { - let mut keys: Vec, B>> = Vec::new(); - let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); - (0..pairs).for_each(|_| { - keys.push(GLWESwitchingKeyPrepared::alloc( - module, - base2k, - k, - Rank(1), - rank, - dnum, - dsize, - )); - }); - Self { keys } + self.tensor_key_prepared_alloc_bytes( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } +} + +impl TensorKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} + +impl TensorKeyPrepared, B> +where + Module: TensorKeyPreparedAlloc, +{ + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + { + module.tensor_key_prepared_alloc_from_infos(infos) + } + + pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self { + module.tensor_key_prepared_alloc(base2k, k, dnum, dsize, rank) } pub fn alloc_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWETensorKey" - ); - let rank_out: usize = infos.rank_out().into(); - let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); - pairs - * GLWESwitchingKeyPrepared::alloc_bytes( - module, - infos.base2k(), - infos.k(), - Rank(1), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) + module.tensor_key_prepared_alloc_bytes_from_infos(infos) } - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - Module: VmpPMatAllocBytes, - { - let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GLWESwitchingKeyPrepared::alloc_bytes(module, base2k, k, Rank(1), rank, dnum, dsize) + pub fn alloc_bytes_with( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize { + module.tensor_key_prepared_alloc_bytes(base2k, k, rank, dnum, dsize) } } @@ -155,22 +170,38 @@ impl TensorKeyPrepared { } } -pub trait TensorKeyPrepareTmpBytes { - fn tensor_key_prepare_tmp_bytes(&self, infos: &A) -> usize; -} - -impl TensorKeyPrepareTmpBytes for Module +pub trait TensorKeyPrepare where - Module: GLWESwitchingKeyPrepareTmpBytes, + Self: GLWESwitchingKeyPrepare, { - fn tensor_key_prepare_tmp_bytes(&self, infos: &A) -> usize { + fn tensor_key_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { self.glwe_switching_key_prepare_tmp_bytes(infos) } + + fn tensor_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: TensorKeyPreparedToMut, + O: TensorKeyToRef, + { + let mut res: TensorKeyPrepared<&mut [u8], B> = res.to_mut(); + let other: TensorKey<&[u8]> = other.to_ref(); + + assert_eq!(res.keys.len(), other.keys.len()); + + for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { + self.glwe_switching_prepare(a, b, scratch); + } + } } +impl TensorKeyPrepare for Module where Module: GLWESwitchingKeyPrepare {} + impl TensorKeyPrepared, B> where - Module: TensorKeyPrepareTmpBytes, + Module: TensorKeyPrepare, { fn prepare_tmp_bytes(&self, module: &Module, infos: &A) -> usize where @@ -180,33 +211,6 @@ where } } -pub trait TensorKeyPrepare { - fn tensor_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) - where - R: TensorKeyPreparedToMut, - O: TensorKeyToRef; -} - -impl TensorKeyPrepare for Module -where - Module: GLWESwitchingKeyPrepare, -{ - fn tensor_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) - where - R: TensorKeyPreparedToMut, - O: TensorKeyToRef, - { - let res = res.to_mut(); - let other = other.to_ref(); - - assert_eq!(self.keys.len(), other.keys.len()); - - for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { - self.glwe_switching_prepare(a, b, scratch); - } - } -} - impl TensorKeyPrepared where Module: TensorKeyPrepare, @@ -219,35 +223,6 @@ where } } -pub trait TensorKeyPrepareAlloc { - fn tensor_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: TensorKeyToRef; -} - -impl TensorKeyPrepareAlloc for Module -where - Module: TensorKeyPrepare, -{ - fn tensor_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: TensorKeyToRef, - { - let mut ct_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc(self, other); - self.tensor_key_prepare(ct_prepared, other, scratch); - ct_prepared - } -} - -impl TensorKey { - pub fn prepare_alloc(&self, module: &Module, scratch: &Scratch) - where - Module: TensorKeyPrepareAlloc, - { - module.tensor_key_prepare_alloc(self, scratch); - } -} - pub trait TensorKeyPreparedToMut { fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/ggsw_ct.rs b/poulpy-core/src/layouts/prepared/ggsw_ct.rs index 120c9ed..da9c5db 100644 --- a/poulpy-core/src/layouts/prepared/ggsw_ct.rs +++ b/poulpy-core/src/layouts/prepared/ggsw_ct.rs @@ -1,11 +1,10 @@ use poulpy_hal::{ api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare, VmpPrepareTmpBytes}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToMut, VmpPMatToRef, ZnxInfos}, - oep::VmpPMatAllocBytesImpl, }; use crate::layouts::{ - Base2K, BuildError, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq)] @@ -50,143 +49,18 @@ impl GGSWInfos for GGSWPrepared { } } -pub struct GGSWCiphertextPreparedBuilder { - data: Option>, - base2k: Option, - k: Option, - dsize: Option, -} - -impl GGSWPrepared { - #[inline] - pub fn builder() -> GGSWCiphertextPreparedBuilder { - GGSWCiphertextPreparedBuilder { - data: None, - base2k: None, - k: None, - dsize: None, - } - } -} - -impl GGSWCiphertextPreparedBuilder, B> { - #[inline] - pub fn layout(mut self, infos: &A) -> Self - where - A: GGSWInfos, - B: VmpPMatAllocBytesImpl, - { - debug_assert!( - infos.size() as u32 > infos.dsize().0, - "invalid ggsw: ceil(k/base2k): {} <= dsize: {}", - infos.size(), - infos.dsize() - ); - - assert!( - infos.dnum().0 * infos.dsize().0 <= infos.size() as u32, - "invalid ggsw: dnum: {} * dsize:{} > ceil(k/base2k): {}", - infos.dnum(), - infos.dsize(), - infos.size(), - ); - - self.data = Some(VmpPMat::alloc( - infos.n().into(), - infos.dnum().into(), - (infos.rank() + 1).into(), - (infos.rank() + 1).into(), - infos.size(), - )); - self.base2k = Some(infos.base2k()); - self.k = Some(infos.k()); - self.dsize = Some(infos.dsize()); - self - } -} - -impl GGSWCiphertextPreparedBuilder { - #[inline] - pub fn data(mut self, data: VmpPMat) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - #[inline] - pub fn dsize(mut self, dsize: Dsize) -> Self { - self.dsize = Some(dsize); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VmpPMat = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - let dsize: Dsize = self.dsize.ok_or(BuildError::MissingDigits)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if dsize == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GGSWPrepared { - data, - base2k, - k, - dsize, - }) - } -} - -impl GGSWPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self - where - A: GGSWInfos, - Module: VmpPMatAlloc, - { - Self::alloc_with( - module, - infos.base2k(), - infos.k(), - infos.dnum(), - infos.dsize(), - infos.rank(), - ) - } - - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self - where - Module: VmpPMatAlloc, - { +pub trait GGSWPreparedAlloc +where + Self: GetDegree + VmpPMatAlloc + VmpPMatAllocBytes, +{ + fn ggsw_prepared_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + dnum: Dnum, + dsize: Dsize, + rank: Rank, + ) -> GGSWPrepared, B> { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -201,8 +75,8 @@ impl GGSWPrepared, B> { dsize.0, ); - Self { - data: module.vmp_pmat_alloc( + GGSWPrepared { + data: self.vmp_pmat_alloc( dnum.into(), (rank + 1).into(), (rank + 1).into(), @@ -214,13 +88,12 @@ impl GGSWPrepared, B> { } } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + fn ggsw_prepared_alloc_from_infos(&self, infos: &A) -> GGSWPrepared, B> where A: GGSWInfos, - Module: VmpPMatAllocBytes, { - Self::alloc_bytes_with( - module, + assert_eq!(self.n(), infos.n()); + self.ggsw_prepared_alloc( infos.base2k(), infos.k(), infos.dnum(), @@ -229,10 +102,7 @@ impl GGSWPrepared, B> { ) } - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize - where - Module: VmpPMatAllocBytes, - { + fn ggsw_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -247,7 +117,57 @@ impl GGSWPrepared, B> { dsize.0, ); - module.vmp_pmat_alloc_bytes(dnum.into(), (rank + 1).into(), (rank + 1).into(), size) + self.vmp_pmat_alloc_bytes(dnum.into(), (rank + 1).into(), (rank + 1).into(), size) + } + + fn ggsw_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + where + A: GGSWInfos, + { + assert_eq!(self.n(), infos.n()); + self.ggsw_prepared_alloc_bytes( + infos.base2k(), + infos.k(), + infos.dnum(), + infos.dsize(), + infos.rank(), + ) + } +} + +impl GGSWPreparedAlloc for Module where Module: GetDegree + VmpPMatAlloc + VmpPMatAllocBytes {} + +impl GGSWPrepared, B> +where + Module: GGSWPreparedAlloc, +{ + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGSWInfos, + { + module.ggsw_prepared_alloc_from_infos(infos) + } + + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self { + module.ggsw_prepared_alloc(base2k, k, dnum, dsize, rank) + } + + pub fn alloc_bytes(module: &Module, infos: &A) -> usize + where + A: GGSWInfos, + { + module.ggsw_prepared_alloc_bytes_from_infos(infos) + } + + pub fn alloc_bytes_with( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + dnum: Dnum, + dsize: Dsize, + rank: Rank, + ) -> usize { + module.ggsw_prepared_alloc_bytes(base2k, k, dnum, dsize, rank) } } @@ -257,20 +177,15 @@ impl GGSWPrepared { } } -pub trait GGSWPrepareTmpBytes { - fn ggsw_prepare_tmp_bytes(&self, infos: &A) -> usize - where - A: GGSWInfos; -} - -impl GGSWPrepareTmpBytes for Module +pub trait GGSWPrepare where - Module: VmpPrepareTmpBytes, + Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare, { fn ggsw_prepare_tmp_bytes(&self, infos: &A) -> usize where A: GGSWInfos, { + assert_eq!(self.n(), infos.n()); self.vmp_prepare_tmp_bytes( infos.dnum().into(), (infos.rank() + 1).into(), @@ -278,31 +193,6 @@ where infos.size(), ) } -} - -impl GGSWPrepared, B> -where - Module: GGSWPrepareTmpBytes, -{ - pub fn prepare_tmp_bytes(&self, module: Module, infos: &A) -> usize - where - A: GGSWInfos, - { - module.ggsw_prepare_tmp_bytes(self) - } -} - -pub trait GGSWPrepare { - fn ggsw_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: GGSWPreparedToMut, - O: GGSWToRef; -} - -impl GGSWPrepare for Module -where - Module: VmpPrepare, -{ fn ggsw_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GGSWPreparedToMut, @@ -310,6 +200,8 @@ where { let mut res: GGSWPrepared<&mut [u8], B> = res.to_mut(); let other: GGSW<&[u8]> = other.to_ref(); + assert_eq!(res.n(), self.n()); + assert_eq!(other.n(), self.n()); assert_eq!(res.k, other.k); assert_eq!(res.base2k, other.base2k); assert_eq!(res.dsize, other.dsize); @@ -317,6 +209,20 @@ where } } +impl GGSWPrepare for Module where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare {} + +impl GGSWPrepared, B> +where + Module: GGSWPrepare, +{ + pub fn prepare_tmp_bytes(&self, module: Module, infos: &A) -> usize + where + A: GGSWInfos, + { + module.ggsw_prepare_tmp_bytes(infos) + } +} + impl GGSWPrepared where Module: GGSWPrepare, @@ -329,35 +235,6 @@ where } } -pub trait GGSWPrepareAlloc { - fn ggsw_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GGSWToRef; -} - -impl GGSWPrepareAlloc for Module -where - Module: GGSWPrepare, -{ - fn ggsw_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GGSWToRef, - { - let mut ct_prepared: GGSWPrepared, B> = GGSWPrepared::alloc(self, other); - self.ggsw_prepare(&mut ct_prepared, other, scratch); - ct_prepared - } -} - -impl GGSW { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) - where - Module: GGSWPrepareAlloc, - { - module.ggsw_prepare_alloc(self, scratch); - } -} - pub trait GGSWPreparedToMut { fn to_mut(&mut self) -> GGSWPrepared<&mut [u8], B>; } @@ -383,7 +260,7 @@ impl GGSWCiphertextPreparedToRef for GGSWPrepared GLWEInfos for GLWEPublicKeyPrepared { } } -pub struct GLWEPublicKeyPreparedBuilder { - data: Option>, - base2k: Option, - k: Option, -} - -impl GLWEPublicKeyPrepared { - #[inline] - pub fn builder() -> GLWEPublicKeyPreparedBuilder { - GLWEPublicKeyPreparedBuilder { - data: None, - base2k: None, - k: None, - } - } -} - -impl GLWEPublicKeyPreparedBuilder, B> { - #[inline] - pub fn layout(mut self, layout: &A) -> Self - where - A: GLWEInfos, - B: VecZnxDftAllocBytesImpl, - { - self.data = Some(VecZnxDft::alloc( - layout.n().into(), - (layout.rank() + 1).into(), - layout.size(), - )); - self.base2k = Some(layout.base2k()); - self.k = Some(layout.k()); - self - } -} - -impl GLWEPublicKeyPreparedBuilder { - #[inline] - pub fn data(mut self, data: VecZnxDft) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VecZnxDft = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GLWEPublicKeyPrepared { - data, - base2k, - k, - dist: Distribution::NONE, - }) - } -} - -impl GLWEPublicKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self - where - A: GLWEInfos, - Module: VecZnxDftAlloc, - { - debug_assert_eq!(module.n(), infos.n().0 as usize, "module.n() != infos.n()"); - Self::alloc_with(module, infos.base2k(), infos.k(), infos.rank()) - } - - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self - where - Module: VecZnxDftAlloc, - { - Self { - data: module.vec_znx_dft_alloc((rank + 1).into(), k.0.div_ceil(base2k.0) as usize), - base2k, - k, - dist: Distribution::NONE, - } - } - - pub fn alloc_bytes(module: &Module, infos: &A) -> usize - where - A: GLWEInfos, - Module: VecZnxDftAllocBytes, - { - debug_assert_eq!(module.n(), infos.n().0 as usize, "module.n() != infos.n()"); - Self::alloc_bytes_with(module, infos.base2k(), infos.k(), infos.rank()) - } - - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize - where - Module: VecZnxDftAllocBytes, - { - module.vec_znx_dft_alloc_bytes((rank + 1).into(), k.0.div_ceil(base2k.0) as usize) - } -} - -pub trait GLWEPublicKeyPrepareTmpBytes { - fn glwe_public_key_prepare_tmp_bytes(&self, infos: &A) - where - A: GLWEInfos; -} - -impl GLWEPublicKeyPrepareTmpBytes for Module { - fn glwe_public_key_prepare_tmp_bytes(&self, infos: &A) - where - A: GLWEInfos, - { - 0 - } -} - -impl GLWEPublicKeyPrepared, B> { - pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) - where - A: GLWEInfos, - Module: GLWEPublicKeyPrepareTmpBytes, - { - module.glwe_public_key_prepare_tmp_bytes(infos); - } -} - -pub trait GLWEPublicKeyPrepare { - fn glwe_public_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) - where - R: GLWEPublicKeyPreparedToMut + SetDist, - O: GLWEPublicKeyToRef; -} - -impl GLWEPublicKeyPrepare for Module +pub trait GLWEPublicKeyPreparedAlloc where - Module: VecZnxDftAlloc + VecZnxDftApply, + Self: GetDegree + VecZnxDftAlloc + VecZnxDftAllocBytes, { - fn glwe_public_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + fn glwe_public_key_prepared_alloc(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKeyPrepared, B> { + GLWEPublicKeyPrepared { + data: self.vec_znx_dft_alloc((rank + 1).into(), k.0.div_ceil(base2k.0) as usize), + base2k, + k, + dist: Distribution::NONE, + } + } + + fn glwe_public_key_alloc_from_infos(&self, infos: &A) -> GLWEPublicKeyPrepared, B> + where + A: GLWEInfos, + { + self.glwe_public_key_prepared_alloc(infos.base2k(), infos.k(), infos.rank()) + } + + fn glwe_public_key_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + self.vec_znx_dft_alloc_bytes((rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + } + + fn glwe_public_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.glwe_public_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.rank()) + } +} + +impl GLWEPublicKeyPreparedAlloc for Module where Self: VecZnxDftAlloc + VecZnxDftAllocBytes {} + +impl GLWEPublicKeyPrepared, B> +where + Module: GLWEPublicKeyPreparedAlloc, +{ + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GLWEInfos, + { + module.glwe_public_key_alloc_from_infos(infos) + } + + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { + module.glwe_public_key_prepared_alloc(base2k, k, rank) + } + + pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + where + A: GLWEInfos, + { + module.glwe_public_key_prepared_alloc_bytes_from_infos(infos) + } + + pub fn alloc_bytes(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + module.glwe_public_key_prepared_alloc_bytes(base2k, k, rank) + } +} + +pub trait GLWEPublicKeyPrepare +where + Self: GetDegree + VecZnxDftApply, +{ + fn glwe_public_key_prepare(&self, res: &mut R, other: &O) where R: GLWEPublicKeyPreparedToMut + SetDist, - O: GLWEPublicKeyToRef, + O: GLWEPublicKeyToRef + GetDist, { { - let res: GLWEPublicKeyPrepared<&mut [u8], B> = res.to_mut(); + let mut res: GLWEPublicKeyPrepared<&mut [u8], B> = res.to_mut(); let other: GLWEPublicKey<&[u8]> = other.to_ref(); - assert_eq!(res.n(), self.n() as u32); - assert_eq!(other.n(), self.n() as u32); + assert_eq!(res.n(), self.n()); + assert_eq!(other.n(), self.n()); assert_eq!(res.size(), other.size()); assert_eq!(res.k(), other.k()); assert_eq!(res.base2k(), other.base2k()); - for i in 0..(self.rank() + 1).into() { - self.vec_znx_dft_apply(1, 0, &mut self.data, i, &other.data, i); + for i in 0..(res.rank() + 1).into() { + self.vec_znx_dft_apply(1, 0, &mut res.data, i, &other.data, i); } } - res.set_dist(other.dist); + res.set_dist(other.get_dist()); } } +impl GLWEPublicKeyPrepare for Module where Self: GetDegree + VecZnxDftApply {} + impl GLWEPublicKeyPrepared where Module: GLWEPublicKeyPrepare, { - pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + pub fn prepare(&mut self, module: &Module, other: &O) where - O: GLWEPublicKeyToRef, + O: GLWEPublicKeyToRef + GetDist, { - module.glwe_public_key_prepare(self, other, scratch); - } -} - -pub trait GLWEPublicKeyPrepareAlloc { - fn glwe_public_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GLWEPublicKeyToRef; -} - -impl GLWEPublicKeyPrepareAlloc for Module -where - Module: GLWEPublicKeyPrepare, -{ - fn glwe_public_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GLWEPublicKeyToRef, - { - let mut ct_prepared: GLWEPublicKeyPrepared, B> = GLWEPublicKeyPrepared::alloc(self, other); - self.glwe_public_key_prepare(&mut ct_prepared, ct_prepared, scratch); - ct_prepared - } -} - -impl GLWEPublicKey { - pub fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) - where - Module: GLWEPublicKeyPrepareAlloc, - { - module.glwe_public_key_prepare_alloc(self, scratch); + module.glwe_public_key_prepare(self, other); } } diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index c75f425..4bac6c0 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -1,12 +1,12 @@ use poulpy_hal::{ api::{SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, SvpPPol, SvpPPolToMut, SvpPPolToRef, ZnxInfos}, + layouts::{Backend, Data, DataMut, DataRef, Module, SvpPPol, SvpPPolToMut, SvpPPolToRef, ZnxInfos}, }; use crate::{ dist::Distribution, layouts::{ - Base2K, Degree, GLWEInfos, GLWESecret, GLWESecretToMut, GLWESecretToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, GLWEInfos, GLWESecret, GLWESecretToRef, GetDegree, GetDist, LWEInfos, Rank, TorusPrecision, prepared::SetDist, }, }; @@ -44,40 +44,63 @@ impl GLWEInfos for GLWESecretPrepared { Rank(self.data.cols() as u32) } } -impl GLWESecretPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self - where - A: GLWEInfos, - Module: SvpPPolAlloc, - { - assert_eq!(module.n() as u32, infos.n()); - Self::alloc_with(module, infos.rank()) - } - pub fn alloc_with(module: &Module, rank: Rank) -> Self - where - Module: SvpPPolAlloc, - { - Self { - data: module.svp_ppol_alloc(rank.into()), +pub trait GLWESecretPreparedAlloc +where + Self: GetDegree + SvpPPolAllocBytes + SvpPPolAlloc, +{ + fn glwe_secret_prepared_alloc(&self, rank: Rank) -> GLWESecretPrepared, B> { + GLWESecretPrepared { + data: self.svp_ppol_alloc(rank.into()), dist: Distribution::NONE, } } - - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + fn glwe_secret_prepared_alloc_from_infos(&self, infos: &A) -> GLWESecretPrepared, B> where A: GLWEInfos, - Module: SvpPPolAllocBytes, { - assert_eq!(module.n() as u32, infos.n()); - Self::alloc_bytes_with(module, infos.rank()) + assert_eq!(self.n(), infos.n()); + self.glwe_secret_prepared_alloc(infos.rank()) } - pub fn alloc_bytes_with(module: &Module, rank: Rank) -> usize + fn glwe_secret_alloc_bytes(&self, rank: Rank) -> usize { + self.svp_ppol_alloc_bytes(rank.into()) + } + fn glwe_secret_alloc_bytes_from_infos(&self, infos: &A) -> usize where - Module: SvpPPolAllocBytes, + A: GLWEInfos, { - module.svp_ppol_alloc_bytes(rank.into()) + assert_eq!(self.n(), infos.n()); + self.glwe_secret_alloc_bytes(infos.rank()) + } +} + +impl GLWESecretPreparedAlloc for Module where Self: GetDegree + SvpPPolAllocBytes + SvpPPolAlloc {} + +impl GLWESecretPrepared, B> +where + Module: GLWESecretPreparedAlloc, +{ + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GLWEInfos, + { + module.glwe_secret_prepared_alloc_from_infos(infos) + } + + pub fn alloc(module: &Module, rank: Rank) -> Self { + module.glwe_secret_prepared_alloc(rank) + } + + pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + where + A: GLWEInfos, + { + module.glwe_secret_alloc_bytes_from_infos(infos) + } + + pub fn alloc_bytes(module: &Module, rank: Rank) -> usize { + module.glwe_secret_alloc_bytes(rank) } } @@ -91,81 +114,29 @@ impl GLWESecretPrepared { } } -pub trait GLWESecretPrepareTmpBytes { - fn glwe_secret_prepare_tmp_bytes(&self, infos: &A) - where - A: GLWEInfos; -} - -impl GLWESecretPrepareTmpBytes for Module { - fn glwe_secret_prepare_tmp_bytes(&self, infos: &A) - where - A: GLWEInfos, - { - 0 - } -} - -impl GLWESecretPrepared, B> +pub trait GLWESecretPrepare where - Module: GLWESecretPrepareTmpBytes, + Self: SvpPrepare, { - fn prepare_tmp_bytes(&self, module: &Module, infos: &A) -> usize - where - A: GLWEInfos, - { - 0 - } -} - -pub trait GLWESecretPrepare { - fn glwe_secret_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + fn glwe_secret_prepare(&self, res: &mut R, other: &O) where R: GLWESecretPreparedToMut + SetDist, - O: GLWESecretToRef; -} - -impl GLWESecretPrepare for Module -where - Module: SvpPrepare, -{ - fn glwe_secret_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: GLWESecretPreparedToMut + SetDist, - O: GLWESecretToRef, + O: GLWESecretToRef + GetDist, { { - let res: GLWESecretPrepared<&mut [u8], _> = res.to_mut(); + let mut res: GLWESecretPrepared<&mut [u8], _> = res.to_mut(); let other: GLWESecret<&[u8]> = other.to_ref(); - for i in 0..self.rank().into() { + for i in 0..res.rank().into() { self.svp_prepare(&mut res.data, i, &other.data, i); } } - res.set_dist(other.dist); + res.set_dist(other.get_dist()); } } -pub trait GLWESecretPrepareAlloc { - fn glwe_secret_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GLWESecretToMut; -} - -impl GLWESecretPrepareAlloc for Module -where - Module: GLWESecretPrepare, -{ - fn glwe_secret_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GLWESecretToMut, - { - let mut ct_prep: GLWESecretPrepared, B> = GLWESecretPrepared::alloc(self, self); - self.glwe_secret_prepare(&mut ct_prep, other, scratch); - ct_prep - } -} +impl GLWESecretPrepare for Module where Self: SvpPrepare {} pub trait GLWESecretPreparedToRef { fn to_ref(&self) -> GLWESecretPrepared<&[u8], B>; diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index e2a8fdc..5f10f66 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -1,13 +1,9 @@ -use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToMut, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, - TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, prepared::{ - GLWESecretPrepareTmpBytes, GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, }, }; @@ -57,11 +53,22 @@ impl GGLWEInfos for GLWEToLWESwitchingKeyPrepared { } } -impl GLWEToLWESwitchingKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self +pub trait GLWEToLWESwitchingKeyPreparedAlloc +where + Self: GLWESwitchingKeyPreparedAlloc, +{ + fn glwe_to_lwe_switching_key_prepared_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + ) -> GLWEToLWESwitchingKeyPrepared, B> { + GLWEToLWESwitchingKeyPrepared(self.glwe_switching_key_prepared_alloc(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) + } + fn glwe_to_lwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> GLWEToLWESwitchingKeyPrepared, B> where A: GGLWEInfos, - Module: VmpPMatAlloc, { debug_assert_eq!( infos.rank_out().0, @@ -73,28 +80,22 @@ impl GLWEToLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" ); - Self(GLWESwitchingKeyPrepared::alloc_from_infos(module, infos)) + self.glwe_to_lwe_switching_key_prepared_alloc(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self - where - Module: VmpPMatAlloc, - { - Self(GLWESwitchingKeyPrepared::alloc( - module, - base2k, - k, - rank_in, - Rank(1), - dnum, - Dsize(1), - )) + fn glwe_to_lwe_switching_key_prepared_alloc_bytes( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + ) -> usize { + self.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank_in, Rank(1), dnum, Dsize(1)) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + fn glwe_to_lwe_switching_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, { debug_assert_eq!( infos.rank_out().0, @@ -106,67 +107,73 @@ impl GLWEToLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" ); - GLWESwitchingKeyPrepared::alloc_bytes_from_infos(module, infos) - } - - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize - where - Module: VmpPMatAllocBytes, - { - GLWESwitchingKeyPrepared::alloc_bytes(module, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + self.glwe_to_lwe_switching_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) } } -pub trait GLWEToLWESwitchingKeyPrepareTmpBytes { - fn glwe_to_lwe_switching_key_prepare_tmp_bytes(&self, infos: &A) - where - A: GGLWEInfos; -} +impl GLWEToLWESwitchingKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} -impl GLWEToLWESwitchingKeyPrepareTmpBytes for Module +impl GLWEToLWESwitchingKeyPrepared, B> where - Module: GLWEToLWESwitchingKeyPrepareTmpBytes, + Module: GLWEToLWESwitchingKeyPreparedAlloc, +{ + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + { + module.glwe_to_lwe_switching_key_prepared_alloc_from_infos(infos) + } + + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { + module.glwe_to_lwe_switching_key_prepared_alloc(base2k, k, rank_in, dnum) + } + + pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + { + module.glwe_to_lwe_switching_key_prepared_alloc_bytes_from_infos(infos) + } + + pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { + module.glwe_to_lwe_switching_key_prepared_alloc_bytes(base2k, k, rank_in, dnum) + } +} + +pub trait GLWEToLWESwitchingKeyPrepare +where + Self: GLWESwitchingKeyPrepare, { fn glwe_to_lwe_switching_key_prepare_tmp_bytes(&self, infos: &A) where A: GGLWEInfos, { - self.glwe_to_lwe_switching_key_prepare_tmp_bytes(infos); + self.glwe_switching_key_prepare_tmp_bytes(infos); } -} -impl GLWEToLWESwitchingKeyPrepared, B> { - pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) - where - A: GLWEInfos, - Module: GLWEToLWESwitchingKeyPrepareTmpBytes, - { - module.glwe_secret_prepare_tmp_bytes(infos); - } -} - -pub trait GLWEToLWESwitchingKeyPrepare { - fn glwe_to_lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) - where - R: GLWEToLWESwitchingKeyPreparedToMut, - O: GLWEToLWESwitchingKeyToRef; -} - -impl GLWEToLWESwitchingKeyPrepare for Module -where - Module: GLWESwitchingKeyPrepare, -{ - fn glwe_to_lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + fn glwe_to_lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GLWEToLWESwitchingKeyPreparedToMut, O: GLWEToLWESwitchingKeyToRef, { - self.glwe_switching_prepare(&mut res.to_mut().0, other, scratch); + self.glwe_switching_prepare(&mut res.to_mut().0, &other.to_ref().0, scratch); + } +} + +impl GLWEToLWESwitchingKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} + +impl GLWEToLWESwitchingKeyPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) + where + A: GGLWEInfos, + Module: GLWEToLWESwitchingKeyPrepare, + { + module.glwe_to_lwe_switching_key_prepare_tmp_bytes(infos); } } impl GLWEToLWESwitchingKeyPrepared { - fn prepare(&mut self, module: &Module, other: &O, scratch: &Scratch) + fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) where O: GLWEToLWESwitchingKeyToRef, Module: GLWEToLWESwitchingKeyPrepare, @@ -175,26 +182,6 @@ impl GLWEToLWESwitchingKeyPrepared { } } -pub trait GLWEToLWESwitchingKeyPrepareAlloc { - fn glwe_to_lwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GLWEToLWESwitchingKeyToRef; -} - -impl GLWEToLWESwitchingKeyPrepareAlloc for Module -where - Module: GLWEToLWESwitchingKeyPrepare, -{ - fn glwe_to_lwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GLWEToLWESwitchingKeyToRef, - { - let mut ct_prep: GLWEToLWESwitchingKeyPrepared, B> = GLWEToLWESwitchingKeyPrepared::alloc(self, &other.to_ref()); - self.glwe_to_lwe_switching_key_prepare(&mut ct_prep, other, scratch); - ct_prep - } -} - pub trait GLWEToLWESwitchingKeyPreparedToRef { fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/lwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_ksk.rs index cf5f31b..6017f2f 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_ksk.rs @@ -1,11 +1,11 @@ -use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKeyToRef, Rank, TorusPrecision, - prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef}, + prepared::{ + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPreparedToRef, + }, }; #[derive(PartialEq, Eq)] @@ -52,11 +52,22 @@ impl GGLWEInfos for LWESwitchingKeyPrepared { } } -impl LWESwitchingKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self +pub trait LWESwitchingKeyPreparedAlloc +where + Self: GLWESwitchingKeyPreparedAlloc, +{ + fn lwe_switching_key_prepared_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + dnum: Dnum, + ) -> LWESwitchingKeyPrepared, B> { + LWESwitchingKeyPrepared(self.glwe_switching_key_prepared_alloc(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) + } + + fn lwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> LWESwitchingKeyPrepared, B> where A: GGLWEInfos, - Module: VmpPMatAlloc, { debug_assert_eq!( infos.dsize().0, @@ -73,28 +84,16 @@ impl LWESwitchingKeyPrepared, B> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - Self(GLWESwitchingKeyPrepared::alloc_from_infos(module, infos)) + self.lwe_switching_key_prepared_alloc(infos.base2k(), infos.k(), infos.dnum()) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self - where - Module: VmpPMatAlloc, - { - Self(GLWESwitchingKeyPrepared::alloc( - module, - base2k, - k, - Rank(1), - Rank(1), - dnum, - Dsize(1), - )) + fn lwe_switching_key_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + self.glwe_switching_key_prepared_alloc_bytes(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + fn lwe_switching_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, { debug_assert_eq!( infos.dsize().0, @@ -111,67 +110,72 @@ impl LWESwitchingKeyPrepared, B> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GLWESwitchingKeyPrepared::alloc_bytes_from_infos(module, infos) - } - - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize - where - Module: VmpPMatAllocBytes, - { - GLWESwitchingKeyPrepared::alloc_bytes(module, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) + self.lwe_switching_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.dnum()) } } -pub trait LWESwitchingKeyPrepareTmpBytes { - fn lwe_switching_key_prepare_tmp_bytes(&self, infos: &A) - where - A: GGLWEInfos; -} +impl LWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} -impl LWESwitchingKeyPrepareTmpBytes for Module +impl LWESwitchingKeyPrepared, B> where - Module: LWESwitchingKeyPrepareTmpBytes, + Module: LWESwitchingKeyPreparedAlloc, +{ + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + { + module.lwe_switching_key_prepared_alloc_from_infos(infos) + } + + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { + module.lwe_switching_key_prepared_alloc(base2k, k, dnum) + } + + pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + { + module.lwe_switching_key_prepared_alloc_bytes_from_infos(infos) + } + + pub fn alloc_bytes(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + module.lwe_switching_key_prepared_alloc_bytes(base2k, k, dnum) + } +} + +pub trait LWESwitchingKeyPrepare +where + Self: GLWESwitchingKeyPrepare, { fn lwe_switching_key_prepare_tmp_bytes(&self, infos: &A) where A: GGLWEInfos, { - self.lwe_switching_key_prepare_tmp_bytes(infos); + self.glwe_switching_key_prepare_tmp_bytes(infos); } -} - -impl LWESwitchingKeyPrepared, B> { - pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) - where - A: GLWEInfos, - Module: LWESwitchingKeyPrepareTmpBytes, - { - module.glwe_secret_prepare_tmp_bytes(infos); - } -} - -pub trait LWESwitchingKeyPrepare { - fn lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) - where - R: LWESwitchingKeyPreparedToMut, - O: LWESwitchingKeyToRef; -} - -impl LWESwitchingKeyPrepare for Module -where - Module: GLWESwitchingKeyPrepare, -{ - fn lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + fn lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: LWESwitchingKeyPreparedToMut, O: LWESwitchingKeyToRef, { - self.glwe_switching_prepare(&mut res.to_mut().0, other, scratch); + self.glwe_switching_prepare(&mut res.to_mut().0, &other.to_ref().0, scratch); + } +} + +impl LWESwitchingKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} + +impl LWESwitchingKeyPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) + where + A: GGLWEInfos, + Module: LWESwitchingKeyPrepare, + { + module.lwe_switching_key_prepare_tmp_bytes(infos); } } impl LWESwitchingKeyPrepared { - fn prepare(&mut self, module: &Module, other: &O, scratch: &Scratch) + fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) where O: LWESwitchingKeyToRef, Module: LWESwitchingKeyPrepare, @@ -180,26 +184,6 @@ impl LWESwitchingKeyPrepared { } } -pub trait LWESwitchingKeyPrepareAlloc { - fn lwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: LWESwitchingKeyToRef; -} - -impl LWESwitchingKeyPrepareAlloc for Module -where - Module: LWESwitchingKeyPrepare, -{ - fn lwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: LWESwitchingKeyToRef, - { - let mut ct_prep: LWESwitchingKeyPrepared, B> = LWESwitchingKeyPrepared::alloc(self, &other.to_ref()); - self.lwe_switching_key_prepare(&mut ct_prep, other, scratch); - ct_prep - } -} - pub trait LWESwitchingKeyPreparedToRef { fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs index 954553a..ed99a51 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -1,11 +1,11 @@ -use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKeyToRef, Rank, TorusPrecision, - prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef}, + prepared::{ + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPreparedToRef, + }, }; /// A special [GLWESwitchingKey] required to for the conversion from [LWECiphertext] to [GLWECiphertext]. @@ -54,11 +54,22 @@ impl GGLWEInfos for LWEToGLWESwitchingKeyPrepared { } } -impl LWEToGLWESwitchingKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self +pub trait LWEToGLWESwitchingKeyPreparedAlloc +where + Self: GLWESwitchingKeyPreparedAlloc, +{ + fn lwe_to_glwe_switching_key_prepared_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_out: Rank, + dnum: Dnum, + ) -> LWEToGLWESwitchingKeyPrepared, B> { + LWEToGLWESwitchingKeyPrepared(self.glwe_switching_key_prepared_alloc(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) + } + fn lwe_to_glwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> LWEToGLWESwitchingKeyPrepared, B> where A: GGLWEInfos, - Module: VmpPMatAlloc, { debug_assert_eq!( infos.rank_in().0, @@ -70,28 +81,22 @@ impl LWEToGLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - Self(GLWESwitchingKeyPrepared::alloc_from_infos(module, infos)) + self.lwe_to_glwe_switching_key_prepared_alloc(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self - where - Module: VmpPMatAlloc, - { - Self(GLWESwitchingKeyPrepared::alloc( - module, - base2k, - k, - Rank(1), - rank_out, - dnum, - Dsize(1), - )) + fn lwe_to_glwe_switching_key_prepared_alloc_bytes( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_out: Rank, + dnum: Dnum, + ) -> usize { + self.glwe_switching_key_prepared_alloc_bytes(base2k, k, Rank(1), rank_out, dnum, Dsize(1)) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + fn lwe_to_glwe_switching_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, { debug_assert_eq!( infos.rank_in().0, @@ -103,67 +108,73 @@ impl LWEToGLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - GLWESwitchingKeyPrepared::alloc_bytes_from_infos(module, infos) - } - - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize - where - Module: VmpPMatAllocBytes, - { - GLWESwitchingKeyPrepared::alloc_bytes(module, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + self.lwe_to_glwe_switching_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) } } -pub trait LWEToGLWESwitchingKeyPrepareTmpBytes { - fn lwe_to_glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) - where - A: GGLWEInfos; -} +impl LWEToGLWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} -impl LWEToGLWESwitchingKeyPrepareTmpBytes for Module +impl LWEToGLWESwitchingKeyPrepared, B> where - Module: LWEToGLWESwitchingKeyPrepareTmpBytes, + Module: LWEToGLWESwitchingKeyPreparedAlloc, +{ + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + { + module.lwe_to_glwe_switching_key_prepared_alloc_from_infos(infos) + } + + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { + module.lwe_to_glwe_switching_key_prepared_alloc(base2k, k, rank_out, dnum) + } + + pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + { + module.lwe_to_glwe_switching_key_prepared_alloc_bytes_from_infos(infos) + } + + pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize { + module.lwe_to_glwe_switching_key_prepared_alloc_bytes(base2k, k, rank_out, dnum) + } +} + +pub trait LWEToGLWESwitchingKeyPrepare +where + Self: GLWESwitchingKeyPrepare, { fn lwe_to_glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) where A: GGLWEInfos, { - self.lwe_to_glwe_switching_key_prepare_tmp_bytes(infos); + self.glwe_switching_key_prepare_tmp_bytes(infos); } -} -impl LWEToGLWESwitchingKeyPrepared, B> { - pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) - where - A: GLWEInfos, - Module: LWEToGLWESwitchingKeyPrepareTmpBytes, - { - module.glwe_secret_prepare_tmp_bytes(infos); - } -} - -pub trait LWEToGLWESwitchingKeyPrepare { - fn lwe_to_glwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) - where - R: LWEToGLWESwitchingKeyPreparedToMut, - O: LWEToGLWESwitchingKeyToRef; -} - -impl LWEToGLWESwitchingKeyPrepare for Module -where - Module: GLWESwitchingKeyPrepare, -{ - fn lwe_to_glwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + fn lwe_to_glwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: LWEToGLWESwitchingKeyPreparedToMut, O: LWEToGLWESwitchingKeyToRef, { - self.glwe_switching_prepare(&mut res.to_mut().0, other, scratch); + self.glwe_switching_prepare(&mut res.to_mut().0, &other.to_ref().0, scratch); + } +} + +impl LWEToGLWESwitchingKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} + +impl LWEToGLWESwitchingKeyPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) + where + A: GGLWEInfos, + Module: LWEToGLWESwitchingKeyPrepare, + { + module.lwe_to_glwe_switching_key_prepare_tmp_bytes(infos); } } impl LWEToGLWESwitchingKeyPrepared { - fn prepare(&mut self, module: &Module, other: &O, scratch: &Scratch) + fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) where O: LWEToGLWESwitchingKeyToRef, Module: LWEToGLWESwitchingKeyPrepare, @@ -172,26 +183,6 @@ impl LWEToGLWESwitchingKeyPrepared { } } -pub trait LWEToGLWESwitchingKeyPrepareAlloc { - fn lwe_to_glwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: LWEToGLWESwitchingKeyToRef; -} - -impl LWEToGLWESwitchingKeyPrepareAlloc for Module -where - Module: LWEToGLWESwitchingKeyPrepare, -{ - fn lwe_to_glwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: LWEToGLWESwitchingKeyToRef, - { - let mut ct_prep: LWEToGLWESwitchingKeyPrepared, B> = LWEToGLWESwitchingKeyPrepared::alloc(self, &other.to_ref()); - self.lwe_to_glwe_switching_key_prepare(&mut ct_prep, other, scratch); - ct_prep - } -} - pub trait LWEToGLWESwitchingKeyPreparedToRef { fn to_ref(&self) -> LWEToGLWESwitchingKeyPrepared<&[u8], B>; } diff --git a/poulpy-core/src/scratch.rs b/poulpy-core/src/scratch.rs index 0a94b39..01b260e 100644 --- a/poulpy-core/src/scratch.rs +++ b/poulpy-core/src/scratch.rs @@ -129,12 +129,11 @@ where { let (data, scratch) = self.take_vec_znx(infos.n().into(), (infos.rank() + 1).into(), infos.size()); ( - GLWECiphertext::builder() - .base2k(infos.base2k()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GLWECiphertext { + k: infos.k(), + base2k: infos.base2k(), + data, + }, scratch, ) } @@ -169,12 +168,11 @@ where { let (data, scratch) = self.take_vec_znx(infos.n().into(), 1, infos.size()); ( - GLWEPlaintext::builder() - .base2k(infos.base2k()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GLWEPlaintext { + k: infos.k(), + base2k: infos.base2k(), + data, + }, scratch, ) } @@ -196,13 +194,12 @@ where infos.size(), ); ( - GGLWE::builder() - .base2k(infos.base2k()) - .k(infos.k()) - .dsize(infos.dsize()) - .data(data) - .build() - .unwrap(), + GGLWE { + k: infos.k(), + base2k: infos.base2k(), + dsize: infos.dsize(), + data, + }, scratch, ) } @@ -224,13 +221,12 @@ where infos.size(), ); ( - GGLWEPrepared::builder() - .base2k(infos.base2k()) - .dsize(infos.dsize()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GGLWEPrepared { + k: infos.k(), + base2k: infos.base2k(), + dsize: infos.dsize(), + data, + }, scratch, ) } @@ -252,13 +248,12 @@ where infos.size(), ); ( - GGSW::builder() - .base2k(infos.base2k()) - .dsize(infos.dsize()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GGSW { + k: infos.k(), + base2k: infos.base2k(), + dsize: infos.dsize(), + data, + }, scratch, ) } @@ -280,13 +275,12 @@ where infos.size(), ); ( - GGSWPrepared::builder() - .base2k(infos.base2k()) - .dsize(infos.dsize()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GGSWPrepared { + k: infos.k(), + base2k: infos.base2k(), + dsize: infos.dsize(), + data, + }, scratch, ) } @@ -321,13 +315,12 @@ where { let (data, scratch) = self.take_vec_znx(infos.n().into(), (infos.rank() + 1).into(), infos.size()); ( - GLWEPublicKey::builder() - .base2k(infos.base2k()) - .k(infos.k()) - .base2k(infos.base2k()) - .data(data) - .build() - .unwrap(), + GLWEPublicKey { + k: infos.k(), + dist: Distribution::NONE, + base2k: infos.base2k(), + data, + }, scratch, ) } @@ -343,12 +336,12 @@ where { let (data, scratch) = self.take_vec_znx_dft(infos.n().into(), (infos.rank() + 1).into(), infos.size()); ( - GLWEPublicKeyPrepared::builder() - .base2k(infos.base2k()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GLWEPublicKeyPrepared { + k: infos.k(), + dist: Distribution::NONE, + base2k: infos.base2k(), + data, + }, scratch, ) } diff --git a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs index 5b066a1..b63d616 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -182,7 +182,7 @@ where AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc(module, &tensor_key_layout); + let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct_out.automorphism( @@ -362,7 +362,7 @@ where AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc(module, &tensor_key_layout); + let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct.automorphism_inplace(module, &auto_key_prepared, &tsk_prepared, scratch.borrow()); diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs index 3213894..1af36d1 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs @@ -101,7 +101,7 @@ where { Self { blocks: (0..T::WORD_SIZE) - .map(|_| GGSWPrepared::alloc_with(module, base2k, k, dnum, dsize, rank)) + .map(|_| GGSWPrepared::alloc(module, base2k, k, dnum, dsize, rank)) .collect(), _base: 1, _phantom: PhantomData, diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs index a8fa39a..22e9bd4 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs @@ -122,7 +122,7 @@ where A: BlindRotationKeyInfos, { let mut data: Vec, B>> = Vec::with_capacity(infos.n_lwe().into()); - (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWPrepared::alloc(module, infos))); + (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWPrepared::alloc_from_infos(module, infos))); Self { data, dist: Distribution::NONE,