packing test

This commit is contained in:
Pro7ech
2025-10-20 17:29:23 +02:00
parent d4c936e04c
commit 749552801c
4 changed files with 40 additions and 73 deletions

View File

@@ -179,6 +179,8 @@ where
} }
} }
impl<BE: Backend> GLWERotate<BE> for Module<BE> where Self: ModuleN + VecZnxRotate + VecZnxRotateInplace<BE> {}
pub trait GLWERotate<BE: Backend> pub trait GLWERotate<BE: Backend>
where where
Self: ModuleN + VecZnxRotate + VecZnxRotateInplace<BE>, Self: ModuleN + VecZnxRotate + VecZnxRotateInplace<BE>,
@@ -253,6 +255,8 @@ where
} }
} }
impl<BE: Backend> GLWECopy for Module<BE> where Self: ModuleN + VecZnxCopy {}
pub trait GLWECopy pub trait GLWECopy
where where
Self: ModuleN + VecZnxCopy, Self: ModuleN + VecZnxCopy,
@@ -278,6 +282,8 @@ where
} }
} }
impl<BE: Backend> GLWEShift<BE> for Module<BE> where Self: ModuleN + VecZnxRshInplace<BE> {}
pub trait GLWEShift<BE: Backend> pub trait GLWEShift<BE: Backend>
where where
Self: ModuleN + VecZnxRshInplace<BE>, Self: ModuleN + VecZnxRshInplace<BE>,
@@ -308,6 +314,8 @@ impl GLWE<Vec<u8>> {
} }
} }
impl<BE: Backend> GLWENormalize<BE> for Module<BE> where Self: ModuleN + VecZnxNormalize<BE> + VecZnxNormalizeInplace<BE> {}
pub trait GLWENormalize<BE: Backend> pub trait GLWENormalize<BE: Backend>
where where
Self: ModuleN + VecZnxNormalize<BE> + VecZnxNormalizeInplace<BE>, Self: ModuleN + VecZnxNormalize<BE> + VecZnxNormalizeInplace<BE>,

View File

@@ -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_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace,
// GLWE Trace // GLWE Trace
// glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, // 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 Encryption
gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, 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_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk,

View File

@@ -4,9 +4,9 @@ pub mod external_product;
pub mod keyswitch; pub mod keyswitch;
mod conversion; mod conversion;
// mod packing; mod packing;
// mod trace; // mod trace;
pub use conversion::*; pub use conversion::*;
// pub use packing::*; pub use packing::*;
// pub use trace::*; // pub use trace::*;

View File

@@ -1,78 +1,34 @@
use std::collections::HashMap; use std::collections::HashMap;
use poulpy_hal::{ use poulpy_hal::{
api::{ api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow},
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, layouts::{Backend, Module, Scratch, ScratchOwned},
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,
},
source::Source, source::Source,
}; };
use crate::{ use crate::{
GLWEOperations, GLWEPacker, AutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWEPacker, GLWEPacking, GLWERotate, GLWESub, ScratchTakeCore,
layouts::{ layouts::{
AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, AutomorphismKey, AutomorphismKeyLayout, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GLWE, GLWELayout,
prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc,
prepared::{AutomorphismKeyPrepared, GLWESecretPrepared},
}, },
}; };
pub fn test_glwe_packing<B>(module: &Module<B>) pub fn test_glwe_packing<BE: Backend>(module: &Module<BE>)
where where
Module<B>: VecZnxDftBytesOf Module<BE>: GLWEEncryptSk<BE>
+ VecZnxAutomorphism + AutomorphismKeyEncryptSk<BE>
+ VecZnxBigAutomorphismInplace<B> + AutomorphismKeyPrepare<BE>
+ VecZnxBigSubSmallNegateInplace<B> + AutomorphismKeyPreparedAlloc<BE>
+ VecZnxNegateInplace + GLWEPacking<BE>
+ VecZnxRshInplace<B> + GLWESecretPrepare<BE>
+ VecZnxRotateInplace<B> + GLWESecretPreparedAlloc<BE>
+ VecZnxBigNormalize<B> + GLWESub
+ VecZnxDftApply<B> + GLWEDecrypt<BE>
+ VecZnxRotate + GLWERotate<BE>,
+ SvpApplyDftToDftInplace<B> ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
+ VecZnxIdftApplyConsume<B> Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxBigBytesOf
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxAddScalarInplace
+ VmpPMatAlloc<B>
+ VmpPrepare<B>
+ VmpApplyDftToDftTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<B>
+ VecZnxBigNormalizeTmpBytes
+ VecZnxSwitchRing
+ VecZnxAutomorphismInplace<B>
+ VecZnxCopy,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ TakeSvpPPolImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>,
{ {
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]);
@@ -104,7 +60,7 @@ where
dnum: dnum.into(), dnum: dnum.into(),
}; };
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos)
| AutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos) | AutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos)
| GLWEPacker::tmp_bytes(module, &glwe_out_infos, &key_infos), | GLWEPacker::tmp_bytes(module, &glwe_out_infos, &key_infos),
@@ -112,7 +68,9 @@ where
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_out_infos); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_out_infos);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_dft: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
let mut sk_dft: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk);
sk_dft.prepare(module, &sk);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos);
let mut data: Vec<i64> = vec![0i64; n]; let mut data: Vec<i64> = vec![0i64; n];
@@ -124,7 +82,7 @@ where
let gal_els: Vec<i64> = GLWEPacker::galois_elements(module); let gal_els: Vec<i64> = GLWEPacker::galois_elements(module);
let mut auto_keys: HashMap<i64, AutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new(); let mut auto_keys: HashMap<i64, AutomorphismKeyPrepared<Vec<u8>, BE>> = HashMap::new();
let mut tmp: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&key_infos); let mut tmp: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&key_infos);
gal_els.iter().for_each(|gal_el| { gal_els.iter().for_each(|gal_el| {
tmp.encrypt_sk( tmp.encrypt_sk(
@@ -135,13 +93,14 @@ where
&mut source_xe, &mut source_xe,
scratch.borrow(), scratch.borrow(),
); );
let atk_prepared: AutomorphismKeyPrepared<Vec<u8>, B> = tmp.prepare_alloc(module, scratch.borrow()); let mut atk_prepared: AutomorphismKeyPrepared<Vec<u8>, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &tmp);
atk_prepared.prepare(module, &tmp, scratch.borrow());
auto_keys.insert(*gal_el, atk_prepared); auto_keys.insert(*gal_el, atk_prepared);
}); });
let log_batch: usize = 0; 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<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos); let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos);
@@ -166,7 +125,7 @@ where
scratch.borrow(), 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) { if reverse_bits_msb(i, log_n as u32).is_multiple_of(5) {
packer.add(module, Some(&ct), &auto_keys, scratch.borrow()); packer.add(module, Some(&ct), &auto_keys, scratch.borrow());
@@ -190,7 +149,7 @@ where
res.decrypt(module, &mut pt, &sk_dft, scratch.borrow()); 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(); let noise_have: f64 = pt.std().log2();