Skip to content

Commit

Permalink
add kzg flag
Browse files Browse the repository at this point in the history
  • Loading branch information
CeciliaZ030 committed Jun 25, 2024
1 parent 8e87b45 commit 72f559f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 35 deletions.
15 changes: 4 additions & 11 deletions core/src/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,13 @@ use kzg::eip_4844::Blob;
use raiko_lib::{
builder::{
prepare::TaikoHeaderPrepStrategy, BlockBuilder, OptimisticDatabase, TkoTxExecStrategy,
},
clear_line,
consts::ChainSpec,
inplace_print,
input::{
}, clear_line, commitment_to_version_hash, consts::ChainSpec, inplace_print, input::{
decode_anchor, proposeBlockCall, BlockProposed, GuestInput, TaikoGuestInput,
TaikoProverData,
},
primitives::{
}, primitives::{
eip4844::{self, set_commitment_proof, MAINNET_KZG_TRUSTED_SETUP},
mpt::proofs_to_tries,
},
utils::{generate_transactions, to_header, zlib_compress_data},
Measurement,
}, utils::{generate_transactions, to_header, zlib_compress_data}, Measurement
};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
Expand Down Expand Up @@ -369,7 +362,7 @@ fn preflight_blob_versioned_hash(blob_str: &str) -> [u8; 32] {
kzg_settings,
)
.expect("Could not create kzg commitment from blob");
let version_hash: [u8; 32] = eip4844::commitment_to_version_hash(&commitment.to_bytes()).0;
let version_hash: [u8; 32] = commitment_to_version_hash(&commitment.to_bytes()).0;
version_hash
}

Expand Down
5 changes: 4 additions & 1 deletion lib/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use std::path::PathBuf;

#[cfg(not(feature = "std"))]
use crate::no_std::*;
use crate::primitives::eip4844::TaikoKzgSettings;
use crate::serde_helper::option_array_48;
use crate::{
consts::ChainSpec,
Expand All @@ -32,6 +31,9 @@ use revm::primitives::HashMap;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;

#[cfg(feature = "kzg")]
use crate::primitives::eip4844::TaikoKzgSettings;

/// Represents the state of an account's storage.
/// The storage trie together with the used storage slots allow us to reconstruct all the
/// required values.
Expand Down Expand Up @@ -97,6 +99,7 @@ pub struct TaikoGuestInput {
pub prover_data: TaikoProverData,
#[serde(with = "option_array_48")]
pub blob_commitment: Option<[u8; 48]>,
#[cfg(feature = "kzg")]
pub kzg_settings: Option<TaikoKzgSettings>,
pub skip_verify_blob: bool,
}
Expand Down
8 changes: 8 additions & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ mod no_std {
};
}

use revm_primitives::{B256, VERSIONED_HASH_VERSION_KZG};
use sha2::{Digest, Sha256};
use tracing::debug;

pub mod builder;
Expand Down Expand Up @@ -192,6 +194,12 @@ pub fn guest_mem_forget<T>(_t: T) {
core::mem::forget(_t)
}

pub fn commitment_to_version_hash(commitment: &[u8; 48]) -> B256 {
let mut hash = Sha256::digest(commitment);
hash[0] = VERSIONED_HASH_VERSION_KZG;
B256::new(hash.into())
}

pub trait RlpBytes: Sized {
/// Decodes the blob into the appropriate type.
/// The input must contain exactly one value and no trailing data.
Expand Down
7 changes: 1 addition & 6 deletions lib/src/primitives/eip4844.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::commitment_to_version_hash;
use crate::input::GuestInput;
use kzg::eip_4844::{
blob_to_polynomial, compute_challenge, compute_kzg_proof_rust,
Expand Down Expand Up @@ -119,12 +120,6 @@ pub fn set_commitment_proof(proof: &KzgGroup, commitment: &KzgGroup) -> Result<(
Ok(())
}

pub fn commitment_to_version_hash(commitment: &KzgGroup) -> B256 {
let mut hash = Sha256::digest(commitment);
hash[0] = VERSIONED_HASH_VERSION_KZG;
B256::new(hash.into())
}

#[cfg(test)]
mod test {
use std::io::Read;
Expand Down
33 changes: 20 additions & 13 deletions lib/src/protocol_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ use alloy_consensus::Header as AlloyConsensusHeader;
use alloy_primitives::{Address, TxHash, B256};
use alloy_sol_types::SolValue;
use anyhow::{ensure, Result};
// use c_kzg::{Blob, KzgCommitment, KzgSettings};
use sha2::Digest as _;
use cfg_if::cfg_if;

#[cfg(not(feature = "std"))]
use crate::no_std::*;
use crate::{
commitment_to_version_hash,
consts::{SupportedChainSpecs, VerifierType},
input::{BlockMetadata, EthDeposit, GuestInput, Transition},
primitives::{
eip4844::{self, KzgField},
keccak::keccak,
},
primitives::keccak::keccak,
utils::HeaderHasher,
};

Expand All @@ -28,7 +25,7 @@ pub struct ProtocolInstance {
pub sgx_instance: Address, // only used for SGX
pub chain_id: u64,
pub verifier_address: Address,
pub proof_of_equivalence: (KzgField, KzgField),
pub proof_of_equivalence: ([u8; 32], [u8; 32]),
}

impl ProtocolInstance {
Expand All @@ -38,12 +35,22 @@ impl ProtocolInstance {
proof_type: VerifierType,
) -> Result<Self> {
let blob_used = input.taiko.block_proposed.meta.blobUsed;
let mut proof_of_equivalence = (KzgField::default(), KzgField::default());

// If blob is used, tx_list_hash is the commitment to the blob
// and we need to comput the proof of equivalence when not skipping with the kzg feature enabled
// Otherwise the proof_of_equivalence is 0
let mut proof_of_equivalence = ([0u8; 32], [0u8; 32]);
let tx_list_hash = if blob_used {
if input.taiko.skip_verify_blob {
proof_of_equivalence = eip4844::proof_of_equivalence(input)?;
if !input.taiko.skip_verify_blob {
cfg_if::cfg_if!(
if #[cfg(feature = "kzg")] {
proof_of_equivalence = crate::primitives::eip4844::proof_of_equivalence(input)?;
} else {
return Err(anyhow::anyhow!("kzg feature is not enabled"));
}
);
}
eip4844::commitment_to_version_hash(&input.taiko.blob_commitment.unwrap())
commitment_to_version_hash(&input.taiko.blob_commitment.unwrap())
} else {
TxHash::from(keccak(input.taiko.tx_data.as_slice()))
};
Expand Down Expand Up @@ -222,7 +229,7 @@ mod tests {
graffiti: b256!("0000000000000000000000000000000000000000000000000000000000000000"),
};
let meta_hash = b256!("9608088f69e586867154a693565b4f3234f26f82d44ef43fb99fd774e7266024");
let proof_of_equivalence = (KzgField::default(), KzgField::default());
let proof_of_equivalence = ([0u8; 32], [0u8; 32]);

let pi_hash = keccak::keccak(
(
Expand Down Expand Up @@ -257,7 +264,7 @@ mod tests {
let (meta, trans, _proof) =
<(BlockMetadata, Transition, TierProof)>::abi_decode_params(&input, false).unwrap();
let meta_hash: B256 = keccak::keccak(meta.abi_encode()).into();
let proof_of_equivalence = (KzgField::default(), KzgField::default());
let proof_of_equivalence = ([0u8; 32], [0u8; 32]);

let pi_hash = keccak::keccak(
(
Expand Down
11 changes: 7 additions & 4 deletions provers/risc0/driver/src/methods/risc0_guest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

pub const RISC0_GUEST_ELF: &[u8] = include_bytes!("../../../guest/target/riscv32im-risc0-zkvm-elf/release/risc0-guest");
pub const RISC0_GUEST_ID: [u32; 8] = [1759551775, 3661761430, 1950818100, 3620390442, 4224211464, 686568881, 1963228835, 1545061903];
pub const RISC0_GUEST_PATH: &str = r#"/home/ubuntu/raiko/provers/risc0/guest/target/riscv32im-risc0-zkvm-elf/release/risc0-guest"#;
pub const RISC0_GUEST_ELF: &[u8] =
include_bytes!("../../../guest/target/riscv32im-risc0-zkvm-elf/release/risc0-guest");
pub const RISC0_GUEST_ID: [u32; 8] = [
1759551775, 3661761430, 1950818100, 3620390442, 4224211464, 686568881, 1963228835, 1545061903,
];
pub const RISC0_GUEST_PATH: &str =
r#"/home/ubuntu/raiko/provers/risc0/guest/target/riscv32im-risc0-zkvm-elf/release/risc0-guest"#;
Binary file modified provers/sp1/guest/elf/sp1-guest
Binary file not shown.

0 comments on commit 72f559f

Please sign in to comment.