mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 21:26:41 +01:00
updated repo for publishing (#74)
This commit is contained in:
committed by
GitHub
parent
0be569eca0
commit
62eb87cc07
489
poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs
Normal file
489
poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs
Normal file
@@ -0,0 +1,489 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use poulpy_backend::hal::{
|
||||
api::{
|
||||
ScratchAvailable, TakeMatZnx, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, TakeVecZnxDftSlice, TakeVecZnxSlice,
|
||||
VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace,
|
||||
VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallBInplace, VecZnxCopy, VecZnxDftAddInplace,
|
||||
VecZnxDftAllocBytes, VecZnxDftCopy, VecZnxDftFromVecZnx, VecZnxDftToVecZnxBigConsume, VecZnxDftToVecZnxBigTmpA,
|
||||
VecZnxNegateInplace, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace,
|
||||
VecZnxRshInplace, VecZnxSub, VecZnxSubABInplace, VecZnxSwithcDegree, VmpApply, VmpApplyAdd, VmpApplyTmpBytes,
|
||||
},
|
||||
layouts::{Backend, DataMut, DataRef, Module, Scratch, ToOwnedDeep},
|
||||
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
|
||||
};
|
||||
|
||||
use poulpy_core::{GLWEOperations, TakeGGLWE, TakeGLWECt, layouts::Infos};
|
||||
|
||||
use poulpy_core::layouts::{GGSWCiphertext, GLWECiphertext, LWECiphertext, prepared::GGLWEAutomorphismKeyPrepared};
|
||||
|
||||
use crate::tfhe::{
|
||||
blind_rotation::{
|
||||
BlincRotationExecute, BlindRotationAlgo, BlindRotationKeyPrepared, LookUpTable, LookUpTableRotationDirection,
|
||||
},
|
||||
circuit_bootstrapping::{CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute},
|
||||
};
|
||||
|
||||
impl<D: DataRef, BRA: BlindRotationAlgo, B> CirtuitBootstrappingExecute<B> for CircuitBootstrappingKeyPrepared<D, BRA, B>
|
||||
where
|
||||
Module<B>: VecZnxRotateInplace
|
||||
+ VecZnxNormalizeInplace<B>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxSwithcDegree
|
||||
+ VecZnxBigAutomorphismInplace<B>
|
||||
+ VecZnxRshInplace
|
||||
+ VecZnxDftCopy<B>
|
||||
+ VecZnxDftToVecZnxBigTmpA<B>
|
||||
+ VecZnxSub
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNegateInplace
|
||||
+ VecZnxCopy
|
||||
+ VecZnxSubABInplace
|
||||
+ VecZnxDftAllocBytes
|
||||
+ VmpApplyTmpBytes
|
||||
+ VecZnxBigNormalizeTmpBytes
|
||||
+ VmpApply<B>
|
||||
+ VmpApplyAdd<B>
|
||||
+ VecZnxDftFromVecZnx<B>
|
||||
+ VecZnxDftToVecZnxBigConsume<B>
|
||||
+ VecZnxBigAddSmallInplace<B>
|
||||
+ VecZnxBigNormalize<B>
|
||||
+ VecZnxAutomorphismInplace
|
||||
+ VecZnxBigSubSmallBInplace<B>
|
||||
+ VecZnxBigAllocBytes
|
||||
+ VecZnxDftAddInplace<B>
|
||||
+ VecZnxRotate,
|
||||
B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
|
||||
Scratch<B>: TakeVecZnx
|
||||
+ TakeVecZnxDftSlice<B>
|
||||
+ TakeVecZnxBig<B>
|
||||
+ TakeVecZnxDft<B>
|
||||
+ TakeMatZnx
|
||||
+ ScratchAvailable
|
||||
+ TakeVecZnxSlice,
|
||||
BlindRotationKeyPrepared<D, BRA, B>: BlincRotationExecute<B>,
|
||||
{
|
||||
fn execute_to_constant<DM: DataMut, DR: DataRef>(
|
||||
&self,
|
||||
module: &Module<B>,
|
||||
res: &mut GGSWCiphertext<DM>,
|
||||
lwe: &LWECiphertext<DR>,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
scratch: &mut Scratch<B>,
|
||||
) {
|
||||
circuit_bootstrap_core(
|
||||
false,
|
||||
module,
|
||||
0,
|
||||
res,
|
||||
lwe,
|
||||
log_domain,
|
||||
extension_factor,
|
||||
self,
|
||||
scratch,
|
||||
);
|
||||
}
|
||||
|
||||
fn execute_to_exponent<DM: DataMut, DR: DataRef>(
|
||||
&self,
|
||||
module: &Module<B>,
|
||||
log_gap_out: usize,
|
||||
res: &mut GGSWCiphertext<DM>,
|
||||
lwe: &LWECiphertext<DR>,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
scratch: &mut Scratch<B>,
|
||||
) {
|
||||
circuit_bootstrap_core(
|
||||
true,
|
||||
module,
|
||||
log_gap_out,
|
||||
res,
|
||||
lwe,
|
||||
log_domain,
|
||||
extension_factor,
|
||||
self,
|
||||
scratch,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
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>,
|
||||
lwe: &LWECiphertext<DLwe>,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
key: &CircuitBootstrappingKeyPrepared<DBrk, BRA, B>,
|
||||
scratch: &mut Scratch<B>,
|
||||
) where
|
||||
DRes: DataMut,
|
||||
DLwe: DataRef,
|
||||
DBrk: DataRef,
|
||||
Module<B>: VecZnxRotateInplace
|
||||
+ VecZnxNormalizeInplace<B>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxSwithcDegree
|
||||
+ VecZnxBigAutomorphismInplace<B>
|
||||
+ VecZnxRshInplace
|
||||
+ VecZnxDftCopy<B>
|
||||
+ VecZnxDftToVecZnxBigTmpA<B>
|
||||
+ VecZnxSub
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNegateInplace
|
||||
+ VecZnxCopy
|
||||
+ VecZnxSubABInplace
|
||||
+ VecZnxDftAllocBytes
|
||||
+ VmpApplyTmpBytes
|
||||
+ VecZnxBigNormalizeTmpBytes
|
||||
+ VmpApply<B>
|
||||
+ VmpApplyAdd<B>
|
||||
+ VecZnxDftFromVecZnx<B>
|
||||
+ VecZnxDftToVecZnxBigConsume<B>
|
||||
+ VecZnxBigAddSmallInplace<B>
|
||||
+ VecZnxBigNormalize<B>
|
||||
+ VecZnxAutomorphismInplace
|
||||
+ VecZnxBigSubSmallBInplace<B>
|
||||
+ VecZnxBigAllocBytes
|
||||
+ VecZnxDftAddInplace<B>
|
||||
+ VecZnxRotate,
|
||||
B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
|
||||
Scratch<B>: TakeVecZnxDftSlice<B>
|
||||
+ TakeVecZnxBig<B>
|
||||
+ TakeVecZnxDft<B>
|
||||
+ TakeVecZnx
|
||||
+ ScratchAvailable
|
||||
+ TakeVecZnxSlice
|
||||
+ TakeMatZnx,
|
||||
BlindRotationKeyPrepared<DBrk, BRA, B>: BlincRotationExecute<B>,
|
||||
{
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert_eq!(res.n(), key.brk.n());
|
||||
assert_eq!(lwe.basek(), key.brk.basek());
|
||||
assert_eq!(res.basek(), key.brk.basek());
|
||||
}
|
||||
|
||||
let n: usize = res.n();
|
||||
let basek: usize = res.basek();
|
||||
let rows: usize = res.rows();
|
||||
let rank: usize = res.rank();
|
||||
let k: usize = res.k();
|
||||
|
||||
let alpha: usize = rows.next_power_of_two();
|
||||
|
||||
let mut f: Vec<i64> = vec![0i64; (1 << log_domain) * alpha];
|
||||
|
||||
if to_exponent {
|
||||
(0..rows).for_each(|i| {
|
||||
f[i] = 1 << (basek * (rows - 1 - i));
|
||||
});
|
||||
} else {
|
||||
(0..1 << log_domain).for_each(|j| {
|
||||
(0..rows).for_each(|i| {
|
||||
f[j * alpha + i] = j as i64 * (1 << (basek * (rows - 1 - i)));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Lut precision, basically must be able to hold the decomposition power basis of the GGSW
|
||||
let mut lut: LookUpTable = LookUpTable::alloc(n, basek, basek * rows, extension_factor);
|
||||
lut.set(module, &f, basek * rows);
|
||||
|
||||
if to_exponent {
|
||||
lut.set_rotation_direction(LookUpTableRotationDirection::Right);
|
||||
}
|
||||
|
||||
// TODO: separate GGSW k from output of blind rotation k
|
||||
let (mut res_glwe, scratch1) = scratch.take_glwe_ct(n, basek, k, rank);
|
||||
let (mut tmp_gglwe, scratch2) = scratch1.take_gglwe(n, basek, k, rows, 1, rank.max(1), rank);
|
||||
|
||||
key.brk.execute(module, &mut res_glwe, lwe, &lut, scratch2);
|
||||
|
||||
let gap: usize = 2 * lut.drift / lut.extension_factor();
|
||||
|
||||
let log_gap_in: usize = (usize::BITS - (gap * alpha - 1).leading_zeros()) as _;
|
||||
|
||||
(0..rows).for_each(|i| {
|
||||
let mut tmp_glwe: GLWECiphertext<&mut [u8]> = tmp_gglwe.at_mut(i, 0);
|
||||
|
||||
if to_exponent {
|
||||
// Isolates i-th LUT and moves coefficients according to requested gap.
|
||||
post_process(
|
||||
module,
|
||||
&mut tmp_glwe,
|
||||
&res_glwe,
|
||||
log_gap_in,
|
||||
log_gap_out,
|
||||
log_domain,
|
||||
&key.atk,
|
||||
scratch2,
|
||||
);
|
||||
} else {
|
||||
tmp_glwe.trace(module, 0, module.log_n(), &res_glwe, &key.atk, scratch2);
|
||||
}
|
||||
|
||||
if i < rows {
|
||||
res_glwe.rotate_inplace(module, -(gap as i64));
|
||||
}
|
||||
});
|
||||
|
||||
// Expands GGLWE to GGSW using GGLWE(s^2)
|
||||
res.from_gglwe(module, &tmp_gglwe, &key.tsk, scratch2);
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn post_process<DataRes, DataA, B: Backend>(
|
||||
module: &Module<B>,
|
||||
res: &mut GLWECiphertext<DataRes>,
|
||||
a: &GLWECiphertext<DataA>,
|
||||
log_gap_in: usize,
|
||||
log_gap_out: usize,
|
||||
log_domain: usize,
|
||||
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>>,
|
||||
scratch: &mut Scratch<B>,
|
||||
) where
|
||||
DataRes: DataMut,
|
||||
DataA: DataRef,
|
||||
Module<B>: VecZnxRotateInplace
|
||||
+ VecZnxNormalizeInplace<B>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxSwithcDegree
|
||||
+ VecZnxBigAutomorphismInplace<B>
|
||||
+ VecZnxRshInplace
|
||||
+ VecZnxDftCopy<B>
|
||||
+ VecZnxDftToVecZnxBigTmpA<B>
|
||||
+ VecZnxSub
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNegateInplace
|
||||
+ VecZnxCopy
|
||||
+ VecZnxSubABInplace
|
||||
+ VecZnxDftAllocBytes
|
||||
+ VmpApplyTmpBytes
|
||||
+ VecZnxBigNormalizeTmpBytes
|
||||
+ VmpApply<B>
|
||||
+ VmpApplyAdd<B>
|
||||
+ VecZnxDftFromVecZnx<B>
|
||||
+ VecZnxDftToVecZnxBigConsume<B>
|
||||
+ VecZnxBigAddSmallInplace<B>
|
||||
+ VecZnxBigNormalize<B>
|
||||
+ VecZnxAutomorphismInplace
|
||||
+ VecZnxBigSubSmallBInplace<B>
|
||||
+ VecZnxRotate,
|
||||
Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx,
|
||||
{
|
||||
let log_n: usize = module.log_n();
|
||||
|
||||
let mut cts: HashMap<usize, GLWECiphertext<Vec<u8>>> = HashMap::new();
|
||||
|
||||
// First partial trace, vanishes all coefficients which are not multiples of gap_in
|
||||
// [1, 1, 1, 1, 0, 0, 0, ..., 0, 0, -1, -1, -1, -1] -> [1, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0]
|
||||
res.trace(
|
||||
module,
|
||||
module.log_n() - log_gap_in + 1,
|
||||
log_n,
|
||||
a,
|
||||
auto_keys,
|
||||
scratch,
|
||||
);
|
||||
|
||||
// TODO: optimize with packing and final partial trace
|
||||
// If gap_out < gap_in, then we need to repack, i.e. reduce the cap between coefficients.
|
||||
if log_gap_in != log_gap_out {
|
||||
let steps: i32 = 1 << log_domain;
|
||||
(0..steps).for_each(|i| {
|
||||
if i != 0 {
|
||||
res.rotate_inplace(module, -(1 << log_gap_in));
|
||||
}
|
||||
cts.insert(i as usize * (1 << log_gap_out), res.to_owned_deep());
|
||||
});
|
||||
pack(module, &mut cts, log_gap_out, auto_keys, scratch);
|
||||
let packed: GLWECiphertext<Vec<u8>> = cts.remove(&0).unwrap();
|
||||
res.trace(
|
||||
module,
|
||||
log_n - log_gap_out,
|
||||
log_n,
|
||||
&packed,
|
||||
auto_keys,
|
||||
scratch,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pack<D: DataMut, B: Backend>(
|
||||
module: &Module<B>,
|
||||
cts: &mut HashMap<usize, GLWECiphertext<D>>,
|
||||
log_gap_out: usize,
|
||||
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>>,
|
||||
scratch: &mut Scratch<B>,
|
||||
) where
|
||||
Module<B>: VecZnxRotateInplace
|
||||
+ VecZnxNormalizeInplace<B>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxSwithcDegree
|
||||
+ VecZnxBigAutomorphismInplace<B>
|
||||
+ VecZnxRshInplace
|
||||
+ VecZnxDftCopy<B>
|
||||
+ VecZnxDftToVecZnxBigTmpA<B>
|
||||
+ VecZnxSub
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNegateInplace
|
||||
+ VecZnxCopy
|
||||
+ VecZnxSubABInplace
|
||||
+ VecZnxDftAllocBytes
|
||||
+ VmpApplyTmpBytes
|
||||
+ VecZnxBigNormalizeTmpBytes
|
||||
+ VmpApply<B>
|
||||
+ VmpApplyAdd<B>
|
||||
+ VecZnxDftFromVecZnx<B>
|
||||
+ VecZnxDftToVecZnxBigConsume<B>
|
||||
+ VecZnxBigAddSmallInplace<B>
|
||||
+ VecZnxBigNormalize<B>
|
||||
+ VecZnxAutomorphismInplace
|
||||
+ VecZnxBigSubSmallBInplace<B>
|
||||
+ VecZnxRotate,
|
||||
Scratch<B>: TakeVecZnx + TakeVecZnxDft<B> + ScratchAvailable,
|
||||
{
|
||||
let log_n: usize = module.log_n();
|
||||
|
||||
let basek: usize = cts.get(&0).unwrap().basek();
|
||||
let k: usize = cts.get(&0).unwrap().k();
|
||||
let rank: usize = cts.get(&0).unwrap().rank();
|
||||
|
||||
(0..log_n - log_gap_out).for_each(|i| {
|
||||
let t: usize = 16.min(1 << (log_n - 1 - i));
|
||||
|
||||
let auto_key: &GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = if i == 0 {
|
||||
auto_keys.get(&-1).unwrap()
|
||||
} else {
|
||||
auto_keys.get(&module.galois_element(1 << (i - 1))).unwrap()
|
||||
};
|
||||
|
||||
(0..t).for_each(|j| {
|
||||
let mut a: Option<GLWECiphertext<D>> = cts.remove(&j);
|
||||
let mut b: Option<GLWECiphertext<D>> = cts.remove(&(j + t));
|
||||
|
||||
combine(
|
||||
module,
|
||||
basek,
|
||||
k,
|
||||
rank,
|
||||
a.as_mut(),
|
||||
b.as_mut(),
|
||||
i,
|
||||
auto_key,
|
||||
scratch,
|
||||
);
|
||||
|
||||
if let Some(a) = a {
|
||||
cts.insert(j, a);
|
||||
} else if let Some(b) = b {
|
||||
cts.insert(j, b);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn combine<A: DataMut, D: DataMut, DataAK: DataRef, B: Backend>(
|
||||
module: &Module<B>,
|
||||
basek: usize,
|
||||
k: usize,
|
||||
rank: usize,
|
||||
a: Option<&mut GLWECiphertext<A>>,
|
||||
b: Option<&mut GLWECiphertext<D>>,
|
||||
i: usize,
|
||||
auto_key: &GGLWEAutomorphismKeyPrepared<DataAK, B>,
|
||||
scratch: &mut Scratch<B>,
|
||||
) where
|
||||
Module<B>: VecZnxRotateInplace
|
||||
+ VecZnxNormalizeInplace<B>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxSwithcDegree
|
||||
+ VecZnxBigAutomorphismInplace<B>
|
||||
+ VecZnxRshInplace
|
||||
+ VecZnxDftCopy<B>
|
||||
+ VecZnxDftToVecZnxBigTmpA<B>
|
||||
+ VecZnxSub
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNegateInplace
|
||||
+ VecZnxCopy
|
||||
+ VecZnxSubABInplace
|
||||
+ VecZnxDftAllocBytes
|
||||
+ VmpApplyTmpBytes
|
||||
+ VecZnxBigNormalizeTmpBytes
|
||||
+ VmpApply<B>
|
||||
+ VmpApplyAdd<B>
|
||||
+ VecZnxDftFromVecZnx<B>
|
||||
+ VecZnxDftToVecZnxBigConsume<B>
|
||||
+ VecZnxBigAddSmallInplace<B>
|
||||
+ VecZnxBigNormalize<B>
|
||||
+ VecZnxAutomorphismInplace
|
||||
+ VecZnxBigSubSmallBInplace<B>
|
||||
+ VecZnxRotate,
|
||||
Scratch<B>: TakeVecZnx + TakeVecZnxDft<B> + ScratchAvailable,
|
||||
{
|
||||
// Goal is to evaluate: a = a + b*X^t + phi(a - b*X^t))
|
||||
// We also use the identity: AUTO(a * X^t, g) = -X^t * AUTO(a, g)
|
||||
// where t = 2^(log_n - i - 1) and g = 5^{2^(i - 1)}
|
||||
// Different cases for wether a and/or b are zero.
|
||||
//
|
||||
// Implicite RSH without modulus switch, introduces extra I(X) * Q/2 on decryption.
|
||||
// Necessary so that the scaling of the plaintext remains constant.
|
||||
// It however is ok to do so here because coefficients are eventually
|
||||
// either mapped to garbage or twice their value which vanishes I(X)
|
||||
// since 2*(I(X) * Q/2) = I(X) * Q = 0 mod Q.
|
||||
if let Some(a) = a {
|
||||
let n: usize = a.n();
|
||||
let log_n: usize = (u64::BITS - (n - 1).leading_zeros()) as _;
|
||||
let t: i64 = 1 << (log_n - i - 1);
|
||||
|
||||
if let Some(b) = b {
|
||||
let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(n, basek, k, rank);
|
||||
|
||||
// a = a * X^-t
|
||||
a.rotate_inplace(module, -t);
|
||||
|
||||
// tmp_b = a * X^-t - b
|
||||
tmp_b.sub(module, a, b);
|
||||
tmp_b.rsh(module, 1);
|
||||
|
||||
// a = a * X^-t + b
|
||||
a.add_inplace(module, b);
|
||||
a.rsh(module, 1);
|
||||
|
||||
tmp_b.normalize_inplace(module, scratch_1);
|
||||
|
||||
// tmp_b = phi(a * X^-t - b)
|
||||
tmp_b.automorphism_inplace(module, auto_key, scratch_1);
|
||||
|
||||
// a = a * X^-t + b - phi(a * X^-t - b)
|
||||
a.sub_inplace_ab(module, &tmp_b);
|
||||
a.normalize_inplace(module, scratch_1);
|
||||
|
||||
// a = a + b * X^t - phi(a * X^-t - b) * X^t
|
||||
// = a + b * X^t - phi(a * X^-t - b) * - phi(X^t)
|
||||
// = a + b * X^t + phi(a - b * X^t)
|
||||
a.rotate_inplace(module, t);
|
||||
} else {
|
||||
a.rsh(module, 1);
|
||||
// a = a + phi(a)
|
||||
a.automorphism_add_inplace(module, auto_key, scratch);
|
||||
}
|
||||
} else if let Some(b) = b {
|
||||
let n: usize = b.n();
|
||||
let log_n: usize = (u64::BITS - (n - 1).leading_zeros()) as _;
|
||||
let t: i64 = 1 << (log_n - i - 1);
|
||||
|
||||
let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(n, basek, k, rank);
|
||||
tmp_b.rotate(module, t, b);
|
||||
tmp_b.rsh(module, 1);
|
||||
|
||||
// a = (b* X^t - phi(b* X^t))
|
||||
b.automorphism_sub_ba(module, &tmp_b, auto_key, scratch_1);
|
||||
}
|
||||
}
|
||||
165
poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs
Normal file
165
poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs
Normal file
@@ -0,0 +1,165 @@
|
||||
use poulpy_core::layouts::{
|
||||
GGLWEAutomorphismKey, GGLWETensorKey, GLWECiphertext, GLWESecret, LWESecret,
|
||||
prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, PrepareAlloc},
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use poulpy_backend::hal::{
|
||||
api::{
|
||||
ScratchAvailable, SvpApply, SvpApplyInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeSvpPPol,
|
||||
TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism,
|
||||
VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftFromVecZnx, VecZnxDftToVecZnxBigConsume, VecZnxDftToVecZnxBigTmpA,
|
||||
VecZnxFillUniform, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubABInplace,
|
||||
VecZnxSwithcDegree, VmpPMatAlloc, VmpPrepare,
|
||||
},
|
||||
layouts::{Backend, Data, DataRef, Module, Scratch},
|
||||
source::Source,
|
||||
};
|
||||
|
||||
use crate::tfhe::blind_rotation::{
|
||||
BlindRotationAlgo, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk, BlindRotationKeyPrepared,
|
||||
};
|
||||
|
||||
pub trait CircuitBootstrappingKeyEncryptSk<B: Backend> {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn encrypt_sk<DLwe, DGlwe>(
|
||||
module: &Module<B>,
|
||||
basek: usize,
|
||||
sk_lwe: &LWESecret<DLwe>,
|
||||
sk_glwe: &GLWESecret<DGlwe>,
|
||||
k_brk: usize,
|
||||
rows_brk: usize,
|
||||
k_trace: usize,
|
||||
rows_trace: usize,
|
||||
k_tsk: usize,
|
||||
rows_tsk: usize,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
sigma: f64,
|
||||
scratch: &mut Scratch<B>,
|
||||
) -> Self
|
||||
where
|
||||
DLwe: DataRef,
|
||||
DGlwe: DataRef;
|
||||
}
|
||||
|
||||
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>>>,
|
||||
}
|
||||
|
||||
impl<BRA: BlindRotationAlgo, B: Backend> CircuitBootstrappingKeyEncryptSk<B> for CircuitBootstrappingKey<Vec<u8>, BRA>
|
||||
where
|
||||
BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk<B>,
|
||||
Module<B>: SvpApply<B>
|
||||
+ VecZnxDftToVecZnxBigTmpA<B>
|
||||
+ VecZnxAddScalarInplace
|
||||
+ VecZnxDftAllocBytes
|
||||
+ VecZnxBigNormalize<B>
|
||||
+ VecZnxDftFromVecZnx<B>
|
||||
+ SvpApplyInplace<B>
|
||||
+ VecZnxDftToVecZnxBigConsume<B>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxFillUniform
|
||||
+ VecZnxSubABInplace
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNormalizeInplace<B>
|
||||
+ VecZnxAddNormal
|
||||
+ VecZnxNormalize<B>
|
||||
+ VecZnxSub
|
||||
+ SvpPrepare<B>
|
||||
+ VecZnxSwithcDegree
|
||||
+ SvpPPolAllocBytes
|
||||
+ SvpPPolAlloc<B>
|
||||
+ VecZnxAutomorphism,
|
||||
Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeSvpPPol<B> + TakeVecZnxBig<B>,
|
||||
{
|
||||
fn encrypt_sk<DLwe, DGlwe>(
|
||||
module: &Module<B>,
|
||||
basek: usize,
|
||||
sk_lwe: &LWESecret<DLwe>,
|
||||
sk_glwe: &GLWESecret<DGlwe>,
|
||||
k_brk: usize,
|
||||
rows_brk: usize,
|
||||
k_trace: usize,
|
||||
rows_trace: usize,
|
||||
k_tsk: usize,
|
||||
rows_tsk: usize,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
sigma: f64,
|
||||
scratch: &mut Scratch<B>,
|
||||
) -> Self
|
||||
where
|
||||
DLwe: DataRef,
|
||||
DGlwe: DataRef,
|
||||
Module<B>:,
|
||||
{
|
||||
let mut auto_keys: HashMap<i64, GGLWEAutomorphismKey<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(sk_glwe.n(), basek, k_trace, rows_trace, 1, sk_glwe.rank());
|
||||
key.encrypt_sk(
|
||||
module, *gal_el, sk_glwe, source_xa, source_xe, sigma, scratch,
|
||||
);
|
||||
auto_keys.insert(*gal_el, key);
|
||||
});
|
||||
|
||||
let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch);
|
||||
|
||||
let mut brk: BlindRotationKey<Vec<u8>, BRA> = BlindRotationKey::<Vec<u8>, BRA>::alloc(
|
||||
sk_glwe.n(),
|
||||
sk_lwe.n(),
|
||||
basek,
|
||||
k_brk,
|
||||
rows_brk,
|
||||
sk_glwe.rank(),
|
||||
);
|
||||
|
||||
brk.encrypt_sk(
|
||||
module,
|
||||
&sk_glwe_prepared,
|
||||
sk_lwe,
|
||||
source_xa,
|
||||
source_xe,
|
||||
sigma,
|
||||
scratch,
|
||||
);
|
||||
|
||||
let mut tsk: GGLWETensorKey<Vec<u8>> = GGLWETensorKey::alloc(sk_glwe.n(), basek, k_tsk, rows_tsk, 1, sk_glwe.rank());
|
||||
tsk.encrypt_sk(module, sk_glwe, source_xa, source_xe, sigma, scratch);
|
||||
|
||||
Self {
|
||||
brk,
|
||||
atk: auto_keys,
|
||||
tsk,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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>>,
|
||||
}
|
||||
|
||||
impl<D: DataRef, BRA: BlindRotationAlgo, B: Backend> PrepareAlloc<B, CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B>>
|
||||
for CircuitBootstrappingKey<D, BRA>
|
||||
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>>,
|
||||
{
|
||||
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();
|
||||
for (key, value) in &self.atk {
|
||||
atk.insert(*key, value.prepare_alloc(module, scratch));
|
||||
}
|
||||
CircuitBootstrappingKeyPrepared { brk, tsk, atk }
|
||||
}
|
||||
}
|
||||
34
poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs
Normal file
34
poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
mod circuit;
|
||||
mod key;
|
||||
pub mod tests;
|
||||
|
||||
pub use circuit::*;
|
||||
pub use key::*;
|
||||
|
||||
use poulpy_core::layouts::{GGSWCiphertext, LWECiphertext};
|
||||
|
||||
use poulpy_backend::hal::layouts::{Backend, DataMut, DataRef, Module, Scratch};
|
||||
|
||||
pub trait CirtuitBootstrappingExecute<B: Backend> {
|
||||
fn execute_to_constant<DM: DataMut, DR: DataRef>(
|
||||
&self,
|
||||
module: &Module<B>,
|
||||
res: &mut GGSWCiphertext<DM>,
|
||||
lwe: &LWECiphertext<DR>,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
scratch: &mut Scratch<B>,
|
||||
);
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn execute_to_exponent<DM: DataMut, DR: DataRef>(
|
||||
&self,
|
||||
module: &Module<B>,
|
||||
log_gap_out: usize,
|
||||
res: &mut GGSWCiphertext<DM>,
|
||||
lwe: &LWECiphertext<DR>,
|
||||
log_domain: usize,
|
||||
extension_factor: usize,
|
||||
scratch: &mut Scratch<B>,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,409 @@
|
||||
use std::time::Instant;
|
||||
|
||||
use poulpy_backend::hal::{
|
||||
api::{
|
||||
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApply, SvpApplyInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare,
|
||||
VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace,
|
||||
VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace,
|
||||
VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallBInplace, VecZnxCopy, VecZnxDftAddInplace,
|
||||
VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftCopy, VecZnxDftFromVecZnx, VecZnxDftToVecZnxBigConsume,
|
||||
VecZnxDftToVecZnxBigTmpA, VecZnxFillUniform, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace,
|
||||
VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubABInplace,
|
||||
VecZnxSwithcDegree, VmpApply, VmpApplyAdd, VmpApplyTmpBytes, VmpPMatAlloc, VmpPrepare, ZnxView, ZnxViewMut,
|
||||
},
|
||||
layouts::{Backend, Module, ScalarZnx, ScratchOwned},
|
||||
oep::{
|
||||
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeMatZnxImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
|
||||
TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl,
|
||||
},
|
||||
source::Source,
|
||||
};
|
||||
|
||||
use crate::tfhe::{
|
||||
blind_rotation::{
|
||||
BlincRotationExecute, BlindRotationAlgo, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk,
|
||||
BlindRotationKeyPrepared,
|
||||
},
|
||||
circuit_bootstrapping::{
|
||||
CircuitBootstrappingKey, CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute,
|
||||
},
|
||||
};
|
||||
|
||||
use poulpy_core::layouts::prepared::PrepareAlloc;
|
||||
|
||||
use poulpy_core::layouts::{
|
||||
GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWEPlaintext, LWESecret,
|
||||
prepared::{GGSWCiphertextPrepared, GLWESecretPrepared},
|
||||
};
|
||||
|
||||
pub fn test_circuit_bootstrapping_to_exponent<B, BRA: BlindRotationAlgo>(module: &Module<B>)
|
||||
where
|
||||
Module<B>: VecZnxFillUniform
|
||||
+ VecZnxAddNormal
|
||||
+ VecZnxNormalizeInplace<B>
|
||||
+ VecZnxDftAllocBytes
|
||||
+ VecZnxBigNormalize<B>
|
||||
+ VecZnxDftFromVecZnx<B>
|
||||
+ SvpApplyInplace<B>
|
||||
+ VecZnxDftToVecZnxBigConsume<B>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxSubABInplace
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNormalize<B>
|
||||
+ VecZnxSub
|
||||
+ VecZnxAddScalarInplace
|
||||
+ VecZnxAutomorphism
|
||||
+ VecZnxSwithcDegree
|
||||
+ VecZnxBigAllocBytes
|
||||
+ VecZnxDftToVecZnxBigTmpA<B>
|
||||
+ SvpApply<B>
|
||||
+ VecZnxBigAddInplace<B>
|
||||
+ VecZnxBigAddSmallInplace<B>
|
||||
+ VecZnxBigAlloc<B>
|
||||
+ VecZnxDftAlloc<B>
|
||||
+ VecZnxBigNormalizeTmpBytes
|
||||
+ VmpPMatAlloc<B>
|
||||
+ VmpPrepare<B>
|
||||
+ SvpPrepare<B>
|
||||
+ SvpPPolAlloc<B>
|
||||
+ VmpApplyTmpBytes
|
||||
+ VmpApply<B>
|
||||
+ VmpApplyAdd<B>
|
||||
+ SvpPPolAllocBytes
|
||||
+ VecZnxRotateInplace
|
||||
+ VecZnxBigAutomorphismInplace<B>
|
||||
+ VecZnxRshInplace
|
||||
+ VecZnxDftCopy<B>
|
||||
+ VecZnxNegateInplace
|
||||
+ VecZnxCopy
|
||||
+ VecZnxAutomorphismInplace
|
||||
+ VecZnxBigSubSmallBInplace<B>
|
||||
+ VecZnxBigAllocBytes
|
||||
+ VecZnxDftAddInplace<B>
|
||||
+ VecZnxRotate,
|
||||
B: Backend
|
||||
+ ScratchOwnedAllocImpl<B>
|
||||
+ ScratchOwnedBorrowImpl<B>
|
||||
+ TakeVecZnxDftImpl<B>
|
||||
+ ScratchAvailableImpl<B>
|
||||
+ TakeVecZnxImpl<B>
|
||||
+ TakeScalarZnxImpl<B>
|
||||
+ TakeSvpPPolImpl<B>
|
||||
+ TakeVecZnxBigImpl<B>
|
||||
+ TakeVecZnxDftSliceImpl<B>
|
||||
+ TakeMatZnxImpl<B>
|
||||
+ TakeVecZnxSliceImpl<B>,
|
||||
BlindRotationKey<Vec<u8>, BRA>: PrepareAlloc<B, BlindRotationKeyPrepared<Vec<u8>, BRA, B>>,
|
||||
BlindRotationKeyPrepared<Vec<u8>, BRA, B>: BlincRotationExecute<B>,
|
||||
BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk<B>,
|
||||
{
|
||||
let n: usize = module.n();
|
||||
let basek: usize = 17;
|
||||
let extension_factor: usize = 1;
|
||||
let rank: usize = 1;
|
||||
let sigma: f64 = 3.2;
|
||||
|
||||
let n_lwe: usize = 77;
|
||||
let k_lwe_pt: usize = 4;
|
||||
let k_lwe_ct: usize = 22;
|
||||
let block_size: usize = 7;
|
||||
|
||||
let k_brk: usize = 5 * basek;
|
||||
let rows_brk: usize = 4;
|
||||
|
||||
let k_trace: usize = 5 * basek;
|
||||
let rows_trace: usize = 4;
|
||||
|
||||
let k_tsk: usize = 5 * basek;
|
||||
let rows_tsk: usize = 4;
|
||||
|
||||
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(1 << 23);
|
||||
|
||||
let mut source_xs: Source = Source::new([1u8; 32]);
|
||||
let mut source_xa: Source = Source::new([1u8; 32]);
|
||||
let mut source_xe: Source = Source::new([1u8; 32]);
|
||||
|
||||
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
|
||||
sk_lwe.fill_binary_block(block_size, &mut source_xs);
|
||||
|
||||
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
|
||||
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
|
||||
|
||||
let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
|
||||
|
||||
let data: i64 = 1;
|
||||
|
||||
let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_pt);
|
||||
pt_lwe.encode_i64(data, k_lwe_pt + 1);
|
||||
|
||||
println!("pt_lwe: {}", pt_lwe);
|
||||
|
||||
let mut ct_lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe_ct);
|
||||
ct_lwe.encrypt_sk(
|
||||
module,
|
||||
&pt_lwe,
|
||||
&sk_lwe,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
sigma,
|
||||
);
|
||||
|
||||
let now: Instant = Instant::now();
|
||||
let cbt_key: CircuitBootstrappingKey<Vec<u8>, BRA> = CircuitBootstrappingKey::encrypt_sk(
|
||||
module,
|
||||
basek,
|
||||
&sk_lwe,
|
||||
&sk_glwe,
|
||||
k_brk,
|
||||
rows_brk,
|
||||
k_trace,
|
||||
rows_trace,
|
||||
k_tsk,
|
||||
rows_tsk,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
sigma,
|
||||
scratch.borrow(),
|
||||
);
|
||||
println!("CBT-KGEN: {} ms", now.elapsed().as_millis());
|
||||
|
||||
let k_ggsw_res: usize = 4 * basek;
|
||||
let rows_ggsw_res: usize = 2;
|
||||
|
||||
let mut res: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(n, basek, k_ggsw_res, rows_ggsw_res, 1, rank);
|
||||
|
||||
let log_gap_out = 1;
|
||||
|
||||
let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow());
|
||||
|
||||
let now: Instant = Instant::now();
|
||||
cbt_prepared.execute_to_exponent(
|
||||
module,
|
||||
log_gap_out,
|
||||
&mut res,
|
||||
&ct_lwe,
|
||||
k_lwe_pt,
|
||||
extension_factor,
|
||||
scratch.borrow(),
|
||||
);
|
||||
println!("CBT: {} ms", now.elapsed().as_millis());
|
||||
|
||||
// X^{data * 2^log_gap_out}
|
||||
let mut pt_ggsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
|
||||
pt_ggsw.at_mut(0, 0)[0] = 1;
|
||||
module.vec_znx_rotate_inplace(data * (1 << log_gap_out), &mut pt_ggsw.as_vec_znx_mut(), 0);
|
||||
|
||||
res.print_noise(module, &sk_glwe_prepared, &pt_ggsw);
|
||||
|
||||
let k_glwe: usize = k_ggsw_res;
|
||||
|
||||
let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_glwe, rank);
|
||||
let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, basek);
|
||||
pt_glwe.data.at_mut(0, 0)[0] = 1 << (basek - 2);
|
||||
|
||||
ct_glwe.encrypt_sk(
|
||||
module,
|
||||
&pt_glwe,
|
||||
&sk_glwe_prepared,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
sigma,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
let res_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = res.prepare_alloc(module, scratch.borrow());
|
||||
|
||||
ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow());
|
||||
|
||||
let mut pt_res: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_glwe);
|
||||
ct_glwe.decrypt(module, &mut pt_res, &sk_glwe_prepared, scratch.borrow());
|
||||
|
||||
// Parameters are set such that the first limb should be noiseless.
|
||||
let mut pt_want: Vec<i64> = vec![0i64; module.n()];
|
||||
pt_want[data as usize * (1 << log_gap_out)] = pt_glwe.data.at(0, 0)[0];
|
||||
assert_eq!(pt_res.data.at(0, 0), pt_want);
|
||||
}
|
||||
|
||||
pub fn test_circuit_bootstrapping_to_constant<B, BRA: BlindRotationAlgo>(module: &Module<B>)
|
||||
where
|
||||
Module<B>: VecZnxFillUniform
|
||||
+ VecZnxAddNormal
|
||||
+ VecZnxNormalizeInplace<B>
|
||||
+ VecZnxDftAllocBytes
|
||||
+ VecZnxBigNormalize<B>
|
||||
+ VecZnxDftFromVecZnx<B>
|
||||
+ SvpApplyInplace<B>
|
||||
+ VecZnxDftToVecZnxBigConsume<B>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxSubABInplace
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNormalize<B>
|
||||
+ VecZnxSub
|
||||
+ VecZnxAddScalarInplace
|
||||
+ VecZnxAutomorphism
|
||||
+ VecZnxSwithcDegree
|
||||
+ VecZnxBigAllocBytes
|
||||
+ VecZnxDftToVecZnxBigTmpA<B>
|
||||
+ SvpApply<B>
|
||||
+ VecZnxBigAddInplace<B>
|
||||
+ VecZnxBigAddSmallInplace<B>
|
||||
+ VecZnxBigAlloc<B>
|
||||
+ VecZnxDftAlloc<B>
|
||||
+ VecZnxBigNormalizeTmpBytes
|
||||
+ VmpPMatAlloc<B>
|
||||
+ VmpPrepare<B>
|
||||
+ SvpPrepare<B>
|
||||
+ SvpPPolAlloc<B>
|
||||
+ VmpApplyTmpBytes
|
||||
+ VmpApply<B>
|
||||
+ VmpApplyAdd<B>
|
||||
+ SvpPPolAllocBytes
|
||||
+ VecZnxRotateInplace
|
||||
+ VecZnxBigAutomorphismInplace<B>
|
||||
+ VecZnxRshInplace
|
||||
+ VecZnxDftCopy<B>
|
||||
+ VecZnxNegateInplace
|
||||
+ VecZnxCopy
|
||||
+ VecZnxAutomorphismInplace
|
||||
+ VecZnxBigSubSmallBInplace<B>
|
||||
+ VecZnxBigAllocBytes
|
||||
+ VecZnxDftAddInplace<B>
|
||||
+ VecZnxRotate,
|
||||
B: Backend
|
||||
+ ScratchOwnedAllocImpl<B>
|
||||
+ ScratchOwnedBorrowImpl<B>
|
||||
+ TakeVecZnxDftImpl<B>
|
||||
+ ScratchAvailableImpl<B>
|
||||
+ TakeVecZnxImpl<B>
|
||||
+ TakeScalarZnxImpl<B>
|
||||
+ TakeSvpPPolImpl<B>
|
||||
+ TakeVecZnxBigImpl<B>
|
||||
+ TakeVecZnxDftSliceImpl<B>
|
||||
+ TakeMatZnxImpl<B>
|
||||
+ TakeVecZnxSliceImpl<B>,
|
||||
BlindRotationKey<Vec<u8>, BRA>: PrepareAlloc<B, BlindRotationKeyPrepared<Vec<u8>, BRA, B>>,
|
||||
BlindRotationKeyPrepared<Vec<u8>, BRA, B>: BlincRotationExecute<B>,
|
||||
BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk<B>,
|
||||
{
|
||||
let n = module.n();
|
||||
let basek: usize = 14;
|
||||
let extension_factor: usize = 1;
|
||||
let rank: usize = 2;
|
||||
let sigma: f64 = 3.2;
|
||||
|
||||
let n_lwe: usize = 77;
|
||||
let k_lwe_pt: usize = 1;
|
||||
let k_lwe_ct: usize = 13;
|
||||
let block_size: usize = 7;
|
||||
|
||||
let k_brk: usize = 5 * basek;
|
||||
let rows_brk: usize = 3;
|
||||
|
||||
let k_trace: usize = 5 * basek;
|
||||
let rows_trace: usize = 4;
|
||||
|
||||
let k_tsk: usize = 5 * basek;
|
||||
let rows_tsk: usize = 4;
|
||||
|
||||
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(1 << 23);
|
||||
|
||||
let mut source_xs: Source = Source::new([1u8; 32]);
|
||||
let mut source_xa: Source = Source::new([1u8; 32]);
|
||||
let mut source_xe: Source = Source::new([1u8; 32]);
|
||||
|
||||
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
|
||||
sk_lwe.fill_binary_block(block_size, &mut source_xs);
|
||||
|
||||
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
|
||||
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
|
||||
|
||||
let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
|
||||
|
||||
let data: i64 = 1;
|
||||
|
||||
let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_pt);
|
||||
pt_lwe.encode_i64(data, k_lwe_pt + 1);
|
||||
|
||||
println!("pt_lwe: {}", pt_lwe);
|
||||
|
||||
let mut ct_lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe_ct);
|
||||
ct_lwe.encrypt_sk(
|
||||
module,
|
||||
&pt_lwe,
|
||||
&sk_lwe,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
sigma,
|
||||
);
|
||||
|
||||
let now: Instant = Instant::now();
|
||||
let cbt_key: CircuitBootstrappingKey<Vec<u8>, BRA> = CircuitBootstrappingKey::encrypt_sk(
|
||||
module,
|
||||
basek,
|
||||
&sk_lwe,
|
||||
&sk_glwe,
|
||||
k_brk,
|
||||
rows_brk,
|
||||
k_trace,
|
||||
rows_trace,
|
||||
k_tsk,
|
||||
rows_tsk,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
sigma,
|
||||
scratch.borrow(),
|
||||
);
|
||||
println!("CBT-KGEN: {} ms", now.elapsed().as_millis());
|
||||
|
||||
let k_ggsw_res: usize = 4 * basek;
|
||||
let rows_ggsw_res: usize = 3;
|
||||
|
||||
let mut res: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(n, basek, k_ggsw_res, rows_ggsw_res, 1, rank);
|
||||
|
||||
let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow());
|
||||
|
||||
let now: Instant = Instant::now();
|
||||
cbt_prepared.execute_to_constant(
|
||||
module,
|
||||
&mut res,
|
||||
&ct_lwe,
|
||||
k_lwe_pt,
|
||||
extension_factor,
|
||||
scratch.borrow(),
|
||||
);
|
||||
println!("CBT: {} ms", now.elapsed().as_millis());
|
||||
|
||||
// X^{data * 2^log_gap_out}
|
||||
let mut pt_ggsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
|
||||
pt_ggsw.at_mut(0, 0)[0] = data;
|
||||
|
||||
res.print_noise(module, &sk_glwe_prepared, &pt_ggsw);
|
||||
|
||||
let k_glwe: usize = k_ggsw_res;
|
||||
|
||||
let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_glwe, rank);
|
||||
let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, basek);
|
||||
pt_glwe.data.at_mut(0, 0)[0] = 1 << (basek - k_lwe_pt - 1);
|
||||
|
||||
ct_glwe.encrypt_sk(
|
||||
module,
|
||||
&pt_glwe,
|
||||
&sk_glwe_prepared,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
sigma,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
let res_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = res.prepare_alloc(module, scratch.borrow());
|
||||
|
||||
ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow());
|
||||
|
||||
let mut pt_res: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_glwe);
|
||||
ct_glwe.decrypt(module, &mut pt_res, &sk_glwe_prepared, scratch.borrow());
|
||||
|
||||
// Parameters are set such that the first limb should be noiseless.
|
||||
let mut pt_want: Vec<i64> = vec![0i64; module.n()];
|
||||
pt_want[0] = pt_glwe.data.at(0, 0)[0] * data;
|
||||
assert_eq!(pt_res.data.at(0, 0), pt_want);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
use poulpy_backend::{
|
||||
hal::{api::ModuleNew, layouts::Module},
|
||||
implementation::cpu_spqlios::FFT64,
|
||||
};
|
||||
|
||||
use crate::tfhe::{
|
||||
blind_rotation::CGGI,
|
||||
circuit_bootstrapping::tests::circuit_bootstrapping::{
|
||||
test_circuit_bootstrapping_to_constant, test_circuit_bootstrapping_to_exponent,
|
||||
},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_to_constant() {
|
||||
let module: Module<FFT64> = Module::<FFT64>::new(256);
|
||||
test_circuit_bootstrapping_to_constant::<FFT64, CGGI>(&module);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_exponent() {
|
||||
let module: Module<FFT64> = Module::<FFT64>::new(256);
|
||||
test_circuit_bootstrapping_to_exponent::<FFT64, CGGI>(&module);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
mod fft64;
|
||||
@@ -0,0 +1 @@
|
||||
mod cpu_spqlios;
|
||||
@@ -0,0 +1,4 @@
|
||||
pub mod circuit_bootstrapping;
|
||||
|
||||
#[cfg(test)]
|
||||
mod implementation;
|
||||
Reference in New Issue
Block a user