keyswitch tests

This commit is contained in:
Pro7ech
2025-10-20 15:32:52 +02:00
parent 0c894c19db
commit 252eda36fe
60 changed files with 918 additions and 945 deletions

View File

@@ -5,7 +5,8 @@ use poulpy_hal::{
};
use crate::{
GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore,
AutomorphismKeyCompressedEncryptSk, AutomorphismKeyEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk,
GLWESwitchingKeyEncryptSk, ScratchTakeCore,
encryption::SIGMA,
layouts::{
AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, GLWESecret, GLWESecretPrepare,
@@ -16,7 +17,7 @@ use crate::{
pub fn test_gglwe_automorphism_key_encrypt_sk<BE: Backend>(module: &Module<BE>)
where
Module<BE>: GGLWEEncryptSk<BE>
Module<BE>: AutomorphismKeyEncryptSk<BE>
+ GGLWEKeyswitch<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>
@@ -82,7 +83,6 @@ where
sk_out_prepared.prepare(module, &sk_out);
atk.key
.key
.assert_noise(module, &sk_out_prepared, &sk.data, SIGMA);
}
}
@@ -90,7 +90,7 @@ where
pub fn test_gglwe_automorphism_key_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>)
where
Module<BE>: GGLWEEncryptSk<BE>
Module<BE>: AutomorphismKeyCompressedEncryptSk<BE>
+ GGLWEKeyswitch<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>
@@ -125,7 +125,9 @@ where
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(AutomorphismKey::encrypt_sk_tmp_bytes(module, &atk_infos));
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(
module, &atk_infos,
));
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&atk_infos);
sk.fill_ternary_prob(0.5, &mut source_xs);
@@ -153,7 +155,6 @@ where
atk.decompress(module, &atk_compressed);
atk.key
.key
.assert_noise(module, &sk_out_prepared, &sk.data, SIGMA);
}
}

View File

@@ -31,7 +31,7 @@ where
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let n = module.n();
let n: usize = module.n();
let base2k: usize = 12;
let k_ksk: usize = 54;
let dsize: usize = k_ksk / base2k;

View File

@@ -4,7 +4,7 @@ use poulpy_hal::{
VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxSubScalarInplace,
VecZnxSwitchRing,
},
layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxDft},
layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxBig, VecZnxDft},
source::Source,
};
@@ -73,8 +73,8 @@ where
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos);
let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1);
let mut sk_ij_big = module.vec_znx_big_alloc(1, 1);
let mut sk_ij_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(1, 1);
let mut sk_ij_big: VecZnxBig<Vec<u8>, BE> = module.vec_znx_big_alloc(1, 1);
let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), 1_u32.into());
let mut sk_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(rank, 1);
@@ -126,8 +126,6 @@ where
+ VecZnxFillUniform
+ VecZnxCopy
+ VecZnxSwitchRing,
// + VecZnxNormalizeInplaceImpl<BE>
// + VecZnxNormalizeImpl<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
@@ -170,8 +168,8 @@ where
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos);
let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1);
let mut sk_ij_big = module.vec_znx_big_alloc(1, 1);
let mut sk_ij_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(1, 1);
let mut sk_ij_big: VecZnxBig<Vec<u8>, BE> = module.vec_znx_big_alloc(1, 1);
let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), 1_u32.into());
let mut sk_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(rank, 1);

View File

@@ -9,7 +9,8 @@ use crate::{
encryption::SIGMA,
layouts::{
GGSW, GGSWLayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyLayout,
GLWESwitchingKeyPreparedAlloc, TensorKey, TensorKeyLayout, TensorKeyPreparedAlloc,
GLWESwitchingKeyPrepare, GLWESwitchingKeyPreparedAlloc, TensorKey, TensorKeyLayout, TensorKeyPrepare,
TensorKeyPreparedAlloc,
prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared},
},
noise::noise_ggsw_keyswitch,
@@ -26,7 +27,9 @@ where
+ GLWESecretPrepare<BE>
+ TensorKeyPreparedAlloc<BE>
+ GLWESwitchingKeyPreparedAlloc<BE>
+ GGSWNoise<BE>,
+ GGSWNoise<BE>
+ GLWESwitchingKeyPrepare<BE>
+ TensorKeyPrepare<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
@@ -145,8 +148,12 @@ where
scratch.borrow(),
);
let ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> = GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
let tsk_prepared: TensorKeyPrepared<Vec<u8>, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk);
let mut ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> =
GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
ksk_prepared.prepare(module, &ksk, scratch.borrow());
let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk);
tsk_prepared.prepare(module, &tsk, scratch.borrow());
ggsw_out.keyswitch(
module,
@@ -188,7 +195,9 @@ where
+ GLWESecretPrepare<BE>
+ TensorKeyPreparedAlloc<BE>
+ GLWESwitchingKeyPreparedAlloc<BE>
+ GGSWNoise<BE>,
+ GGSWNoise<BE>
+ GLWESwitchingKeyPrepare<BE>
+ TensorKeyPrepare<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
@@ -259,6 +268,7 @@ where
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into());
sk_in.fill_ternary_prob(var_xs, &mut source_xs);
let mut sk_in_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_in_prepared.prepare(module, &sk_in);
@@ -295,8 +305,12 @@ where
scratch.borrow(),
);
let ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> = GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
let tsk_prepared: TensorKeyPrepared<Vec<u8>, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk);
let mut ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> =
GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
ksk_prepared.prepare(module, &ksk, scratch.borrow());
let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk);
tsk_prepared.prepare(module, &tsk, scratch.borrow());
ggsw_out.keyswitch_inplace(module, &ksk_prepared, &tsk_prepared, scratch.borrow());

View File

@@ -1,14 +1,15 @@
use poulpy_hal::{
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow},
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform},
layouts::{Backend, Module, Scratch, ScratchOwned},
source::Source,
};
use crate::{
ScratchTakeCore,
GLWEEncryptSk, GLWEKeyswitch, GLWENoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore,
encryption::SIGMA,
layouts::{
GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout,
GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey,
GLWESwitchingKeyLayout, GLWESwitchingKeyPrepare, GLWESwitchingKeyPreparedAlloc,
prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared},
},
noise::log2_std_noise_gglwe_product,
@@ -17,7 +18,15 @@ use crate::{
#[allow(clippy::too_many_arguments)]
pub fn test_glwe_keyswitch<BE: Backend>(module: &Module<BE>)
where
Module<BE>:,
Module<BE>: VecZnxFillUniform
+ GLWESwitchingKeyEncryptSk<BE>
+ GLWEEncryptSk<BE>
+ GLWEKeyswitch<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>
+ GLWESwitchingKeyPrepare<BE>
+ GLWESwitchingKeyPreparedAlloc<BE>
+ GLWENoise<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
@@ -48,7 +57,7 @@ where
rank: rank_out.into(),
};
let key_apply: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
let ksk: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(),
base2k: base2k.into(),
k: k_ksk.into(),
@@ -58,7 +67,7 @@ where
rank_out: rank_out.into(),
};
let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&key_apply);
let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&ksk);
let mut glwe_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_in_infos);
let mut glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_in_infos);
@@ -70,18 +79,22 @@ where
module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply)
GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk)
| GLWE::encrypt_sk_tmp_bytes(module, &glwe_in_infos)
| GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &key_apply),
| GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &ksk),
);
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 sk_in_prepared: GLWESecretPrepared<Vec<u8>, BE> = sk_in.prepare_alloc(module, scratch.borrow());
let mut sk_in_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank_in.into());
sk_in_prepared.prepare(module, &sk_in);
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 sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = sk_out.prepare_alloc(module, scratch.borrow());
let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank_out.into());
sk_out_prepared.prepare(module, &sk_out);
ksk.encrypt_sk(
module,
@@ -101,7 +114,9 @@ where
scratch.borrow(),
);
let ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> = ksk.prepare_alloc(module, scratch.borrow());
let mut ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> =
GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
ksk_prepared.prepare(module, &ksk, scratch.borrow());
glwe_out.keyswitch(module, &glwe_in, &ksk_prepared, scratch.borrow());
@@ -126,7 +141,15 @@ where
pub fn test_glwe_keyswitch_inplace<BE: Backend>(module: &Module<BE>)
where
Module<BE>:,
Module<BE>: VecZnxFillUniform
+ GLWESwitchingKeyEncryptSk<BE>
+ GLWEEncryptSk<BE>
+ GLWEKeyswitch<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>
+ GLWESwitchingKeyPrepare<BE>
+ GLWESwitchingKeyPreparedAlloc<BE>
+ GLWENoise<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
@@ -148,7 +171,7 @@ where
rank: rank.into(),
};
let key_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
let ksk_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(),
base2k: base2k.into(),
k: k_ksk.into(),
@@ -158,7 +181,7 @@ where
rank_out: rank.into(),
};
let mut key_apply: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&key_apply_infos);
let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&ksk_infos);
let mut glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
@@ -169,20 +192,24 @@ where
module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply_infos)
GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_infos)
| GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos)
| GLWE::keyswitch_inplace_tmp_bytes(module, &glwe_out_infos, &key_apply_infos),
| GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_out_infos, &ksk_infos),
);
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into());
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let sk_in_prepared: GLWESecretPrepared<Vec<u8>, BE> = sk_in.prepare_alloc(module, scratch.borrow());
let mut sk_in_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_in_prepared.prepare(module, &sk_in);
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into());
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = sk_out.prepare_alloc(module, scratch.borrow());
key_apply.encrypt_sk(
let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_out_prepared.prepare(module, &sk_out);
ksk.encrypt_sk(
module,
&sk_in,
&sk_out,
@@ -200,7 +227,9 @@ where
scratch.borrow(),
);
let ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> = key_apply.prepare_alloc(module, scratch.borrow());
let mut ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> =
GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
ksk_prepared.prepare(module, &ksk, scratch.borrow());
glwe_out.keyswitch_inplace(module, &ksk_prepared, scratch.borrow());

View File

@@ -5,15 +5,21 @@ use poulpy_hal::{
};
use crate::{
ScratchTakeCore,
LWEDecrypt, LWEEncryptSk, LWEKeySwitch, LWESwitchingKeyEncrypt, ScratchTakeCore,
layouts::{
LWE, LWELayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, prepared::LWESwitchingKeyPrepared,
LWE, LWELayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, LWESwitchingKeyPrepare,
LWESwitchingKeyPreparedAlloc, prepared::LWESwitchingKeyPrepared,
},
};
pub fn test_lwe_keyswitch<BE: Backend>(module: &Module<BE>)
where
Module<BE>:,
Module<BE>: LWEKeySwitch<BE>
+ LWESwitchingKeyEncrypt<BE>
+ LWEEncryptSk<BE>
+ LWESwitchingKeyPreparedAlloc<BE>
+ LWEDecrypt<BE>
+ LWESwitchingKeyPrepare<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
@@ -89,7 +95,8 @@ where
let mut lwe_ct_out: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_out_infos);
let ksk_prepared: LWESwitchingKeyPrepared<Vec<u8>, BE> = ksk.prepare_alloc(module, scratch.borrow());
let mut ksk_prepared: LWESwitchingKeyPrepared<Vec<u8>, BE> = LWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
ksk_prepared.prepare(module, &ksk, scratch.borrow());
lwe_ct_out.keyswitch(module, &lwe_ct_in, &ksk_prepared, scratch.borrow());