fixed sampling & rlwe encryption

This commit is contained in:
Jean-Philippe Bossuat
2025-02-12 08:25:38 +01:00
parent 4a01bb8420
commit 1f52a3d266
6 changed files with 60 additions and 36 deletions

View File

@@ -145,28 +145,35 @@ pub fn encrypt_rlwe_sk_thread_safe(
// c1 <- Z_{2^prec}[X]/(X^{N}+1)
c1.fill_uniform(log_base2k, limbs, source_xa);
let bytes_of_vec_znx_dft = module.bytes_of_vec_znx_dft(limbs);
let bytes_of_vec_znx_dft: usize = module.bytes_of_vec_znx_dft(limbs);
// Scratch space for DFT values
let mut buf_dft: VecZnxDft =
VecZnxDft::from_bytes(limbs, &mut tmp_bytes[..bytes_of_vec_znx_dft]);
// Applies buf_dft <- s * c1
// Applies buf_dft <- DFT(s) * DFT(c1)
module.svp_apply_dft(&mut buf_dft, sk, c1, limbs);
// Alias scratch space
let mut buf_big: VecZnxBig = buf_dft.as_vec_znx_big();
if let Some(pt) = pt {
// buf_big <- m - buf_big
module.vec_znx_big_sub_small_a_inplace(&mut buf_big, pt.at(0));
};
// buf_big = s x c1
module.vec_znx_idft_tmp_a(&mut buf_big, &mut buf_dft, limbs);
let carry: &mut [u8] = &mut tmp_bytes[bytes_of_vec_znx_dft..];
// c0 <- normalize(buf_big) + e
// c0 <- -s x c1 + m
let c0: &mut VecZnx = ct.at_mut(0);
module.vec_znx_big_normalize(log_base2k, c0, &buf_big, carry);
if let Some(pt) = pt {
module.vec_znx_big_sub_small_a_inplace(&mut buf_big, pt.at(0));
module.vec_znx_big_normalize(log_base2k, c0, &buf_big, carry);
} else {
module.vec_znx_big_normalize(log_base2k, c0, &buf_big, carry);
module.vec_znx_negate_inplace(c0);
}
// c0 <- -s x c1 + m + e
c0.add_normal(log_base2k, log_q, source_xe, sigma, (sigma * 6.0).ceil());
}

View File

@@ -1,7 +1,7 @@
use crate::encryptor::encrypt_grlwe_sk_thread_safe;
use crate::encryptor::{encrypt_grlwe_sk_thread_safe, encrypt_grlwe_sk_tmp_bytes};
use crate::keys::{PublicKey, SecretKey, SwitchingKey};
use crate::parameters::Parameters;
use base2k::SvpPPol;
use base2k::{Module, SvpPPol, SvpPPolOps};
use sampling::source::Source;
pub struct KeyGenerator {}
@@ -38,21 +38,28 @@ impl KeyGenerator {
);
pk
}
pub fn gen_switching_key_thread_safe(
&self,
params: &Parameters,
sk_in: &SecretKey,
sk_out: &SecretKey,
rows: usize,
log_q: usize,
tmp_bytes: &mut [u8],
) -> SwitchingKey {
let swk: SwitchingKey = SwitchingKey::new(params.module(), params.log_base2k(), rows, log_q, 0);
let module: &base2k::Module = params.module();
encrypt_grlwe_sk_thread_safe(module, swk.0, &sk_in.0, sk_out, source_xa, source_xe, sigma, tmp_bytes);
swk
}
}
pub fn gen_switching_key_thread_safe_tmp_bytes(
module: &Module,
log_base2k: usize,
rows: usize,
log_q: usize,
) -> usize {
encrypt_grlwe_sk_tmp_bytes(module, log_base2k, rows, log_q)
}
pub fn gen_switching_key_thread_safe(
module: &Module,
swk: &mut SwitchingKey,
sk_in: &SecretKey,
sk_out: &SvpPPol,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
tmp_bytes: &mut [u8],
) {
encrypt_grlwe_sk_thread_safe(
module, &mut swk.0, &sk_in.0, sk_out, source_xa, source_xe, sigma, tmp_bytes,
);
}

View File

@@ -8,8 +8,8 @@ use sampling::source::Source;
pub struct SecretKey(pub Scalar);
impl SecretKey {
pub fn new(params: &Module) -> Self {
SecretKey(Scalar::new(params.n()))
pub fn new(module: &Module) -> Self {
SecretKey(Scalar::new(module.n()))
}
pub fn fill_ternary_prob(&mut self, prob: f64, source: &mut Source) {
@@ -58,7 +58,7 @@ impl PublicKey {
}
}
pub struct SwitchingKey(GadgetCiphertext);
pub struct SwitchingKey(pub GadgetCiphertext);
impl SwitchingKey {
pub fn new(