mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 13:16:44 +01:00
prototype trait for Elem<T> + new ciphertext for VmPPmat
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use crate::ffi::vec_znx_big;
|
||||
use crate::ffi::vec_znx_dft;
|
||||
use crate::ffi::vec_znx_dft::bytes_of_vec_znx_dft;
|
||||
use crate::{Infos, Module, VecZnx, VecZnxApi, VecZnxBig};
|
||||
use crate::{Infos, Module, VecZnxApi, VecZnxBig};
|
||||
|
||||
pub struct VecZnxDft(pub *mut vec_znx_dft::vec_znx_dft_t, pub usize);
|
||||
|
||||
@@ -9,8 +9,8 @@ impl VecZnxDft {
|
||||
/// 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(limbs: usize, data: &mut [u8]) -> VecZnxDft {
|
||||
VecZnxDft(data.as_mut_ptr() as *mut vec_znx_dft::vec_znx_dft_t, limbs)
|
||||
pub fn from_bytes(cols: usize, data: &mut [u8]) -> VecZnxDft {
|
||||
VecZnxDft(data.as_mut_ptr() as *mut vec_znx_dft::vec_znx_dft_t, cols)
|
||||
}
|
||||
|
||||
/// Cast a [VecZnxDft] into a [VecZnxBig].
|
||||
@@ -19,36 +19,36 @@ impl VecZnxDft {
|
||||
pub fn as_vec_znx_big(&mut self) -> VecZnxBig {
|
||||
VecZnxBig(self.0 as *mut vec_znx_big::vec_znx_bigcoeff_t, self.1)
|
||||
}
|
||||
pub fn limbs(&self) -> usize {
|
||||
pub fn cols(&self) -> usize {
|
||||
self.1
|
||||
}
|
||||
}
|
||||
|
||||
pub trait VecZnxDftOps {
|
||||
/// Allocates a vector Z[X]/(X^N+1) that stores normalized in the DFT space.
|
||||
fn new_vec_znx_dft(&self, limbs: usize) -> VecZnxDft;
|
||||
fn new_vec_znx_dft(&self, cols: usize) -> VecZnxDft;
|
||||
|
||||
/// Returns a new [VecZnxDft] with the provided bytes array as backing array.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `limbs`: the number of limbs of the [VecZnxDft].
|
||||
/// * `cols`: the number of cols of the [VecZnxDft].
|
||||
/// * `bytes`: a byte array of size at least [Module::bytes_of_vec_znx_dft].
|
||||
///
|
||||
/// # Panics
|
||||
/// If `bytes.len()` < [Module::bytes_of_vec_znx_dft].
|
||||
fn new_vec_znx_dft_from_bytes(&self, limbs: usize, bytes: &mut [u8]) -> VecZnxDft;
|
||||
fn new_vec_znx_dft_from_bytes(&self, cols: usize, bytes: &mut [u8]) -> VecZnxDft;
|
||||
|
||||
/// Returns a new [VecZnxDft] with the provided bytes array as backing array.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `limbs`: the number of limbs of the [VecZnxDft].
|
||||
/// * `cols`: the number of cols of the [VecZnxDft].
|
||||
/// * `bytes`: a byte array of size at least [Module::bytes_of_vec_znx_dft].
|
||||
///
|
||||
/// # Panics
|
||||
/// If `bytes.len()` < [Module::bytes_of_vec_znx_dft].
|
||||
fn bytes_of_vec_znx_dft(&self, limbs: usize) -> usize;
|
||||
fn bytes_of_vec_znx_dft(&self, cols: usize) -> usize;
|
||||
|
||||
/// Returns the minimum number of bytes necessary to allocate
|
||||
/// a new [VecZnxDft] through [VecZnxDft::from_bytes].
|
||||
@@ -69,33 +69,33 @@ pub trait VecZnxDftOps {
|
||||
}
|
||||
|
||||
impl VecZnxDftOps for Module {
|
||||
fn new_vec_znx_dft(&self, limbs: usize) -> VecZnxDft {
|
||||
unsafe { VecZnxDft(vec_znx_dft::new_vec_znx_dft(self.0, limbs as u64), limbs) }
|
||||
fn new_vec_znx_dft(&self, cols: usize) -> VecZnxDft {
|
||||
unsafe { VecZnxDft(vec_znx_dft::new_vec_znx_dft(self.0, cols as u64), cols) }
|
||||
}
|
||||
|
||||
fn new_vec_znx_dft_from_bytes(&self, limbs: usize, bytes: &mut [u8]) -> VecZnxDft {
|
||||
fn new_vec_znx_dft_from_bytes(&self, cols: usize, bytes: &mut [u8]) -> VecZnxDft {
|
||||
assert!(
|
||||
bytes.len() >= <Module as VecZnxDftOps>::bytes_of_vec_znx_dft(self, limbs),
|
||||
bytes.len() >= <Module as VecZnxDftOps>::bytes_of_vec_znx_dft(self, cols),
|
||||
"invalid bytes: bytes.len()={} < bytes_of_vec_znx_dft={}",
|
||||
bytes.len(),
|
||||
<Module as VecZnxDftOps>::bytes_of_vec_znx_dft(self, limbs)
|
||||
<Module as VecZnxDftOps>::bytes_of_vec_znx_dft(self, cols)
|
||||
);
|
||||
VecZnxDft::from_bytes(limbs, bytes)
|
||||
VecZnxDft::from_bytes(cols, bytes)
|
||||
}
|
||||
|
||||
fn bytes_of_vec_znx_dft(&self, limbs: usize) -> usize {
|
||||
unsafe { bytes_of_vec_znx_dft(self.0, limbs as u64) as usize }
|
||||
fn bytes_of_vec_znx_dft(&self, cols: usize) -> usize {
|
||||
unsafe { bytes_of_vec_znx_dft(self.0, cols as u64) as usize }
|
||||
}
|
||||
|
||||
fn vec_znx_idft_tmp_a(&self, b: &mut VecZnxBig, a: &mut VecZnxDft, a_limbs: usize) {
|
||||
assert!(
|
||||
b.limbs() >= a_limbs,
|
||||
"invalid c_vector: b_vector.limbs()={} < a_limbs={}",
|
||||
b.limbs(),
|
||||
b.cols() >= a_limbs,
|
||||
"invalid c_vector: b_vector.cols()={} < a_limbs={}",
|
||||
b.cols(),
|
||||
a_limbs
|
||||
);
|
||||
unsafe {
|
||||
vec_znx_dft::vec_znx_idft_tmp_a(self.0, b.0, b.limbs() as u64, a.0, a_limbs as u64)
|
||||
vec_znx_dft::vec_znx_idft_tmp_a(self.0, b.0, b.cols() as u64, a.0, a_limbs as u64)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,21 +106,21 @@ impl VecZnxDftOps for Module {
|
||||
/// b <- DFT(a)
|
||||
///
|
||||
/// # Panics
|
||||
/// If b.limbs < a_limbs
|
||||
fn vec_znx_dft<T: VecZnxApi + Infos>(&self, b: &mut VecZnxDft, a: &T, a_limbs: usize) {
|
||||
/// If b.cols < a_cols
|
||||
fn vec_znx_dft<T: VecZnxApi + Infos>(&self, b: &mut VecZnxDft, a: &T, a_cols: usize) {
|
||||
assert!(
|
||||
b.limbs() >= a_limbs,
|
||||
"invalid a_limbs: b.limbs()={} < a_limbs={}",
|
||||
b.limbs(),
|
||||
a_limbs
|
||||
b.cols() >= a_cols,
|
||||
"invalid a_cols: b.cols()={} < a_cols={}",
|
||||
b.cols(),
|
||||
a_cols
|
||||
);
|
||||
unsafe {
|
||||
vec_znx_dft::vec_znx_dft(
|
||||
self.0,
|
||||
b.0,
|
||||
b.limbs() as u64,
|
||||
b.cols() as u64,
|
||||
a.as_ptr(),
|
||||
a_limbs as u64,
|
||||
a_cols as u64,
|
||||
a.n() as u64,
|
||||
)
|
||||
}
|
||||
@@ -131,20 +131,20 @@ impl VecZnxDftOps for Module {
|
||||
&self,
|
||||
b: &mut VecZnxBig,
|
||||
a: &mut VecZnxDft,
|
||||
a_limbs: usize,
|
||||
a_cols: usize,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
assert!(
|
||||
b.limbs() >= a_limbs,
|
||||
"invalid c_vector: b.limbs()={} < a_limbs={}",
|
||||
b.limbs(),
|
||||
a_limbs
|
||||
b.cols() >= a_cols,
|
||||
"invalid c_vector: b.cols()={} < a_cols={}",
|
||||
b.cols(),
|
||||
a_cols
|
||||
);
|
||||
assert!(
|
||||
a.limbs() >= a_limbs,
|
||||
"invalid c_vector: a.limbs()={} < a_limbs={}",
|
||||
a.limbs(),
|
||||
a_limbs
|
||||
a.cols() >= a_cols,
|
||||
"invalid c_vector: a.cols()={} < a_cols={}",
|
||||
a.cols(),
|
||||
a_cols
|
||||
);
|
||||
assert!(
|
||||
tmp_bytes.len() <= <Module as VecZnxDftOps>::vec_znx_idft_tmp_bytes(self),
|
||||
@@ -156,9 +156,9 @@ impl VecZnxDftOps for Module {
|
||||
vec_znx_dft::vec_znx_idft(
|
||||
self.0,
|
||||
b.0,
|
||||
a.limbs() as u64,
|
||||
a.cols() as u64,
|
||||
a.0,
|
||||
a_limbs as u64,
|
||||
a_cols as u64,
|
||||
tmp_bytes.as_mut_ptr(),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user