Skip to content

Commit

Permalink
feat(prover): bls12-381 & updates patches (#350)
Browse files Browse the repository at this point in the history
* 51 block test

* patches

* fix secp256k1

* script tweak

* cleanup

* update bn

* merge

* compile

* running

* sweep

* fmt

* add back zkop

* zkop

* update default chain spec

* est fork chain spec

* update

* add missing chainspec feature

* fix risc0 cargo.lock

* fix r0 compile

* update default chain spec

* est fork chain spec

* add missing chainspec feature

* update to ontake revm

Signed-off-by: smtmfft <[email protected]>

* update sp1 dependency

Signed-off-by: smtmfft <[email protected]>

* refine error return

Signed-off-by: smtmfft <[email protected]>

* test rpc

Signed-off-by: smtmfft <[email protected]>

* fix ci

Signed-off-by: smtmfft <[email protected]>

* fix rebase error

* update

* cargo lock

* update input & script
native proof ok

* update

* fix test_native_prover

* fix rpc

* Update host/config/chain_spec_list_devnet.json

Co-authored-by: Brecht Devos <[email protected]>

* fix beacon rpc url

* apply comment & update contracts

* fix contracts

* fix contracts

* fmt &clippy

* Delete core/input-taiko_mainnet-182300.json

* delete Groth16

* typos

* cargo fmt

* delete

* ignore test_native_prover

---------

Signed-off-by: smtmfft <[email protected]>
Co-authored-by: Chris Tian <[email protected]>
Co-authored-by: smtmfft <[email protected]>
Co-authored-by: smtmfft <[email protected]>
Co-authored-by: Brecht Devos <[email protected]>
  • Loading branch information
5 people authored Aug 29, 2024
1 parent 829609c commit 7356e8f
Show file tree
Hide file tree
Showing 52 changed files with 1,464 additions and 947 deletions.
1,380 changes: 888 additions & 492 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ reth-evm-ethereum = { git = "https://github.com/taikoxyz/taiko-reth.git", branch
reth-evm = { git = "https://github.com/taikoxyz/taiko-reth.git", branch = "v1.0.0-rc.2-taiko", default-features = false }
reth-rpc-types = { git = "https://github.com/taikoxyz/taiko-reth.git", branch = "v1.0.0-rc.2-taiko", default-features = false }
reth-revm = { git = "https://github.com/taikoxyz/taiko-reth.git", branch = "v1.0.0-rc.2-taiko", default-features = false }
reth-chainspec = { git = "https://github.com/taikoxyz/taiko-reth.git", branch = "v1.0.0-rc.2-taiko", default-features = false }
reth-chainspec = { git = "https://github.com/taikoxyz/taiko-reth.git", branch = "v1.0.0-rc.2-taiko", default-features = false, features = [
"taiko",
] }
reth-provider = { git = "https://github.com/taikoxyz/taiko-reth.git", branch = "v1.0.0-rc.2-taiko", default-features = false }

# risc zero
Expand All @@ -57,9 +59,9 @@ risc0-build = { version = "1.0.1" }
risc0-binfmt = { version = "1.0.1" }

# SP1
sp1-sdk = { version = "1.0.1" }
sp1-zkvm = { version = "1.0.1" }
sp1-helper = { version = "1.0.1" }
sp1-sdk = { git = "https://github.com/succinctlabs/sp1", branch = "dev" }
sp1-zkvm = { git = "https://github.com/succinctlabs/sp1", branch = "dev" }
sp1-helper = { git = "https://github.com/succinctlabs/sp1", branch = "dev" }

# alloy
alloy-rlp = { version = "0.3.4", default-features = false }
Expand Down Expand Up @@ -141,8 +143,8 @@ url = "2.5.0"
async-trait = "0.1.80"

# crypto
kzg = { package = "rust-kzg-zkcrypto", git = "https://github.com/brechtpd/rust-kzg.git", branch = "sp1-patch", default-features = false }
kzg_traits = { package = "kzg", git = "https://github.com/brechtpd/rust-kzg.git", branch = "sp1-patch", default-features = false }
kzg = { package = "rust-kzg-zkcrypto", git = "https://github.com/ceciliaz030/rust-kzg.git", branch = "brecht/sp1-patch", default-features = false }
kzg_traits = { package = "kzg", git = "https://github.com/ceciliaz030/rust-kzg.git", branch = "brecht/sp1-patch", default-features = false }
sha3 = { version = "0.10", default-features = false }
sha2 = "0.10.8"
secp256k1 = { version = "0.29", default-features = false, features = [
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ARG BUILD_FLAGS=""

WORKDIR /opt/raiko
COPY . .
RUN cargo build --release ${BUILD_FLAGS} --features "sgx,risc0" --features "docker_build"
RUN cargo build --release ${BUILD_FLAGS} --features "sgx" --features "docker_build"

FROM gramineproject/gramine:1.6-jammy as runtime
ENV DEBIAN_FRONTEND=noninteractive
Expand Down
20 changes: 1 addition & 19 deletions core/src/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ use clap::{Args, ValueEnum};
use raiko_lib::{
consts::VerifierType,
input::{BlobProofType, GuestInput, GuestOutput},
primitives::eip4844::{calc_kzg_proof, commitment_to_version_hash, kzg_proof_to_bytes},
prover::{IdStore, IdWrite, Proof, ProofKey, Prover, ProverError},
};
use reth_primitives::hex;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_with::{serde_as, DisplayFromStr};
Expand Down Expand Up @@ -174,7 +172,7 @@ impl ProofType {
config: &Value,
store: Option<&mut dyn IdWrite>,
) -> RaikoResult<Proof> {
let mut proof = match self {
match self {
ProofType::Native => NativeProver::run(input.clone(), output, config, store)
.await
.map_err(<ProverError as Into<RaikoError>>::into),
Expand Down Expand Up @@ -202,23 +200,7 @@ impl ProofType {
#[cfg(not(feature = "sgx"))]
Err(RaikoError::FeatureNotSupportedError(*self))
}
}?;

// Add the kzg proof to the proof if needed
if let Some(blob_commitment) = input.taiko.blob_commitment.clone() {
let kzg_proof = calc_kzg_proof(
&input.taiko.tx_data,
&commitment_to_version_hash(&blob_commitment.try_into().map_err(|_| {
RaikoError::Conversion(
"Could not convert blob commitment to version hash".to_owned(),
)
})?),
)
.map_err(|e| anyhow::anyhow!(e))?;
proof.kzg_proof = Some(hex::encode(kzg_proof_to_bytes(&kzg_proof)));
}

Ok(proof)
}

pub async fn cancel_proof(
Expand Down
35 changes: 30 additions & 5 deletions core/src/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use alloy_sol_types::{SolCall, SolEvent};
use anyhow::{anyhow, bail, ensure, Result};
use kzg_traits::{
eip_4844::{blob_to_kzg_commitment_rust, Blob},
G1,
Fr, G1,
};
use raiko_lib::{
builder::{OptimisticDatabase, RethBlockBuilder},
Expand Down Expand Up @@ -240,7 +240,12 @@ async fn prepare_taiko_chain_input(
.await?;

// Fetch the tx data from either calldata or blobdata
let (tx_data, blob_commitment) = if proposal_event.meta.blobUsed {
let (tx_data, blob_commitment, blob_proof) = if proposal_event.meta.blobUsed {
use eip4844::{
calc_kzg_proof_commitment, calc_kzg_proof_with_point, commitment_to_version_hash,
proof_of_equivalence,
};
use kzg::kzg_types::ZFr;
debug!("blob active");
// Get the blob hashes attached to the propose tx
let blob_hashes = proposal_tx.blob_versioned_hashes.unwrap_or_default();
Expand All @@ -257,14 +262,33 @@ async fn prepare_taiko_chain_input(
RaikoError::Preflight("Beacon RPC URL is required for Taiko chains".to_owned())
})?;
let blob = get_blob_data(&beacon_rpc_url, slot_id, blob_hash).await?;
let commitment = eip4844::calc_kzg_proof_commitment(&blob).map_err(|e| anyhow!(e))?;
let commitment = calc_kzg_proof_commitment(&blob).map_err(|e| anyhow!(e))?;
let blob_proof = match blob_proof_type {
BlobProofType::KzgVersionedHash => None,
BlobProofType::ProofOfEquivalence => {
let (x, y) = proof_of_equivalence(&blob, &commitment_to_version_hash(&commitment))
.map_err(|e| anyhow!(e))?;

debug!("x {:?} y {:?}", x, y);
debug!(
"calc_kzg_proof_with_point {:?}",
calc_kzg_proof_with_point(&blob, ZFr::from_bytes(&x).unwrap()).unwrap()
);

Some(
calc_kzg_proof_with_point(&blob, ZFr::from_bytes(&x).unwrap())
.map(|g1| g1.to_bytes().to_vec())
.map_err(|e| anyhow!(e))?,
)
}
};

(blob, Some(commitment.to_vec()))
(blob, Some(commitment.to_vec()), blob_proof)
} else {
// Get the tx list data directly from the propose transaction data
let proposal_call = proposeBlockCall::abi_decode(&proposal_tx.input, false)
.map_err(|_| RaikoError::Preflight("Could not decode proposeBlockCall".to_owned()))?;
(proposal_call.txList.as_ref().to_owned(), None)
(proposal_call.txList.as_ref().to_owned(), None, None)
};

// Create the input struct without the block data set
Expand All @@ -273,6 +297,7 @@ async fn prepare_taiko_chain_input(
tx_data,
anchor_tx: Some(anchor_tx.clone()),
blob_commitment,
blob_proof,
block_proposed: proposal_event,
prover_data,
blob_proof_type,
Expand Down
36 changes: 35 additions & 1 deletion core/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,45 @@ impl Prover for NativeProver {
Ok(Proof {
proof: None,
quote: None,
kzg_proof: None,
})
}

async fn cancel(_proof_key: ProofKey, _read: Box<&mut dyn IdStore>) -> ProverResult<()> {
Ok(())
}
}

#[ignore = "Only used to test serialized data"]
#[tokio::test(flavor = "multi_thread")]
async fn test_native_prover() {
use serde_json::json;

// Get the current working directory
let current_dir = std::env::current_dir().expect("Failed to get current directory");

// Adjust as needed based on your tests
let file_name = "ethereum-20612846.json";
let path = current_dir.join("../data").join(file_name);

// Check if the path exists
if !path.exists() {
panic!("File does not exist: {}", path.display());
}
let json = std::fs::read_to_string(path).unwrap();

// Deserialize the input.
let input: GuestInput = serde_json::from_str(&json).unwrap();
let output = GuestOutput {
header: reth_primitives::Header::default(),
hash: reth_primitives::B256::default(),
};

let param = json!({
"native": {
"json_guest_input": null
}
});
NativeProver::run(input, &output, &param, None)
.await
.expect_err("Default output should not match input.");
}
16 changes: 9 additions & 7 deletions core/src/provider/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ impl BlockDataProvider for RpcBlockDataProvider {
));
}

batch
.send()
.await
.map_err(|_| RaikoError::RPC("Error sending batch request".to_owned()))?;
batch.send().await.map_err(|e| {
RaikoError::RPC(format!(
"Error sending batch request for block {:?}: {e}",
blocks_to_fetch
))
})?;

let mut blocks = Vec::with_capacity(max_batch_size);
// Collect the data from the batch
Expand Down Expand Up @@ -132,7 +134,7 @@ impl BlockDataProvider for RpcBlockDataProvider {
batch
.send()
.await
.map_err(|_| RaikoError::RPC("Error sending batch request".to_owned()))?;
.map_err(|e| RaikoError::RPC(format!("Error sending batch request {e}")))?;

let mut accounts = vec![];
// Collect the data from the batch
Expand Down Expand Up @@ -197,7 +199,7 @@ impl BlockDataProvider for RpcBlockDataProvider {
batch
.send()
.await
.map_err(|_| RaikoError::RPC("Error sending batch request".to_owned()))?;
.map_err(|e| RaikoError::RPC(format!("Error sending batch request {e}")))?;

let mut values = Vec::with_capacity(max_batch_size);
// Collect the data from the batch
Expand Down Expand Up @@ -299,7 +301,7 @@ impl BlockDataProvider for RpcBlockDataProvider {
batch
.send()
.await
.map_err(|_| RaikoError::RPC("Error sending batch request".to_owned()))?;
.map_err(|e| RaikoError::RPC(format!("Error sending batch request {e}")))?;

// Collect the data from the batch
for request in requests {
Expand Down
1 change: 1 addition & 0 deletions data/ethereum-20612842.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions data/ethereum-20612843.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions data/ethereum-20612844.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions data/ethereum-20612845.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions data/ethereum-20612846.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion data/input-ethereum-20335518.json

This file was deleted.

1 change: 0 additions & 1 deletion data/input-ethereum-20378340.json

This file was deleted.

1 change: 0 additions & 1 deletion data/input-taiko-mainnet-192319.json

This file was deleted.

1 change: 0 additions & 1 deletion data/input-taiko_mainnet-182300.json

This file was deleted.

1 change: 0 additions & 1 deletion data/input.json

This file was deleted.

1 change: 1 addition & 0 deletions data/taiko-mainnet-328805.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions data/taiko-mainnet-328808.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions data/taiko_mainnet-328833.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions data/taiko_mainnet-328834.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions data/taiko_mainnet-328835.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions data/taiko_mainnet-328836.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions data/taiko_mainnet-328837.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions host/config/chain_spec_list_default.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"l1_contract": null,
"l2_contract": null,
"rpc": "https://rpc.ankr.com/eth",
"rpc": "https://ethereum-rpc.publicnode.com",
"beacon_rpc": "https://ethereum-beacon-api.publicnode.com",
"verifier_address": {
"SGX":"0x532efbf6d62720d0b2a2bb9d11066e8588cae6d9",
Expand Down Expand Up @@ -73,9 +73,9 @@
{
"name": "taiko_a7",
"chain_id": 167009,
"max_spec_id": "SHANGHAI",
"max_spec_id": "HEKLA",
"hard_forks": {
"SHANGHAI": {
"HEKLA": {
"Block": 0
},
"CANCUN": "TBD"
Expand All @@ -102,9 +102,9 @@
{
"name": "taiko_mainnet",
"chain_id": 167000,
"max_spec_id": "SHANGHAI",
"max_spec_id": "HEKLA",
"hard_forks": {
"SHANGHAI": {
"HEKLA": {
"Block": 0
},
"CANCUN": "TBD"
Expand Down
4 changes: 2 additions & 2 deletions host/config/chain_spec_list_devnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
{
"name": "taiko_dev",
"chain_id": 167001,
"max_spec_id": "SHANGHAI",
"max_spec_id": "HEKLA",
"hard_forks": {
"SHANGHAI": {
"HEKLA": {
"Block": 0
},
"CANCUN": "TBD"
Expand Down
2 changes: 1 addition & 1 deletion host/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl ProofActor {
result = Self::handle_message(proof_request, key.clone(), &opts, &chain_specs) => {
match result {
Ok(()) => {
info!("Proof generated");
info!("Host handling message");
}
Err(error) => {
error!("Worker failed due to: {error:?}");
Expand Down
3 changes: 1 addition & 2 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,4 @@ std = [
sgx = []
sp1 = []
risc0 = []
sp1-cycle-tracker = []
proof_of_equivalence = []
sp1-cycle-tracker = []
10 changes: 8 additions & 2 deletions lib/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use reth_primitives::revm_primitives::{
Account, AccountInfo, AccountStatus, Bytecode, Bytes, HashMap,
};
use reth_primitives::{Address, BlockWithSenders, Header, B256, KECCAK_EMPTY, U256};
use tracing::debug;
use tracing::{debug, error};

pub fn calculate_block_header(input: &GuestInput) -> Header {
let cycle_tracker = CycleTracker::start("initialize_database");
Expand Down Expand Up @@ -116,6 +116,7 @@ impl<DB: Database<Error = ProviderError> + DatabaseCommit + OptimisticDatabase>
l1_header: self.input.taiko.l1_header.clone(),
parent_header: self.input.parent_header.clone(),
l2_contract: self.input.chain_spec.l2_contract.unwrap_or_default(),
..Default::default()
})
.optimistic(optimistic);
let BlockExecutionOutput {
Expand All @@ -125,7 +126,12 @@ impl<DB: Database<Error = ProviderError> + DatabaseCommit + OptimisticDatabase>
gas_used: _,
db: full_state,
valid_transaction_indices,
} = executor.execute((&block, total_difficulty).into())?;
} = executor
.execute((&block, total_difficulty).into())
.map_err(|e| {
error!("Error executing block: {:?}", e);
e
})?;
// Filter out the valid transactions so that the header checks only take these into account
block.body = valid_transaction_indices
.iter()
Expand Down
8 changes: 8 additions & 0 deletions lib/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ mod tests {
);
}

#[test]
fn revm_taiko_a7_spec_id() {
let taiko_a7_spec = SupportedChainSpecs::default()
.get_chain_spec(&Network::TaikoA7.to_string())
.unwrap();
assert_eq!(taiko_a7_spec.spec_id(1, 0), Some(SpecId::HEKLA));
}

#[ignore]
#[test]
fn serde_chain_spec() {
Expand Down
1 change: 1 addition & 0 deletions lib/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub struct TaikoGuestInput {
pub block_proposed: BlockProposed,
pub prover_data: TaikoProverData,
pub blob_commitment: Option<Vec<u8>>,
pub blob_proof: Option<Vec<u8>>,
pub blob_proof_type: BlobProofType,
}

Expand Down
Loading

0 comments on commit 7356e8f

Please sign in to comment.