Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (kzg): v36 rust-kzg diff - Don't merge #11

Open
wants to merge 28 commits into
base: v36-taiko
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
45a38bc
use rust_kzg & change sha2 to sp1-patches
CeciliaZ030 Jun 15, 2024
b25b84c
clippy & fmt
CeciliaZ030 Jun 15, 2024
9acc587
remove all c-kzg
CeciliaZ030 Jun 15, 2024
0436080
revert cross out CHANGELOG
CeciliaZ030 Jun 16, 2024
0bfab25
change local dep to remote
CeciliaZ030 Jun 16, 2024
8b270d2
Update CHANGELOG.md
CeciliaZ030 Jun 16, 2024
a7fb540
recover const G1_POINTS, G2_POINTS
CeciliaZ030 Jun 16, 2024
db1e3fa
Merge branch 'swap-rust-kzg' of https://github.com/taikoxyz/revm into…
CeciliaZ030 Jun 16, 2024
8d68e96
pub static DEFAULT: OnceBox<KZGSettings> = OnceBox::new();
CeciliaZ030 Jun 19, 2024
1aee4c2
fix env_setting load kzg_settings
CeciliaZ030 Jun 21, 2024
97a72b7
expose G1 G2
CeciliaZ030 Jun 21, 2024
8416e5d
update Cargo.lock
CeciliaZ030 Jun 24, 2024
b24c53d
patch-v0.10.8
CeciliaZ030 Jun 25, 2024
542dd0f
rust-kzg-zkcrypto default-features = false
CeciliaZ030 Jun 25, 2024
e568d95
Squashed commit of the following:
CeciliaZ030 Jun 25, 2024
d6e6d90
compile after merge
CeciliaZ030 Jun 25, 2024
ceb13a1
Squashed commit of the following:
CeciliaZ030 Jun 25, 2024
eb7125e
merge again
CeciliaZ030 Jun 25, 2024
362cf70
v0.3.12-serialize
CeciliaZ030 Jun 25, 2024
08db080
blst = ["revm-precompile/blst"]
CeciliaZ030 Jun 25, 2024
8833a15
revm default = ["std", "kzg", "secp256k1", "blst"]
CeciliaZ030 Jun 25, 2024
60bef80
Merge branch 'v36-taiko' into v36-taiko-kzg
CeciliaZ030 Jun 25, 2024
fa7a268
update
CeciliaZ030 Jun 25, 2024
867b336
#dummy
CeciliaZ030 Jun 25, 2024
425f983
#dummy
CeciliaZ030 Jun 25, 2024
44e1c7f
#dummy
CeciliaZ030 Jun 25, 2024
610eaf6
change "kzg" to "c-kzg"
CeciliaZ030 Jun 26, 2024
402c226
remove secp256k1 patch
CeciliaZ030 Jun 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,107 changes: 691 additions & 416 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ debug = true
[profile.ethtests]
inherits = "test"
opt-level = 3

[patch.crates-io]
blst = { git = "https://github.com/CeciliaZ030/blst.git", branch = "v0.3.12-serialize" }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ cd revm
cargo build --release
```

**_Note:_** `clang` is required for building revm with `c-kzg` or `secp256k1` feature flags as they depend on `C` libraries. If you don't have it installed, you can install it with `apt install clang`.
**_Note:_** `clang` is required for building revm with ~~`c-kzg`~~ or `secp256k1` feature flags as they depend on `C` libraries. If you don't have it installed, you can install it with `apt install clang`.

# Running eth tests

Expand Down
1 change: 0 additions & 1 deletion crates/interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ hashbrown = ["revm-primitives/hashbrown"]
serde = ["dep:serde", "revm-primitives/serde"]
arbitrary = ["std", "revm-primitives/arbitrary"]
asm-keccak = ["revm-primitives/asm-keccak"]
portable = ["revm-primitives/portable"]
parse = ["dep:paste", "dep:phf"]

taiko = ["revm-primitives/taiko"]
Expand Down
4 changes: 2 additions & 2 deletions crates/precompile/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ Full git log:
date 28.09.2023

Summary:
* Cancun EIP-4844 precompile. It is behind `c-kzg` that is enabled by default
the reason is that c-kzg fails to build on wasm and some docker images.
* Cancun EIP-4844 precompile. It is behind `kzg` that is enabled by default
~~the reason is that c-kzg fails to build on wasm and some docker images.~~
* no_std support
* small fixes to return out of gas for modepx and pairing precompiles.

Expand Down
28 changes: 13 additions & 15 deletions crates/precompile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,39 @@ revm-primitives = { path = "../primitives", version = "4.0.0", default-features
bn = { package = "substrate-bn", version = "0.6", default-features = false }
once_cell = { version = "1.19", default-features = false, features = ["alloc"] }
ripemd = { version = "0.1", default-features = false }
sha2 = { version = "0.10", default-features = false }
# sha2 = { version = "0.10", default-features = false }
sha2 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", branch = "patch-v0.10.8" }

# modexp precompile
aurora-engine-modexp = { version = "1.1", default-features = false }

# Optional KZG point evaluation precompile
c-kzg = { git = "https://github.com/brechtpd/c-kzg-4844", branch = "for-alpha7", default-features = false, optional = true, features = [
"preload-kzg-settings",
"no-threads",
]}
rust-kzg-zkcrypto = { git = "https://github.com/CeciliaZ030/rust-kzg.git", branch = "sp1-patch", default-features = false, optional = true }
kzg = { git = "https://github.com/CeciliaZ030/rust-kzg.git", branch = "sp1-patch",default-features = false, optional = true }


# ecRecover precompile
k256 = { version = "0.13.3", default-features = false, features = ["ecdsa"] }
secp256k1 = { version = "0.29", default-features = false, features = [
"alloc",
"recovery",
"rand",
secp256k1 = { version = "0.29", default-features = false, optional = true, features = [
"global-context",
], optional = true }
"recovery",
] }

# BLS12-381 precompiles
blst = { version = "0.3.11", optional = true }
blst = { version = "0.3.12", optional = true }

[dev-dependencies]
criterion = { version = "0.5" }
rand = { version = "0.8", features = ["std"] }

[features]
default = ["std", "c-kzg", "secp256k1", "portable", "blst"]
default = ["std", "c-kzg", "secp256k1", "blst"]
std = [
"revm-primitives/std",
"k256/std",
"once_cell/std",
"ripemd/std",
"sha2/std",
"c-kzg?/std",
"secp256k1?/std",
]
hashbrown = ["revm-primitives/hashbrown"]
Expand All @@ -78,8 +76,8 @@ negate-taiko-default-handler = [
# These libraries may not work on all no_std platforms as they depend on C.

# Enables the KZG point evaluation precompile.
c-kzg = ["dep:c-kzg", "revm-primitives/c-kzg"]
portable = ["revm-primitives/portable", "c-kzg?/portable"]
c-kzg = ["dep:kzg", "dep:rust-kzg-zkcrypto", "revm-primitives/c-kzg"]


# Use `secp256k1` as a faster alternative to `k256`.
# The problem that `secp256k1` has is it fails to build for `wasm` target on Windows and Mac as it is c lib.
Expand Down
38 changes: 21 additions & 17 deletions crates/precompile/src/kzg_point_evaluation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use core::mem::MaybeUninit;

use crate::{Address, Error, Precompile, PrecompileResult, PrecompileWithAddress};
use c_kzg::{Bytes32, Bytes48, KzgProof, KzgSettings};
use kzg::eip_4844::{Bytes32, Bytes48, C_KZG_RET_OK};
use revm_primitives::{hex_literal::hex, Bytes, Env};
use rust_kzg_zkcrypto::eip_4844::verify_kzg_proof;
use sha2::{Digest, Sha256};

pub const POINT_EVALUATION: PrecompileWithAddress =
Expand Down Expand Up @@ -48,9 +51,21 @@ pub fn run(input: &Bytes, gas_limit: u64, env: &Env) -> PrecompileResult {
let z = as_bytes32(&input[32..64]);
let y = as_bytes32(&input[64..96]);
let proof = as_bytes48(&input[144..192]);
if !verify_kzg_proof(commitment, z, y, proof, env.cfg.kzg_settings.get()) {
return Err(Error::BlobVerifyKzgProofFailed);
}
let mut verified: MaybeUninit<bool> = MaybeUninit::uninit();

unsafe {
if verify_kzg_proof(
verified.as_mut_ptr(),
commitment,
z,
y,
proof,
env.cfg.kzg_settings.get(),
) != C_KZG_RET_OK
{
return Err(Error::BlobVerifyKzgProofFailed);
}
};
#[cfg(feature = "sp1-cycle-tracker")]
println!("cycle-tracker-end: kzg");

Expand All @@ -66,17 +81,6 @@ pub fn kzg_to_versioned_hash(commitment: &[u8]) -> [u8; 32] {
hash
}

#[inline]
pub fn verify_kzg_proof(
commitment: &Bytes48,
z: &Bytes32,
y: &Bytes32,
proof: &Bytes48,
kzg_settings: &KzgSettings,
) -> bool {
KzgProof::verify_kzg_proof(commitment, z, y, proof, kzg_settings).unwrap_or(false)
}

#[inline]
#[track_caller]
pub fn as_array<const N: usize>(bytes: &[u8]) -> &[u8; N] {
Expand All @@ -85,14 +89,14 @@ pub fn as_array<const N: usize>(bytes: &[u8]) -> &[u8; N] {

#[inline]
#[track_caller]
pub fn as_bytes32(bytes: &[u8]) -> &Bytes32 {
pub fn as_bytes32(bytes: &[u8]) -> *const Bytes32 {
// SAFETY: `#[repr(C)] Bytes32([u8; 32])`
unsafe { &*as_array::<32>(bytes).as_ptr().cast() }
}

#[inline]
#[track_caller]
pub fn as_bytes48(bytes: &[u8]) -> &Bytes48 {
pub fn as_bytes48(bytes: &[u8]) -> *const Bytes48 {
// SAFETY: `#[repr(C)] Bytes48([u8; 48])`
unsafe { &*as_array::<48>(bytes).as_ptr().cast() }
}
Expand Down
2 changes: 1 addition & 1 deletion crates/precompile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl Precompiles {

/// Returns precompiles for Cancun spec.
///
/// If the `c-kzg` feature is not enabled KZG Point Evaluation precompile will not be included,
/// If the `kzg` feature is not enabled KZG Point Evaluation precompile will not be included,
/// effectively making this the same as Berlin.
pub fn cancun() -> &'static Self {
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,4 @@ Changelog:
# v1.0.0
date: 29.01.2023

Interpreter was extracted from main revm crate at the revm v3.0.0 version.
Interpreter was extracted from main revm crate at the revm v3.0.0 version.
15 changes: 6 additions & 9 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ auto_impl = "1.2"
bitvec = { version = "1", default-features = false, features = ["alloc"] }
bitflags = { version = "2.5.0", default-features = false }

# For setting the CfgEnv KZGSettings. Enabled by c-kzg flag.
c-kzg = { git = "https://github.com/brechtpd/c-kzg-4844", branch = "for-alpha7", default-features = false, optional = true, features = [
"preload-kzg-settings",
"no-threads",
]}
rust-kzg-zkcrypto = { git = "https://github.com/CeciliaZ030/rust-kzg.git", branch = "sp1-patch", default-features = false, optional = true }
kzg = { git = "https://github.com/CeciliaZ030/rust-kzg.git", branch = "sp1-patch", optional = true }


once_cell = { version = "1.19", default-features = false, optional = true }

# utility
Expand All @@ -53,7 +52,7 @@ serde = { version = "1.0", default-features = false, features = [
hex = { version = "0.4", default-features = false }

[features]
default = ["std", "c-kzg", "portable"]
default = ["std", "c-kzg"]
std = [
"serde?/std",
"alloy-primitives/std",
Expand All @@ -69,11 +68,9 @@ serde = [
"hashbrown/serde",
"bitvec/serde",
"bitflags/serde",
"c-kzg?/serde",
]
arbitrary = ["std", "alloy-primitives/arbitrary", "bitflags/arbitrary"]
asm-keccak = ["alloy-primitives/asm-keccak"]
portable = ["c-kzg?/portable"]

optimism = []
taiko = []
Expand All @@ -100,4 +97,4 @@ optional_beneficiary_reward = []
rand = ["alloy-primitives/rand"]

# See comments in `revm-precompile`
c-kzg = ["dep:c-kzg", "dep:once_cell", "dep:derive_more"]
c-kzg = ["dep:kzg", "dep:rust-kzg-zkcrypto", "dep:once_cell", "dep:derive_more"]
6 changes: 3 additions & 3 deletions crates/primitives/src/kzg.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod env_settings;
mod trusted_setup_points;

pub use c_kzg::KzgSettings;
pub use env_settings::EnvKzgSettings;
pub use kzg::eip_4844::CKZGSettings as KZGSettings;
pub use trusted_setup_points::{
parse_kzg_trusted_setup, G1Points, G2Points, KzgErrors, BYTES_PER_G1_POINT, BYTES_PER_G2_POINT,
G1_POINTS, G2_POINTS, NUM_G1_POINTS, NUM_G2_POINTS,
parse_kzg_trusted_setup, G1Points, G2Points, KzgErrors, BYTES_PER_G1, BYTES_PER_G2,
NUM_G1_POINTS, NUM_G2_POINTS, G2_POINTS, G1_POINTS,
};
53 changes: 41 additions & 12 deletions crates/primitives/src/kzg/env_settings.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@

use super::{
trusted_setup_points::{G1_POINTS, G2_POINTS},
KzgSettings,
trusted_setup_points::KzgErrors, G1Points, G2Points, KZGSettings
};
use core::{
fmt,
hash::{Hash, Hasher},
mem::MaybeUninit,
};
use kzg::eip_4844::{
load_trusted_setup_rust, BYTES_PER_G1, BYTES_PER_G2, C_KZG_RET_OK, FIELD_ELEMENTS_PER_BLOB,
TRUSTED_SETUP_NUM_G2_POINTS,
};
use core::hash::{Hash, Hasher};
use once_cell::race::OnceBox;
use once_cell::{race::OnceBox, unsync::OnceCell};
use rust_kzg_zkcrypto::eip_4844::load_trusted_setup;
use std::{boxed::Box, sync::Arc};

/// KZG Settings that allow us to specify a custom trusted setup.
/// or use hardcoded default settings.
#[derive(Debug, Clone, Default, Eq)]
#[derive(Debug, Clone, Default)]
pub enum EnvKzgSettings {
/// Default mainnet trusted setup
#[default]
Default,
/// Custom trusted setup.
Custom(Arc<c_kzg::KzgSettings>),
Custom(Arc<KZGSettings>),
}

// Implement PartialEq and Hash manually because `c_kzg::KzgSettings` does not implement them
// Implement PartialEq and Hash manually because `KZGSettings` does not implement them
impl PartialEq for EnvKzgSettings {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
Expand All @@ -28,6 +38,8 @@ impl PartialEq for EnvKzgSettings {
}
}

impl Eq for EnvKzgSettings {}

impl Hash for EnvKzgSettings {
fn hash<H: Hasher>(&self, state: &mut H) {
core::mem::discriminant(self).hash(state);
Expand All @@ -42,18 +54,35 @@ impl EnvKzgSettings {
/// Return set KZG settings.
///
/// In will initialize the default settings if it is not already loaded.
pub fn get(&self) -> &KzgSettings {
pub fn get(&self) -> &KZGSettings {
match self {
Self::Default => {
static DEFAULT: OnceBox<KzgSettings> = OnceBox::new();
static DEFAULT: OnceBox<KZGSettings> = OnceBox::new();
DEFAULT.get_or_init(|| {
let settings =
KzgSettings::load_trusted_setup(G1_POINTS.as_ref(), G2_POINTS.as_ref())
.expect("failed to load default trusted setup");
Box::new(settings)
let mut kzg_settings = MaybeUninit::<KZGSettings>::uninit();
unsafe {
if load_trusted_setup(
kzg_settings.as_mut_ptr(),
G1_POINTS.as_ptr().cast(),
G1_POINTS.len(),
G2_POINTS.as_ptr().cast(),
G2_POINTS.len(),
) != C_KZG_RET_OK
{
panic!("Failed to load default trusted setup");
}
Box::new(kzg_settings.assume_init())
}
})
}
Self::Custom(settings) => settings,
}
}
}


#[test]
fn test_load_trusted_setup() {
let kzg_env = EnvKzgSettings::default();
let settings = kzg_env.get();
}
18 changes: 10 additions & 8 deletions crates/primitives/src/kzg/trusted_setup_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@ use core::fmt;
use derive_more::{AsMut, AsRef, Deref, DerefMut};
use std::boxed::Box;

pub use c_kzg::{BYTES_PER_G1_POINT, BYTES_PER_G2_POINT};
pub use kzg::eip_4844::{
BYTES_PER_G1, BYTES_PER_G2, FIELD_ELEMENTS_PER_BLOB, TRUSTED_SETUP_NUM_G2_POINTS,
};

/// Number of G1 Points.
pub const NUM_G1_POINTS: usize = 4096;
pub const NUM_G1_POINTS: usize = FIELD_ELEMENTS_PER_BLOB;

/// Number of G2 Points.
pub const NUM_G2_POINTS: usize = 65;
pub const NUM_G2_POINTS: usize = TRUSTED_SETUP_NUM_G2_POINTS;

/// A newtype over list of G1 point from kzg trusted setup.
#[derive(Debug, Clone, PartialEq, AsRef, AsMut, Deref, DerefMut)]
#[repr(transparent)]
pub struct G1Points(pub [[u8; BYTES_PER_G1_POINT]; NUM_G1_POINTS]);
pub struct G1Points(pub [[u8; BYTES_PER_G1]; NUM_G1_POINTS]);

impl Default for G1Points {
fn default() -> Self {
Self([[0; BYTES_PER_G1_POINT]; NUM_G1_POINTS])
Self([[0; BYTES_PER_G1]; NUM_G1_POINTS])
}
}

/// A newtype over list of G2 point from kzg trusted setup.
#[derive(Debug, Clone, Eq, PartialEq, AsRef, AsMut, Deref, DerefMut)]
#[repr(transparent)]
pub struct G2Points(pub [[u8; BYTES_PER_G2_POINT]; NUM_G2_POINTS]);
pub struct G2Points(pub [[u8; BYTES_PER_G2]; NUM_G2_POINTS]);

impl Default for G2Points {
fn default() -> Self {
Self([[0; BYTES_PER_G2_POINT]; NUM_G2_POINTS])
Self([[0; BYTES_PER_G2]; NUM_G2_POINTS])
}
}

Expand All @@ -49,7 +51,7 @@ pub const G2_POINTS: &G2Points = {
/// Parses the contents of a KZG trusted setup file into a list of G1 and G2 points.
///
/// These can then be used to create a KZG settings object with
/// [`KzgSettings::load_trusted_setup`](c_kzg::KzgSettings::load_trusted_setup).
/// [`KzgSettings::load_trusted_setup`](KZGSettings::load_trusted_setup).
pub fn parse_kzg_trusted_setup(
trusted_setup: &str,
) -> Result<(Box<G1Points>, Box<G2Points>), KzgErrors> {
Expand Down
Loading