update BDD ciphertext types + API for GLWEToLWE

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

View File

@@ -4,7 +4,7 @@ use poulpy_hal::{
}; };
use crate::{ use crate::{
GLWEKeyswitch, ScratchTakeCore, GLWEKeyswitch, GLWERotate, ScratchTakeCore,
layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank}, layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank},
}; };
@@ -37,11 +37,11 @@ where
} }
impl<BE: Backend> LWESampleExtract for Module<BE> where Self: ModuleN {} impl<BE: Backend> LWESampleExtract for Module<BE> where Self: ModuleN {}
impl<BE: Backend> LWEFromGLWE<BE> for Module<BE> where Self: GLWEKeyswitch<BE> + LWESampleExtract {} impl<BE: Backend> LWEFromGLWE<BE> for Module<BE> where Self: GLWEKeyswitch<BE> + LWESampleExtract + GLWERotate<BE> {}
pub trait LWEFromGLWE<BE: Backend> pub trait LWEFromGLWE<BE: Backend>
where where
Self: GLWEKeyswitch<BE> + LWESampleExtract, Self: GLWEKeyswitch<BE> + LWESampleExtract + GLWERotate<BE>,
{ {
fn lwe_from_glwe_tmp_bytes<R, A, K>(&self, lwe_infos: &R, glwe_infos: &A, key_infos: &K) -> usize fn lwe_from_glwe_tmp_bytes<R, A, K>(&self, lwe_infos: &R, glwe_infos: &A, key_infos: &K) -> usize
where where
@@ -61,10 +61,11 @@ where
lwe_infos.base2k(), lwe_infos.base2k(),
lwe_infos.k(), lwe_infos.k(),
1u32.into(), 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<R, A, K>(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch<BE>) fn lwe_from_glwe<R, A, K>(&self, res: &mut R, a: &A, a_idx: usize, key: &K, scratch: &mut Scratch<BE>)
where where
R: LWEToMut, R: LWEToMut,
A: GLWEToRef, A: GLWEToRef,
@@ -85,9 +86,20 @@ where
rank: Rank(1), rank: Rank(1),
}; };
let (mut tmp_glwe, scratch_1) = scratch.take_glwe(&glwe_layout); let (mut tmp_glwe_rank_1, scratch_1) = scratch.take_glwe(&glwe_layout);
self.glwe_keyswitch(&mut tmp_glwe, a, key, scratch_1);
self.lwe_sample_extract(res, &tmp_glwe); 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<D: DataMut> LWE<D> {
module.lwe_sample_extract(self, a); module.lwe_sample_extract(self, a);
} }
pub fn from_glwe<A, K, M, BE: Backend>(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch<BE>) pub fn from_glwe<A, K, M, BE: Backend>(&mut self, module: &M, a: &A, a_idx: usize, key: &K, scratch: &mut Scratch<BE>)
where where
A: GLWEToRef, A: GLWEToRef,
K: GGLWEPreparedToRef<BE> + GGLWEInfos, K: GGLWEPreparedToRef<BE> + GGLWEInfos,
M: LWEFromGLWE<BE>, M: LWEFromGLWE<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
module.lwe_from_glwe(self, a, key, scratch); module.lwe_from_glwe(self, a, a_idx, key, scratch);
} }
} }

View File

@@ -5,7 +5,7 @@ use poulpy_hal::{
}; };
use crate::{ use crate::{
GLWEDecrypt, GLWEEncryptSk, GLWEFromLWE, GLWEToLWESwitchingKeyEncryptSk, LWEDecrypt, LWEEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWEFromLWE, GLWEToLWESwitchingKeyEncryptSk, LWEDecrypt, LWEEncryptSk, LWEFromGLWE,
LWEToGLWESwitchingKeyEncryptSk, ScratchTakeCore, LWEToGLWESwitchingKeyEncryptSk, ScratchTakeCore,
layouts::{ layouts::{
Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWEToLWEKey, Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWEToLWEKey,
@@ -110,6 +110,7 @@ where
+ GLWEToLWESwitchingKeyEncryptSk<BE> + GLWEToLWESwitchingKeyEncryptSk<BE>
+ GLWEEncryptSk<BE> + GLWEEncryptSk<BE>
+ LWEDecrypt<BE> + LWEDecrypt<BE>
+ LWEFromGLWE<BE>
+ GLWEDecrypt<BE> + GLWEDecrypt<BE>
+ GLWESecretPreparedFactory<BE> + GLWESecretPreparedFactory<BE>
+ GLWEToLWESwitchingKeyEncryptSk<BE> + GLWEToLWESwitchingKeyEncryptSk<BE>
@@ -163,9 +164,14 @@ where
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe); let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
sk_lwe.fill_ternary_prob(0.5, &mut source_xs); sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
let data: i64 = 17; let a_idx: usize = 1;
let mut data: Vec<i64> = vec![0i64; module.n()];
data[a_idx] = 17;
let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos); let mut glwe_pt: GLWEPlaintext<Vec<u8>> = 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<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos); let mut glwe_ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
glwe_ct.encrypt_sk( glwe_ct.encrypt_sk(
@@ -193,10 +199,10 @@ where
let mut ksk_prepared: GLWEToLWEKeyPrepared<Vec<u8>, BE> = GLWEToLWEKeyPrepared::alloc_from_infos(module, &ksk); let mut ksk_prepared: GLWEToLWEKeyPrepared<Vec<u8>, BE> = GLWEToLWEKeyPrepared::alloc_from_infos(module, &ksk);
ksk_prepared.prepare(module, &ksk, scratch.borrow()); 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<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos); let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos);
lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe); 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]);
} }

View File

@@ -1,36 +1,43 @@
use std::marker::PhantomData; 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 poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch};
use crate::tfhe::bdd_arithmetic::{ use crate::tfhe::{
BitSize, ExecuteBDDCircuit, FheUint, FheUintPrepared, GetBitCircuitInfo, GetGGSWBit, UnsignedInteger, circuits, bdd_arithmetic::{
BDDKeyPrepared, BitSize, ExecuteBDDCircuit, FheUint, FheUintPrepared, GetBitCircuitInfo, GetGGSWBit, UnsignedInteger,
circuits,
},
blind_rotation::BlindRotationAlgo,
}; };
impl<T: UnsignedInteger, BE: Backend> ExecuteBDDCircuit2WTo1W<T, BE> for Module<BE> where Self: Sized + ExecuteBDDCircuit<T, BE> {} impl<T: UnsignedInteger, BE: Backend> ExecuteBDDCircuit2WTo1W<T, BE> for Module<BE> where
Self: Sized + ExecuteBDDCircuit<T, BE> + GLWEPacking<BE> + GLWECopy
{
}
pub trait ExecuteBDDCircuit2WTo1W<T: UnsignedInteger, BE: Backend> pub trait ExecuteBDDCircuit2WTo1W<T: UnsignedInteger, BE: Backend>
where where
Self: Sized + ExecuteBDDCircuit<T, BE>, Self: Sized + ExecuteBDDCircuit<T, BE> + GLWEPacking<BE> + GLWECopy,
{ {
/// Operations Z x Z -> Z /// Operations Z x Z -> Z
fn execute_bdd_circuit_2w_to_1w<R, C, A, B>( fn execute_bdd_circuit_2w_to_1w<R, C, A, B, DK, BRA>(
&self, &self,
out: &mut FheUint<R, T>, out: &mut FheUint<R, T>,
circuit: &C, circuit: &C,
a: &FheUintPrepared<A, T, BE>, a: &FheUintPrepared<A, T, BE>,
b: &FheUintPrepared<B, T, BE>, b: &FheUintPrepared<B, T, BE>,
key: &BDDKeyPrepared<DK, BRA, BE>,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
BRA: BlindRotationAlgo,
DK: DataRef,
C: GetBitCircuitInfo<T>, C: GetBitCircuitInfo<T>,
R: DataMut, R: DataMut,
A: DataRef, A: DataRef,
B: DataRef, B: DataRef,
Scratch<BE>: ScratchTakeCore<BE>,
{ {
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 // Collects inputs into a single array
let inputs: Vec<&dyn GetGGSWBit<BE>> = [a as &dyn GetGGSWBit<BE>, b as &dyn GetGGSWBit<BE>].to_vec(); let inputs: Vec<&dyn GetGGSWBit<BE>> = [a as &dyn GetGGSWBit<BE>, b as &dyn GetGGSWBit<BE>].to_vec();
let helper: FheUintHelper<'_, T, BE> = FheUintHelper { let helper: FheUintHelper<'_, T, BE> = FheUintHelper {
@@ -38,8 +45,13 @@ where
_phantom: PhantomData, _phantom: PhantomData,
}; };
let (mut out_bits, scratch_1) = scratch.take_glwe_slice(T::WORD_SIZE, out);
// Evaluates out[i] = circuit[i](a, b) // 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:meta])* $vis:vis $trait_name:ident, $method_name:ident) => {
$(#[$meta])* $(#[$meta])*
$vis trait $trait_name<T: UnsignedInteger, BE: Backend> { $vis trait $trait_name<T: UnsignedInteger, BE: Backend> {
fn $method_name<A, M, B>( fn $method_name<A, M, K, BRA, B>(
&mut self, &mut self,
module: &M, module: &M,
a: &FheUintPrepared<A, T, BE>, a: &FheUintPrepared<A, T, BE>,
b: &FheUintPrepared<B, T, BE>, b: &FheUintPrepared<B, T, BE>,
key: &BDDKeyPrepared<K, BRA, BE>,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
M: ExecuteBDDCircuit2WTo1W<T, BE>, M: ExecuteBDDCircuit2WTo1W<T, BE>,
A: DataRef, A: DataRef,
B: DataRef; B: DataRef,
K: DataRef,
BRA: BlindRotationAlgo,
Scratch<BE>: ScratchTakeCore<BE>;
} }
}; };
} }
@@ -106,18 +122,22 @@ macro_rules! define_bdd_2w_to_1w_trait {
macro_rules! impl_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) => { ($trait_name:ident, $method_name:ident, $ty:ty, $n:literal, $circuit_ty:ty, $output_circuits:path) => {
impl<D: DataMut, BE: Backend> $trait_name<$ty, BE> for FheUint<D, $ty> { impl<D: DataMut, BE: Backend> $trait_name<$ty, BE> for FheUint<D, $ty> {
fn $method_name<A, M, B>( fn $method_name<A, M, K, BRA, B>(
&mut self, &mut self,
module: &M, module: &M,
a: &FheUintPrepared<A, $ty, BE>, a: &FheUintPrepared<A, $ty, BE>,
b: &FheUintPrepared<B, $ty, BE>, b: &FheUintPrepared<B, $ty, BE>,
key: &BDDKeyPrepared<K, BRA, BE>,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
M: ExecuteBDDCircuit2WTo1W<$ty, BE>, M: ExecuteBDDCircuit2WTo1W<$ty, BE>,
A: DataRef, A: DataRef,
B: DataRef, B: DataRef,
K: DataRef,
BRA: BlindRotationAlgo,
Scratch<BE>: ScratchTakeCore<BE>,
{ {
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)
} }
} }
}; };

View File

@@ -1,9 +1,9 @@
use itertools::Itertools; use itertools::Itertools;
use poulpy_core::{ use poulpy_core::{
GLWECopy, GLWEDecrypt, GLWEEncryptSk, GLWEPacking, ScratchTakeCore, GLWECopy, GLWEDecrypt, GLWEEncryptSk, GLWEPacking, GLWERotate, LWEFromGLWE, ScratchTakeCore,
layouts::{ layouts::{
GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, LWEInfos, TorusPrecision, Base2K, Degree, GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, GLWEToRef,
prepared::GLWEAutomorphismKeyPrepared, LWEInfos, LWEToMut, Rank, TorusPrecision, prepared::GLWEAutomorphismKeyPrepared,
}, },
}; };
use poulpy_hal::{ use poulpy_hal::{
@@ -15,58 +15,49 @@ use std::{collections::HashMap, marker::PhantomData};
use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger}; use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger};
/// An FHE ciphertext encrypting the bits of an [UnsignedInteger] in compressed format (ideal for decryption/serialization). /// An FHE ciphertext encrypting the bits of an [UnsignedInteger].
pub struct FheUintCompressed<D: Data, T: UnsignedInteger>(pub(crate) GLWE<D>, pub(crate) PhantomData<T>); pub struct FheUint<D: Data, T: UnsignedInteger> {
pub(crate) bits: GLWE<D>,
pub(crate) _phantom: PhantomData<T>,
}
impl<D: DataMut, T: UnsignedInteger> FheUintCompressed<D, T> { impl<T: UnsignedInteger> FheUint<Vec<u8>, T> {
#[allow(dead_code)] pub fn alloc_from_infos<A>(infos: &A) -> Self
fn post_process<ATK, M, BE: Backend>( where
&mut self, A: GLWEInfos,
module: &M,
mut tmp_res: Vec<GLWE<&mut [u8]>>,
auto_keys: &HashMap<i64, GLWEAutomorphismKeyPrepared<ATK, BE>>,
scratch: &mut Scratch<BE>,
) where
ATK: DataRef,
M: GLWEPacking<BE> + GLWECopy,
Scratch<BE>: ScratchTakeCore<BE>,
{ {
// Repacks the GLWE ciphertexts bits Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank())
let gap: usize = module.n() / T::WORD_SIZE; }
let log_gap: usize = (usize::BITS - (gap - 1).leading_zeros()) as usize;
let mut cts: HashMap<usize, &mut GLWE<&mut [u8]>> = HashMap::new(); pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
for (i, ct) in tmp_res.iter_mut().enumerate().take(T::WORD_SIZE) { Self {
cts.insert(i * gap, ct); 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<D: DataRef, T: UnsignedInteger> LWEInfos for FheUintCompressed<D, T> { impl<D: DataRef, T: UnsignedInteger> LWEInfos for FheUint<D, T> {
fn base2k(&self) -> poulpy_core::layouts::Base2K { fn base2k(&self) -> poulpy_core::layouts::Base2K {
self.0.base2k() self.bits.base2k()
} }
fn k(&self) -> poulpy_core::layouts::TorusPrecision { fn k(&self) -> poulpy_core::layouts::TorusPrecision {
self.0.k() self.bits.k()
} }
fn n(&self) -> poulpy_core::layouts::Degree { fn n(&self) -> poulpy_core::layouts::Degree {
self.0.n() self.bits.n()
} }
} }
impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUintCompressed<D, T> { impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUint<D, T> {
fn rank(&self) -> poulpy_core::layouts::Rank { fn rank(&self) -> poulpy_core::layouts::Rank {
self.0.rank() self.bits.rank()
} }
} }
impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintCompressed<D, T> { impl<D: DataMut, T: UnsignedInteger + ToBits> FheUint<D, T> {
pub fn encrypt_sk<S, M, BE: Backend>( pub fn encrypt_sk<S, M, BE: Backend>(
&mut self, &mut self,
module: &M, module: &M,
@@ -103,13 +94,13 @@ impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintCompressed<D, T> {
let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos); let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos);
pt.encode_vec_i64(&data_bits, TorusPrecision(1)); pt.encode_vec_i64(&data_bits, TorusPrecision(2));
self.0 self.bits
.encrypt_sk(module, &pt, sk, source_xa, source_xe, scratch_1); .encrypt_sk(module, &pt, sk, source_xa, source_xe, scratch_1);
} }
} }
impl<D: DataRef, T: UnsignedInteger + FromBits> FheUintCompressed<D, T> { impl<D: DataRef, T: UnsignedInteger + FromBits> FheUint<D, T> {
pub fn decrypt<S, M, BE: Backend>(&self, module: &M, sk: &S, scratch: &mut Scratch<BE>) -> T pub fn decrypt<S, M, BE: Backend>(&self, module: &M, sk: &S, scratch: &mut Scratch<BE>) -> T
where where
S: GLWESecretPreparedToRef<BE> + GLWEInfos, S: GLWESecretPreparedToRef<BE> + GLWEInfos,
@@ -133,13 +124,61 @@ impl<D: DataRef, T: UnsignedInteger + FromBits> FheUintCompressed<D, T> {
let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos); 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<i64> = vec![0i64; module.n()]; let mut data: Vec<i64> = vec![0i64; module.n()];
pt.decode_vec_i64(&mut data, TorusPrecision(1)); pt.decode_vec_i64(&mut data, TorusPrecision(2));
let bits: Vec<u8> = data.iter().step_by(gap).map(|c| *c as u8).collect_vec(); let bits: Vec<u8> = data.iter().step_by(gap).map(|c| *c as u8).collect_vec();
T::from_bits(&bits) T::from_bits(&bits)
} }
} }
impl<D: DataMut, T: UnsignedInteger> FheUint<D, T> {
#[allow(dead_code)]
pub(crate) fn pack<D1, ATK, M, BE: Backend>(
&mut self,
module: &M,
mut tmp_res: Vec<GLWE<D1>>,
auto_keys: &HashMap<i64, GLWEAutomorphismKeyPrepared<ATK, BE>>,
scratch: &mut Scratch<BE>,
) where
D1: DataMut,
ATK: DataRef,
M: GLWEPacking<BE> + GLWECopy,
Scratch<BE>: ScratchTakeCore<BE>,
{
// 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<usize, &mut GLWE<D1>> = 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<D: DataRef, T: UnsignedInteger> FheUint<D, T> {
pub fn get_bit<L, K, M, BE: Backend>(&self, module: &M, bit: usize, res: &mut L, ks: &K, scratch: &mut Scratch<BE>)
where
L: LWEToMut,
K: GGLWEPreparedToRef<BE> + GGLWEInfos,
M: LWEFromGLWE<BE> + GLWERotate<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
let gap: usize = module.n() / T::WORD_SIZE;
res.to_mut().from_glwe(module, self, bit * gap, ks, scratch);
}
}
impl<D: DataRef, T: UnsignedInteger> GLWEToRef for FheUint<D, T> {
fn to_ref(&self) -> GLWE<&[u8]> {
self.bits.to_ref()
}
}

View File

@@ -1,10 +1,8 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use crate::tfhe::bdd_arithmetic::{BddKeyPrepared, FheUintBlockDebugPrepare, ToBits}; use crate::tfhe::bdd_arithmetic::{BDDKeyPrepared, FheUint, FheUintBlockDebugPrepare, ToBits};
use crate::tfhe::{ use crate::tfhe::{
bdd_arithmetic::{FheUint, UnsignedInteger}, bdd_arithmetic::UnsignedInteger, blind_rotation::BlindRotationAlgo, circuit_bootstrapping::CirtuitBootstrappingExecute,
blind_rotation::BlindRotationAlgo,
circuit_bootstrapping::CirtuitBootstrappingExecute,
}; };
use poulpy_core::GGSWNoise; use poulpy_core::GGSWNoise;
@@ -113,25 +111,23 @@ impl<D: DataRef, T: UnsignedInteger + ToBits> FheUintPreparedDebug<D, T> {
impl<BRA: BlindRotationAlgo, BE: Backend, T: UnsignedInteger> FheUintBlockDebugPrepare<BRA, T, BE> for Module<BE> impl<BRA: BlindRotationAlgo, BE: Backend, T: UnsignedInteger> FheUintBlockDebugPrepare<BRA, T, BE> for Module<BE>
where where
Self: LWEFromGLWE<BE> + CirtuitBootstrappingExecute<BRA, BE> + GGSWPreparedFactory<BE>, Self: ModuleN + LWEFromGLWE<BE> + CirtuitBootstrappingExecute<BRA, BE> + GGSWPreparedFactory<BE>,
Scratch<BE>: ScratchTakeCore<BE>, Scratch<BE>: ScratchTakeCore<BE>,
{ {
fn fhe_uint_debug_prepare<DM, DR0, DR1>( fn fhe_uint_debug_prepare<DM, DR0, DR1>(
&self, &self,
res: &mut FheUintPreparedDebug<DM, T>, res: &mut FheUintPreparedDebug<DM, T>,
bits: &FheUint<DR0, T>, bits: &FheUint<DR0, T>,
key: &BddKeyPrepared<DR1, BRA, BE>, key: &BDDKeyPrepared<DR1, BRA, BE>,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
DM: DataMut, DM: DataMut,
DR0: DataRef, DR0: DataRef,
DR1: DataRef, DR1: DataRef,
{ {
assert_eq!(res.bits.len(), bits.bits.len()); let mut lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(bits); //TODO: add TakeLWE
for (bit, dst) in res.bits.iter_mut().enumerate() {
let mut lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&bits.bits[0]); //TODO: add TakeLWE bits.get_bit(self, bit, &mut lwe, &key.ks, scratch);
for (dst, src) in res.bits.iter_mut().zip(bits.bits.iter()) {
lwe.from_glwe(self, src, &key.ks, scratch);
key.cbt.execute_to_constant(self, dst, &lwe, 1, 1, scratch); key.cbt.execute_to_constant(self, dst, &lwe, 1, 1, scratch);
} }
} }
@@ -142,7 +138,7 @@ impl<D: DataMut, T: UnsignedInteger> FheUintPreparedDebug<D, T> {
&mut self, &mut self,
module: &M, module: &M,
other: &FheUint<O, T>, other: &FheUint<O, T>,
key: &BddKeyPrepared<K, BRA, BE>, key: &BDDKeyPrepared<K, BRA, BE>,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
BRA: BlindRotationAlgo, BRA: BlindRotationAlgo,

View File

@@ -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<D: Data, T: UnsignedInteger> {
pub(crate) bits: Vec<GLWE<D>>,
pub(crate) _phantom: PhantomData<T>,
}
impl<D: DataRef, T: UnsignedInteger> LWEInfos for FheUint<D, T> {
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<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUint<D, T> {
fn rank(&self) -> poulpy_core::layouts::Rank {
self.bits[0].rank()
}
}
impl<D: Data, T: UnsignedInteger> FheUint<D, T> {
pub fn new(bits: Vec<GLWE<D>>) -> Self {
assert_eq!(bits.len(), T::WORD_SIZE);
Self {
bits,
_phantom: PhantomData,
}
}
}
impl<T: UnsignedInteger> FheUint<Vec<u8>, T> {
pub fn alloc_from_infos<A, BE: Backend>(module: &Module<BE>, infos: &A) -> Self
where
A: GLWEInfos,
{
Self::alloc(module, infos.base2k(), infos.k(), infos.rank())
}
pub fn alloc<BE: Backend>(module: &Module<BE>, 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<D: DataMut, T: UnsignedInteger + ToBits> FheUint<D, T> {
pub fn encrypt_sk<S, BE: Backend>(
&mut self,
module: &Module<BE>,
value: T,
sk: &S,
source_xa: &mut Source,
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
S: GLWESecretPreparedToRef<BE> + GLWEInfos,
Module<BE>: GLWEEncryptSk<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
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<D: DataRef, T: UnsignedInteger + FromBits + ToBits> FheUint<D, T> {
pub fn decrypt<S, BE: Backend>(&self, module: &Module<BE>, sk: &S, scratch: &mut Scratch<BE>) -> T
where
Module<BE>: GLWEDecrypt<BE>,
S: GLWESecretPreparedToRef<BE> + GLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
#[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<u8> = 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<S, BE: Backend>(&self, module: &Module<BE>, sk: &S, want: T, scratch: &mut Scratch<BE>) -> Vec<f64>
where
Module<BE>: GLWENoise<BE>,
S: GLWESecretPreparedToRef<BE> + GLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
#[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<f64> = 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
}
}

View File

@@ -1,11 +1,9 @@
mod fhe_uint_compressed; mod fhe_uint;
mod fhe_uint_prepared; mod fhe_uint_prepared;
mod fheuint;
mod fhe_uint_prepared_debug; mod fhe_uint_prepared_debug;
pub use 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 fhe_uint_prepared::*;
pub use fheuint::*;

View File

@@ -7,6 +7,7 @@ use crate::tfhe::{
CircuitBootstrappingKeyPrepared, CircuitBootstrappingKeyPreparedFactory, CirtuitBootstrappingExecute, CircuitBootstrappingKeyPrepared, CircuitBootstrappingKeyPreparedFactory, CirtuitBootstrappingExecute,
}, },
}; };
use poulpy_core::{ use poulpy_core::{
GLWEToLWESwitchingKeyEncryptSk, GetDistribution, LWEFromGLWE, ScratchTakeCore, GLWEToLWESwitchingKeyEncryptSk, GetDistribution, LWEFromGLWE, ScratchTakeCore,
layouts::{ layouts::{
@@ -45,8 +46,8 @@ where
D: Data, D: Data,
BRA: BlindRotationAlgo, BRA: BlindRotationAlgo,
{ {
cbt: CircuitBootstrappingKey<D, BRA>, pub(crate) cbt: CircuitBootstrappingKey<D, BRA>,
ks: GLWEToLWEKey<D>, pub(crate) ks: GLWEToLWEKey<D>,
} }
impl<BRA: BlindRotationAlgo> BDDKey<Vec<u8>, BRA> impl<BRA: BlindRotationAlgo> BDDKey<Vec<u8>, BRA>
@@ -123,7 +124,7 @@ impl<D: DataMut, BRA: BlindRotationAlgo> BDDKey<D, BRA> {
} }
} }
pub struct BddKeyPrepared<D, BRA, BE> pub struct BDDKeyPrepared<D, BRA, BE>
where where
D: Data, D: Data,
BRA: BlindRotationAlgo, BRA: BlindRotationAlgo,
@@ -137,11 +138,11 @@ pub trait BDDKeyPreparedFactory<BRA: BlindRotationAlgo, BE: Backend>
where where
Self: Sized + CircuitBootstrappingKeyPreparedFactory<BRA, BE> + GLWEToLWEKeyPreparedFactory<BE>, Self: Sized + CircuitBootstrappingKeyPreparedFactory<BRA, BE> + GLWEToLWEKeyPreparedFactory<BE>,
{ {
fn alloc_bdd_key_from_infos<A>(&self, infos: &A) -> BddKeyPrepared<Vec<u8>, BRA, BE> fn alloc_bdd_key_from_infos<A>(&self, infos: &A) -> BDDKeyPrepared<Vec<u8>, BRA, BE>
where where
A: BDDKeyInfos, A: BDDKeyInfos,
{ {
BddKeyPrepared { BDDKeyPrepared {
cbt: CircuitBootstrappingKeyPrepared::alloc_from_infos(self, &infos.cbt_infos()), cbt: CircuitBootstrappingKeyPrepared::alloc_from_infos(self, &infos.cbt_infos()),
ks: GLWEToLWEKeyPrepared::alloc_from_infos(self, &infos.ks_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())) .max(self.prepare_glwe_to_lwe_key_tmp_bytes(&infos.ks_infos()))
} }
fn prepare_bdd_key<DM, DR>(&self, res: &mut BddKeyPrepared<DM, BRA, BE>, other: &BDDKey<DR, BRA>, scratch: &mut Scratch<BE>) fn prepare_bdd_key<DM, DR>(&self, res: &mut BDDKeyPrepared<DM, BRA, BE>, other: &BDDKey<DR, BRA>, scratch: &mut Scratch<BE>)
where where
DM: DataMut, DM: DataMut,
DR: DataRef, DR: DataRef,
@@ -170,7 +171,7 @@ impl<BRA: BlindRotationAlgo, BE: Backend> BDDKeyPreparedFactory<BRA, BE> for Mod
{ {
} }
impl<BRA: BlindRotationAlgo, BE: Backend> BddKeyPrepared<Vec<u8>, BRA, BE> { impl<BRA: BlindRotationAlgo, BE: Backend> BDDKeyPrepared<Vec<u8>, BRA, BE> {
pub fn alloc_from_infos<M, A>(module: &M, infos: &A) -> Self pub fn alloc_from_infos<M, A>(module: &M, infos: &A) -> Self
where where
M: BDDKeyPreparedFactory<BRA, BE>, M: BDDKeyPreparedFactory<BRA, BE>,
@@ -180,7 +181,7 @@ impl<BRA: BlindRotationAlgo, BE: Backend> BddKeyPrepared<Vec<u8>, BRA, BE> {
} }
} }
impl<D: DataMut, BRA: BlindRotationAlgo, BE: Backend> BddKeyPrepared<D, BRA, BE> { impl<D: DataMut, BRA: BlindRotationAlgo, BE: Backend> BDDKeyPrepared<D, BRA, BE> {
pub fn prepare<DR, M>(&mut self, module: &M, other: &BDDKey<DR, BRA>, scratch: &mut Scratch<BE>) pub fn prepare<DR, M>(&mut self, module: &M, other: &BDDKey<DR, BRA>, scratch: &mut Scratch<BE>)
where where
DR: DataRef, DR: DataRef,
@@ -200,7 +201,7 @@ pub trait FheUintBlocksPrepare<BRA: BlindRotationAlgo, T: UnsignedInteger, BE: B
&self, &self,
res: &mut FheUintPrepared<DM, T, BE>, res: &mut FheUintPrepared<DM, T, BE>,
bits: &FheUint<DR0, T>, bits: &FheUint<DR0, T>,
key: &BddKeyPrepared<DR1, BRA, BE>, key: &BDDKeyPrepared<DR1, BRA, BE>,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
DM: DataMut, DM: DataMut,
@@ -230,19 +231,17 @@ where
&self, &self,
res: &mut FheUintPrepared<DM, T, BE>, res: &mut FheUintPrepared<DM, T, BE>,
bits: &FheUint<DR0, T>, bits: &FheUint<DR0, T>,
key: &BddKeyPrepared<DR1, BRA, BE>, key: &BDDKeyPrepared<DR1, BRA, BE>,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
DM: DataMut, DM: DataMut,
DR0: DataRef, DR0: DataRef,
DR1: DataRef, DR1: DataRef,
{ {
assert_eq!(res.bits.len(), bits.bits.len()); let mut lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(bits); //TODO: add TakeLWE
let mut lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&bits.bits[0]); //TODO: add TakeLWE
let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(res); let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(res);
for (dst, src) in res.bits.iter_mut().zip(bits.bits.iter()) { for (bit, dst) in res.bits.iter_mut().enumerate() {
lwe.from_glwe(self, src, &key.ks, scratch_1); bits.get_bit(self, bit, &mut lwe, &key.ks, scratch_1);
key.cbt key.cbt
.execute_to_constant(self, &mut tmp_ggsw, &lwe, 1, 1, scratch_1); .execute_to_constant(self, &mut tmp_ggsw, &lwe, 1, 1, scratch_1);
dst.prepare(self, &tmp_ggsw, scratch_1); dst.prepare(self, &tmp_ggsw, scratch_1);
@@ -255,7 +254,7 @@ impl<D: DataMut, T: UnsignedInteger, BE: Backend> FheUintPrepared<D, T, BE> {
&mut self, &mut self,
module: &M, module: &M,
other: &FheUint<O, T>, other: &FheUint<O, T>,
key: &BddKeyPrepared<K, BRA, BE>, key: &BDDKeyPrepared<K, BRA, BE>,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
BRA: BlindRotationAlgo, BRA: BlindRotationAlgo,
@@ -273,7 +272,7 @@ pub trait FheUintBlockDebugPrepare<BRA: BlindRotationAlgo, T: UnsignedInteger, B
&self, &self,
res: &mut FheUintPreparedDebug<DM, T>, res: &mut FheUintPreparedDebug<DM, T>,
bits: &FheUint<DR0, T>, bits: &FheUint<DR0, T>,
key: &BddKeyPrepared<DR1, BRA, BE>, key: &BDDKeyPrepared<DR1, BRA, BE>,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
DM: DataMut, DM: DataMut,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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