mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 13:16:44 +01:00
prototype trait for Elem<T> + new ciphertext for VmPPmat
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
use base2k::{
|
||||
FFT64, Module, Sampling, SvpPPolOps, VecZnx, VecZnxApi, VecZnxBig, VecZnxDft, VecZnxDftOps,
|
||||
VmpPMat, VmpPMatOps, alloc_aligned_u8,
|
||||
FFT64, Module, Sampling, SvpPPolOps, VecZnx, VecZnxBig, VecZnxDft, VecZnxDftOps, VmpPMat,
|
||||
VmpPMatOps, alloc_aligned_u8,
|
||||
};
|
||||
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
|
||||
use rlwe::{
|
||||
ciphertext::{Ciphertext, GadgetCiphertext},
|
||||
ciphertext::{Ciphertext, new_gadget_ciphertext},
|
||||
elem::Elem,
|
||||
encryptor::{encrypt_grlwe_sk_thread_safe, encrypt_grlwe_sk_tmp_bytes},
|
||||
evaluator::gadget_product_tmp_bytes,
|
||||
@@ -18,7 +18,7 @@ fn gadget_product_inplace(c: &mut Criterion) {
|
||||
fn gadget_product<'a>(
|
||||
module: &'a Module,
|
||||
elem: &'a mut Elem<VecZnx>,
|
||||
gadget_ct: &'a GadgetCiphertext,
|
||||
gadget_ct: &'a Ciphertext<VmpPMat>,
|
||||
tmp_bytes: &'a mut [u8],
|
||||
) -> Box<dyn FnMut() + 'a> {
|
||||
let factor: usize = 2;
|
||||
@@ -105,7 +105,7 @@ fn gadget_product_inplace(c: &mut Criterion) {
|
||||
let mut sk1_svp_ppol: base2k::SvpPPol = params.module().new_svp_ppol();
|
||||
params.module().svp_prepare(&mut sk1_svp_ppol, &sk1.0);
|
||||
|
||||
let mut gadget_ct: GadgetCiphertext = GadgetCiphertext::new(
|
||||
let mut gadget_ct: Ciphertext<VmpPMat> = new_gadget_ciphertext(
|
||||
params.module(),
|
||||
params.log_base2k(),
|
||||
params.limbs_q(),
|
||||
@@ -123,7 +123,7 @@ fn gadget_product_inplace(c: &mut Criterion) {
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
let mut ct: Ciphertext = params.new_ciphertext(params.log_q());
|
||||
let mut ct: Ciphertext<VecZnx> = params.new_ciphertext(params.log_q());
|
||||
|
||||
params.encrypt_rlwe_sk_thread_safe(
|
||||
&mut ct,
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
use base2k::{Encoding, FFT64, SvpPPolOps, VecZnxApi, VecZnx};
|
||||
use base2k::{Encoding, FFT64, SvpPPolOps, VecZnx, VecZnxApi};
|
||||
use rlwe::{
|
||||
ciphertext::Ciphertext,
|
||||
decryptor::{Decryptor, decrypt_rlwe_thread_safe_tmp_byte},
|
||||
encryptor::{EncryptorSk, encrypt_rlwe_sk_tmp_bytes},
|
||||
keys::SecretKey,
|
||||
parameters::{Parameters, ParametersLiteral},
|
||||
plaintext::Plaintext,
|
||||
};
|
||||
use sampling::source::{Source, new_seed};
|
||||
use sampling::source::Source;
|
||||
|
||||
fn main() {
|
||||
let params_lit: ParametersLiteral = ParametersLiteral {
|
||||
@@ -30,8 +28,7 @@ fn main() {
|
||||
|
||||
let mut source: Source = Source::new([0; 32]);
|
||||
let mut sk: SecretKey = SecretKey::new(params.module());
|
||||
//sk.fill_ternary_hw(params.xs(), &mut source);
|
||||
sk.0.0[0] = 1;
|
||||
sk.fill_ternary_hw(params.xs(), &mut source);
|
||||
|
||||
let mut want = vec![i64::default(); params.n()];
|
||||
|
||||
@@ -47,10 +44,10 @@ fn main() {
|
||||
pt.0.value[0].normalize(log_base2k, &mut tmp_bytes);
|
||||
|
||||
println!("log_k: {}", log_k);
|
||||
pt.0.value[0].print_limbs(pt.limbs(), 16);
|
||||
pt.0.value[0].print(pt.cols(), 16);
|
||||
println!();
|
||||
|
||||
let mut ct: Ciphertext = params.new_ciphertext(params.log_q());
|
||||
let mut ct: Ciphertext<VecZnx> = params.new_ciphertext(params.log_q());
|
||||
|
||||
let mut source_xe: Source = Source::new([1; 32]);
|
||||
let mut source_xa: Source = Source::new([2; 32]);
|
||||
@@ -69,7 +66,7 @@ fn main() {
|
||||
|
||||
params.decrypt_rlwe_thread_safe(&mut pt, &ct, &sk_svp_ppol, &mut tmp_bytes);
|
||||
|
||||
pt.0.value[0].print_limbs(pt.limbs(), 16);
|
||||
pt.0.value[0].print(pt.cols(), 16);
|
||||
|
||||
let mut have = vec![i64::default(); params.n()];
|
||||
|
||||
|
||||
@@ -1,22 +1,15 @@
|
||||
use base2k::{
|
||||
Encoding, FFT64, Infos, Sampling, Scalar, SvpPPolOps, VecZnx, VecZnxApi, VecZnxBig, VecZnxDft,
|
||||
VecZnxOps,
|
||||
};
|
||||
use base2k::{Encoding, FFT64, SvpPPolOps, VecZnx, VecZnxApi, VmpPMat};
|
||||
use rlwe::{
|
||||
ciphertext::{Ciphertext, GadgetCiphertext},
|
||||
decryptor::{Decryptor, decrypt_rlwe_thread_safe, decrypt_rlwe_thread_safe_tmp_byte},
|
||||
elem::Elem,
|
||||
encryptor::{
|
||||
EncryptorSk, encrypt_grlwe_sk_thread_safe, encrypt_grlwe_sk_tmp_bytes,
|
||||
encrypt_rlwe_sk_tmp_bytes,
|
||||
},
|
||||
ciphertext::{Ciphertext, new_gadget_ciphertext},
|
||||
decryptor::decrypt_rlwe_thread_safe,
|
||||
encryptor::{encrypt_grlwe_sk_thread_safe, encrypt_grlwe_sk_tmp_bytes},
|
||||
evaluator::{gadget_product_inplace_thread_safe, gadget_product_tmp_bytes},
|
||||
key_generator::{gen_switching_key_thread_safe, gen_switching_key_thread_safe_tmp_bytes},
|
||||
keys::{SecretKey, SwitchingKey},
|
||||
key_generator::gen_switching_key_thread_safe_tmp_bytes,
|
||||
keys::SecretKey,
|
||||
parameters::{Parameters, ParametersLiteral},
|
||||
plaintext::Plaintext,
|
||||
};
|
||||
use sampling::source::{Source, new_seed};
|
||||
use sampling::source::Source;
|
||||
|
||||
fn main() {
|
||||
let params_lit: ParametersLiteral = ParametersLiteral {
|
||||
@@ -82,7 +75,7 @@ fn main() {
|
||||
let mut sk1_svp_ppol: base2k::SvpPPol = params.module().new_svp_ppol();
|
||||
params.module().svp_prepare(&mut sk1_svp_ppol, &sk1.0);
|
||||
|
||||
let mut gadget_ct: GadgetCiphertext = GadgetCiphertext::new(
|
||||
let mut gadget_ct: Ciphertext<VmpPMat> = new_gadget_ciphertext(
|
||||
params.module(),
|
||||
log_base2k,
|
||||
params.limbs_q(),
|
||||
@@ -100,21 +93,15 @@ fn main() {
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
println!("DONE?");
|
||||
|
||||
let mut pt: Plaintext<VecZnx> = Plaintext::<VecZnx>::new(
|
||||
params.module(),
|
||||
params.log_base2k(),
|
||||
params.log_q(),
|
||||
params.log_scale(),
|
||||
);
|
||||
let mut pt: Plaintext<VecZnx> =
|
||||
Plaintext::<VecZnx>::new(params.module(), params.log_base2k(), params.log_q());
|
||||
|
||||
let mut want = vec![i64::default(); params.n()];
|
||||
want.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
|
||||
pt.0.value[0].encode_vec_i64(log_base2k, log_k, &want, 32);
|
||||
pt.0.value[0].normalize(log_base2k, &mut tmp_bytes);
|
||||
|
||||
let mut ct: Ciphertext = params.new_ciphertext(params.log_q());
|
||||
let mut ct: Ciphertext<VecZnx> = params.new_ciphertext(params.log_q());
|
||||
|
||||
params.encrypt_rlwe_sk_thread_safe(
|
||||
&mut ct,
|
||||
@@ -132,10 +119,10 @@ fn main() {
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
println!("ct.limbs()={}", ct.limbs());
|
||||
println!("ct.limbs()={}", ct.cols());
|
||||
println!("gadget_ct.rows()={}", gadget_ct.rows());
|
||||
println!("gadget_ct.cols()={}", gadget_ct.cols());
|
||||
println!("res.limbs()={}", ct.limbs());
|
||||
println!("res.limbs()={}", ct.cols());
|
||||
println!();
|
||||
|
||||
decrypt_rlwe_thread_safe(
|
||||
@@ -146,7 +133,7 @@ fn main() {
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
pt.0.value[0].print_limbs(pt.limbs(), 16);
|
||||
pt.0.value[0].print(pt.cols(), 16);
|
||||
|
||||
let mut have: Vec<i64> = vec![i64::default(); params.n()];
|
||||
|
||||
|
||||
@@ -1,47 +1,47 @@
|
||||
use crate::elem::{Elem, ElemBasics};
|
||||
use crate::elem::{Elem, ElemVecZnx, VecZnxCommon};
|
||||
use crate::parameters::Parameters;
|
||||
use crate::plaintext::Plaintext;
|
||||
use base2k::{Infos, Module, VecZnx, VecZnxApi, VmpPMat, VmpPMatOps};
|
||||
use base2k::{Infos, Module, VecZnx, VecZnxApi, VmpPMat};
|
||||
|
||||
pub struct Ciphertext(pub Elem<VecZnx>);
|
||||
pub struct Ciphertext<T>(pub Elem<T>);
|
||||
|
||||
impl Ciphertext {
|
||||
pub fn new(
|
||||
module: &Module,
|
||||
log_base2k: usize,
|
||||
log_q: usize,
|
||||
degree: usize,
|
||||
log_scale: usize,
|
||||
) -> Self {
|
||||
Self(Elem::new(module, log_base2k, log_q, degree, log_scale))
|
||||
impl Ciphertext<VecZnx> {
|
||||
pub fn new(module: &Module, log_base2k: usize, log_q: usize, rows: usize) -> Self {
|
||||
Self(Elem::<VecZnx>::new(module, log_base2k, log_q, rows))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Ciphertext<T>
|
||||
where
|
||||
T: VecZnxCommon,
|
||||
Elem<T>: Infos + ElemVecZnx<T>,
|
||||
{
|
||||
pub fn n(&self) -> usize {
|
||||
self.0.n()
|
||||
}
|
||||
|
||||
pub fn degree(&self) -> usize {
|
||||
self.0.degree()
|
||||
}
|
||||
|
||||
pub fn log_q(&self) -> usize {
|
||||
self.0.log_q()
|
||||
self.0.log_q
|
||||
}
|
||||
|
||||
pub fn limbs(&self) -> usize {
|
||||
self.0.limbs()
|
||||
pub fn rows(&self) -> usize {
|
||||
self.0.rows()
|
||||
}
|
||||
|
||||
pub fn at(&self, i: usize) -> &(impl VecZnxApi + Infos) {
|
||||
pub fn cols(&self) -> usize {
|
||||
self.0.cols()
|
||||
}
|
||||
|
||||
pub fn at(&self, i: usize) -> &T {
|
||||
self.0.at(i)
|
||||
}
|
||||
|
||||
pub fn at_mut(&mut self, i: usize) -> &mut (impl VecZnxApi + Infos) {
|
||||
pub fn at_mut(&mut self, i: usize) -> &mut T {
|
||||
self.0.at_mut(i)
|
||||
}
|
||||
|
||||
pub fn log_base2k(&self) -> usize {
|
||||
self.0.log_base2k()
|
||||
self.0.log_base2k
|
||||
}
|
||||
|
||||
pub fn log_scale(&self) -> usize {
|
||||
@@ -52,87 +52,59 @@ impl Ciphertext {
|
||||
self.0.zero()
|
||||
}
|
||||
|
||||
pub fn as_plaintext(&self) -> Plaintext<VecZnx> {
|
||||
unsafe { Plaintext(std::ptr::read(&self.0)) }
|
||||
pub fn as_plaintext(&self) -> Plaintext<T> {
|
||||
unsafe { Plaintext::<T>(std::ptr::read(&self.0)) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Parameters {
|
||||
pub fn new_ciphertext(&self, log_q: usize) -> Ciphertext {
|
||||
Ciphertext::new(self.module(), self.log_base2k(), log_q, self.log_scale(), 1)
|
||||
pub fn new_ciphertext(&self, log_q: usize) -> Ciphertext<VecZnx> {
|
||||
Ciphertext::new(self.module(), self.log_base2k(), log_q, 2)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GadgetCiphertext {
|
||||
pub value: VmpPMat,
|
||||
pub log_base2k: usize,
|
||||
pub log_q: usize,
|
||||
pub fn new_gadget_ciphertext(
|
||||
module: &Module,
|
||||
log_base2k: usize,
|
||||
rows: usize,
|
||||
log_q: usize,
|
||||
) -> Ciphertext<VmpPMat> {
|
||||
let cols: usize = (log_q + log_base2k - 1) / log_base2k;
|
||||
let mut elem: Elem<VmpPMat> = Elem::<VmpPMat>::new(module, log_base2k, rows, 2 * cols);
|
||||
elem.log_q = log_q;
|
||||
Ciphertext(elem)
|
||||
}
|
||||
|
||||
impl GadgetCiphertext {
|
||||
pub fn new(module: &Module, log_base2k: usize, rows: usize, log_q: usize) -> Self {
|
||||
let cols: usize = (log_q + log_base2k - 1) / log_base2k;
|
||||
Self {
|
||||
value: module.new_vmp_pmat(rows, cols * 2),
|
||||
log_base2k,
|
||||
log_q,
|
||||
}
|
||||
}
|
||||
pub fn new_rgsw_ciphertext(
|
||||
module: &Module,
|
||||
log_base2k: usize,
|
||||
rows: usize,
|
||||
log_q: usize,
|
||||
) -> Ciphertext<VmpPMat> {
|
||||
let cols: usize = (log_q + log_base2k - 1) / log_base2k;
|
||||
let mut elem: Elem<VmpPMat> = Elem::<VmpPMat>::new(module, log_base2k, 2 * rows, 2 * cols);
|
||||
elem.log_q = log_q;
|
||||
Ciphertext(elem)
|
||||
}
|
||||
|
||||
impl Ciphertext<VmpPMat> {
|
||||
pub fn n(&self) -> usize {
|
||||
self.value.n
|
||||
self.0.n()
|
||||
}
|
||||
|
||||
pub fn rows(&self) -> usize {
|
||||
self.value.rows
|
||||
self.0.rows()
|
||||
}
|
||||
|
||||
pub fn cols(&self) -> usize {
|
||||
self.value.cols
|
||||
}
|
||||
|
||||
pub fn log_q(&self) -> usize {
|
||||
self.log_q
|
||||
self.0.cols()
|
||||
}
|
||||
|
||||
pub fn log_base2k(&self) -> usize {
|
||||
self.log_base2k
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RGSWCiphertext {
|
||||
pub value: VmpPMat,
|
||||
pub log_base2k: usize,
|
||||
pub log_q: usize,
|
||||
}
|
||||
|
||||
impl RGSWCiphertext {
|
||||
pub fn new(module: &Module, log_base2k: usize, rows: usize, log_q: usize) -> Self {
|
||||
let cols: usize = (log_q + log_base2k - 1) / log_base2k;
|
||||
Self {
|
||||
value: module.new_vmp_pmat(rows * 2, cols * 2),
|
||||
log_base2k,
|
||||
log_q,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn n(&self) -> usize {
|
||||
self.value.n
|
||||
}
|
||||
|
||||
pub fn rows(&self) -> usize {
|
||||
self.value.rows
|
||||
}
|
||||
|
||||
pub fn cols(&self) -> usize {
|
||||
self.value.cols
|
||||
self.0.log_base2k
|
||||
}
|
||||
|
||||
pub fn log_q(&self) -> usize {
|
||||
self.log_q
|
||||
}
|
||||
|
||||
pub fn log_base2k(&self) -> usize {
|
||||
self.log_base2k
|
||||
self.0.log_q
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
use crate::{
|
||||
ciphertext::Ciphertext,
|
||||
elem::{Elem, ElemBasics},
|
||||
elem::{Elem, ElemVecZnx, VecZnxCommon},
|
||||
keys::SecretKey,
|
||||
parameters::Parameters,
|
||||
plaintext::Plaintext,
|
||||
};
|
||||
use base2k::{
|
||||
Infos, VecZnx, Module, SvpPPol, SvpPPolOps, VecZnxApi, VecZnxBigOps, VecZnxDft, VecZnxDftOps,
|
||||
Infos, Module, SvpPPol, SvpPPolOps, VecZnx, VecZnxApi, VecZnxBigOps, VecZnxDft, VecZnxDftOps,
|
||||
};
|
||||
use std::cmp::min;
|
||||
|
||||
@@ -34,13 +34,16 @@ impl Parameters {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn decrypt_rlwe_thread_safe(
|
||||
pub fn decrypt_rlwe_thread_safe<T>(
|
||||
&self,
|
||||
res: &mut Plaintext<VecZnx>,
|
||||
ct: &Ciphertext,
|
||||
res: &mut Plaintext<T>,
|
||||
ct: &Ciphertext<T>,
|
||||
sk: &SvpPPol,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
) where
|
||||
T: VecZnxCommon,
|
||||
Elem<T>: Infos + ElemVecZnx<T>,
|
||||
{
|
||||
decrypt_rlwe_thread_safe(self.module(), &mut res.0, &ct.0, sk, tmp_bytes)
|
||||
}
|
||||
}
|
||||
@@ -52,26 +55,29 @@ pub fn decrypt_rlwe_thread_safe<T>(
|
||||
sk: &SvpPPol,
|
||||
tmp_bytes: &mut [u8],
|
||||
) where
|
||||
T: VecZnxApi + Infos,
|
||||
T: VecZnxCommon,
|
||||
Elem<T>: Infos + ElemVecZnx<T>,
|
||||
{
|
||||
let cols: usize = a.cols();
|
||||
|
||||
assert!(
|
||||
tmp_bytes.len() >= decrypt_rlwe_thread_safe_tmp_byte(module, a.limbs()),
|
||||
tmp_bytes.len() >= decrypt_rlwe_thread_safe_tmp_byte(module, cols),
|
||||
"invalid tmp_bytes: tmp_bytes.len()={} < decrypt_rlwe_thread_safe_tmp_byte={}",
|
||||
tmp_bytes.len(),
|
||||
decrypt_rlwe_thread_safe_tmp_byte(module, a.limbs())
|
||||
decrypt_rlwe_thread_safe_tmp_byte(module, cols)
|
||||
);
|
||||
|
||||
let res_dft_bytes: usize = module.bytes_of_vec_znx_dft(a.limbs());
|
||||
let res_dft_bytes: usize = module.bytes_of_vec_znx_dft(cols);
|
||||
|
||||
let mut res_dft: VecZnxDft = VecZnxDft::from_bytes(a.limbs(), tmp_bytes);
|
||||
let mut res_dft: VecZnxDft = VecZnxDft::from_bytes(a.cols(), 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], a.limbs());
|
||||
module.svp_apply_dft(&mut res_dft, sk, a.at(1), cols);
|
||||
// res_big <- ct[1] x sk
|
||||
module.vec_znx_idft_tmp_a(&mut res_big, &mut res_dft, a.limbs());
|
||||
module.vec_znx_idft_tmp_a(&mut res_big, &mut res_dft, cols);
|
||||
// res_big <- ct[1] x sk + ct[0]
|
||||
module.vec_znx_big_add_small_inplace(&mut res_big, &a.value[0]);
|
||||
module.vec_znx_big_add_small_inplace(&mut res_big, a.at(0));
|
||||
// res <- normalize(ct[1] x sk + ct[0])
|
||||
module.vec_znx_big_normalize(
|
||||
a.log_base2k(),
|
||||
|
||||
248
rlwe/src/elem.rs
248
rlwe/src/elem.rs
@@ -1,82 +1,68 @@
|
||||
use crate::parameters::Parameters;
|
||||
use base2k::{Infos, Module, VecZnx, VecZnxApi, VecZnxBorrow, VecZnxOps};
|
||||
use base2k::{Infos, Module, VecZnx, VecZnxApi, VecZnxBorrow, VecZnxOps, VmpPMat, VmpPMatOps};
|
||||
|
||||
use crate::parameters::Parameters;
|
||||
|
||||
impl Parameters {
|
||||
pub fn bytes_of_elem(&self, log_q: usize, degree: usize) -> usize {
|
||||
Elem::<VecZnx>::bytes_of(self.module(), self.log_base2k(), log_q, degree)
|
||||
}
|
||||
|
||||
pub fn elem_from_bytes(&self, log_q: usize, degree: usize, bytes: &mut [u8]) -> Elem<VecZnx> {
|
||||
Elem::<VecZnx>::from_bytes(self.module(), self.log_base2k(), log_q, degree, bytes)
|
||||
}
|
||||
|
||||
pub fn elem_borrow_from_bytes(&self, log_q: usize, degree: usize, bytes: &mut [u8]) -> Elem<VecZnxBorrow> {
|
||||
Elem::<VecZnxBorrow>::from_bytes(self.module(), self.log_base2k(), log_q, degree, bytes)
|
||||
pub fn elem_from_bytes<T>(&self, log_q: usize, rows: usize, bytes: &mut [u8]) -> Elem<T>
|
||||
where
|
||||
T: VecZnxCommon,
|
||||
Elem<T>: Infos + ElemVecZnx<T>,
|
||||
{
|
||||
Elem::<T>::from_bytes(self.module(), self.log_base2k(), log_q, rows, bytes)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Elem<T: VecZnxApi + Infos> {
|
||||
pub struct Elem<T> {
|
||||
pub value: Vec<T>,
|
||||
pub log_base2k: usize,
|
||||
pub log_q: usize,
|
||||
pub log_scale: usize,
|
||||
}
|
||||
|
||||
pub trait ElemBasics<T>
|
||||
where
|
||||
T: VecZnxApi + Infos,
|
||||
{
|
||||
fn n(&self) -> usize;
|
||||
fn degree(&self) -> usize;
|
||||
fn limbs(&self) -> usize;
|
||||
fn log_base2k(&self) -> usize;
|
||||
fn log_scale(&self) -> usize;
|
||||
fn log_q(&self) -> usize;
|
||||
pub trait VecZnxCommon: VecZnxApi + Infos {}
|
||||
impl VecZnxCommon for VecZnx {}
|
||||
impl VecZnxCommon for VecZnxBorrow {}
|
||||
|
||||
pub trait ElemVecZnx<T: VecZnxCommon> {
|
||||
fn from_bytes(
|
||||
module: &Module,
|
||||
log_base2k: usize,
|
||||
log_q: usize,
|
||||
rows: usize,
|
||||
bytes: &mut [u8],
|
||||
) -> Elem<T>;
|
||||
fn bytes_of(module: &Module, log_base2k: usize, log_q: usize, rows: usize) -> usize;
|
||||
fn at(&self, i: usize) -> &T;
|
||||
fn at_mut(&mut self, i: usize) -> &mut T;
|
||||
fn zero(&mut self);
|
||||
}
|
||||
|
||||
impl Elem<VecZnx> {
|
||||
pub fn new(
|
||||
module: &Module,
|
||||
log_base2k: usize,
|
||||
log_q: usize,
|
||||
degree: usize,
|
||||
log_scale: usize,
|
||||
) -> Self {
|
||||
let limbs: usize = (log_q + log_base2k - 1) / log_base2k;
|
||||
let mut value: Vec<VecZnx> = Vec::new();
|
||||
(0..degree + 1).for_each(|_| value.push(module.new_vec_znx(limbs)));
|
||||
Self {
|
||||
value,
|
||||
log_q,
|
||||
log_base2k,
|
||||
log_scale: log_scale,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bytes_of(module: &Module, log_base2k: usize, log_q: usize, degree: usize) -> usize {
|
||||
impl<T> ElemVecZnx<T> for Elem<T>
|
||||
where
|
||||
T: VecZnxCommon<Owned = T>,
|
||||
Elem<T>: Infos,
|
||||
{
|
||||
fn bytes_of(module: &Module, log_base2k: usize, log_q: usize, rows: usize) -> usize {
|
||||
let cols = (log_q + log_base2k - 1) / log_base2k;
|
||||
module.n() * cols * (degree + 1) * 8
|
||||
module.n() * cols * (rows + 1) * 8
|
||||
}
|
||||
|
||||
pub fn from_bytes(
|
||||
fn from_bytes(
|
||||
module: &Module,
|
||||
log_base2k: usize,
|
||||
log_q: usize,
|
||||
degree: usize,
|
||||
rows: usize,
|
||||
bytes: &mut [u8],
|
||||
) -> Self {
|
||||
) -> Elem<T> {
|
||||
assert!(rows > 0);
|
||||
let n: usize = module.n();
|
||||
assert!(bytes.len() >= Self::bytes_of(module, log_base2k, log_q, degree));
|
||||
let mut value: Vec<VecZnx> = Vec::new();
|
||||
assert!(bytes.len() >= Self::bytes_of(module, log_base2k, log_q, rows));
|
||||
let mut value: Vec<T> = Vec::new();
|
||||
let limbs: usize = (log_q + log_base2k - 1) / log_base2k;
|
||||
let size = VecZnx::bytes_of(n, limbs);
|
||||
let size = T::bytes_of(n, limbs);
|
||||
let mut ptr: usize = 0;
|
||||
(0..degree + 1).for_each(|_| {
|
||||
value.push(VecZnx::from_bytes(n, limbs, &mut bytes[ptr..]));
|
||||
(0..rows).for_each(|_| {
|
||||
value.push(T::from_bytes(n, limbs, &mut bytes[ptr..]));
|
||||
ptr += size
|
||||
});
|
||||
Self {
|
||||
@@ -86,74 +72,14 @@ impl Elem<VecZnx> {
|
||||
log_scale: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Elem<VecZnxBorrow> {
|
||||
|
||||
pub fn bytes_of(module: &Module, log_base2k: usize, log_q: usize, degree: usize) -> usize {
|
||||
let cols = (log_q + log_base2k - 1) / log_base2k;
|
||||
module.n() * cols * (degree + 1) * 8
|
||||
}
|
||||
|
||||
pub fn from_bytes(
|
||||
module: &Module,
|
||||
log_base2k: usize,
|
||||
log_q: usize,
|
||||
degree: usize,
|
||||
bytes: &mut [u8],
|
||||
) -> Self {
|
||||
let n: usize = module.n();
|
||||
assert!(bytes.len() >= Self::bytes_of(module, log_base2k, log_q, degree));
|
||||
let mut value: Vec<VecZnxBorrow> = Vec::new();
|
||||
let limbs: usize = (log_q + log_base2k - 1) / log_base2k;
|
||||
let size = VecZnxBorrow::bytes_of(n, limbs);
|
||||
let mut ptr: usize = 0;
|
||||
(0..degree + 1).for_each(|_| {
|
||||
value.push(VecZnxBorrow::from_bytes(n, limbs, &mut bytes[ptr..]));
|
||||
ptr += size
|
||||
});
|
||||
Self {
|
||||
value,
|
||||
log_q,
|
||||
log_base2k,
|
||||
log_scale: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<T: VecZnxApi + Infos> ElemBasics<T> for Elem<T> {
|
||||
fn n(&self) -> usize {
|
||||
self.value[0].n()
|
||||
}
|
||||
|
||||
fn degree(&self) -> usize {
|
||||
self.value.len()
|
||||
}
|
||||
|
||||
fn limbs(&self) -> usize {
|
||||
self.value[0].limbs()
|
||||
}
|
||||
|
||||
fn log_base2k(&self) -> usize {
|
||||
self.log_base2k
|
||||
}
|
||||
|
||||
fn log_scale(&self) -> usize {
|
||||
self.log_scale
|
||||
}
|
||||
|
||||
fn log_q(&self) -> usize {
|
||||
self.log_q
|
||||
}
|
||||
|
||||
fn at(&self, i: usize) -> &T {
|
||||
assert!(i <= self.degree());
|
||||
assert!(i < self.rows());
|
||||
&self.value[i]
|
||||
}
|
||||
|
||||
fn at_mut(&mut self, i: usize) -> &mut T {
|
||||
assert!(i <= self.degree());
|
||||
assert!(i < self.rows());
|
||||
&mut self.value[i]
|
||||
}
|
||||
|
||||
@@ -161,3 +87,97 @@ impl<T: VecZnxApi + Infos> ElemBasics<T> for Elem<T> {
|
||||
self.value.iter_mut().for_each(|i| i.zero());
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Elem<T> {
|
||||
pub fn log_base2k(&self) -> usize {
|
||||
self.log_base2k
|
||||
}
|
||||
|
||||
pub fn log_q(&self) -> usize {
|
||||
self.log_q
|
||||
}
|
||||
|
||||
pub fn log_scale(&self) -> usize {
|
||||
self.log_scale
|
||||
}
|
||||
}
|
||||
|
||||
impl Infos for Elem<VecZnx> {
|
||||
fn n(&self) -> usize {
|
||||
self.value[0].n()
|
||||
}
|
||||
|
||||
fn log_n(&self) -> usize {
|
||||
self.value[0].log_n()
|
||||
}
|
||||
|
||||
fn rows(&self) -> usize {
|
||||
self.value.len()
|
||||
}
|
||||
fn cols(&self) -> usize {
|
||||
self.value[0].cols()
|
||||
}
|
||||
}
|
||||
|
||||
impl Infos for Elem<VecZnxBorrow> {
|
||||
fn n(&self) -> usize {
|
||||
self.value[0].n()
|
||||
}
|
||||
|
||||
fn log_n(&self) -> usize {
|
||||
self.value[0].log_n()
|
||||
}
|
||||
|
||||
fn rows(&self) -> usize {
|
||||
self.value.len()
|
||||
}
|
||||
fn cols(&self) -> usize {
|
||||
self.value[0].cols()
|
||||
}
|
||||
}
|
||||
|
||||
impl Elem<VecZnx> {
|
||||
pub fn new(module: &Module, log_base2k: usize, log_q: usize, rows: usize) -> Self {
|
||||
assert!(rows > 0);
|
||||
let limbs: usize = (log_q + log_base2k - 1) / log_base2k;
|
||||
let mut value: Vec<VecZnx> = Vec::new();
|
||||
(0..rows).for_each(|_| value.push(module.new_vec_znx(limbs)));
|
||||
Self {
|
||||
value,
|
||||
log_q,
|
||||
log_base2k,
|
||||
log_scale: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Infos for Elem<VmpPMat> {
|
||||
fn n(&self) -> usize {
|
||||
self.value[0].n()
|
||||
}
|
||||
|
||||
fn log_n(&self) -> usize {
|
||||
self.value[0].log_n()
|
||||
}
|
||||
|
||||
fn rows(&self) -> usize {
|
||||
self.value[0].rows()
|
||||
}
|
||||
|
||||
fn cols(&self) -> usize {
|
||||
self.value[0].cols()
|
||||
}
|
||||
}
|
||||
|
||||
impl Elem<VmpPMat> {
|
||||
pub fn new(module: &Module, log_base2k: usize, rows: usize, cols: usize) -> Self {
|
||||
assert!(rows > 0);
|
||||
assert!(cols > 0);
|
||||
Self {
|
||||
value: Vec::from([module.new_vmp_pmat(rows, cols); 1]),
|
||||
log_q: 0,
|
||||
log_base2k: log_base2k,
|
||||
log_scale: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
use crate::ciphertext::{Ciphertext, GadgetCiphertext};
|
||||
use crate::elem::{Elem, ElemBasics};
|
||||
use crate::ciphertext::Ciphertext;
|
||||
use crate::elem::{Elem, ElemVecZnx, VecZnxCommon};
|
||||
use crate::keys::SecretKey;
|
||||
use crate::parameters::Parameters;
|
||||
use crate::plaintext::Plaintext;
|
||||
use base2k::sampling::Sampling;
|
||||
use base2k::{
|
||||
cast_mut, Infos, VecZnxBorrow, Module, Scalar, SvpPPol, SvpPPolOps, VecZnx, VecZnxApi, VecZnxBig, VecZnxBigOps,
|
||||
VecZnxDft, VecZnxDftOps, VecZnxOps, VmpPMatOps, alloc_aligned_u8, cast,
|
||||
Infos, Module, Scalar, SvpPPol, SvpPPolOps, VecZnx, VecZnxApi, VecZnxBig, VecZnxBigOps,
|
||||
VecZnxBorrow, VecZnxDft, VecZnxDftOps, VecZnxOps, VmpPMat, VmpPMatOps, cast_mut,
|
||||
};
|
||||
use rand_distr::num_traits::ops::bytes;
|
||||
|
||||
use sampling::source::{Source, new_seed};
|
||||
|
||||
pub struct EncryptorSk {
|
||||
@@ -49,12 +49,15 @@ impl EncryptorSk {
|
||||
self.source_xe = Source::new(seed)
|
||||
}
|
||||
|
||||
pub fn encrypt_rlwe_sk(
|
||||
pub fn encrypt_rlwe_sk<T>(
|
||||
&mut self,
|
||||
params: &Parameters,
|
||||
ct: &mut Ciphertext,
|
||||
pt: Option<&Plaintext<VecZnx>>,
|
||||
) {
|
||||
ct: &mut Ciphertext<T>,
|
||||
pt: Option<&Plaintext<T>>,
|
||||
) where
|
||||
T: VecZnxCommon,
|
||||
Elem<T>: Infos + ElemVecZnx<T>,
|
||||
{
|
||||
assert!(
|
||||
self.initialized == true,
|
||||
"invalid call to [EncryptorSk.encrypt_rlwe_sk]: [EncryptorSk] has not been initialized with a [SecretKey]"
|
||||
@@ -69,15 +72,18 @@ impl EncryptorSk {
|
||||
);
|
||||
}
|
||||
|
||||
pub fn encrypt_rlwe_sk_thread_safe(
|
||||
pub fn encrypt_rlwe_sk_thread_safe<T>(
|
||||
&self,
|
||||
params: &Parameters,
|
||||
ct: &mut Ciphertext,
|
||||
pt: Option<&Plaintext<VecZnx>>,
|
||||
ct: &mut Ciphertext<T>,
|
||||
pt: Option<&Plaintext<T>>,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
) where
|
||||
T: VecZnxCommon,
|
||||
Elem<T>: Infos + ElemVecZnx<T>,
|
||||
{
|
||||
assert!(
|
||||
self.initialized == true,
|
||||
"invalid call to [EncryptorSk.encrypt_rlwe_sk_thread_safe]: [EncryptorSk] has not been initialized with a [SecretKey]"
|
||||
@@ -91,16 +97,19 @@ impl Parameters {
|
||||
encrypt_rlwe_sk_tmp_bytes(self.module(), self.log_base2k(), log_q)
|
||||
}
|
||||
|
||||
pub fn encrypt_rlwe_sk_thread_safe(
|
||||
pub fn encrypt_rlwe_sk_thread_safe<T>(
|
||||
&self,
|
||||
ct: &mut Ciphertext,
|
||||
pt: Option<&Plaintext<VecZnx>>,
|
||||
ct: &mut Ciphertext<T>,
|
||||
pt: Option<&Plaintext<T>>,
|
||||
sk: &SvpPPol,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
encrypt_rlwe_sk_thread_safe::<VecZnx>(
|
||||
) where
|
||||
T: VecZnxCommon,
|
||||
Elem<T>: Infos + ElemVecZnx<T>,
|
||||
{
|
||||
encrypt_rlwe_sk_thread_safe(
|
||||
self.module(),
|
||||
&mut ct.0,
|
||||
pt.map(|pt| &pt.0),
|
||||
@@ -128,9 +137,10 @@ pub fn encrypt_rlwe_sk_thread_safe<T>(
|
||||
sigma: f64,
|
||||
tmp_bytes: &mut [u8],
|
||||
) where
|
||||
T: VecZnxApi + Infos,
|
||||
T: VecZnxCommon,
|
||||
Elem<T>: Infos + ElemVecZnx<T>,
|
||||
{
|
||||
let limbs: usize = ct.limbs();
|
||||
let cols: usize = ct.cols();
|
||||
let log_base2k: usize = ct.log_base2k();
|
||||
let log_q: usize = ct.log_q();
|
||||
|
||||
@@ -146,22 +156,22 @@ pub fn encrypt_rlwe_sk_thread_safe<T>(
|
||||
let c1: &mut T = ct.at_mut(1);
|
||||
|
||||
// c1 <- Z_{2^prec}[X]/(X^{N}+1)
|
||||
module.fill_uniform(log_base2k, c1, limbs, source_xa);
|
||||
module.fill_uniform(log_base2k, c1, cols, source_xa);
|
||||
|
||||
let bytes_of_vec_znx_dft: usize = module.bytes_of_vec_znx_dft(limbs);
|
||||
let bytes_of_vec_znx_dft: usize = module.bytes_of_vec_znx_dft(cols);
|
||||
|
||||
// Scratch space for DFT values
|
||||
let mut buf_dft: VecZnxDft =
|
||||
VecZnxDft::from_bytes(limbs, &mut tmp_bytes[..bytes_of_vec_znx_dft]);
|
||||
VecZnxDft::from_bytes(cols, &mut tmp_bytes[..bytes_of_vec_znx_dft]);
|
||||
|
||||
// Applies buf_dft <- DFT(s) * DFT(c1)
|
||||
module.svp_apply_dft(&mut buf_dft, sk, c1, limbs);
|
||||
module.svp_apply_dft(&mut buf_dft, sk, c1, cols);
|
||||
|
||||
// Alias scratch space
|
||||
let mut buf_big: VecZnxBig = buf_dft.as_vec_znx_big();
|
||||
|
||||
// buf_big = s x c1
|
||||
module.vec_znx_idft_tmp_a(&mut buf_big, &mut buf_dft, limbs);
|
||||
module.vec_znx_idft_tmp_a(&mut buf_big, &mut buf_dft, cols);
|
||||
|
||||
let carry: &mut [u8] = &mut tmp_bytes[bytes_of_vec_znx_dft..];
|
||||
|
||||
@@ -194,7 +204,7 @@ pub fn encrypt_grlwe_sk_tmp_bytes(
|
||||
log_q: usize,
|
||||
) -> usize {
|
||||
let cols = (log_q + log_base2k - 1) / log_base2k;
|
||||
Elem::<VecZnx>::bytes_of(module, log_base2k, log_q, 1)
|
||||
Elem::<VecZnx>::bytes_of(module, log_base2k, log_q, 2)
|
||||
+ Plaintext::<VecZnx>::bytes_of(module, log_base2k, log_q)
|
||||
+ encrypt_rlwe_sk_tmp_bytes(module, log_base2k, log_q)
|
||||
+ module.vmp_prepare_tmp_bytes(rows, 2 * cols)
|
||||
@@ -202,7 +212,7 @@ pub fn encrypt_grlwe_sk_tmp_bytes(
|
||||
|
||||
pub fn encrypt_grlwe_sk_thread_safe(
|
||||
module: &Module,
|
||||
ct: &mut GadgetCiphertext,
|
||||
ct: &mut Ciphertext<VmpPMat>,
|
||||
m: &Scalar,
|
||||
sk: &SvpPPol,
|
||||
source_xa: &mut Source,
|
||||
@@ -212,7 +222,7 @@ pub fn encrypt_grlwe_sk_thread_safe(
|
||||
) {
|
||||
let rows: usize = ct.rows();
|
||||
let log_q: usize = ct.log_q();
|
||||
let cols: usize = (log_q + ct.log_base2k() - 1) / ct.log_base2k();
|
||||
//let cols: usize = (log_q + ct.log_base2k() - 1) / ct.log_base2k();
|
||||
let log_base2k: usize = ct.log_base2k();
|
||||
|
||||
let min_tmp_bytes_len = encrypt_grlwe_sk_tmp_bytes(module, log_base2k, rows, log_q);
|
||||
@@ -224,24 +234,24 @@ pub fn encrypt_grlwe_sk_thread_safe(
|
||||
min_tmp_bytes_len
|
||||
);
|
||||
|
||||
let bytes_of_elem: usize = Elem::<VecZnxBorrow>::bytes_of(module, log_base2k, log_q, 1);
|
||||
let bytes_of_elem: usize = Elem::<VecZnxBorrow>::bytes_of(module, log_base2k, log_q, 2);
|
||||
let bytes_of_pt: usize = Plaintext::<VecZnx>::bytes_of(module, log_base2k, log_q);
|
||||
let bytes_of_enc_sk: usize = encrypt_rlwe_sk_tmp_bytes(module, log_base2k, log_q);
|
||||
let bytes_of_vmp_prepare_row: usize = module.vmp_prepare_tmp_bytes(rows, 2 * cols);
|
||||
|
||||
let (tmp_bytes_pt, tmp_bytes) = tmp_bytes.split_at_mut(bytes_of_pt);
|
||||
let (tmp_bytes_enc_sk, tmp_bytes) = tmp_bytes.split_at_mut(bytes_of_enc_sk);
|
||||
let (tmp_bytes_elem, tmp_bytes_vmp_prepare_row) = tmp_bytes.split_at_mut(bytes_of_elem);
|
||||
|
||||
let mut tmp_elem: Elem<VecZnxBorrow> = Elem::<VecZnxBorrow>::from_bytes(module, log_base2k, ct.log_q(), 1, tmp_bytes_elem);
|
||||
let mut tmp_pt: Plaintext<VecZnxBorrow> = Plaintext::<VecZnxBorrow>::from_bytes(module, log_base2k, log_q, tmp_bytes_pt);
|
||||
let mut tmp_elem: Elem<VecZnxBorrow> =
|
||||
Elem::<VecZnxBorrow>::from_bytes(module, log_base2k, ct.log_q(), 2, tmp_bytes_elem);
|
||||
let mut tmp_pt: Plaintext<VecZnxBorrow> =
|
||||
Plaintext::<VecZnxBorrow>::from_bytes(module, log_base2k, log_q, tmp_bytes_pt);
|
||||
|
||||
(0..rows).for_each(|row_i| {
|
||||
// Sets the i-th row of the RLWE sample to m (i.e. m * 2^{-log_base2k*i})
|
||||
tmp_pt.0.value[0].at_mut(row_i).copy_from_slice(&m.0);
|
||||
tmp_pt.0.value[0].at_mut(row_i).copy_from_slice(&m.0);
|
||||
|
||||
// Encrypts RLWE(m * 2^{-log_base2k*i})
|
||||
|
||||
encrypt_rlwe_sk_thread_safe(
|
||||
module,
|
||||
&mut tmp_elem,
|
||||
@@ -252,24 +262,23 @@ pub fn encrypt_grlwe_sk_thread_safe(
|
||||
sigma,
|
||||
tmp_bytes_enc_sk,
|
||||
);
|
||||
|
||||
|
||||
// Zeroes the ith-row of tmp_pt
|
||||
tmp_pt.0.value[0].at_mut(row_i).fill(0);
|
||||
|
||||
println!("row:{}/{}", row_i, rows);
|
||||
tmp_elem.at(0).print_limbs(tmp_elem.limbs(), tmp_elem.n());
|
||||
tmp_elem.at(1).print_limbs(tmp_elem.limbs(), tmp_elem.n());
|
||||
println!();
|
||||
println!(">>>");
|
||||
//println!("row:{}/{}", row_i, rows);
|
||||
//tmp_elem.at(0).print(tmp_elem.limbs(), tmp_elem.n());
|
||||
//tmp_elem.at(1).print(tmp_elem.limbs(), tmp_elem.n());
|
||||
//println!();
|
||||
//println!(">>>");
|
||||
|
||||
// GRLWE[row_i][0||1] = [-as + m * 2^{-i*log_base2k} + e*2^{-log_q} || a]
|
||||
module.vmp_prepare_row(
|
||||
&mut ct.value,
|
||||
&mut ct.0.value[0],
|
||||
cast_mut::<u8, i64>(tmp_bytes_elem),
|
||||
row_i,
|
||||
tmp_bytes_vmp_prepare_row,
|
||||
);
|
||||
});
|
||||
println!("DONE");
|
||||
//println!("DONE");
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use crate::{
|
||||
ciphertext::{Ciphertext, GadgetCiphertext, RGSWCiphertext},
|
||||
elem::{Elem, ElemBasics},
|
||||
ciphertext::Ciphertext,
|
||||
elem::{Elem, ElemVecZnx, VecZnxCommon},
|
||||
};
|
||||
use base2k::{
|
||||
Infos, Module, VecZnx, VecZnxApi, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps, VmpPMatOps,
|
||||
Infos, Module, VecZnxApi, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps, VmpPMat, VmpPMatOps,
|
||||
};
|
||||
use std::cmp::min;
|
||||
|
||||
@@ -22,13 +22,14 @@ 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, T>(
|
||||
pub fn gadget_product_inplace_thread_safe<const OVERWRITE: bool, T: VecZnxApi<Owned = T> + Infos>(
|
||||
module: &Module,
|
||||
res: &mut Elem<T>,
|
||||
b: &GadgetCiphertext,
|
||||
b: &Ciphertext<VmpPMat>,
|
||||
tmp_bytes: &mut [u8],
|
||||
) where
|
||||
T: VecZnxApi + Infos,
|
||||
T: VecZnxCommon,
|
||||
Elem<T>: Infos + ElemVecZnx<T>,
|
||||
{
|
||||
unsafe {
|
||||
let a_ptr: *const T = res.at(1) as *const T;
|
||||
@@ -50,21 +51,22 @@ pub fn gadget_product_inplace_thread_safe<const OVERWRITE: bool, T>(
|
||||
///
|
||||
/// 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<const OVERWRITE: bool, T>(
|
||||
pub fn gadget_product_thread_safe<const OVERWRITE: bool, T: VecZnxApi<Owned = T> + Infos>(
|
||||
module: &Module,
|
||||
res: &mut Elem<T>,
|
||||
a: &T,
|
||||
b: &GadgetCiphertext,
|
||||
b: &Ciphertext<VmpPMat>,
|
||||
tmp_bytes: &mut [u8],
|
||||
) where
|
||||
T: VecZnxApi + Infos,
|
||||
T: VecZnxCommon,
|
||||
Elem<T>: Infos + ElemVecZnx<T>,
|
||||
{
|
||||
let log_base2k: usize = b.log_base2k();
|
||||
let rows: usize = min(b.rows(), a.limbs());
|
||||
let rows: usize = min(b.rows(), a.cols());
|
||||
let cols: usize = b.cols();
|
||||
|
||||
let bytes_vmp_apply_dft: usize =
|
||||
module.vmp_apply_dft_to_dft_tmp_bytes(cols, a.limbs(), rows, cols);
|
||||
module.vmp_apply_dft_to_dft_tmp_bytes(cols, a.cols(), rows, cols);
|
||||
let bytes_vec_znx_dft: usize = module.bytes_of_vec_znx_dft(cols);
|
||||
|
||||
let (tmp_bytes_vmp_apply_dft, tmp_bytes) = tmp_bytes.split_at_mut(bytes_vmp_apply_dft);
|
||||
@@ -82,11 +84,16 @@ pub fn gadget_product_thread_safe<const OVERWRITE: bool, T>(
|
||||
module.new_vec_znx_big_from_bytes(cols >> 1, tmp_bytes_res_dft_c1);
|
||||
|
||||
// a_dft <- DFT(a)
|
||||
module.vec_znx_dft(&mut c1_dft, a, a.limbs());
|
||||
module.vec_znx_dft(&mut c1_dft, a, a.cols());
|
||||
|
||||
// (n x cols) <- (n x limbs=rows) x (rows x cols)
|
||||
// res_dft[a * (G0|G1)] <- sum[rows] DFT(a) x (DFT(G0)|DFT(G1))
|
||||
module.vmp_apply_dft_to_dft(&mut res_dft, &c1_dft, &b.value, tmp_bytes_vmp_apply_dft);
|
||||
module.vmp_apply_dft_to_dft(
|
||||
&mut res_dft,
|
||||
&c1_dft,
|
||||
&b.0.value[0],
|
||||
tmp_bytes_vmp_apply_dft,
|
||||
);
|
||||
|
||||
// res_big[a * (G0|G1)] <- IDFT(res_dft[a * (G0|G1)])
|
||||
module.vec_znx_idft_tmp_a(&mut res_big, &mut res_dft, cols);
|
||||
@@ -105,24 +112,25 @@ pub fn gadget_product_thread_safe<const OVERWRITE: bool, T>(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rgsw_product_thread_safe<T>(
|
||||
pub fn rgsw_product_thread_safe<T: VecZnxApi<Owned = T> + Infos>(
|
||||
module: &Module,
|
||||
res: &mut Elem<T>,
|
||||
a: &Ciphertext,
|
||||
b: &RGSWCiphertext,
|
||||
a: &Ciphertext<T>,
|
||||
b: &Ciphertext<VmpPMat>,
|
||||
tmp_bytes: &mut [u8],
|
||||
) where
|
||||
T: VecZnxApi + Infos,
|
||||
T: VecZnxCommon,
|
||||
Elem<T>: Infos + ElemVecZnx<T>,
|
||||
{
|
||||
let log_base2k: usize = b.log_base2k();
|
||||
let rows: usize = a.limbs();
|
||||
let rows: usize = min(b.rows(), a.cols());
|
||||
let cols: usize = b.cols();
|
||||
let in_limbs = a.limbs();
|
||||
let out_limbs: usize = a.limbs();
|
||||
let in_cols = a.cols();
|
||||
let out_cols: usize = a.cols();
|
||||
|
||||
let bytes_of_vec_znx_dft = module.bytes_of_vec_znx_dft(cols);
|
||||
let bytes_of_vmp_apply_dft_to_dft =
|
||||
module.vmp_apply_dft_to_dft_tmp_bytes(out_limbs, in_limbs, rows, cols);
|
||||
module.vmp_apply_dft_to_dft_tmp_bytes(out_cols, in_cols, rows, cols);
|
||||
|
||||
let (tmp_bytes_c0_dft, tmp_bytes) = tmp_bytes.split_at_mut(bytes_of_vec_znx_dft);
|
||||
let (tmp_bytes_c1_dft, tmp_bytes) = tmp_bytes.split_at_mut(bytes_of_vec_znx_dft);
|
||||
@@ -139,16 +147,16 @@ pub fn rgsw_product_thread_safe<T>(
|
||||
let mut r2_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes(cols, tmp_bytes_r2_dft);
|
||||
|
||||
// c0_dft <- DFT(a[0])
|
||||
module.vec_znx_dft(&mut c0_dft, a.at(0), a.limbs());
|
||||
module.vec_znx_dft(&mut c0_dft, a.at(0), in_cols);
|
||||
|
||||
// r_dft <- sum[rows] c0_dft[cols] x RGSW[0][cols]
|
||||
module.vmp_apply_dft_to_dft(
|
||||
&mut r1_dft,
|
||||
&c1_dft,
|
||||
&b.value,
|
||||
&b.0.value[0],
|
||||
bytes_of_vmp_apply_dft_to_dft,
|
||||
);
|
||||
|
||||
// c1_dft <- DFT(a[1])
|
||||
module.vec_znx_dft(&mut c1_dft, a.at(1), a.limbs());
|
||||
module.vec_znx_dft(&mut c1_dft, a.at(1), in_cols);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::ciphertext::GadgetCiphertext;
|
||||
use crate::ciphertext::{Ciphertext, new_gadget_ciphertext};
|
||||
use crate::elem::Elem;
|
||||
use crate::encryptor::{encrypt_rlwe_sk_thread_safe, encrypt_rlwe_sk_tmp_bytes};
|
||||
use base2k::{Module, Scalar, SvpPPol, SvpPPolOps, VecZnx};
|
||||
use base2k::{Module, Scalar, SvpPPol, SvpPPolOps, VecZnx, VmpPMat};
|
||||
use sampling::source::Source;
|
||||
|
||||
pub struct SecretKey(pub Scalar);
|
||||
@@ -28,7 +28,7 @@ pub struct PublicKey(pub Elem<VecZnx>);
|
||||
|
||||
impl PublicKey {
|
||||
pub fn new(module: &Module, log_base2k: usize, log_q: usize) -> PublicKey {
|
||||
PublicKey(Elem::new(module, log_base2k, log_q, 1, 0))
|
||||
PublicKey(Elem::<VecZnx>::new(module, log_base2k, log_q, 2))
|
||||
}
|
||||
|
||||
pub fn gen_thread_safe(
|
||||
@@ -57,11 +57,11 @@ impl PublicKey {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SwitchingKey(pub GadgetCiphertext);
|
||||
pub struct SwitchingKey(pub Ciphertext<VmpPMat>);
|
||||
|
||||
impl SwitchingKey {
|
||||
pub fn new(module: &Module, log_base2k: usize, rows: usize, log_q: usize) -> SwitchingKey {
|
||||
SwitchingKey(GadgetCiphertext::new(module, log_base2k, rows, log_q))
|
||||
SwitchingKey(new_gadget_ciphertext(module, log_base2k, rows, log_q))
|
||||
}
|
||||
|
||||
pub fn n(&self) -> usize {
|
||||
|
||||
@@ -1,62 +1,72 @@
|
||||
use crate::ciphertext::Ciphertext;
|
||||
use crate::elem::{Elem, ElemBasics};
|
||||
use crate::elem::{Elem, ElemVecZnx, VecZnxCommon};
|
||||
use crate::parameters::Parameters;
|
||||
use base2k::{Infos, Module, VecZnx, VecZnxApi, VecZnxBorrow};
|
||||
use base2k::{Infos, Module, VecZnx, VecZnxApi};
|
||||
|
||||
pub struct Plaintext<T: VecZnxApi + Infos>(pub Elem<T>);
|
||||
pub struct Plaintext<T>(pub Elem<T>);
|
||||
|
||||
impl Parameters {
|
||||
pub fn new_plaintext(&self, log_q: usize) -> Plaintext<VecZnx> {
|
||||
Plaintext::new(self.module(), self.log_base2k(), log_q, self.log_scale())
|
||||
Plaintext::new(self.module(), self.log_base2k(), log_q)
|
||||
}
|
||||
|
||||
pub fn bytes_of_plaintext(&self, log_q: usize) -> usize {
|
||||
Elem::<VecZnx>::bytes_of(self.module(), self.log_base2k(), log_q, 0)
|
||||
pub fn bytes_of_plaintext<T>(&self, log_q: usize) -> usize
|
||||
where
|
||||
T: VecZnxCommon,
|
||||
Elem<T>: Infos + ElemVecZnx<T>,
|
||||
{
|
||||
Elem::<T>::bytes_of(self.module(), self.log_base2k(), log_q, 1)
|
||||
}
|
||||
|
||||
pub fn plaintext_from_bytes(&self, log_q: usize, bytes: &mut [u8]) -> Plaintext<VecZnx> {
|
||||
Plaintext(self.elem_from_bytes(log_q, 0, bytes))
|
||||
}
|
||||
|
||||
pub fn plaintext_borrow_from_bytes(&self, log_q: usize, bytes: &mut [u8]) -> Plaintext<VecZnxBorrow> {
|
||||
Plaintext(self.elem_borrow_from_bytes(log_q, 0, bytes))
|
||||
pub fn plaintext_from_bytes<T>(&self, log_q: usize, bytes: &mut [u8]) -> Plaintext<T>
|
||||
where
|
||||
T: VecZnxCommon,
|
||||
Elem<T>: Infos + ElemVecZnx<T>,
|
||||
{
|
||||
Plaintext::<T>(self.elem_from_bytes::<T>(log_q, 1, bytes))
|
||||
}
|
||||
}
|
||||
|
||||
impl Plaintext<VecZnx> {
|
||||
pub fn new(module: &Module, log_base2k: usize, log_q: usize, log_scale: usize) -> Self {
|
||||
Self(Elem::<VecZnx>::new(module, log_base2k, log_q, 0, log_scale))
|
||||
pub fn new(module: &Module, log_base2k: usize, log_q: usize) -> Self {
|
||||
Self(Elem::<VecZnx>::new(module, log_base2k, log_q, 1))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Plaintext<T>
|
||||
where
|
||||
T: VecZnxCommon,
|
||||
Elem<T>: Infos + ElemVecZnx<T>,
|
||||
{
|
||||
pub fn bytes_of(module: &Module, log_base2k: usize, log_q: usize) -> usize {
|
||||
Elem::<VecZnx>::bytes_of(module, log_base2k, log_q, 0)
|
||||
Elem::<T>::bytes_of(module, log_base2k, log_q, 1)
|
||||
}
|
||||
|
||||
pub fn from_bytes(module: &Module, log_base2k: usize, log_q: usize, bytes: &mut [u8]) -> Self {
|
||||
Self(Elem::<VecZnx>::from_bytes(module, log_base2k, log_q, 0, bytes))
|
||||
Self(Elem::<T>::from_bytes(module, log_base2k, log_q, 1, bytes))
|
||||
}
|
||||
|
||||
pub fn n(&self) -> usize {
|
||||
self.0.n()
|
||||
}
|
||||
|
||||
pub fn degree(&self) -> usize {
|
||||
self.0.degree()
|
||||
}
|
||||
|
||||
pub fn log_q(&self) -> usize {
|
||||
self.0.log_q()
|
||||
self.0.log_q
|
||||
}
|
||||
|
||||
pub fn limbs(&self) -> usize {
|
||||
self.0.limbs()
|
||||
pub fn rows(&self) -> usize {
|
||||
self.0.rows()
|
||||
}
|
||||
|
||||
pub fn at(&self, i: usize) -> &VecZnx {
|
||||
pub fn cols(&self) -> usize {
|
||||
self.0.cols()
|
||||
}
|
||||
|
||||
pub fn at(&self, i: usize) -> &T {
|
||||
self.0.at(i)
|
||||
}
|
||||
|
||||
pub fn at_mut(&mut self, i: usize) -> &mut VecZnx {
|
||||
pub fn at_mut(&mut self, i: usize) -> &mut T {
|
||||
self.0.at_mut(i)
|
||||
}
|
||||
|
||||
@@ -72,61 +82,7 @@ impl Plaintext<VecZnx> {
|
||||
self.0.zero()
|
||||
}
|
||||
|
||||
pub fn as_ciphertext(&self) -> Ciphertext {
|
||||
unsafe { Ciphertext(std::ptr::read(&self.0)) }
|
||||
pub fn as_ciphertext(&self) -> Ciphertext<T> {
|
||||
unsafe { Ciphertext::<T>(std::ptr::read(&self.0)) }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Plaintext<VecZnxBorrow> {
|
||||
|
||||
pub fn bytes_of(module: &Module, log_base2k: usize, log_q: usize) -> usize {
|
||||
Elem::<VecZnxBorrow>::bytes_of(module, log_base2k, log_q, 0)
|
||||
}
|
||||
|
||||
pub fn from_bytes(module: &Module, log_base2k: usize, log_q: usize, bytes: &mut [u8]) -> Self {
|
||||
Self(Elem::<VecZnxBorrow>::from_bytes(module, log_base2k, log_q, 0, bytes))
|
||||
}
|
||||
|
||||
pub fn n(&self) -> usize {
|
||||
self.0.n()
|
||||
}
|
||||
|
||||
pub fn degree(&self) -> usize {
|
||||
self.0.degree()
|
||||
}
|
||||
|
||||
pub fn log_q(&self) -> usize {
|
||||
self.0.log_q()
|
||||
}
|
||||
|
||||
pub fn limbs(&self) -> usize {
|
||||
self.0.limbs()
|
||||
}
|
||||
|
||||
pub fn at(&self, i: usize) -> &VecZnxBorrow {
|
||||
self.0.at(i)
|
||||
}
|
||||
|
||||
pub fn at_mut(&mut self, i: usize) -> &mut VecZnxBorrow {
|
||||
self.0.at_mut(i)
|
||||
}
|
||||
|
||||
pub fn log_base2k(&self) -> usize {
|
||||
self.0.log_base2k()
|
||||
}
|
||||
|
||||
pub fn log_scale(&self) -> usize {
|
||||
self.0.log_scale()
|
||||
}
|
||||
|
||||
pub fn zero(&mut self) {
|
||||
self.0.zero()
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn as_ciphertext(&self) -> Ciphertext {
|
||||
unsafe { Ciphertext(std::ptr::read(&self.0)) }
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user