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:
@@ -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