use poulpy_hal::{ api::{VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VecZnxDft, VecZnxDftToMut, VecZnxDftToRef, ZnxInfos}, oep::VecZnxDftAllocBytesImpl, }; use crate::{ dist::Distribution, layouts::{ Base2K, BuildError, Degree, GLWEInfos, GLWEPublicKey, LWEInfos, Rank, TorusPrecision, prepared::{Prepare, PrepareAlloc, PrepareScratchSpace}, }, }; #[derive(PartialEq, Eq)] pub struct GLWEPublicKeyPrepared { pub(crate) data: VecZnxDft, pub(crate) base2k: Base2K, pub(crate) k: TorusPrecision, pub(crate) dist: Distribution, } impl LWEInfos for GLWEPublicKeyPrepared { fn base2k(&self) -> Base2K { self.base2k } fn k(&self) -> TorusPrecision { self.k } fn size(&self) -> usize { self.data.size() } fn n(&self) -> Degree { Degree(self.data.n() as u32) } } impl GLWEInfos for GLWEPublicKeyPrepared { fn rank(&self) -> Rank { Rank(self.data.cols() as u32 - 1) } } 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) } } impl PrepareAlloc, B>> for GLWEPublicKey where Module: VecZnxDftAlloc + VecZnxDftApply, { fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GLWEPublicKeyPrepared, B> { let mut pk_prepared: GLWEPublicKeyPrepared, B> = GLWEPublicKeyPrepared::alloc(module, self); pk_prepared.prepare(module, self, scratch); pk_prepared } } impl PrepareScratchSpace for GLWEPublicKeyPrepared, B> { fn prepare_scratch_space(_module: &Module, _infos: &A) -> usize { 0 } } impl Prepare> for GLWEPublicKeyPrepared where Module: VecZnxDftApply, { fn prepare(&mut self, module: &Module, other: &GLWEPublicKey, _scratch: &mut Scratch) { #[cfg(debug_assertions)] { assert_eq!(self.n(), other.n()); assert_eq!(self.size(), other.size()); } (0..(self.rank() + 1).into()).for_each(|i| { module.vec_znx_dft_apply(1, 0, &mut self.data, i, &other.data, i); }); self.k = other.k(); self.base2k = other.base2k(); self.dist = other.dist; } } pub trait GLWEPublicKeyPreparedToMut { fn to_mut(&mut self) -> GLWEPublicKeyPrepared<&mut [u8], B>; } impl GLWEPublicKeyPreparedToMut for GLWEPublicKeyPrepared { fn to_mut(&mut self) -> GLWEPublicKeyPrepared<&mut [u8], B> { GLWEPublicKeyPrepared { dist: self.dist, k: self.k, base2k: self.base2k, data: self.data.to_mut(), } } } pub trait GLWEPublicKeyPreparedToRef { fn to_ref(&self) -> GLWEPublicKeyPrepared<&[u8], B>; } impl GLWEPublicKeyPreparedToRef for GLWEPublicKeyPrepared { fn to_ref(&self) -> GLWEPublicKeyPrepared<&[u8], B> { GLWEPublicKeyPrepared { data: self.data.to_ref(), dist: self.dist, k: self.k, base2k: self.base2k, } } }