Skip to content

Commit

Permalink
Expand Prover-Client to Support Multiple ZkVm (#516)
Browse files Browse the repository at this point in the history
* feat: add risc0 and sp1 as features

* feat: support multiple hosts
  • Loading branch information
prajwolrg authored Dec 5, 2024
1 parent e0afba5 commit 7ef0695
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 67 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

73 changes: 46 additions & 27 deletions bin/prover-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@ path = "src/main.rs"
required-features = ["prover"]

[dependencies]
strata-btcio = { workspace = true }
strata-common = { workspace = true }
strata-db = { workspace = true }
strata-primitives = { workspace = true }
strata-proofimpl-btc-blockspace = { workspace = true }
strata-proofimpl-checkpoint = { workspace = true }
strata-proofimpl-evm-ee-stf = { workspace = true }
strata-proofimpl-l1-batch = { workspace = true }
strata-prover-client-rpc-api = { workspace = true }
strata-rocksdb = { workspace = true }
strata-rpc-types = { workspace = true }
strata-sp1-adapter = { workspace = true, features = ["prover"] }
strata-sp1-guest-builder = { path = "../../provers/sp1" }
strata-state = { workspace = true }
strata-zkvm = { workspace = true }
strata-btcio.workspace = true
strata-common.workspace = true
strata-db.workspace = true
strata-native-zkvm-adapter.workspace = true
strata-primitives.workspace = true
strata-proofimpl-btc-blockspace.workspace = true
strata-proofimpl-checkpoint.workspace = true
strata-proofimpl-evm-ee-stf.workspace = true
strata-proofimpl-l1-batch.workspace = true
strata-prover-client-rpc-api.workspace = true
strata-rocksdb.workspace = true
strata-rpc-types.workspace = true
strata-state.workspace = true
strata-zkvm.workspace = true

alloy-rpc-types = { workspace = true }
alloy-rpc-types.workspace = true
anyhow.workspace = true
argh.workspace = true
async-trait.workspace = true
Expand All @@ -35,19 +34,39 @@ borsh.workspace = true
hex.workspace = true
jsonrpsee = { workspace = true, features = ["http-client"] }
musig2.workspace = true
num-bigint = "0.4.6"
num-traits = "0.2.19"
rand.workspace = true
rayon = "1.8.0"
rockbound = { workspace = true }
serde = { workspace = true }
sp1-sdk = "3.0.0"
strata-tx-parser = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
rockbound.workspace = true
serde.workspace = true
strata-tx-parser.workspace = true
thiserror.workspace = true
tokio.workspace = true
tracing.workspace = true
tracing-subscriber = { workspace = true, features = ["env-filter"] }
uuid = { workspace = true }
uuid.workspace = true

# sp1
strata-sp1-adapter = { workspace = true, optional = true, features = [
"prover",
] }
strata-sp1-guest-builder = { path = "../../provers/sp1", optional = true, features = [
"prover",
] }

# risc0
strata-risc0-adapter = { workspace = true, optional = true, features = [
"prover",
] }
strata-risc0-guest-builder = { path = "../../provers/risc0", optional = true, features = [
"prover",
] }

[features]
prover = []
default = ["sp1"]
mock = [
"strata-sp1-guest-builder/mock",
"strata-sp1-adapter/mock",
"strata-risc0-adapter/mock",
]
risc0 = ["strata-risc0-adapter", "strata-risc0-guest-builder"]
sp1 = ["strata-sp1-adapter", "strata-sp1-guest-builder"]
1 change: 1 addition & 0 deletions bin/prover-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod prover;
mod proving_ops;
mod rpc_server;
mod task;
mod zkvm;

#[tokio::main]
async fn main() {
Expand Down
5 changes: 4 additions & 1 deletion bin/prover-client/src/primitives/vms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::{collections::HashMap, hash::Hash};

use strata_zkvm::ZkVmHost;

use crate::zkvm;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ProofVm {
BtcProving,
Expand All @@ -23,9 +25,10 @@ impl<Vm: ZkVmHost> ZkVMManager<Vm> {
}
}

pub fn add_vm(&mut self, proof_vm: ProofVm, vm: Vm) {
pub fn add_vm(&mut self, proof_vm: ProofVm) {
// The `Vm` is expected to live for the lifetime of the ProverManager, ensuring the same
// instance is reused to prove the same guest program
let vm = zkvm::get_host(proof_vm);
let vm = Box::new(vm);
let static_vm: &'static Vm = Box::leak(vm);
self.vms.insert(proof_vm, static_vm);
Expand Down
42 changes: 6 additions & 36 deletions bin/prover-client/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,42 +186,12 @@ impl Prover {
let db = ProofDb::new(rbdb, db_ops);

let mut zkvm_manager = ZkVMManager::new();
zkvm_manager.add_vm(
ProofVm::BtcProving,
SP1Host::new_from_bytes(
&GUEST_BTC_BLOCKSPACE_ELF,
&GUEST_BTC_BLOCKSPACE_PK,
&GUEST_BTC_BLOCKSPACE_VK,
),
);
zkvm_manager.add_vm(
ProofVm::L1Batch,
SP1Host::new_from_bytes(&GUEST_L1_BATCH_ELF, &GUEST_L1_BATCH_PK, &GUEST_L1_BATCH_VK),
);
zkvm_manager.add_vm(
ProofVm::ELProving,
SP1Host::new_from_bytes(
&GUEST_EVM_EE_STF_ELF,
&GUEST_EVM_EE_STF_PK,
&GUEST_EVM_EE_STF_VK,
),
);
zkvm_manager.add_vm(
ProofVm::CLProving,
SP1Host::new_from_bytes(&GUEST_CL_STF_ELF, &GUEST_CL_STF_PK, &GUEST_CL_STF_VK),
);
zkvm_manager.add_vm(
ProofVm::CLAggregation,
SP1Host::new_from_bytes(&GUEST_CL_AGG_ELF, &GUEST_CL_AGG_PK, &GUEST_CL_AGG_VK),
);
zkvm_manager.add_vm(
ProofVm::Checkpoint,
SP1Host::new_from_bytes(
&GUEST_CHECKPOINT_ELF,
&GUEST_CHECKPOINT_PK,
&GUEST_CHECKPOINT_VK,
),
);
zkvm_manager.add_vm(ProofVm::BtcProving);
zkvm_manager.add_vm(ProofVm::L1Batch);
zkvm_manager.add_vm(ProofVm::ELProving);
zkvm_manager.add_vm(ProofVm::CLProving);
zkvm_manager.add_vm(ProofVm::CLAggregation);
zkvm_manager.add_vm(ProofVm::Checkpoint);

Self {
pool: rayon::ThreadPoolBuilder::new()
Expand Down
31 changes: 31 additions & 0 deletions bin/prover-client/src/zkvm/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use strata_zkvm::ZkVmHost;

use crate::primitives::vms::ProofVm;

pub mod native;
#[cfg(feature = "risc0")]
pub mod risc0;
#[cfg(feature = "sp1")]
pub mod sp1;

#[cfg(all(feature = "risc0", not(feature = "sp1")))]
pub fn get_host(vm: ProofVm) -> impl ZkVmHost {
risc0::get_host(vm)
}

#[cfg(all(feature = "sp1", not(feature = "risc0")))]
pub fn get_host(vm: ProofVm) -> impl ZkVmHost {
sp1::get_host(vm)
}

// Native Host is used if both risc0 and sp1 are disabled
#[cfg(all(not(feature = "sp1"), not(feature = "risc0")))]
pub fn get_host(vm: ProofVm) -> impl ZkVmHost {
native::get_host(vm)
}

// Use SP1 if both risc0 and sp1 are enabled
#[cfg(all(feature = "sp1", feature = "risc0"))]
pub fn get_host(vm: ProofVm) -> impl ZkVmHost {
sp1::get_host(vm)
}
56 changes: 56 additions & 0 deletions bin/prover-client/src/zkvm/native.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::sync::Arc;

use strata_native_zkvm_adapter::{NativeHost, NativeMachine};
use strata_proofimpl_btc_blockspace::logic::process_blockspace_proof_outer;
use strata_proofimpl_checkpoint::process_checkpoint_proof_outer;
use strata_proofimpl_cl_agg::process_cl_agg;
use strata_proofimpl_cl_stf::process_cl_stf;
use strata_proofimpl_evm_ee_stf::process_block_transaction_outer;
use strata_proofimpl_l1_batch::process_l1_batch_proof;

/// A mock verification key used in native mode when proof verification is not performed.
///
/// This constant provides a placeholder value for scenarios where a verification key is
/// required by a function signature, but actual verification is skipped.
const MOCK_VK: [u32; 8] = [0u32; 8];

pub fn get_host(vm: ProofVm) -> impl ZkVmHost {
match vm {
ProofVm::BtcProving => NativeHost {
process_proof: Arc::new(Box::new(move |zkvm: &NativeMachine| {
process_blockspace_proof_outer(zkvm);
Ok(())
})),
},
ProofVm::L1Batch => NativeHost {
process_proof: Arc::new(Box::new(move |zkvm: &NativeMachine| {
process_l1_batch_proof(zkvm, &MOCK_VK);
Ok(())
})),
},
ProofVm::ELProving => NativeHost {
process_proof: Arc::new(Box::new(move |zkvm: &NativeMachine| {
process_block_transaction_outer(zkvm);
Ok(())
})),
},
ProofVm::CLProving => NativeHost {
process_proof: Arc::new(Box::new(move |zkvm: &NativeMachine| {
process_cl_stf(zkvm, &MOCK_VK);
Ok(())
})),
},
ProofVm::CLAggregation => NativeHost {
process_proof: Arc::new(Box::new(move |zkvm: &NativeMachine| {
process_cl_agg(zkvm, &MOCK_VK);
Ok(())
})),
},
ProofVm::Checkpoint => NativeHost {
process_proof: Arc::new(Box::new(move |zkvm: &NativeMachine| {
process_checkpoint_proof_outer(zkvm, &MOCK_VK, &MOCK_VK);
Ok(())
})),
},
}
}
16 changes: 16 additions & 0 deletions bin/prover-client/src/zkvm/risc0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use strata_risc0_adapter::Risc0Host;
use strata_risc0_guest_builder::{
GUEST_RISC0_BTC_BLOCKSPACE_ELF, GUEST_RISC0_CHECKPOINT_ELF, GUEST_RISC0_CL_AGG_ELF,
GUEST_RISC0_CL_STF_ELF, GUEST_RISC0_EVM_EE_STF_ELF, GUEST_RISC0_L1_BATCH_ELF,
};

pub fn get_host(vm: ProofVm) -> impl ZkVmHost {
match vm {
ProofVm::BtcProving => Risc0Host::init(GUEST_RISC0_BTC_BLOCKSPACE_ELF),
ProofVm::L1Batch => Risc0Host::init(GUEST_RISC0_L1_BATCH_ELF),
ProofVm::ELProving => Risc0Host::init(GUEST_RISC0_EVM_EE_STF_ELF),
ProofVm::CLProving => Risc0Host::init(GUEST_RISC0_CL_STF_ELF),
ProofVm::CLAggregation => Risc0Host::init(GUEST_RISC0_CL_AGG_ELF),
ProofVm::Checkpoint => Risc0Host::init(GUEST_RISC0_CHECKPOINT_ELF),
}
}
36 changes: 36 additions & 0 deletions bin/prover-client/src/zkvm/sp1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use strata_sp1_adapter::SP1Host;
use strata_sp1_guest_builder::*;
use strata_zkvm::ZkVmHost;

use crate::primitives::vms::ProofVm;

pub fn get_host(vm: ProofVm) -> impl ZkVmHost {
match vm {
ProofVm::BtcProving => SP1Host::new_from_bytes(
&GUEST_BTC_BLOCKSPACE_ELF,
&GUEST_BTC_BLOCKSPACE_PK,
&GUEST_BTC_BLOCKSPACE_VK,
),
ProofVm::L1Batch => SP1Host::new_from_bytes(
&GUEST_BTC_BLOCKSPACE_ELF,
&GUEST_BTC_BLOCKSPACE_PK,
&GUEST_BTC_BLOCKSPACE_VK,
),
ProofVm::ELProving => SP1Host::new_from_bytes(
&GUEST_EVM_EE_STF_ELF,
&GUEST_EVM_EE_STF_PK,
&GUEST_EVM_EE_STF_VK,
),
ProofVm::CLProving => {
SP1Host::new_from_bytes(&GUEST_CL_STF_ELF, &GUEST_CL_STF_PK, &GUEST_CL_STF_VK)
}
ProofVm::CLAggregation => {
SP1Host::new_from_bytes(&GUEST_CL_AGG_ELF, &GUEST_CL_AGG_PK, &GUEST_CL_AGG_VK)
}
ProofVm::Checkpoint => SP1Host::new_from_bytes(
&GUEST_CHECKPOINT_ELF,
&GUEST_CHECKPOINT_PK,
&GUEST_CHECKPOINT_VK,
),
}
}

0 comments on commit 7ef0695

Please sign in to comment.