arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs
poulpy-schemes/src/tfhe/bdd_arithmetic/test.rsRaw
  1use std::time::Instant;
  2
  3use poulpy_backend::FFT64Ref;
  4use poulpy_core::{
  5    GGSWNoise, GLWEDecrypt, GLWENoise, ScratchTakeCore,
  6    layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, LWESecret, prepared::GLWESecretPrepared},
  7};
  8use poulpy_hal::{
  9    api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
 10    layouts::{Backend, Module, Scratch, ScratchOwned},
 11    source::Source,
 12};
 13use rand::RngCore;
 14
 15use crate::tfhe::{
 16    bdd_arithmetic::{
 17        Add, BDDKey, BDDKeyEncryptSk, BDDKeyLayout, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W,
 18        FheUintBlockDebugPrepare, FheUintBlocks, FheUintBlocksPrepare, FheUintBlocksPrepared, FheUintBlocksPreparedDebug,
 19        FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, Sub, TEST_BDD_KEY_LAYOUT, TEST_BLOCK_SIZE, TEST_GGSW_INFOS,
 20        TEST_GLWE_INFOS, TEST_N_LWE,
 21    },
 22    blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory, CGGI},
 23};
 24
 25#[test]
 26fn test_bdd_2w_to_1w_fft64_ref() {
 27    test_bdd_2w_to_1w::<FFT64Ref, CGGI>()
 28}
 29
 30fn test_bdd_2w_to_1w<BE: Backend, BRA: BlindRotationAlgo>()
 31where
 32    Module<BE>: ModuleNew<BE>
 33        + GLWESecretPreparedFactory<BE>
 34        + GLWEDecrypt<BE>
 35        + GLWENoise<BE>
 36        + FheUintBlocksPreparedFactory<u32, BE>
 37        + FheUintBlocksPreparedEncryptSk<u32, BE>
 38        + FheUintBlockDebugPrepare<BRA, u32, BE>
 39        + BDDKeyEncryptSk<BRA, BE>
 40        + BDDKeyPreparedFactory<BRA, BE>
 41        + GGSWNoise<BE>
 42        + FheUintBlocksPrepare<BRA, u32, BE>
 43        + ExecuteBDDCircuit2WTo1W<u32, BE>,
 44    BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>,
 45    ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
 46    Scratch<BE>: ScratchTakeCore<BE>,
 47{
 48    let glwe_infos: GLWELayout = TEST_GLWE_INFOS;
 49    let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS;
 50
 51    let n_glwe: usize = glwe_infos.n().into();
 52
 53    let module: Module<BE> = Module::<BE>::new(n_glwe as u64);
 54    let mut source: Source = Source::new([6u8; 32]);
 55    let mut source_xs: Source = Source::new([1u8; 32]);
 56    let mut source_xa: Source = Source::new([2u8; 32]);
 57    let mut source_xe: Source = Source::new([3u8; 32]);
 58
 59    let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22);
 60
 61    let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
 62    sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
 63    let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos);
 64    sk_glwe_prep.prepare(&module, &sk_glwe);
 65
 66    let a: u32 = source.next_u32();
 67    let b: u32 = source.next_u32();
 68
 69    println!("a: {a}");
 70    println!("b: {b}");
 71
 72    let mut a_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> =
 73        FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
 74    let mut b_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> =
 75        FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
 76    let mut c_enc: FheUintBlocks<Vec<u8>, u32> = FheUintBlocks::<Vec<u8>, u32>::alloc(&module, &glwe_infos);
 77    let mut c_enc_prep_debug: FheUintBlocksPreparedDebug<Vec<u8>, u32> =
 78        FheUintBlocksPreparedDebug::<Vec<u8>, u32>::alloc(&module, &ggsw_infos);
 79    let mut c_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> =
 80        FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
 81
 82    a_enc_prep.encrypt_sk(
 83        &module,
 84        a,
 85        &sk_glwe_prep,
 86        &mut source_xa,
 87        &mut source_xe,
 88        scratch.borrow(),
 89    );
 90    b_enc_prep.encrypt_sk(
 91        &module,
 92        b,
 93        &sk_glwe_prep,
 94        &mut source_xa,
 95        &mut source_xe,
 96        scratch.borrow(),
 97    );
 98
 99    let start: Instant = Instant::now();
100    c_enc.sub(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
101
102    let duration: std::time::Duration = start.elapsed();
103    println!("add: {} ms", duration.as_millis());
104
105    println!(
106        "have: {}",
107        c_enc.decrypt(&module, &sk_glwe_prep, scratch.borrow())
108    );
109    println!("want: {}", a.wrapping_sub(b));
110    println!(
111        "noise: {:?}",
112        c_enc.noise(&module, &sk_glwe_prep, a.wrapping_sub(b), scratch.borrow())
113    );
114
115    let n_lwe: u32 = TEST_N_LWE;
116    let block_size: u32 = TEST_BLOCK_SIZE;
117
118    let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into());
119    sk_lwe.fill_binary_block(block_size as usize, &mut source_xs);
120
121    let bdd_key_infos: BDDKeyLayout = TEST_BDD_KEY_LAYOUT;
122
123    let mut bdd_key: BDDKey<Vec<u8>, BRA> = BDDKey::alloc_from_infos(&bdd_key_infos);
124
125    let now: Instant = Instant::now();
126    bdd_key.encrypt_sk(
127        &module,
128        &sk_lwe,
129        &sk_glwe,
130        &mut source_xa,
131        &mut source_xe,
132        scratch.borrow(),
133    );
134    let mut bdd_key_prepared: BDDKeyPrepared<Vec<u8>, BRA, BE> = BDDKeyPrepared::alloc_from_infos(&module, &bdd_key_infos);
135    bdd_key_prepared.prepare(&module, &bdd_key, scratch.borrow());
136    println!("BDD-KGEN: {} ms", now.elapsed().as_millis());
137
138    let now: Instant = Instant::now();
139    c_enc_prep_debug.prepare(&module, &c_enc, &bdd_key_prepared, scratch.borrow());
140    println!("CBT: {} ms", now.elapsed().as_millis());
141
142    c_enc_prep_debug.noise(&module, &sk_glwe_prep, a.wrapping_sub(b));
143
144    let now: Instant = Instant::now();
145    c_enc_prep.prepare(&module, &c_enc, &bdd_key_prepared, scratch.borrow());
146    println!("CBT: {} ms", now.elapsed().as_millis());
147
148    let start: Instant = Instant::now();
149    c_enc.add(&module, &c_enc_prep, &b_enc_prep, scratch.borrow());
150
151    let duration: std::time::Duration = start.elapsed();
152    println!("add: {} ms", duration.as_millis());
153
154    println!(
155        "have: {}",
156        c_enc.decrypt(&module, &sk_glwe_prep, scratch.borrow())
157    );
158    println!("want: {}", b.wrapping_add(a.wrapping_sub(b)));
159    println!(
160        "noise: {:?}",
161        c_enc.noise(
162            &module,
163            &sk_glwe_prep,
164            b.wrapping_add(a.wrapping_sub(b)),
165            scratch.borrow()
166        )
167    );
168}