Skip to content

Commit

Permalink
feat(Recursion): evaluate constraints in a single expression (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirhemo authored Apr 25, 2024
1 parent b4ae919 commit d6d5560
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 154 deletions.
49 changes: 32 additions & 17 deletions core/src/stark/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,17 @@ impl<'a, SC: StarkGenericConfig> AirBuilderWithPublicValues for ProverConstraint
}
}

pub type VerifierConstraintFolder<'a, SC> =
GenericVerifierConstraintFolder<'a, Val<SC>, Challenge<SC>, Challenge<SC>, Challenge<SC>>;
pub type VerifierConstraintFolder<'a, SC> = GenericVerifierConstraintFolder<
'a,
Val<SC>,
Challenge<SC>,
Val<SC>,
Challenge<SC>,
Challenge<SC>,
>;

/// A folder for verifier constraints.
pub struct GenericVerifierConstraintFolder<'a, F, EF, Var, Expr> {
pub struct GenericVerifierConstraintFolder<'a, F, EF, PubVar, Var, Expr> {
pub preprocessed: VerticalPair<RowMajorMatrixView<'a, Var>, RowMajorMatrixView<'a, Var>>,
pub main: VerticalPair<RowMajorMatrixView<'a, Var>, RowMajorMatrixView<'a, Var>>,
pub perm: VerticalPair<RowMajorMatrixView<'a, Var>, RowMajorMatrixView<'a, Var>>,
Expand All @@ -140,11 +146,12 @@ pub struct GenericVerifierConstraintFolder<'a, F, EF, Var, Expr> {
pub is_transition: Var,
pub alpha: Var,
pub accumulator: Expr,
pub public_values: &'a [F],
pub public_values: &'a [PubVar],
pub _marker: PhantomData<(F, EF)>,
}

impl<'a, F, EF, Var, Expr> AirBuilder for GenericVerifierConstraintFolder<'a, F, EF, Var, Expr>
impl<'a, F, EF, PubVar, Var, Expr> AirBuilder
for GenericVerifierConstraintFolder<'a, F, EF, PubVar, Var, Expr>
where
F: Field,
EF: ExtensionField<F>,
Expand All @@ -170,6 +177,7 @@ where
+ Mul<Expr, Output = Expr>
+ Send
+ Sync,
PubVar: Into<Expr> + Copy,
{
type F = F;
type Expr = Expr;
Expand Down Expand Up @@ -203,8 +211,8 @@ where
}
}

impl<'a, F, EF, Var, Expr> ExtensionBuilder
for GenericVerifierConstraintFolder<'a, F, EF, Var, Expr>
impl<'a, F, EF, PubVar, Var, Expr> ExtensionBuilder
for GenericVerifierConstraintFolder<'a, F, EF, PubVar, Var, Expr>
where
F: Field,
EF: ExtensionField<F>,
Expand All @@ -230,6 +238,7 @@ where
+ Mul<Expr, Output = Expr>
+ Send
+ Sync,
PubVar: Into<Expr> + Copy,
{
type EF = EF;
type ExprEF = Expr;
Expand All @@ -243,8 +252,8 @@ where
}
}

impl<'a, F, EF, Var, Expr> PermutationAirBuilder
for GenericVerifierConstraintFolder<'a, F, EF, Var, Expr>
impl<'a, F, EF, PubVar, Var, Expr> PermutationAirBuilder
for GenericVerifierConstraintFolder<'a, F, EF, PubVar, Var, Expr>
where
F: Field,
EF: ExtensionField<F>,
Expand All @@ -270,6 +279,7 @@ where
+ Mul<Expr, Output = Expr>
+ Send
+ Sync,
PubVar: Into<Expr> + Copy,
{
type MP = VerticalPair<RowMajorMatrixView<'a, Var>, RowMajorMatrixView<'a, Var>>;
type RandomVar = Var;
Expand All @@ -283,8 +293,8 @@ where
}
}

impl<'a, F, EF, Var, Expr> MultiTableAirBuilder
for GenericVerifierConstraintFolder<'a, F, EF, Var, Expr>
impl<'a, F, EF, PubVar, Var, Expr> MultiTableAirBuilder
for GenericVerifierConstraintFolder<'a, F, EF, PubVar, Var, Expr>
where
F: Field,
EF: ExtensionField<F>,
Expand All @@ -310,6 +320,7 @@ where
+ Mul<Expr, Output = Expr>
+ Send
+ Sync,
PubVar: Into<Expr> + Copy,
{
type Sum = Var;

Expand All @@ -318,7 +329,8 @@ where
}
}

impl<'a, F, EF, Var, Expr> PairBuilder for GenericVerifierConstraintFolder<'a, F, EF, Var, Expr>
impl<'a, F, EF, PubVar, Var, Expr> PairBuilder
for GenericVerifierConstraintFolder<'a, F, EF, PubVar, Var, Expr>
where
F: Field,
EF: ExtensionField<F>,
Expand All @@ -344,14 +356,15 @@ where
+ Mul<Expr, Output = Expr>
+ Send
+ Sync,
PubVar: Into<Expr> + Copy,
{
fn preprocessed(&self) -> Self::M {
self.preprocessed
}
}

impl<'a, F, EF, Var, Expr> EmptyMessageBuilder
for GenericVerifierConstraintFolder<'a, F, EF, Var, Expr>
impl<'a, F, EF, PubVar, Var, Expr> EmptyMessageBuilder
for GenericVerifierConstraintFolder<'a, F, EF, PubVar, Var, Expr>
where
F: Field,
EF: ExtensionField<F>,
Expand All @@ -377,11 +390,12 @@ where
+ Mul<Expr, Output = Expr>
+ Send
+ Sync,
PubVar: Into<Expr> + Copy,
{
}

impl<'a, F, EF, Var, Expr> AirBuilderWithPublicValues
for GenericVerifierConstraintFolder<'a, F, EF, Var, Expr>
impl<'a, F, EF, PubVar, Var, Expr> AirBuilderWithPublicValues
for GenericVerifierConstraintFolder<'a, F, EF, PubVar, Var, Expr>
where
F: Field,
EF: ExtensionField<F>,
Expand All @@ -407,8 +421,9 @@ where
+ Mul<Expr, Output = Expr>
+ Send
+ Sync,
PubVar: Into<Expr> + Copy,
{
type PublicVar = Self::F;
type PublicVar = PubVar;

fn public_values(&self) -> &[Self::PublicVar] {
self.public_values
Expand Down
13 changes: 6 additions & 7 deletions recursion/circuit/src/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use sp1_recursion_compiler::ir::Felt;
use sp1_recursion_compiler::ir::{Builder, Config, Ext};
use sp1_recursion_compiler::prelude::SymbolicExt;
use sp1_recursion_program::commit::PolynomialSpaceVariable;
use sp1_recursion_program::folder::RecursiveVerifierConstraintFolder;

use sp1_recursion_program::stark::RecursiveVerifierConstraintFolder;

use crate::domain::TwoAdicMultiplicativeCosetVariable;
use crate::stark::StarkVerifierCircuit;
Expand Down Expand Up @@ -57,15 +58,12 @@ where
next: unflatten(&opening.permutation.next),
};

let zero: Ext<SC::Val, SC::Challenge> = builder.eval(SC::Val::zero());

let mut folder_pv = Vec::new();
for i in 0..PROOF_MAX_NUM_PVS {
folder_pv.push(builder.get(&public_values, i));
}

let mut folder = RecursiveVerifierConstraintFolder {
builder,
let mut folder = RecursiveVerifierConstraintFolder::<C> {
preprocessed: opening.preprocessed.view(),
main: opening.main.view(),
perm: perm_opening.view(),
Expand All @@ -76,11 +74,12 @@ where
is_last_row: selectors.is_last_row,
is_transition: selectors.is_transition,
alpha,
accumulator: zero,
accumulator: SymbolicExt::zero(),
_marker: std::marker::PhantomData,
};

chip.eval(&mut folder);
folder.accumulator
builder.eval(folder.accumulator)
}

fn recompute_quotient(
Expand Down
2 changes: 1 addition & 1 deletion recursion/circuit/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use sp1_recursion_compiler::prelude::SymbolicVar;
use sp1_recursion_core::stark::config::{outer_fri_config, BabyBearPoseidon2Outer};
use sp1_recursion_core::stark::RecursionAir;
use sp1_recursion_program::commit::PolynomialSpaceVariable;
use sp1_recursion_program::folder::RecursiveVerifierConstraintFolder;
use sp1_recursion_program::stark::RecursiveVerifierConstraintFolder;

use crate::domain::{new_coset, TwoAdicMultiplicativeCosetVariable};
use crate::types::TwoAdicPcsMatsVariable;
Expand Down
4 changes: 4 additions & 0 deletions recursion/compiler/src/asm/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ impl<F: PrimeField32, EF: ExtensionField<F>> AssemblyCode<F, EF> {
Self { blocks, labels }
}

pub fn size(&self) -> usize {
self.blocks.iter().map(|block| block.0.len()).sum()
}

/// Convert the assembly code to a program.
pub fn machine_code(self) -> RecursionProgram<F> {
let blocks = self.blocks;
Expand Down
1 change: 1 addition & 0 deletions recursion/compiler/src/asm/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ impl<F: PrimeField32 + TwoAdicField, EF: ExtensionField<F> + TwoAdicField> AsmCo

pub fn compile(self) -> RecursionProgram<F> {
let code = self.code();
tracing::info!("Program size: {}", code.size());
code.machine_code()
}

Expand Down
12 changes: 6 additions & 6 deletions recursion/program/src/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use sp1_recursion_compiler::prelude::ExtConst;
use sp1_recursion_compiler::prelude::{Builder, Ext, SymbolicExt};

use crate::commit::PolynomialSpaceVariable;
use crate::folder::RecursiveVerifierConstraintFolder;
use crate::fri::TwoAdicMultiplicativeCosetVariable;
use crate::stark::RecursiveVerifierConstraintFolder;
use crate::stark::StarkVerifier;
use crate::types::ChipOpenedValuesVariable;
use crate::types::ChipOpening;
Expand Down Expand Up @@ -55,15 +55,14 @@ where
next: unflatten(&opening.permutation.next),
};

let zero: Ext<SC::Val, SC::Challenge> = builder.eval(SC::Val::zero());
// let zero: Ext<SC::Val, SC::Challenge> = builder.eval(SC::Val::zero());

let mut folder_pv = Vec::new();
for i in 0..PROOF_MAX_NUM_PVS {
folder_pv.push(builder.get(&public_values, i));
}

let mut folder = RecursiveVerifierConstraintFolder {
builder,
let mut folder = RecursiveVerifierConstraintFolder::<C> {
preprocessed: opening.preprocessed.view(),
main: opening.main.view(),
perm: perm_opening.view(),
Expand All @@ -74,11 +73,12 @@ where
is_last_row: selectors.is_last_row,
is_transition: selectors.is_transition,
alpha,
accumulator: zero,
accumulator: SymbolicExt::zero(),
_marker: std::marker::PhantomData,
};

chip.eval(&mut folder);
folder.accumulator
builder.eval(folder.accumulator)
}

fn recompute_quotient(
Expand Down
121 changes: 0 additions & 121 deletions recursion/program/src/folder.rs

This file was deleted.

1 change: 0 additions & 1 deletion recursion/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
pub mod challenger;
pub mod commit;
pub mod constraints;
pub mod folder;
pub mod fri;
pub mod hints;
pub mod reduce;
Expand Down
Loading

0 comments on commit d6d5560

Please sign in to comment.