use poulpy_hal::layouts::{ Backend, Data, DataMut, DataRef, Module, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos, }; use crate::{ dist::Distribution, layouts::{Base2K, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, 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: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, 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 } fn k(&self) -> TorusPrecision { self.k } fn n(&self) -> RingDegree { RingDegree(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) -> RingDegree { 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 trait GLWEPublicKeyAlloc where Self: GetRingDegree, { fn alloc_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKey> { GLWEPublicKey { data: VecZnx::alloc( self.ring_degree().into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize, ), base2k, k, dist: Distribution::NONE, } } fn alloc_glwe_public_key_from_infos(&self, infos: &A) -> GLWEPublicKey> where A: GLWEInfos, { self.alloc_glwe_public_key(infos.base2k(), infos.k(), infos.rank()) } fn bytes_of_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { VecZnx::bytes_of( self.ring_degree().into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize, ) } fn bytes_of_glwe_public_key_from_infos(&self, infos: &A) -> usize where A: GLWEInfos, { self.bytes_of_glwe_public_key(infos.base2k(), infos.k(), infos.rank()) } } impl GLWEPublicKeyAlloc for Module where Self: GetRingDegree {} impl GLWEPublicKey> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GLWEInfos, M: GLWEPublicKeyAlloc, { module.alloc_glwe_public_key_from_infos(infos) } pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self where M: GLWEPublicKeyAlloc, { module.alloc_glwe_public_key(base2k, k, rank) } pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GLWEInfos, M: GLWEPublicKeyAlloc, { module.bytes_of_glwe_public_key_from_infos(infos) } pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize where M: GLWEPublicKeyAlloc, { module.bytes_of_glwe_public_key(base2k, k, rank) } } 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(), } } }