diff --git a/src/polynomial/commitment.rs b/src/polynomial/commitment.rs index df92a31b..4b2bf872 100644 --- a/src/polynomial/commitment.rs +++ b/src/polynomial/commitment.rs @@ -4,7 +4,6 @@ use rayon::prelude::*; use crate::field::extension_field::Extendable; use crate::field::extension_field::{FieldExtension, Frobenius}; use crate::field::field::Field; -use crate::field::interpolation::interpolate2; use crate::fri::{prover::fri_proof, verifier::verify_fri_proof, FriConfig}; use crate::merkle_tree::MerkleTree; use crate::plonk_challenger::Challenger; @@ -139,7 +138,7 @@ impl ListPolynomialCommitment { let zs_composition_poly = alpha.reduce_polys(zs_polys); let zs_quotient = Self::compute_quotient([zeta, g * zeta], zs_composition_poly); - final_poly = alpha.shift_poly(final_poly); + alpha.shift_poly(&mut final_poly); final_poly += zs_quotient; // When working in an extension field, need to check that wires are in the base field. @@ -153,7 +152,7 @@ impl ListPolynomialCommitment { let wires_quotient = Self::compute_quotient([zeta, zeta.frobenius()], wire_composition_poly); - final_poly = alpha.shift_poly(final_poly); + alpha.shift_poly(&mut final_poly); final_poly += wires_quotient; let lde_final_poly = final_poly.lde(config.rate_bits); diff --git a/src/polynomial/polynomial.rs b/src/polynomial/polynomial.rs index aefcc0c6..02f66684 100644 --- a/src/polynomial/polynomial.rs +++ b/src/polynomial/polynomial.rs @@ -1,6 +1,6 @@ use std::cmp::max; use std::iter::Sum; -use std::ops::{Add, AddAssign, Mul, Sub, SubAssign}; +use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign}; use crate::field::extension_field::Extendable; use crate::field::fft::{fft, ifft}; @@ -253,6 +253,16 @@ impl AddAssign for PolynomialCoeffs { } } +impl AddAssign<&Self> for PolynomialCoeffs { + fn add_assign(&mut self, rhs: &Self) { + let len = max(self.len(), rhs.len()); + self.coeffs.resize(len, F::ZERO); + for (l, &r) in self.coeffs.iter_mut().zip(&rhs.coeffs) { + *l += r; + } + } +} + impl SubAssign for PolynomialCoeffs { fn sub_assign(&mut self, rhs: Self) { let len = max(self.len(), rhs.len()); @@ -263,6 +273,16 @@ impl SubAssign for PolynomialCoeffs { } } +impl SubAssign<&Self> for PolynomialCoeffs { + fn sub_assign(&mut self, rhs: &Self) { + let len = max(self.len(), rhs.len()); + self.coeffs.resize(len, F::ZERO); + for (l, &r) in self.coeffs.iter_mut().zip(&rhs.coeffs) { + *l -= r; + } + } +} + impl Mul for &PolynomialCoeffs { type Output = PolynomialCoeffs; @@ -272,6 +292,12 @@ impl Mul for &PolynomialCoeffs { } } +impl MulAssign for PolynomialCoeffs { + fn mul_assign(&mut self, rhs: F) { + self.coeffs.iter_mut().for_each(|x| *x *= rhs); + } +} + impl Mul for &PolynomialCoeffs { type Output = PolynomialCoeffs; diff --git a/src/util/scaling.rs b/src/util/scaling.rs index 7155cb37..25da24c7 100644 --- a/src/util/scaling.rs +++ b/src/util/scaling.rs @@ -20,9 +20,9 @@ impl ReducingFactor { self.base * x } - fn mul_poly(&mut self, p: PolynomialCoeffs) -> PolynomialCoeffs { + fn mul_poly(&mut self, p: &mut PolynomialCoeffs) { self.count += 1; - &p * self.base + *p *= self.base; } pub fn reduce(&mut self, iter: impl DoubleEndedIterator>) -> F { @@ -34,8 +34,10 @@ impl ReducingFactor { &mut self, polys: impl DoubleEndedIterator>>, ) -> PolynomialCoeffs { - polys.rev().fold(PolynomialCoeffs::empty(), |acc, x| { - &self.mul_poly(acc) + x.borrow() + polys.rev().fold(PolynomialCoeffs::empty(), |mut acc, x| { + self.mul_poly(&mut acc); + acc += x.borrow(); + acc }) } @@ -45,10 +47,9 @@ impl ReducingFactor { tmp } - pub fn shift_poly(&mut self, p: PolynomialCoeffs) -> PolynomialCoeffs { - let tmp = &p * self.base.exp(self.count); + pub fn shift_poly(&mut self, p: &mut PolynomialCoeffs) { + *p *= self.base.exp(self.count); self.count = 0; - tmp } pub fn reset(&mut self) {