use poulpy_hal::{ api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::{Decompress, GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] pub struct AutomorphismKeyCompressed { pub(crate) key: GLWESwitchingKeyCompressed, pub(crate) p: i64, } impl LWEInfos for AutomorphismKeyCompressed { 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 AutomorphismKeyCompressed { fn rank(&self) -> Rank { self.rank_out() } } impl GGLWEInfos for AutomorphismKeyCompressed { 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 AutomorphismKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } impl FillUniform for AutomorphismKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } impl fmt::Display for AutomorphismKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(AutomorphismKeyCompressed: p={}) {}", self.p, self.key) } } impl AutomorphismKeyCompressed> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, { debug_assert_eq!(infos.rank_in(), infos.rank_out()); Self { key: GLWESwitchingKeyCompressed::alloc(infos), p: 0, } } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { Self { key: GLWESwitchingKeyCompressed::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()); GLWESwitchingKeyCompressed::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank, dnum, dsize) } } impl ReaderFrom for AutomorphismKeyCompressed { 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 AutomorphismKeyCompressed { 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 AutomorphismKey where Module: VecZnxFillUniform + VecZnxCopy, { fn decompress(&mut self, module: &Module, other: &AutomorphismKeyCompressed) { self.key.decompress(module, &other.key); self.p = other.p; } } pub trait AutomorphismKeyCompressedToRef { fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]>; } impl AutomorphismKeyCompressedToRef for AutomorphismKeyCompressed where GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, { fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]> { AutomorphismKeyCompressed { key: self.key.to_ref(), p: self.p, } } } pub trait AutomorphismKeyCompressedToMut { fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]>; } impl AutomorphismKeyCompressedToMut for AutomorphismKeyCompressed where GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, { fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]> { AutomorphismKeyCompressed { p: self.p, key: self.key.to_mut(), } } }