use poulpy_core::{ GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, SIGMA, ScratchTakeCore, layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared}, }; use poulpy_hal::{ api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow}, layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use rand::RngCore; use crate::tfhe::{ bdd_arithmetic::{ BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintPrepare, FheUintPrepareDebug, FheUintPreparedDebug, FheUintPreparedEncryptSk, FheUintPreparedFactory, tests::test_suite::{TEST_BASE2K, TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext}, }, blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, }; pub fn test_bdd_prepare(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 glwe_infos: GLWELayout = TEST_GLWE_INFOS; let ggsw_infos: GGSWLayout = TEST_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 mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); // 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_debug: FheUintPreparedDebug, u32> = FheUintPreparedDebug::, u32>::alloc_from_infos(module, &ggsw_infos); let mut scratch_2 = ScratchOwned::alloc(module.fhe_uint_prepare_tmp_bytes(7, 1, &c_enc_prep_debug, &c_enc, bdd_key_prepared)); // GGSW(value) c_enc_prep_debug.prepare(module, &c_enc, bdd_key_prepared, scratch_2.borrow()); let max_noise = |col_i: usize| { let mut noise: f64 = -(ggsw_infos.size() as f64 * TEST_BASE2K as f64) + SIGMA.log2() + 2.0; noise += 0.5 * ggsw_infos.log_n() as f64; if col_i != 0 { noise += 0.5 * ggsw_infos.log_n() as f64 } noise }; // c_enc_prep_debug.print_noise(module, sk_glwe_prep, value); c_enc_prep_debug.assert_noise(module, sk_glwe_prep, value, &max_noise); }