Skip to content

Commit 68f2343

Browse files
authored
Add execute to Host trait, update proof report generation, and improve documentation (#11)
* add name to prover trait * refactor: use prover name * refactor: save proof receipt based on env var * feat: update proof report with execution and proving time * feat: add execute to host * refactor: remove perf files * update readme and makefile
1 parent 7db07d0 commit 68f2343

File tree

23 files changed

+284
-194
lines changed

23 files changed

+284
-194
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ target/
22
elf/
33
cache/
44

5-
*.pb
6-
*.trace
5+
*.trace_profile
76
*.proof

.vscode/settings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
},
1717
"rust-analyzer.linkedProjects": [
1818
"Cargo.toml",
19-
"artifacts/sp1/Cargo.toml",
20-
"artifacts/risc0/Cargo.toml"
19+
// "artifacts/sp1/Cargo.toml",
20+
// "artifacts/risc0/Cargo.toml"
2121
],
2222
"rust-analyzer.diagnostics.enable": true,
2323
"todohighlight.isEnable": true,

Makefile

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ CARGO_INSTALL_EXTRA_FLAGS ?=
1414
# List of features to use for building
1515
FEATURES ?=
1616

17+
# List of programs
18+
PROGRAMS ?= fibonacci,sha2-chain,schnorr-sig-verify
19+
20+
# ZkVm to use
21+
ZKVM ?= sp1
22+
1723
##@ Help
1824

1925
.PHONY: help
@@ -57,29 +63,24 @@ sec: ## Check for security advisories on any dependencies.
5763
##@ Prover
5864

5965
.PHONY: report
60-
report: prover-clean ## Generate proof report for programs
61-
cargo run --release -- --programs fibonacci,sha2-chain,schnorr-sig-verify
66+
report: prover-clean ## Generate proof report for programs for all supported ZkVm
67+
cargo run --release -- --programs $(PROGRAMS)
6268

6369
.PHONY: report-sp1
64-
report-sp1: prover-clean ## Generate SP1 proof report for programs
65-
cargo run --release --no-default-features -F sp1-mock -- --programs fibonacci,sha2-chain,schnorr-sig-verify
70+
report-sp1: prover-clean ## Generate SP1 proof report for given programs
71+
cargo run --release --no-default-features -F sp1-mock -- --programs $(PROGRAMS)
6672

6773
.PHONY: report-risc0
68-
report-risc0: prover-clean ## Generate Risc0 proof report for programs
69-
cargo run --release --no-default-features -F risc0-mock -- --programs fibonacci,sha2-chain,schnorr-sig-verify
70-
71-
.PHONY: proof-sp1-fibonacci
72-
proof-sp1-fibonacci: prover-clean ## Generate SP1 Groth16 proof for fibonacci program
73-
cargo run --release --no-default-features -F sp1 -- --programs fibonacci
74-
75-
.PHONY: proof-risc0-fibonacci
76-
proof-risc0-fibonacci: prover-clean ## Generate Risc0 Groth16 proof for fibonacci program
77-
cargo run --release --no-default-features -F risc0 -- --programs fibonacci
74+
report-risc0: prover-clean ## Generate Risc0 proof report for given programs
75+
cargo run --release --no-default-features -F risc0-mock -- --programs $(PROGRAMS)
7876

77+
.PHONY: proof
78+
proof: ## Generate proof for the given program using the given ZkVm
79+
ZKVM_PROOF_DUMP=1 cargo run --release --no-default-features -F $(ZKVM) -- --programs $(PROGRAMS)
7980

8081
.PHONY: prover-clean
8182
prover-clean: ## Cleans up proofs and profiling data generated
82-
rm -rf *.trace
83+
rm -rf *.trace_profile
8384
rm -rf *.proof
8485

8586
##@ Code Quality

README.md

Lines changed: 124 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,134 @@ Currently, the repository supports the following ZKVMs:
2626

2727
---
2828

29-
## Usage
29+
# Usage Guide
3030

31-
### Generating report of cycle count of different programs
31+
## Generating Report of Cycle Counts for Programs
32+
33+
To generate a report of cycle counts for one or more programs, use the following command:
34+
35+
```bash
36+
make report PROGRAMS=<PROGRAM_NAME>
37+
```
38+
39+
### Notes:
40+
- **Multiple Programs Supported:** You can specify multiple programs by separating them with commas.
41+
- **Example Commands:**
42+
```bash
43+
make report PROGRAMS=fibonacci
44+
make report PROGRAMS=fibonacci,sha2-chain
45+
```
46+
- **Optional Parameter:** If `PROGRAMS` is left empty, the report will be generated for all supported programs.
47+
48+
### Supported Programs
49+
Here is the list of currently supported programs:
50+
- `fibonacci`
51+
- `sha2-chain`
52+
- `schnorr-sig-verify`
53+
54+
## Generating Report for a Specific ZKVM
55+
56+
To generate profiling data for a specific ZKVM, use the following commands:
57+
58+
```bash
59+
make report-<ZkVm>
60+
```
61+
62+
### Example Commands:
63+
- For Risc0:
64+
```bash
65+
make report-risc0
66+
```
67+
- For SP1:
68+
```bash
69+
make report-sp1
70+
```
71+
- Similar to the general `make report`, you can also pass the `PROGRAMS` parameter:
72+
```bash
73+
make report-risc0 PROGRAMS=fibonacci
74+
make report-sp1 PROGRAMS=fibonacci,sha2-chain
75+
```
76+
77+
78+
## Generating Profile Data
79+
80+
To dump profiling data, set the environment variable `ZKVM_PROFILING_DUMP=1` while running any of the above command.
81+
82+
```bash
83+
ZKVM_PROFILING_DUMP=1 make report
84+
```
85+
86+
87+
### Viewing SP1 Profile
88+
89+
The profiling data for SP1 can be viewed using [Samply](https://github.com/mstange/samply). To view the profile:
90+
91+
```bash
92+
samply load <FILENAME>.trace_profile
93+
```
94+
95+
### Viewing Risc0 Profile
96+
97+
The profiling data for Risc0 can be viewed using [pprof](https://github.com/google/pprof). To view the profile:
98+
99+
```bash
100+
go tool pprof -http=127.0.0.1:8000 <FILENAME>.trace_profile
101+
```
102+
103+
## Generating Proof for Program
104+
105+
If proof is required, use the following command:
32106

33107
```bash
34-
make report
108+
make proof ZKVM=<ZKVM_NAME> PROGRAMS=<PROGRAM_NAME>
35109
```
110+
> The proofs are dumped by default
111+
112+
### Additional Options:
113+
- **Dump Profiling Data:** To dump proofs, set the environment variable `ZKVM_PROOF_DUMP=1` while running the command:
114+
115+
## Cleaning Up Generated Data
116+
To clean up generated proofs and profiling data, run:
117+
118+
```bash
119+
make prover-clean
120+
```
121+
This will remove all `.trace_profile` and `.proof` files from the current directory.
122+
123+
---
124+
125+
### Example Workflow
126+
1. **Generate Report for All Programs:**
127+
```bash
128+
make report
129+
```
130+
131+
2. **Generate Report for Specific Programs:**
132+
```bash
133+
make report PROGRAMS=fibonacci,sha2-chain
134+
```
135+
136+
3. **Generate SP1 Report for Specific Programs:**
137+
```bash
138+
make report-sp1 PROGRAMS=schnorr-sig-verify
139+
```
140+
141+
4. **Dump Profiling Data for Risc0:**
142+
```bash
143+
ZKVM_PROFILING_DUMP=1 make report-risc0 PROGRAMS=fibonacci
144+
```
145+
146+
5. **Generate and dump Risc0 Proof**
147+
```bash
148+
make proof ZKVM=risc0 PROGRAMS=fibonacci
149+
```
150+
151+
152+
5. **Clean Up Generated Data:**
153+
```bash
154+
make prover-clean
155+
```
156+
36157

37158
---
38159

crates/native/src/host.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ impl ZkVmHost for NativeHost {
2222
native_machine: NativeMachine,
2323
_proof_type: ProofType,
2424
) -> ZkVmResult<NativeProofReceipt> {
25+
let (public_values, _) = self.execute(native_machine)?;
26+
let proof = Proof::default();
27+
Ok(ProofReceipt::new(proof, public_values).try_into()?)
28+
}
29+
30+
fn execute<'a>(&self, native_machine: NativeMachine) -> ZkVmResult<(PublicValues, u64)> {
2531
(self.process_proof)(&native_machine)?;
2632
let output = native_machine.state.borrow().output.clone();
27-
let proof = Proof::default();
2833
let public_values = PublicValues::new(output);
29-
Ok(ProofReceipt::new(proof, public_values).try_into()?)
34+
// Since we don't care about cycle counts in the native mode, setting it to zero
35+
Ok((public_values, 0))
3036
}
3137

3238
fn get_elf(&self) -> &[u8] {

crates/risc0/src/host.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::fmt;
22

33
use hex::encode;
4-
use risc0_zkvm::{compute_image_id, default_prover, sha::Digest, Journal, ProverOpts};
4+
use risc0_zkvm::{
5+
compute_image_id, default_executor, default_prover, sha::Digest, Journal, ProverOpts,
6+
};
57
use serde::{de::DeserializeOwned, Serialize};
68
use strata_zkvm::{
79
ProofType, PublicValues, VerificationKey, ZkVmError, ZkVmHost, ZkVmInputBuilder, ZkVmResult,
@@ -58,6 +60,28 @@ impl ZkVmHost for Risc0Host {
5860
Ok(proof_info.receipt.into())
5961
}
6062

63+
fn execute<'a>(
64+
&self,
65+
prover_input: <Self::Input<'a> as ZkVmInputBuilder<'a>>::Input,
66+
) -> ZkVmResult<(PublicValues, u64)> {
67+
let executor = default_executor();
68+
69+
if std::env::var("ZKVM_PROFILING_DUMP")
70+
.map(|v| v == "1" || v.to_lowercase() == "true")
71+
.unwrap_or(false)
72+
{
73+
let profiling_file_name = format!("{:?}.trace_profile", self);
74+
std::env::set_var("RISC0_PPROF_OUT", profiling_file_name);
75+
}
76+
77+
// TODO: handle error
78+
let session_info = executor.execute(prover_input, self.get_elf()).unwrap();
79+
80+
let cycles = session_info.cycles();
81+
let public_values = PublicValues::new(session_info.journal.bytes);
82+
Ok((public_values, cycles))
83+
}
84+
6185
fn extract_serde_public_output<T: Serialize + DeserializeOwned>(
6286
proof: &PublicValues,
6387
) -> ZkVmResult<T> {

crates/risc0/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,3 @@ pub use groth16_verifier::*;
1414

1515
mod env;
1616
pub use env::Risc0ZkVmEnv;
17-
18-
#[cfg(feature = "perf")]
19-
mod perf;

crates/risc0/src/perf.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

crates/sp1/src/host.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,32 @@ impl ZkVmHost for SP1Host {
6666
Ok(proof_info.into())
6767
}
6868

69+
fn execute<'a>(
70+
&self,
71+
prover_input: <Self::Input<'a> as ZkVmInputBuilder<'a>>::Input,
72+
) -> ZkVmResult<(PublicValues, u64)> {
73+
let client = ProverClient::from_env();
74+
75+
if std::env::var("ZKVM_PROFILING_DUMP")
76+
.map(|v| v == "1" || v.to_lowercase() == "true")
77+
.unwrap_or(false)
78+
{
79+
let profiling_file_name = format!("{:?}.trace_profile", self);
80+
std::env::set_var("TRACE_FILE", profiling_file_name);
81+
}
82+
83+
let (output, report) = client.execute(self.get_elf(), &prover_input).run().unwrap();
84+
85+
// Remove the variable after execution to avoid duplication of trace generation in perf
86+
// report
87+
std::env::remove_var("TRACE_FILE");
88+
89+
let public_values = PublicValues::new(output.to_vec());
90+
let total_cycles = report.total_instruction_count();
91+
92+
Ok((public_values, total_cycles))
93+
}
94+
6995
fn extract_serde_public_output<T: Serialize + DeserializeOwned>(
7096
public_values: &PublicValues,
7197
) -> ZkVmResult<T> {

crates/sp1/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,5 @@ mod env;
1515
#[cfg(feature = "zkvm")]
1616
pub use env::Sp1ZkVmEnv;
1717

18-
#[cfg(feature = "perf")]
19-
pub mod perf;
20-
2118
mod groth16_verifier;
2219
pub use groth16_verifier::*;

0 commit comments

Comments
 (0)