poulpy/poulpy-core/src/tests/generics/encryption/glwe_tsk.rs
poulpy-core/src/tests/generics/encryption/glwe_tsk.rsRaw
1use poulpy_hal::{
2 api::{
3 DFT, IDFTConsume, IDFTTmpA, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApply, SvpApplyInplace, SvpPPolAlloc,
4 SvpPPolAllocBytes, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace,
5 VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc,
6 VecZnxDftAllocBytes, VecZnxFillUniform, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub,
7 VecZnxSubABInplace, VecZnxSubScalarInplace, VecZnxSwithcDegree,
8 },
9 layouts::{Backend, Module, ScratchOwned, VecZnxDft},
10 oep::{
11 ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
12 TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl,
13 },
14 source::Source,
15};
16
17use crate::{
18 encryption::SIGMA,
19 layouts::{
20 GGLWETensorKey, GLWEPlaintext, GLWESecret, Infos,
21 compressed::{Decompress, GGLWETensorKeyCompressed},
22 prepared::{GLWESecretPrepared, PrepareAlloc},
23 },
24};
25
26pub fn test_glwe_tensor_key_encrypt_sk<B>(module: &Module<B>, basek: usize, k: usize, rank: usize)
27where
28 Module<B>: VecZnxDftAllocBytes
29 + VecZnxBigNormalize<B>
30 + DFT<B>
31 + SvpApplyInplace<B>
32 + IDFTConsume<B>
33 + VecZnxNormalizeTmpBytes
34 + VecZnxFillUniform
35 + VecZnxSubABInplace
36 + VecZnxAddInplace
37 + VecZnxNormalizeInplace<B>
38 + VecZnxAddNormal
39 + VecZnxNormalize<B>
40 + VecZnxSub
41 + SvpPrepare<B>
42 + SvpPPolAllocBytes
43 + SvpPPolAlloc<B>
44 + VecZnxBigAddSmallInplace<B>
45 + VecZnxBigAllocBytes
46 + VecZnxBigAddInplace<B>
47 + VecZnxCopy
48 + VecZnxDftAlloc<B>
49 + SvpApply<B>
50 + VecZnxBigAlloc<B>
51 + IDFTTmpA<B>
52 + VecZnxAddScalarInplace
53 + VecZnxSwithcDegree
54 + VecZnxSubScalarInplace,
55 B: Backend
56 + TakeVecZnxDftImpl<B>
57 + TakeVecZnxBigImpl<B>
58 + ScratchOwnedAllocImpl<B>
59 + ScratchOwnedBorrowImpl<B>
60 + ScratchAvailableImpl<B>
61 + TakeScalarZnxImpl<B>
62 + TakeVecZnxImpl<B>
63 + VecZnxDftAllocBytesImpl<B>
64 + VecZnxBigAllocBytesImpl<B>
65 + TakeSvpPPolImpl<B>,
66{
67 let n: usize = module.n();
68 let rows: usize = k / basek;
69
70 let mut tensor_key: GGLWETensorKey<Vec<u8>> = GGLWETensorKey::alloc(n, basek, k, rows, 1, rank);
71
72 let mut source_xs: Source = Source::new([0u8; 32]);
73 let mut source_xe: Source = Source::new([0u8; 32]);
74 let mut source_xa: Source = Source::new([0u8; 32]);
75
76 let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGLWETensorKey::encrypt_sk_scratch_space(
77 module,
78 basek,
79 tensor_key.k(),
80 rank,
81 ));
82
83 let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
84 sk.fill_ternary_prob(0.5, &mut source_xs);
85 let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
86
87 tensor_key.encrypt_sk(
88 module,
89 &sk,
90 &mut source_xa,
91 &mut source_xe,
92 scratch.borrow(),
93 );
94
95 let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k);
96
97 let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1);
98 let mut sk_ij_big = module.vec_znx_big_alloc(1, 1);
99 let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, 1);
100 let mut sk_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(rank, 1);
101
102 (0..rank).for_each(|i| {
103 module.dft(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i);
104 });
105
106 (0..rank).for_each(|i| {
107 (0..rank).for_each(|j| {
108 module.svp_apply(&mut sk_ij_dft, 0, &sk_prepared.data, j, &sk_dft, i);
109 module.idft_tmp_a(&mut sk_ij_big, 0, &mut sk_ij_dft, 0);
110 module.vec_znx_big_normalize(
111 basek,
112 &mut sk_ij.data.as_vec_znx_mut(),
113 0,
114 &sk_ij_big,
115 0,
116 scratch.borrow(),
117 );
118 (0..tensor_key.rank_in()).for_each(|col_i| {
119 (0..tensor_key.rows()).for_each(|row_i| {
120 tensor_key
121 .at(i, j)
122 .at(row_i, col_i)
123 .decrypt(module, &mut pt, &sk_prepared, scratch.borrow());
124
125 module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij.data, col_i);
126
127 let std_pt: f64 = pt.data.std(basek, 0) * (k as f64).exp2();
128 assert!((SIGMA - std_pt).abs() <= 0.5, "{} {}", SIGMA, std_pt);
129 });
130 });
131 })
132 })
133}
134
135pub fn test_glwe_tensor_key_compressed_encrypt_sk<B>(module: &Module<B>, basek: usize, k: usize, rank: usize)
136where
137 Module<B>: VecZnxDftAllocBytes
138 + VecZnxBigNormalize<B>
139 + DFT<B>
140 + SvpApplyInplace<B>
141 + IDFTConsume<B>
142 + VecZnxNormalizeTmpBytes
143 + VecZnxFillUniform
144 + VecZnxSubABInplace
145 + VecZnxAddInplace
146 + VecZnxNormalizeInplace<B>
147 + VecZnxAddNormal
148 + VecZnxNormalize<B>
149 + VecZnxSub
150 + SvpPrepare<B>
151 + SvpPPolAllocBytes
152 + SvpPPolAlloc<B>
153 + VecZnxBigAddSmallInplace<B>
154 + VecZnxBigAllocBytes
155 + VecZnxBigAddInplace<B>
156 + VecZnxCopy
157 + VecZnxDftAlloc<B>
158 + SvpApply<B>
159 + VecZnxBigAlloc<B>
160 + IDFTTmpA<B>
161 + VecZnxAddScalarInplace
162 + VecZnxSwithcDegree
163 + VecZnxSubScalarInplace,
164 B: Backend
165 + TakeVecZnxDftImpl<B>
166 + TakeVecZnxBigImpl<B>
167 + ScratchOwnedAllocImpl<B>
168 + ScratchOwnedBorrowImpl<B>
169 + ScratchAvailableImpl<B>
170 + TakeScalarZnxImpl<B>
171 + TakeVecZnxImpl<B>
172 + VecZnxDftAllocBytesImpl<B>
173 + VecZnxBigAllocBytesImpl<B>
174 + TakeSvpPPolImpl<B>,
175{
176 let n: usize = module.n();
177 let rows: usize = k / basek;
178
179 let mut tensor_key_compressed: GGLWETensorKeyCompressed<Vec<u8>> =
180 GGLWETensorKeyCompressed::alloc(n, basek, k, rows, 1, rank);
181
182 let mut source_xs: Source = Source::new([0u8; 32]);
183 let mut source_xe: Source = Source::new([0u8; 32]);
184
185 let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGLWETensorKeyCompressed::encrypt_sk_scratch_space(
186 module,
187 basek,
188 tensor_key_compressed.k(),
189 rank,
190 ));
191
192 let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
193 sk.fill_ternary_prob(0.5, &mut source_xs);
194 let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
195
196 let seed_xa: [u8; 32] = [1u8; 32];
197
198 tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow());
199
200 let mut tensor_key: GGLWETensorKey<Vec<u8>> = GGLWETensorKey::alloc(n, basek, k, rows, 1, rank);
201 tensor_key.decompress(module, &tensor_key_compressed);
202
203 let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k);
204
205 let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1);
206 let mut sk_ij_big = module.vec_znx_big_alloc(1, 1);
207 let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, 1);
208 let mut sk_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(rank, 1);
209
210 (0..rank).for_each(|i| {
211 module.dft(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i);
212 });
213
214 (0..rank).for_each(|i| {
215 (0..rank).for_each(|j| {
216 module.svp_apply(&mut sk_ij_dft, 0, &sk_prepared.data, j, &sk_dft, i);
217 module.idft_tmp_a(&mut sk_ij_big, 0, &mut sk_ij_dft, 0);
218 module.vec_znx_big_normalize(
219 basek,
220 &mut sk_ij.data.as_vec_znx_mut(),
221 0,
222 &sk_ij_big,
223 0,
224 scratch.borrow(),
225 );
226 (0..tensor_key.rank_in()).for_each(|col_i| {
227 (0..tensor_key.rows()).for_each(|row_i| {
228 tensor_key
229 .at(i, j)
230 .at(row_i, col_i)
231 .decrypt(module, &mut pt, &sk_prepared, scratch.borrow());
232
233 module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij.data, col_i);
234
235 let std_pt: f64 = pt.data.std(basek, 0) * (k as f64).exp2();
236 assert!((SIGMA - std_pt).abs() <= 0.5, "{} {}", SIGMA, std_pt);
237 });
238 });
239 })
240 })
241}