mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-07 16:23:12 +00:00
Use ReducingFactor
This commit is contained in:
parent
150d764440
commit
a31c58b69d
@ -1,6 +1,6 @@
|
|||||||
use std::borrow::Borrow;
|
use std::borrow::Borrow;
|
||||||
|
|
||||||
use plonky2_field::extension_field::Extendable;
|
use plonky2_field::extension_field::{Extendable, FieldExtension};
|
||||||
use plonky2_field::field_types::Field;
|
use plonky2_field::field_types::Field;
|
||||||
use plonky2_field::polynomial::PolynomialCoeffs;
|
use plonky2_field::polynomial::PolynomialCoeffs;
|
||||||
|
|
||||||
@ -35,6 +35,11 @@ impl<F: Field> ReducingFactor<F> {
|
|||||||
self.base * x
|
self.base * x
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn mul_ext<FE: FieldExtension<D, BaseField = F>, const D: usize>(&mut self, x: FE) -> FE {
|
||||||
|
self.count += 1;
|
||||||
|
x.scalar_mul(self.base)
|
||||||
|
}
|
||||||
|
|
||||||
fn mul_poly(&mut self, p: &mut PolynomialCoeffs<F>) {
|
fn mul_poly(&mut self, p: &mut PolynomialCoeffs<F>) {
|
||||||
self.count += 1;
|
self.count += 1;
|
||||||
*p *= self.base;
|
*p *= self.base;
|
||||||
@ -45,6 +50,14 @@ impl<F: Field> ReducingFactor<F> {
|
|||||||
.fold(F::ZERO, |acc, x| self.mul(acc) + *x.borrow())
|
.fold(F::ZERO, |acc, x| self.mul(acc) + *x.borrow())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reduce_ext<FE: FieldExtension<D, BaseField = F>, const D: usize>(
|
||||||
|
&mut self,
|
||||||
|
iter: impl DoubleEndedIterator<Item = impl Borrow<FE>>,
|
||||||
|
) -> FE {
|
||||||
|
iter.rev()
|
||||||
|
.fold(FE::ZERO, |acc, x| self.mul_ext(acc) + *x.borrow())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn reduce_polys(
|
pub fn reduce_polys(
|
||||||
&mut self,
|
&mut self,
|
||||||
polys: impl DoubleEndedIterator<Item = impl Borrow<PolynomialCoeffs<F>>>,
|
polys: impl DoubleEndedIterator<Item = impl Borrow<PolynomialCoeffs<F>>>,
|
||||||
|
|||||||
@ -11,6 +11,7 @@ use plonky2::iop::ext_target::ExtensionTarget;
|
|||||||
use plonky2::iop::target::Target;
|
use plonky2::iop::target::Target;
|
||||||
use plonky2::plonk::circuit_builder::CircuitBuilder;
|
use plonky2::plonk::circuit_builder::CircuitBuilder;
|
||||||
use plonky2::plonk::config::{AlgebraicHasher, GenericConfig, Hasher};
|
use plonky2::plonk::config::{AlgebraicHasher, GenericConfig, Hasher};
|
||||||
|
use plonky2::util::reducing::{ReducingFactor, ReducingFactorTarget};
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
|
|
||||||
use crate::config::StarkConfig;
|
use crate::config::StarkConfig;
|
||||||
@ -283,19 +284,15 @@ pub(crate) fn eval_permutation_checks<F, FE, C, S, const D: usize, const D2: usi
|
|||||||
pair: PermutationPair { column_pairs },
|
pair: PermutationPair { column_pairs },
|
||||||
challenge: PermutationChallenge { beta, gamma },
|
challenge: PermutationChallenge { beta, gamma },
|
||||||
} = instance;
|
} = instance;
|
||||||
let mut reduced =
|
let mut factor = ReducingFactor::new(*beta);
|
||||||
column_pairs
|
let (lhs, rhs): (Vec<_>, Vec<_>) = column_pairs
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.map(|&(i, j)| (vars.local_values[i], vars.local_values[j]))
|
||||||
.fold((FE::ZERO, FE::ZERO), |(lhs, rhs), &(i, j)| {
|
.unzip();
|
||||||
(
|
(
|
||||||
lhs.scalar_mul(*beta) + vars.local_values[i],
|
factor.reduce_ext(lhs.into_iter()) + FE::from_basefield(*gamma),
|
||||||
rhs.scalar_mul(*beta) + vars.local_values[j],
|
factor.reduce_ext(rhs.into_iter()) + FE::from_basefield(*gamma),
|
||||||
)
|
)
|
||||||
});
|
|
||||||
reduced.0 += FE::from_basefield(*gamma);
|
|
||||||
reduced.1 += FE::from_basefield(*gamma);
|
|
||||||
reduced
|
|
||||||
})
|
})
|
||||||
.unzip();
|
.unzip();
|
||||||
let constraint = next_zs[i] * reduced_rhs.into_iter().product()
|
let constraint = next_zs[i] * reduced_rhs.into_iter().product()
|
||||||
@ -353,19 +350,17 @@ pub(crate) fn eval_permutation_checks_recursively<F, S, const D: usize>(
|
|||||||
let zero = builder.zero_extension();
|
let zero = builder.zero_extension();
|
||||||
let beta_ext = builder.convert_to_ext(*beta);
|
let beta_ext = builder.convert_to_ext(*beta);
|
||||||
let gamma_ext = builder.convert_to_ext(*gamma);
|
let gamma_ext = builder.convert_to_ext(*gamma);
|
||||||
let mut reduced =
|
let mut factor = ReducingFactorTarget::new(beta_ext);
|
||||||
column_pairs
|
let (lhs, rhs): (Vec<_>, Vec<_>) = column_pairs
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.map(|&(i, j)| (vars.local_values[i], vars.local_values[j]))
|
||||||
.fold((zero, zero), |(lhs, rhs), &(i, j)| {
|
.unzip();
|
||||||
(
|
let reduced_lhs = factor.reduce(&lhs, builder);
|
||||||
builder.mul_add_extension(lhs, beta_ext, vars.local_values[i]),
|
let reduced_rhs = factor.reduce(&rhs, builder);
|
||||||
builder.mul_add_extension(rhs, beta_ext, vars.local_values[j]),
|
(
|
||||||
)
|
builder.add_extension(reduced_lhs, gamma_ext),
|
||||||
});
|
builder.add_extension(reduced_rhs, gamma_ext),
|
||||||
reduced.0 = builder.add_extension(reduced.0, gamma_ext);
|
)
|
||||||
reduced.1 = builder.add_extension(reduced.1, gamma_ext);
|
|
||||||
reduced
|
|
||||||
})
|
})
|
||||||
.unzip();
|
.unzip();
|
||||||
let reduced_lhs_product = builder.mul_many_extension(&reduced_lhs);
|
let reduced_lhs_product = builder.mul_many_extension(&reduced_lhs);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user