Added size and memory layout to VecZnxBig, VecZnxDft and VmpPmat

This commit is contained in:
Jean-Philippe Bossuat
2025-04-25 09:19:47 +02:00
parent f0eaddb63e
commit 3bdddd3857
22 changed files with 195 additions and 119 deletions

View File

@@ -35,7 +35,7 @@ fn main() {
module.fill_uniform(log_base2k, &mut a, cols, &mut source); module.fill_uniform(log_base2k, &mut a, cols, &mut source);
// Scratch space for DFT values // Scratch space for DFT values
let mut buf_dft: VecZnxDft = module.new_vec_znx_dft(a.cols()); let mut buf_dft: VecZnxDft = module.new_vec_znx_dft(1, a.cols());
// Applies buf_dft <- s * a // Applies buf_dft <- s * a
module.svp_apply_dft(&mut buf_dft, &s_ppol, &a); module.svp_apply_dft(&mut buf_dft, &s_ppol, &a);

View File

@@ -30,7 +30,7 @@ fn main() {
a.print(0, a.cols(), n); a.print(0, a.cols(), n);
println!(); println!();
let mut vmp_pmat: VmpPMat = module.new_vmp_pmat(rows, cols); let mut vmp_pmat: VmpPMat = module.new_vmp_pmat(1, rows, cols);
(0..a.cols()).for_each(|row_i| { (0..a.cols()).for_each(|row_i| {
let mut tmp: VecZnx = module.new_vec_znx(1, cols); let mut tmp: VecZnx = module.new_vec_znx(1, cols);
@@ -38,7 +38,7 @@ fn main() {
module.vmp_prepare_row(&mut vmp_pmat, tmp.raw(), row_i, &mut buf); module.vmp_prepare_row(&mut vmp_pmat, tmp.raw(), row_i, &mut buf);
}); });
let mut c_dft: VecZnxDft = module.new_vec_znx_dft(cols); let mut c_dft: VecZnxDft = module.new_vec_znx_dft(1, cols);
module.vmp_apply_dft(&mut c_dft, &a, &vmp_pmat, &mut buf); module.vmp_apply_dft(&mut c_dft, &a, &vmp_pmat, &mut buf);
let mut c_big: VecZnxBig = c_dft.as_vec_znx_big(); let mut c_big: VecZnxBig = c_dft.as_vec_znx_big();

View File

@@ -261,7 +261,7 @@ fn decode_coeff_i64(a: &VecZnx, poly_idx: usize, log_base2k: usize, log_k: usize
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{Encoding, VecZnx}; use crate::{Encoding, Infos, VecZnx};
use itertools::izip; use itertools::izip;
use sampling::source::Source; use sampling::source::Source;

View File

@@ -1,3 +1,5 @@
use crate::LAYOUT;
pub trait Infos { pub trait Infos {
/// Returns the ring degree of the receiver. /// Returns the ring degree of the receiver.
fn n(&self) -> usize; fn n(&self) -> usize;
@@ -5,6 +7,12 @@ pub trait Infos {
/// Returns the base two logarithm of the ring dimension of the receiver. /// Returns the base two logarithm of the ring dimension of the receiver.
fn log_n(&self) -> usize; fn log_n(&self) -> usize;
/// Returns the number of stacked polynomials.
fn size(&self) -> usize;
/// Returns the memory layout of the stacked polynomials.
fn layout(&self) -> LAYOUT;
/// Returns the number of columns of the receiver. /// Returns the number of columns of the receiver.
/// This method is equivalent to [Infos::cols]. /// This method is equivalent to [Infos::cols].
fn cols(&self) -> usize; fn cols(&self) -> usize;

View File

@@ -34,7 +34,6 @@ pub enum LAYOUT {
COL, COL,
} }
pub fn is_aligned_custom<T>(ptr: *const T, align: usize) -> bool { pub fn is_aligned_custom<T>(ptr: *const T, align: usize) -> bool {
(ptr as usize) % align == 0 (ptr as usize) % align == 0
} }

View File

@@ -1,6 +1,6 @@
use crate::ffi::svp::{self, svp_ppol_t}; use crate::ffi::svp::{self, svp_ppol_t};
use crate::ffi::vec_znx_dft::vec_znx_dft_t; use crate::ffi::vec_znx_dft::vec_znx_dft_t;
use crate::{assert_alignement, Module, VecZnx, VecZnxDft, BACKEND, LAYOUT}; use crate::{BACKEND, LAYOUT, Module, VecZnx, VecZnxDft, assert_alignement};
use crate::{Infos, alloc_aligned, cast_mut}; use crate::{Infos, alloc_aligned, cast_mut};
use rand::seq::SliceRandom; use rand::seq::SliceRandom;

View File

@@ -1,7 +1,7 @@
use crate::LAYOUT;
use crate::cast_mut; use crate::cast_mut;
use crate::ffi::vec_znx; use crate::ffi::vec_znx;
use crate::ffi::znx; use crate::ffi::znx;
use crate::LAYOUT;
use crate::{Infos, Module}; use crate::{Infos, Module};
use crate::{alloc_aligned, assert_alignement}; use crate::{alloc_aligned, assert_alignement};
use itertools::izip; use itertools::izip;
@@ -99,11 +99,6 @@ impl VecZnx {
self.data.len() == 0 self.data.len() == 0
} }
/// TODO: when SML refactoring is done, move this to the [Infos] trait.
pub fn size(&self) -> usize {
self.size
}
/// Total size is [VecZnx::n()] * [VecZnx::size()] * [VecZnx::cols()]. /// Total size is [VecZnx::n()] * [VecZnx::size()] * [VecZnx::cols()].
pub fn raw(&self) -> &[i64] { pub fn raw(&self) -> &[i64] {
unsafe { std::slice::from_raw_parts(self.ptr, self.n * self.size * self.cols) } unsafe { std::slice::from_raw_parts(self.ptr, self.n * self.size * self.cols) }
@@ -225,6 +220,14 @@ impl Infos for VecZnx {
self.n self.n
} }
fn size(&self) -> usize {
self.size
}
fn layout(&self) -> LAYOUT {
self.layout
}
/// Returns the number of cols of the [VecZnx]. /// Returns the number of cols of the [VecZnx].
fn cols(&self) -> usize { fn cols(&self) -> usize {
self.cols self.cols

View File

@@ -1,11 +1,13 @@
use crate::ffi::vec_znx_big::{self, vec_znx_big_t}; use crate::ffi::vec_znx_big::{self, vec_znx_big_t};
use crate::{BACKEND, Infos, Module, VecZnx, VecZnxDft, alloc_aligned, assert_alignement}; use crate::{BACKEND, Infos, LAYOUT, Module, VecZnx, VecZnxDft, alloc_aligned, assert_alignement};
pub struct VecZnxBig { pub struct VecZnxBig {
pub data: Vec<u8>, pub data: Vec<u8>,
pub ptr: *mut u8, pub ptr: *mut u8,
pub n: usize, pub n: usize,
pub size: usize,
pub cols: usize, pub cols: usize,
pub layout: LAYOUT,
pub backend: BACKEND, pub backend: BACKEND,
} }
@@ -13,10 +15,10 @@ impl VecZnxBig {
/// Returns a new [VecZnxBig] with the provided data as backing array. /// Returns a new [VecZnxBig] with the provided data as backing array.
/// User must ensure that data is properly alligned and that /// User must ensure that data is properly alligned and that
/// the size of data is at least equal to [Module::bytes_of_vec_znx_big]. /// the size of data is at least equal to [Module::bytes_of_vec_znx_big].
pub fn from_bytes(module: &Module, cols: usize, bytes: &mut [u8]) -> Self { pub fn from_bytes(module: &Module, size: usize, cols: usize, bytes: &mut [u8]) -> Self {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
assert_eq!(bytes.len(), module.bytes_of_vec_znx_big(cols)); assert_eq!(bytes.len(), module.bytes_of_vec_znx_big(size, cols));
assert_alignement(bytes.as_ptr()) assert_alignement(bytes.as_ptr())
}; };
unsafe { unsafe {
@@ -24,22 +26,26 @@ impl VecZnxBig {
data: Vec::from_raw_parts(bytes.as_mut_ptr(), bytes.len(), bytes.len()), data: Vec::from_raw_parts(bytes.as_mut_ptr(), bytes.len(), bytes.len()),
ptr: bytes.as_mut_ptr(), ptr: bytes.as_mut_ptr(),
n: module.n(), n: module.n(),
size: size,
layout: LAYOUT::COL,
cols: cols, cols: cols,
backend: module.backend, backend: module.backend,
} }
} }
} }
pub fn from_bytes_borrow(module: &Module, cols: usize, bytes: &mut [u8]) -> Self { pub fn from_bytes_borrow(module: &Module, size: usize, cols: usize, bytes: &mut [u8]) -> Self {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
assert_eq!(bytes.len(), module.bytes_of_vec_znx_big(cols)); assert_eq!(bytes.len(), module.bytes_of_vec_znx_big(size, cols));
assert_alignement(bytes.as_ptr()); assert_alignement(bytes.as_ptr());
} }
Self { Self {
data: Vec::new(), data: Vec::new(),
ptr: bytes.as_mut_ptr(), ptr: bytes.as_mut_ptr(),
n: module.n(), n: module.n(),
size: size,
layout: LAYOUT::COL,
cols: cols, cols: cols,
backend: module.backend, backend: module.backend,
} }
@@ -50,6 +56,8 @@ impl VecZnxBig {
data: Vec::new(), data: Vec::new(),
ptr: self.ptr, ptr: self.ptr,
n: self.n, n: self.n,
size: self.size,
layout: LAYOUT::COL,
cols: self.cols, cols: self.cols,
backend: self.backend, backend: self.backend,
} }
@@ -81,6 +89,14 @@ impl Infos for VecZnxBig {
self.n self.n
} }
fn size(&self) -> usize {
self.size
}
fn layout(&self) -> LAYOUT {
self.layout
}
/// Returns the number of cols of the [VecZnx]. /// Returns the number of cols of the [VecZnx].
fn cols(&self) -> usize { fn cols(&self) -> usize {
self.cols self.cols
@@ -94,7 +110,7 @@ impl Infos for VecZnxBig {
pub trait VecZnxBigOps { pub trait VecZnxBigOps {
/// Allocates a vector Z[X]/(X^N+1) that stores not normalized values. /// Allocates a vector Z[X]/(X^N+1) that stores not normalized values.
fn new_vec_znx_big(&self, cols: usize) -> VecZnxBig; fn new_vec_znx_big(&self, size: usize, cols: usize) -> VecZnxBig;
/// Returns a new [VecZnxBig] with the provided bytes array as backing array. /// Returns a new [VecZnxBig] with the provided bytes array as backing array.
/// ///
@@ -107,7 +123,7 @@ pub trait VecZnxBigOps {
/// ///
/// # Panics /// # Panics
/// If `bytes.len()` < [Module::bytes_of_vec_znx_big]. /// If `bytes.len()` < [Module::bytes_of_vec_znx_big].
fn new_vec_znx_big_from_bytes(&self, cols: usize, bytes: &mut [u8]) -> VecZnxBig; fn new_vec_znx_big_from_bytes(&self, size: usize, cols: usize, bytes: &mut [u8]) -> VecZnxBig;
/// Returns a new [VecZnxBig] with the provided bytes array as backing array. /// Returns a new [VecZnxBig] with the provided bytes array as backing array.
/// ///
@@ -120,11 +136,11 @@ pub trait VecZnxBigOps {
/// ///
/// # Panics /// # Panics
/// If `bytes.len()` < [Module::bytes_of_vec_znx_big]. /// If `bytes.len()` < [Module::bytes_of_vec_znx_big].
fn new_vec_znx_big_from_bytes_borrow(&self, cols: usize, tmp_bytes: &mut [u8]) -> VecZnxBig; fn new_vec_znx_big_from_bytes_borrow(&self, size: usize, cols: usize, tmp_bytes: &mut [u8]) -> VecZnxBig;
/// Returns the minimum number of bytes necessary to allocate /// Returns the minimum number of bytes necessary to allocate
/// a new [VecZnxBig] through [VecZnxBig::from_bytes]. /// a new [VecZnxBig] through [VecZnxBig::from_bytes].
fn bytes_of_vec_znx_big(&self, cols: usize) -> usize; fn bytes_of_vec_znx_big(&self, size: usize, cols: usize) -> usize;
/// b <- b - a /// b <- b - a
fn vec_znx_big_sub_small_a_inplace(&self, b: &mut VecZnxBig, a: &VecZnx); fn vec_znx_big_sub_small_a_inplace(&self, b: &mut VecZnxBig, a: &VecZnx);
@@ -162,28 +178,30 @@ pub trait VecZnxBigOps {
} }
impl VecZnxBigOps for Module { impl VecZnxBigOps for Module {
fn new_vec_znx_big(&self, cols: usize) -> VecZnxBig { fn new_vec_znx_big(&self, size: usize, cols: usize) -> VecZnxBig {
let mut data: Vec<u8> = alloc_aligned::<u8>(self.bytes_of_vec_znx_big(cols)); let mut data: Vec<u8> = alloc_aligned::<u8>(self.bytes_of_vec_znx_big(size, cols));
let ptr: *mut u8 = data.as_mut_ptr(); let ptr: *mut u8 = data.as_mut_ptr();
VecZnxBig { VecZnxBig {
data: data, data: data,
ptr: ptr, ptr: ptr,
n: self.n(), n: self.n(),
size: size,
layout: LAYOUT::COL,
cols: cols, cols: cols,
backend: self.backend(), backend: self.backend(),
} }
} }
fn new_vec_znx_big_from_bytes(&self, cols: usize, bytes: &mut [u8]) -> VecZnxBig { fn new_vec_znx_big_from_bytes(&self, size: usize, cols: usize, bytes: &mut [u8]) -> VecZnxBig {
VecZnxBig::from_bytes(self, cols, bytes) VecZnxBig::from_bytes(self, size, cols, bytes)
} }
fn new_vec_znx_big_from_bytes_borrow(&self, cols: usize, tmp_bytes: &mut [u8]) -> VecZnxBig { fn new_vec_znx_big_from_bytes_borrow(&self, size: usize, cols: usize, tmp_bytes: &mut [u8]) -> VecZnxBig {
VecZnxBig::from_bytes_borrow(self, cols, tmp_bytes) VecZnxBig::from_bytes_borrow(self, size, cols, tmp_bytes)
} }
fn bytes_of_vec_znx_big(&self, cols: usize) -> usize { fn bytes_of_vec_znx_big(&self, size: usize, cols: usize) -> usize {
unsafe { vec_znx_big::bytes_of_vec_znx_big(self.ptr, cols as u64) as usize } unsafe { vec_znx_big::bytes_of_vec_znx_big(self.ptr, cols as u64) as usize * size }
} }
fn vec_znx_big_sub_small_a_inplace(&self, b: &mut VecZnxBig, a: &VecZnx) { fn vec_znx_big_sub_small_a_inplace(&self, b: &mut VecZnxBig, a: &VecZnx) {

View File

@@ -1,13 +1,15 @@
use crate::ffi::vec_znx_big::vec_znx_big_t; use crate::ffi::vec_znx_big::vec_znx_big_t;
use crate::ffi::vec_znx_dft; use crate::ffi::vec_znx_dft;
use crate::ffi::vec_znx_dft::{bytes_of_vec_znx_dft, vec_znx_dft_t}; use crate::ffi::vec_znx_dft::{bytes_of_vec_znx_dft, vec_znx_dft_t};
use crate::{BACKEND, Infos, Module, VecZnxBig, assert_alignement}; use crate::{BACKEND, Infos, LAYOUT, Module, VecZnxBig, assert_alignement};
use crate::{DEFAULTALIGN, VecZnx, alloc_aligned}; use crate::{DEFAULTALIGN, VecZnx, alloc_aligned};
pub struct VecZnxDft { pub struct VecZnxDft {
pub data: Vec<u8>, pub data: Vec<u8>,
pub ptr: *mut u8, pub ptr: *mut u8,
pub n: usize, pub n: usize,
pub size: usize,
pub layout: LAYOUT,
pub cols: usize, pub cols: usize,
pub backend: BACKEND, pub backend: BACKEND,
} }
@@ -16,10 +18,10 @@ impl VecZnxDft {
/// Returns a new [VecZnxDft] with the provided data as backing array. /// Returns a new [VecZnxDft] with the provided data as backing array.
/// User must ensure that data is properly alligned and that /// User must ensure that data is properly alligned and that
/// the size of data is at least equal to [Module::bytes_of_vec_znx_dft]. /// the size of data is at least equal to [Module::bytes_of_vec_znx_dft].
pub fn from_bytes(module: &Module, cols: usize, bytes: &mut [u8]) -> VecZnxDft { pub fn from_bytes(module: &Module, size: usize, cols: usize, bytes: &mut [u8]) -> VecZnxDft {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
assert_eq!(bytes.len(), module.bytes_of_vec_znx_dft(cols)); assert_eq!(bytes.len(), module.bytes_of_vec_znx_dft(size, cols));
assert_alignement(bytes.as_ptr()) assert_alignement(bytes.as_ptr())
} }
unsafe { unsafe {
@@ -27,22 +29,26 @@ impl VecZnxDft {
data: Vec::from_raw_parts(bytes.as_mut_ptr(), bytes.len(), bytes.len()), data: Vec::from_raw_parts(bytes.as_mut_ptr(), bytes.len(), bytes.len()),
ptr: bytes.as_mut_ptr(), ptr: bytes.as_mut_ptr(),
n: module.n(), n: module.n(),
size: size,
layout: LAYOUT::COL,
cols: cols, cols: cols,
backend: module.backend, backend: module.backend,
} }
} }
} }
pub fn from_bytes_borrow(module: &Module, cols: usize, bytes: &mut [u8]) -> VecZnxDft { pub fn from_bytes_borrow(module: &Module, size: usize, cols: usize, bytes: &mut [u8]) -> VecZnxDft {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
assert_eq!(bytes.len(), module.bytes_of_vec_znx_dft(cols)); assert_eq!(bytes.len(), module.bytes_of_vec_znx_dft(size, cols));
assert_alignement(bytes.as_ptr()); assert_alignement(bytes.as_ptr());
} }
VecZnxDft { VecZnxDft {
data: Vec::new(), data: Vec::new(),
ptr: bytes.as_mut_ptr(), ptr: bytes.as_mut_ptr(),
n: module.n(), n: module.n(),
size: size,
layout: LAYOUT::COL,
cols: cols, cols: cols,
backend: module.backend, backend: module.backend,
} }
@@ -56,6 +62,8 @@ impl VecZnxDft {
data: Vec::new(), data: Vec::new(),
ptr: self.ptr, ptr: self.ptr,
n: self.n, n: self.n,
layout: LAYOUT::COL,
size: self.size,
cols: self.cols, cols: self.cols,
backend: self.backend, backend: self.backend,
} }
@@ -105,6 +113,14 @@ impl Infos for VecZnxDft {
self.n self.n
} }
fn size(&self) -> usize {
self.size
}
fn layout(&self) -> LAYOUT {
self.layout
}
/// Returns the number of cols of the [VecZnx]. /// Returns the number of cols of the [VecZnx].
fn cols(&self) -> usize { fn cols(&self) -> usize {
self.cols self.cols
@@ -118,7 +134,7 @@ impl Infos for VecZnxDft {
pub trait VecZnxDftOps { pub trait VecZnxDftOps {
/// Allocates a vector Z[X]/(X^N+1) that stores normalized in the DFT space. /// Allocates a vector Z[X]/(X^N+1) that stores normalized in the DFT space.
fn new_vec_znx_dft(&self, cols: usize) -> VecZnxDft; fn new_vec_znx_dft(&self, size: usize, cols: usize) -> VecZnxDft;
/// Returns a new [VecZnxDft] with the provided bytes array as backing array. /// Returns a new [VecZnxDft] with the provided bytes array as backing array.
/// ///
@@ -131,7 +147,7 @@ pub trait VecZnxDftOps {
/// ///
/// # Panics /// # Panics
/// If `bytes.len()` < [Module::bytes_of_vec_znx_dft]. /// If `bytes.len()` < [Module::bytes_of_vec_znx_dft].
fn new_vec_znx_dft_from_bytes(&self, cols: usize, bytes: &mut [u8]) -> VecZnxDft; fn new_vec_znx_dft_from_bytes(&self, size: usize, cols: usize, bytes: &mut [u8]) -> VecZnxDft;
/// Returns a new [VecZnxDft] with the provided bytes array as backing array. /// Returns a new [VecZnxDft] with the provided bytes array as backing array.
/// ///
@@ -144,7 +160,7 @@ pub trait VecZnxDftOps {
/// ///
/// # Panics /// # Panics
/// If `bytes.len()` < [Module::bytes_of_vec_znx_dft]. /// If `bytes.len()` < [Module::bytes_of_vec_znx_dft].
fn new_vec_znx_dft_from_bytes_borrow(&self, cols: usize, bytes: &mut [u8]) -> VecZnxDft; fn new_vec_znx_dft_from_bytes_borrow(&self, size: usize, cols: usize, bytes: &mut [u8]) -> VecZnxDft;
/// Returns a new [VecZnxDft] with the provided bytes array as backing array. /// Returns a new [VecZnxDft] with the provided bytes array as backing array.
/// ///
@@ -155,7 +171,7 @@ pub trait VecZnxDftOps {
/// ///
/// # Panics /// # Panics
/// If `bytes.len()` < [Module::bytes_of_vec_znx_dft]. /// If `bytes.len()` < [Module::bytes_of_vec_znx_dft].
fn bytes_of_vec_znx_dft(&self, cols: usize) -> usize; fn bytes_of_vec_znx_dft(&self, size: usize, cols: usize) -> usize;
/// Returns the minimum number of bytes necessary to allocate /// Returns the minimum number of bytes necessary to allocate
/// a new [VecZnxDft] through [VecZnxDft::from_bytes]. /// a new [VecZnxDft] through [VecZnxDft::from_bytes].
@@ -176,28 +192,30 @@ pub trait VecZnxDftOps {
} }
impl VecZnxDftOps for Module { impl VecZnxDftOps for Module {
fn new_vec_znx_dft(&self, cols: usize) -> VecZnxDft { fn new_vec_znx_dft(&self, size: usize, cols: usize) -> VecZnxDft {
let mut data: Vec<u8> = alloc_aligned::<u8>(self.bytes_of_vec_znx_dft(cols)); let mut data: Vec<u8> = alloc_aligned::<u8>(self.bytes_of_vec_znx_dft(size, cols));
let ptr: *mut u8 = data.as_mut_ptr(); let ptr: *mut u8 = data.as_mut_ptr();
VecZnxDft { VecZnxDft {
data: data, data: data,
ptr: ptr, ptr: ptr,
n: self.n(), n: self.n(),
size: size,
layout: LAYOUT::COL,
cols: cols, cols: cols,
backend: self.backend(), backend: self.backend(),
} }
} }
fn new_vec_znx_dft_from_bytes(&self, cols: usize, tmp_bytes: &mut [u8]) -> VecZnxDft { fn new_vec_znx_dft_from_bytes(&self, size: usize, cols: usize, tmp_bytes: &mut [u8]) -> VecZnxDft {
VecZnxDft::from_bytes(self, cols, tmp_bytes) VecZnxDft::from_bytes(self, size, cols, tmp_bytes)
} }
fn new_vec_znx_dft_from_bytes_borrow(&self, cols: usize, tmp_bytes: &mut [u8]) -> VecZnxDft { fn new_vec_znx_dft_from_bytes_borrow(&self, size: usize, cols: usize, tmp_bytes: &mut [u8]) -> VecZnxDft {
VecZnxDft::from_bytes_borrow(self, cols, tmp_bytes) VecZnxDft::from_bytes_borrow(self, size, cols, tmp_bytes)
} }
fn bytes_of_vec_znx_dft(&self, cols: usize) -> usize { fn bytes_of_vec_znx_dft(&self, size: usize, cols: usize) -> usize {
unsafe { bytes_of_vec_znx_dft(self.ptr, cols as u64) as usize } unsafe { bytes_of_vec_znx_dft(self.ptr, cols as u64) as usize * size }
} }
fn vec_znx_idft_tmp_a(&self, b: &mut VecZnxBig, a: &mut VecZnxDft) { fn vec_znx_idft_tmp_a(&self, b: &mut VecZnxBig, a: &mut VecZnxDft) {
@@ -318,8 +336,8 @@ mod tests {
let cols: usize = 2; let cols: usize = 2;
let log_base2k: usize = 17; let log_base2k: usize = 17;
let mut a: VecZnx = module.new_vec_znx(1, cols); let mut a: VecZnx = module.new_vec_znx(1, cols);
let mut a_dft: VecZnxDft = module.new_vec_znx_dft(cols); let mut a_dft: VecZnxDft = module.new_vec_znx_dft(1, cols);
let mut b_dft: VecZnxDft = module.new_vec_znx_dft(cols); let mut b_dft: VecZnxDft = module.new_vec_znx_dft(1, cols);
let mut source: Source = Source::new(new_seed()); let mut source: Source = Source::new(new_seed());
module.fill_uniform(log_base2k, &mut a, cols, &mut source); module.fill_uniform(log_base2k, &mut a, cols, &mut source);

View File

@@ -1,7 +1,7 @@
use crate::ffi::vec_znx_big::vec_znx_big_t; use crate::ffi::vec_znx_big::vec_znx_big_t;
use crate::ffi::vec_znx_dft::vec_znx_dft_t; use crate::ffi::vec_znx_dft::vec_znx_dft_t;
use crate::ffi::vmp::{self, vmp_pmat_t}; use crate::ffi::vmp::{self, vmp_pmat_t};
use crate::{BACKEND, Infos, Module, VecZnx, VecZnxBig, VecZnxDft, alloc_aligned, assert_alignement}; use crate::{BACKEND, Infos, LAYOUT, Module, VecZnx, VecZnxBig, VecZnxDft, alloc_aligned, assert_alignement};
/// Vector Matrix Product Prepared Matrix: a vector of [VecZnx], /// Vector Matrix Product Prepared Matrix: a vector of [VecZnx],
/// stored as a 3D matrix in the DFT domain in a single contiguous array. /// stored as a 3D matrix in the DFT domain in a single contiguous array.
@@ -23,8 +23,11 @@ pub struct VmpPMat {
cols: usize, cols: usize,
/// The ring degree of each [VecZnxDft]. /// The ring degree of each [VecZnxDft].
n: usize, n: usize,
/// The number of stacked [VmpPMat], must be a square.
#[warn(dead_code)] size: usize,
/// The memory layout of the stacked [VmpPMat].
layout: LAYOUT,
/// The backend fft or ntt.
backend: BACKEND, backend: BACKEND,
} }
@@ -38,6 +41,14 @@ impl Infos for VmpPMat {
(usize::BITS - (self.n() - 1).leading_zeros()) as _ (usize::BITS - (self.n() - 1).leading_zeros()) as _
} }
fn size(&self) -> usize {
self.size
}
fn layout(&self) -> LAYOUT {
self.layout
}
/// Returns the number of rows (i.e. of [VecZnxDft]) of the [VmpPMat] /// Returns the number of rows (i.e. of [VecZnxDft]) of the [VmpPMat]
fn rows(&self) -> usize { fn rows(&self) -> usize {
self.rows self.rows
@@ -120,12 +131,16 @@ impl VmpPMat {
&self.raw::<T>()[blk * nrows * ncols * 8 + (col / 2) * (2 * nrows) * 8 + row * 2 * 8 + (col % 2) * 8..] &self.raw::<T>()[blk * nrows * ncols * 8 + (col / 2) * (2 * nrows) * 8 + row * 2 * 8 + (col % 2) * 8..]
} }
} }
fn backend(&self) -> BACKEND {
self.backend
}
} }
/// This trait implements methods for vector matrix product, /// This trait implements methods for vector matrix product,
/// that is, multiplying a [VecZnx] with a [VmpPMat]. /// that is, multiplying a [VecZnx] with a [VmpPMat].
pub trait VmpPMatOps { pub trait VmpPMatOps {
fn bytes_of_vmp_pmat(&self, rows: usize, cols: usize) -> usize; fn bytes_of_vmp_pmat(&self, size: usize, rows: usize, cols: usize) -> usize;
/// Allocates a new [VmpPMat] with the given number of rows and columns. /// Allocates a new [VmpPMat] with the given number of rows and columns.
/// ///
@@ -133,7 +148,7 @@ pub trait VmpPMatOps {
/// ///
/// * `rows`: number of rows (number of [VecZnxDft]). /// * `rows`: number of rows (number of [VecZnxDft]).
/// * `cols`: number of cols (number of cols of each [VecZnxDft]). /// * `cols`: number of cols (number of cols of each [VecZnxDft]).
fn new_vmp_pmat(&self, rows: usize, cols: usize) -> VmpPMat; fn new_vmp_pmat(&self, size: usize, rows: usize, cols: usize) -> VmpPMat;
/// Returns the number of bytes needed as scratch space for [VmpPMatOps::vmp_prepare_contiguous]. /// Returns the number of bytes needed as scratch space for [VmpPMatOps::vmp_prepare_contiguous].
/// ///
@@ -360,17 +375,19 @@ pub trait VmpPMatOps {
} }
impl VmpPMatOps for Module { impl VmpPMatOps for Module {
fn bytes_of_vmp_pmat(&self, rows: usize, cols: usize) -> usize { fn bytes_of_vmp_pmat(&self, size: usize, rows: usize, cols: usize) -> usize {
unsafe { vmp::bytes_of_vmp_pmat(self.ptr, rows as u64, cols as u64) as usize } unsafe { vmp::bytes_of_vmp_pmat(self.ptr, rows as u64, cols as u64) as usize * size }
} }
fn new_vmp_pmat(&self, rows: usize, cols: usize) -> VmpPMat { fn new_vmp_pmat(&self, size: usize, rows: usize, cols: usize) -> VmpPMat {
let mut data: Vec<u8> = alloc_aligned::<u8>(self.bytes_of_vmp_pmat(rows, cols)); let mut data: Vec<u8> = alloc_aligned::<u8>(self.bytes_of_vmp_pmat(size, rows, cols));
let ptr: *mut u8 = data.as_mut_ptr(); let ptr: *mut u8 = data.as_mut_ptr();
VmpPMat { VmpPMat {
data: data, data: data,
ptr: ptr, ptr: ptr,
n: self.n(), n: self.n(),
size: size,
layout: LAYOUT::COL,
cols: cols, cols: cols,
rows: rows, rows: rows,
backend: self.backend(), backend: self.backend(),
@@ -643,12 +660,12 @@ mod tests {
let vpmat_cols: usize = 5; let vpmat_cols: usize = 5;
let log_base2k: usize = 8; let log_base2k: usize = 8;
let mut a: VecZnx = module.new_vec_znx(1, vpmat_cols); let mut a: VecZnx = module.new_vec_znx(1, vpmat_cols);
let mut a_dft: VecZnxDft = module.new_vec_znx_dft(vpmat_cols); let mut a_dft: VecZnxDft = module.new_vec_znx_dft(1, vpmat_cols);
let mut a_big: VecZnxBig = module.new_vec_znx_big(vpmat_cols); let mut a_big: VecZnxBig = module.new_vec_znx_big(1, vpmat_cols);
let mut b_big: VecZnxBig = module.new_vec_znx_big(vpmat_cols); let mut b_big: VecZnxBig = module.new_vec_znx_big(1, vpmat_cols);
let mut b_dft: VecZnxDft = module.new_vec_znx_dft(vpmat_cols); let mut b_dft: VecZnxDft = module.new_vec_znx_dft(1, vpmat_cols);
let mut vmpmat_0: VmpPMat = module.new_vmp_pmat(vpmat_rows, vpmat_cols); let mut vmpmat_0: VmpPMat = module.new_vmp_pmat(1, vpmat_rows, vpmat_cols);
let mut vmpmat_1: VmpPMat = module.new_vmp_pmat(vpmat_rows, vpmat_cols); let mut vmpmat_1: VmpPMat = module.new_vmp_pmat(1, vpmat_rows, vpmat_cols);
let mut tmp_bytes: Vec<u8> = alloc_aligned(module.vmp_prepare_tmp_bytes(vpmat_rows, vpmat_cols)); let mut tmp_bytes: Vec<u8> = alloc_aligned(module.vmp_prepare_tmp_bytes(vpmat_rows, vpmat_cols));

View File

@@ -104,8 +104,8 @@ fn bench_gadget_product_inplace(c: &mut Criterion) {
&mut tmp_bytes, &mut tmp_bytes,
); );
let mut res_dft_0: VecZnxDft = params.module().new_vec_znx_dft(gadget_ct.cols()); let mut res_dft_0: VecZnxDft = params.module().new_vec_znx_dft(1, gadget_ct.cols());
let mut res_dft_1: VecZnxDft = params.module().new_vec_znx_dft(gadget_ct.cols()); let mut res_dft_1: VecZnxDft = params.module().new_vec_znx_dft(1, gadget_ct.cols());
let mut a: VecZnx = params.module().new_vec_znx(0, params.cols_q()); let mut a: VecZnx = params.module().new_vec_znx(0, params.cols_q());
params params

View File

@@ -152,7 +152,7 @@ pub fn automorphism(
pub fn automorphism_inplace_tmp_bytes(module: &Module, c_cols: usize, a_cols: usize, b_rows: usize, b_cols: usize) -> usize { pub fn automorphism_inplace_tmp_bytes(module: &Module, c_cols: usize, a_cols: usize, b_rows: usize, b_cols: usize) -> usize {
return module.vmp_apply_dft_to_dft_tmp_bytes(c_cols, a_cols, b_rows, b_cols) return module.vmp_apply_dft_to_dft_tmp_bytes(c_cols, a_cols, b_rows, b_cols)
+ 2 * module.bytes_of_vec_znx_dft(std::cmp::min(c_cols, a_cols)); + 2 * module.bytes_of_vec_znx_dft(1, std::cmp::min(c_cols, a_cols));
} }
pub fn automorphism_inplace( pub fn automorphism_inplace(
@@ -184,11 +184,11 @@ pub fn automorphism_big(
assert_alignement(tmp_bytes.as_ptr()); assert_alignement(tmp_bytes.as_ptr());
} }
let (tmp_bytes_b1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(cols)); let (tmp_bytes_b1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
let (tmp_bytes_res_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(cols)); let (tmp_bytes_res_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
let mut a1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(cols, tmp_bytes_b1_dft); let mut a1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, cols, tmp_bytes_b1_dft);
let mut res_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(cols, tmp_bytes_res_dft); let mut res_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, cols, tmp_bytes_res_dft);
// a1_dft = DFT(a[1]) // a1_dft = DFT(a[1])
module.vec_znx_dft(&mut a1_dft, a.at(1)); module.vec_znx_dft(&mut a1_dft, a.at(1));

View File

@@ -1,6 +1,6 @@
use crate::elem::{Elem, ElemCommon}; use crate::elem::{Elem, ElemCommon};
use crate::parameters::Parameters; use crate::parameters::Parameters;
use base2k::{Infos, Module, VecZnx, VmpPMat}; use base2k::{Infos, LAYOUT, Module, VecZnx, VmpPMat};
pub struct Ciphertext<T>(pub Elem<T>); pub struct Ciphertext<T>(pub Elem<T>);
@@ -38,6 +38,10 @@ where
self.elem().size() self.elem().size()
} }
fn layout(&self) -> LAYOUT {
self.elem().layout()
}
fn rows(&self) -> usize { fn rows(&self) -> usize {
self.elem().rows() self.elem().rows()
} }

View File

@@ -20,8 +20,8 @@ impl Decryptor {
} }
} }
pub fn decrypt_rlwe_tmp_byte(module: &Module, limbs: usize) -> usize { pub fn decrypt_rlwe_tmp_byte(module: &Module, cols: usize) -> usize {
module.bytes_of_vec_znx_dft(limbs) + module.vec_znx_big_normalize_tmp_bytes() module.bytes_of_vec_znx_dft(1, cols) + module.vec_znx_big_normalize_tmp_bytes()
} }
impl Parameters { impl Parameters {
@@ -47,9 +47,9 @@ pub fn decrypt_rlwe(module: &Module, res: &mut Elem<VecZnx>, a: &Elem<VecZnx>, s
decrypt_rlwe_tmp_byte(module, cols) decrypt_rlwe_tmp_byte(module, cols)
); );
let (tmp_bytes_vec_znx_dft, tmp_bytes_normalize) = tmp_bytes.split_at_mut(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(1, cols));
let mut res_dft: VecZnxDft = VecZnxDft::from_bytes_borrow(module, cols, tmp_bytes_vec_znx_dft); let mut res_dft: VecZnxDft = VecZnxDft::from_bytes_borrow(module, 1, cols, tmp_bytes_vec_znx_dft);
let mut res_big: base2k::VecZnxBig = res_dft.as_vec_znx_big(); let mut res_big: base2k::VecZnxBig = res_dft.as_vec_znx_big();
// res_dft <- DFT(ct[1]) * DFT(sk) // res_dft <- DFT(ct[1]) * DFT(sk)

View File

@@ -1,4 +1,4 @@
use base2k::{Infos, Module, VecZnx, VecZnxOps, VmpPMat, VmpPMatOps}; use base2k::{Infos, LAYOUT, Module, VecZnx, VecZnxOps, VmpPMat, VmpPMatOps};
pub struct Elem<T> { pub struct Elem<T> {
pub value: Vec<T>, pub value: Vec<T>,
@@ -71,6 +71,7 @@ pub trait ElemCommon<T> {
fn elem(&self) -> &Elem<T>; fn elem(&self) -> &Elem<T>;
fn elem_mut(&mut self) -> &mut Elem<T>; fn elem_mut(&mut self) -> &mut Elem<T>;
fn size(&self) -> usize; fn size(&self) -> usize;
fn layout(&self) -> LAYOUT;
fn rows(&self) -> usize; fn rows(&self) -> usize;
fn cols(&self) -> usize; fn cols(&self) -> usize;
fn log_base2k(&self) -> usize; fn log_base2k(&self) -> usize;
@@ -101,6 +102,10 @@ impl<T: Infos> ElemCommon<T> for Elem<T> {
self.value.len() self.value.len()
} }
fn layout(&self) -> LAYOUT {
self.value[0].layout()
}
fn rows(&self) -> usize { fn rows(&self) -> usize {
self.value[0].rows() self.value[0].rows()
} }
@@ -152,7 +157,7 @@ impl Elem<VmpPMat> {
assert!(rows > 0); assert!(rows > 0);
assert!(cols > 0); assert!(cols > 0);
let mut value: Vec<VmpPMat> = Vec::new(); let mut value: Vec<VmpPMat> = Vec::new();
(0..size).for_each(|_| value.push(module.new_vmp_pmat(rows, cols))); (0..size).for_each(|_| value.push(module.new_vmp_pmat(1, rows, cols)));
Self { Self {
value: value, value: value,
log_q: 0, log_q: 0,

View File

@@ -108,7 +108,7 @@ impl EncryptorSk {
} }
pub fn encrypt_rlwe_sk_tmp_bytes(module: &Module, log_base2k: usize, log_q: usize) -> usize { pub fn encrypt_rlwe_sk_tmp_bytes(module: &Module, log_base2k: usize, log_q: usize) -> usize {
module.bytes_of_vec_znx_dft((log_q + log_base2k - 1) / log_base2k) + module.vec_znx_big_normalize_tmp_bytes() module.bytes_of_vec_znx_dft(1, (log_q + log_base2k - 1) / log_base2k) + module.vec_znx_big_normalize_tmp_bytes()
} }
pub fn encrypt_rlwe_sk( pub fn encrypt_rlwe_sk(
module: &Module, module: &Module,
@@ -151,10 +151,10 @@ fn encrypt_rlwe_sk_core<const PT_POS: u8>(
// c1 <- Z_{2^prec}[X]/(X^{N}+1) // c1 <- Z_{2^prec}[X]/(X^{N}+1)
module.fill_uniform(log_base2k, c1, cols, source_xa); module.fill_uniform(log_base2k, c1, cols, source_xa);
let (tmp_bytes_vec_znx_dft, tmp_bytes_normalize) = tmp_bytes.split_at_mut(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(1, cols));
// Scratch space for DFT values // Scratch space for DFT values
let mut buf_dft: VecZnxDft = VecZnxDft::from_bytes_borrow(module, cols, tmp_bytes_vec_znx_dft); let mut buf_dft: VecZnxDft = VecZnxDft::from_bytes_borrow(module, 1, cols, tmp_bytes_vec_znx_dft);
// Applies buf_dft <- DFT(s) * DFT(c1) // Applies buf_dft <- DFT(s) * DFT(c1)
module.svp_apply_dft(&mut buf_dft, sk, c1); module.svp_apply_dft(&mut buf_dft, sk, c1);

View File

@@ -46,7 +46,7 @@ pub fn gadget_product_core(
pub fn gadget_product_big_tmp_bytes(module: &Module, c_cols: usize, a_cols: usize, b_rows: usize, b_cols: usize) -> usize { pub fn gadget_product_big_tmp_bytes(module: &Module, c_cols: usize, a_cols: usize, b_rows: usize, b_cols: usize) -> usize {
return module.vmp_apply_dft_to_dft_tmp_bytes(c_cols, a_cols, b_rows, b_cols) return module.vmp_apply_dft_to_dft_tmp_bytes(c_cols, a_cols, b_rows, b_cols)
+ 2 * module.bytes_of_vec_znx_dft(min(c_cols, a_cols)); + 2 * module.bytes_of_vec_znx_dft(1, min(c_cols, a_cols));
} }
/// Evaluates the gadget product: c.at(i) = IDFT(<DFT(a.at(i)), b.at(i)>) /// Evaluates the gadget product: c.at(i) = IDFT(<DFT(a.at(i)), b.at(i)>)
@@ -66,11 +66,11 @@ pub fn gadget_product_big(
) { ) {
let cols: usize = min(c.cols(), a.cols()); let cols: usize = min(c.cols(), a.cols());
let (tmp_bytes_b1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(cols)); let (tmp_bytes_b1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
let (tmp_bytes_res_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(cols)); let (tmp_bytes_res_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
let mut a1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(cols, tmp_bytes_b1_dft); let mut a1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, cols, tmp_bytes_b1_dft);
let mut res_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(cols, tmp_bytes_res_dft); let mut res_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, cols, tmp_bytes_res_dft);
// a1_dft = DFT(a[1]) // a1_dft = DFT(a[1])
module.vec_znx_dft(&mut a1_dft, a.at(1)); module.vec_znx_dft(&mut a1_dft, a.at(1));
@@ -99,11 +99,11 @@ pub fn gadget_product(
) { ) {
let cols: usize = min(c.cols(), a.cols()); let cols: usize = min(c.cols(), a.cols());
let (tmp_bytes_b1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(cols)); let (tmp_bytes_b1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
let (tmp_bytes_res_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(cols)); let (tmp_bytes_res_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
let mut a1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(cols, tmp_bytes_b1_dft); let mut a1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, cols, tmp_bytes_b1_dft);
let mut res_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(cols, tmp_bytes_res_dft); let mut res_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, cols, tmp_bytes_res_dft);
let mut res_big: VecZnxBig = res_dft.as_vec_znx_big(); let mut res_big: VecZnxBig = res_dft.as_vec_znx_big();
// a1_dft = DFT(a[1]) // a1_dft = DFT(a[1])
@@ -215,7 +215,7 @@ mod test {
let mut elem_res: Elem<VecZnx> = Elem::<VecZnx>::new(params.module(), log_base2k, params.log_qp(), 2); let mut elem_res: Elem<VecZnx> = Elem::<VecZnx>::new(params.module(), log_base2k, params.log_qp(), 2);
// Ideal output = a * s // Ideal output = a * s
let mut a_dft: VecZnxDft = params.module().new_vec_znx_dft(a.cols()); let mut a_dft: VecZnxDft = params.module().new_vec_znx_dft(1, a.cols());
let mut a_big: VecZnxBig = a_dft.as_vec_znx_big(); let mut a_big: VecZnxBig = a_dft.as_vec_znx_big();
let mut a_times_s: VecZnx = params.module().new_vec_znx(1, a.cols()); let mut a_times_s: VecZnx = params.module().new_vec_znx(1, a.cols());
@@ -236,8 +236,8 @@ mod test {
a_trunc.copy_from(&a); a_trunc.copy_from(&a);
(1..gadget_ct.cols() + 1).for_each(|b_cols| { (1..gadget_ct.cols() + 1).for_each(|b_cols| {
let mut res_dft_0: VecZnxDft = params.module().new_vec_znx_dft(b_cols); let mut res_dft_0: VecZnxDft = params.module().new_vec_znx_dft(1, b_cols);
let mut res_dft_1: VecZnxDft = params.module().new_vec_znx_dft(b_cols); let mut res_dft_1: VecZnxDft = params.module().new_vec_znx_dft(1, b_cols);
let mut res_big_0: VecZnxBig = res_dft_0.as_vec_znx_big(); let mut res_big_0: VecZnxBig = res_dft_0.as_vec_znx_big();
let mut res_big_1: VecZnxBig = res_dft_1.as_vec_znx_big(); let mut res_big_1: VecZnxBig = res_dft_1.as_vec_znx_big();

View File

@@ -8,8 +8,8 @@ pub fn key_switch_tmp_bytes(module: &Module, log_base2k: usize, res_logq: usize,
let in_cols: usize = (in_logq + log_base2k - 1) / log_base2k; let in_cols: usize = (in_logq + log_base2k - 1) / log_base2k;
let res_cols: usize = (res_logq + log_base2k - 1) / log_base2k; let res_cols: usize = (res_logq + log_base2k - 1) / log_base2k;
return module.vmp_apply_dft_to_dft_tmp_bytes(res_cols, in_cols, in_cols, gct_cols) return module.vmp_apply_dft_to_dft_tmp_bytes(res_cols, in_cols, in_cols, gct_cols)
+ module.bytes_of_vec_znx_dft(std::cmp::min(res_cols, in_cols)) + module.bytes_of_vec_znx_dft(1, std::cmp::min(res_cols, in_cols))
+ module.bytes_of_vec_znx_dft(gct_cols); + module.bytes_of_vec_znx_dft(1, gct_cols);
} }
pub fn key_switch_rlwe( pub fn key_switch_rlwe(
@@ -54,11 +54,11 @@ fn key_switch_rlwe_core(
assert_alignement(tmp_bytes.as_ptr()); assert_alignement(tmp_bytes.as_ptr());
} }
let (tmp_bytes_a1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(cols)); let (tmp_bytes_a1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
let (tmp_bytes_res_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(b_cols)); let (tmp_bytes_res_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, b_cols));
let mut a1_dft = module.new_vec_znx_dft_from_bytes_borrow(cols, tmp_bytes_a1_dft); let mut a1_dft = module.new_vec_znx_dft_from_bytes_borrow(1, cols, tmp_bytes_a1_dft);
let mut res_dft = module.new_vec_znx_dft_from_bytes_borrow(b_cols, tmp_bytes_res_dft); let mut res_dft = module.new_vec_znx_dft_from_bytes_borrow(1, b_cols, tmp_bytes_res_dft);
let mut res_big = res_dft.as_vec_znx_big(); let mut res_big = res_dft.as_vec_znx_big();
module.vec_znx_dft(&mut a1_dft, a.at(1)); module.vec_znx_dft(&mut a1_dft, a.at(1));

View File

@@ -1,7 +1,7 @@
use crate::ciphertext::Ciphertext; use crate::ciphertext::Ciphertext;
use crate::elem::{Elem, ElemCommon, ElemVecZnx}; use crate::elem::{Elem, ElemCommon, ElemVecZnx};
use crate::parameters::Parameters; use crate::parameters::Parameters;
use base2k::{Module, VecZnx}; use base2k::{LAYOUT, Module, VecZnx};
pub struct Plaintext(pub Elem<VecZnx>); pub struct Plaintext(pub Elem<VecZnx>);
@@ -79,6 +79,10 @@ impl ElemCommon<VecZnx> for Plaintext {
self.elem().size() self.elem().size()
} }
fn layout(&self) -> LAYOUT {
self.elem().layout()
}
fn rows(&self) -> usize { fn rows(&self) -> usize {
self.0.rows() self.0.rows()
} }

View File

@@ -18,8 +18,8 @@ pub fn rgsw_product_tmp_bytes(module: &Module, log_base2k: usize, res_logq: usiz
let in_cols: usize = (in_logq + log_base2k - 1) / log_base2k; let in_cols: usize = (in_logq + log_base2k - 1) / log_base2k;
let res_cols: usize = (res_logq + log_base2k - 1) / log_base2k; let res_cols: usize = (res_logq + log_base2k - 1) / log_base2k;
return module.vmp_apply_dft_to_dft_tmp_bytes(res_cols, in_cols, in_cols, gct_cols) return module.vmp_apply_dft_to_dft_tmp_bytes(res_cols, in_cols, in_cols, gct_cols)
+ module.bytes_of_vec_znx_dft(std::cmp::min(res_cols, in_cols)) + module.bytes_of_vec_znx_dft(1, std::cmp::min(res_cols, in_cols))
+ 2 * module.bytes_of_vec_znx_dft(gct_cols); + 2 * module.bytes_of_vec_znx_dft(1, gct_cols);
} }
pub fn rgsw_product( pub fn rgsw_product(
@@ -40,13 +40,13 @@ pub fn rgsw_product(
assert_alignement(tmp_bytes.as_ptr()); assert_alignement(tmp_bytes.as_ptr());
} }
let (tmp_bytes_ai_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(a.cols())); let (tmp_bytes_ai_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, a.cols()));
let (tmp_bytes_c0_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(b_cols)); let (tmp_bytes_c0_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, b_cols));
let (tmp_bytes_c1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(b_cols)); let (tmp_bytes_c1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, b_cols));
let mut ai_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(a.cols(), tmp_bytes_ai_dft); let mut ai_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, a.cols(), tmp_bytes_ai_dft);
let mut c0_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(b_cols, tmp_bytes_c0_dft); let mut c0_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, b_cols, tmp_bytes_c0_dft);
let mut c1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(b_cols, tmp_bytes_c1_dft); let mut c1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, b_cols, tmp_bytes_c1_dft);
let mut c0_big: VecZnxBig = c0_dft.as_vec_znx_big(); let mut c0_big: VecZnxBig = c0_dft.as_vec_znx_big();
let mut c1_big: VecZnxBig = c1_dft.as_vec_znx_big(); let mut c1_big: VecZnxBig = c1_dft.as_vec_znx_big();
@@ -82,13 +82,13 @@ pub fn rgsw_product_inplace(
assert_alignement(tmp_bytes.as_ptr()); assert_alignement(tmp_bytes.as_ptr());
} }
let (tmp_bytes_ai_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(a.cols())); let (tmp_bytes_ai_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, a.cols()));
let (tmp_bytes_c0_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(b_cols)); let (tmp_bytes_c0_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, b_cols));
let (tmp_bytes_c1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(b_cols)); let (tmp_bytes_c1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, b_cols));
let mut ai_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(a.cols(), tmp_bytes_ai_dft); let mut ai_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, a.cols(), tmp_bytes_ai_dft);
let mut c0_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(b_cols, tmp_bytes_c0_dft); let mut c0_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, b_cols, tmp_bytes_c0_dft);
let mut c1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(b_cols, tmp_bytes_c1_dft); let mut c1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, b_cols, tmp_bytes_c1_dft);
let mut c0_big: VecZnxBig = c0_dft.as_vec_znx_big(); let mut c0_big: VecZnxBig = c0_dft.as_vec_znx_big();
let mut c1_big: VecZnxBig = c1_dft.as_vec_znx_big(); let mut c1_big: VecZnxBig = c1_dft.as_vec_znx_big();

View File

@@ -22,7 +22,7 @@ impl Parameters {
pub fn trace_tmp_bytes(module: &Module, c_cols: usize, a_cols: usize, b_rows: usize, b_cols: usize) -> usize { pub fn trace_tmp_bytes(module: &Module, c_cols: usize, a_cols: usize, b_rows: usize, b_cols: usize) -> usize {
return module.vmp_apply_dft_to_dft_tmp_bytes(c_cols, a_cols, b_rows, b_cols) return module.vmp_apply_dft_to_dft_tmp_bytes(c_cols, a_cols, b_rows, b_cols)
+ 2 * module.bytes_of_vec_znx_dft(std::cmp::min(c_cols, a_cols)); + 2 * module.bytes_of_vec_znx_dft(1, std::cmp::min(c_cols, a_cols));
} }
pub fn trace_inplace( pub fn trace_inplace(
@@ -59,11 +59,11 @@ pub fn trace_inplace(
let cols: usize = std::cmp::min(b_cols, a.cols()); let cols: usize = std::cmp::min(b_cols, a.cols());
let (tmp_bytes_b1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(cols)); let (tmp_bytes_b1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
let (tmp_bytes_res_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(b_cols)); let (tmp_bytes_res_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, b_cols));
let mut a1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(cols, tmp_bytes_b1_dft); let mut a1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, cols, tmp_bytes_b1_dft);
let mut res_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(b_cols, tmp_bytes_res_dft); let mut res_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, b_cols, tmp_bytes_res_dft);
let mut res_big: VecZnxBig = res_dft.as_vec_znx_big(); let mut res_big: VecZnxBig = res_dft.as_vec_znx_big();
let log_base2k: usize = a.log_base2k(); let log_base2k: usize = a.log_base2k();

View File

@@ -45,7 +45,7 @@ impl Source {
min + ((self.next_u64() << 11 >> 11) as f64) / MAXF64 * (max - min) min + ((self.next_u64() << 11 >> 11) as f64) / MAXF64 * (max - min)
} }
pub fn next_i64(&mut self) -> i64{ pub fn next_i64(&mut self) -> i64 {
self.next_u64() as i64 self.next_u64() as i64
} }
} }