use poulpy_hal::{ api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, }; use crate::layouts::{ Base2K, Degree, Digits, GGLWELayoutInfos, GGLWETensorKey, GLWEInfos, LWEInfos, Rank, Rows, TorusPrecision, prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc}, }; #[derive(PartialEq, Eq)] pub struct GGLWETensorKeyPrepared { pub(crate) keys: Vec>, } impl LWEInfos for GGLWETensorKeyPrepared { fn n(&self) -> Degree { self.keys[0].n() } fn base2k(&self) -> Base2K { self.keys[0].base2k() } fn k(&self) -> TorusPrecision { self.keys[0].k() } fn size(&self) -> usize { self.keys[0].size() } } impl GLWEInfos for GGLWETensorKeyPrepared { fn rank(&self) -> Rank { self.rank_out() } } impl GGLWELayoutInfos for GGLWETensorKeyPrepared { fn rank_in(&self) -> Rank { self.rank_out() } fn rank_out(&self) -> Rank { self.keys[0].rank_out() } fn digits(&self) -> Digits { self.keys[0].digits() } fn rows(&self) -> Rows { self.keys[0].rows() } } impl GGLWETensorKeyPrepared, B> { pub fn alloc(module: &Module, infos: &A) -> Self where A: GGLWELayoutInfos, Module: VmpPMatAlloc, { assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKeyPrepared" ); Self::alloc_with( module, infos.base2k(), infos.k(), infos.rows(), infos.digits(), infos.rank_out(), ) } pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rows: Rows, digits: Digits, rank: Rank) -> Self where Module: VmpPMatAlloc, { let mut keys: Vec, B>> = Vec::new(); let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); (0..pairs).for_each(|_| { keys.push(GGLWESwitchingKeyPrepared::alloc_with( module, base2k, k, rows, digits, Rank(1), rank, )); }); Self { keys } } pub fn alloc_bytes(module: &Module, infos: &A) -> usize where A: GGLWELayoutInfos, Module: VmpPMatAllocBytes, { assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKey" ); let rank_out: usize = infos.rank_out().into(); let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); pairs * GGLWESwitchingKeyPrepared::alloc_bytes_with( module, infos.base2k(), infos.k(), infos.rows(), infos.digits(), Rank(1), infos.rank_out(), ) } pub fn alloc_bytes_with( module: &Module, base2k: Base2K, k: TorusPrecision, rows: Rows, digits: Digits, rank: Rank, ) -> usize where Module: VmpPMatAllocBytes, { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; pairs * GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rows, digits, Rank(1), rank) } } impl GGLWETensorKeyPrepared { // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKeyPrepared { if i > j { std::mem::swap(&mut i, &mut j); }; let rank: usize = self.rank_out().into(); &mut self.keys[i * rank + j - (i * (i + 1) / 2)] } } impl GGLWETensorKeyPrepared { // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKeyPrepared { if i > j { std::mem::swap(&mut i, &mut j); }; let rank: usize = self.rank_out().into(); &self.keys[i * rank + j - (i * (i + 1) / 2)] } } impl Prepare> for GGLWETensorKeyPrepared where Module: VmpPrepare, { fn prepare(&mut self, module: &Module, other: &GGLWETensorKey, scratch: &mut Scratch) { #[cfg(debug_assertions)] { assert_eq!(self.keys.len(), other.keys.len()); } self.keys .iter_mut() .zip(other.keys.iter()) .for_each(|(a, b)| { a.prepare(module, b, scratch); }); } } impl PrepareAlloc, B>> for GGLWETensorKey where Module: VmpPMatAlloc + VmpPrepare, { fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWETensorKeyPrepared, B> { let mut tsk_prepared: GGLWETensorKeyPrepared, B> = GGLWETensorKeyPrepared::alloc(module, self); tsk_prepared.prepare(module, self, scratch); tsk_prepared } }