use poulpy_hal::{ layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ Base2K, Degree, Digits, GGLWELayoutInfos, GGLWESwitchingKey, GLWECiphertext, GLWEInfos, LWEInfos, Rank, Rows, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GGLWEAutomorphismKeyLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rows: Rows, pub digits: Digits, pub rank: Rank, } #[derive(PartialEq, Eq, Clone)] pub struct GGLWEAutomorphismKey { pub(crate) key: GGLWESwitchingKey, pub(crate) p: i64, } impl GGLWEAutomorphismKey { pub fn p(&self) -> i64 { self.p } } impl LWEInfos for GGLWEAutomorphismKey { 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 GGLWEAutomorphismKey { fn rank(&self) -> Rank { self.rank_out() } } impl GGLWELayoutInfos for GGLWEAutomorphismKey { 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 LWEInfos for GGLWEAutomorphismKeyLayout { fn base2k(&self) -> Base2K { self.base2k } fn k(&self) -> TorusPrecision { self.k } fn n(&self) -> Degree { self.n } } impl GLWEInfos for GGLWEAutomorphismKeyLayout { fn rank(&self) -> Rank { self.rank } } impl GGLWELayoutInfos for GGLWEAutomorphismKeyLayout { fn rank_in(&self) -> Rank { self.rank } fn digits(&self) -> Digits { self.digits } fn rank_out(&self) -> Rank { self.rank } fn rows(&self) -> Rows { self.rows } } impl fmt::Debug for GGLWEAutomorphismKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } impl FillUniform for GGLWEAutomorphismKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } impl fmt::Display for GGLWEAutomorphismKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(AutomorphismKey: p={}) {}", self.p, self.key) } } impl GGLWEAutomorphismKey> { pub fn alloc(infos: &A) -> Self where A: GGLWELayoutInfos, { assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); GGLWEAutomorphismKey { key: GGLWESwitchingKey::alloc(infos), p: 0, } } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rows: Rows, digits: Digits, rank: Rank) -> Self { GGLWEAutomorphismKey { key: GGLWESwitchingKey::alloc_with(n, base2k, k, rows, digits, rank, rank), p: 0, } } pub fn alloc_bytes(infos: &A) -> usize where A: GGLWELayoutInfos, { assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); GGLWESwitchingKey::alloc_bytes(infos) } pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rows: Rows, digits: Digits, rank: Rank) -> usize { GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, rows, digits, rank, rank) } } impl GGLWEAutomorphismKey { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { self.key.at(row, col) } } impl GGLWEAutomorphismKey { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { self.key.at_mut(row, col) } } impl ReaderFrom for GGLWEAutomorphismKey { 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 GGLWEAutomorphismKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.p as u64)?; self.key.write_to(writer) } }