mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
Various improvement to memory management and API
[module]: added enum for backend [VecZnx, VecZnxDft, VecZnxBig, VmpPMat]: added ptr to data [VecZnxBorrow]: removed [VecZnxAPI]: removed
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use base2k::{Encoding, FFT64, SvpPPolOps, VecZnx, VecZnxApi};
|
||||
use base2k::{Encoding, SvpPPolOps, VecZnx, alloc_aligned};
|
||||
use rlwe::{
|
||||
ciphertext::Ciphertext,
|
||||
elem::ElemCommon,
|
||||
@@ -10,6 +10,7 @@ use sampling::source::Source;
|
||||
|
||||
fn main() {
|
||||
let params_lit: ParametersLiteral = ParametersLiteral {
|
||||
backend: base2k::MODULETYPE::FFT64,
|
||||
log_n: 10,
|
||||
log_q: 54,
|
||||
log_p: 0,
|
||||
@@ -19,13 +20,12 @@ fn main() {
|
||||
xs: 128,
|
||||
};
|
||||
|
||||
let params: Parameters = Parameters::new::<FFT64>(¶ms_lit);
|
||||
let params: Parameters = Parameters::new(¶ms_lit);
|
||||
|
||||
let mut tmp_bytes: Vec<u8> = vec![
|
||||
0u8;
|
||||
let mut tmp_bytes: Vec<u8> = alloc_aligned(
|
||||
params.decrypt_rlwe_tmp_byte(params.log_q())
|
||||
| params.encrypt_rlwe_sk_tmp_bytes(params.log_q())
|
||||
];
|
||||
| params.encrypt_rlwe_sk_tmp_bytes(params.log_q()),
|
||||
);
|
||||
|
||||
let mut source: Source = Source::new([0; 32]);
|
||||
let mut sk: SecretKey = SecretKey::new(params.module());
|
||||
@@ -35,7 +35,7 @@ fn main() {
|
||||
|
||||
want.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
|
||||
|
||||
let mut pt: Plaintext<VecZnx> = params.new_plaintext(params.log_q());
|
||||
let mut pt: Plaintext = params.new_plaintext(params.log_q());
|
||||
|
||||
let log_base2k = pt.log_base2k();
|
||||
|
||||
@@ -56,7 +56,7 @@ fn main() {
|
||||
let mut sk_svp_ppol: base2k::SvpPPol = params.module().new_svp_ppol();
|
||||
params.module().svp_prepare(&mut sk_svp_ppol, &sk.0);
|
||||
|
||||
params.encrypt_rlwe_sk_thread_safe(
|
||||
params.encrypt_rlwe_sk(
|
||||
&mut ct,
|
||||
Some(&pt),
|
||||
&sk_svp_ppol,
|
||||
@@ -66,7 +66,6 @@ fn main() {
|
||||
);
|
||||
|
||||
params.decrypt_rlwe(&mut pt, &ct, &sk_svp_ppol, &mut tmp_bytes);
|
||||
|
||||
pt.0.value[0].print(pt.cols(), 16);
|
||||
|
||||
let mut have = vec![i64::default(); params.n()];
|
||||
|
||||
151
rlwe/examples/rlk_experiments.rs
Normal file
151
rlwe/examples/rlk_experiments.rs
Normal file
@@ -0,0 +1,151 @@
|
||||
use base2k::{
|
||||
Encoding, Infos, Module, Sampling, SvpPPol, SvpPPolOps, VecZnx, VecZnxDftOps, VecZnxOps,
|
||||
VmpPMat, VmpPMatOps, is_aligned,
|
||||
};
|
||||
use itertools::izip;
|
||||
use rlwe::ciphertext::{Ciphertext, new_gadget_ciphertext};
|
||||
use rlwe::elem::ElemCommon;
|
||||
use rlwe::encryptor::encrypt_rlwe_sk;
|
||||
use rlwe::keys::SecretKey;
|
||||
use rlwe::plaintext::Plaintext;
|
||||
use sampling::source::{Source, new_seed};
|
||||
|
||||
fn main() {
|
||||
let n: usize = 32;
|
||||
let module: Module = Module::new(n, base2k::MODULETYPE::FFT64);
|
||||
let log_base2k: usize = 16;
|
||||
let log_k: usize = 32;
|
||||
let cols: usize = 4;
|
||||
|
||||
let mut a: VecZnx = module.new_vec_znx(cols);
|
||||
let mut data: Vec<i64> = vec![0i64; n];
|
||||
data[0] = 0;
|
||||
data[1] = 0;
|
||||
a.encode_vec_i64(log_base2k, log_k, &data, 16);
|
||||
|
||||
let mut a_dft: base2k::VecZnxDft = module.new_vec_znx_dft(cols);
|
||||
|
||||
module.vec_znx_dft(&mut a_dft, &a, cols);
|
||||
|
||||
(0..cols).for_each(|i| {
|
||||
println!("{:?}", a_dft.at::<f64>(&module, i));
|
||||
})
|
||||
}
|
||||
|
||||
pub struct GadgetCiphertextProtocol {}
|
||||
|
||||
impl GadgetCiphertextProtocol {
|
||||
pub fn new() -> GadgetCiphertextProtocol {
|
||||
Self {}
|
||||
}
|
||||
|
||||
pub fn allocate(
|
||||
module: &Module,
|
||||
log_base2k: usize,
|
||||
rows: usize,
|
||||
log_q: usize,
|
||||
) -> GadgetCiphertextShare {
|
||||
GadgetCiphertextShare::new(module, log_base2k, rows, log_q)
|
||||
}
|
||||
|
||||
pub fn gen_share(
|
||||
module: &Module,
|
||||
sk: &SecretKey,
|
||||
pt: &Plaintext,
|
||||
seed: &[u8; 32],
|
||||
share: &mut GadgetCiphertextShare,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
share.seed.copy_from_slice(seed);
|
||||
let mut source_xe: Source = Source::new(new_seed());
|
||||
let mut source_xa: Source = Source::new(*seed);
|
||||
let mut sk_ppol: SvpPPol = module.new_svp_ppol();
|
||||
sk.prepare(module, &mut sk_ppol);
|
||||
share.value.iter_mut().for_each(|ai| {
|
||||
//let elem = Elem<VecZnx>{};
|
||||
//encrypt_rlwe_sk_thread_safe(module, ai, Some(pt.elem()), &sk_ppol, &mut source_xa, &mut source_xe, 3.2, tmp_bytes);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GadgetCiphertextShare {
|
||||
pub seed: [u8; 32],
|
||||
pub log_q: usize,
|
||||
pub log_base2k: usize,
|
||||
pub value: Vec<VecZnx>,
|
||||
}
|
||||
|
||||
impl GadgetCiphertextShare {
|
||||
pub fn new(module: &Module, log_base2k: usize, rows: usize, log_q: usize) -> Self {
|
||||
let value: Vec<VecZnx> = Vec::new();
|
||||
let cols: usize = (log_q + log_base2k - 1) / log_base2k;
|
||||
(0..rows).for_each(|_| {
|
||||
let vec_znx: VecZnx = module.new_vec_znx(cols);
|
||||
});
|
||||
Self {
|
||||
seed: [u8::default(); 32],
|
||||
log_q: log_q,
|
||||
log_base2k: log_base2k,
|
||||
value: value,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rows(&self) -> usize {
|
||||
self.value.len()
|
||||
}
|
||||
|
||||
pub fn cols(&self) -> usize {
|
||||
self.value[0].cols()
|
||||
}
|
||||
|
||||
pub fn aggregate_inplace(&mut self, module: &Module, a: &GadgetCiphertextShare) {
|
||||
izip!(self.value.iter_mut(), a.value.iter()).for_each(|(bi, ai)| {
|
||||
module.vec_znx_add_inplace(bi, ai);
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get(&self, module: &Module, b: &mut Ciphertext<VmpPMat>, tmp_bytes: &mut [u8]) {
|
||||
assert!(is_aligned(tmp_bytes.as_ptr()));
|
||||
|
||||
let rows: usize = b.rows();
|
||||
let cols: usize = b.cols();
|
||||
|
||||
assert!(tmp_bytes.len() >= gadget_ciphertext_share_get_tmp_bytes(module, rows, cols));
|
||||
|
||||
assert_eq!(self.value.len(), rows);
|
||||
assert_eq!(self.value[0].cols(), cols);
|
||||
|
||||
let (tmp_bytes_vmp_prepare_row, tmp_bytes_vec_znx) =
|
||||
tmp_bytes.split_at_mut(module.vmp_prepare_tmp_bytes(rows, cols));
|
||||
|
||||
let mut c: VecZnx = VecZnx::from_bytes_borrow(module.n(), cols, tmp_bytes_vec_znx);
|
||||
|
||||
let mut source: Source = Source::new(self.seed);
|
||||
|
||||
(0..self.value.len()).for_each(|row_i| {
|
||||
module.vmp_prepare_row(
|
||||
b.at_mut(0),
|
||||
self.value[row_i].raw(),
|
||||
row_i,
|
||||
tmp_bytes_vmp_prepare_row,
|
||||
);
|
||||
module.fill_uniform(self.log_base2k, &mut c, cols, &mut source);
|
||||
module.vmp_prepare_row(b.at_mut(1), c.raw(), row_i, tmp_bytes_vmp_prepare_row)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_new(&self, module: &Module, tmp_bytes: &mut [u8]) -> Ciphertext<VmpPMat> {
|
||||
let mut b: Ciphertext<VmpPMat> =
|
||||
new_gadget_ciphertext(module, self.log_base2k, self.rows(), self.log_q);
|
||||
self.get(module, &mut b, tmp_bytes);
|
||||
b
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gadget_ciphertext_share_get_tmp_bytes(module: &Module, rows: usize, cols: usize) -> usize {
|
||||
module.vmp_prepare_tmp_bytes(rows, cols) + module.bytes_of_vec_znx(cols)
|
||||
}
|
||||
|
||||
pub struct CircularCiphertextProtocol {}
|
||||
|
||||
pub struct CircularGadgetCiphertextProtocol {}
|
||||
Reference in New Issue
Block a user