use poulpy_hal::{ api::{VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf}, layouts::{Backend, Data, DataMut, DataRef, Module, VecZnxDft, VecZnxDftToMut, VecZnxDftToRef, ZnxInfos}, }; use crate::{ dist::Distribution, layouts::{ Base2K, GLWEInfos, GLWEPublicKey, GLWEPublicKeyToRef, GetDist, GetRingDegree, LWEInfos, Rank, RingDegree, 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) -> RingDegree { RingDegree(self.data.n() as u32) } } impl GLWEInfos for GLWEPublicKeyPrepared { fn rank(&self) -> Rank { Rank(self.data.cols() as u32 - 1) } } pub trait GLWEPublicKeyPreparedAlloc where Self: GetRingDegree + VecZnxDftAlloc + VecZnxDftBytesOf, { fn alloc_glwe_public_key_prepared(&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 alloc_glwe_public_key_prepared_from_infos(&self, infos: &A) -> GLWEPublicKeyPrepared, B> where A: GLWEInfos, { self.alloc_glwe_public_key_prepared(infos.base2k(), infos.k(), infos.rank()) } fn bytes_of_glwe_public_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { self.bytes_of_vec_znx_dft((rank + 1).into(), k.0.div_ceil(base2k.0) as usize) } fn bytes_of_glwe_public_key_prepared_from_infos(&self, infos: &A) -> usize where A: GLWEInfos, { self.bytes_of_glwe_public_key_prepared(infos.base2k(), infos.k(), infos.rank()) } } impl GLWEPublicKeyPreparedAlloc for Module where Self: VecZnxDftAlloc + VecZnxDftBytesOf {} impl GLWEPublicKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GLWEInfos, M: GLWEPublicKeyPreparedAlloc, { module.alloc_glwe_public_key_prepared_from_infos(infos) } pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self where M: GLWEPublicKeyPreparedAlloc, { module.alloc_glwe_public_key_prepared(base2k, k, rank) } pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GLWEInfos, M: GLWEPublicKeyPreparedAlloc, { module.bytes_of_glwe_public_key_prepared_from_infos(infos) } pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize where M: GLWEPublicKeyPreparedAlloc, { module.bytes_of_glwe_public_key_prepared(base2k, k, rank) } } pub trait GLWEPublicKeyPrepare where Self: GetRingDegree + VecZnxDftApply, { fn prepare_glwe_public_key(&self, res: &mut R, other: &O) where R: GLWEPublicKeyPreparedToMut + SetDist, O: GLWEPublicKeyToRef + GetDist, { { let mut res: GLWEPublicKeyPrepared<&mut [u8], B> = res.to_mut(); let other: GLWEPublicKey<&[u8]> = other.to_ref(); assert_eq!(res.n(), self.ring_degree()); assert_eq!(other.n(), self.ring_degree()); assert_eq!(res.size(), other.size()); assert_eq!(res.k(), other.k()); assert_eq!(res.base2k(), other.base2k()); 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.get_dist()); } } impl GLWEPublicKeyPrepare for Module where Self: GetRingDegree + VecZnxDftApply {} impl GLWEPublicKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O) where O: GLWEPublicKeyToRef + GetDist, M: GLWEPublicKeyPrepare, { module.prepare_glwe_public_key(self, other); } } 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, } } }