mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-28 10:33:10 +00:00
Add a reduce_polys_base (#149)
* Add a reduce_polys_base Reducing the polynomials in `open_plonk` was taking ~100ms on my machine. It was converting BF polynomials to the EF early on; by doing more work in the BF we can reduce it to ~20ms. * PR feedback
This commit is contained in:
parent
bb1c083e1e
commit
2d9891983f
@ -65,6 +65,14 @@ pub trait FieldExtension<const D: usize>: Field {
|
||||
fn is_in_basefield(&self) -> bool {
|
||||
self.to_basefield_array()[1..].iter().all(|x| x.is_zero())
|
||||
}
|
||||
|
||||
fn scalar_mul(&self, scalar: Self::BaseField) -> Self {
|
||||
let mut res = self.to_basefield_array();
|
||||
res.iter_mut().for_each(|x| {
|
||||
*x *= scalar;
|
||||
});
|
||||
Self::from_basefield_array(res)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Field> FieldExtension<1> for F {
|
||||
|
||||
@ -171,7 +171,6 @@ impl<F: Field> PolynomialBatchCommitment<F> {
|
||||
let mut zs_polys = commitments[PlonkPolynomials::ZS_PARTIAL_PRODUCTS.index]
|
||||
.polynomials
|
||||
.iter()
|
||||
.map(|p| p.to_extension())
|
||||
.collect::<Vec<_>>();
|
||||
let partial_products_polys = zs_polys.split_off(common_data.zs_range().end);
|
||||
|
||||
@ -183,12 +182,11 @@ impl<F: Field> PolynomialBatchCommitment<F> {
|
||||
]
|
||||
.iter()
|
||||
.flat_map(|&p| &commitments[p.index].polynomials)
|
||||
.map(|p| p.to_extension())
|
||||
.chain(partial_products_polys);
|
||||
let single_composition_poly = timed!(
|
||||
timing,
|
||||
"reduce single polys",
|
||||
alpha.reduce_polys(single_polys)
|
||||
alpha.reduce_polys_base(single_polys)
|
||||
);
|
||||
|
||||
let single_quotient = Self::compute_quotient([zeta], single_composition_poly);
|
||||
@ -199,7 +197,7 @@ impl<F: Field> PolynomialBatchCommitment<F> {
|
||||
let zs_composition_poly = timed!(
|
||||
timing,
|
||||
"reduce Z polys",
|
||||
alpha.reduce_polys(zs_polys.into_iter())
|
||||
alpha.reduce_polys_base(zs_polys.into_iter())
|
||||
);
|
||||
|
||||
let zs_quotient = Self::compute_quotient([zeta, g * zeta], zs_composition_poly);
|
||||
|
||||
@ -5,7 +5,7 @@ use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
|
||||
use anyhow::{ensure, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::field::extension_field::Extendable;
|
||||
use crate::field::extension_field::{Extendable, FieldExtension};
|
||||
use crate::field::fft::{fft, fft_with_options, ifft};
|
||||
use crate::field::field_types::Field;
|
||||
use crate::util::log2_strict;
|
||||
@ -215,6 +215,13 @@ impl<F: Field> PolynomialCoeffs<F> {
|
||||
{
|
||||
PolynomialCoeffs::new(self.coeffs.iter().map(|&c| c.into()).collect())
|
||||
}
|
||||
|
||||
pub fn mul_extension<const D: usize>(&self, rhs: F::Extension) -> PolynomialCoeffs<F::Extension>
|
||||
where
|
||||
F: Extendable<D>,
|
||||
{
|
||||
PolynomialCoeffs::new(self.coeffs.iter().map(|&c| rhs.scalar_mul(c)).collect())
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Field> PartialEq for PolynomialCoeffs<F> {
|
||||
|
||||
@ -55,6 +55,20 @@ impl<F: Field> ReducingFactor<F> {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn reduce_polys_base<BF: Extendable<D, Extension = F>, const D: usize>(
|
||||
&mut self,
|
||||
polys: impl IntoIterator<Item = impl Borrow<PolynomialCoeffs<BF>>>,
|
||||
) -> PolynomialCoeffs<F> {
|
||||
self.base
|
||||
.powers()
|
||||
.zip(polys)
|
||||
.map(|(base_power, poly)| {
|
||||
self.count += 1;
|
||||
poly.borrow().mul_extension(base_power)
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub fn shift(&mut self, x: F) -> F {
|
||||
let tmp = self.base.exp(self.count) * x;
|
||||
self.count = 0;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user