mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
Examples, benchmarks, and minor changes for consistency, in BDD API
This commit is contained in:
@@ -188,15 +188,35 @@ macro_rules! impl_bdd_2w_to_1w_trait {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// a + b
|
||||
define_bdd_2w_to_1w_trait!(pub Add, add);
|
||||
|
||||
// a - b
|
||||
define_bdd_2w_to_1w_trait!(pub Sub, sub);
|
||||
|
||||
// a << b
|
||||
define_bdd_2w_to_1w_trait!(pub Sll, sll);
|
||||
|
||||
// a >> b arithmetic
|
||||
define_bdd_2w_to_1w_trait!(pub Sra, sra);
|
||||
|
||||
// a >> b logical
|
||||
define_bdd_2w_to_1w_trait!(pub Srl, srl);
|
||||
|
||||
// signed a < signed b
|
||||
define_bdd_2w_to_1w_trait!(pub Slt, slt);
|
||||
|
||||
// unsigned a < unsigned b
|
||||
define_bdd_2w_to_1w_trait!(pub Sltu, sltu);
|
||||
|
||||
// a or b
|
||||
define_bdd_2w_to_1w_trait!(pub Or, or);
|
||||
|
||||
// a and b
|
||||
define_bdd_2w_to_1w_trait!(pub And, and);
|
||||
|
||||
// a xor b
|
||||
define_bdd_2w_to_1w_trait!(pub Xor, xor);
|
||||
|
||||
impl_bdd_2w_to_1w_trait!(
|
||||
|
||||
@@ -8,9 +8,9 @@ use poulpy_hal::layouts::{Backend, Module, Scratch, ZnxZero};
|
||||
|
||||
use crate::bin_fhe::bdd_arithmetic::{Cmux, GetGGSWBit, UnsignedInteger};
|
||||
|
||||
impl<T: UnsignedInteger, BE: Backend> GLWEBlinSelection<T, BE> for Module<BE> where Self: GLWECopy + Cmux<BE> + GLWEDecrypt<BE> {}
|
||||
impl<T: UnsignedInteger, BE: Backend> GLWEBlindSelection<T, BE> for Module<BE> where Self: GLWECopy + Cmux<BE> + GLWEDecrypt<BE> {}
|
||||
|
||||
pub trait GLWEBlinSelection<T: UnsignedInteger, BE: Backend>
|
||||
pub trait GLWEBlindSelection<T: UnsignedInteger, BE: Backend>
|
||||
where
|
||||
Self: GLWECopy + Cmux<BE> + GLWEDecrypt<BE>,
|
||||
{
|
||||
|
||||
@@ -87,7 +87,7 @@ impl<D: DataMut, T: UnsignedInteger + ToBits> FheUint<D, T> {
|
||||
&mut self,
|
||||
module: &M,
|
||||
data: T,
|
||||
sk: &S,
|
||||
sk_glwe: &S,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
scratch: &mut Scratch<BE>,
|
||||
@@ -100,7 +100,7 @@ impl<D: DataMut, T: UnsignedInteger + ToBits> FheUint<D, T> {
|
||||
{
|
||||
assert!(module.n().is_multiple_of(T::BITS as usize));
|
||||
assert_eq!(self.n(), module.n() as u32);
|
||||
assert_eq!(sk.n(), module.n() as u32);
|
||||
assert_eq!(sk_glwe.n(), module.n() as u32);
|
||||
}
|
||||
|
||||
let mut data_bits: Vec<i64> = vec![0i64; module.n()];
|
||||
@@ -122,7 +122,7 @@ impl<D: DataMut, T: UnsignedInteger + ToBits> FheUint<D, T> {
|
||||
|
||||
pt.encode_vec_i64(&data_bits, TorusPrecision(2));
|
||||
self.bits
|
||||
.encrypt_sk(module, &pt, sk, source_xa, source_xe, scratch_1);
|
||||
.encrypt_sk(module, &pt, sk_glwe, source_xa, source_xe, scratch_1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,7 +150,7 @@ impl<D: DataRef, T: UnsignedInteger + FromBits> FheUint<D, T> {
|
||||
self.bits.noise(module, &pt, sk, scratch_1)
|
||||
}
|
||||
|
||||
pub fn decrypt<S, M, BE: Backend>(&self, module: &M, sk: &S, scratch: &mut Scratch<BE>) -> T
|
||||
pub fn decrypt<S, M, BE: Backend>(&self, module: &M, sk_glwe: &S, scratch: &mut Scratch<BE>) -> T
|
||||
where
|
||||
S: GLWESecretPreparedToRef<BE> + GLWEInfos,
|
||||
M: ModuleLogN + GLWEDecrypt<BE>,
|
||||
@@ -160,7 +160,7 @@ impl<D: DataRef, T: UnsignedInteger + FromBits> FheUint<D, T> {
|
||||
{
|
||||
assert!(module.n().is_multiple_of(T::BITS as usize));
|
||||
assert_eq!(self.n(), module.n() as u32);
|
||||
assert_eq!(sk.n(), module.n() as u32);
|
||||
assert_eq!(sk_glwe.n(), module.n() as u32);
|
||||
}
|
||||
|
||||
let pt_infos = GLWEPlaintextLayout {
|
||||
@@ -171,7 +171,7 @@ impl<D: DataRef, T: UnsignedInteger + FromBits> FheUint<D, T> {
|
||||
|
||||
let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos);
|
||||
|
||||
self.bits.decrypt(module, &mut pt, sk, scratch_1);
|
||||
self.bits.decrypt(module, &mut pt, sk_glwe, scratch_1);
|
||||
|
||||
let mut data_bits: Vec<i64> = vec![0i64; module.n()];
|
||||
pt.decode_vec_i64(&mut data_bits, TorusPrecision(2));
|
||||
|
||||
@@ -34,22 +34,22 @@ pub trait BDDKeyInfos {
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct BDDKeyLayout {
|
||||
pub cbt: CircuitBootstrappingKeyLayout,
|
||||
pub ks_glwe: Option<GLWESwitchingKeyLayout>,
|
||||
pub ks_lwe: GLWEToLWEKeyLayout,
|
||||
pub cbt_layout: CircuitBootstrappingKeyLayout,
|
||||
pub ks_glwe_layout: Option<GLWESwitchingKeyLayout>,
|
||||
pub ks_lwe_layout: GLWEToLWEKeyLayout,
|
||||
}
|
||||
|
||||
impl BDDKeyInfos for BDDKeyLayout {
|
||||
fn cbt_infos(&self) -> CircuitBootstrappingKeyLayout {
|
||||
self.cbt
|
||||
self.cbt_layout
|
||||
}
|
||||
|
||||
fn ks_glwe_infos(&self) -> Option<GLWESwitchingKeyLayout> {
|
||||
self.ks_glwe
|
||||
self.ks_glwe_layout
|
||||
}
|
||||
|
||||
fn ks_lwe_infos(&self) -> GLWEToLWEKeyLayout {
|
||||
self.ks_lwe
|
||||
self.ks_lwe_layout
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,9 +176,9 @@ where
|
||||
impl<D: DataRef, BRA: BlindRotationAlgo, BE: Backend> BDDKeyInfos for BDDKeyPrepared<D, BRA, BE> {
|
||||
fn cbt_infos(&self) -> CircuitBootstrappingKeyLayout {
|
||||
CircuitBootstrappingKeyLayout {
|
||||
layout_brk: self.cbt.brk_infos(),
|
||||
layout_atk: self.cbt.atk_infos(),
|
||||
layout_tsk: self.cbt.tsk_infos(),
|
||||
brk_layout: self.cbt.brk_infos(),
|
||||
atk_layout: self.cbt.atk_infos(),
|
||||
tsk_layout: self.cbt.tsk_infos(),
|
||||
}
|
||||
}
|
||||
fn ks_glwe_infos(&self) -> Option<GLWESwitchingKeyLayout> {
|
||||
|
||||
@@ -16,7 +16,7 @@ use rand::RngCore;
|
||||
|
||||
use crate::bin_fhe::{
|
||||
bdd_arithmetic::{
|
||||
FheUintPrepared, GLWEBlinSelection,
|
||||
FheUintPrepared, GLWEBlindSelection,
|
||||
tests::test_suite::{TEST_FHEUINT_BASE2K, TEST_RANK, TestContext},
|
||||
},
|
||||
blind_rotation::BlindRotationAlgo,
|
||||
@@ -28,7 +28,7 @@ where
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GGSWPreparedFactory<BE>
|
||||
+ GGSWEncryptSk<BE>
|
||||
+ GLWEBlinSelection<u32, BE>
|
||||
+ GLWEBlindSelection<u32, BE>
|
||||
+ GLWEDecrypt<BE>
|
||||
+ GLWEEncryptSk<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
|
||||
@@ -165,8 +165,8 @@ pub(crate) static TEST_GGSW_INFOS: GGSWLayout = GGSWLayout {
|
||||
};
|
||||
|
||||
pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout {
|
||||
cbt: CircuitBootstrappingKeyLayout {
|
||||
layout_brk: BlindRotationKeyLayout {
|
||||
cbt_layout: CircuitBootstrappingKeyLayout {
|
||||
brk_layout: BlindRotationKeyLayout {
|
||||
n_glwe: Degree(TEST_N_GLWE),
|
||||
n_lwe: Degree(TEST_N_LWE),
|
||||
base2k: Base2K(TEST_BRK_BASE2K),
|
||||
@@ -174,7 +174,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout {
|
||||
dnum: Dnum(4),
|
||||
rank: Rank(TEST_RANK),
|
||||
},
|
||||
layout_atk: GLWEAutomorphismKeyLayout {
|
||||
atk_layout: GLWEAutomorphismKeyLayout {
|
||||
n: Degree(TEST_N_GLWE),
|
||||
base2k: Base2K(TEST_ATK_BASE2K),
|
||||
k: TorusPrecision(52),
|
||||
@@ -182,7 +182,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout {
|
||||
dnum: Dnum(4),
|
||||
dsize: Dsize(1),
|
||||
},
|
||||
layout_tsk: GGLWEToGGSWKeyLayout {
|
||||
tsk_layout: GGLWEToGGSWKeyLayout {
|
||||
n: Degree(TEST_N_GLWE),
|
||||
base2k: Base2K(TEST_TSK_BASE2K),
|
||||
k: TorusPrecision(52),
|
||||
@@ -191,7 +191,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout {
|
||||
dsize: Dsize(1),
|
||||
},
|
||||
},
|
||||
ks_glwe: Some(GLWESwitchingKeyLayout {
|
||||
ks_glwe_layout: Some(GLWESwitchingKeyLayout {
|
||||
n: Degree(TEST_N_GLWE),
|
||||
base2k: Base2K(TEST_LWE_BASE2K),
|
||||
k: TorusPrecision(20),
|
||||
@@ -200,7 +200,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout {
|
||||
dnum: Dnum(3),
|
||||
dsize: Dsize(1),
|
||||
}),
|
||||
ks_lwe: GLWEToLWEKeyLayout {
|
||||
ks_lwe_layout: GLWEToLWEKeyLayout {
|
||||
n: Degree(TEST_N_GLWE),
|
||||
base2k: Base2K(TEST_LWE_BASE2K),
|
||||
k: TorusPrecision(16),
|
||||
|
||||
@@ -27,9 +27,9 @@ pub trait CircuitBootstrappingKeyInfos {
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct CircuitBootstrappingKeyLayout {
|
||||
pub layout_brk: BlindRotationKeyLayout,
|
||||
pub layout_atk: GLWEAutomorphismKeyLayout,
|
||||
pub layout_tsk: GGLWEToGGSWKeyLayout,
|
||||
pub brk_layout: BlindRotationKeyLayout,
|
||||
pub atk_layout: GLWEAutomorphismKeyLayout,
|
||||
pub tsk_layout: GGLWEToGGSWKeyLayout,
|
||||
}
|
||||
|
||||
impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout {
|
||||
@@ -38,15 +38,15 @@ impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout {
|
||||
}
|
||||
|
||||
fn atk_infos(&self) -> GLWEAutomorphismKeyLayout {
|
||||
self.layout_atk
|
||||
self.atk_layout
|
||||
}
|
||||
|
||||
fn brk_infos(&self) -> BlindRotationKeyLayout {
|
||||
self.layout_brk
|
||||
self.brk_layout
|
||||
}
|
||||
|
||||
fn tsk_infos(&self) -> GGLWEToGGSWKeyLayout {
|
||||
self.layout_tsk
|
||||
self.tsk_layout
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ where
|
||||
};
|
||||
|
||||
let cbt_infos: CircuitBootstrappingKeyLayout = CircuitBootstrappingKeyLayout {
|
||||
layout_brk: BlindRotationKeyLayout {
|
||||
brk_layout: BlindRotationKeyLayout {
|
||||
n_glwe: n_glwe.into(),
|
||||
n_lwe: n_lwe.into(),
|
||||
base2k: base2k_brk.into(),
|
||||
@@ -86,7 +86,7 @@ where
|
||||
dnum: rows_brk.into(),
|
||||
rank: rank.into(),
|
||||
},
|
||||
layout_atk: GLWEAutomorphismKeyLayout {
|
||||
atk_layout: GLWEAutomorphismKeyLayout {
|
||||
n: n_glwe.into(),
|
||||
base2k: base2k_atk.into(),
|
||||
k: k_atk.into(),
|
||||
@@ -94,7 +94,7 @@ where
|
||||
rank: rank.into(),
|
||||
dsize: Dsize(1),
|
||||
},
|
||||
layout_tsk: GGLWEToGGSWKeyLayout {
|
||||
tsk_layout: GGLWEToGGSWKeyLayout {
|
||||
n: n_glwe.into(),
|
||||
base2k: base2k_tsk.into(),
|
||||
k: k_tsk.into(),
|
||||
@@ -285,7 +285,7 @@ where
|
||||
};
|
||||
|
||||
let cbt_infos: CircuitBootstrappingKeyLayout = CircuitBootstrappingKeyLayout {
|
||||
layout_brk: BlindRotationKeyLayout {
|
||||
brk_layout: BlindRotationKeyLayout {
|
||||
n_glwe: n_glwe.into(),
|
||||
n_lwe: n_lwe.into(),
|
||||
base2k: base2k_brk.into(),
|
||||
@@ -293,7 +293,7 @@ where
|
||||
dnum: rows_brk.into(),
|
||||
rank: rank.into(),
|
||||
},
|
||||
layout_atk: GLWEAutomorphismKeyLayout {
|
||||
atk_layout: GLWEAutomorphismKeyLayout {
|
||||
n: n_glwe.into(),
|
||||
base2k: base2k_atk.into(),
|
||||
k: k_atk.into(),
|
||||
@@ -301,7 +301,7 @@ where
|
||||
rank: rank.into(),
|
||||
dsize: Dsize(1),
|
||||
},
|
||||
layout_tsk: GGLWEToGGSWKeyLayout {
|
||||
tsk_layout: GGLWEToGGSWKeyLayout {
|
||||
n: n_glwe.into(),
|
||||
base2k: base2k_tsk.into(),
|
||||
k: k_tsk.into(),
|
||||
|
||||
Reference in New Issue
Block a user