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:
Jean-Philippe Bossuat
2025-03-17 12:07:40 +01:00
parent 97a1559bf2
commit 46c577409e
28 changed files with 896 additions and 1064 deletions

View File

@@ -11,6 +11,7 @@ criterion = {workspace = true}
base2k = {path="../base2k"}
sampling = {path="../sampling"}
rand_distr = {workspace = true}
itertools = {workspace = true}
[[bench]]
name = "gadget_product"

View File

@@ -1,5 +1,5 @@
use base2k::{
FFT64, Infos, Module, Sampling, SvpPPolOps, VecZnx, VecZnxDft, VecZnxDftOps, VecZnxOps,
Infos, MODULETYPE, Module, Sampling, SvpPPolOps, VecZnx, VecZnxDft, VecZnxDftOps, VecZnxOps,
VmpPMat, alloc_aligned_u8,
};
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
@@ -36,6 +36,7 @@ fn bench_gadget_product_inplace(c: &mut Criterion) {
for log_n in 10..11 {
let params_lit: ParametersLiteral = ParametersLiteral {
backend: MODULETYPE::FFT64,
log_n: log_n,
log_q: 32,
log_p: 0,
@@ -45,7 +46,7 @@ fn bench_gadget_product_inplace(c: &mut Criterion) {
xs: 128,
};
let params: Parameters = Parameters::new::<FFT64>(&params_lit);
let params: Parameters = Parameters::new(&params_lit);
let mut tmp_bytes: Vec<u8> = alloc_aligned_u8(
params.encrypt_rlwe_sk_tmp_bytes(params.log_q())
@@ -101,7 +102,7 @@ fn bench_gadget_product_inplace(c: &mut Criterion) {
let mut ct: Ciphertext<VecZnx> = params.new_ciphertext(params.log_q());
params.encrypt_rlwe_sk_thread_safe(
params.encrypt_rlwe_sk(
&mut ct,
None,
&sk0_svp_ppol,

View File

@@ -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>(&params_lit);
let params: Parameters = Parameters::new(&params_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()];

View 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 {}

View File

@@ -1,11 +1,11 @@
use crate::{
ciphertext::Ciphertext,
elem::{Elem, ElemCommon, VecZnxCommon},
elem::{Elem, ElemCommon},
keys::SecretKey,
parameters::Parameters,
plaintext::Plaintext,
};
use base2k::{Module, SvpPPol, SvpPPolOps, VecZnxBigOps, VecZnxDft, VecZnxDftOps};
use base2k::{Module, SvpPPol, SvpPPolOps, VecZnx, VecZnxBigOps, VecZnxDft, VecZnxDftOps};
use std::cmp::min;
pub struct Decryptor {
@@ -32,30 +32,24 @@ impl Parameters {
)
}
pub fn decrypt_rlwe<T>(
pub fn decrypt_rlwe(
&self,
res: &mut Plaintext<T>,
ct: &Ciphertext<T>,
res: &mut Plaintext,
ct: &Ciphertext<VecZnx>,
sk: &SvpPPol,
tmp_bytes: &mut [u8],
) where
T: VecZnxCommon<Owned = T>,
Elem<T>: ElemCommon<T>,
{
) {
decrypt_rlwe(self.module(), &mut res.0, &ct.0, sk, tmp_bytes)
}
}
pub fn decrypt_rlwe<T>(
pub fn decrypt_rlwe(
module: &Module,
res: &mut Elem<T>,
a: &Elem<T>,
res: &mut Elem<VecZnx>,
a: &Elem<VecZnx>,
sk: &SvpPPol,
tmp_bytes: &mut [u8],
) where
T: VecZnxCommon<Owned = T>,
Elem<T>: ElemCommon<T>,
{
) {
let cols: usize = a.cols();
assert!(
@@ -65,9 +59,11 @@ pub fn decrypt_rlwe<T>(
decrypt_rlwe_tmp_byte(module, cols)
);
let res_dft_bytes: usize = module.bytes_of_vec_znx_dft(cols);
let (tmp_bytes_vec_znx_dft, tmp_bytes_normalize) =
tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(cols));
let mut res_dft: VecZnxDft = VecZnxDft::from_bytes(a.cols(), tmp_bytes);
let mut res_dft: VecZnxDft =
VecZnxDft::from_bytes_borrow(module, a.cols(), tmp_bytes_vec_znx_dft);
let mut res_big: base2k::VecZnxBig = res_dft.as_vec_znx_big();
// res_dft <- DFT(ct[1]) * DFT(sk)
@@ -77,12 +73,7 @@ pub fn decrypt_rlwe<T>(
// res_big <- ct[1] x sk + ct[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(),
res.at_mut(0),
&res_big,
&mut tmp_bytes[res_dft_bytes..],
);
module.vec_znx_big_normalize(a.log_base2k(), res.at_mut(0), &res_big, tmp_bytes_normalize);
res.log_base2k = a.log_base2k();
res.log_q = min(res.log_q(), a.log_q());

View File

@@ -1,17 +1,7 @@
use base2k::{Infos, Module, VecZnx, VecZnxBorrow, VecZnxOps, VmpPMat, VmpPMatOps};
use base2k::{Infos, Module, VecZnx, VecZnxOps, VmpPMat, VmpPMatOps};
use crate::parameters::Parameters;
impl Parameters {
pub fn elem_from_bytes<T>(&self, log_q: usize, size: usize, bytes: &mut [u8]) -> Elem<T>
where
T: VecZnxCommon<Owned = T>,
Elem<T>: ElemVecZnx<T>,
{
Elem::<T>::from_bytes(self.module(), self.log_base2k(), log_q, size, bytes)
}
}
pub struct Elem<T> {
pub value: Vec<T>,
pub log_base2k: usize,
@@ -19,26 +9,26 @@ pub struct Elem<T> {
pub log_scale: usize,
}
pub trait VecZnxCommon: base2k::VecZnxCommon {}
impl VecZnxCommon for VecZnx {}
impl VecZnxCommon for VecZnxBorrow {}
pub trait ElemVecZnx<T: VecZnxCommon<Owned = T>> {
pub trait ElemVecZnx {
fn from_bytes(
module: &Module,
log_base2k: usize,
log_q: usize,
size: usize,
bytes: &mut [u8],
) -> Elem<T>;
) -> Elem<VecZnx>;
fn from_bytes_borrow(
module: &Module,
log_base2k: usize,
log_q: usize,
size: usize,
bytes: &mut [u8],
) -> Elem<VecZnx>;
fn bytes_of(module: &Module, log_base2k: usize, log_q: usize, size: usize) -> usize;
fn zero(&mut self);
}
impl<T> ElemVecZnx<T> for Elem<T>
where
T: VecZnxCommon<Owned = T>,
{
impl ElemVecZnx for Elem<VecZnx> {
fn bytes_of(module: &Module, log_base2k: usize, log_q: usize, size: usize) -> usize {
let cols = (log_q + log_base2k - 1) / log_base2k;
module.n() * cols * size * 8
@@ -50,16 +40,42 @@ where
log_q: usize,
size: usize,
bytes: &mut [u8],
) -> Elem<T> {
) -> Elem<VecZnx> {
assert!(size > 0);
let n: usize = module.n();
assert!(bytes.len() >= Self::bytes_of(module, log_base2k, log_q, size));
let mut value: Vec<T> = Vec::new();
let mut value: Vec<VecZnx> = Vec::new();
let limbs: usize = (log_q + log_base2k - 1) / log_base2k;
let elem_size = T::bytes_of(n, limbs);
let elem_size = VecZnx::bytes_of(n, limbs);
let mut ptr: usize = 0;
(0..size).for_each(|_| {
value.push(T::from_bytes(n, limbs, &mut bytes[ptr..]));
value.push(VecZnx::from_bytes(n, limbs, &mut bytes[ptr..]));
ptr += elem_size
});
Self {
value,
log_q,
log_base2k,
log_scale: 0,
}
}
fn from_bytes_borrow(
module: &Module,
log_base2k: usize,
log_q: usize,
size: usize,
bytes: &mut [u8],
) -> Elem<VecZnx> {
assert!(size > 0);
let n: usize = module.n();
assert!(bytes.len() >= Self::bytes_of(module, log_base2k, log_q, size));
let mut value: Vec<VecZnx> = Vec::new();
let limbs: usize = (log_q + log_base2k - 1) / log_base2k;
let elem_size = VecZnx::bytes_of(n, limbs);
let mut ptr: usize = 0;
(0..size).for_each(|_| {
value.push(VecZnx::from_bytes_borrow(n, limbs, &mut bytes[ptr..]));
ptr += elem_size
});
Self {

View File

@@ -1,12 +1,12 @@
use crate::ciphertext::Ciphertext;
use crate::elem::{Elem, ElemCommon, ElemVecZnx, VecZnxCommon};
use crate::elem::{Elem, ElemCommon, ElemVecZnx};
use crate::keys::SecretKey;
use crate::parameters::Parameters;
use crate::plaintext::Plaintext;
use base2k::sampling::Sampling;
use base2k::{
Module, Scalar, SvpPPol, SvpPPolOps, VecZnx, VecZnxApi, VecZnxBig, VecZnxBigOps, VecZnxBorrow,
VecZnxDft, VecZnxDftOps, VecZnxOps, VmpPMat, VmpPMatOps,
Module, Scalar, SvpPPol, SvpPPolOps, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps,
VecZnxOps, VmpPMat, VmpPMatOps,
};
use sampling::source::{Source, new_seed};
@@ -49,20 +49,17 @@ impl EncryptorSk {
self.source_xe = Source::new(seed)
}
pub fn encrypt_rlwe_sk<T>(
pub fn encrypt_rlwe_sk(
&mut self,
params: &Parameters,
ct: &mut Ciphertext<T>,
pt: Option<&Plaintext<T>>,
) where
T: VecZnxCommon<Owned = T>,
Elem<T>: ElemCommon<T>,
{
ct: &mut Ciphertext<VecZnx>,
pt: Option<&Plaintext>,
) {
assert!(
self.initialized == true,
"invalid call to [EncryptorSk.encrypt_rlwe_sk]: [EncryptorSk] has not been initialized with a [SecretKey]"
);
params.encrypt_rlwe_sk_thread_safe(
params.encrypt_rlwe_sk(
ct,
pt,
&self.sk,
@@ -72,23 +69,20 @@ impl EncryptorSk {
);
}
pub fn encrypt_rlwe_sk_thread_safe<T>(
pub fn encrypt_rlwe_sk_core(
&self,
params: &Parameters,
ct: &mut Ciphertext<T>,
pt: Option<&Plaintext<T>>,
ct: &mut Ciphertext<VecZnx>,
pt: Option<&Plaintext>,
source_xa: &mut Source,
source_xe: &mut Source,
tmp_bytes: &mut [u8],
) where
T: VecZnxCommon<Owned = T>,
Elem<T>: ElemCommon<T>,
{
) {
assert!(
self.initialized == true,
"invalid call to [EncryptorSk.encrypt_rlwe_sk_thread_safe]: [EncryptorSk] has not been initialized with a [SecretKey]"
"invalid call to [EncryptorSk.encrypt_rlwe_sk]: [EncryptorSk] has not been initialized with a [SecretKey]"
);
params.encrypt_rlwe_sk_thread_safe(ct, pt, &self.sk, source_xa, source_xe, tmp_bytes);
params.encrypt_rlwe_sk(ct, pt, &self.sk, source_xa, source_xe, tmp_bytes);
}
}
@@ -97,19 +91,16 @@ impl Parameters {
encrypt_rlwe_sk_tmp_bytes(self.module(), self.log_base2k(), log_q)
}
pub fn encrypt_rlwe_sk_thread_safe<T>(
pub fn encrypt_rlwe_sk(
&self,
ct: &mut Ciphertext<T>,
pt: Option<&Plaintext<T>>,
ct: &mut Ciphertext<VecZnx>,
pt: Option<&Plaintext>,
sk: &SvpPPol,
source_xa: &mut Source,
source_xe: &mut Source,
tmp_bytes: &mut [u8],
) where
T: VecZnxCommon<Owned = T>,
Elem<T>: ElemCommon<T>,
{
encrypt_rlwe_sk_thread_safe(
) {
encrypt_rlwe_sk(
self.module(),
&mut ct.0,
pt.map(|pt| &pt.0),
@@ -127,19 +118,16 @@ pub fn encrypt_rlwe_sk_tmp_bytes(module: &Module, log_base2k: usize, log_q: usiz
+ module.vec_znx_big_normalize_tmp_bytes()
}
pub fn encrypt_rlwe_sk_thread_safe<T>(
pub fn encrypt_rlwe_sk(
module: &Module,
ct: &mut Elem<T>,
pt: Option<&Elem<T>>,
ct: &mut Elem<VecZnx>,
pt: Option<&Elem<VecZnx>>,
sk: &SvpPPol,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
tmp_bytes: &mut [u8],
) where
T: VecZnxCommon<Owned = T>,
Elem<T>: ElemCommon<T>,
{
) {
let cols: usize = ct.cols();
let log_base2k: usize = ct.log_base2k();
let log_q: usize = ct.log_q();
@@ -153,16 +141,16 @@ pub fn encrypt_rlwe_sk_thread_safe<T>(
let log_q: usize = ct.log_q();
let log_base2k: usize = ct.log_base2k();
let c1: &mut T = ct.at_mut(1);
let c1: &mut VecZnx = ct.at_mut(1);
// c1 <- Z_{2^prec}[X]/(X^{N}+1)
module.fill_uniform(log_base2k, c1, cols, source_xa);
let bytes_of_vec_znx_dft: usize = module.bytes_of_vec_znx_dft(cols);
let (tmp_bytes_vec_znx_dft, tmp_bytes_normalize) =
tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(cols));
// Scratch space for DFT values
let mut buf_dft: VecZnxDft =
VecZnxDft::from_bytes(cols, &mut tmp_bytes[..bytes_of_vec_znx_dft]);
let mut buf_dft: VecZnxDft = VecZnxDft::from_bytes_borrow(module, cols, tmp_bytes_vec_znx_dft);
// Applies buf_dft <- DFT(s) * DFT(c1)
module.svp_apply_dft(&mut buf_dft, sk, c1, cols);
@@ -173,16 +161,14 @@ pub fn encrypt_rlwe_sk_thread_safe<T>(
// buf_big = s x c1
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..];
// c0 <- -s x c1 + m
let c0: &mut T = ct.at_mut(0);
let c0: &mut VecZnx = ct.at_mut(0);
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);
module.vec_znx_big_normalize(log_base2k, c0, &buf_big, tmp_bytes_normalize);
} else {
module.vec_znx_big_normalize(log_base2k, c0, &buf_big, carry);
module.vec_znx_big_normalize(log_base2k, c0, &buf_big, tmp_bytes_normalize);
module.vec_znx_negate_inplace(c0);
}
@@ -211,7 +197,7 @@ pub fn encrypt_grlwe_sk_tmp_bytes(
) -> usize {
let cols = (log_q + log_base2k - 1) / log_base2k;
Elem::<VecZnx>::bytes_of(module, log_base2k, log_q, 2)
+ Plaintext::<VecZnx>::bytes_of(module, log_base2k, log_q)
+ Plaintext::bytes_of(module, log_base2k, log_q)
+ encrypt_rlwe_sk_tmp_bytes(module, log_base2k, log_q)
+ module.vmp_prepare_tmp_bytes(rows, cols)
}
@@ -240,25 +226,25 @@ pub fn encrypt_grlwe_sk(
min_tmp_bytes_len
);
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_elem: usize = Elem::<VecZnx>::bytes_of(module, log_base2k, log_q, 2);
let bytes_of_pt: usize = Plaintext::bytes_of(module, log_base2k, log_q);
let bytes_of_enc_sk: usize = encrypt_rlwe_sk_tmp_bytes(module, log_base2k, log_q);
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(), 2, 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<VecZnx> =
Elem::<VecZnx>::from_bytes_borrow(module, log_base2k, ct.log_q(), 2, tmp_bytes_elem);
let mut tmp_pt: Plaintext =
Plaintext::from_bytes_borrow(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.at_mut(0).at_mut(row_i).copy_from_slice(&m.0);
tmp_pt.at_mut(0).at_mut(row_i).copy_from_slice(&m.raw());
// Encrypts RLWE(m * 2^{-log_base2k*i})
encrypt_rlwe_sk_thread_safe(
encrypt_rlwe_sk(
module,
&mut tmp_elem,
Some(&tmp_pt.0),

View File

@@ -1,9 +1,5 @@
use crate::{
ciphertext::Ciphertext,
elem::{Elem, ElemCommon, ElemVecZnx, VecZnxCommon},
parameters::Parameters,
};
use base2k::{Module, VecZnxDft, VecZnxDftOps, VmpPMat, VmpPMatOps};
use crate::{ciphertext::Ciphertext, elem::ElemCommon, parameters::Parameters};
use base2k::{Module, VecZnx, VecZnxDft, VecZnxDftOps, VmpPMat, VmpPMatOps};
use std::cmp::min;
pub fn gadget_product_tmp_bytes(
@@ -53,19 +49,16 @@ impl Parameters {
///
/// 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_cols, b_cols) cols.
pub fn gadget_product_core<T>(
pub fn gadget_product_core(
module: &Module,
res_dft_0: &mut VecZnxDft,
res_dft_1: &mut VecZnxDft,
a: &T,
a: &VecZnx,
a_cols: usize,
b: &Ciphertext<VmpPMat>,
b_cols: usize,
tmp_bytes: &mut [u8],
) where
T: VecZnxCommon<Owned = T>,
Elem<T>: ElemVecZnx<T>,
{
) {
assert!(b_cols <= b.cols());
module.vec_znx_dft(res_dft_1, a, min(a_cols, b_cols));
module.vmp_apply_dft_to_dft(res_dft_0, res_dft_1, b.at(0), tmp_bytes);
@@ -104,7 +97,7 @@ mod test {
plaintext::Plaintext,
};
use base2k::{
FFT64, Infos, Sampling, SvpPPolOps, VecZnx, VecZnxApi, VecZnxBig, VecZnxBigOps, VecZnxDft,
Infos, MODULETYPE, Sampling, SvpPPolOps, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft,
VecZnxDftOps, VecZnxOps, VmpPMat, alloc_aligned_u8,
};
use sampling::source::{Source, new_seed};
@@ -117,6 +110,7 @@ mod test {
// Basic parameters with enough limbs to test edge cases
let params_lit: ParametersLiteral = ParametersLiteral {
backend: MODULETYPE::FFT64,
log_n: 12,
log_q: q_cols * log_base2k,
log_p: p_cols * log_base2k,
@@ -126,7 +120,7 @@ mod test {
xs: 1 << 11,
};
let params: Parameters = Parameters::new::<FFT64>(&params_lit);
let params: Parameters = Parameters::new(&params_lit);
// scratch space
let mut tmp_bytes: Vec<u8> = alloc_aligned_u8(
@@ -213,8 +207,8 @@ mod test {
);
// Plaintext for decrypted output of gadget product
let mut pt: Plaintext<VecZnx> =
Plaintext::<VecZnx>::new(params.module(), params.log_base2k(), params.log_qp());
let mut pt: Plaintext =
Plaintext::new(params.module(), params.log_base2k(), params.log_qp());
// Iterates over all possible cols values for input/output polynomials and gadget ciphertext.

View File

@@ -1,6 +1,6 @@
use crate::ciphertext::{Ciphertext, new_gadget_ciphertext};
use crate::elem::{Elem, ElemCommon};
use crate::encryptor::{encrypt_rlwe_sk_thread_safe, encrypt_rlwe_sk_tmp_bytes};
use crate::encryptor::{encrypt_rlwe_sk, encrypt_rlwe_sk_tmp_bytes};
use base2k::{Module, Scalar, SvpPPol, SvpPPolOps, VecZnx, VmpPMat};
use sampling::source::Source;
@@ -40,7 +40,7 @@ impl PublicKey {
xe_source: &mut Source,
tmp_bytes: &mut [u8],
) {
encrypt_rlwe_sk_thread_safe(
encrypt_rlwe_sk(
module,
&mut self.0,
None,

View File

@@ -1,6 +1,7 @@
use base2k::module::{MODULETYPE, Module};
pub struct ParametersLiteral {
pub backend: MODULETYPE,
pub log_n: usize,
pub log_q: usize,
pub log_p: usize,
@@ -22,7 +23,7 @@ pub struct Parameters {
}
impl Parameters {
pub fn new<const MTYPE: MODULETYPE>(p: &ParametersLiteral) -> Self {
pub fn new(p: &ParametersLiteral) -> Self {
assert!(
p.log_n + 2 * p.log_base2k <= 53,
"invalid parameters: p.log_n + 2*p.log_base2k > 53"
@@ -35,7 +36,7 @@ impl Parameters {
log_base2k: p.log_base2k,
xe: p.xe,
xs: p.xs,
module: Module::new::<MTYPE>(1 << p.log_n),
module: Module::new(1 << p.log_n, p.backend),
}
}

View File

@@ -1,61 +1,65 @@
use crate::ciphertext::Ciphertext;
use crate::elem::{Elem, ElemCommon, ElemVecZnx, VecZnxCommon};
use crate::elem::{Elem, ElemCommon, ElemVecZnx};
use crate::parameters::Parameters;
use base2k::{Module, VecZnx};
pub struct Plaintext<T>(pub Elem<T>);
pub struct Plaintext(pub Elem<VecZnx>);
impl Parameters {
pub fn new_plaintext(&self, log_q: usize) -> Plaintext<VecZnx> {
pub fn new_plaintext(&self, log_q: usize) -> Plaintext {
Plaintext::new(self.module(), self.log_base2k(), log_q)
}
pub fn bytes_of_plaintext<T>(&self, log_q: usize) -> usize
where
T: VecZnxCommon<Owned = T>,
Elem<T>: ElemVecZnx<T>,
{
Elem::<T>::bytes_of(self.module(), self.log_base2k(), log_q, 1)
pub fn bytes_of_plaintext(&self, log_q: usize) -> usize
where {
Elem::<VecZnx>::bytes_of(self.module(), self.log_base2k(), log_q, 1)
}
pub fn plaintext_from_bytes<T>(&self, log_q: usize, bytes: &mut [u8]) -> Plaintext<T>
where
T: VecZnxCommon<Owned = T>,
Elem<T>: ElemVecZnx<T>,
{
Plaintext::<T>(self.elem_from_bytes::<T>(log_q, 1, bytes))
pub fn plaintext_from_bytes(&self, log_q: usize, bytes: &mut [u8]) -> Plaintext {
Plaintext(Elem::<VecZnx>::from_bytes(
self.module(),
self.log_base2k(),
log_q,
1,
bytes,
))
}
}
impl Plaintext<VecZnx> {
impl Plaintext {
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<Owned = T>,
Elem<T>: ElemVecZnx<T>,
{
impl Plaintext {
pub fn bytes_of(module: &Module, log_base2k: usize, log_q: usize) -> usize {
Elem::<T>::bytes_of(module, log_base2k, log_q, 1)
Elem::<VecZnx>::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::<T>::from_bytes(module, log_base2k, log_q, 1, bytes))
Self(Elem::<VecZnx>::from_bytes(
module, log_base2k, log_q, 1, bytes,
))
}
pub fn as_ciphertext(&self) -> Ciphertext<T> {
unsafe { Ciphertext::<T>(std::ptr::read(&self.0)) }
pub fn from_bytes_borrow(
module: &Module,
log_base2k: usize,
log_q: usize,
bytes: &mut [u8],
) -> Self {
Self(Elem::<VecZnx>::from_bytes_borrow(
module, log_base2k, log_q, 1, bytes,
))
}
pub fn as_ciphertext(&self) -> Ciphertext<VecZnx> {
unsafe { Ciphertext::<VecZnx>(std::ptr::read(&self.0)) }
}
}
impl<T> ElemCommon<T> for Plaintext<T>
where
T: VecZnxCommon<Owned = T>,
Elem<T>: ElemVecZnx<T>,
{
impl ElemCommon<VecZnx> for Plaintext {
fn n(&self) -> usize {
self.0.n()
}
@@ -68,11 +72,11 @@ where
self.0.log_q
}
fn elem(&self) -> &Elem<T> {
fn elem(&self) -> &Elem<VecZnx> {
&self.0
}
fn elem_mut(&mut self) -> &mut Elem<T> {
fn elem_mut(&mut self) -> &mut Elem<VecZnx> {
&mut self.0
}
@@ -88,11 +92,11 @@ where
self.0.cols()
}
fn at(&self, i: usize) -> &T {
fn at(&self, i: usize) -> &VecZnx {
self.0.at(i)
}
fn at_mut(&mut self, i: usize) -> &mut T {
fn at_mut(&mut self, i: usize) -> &mut VecZnx {
self.0.at_mut(i)
}

View File

@@ -1,20 +1,19 @@
use crate::{
ciphertext::Ciphertext,
elem::{Elem, ElemCommon, ElemVecZnx, VecZnxCommon},
elem::{Elem, ElemCommon, ElemVecZnx},
};
use base2k::{
Module, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps, VmpPMat, VmpPMatOps,
};
use base2k::{Module, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps, VmpPMat, VmpPMatOps};
use std::cmp::min;
pub fn rgsw_product<T>(
pub fn rgsw_product(
module: &Module,
_res: &mut Elem<T>,
a: &Ciphertext<T>,
_res: &mut Elem<VecZnx>,
a: &Ciphertext<VecZnx>,
b: &Ciphertext<VmpPMat>,
tmp_bytes: &mut [u8],
) where
T: VecZnxCommon<Owned = T>,
Elem<T>: ElemVecZnx<T>,
{
) {
let _log_base2k: usize = b.log_base2k();
let rows: usize = min(b.rows(), a.cols());
let cols: usize = b.cols();