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

Uni-Stark Challenges #2

Merged
merged 49 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
0533bf8
wip
topanisto Aug 8, 2024
3ebc6a8
new struct for callbacks, nothing is working yet
topanisto Aug 8, 2024
394b46e
small changes
topanisto Aug 8, 2024
4cab56d
wip
topanisto Aug 8, 2024
9c5191f
quotient wip
topanisto Aug 8, 2024
54ccb0b
add new structs, wip
topanisto Aug 10, 2024
9fcd1da
methods on structs
topanisto Aug 11, 2024
6edfa20
cleaning up methods
topanisto Aug 11, 2024
1b41c7a
small changes
topanisto Aug 12, 2024
adb36e7
moving run_stage into methods for state
topanisto Aug 13, 2024
6690743
cleaning up comments
topanisto Aug 13, 2024
06c6e51
state methods and added verifier compatibility
topanisto Aug 28, 2024
1ef7045
broken verifier
topanisto Aug 29, 2024
818cd27
broken verifier
topanisto Aug 29, 2024
25713bf
fixed verifier
topanisto Aug 29, 2024
cbdd1d2
verifier correct
topanisto Aug 29, 2024
65e4399
check constraints
topanisto Aug 29, 2024
df5b74e
check constraints
topanisto Aug 29, 2024
348e2a1
challenge array wip
topanisto Aug 29, 2024
72c6eb8
cleaning up
topanisto Aug 30, 2024
67fd48b
make compile
Schaeff Aug 30, 2024
c1866fc
refactor
Schaeff Sep 5, 2024
a1bd511
extend symbolic_builder
Schaeff Sep 5, 2024
4780d7c
debug
Schaeff Sep 5, 2024
1f45d2a
wip
Schaeff Sep 5, 2024
af85a5b
refactor multistage proving
Schaeff Sep 6, 2024
19ae3c9
fix
Schaeff Sep 6, 2024
bcd8003
access challenges
Schaeff Sep 6, 2024
b89e07c
fix multi stage length
Schaeff Sep 10, 2024
cc341b8
implement multi-stage publics
Schaeff Sep 10, 2024
a4200c8
constructor
Schaeff Sep 10, 2024
80b1adc
check constraints, fix challenge values
Schaeff Sep 11, 2024
8f48ac2
clean
Schaeff Sep 11, 2024
10589f9
Merge branch 'main' of github.com:Schaeff/Plonky3 into uni-stark-chal…
Schaeff Sep 11, 2024
9e5618e
apply suggestion from @topanisto
Schaeff Sep 12, 2024
a9e7c1b
move all changes to uni-stark
Schaeff Sep 12, 2024
0e13d88
commit traits
Schaeff Sep 12, 2024
16d55a3
revert more changes
Schaeff Sep 12, 2024
1a3b276
remove auto impl
Schaeff Sep 12, 2024
867a317
clippy
Schaeff Sep 12, 2024
d9a0a32
reduce diff
Schaeff Sep 13, 2024
6577f2e
clean
Schaeff Sep 13, 2024
3c19588
remove
Schaeff Sep 13, 2024
5055807
allow too many arguments
Schaeff Sep 13, 2024
14b84d0
fix clippy and keep same API
Schaeff Sep 16, 2024
cc1bc7d
revert
Schaeff Sep 16, 2024
253e793
address review comments
Schaeff Sep 16, 2024
84f3d7f
fix
Schaeff Sep 16, 2024
cf80bf2
rename
Schaeff Sep 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion keccak-air/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ p3-air = { path = "../air" }
p3-field = { path = "../field" }
p3-matrix = { path = "../matrix" }
p3-maybe-rayon = { path = "../maybe-rayon" }
p3-uni-stark = { path = "../uni-stark" }
p3-util = { path = "../util" }
tracing = "0.1.37"

Expand All @@ -29,7 +30,6 @@ p3-poseidon = { path = "../poseidon" }
p3-poseidon2 = { path = "../poseidon2" }
p3-sha256 = { path = "../sha256", features = ["asm"] }
p3-symmetric = { path = "../symmetric" }
p3-uni-stark = { path = "../uni-stark" }
rand = "0.8.5"
tracing-subscriber = { version = "0.3.17", features = ["std", "env-filter"] }
tracing-forest = { version = "0.1.6", features = ["ansi", "smallvec"] }
Expand Down
3 changes: 3 additions & 0 deletions keccak-air/src/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use core::borrow::Borrow;
use p3_air::{Air, AirBuilder, BaseAir};
use p3_field::AbstractField;
use p3_matrix::Matrix;
use p3_uni_stark::MultiStageAir;

use crate::columns::{KeccakCols, NUM_KECCAK_COLS};
use crate::constants::rc_value_bit;
Expand All @@ -20,6 +21,8 @@ impl<F> BaseAir<F> for KeccakAir {
}
}

impl<AB: AirBuilder> MultiStageAir<AB> for KeccakAir {}
georgwiese marked this conversation as resolved.
Show resolved Hide resolved

impl<AB: AirBuilder> Air<AB> for KeccakAir {
#[inline]
fn eval(&self, builder: &mut AB) {
Expand Down
64 changes: 49 additions & 15 deletions uni-stark/src/check_constraints.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
use alloc::vec::Vec;

use itertools::Itertools;
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,
preprocessed: &RowMajorMatrix<F>,
main: &RowMajorMatrix<F>,
public_values: &Vec<F>,
stages: Vec<&RowMajorMatrix<F>>,
public_values: &Vec<&Vec<F>>,
challenges: Vec<&Vec<F>>,
) where
F: Field,
A: for<'a> Air<DebugConstraintBuilder<'a, F>>,
{
let height = main.height();
let num_stages = stages.len();
let height = stages[0].height();

(0..height).for_each(|i| {
let i_next = (i + 1) % height;
Expand All @@ -29,17 +34,29 @@ pub(crate) fn check_constraints<F, A>(
RowMajorMatrixView::new_row(&*next_preprocessed),
);

let local = main.row_slice(i);
let next = main.row_slice(i_next);
let main = VerticalPair::new(
RowMajorMatrixView::new_row(&*local),
RowMajorMatrixView::new_row(&*next),
);
let stages_local_next = stages
.iter()
.map(|trace| {
let stage_local = trace.row_slice(i);
let stage_next = trace.row_slice(i_next);
(stage_local, stage_next)
})
.collect_vec();

let stages = (0..num_stages)
.map(|stage| {
VerticalPair::new(
RowMajorMatrixView::new_row(&*stages_local_next[stage].0),
RowMajorMatrixView::new_row(&*stages_local_next[stage].1),
)
})
.collect();

let mut builder = DebugConstraintBuilder {
row_index: i,
challenges: challenges.clone(),
preprocessed,
main,
stages,
public_values,
is_first_row: F::from_bool(i == 0),
is_last_row: F::from_bool(i == height - 1),
Expand All @@ -56,8 +73,9 @@ pub(crate) fn check_constraints<F, A>(
pub struct DebugConstraintBuilder<'a, F: Field> {
row_index: usize,
preprocessed: VerticalPair<RowMajorMatrixView<'a, F>, RowMajorMatrixView<'a, F>>,
main: VerticalPair<RowMajorMatrixView<'a, F>, RowMajorMatrixView<'a, F>>,
public_values: &'a [F],
challenges: Vec<&'a Vec<F>>,
stages: Vec<VerticalPair<RowMajorMatrixView<'a, F>, RowMajorMatrixView<'a, F>>>,
public_values: &'a [&'a Vec<F>],
is_first_row: F,
is_last_row: F,
is_transition: F,
Expand Down Expand Up @@ -89,7 +107,7 @@ where
}

fn main(&self) -> Self::M {
self.main
self.stages[0]
}

fn assert_zero<I: Into<Self::Expr>>(&mut self, x: I) {
Expand All @@ -115,8 +133,8 @@ where
impl<'a, F: Field> AirBuilderWithPublicValues for DebugConstraintBuilder<'a, F> {
type PublicVar = Self::F;

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

Expand All @@ -125,3 +143,19 @@ impl<'a, F: Field> PairBuilder for DebugConstraintBuilder<'a, F> {
self.preprocessed
}
}

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 stage_trace(&self, stage: usize) -> Self::M {
self.stages[stage]
}

fn stage_challenges(&self, stage: usize) -> &[Self::Expr] {
self.challenges[stage]
}
}
57 changes: 45 additions & 12 deletions uni-stark/src/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ 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)]
pub struct ProverConstraintFolder<'a, SC: StarkGenericConfig> {
pub main: RowMajorMatrix<PackedVal<SC>>,
pub challenges: Vec<Vec<Val<SC>>>,
pub stages: Vec<RowMajorMatrix<PackedVal<SC>>>,
pub preprocessed: RowMajorMatrix<PackedVal<SC>>,
pub public_values: &'a Vec<Val<SC>>,
pub public_values: &'a Vec<Vec<Val<SC>>>,
pub is_first_row: PackedVal<SC>,
pub is_last_row: PackedVal<SC>,
pub is_transition: PackedVal<SC>,
Expand All @@ -23,9 +25,10 @@ type ViewPair<'a, T> = VerticalPair<RowMajorMatrixView<'a, T>, RowMajorMatrixVie

#[derive(Debug)]
pub struct VerifierConstraintFolder<'a, SC: StarkGenericConfig> {
pub main: ViewPair<'a, SC::Challenge>,
pub challenges: Vec<Vec<Val<SC>>>,
pub stages: Vec<ViewPair<'a, SC::Challenge>>,
pub preprocessed: ViewPair<'a, SC::Challenge>,
pub public_values: &'a Vec<Val<SC>>,
pub public_values: Vec<&'a Vec<Val<SC>>>,
pub is_first_row: SC::Challenge,
pub is_last_row: SC::Challenge,
pub is_transition: SC::Challenge,
Expand All @@ -40,7 +43,7 @@ impl<'a, SC: StarkGenericConfig> AirBuilder for ProverConstraintFolder<'a, SC> {
type M = RowMajorMatrix<PackedVal<SC>>;

fn main(&self) -> Self::M {
self.main.clone()
self.stages[0].clone()
}

fn is_first_row(&self) -> Self::Expr {
Expand All @@ -67,10 +70,25 @@ 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 public_values(&self) -> &[Self::F] {
self.public_values
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 stage_trace(&self, stage: usize) -> <Self as AirBuilder>::M {
self.stages[stage].clone()
}

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

Expand All @@ -87,7 +105,7 @@ impl<'a, SC: StarkGenericConfig> AirBuilder for VerifierConstraintFolder<'a, SC>
type M = ViewPair<'a, SC::Challenge>;

fn main(&self) -> Self::M {
self.main
self.stages[0]
}

fn is_first_row(&self) -> Self::Expr {
Expand All @@ -114,10 +132,25 @@ 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 public_values(&self) -> &[Self::F] {
self.public_values
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 stage_trace(&self, stage: usize) -> <Self as AirBuilder>::M {
self.stages[stage]
}

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

Expand Down
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: 15 additions & 4 deletions uni-stark/src/proof.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use alloc::vec::Vec;

use p3_commit::Pcs;
use p3_matrix::dense::RowMajorMatrix;
use serde::{Deserialize, Serialize};

use crate::StarkGenericConfig;
use crate::{StarkGenericConfig, Val};

type Com<SC> = <<SC as StarkGenericConfig>::Pcs as Pcs<
<SC as StarkGenericConfig>::Challenge,
Expand All @@ -25,20 +26,21 @@ pub struct Proof<SC: StarkGenericConfig> {
pub(crate) opened_values: OpenedValues<SC::Challenge>,
pub(crate) opening_proof: PcsProof<SC>,
pub(crate) degree_bits: usize,
pub(crate) challenge_counts: Vec<usize>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Commitments<Com> {
pub(crate) trace: Com,
pub(crate) stages: Vec<Com>,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub(crate) stages: Vec<Com>,
pub(crate) traces_by_stage: Vec<Com>,

pub(crate) quotient_chunks: Com,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct OpenedValues<Challenge> {
pub(crate) preprocessed_local: Vec<Challenge>,
pub(crate) preprocessed_next: Vec<Challenge>,
pub(crate) trace_local: Vec<Challenge>,
pub(crate) trace_next: Vec<Challenge>,
pub(crate) stages_local: Vec<Vec<Challenge>>,
pub(crate) stages_next: Vec<Vec<Challenge>>,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, again, I would still include "trace" in the name. traces_by_stage_local?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that's more a general comment: What's a stage? To me it's just a number, for anything else I think it would be better to give a more specific name.

pub(crate) quotient_chunks: Vec<Vec<Challenge>>,
}

Expand All @@ -52,3 +54,12 @@ pub struct StarkProvingKey<SC: StarkGenericConfig> {
pub struct StarkVerifyingKey<SC: StarkGenericConfig> {
pub preprocessed_commit: Com<SC>,
}

pub struct ProcessedStage<SC: StarkGenericConfig> {
georgwiese marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) commitment: Com<SC>,
pub(crate) prover_data: PcsProverData<SC>,
pub(crate) challenge_values: Vec<Val<SC>>,
pub(crate) public_values: Vec<Val<SC>>,
#[cfg(debug_assertions)]
pub(crate) trace: RowMajorMatrix<Val<SC>>,
}
Loading
Loading