mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 13:16:44 +01:00
more refactoring
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
pub trait Infos {
|
||||
use crate::{Backend, Module};
|
||||
|
||||
pub trait ZnxInfos {
|
||||
/// Returns the ring degree of the polynomials.
|
||||
fn n(&self) -> usize;
|
||||
|
||||
@@ -18,20 +20,34 @@ pub trait Infos {
|
||||
fn poly_count(&self) -> usize;
|
||||
}
|
||||
|
||||
pub trait VecZnxLayout: Infos {
|
||||
pub trait ZnxBase<B: Backend> {
|
||||
type Scalar;
|
||||
fn new(module: &Module<B>, cols: usize, limbs: usize) -> Self;
|
||||
fn from_bytes(module: &Module<B>, cols: usize, limbs: usize, bytes: &mut [u8]) -> Self;
|
||||
fn from_bytes_borrow(module: &Module<B>, cols: usize, limbs: usize, bytes: &mut [u8]) -> Self;
|
||||
fn bytes_of(module: &Module<B>, cols: usize, limbs: usize) -> usize;
|
||||
}
|
||||
|
||||
pub trait ZnxLayout: ZnxInfos {
|
||||
type Scalar;
|
||||
|
||||
/// Returns a non-mutable pointer to the underlying coefficients array.
|
||||
fn as_ptr(&self) -> *const Self::Scalar;
|
||||
|
||||
/// Returns a mutable pointer to the underlying coefficients array.
|
||||
fn as_mut_ptr(&mut self) -> *mut Self::Scalar;
|
||||
|
||||
/// Returns a non-mutable reference to the entire underlying coefficient array.
|
||||
fn raw(&self) -> &[Self::Scalar] {
|
||||
unsafe { std::slice::from_raw_parts(self.as_ptr(), self.n() * self.poly_count()) }
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the entire underlying coefficient array.
|
||||
fn raw_mut(&mut self) -> &mut [Self::Scalar] {
|
||||
unsafe { std::slice::from_raw_parts_mut(self.as_mut_ptr(), self.n() * self.poly_count()) }
|
||||
}
|
||||
|
||||
/// Returns a non-mutable pointer starting at the (i, j)-th small polynomial.
|
||||
fn at_ptr(&self, i: usize, j: usize) -> *const Self::Scalar {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
@@ -42,6 +58,7 @@ pub trait VecZnxLayout: Infos {
|
||||
unsafe { self.as_ptr().add(offset) }
|
||||
}
|
||||
|
||||
/// Returns a mutable pointer starting at the (i, j)-th small polynomial.
|
||||
fn at_mut_ptr(&mut self, i: usize, j: usize) -> *mut Self::Scalar {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
@@ -52,18 +69,22 @@ pub trait VecZnxLayout: Infos {
|
||||
unsafe { self.as_mut_ptr().add(offset) }
|
||||
}
|
||||
|
||||
/// Returns non-mutable reference to the (i, j)-th small polynomial.
|
||||
fn at_poly(&self, i: usize, j: usize) -> &[Self::Scalar] {
|
||||
unsafe { std::slice::from_raw_parts(self.at_ptr(i, j), self.n()) }
|
||||
}
|
||||
|
||||
/// Returns mutable reference to the (i, j)-th small polynomial.
|
||||
fn at_poly_mut(&mut self, i: usize, j: usize) -> &mut [Self::Scalar] {
|
||||
unsafe { std::slice::from_raw_parts_mut(self.at_mut_ptr(i, j), self.n()) }
|
||||
}
|
||||
|
||||
/// Returns non-mutable reference to the i-th limb.
|
||||
fn at_limb(&self, j: usize) -> &[Self::Scalar] {
|
||||
unsafe { std::slice::from_raw_parts(self.at_ptr(0, j), self.n() * self.cols()) }
|
||||
}
|
||||
|
||||
/// Returns mutable reference to the i-th limb.
|
||||
fn at_limb_mut(&mut self, j: usize) -> &mut [Self::Scalar] {
|
||||
unsafe { std::slice::from_raw_parts_mut(self.at_mut_ptr(0, j), self.n() * self.cols()) }
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::ffi::znx::znx_zero_i64_ref;
|
||||
use crate::{Infos, VecZnx, VecZnxLayout};
|
||||
use crate::{VecZnx, ZnxInfos, ZnxLayout};
|
||||
use itertools::izip;
|
||||
use rug::{Assign, Float};
|
||||
use std::cmp::min;
|
||||
@@ -262,17 +262,18 @@ fn decode_coeff_i64(a: &VecZnx, col_i: usize, log_base2k: usize, log_k: usize, i
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{Encoding, Infos, VecZnx, VecZnxLayout};
|
||||
use crate::{Encoding, FFT64, Module, VecZnx, ZnxBase, ZnxInfos, ZnxLayout};
|
||||
use itertools::izip;
|
||||
use sampling::source::Source;
|
||||
|
||||
#[test]
|
||||
fn test_set_get_i64_lo_norm() {
|
||||
let n: usize = 8;
|
||||
let module: Module<FFT64> = Module::<FFT64>::new(n);
|
||||
let log_base2k: usize = 17;
|
||||
let cols: usize = 5;
|
||||
let log_k: usize = cols * log_base2k - 5;
|
||||
let mut a: VecZnx = VecZnx::new(n, 2, cols);
|
||||
let mut a: VecZnx = VecZnx::new(&module, 2, cols);
|
||||
let mut source: Source = Source::new([0u8; 32]);
|
||||
let raw: &mut [i64] = a.raw_mut();
|
||||
raw.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
|
||||
@@ -290,10 +291,11 @@ mod tests {
|
||||
#[test]
|
||||
fn test_set_get_i64_hi_norm() {
|
||||
let n: usize = 8;
|
||||
let module: Module<FFT64> = Module::<FFT64>::new(n);
|
||||
let log_base2k: usize = 17;
|
||||
let cols: usize = 5;
|
||||
let log_k: usize = cols * log_base2k - 5;
|
||||
let mut a: VecZnx = VecZnx::new(n, 2, cols);
|
||||
let mut a: VecZnx = VecZnx::new(&module, 2, cols);
|
||||
let mut source = Source::new([0u8; 32]);
|
||||
let raw: &mut [i64] = a.raw_mut();
|
||||
raw.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
|
||||
|
||||
@@ -3,26 +3,26 @@ pub mod encoding;
|
||||
#[allow(non_camel_case_types, non_snake_case, non_upper_case_globals, dead_code, improper_ctypes)]
|
||||
// Other modules and exports
|
||||
pub mod ffi;
|
||||
pub mod mat_znx_dft;
|
||||
pub mod module;
|
||||
pub mod sampling;
|
||||
pub mod scalar_znx_dft;
|
||||
pub mod stats;
|
||||
pub mod svp;
|
||||
pub mod vec_znx;
|
||||
pub mod vec_znx_big;
|
||||
pub mod vec_znx_dft;
|
||||
pub mod vmp;
|
||||
|
||||
pub use commons::*;
|
||||
pub use encoding::*;
|
||||
pub use mat_znx_dft::*;
|
||||
pub use module::*;
|
||||
pub use sampling::*;
|
||||
pub use scalar_znx_dft::*;
|
||||
#[allow(unused_imports)]
|
||||
pub use stats::*;
|
||||
pub use svp::*;
|
||||
pub use vec_znx::*;
|
||||
pub use vec_znx_big::*;
|
||||
pub use vec_znx_dft::*;
|
||||
pub use vmp::*;
|
||||
|
||||
pub const GALOISGENERATOR: u64 = 5;
|
||||
pub const DEFAULTALIGN: usize = 64;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::ffi::vec_znx_big::vec_znx_big_t;
|
||||
use crate::ffi::vec_znx_dft::vec_znx_dft_t;
|
||||
use crate::ffi::vmp::{self, vmp_pmat_t};
|
||||
use crate::{Backend, FFT64, Infos, Module, VecZnx, VecZnxBig, VecZnxDft, VecZnxLayout, alloc_aligned, assert_alignement};
|
||||
use crate::{Backend, FFT64, Module, VecZnx, VecZnxBig, VecZnxDft, ZnxInfos, ZnxLayout, alloc_aligned, assert_alignement};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
/// Vector Matrix Product Prepared Matrix: a vector of [VecZnx],
|
||||
@@ -10,7 +10,7 @@ use std::marker::PhantomData;
|
||||
///
|
||||
/// [VmpPMat] is used to permform a vector matrix product between a [VecZnx]/[VecZnxDft] and a [VmpPMat].
|
||||
/// See the trait [VmpPMatOps] for additional information.
|
||||
pub struct VmpPMat<B: Backend> {
|
||||
pub struct MatZnxDft<B: Backend> {
|
||||
/// Raw data, is empty if borrowing scratch space.
|
||||
data: Vec<u8>,
|
||||
/// Pointer to data. Can point to scratch space.
|
||||
@@ -26,7 +26,7 @@ pub struct VmpPMat<B: Backend> {
|
||||
_marker: PhantomData<B>,
|
||||
}
|
||||
|
||||
impl<B: Backend> Infos for VmpPMat<B> {
|
||||
impl<B: Backend> ZnxInfos for MatZnxDft<B> {
|
||||
fn n(&self) -> usize {
|
||||
self.n
|
||||
}
|
||||
@@ -52,11 +52,11 @@ impl<B: Backend> Infos for VmpPMat<B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl VmpPMat<FFT64> {
|
||||
fn new(module: &Module<FFT64>, rows: usize, cols: usize, limbs: usize) -> VmpPMat<FFT64> {
|
||||
let mut data: Vec<u8> = alloc_aligned::<u8>(module.bytes_of_vmp_pmat(rows, cols, limbs));
|
||||
impl MatZnxDft<FFT64> {
|
||||
fn new(module: &Module<FFT64>, rows: usize, cols: usize, limbs: usize) -> MatZnxDft<FFT64> {
|
||||
let mut data: Vec<u8> = alloc_aligned::<u8>(module.bytes_of_mat_znx_dft(rows, cols, limbs));
|
||||
let ptr: *mut u8 = data.as_mut_ptr();
|
||||
VmpPMat::<FFT64> {
|
||||
MatZnxDft::<FFT64> {
|
||||
data: data,
|
||||
ptr: ptr,
|
||||
n: module.n(),
|
||||
@@ -126,8 +126,8 @@ impl VmpPMat<FFT64> {
|
||||
|
||||
/// This trait implements methods for vector matrix product,
|
||||
/// that is, multiplying a [VecZnx] with a [VmpPMat].
|
||||
pub trait VmpPMatOps<B: Backend> {
|
||||
fn bytes_of_vmp_pmat(&self, rows: usize, cols: usize, limbs: usize) -> usize;
|
||||
pub trait MatZnxDftOps<B: Backend> {
|
||||
fn bytes_of_mat_znx_dft(&self, rows: usize, cols: usize, limbs: usize) -> usize;
|
||||
|
||||
/// Allocates a new [VmpPMat] with the given number of rows and columns.
|
||||
///
|
||||
@@ -135,7 +135,7 @@ pub trait VmpPMatOps<B: Backend> {
|
||||
///
|
||||
/// * `rows`: number of rows (number of [VecZnxDft]).
|
||||
/// * `size`: number of size (number of size of each [VecZnxDft]).
|
||||
fn new_vmp_pmat(&self, rows: usize, cols: usize, limbs: usize) -> VmpPMat<B>;
|
||||
fn new_mat_znx_dft(&self, rows: usize, cols: usize, limbs: usize) -> MatZnxDft<B>;
|
||||
|
||||
/// Returns the number of bytes needed as scratch space for [VmpPMatOps::vmp_prepare_contiguous].
|
||||
///
|
||||
@@ -154,7 +154,7 @@ pub trait VmpPMatOps<B: Backend> {
|
||||
/// * `b`: [VmpPMat] on which the values are encoded.
|
||||
/// * `a`: the contiguous array of [i64] of the 3D matrix to encode on the [VmpPMat].
|
||||
/// * `buf`: scratch space, the size of buf can be obtained with [VmpPMatOps::vmp_prepare_tmp_bytes].
|
||||
fn vmp_prepare_contiguous(&self, b: &mut VmpPMat<B>, a: &[i64], buf: &mut [u8]);
|
||||
fn vmp_prepare_contiguous(&self, b: &mut MatZnxDft<B>, a: &[i64], buf: &mut [u8]);
|
||||
|
||||
/// Prepares the ith-row of [VmpPMat] from a [VecZnx].
|
||||
///
|
||||
@@ -166,7 +166,7 @@ pub trait VmpPMatOps<B: Backend> {
|
||||
/// * `buf`: scratch space, the size of buf can be obtained with [VmpPMatOps::vmp_prepare_tmp_bytes].
|
||||
///
|
||||
/// The size of buf can be obtained with [VmpPMatOps::vmp_prepare_tmp_bytes].
|
||||
fn vmp_prepare_row(&self, b: &mut VmpPMat<B>, a: &[i64], row_i: usize, tmp_bytes: &mut [u8]);
|
||||
fn vmp_prepare_row(&self, b: &mut MatZnxDft<B>, a: &[i64], row_i: usize, tmp_bytes: &mut [u8]);
|
||||
|
||||
/// Extracts the ith-row of [VmpPMat] into a [VecZnxBig].
|
||||
///
|
||||
@@ -175,7 +175,7 @@ pub trait VmpPMatOps<B: Backend> {
|
||||
/// * `b`: the [VecZnxBig] to on which to extract the row of the [VmpPMat].
|
||||
/// * `a`: [VmpPMat] on which the values are encoded.
|
||||
/// * `row_i`: the index of the row to extract.
|
||||
fn vmp_extract_row(&self, b: &mut VecZnxBig<B>, a: &VmpPMat<B>, row_i: usize);
|
||||
fn vmp_extract_row(&self, b: &mut VecZnxBig<B>, a: &MatZnxDft<B>, row_i: usize);
|
||||
|
||||
/// Prepares the ith-row of [VmpPMat] from a [VecZnxDft].
|
||||
///
|
||||
@@ -186,7 +186,7 @@ pub trait VmpPMatOps<B: Backend> {
|
||||
/// * `row_i`: the index of the row to prepare.
|
||||
///
|
||||
/// The size of buf can be obtained with [VmpPMatOps::vmp_prepare_tmp_bytes].
|
||||
fn vmp_prepare_row_dft(&self, b: &mut VmpPMat<B>, a: &VecZnxDft<B>, row_i: usize);
|
||||
fn vmp_prepare_row_dft(&self, b: &mut MatZnxDft<B>, a: &VecZnxDft<B>, row_i: usize);
|
||||
|
||||
/// Extracts the ith-row of [VmpPMat] into a [VecZnxDft].
|
||||
///
|
||||
@@ -195,7 +195,7 @@ pub trait VmpPMatOps<B: Backend> {
|
||||
/// * `b`: the [VecZnxDft] to on which to extract the row of the [VmpPMat].
|
||||
/// * `a`: [VmpPMat] on which the values are encoded.
|
||||
/// * `row_i`: the index of the row to extract.
|
||||
fn vmp_extract_row_dft(&self, b: &mut VecZnxDft<B>, a: &VmpPMat<B>, row_i: usize);
|
||||
fn vmp_extract_row_dft(&self, b: &mut VecZnxDft<B>, a: &MatZnxDft<B>, row_i: usize);
|
||||
|
||||
/// Returns the size of the stratch space necessary for [VmpPMatOps::vmp_apply_dft].
|
||||
///
|
||||
@@ -231,7 +231,7 @@ pub trait VmpPMatOps<B: Backend> {
|
||||
/// * `a`: the left operand [VecZnx] of the vector matrix product.
|
||||
/// * `b`: the right operand [VmpPMat] of the vector matrix product.
|
||||
/// * `buf`: scratch space, the size can be obtained with [VmpPMatOps::vmp_apply_dft_tmp_bytes].
|
||||
fn vmp_apply_dft(&self, c: &mut VecZnxDft<B>, a: &VecZnx, b: &VmpPMat<B>, buf: &mut [u8]);
|
||||
fn vmp_apply_dft(&self, c: &mut VecZnxDft<B>, a: &VecZnx, b: &MatZnxDft<B>, buf: &mut [u8]);
|
||||
|
||||
/// Applies the vector matrix product [VecZnxDft] x [VmpPMat] and adds on the receiver.
|
||||
///
|
||||
@@ -257,7 +257,7 @@ pub trait VmpPMatOps<B: Backend> {
|
||||
/// * `a`: the left operand [VecZnx] of the vector matrix product.
|
||||
/// * `b`: the right operand [VmpPMat] of the vector matrix product.
|
||||
/// * `buf`: scratch space, the size can be obtained with [VmpPMatOps::vmp_apply_dft_tmp_bytes].
|
||||
fn vmp_apply_dft_add(&self, c: &mut VecZnxDft<B>, a: &VecZnx, b: &VmpPMat<B>, buf: &mut [u8]);
|
||||
fn vmp_apply_dft_add(&self, c: &mut VecZnxDft<B>, a: &VecZnx, b: &MatZnxDft<B>, buf: &mut [u8]);
|
||||
|
||||
/// Returns the size of the stratch space necessary for [VmpPMatOps::vmp_apply_dft_to_dft].
|
||||
///
|
||||
@@ -294,7 +294,7 @@ pub trait VmpPMatOps<B: Backend> {
|
||||
/// * `a`: the left operand [VecZnxDft] of the vector matrix product.
|
||||
/// * `b`: the right operand [VmpPMat] of the vector matrix product.
|
||||
/// * `buf`: scratch space, the size can be obtained with [VmpPMatOps::vmp_apply_dft_to_dft_tmp_bytes].
|
||||
fn vmp_apply_dft_to_dft(&self, c: &mut VecZnxDft<B>, a: &VecZnxDft<B>, b: &VmpPMat<B>, buf: &mut [u8]);
|
||||
fn vmp_apply_dft_to_dft(&self, c: &mut VecZnxDft<B>, a: &VecZnxDft<B>, b: &MatZnxDft<B>, buf: &mut [u8]);
|
||||
|
||||
/// Applies the vector matrix product [VecZnxDft] x [VmpPMat] and adds on top of the receiver instead of overwritting it.
|
||||
/// The size of `buf` is given by [VmpPMatOps::vmp_apply_dft_to_dft_tmp_bytes].
|
||||
@@ -321,7 +321,7 @@ pub trait VmpPMatOps<B: Backend> {
|
||||
/// * `a`: the left operand [VecZnxDft] of the vector matrix product.
|
||||
/// * `b`: the right operand [VmpPMat] of the vector matrix product.
|
||||
/// * `buf`: scratch space, the size can be obtained with [VmpPMatOps::vmp_apply_dft_to_dft_tmp_bytes].
|
||||
fn vmp_apply_dft_to_dft_add(&self, c: &mut VecZnxDft<B>, a: &VecZnxDft<B>, b: &VmpPMat<B>, buf: &mut [u8]);
|
||||
fn vmp_apply_dft_to_dft_add(&self, c: &mut VecZnxDft<B>, a: &VecZnxDft<B>, b: &MatZnxDft<B>, buf: &mut [u8]);
|
||||
|
||||
/// Applies the vector matrix product [VecZnxDft] x [VmpPMat] in place.
|
||||
/// The size of `buf` is given by [VmpPMatOps::vmp_apply_dft_to_dft_tmp_bytes].
|
||||
@@ -347,15 +347,15 @@ pub trait VmpPMatOps<B: Backend> {
|
||||
/// * `b`: the input and output of the vector matrix product, as a [VecZnxDft].
|
||||
/// * `a`: the right operand [VmpPMat] of the vector matrix product.
|
||||
/// * `buf`: scratch space, the size can be obtained with [VmpPMatOps::vmp_apply_dft_to_dft_tmp_bytes].
|
||||
fn vmp_apply_dft_to_dft_inplace(&self, b: &mut VecZnxDft<B>, a: &VmpPMat<B>, buf: &mut [u8]);
|
||||
fn vmp_apply_dft_to_dft_inplace(&self, b: &mut VecZnxDft<B>, a: &MatZnxDft<B>, buf: &mut [u8]);
|
||||
}
|
||||
|
||||
impl VmpPMatOps<FFT64> for Module<FFT64> {
|
||||
fn new_vmp_pmat(&self, rows: usize, cols: usize, limbs: usize) -> VmpPMat<FFT64> {
|
||||
VmpPMat::<FFT64>::new(self, rows, cols, limbs)
|
||||
impl MatZnxDftOps<FFT64> for Module<FFT64> {
|
||||
fn new_mat_znx_dft(&self, rows: usize, cols: usize, limbs: usize) -> MatZnxDft<FFT64> {
|
||||
MatZnxDft::<FFT64>::new(self, rows, cols, limbs)
|
||||
}
|
||||
|
||||
fn bytes_of_vmp_pmat(&self, rows: usize, cols: usize, limbs: usize) -> usize {
|
||||
fn bytes_of_mat_znx_dft(&self, rows: usize, cols: usize, limbs: usize) -> usize {
|
||||
unsafe { vmp::bytes_of_vmp_pmat(self.ptr, rows as u64, (limbs * cols) as u64) as usize }
|
||||
}
|
||||
|
||||
@@ -363,7 +363,7 @@ impl VmpPMatOps<FFT64> for Module<FFT64> {
|
||||
unsafe { vmp::vmp_prepare_tmp_bytes(self.ptr, rows as u64, (size * cols) as u64) as usize }
|
||||
}
|
||||
|
||||
fn vmp_prepare_contiguous(&self, b: &mut VmpPMat<FFT64>, a: &[i64], tmp_bytes: &mut [u8]) {
|
||||
fn vmp_prepare_contiguous(&self, b: &mut MatZnxDft<FFT64>, a: &[i64], tmp_bytes: &mut [u8]) {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert_eq!(a.len(), b.n() * b.poly_count());
|
||||
@@ -382,7 +382,7 @@ impl VmpPMatOps<FFT64> for Module<FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
fn vmp_prepare_row(&self, b: &mut VmpPMat<FFT64>, a: &[i64], row_i: usize, tmp_bytes: &mut [u8]) {
|
||||
fn vmp_prepare_row(&self, b: &mut MatZnxDft<FFT64>, a: &[i64], row_i: usize, tmp_bytes: &mut [u8]) {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert_eq!(a.len(), b.limbs() * self.n() * b.cols());
|
||||
@@ -402,7 +402,7 @@ impl VmpPMatOps<FFT64> for Module<FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
fn vmp_extract_row(&self, b: &mut VecZnxBig<FFT64>, a: &VmpPMat<FFT64>, row_i: usize) {
|
||||
fn vmp_extract_row(&self, b: &mut VecZnxBig<FFT64>, a: &MatZnxDft<FFT64>, row_i: usize) {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert_eq!(a.n(), b.n());
|
||||
@@ -421,7 +421,7 @@ impl VmpPMatOps<FFT64> for Module<FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
fn vmp_prepare_row_dft(&self, b: &mut VmpPMat<FFT64>, a: &VecZnxDft<FFT64>, row_i: usize) {
|
||||
fn vmp_prepare_row_dft(&self, b: &mut MatZnxDft<FFT64>, a: &VecZnxDft<FFT64>, row_i: usize) {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert_eq!(a.n(), b.n());
|
||||
@@ -439,7 +439,7 @@ impl VmpPMatOps<FFT64> for Module<FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
fn vmp_extract_row_dft(&self, b: &mut VecZnxDft<FFT64>, a: &VmpPMat<FFT64>, row_i: usize) {
|
||||
fn vmp_extract_row_dft(&self, b: &mut VecZnxDft<FFT64>, a: &MatZnxDft<FFT64>, row_i: usize) {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert_eq!(a.n(), b.n());
|
||||
@@ -469,7 +469,7 @@ impl VmpPMatOps<FFT64> for Module<FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
fn vmp_apply_dft(&self, c: &mut VecZnxDft<FFT64>, a: &VecZnx, b: &VmpPMat<FFT64>, tmp_bytes: &mut [u8]) {
|
||||
fn vmp_apply_dft(&self, c: &mut VecZnxDft<FFT64>, a: &VecZnx, b: &MatZnxDft<FFT64>, tmp_bytes: &mut [u8]) {
|
||||
debug_assert!(tmp_bytes.len() >= self.vmp_apply_dft_tmp_bytes(c.limbs(), a.limbs(), b.rows(), b.limbs()));
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
@@ -491,7 +491,7 @@ impl VmpPMatOps<FFT64> for Module<FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
fn vmp_apply_dft_add(&self, c: &mut VecZnxDft<FFT64>, a: &VecZnx, b: &VmpPMat<FFT64>, tmp_bytes: &mut [u8]) {
|
||||
fn vmp_apply_dft_add(&self, c: &mut VecZnxDft<FFT64>, a: &VecZnx, b: &MatZnxDft<FFT64>, tmp_bytes: &mut [u8]) {
|
||||
debug_assert!(tmp_bytes.len() >= self.vmp_apply_dft_tmp_bytes(c.limbs(), a.limbs(), b.rows(), b.limbs()));
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
@@ -525,7 +525,7 @@ impl VmpPMatOps<FFT64> for Module<FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
fn vmp_apply_dft_to_dft(&self, c: &mut VecZnxDft<FFT64>, a: &VecZnxDft<FFT64>, b: &VmpPMat<FFT64>, tmp_bytes: &mut [u8]) {
|
||||
fn vmp_apply_dft_to_dft(&self, c: &mut VecZnxDft<FFT64>, a: &VecZnxDft<FFT64>, b: &MatZnxDft<FFT64>, tmp_bytes: &mut [u8]) {
|
||||
debug_assert!(tmp_bytes.len() >= self.vmp_apply_dft_to_dft_tmp_bytes(c.limbs(), a.limbs(), b.rows(), b.limbs()));
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
@@ -546,7 +546,13 @@ impl VmpPMatOps<FFT64> for Module<FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
fn vmp_apply_dft_to_dft_add(&self, c: &mut VecZnxDft<FFT64>, a: &VecZnxDft<FFT64>, b: &VmpPMat<FFT64>, tmp_bytes: &mut [u8]) {
|
||||
fn vmp_apply_dft_to_dft_add(
|
||||
&self,
|
||||
c: &mut VecZnxDft<FFT64>,
|
||||
a: &VecZnxDft<FFT64>,
|
||||
b: &MatZnxDft<FFT64>,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
debug_assert!(tmp_bytes.len() >= self.vmp_apply_dft_to_dft_tmp_bytes(c.limbs(), a.limbs(), b.rows(), b.limbs()));
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
@@ -567,7 +573,7 @@ impl VmpPMatOps<FFT64> for Module<FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
fn vmp_apply_dft_to_dft_inplace(&self, b: &mut VecZnxDft<FFT64>, a: &VmpPMat<FFT64>, tmp_bytes: &mut [u8]) {
|
||||
fn vmp_apply_dft_to_dft_inplace(&self, b: &mut VecZnxDft<FFT64>, a: &MatZnxDft<FFT64>, tmp_bytes: &mut [u8]) {
|
||||
debug_assert!(tmp_bytes.len() >= self.vmp_apply_dft_to_dft_tmp_bytes(b.limbs(), b.limbs(), a.rows(), a.limbs()));
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
@@ -592,8 +598,8 @@ impl VmpPMatOps<FFT64> for Module<FFT64> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{
|
||||
FFT64, Module, Sampling, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps, VecZnxLayout, VecZnxOps, VmpPMat,
|
||||
VmpPMatOps, alloc_aligned,
|
||||
FFT64, MatZnxDft, MatZnxDftOps, Module, Sampling, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps, VecZnxOps,
|
||||
ZnxLayout, alloc_aligned,
|
||||
};
|
||||
use sampling::source::Source;
|
||||
|
||||
@@ -608,8 +614,8 @@ mod tests {
|
||||
let mut a_big: VecZnxBig<FFT64> = module.new_vec_znx_big(1, vpmat_size);
|
||||
let mut b_big: VecZnxBig<FFT64> = module.new_vec_znx_big(1, vpmat_size);
|
||||
let mut b_dft: VecZnxDft<FFT64> = module.new_vec_znx_dft(1, vpmat_size);
|
||||
let mut vmpmat_0: VmpPMat<FFT64> = module.new_vmp_pmat(vpmat_rows, 1, vpmat_size);
|
||||
let mut vmpmat_1: VmpPMat<FFT64> = module.new_vmp_pmat(vpmat_rows, 1, vpmat_size);
|
||||
let mut vmpmat_0: MatZnxDft<FFT64> = module.new_mat_znx_dft(vpmat_rows, 1, vpmat_size);
|
||||
let mut vmpmat_1: MatZnxDft<FFT64> = module.new_mat_znx_dft(vpmat_rows, 1, vpmat_size);
|
||||
|
||||
let mut tmp_bytes: Vec<u8> = alloc_aligned(module.vmp_prepare_tmp_bytes(vpmat_rows, 1, vpmat_size));
|
||||
|
||||
@@ -619,15 +625,15 @@ mod tests {
|
||||
module.vec_znx_dft(&mut a_dft, &a);
|
||||
module.vmp_prepare_row(&mut vmpmat_0, &a.raw(), row_i, &mut tmp_bytes);
|
||||
|
||||
// Checks that prepare(vmp_pmat, a) = prepare_dft(vmp_pmat, a_dft)
|
||||
// Checks that prepare(mat_znx_dft, a) = prepare_dft(mat_znx_dft, a_dft)
|
||||
module.vmp_prepare_row_dft(&mut vmpmat_1, &a_dft, row_i);
|
||||
assert_eq!(vmpmat_0.raw(), vmpmat_1.raw());
|
||||
|
||||
// Checks that a_dft = extract_dft(prepare(vmp_pmat, a), b_dft)
|
||||
// Checks that a_dft = extract_dft(prepare(mat_znx_dft, a), b_dft)
|
||||
module.vmp_extract_row_dft(&mut b_dft, &vmpmat_0, row_i);
|
||||
assert_eq!(a_dft.raw(), b_dft.raw());
|
||||
|
||||
// Checks that a_big = extract(prepare_dft(vmp_pmat, a_dft), b_big)
|
||||
// Checks that a_big = extract(prepare_dft(mat_znx_dft, a_dft), b_big)
|
||||
module.vmp_extract_row(&mut b_big, &vmpmat_0, row_i);
|
||||
module.vec_znx_idft(&mut a_big, &a_dft, &mut tmp_bytes);
|
||||
assert_eq!(a_big.raw(), b_big.raw());
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{Backend, Infos, Module, VecZnx, VecZnxLayout};
|
||||
use crate::{Backend, Module, VecZnx, ZnxInfos, ZnxLayout};
|
||||
use rand_distr::{Distribution, Normal};
|
||||
use sampling::source::Source;
|
||||
|
||||
|
||||
@@ -2,9 +2,9 @@ use std::marker::PhantomData;
|
||||
|
||||
use crate::ffi::svp::{self, svp_ppol_t};
|
||||
use crate::ffi::vec_znx_dft::vec_znx_dft_t;
|
||||
use crate::{Backend, FFT64, Module, VecZnx, VecZnxDft, VecZnxLayout, assert_alignement};
|
||||
use crate::{Backend, FFT64, Module, VecZnx, VecZnxDft, ZnxLayout, assert_alignement};
|
||||
|
||||
use crate::{Infos, alloc_aligned, cast_mut};
|
||||
use crate::{ZnxInfos, alloc_aligned, cast_mut};
|
||||
use rand::seq::SliceRandom;
|
||||
use rand_core::RngCore;
|
||||
use rand_distr::{Distribution, weighted::WeightedIndex};
|
||||
@@ -148,7 +148,7 @@ impl<B: Backend> ScalarOps for Module<B> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SvpPPol<B: Backend> {
|
||||
pub struct ScalarZnxDft<B: Backend> {
|
||||
pub n: usize,
|
||||
pub data: Vec<u8>,
|
||||
pub ptr: *mut u8,
|
||||
@@ -157,7 +157,7 @@ pub struct SvpPPol<B: Backend> {
|
||||
|
||||
/// A prepared [crate::Scalar] for [SvpPPolOps::svp_apply_dft].
|
||||
/// An [SvpPPol] an be seen as a [VecZnxDft] of one limb.
|
||||
impl SvpPPol<FFT64> {
|
||||
impl ScalarZnxDft<FFT64> {
|
||||
pub fn new(module: &Module<FFT64>) -> Self {
|
||||
module.new_svp_ppol()
|
||||
}
|
||||
@@ -207,9 +207,9 @@ impl SvpPPol<FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SvpPPolOps<B: Backend> {
|
||||
pub trait ScalarZnxDftOps<B: Backend> {
|
||||
/// Allocates a new [SvpPPol].
|
||||
fn new_svp_ppol(&self) -> SvpPPol<B>;
|
||||
fn new_svp_ppol(&self) -> ScalarZnxDft<B>;
|
||||
|
||||
/// Returns the minimum number of bytes necessary to allocate
|
||||
/// a new [SvpPPol] through [SvpPPol::from_bytes] ro.
|
||||
@@ -218,26 +218,26 @@ pub trait SvpPPolOps<B: Backend> {
|
||||
/// Allocates a new [SvpPPol] from an array of bytes.
|
||||
/// The array of bytes is owned by the [SvpPPol].
|
||||
/// The method will panic if bytes.len() < [SvpPPolOps::bytes_of_svp_ppol]
|
||||
fn new_svp_ppol_from_bytes(&self, bytes: &mut [u8]) -> SvpPPol<B>;
|
||||
fn new_svp_ppol_from_bytes(&self, bytes: &mut [u8]) -> ScalarZnxDft<B>;
|
||||
|
||||
/// Allocates a new [SvpPPol] from an array of bytes.
|
||||
/// The array of bytes is borrowed by the [SvpPPol].
|
||||
/// The method will panic if bytes.len() < [SvpPPolOps::bytes_of_svp_ppol]
|
||||
fn new_svp_ppol_from_bytes_borrow(&self, tmp_bytes: &mut [u8]) -> SvpPPol<B>;
|
||||
fn new_svp_ppol_from_bytes_borrow(&self, tmp_bytes: &mut [u8]) -> ScalarZnxDft<B>;
|
||||
|
||||
/// Prepares a [crate::Scalar] for a [SvpPPolOps::svp_apply_dft].
|
||||
fn svp_prepare(&self, svp_ppol: &mut SvpPPol<B>, a: &Scalar);
|
||||
fn svp_prepare(&self, svp_ppol: &mut ScalarZnxDft<B>, a: &Scalar);
|
||||
|
||||
/// Applies the [SvpPPol] x [VecZnxDft] product, where each limb of
|
||||
/// the [VecZnxDft] is multiplied with [SvpPPol].
|
||||
fn svp_apply_dft(&self, c: &mut VecZnxDft<B>, a: &SvpPPol<B>, b: &VecZnx);
|
||||
fn svp_apply_dft(&self, c: &mut VecZnxDft<B>, a: &ScalarZnxDft<B>, b: &VecZnx);
|
||||
}
|
||||
|
||||
impl SvpPPolOps<FFT64> for Module<FFT64> {
|
||||
fn new_svp_ppol(&self) -> SvpPPol<FFT64> {
|
||||
impl ScalarZnxDftOps<FFT64> for Module<FFT64> {
|
||||
fn new_svp_ppol(&self) -> ScalarZnxDft<FFT64> {
|
||||
let mut data: Vec<u8> = alloc_aligned::<u8>(self.bytes_of_svp_ppol());
|
||||
let ptr: *mut u8 = data.as_mut_ptr();
|
||||
SvpPPol::<FFT64> {
|
||||
ScalarZnxDft::<FFT64> {
|
||||
data: data,
|
||||
ptr: ptr,
|
||||
n: self.n(),
|
||||
@@ -249,19 +249,19 @@ impl SvpPPolOps<FFT64> for Module<FFT64> {
|
||||
unsafe { svp::bytes_of_svp_ppol(self.ptr) as usize }
|
||||
}
|
||||
|
||||
fn new_svp_ppol_from_bytes(&self, bytes: &mut [u8]) -> SvpPPol<FFT64> {
|
||||
SvpPPol::from_bytes(self, bytes)
|
||||
fn new_svp_ppol_from_bytes(&self, bytes: &mut [u8]) -> ScalarZnxDft<FFT64> {
|
||||
ScalarZnxDft::from_bytes(self, bytes)
|
||||
}
|
||||
|
||||
fn new_svp_ppol_from_bytes_borrow(&self, tmp_bytes: &mut [u8]) -> SvpPPol<FFT64> {
|
||||
SvpPPol::from_bytes_borrow(self, tmp_bytes)
|
||||
fn new_svp_ppol_from_bytes_borrow(&self, tmp_bytes: &mut [u8]) -> ScalarZnxDft<FFT64> {
|
||||
ScalarZnxDft::from_bytes_borrow(self, tmp_bytes)
|
||||
}
|
||||
|
||||
fn svp_prepare(&self, svp_ppol: &mut SvpPPol<FFT64>, a: &Scalar) {
|
||||
fn svp_prepare(&self, svp_ppol: &mut ScalarZnxDft<FFT64>, a: &Scalar) {
|
||||
unsafe { svp::svp_prepare(self.ptr, svp_ppol.ptr as *mut svp_ppol_t, a.as_ptr()) }
|
||||
}
|
||||
|
||||
fn svp_apply_dft(&self, c: &mut VecZnxDft<FFT64>, a: &SvpPPol<FFT64>, b: &VecZnx) {
|
||||
fn svp_apply_dft(&self, c: &mut VecZnxDft<FFT64>, a: &ScalarZnxDft<FFT64>, b: &VecZnx) {
|
||||
unsafe {
|
||||
svp::svp_apply_dft(
|
||||
self.ptr,
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{Encoding, Infos, VecZnx};
|
||||
use crate::{Encoding, VecZnx, ZnxInfos};
|
||||
use rug::Float;
|
||||
use rug::float::Round;
|
||||
use rug::ops::{AddAssignRound, DivAssignRound, SubAssignRound};
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
use crate::Backend;
|
||||
use crate::ZnxBase;
|
||||
use crate::cast_mut;
|
||||
use crate::ffi::vec_znx;
|
||||
use crate::ffi::znx;
|
||||
use crate::{Infos, Module, VecZnxLayout};
|
||||
use crate::{Module, ZnxInfos, ZnxLayout};
|
||||
use crate::{alloc_aligned, assert_alignement};
|
||||
use itertools::izip;
|
||||
use std::cmp::min;
|
||||
@@ -35,7 +36,7 @@ pub struct VecZnx {
|
||||
pub ptr: *mut i64,
|
||||
}
|
||||
|
||||
impl Infos for VecZnx {
|
||||
impl ZnxInfos for VecZnx {
|
||||
fn n(&self) -> usize {
|
||||
self.n
|
||||
}
|
||||
@@ -61,7 +62,7 @@ impl Infos for VecZnx {
|
||||
}
|
||||
}
|
||||
|
||||
impl VecZnxLayout for VecZnx {
|
||||
impl ZnxLayout for VecZnx {
|
||||
type Scalar = i64;
|
||||
|
||||
fn as_ptr(&self) -> *const Self::Scalar {
|
||||
@@ -84,9 +85,12 @@ pub fn copy_vec_znx_from(b: &mut VecZnx, a: &VecZnx) {
|
||||
data_b[..size].copy_from_slice(&data_a[..size])
|
||||
}
|
||||
|
||||
impl VecZnx {
|
||||
impl<B: Backend> ZnxBase<B> for VecZnx {
|
||||
type Scalar = i64;
|
||||
|
||||
/// Allocates a new [VecZnx] composed of #size polynomials of Z\[X\].
|
||||
pub fn new(n: usize, cols: usize, limbs: usize) -> Self {
|
||||
fn new(module: &Module<B>, cols: usize, limbs: usize) -> Self {
|
||||
let n: usize = module.n();
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert!(n > 0);
|
||||
@@ -94,7 +98,7 @@ impl VecZnx {
|
||||
assert!(cols > 0);
|
||||
assert!(limbs > 0);
|
||||
}
|
||||
let mut data: Vec<i64> = alloc_aligned::<i64>(n * cols * limbs);
|
||||
let mut data: Vec<i64> = alloc_aligned::<i64>(Self::bytes_of(module, cols, limbs));
|
||||
let ptr: *mut i64 = data.as_mut_ptr();
|
||||
Self {
|
||||
n: n,
|
||||
@@ -105,6 +109,57 @@ impl VecZnx {
|
||||
}
|
||||
}
|
||||
|
||||
fn bytes_of(module: &Module<B>, cols: usize, limbs: usize) -> usize {
|
||||
module.n() * cols * limbs * size_of::<i64>()
|
||||
}
|
||||
|
||||
/// Returns a new struct implementing [VecZnx] with the provided data as backing array.
|
||||
///
|
||||
/// The struct will take ownership of buf[..[Self::bytes_of]]
|
||||
///
|
||||
/// User must ensure that data is properly alligned and that
|
||||
/// the limbs of data is equal to [Self::bytes_of].
|
||||
fn from_bytes(module: &Module<B>, cols: usize, limbs: usize, bytes: &mut [u8]) -> Self {
|
||||
let n: usize = module.n();
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert!(cols > 0);
|
||||
assert!(limbs > 0);
|
||||
assert_eq!(bytes.len(), Self::bytes_of(module, cols, limbs));
|
||||
assert_alignement(bytes.as_ptr());
|
||||
}
|
||||
unsafe {
|
||||
let bytes_i64: &mut [i64] = cast_mut::<u8, i64>(bytes);
|
||||
let ptr: *mut i64 = bytes_i64.as_mut_ptr();
|
||||
Self {
|
||||
n: n,
|
||||
cols: cols,
|
||||
limbs: limbs,
|
||||
data: Vec::from_raw_parts(ptr, bytes.len(), bytes.len()),
|
||||
ptr: ptr,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn from_bytes_borrow(module: &Module<B>, cols: usize, limbs: usize, bytes: &mut [u8]) -> Self {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert!(cols > 0);
|
||||
assert!(limbs > 0);
|
||||
assert!(bytes.len() >= Self::bytes_of(module, cols, limbs));
|
||||
assert_alignement(bytes.as_ptr());
|
||||
}
|
||||
Self {
|
||||
n: module.n(),
|
||||
cols: cols,
|
||||
limbs: limbs,
|
||||
data: Vec::new(),
|
||||
ptr: bytes.as_mut_ptr() as *mut i64,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl VecZnx {
|
||||
/// Truncates the precision of the [VecZnx] by k bits.
|
||||
///
|
||||
/// # Arguments
|
||||
@@ -133,54 +188,6 @@ impl VecZnx {
|
||||
}
|
||||
}
|
||||
|
||||
fn bytes_of(n: usize, cols: usize, limbs: usize) -> usize {
|
||||
n * cols * limbs * size_of::<i64>()
|
||||
}
|
||||
|
||||
/// Returns a new struct implementing [VecZnx] with the provided data as backing array.
|
||||
///
|
||||
/// The struct will take ownership of buf[..[Self::bytes_of]]
|
||||
///
|
||||
/// User must ensure that data is properly alligned and that
|
||||
/// the limbs of data is equal to [Self::bytes_of].
|
||||
pub fn from_bytes(n: usize, cols: usize, limbs: usize, bytes: &mut [u8]) -> Self {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert!(cols > 0);
|
||||
assert!(limbs > 0);
|
||||
assert_eq!(bytes.len(), Self::bytes_of(n, cols, limbs));
|
||||
assert_alignement(bytes.as_ptr());
|
||||
}
|
||||
unsafe {
|
||||
let bytes_i64: &mut [i64] = cast_mut::<u8, i64>(bytes);
|
||||
let ptr: *mut i64 = bytes_i64.as_mut_ptr();
|
||||
Self {
|
||||
n: n,
|
||||
cols: cols,
|
||||
limbs: limbs,
|
||||
data: Vec::from_raw_parts(ptr, bytes.len(), bytes.len()),
|
||||
ptr: ptr,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_bytes_borrow(n: usize, cols: usize, limbs: usize, bytes: &mut [u8]) -> Self {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert!(cols > 0);
|
||||
assert!(limbs > 0);
|
||||
assert!(bytes.len() >= Self::bytes_of(n, cols, limbs));
|
||||
assert_alignement(bytes.as_ptr());
|
||||
}
|
||||
Self {
|
||||
n: n,
|
||||
cols: cols,
|
||||
limbs: limbs,
|
||||
data: Vec::new(),
|
||||
ptr: bytes.as_mut_ptr() as *mut i64,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn copy_from(&mut self, a: &Self) {
|
||||
copy_vec_znx_from(self, a);
|
||||
}
|
||||
@@ -394,19 +401,19 @@ pub trait VecZnxOps {
|
||||
|
||||
impl<B: Backend> VecZnxOps for Module<B> {
|
||||
fn new_vec_znx(&self, cols: usize, limbs: usize) -> VecZnx {
|
||||
VecZnx::new(self.n(), cols, limbs)
|
||||
VecZnx::new(self, cols, limbs)
|
||||
}
|
||||
|
||||
fn bytes_of_vec_znx(&self, cols: usize, limbs: usize) -> usize {
|
||||
VecZnx::bytes_of(self.n(), cols, limbs)
|
||||
VecZnx::bytes_of(self, cols, limbs)
|
||||
}
|
||||
|
||||
fn new_vec_znx_from_bytes(&self, cols: usize, limbs: usize, bytes: &mut [u8]) -> VecZnx {
|
||||
VecZnx::from_bytes(self.n(), cols, limbs, bytes)
|
||||
VecZnx::from_bytes(self, cols, limbs, bytes)
|
||||
}
|
||||
|
||||
fn new_vec_znx_from_bytes_borrow(&self, cols: usize, limbs: usize, tmp_bytes: &mut [u8]) -> VecZnx {
|
||||
VecZnx::from_bytes_borrow(self.n(), cols, limbs, tmp_bytes)
|
||||
VecZnx::from_bytes_borrow(self, cols, limbs, tmp_bytes)
|
||||
}
|
||||
|
||||
fn vec_znx_normalize_tmp_bytes(&self, cols: usize) -> usize {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::ffi::vec_znx_big::{self, vec_znx_big_t};
|
||||
use crate::{Backend, FFT64, Infos, Module, VecZnx, VecZnxDft, VecZnxLayout, alloc_aligned, assert_alignement};
|
||||
use crate::{Backend, FFT64, Module, VecZnx, VecZnxDft, ZnxBase, ZnxInfos, ZnxLayout, alloc_aligned, assert_alignement};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub struct VecZnxBig<B: Backend> {
|
||||
@@ -10,16 +10,17 @@ pub struct VecZnxBig<B: Backend> {
|
||||
pub limbs: usize,
|
||||
pub _marker: PhantomData<B>,
|
||||
}
|
||||
impl<B: Backend> ZnxBase<B> for VecZnxBig<B> {
|
||||
type Scalar = u8;
|
||||
|
||||
impl VecZnxBig<FFT64> {
|
||||
pub fn new(module: &Module<FFT64>, cols: usize, limbs: usize) -> Self {
|
||||
fn new(module: &Module<B>, cols: usize, limbs: usize) -> Self {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert!(cols > 0);
|
||||
assert!(limbs > 0);
|
||||
}
|
||||
let mut data: Vec<u8> = alloc_aligned::<u8>(module.bytes_of_vec_znx_big(cols, limbs));
|
||||
let ptr: *mut u8 = data.as_mut_ptr();
|
||||
let mut data: Vec<Self::Scalar> = alloc_aligned::<u8>(Self::bytes_of(module, cols, limbs));
|
||||
let ptr: *mut Self::Scalar = data.as_mut_ptr();
|
||||
Self {
|
||||
data: data,
|
||||
ptr: ptr,
|
||||
@@ -30,15 +31,19 @@ impl VecZnxBig<FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
fn bytes_of(module: &Module<B>, cols: usize, limbs: usize) -> usize {
|
||||
unsafe { vec_znx_big::bytes_of_vec_znx_big(module.ptr, limbs as u64) as usize * cols }
|
||||
}
|
||||
|
||||
/// Returns a new [VecZnxBig] with the provided data as backing array.
|
||||
/// 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].
|
||||
pub fn from_bytes(module: &Module<FFT64>, cols: usize, limbs: usize, bytes: &mut [u8]) -> Self {
|
||||
fn from_bytes(module: &Module<B>, cols: usize, limbs: usize, bytes: &mut [Self::Scalar]) -> Self {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert!(cols > 0);
|
||||
assert!(limbs > 0);
|
||||
assert_eq!(bytes.len(), module.bytes_of_vec_znx_big(cols, limbs));
|
||||
assert_eq!(bytes.len(), Self::bytes_of(module, cols, limbs));
|
||||
assert_alignement(bytes.as_ptr())
|
||||
};
|
||||
unsafe {
|
||||
@@ -53,12 +58,12 @@ impl VecZnxBig<FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_bytes_borrow(module: &Module<FFT64>, cols: usize, limbs: usize, bytes: &mut [u8]) -> Self {
|
||||
fn from_bytes_borrow(module: &Module<B>, cols: usize, limbs: usize, bytes: &mut [Self::Scalar]) -> Self {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert!(cols > 0);
|
||||
assert!(limbs > 0);
|
||||
assert_eq!(bytes.len(), module.bytes_of_vec_znx_big(cols, limbs));
|
||||
assert_eq!(bytes.len(), Self::bytes_of(module, cols, limbs));
|
||||
assert_alignement(bytes.as_ptr());
|
||||
}
|
||||
Self {
|
||||
@@ -70,24 +75,9 @@ impl VecZnxBig<FFT64> {
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_vec_znx_dft(&mut self) -> VecZnxDft<FFT64> {
|
||||
VecZnxDft::<FFT64> {
|
||||
data: Vec::new(),
|
||||
ptr: self.ptr,
|
||||
n: self.n,
|
||||
cols: self.cols,
|
||||
limbs: self.limbs,
|
||||
_marker: self._marker,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print(&self, n: usize) {
|
||||
(0..self.limbs()).for_each(|i| println!("{}: {:?}", i, &self.at_limb(i)[..n]));
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> Infos for VecZnxBig<B> {
|
||||
impl<B: Backend> ZnxInfos for VecZnxBig<B> {
|
||||
fn log_n(&self) -> usize {
|
||||
(usize::BITS - (self.n - 1).leading_zeros()) as _
|
||||
}
|
||||
@@ -113,7 +103,7 @@ impl<B: Backend> Infos for VecZnxBig<B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl VecZnxLayout for VecZnxBig<FFT64> {
|
||||
impl ZnxLayout for VecZnxBig<FFT64> {
|
||||
type Scalar = i64;
|
||||
|
||||
fn as_ptr(&self) -> *const Self::Scalar {
|
||||
@@ -125,6 +115,12 @@ impl VecZnxLayout for VecZnxBig<FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
impl VecZnxBig<FFT64> {
|
||||
pub fn print(&self, n: usize) {
|
||||
(0..self.limbs()).for_each(|i| println!("{}: {:?}", i, &self.at_limb(i)[..n]));
|
||||
}
|
||||
}
|
||||
|
||||
pub trait VecZnxBigOps<B: Backend> {
|
||||
/// Allocates a vector Z[X]/(X^N+1) that stores not normalized values.
|
||||
fn new_vec_znx_big(&self, cols: usize, limbs: usize) -> VecZnxBig<B>;
|
||||
@@ -220,7 +216,7 @@ impl VecZnxBigOps<FFT64> for Module<FFT64> {
|
||||
}
|
||||
|
||||
fn bytes_of_vec_znx_big(&self, cols: usize, limbs: usize) -> usize {
|
||||
unsafe { vec_znx_big::bytes_of_vec_znx_big(self.ptr, limbs as u64) as usize * cols }
|
||||
VecZnxBig::bytes_of(self, cols, limbs)
|
||||
}
|
||||
|
||||
/// [VecZnxBig] (3 cols and 4 limbs)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::ffi::vec_znx_big::vec_znx_big_t;
|
||||
use crate::ffi::vec_znx_dft;
|
||||
use crate::ffi::vec_znx_dft::{bytes_of_vec_znx_dft, vec_znx_dft_t};
|
||||
use crate::{Backend, FFT64, Infos, Module, VecZnxBig, VecZnxLayout, assert_alignement};
|
||||
use crate::{Backend, FFT64, Module, VecZnxBig, ZnxBase, ZnxInfos, ZnxLayout, assert_alignement};
|
||||
use crate::{VecZnx, alloc_aligned};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
@@ -14,15 +14,17 @@ pub struct VecZnxDft<B: Backend> {
|
||||
pub _marker: PhantomData<B>,
|
||||
}
|
||||
|
||||
impl VecZnxDft<FFT64> {
|
||||
pub fn new(module: &Module<FFT64>, cols: usize, limbs: usize) -> Self {
|
||||
impl<B: Backend> ZnxBase<B> for VecZnxDft<B> {
|
||||
type Scalar = u8;
|
||||
|
||||
fn new(module: &Module<B>, cols: usize, limbs: usize) -> Self {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert!(cols > 0);
|
||||
assert!(limbs > 0);
|
||||
}
|
||||
let mut data: Vec<u8> = alloc_aligned::<u8>(module.bytes_of_vec_znx_dft(cols, limbs));
|
||||
let ptr: *mut u8 = data.as_mut_ptr();
|
||||
let mut data: Vec<Self::Scalar> = alloc_aligned(Self::bytes_of(module, cols, limbs));
|
||||
let ptr: *mut Self::Scalar = data.as_mut_ptr();
|
||||
Self {
|
||||
data: data,
|
||||
ptr: ptr,
|
||||
@@ -33,19 +35,19 @@ impl VecZnxDft<FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
fn bytes_of(module: &Module<FFT64>, cols: usize, limbs: usize) -> usize {
|
||||
fn bytes_of(module: &Module<B>, cols: usize, limbs: usize) -> usize {
|
||||
unsafe { bytes_of_vec_znx_dft(module.ptr, limbs as u64) as usize * cols }
|
||||
}
|
||||
|
||||
/// Returns a new [VecZnxDft] with the provided data as backing array.
|
||||
/// 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].
|
||||
pub fn from_bytes(module: &Module<FFT64>, cols: usize, limbs: usize, bytes: &mut [u8]) -> Self {
|
||||
fn from_bytes(module: &Module<B>, cols: usize, limbs: usize, bytes: &mut [Self::Scalar]) -> Self {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert!(cols > 0);
|
||||
assert!(limbs > 0);
|
||||
assert_eq!(bytes.len(), module.bytes_of_vec_znx_dft(cols, limbs));
|
||||
assert_eq!(bytes.len(), Self::bytes_of(module, cols, limbs));
|
||||
assert_alignement(bytes.as_ptr())
|
||||
}
|
||||
unsafe {
|
||||
@@ -60,12 +62,12 @@ impl VecZnxDft<FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_bytes_borrow(module: &Module<FFT64>, cols: usize, limbs: usize, bytes: &mut [u8]) -> Self {
|
||||
fn from_bytes_borrow(module: &Module<B>, cols: usize, limbs: usize, bytes: &mut [Self::Scalar]) -> Self {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert!(cols > 0);
|
||||
assert!(limbs > 0);
|
||||
assert_eq!(bytes.len(), module.bytes_of_vec_znx_dft(cols, limbs));
|
||||
assert_eq!(bytes.len(), Self::bytes_of(module, cols, limbs));
|
||||
assert_alignement(bytes.as_ptr());
|
||||
}
|
||||
Self {
|
||||
@@ -77,12 +79,14 @@ impl VecZnxDft<FFT64> {
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> VecZnxDft<B> {
|
||||
/// Cast a [VecZnxDft] into a [VecZnxBig].
|
||||
/// The returned [VecZnxBig] shares the backing array
|
||||
/// with the original [VecZnxDft].
|
||||
pub fn as_vec_znx_big(&mut self) -> VecZnxBig<FFT64> {
|
||||
VecZnxBig::<FFT64> {
|
||||
pub fn as_vec_znx_big(&mut self) -> VecZnxBig<B> {
|
||||
VecZnxBig::<B> {
|
||||
data: Vec::new(),
|
||||
ptr: self.ptr,
|
||||
n: self.n,
|
||||
@@ -91,13 +95,9 @@ impl VecZnxDft<FFT64> {
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print(&self, n: usize) {
|
||||
(0..self.limbs()).for_each(|i| println!("{}: {:?}", i, &self.at_limb(i)[..n]));
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> Infos for VecZnxDft<B> {
|
||||
impl<B: Backend> ZnxInfos for VecZnxDft<B> {
|
||||
fn n(&self) -> usize {
|
||||
self.n
|
||||
}
|
||||
@@ -123,7 +123,7 @@ impl<B: Backend> Infos for VecZnxDft<B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl VecZnxLayout for VecZnxDft<FFT64> {
|
||||
impl ZnxLayout for VecZnxDft<FFT64> {
|
||||
type Scalar = f64;
|
||||
|
||||
fn as_ptr(&self) -> *const Self::Scalar {
|
||||
@@ -135,6 +135,12 @@ impl VecZnxLayout for VecZnxDft<FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
impl VecZnxDft<FFT64> {
|
||||
pub fn print(&self, n: usize) {
|
||||
(0..self.limbs()).for_each(|i| println!("{}: {:?}", i, &self.at_limb(i)[..n]));
|
||||
}
|
||||
}
|
||||
|
||||
pub trait VecZnxDftOps<B: Backend> {
|
||||
/// Allocates a vector Z[X]/(X^N+1) that stores normalized in the DFT space.
|
||||
fn new_vec_znx_dft(&self, cols: usize, limbs: usize) -> VecZnxDft<B>;
|
||||
@@ -314,7 +320,7 @@ impl VecZnxDftOps<FFT64> for Module<FFT64> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{FFT64, Module, Sampling, VecZnx, VecZnxDft, VecZnxDftOps, VecZnxLayout, VecZnxOps, alloc_aligned};
|
||||
use crate::{FFT64, Module, Sampling, VecZnx, VecZnxDft, VecZnxDftOps, VecZnxOps, ZnxLayout, alloc_aligned};
|
||||
use itertools::izip;
|
||||
use sampling::source::Source;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user