mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 13:16:44 +01:00
compiling CBT but failing tests
This commit is contained in:
@@ -1,185 +1,182 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use poulpy_hal::{
|
||||
api::{
|
||||
ScratchAvailable, TakeMatZnx, TakeSlice, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, TakeVecZnxDftSlice, TakeVecZnxSlice,
|
||||
VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf,
|
||||
VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAddInplace,
|
||||
VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace,
|
||||
VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace,
|
||||
VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft,
|
||||
VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes,
|
||||
},
|
||||
layouts::{Backend, DataMut, DataRef, Module, Scratch, ToOwnedDeep},
|
||||
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
|
||||
api::{ModuleLogN, ModuleN, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
layouts::{Backend, DataRef, Module, Scratch, ScratchOwned, ToOwnedDeep},
|
||||
};
|
||||
|
||||
use poulpy_core::{
|
||||
GLWEOperations, TakeGGLWE, TakeGLWE,
|
||||
layouts::{Dsize, GGLWELayout, GGSWInfos, GLWEInfos, LWEInfos},
|
||||
GGSWFromGGLWE, GLWEPacking, GLWETrace, ScratchTakeCore,
|
||||
layouts::{Dsize, GGLWELayout, GGSWInfos, GGSWToMut, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, LWEToRef},
|
||||
};
|
||||
|
||||
use poulpy_core::glwe_packing;
|
||||
use poulpy_core::layouts::{GGSW, GLWE, LWE, prepared::GLWEAutomorphismKeyPrepared};
|
||||
|
||||
use crate::tfhe::{
|
||||
blind_rotation::{
|
||||
BlincRotationExecute, BlindRotationAlgo, BlindRotationKeyPrepared, LookUpTable, LookUpTableRotationDirection,
|
||||
BlindRotationAlgo, BlindRotationExecute, LookUpTableLayout, LookUpTableRotationDirection, LookupTable, LookupTableFactory,
|
||||
},
|
||||
circuit_bootstrapping::{CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute},
|
||||
circuit_bootstrapping::CircuitBootstrappingKeyPrepared,
|
||||
};
|
||||
|
||||
impl<D: DataRef, BRA: BlindRotationAlgo, B> CirtuitBootstrappingExecute<B> for CircuitBootstrappingKeyPrepared<D, BRA, B>
|
||||
where
|
||||
Module<B>: VecZnxRotateInplace<B>
|
||||
+ VecZnxNormalizeInplace<B>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxSwitchRing
|
||||
+ VecZnxBigAutomorphismInplace<B>
|
||||
+ VecZnxRshInplace<B>
|
||||
+ VecZnxDftCopy<B>
|
||||
+ VecZnxIdftApplyTmpA<B>
|
||||
+ VecZnxSub
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNegateInplace
|
||||
+ VecZnxCopy
|
||||
+ VecZnxSubInplace
|
||||
+ VecZnxDftBytesOf
|
||||
+ VmpApplyDftToDftTmpBytes
|
||||
+ VecZnxBigNormalizeTmpBytes
|
||||
+ VmpApplyDftToDft<B>
|
||||
+ VmpApplyDftToDftAdd<B>
|
||||
+ VecZnxDftApply<B>
|
||||
+ VecZnxIdftApplyConsume<B>
|
||||
+ VecZnxBigAddSmallInplace<B>
|
||||
+ VecZnxBigNormalize<B>
|
||||
+ VecZnxAutomorphismInplace<B>
|
||||
+ VecZnxBigSubSmallNegateInplace<B>
|
||||
+ VecZnxRotateInplaceTmpBytes
|
||||
+ VecZnxBigBytesOf
|
||||
+ VecZnxDftAddInplace<B>
|
||||
+ VecZnxRotate
|
||||
+ VecZnxNormalize<B>,
|
||||
B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
|
||||
Scratch<B>: TakeVecZnx
|
||||
+ TakeVecZnxDftSlice<B>
|
||||
+ TakeVecZnxBig<B>
|
||||
+ TakeVecZnxDft<B>
|
||||
+ TakeMatZnx
|
||||
+ ScratchAvailable
|
||||
+ TakeVecZnxSlice
|
||||
+ TakeSlice,
|
||||
BlindRotationKeyPrepared<D, BRA, B>: BlincRotationExecute<B>,
|
||||
{
|
||||
fn execute_to_constant<DM: DataMut, DR: DataRef>(
|
||||
pub trait CirtuitBootstrappingExecute<BRA: BlindRotationAlgo, BE: Backend> {
|
||||
fn circuit_bootstrapping_execute_to_constant<R, L, D>(
|
||||
&self,
|
||||
module: &Module<B>,
|
||||
res: &mut GGSW<DM>,
|
||||
lwe: &LWE<DR>,
|
||||
res: &mut R,
|
||||
lwe: &L,
|
||||
key: &CircuitBootstrappingKeyPrepared<D, BRA, BE>,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
scratch: &mut Scratch<B>,
|
||||
) {
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
R: GGSWToMut + GGSWInfos,
|
||||
L: LWEToRef + LWEInfos,
|
||||
D: DataRef;
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn circuit_bootstrapping_execute_to_exponent<R, L, D>(
|
||||
&self,
|
||||
log_gap_out: usize,
|
||||
res: &mut R,
|
||||
lwe: &L,
|
||||
key: &CircuitBootstrappingKeyPrepared<D, BRA, BE>,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
R: GGSWToMut + GGSWInfos,
|
||||
L: LWEToRef + LWEInfos,
|
||||
D: DataRef;
|
||||
}
|
||||
|
||||
impl<D: DataRef, BRA: BlindRotationAlgo, BE: Backend> CircuitBootstrappingKeyPrepared<D, BRA, BE> {
|
||||
pub fn execute_to_constant<M, L, R>(
|
||||
&self,
|
||||
module: &M,
|
||||
res: &mut R,
|
||||
lwe: &L,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
M: CirtuitBootstrappingExecute<BRA, BE>,
|
||||
R: GGSWToMut + GGSWInfos,
|
||||
L: LWEToRef + LWEInfos,
|
||||
{
|
||||
module.circuit_bootstrapping_execute_to_constant(res, lwe, self, log_domain, extension_factor, scratch);
|
||||
}
|
||||
|
||||
pub fn execute_to_exponent<R, L, M>(
|
||||
&self,
|
||||
module: &M,
|
||||
log_gap_out: usize,
|
||||
res: &mut R,
|
||||
lwe: &L,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
M: CirtuitBootstrappingExecute<BRA, BE>,
|
||||
R: GGSWToMut + GGSWInfos,
|
||||
L: LWEToRef + LWEInfos,
|
||||
{
|
||||
module.circuit_bootstrapping_execute_to_exponent(
|
||||
log_gap_out,
|
||||
res,
|
||||
lwe,
|
||||
self,
|
||||
log_domain,
|
||||
extension_factor,
|
||||
scratch,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl<BRA: BlindRotationAlgo, BE: Backend> CirtuitBootstrappingExecute<BRA, BE> for Module<BE>
|
||||
where
|
||||
Self: ModuleN + LookupTableFactory + BlindRotationExecute<BRA, BE> + GLWETrace<BE> + GLWEPacking<BE> + GGSWFromGGLWE<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
fn circuit_bootstrapping_execute_to_constant<R, L, D>(
|
||||
&self,
|
||||
res: &mut R,
|
||||
lwe: &L,
|
||||
key: &CircuitBootstrappingKeyPrepared<D, BRA, BE>,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
R: GGSWToMut + GGSWInfos,
|
||||
L: LWEToRef + LWEInfos,
|
||||
D: DataRef,
|
||||
{
|
||||
circuit_bootstrap_core(
|
||||
false,
|
||||
module,
|
||||
self,
|
||||
0,
|
||||
res,
|
||||
lwe,
|
||||
log_domain,
|
||||
extension_factor,
|
||||
self,
|
||||
key,
|
||||
scratch,
|
||||
);
|
||||
}
|
||||
|
||||
fn execute_to_exponent<DM: DataMut, DR: DataRef>(
|
||||
fn circuit_bootstrapping_execute_to_exponent<R, L, D>(
|
||||
&self,
|
||||
module: &Module<B>,
|
||||
log_gap_out: usize,
|
||||
res: &mut GGSW<DM>,
|
||||
lwe: &LWE<DR>,
|
||||
res: &mut R,
|
||||
lwe: &L,
|
||||
key: &CircuitBootstrappingKeyPrepared<D, BRA, BE>,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
scratch: &mut Scratch<B>,
|
||||
) {
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
R: GGSWToMut + GGSWInfos,
|
||||
L: LWEToRef + LWEInfos,
|
||||
D: DataRef,
|
||||
{
|
||||
circuit_bootstrap_core(
|
||||
true,
|
||||
module,
|
||||
self,
|
||||
log_gap_out,
|
||||
res,
|
||||
lwe,
|
||||
log_domain,
|
||||
extension_factor,
|
||||
self,
|
||||
key,
|
||||
scratch,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn circuit_bootstrap_core<DRes, DLwe, DBrk, BRA: BlindRotationAlgo, B>(
|
||||
pub fn circuit_bootstrap_core<R, L, D, M, BRA: BlindRotationAlgo, BE: Backend>(
|
||||
to_exponent: bool,
|
||||
module: &Module<B>,
|
||||
module: &M,
|
||||
log_gap_out: usize,
|
||||
res: &mut GGSW<DRes>,
|
||||
lwe: &LWE<DLwe>,
|
||||
res: &mut R,
|
||||
lwe: &L,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
key: &CircuitBootstrappingKeyPrepared<DBrk, BRA, B>,
|
||||
scratch: &mut Scratch<B>,
|
||||
key: &CircuitBootstrappingKeyPrepared<D, BRA, BE>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
DRes: DataMut,
|
||||
DLwe: DataRef,
|
||||
DBrk: DataRef,
|
||||
Module<B>: VecZnxRotateInplace<B>
|
||||
+ VecZnxNormalizeInplace<B>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxSwitchRing
|
||||
+ VecZnxBigAutomorphismInplace<B>
|
||||
+ VecZnxRshInplace<B>
|
||||
+ VecZnxDftCopy<B>
|
||||
+ VecZnxIdftApplyTmpA<B>
|
||||
+ VecZnxSub
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNegateInplace
|
||||
+ VecZnxCopy
|
||||
+ VecZnxSubInplace
|
||||
+ VecZnxDftBytesOf
|
||||
+ VmpApplyDftToDftTmpBytes
|
||||
+ VecZnxBigNormalizeTmpBytes
|
||||
+ VmpApplyDftToDft<B>
|
||||
+ VmpApplyDftToDftAdd<B>
|
||||
+ VecZnxDftApply<B>
|
||||
+ VecZnxIdftApplyConsume<B>
|
||||
+ VecZnxBigAddSmallInplace<B>
|
||||
+ VecZnxBigNormalize<B>
|
||||
+ VecZnxAutomorphismInplace<B>
|
||||
+ VecZnxBigSubSmallNegateInplace<B>
|
||||
+ VecZnxBigBytesOf
|
||||
+ VecZnxDftAddInplace<B>
|
||||
+ VecZnxRotateInplaceTmpBytes
|
||||
+ VecZnxRotate
|
||||
+ VecZnxNormalize<B>,
|
||||
B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
|
||||
Scratch<B>: TakeVecZnxDftSlice<B>
|
||||
+ TakeVecZnxBig<B>
|
||||
+ TakeVecZnxDft<B>
|
||||
+ TakeVecZnx
|
||||
+ ScratchAvailable
|
||||
+ TakeVecZnxSlice
|
||||
+ TakeMatZnx
|
||||
+ TakeSlice,
|
||||
BlindRotationKeyPrepared<DBrk, BRA, B>: BlincRotationExecute<B>,
|
||||
R: GGSWToMut,
|
||||
L: LWEToRef,
|
||||
D: DataRef,
|
||||
M: ModuleN + LookupTableFactory + BlindRotationExecute<BRA, BE> + GLWETrace<BE> + GLWEPacking<BE> + GGSWFromGGLWE<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
use poulpy_core::layouts::LWEInfos;
|
||||
let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
|
||||
let lwe: &LWE<&[u8]> = &lwe.to_ref();
|
||||
|
||||
assert_eq!(res.n(), key.brk.n());
|
||||
assert_eq!(lwe.base2k(), key.brk.base2k());
|
||||
assert_eq!(res.base2k(), key.brk.base2k());
|
||||
}
|
||||
assert_eq!(res.n(), key.brk.n());
|
||||
assert_eq!(lwe.base2k(), key.brk.base2k());
|
||||
assert_eq!(res.base2k(), key.brk.base2k());
|
||||
|
||||
let n: usize = res.n().into();
|
||||
let base2k: usize = res.base2k().into();
|
||||
@@ -203,8 +200,15 @@ pub fn circuit_bootstrap_core<DRes, DLwe, DBrk, BRA: BlindRotationAlgo, B>(
|
||||
});
|
||||
}
|
||||
|
||||
let lut_infos: LookUpTableLayout = LookUpTableLayout {
|
||||
n: module.n().into(),
|
||||
extension_factor,
|
||||
k: (base2k * dnum).into(),
|
||||
base2k: base2k.into(),
|
||||
};
|
||||
|
||||
// Lut precision, basically must be able to hold the decomposition power basis of the GGSW
|
||||
let mut lut: LookUpTable = LookUpTable::alloc(module, base2k, base2k * dnum, extension_factor);
|
||||
let mut lut: LookupTable = LookupTable::alloc(&lut_infos);
|
||||
lut.set(module, &f, base2k * dnum);
|
||||
|
||||
if to_exponent {
|
||||
@@ -212,7 +216,7 @@ pub fn circuit_bootstrap_core<DRes, DLwe, DBrk, BRA: BlindRotationAlgo, B>(
|
||||
}
|
||||
|
||||
// TODO: separate GGSW k from output of blind rotation k
|
||||
let (mut res_glwe, scratch_1) = scratch.take_glwe_ct(res);
|
||||
let (mut res_glwe, scratch_1) = scratch.take_glwe(res);
|
||||
|
||||
let gglwe_infos: GGLWELayout = GGLWELayout {
|
||||
n: n.into(),
|
||||
@@ -252,7 +256,7 @@ pub fn circuit_bootstrap_core<DRes, DLwe, DBrk, BRA: BlindRotationAlgo, B>(
|
||||
}
|
||||
|
||||
if i < dnum {
|
||||
res_glwe.rotate_inplace(module, -(gap as i64), scratch_2);
|
||||
module.glwe_rotate_inplace(-(gap as i64), &mut res_glwe, scratch_2);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -261,46 +265,24 @@ pub fn circuit_bootstrap_core<DRes, DLwe, DBrk, BRA: BlindRotationAlgo, B>(
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn post_process<DataRes, DataA, B: Backend>(
|
||||
module: &Module<B>,
|
||||
res: &mut GLWE<DataRes>,
|
||||
a: &GLWE<DataA>,
|
||||
fn post_process<R, A, M, BE: Backend>(
|
||||
module: &M,
|
||||
res: &mut R,
|
||||
a: &A,
|
||||
log_gap_in: usize,
|
||||
log_gap_out: usize,
|
||||
log_domain: usize,
|
||||
auto_keys: &HashMap<i64, GLWEAutomorphismKeyPrepared<Vec<u8>, B>>,
|
||||
scratch: &mut Scratch<B>,
|
||||
auto_keys: &HashMap<i64, GLWEAutomorphismKeyPrepared<Vec<u8>, BE>>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
DataRes: DataMut,
|
||||
DataA: DataRef,
|
||||
Module<B>: VecZnxRotateInplace<B>
|
||||
+ VecZnxNormalizeInplace<B>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxSwitchRing
|
||||
+ VecZnxBigAutomorphismInplace<B>
|
||||
+ VecZnxRshInplace<B>
|
||||
+ VecZnxDftCopy<B>
|
||||
+ VecZnxIdftApplyTmpA<B>
|
||||
+ VecZnxSub
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNegateInplace
|
||||
+ VecZnxCopy
|
||||
+ VecZnxSubInplace
|
||||
+ VecZnxDftBytesOf
|
||||
+ VmpApplyDftToDftTmpBytes
|
||||
+ VecZnxBigNormalizeTmpBytes
|
||||
+ VmpApplyDftToDft<B>
|
||||
+ VmpApplyDftToDftAdd<B>
|
||||
+ VecZnxDftApply<B>
|
||||
+ VecZnxIdftApplyConsume<B>
|
||||
+ VecZnxBigAddSmallInplace<B>
|
||||
+ VecZnxBigNormalize<B>
|
||||
+ VecZnxAutomorphismInplace<B>
|
||||
+ VecZnxBigSubSmallNegateInplace<B>
|
||||
+ VecZnxRotate
|
||||
+ VecZnxNormalize<B>,
|
||||
Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx,
|
||||
R: GLWEToMut,
|
||||
A: GLWEToRef,
|
||||
M: ModuleLogN + GLWETrace<BE> + GLWEPacking<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
|
||||
let a: &GLWE<&[u8]> = &a.to_ref();
|
||||
|
||||
let log_n: usize = module.log_n();
|
||||
|
||||
let mut cts: HashMap<usize, &mut GLWE<Vec<u8>>> = HashMap::new();
|
||||
@@ -326,7 +308,7 @@ fn post_process<DataRes, DataA, B: Backend>(
|
||||
|
||||
for i in 0..steps {
|
||||
if i != 0 {
|
||||
res.rotate_inplace(module, -(1 << log_gap_in), scratch);
|
||||
module.glwe_rotate_inplace(-(1 << log_gap_in), res, scratch);
|
||||
}
|
||||
cts_vec.push(res.to_owned_deep());
|
||||
}
|
||||
@@ -335,7 +317,8 @@ fn post_process<DataRes, DataA, B: Backend>(
|
||||
cts.insert(i * (1 << log_gap_out), ct);
|
||||
}
|
||||
|
||||
glwe_packing(module, &mut cts, log_gap_out, auto_keys, scratch);
|
||||
module.glwe_pack(&mut cts, log_gap_out, auto_keys, scratch);
|
||||
|
||||
let packed: &mut GLWE<Vec<u8>> = cts.remove(&0).unwrap();
|
||||
res.trace(
|
||||
module,
|
||||
|
||||
Reference in New Issue
Block a user