arnaucube
arnaucube/poulpyPublic Self-hosted repository
Add tests to BDD
Pro7ech jeanphilippe.bossuat@gmail.com
Thu, 23 Oct 2025 10:11:12 +0200
commit

06795e95479d24f923217ad406b67463feb88170

parent

9067de8d96d2036f3a3d085c06e3609485772356

25 files changed, 1080 insertions(+), 116 deletions(-)

jump to
M poulpy-core/src/noise/ggsw.rs → poulpy-core/src/noise/ggsw.rs

@@ -11,7 +11,7 @@ use crate::layouts::prepared::GLWESecretPreparedToRef;

use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; impl<D: DataRef> GGSW<D> { - pub fn assert_noise<M, BE: Backend, P, S, F>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: F) + pub fn assert_noise<M, BE: Backend, P, S, F>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: &F) where S: GLWESecretPreparedToRef<BE>, P: ScalarZnxToRef,

@@ -32,7 +32,7 @@ }

} pub trait GGSWNoise<BE: Backend> { - fn ggsw_assert_noise<R, S, P, F>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: F) + fn ggsw_assert_noise<R, S, P, F>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: &F) where R: GGSWToRef, S: GLWESecretPreparedToRef<BE>,

@@ -57,7 +57,7 @@ + VecZnxSubInplace,

Scratch<BE>: ScratchTakeBasic, ScratchOwned<BE>: ScratchOwnedBorrow<BE> + ScratchOwnedAlloc<BE>, { - fn ggsw_assert_noise<R, S, P, F>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: F) + fn ggsw_assert_noise<R, S, P, F>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: &F) where R: GGSWToRef, S: GLWESecretPreparedToRef<BE>,
M poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs → poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs

@@ -168,7 +168,7 @@ k_tsk,

) + 0.5 }; - ct_out.assert_noise(module, &sk_prepared, &pt_scalar, max_noise); + ct_out.assert_noise(module, &sk_prepared, &pt_scalar, &max_noise); } } }

@@ -308,7 +308,7 @@ k_tsk,

) + 0.5 }; - ct.assert_noise(module, &sk_prepared, &pt_scalar, max_noise); + ct.assert_noise(module, &sk_prepared, &pt_scalar, &max_noise); } } }
M poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs → poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs

@@ -65,7 +65,7 @@ );

let noise_f = |_col_i: usize| -(k as f64) + SIGMA.log2() + 0.5; - ct.assert_noise(module, &sk_prepared, &pt_scalar, noise_f); + ct.assert_noise(module, &sk_prepared, &pt_scalar, &noise_f); } } }

@@ -126,7 +126,7 @@

let mut ct: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos); ct.decompress(module, &ct_compressed); - ct.assert_noise(module, &sk_prepared, &pt_scalar, noise_f); + ct.assert_noise(module, &sk_prepared, &pt_scalar, &noise_f); } } }
M poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs → poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs

@@ -143,7 +143,7 @@ k_apply,

) + 0.5 }; - ggsw_out.assert_noise(module, &sk_prepared, &pt_in, max_noise); + ggsw_out.assert_noise(module, &sk_prepared, &pt_in, &max_noise); } } }

@@ -266,7 +266,7 @@ k_apply,

) + 0.5 }; - ggsw_out.assert_noise(module, &sk_prepared, &pt_in, max_noise); + ggsw_out.assert_noise(module, &sk_prepared, &pt_in, &max_noise); } } }
M poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs → poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs

@@ -175,7 +175,7 @@ k_tsk,

) + 0.5 }; - ggsw_out.assert_noise(module, &sk_out_prepared, &pt_scalar, max_noise); + ggsw_out.assert_noise(module, &sk_out_prepared, &pt_scalar, &max_noise); } } }

@@ -323,7 +323,7 @@ k_tsk,

) + 0.5 }; - ggsw_out.assert_noise(module, &sk_out_prepared, &pt_scalar, max_noise); + ggsw_out.assert_noise(module, &sk_out_prepared, &pt_scalar, &max_noise); } } }
M poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs

@@ -5,12 +5,9 @@ GLWEDecrypt, GLWENoise,

layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, LWEInfos, Rank, TorusPrecision}, }; -#[cfg(test)] use poulpy_core::GLWEEncryptSk; use poulpy_core::ScratchTakeCore; use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, ZnxZero}; -#[cfg(test)] -#[cfg(test)] use poulpy_hal::source::Source; use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger};

@@ -43,16 +40,14 @@ }

} impl<T: UnsignedInteger> FheUintBlocks<Vec<u8>, T> { - #[allow(dead_code)] - pub(crate) fn alloc<A, BE: Backend>(module: &Module<BE>, infos: &A) -> Self + pub fn alloc_from_infos<A, BE: Backend>(module: &Module<BE>, infos: &A) -> Self where A: GLWEInfos, { - Self::alloc_with(module, infos.base2k(), infos.k(), infos.rank()) + Self::alloc(module, infos.base2k(), infos.k(), infos.rank()) } - #[allow(dead_code)] - pub(crate) fn alloc_with<BE: Backend>(module: &Module<BE>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { + pub fn alloc<BE: Backend>(module: &Module<BE>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { Self { blocks: (0..T::WORD_SIZE) .map(|_| GLWE::alloc(module.n().into(), base2k, k, rank))

@@ -64,9 +59,7 @@ }

} impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintBlocks<D, T> { - #[allow(dead_code)] - #[cfg(test)] - pub(crate) fn encrypt_sk<S, BE: Backend>( + pub fn encrypt_sk<S, BE: Backend>( &mut self, module: &Module<BE>, value: T,
M poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_debug.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_debug.rs

@@ -7,32 +7,30 @@ blind_rotation::BlindRotationAlgo,

circuit_bootstrapping::CirtuitBootstrappingExecute, }; use poulpy_core::GGSWNoise; -#[cfg(test)] + use poulpy_core::layouts::{Base2K, Dnum, Dsize, Rank, TorusPrecision}; use poulpy_core::layouts::{GGSW, GLWESecretPreparedToRef}; use poulpy_core::{ LWEFromGLWE, ScratchTakeCore, layouts::{GGSWInfos, GGSWPreparedFactory, GLWEInfos, LWE, LWEInfos}, }; -#[cfg(test)] + use poulpy_hal::api::ModuleN; use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; -pub(crate) struct FheUintBlocksPreparedDebug<D: Data, T: UnsignedInteger> { +pub struct FheUintBlocksPreparedDebug<D: Data, T: UnsignedInteger> { pub(crate) blocks: Vec<GGSW<D>>, pub(crate) _base: u8, pub(crate) _phantom: PhantomData<T>, } -#[cfg(test)] impl<T: UnsignedInteger> FheUintBlocksPreparedDebug<Vec<u8>, T> { - #[allow(dead_code)] - pub(crate) fn alloc<A, M>(module: &M, infos: &A) -> Self + pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self where M: ModuleN, A: GGSWInfos, { - Self::alloc_with( + Self::alloc( module, infos.base2k(), infos.k(),

@@ -42,8 +40,7 @@ infos.rank(),

) } - #[allow(dead_code)] - pub(crate) fn alloc_with<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self + pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self where M: ModuleN, {

@@ -88,8 +85,7 @@ }

} impl<D: DataRef, T: UnsignedInteger + ToBits> FheUintBlocksPreparedDebug<D, T> { - #[allow(dead_code)] - pub(crate) fn noise<S, M, BE: Backend>(&self, module: &M, sk: &S, want: T) + pub fn print_noise<S, M, BE: Backend>(&self, module: &M, sk: &S, want: T) where S: GLWESecretPreparedToRef<BE>, M: GGSWNoise<BE>,

@@ -99,6 +95,20 @@ use poulpy_hal::layouts::{ScalarZnx, ZnxViewMut};

let mut pt_want = ScalarZnx::alloc(self.n().into(), 1); pt_want.at_mut(0, 0)[0] = want.bit(i) as i64; ggsw.print_noise(module, sk, &pt_want); + } + } + + pub fn assert_noise<S, M, F, BE: Backend>(&self, module: &M, sk: &S, want: T, max_noise: &F) + where + S: GLWESecretPreparedToRef<BE>, + M: GGSWNoise<BE>, + F: Fn(usize) -> f64, + { + for (i, ggsw) in self.blocks.iter().enumerate() { + use poulpy_hal::layouts::{ScalarZnx, ZnxViewMut}; + let mut pt_want = ScalarZnx::alloc(self.n().into(), 1); + pt_want.at_mut(0, 0)[0] = want.bit(i) as i64; + ggsw.assert_noise(module, sk, &pt_want, max_noise); } } }

@@ -130,7 +140,6 @@ }

} impl<D: DataMut, T: UnsignedInteger> FheUintBlocksPreparedDebug<D, T> { - #[allow(dead_code)] pub fn prepare<BRA, M, O, K, BE: Backend>( &mut self, module: &M,
M poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs

@@ -3,17 +3,16 @@

use poulpy_core::layouts::{ Base2K, Dnum, Dsize, GGSWInfos, GGSWPreparedFactory, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared, }; -#[cfg(test)] + use poulpy_core::{GGSWEncryptSk, ScratchTakeCore, layouts::GLWESecretPreparedToRef}; use poulpy_hal::layouts::{Backend, Data, DataRef, Module}; -#[cfg(test)] + use poulpy_hal::{ api::ModuleN, layouts::{DataMut, Scratch}, source::Source, }; -#[cfg(test)] use crate::tfhe::bdd_arithmetic::ToBits; use crate::tfhe::bdd_arithmetic::UnsignedInteger;

@@ -83,13 +82,11 @@ module.alloc_fhe_uint_blocks_prepared(base2k, k, dnum, dsize, rank)

} } -#[cfg(test)] impl<T: UnsignedInteger + ToBits, BE: Backend> FheUintBlocksPreparedEncryptSk<T, BE> for Module<BE> where Self: Sized + ModuleN + GGSWEncryptSk<BE> + GGSWPreparedFactory<BE> { } -#[cfg(test)] pub trait FheUintBlocksPreparedEncryptSk<T: UnsignedInteger + ToBits, BE: Backend> where Self: Sized + ModuleN + GGSWEncryptSk<BE> + GGSWPreparedFactory<BE>,

@@ -126,7 +123,6 @@ }

} } -#[cfg(test)] impl<D: DataMut, T: UnsignedInteger + ToBits, BE: Backend> FheUintBlocksPrepared<D, T, BE> { pub(crate) fn encrypt_sk<M, S>( &mut self,
M poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/mod.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/mod.rs

@@ -2,10 +2,9 @@ mod block;

mod block_prepared; mod word; -#[cfg(test)] mod block_debug; -#[cfg(test)] -pub(crate) use block_debug::*; + +pub use block_debug::*; pub use block::*; pub use block_prepared::*;
M poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs

@@ -1,4 +1,3 @@

-#[cfg(test)] use crate::tfhe::bdd_arithmetic::FheUintBlocksPreparedDebug; use crate::tfhe::{ bdd_arithmetic::{FheUintBlocks, FheUintBlocksPrepared, UnsignedInteger},

@@ -281,8 +280,7 @@ module.fhe_uint_blocks_prepare(self, other, key, scratch);

} } -#[cfg(test)] -pub(crate) trait FheUintBlockDebugPrepare<BRA: BlindRotationAlgo, T: UnsignedInteger, BE: Backend> { +pub trait FheUintBlockDebugPrepare<BRA: BlindRotationAlgo, T: UnsignedInteger, BE: Backend> { fn fhe_uint_block_debug_prepare<DM, DR0, DR1>( &self, res: &mut FheUintBlocksPreparedDebug<DM, T>,
M poulpy-schemes/src/tfhe/bdd_arithmetic/mod.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/mod.rs

@@ -3,7 +3,6 @@ mod ciphertexts;

mod circuits; mod eval; mod key; -mod parameters; pub use bdd_2w_to_1w::*; pub use ciphertexts::*;

@@ -11,11 +10,7 @@ pub(crate) use circuits::*;

pub(crate) use eval::*; pub use key::*; -#[cfg(test)] -pub(crate) use parameters::*; - -#[cfg(test)] -mod test; +pub mod tests; pub trait UnsignedInteger: Copy + 'static { const WORD_SIZE: usize;
M poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/mod.rs

@@ -1,30 +1,44 @@

-#[cfg(test)] +mod add; +mod and; +mod or; +mod prepare; +mod sll; +mod slt; +mod sltu; +mod sra; +mod srl; +mod sub; +mod xor; + +pub use add::*; +pub use and::*; +pub use or::*; +pub use prepare::*; +pub use sll::*; +pub use slt::*; +pub use sltu::*; +pub use sra::*; +pub use srl::*; +pub use sub::*; +pub use xor::*; + use poulpy_core::layouts::{ Base2K, Degree, Dnum, Dsize, GGSWLayout, GLWEAutomorphismKeyLayout, GLWELayout, GLWETensorKeyLayout, GLWEToLWEKeyLayout, Rank, TorusPrecision, }; -#[cfg(test)] use crate::tfhe::{ bdd_arithmetic::BDDKeyLayout, blind_rotation::BlindRotationKeyLayout, circuit_bootstrapping::CircuitBootstrappingKeyLayout, }; -#[cfg(test)] pub(crate) const TEST_N_GLWE: u32 = 512; -#[cfg(test)] pub(crate) const TEST_N_LWE: u32 = 77; -#[cfg(test)] pub(crate) const TEST_BASE2K: u32 = 13; -#[cfg(test)] pub(crate) const TEST_K_GLWE: u32 = 26; -#[cfg(test)] pub(crate) const TEST_K_GGSW: u32 = 39; -#[cfg(test)] pub(crate) const TEST_BLOCK_SIZE: u32 = 7; -#[cfg(test)] pub(crate) const TEST_RANK: u32 = 2; -#[cfg(test)] pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout { n: Degree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K),

@@ -32,7 +46,6 @@ k: TorusPrecision(TEST_K_GLWE),

rank: Rank(TEST_RANK), }; -#[cfg(test)] pub(crate) static TEST_GGSW_INFOS: GGSWLayout = GGSWLayout { n: Degree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K),

@@ -42,7 +55,6 @@ dnum: Dnum(2),

dsize: Dsize(1), }; -#[cfg(test)] pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { cbt: CircuitBootstrappingKeyLayout { layout_brk: BlindRotationKeyLayout {
M poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/prepare.rs

@@ -1,8 +1,5 @@

-use std::time::Instant; - -use poulpy_backend::FFT64Ref; use poulpy_core::{ - GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, + GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, SIGMA, ScratchTakeCore, layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, LWESecret, prepared::GLWESecretPrepared}, }; use poulpy_hal::{

@@ -14,20 +11,15 @@ use rand::RngCore;

use crate::tfhe::{ bdd_arithmetic::{ - Add, BDDKey, BDDKeyEncryptSk, BDDKeyLayout, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, - FheUintBlockDebugPrepare, FheUintBlocks, FheUintBlocksPrepare, FheUintBlocksPrepared, FheUintBlocksPreparedDebug, - FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, Sub, TEST_BDD_KEY_LAYOUT, TEST_BLOCK_SIZE, TEST_GGSW_INFOS, - TEST_GLWE_INFOS, TEST_N_LWE, + BDDKey, BDDKeyEncryptSk, BDDKeyLayout, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, + FheUintBlockDebugPrepare, FheUintBlocks, FheUintBlocksPrepare, FheUintBlocksPreparedDebug, + FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, + tests::test_suite::{TEST_BASE2K, TEST_BDD_KEY_LAYOUT, TEST_BLOCK_SIZE, TEST_GGSW_INFOS, TEST_GLWE_INFOS, TEST_N_LWE}, }, - blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory, CGGI}, + blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, }; -#[test] -fn test_bdd_2w_to_1w_fft64_ref() { - test_bdd_2w_to_1w::<FFT64Ref, CGGI>() -} - -fn test_bdd_2w_to_1w<BE: Backend, BRA: BlindRotationAlgo>() +pub fn test_bdd_prepare<BRA: BlindRotationAlgo, BE: Backend>() where Module<BE>: ModuleNew<BE> + GLWESecretPreparedFactory<BE>

@@ -59,28 +51,24 @@ let mut source_xe: Source = Source::new([3u8; 32]);

let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22); + // GLWE Secret let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); sk_glwe_prep.prepare(&module, &sk_glwe); - let a: u32 = 645139204; - let b: u32 = 0; - - println!("a: {a}"); - println!("b: {b}"); - + // LWE Secret let n_lwe: u32 = TEST_N_LWE; let block_size: u32 = TEST_BLOCK_SIZE; - let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into()); sk_lwe.fill_binary_block(block_size as usize, &mut source_xs); + // CBT KEY let bdd_key_infos: BDDKeyLayout = TEST_BDD_KEY_LAYOUT; let mut bdd_key: BDDKey<Vec<u8>, BRA> = BDDKey::alloc_from_infos(&bdd_key_infos); - let now: Instant = Instant::now(); source.fill_bytes(&mut scratch.borrow().data); scratch.borrow().data.fill(0); bdd_key.encrypt_sk(

@@ -94,44 +82,36 @@ );

let mut bdd_key_prepared: BDDKeyPrepared<Vec<u8>, BRA, BE> = BDDKeyPrepared::alloc_from_infos(&module, &bdd_key_infos); source.fill_bytes(&mut scratch.borrow().data); bdd_key_prepared.prepare(&module, &bdd_key, scratch.borrow()); - println!("BDD-KGEN: {} ms", now.elapsed().as_millis()); - let mut sum_enc: FheUintBlocks<Vec<u8>, u32> = FheUintBlocks::<Vec<u8>, u32>::alloc(&module, &glwe_infos); - let mut a_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = - FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); - let mut b_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = - FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); - - source.fill_bytes(&mut scratch.borrow().data); - a_enc_prep.encrypt_sk( + // GLWE(value) + let mut c_enc: FheUintBlocks<Vec<u8>, u32> = FheUintBlocks::alloc_from_infos(&module, &glwe_infos); + let value: u32 = source.next_u32(); + c_enc.encrypt_sk( &module, - a, - &sk_glwe_prep, - &mut source_xa, - &mut source_xe, - scratch.borrow(), - ); - source.fill_bytes(&mut scratch.borrow().data); - b_enc_prep.encrypt_sk( - &module, - b, + value, &sk_glwe_prep, &mut source_xa, &mut source_xe, scratch.borrow(), ); - // d + a - sum_enc.add(&module, &a_enc_prep, &b_enc_prep, scratch.borrow()); + // GGSW(0) + let mut c_enc_prep_debug: FheUintBlocksPreparedDebug<Vec<u8>, u32> = + FheUintBlocksPreparedDebug::<Vec<u8>, u32>::alloc_from_infos(&module, &ggsw_infos); - println!( - "other have: {:032b}", - sum_enc.decrypt(&module, &sk_glwe_prep, scratch.borrow()) - ); + // GGSW(value) + c_enc_prep_debug.prepare(&module, &c_enc, &bdd_key_prepared, scratch.borrow()); + + let max_noise = |col_i: usize| { + let mut noise: f64 = -(ggsw_infos.size() as f64 * TEST_BASE2K as f64) + SIGMA.log2() + 1.0; + noise += 0.5 * ggsw_infos.log_n() as f64; + if col_i != 0 { + noise += 0.5 * ggsw_infos.log_n() as f64 + } + noise + }; - println!("other want: {:032b}", b.wrapping_add(a)); + // c_enc_prep_debug.print_noise(&module, &sk_glwe_prep, value); - // print a, b, and d - println!("a: {a}"); - println!("b: {b}"); + c_enc_prep_debug.assert_noise(&module, &sk_glwe_prep, value, &max_noise); }
A poulpy-schemes/src/tfhe/bdd_arithmetic/tests/fft64_ref.rs

@@ -0,0 +1,64 @@

+use poulpy_backend::FFT64Ref; + +use crate::tfhe::{ + bdd_arithmetic::tests::test_suite::{ + test_bdd_add, test_bdd_and, test_bdd_or, test_bdd_prepare, test_bdd_sll, test_bdd_slt, test_bdd_sltu, test_bdd_sra, + test_bdd_srl, test_bdd_sub, test_bdd_xor, + }, + blind_rotation::CGGI, +}; + +#[test] +fn test_bdd_prepare_fft64_ref() { + test_bdd_prepare::<CGGI, FFT64Ref>() +} + +#[test] +fn test_bdd_add_fft64_ref() { + test_bdd_add::<CGGI, FFT64Ref>() +} + +#[test] +fn test_bdd_and_fft64_ref() { + test_bdd_and::<CGGI, FFT64Ref>() +} + +#[test] +fn test_bdd_or_fft64_ref() { + test_bdd_or::<CGGI, FFT64Ref>() +} + +#[test] +fn test_bdd_sll_fft64_ref() { + test_bdd_sll::<CGGI, FFT64Ref>() +} + +#[test] +fn test_bdd_slt_fft64_ref() { + test_bdd_slt::<CGGI, FFT64Ref>() +} + +#[test] +fn test_bdd_sltu_fft64_ref() { + test_bdd_sltu::<CGGI, FFT64Ref>() +} + +#[test] +fn test_bdd_sra_fft64_ref() { + test_bdd_sra::<CGGI, FFT64Ref>() +} + +#[test] +fn test_bdd_srl_fft64_ref() { + test_bdd_srl::<CGGI, FFT64Ref>() +} + +#[test] +fn test_bdd_sub_fft64_ref() { + test_bdd_sub::<CGGI, FFT64Ref>() +} + +#[test] +fn test_bdd_xor_fft64_ref() { + test_bdd_xor::<CGGI, FFT64Ref>() +}
A poulpy-schemes/src/tfhe/bdd_arithmetic/tests/mod.rs

@@ -0,0 +1,4 @@

+pub mod test_suite; + +#[cfg(test)] +mod fft64_ref;
A poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/add.rs

@@ -0,0 +1,93 @@

+use poulpy_core::{ + GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, + layouts::{GGSWLayout, GLWELayout, GLWESecret, 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::{ + Add, BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUintBlockDebugPrepare, FheUintBlocks, + FheUintBlocksPrepare, FheUintBlocksPrepared, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, + tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS}, + }, + blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, +}; + +pub fn test_bdd_add<BRA: BlindRotationAlgo, BE: Backend>() +where + Module<BE>: ModuleNew<BE> + + GLWESecretPreparedFactory<BE> + + GLWEDecrypt<BE> + + GLWENoise<BE> + + FheUintBlocksPreparedFactory<u32, BE> + + FheUintBlocksPreparedEncryptSk<u32, BE> + + FheUintBlockDebugPrepare<BRA, u32, BE> + + BDDKeyEncryptSk<BRA, BE> + + BDDKeyPreparedFactory<BRA, BE> + + GGSWNoise<BE> + + FheUintBlocksPrepare<BRA, u32, BE> + + ExecuteBDDCircuit2WTo1W<u32, BE> + + GLWEEncryptSk<BE>, + BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, +{ + let glwe_infos: GLWELayout = TEST_GLWE_INFOS; + let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS; + + let n_glwe: usize = glwe_infos.n().into(); + + let module: Module<BE> = Module::<BE>::new(n_glwe as u64); + let mut source: Source = Source::new([6u8; 32]); + let mut source_xs: Source = Source::new([1u8; 32]); + let mut source_xa: Source = Source::new([2u8; 32]); + let mut source_xe: Source = Source::new([3u8; 32]); + + let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks<Vec<u8>, u32> = FheUintBlocks::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + + let a: u32 = source.next_u32(); + let b: u32 = source.next_u32(); + + source.fill_bytes(&mut scratch.borrow().data); + a_enc_prep.encrypt_sk( + &module, + a, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + source.fill_bytes(&mut scratch.borrow().data); + b_enc_prep.encrypt_sk( + &module, + b, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + + // d + a + res.add(&module, &a_enc_prep, &b_enc_prep, scratch.borrow()); + + assert_eq!( + res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), + a.wrapping_add(b) + ); +}
A poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/and.rs

@@ -0,0 +1,89 @@

+use poulpy_core::{ + GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, + layouts::{GGSWLayout, GLWELayout, GLWESecret, 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::{ + And, BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUintBlockDebugPrepare, FheUintBlocks, + FheUintBlocksPrepare, FheUintBlocksPrepared, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, + tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS}, + }, + blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, +}; + +pub fn test_bdd_and<BRA: BlindRotationAlgo, BE: Backend>() +where + Module<BE>: ModuleNew<BE> + + GLWESecretPreparedFactory<BE> + + GLWEDecrypt<BE> + + GLWENoise<BE> + + FheUintBlocksPreparedFactory<u32, BE> + + FheUintBlocksPreparedEncryptSk<u32, BE> + + FheUintBlockDebugPrepare<BRA, u32, BE> + + BDDKeyEncryptSk<BRA, BE> + + BDDKeyPreparedFactory<BRA, BE> + + GGSWNoise<BE> + + FheUintBlocksPrepare<BRA, u32, BE> + + ExecuteBDDCircuit2WTo1W<u32, BE> + + GLWEEncryptSk<BE>, + BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, +{ + let glwe_infos: GLWELayout = TEST_GLWE_INFOS; + let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS; + + let n_glwe: usize = glwe_infos.n().into(); + + let module: Module<BE> = Module::<BE>::new(n_glwe as u64); + let mut source: Source = Source::new([6u8; 32]); + let mut source_xs: Source = Source::new([1u8; 32]); + let mut source_xa: Source = Source::new([2u8; 32]); + let mut source_xe: Source = Source::new([3u8; 32]); + + let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks<Vec<u8>, u32> = FheUintBlocks::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + + let a: u32 = source.next_u32(); + let b: u32 = source.next_u32(); + + source.fill_bytes(&mut scratch.borrow().data); + a_enc_prep.encrypt_sk( + &module, + a, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + source.fill_bytes(&mut scratch.borrow().data); + b_enc_prep.encrypt_sk( + &module, + b, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + + res.and(&module, &a_enc_prep, &b_enc_prep, scratch.borrow()); + + assert_eq!(res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), a & b); +}
A poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/or.rs

@@ -0,0 +1,89 @@

+use poulpy_core::{ + GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, + layouts::{GGSWLayout, GLWELayout, GLWESecret, 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, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUintBlockDebugPrepare, FheUintBlocks, + FheUintBlocksPrepare, FheUintBlocksPrepared, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, Or, + tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS}, + }, + blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, +}; + +pub fn test_bdd_or<BRA: BlindRotationAlgo, BE: Backend>() +where + Module<BE>: ModuleNew<BE> + + GLWESecretPreparedFactory<BE> + + GLWEDecrypt<BE> + + GLWENoise<BE> + + FheUintBlocksPreparedFactory<u32, BE> + + FheUintBlocksPreparedEncryptSk<u32, BE> + + FheUintBlockDebugPrepare<BRA, u32, BE> + + BDDKeyEncryptSk<BRA, BE> + + BDDKeyPreparedFactory<BRA, BE> + + GGSWNoise<BE> + + FheUintBlocksPrepare<BRA, u32, BE> + + ExecuteBDDCircuit2WTo1W<u32, BE> + + GLWEEncryptSk<BE>, + BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, +{ + let glwe_infos: GLWELayout = TEST_GLWE_INFOS; + let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS; + + let n_glwe: usize = glwe_infos.n().into(); + + let module: Module<BE> = Module::<BE>::new(n_glwe as u64); + let mut source: Source = Source::new([6u8; 32]); + let mut source_xs: Source = Source::new([1u8; 32]); + let mut source_xa: Source = Source::new([2u8; 32]); + let mut source_xe: Source = Source::new([3u8; 32]); + + let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks<Vec<u8>, u32> = FheUintBlocks::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + + let a: u32 = source.next_u32(); + let b: u32 = source.next_u32(); + + source.fill_bytes(&mut scratch.borrow().data); + a_enc_prep.encrypt_sk( + &module, + a, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + source.fill_bytes(&mut scratch.borrow().data); + b_enc_prep.encrypt_sk( + &module, + b, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + + res.or(&module, &a_enc_prep, &b_enc_prep, scratch.borrow()); + + assert_eq!(res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), a | b); +}
A poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sll.rs

@@ -0,0 +1,92 @@

+use poulpy_core::{ + GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, + layouts::{GGSWLayout, GLWELayout, GLWESecret, 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, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUintBlockDebugPrepare, FheUintBlocks, + FheUintBlocksPrepare, FheUintBlocksPrepared, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, Sll, + tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS}, + }, + blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, +}; + +pub fn test_bdd_sll<BRA: BlindRotationAlgo, BE: Backend>() +where + Module<BE>: ModuleNew<BE> + + GLWESecretPreparedFactory<BE> + + GLWEDecrypt<BE> + + GLWENoise<BE> + + FheUintBlocksPreparedFactory<u32, BE> + + FheUintBlocksPreparedEncryptSk<u32, BE> + + FheUintBlockDebugPrepare<BRA, u32, BE> + + BDDKeyEncryptSk<BRA, BE> + + BDDKeyPreparedFactory<BRA, BE> + + GGSWNoise<BE> + + FheUintBlocksPrepare<BRA, u32, BE> + + ExecuteBDDCircuit2WTo1W<u32, BE> + + GLWEEncryptSk<BE>, + BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, +{ + let glwe_infos: GLWELayout = TEST_GLWE_INFOS; + let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS; + + let n_glwe: usize = glwe_infos.n().into(); + + let module: Module<BE> = Module::<BE>::new(n_glwe as u64); + let mut source: Source = Source::new([6u8; 32]); + let mut source_xs: Source = Source::new([1u8; 32]); + let mut source_xa: Source = Source::new([2u8; 32]); + let mut source_xe: Source = Source::new([3u8; 32]); + + let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks<Vec<u8>, u32> = FheUintBlocks::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + + let a: u32 = source.next_u32(); + let b: u32 = source.next_u32() & 15; + + source.fill_bytes(&mut scratch.borrow().data); + a_enc_prep.encrypt_sk( + &module, + a, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + source.fill_bytes(&mut scratch.borrow().data); + b_enc_prep.encrypt_sk( + &module, + b, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + + res.sll(&module, &a_enc_prep, &b_enc_prep, scratch.borrow()); + + assert_eq!( + res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), + a.wrapping_shl(b) + ); +}
A poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/slt.rs

@@ -0,0 +1,93 @@

+use poulpy_core::{ + GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, + layouts::{GGSWLayout, GLWELayout, GLWESecret, 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, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUintBlockDebugPrepare, FheUintBlocks, + FheUintBlocksPrepare, FheUintBlocksPrepared, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, Slt, + tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS}, + }, + blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, +}; + +pub fn test_bdd_slt<BRA: BlindRotationAlgo, BE: Backend>() +where + Module<BE>: ModuleNew<BE> + + GLWESecretPreparedFactory<BE> + + GLWEDecrypt<BE> + + GLWENoise<BE> + + FheUintBlocksPreparedFactory<u32, BE> + + FheUintBlocksPreparedEncryptSk<u32, BE> + + FheUintBlockDebugPrepare<BRA, u32, BE> + + BDDKeyEncryptSk<BRA, BE> + + BDDKeyPreparedFactory<BRA, BE> + + GGSWNoise<BE> + + FheUintBlocksPrepare<BRA, u32, BE> + + ExecuteBDDCircuit2WTo1W<u32, BE> + + GLWEEncryptSk<BE>, + BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, +{ + let glwe_infos: GLWELayout = TEST_GLWE_INFOS; + let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS; + + let n_glwe: usize = glwe_infos.n().into(); + + let module: Module<BE> = Module::<BE>::new(n_glwe as u64); + let mut source: Source = Source::new([6u8; 32]); + let mut source_xs: Source = Source::new([1u8; 32]); + let mut source_xa: Source = Source::new([2u8; 32]); + let mut source_xe: Source = Source::new([3u8; 32]); + + let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks<Vec<u8>, u32> = FheUintBlocks::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + + let a: u32 = source.next_u32(); + let b: u32 = source.next_u32(); + + source.fill_bytes(&mut scratch.borrow().data); + a_enc_prep.encrypt_sk( + &module, + a, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + source.fill_bytes(&mut scratch.borrow().data); + b_enc_prep.encrypt_sk( + &module, + b, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + + // d + a + res.slt(&module, &a_enc_prep, &b_enc_prep, scratch.borrow()); + + assert_eq!( + res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), + ((a as i32) < (b as i32)) as u32 + ); +}
A poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sltu.rs

@@ -0,0 +1,93 @@

+use poulpy_core::{ + GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, + layouts::{GGSWLayout, GLWELayout, GLWESecret, 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, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUintBlockDebugPrepare, FheUintBlocks, + FheUintBlocksPrepare, FheUintBlocksPrepared, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, Sltu, + tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS}, + }, + blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, +}; + +pub fn test_bdd_sltu<BRA: BlindRotationAlgo, BE: Backend>() +where + Module<BE>: ModuleNew<BE> + + GLWESecretPreparedFactory<BE> + + GLWEDecrypt<BE> + + GLWENoise<BE> + + FheUintBlocksPreparedFactory<u32, BE> + + FheUintBlocksPreparedEncryptSk<u32, BE> + + FheUintBlockDebugPrepare<BRA, u32, BE> + + BDDKeyEncryptSk<BRA, BE> + + BDDKeyPreparedFactory<BRA, BE> + + GGSWNoise<BE> + + FheUintBlocksPrepare<BRA, u32, BE> + + ExecuteBDDCircuit2WTo1W<u32, BE> + + GLWEEncryptSk<BE>, + BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, +{ + let glwe_infos: GLWELayout = TEST_GLWE_INFOS; + let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS; + + let n_glwe: usize = glwe_infos.n().into(); + + let module: Module<BE> = Module::<BE>::new(n_glwe as u64); + let mut source: Source = Source::new([6u8; 32]); + let mut source_xs: Source = Source::new([1u8; 32]); + let mut source_xa: Source = Source::new([2u8; 32]); + let mut source_xe: Source = Source::new([3u8; 32]); + + let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks<Vec<u8>, u32> = FheUintBlocks::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + + let a: u32 = source.next_u32(); + let b: u32 = source.next_u32(); + + source.fill_bytes(&mut scratch.borrow().data); + a_enc_prep.encrypt_sk( + &module, + a, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + source.fill_bytes(&mut scratch.borrow().data); + b_enc_prep.encrypt_sk( + &module, + b, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + + // d + a + res.sltu(&module, &a_enc_prep, &b_enc_prep, scratch.borrow()); + + assert_eq!( + res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), + (a < b) as u32 + ); +}
A poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sra.rs

@@ -0,0 +1,92 @@

+use poulpy_core::{ + GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, + layouts::{GGSWLayout, GLWELayout, GLWESecret, 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, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUintBlockDebugPrepare, FheUintBlocks, + FheUintBlocksPrepare, FheUintBlocksPrepared, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, Sra, + tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS}, + }, + blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, +}; + +pub fn test_bdd_sra<BRA: BlindRotationAlgo, BE: Backend>() +where + Module<BE>: ModuleNew<BE> + + GLWESecretPreparedFactory<BE> + + GLWEDecrypt<BE> + + GLWENoise<BE> + + FheUintBlocksPreparedFactory<u32, BE> + + FheUintBlocksPreparedEncryptSk<u32, BE> + + FheUintBlockDebugPrepare<BRA, u32, BE> + + BDDKeyEncryptSk<BRA, BE> + + BDDKeyPreparedFactory<BRA, BE> + + GGSWNoise<BE> + + FheUintBlocksPrepare<BRA, u32, BE> + + ExecuteBDDCircuit2WTo1W<u32, BE> + + GLWEEncryptSk<BE>, + BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, +{ + let glwe_infos: GLWELayout = TEST_GLWE_INFOS; + let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS; + + let n_glwe: usize = glwe_infos.n().into(); + + let module: Module<BE> = Module::<BE>::new(n_glwe as u64); + let mut source: Source = Source::new([6u8; 32]); + let mut source_xs: Source = Source::new([1u8; 32]); + let mut source_xa: Source = Source::new([2u8; 32]); + let mut source_xe: Source = Source::new([3u8; 32]); + + let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks<Vec<u8>, u32> = FheUintBlocks::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + + let a: u32 = source.next_u32(); + let b: u32 = source.next_u32() & 15; + + source.fill_bytes(&mut scratch.borrow().data); + a_enc_prep.encrypt_sk( + &module, + a, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + source.fill_bytes(&mut scratch.borrow().data); + b_enc_prep.encrypt_sk( + &module, + b, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + + res.sra(&module, &a_enc_prep, &b_enc_prep, scratch.borrow()); + + assert_eq!( + res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), + ((a as i32) >> b) as u32 + ); +}
A poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/srl.rs

@@ -0,0 +1,92 @@

+use poulpy_core::{ + GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, + layouts::{GGSWLayout, GLWELayout, GLWESecret, 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, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUintBlockDebugPrepare, FheUintBlocks, + FheUintBlocksPrepare, FheUintBlocksPrepared, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, Srl, + tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS}, + }, + blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, +}; + +pub fn test_bdd_srl<BRA: BlindRotationAlgo, BE: Backend>() +where + Module<BE>: ModuleNew<BE> + + GLWESecretPreparedFactory<BE> + + GLWEDecrypt<BE> + + GLWENoise<BE> + + FheUintBlocksPreparedFactory<u32, BE> + + FheUintBlocksPreparedEncryptSk<u32, BE> + + FheUintBlockDebugPrepare<BRA, u32, BE> + + BDDKeyEncryptSk<BRA, BE> + + BDDKeyPreparedFactory<BRA, BE> + + GGSWNoise<BE> + + FheUintBlocksPrepare<BRA, u32, BE> + + ExecuteBDDCircuit2WTo1W<u32, BE> + + GLWEEncryptSk<BE>, + BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, +{ + let glwe_infos: GLWELayout = TEST_GLWE_INFOS; + let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS; + + let n_glwe: usize = glwe_infos.n().into(); + + let module: Module<BE> = Module::<BE>::new(n_glwe as u64); + let mut source: Source = Source::new([6u8; 32]); + let mut source_xs: Source = Source::new([1u8; 32]); + let mut source_xa: Source = Source::new([2u8; 32]); + let mut source_xe: Source = Source::new([3u8; 32]); + + let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks<Vec<u8>, u32> = FheUintBlocks::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + + let a: u32 = source.next_u32(); + let b: u32 = source.next_u32() & 15; + + source.fill_bytes(&mut scratch.borrow().data); + a_enc_prep.encrypt_sk( + &module, + a, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + source.fill_bytes(&mut scratch.borrow().data); + b_enc_prep.encrypt_sk( + &module, + b, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + + res.srl(&module, &a_enc_prep, &b_enc_prep, scratch.borrow()); + + assert_eq!( + res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), + a >> b + ); +}
A poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sub.rs

@@ -0,0 +1,92 @@

+use poulpy_core::{ + GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, + layouts::{GGSWLayout, GLWELayout, GLWESecret, 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, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUintBlockDebugPrepare, FheUintBlocks, + FheUintBlocksPrepare, FheUintBlocksPrepared, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, Sub, + tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS}, + }, + blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, +}; + +pub fn test_bdd_sub<BRA: BlindRotationAlgo, BE: Backend>() +where + Module<BE>: ModuleNew<BE> + + GLWESecretPreparedFactory<BE> + + GLWEDecrypt<BE> + + GLWENoise<BE> + + FheUintBlocksPreparedFactory<u32, BE> + + FheUintBlocksPreparedEncryptSk<u32, BE> + + FheUintBlockDebugPrepare<BRA, u32, BE> + + BDDKeyEncryptSk<BRA, BE> + + BDDKeyPreparedFactory<BRA, BE> + + GGSWNoise<BE> + + FheUintBlocksPrepare<BRA, u32, BE> + + ExecuteBDDCircuit2WTo1W<u32, BE> + + GLWEEncryptSk<BE>, + BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, +{ + let glwe_infos: GLWELayout = TEST_GLWE_INFOS; + let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS; + + let n_glwe: usize = glwe_infos.n().into(); + + let module: Module<BE> = Module::<BE>::new(n_glwe as u64); + let mut source: Source = Source::new([6u8; 32]); + let mut source_xs: Source = Source::new([1u8; 32]); + let mut source_xa: Source = Source::new([2u8; 32]); + let mut source_xe: Source = Source::new([3u8; 32]); + + let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks<Vec<u8>, u32> = FheUintBlocks::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + + let a: u32 = source.next_u32(); + let b: u32 = source.next_u32(); + + source.fill_bytes(&mut scratch.borrow().data); + a_enc_prep.encrypt_sk( + &module, + a, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + source.fill_bytes(&mut scratch.borrow().data); + b_enc_prep.encrypt_sk( + &module, + b, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + + res.sub(&module, &a_enc_prep, &b_enc_prep, scratch.borrow()); + + assert_eq!( + res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), + a.wrapping_sub(b) + ); +}
A poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/xor.rs

@@ -0,0 +1,89 @@

+use poulpy_core::{ + GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, + layouts::{GGSWLayout, GLWELayout, GLWESecret, 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, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUintBlockDebugPrepare, FheUintBlocks, + FheUintBlocksPrepare, FheUintBlocksPrepared, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, Xor, + tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS}, + }, + blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, +}; + +pub fn test_bdd_xor<BRA: BlindRotationAlgo, BE: Backend>() +where + Module<BE>: ModuleNew<BE> + + GLWESecretPreparedFactory<BE> + + GLWEDecrypt<BE> + + GLWENoise<BE> + + FheUintBlocksPreparedFactory<u32, BE> + + FheUintBlocksPreparedEncryptSk<u32, BE> + + FheUintBlockDebugPrepare<BRA, u32, BE> + + BDDKeyEncryptSk<BRA, BE> + + BDDKeyPreparedFactory<BRA, BE> + + GGSWNoise<BE> + + FheUintBlocksPrepare<BRA, u32, BE> + + ExecuteBDDCircuit2WTo1W<u32, BE> + + GLWEEncryptSk<BE>, + BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, +{ + let glwe_infos: GLWELayout = TEST_GLWE_INFOS; + let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS; + + let n_glwe: usize = glwe_infos.n().into(); + + let module: Module<BE> = Module::<BE>::new(n_glwe as u64); + let mut source: Source = Source::new([6u8; 32]); + let mut source_xs: Source = Source::new([1u8; 32]); + let mut source_xa: Source = Source::new([2u8; 32]); + let mut source_xe: Source = Source::new([3u8; 32]); + + let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks<Vec<u8>, u32> = FheUintBlocks::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = + FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); + + let a: u32 = source.next_u32(); + let b: u32 = source.next_u32(); + + source.fill_bytes(&mut scratch.borrow().data); + a_enc_prep.encrypt_sk( + &module, + a, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + source.fill_bytes(&mut scratch.borrow().data); + b_enc_prep.encrypt_sk( + &module, + b, + &sk_glwe_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + + res.xor(&module, &a_enc_prep, &b_enc_prep, scratch.borrow()); + + assert_eq!(res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), a ^ b); +}