From dcf73a5f9610ba9d16a3c8e0de0b3835e5e5d5e4 Mon Sep 17 00:00:00 2001 From: Cesar Descalzo Date: Thu, 4 Jul 2024 04:08:27 +0200 Subject: [PATCH] Impl sub, mul and div for actual objects (#843) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Impl sub, mul and div for actual objects * Add non-reference scalar multiplication Co-authored-by: Antonio Mejías Gil * Implement arithmetic operators with a macro Co-authored-by: Antonio Mejías Gil * Undonde smaller breaking change Co-authored-by: Antonio Mejías Gil * Add non-reference scalar multiplication Co-authored-by: Antonio Mejías Gil --------- Co-authored-by: Antonio Mejías Gil --- poly/src/polynomial/univariate/dense.rs | 53 +++++++++++++++++++++---- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/poly/src/polynomial/univariate/dense.rs b/poly/src/polynomial/univariate/dense.rs index 1df019d58..dca859fd0 100644 --- a/poly/src/polynomial/univariate/dense.rs +++ b/poly/src/polynomial/univariate/dense.rs @@ -285,14 +285,6 @@ impl DerefMut for DensePolynomial { } } -impl Add for DensePolynomial { - type Output = DensePolynomial; - - fn add(self, other: DensePolynomial) -> Self { - &self + &other - } -} - impl<'a, 'b, F: Field> Add<&'a DensePolynomial> for &'b DensePolynomial { type Output = DensePolynomial; @@ -601,6 +593,15 @@ impl<'b, F: Field> Mul for &'b DensePolynomial { } } +impl Mul for DensePolynomial { + type Output = DensePolynomial; + + #[inline] + fn mul(self, elem: F) -> DensePolynomial { + &self * elem + } +} + /// Performs O(nlogn) multiplication of polynomials if F is smooth. impl<'a, 'b, F: FftField> Mul<&'a DensePolynomial> for &'b DensePolynomial { type Output = DensePolynomial; @@ -620,6 +621,37 @@ impl<'a, 'b, F: FftField> Mul<&'a DensePolynomial> for &'b DensePolynomial } } +macro_rules! impl_op { + ($trait:ident, $method:ident, $field_bound:ident) => { + impl $trait> for DensePolynomial { + type Output = DensePolynomial; + + #[inline] + fn $method(self, other: DensePolynomial) -> DensePolynomial { + (&self).$method(&other) + } + } + + impl<'a, F: $field_bound> $trait<&'a DensePolynomial> for DensePolynomial { + type Output = DensePolynomial; + + #[inline] + fn $method(self, other: &'a DensePolynomial) -> DensePolynomial { + (&self).$method(other) + } + } + + impl<'a, F: $field_bound> $trait> for &'a DensePolynomial { + type Output = DensePolynomial; + + #[inline] + fn $method(self, other: DensePolynomial) -> DensePolynomial { + self.$method(&other) + } + } + }; +} + impl Zero for DensePolynomial { /// Returns the zero polynomial. fn zero() -> Self { @@ -632,6 +664,11 @@ impl Zero for DensePolynomial { } } +impl_op!(Add, add, Field); +impl_op!(Sub, sub, Field); +impl_op!(Mul, mul, FftField); +impl_op!(Div, div, Field); + #[cfg(test)] mod tests { use crate::{polynomial::univariate::*, GeneralEvaluationDomain};