use backend::hal::{ api::{MatZnxAlloc, MatZnxAllocBytes, VmpPMatAlloc, VmpPMatAllocBytes, VmpPMatPrepare}, layouts::{Backend, Data, DataMut, DataRef, MatZnx, Module, ReaderFrom, Scratch, VmpPMat, WriterTo}, }; use crate::{GLWECiphertext, Infos}; pub trait GGLWEExecLayoutFamily = VmpPMatAlloc + VmpPMatAllocBytes + VmpPMatPrepare; #[derive(PartialEq, Eq)] pub struct GGLWECiphertext { pub(crate) data: MatZnx, pub(crate) basek: usize, pub(crate) k: usize, pub(crate) digits: usize, } impl GGLWECiphertext { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { GLWECiphertext { data: self.data.at(row, col), basek: self.basek, k: self.k, } } } impl GGLWECiphertext { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { GLWECiphertext { data: self.data.at_mut(row, col), basek: self.basek, k: self.k, } } } impl GGLWECiphertext> { pub fn alloc( module: &Module, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize, rank_out: usize, ) -> Self where Module: MatZnxAlloc, { let size: usize = k.div_ceil(basek); debug_assert!( size > digits, "invalid gglwe: ceil(k/basek): {} <= digits: {}", size, digits ); assert!( rows * digits <= size, "invalid gglwe: rows: {} * digits:{} > ceil(k/basek): {}", rows, digits, size ); Self { data: module.mat_znx_alloc(rows, rank_in, rank_out + 1, size), basek: basek, k, digits, } } pub fn bytes_of( module: &Module, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize, rank_out: usize, ) -> usize where Module: MatZnxAllocBytes, { let size: usize = k.div_ceil(basek); debug_assert!( size > digits, "invalid gglwe: ceil(k/basek): {} <= digits: {}", size, digits ); assert!( rows * digits <= size, "invalid gglwe: rows: {} * digits:{} > ceil(k/basek): {}", rows, digits, size ); module.mat_znx_alloc_bytes(rows, rank_in, rank_out + 1, rows) } } impl Infos for GGLWECiphertext { type Inner = MatZnx; fn inner(&self) -> &Self::Inner { &self.data } fn basek(&self) -> usize { self.basek } fn k(&self) -> usize { self.k } } impl GGLWECiphertext { pub fn rank(&self) -> usize { self.data.cols_out() - 1 } pub fn digits(&self) -> usize { self.digits } pub fn rank_in(&self) -> usize { self.data.cols_in() } pub fn rank_out(&self) -> usize { self.data.cols_out() - 1 } } use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; impl ReaderFrom for GGLWECiphertext { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = reader.read_u64::()? as usize; self.basek = reader.read_u64::()? as usize; self.digits = reader.read_u64::()? as usize; self.data.read_from(reader) } } impl WriterTo for GGLWECiphertext { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.k as u64)?; writer.write_u64::(self.basek as u64)?; writer.write_u64::(self.digits as u64)?; self.data.write_to(writer) } } #[derive(PartialEq, Eq)] pub struct GGLWECiphertextExec { pub(crate) data: VmpPMat, pub(crate) basek: usize, pub(crate) k: usize, pub(crate) digits: usize, } impl GGLWECiphertextExec, B> { pub fn alloc(module: &Module, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize, rank_out: usize) -> Self where Module: GGLWEExecLayoutFamily, { let size: usize = k.div_ceil(basek); debug_assert!( size > digits, "invalid gglwe: ceil(k/basek): {} <= digits: {}", size, digits ); assert!( rows * digits <= size, "invalid gglwe: rows: {} * digits:{} > ceil(k/basek): {}", rows, digits, size ); Self { data: module.vmp_pmat_alloc(rows, rank_in, rank_out + 1, size), basek: basek, k, digits, } } pub fn bytes_of( module: &Module, basek: usize, k: usize, rows: usize, digits: usize, rank_in: usize, rank_out: usize, ) -> usize where Module: GGLWEExecLayoutFamily, { let size: usize = k.div_ceil(basek); debug_assert!( size > digits, "invalid gglwe: ceil(k/basek): {} <= digits: {}", size, digits ); assert!( rows * digits <= size, "invalid gglwe: rows: {} * digits:{} > ceil(k/basek): {}", rows, digits, size ); module.vmp_pmat_alloc_bytes(rows, rank_in, rank_out + 1, rows) } } impl Infos for GGLWECiphertextExec { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { &self.data } fn basek(&self) -> usize { self.basek } fn k(&self) -> usize { self.k } } impl GGLWECiphertextExec { pub fn rank(&self) -> usize { self.data.cols_out() - 1 } pub fn digits(&self) -> usize { self.digits } pub fn rank_in(&self) -> usize { self.data.cols_in() } pub fn rank_out(&self) -> usize { self.data.cols_out() - 1 } } impl GGLWECiphertextExec { pub fn prepare(&mut self, module: &Module, other: &GGLWECiphertext, scratch: &mut Scratch) where DataOther: DataRef, Module: GGLWEExecLayoutFamily, { module.vmp_prepare(&mut self.data, &other.data, scratch); self.basek = other.basek; self.k = other.k; self.digits = other.digits; } }