use backend::hal::{ api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPMatPrepare}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat}, }; use crate::layouts::{GGLWESwitchingKey, Infos, prepared::GGLWECiphertextExec}; #[derive(PartialEq, Eq)] pub struct GGLWESwitchingKeyExec { 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 GGLWESwitchingKeyExec, B> { pub fn alloc( module: &Module, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize, rank_out: usize, ) -> Self where Module: VmpPMatAlloc, { GGLWESwitchingKeyExec::, B> { key: GGLWECiphertextExec::alloc(module, n, basek, k, rows, digits, rank_in, rank_out), sk_in_n: 0, sk_out_n: 0, } } pub fn bytes_of( module: &Module, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize, rank_out: usize, ) -> usize where Module: VmpPMatAllocBytes, { GGLWECiphertextExec::bytes_of(module, n, basek, k, rows, digits, rank_in, rank_out) } pub fn from(module: &Module, other: &GGLWESwitchingKey, scratch: &mut Scratch) -> Self where Module: VmpPMatAlloc + VmpPMatPrepare, { let mut ksk_exec: GGLWESwitchingKeyExec, B> = Self::alloc( module, other.n(), 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 GGLWESwitchingKeyExec { 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 GGLWESwitchingKeyExec { 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 GGLWESwitchingKeyExec { pub fn prepare(&mut self, module: &Module, other: &GGLWESwitchingKey, scratch: &mut Scratch) where DataOther: DataRef, Module: VmpPMatPrepare, { self.key.prepare(module, &other.key, scratch); self.sk_in_n = other.sk_in_n; self.sk_out_n = other.sk_out_n; } }