update BDD ciphertext types + API for GLWEToLWE

This commit is contained in:
Pro7ech
2025-10-29 00:59:45 +01:00
parent 37c76b6420
commit c761d2cae0
23 changed files with 576 additions and 618 deletions

View File

@@ -1,74 +1,80 @@
use std::sync::LazyLock;
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, test_glwe_to_glwe_blind_rotation, test_scalar_to_ggsw_blind_rotation,
TestContext, 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, test_glwe_to_glwe_blind_rotation,
test_scalar_to_ggsw_blind_rotation,
},
blind_rotation::CGGI,
};
static TEST_CONTEXT_CGGI_FFT64_REF: LazyLock<TestContext<CGGI, FFT64Ref>> =
LazyLock::new(|| TestContext::<CGGI, FFT64Ref>::new());
#[test]
fn test_glwe_to_glwe_blind_rotation_fft64_ref() {
test_glwe_to_glwe_blind_rotation::<FFT64Ref>()
test_glwe_to_glwe_blind_rotation(&TEST_CONTEXT_CGGI_FFT64_REF)
}
#[test]
fn test_scalar_to_ggsw_blind_rotation_fft64_ref() {
test_scalar_to_ggsw_blind_rotation::<FFT64Ref>()
test_scalar_to_ggsw_blind_rotation(&TEST_CONTEXT_CGGI_FFT64_REF)
}
#[test]
fn test_bdd_prepare_fft64_ref() {
test_bdd_prepare::<CGGI, FFT64Ref>()
test_bdd_prepare(&TEST_CONTEXT_CGGI_FFT64_REF)
}
#[test]
fn test_bdd_add_fft64_ref() {
test_bdd_add::<CGGI, FFT64Ref>()
test_bdd_add(&TEST_CONTEXT_CGGI_FFT64_REF)
}
#[test]
fn test_bdd_and_fft64_ref() {
test_bdd_and::<CGGI, FFT64Ref>()
test_bdd_and(&TEST_CONTEXT_CGGI_FFT64_REF)
}
#[test]
fn test_bdd_or_fft64_ref() {
test_bdd_or::<CGGI, FFT64Ref>()
test_bdd_or(&TEST_CONTEXT_CGGI_FFT64_REF)
}
#[test]
fn test_bdd_sll_fft64_ref() {
test_bdd_sll::<CGGI, FFT64Ref>()
test_bdd_sll(&TEST_CONTEXT_CGGI_FFT64_REF)
}
#[test]
fn test_bdd_slt_fft64_ref() {
test_bdd_slt::<CGGI, FFT64Ref>()
test_bdd_slt(&TEST_CONTEXT_CGGI_FFT64_REF)
}
#[test]
fn test_bdd_sltu_fft64_ref() {
test_bdd_sltu::<CGGI, FFT64Ref>()
test_bdd_sltu(&TEST_CONTEXT_CGGI_FFT64_REF)
}
#[test]
fn test_bdd_sra_fft64_ref() {
test_bdd_sra::<CGGI, FFT64Ref>()
test_bdd_sra(&TEST_CONTEXT_CGGI_FFT64_REF)
}
#[test]
fn test_bdd_srl_fft64_ref() {
test_bdd_srl::<CGGI, FFT64Ref>()
test_bdd_srl(&TEST_CONTEXT_CGGI_FFT64_REF)
}
#[test]
fn test_bdd_sub_fft64_ref() {
test_bdd_sub::<CGGI, FFT64Ref>()
test_bdd_sub(&TEST_CONTEXT_CGGI_FFT64_REF)
}
#[test]
fn test_bdd_xor_fft64_ref() {
test_bdd_xor::<CGGI, FFT64Ref>()
test_bdd_xor(&TEST_CONTEXT_CGGI_FFT64_REF)
}

View File

@@ -1,6 +1,6 @@
use poulpy_core::{
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
layouts::{GGSWLayout, GLWELayout, GLWESecretPrepared, GLWESecretPreparedFactory},
};
use poulpy_hal::{
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
@@ -11,14 +11,14 @@ use rand::RngCore;
use crate::tfhe::{
bdd_arithmetic::{
Add, BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
Add, BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
},
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
};
pub fn test_bdd_add<BRA: BlindRotationAlgo, BE: Backend>()
pub fn test_bdd_add<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
where
Module<BE>: ModuleNew<BE>
+ GLWESecretPreparedFactory<BE>
@@ -40,52 +40,53 @@ where
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> = &test_context.module;
let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
let bdd_key_prepared: &BDDKeyPrepared<Vec<u8>, BRA, BE> = &test_context.bdd_key;
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: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<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,
module,
a,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
source.fill_bytes(&mut scratch.borrow().data);
b_enc_prep.encrypt_sk(
&module,
module,
b,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
// d + a
res.add(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
// a + b
res.add(
module,
&a_enc_prep,
&b_enc_prep,
bdd_key_prepared,
scratch.borrow(),
);
assert_eq!(
res.decrypt(&module, &sk_glwe_prep, scratch.borrow()),
res.decrypt(module, sk_glwe_prep, scratch.borrow()),
a.wrapping_add(b)
);
}

View File

@@ -1,6 +1,6 @@
use poulpy_core::{
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
};
use poulpy_hal::{
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
@@ -11,14 +11,14 @@ use rand::RngCore;
use crate::tfhe::{
bdd_arithmetic::{
And, BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
And, BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
},
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
};
pub fn test_bdd_and<BRA: BlindRotationAlgo, BE: Backend>()
pub fn test_bdd_and<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
where
Module<BE>: ModuleNew<BE>
+ GLWESecretPreparedFactory<BE>
@@ -40,48 +40,49 @@ where
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> = &test_context.module;
let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
let bdd_key_prepared: &BDDKeyPrepared<Vec<u8>, BRA, BE> = &test_context.bdd_key;
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: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<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,
module,
a,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
source.fill_bytes(&mut scratch.borrow().data);
b_enc_prep.encrypt_sk(
&module,
module,
b,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
res.and(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
res.and(
module,
&a_enc_prep,
&b_enc_prep,
bdd_key_prepared,
scratch.borrow(),
);
assert_eq!(res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), a & b);
assert_eq!(res.decrypt(module, sk_glwe_prep, scratch.borrow()), a & b);
}

View File

@@ -1,8 +1,8 @@
use poulpy_core::{
GGSWEncryptSk, GGSWNoise, GLWEDecrypt, GLWEEncryptSk, SIGMA, ScratchTakeCore,
layouts::{
Base2K, Degree, Dnum, Dsize, GGSW, GGSWLayout, GGSWPreparedFactory, GLWESecret, GLWESecretPrepared,
GLWESecretPreparedFactory, LWEInfos, Rank, TorusPrecision,
Base2K, Dnum, Dsize, GGSW, GGSWLayout, GGSWPreparedFactory, GLWESecretPrepared, GLWESecretPreparedFactory, LWEInfos,
Rank, TorusPrecision,
},
};
use poulpy_hal::{
@@ -12,9 +12,15 @@ use poulpy_hal::{
};
use rand::RngCore;
use crate::tfhe::bdd_arithmetic::{FheUintPrepared, GGSWBlindRotation};
use crate::tfhe::{
bdd_arithmetic::{
FheUintPrepared, GGSWBlindRotation,
tests::test_suite::{TEST_BASE2K, TEST_RANK, TestContext},
},
blind_rotation::BlindRotationAlgo,
};
pub fn test_scalar_to_ggsw_blind_rotation<BE: Backend>()
pub fn test_scalar_to_ggsw_blind_rotation<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
where
Module<BE>: ModuleNew<BE>
+ GLWESecretPreparedFactory<BE>
@@ -28,14 +34,16 @@ where
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
let n: Degree = Degree(1 << 11);
let base2k: Base2K = Base2K(13);
let rank: Rank = Rank(1);
let module: &Module<BE> = &test_context.module;
let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
let base2k: Base2K = TEST_BASE2K.into();
let rank: Rank = TEST_RANK.into();
let k_ggsw_res: TorusPrecision = TorusPrecision(39);
let k_ggsw_apply: TorusPrecision = TorusPrecision(52);
let ggsw_res_infos: GGSWLayout = GGSWLayout {
n,
n: module.n().into(),
base2k,
k: k_ggsw_res,
rank,
@@ -44,7 +52,7 @@ where
};
let ggsw_k_infos: GGSWLayout = GGSWLayout {
n,
n: module.n().into(),
base2k,
k: k_ggsw_apply,
rank,
@@ -52,24 +60,14 @@ where
dsize: Dsize(1),
};
let n_glwe: usize = 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(n, rank);
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(&module, rank);
sk_glwe_prep.prepare(&module, &sk_glwe);
let mut res: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_res_infos);
let mut scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n_glwe, 1);
let mut scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(module.n(), 1);
scalar
.raw_mut()
.iter_mut()
@@ -80,17 +78,17 @@ where
// println!("k: {k}");
let mut k_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_k_infos);
let mut k_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(module, &ggsw_k_infos);
k_enc_prep.encrypt_sk(
&module,
module,
k,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
let base: [usize; 2] = [6, 5];
let base: [usize; 2] = [module.log_n() >> 1, module.log_n() - (module.log_n() >> 1)];
assert_eq!(base.iter().sum::<usize>(), module.log_n());
@@ -98,7 +96,7 @@ where
let mut bit_start: usize = 0;
let max_noise = |col_i: usize| {
let mut noise: f64 = -(ggsw_res_infos.size() as f64 * base2k.as_usize() as f64) + SIGMA.log2() + 2.0;
let mut noise: f64 = -(ggsw_res_infos.size() as f64 * base2k.as_usize() as f64) + SIGMA.log2() + 3.0;
noise += 0.5 * ggsw_res_infos.log_n() as f64;
if col_i != 0 {
noise += 0.5 * ggsw_res_infos.log_n() as f64
@@ -136,7 +134,7 @@ where
// res.print_noise(&module, &sk_glwe_prep, &scalar_want);
res.assert_noise(&module, &sk_glwe_prep, &scalar_want, &max_noise);
res.assert_noise(module, sk_glwe_prep, &scalar_want, &max_noise);
bit_step += digit;
bit_start += digit;

View File

@@ -1,8 +1,8 @@
use poulpy_core::{
GGSWEncryptSk, GLWEDecrypt, GLWEEncryptSk, ScratchTakeCore,
layouts::{
Base2K, Degree, Dnum, Dsize, GGSWLayout, GGSWPreparedFactory, GLWE, GLWELayout, GLWEPlaintext, GLWESecret,
GLWESecretPrepared, GLWESecretPreparedFactory, LWEInfos, Rank, TorusPrecision,
Base2K, Dnum, Dsize, GGSWLayout, GGSWPreparedFactory, GLWE, GLWELayout, GLWEPlaintext, GLWESecretPrepared,
GLWESecretPreparedFactory, Rank, TorusPrecision,
},
};
use poulpy_hal::{
@@ -12,9 +12,15 @@ use poulpy_hal::{
};
use rand::RngCore;
use crate::tfhe::bdd_arithmetic::{FheUintPrepared, GLWEBlindRotation};
use crate::tfhe::{
bdd_arithmetic::{
FheUintPrepared, GLWEBlindRotation,
tests::test_suite::{TEST_BASE2K, TEST_RANK, TestContext},
},
blind_rotation::BlindRotationAlgo,
};
pub fn test_glwe_to_glwe_blind_rotation<BE: Backend>()
pub fn test_glwe_to_glwe_blind_rotation<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
where
Module<BE>: ModuleNew<BE>
+ GLWESecretPreparedFactory<BE>
@@ -26,21 +32,23 @@ where
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
let n: Degree = Degree(1 << 11);
let base2k: Base2K = Base2K(13);
let rank: Rank = Rank(1);
let module: &Module<BE> = &test_context.module;
let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
let base2k: Base2K = TEST_BASE2K.into();
let rank: Rank = TEST_RANK.into();
let k_glwe: TorusPrecision = TorusPrecision(26);
let k_ggsw: TorusPrecision = TorusPrecision(39);
let dnum: Dnum = Dnum(3);
let glwe_infos: GLWELayout = GLWELayout {
n,
n: module.n().into(),
base2k,
k: k_glwe,
rank,
};
let ggsw_infos: GGSWLayout = GGSWLayout {
n,
n: module.n().into(),
base2k,
k: k_ggsw,
rank,
@@ -48,21 +56,12 @@ where
dsize: Dsize(1),
};
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: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
let mut test_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
@@ -72,17 +71,17 @@ where
let k: u32 = source.next_u32();
let mut k_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut k_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(module, &ggsw_infos);
k_enc_prep.encrypt_sk(
&module,
module,
k,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
let base: [usize; 2] = [6, 5];
let base: [usize; 2] = [module.log_n() >> 1, module.log_n() - (module.log_n() >> 1)];
assert_eq!(base.iter().sum::<usize>(), module.log_n());
@@ -112,7 +111,7 @@ where
scratch.borrow(),
);
res.decrypt(&module, &mut pt, &sk_glwe_prep, scratch.borrow());
res.decrypt(module, &mut pt, sk_glwe_prep, scratch.borrow());
assert_eq!(
(((k >> bit_start) & mask) << bit_step) as i64,

View File

@@ -17,6 +17,11 @@ pub use and::*;
pub use ggsw_blind_rotations::*;
pub use glwe_blind_rotation::*;
pub use or::*;
use poulpy_hal::{
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
layouts::{Backend, Module, Scratch, ScratchOwned},
source::Source,
};
pub use prepare::*;
pub use sll::*;
pub use slt::*;
@@ -26,15 +31,96 @@ pub use srl::*;
pub use sub::*;
pub use xor::*;
use poulpy_core::layouts::{
Base2K, Degree, Dnum, Dsize, GGSWLayout, GLWEAutomorphismKeyLayout, GLWELayout, GLWETensorKeyLayout, GLWEToLWEKeyLayout,
Rank, TorusPrecision,
use poulpy_core::{
ScratchTakeCore,
layouts::{
Base2K, Degree, Dnum, Dsize, GGSWLayout, GLWEAutomorphismKeyLayout, GLWELayout, GLWESecret, GLWESecretPrepared,
GLWESecretPreparedFactory, GLWETensorKeyLayout, GLWEToLWEKeyLayout, LWESecret, Rank, TorusPrecision,
},
};
use crate::tfhe::{
bdd_arithmetic::BDDKeyLayout, blind_rotation::BlindRotationKeyLayout, circuit_bootstrapping::CircuitBootstrappingKeyLayout,
bdd_arithmetic::{BDDKey, BDDKeyEncryptSk, BDDKeyLayout, BDDKeyPrepared, BDDKeyPreparedFactory},
blind_rotation::{
BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory, BlindRotationKeyLayout, BlindRotationKeyPreparedFactory,
},
circuit_bootstrapping::CircuitBootstrappingKeyLayout,
};
pub struct TestContext<BRA: BlindRotationAlgo, BE: Backend> {
pub module: Module<BE>,
pub sk_glwe: GLWESecretPrepared<Vec<u8>, BE>,
pub sk_lwe: LWESecret<Vec<u8>>,
pub bdd_key: BDDKeyPrepared<Vec<u8>, BRA, BE>,
}
impl<BRA: BlindRotationAlgo, BE: Backend> Default for TestContext<BRA, BE>
where
Module<BE>: ModuleNew<BE>
+ BDDKeyEncryptSk<BRA, BE>
+ GLWESecretPreparedFactory<BE>
+ BlindRotationKeyPreparedFactory<BRA, BE>
+ BDDKeyPreparedFactory<BRA, BE>,
BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn default() -> Self {
Self::new()
}
}
impl<BRA: BlindRotationAlgo, BE: Backend> TestContext<BRA, BE> {
pub fn new() -> Self
where
Module<BE>: ModuleNew<BE>
+ BDDKeyEncryptSk<BRA, BE>
+ GLWESecretPreparedFactory<BE>
+ BlindRotationKeyPreparedFactory<BRA, BE>
+ BDDKeyPreparedFactory<BRA, BE>,
BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
let module: Module<BE> = Module::<BE>::new(TEST_N_GLWE as u64);
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(TEST_N_GLWE.into(), TEST_RANK.into());
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(&module, TEST_RANK.into());
sk_glwe_prep.prepare(&module, &sk_glwe);
let n_lwe: u32 = TEST_N_LWE;
let block_size: u32 = TEST_BLOCK_SIZE;
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into());
sk_lwe.fill_binary_block(block_size as usize, &mut source_xs);
let bdd_key_infos: BDDKeyLayout = TEST_BDD_KEY_LAYOUT;
let mut bdd_key: BDDKey<Vec<u8>, BRA> = BDDKey::alloc_from_infos(&bdd_key_infos);
bdd_key.encrypt_sk(
&module,
&sk_lwe,
&sk_glwe,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
let mut bdd_key_prepared: BDDKeyPrepared<Vec<u8>, BRA, BE> = BDDKeyPrepared::alloc_from_infos(&module, &bdd_key_infos);
bdd_key_prepared.prepare(&module, &bdd_key, scratch.borrow());
TestContext {
bdd_key: bdd_key_prepared,
sk_glwe: sk_glwe_prep,
sk_lwe,
module,
}
}
}
pub(crate) const TEST_N_GLWE: u32 = 512;
pub(crate) const TEST_N_LWE: u32 = 77;
pub(crate) const TEST_BASE2K: u32 = 13;

View File

@@ -1,6 +1,6 @@
use poulpy_core::{
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
};
use poulpy_hal::{
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
@@ -11,14 +11,14 @@ use rand::RngCore;
use crate::tfhe::{
bdd_arithmetic::{
BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Or,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Or,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
},
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
};
pub fn test_bdd_or<BRA: BlindRotationAlgo, BE: Backend>()
pub fn test_bdd_or<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
where
Module<BE>: ModuleNew<BE>
+ GLWESecretPreparedFactory<BE>
@@ -40,48 +40,49 @@ where
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> = &test_context.module;
let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
let bdd_key_prepared: &BDDKeyPrepared<Vec<u8>, BRA, BE> = &test_context.bdd_key;
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: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<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,
module,
a,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
source.fill_bytes(&mut scratch.borrow().data);
b_enc_prep.encrypt_sk(
&module,
module,
b,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
res.or(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
res.or(
module,
&a_enc_prep,
&b_enc_prep,
bdd_key_prepared,
scratch.borrow(),
);
assert_eq!(res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), a | b);
assert_eq!(res.decrypt(module, sk_glwe_prep, scratch.borrow()), a | b);
}

View File

@@ -1,6 +1,6 @@
use poulpy_core::{
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, SIGMA, ScratchTakeCore,
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, LWESecret, prepared::GLWESecretPrepared},
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
};
use poulpy_hal::{
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
@@ -11,15 +11,14 @@ use rand::RngCore;
use crate::tfhe::{
bdd_arithmetic::{
BDDKey, BDDKeyEncryptSk, BDDKeyLayout, BDDKeyPreparedFactory, BddKeyPrepared, ExecuteBDDCircuit2WTo1W, FheUint,
FheUintBlockDebugPrepare, FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory,
FheUintPreparedDebug,
tests::test_suite::{TEST_BASE2K, TEST_BDD_KEY_LAYOUT, TEST_BLOCK_SIZE, TEST_GGSW_INFOS, TEST_GLWE_INFOS, TEST_N_LWE},
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPreparedDebug,
tests::test_suite::{TEST_BASE2K, TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
},
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
};
pub fn test_bdd_prepare<BRA: BlindRotationAlgo, BE: Backend>()
pub fn test_bdd_prepare<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
where
Module<BE>: ModuleNew<BE>
+ GLWESecretPreparedFactory<BE>
@@ -41,55 +40,24 @@ where
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> = &test_context.module;
let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
let bdd_key_prepared: &BDDKeyPrepared<Vec<u8>, BRA, BE> = &test_context.bdd_key;
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);
// GLWE Secret
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos);
sk_glwe_prep.prepare(&module, &sk_glwe);
// LWE Secret
let n_lwe: u32 = TEST_N_LWE;
let block_size: u32 = TEST_BLOCK_SIZE;
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into());
sk_lwe.fill_binary_block(block_size as usize, &mut source_xs);
// CBT KEY
let bdd_key_infos: BDDKeyLayout = TEST_BDD_KEY_LAYOUT;
let mut bdd_key: BDDKey<Vec<u8>, BRA> = BDDKey::alloc_from_infos(&bdd_key_infos);
source.fill_bytes(&mut scratch.borrow().data);
scratch.borrow().data.fill(0);
bdd_key.encrypt_sk(
&module,
&sk_lwe,
&sk_glwe,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
let mut bdd_key_prepared: BddKeyPrepared<Vec<u8>, BRA, BE> = BddKeyPrepared::alloc_from_infos(&module, &bdd_key_infos);
source.fill_bytes(&mut scratch.borrow().data);
bdd_key_prepared.prepare(&module, &bdd_key, scratch.borrow());
// GLWE(value)
let mut c_enc: FheUint<Vec<u8>, u32> = FheUint::alloc_from_infos(&module, &glwe_infos);
let mut c_enc: FheUint<Vec<u8>, u32> = FheUint::alloc_from_infos(&glwe_infos);
let value: u32 = source.next_u32();
c_enc.encrypt_sk(
&module,
module,
value,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
@@ -97,13 +65,13 @@ where
// GGSW(0)
let mut c_enc_prep_debug: FheUintPreparedDebug<Vec<u8>, u32> =
FheUintPreparedDebug::<Vec<u8>, u32>::alloc_from_infos(&module, &ggsw_infos);
FheUintPreparedDebug::<Vec<u8>, u32>::alloc_from_infos(module, &ggsw_infos);
// GGSW(value)
c_enc_prep_debug.prepare(&module, &c_enc, &bdd_key_prepared, scratch.borrow());
c_enc_prep_debug.prepare(module, &c_enc, bdd_key_prepared, scratch.borrow());
let max_noise = |col_i: usize| {
let mut noise: f64 = -(ggsw_infos.size() as f64 * TEST_BASE2K as f64) + SIGMA.log2() + 1.0;
let mut noise: f64 = -(ggsw_infos.size() as f64 * TEST_BASE2K as f64) + SIGMA.log2() + 2.0;
noise += 0.5 * ggsw_infos.log_n() as f64;
if col_i != 0 {
noise += 0.5 * ggsw_infos.log_n() as f64
@@ -111,7 +79,7 @@ where
noise
};
// c_enc_prep_debug.print_noise(&module, &sk_glwe_prep, value);
// c_enc_prep_debug.print_noise(module, sk_glwe_prep, value);
c_enc_prep_debug.assert_noise(&module, &sk_glwe_prep, value, &max_noise);
c_enc_prep_debug.assert_noise(module, sk_glwe_prep, value, &max_noise);
}

View File

@@ -1,6 +1,6 @@
use poulpy_core::{
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
};
use poulpy_hal::{
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
@@ -11,14 +11,14 @@ use rand::RngCore;
use crate::tfhe::{
bdd_arithmetic::{
BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Sll,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Sll,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
},
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
};
pub fn test_bdd_sll<BRA: BlindRotationAlgo, BE: Backend>()
pub fn test_bdd_sll<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
where
Module<BE>: ModuleNew<BE>
+ GLWESecretPreparedFactory<BE>
@@ -40,51 +40,52 @@ where
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> = &test_context.module;
let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
let bdd_key_prepared: &BDDKeyPrepared<Vec<u8>, BRA, BE> = &test_context.bdd_key;
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: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<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,
module,
a,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
source.fill_bytes(&mut scratch.borrow().data);
b_enc_prep.encrypt_sk(
&module,
module,
b,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
res.sll(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
res.sll(
module,
&a_enc_prep,
&b_enc_prep,
bdd_key_prepared,
scratch.borrow(),
);
assert_eq!(
res.decrypt(&module, &sk_glwe_prep, scratch.borrow()),
res.decrypt(module, sk_glwe_prep, scratch.borrow()),
a.wrapping_shl(b)
);
}

View File

@@ -1,6 +1,6 @@
use poulpy_core::{
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
};
use poulpy_hal::{
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
@@ -11,14 +11,14 @@ use rand::RngCore;
use crate::tfhe::{
bdd_arithmetic::{
BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Slt,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Slt,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
},
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
};
pub fn test_bdd_slt<BRA: BlindRotationAlgo, BE: Backend>()
pub fn test_bdd_slt<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
where
Module<BE>: ModuleNew<BE>
+ GLWESecretPreparedFactory<BE>
@@ -40,52 +40,53 @@ where
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> = &test_context.module;
let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
let bdd_key_prepared: &BDDKeyPrepared<Vec<u8>, BRA, BE> = &test_context.bdd_key;
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: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<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,
module,
a,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
source.fill_bytes(&mut scratch.borrow().data);
b_enc_prep.encrypt_sk(
&module,
module,
b,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
// d + a
res.slt(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
res.slt(
module,
&a_enc_prep,
&b_enc_prep,
bdd_key_prepared,
scratch.borrow(),
);
assert_eq!(
res.decrypt(&module, &sk_glwe_prep, scratch.borrow()),
res.decrypt(module, sk_glwe_prep, scratch.borrow()),
((a as i32) < (b as i32)) as u32
);
}

View File

@@ -1,6 +1,6 @@
use poulpy_core::{
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
};
use poulpy_hal::{
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
@@ -11,14 +11,14 @@ use rand::RngCore;
use crate::tfhe::{
bdd_arithmetic::{
BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Sltu,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Sltu,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
},
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
};
pub fn test_bdd_sltu<BRA: BlindRotationAlgo, BE: Backend>()
pub fn test_bdd_sltu<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
where
Module<BE>: ModuleNew<BE>
+ GLWESecretPreparedFactory<BE>
@@ -40,52 +40,53 @@ where
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> = &test_context.module;
let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
let bdd_key_prepared: &BDDKeyPrepared<Vec<u8>, BRA, BE> = &test_context.bdd_key;
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: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<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,
module,
a,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
source.fill_bytes(&mut scratch.borrow().data);
b_enc_prep.encrypt_sk(
&module,
module,
b,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
// d + a
res.sltu(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
res.sltu(
module,
&a_enc_prep,
&b_enc_prep,
bdd_key_prepared,
scratch.borrow(),
);
assert_eq!(
res.decrypt(&module, &sk_glwe_prep, scratch.borrow()),
res.decrypt(module, sk_glwe_prep, scratch.borrow()),
(a < b) as u32
);
}

View File

@@ -1,6 +1,6 @@
use poulpy_core::{
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
};
use poulpy_hal::{
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
@@ -11,14 +11,14 @@ use rand::RngCore;
use crate::tfhe::{
bdd_arithmetic::{
BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Sra,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Sra,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
},
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
};
pub fn test_bdd_sra<BRA: BlindRotationAlgo, BE: Backend>()
pub fn test_bdd_sra<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
where
Module<BE>: ModuleNew<BE>
+ GLWESecretPreparedFactory<BE>
@@ -40,51 +40,52 @@ where
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> = &test_context.module;
let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
let bdd_key_prepared: &BDDKeyPrepared<Vec<u8>, BRA, BE> = &test_context.bdd_key;
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: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<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,
module,
a,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
source.fill_bytes(&mut scratch.borrow().data);
b_enc_prep.encrypt_sk(
&module,
module,
b,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
res.sra(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
res.sra(
module,
&a_enc_prep,
&b_enc_prep,
bdd_key_prepared,
scratch.borrow(),
);
assert_eq!(
res.decrypt(&module, &sk_glwe_prep, scratch.borrow()),
res.decrypt(module, sk_glwe_prep, scratch.borrow()),
((a as i32) >> b) as u32
);
}

View File

@@ -1,6 +1,6 @@
use poulpy_core::{
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
};
use poulpy_hal::{
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
@@ -11,14 +11,14 @@ use rand::RngCore;
use crate::tfhe::{
bdd_arithmetic::{
BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Srl,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Srl,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
},
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
};
pub fn test_bdd_srl<BRA: BlindRotationAlgo, BE: Backend>()
pub fn test_bdd_srl<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
where
Module<BE>: ModuleNew<BE>
+ GLWESecretPreparedFactory<BE>
@@ -40,51 +40,49 @@ where
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> = &test_context.module;
let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
let bdd_key_prepared: &BDDKeyPrepared<Vec<u8>, BRA, BE> = &test_context.bdd_key;
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: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<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,
module,
a,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
source.fill_bytes(&mut scratch.borrow().data);
b_enc_prep.encrypt_sk(
&module,
module,
b,
&sk_glwe_prep,
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
res.srl(
module,
&a_enc_prep,
&b_enc_prep,
bdd_key_prepared,
scratch.borrow(),
);
assert_eq!(res.decrypt(module, sk_glwe_prep, scratch.borrow()), a >> b);
}

View File

@@ -1,6 +1,6 @@
use poulpy_core::{
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
};
use poulpy_hal::{
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
@@ -11,14 +11,14 @@ use rand::RngCore;
use crate::tfhe::{
bdd_arithmetic::{
BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Sub,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Sub,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
},
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
};
pub fn test_bdd_sub<BRA: BlindRotationAlgo, BE: Backend>()
pub fn test_bdd_sub<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
where
Module<BE>: ModuleNew<BE>
+ GLWESecretPreparedFactory<BE>
@@ -40,51 +40,52 @@ where
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> = &test_context.module;
let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
let bdd_key_prepared: &BDDKeyPrepared<Vec<u8>, BRA, BE> = &test_context.bdd_key;
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: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<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,
module,
a,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
source.fill_bytes(&mut scratch.borrow().data);
b_enc_prep.encrypt_sk(
&module,
module,
b,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
res.sub(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
res.sub(
module,
&a_enc_prep,
&b_enc_prep,
bdd_key_prepared,
scratch.borrow(),
);
assert_eq!(
res.decrypt(&module, &sk_glwe_prep, scratch.borrow()),
res.decrypt(module, sk_glwe_prep, scratch.borrow()),
a.wrapping_sub(b)
);
}

View File

@@ -1,6 +1,6 @@
use poulpy_core::{
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
};
use poulpy_hal::{
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
@@ -11,14 +11,14 @@ use rand::RngCore;
use crate::tfhe::{
bdd_arithmetic::{
BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Xor,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Xor,
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
},
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
};
pub fn test_bdd_xor<BRA: BlindRotationAlgo, BE: Backend>()
pub fn test_bdd_xor<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
where
Module<BE>: ModuleNew<BE>
+ GLWESecretPreparedFactory<BE>
@@ -40,48 +40,49 @@ where
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> = &test_context.module;
let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
let bdd_key_prepared: &BDDKeyPrepared<Vec<u8>, BRA, BE> = &test_context.bdd_key;
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: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&module, &glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
let mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&glwe_infos);
let mut a_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<Vec<u8>, u32, BE>::alloc(module, &ggsw_infos);
let mut b_enc_prep: FheUintPrepared<Vec<u8>, u32, BE> = FheUintPrepared::<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,
module,
a,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
source.fill_bytes(&mut scratch.borrow().data);
b_enc_prep.encrypt_sk(
&module,
module,
b,
&sk_glwe_prep,
sk_glwe_prep,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
res.xor(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
res.xor(
module,
&a_enc_prep,
&b_enc_prep,
bdd_key_prepared,
scratch.borrow(),
);
assert_eq!(res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), a ^ b);
assert_eq!(res.decrypt(module, sk_glwe_prep, scratch.borrow()), a ^ b);
}