mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 13:16:44 +01:00
fixed gadget product & related example
This commit is contained in:
@@ -48,7 +48,7 @@ impl Ciphertext {
|
||||
self.0.log_scale
|
||||
}
|
||||
|
||||
pub fn zero(&mut self){
|
||||
pub fn zero(&mut self) {
|
||||
self.0.zero()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
use crate::{
|
||||
ciphertext::{Ciphertext, GadgetCiphertext}, elem::Elem, keys::SecretKey, parameters::Parameters, plaintext::Plaintext
|
||||
ciphertext::{Ciphertext, GadgetCiphertext},
|
||||
elem::Elem,
|
||||
keys::SecretKey,
|
||||
parameters::Parameters,
|
||||
plaintext::Plaintext,
|
||||
};
|
||||
use base2k::{Module, SvpPPol, SvpPPolOps, VecZnxDft};
|
||||
use std::cmp::min;
|
||||
@@ -46,26 +50,24 @@ pub fn decrypt_rlwe_thread_safe(
|
||||
sk: &SvpPPol,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
let limbs: usize = min(res.limbs(), a.limbs());
|
||||
|
||||
assert!(
|
||||
tmp_bytes.len() >= decrypt_rlwe_thread_safe_tmp_byte(module, limbs),
|
||||
tmp_bytes.len() >= decrypt_rlwe_thread_safe_tmp_byte(module, a.limbs()),
|
||||
"invalid tmp_bytes: tmp_bytes.len()={} < decrypt_rlwe_thread_safe_tmp_byte={}",
|
||||
tmp_bytes.len(),
|
||||
decrypt_rlwe_thread_safe_tmp_byte(module, limbs)
|
||||
decrypt_rlwe_thread_safe_tmp_byte(module, a.limbs())
|
||||
);
|
||||
|
||||
let res_dft_bytes: usize = module.bytes_of_vec_znx_dft(limbs);
|
||||
let res_dft_bytes: usize = module.bytes_of_vec_znx_dft(a.limbs());
|
||||
|
||||
let mut res_dft: VecZnxDft = VecZnxDft::from_bytes(limbs, tmp_bytes);
|
||||
let mut res_dft: VecZnxDft = VecZnxDft::from_bytes(a.limbs(), tmp_bytes);
|
||||
let mut res_big: base2k::VecZnxBig = res_dft.as_vec_znx_big();
|
||||
|
||||
// res_dft <- DFT(ct[1]) * DFT(sk)
|
||||
module.svp_apply_dft(&mut res_dft, sk, &a.value[1], limbs);
|
||||
module.svp_apply_dft(&mut res_dft, sk, &a.value[1], a.limbs());
|
||||
// res_big <- ct[1] x sk
|
||||
module.vec_znx_idft_tmp_a(&mut res_big, &mut res_dft, limbs);
|
||||
module.vec_znx_idft_tmp_a(&mut res_big, &mut res_dft, a.limbs());
|
||||
// res_big <- ct[1] x sk + ct[0]
|
||||
module.vec_znx_big_add_small_inplace(&mut res_big, &a.value[0], limbs);
|
||||
module.vec_znx_big_add_small_inplace(&mut res_big, &a.value[0]);
|
||||
// res <- normalize(ct[1] x sk + ct[0])
|
||||
module.vec_znx_big_normalize(
|
||||
a.log_base2k(),
|
||||
|
||||
@@ -91,7 +91,7 @@ impl Elem {
|
||||
&mut self.value[i]
|
||||
}
|
||||
|
||||
pub fn zero(&mut self){
|
||||
pub fn zero(&mut self) {
|
||||
self.value.iter_mut().for_each(|i| i.zero());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,7 +175,7 @@ pub fn encrypt_rlwe_sk_thread_safe(
|
||||
}
|
||||
|
||||
// c0 <- -s x c1 + m + e
|
||||
//c0.add_normal(log_base2k, log_q, source_xe, sigma, (sigma * 6.0).ceil());
|
||||
c0.add_normal(log_base2k, log_q, source_xe, sigma, (sigma * 6.0).ceil());
|
||||
}
|
||||
|
||||
pub fn encrypt_grlwe_sk_tmp_bytes(
|
||||
@@ -245,6 +245,7 @@ pub fn encrypt_grlwe_sk_thread_safe(
|
||||
// Zeroes the ith-row of tmp_pt
|
||||
tmp_pt.0.value[0].at_mut(row_i).fill(0);
|
||||
|
||||
/*
|
||||
let mut res: Elem = Elem::new(module, log_base2k, log_q, 0, tmp_elem.log_scale);
|
||||
|
||||
decrypt_rlwe_thread_safe(module, &mut res, &tmp_elem, sk, tmp_bytes_encrypt_sk);
|
||||
@@ -252,6 +253,7 @@ pub fn encrypt_grlwe_sk_thread_safe(
|
||||
println!("row:{}", row_i);
|
||||
res.value[0].print_limbs(res.limbs(), 16);
|
||||
println!();
|
||||
*/
|
||||
|
||||
// GRLWE[row_i][0] = -as + m * 2^{-i*log_base2k} + e*2^{-log_q}
|
||||
module.vmp_prepare_row(
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
use crate::ciphertext::{Ciphertext, GadgetCiphertext};
|
||||
use base2k::{Module, VecZnxBig, VecZnxDft, VmpPMatOps};
|
||||
use crate::{
|
||||
ciphertext::{Ciphertext, GadgetCiphertext},
|
||||
elem::Elem,
|
||||
};
|
||||
use base2k::{Infos, Module, VecZnx, VecZnxBig, VecZnxDft, VmpPMatOps};
|
||||
|
||||
pub fn gadget_product_tmp_bytes(
|
||||
module: &Module,
|
||||
@@ -16,34 +19,27 @@ pub fn gadget_product_tmp_bytes(
|
||||
+ 2 * module.bytes_of_vec_znx_dft(gct_cols)
|
||||
}
|
||||
|
||||
pub fn gadget_product_inplace_thread_safe(
|
||||
module: &Module,
|
||||
a: &mut Ciphertext,
|
||||
b: &GadgetCiphertext,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
// This is safe to do because the relevant values of a are copied to a buffer before being
|
||||
// overwritten.
|
||||
unsafe {
|
||||
let a_ptr: *mut Ciphertext = a;
|
||||
gadget_product_thread_safe(module, a, &*a_ptr, b, tmp_bytes)
|
||||
}
|
||||
}
|
||||
|
||||
/// Evaluates the gadget product res <- a x b.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `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)
|
||||
/// containing b_nrows [VecZnx], each of b_ncols limbs.
|
||||
///
|
||||
/// # Computation
|
||||
///
|
||||
/// 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(
|
||||
module: &Module,
|
||||
res: &mut Ciphertext,
|
||||
a: &Ciphertext,
|
||||
res: &mut Elem,
|
||||
a: &VecZnx,
|
||||
b: &GadgetCiphertext,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
assert!(
|
||||
a.log_base2k() == b.log_base2k(),
|
||||
"invalid inputs: a.log_base2k={} != b.log_base2k={}",
|
||||
a.log_base2k(),
|
||||
b.log_base2k()
|
||||
);
|
||||
|
||||
let log_base2k: usize = b.log_base2k();
|
||||
let cols: usize = b.cols();
|
||||
|
||||
@@ -57,7 +53,7 @@ pub fn gadget_product_thread_safe(
|
||||
let mut res_big: VecZnxBig = res_dft.as_vec_znx_big();
|
||||
|
||||
// c1_dft <- DFT(c1) [cols]
|
||||
module.vec_znx_dft(&mut c1_dft, a.at(1), a.limbs());
|
||||
module.vec_znx_dft(&mut c1_dft, a, a.limbs());
|
||||
|
||||
// res_dft <- sum[rows] DFT(c1)[cols] x GadgetCiphertext[0][cols]
|
||||
module.vmp_apply_dft_to_dft(&mut res_dft, &c1_dft, &b.value[0], tmp_bytes_vmp_apply_dft);
|
||||
@@ -65,9 +61,6 @@ pub fn gadget_product_thread_safe(
|
||||
// res_big <- IDFT(DFT(c1) x GadgetCiphertext[0])
|
||||
module.vec_znx_idft_tmp_a(&mut res_big, &mut res_dft, cols);
|
||||
|
||||
// res_big <- c0 + c1_dft x GadgetCiphertext[0]
|
||||
module.vec_znx_big_add_small_inplace(&mut res_big, a.at(0), cols);
|
||||
|
||||
// res[0] = normalize(c0 + c1_dft x GadgetCiphertext[0])
|
||||
module.vec_znx_big_normalize(log_base2k, res.at_mut(0), &res_big, tmp_bytes_vmp_apply_dft);
|
||||
|
||||
|
||||
@@ -65,19 +65,19 @@ impl SwitchingKey {
|
||||
SwitchingKey(GadgetCiphertext::new(module, log_base2k, rows, log_q))
|
||||
}
|
||||
|
||||
pub fn n(&self) -> usize{
|
||||
pub fn n(&self) -> usize {
|
||||
self.0.n()
|
||||
}
|
||||
|
||||
pub fn rows(&self) -> usize{
|
||||
pub fn rows(&self) -> usize {
|
||||
self.0.rows()
|
||||
}
|
||||
|
||||
pub fn cols(&self) -> usize{
|
||||
pub fn cols(&self) -> usize {
|
||||
self.0.cols()
|
||||
}
|
||||
|
||||
pub fn log_base2k(&self) -> usize{
|
||||
pub fn log_base2k(&self) -> usize {
|
||||
self.0.log_base2k()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ impl Plaintext {
|
||||
self.0.log_scale()
|
||||
}
|
||||
|
||||
pub fn zero(&mut self){
|
||||
pub fn zero(&mut self) {
|
||||
self.0.zero()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user