use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, RingDegree, TensorKey, TensorKeyToRef, TorusPrecision, prepared::{ GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, }, }; #[derive(PartialEq, Eq)] pub struct TensorKeyPrepared { pub(crate) keys: Vec>, } impl LWEInfos for TensorKeyPrepared { fn n(&self) -> RingDegree { 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 TensorKeyPrepared { fn rank(&self) -> Rank { self.rank_out() } } impl GGLWEInfos for TensorKeyPrepared { fn rank_in(&self) -> Rank { self.rank_out() } fn rank_out(&self) -> Rank { self.keys[0].rank_out() } fn dsize(&self) -> Dsize { self.keys[0].dsize() } fn dnum(&self) -> Dnum { self.keys[0].dnum() } } pub trait TensorKeyPreparedAlloc where Self: GLWESwitchingKeyPreparedAlloc, { fn alloc_tensor_key_prepared( &self, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank, ) -> TensorKeyPrepared, B> { let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); TensorKeyPrepared { keys: (0..pairs) .map(|_| self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), rank, dnum, dsize)) .collect(), } } fn alloc_tensor_key_prepared_from_infos(&self, infos: &A) -> TensorKeyPrepared, B> where A: GGLWEInfos, { assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKeyPrepared" ); self.alloc_tensor_key_prepared( infos.base2k(), infos.k(), infos.dnum(), infos.dsize(), infos.rank_out(), ) } fn bytes_of_tensor_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; pairs * self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), rank, dnum, dsize) } fn bytes_of_tensor_key_prepared_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { self.bytes_of_tensor_key_prepared( infos.base2k(), infos.k(), infos.rank(), infos.dnum(), infos.dsize(), ) } } impl TensorKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} impl TensorKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, M: TensorKeyPreparedAlloc, { module.alloc_tensor_key_prepared_from_infos(infos) } pub fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self where M: TensorKeyPreparedAlloc, { module.alloc_tensor_key_prepared(base2k, k, dnum, dsize, rank) } pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, M: TensorKeyPreparedAlloc, { module.bytes_of_tensor_key_prepared_from_infos(infos) } pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where M: TensorKeyPreparedAlloc, { module.bytes_of_tensor_key_prepared(base2k, k, rank, dnum, dsize) } } impl TensorKeyPrepared { // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyPrepared { 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 TensorKeyPrepared { // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKeyPrepared { 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)] } } pub trait TensorKeyPrepare where Self: GLWESwitchingKeyPrepare, { fn prepare_tensor_key_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { self.prepare_glwe_switching_key_tmp_bytes(infos) } fn prepare_tensor_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: TensorKeyPreparedToMut, O: TensorKeyToRef, { let mut res: TensorKeyPrepared<&mut [u8], B> = res.to_mut(); let other: TensorKey<&[u8]> = other.to_ref(); assert_eq!(res.keys.len(), other.keys.len()); for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { self.prepare_glwe_switching(a, b, scratch); } } } impl TensorKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} impl TensorKeyPrepared, B> { fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize where A: GGLWEInfos, M: TensorKeyPrepare, { module.prepare_tensor_key_tmp_bytes(infos) } } impl TensorKeyPrepared { fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: TensorKeyToRef, M: TensorKeyPrepare, { module.prepare_tensor_key(self, other, scratch); } } pub trait TensorKeyPreparedToMut { fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B>; } impl TensorKeyPreparedToMut for TensorKeyPrepared where GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, { fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B> { TensorKeyPrepared { keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), } } } pub trait TensorKeyPreparedToRef { fn to_ref(&self) -> TensorKeyPrepared<&[u8], B>; } impl TensorKeyPreparedToRef for TensorKeyPrepared where GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, { fn to_ref(&self) -> TensorKeyPrepared<&[u8], B> { TensorKeyPrepared { keys: self.keys.iter().map(|c| c.to_ref()).collect(), } } }