From c761d2cae0e13e81644d70789ef28374ba72b8da Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Wed, 29 Oct 2025 00:59:45 +0100 Subject: [PATCH] update BDD ciphertext types + API for GLWEToLWE --- poulpy-core/src/conversion/glwe_to_lwe.rs | 32 +++- .../src/tests/test_suite/conversion.rs | 16 +- .../src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs | 50 +++-- .../{fhe_uint_compressed.rs => fhe_uint.rs} | 119 ++++++++---- .../ciphertexts/fhe_uint_prepared_debug.rs | 20 +- .../bdd_arithmetic/ciphertexts/fheuint.rs | 176 ------------------ .../tfhe/bdd_arithmetic/ciphertexts/mod.rs | 6 +- poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs | 33 ++-- .../tfhe/bdd_arithmetic/tests/fft64_ref.rs | 36 ++-- .../bdd_arithmetic/tests/test_suite/add.rs | 45 ++--- .../bdd_arithmetic/tests/test_suite/and.rs | 43 ++--- .../tests/test_suite/ggsw_blind_rotations.rs | 50 +++-- .../tests/test_suite/glwe_blind_rotation.rs | 45 +++-- .../bdd_arithmetic/tests/test_suite/mod.rs | 94 +++++++++- .../bdd_arithmetic/tests/test_suite/or.rs | 45 ++--- .../tests/test_suite/prepare.rs | 66 ++----- .../bdd_arithmetic/tests/test_suite/sll.rs | 45 ++--- .../bdd_arithmetic/tests/test_suite/slt.rs | 45 ++--- .../bdd_arithmetic/tests/test_suite/sltu.rs | 45 ++--- .../bdd_arithmetic/tests/test_suite/sra.rs | 45 ++--- .../bdd_arithmetic/tests/test_suite/srl.rs | 48 +++-- .../bdd_arithmetic/tests/test_suite/sub.rs | 45 ++--- .../bdd_arithmetic/tests/test_suite/xor.rs | 45 ++--- 23 files changed, 576 insertions(+), 618 deletions(-) rename poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/{fhe_uint_compressed.rs => fhe_uint.rs} (59%) delete mode 100644 poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fheuint.rs diff --git a/poulpy-core/src/conversion/glwe_to_lwe.rs b/poulpy-core/src/conversion/glwe_to_lwe.rs index fbf5912..96dc65b 100644 --- a/poulpy-core/src/conversion/glwe_to_lwe.rs +++ b/poulpy-core/src/conversion/glwe_to_lwe.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use crate::{ - GLWEKeyswitch, ScratchTakeCore, + GLWEKeyswitch, GLWERotate, ScratchTakeCore, layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank}, }; @@ -37,11 +37,11 @@ where } impl LWESampleExtract for Module where Self: ModuleN {} -impl LWEFromGLWE for Module where Self: GLWEKeyswitch + LWESampleExtract {} +impl LWEFromGLWE for Module where Self: GLWEKeyswitch + LWESampleExtract + GLWERotate {} pub trait LWEFromGLWE where - Self: GLWEKeyswitch + LWESampleExtract, + Self: GLWEKeyswitch + LWESampleExtract + GLWERotate, { fn lwe_from_glwe_tmp_bytes(&self, lwe_infos: &R, glwe_infos: &A, key_infos: &K) -> usize where @@ -61,10 +61,11 @@ where lwe_infos.base2k(), lwe_infos.k(), 1u32.into(), - ) + self.glwe_keyswitch_tmp_bytes(&res_infos, glwe_infos, key_infos) + ) + GLWE::bytes_of_from_infos(glwe_infos) + + self.glwe_keyswitch_tmp_bytes(&res_infos, glwe_infos, key_infos) } - fn lwe_from_glwe(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) + fn lwe_from_glwe(&self, res: &mut R, a: &A, a_idx: usize, key: &K, scratch: &mut Scratch) where R: LWEToMut, A: GLWEToRef, @@ -85,9 +86,20 @@ where rank: Rank(1), }; - let (mut tmp_glwe, scratch_1) = scratch.take_glwe(&glwe_layout); - self.glwe_keyswitch(&mut tmp_glwe, a, key, scratch_1); - self.lwe_sample_extract(res, &tmp_glwe); + let (mut tmp_glwe_rank_1, scratch_1) = scratch.take_glwe(&glwe_layout); + + match a_idx { + 0 => { + self.glwe_keyswitch(&mut tmp_glwe_rank_1, a, key, scratch_1); + } + _ => { + let (mut tmp_glwe_in, scratch_2) = scratch_1.take_glwe(a); + self.glwe_rotate(-(a_idx as i64), &mut tmp_glwe_in, a); + self.glwe_keyswitch(&mut tmp_glwe_rank_1, &tmp_glwe_in, key, scratch_2); + } + } + + self.lwe_sample_extract(res, &tmp_glwe_rank_1); } } @@ -112,13 +124,13 @@ impl LWE { module.lwe_sample_extract(self, a); } - pub fn from_glwe(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) + pub fn from_glwe(&mut self, module: &M, a: &A, a_idx: usize, key: &K, scratch: &mut Scratch) where A: GLWEToRef, K: GGLWEPreparedToRef + GGLWEInfos, M: LWEFromGLWE, Scratch: ScratchTakeCore, { - module.lwe_from_glwe(self, a, key, scratch); + module.lwe_from_glwe(self, a, a_idx, key, scratch); } } diff --git a/poulpy-core/src/tests/test_suite/conversion.rs b/poulpy-core/src/tests/test_suite/conversion.rs index 2412411..4c2fe7f 100644 --- a/poulpy-core/src/tests/test_suite/conversion.rs +++ b/poulpy-core/src/tests/test_suite/conversion.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ }; use crate::{ - GLWEDecrypt, GLWEEncryptSk, GLWEFromLWE, GLWEToLWESwitchingKeyEncryptSk, LWEDecrypt, LWEEncryptSk, + GLWEDecrypt, GLWEEncryptSk, GLWEFromLWE, GLWEToLWESwitchingKeyEncryptSk, LWEDecrypt, LWEEncryptSk, LWEFromGLWE, LWEToGLWESwitchingKeyEncryptSk, ScratchTakeCore, layouts::{ Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWEToLWEKey, @@ -110,6 +110,7 @@ where + GLWEToLWESwitchingKeyEncryptSk + GLWEEncryptSk + LWEDecrypt + + LWEFromGLWE + GLWEDecrypt + GLWESecretPreparedFactory + GLWEToLWESwitchingKeyEncryptSk @@ -163,9 +164,14 @@ where let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe); sk_lwe.fill_ternary_prob(0.5, &mut source_xs); - let data: i64 = 17; + let a_idx: usize = 1; + + let mut data: Vec = vec![0i64; module.n()]; + data[a_idx] = 17; let mut glwe_pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); - glwe_pt.encode_coeff_i64(data, k_lwe_pt, 0); + glwe_pt.encode_vec_i64(&data, k_lwe_pt); + + println!("glwe_pt: {glwe_pt}"); let mut glwe_ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); glwe_ct.encrypt_sk( @@ -193,10 +199,10 @@ where let mut ksk_prepared: GLWEToLWEKeyPrepared, BE> = GLWEToLWEKeyPrepared::alloc_from_infos(module, &ksk); ksk_prepared.prepare(module, &ksk, scratch.borrow()); - lwe_ct.from_glwe(module, &glwe_ct, &ksk_prepared, scratch.borrow()); + lwe_ct.from_glwe(module, &glwe_ct, a_idx, &ksk_prepared, scratch.borrow()); let mut lwe_pt: LWEPlaintext> = LWEPlaintext::alloc_from_infos(&lwe_infos); lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe); - assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]); + assert_eq!(glwe_pt.data.at(0, 0)[a_idx], lwe_pt.data.at(0, 0)[0]); } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs index 3f9628f..3195917 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs @@ -1,36 +1,43 @@ use std::marker::PhantomData; -use poulpy_core::layouts::GGSWPrepared; +use poulpy_core::{GLWECopy, GLWEPacking, ScratchTakeCore, layouts::GGSWPrepared}; use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; -use crate::tfhe::bdd_arithmetic::{ - BitSize, ExecuteBDDCircuit, FheUint, FheUintPrepared, GetBitCircuitInfo, GetGGSWBit, UnsignedInteger, circuits, +use crate::tfhe::{ + bdd_arithmetic::{ + BDDKeyPrepared, BitSize, ExecuteBDDCircuit, FheUint, FheUintPrepared, GetBitCircuitInfo, GetGGSWBit, UnsignedInteger, + circuits, + }, + blind_rotation::BlindRotationAlgo, }; -impl ExecuteBDDCircuit2WTo1W for Module where Self: Sized + ExecuteBDDCircuit {} +impl ExecuteBDDCircuit2WTo1W for Module where + Self: Sized + ExecuteBDDCircuit + GLWEPacking + GLWECopy +{ +} pub trait ExecuteBDDCircuit2WTo1W where - Self: Sized + ExecuteBDDCircuit, + Self: Sized + ExecuteBDDCircuit + GLWEPacking + GLWECopy, { /// Operations Z x Z -> Z - fn execute_bdd_circuit_2w_to_1w( + fn execute_bdd_circuit_2w_to_1w( &self, out: &mut FheUint, circuit: &C, a: &FheUintPrepared, b: &FheUintPrepared, + key: &BDDKeyPrepared, scratch: &mut Scratch, ) where + BRA: BlindRotationAlgo, + DK: DataRef, C: GetBitCircuitInfo, R: DataMut, A: DataRef, B: DataRef, + Scratch: ScratchTakeCore, { - assert_eq!(out.bits.len(), T::WORD_SIZE); - assert_eq!(b.bits.len(), T::WORD_SIZE); - assert_eq!(b.bits.len(), T::WORD_SIZE); - // Collects inputs into a single array let inputs: Vec<&dyn GetGGSWBit> = [a as &dyn GetGGSWBit, b as &dyn GetGGSWBit].to_vec(); let helper: FheUintHelper<'_, T, BE> = FheUintHelper { @@ -38,8 +45,13 @@ where _phantom: PhantomData, }; + let (mut out_bits, scratch_1) = scratch.take_glwe_slice(T::WORD_SIZE, out); + // Evaluates out[i] = circuit[i](a, b) - self.execute_bdd_circuit(&mut out.bits, &helper, circuit, scratch); + self.execute_bdd_circuit(&mut out_bits, &helper, circuit, scratch_1); + + // Repacks the bits + out.pack(self, out_bits, &key.cbt.atk, scratch_1); } } @@ -88,16 +100,20 @@ macro_rules! define_bdd_2w_to_1w_trait { ($(#[$meta:meta])* $vis:vis $trait_name:ident, $method_name:ident) => { $(#[$meta])* $vis trait $trait_name { - fn $method_name( + fn $method_name( &mut self, module: &M, a: &FheUintPrepared, b: &FheUintPrepared, + key: &BDDKeyPrepared, scratch: &mut Scratch, ) where M: ExecuteBDDCircuit2WTo1W, A: DataRef, - B: DataRef; + B: DataRef, + K: DataRef, + BRA: BlindRotationAlgo, + Scratch: ScratchTakeCore; } }; } @@ -106,18 +122,22 @@ macro_rules! define_bdd_2w_to_1w_trait { macro_rules! impl_bdd_2w_to_1w_trait { ($trait_name:ident, $method_name:ident, $ty:ty, $n:literal, $circuit_ty:ty, $output_circuits:path) => { impl $trait_name<$ty, BE> for FheUint { - fn $method_name( + fn $method_name( &mut self, module: &M, a: &FheUintPrepared, b: &FheUintPrepared, + key: &BDDKeyPrepared, scratch: &mut Scratch, ) where M: ExecuteBDDCircuit2WTo1W<$ty, BE>, A: DataRef, B: DataRef, + K: DataRef, + BRA: BlindRotationAlgo, + Scratch: ScratchTakeCore, { - module.execute_bdd_circuit_2w_to_1w(self, &$output_circuits, a, b, scratch) + module.execute_bdd_circuit_2w_to_1w(self, &$output_circuits, a, b, key, scratch) } } }; diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint_compressed.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint.rs similarity index 59% rename from poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint_compressed.rs rename to poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint.rs index d4b180b..cb23e8e 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint_compressed.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint.rs @@ -1,9 +1,9 @@ use itertools::Itertools; use poulpy_core::{ - GLWECopy, GLWEDecrypt, GLWEEncryptSk, GLWEPacking, ScratchTakeCore, + GLWECopy, GLWEDecrypt, GLWEEncryptSk, GLWEPacking, GLWERotate, LWEFromGLWE, ScratchTakeCore, layouts::{ - GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, LWEInfos, TorusPrecision, - prepared::GLWEAutomorphismKeyPrepared, + Base2K, Degree, GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, GLWEToRef, + LWEInfos, LWEToMut, Rank, TorusPrecision, prepared::GLWEAutomorphismKeyPrepared, }, }; use poulpy_hal::{ @@ -15,58 +15,49 @@ use std::{collections::HashMap, marker::PhantomData}; use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger}; -/// An FHE ciphertext encrypting the bits of an [UnsignedInteger] in compressed format (ideal for decryption/serialization). -pub struct FheUintCompressed(pub(crate) GLWE, pub(crate) PhantomData); +/// An FHE ciphertext encrypting the bits of an [UnsignedInteger]. +pub struct FheUint { + pub(crate) bits: GLWE, + pub(crate) _phantom: PhantomData, +} -impl FheUintCompressed { - #[allow(dead_code)] - fn post_process( - &mut self, - module: &M, - mut tmp_res: Vec>, - auto_keys: &HashMap>, - scratch: &mut Scratch, - ) where - ATK: DataRef, - M: GLWEPacking + GLWECopy, - Scratch: ScratchTakeCore, +impl FheUint, T> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GLWEInfos, { - // Repacks the GLWE ciphertexts bits - let gap: usize = module.n() / T::WORD_SIZE; - let log_gap: usize = (usize::BITS - (gap - 1).leading_zeros()) as usize; - let mut cts: HashMap> = HashMap::new(); - for (i, ct) in tmp_res.iter_mut().enumerate().take(T::WORD_SIZE) { - cts.insert(i * gap, ct); + Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank()) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { + Self { + bits: GLWE::alloc(n, base2k, k, rank), + _phantom: PhantomData, } - - module.glwe_pack(&mut cts, log_gap, auto_keys, scratch); - - // And copies the repacked ciphertext on the receiver. - module.glwe_copy(&mut self.0, cts.remove(&0).unwrap()); } } -impl LWEInfos for FheUintCompressed { +impl LWEInfos for FheUint { fn base2k(&self) -> poulpy_core::layouts::Base2K { - self.0.base2k() + self.bits.base2k() } fn k(&self) -> poulpy_core::layouts::TorusPrecision { - self.0.k() + self.bits.k() } fn n(&self) -> poulpy_core::layouts::Degree { - self.0.n() + self.bits.n() } } -impl GLWEInfos for FheUintCompressed { +impl GLWEInfos for FheUint { fn rank(&self) -> poulpy_core::layouts::Rank { - self.0.rank() + self.bits.rank() } } -impl FheUintCompressed { +impl FheUint { pub fn encrypt_sk( &mut self, module: &M, @@ -103,13 +94,13 @@ impl FheUintCompressed { let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos); - pt.encode_vec_i64(&data_bits, TorusPrecision(1)); - self.0 + pt.encode_vec_i64(&data_bits, TorusPrecision(2)); + self.bits .encrypt_sk(module, &pt, sk, source_xa, source_xe, scratch_1); } } -impl FheUintCompressed { +impl FheUint { pub fn decrypt(&self, module: &M, sk: &S, scratch: &mut Scratch) -> T where S: GLWESecretPreparedToRef + GLWEInfos, @@ -133,13 +124,61 @@ impl FheUintCompressed { let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos); - self.0.decrypt(module, &mut pt, sk, scratch_1); + self.bits.decrypt(module, &mut pt, sk, scratch_1); let mut data: Vec = vec![0i64; module.n()]; - pt.decode_vec_i64(&mut data, TorusPrecision(1)); + pt.decode_vec_i64(&mut data, TorusPrecision(2)); let bits: Vec = data.iter().step_by(gap).map(|c| *c as u8).collect_vec(); T::from_bits(&bits) } } + +impl FheUint { + #[allow(dead_code)] + pub(crate) fn pack( + &mut self, + module: &M, + mut tmp_res: Vec>, + auto_keys: &HashMap>, + scratch: &mut Scratch, + ) where + D1: DataMut, + ATK: DataRef, + M: GLWEPacking + GLWECopy, + Scratch: ScratchTakeCore, + { + // Repacks the GLWE ciphertexts bits + let gap: usize = module.n() / T::WORD_SIZE; + let log_gap: usize = (usize::BITS - (gap - 1).leading_zeros()) as usize; + let mut cts: HashMap> = HashMap::new(); + for (i, ct) in tmp_res.iter_mut().enumerate().take(T::WORD_SIZE) { + cts.insert(i * gap, ct); + } + + module.glwe_pack(&mut cts, log_gap, auto_keys, scratch); + + // And copies the repacked ciphertext on the receiver. + module.glwe_copy(&mut self.bits, cts.remove(&0).unwrap()); + } +} + +impl FheUint { + pub fn get_bit(&self, module: &M, bit: usize, res: &mut L, ks: &K, scratch: &mut Scratch) + where + L: LWEToMut, + K: GGLWEPreparedToRef + GGLWEInfos, + M: LWEFromGLWE + GLWERotate, + Scratch: ScratchTakeCore, + { + let gap: usize = module.n() / T::WORD_SIZE; + res.to_mut().from_glwe(module, self, bit * gap, ks, scratch); + } +} + +impl GLWEToRef for FheUint { + fn to_ref(&self) -> GLWE<&[u8]> { + self.bits.to_ref() + } +} diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint_prepared_debug.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint_prepared_debug.rs index 890f2bf..cc21983 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint_prepared_debug.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint_prepared_debug.rs @@ -1,10 +1,8 @@ use std::marker::PhantomData; -use crate::tfhe::bdd_arithmetic::{BddKeyPrepared, FheUintBlockDebugPrepare, ToBits}; +use crate::tfhe::bdd_arithmetic::{BDDKeyPrepared, FheUint, FheUintBlockDebugPrepare, ToBits}; use crate::tfhe::{ - bdd_arithmetic::{FheUint, UnsignedInteger}, - blind_rotation::BlindRotationAlgo, - circuit_bootstrapping::CirtuitBootstrappingExecute, + bdd_arithmetic::UnsignedInteger, blind_rotation::BlindRotationAlgo, circuit_bootstrapping::CirtuitBootstrappingExecute, }; use poulpy_core::GGSWNoise; @@ -113,25 +111,23 @@ impl FheUintPreparedDebug { impl FheUintBlockDebugPrepare for Module where - Self: LWEFromGLWE + CirtuitBootstrappingExecute + GGSWPreparedFactory, + Self: ModuleN + LWEFromGLWE + CirtuitBootstrappingExecute + GGSWPreparedFactory, Scratch: ScratchTakeCore, { fn fhe_uint_debug_prepare( &self, res: &mut FheUintPreparedDebug, bits: &FheUint, - key: &BddKeyPrepared, + key: &BDDKeyPrepared, scratch: &mut Scratch, ) where DM: DataMut, DR0: DataRef, DR1: DataRef, { - assert_eq!(res.bits.len(), bits.bits.len()); - - let mut lwe: LWE> = LWE::alloc_from_infos(&bits.bits[0]); //TODO: add TakeLWE - for (dst, src) in res.bits.iter_mut().zip(bits.bits.iter()) { - lwe.from_glwe(self, src, &key.ks, scratch); + let mut lwe: LWE> = LWE::alloc_from_infos(bits); //TODO: add TakeLWE + for (bit, dst) in res.bits.iter_mut().enumerate() { + bits.get_bit(self, bit, &mut lwe, &key.ks, scratch); key.cbt.execute_to_constant(self, dst, &lwe, 1, 1, scratch); } } @@ -142,7 +138,7 @@ impl FheUintPreparedDebug { &mut self, module: &M, other: &FheUint, - key: &BddKeyPrepared, + key: &BDDKeyPrepared, scratch: &mut Scratch, ) where BRA: BlindRotationAlgo, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fheuint.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fheuint.rs deleted file mode 100644 index 3865818..0000000 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fheuint.rs +++ /dev/null @@ -1,176 +0,0 @@ -use std::marker::PhantomData; - -use poulpy_core::{ - GLWEDecrypt, GLWENoise, - layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, LWEInfos, Rank, TorusPrecision}, -}; - -use poulpy_core::GLWEEncryptSk; -use poulpy_core::ScratchTakeCore; -use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, ZnxZero}; -use poulpy_hal::source::Source; - -use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger}; - -/// An FHE ciphertext encrypting the bits of an [UnsignedInteger]. -pub struct FheUint { - pub(crate) bits: Vec>, - pub(crate) _phantom: PhantomData, -} - -impl LWEInfos for FheUint { - fn base2k(&self) -> poulpy_core::layouts::Base2K { - self.bits[0].base2k() - } - - fn k(&self) -> poulpy_core::layouts::TorusPrecision { - self.bits[0].k() - } - - fn n(&self) -> poulpy_core::layouts::Degree { - self.bits[0].n() - } -} - -impl GLWEInfos for FheUint { - fn rank(&self) -> poulpy_core::layouts::Rank { - self.bits[0].rank() - } -} - -impl FheUint { - pub fn new(bits: Vec>) -> Self { - assert_eq!(bits.len(), T::WORD_SIZE); - Self { - bits, - _phantom: PhantomData, - } - } -} - -impl FheUint, T> { - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self - where - A: GLWEInfos, - { - Self::alloc(module, infos.base2k(), infos.k(), infos.rank()) - } - - pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { - Self { - bits: (0..T::WORD_SIZE) - .map(|_| GLWE::alloc(module.n().into(), base2k, k, rank)) - .collect(), - _phantom: PhantomData, - } - } -} - -impl FheUint { - pub fn encrypt_sk( - &mut self, - module: &Module, - value: T, - sk: &S, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - S: GLWESecretPreparedToRef + GLWEInfos, - Module: GLWEEncryptSk, - Scratch: ScratchTakeCore, - { - use poulpy_core::layouts::GLWEPlaintextLayout; - use poulpy_hal::layouts::ZnxZero; - - #[cfg(debug_assertions)] - { - assert!(module.n().is_multiple_of(T::WORD_SIZE)); - assert_eq!(self.n(), module.n() as u32); - assert_eq!(sk.n(), module.n() as u32); - } - - let pt_infos = GLWEPlaintextLayout { - n: self.n(), - base2k: self.base2k(), - k: 1_usize.into(), - }; - - let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos); - pt.data.zero(); - - for i in 0..T::WORD_SIZE { - pt.encode_coeff_i64(value.bit(i) as i64, TorusPrecision(2), 0); - self.bits[i].encrypt_sk(module, &pt, sk, source_xa, source_xe, scratch_1); - } - } -} - -impl FheUint { - pub fn decrypt(&self, module: &Module, sk: &S, scratch: &mut Scratch) -> T - where - Module: GLWEDecrypt, - S: GLWESecretPreparedToRef + GLWEInfos, - Scratch: ScratchTakeCore, - { - #[cfg(debug_assertions)] - { - assert!(module.n().is_multiple_of(T::WORD_SIZE)); - assert_eq!(self.n(), module.n() as u32); - assert_eq!(sk.n(), module.n() as u32); - } - - let pt_infos = GLWEPlaintextLayout { - n: self.n(), - base2k: self.base2k(), - k: self.k(), - }; - - let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos); - - let mut bits: Vec = vec![0u8; T::WORD_SIZE]; - - let base2k: usize = self.base2k().into(); - let scale: f64 = 4.0 / ((1 << base2k) as f64); - - for (i, bit) in bits.iter_mut().enumerate().take(T::WORD_SIZE) { - self.bits[i].decrypt(module, &mut pt, sk, scratch_1); - let value: i64 = pt.decode_coeff_i64(base2k.into(), 0); - *bit = ((value as f64) * scale).round() as u8; - } - - T::from_bits(&bits) - } - - pub fn noise(&self, module: &Module, sk: &S, want: T, scratch: &mut Scratch) -> Vec - where - Module: GLWENoise, - S: GLWESecretPreparedToRef + GLWEInfos, - Scratch: ScratchTakeCore, - { - #[cfg(debug_assertions)] - { - assert!(module.n().is_multiple_of(T::WORD_SIZE)); - assert_eq!(self.n(), module.n() as u32); - assert_eq!(sk.n(), module.n() as u32); - } - - let pt_infos = GLWEPlaintextLayout { - n: self.n(), - base2k: self.base2k(), - k: 1_usize.into(), - }; - - let (mut pt_want, scratch_1) = scratch.take_glwe_plaintext(&pt_infos); - pt_want.data.zero(); - - let mut noise: Vec = vec![0f64; T::WORD_SIZE]; - - for (i, noise_i) in noise.iter_mut().enumerate().take(T::WORD_SIZE) { - pt_want.encode_coeff_i64(want.bit(i) as i64, TorusPrecision(2), 0); - *noise_i = self.bits[i].noise(module, sk, &pt_want, scratch_1); - } - - noise - } -} diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/mod.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/mod.rs index 22f5043..b3bd18d 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/mod.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/mod.rs @@ -1,11 +1,9 @@ -mod fhe_uint_compressed; +mod fhe_uint; mod fhe_uint_prepared; -mod fheuint; mod fhe_uint_prepared_debug; pub use fhe_uint_prepared_debug::*; -pub use fhe_uint_compressed::*; +pub use fhe_uint::*; pub use fhe_uint_prepared::*; -pub use fheuint::*; diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs index 8ca9659..c11c496 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs @@ -7,6 +7,7 @@ use crate::tfhe::{ CircuitBootstrappingKeyPrepared, CircuitBootstrappingKeyPreparedFactory, CirtuitBootstrappingExecute, }, }; + use poulpy_core::{ GLWEToLWESwitchingKeyEncryptSk, GetDistribution, LWEFromGLWE, ScratchTakeCore, layouts::{ @@ -45,8 +46,8 @@ where D: Data, BRA: BlindRotationAlgo, { - cbt: CircuitBootstrappingKey, - ks: GLWEToLWEKey, + pub(crate) cbt: CircuitBootstrappingKey, + pub(crate) ks: GLWEToLWEKey, } impl BDDKey, BRA> @@ -123,7 +124,7 @@ impl BDDKey { } } -pub struct BddKeyPrepared +pub struct BDDKeyPrepared where D: Data, BRA: BlindRotationAlgo, @@ -137,11 +138,11 @@ pub trait BDDKeyPreparedFactory where Self: Sized + CircuitBootstrappingKeyPreparedFactory + GLWEToLWEKeyPreparedFactory, { - fn alloc_bdd_key_from_infos(&self, infos: &A) -> BddKeyPrepared, BRA, BE> + fn alloc_bdd_key_from_infos(&self, infos: &A) -> BDDKeyPrepared, BRA, BE> where A: BDDKeyInfos, { - BddKeyPrepared { + BDDKeyPrepared { cbt: CircuitBootstrappingKeyPrepared::alloc_from_infos(self, &infos.cbt_infos()), ks: GLWEToLWEKeyPrepared::alloc_from_infos(self, &infos.ks_infos()), } @@ -155,7 +156,7 @@ where .max(self.prepare_glwe_to_lwe_key_tmp_bytes(&infos.ks_infos())) } - fn prepare_bdd_key(&self, res: &mut BddKeyPrepared, other: &BDDKey, scratch: &mut Scratch) + fn prepare_bdd_key(&self, res: &mut BDDKeyPrepared, other: &BDDKey, scratch: &mut Scratch) where DM: DataMut, DR: DataRef, @@ -170,7 +171,7 @@ impl BDDKeyPreparedFactory for Mod { } -impl BddKeyPrepared, BRA, BE> { +impl BDDKeyPrepared, BRA, BE> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where M: BDDKeyPreparedFactory, @@ -180,7 +181,7 @@ impl BddKeyPrepared, BRA, BE> { } } -impl BddKeyPrepared { +impl BDDKeyPrepared { pub fn prepare(&mut self, module: &M, other: &BDDKey, scratch: &mut Scratch) where DR: DataRef, @@ -200,7 +201,7 @@ pub trait FheUintBlocksPrepare, bits: &FheUint, - key: &BddKeyPrepared, + key: &BDDKeyPrepared, scratch: &mut Scratch, ) where DM: DataMut, @@ -230,19 +231,17 @@ where &self, res: &mut FheUintPrepared, bits: &FheUint, - key: &BddKeyPrepared, + key: &BDDKeyPrepared, scratch: &mut Scratch, ) where DM: DataMut, DR0: DataRef, DR1: DataRef, { - assert_eq!(res.bits.len(), bits.bits.len()); - - let mut lwe: LWE> = LWE::alloc_from_infos(&bits.bits[0]); //TODO: add TakeLWE + let mut lwe: LWE> = LWE::alloc_from_infos(bits); //TODO: add TakeLWE let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(res); - for (dst, src) in res.bits.iter_mut().zip(bits.bits.iter()) { - lwe.from_glwe(self, src, &key.ks, scratch_1); + for (bit, dst) in res.bits.iter_mut().enumerate() { + bits.get_bit(self, bit, &mut lwe, &key.ks, scratch_1); key.cbt .execute_to_constant(self, &mut tmp_ggsw, &lwe, 1, 1, scratch_1); dst.prepare(self, &tmp_ggsw, scratch_1); @@ -255,7 +254,7 @@ impl FheUintPrepared { &mut self, module: &M, other: &FheUint, - key: &BddKeyPrepared, + key: &BDDKeyPrepared, scratch: &mut Scratch, ) where BRA: BlindRotationAlgo, @@ -273,7 +272,7 @@ pub trait FheUintBlockDebugPrepare, bits: &FheUint, - key: &BddKeyPrepared, + key: &BDDKeyPrepared, scratch: &mut Scratch, ) where DM: DataMut, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/fft64_ref.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/fft64_ref.rs index b855ffa..bf60d25 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/fft64_ref.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/fft64_ref.rs @@ -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> = + LazyLock::new(|| TestContext::::new()); + #[test] fn test_glwe_to_glwe_blind_rotation_fft64_ref() { - test_glwe_to_glwe_blind_rotation::() + 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::() + test_scalar_to_ggsw_blind_rotation(&TEST_CONTEXT_CGGI_FFT64_REF) } #[test] fn test_bdd_prepare_fft64_ref() { - test_bdd_prepare::() + test_bdd_prepare(&TEST_CONTEXT_CGGI_FFT64_REF) } #[test] fn test_bdd_add_fft64_ref() { - test_bdd_add::() + test_bdd_add(&TEST_CONTEXT_CGGI_FFT64_REF) } #[test] fn test_bdd_and_fft64_ref() { - test_bdd_and::() + test_bdd_and(&TEST_CONTEXT_CGGI_FFT64_REF) } #[test] fn test_bdd_or_fft64_ref() { - test_bdd_or::() + test_bdd_or(&TEST_CONTEXT_CGGI_FFT64_REF) } #[test] fn test_bdd_sll_fft64_ref() { - test_bdd_sll::() + test_bdd_sll(&TEST_CONTEXT_CGGI_FFT64_REF) } #[test] fn test_bdd_slt_fft64_ref() { - test_bdd_slt::() + test_bdd_slt(&TEST_CONTEXT_CGGI_FFT64_REF) } #[test] fn test_bdd_sltu_fft64_ref() { - test_bdd_sltu::() + test_bdd_sltu(&TEST_CONTEXT_CGGI_FFT64_REF) } #[test] fn test_bdd_sra_fft64_ref() { - test_bdd_sra::() + test_bdd_sra(&TEST_CONTEXT_CGGI_FFT64_REF) } #[test] fn test_bdd_srl_fft64_ref() { - test_bdd_srl::() + test_bdd_srl(&TEST_CONTEXT_CGGI_FFT64_REF) } #[test] fn test_bdd_sub_fft64_ref() { - test_bdd_sub::() + test_bdd_sub(&TEST_CONTEXT_CGGI_FFT64_REF) } #[test] fn test_bdd_xor_fft64_ref() { - test_bdd_xor::() + test_bdd_xor(&TEST_CONTEXT_CGGI_FFT64_REF) } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/add.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/add.rs index 1f50766..beff7fd 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/add.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/add.rs @@ -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() +pub fn test_bdd_add(test_context: &TestContext) where Module: ModuleNew + GLWESecretPreparedFactory @@ -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 = &test_context.module; + let sk_glwe_prep: &GLWESecretPrepared, BE> = &test_context.sk_glwe; + let bdd_key_prepared: &BDDKeyPrepared, BRA, BE> = &test_context.bdd_key; - let module: Module = Module::::new(n_glwe as u64); let mut source: Source = Source::new([6u8; 32]); - let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([2u8; 32]); let mut source_xe: Source = Source::new([3u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); - sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); - sk_glwe_prep.prepare(&module, &sk_glwe); - - let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&module, &glwe_infos); - let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); - let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&glwe_infos); + let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(module, &ggsw_infos); + let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, 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) ); } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/and.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/and.rs index aa91e01..5bb8ffc 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/and.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/and.rs @@ -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() +pub fn test_bdd_and(test_context: &TestContext) where Module: ModuleNew + GLWESecretPreparedFactory @@ -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 = &test_context.module; + let sk_glwe_prep: &GLWESecretPrepared, BE> = &test_context.sk_glwe; + let bdd_key_prepared: &BDDKeyPrepared, BRA, BE> = &test_context.bdd_key; - let module: Module = Module::::new(n_glwe as u64); let mut source: Source = Source::new([6u8; 32]); - let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([2u8; 32]); let mut source_xe: Source = Source::new([3u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); - sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); - sk_glwe_prep.prepare(&module, &sk_glwe); - - let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&module, &glwe_infos); - let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); - let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&glwe_infos); + let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(module, &ggsw_infos); + let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, 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); } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/ggsw_blind_rotations.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/ggsw_blind_rotations.rs index 45f9197..5f7ae02 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/ggsw_blind_rotations.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/ggsw_blind_rotations.rs @@ -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() +pub fn test_scalar_to_ggsw_blind_rotation(test_context: &TestContext) where Module: ModuleNew + GLWESecretPreparedFactory @@ -28,14 +34,16 @@ where ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, { - let n: Degree = Degree(1 << 11); - let base2k: Base2K = Base2K(13); - let rank: Rank = Rank(1); + let module: &Module = &test_context.module; + let sk_glwe_prep: &GLWESecretPrepared, 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 = Module::::new(n_glwe as u64); let mut source: Source = Source::new([6u8; 32]); - let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([2u8; 32]); let mut source_xe: Source = Source::new([3u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - - let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n, rank); - sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(&module, rank); - sk_glwe_prep.prepare(&module, &sk_glwe); - let mut res: GGSW> = GGSW::alloc_from_infos(&ggsw_res_infos); - let mut scalar: ScalarZnx> = ScalarZnx::alloc(n_glwe, 1); + let mut scalar: ScalarZnx> = ScalarZnx::alloc(module.n(), 1); scalar .raw_mut() .iter_mut() @@ -80,17 +78,17 @@ where // println!("k: {k}"); - let mut k_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_k_infos); + let mut k_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, 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::(), 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; diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/glwe_blind_rotation.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/glwe_blind_rotation.rs index 78a03de..c7d503f 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/glwe_blind_rotation.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/glwe_blind_rotation.rs @@ -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() +pub fn test_glwe_to_glwe_blind_rotation(test_context: &TestContext) where Module: ModuleNew + GLWESecretPreparedFactory @@ -26,21 +32,23 @@ where ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, { - let n: Degree = Degree(1 << 11); - let base2k: Base2K = Base2K(13); - let rank: Rank = Rank(1); + let module: &Module = &test_context.module; + let sk_glwe_prep: &GLWESecretPrepared, 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 = Module::::new(n_glwe as u64); let mut source: Source = Source::new([6u8; 32]); - let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([2u8; 32]); let mut source_xe: Source = Source::new([3u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); - sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); - sk_glwe_prep.prepare(&module, &sk_glwe); - let mut res: GLWE> = GLWE::alloc_from_infos(&glwe_infos); let mut test_glwe: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); @@ -72,17 +71,17 @@ where let k: u32 = source.next_u32(); - let mut k_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut k_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, 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::(), 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, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/mod.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/mod.rs index 1b2c645..30e5b8f 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/mod.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/mod.rs @@ -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 { + pub module: Module, + pub sk_glwe: GLWESecretPrepared, BE>, + pub sk_lwe: LWESecret>, + pub bdd_key: BDDKeyPrepared, BRA, BE>, +} + +impl Default for TestContext +where + Module: ModuleNew + + BDDKeyEncryptSk + + GLWESecretPreparedFactory + + BlindRotationKeyPreparedFactory + + BDDKeyPreparedFactory, + BlindRotationKey, BRA>: BlindRotationKeyFactory, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, +{ + fn default() -> Self { + Self::new() + } +} + +impl TestContext { + pub fn new() -> Self + where + Module: ModuleNew + + BDDKeyEncryptSk + + GLWESecretPreparedFactory + + BlindRotationKeyPreparedFactory + + BDDKeyPreparedFactory, + BlindRotationKey, BRA>: BlindRotationKeyFactory, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, + { + let module: Module = Module::::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 = ScratchOwned::alloc(1 << 22); + + let mut sk_glwe: GLWESecret> = 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, 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> = 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, 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, 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; diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/or.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/or.rs index a13b8cb..e76cc2f 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/or.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/or.rs @@ -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() +pub fn test_bdd_or(test_context: &TestContext) where Module: ModuleNew + GLWESecretPreparedFactory @@ -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 = &test_context.module; + let sk_glwe_prep: &GLWESecretPrepared, BE> = &test_context.sk_glwe; + let bdd_key_prepared: &BDDKeyPrepared, BRA, BE> = &test_context.bdd_key; - let module: Module = Module::::new(n_glwe as u64); let mut source: Source = Source::new([6u8; 32]); - let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([2u8; 32]); let mut source_xe: Source = Source::new([3u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); - sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); - sk_glwe_prep.prepare(&module, &sk_glwe); - - let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&module, &glwe_infos); - let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); - let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&glwe_infos); + let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(module, &ggsw_infos); + let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, 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); } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/prepare.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/prepare.rs index 8cdf932..acc82de 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/prepare.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/prepare.rs @@ -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() +pub fn test_bdd_prepare(test_context: &TestContext) where Module: ModuleNew + GLWESecretPreparedFactory @@ -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 = &test_context.module; + let sk_glwe_prep: &GLWESecretPrepared, BE> = &test_context.sk_glwe; + let bdd_key_prepared: &BDDKeyPrepared, BRA, BE> = &test_context.bdd_key; - let module: Module = Module::::new(n_glwe as u64); let mut source: Source = Source::new([6u8; 32]); - let mut source_xs: Source = Source::new([1u8; 32]); + let mut source_xa: Source = Source::new([2u8; 32]); let mut source_xe: Source = Source::new([3u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - // GLWE Secret - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); - sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); - sk_glwe_prep.prepare(&module, &sk_glwe); - - // LWE Secret - let n_lwe: u32 = TEST_N_LWE; - let block_size: u32 = TEST_BLOCK_SIZE; - let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe.into()); - sk_lwe.fill_binary_block(block_size as usize, &mut source_xs); - - // CBT KEY - let bdd_key_infos: BDDKeyLayout = TEST_BDD_KEY_LAYOUT; - - let mut bdd_key: BDDKey, BRA> = BDDKey::alloc_from_infos(&bdd_key_infos); - - 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, 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, u32> = FheUint::alloc_from_infos(&module, &glwe_infos); + let mut c_enc: FheUint, 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, u32> = - FheUintPreparedDebug::, u32>::alloc_from_infos(&module, &ggsw_infos); + FheUintPreparedDebug::, 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); } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sll.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sll.rs index f0bc716..951d55c 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sll.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sll.rs @@ -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() +pub fn test_bdd_sll(test_context: &TestContext) where Module: ModuleNew + GLWESecretPreparedFactory @@ -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 = &test_context.module; + let sk_glwe_prep: &GLWESecretPrepared, BE> = &test_context.sk_glwe; + let bdd_key_prepared: &BDDKeyPrepared, BRA, BE> = &test_context.bdd_key; - let module: Module = Module::::new(n_glwe as u64); let mut source: Source = Source::new([6u8; 32]); - let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([2u8; 32]); let mut source_xe: Source = Source::new([3u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); - sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); - sk_glwe_prep.prepare(&module, &sk_glwe); - - let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&module, &glwe_infos); - let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); - let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&glwe_infos); + let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(module, &ggsw_infos); + let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, 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) ); } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/slt.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/slt.rs index 3512bd6..3abcb26 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/slt.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/slt.rs @@ -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() +pub fn test_bdd_slt(test_context: &TestContext) where Module: ModuleNew + GLWESecretPreparedFactory @@ -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 = &test_context.module; + let sk_glwe_prep: &GLWESecretPrepared, BE> = &test_context.sk_glwe; + let bdd_key_prepared: &BDDKeyPrepared, BRA, BE> = &test_context.bdd_key; - let module: Module = Module::::new(n_glwe as u64); let mut source: Source = Source::new([6u8; 32]); - let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([2u8; 32]); let mut source_xe: Source = Source::new([3u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); - sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); - sk_glwe_prep.prepare(&module, &sk_glwe); - - let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&module, &glwe_infos); - let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); - let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&glwe_infos); + let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(module, &ggsw_infos); + let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, 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 ); } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sltu.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sltu.rs index 9f24d7d..b7e7cf3 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sltu.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sltu.rs @@ -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() +pub fn test_bdd_sltu(test_context: &TestContext) where Module: ModuleNew + GLWESecretPreparedFactory @@ -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 = &test_context.module; + let sk_glwe_prep: &GLWESecretPrepared, BE> = &test_context.sk_glwe; + let bdd_key_prepared: &BDDKeyPrepared, BRA, BE> = &test_context.bdd_key; - let module: Module = Module::::new(n_glwe as u64); let mut source: Source = Source::new([6u8; 32]); - let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([2u8; 32]); let mut source_xe: Source = Source::new([3u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); - sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); - sk_glwe_prep.prepare(&module, &sk_glwe); - - let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&module, &glwe_infos); - let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); - let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&glwe_infos); + let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(module, &ggsw_infos); + let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, 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 ); } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sra.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sra.rs index ee435eb..6d50bec 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sra.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sra.rs @@ -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() +pub fn test_bdd_sra(test_context: &TestContext) where Module: ModuleNew + GLWESecretPreparedFactory @@ -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 = &test_context.module; + let sk_glwe_prep: &GLWESecretPrepared, BE> = &test_context.sk_glwe; + let bdd_key_prepared: &BDDKeyPrepared, BRA, BE> = &test_context.bdd_key; - let module: Module = Module::::new(n_glwe as u64); let mut source: Source = Source::new([6u8; 32]); - let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([2u8; 32]); let mut source_xe: Source = Source::new([3u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); - sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); - sk_glwe_prep.prepare(&module, &sk_glwe); - - let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&module, &glwe_infos); - let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); - let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&glwe_infos); + let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(module, &ggsw_infos); + let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, 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 ); } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/srl.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/srl.rs index 9d50aad..0ddcad8 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/srl.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/srl.rs @@ -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() +pub fn test_bdd_srl(test_context: &TestContext) where Module: ModuleNew + GLWESecretPreparedFactory @@ -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 = &test_context.module; + let sk_glwe_prep: &GLWESecretPrepared, BE> = &test_context.sk_glwe; + let bdd_key_prepared: &BDDKeyPrepared, BRA, BE> = &test_context.bdd_key; - let module: Module = Module::::new(n_glwe as u64); let mut source: Source = Source::new([6u8; 32]); - let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([2u8; 32]); let mut source_xe: Source = Source::new([3u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); - sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); - sk_glwe_prep.prepare(&module, &sk_glwe); - - let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&module, &glwe_infos); - let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); - let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&glwe_infos); + let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(module, &ggsw_infos); + let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, 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); } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sub.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sub.rs index 56dc5e9..1b96d9f 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sub.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/sub.rs @@ -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() +pub fn test_bdd_sub(test_context: &TestContext) where Module: ModuleNew + GLWESecretPreparedFactory @@ -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 = &test_context.module; + let sk_glwe_prep: &GLWESecretPrepared, BE> = &test_context.sk_glwe; + let bdd_key_prepared: &BDDKeyPrepared, BRA, BE> = &test_context.bdd_key; - let module: Module = Module::::new(n_glwe as u64); let mut source: Source = Source::new([6u8; 32]); - let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([2u8; 32]); let mut source_xe: Source = Source::new([3u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); - sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); - sk_glwe_prep.prepare(&module, &sk_glwe); - - let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&module, &glwe_infos); - let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); - let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&glwe_infos); + let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(module, &ggsw_infos); + let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, 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) ); } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/xor.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/xor.rs index 5c99736..82a0854 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/xor.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/xor.rs @@ -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() +pub fn test_bdd_xor(test_context: &TestContext) where Module: ModuleNew + GLWESecretPreparedFactory @@ -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 = &test_context.module; + let sk_glwe_prep: &GLWESecretPrepared, BE> = &test_context.sk_glwe; + let bdd_key_prepared: &BDDKeyPrepared, BRA, BE> = &test_context.bdd_key; - let module: Module = Module::::new(n_glwe as u64); let mut source: Source = Source::new([6u8; 32]); - let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([2u8; 32]); let mut source_xe: Source = Source::new([3u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); - sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); - sk_glwe_prep.prepare(&module, &sk_glwe); - - let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&module, &glwe_infos); - let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); - let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut res: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&glwe_infos); + let mut a_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, u32, BE>::alloc(module, &ggsw_infos); + let mut b_enc_prep: FheUintPrepared, u32, BE> = FheUintPrepared::, 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); }