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, GLWEPublicKeyToRef, LWEInfos, Rank, TorusPrecision}, }; #[derive(PartialEq, Eq)] pub struct GLWEPublicKeyPrepared { pub(crate) data: VecZnxDft, pub(crate) base2k: Base2K, pub(crate) k: TorusPrecision, pub(crate) dist: Distribution, } pub(crate) trait SetDist { fn set_dist(&mut self, dist: Distribution); } impl SetDist for GLWEPublicKeyPrepared { fn set_dist(&mut self, dist: Distribution) { self.dist = dist } } 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) } } 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 where Module: VecZnxDftAlloc + VecZnxDftApply, { fn glwe_public_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) where R: GLWEPublicKeyPreparedToMut + SetDist, O: GLWEPublicKeyToRef, { { let 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.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); } } res.set_dist(other.dist); } } impl GLWEPublicKeyPrepared where Module: GLWEPublicKeyPrepare, { pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) where O: GLWEPublicKeyToRef, { 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); } } 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, } } }