From 06795e95479d24f923217ad406b67463feb88170 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Thu, 23 Oct 2025 10:11:12 +0200 Subject: [PATCH] Add tests to BDD --- poulpy-core/src/noise/ggsw.rs | 6 +- .../tests/test_suite/automorphism/ggsw_ct.rs | 4 +- .../tests/test_suite/encryption/ggsw_ct.rs | 4 +- .../test_suite/external_product/ggsw_ct.rs | 4 +- .../src/tests/test_suite/keyswitch/ggsw_ct.rs | 4 +- .../tfhe/bdd_arithmetic/ciphertexts/block.rs | 15 +-- .../bdd_arithmetic/ciphertexts/block_debug.rs | 33 ++++--- .../ciphertexts/block_prepared.rs | 8 +- .../tfhe/bdd_arithmetic/ciphertexts/mod.rs | 5 +- poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs | 4 +- poulpy-schemes/src/tfhe/bdd_arithmetic/mod.rs | 7 +- .../tfhe/bdd_arithmetic/tests/fft64_ref.rs | 64 +++++++++++++ .../src/tfhe/bdd_arithmetic/tests/mod.rs | 4 + .../bdd_arithmetic/tests/test_suite/add.rs | 93 +++++++++++++++++++ .../bdd_arithmetic/tests/test_suite/and.rs | 89 ++++++++++++++++++ .../test_suite/mod.rs} | 36 ++++--- .../bdd_arithmetic/tests/test_suite/or.rs | 89 ++++++++++++++++++ .../{test.rs => tests/test_suite/prepare.rs} | 84 +++++++---------- .../bdd_arithmetic/tests/test_suite/sll.rs | 92 ++++++++++++++++++ .../bdd_arithmetic/tests/test_suite/slt.rs | 93 +++++++++++++++++++ .../bdd_arithmetic/tests/test_suite/sltu.rs | 93 +++++++++++++++++++ .../bdd_arithmetic/tests/test_suite/sra.rs | 92 ++++++++++++++++++ .../bdd_arithmetic/tests/test_suite/srl.rs | 92 ++++++++++++++++++ .../bdd_arithmetic/tests/test_suite/sub.rs | 92 ++++++++++++++++++ .../bdd_arithmetic/tests/test_suite/xor.rs | 89 ++++++++++++++++++ 25 files changed, 1080 insertions(+), 116 deletions(-) create mode 100644 poulpy-schemes/src/tfhe/bdd_arithmetic/tests/fft64_ref.rs create mode 100644 poulpy-schemes/src/tfhe/bdd_arithmetic/tests/mod.rs create mode 100644 poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/add.rs create mode 100644 poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/and.rs rename poulpy-schemes/src/tfhe/bdd_arithmetic/{parameters.rs => tests/test_suite/mod.rs} (88%) create mode 100644 poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/or.rs rename poulpy-schemes/src/tfhe/bdd_arithmetic/{test.rs => tests/test_suite/prepare.rs} (62%) create mode 100644 poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sll.rs create mode 100644 poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/slt.rs create mode 100644 poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sltu.rs create mode 100644 poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sra.rs create mode 100644 poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/srl.rs create mode 100644 poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sub.rs create mode 100644 poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/xor.rs diff --git a/poulpy-core/src/noise/ggsw.rs b/poulpy-core/src/noise/ggsw.rs index 7adeeca..b6805c6 100644 --- a/poulpy-core/src/noise/ggsw.rs +++ b/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 GGSW { - pub fn assert_noise(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: F) + pub fn assert_noise(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: &F) where S: GLWESecretPreparedToRef, P: ScalarZnxToRef, @@ -32,7 +32,7 @@ impl GGSW { } pub trait GGSWNoise { - fn ggsw_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: F) + fn ggsw_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: &F) where R: GGSWToRef, S: GLWESecretPreparedToRef, @@ -57,7 +57,7 @@ where Scratch: ScratchTakeBasic, ScratchOwned: ScratchOwnedBorrow + ScratchOwnedAlloc, { - fn ggsw_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: F) + fn ggsw_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: &F) where R: GGSWToRef, S: GLWESecretPreparedToRef, diff --git a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs index c3aa3c3..b978a9d 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -168,7 +168,7 @@ where ) + 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 @@ where ) + 0.5 }; - ct.assert_noise(module, &sk_prepared, &pt_scalar, max_noise); + ct.assert_noise(module, &sk_prepared, &pt_scalar, &max_noise); } } } diff --git a/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs index 54b5df0..e53eedf 100644 --- a/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs @@ -65,7 +65,7 @@ where 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 @@ where let mut ct: GGSW> = 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); } } } diff --git a/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs index 3fe2da4..b455b70 100644 --- a/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs @@ -143,7 +143,7 @@ where ) + 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 @@ where ) + 0.5 }; - ggsw_out.assert_noise(module, &sk_prepared, &pt_in, max_noise); + ggsw_out.assert_noise(module, &sk_prepared, &pt_in, &max_noise); } } } diff --git a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs index b582d89..28e71a5 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs @@ -175,7 +175,7 @@ where ) + 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 @@ where ) + 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); } } } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs index f670fe7..0109eb7 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs @@ -5,12 +5,9 @@ use poulpy_core::{ 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 GLWEInfos for FheUintBlocks { } impl FheUintBlocks, T> { - #[allow(dead_code)] - pub(crate) fn alloc(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &Module, 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(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { + pub fn alloc(module: &Module, 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 FheUintBlocks, T> { } impl FheUintBlocks { - #[allow(dead_code)] - #[cfg(test)] - pub(crate) fn encrypt_sk( + pub fn encrypt_sk( &mut self, module: &Module, value: T, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_debug.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_debug.rs index 61c4776..bf98972 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_debug.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_debug.rs @@ -7,32 +7,30 @@ use crate::tfhe::{ 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 { +pub struct FheUintBlocksPreparedDebug { pub(crate) blocks: Vec>, pub(crate) _base: u8, pub(crate) _phantom: PhantomData, } -#[cfg(test)] impl FheUintBlocksPreparedDebug, T> { - #[allow(dead_code)] - pub(crate) fn alloc(module: &M, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where M: ModuleN, A: GGSWInfos, { - Self::alloc_with( + Self::alloc( module, infos.base2k(), infos.k(), @@ -42,8 +40,7 @@ impl FheUintBlocksPreparedDebug, T> { ) } - #[allow(dead_code)] - pub(crate) fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self where M: ModuleN, { @@ -88,8 +85,7 @@ impl GGSWInfos for FheUintBlocksPreparedDebug FheUintBlocksPreparedDebug { - #[allow(dead_code)] - pub(crate) fn noise(&self, module: &M, sk: &S, want: T) + pub fn print_noise(&self, module: &M, sk: &S, want: T) where S: GLWESecretPreparedToRef, M: GGSWNoise, @@ -101,6 +97,20 @@ impl FheUintBlocksPreparedDebug { ggsw.print_noise(module, sk, &pt_want); } } + + pub fn assert_noise(&self, module: &M, sk: &S, want: T, max_noise: &F) + where + S: GLWESecretPreparedToRef, + M: GGSWNoise, + 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); + } + } } impl FheUintBlockDebugPrepare for Module @@ -130,7 +140,6 @@ where } impl FheUintBlocksPreparedDebug { - #[allow(dead_code)] pub fn prepare( &mut self, module: &M, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs index 2eabade..bdc1945 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs @@ -3,17 +3,16 @@ use std::marker::PhantomData; 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 @@ impl FheUintBlocksPrepared, T, BE> { } } -#[cfg(test)] impl FheUintBlocksPreparedEncryptSk for Module where Self: Sized + ModuleN + GGSWEncryptSk + GGSWPreparedFactory { } -#[cfg(test)] pub trait FheUintBlocksPreparedEncryptSk where Self: Sized + ModuleN + GGSWEncryptSk + GGSWPreparedFactory, @@ -126,7 +123,6 @@ where } } -#[cfg(test)] impl FheUintBlocksPrepared { pub(crate) fn encrypt_sk( &mut self, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/mod.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/mod.rs index b8054d7..9dcc018 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/mod.rs +++ b/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::*; diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs index d1c20c6..373eb7c 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs +++ b/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 @@ impl FheUintBlocksPrepared { +pub trait FheUintBlockDebugPrepare { fn fhe_uint_block_debug_prepare( &self, res: &mut FheUintBlocksPreparedDebug, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/mod.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/mod.rs index d89348f..0f66049 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/mod.rs +++ b/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; diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/fft64_ref.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/fft64_ref.rs new file mode 100644 index 0000000..fbae3c8 --- /dev/null +++ b/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::() +} + +#[test] +fn test_bdd_add_fft64_ref() { + test_bdd_add::() +} + +#[test] +fn test_bdd_and_fft64_ref() { + test_bdd_and::() +} + +#[test] +fn test_bdd_or_fft64_ref() { + test_bdd_or::() +} + +#[test] +fn test_bdd_sll_fft64_ref() { + test_bdd_sll::() +} + +#[test] +fn test_bdd_slt_fft64_ref() { + test_bdd_slt::() +} + +#[test] +fn test_bdd_sltu_fft64_ref() { + test_bdd_sltu::() +} + +#[test] +fn test_bdd_sra_fft64_ref() { + test_bdd_sra::() +} + +#[test] +fn test_bdd_srl_fft64_ref() { + test_bdd_srl::() +} + +#[test] +fn test_bdd_sub_fft64_ref() { + test_bdd_sub::() +} + +#[test] +fn test_bdd_xor_fft64_ref() { + test_bdd_xor::() +} diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/mod.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/mod.rs new file mode 100644 index 0000000..d1828a6 --- /dev/null +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/mod.rs @@ -0,0 +1,4 @@ +pub mod test_suite; + +#[cfg(test)] +mod fft64_ref; diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/add.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/add.rs new file mode 100644 index 0000000..2b568f6 --- /dev/null +++ b/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() +where + Module: ModuleNew + + GLWESecretPreparedFactory + + GLWEDecrypt + + GLWENoise + + FheUintBlocksPreparedFactory + + FheUintBlocksPreparedEncryptSk + + FheUintBlockDebugPrepare + + BDDKeyEncryptSk + + BDDKeyPreparedFactory + + GGSWNoise + + FheUintBlocksPrepare + + 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 n_glwe: usize = glwe_infos.n().into(); + + let module: Module = Module::::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 = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks, u32> = FheUintBlocks::, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, 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) + ); +} diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/and.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/and.rs new file mode 100644 index 0000000..41936e0 --- /dev/null +++ b/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() +where + Module: ModuleNew + + GLWESecretPreparedFactory + + GLWEDecrypt + + GLWENoise + + FheUintBlocksPreparedFactory + + FheUintBlocksPreparedEncryptSk + + FheUintBlockDebugPrepare + + BDDKeyEncryptSk + + BDDKeyPreparedFactory + + GGSWNoise + + FheUintBlocksPrepare + + 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 n_glwe: usize = glwe_infos.n().into(); + + let module: Module = Module::::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 = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks, u32> = FheUintBlocks::, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, 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); +} diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/mod.rs similarity index 88% rename from poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs rename to poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/mod.rs index f807414..73c96d3 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs +++ b/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 @@ pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout { 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 @@ pub(crate) static TEST_GGSW_INFOS: GGSWLayout = GGSWLayout { dsize: Dsize(1), }; -#[cfg(test)] pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { cbt: CircuitBootstrappingKeyLayout { layout_brk: BlindRotationKeyLayout { diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/or.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/or.rs new file mode 100644 index 0000000..779a82e --- /dev/null +++ b/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() +where + Module: ModuleNew + + GLWESecretPreparedFactory + + GLWEDecrypt + + GLWENoise + + FheUintBlocksPreparedFactory + + FheUintBlocksPreparedEncryptSk + + FheUintBlockDebugPrepare + + BDDKeyEncryptSk + + BDDKeyPreparedFactory + + GGSWNoise + + FheUintBlocksPrepare + + 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 n_glwe: usize = glwe_infos.n().into(); + + let module: Module = Module::::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 = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks, u32> = FheUintBlocks::, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, 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); +} diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/prepare.rs similarity index 62% rename from poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs rename to poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/prepare.rs index 3fd5dd5..3fcab1b 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs +++ b/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::() -} - -fn test_bdd_2w_to_1w() +pub fn test_bdd_prepare() where Module: ModuleNew + GLWESecretPreparedFactory @@ -59,28 +51,24 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); + // GLWE Secret let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared, 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> = 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, 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 @@ where let mut bdd_key_prepared: BDDKeyPrepared, 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, u32> = FheUintBlocks::, u32>::alloc(&module, &glwe_infos); - let mut a_enc_prep: FheUintBlocksPrepared, u32, BE> = - FheUintBlocksPrepared::, u32, BE>::alloc(&module, &ggsw_infos); - let mut b_enc_prep: FheUintBlocksPrepared, u32, BE> = - FheUintBlocksPrepared::, 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, 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, u32> = + FheUintBlocksPreparedDebug::, 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()); - println!("other want: {:032b}", b.wrapping_add(a)); + 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 + }; - // print a, b, and d - println!("a: {a}"); - println!("b: {b}"); + // c_enc_prep_debug.print_noise(&module, &sk_glwe_prep, value); + + c_enc_prep_debug.assert_noise(&module, &sk_glwe_prep, value, &max_noise); } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sll.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sll.rs new file mode 100644 index 0000000..3378d05 --- /dev/null +++ b/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() +where + Module: ModuleNew + + GLWESecretPreparedFactory + + GLWEDecrypt + + GLWENoise + + FheUintBlocksPreparedFactory + + FheUintBlocksPreparedEncryptSk + + FheUintBlockDebugPrepare + + BDDKeyEncryptSk + + BDDKeyPreparedFactory + + GGSWNoise + + FheUintBlocksPrepare + + 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 n_glwe: usize = glwe_infos.n().into(); + + let module: Module = Module::::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 = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks, u32> = FheUintBlocks::, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, 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) + ); +} diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/slt.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/slt.rs new file mode 100644 index 0000000..9ae2203 --- /dev/null +++ b/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() +where + Module: ModuleNew + + GLWESecretPreparedFactory + + GLWEDecrypt + + GLWENoise + + FheUintBlocksPreparedFactory + + FheUintBlocksPreparedEncryptSk + + FheUintBlockDebugPrepare + + BDDKeyEncryptSk + + BDDKeyPreparedFactory + + GGSWNoise + + FheUintBlocksPrepare + + 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 n_glwe: usize = glwe_infos.n().into(); + + let module: Module = Module::::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 = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks, u32> = FheUintBlocks::, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, 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 + ); +} diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sltu.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sltu.rs new file mode 100644 index 0000000..4ffc2b3 --- /dev/null +++ b/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() +where + Module: ModuleNew + + GLWESecretPreparedFactory + + GLWEDecrypt + + GLWENoise + + FheUintBlocksPreparedFactory + + FheUintBlocksPreparedEncryptSk + + FheUintBlockDebugPrepare + + BDDKeyEncryptSk + + BDDKeyPreparedFactory + + GGSWNoise + + FheUintBlocksPrepare + + 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 n_glwe: usize = glwe_infos.n().into(); + + let module: Module = Module::::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 = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks, u32> = FheUintBlocks::, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, 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 + ); +} diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sra.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sra.rs new file mode 100644 index 0000000..554a57e --- /dev/null +++ b/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() +where + Module: ModuleNew + + GLWESecretPreparedFactory + + GLWEDecrypt + + GLWENoise + + FheUintBlocksPreparedFactory + + FheUintBlocksPreparedEncryptSk + + FheUintBlockDebugPrepare + + BDDKeyEncryptSk + + BDDKeyPreparedFactory + + GGSWNoise + + FheUintBlocksPrepare + + 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 n_glwe: usize = glwe_infos.n().into(); + + let module: Module = Module::::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 = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks, u32> = FheUintBlocks::, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, 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 + ); +} diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/srl.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/srl.rs new file mode 100644 index 0000000..b79ec56 --- /dev/null +++ b/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() +where + Module: ModuleNew + + GLWESecretPreparedFactory + + GLWEDecrypt + + GLWENoise + + FheUintBlocksPreparedFactory + + FheUintBlocksPreparedEncryptSk + + FheUintBlockDebugPrepare + + BDDKeyEncryptSk + + BDDKeyPreparedFactory + + GGSWNoise + + FheUintBlocksPrepare + + 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 n_glwe: usize = glwe_infos.n().into(); + + let module: Module = Module::::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 = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks, u32> = FheUintBlocks::, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, 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 + ); +} diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sub.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sub.rs new file mode 100644 index 0000000..159e414 --- /dev/null +++ b/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() +where + Module: ModuleNew + + GLWESecretPreparedFactory + + GLWEDecrypt + + GLWENoise + + FheUintBlocksPreparedFactory + + FheUintBlocksPreparedEncryptSk + + FheUintBlockDebugPrepare + + BDDKeyEncryptSk + + BDDKeyPreparedFactory + + GGSWNoise + + FheUintBlocksPrepare + + 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 n_glwe: usize = glwe_infos.n().into(); + + let module: Module = Module::::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 = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks, u32> = FheUintBlocks::, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, 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) + ); +} diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/xor.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/xor.rs new file mode 100644 index 0000000..1f1f8ef --- /dev/null +++ b/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() +where + Module: ModuleNew + + GLWESecretPreparedFactory + + GLWEDecrypt + + GLWENoise + + FheUintBlocksPreparedFactory + + FheUintBlocksPreparedEncryptSk + + FheUintBlockDebugPrepare + + BDDKeyEncryptSk + + BDDKeyPreparedFactory + + GGSWNoise + + FheUintBlocksPrepare + + 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 n_glwe: usize = glwe_infos.n().into(); + + let module: Module = Module::::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 = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); + sk_glwe.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); + + let mut res: FheUintBlocks, u32> = FheUintBlocks::, u32>::alloc_from_infos(&module, &glwe_infos); + let mut a_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, 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); +}