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