Skip to content

Commit

Permalink
feat(prover): proof of equivalence using rust-kzg (#295)
Browse files Browse the repository at this point in the history
* add cycle-tracker

* git = "https://github.com/ceciliaz030/alloy", branch = "175_4e22b9e-cycle"

* revm v35_taiko_v2-cycls

* make harness part of root workspace

* update Cargo.lock

* [dev-dependencies]
substrate-bn = "0.6.0"

* use [email protected]

* cargo update

* clippy & fmt

* remove typo

* add [features] sp1-cycle-tracker in guest

* apply comment

* struct CycleTracker

* update

* fmt

* Update proof.rs

* Update proof.rs

* Update proof.rs

* primitives should not exist

* added rust-kzg

* wip

* clear c-kzg

* mod eip4844 done

* output done

* type KzgGroup, KzgField + test

* test in mod eip4844

* clippy & fmt

* remove extra

* x = sha256(sha256(blob), kzg_commit(blob))
sha256(blob) -- inside
kzg_commit(blob) -- ouside
sha256(sha256(blob), kzg_commit(blob)) -- inside -> PI

* updated dependencies

* add 4844 precompile test + update sp1 patch name

* rust-kzg default-features = false + clippy & fmt

* fmt all

* fixed diff & compiled client

* add kzg flag

* Squashed commit of the following:

commit 35f1c99
Author: Brechtpd <[email protected]>
Date:   Tue Jun 25 07:36:33 2024 +0200

    small refactor

commit 6e38c86
Author: Brechtpd <[email protected]>
Date:   Tue Jun 25 07:14:44 2024 +0200

    unify state commitment so default commit interface can be used

commit 89bb3e3
Author: Brechtpd <[email protected]>
Date:   Tue Jun 25 05:39:53 2024 +0200

    fix local dependency

commit 457b4ea
Merge: c9bf502 fa339df
Author: Brechtpd <[email protected]>
Date:   Tue Jun 25 05:01:22 2024 +0200

    Merge remote-tracking branch 'origin/main' into reth-wip

commit c9bf502
Author: Brechtpd <[email protected]>
Date:   Tue Jun 25 05:01:08 2024 +0200

    add back validation for receipts/log bloom/gas used

commit 3c708a6
Author: Brechtpd <[email protected]>
Date:   Tue Jun 25 05:00:43 2024 +0200

    update dependencies + cleanup import warnings with patching

commit 144a78f
Author: Brechtpd <[email protected]>
Date:   Mon Jun 24 23:08:56 2024 +0200

    update to reth version without mdbx dependency

commit a94443c
Author: Brechtpd <[email protected]>
Date:   Mon Jun 24 22:31:19 2024 +0200

    lock to latest reth

commit 69f02ba
Author: Brechtpd <[email protected]>
Date:   Mon Jun 24 22:29:59 2024 +0200

    clippy fixes

commit 133f01a
Author: Brechtpd <[email protected]>
Date:   Mon Jun 24 21:58:29 2024 +0200

    misc fixes

commit ca6b510
Author: Brechtpd <[email protected]>
Date:   Mon Jun 24 07:09:28 2024 +0200

    update sp1 cargo

commit 3885979
Author: Brechtpd <[email protected]>
Date:   Mon Jun 24 07:07:29 2024 +0200

    fix ci rules

commit 2d4cc96
Merge: 3b5cd36 62158a0
Author: Brechtpd <[email protected]>
Date:   Mon Jun 24 07:07:23 2024 +0200

    Merge remote-tracking branch 'origin/main' into reth-wip

commit 3b5cd36
Author: Brechtpd <[email protected]>
Date:   Mon Jun 24 06:25:39 2024 +0200

    fix local dependencies

commit 7413da6
Author: Brechtpd <[email protected]>
Date:   Mon Jun 24 06:19:50 2024 +0200

    update to reth v1.0.0-rc.2

commit dbe0a36
Author: brechtpd <[email protected]>
Date:   Fri Jun 21 22:23:47 2024 +0200

    misc cleanup

commit 9f425b4
Author: brechtpd <[email protected]>
Date:   Sun Jun 16 05:12:24 2024 +0200

    remove revm as a direct dependency + remove dead code

commit 01bd522
Merge: 80adeea 125a349
Author: brechtpd <[email protected]>
Date:   Sat Jun 15 02:26:08 2024 +0200

    Merge remote-tracking branch 'origin/main' into reth-wip

commit 80adeea
Author: brechtpd <[email protected]>
Date:   Fri Jun 14 03:00:56 2024 +0200

    proving blocks with reth fully working (with some caveats)

commit 07a83cb
Author: brechtpd <[email protected]>
Date:   Mon Jun 10 01:04:04 2024 +0200

    better invalid tx support

commit cf15ee1
Author: brechtpd <[email protected]>
Date:   Fri May 31 13:52:50 2024 +0700

    Taiko support mostly working (but incomplete)

commit c9dd6ef
Merge: d463658 9f80be5
Author: brechtpd <[email protected]>
Date:   Fri May 24 12:40:24 2024 +0200

    Merge remote-tracking branch 'origin/main' into reth-wip

commit d463658
Author: Brechtpd <[email protected]>
Date:   Thu May 23 02:59:20 2024 +0200

    cleanup

commit 0f203d4
Author: Brechtpd <[email protected]>
Date:   Wed May 22 09:03:47 2024 +0200

    fmt

commit 59ad7d3
Author: Brechtpd <[email protected]>
Date:   Wed May 22 08:54:05 2024 +0200

    reth builder (pretty important)

commit 85a924d
Author: Brechtpd <[email protected]>
Date:   Wed May 22 08:53:41 2024 +0200

    switch to reth header

commit a214fe4
Author: Brechtpd <[email protected]>
Date:   Wed May 22 08:53:04 2024 +0200

    reth builder full coverage

commit 8905e46
Author: Brechtpd <[email protected]>
Date:   Mon May 20 10:09:18 2024 +0200

    reth wip

* fixed flgs, seeing errors

* compiles

* wip

* lib compile

* r0 compile

* compiled

* user interface in ProofRequest

* statically compiled KzgSetting deserizlized struct

* wip

* fix serialization

* clippy & fmt fix

* clippy & fmt

* make rust-kzg/c-kzg play nice with eachother + misc changes

* remove cached input from repo

* fix fmt + clippy

---------

Co-authored-by: Brechtpd <[email protected]>
  • Loading branch information
CeciliaZ030 and Brechtpd authored Jun 30, 2024
1 parent 546ab19 commit 4c8c44e
Show file tree
Hide file tree
Showing 25 changed files with 1,205 additions and 778 deletions.
392 changes: 248 additions & 144 deletions Cargo.lock

Large diffs are not rendered by default.

44 changes: 23 additions & 21 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ raiko-core = { path = "./core" }

# reth
reth-primitives = { git = "https://github.com/taikoxyz/taiko-reth.git", branch = "v1.0.0-rc.2-taiko", default-features = false, features = ["alloy-compat", "taiko"] }
reth-evm-ethereum = { git = "https://github.com/taikoxyz/taiko-reth.git", branch = "v1.0.0-rc.2-taiko" }
reth-evm-ethereum = { git = "https://github.com/taikoxyz/taiko-reth.git", branch = "v1.0.0-rc.2-taiko", default-features = false }
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" }
reth-revm = { git = "https://github.com/taikoxyz/taiko-reth.git", branch = "v1.0.0-rc.2-taiko" }
reth-chainspec = { git = "https://github.com/taikoxyz/taiko-reth.git", branch = "v1.0.0-rc.2-taiko" }
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-provider = { git = "https://github.com/taikoxyz/taiko-reth.git", branch = "v1.0.0-rc.2-taiko", default-features = false }

# risc zero
Expand All @@ -66,22 +66,26 @@ alloy-primitives = { version = "0.7.2", default-features = false }
alloy-sol-types = { version = "0.7.2", default-features = false }
reqwest_alloy = { package = "reqwest", version = "0.12.4", features = ["json"] }

alloy-rpc-types = { version = "0.1" }
alloy-rpc-client = { version = "0.1" }
alloy-consensus = { version = "0.1", features = [
alloy-rpc-types = { version = "0.1", default-features = false }
alloy-rpc-client = { version = "0.1", default-features = false }
alloy-consensus = { version = "0.1", default-features = false, features = [
"serde",
] }
alloy-network = { version = "0.1", default-features = false, features = [
"k256",
] }
alloy-contract = { version = "0.1" }
alloy-contract = { version = "0.1", default-features = false }
alloy-eips = { version = "0.1", default-features = false, features = [
"serde",
] }
alloy-provider = { version = "0.1" }
alloy-transport-http = { version = "0.1" }
alloy-signer = { version = "0.1" }
alloy-signer-local = { version = "0.1" }
alloy-provider = { version = "0.1", default-features = false, features = [
"reqwest",
] }
alloy-transport-http = { version = "0.1",default-features = false, features = [
"reqwest",
] }
alloy-signer = { version = "0.1", default-features = false}
alloy-signer-local = { version = "0.1", default-features = false }

# ethers (TODO: remove)
ethers-contract = { git = "https://github.com/smtmfft/ethers-rs", branch = "ethers-core-2.0.10" }
Expand Down Expand Up @@ -133,15 +137,12 @@ reqwest = { version = "0.11.22", features = ["json"] }
url = "2.5.0"

# crypto
c-kzg = { git = "https://github.com/brechtpd/c-kzg-4844", branch = "for-alpha7", default-features = false, features = [
"preload-kzg-settings",
"no-threads",
] }
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 }
sha3 = { version = "0.10", default-features = false }
sha2 = "0.10.8"
secp256k1 = { git = "https://github.com/CeciliaZ030/rust-secp256k1", branch = "sp1-patch", features = [
'global-context',
"rand",
secp256k1 = { version = "0.29", default-features = false, features = [
"global-context",
"recovery",
] }

Expand Down Expand Up @@ -177,5 +178,6 @@ pathdiff = "0.2.1"
[patch.crates-io]
revm = { git = "https://github.com/taikoxyz/revm.git", branch = "v36-taiko" }
revm-primitives = { git = "https://github.com/taikoxyz/revm.git", branch = "v36-taiko" }
c-kzg = { git = "https://github.com/brechtpd/c-kzg-4844", branch = "for-alpha7" }
secp256k1 = { git = "https://github.com/CeciliaZ030/rust-secp256k1", branch = "sp1-patch" }
revm-precompile = { git = "https://github.com/taikoxyz/revm.git", branch = "v36-taiko" }
secp256k1 = { git = "https://github.com/CeciliaZ030/rust-secp256k1", branch = "sp1-patch" }
blst = { git = "https://github.com/CeciliaZ030/blst.git", branch = "v0.3.12-serialize" }
6 changes: 4 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ risc0-driver = { path = "../provers/risc0/driver", optional = true }
sgx-prover = { path = "../provers/sgx/prover", optional = true }

# raiko
raiko-lib = { workspace = true, features = ["c-kzg"] }
raiko-lib = { workspace = true }

# reth
reth-primitives.workspace = true
Expand All @@ -34,6 +34,7 @@ alloy-rpc-client = { workspace = true }

# tracing and logging
tracing = { workspace = true }
bincode = { workspace = true }

# errors
anyhow = { workspace = true }
Expand All @@ -45,7 +46,8 @@ serde_json = { workspace = true }
serde_with = { workspace = true }

# c-kzg
c-kzg = { workspace = true }
kzg = { workspace = true }
kzg_traits = { workspace = true }

# async
tokio = { workspace = true }
Expand Down
47 changes: 40 additions & 7 deletions core/src/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use crate::{merge, prover::NativeProver};
use alloy_primitives::{Address, B256};
use clap::{Args, ValueEnum};
use raiko_lib::{
input::{GuestInput, GuestOutput},
input::{BlobProofType, GuestInput, GuestOutput},
primitives::eip4844::{calc_kzg_proof, commitment_to_version_hash, kzg_proof_to_bytes},
prover::{Proof, Prover, ProverError},
};
use reth_primitives::hex;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_with::{serde_as, DisplayFromStr};
Expand All @@ -17,6 +19,10 @@ pub enum RaikoError {
#[error("Unknown proof type: {0}")]
InvalidProofType(String),

/// For invalid proof type generation request.
#[error("Unknown proof type: {0}")]
InvalidBlobOption(String),

/// For invalid proof request configuration.
#[error("Invalid proof request: {0}")]
InvalidRequestConfig(String),
Expand Down Expand Up @@ -128,35 +134,51 @@ impl ProofType {
output: &GuestOutput,
config: &Value,
) -> RaikoResult<Proof> {
match self {
ProofType::Native => NativeProver::run(input, output, config)
let mut proof = match self {
ProofType::Native => NativeProver::run(input.clone(), output, config)
.await
.map_err(|e| e.into()),
ProofType::Sp1 => {
#[cfg(feature = "sp1")]
return sp1_driver::Sp1Prover::run(input, output, config)
return sp1_driver::Sp1Prover::run(input.clone(), output, config)
.await
.map_err(|e| e.into());
#[cfg(not(feature = "sp1"))]
Err(RaikoError::FeatureNotSupportedError(self.clone()))
}
ProofType::Risc0 => {
#[cfg(feature = "risc0")]
return risc0_driver::Risc0Prover::run(input, output, config)
return risc0_driver::Risc0Prover::run(input.clone(), output, config)
.await
.map_err(|e| e.into());
#[cfg(not(feature = "risc0"))]
Err(RaikoError::FeatureNotSupportedError(self.clone()))
}
ProofType::Sgx => {
#[cfg(feature = "sgx")]
return sgx_prover::SgxProver::run(input, output, config)
return sgx_prover::SgxProver::run(input.clone(), output, config)
.await
.map_err(|e| e.into());
#[cfg(not(feature = "sgx"))]
Err(RaikoError::FeatureNotSupportedError(self.clone()))
}
}?;

// 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().unwrap()),
)
.unwrap();
let kzg_proof_hex = hex::encode(kzg_proof_to_bytes(&kzg_proof));
proof
.as_object_mut()
.unwrap()
.insert("kzg_proof".to_string(), Value::String(kzg_proof_hex));
}

Ok(proof)
}
}

Expand All @@ -168,7 +190,7 @@ pub struct ProofRequest {
pub block_number: u64,
/// The network to generate the proof for.
pub network: String,
/// The L1 network to grnerate the proof for.
/// The L1 network to generate the proof for.
pub l1_network: String,
/// Graffiti.
pub graffiti: B256,
Expand All @@ -177,6 +199,8 @@ pub struct ProofRequest {
pub prover: Address,
/// The proof type.
pub proof_type: ProofType,
/// Blob proof type.
pub blob_proof_type: BlobProofType,
#[serde(flatten)]
/// Additional prover params.
pub prover_args: HashMap<String, Value>,
Expand Down Expand Up @@ -204,6 +228,8 @@ pub struct ProofRequestOpt {
#[arg(long, require_equals = true)]
/// The proof type.
pub proof_type: Option<String>,
/// Blob proof type.
pub blob_proof_type: Option<String>,
#[command(flatten)]
#[serde(flatten)]
/// Any additional prover params in JSON format.
Expand Down Expand Up @@ -294,6 +320,13 @@ impl TryFrom<ProofRequestOpt> for ProofRequest {
))?
.parse()
.map_err(|_| RaikoError::InvalidRequestConfig("Invalid proof_type".to_string()))?,
blob_proof_type: value
.blob_proof_type
.unwrap_or("ProofOfCommitment".to_string())
.parse()
.map_err(|_| {
RaikoError::InvalidRequestConfig("Invalid blob_proof_type".to_string())
})?,
prover_args: value.prover_args.into(),
})
}
Expand Down
40 changes: 34 additions & 6 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl Raiko {
graffiti: self.request.graffiti,
prover: self.request.prover,
},
self.request.blob_proof_type.clone(),
)
.await
.map_err(Into::<RaikoError>::into)
Expand Down Expand Up @@ -197,6 +198,7 @@ mod tests {
use clap::ValueEnum;
use raiko_lib::{
consts::{Network, SupportedChainSpecs},
input::BlobProofType,
primitives::B256,
};
use serde_json::{json, Value};
Expand Down Expand Up @@ -255,15 +257,11 @@ mod tests {
let provider =
RpcBlockDataProvider::new(&taiko_chain_spec.rpc, proof_request.block_number - 1)
.expect("Could not create RpcBlockDataProvider");
let proof_type = proof_request.proof_type.to_owned();
let raiko = Raiko::new(l1_chain_spec, taiko_chain_spec, proof_request);
let mut input = raiko
let raiko = Raiko::new(l1_chain_spec, taiko_chain_spec, proof_request.clone());
let input = raiko
.generate_input(provider)
.await
.expect("input generation failed");
if is_ci() && proof_type == ProofType::Sp1 {
input.taiko.skip_verify_blob = true;
}
let output = raiko.get_output(&input).expect("output generation failed");
let _proof = raiko
.prove(input, &output)
Expand Down Expand Up @@ -293,6 +291,7 @@ mod tests {
prover: Address::ZERO,
l1_network,
proof_type,
blob_proof_type: BlobProofType::ProofOfEquivalence,
prover_args: test_proof_params(),
};
prove_block(l1_chain_spec, taiko_chain_spec, proof_request).await;
Expand All @@ -319,6 +318,35 @@ mod tests {
prover: Address::ZERO,
l1_network,
proof_type,
blob_proof_type: BlobProofType::ProofOfEquivalence,
prover_args: test_proof_params(),
};
prove_block(l1_chain_spec, taiko_chain_spec, proof_request).await;
}
}

#[tokio::test(flavor = "multi_thread")]
async fn test_prove_block_taiko_mainnet() {
let proof_type = get_proof_type_from_env();
// Skip test on SP1 for now because it's too slow on CI
if !(is_ci() && proof_type == ProofType::Sp1) {
let network = Network::TaikoMainnet.to_string();
let l1_network = Network::Ethereum.to_string();
let block_number = 88970;
let taiko_chain_spec = SupportedChainSpecs::default()
.get_chain_spec(&network)
.unwrap();
let l1_chain_spec = SupportedChainSpecs::default()
.get_chain_spec(&l1_network)
.unwrap();
let proof_request = ProofRequest {
block_number,
network,
graffiti: B256::ZERO,
prover: Address::ZERO,
l1_network,
proof_type,
blob_proof_type: BlobProofType::ProofOfEquivalence,
prover_args: test_proof_params(),
};
prove_block(l1_chain_spec, taiko_chain_spec, proof_request).await;
Expand Down
Loading

0 comments on commit 4c8c44e

Please sign in to comment.