arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/tests/generics/conversion.rs
poulpy-core/src/tests/generics/conversion.rsRaw
  1use poulpy_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    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        + 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
 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(module, &lwe_pt, &sk_lwe, &mut source_xa, &mut source_xe);
102
103    let mut ksk: LWEToGLWESwitchingKey<Vec<u8>> = LWEToGLWESwitchingKey::alloc(n, basek, k_ksk, lwe_ct.size(), rank);
104
105    ksk.encrypt_sk(
106        module,
107        &sk_lwe,
108        &sk_glwe,
109        &mut source_xa,
110        &mut source_xe,
111        scratch.borrow(),
112    );
113
114    let mut glwe_ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_glwe_ct, rank);
115
116    let ksk_prepared: LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
117
118    glwe_ct.from_lwe(module, &lwe_ct, &ksk_prepared, scratch.borrow());
119
120    let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_glwe_ct);
121    glwe_ct.decrypt(module, &mut glwe_pt, &sk_glwe_prepared, scratch.borrow());
122
123    assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]);
124}
125
126pub fn test_glwe_to_lwe<B>(module: &Module<B>)
127where
128    Module<B>: VecZnxDftAllocBytes
129        + VecZnxBigNormalize<B>
130        + VecZnxDftFromVecZnx<B>
131        + SvpApplyInplace<B>
132        + VecZnxDftToVecZnxBigConsume<B>
133        + VecZnxFillUniform
134        + VecZnxSubABInplace
135        + VecZnxAddInplace
136        + VecZnxNormalizeInplace<B>
137        + VecZnxAddNormal
138        + VecZnxNormalize<B>
139        + VecZnxSub
140        + SvpPrepare<B>
141        + SvpPPolAllocBytes
142        + SvpPPolAlloc<B>
143        + VecZnxBigAllocBytes
144        + VecZnxBigAddInplace<B>
145        + VecZnxBigAddSmallInplace<B>
146        + VecZnxNormalizeTmpBytes
147        + VecZnxAddScalarInplace
148        + VmpPMatAlloc<B>
149        + VmpPrepare<B>
150        + VmpApplyTmpBytes
151        + VmpApply<B>
152        + VmpApplyAdd<B>
153        + VecZnxBigNormalizeTmpBytes
154        + VecZnxSwithcDegree
155        + VecZnxAutomorphismInplace,
156    B: Backend
157        + TakeVecZnxDftImpl<B>
158        + TakeVecZnxBigImpl<B>
159        + TakeSvpPPolImpl<B>
160        + ScratchOwnedAllocImpl<B>
161        + ScratchOwnedBorrowImpl<B>
162        + ScratchAvailableImpl<B>
163        + TakeScalarZnxImpl<B>
164        + TakeVecZnxImpl<B>,
165{
166    let n: usize = module.n();
167    let basek: usize = 17;
168
169    let rank: usize = 2;
170
171    let n_lwe: usize = 22;
172    let k_lwe_ct: usize = 2 * basek;
173    let k_lwe_pt: usize = 8;
174
175    let k_glwe_ct: usize = 3 * basek;
176
177    let k_ksk: usize = k_lwe_ct + basek;
178
179    let mut source_xs: Source = Source::new([0u8; 32]);
180    let mut source_xa: Source = Source::new([0u8; 32]);
181    let mut source_xe: Source = Source::new([0u8; 32]);
182
183    let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
184        LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k_ksk, rank)
185            | LWECiphertext::from_glwe_scratch_space(module, n, basek, k_lwe_ct, k_glwe_ct, k_ksk, rank)
186            | GLWECiphertext::decrypt_scratch_space(module, n, basek, k_glwe_ct),
187    );
188
189    let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
190    sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
191
192    let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
193
194    let mut sk_lwe = LWESecret::alloc(n_lwe);
195    sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
196
197    let data: i64 = 17;
198    let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_glwe_ct);
199    glwe_pt.encode_coeff_i64(data, k_lwe_pt, 0);
200
201    let mut glwe_ct = GLWECiphertext::alloc(n, basek, k_glwe_ct, rank);
202    glwe_ct.encrypt_sk(
203        module,
204        &glwe_pt,
205        &sk_glwe_prepared,
206        &mut source_xa,
207        &mut source_xe,
208        scratch.borrow(),
209    );
210
211    let mut ksk: GLWEToLWESwitchingKey<Vec<u8>> = GLWEToLWESwitchingKey::alloc(n, basek, k_ksk, glwe_ct.size(), rank);
212
213    ksk.encrypt_sk(
214        module,
215        &sk_lwe,
216        &sk_glwe,
217        &mut source_xa,
218        &mut source_xe,
219        scratch.borrow(),
220    );
221
222    let mut lwe_ct: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe_ct);
223
224    let ksk_prepared: GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
225
226    lwe_ct.from_glwe(module, &glwe_ct, &ksk_prepared, scratch.borrow());
227
228    let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_ct);
229    lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe);
230
231    assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]);
232}