Skip to content

Commit dcf73a5

Browse files
Impl sub, mul and div for actual objects (#843)
* Impl sub, mul and div for actual objects * Add non-reference scalar multiplication Co-authored-by: Antonio Mejías Gil <anmegi.95@gmail.com> * Implement arithmetic operators with a macro Co-authored-by: Antonio Mejías Gil <anmegi.95@gmail.com> * Undonde smaller breaking change Co-authored-by: Antonio Mejías Gil <anmegi.95@gmail.com> * Add non-reference scalar multiplication Co-authored-by: Antonio Mejías Gil <anmegi.95@gmail.com> --------- Co-authored-by: Antonio Mejías Gil <anmegi.95@gmail.com>
1 parent e6374ba commit dcf73a5

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

poly/src/polynomial/univariate/dense.rs

+45-8
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)