use poulpy_hal::{ api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::{Decompress, GGLWESwitchingKeyCompressed}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] pub struct GGLWEAutomorphismKeyCompressed { pub(crate) key: GGLWESwitchingKeyCompressed, pub(crate) p: i64, } impl LWEInfos for GGLWEAutomorphismKeyCompressed { fn n(&self) -> Degree { self.key.n() } fn base2k(&self) -> Base2K { self.key.base2k() } fn k(&self) -> TorusPrecision { self.key.k() } fn size(&self) -> usize { self.key.size() } } impl GLWEInfos for GGLWEAutomorphismKeyCompressed { fn rank(&self) -> Rank { self.rank_out() } } impl GGLWEInfos for GGLWEAutomorphismKeyCompressed { fn rank_in(&self) -> Rank { self.key.rank_in() } fn rank_out(&self) -> Rank { self.key.rank_out() } fn dsize(&self) -> Dsize { self.key.dsize() } fn dnum(&self) -> Dnum { self.key.dnum() } } impl fmt::Debug for GGLWEAutomorphismKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } impl FillUniform for GGLWEAutomorphismKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } impl fmt::Display for GGLWEAutomorphismKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(AutomorphismKeyCompressed: p={}) {}", self.p, self.key) } } impl GGLWEAutomorphismKeyCompressed> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, { debug_assert_eq!(infos.rank_in(), infos.rank_out()); Self { key: GGLWESwitchingKeyCompressed::alloc(infos), p: 0, } } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { Self { key: GGLWESwitchingKeyCompressed::alloc_with(n, base2k, k, rank, rank, dnum, dsize), p: 0, } } pub fn alloc_bytes(infos: &A) -> usize where A: GGLWEInfos, { debug_assert_eq!(infos.rank_in(), infos.rank_out()); GGLWESwitchingKeyCompressed::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank, dnum, dsize) } } impl ReaderFrom for GGLWEAutomorphismKeyCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.p = reader.read_u64::()? as i64; self.key.read_from(reader) } } impl WriterTo for GGLWEAutomorphismKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.p as u64)?; self.key.write_to(writer) } } impl Decompress> for GGLWEAutomorphismKey where Module: VecZnxFillUniform + VecZnxCopy, { fn decompress(&mut self, module: &Module, other: &GGLWEAutomorphismKeyCompressed) { self.key.decompress(module, &other.key); self.p = other.p; } }