arnaucube
arnaucube/poulpyPublic Self-hosted repository
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},
  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(GLWE::decrypt_tmp_bytes(module, &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_in);
 67
 68            ct_in.encrypt_sk(
 69                module,
 70                &pt_in,
 71                &sk_prep,
 72                &mut source_xa,
 73                &mut source_xe,
 74                scratch.borrow(),
 75            );
 76
 77            let mut data: Vec<Float> = (0..module.n()).map(|_| Float::with_val(128, 0)).collect();
 78            ct_in
 79                .data()
 80                .decode_vec_float(ct_in.base2k().into(), 0, &mut data);
 81
 82            ct_out.fill_uniform(ct_out.base2k().into(), &mut source_xa);
 83            module.glwe_normalize(&mut ct_out, &ct_in, scratch.borrow());
 84
 85            let mut data_conv: Vec<Float> = (0..module.n()).map(|_| Float::with_val(128, 0)).collect();
 86            ct_out
 87                .data()
 88                .decode_vec_float(ct_out.base2k().into(), 0, &mut data_conv);
 89
 90            ct_out.assert_noise(
 91                module,
 92                &sk_prep,
 93                &pt_out,
 94                -(ct_out.k().as_u32() as f64) + SIGMA.log2() + 0.5,
 95            );
 96        }
 97    }
 98}
 99
100pub fn test_lwe_to_glwe<BE: Backend>(module: &Module<BE>)
101where
102    Module<BE>: GLWEFromLWE<BE>
103        + LWEToGLWESwitchingKeyEncryptSk<BE>
104        + GLWEDecrypt<BE>
105        + GLWESecretPreparedFactory<BE>
106        + LWEEncryptSk<BE>
107        + LWEToGLWEKeyPreparedFactory<BE>,
108    ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
109    Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
110{
111    let n_glwe: Degree = Degree(module.n() as u32);
112    let n_lwe: Degree = Degree(22);
113
114    let rank: Rank = Rank(2);
115    let k_lwe_pt: TorusPrecision = TorusPrecision(8);
116
117    let mut source_xs: Source = Source::new([0u8; 32]);
118    let mut source_xa: Source = Source::new([0u8; 32]);
119    let mut source_xe: Source = Source::new([0u8; 32]);
120
121    let lwe_to_glwe_infos: LWEToGLWEKeyLayout = LWEToGLWEKeyLayout {
122        n: n_glwe,
123        base2k: Base2K(17),
124        k: TorusPrecision(51),
125        dnum: Dnum(2),
126        rank_out: rank,
127    };
128
129    let glwe_infos: GLWELayout = GLWELayout {
130        n: n_glwe,
131        base2k: Base2K(17),
132        k: TorusPrecision(34),
133        rank,
134    };
135
136    let lwe_infos: LWELayout = LWELayout {
137        n: n_lwe,
138        base2k: Base2K(17),
139        k: TorusPrecision(34),
140    };
141
142    let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
143        LWEToGLWEKey::encrypt_sk_tmp_bytes(module, &lwe_to_glwe_infos)
144            | GLWE::from_lwe_tmp_bytes(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos)
145            | GLWE::decrypt_tmp_bytes(module, &glwe_infos),
146    );
147
148    let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
149    sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
150
151    let mut sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_glwe);
152    sk_glwe_prepared.prepare(module, &sk_glwe);
153
154    let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
155    sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
156
157    let data: i64 = 17;
158
159    let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos);
160    lwe_pt.encode_i64(data, k_lwe_pt);
161
162    let mut lwe_ct: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos);
163    lwe_ct.encrypt_sk(module, &lwe_pt, &sk_lwe, &mut source_xa, &mut source_xe);
164
165    let mut ksk: LWEToGLWEKey<Vec<u8>> = LWEToGLWEKey::alloc_from_infos(&lwe_to_glwe_infos);
166
167    ksk.encrypt_sk(
168        module,
169        &sk_lwe,
170        &sk_glwe_prepared,
171        &mut source_xa,
172        &mut source_xe,
173        scratch.borrow(),
174    );
175
176    let mut glwe_ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
177
178    let mut ksk_prepared: LWEToGLWEKeyPrepared<Vec<u8>, BE> = LWEToGLWEKeyPrepared::alloc_from_infos(module, &ksk);
179    ksk_prepared.prepare(module, &ksk, scratch.borrow());
180
181    glwe_ct.from_lwe(module, &lwe_ct, &ksk_prepared, scratch.borrow());
182
183    let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
184    glwe_ct.decrypt(module, &mut glwe_pt, &sk_glwe_prepared, scratch.borrow());
185
186    assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]);
187}
188
189pub fn test_glwe_to_lwe<BE: Backend>(module: &Module<BE>)
190where
191    Module<BE>: GLWEFromLWE<BE>
192        + GLWEToLWESwitchingKeyEncryptSk<BE>
193        + GLWEEncryptSk<BE>
194        + LWEDecrypt<BE>
195        + LWEFromGLWE<BE>
196        + GLWEDecrypt<BE>
197        + GLWESecretPreparedFactory<BE>
198        + GLWEToLWESwitchingKeyEncryptSk<BE>
199        + GLWEToLWEKeyPreparedFactory<BE>,
200    ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
201    Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
202{
203    let n_glwe: Degree = Degree(module.n() as u32);
204    let n_lwe: Degree = Degree(22);
205
206    let rank: Rank = Rank(2);
207    let k_lwe_pt: TorusPrecision = TorusPrecision(8);
208
209    let glwe_to_lwe_infos: GLWEToLWEKeyLayout = GLWEToLWEKeyLayout {
210        n: n_glwe,
211        base2k: Base2K(17),
212        k: TorusPrecision(51),
213        dnum: Dnum(2),
214        rank_in: rank,
215    };
216
217    let glwe_infos: GLWELayout = GLWELayout {
218        n: n_glwe,
219        base2k: Base2K(17),
220        k: TorusPrecision(34),
221        rank,
222    };
223
224    let lwe_infos: LWELayout = LWELayout {
225        n: n_lwe,
226        base2k: Base2K(17),
227        k: TorusPrecision(34),
228    };
229
230    let mut source_xs: Source = Source::new([0u8; 32]);
231    let mut source_xa: Source = Source::new([0u8; 32]);
232    let mut source_xe: Source = Source::new([0u8; 32]);
233
234    let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
235        GLWEToLWEKey::encrypt_sk_tmp_bytes(module, &glwe_to_lwe_infos)
236            | LWE::from_glwe_tmp_bytes(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos)
237            | GLWE::decrypt_tmp_bytes(module, &glwe_infos),
238    );
239
240    let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
241    sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
242
243    let mut sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_glwe);
244    sk_glwe_prepared.prepare(module, &sk_glwe);
245
246    let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
247    sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
248
249    let a_idx: usize = 1;
250
251    let mut data: Vec<i64> = vec![0i64; module.n()];
252    data[a_idx] = 17;
253    let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
254    glwe_pt.encode_vec_i64(&data, k_lwe_pt);
255
256    println!("glwe_pt: {glwe_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(
271        module,
272        &sk_lwe,
273        &sk_glwe,
274        &mut source_xa,
275        &mut source_xe,
276        scratch.borrow(),
277    );
278
279    let mut lwe_ct: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos);
280
281    let mut ksk_prepared: GLWEToLWEKeyPrepared<Vec<u8>, BE> = GLWEToLWEKeyPrepared::alloc_from_infos(module, &ksk);
282    ksk_prepared.prepare(module, &ksk, scratch.borrow());
283
284    lwe_ct.from_glwe(module, &glwe_ct, a_idx, &ksk_prepared, scratch.borrow());
285
286    let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos);
287    lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe);
288
289    assert_eq!(glwe_pt.data.at(0, 0)[a_idx], lwe_pt.data.at(0, 0)[0]);
290}