mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 21:26:41 +01:00
wip
This commit is contained in:
@@ -20,7 +20,7 @@ use poulpy_core::{
|
||||
};
|
||||
|
||||
use poulpy_core::glwe_packing;
|
||||
use poulpy_core::layouts::{GGSWCiphertext, GLWECiphertext, LWECiphertext, prepared::GGLWEAutomorphismKeyPrepared};
|
||||
use poulpy_core::layouts::{GGSW, GLWECiphertext, LWECiphertext, prepared::AutomorphismKeyPrepared};
|
||||
|
||||
use crate::tfhe::{
|
||||
blind_rotation::{
|
||||
@@ -74,7 +74,7 @@ where
|
||||
fn execute_to_constant<DM: DataMut, DR: DataRef>(
|
||||
&self,
|
||||
module: &Module<B>,
|
||||
res: &mut GGSWCiphertext<DM>,
|
||||
res: &mut GGSW<DM>,
|
||||
lwe: &LWECiphertext<DR>,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
@@ -97,7 +97,7 @@ where
|
||||
&self,
|
||||
module: &Module<B>,
|
||||
log_gap_out: usize,
|
||||
res: &mut GGSWCiphertext<DM>,
|
||||
res: &mut GGSW<DM>,
|
||||
lwe: &LWECiphertext<DR>,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
@@ -122,7 +122,7 @@ pub fn circuit_bootstrap_core<DRes, DLwe, DBrk, BRA: BlindRotationAlgo, B>(
|
||||
to_exponent: bool,
|
||||
module: &Module<B>,
|
||||
log_gap_out: usize,
|
||||
res: &mut GGSWCiphertext<DRes>,
|
||||
res: &mut GGSW<DRes>,
|
||||
lwe: &LWECiphertext<DLwe>,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
@@ -268,7 +268,7 @@ fn post_process<DataRes, DataA, B: Backend>(
|
||||
log_gap_in: usize,
|
||||
log_gap_out: usize,
|
||||
log_domain: usize,
|
||||
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>>,
|
||||
auto_keys: &HashMap<i64, AutomorphismKeyPrepared<Vec<u8>, B>>,
|
||||
scratch: &mut Scratch<B>,
|
||||
) where
|
||||
DataRes: DataMut,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use poulpy_core::layouts::{
|
||||
GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GGLWEInfos, GGLWETensorKey, GGLWETensorKeyLayout, GGSWInfos,
|
||||
GLWECiphertext, GLWEInfos, GLWESecret, LWEInfos, LWESecret,
|
||||
prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, PrepareAlloc},
|
||||
AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GGSWInfos, GLWECiphertext, GLWEInfos, GLWESecret, LWEInfos, LWESecret,
|
||||
TensorKey, TensorKeyLayout,
|
||||
prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc, TensorKeyPrepared},
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
@@ -24,19 +24,19 @@ use crate::tfhe::blind_rotation::{
|
||||
|
||||
pub trait CircuitBootstrappingKeyInfos {
|
||||
fn brk_infos(&self) -> BlindRotationKeyLayout;
|
||||
fn atk_infos(&self) -> GGLWEAutomorphismKeyLayout;
|
||||
fn tsk_infos(&self) -> GGLWETensorKeyLayout;
|
||||
fn atk_infos(&self) -> AutomorphismKeyLayout;
|
||||
fn tsk_infos(&self) -> TensorKeyLayout;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct CircuitBootstrappingKeyLayout {
|
||||
pub layout_brk: BlindRotationKeyLayout,
|
||||
pub layout_atk: GGLWEAutomorphismKeyLayout,
|
||||
pub layout_tsk: GGLWETensorKeyLayout,
|
||||
pub layout_atk: AutomorphismKeyLayout,
|
||||
pub layout_tsk: TensorKeyLayout,
|
||||
}
|
||||
|
||||
impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout {
|
||||
fn atk_infos(&self) -> GGLWEAutomorphismKeyLayout {
|
||||
fn atk_infos(&self) -> AutomorphismKeyLayout {
|
||||
self.layout_atk
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout {
|
||||
self.layout_brk
|
||||
}
|
||||
|
||||
fn tsk_infos(&self) -> GGLWETensorKeyLayout {
|
||||
fn tsk_infos(&self) -> TensorKeyLayout {
|
||||
self.layout_tsk
|
||||
}
|
||||
}
|
||||
@@ -68,8 +68,8 @@ pub trait CircuitBootstrappingKeyEncryptSk<B: Backend> {
|
||||
|
||||
pub struct CircuitBootstrappingKey<D: Data, BRA: BlindRotationAlgo> {
|
||||
pub(crate) brk: BlindRotationKey<D, BRA>,
|
||||
pub(crate) tsk: GGLWETensorKey<Vec<u8>>,
|
||||
pub(crate) atk: HashMap<i64, GGLWEAutomorphismKey<Vec<u8>>>,
|
||||
pub(crate) tsk: TensorKey<Vec<u8>>,
|
||||
pub(crate) atk: HashMap<i64, AutomorphismKey<Vec<u8>>>,
|
||||
}
|
||||
|
||||
impl<BRA: BlindRotationAlgo, B: Backend> CircuitBootstrappingKeyEncryptSk<B> for CircuitBootstrappingKey<Vec<u8>, BRA>
|
||||
@@ -117,14 +117,14 @@ where
|
||||
assert_eq!(sk_glwe.n(), cbt_infos.atk_infos().n());
|
||||
assert_eq!(sk_glwe.n(), cbt_infos.tsk_infos().n());
|
||||
|
||||
let atk_infos: GGLWEAutomorphismKeyLayout = cbt_infos.atk_infos();
|
||||
let atk_infos: AutomorphismKeyLayout = cbt_infos.atk_infos();
|
||||
let brk_infos: BlindRotationKeyLayout = cbt_infos.brk_infos();
|
||||
let trk_infos: GGLWETensorKeyLayout = cbt_infos.tsk_infos();
|
||||
let trk_infos: TensorKeyLayout = cbt_infos.tsk_infos();
|
||||
|
||||
let mut auto_keys: HashMap<i64, GGLWEAutomorphismKey<Vec<u8>>> = HashMap::new();
|
||||
let mut auto_keys: HashMap<i64, AutomorphismKey<Vec<u8>>> = HashMap::new();
|
||||
let gal_els: Vec<i64> = GLWECiphertext::trace_galois_elements(module);
|
||||
gal_els.iter().for_each(|gal_el| {
|
||||
let mut key: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&atk_infos);
|
||||
let mut key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&atk_infos);
|
||||
key.encrypt_sk(module, *gal_el, sk_glwe, source_xa, source_xe, scratch);
|
||||
auto_keys.insert(*gal_el, key);
|
||||
});
|
||||
@@ -141,7 +141,7 @@ where
|
||||
scratch,
|
||||
);
|
||||
|
||||
let mut tsk: GGLWETensorKey<Vec<u8>> = GGLWETensorKey::alloc(&trk_infos);
|
||||
let mut tsk: TensorKey<Vec<u8>> = TensorKey::alloc(&trk_infos);
|
||||
tsk.encrypt_sk(module, sk_glwe, source_xa, source_xe, scratch);
|
||||
|
||||
Self {
|
||||
@@ -154,14 +154,14 @@ where
|
||||
|
||||
pub struct CircuitBootstrappingKeyPrepared<D: Data, BRA: BlindRotationAlgo, B: Backend> {
|
||||
pub(crate) brk: BlindRotationKeyPrepared<D, BRA, B>,
|
||||
pub(crate) tsk: GGLWETensorKeyPrepared<Vec<u8>, B>,
|
||||
pub(crate) atk: HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>>,
|
||||
pub(crate) tsk: TensorKeyPrepared<Vec<u8>, B>,
|
||||
pub(crate) atk: HashMap<i64, AutomorphismKeyPrepared<Vec<u8>, B>>,
|
||||
}
|
||||
|
||||
impl<D: DataRef, BRA: BlindRotationAlgo, B: Backend> CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyPrepared<D, BRA, B> {
|
||||
fn atk_infos(&self) -> GGLWEAutomorphismKeyLayout {
|
||||
fn atk_infos(&self) -> AutomorphismKeyLayout {
|
||||
let (_, atk) = self.atk.iter().next().expect("atk is empty");
|
||||
GGLWEAutomorphismKeyLayout {
|
||||
AutomorphismKeyLayout {
|
||||
n: atk.n(),
|
||||
base2k: atk.base2k(),
|
||||
k: atk.k(),
|
||||
@@ -182,8 +182,8 @@ impl<D: DataRef, BRA: BlindRotationAlgo, B: Backend> CircuitBootstrappingKeyInfo
|
||||
}
|
||||
}
|
||||
|
||||
fn tsk_infos(&self) -> GGLWETensorKeyLayout {
|
||||
GGLWETensorKeyLayout {
|
||||
fn tsk_infos(&self) -> TensorKeyLayout {
|
||||
TensorKeyLayout {
|
||||
n: self.tsk.n(),
|
||||
base2k: self.tsk.base2k(),
|
||||
k: self.tsk.k(),
|
||||
@@ -199,13 +199,13 @@ impl<D: DataRef, BRA: BlindRotationAlgo, B: Backend> PrepareAlloc<B, CircuitBoot
|
||||
where
|
||||
Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>,
|
||||
BlindRotationKey<D, BRA>: PrepareAlloc<B, BlindRotationKeyPrepared<Vec<u8>, BRA, B>>,
|
||||
GGLWETensorKey<D>: PrepareAlloc<B, GGLWETensorKeyPrepared<Vec<u8>, B>>,
|
||||
GGLWEAutomorphismKey<D>: PrepareAlloc<B, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>>,
|
||||
TensorKey<D>: PrepareAlloc<B, TensorKeyPrepared<Vec<u8>, B>>,
|
||||
AutomorphismKey<D>: PrepareAlloc<B, AutomorphismKeyPrepared<Vec<u8>, B>>,
|
||||
{
|
||||
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> {
|
||||
let brk: BlindRotationKeyPrepared<Vec<u8>, BRA, B> = self.brk.prepare_alloc(module, scratch);
|
||||
let tsk: GGLWETensorKeyPrepared<Vec<u8>, B> = self.tsk.prepare_alloc(module, scratch);
|
||||
let mut atk: HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new();
|
||||
let tsk: TensorKeyPrepared<Vec<u8>, B> = self.tsk.prepare_alloc(module, scratch);
|
||||
let mut atk: HashMap<i64, AutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new();
|
||||
for (key, value) in &self.atk {
|
||||
atk.insert(*key, value.prepare_alloc(module, scratch));
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ pub mod tests;
|
||||
pub use circuit::*;
|
||||
pub use key::*;
|
||||
|
||||
use poulpy_core::layouts::{GGSWCiphertext, LWECiphertext};
|
||||
use poulpy_core::layouts::{GGSW, LWECiphertext};
|
||||
|
||||
use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch};
|
||||
|
||||
@@ -13,7 +13,7 @@ pub trait CirtuitBootstrappingExecute<B: Backend> {
|
||||
fn execute_to_constant<DM: DataMut, DR: DataRef>(
|
||||
&self,
|
||||
module: &Module<B>,
|
||||
res: &mut GGSWCiphertext<DM>,
|
||||
res: &mut GGSW<DM>,
|
||||
lwe: &LWECiphertext<DR>,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
@@ -25,7 +25,7 @@ pub trait CirtuitBootstrappingExecute<B: Backend> {
|
||||
&self,
|
||||
module: &Module<B>,
|
||||
log_gap_out: usize,
|
||||
res: &mut GGSWCiphertext<DM>,
|
||||
res: &mut GGSW<DM>,
|
||||
lwe: &LWECiphertext<DR>,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
|
||||
@@ -32,12 +32,12 @@ use crate::tfhe::{
|
||||
};
|
||||
|
||||
use poulpy_core::layouts::{
|
||||
Dsize, GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertextLayout, LWECiphertextLayout, prepared::PrepareAlloc,
|
||||
AutomorphismKeyLayout, Dsize, GGSWCiphertextLayout, LWECiphertextLayout, TensorKeyLayout, prepared::PrepareAlloc,
|
||||
};
|
||||
|
||||
use poulpy_core::layouts::{
|
||||
GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWEPlaintext, LWESecret,
|
||||
prepared::{GGSWCiphertextPrepared, GLWESecretPrepared},
|
||||
GGSW, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWEPlaintext, LWESecret,
|
||||
prepared::{GGSWPrepared, GLWESecretPrepared},
|
||||
};
|
||||
|
||||
pub fn test_circuit_bootstrapping_to_exponent<B, BRA: BlindRotationAlgo>(module: &Module<B>)
|
||||
@@ -143,7 +143,7 @@ where
|
||||
dnum: rows_brk.into(),
|
||||
rank: rank.into(),
|
||||
},
|
||||
layout_atk: GGLWEAutomorphismKeyLayout {
|
||||
layout_atk: AutomorphismKeyLayout {
|
||||
n: n_glwe.into(),
|
||||
base2k: base2k.into(),
|
||||
k: k_atk.into(),
|
||||
@@ -151,7 +151,7 @@ where
|
||||
rank: rank.into(),
|
||||
dsize: Dsize(1),
|
||||
},
|
||||
layout_tsk: GGLWETensorKeyLayout {
|
||||
layout_tsk: TensorKeyLayout {
|
||||
n: n_glwe.into(),
|
||||
base2k: base2k.into(),
|
||||
k: k_tsk.into(),
|
||||
@@ -206,7 +206,7 @@ where
|
||||
);
|
||||
println!("CBT-KGEN: {} ms", now.elapsed().as_millis());
|
||||
|
||||
let mut res: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_infos);
|
||||
let mut res: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos);
|
||||
|
||||
let log_gap_out = 1;
|
||||
|
||||
@@ -249,7 +249,7 @@ where
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
let res_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = res.prepare_alloc(module, scratch.borrow());
|
||||
let res_prepared: GGSWPrepared<Vec<u8>, B> = res.prepare_alloc(module, scratch.borrow());
|
||||
|
||||
ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow());
|
||||
|
||||
@@ -365,7 +365,7 @@ where
|
||||
dnum: rows_brk.into(),
|
||||
rank: rank.into(),
|
||||
},
|
||||
layout_atk: GGLWEAutomorphismKeyLayout {
|
||||
layout_atk: AutomorphismKeyLayout {
|
||||
n: n_glwe.into(),
|
||||
base2k: base2k.into(),
|
||||
k: k_atk.into(),
|
||||
@@ -373,7 +373,7 @@ where
|
||||
rank: rank.into(),
|
||||
dsize: Dsize(1),
|
||||
},
|
||||
layout_tsk: GGLWETensorKeyLayout {
|
||||
layout_tsk: TensorKeyLayout {
|
||||
n: n_glwe.into(),
|
||||
base2k: base2k.into(),
|
||||
k: k_tsk.into(),
|
||||
@@ -428,7 +428,7 @@ where
|
||||
);
|
||||
println!("CBT-KGEN: {} ms", now.elapsed().as_millis());
|
||||
|
||||
let mut res: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_infos);
|
||||
let mut res: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos);
|
||||
|
||||
let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow());
|
||||
|
||||
@@ -462,7 +462,7 @@ where
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
let res_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = res.prepare_alloc(module, scratch.borrow());
|
||||
let res_prepared: GGSWPrepared<Vec<u8>, B> = res.prepare_alloc(module, scratch.borrow());
|
||||
|
||||
ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user