poulpy/poulpy-core/src/tests/generics/automorphism/glwe_ct.rs
poulpy-core/src/tests/generics/automorphism/glwe_ct.rsRaw
1use poulpy_hal::{
2 api::{
3 ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, VecZnxAddInplace,
4 VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxBigAddInplace,
5 VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAllocBytes,
6 VecZnxDftFromVecZnx, VecZnxDftToVecZnxBigConsume, VecZnxFillUniform, VecZnxNormalize, VecZnxNormalizeInplace,
7 VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubABInplace, VecZnxSwithcDegree, VmpApply, VmpApplyAdd, VmpApplyTmpBytes,
8 VmpPMatAlloc, VmpPrepare,
9 },
10 layouts::{Backend, Module, ScratchOwned},
11 oep::{
12 ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
13 TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl,
14 },
15 source::Source,
16};
17
18use crate::{
19 encryption::SIGMA,
20 layouts::{
21 GGLWEAutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos,
22 prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc},
23 },
24 noise::log2_std_noise_gglwe_product,
25};
26
27#[allow(clippy::too_many_arguments)]
28pub fn test_glwe_automorphism<B>(
29 module: &Module<B>,
30 basek: usize,
31 p: i64,
32 k_out: usize,
33 k_in: usize,
34 k_ksk: usize,
35 digits: usize,
36 rank: usize,
37) where
38 Module<B>: VecZnxDftAllocBytes
39 + VecZnxBigNormalize<B>
40 + VecZnxDftFromVecZnx<B>
41 + SvpApplyInplace<B>
42 + VecZnxDftToVecZnxBigConsume<B>
43 + VecZnxNormalizeTmpBytes
44 + VecZnxFillUniform
45 + VecZnxSubABInplace
46 + VecZnxAddInplace
47 + VecZnxNormalizeInplace<B>
48 + VecZnxAddNormal
49 + VecZnxNormalize<B>
50 + VecZnxSub
51 + SvpPrepare<B>
52 + SvpPPolAllocBytes
53 + SvpPPolAlloc<B>
54 + VecZnxBigAllocBytes
55 + VecZnxBigAddInplace<B>
56 + VecZnxBigAddSmallInplace<B>
57 + VmpApplyTmpBytes
58 + VecZnxBigNormalizeTmpBytes
59 + VmpApply<B>
60 + VmpApplyAdd<B>
61 + VecZnxAutomorphism
62 + VecZnxSwithcDegree
63 + VecZnxAddScalarInplace
64 + VecZnxAutomorphismInplace
65 + VmpPMatAlloc<B>
66 + VmpPrepare<B>,
67 B: Backend
68 + TakeVecZnxDftImpl<B>
69 + TakeVecZnxBigImpl<B>
70 + TakeSvpPPolImpl<B>
71 + ScratchOwnedAllocImpl<B>
72 + ScratchOwnedBorrowImpl<B>
73 + ScratchAvailableImpl<B>
74 + TakeScalarZnxImpl<B>
75 + TakeVecZnxImpl<B>,
76{
77 let n: usize = module.n();
78 let rows: usize = k_in.div_ceil(basek * digits);
79
80 let mut autokey: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(n, basek, k_ksk, rows, digits, rank);
81 let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_in, rank);
82 let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_out, rank);
83 let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_in);
84
85 let mut source_xs: Source = Source::new([0u8; 32]);
86 let mut source_xe: Source = Source::new([0u8; 32]);
87 let mut source_xa: Source = Source::new([0u8; 32]);
88
89 module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_in, &mut source_xa);
90
91 let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
92 GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, n, basek, autokey.k(), rank)
93 | GLWECiphertext::decrypt_scratch_space(module, n, basek, ct_out.k())
94 | GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, ct_in.k())
95 | GLWECiphertext::automorphism_scratch_space(
96 module,
97 n,
98 basek,
99 ct_out.k(),
100 ct_in.k(),
101 autokey.k(),
102 digits,
103 rank,
104 ),
105 );
106
107 let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
108 sk.fill_ternary_prob(0.5, &mut source_xs);
109 let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
110
111 autokey.encrypt_sk(
112 module,
113 p,
114 &sk,
115 &mut source_xa,
116 &mut source_xe,
117 scratch.borrow(),
118 );
119
120 ct_in.encrypt_sk(
121 module,
122 &pt_want,
123 &sk_prepared,
124 &mut source_xa,
125 &mut source_xe,
126 scratch.borrow(),
127 );
128
129 let mut autokey_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> =
130 GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank);
131 autokey_prepared.prepare(module, &autokey, scratch.borrow());
132
133 ct_out.automorphism(module, &ct_in, &autokey_prepared, scratch.borrow());
134
135 let max_noise: f64 = log2_std_noise_gglwe_product(
136 module.n() as f64,
137 basek * digits,
138 0.5,
139 0.5,
140 0f64,
141 SIGMA * SIGMA,
142 0f64,
143 rank as f64,
144 k_in,
145 k_ksk,
146 );
147
148 module.vec_znx_automorphism_inplace(p, &mut pt_want.data, 0);
149
150 ct_out.assert_noise(module, &sk_prepared, &pt_want, max_noise + 1.0);
151}
152
153#[allow(clippy::too_many_arguments)]
154pub fn test_glwe_automorphism_inplace<B>(
155 module: &Module<B>,
156 basek: usize,
157 p: i64,
158 k_ct: usize,
159 k_ksk: usize,
160 digits: usize,
161 rank: usize,
162) where
163 Module<B>: VecZnxDftAllocBytes
164 + VecZnxBigNormalize<B>
165 + VecZnxDftFromVecZnx<B>
166 + SvpApplyInplace<B>
167 + VecZnxDftToVecZnxBigConsume<B>
168 + VecZnxNormalizeTmpBytes
169 + VecZnxFillUniform
170 + VecZnxSubABInplace
171 + VecZnxAddInplace
172 + VecZnxNormalizeInplace<B>
173 + VecZnxAddNormal
174 + VecZnxNormalize<B>
175 + VecZnxSub
176 + SvpPrepare<B>
177 + SvpPPolAllocBytes
178 + SvpPPolAlloc<B>
179 + VecZnxBigAllocBytes
180 + VecZnxBigAddInplace<B>
181 + VecZnxBigAddSmallInplace<B>
182 + VmpApplyTmpBytes
183 + VecZnxBigNormalizeTmpBytes
184 + VmpApply<B>
185 + VmpApplyAdd<B>
186 + VecZnxAutomorphism
187 + VecZnxSwithcDegree
188 + VecZnxAddScalarInplace
189 + VecZnxAutomorphismInplace
190 + VmpPMatAlloc<B>
191 + VmpPrepare<B>,
192 B: Backend
193 + TakeVecZnxDftImpl<B>
194 + TakeVecZnxBigImpl<B>
195 + TakeSvpPPolImpl<B>
196 + ScratchOwnedAllocImpl<B>
197 + ScratchOwnedBorrowImpl<B>
198 + ScratchAvailableImpl<B>
199 + TakeScalarZnxImpl<B>
200 + TakeVecZnxImpl<B>,
201{
202 let n: usize = module.n();
203 let rows: usize = k_ct.div_ceil(basek * digits);
204
205 let mut autokey: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(n, basek, k_ksk, rows, digits, rank);
206 let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_ct, rank);
207 let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_ct);
208
209 let mut source_xs: Source = Source::new([0u8; 32]);
210 let mut source_xe: Source = Source::new([0u8; 32]);
211 let mut source_xa: Source = Source::new([0u8; 32]);
212
213 module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_ct, &mut source_xa);
214
215 let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
216 GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, n, basek, autokey.k(), rank)
217 | GLWECiphertext::decrypt_scratch_space(module, n, basek, ct.k())
218 | GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, ct.k())
219 | GLWECiphertext::automorphism_inplace_scratch_space(module, n, basek, ct.k(), autokey.k(), digits, rank),
220 );
221
222 let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
223 sk.fill_ternary_prob(0.5, &mut source_xs);
224 let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
225
226 autokey.encrypt_sk(
227 module,
228 p,
229 &sk,
230 &mut source_xa,
231 &mut source_xe,
232 scratch.borrow(),
233 );
234
235 ct.encrypt_sk(
236 module,
237 &pt_want,
238 &sk_prepared,
239 &mut source_xa,
240 &mut source_xe,
241 scratch.borrow(),
242 );
243
244 let mut autokey_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> =
245 GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank);
246 autokey_prepared.prepare(module, &autokey, scratch.borrow());
247
248 ct.automorphism_inplace(module, &autokey_prepared, scratch.borrow());
249
250 let max_noise: f64 = log2_std_noise_gglwe_product(
251 module.n() as f64,
252 basek * digits,
253 0.5,
254 0.5,
255 0f64,
256 SIGMA * SIGMA,
257 0f64,
258 rank as f64,
259 k_ct,
260 k_ksk,
261 );
262
263 module.vec_znx_automorphism_inplace(p, &mut pt_want.data, 0);
264
265 ct.assert_noise(module, &sk_prepared, &pt_want, max_noise + 1.0);
266}