Added VecZnxBorrow

This commit is contained in:
Jean-Philippe Bossuat
2025-02-14 18:26:54 +01:00
parent 68e61dc0e3
commit 67d8fd31b7
12 changed files with 605 additions and 595 deletions

View File

@@ -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::{Module, VecZnx, VecZnxBig};
use crate::{Infos, Module, VecZnx, VecZnxApi, VecZnxBig};
pub struct VecZnxDft(pub *mut vec_znx_dft::vec_znx_dft_t, pub usize);
@@ -24,11 +24,9 @@ impl VecZnxDft {
}
}
impl Module {
// Allocates a vector Z[X]/(X^N+1) that stores normalized in the DFT space.
pub fn new_vec_znx_dft(&self, limbs: usize) -> VecZnxDft {
unsafe { VecZnxDft(vec_znx_dft::new_vec_znx_dft(self.0, limbs as u64), limbs) }
}
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;
/// Returns a new [VecZnxDft] with the provided bytes array as backing array.
///
@@ -39,24 +37,57 @@ impl Module {
///
/// # Panics
/// If `bytes.len()` < [Module::bytes_of_vec_znx_dft].
pub fn new_vec_znx_dft_from_bytes(&self, limbs: usize, bytes: &mut [u8]) -> VecZnxDft {
fn new_vec_znx_dft_from_bytes(&self, limbs: 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].
/// * `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;
/// Returns the minimum number of bytes necessary to allocate
/// a new [VecZnxDft] through [VecZnxDft::from_bytes].
fn vec_znx_idft_tmp_bytes(&self) -> usize;
/// b <- IDFT(a), uses a as scratch space.
fn vec_znx_idft_tmp_a(&self, b: &mut VecZnxBig, a: &mut VecZnxDft, a_limbs: usize);
fn vec_znx_idft(
&self,
b: &mut VecZnxBig,
a: &mut VecZnxDft,
a_limbs: usize,
tmp_bytes: &mut [u8],
);
fn vec_znx_dft<T: VecZnxApi + Infos>(&self, b: &mut VecZnxDft, a: &T, a_limbs: usize);
}
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_from_bytes(&self, limbs: usize, bytes: &mut [u8]) -> VecZnxDft {
assert!(
bytes.len() >= self.bytes_of_vec_znx_dft(limbs),
bytes.len() >= <Module as VecZnxDftOps>::bytes_of_vec_znx_dft(self, limbs),
"invalid bytes: bytes.len()={} < bytes_of_vec_znx_dft={}",
bytes.len(),
self.bytes_of_vec_znx_dft(limbs)
<Module as VecZnxDftOps>::bytes_of_vec_znx_dft(self, limbs)
);
VecZnxDft::from_bytes(limbs, bytes)
}
/// Returns the minimum number of bytes necessary to allocate
/// a new [VecZnxDft] through [VecZnxDft::from_bytes].
pub fn bytes_of_vec_znx_dft(&self, limbs: usize) -> usize {
fn bytes_of_vec_znx_dft(&self, limbs: usize) -> usize {
unsafe { bytes_of_vec_znx_dft(self.0, limbs as u64) as usize }
}
// b <- IDFT(a), uses a as scratch space.
pub fn vec_znx_idft_tmp_a(&self, b: &mut VecZnxBig, a: &mut VecZnxDft, a_limbs: 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={}",
@@ -68,8 +99,7 @@ impl Module {
}
}
// Returns the size of the scratch space for [vec_znx_idft].
pub fn vec_znx_idft_tmp_bytes(&self) -> usize {
fn vec_znx_idft_tmp_bytes(&self) -> usize {
unsafe { vec_znx_dft::vec_znx_idft_tmp_bytes(self.0) as usize }
}
@@ -77,7 +107,7 @@ impl Module {
///
/// # Panics
/// If b.limbs < a_limbs
pub fn vec_znx_dft(&self, b: &mut VecZnxDft, a: &VecZnx, a_limbs: usize) {
fn vec_znx_dft<T: VecZnxApi + Infos>(&self, b: &mut VecZnxDft, a: &T, a_limbs: usize) {
assert!(
b.limbs() >= a_limbs,
"invalid a_limbs: b.limbs()={} < a_limbs={}",
@@ -91,13 +121,13 @@ impl Module {
b.limbs() as u64,
a.as_ptr(),
a_limbs as u64,
a.n as u64,
a.n() as u64,
)
}
}
// b <- IDFT(a), scratch space size obtained with [vec_znx_idft_tmp_bytes].
pub fn vec_znx_idft(
fn vec_znx_idft(
&self,
b: &mut VecZnxBig,
a: &mut VecZnxDft,
@@ -117,10 +147,10 @@ impl Module {
a_limbs
);
assert!(
tmp_bytes.len() <= self.vec_znx_idft_tmp_bytes(),
tmp_bytes.len() <= <Module as VecZnxDftOps>::vec_znx_idft_tmp_bytes(self),
"invalid tmp_bytes: tmp_bytes.len()={} < self.vec_znx_idft_tmp_bytes()={}",
tmp_bytes.len(),
self.vec_znx_idft_tmp_bytes()
<Module as VecZnxDftOps>::vec_znx_idft_tmp_bytes(self)
);
unsafe {
vec_znx_dft::vec_znx_idft(