Skip to content

Commit

Permalink
Add state machine AIR
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmilson committed Dec 16, 2024
1 parent 1fc25f3 commit 6187464
Show file tree
Hide file tree
Showing 65 changed files with 59,160 additions and 130 deletions.
4 changes: 3 additions & 1 deletion stwo_cairo_prover/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion stwo_cairo_prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ serde = "1.0.207"
serde_json = "1.0.1"
sonic-rs = "0.3.10"
# TODO(ShaharS): take stwo version from the source repository.
stwo-prover = { git = "https://github.com/starkware-libs/stwo", rev = "264922e" }
stwo-prover = { git = "https://github.com/starkware-libs/stwo", rev = "af9250" }
thiserror = "1.0.63"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
Expand Down
2 changes: 2 additions & 0 deletions stwo_cairo_prover/crates/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ edition = "2021"
[dependencies]
env_logger.workspace = true
log.workspace = true
starknet-ff.workspace = true
stwo-prover.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
1 change: 1 addition & 0 deletions stwo_cairo_prover/crates/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod logging_utils;
pub mod serialise;
170 changes: 170 additions & 0 deletions stwo_cairo_prover/crates/utils/src/serialise.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
use starknet_ff::FieldElement;
use stwo_prover::core::fields::m31::BaseField;
use stwo_prover::core::fields::qm31::SecureField;
use stwo_prover::core::fri::{FriLayerProof, FriProof};
use stwo_prover::core::pcs::CommitmentSchemeProof;
use stwo_prover::core::poly::line::LinePoly;
use stwo_prover::core::prover::StarkProof;
use stwo_prover::core::vcs::ops::MerkleHasher;
use stwo_prover::core::vcs::prover::MerkleDecommitment;

pub trait CairoSerialize {
fn serialize(&self, output: &mut Vec<FieldElement>);
}

impl CairoSerialize for u32 {
fn serialize(&self, output: &mut Vec<FieldElement>) {
output.push((*self).into());
}
}

impl CairoSerialize for u64 {
fn serialize(&self, output: &mut Vec<FieldElement>) {
output.push((*self).into());
}
}

impl CairoSerialize for usize {
fn serialize(&self, output: &mut Vec<FieldElement>) {
output.push((*self).into());
}
}

impl CairoSerialize for BaseField {
fn serialize(&self, output: &mut Vec<FieldElement>) {
output.push(self.0.into());
}
}

impl CairoSerialize for SecureField {
fn serialize(&self, output: &mut Vec<FieldElement>) {
output.extend(self.to_m31_array().map(|c| FieldElement::from(c.0)));
}
}

impl<H: MerkleHasher> CairoSerialize for MerkleDecommitment<H>
where
H::Hash: CairoSerialize,
{
fn serialize(&self, output: &mut Vec<FieldElement>) {
let Self {
hash_witness,
column_witness,
} = self;
hash_witness.serialize(output);
column_witness.serialize(output);
}
}

impl CairoSerialize for LinePoly {
fn serialize(&self, output: &mut Vec<FieldElement>) {
(**self).serialize(output);
output.push((self.len().ilog2()).into());
}
}

impl<H: MerkleHasher> CairoSerialize for FriLayerProof<H>
where
H::Hash: CairoSerialize,
{
fn serialize(&self, output: &mut Vec<FieldElement>) {
let Self {
fri_witness,
decommitment,
commitment,
} = self;
fri_witness.serialize(output);
decommitment.serialize(output);
commitment.serialize(output);
}
}

impl<H: MerkleHasher> CairoSerialize for FriProof<H>
where
H::Hash: CairoSerialize,
{
fn serialize(&self, output: &mut Vec<FieldElement>) {
let Self {
first_layer,
inner_layers,
last_layer_poly,
} = self;
first_layer.serialize(output);
inner_layers.serialize(output);
last_layer_poly.serialize(output);
}
}

impl CairoSerialize for FieldElement {
fn serialize(&self, output: &mut Vec<FieldElement>) {
output.push(*self);
}
}

impl<H: MerkleHasher> CairoSerialize for CommitmentSchemeProof<H>
where
H::Hash: CairoSerialize,
{
fn serialize(&self, output: &mut Vec<FieldElement>) {
let Self {
commitments,
sampled_values,
decommitments,
queried_values,
proof_of_work,
fri_proof,
} = self;
commitments.serialize(output);
sampled_values.serialize(output);
decommitments.serialize(output);
queried_values.serialize(output);
output.push((*proof_of_work).into());
fri_proof.serialize(output);
}
}

impl<H: MerkleHasher> CairoSerialize for StarkProof<H>
where
H::Hash: CairoSerialize,
{
fn serialize(&self, output: &mut Vec<FieldElement>) {
let Self(commitment_scheme_proof) = self;
commitment_scheme_proof.serialize(output);
}
}

impl<T: CairoSerialize> CairoSerialize for [T] {
fn serialize(&self, output: &mut Vec<FieldElement>) {
output.push(self.len().into());
self.iter().for_each(|v| v.serialize(output));
}
}

impl<T: CairoSerialize, const N: usize> CairoSerialize for [T; N] {
fn serialize(&self, output: &mut Vec<FieldElement>) {
self.iter().for_each(|v| v.serialize(output));
}
}

impl<T: CairoSerialize> CairoSerialize for Vec<T> {
fn serialize(&self, output: &mut Vec<FieldElement>) {
(**self).serialize(output);
}
}

impl<T0: CairoSerialize, T1: CairoSerialize> CairoSerialize for (T0, T1) {
fn serialize(&self, output: &mut Vec<FieldElement>) {
let (v0, v1) = self;
v0.serialize(output);
v1.serialize(output);
}
}

impl<T0: CairoSerialize, T1: CairoSerialize, T2: CairoSerialize> CairoSerialize for (T0, T1, T2) {
fn serialize(&self, output: &mut Vec<FieldElement>) {
let (v0, v1, v2) = self;
v0.serialize(output);
v1.serialize(output);
v2.serialize(output);
}
}
2 changes: 1 addition & 1 deletion stwo_cairo_verifier/.cairofmtignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tests/verifier/proofs
*_proof.cairo
2 changes: 1 addition & 1 deletion stwo_cairo_verifier/.tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
scarb nightly-2024-11-19
scarb nightly-2024-12-14
starknet-foundry 0.33.0
4 changes: 2 additions & 2 deletions stwo_cairo_verifier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ Modify [`Scarb.toml`](./Scarb.toml) to use [Starknet Foundary](https://github.co

```diff
[dev-dependencies]
- cairo_test = "2.8.5"
- cairo_test = "2.9.2"
+ snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.33.0" }
+ assert_macros = "2.8.5"
+ assert_macros = "2.9.2"
+
+ [scripts]
+ test = "snforge test --max-n-steps 100000000"
Expand Down
17 changes: 16 additions & 1 deletion stwo_cairo_verifier/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,22 @@ name = "bounded_int"
version = "0.1.0"

[[package]]
name = "stwo_cairo_verifier"
name = "stwo_cairo_air"
version = "0.1.0"
dependencies = [
"stwo_constraint_framework",
"stwo_verifier_core",
]

[[package]]
name = "stwo_constraint_framework"
version = "0.1.0"
dependencies = [
"stwo_verifier_core",
]

[[package]]
name = "stwo_verifier_core"
version = "0.1.0"
dependencies = [
"bounded_int",
Expand Down
24 changes: 6 additions & 18 deletions stwo_cairo_verifier/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
[workspace]
members = ["bounded_int"]

[package]
name = "stwo_cairo_verifier"
version = "0.1.0"
edition = "2024_07"

[lib]
casm = true

[tool.fmt]
sort-module-level-items = true

[dependencies]
bounded_int = { path = "bounded_int" }

[dev-dependencies]
cairo_test = "2.8.5"
members = [
"crates/bounded_int",
"crates/cairo_air",
"crates/constraint_framework",
"crates/verifier_core",
]
11 changes: 11 additions & 0 deletions stwo_cairo_verifier/cairo_project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[crate_roots]
verifier = "src"
bounded_int = "bounded_int/src"

[config.override.verifier]
edition = "2024_07"

[config.override.verifier.dependencies.bounded_int]

[config.override.bounded_int]
edition = "2023_10"
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Utility crate that exports internal corelib function.
//! This crate is compiled with an older edition that does not enforce visibity rules.

use core::integer::upcast;
use core::internal::bounded_int::{
BoundedInt, add, constrain, div_rem, sub, AddHelper, ConstrainHelper, DivRemHelper, SubHelper,
AddHelper, BoundedInt, ConstrainHelper, DivRemHelper, SubHelper, add, constrain, div_rem, sub,
};

use core::integer::upcast;
17 changes: 17 additions & 0 deletions stwo_cairo_verifier/crates/cairo_air/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "stwo_cairo_air"
version = "0.1.0"
edition = "2024_07"

[lib]
casm = true

[tool.fmt]
sort-module-level-items = true

[dependencies]
stwo_verifier_core = { path = "../verifier_core" }
stwo_constraint_framework = { path = "../constraint_framework" }

[dev-dependencies]
cairo_test = "2.9.2"
32 changes: 32 additions & 0 deletions stwo_cairo_verifier/crates/cairo_air/src/components.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use stwo_constraint_framework::PreprocessedMaskValues;
use stwo_verifier_core::ColumnSpan;
use stwo_verifier_core::circle::CirclePoint;
use stwo_verifier_core::fields::qm31::QM31;

pub mod addr_to_id;
pub mod genericopcode;
pub mod id_to_f252;
pub mod range_check;
pub mod ret_opcode;
pub mod verify_instruction;

pub trait CairoComponent<T> {
fn mask_points(
self: @T,
ref trace_mask_points: Array<Array<CirclePoint<QM31>>>,
ref interaction_trace_mask_points: Array<Array<CirclePoint<QM31>>>,
point: CirclePoint<QM31>,
);

fn max_constraint_log_degree_bound(self: @T) -> u32;

fn evaluate_constraints_at_point(
self: @T,
ref sum: QM31,
ref preprocessed_mask_values: PreprocessedMaskValues,
ref trace_mask_values: ColumnSpan<Array<QM31>>,
ref interaction_trace_mask_values: ColumnSpan<Array<QM31>>,
random_coeff: QM31,
point: CirclePoint<QM31>,
);
}
Loading

0 comments on commit 6187464

Please sign in to comment.