gglwe tests

This commit is contained in:
Rasoul Akhavan Mahdavi
2025-10-19 21:13:52 -04:00
parent a706b759d6
commit 1ba54b68c7
7 changed files with 113 additions and 215 deletions

View File

@@ -1,66 +1,32 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare,
VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform,
VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace,
VecZnxSubScalarInplace, VecZnxSwitchRing, VmpPMatAlloc, VmpPrepare,
},
layouts::{Backend, Module, ScratchOwned},
oep::{
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl,
ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform,
},
layouts::{Backend, Module, Scratch, ScratchOwned},
source::Source,
};
use crate::{
encryption::SIGMA,
layouts::{
GGLWECiphertextLayout, GLWESecret, GLWESwitchingKey,
compressed::{Decompress, GLWESwitchingKeyCompressed},
prepared::{GLWESecretPrepared, PrepareAlloc},
},
decryption::GLWEDecrypt, encryption::SIGMA, layouts::{
prepared::{GGLWEPrepare, GGLWEPreparedAlloc, GLWESecretPrepared}, GGLWELayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress
}, noise::GGLWENoise, GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore
};
pub fn test_gglwe_switching_key_encrypt_sk<B>(module: &Module<B>)
pub fn test_gglwe_switching_key_encrypt_sk<BE: Backend>(module: &Module<BE>)
where
Module<B>: VecZnxDftBytesOf
+ VecZnxBigNormalize<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxNormalizeTmpBytes
Module<BE>: GGLWEEncryptSk<BE>
+ GGLWEPrepare<BE>
+ GGLWEPreparedAlloc<BE>
+ GGLWEKeyswitch<BE>
+ GLWEDecrypt<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>
+ GLWESwitchingKeyAlloc
+ GLWESwitchingKeyEncryptSk<BE>
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxSwitchRing
+ VecZnxAddScalarInplace
+ VecZnxBigBytesOf
+ VecZnxBigAddInplace<B>
+ VecZnxSubScalarInplace
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPrepare<B>,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>
+ VecZnxDftAllocBytesImpl<B>
+ VecZnxBigAllocBytesImpl<B>
+ TakeSvpPPolImpl<B>,
+ GGLWENoise<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 12;
let k_ksk: usize = 54;
@@ -71,7 +37,7 @@ where
let n: usize = module.n();
let dnum: usize = (k_ksk - di * base2k) / (di * base2k);
let gglwe_infos: GGLWECiphertextLayout = GGLWECiphertextLayout {
let gglwe_infos: GGLWELayout = GGLWELayout {
n: n.into(),
base2k: base2k.into(),
k: k_ksk.into(),
@@ -81,23 +47,24 @@ where
rank_out: rank_out.into(),
};
let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos);
let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(module, &gglwe_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<B> = ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_tmp_bytes(
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_tmp_bytes(
module,
&gglwe_infos,
));
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_in.into());
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(module, 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());
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(module, rank_out.into());
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = 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,
@@ -115,44 +82,23 @@ where
}
}
pub fn test_gglwe_switching_key_compressed_encrypt_sk<B>(module: &Module<B>)
pub fn test_gglwe_switching_key_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>)
where
Module<B>: VecZnxDftBytesOf
+ VecZnxBigNormalize<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxSwitchRing
+ VecZnxAddScalarInplace
+ VecZnxBigBytesOf
+ VecZnxBigAddInplace<B>
+ VecZnxSubScalarInplace
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPrepare<B>,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>
+ VecZnxDftAllocBytesImpl<B>
+ VecZnxBigAllocBytesImpl<B>
+ TakeSvpPPolImpl<B>,
Module<BE>: GGLWEEncryptSk<BE>
+ GGLWEPrepare<BE>
+ GGLWEPreparedAlloc<BE>
+ GGLWEKeyswitch<BE>
+ GLWEDecrypt<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>
+ GLWESwitchingKeyAlloc
+ GLWESwitchingKeyEncryptSk<BE>
+ GLWESwitchingKeyCompressedEncryptSk<BE>
+ GLWESwitchingKeyDecompress
+ GGLWENoise<BE>
+ VecZnxFillUniform,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 12;
let k_ksk: usize = 54;
@@ -163,7 +109,7 @@ where
let n: usize = module.n();
let dnum: usize = (k_ksk - di * base2k) / (di * base2k);
let gglwe_infos: GGLWECiphertextLayout = GGLWECiphertextLayout {
let gglwe_infos: GGLWELayout = GGLWELayout {
n: n.into(),
base2k: base2k.into(),
k: k_ksk.into(),
@@ -174,22 +120,23 @@ where
};
let mut ksk_compressed: GLWESwitchingKeyCompressed<Vec<u8>> =
GLWESwitchingKeyCompressed::alloc_from_infos(&gglwe_infos);
GLWESwitchingKeyCompressed::alloc_from_infos(module, &gglwe_infos);
let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWESwitchingKeyCompressed::encrypt_sk_tmp_bytes(
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(GLWESwitchingKeyCompressed::encrypt_sk_tmp_bytes(
module,
&gglwe_infos,
));
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_in.into());
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(module, 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());
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(module, rank_out.into());
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = 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);
let seed_xa = [1u8; 32];
@@ -202,7 +149,7 @@ where
scratch.borrow(),
);
let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos);
let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(module, &gglwe_infos);
ksk.decompress(module, &ksk_compressed);
ksk.key

View File

@@ -16,17 +16,17 @@ use crate::{
},
};
pub fn test_glwe_encrypt_sk<B: Backend>(module: &Module<B>)
pub fn test_glwe_encrypt_sk<BE: Backend>(module: &Module<BE>)
where
Module<B>: GLWEAlloc
+ GLWEEncryptSk<B>
+ GLWEDecrypt<B>
+ GLWESecretPreparedAlloc<B>
+ GLWESecretPrepare<B>
Module<BE>: GLWEAlloc
+ GLWEEncryptSk<BE>
+ GLWEDecrypt<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>
+ VecZnxFillUniform
+ GLWESub,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
Scratch<B>: ScratchAvailable + ScratchTakeCore<B>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 8;
let k_ct: usize = 54;
@@ -56,13 +56,13 @@ where
let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> =
let mut scratch: ScratchOwned<BE> =
ScratchOwned::alloc(GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos));
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &glwe_infos);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_prepared: GLWESecretPrepared<Vec<u8>, B> = GLWESecretPrepared::alloc(module, rank.into());
let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_prepared.prepare(module, &sk);
module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);
@@ -87,24 +87,24 @@ where
}
}
pub fn test_glwe_compressed_encrypt_sk<B: Backend>(module: &Module<B>)
pub fn test_glwe_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>)
where
Module<B>: GLWEAlloc
+ GLWECompressedEncryptSk<B>
+ GLWEDecrypt<B>
+ GLWESecretPreparedAlloc<B>
+ GLWESecretPrepare<B>
Module<BE>: GLWEAlloc
+ GLWECompressedEncryptSk<BE>
+ GLWEDecrypt<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>
+ VecZnxFillUniform
+ GLWESub,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
Scratch<B>: ScratchAvailable + ScratchTakeCore<B>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 8;
let k_ct: usize = 54;
let k_pt: usize = 30;
for rank in 1_usize..3 {
println!("rank: {}", rank);
// println!("rank: {}", rank);
let n: usize = module.n();
let glwe_infos: GLWELayout = GLWELayout {
@@ -129,14 +129,14 @@ where
let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
GLWECompressed::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos),
);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &glwe_infos);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_prepared: GLWESecretPrepared<Vec<u8>, B> = GLWESecretPrepared::alloc(module, rank.into());
let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_prepared.prepare(module, &sk);
module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);
@@ -170,17 +170,17 @@ where
}
}
pub fn test_glwe_encrypt_zero_sk<B: Backend>(module: &Module<B>)
pub fn test_glwe_encrypt_zero_sk<BE: Backend>(module: &Module<BE>)
where
Module<B>: GLWEAlloc
+ GLWEEncryptSk<B>
+ GLWEDecrypt<B>
+ GLWESecretPreparedAlloc<B>
+ GLWESecretPrepare<B>
Module<BE>: GLWEAlloc
+ GLWEEncryptSk<BE>
+ GLWEDecrypt<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>
+ VecZnxFillUniform
+ GLWESub,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
Scratch<B>: ScratchAvailable + ScratchTakeCore<B>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 8;
let k_ct: usize = 54;
@@ -201,13 +201,13 @@ where
let mut source_xe: Source = Source::new([1u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> =
let mut scratch: ScratchOwned<BE> =
ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos));
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &glwe_infos);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_prepared: GLWESecretPrepared<Vec<u8>, B> = GLWESecretPrepared::alloc(module, rank.into());
let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_prepared.prepare(module, &sk);
let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(module, &glwe_infos);
@@ -225,20 +225,20 @@ where
}
}
pub fn test_glwe_encrypt_pk<B: Backend>(module: &Module<B>)
pub fn test_glwe_encrypt_pk<BE: Backend>(module: &Module<BE>)
where
Module<B>: GLWEAlloc
+ GLWEEncryptPk<B>
+ GLWEPublicKeyPrepare<B>
+ GLWEPublicKeyPreparedAlloc<B>
+ GLWEPublicKeyGenerate<B>
+ GLWEDecrypt<B>
+ GLWESecretPreparedAlloc<B>
+ GLWESecretPrepare<B>
Module<BE>: GLWEAlloc
+ GLWEEncryptPk<BE>
+ GLWEPublicKeyPrepare<BE>
+ GLWEPublicKeyPreparedAlloc<BE>
+ GLWEPublicKeyGenerate<BE>
+ GLWEDecrypt<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>
+ VecZnxFillUniform
+ GLWESub,
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
Scratch<B>: ScratchAvailable + ScratchTakeCore<B>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 8;
let k_ct: usize = 54;
@@ -262,13 +262,13 @@ where
let mut source_xa: Source = Source::new([0u8; 32]);
let mut source_xu: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> =
let mut scratch: ScratchOwned<BE> =
ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_pk_tmp_bytes(module, &glwe_infos));
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &glwe_infos);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_prepared: GLWESecretPrepared<Vec<u8>, B> = GLWESecretPrepared::alloc(module, rank.into());
let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_prepared.prepare(module, &sk);
let mut pk: GLWEPublicKey<Vec<u8>> = GLWEPublicKey::alloc_from_infos(module, &glwe_infos);
@@ -276,7 +276,7 @@ where
module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);
let mut pk_prepared: GLWEPublicKeyPrepared<Vec<u8>, B> = GLWEPublicKeyPrepared::alloc_from_infos(module, &glwe_infos);
let mut pk_prepared: GLWEPublicKeyPrepared<Vec<u8>, BE> = GLWEPublicKeyPrepared::alloc_from_infos(module, &glwe_infos);
pk_prepared.prepare(module, &pk);
ct.encrypt_pk(

View File

@@ -1,11 +1,11 @@
// mod gglwe_atk;
// mod gglwe_ct;
mod gglwe_ct;
// mod ggsw_ct;
mod glwe_ct;
// mod glwe_tsk;
// pub use gglwe_atk::*;
// pub use gglwe_ct::*;
pub use gglwe_ct::*;
// pub use ggsw_ct::*;
pub use glwe_ct::*;
// pub use glwe_tsk::*;