use poulpy_hal::{ layouts::{Data, DataMut, DataRef, ReaderFrom, ScalarZnx, WriterTo, ZnxInfos, ZnxZero}, source::Source, }; use crate::{ dist::Distribution, layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWESecretLayout { pub n: Degree, pub rank: Rank, } impl LWEInfos for GLWESecretLayout { fn base2k(&self) -> Base2K { Base2K(0) } fn k(&self) -> TorusPrecision { TorusPrecision(0) } fn n(&self) -> Degree { self.n } fn size(&self) -> usize { 1 } } impl GLWEInfos for GLWESecretLayout { fn rank(&self) -> Rank { self.rank } } #[derive(PartialEq, Eq, Clone)] pub struct GLWESecret { pub(crate) data: ScalarZnx, pub(crate) dist: Distribution, } impl LWEInfos for GLWESecret { fn base2k(&self) -> Base2K { Base2K(0) } fn k(&self) -> TorusPrecision { TorusPrecision(0) } fn n(&self) -> Degree { Degree(self.data.n() as u32) } fn size(&self) -> usize { 1 } } impl GLWEInfos for GLWESecret { fn rank(&self) -> Rank { Rank(self.data.cols() as u32) } } impl GLWESecret> { pub fn alloc(infos: &A) -> Self where A: GLWEInfos, { Self::alloc_with(infos.n(), infos.rank()) } pub fn alloc_with(n: Degree, rank: Rank) -> Self { Self { data: ScalarZnx::alloc(n.into(), rank.into()), dist: Distribution::NONE, } } pub fn alloc_bytes(infos: &A) -> usize where A: GLWEInfos, { Self::alloc_bytes_with(infos.n(), infos.rank()) } pub fn alloc_bytes_with(n: Degree, rank: Rank) -> usize { ScalarZnx::alloc_bytes(n.into(), rank.into()) } } impl GLWESecret { pub fn fill_ternary_prob(&mut self, prob: f64, source: &mut Source) { (0..self.rank().into()).for_each(|i| { self.data.fill_ternary_prob(i, prob, source); }); self.dist = Distribution::TernaryProb(prob); } pub fn fill_ternary_hw(&mut self, hw: usize, source: &mut Source) { (0..self.rank().into()).for_each(|i| { self.data.fill_ternary_hw(i, hw, source); }); self.dist = Distribution::TernaryFixed(hw); } pub fn fill_binary_prob(&mut self, prob: f64, source: &mut Source) { (0..self.rank().into()).for_each(|i| { self.data.fill_binary_prob(i, prob, source); }); self.dist = Distribution::BinaryProb(prob); } pub fn fill_binary_hw(&mut self, hw: usize, source: &mut Source) { (0..self.rank().into()).for_each(|i| { self.data.fill_binary_hw(i, hw, source); }); self.dist = Distribution::BinaryFixed(hw); } pub fn fill_binary_block(&mut self, block_size: usize, source: &mut Source) { (0..self.rank().into()).for_each(|i| { self.data.fill_binary_block(i, block_size, source); }); self.dist = Distribution::BinaryBlock(block_size); } pub fn fill_zero(&mut self) { self.data.zero(); self.dist = Distribution::ZERO; } } impl ReaderFrom for GLWESecret { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { match Distribution::read_from(reader) { Ok(dist) => self.dist = dist, Err(e) => return Err(e), } self.data.read_from(reader) } } impl WriterTo for GLWESecret { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { match self.dist.write_to(writer) { Ok(()) => {} Err(e) => return Err(e), } self.data.write_to(writer) } }