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