Skip to content

Commit

Permalink
feat: union VerifierType and ProofType
Browse files Browse the repository at this point in the history
  • Loading branch information
keroro520 committed Nov 28, 2024
1 parent 2c71c7a commit 378664f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 55 deletions.
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/prover.rs
Original file line number Diff line number Diff line change
@@ -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},
};
Expand Down Expand Up @@ -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(
Expand Down
55 changes: 16 additions & 39 deletions lib/src/consts.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<ProofType> 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 {
Expand All @@ -161,7 +138,7 @@ pub struct ChainSpec {
pub l2_contract: Option<Address>,
pub rpc: String,
pub beacon_rpc: Option<String>,
pub verifier_address_forks: BTreeMap<SpecId, BTreeMap<VerifierType, Option<Address>>>,
pub verifier_address_forks: BTreeMap<SpecId, BTreeMap<ProofType, Option<Address>>>,
pub genesis_time: u64,
pub seconds_per_slot: u64,
pub is_taiko: bool,
Expand Down Expand Up @@ -229,14 +206,14 @@ impl ChainSpec {
pub fn get_fork_verifier_address(
&self,
block_num: u64,
verifier_type: VerifierType,
proof_type: ProofType,
) -> Result<Address> {
// 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"))
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 8 additions & 7 deletions lib/src/protocol_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -138,7 +139,7 @@ pub struct ProtocolInstance {
}

impl ProtocolInstance {
pub fn new(input: &GuestInput, header: &Header, proof_type: VerifierType) -> Result<Self> {
pub fn new(input: &GuestInput, header: &Header, proof_type: ProofType) -> Result<Self> {
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.
Expand Down Expand Up @@ -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,
}
}

Expand Down
4 changes: 2 additions & 2 deletions provers/sgx/guest/src/one_shot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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!(
Expand Down
6 changes: 3 additions & 3 deletions provers/sgx/setup/src/setup_bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion provers/sp1/driver/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 378664f

Please sign in to comment.