Skip to content

Commit

Permalink
feat: implement blevm-mock (#131)
Browse files Browse the repository at this point in the history
Closes #93

- Deleted provers/blevm/elf/riscv32im-succinct-zkvm-elf because
https://succinctlabs.notion.site/sp1-v4-migration-guide states we
shouldn't commit the ELFs to source control.
- Modifies the script so that it can run in either: prove or execute
mode.
- Modifies the script so that it can run on `blevm` or `blevm-mock` ELF
- Added proof generation time to README
  • Loading branch information
rootulp authored Feb 4, 2025
1 parent 436148d commit e50125e
Show file tree
Hide file tree
Showing 13 changed files with 1,181 additions and 747 deletions.
1,665 changes: 993 additions & 672 deletions provers/blevm/Cargo.lock

Large diffs are not rendered by default.

15 changes: 5 additions & 10 deletions provers/blevm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
[workspace]
members = [
"blevm",
"blevm-mock",
"script",
"blevm-aggregator",
"common",
]
members = ["blevm", "blevm-mock", "script", "blevm-aggregator", "common"]
resolver = "2"

[workspace.dependencies]
serde = { version = "1.0.200", default-features = false, features = ["derive"] }
alloy-sol-types = "0.7.7"
rsp-client-executor = {git = "https://github.com/succinctlabs/rsp.git", rev="c01149568a2ed4d3e766756e8b847c870a0b1e4e"}
rsp-client-executor = { git = "https://github.com/succinctlabs/rsp.git", rev = "c01149568a2ed4d3e766756e8b847c870a0b1e4e" }
reth-primitives = { git = "https://github.com/sp1-patches/reth", tag = "rsp-20240830", default-features = false, features = [
"alloy-compat",
"optimism",
"std",
] }
celestia-types = {git="https://github.com/S1nus/lumina.git", rev="c2971dfbccc0b56a2ad61ea587ca84c11fcfb1a3"}
celestia-rpc = {git="https://github.com/S1nus/lumina.git", rev="c2971dfbccc0b56a2ad61ea587ca84c11fcfb1a3"}
celestia-types = { git = "https://github.com/S1nus/lumina.git", rev = "c2971dfbccc0b56a2ad61ea587ca84c11fcfb1a3" }
celestia-rpc = { git = "https://github.com/S1nus/lumina.git", rev = "c2971dfbccc0b56a2ad61ea587ca84c11fcfb1a3" }
tendermint-proto = "*"
tendermint = "*"
bincode = "1.3.3"
nmt-rs = "*"
sp1-sdk = "4.0.1"
sp1-zkvm = "4.0.1"
sp1-helper = "4.0.1"
sp1-build = "4.0.1"

[patch.crates-io]
ecdsa-core = { git = "https://github.com/sp1-patches/signatures", package = "ecdsa", tag = "patch-0.16.9-sp1-4.0.0" }
Expand Down
34 changes: 22 additions & 12 deletions provers/blevm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,33 @@ The `script` binary will generate an SP1 proof but it depends on a DA node. You
```shell
# Change to the correct directory
cd celestia-zkevm-ibc-demo/provers/blevm/script
# Run the script
cargo run
# Execute blevm mock
RUST_LOG=info cargo run --release -- --execute --mock
# Execute blevm
RUST_LOG=info cargo run --release -- --execute
# Generate a mock proof
RUST_LOG=info cargo run --release -- --prove --mock
# Generate a real proof
RUST_LOG=info cargo run --release -- --prove
```

4. [Optional] To generate a `blevm-mock` proof, modify `script/src/bin/main.rs` with the diff below then run the script again.
### Development

```diff
let prover_config = ProverConfig {
- elf_bytes: include_elf!("blevm"),
+ elf_bytes: include_elf!("blevm-mock"),
};
```
While developing SP1 programs (i.e. `blevm`, `blevm-mock`, `blevm-aggregate`) it is helpful to generate [development builds](https://docs.succinct.xyz/docs/writing-programs/compiling#development-builds):

```shell
# Change to an SP1 program crate
cd blevm-mock
# Build for development
cargo prove build
```

## FAQ

How long does it take to generate a proof?

| Proof | Time | SP1_PROVER |
|-------|-----------|------------|
| blevm | 6 minutes | network |
| SP1_PROVER | Program | Time |
|------------|------------|------------|
| network | blevm-mock | 30 seconds |
| network | blevm | 6 minutes |
4 changes: 4 additions & 0 deletions provers/blevm/blevm-aggregator/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl Buffer {
}
}

#[allow(dead_code)]
/// Set the position ptr to the beginning of the buffer.
pub fn head(&mut self) {
self.ptr = 0;
Expand All @@ -37,18 +38,21 @@ impl Buffer {
result
}

#[allow(dead_code)]
pub fn read_slice(&mut self, slice: &mut [u8]) {
slice.copy_from_slice(&self.data[self.ptr..self.ptr + slice.len()]);
self.ptr += slice.len();
}

#[allow(dead_code)]
/// Write the serializable object from the buffer.
pub fn write<T: Serialize>(&mut self, data: &T) {
let mut tmp = Vec::new();
bincode::serialize_into(&mut tmp, data).expect("serialization failed");
self.data.extend(tmp);
}

#[allow(dead_code)]
/// Write the slice of bytes to the buffer.
pub fn write_slice(&mut self, slice: &[u8]) {
self.data.extend_from_slice(slice);
Expand Down
9 changes: 9 additions & 0 deletions provers/blevm/blevm-mock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ edition = "2021"
[dependencies]
sp1-zkvm = { workspace = true }
blevm-common = { path = "../common" }
alloy-sol-types = { workspace = true }
rsp-client-executor = {workspace=true}
celestia-types = {workspace=true}
nmt-rs = "*"
reth-primitives = {workspace=true}
tendermint = {workspace=true}
tendermint-proto = {workspace=true}
bincode = {workspace=true}
hex = "0.4.3"
51 changes: 33 additions & 18 deletions provers/blevm/blevm-mock/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,42 @@
sp1_zkvm::entrypoint!(main);

use blevm_common::BlevmOutput;
use celestia_types::nmt::Namespace;
use celestia_types::nmt::NamespaceProof;
use nmt_rs::{simple_merkle::proof::Proof, NamespacedHash, TmSha2Hasher};
use rsp_client_executor::io::ClientExecutorInput;

pub fn main() {
let blob_commitment = sp1_zkvm::io::read::<Vec<u8>>();
let header_hash = sp1_zkvm::io::read::<Vec<u8>>();
let prev_header_hash = sp1_zkvm::io::read::<Vec<u8>>();
let height = sp1_zkvm::io::read::<u64>();
let gas_used = sp1_zkvm::io::read::<u64>();
let beneficiary = sp1_zkvm::io::read::<Vec<u8>>();
let state_root = sp1_zkvm::io::read::<Vec<u8>>();
let celestia_header_hash = sp1_zkvm::io::read::<Vec<u8>>();

// This is a mock proof so it hard-codes all the output values. Note: these values were sourced
// from a valid execution of blevm.
let output = BlevmOutput {
blob_commitment: blob_commitment.try_into().unwrap(),
header_hash: header_hash.try_into().unwrap(),
prev_header_hash: prev_header_hash.try_into().unwrap(),
height,
gas_used,
beneficiary: beneficiary.try_into().unwrap(),
state_root: state_root.try_into().unwrap(),
celestia_header_hash: celestia_header_hash.try_into().unwrap(),
blob_commitment: [
196, 0, 0, 0, 0, 0, 0, 0, 121, 70, 207, 82, 142, 221, 116, 94, 251, 37, 32, 18, 70,
230, 71, 213, 170, 202, 63, 181, 43, 240, 246, 6,
],
header_hash: [
182, 149, 180, 171, 11, 211, 63, 76, 133, 106, 134, 184, 20, 76, 104, 254, 40, 136, 41,
140, 238, 199, 193, 86, 163, 56, 170, 193, 61, 146, 213, 227,
],
prev_header_hash: [
194, 70, 12, 164, 151, 147, 237, 105, 187, 154, 187, 153, 78, 140, 25, 59, 84, 254,
152, 25, 224, 239, 83, 45, 145, 73, 226, 110, 100, 51, 95, 167,
],
height: 14900876081506838043,
gas_used: 18884864,
beneficiary: [
4, 26, 65, 0, 0, 0, 0, 0, 149, 34, 34, 144, 221, 114, 120, 170, 61, 221, 56, 156,
],
state_root: [
193, 225, 209, 101, 204, 75, 175, 229, 27, 56, 213, 58, 25, 68, 72, 76, 140, 126, 48,
23, 127, 212, 219, 222, 63, 98, 45, 102, 165, 88, 255, 220,
],
celestia_header_hash: [
120, 107, 54, 46, 182, 50, 89, 93, 115, 224, 125, 214, 72, 215, 109, 67, 90, 48, 217,
144, 215, 85, 206, 228, 192, 183, 123, 79, 244, 136, 195, 212,
],
};

sp1_zkvm::io::commit(&output);
let serialized_output = bincode::serialize(&output).unwrap();
sp1_zkvm::io::commit(&serialized_output);
}
24 changes: 21 additions & 3 deletions provers/blevm/blevm-prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use nmt_rs::{
TmSha2Hasher,
};
use rsp_client_executor::io::ClientExecutorInput;
use sp1_sdk::{ProverClient, SP1Stdin};
use sp1_sdk::{ExecutionReport, ProverClient, SP1PublicValues, SP1Stdin};
use std::error::Error;
use tendermint_proto::{
v0_37::{types::BlockId as RawBlockId, version::Consensus as RawConsensusVersion},
Expand Down Expand Up @@ -187,7 +187,7 @@ impl BlockProver {
}
}

pub async fn generate_proof(&self, input: BlockProverInput) -> Result<Vec<u8>, Box<dyn Error>> {
async fn get_stdin(&self, input: BlockProverInput) -> Result<SP1Stdin, Box<dyn Error>> {
// Create blob from L2 block data
let block: ClientExecutorInput = bincode::deserialize(&input.l2_block_data)?;
let block_bytes = bincode::serialize(&block.current_block)?;
Expand Down Expand Up @@ -220,10 +220,28 @@ impl BlockProver {
stdin.write(&row_root_multiproof);
stdin.write(&nmt_multiproofs);
stdin.write(&selected_roots);
Ok(stdin)
}

pub async fn execute(
&self,
input: BlockProverInput,
) -> Result<(SP1PublicValues, ExecutionReport), Box<dyn Error>> {
let client: sp1_sdk::EnvProver = ProverClient::from_env();
let stdin = self.get_stdin(input).await?;
let (public_values, execution_report) = client
.execute(self.prover_config.elf_bytes, &stdin)
.run()
.unwrap();

Ok((public_values, execution_report))
}

pub async fn generate_proof(&self, input: BlockProverInput) -> Result<Vec<u8>, Box<dyn Error>> {
// Generate and return the proof
let client = ProverClient::from_env();
let client: sp1_sdk::EnvProver = ProverClient::from_env();
let (pk, _) = client.setup(self.prover_config.elf_bytes);
let stdin = self.get_stdin(input).await?;
let proof = client.prove(&pk, &stdin).groth16().run()?;

bincode::serialize(&proof).map_err(|e| e.into())
Expand Down
3 changes: 2 additions & 1 deletion provers/blevm/blevm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ pub fn main() {
state_root: header.state_root.into(),
celestia_header_hash: celestia_header_hash.as_bytes().try_into().unwrap(),
};
sp1_zkvm::io::commit(&output);
let serialized_output = bincode::serialize(&output).unwrap();
sp1_zkvm::io::commit(&serialized_output);

println!(
"cycle-tracker-end: hashing the block header, and commiting its fields as public values"
Expand Down
2 changes: 1 addition & 1 deletion provers/blevm/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug)]
pub struct BlevmOutput {
pub blob_commitment: [u8; 32],
pub header_hash: [u8; 32],
Expand Down
Binary file removed provers/blevm/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
5 changes: 4 additions & 1 deletion provers/blevm/script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ bincode = { workspace = true }
rsp-client-executor = { workspace = true }
nmt-rs = { workspace = true }
tokio = { version = "1", features = ["full"] }
blevm-prover = { path = "../blevm-prover" }
dotenv = "0.15.0"

blevm-prover = { path = "../blevm-prover" }
blevm-common = {path = "../common"}

[build-dependencies]
sp1-helper = { workspace = true }
sp1-build = { workspace = true }
6 changes: 3 additions & 3 deletions provers/blevm/script/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use sp1_helper::build_program_with_args;
use sp1_build::build_program;

fn main() {
build_program_with_args("../blevm", Default::default());
// build_program_with_args("../blevm-mock", Default::default());
build_program("../blevm");
build_program("../blevm-mock");
}
Loading

0 comments on commit e50125e

Please sign in to comment.