diff --git a/Cargo.lock b/Cargo.lock index 0d6ef6223..5166f0b9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13305,22 +13305,22 @@ dependencies = [ "hex", "jsonrpsee", "musig2", - "num-bigint 0.4.6", - "num-traits", "rand", "rayon", "rockbound", "serde", - "sp1-sdk", "strata-btcio", "strata-common", "strata-db", + "strata-native-zkvm-adapter", "strata-primitives", "strata-proofimpl-btc-blockspace", "strata-proofimpl-checkpoint", "strata-proofimpl-evm-ee-stf", "strata-proofimpl-l1-batch", "strata-prover-client-rpc-api", + "strata-risc0-adapter", + "strata-risc0-guest-builder", "strata-rocksdb", "strata-rpc-types", "strata-sp1-adapter", diff --git a/bin/prover-client/Cargo.toml b/bin/prover-client/Cargo.toml index 222cce122..58d74ec33 100644 --- a/bin/prover-client/Cargo.toml +++ b/bin/prover-client/Cargo.toml @@ -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 @@ -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"] diff --git a/bin/prover-client/src/main.rs b/bin/prover-client/src/main.rs index 36b231f0c..81fddadd3 100644 --- a/bin/prover-client/src/main.rs +++ b/bin/prover-client/src/main.rs @@ -29,6 +29,7 @@ mod prover; mod proving_ops; mod rpc_server; mod task; +mod zkvm; #[tokio::main] async fn main() { diff --git a/bin/prover-client/src/primitives/vms.rs b/bin/prover-client/src/primitives/vms.rs index 5a03d32c6..918072435 100644 --- a/bin/prover-client/src/primitives/vms.rs +++ b/bin/prover-client/src/primitives/vms.rs @@ -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, @@ -23,9 +25,10 @@ impl ZkVMManager { } } - 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); diff --git a/bin/prover-client/src/prover.rs b/bin/prover-client/src/prover.rs index 703059db0..63f278cba 100644 --- a/bin/prover-client/src/prover.rs +++ b/bin/prover-client/src/prover.rs @@ -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() diff --git a/bin/prover-client/src/zkvm/mod.rs b/bin/prover-client/src/zkvm/mod.rs new file mode 100644 index 000000000..fdd94b725 --- /dev/null +++ b/bin/prover-client/src/zkvm/mod.rs @@ -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) +} diff --git a/bin/prover-client/src/zkvm/native.rs b/bin/prover-client/src/zkvm/native.rs new file mode 100644 index 000000000..26bc5143c --- /dev/null +++ b/bin/prover-client/src/zkvm/native.rs @@ -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(()) + })), + }, + } +} diff --git a/bin/prover-client/src/zkvm/risc0.rs b/bin/prover-client/src/zkvm/risc0.rs new file mode 100644 index 000000000..7e2e2f231 --- /dev/null +++ b/bin/prover-client/src/zkvm/risc0.rs @@ -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), + } +} diff --git a/bin/prover-client/src/zkvm/sp1.rs b/bin/prover-client/src/zkvm/sp1.rs new file mode 100644 index 000000000..28910a7dc --- /dev/null +++ b/bin/prover-client/src/zkvm/sp1.rs @@ -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, + ), + } +}