From 51dfc702a5870c6e3062677ec0add681cc9dd71c Mon Sep 17 00:00:00 2001 From: "keroroxx520@gmail.com" Date: Thu, 28 Nov 2024 14:41:03 +0800 Subject: [PATCH 1/5] feat(lib): repr ProofType in u8 --- lib/src/proof_type.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/src/proof_type.rs b/lib/src/proof_type.rs index c8a261fb..74a3ef66 100644 --- a/lib/src/proof_type.rs +++ b/lib/src/proof_type.rs @@ -3,25 +3,25 @@ use serde::{Deserialize, Serialize}; #[derive( PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Default, Deserialize, Serialize, Hash, Copy, )] -/// Available proof types. +#[repr(u8)] pub enum ProofType { #[default] /// # Native /// /// This builds the block the same way the node does and then runs the result. - Native, + Native = 0u8, /// # Sp1 /// /// Uses the SP1 prover to build the block. - Sp1, + Sp1 = 1u8, /// # Sgx /// /// Builds the block on a SGX supported CPU to create a proof. - Sgx, + Sgx = 2u8, /// # Risc0 /// /// Uses the RISC0 prover to build the block. - Risc0, + Risc0 = 3u8, } impl std::fmt::Display for ProofType { From 5363dee92da78963878771b5ccfc4446bb87ee9d Mon Sep 17 00:00:00 2001 From: "keroroxx520@gmail.com" Date: Thu, 28 Nov 2024 17:44:34 +0800 Subject: [PATCH 2/5] feat(lib): serialize alias for ProofType --- lib/src/proof_type.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/src/proof_type.rs b/lib/src/proof_type.rs index 74a3ef66..c1c45dc7 100644 --- a/lib/src/proof_type.rs +++ b/lib/src/proof_type.rs @@ -9,18 +9,22 @@ pub enum ProofType { /// # Native /// /// This builds the block the same way the node does and then runs the result. + #[serde(alias = "NATIVE")] Native = 0u8, /// # Sp1 /// /// Uses the SP1 prover to build the block. + #[serde(alias = "SP1")] Sp1 = 1u8, /// # Sgx /// /// Builds the block on a SGX supported CPU to create a proof. + #[serde(alias = "SGX")] Sgx = 2u8, /// # Risc0 /// /// Uses the RISC0 prover to build the block. + #[serde(alias = "RISC0")] Risc0 = 3u8, } From 76cf73838311a3fa7933531c7262aa91f2052b0f Mon Sep 17 00:00:00 2001 From: "keroroxx520@gmail.com" Date: Thu, 28 Nov 2024 15:19:42 +0800 Subject: [PATCH 3/5] feat: apply proof type code --- provers/risc0/driver/src/lib.rs | 5 ++--- provers/sp1/driver/src/lib.rs | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/provers/risc0/driver/src/lib.rs b/provers/risc0/driver/src/lib.rs index 536e8ebe..18a34bdd 100644 --- a/provers/risc0/driver/src/lib.rs +++ b/provers/risc0/driver/src/lib.rs @@ -14,6 +14,7 @@ use raiko_lib::{ AggregationGuestInput, AggregationGuestOutput, GuestInput, GuestOutput, ZkAggregationGuestInput, }, + proof_type::ProofType, prover::{IdStore, IdWrite, Proof, ProofKey, Prover, ProverConfig, ProverError, ProverResult}, }; use risc0_zkvm::{ @@ -60,8 +61,6 @@ impl From for Proof { pub struct Risc0Prover; -const RISC0_PROVER_CODE: u8 = 3; - impl Prover for Risc0Prover { async fn run( input: GuestInput, @@ -75,7 +74,7 @@ impl Prover for Risc0Prover { input.chain_spec.chain_id, input.block.header.number, output.hash, - RISC0_PROVER_CODE, + ProofType::Risc0 as u8, ); debug!("elf code length: {}", RISC0_GUEST_ELF.len()); diff --git a/provers/sp1/driver/src/lib.rs b/provers/sp1/driver/src/lib.rs index 9f5fd26f..cf0fcf10 100644 --- a/provers/sp1/driver/src/lib.rs +++ b/provers/sp1/driver/src/lib.rs @@ -6,6 +6,7 @@ use raiko_lib::{ AggregationGuestInput, AggregationGuestOutput, GuestInput, GuestOutput, ZkAggregationGuestInput, }, + proof_type::ProofType, prover::{IdStore, IdWrite, Proof, ProofKey, Prover, ProverConfig, ProverError, ProverResult}, Measurement, }; @@ -27,7 +28,6 @@ use proof_verify::remote_contract_verify::verify_sol_by_contract_call; pub const ELF: &[u8] = include_bytes!("../../guest/elf/sp1-guest"); pub const AGGREGATION_ELF: &[u8] = include_bytes!("../../guest/elf/sp1-aggregation"); -const SP1_PROVER_CODE: u8 = 1; #[serde_as] #[derive(Clone, Debug, Serialize, Deserialize)] @@ -156,7 +156,7 @@ impl Prover for Sp1Prover { input.chain_spec.chain_id, input.block.header.number, output.hash, - SP1_PROVER_CODE, + ProofType::Sp1 as u8, ), proof_id.clone(), ) From 25115e672408b27c974ac703d7a837bf2614571f Mon Sep 17 00:00:00 2001 From: "keroroxx520@gmail.com" Date: Thu, 28 Nov 2024 15:41:04 +0800 Subject: [PATCH 4/5] feat: union VerifierType and ProofType --- core/src/lib.rs | 2 +- core/src/prover.rs | 4 +- lib/src/consts.rs | 55 +++++++----------------- lib/src/protocol_instance.rs | 15 ++++--- provers/sgx/guest/src/one_shot.rs | 4 +- provers/sgx/setup/src/setup_bootstrap.rs | 6 +-- provers/sp1/driver/src/verifier.rs | 3 +- 7 files changed, 34 insertions(+), 55 deletions(-) diff --git a/core/src/lib.rs b/core/src/lib.rs index 46113094..fa7adc02 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -91,7 +91,7 @@ impl Raiko { Ok(GuestOutput { header: header.clone(), - hash: ProtocolInstance::new(input, &header, self.request.proof_type.into())? + hash: ProtocolInstance::new(input, &header, self.request.proof_type)? .instance_hash(), }) } diff --git a/core/src/prover.rs b/core/src/prover.rs index de89d859..88581a6e 100644 --- a/core/src/prover.rs +++ b/core/src/prover.rs @@ -1,8 +1,8 @@ use std::path::Path; use raiko_lib::{ - consts::VerifierType, input::{GuestInput, GuestOutput}, + proof_type::ProofType, protocol_instance::ProtocolInstance, prover::{IdStore, IdWrite, Proof, ProofKey, Prover, ProverConfig, ProverError, ProverResult}, }; @@ -49,7 +49,7 @@ impl Prover for NativeProver { trace!("Running the native prover for input {input:?}"); - let pi = ProtocolInstance::new(&input, &output.header, VerifierType::None) + let pi = ProtocolInstance::new(&input, &output.header, ProofType::Native) .map_err(|e| ProverError::GuestError(e.to_string()))?; if pi.instance_hash() != output.hash { return Err(ProverError::GuestError( diff --git a/lib/src/consts.rs b/lib/src/consts.rs index b73ed43f..98394833 100644 --- a/lib/src/consts.rs +++ b/lib/src/consts.rs @@ -1,23 +1,20 @@ //! Constants for the Ethereum protocol. extern crate alloc; +use crate::primitives::{uint, BlockNumber, ChainId, U256}; +use crate::proof_type::ProofType; use alloc::collections::BTreeMap; - use alloy_primitives::Address; use anyhow::{anyhow, bail, Result}; +use once_cell::sync::Lazy; use reth_primitives::revm_primitives::SpecId; use serde::{Deserialize, Serialize}; use serde_json::Value; - -#[cfg(not(feature = "std"))] -use crate::no_std::*; -use crate::primitives::{uint, BlockNumber, ChainId, U256}; - -use once_cell::sync::Lazy; use std::path::PathBuf; use std::{collections::HashMap, env::var}; -use crate::proof_type::ProofType; +#[cfg(not(feature = "std"))] +use crate::no_std::*; /// U256 representation of 0. pub const ZERO: U256 = U256::ZERO; @@ -129,26 +126,6 @@ impl Default for Eip1559Constants { } } -#[repr(u8)] -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] -pub enum VerifierType { - None, - SGX, - SP1, - RISC0, -} - -impl From for VerifierType { - fn from(val: ProofType) -> Self { - match val { - ProofType::Native => VerifierType::None, - ProofType::Sgx => VerifierType::SGX, - ProofType::Sp1 => VerifierType::SP1, - ProofType::Risc0 => VerifierType::RISC0, - } - } -} - /// Specification of a specific chain. #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)] pub struct ChainSpec { @@ -161,7 +138,7 @@ pub struct ChainSpec { pub l2_contract: Option
, pub rpc: String, pub beacon_rpc: Option, - pub verifier_address_forks: BTreeMap>>, + pub verifier_address_forks: BTreeMap>>, pub genesis_time: u64, pub seconds_per_slot: u64, pub is_taiko: bool, @@ -229,14 +206,14 @@ impl ChainSpec { pub fn get_fork_verifier_address( &self, block_num: u64, - verifier_type: VerifierType, + proof_type: ProofType, ) -> Result
{ // fall down to the first fork that is active as default for (spec_id, fork) in self.hard_forks.iter().rev() { if fork.active(block_num, 0u64) { if let Some(fork_verifier) = self.verifier_address_forks.get(spec_id) { return fork_verifier - .get(&verifier_type) + .get(&proof_type) .ok_or_else(|| anyhow!("Verifier type not found")) .and_then(|address| { address.ok_or_else(|| anyhow!("Verifier address not found")) @@ -344,7 +321,7 @@ mod tests { .get_chain_spec(&Network::Ethereum.to_string()) .unwrap(); let verifier_address = eth_mainnet_spec - .get_fork_verifier_address(15_537_394, VerifierType::SGX) + .get_fork_verifier_address(15_537_394, ProofType::Sgx) .unwrap(); assert_eq!( verifier_address, @@ -355,14 +332,14 @@ mod tests { .get_chain_spec(&Network::TaikoA7.to_string()) .unwrap(); let verifier_address = hekla_mainnet_spec - .get_fork_verifier_address(12345, VerifierType::SGX) + .get_fork_verifier_address(12345, ProofType::Sgx) .unwrap(); assert_eq!( verifier_address, address!("532efbf6d62720d0b2a2bb9d11066e8588cae6d9") ); let verifier_address = hekla_mainnet_spec - .get_fork_verifier_address(15_537_394, VerifierType::SGX) + .get_fork_verifier_address(15_537_394, ProofType::Sgx) .unwrap(); assert_eq!( verifier_address, @@ -371,12 +348,12 @@ mod tests { } #[test] - fn forked_none_verifier_address() { + fn forked_native_verifier_address() { let eth_mainnet_spec = SupportedChainSpecs::default() .get_chain_spec(&Network::Ethereum.to_string()) .unwrap(); let verifier_address = eth_mainnet_spec - .get_fork_verifier_address(15_537_394, VerifierType::None) + .get_fork_verifier_address(15_537_394, ProofType::Native) .unwrap_or_default(); assert_eq!(verifier_address, Address::ZERO); } @@ -407,9 +384,9 @@ mod tests { verifier_address_forks: BTreeMap::from([( SpecId::FRONTIER, BTreeMap::from([ - (VerifierType::SGX, Some(Address::default())), - (VerifierType::SP1, None), - (VerifierType::RISC0, Some(Address::default())), + (ProofType::Sgx, Some(Address::default())), + (ProofType::Sp1, None), + (ProofType::Risc0, Some(Address::default())), ]), )]), genesis_time: 0u64, diff --git a/lib/src/protocol_instance.rs b/lib/src/protocol_instance.rs index c779735d..019812b4 100644 --- a/lib/src/protocol_instance.rs +++ b/lib/src/protocol_instance.rs @@ -6,7 +6,7 @@ use reth_primitives::Header; #[cfg(not(feature = "std"))] use crate::no_std::*; use crate::{ - consts::{SupportedChainSpecs, VerifierType}, + consts::SupportedChainSpecs, input::{ ontake::{BlockMetadataV2, BlockProposedV2}, BlobProofType, BlockMetadata, BlockProposed, BlockProposedFork, EthDeposit, GuestInput, @@ -16,6 +16,7 @@ use crate::{ eip4844::{self, commitment_to_version_hash}, keccak::keccak, }, + proof_type::ProofType, CycleTracker, }; use reth_evm_ethereum::taiko::ANCHOR_GAS_LIMIT; @@ -138,7 +139,7 @@ pub struct ProtocolInstance { } impl ProtocolInstance { - pub fn new(input: &GuestInput, header: &Header, proof_type: VerifierType) -> Result { + pub fn new(input: &GuestInput, header: &Header, proof_type: ProofType) -> Result { let blob_used = input.taiko.block_proposed.blob_used(); // If blob is used, tx_list_hash is the commitment to the blob // and we need to verify the blob hash matches the blob data. @@ -307,16 +308,16 @@ impl ProtocolInstance { // Make sure the verifier supports the blob proof type fn get_blob_proof_type( - proof_type: VerifierType, + proof_type: ProofType, blob_proof_type_hint: BlobProofType, ) -> BlobProofType { // Enforce different blob proof type for different provers // due to performance considerations match proof_type { - VerifierType::None => blob_proof_type_hint, - VerifierType::SGX => BlobProofType::KzgVersionedHash, - VerifierType::SP1 => BlobProofType::ProofOfEquivalence, - VerifierType::RISC0 => BlobProofType::ProofOfEquivalence, + ProofType::Native => blob_proof_type_hint, + ProofType::Sgx => BlobProofType::KzgVersionedHash, + ProofType::Sp1 => BlobProofType::ProofOfEquivalence, + ProofType::Risc0 => BlobProofType::ProofOfEquivalence, } } diff --git a/provers/sgx/guest/src/one_shot.rs b/provers/sgx/guest/src/one_shot.rs index 156f92f9..e3d9c0e2 100644 --- a/provers/sgx/guest/src/one_shot.rs +++ b/provers/sgx/guest/src/one_shot.rs @@ -9,9 +9,9 @@ use anyhow::{anyhow, bail, Context, Error, Result}; use base64_serde::base64_serde_type; use raiko_lib::{ builder::calculate_block_header, - consts::VerifierType, input::{GuestInput, RawAggregationGuestInput}, primitives::{keccak, Address, B256}, + proof_type::ProofType, protocol_instance::{aggregation_output_combine, ProtocolInstance}, }; use secp256k1::{Keypair, SecretKey}; @@ -134,7 +134,7 @@ pub async fn one_shot(global_opts: GlobalOpts, args: OneShotArgs) -> Result<()> // Process the block let header = calculate_block_header(&input); // Calculate the public input hash - let pi = ProtocolInstance::new(&input, &header, VerifierType::SGX)?.sgx_instance(new_instance); + let pi = ProtocolInstance::new(&input, &header, ProofType::Sgx)?.sgx_instance(new_instance); let pi_hash = pi.instance_hash(); println!( diff --git a/provers/sgx/setup/src/setup_bootstrap.rs b/provers/sgx/setup/src/setup_bootstrap.rs index 1958b167..31dcae80 100644 --- a/provers/sgx/setup/src/setup_bootstrap.rs +++ b/provers/sgx/setup/src/setup_bootstrap.rs @@ -8,7 +8,7 @@ use std::{ use anyhow::{anyhow, Context, Result}; use file_lock::{FileLock, FileOptions}; -use raiko_lib::consts::{SupportedChainSpecs, VerifierType}; +use raiko_lib::{consts::SupportedChainSpecs, proof_type::ProofType}; use serde_json::{Number, Value}; use sgx_prover::{ bootstrap, check_bootstrap, get_instance_id, register_sgx_instance, remove_instance_id, @@ -69,8 +69,8 @@ pub(crate) async fn setup_bootstrap( // clean check file remove_instance_id(&config_dir)?; let bootstrap_proof = bootstrap(secret_dir, gramine_cmd()).await?; - let verifier_address = taiko_chain_spec - .get_fork_verifier_address(bootstrap_args.block_num, VerifierType::SGX)?; + let verifier_address = + taiko_chain_spec.get_fork_verifier_address(bootstrap_args.block_num, ProofType::Sgx)?; let register_id = register_sgx_instance( &bootstrap_proof.quote, &l1_chain_spec.rpc, diff --git a/provers/sp1/driver/src/verifier.rs b/provers/sp1/driver/src/verifier.rs index 20c760e9..9b5c9533 100644 --- a/provers/sp1/driver/src/verifier.rs +++ b/provers/sp1/driver/src/verifier.rs @@ -3,6 +3,7 @@ use alloy_primitives::B256; use raiko_lib::builder::calculate_block_header; use raiko_lib::consts::VerifierType; use raiko_lib::input::{BlobProofType, GuestInput, GuestOutput}; +use raiko_lib::proof_type::ProofType; use raiko_lib::protocol_instance::ProtocolInstance; use raiko_lib::prover::Prover; use raiko_lib::Measurement; @@ -39,7 +40,7 @@ async fn main() { let header = calculate_block_header(&input); - let _pi = ProtocolInstance::new(&input, &header, VerifierType::SP1) + let _pi = ProtocolInstance::new(&input, &header, ProofType::SP1) .unwrap() .instance_hash(); From e61a2661a37146b05f40c2575841c46b7cc7045e Mon Sep 17 00:00:00 2001 From: "keroroxx520@gmail.com" Date: Thu, 28 Nov 2024 18:46:31 +0800 Subject: [PATCH 5/5] chore: fix lint errors --- provers/risc0/guest/src/main.rs | 6 +++--- provers/sp1/driver/src/verifier.rs | 1 - provers/sp1/guest/src/main.rs | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/provers/risc0/guest/src/main.rs b/provers/risc0/guest/src/main.rs index 18a58c7a..4c00471f 100644 --- a/provers/risc0/guest/src/main.rs +++ b/provers/risc0/guest/src/main.rs @@ -1,12 +1,12 @@ #![no_main] harness::entrypoint!(main, tests, zk_op::tests); use raiko_lib::{ - builder::calculate_block_header, consts::VerifierType, input::GuestInput, + builder::calculate_block_header, input::GuestInput, proof_type::ProofType, protocol_instance::ProtocolInstance, }; use revm_precompile::zk_op::ZkOperation; -use zk_op::Risc0Operator; use risc0_zkvm::guest::env; +use zk_op::Risc0Operator; pub mod mem; @@ -21,7 +21,7 @@ fn main() { .expect("Failed to set ZkvmOperations"); let header = calculate_block_header(&input); - let pi = ProtocolInstance::new(&input, &header, VerifierType::RISC0) + let pi = ProtocolInstance::new(&input, &header, ProofType::Risc0) .unwrap() .instance_hash(); diff --git a/provers/sp1/driver/src/verifier.rs b/provers/sp1/driver/src/verifier.rs index 9b5c9533..ef1a5740 100644 --- a/provers/sp1/driver/src/verifier.rs +++ b/provers/sp1/driver/src/verifier.rs @@ -1,7 +1,6 @@ #![cfg(feature = "enable")] use alloy_primitives::B256; use raiko_lib::builder::calculate_block_header; -use raiko_lib::consts::VerifierType; use raiko_lib::input::{BlobProofType, GuestInput, GuestOutput}; use raiko_lib::proof_type::ProofType; use raiko_lib::protocol_instance::ProtocolInstance; diff --git a/provers/sp1/guest/src/main.rs b/provers/sp1/guest/src/main.rs index c6ef522d..dc299ab3 100644 --- a/provers/sp1/guest/src/main.rs +++ b/provers/sp1/guest/src/main.rs @@ -2,7 +2,7 @@ harness::entrypoint!(main, tests, zk_op::tests); use raiko_lib::{ - builder::calculate_block_header, consts::VerifierType, input::GuestInput, + builder::calculate_block_header, input::GuestInput, proof_type::ProofType, protocol_instance::ProtocolInstance, CycleTracker, }; @@ -20,7 +20,7 @@ pub fn main() { ct.end(); ct = CycleTracker::start("ProtocolInstance"); - let pi = ProtocolInstance::new(&input, &header, VerifierType::SP1) + let pi = ProtocolInstance::new(&input, &header, ProofType::Sp1) .unwrap() .instance_hash(); ct.end();