From 8c4c4bc0c603bc4c0a2b900064cea45b3d07b9d3 Mon Sep 17 00:00:00 2001 From: Cesar199999 Date: Tue, 25 Jun 2024 16:12:24 +0200 Subject: [PATCH 1/5] Impl sub, mul and div for actual objects --- poly/src/polynomial/univariate/dense.rs | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/poly/src/polynomial/univariate/dense.rs b/poly/src/polynomial/univariate/dense.rs index 1df019d58..f9cec676f 100644 --- a/poly/src/polynomial/univariate/dense.rs +++ b/poly/src/polynomial/univariate/dense.rs @@ -487,6 +487,15 @@ impl<'a, 'b, F: Field> Sub<&'a DensePolynomial> for &'b DensePolynomial { } } +impl Sub> for DensePolynomial { + type Output = DensePolynomial; + + #[inline] + fn sub(self, other: DensePolynomial) -> DensePolynomial { + &self - &other + } +} + impl<'a, 'b, F: Field> Sub<&'a SparsePolynomial> for &'b DensePolynomial { type Output = DensePolynomial; @@ -584,6 +593,15 @@ impl<'a, 'b, F: Field> Div<&'a DensePolynomial> for &'b DensePolynomial { } } +impl Div> for DensePolynomial { + type Output = DensePolynomial; + + #[inline] + fn div(self, divisor: DensePolynomial) -> DensePolynomial { + &self / &divisor + } +} + impl<'b, F: Field> Mul for &'b DensePolynomial { type Output = DensePolynomial; @@ -620,6 +638,15 @@ impl<'a, 'b, F: FftField> Mul<&'a DensePolynomial> for &'b DensePolynomial } } +impl Mul> for DensePolynomial { + type Output = DensePolynomial; + + #[inline] + fn mul(self, other: DensePolynomial) -> DensePolynomial { + &self * &other + } +} + impl Zero for DensePolynomial { /// Returns the zero polynomial. fn zero() -> Self { From 399264ef54dea8893365dd7738ebd96d251756c5 Mon Sep 17 00:00:00 2001 From: Cesar199999 Date: Wed, 26 Jun 2024 09:33:01 +0200 Subject: [PATCH 2/5] Add non-reference scalar multiplication MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Mejías Gil --- poly/src/polynomial/univariate/dense.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/poly/src/polynomial/univariate/dense.rs b/poly/src/polynomial/univariate/dense.rs index f9cec676f..3dfd3dfeb 100644 --- a/poly/src/polynomial/univariate/dense.rs +++ b/poly/src/polynomial/univariate/dense.rs @@ -619,6 +619,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; From 865505507f9727d29250d5e1798b3890b8635832 Mon Sep 17 00:00:00 2001 From: Cesar199999 Date: Wed, 26 Jun 2024 11:07:28 +0200 Subject: [PATCH 3/5] Implement arithmetic operators with a macro MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Mejías Gil --- poly/src/polynomial/univariate/dense.rs | 65 +++++++++++++------------ 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/poly/src/polynomial/univariate/dense.rs b/poly/src/polynomial/univariate/dense.rs index 3dfd3dfeb..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; @@ -487,15 +479,6 @@ impl<'a, 'b, F: Field> Sub<&'a DensePolynomial> for &'b DensePolynomial { } } -impl Sub> for DensePolynomial { - type Output = DensePolynomial; - - #[inline] - fn sub(self, other: DensePolynomial) -> DensePolynomial { - &self - &other - } -} - impl<'a, 'b, F: Field> Sub<&'a SparsePolynomial> for &'b DensePolynomial { type Output = DensePolynomial; @@ -593,15 +576,6 @@ impl<'a, 'b, F: Field> Div<&'a DensePolynomial> for &'b DensePolynomial { } } -impl Div> for DensePolynomial { - type Output = DensePolynomial; - - #[inline] - fn div(self, divisor: DensePolynomial) -> DensePolynomial { - &self / &divisor - } -} - impl<'b, F: Field> Mul for &'b DensePolynomial { type Output = DensePolynomial; @@ -647,13 +621,35 @@ impl<'a, 'b, F: FftField> Mul<&'a DensePolynomial> for &'b DensePolynomial } } -impl Mul> for DensePolynomial { - type Output = DensePolynomial; +macro_rules! impl_op { + ($trait:ident, $method:ident, $field_bound:ident) => { + impl $trait> for DensePolynomial { + type Output = DensePolynomial; - #[inline] - fn mul(self, other: DensePolynomial) -> DensePolynomial { - &self * &other - } + #[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 { @@ -668,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}; From 4f14ebe58d061f736611e9f7847325cbdab698db Mon Sep 17 00:00:00 2001 From: Cesar199999 Date: Wed, 26 Jun 2024 11:16:39 +0200 Subject: [PATCH 4/5] Undonde smaller breaking change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Mejías Gil --- poly/src/polynomial/univariate/dense.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/poly/src/polynomial/univariate/dense.rs b/poly/src/polynomial/univariate/dense.rs index dca859fd0..5b9ba4643 100644 --- a/poly/src/polynomial/univariate/dense.rs +++ b/poly/src/polynomial/univariate/dense.rs @@ -593,15 +593,6 @@ 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; From ee19e232c2f4922635ef8b6c0ff9531fce039494 Mon Sep 17 00:00:00 2001 From: Cesar199999 Date: Wed, 26 Jun 2024 09:33:01 +0200 Subject: [PATCH 5/5] Add non-reference scalar multiplication MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Mejías Gil --- poly/src/polynomial/univariate/dense.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/poly/src/polynomial/univariate/dense.rs b/poly/src/polynomial/univariate/dense.rs index 5b9ba4643..dca859fd0 100644 --- a/poly/src/polynomial/univariate/dense.rs +++ b/poly/src/polynomial/univariate/dense.rs @@ -593,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;