Skip to content

Commit

Permalink
type KzgGroup, KzgField + test
Browse files Browse the repository at this point in the history
  • Loading branch information
CeciliaZ030 committed Jun 22, 2024
1 parent 835fe0e commit 8371598
Show file tree
Hide file tree
Showing 6 changed files with 4,269 additions and 218 deletions.
4 changes: 2 additions & 2 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ mod tests {
if is_ci() && proof_type == ProofType::Sp1 {
input.taiko.skip_verify_blob = true;
}
let output = raiko.get_output(&input).expect("output generation failed");
let mut output = raiko.get_output(&input).expect("output generation failed");
let _proof = raiko
.prove(input, &output)
.prove(input, &mut output)
.await
.expect("proof generation failed");
}
Expand Down
192 changes: 8 additions & 184 deletions core/src/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use raiko_lib::{
TaikoProverData,
},
primitives::{
self, eip4844::{self, get_kzg_proof, set_commitment_proof, MAINNET_KZG_TRUSTED_SETUP}, mpt::proofs_to_tries
self, eip4844::{self, get_kzg_proof_commitment, set_commitment_proof, MAINNET_KZG_TRUSTED_SETUP, KzgField, KzgGroup},
mpt::proofs_to_tries
},
utils::{generate_transactions, to_header, zlib_compress_data},
Measurement,
Expand Down Expand Up @@ -293,9 +294,9 @@ async fn prepare_taiko_chain_input(
let blob = get_blob_data(&beacon_rpc_url, slot_id, blob_hash).await?;

let kzg_settings = eip4844::MAINNET_KZG_TRUSTED_SETUP.as_ref().clone();
let (proof, y) = eip4844::get_kzg_proof(&blob, &kzg_settings)
let (proof, commitment) = eip4844::get_kzg_proof_commitment(&blob, &kzg_settings)
.map_err(|e| anyhow!(e))?;
set_commitment_proof(proof, y)
set_commitment_proof(&proof, &commitment)
.map_err(|e| anyhow!(e))?;

(blob, Some(blob_hash), Some(kzg_settings))
Expand Down Expand Up @@ -359,7 +360,8 @@ fn blob_to_bytes(blob_str: &str) -> Vec<u8> {
}
}

fn calc_blob_versioned_hash(blob_str: &str) -> [u8; 32] {
fn preflight_blob_versioned_hash(blob_str: &str) -> [u8; 32] {
use kzg::G1;
let blob_bytes: Vec<u8> = hex::decode(blob_str.to_lowercase().trim_start_matches("0x"))
.expect("Could not decode blob");
let kzg_settings = MAINNET_KZG_TRUSTED_SETUP.as_ref();
Expand All @@ -369,7 +371,7 @@ fn calc_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(&bincode::serialize(&commitment).unwrap()).0;
let version_hash: [u8; 32] = eip4844::commitment_to_version_hash(&commitment.to_bytes()).0;
version_hash
}

Expand Down Expand Up @@ -429,7 +431,7 @@ async fn get_blob_data_beacon(
.iter()
.find(|blob| {
// calculate from plain blob
blob_hash == calc_blob_versioned_hash(&blob.blob)
blob_hash == preflight_blob_versioned_hash(&blob.blob)
})
.cloned();
assert!(tx_blob.is_some());
Expand Down Expand Up @@ -635,63 +637,11 @@ mod test {
use ethers_core::types::Transaction;
use raiko_lib::{
consts::{Network, SupportedChainSpecs},
primitives::{eip4844_::parse_kzg_trusted_setup, kzg::KzgSettings},
utils::decode_transactions,
};

use super::*;

#[allow(dead_code)]
fn calc_commit_versioned_hash(commitment: &str) -> [u8; 32] {
let commit_bytes = hex::decode(commitment.to_lowercase().trim_start_matches("0x")).unwrap();
let kzg_commit = c_kzg::KzgCommitment::from_bytes(&commit_bytes).unwrap();
let version_hash: [u8; 32] = kzg_to_versioned_hash(&kzg_commit).0;
version_hash
}

// TODO(Cecilia): "../kzg_parsed_trust_setup" does not exist
#[ignore]
#[test]
fn test_parse_kzg_trusted_setup() {
// check if file exists
let b_file_exists = std::path::Path::new("../kzg_parsed_trust_setup").exists();
assert!(b_file_exists);
// open file as lines of strings
let kzg_trust_setup_str = std::fs::read_to_string("../kzg_parsed_trust_setup").unwrap();
let (g1, g2) = parse_kzg_trusted_setup(&kzg_trust_setup_str)
.map_err(|e| {
println!("error: {e:?}");
e
})
.unwrap();
println!("g1: {:?}", g1.0.len());
println!("g2: {:?}", g2.0.len());
}

// TODO(Cecilia): "../kzg_parsed_trust_setup" does not exist
#[ignore]
#[test]
fn test_blob_to_kzg_commitment() {
// check if file exists
let b_file_exists = std::path::Path::new("../kzg_parsed_trust_setup").exists();
assert!(b_file_exists);
// open file as lines of strings
let kzg_trust_setup_str = std::fs::read_to_string("../kzg_parsed_trust_setup").unwrap();
let (g1, g2) = parse_kzg_trusted_setup(&kzg_trust_setup_str)
.map_err(|e| {
println!("error: {e:?}");
e
})
.unwrap();
let kzg_settings = KzgSettings::load_trusted_setup(&g1.0, &g2.0).unwrap();
let blob = [0u8; 131072].into();
let kzg_commit = KzgCommitment::blob_to_kzg_commitment(&blob, &kzg_settings).unwrap();
assert_eq!(
kzg_to_versioned_hash(&kzg_commit).to_string(),
"0x010657f37554c781402a22917dee2f75def7ab966d7b770905398eba3c444014"
);
}

#[test]
fn test_new_blob_decode() {
let valid_blob_str = "\
Expand Down Expand Up @@ -739,139 +689,13 @@ mod test {
0000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000";
// println!("valid blob: {:?}", valid_blob_str);
let blob_str = format!("{:0<262144}", valid_blob_str);
let dec_blob = blob_to_bytes(&blob_str);
println!("dec blob tx len: {:?}", dec_blob.len());
let txs = decode_transactions(&dec_blob);
println!("dec blob tx: {txs:?}");
// assert_eq!(hex::encode(dec_blob), expected_dec_blob);
}

#[test]
fn test_c_kzg_lib_commitment() {
// check c-kzg mainnet trusted setup is ok
let kzg_settings = Arc::clone(&*MAINNET_KZG_TRUSTED_SETUP);
let blob = [0u8; 131072].into();
let kzg_commit = KzgCommitment::blob_to_kzg_commitment(&blob, &kzg_settings).unwrap();
assert_eq!(
kzg_to_versioned_hash(&kzg_commit).to_string(),
"0x010657f37554c781402a22917dee2f75def7ab966d7b770905398eba3c444014"
);
}

// #[ignore]
// #[tokio::test]
// async fn test_propose_block() {
// tokio::task::spawn_blocking(|| {
// let l2_chain_spec = get_taiko_chain_spec("internal_devnet_a");
// let mut l1_provider = new_provider(
// None,
// Some("https://localhost:8545".to_owned()),
// Some("https://localhost:3500/".to_owned()),
// )
// .expect("bad provider");
// let (propose_tx, block_metadata) = l1_provider
// .get_propose(&ProposeQuery {
// l1_contract: H160::from_slice(l2_chain_spec.l1_contract.unwrap().as_slice()),
// l1_block_no: 6093,
// l2_block_no: 1000,
// })
// .expect("bad get_propose");
// println!("propose_tx: {:?}", propose_tx);
// println!("block_metadata: {:?}", block_metadata);
// })
// .await
// .unwrap();
// }
//
// #[ignore]
// #[tokio::test]
// async fn test_fetch_blob_data_and_hash() {
// tokio::task::spawn_blocking(|| {
// let mut provider = new_provider(
// None,
// Some("https://l1rpc.internal.taiko.xyz/".to_owned()),
// Some("https://l1beacon.internal.taiko.xyz/".to_owned()),
// )
// .expect("bad provider");
// let blob_data = fetch_blob_data("http://localhost:3500".to_string(), 5).unwrap();
// let blob_data = provider.get_blob_data(17138).unwrap();
// println!("blob len: {:?}", blob_data.data[0].blob.len());
// let dec_blob = decode_blob_data(&blob_data.data[0].blob);
// println!("dec blob tx: {:?}", dec_blob.len());
//
// println!("blob commitment: {:?}", blob_data.data[0].kzg_commitment);
// let blob_hash = calc_commit_versioned_hash(&blob_data.data[0].kzg_commitment);
// println!("blob hash {:?}", hex::encode(blob_hash));
// })
// .await
// .unwrap();
// }
//
// #[ignore]
// #[tokio::test]
// async fn test_fetch_and_verify_blob_data() {
// tokio::task::spawn_blocking(|| {
// let mut provider = new_provider(
// None,
// Some("https://l1rpc.internal.taiko.xyz".to_owned()),
// Some("https://l1beacon.internal.taiko.xyz".to_owned()),
// )
// .expect("bad provider");
// let blob_data = provider.get_blob_data(168).unwrap();
// let blob_bytes: [u8; 4096 * 32] = hex::decode(
// blob_data.data[0]
// .blob
// .to_lowercase()
// .trim_start_matches("0x"),
// )
// .unwrap()
// .try_into()
// .unwrap();
// let blob: Blob = blob_bytes.into();
// let kzg_settings = Arc::clone(&*MAINNET_KZG_TRUSTED_SETUP);
// let kzg_commit: KzgCommitment =
// KzgCommitment::blob_to_kzg_commitment(&blob, &kzg_settings).unwrap();
// assert_eq!(
// "0x".to_owned() + &kzg_commit.as_hex_string(),
// blob_data.data[0].kzg_commitment
// );
// println!("blob commitment: {:?}", blob_data.data[0].kzg_commitment);
// let calc_versioned_hash =
// calc_commit_versioned_hash(&blob_data.data[0].kzg_commitment); println!("blob hash
// {:?}", hex::encode(calc_versioned_hash)); })
// .await
// .unwrap();
// }
//
// #[ignore]
// #[tokio::test]
// async fn test_fetch_and_decode_blob_tx() {
// let block_num = std::env::var("TAIKO_L2_BLOCK_NO")
// .unwrap_or("94".to_owned())
// .parse::<u64>()
// .unwrap();
// tokio::task::spawn_blocking(move || {
// let mut provider = new_provider(
// None,
// Some("http://35.202.137.144:8545".to_owned()),
// Some("http://35.202.137.144:3500".to_owned()),
// )
// .expect("bad provider");
// let blob_data = provider.get_blob_data(block_num).unwrap();
// println!("blob str len: {:?}", blob_data.data[0].blob.len());
// let blob_bytes = decode_blob_data(&blob_data.data[0].blob);
// println!("blob byte len: {:?}", blob_bytes.len());
// println!("blob bytes {:?}", blob_bytes);
// rlp decode blob tx
// let txs: Vec<Transaction> = rlp_decode_list(&blob_bytes).unwrap();
// println!("blob tx: {:?}", txs);
// })
// .await
// .unwrap();
// }

#[ignore]
#[test]
fn test_slot_block_num_mapping() {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use anyhow::{anyhow, Result};
use revm::primitives::HashMap;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use crate::primitives::eip4844::TaikoKzgSettings;
use crate::primitives::eip4844::{KzgField, TaikoKzgSettings};

#[cfg(not(feature = "std"))]
use crate::no_std::*;
Expand Down Expand Up @@ -112,7 +112,7 @@ pub struct GuestOutput {
#[serde_as(as = "RlpHexBytes")]
pub header: AlloyConsensusHeader,
pub hash: B256,
pub proof_of_equivalence: Option<Vec<u8>>
pub proof_of_equivalence: Option<KzgField>
}

sol! {
Expand Down
Loading

0 comments on commit 8371598

Please sign in to comment.