Skip to content

Commit

Permalink
Implement arithmetic operators with a macro
Browse files Browse the repository at this point in the history
Co-authored-by: Antonio Mejías Gil <[email protected]>
  • Loading branch information
Cesar199999 and Antonio95 committed Jun 26, 2024
1 parent 399264e commit 8655055
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions poly/src/polynomial/univariate/dense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,6 @@ impl<F: Field> DerefMut for DensePolynomial<F> {
}
}

impl<F: Field> Add for DensePolynomial<F> {
type Output = DensePolynomial<F>;

fn add(self, other: DensePolynomial<F>) -> Self {
&self + &other
}
}

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

Expand Down Expand Up @@ -487,15 +479,6 @@ impl<'a, 'b, F: Field> Sub<&'a DensePolynomial<F>> for &'b DensePolynomial<F> {
}
}

impl<F: Field> Sub<DensePolynomial<F>> for DensePolynomial<F> {
type Output = DensePolynomial<F>;

#[inline]
fn sub(self, other: DensePolynomial<F>) -> DensePolynomial<F> {
&self - &other
}
}

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

Expand Down Expand Up @@ -593,15 +576,6 @@ impl<'a, 'b, F: Field> Div<&'a DensePolynomial<F>> for &'b DensePolynomial<F> {
}
}

impl<F: Field> Div<DensePolynomial<F>> for DensePolynomial<F> {
type Output = DensePolynomial<F>;

#[inline]
fn div(self, divisor: DensePolynomial<F>) -> DensePolynomial<F> {
&self / &divisor
}
}

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

Expand Down Expand Up @@ -647,13 +621,35 @@ impl<'a, 'b, F: FftField> Mul<&'a DensePolynomial<F>> for &'b DensePolynomial<F>
}
}

impl<F: FftField> Mul<DensePolynomial<F>> for DensePolynomial<F> {
type Output = DensePolynomial<F>;
macro_rules! impl_op {
($trait:ident, $method:ident, $field_bound:ident) => {
impl<F: $field_bound> $trait<DensePolynomial<F>> for DensePolynomial<F> {
type Output = DensePolynomial<F>;

#[inline]
fn mul(self, other: DensePolynomial<F>) -> DensePolynomial<F> {
&self * &other
}
#[inline]
fn $method(self, other: DensePolynomial<F>) -> DensePolynomial<F> {
(&self).$method(&other)
}
}

impl<'a, F: $field_bound> $trait<&'a DensePolynomial<F>> for DensePolynomial<F> {
type Output = DensePolynomial<F>;

#[inline]
fn $method(self, other: &'a DensePolynomial<F>) -> DensePolynomial<F> {
(&self).$method(other)
}
}

impl<'a, F: $field_bound> $trait<DensePolynomial<F>> for &'a DensePolynomial<F> {
type Output = DensePolynomial<F>;

#[inline]
fn $method(self, other: DensePolynomial<F>) -> DensePolynomial<F> {
self.$method(&other)
}
}
};
}

impl<F: Field> Zero for DensePolynomial<F> {
Expand All @@ -668,6 +664,11 @@ impl<F: Field> Zero for DensePolynomial<F> {
}
}

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};
Expand Down

0 comments on commit 8655055

Please sign in to comment.