poulpy/poulpy-core/src/noise/gglwe_ct.rs
poulpy-core/src/noise/gglwe_ct.rsRaw
1use poulpy_hal::{
2 api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxSubScalarInplace},
3 layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, ZnxZero},
4 oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, VecZnxSubScalarInplaceImpl},
5};
6
7use crate::decryption::GLWEDecryption;
8use crate::layouts::{
9 GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos,
10 prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
11};
12
13impl<D: DataRef> GGLWE<D> {
14 pub fn assert_noise<M, BE, DataSk, DataWant>(
15 &self,
16 module: &M,
17 sk_prepared: &GLWESecretPrepared<DataSk, BE>,
18 pt_want: &ScalarZnx<DataWant>,
19 max_noise: f64,
20 ) where
21 DataSk: DataRef,
22 DataWant: DataRef,
23 M: GGLWENoise<BE>,
24 BE: Backend
25 + ScratchOwnedAllocImpl<BE>
26 + ScratchOwnedBorrowImpl<BE>
27 + ScratchOwnedBorrow<BE>
28 + VecZnxSubScalarInplaceImpl<BE>,
29 {
30 module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise);
31 }
32
33 // pub fn assert_noise<B, DataSk, DataWant>(
34 // &self,
35 // module: &Module<B>,
36 // sk: &GLWESecretPrepared<DataSk, B>,
37 // pt_want: &ScalarZnx<DataWant>,
38 // max_noise: f64,
39 // ) where
40 // DataSk: DataRef,
41 // DataWant: DataRef,
42 // Module<B>: VecZnxDftBytesOf
43 // + VecZnxBigBytesOf
44 // + VecZnxDftApply<B>
45 // + SvpApplyDftToDftInplace<B>
46 // + VecZnxIdftApplyConsume<B>
47 // + VecZnxBigAddInplace<B>
48 // + VecZnxBigAddSmallInplace<B>
49 // + VecZnxBigNormalize<B>
50 // + VecZnxNormalizeTmpBytes
51 // + VecZnxSubScalarInplace,
52 // B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
53 // {
54 // let dsize: usize = self.dsize().into();
55 // let base2k: usize = self.base2k().into();
56
57 // let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self));
58 // let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, self);
59
60 // (0..self.rank_in().into()).for_each(|col_i| {
61 // (0..self.dnum().into()).for_each(|row_i| {
62 // self.at(row_i, col_i)
63 // .decrypt(module, &mut pt, sk, scratch.borrow());
64
65 // module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, col_i);
66
67 // let noise_have: f64 = pt.data.std(base2k, 0).log2();
68
69 // println!("noise_have: {noise_have}");
70
71 // assert!(
72 // noise_have <= max_noise,
73 // "noise_have: {noise_have} > max_noise: {max_noise}"
74 // );
75
76 // pt.data.zero();
77 // });
78 // });
79 // }
80}
81
82pub trait GGLWENoise<BE: Backend> {
83 fn gglwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
84 where
85 R: GGLWEToRef,
86 S: GLWESecretPreparedToRef<BE>,
87 P: ScalarZnxToRef,
88 BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE> + VecZnxSubScalarInplaceImpl<BE>;
89}
90
91impl<BE: Backend> GGLWENoise<BE> for Module<BE>
92where
93 Module<BE>: GLWEDecryption<BE>,
94 Scratch<BE>: ScratchTakeBasic
95 + ScratchOwnedAllocImpl<BE>
96 + ScratchOwnedBorrowImpl<BE>
97 + ScratchOwnedBorrowImpl<BE>
98 + ScratchOwnedBorrow<BE>,
99{
100 fn gglwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
101 where
102 R: GGLWEToRef,
103 S: GLWESecretPreparedToRef<BE>,
104 P: ScalarZnxToRef,
105 BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE> + VecZnxSubScalarInplaceImpl<BE>,
106 {
107 let res: &GGLWE<&[u8]> = &res.to_ref();
108
109 let dsize: usize = res.dsize().into();
110 let base2k: usize = res.base2k().into();
111
112 let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res));
113 let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self, res);
114
115 (0..res.rank_in().into()).for_each(|col_i| {
116 (0..res.dnum().into()).for_each(|row_i| {
117 self.glwe_decrypt(
118 &res.at(row_i, col_i),
119 &mut pt,
120 sk_prepared,
121 scratch.borrow(),
122 );
123
124 self.vec_znx_sub_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, col_i);
125
126 let noise_have: f64 = pt.data.std(base2k, 0).log2();
127
128 println!("noise_have: {noise_have}");
129
130 assert!(
131 noise_have <= max_noise,
132 "noise_have: {noise_have} > max_noise: {max_noise}"
133 );
134
135 pt.data.zero();
136 });
137 });
138 }
139}