use poulpy_hal::{ api::{ ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSubInplace, }, layouts::{Backend, DataRef, Module, Scratch, ScratchOwned}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, }; use crate::layouts::{GLWE, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; impl GLWE { pub fn noise( &self, module: &Module, sk_prepared: &GLWESecretPrepared, pt_want: &GLWEPlaintext, scratch: &mut Scratch, ) -> f64 where DataSk: DataRef, DataPt: DataRef, B: Backend, Module: VecZnxDftApply + VecZnxSubInplace + VecZnxNormalizeInplace + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize, Scratch: TakeVecZnxDft + TakeVecZnxBig, { let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); self.decrypt(module, &mut pt_have, sk_prepared, scratch); module.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0); module.vec_znx_normalize_inplace(self.base2k().into(), &mut pt_have.data, 0, scratch); pt_have.data.std(self.base2k().into(), 0).log2() } pub fn assert_noise( &self, module: &Module, sk_prepared: &GLWESecretPrepared, pt_want: &GLWEPlaintext, max_noise: f64, ) where DataSk: DataRef, DataPt: DataRef, Module: VecZnxDftBytesOf + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize + VecZnxNormalizeTmpBytes + VecZnxSubInplace + VecZnxNormalizeInplace, B: Backend + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self)); let noise_have: f64 = self.noise(module, sk_prepared, pt_want, scratch.borrow()); assert!(noise_have <= max_noise, "{noise_have} {max_noise}"); } }