Skip to content

Commit

Permalink
Add scalar multiplication impl for DensePolynomial (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanleh authored Apr 3, 2021
1 parent e7eeea3 commit 0bd355b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<F>` implementation for `DensePolynomial`

### Improvements

Expand Down
30 changes: 30 additions & 0 deletions poly/src/polynomial/univariate/dense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,23 @@ impl<'a, 'b, F: Field> Div<&'a DensePolynomial<F>> for &'b DensePolynomial<F> {
}
}

impl<'a, 'b, F: Field> Mul<F> for &'b DensePolynomial<F> {
type Output = DensePolynomial<F>;

#[inline]
fn mul(self, elem: F) -> DensePolynomial<F> {
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<F>> for &'b DensePolynomial<F> {
type Output = DensePolynomial<F>;
Expand Down Expand Up @@ -566,6 +583,19 @@ mod tests {
}
}

#[test]
fn mul_random_element() {
let rng = &mut test_rng();
for degree in 0..70 {
let a = DensePolynomial::<Fr>::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();
Expand Down

0 comments on commit 0bd355b

Please sign in to comment.