use backend::hal::{ api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPMatPrepare}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat}, }; use crate::{GGSWCiphertext, GGSWLayoutFamily, Infos}; #[derive(PartialEq, Eq)] pub struct GGSWCiphertextExec { pub(crate) data: VmpPMat, pub(crate) basek: usize, pub(crate) k: usize, pub(crate) digits: usize, } impl GGSWCiphertextExec, B> { pub fn alloc(module: &Module, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self where Module: GGSWLayoutFamily, { let size: usize = k.div_ceil(basek); debug_assert!(digits > 0, "invalid ggsw: `digits` == 0"); debug_assert!( size > digits, "invalid ggsw: ceil(k/basek): {} <= digits: {}", size, digits ); assert!( rows * digits <= size, "invalid ggsw: rows: {} * digits:{} > ceil(k/basek): {}", rows, digits, size ); Self { data: module.vmp_pmat_alloc(n, rows, rank + 1, rank + 1, k.div_ceil(basek)), basek, k: k, digits, } } pub fn bytes_of(module: &Module, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> usize where Module: GGSWLayoutFamily, { let size: usize = k.div_ceil(basek); debug_assert!( size > digits, "invalid ggsw: ceil(k/basek): {} <= digits: {}", size, digits ); assert!( rows * digits <= size, "invalid ggsw: rows: {} * digits:{} > ceil(k/basek): {}", rows, digits, size ); module.vmp_pmat_alloc_bytes(n, rows, rank + 1, rank + 1, size) } pub fn from( module: &Module, other: &GGSWCiphertext, scratch: &mut Scratch, ) -> GGSWCiphertextExec, B> where Module: GGSWLayoutFamily, { let mut ggsw_exec: GGSWCiphertextExec, B> = Self::alloc( module, other.n(), other.basek(), other.k(), other.rows(), other.digits(), other.rank(), ); ggsw_exec.prepare(module, other, scratch); ggsw_exec } } impl Infos for GGSWCiphertextExec { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { &self.data } fn basek(&self) -> usize { self.basek } fn k(&self) -> usize { self.k } } impl GGSWCiphertextExec { pub fn rank(&self) -> usize { self.data.cols_out() - 1 } pub fn digits(&self) -> usize { self.digits } } impl GGSWCiphertextExec { pub fn prepare(&mut self, module: &Module, other: &GGSWCiphertext, scratch: &mut Scratch) where DataOther: DataRef, Module: GGSWLayoutFamily, { module.vmp_prepare(&mut self.data, &other.data, scratch); self.k = other.k; self.basek = other.basek; self.digits = other.digits; } }