poulpy_core/noise/
gglwe.rs1use poulpy_hal::{
2 api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxFillUniform, VecZnxSubScalarInplace},
3 layouts::{Backend, DataRef, Module, ScalarZnxToRef, Scratch, ScratchOwned, ZnxZero},
4};
5
6use crate::decryption::GLWEDecrypt;
7use crate::layouts::{GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos, prepared::GLWESecretPreparedToRef};
8
9impl<D: DataRef> GGLWE<D> {
10 pub fn assert_noise<M, S, P, BE: Backend>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: f64)
11 where
12 S: GLWESecretPreparedToRef<BE>,
13 P: ScalarZnxToRef,
14 M: GGLWENoise<BE>,
15 Scratch<BE>: ScratchTakeBasic,
16 {
17 module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise);
18 }
19}
20
21pub trait GGLWENoise<BE: Backend> {
22 fn gglwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
23 where
24 R: GGLWEToRef,
25 S: GLWESecretPreparedToRef<BE>,
26 P: ScalarZnxToRef,
27 Scratch<BE>: ScratchTakeBasic;
28}
29
30impl<BE: Backend> GGLWENoise<BE> for Module<BE>
31where
32 Module<BE>: GLWEDecrypt<BE> + VecZnxFillUniform + VecZnxSubScalarInplace,
33 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
34 Scratch<BE>: ScratchAvailable + ScratchTakeBasic,
35{
36 fn gglwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
37 where
38 R: GGLWEToRef,
39 S: GLWESecretPreparedToRef<BE>,
40 P: ScalarZnxToRef,
41 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
42 Scratch<BE>: ScratchAvailable + ScratchTakeBasic,
43 {
44 let res: &GGLWE<&[u8]> = &res.to_ref();
45
46 let dsize: usize = res.dsize().into();
47 let base2k: usize = res.base2k().into();
48
49 let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res));
50 let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res);
51
52 (0..res.rank_in().into()).for_each(|col_i| {
53 (0..res.dnum().into()).for_each(|row_i| {
54 self.glwe_decrypt(
55 &res.at(row_i, col_i),
56 &mut pt,
57 sk_prepared,
58 scratch.borrow(),
59 );
60
61 self.vec_znx_sub_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, col_i);
62
63 let noise_have: f64 = pt.data.std(base2k, 0).log2();
64
65 assert!(
68 noise_have <= max_noise,
69 "noise_have: {noise_have} > max_noise: {max_noise}"
70 );
71
72 pt.data.zero();
73 });
74 });
75 }
76}