From 8dc4946736bd2936a3b0e96d26f083d6d542fac9 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 20 Oct 2025 10:39:00 +0200 Subject: [PATCH] ggsw encrypt --- poulpy-core/src/lib.rs | 1 + poulpy-core/src/noise/mod.rs | 4 +- .../tests/test_suite/encryption/ggsw_ct.rs | 90 +++++-------------- .../src/tests/test_suite/encryption/mod.rs | 4 +- 4 files changed, 29 insertions(+), 70 deletions(-) diff --git a/poulpy-core/src/lib.rs b/poulpy-core/src/lib.rs index 8c45893..dae6157 100644 --- a/poulpy-core/src/lib.rs +++ b/poulpy-core/src/lib.rs @@ -21,6 +21,7 @@ pub use encryption::*; pub use external_product::*; pub use glwe_packing::*; pub use keyswitching::*; +pub use noise::*; pub use encryption::SIGMA; diff --git a/poulpy-core/src/noise/mod.rs b/poulpy-core/src/noise/mod.rs index cb2cefe..aace6ad 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/test_suite/encryption/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs index 7620709..529a42f 100644 --- a/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs @@ -1,32 +1,23 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, - VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScalarZnx, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl, - }, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow}, + layouts::{Backend, Module, ScalarZnx, Scratch, ScratchOwned}, source::Source, }; use crate::{ + GGSWCompressedEncryptSk, GGSWEncryptSk, GGSWNoise, ScratchTakeCore, encryption::SIGMA, layouts::{ - GGSW, GGSWCiphertextLayout, GLWESecret, - compressed::{Decompress, GGSWCompressed}, - prepared::{GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWDecompress, GGSWLayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, compressed::GGSWCompressed, + prepared::GLWESecretPrepared, }, }; -pub fn test_ggsw_encrypt_sk(module: &Module) +pub fn test_ggsw_encrypt_sk(module: &Module) where - ScratchOwned: ScratchOwnedAlloc, - Module: SvpPrepare, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, + Module: GGSWEncryptSk + GLWESecretPreparedAlloc + GLWESecretPrepare + GGSWNoise, { let base2k: usize = 12; let k: usize = 54; @@ -36,7 +27,7 @@ where let n: usize = module.n(); let dnum: usize = (k - di * base2k) / (di * base2k); - let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k.into(), @@ -55,11 +46,13 @@ where pt_scalar.fill_ternary_hw(0, n, &mut source_xs); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos)); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ggsw_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); ct.encrypt_sk( module, @@ -77,46 +70,12 @@ where } } -pub fn test_ggsw_compressed_encrypt_sk(module: &Module) +pub fn test_ggsw_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigAddSmallInplace - + VecZnxAddScalarInplace - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxCopy - + VmpPMatAlloc - + VmpPrepare - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, + Module: + GGSWCompressedEncryptSk + GLWESecretPreparedAlloc + GLWESecretPrepare + GGSWNoise + GGSWDecompress, { let base2k: usize = 12; let k: usize = 54; @@ -126,7 +85,7 @@ where let n: usize = module.n(); let dnum: usize = (k - di * base2k) / (di * base2k); - let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k.into(), @@ -144,14 +103,13 @@ where pt_scalar.fill_ternary_hw(0, n, &mut source_xs); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSWCompressed::encrypt_sk_tmp_bytes( - module, - &ggsw_infos, - )); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSWCompressed::encrypt_sk_tmp_bytes(module, &ggsw_infos)); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ggsw_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]; diff --git a/poulpy-core/src/tests/test_suite/encryption/mod.rs b/poulpy-core/src/tests/test_suite/encryption/mod.rs index afc5599..d871177 100644 --- a/poulpy-core/src/tests/test_suite/encryption/mod.rs +++ b/poulpy-core/src/tests/test_suite/encryption/mod.rs @@ -1,11 +1,11 @@ mod gglwe_atk; mod gglwe_ct; -// mod ggsw_ct; +mod ggsw_ct; mod glwe_ct; mod glwe_tsk; pub use gglwe_atk::*; pub use gglwe_ct::*; -// pub use ggsw_ct::*; +pub use ggsw_ct::*; pub use glwe_ct::*; pub use glwe_tsk::*;