use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos}; use crate::{ dist::Distribution, layouts::{Base2K, BuildError, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; #[derive(PartialEq, Eq)] pub struct GLWEPublicKey { pub(crate) data: VecZnx, pub(crate) base2k: Base2K, pub(crate) k: TorusPrecision, pub(crate) dist: Distribution, } #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWEPublicKeyLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, } impl LWEInfos for GLWEPublicKey { fn base2k(&self) -> Base2K { self.base2k } fn k(&self) -> TorusPrecision { self.k } fn n(&self) -> Degree { Degree(self.data.n() as u32) } fn size(&self) -> usize { self.data.size() } } impl GLWEInfos for GLWEPublicKey { fn rank(&self) -> Rank { Rank(self.data.cols() as u32 - 1) } } impl LWEInfos for GLWEPublicKeyLayout { fn base2k(&self) -> Base2K { self.base2k } fn k(&self) -> TorusPrecision { self.k } fn n(&self) -> Degree { self.n } fn size(&self) -> usize { self.k.0.div_ceil(self.base2k.0) as usize } } impl GLWEInfos for GLWEPublicKeyLayout { fn rank(&self) -> Rank { self.rank } } 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 A: GLWEInfos, { Self::alloc_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { Self { data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), base2k, k, dist: Distribution::NONE, } } pub fn alloc_bytes(infos: &A) -> usize where A: GLWEInfos, { Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { VecZnx::alloc_bytes(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize) } } impl ReaderFrom for GLWEPublicKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); match Distribution::read_from(reader) { Ok(dist) => self.dist = dist, Err(e) => return Err(e), } self.data.read_from(reader) } } impl WriterTo for GLWEPublicKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.0)?; writer.write_u32::(self.base2k.0)?; match self.dist.write_to(writer) { Ok(()) => {} Err(e) => return Err(e), } self.data.write_to(writer) } } pub trait GLWEPublicKeyToRef { fn to_ref(&self) -> GLWEPublicKey<&[u8]>; } impl GLWEPublicKeyToRef for GLWEPublicKey { fn to_ref(&self) -> GLWEPublicKey<&[u8]> { GLWEPublicKey { data: self.data.to_ref(), base2k: self.base2k, k: self.k, dist: self.dist, } } } pub trait GLWEPublicKeyToMut { fn to_mut(&mut self) -> GLWEPublicKey<&mut [u8]>; } impl GLWEPublicKeyToMut for GLWEPublicKey { fn to_mut(&mut self) -> GLWEPublicKey<&mut [u8]> { GLWEPublicKey { base2k: self.base2k, k: self.k, dist: self.dist, data: self.data.to_mut(), } } }