Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
move all changes to uni-stark
Browse files Browse the repository at this point in the history
  • Loading branch information
Schaeff committed Sep 12, 2024
1 parent 9e5618e commit a9e7c1b
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 81 deletions.
37 changes: 1 addition & 36 deletions air/src/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,10 @@ pub trait BaseAir<F>: Sync {
None
}

fn stage_count(&self) -> usize {
1
}

/// The number of preprocessed columns in this AIR
fn preprocessed_width(&self) -> usize {
0
}

/// The number of columns in a given higher-stage trace.
fn multi_stage_width(&self, stage: u32) -> usize {
match stage {
0 => self.width(),
_ => unimplemented!(),
}
}

/// The number of challenges produced at the end of each stage
fn challenge_count(&self, _stage: u32) -> usize {
0
}
}

/// An AIR that works with a particular `AirBuilder`.
Expand Down Expand Up @@ -135,16 +118,7 @@ pub trait AirBuilder: Sized {
pub trait AirBuilderWithPublicValues: AirBuilder {
type PublicVar: Into<Self::Expr> + Copy;

fn stage_public_values(&self, stage: usize) -> &[Self::PublicVar] {
match stage {
0 => self.public_values(),
_ => unimplemented!(),
}
}

fn public_values(&self) -> &[Self::PublicVar] {
self.stage_public_values(0)
}
fn public_values(&self) -> &[Self::PublicVar];
}

pub trait PairBuilder: AirBuilder {
Expand Down Expand Up @@ -188,15 +162,6 @@ pub trait PermutationAirBuilder: ExtensionBuilder {
fn permutation_randomness(&self) -> &[Self::RandomVar];
}

pub trait MultistageAirBuilder: AirBuilder {
type Challenge: Clone + Into<Self::Expr>;

/// Traces from each stage.
fn multi_stage(&self, stage: usize) -> Self::M;

/// Challenges from each stage, drawn from the base field
fn challenges(&self, stage: usize) -> &[Self::Challenge];
}
#[derive(Debug)]
pub struct FilteredAirBuilder<'a, AB: AirBuilder> {
pub inner: &'a mut AB,
Expand Down
12 changes: 9 additions & 3 deletions uni-stark/src/check_constraints.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use alloc::vec::Vec;

use itertools::Itertools;
use p3_air::{Air, AirBuilder, AirBuilderWithPublicValues, MultistageAirBuilder, PairBuilder};
use p3_air::{Air, AirBuilder, AirBuilderWithPublicValues, PairBuilder};
use p3_field::Field;
use p3_matrix::dense::{RowMajorMatrix, RowMajorMatrixView};
use p3_matrix::stack::VerticalPair;
use p3_matrix::Matrix;
use tracing::instrument;

use crate::traits::MultistageAirBuilder;

#[instrument(name = "check constraints", skip_all)]
pub(crate) fn check_constraints<F, A>(
air: &A,
Expand Down Expand Up @@ -131,8 +133,8 @@ where
impl<'a, F: Field> AirBuilderWithPublicValues for DebugConstraintBuilder<'a, F> {
type PublicVar = Self::F;

fn stage_public_values(&self, stage: usize) -> &[Self::F] {
self.public_values[stage]
fn public_values(&self) -> &[Self::PublicVar] {
self.stage_public_values(0)
}
}

Expand All @@ -145,6 +147,10 @@ impl<'a, F: Field> PairBuilder for DebugConstraintBuilder<'a, F> {
impl<'a, F: Field> MultistageAirBuilder for DebugConstraintBuilder<'a, F> {
type Challenge = Self::Expr;

fn stage_public_values(&self, stage: usize) -> &[Self::F] {
self.public_values[stage]
}

fn multi_stage(&self, stage: usize) -> Self::M {
self.stages[stage]
}
Expand Down
49 changes: 28 additions & 21 deletions uni-stark/src/folder.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use alloc::vec::Vec;

use p3_air::{AirBuilder, AirBuilderWithPublicValues, MultistageAirBuilder, PairBuilder};
use p3_air::{AirBuilder, AirBuilderWithPublicValues, PairBuilder};
use p3_field::AbstractField;
use p3_matrix::dense::{RowMajorMatrix, RowMajorMatrixView};
use p3_matrix::stack::VerticalPair;

use crate::traits::MultistageAirBuilder;
use crate::{PackedChallenge, PackedVal, StarkGenericConfig, Val};

#[derive(Debug)]
Expand Down Expand Up @@ -69,29 +70,32 @@ impl<'a, SC: StarkGenericConfig> AirBuilder for ProverConstraintFolder<'a, SC> {
}

impl<'a, SC: StarkGenericConfig> AirBuilderWithPublicValues for ProverConstraintFolder<'a, SC> {
type PublicVar = Self::F;
type PublicVar = Val<SC>;

fn stage_public_values(&self, stage: usize) -> &[Self::F] {
&self.public_values[stage]
}
}

impl<'a, SC: StarkGenericConfig> PairBuilder for ProverConstraintFolder<'a, SC> {
fn preprocessed(&self) -> Self::M {
self.preprocessed.clone()
fn public_values(&self) -> &[Self::PublicVar] {
self.stage_public_values(0)
}
}

impl<'a, SC: StarkGenericConfig> MultistageAirBuilder for ProverConstraintFolder<'a, SC> {
type Challenge = Val<SC>;

fn multi_stage(&self, stage: usize) -> Self::M {
fn multi_stage(&self, stage: usize) -> <Self as AirBuilder>::M {
self.stages[stage].clone()
}

fn challenges(&self, stage: usize) -> &[Self::Challenge] {
&self.challenges[stage]
}
fn stage_public_values(&self, stage: usize) -> &[Self::PublicVar] {
&self.public_values[stage]
}
}

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

impl<'a, SC: StarkGenericConfig> AirBuilder for VerifierConstraintFolder<'a, SC> {
Expand Down Expand Up @@ -128,27 +132,30 @@ impl<'a, SC: StarkGenericConfig> AirBuilder for VerifierConstraintFolder<'a, SC>
}

impl<'a, SC: StarkGenericConfig> AirBuilderWithPublicValues for VerifierConstraintFolder<'a, SC> {
type PublicVar = Self::F;
type PublicVar = Val<SC>;

fn stage_public_values(&self, stage: usize) -> &[Self::F] {
self.public_values[stage]
}
}

impl<'a, SC: StarkGenericConfig> PairBuilder for VerifierConstraintFolder<'a, SC> {
fn preprocessed(&self) -> Self::M {
self.preprocessed
fn public_values(&self) -> &[Self::PublicVar] {
self.stage_public_values(0)
}
}

impl<'a, SC: StarkGenericConfig> MultistageAirBuilder for VerifierConstraintFolder<'a, SC> {
type Challenge = Val<SC>;

fn multi_stage(&self, stage: usize) -> Self::M {
fn multi_stage(&self, stage: usize) -> <Self as AirBuilder>::M {
self.stages[stage]
}

fn challenges(&self, stage: usize) -> &[Self::Challenge] {
&self.challenges[stage]
}
fn stage_public_values(&self, stage: usize) -> &[Self::PublicVar] {
self.public_values[stage]
}
}

impl<'a, SC: StarkGenericConfig> PairBuilder for VerifierConstraintFolder<'a, SC> {
fn preprocessed(&self) -> Self::M {
self.preprocessed
}
}
2 changes: 2 additions & 0 deletions uni-stark/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod prover;
mod symbolic_builder;
mod symbolic_expression;
mod symbolic_variable;
mod traits;
mod verifier;
mod zerofier_coset;

Expand All @@ -26,5 +27,6 @@ pub use prover::*;
pub use symbolic_builder::*;
pub use symbolic_expression::*;
pub use symbolic_variable::*;
pub use traits::*;
pub use verifier::*;
pub use zerofier_coset::*;
19 changes: 13 additions & 6 deletions uni-stark/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use p3_util::log2_strict_usize;
use tracing::instrument;

use crate::symbolic_builder::{get_log_quotient_degree, SymbolicAirBuilder};
use crate::traits::MultiStageAir;
use crate::{
CallbackResult, Commitments, NextStageTraceCallback, PackedChallenge, PackedVal, Proof,
ProverConstraintFolder, QuotientInputs, Stage, StarkGenericConfig, StarkProvingKey, State, Val,
Expand Down Expand Up @@ -43,7 +44,8 @@ pub fn prove<
) -> Proof<SC>
where
SC: StarkGenericConfig,
A: Air<SymbolicAirBuilder<Val<SC>>> + for<'a> Air<ProverConstraintFolder<'a, SC>>,
A: MultiStageAir<SymbolicAirBuilder<Val<SC>>>
+ for<'a> MultiStageAir<ProverConstraintFolder<'a, SC>>,
{
prove_with_key::<_, _, Panic>(
config,
Expand Down Expand Up @@ -74,12 +76,13 @@ pub fn prove_with_key<
) -> Proof<SC>
where
SC: StarkGenericConfig,
A: Air<SymbolicAirBuilder<Val<SC>>> + for<'a> Air<ProverConstraintFolder<'a, SC>>,
A: MultiStageAir<SymbolicAirBuilder<Val<SC>>>
+ for<'a> MultiStageAir<ProverConstraintFolder<'a, SC>>,
T: NextStageTraceCallback<SC>,
{
let degree = stage_0_trace.height();
let log_degree = log2_strict_usize(degree);
let stage_count = air.stage_count();
let stage_count = <A as MultiStageAir<SymbolicAirBuilder<_>>>::stage_count(air);

let pcs = config.pcs();
let trace_domain = pcs.natural_domain_for_degree(degree);
Expand All @@ -95,7 +98,7 @@ where
let mut state = State::new(pcs, trace_domain, challenger, log_degree);
let mut stage = Stage {
trace: stage_0_trace,
challenge_count: air.challenge_count(0),
challenge_count: <A as MultiStageAir<SymbolicAirBuilder<_>>>::challenge_count(air, 0),
public_values: stage_0_public_values.to_owned(),
};

Expand All @@ -118,7 +121,10 @@ where
// go to the next stage
stage = Stage {
trace,
challenge_count: air.challenge_count(stage_id as u32),
challenge_count: <A as MultiStageAir<SymbolicAirBuilder<_>>>::challenge_count(
air,
stage_id as u32,
),
public_values,
};
}
Expand Down Expand Up @@ -169,7 +175,8 @@ pub fn finish<
) -> Proof<SC>
where
SC: StarkGenericConfig,
A: Air<SymbolicAirBuilder<Val<SC>>> + for<'a> Air<ProverConstraintFolder<'a, SC>>,
A: MultiStageAir<SymbolicAirBuilder<Val<SC>>>
+ for<'a> MultiStageAir<ProverConstraintFolder<'a, SC>>,
{
let log_quotient_degree = get_log_quotient_degree::<Val<SC>, A>(
air,
Expand Down
28 changes: 17 additions & 11 deletions uni-stark/src/symbolic_builder.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use alloc::vec;
use alloc::vec::Vec;

use p3_air::{Air, AirBuilder, AirBuilderWithPublicValues, MultistageAirBuilder, PairBuilder};
use p3_air::{AirBuilder, AirBuilderWithPublicValues, PairBuilder};
use p3_field::Field;
use p3_matrix::dense::RowMajorMatrix;
use p3_util::log2_ceil_usize;
use tracing::instrument;

use crate::symbolic_expression::SymbolicExpression;
use crate::symbolic_variable::SymbolicVariable;
use crate::traits::{MultiStageAir, MultistageAirBuilder};
use crate::Entry;

#[instrument(name = "infer log of constraint degree", skip_all)]
pub fn get_log_quotient_degree<F, A>(air: &A, public_values_counts: &[usize]) -> usize
where
F: Field,
A: Air<SymbolicAirBuilder<F>>,
A: MultiStageAir<SymbolicAirBuilder<F>>,
{
// We pad to at least degree 2, since a quotient argument doesn't make sense with smaller degrees.
let constraint_degree = get_max_constraint_degree(air, public_values_counts).max(2);
Expand All @@ -30,7 +31,7 @@ where
pub fn get_max_constraint_degree<F, A>(air: &A, public_values_counts: &[usize]) -> usize
where
F: Field,
A: Air<SymbolicAirBuilder<F>>,
A: MultiStageAir<SymbolicAirBuilder<F>>,
{
get_symbolic_constraints(air, public_values_counts)
.iter()
Expand All @@ -46,7 +47,7 @@ pub fn get_symbolic_constraints<F, A>(
) -> Vec<SymbolicExpression<F>>
where
F: Field,
A: Air<SymbolicAirBuilder<F>>,
A: MultiStageAir<SymbolicAirBuilder<F>>,
{
let widths: Vec<_> = (0..air.stage_count())
.map(|i| air.multi_stage_width(i as u32))
Expand Down Expand Up @@ -174,20 +175,19 @@ impl<F: Field> AirBuilder for SymbolicAirBuilder<F> {

impl<F: Field> AirBuilderWithPublicValues for SymbolicAirBuilder<F> {
type PublicVar = SymbolicVariable<F>;
fn stage_public_values(&self, stage: usize) -> &[Self::PublicVar] {
&self.public_values[stage]
}
}

impl<F: Field> PairBuilder for SymbolicAirBuilder<F> {
fn preprocessed(&self) -> Self::M {
self.preprocessed.clone()
fn public_values(&self) -> &[Self::PublicVar] {
self.stage_public_values(0)
}
}

impl<F: Field> MultistageAirBuilder for SymbolicAirBuilder<F> {
type Challenge = Self::Var;

fn stage_public_values(&self, stage: usize) -> &[Self::PublicVar] {
&self.public_values[stage]
}

fn multi_stage(&self, stage: usize) -> Self::M {
self.stages[stage].clone()
}
Expand All @@ -196,3 +196,9 @@ impl<F: Field> MultistageAirBuilder for SymbolicAirBuilder<F> {
&self.challenges[stage]
}
}

impl<F: Field> PairBuilder for SymbolicAirBuilder<F> {
fn preprocessed(&self) -> Self::M {
self.preprocessed.clone()
}
}
Loading

0 comments on commit a9e7c1b

Please sign in to comment.