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:
@@ -132,17 +132,21 @@ impl GLWEPacker {
|
||||
}
|
||||
|
||||
/// Flush result to`res`.
|
||||
pub fn flush<R, M, BE: Backend>(&mut self, module: &M, res: &mut R)
|
||||
pub fn flush<R, M, BE: Backend>(&mut self, module: &M, res: &mut R, scratch: &mut Scratch<BE>)
|
||||
where
|
||||
R: GLWEToMut,
|
||||
R: GLWEToMut + GLWEInfos,
|
||||
M: GLWEPackerOps<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
assert!(self.counter as u32 == self.accumulators[0].data.n());
|
||||
// Copy result GLWE into res GLWE
|
||||
module.glwe_copy(
|
||||
res,
|
||||
&self.accumulators[module.log_n() - self.log_batch - 1].data,
|
||||
);
|
||||
|
||||
let out: &GLWE<Vec<u8>> = &self.accumulators[module.log_n() - self.log_batch - 1].data;
|
||||
|
||||
if out.base2k() == res.base2k() {
|
||||
module.glwe_copy(res, out)
|
||||
} else {
|
||||
module.glwe_normalize(res, out, scratch);
|
||||
}
|
||||
|
||||
self.reset();
|
||||
}
|
||||
@@ -244,7 +248,11 @@ fn pack_core<A, K, H, M, BE: Backend>(
|
||||
|
||||
// No previous value -> copies and sets flags accordingly
|
||||
if let Some(a_ref) = a {
|
||||
module.glwe_copy(&mut acc_mut_ref.data, a_ref);
|
||||
if a_ref.base2k() == acc_mut_ref.data.base2k() {
|
||||
module.glwe_copy(&mut acc_mut_ref.data, a_ref);
|
||||
} else {
|
||||
module.glwe_normalize(&mut acc_mut_ref.data, a_ref, scratch);
|
||||
}
|
||||
acc_mut_ref.value = true
|
||||
} else {
|
||||
acc_mut_ref.value = false
|
||||
@@ -331,30 +339,29 @@ fn combine<B, K, H, M, BE: Backend>(
|
||||
// since 2*(I(X) * Q/2) = I(X) * Q = 0 mod Q.
|
||||
if acc.value {
|
||||
if let Some(b) = b {
|
||||
let (mut tmp_b, scratch_1) = scratch.take_glwe(a);
|
||||
let (mut tmp, scratch_1) = scratch.take_glwe(a);
|
||||
|
||||
// a = a * X^-t
|
||||
module.glwe_rotate_inplace(-t, a, scratch_1);
|
||||
|
||||
// tmp_b = a * X^-t - b
|
||||
module.glwe_sub(&mut tmp_b, a, b);
|
||||
module.glwe_rsh(1, &mut tmp_b, scratch_1);
|
||||
|
||||
module.glwe_sub(&mut tmp, a, b);
|
||||
module.glwe_rsh(1, &mut tmp, scratch_1);
|
||||
// a = a * X^-t + b
|
||||
module.glwe_add_inplace(a, b);
|
||||
module.glwe_rsh(1, a, scratch_1);
|
||||
|
||||
module.glwe_normalize_inplace(&mut tmp_b, scratch_1);
|
||||
module.glwe_rsh(1, a, scratch_1);
|
||||
module.glwe_normalize_inplace(&mut tmp, scratch_1);
|
||||
|
||||
// tmp_b = phi(a * X^-t - b)
|
||||
if let Some(auto_key) = auto_keys.get_automorphism_key(gal_el) {
|
||||
module.glwe_automorphism_inplace(&mut tmp_b, auto_key, scratch_1);
|
||||
module.glwe_automorphism_inplace(&mut tmp, auto_key, scratch_1);
|
||||
} else {
|
||||
panic!("auto_key[{gal_el}] not found");
|
||||
}
|
||||
|
||||
// a = a * X^-t + b - phi(a * X^-t - b)
|
||||
module.glwe_sub_inplace(a, &tmp_b);
|
||||
module.glwe_sub_inplace(a, &tmp);
|
||||
module.glwe_normalize_inplace(a, scratch_1);
|
||||
|
||||
// a = a + b * X^t - phi(a * X^-t - b) * X^t
|
||||
|
||||
@@ -7,9 +7,16 @@ use poulpy_hal::{
|
||||
|
||||
use crate::{
|
||||
GLWEAdd, GLWEAutomorphism, GLWECopy, GLWENormalize, GLWERotate, GLWEShift, GLWESub, GLWETrace, ScratchTakeCore,
|
||||
layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWEAutomorphismKeyHelper, GLWEInfos, GLWEToMut, GetGaloisElement},
|
||||
layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEAutomorphismKeyHelper, GLWEInfos, GLWEToMut, GetGaloisElement},
|
||||
};
|
||||
pub trait GLWEPacking<BE: Backend> {
|
||||
fn glwe_pack_galois_elements(&self) -> Vec<i64>;
|
||||
|
||||
fn glwe_pack_tmp_bytes<R, K>(&self, res: &R, key: &K) -> usize
|
||||
where
|
||||
R: GLWEInfos,
|
||||
K: GGLWEInfos;
|
||||
|
||||
/// Packs [x_0: GLWE(m_0), x_1: GLWE(m_1), ..., x_i: GLWE(m_i)]
|
||||
/// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)]
|
||||
fn glwe_pack<R, A, K, H>(
|
||||
@@ -40,6 +47,22 @@ where
|
||||
+ GLWETrace<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
fn glwe_pack_galois_elements(&self) -> Vec<i64> {
|
||||
self.glwe_trace_galois_elements()
|
||||
}
|
||||
|
||||
fn glwe_pack_tmp_bytes<R, K>(&self, res: &R, key: &K) -> usize
|
||||
where
|
||||
R: GLWEInfos,
|
||||
K: GGLWEInfos,
|
||||
{
|
||||
self.glwe_rotate_tmp_bytes()
|
||||
.max(self.glwe_rsh_tmp_byte())
|
||||
.max(self.glwe_normalize_tmp_bytes())
|
||||
.max(self.glwe_automorphism_tmp_bytes(res, res, key))
|
||||
+ GLWE::bytes_of_from_infos(res)
|
||||
}
|
||||
|
||||
/// Packs [x_0: GLWE(m_0), x_1: GLWE(m_1), ..., x_i: GLWE(m_i)]
|
||||
/// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)]
|
||||
fn glwe_pack<R, A, K, H>(
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use poulpy_hal::{
|
||||
api::{ModuleLogN, VecZnxNormalize, VecZnxNormalizeTmpBytes},
|
||||
api::{ModuleLogN, VecZnxNormalizeTmpBytes},
|
||||
layouts::{Backend, CyclotomicOrder, DataMut, GaloisElement, Module, Scratch, VecZnx, galois_element},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
GLWEAutomorphism, GLWECopy, GLWEShift, ScratchTakeCore,
|
||||
GLWEAutomorphism, GLWECopy, GLWENormalize, GLWEShift, ScratchTakeCore,
|
||||
layouts::{
|
||||
GGLWEInfos, GGLWELayout, GGLWEPreparedToRef, GLWE, GLWEAutomorphismKeyHelper, GLWEInfos, GLWELayout, GLWEToMut,
|
||||
GLWEToRef, GetGaloisElement, LWEInfos,
|
||||
@@ -75,7 +75,7 @@ where
|
||||
+ GLWECopy
|
||||
+ CyclotomicOrder
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxNormalize<BE>,
|
||||
+ GLWENormalize<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
fn glwe_trace_galois_elements(&self) -> Vec<i64> {
|
||||
@@ -114,15 +114,28 @@ where
|
||||
K: GGLWEPreparedToRef<BE> + GetGaloisElement + GGLWEInfos,
|
||||
H: GLWEAutomorphismKeyHelper<K, BE>,
|
||||
{
|
||||
let (mut tmp, scratch_1) = if a.k() > res.k() {
|
||||
scratch.take_glwe(a)
|
||||
} else {
|
||||
scratch.take_glwe(res)
|
||||
};
|
||||
let atk_layout: &GGLWELayout = &keys.automorphism_key_infos();
|
||||
|
||||
let (mut tmp, scratch_1) = scratch.take_glwe(&GLWELayout {
|
||||
n: res.n(),
|
||||
base2k: atk_layout.base2k(),
|
||||
k: a.k().max(res.k()),
|
||||
rank: res.rank(),
|
||||
});
|
||||
|
||||
if a.base2k() == atk_layout.base2k() {
|
||||
self.glwe_copy(&mut tmp, a);
|
||||
} else {
|
||||
self.glwe_normalize(&mut tmp, a, scratch_1);
|
||||
}
|
||||
|
||||
self.glwe_copy(&mut tmp, a);
|
||||
self.glwe_trace_inplace(&mut tmp, skip, keys, scratch_1);
|
||||
self.glwe_copy(res, &tmp);
|
||||
|
||||
if res.base2k() == atk_layout.base2k() {
|
||||
self.glwe_copy(res, &tmp);
|
||||
} else {
|
||||
self.glwe_normalize(res, &tmp, scratch_1);
|
||||
}
|
||||
}
|
||||
|
||||
fn glwe_trace_inplace<R, K, H>(&self, res: &mut R, skip: usize, keys: &H, scratch: &mut Scratch<BE>)
|
||||
@@ -143,52 +156,15 @@ where
|
||||
assert_eq!(ksk_infos.rank_out(), res.rank());
|
||||
|
||||
if res.base2k() != ksk_infos.base2k() {
|
||||
let (mut self_conv, scratch_1) = scratch.take_glwe(&GLWELayout {
|
||||
let (mut res_conv, scratch_1) = scratch.take_glwe(&GLWELayout {
|
||||
n: self.n().into(),
|
||||
base2k: ksk_infos.base2k(),
|
||||
k: res.k(),
|
||||
rank: res.rank(),
|
||||
});
|
||||
|
||||
for j in 0..(res.rank() + 1).into() {
|
||||
self.vec_znx_normalize(
|
||||
ksk_infos.base2k().into(),
|
||||
&mut self_conv.data,
|
||||
j,
|
||||
res.base2k().into(),
|
||||
res.data(),
|
||||
j,
|
||||
scratch_1,
|
||||
);
|
||||
}
|
||||
|
||||
for i in skip..log_n {
|
||||
self.glwe_rsh(1, &mut self_conv, scratch_1);
|
||||
|
||||
let p: i64 = if i == 0 {
|
||||
-1
|
||||
} else {
|
||||
self.galois_element(1 << (i - 1))
|
||||
};
|
||||
|
||||
if let Some(key) = keys.get_automorphism_key(p) {
|
||||
self.glwe_automorphism_add_inplace(&mut self_conv, key, scratch_1);
|
||||
} else {
|
||||
panic!("keys[{p}] is empty")
|
||||
}
|
||||
}
|
||||
|
||||
for j in 0..(res.rank() + 1).into() {
|
||||
self.vec_znx_normalize(
|
||||
res.base2k().into(),
|
||||
res.data_mut(),
|
||||
j,
|
||||
ksk_infos.base2k().into(),
|
||||
&self_conv.data,
|
||||
j,
|
||||
scratch_1,
|
||||
);
|
||||
}
|
||||
self.glwe_normalize(&mut res_conv, res, scratch_1);
|
||||
self.glwe_trace_inplace(&mut res_conv, skip, keys, scratch_1);
|
||||
self.glwe_normalize(res, &res_conv, scratch_1);
|
||||
} else {
|
||||
for i in skip..log_n {
|
||||
self.glwe_rsh(1, res, scratch);
|
||||
|
||||
@@ -1,74 +1,91 @@
|
||||
use poulpy_hal::{
|
||||
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxFillUniform, VecZnxSubScalarInplace},
|
||||
layouts::{Backend, DataRef, Module, ScalarZnxToRef, Scratch, ScratchOwned, ZnxZero},
|
||||
api::VecZnxAddScalarInplace,
|
||||
layouts::{Backend, DataRef, Module, ScalarZnxToRef, Scratch, Stats, ZnxInfos, ZnxZero},
|
||||
};
|
||||
|
||||
use crate::decryption::GLWEDecrypt;
|
||||
use crate::layouts::{GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos, prepared::GLWESecretPreparedToRef};
|
||||
use crate::{
|
||||
GLWENoise,
|
||||
layouts::{GGLWE, GGLWEInfos, GGLWEToRef, prepared::GLWESecretPreparedToRef},
|
||||
};
|
||||
use crate::{ScratchTakeCore, layouts::GLWEPlaintext};
|
||||
|
||||
impl<D: DataRef> GGLWE<D> {
|
||||
pub fn assert_noise<M, S, P, BE: Backend>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: f64)
|
||||
pub fn noise<M, S, P, BE: Backend>(
|
||||
&self,
|
||||
module: &M,
|
||||
row: usize,
|
||||
col: usize,
|
||||
pt_want: &P,
|
||||
sk_prepared: &S,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) -> Stats
|
||||
where
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: ScalarZnxToRef,
|
||||
M: GGLWENoise<BE>,
|
||||
Scratch<BE>: ScratchTakeBasic,
|
||||
{
|
||||
module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise);
|
||||
module.gglwe_noise(self, row, col, pt_want, sk_prepared, scratch)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GGLWENoise<BE: Backend> {
|
||||
fn gglwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
|
||||
fn gglwe_noise_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
A: GGLWEInfos;
|
||||
|
||||
fn gglwe_noise<R, S, P>(
|
||||
&self,
|
||||
res: &R,
|
||||
res_row: usize,
|
||||
res_col: usize,
|
||||
pt_want: &P,
|
||||
sk_prepared: &S,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) -> Stats
|
||||
where
|
||||
R: GGLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: ScalarZnxToRef,
|
||||
Scratch<BE>: ScratchTakeBasic;
|
||||
P: ScalarZnxToRef;
|
||||
}
|
||||
|
||||
impl<BE: Backend> GGLWENoise<BE> for Module<BE>
|
||||
where
|
||||
Module<BE>: GLWEDecrypt<BE> + VecZnxFillUniform + VecZnxSubScalarInplace,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeBasic,
|
||||
Module<BE>: VecZnxAddScalarInplace + GLWENoise<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
fn gglwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
|
||||
fn gglwe_noise_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
GLWEPlaintext::bytes_of_from_infos(infos) + self.glwe_noise_tmp_bytes(infos)
|
||||
}
|
||||
|
||||
fn gglwe_noise<R, S, P>(
|
||||
&self,
|
||||
res: &R,
|
||||
res_row: usize,
|
||||
res_col: usize,
|
||||
pt_want: &P,
|
||||
sk_prepared: &S,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) -> Stats
|
||||
where
|
||||
R: GGLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: ScalarZnxToRef,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeBasic,
|
||||
{
|
||||
let res: &GGLWE<&[u8]> = &res.to_ref();
|
||||
|
||||
let dsize: usize = res.dsize().into();
|
||||
let base2k: usize = res.base2k().into();
|
||||
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res));
|
||||
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res);
|
||||
|
||||
(0..res.rank_in().into()).for_each(|col_i| {
|
||||
(0..res.dnum().into()).for_each(|row_i| {
|
||||
self.glwe_decrypt(
|
||||
&res.at(row_i, col_i),
|
||||
&mut pt,
|
||||
sk_prepared,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
self.vec_znx_sub_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, col_i);
|
||||
|
||||
let noise_have: f64 = pt.data.stats(base2k, 0).std().log2();
|
||||
|
||||
assert!(
|
||||
noise_have <= max_noise,
|
||||
"noise_have: {noise_have} > max_noise: {max_noise}"
|
||||
);
|
||||
|
||||
pt.data.zero();
|
||||
});
|
||||
});
|
||||
let (mut pt, scratch_1) = scratch.take_glwe_plaintext(res);
|
||||
pt.data_mut().zero();
|
||||
println!("col: {res_col} {}", pt_want.to_ref().cols());
|
||||
self.vec_znx_add_scalar_inplace(
|
||||
&mut pt.data,
|
||||
0,
|
||||
(dsize - 1) + res_row * dsize,
|
||||
pt_want,
|
||||
res_col,
|
||||
);
|
||||
self.glwe_noise(&res.at(res_row, res_col), &pt, sk_prepared, scratch_1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +1,49 @@
|
||||
use poulpy_hal::{
|
||||
api::{
|
||||
ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, VecZnxBigAlloc,
|
||||
VecZnxBigNormalize, VecZnxDftAlloc, VecZnxDftApply, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, VecZnxSubInplace,
|
||||
ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes,
|
||||
VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume,
|
||||
},
|
||||
layouts::{Backend, DataRef, Module, ScalarZnxToRef, Scratch, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero},
|
||||
layouts::{Backend, DataRef, Module, ScalarZnxToRef, Scratch, Stats, ZnxZero},
|
||||
};
|
||||
|
||||
use crate::decryption::GLWEDecrypt;
|
||||
use crate::layouts::prepared::GLWESecretPreparedToRef;
|
||||
use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared};
|
||||
use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, LWEInfos, prepared::GLWESecretPrepared};
|
||||
use crate::{GLWENoise, layouts::prepared::GLWESecretPreparedToRef};
|
||||
use crate::{ScratchTakeCore, layouts::GLWEPlaintext};
|
||||
|
||||
impl<D: DataRef> GGSW<D> {
|
||||
pub fn assert_noise<M, BE: Backend, P, S, F>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: &F)
|
||||
pub fn noise<M, BE: Backend, P, S>(
|
||||
&self,
|
||||
module: &M,
|
||||
row: usize,
|
||||
col: usize,
|
||||
pt_want: &P,
|
||||
sk_prepared: &S,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) -> Stats
|
||||
where
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: ScalarZnxToRef,
|
||||
M: GGSWNoise<BE>,
|
||||
F: Fn(usize) -> f64,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
module.ggsw_assert_noise(self, sk_prepared, pt_want, max_noise);
|
||||
}
|
||||
|
||||
pub fn print_noise<M, BE: Backend, P, S>(&self, module: &M, sk_prepared: &S, pt_want: &P)
|
||||
where
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: ScalarZnxToRef,
|
||||
M: GGSWNoise<BE>,
|
||||
{
|
||||
module.ggsw_print_noise(self, sk_prepared, pt_want);
|
||||
module.ggsw_noise(self, row, col, pt_want, sk_prepared, scratch)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GGSWNoise<BE: Backend> {
|
||||
fn ggsw_assert_noise<R, S, P, F>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: &F)
|
||||
fn ggsw_noise_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
R: GGSWToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: ScalarZnxToRef,
|
||||
F: Fn(usize) -> f64;
|
||||
A: GGSWInfos;
|
||||
|
||||
fn ggsw_print_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P)
|
||||
fn ggsw_noise<R, S, P>(
|
||||
&self,
|
||||
res: &R,
|
||||
res_row: usize,
|
||||
res_col: usize,
|
||||
pt_want: &P,
|
||||
sk_prepared: &S,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) -> Stats
|
||||
where
|
||||
R: GGSWToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
@@ -48,79 +52,39 @@ pub trait GGSWNoise<BE: Backend> {
|
||||
|
||||
impl<BE: Backend> GGSWNoise<BE> for Module<BE>
|
||||
where
|
||||
Module<BE>: GLWEDecrypt<BE>
|
||||
+ VecZnxDftAlloc<BE>
|
||||
+ VecZnxBigAlloc<BE>
|
||||
+ VecZnxAddScalarInplace
|
||||
+ VecZnxIdftApplyTmpA<BE>
|
||||
+ VecZnxSubInplace,
|
||||
Scratch<BE>: ScratchTakeBasic,
|
||||
ScratchOwned<BE>: ScratchOwnedBorrow<BE> + ScratchOwnedAlloc<BE>,
|
||||
Module<BE>: VecZnxAddScalarInplace
|
||||
+ VecZnxDftApply<BE>
|
||||
+ SvpApplyDftToDftInplace<BE>
|
||||
+ VecZnxIdftApplyConsume<BE>
|
||||
+ VecZnxDftBytesOf
|
||||
+ VecZnxBigNormalize<BE>
|
||||
+ VecZnxBigNormalizeTmpBytes
|
||||
+ GLWENoise<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
fn ggsw_assert_noise<R, S, P, F>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: &F)
|
||||
fn ggsw_noise_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
R: GGSWToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: ScalarZnxToRef,
|
||||
F: Fn(usize) -> f64,
|
||||
A: GGSWInfos,
|
||||
{
|
||||
let res: &GGSW<&[u8]> = &res.to_ref();
|
||||
let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref();
|
||||
|
||||
let base2k: usize = res.base2k().into();
|
||||
let dsize: usize = res.dsize().into();
|
||||
|
||||
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res);
|
||||
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res);
|
||||
let mut pt_dft: VecZnxDft<Vec<u8>, BE> = self.vec_znx_dft_alloc(1, res.size());
|
||||
let mut pt_big: VecZnxBig<Vec<u8>, BE> = self.vec_znx_big_alloc(1, res.size());
|
||||
|
||||
let mut scratch: ScratchOwned<BE> =
|
||||
ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res) | self.vec_znx_normalize_tmp_bytes());
|
||||
|
||||
(0..(res.rank() + 1).into()).for_each(|col_j| {
|
||||
(0..res.dnum().into()).for_each(|row_i| {
|
||||
self.vec_znx_add_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, 0);
|
||||
|
||||
// mul with sk[col_j-1]
|
||||
if col_j > 0 {
|
||||
self.vec_znx_dft_apply(1, 0, &mut pt_dft, 0, &pt.data, 0);
|
||||
self.svp_apply_dft_to_dft_inplace(&mut pt_dft, 0, &sk_prepared.data, col_j - 1);
|
||||
self.vec_znx_idft_apply_tmpa(&mut pt_big, 0, &mut pt_dft, 0);
|
||||
self.vec_znx_big_normalize(
|
||||
base2k,
|
||||
&mut pt.data,
|
||||
0,
|
||||
base2k,
|
||||
&pt_big,
|
||||
0,
|
||||
scratch.borrow(),
|
||||
);
|
||||
}
|
||||
|
||||
self.glwe_decrypt(
|
||||
&res.at(row_i, col_j),
|
||||
&mut pt_have,
|
||||
sk_prepared,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0);
|
||||
|
||||
let std_pt: f64 = pt_have.data.stats(base2k, 0).std().log2();
|
||||
let noise: f64 = max_noise(col_j);
|
||||
assert!(std_pt <= noise, "{std_pt} > {noise}");
|
||||
|
||||
pt.data.zero();
|
||||
});
|
||||
});
|
||||
GLWEPlaintext::bytes_of_from_infos(infos)
|
||||
+ (self.bytes_of_vec_znx_dft(1, infos.size()) + self.vec_znx_big_normalize_tmp_bytes())
|
||||
.max(self.glwe_noise_tmp_bytes(infos))
|
||||
}
|
||||
|
||||
fn ggsw_print_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P)
|
||||
fn ggsw_noise<R, S, P>(
|
||||
&self,
|
||||
res: &R,
|
||||
res_row: usize,
|
||||
res_col: usize,
|
||||
pt_want: &P,
|
||||
sk_prepared: &S,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) -> Stats
|
||||
where
|
||||
R: GGSWToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: ScalarZnxToRef,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
let res: &GGSW<&[u8]> = &res.to_ref();
|
||||
let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref();
|
||||
@@ -128,47 +92,19 @@ where
|
||||
let base2k: usize = res.base2k().into();
|
||||
let dsize: usize = res.dsize().into();
|
||||
|
||||
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res);
|
||||
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res);
|
||||
let mut pt_dft: VecZnxDft<Vec<u8>, BE> = self.vec_znx_dft_alloc(1, res.size());
|
||||
let mut pt_big: VecZnxBig<Vec<u8>, BE> = self.vec_znx_big_alloc(1, res.size());
|
||||
let (mut pt, scratch_1) = scratch.take_glwe_plaintext(res);
|
||||
pt.data_mut().zero();
|
||||
self.vec_znx_add_scalar_inplace(&mut pt.data, 0, (dsize - 1) + res_row * dsize, pt_want, 0);
|
||||
|
||||
let mut scratch: ScratchOwned<BE> =
|
||||
ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res) | self.vec_znx_normalize_tmp_bytes());
|
||||
|
||||
for col_j in 0..(res.rank() + 1).into() {
|
||||
for row_i in 0..res.dnum().into() {
|
||||
self.vec_znx_add_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, 0);
|
||||
|
||||
// mul with sk[col_j-1]
|
||||
if col_j > 0 {
|
||||
self.vec_znx_dft_apply(1, 0, &mut pt_dft, 0, &pt.data, 0);
|
||||
self.svp_apply_dft_to_dft_inplace(&mut pt_dft, 0, &sk_prepared.data, col_j - 1);
|
||||
self.vec_znx_idft_apply_tmpa(&mut pt_big, 0, &mut pt_dft, 0);
|
||||
self.vec_znx_big_normalize(
|
||||
base2k,
|
||||
&mut pt.data,
|
||||
0,
|
||||
base2k,
|
||||
&pt_big,
|
||||
0,
|
||||
scratch.borrow(),
|
||||
);
|
||||
}
|
||||
|
||||
self.glwe_decrypt(
|
||||
&res.at(row_i, col_j),
|
||||
&mut pt_have,
|
||||
sk_prepared,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0);
|
||||
|
||||
let std_pt: f64 = pt_have.data.stats(base2k, 0).std().log2();
|
||||
println!("col: {col_j} row: {row_i}: {std_pt}");
|
||||
pt.data.zero();
|
||||
}
|
||||
// mul with sk[col_j-1]
|
||||
if res_col > 0 {
|
||||
let (mut pt_dft, scratch_2) = scratch_1.take_vec_znx_dft(self, 1, res.size());
|
||||
self.vec_znx_dft_apply(1, 0, &mut pt_dft, 0, &pt.data, 0);
|
||||
self.svp_apply_dft_to_dft_inplace(&mut pt_dft, 0, &sk_prepared.data, res_col - 1);
|
||||
let pt_big = self.vec_znx_idft_apply_consume(pt_dft);
|
||||
self.vec_znx_big_normalize(base2k, &mut pt.data, 0, base2k, &pt_big, 0, scratch_2);
|
||||
}
|
||||
|
||||
self.glwe_noise(&res.at(res_row, res_col), &pt, sk_prepared, scratch_1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,83 +1,61 @@
|
||||
use poulpy_hal::{
|
||||
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxNormalizeInplace, VecZnxSubInplace},
|
||||
layouts::{Backend, DataRef, Module, Scratch, ScratchOwned, Stats},
|
||||
};
|
||||
use poulpy_hal::layouts::{Backend, DataRef, Module, Scratch, Stats};
|
||||
|
||||
use crate::{
|
||||
ScratchTakeCore,
|
||||
GLWENormalize, GLWESub, ScratchTakeCore,
|
||||
decryption::GLWEDecrypt,
|
||||
layouts::{GLWE, GLWEPlaintext, GLWEPlaintextToRef, GLWEToRef, LWEInfos, prepared::GLWESecretPreparedToRef},
|
||||
layouts::{GLWE, GLWEInfos, GLWEPlaintext, GLWEToRef, LWEInfos, prepared::GLWESecretPreparedToRef},
|
||||
};
|
||||
|
||||
impl<D: DataRef> GLWE<D> {
|
||||
pub fn noise<M, S, P, BE: Backend>(&self, module: &M, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> Stats
|
||||
pub fn noise<M, P, S, BE: Backend>(&self, module: &M, pt_want: &P, sk_prepared: &S, scratch: &mut Scratch<BE>) -> Stats
|
||||
where
|
||||
M: GLWENoise<BE>,
|
||||
P: GLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: GLWEPlaintextToRef,
|
||||
{
|
||||
module.glwe_noise(self, sk_prepared, pt_want, scratch)
|
||||
}
|
||||
|
||||
pub fn assert_noise<M, BE: Backend, S, P>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: f64)
|
||||
where
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: GLWEPlaintextToRef,
|
||||
M: GLWENoise<BE>,
|
||||
{
|
||||
module.glwe_assert_noise(self, sk_prepared, pt_want, max_noise);
|
||||
module.glwe_noise(self, pt_want, sk_prepared, scratch)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GLWENoise<BE: Backend> {
|
||||
fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> Stats
|
||||
fn glwe_noise_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
R: GLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: GLWEPlaintextToRef;
|
||||
A: GLWEInfos;
|
||||
|
||||
fn glwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
|
||||
fn glwe_noise<R, P, S>(&self, res: &R, pt_want: &P, sk_prepared: &S, scratch: &mut Scratch<BE>) -> Stats
|
||||
where
|
||||
R: GLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: GLWEPlaintextToRef;
|
||||
R: GLWEToRef + GLWEInfos,
|
||||
P: GLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>;
|
||||
}
|
||||
|
||||
impl<BE: Backend> GLWENoise<BE> for Module<BE>
|
||||
where
|
||||
Module<BE>: GLWEDecrypt<BE> + VecZnxSubInplace + VecZnxNormalizeInplace<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Module<BE>: GLWEDecrypt<BE> + GLWESub + GLWENormalize<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> Stats
|
||||
fn glwe_noise_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
R: GLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: GLWEPlaintextToRef,
|
||||
A: GLWEInfos,
|
||||
{
|
||||
let res_ref: &GLWE<&[u8]> = &res.to_ref();
|
||||
|
||||
let pt_want: &GLWEPlaintext<&[u8]> = &pt_want.to_ref();
|
||||
|
||||
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res_ref);
|
||||
self.glwe_decrypt(res, &mut pt_have, sk_prepared, scratch);
|
||||
self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
|
||||
self.vec_znx_normalize_inplace(res_ref.base2k().into(), &mut pt_have.data, 0, scratch);
|
||||
pt_have.data.stats(res_ref.base2k().into(), 0)
|
||||
GLWEPlaintext::bytes_of_from_infos(infos)
|
||||
+ self
|
||||
.glwe_normalize_tmp_bytes()
|
||||
.max(self.glwe_decrypt_tmp_bytes(infos))
|
||||
}
|
||||
|
||||
fn glwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
|
||||
fn glwe_noise<R, P, S>(&self, res: &R, pt_want: &P, sk_prepared: &S, scratch: &mut Scratch<BE>) -> Stats
|
||||
where
|
||||
R: GLWEToRef,
|
||||
R: GLWEToRef + GLWEInfos,
|
||||
P: GLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: GLWEPlaintextToRef,
|
||||
{
|
||||
let res: &GLWE<&[u8]> = &res.to_ref();
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res));
|
||||
let noise_have: f64 = self
|
||||
.glwe_noise(res, sk_prepared, pt_want, scratch.borrow())
|
||||
.std()
|
||||
.log2();
|
||||
assert!(noise_have <= max_noise, "{noise_have} {max_noise}");
|
||||
let (mut pt_have, scratch_1) = scratch.take_glwe_plaintext(res);
|
||||
self.glwe_decrypt(res, &mut pt_have, sk_prepared, scratch_1);
|
||||
// println!("pt_have: {pt_have}");
|
||||
// println!("pt_want: {}", pt_want.to_ref());
|
||||
self.glwe_sub_inplace(&mut pt_have, pt_want);
|
||||
self.glwe_normalize_inplace(&mut pt_have, scratch_1);
|
||||
pt_have.data.stats(pt_have.base2k().into(), 0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,8 @@ glwe_external_product => crate::tests::test_suite::external_product::test_glwe_e
|
||||
glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace,
|
||||
// GLWE Trace
|
||||
glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace,
|
||||
glwe_packing => crate::tests::test_suite::test_glwe_packer,
|
||||
glwe_packing => crate::tests::test_suite::test_glwe_packing,
|
||||
glwe_packer => crate::tests::test_suite::test_glwe_packer,
|
||||
// GGLWE Encryption
|
||||
gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk,
|
||||
gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk,
|
||||
@@ -92,7 +93,8 @@ glwe_external_product => crate::tests::test_suite::external_product::test_glwe_e
|
||||
glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace,
|
||||
// GLWE Trace
|
||||
glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace,
|
||||
glwe_packing => crate::tests::test_suite::test_glwe_packer,
|
||||
glwe_packing => crate::tests::test_suite::test_glwe_packing,
|
||||
glwe_packer => crate::tests::test_suite::test_glwe_packer,
|
||||
// GGLWE Encryption
|
||||
gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk,
|
||||
gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk,
|
||||
|
||||
@@ -5,11 +5,11 @@ use poulpy_hal::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
GLWEAutomorphismKeyAutomorphism, GLWEAutomorphismKeyEncryptSk, GLWEDecrypt, ScratchTakeCore,
|
||||
GGLWENoise, GLWEAutomorphismKeyAutomorphism, GLWEAutomorphismKeyEncryptSk, ScratchTakeCore,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
GGLWEInfos, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWEPlaintext,
|
||||
GLWESecret, GLWESecretPreparedFactory, LWEInfos,
|
||||
GGLWEInfos, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWEInfos, GLWESecret,
|
||||
GLWESecretPreparedFactory,
|
||||
prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared},
|
||||
},
|
||||
var_noise_gglwe_product_v2,
|
||||
@@ -25,7 +25,7 @@ where
|
||||
+ GaloisElement
|
||||
+ VecZnxSubScalarInplace
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GLWEDecrypt<BE>,
|
||||
+ GGLWENoise<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||
{
|
||||
@@ -132,8 +132,6 @@ where
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
let mut pt_out: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&auto_key_out_infos);
|
||||
|
||||
let mut sk_auto: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&auto_key_out_infos);
|
||||
sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk
|
||||
for i in 0..rank {
|
||||
@@ -149,40 +147,33 @@ where
|
||||
let mut sk_auto_dft: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_auto);
|
||||
sk_auto_dft.prepare(module, &sk_auto);
|
||||
|
||||
for col_i in 0..auto_key_out.rank_in().into() {
|
||||
for row_i in 0..auto_key_out.dnum().into() {
|
||||
auto_key_out
|
||||
.at(row_i, col_i)
|
||||
.decrypt(module, &mut pt_out, &sk_auto_dft, scratch.borrow());
|
||||
let max_noise: f64 = var_noise_gglwe_product_v2(
|
||||
module.n() as f64,
|
||||
k_ksk,
|
||||
dnum_ksk,
|
||||
dsize,
|
||||
base2k_key,
|
||||
0.5,
|
||||
0.5,
|
||||
0f64,
|
||||
SIGMA * SIGMA,
|
||||
0f64,
|
||||
rank as f64,
|
||||
)
|
||||
.sqrt()
|
||||
.log2();
|
||||
|
||||
module.vec_znx_sub_scalar_inplace(
|
||||
&mut pt_out.data,
|
||||
0,
|
||||
(dsize_in - 1) + row_i * dsize_in,
|
||||
&sk.data,
|
||||
col_i,
|
||||
);
|
||||
|
||||
let noise_have: f64 = pt_out.data.stats(pt_out.base2k().into(), 0).std().log2();
|
||||
let max_noise: f64 = var_noise_gglwe_product_v2(
|
||||
module.n() as f64,
|
||||
k_ksk,
|
||||
dnum_ksk,
|
||||
dsize,
|
||||
base2k_key,
|
||||
0.5,
|
||||
0.5,
|
||||
0f64,
|
||||
SIGMA * SIGMA,
|
||||
0f64,
|
||||
rank as f64,
|
||||
)
|
||||
.sqrt()
|
||||
.log2();
|
||||
for row in 0..auto_key_out.dnum().as_usize() {
|
||||
for col in 0..auto_key_out.rank().as_usize() {
|
||||
let noise_have = auto_key_out
|
||||
.key
|
||||
.noise(module, row, col, &sk.data, &sk_auto_dft, scratch.borrow())
|
||||
.std()
|
||||
.log2();
|
||||
|
||||
assert!(
|
||||
noise_have < max_noise + 0.5,
|
||||
"{noise_have} {}",
|
||||
"{noise_have} > {}",
|
||||
max_noise + 0.5
|
||||
);
|
||||
}
|
||||
@@ -201,7 +192,7 @@ where
|
||||
+ GaloisElement
|
||||
+ VecZnxSubScalarInplace
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GLWEDecrypt<BE>,
|
||||
+ GGLWENoise<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||
{
|
||||
@@ -284,8 +275,6 @@ where
|
||||
// gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0)
|
||||
auto_key.automorphism_inplace(module, &auto_key_apply_prepared, scratch.borrow());
|
||||
|
||||
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&auto_key);
|
||||
|
||||
let mut sk_auto: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&auto_key);
|
||||
sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk
|
||||
|
||||
@@ -302,43 +291,37 @@ where
|
||||
let mut sk_auto_dft: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_auto);
|
||||
sk_auto_dft.prepare(module, &sk_auto);
|
||||
|
||||
(0..auto_key.rank_in().into()).for_each(|col_i| {
|
||||
(0..auto_key.dnum().into()).for_each(|row_i| {
|
||||
auto_key
|
||||
.at(row_i, col_i)
|
||||
.decrypt(module, &mut pt, &sk_auto_dft, scratch.borrow());
|
||||
module.vec_znx_sub_scalar_inplace(
|
||||
&mut pt.data,
|
||||
0,
|
||||
(dsize_in - 1) + row_i * dsize_in,
|
||||
&sk.data,
|
||||
col_i,
|
||||
);
|
||||
let max_noise: f64 = var_noise_gglwe_product_v2(
|
||||
module.n() as f64,
|
||||
k_ksk,
|
||||
dnum_ksk,
|
||||
dsize,
|
||||
base2k_key,
|
||||
0.5,
|
||||
0.5,
|
||||
0f64,
|
||||
SIGMA * SIGMA,
|
||||
0f64,
|
||||
rank as f64,
|
||||
)
|
||||
.sqrt()
|
||||
.log2();
|
||||
|
||||
let noise_have: f64 = pt.data.stats(pt.base2k().into(), 0).std().log2();
|
||||
let max_noise: f64 = var_noise_gglwe_product_v2(
|
||||
module.n() as f64,
|
||||
k_ksk,
|
||||
dnum_ksk,
|
||||
dsize,
|
||||
base2k_key,
|
||||
0.5,
|
||||
0.5,
|
||||
0f64,
|
||||
SIGMA * SIGMA,
|
||||
0f64,
|
||||
rank as f64,
|
||||
)
|
||||
.sqrt()
|
||||
.log2();
|
||||
for row in 0..auto_key.dnum().as_usize() {
|
||||
for col in 0..auto_key.rank().as_usize() {
|
||||
let noise_have = auto_key
|
||||
.key
|
||||
.noise(module, row, col, &sk.data, &sk_auto_dft, scratch.borrow())
|
||||
.std()
|
||||
.log2();
|
||||
|
||||
assert!(
|
||||
noise_have < max_noise + 0.5,
|
||||
"{noise_have} {}",
|
||||
max_noise + 0.5
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ use crate::{
|
||||
GGLWEToGGSWKeyEncryptSk, GGSWAutomorphism, GGSWEncryptSk, GGSWNoise, GLWEAutomorphismKeyEncryptSk, ScratchTakeCore,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
GGLWEToGGSWKey, GGLWEToGGSWKeyLayout, GGLWEToGGSWKeyPreparedFactory, GGSW, GGSWLayout, GLWEAutomorphismKey,
|
||||
GLWEAutomorphismKeyPreparedFactory, GLWESecret, GLWESecretPreparedFactory,
|
||||
GGLWEToGGSWKey, GGLWEToGGSWKeyLayout, GGLWEToGGSWKeyPreparedFactory, GGSW, GGSWInfos, GGSWLayout, GLWEAutomorphismKey,
|
||||
GLWEAutomorphismKeyPreparedFactory, GLWEInfos, GLWESecret, GLWESecretPreparedFactory,
|
||||
prepared::{GGLWEToGGSWKeyPrepared, GLWEAutomorphismKeyPrepared, GLWESecretPrepared},
|
||||
},
|
||||
noise::noise_ggsw_keyswitch,
|
||||
@@ -169,7 +169,17 @@ where
|
||||
) + 0.5
|
||||
};
|
||||
|
||||
ct_out.assert_noise(module, &sk_prepared, &pt_scalar, &max_noise);
|
||||
for row in 0..ct_out.dnum().as_usize() {
|
||||
for col in 0..ct_out.rank().as_usize() + 1 {
|
||||
assert!(
|
||||
ct_out
|
||||
.noise(module, row, col, &pt_scalar, &sk_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise(col)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -307,10 +317,22 @@ where
|
||||
k_out,
|
||||
k_ksk,
|
||||
k_tsk,
|
||||
) + 0.5
|
||||
) + 4.0
|
||||
};
|
||||
|
||||
ct.assert_noise(module, &sk_prepared, &pt_scalar, &max_noise);
|
||||
for row in 0..ct.dnum().as_usize() {
|
||||
for col in 0..ct.rank().as_usize() + 1 {
|
||||
let noise_have: f64 = ct
|
||||
.noise(module, row, col, &pt_scalar, &sk_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2();
|
||||
let noise_max: f64 = max_noise(col);
|
||||
assert!(
|
||||
noise_have <= noise_max,
|
||||
"noise_have:{noise_have} > noise_max:{noise_max}",
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +135,13 @@ where
|
||||
module.glwe_normalize(&mut pt_out, &pt_in, scratch.borrow());
|
||||
module.vec_znx_automorphism_inplace(p, &mut pt_out.data, 0, scratch.borrow());
|
||||
|
||||
ct_out.assert_noise(module, &sk_prepared, &pt_out, max_noise + 1.0);
|
||||
assert!(
|
||||
ct_out
|
||||
.noise(module, &pt_out, &sk_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise + 1.0
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -249,7 +255,12 @@ where
|
||||
|
||||
module.vec_znx_automorphism_inplace(p, &mut pt_want.data, 0, scratch.borrow());
|
||||
|
||||
ct.assert_noise(module, &sk_prepared, &pt_want, max_noise + 1.0);
|
||||
assert!(
|
||||
ct.noise(module, &pt_want, &sk_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise + 1.0
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,14 +56,14 @@ where
|
||||
sk_prep.prepare(module, &sk);
|
||||
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
|
||||
GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos_in).max(GLWE::decrypt_tmp_bytes(module, &glwe_infos_out)),
|
||||
GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos_in).max(module.glwe_noise_tmp_bytes(&glwe_infos_out)),
|
||||
);
|
||||
|
||||
let mut ct_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos_in);
|
||||
let mut ct_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos_out);
|
||||
|
||||
let pt_in: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos_in);
|
||||
let pt_out: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos_in);
|
||||
let pt_out: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos_out);
|
||||
|
||||
ct_in.encrypt_sk(
|
||||
module,
|
||||
@@ -87,12 +87,13 @@ where
|
||||
.data()
|
||||
.decode_vec_float(ct_out.base2k().into(), 0, &mut data_conv);
|
||||
|
||||
ct_out.assert_noise(
|
||||
module,
|
||||
&sk_prep,
|
||||
&pt_out,
|
||||
-(ct_out.k().as_u32() as f64) + SIGMA.log2() + 0.5,
|
||||
);
|
||||
assert!(
|
||||
ct_out
|
||||
.noise(module, &pt_out, &sk_prep, scratch.borrow())
|
||||
.std()
|
||||
.log2()
|
||||
<= -(ct_out.k().as_u32() as f64) + SIGMA.log2() + 0.50
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ use crate::{
|
||||
GLWESwitchingKeyEncryptSk, ScratchTakeCore,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
GLWEAutomorphismKey, GLWEAutomorphismKeyDecompress, GLWEAutomorphismKeyLayout, GLWEInfos, GLWESecret,
|
||||
GLWESecretPreparedFactory, GLWESwitchingKeyDecompress, compressed::GLWEAutomorphismKeyCompressed,
|
||||
GGLWEInfos, GLWEAutomorphismKey, GLWEAutomorphismKeyDecompress, GLWEAutomorphismKeyLayout, GLWEInfos, GLWESecret,
|
||||
GLWESecretPreparedFactory, GLWESwitchingKeyDecompress, LWEInfos, compressed::GLWEAutomorphismKeyCompressed,
|
||||
prepared::GLWESecretPrepared,
|
||||
},
|
||||
noise::GGLWENoise,
|
||||
@@ -84,8 +84,26 @@ where
|
||||
let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, sk_out.rank());
|
||||
sk_out_prepared.prepare(module, &sk_out);
|
||||
|
||||
atk.key
|
||||
.assert_noise(module, &sk_out_prepared, &sk.data, SIGMA);
|
||||
let max_noise: f64 = SIGMA.log2() - (atk.k().as_usize() as f64) + 0.5;
|
||||
|
||||
for row in 0..atk.dnum().as_usize() {
|
||||
for col in 0..atk.rank().as_usize() {
|
||||
assert!(
|
||||
atk.key
|
||||
.noise(
|
||||
module,
|
||||
row,
|
||||
col,
|
||||
&sk.data,
|
||||
&sk_out_prepared,
|
||||
scratch.borrow()
|
||||
)
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -106,18 +124,18 @@ where
|
||||
{
|
||||
let base2k: usize = 12;
|
||||
let k_ksk: usize = 60;
|
||||
let dsize: usize = k_ksk.div_ceil(base2k) - 1;
|
||||
for rank in 1_usize..3 {
|
||||
for di in 1..dsize + 1 {
|
||||
let max_dsize: usize = k_ksk.div_ceil(base2k) - 1;
|
||||
for rank in 2_usize..3 {
|
||||
for dsize in 1..max_dsize + 1 {
|
||||
let n: usize = module.n();
|
||||
let dnum: usize = (k_ksk - di * base2k) / (di * base2k);
|
||||
let dnum: usize = (k_ksk - dsize * base2k) / (dsize * base2k);
|
||||
|
||||
let atk_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout {
|
||||
n: n.into(),
|
||||
base2k: base2k.into(),
|
||||
k: k_ksk.into(),
|
||||
dnum: dnum.into(),
|
||||
dsize: di.into(),
|
||||
dsize: dsize.into(),
|
||||
rank: rank.into(),
|
||||
};
|
||||
|
||||
@@ -134,7 +152,7 @@ where
|
||||
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&atk_infos);
|
||||
sk.fill_ternary_prob(0.5, &mut source_xs);
|
||||
|
||||
let p = -5;
|
||||
let p: i64 = -5;
|
||||
|
||||
let seed_xa: [u8; 32] = [1u8; 32];
|
||||
|
||||
@@ -156,8 +174,31 @@ where
|
||||
let mut atk: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&atk_infos);
|
||||
atk.decompress(module, &atk_compressed);
|
||||
|
||||
atk.key
|
||||
.assert_noise(module, &sk_out_prepared, &sk.data, SIGMA);
|
||||
let max_noise: f64 = SIGMA.log2() - (atk.k().as_usize() as f64) + 0.5;
|
||||
|
||||
println!("rank: {rank} dsize: {dsize} dnum: {dnum}");
|
||||
for row in 0..atk.dnum().as_usize() {
|
||||
for col in 0..atk.rank().as_usize() {
|
||||
let noise_have = atk
|
||||
.key
|
||||
.noise(
|
||||
module,
|
||||
row,
|
||||
col,
|
||||
&sk.data,
|
||||
&sk_out_prepared,
|
||||
scratch.borrow(),
|
||||
)
|
||||
.std()
|
||||
.log2();
|
||||
|
||||
assert!(
|
||||
noise_have < max_noise + 0.5,
|
||||
"row:{row} col:{col} noise_have:{noise_have} > max_noise:{}",
|
||||
max_noise + 0.5
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ use crate::{
|
||||
decryption::GLWEDecrypt,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
GGLWELayout, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyCompressed,
|
||||
GLWESwitchingKeyDecompress,
|
||||
GGLWEInfos, GGLWELayout, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyCompressed,
|
||||
GLWESwitchingKeyDecompress, LWEInfos,
|
||||
prepared::{GGLWEPreparedFactory, GLWESecretPrepared},
|
||||
},
|
||||
noise::GGLWENoise,
|
||||
@@ -74,8 +74,30 @@ where
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
ksk.key
|
||||
.assert_noise(module, &sk_out_prepared, &sk_in.data, SIGMA);
|
||||
let max_noise: f64 = SIGMA.log2() - (ksk.k().as_usize() as f64) + 0.5;
|
||||
|
||||
for row in 0..ksk.dnum().as_usize() {
|
||||
for col in 0..ksk.rank_in().as_usize() {
|
||||
let noise_have = ksk
|
||||
.key
|
||||
.noise(
|
||||
module,
|
||||
row,
|
||||
col,
|
||||
&sk_in.data,
|
||||
&sk_out_prepared,
|
||||
scratch.borrow(),
|
||||
)
|
||||
.std()
|
||||
.log2();
|
||||
|
||||
assert!(
|
||||
noise_have < max_noise + 0.5,
|
||||
"row:{row} col:{col} noise_have:{noise_have} > max_noise:{}",
|
||||
max_noise + 0.5
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -148,8 +170,30 @@ where
|
||||
let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos);
|
||||
ksk.decompress(module, &ksk_compressed);
|
||||
|
||||
ksk.key
|
||||
.assert_noise(module, &sk_out_prepared, &sk_in.data, SIGMA);
|
||||
let max_noise: f64 = SIGMA.log2() - (ksk.k().as_usize() as f64) + 0.5;
|
||||
|
||||
for row in 0..ksk.dnum().as_usize() {
|
||||
for col in 0..ksk.rank_in().as_usize() {
|
||||
let noise_have = ksk
|
||||
.key
|
||||
.noise(
|
||||
module,
|
||||
row,
|
||||
col,
|
||||
&sk_in.data,
|
||||
&sk_out_prepared,
|
||||
scratch.borrow(),
|
||||
)
|
||||
.std()
|
||||
.log2();
|
||||
|
||||
assert!(
|
||||
noise_have < max_noise + 0.5,
|
||||
"row:{row} col:{col} noise_have:{noise_have} > max_noise:{}",
|
||||
max_noise + 0.5
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,9 @@ use crate::{
|
||||
decryption::GLWEDecrypt,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
Dsize, GGLWEDecompress, GGLWEToGGSWKey, GGLWEToGGSWKeyCompressed, GGLWEToGGSWKeyDecompress, GGLWEToGGSWKeyLayout,
|
||||
GLWESecret, GLWESecretPreparedFactory, GLWESecretTensor, GLWESecretTensorFactory, LWEInfos, prepared::GLWESecretPrepared,
|
||||
Dsize, GGLWE, GGLWEDecompress, GGLWEInfos, GGLWEToGGSWKey, GGLWEToGGSWKeyCompressed, GGLWEToGGSWKeyDecompress,
|
||||
GGLWEToGGSWKeyLayout, GLWESecret, GLWESecretPreparedFactory, GLWESecretTensor, GLWESecretTensorFactory,
|
||||
LWEInfos, prepared::GLWESecretPrepared,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -79,7 +80,17 @@ where
|
||||
);
|
||||
}
|
||||
|
||||
module.gglwe_assert_noise(key.at(i), &sk_prepared, &pt_want, max_noise);
|
||||
let ksk: &GGLWE<Vec<u8>> = key.at(i);
|
||||
for row in 0..ksk.dnum().as_usize() {
|
||||
for col in 0..ksk.rank_in().as_usize() {
|
||||
assert!(
|
||||
ksk.noise(module, row, col, &pt_want, &sk_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -135,8 +146,31 @@ where
|
||||
let mut sk_tensor: GLWESecretTensor<Vec<u8>> = GLWESecretTensor::alloc_from_infos(&sk);
|
||||
sk_tensor.prepare(module, &sk, scratch.borrow());
|
||||
|
||||
let max_noise = SIGMA.log2() + 0.5 - (key.k().as_u32() as f64);
|
||||
|
||||
let mut pt_want: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(module.n(), rank);
|
||||
|
||||
for i in 0..rank {
|
||||
module.gglwe_assert_noise(key.at(i), &sk_prepared, &sk_tensor.data, SIGMA + 0.5);
|
||||
for j in 0..rank {
|
||||
module.vec_znx_copy(
|
||||
&mut pt_want.as_vec_znx_mut(),
|
||||
j,
|
||||
&sk_tensor.at(i, j).as_vec_znx(),
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
let ksk: &GGLWE<Vec<u8>> = key.at(i);
|
||||
for row in 0..ksk.dnum().as_usize() {
|
||||
for col in 0..ksk.rank_in().as_usize() {
|
||||
assert!(
|
||||
ksk.noise(module, row, col, &pt_want, &sk_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ use crate::{
|
||||
GGSWCompressedEncryptSk, GGSWEncryptSk, GGSWNoise, ScratchTakeCore,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
GGSW, GGSWDecompress, GGSWLayout, GLWESecret, GLWESecretPreparedFactory, compressed::GGSWCompressed,
|
||||
prepared::GLWESecretPrepared,
|
||||
GGSW, GGSWDecompress, GGSWInfos, GGSWLayout, GLWEInfos, GLWESecret, GLWESecretPreparedFactory,
|
||||
compressed::GGSWCompressed, prepared::GLWESecretPrepared,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -65,7 +65,16 @@ where
|
||||
|
||||
let noise_f = |_col_i: usize| -(k as f64) + SIGMA.log2() + 0.5;
|
||||
|
||||
ct.assert_noise(module, &sk_prepared, &pt_scalar, &noise_f);
|
||||
for row in 0..ct.dnum().as_usize() {
|
||||
for col in 0..ct.rank().as_usize() + 1 {
|
||||
assert!(
|
||||
ct.noise(module, row, col, &pt_scalar, &sk_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2()
|
||||
<= noise_f(col)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -126,7 +135,16 @@ where
|
||||
let mut ct: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos);
|
||||
ct.decompress(module, &ct_compressed);
|
||||
|
||||
ct.assert_noise(module, &sk_prepared, &pt_scalar, &noise_f);
|
||||
for row in 0..ct.dnum().as_usize() {
|
||||
for col in 0..ct.rank().as_usize() + 1 {
|
||||
assert!(
|
||||
ct.noise(module, row, col, &pt_scalar, &sk_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2()
|
||||
<= noise_f(col)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,7 @@ use poulpy_hal::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
GLWECompressedEncryptSk, GLWEEncryptPk, GLWEEncryptSk, GLWEPublicKeyGenerate, GLWESub, ScratchTakeCore,
|
||||
decryption::GLWEDecrypt,
|
||||
GLWECompressedEncryptSk, GLWEEncryptPk, GLWEEncryptSk, GLWENoise, GLWEPublicKeyGenerate, GLWESub, ScratchTakeCore,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWEPublicKeyPreparedFactory, GLWESecret,
|
||||
@@ -18,7 +17,7 @@ use crate::{
|
||||
|
||||
pub fn test_glwe_encrypt_sk<BE: Backend>(module: &Module<BE>)
|
||||
where
|
||||
Module<BE>: GLWEEncryptSk<BE> + GLWEDecrypt<BE> + GLWESecretPreparedFactory<BE> + VecZnxFillUniform + GLWESub,
|
||||
Module<BE>: GLWEEncryptSk<BE> + GLWENoise<BE> + GLWESecretPreparedFactory<BE> + VecZnxFillUniform + GLWESub,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||
{
|
||||
@@ -44,14 +43,13 @@ where
|
||||
|
||||
let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
|
||||
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&pt_infos);
|
||||
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&pt_infos);
|
||||
|
||||
let mut source_xs: Source = Source::new([0u8; 32]);
|
||||
let mut source_xe: Source = Source::new([0u8; 32]);
|
||||
let mut source_xa: Source = Source::new([0u8; 32]);
|
||||
|
||||
let mut scratch: ScratchOwned<BE> =
|
||||
ScratchOwned::alloc(GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos));
|
||||
ScratchOwned::alloc(GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos).max(module.glwe_noise_tmp_bytes(&glwe_infos)));
|
||||
|
||||
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
|
||||
sk.fill_ternary_prob(0.5, &mut source_xs);
|
||||
@@ -70,12 +68,11 @@ where
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
|
||||
|
||||
module.glwe_sub_inplace(&mut pt_want, &pt_have);
|
||||
|
||||
let noise_have: f64 = pt_want.data.stats(base2k, 0).std() * (ct.k().as_u32() as f64).exp2();
|
||||
let noise_want: f64 = SIGMA;
|
||||
let noise_have: f64 = ct
|
||||
.noise(module, &pt_want, &sk_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2();
|
||||
let noise_want: f64 = SIGMA.log2() - (ct.k().as_usize() as f64) + 0.5;
|
||||
|
||||
assert!(noise_have <= noise_want + 0.2);
|
||||
}
|
||||
@@ -83,7 +80,7 @@ where
|
||||
|
||||
pub fn test_glwe_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>)
|
||||
where
|
||||
Module<BE>: GLWECompressedEncryptSk<BE> + GLWEDecrypt<BE> + GLWESecretPreparedFactory<BE> + VecZnxFillUniform + GLWESub,
|
||||
Module<BE>: GLWECompressedEncryptSk<BE> + GLWENoise<BE> + GLWESecretPreparedFactory<BE> + VecZnxFillUniform + GLWESub,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||
{
|
||||
@@ -110,14 +107,13 @@ where
|
||||
let mut ct_compressed: GLWECompressed<Vec<u8>> = GLWECompressed::alloc_from_infos(&glwe_infos);
|
||||
|
||||
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&pt_infos);
|
||||
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&pt_infos);
|
||||
|
||||
let mut source_xs: Source = Source::new([0u8; 32]);
|
||||
let mut source_xe: Source = Source::new([0u8; 32]);
|
||||
let mut source_xa: Source = Source::new([0u8; 32]);
|
||||
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
|
||||
GLWECompressed::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos),
|
||||
GLWECompressed::encrypt_sk_tmp_bytes(module, &glwe_infos).max(module.glwe_noise_tmp_bytes(&glwe_infos)),
|
||||
);
|
||||
|
||||
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
|
||||
@@ -142,24 +138,18 @@ where
|
||||
let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
|
||||
ct.decompress(module, &ct_compressed);
|
||||
|
||||
ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
|
||||
|
||||
module.glwe_sub_inplace(&mut pt_want, &pt_have);
|
||||
|
||||
let noise_have: f64 = pt_want.data.stats(base2k, 0).std() * (ct.k().as_u32() as f64).exp2();
|
||||
let noise_want: f64 = SIGMA;
|
||||
|
||||
assert!(
|
||||
noise_have <= noise_want + 0.2,
|
||||
"{noise_have} <= {}",
|
||||
noise_want + 0.2
|
||||
);
|
||||
let noise_have: f64 = ct
|
||||
.noise(module, &pt_want, &sk_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2();
|
||||
let noise_want: f64 = SIGMA.log2() - (ct.k().as_usize() as f64) + 0.5;
|
||||
assert!(noise_have <= noise_want + 0.2);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn test_glwe_encrypt_zero_sk<BE: Backend>(module: &Module<BE>)
|
||||
where
|
||||
Module<BE>: GLWEEncryptSk<BE> + GLWEDecrypt<BE> + GLWESecretPreparedFactory<BE> + VecZnxFillUniform + GLWESub,
|
||||
Module<BE>: GLWEEncryptSk<BE> + GLWENoise<BE> + GLWESecretPreparedFactory<BE> + VecZnxFillUniform + GLWESub,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||
{
|
||||
@@ -176,14 +166,17 @@ where
|
||||
rank: rank.into(),
|
||||
};
|
||||
|
||||
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
|
||||
let pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
|
||||
|
||||
let mut source_xs: Source = Source::new([0u8; 32]);
|
||||
let mut source_xe: Source = Source::new([1u8; 32]);
|
||||
let mut source_xa: Source = Source::new([0u8; 32]);
|
||||
|
||||
let mut scratch: ScratchOwned<BE> =
|
||||
ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos));
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
|
||||
module
|
||||
.glwe_noise_tmp_bytes(&glwe_infos)
|
||||
.max(GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos)),
|
||||
);
|
||||
|
||||
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
|
||||
sk.fill_ternary_prob(0.5, &mut source_xs);
|
||||
@@ -200,9 +193,13 @@ where
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
ct.decrypt(module, &mut pt, &sk_prepared, scratch.borrow());
|
||||
|
||||
assert!((SIGMA - pt.data.stats(base2k, 0).std() * (k_ct as f64).exp2()) <= 0.2);
|
||||
let noise_have: f64 = ct
|
||||
.noise(module, &pt, &sk_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2();
|
||||
let noise_want: f64 = SIGMA.log2() - (ct.k().as_usize() as f64) + 0.5;
|
||||
assert!(noise_have <= noise_want + 0.2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,7 +208,7 @@ where
|
||||
Module<BE>: GLWEEncryptPk<BE>
|
||||
+ GLWEPublicKeyPreparedFactory<BE>
|
||||
+ GLWEPublicKeyGenerate<BE>
|
||||
+ GLWEDecrypt<BE>
|
||||
+ GLWENoise<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ VecZnxFillUniform
|
||||
+ GLWESub,
|
||||
@@ -232,7 +229,6 @@ where
|
||||
};
|
||||
|
||||
let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
|
||||
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
|
||||
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
|
||||
|
||||
let mut source_xs: Source = Source::new([0u8; 32]);
|
||||
@@ -240,8 +236,11 @@ where
|
||||
let mut source_xa: Source = Source::new([0u8; 32]);
|
||||
let mut source_xu: Source = Source::new([0u8; 32]);
|
||||
|
||||
let mut scratch: ScratchOwned<BE> =
|
||||
ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_pk_tmp_bytes(module, &glwe_infos));
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
|
||||
module
|
||||
.glwe_noise_tmp_bytes(&glwe_infos)
|
||||
.max(GLWE::encrypt_pk_tmp_bytes(module, &glwe_infos)),
|
||||
);
|
||||
|
||||
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
|
||||
sk.fill_ternary_prob(0.5, &mut source_xs);
|
||||
@@ -266,17 +265,11 @@ where
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
|
||||
|
||||
module.glwe_sub_inplace(&mut pt_want, &pt_have);
|
||||
|
||||
let noise_have: f64 = pt_want.data.stats(base2k, 0).std().log2();
|
||||
let noise_have: f64 = ct
|
||||
.noise(module, &pt_want, &sk_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2();
|
||||
let noise_want: f64 = ((((rank as f64) + 1.0) * n as f64 * 0.5 * SIGMA * SIGMA).sqrt()).log2() - (k_ct as f64);
|
||||
|
||||
assert!(
|
||||
noise_have <= noise_want + 0.2,
|
||||
"{noise_have} <= {}",
|
||||
noise_want + 0.2
|
||||
);
|
||||
assert!(noise_have <= noise_want + 0.2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,9 @@ use crate::{
|
||||
decryption::GLWEDecrypt,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
Dsize, GGLWEDecompress, GLWESecret, GLWESecretPreparedFactory, GLWESecretTensor, GLWESecretTensorFactory, GLWETensorKey,
|
||||
GLWETensorKeyCompressed, GLWETensorKeyLayout, prepared::GLWESecretPrepared,
|
||||
Dsize, GGLWEDecompress, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPreparedFactory, GLWESecretTensor,
|
||||
GLWESecretTensorFactory, GLWETensorKey, GLWETensorKeyCompressed, GLWETensorKeyLayout, LWEInfos,
|
||||
prepared::GLWESecretPrepared,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -67,7 +68,27 @@ where
|
||||
let mut sk_tensor: GLWESecretTensor<Vec<u8>> = GLWESecretTensor::alloc_from_infos(&sk);
|
||||
sk_tensor.prepare(module, &sk, scratch.borrow());
|
||||
|
||||
module.gglwe_assert_noise(&tensor_key, &sk_prepared, &sk_tensor.data, SIGMA + 0.5);
|
||||
let max_noise: f64 = SIGMA.log2() - (tensor_key.k().as_usize() as f64) + 0.5;
|
||||
|
||||
for row in 0..tensor_key.dnum().as_usize() {
|
||||
for col in 0..tensor_key.rank().as_usize() + 1 {
|
||||
assert!(
|
||||
tensor_key
|
||||
.0
|
||||
.noise(
|
||||
module,
|
||||
row,
|
||||
col,
|
||||
&sk_tensor.data,
|
||||
&sk_prepared,
|
||||
scratch.borrow()
|
||||
)
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,6 +145,26 @@ where
|
||||
let mut sk_tensor: GLWESecretTensor<Vec<u8>> = GLWESecretTensor::alloc_from_infos(&sk);
|
||||
sk_tensor.prepare(module, &sk, scratch.borrow());
|
||||
|
||||
module.gglwe_assert_noise(&tensor_key, &sk_prepared, &sk_tensor.data, SIGMA + 0.5);
|
||||
let max_noise: f64 = SIGMA.log2() - (tensor_key.k().as_usize() as f64) + 0.5;
|
||||
|
||||
for row in 0..tensor_key.dnum().as_usize() {
|
||||
for col in 0..tensor_key.rank().as_usize() + 1 {
|
||||
assert!(
|
||||
tensor_key
|
||||
.0
|
||||
.noise(
|
||||
module,
|
||||
row,
|
||||
col,
|
||||
&sk_tensor.data,
|
||||
&sk_prepared,
|
||||
scratch.borrow()
|
||||
)
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,8 @@ use crate::{
|
||||
GGLWEExternalProduct, GGLWENoise, GGSWEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
GGSW, GGSWLayout, GGSWPreparedFactory, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyLayout,
|
||||
GGLWEInfos, GGSW, GGSWLayout, GGSWPreparedFactory, GLWEInfos, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey,
|
||||
GLWESwitchingKeyLayout,
|
||||
prepared::{GGSWPrepared, GLWESecretPrepared},
|
||||
},
|
||||
noise::noise_ggsw_product,
|
||||
@@ -159,9 +160,25 @@ where
|
||||
k_ggsw,
|
||||
);
|
||||
|
||||
ct_gglwe_out
|
||||
.key
|
||||
.assert_noise(module, &sk_out_prepared, &sk_in.data, max_noise + 0.5);
|
||||
for row in 0..ct_gglwe_out.dnum().as_usize() {
|
||||
for col in 0..ct_gglwe_out.rank_in().as_usize() {
|
||||
assert!(
|
||||
ct_gglwe_out
|
||||
.key
|
||||
.noise(
|
||||
module,
|
||||
row,
|
||||
col,
|
||||
&sk_in.data,
|
||||
&sk_out_prepared,
|
||||
scratch.borrow()
|
||||
)
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise + 0.5
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -300,9 +317,25 @@ where
|
||||
k_ggsw,
|
||||
);
|
||||
|
||||
ct_gglwe
|
||||
.key
|
||||
.assert_noise(module, &sk_out_prepared, &sk_in.data, max_noise + 0.5);
|
||||
for row in 0..ct_gglwe.dnum().as_usize() {
|
||||
for col in 0..ct_gglwe.rank_in().as_usize() {
|
||||
assert!(
|
||||
ct_gglwe
|
||||
.key
|
||||
.noise(
|
||||
module,
|
||||
row,
|
||||
col,
|
||||
&sk_in.data,
|
||||
&sk_out_prepared,
|
||||
scratch.borrow()
|
||||
)
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise + 0.5
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ use crate::{
|
||||
GGSWEncryptSk, GGSWExternalProduct, GGSWNoise, ScratchTakeCore,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
GGSW, GGSWLayout, GGSWPreparedFactory, GLWESecret, GLWESecretPreparedFactory,
|
||||
GGSW, GGSWInfos, GGSWLayout, GGSWPreparedFactory, GLWEInfos, GLWESecret, GLWESecretPreparedFactory,
|
||||
prepared::{GGSWPrepared, GLWESecretPrepared},
|
||||
},
|
||||
noise::noise_ggsw_product,
|
||||
@@ -146,7 +146,17 @@ where
|
||||
) + 0.5
|
||||
};
|
||||
|
||||
ggsw_out.assert_noise(module, &sk_prepared, &pt_in, &max_noise);
|
||||
for row in 0..ggsw_out.dnum().as_usize() {
|
||||
for col in 0..ggsw_out.rank().as_usize() + 1 {
|
||||
assert!(
|
||||
ggsw_out
|
||||
.noise(module, row, col, &pt_in, &sk_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise(col)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -271,7 +281,17 @@ where
|
||||
) + 0.5
|
||||
};
|
||||
|
||||
ggsw_out.assert_noise(module, &sk_prepared, &pt_in, &max_noise);
|
||||
for row in 0..ggsw_out.dnum().as_usize() {
|
||||
for col in 0..ggsw_out.rank().as_usize() + 1 {
|
||||
assert!(
|
||||
ggsw_out
|
||||
.noise(module, row, col, &pt_in, &sk_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise(col)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,7 +145,13 @@ where
|
||||
k_ggsw,
|
||||
);
|
||||
|
||||
glwe_out.assert_noise(module, &sk_prepared, &pt_out, max_noise + 0.5);
|
||||
assert!(
|
||||
glwe_out
|
||||
.noise(module, &pt_out, &sk_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise + 1.0
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -268,7 +274,13 @@ where
|
||||
k_ggsw,
|
||||
);
|
||||
|
||||
glwe_out.assert_noise(module, &sk_prepared, &pt_want, max_noise + 0.5);
|
||||
assert!(
|
||||
glwe_out
|
||||
.noise(module, &pt_want, &sk_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise + 1.0
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,25 +33,26 @@ where
|
||||
let mut source_xa: Source = Source::new([0u8; 32]);
|
||||
|
||||
let n: usize = module.n();
|
||||
let base2k: usize = 18;
|
||||
let base2k_out: usize = 15;
|
||||
let base2k_key: usize = 10;
|
||||
let k_ct: usize = 36;
|
||||
let pt_k: usize = 18;
|
||||
let rank: usize = 3;
|
||||
let dsize: usize = 1;
|
||||
let k_ksk: usize = k_ct + base2k * dsize;
|
||||
let k_ksk: usize = k_ct + base2k_key * dsize;
|
||||
|
||||
let dnum: usize = k_ct.div_ceil(base2k * dsize);
|
||||
let dnum: usize = k_ct.div_ceil(base2k_key * dsize);
|
||||
|
||||
let glwe_out_infos: GLWELayout = GLWELayout {
|
||||
n: n.into(),
|
||||
base2k: base2k.into(),
|
||||
base2k: base2k_out.into(),
|
||||
k: k_ct.into(),
|
||||
rank: rank.into(),
|
||||
};
|
||||
|
||||
let key_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout {
|
||||
n: n.into(),
|
||||
base2k: base2k.into(),
|
||||
base2k: base2k_key.into(),
|
||||
k: k_ksk.into(),
|
||||
rank: rank.into(),
|
||||
dsize: dsize.into(),
|
||||
@@ -134,7 +135,7 @@ where
|
||||
});
|
||||
|
||||
let mut res: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos);
|
||||
packer.flush(module, &mut res);
|
||||
packer.flush(module, &mut res, scratch.borrow());
|
||||
|
||||
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
|
||||
let mut data: Vec<i64> = vec![0i64; n];
|
||||
@@ -153,7 +154,7 @@ where
|
||||
let noise_have: f64 = pt.stats().std().log2();
|
||||
|
||||
assert!(
|
||||
noise_have < -((k_ct - base2k) as f64),
|
||||
noise_have < -((k_ct - base2k_out) as f64),
|
||||
"noise: {noise_have}"
|
||||
);
|
||||
}
|
||||
|
||||
148
poulpy-core/src/tests/test_suite/glwe_packing.rs
Normal file
148
poulpy-core/src/tests/test_suite/glwe_packing.rs
Normal file
@@ -0,0 +1,148 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use itertools::Itertools;
|
||||
use poulpy_hal::{
|
||||
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
layouts::{Backend, Module, Scratch, ScratchOwned},
|
||||
source::Source,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
GLWEAutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWENoise, GLWEPacking, GLWERotate, GLWESub, ScratchTakeCore,
|
||||
layouts::{
|
||||
GLWE, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext,
|
||||
GLWESecret, GLWESecretPreparedFactory,
|
||||
prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared},
|
||||
},
|
||||
};
|
||||
|
||||
pub fn test_glwe_packing<BE: Backend>(module: &Module<BE>)
|
||||
where
|
||||
Module<BE>: GLWEEncryptSk<BE>
|
||||
+ GLWEAutomorphismKeyEncryptSk<BE>
|
||||
+ GLWEAutomorphismKeyPreparedFactory<BE>
|
||||
+ GLWEPacking<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GLWESub
|
||||
+ GLWEDecrypt<BE>
|
||||
+ GLWERotate<BE>
|
||||
+ GLWENoise<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||
{
|
||||
let mut source_xs: Source = Source::new([0u8; 32]);
|
||||
let mut source_xe: Source = Source::new([0u8; 32]);
|
||||
let mut source_xa: Source = Source::new([0u8; 32]);
|
||||
|
||||
let n: usize = module.n();
|
||||
let base2k_out: usize = 15;
|
||||
let base2k_key: usize = 10;
|
||||
let k_ct: usize = 36;
|
||||
let pt_k: usize = base2k_out;
|
||||
let rank: usize = 3;
|
||||
let dsize: usize = 1;
|
||||
let k_ksk: usize = k_ct + base2k_key * dsize;
|
||||
|
||||
let dnum: usize = k_ct.div_ceil(base2k_key * dsize);
|
||||
|
||||
let glwe_out_infos: GLWELayout = GLWELayout {
|
||||
n: n.into(),
|
||||
base2k: base2k_out.into(),
|
||||
k: k_ct.into(),
|
||||
rank: rank.into(),
|
||||
};
|
||||
|
||||
let key_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout {
|
||||
n: n.into(),
|
||||
base2k: base2k_key.into(),
|
||||
k: k_ksk.into(),
|
||||
rank: rank.into(),
|
||||
dsize: dsize.into(),
|
||||
dnum: dnum.into(),
|
||||
};
|
||||
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
|
||||
GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos)
|
||||
.max(GLWEAutomorphismKey::encrypt_sk_tmp_bytes(
|
||||
module, &key_infos,
|
||||
))
|
||||
.max(module.glwe_pack_tmp_bytes(&glwe_out_infos, &key_infos)),
|
||||
);
|
||||
|
||||
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_out_infos);
|
||||
sk.fill_ternary_prob(0.5, &mut source_xs);
|
||||
|
||||
let mut sk_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk);
|
||||
sk_prep.prepare(module, &sk);
|
||||
|
||||
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
|
||||
let mut data: Vec<i64> = vec![0i64; n];
|
||||
data.iter_mut().enumerate().for_each(|(i, x)| {
|
||||
*x = i as i64;
|
||||
});
|
||||
|
||||
pt.encode_vec_i64(&data, pt_k.into());
|
||||
|
||||
let gal_els: Vec<i64> = module.glwe_pack_galois_elements();
|
||||
|
||||
let mut auto_keys: HashMap<i64, GLWEAutomorphismKeyPrepared<Vec<u8>, BE>> = HashMap::new();
|
||||
let mut tmp: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&key_infos);
|
||||
gal_els.iter().for_each(|gal_el| {
|
||||
tmp.encrypt_sk(
|
||||
module,
|
||||
*gal_el,
|
||||
&sk,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
let mut atk_prepared: GLWEAutomorphismKeyPrepared<Vec<u8>, BE> =
|
||||
GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &tmp);
|
||||
atk_prepared.prepare(module, &tmp, scratch.borrow());
|
||||
auto_keys.insert(*gal_el, atk_prepared);
|
||||
});
|
||||
|
||||
let mut cts = (0..n)
|
||||
.step_by(5)
|
||||
.map(|_| {
|
||||
let mut ct = GLWE::alloc_from_infos(&glwe_out_infos);
|
||||
ct.encrypt_sk(
|
||||
module,
|
||||
&pt,
|
||||
&sk_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
module.glwe_rotate_inplace(-5, &mut pt, scratch.borrow()); // X^-batch * pt
|
||||
ct
|
||||
})
|
||||
.collect_vec();
|
||||
|
||||
let mut cts_map: HashMap<usize, &mut GLWE<Vec<u8>>> = HashMap::new();
|
||||
|
||||
for (i, ct) in cts.iter_mut().enumerate() {
|
||||
cts_map.insert(5 * i, ct);
|
||||
}
|
||||
|
||||
let mut res: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos);
|
||||
|
||||
module.glwe_pack(&mut res, cts_map, 0, &auto_keys, scratch.borrow());
|
||||
|
||||
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
|
||||
let mut data: Vec<i64> = vec![0i64; n];
|
||||
data.iter_mut().enumerate().for_each(|(i, x)| {
|
||||
if i.is_multiple_of(5) {
|
||||
*x = i as i64;
|
||||
}
|
||||
});
|
||||
|
||||
pt_want.encode_vec_i64(&data, pt_k.into());
|
||||
|
||||
assert!(
|
||||
res.noise(module, &pt_want, &sk_prep, scratch.borrow())
|
||||
.std()
|
||||
.log2()
|
||||
<= ((k_ct - base2k_out) as f64)
|
||||
);
|
||||
}
|
||||
@@ -8,7 +8,8 @@ use crate::{
|
||||
GGLWEKeyswitch, GGLWENoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyLayout, GLWESwitchingKeyPreparedFactory,
|
||||
GGLWEInfos, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyLayout,
|
||||
GLWESwitchingKeyPreparedFactory,
|
||||
prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared},
|
||||
},
|
||||
noise::log2_std_noise_gglwe_product,
|
||||
@@ -154,9 +155,25 @@ where
|
||||
.sqrt()
|
||||
.log2();
|
||||
|
||||
gglwe_s0s2
|
||||
.key
|
||||
.assert_noise(module, &sk2_prepared, &sk0.data, max_noise + 0.5);
|
||||
for row in 0..gglwe_s0s2.dnum().as_usize() {
|
||||
for col in 0..gglwe_s0s2.rank_in().as_usize() {
|
||||
assert!(
|
||||
gglwe_s0s2
|
||||
.key
|
||||
.noise(
|
||||
module,
|
||||
row,
|
||||
col,
|
||||
&sk0.data,
|
||||
&sk2_prepared,
|
||||
scratch_apply.borrow()
|
||||
)
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise + 0.5
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -284,9 +301,25 @@ where
|
||||
k_ksk,
|
||||
);
|
||||
|
||||
gglwe_s0s2
|
||||
.key
|
||||
.assert_noise(module, &sk2_prepared, &sk0.data, max_noise + 0.5);
|
||||
for row in 0..gglwe_s0s2.dnum().as_usize() {
|
||||
for col in 0..gglwe_s0s2.rank_in().as_usize() {
|
||||
assert!(
|
||||
gglwe_s0s2
|
||||
.key
|
||||
.noise(
|
||||
module,
|
||||
row,
|
||||
col,
|
||||
&sk0.data,
|
||||
&sk2_prepared,
|
||||
scratch_apply.borrow()
|
||||
)
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise + 0.5
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ use crate::{
|
||||
GGLWEToGGSWKeyEncryptSk, GGSWEncryptSk, GGSWKeyswitch, GGSWNoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
GGLWEToGGSWKey, GGLWEToGGSWKeyPrepared, GGLWEToGGSWKeyPreparedFactory, GGSW, GGSWLayout, GLWESecret,
|
||||
GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyLayout, GLWESwitchingKeyPreparedFactory,
|
||||
GGLWEToGGSWKey, GGLWEToGGSWKeyPrepared, GGLWEToGGSWKeyPreparedFactory, GGSW, GGSWInfos, GGSWLayout, GLWEInfos,
|
||||
GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyLayout, GLWESwitchingKeyPreparedFactory,
|
||||
GLWETensorKeyLayout,
|
||||
prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared},
|
||||
},
|
||||
@@ -180,7 +180,24 @@ where
|
||||
) + 0.5
|
||||
};
|
||||
|
||||
ggsw_out.assert_noise(module, &sk_out_prepared, &pt_scalar, &max_noise);
|
||||
for row in 0..ggsw_out.dnum().as_usize() {
|
||||
for col in 0..ggsw_out.rank().as_usize() + 1 {
|
||||
assert!(
|
||||
ggsw_out
|
||||
.noise(
|
||||
module,
|
||||
row,
|
||||
col,
|
||||
&pt_scalar,
|
||||
&sk_out_prepared,
|
||||
scratch.borrow()
|
||||
)
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise(col)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -330,7 +347,24 @@ where
|
||||
) + 0.5
|
||||
};
|
||||
|
||||
ggsw_out.assert_noise(module, &sk_out_prepared, &pt_scalar, &max_noise);
|
||||
for row in 0..ggsw_out.dnum().as_usize() {
|
||||
for col in 0..ggsw_out.rank().as_usize() + 1 {
|
||||
assert!(
|
||||
ggsw_out
|
||||
.noise(
|
||||
module,
|
||||
row,
|
||||
col,
|
||||
&pt_scalar,
|
||||
&sk_out_prepared,
|
||||
scratch.borrow()
|
||||
)
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise(col)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,7 +140,13 @@ where
|
||||
|
||||
module.glwe_normalize(&mut pt_out, &pt_in, scratch.borrow());
|
||||
|
||||
glwe_out.assert_noise(module, &sk_out_prepared, &pt_out, max_noise + 0.5);
|
||||
assert!(
|
||||
glwe_out
|
||||
.noise(module, &pt_out, &sk_out_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise + 1.0
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -260,7 +266,13 @@ where
|
||||
.sqrt()
|
||||
.log2();
|
||||
|
||||
glwe_out.assert_noise(module, &sk_out_prepared, &pt_want, max_noise + 0.5);
|
||||
assert!(
|
||||
glwe_out
|
||||
.noise(module, &pt_want, &sk_out_prepared, scratch.borrow())
|
||||
.std()
|
||||
.log2()
|
||||
<= max_noise + 1.0
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,10 @@ pub mod keyswitch;
|
||||
|
||||
mod conversion;
|
||||
mod glwe_packer;
|
||||
mod glwe_packing;
|
||||
mod trace;
|
||||
|
||||
pub use conversion::*;
|
||||
pub use glwe_packer::*;
|
||||
pub use glwe_packing::*;
|
||||
pub use trace::*;
|
||||
|
||||
@@ -32,26 +32,27 @@ where
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||
{
|
||||
let base2k: usize = 8;
|
||||
let base2k_out: usize = 15;
|
||||
let base2k_key: usize = 10;
|
||||
let k: usize = 54;
|
||||
|
||||
for rank in 1_usize..3 {
|
||||
let n: usize = module.n();
|
||||
let k_autokey: usize = k + base2k;
|
||||
let k_autokey: usize = k + base2k_key;
|
||||
|
||||
let dsize: usize = 1;
|
||||
let dnum: usize = k.div_ceil(base2k * dsize);
|
||||
let dnum: usize = k.div_ceil(base2k_key * dsize);
|
||||
|
||||
let glwe_out_infos: GLWELayout = GLWELayout {
|
||||
n: n.into(),
|
||||
base2k: base2k.into(),
|
||||
base2k: base2k_out.into(),
|
||||
k: k.into(),
|
||||
rank: rank.into(),
|
||||
};
|
||||
|
||||
let key_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout {
|
||||
n: n.into(),
|
||||
base2k: base2k.into(),
|
||||
base2k: base2k_key.into(),
|
||||
k: k_autokey.into(),
|
||||
rank: rank.into(),
|
||||
dsize: dsize.into(),
|
||||
@@ -85,7 +86,7 @@ where
|
||||
.iter_mut()
|
||||
.for_each(|x| *x = source_xa.next_i64() & 0xFF);
|
||||
|
||||
module.vec_znx_fill_uniform(base2k, &mut pt_have.data, 0, &mut source_xa);
|
||||
module.vec_znx_fill_uniform(base2k_out, &mut pt_have.data, 0, &mut source_xa);
|
||||
|
||||
glwe_out.encrypt_sk(
|
||||
module,
|
||||
@@ -121,13 +122,18 @@ where
|
||||
glwe_out.decrypt(module, &mut pt_have, &sk_dft, scratch.borrow());
|
||||
|
||||
module.vec_znx_sub_inplace(&mut pt_want.data, 0, &pt_have.data, 0);
|
||||
module.vec_znx_normalize_inplace(base2k, &mut pt_want.data, 0, scratch.borrow());
|
||||
module.vec_znx_normalize_inplace(
|
||||
pt_want.base2k().as_usize(),
|
||||
&mut pt_want.data,
|
||||
0,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
let noise_have: f64 = pt_want.stats().std().log2();
|
||||
|
||||
let mut noise_want: f64 = var_noise_gglwe_product(
|
||||
n as f64,
|
||||
base2k,
|
||||
base2k_key * dsize,
|
||||
0.5,
|
||||
0.5,
|
||||
1.0 / 12.0,
|
||||
|
||||
Reference in New Issue
Block a user