Add cross-basek normalization (#90)

* added cross_basek_normalization

* updated method signatures to take layouts

* fixed cross-base normalization

fix #91
fix #93
This commit is contained in:
Jean-Philippe Bossuat
2025-09-30 14:40:10 +02:00
committed by GitHub
parent 4da790ea6a
commit 37e13b965c
216 changed files with 12481 additions and 7745 deletions

View File

@@ -2,7 +2,8 @@ use poulpy_backend::cpu_spqlios::FFT64Spqlios;
use poulpy_core::{
GLWEOperations, SIGMA,
layouts::{
GLWECiphertext, GLWEPlaintext, GLWESecret, Infos,
Base2K, Degree, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWEPlaintextLayout, GLWESecret, LWEInfos, Rank,
TorusPrecision,
prepared::{GLWESecretPrepared, PrepareAlloc},
},
};
@@ -16,27 +17,36 @@ fn main() {
// Ring degree
let log_n: usize = 10;
let n: usize = 1 << log_n;
let n: Degree = Degree(1 << log_n);
// Base-2-k (implicit digit decomposition)
let basek: usize = 14;
let base2k: Base2K = Base2K(14);
// Ciphertext Torus precision (equivalent to ciphertext modulus)
let k_ct: usize = 27;
let k_ct: TorusPrecision = TorusPrecision(27);
// Plaintext Torus precision (equivament to plaintext modulus)
let k_pt: usize = basek;
let k_pt: TorusPrecision = TorusPrecision(base2k.into());
// GLWE rank
let rank: usize = 1;
let rank: Rank = Rank(1);
// Instantiate Module (DFT Tables)
let module: Module<FFT64Spqlios> = Module::<FFT64Spqlios>::new(n as u64);
let module: Module<FFT64Spqlios> = Module::<FFT64Spqlios>::new(n.0 as u64);
let glwe_ct_infos: GLWECiphertextLayout = GLWECiphertextLayout {
n,
base2k,
k: k_ct,
rank,
};
let glwe_pt_infos: GLWEPlaintextLayout = GLWEPlaintextLayout { n, base2k, k: k_pt };
// Allocates ciphertext & plaintexts
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_ct, rank);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_pt);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_pt);
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_ct_infos);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_pt_infos);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_pt_infos);
// CPRNG
let mut source_xs: Source = Source::new([0u8; 32]);
@@ -45,19 +55,19 @@ fn main() {
// Scratch space
let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc(
GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k())
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct.k()),
GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_ct_infos)
| GLWECiphertext::decrypt_scratch_space(&module, &glwe_ct_infos),
);
// Generate secret-key
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_ct_infos);
sk.fill_ternary_prob(0.5, &mut source_xs);
// Backend-prepared secret
let sk_prepared: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow());
// Uniform plaintext
module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, &mut source_xa);
module.vec_znx_fill_uniform(base2k.into(), &mut pt_want.data, 0, &mut source_xa);
// Encryption
ct.encrypt_sk(
@@ -76,7 +86,7 @@ fn main() {
pt_want.sub_inplace_ab(&module, &pt_have);
// Ideal vs. actual noise
let noise_have: f64 = pt_want.data.std(basek, 0) * (ct.k() as f64).exp2();
let noise_have: f64 = pt_want.data.std(base2k.into(), 0) * (ct.k().as_u32() as f64).exp2();
let noise_want: f64 = SIGMA;
// Check