From 60fbd3e625f2a3284c665e6b26940a224088fbce Mon Sep 17 00:00:00 2001 From: Rasoul Akhavan Mahdavi Date: Mon, 20 Oct 2025 00:40:43 -0400 Subject: [PATCH] glwe tsk test --- poulpy-core/src/decryption/glwe_ct.rs | 6 +- poulpy-core/src/tests/mod.rs | 2 +- .../tests/test_suite/encryption/glwe_tsk.rs | 175 ++++++------------ .../src/tests/test_suite/encryption/mod.rs | 4 +- 4 files changed, 67 insertions(+), 120 deletions(-) diff --git a/poulpy-core/src/decryption/glwe_ct.rs b/poulpy-core/src/decryption/glwe_ct.rs index 3c64fda..6dc7f5a 100644 --- a/poulpy-core/src/decryption/glwe_ct.rs +++ b/poulpy-core/src/decryption/glwe_ct.rs @@ -3,7 +3,7 @@ use poulpy_hal::{ ModuleN, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, }, - layouts::{Backend, DataMut, DataViewMut, Module, Scratch}, + layouts::{Backend, DataRef, DataViewMut, Module, Scratch}, }; use crate::layouts::{ @@ -21,8 +21,8 @@ impl GLWE> { } } -impl GLWE { - pub fn decrypt(&mut self, module: &M, pt: &mut P, sk: &S, scratch: &mut Scratch) +impl GLWE { + pub fn decrypt(&self, module: &M, pt: &mut P, sk: &S, scratch: &mut Scratch) where P: GLWEPlaintextToMut, S: GLWESecretPreparedToRef, diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index b04779e..8bdf034 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -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_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_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 Keyswitching // gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, diff --git a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs index da05ba1..c7f5e2e 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -1,68 +1,31 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, - SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - 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, + ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, VecZnxBigAlloc, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxSubScalarInplace, VecZnxSwitchRing + }, layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxDft}, oep::{VecZnxNormalizeImpl, VecZnxNormalizeInplaceImpl}, source::Source }; use crate::{ - encryption::SIGMA, - layouts::{ - Dsize, GLWEPlaintext, GLWESecret, TensorKey, TensorKeyLayout, - compressed::{Decompress, TensorKeyCompressed}, - prepared::{GLWESecretPrepared, PrepareAlloc}, - }, + decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ + prepared::GLWESecretPrepared, Dsize, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, TensorKey, TensorKeyAlloc, TensorKeyCompressed, TensorKeyLayout + }, GGLWETensorKeyCompressedEncryptSk, ScratchTakeCore, TensorKeyEncryptSk }; -pub fn test_gglwe_tensor_key_encrypt_sk(module: &Module) +pub fn test_gglwe_tensor_key_encrypt_sk(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigAddSmallInplace - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxCopy - + VecZnxDftAlloc - + SvpApplyDftToDft - + VecZnxBigAlloc - + VecZnxIdftApplyTmpA - + VecZnxAddScalarInplace - + VecZnxSwitchRing + Module: TensorKeyEncryptSk + + TensorKeyAlloc + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GLWEDecrypt + + VecZnxDftAlloc + + VecZnxBigAlloc + + VecZnxDftApply + + SvpApplyDftToDft + + VecZnxIdftApplyTmpA + + VecZnxBigNormalize + VecZnxSubScalarInplace, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 8; let k: usize = 54; @@ -80,20 +43,21 @@ where rank: rank.into(), }; - let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(module, &tensor_key_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 = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes( + let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes( module, &tensor_key_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&tensor_key_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); tensor_key.encrypt_sk( module, @@ -103,12 +67,12 @@ where scratch.borrow(), ); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &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: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); - let mut sk_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(rank, 1); + let mut sk_ij: GLWESecret> = GLWESecret::alloc(module, 1_u32.into()); + let mut sk_dft: VecZnxDft, BE> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank { module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); @@ -128,10 +92,11 @@ where scratch.borrow(), ); for row_i in 0..dnum { - tensor_key - .at(i, j) - .at(row_i, 0) - .decrypt(module, &mut pt, &sk_prepared, scratch.borrow()); + let ct = tensor_key + .at(i, j) + .at(row_i, 0); + + 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); @@ -143,46 +108,27 @@ where } } -pub fn test_gglwe_tensor_key_compressed_encrypt_sk(module: &Module) +pub fn test_gglwe_tensor_key_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigAddSmallInplace - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxCopy - + VecZnxDftAlloc - + SvpApplyDftToDft - + VecZnxBigAlloc - + VecZnxIdftApplyTmpA - + VecZnxAddScalarInplace - + VecZnxSwitchRing - + VecZnxSubScalarInplace, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + Module: TensorKeyEncryptSk + + TensorKeyAlloc + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GGLWETensorKeyCompressedEncryptSk + + GLWEDecrypt + + VecZnxDftAlloc + + VecZnxBigAlloc + + VecZnxDftApply + + SvpApplyDftToDft + + VecZnxIdftApplyTmpA + + VecZnxSubScalarInplace + + VecZnxFillUniform + + VecZnxCopy + + VecZnxSwitchRing, + // + VecZnxNormalizeInplaceImpl + // + VecZnxNormalizeImpl, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k = 8; let k = 54; @@ -199,33 +145,34 @@ where rank: rank.into(), }; - let mut tensor_key_compressed: TensorKeyCompressed> = TensorKeyCompressed::alloc_from_infos(&tensor_key_infos); + let mut tensor_key_compressed: TensorKeyCompressed> = TensorKeyCompressed::alloc_from_infos(module, &tensor_key_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKeyCompressed::encrypt_sk_tmp_bytes( + let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKeyCompressed::encrypt_sk_tmp_bytes( module, &tensor_key_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&tensor_key_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); let seed_xa: [u8; 32] = [1u8; 32]; tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow()); - let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(module, &tensor_key_infos); tensor_key.decompress(module, &tensor_key_compressed); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &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: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); - let mut sk_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(rank, 1); + let mut sk_ij: GLWESecret> = GLWESecret::alloc(module, 1_u32.into()); + let mut sk_dft: VecZnxDft, BE> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank { module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); diff --git a/poulpy-core/src/tests/test_suite/encryption/mod.rs b/poulpy-core/src/tests/test_suite/encryption/mod.rs index a23cb64..afc5599 100644 --- a/poulpy-core/src/tests/test_suite/encryption/mod.rs +++ b/poulpy-core/src/tests/test_suite/encryption/mod.rs @@ -2,10 +2,10 @@ mod gglwe_atk; mod gglwe_ct; // mod ggsw_ct; mod glwe_ct; -// mod glwe_tsk; +mod glwe_tsk; pub use gglwe_atk::*; pub use gglwe_ct::*; // pub use ggsw_ct::*; pub use glwe_ct::*; -// pub use glwe_tsk::*; +pub use glwe_tsk::*;