use poulpy_hal::{ layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use std::{fmt, marker::PhantomData}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use poulpy_core::{ Distribution, layouts::{Base2K, Degree, Dsize, GGSWInfos, GLWEInfos, LWEInfos, TorusPrecision, compressed::GGSWCompressed}, }; use crate::bin_fhe::blind_rotation::{BlindRotationAlgo, BlindRotationKeyInfos}; #[derive(Clone)] pub struct BlindRotationKeyCompressed { pub(crate) keys: Vec>, pub(crate) dist: Distribution, pub(crate) _phantom: PhantomData, } pub trait BlindRotationKeyCompressedFactory { fn blind_rotation_key_compressed_alloc(infos: &A) -> BlindRotationKeyCompressed, BRA> where A: BlindRotationKeyInfos; } impl BlindRotationKeyCompressed, BRA> where Self: BlindRotationKeyCompressedFactory, { pub fn alloc(infos: &A) -> BlindRotationKeyCompressed, BRA> where A: BlindRotationKeyInfos, { Self::blind_rotation_key_compressed_alloc(infos) } } impl fmt::Debug for BlindRotationKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } impl PartialEq for BlindRotationKeyCompressed { fn eq(&self, other: &Self) -> bool { if self.keys.len() != other.keys.len() { return false; } for (a, b) in self.keys.iter().zip(other.keys.iter()) { if a != b { return false; } } self.dist == other.dist && self._phantom == other._phantom } } impl Eq for BlindRotationKeyCompressed {} impl fmt::Display for BlindRotationKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { for (i, key) in self.keys.iter().enumerate() { write!(f, "key[{i}]: {key}")?; } writeln!(f, "{:?}", self.dist) } } impl FillUniform for BlindRotationKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.keys .iter_mut() .for_each(|key| key.fill_uniform(log_bound, source)); } } impl ReaderFrom for BlindRotationKeyCompressed { 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), } let len: usize = reader.read_u64::()? as usize; if self.keys.len() != len { return Err(std::io::Error::new( std::io::ErrorKind::InvalidData, format!("self.keys.len()={} != read len={}", self.keys.len(), len), )); } for key in &mut self.keys { key.read_from(reader)?; } Ok(()) } } impl WriterTo for BlindRotationKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { match self.dist.write_to(writer) { Ok(()) => {} Err(e) => return Err(e), } writer.write_u64::(self.keys.len() as u64)?; for key in &self.keys { key.write_to(writer)?; } Ok(()) } } impl BlindRotationKeyInfos for BlindRotationKeyCompressed { fn n_glwe(&self) -> Degree { self.n() } fn n_lwe(&self) -> Degree { Degree(self.keys.len() as u32) } } impl LWEInfos for BlindRotationKeyCompressed { fn n(&self) -> Degree { self.keys[0].n() } fn size(&self) -> usize { self.keys[0].size() } fn k(&self) -> TorusPrecision { self.keys[0].k() } fn base2k(&self) -> Base2K { self.keys[0].base2k() } } impl GLWEInfos for BlindRotationKeyCompressed { fn rank(&self) -> poulpy_core::layouts::Rank { self.keys[0].rank() } } impl GGSWInfos for BlindRotationKeyCompressed { fn dnum(&self) -> poulpy_core::layouts::Dnum { self.keys[0].dnum() } fn dsize(&self) -> poulpy_core::layouts::Dsize { Dsize(1) } } impl BlindRotationKeyCompressed { #[allow(dead_code)] pub(crate) fn block_size(&self) -> usize { match self.dist { Distribution::BinaryBlock(value) => value, _ => 1, } } }