mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
update BDD ciphertext types + API for GLWEToLWE
This commit is contained in:
@@ -4,7 +4,7 @@ use poulpy_hal::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
GLWEKeyswitch, ScratchTakeCore,
|
||||
GLWEKeyswitch, GLWERotate, ScratchTakeCore,
|
||||
layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank},
|
||||
};
|
||||
|
||||
@@ -37,11 +37,11 @@ where
|
||||
}
|
||||
|
||||
impl<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>
|
||||
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
|
||||
where
|
||||
@@ -61,10 +61,11 @@ where
|
||||
lwe_infos.base2k(),
|
||||
lwe_infos.k(),
|
||||
1u32.into(),
|
||||
) + self.glwe_keyswitch_tmp_bytes(&res_infos, glwe_infos, key_infos)
|
||||
) + GLWE::bytes_of_from_infos(glwe_infos)
|
||||
+ self.glwe_keyswitch_tmp_bytes(&res_infos, glwe_infos, key_infos)
|
||||
}
|
||||
|
||||
fn lwe_from_glwe<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
|
||||
R: LWEToMut,
|
||||
A: GLWEToRef,
|
||||
@@ -85,9 +86,20 @@ where
|
||||
rank: Rank(1),
|
||||
};
|
||||
|
||||
let (mut tmp_glwe, scratch_1) = scratch.take_glwe(&glwe_layout);
|
||||
self.glwe_keyswitch(&mut tmp_glwe, a, key, scratch_1);
|
||||
self.lwe_sample_extract(res, &tmp_glwe);
|
||||
let (mut tmp_glwe_rank_1, scratch_1) = scratch.take_glwe(&glwe_layout);
|
||||
|
||||
match a_idx {
|
||||
0 => {
|
||||
self.glwe_keyswitch(&mut tmp_glwe_rank_1, a, key, scratch_1);
|
||||
}
|
||||
_ => {
|
||||
let (mut tmp_glwe_in, scratch_2) = scratch_1.take_glwe(a);
|
||||
self.glwe_rotate(-(a_idx as i64), &mut tmp_glwe_in, a);
|
||||
self.glwe_keyswitch(&mut tmp_glwe_rank_1, &tmp_glwe_in, key, scratch_2);
|
||||
}
|
||||
}
|
||||
|
||||
self.lwe_sample_extract(res, &tmp_glwe_rank_1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,13 +124,13 @@ impl<D: DataMut> LWE<D> {
|
||||
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
|
||||
A: GLWEToRef,
|
||||
K: GGLWEPreparedToRef<BE> + GGLWEInfos,
|
||||
M: LWEFromGLWE<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
module.lwe_from_glwe(self, a, key, scratch);
|
||||
module.lwe_from_glwe(self, a, a_idx, key, scratch);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use poulpy_hal::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
GLWEDecrypt, GLWEEncryptSk, GLWEFromLWE, GLWEToLWESwitchingKeyEncryptSk, LWEDecrypt, LWEEncryptSk,
|
||||
GLWEDecrypt, GLWEEncryptSk, GLWEFromLWE, GLWEToLWESwitchingKeyEncryptSk, LWEDecrypt, LWEEncryptSk, LWEFromGLWE,
|
||||
LWEToGLWESwitchingKeyEncryptSk, ScratchTakeCore,
|
||||
layouts::{
|
||||
Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWEToLWEKey,
|
||||
@@ -110,6 +110,7 @@ where
|
||||
+ GLWEToLWESwitchingKeyEncryptSk<BE>
|
||||
+ GLWEEncryptSk<BE>
|
||||
+ LWEDecrypt<BE>
|
||||
+ LWEFromGLWE<BE>
|
||||
+ GLWEDecrypt<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GLWEToLWESwitchingKeyEncryptSk<BE>
|
||||
@@ -163,9 +164,14 @@ where
|
||||
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
|
||||
sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
|
||||
|
||||
let data: i64 = 17;
|
||||
let a_idx: usize = 1;
|
||||
|
||||
let mut data: Vec<i64> = vec![0i64; module.n()];
|
||||
data[a_idx] = 17;
|
||||
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);
|
||||
glwe_ct.encrypt_sk(
|
||||
@@ -193,10 +199,10 @@ where
|
||||
let mut ksk_prepared: GLWEToLWEKeyPrepared<Vec<u8>, BE> = GLWEToLWEKeyPrepared::alloc_from_infos(module, &ksk);
|
||||
ksk_prepared.prepare(module, &ksk, scratch.borrow());
|
||||
|
||||
lwe_ct.from_glwe(module, &glwe_ct, &ksk_prepared, scratch.borrow());
|
||||
lwe_ct.from_glwe(module, &glwe_ct, a_idx, &ksk_prepared, scratch.borrow());
|
||||
|
||||
let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos);
|
||||
lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe);
|
||||
|
||||
assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]);
|
||||
assert_eq!(glwe_pt.data.at(0, 0)[a_idx], lwe_pt.data.at(0, 0)[0]);
|
||||
}
|
||||
|
||||
@@ -1,36 +1,43 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use poulpy_core::layouts::GGSWPrepared;
|
||||
use poulpy_core::{GLWECopy, GLWEPacking, ScratchTakeCore, layouts::GGSWPrepared};
|
||||
use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch};
|
||||
|
||||
use crate::tfhe::bdd_arithmetic::{
|
||||
BitSize, ExecuteBDDCircuit, FheUint, FheUintPrepared, GetBitCircuitInfo, GetGGSWBit, UnsignedInteger, circuits,
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{
|
||||
BDDKeyPrepared, BitSize, ExecuteBDDCircuit, FheUint, FheUintPrepared, GetBitCircuitInfo, GetGGSWBit, UnsignedInteger,
|
||||
circuits,
|
||||
},
|
||||
blind_rotation::BlindRotationAlgo,
|
||||
};
|
||||
|
||||
impl<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>
|
||||
where
|
||||
Self: Sized + ExecuteBDDCircuit<T, BE>,
|
||||
Self: Sized + ExecuteBDDCircuit<T, BE> + GLWEPacking<BE> + GLWECopy,
|
||||
{
|
||||
/// 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,
|
||||
out: &mut FheUint<R, T>,
|
||||
circuit: &C,
|
||||
a: &FheUintPrepared<A, T, BE>,
|
||||
b: &FheUintPrepared<B, T, BE>,
|
||||
key: &BDDKeyPrepared<DK, BRA, BE>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
BRA: BlindRotationAlgo,
|
||||
DK: DataRef,
|
||||
C: GetBitCircuitInfo<T>,
|
||||
R: DataMut,
|
||||
A: 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
|
||||
let inputs: Vec<&dyn GetGGSWBit<BE>> = [a as &dyn GetGGSWBit<BE>, b as &dyn GetGGSWBit<BE>].to_vec();
|
||||
let helper: FheUintHelper<'_, T, BE> = FheUintHelper {
|
||||
@@ -38,8 +45,13 @@ where
|
||||
_phantom: PhantomData,
|
||||
};
|
||||
|
||||
let (mut out_bits, scratch_1) = scratch.take_glwe_slice(T::WORD_SIZE, out);
|
||||
|
||||
// Evaluates out[i] = circuit[i](a, b)
|
||||
self.execute_bdd_circuit(&mut out.bits, &helper, circuit, scratch);
|
||||
self.execute_bdd_circuit(&mut out_bits, &helper, circuit, scratch_1);
|
||||
|
||||
// Repacks the bits
|
||||
out.pack(self, out_bits, &key.cbt.atk, scratch_1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,16 +100,20 @@ macro_rules! define_bdd_2w_to_1w_trait {
|
||||
($(#[$meta:meta])* $vis:vis $trait_name:ident, $method_name:ident) => {
|
||||
$(#[$meta])*
|
||||
$vis trait $trait_name<T: UnsignedInteger, BE: Backend> {
|
||||
fn $method_name<A, M, B>(
|
||||
fn $method_name<A, M, K, BRA, B>(
|
||||
&mut self,
|
||||
module: &M,
|
||||
a: &FheUintPrepared<A, T, BE>,
|
||||
b: &FheUintPrepared<B, T, BE>,
|
||||
key: &BDDKeyPrepared<K, BRA, BE>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
M: ExecuteBDDCircuit2WTo1W<T, BE>,
|
||||
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 {
|
||||
($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> {
|
||||
fn $method_name<A, M, B>(
|
||||
fn $method_name<A, M, K, BRA, B>(
|
||||
&mut self,
|
||||
module: &M,
|
||||
a: &FheUintPrepared<A, $ty, BE>,
|
||||
b: &FheUintPrepared<B, $ty, BE>,
|
||||
key: &BDDKeyPrepared<K, BRA, BE>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
M: ExecuteBDDCircuit2WTo1W<$ty, BE>,
|
||||
A: 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)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use itertools::Itertools;
|
||||
use poulpy_core::{
|
||||
GLWECopy, GLWEDecrypt, GLWEEncryptSk, GLWEPacking, ScratchTakeCore,
|
||||
GLWECopy, GLWEDecrypt, GLWEEncryptSk, GLWEPacking, GLWERotate, LWEFromGLWE, ScratchTakeCore,
|
||||
layouts::{
|
||||
GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, LWEInfos, TorusPrecision,
|
||||
prepared::GLWEAutomorphismKeyPrepared,
|
||||
Base2K, Degree, GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, GLWEToRef,
|
||||
LWEInfos, LWEToMut, Rank, TorusPrecision, prepared::GLWEAutomorphismKeyPrepared,
|
||||
},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
@@ -15,58 +15,49 @@ use std::{collections::HashMap, marker::PhantomData};
|
||||
|
||||
use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger};
|
||||
|
||||
/// An FHE ciphertext encrypting the bits of an [UnsignedInteger] in compressed format (ideal for decryption/serialization).
|
||||
pub struct FheUintCompressed<D: Data, T: UnsignedInteger>(pub(crate) GLWE<D>, pub(crate) PhantomData<T>);
|
||||
/// An FHE ciphertext encrypting the bits of an [UnsignedInteger].
|
||||
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> {
|
||||
#[allow(dead_code)]
|
||||
fn post_process<ATK, M, BE: Backend>(
|
||||
&mut self,
|
||||
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>,
|
||||
impl<T: UnsignedInteger> FheUint<Vec<u8>, T> {
|
||||
pub fn alloc_from_infos<A>(infos: &A) -> Self
|
||||
where
|
||||
A: GLWEInfos,
|
||||
{
|
||||
// Repacks the GLWE ciphertexts bits
|
||||
let gap: usize = module.n() / T::WORD_SIZE;
|
||||
let log_gap: usize = (usize::BITS - (gap - 1).leading_zeros()) as usize;
|
||||
let mut cts: HashMap<usize, &mut GLWE<&mut [u8]>> = HashMap::new();
|
||||
for (i, ct) in tmp_res.iter_mut().enumerate().take(T::WORD_SIZE) {
|
||||
cts.insert(i * gap, ct);
|
||||
Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank())
|
||||
}
|
||||
|
||||
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());
|
||||
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
|
||||
Self {
|
||||
bits: GLWE::alloc(n, base2k, k, rank),
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
self.0.base2k()
|
||||
self.bits.base2k()
|
||||
}
|
||||
|
||||
fn k(&self) -> poulpy_core::layouts::TorusPrecision {
|
||||
self.0.k()
|
||||
self.bits.k()
|
||||
}
|
||||
|
||||
fn n(&self) -> poulpy_core::layouts::Degree {
|
||||
self.0.n()
|
||||
self.bits.n()
|
||||
}
|
||||
}
|
||||
|
||||
impl<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 {
|
||||
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>(
|
||||
&mut self,
|
||||
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);
|
||||
|
||||
pt.encode_vec_i64(&data_bits, TorusPrecision(1));
|
||||
self.0
|
||||
pt.encode_vec_i64(&data_bits, TorusPrecision(2));
|
||||
self.bits
|
||||
.encrypt_sk(module, &pt, sk, source_xa, source_xe, scratch_1);
|
||||
}
|
||||
}
|
||||
|
||||
impl<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
|
||||
where
|
||||
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);
|
||||
|
||||
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()];
|
||||
|
||||
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();
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use crate::tfhe::bdd_arithmetic::{BddKeyPrepared, FheUintBlockDebugPrepare, ToBits};
|
||||
use crate::tfhe::bdd_arithmetic::{BDDKeyPrepared, FheUint, FheUintBlockDebugPrepare, ToBits};
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{FheUint, UnsignedInteger},
|
||||
blind_rotation::BlindRotationAlgo,
|
||||
circuit_bootstrapping::CirtuitBootstrappingExecute,
|
||||
bdd_arithmetic::UnsignedInteger, blind_rotation::BlindRotationAlgo, circuit_bootstrapping::CirtuitBootstrappingExecute,
|
||||
};
|
||||
use poulpy_core::GGSWNoise;
|
||||
|
||||
@@ -113,25 +111,23 @@ impl<D: DataRef, T: UnsignedInteger + ToBits> FheUintPreparedDebug<D, T> {
|
||||
|
||||
impl<BRA: BlindRotationAlgo, BE: Backend, T: UnsignedInteger> FheUintBlockDebugPrepare<BRA, T, BE> for Module<BE>
|
||||
where
|
||||
Self: LWEFromGLWE<BE> + CirtuitBootstrappingExecute<BRA, BE> + GGSWPreparedFactory<BE>,
|
||||
Self: ModuleN + LWEFromGLWE<BE> + CirtuitBootstrappingExecute<BRA, BE> + GGSWPreparedFactory<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
fn fhe_uint_debug_prepare<DM, DR0, DR1>(
|
||||
&self,
|
||||
res: &mut FheUintPreparedDebug<DM, T>,
|
||||
bits: &FheUint<DR0, T>,
|
||||
key: &BddKeyPrepared<DR1, BRA, BE>,
|
||||
key: &BDDKeyPrepared<DR1, BRA, BE>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
DM: DataMut,
|
||||
DR0: DataRef,
|
||||
DR1: DataRef,
|
||||
{
|
||||
assert_eq!(res.bits.len(), bits.bits.len());
|
||||
|
||||
let mut lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&bits.bits[0]); //TODO: add TakeLWE
|
||||
for (dst, src) in res.bits.iter_mut().zip(bits.bits.iter()) {
|
||||
lwe.from_glwe(self, src, &key.ks, scratch);
|
||||
let mut lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(bits); //TODO: add TakeLWE
|
||||
for (bit, dst) in res.bits.iter_mut().enumerate() {
|
||||
bits.get_bit(self, bit, &mut lwe, &key.ks, scratch);
|
||||
key.cbt.execute_to_constant(self, dst, &lwe, 1, 1, scratch);
|
||||
}
|
||||
}
|
||||
@@ -142,7 +138,7 @@ impl<D: DataMut, T: UnsignedInteger> FheUintPreparedDebug<D, T> {
|
||||
&mut self,
|
||||
module: &M,
|
||||
other: &FheUint<O, T>,
|
||||
key: &BddKeyPrepared<K, BRA, BE>,
|
||||
key: &BDDKeyPrepared<K, BRA, BE>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
BRA: BlindRotationAlgo,
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
mod fhe_uint_compressed;
|
||||
mod fhe_uint;
|
||||
mod fhe_uint_prepared;
|
||||
mod fheuint;
|
||||
|
||||
mod fhe_uint_prepared_debug;
|
||||
|
||||
pub use fhe_uint_prepared_debug::*;
|
||||
|
||||
pub use fhe_uint_compressed::*;
|
||||
pub use fhe_uint::*;
|
||||
pub use fhe_uint_prepared::*;
|
||||
pub use fheuint::*;
|
||||
|
||||
@@ -7,6 +7,7 @@ use crate::tfhe::{
|
||||
CircuitBootstrappingKeyPrepared, CircuitBootstrappingKeyPreparedFactory, CirtuitBootstrappingExecute,
|
||||
},
|
||||
};
|
||||
|
||||
use poulpy_core::{
|
||||
GLWEToLWESwitchingKeyEncryptSk, GetDistribution, LWEFromGLWE, ScratchTakeCore,
|
||||
layouts::{
|
||||
@@ -45,8 +46,8 @@ where
|
||||
D: Data,
|
||||
BRA: BlindRotationAlgo,
|
||||
{
|
||||
cbt: CircuitBootstrappingKey<D, BRA>,
|
||||
ks: GLWEToLWEKey<D>,
|
||||
pub(crate) cbt: CircuitBootstrappingKey<D, BRA>,
|
||||
pub(crate) ks: GLWEToLWEKey<D>,
|
||||
}
|
||||
|
||||
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
|
||||
D: Data,
|
||||
BRA: BlindRotationAlgo,
|
||||
@@ -137,11 +138,11 @@ pub trait BDDKeyPreparedFactory<BRA: BlindRotationAlgo, BE: Backend>
|
||||
where
|
||||
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
|
||||
A: BDDKeyInfos,
|
||||
{
|
||||
BddKeyPrepared {
|
||||
BDDKeyPrepared {
|
||||
cbt: CircuitBootstrappingKeyPrepared::alloc_from_infos(self, &infos.cbt_infos()),
|
||||
ks: GLWEToLWEKeyPrepared::alloc_from_infos(self, &infos.ks_infos()),
|
||||
}
|
||||
@@ -155,7 +156,7 @@ where
|
||||
.max(self.prepare_glwe_to_lwe_key_tmp_bytes(&infos.ks_infos()))
|
||||
}
|
||||
|
||||
fn prepare_bdd_key<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
|
||||
DM: DataMut,
|
||||
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
|
||||
where
|
||||
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>)
|
||||
where
|
||||
DR: DataRef,
|
||||
@@ -200,7 +201,7 @@ pub trait FheUintBlocksPrepare<BRA: BlindRotationAlgo, T: UnsignedInteger, BE: B
|
||||
&self,
|
||||
res: &mut FheUintPrepared<DM, T, BE>,
|
||||
bits: &FheUint<DR0, T>,
|
||||
key: &BddKeyPrepared<DR1, BRA, BE>,
|
||||
key: &BDDKeyPrepared<DR1, BRA, BE>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
DM: DataMut,
|
||||
@@ -230,19 +231,17 @@ where
|
||||
&self,
|
||||
res: &mut FheUintPrepared<DM, T, BE>,
|
||||
bits: &FheUint<DR0, T>,
|
||||
key: &BddKeyPrepared<DR1, BRA, BE>,
|
||||
key: &BDDKeyPrepared<DR1, BRA, BE>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
DM: DataMut,
|
||||
DR0: DataRef,
|
||||
DR1: DataRef,
|
||||
{
|
||||
assert_eq!(res.bits.len(), bits.bits.len());
|
||||
|
||||
let mut lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&bits.bits[0]); //TODO: add TakeLWE
|
||||
let mut lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(bits); //TODO: add TakeLWE
|
||||
let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(res);
|
||||
for (dst, src) in res.bits.iter_mut().zip(bits.bits.iter()) {
|
||||
lwe.from_glwe(self, src, &key.ks, scratch_1);
|
||||
for (bit, dst) in res.bits.iter_mut().enumerate() {
|
||||
bits.get_bit(self, bit, &mut lwe, &key.ks, scratch_1);
|
||||
key.cbt
|
||||
.execute_to_constant(self, &mut tmp_ggsw, &lwe, 1, 1, scratch_1);
|
||||
dst.prepare(self, &tmp_ggsw, scratch_1);
|
||||
@@ -255,7 +254,7 @@ impl<D: DataMut, T: UnsignedInteger, BE: Backend> FheUintPrepared<D, T, BE> {
|
||||
&mut self,
|
||||
module: &M,
|
||||
other: &FheUint<O, T>,
|
||||
key: &BddKeyPrepared<K, BRA, BE>,
|
||||
key: &BDDKeyPrepared<K, BRA, BE>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
BRA: BlindRotationAlgo,
|
||||
@@ -273,7 +272,7 @@ pub trait FheUintBlockDebugPrepare<BRA: BlindRotationAlgo, T: UnsignedInteger, B
|
||||
&self,
|
||||
res: &mut FheUintPreparedDebug<DM, T>,
|
||||
bits: &FheUint<DR0, T>,
|
||||
key: &BddKeyPrepared<DR1, BRA, BE>,
|
||||
key: &BDDKeyPrepared<DR1, BRA, BE>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
DM: DataMut,
|
||||
|
||||
@@ -1,74 +1,80 @@
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use poulpy_backend::FFT64Ref;
|
||||
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::tests::test_suite::{
|
||||
test_bdd_add, test_bdd_and, test_bdd_or, test_bdd_prepare, test_bdd_sll, test_bdd_slt, test_bdd_sltu, test_bdd_sra,
|
||||
test_bdd_srl, test_bdd_sub, test_bdd_xor, test_glwe_to_glwe_blind_rotation, test_scalar_to_ggsw_blind_rotation,
|
||||
TestContext, test_bdd_add, test_bdd_and, test_bdd_or, test_bdd_prepare, test_bdd_sll, test_bdd_slt, test_bdd_sltu,
|
||||
test_bdd_sra, test_bdd_srl, test_bdd_sub, test_bdd_xor, test_glwe_to_glwe_blind_rotation,
|
||||
test_scalar_to_ggsw_blind_rotation,
|
||||
},
|
||||
blind_rotation::CGGI,
|
||||
};
|
||||
|
||||
static TEST_CONTEXT_CGGI_FFT64_REF: LazyLock<TestContext<CGGI, FFT64Ref>> =
|
||||
LazyLock::new(|| TestContext::<CGGI, FFT64Ref>::new());
|
||||
|
||||
#[test]
|
||||
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]
|
||||
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]
|
||||
fn test_bdd_prepare_fft64_ref() {
|
||||
test_bdd_prepare::<CGGI, FFT64Ref>()
|
||||
test_bdd_prepare(&TEST_CONTEXT_CGGI_FFT64_REF)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bdd_add_fft64_ref() {
|
||||
test_bdd_add::<CGGI, FFT64Ref>()
|
||||
test_bdd_add(&TEST_CONTEXT_CGGI_FFT64_REF)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bdd_and_fft64_ref() {
|
||||
test_bdd_and::<CGGI, FFT64Ref>()
|
||||
test_bdd_and(&TEST_CONTEXT_CGGI_FFT64_REF)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bdd_or_fft64_ref() {
|
||||
test_bdd_or::<CGGI, FFT64Ref>()
|
||||
test_bdd_or(&TEST_CONTEXT_CGGI_FFT64_REF)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bdd_sll_fft64_ref() {
|
||||
test_bdd_sll::<CGGI, FFT64Ref>()
|
||||
test_bdd_sll(&TEST_CONTEXT_CGGI_FFT64_REF)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bdd_slt_fft64_ref() {
|
||||
test_bdd_slt::<CGGI, FFT64Ref>()
|
||||
test_bdd_slt(&TEST_CONTEXT_CGGI_FFT64_REF)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bdd_sltu_fft64_ref() {
|
||||
test_bdd_sltu::<CGGI, FFT64Ref>()
|
||||
test_bdd_sltu(&TEST_CONTEXT_CGGI_FFT64_REF)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bdd_sra_fft64_ref() {
|
||||
test_bdd_sra::<CGGI, FFT64Ref>()
|
||||
test_bdd_sra(&TEST_CONTEXT_CGGI_FFT64_REF)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bdd_srl_fft64_ref() {
|
||||
test_bdd_srl::<CGGI, FFT64Ref>()
|
||||
test_bdd_srl(&TEST_CONTEXT_CGGI_FFT64_REF)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bdd_sub_fft64_ref() {
|
||||
test_bdd_sub::<CGGI, FFT64Ref>()
|
||||
test_bdd_sub(&TEST_CONTEXT_CGGI_FFT64_REF)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bdd_xor_fft64_ref() {
|
||||
test_bdd_xor::<CGGI, FFT64Ref>()
|
||||
test_bdd_xor(&TEST_CONTEXT_CGGI_FFT64_REF)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use poulpy_core::{
|
||||
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecretPrepared, GLWESecretPreparedFactory},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
@@ -11,14 +11,14 @@ use rand::RngCore;
|
||||
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{
|
||||
Add, BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
|
||||
Add, BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
|
||||
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
|
||||
},
|
||||
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
|
||||
};
|
||||
|
||||
pub fn test_bdd_add<BRA: BlindRotationAlgo, BE: Backend>()
|
||||
pub fn test_bdd_add<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
|
||||
where
|
||||
Module<BE>: ModuleNew<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
@@ -40,52 +40,53 @@ where
|
||||
let glwe_infos: GLWELayout = TEST_GLWE_INFOS;
|
||||
let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS;
|
||||
|
||||
let n_glwe: usize = glwe_infos.n().into();
|
||||
let module: &Module<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_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_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: 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 mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&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 b: u32 = source.next_u32();
|
||||
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
a_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
a,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
b_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
b,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
// d + a
|
||||
res.add(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
|
||||
// a + b
|
||||
res.add(
|
||||
module,
|
||||
&a_enc_prep,
|
||||
&b_enc_prep,
|
||||
bdd_key_prepared,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
res.decrypt(&module, &sk_glwe_prep, scratch.borrow()),
|
||||
res.decrypt(module, sk_glwe_prep, scratch.borrow()),
|
||||
a.wrapping_add(b)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use poulpy_core::{
|
||||
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
@@ -11,14 +11,14 @@ use rand::RngCore;
|
||||
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{
|
||||
And, BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
|
||||
And, BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
|
||||
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
|
||||
},
|
||||
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
|
||||
};
|
||||
|
||||
pub fn test_bdd_and<BRA: BlindRotationAlgo, BE: Backend>()
|
||||
pub fn test_bdd_and<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
|
||||
where
|
||||
Module<BE>: ModuleNew<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
@@ -40,48 +40,49 @@ where
|
||||
let glwe_infos: GLWELayout = TEST_GLWE_INFOS;
|
||||
let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS;
|
||||
|
||||
let n_glwe: usize = glwe_infos.n().into();
|
||||
let module: &Module<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_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_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: 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 mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&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 b: u32 = source.next_u32();
|
||||
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
a_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
a,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
b_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
b,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
res.and(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
|
||||
res.and(
|
||||
module,
|
||||
&a_enc_prep,
|
||||
&b_enc_prep,
|
||||
bdd_key_prepared,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
assert_eq!(res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), a & b);
|
||||
assert_eq!(res.decrypt(module, sk_glwe_prep, scratch.borrow()), a & b);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use poulpy_core::{
|
||||
GGSWEncryptSk, GGSWNoise, GLWEDecrypt, GLWEEncryptSk, SIGMA, ScratchTakeCore,
|
||||
layouts::{
|
||||
Base2K, Degree, Dnum, Dsize, GGSW, GGSWLayout, GGSWPreparedFactory, GLWESecret, GLWESecretPrepared,
|
||||
GLWESecretPreparedFactory, LWEInfos, Rank, TorusPrecision,
|
||||
Base2K, Dnum, Dsize, GGSW, GGSWLayout, GGSWPreparedFactory, GLWESecretPrepared, GLWESecretPreparedFactory, LWEInfos,
|
||||
Rank, TorusPrecision,
|
||||
},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
@@ -12,9 +12,15 @@ use poulpy_hal::{
|
||||
};
|
||||
use rand::RngCore;
|
||||
|
||||
use crate::tfhe::bdd_arithmetic::{FheUintPrepared, GGSWBlindRotation};
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{
|
||||
FheUintPrepared, GGSWBlindRotation,
|
||||
tests::test_suite::{TEST_BASE2K, TEST_RANK, TestContext},
|
||||
},
|
||||
blind_rotation::BlindRotationAlgo,
|
||||
};
|
||||
|
||||
pub fn test_scalar_to_ggsw_blind_rotation<BE: Backend>()
|
||||
pub fn test_scalar_to_ggsw_blind_rotation<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
|
||||
where
|
||||
Module<BE>: ModuleNew<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
@@ -28,14 +34,16 @@ where
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
let n: Degree = Degree(1 << 11);
|
||||
let base2k: Base2K = Base2K(13);
|
||||
let rank: Rank = Rank(1);
|
||||
let module: &Module<BE> = &test_context.module;
|
||||
let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
|
||||
|
||||
let base2k: Base2K = TEST_BASE2K.into();
|
||||
let rank: Rank = TEST_RANK.into();
|
||||
let k_ggsw_res: TorusPrecision = TorusPrecision(39);
|
||||
let k_ggsw_apply: TorusPrecision = TorusPrecision(52);
|
||||
|
||||
let ggsw_res_infos: GGSWLayout = GGSWLayout {
|
||||
n,
|
||||
n: module.n().into(),
|
||||
base2k,
|
||||
k: k_ggsw_res,
|
||||
rank,
|
||||
@@ -44,7 +52,7 @@ where
|
||||
};
|
||||
|
||||
let ggsw_k_infos: GGSWLayout = GGSWLayout {
|
||||
n,
|
||||
n: module.n().into(),
|
||||
base2k,
|
||||
k: k_ggsw_apply,
|
||||
rank,
|
||||
@@ -52,24 +60,14 @@ where
|
||||
dsize: Dsize(1),
|
||||
};
|
||||
|
||||
let n_glwe: usize = n.into();
|
||||
|
||||
let module: Module<BE> = Module::<BE>::new(n_glwe as u64);
|
||||
let mut source: Source = Source::new([6u8; 32]);
|
||||
let mut source_xs: Source = Source::new([1u8; 32]);
|
||||
let mut source_xa: Source = Source::new([2u8; 32]);
|
||||
let mut source_xe: Source = Source::new([3u8; 32]);
|
||||
|
||||
let mut scratch: ScratchOwned<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 scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n_glwe, 1);
|
||||
let mut scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(module.n(), 1);
|
||||
scalar
|
||||
.raw_mut()
|
||||
.iter_mut()
|
||||
@@ -80,17 +78,17 @@ where
|
||||
|
||||
// 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(
|
||||
&module,
|
||||
module,
|
||||
k,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
let base: [usize; 2] = [6, 5];
|
||||
let base: [usize; 2] = [module.log_n() >> 1, module.log_n() - (module.log_n() >> 1)];
|
||||
|
||||
assert_eq!(base.iter().sum::<usize>(), module.log_n());
|
||||
|
||||
@@ -98,7 +96,7 @@ where
|
||||
let mut bit_start: usize = 0;
|
||||
|
||||
let max_noise = |col_i: usize| {
|
||||
let mut noise: f64 = -(ggsw_res_infos.size() as f64 * base2k.as_usize() as f64) + SIGMA.log2() + 2.0;
|
||||
let mut noise: f64 = -(ggsw_res_infos.size() as f64 * base2k.as_usize() as f64) + SIGMA.log2() + 3.0;
|
||||
noise += 0.5 * ggsw_res_infos.log_n() as f64;
|
||||
if col_i != 0 {
|
||||
noise += 0.5 * ggsw_res_infos.log_n() as f64
|
||||
@@ -136,7 +134,7 @@ where
|
||||
|
||||
// res.print_noise(&module, &sk_glwe_prep, &scalar_want);
|
||||
|
||||
res.assert_noise(&module, &sk_glwe_prep, &scalar_want, &max_noise);
|
||||
res.assert_noise(module, sk_glwe_prep, &scalar_want, &max_noise);
|
||||
|
||||
bit_step += digit;
|
||||
bit_start += digit;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use poulpy_core::{
|
||||
GGSWEncryptSk, GLWEDecrypt, GLWEEncryptSk, ScratchTakeCore,
|
||||
layouts::{
|
||||
Base2K, Degree, Dnum, Dsize, GGSWLayout, GGSWPreparedFactory, GLWE, GLWELayout, GLWEPlaintext, GLWESecret,
|
||||
GLWESecretPrepared, GLWESecretPreparedFactory, LWEInfos, Rank, TorusPrecision,
|
||||
Base2K, Dnum, Dsize, GGSWLayout, GGSWPreparedFactory, GLWE, GLWELayout, GLWEPlaintext, GLWESecretPrepared,
|
||||
GLWESecretPreparedFactory, Rank, TorusPrecision,
|
||||
},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
@@ -12,9 +12,15 @@ use poulpy_hal::{
|
||||
};
|
||||
use rand::RngCore;
|
||||
|
||||
use crate::tfhe::bdd_arithmetic::{FheUintPrepared, GLWEBlindRotation};
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{
|
||||
FheUintPrepared, GLWEBlindRotation,
|
||||
tests::test_suite::{TEST_BASE2K, TEST_RANK, TestContext},
|
||||
},
|
||||
blind_rotation::BlindRotationAlgo,
|
||||
};
|
||||
|
||||
pub fn test_glwe_to_glwe_blind_rotation<BE: Backend>()
|
||||
pub fn test_glwe_to_glwe_blind_rotation<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
|
||||
where
|
||||
Module<BE>: ModuleNew<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
@@ -26,21 +32,23 @@ where
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
let n: Degree = Degree(1 << 11);
|
||||
let base2k: Base2K = Base2K(13);
|
||||
let rank: Rank = Rank(1);
|
||||
let module: &Module<BE> = &test_context.module;
|
||||
let sk_glwe_prep: &GLWESecretPrepared<Vec<u8>, BE> = &test_context.sk_glwe;
|
||||
|
||||
let base2k: Base2K = TEST_BASE2K.into();
|
||||
let rank: Rank = TEST_RANK.into();
|
||||
let k_glwe: TorusPrecision = TorusPrecision(26);
|
||||
let k_ggsw: TorusPrecision = TorusPrecision(39);
|
||||
let dnum: Dnum = Dnum(3);
|
||||
|
||||
let glwe_infos: GLWELayout = GLWELayout {
|
||||
n,
|
||||
n: module.n().into(),
|
||||
base2k,
|
||||
k: k_glwe,
|
||||
rank,
|
||||
};
|
||||
let ggsw_infos: GGSWLayout = GGSWLayout {
|
||||
n,
|
||||
n: module.n().into(),
|
||||
base2k,
|
||||
k: k_ggsw,
|
||||
rank,
|
||||
@@ -48,21 +56,12 @@ where
|
||||
dsize: Dsize(1),
|
||||
};
|
||||
|
||||
let n_glwe: usize = glwe_infos.n().into();
|
||||
|
||||
let module: Module<BE> = Module::<BE>::new(n_glwe as u64);
|
||||
let mut source: Source = Source::new([6u8; 32]);
|
||||
let mut source_xs: Source = Source::new([1u8; 32]);
|
||||
let mut source_xa: Source = Source::new([2u8; 32]);
|
||||
let mut source_xe: Source = Source::new([3u8; 32]);
|
||||
|
||||
let mut scratch: ScratchOwned<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 test_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
|
||||
@@ -72,17 +71,17 @@ where
|
||||
|
||||
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(
|
||||
&module,
|
||||
module,
|
||||
k,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
let base: [usize; 2] = [6, 5];
|
||||
let base: [usize; 2] = [module.log_n() >> 1, module.log_n() - (module.log_n() >> 1)];
|
||||
|
||||
assert_eq!(base.iter().sum::<usize>(), module.log_n());
|
||||
|
||||
@@ -112,7 +111,7 @@ where
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
res.decrypt(&module, &mut pt, &sk_glwe_prep, scratch.borrow());
|
||||
res.decrypt(module, &mut pt, sk_glwe_prep, scratch.borrow());
|
||||
|
||||
assert_eq!(
|
||||
(((k >> bit_start) & mask) << bit_step) as i64,
|
||||
|
||||
@@ -17,6 +17,11 @@ pub use and::*;
|
||||
pub use ggsw_blind_rotations::*;
|
||||
pub use glwe_blind_rotation::*;
|
||||
pub use or::*;
|
||||
use poulpy_hal::{
|
||||
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
layouts::{Backend, Module, Scratch, ScratchOwned},
|
||||
source::Source,
|
||||
};
|
||||
pub use prepare::*;
|
||||
pub use sll::*;
|
||||
pub use slt::*;
|
||||
@@ -26,15 +31,96 @@ pub use srl::*;
|
||||
pub use sub::*;
|
||||
pub use xor::*;
|
||||
|
||||
use poulpy_core::layouts::{
|
||||
Base2K, Degree, Dnum, Dsize, GGSWLayout, GLWEAutomorphismKeyLayout, GLWELayout, GLWETensorKeyLayout, GLWEToLWEKeyLayout,
|
||||
Rank, TorusPrecision,
|
||||
use poulpy_core::{
|
||||
ScratchTakeCore,
|
||||
layouts::{
|
||||
Base2K, Degree, Dnum, Dsize, GGSWLayout, GLWEAutomorphismKeyLayout, GLWELayout, GLWESecret, GLWESecretPrepared,
|
||||
GLWESecretPreparedFactory, GLWETensorKeyLayout, GLWEToLWEKeyLayout, LWESecret, Rank, TorusPrecision,
|
||||
},
|
||||
};
|
||||
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::BDDKeyLayout, blind_rotation::BlindRotationKeyLayout, circuit_bootstrapping::CircuitBootstrappingKeyLayout,
|
||||
bdd_arithmetic::{BDDKey, BDDKeyEncryptSk, BDDKeyLayout, BDDKeyPrepared, BDDKeyPreparedFactory},
|
||||
blind_rotation::{
|
||||
BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory, BlindRotationKeyLayout, BlindRotationKeyPreparedFactory,
|
||||
},
|
||||
circuit_bootstrapping::CircuitBootstrappingKeyLayout,
|
||||
};
|
||||
|
||||
pub struct TestContext<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_LWE: u32 = 77;
|
||||
pub(crate) const TEST_BASE2K: u32 = 13;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use poulpy_core::{
|
||||
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
@@ -11,14 +11,14 @@ use rand::RngCore;
|
||||
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{
|
||||
BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
|
||||
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Or,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
|
||||
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
|
||||
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Or,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
|
||||
},
|
||||
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
|
||||
};
|
||||
|
||||
pub fn test_bdd_or<BRA: BlindRotationAlgo, BE: Backend>()
|
||||
pub fn test_bdd_or<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
|
||||
where
|
||||
Module<BE>: ModuleNew<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
@@ -40,48 +40,49 @@ where
|
||||
let glwe_infos: GLWELayout = TEST_GLWE_INFOS;
|
||||
let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS;
|
||||
|
||||
let n_glwe: usize = glwe_infos.n().into();
|
||||
let module: &Module<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_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_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: 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 mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&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 b: u32 = source.next_u32();
|
||||
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
a_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
a,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
b_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
b,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
res.or(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
|
||||
res.or(
|
||||
module,
|
||||
&a_enc_prep,
|
||||
&b_enc_prep,
|
||||
bdd_key_prepared,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
assert_eq!(res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), a | b);
|
||||
assert_eq!(res.decrypt(module, sk_glwe_prep, scratch.borrow()), a | b);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use poulpy_core::{
|
||||
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, SIGMA, ScratchTakeCore,
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, LWESecret, prepared::GLWESecretPrepared},
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
@@ -11,15 +11,14 @@ use rand::RngCore;
|
||||
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{
|
||||
BDDKey, BDDKeyEncryptSk, BDDKeyLayout, BDDKeyPreparedFactory, BddKeyPrepared, ExecuteBDDCircuit2WTo1W, FheUint,
|
||||
FheUintBlockDebugPrepare, FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory,
|
||||
FheUintPreparedDebug,
|
||||
tests::test_suite::{TEST_BASE2K, TEST_BDD_KEY_LAYOUT, TEST_BLOCK_SIZE, TEST_GGSW_INFOS, TEST_GLWE_INFOS, TEST_N_LWE},
|
||||
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
|
||||
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPreparedDebug,
|
||||
tests::test_suite::{TEST_BASE2K, TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
|
||||
},
|
||||
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
|
||||
};
|
||||
|
||||
pub fn test_bdd_prepare<BRA: BlindRotationAlgo, BE: Backend>()
|
||||
pub fn test_bdd_prepare<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
|
||||
where
|
||||
Module<BE>: ModuleNew<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
@@ -41,55 +40,24 @@ where
|
||||
let glwe_infos: GLWELayout = TEST_GLWE_INFOS;
|
||||
let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS;
|
||||
|
||||
let n_glwe: usize = glwe_infos.n().into();
|
||||
let module: &Module<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_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);
|
||||
|
||||
// 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)
|
||||
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();
|
||||
c_enc.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
value,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
@@ -97,13 +65,13 @@ where
|
||||
|
||||
// GGSW(0)
|
||||
let mut c_enc_prep_debug: FheUintPreparedDebug<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)
|
||||
c_enc_prep_debug.prepare(&module, &c_enc, &bdd_key_prepared, scratch.borrow());
|
||||
c_enc_prep_debug.prepare(module, &c_enc, bdd_key_prepared, scratch.borrow());
|
||||
|
||||
let max_noise = |col_i: usize| {
|
||||
let mut noise: f64 = -(ggsw_infos.size() as f64 * TEST_BASE2K as f64) + SIGMA.log2() + 1.0;
|
||||
let mut noise: f64 = -(ggsw_infos.size() as f64 * TEST_BASE2K as f64) + SIGMA.log2() + 2.0;
|
||||
noise += 0.5 * ggsw_infos.log_n() as f64;
|
||||
if col_i != 0 {
|
||||
noise += 0.5 * ggsw_infos.log_n() as f64
|
||||
@@ -111,7 +79,7 @@ where
|
||||
noise
|
||||
};
|
||||
|
||||
// c_enc_prep_debug.print_noise(&module, &sk_glwe_prep, value);
|
||||
// c_enc_prep_debug.print_noise(module, sk_glwe_prep, value);
|
||||
|
||||
c_enc_prep_debug.assert_noise(&module, &sk_glwe_prep, value, &max_noise);
|
||||
c_enc_prep_debug.assert_noise(module, sk_glwe_prep, value, &max_noise);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use poulpy_core::{
|
||||
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
@@ -11,14 +11,14 @@ use rand::RngCore;
|
||||
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{
|
||||
BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
|
||||
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Sll,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
|
||||
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
|
||||
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Sll,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
|
||||
},
|
||||
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
|
||||
};
|
||||
|
||||
pub fn test_bdd_sll<BRA: BlindRotationAlgo, BE: Backend>()
|
||||
pub fn test_bdd_sll<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
|
||||
where
|
||||
Module<BE>: ModuleNew<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
@@ -40,51 +40,52 @@ where
|
||||
let glwe_infos: GLWELayout = TEST_GLWE_INFOS;
|
||||
let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS;
|
||||
|
||||
let n_glwe: usize = glwe_infos.n().into();
|
||||
let module: &Module<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_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_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: 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 mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&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 b: u32 = source.next_u32() & 15;
|
||||
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
a_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
a,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
b_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
b,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
res.sll(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
|
||||
res.sll(
|
||||
module,
|
||||
&a_enc_prep,
|
||||
&b_enc_prep,
|
||||
bdd_key_prepared,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
res.decrypt(&module, &sk_glwe_prep, scratch.borrow()),
|
||||
res.decrypt(module, sk_glwe_prep, scratch.borrow()),
|
||||
a.wrapping_shl(b)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use poulpy_core::{
|
||||
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
@@ -11,14 +11,14 @@ use rand::RngCore;
|
||||
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{
|
||||
BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
|
||||
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Slt,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
|
||||
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
|
||||
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Slt,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
|
||||
},
|
||||
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
|
||||
};
|
||||
|
||||
pub fn test_bdd_slt<BRA: BlindRotationAlgo, BE: Backend>()
|
||||
pub fn test_bdd_slt<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
|
||||
where
|
||||
Module<BE>: ModuleNew<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
@@ -40,52 +40,53 @@ where
|
||||
let glwe_infos: GLWELayout = TEST_GLWE_INFOS;
|
||||
let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS;
|
||||
|
||||
let n_glwe: usize = glwe_infos.n().into();
|
||||
let module: &Module<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_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_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: 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 mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&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 b: u32 = source.next_u32();
|
||||
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
a_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
a,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
b_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
b,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
// d + a
|
||||
res.slt(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
|
||||
res.slt(
|
||||
module,
|
||||
&a_enc_prep,
|
||||
&b_enc_prep,
|
||||
bdd_key_prepared,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
res.decrypt(&module, &sk_glwe_prep, scratch.borrow()),
|
||||
res.decrypt(module, sk_glwe_prep, scratch.borrow()),
|
||||
((a as i32) < (b as i32)) as u32
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use poulpy_core::{
|
||||
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
@@ -11,14 +11,14 @@ use rand::RngCore;
|
||||
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{
|
||||
BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
|
||||
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Sltu,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
|
||||
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
|
||||
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Sltu,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
|
||||
},
|
||||
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
|
||||
};
|
||||
|
||||
pub fn test_bdd_sltu<BRA: BlindRotationAlgo, BE: Backend>()
|
||||
pub fn test_bdd_sltu<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
|
||||
where
|
||||
Module<BE>: ModuleNew<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
@@ -40,52 +40,53 @@ where
|
||||
let glwe_infos: GLWELayout = TEST_GLWE_INFOS;
|
||||
let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS;
|
||||
|
||||
let n_glwe: usize = glwe_infos.n().into();
|
||||
let module: &Module<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_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_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: 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 mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&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 b: u32 = source.next_u32();
|
||||
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
a_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
a,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
b_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
b,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
// d + a
|
||||
res.sltu(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
|
||||
res.sltu(
|
||||
module,
|
||||
&a_enc_prep,
|
||||
&b_enc_prep,
|
||||
bdd_key_prepared,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
res.decrypt(&module, &sk_glwe_prep, scratch.borrow()),
|
||||
res.decrypt(module, sk_glwe_prep, scratch.borrow()),
|
||||
(a < b) as u32
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use poulpy_core::{
|
||||
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
@@ -11,14 +11,14 @@ use rand::RngCore;
|
||||
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{
|
||||
BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
|
||||
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Sra,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
|
||||
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
|
||||
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Sra,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
|
||||
},
|
||||
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
|
||||
};
|
||||
|
||||
pub fn test_bdd_sra<BRA: BlindRotationAlgo, BE: Backend>()
|
||||
pub fn test_bdd_sra<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
|
||||
where
|
||||
Module<BE>: ModuleNew<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
@@ -40,51 +40,52 @@ where
|
||||
let glwe_infos: GLWELayout = TEST_GLWE_INFOS;
|
||||
let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS;
|
||||
|
||||
let n_glwe: usize = glwe_infos.n().into();
|
||||
let module: &Module<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_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_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: 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 mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&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 b: u32 = source.next_u32() & 15;
|
||||
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
a_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
a,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
b_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
b,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
res.sra(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
|
||||
res.sra(
|
||||
module,
|
||||
&a_enc_prep,
|
||||
&b_enc_prep,
|
||||
bdd_key_prepared,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
res.decrypt(&module, &sk_glwe_prep, scratch.borrow()),
|
||||
res.decrypt(module, sk_glwe_prep, scratch.borrow()),
|
||||
((a as i32) >> b) as u32
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use poulpy_core::{
|
||||
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
@@ -11,14 +11,14 @@ use rand::RngCore;
|
||||
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{
|
||||
BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
|
||||
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Srl,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
|
||||
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
|
||||
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Srl,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
|
||||
},
|
||||
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
|
||||
};
|
||||
|
||||
pub fn test_bdd_srl<BRA: BlindRotationAlgo, BE: Backend>()
|
||||
pub fn test_bdd_srl<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
|
||||
where
|
||||
Module<BE>: ModuleNew<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
@@ -40,51 +40,49 @@ where
|
||||
let glwe_infos: GLWELayout = TEST_GLWE_INFOS;
|
||||
let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS;
|
||||
|
||||
let n_glwe: usize = glwe_infos.n().into();
|
||||
let module: &Module<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_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_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: 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 mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&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 b: u32 = source.next_u32() & 15;
|
||||
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
a_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
a,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
b_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
b,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
res.srl(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
|
||||
|
||||
assert_eq!(
|
||||
res.decrypt(&module, &sk_glwe_prep, scratch.borrow()),
|
||||
a >> b
|
||||
res.srl(
|
||||
module,
|
||||
&a_enc_prep,
|
||||
&b_enc_prep,
|
||||
bdd_key_prepared,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
assert_eq!(res.decrypt(module, sk_glwe_prep, scratch.borrow()), a >> b);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use poulpy_core::{
|
||||
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
@@ -11,14 +11,14 @@ use rand::RngCore;
|
||||
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{
|
||||
BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
|
||||
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Sub,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
|
||||
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
|
||||
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Sub,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
|
||||
},
|
||||
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
|
||||
};
|
||||
|
||||
pub fn test_bdd_sub<BRA: BlindRotationAlgo, BE: Backend>()
|
||||
pub fn test_bdd_sub<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
|
||||
where
|
||||
Module<BE>: ModuleNew<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
@@ -40,51 +40,52 @@ where
|
||||
let glwe_infos: GLWELayout = TEST_GLWE_INFOS;
|
||||
let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS;
|
||||
|
||||
let n_glwe: usize = glwe_infos.n().into();
|
||||
let module: &Module<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_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_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: 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 mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&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 b: u32 = source.next_u32();
|
||||
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
a_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
a,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
b_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
b,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
res.sub(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
|
||||
res.sub(
|
||||
module,
|
||||
&a_enc_prep,
|
||||
&b_enc_prep,
|
||||
bdd_key_prepared,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
res.decrypt(&module, &sk_glwe_prep, scratch.borrow()),
|
||||
res.decrypt(module, sk_glwe_prep, scratch.borrow()),
|
||||
a.wrapping_sub(b)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use poulpy_core::{
|
||||
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, prepared::GLWESecretPrepared},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
@@ -11,14 +11,14 @@ use rand::RngCore;
|
||||
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{
|
||||
BDDKeyEncryptSk, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare, FheUintBlocksPrepare,
|
||||
FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Xor,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS},
|
||||
BDDKeyEncryptSk, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, FheUint, FheUintBlockDebugPrepare,
|
||||
FheUintBlocksPrepare, FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, FheUintPrepared, Xor,
|
||||
tests::test_suite::{TEST_GGSW_INFOS, TEST_GLWE_INFOS, TestContext},
|
||||
},
|
||||
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory},
|
||||
};
|
||||
|
||||
pub fn test_bdd_xor<BRA: BlindRotationAlgo, BE: Backend>()
|
||||
pub fn test_bdd_xor<BRA: BlindRotationAlgo, BE: Backend>(test_context: &TestContext<BRA, BE>)
|
||||
where
|
||||
Module<BE>: ModuleNew<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
@@ -40,48 +40,49 @@ where
|
||||
let glwe_infos: GLWELayout = TEST_GLWE_INFOS;
|
||||
let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS;
|
||||
|
||||
let n_glwe: usize = glwe_infos.n().into();
|
||||
let module: &Module<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_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_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: 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 mut res: FheUint<Vec<u8>, u32> = FheUint::<Vec<u8>, u32>::alloc_from_infos(&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 b: u32 = source.next_u32();
|
||||
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
a_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
a,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
source.fill_bytes(&mut scratch.borrow().data);
|
||||
b_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
module,
|
||||
b,
|
||||
&sk_glwe_prep,
|
||||
sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
res.xor(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
|
||||
res.xor(
|
||||
module,
|
||||
&a_enc_prep,
|
||||
&b_enc_prep,
|
||||
bdd_key_prepared,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
assert_eq!(res.decrypt(&module, &sk_glwe_prep, scratch.borrow()), a ^ b);
|
||||
assert_eq!(res.decrypt(module, sk_glwe_prep, scratch.borrow()), a ^ b);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user