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

@@ -1,7 +1,8 @@
use poulpy_core::{
GLWEOperations,
layouts::{
GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos, LWECiphertext, LWEPlaintext, LWESecret,
GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWECiphertext,
GLWECiphertextLayout, GLWEPlaintext, GLWESecret, LWECiphertext, LWECiphertextLayout, LWEInfos, LWEPlaintext, LWESecret,
prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc},
},
};
@@ -20,9 +21,10 @@ use poulpy_hal::{
};
use poulpy_schemes::tfhe::{
blind_rotation::CGGI,
blind_rotation::{BlindRotationKeyLayout, CGGI},
circuit_bootstrapping::{
CircuitBootstrappingKey, CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute,
CircuitBootstrappingKey, CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyLayout,
CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute,
},
};
@@ -34,7 +36,7 @@ fn main() {
let module: Module<BackendImpl> = Module::<BackendImpl>::new(n_glwe as u64);
// Base 2 loga
let basek: usize = 13;
let base2k: usize = 13;
// Lookup table extension factor
let extension_factor: usize = 1;
@@ -58,25 +60,67 @@ fn main() {
let rows_ggsw_res: usize = 2;
// GGSW output modulus
let k_ggsw_res: usize = (rows_ggsw_res + 1) * basek;
let k_ggsw_res: usize = (rows_ggsw_res + 1) * base2k;
// Blind rotation key GGSW number of rows
let rows_brk: usize = rows_ggsw_res + 1;
// Blind rotation key GGSW modulus
let k_brk: usize = (rows_brk + 1) * basek;
let k_brk: usize = (rows_brk + 1) * base2k;
// GGLWE automorphism keys number of rows
let rows_trace: usize = rows_ggsw_res + 1;
// GGLWE automorphism keys modulus
let k_trace: usize = (rows_trace + 1) * basek;
let k_trace: usize = (rows_trace + 1) * base2k;
// GGLWE tensor key number of rows
let rows_tsk: usize = rows_ggsw_res + 1;
// GGLWE tensor key modulus
let k_tsk: usize = (rows_tsk + 1) * basek;
let k_tsk: usize = (rows_tsk + 1) * base2k;
let cbt_infos: CircuitBootstrappingKeyLayout = CircuitBootstrappingKeyLayout {
layout_brk: BlindRotationKeyLayout {
n_glwe: n_glwe.into(),
n_lwe: n_lwe.into(),
base2k: base2k.into(),
k: k_brk.into(),
rows: rows_brk.into(),
rank: rank.into(),
},
layout_atk: GGLWEAutomorphismKeyLayout {
n: n_glwe.into(),
base2k: base2k.into(),
k: k_trace.into(),
rows: rows_trace.into(),
digits: 1_u32.into(),
rank: rank.into(),
},
layout_tsk: GGLWETensorKeyLayout {
n: n_glwe.into(),
base2k: base2k.into(),
k: k_tsk.into(),
rows: rows_tsk.into(),
digits: 1_u32.into(),
rank: rank.into(),
},
};
let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout {
n: n_glwe.into(),
base2k: base2k.into(),
k: k_ggsw_res.into(),
rows: rows_ggsw_res.into(),
digits: 1_u32.into(),
rank: rank.into(),
};
let lwe_infos = LWECiphertextLayout {
n: n_lwe.into(),
k: k_lwe_ct.into(),
base2k: base2k.into(),
};
// Scratch space (4MB)
let mut scratch: ScratchOwned<BackendImpl> = ScratchOwned::alloc(1 << 22);
@@ -91,12 +135,12 @@ fn main() {
let mut source_xe: Source = Source::new([1u8; 32]);
// LWE secret
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into());
sk_lwe.fill_binary_block(block_size, &mut source_xs);
sk_lwe.fill_zero();
// GLWE secret
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n_glwe, rank);
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n_glwe.into(), rank.into());
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
// sk_glwe.fill_zero();
@@ -107,17 +151,23 @@ fn main() {
let data: i64 = 1 % (1 << k_lwe_pt);
// LWE plaintext
let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_pt);
let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into());
// LWE plaintext(data * 2^{- (k_lwe_pt - 1)})
pt_lwe.encode_i64(data, k_lwe_pt + 1); // +1 for padding bit
pt_lwe.encode_i64(data, (k_lwe_pt + 1).into()); // +1 for padding bit
// Normalize plaintext to nicely print coefficients
module.zn_normalize_inplace(pt_lwe.n(), basek, pt_lwe.data_mut(), 0, scratch.borrow());
println!("pt_lwe: {}", pt_lwe);
module.zn_normalize_inplace(
pt_lwe.n().into(),
base2k,
pt_lwe.data_mut(),
0,
scratch.borrow(),
);
println!("pt_lwe: {pt_lwe}");
// LWE ciphertext
let mut ct_lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe_ct);
let mut ct_lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&lwe_infos);
// Encrypt LWE Plaintext
ct_lwe.encrypt_sk(&module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe);
@@ -127,15 +177,9 @@ fn main() {
// Circuit bootstrapping evaluation key
let cbt_key: CircuitBootstrappingKey<Vec<u8>, CGGI> = CircuitBootstrappingKey::encrypt_sk(
&module,
basek,
&sk_lwe,
&sk_glwe,
k_brk,
rows_brk,
k_trace,
rows_trace,
k_tsk,
rows_tsk,
&cbt_infos,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
@@ -143,7 +187,7 @@ fn main() {
println!("CBT-KGEN: {} ms", now.elapsed().as_millis());
// Output GGSW
let mut res: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(n_glwe, basek, k_ggsw_res, rows_ggsw_res, 1, rank);
let mut res: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_infos);
// Circuit bootstrapping key prepared (opaque backend dependant write only struct)
let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, CGGI, BackendImpl> =
@@ -170,19 +214,26 @@ fn main() {
// Tests RLWE(1) * GGSW(data)
let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout {
n: n_glwe.into(),
base2k: base2k.into(),
k: (k_ggsw_res - base2k).into(),
rank: rank.into(),
};
// GLWE ciphertext modulus
let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n_glwe, basek, k_ggsw_res - basek, rank);
let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_infos);
// Some GLWE plaintext with signed data
let k_glwe_pt: usize = 3;
let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n_glwe, basek, basek);
let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_infos);
let mut data_vec: Vec<i64> = vec![0i64; n_glwe];
data_vec
.iter_mut()
.enumerate()
.for_each(|(x, y)| *y = (x % (1 << (k_glwe_pt - 1))) as i64 - (1 << (k_glwe_pt - 2)));
pt_glwe.encode_vec_i64(&data_vec, k_lwe_pt + 2);
pt_glwe.encode_vec_i64(&data_vec, (k_lwe_pt + 2).into());
pt_glwe.normalize_inplace(&module, scratch.borrow());
println!("{}", pt_glwe);
@@ -204,7 +255,7 @@ fn main() {
ct_glwe.external_product_inplace(&module, &res_prepared, scratch.borrow());
// Decrypt
let mut pt_res: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n_glwe, basek, ct_glwe.k());
let mut pt_res: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_infos);
ct_glwe.decrypt(&module, &mut pt_res, &sk_glwe_prepared, scratch.borrow());
println!("pt_res: {:?}", &pt_res.data.at(0, 0)[..64]);