Add splice u8 and u16

This commit is contained in:
Pro7ech
2025-10-29 21:02:57 +01:00
parent 8743eeb800
commit f03bb4931b
5 changed files with 247 additions and 52 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_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_splice_u8, test_fhe_uint_splice_u16,
test_glwe_to_glwe_blind_rotation, test_scalar_to_ggsw_blind_rotation,
},
blind_rotation::CGGI,
};
@@ -14,6 +14,16 @@ use crate::tfhe::{
static TEST_CONTEXT_CGGI_FFT64_REF: LazyLock<TestContext<CGGI, FFT64Ref>> =
LazyLock::new(|| TestContext::<CGGI, FFT64Ref>::new());
#[test]
fn test_fhe_uint_splice_u8_fft64_ref() {
test_fhe_uint_splice_u8(&TEST_CONTEXT_CGGI_FFT64_REF)
}
#[test]
fn test_fhe_uint_splice_u16_fft64_ref() {
test_fhe_uint_splice_u16(&TEST_CONTEXT_CGGI_FFT64_REF)
}
#[test]
fn test_glwe_to_glwe_blind_rotation_fft64_ref() {
test_glwe_to_glwe_blind_rotation(&TEST_CONTEXT_CGGI_FFT64_REF)

View File

@@ -0,0 +1,128 @@
use poulpy_core::{
GLWEAdd, GLWEDecrypt, GLWEEncryptSk, GLWERotate, GLWESub, GLWETrace,
layouts::{GLWELayout, GLWESecretPrepared},
};
use poulpy_hal::{
api::{ScratchOwnedAlloc, ScratchOwnedBorrow},
layouts::{Backend, Module, Scratch, ScratchOwned},
source::Source,
};
use crate::tfhe::{
bdd_arithmetic::{
BDDKeyPrepared, FheUint, ScratchTakeBDD,
tests::test_suite::{TEST_GLWE_INFOS, TestContext},
},
blind_rotation::BlindRotationAlgo,
};
pub fn test_fhe_uint_splice_u8<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 b_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 = 0xFFFFFFFF;
let b: u32 = 0xAABBCCDD;
b_enc.encrypt_sk(
module,
b,
sk,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
a_enc.encrypt_sk(
module,
a,
sk,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
for dst in 0..4 {
for src in 0..4 {
c_enc.splice_u8(module, dst, src, &a_enc, &b_enc, keys, scratch.borrow());
let rj: u32 = (dst << 3) as u32;
let ri: u32 = (src << 3) as u32;
let a_r: u32 = a.rotate_right(rj);
let b_r: u32 = b.rotate_right(ri);
let c_want: u32 = ((a_r & 0xFFFF_FF00) | (b_r & 0x0000_00FF)).rotate_left(rj);
assert_eq!(c_want, c_enc.decrypt(module, sk, scratch.borrow()));
}
}
}
pub fn test_fhe_uint_splice_u16<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 b_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 = 0xFFFFFFFF;
let b: u32 = 0xAABBCCDD;
b_enc.encrypt_sk(
module,
b,
sk,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
a_enc.encrypt_sk(
module,
a,
sk,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
);
for dst in 0..2 {
for src in 0..2 {
c_enc.splice_u16(module, dst, src, &a_enc, &b_enc, keys, scratch.borrow());
let rj: u32 = (dst << 4) as u32;
let ri: u32 = (src << 4) as u32;
let a_r: u32 = a.rotate_right(rj);
let b_r: u32 = b.rotate_right(ri);
let c_want: u32 = ((a_r & 0xFFFF_0000) | (b_r & 0x0000_FFFF)).rotate_left(rj);
assert_eq!(c_want, c_enc.decrypt(module, sk, scratch.borrow()));
}
}
}

View File

@@ -1,5 +1,6 @@
mod add;
mod and;
mod fheuint;
mod ggsw_blind_rotations;
mod glwe_blind_rotation;
mod or;
@@ -14,6 +15,7 @@ mod xor;
pub use add::*;
pub use and::*;
pub use fheuint::*;
pub use ggsw_blind_rotations::*;
pub use glwe_blind_rotation::*;
pub use or::*;