Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: union proof type relevant stuff #422

Merged
merged 5 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 9 additions & 5 deletions lib/src/proof_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@ 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,
#[serde(alias = "NATIVE")]
Native = 0u8,
/// # Sp1
///
/// Uses the SP1 prover to build the block.
Sp1,
#[serde(alias = "SP1")]
Sp1 = 1u8,
/// # Sgx
///
/// Builds the block on a SGX supported CPU to create a proof.
Sgx,
#[serde(alias = "SGX")]
Sgx = 2u8,
/// # Risc0
///
/// Uses the RISC0 prover to build the block.
Risc0,
#[serde(alias = "RISC0")]
Risc0 = 3u8,
}

impl std::fmt::Display for ProofType {
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
5 changes: 2 additions & 3 deletions provers/risc0/driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -60,8 +61,6 @@ impl From<Risc0Response> for Proof {

pub struct Risc0Prover;

const RISC0_PROVER_CODE: u8 = 3;

impl Prover for Risc0Prover {
async fn run(
input: GuestInput,
Expand All @@ -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());
Expand Down
6 changes: 3 additions & 3 deletions provers/risc0/guest/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();

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
4 changes: 2 additions & 2 deletions provers/sp1/driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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)]
Expand Down Expand Up @@ -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(),
)
Expand Down
4 changes: 2 additions & 2 deletions provers/sp1/driver/src/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![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;
use raiko_lib::prover::Prover;
use raiko_lib::Measurement;
Expand Down Expand Up @@ -39,7 +39,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
4 changes: 2 additions & 2 deletions provers/sp1/guest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -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();
Expand Down
Loading