use std::fmt; use poulpy_hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; use crate::layouts::{Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, Rank, SetGLWEInfos, 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 SetGLWEInfos for GLWEPlaintext { fn set_base2k(&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) } } 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_from_infos(infos: &A) -> Self where A: GLWEInfos, { Self::alloc(infos.n(), infos.base2k(), infos.k()) } pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision) -> Self { GLWEPlaintext { data: VecZnx::alloc(n.into(), 1, 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()) } pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { VecZnx::bytes_of(n.into(), 1, k.0.div_ceil(base2k.0) as usize) } } impl GLWEToRef for GLWEPlaintext { fn to_ref(&self) -> GLWE<&[u8]> { GLWE { k: self.k, base2k: self.base2k, data: self.data.to_ref(), } } } impl GLWEToMut for GLWEPlaintext { fn to_mut(&mut self) -> GLWE<&mut [u8]> { GLWE { k: self.k, base2k: self.base2k, data: self.data.to_mut(), } } } pub trait GLWEPlaintextToRef { fn to_ref(&self) -> GLWEPlaintext<&[u8]>; } impl GLWEPlaintextToRef for GLWEPlaintext { fn to_ref(&self) -> GLWEPlaintext<&[u8]> { GLWEPlaintext { data: self.data.to_ref(), base2k: self.base2k, k: self.k, } } } pub trait GLWEPlaintextToMut { fn to_ref(&mut self) -> GLWEPlaintext<&mut [u8]>; } impl GLWEPlaintextToMut for GLWEPlaintext { fn to_ref(&mut self) -> GLWEPlaintext<&mut [u8]> { GLWEPlaintext { base2k: self.base2k, k: self.k, data: self.data.to_mut(), } } } impl GLWEPlaintext { pub fn data_mut(&mut self) -> &mut VecZnx { &mut self.data } } impl GLWEPlaintext { pub fn data(&self) -> &VecZnx { &self.data } }