arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/core/src/noise/glwe_ct.rs
core/src/noise/glwe_ct.rsRaw
 1use backend::hal::{
 2    api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxNormalizeInplace, VecZnxSubABInplace},
 3    layouts::{Backend, DataRef, Module, ScratchOwned},
 4    oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl},
 5};
 6
 7use crate::{
 8    layouts::GLWEPlaintext,
 9    layouts::prepared::GLWESecretPrepared,
10    layouts::{GLWECiphertext, Infos},
11    trait_families::GLWEDecryptFamily,
12};
13
14impl<D: DataRef> GLWECiphertext<D> {
15    pub fn assert_noise<B: Backend, DataSk, DataPt>(
16        &self,
17        module: &Module<B>,
18        sk_prepared: &GLWESecretPrepared<DataSk, B>,
19        pt_want: &GLWEPlaintext<DataPt>,
20        max_noise: f64,
21    ) where
22        DataSk: DataRef,
23        DataPt: DataRef,
24        Module<B>: GLWEDecryptFamily<B> + VecZnxSubABInplace + VecZnxNormalizeInplace<B>,
25        B: TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B> + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
26    {
27        let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self.n(), self.basek(), self.k());
28
29        let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(
30            module,
31            self.n(),
32            self.basek(),
33            self.k(),
34        ));
35
36        self.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
37
38        module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
39        module.vec_znx_normalize_inplace(self.basek(), &mut pt_have.data, 0, scratch.borrow());
40
41        let noise_have: f64 = pt_have.data.std(self.basek(), 0).log2();
42        assert!(noise_have <= max_noise, "{} {}", noise_have, max_noise);
43    }
44}