limbs -> cols

This commit is contained in:
Jean-Philippe Bossuat
2025-02-19 10:03:31 +01:00
parent 3937a43b08
commit 014bf0c2d1
3 changed files with 80 additions and 86 deletions

View File

@@ -54,7 +54,6 @@ pub trait Encoding {
impl Encoding for VecZnx { impl Encoding for VecZnx {
fn encode_vec_i64(&mut self, log_base2k: usize, log_k: usize, data: &[i64], log_max: usize) { fn encode_vec_i64(&mut self, log_base2k: usize, log_k: usize, data: &[i64], log_max: usize) {
let cols: usize = (log_k + log_base2k - 1) / log_base2k; let cols: usize = (log_k + log_base2k - 1) / log_base2k;
assert!(cols <= self.cols(), "invalid argument log_k: (log_k + self.log_base2k - 1)/self.log_base2k={} > self.cols()={}", cols, self.cols()); assert!(cols <= self.cols(), "invalid argument log_k: (log_k + self.log_base2k - 1)/self.log_base2k={} > self.cols()={}", cols, self.cols());

View File

@@ -8,7 +8,7 @@ pub trait Infos {
fn log_n(&self) -> usize; fn log_n(&self) -> usize;
/// Returns the number of columns of the receiver. /// Returns the number of columns of the receiver.
/// This method is equivalent to [Infos::limbs]. /// This method is equivalent to [Infos::cols].
fn cols(&self) -> usize; fn cols(&self) -> usize;
/// Returns the number of rows of the receiver. /// Returns the number of rows of the receiver.
@@ -26,12 +26,12 @@ impl Infos for VecZnx {
self.n self.n
} }
/// Returns the number of limbs of the [VecZnx]. /// Returns the number of cols of the [VecZnx].
fn cols(&self) -> usize { fn cols(&self) -> usize {
self.data.len() / self.n self.data.len() / self.n
} }
/// Returns the number of limbs of the [VecZnx]. /// Returns the number of rows of the [VecZnx].
fn rows(&self) -> usize { fn rows(&self) -> usize {
1 1
} }
@@ -48,12 +48,12 @@ impl Infos for VecZnxBorrow {
self.n self.n
} }
/// Returns the number of limbs of the [VecZnx]. /// Returns the number of cols of the [VecZnx].
fn cols(&self) -> usize { fn cols(&self) -> usize {
self.limbs self.cols
} }
/// Returns the number of limbs of the [VecZnx]. /// Returns the number of rows of the [VecZnx].
fn rows(&self) -> usize { fn rows(&self) -> usize {
1 1
} }
@@ -75,9 +75,9 @@ impl Infos for VmpPMat {
} }
/// Returns the number of cols of the [VmpPMat]. /// Returns the number of cols of the [VmpPMat].
/// The number of cols refers to the number of limbs /// The number of cols refers to the number of cols
/// of each [VecZnxDft]. /// of each [VecZnxDft].
/// This method is equivalent to [Self::limbs]. /// This method is equivalent to [Self::cols].
fn cols(&self) -> usize { fn cols(&self) -> usize {
self.cols self.cols
} }

View File

@@ -10,11 +10,11 @@ use std::cmp::min;
pub trait VecZnxApi { pub trait VecZnxApi {
type Owned: VecZnxApi + Infos; type Owned: VecZnxApi + Infos;
fn from_bytes(n: usize, limbs: usize, bytes: &mut [u8]) -> Self::Owned; fn from_bytes(n: usize, cols: usize, bytes: &mut [u8]) -> Self::Owned;
/// Returns the minimum size of the [u8] array required to assign a /// Returns the minimum size of the [u8] array required to assign a
/// new backend array. /// new backend array.
fn bytes_of(n: usize, limbs: usize) -> usize; fn bytes_of(n: usize, cols: usize) -> usize;
/// Returns the backing array. /// Returns the backing array.
fn raw(&self) -> &[i64]; fn raw(&self) -> &[i64];
@@ -28,16 +28,16 @@ pub trait VecZnxApi {
/// Returns a mutable pointer to the backing array. /// Returns a mutable pointer to the backing array.
fn as_mut_ptr(&mut self) -> *mut i64; fn as_mut_ptr(&mut self) -> *mut i64;
/// Returns a non-mutable reference to the i-th limb. /// Returns a non-mutable reference to the i-th cols.
fn at(&self, i: usize) -> &[i64]; fn at(&self, i: usize) -> &[i64];
/// Returns a mutable reference to the i-th limb . /// Returns a mutable reference to the i-th cols .
fn at_mut(&mut self, i: usize) -> &mut [i64]; fn at_mut(&mut self, i: usize) -> &mut [i64];
/// Returns a non-mutable pointer to the i-th limb. /// Returns a non-mutable pointer to the i-th cols.
fn at_ptr(&self, i: usize) -> *const i64; fn at_ptr(&self, i: usize) -> *const i64;
/// Returns a mutable pointer to the i-th limb. /// Returns a mutable pointer to the i-th cols.
fn at_mut_ptr(&mut self, i: usize) -> *mut i64; fn at_mut_ptr(&mut self, i: usize) -> *mut i64;
/// Zeroes the backing array. /// Zeroes the backing array.
@@ -50,11 +50,11 @@ pub trait VecZnxApi {
/// ///
/// * `log_base2k`: the base two logarithm of the coefficients decomposition. /// * `log_base2k`: the base two logarithm of the coefficients decomposition.
/// * `k`: the shift amount. /// * `k`: the shift amount.
/// * `carry`: scratch space of size at least equal to self.n() * self.limbs() << 3. /// * `carry`: scratch space of size at least equal to self.n() * self.cols() << 3.
/// ///
/// # Panics /// # Panics
/// ///
/// The method will panic if carry.len() < self.n() * self.limbs() << 3. /// The method will panic if carry.len() < self.n() * self.cols() << 3.
fn rsh(&mut self, log_base2k: usize, k: usize, carry: &mut [u8]); fn rsh(&mut self, log_base2k: usize, k: usize, carry: &mut [u8]);
/// If self.n() > a.n(): Extracts X^{i*self.n()/a.n()} -> X^{i}. /// If self.n() > a.n(): Extracts X^{i*self.n()/a.n()} -> X^{i}.
@@ -67,16 +67,16 @@ pub trait VecZnxApi {
where where
Self: AsRef<T>; Self: AsRef<T>;
fn print(&self, limbs: usize, n: usize); fn print(&self, cols: usize, n: usize);
} }
pub fn bytes_of_vec_znx(n: usize, limbs: usize) -> usize { pub fn bytes_of_vec_znx(n: usize, cols: usize) -> usize {
n * limbs * 8 n * cols * 8
} }
pub struct VecZnxBorrow { pub struct VecZnxBorrow {
pub n: usize, pub n: usize,
pub limbs: usize, pub cols: usize,
pub data: *mut i64, pub data: *mut i64,
} }
@@ -89,25 +89,25 @@ impl VecZnxApi for VecZnxBorrow {
/// ///
/// 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 [VecZnx::bytes_of]. /// the size of data is at least equal to [VecZnx::bytes_of].
fn from_bytes(n: usize, limbs: usize, bytes: &mut [u8]) -> Self::Owned { fn from_bytes(n: usize, cols: usize, bytes: &mut [u8]) -> Self::Owned {
let size = Self::bytes_of(n, limbs); let size = Self::bytes_of(n, cols);
assert!( assert!(
bytes.len() >= size, bytes.len() >= size,
"invalid buffer: buf.len()={} < self.buffer_size(n={}, limbs={})={}", "invalid buffer: buf.len()={} < self.buffer_size(n={}, cols={})={}",
bytes.len(), bytes.len(),
n, n,
limbs, cols,
size size
); );
VecZnxBorrow { VecZnxBorrow {
n: n, n: n,
limbs: limbs, cols: cols,
data: cast_mut(&mut bytes[..size]).as_mut_ptr(), data: cast_mut(&mut bytes[..size]).as_mut_ptr(),
} }
} }
fn bytes_of(n: usize, limbs: usize) -> usize { fn bytes_of(n: usize, cols: usize) -> usize {
bytes_of_vec_znx(n, limbs) bytes_of_vec_znx(n, cols)
} }
fn as_ptr(&self) -> *const i64 { fn as_ptr(&self) -> *const i64 {
@@ -119,11 +119,11 @@ impl VecZnxApi for VecZnxBorrow {
} }
fn raw(&self) -> &[i64] { fn raw(&self) -> &[i64] {
unsafe { std::slice::from_raw_parts(self.data, self.n * self.limbs) } unsafe { std::slice::from_raw_parts(self.data, self.n * self.cols) }
} }
fn raw_mut(&mut self) -> &mut [i64] { fn raw_mut(&mut self) -> &mut [i64] {
unsafe { std::slice::from_raw_parts_mut(self.data, self.n * self.limbs) } unsafe { std::slice::from_raw_parts_mut(self.data, self.n * self.cols) }
} }
fn at(&self, i: usize) -> &[i64] { fn at(&self, i: usize) -> &[i64] {
@@ -146,7 +146,7 @@ impl VecZnxApi for VecZnxBorrow {
fn zero(&mut self) { fn zero(&mut self) {
unsafe { unsafe {
znx_zero_i64_ref((self.n * self.limbs) as u64, self.data); znx_zero_i64_ref((self.n * self.cols) as u64, self.data);
} }
} }
@@ -165,8 +165,8 @@ impl VecZnxApi for VecZnxBorrow {
switch_degree(a, self.as_ref()); switch_degree(a, self.as_ref());
} }
fn print(&self, limbs: usize, n: usize) { fn print(&self, cols: usize, n: usize) {
(0..limbs).for_each(|i| println!("{}: {:?}", i, &self.at(i)[..n])) (0..cols).for_each(|i| println!("{}: {:?}", i, &self.at(i)[..n]))
} }
} }
@@ -179,14 +179,14 @@ impl VecZnxApi for VecZnx {
/// ///
/// 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 [VecZnx::bytes_of]. /// the size of data is at least equal to [VecZnx::bytes_of].
fn from_bytes(n: usize, limbs: usize, buf: &mut [u8]) -> Self::Owned { fn from_bytes(n: usize, cols: usize, buf: &mut [u8]) -> Self::Owned {
let size = Self::bytes_of(n, limbs); let size = Self::bytes_of(n, cols);
assert!( assert!(
buf.len() >= size, buf.len() >= size,
"invalid buffer: buf.len()={} < self.buffer_size(n={}, limbs={})={}", "invalid buffer: buf.len()={} < self.buffer_size(n={}, cols={})={}",
buf.len(), buf.len(),
n, n,
limbs, cols,
size size
); );
@@ -196,8 +196,8 @@ impl VecZnxApi for VecZnx {
} }
} }
fn bytes_of(n: usize, limbs: usize) -> usize { fn bytes_of(n: usize, cols: usize) -> usize {
bytes_of_vec_znx(n, limbs) bytes_of_vec_znx(n, cols)
} }
fn raw(&self) -> &[i64] { fn raw(&self) -> &[i64] {
@@ -253,8 +253,8 @@ impl VecZnxApi for VecZnx {
switch_degree(a, self.as_ref()) switch_degree(a, self.as_ref())
} }
fn print(&self, limbs: usize, n: usize) { fn print(&self, cols: usize, n: usize) {
(0..limbs).for_each(|i| println!("{}: {:?}", i, &self.at(i)[..n])) (0..cols).for_each(|i| println!("{}: {:?}", i, &self.at(i)[..n]))
} }
} }
@@ -265,16 +265,16 @@ impl VecZnxApi for VecZnx {
pub struct VecZnx { pub struct VecZnx {
/// Polynomial degree. /// Polynomial degree.
pub n: usize, pub n: usize,
/// Polynomial coefficients, as a contiguous array. Each limb is equally spaced by n. /// Polynomial coefficients, as a contiguous array. Each col is equally spaced by n.
pub data: Vec<i64>, pub data: Vec<i64>,
} }
impl VecZnx { impl VecZnx {
/// Allocates a new [VecZnx] composed of #limbs polynomials of Z\[X\]. /// Allocates a new [VecZnx] composed of #cols polynomials of Z\[X\].
pub fn new(n: usize, limbs: usize) -> Self { pub fn new(n: usize, cols: usize) -> Self {
Self { Self {
n: n, n: n,
data: alloc_aligned::<i64>(n * limbs, 64), data: alloc_aligned::<i64>(n * cols, 64),
} }
} }
@@ -321,9 +321,9 @@ pub fn switch_degree<T: VecZnxApi + Infos>(b: &mut T, a: &T) {
b.zero(); b.zero();
} }
let limbs = min(a.cols(), b.cols()); let cols = min(a.cols(), b.cols());
(0..limbs).for_each(|i| { (0..cols).for_each(|i| {
izip!( izip!(
a.at(i).iter().step_by(gap_in), a.at(i).iter().step_by(gap_in),
b.at_mut(i).iter_mut().step_by(gap_out) b.at_mut(i).iter_mut().step_by(gap_out)
@@ -369,12 +369,12 @@ pub fn rsh<T: VecZnxApi + Infos>(log_base2k: usize, a: &mut T, k: usize, carry:
n n
); );
let limbs: usize = a.cols(); let cols: usize = a.cols();
let limbs_steps: usize = k / log_base2k; let cols_steps: usize = k / log_base2k;
a.raw_mut().rotate_right(n * limbs_steps); a.raw_mut().rotate_right(n * cols_steps);
unsafe { unsafe {
znx::znx_zero_i64_ref((n * limbs_steps) as u64, a.as_mut_ptr()); znx::znx_zero_i64_ref((n * cols_steps) as u64, a.as_mut_ptr());
} }
let k_rem = k % log_base2k; let k_rem = k % log_base2k;
@@ -389,7 +389,7 @@ pub fn rsh<T: VecZnxApi + Infos>(log_base2k: usize, a: &mut T, k: usize, carry:
let mask: i64 = (1 << k_rem) - 1; let mask: i64 = (1 << k_rem) - 1;
let log_base2k: usize = log_base2k; let log_base2k: usize = log_base2k;
(limbs_steps..limbs).for_each(|i| { (cols_steps..cols).for_each(|i| {
izip!(carry_i64.iter_mut(), a.at_mut(i).iter_mut()).for_each(|(ci, xi)| { izip!(carry_i64.iter_mut(), a.at_mut(i).iter_mut()).for_each(|(ci, xi)| {
*xi += *ci << log_base2k; *xi += *ci << log_base2k;
*ci = *xi & mask; *ci = *xi & mask;
@@ -404,12 +404,12 @@ pub trait VecZnxOps {
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `limbs`: the number of limbs. /// * `cols`: the number of cols.
fn new_vec_znx(&self, limbs: usize) -> VecZnx; fn new_vec_znx(&self, cols: usize) -> VecZnx;
/// Returns the minimum number of bytes necessary to allocate /// Returns the minimum number of bytes necessary to allocate
/// a new [VecZnx] through [VecZnx::from_bytes]. /// a new [VecZnx] through [VecZnx::from_bytes].
fn bytes_of_vec_znx(&self, limbs: usize) -> usize; fn bytes_of_vec_znx(&self, cols: usize) -> usize;
/// c <- a + b. /// c <- a + b.
fn vec_znx_add<T: VecZnxApi + Infos>(&self, c: &mut T, a: &T, b: &T); fn vec_znx_add<T: VecZnxApi + Infos>(&self, c: &mut T, a: &T, b: &T);
@@ -436,10 +436,10 @@ pub trait VecZnxOps {
fn vec_znx_rotate_inplace<T: VecZnxApi + Infos>(&self, k: i64, a: &mut T); fn vec_znx_rotate_inplace<T: VecZnxApi + Infos>(&self, k: i64, a: &mut T);
/// b <- phi_k(a) where phi_k: X^i -> X^{i*k} (mod (X^{n} + 1)) /// b <- phi_k(a) where phi_k: X^i -> X^{i*k} (mod (X^{n} + 1))
fn vec_znx_automorphism<T: VecZnxApi + Infos>(&self, k: i64, b: &mut T, a: &T, a_limbs: usize); fn vec_znx_automorphism<T: VecZnxApi + Infos>(&self, k: i64, b: &mut T, a: &T, a_cols: usize);
/// a <- phi_k(a) where phi_k: X^i -> X^{i*k} (mod (X^{n} + 1)) /// a <- phi_k(a) where phi_k: X^i -> X^{i*k} (mod (X^{n} + 1))
fn vec_znx_automorphism_inplace<T: VecZnxApi + Infos>(&self, k: i64, a: &mut T, a_limbs: usize); fn vec_znx_automorphism_inplace<T: VecZnxApi + Infos>(&self, k: i64, a: &mut T, a_cols: usize);
/// Splits b into subrings and copies them them into a. /// Splits b into subrings and copies them them into a.
/// ///
@@ -459,12 +459,12 @@ pub trait VecZnxOps {
} }
impl VecZnxOps for Module { impl VecZnxOps for Module {
fn new_vec_znx(&self, limbs: usize) -> VecZnx { fn new_vec_znx(&self, cols: usize) -> VecZnx {
VecZnx::new(self.n(), limbs) VecZnx::new(self.n(), cols)
} }
fn bytes_of_vec_znx(&self, limbs: usize) -> usize { fn bytes_of_vec_znx(&self, cols: usize) -> usize {
self.n() * limbs * 8 self.n() * cols * 8
} }
// c <- a + b // c <- a + b
@@ -597,18 +597,18 @@ impl VecZnxOps for Module {
} }
} }
/// Maps X^i to X^{ik} mod X^{n}+1. The mapping is applied independently on each limbs. /// Maps X^i to X^{ik} mod X^{n}+1. The mapping is applied independently on each cols.
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `a`: input. /// * `a`: input.
/// * `b`: output. /// * `b`: output.
/// * `k`: the power to which to map each coefficients. /// * `k`: the power to which to map each coefficients.
/// * `limbs_a`: the number of limbs_a on which to apply the mapping. /// * `a_cols`: the number of a_cols on which to apply the mapping.
/// ///
/// # Panics /// # Panics
/// ///
/// The method will panic if the argument `limbs_a` is greater than `a.limbs()`. /// The method will panic if the argument `a` is greater than `a.cols()`.
/// ///
/// # Example /// # Example
/// ``` /// ```
@@ -621,23 +621,23 @@ impl VecZnxOps for Module {
/// let mut b: VecZnx = module.new_vec_znx(2); /// let mut b: VecZnx = module.new_vec_znx(2);
/// let mut c: VecZnx = module.new_vec_znx(2); /// let mut c: VecZnx = module.new_vec_znx(2);
/// ///
/// (0..a.limbs()).for_each(|i|{ /// (0..a.cols()).for_each(|i|{
/// a.at_mut(i).iter_mut().enumerate().for_each(|(i, x)|{ /// a.at_mut(i).iter_mut().enumerate().for_each(|(i, x)|{
/// *x = i as i64 /// *x = i as i64
/// }) /// })
/// }); /// });
/// ///
/// module.vec_znx_automorphism(-1, &mut b, &a, 1); // X^i -> X^(-i) /// module.vec_znx_automorphism(-1, &mut b, &a, 1); // X^i -> X^(-i)
/// let limb = c.at_mut(0); /// let col = c.at_mut(0);
/// (1..limb.len()).for_each(|i|{ /// (1..col.len()).for_each(|i|{
/// limb[n-i] = -(i as i64) /// col[n-i] = -(i as i64)
/// }); /// });
/// izip!(b.data.iter(), c.data.iter()).for_each(|(a, b)| assert_eq!(a, b, "{} != {}", a, b)); /// izip!(b.data.iter(), c.data.iter()).for_each(|(a, b)| assert_eq!(a, b, "{} != {}", a, b));
/// ``` /// ```
fn vec_znx_automorphism<T: VecZnxApi + Infos>(&self, k: i64, b: &mut T, a: &T, limbs_a: usize) { fn vec_znx_automorphism<T: VecZnxApi + Infos>(&self, k: i64, b: &mut T, a: &T, a_cols: usize) {
assert_eq!(a.n(), self.n()); assert_eq!(a.n(), self.n());
assert_eq!(b.n(), self.n()); assert_eq!(b.n(), self.n());
assert!(a.cols() >= limbs_a); assert!(a.cols() >= a_cols);
unsafe { unsafe {
vec_znx::vec_znx_automorphism( vec_znx::vec_znx_automorphism(
self.0, self.0,
@@ -646,23 +646,23 @@ impl VecZnxOps for Module {
b.cols() as u64, b.cols() as u64,
b.n() as u64, b.n() as u64,
a.as_ptr(), a.as_ptr(),
limbs_a as u64, a_cols as u64,
a.n() as u64, a.n() as u64,
); );
} }
} }
/// Maps X^i to X^{ik} mod X^{n}+1. The mapping is applied independently on each limbs. /// Maps X^i to X^{ik} mod X^{n}+1. The mapping is applied independently on each cols.
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `a`: input and output. /// * `a`: input and output.
/// * `k`: the power to which to map each coefficients. /// * `k`: the power to which to map each coefficients.
/// * `limbs_a`: the number of limbs on which to apply the mapping. /// * `a_cols`: the number of cols on which to apply the mapping.
/// ///
/// # Panics /// # Panics
/// ///
/// The method will panic if the argument `limbs` is greater than `self.limbs()`. /// The method will panic if the argument `cols` is greater than `self.cols()`.
/// ///
/// # Example /// # Example
/// ``` /// ```
@@ -674,27 +674,22 @@ impl VecZnxOps for Module {
/// let mut a: VecZnx = VecZnx::new(n, 2); /// let mut a: VecZnx = VecZnx::new(n, 2);
/// let mut b: VecZnx = VecZnx::new(n, 2); /// let mut b: VecZnx = VecZnx::new(n, 2);
/// ///
/// (0..a.limbs()).for_each(|i|{ /// (0..a.cols()).for_each(|i|{
/// a.at_mut(i).iter_mut().enumerate().for_each(|(i, x)|{ /// a.at_mut(i).iter_mut().enumerate().for_each(|(i, x)|{
/// *x = i as i64 /// *x = i as i64
/// }) /// })
/// }); /// });
/// ///
/// module.vec_znx_automorphism_inplace(-1, &mut a, 1); // X^i -> X^(-i) /// module.vec_znx_automorphism_inplace(-1, &mut a, 1); // X^i -> X^(-i)
/// let limb = b.at_mut(0); /// let col = b.at_mut(0);
/// (1..limb.len()).for_each(|i|{ /// (1..col.len()).for_each(|i|{
/// limb[n-i] = -(i as i64) /// col[n-i] = -(i as i64)
/// }); /// });
/// izip!(a.data.iter(), b.data.iter()).for_each(|(a, b)| assert_eq!(a, b, "{} != {}", a, b)); /// izip!(a.data.iter(), b.data.iter()).for_each(|(a, b)| assert_eq!(a, b, "{} != {}", a, b));
/// ``` /// ```
fn vec_znx_automorphism_inplace<T: VecZnxApi + Infos>( fn vec_znx_automorphism_inplace<T: VecZnxApi + Infos>(&self, k: i64, a: &mut T, a_cols: usize) {
&self,
k: i64,
a: &mut T,
limbs_a: usize,
) {
assert_eq!(a.n(), self.n()); assert_eq!(a.n(), self.n());
assert!(a.cols() >= limbs_a); assert!(a.cols() >= a_cols);
unsafe { unsafe {
vec_znx::vec_znx_automorphism( vec_znx::vec_znx_automorphism(
self.0, self.0,
@@ -703,7 +698,7 @@ impl VecZnxOps for Module {
a.cols() as u64, a.cols() as u64,
a.n() as u64, a.n() as u64,
a.as_ptr(), a.as_ptr(),
limbs_a as u64, a_cols as u64,
a.n() as u64, a.n() as u64,
); );
} }