From 0bd355b3405c5dfd136c5b57a673727ac99513de Mon Sep 17 00:00:00 2001 From: Ryan Lehmkuhl Date: Sat, 3 Apr 2021 10:20:24 -0700 Subject: [PATCH] Add scalar multiplication impl for DensePolynomial (#258) --- CHANGELOG.md | 1 + poly/src/polynomial/univariate/dense.rs | 30 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b290b328e..dcee9e16b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Features - [\#230](https://github.com/arkworks-rs/algebra/pull/230) (ark-ec) Add `wnaf_mul` implementation for `ProjectiveCurve`. +- [\#258](https://github.com/arkworks-rs/algebra/pull/258) (ark-poly) Add `Mul` implementation for `DensePolynomial` ### Improvements diff --git a/poly/src/polynomial/univariate/dense.rs b/poly/src/polynomial/univariate/dense.rs index aedd6c6b7..a86474904 100644 --- a/poly/src/polynomial/univariate/dense.rs +++ b/poly/src/polynomial/univariate/dense.rs @@ -398,6 +398,23 @@ impl<'a, 'b, F: Field> Div<&'a DensePolynomial> for &'b DensePolynomial { } } +impl<'a, 'b, F: Field> Mul for &'b DensePolynomial { + type Output = DensePolynomial; + + #[inline] + fn mul(self, elem: F) -> DensePolynomial { + if self.is_zero() || elem.is_zero() { + DensePolynomial::zero() + } else { + let mut result = self.clone(); + cfg_iter_mut!(result).for_each(|e| { + *e *= elem; + }); + result + } + } +} + /// Performs O(nlogn) multiplication of polynomials if F is smooth. impl<'a, 'b, F: FftField> Mul<&'a DensePolynomial> for &'b DensePolynomial { type Output = DensePolynomial; @@ -566,6 +583,19 @@ mod tests { } } + #[test] + fn mul_random_element() { + let rng = &mut test_rng(); + for degree in 0..70 { + let a = DensePolynomial::::rand(degree, rng); + let e = Fr::rand(rng); + assert_eq!( + &a * e, + a.naive_mul(&DensePolynomial::from_coefficients_slice(&[e])) + ) + } + } + #[test] fn mul_polynomials_random() { let rng = &mut test_rng();