Skip to content

Commit

Permalink
feat: Add cycle-tracking example with multiple binaries (#1529)
Browse files Browse the repository at this point in the history
  • Loading branch information
ratankaliani authored Sep 20, 2024
1 parent 14567af commit 8e82b81
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 60 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,42 @@ jobs:
cargo add sp1-sdk --path $GITHUB_WORKSPACE/crates/sdk
SP1_DEV=1 RUST_LOG=info cargo run --release
cycle-tracking:
name: Example (Cycle Tracking)
runs-on:
[
runs-on,
runner=64cpu-linux-arm64,
spot=false,
"run-id=${{ github.run_id }}",
]
env:
CARGO_NET_GIT_FETCH_WITH_CLI: "true"
steps:
- name: Checkout sources
uses: actions/checkout@v4

- name: Setup CI
uses: ./.github/actions/setup

- name: Install SP1 toolchain
run: |
curl -L https://sp1.succinct.xyz | bash
~/.sp1/bin/sp1up
~/.sp1/bin/cargo-prove prove --version
- name: Install SP1 CLI
run: |
cd crates/cli
cargo install --force --locked --path .
cd ~
- name: Run script
run: |
cd examples/cycle-tracking/script
cargo add sp1-sdk --path $GITHUB_WORKSPACE/crates/sdk
SP1_DEV=1 RUST_LOG=info cargo run --release
tendermint:
name: Example (Tendermint)
runs-on:
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,10 @@ jobs:
cd ../script
cargo remove sp1-sdk
cargo add sp1-sdk --path $GITHUB_WORKSPACE/crates/sdk
SP1_DEV=1 RUST_LOG=info cargo run --release
- name: Run cycle tracking script
run: |
cd examples/cycle-tracking/script
cargo add sp1-sdk --path $GITHUB_WORKSPACE/crates/sdk
SP1_DEV=1 RUST_LOG=info cargo run --release
6 changes: 3 additions & 3 deletions examples/cycle-tracking/program/Cargo.lock

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

8 changes: 8 additions & 0 deletions examples/cycle-tracking/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ version = "1.1.0"
edition = "2021"
publish = false

[[bin]]
name = "normal"
path = "bin/normal.rs"

[[bin]]
name = "report"
path = "bin/report.rs"

[dependencies]
sp1-zkvm = { path = "../../../crates/zkvm/entrypoint" }
sp1-derive = { path = "../../../crates/derive" }
Binary file modified examples/cycle-tracking/program/elf/normal
Binary file not shown.
Binary file modified examples/cycle-tracking/program/elf/report
Binary file not shown.
Binary file not shown.
32 changes: 0 additions & 32 deletions examples/cycle-tracking/program/src/main.rs

This file was deleted.

11 changes: 9 additions & 2 deletions examples/cycle-tracking/script/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
use sp1_build::build_program;
use sp1_build::{build_program_with_args, BuildArgs};

fn main() {
build_program("../program")
build_program_with_args("../program", BuildArgs {
binary: "normal".to_string(),
..Default::default()
});
build_program_with_args("../program", BuildArgs {
binary: "report".to_string(),
..Default::default()
});
}
33 changes: 10 additions & 23 deletions examples/cycle-tracking/script/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
use sp1_sdk::{utils, ProverClient, SP1ProofWithPublicValues, SP1Stdin};
use sp1_sdk::{utils, ProverClient, SP1Stdin};

/// The ELF we want to execute inside the zkVM.
const ELF: &[u8] = include_bytes!("../../program/elf/riscv32im-succinct-zkvm-elf");
const REPORT_ELF: &[u8] = include_bytes!("../../program/elf/report");
const NORMAL_ELF: &[u8] = include_bytes!("../../program/elf/normal");

fn main() {
// Setup a tracer for logging.
utils::setup_logger();

// Create an input stream.
let stdin = SP1Stdin::new();

// Generate the proof for the given program.
// Execute the normal program.
let client = ProverClient::new();
let (pk, vk) = client.setup(ELF);
let proof = client.prove(&pk, stdin).run().expect("proving failed");

// Verify proof.
client.verify(&proof, &vk).expect("verification failed");

// Test a round trip of proof serialization and deserialization.
proof
.save("proof-with-pis.bin")
.expect("saving proof failed");
let deserialized_proof =
SP1ProofWithPublicValues::load("proof-with-pis.bin").expect("loading proof failed");
let (_, _) = client.execute(NORMAL_ELF, SP1Stdin::new()).run().expect("proving failed");

// Verify the deserialized proof.
client
.verify(&deserialized_proof, &vk)
.expect("verification failed");
// Execute the report program.
let (_, report) = client.execute(REPORT_ELF, SP1Stdin::new()).run().expect("proving failed");

println!("successfully generated and verified proof for the program!")
// Get the "setup" cycle count from the report program.
let setup_cycles = report.cycle_tracker.get("setup").unwrap();
println!("Using cycle-tracker-report saves the number of cycles to the cycle-tracker mapping in the report.\nHere's the number of cycles used by the setup: {}", setup_cycles);
}

0 comments on commit 8e82b81

Please sign in to comment.