Skip to content

Commit 833fb44

Browse files
authored
Merge branch 'master' into master
2 parents 95b1196 + dcf73a5 commit 833fb44

File tree

3 files changed

+99
-10
lines changed

3 files changed

+99
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [\#772](https://github.com/arkworks-rs/algebra/pull/772) (`ark-ff`) Implementation of `mul` method for `BigInteger`.
66
- [\#794](https://github.com/arkworks-rs/algebra/pull/794) (`ark-ff`) Fix `wasm` compilation.
77
- [\#837](https://github.com/arkworks-rs/algebra/pull/837) (`ark-serialize`) Fix array deserialization panic.
8+
- [\#845](https://github.com/arkworks-rs/algebra/pull/845) (`Algebra`) Implementation of `mul` method for `DenseMultilinearExtension<F> * F`.
89

910
### Breaking changes
1011

poly/src/evaluations/multivariate/multilinear/dense.rs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use ark_std::{
1111
fmt::Formatter,
1212
iter::IntoIterator,
1313
log2,
14-
ops::{Add, AddAssign, Index, Neg, Sub, SubAssign},
14+
ops::{Add, AddAssign, Index, Mul, MulAssign, Neg, Sub, SubAssign},
1515
rand::Rng,
1616
slice::{Iter, IterMut},
1717
vec::*,
@@ -331,6 +331,44 @@ impl<'a, F: Field> SubAssign<&'a DenseMultilinearExtension<F>> for DenseMultilin
331331
}
332332
}
333333

334+
impl<F: Field> Mul<F> for DenseMultilinearExtension<F> {
335+
type Output = DenseMultilinearExtension<F>;
336+
337+
fn mul(self, scalar: F) -> Self::Output {
338+
&self * &scalar
339+
}
340+
}
341+
342+
impl<'a, 'b, F: Field> Mul<&'a F> for &'b DenseMultilinearExtension<F> {
343+
type Output = DenseMultilinearExtension<F>;
344+
345+
fn mul(self, scalar: &'a F) -> Self::Output {
346+
if scalar.is_zero() {
347+
return DenseMultilinearExtension::zero();
348+
} else if scalar.is_one() {
349+
return self.clone();
350+
}
351+
let result: Vec<F> = self.evaluations.iter().map(|&x| x * scalar).collect();
352+
353+
DenseMultilinearExtension {
354+
num_vars: self.num_vars,
355+
evaluations: result,
356+
}
357+
}
358+
}
359+
360+
impl<F: Field> MulAssign<F> for DenseMultilinearExtension<F> {
361+
fn mul_assign(&mut self, scalar: F) {
362+
*self = &*self * &scalar
363+
}
364+
}
365+
366+
impl<'a, F: Field> MulAssign<&'a F> for DenseMultilinearExtension<F> {
367+
fn mul_assign(&mut self, scalar: &'a F) {
368+
*self = &*self * scalar
369+
}
370+
}
371+
334372
impl<F: Field> fmt::Debug for DenseMultilinearExtension<F> {
335373
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
336374
write!(f, "DenseML(nv = {}, evaluations = [", self.num_vars)?;
@@ -394,7 +432,7 @@ impl<F: Field> Polynomial<F> for DenseMultilinearExtension<F> {
394432
#[cfg(test)]
395433
mod tests {
396434
use crate::{DenseMultilinearExtension, MultilinearExtension, Polynomial};
397-
use ark_ff::{Field, Zero};
435+
use ark_ff::{Field, One, Zero};
398436
use ark_std::{ops::Neg, test_rng, vec::*, UniformRand};
399437
use ark_test_curves::bls12_381::Fr;
400438

@@ -471,6 +509,7 @@ mod tests {
471509
const NV: usize = 10;
472510
let mut rng = test_rng();
473511
for _ in 0..20 {
512+
let scalar = Fr::rand(&mut rng);
474513
let point: Vec<_> = (0..NV).map(|_| Fr::rand(&mut rng)).collect();
475514
let poly1 = DenseMultilinearExtension::rand(NV, &mut rng);
476515
let poly2 = DenseMultilinearExtension::rand(NV, &mut rng);
@@ -482,6 +521,8 @@ mod tests {
482521
assert_eq!((&poly1 - &poly2).evaluate(&point), v1 - v2);
483522
// test negate
484523
assert_eq!(poly1.clone().neg().evaluate(&point), -v1);
524+
// test mul poly by scalar
525+
assert_eq!((&poly1 * &scalar).evaluate(&point), v1 * scalar);
485526
// test add assign
486527
{
487528
let mut poly1 = poly1.clone();
@@ -515,6 +556,16 @@ mod tests {
515556
assert_eq!(zero.evaluate(&point), scalar * v1);
516557
}
517558
}
559+
// test mul_assign for poly * scalar
560+
{
561+
let mut poly1_cloned = poly1.clone();
562+
poly1_cloned *= Fr::one();
563+
assert_eq!(poly1_cloned.evaluate(&point), v1);
564+
poly1_cloned *= scalar;
565+
assert_eq!(poly1_cloned.evaluate(&point), v1 * scalar);
566+
poly1_cloned *= Fr::zero();
567+
assert_eq!(poly1_cloned, DenseMultilinearExtension::zero());
568+
}
518569
}
519570
}
520571

poly/src/polynomial/univariate/dense.rs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,6 @@ impl<F: Field> DerefMut for DensePolynomial<F> {
285285
}
286286
}
287287

288-
impl<F: Field> Add for DensePolynomial<F> {
289-
type Output = DensePolynomial<F>;
290-
291-
fn add(self, other: DensePolynomial<F>) -> Self {
292-
&self + &other
293-
}
294-
}
295-
296288
impl<'a, 'b, F: Field> Add<&'a DensePolynomial<F>> for &'b DensePolynomial<F> {
297289
type Output = DensePolynomial<F>;
298290

@@ -601,6 +593,15 @@ impl<'b, F: Field> Mul<F> for &'b DensePolynomial<F> {
601593
}
602594
}
603595

596+
impl<F: Field> Mul<F> for DensePolynomial<F> {
597+
type Output = DensePolynomial<F>;
598+
599+
#[inline]
600+
fn mul(self, elem: F) -> DensePolynomial<F> {
601+
&self * elem
602+
}
603+
}
604+
604605
/// Performs O(nlogn) multiplication of polynomials if F is smooth.
605606
impl<'a, 'b, F: FftField> Mul<&'a DensePolynomial<F>> for &'b DensePolynomial<F> {
606607
type Output = DensePolynomial<F>;
@@ -620,6 +621,37 @@ impl<'a, 'b, F: FftField> Mul<&'a DensePolynomial<F>> for &'b DensePolynomial<F>
620621
}
621622
}
622623

624+
macro_rules! impl_op {
625+
($trait:ident, $method:ident, $field_bound:ident) => {
626+
impl<F: $field_bound> $trait<DensePolynomial<F>> for DensePolynomial<F> {
627+
type Output = DensePolynomial<F>;
628+
629+
#[inline]
630+
fn $method(self, other: DensePolynomial<F>) -> DensePolynomial<F> {
631+
(&self).$method(&other)
632+
}
633+
}
634+
635+
impl<'a, F: $field_bound> $trait<&'a DensePolynomial<F>> for DensePolynomial<F> {
636+
type Output = DensePolynomial<F>;
637+
638+
#[inline]
639+
fn $method(self, other: &'a DensePolynomial<F>) -> DensePolynomial<F> {
640+
(&self).$method(other)
641+
}
642+
}
643+
644+
impl<'a, F: $field_bound> $trait<DensePolynomial<F>> for &'a DensePolynomial<F> {
645+
type Output = DensePolynomial<F>;
646+
647+
#[inline]
648+
fn $method(self, other: DensePolynomial<F>) -> DensePolynomial<F> {
649+
self.$method(&other)
650+
}
651+
}
652+
};
653+
}
654+
623655
impl<F: Field> Zero for DensePolynomial<F> {
624656
/// Returns the zero polynomial.
625657
fn zero() -> Self {
@@ -632,6 +664,11 @@ impl<F: Field> Zero for DensePolynomial<F> {
632664
}
633665
}
634666

667+
impl_op!(Add, add, Field);
668+
impl_op!(Sub, sub, Field);
669+
impl_op!(Mul, mul, FftField);
670+
impl_op!(Div, div, Field);
671+
635672
#[cfg(test)]
636673
mod tests {
637674
use crate::{polynomial::univariate::*, GeneralEvaluationDomain};

0 commit comments

Comments
 (0)