poulpy_core/noise/
gglwe_ct.rs1use poulpy_hal::{
2 api::{
3 ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
4 VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume,
5 VecZnxNormalizeTmpBytes, VecZnxSubScalarInplace,
6 },
7 layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, ZnxZero},
8 oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl},
9};
10
11use crate::layouts::{GGLWECiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared};
12
13impl<D: DataRef> GGLWECiphertext<D> {
14 pub fn assert_noise<B, DataSk, DataWant>(
15 self,
16 module: &Module<B>,
17 sk: &GLWESecretPrepared<DataSk, B>,
18 pt_want: &ScalarZnx<DataWant>,
19 max_noise: f64,
20 ) where
21 DataSk: DataRef,
22 DataWant: DataRef,
23 Module<B>: VecZnxDftAllocBytes
24 + VecZnxBigAllocBytes
25 + VecZnxDftApply<B>
26 + SvpApplyDftToDftInplace<B>
27 + VecZnxIdftApplyConsume<B>
28 + VecZnxBigAddInplace<B>
29 + VecZnxBigAddSmallInplace<B>
30 + VecZnxBigNormalize<B>
31 + VecZnxNormalizeTmpBytes
32 + VecZnxSubScalarInplace,
33 B: Backend + TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B> + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
34 {
35 let digits: usize = self.digits();
36 let basek: usize = self.basek();
37 let k: usize = self.k();
38
39 let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, basek, k));
40 let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self.n(), basek, k);
41
42 (0..self.rank_in()).for_each(|col_i| {
43 (0..self.rows()).for_each(|row_i| {
44 self.at(row_i, col_i)
45 .decrypt(module, &mut pt, sk, scratch.borrow());
46
47 module.vec_znx_sub_scalar_inplace(
48 &mut pt.data,
49 0,
50 (digits - 1) + row_i * digits,
51 pt_want,
52 col_i,
53 );
54
55 let noise_have: f64 = pt.data.std(basek, 0).log2();
56
57 assert!(
58 noise_have <= max_noise,
59 "noise_have: {} > max_noise: {}",
60 noise_have,
61 max_noise
62 );
63
64 pt.data.zero();
65 });
66 });
67 }
68}