poulpy/poulpy-core/src/encryption/ggsw_ct.rs
poulpy-core/src/encryption/ggsw_ct.rsRaw
1use poulpy_hal::{
2 api::{ModuleN, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace},
3 layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, VecZnx, ZnxInfos, ZnxZero},
4 source::Source,
5};
6
7use crate::{
8 SIGMA, ScratchTakeCore,
9 encryption::glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal},
10 layouts::{
11 GGSW, GGSWInfos, GGSWToMut, GLWEInfos, LWEInfos,
12 prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
13 },
14};
15
16impl GGSW<Vec<u8>> {
17 pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
18 where
19 A: GGSWInfos,
20 M: GGSWEncryptSk<BE>,
21 {
22 module.ggsw_encrypt_sk_tmp_bytes(infos)
23 }
24}
25
26impl<D: DataMut> GGSW<D> {
27 #[allow(clippy::too_many_arguments)]
28 pub fn encrypt_sk<P, S, M, BE: Backend>(
29 &mut self,
30 module: &M,
31 pt: &P,
32 sk: &S,
33 source_xa: &mut Source,
34 source_xe: &mut Source,
35 scratch: &mut Scratch<BE>,
36 ) where
37 P: ScalarZnxToRef,
38 S: GLWESecretPreparedToRef<BE>,
39 M: GGSWEncryptSk<BE>,
40 Scratch<BE>: ScratchTakeCore<BE>,
41 {
42 module.ggsw_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch);
43 }
44}
45
46pub trait GGSWEncryptSk<B: Backend> {
47 fn ggsw_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
48 where
49 A: GGSWInfos;
50
51 fn ggsw_encrypt_sk<R, P, S>(
52 &self,
53 res: &mut R,
54 pt: &P,
55 sk: &S,
56 source_xa: &mut Source,
57 source_xe: &mut Source,
58 scratch: &mut Scratch<B>,
59 ) where
60 R: GGSWToMut,
61 P: ScalarZnxToRef,
62 S: GLWESecretPreparedToRef<B>;
63}
64
65impl<B: Backend> GGSWEncryptSk<B> for Module<B>
66where
67 Module<B>: ModuleN
68 + GLWEEncryptSkInternal<B>
69 + GLWEEncryptSk<B>
70 + VecZnxDftBytesOf
71 + VecZnxNormalizeInplace<B>
72 + VecZnxAddScalarInplace,
73 Scratch<B>: ScratchTakeCore<B>,
74{
75 fn ggsw_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
76 where
77 A: GGSWInfos,
78 {
79 let size = infos.size();
80 self.glwe_encrypt_sk_tmp_bytes(infos)
81 + VecZnx::bytes_of(self.n(), (infos.rank() + 1).into(), size)
82 + VecZnx::bytes_of(self.n(), 1, size)
83 + self.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size)
84 }
85
86 fn ggsw_encrypt_sk<R, P, S>(
87 &self,
88 res: &mut R,
89 pt: &P,
90 sk: &S,
91 source_xa: &mut Source,
92 source_xe: &mut Source,
93 scratch: &mut Scratch<B>,
94 ) where
95 R: GGSWToMut,
96 P: ScalarZnxToRef,
97 S: GLWESecretPreparedToRef<B>,
98 {
99 let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
100 let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
101 let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref();
102
103 assert_eq!(res.rank(), sk.rank());
104 assert_eq!(res.n(), self.n() as u32);
105 assert_eq!(pt.n(), self.n());
106 assert_eq!(sk.n(), self.n() as u32);
107
108 let k: usize = res.k().into();
109 let base2k: usize = res.base2k().into();
110 let rank: usize = res.rank().into();
111 let dsize: usize = res.dsize().into();
112 let cols: usize = (rank + 1).into();
113
114 let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout());
115
116 for row_i in 0..res.dnum().into() {
117 tmp_pt.data.zero();
118 // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
119 self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0);
120 self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1);
121 for col_j in 0..rank + 1 {
122 self.glwe_encrypt_sk_internal(
123 base2k,
124 k,
125 res.at_mut(row_i, col_j).data_mut(),
126 cols,
127 false,
128 Some((&tmp_pt, col_j)),
129 sk,
130 source_xa,
131 source_xe,
132 SIGMA,
133 scratch_1,
134 );
135 }
136 }
137 }
138}