use backend::hal::{ api::{MatZnxAlloc, MatZnxAllocBytes}, layouts::{Backend, Data, DataMut, DataRef, MatZnx, Module, ReaderFrom, Scratch, VmpPMat, WriterTo}, }; use crate::{GGLWEExecLayoutFamily, GLWECiphertext, GLWESwitchingKey, GLWESwitchingKeyExec, Infos}; #[derive(PartialEq, Eq)] pub struct AutomorphismKey { pub(crate) key: GLWESwitchingKey, pub(crate) p: i64, } impl AutomorphismKey> { pub fn alloc(module: &Module, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self where Module: MatZnxAlloc, { AutomorphismKey { key: GLWESwitchingKey::alloc(module, basek, k, rows, digits, rank, rank), p: 0, } } pub fn bytes_of(module: &Module, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> usize where Module: MatZnxAllocBytes, { GLWESwitchingKey::>::bytes_of(module, basek, k, rows, digits, rank, rank) } } impl Infos for AutomorphismKey { type Inner = MatZnx; fn inner(&self) -> &Self::Inner { &self.key.inner() } fn basek(&self) -> usize { self.key.basek() } fn k(&self) -> usize { self.key.k() } } impl AutomorphismKey { pub fn p(&self) -> i64 { self.p } pub fn digits(&self) -> usize { self.key.digits() } pub fn rank(&self) -> usize { self.key.rank() } pub fn rank_in(&self) -> usize { self.key.rank_in() } pub fn rank_out(&self) -> usize { self.key.rank_out() } } impl AutomorphismKey { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { self.key.at(row, col) } } impl AutomorphismKey { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { self.key.at_mut(row, col) } } use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; impl ReaderFrom for AutomorphismKey { 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 AutomorphismKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.p as u64)?; self.key.write_to(writer) } } #[derive(PartialEq, Eq)] pub struct AutomorphismKeyExec { pub(crate) key: GLWESwitchingKeyExec, pub(crate) p: i64, } impl AutomorphismKeyExec, B> { pub fn alloc(module: &Module, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self where Module: GGLWEExecLayoutFamily, { AutomorphismKeyExec::, B> { key: GLWESwitchingKeyExec::alloc(module, basek, k, rows, digits, rank, rank), p: 0, } } pub fn bytes_of(module: &Module, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> usize where Module: GGLWEExecLayoutFamily, { GLWESwitchingKeyExec::, B>::bytes_of(module, basek, k, rows, digits, rank, rank) } pub fn from(module: &Module, other: &AutomorphismKey, scratch: &mut Scratch) -> Self where Module: GGLWEExecLayoutFamily, { let mut atk_exec: AutomorphismKeyExec, B> = Self::alloc( module, other.basek(), other.k(), other.rows(), other.digits(), other.rank(), ); atk_exec.prepare(module, other, scratch); atk_exec } } impl AutomorphismKeyExec { pub fn prepare(&mut self, module: &Module, other: &AutomorphismKey, scratch: &mut Scratch) where DataOther: DataRef, Module: GGLWEExecLayoutFamily, { self.key.prepare(module, &other.key, scratch); self.p = other.p; } } impl Infos for AutomorphismKeyExec { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { &self.key.inner() } fn basek(&self) -> usize { self.key.basek() } fn k(&self) -> usize { self.key.k() } } impl AutomorphismKeyExec { pub fn p(&self) -> i64 { self.p } pub fn digits(&self) -> usize { self.key.digits() } pub fn rank(&self) -> usize { self.key.rank() } pub fn rank_in(&self) -> usize { self.key.rank_in() } pub fn rank_out(&self) -> usize { self.key.rank_out() } }