use poulpy_hal::layouts::{Backend, DataRef, Module, Scratch, Stats}; use crate::{ GLWENormalize, GLWESub, ScratchTakeCore, decryption::GLWEDecrypt, layouts::{GLWE, GLWEInfos, GLWEPlaintext, GLWEToRef, LWEInfos, prepared::GLWESecretPreparedToRef}, }; impl GLWE { pub fn noise(&self, module: &M, pt_want: &P, sk_prepared: &S, scratch: &mut Scratch) -> Stats where M: GLWENoise, P: GLWEToRef, S: GLWESecretPreparedToRef, { module.glwe_noise(self, pt_want, sk_prepared, scratch) } } pub trait GLWENoise { fn glwe_noise_tmp_bytes(&self, infos: &A) -> usize where A: GLWEInfos; fn glwe_noise(&self, res: &R, pt_want: &P, sk_prepared: &S, scratch: &mut Scratch) -> Stats where R: GLWEToRef + GLWEInfos, P: GLWEToRef, S: GLWESecretPreparedToRef; } impl GLWENoise for Module where Module: GLWEDecrypt + GLWESub + GLWENormalize, Scratch: ScratchTakeCore, { fn glwe_noise_tmp_bytes(&self, infos: &A) -> usize where A: GLWEInfos, { GLWEPlaintext::bytes_of_from_infos(infos) + self .glwe_normalize_tmp_bytes() .max(self.glwe_decrypt_tmp_bytes(infos)) } fn glwe_noise(&self, res: &R, pt_want: &P, sk_prepared: &S, scratch: &mut Scratch) -> Stats where R: GLWEToRef + GLWEInfos, P: GLWEToRef, S: GLWESecretPreparedToRef, { let (mut pt_have, scratch_1) = scratch.take_glwe_plaintext(res); self.glwe_decrypt(res, &mut pt_have, sk_prepared, scratch_1); // println!("pt_have: {pt_have}"); // println!("pt_want: {}", pt_want.to_ref()); self.glwe_sub_inplace(&mut pt_have, pt_want); self.glwe_normalize_inplace(&mut pt_have, scratch_1); pt_have.data.stats(pt_have.base2k().into(), 0) } }