use backend::hal::{ api::{MatZnxAlloc, MatZnxAllocBytes}, layouts::{Backend, Data, DataMut, DataRef, MatZnx, Module, ReaderFrom, Scratch, VmpPMat, WriterTo}, }; use crate::{GGLWECiphertext, GGLWECiphertextExec, GGLWEExecLayoutFamily, GLWECiphertext, Infos}; #[derive(PartialEq, Eq)] pub struct GLWESwitchingKey { pub(crate) key: GGLWECiphertext, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } impl GLWESwitchingKey> { pub fn alloc( module: &Module, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize, rank_out: usize, ) -> Self where Module: MatZnxAlloc, { GLWESwitchingKey { key: GGLWECiphertext::alloc(module, basek, k, rows, digits, rank_in, rank_out), sk_in_n: 0, sk_out_n: 0, } } pub fn bytes_of( module: &Module, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize, rank_out: usize, ) -> usize where Module: MatZnxAllocBytes, { GGLWECiphertext::>::bytes_of(module, basek, k, rows, digits, rank_in, rank_out) } } impl Infos for GLWESwitchingKey { 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 GLWESwitchingKey { pub fn rank(&self) -> usize { self.key.data.cols_out() - 1 } pub fn rank_in(&self) -> usize { self.key.data.cols_in() } pub fn rank_out(&self) -> usize { self.key.data.cols_out() - 1 } pub fn digits(&self) -> usize { self.key.digits() } pub fn sk_degree_in(&self) -> usize { self.sk_in_n } pub fn sk_degree_out(&self) -> usize { self.sk_out_n } } impl GLWESwitchingKey { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { self.key.at(row, col) } } impl GLWESwitchingKey { 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 GLWESwitchingKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.sk_in_n = reader.read_u64::()? as usize; self.sk_out_n = reader.read_u64::()? as usize; self.key.read_from(reader) } } impl WriterTo for GLWESwitchingKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.sk_in_n as u64)?; writer.write_u64::(self.sk_out_n as u64)?; self.key.write_to(writer) } } #[derive(PartialEq, Eq)] pub struct GLWESwitchingKeyExec { pub(crate) key: GGLWECiphertextExec, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } impl GLWESwitchingKeyExec, B> { pub fn alloc(module: &Module, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize, rank_out: usize) -> Self where Module: GGLWEExecLayoutFamily, { GLWESwitchingKeyExec::, B> { key: GGLWECiphertextExec::alloc(module, basek, k, rows, digits, rank_in, rank_out), sk_in_n: 0, sk_out_n: 0, } } pub fn bytes_of( module: &Module, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize, rank_out: usize, ) -> usize where Module: GGLWEExecLayoutFamily, { GGLWECiphertextExec::bytes_of(module, basek, k, rows, digits, rank_in, rank_out) } pub fn from(module: &Module, other: &GLWESwitchingKey, scratch: &mut Scratch) -> Self where Module: GGLWEExecLayoutFamily, { let mut ksk_exec: GLWESwitchingKeyExec, B> = Self::alloc( module, other.basek(), other.k(), other.rows(), other.digits(), other.rank_in(), other.rank_out(), ); ksk_exec.prepare(module, other, scratch); ksk_exec } } impl Infos for GLWESwitchingKeyExec { 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 GLWESwitchingKeyExec { pub fn rank(&self) -> usize { self.key.data.cols_out() - 1 } pub fn rank_in(&self) -> usize { self.key.data.cols_in() } pub fn rank_out(&self) -> usize { self.key.data.cols_out() - 1 } pub fn digits(&self) -> usize { self.key.digits() } pub fn sk_degree_in(&self) -> usize { self.sk_in_n } pub fn sk_degree_out(&self) -> usize { self.sk_out_n } } impl GLWESwitchingKeyExec { pub fn prepare(&mut self, module: &Module, other: &GLWESwitchingKey, scratch: &mut Scratch) where DataOther: DataRef, Module: GGLWEExecLayoutFamily, { self.key.prepare(module, &other.key, scratch); self.sk_in_n = other.sk_in_n; self.sk_out_n = other.sk_out_n; } }