Add tests to BDD

This commit is contained in:
Pro7ech
2025-10-23 10:11:12 +02:00
parent 9067de8d96
commit 06795e9547
25 changed files with 1080 additions and 116 deletions

View File

@@ -11,7 +11,7 @@ use crate::layouts::prepared::GLWESecretPreparedToRef;
use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared};
impl<D: DataRef> GGSW<D> { 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 where
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef, P: ScalarZnxToRef,
@@ -32,7 +32,7 @@ impl<D: DataRef> GGSW<D> {
} }
pub trait GGSWNoise<BE: Backend> { 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 where
R: GGSWToRef, R: GGSWToRef,
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,
@@ -57,7 +57,7 @@ where
Scratch<BE>: ScratchTakeBasic, Scratch<BE>: ScratchTakeBasic,
ScratchOwned<BE>: ScratchOwnedBorrow<BE> + ScratchOwnedAlloc<BE>, 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 where
R: GGSWToRef, R: GGSWToRef,
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,

View File

@@ -168,7 +168,7 @@ where
) + 0.5 ) + 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 ) + 0.5
}; };
ct.assert_noise(module, &sk_prepared, &pt_scalar, max_noise); ct.assert_noise(module, &sk_prepared, &pt_scalar, &max_noise);
} }
} }
} }

View File

@@ -65,7 +65,7 @@ where
let noise_f = |_col_i: usize| -(k as f64) + SIGMA.log2() + 0.5; 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<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos); let mut ct: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos);
ct.decompress(module, &ct_compressed); 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);
} }
} }
} }

View File

@@ -143,7 +143,7 @@ where
) + 0.5 ) + 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 ) + 0.5
}; };
ggsw_out.assert_noise(module, &sk_prepared, &pt_in, max_noise); ggsw_out.assert_noise(module, &sk_prepared, &pt_in, &max_noise);
} }
} }
} }

View File

@@ -175,7 +175,7 @@ where
) + 0.5 ) + 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 ) + 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);
} }
} }
} }

View File

@@ -5,12 +5,9 @@ use poulpy_core::{
layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, LWEInfos, Rank, TorusPrecision}, layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, LWEInfos, Rank, TorusPrecision},
}; };
#[cfg(test)]
use poulpy_core::GLWEEncryptSk; use poulpy_core::GLWEEncryptSk;
use poulpy_core::ScratchTakeCore; use poulpy_core::ScratchTakeCore;
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, ZnxZero}; use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, ZnxZero};
#[cfg(test)]
#[cfg(test)]
use poulpy_hal::source::Source; use poulpy_hal::source::Source;
use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger}; use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger};
@@ -43,16 +40,14 @@ impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUintBlocks<D, T> {
} }
impl<T: UnsignedInteger> FheUintBlocks<Vec<u8>, T> { impl<T: UnsignedInteger> FheUintBlocks<Vec<u8>, T> {
#[allow(dead_code)] pub fn alloc_from_infos<A, BE: Backend>(module: &Module<BE>, infos: &A) -> Self
pub(crate) fn alloc<A, BE: Backend>(module: &Module<BE>, infos: &A) -> Self
where where
A: GLWEInfos, 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 fn alloc<BE: Backend>(module: &Module<BE>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
pub(crate) fn alloc_with<BE: Backend>(module: &Module<BE>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
Self { Self {
blocks: (0..T::WORD_SIZE) blocks: (0..T::WORD_SIZE)
.map(|_| GLWE::alloc(module.n().into(), base2k, k, rank)) .map(|_| GLWE::alloc(module.n().into(), base2k, k, rank))
@@ -64,9 +59,7 @@ impl<T: UnsignedInteger> FheUintBlocks<Vec<u8>, T> {
} }
impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintBlocks<D, T> { impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintBlocks<D, T> {
#[allow(dead_code)] pub fn encrypt_sk<S, BE: Backend>(
#[cfg(test)]
pub(crate) fn encrypt_sk<S, BE: Backend>(
&mut self, &mut self,
module: &Module<BE>, module: &Module<BE>,
value: T, value: T,

View File

@@ -7,32 +7,30 @@ use crate::tfhe::{
circuit_bootstrapping::CirtuitBootstrappingExecute, circuit_bootstrapping::CirtuitBootstrappingExecute,
}; };
use poulpy_core::GGSWNoise; use poulpy_core::GGSWNoise;
#[cfg(test)]
use poulpy_core::layouts::{Base2K, Dnum, Dsize, Rank, TorusPrecision}; use poulpy_core::layouts::{Base2K, Dnum, Dsize, Rank, TorusPrecision};
use poulpy_core::layouts::{GGSW, GLWESecretPreparedToRef}; use poulpy_core::layouts::{GGSW, GLWESecretPreparedToRef};
use poulpy_core::{ use poulpy_core::{
LWEFromGLWE, ScratchTakeCore, LWEFromGLWE, ScratchTakeCore,
layouts::{GGSWInfos, GGSWPreparedFactory, GLWEInfos, LWE, LWEInfos}, layouts::{GGSWInfos, GGSWPreparedFactory, GLWEInfos, LWE, LWEInfos},
}; };
#[cfg(test)]
use poulpy_hal::api::ModuleN; use poulpy_hal::api::ModuleN;
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; 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) blocks: Vec<GGSW<D>>,
pub(crate) _base: u8, pub(crate) _base: u8,
pub(crate) _phantom: PhantomData<T>, pub(crate) _phantom: PhantomData<T>,
} }
#[cfg(test)]
impl<T: UnsignedInteger> FheUintBlocksPreparedDebug<Vec<u8>, T> { impl<T: UnsignedInteger> FheUintBlocksPreparedDebug<Vec<u8>, T> {
#[allow(dead_code)] pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
pub(crate) fn alloc<A, M>(module: &M, infos: &A) -> Self
where where
M: ModuleN, M: ModuleN,
A: GGSWInfos, A: GGSWInfos,
{ {
Self::alloc_with( Self::alloc(
module, module,
infos.base2k(), infos.base2k(),
infos.k(), infos.k(),
@@ -42,8 +40,7 @@ impl<T: UnsignedInteger> FheUintBlocksPreparedDebug<Vec<u8>, T> {
) )
} }
#[allow(dead_code)] pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self
pub(crate) fn alloc_with<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self
where where
M: ModuleN, M: ModuleN,
{ {
@@ -88,8 +85,7 @@ impl<D: DataRef, T: UnsignedInteger> GGSWInfos for FheUintBlocksPreparedDebug<D,
} }
impl<D: DataRef, T: UnsignedInteger + ToBits> FheUintBlocksPreparedDebug<D, T> { impl<D: DataRef, T: UnsignedInteger + ToBits> FheUintBlocksPreparedDebug<D, T> {
#[allow(dead_code)] pub fn print_noise<S, M, BE: Backend>(&self, module: &M, sk: &S, want: T)
pub(crate) fn noise<S, M, BE: Backend>(&self, module: &M, sk: &S, want: T)
where where
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,
M: GGSWNoise<BE>, M: GGSWNoise<BE>,
@@ -101,6 +97,20 @@ impl<D: DataRef, T: UnsignedInteger + ToBits> FheUintBlocksPreparedDebug<D, T> {
ggsw.print_noise(module, sk, &pt_want); 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);
}
}
} }
impl<BRA: BlindRotationAlgo, BE: Backend, T: UnsignedInteger> FheUintBlockDebugPrepare<BRA, T, BE> for Module<BE> impl<BRA: BlindRotationAlgo, BE: Backend, T: UnsignedInteger> FheUintBlockDebugPrepare<BRA, T, BE> for Module<BE>
@@ -130,7 +140,6 @@ where
} }
impl<D: DataMut, T: UnsignedInteger> FheUintBlocksPreparedDebug<D, T> { impl<D: DataMut, T: UnsignedInteger> FheUintBlocksPreparedDebug<D, T> {
#[allow(dead_code)]
pub fn prepare<BRA, M, O, K, BE: Backend>( pub fn prepare<BRA, M, O, K, BE: Backend>(
&mut self, &mut self,
module: &M, module: &M,

View File

@@ -3,17 +3,16 @@ use std::marker::PhantomData;
use poulpy_core::layouts::{ use poulpy_core::layouts::{
Base2K, Dnum, Dsize, GGSWInfos, GGSWPreparedFactory, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared, Base2K, Dnum, Dsize, GGSWInfos, GGSWPreparedFactory, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared,
}; };
#[cfg(test)]
use poulpy_core::{GGSWEncryptSk, ScratchTakeCore, layouts::GLWESecretPreparedToRef}; use poulpy_core::{GGSWEncryptSk, ScratchTakeCore, layouts::GLWESecretPreparedToRef};
use poulpy_hal::layouts::{Backend, Data, DataRef, Module}; use poulpy_hal::layouts::{Backend, Data, DataRef, Module};
#[cfg(test)]
use poulpy_hal::{ use poulpy_hal::{
api::ModuleN, api::ModuleN,
layouts::{DataMut, Scratch}, layouts::{DataMut, Scratch},
source::Source, source::Source,
}; };
#[cfg(test)]
use crate::tfhe::bdd_arithmetic::ToBits; use crate::tfhe::bdd_arithmetic::ToBits;
use crate::tfhe::bdd_arithmetic::UnsignedInteger; use crate::tfhe::bdd_arithmetic::UnsignedInteger;
@@ -83,13 +82,11 @@ impl<T: UnsignedInteger, BE: Backend> FheUintBlocksPrepared<Vec<u8>, T, BE> {
} }
} }
#[cfg(test)]
impl<T: UnsignedInteger + ToBits, BE: Backend> FheUintBlocksPreparedEncryptSk<T, BE> for Module<BE> where impl<T: UnsignedInteger + ToBits, BE: Backend> FheUintBlocksPreparedEncryptSk<T, BE> for Module<BE> where
Self: Sized + ModuleN + GGSWEncryptSk<BE> + GGSWPreparedFactory<BE> Self: Sized + ModuleN + GGSWEncryptSk<BE> + GGSWPreparedFactory<BE>
{ {
} }
#[cfg(test)]
pub trait FheUintBlocksPreparedEncryptSk<T: UnsignedInteger + ToBits, BE: Backend> pub trait FheUintBlocksPreparedEncryptSk<T: UnsignedInteger + ToBits, BE: Backend>
where where
Self: Sized + ModuleN + GGSWEncryptSk<BE> + GGSWPreparedFactory<BE>, Self: Sized + ModuleN + GGSWEncryptSk<BE> + GGSWPreparedFactory<BE>,
@@ -126,7 +123,6 @@ where
} }
} }
#[cfg(test)]
impl<D: DataMut, T: UnsignedInteger + ToBits, BE: Backend> FheUintBlocksPrepared<D, T, BE> { impl<D: DataMut, T: UnsignedInteger + ToBits, BE: Backend> FheUintBlocksPrepared<D, T, BE> {
pub(crate) fn encrypt_sk<M, S>( pub(crate) fn encrypt_sk<M, S>(
&mut self, &mut self,

View File

@@ -2,10 +2,9 @@ mod block;
mod block_prepared; mod block_prepared;
mod word; mod word;
#[cfg(test)]
mod block_debug; mod block_debug;
#[cfg(test)]
pub(crate) use block_debug::*; pub use block_debug::*;
pub use block::*; pub use block::*;
pub use block_prepared::*; pub use block_prepared::*;

View File

@@ -1,4 +1,3 @@
#[cfg(test)]
use crate::tfhe::bdd_arithmetic::FheUintBlocksPreparedDebug; use crate::tfhe::bdd_arithmetic::FheUintBlocksPreparedDebug;
use crate::tfhe::{ use crate::tfhe::{
bdd_arithmetic::{FheUintBlocks, FheUintBlocksPrepared, UnsignedInteger}, bdd_arithmetic::{FheUintBlocks, FheUintBlocksPrepared, UnsignedInteger},
@@ -281,8 +280,7 @@ impl<D: DataMut, T: UnsignedInteger, BE: Backend> FheUintBlocksPrepared<D, T, BE
} }
} }
#[cfg(test)] pub trait FheUintBlockDebugPrepare<BRA: BlindRotationAlgo, T: UnsignedInteger, BE: Backend> {
pub(crate) trait FheUintBlockDebugPrepare<BRA: BlindRotationAlgo, T: UnsignedInteger, BE: Backend> {
fn fhe_uint_block_debug_prepare<DM, DR0, DR1>( fn fhe_uint_block_debug_prepare<DM, DR0, DR1>(
&self, &self,
res: &mut FheUintBlocksPreparedDebug<DM, T>, res: &mut FheUintBlocksPreparedDebug<DM, T>,

View File

@@ -3,7 +3,6 @@ mod ciphertexts;
mod circuits; mod circuits;
mod eval; mod eval;
mod key; mod key;
mod parameters;
pub use bdd_2w_to_1w::*; pub use bdd_2w_to_1w::*;
pub use ciphertexts::*; pub use ciphertexts::*;
@@ -11,11 +10,7 @@ pub(crate) use circuits::*;
pub(crate) use eval::*; pub(crate) use eval::*;
pub use key::*; pub use key::*;
#[cfg(test)] pub mod tests;
pub(crate) use parameters::*;
#[cfg(test)]
mod test;
pub trait UnsignedInteger: Copy + 'static { pub trait UnsignedInteger: Copy + 'static {
const WORD_SIZE: usize; const WORD_SIZE: usize;

View File

@@ -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>()
}

View File

@@ -0,0 +1,4 @@
pub mod test_suite;
#[cfg(test)]
mod fft64_ref;

View File

@@ -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)
);
}

View File

@@ -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);
}

View File

@@ -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::{ use poulpy_core::layouts::{
Base2K, Degree, Dnum, Dsize, GGSWLayout, GLWEAutomorphismKeyLayout, GLWELayout, GLWETensorKeyLayout, GLWEToLWEKeyLayout, Base2K, Degree, Dnum, Dsize, GGSWLayout, GLWEAutomorphismKeyLayout, GLWELayout, GLWETensorKeyLayout, GLWEToLWEKeyLayout,
Rank, TorusPrecision, Rank, TorusPrecision,
}; };
#[cfg(test)]
use crate::tfhe::{ use crate::tfhe::{
bdd_arithmetic::BDDKeyLayout, blind_rotation::BlindRotationKeyLayout, circuit_bootstrapping::CircuitBootstrappingKeyLayout, bdd_arithmetic::BDDKeyLayout, blind_rotation::BlindRotationKeyLayout, circuit_bootstrapping::CircuitBootstrappingKeyLayout,
}; };
#[cfg(test)]
pub(crate) const TEST_N_GLWE: u32 = 512; pub(crate) const TEST_N_GLWE: u32 = 512;
#[cfg(test)]
pub(crate) const TEST_N_LWE: u32 = 77; pub(crate) const TEST_N_LWE: u32 = 77;
#[cfg(test)]
pub(crate) const TEST_BASE2K: u32 = 13; pub(crate) const TEST_BASE2K: u32 = 13;
#[cfg(test)]
pub(crate) const TEST_K_GLWE: u32 = 26; pub(crate) const TEST_K_GLWE: u32 = 26;
#[cfg(test)]
pub(crate) const TEST_K_GGSW: u32 = 39; pub(crate) const TEST_K_GGSW: u32 = 39;
#[cfg(test)]
pub(crate) const TEST_BLOCK_SIZE: u32 = 7; pub(crate) const TEST_BLOCK_SIZE: u32 = 7;
#[cfg(test)]
pub(crate) const TEST_RANK: u32 = 2; pub(crate) const TEST_RANK: u32 = 2;
#[cfg(test)]
pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout { pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout {
n: Degree(TEST_N_GLWE), n: Degree(TEST_N_GLWE),
base2k: Base2K(TEST_BASE2K), base2k: Base2K(TEST_BASE2K),
@@ -32,7 +46,6 @@ pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout {
rank: Rank(TEST_RANK), rank: Rank(TEST_RANK),
}; };
#[cfg(test)]
pub(crate) static TEST_GGSW_INFOS: GGSWLayout = GGSWLayout { pub(crate) static TEST_GGSW_INFOS: GGSWLayout = GGSWLayout {
n: Degree(TEST_N_GLWE), n: Degree(TEST_N_GLWE),
base2k: Base2K(TEST_BASE2K), base2k: Base2K(TEST_BASE2K),
@@ -42,7 +55,6 @@ pub(crate) static TEST_GGSW_INFOS: GGSWLayout = GGSWLayout {
dsize: Dsize(1), dsize: Dsize(1),
}; };
#[cfg(test)]
pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout {
cbt: CircuitBootstrappingKeyLayout { cbt: CircuitBootstrappingKeyLayout {
layout_brk: BlindRotationKeyLayout { layout_brk: BlindRotationKeyLayout {

View File

@@ -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);
}

View File

@@ -1,8 +1,5 @@
use std::time::Instant;
use poulpy_backend::FFT64Ref;
use poulpy_core::{ use poulpy_core::{
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, SIGMA, ScratchTakeCore,
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, LWESecret, prepared::GLWESecretPrepared}, layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, LWESecret, prepared::GLWESecretPrepared},
}; };
use poulpy_hal::{ use poulpy_hal::{
@@ -14,20 +11,15 @@ use rand::RngCore;
use crate::tfhe::{ use crate::tfhe::{
bdd_arithmetic::{ bdd_arithmetic::{
Add, BDDKey, BDDKeyEncryptSk, BDDKeyLayout, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, BDDKey, BDDKeyEncryptSk, BDDKeyLayout, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W,
FheUintBlockDebugPrepare, FheUintBlocks, FheUintBlocksPrepare, FheUintBlocksPrepared, FheUintBlocksPreparedDebug, FheUintBlockDebugPrepare, FheUintBlocks, FheUintBlocksPrepare, FheUintBlocksPreparedDebug,
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, Sub, TEST_BDD_KEY_LAYOUT, TEST_BLOCK_SIZE, TEST_GGSW_INFOS, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory,
TEST_GLWE_INFOS, TEST_N_LWE, 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] pub fn test_bdd_prepare<BRA: BlindRotationAlgo, BE: Backend>()
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>()
where where
Module<BE>: ModuleNew<BE> Module<BE>: ModuleNew<BE>
+ GLWESecretPreparedFactory<BE> + GLWESecretPreparedFactory<BE>
@@ -59,28 +51,24 @@ where
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22); let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22);
// GLWE Secret
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
sk_glwe.fill_ternary_prob(0.5, &mut source_xs); 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); let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos);
sk_glwe_prep.prepare(&module, &sk_glwe); sk_glwe_prep.prepare(&module, &sk_glwe);
let a: u32 = 645139204; // LWE Secret
let b: u32 = 0;
println!("a: {a}");
println!("b: {b}");
let n_lwe: u32 = TEST_N_LWE; let n_lwe: u32 = TEST_N_LWE;
let block_size: u32 = TEST_BLOCK_SIZE; let block_size: u32 = TEST_BLOCK_SIZE;
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into()); let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into());
sk_lwe.fill_binary_block(block_size as usize, &mut source_xs); sk_lwe.fill_binary_block(block_size as usize, &mut source_xs);
// CBT KEY
let bdd_key_infos: BDDKeyLayout = TEST_BDD_KEY_LAYOUT; 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 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); source.fill_bytes(&mut scratch.borrow().data);
scratch.borrow().data.fill(0); scratch.borrow().data.fill(0);
bdd_key.encrypt_sk( bdd_key.encrypt_sk(
@@ -94,44 +82,36 @@ where
let mut bdd_key_prepared: BDDKeyPrepared<Vec<u8>, BRA, BE> = BDDKeyPrepared::alloc_from_infos(&module, &bdd_key_infos); 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); source.fill_bytes(&mut scratch.borrow().data);
bdd_key_prepared.prepare(&module, &bdd_key, scratch.borrow()); 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); // GLWE(value)
let mut a_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = let mut c_enc: FheUintBlocks<Vec<u8>, u32> = FheUintBlocks::alloc_from_infos(&module, &glwe_infos);
FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos); let value: u32 = source.next_u32();
let mut b_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> = c_enc.encrypt_sk(
FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
source.fill_bytes(&mut scratch.borrow().data);
a_enc_prep.encrypt_sk(
&module, &module,
a, value,
&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, &sk_glwe_prep,
&mut source_xa, &mut source_xa,
&mut source_xe, &mut source_xe,
scratch.borrow(), scratch.borrow(),
); );
// d + a // GGSW(0)
sum_enc.add(&module, &a_enc_prep, &b_enc_prep, scratch.borrow()); let mut c_enc_prep_debug: FheUintBlocksPreparedDebug<Vec<u8>, u32> =
FheUintBlocksPreparedDebug::<Vec<u8>, u32>::alloc_from_infos(&module, &ggsw_infos);
println!( // GGSW(value)
"other have: {:032b}", c_enc_prep_debug.prepare(&module, &c_enc, &bdd_key_prepared, scratch.borrow());
sum_enc.decrypt(&module, &sk_glwe_prep, 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 // c_enc_prep_debug.print_noise(&module, &sk_glwe_prep, value);
println!("a: {a}");
println!("b: {b}"); c_enc_prep_debug.assert_noise(&module, &sk_glwe_prep, value, &max_noise);
} }

View File

@@ -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)
);
}

View File

@@ -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
);
}

View File

@@ -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
);
}

View File

@@ -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
);
}

View File

@@ -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
);
}

View File

@@ -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)
);
}

View File

@@ -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);
}