Skip to content

Commit

Permalink
feat(raiko): enable kzg blob check (#148)
Browse files Browse the repository at this point in the history
* feat(raiko): enable kzg blob check

* update build script

* update ci

* fix ci

* keep fixing ci

* keep fixing ci - sgx docker build

* fix ci risc0

* update ci env
  • Loading branch information
smtmfft authored May 4, 2024
1 parent 8b23bae commit 9865b4c
Show file tree
Hide file tree
Showing 21 changed files with 234 additions and 62 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
!/rust-toolchain
!/taiko.toml
!/tests
!/setup
!/setup
!/kzg_settings_raw.bin
20 changes: 14 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ jobs:
name: Build and test risc0
runs-on: ubuntu-latest
timeout-minutes: 60
env:
CC: gcc
CC_riscv32im_risc0_zkvm_elf: /tmp/riscv/bin/riscv32-unknown-elf-gcc
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- run: .github/toolchain-before.sh risc0
- run: scripts/toolchain-before.sh risc0
- run: scripts/toolchain.sh risc0
- uses: actions-rs/toolchain@v1
with:
profile: minimal
- run: .github/toolchain-after.sh risc0
- run: scripts/toolchain-after.sh risc0
- uses: risc0/risc0/.github/actions/sccache@release-0.19
- name: Install cargo-binstall
uses: cargo-bins/cargo-binstall@v1.6.4
Expand All @@ -41,13 +45,17 @@ jobs:
name: Build and test sp1
runs-on: ubuntu-latest
timeout-minutes: 60
env:
CC: gcc
CC_riscv32im_succinct_zkvm_elf: /tmp/riscv/bin/riscv32-unknown-elf-gcc
steps:
- uses: actions/checkout@v4
- run: .github/toolchain-before.sh sp1
- run: scripts/toolchain-before.sh sp1
- uses: actions-rs/toolchain@v1
with:
profile: minimal
- run: .github/toolchain-after.sh sp1
- run: scripts/toolchain.sh sp1
- run: scripts/toolchain-after.sh sp1
- name: Setup and build
run: |
curl -L https://sp1.succinct.xyz | bash
Expand All @@ -62,11 +70,11 @@ jobs:
timeout-minutes: 60
steps:
- uses: actions/checkout@v4
- run: .github/toolchain-before.sh sgx
- run: scripts/toolchain-before.sh sgx
- uses: actions-rs/toolchain@v1
with:
profile: minimal
- run: .github/toolchain-after.sh sgx
- run: scripts/toolchain-after.sh sgx
- name: Setup and build
run: |
cargo build --features sgx
Expand Down
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ utoipa = { version = "4.2.0", features = ["axum_extras"] }
# misc
hashbrown = { version = "0.14", features = ["inline-more"] }
c-kzg = "1.0.0"
c-kzg-taiko = { git = "https://github.com/smtmfft/c-kzg-4844", branch = "for-alpha7", default-features = false, features = [
"preload-kzg-settings",
] }
tempfile = "3.8"
anyhow = "1.0"
bincode = "1.3.3"
Expand Down
28 changes: 15 additions & 13 deletions host/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
"l1_network": "holesky",
"prover": "0x70997970C51812dc3A010C7d01b50e0d17dc79C8",
"graffiti": "8008500000000000000000000000000000000000000000000000000000000000",
"proof_type": "sgx",
"sgx": {
"instance_id": 456,
"setup": false,
"bootstrap": false,
"prove": true,
"input_path": null
},
"risc0": {
"bonsai": true,
"snark": true,
"profile": false,
"execution_po2": 20
"proof_type": "risc0",
"prover_args": {
"sgx": {
"instance_id": 456,
"setup": false,
"bootstrap": false,
"prove": true,
"input_path": null
},
"risc0": {
"bonsai": true,
"snark": true,
"profile": false,
"execution_po2": 20
}
}
}
15 changes: 12 additions & 3 deletions host/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use raiko_lib::{
builder::{BlockBuilderStrategy, TaikoStrategy},
input::{GuestInput, GuestOutput, TaikoProverData, WrappedHeader},
protocol_instance::{assemble_protocol_instance, ProtocolInstance},
prover::{to_proof, Proof, Prover, ProverResult},
prover::{to_proof, Proof, Prover, ProverError, ProverResult},
Measurement,
};
use serde::{Deserialize, Serialize};
use tracing::{info, warn};
use tracing::{info, trace, warn};

use crate::{
error::HostResult,
Expand Down Expand Up @@ -126,10 +126,19 @@ pub struct NativeResponse {

impl Prover for NativeProver {
async fn run(
_input: GuestInput,
input: GuestInput,
output: GuestOutput,
_request: &serde_json::Value,
) -> ProverResult<Proof> {
trace!("Running the native prover for input {:?}", input);
match output.clone() {
GuestOutput::Success((wrapedheader, _)) => {
assemble_protocol_instance(&input, &wrapedheader.header)
.map_err(|e| ProverError::GuestError(e.to_string()))?;
}
_ => return Err(ProverError::GuestError("Unexpected output".to_string())),
}

to_proof(Ok(NativeResponse { output }))
}

Expand Down
10 changes: 5 additions & 5 deletions host/src/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ pub fn preflight(
block_number,
)?;

// Fetch the tx list
let (tx_list, tx_blob_hash) = if proposal_event.meta.blobUsed {
// Fetch the tx data from either calldata or blobdata
let (tx_data, tx_blob_hash) = if proposal_event.meta.blobUsed {
println!("blob active");
// Get the blob hashes attached to the propose tx
let blob_hashes = proposal_tx.blob_versioned_hashes.unwrap_or_default();
Expand Down Expand Up @@ -126,7 +126,7 @@ pub fn preflight(
// Create the transactions from the proposed tx list
let transactions = generate_transactions(
proposal_event.meta.blobUsed,
&tx_list,
&tx_data,
Some(anchor_tx.clone()),
);
// Do a sanity check using the transactions returned by the node
Expand All @@ -138,7 +138,7 @@ pub fn preflight(
// Create the input struct without the block data set
TaikoGuestInput {
l1_header: to_header(&l1_state_block.header),
tx_list,
tx_data,
anchor_tx: serde_json::to_string(&anchor_tx).unwrap(),
tx_blob_hash,
block_proposed: proposal_event,
Expand All @@ -148,7 +148,7 @@ pub fn preflight(
// For Ethereum blocks we just convert the block transactions in a tx_list
// so that we don't have to supports separate paths.
TaikoGuestInput {
tx_list: alloy_rlp::encode(&get_transactions_from_block(&block)),
tx_data: alloy_rlp::encode(&get_transactions_from_block(&block)),
..Default::default()
}
};
Expand Down
Binary file added kzg_settings_raw.bin
Binary file not shown.
2 changes: 2 additions & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ serde_with = { workspace = true, optional = true }
thiserror-no-std = { workspace = true }
url = { workspace = true }
hex = { workspace = true }
c-kzg-taiko = { workspace = true }
sha2 = { workspace = true }

# [target.'cfg(feature = "std")'.dependencies]
thiserror = { workspace = true, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion lib/src/builder/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl TxExecStrategy for TkoTxExecStrategy {
};
let mut transactions = generate_transactions(
block_builder.input.taiko.block_proposed.meta.blobUsed,
&block_builder.input.taiko.tx_list,
&block_builder.input.taiko.tx_data,
anchor_tx,
);

Expand Down
2 changes: 1 addition & 1 deletion lib/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub struct TaikoGuestInput {
/// header
#[serde_as(as = "RlpBytes")]
pub l1_header: AlloyConsensusHeader,
pub tx_list: Vec<u8>,
pub tx_data: Vec<u8>,
pub anchor_tx: String,
pub block_proposed: BlockProposed,
pub prover_data: TaikoProverData,
Expand Down
24 changes: 22 additions & 2 deletions lib/src/protocol_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use alloy_consensus::Header as AlloyConsensusHeader;
use alloy_primitives::{Address, TxHash, B256};
use alloy_sol_types::SolValue;
use anyhow::{ensure, Result};
use c_kzg_taiko::{Blob, KzgCommitment, KzgSettings};
use raiko_primitives::keccak::keccak;
use sha2::{Digest as _, Sha256};

use super::taiko_utils::ANCHOR_GAS_LIMIT;
#[cfg(not(feature = "std"))]
Expand All @@ -13,6 +15,8 @@ use crate::{
taiko_utils::HeaderHasher,
};

const KZG_TRUST_SETUP_DATA: &[u8] = include_bytes!("../../kzg_settings_raw.bin");

#[derive(Debug)]
pub struct ProtocolInstance {
pub transition: Transition,
Expand Down Expand Up @@ -78,16 +82,32 @@ pub enum EvidenceType {
Native,
}

pub const VERSIONED_HASH_VERSION_KZG: u8 = 0x01;
pub fn kzg_to_versioned_hash(commitment: KzgCommitment) -> B256 {
let mut res = Sha256::digest(commitment.as_slice());
res[0] = VERSIONED_HASH_VERSION_KZG;
B256::new(res.into())
}

// TODO(cecilia): rewrite
pub fn assemble_protocol_instance(
input: &GuestInput,
header: &AlloyConsensusHeader,
) -> Result<ProtocolInstance> {
let blob_used = input.taiko.block_proposed.meta.blobUsed;
let tx_list_hash = if blob_used {
input.taiko.tx_blob_hash.unwrap()
let mut data = Vec::from(KZG_TRUST_SETUP_DATA);
let kzg_settings = KzgSettings::from_u8_slice(&mut data);
let kzg_commit = KzgCommitment::blob_to_kzg_commitment(
&Blob::from_bytes(&input.taiko.tx_data.as_slice()).unwrap(),
&kzg_settings,
)
.unwrap();
let versioned_hash = kzg_to_versioned_hash(kzg_commit);
assert_eq!(versioned_hash, input.taiko.tx_blob_hash.unwrap());
versioned_hash
} else {
TxHash::from(keccak(input.taiko.tx_list.as_slice()))
TxHash::from(keccak(input.taiko.tx_data.as_slice()))
};

let deposits = input
Expand Down
24 changes: 22 additions & 2 deletions provers/risc0/guest/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9865b4c

Please sign in to comment.