poulpy/poulpy-core/src/tests/test_suite/conversion.rs
poulpy-core/src/tests/test_suite/conversion.rsRaw
1use poulpy_hal::{
2 api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform, VecZnxNormalize},
3 layouts::{Backend, FillUniform, Module, Scratch, ScratchOwned, ZnxView},
4 source::Source,
5};
6use rug::Float;
7
8use crate::{
9 GLWEDecrypt, GLWEEncryptSk, GLWEFromLWE, GLWENoise, GLWENormalize, GLWEToLWESwitchingKeyEncryptSk, LWEDecrypt, LWEEncryptSk,
10 LWEFromGLWE, LWEToGLWESwitchingKeyEncryptSk, SIGMA, ScratchTakeCore,
11 layouts::{
12 Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWEToLWEKey,
13 GLWEToLWEKeyLayout, GLWEToLWEKeyPrepared, GLWEToLWEKeyPreparedFactory, LWE, LWEInfos, LWELayout, LWEPlaintext, LWESecret,
14 LWEToGLWEKey, LWEToGLWEKeyLayout, LWEToGLWEKeyPrepared, LWEToGLWEKeyPreparedFactory, Rank, TorusPrecision,
15 prepared::GLWESecretPrepared,
16 },
17};
18
19pub fn test_glwe_base2k_conversion<BE: Backend>(module: &Module<BE>)
20where
21 Module<BE>: GLWEEncryptSk<BE>
22 + GLWEDecrypt<BE>
23 + GLWENormalize<BE>
24 + VecZnxFillUniform
25 + GLWESecretPreparedFactory<BE>
26 + GLWENoise<BE>,
27 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
28 Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
29{
30 let n_glwe: Degree = Degree(module.n() as u32);
31
32 let mut source_xs: Source = Source::new([0u8; 32]);
33 let mut source_xa: Source = Source::new([0u8; 32]);
34 let mut source_xe: Source = Source::new([0u8; 32]);
35
36 for rank in 1_usize..3 {
37 for bases in [[12, 8], [8, 12]] {
38 let glwe_infos_in: GLWELayout = GLWELayout {
39 n: n_glwe,
40 base2k: Base2K(bases[0]),
41 k: TorusPrecision(34),
42 rank: Rank(rank as u32),
43 };
44
45 let glwe_infos_out: GLWELayout = GLWELayout {
46 n: n_glwe,
47 base2k: Base2K(bases[1]),
48 k: TorusPrecision(34),
49 rank: Rank(rank as u32),
50 };
51
52 let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(module.n().into(), rank.into());
53 sk.fill_ternary_prob(0.5, &mut source_xs);
54
55 let mut sk_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk);
56 sk_prep.prepare(module, &sk);
57
58 let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
59 GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos_in).max(module.glwe_noise_tmp_bytes(&glwe_infos_out)),
60 );
61
62 let mut ct_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos_in);
63 let mut ct_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos_out);
64
65 let pt_in: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos_in);
66 let pt_out: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos_out);
67
68 ct_in.encrypt_sk(module, &pt_in, &sk_prep, &mut source_xa, &mut source_xe, scratch.borrow());
69
70 let mut data: Vec<Float> = (0..module.n()).map(|_| Float::with_val(128, 0)).collect();
71 ct_in.data().decode_vec_float(ct_in.base2k().into(), 0, &mut data);
72
73 ct_out.fill_uniform(ct_out.base2k().into(), &mut source_xa);
74 module.glwe_normalize(&mut ct_out, &ct_in, scratch.borrow());
75
76 let mut data_conv: Vec<Float> = (0..module.n()).map(|_| Float::with_val(128, 0)).collect();
77 ct_out.data().decode_vec_float(ct_out.base2k().into(), 0, &mut data_conv);
78
79 assert!(
80 ct_out.noise(module, &pt_out, &sk_prep, scratch.borrow()).std().log2()
81 <= -(ct_out.k().as_u32() as f64) + SIGMA.log2() + 0.50
82 )
83 }
84 }
85}
86
87pub fn test_lwe_to_glwe<BE: Backend>(module: &Module<BE>)
88where
89 Module<BE>: GLWEFromLWE<BE>
90 + LWEToGLWESwitchingKeyEncryptSk<BE>
91 + GLWEDecrypt<BE>
92 + GLWESecretPreparedFactory<BE>
93 + LWEEncryptSk<BE>
94 + LWEToGLWEKeyPreparedFactory<BE>
95 + VecZnxNormalize<BE>,
96 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
97 Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
98{
99 let n_glwe: Degree = Degree(module.n() as u32);
100 let n_lwe: Degree = Degree(22);
101
102 let rank: Rank = Rank(2);
103 let k_lwe_pt: TorusPrecision = TorusPrecision(8);
104
105 let mut source_xs: Source = Source::new([0u8; 32]);
106 let mut source_xa: Source = Source::new([0u8; 32]);
107 let mut source_xe: Source = Source::new([0u8; 32]);
108
109 let lwe_to_glwe_infos: LWEToGLWEKeyLayout = LWEToGLWEKeyLayout {
110 n: n_glwe,
111 base2k: Base2K(13),
112 k: TorusPrecision(92),
113 dnum: Dnum(2),
114 rank_out: rank,
115 };
116
117 let glwe_infos: GLWELayout = GLWELayout {
118 n: n_glwe,
119 base2k: Base2K(15),
120 k: TorusPrecision(75),
121 rank,
122 };
123
124 let lwe_infos: LWELayout = LWELayout {
125 n: n_lwe,
126 base2k: Base2K(17),
127 k: TorusPrecision(75),
128 };
129
130 let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
131 LWEToGLWEKey::encrypt_sk_tmp_bytes(module, &lwe_to_glwe_infos)
132 | GLWE::from_lwe_tmp_bytes(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos)
133 | GLWE::decrypt_tmp_bytes(module, &glwe_infos),
134 );
135
136 let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
137 sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
138
139 let mut sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_glwe);
140 sk_glwe_prepared.prepare(module, &sk_glwe);
141
142 let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
143 sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
144
145 let data: i64 = 17;
146
147 let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos);
148 lwe_pt.encode_i64(data, k_lwe_pt);
149
150 let mut lwe_ct: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos);
151 lwe_ct.encrypt_sk(module, &lwe_pt, &sk_lwe, &mut source_xa, &mut source_xe, scratch.borrow());
152
153 let mut ksk: LWEToGLWEKey<Vec<u8>> = LWEToGLWEKey::alloc_from_infos(&lwe_to_glwe_infos);
154
155 ksk.encrypt_sk(
156 module,
157 &sk_lwe,
158 &sk_glwe_prepared,
159 &mut source_xa,
160 &mut source_xe,
161 scratch.borrow(),
162 );
163
164 let mut glwe_ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
165
166 let mut ksk_prepared: LWEToGLWEKeyPrepared<Vec<u8>, BE> = LWEToGLWEKeyPrepared::alloc_from_infos(module, &ksk);
167 ksk_prepared.prepare(module, &ksk, scratch.borrow());
168
169 glwe_ct.from_lwe(module, &lwe_ct, &ksk_prepared, scratch.borrow());
170
171 let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
172 glwe_ct.decrypt(module, &mut glwe_pt, &sk_glwe_prepared, scratch.borrow());
173
174 let mut lwe_pt_conv = LWEPlaintext::alloc(glwe_pt.base2k(), lwe_pt.k());
175
176 module.vec_znx_normalize(
177 lwe_pt_conv.data_mut(),
178 glwe_pt.base2k().as_usize(),
179 0,
180 0,
181 lwe_pt.data(),
182 lwe_pt.base2k().as_usize(),
183 0,
184 scratch.borrow(),
185 );
186
187 assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt_conv.data.at(0, 0)[0]);
188}
189
190pub fn test_glwe_to_lwe<BE: Backend>(module: &Module<BE>)
191where
192 Module<BE>: GLWEFromLWE<BE>
193 + GLWEToLWESwitchingKeyEncryptSk<BE>
194 + GLWEEncryptSk<BE>
195 + LWEDecrypt<BE>
196 + LWEFromGLWE<BE>
197 + GLWEDecrypt<BE>
198 + GLWESecretPreparedFactory<BE>
199 + GLWEToLWESwitchingKeyEncryptSk<BE>
200 + GLWEToLWEKeyPreparedFactory<BE>
201 + VecZnxNormalize<BE>,
202 ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
203 Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
204{
205 let n_glwe: Degree = Degree(module.n() as u32);
206 let n_lwe: Degree = Degree(22);
207
208 let rank: Rank = Rank(2);
209 let k_lwe_pt: TorusPrecision = TorusPrecision(8);
210
211 let glwe_to_lwe_infos: GLWEToLWEKeyLayout = GLWEToLWEKeyLayout {
212 n: n_glwe,
213 base2k: Base2K(13),
214 k: TorusPrecision(91),
215 dnum: Dnum(2),
216 rank_in: rank,
217 };
218
219 let glwe_infos: GLWELayout = GLWELayout {
220 n: n_glwe,
221 base2k: Base2K(17),
222 k: TorusPrecision(72),
223 rank,
224 };
225
226 let lwe_infos: LWELayout = LWELayout {
227 n: n_lwe,
228 base2k: Base2K(15),
229 k: TorusPrecision(72),
230 };
231
232 let mut source_xs: Source = Source::new([0u8; 32]);
233 let mut source_xa: Source = Source::new([0u8; 32]);
234 let mut source_xe: Source = Source::new([0u8; 32]);
235
236 let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
237 GLWEToLWEKey::encrypt_sk_tmp_bytes(module, &glwe_to_lwe_infos)
238 | LWE::from_glwe_tmp_bytes(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos)
239 | GLWE::decrypt_tmp_bytes(module, &glwe_infos),
240 );
241
242 let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
243 sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
244
245 let mut sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_glwe);
246 sk_glwe_prepared.prepare(module, &sk_glwe);
247
248 let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
249 sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
250
251 let a_idx: usize = 1;
252
253 let mut data: Vec<i64> = vec![0i64; module.n()];
254 data[a_idx] = 17;
255 let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
256 glwe_pt.encode_vec_i64(&data, k_lwe_pt);
257
258 let mut glwe_ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
259 glwe_ct.encrypt_sk(
260 module,
261 &glwe_pt,
262 &sk_glwe_prepared,
263 &mut source_xa,
264 &mut source_xe,
265 scratch.borrow(),
266 );
267
268 let mut ksk: GLWEToLWEKey<Vec<u8>> = GLWEToLWEKey::alloc_from_infos(&glwe_to_lwe_infos);
269
270 ksk.encrypt_sk(module, &sk_lwe, &sk_glwe, &mut source_xa, &mut source_xe, scratch.borrow());
271
272 let mut lwe_ct: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos);
273
274 let mut ksk_prepared: GLWEToLWEKeyPrepared<Vec<u8>, BE> = GLWEToLWEKeyPrepared::alloc_from_infos(module, &ksk);
275 ksk_prepared.prepare(module, &ksk, scratch.borrow());
276
277 lwe_ct.from_glwe(module, &glwe_ct, a_idx, &ksk_prepared, scratch.borrow());
278
279 let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos);
280 lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe, scratch.borrow());
281
282 let mut glwe_pt_conv = GLWEPlaintext::alloc(glwe_ct.n(), lwe_pt.base2k(), lwe_pt.k());
283
284 module.vec_znx_normalize(
285 glwe_pt_conv.data_mut(),
286 lwe_pt.base2k().as_usize(),
287 0,
288 0,
289 glwe_pt.data(),
290 glwe_ct.base2k().as_usize(),
291 0,
292 scratch.borrow(),
293 );
294
295 assert_eq!(glwe_pt_conv.data.at(0, 0)[a_idx], lwe_pt.data.at(0, 0)[0]);
296}