use poulpy_hal::{ layouts::{ Data, DataMut, DataRef, FillUniform, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos, }, source::Source, }; use crate::layouts::{Base2K, Degree, LWEInfos, Rank, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; pub trait GLWEInfos where Self: LWEInfos, { fn rank(&self) -> Rank; fn glwe_layout(&self) -> GLWELayout { GLWELayout { n: self.n(), base2k: self.base2k(), k: self.k(), rank: self.rank(), } } } pub trait SetGLWEInfos { fn set_k(&mut self, k: TorusPrecision); fn set_base2k(&mut self, base2k: Base2K); } #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWELayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, } impl LWEInfos for GLWELayout { fn n(&self) -> Degree { self.n } fn base2k(&self) -> Base2K { self.base2k } fn k(&self) -> TorusPrecision { self.k } } impl GLWEInfos for GLWELayout { fn rank(&self) -> Rank { self.rank } } #[derive(PartialEq, Eq, Clone)] pub struct GLWE { pub(crate) data: VecZnx, pub(crate) base2k: Base2K, pub(crate) k: TorusPrecision, } impl SetGLWEInfos for GLWE { fn set_base2k(&mut self, base2k: Base2K) { self.base2k = base2k } fn set_k(&mut self, k: TorusPrecision) { self.k = k } } impl GLWE { pub fn data(&self) -> &VecZnx { &self.data } } impl GLWE { pub fn data_mut(&mut self) -> &mut VecZnx { &mut self.data } } impl LWEInfos for GLWE { 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 GLWE { fn rank(&self) -> Rank { Rank(self.data.cols() as u32 - 1) } } impl ToOwnedDeep for GLWE { type Owned = GLWE>; fn to_owned_deep(&self) -> Self::Owned { GLWE { data: self.data.to_owned_deep(), k: self.k, base2k: self.base2k, } } } impl fmt::Debug for GLWE { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } impl fmt::Display for GLWE { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "GLWE: base2k={} k={}: {}", self.base2k().0, self.k().0, self.data) } } impl FillUniform for GLWE { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } impl GLWE> { pub fn alloc_from_infos(infos: &A) -> Self where A: GLWEInfos, { Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank()) } pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { GLWE { data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), base2k, k, } } pub fn bytes_of_from_infos(infos: &A) -> usize where A: GLWEInfos, { Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.rank()) } pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { VecZnx::bytes_of(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize) } } impl ReaderFrom for GLWE { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); self.data.read_from(reader) } } impl WriterTo for GLWE { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.0)?; writer.write_u32::(self.base2k.0)?; self.data.write_to(writer) } } pub trait GLWEToRef: Sized { fn to_ref(&self) -> GLWE<&[u8]>; } impl GLWEToRef for GLWE { fn to_ref(&self) -> GLWE<&[u8]> { GLWE { k: self.k, base2k: self.base2k, data: self.data.to_ref(), } } } pub trait GLWEToMut: GLWEToRef { fn to_mut(&mut self) -> GLWE<&mut [u8]>; } impl GLWEToMut for GLWE { fn to_mut(&mut self) -> GLWE<&mut [u8]> { GLWE { k: self.k, base2k: self.base2k, data: self.data.to_mut(), } } }