poulpy_core/noise/
glwe.rs1use poulpy_hal::layouts::{Backend, DataRef, Module, Scratch, Stats};
2
3use crate::{
4 GLWENormalize, GLWESub, ScratchTakeCore,
5 decryption::GLWEDecrypt,
6 layouts::{GLWE, GLWEInfos, GLWEPlaintext, GLWEToRef, LWEInfos, prepared::GLWESecretPreparedToRef},
7};
8
9impl<D: DataRef> GLWE<D> {
10 pub fn noise<M, P, S, BE: Backend>(&self, module: &M, pt_want: &P, sk_prepared: &S, scratch: &mut Scratch<BE>) -> Stats
11 where
12 M: GLWENoise<BE>,
13 P: GLWEToRef,
14 S: GLWESecretPreparedToRef<BE>,
15 {
16 module.glwe_noise(self, pt_want, sk_prepared, scratch)
17 }
18}
19
20pub trait GLWENoise<BE: Backend> {
21 fn glwe_noise_tmp_bytes<A>(&self, infos: &A) -> usize
22 where
23 A: GLWEInfos;
24
25 fn glwe_noise<R, P, S>(&self, res: &R, pt_want: &P, sk_prepared: &S, scratch: &mut Scratch<BE>) -> Stats
26 where
27 R: GLWEToRef + GLWEInfos,
28 P: GLWEToRef,
29 S: GLWESecretPreparedToRef<BE>;
30}
31
32impl<BE: Backend> GLWENoise<BE> for Module<BE>
33where
34 Module<BE>: GLWEDecrypt<BE> + GLWESub + GLWENormalize<BE>,
35 Scratch<BE>: ScratchTakeCore<BE>,
36{
37 fn glwe_noise_tmp_bytes<A>(&self, infos: &A) -> usize
38 where
39 A: GLWEInfos,
40 {
41 GLWEPlaintext::bytes_of_from_infos(infos)
42 + self
43 .glwe_normalize_tmp_bytes()
44 .max(self.glwe_decrypt_tmp_bytes(infos))
45 }
46
47 fn glwe_noise<R, P, S>(&self, res: &R, pt_want: &P, sk_prepared: &S, scratch: &mut Scratch<BE>) -> Stats
48 where
49 R: GLWEToRef + GLWEInfos,
50 P: GLWEToRef,
51 S: GLWESecretPreparedToRef<BE>,
52 {
53 let (mut pt_have, scratch_1) = scratch.take_glwe_plaintext(res);
54 self.glwe_decrypt(res, &mut pt_have, sk_prepared, scratch_1);
55 self.glwe_sub_inplace(&mut pt_have, pt_want);
58 self.glwe_normalize_inplace(&mut pt_have, scratch_1);
59 pt_have.data.stats(pt_have.base2k().into(), 0)
60 }
61}