Skip to content

Commit

Permalink
Impl sub, mul and div for actual objects (#843)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Cesar199999 and Antonio95 authored Jul 4, 2024
1 parent e6374ba commit dcf73a5
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 dcf73a5

Please sign in to comment.