Skip to content

Commit

Permalink
Merge pull request #5 from HungryCatsStudio/cesar/non-reference-polyn…
Browse files Browse the repository at this point in the history
…omial-arithmetic

Cesar/non reference polynomial arithmetic
  • Loading branch information
Cesar199999 committed Jun 28, 2024
2 parents 5a781ae + ee19e23 commit 5f38a62
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 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 @@ -601,6 +593,15 @@ impl<'b, F: Field> Mul<F> for &'b DensePolynomial<F> {
}
}

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

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

/// Performs O(nlogn) multiplication of polynomials if F is smooth.
impl<'a, 'b, F: FftField> Mul<&'a DensePolynomial<F>> for &'b DensePolynomial<F> {
type Output = DensePolynomial<F>;
Expand All @@ -620,6 +621,37 @@ impl<'a, 'b, F: FftField> Mul<&'a DensePolynomial<F>> for &'b 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 $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> {
/// Returns the zero polynomial.
fn zero() -> Self {
Expand All @@ -632,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 5f38a62

Please sign in to comment.