Skip to content

chore(deps): c-kzg 2.0 #2240

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

Merged
merged 9 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ tree_hash = "0.9.1"
tree_hash_derive = "0.9.1"

# crypto
c-kzg = { version = "1.0", default-features = false }
c-kzg = { version = "2.0", default-features = false }
elliptic-curve = { version = "0.13", default-features = false }
k256 = { version = "0.13", default-features = false, features = ["ecdsa"] }
sha2 = { version = "0.10", default-features = false }
Expand Down
52 changes: 26 additions & 26 deletions crates/eips/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ exclude.workspace = true
[package.metadata.docs.rs]
all-features = true
rustdoc-args = [
"-Zunstable-options",
"--generate-link-to-definition",
"--show-type-layout",
"-Zunstable-options",
"--generate-link-to-definition",
"--show-type-layout",
]

[lints]
Expand All @@ -37,15 +37,16 @@ alloy-serde = { workspace = true, optional = true }
serde = { workspace = true, optional = true }

# kzg
c-kzg = { workspace = true, optional = true }
c-kzg = { workspace = true, optional = true, features = [
"ethereum_kzg_settings",
] }
derive_more = { workspace = true, features = [
"as_ref",
"deref",
"deref_mut",
"from",
"into_iterator",
"as_ref",
"deref",
"deref_mut",
"from",
"into_iterator",
], default-features = false }
once_cell = { workspace = true, features = ["race", "alloc"], optional = true }
sha2 = { workspace = true, optional = true }

# ssz
Expand All @@ -60,9 +61,9 @@ auto_impl.workspace = true

[dev-dependencies]
alloy-primitives = { workspace = true, features = [
"rand",
"serde",
"arbitrary",
"rand",
"serde",
"arbitrary",
] }
arbitrary = { workspace = true, features = ["derive"] }
serde_json.workspace = true
Expand All @@ -75,15 +76,14 @@ std = [
"alloy-rlp/std",
"serde?/std",
"c-kzg?/std",
"once_cell?/std",
"alloy-serde?/std",
"alloy-eip2124/std",
"alloy-eip2930/std",
"alloy-eip7702/std",
"derive_more/std",
"serde_json/std",
"sha2?/std",
"either/std"
"either/std",
]
serde = [
"dep:alloy-serde",
Expand All @@ -94,22 +94,22 @@ serde = [
"alloy-eip7702/serde",
"alloy-eip2124/serde",
"either/serde",
"rand/serde"
"rand/serde",
]
serde-bincode-compat = ["alloy-eip7702/serde-bincode-compat"]
kzg = ["kzg-sidecar", "sha2", "dep:c-kzg", "dep:once_cell"]
kzg = ["kzg-sidecar", "sha2", "dep:c-kzg"]
kzg-sidecar = ["sha2"]
k256 = ["alloy-eip7702/k256"]
sha2 = ["dep:sha2"]
ssz = ["std", "dep:ethereum_ssz", "dep:ethereum_ssz_derive"]
arbitrary = [
"std",
"kzg-sidecar",
"dep:arbitrary",
"alloy-primitives/arbitrary",
"alloy-serde?/arbitrary",
"alloy-eip2124/arbitrary",
"alloy-eip2930/arbitrary",
"alloy-eip7702/arbitrary",
"alloy-eip7702/k256",
"std",
"kzg-sidecar",
"dep:arbitrary",
"alloy-primitives/arbitrary",
"alloy-serde?/arbitrary",
"alloy-eip2124/arbitrary",
"alloy-eip2930/arbitrary",
"alloy-eip7702/arbitrary",
"alloy-eip7702/k256",
]
10 changes: 3 additions & 7 deletions crates/eips/src/eip4844/builder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use crate::eip4844::Blob;
#[cfg(feature = "kzg")]
use c_kzg::{KzgCommitment, KzgProof};

use crate::eip4844::{
utils::WholeFe, BYTES_PER_BLOB, FIELD_ELEMENTS_PER_BLOB, FIELD_ELEMENT_BYTES_USIZE,
utils::WholeFe, Blob, BYTES_PER_BLOB, FIELD_ELEMENTS_PER_BLOB, FIELD_ELEMENT_BYTES_USIZE,
};
use alloc::vec::Vec;

Expand Down Expand Up @@ -383,8 +379,8 @@ impl<T: SidecarCoder> SidecarBuilder<T> {
for blob in &self.inner.blobs {
// SAFETY: same size
let blob = unsafe { core::mem::transmute::<&Blob, &c_kzg::Blob>(blob) };
let commitment = KzgCommitment::blob_to_kzg_commitment(blob, settings)?;
let proof = KzgProof::compute_blob_kzg_proof(blob, &commitment.to_bytes(), settings)?;
let commitment = settings.blob_to_kzg_commitment(blob)?;
let proof = settings.compute_blob_kzg_proof(blob, &commitment.to_bytes())?;

// SAFETY: same size
unsafe {
Expand Down
24 changes: 3 additions & 21 deletions crates/eips/src/eip4844/env_settings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::eip4844::trusted_setup_points::{G1_POINTS, G2_POINTS};
use alloc::sync::Arc;
use core::hash::{Hash, Hasher};

Expand Down Expand Up @@ -43,25 +42,7 @@ impl EnvKzgSettings {
#[inline]
pub fn get(&self) -> &KzgSettings {
match self {
Self::Default => {
let load = || {
KzgSettings::load_trusted_setup(&G1_POINTS.0, &G2_POINTS.0)
.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::Default => c_kzg::ethereum_kzg_settings(0),
Self::Custom(settings) => settings,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this ckzg api is incredibly weird, because this allows providing an argument which is then ignored the second time this function is called because the kzgsettings are cached.
however, for this crate this is perfectly fine

}
}
Expand All @@ -71,7 +52,8 @@ impl EnvKzgSettings {
pub fn load_from_trusted_setup_file(
trusted_setup_file: &std::path::Path,
) -> Result<Self, c_kzg::Error> {
let settings = KzgSettings::load_trusted_setup_file(trusted_setup_file)?;
let precompute = 0; // See: <https://github.com/ethereum/c-kzg-4844/blob/dffa18ee350aeef38f749ffad24a27c1645fb4f8/README.md?plain=1#L112>
let settings = KzgSettings::load_trusted_setup_file(trusted_setup_file, precompute)?;
Ok(Self::Custom(Arc::new(settings)))
}
}
Binary file removed crates/eips/src/eip4844/g1_points.bin
Binary file not shown.
Binary file removed crates/eips/src/eip4844/g2_points.bin
Binary file not shown.
14 changes: 7 additions & 7 deletions crates/eips/src/eip4844/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ impl BlobTransactionSidecarItem {
let proof = c_kzg::Bytes48::from_bytes(self.kzg_proof.as_slice())
.map_err(BlobTransactionValidationError::KZGError)?;

let result = c_kzg::KzgProof::verify_blob_kzg_proof(&blob, &commitment, &proof, settings)
let result = settings
.verify_blob_kzg_proof(&blob, &commitment, &proof)
.map_err(BlobTransactionValidationError::KZGError)?;

result.then_some(()).ok_or(BlobTransactionValidationError::InvalidProof)
Expand Down Expand Up @@ -269,14 +270,13 @@ impl BlobTransactionSidecar {

// SAFETY: ALL types have the same size
let res = unsafe {
c_kzg::KzgProof::verify_blob_kzg_proof_batch(
proof_settings.verify_blob_kzg_proof_batch(
// blobs
core::mem::transmute::<&[Blob], &[c_kzg::Blob]>(self.blobs.as_slice()),
// commitments
core::mem::transmute::<&[Bytes48], &[c_kzg::Bytes48]>(self.commitments.as_slice()),
// proofs
core::mem::transmute::<&[Bytes48], &[c_kzg::Bytes48]>(self.proofs.as_slice()),
proof_settings,
)
}
.map_err(BlobTransactionValidationError::KZGError)?;
Expand Down Expand Up @@ -339,23 +339,23 @@ impl BlobTransactionSidecar {
#[cfg(all(feature = "kzg", any(test, feature = "arbitrary")))]
pub fn try_from_blobs(blobs: Vec<c_kzg::Blob>) -> Result<Self, c_kzg::Error> {
use crate::eip4844::env_settings::EnvKzgSettings;
use c_kzg::{KzgCommitment, KzgProof};

let kzg_settings = EnvKzgSettings::Default;

let commitments = blobs
.iter()
.map(|blob| {
KzgCommitment::blob_to_kzg_commitment(&blob.clone(), kzg_settings.get())
.map(|blob| blob.to_bytes())
kzg_settings.get().blob_to_kzg_commitment(&blob.clone()).map(|blob| blob.to_bytes())
})
.collect::<Result<Vec<_>, _>>()?;

let proofs = blobs
.iter()
.zip(commitments.iter())
.map(|(blob, commitment)| {
KzgProof::compute_blob_kzg_proof(blob, commitment, kzg_settings.get())
kzg_settings
.get()
.compute_blob_kzg_proof(blob, commitment)
.map(|blob| blob.to_bytes())
})
.collect::<Result<Vec<_>, _>>()?;
Expand Down
19 changes: 4 additions & 15 deletions crates/eips/src/eip4844/trusted_setup_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use alloy_primitives::hex;
use core::fmt;
use derive_more::{AsMut, AsRef, Deref, DerefMut};

pub use c_kzg::{BYTES_PER_G1_POINT, BYTES_PER_G2_POINT};
pub use c_kzg::{BYTES_PER_COMMITMENT as BYTES_PER_G1_POINT, FIELD_ELEMENTS_PER_BLOB};

/// Bytes per G2 point.
pub const BYTES_PER_G2_POINT: usize = 96;

/// Number of G1 Points.
pub const NUM_G1_POINTS: usize = 4096;
Expand Down Expand Up @@ -33,20 +36,6 @@ impl Default for G2Points {
}
}

/// Default G1 points.
pub const G1_POINTS: &G1Points = {
const BYTES: &[u8] = include_bytes!("./g1_points.bin");
assert!(BYTES.len() == core::mem::size_of::<G1Points>());
unsafe { &*BYTES.as_ptr().cast::<G1Points>() }
};

/// Default G2 points.
pub const G2_POINTS: &G2Points = {
const BYTES: &[u8] = include_bytes!("./g2_points.bin");
assert!(BYTES.len() == core::mem::size_of::<G2Points>());
unsafe { &*BYTES.as_ptr().cast::<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
Expand Down
Loading