Add bit/byte extraction to glwe

This commit is contained in:
Pro7ech
2025-11-05 11:12:10 +01:00
parent 92cfef5b60
commit be6483df75
5 changed files with 80 additions and 6 deletions

View File

@@ -5,8 +5,8 @@ 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_sext, test_fhe_uint_splice_u8,
test_fhe_uint_splice_u16, test_glwe_blind_selection, test_glwe_to_glwe_blind_rotation,
test_bdd_sra, test_bdd_srl, test_bdd_sub, test_bdd_xor, test_fhe_uint_get_bit_glwe, 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,
@@ -15,6 +15,11 @@ use crate::tfhe::{
static TEST_CONTEXT_CGGI_FFT64_REF: LazyLock<TestContext<CGGI, FFT64Ref>> =
LazyLock::new(|| TestContext::<CGGI, FFT64Ref>::new());
#[test]
fn test_fhe_uint_get_bit_glwe_fft64_ref() {
test_fhe_uint_get_bit_glwe(&TEST_CONTEXT_CGGI_FFT64_REF);
}
#[test]
fn test_fhe_uint_sext_fft64_ref() {
test_fhe_uint_sext(&TEST_CONTEXT_CGGI_FFT64_REF);

View File

@@ -7,10 +7,11 @@ use poulpy_hal::{
layouts::{Backend, Module, Scratch, ScratchOwned},
source::Source,
};
use rand::RngCore;
use crate::tfhe::{
bdd_arithmetic::{
BDDKeyPrepared, FheUint, ScratchTakeBDD,
BDDKeyPrepared, FheUint, ScratchTakeBDD, ToBits,
tests::test_suite::{TEST_GLWE_INFOS, TestContext},
},
blind_rotation::BlindRotationAlgo,
@@ -171,3 +172,40 @@ where
}
}
}
pub fn test_fhe_uint_get_bit_glwe<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
where
Module<BE>: GLWEEncryptSk<BE> + GLWERotate<BE> + GLWETrace<BE> + GLWESub + GLWEAdd + GLWEDecrypt<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchTakeBDD<u32, BE>,
{
let glwe_infos: GLWELayout = TEST_GLWE_INFOS;
let module: &Module<BE> = &test_context.module;
let sk: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
let keys: &BDDKeyPrepared<Vec<u8>, 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<BE> = ScratchOwned::alloc(1 << 22);
let mut a_enc: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&glwe_infos);
let mut c_enc: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&glwe_infos);
let a: u32 = source_xa.next_u32();
a_enc.encrypt_sk(
module,
a,
sk,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
for i in 0..32 {
a_enc.get_bit_glwe(module, i, &mut c_enc, keys, scratch.borrow());
assert_eq!(a.bit(i) as u32, c_enc.decrypt(module, sk, scratch.borrow()));
}
}