Fix seeded glwe encryption

This commit is contained in:
Pro7ech
2025-11-18 23:57:24 +01:00
parent 2fb1627613
commit 59a1b6616a
11 changed files with 117 additions and 47 deletions

View File

@@ -275,8 +275,6 @@ where
let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
glwe_pt.encode_vec_i64(&data, k_lwe_pt);
println!("glwe_pt: {glwe_pt}");
let mut glwe_ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
glwe_ct.encrypt_sk(
module,

View File

@@ -176,7 +176,6 @@ where
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

View File

@@ -5,12 +5,13 @@ use poulpy_hal::{
};
use crate::{
GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore,
GGLWECompressedEncryptSk, GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk,
ScratchTakeCore,
decryption::GLWEDecrypt,
encryption::SIGMA,
layouts::{
GGLWEInfos, GGLWELayout, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyCompressed,
GLWESwitchingKeyDecompress, LWEInfos,
GGLWE, GGLWECompressed, GGLWEInfos, GGLWELayout, GLWESecret, GLWESecretPreparedFactory,
GLWESwitchingKey, GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress, LWEInfos,
prepared::{GGLWEPreparedFactory, GLWESecretPrepared},
},
noise::GGLWENoise,
@@ -121,18 +122,18 @@ where
let n: usize = module.n();
let base2k: usize = 12;
let k_ksk: usize = 54;
let dsize: usize = k_ksk / base2k;
let max_dsize: usize = k_ksk / base2k;
for rank_in in 1_usize..3 {
for rank_out in 1_usize..3 {
for di in 1_usize..dsize + 1 {
let dnum: usize = (k_ksk - di * base2k) / (di * base2k);
for dsize in 1_usize..max_dsize {
let dnum: usize = (k_ksk - dsize * base2k) / (dsize * base2k);
let gglwe_infos: GGLWELayout = GGLWELayout {
n: n.into(),
base2k: base2k.into(),
k: k_ksk.into(),
dnum: dnum.into(),
dsize: di.into(),
dsize: dsize.into(),
rank_in: rank_in.into(),
rank_out: rank_out.into(),
};
@@ -198,3 +199,95 @@ where
}
}
}
pub fn test_gglwe_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>)
where
Module<BE>: GGLWEEncryptSk<BE>
+ GGLWEPreparedFactory<BE>
+ GGLWEKeyswitch<BE>
+ GLWEDecrypt<BE>
+ GLWESecretPreparedFactory<BE>
+ GLWESwitchingKeyEncryptSk<BE>
+ GGLWECompressedEncryptSk<BE>
+ GLWESwitchingKeyDecompress
+ GGLWENoise<BE>
+ VecZnxFillUniform,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let n: usize = module.n();
let base2k: usize = 12;
let k_ksk: usize = 54;
let max_dsize: usize = k_ksk / base2k;
for rank_in in 1_usize..3 {
for rank_out in 1_usize..3 {
for dsize in 1_usize..max_dsize + 1 {
let dnum: usize = (k_ksk - dsize * base2k) / (dsize * base2k);
let gglwe_infos: GGLWELayout = GGLWELayout {
n: n.into(),
base2k: base2k.into(),
k: k_ksk.into(),
dnum: dnum.into(),
dsize: dsize.into(),
rank_in: rank_in.into(),
rank_out: rank_out.into(),
};
let mut ksk_compressed: GGLWECompressed<Vec<u8>> = GGLWECompressed::alloc_from_infos(&gglwe_infos);
let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<BE> =
ScratchOwned::alloc(GGLWECompressed::encrypt_sk_tmp_bytes(module, &gglwe_infos));
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_in.into());
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into());
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank_out.into());
sk_out_prepared.prepare(module, &sk_out);
let seed_xa = [1u8; 32];
ksk_compressed.encrypt_sk(
module,
&sk_in.data,
&sk_out_prepared,
seed_xa,
&mut source_xe,
scratch.borrow(),
);
let mut ksk: GGLWE<Vec<u8>> = GGLWE::alloc_from_infos(&gglwe_infos);
ksk.decompress(module, &ksk_compressed);
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
.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
);
}
}
}
}
}
}

View File

@@ -10,8 +10,8 @@ use crate::{
encryption::SIGMA,
layouts::{
Dsize, GGLWE, GGLWEDecompress, GGLWEInfos, GGLWEToGGSWKey, GGLWEToGGSWKeyCompressed, GGLWEToGGSWKeyDecompress,
GGLWEToGGSWKeyLayout, GLWESecret, GLWESecretPreparedFactory, GLWESecretTensor, GLWESecretTensorFactory,
LWEInfos, prepared::GLWESecretPrepared,
GGLWEToGGSWKeyLayout, GLWESecret, GLWESecretPreparedFactory, GLWESecretTensor, GLWESecretTensorFactory, LWEInfos,
prepared::GLWESecretPrepared,
},
};

View File

@@ -9,7 +9,7 @@ use crate::{
decryption::GLWEDecrypt,
encryption::SIGMA,
layouts::{
Dsize, GGLWEDecompress, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPreparedFactory, GLWESecretTensor,
Dsize, GGLWEDecompress, GGLWEInfos, GLWESecret, GLWESecretPreparedFactory, GLWESecretTensor,
GLWESecretTensorFactory, GLWETensorKey, GLWETensorKeyCompressed, GLWETensorKeyLayout, LWEInfos,
prepared::GLWESecretPrepared,
},
@@ -71,7 +71,7 @@ where
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 {
for col in 0..tensor_key.rank_in().as_usize() {
assert!(
tensor_key
.0
@@ -148,7 +148,7 @@ where
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 {
for col in 0..tensor_key.rank_in().as_usize() {
assert!(
tensor_key
.0

View File

@@ -8,7 +8,7 @@ use crate::{
GGLWEExternalProduct, GGLWENoise, GGSWEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore,
encryption::SIGMA,
layouts::{
GGLWEInfos, GGSW, GGSWLayout, GGSWPreparedFactory, GLWEInfos, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey,
GGLWEInfos, GGSW, GGSWLayout, GGSWPreparedFactory, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey,
GLWESwitchingKeyLayout,
prepared::{GGSWPrepared, GLWESecretPrepared},
},