Skip to content

Commit

Permalink
Merge branch 'main' into 10-01-generalize_field_in_circlepoint
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmilson authored Oct 11, 2024
2 parents 0a98ad5 + 280f614 commit 88b700e
Show file tree
Hide file tree
Showing 18 changed files with 542 additions and 363 deletions.
506 changes: 237 additions & 269 deletions stwo_cairo_prover/Cargo.lock

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion stwo_cairo_prover/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["crates/prover"]
members = ["crates/adapted_prover", "crates/prover", "crates/vm_runner"]
resolver = "2"

[workspace.package]
Expand All @@ -8,7 +8,21 @@ edition = "2021"

[workspace.dependencies]
bytemuck = { version = "1.16.3", features = ["derive"] }
cairo-lang-casm = "2.7.1"
# TODO(yuval): Use an official version, not a specific commit.
cairo-vm = { git = "https://github.com/lambdaclass/cairo-vm", rev = "3fb0344c", features = ["mod_builtin"]}
clap = { version = "4.3.10", features = ["derive"] }
hex = "0.4.3"
itertools = "0.12.0"
num-traits = "0.2.17"
rand = "0.8.5"
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 = "e04fd5b" }
thiserror = "1.0.63"


[profile.bench]
codegen-units = 1
Expand Down
12 changes: 12 additions & 0 deletions stwo_cairo_prover/crates/adapted_prover/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "adapted_stwo"
version = "0.1.0"
edition = "2021"

[dependencies]
clap.workspace = true
serde.workspace = true
serde_json.workspace = true
stwo_cairo_prover = { path = "../prover", version = "~0.1.0" }
stwo-prover.workspace = true
thiserror.workspace = true
68 changes: 68 additions & 0 deletions stwo_cairo_prover/crates/adapted_prover/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::path::PathBuf;
use std::process::ExitCode;

use clap::Parser;
use stwo_cairo_prover::cairo_air::{prove_cairo, CairoProof};
use stwo_cairo_prover::input::vm_import::{import_from_vm_output, VmImportError};
use stwo_cairo_prover::input::CairoInput;
use stwo_prover::core::prover::ProvingError;
use stwo_prover::core::vcs::blake2_merkle::Blake2sMerkleHasher;
use thiserror::Error;

/// Command line arguments for adapted_stwo.
/// Example command line:
/// ```
/// cargo run -r --bin adapted_stwo -- --pub_json absolute/path/to/pub.json
/// --priv_json absolute/path/to/priv.json --proof_path path/to/proof
/// ```
#[derive(Parser, Debug)]
struct Args {
#[structopt(long = "pub_json")]
pub_json: PathBuf,
#[structopt(long = "priv_json")]
priv_json: PathBuf,
#[structopt(long = "proof_path")]
proof_path: PathBuf,
}

#[derive(Debug, Error)]
enum Error {
#[error("Invalid arguments: {0}")]
Cli(#[from] clap::Error),
#[error("VM import failed: {0}")]
VmImport(#[from] VmImportError),
#[error("Proving failed: {0}")]
Proving(#[from] ProvingError),
#[error("Serialization failed: {0}")]
Serde(#[from] serde_json::error::Error),
#[error("IO failed: {0}")]
IO(#[from] std::io::Error),
}

fn main() -> ExitCode {
match run(std::env::args()) {
Ok(_) => {
println!("Adapted prover succeeded");
ExitCode::SUCCESS
}
Err(error) => {
println!("Adapted prover failed: {error}");
ExitCode::FAILURE
}
}
}

fn run(args: impl Iterator<Item = String>) -> Result<CairoProof<Blake2sMerkleHasher>, Error> {
let args = Args::try_parse_from(args)?;

let vm_output: CairoInput =
import_from_vm_output(args.pub_json.as_path(), args.priv_json.as_path())?;

let proof = prove_cairo(vm_output)?;

// TODO(yuval): This is just some serialization for the sake of serialization. Find the right
// way to serialize the proof.
std::fs::write(args.proof_path, serde_json::to_string(&proof)?)?;

Ok(proof)
}
56 changes: 1 addition & 55 deletions stwo_cairo_prover/crates/prover/Cargo.lock

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

21 changes: 10 additions & 11 deletions stwo_cairo_prover/crates/prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ edition = "2021"

[dependencies]
bytemuck.workspace = true
cairo-lang-casm = "2.7.1"
cairo-vm = "1.0.1"
hex = "0.4.3"
cairo-lang-casm.workspace = true
cairo-vm.workspace = true
hex.workspace = true
itertools.workspace = true
num-traits = "0.2.17"
serde = "1.0.207"
sonic-rs = "0.3.10"
# TODO(ShaharS): take stwo version from the source repository.
stwo-prover = { git = "https://github.com/starkware-libs/stwo", rev = "e04fd5b" }
thiserror = "1.0.63"
num-traits.workspace = true
serde.workspace = true
sonic-rs.workspace = true
stwo-prover.workspace = true
thiserror.workspace = true

[dev-dependencies]
cairo-lang-casm = "2.7.1"
rand = "0.8.5"
cairo-lang-casm.workspace = true
rand.workspace = true
18 changes: 11 additions & 7 deletions stwo_cairo_prover/crates/prover/src/cairo_air/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use itertools::{chain, Itertools};
use num_traits::Zero;
use serde::{Deserialize, Serialize};
use stwo_prover::constraint_framework::TraceLocationAllocator;
use stwo_prover::core::air::{Component, ComponentProver};
use stwo_prover::core::backend::simd::SimdBackend;
Expand All @@ -11,7 +12,7 @@ use stwo_prover::core::pcs::{
CommitmentSchemeProver, CommitmentSchemeVerifier, PcsConfig, TreeVec,
};
use stwo_prover::core::poly::circle::{CanonicCoset, PolyOps};
use stwo_prover::core::prover::{prove, verify, StarkProof, VerificationError};
use stwo_prover::core::prover::{prove, verify, ProvingError, StarkProof, VerificationError};
use stwo_prover::core::vcs::blake2_merkle::{Blake2sMerkleChannel, Blake2sMerkleHasher};
use stwo_prover::core::vcs::ops::MerkleHasher;
use thiserror::Error;
Expand Down Expand Up @@ -44,12 +45,14 @@ const RC9_LOG_REPS: u32 = 1;
const RC9_LOG_HEIGHT: u32 = RC9_LOG_MAX - RC9_LOG_REPS;
const RC9_REPS: usize = 1 << RC9_LOG_REPS;

#[derive(Serialize, Deserialize)]
pub struct CairoProof<H: MerkleHasher> {
pub claim: CairoClaim,
pub interaction_claim: CairoInteractionClaim,
pub stark_proof: StarkProof<H>,
}

#[derive(Serialize, Deserialize)]
pub struct CairoClaim {
// Common claim values.
pub public_memory: Vec<(u32, [u32; 8])>,
Expand Down Expand Up @@ -95,6 +98,7 @@ impl CairoInteractionElements {
}
}

#[derive(Serialize, Deserialize)]
pub struct CairoInteractionClaim {
pub ret: Vec<RetOpcodeInteractionClaim>,
pub range_check_builtin: RangeCheckBuiltinInteractionClaim,
Expand Down Expand Up @@ -231,7 +235,7 @@ impl CairoComponents {
}

const LOG_MAX_ROWS: u32 = 20;
pub fn prove_cairo(input: CairoInput) -> CairoProof<Blake2sMerkleHasher> {
pub fn prove_cairo(input: CairoInput) -> Result<CairoProof<Blake2sMerkleHasher>, ProvingError> {
let config = PcsConfig::default();
let twiddles = SimdBackend::precompute_twiddles(
CanonicCoset::new(LOG_MAX_ROWS + config.fri_config.log_blowup_factor + 2)
Expand Down Expand Up @@ -333,13 +337,13 @@ pub fn prove_cairo(input: CairoInput) -> CairoProof<Blake2sMerkleHasher> {
let components = component_builder.provers();

// Prove stark.
let proof = prove::<SimdBackend, _>(&components, channel, commitment_scheme).unwrap();
let proof = prove::<SimdBackend, _>(&components, channel, commitment_scheme)?;

CairoProof {
Ok(CairoProof {
claim,
interaction_claim,
stark_proof: proof,
}
})
}

pub fn verify_cairo(
Expand Down Expand Up @@ -419,13 +423,13 @@ mod tests {

#[test]
fn test_basic_cairo_air() {
let cairo_proof = prove_cairo(test_input());
let cairo_proof = prove_cairo(test_input()).unwrap();
verify_cairo(cairo_proof).unwrap();
}

#[test]
fn test_full_cairo_air() {
let cairo_proof = prove_cairo(small_cairo_input());
let cairo_proof = prove_cairo(small_cairo_input()).unwrap();
verify_cairo(cairo_proof).unwrap();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use num_traits::One;
use serde::{Deserialize, Serialize};
use stwo_prover::constraint_framework::logup::LogupAtRow;
use stwo_prover::constraint_framework::{EvalAtRow, FrameworkComponent, FrameworkEval};
use stwo_prover::core::channel::Channel;
Expand Down Expand Up @@ -84,7 +85,7 @@ impl FrameworkEval for MemoryEval {
}
}

#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct MemoryClaim {
pub log_address_bound: u32,
}
Expand All @@ -101,7 +102,7 @@ impl MemoryClaim {
}
}

#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct MemoryInteractionClaim {
pub claimed_sum: SecureField,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use num_traits::{One, Zero};
use serde::{Deserialize, Serialize};
use stwo_prover::constraint_framework::logup::LogupAtRow;
use stwo_prover::constraint_framework::{EvalAtRow, FrameworkComponent, FrameworkEval};
use stwo_prover::core::channel::Channel;
Expand Down Expand Up @@ -93,7 +94,7 @@ impl FrameworkEval for RangeCheckBuiltinEval {
}
}

#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct RangeCheckBuiltinClaim {
pub memory_segment: SegmentAddrs,
}
Expand All @@ -113,7 +114,7 @@ impl RangeCheckBuiltinClaim {
}
}

#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct RangeCheckBuiltinInteractionClaim {
pub log_size: u32,
pub claimed_sum: SecureField,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use num_traits::{One, Zero};
use serde::{Deserialize, Serialize};
use stwo_prover::constraint_framework::logup::LogupAtRow;
use stwo_prover::constraint_framework::{EvalAtRow, FrameworkComponent, FrameworkEval};
use stwo_prover::core::channel::Channel;
Expand Down Expand Up @@ -52,7 +53,7 @@ impl<const N_REPETITIONS: usize> FrameworkEval for RangeCheckUnitEval<N_REPETITI
/// Range check unit component claim.
/// `log_rc_height` is the log of the number of rows in the range check table, meaning that that
/// range checked is 0..(N_REPETITIONS * 2^log_rc_height).
#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct RangeCheckClaim<const N_REPETITIONS: usize> {
pub log_rc_height: u32,
}
Expand All @@ -70,7 +71,7 @@ impl<const N_REPETITIONS: usize> RangeCheckClaim<N_REPETITIONS> {
}
}

#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct RangeCheckInteractionClaim<const N_REPETITIONS: usize> {
pub claimed_sum: SecureField,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use num_traits::{One, Zero};
use serde::{Deserialize, Serialize};
use stwo_prover::constraint_framework::logup::LogupAtRow;
use stwo_prover::constraint_framework::{EvalAtRow, FrameworkComponent, FrameworkEval};
use stwo_prover::core::channel::Channel;
Expand Down Expand Up @@ -88,7 +89,7 @@ impl FrameworkEval for RetOpcodeEval {
}
}

#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct RetOpcodeClaim {
pub n_rets: usize,
}
Expand All @@ -105,7 +106,7 @@ impl RetOpcodeClaim {
}
}

#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct RetOpcodeInteractionClaim {
pub log_size: u32,
pub claimed_sum: SecureField,
Expand Down
4 changes: 3 additions & 1 deletion stwo_cairo_prover/crates/prover/src/input/instructions.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use serde::{Deserialize, Serialize};

use super::decode::Instruction;
use super::mem::{Memory, MemoryValue};
use super::vm_import::TraceEntry;

// TODO(spapini): Move this:
#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct VmState {
pub pc: u32,
pub ap: u32,
Expand Down
Loading

0 comments on commit 88b700e

Please sign in to comment.