Skip to content

Commit

Permalink
Enable SP1 Mock Mode with Proof Composition Support via Feature Flag (#…
Browse files Browse the repository at this point in the history
…493)

* feat(sp1): add mock mode

* feat(risc0): mock mode
  • Loading branch information
prajwolrg authored Nov 29, 2024
1 parent 03a6b1c commit 0ea3c38
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 3 deletions.
1 change: 1 addition & 0 deletions crates/zkvm/adapters/risc0/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
[features]
default = []
prover = []
mock = []
5 changes: 5 additions & 0 deletions crates/zkvm/adapters/risc0/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ impl ZkVmHost for Risc0Host {
prover_input: <Self::Input<'a> as ZkVmInputBuilder<'a>>::Input,
proof_type: ProofType,
) -> ZkVmResult<(Proof, VerificationKey)> {
#[cfg(feature = "mock")]
{
std::env::set_var("RISC0_DEV_MODE", "true");
}

// Setup the prover
let opts = match proof_type {
ProofType::Core => ProverOpts::default(),
Expand Down
1 change: 1 addition & 0 deletions crates/zkvm/adapters/sp1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ tracing.workspace = true
default = []
prover = ["sp1-sdk"]
zkvm = ["sp1-zkvm"]
mock = []
20 changes: 19 additions & 1 deletion crates/zkvm/adapters/sp1/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use serde::{de::DeserializeOwned, Serialize};
#[cfg(not(feature = "mock"))]
use sha2::{Digest, Sha256};
use sp1_zkvm::{io, lib::verify::verify_sp1_proof};
use sp1_zkvm::io;
#[cfg(not(feature = "mock"))]
use sp1_zkvm::lib::verify::verify_sp1_proof;
use strata_zkvm::{Proof, ZkVmEnv};

#[cfg(not(feature = "mock"))]
use crate::verify_groth16;

pub struct Sp1ZkVmEnv;
Expand All @@ -24,11 +28,16 @@ impl ZkVmEnv for Sp1ZkVmEnv {
io::commit_slice(output_raw);
}

#[cfg(not(feature = "mock"))]
fn verify_native_proof(&self, vk_digest: &[u32; 8], public_values: &[u8]) {
let pv_digest = Sha256::digest(public_values);
verify_sp1_proof(vk_digest, &pv_digest.into());
}

#[cfg(feature = "mock")]
fn verify_native_proof(&self, _vk_digest: &[u32; 8], _public_values: &[u8]) {}

#[cfg(not(feature = "mock"))]
fn verify_groth16_proof(
&self,
proof: &Proof,
Expand All @@ -38,6 +47,15 @@ impl ZkVmEnv for Sp1ZkVmEnv {
verify_groth16(proof, verification_key, public_params_raw).unwrap();
}

#[cfg(feature = "mock")]
fn verify_groth16_proof(
&self,
_proof: &Proof,
_verification_key: &[u8],
_public_params_raw: &[u8],
) {
}

fn read_verified_serde<T: DeserializeOwned>(&self, vk_digest: &[u32; 8]) -> T {
let buf = self.read_verified_buf(vk_digest);
bincode::deserialize(&buf).expect("bincode deserialization failed")
Expand Down
5 changes: 5 additions & 0 deletions crates/zkvm/adapters/sp1/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ impl ZkVmHost for SP1Host {
prover_input: <Self::Input<'a> as ZkVmInputBuilder<'a>>::Input,
proof_type: ProofType,
) -> ZkVmResult<(Proof, VerificationKey)> {
#[cfg(feature = "mock")]
{
std::env::set_var("SP1_PROVER", "mock");
}

sp1_sdk::utils::setup_logger();
let client = ProverClient::new();

Expand Down
3 changes: 2 additions & 1 deletion provers/sp1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ sp1-sdk = "3.0.0"

[features]
default = ["prover"]
prover = ["dep:strata-sp1-adapter"]
prover = ["strata-sp1-adapter"]
mock = []
12 changes: 12 additions & 0 deletions provers/sp1/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,21 @@ fn ensure_cache_validity(program: &str) -> Result<SP1VerifyingKey, String> {
/// Generates the ELF contents and VK hash for a given program.
#[cfg(not(debug_assertions))]
fn generate_elf_contents_and_vk_hash(program: &str) -> ([u32; 8], String) {
let features = {
#[cfg(feature = "mock")]
{
vec!["mock".to_string()]
}
#[cfg(not(feature = "mock"))]
{
vec![]
}
};

let build_args = BuildArgs {
elf_name: format!("{}.elf", program),
output_directory: "cache".to_owned(),
features,
..Default::default()
};

Expand Down
3 changes: 3 additions & 0 deletions provers/sp1/guest-btc-blockspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ strata-sp1-adapter = { path = "../../../crates/zkvm/adapters/sp1", features = [
[patch.crates-io]
secp256k1 = { git = "https://github.com/sp1-patches/rust-secp256k1", branch = "patch-secp256k1-v0.29.1" }
sha2-v0-10-8 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", branch = "patch-sha2-v0.10.8" }

[features]
mock = ["strata-sp1-adapter/mock"]
3 changes: 3 additions & 0 deletions provers/sp1/guest-checkpoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ strata-sp1-adapter = { path = "../../../crates/zkvm/adapters/sp1", features = [
[patch.crates-io]
secp256k1 = { git = "https://github.com/sp1-patches/rust-secp256k1", branch = "patch-secp256k1-v0.29.1" }
sha2-v0-10-8 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", branch = "patch-sha2-v0.10.8" }

[features]
mock = ["strata-sp1-adapter/mock"]
3 changes: 3 additions & 0 deletions provers/sp1/guest-cl-agg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ strata-sp1-adapter = { path = "../../../crates/zkvm/adapters/sp1", features = [
[patch.crates-io]
secp256k1 = { git = "https://github.com/sp1-patches/rust-secp256k1", branch = "patch-secp256k1-v0.29.1" }
sha2-v0-10-8 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", branch = "patch-sha2-v0.10.8" }

[features]
mock = ["strata-sp1-adapter/mock"]
3 changes: 3 additions & 0 deletions provers/sp1/guest-cl-stf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ strata-sp1-adapter = { path = "../../../crates/zkvm/adapters/sp1", features = [
[patch.crates-io]
secp256k1 = { git = "https://github.com/sp1-patches/rust-secp256k1", branch = "patch-secp256k1-v0.29.1" }
sha2-v0-10-8 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", branch = "patch-sha2-v0.10.8" }

[features]
mock = ["strata-sp1-adapter/mock"]
3 changes: 3 additions & 0 deletions provers/sp1/guest-evm-ee-stf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ revm-primitives = { git = "https://github.com/sp1-patches/revm-new", branch = "j
secp256k1 = { git = "https://github.com/sp1-patches/rust-secp256k1", branch = "patch-secp256k1-v0.29.1" }
sha2-v0-10-8 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", branch = "patch-sha2-v0.10.8" }
tiny-keccak = { git = "https://github.com/sp1-patches/tiny-keccak", branch = "patch-v2.0.2" }

[features]
mock = ["strata-sp1-adapter/mock"]
3 changes: 3 additions & 0 deletions provers/sp1/guest-l1-batch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ strata-sp1-adapter = { path = "../../../crates/zkvm/adapters/sp1", features = [
[patch.crates-io]
secp256k1 = { git = "https://github.com/sp1-patches/rust-secp256k1", branch = "patch-secp256k1-v0.29.1" }
sha2-v0-10-8 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", branch = "patch-sha2-v0.10.8" }

[features]
mock = ["strata-sp1-adapter/mock"]
7 changes: 6 additions & 1 deletion provers/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ strata-risc0-guest-builder = { path = "../risc0", optional = true, features = [
] }

[features]
default = ["sp1"]
sp1 = ["strata-sp1-adapter", "strata-sp1-guest-builder"]
risc0 = ["strata-risc0-adapter", "strata-risc0-guest-builder"]
default = ["sp1"]
mock = [
"strata-sp1-guest-builder/mock",
"strata-sp1-adapter/mock",
"strata-risc0-adapter/mock",
]

0 comments on commit 0ea3c38

Please sign in to comment.