use poulpy_hal::{ api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare, VmpPrepareTmpBytes}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToMut, VmpPMatToRef, ZnxInfos}, oep::VmpPMatAllocBytesImpl, }; use crate::layouts::{ Base2K, BuildError, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq)] pub struct GGSWPrepared { pub(crate) data: VmpPMat, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) dsize: Dsize, } impl LWEInfos for GGSWPrepared { fn n(&self) -> Degree { Degree(self.data.n() as u32) } fn base2k(&self) -> Base2K { self.base2k } fn k(&self) -> TorusPrecision { self.k } fn size(&self) -> usize { self.data.size() } } impl GLWEInfos for GGSWPrepared { fn rank(&self) -> Rank { Rank(self.data.cols_out() as u32 - 1) } } impl GGSWInfos for GGSWPrepared { fn dsize(&self) -> Dsize { self.dsize } fn dnum(&self) -> Dnum { Dnum(self.data.rows() as u32) } } pub struct GGSWCiphertextPreparedBuilder { data: Option>, base2k: Option, k: Option, dsize: Option, } impl GGSWPrepared { #[inline] pub fn builder() -> GGSWCiphertextPreparedBuilder { GGSWCiphertextPreparedBuilder { data: None, base2k: None, k: None, dsize: None, } } } impl GGSWCiphertextPreparedBuilder, B> { #[inline] pub fn layout(mut self, infos: &A) -> Self where A: GGSWInfos, B: VmpPMatAllocBytesImpl, { debug_assert!( infos.size() as u32 > infos.dsize().0, "invalid ggsw: ceil(k/base2k): {} <= dsize: {}", infos.size(), infos.dsize() ); assert!( infos.dnum().0 * infos.dsize().0 <= infos.size() as u32, "invalid ggsw: dnum: {} * dsize:{} > ceil(k/base2k): {}", infos.dnum(), infos.dsize(), infos.size(), ); self.data = Some(VmpPMat::alloc( infos.n().into(), infos.dnum().into(), (infos.rank() + 1).into(), (infos.rank() + 1).into(), infos.size(), )); self.base2k = Some(infos.base2k()); self.k = Some(infos.k()); self.dsize = Some(infos.dsize()); self } } impl GGSWCiphertextPreparedBuilder { #[inline] pub fn data(mut self, data: VmpPMat) -> Self { self.data = Some(data); self } #[inline] pub fn base2k(mut self, base2k: Base2K) -> Self { self.base2k = Some(base2k); self } #[inline] pub fn k(mut self, k: TorusPrecision) -> Self { self.k = Some(k); self } #[inline] pub fn dsize(mut self, dsize: Dsize) -> Self { self.dsize = Some(dsize); self } pub fn build(self) -> Result, BuildError> { let data: VmpPMat = self.data.ok_or(BuildError::MissingData)?; let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; let dsize: Dsize = self.dsize.ok_or(BuildError::MissingDigits)?; if base2k == 0_u32 { return Err(BuildError::ZeroBase2K); } if dsize == 0_u32 { return Err(BuildError::ZeroBase2K); } if k == 0_u32 { return Err(BuildError::ZeroTorusPrecision); } if data.n() == 0 { return Err(BuildError::ZeroDegree); } if data.cols() == 0 { return Err(BuildError::ZeroCols); } if data.size() == 0 { return Err(BuildError::ZeroLimbs); } Ok(GGSWPrepared { data, base2k, k, dsize, }) } } impl GGSWPrepared, B> { pub fn alloc(module: &Module, infos: &A) -> Self where A: GGSWInfos, Module: VmpPMatAlloc, { Self::alloc_with( module, infos.base2k(), infos.k(), infos.dnum(), infos.dsize(), infos.rank(), ) } pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self where Module: VmpPMatAlloc, { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, "invalid ggsw: ceil(k/base2k): {size} <= dsize: {}", dsize.0 ); assert!( dnum.0 * dsize.0 <= size as u32, "invalid ggsw: dnum: {} * dsize:{} > ceil(k/base2k): {size}", dnum.0, dsize.0, ); Self { data: module.vmp_pmat_alloc( dnum.into(), (rank + 1).into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize, ), k, base2k, dsize, } } pub fn alloc_bytes(module: &Module, infos: &A) -> usize where A: GGSWInfos, Module: VmpPMatAllocBytes, { Self::alloc_bytes_with( module, infos.base2k(), infos.k(), infos.dnum(), infos.dsize(), infos.rank(), ) } pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize where Module: VmpPMatAllocBytes, { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, "invalid ggsw: ceil(k/base2k): {size} <= dsize: {}", dsize.0 ); assert!( dnum.0 * dsize.0 <= size as u32, "invalid ggsw: dnum: {} * dsize:{} > ceil(k/base2k): {size}", dnum.0, dsize.0, ); module.vmp_pmat_alloc_bytes(dnum.into(), (rank + 1).into(), (rank + 1).into(), size) } } impl GGSWPrepared { pub fn data(&self) -> &VmpPMat { &self.data } } pub trait GGSWPrepareTmpBytes { fn ggsw_prepare_tmp_bytes(&self, infos: &A) -> usize where A: GGSWInfos; } impl GGSWPrepareTmpBytes for Module where Module: VmpPrepareTmpBytes, { fn ggsw_prepare_tmp_bytes(&self, infos: &A) -> usize where A: GGSWInfos, { self.vmp_prepare_tmp_bytes( infos.dnum().into(), (infos.rank() + 1).into(), (infos.rank() + 1).into(), infos.size(), ) } } impl GGSWPrepared, B> where Module: GGSWPrepareTmpBytes, { pub fn prepare_tmp_bytes(&self, module: Module, infos: &A) -> usize where A: GGSWInfos, { module.ggsw_prepare_tmp_bytes(self) } } pub trait GGSWPrepare { fn ggsw_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GGSWPreparedToMut, O: GGSWToRef; } impl GGSWPrepare for Module where Module: VmpPrepare, { fn ggsw_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GGSWPreparedToMut, O: GGSWToRef, { let mut res: GGSWPrepared<&mut [u8], B> = res.to_mut(); let other: GGSW<&[u8]> = other.to_ref(); assert_eq!(res.k, other.k); assert_eq!(res.base2k, other.base2k); assert_eq!(res.dsize, other.dsize); self.vmp_prepare(&mut res.data, &other.data, scratch); } } impl GGSWPrepared where Module: GGSWPrepare, { pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) where O: GGSWToRef, { module.ggsw_prepare(self, other, scratch); } } pub trait GGSWPrepareAlloc { fn ggsw_prepare_alloc(&self, other: &O, scratch: &mut Scratch) where O: GGSWToRef; } impl GGSWPrepareAlloc for Module where Module: GGSWPrepare, { fn ggsw_prepare_alloc(&self, other: &O, scratch: &mut Scratch) where O: GGSWToRef, { let mut ct_prepared: GGSWPrepared, B> = GGSWPrepared::alloc(self, other); self.ggsw_prepare(&mut ct_prepared, other, scratch); ct_prepared } } impl GGSW { fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) where Module: GGSWPrepareAlloc, { module.ggsw_prepare_alloc(self, scratch); } } pub trait GGSWPreparedToMut { fn to_mut(&mut self) -> GGSWPrepared<&mut [u8], B>; } impl GGSWPreparedToMut for GGSWPrepared { fn to_mut(&mut self) -> GGSWPrepared<&mut [u8], B> { GGSWPrepared { base2k: self.base2k, k: self.k, dsize: self.dsize, data: self.data.to_mut(), } } } pub trait GGSWCiphertextPreparedToRef { fn to_ref(&self) -> GGSWPrepared<&[u8], B>; } impl GGSWCiphertextPreparedToRef for GGSWPrepared { fn to_ref(&self) -> GGSWPrepared<&[u8], B> { GGSWPrepared { base2k: self.base2k, k: self.k, dsize: self.dsize, data: self.data.to_mut(), } } }