use std::hint::black_box; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use poulpy_backend::{FFT64Avx, FFT64Ref}; use poulpy_core::{ GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared}, }; use poulpy_hal::{ api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow}, layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use rand::RngCore; use poulpy_schemes::tfhe::{ bdd_arithmetic::{ BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintPrepare, FheUintPrepareDebug, FheUintPrepared, FheUintPreparedEncryptSk, FheUintPreparedFactory, tests::test_suite::TestContext, }, blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory, CGGI}, }; pub fn benc_bdd_prepare( c: &mut Criterion, label: &str, test_context: &TestContext, ) where Module: ModuleNew + GLWESecretPreparedFactory + GLWEDecrypt + GLWENoise + FheUintPreparedFactory + FheUintPreparedEncryptSk + FheUintPrepareDebug + BDDKeyEncryptSk + BDDKeyPreparedFactory + GGSWNoise + FheUintPrepare + ExecuteBDDCircuit2WTo1W + GLWEEncryptSk, BlindRotationKey, BRA>: BlindRotationKeyFactory, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, { let group_name: String = format!("bdd_prepare::{label}"); let mut group = c.benchmark_group(group_name); fn runner(test_context: &TestContext) -> impl FnMut() where Module: ModuleNew + GLWESecretPreparedFactory + GLWEDecrypt + GLWENoise + FheUintPreparedFactory + FheUintPreparedEncryptSk + FheUintPrepareDebug + BDDKeyEncryptSk + BDDKeyPreparedFactory + GGSWNoise + FheUintPrepare + ExecuteBDDCircuit2WTo1W + GLWEEncryptSk, BlindRotationKey, BRA>: BlindRotationKeyFactory, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, { let glwe_infos: GLWELayout = test_context.glwe_infos(); let ggsw_infos: GGSWLayout = test_context.ggsw_infos(); let module: &Module = &test_context.module; let sk_glwe_prep: &GLWESecretPrepared, BE> = &test_context.sk_glwe; let bdd_key_prepared: &BDDKeyPrepared, BRA, BE> = &test_context.bdd_key; let mut source: Source = Source::new([6u8; 32]); let mut source_xa: Source = Source::new([2u8; 32]); let mut source_xe: Source = Source::new([3u8; 32]); let threads = 1; let mut scratch: ScratchOwned = ScratchOwned::alloc((1 << 22) * threads); // GLWE(value) let mut c_enc: FheUint, u32> = FheUint::alloc_from_infos(&glwe_infos); let value: u32 = source.next_u32(); c_enc.encrypt_sk( module, value, sk_glwe_prep, &mut source_xa, &mut source_xe, scratch.borrow(), ); // GGSW(0) let mut c_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc_from_infos(module, &ggsw_infos); // GGSW(value) move || { c_enc_prep.prepare_custom_multi_thread(threads, module, &c_enc, 0, 32, bdd_key_prepared, scratch.borrow()); black_box(()); } } let id: BenchmarkId = BenchmarkId::from_parameter(format!("n_glwe: {}", test_context.module.n())); let mut runner = runner::(test_context); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); group.finish(); } fn bench_bdd_prepare_cpu_ref_fft64(c: &mut Criterion) { benc_bdd_prepare::( c, "bdd_prepare_fft64_ref", &TestContext::::new(), ); } criterion_group!(benches, bench_bdd_prepare_cpu_ref_fft64,); criterion_main!(benches);