poulpy/core/src/tests/generics/conversion.rs
core/src/tests/generics/conversion.rsRaw
1use backend::hal::{
2 api::{
3 ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, VecZnxAddInplace,
4 VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
5 VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAllocBytes, VecZnxDftFromVecZnx,
6 VecZnxDftToVecZnxBigConsume, VecZnxFillUniform, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes,
7 VecZnxSub, VecZnxSubABInplace, VecZnxSwithcDegree, VmpApply, VmpApplyAdd, VmpApplyTmpBytes, VmpPMatAlloc, VmpPrepare,
8 ZnxView,
9 },
10 layouts::{Backend, Module, ScratchOwned},
11 oep::{
12 ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
13 TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl,
14 },
15};
16use sampling::source::Source;
17
18use crate::layouts::{
19 GLWECiphertext, GLWEPlaintext, GLWESecret, GLWEToLWESwitchingKey, Infos, LWECiphertext, LWEPlaintext, LWESecret,
20 LWEToGLWESwitchingKey,
21 prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared, PrepareAlloc},
22};
23
24pub fn test_lwe_to_glwe<B: Backend>(module: &Module<B>)
25where
26 Module<B>: VecZnxDftAllocBytes
27 + VecZnxBigNormalize<B>
28 + VecZnxDftFromVecZnx<B>
29 + SvpApplyInplace<B>
30 + VecZnxDftToVecZnxBigConsume<B>
31 + VecZnxFillUniform
32 + VecZnxSubABInplace
33 + VecZnxAddInplace
34 + VecZnxNormalizeInplace<B>
35 + VecZnxAddNormal
36 + VecZnxNormalize<B>
37 + VecZnxSub
38 + SvpPrepare<B>
39 + SvpPPolAllocBytes
40 + SvpPPolAlloc<B>
41 + VecZnxBigAllocBytes
42 + VecZnxBigAddInplace<B>
43 + VecZnxBigAddSmallInplace<B>
44 + VecZnxNormalizeTmpBytes
45 + VecZnxAddScalarInplace
46 + VmpPMatAlloc<B>
47 + VmpPrepare<B>
48 + VmpApplyTmpBytes
49 + VmpApply<B>
50 + VmpApplyAdd<B>
51 + VecZnxBigNormalizeTmpBytes
52 + VecZnxSwithcDegree
53 + VecZnxAutomorphismInplace,
54 B: TakeVecZnxDftImpl<B>
55 + TakeVecZnxBigImpl<B>
56 + TakeSvpPPolImpl<B>
57 + ScratchOwnedAllocImpl<B>
58 + ScratchOwnedBorrowImpl<B>
59 + ScratchAvailableImpl<B>
60 + TakeScalarZnxImpl<B>
61 + TakeVecZnxImpl<B>,
62{
63 let n: usize = module.n();
64 let basek: usize = 17;
65 let sigma: f64 = 3.2;
66
67 let rank: usize = 2;
68
69 let n_lwe: usize = 22;
70 let k_lwe_ct: usize = 2 * basek;
71 let k_lwe_pt: usize = 8;
72
73 let k_glwe_ct: usize = 3 * basek;
74
75 let k_ksk: usize = k_lwe_ct + basek;
76
77 let mut source_xs: Source = Source::new([0u8; 32]);
78 let mut source_xa: Source = Source::new([0u8; 32]);
79 let mut source_xe: Source = Source::new([0u8; 32]);
80
81 let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
82 LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k_ksk, rank)
83 | GLWECiphertext::from_lwe_scratch_space(module, n, basek, k_lwe_ct, k_glwe_ct, k_ksk, rank)
84 | GLWECiphertext::decrypt_scratch_space(module, n, basek, k_glwe_ct),
85 );
86
87 let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
88 sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
89
90 let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
91
92 let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
93 sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
94
95 let data: i64 = 17;
96
97 let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_pt);
98 lwe_pt.encode_i64(data, k_lwe_pt);
99
100 let mut lwe_ct: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe_ct);
101 lwe_ct.encrypt_sk(
102 module,
103 &lwe_pt,
104 &sk_lwe,
105 &mut source_xa,
106 &mut source_xe,
107 sigma,
108 );
109
110 let mut ksk: LWEToGLWESwitchingKey<Vec<u8>> = LWEToGLWESwitchingKey::alloc(n, basek, k_ksk, lwe_ct.size(), rank);
111
112 ksk.encrypt_sk(
113 module,
114 &sk_lwe,
115 &sk_glwe,
116 &mut source_xa,
117 &mut source_xe,
118 sigma,
119 scratch.borrow(),
120 );
121
122 let mut glwe_ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_glwe_ct, rank);
123
124 let ksk_prepared: LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
125
126 glwe_ct.from_lwe(module, &lwe_ct, &ksk_prepared, scratch.borrow());
127
128 let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_glwe_ct);
129 glwe_ct.decrypt(module, &mut glwe_pt, &sk_glwe_prepared, scratch.borrow());
130
131 assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]);
132}
133
134pub fn test_glwe_to_lwe<B: Backend>(module: &Module<B>)
135where
136 Module<B>: VecZnxDftAllocBytes
137 + VecZnxBigNormalize<B>
138 + VecZnxDftFromVecZnx<B>
139 + SvpApplyInplace<B>
140 + VecZnxDftToVecZnxBigConsume<B>
141 + VecZnxFillUniform
142 + VecZnxSubABInplace
143 + VecZnxAddInplace
144 + VecZnxNormalizeInplace<B>
145 + VecZnxAddNormal
146 + VecZnxNormalize<B>
147 + VecZnxSub
148 + SvpPrepare<B>
149 + SvpPPolAllocBytes
150 + SvpPPolAlloc<B>
151 + VecZnxBigAllocBytes
152 + VecZnxBigAddInplace<B>
153 + VecZnxBigAddSmallInplace<B>
154 + VecZnxNormalizeTmpBytes
155 + VecZnxAddScalarInplace
156 + VmpPMatAlloc<B>
157 + VmpPrepare<B>
158 + VmpApplyTmpBytes
159 + VmpApply<B>
160 + VmpApplyAdd<B>
161 + VecZnxBigNormalizeTmpBytes
162 + VecZnxSwithcDegree
163 + VecZnxAutomorphismInplace,
164 B: TakeVecZnxDftImpl<B>
165 + TakeVecZnxBigImpl<B>
166 + TakeSvpPPolImpl<B>
167 + ScratchOwnedAllocImpl<B>
168 + ScratchOwnedBorrowImpl<B>
169 + ScratchAvailableImpl<B>
170 + TakeScalarZnxImpl<B>
171 + TakeVecZnxImpl<B>,
172{
173 let n: usize = module.n();
174 let basek: usize = 17;
175 let sigma: f64 = 3.2;
176
177 let rank: usize = 2;
178
179 let n_lwe: usize = 22;
180 let k_lwe_ct: usize = 2 * basek;
181 let k_lwe_pt: usize = 8;
182
183 let k_glwe_ct: usize = 3 * basek;
184
185 let k_ksk: usize = k_lwe_ct + basek;
186
187 let mut source_xs: Source = Source::new([0u8; 32]);
188 let mut source_xa: Source = Source::new([0u8; 32]);
189 let mut source_xe: Source = Source::new([0u8; 32]);
190
191 let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
192 LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k_ksk, rank)
193 | LWECiphertext::from_glwe_scratch_space(module, n, basek, k_lwe_ct, k_glwe_ct, k_ksk, rank)
194 | GLWECiphertext::decrypt_scratch_space(module, n, basek, k_glwe_ct),
195 );
196
197 let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
198 sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
199
200 let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
201
202 let mut sk_lwe = LWESecret::alloc(n_lwe);
203 sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
204
205 let data: i64 = 17;
206 let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_glwe_ct);
207 glwe_pt.encode_coeff_i64(data, k_lwe_pt, 0);
208
209 let mut glwe_ct = GLWECiphertext::alloc(n, basek, k_glwe_ct, rank);
210 glwe_ct.encrypt_sk(
211 module,
212 &glwe_pt,
213 &sk_glwe_prepared,
214 &mut source_xa,
215 &mut source_xe,
216 sigma,
217 scratch.borrow(),
218 );
219
220 let mut ksk: GLWEToLWESwitchingKey<Vec<u8>> = GLWEToLWESwitchingKey::alloc(n, basek, k_ksk, glwe_ct.size(), rank);
221
222 ksk.encrypt_sk(
223 module,
224 &sk_lwe,
225 &sk_glwe,
226 &mut source_xa,
227 &mut source_xe,
228 sigma,
229 scratch.borrow(),
230 );
231
232 let mut lwe_ct: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe_ct);
233
234 let ksk_prepared: GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
235
236 lwe_ct.from_glwe(module, &glwe_ct, &ksk_prepared, scratch.borrow());
237
238 let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_ct);
239 lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe);
240
241 assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]);
242}