From ede2abb5f9332085abc73f141a40c00f963ba047 Mon Sep 17 00:00:00 2001 From: arnaucube Date: Thu, 5 Feb 2026 17:08:24 +0000 Subject: [PATCH] dbg normalize check --- poulpy-core/examples/encryption.rs | 30 +++++++++++++++++++++++++----- poulpy-core/src/encryption/glwe.rs | 21 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/poulpy-core/examples/encryption.rs b/poulpy-core/examples/encryption.rs index 08e1595..0631576 100644 --- a/poulpy-core/examples/encryption.rs +++ b/poulpy-core/examples/encryption.rs @@ -1,9 +1,9 @@ use poulpy_core::{ - GLWESub, SIGMA, layouts::{ - Base2K, Degree, GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWESecret, LWEInfos, Rank, TorusPrecision, - prepared::GLWESecretPrepared, + prepared::GLWESecretPrepared, Base2K, Degree, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWESecret, LWEInfos, Rank, + TorusPrecision, GLWE, }, + GLWESub, SIGMA, }; #[cfg(all(feature = "enable-avx", target_arch = "x86_64"))] @@ -71,18 +71,38 @@ fn main() { let mut sk_prepared: GLWESecretPrepared, BackendImpl> = GLWESecretPrepared::alloc(&module, rank); sk_prepared.prepare(&module, &sk); + // DBG: + // obtain pk from sk + let mut pk = poulpy_core::layouts::GLWEPublicKey::alloc_from_infos(&glwe_ct_infos); + pk.generate(&module, &sk_prepared, &mut source_xa, &mut source_xe); + // Backend-prepared publickey: + let mut pk_prepared: poulpy_core::layouts::GLWEPublicKeyPrepared, BackendImpl> = + poulpy_core::layouts::GLWEPublicKeyPrepared::alloc(&module, base2k, k_ct, rank); + pk_prepared.prepare(&module, &pk); + // Uniform plaintext module.vec_znx_fill_uniform(base2k.into(), &mut pt_want.data, 0, &mut source_xa); // Encryption - ct.encrypt_sk( + // ct.encrypt_sk( + // &module, + // &pt_want, + // &sk_prepared, + // &mut source_xa, + // &mut source_xe, + // scratch.borrow(), + // ); + + // DBG: encrypt with public key + ct.encrypt_pk( &module, &pt_want, - &sk_prepared, + &pk_prepared, &mut source_xa, &mut source_xe, scratch.borrow(), ); + dbg!("encrypted"); // Decryption ct.decrypt(&module, &mut pt_have, &sk_prepared, scratch.borrow()); diff --git a/poulpy-core/src/encryption/glwe.rs b/poulpy-core/src/encryption/glwe.rs index 06b6426..4404eb4 100644 --- a/poulpy-core/src/encryption/glwe.rs +++ b/poulpy-core/src/encryption/glwe.rs @@ -339,6 +339,7 @@ where + SvpPPolBytesOf + ModuleN + VecZnxDftBytesOf, + BE: Backend, Scratch: ScratchTakeBasic, { fn glwe_encrypt_pk_internal( @@ -413,11 +414,31 @@ where // ct[i] = norm(ci_big) self.vec_znx_big_normalize(&mut res.data, base2k, 0, i, &ci_big, base2k, 0, scratch_2); + + use poulpy_hal::layouts::ZnxView; + for k in 0..res.size(){ + normalize_check(i, k, ci_big.at(i, k), res.data.at(i, k)); + } } } } } +// DBG +fn normalize_check(i: usize /*col*/, k: usize, a: &[i64], b: &[i64]) { + assert_eq!(a.len(), b.len()); + // let c_{i,j} be the coefficients of the normalized polynomial, then + // i. it is K-normalized if c_{i,j} \in [-2^{K-1}, 2^{K-1}) \forall i=0,\ldots,N-1; j \geq 1 + // ii. it is reduced if additionally c_{i,0}=0 ~\forall~ i=0, \ldots, N-1 + if k == 0 { + for j in 0..a.len() { + assert_eq!(b[j], a[j]); + } + } + // println!("a {:?}", a); + // println!("b {:?}", b); +} + pub(crate) trait GLWEEncryptSkInternal { #[allow(clippy::too_many_arguments)] fn glwe_encrypt_sk_internal(