mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
fixed many test noise check + update noise retrieval (not passing)
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use poulpy_core::{
|
||||
GLWENormalize,
|
||||
layouts::{
|
||||
GGLWEToGGSWKeyLayout, GGSW, GGSWLayout, GLWE, GLWEAutomorphismKeyLayout, GLWELayout, GLWEPlaintext, GLWESecret, LWE,
|
||||
LWELayout, LWEPlaintext, LWESecret,
|
||||
GGLWEToGGSWKeyLayout, GGSW, GGSWInfos, GGSWLayout, GLWE, GLWEAutomorphismKeyLayout, GLWEInfos, GLWELayout, GLWEPlaintext,
|
||||
GLWESecret, LWE, LWELayout, LWEPlaintext, LWESecret,
|
||||
prepared::{GGSWPrepared, GLWESecretPrepared},
|
||||
},
|
||||
};
|
||||
@@ -212,7 +212,23 @@ fn main() {
|
||||
pt_ggsw.at_mut(0, 0)[0] = data;
|
||||
|
||||
// Prints noise of GGSW(data)
|
||||
res.print_noise(&module, &sk_glwe_prepared, &pt_ggsw);
|
||||
for row in 0..res.dnum().as_usize() {
|
||||
for col in 0..res.rank().as_usize() + 1 {
|
||||
println!(
|
||||
"row:{row} col:{col} -> {}",
|
||||
res.noise(
|
||||
&module,
|
||||
row,
|
||||
col,
|
||||
&pt_ggsw,
|
||||
&sk_glwe_prepared,
|
||||
scratch.borrow()
|
||||
)
|
||||
.std()
|
||||
.log2()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Tests RLWE(1) * GGSW(data)
|
||||
|
||||
|
||||
@@ -146,7 +146,7 @@ impl<D: DataRef, T: UnsignedInteger + FromBits> FheUint<D, T> {
|
||||
data_bits[T::bit_index(i) << log_gap] = want.bit(i) as i64
|
||||
}
|
||||
pt.encode_vec_i64(&data_bits, TorusPrecision(2));
|
||||
self.bits.noise(module, sk, &pt, scratch_1)
|
||||
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
|
||||
|
||||
@@ -13,8 +13,8 @@ use poulpy_core::{
|
||||
layouts::{GGSWInfos, GGSWPreparedFactory, GLWEInfos, LWEInfos},
|
||||
};
|
||||
|
||||
use poulpy_hal::api::ModuleN;
|
||||
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
|
||||
use poulpy_hal::api::{ModuleN, ScratchTakeBasic};
|
||||
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, Stats};
|
||||
|
||||
pub struct FheUintPreparedDebug<D: Data, T: UnsignedInteger> {
|
||||
pub(crate) bits: Vec<GGSW<D>>,
|
||||
@@ -81,31 +81,28 @@ impl<D: DataRef, T: UnsignedInteger> GGSWInfos for FheUintPreparedDebug<D, T> {
|
||||
}
|
||||
|
||||
impl<D: DataRef, T: UnsignedInteger + ToBits> FheUintPreparedDebug<D, T> {
|
||||
pub fn print_noise<S, M, BE: Backend>(&self, module: &M, sk: &S, want: T)
|
||||
pub fn noise<S, M, BE: Backend>(
|
||||
&self,
|
||||
module: &M,
|
||||
row: usize,
|
||||
col: usize,
|
||||
want: T,
|
||||
sk: &S,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) -> Vec<Stats>
|
||||
where
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
M: GGSWNoise<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
let mut stats = Vec::new();
|
||||
for (i, ggsw) in self.bits.iter().enumerate() {
|
||||
use poulpy_hal::layouts::{ScalarZnx, ZnxViewMut};
|
||||
let mut pt_want = ScalarZnx::alloc(self.n().into(), 1);
|
||||
use poulpy_hal::layouts::ZnxViewMut;
|
||||
let (mut pt_want, scratch_1) = scratch.take_scalar_znx(self.n().into(), 1);
|
||||
pt_want.at_mut(0, 0)[0] = want.bit(i) as i64;
|
||||
ggsw.print_noise(module, sk, &pt_want);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn assert_noise<S, M, F, BE: Backend>(&self, module: &M, sk: &S, want: T, max_noise: &F)
|
||||
where
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
M: GGSWNoise<BE>,
|
||||
F: Fn(usize) -> f64,
|
||||
{
|
||||
for (i, ggsw) in self.bits.iter().enumerate() {
|
||||
use poulpy_hal::layouts::{ScalarZnx, ZnxViewMut};
|
||||
let mut pt_want = ScalarZnx::alloc(self.n().into(), 1);
|
||||
pt_want.at_mut(0, 0)[0] = want.bit(i) as i64;
|
||||
ggsw.assert_noise(module, sk, &pt_want, max_noise);
|
||||
stats.push(ggsw.noise(module, row, col, &pt_want, sk, scratch_1));
|
||||
}
|
||||
stats
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use poulpy_core::{
|
||||
GGSWEncryptSk, GGSWNoise, GLWEDecrypt, GLWEEncryptSk, SIGMA, ScratchTakeCore,
|
||||
layouts::{
|
||||
Base2K, Dnum, Dsize, GGSW, GGSWLayout, GGSWPreparedFactory, GLWESecretPrepared, GLWESecretPreparedFactory, LWEInfos,
|
||||
Rank, TorusPrecision,
|
||||
Base2K, Dnum, Dsize, GGSW, GGSWInfos, GGSWLayout, GGSWPreparedFactory, GLWEInfos, GLWESecretPrepared,
|
||||
GLWESecretPreparedFactory, LWEInfos, Rank, TorusPrecision,
|
||||
},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
@@ -131,9 +131,23 @@ where
|
||||
|
||||
module.vec_znx_rotate_inplace(-rot, &mut scalar_want.as_vec_znx_mut(), 0, scratch.borrow());
|
||||
|
||||
// res.print_noise(&module, &sk_glwe_prep, &scalar_want);
|
||||
|
||||
res.assert_noise(module, sk_glwe_prep, &scalar_want, &max_noise);
|
||||
for row in 0..res.dnum().as_usize() {
|
||||
for col in 0..res.rank().as_usize() + 1 {
|
||||
assert!(
|
||||
res.noise(
|
||||
module,
|
||||
row,
|
||||
col,
|
||||
&scalar_want,
|
||||
sk_glwe_prep,
|
||||
scratch.borrow()
|
||||
)
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise(col)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
bit_step += digit;
|
||||
bit_start += digit;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use poulpy_core::{
|
||||
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWENoise, SIGMA, ScratchTakeCore,
|
||||
layouts::{GGSWLayout, GLWELayout, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
|
||||
layouts::{GGSWInfos, GGSWLayout, GLWEInfos, GLWELayout, GLWESecretPreparedFactory, LWEInfos, prepared::GLWESecretPrepared},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
layouts::{Backend, Module, Scratch, ScratchOwned},
|
||||
layouts::{Backend, Module, Scratch, ScratchOwned, Stats},
|
||||
source::Source,
|
||||
};
|
||||
use rand::RngCore;
|
||||
@@ -81,7 +81,12 @@ where
|
||||
noise
|
||||
};
|
||||
|
||||
// c_enc_prep_debug.print_noise(module, sk_glwe_prep, value);
|
||||
|
||||
c_enc_prep_debug.assert_noise(module, sk_glwe_prep, value, &max_noise);
|
||||
for row in 0..c_enc_prep_debug.dnum().as_usize() {
|
||||
for col in 0..c_enc_prep_debug.rank().as_usize() + 1 {
|
||||
let stats: Vec<Stats> = c_enc_prep_debug.noise(module, row, col, value, sk_glwe_prep, scratch.borrow());
|
||||
for stat in &stats {
|
||||
assert!(stat.std().log2() <= max_noise(col))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@ use std::collections::HashMap;
|
||||
|
||||
use poulpy_hal::{
|
||||
api::{ModuleLogN, ModuleN, ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
layouts::{Backend, DataRef, Module, Scratch, ScratchOwned, ToOwnedDeep},
|
||||
layouts::{Backend, DataRef, Module, Scratch, ScratchOwned},
|
||||
};
|
||||
|
||||
use poulpy_core::{
|
||||
GGSWFromGGLWE, GLWEDecrypt, GLWEPacking, GLWERotate, GLWETrace, ScratchTakeCore,
|
||||
GGSWExpandRows, GGSWFromGGLWE, GLWECopy, GLWEDecrypt, GLWENormalize, GLWEPacking, GLWERotate, GLWETrace, ScratchTakeCore,
|
||||
layouts::{
|
||||
Dsize, GGLWE, GGLWEInfos, GGLWELayout, GGLWEPreparedToRef, GGSWInfos, GGSWToMut, GLWEAutomorphismKeyHelper, GLWEInfos,
|
||||
GLWESecretPreparedFactory, GLWEToMut, GLWEToRef, GetGaloisElement, LWEInfos, LWEToRef, Rank,
|
||||
GLWELayout, GLWESecretPreparedFactory, GLWEToMut, GLWEToRef, GetGaloisElement, LWEInfos, LWEToRef, Rank,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -114,10 +114,12 @@ where
|
||||
+ BlindRotationExecute<BRA, BE>
|
||||
+ GLWETrace<BE>
|
||||
+ GLWEPacking<BE>
|
||||
+ GGSWFromGGLWE<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GLWEDecrypt<BE>
|
||||
+ GLWERotate<BE>,
|
||||
+ GLWERotate<BE>
|
||||
+ GLWENormalize<BE>
|
||||
+ GLWECopy
|
||||
+ GGSWExpandRows<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
@@ -240,7 +242,10 @@ pub fn circuit_bootstrap_core<R, L, D, M, BRA: BlindRotationAlgo, BE: Backend>(
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GLWEDecrypt<BE>
|
||||
+ GLWERotate<BE>
|
||||
+ ModuleLogN,
|
||||
+ ModuleLogN
|
||||
+ GLWENormalize<BE>
|
||||
+ GLWECopy
|
||||
+ GGSWExpandRows<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
@@ -248,27 +253,22 @@ pub fn circuit_bootstrap_core<R, L, D, M, BRA: BlindRotationAlgo, BE: Backend>(
|
||||
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());
|
||||
|
||||
let n: usize = res.n().into();
|
||||
let base2k: usize = res.base2k().into();
|
||||
let dnum: usize = res.dnum().into();
|
||||
let rank: usize = res.rank().into();
|
||||
let k: usize = res.k().into();
|
||||
let base2k_res: usize = res.base2k().as_usize();
|
||||
let dnum_res: usize = res.dnum().into();
|
||||
|
||||
let alpha: usize = dnum.next_power_of_two();
|
||||
let alpha: usize = dnum_res.next_power_of_two();
|
||||
|
||||
let mut f: Vec<i64> = vec![0i64; (1 << log_domain) * alpha];
|
||||
|
||||
if to_exponent {
|
||||
(0..dnum).for_each(|i| {
|
||||
f[i] = 1 << (base2k * (dnum - 1 - i));
|
||||
(0..dnum_res).for_each(|i| {
|
||||
f[i] = 1 << (base2k_res * (dnum_res - 1 - i));
|
||||
});
|
||||
} else {
|
||||
(0..1 << log_domain).for_each(|j| {
|
||||
(0..dnum).for_each(|i| {
|
||||
f[j * alpha + i] = j as i64 * (1 << (base2k * (dnum - 1 - i)));
|
||||
(0..dnum_res).for_each(|i| {
|
||||
f[j * alpha + i] = j as i64 * (1 << (base2k_res * (dnum_res - 1 - i)));
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -276,65 +276,79 @@ pub fn circuit_bootstrap_core<R, L, D, M, BRA: BlindRotationAlgo, BE: Backend>(
|
||||
let lut_infos: LookUpTableLayout = LookUpTableLayout {
|
||||
n: module.n().into(),
|
||||
extension_factor,
|
||||
k: (base2k * dnum).into(),
|
||||
base2k: base2k.into(),
|
||||
k: (base2k_res * dnum_res).into(),
|
||||
base2k: key.brk.base2k(),
|
||||
};
|
||||
|
||||
// Lut precision, basically must be able to hold the decomposition power basis of the GGSW
|
||||
let mut lut: LookupTable = LookupTable::alloc(&lut_infos);
|
||||
lut.set(module, &f, base2k * dnum);
|
||||
lut.set(module, &f, base2k_res * dnum_res);
|
||||
|
||||
if to_exponent {
|
||||
lut.set_rotation_direction(LookUpTableRotationDirection::Right);
|
||||
}
|
||||
|
||||
// TODO: separate GGSW k from output of blind rotation k
|
||||
let (mut res_glwe, scratch_1) = scratch.take_glwe(res);
|
||||
|
||||
let gglwe_infos: GGLWELayout = GGLWELayout {
|
||||
n: n.into(),
|
||||
base2k: base2k.into(),
|
||||
k: k.into(),
|
||||
dnum: dnum.into(),
|
||||
dsize: Dsize(1),
|
||||
rank_in: rank.max(1).into(),
|
||||
rank_out: rank.into(),
|
||||
let glwe_brk_layout = &GLWELayout {
|
||||
n: key.brk.n(),
|
||||
base2k: key.brk.base2k(),
|
||||
k: key.brk.k(),
|
||||
rank: key.brk.rank(),
|
||||
};
|
||||
|
||||
let (mut tmp_gglwe, scratch_2) = scratch_1.take_gglwe(&gglwe_infos);
|
||||
let atk_layout: &GGLWELayout = &key.atk.automorphism_key_infos();
|
||||
|
||||
key.brk.execute(module, &mut res_glwe, lwe, &lut, scratch_2);
|
||||
let glwe_atk_layout: &GLWELayout = &GLWELayout {
|
||||
n: glwe_brk_layout.n(),
|
||||
base2k: atk_layout.base2k(),
|
||||
k: glwe_brk_layout.k(),
|
||||
rank: glwe_brk_layout.rank(),
|
||||
};
|
||||
|
||||
let (mut res_glwe_atk_layout, scratch_1) = scratch.take_glwe(glwe_atk_layout);
|
||||
|
||||
// Execute blind rotation over BRK layout and returns result over ATK layout
|
||||
{
|
||||
let (mut res_glwe_brk_layout, scratch_2) = scratch_1.take_glwe(glwe_brk_layout);
|
||||
key.brk
|
||||
.execute(module, &mut res_glwe_brk_layout, lwe, &lut, scratch_2);
|
||||
|
||||
if res_glwe_brk_layout.base2k() == res_glwe_atk_layout.base2k() {
|
||||
module.glwe_copy(&mut res_glwe_atk_layout, &res_glwe_brk_layout);
|
||||
} else {
|
||||
module.glwe_normalize(&mut res_glwe_atk_layout, &res_glwe_brk_layout, scratch_2);
|
||||
}
|
||||
}
|
||||
|
||||
let gap: usize = 2 * lut.drift / lut.extension_factor();
|
||||
|
||||
let log_gap_in: usize = (usize::BITS - (gap * alpha - 1).leading_zeros()) as _;
|
||||
|
||||
(0..dnum).for_each(|i| {
|
||||
let mut tmp_glwe: GLWE<&mut [u8]> = tmp_gglwe.at_mut(i, 0);
|
||||
for i in 0..dnum_res {
|
||||
let mut res_row: GLWE<&mut [u8]> = res.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,
|
||||
&mut res_row,
|
||||
&res_glwe_atk_layout,
|
||||
log_gap_in,
|
||||
log_gap_out,
|
||||
log_domain,
|
||||
&key.atk,
|
||||
scratch_2,
|
||||
scratch_1,
|
||||
);
|
||||
} else {
|
||||
tmp_glwe.trace(module, 0, &res_glwe, &key.atk, scratch_2);
|
||||
module.glwe_trace(&mut res_row, 0, &res_glwe_atk_layout, &key.atk, scratch_1);
|
||||
}
|
||||
|
||||
if i < dnum {
|
||||
module.glwe_rotate_inplace(-(gap as i64), &mut res_glwe, scratch_2);
|
||||
if i < dnum_res {
|
||||
module.glwe_rotate_inplace(-(gap as i64), &mut res_glwe_atk_layout, scratch_1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Expands GGLWE to GGSW using GGLWE(s^2)
|
||||
res.from_gglwe(module, &tmp_gglwe, &key.tsk, scratch_2);
|
||||
module.ggsw_expand_row(res, &key.tsk, scratch);
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
@@ -348,47 +362,48 @@ fn post_process<R, A, M, H, K, BE: Backend>(
|
||||
auto_keys: &H,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
R: GLWEToMut,
|
||||
A: GLWEToRef,
|
||||
R: GLWEToMut + GLWEInfos,
|
||||
A: GLWEToRef + GLWEInfos,
|
||||
H: GLWEAutomorphismKeyHelper<K, BE>,
|
||||
K: GGLWEPreparedToRef<BE> + GetGaloisElement + GGLWEInfos,
|
||||
M: ModuleLogN + GLWETrace<BE> + GLWEPacking<BE> + GLWERotate<BE>,
|
||||
M: ModuleLogN + GLWETrace<BE> + GLWEPacking<BE> + GLWERotate<BE> + GLWECopy,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
|
||||
let a: &GLWE<&[u8]> = &a.to_ref();
|
||||
|
||||
let mut cts: HashMap<usize, &mut GLWE<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,
|
||||
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 (mut a_trace, scratch_1) = scratch.take_glwe(a);
|
||||
|
||||
// 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]
|
||||
module.glwe_trace(
|
||||
&mut a_trace,
|
||||
module.log_n() - log_gap_in + 1,
|
||||
a,
|
||||
auto_keys,
|
||||
scratch_1,
|
||||
);
|
||||
|
||||
let steps: usize = 1 << log_domain;
|
||||
|
||||
// TODO: from Scratch
|
||||
let mut cts_vec: Vec<GLWE<Vec<u8>>> = Vec::new();
|
||||
let (mut cts_vec, scratch_2) = scratch_1.take_glwe_slice(steps, a);
|
||||
|
||||
for i in 0..steps {
|
||||
if i != 0 {
|
||||
module.glwe_rotate_inplace(-(1 << log_gap_in), res, scratch);
|
||||
module.glwe_rotate_inplace(-(1 << log_gap_in), &mut a_trace, scratch_2);
|
||||
}
|
||||
cts_vec.push(res.to_owned_deep());
|
||||
|
||||
module.glwe_copy(&mut cts_vec[i], &a_trace);
|
||||
}
|
||||
|
||||
let mut cts: HashMap<usize, &mut GLWE<&mut [u8]>> = HashMap::new();
|
||||
for (i, ct) in cts_vec.iter_mut().enumerate().take(steps) {
|
||||
cts.insert(i * (1 << log_gap_out), ct);
|
||||
}
|
||||
|
||||
module.glwe_pack(res, cts, log_gap_out, auto_keys, scratch);
|
||||
module.glwe_pack(res, cts, log_gap_out, auto_keys, scratch_2);
|
||||
} else {
|
||||
module.glwe_trace(res, module.log_n() - log_gap_in + 1, a, auto_keys, scratch);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,8 @@ use crate::tfhe::{
|
||||
use poulpy_core::{
|
||||
GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWEExternalProduct, LWEEncryptSk, ScratchTakeCore,
|
||||
layouts::{
|
||||
Dsize, GGLWEToGGSWKeyLayout, GGSWLayout, GGSWPreparedFactory, GLWEAutomorphismKeyLayout, GLWESecretPreparedFactory,
|
||||
LWELayout,
|
||||
Dsize, GGLWEToGGSWKeyLayout, GGSWInfos, GGSWLayout, GGSWPreparedFactory, GLWEAutomorphismKeyLayout, GLWEInfos,
|
||||
GLWESecretPreparedFactory, LWELayout,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -186,8 +186,23 @@ where
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
res.print_noise(module, &sk_glwe_prepared, &pt_ggsw);
|
||||
|
||||
for row in 0..res.dnum().as_usize() {
|
||||
for col in 0..res.rank().as_usize() + 1 {
|
||||
println!(
|
||||
"row:{row} col:{col} -> {}",
|
||||
res.noise(
|
||||
module,
|
||||
row,
|
||||
col,
|
||||
&pt_ggsw,
|
||||
&sk_glwe_prepared,
|
||||
scratch.borrow()
|
||||
)
|
||||
.std()
|
||||
.log2()
|
||||
)
|
||||
}
|
||||
}
|
||||
let mut ct_glwe: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&ggsw_infos);
|
||||
let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&ggsw_infos);
|
||||
pt_glwe.data.at_mut(0, 0)[0] = 1 << (base2k - 2);
|
||||
@@ -234,7 +249,10 @@ where
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
let n_glwe: usize = module.n();
|
||||
let base2k: usize = 14;
|
||||
let base2k_res: usize = 14;
|
||||
let base2k_brk: usize = 13;
|
||||
let base2k_tsk: usize = 12;
|
||||
let base2k_atk: usize = 11;
|
||||
let extension_factor: usize = 1;
|
||||
let rank: usize = 1;
|
||||
|
||||
@@ -243,36 +261,36 @@ where
|
||||
let k_lwe_ct: usize = 13;
|
||||
let block_size: usize = 7;
|
||||
|
||||
let k_brk: usize = 5 * base2k;
|
||||
let k_ggsw_res: usize = 4 * base2k_res;
|
||||
let rows_ggsw_res: usize = 3;
|
||||
|
||||
let k_brk: usize = k_ggsw_res + base2k_brk;
|
||||
let rows_brk: usize = 3;
|
||||
|
||||
let k_atk: usize = 5 * base2k;
|
||||
let k_atk: usize = k_ggsw_res + base2k_tsk;
|
||||
let rows_atk: usize = 4;
|
||||
|
||||
let k_tsk: usize = 5 * base2k;
|
||||
let k_tsk: usize = k_ggsw_res + base2k_atk;
|
||||
let rows_tsk: usize = 4;
|
||||
|
||||
let k_ggsw_res: usize = 4 * base2k;
|
||||
let rows_ggsw_res: usize = 3;
|
||||
|
||||
let lwe_infos: LWELayout = LWELayout {
|
||||
n: n_lwe.into(),
|
||||
k: k_lwe_ct.into(),
|
||||
base2k: base2k.into(),
|
||||
base2k: base2k_brk.into(),
|
||||
};
|
||||
|
||||
let cbt_infos: CircuitBootstrappingKeyLayout = CircuitBootstrappingKeyLayout {
|
||||
layout_brk: BlindRotationKeyLayout {
|
||||
n_glwe: n_glwe.into(),
|
||||
n_lwe: n_lwe.into(),
|
||||
base2k: base2k.into(),
|
||||
base2k: base2k_brk.into(),
|
||||
k: k_brk.into(),
|
||||
dnum: rows_brk.into(),
|
||||
rank: rank.into(),
|
||||
},
|
||||
layout_atk: GLWEAutomorphismKeyLayout {
|
||||
n: n_glwe.into(),
|
||||
base2k: base2k.into(),
|
||||
base2k: base2k_atk.into(),
|
||||
k: k_atk.into(),
|
||||
dnum: rows_atk.into(),
|
||||
rank: rank.into(),
|
||||
@@ -280,7 +298,7 @@ where
|
||||
},
|
||||
layout_tsk: GGLWEToGGSWKeyLayout {
|
||||
n: n_glwe.into(),
|
||||
base2k: base2k.into(),
|
||||
base2k: base2k_tsk.into(),
|
||||
k: k_tsk.into(),
|
||||
dnum: rows_tsk.into(),
|
||||
dsize: Dsize(1),
|
||||
@@ -290,7 +308,7 @@ where
|
||||
|
||||
let ggsw_infos: GGSWLayout = GGSWLayout {
|
||||
n: n_glwe.into(),
|
||||
base2k: base2k.into(),
|
||||
base2k: base2k_res.into(),
|
||||
k: k_ggsw_res.into(),
|
||||
dnum: rows_ggsw_res.into(),
|
||||
dsize: Dsize(1),
|
||||
@@ -314,7 +332,7 @@ where
|
||||
|
||||
let data: i64 = 1;
|
||||
|
||||
let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(base2k.into(), k_lwe_pt.into());
|
||||
let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(base2k_res.into(), k_lwe_pt.into());
|
||||
pt_lwe.encode_i64(data, (k_lwe_pt + 1).into());
|
||||
|
||||
println!("pt_lwe: {pt_lwe}");
|
||||
@@ -365,11 +383,27 @@ where
|
||||
let mut pt_ggsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n_glwe, 1);
|
||||
pt_ggsw.at_mut(0, 0)[0] = data;
|
||||
|
||||
res.print_noise(module, &sk_glwe_prepared, &pt_ggsw);
|
||||
for row in 0..res.dnum().as_usize() {
|
||||
for col in 0..res.rank().as_usize() + 1 {
|
||||
println!(
|
||||
"row:{row} col:{col} -> {}",
|
||||
res.noise(
|
||||
module,
|
||||
row,
|
||||
col,
|
||||
&pt_ggsw,
|
||||
&sk_glwe_prepared,
|
||||
scratch.borrow()
|
||||
)
|
||||
.std()
|
||||
.log2()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
let mut ct_glwe: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&ggsw_infos);
|
||||
let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&ggsw_infos);
|
||||
pt_glwe.data.at_mut(0, 0)[0] = 1 << (base2k - k_lwe_pt - 1);
|
||||
pt_glwe.data.at_mut(0, 0)[0] = 1 << (base2k_res - k_lwe_pt - 1);
|
||||
|
||||
ct_glwe.encrypt_sk(
|
||||
module,
|
||||
@@ -391,5 +425,6 @@ where
|
||||
// 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;
|
||||
println!("pt_res: {pt_res}");
|
||||
assert_eq!(pt_res.data.at(0, 0), pt_want);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user