use poulpy_hal::{ api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ Base2K, Degree, Digits, GGLWEAutomorphismKey, GGLWELayoutInfos, GLWEInfos, LWEInfos, Rank, Rows, 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 GGLWELayoutInfos for GGLWEAutomorphismKeyCompressed { fn rank_in(&self) -> Rank { self.key.rank_in() } fn rank_out(&self) -> Rank { self.key.rank_out() } fn digits(&self) -> Digits { self.key.digits() } fn rows(&self) -> Rows { self.key.rows() } } 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: GGLWELayoutInfos, { 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, rows: Rows, digits: Digits, rank: Rank) -> Self { Self { key: GGLWESwitchingKeyCompressed::alloc_with(n, base2k, k, rows, digits, rank, rank), p: 0, } } pub fn alloc_bytes(infos: &A) -> usize where A: GGLWELayoutInfos, { debug_assert_eq!(infos.rank_in(), infos.rank_out()); GGLWESwitchingKeyCompressed::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rows: Rows, digits: Digits, rank: Rank) -> usize { GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rows, digits, rank, rank) } } 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; } }