Skip to content

Commit

Permalink
[Feature] Move Mainnet KZG group and Lazy<KzgSettings> (#368)
Browse files Browse the repository at this point in the history
* modification Eip1559Estimation return type

* chore: fmt

* done with trusted_setup_point

* mod: done with env_settings

* mod: fmt

* fix CI prompts

* debuging CI (no_std)

* fmt

* del .DS_store

* fmt

* touchup

---------

Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
  • Loading branch information
developeruche and DaniPopes authored Mar 22, 2024
1 parent 98e9012 commit 936f065
Show file tree
Hide file tree
Showing 9 changed files with 4,392 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.vscode
.idea
.env
.DS_Store
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ tokio-util = "0.7"
tokio-stream = "0.1"
tower = { version = "0.4", features = ["util"] }

# tracing
tracing = "0.1"
tracing-subscriber = "0.3"

Expand All @@ -84,14 +85,15 @@ base64 = "0.22"
bimap = "0.6"
home = "0.5"
itertools = "0.12"
once_cell = "1.19"
once_cell = { version = "1.19", default-features = false }
pin-project = "1.1"
rand = "0.8"
reqwest = { version = "0.11", default-features = false }
semver = "1.0"
thiserror = "1.0"
thiserror-no-std = "2.0.2"
url = "2.5"
derive_more = "0.99.17"

## serde
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
Expand Down
21 changes: 12 additions & 9 deletions crates/eips/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,28 @@ exclude.workspace = true

[dependencies]
alloy-primitives = { workspace = true, features = ["rlp"], default-features = false }
alloy-rlp = { workspace = true, features = ["derive"] }
alloy-rlp = { workspace = true, features = ["derive"], default-features = false }
alloy-serde.workspace = true

ethereum_ssz_derive = { workspace = true, optional = true }
ethereum_ssz = { workspace = true, optional = true }

# serde
serde = { workspace = true, default-features = false, optional = true }

# kzg
derive_more = { workspace = true, optional = true }
c-kzg = { version = "1.0", features = ["serde"], optional = true }
once_cell = { workspace = true, optional = true }

# ssz
ethereum_ssz_derive = { workspace = true, optional = true }
ethereum_ssz = { workspace = true, optional = true }

# arbitrary
arbitrary = { workspace = true, features = ["derive"], optional = true }
proptest = { workspace = true, optional = true }
proptest-derive = { workspace = true, optional = true }

[dev-dependencies]
alloy-primitives = { workspace = true, features = [
"rand",
"serde",
"arbitrary",
] }
alloy-primitives = { workspace = true, features = ["rand", "serde", "arbitrary"] }
arbitrary = { workspace = true, features = ["derive"] }
proptest = { workspace = true }
proptest-derive = { workspace = true }
Expand All @@ -49,4 +51,5 @@ arbitrary = [
"dep:proptest",
"alloy-primitives/arbitrary",
]
kzg = ["std", "dep:derive_more", "dep:c-kzg", "dep:once_cell"]
ssz = ["dep:ethereum_ssz", "dep:ethereum_ssz_derive", "alloy-primitives/ssz", "std"]
69 changes: 69 additions & 0 deletions crates/eips/src/eip4844/env_settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::eip4844::trusted_setup_points::{G1_POINTS, G2_POINTS};
use c_kzg::KzgSettings;
use std::{
hash::{Hash, Hasher},
sync::Arc,
};

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

// Implement PartialEq and Hash manually because `c_kzg::KzgSettings` does not implement them
impl PartialEq for EnvKzgSettings {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Default, Self::Default) => true,
(Self::Custom(a), Self::Custom(b)) => Arc::ptr_eq(a, b),
_ => false,
}
}
}

impl Hash for EnvKzgSettings {
fn hash<H: Hasher>(&self, state: &mut H) {
core::mem::discriminant(self).hash(state);
match self {
Self::Default => {}
Self::Custom(settings) => Arc::as_ptr(settings).hash(state),
}
}
}

impl EnvKzgSettings {
/// Return set KZG settings.
///
/// In will initialize the default settings if it is not already loaded.
pub fn get(&self) -> &KzgSettings {
match self {
Self::Default => {
let load = || {
KzgSettings::load_trusted_setup(G1_POINTS.as_ref(), G2_POINTS.as_ref())
.expect("failed to load default trusted setup")
};
#[cfg(feature = "std")]
{
use once_cell as _;
use std::sync::OnceLock;

static DEFAULT: OnceLock<KzgSettings> = OnceLock::new();
DEFAULT.get_or_init(load)
}
#[cfg(not(feature = "std"))]
{
use once_cell::race::OnceBox;
static DEFAULT: OnceBox<KzgSettings> = OnceBox::new();
DEFAULT.get_or_init(|| alloc::boxed::Box::new(load))
}
}
Self::Custom(settings) => settings,
}
}
}
Binary file added crates/eips/src/eip4844/g1_points.bin
Binary file not shown.
Binary file added crates/eips/src/eip4844/g2_points.bin
Binary file not shown.
7 changes: 7 additions & 0 deletions crates/eips/src/eip4844.rs → crates/eips/src/eip4844/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
//!
//! [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844

/// Module houses the KZG settings, enabling Custom and Default
#[cfg(feature = "kzg")]
pub mod env_settings;
/// This module contains functions and types used for parsing and utilizing the [Trusted Setup]( https://ceremony.ethereum.org/) for the `KzgSettings`.
#[cfg(feature = "kzg")]
pub mod trusted_setup_points;

use alloy_primitives::{b256, FixedBytes, U256};

/// The modulus of the BLS group used in the KZG commitment scheme. All field
Expand Down
Loading

0 comments on commit 936f065

Please sign in to comment.