use poulpy_hal::{ layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::{ GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, }, prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] pub struct AutomorphismKeyCompressed { pub(crate) key: GLWESwitchingKeyCompressed, pub(crate) p: i64, } impl GetAutomorphismGaloisElement for AutomorphismKeyCompressed { fn p(&self) -> i64 { self.p } } 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 AutomorphismKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} pub trait AutomorphismKeyCompressedAlloc where Self: GLWESwitchingKeyCompressedAlloc, { fn alloc_automorphism_key_compressed( &self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize, ) -> AutomorphismKeyCompressed> { AutomorphismKeyCompressed { key: self.alloc_glwe_switching_key_compressed(base2k, k, rank, rank, dnum, dsize), p: 0, } } fn alloc_automorphism_key_compressed_from_infos(&self, infos: &A) -> AutomorphismKeyCompressed> where A: GGLWEInfos, { assert_eq!(infos.rank_in(), infos.rank_out()); self.alloc_automorphism_key_compressed( infos.base2k(), infos.k(), infos.rank(), infos.dnum(), infos.dsize(), ) } fn bytes_of_automorphism_key_compressed( &self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize, ) -> usize { self.bytes_of_glwe_switching_key_compressed(base2k, k, rank, dnum, dsize) } fn bytes_of_automorphism_key_compressed_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { assert_eq!(infos.rank_in(), infos.rank_out()); self.bytes_of_automorphism_key_compressed( infos.base2k(), infos.k(), infos.rank(), infos.dnum(), infos.dsize(), ) } } impl AutomorphismKeyCompressed> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, M: AutomorphismKeyCompressedAlloc, { module.alloc_automorphism_key_compressed_from_infos(infos) } pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where M: AutomorphismKeyCompressedAlloc, { module.alloc_automorphism_key_compressed(base2k, k, rank, dnum, dsize) } pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, M: AutomorphismKeyCompressedAlloc, { module.bytes_of_automorphism_key_compressed_from_infos(infos) } pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where M: AutomorphismKeyCompressedAlloc, { module.bytes_of_automorphism_key_compressed(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) } } pub trait AutomorphismKeyDecompress where Self: GLWESwitchingKeyDecompress, { fn decompress_automorphism_key(&self, res: &mut R, other: &O) where R: AutomorphismKeyToMut + SetAutomorphismGaloisElement, O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, { self.decompress_glwe_switching_key(&mut res.to_mut().key, &other.to_ref().key); res.set_p(other.p()); } } impl AutomorphismKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} impl AutomorphismKey where Self: SetAutomorphismGaloisElement, { pub fn decompress(&mut self, module: &M, other: &O) where O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, M: AutomorphismKeyDecompress, { module.decompress_automorphism_key(self, other); } } 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(), } } }