Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some updates when I implement omr #162

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion algebra/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum AlgebraError {
/// Error that occurs when the given modulus has no primitive root with the given degree.
#[error("There is no primitive root with the degree {degree:?} and the modulus {modulus:?}!")]
NoPrimitiveRoot {
/// the degree for the primitive root
/// The degree for the primitive root
degree: Box<dyn Debug>,
/// The modulus.
modulus: Box<dyn Debug>,
Expand Down
19 changes: 16 additions & 3 deletions algebra/src/modulus/barrett/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,16 +480,29 @@ impl<T: Numeric> ReduceDotProduct<T> for BarrettModulus<T> {

debug_assert_eq!(a.len(), b.len());

a.chunks_exact(16)
.zip(b.chunks_exact(16))
let mut a_iter = a.chunks_exact(16);
let mut b_iter = b.chunks_exact(16);

let inter = (&mut a_iter)
.zip(&mut b_iter)
.map(|(a_s, b_s)| {
let mut c: [T; 2] = [T::ZERO, T::ZERO];
for (&a, &b) in a_s.iter().zip(b_s) {
multiply_add(&mut c, a, b);
}
self.reduce(c)
})
.fold(T::ZERO, |acc: T, b| self.value.reduce_add(acc, b))
.fold(T::ZERO, |acc: T, b| self.value.reduce_add(acc, b));

let mut c: [T; 2] = [T::ZERO, T::ZERO];
a_iter
.remainder()
.iter()
.zip(b_iter.remainder())
.for_each(|(&a, &b)| {
multiply_add(&mut c, a, b);
});
self.reduce_add(self.reduce(c), inter)
}
}

Expand Down
2 changes: 1 addition & 1 deletion algebra/src/modulus/barrett/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<T: Numeric> PrimitiveRoot<T> for BarrettModulus<T> {
}

let mut rng = rand::thread_rng();
let distr = Uniform::new_inclusive(T::ONE + T::ONE, modulus_minus_one);
let distr = Uniform::new_inclusive(T::TWO, modulus_minus_one);

let mut w = T::ZERO;

Expand Down
35 changes: 30 additions & 5 deletions algebra/src/ntt/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
//! Defines Number Theory Transform algorithms.

use crate::{arith::PrimitiveRoot, reduce::Modulus, AlgebraError};
use crate::{arith::PrimitiveRoot, AlgebraError};

mod table;

pub use table::*;

/// An abstract for ntt table generation.
pub trait NttTable: Sized + Clone + Send + Sync {
pub trait NttTable: Sized + Send + Sync {
/// The value type.
type ValueT;

/// The modulus type.
type ModulusT: PrimitiveRoot<Self::ValueT>;

/// Creates a new [`NttTable`].
fn new<M>(modulus: M, log_n: u32) -> Result<Self, AlgebraError>
where
M: Modulus<Self::ValueT> + PrimitiveRoot<Self::ValueT>;
fn new(modulus: Self::ModulusT, log_n: u32) -> Result<Self, AlgebraError>;

/// Get the polynomial modulus degree.
fn dimension(&self) -> usize;
Expand Down Expand Up @@ -130,4 +131,28 @@ pub trait NumberTheoryTransform: NttTable {
degree: usize,
values: &mut [<Self as NttTable>::ValueT],
);

/// Perform a fast lazy polynomial multiplication assignment.
///
/// The coefficients of the result polynomial are in the range `[0, 2*modulus)`
/// and fall back to the range `[0, modulus)` if the ntt table does not support
/// this special case.
fn lazy_mul_assign(&self, a: &mut Self::CoeffPoly, b: &Self::CoeffPoly);

/// Perform a fast polynomial multiplication assignment.
///
/// The coefficients of the result polynomial are in the range `[0, modulus)`.
fn mul_assign(&self, a: &mut Self::CoeffPoly, b: &Self::CoeffPoly);

/// Perform a fast lazy polynomial multiplication in place.
///
/// The coefficients of the result polynomial are in the range `[0, 2*modulus)`
/// and fall back to the range `[0, modulus)` if the ntt table does not support
/// this special case.
fn lazy_mul_inplace(&self, a: &Self::CoeffPoly, b: &Self::CoeffPoly, c: &mut Self::CoeffPoly);

/// Perform a fast polynomial multiplication in place.
///
/// The coefficients of the result polynomial are in the range `[0, modulus)`.
fn mul_inplace(&self, a: &Self::CoeffPoly, b: &Self::CoeffPoly, c: &mut Self::CoeffPoly);
}
Loading