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