From 77622e388b145bb2be417ae3bd22cc9cbe63df5f Mon Sep 17 00:00:00 2001 From: bhargav Date: Tue, 19 Dec 2023 20:32:21 -0600 Subject: [PATCH] chore: refactor `mul` --- README.md | 2 +- benches/benchmark.rs | 20 ++++++++++---------- src/polynomial.rs | 8 ++++---- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 217bbf9..900826c 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ fast-ntt is a Rust package to compute polynomial multiplication in `O(nlog(n))` .collect(), ); let c: Constants = 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()); diff --git a/benches/benchmark.rs b/benches/benchmark.rs index 8484abd..eecee94 100644 --- a/benches/benchmark.rs +++ b/benches/benchmark.rs @@ -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(x: usize, y: usize, c: &Constants) { + 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(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(n: usize, c: &Constants) { + let ONE = T::from(1); let a = Polynomial::new(vec![0; n].iter().map(|_| ONE).collect_vec()); let _ = forward(a.coef, c); } @@ -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::(black_box(1 << n), black_box(1 << n))) }); }); } diff --git a/src/polynomial.rs b/src/polynomial.rs index 8e033a3..b056a25 100644 --- a/src/polynomial.rs +++ b/src/polynomial.rs @@ -124,7 +124,7 @@ pub fn mul_brute( } #[cfg(feature = "parallel")] -pub fn mul( +pub fn fast_mul( lhs: impl PolynomialTrait, rhs: impl PolynomialTrait, c: &Constants, @@ -160,7 +160,7 @@ pub fn mul( } #[cfg(not(feature = "parallel"))] -pub fn mul>( +pub fn fast_mul>( lhs: P, rhs: P, c: &Constants, @@ -272,7 +272,7 @@ mod tests { use crate::{ ntt::{working_modulus, Constants}, numbers::BigInt, - polynomial::{diff, mul, PolynomialTrait}, + polynomial::{diff, fast_mul, PolynomialTrait}, }; #[test] @@ -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); }); }