From 6cf571c0b0dc62e28ad5bb5cfe4e7c8ab6202ab8 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Wed, 5 Nov 2025 09:23:13 +0100 Subject: [PATCH] add sext for fheuint --- poulpy-core/src/glwe_packing.rs | 9 +- poulpy-core/src/glwe_trace.rs | 43 +++----- poulpy-core/src/tests/test_suite/trace.rs | 3 +- .../bdd_arithmetic/ciphertexts/fhe_uint.rs | 54 ++++++++--- .../ciphertexts/fhe_uint_prepared.rs | 97 +++++++++++++++++-- .../ciphertexts/fhe_uint_prepared_debug.rs | 6 +- poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs | 97 ++----------------- .../tfhe/bdd_arithmetic/tests/fft64_ref.rs | 10 +- .../bdd_arithmetic/tests/test_suite/add.rs | 6 +- .../bdd_arithmetic/tests/test_suite/and.rs | 6 +- .../tests/test_suite/fheuint.rs | 45 +++++++++ .../bdd_arithmetic/tests/test_suite/or.rs | 6 +- .../tests/test_suite/prepare.rs | 6 +- .../bdd_arithmetic/tests/test_suite/sll.rs | 6 +- .../bdd_arithmetic/tests/test_suite/slt.rs | 6 +- .../bdd_arithmetic/tests/test_suite/sltu.rs | 6 +- .../bdd_arithmetic/tests/test_suite/sra.rs | 6 +- .../bdd_arithmetic/tests/test_suite/srl.rs | 6 +- .../bdd_arithmetic/tests/test_suite/sub.rs | 6 +- .../bdd_arithmetic/tests/test_suite/xor.rs | 6 +- .../src/tfhe/circuit_bootstrapping/circuit.rs | 5 +- 21 files changed, 243 insertions(+), 192 deletions(-) diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index ecfcb92..0e2f71a 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -83,14 +83,7 @@ where } } - self.glwe_trace( - res, - log_n - log_gap_out, - log_n, - *a.get(&0).unwrap(), - keys, - scratch, - ); + self.glwe_trace(res, log_n - log_gap_out, *a.get(&0).unwrap(), keys, scratch); } } diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index 1240295..2142301 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -31,38 +31,25 @@ impl GLWE> { } impl GLWE { - pub fn trace( - &mut self, - module: &M, - start: usize, - end: usize, - a: &A, - keys: &H, - scratch: &mut Scratch, - ) where + pub fn trace(&mut self, module: &M, skip: usize, a: &A, keys: &H, scratch: &mut Scratch) + where A: GLWEToRef, K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, H: GLWEAutomorphismKeyHelper, Scratch: ScratchTakeCore, M: GLWETrace, { - module.glwe_trace(self, start, end, a, keys, scratch); + module.glwe_trace(self, skip, a, keys, scratch); } - pub fn trace_inplace( - &mut self, - module: &M, - start: usize, - end: usize, - keys: &H, - scratch: &mut Scratch, - ) where + pub fn trace_inplace(&mut self, module: &M, skip: usize, keys: &H, scratch: &mut Scratch) + where K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, H: GLWEAutomorphismKeyHelper, Scratch: ScratchTakeCore, M: GLWETrace, { - module.glwe_trace_inplace(self, start, end, keys, scratch); + module.glwe_trace_inplace(self, skip, keys, scratch); } } @@ -114,7 +101,7 @@ where trace } - fn glwe_trace(&self, res: &mut R, start: usize, end: usize, a: &A, keys: &H, scratch: &mut Scratch) + fn glwe_trace(&self, res: &mut R, skip: usize, a: &A, keys: &H, scratch: &mut Scratch) where R: GLWEToMut, A: GLWEToRef, @@ -122,10 +109,10 @@ where H: GLWEAutomorphismKeyHelper, { self.glwe_copy(res, a); - self.glwe_trace_inplace(res, start, end, keys, scratch); + self.glwe_trace_inplace(res, skip, keys, scratch); } - fn glwe_trace_inplace(&self, res: &mut R, start: usize, end: usize, keys: &H, scratch: &mut Scratch) + fn glwe_trace_inplace(&self, res: &mut R, skip: usize, keys: &H, scratch: &mut Scratch) where R: GLWEToMut, K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, @@ -134,11 +121,11 @@ where let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let ksk_infos: &GGLWELayout = &keys.automorphism_key_infos(); + let log_n: usize = self.log_n(); assert_eq!(res.n(), self.n() as u32); assert_eq!(ksk_infos.n(), self.n() as u32); - assert!(start < end); - assert!(end <= self.log_n()); + assert!(skip <= log_n); assert_eq!(ksk_infos.rank_in(), res.rank()); assert_eq!(ksk_infos.rank_out(), res.rank()); @@ -162,7 +149,7 @@ where ); } - for i in start..end { + for i in skip..log_n { self.glwe_rsh(1, &mut self_conv, scratch_1); let p: i64 = if i == 0 { @@ -192,7 +179,7 @@ where } else { // println!("res: {}", res); - for i in start..end { + for i in skip..log_n { self.glwe_rsh(1, res, scratch); let p: i64 = if i == 0 { @@ -220,14 +207,14 @@ pub trait GLWETrace { A: GLWEInfos, K: GGLWEInfos; - fn glwe_trace(&self, res: &mut R, start: usize, end: usize, a: &A, keys: &H, scratch: &mut Scratch) + fn glwe_trace(&self, res: &mut R, skip: usize, a: &A, keys: &H, scratch: &mut Scratch) where R: GLWEToMut, A: GLWEToRef, K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, H: GLWEAutomorphismKeyHelper; - fn glwe_trace_inplace(&self, res: &mut R, start: usize, end: usize, keys: &H, scratch: &mut Scratch) + fn glwe_trace_inplace(&self, res: &mut R, skip: usize, keys: &H, scratch: &mut Scratch) where R: GLWEToMut, K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, diff --git a/poulpy-core/src/tests/test_suite/trace.rs b/poulpy-core/src/tests/test_suite/trace.rs index ed2ed79..7c3c653 100644 --- a/poulpy-core/src/tests/test_suite/trace.rs +++ b/poulpy-core/src/tests/test_suite/trace.rs @@ -114,8 +114,7 @@ where auto_keys.insert(*gal_el, atk_prepared); }); - glwe_out.trace_inplace(module, 0, 5, &auto_keys, scratch.borrow()); - glwe_out.trace_inplace(module, 5, module.log_n(), &auto_keys, scratch.borrow()); + glwe_out.trace_inplace(module, 0, &auto_keys, scratch.borrow()); (0..pt_want.size()).for_each(|i| pt_want.data.at_mut(0, i)[0] = pt_have.data.at(0, i)[0]); diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint.rs index 789ff69..104a8d8 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint.rs @@ -239,14 +239,7 @@ impl FheUint { // Stores this byte (everything else zeroed) into tmp_trace let (mut tmp_trace, scratch_1) = scratch.take_glwe(a); - module.glwe_trace( - &mut tmp_trace, - trace_start, - module.log_n(), - self, - keys, - scratch_1, - ); + module.glwe_trace(&mut tmp_trace, trace_start, self, keys, scratch_1); // Subtracts to self to zero it module.glwe_sub_inplace(&mut self.bits, &tmp_trace); @@ -262,13 +255,7 @@ impl FheUint { ); // Zeroes all other bytes - module.glwe_trace_inplace( - &mut tmp_fhe_uint_byte, - trace_start, - module.log_n(), - keys, - scratch_1, - ); + module.glwe_trace_inplace(&mut tmp_fhe_uint_byte, trace_start, keys, scratch_1); // Add self[0] += a[0] module.glwe_add_inplace(&mut self.bits, &tmp_fhe_uint_byte); @@ -324,3 +311,40 @@ impl GLWEToRef for FheUint { self.bits.to_ref() } } + +impl FheUint { + pub fn sext(&mut self, module: &M, byte: usize, keys: &H, scratch: &mut Scratch) + where + M:, + H: GLWEAutomorphismKeyHelper, + K: GGLWEPreparedToRef + GGLWEInfos + GetGaloisElement, + BE: Backend, + M: ModuleLogN + GLWERotate + GLWETrace + GLWEAdd + GLWESub + GLWECopy, + Scratch: ScratchTakeCore, + { + assert!(byte < (1 << T::LOG_BYTES)); + + let log_gap: usize = module.log_n() - T::LOG_BITS as usize; + let rot: i64 = (T::bit_index(byte << 3) << log_gap) as i64; + + let (mut sext, scratch_1) = scratch.take_glwe(self); + + // Extract MSB + module.glwe_rotate(-rot, &mut sext, &self.bits); + module.glwe_trace_inplace(&mut sext, 0, keys, scratch_1); + + // Replicates MSB in byte + for i in 0..3 { + let (mut tmp, _) = scratch_1.take_glwe(self); + module.glwe_rotate(((1 << T::LOG_BYTES) << log_gap) << i, &mut tmp, &sext); + module.glwe_add_inplace(&mut sext, &tmp); + } + + // Splice sext + let (mut tmp, scratch_2) = scratch_1.take_glwe(self); + for i in byte..(1 << T::LOG_BYTES) as usize { + FheUint::<&mut [u8], T>::from_glwe_to_mut(&mut tmp).splice_u8(module, i, 0, &self.bits, &sext, keys, scratch_2); + module.glwe_copy(&mut self.bits, &tmp); + } + } +} diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint_prepared.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint_prepared.rs index f8d0ab5..3ea8423 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint_prepared.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint_prepared.rs @@ -1,9 +1,10 @@ use std::marker::PhantomData; +use poulpy_core::LWEFromGLWE; use poulpy_core::layouts::{ Base2K, Dnum, Dsize, GGSWInfos, GGSWPreparedFactory, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared, }; -use poulpy_core::layouts::{GGSWPreparedToMut, GGSWPreparedToRef}; +use poulpy_core::layouts::{GGSWPreparedToMut, GGSWPreparedToRef, LWE}; use poulpy_core::{GGSWEncryptSk, ScratchTakeCore, layouts::GLWESecretPreparedToRef}; use poulpy_hal::layouts::{Backend, Data, DataRef, Module}; @@ -14,8 +15,10 @@ use poulpy_hal::{ source::Source, }; -use crate::tfhe::bdd_arithmetic::ToBits; use crate::tfhe::bdd_arithmetic::UnsignedInteger; +use crate::tfhe::bdd_arithmetic::{BDDKey, BDDKeyHelper, BDDKeyInfos, BDDKeyPrepared, BDDKeyPreparedFactory, FheUint, ToBits}; +use crate::tfhe::blind_rotation::BlindRotationAlgo; +use crate::tfhe::circuit_bootstrapping::CirtuitBootstrappingExecute; /// A prepared FHE ciphertext encrypting the bits of an [UnsignedInteger]. pub struct FheUintPrepared { @@ -23,10 +26,7 @@ pub struct FheUintPrepared { pub(crate) _phantom: PhantomData, } -impl FheUintPreparedFactory for Module where - Self: Sized + GGSWPreparedFactory -{ -} +impl FheUintPreparedFactory for Module where Self: Sized + GGSWPreparedFactory {} pub trait GetGGSWBit { fn get_bit(&self, bit: usize) -> GGSWPrepared<&[u8], BE>; @@ -189,3 +189,88 @@ impl GGSWInfos for FheUintPrepared BDDKeyPrepared { + pub fn prepare(&mut self, module: &M, other: &BDDKey, scratch: &mut Scratch) + where + DR: DataRef, + M: BDDKeyPreparedFactory, + Scratch: ScratchTakeCore, + { + module.prepare_bdd_key(self, other, scratch); + } +} + +pub trait FheUintPrepare { + fn fhe_uint_prepare_tmp_bytes(&self, block_size: usize, extension_factor: usize, res_infos: &R, infos: &A) -> usize + where + R: GGSWInfos, + A: BDDKeyInfos; + fn fhe_uint_prepare( + &self, + res: &mut FheUintPrepared, + bits: &FheUint, + key: &K, + scratch: &mut Scratch, + ) where + DM: DataMut, + DB: DataRef, + DK: DataRef, + K: BDDKeyHelper; +} + +impl FheUintPrepare for Module +where + Self: LWEFromGLWE + CirtuitBootstrappingExecute + GGSWPreparedFactory, + Scratch: ScratchTakeCore, +{ + fn fhe_uint_prepare_tmp_bytes(&self, block_size: usize, extension_factor: usize, res_infos: &R, bdd_infos: &A) -> usize + where + R: GGSWInfos, + A: BDDKeyInfos, + { + self.circuit_bootstrapping_execute_tmp_bytes( + block_size, + extension_factor, + res_infos, + &bdd_infos.cbt_infos(), + ) + } + + fn fhe_uint_prepare( + &self, + res: &mut FheUintPrepared, + bits: &FheUint, + key: &K, + scratch: &mut Scratch, + ) where + DM: DataMut, + DB: DataRef, + DK: DataRef, + K: BDDKeyHelper, + { + let (cbt, ks) = key.get_cbt_key(); + + let mut lwe: LWE> = LWE::alloc_from_infos(bits); //TODO: add TakeLWE + let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(res); + for (bit, dst) in res.bits.iter_mut().enumerate() { + bits.get_bit(self, bit, &mut lwe, ks, scratch_1); + cbt.execute_to_constant(self, &mut tmp_ggsw, &lwe, 1, 1, scratch_1); + dst.prepare(self, &tmp_ggsw, scratch_1); + } + } +} + +impl FheUintPrepared { + pub fn prepare(&mut self, module: &M, other: &FheUint, key: &K, scratch: &mut Scratch) + where + BRA: BlindRotationAlgo, + O: DataRef, + DK: DataRef, + K: BDDKeyHelper, + M: FheUintPrepare, + Scratch: ScratchTakeCore, + { + module.fhe_uint_prepare(self, other, key, scratch); + } +} 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 6f130d9..22fd2b6 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,6 +1,6 @@ use std::marker::PhantomData; -use crate::tfhe::bdd_arithmetic::{BDDKeyPrepared, FheUint, FheUintBlockDebugPrepare, ToBits}; +use crate::tfhe::bdd_arithmetic::{BDDKeyPrepared, FheUint, FheUintPrepareDebug, ToBits}; use crate::tfhe::{ bdd_arithmetic::UnsignedInteger, blind_rotation::BlindRotationAlgo, circuit_bootstrapping::CirtuitBootstrappingExecute, }; @@ -109,7 +109,7 @@ impl FheUintPreparedDebug { } } -impl FheUintBlockDebugPrepare for Module +impl FheUintPrepareDebug for Module where Self: ModuleN + LWEFromGLWE + CirtuitBootstrappingExecute + GGSWPreparedFactory, Scratch: ScratchTakeCore, @@ -144,7 +144,7 @@ impl FheUintPreparedDebug { BRA: BlindRotationAlgo, O: DataRef, K: DataRef, - M: FheUintBlockDebugPrepare, + M: FheUintPrepareDebug, Scratch: ScratchTakeCore, { module.fhe_uint_debug_prepare(self, other, key, scratch); diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs index 509b19f..f636fb8 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs @@ -1,19 +1,19 @@ use crate::tfhe::bdd_arithmetic::FheUintPreparedDebug; use crate::tfhe::{ - bdd_arithmetic::{FheUint, FheUintPrepared, UnsignedInteger}, + bdd_arithmetic::{FheUint, UnsignedInteger}, blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, circuit_bootstrapping::{ CircuitBootstrappingKey, CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyLayout, - CircuitBootstrappingKeyPrepared, CircuitBootstrappingKeyPreparedFactory, CirtuitBootstrappingExecute, + CircuitBootstrappingKeyPrepared, CircuitBootstrappingKeyPreparedFactory, }, }; use poulpy_core::layouts::{GLWEAutomorphismKeyHelper, GLWEAutomorphismKeyPrepared}; use poulpy_core::{ - GLWEToLWESwitchingKeyEncryptSk, GetDistribution, LWEFromGLWE, ScratchTakeCore, + GLWEToLWESwitchingKeyEncryptSk, GetDistribution, ScratchTakeCore, layouts::{ - GGSWInfos, GGSWPreparedFactory, GLWEInfos, GLWESecretToRef, GLWEToLWEKey, GLWEToLWEKeyLayout, - GLWEToLWEKeyPreparedFactory, LWE, LWEInfos, LWESecretToRef, prepared::GLWEToLWEKeyPrepared, + GLWEInfos, GLWESecretToRef, GLWEToLWEKey, GLWEToLWEKeyLayout, GLWEToLWEKeyPreparedFactory, LWEInfos, LWESecretToRef, + prepared::GLWEToLWEKeyPrepared, }, }; use poulpy_hal::{ @@ -194,77 +194,6 @@ impl BDDKeyPrepared, BRA, BE> { } } -impl BDDKeyPrepared { - pub fn prepare(&mut self, module: &M, other: &BDDKey, scratch: &mut Scratch) - where - DR: DataRef, - M: BDDKeyPreparedFactory, - Scratch: ScratchTakeCore, - { - module.prepare_bdd_key(self, other, scratch); - } -} - -pub trait FheUintPrepare { - fn fhe_uint_prepare_tmp_bytes(&self, block_size: usize, extension_factor: usize, res_infos: &R, infos: &A) -> usize - where - R: GGSWInfos, - A: BDDKeyInfos; - fn fhe_uint_prepare( - &self, - res: &mut FheUintPrepared, - bits: &FheUint, - key: &K, - scratch: &mut Scratch, - ) where - DM: DataMut, - DB: DataRef, - DK: DataRef, - K: BDDKeyHelper; -} - -impl FheUintPrepare for Module -where - Self: LWEFromGLWE + CirtuitBootstrappingExecute + GGSWPreparedFactory, - Scratch: ScratchTakeCore, -{ - fn fhe_uint_prepare_tmp_bytes(&self, block_size: usize, extension_factor: usize, res_infos: &R, bdd_infos: &A) -> usize - where - R: GGSWInfos, - A: BDDKeyInfos, - { - self.circuit_bootstrapping_execute_tmp_bytes( - block_size, - extension_factor, - res_infos, - &bdd_infos.cbt_infos(), - ) - } - - fn fhe_uint_prepare( - &self, - res: &mut FheUintPrepared, - bits: &FheUint, - key: &K, - scratch: &mut Scratch, - ) where - DM: DataMut, - DB: DataRef, - DK: DataRef, - K: BDDKeyHelper, - { - let (cbt, ks) = key.get_cbt_key(); - - let mut lwe: LWE> = LWE::alloc_from_infos(bits); //TODO: add TakeLWE - let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(res); - for (bit, dst) in res.bits.iter_mut().enumerate() { - bits.get_bit(self, bit, &mut lwe, ks, scratch_1); - cbt.execute_to_constant(self, &mut tmp_ggsw, &lwe, 1, 1, scratch_1); - dst.prepare(self, &tmp_ggsw, scratch_1); - } - } -} - pub trait BDDKeyHelper { fn get_cbt_key( &self, @@ -274,21 +203,7 @@ pub trait BDDKeyHelper { ); } -impl FheUintPrepared { - pub fn prepare(&mut self, module: &M, other: &FheUint, key: &K, scratch: &mut Scratch) - where - BRA: BlindRotationAlgo, - O: DataRef, - DK: DataRef, - K: BDDKeyHelper, - M: FheUintPrepare, - Scratch: ScratchTakeCore, - { - module.fhe_uint_prepare(self, other, key, scratch); - } -} - -pub trait FheUintBlockDebugPrepare { +pub trait FheUintPrepareDebug { fn fhe_uint_debug_prepare( &self, res: &mut FheUintPreparedDebug, 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 2238266..7653f65 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/fft64_ref.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/fft64_ref.rs @@ -5,8 +5,9 @@ use poulpy_backend::FFT64Ref; use crate::tfhe::{ bdd_arithmetic::tests::test_suite::{ 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_fhe_uint_splice_u8, test_fhe_uint_splice_u16, - test_glwe_blind_selection, test_glwe_to_glwe_blind_rotation, test_scalar_to_ggsw_blind_rotation, + test_bdd_sra, test_bdd_srl, test_bdd_sub, test_bdd_xor, test_fhe_uint_sext, test_fhe_uint_splice_u8, + test_fhe_uint_splice_u16, test_glwe_blind_selection, test_glwe_to_glwe_blind_rotation, + test_scalar_to_ggsw_blind_rotation, }, blind_rotation::CGGI, }; @@ -14,6 +15,11 @@ use crate::tfhe::{ static TEST_CONTEXT_CGGI_FFT64_REF: LazyLock> = LazyLock::new(|| TestContext::::new()); +#[test] +fn test_fhe_uint_sext_fft64_ref() { + test_fhe_uint_sext(&TEST_CONTEXT_CGGI_FFT64_REF); +} + #[test] fn test_glwe_blind_selection_fft64_ref() { test_glwe_blind_selection(&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 3ad0651..683c919 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 @@ -11,8 +11,8 @@ use rand::RngCore; use crate::tfhe::{ bdd_arithmetic::{ - Add, BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, - FheUintPrepare, FheUintPreparedEncryptSk, FheUintPreparedFactory, FheUintPrepared, + Add, BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintPrepare, + FheUintPrepareDebug, FheUintPrepared, FheUintPreparedEncryptSk, FheUintPreparedFactory, tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext}, }, blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, @@ -26,7 +26,7 @@ where + GLWENoise + FheUintPreparedFactory + FheUintPreparedEncryptSk - + FheUintBlockDebugPrepare + + FheUintPrepareDebug + BDDKeyEncryptSk + BDDKeyPreparedFactory + GGSWNoise 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 5ed7471..928d83b 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 @@ -11,8 +11,8 @@ use rand::RngCore; use crate::tfhe::{ bdd_arithmetic::{ - And, BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, - FheUintPrepare, FheUintPreparedEncryptSk, FheUintPreparedFactory, FheUintPrepared, + And, BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintPrepare, + FheUintPrepareDebug, FheUintPrepared, FheUintPreparedEncryptSk, FheUintPreparedFactory, tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext}, }, blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, @@ -26,7 +26,7 @@ where + GLWENoise + FheUintPreparedFactory + FheUintPreparedEncryptSk - + FheUintBlockDebugPrepare + + FheUintPrepareDebug + BDDKeyEncryptSk + BDDKeyPreparedFactory + GGSWNoise diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/fheuint.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/fheuint.rs index 0c82084..8938513 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/fheuint.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/tests/test_suite/fheuint.rs @@ -16,6 +16,51 @@ use crate::tfhe::{ blind_rotation::BlindRotationAlgo, }; +pub fn test_fhe_uint_sext(test_context: &TestContext) +where + Module: GLWEEncryptSk + GLWERotate + GLWETrace + GLWESub + GLWEAdd + GLWEDecrypt, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeBDD, +{ + let glwe_infos: GLWELayout = TEST_GLWE_INFOS; + + let module: &Module = &test_context.module; + let sk: &GLWESecretPrepared, BE> = &test_context.sk_glwe; + let keys: &BDDKeyPrepared, BRA, BE> = &test_context.bdd_key; + + 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 a_enc: FheUint, u32> = FheUint::, u32>::alloc_from_infos(&glwe_infos); + + for j in 0..3 { + for i in 0..32 { + let a: u32 = 0xFFFFFFFF >> i; + + a_enc.encrypt_sk( + module, + a, + sk, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + ); + + a_enc.sext(module, j, keys, scratch.borrow()); + + // println!("{:08x} -> {:08x} {:08x}", a, sext(a, j), a_enc.decrypt(module, sk, scratch.borrow())); + + assert_eq!(sext(a, j), a_enc.decrypt(module, sk, scratch.borrow())); + } + } +} + +pub fn sext(x: u32, byte: usize) -> u32 { + x | ((x >> (byte << 3)) & 1) * (0xFFFF_FFFF & (0xFFFF_FFFF << (byte << 3))) +} + pub fn test_fhe_uint_splice_u8(test_context: &TestContext) where Module: GLWEEncryptSk + GLWERotate + GLWETrace + GLWESub + GLWEAdd + GLWEDecrypt, 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 53f6d2d..69b773c 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 @@ -11,8 +11,8 @@ use rand::RngCore; use crate::tfhe::{ bdd_arithmetic::{ - BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, - FheUintPrepare, FheUintPreparedEncryptSk, FheUintPreparedFactory, FheUintPrepared, Or, + BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintPrepare, + FheUintPrepareDebug, FheUintPrepared, FheUintPreparedEncryptSk, FheUintPreparedFactory, Or, tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext}, }, blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, @@ -26,7 +26,7 @@ where + GLWENoise + FheUintPreparedFactory + FheUintPreparedEncryptSk - + FheUintBlockDebugPrepare + + FheUintPrepareDebug + BDDKeyEncryptSk + BDDKeyPreparedFactory + GGSWNoise 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 d930f40..7da039e 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 @@ -11,8 +11,8 @@ use rand::RngCore; use crate::tfhe::{ bdd_arithmetic::{ - BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, - FheUintPrepare, FheUintPreparedEncryptSk, FheUintPreparedFactory, FheUintPreparedDebug, + BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintPrepare, + FheUintPrepareDebug, FheUintPreparedDebug, FheUintPreparedEncryptSk, FheUintPreparedFactory, tests::test_suite::{TEST_BASE2K, TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext}, }, blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, @@ -26,7 +26,7 @@ where + GLWENoise + FheUintPreparedFactory + FheUintPreparedEncryptSk - + FheUintBlockDebugPrepare + + FheUintPrepareDebug + BDDKeyEncryptSk + BDDKeyPreparedFactory + GGSWNoise 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 14a7205..9c47883 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 @@ -11,8 +11,8 @@ use rand::RngCore; use crate::tfhe::{ bdd_arithmetic::{ - BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, - FheUintPrepare, FheUintPreparedEncryptSk, FheUintPreparedFactory, FheUintPrepared, Sll, + BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintPrepare, + FheUintPrepareDebug, FheUintPrepared, FheUintPreparedEncryptSk, FheUintPreparedFactory, Sll, tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext}, }, blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, @@ -26,7 +26,7 @@ where + GLWENoise + FheUintPreparedFactory + FheUintPreparedEncryptSk - + FheUintBlockDebugPrepare + + FheUintPrepareDebug + BDDKeyEncryptSk + BDDKeyPreparedFactory + GGSWNoise 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 87d8235..fc9b5e1 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 @@ -11,8 +11,8 @@ use rand::RngCore; use crate::tfhe::{ bdd_arithmetic::{ - BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, - FheUintPrepare, FheUintPreparedEncryptSk, FheUintPreparedFactory, FheUintPrepared, Slt, + BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintPrepare, + FheUintPrepareDebug, FheUintPrepared, FheUintPreparedEncryptSk, FheUintPreparedFactory, Slt, tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext}, }, blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, @@ -26,7 +26,7 @@ where + GLWENoise + FheUintPreparedFactory + FheUintPreparedEncryptSk - + FheUintBlockDebugPrepare + + FheUintPrepareDebug + BDDKeyEncryptSk + BDDKeyPreparedFactory + GGSWNoise 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 19ff2c2..444da03 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 @@ -11,8 +11,8 @@ use rand::RngCore; use crate::tfhe::{ bdd_arithmetic::{ - BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, - FheUintPrepare, FheUintPreparedEncryptSk, FheUintPreparedFactory, FheUintPrepared, Sltu, + BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintPrepare, + FheUintPrepareDebug, FheUintPrepared, FheUintPreparedEncryptSk, FheUintPreparedFactory, Sltu, tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext}, }, blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, @@ -26,7 +26,7 @@ where + GLWENoise + FheUintPreparedFactory + FheUintPreparedEncryptSk - + FheUintBlockDebugPrepare + + FheUintPrepareDebug + BDDKeyEncryptSk + BDDKeyPreparedFactory + GGSWNoise 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 79cf96b..abb4269 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 @@ -11,8 +11,8 @@ use rand::RngCore; use crate::tfhe::{ bdd_arithmetic::{ - BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, - FheUintPrepare, FheUintPreparedEncryptSk, FheUintPreparedFactory, FheUintPrepared, Sra, + BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintPrepare, + FheUintPrepareDebug, FheUintPrepared, FheUintPreparedEncryptSk, FheUintPreparedFactory, Sra, tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext}, }, blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, @@ -26,7 +26,7 @@ where + GLWENoise + FheUintPreparedFactory + FheUintPreparedEncryptSk - + FheUintBlockDebugPrepare + + FheUintPrepareDebug + BDDKeyEncryptSk + BDDKeyPreparedFactory + GGSWNoise 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 49429df..d9087fd 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 @@ -11,8 +11,8 @@ use rand::RngCore; use crate::tfhe::{ bdd_arithmetic::{ - BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, - FheUintPrepare, FheUintPreparedEncryptSk, FheUintPreparedFactory, FheUintPrepared, Srl, + BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintPrepare, + FheUintPrepareDebug, FheUintPrepared, FheUintPreparedEncryptSk, FheUintPreparedFactory, Srl, tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext}, }, blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, @@ -26,7 +26,7 @@ where + GLWENoise + FheUintPreparedFactory + FheUintPreparedEncryptSk - + FheUintBlockDebugPrepare + + FheUintPrepareDebug + BDDKeyEncryptSk + BDDKeyPreparedFactory + GGSWNoise 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 f5eea39..6df0c92 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 @@ -11,8 +11,8 @@ use rand::RngCore; use crate::tfhe::{ bdd_arithmetic::{ - BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, - FheUintPrepare, FheUintPreparedEncryptSk, FheUintPreparedFactory, FheUintPrepared, Sub, + BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintPrepare, + FheUintPrepareDebug, FheUintPrepared, FheUintPreparedEncryptSk, FheUintPreparedFactory, Sub, tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext}, }, blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, @@ -26,7 +26,7 @@ where + GLWENoise + FheUintPreparedFactory + FheUintPreparedEncryptSk - + FheUintBlockDebugPrepare + + FheUintPrepareDebug + BDDKeyEncryptSk + BDDKeyPreparedFactory + GGSWNoise 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 c09f3bf..e035246 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 @@ -11,8 +11,8 @@ use rand::RngCore; use crate::tfhe::{ bdd_arithmetic::{ - BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, - FheUintPrepare, FheUintPreparedEncryptSk, FheUintPreparedFactory, FheUintPrepared, Xor, + BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintPrepare, + FheUintPrepareDebug, FheUintPrepared, FheUintPreparedEncryptSk, FheUintPreparedFactory, Xor, tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext}, }, blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, @@ -26,7 +26,7 @@ where + GLWENoise + FheUintPreparedFactory + FheUintPreparedEncryptSk - + FheUintBlockDebugPrepare + + FheUintPrepareDebug + BDDKeyEncryptSk + BDDKeyPreparedFactory + GGSWNoise diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs index 22c841b..83be647 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs @@ -305,7 +305,7 @@ pub fn circuit_bootstrap_core( scratch_2, ); } else { - tmp_glwe.trace(module, 0, module.log_n(), &res_glwe, &key.atk, scratch_2); + tmp_glwe.trace(module, 0, &res_glwe, &key.atk, scratch_2); } // let sk_glwe: &poulpy_core::layouts::GLWESecret<&[u8]> = &sk_glwe.to_ref(); @@ -344,8 +344,6 @@ fn post_process( let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let a: &GLWE<&[u8]> = &a.to_ref(); - let log_n: usize = module.log_n(); - let mut cts: HashMap>> = HashMap::new(); // First partial trace, vanishes all coefficients which are not multiples of gap_in @@ -353,7 +351,6 @@ fn post_process( res.trace( module, module.log_n() - log_gap_in + 1, - log_n, a, auto_keys, scratch,