use std::fmt; use poulpy_hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; use crate::layouts::{ Base2K, BuildError, Degree, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, GLWELayoutSet, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWEPlaintextLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, } impl LWEInfos for GLWEPlaintextLayout { fn base2k(&self) -> Base2K { self.base2k } fn k(&self) -> TorusPrecision { self.k } fn n(&self) -> Degree { self.n } } impl GLWEInfos for GLWEPlaintextLayout { fn rank(&self) -> Rank { Rank(0) } } pub struct GLWEPlaintext { pub data: VecZnx, pub base2k: Base2K, pub k: TorusPrecision, } impl GLWELayoutSet for GLWEPlaintext { fn set_basek(&mut self, base2k: Base2K) { self.base2k = base2k } fn set_k(&mut self, k: TorusPrecision) { self.k = k } } impl LWEInfos for GLWEPlaintext { fn base2k(&self) -> Base2K { self.base2k } fn k(&self) -> TorusPrecision { self.k } fn size(&self) -> usize { self.data.size() } fn n(&self) -> Degree { Degree(self.data.n() as u32) } } impl GLWEInfos for GLWEPlaintext { fn rank(&self) -> Rank { Rank(self.data.cols() as u32 - 1) } } pub struct GLWEPlaintextBuilder { data: Option>, base2k: Option, k: Option, } impl GLWEPlaintext { #[inline] pub fn builder() -> GLWEPlaintextBuilder { GLWEPlaintextBuilder { data: None, base2k: None, k: None, } } } impl GLWEPlaintextBuilder { #[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 == 0 { return Err(BuildError::ZeroBase2K); } if k.0 == 0 { return Err(BuildError::ZeroTorusPrecision); } if data.n() == 0 { return Err(BuildError::ZeroDegree); } if data.cols() != 1 { return Err(BuildError::ZeroCols); } if data.size() == 0 { return Err(BuildError::ZeroLimbs); } Ok(GLWEPlaintext { data, base2k, k }) } } impl fmt::Display for GLWEPlaintext { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "GLWEPlaintext: base2k={} k={}: {}", self.base2k().0, self.k().0, self.data ) } } impl GLWEPlaintext> { pub fn alloc(infos: &A) -> Self where A: GLWEInfos, { Self::alloc_with(infos.n(), infos.base2k(), infos.k(), Rank(0)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { debug_assert!(rank.0 == 0); Self { data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), base2k, k, } } pub fn alloc_bytes(infos: &A) -> usize where A: GLWEInfos, { Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k(), Rank(0)) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { debug_assert!(rank.0 == 0); VecZnx::alloc_bytes(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize) } } impl GLWECiphertextToRef for GLWEPlaintext { fn to_ref(&self) -> GLWECiphertext<&[u8]> { GLWECiphertext::builder() .data(self.data.to_ref()) .k(self.k()) .base2k(self.base2k()) .build() .unwrap() } } impl GLWECiphertextToMut for GLWEPlaintext { fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> { GLWECiphertext::builder() .k(self.k()) .base2k(self.base2k()) .data(self.data.to_mut()) .build() .unwrap() } }