glwe tsk test

This commit is contained in:
Rasoul Akhavan Mahdavi
2025-10-20 00:40:43 -04:00
parent d68c0a2baf
commit 60fbd3e625
4 changed files with 67 additions and 120 deletions

View File

@@ -3,7 +3,7 @@ use poulpy_hal::{
ModuleN, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, ModuleN, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf,
VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes,
}, },
layouts::{Backend, DataMut, DataViewMut, Module, Scratch}, layouts::{Backend, DataRef, DataViewMut, Module, Scratch},
}; };
use crate::layouts::{ use crate::layouts::{
@@ -21,8 +21,8 @@ impl GLWE<Vec<u8>> {
} }
} }
impl<DataSelf: DataMut> GLWE<DataSelf> { impl<DataSelf: DataRef> GLWE<DataSelf> {
pub fn decrypt<P, S, M, BE: Backend>(&mut self, module: &M, pt: &mut P, sk: &S, scratch: &mut Scratch<BE>) pub fn decrypt<P, S, M, BE: Backend>(&self, module: &M, pt: &mut P, sk: &S, scratch: &mut Scratch<BE>)
where where
P: GLWEPlaintextToMut, P: GLWEPlaintextToMut,
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,

View File

@@ -34,7 +34,7 @@ gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_ggl
gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk,
gglwe_automorphism_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_encrypt_sk, gglwe_automorphism_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_encrypt_sk,
gglwe_automorphism_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_compressed_encrypt_sk, gglwe_automorphism_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_compressed_encrypt_sk,
// gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk,
// gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, // gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk,
// GGLWE Keyswitching // GGLWE Keyswitching
// gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, // gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch,

View File

@@ -1,68 +1,31 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, VecZnxBigAlloc, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxSubScalarInplace, VecZnxSwitchRing
SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, }, layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxDft}, oep::{VecZnxNormalizeImpl, VecZnxNormalizeInplaceImpl}, source::Source
VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf,
VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace,
VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing,
},
layouts::{Backend, Module, ScratchOwned, VecZnxDft},
oep::{
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl,
},
source::Source,
}; };
use crate::{ use crate::{
encryption::SIGMA, decryption::GLWEDecrypt, encryption::SIGMA, layouts::{
layouts::{ prepared::GLWESecretPrepared, Dsize, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, TensorKey, TensorKeyAlloc, TensorKeyCompressed, TensorKeyLayout
Dsize, GLWEPlaintext, GLWESecret, TensorKey, TensorKeyLayout, }, GGLWETensorKeyCompressedEncryptSk, ScratchTakeCore, TensorKeyEncryptSk
compressed::{Decompress, TensorKeyCompressed},
prepared::{GLWESecretPrepared, PrepareAlloc},
},
}; };
pub fn test_gglwe_tensor_key_encrypt_sk<B>(module: &Module<B>) pub fn test_gglwe_tensor_key_encrypt_sk<BE: Backend>(module: &Module<BE>)
where where
Module<B>: VecZnxDftBytesOf Module<BE>: TensorKeyEncryptSk<BE>
+ VecZnxBigNormalize<B> + TensorKeyAlloc
+ VecZnxDftApply<B> + GLWESecretPrepare<BE>
+ SvpApplyDftToDftInplace<B> + GLWESecretPreparedAlloc<BE>
+ VecZnxIdftApplyConsume<B> + GLWEDecrypt<BE>
+ VecZnxNormalizeTmpBytes + VecZnxDftAlloc<BE>
+ VecZnxFillUniform + VecZnxBigAlloc<BE>
+ VecZnxSubInplace + VecZnxDftApply<BE>
+ VecZnxAddInplace + SvpApplyDftToDft<BE>
+ VecZnxNormalizeInplace<B> + VecZnxIdftApplyTmpA<BE>
+ VecZnxAddNormal + VecZnxBigNormalize<BE>
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxBigBytesOf
+ VecZnxBigAddInplace<B>
+ VecZnxCopy
+ VecZnxDftAlloc<B>
+ SvpApplyDftToDft<B>
+ VecZnxBigAlloc<B>
+ VecZnxIdftApplyTmpA<B>
+ VecZnxAddScalarInplace
+ VecZnxSwitchRing
+ VecZnxSubScalarInplace, + VecZnxSubScalarInplace,
B: Backend ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
+ TakeVecZnxDftImpl<B> Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
+ TakeVecZnxBigImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>
+ VecZnxDftAllocBytesImpl<B>
+ VecZnxBigAllocBytesImpl<B>
+ TakeSvpPPolImpl<B>,
{ {
let base2k: usize = 8; let base2k: usize = 8;
let k: usize = 54; let k: usize = 54;
@@ -80,20 +43,21 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(&tensor_key_infos); let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(module, &tensor_key_infos);
let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: 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 source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes( let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes(
module, module,
&tensor_key_infos, &tensor_key_infos,
)); ));
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&tensor_key_infos); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &tensor_key_infos);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow()); let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_prepared.prepare(module, &sk);
tensor_key.encrypt_sk( tensor_key.encrypt_sk(
module, module,
@@ -103,12 +67,12 @@ where
scratch.borrow(), scratch.borrow(),
); );
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, &tensor_key_infos);
let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); 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_big = module.vec_znx_big_alloc(1, 1);
let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(module, 1_u32.into());
let mut sk_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(rank, 1); let mut sk_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(rank, 1);
for i in 0..rank { for i in 0..rank {
module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i);
@@ -128,10 +92,11 @@ where
scratch.borrow(), scratch.borrow(),
); );
for row_i in 0..dnum { for row_i in 0..dnum {
tensor_key let ct = tensor_key
.at(i, j) .at(i, j)
.at(row_i, 0) .at(row_i, 0);
.decrypt(module, &mut pt, &sk_prepared, scratch.borrow());
ct.decrypt(module, &mut pt, &sk_prepared, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij.data, 0); module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij.data, 0);
@@ -143,46 +108,27 @@ where
} }
} }
pub fn test_gglwe_tensor_key_compressed_encrypt_sk<B>(module: &Module<B>) pub fn test_gglwe_tensor_key_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>)
where where
Module<B>: VecZnxDftBytesOf Module<BE>: TensorKeyEncryptSk<BE>
+ VecZnxBigNormalize<B> + TensorKeyAlloc
+ VecZnxDftApply<B> + GLWESecretPrepare<BE>
+ SvpApplyDftToDftInplace<B> + GLWESecretPreparedAlloc<BE>
+ VecZnxIdftApplyConsume<B> + GGLWETensorKeyCompressedEncryptSk<BE>
+ VecZnxNormalizeTmpBytes + GLWEDecrypt<BE>
+ VecZnxFillUniform + VecZnxDftAlloc<BE>
+ VecZnxSubInplace + VecZnxBigAlloc<BE>
+ VecZnxAddInplace + VecZnxDftApply<BE>
+ VecZnxNormalizeInplace<B> + SvpApplyDftToDft<BE>
+ VecZnxAddNormal + VecZnxIdftApplyTmpA<BE>
+ VecZnxNormalize<B> + VecZnxSubScalarInplace
+ VecZnxSub + VecZnxFillUniform
+ SvpPrepare<B> + VecZnxCopy
+ SvpPPolBytesOf + VecZnxSwitchRing,
+ SvpPPolAlloc<B> // + VecZnxNormalizeInplaceImpl<BE>
+ VecZnxBigAddSmallInplace<B> // + VecZnxNormalizeImpl<BE>,
+ VecZnxBigBytesOf ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
+ VecZnxBigAddInplace<B> Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
+ VecZnxCopy
+ VecZnxDftAlloc<B>
+ SvpApplyDftToDft<B>
+ VecZnxBigAlloc<B>
+ VecZnxIdftApplyTmpA<B>
+ VecZnxAddScalarInplace
+ VecZnxSwitchRing
+ VecZnxSubScalarInplace,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>
+ VecZnxDftAllocBytesImpl<B>
+ VecZnxBigAllocBytesImpl<B>
+ TakeSvpPPolImpl<B>,
{ {
let base2k = 8; let base2k = 8;
let k = 54; let k = 54;
@@ -199,33 +145,34 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut tensor_key_compressed: TensorKeyCompressed<Vec<u8>> = TensorKeyCompressed::alloc_from_infos(&tensor_key_infos); let mut tensor_key_compressed: TensorKeyCompressed<Vec<u8>> = TensorKeyCompressed::alloc_from_infos(module, &tensor_key_infos);
let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(TensorKeyCompressed::encrypt_sk_tmp_bytes( let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(TensorKeyCompressed::encrypt_sk_tmp_bytes(
module, module,
&tensor_key_infos, &tensor_key_infos,
)); ));
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&tensor_key_infos); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &tensor_key_infos);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow()); let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_prepared.prepare(module, &sk);
let seed_xa: [u8; 32] = [1u8; 32]; let seed_xa: [u8; 32] = [1u8; 32];
tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow()); tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow());
let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(&tensor_key_infos); let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(module, &tensor_key_infos);
tensor_key.decompress(module, &tensor_key_compressed); tensor_key.decompress(module, &tensor_key_compressed);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, &tensor_key_infos);
let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); 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_big = module.vec_znx_big_alloc(1, 1);
let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(module, 1_u32.into());
let mut sk_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(rank, 1); let mut sk_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(rank, 1);
for i in 0..rank { for i in 0..rank {
module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i);

View File

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