From d68c0a2baf608847f4a9178bfd5a22c8c7052cdb Mon Sep 17 00:00:00 2001 From: Rasoul Akhavan Mahdavi Date: Sun, 19 Oct 2025 22:57:00 -0400 Subject: [PATCH] gglwe atk key --- .../src/layouts/compressed/gglwe_atk.rs | 9 +- poulpy-core/src/noise/mod.rs | 4 +- poulpy-core/src/tests/mod.rs | 4 +- .../tests/test_suite/encryption/gglwe_atk.rs | 148 ++++++------------ .../src/tests/test_suite/encryption/mod.rs | 4 +- 5 files changed, 59 insertions(+), 110 deletions(-) diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index 276b695..8c57529 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_atk.rs @@ -20,6 +20,12 @@ pub struct AutomorphismKeyCompressed { pub(crate) p: i64, } +impl GetAutomorphismGaloisElement for AutomorphismKeyCompressed { + fn p(&self) -> i64 { + self.p + } +} + impl LWEInfos for AutomorphismKeyCompressed { fn n(&self) -> Degree { self.key.n() @@ -37,6 +43,7 @@ impl LWEInfos for AutomorphismKeyCompressed { self.key.size() } } + impl GLWEInfos for AutomorphismKeyCompressed { fn rank(&self) -> Rank { self.rank_out() @@ -199,7 +206,7 @@ where } } -impl AutomorphismKeyDecompress for Module where Self: AutomorphismKeyDecompress {} +impl AutomorphismKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} impl AutomorphismKey where diff --git a/poulpy-core/src/noise/mod.rs b/poulpy-core/src/noise/mod.rs index aace6ad..cb2cefe 100644 --- a/poulpy-core/src/noise/mod.rs +++ b/poulpy-core/src/noise/mod.rs @@ -3,8 +3,8 @@ mod ggsw_ct; mod glwe_ct; pub use gglwe_ct::*; -pub use ggsw_ct::*; -pub use glwe_ct::*; +// pub use ggsw_ct::*; +// pub use glwe_ct::*; #[allow(clippy::too_many_arguments)] #[allow(dead_code)] diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 9cad53d..b04779e 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -32,8 +32,8 @@ backend_test_suite!( // GGLWE Encryption gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, -// gglwe_automorphisk_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_encrypt_sk, -// gglwe_automorphisk_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_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_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, // GGLWE Keyswitching diff --git a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs index bd164dc..81b727f 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -1,71 +1,38 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, - VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, - VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, + ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, + VecZnxAutomorphism,VecZnxFillUniform, }, + layouts::{Backend, Module, Scratch, ScratchOwned, GaloisElement}, source::Source, }; use crate::{ encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWEInfos, GLWESecret, - compressed::{AutomorphismKeyCompressed, Decompress}, - prepared::{GLWESecretPrepared, PrepareAlloc}, + compressed::AutomorphismKeyCompressed, prepared::GLWESecretPrepared, + AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, + GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKeyAlloc, GLWESwitchingKeyDecompress }, + noise::GGLWENoise, + GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore }; -pub fn test_gglwe_automorphisk_key_encrypt_sk(module: &Module) +pub fn test_gglwe_automorphism_key_encrypt_sk(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes + Module: GGLWEEncryptSk + + GGLWEKeyswitch + + GLWESecretPreparedAlloc + + GLWESecretPrepare + + GLWESwitchingKeyAlloc + + GLWESwitchingKeyEncryptSk + + GLWESwitchingKeyCompressedEncryptSk + + GLWESwitchingKeyDecompress + + GGLWENoise + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigAddSmallInplace - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxAutomorphismInplace - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxSubScalarInplace - + VecZnxCopy - + VmpPMatAlloc - + VmpPrepare, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxDftImpl - + TakeVecZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl, + + VecZnxAutomorphism, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_ksk: usize = 60; @@ -84,17 +51,17 @@ where rank: rank.into(), }; - let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(module, &atk_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(AutomorphismKey::encrypt_sk_tmp_bytes( + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes( module, &atk_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5; @@ -118,7 +85,8 @@ where i, ); }); - let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); + let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, sk_out.rank().into()); + sk_out_prepared.prepare(module, &sk_out); atk.key .key @@ -127,48 +95,21 @@ where } } -pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk(module: &Module) +pub fn test_gglwe_automorphism_key_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigAddSmallInplace + Module: GGLWEEncryptSk + + GGLWEKeyswitch + + GLWESecretPreparedAlloc + + GLWESecretPrepare + + GLWESwitchingKeyAlloc + + GLWESwitchingKeyEncryptSk + + GLWESwitchingKeyCompressedEncryptSk + + AutomorphismKeyDecompress + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxAutomorphismInplace - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxSubScalarInplace - + VecZnxCopy - + VmpPMatAlloc - + VmpPrepare, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxDftImpl - + TakeVecZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl, + + VecZnxFillUniform + + GGLWENoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_ksk: usize = 60; @@ -187,16 +128,16 @@ where rank: rank.into(), }; - let mut atk_compressed: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc_from_infos(&atk_infos); + let mut atk_compressed: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc_from_infos(module, &atk_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(AutomorphismKey::encrypt_sk_tmp_bytes( + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes( module, &atk_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5; @@ -215,9 +156,10 @@ where i, ); }); - let sk_out_prepared = sk_out.prepare_alloc(module, scratch.borrow()); + let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, sk_out.rank().into()); + sk_out_prepared.prepare(module, &sk_out); - let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(module, &atk_infos); atk.decompress(module, &atk_compressed); atk.key diff --git a/poulpy-core/src/tests/test_suite/encryption/mod.rs b/poulpy-core/src/tests/test_suite/encryption/mod.rs index 2ed1147..a23cb64 100644 --- a/poulpy-core/src/tests/test_suite/encryption/mod.rs +++ b/poulpy-core/src/tests/test_suite/encryption/mod.rs @@ -1,10 +1,10 @@ -// mod gglwe_atk; +mod gglwe_atk; mod gglwe_ct; // mod ggsw_ct; mod glwe_ct; // mod glwe_tsk; -// pub use gglwe_atk::*; +pub use gglwe_atk::*; pub use gglwe_ct::*; // pub use ggsw_ct::*; pub use glwe_ct::*;