From 749552801cc12e7847ff09d6f702381c3ad95905 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 20 Oct 2025 17:29:23 +0200 Subject: [PATCH] packing test --- poulpy-core/src/operations/glwe.rs | 8 ++ poulpy-core/src/tests/mod.rs | 2 +- poulpy-core/src/tests/test_suite/mod.rs | 4 +- poulpy-core/src/tests/test_suite/packing.rs | 99 ++++++--------------- 4 files changed, 40 insertions(+), 73 deletions(-) diff --git a/poulpy-core/src/operations/glwe.rs b/poulpy-core/src/operations/glwe.rs index 48e67ca..417e5f2 100644 --- a/poulpy-core/src/operations/glwe.rs +++ b/poulpy-core/src/operations/glwe.rs @@ -179,6 +179,8 @@ where } } +impl GLWERotate for Module where Self: ModuleN + VecZnxRotate + VecZnxRotateInplace {} + pub trait GLWERotate where Self: ModuleN + VecZnxRotate + VecZnxRotateInplace, @@ -253,6 +255,8 @@ where } } +impl GLWECopy for Module where Self: ModuleN + VecZnxCopy {} + pub trait GLWECopy where Self: ModuleN + VecZnxCopy, @@ -278,6 +282,8 @@ where } } +impl GLWEShift for Module where Self: ModuleN + VecZnxRshInplace {} + pub trait GLWEShift where Self: ModuleN + VecZnxRshInplace, @@ -308,6 +314,8 @@ impl GLWE> { } } +impl GLWENormalize for Module where Self: ModuleN + VecZnxNormalize + VecZnxNormalizeInplace {} + pub trait GLWENormalize where Self: ModuleN + VecZnxNormalize + VecZnxNormalizeInplace, diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 595c480..291a94a 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -28,7 +28,7 @@ glwe_external_product => crate::tests::test_suite::external_product::test_glwe_e glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, // GLWE Trace // glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, -// glwe_packing => crate::tests::test_suite::test_glwe_packing, +glwe_packing => crate::tests::test_suite::test_glwe_packing, // 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, diff --git a/poulpy-core/src/tests/test_suite/mod.rs b/poulpy-core/src/tests/test_suite/mod.rs index e3b8e28..f6ee65e 100644 --- a/poulpy-core/src/tests/test_suite/mod.rs +++ b/poulpy-core/src/tests/test_suite/mod.rs @@ -4,9 +4,9 @@ pub mod external_product; pub mod keyswitch; mod conversion; -// mod packing; +mod packing; // mod trace; pub use conversion::*; -// pub use packing::*; +pub use packing::*; // pub use trace::*; diff --git a/poulpy-core/src/tests/test_suite/packing.rs b/poulpy-core/src/tests/test_suite/packing.rs index fdfbd57..7e132b2 100644 --- a/poulpy-core/src/tests/test_suite/packing.rs +++ b/poulpy-core/src/tests/test_suite/packing.rs @@ -1,78 +1,34 @@ use std::collections::HashMap; use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, - VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow}, + layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ - GLWEOperations, GLWEPacker, + AutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWEPacker, GLWEPacking, GLWERotate, GLWESub, ScratchTakeCore, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GLWE, GLWELayout, + GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, }, }; -pub fn test_glwe_packing(module: &Module) +pub fn test_glwe_packing(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxAutomorphism - + VecZnxBigAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxNegateInplace - + VecZnxRshInplace - + VecZnxRotateInplace - + VecZnxBigNormalize - + VecZnxDftApply - + VecZnxRotate - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxAutomorphismInplace - + VecZnxCopy, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + Module: GLWEEncryptSk + + AutomorphismKeyEncryptSk + + AutomorphismKeyPrepare + + AutomorphismKeyPreparedAlloc + + GLWEPacking + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GLWESub + + GLWEDecrypt + + GLWERotate, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -104,7 +60,7 @@ where dnum: dnum.into(), }; - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) | AutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos) | GLWEPacker::tmp_bytes(module, &glwe_out_infos, &key_infos), @@ -112,7 +68,9 @@ where let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_out_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_dft: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_dft: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk); + sk_dft.prepare(module, &sk); let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut data: Vec = vec![0i64; n]; @@ -124,7 +82,7 @@ where let gal_els: Vec = GLWEPacker::galois_elements(module); - let mut auto_keys: HashMap, B>> = HashMap::new(); + let mut auto_keys: HashMap, BE>> = HashMap::new(); let mut tmp: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( @@ -135,13 +93,14 @@ where &mut source_xe, scratch.borrow(), ); - let atk_prepared: AutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, scratch.borrow()); + let mut atk_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &tmp); + atk_prepared.prepare(module, &tmp, scratch.borrow()); auto_keys.insert(*gal_el, atk_prepared); }); let log_batch: usize = 0; - let mut packer: GLWEPacker = GLWEPacker::new(&glwe_out_infos, log_batch); + let mut packer: GLWEPacker = GLWEPacker::alloc(&glwe_out_infos, log_batch); let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); @@ -166,7 +125,7 @@ where scratch.borrow(), ); - pt.rotate_inplace(module, -(1 << log_batch), scratch.borrow()); // X^-batch * pt + module.glwe_rotate_inplace(-(1 << log_batch), &mut pt, scratch.borrow()); // X^-batch * pt if reverse_bits_msb(i, log_n as u32).is_multiple_of(5) { packer.add(module, Some(&ct), &auto_keys, scratch.borrow()); @@ -190,7 +149,7 @@ where res.decrypt(module, &mut pt, &sk_dft, scratch.borrow()); - pt.sub_inplace_ab(module, &pt_want); + module.glwe_sub_inplace(&mut pt, &pt_want); let noise_have: f64 = pt.std().log2();