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    TakeGGSW, TakeGLWEPt,
  6    layouts::{
  7        GGSWCiphertextLayout, GLWECiphertextLayout, GLWESecret, LWEInfos, LWESecret,
  8        prepared::{GLWESecretPrepared, PrepareAlloc},
  9    },
 10};
 11use poulpy_hal::{
 12    api::{
 13        ModuleNew, ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace,
 14        SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeSlice, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft,
 15        VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace,
 16        VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace,
 17        VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAddInplace,
 18        VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume,
 19        VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate,
 20        VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing,
 21        VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform,
 22        ZnNormalizeInplace,
 23    },
 24    layouts::{Backend, Module, Scratch, ScratchOwned},
 25    oep::{
 26        ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeMatZnxImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
 27        TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl,
 28    },
 29    source::Source,
 30};
 31use rand::RngCore;
 32
 33use crate::tfhe::{
 34    bdd_arithmetic::{
 35        Add, BDDKey, BDDKeyLayout, BDDKeyPrepared, FheUintBlocks, FheUintBlocksPrep, FheUintBlocksPrepDebug, Sub,
 36        TEST_BDD_KEY_LAYOUT, TEST_BLOCK_SIZE, TEST_GGSW_INFOS, TEST_GLWE_INFOS, TEST_N_LWE,
 37    },
 38    blind_rotation::{
 39        BlincRotationExecute, BlindRotationAlgo, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk,
 40        BlindRotationKeyPrepared, CGGI,
 41    },
 42};
 43
 44#[test]
 45fn test_bdd_2w_to_1w_fft64_ref() {
 46    test_bdd_2w_to_1w::<FFT64Ref, CGGI>()
 47}
 48
 49fn test_bdd_2w_to_1w<BE: Backend, BRA: BlindRotationAlgo>()
 50where
 51    Module<BE>: ModuleNew<BE> + SvpPPolAlloc<BE> + SvpPrepare<BE> + VmpPMatAlloc<BE>,
 52    ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
 53    Module<BE>: VecZnxAddScalarInplace
 54        + VecZnxDftAllocBytes
 55        + VecZnxBigNormalize<BE>
 56        + VecZnxDftApply<BE>
 57        + SvpApplyDftToDftInplace<BE>
 58        + VecZnxIdftApplyConsume<BE>
 59        + VecZnxNormalizeTmpBytes
 60        + VecZnxFillUniform
 61        + VecZnxSubInplace
 62        + VecZnxAddInplace
 63        + VecZnxNormalizeInplace<BE>
 64        + VecZnxAddNormal
 65        + VecZnxNormalize<BE>
 66        + VecZnxSub
 67        + VmpPrepare<BE>,
 68    Scratch<BE>: TakeVecZnxDft<BE> + ScratchAvailable + TakeVecZnx + TakeGGSW + TakeScalarZnx + TakeSlice,
 69    Module<BE>: VecZnxCopy + VecZnxNegateInplace + VmpApplyDftToDftTmpBytes + VmpApplyDftToDft<BE> + VmpApplyDftToDftAdd<BE>,
 70    Module<BE>: VecZnxBigAddInplace<BE> + VecZnxBigAddSmallInplace<BE> + VecZnxBigNormalize<BE>,
 71    Scratch<BE>: TakeVecZnxDft<BE> + TakeVecZnxBig<BE> + TakeGLWEPt<BE>,
 72    Module<BE>: VecZnxAutomorphism
 73        + VecZnxSwitchRing
 74        + VecZnxBigAllocBytes
 75        + VecZnxIdftApplyTmpA<BE>
 76        + SvpApplyDftToDft<BE>
 77        + VecZnxBigAlloc<BE>
 78        + VecZnxDftAlloc<BE>
 79        + VecZnxBigNormalizeTmpBytes
 80        + SvpPPolAllocBytes
 81        + VecZnxRotateInplace<BE>
 82        + VecZnxBigAutomorphismInplace<BE>
 83        + VecZnxRshInplace<BE>
 84        + VecZnxDftCopy<BE>
 85        + VecZnxAutomorphismInplace<BE>
 86        + VecZnxBigSubSmallNegateInplace<BE>
 87        + VecZnxRotateInplaceTmpBytes
 88        + VecZnxBigAllocBytes
 89        + VecZnxDftAddInplace<BE>
 90        + VecZnxRotate
 91        + ZnFillUniform
 92        + ZnAddNormal
 93        + ZnNormalizeInplace<BE>,
 94    BE: Backend
 95        + ScratchOwnedAllocImpl<BE>
 96        + ScratchOwnedBorrowImpl<BE>
 97        + TakeVecZnxDftImpl<BE>
 98        + ScratchAvailableImpl<BE>
 99        + TakeVecZnxImpl<BE>
100        + TakeScalarZnxImpl<BE>
101        + TakeSvpPPolImpl<BE>
102        + TakeVecZnxBigImpl<BE>
103        + TakeVecZnxDftSliceImpl<BE>
104        + TakeMatZnxImpl<BE>
105        + TakeVecZnxSliceImpl<BE>,
106    BlindRotationKey<Vec<u8>, BRA>: PrepareAlloc<BE, BlindRotationKeyPrepared<Vec<u8>, BRA, BE>>,
107    BlindRotationKeyPrepared<Vec<u8>, BRA, BE>: BlincRotationExecute<BE>,
108    BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk<BE>,
109{
110    let glwe_infos: GLWECiphertextLayout = TEST_GLWE_INFOS;
111    let ggsw_infos: GGSWCiphertextLayout = TEST_GGSW_INFOS;
112
113    let n_glwe: usize = glwe_infos.n().into();
114
115    let module: Module<BE> = Module::<BE>::new(n_glwe as u64);
116    let mut source: Source = Source::new([6u8; 32]);
117    let mut source_xs: Source = Source::new([1u8; 32]);
118    let mut source_xa: Source = Source::new([2u8; 32]);
119    let mut source_xe: Source = Source::new([3u8; 32]);
120
121    let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22);
122
123    let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_infos);
124    sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
125    let sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = sk_glwe.prepare_alloc(&module, scratch.borrow());
126
127    let a: u32 = source.next_u32();
128    let b: u32 = source.next_u32();
129
130    println!("a: {a}");
131    println!("b: {b}");
132
133    let mut a_enc_prep: FheUintBlocksPrep<Vec<u8>, BE, u32> = FheUintBlocksPrep::<Vec<u8>, BE, u32>::alloc(&module, &ggsw_infos);
134    let mut b_enc_prep: FheUintBlocksPrep<Vec<u8>, BE, u32> = FheUintBlocksPrep::<Vec<u8>, BE, u32>::alloc(&module, &ggsw_infos);
135    let mut c_enc: FheUintBlocks<Vec<u8>, u32> = FheUintBlocks::<Vec<u8>, u32>::alloc(&module, &glwe_infos);
136    let mut c_enc_prep_debug: FheUintBlocksPrepDebug<Vec<u8>, u32> =
137        FheUintBlocksPrepDebug::<Vec<u8>, u32>::alloc(&module, &ggsw_infos);
138    let mut c_enc_prep: FheUintBlocksPrep<Vec<u8>, BE, u32> = FheUintBlocksPrep::<Vec<u8>, BE, u32>::alloc(&module, &ggsw_infos);
139
140    a_enc_prep.encrypt_sk(
141        &module,
142        a,
143        &sk_glwe_prep,
144        &mut source_xa,
145        &mut source_xe,
146        scratch.borrow(),
147    );
148    b_enc_prep.encrypt_sk(
149        &module,
150        b,
151        &sk_glwe_prep,
152        &mut source_xa,
153        &mut source_xe,
154        scratch.borrow(),
155    );
156
157    let start: Instant = Instant::now();
158    c_enc.sub(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
159
160    let duration: std::time::Duration = start.elapsed();
161    println!("add: {} ms", duration.as_millis());
162
163    println!(
164        "have: {}",
165        c_enc.decrypt(&module, &sk_glwe_prep, scratch.borrow())
166    );
167    println!("want: {}", a.wrapping_sub(b));
168    println!(
169        "noise: {:?}",
170        c_enc.noise(&module, &sk_glwe_prep, a.wrapping_sub(b), scratch.borrow())
171    );
172
173    let n_lwe: u32 = TEST_N_LWE;
174    let block_size: u32 = TEST_BLOCK_SIZE;
175
176    let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into());
177    sk_lwe.fill_binary_block(block_size as usize, &mut source_xs);
178
179    let bdd_key_infos: BDDKeyLayout = TEST_BDD_KEY_LAYOUT;
180
181    let now: Instant = Instant::now();
182    let bdd_key: BDDKey<Vec<u8>, Vec<u8>, BRA> = BDDKey::encrypt_sk(
183        &module,
184        &sk_lwe,
185        &sk_glwe,
186        &bdd_key_infos,
187        &mut source_xa,
188        &mut source_xe,
189        scratch.borrow(),
190    );
191    let bdd_key_prepared: BDDKeyPrepared<Vec<u8>, Vec<u8>, BRA, BE> = bdd_key.prepare_alloc(&module, scratch.borrow());
192    println!("BDD-KGEN: {} ms", now.elapsed().as_millis());
193
194    let now: Instant = Instant::now();
195    c_enc_prep_debug.prepare(&module, &c_enc, &bdd_key_prepared, scratch.borrow());
196    println!("CBT: {} ms", now.elapsed().as_millis());
197
198    c_enc_prep_debug.noise(&module, &sk_glwe_prep, a.wrapping_sub(b));
199
200    let now: Instant = Instant::now();
201    c_enc_prep.prepare(&module, &c_enc, &bdd_key_prepared, scratch.borrow());
202    println!("CBT: {} ms", now.elapsed().as_millis());
203
204    let start: Instant = Instant::now();
205    c_enc.add(&module, &c_enc_prep, &b_enc_prep, scratch.borrow());
206
207    let duration: std::time::Duration = start.elapsed();
208    println!("add: {} ms", duration.as_millis());
209
210    println!(
211        "have: {}",
212        c_enc.decrypt(&module, &sk_glwe_prep, scratch.borrow())
213    );
214    println!("want: {}", b.wrapping_add(a.wrapping_sub(b)));
215    println!(
216        "noise: {:?}",
217        c_enc.noise(
218            &module,
219            &sk_glwe_prep,
220            b.wrapping_add(a.wrapping_sub(b)),
221            scratch.borrow()
222        )
223    );
224}