use poulpy_hal::{ layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, ReaderFrom, WriterTo, ZnxInfos}, source::Source, }; use crate::layouts::{Base2K, BuildError, Degree, Digits, GLWECiphertext, GLWEInfos, LWEInfos, Rank, Rows, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; pub trait GGLWELayoutInfos where Self: GLWEInfos, { fn rows(&self) -> Rows; fn digits(&self) -> Digits; fn rank_in(&self) -> Rank; fn rank_out(&self) -> Rank; fn layout(&self) -> GGLWECiphertextLayout { GGLWECiphertextLayout { n: self.n(), base2k: self.base2k(), k: self.k(), rank_in: self.rank_in(), rank_out: self.rank_out(), digits: self.digits(), rows: self.rows(), } } } #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GGLWECiphertextLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rows: Rows, pub digits: Digits, pub rank_in: Rank, pub rank_out: Rank, } impl LWEInfos for GGLWECiphertextLayout { fn base2k(&self) -> Base2K { self.base2k } fn k(&self) -> TorusPrecision { self.k } fn n(&self) -> Degree { self.n } } impl GLWEInfos for GGLWECiphertextLayout { fn rank(&self) -> Rank { self.rank_out } } impl GGLWELayoutInfos for GGLWECiphertextLayout { fn rank_in(&self) -> Rank { self.rank_in } fn digits(&self) -> Digits { self.digits } fn rank_out(&self) -> Rank { self.rank_out } fn rows(&self) -> Rows { self.rows } } #[derive(PartialEq, Eq, Clone)] pub struct GGLWECiphertext { pub(crate) data: MatZnx, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) digits: Digits, } impl LWEInfos for GGLWECiphertext { 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 GGLWECiphertext { fn rank(&self) -> Rank { self.rank_out() } } impl GGLWELayoutInfos for GGLWECiphertext { fn rank_in(&self) -> Rank { Rank(self.data.cols_in() as u32) } fn rank_out(&self) -> Rank { Rank(self.data.cols_out() as u32 - 1) } fn digits(&self) -> Digits { self.digits } fn rows(&self) -> Rows { Rows(self.data.rows() as u32) } } pub struct GGLWECiphertextBuilder { data: Option>, base2k: Option, k: Option, digits: Option, } impl GGLWECiphertext { #[inline] pub fn builder() -> GGLWECiphertextBuilder { GGLWECiphertextBuilder { data: None, base2k: None, k: None, digits: None, } } } impl GGLWECiphertextBuilder> { #[inline] pub fn layout(mut self, infos: &A) -> Self where A: GGLWELayoutInfos, { self.data = Some(MatZnx::alloc( infos.n().into(), infos.rows().into(), infos.rank_in().into(), (infos.rank_out() + 1).into(), infos.size(), )); self.base2k = Some(infos.base2k()); self.k = Some(infos.k()); self.digits = Some(infos.digits()); self } } impl GGLWECiphertextBuilder { #[inline] pub fn data(mut self, data: MatZnx) -> 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 } #[inline] pub fn digits(mut self, digits: Digits) -> Self { self.digits = Some(digits); self } pub fn build(self) -> Result, BuildError> { let data: MatZnx = self.data.ok_or(BuildError::MissingData)?; let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; let digits: Digits = self.digits.ok_or(BuildError::MissingDigits)?; if base2k == 0_u32 { return Err(BuildError::ZeroBase2K); } if digits == 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(GGLWECiphertext { data, base2k, k, digits, }) } } impl GGLWECiphertext { pub fn data(&self) -> &MatZnx { &self.data } } impl GGLWECiphertext { pub fn data_mut(&mut self) -> &mut MatZnx { &mut self.data } } impl fmt::Debug for GGLWECiphertext { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } impl FillUniform for GGLWECiphertext { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } impl fmt::Display for GGLWECiphertext { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "(GGLWECiphertext: k={} base2k={} digits={}) {}", self.k().0, self.base2k().0, self.digits().0, self.data ) } } impl GGLWECiphertext { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { GLWECiphertext::builder() .data(self.data.at(row, col)) .base2k(self.base2k()) .k(self.k()) .build() .unwrap() } } impl GGLWECiphertext { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { GLWECiphertext::builder() .base2k(self.base2k()) .k(self.k()) .data(self.data.at_mut(row, col)) .build() .unwrap() } } impl GGLWECiphertext> { pub fn alloc(infos: &A) -> Self where A: GGLWELayoutInfos, { Self::alloc_with( infos.n(), infos.base2k(), infos.k(), infos.rows(), infos.digits(), infos.rank_in(), infos.rank_out(), ) } pub fn alloc_with( n: Degree, base2k: Base2K, k: TorusPrecision, rows: Rows, digits: Digits, rank_in: Rank, rank_out: Rank, ) -> Self { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > digits.0, "invalid gglwe: ceil(k/base2k): {size} <= digits: {}", digits.0 ); assert!( rows.0 * digits.0 <= size as u32, "invalid gglwe: rows: {} * digits:{} > ceil(k/base2k): {size}", rows.0, digits.0, ); Self { data: MatZnx::alloc( n.into(), rows.into(), rank_in.into(), (rank_out + 1).into(), k.0.div_ceil(base2k.0) as usize, ), k, base2k, digits, } } pub fn alloc_bytes(infos: &A) -> usize where A: GGLWELayoutInfos, { Self::alloc_bytes_with( infos.n(), infos.base2k(), infos.k(), infos.rows(), infos.digits(), infos.rank_in(), infos.rank_out(), ) } pub fn alloc_bytes_with( n: Degree, base2k: Base2K, k: TorusPrecision, rows: Rows, digits: Digits, rank_in: Rank, rank_out: Rank, ) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > digits.0, "invalid gglwe: ceil(k/base2k): {size} <= digits: {}", digits.0 ); assert!( rows.0 * digits.0 <= size as u32, "invalid gglwe: rows: {} * digits:{} > ceil(k/base2k): {size}", rows.0, digits.0, ); MatZnx::alloc_bytes( n.into(), rows.into(), rank_in.into(), (rank_out + 1).into(), k.0.div_ceil(base2k.0) as usize, ) } } impl ReaderFrom for GGLWECiphertext { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); self.digits = Digits(reader.read_u32::()?); self.data.read_from(reader) } } impl WriterTo for GGLWECiphertext { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.0)?; writer.write_u32::(self.base2k.0)?; writer.write_u32::(self.digits.0)?; self.data.write_to(writer) } }