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

@@ -5,9 +5,9 @@ use poulpy_hal::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare,
VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace,
VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace, VecZnxBigNormalize,
VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallBInplace, VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply,
VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply,
VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace,
VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubABInplace,
VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace,
VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare,
},
layouts::{Backend, Module, ScratchOwned},
@@ -21,7 +21,7 @@ use poulpy_hal::{
use crate::{
GLWEOperations, GLWEPacker,
layouts::{
GGLWEAutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret,
GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret,
prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc},
},
};
@@ -31,7 +31,7 @@ where
Module<B>: VecZnxDftAllocBytes
+ VecZnxAutomorphism
+ VecZnxBigAutomorphismInplace<B>
+ VecZnxBigSubSmallBInplace<B>
+ VecZnxBigSubSmallNegateInplace<B>
+ VecZnxNegateInplace
+ VecZnxRshInplace<B>
+ VecZnxRotateInplace<B>
@@ -41,7 +41,7 @@ where
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxFillUniform
+ VecZnxSubABInplace
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
@@ -79,37 +79,53 @@ where
let mut source_xa: Source = Source::new([0u8; 32]);
let n: usize = module.n();
let basek: usize = 18;
let base2k: usize = 18;
let k_ct: usize = 36;
let pt_k: usize = 18;
let rank: usize = 3;
let digits: usize = 1;
let k_ksk: usize = k_ct + basek * digits;
let k_ksk: usize = k_ct + base2k * digits;
let rows: usize = k_ct.div_ceil(basek * digits);
let rows: usize = k_ct.div_ceil(base2k * digits);
let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout {
n: n.into(),
base2k: base2k.into(),
k: k_ct.into(),
rank: rank.into(),
};
let key_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout {
n: n.into(),
base2k: base2k.into(),
k: k_ksk.into(),
rank: rank.into(),
digits: digits.into(),
rows: rows.into(),
};
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GLWECiphertext::encrypt_sk_scratch_space(module, basek, k_ct)
| GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, basek, k_ksk, rank)
| GLWEPacker::scratch_space(module, basek, k_ct, k_ksk, digits, rank),
GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos)
| GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &key_infos)
| GLWEPacker::scratch_space(module, &glwe_out_infos, &key_infos),
);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_out_infos);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_dft: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_ct);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_out_infos);
let mut data: Vec<i64> = vec![0i64; n];
data.iter_mut().enumerate().for_each(|(i, x)| {
*x = i as i64;
});
pt.encode_vec_i64(&data, pt_k);
pt.encode_vec_i64(&data, pt_k.into());
let gal_els: Vec<i64> = GLWEPacker::galois_elements(module);
let mut auto_keys: HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new();
let mut tmp: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(n, basek, k_ksk, rows, digits, rank);
let mut tmp: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&key_infos);
gal_els.iter().for_each(|gal_el| {
tmp.encrypt_sk(
module,
@@ -125,9 +141,9 @@ where
let log_batch: usize = 0;
let mut packer: GLWEPacker = GLWEPacker::new(n, log_batch, basek, k_ct, rank);
let mut packer: GLWEPacker = GLWEPacker::new(&glwe_out_infos, log_batch);
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_ct, rank);
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos);
ct.encrypt_sk(
module,
@@ -164,10 +180,10 @@ where
}
});
let mut res = GLWECiphertext::alloc(n, basek, k_ct, rank);
let mut res: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos);
packer.flush(module, &mut res);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_ct);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_out_infos);
let mut data: Vec<i64> = vec![0i64; n];
data.iter_mut().enumerate().for_each(|(i, x)| {
if i.is_multiple_of(5) {
@@ -175,7 +191,7 @@ where
}
});
pt_want.encode_vec_i64(&data, pt_k);
pt_want.encode_vec_i64(&data, pt_k.into());
res.decrypt(module, &mut pt, &sk_dft, scratch.borrow());
@@ -184,9 +200,8 @@ where
let noise_have: f64 = pt.std().log2();
assert!(
noise_have < -((k_ct - basek) as f64),
"noise: {}",
noise_have
noise_have < -((k_ct - base2k) as f64),
"noise: {noise_have}"
);
}