use std::fmt; use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; use crate::layouts::{ Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetDegree, 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 ) } } pub trait GLWEPlaintextAlloc where Self: GetDegree, { fn alloc_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> GLWEPlaintext> { GLWEPlaintext { data: VecZnx::alloc(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize), base2k, k, } } fn alloc_glwe_plaintext_from_infos(&self, infos: &A) -> GLWEPlaintext> where A: GLWEInfos, { self.alloc_glwe_plaintext(infos.base2k(), infos.k()) } fn bytes_of_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> usize { VecZnx::bytes_of(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize) } fn bytes_of_glwe_plaintext_from_infos(&self, infos: &A) -> usize where A: GLWEInfos, { self.bytes_of_glwe_plaintext(infos.base2k(), infos.k()) } } impl GLWEPlaintext> { pub fn alloc_from_infos(module: Module, infos: &A) -> Self where A: GLWEInfos, Module: GLWEPlaintextAlloc, { module.alloc_glwe_plaintext_from_infos(infos) } pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision) -> Self where Module: GLWEPlaintextAlloc, { module.alloc_glwe_plaintext(base2k, k) } pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize where A: GLWEInfos, Module: GLWEPlaintextAlloc, { module.bytes_of_glwe_plaintext_from_infos(infos) } pub fn bytes_of(module: Module, base2k: Base2K, k: TorusPrecision) -> usize where Module: GLWEPlaintextAlloc, { module.bytes_of_glwe_plaintext(base2k, k) } } 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(), } } }