mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-10 17:53:06 +00:00
Optimize some polynomial operations to avoid cloning.
This commit is contained in:
parent
26e669ddec
commit
492b04843e
@ -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<F: Field> ListPolynomialCommitment<F> {
|
||||
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<F: Field> ListPolynomialCommitment<F> {
|
||||
|
||||
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);
|
||||
|
||||
@ -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<F: Field> AddAssign for PolynomialCoeffs<F> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Field> AddAssign<&Self> for PolynomialCoeffs<F> {
|
||||
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<F: Field> SubAssign for PolynomialCoeffs<F> {
|
||||
fn sub_assign(&mut self, rhs: Self) {
|
||||
let len = max(self.len(), rhs.len());
|
||||
@ -263,6 +273,16 @@ impl<F: Field> SubAssign for PolynomialCoeffs<F> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Field> SubAssign<&Self> for PolynomialCoeffs<F> {
|
||||
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<F: Field> Mul<F> for &PolynomialCoeffs<F> {
|
||||
type Output = PolynomialCoeffs<F>;
|
||||
|
||||
@ -272,6 +292,12 @@ impl<F: Field> Mul<F> for &PolynomialCoeffs<F> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Field> MulAssign<F> for PolynomialCoeffs<F> {
|
||||
fn mul_assign(&mut self, rhs: F) {
|
||||
self.coeffs.iter_mut().for_each(|x| *x *= rhs);
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Field> Mul for &PolynomialCoeffs<F> {
|
||||
type Output = PolynomialCoeffs<F>;
|
||||
|
||||
|
||||
@ -20,9 +20,9 @@ impl<F: Field> ReducingFactor<F> {
|
||||
self.base * x
|
||||
}
|
||||
|
||||
fn mul_poly(&mut self, p: PolynomialCoeffs<F>) -> PolynomialCoeffs<F> {
|
||||
fn mul_poly(&mut self, p: &mut PolynomialCoeffs<F>) {
|
||||
self.count += 1;
|
||||
&p * self.base
|
||||
*p *= self.base;
|
||||
}
|
||||
|
||||
pub fn reduce(&mut self, iter: impl DoubleEndedIterator<Item = impl Borrow<F>>) -> F {
|
||||
@ -34,8 +34,10 @@ impl<F: Field> ReducingFactor<F> {
|
||||
&mut self,
|
||||
polys: impl DoubleEndedIterator<Item = impl Borrow<PolynomialCoeffs<F>>>,
|
||||
) -> PolynomialCoeffs<F> {
|
||||
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<F: Field> ReducingFactor<F> {
|
||||
tmp
|
||||
}
|
||||
|
||||
pub fn shift_poly(&mut self, p: PolynomialCoeffs<F>) -> PolynomialCoeffs<F> {
|
||||
let tmp = &p * self.base.exp(self.count);
|
||||
pub fn shift_poly(&mut self, p: &mut PolynomialCoeffs<F>) {
|
||||
*p *= self.base.exp(self.count);
|
||||
self.count = 0;
|
||||
tmp
|
||||
}
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user