Skip to content

Commit

Permalink
chore: refactor mul
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOLAND committed Dec 20, 2023
1 parent b938148 commit 77622e3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fast-ntt is a Rust package to compute polynomial multiplication in `O(nlog(n))`
.collect(),
);
let c: Constants<BigInt> = working_modulus(N, M);
println!("{}", mul(a, b, c));
println!("{}", fast_mul(a, b, c));

// Polynomial Differentiation
let a = Polynomial::new(vec![3, 2, 1].iter().map(|&x| BigInt::from(x)).collect());
Expand Down
20 changes: 10 additions & 10 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criteri
use fast_ntt::{
ntt::{forward, working_modulus, Constants},
numbers::BigInt,
polynomial::Polynomial,
polynomial::{fast_mul, mul_brute, Polynomial, PolynomialFieldElement, PolynomialTrait},
};
use itertools::Itertools;

const deg: usize = 16;

fn bench_mul(x: usize, y: usize, c: &Constants) {
let ONE = BigInt::from(1);
fn bench_mul<T: PolynomialFieldElement>(x: usize, y: usize, c: &Constants<T>) {
let ONE = T::from(1);
let a = Polynomial::new(vec![0; x].iter().map(|_| ONE).collect_vec());
let b = Polynomial::new(vec![0; y].iter().map(|_| ONE).collect_vec());
let _ = a.mul(b, c);
let _ = fast_mul(a, b, c);
}

fn bench_mul_brute(x: usize, y: usize) {
let ONE = BigInt::from(1);
fn bench_mul_brute<T: PolynomialFieldElement>(x: usize, y: usize) {
let ONE = T::from(1);
let a = Polynomial::new(vec![0; x].iter().map(|_| ONE).collect_vec());
let b = Polynomial::new(vec![0; y].iter().map(|_| ONE).collect_vec());
let _ = a.mul_brute(b);
let _ = mul_brute(a, b);
}

fn bench_forward(n: usize, c: &Constants) {
let ONE = BigInt::from(1);
fn bench_forward<T: PolynomialFieldElement>(n: usize, c: &Constants<T>) {
let ONE = T::from(1);
let a = Polynomial::new(vec![0; n].iter().map(|_| ONE).collect_vec());
let _ = forward(a.coef, c);
}
Expand Down Expand Up @@ -53,7 +53,7 @@ fn criterion_benchmark(c: &mut Criterion) {

let id = BenchmarkId::new("Brute-Force", 1 << n);
group.bench_with_input(id, &n, |b, n| {
b.iter(|| bench_mul_brute(black_box(1 << n), black_box(1 << n)))
b.iter(|| bench_mul_brute::<BigInt>(black_box(1 << n), black_box(1 << n)))
});
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub fn mul_brute<T: PolynomialFieldElement>(
}

#[cfg(feature = "parallel")]
pub fn mul<T: PolynomialFieldElement>(
pub fn fast_mul<T: PolynomialFieldElement>(
lhs: impl PolynomialTrait<T>,
rhs: impl PolynomialTrait<T>,
c: &Constants<T>,
Expand Down Expand Up @@ -160,7 +160,7 @@ pub fn mul<T: PolynomialFieldElement>(
}

#[cfg(not(feature = "parallel"))]
pub fn mul<T: PolynomialFieldElement, P: PolynomialTrait<T>>(
pub fn fast_mul<T: PolynomialFieldElement, P: PolynomialTrait<T>>(
lhs: P,
rhs: P,
c: &Constants<T>,
Expand Down Expand Up @@ -272,7 +272,7 @@ mod tests {
use crate::{
ntt::{working_modulus, Constants},
numbers::BigInt,
polynomial::{diff, mul, PolynomialTrait},
polynomial::{diff, fast_mul, PolynomialTrait},
};

#[test]
Expand Down Expand Up @@ -308,7 +308,7 @@ mod tests {
+ 1;
let c = working_modulus(N, M);

let mul = mul(a, b, &c);
let mul = fast_mul(a, b, &c);
assert_eq!(mul[0], ONE);
});
}
Expand Down

0 comments on commit 77622e3

Please sign in to comment.