fixed gadget product & vec_znx_big_add

This commit is contained in:
Jean-Philippe Bossuat
2025-02-13 10:55:16 +01:00
parent 7c25ad2eba
commit 7019812b65
3 changed files with 96 additions and 64 deletions

View File

@@ -1,8 +1,9 @@
use crate::{
ciphertext::{Ciphertext, GadgetCiphertext},
elem::Elem,
keys::SwitchingKey,
};
use base2k::{Infos, Module, VecZnx, VecZnxBig, VecZnxDft, VmpPMatOps};
use base2k::{Infos, Module, VecZnx, VecZnxBig, VecZnxDft, VecZnxOps, VmpPMatOps};
pub fn gadget_product_tmp_bytes(
module: &Module,
@@ -19,11 +20,23 @@ pub fn gadget_product_tmp_bytes(
+ 2 * module.bytes_of_vec_znx_dft(gct_cols)
}
pub fn gadget_product_inplace_thread_safe<const OVERWRITE: bool>(
module: &Module,
res: &mut Elem,
b: &GadgetCiphertext,
tmp_bytes: &mut [u8],
) {
unsafe {
let a_ptr: *const VecZnx = res.at(1) as *const VecZnx;
gadget_product_thread_safe::<OVERWRITE>(module, res, &*a_ptr, b, tmp_bytes);
}
}
/// Evaluates the gadget product res <- a x b.
///
/// # Arguments
///
/// * `module`: backend support for operations mod (X^N + 1)
/// * `module`: backend support for operations mod (X^N + 1).
/// * `res`: an [Elem] to store (-cs + m * a + e, c) with res_ncols limbs.
/// * `a`: a [VecZnx] of a_ncols limbs.
/// * `b`: a [GadgetCiphertext] as a vector of (-Bs + m * 2^{-k} + E, B)
@@ -33,7 +46,7 @@ pub fn gadget_product_tmp_bytes(
///
/// res = sum[min(a_ncols, b_nrows)] decomp(a, i) * (-B[i]s + m * 2^{-k*i} + E[i], B[i])
/// = (cs + m * a + e, c) with min(res_limbs, b_cols) limbs.
pub fn gadget_product_thread_safe(
pub fn gadget_product_thread_safe<const OVERWRITE: bool>(
module: &Module,
res: &mut Elem,
a: &VecZnx,
@@ -52,24 +65,35 @@ pub fn gadget_product_thread_safe(
let mut res_dft: VecZnxDft = module.new_vec_znx_from_bytes(cols, tmp_bytes_res_dft);
let mut res_big: VecZnxBig = res_dft.as_vec_znx_big();
// c1_dft <- DFT(c1) [cols]
// a_dft <- DFT(a) [cols]
module.vec_znx_dft(&mut c1_dft, a, a.limbs());
// res_dft <- sum[rows] DFT(c1)[cols] x GadgetCiphertext[0][cols]
// >>>>>>>> RES[0]
// res_dft <- sum[rows] DFT(a)[cols] x GadgetCiphertext[0][cols]
module.vmp_apply_dft_to_dft(&mut res_dft, &c1_dft, &b.value[0], tmp_bytes_vmp_apply_dft);
// res_big <- IDFT(DFT(c1) x GadgetCiphertext[0])
// res_big <- IDFT(DFT(a) x GadgetCiphertext[0])
module.vec_znx_idft_tmp_a(&mut res_big, &mut res_dft, cols);
// res[0] = normalize(c0 + c1_dft x GadgetCiphertext[0])
// res_big <- res[0] + a_dft x GadgetCiphertext[0]
module.vec_znx_big_add_small_inplace(&mut res_big, res.at(0));
module.vec_znx_big_normalize(log_base2k, res.at_mut(0), &res_big, tmp_bytes_vmp_apply_dft);
// >>>>>>>> RES[1]
// res_dft <- DFT(c1) x GadgetCiphertext[1]
module.vmp_apply_dft_to_dft(&mut res_dft, &c1_dft, &b.value[1], tmp_bytes_vmp_apply_dft);
// res_big <- IDFT(DFT(c1) x GadgetCiphertext[1])
module.vec_znx_idft_tmp_a(&mut res_big, &mut res_dft, cols);
// res[1] = normalize(c1_dft x GadgetCiphertext[1])
module.vec_znx_big_normalize(log_base2k, res.at_mut(1), &res_big, tmp_bytes_vmp_apply_dft);
if OVERWRITE {
// res[1] = normalize(a_dft x GadgetCiphertext[1])
module.vec_znx_big_normalize(log_base2k, res.at_mut(1), &res_big, tmp_bytes_vmp_apply_dft);
} else {
// res[1] = normalize(a_dft x GadgetCiphertext[1] + res[1])
module.vec_znx_big_add_small_inplace(&mut res_big, res.at(0));
module.vec_znx_big_normalize(log_base2k, res.at_mut(0), &res_big, tmp_bytes_vmp_apply_dft);
}
}