Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support fixed columns in plonky3 #1537

Closed
wants to merge 14 commits into from
Prev Previous commit
Next Next commit
remove symbolic builder, use PairBuilder, adjust eval
  • Loading branch information
Schaeff committed Jul 5, 2024
commit 2e743123048003ca3362877758395e5422a064a3
45 changes: 23 additions & 22 deletions plonky3/src/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

use std::{any::TypeId, collections::BTreeMap};

use p3_air::{Air, AirBuilder, AirBuilderWithPublicValues, BaseAir};
use p3_air::{Air, AirBuilder, AirBuilderWithPublicValues, BaseAir, PairBuilder};
use p3_field::AbstractField;
use p3_goldilocks::Goldilocks;
use p3_matrix::{dense::RowMajorMatrix, MatrixRowSlices};
Expand Down Expand Up @@ -169,47 +169,42 @@ impl<'a, T: FieldElement> PowdrCircuit<'a, T> {
fn to_plonky3_expr<AB: AirBuilder<F = Val>>(
&self,
e: &AlgebraicExpression<T>,
matrix: &AB::M,
main: &AB::M,
fixed: &AB::M,
) -> AB::Expr {
let res = match e {
AlgebraicExpression::Reference(r) => {
let poly_id = r.poly_id;

let row = match r.next {
true => matrix.row_slice(1),
false => matrix.row_slice(0),
};

// witness columns indexes are unchanged, fixed ones are offset by `commitment_count`
let index = match poly_id.ptype {
match poly_id.ptype {
PolynomialType::Committed => {
assert!(
r.poly_id.id < self.analyzed.commitment_count() as u64,
"Plonky3 expects `poly_id` to be contiguous"
);
r.poly_id.id as usize
let row = main.row_slice(r.next as usize);
row[r.poly_id.id as usize].into()
}
PolynomialType::Constant => {
assert!(
r.poly_id.id < self.analyzed.constant_count() as u64,
"Plonky3 expects `poly_id` to be contiguous"
);
self.analyzed.commitment_count() + r.poly_id.id as usize
let row = fixed.row_slice(r.next as usize);
row[r.poly_id.id as usize].into()
}
PolynomialType::Intermediate => {
unreachable!("intermediate polynomials should have been inlined")
}
};

row[index].into()
}
}
AlgebraicExpression::PublicReference(_) => unimplemented!(
"public references are not supported inside algebraic expressions in plonky3"
),
AlgebraicExpression::Number(n) => AB::Expr::from(cast_to_goldilocks(*n)),
AlgebraicExpression::BinaryOperation(AlgebraicBinaryOperation { left, op, right }) => {
let left = self.to_plonky3_expr::<AB>(left, matrix);
let right = self.to_plonky3_expr::<AB>(right, matrix);
let left = self.to_plonky3_expr::<AB>(left, main, fixed);
let right = self.to_plonky3_expr::<AB>(right, main, fixed);

match op {
AlgebraicBinaryOperator::Add => left + right,
Expand All @@ -221,7 +216,7 @@ impl<'a, T: FieldElement> PowdrCircuit<'a, T> {
}
}
AlgebraicExpression::UnaryOperation(AlgebraicUnaryOperation { op, expr }) => {
let expr: <AB as AirBuilder>::Expr = self.to_plonky3_expr::<AB>(expr, matrix);
let expr: <AB as AirBuilder>::Expr = self.to_plonky3_expr::<AB>(expr, main, fixed);

match op {
AlgebraicUnaryOperator::Minus => -expr,
Expand All @@ -247,16 +242,19 @@ impl<'a, T: FieldElement> BaseAir<Val> for PowdrCircuit<'a, T> {
}
}

impl<'a, T: FieldElement, AB: AirBuilderWithPublicValues<F = Val>> Air<AB> for PowdrCircuit<'a, T> {
impl<'a, T: FieldElement, AB: AirBuilderWithPublicValues<F = Val> + PairBuilder> Air<AB>
for PowdrCircuit<'a, T>
{
fn eval(&self, builder: &mut AB) {
let matrix = builder.main();
let main = builder.main();
let fixed = builder.preprocessed();
let pi = builder.public_values();
let publics = self.get_publics();
assert_eq!(publics.len(), pi.len());

// public constraints
let pi_moved = pi.to_vec();
let (local, next) = (matrix.row_slice(0), matrix.row_slice(1));
let (local, next) = (main.row_slice(0), main.row_slice(1));

let public_offset = self.analyzed.commitment_count() + self.analyzed.constant_count();

Expand Down Expand Up @@ -301,8 +299,11 @@ impl<'a, T: FieldElement, AB: AirBuilderWithPublicValues<F = Val>> Air<AB> for P
assert_eq!(identity.right.expressions.len(), 0);
assert!(identity.right.selector.is_none());

let left = self
.to_plonky3_expr::<AB>(identity.left.selector.as_ref().unwrap(), &matrix);
let left = self.to_plonky3_expr::<AB>(
identity.left.selector.as_ref().unwrap(),
&main,
&fixed,
);

builder.assert_zero(left);
}
Expand Down
14 changes: 13 additions & 1 deletion plonky3/src/stark/folder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use p3_air::{AirBuilder, AirBuilderWithPublicValues, TwoRowMatrixView};
use p3_air::{AirBuilder, AirBuilderWithPublicValues, PairBuilder, TwoRowMatrixView};
use p3_field::AbstractField;

use p3_uni_stark::{PackedChallenge, PackedVal, StarkGenericConfig, Val};
Expand Down Expand Up @@ -68,6 +68,12 @@ impl<'a, SC: StarkGenericConfig> AirBuilderWithPublicValues for ProverConstraint
}
}

impl<'a, SC: StarkGenericConfig> PairBuilder for ProverConstraintFolder<'a, SC> {
fn preprocessed(&self) -> Self::M {
self.fixed
}
}

impl<'a, SC: StarkGenericConfig> AirBuilder for VerifierConstraintFolder<'a, SC> {
type F = Val<SC>;
type Expr = SC::Challenge;
Expand Down Expand Up @@ -107,3 +113,9 @@ impl<'a, SC: StarkGenericConfig> AirBuilderWithPublicValues for VerifierConstrai
self.public_values
}
}

impl<'a, SC: StarkGenericConfig> PairBuilder for VerifierConstraintFolder<'a, SC> {
fn preprocessed(&self) -> Self::M {
self.fixed
}
}
8 changes: 5 additions & 3 deletions plonky3/src/stark/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use p3_commit::{Pcs, PolynomialSpace};
use p3_field::{AbstractExtensionField, AbstractField, PackedValue};
use p3_matrix::MatrixGet;
use p3_matrix::{dense::RowMajorMatrix, Matrix};
use p3_uni_stark::{get_log_quotient_degree, Domain, SymbolicAirBuilder, Val};
use p3_uni_stark::{Domain, Val};
use p3_uni_stark::{PackedChallenge, PackedVal, StarkGenericConfig};
use p3_util::log2_ceil_usize;
use p3_util::log2_strict_usize;
use rayon::iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator};

Expand All @@ -26,14 +27,15 @@ pub fn prove<SC, A>(
) -> Proof<SC>
where
SC: StarkGenericConfig,
A: Air<SymbolicAirBuilder<Val<SC>>> + for<'a> Air<ProverConstraintFolder<'a, SC>>,
A: for<'a> Air<ProverConstraintFolder<'a, SC>>,
{
let proving_key = proving_key.expect("only fixed pls");

let degree = trace.height();
let log_degree = log2_strict_usize(degree);

let log_quotient_degree = get_log_quotient_degree::<Val<SC>, A>(air, public_values.len());
let constraint_degree = 2; // TODO: enforce that
let log_quotient_degree = log2_ceil_usize(constraint_degree - 1);
let quotient_degree = 1 << log_quotient_degree;

let pcs = config.pcs();
Expand Down
10 changes: 5 additions & 5 deletions plonky3/src/stark/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use p3_air::{Air, BaseAir, TwoRowMatrixView};
use p3_challenger::{CanObserve, CanSample, FieldChallenger};
use p3_commit::{Pcs, PolynomialSpace};
use p3_field::{AbstractExtensionField, AbstractField, Field};
use p3_uni_stark::{
get_log_quotient_degree, StarkGenericConfig, SymbolicAirBuilder, Val, VerificationError,
};
use p3_uni_stark::{StarkGenericConfig, Val, VerificationError};
use p3_util::log2_ceil_usize;

use super::{
folder::VerifierConstraintFolder,
Expand All @@ -22,7 +21,7 @@ pub fn verify<SC, A>(
) -> Result<(), VerificationError>
where
SC: StarkGenericConfig,
A: Air<SymbolicAirBuilder<Val<SC>>> + for<'a> Air<VerifierConstraintFolder<'a, SC>>,
A: for<'a> Air<VerifierConstraintFolder<'a, SC>>,
{
let verifying_key = verifying_key.expect("fixed please");

Expand All @@ -34,7 +33,8 @@ where
} = proof;

let degree = 1 << degree_bits;
let log_quotient_degree = get_log_quotient_degree::<Val<SC>, A>(air, public_values.len());
let constraint_degree = 2; // TODO: enforce that
let log_quotient_degree = log2_ceil_usize(constraint_degree - 1);
let quotient_degree = 1 << log_quotient_degree;

let pcs = config.pcs();
Expand Down
Loading