use backend::hal::{ api::{MatZnxAlloc, MatZnxAllocBytes}, layouts::{Backend, Data, DataMut, DataRef, MatZnx, Module, ReaderFrom, Scratch, VmpPMat, WriterTo}, }; use crate::{GGLWEExecLayoutFamily, GLWESwitchingKey, GLWESwitchingKeyExec, Infos}; #[derive(PartialEq, Eq)] pub struct GLWETensorKey { pub(crate) keys: Vec>, } impl GLWETensorKey> { pub fn alloc(module: &Module, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self where Module: MatZnxAlloc, { let mut keys: Vec>> = Vec::new(); let pairs: usize = (((rank + 1) * rank) >> 1).max(1); (0..pairs).for_each(|_| { keys.push(GLWESwitchingKey::alloc( module, basek, k, rows, digits, 1, rank, )); }); Self { keys: keys } } pub fn bytes_of(module: &Module, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> usize where Module: MatZnxAllocBytes, { let pairs: usize = (((rank + 1) * rank) >> 1).max(1); pairs * GLWESwitchingKey::>::bytes_of(module, basek, k, rows, digits, 1, rank) } } impl Infos for GLWETensorKey { type Inner = MatZnx; fn inner(&self) -> &Self::Inner { &self.keys[0].inner() } fn basek(&self) -> usize { self.keys[0].basek() } fn k(&self) -> usize { self.keys[0].k() } } impl GLWETensorKey { pub fn rank(&self) -> usize { self.keys[0].rank() } pub fn rank_in(&self) -> usize { self.keys[0].rank_in() } pub fn rank_out(&self) -> usize { self.keys[0].rank_out() } pub fn digits(&self) -> usize { self.keys[0].digits() } } impl GLWETensorKey { // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKey { if i > j { std::mem::swap(&mut i, &mut j); }; let rank: usize = self.rank(); &mut self.keys[i * rank + j - (i * (i + 1) / 2)] } } impl GLWETensorKey { // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKey { if i > j { std::mem::swap(&mut i, &mut j); }; let rank: usize = self.rank(); &self.keys[i * rank + j - (i * (i + 1) / 2)] } } use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; impl ReaderFrom for GLWETensorKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { let len: usize = reader.read_u64::()? as usize; if self.keys.len() != len { return Err(std::io::Error::new( std::io::ErrorKind::InvalidData, format!("self.keys.len()={} != read len={}", self.keys.len(), len), )); } for key in &mut self.keys { key.read_from(reader)?; } Ok(()) } } impl WriterTo for GLWETensorKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.keys.len() as u64)?; for key in &self.keys { key.write_to(writer)?; } Ok(()) } } #[derive(PartialEq, Eq)] pub struct GLWETensorKeyExec { pub(crate) keys: Vec>, } impl GLWETensorKeyExec, B> { pub fn alloc(module: &Module, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self where Module: GGLWEExecLayoutFamily, { let mut keys: Vec, B>> = Vec::new(); let pairs: usize = (((rank + 1) * rank) >> 1).max(1); (0..pairs).for_each(|_| { keys.push(GLWESwitchingKeyExec::alloc( module, basek, k, rows, digits, 1, rank, )); }); Self { keys } } pub fn bytes_of(module: &Module, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> usize where Module: GGLWEExecLayoutFamily, { let pairs: usize = (((rank + 1) * rank) >> 1).max(1); pairs * GLWESwitchingKeyExec::, B>::bytes_of(module, basek, k, rows, digits, 1, rank) } } impl Infos for GLWETensorKeyExec { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { &self.keys[0].inner() } fn basek(&self) -> usize { self.keys[0].basek() } fn k(&self) -> usize { self.keys[0].k() } } impl GLWETensorKeyExec { pub fn rank(&self) -> usize { self.keys[0].rank() } pub fn rank_in(&self) -> usize { self.keys[0].rank_in() } pub fn rank_out(&self) -> usize { self.keys[0].rank_out() } pub fn digits(&self) -> usize { self.keys[0].digits() } } impl GLWETensorKeyExec { // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyExec { if i > j { std::mem::swap(&mut i, &mut j); }; let rank: usize = self.rank(); &mut self.keys[i * rank + j - (i * (i + 1) / 2)] } } impl GLWETensorKeyExec { // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKeyExec { if i > j { std::mem::swap(&mut i, &mut j); }; let rank: usize = self.rank(); &self.keys[i * rank + j - (i * (i + 1) / 2)] } } impl GLWETensorKeyExec { pub fn prepare(&mut self, module: &Module, other: &GLWETensorKey, scratch: &mut Scratch) where DataOther: DataRef, Module: GGLWEExecLayoutFamily, { #[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); }); } }