Skip to content

Commit c04dbec

Browse files
yash-atreyamattsse
andauthored
chore(deps): c-kzg 2.0 (#2240)
* chore(deps): c-kzg 2.0 * todo: kzg load trusted setup? * precompute = 0 * nit * use ethereum_kzg_settings helper * cleanup + rm G1 and G2 point constants * rm g1 g2 points.bin * precompute const * chore: bump ckzg --------- Co-authored-by: Matthias Seitz <[email protected]>
1 parent 89af055 commit c04dbec

File tree

8 files changed

+50
-77
lines changed

8 files changed

+50
-77
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ tree_hash = "0.9.1"
9696
tree_hash_derive = "0.9.1"
9797

9898
# crypto
99-
c-kzg = { version = "1.0", default-features = false }
99+
c-kzg = { version = "2.1", default-features = false }
100100
elliptic-curve = { version = "0.13", default-features = false }
101101
k256 = { version = "0.13", default-features = false, features = ["ecdsa"] }
102102
sha2 = { version = "0.10", default-features = false }

crates/eips/Cargo.toml

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ exclude.workspace = true
1414
[package.metadata.docs.rs]
1515
all-features = true
1616
rustdoc-args = [
17-
"-Zunstable-options",
18-
"--generate-link-to-definition",
19-
"--show-type-layout",
17+
"-Zunstable-options",
18+
"--generate-link-to-definition",
19+
"--show-type-layout",
2020
]
2121

2222
[lints]
@@ -37,15 +37,16 @@ alloy-serde = { workspace = true, optional = true }
3737
serde = { workspace = true, optional = true }
3838

3939
# kzg
40-
c-kzg = { workspace = true, optional = true }
40+
c-kzg = { workspace = true, optional = true, features = [
41+
"ethereum_kzg_settings",
42+
] }
4143
derive_more = { workspace = true, features = [
42-
"as_ref",
43-
"deref",
44-
"deref_mut",
45-
"from",
46-
"into_iterator",
44+
"as_ref",
45+
"deref",
46+
"deref_mut",
47+
"from",
48+
"into_iterator",
4749
], default-features = false }
48-
once_cell = { workspace = true, features = ["race", "alloc"], optional = true }
4950
sha2 = { workspace = true, optional = true }
5051

5152
# ssz
@@ -60,9 +61,9 @@ auto_impl.workspace = true
6061

6162
[dev-dependencies]
6263
alloy-primitives = { workspace = true, features = [
63-
"rand",
64-
"serde",
65-
"arbitrary",
64+
"rand",
65+
"serde",
66+
"arbitrary",
6667
] }
6768
arbitrary = { workspace = true, features = ["derive"] }
6869
serde_json.workspace = true
@@ -75,15 +76,14 @@ std = [
7576
"alloy-rlp/std",
7677
"serde?/std",
7778
"c-kzg?/std",
78-
"once_cell?/std",
7979
"alloy-serde?/std",
8080
"alloy-eip2124/std",
8181
"alloy-eip2930/std",
8282
"alloy-eip7702/std",
8383
"derive_more/std",
8484
"serde_json/std",
8585
"sha2?/std",
86-
"either/std"
86+
"either/std",
8787
]
8888
serde = [
8989
"dep:alloy-serde",
@@ -94,22 +94,22 @@ serde = [
9494
"alloy-eip7702/serde",
9595
"alloy-eip2124/serde",
9696
"either/serde",
97-
"rand/serde"
97+
"rand/serde",
9898
]
9999
serde-bincode-compat = ["alloy-eip7702/serde-bincode-compat"]
100-
kzg = ["kzg-sidecar", "sha2", "dep:c-kzg", "dep:once_cell"]
100+
kzg = ["kzg-sidecar", "sha2", "dep:c-kzg"]
101101
kzg-sidecar = ["sha2"]
102102
k256 = ["alloy-eip7702/k256"]
103103
sha2 = ["dep:sha2"]
104104
ssz = ["std", "dep:ethereum_ssz", "dep:ethereum_ssz_derive"]
105105
arbitrary = [
106-
"std",
107-
"kzg-sidecar",
108-
"dep:arbitrary",
109-
"alloy-primitives/arbitrary",
110-
"alloy-serde?/arbitrary",
111-
"alloy-eip2124/arbitrary",
112-
"alloy-eip2930/arbitrary",
113-
"alloy-eip7702/arbitrary",
114-
"alloy-eip7702/k256",
106+
"std",
107+
"kzg-sidecar",
108+
"dep:arbitrary",
109+
"alloy-primitives/arbitrary",
110+
"alloy-serde?/arbitrary",
111+
"alloy-eip2124/arbitrary",
112+
"alloy-eip2930/arbitrary",
113+
"alloy-eip7702/arbitrary",
114+
"alloy-eip7702/k256",
115115
]

crates/eips/src/eip4844/builder.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
use crate::eip4844::Blob;
2-
#[cfg(feature = "kzg")]
3-
use c_kzg::{KzgCommitment, KzgProof};
4-
51
use crate::eip4844::{
6-
utils::WholeFe, BYTES_PER_BLOB, FIELD_ELEMENTS_PER_BLOB, FIELD_ELEMENT_BYTES_USIZE,
2+
utils::WholeFe, Blob, BYTES_PER_BLOB, FIELD_ELEMENTS_PER_BLOB, FIELD_ELEMENT_BYTES_USIZE,
73
};
84
use alloc::vec::Vec;
95

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

389385
// SAFETY: same size
390386
unsafe {

crates/eips/src/eip4844/env_settings.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
use crate::eip4844::trusted_setup_points::{G1_POINTS, G2_POINTS};
21
use alloc::sync::Arc;
32
use core::hash::{Hash, Hasher};
43

54
// Re-export for convenience
65
pub use c_kzg::KzgSettings;
76

7+
/// Precompute value that optimizes computing cell kzg proofs.
8+
///
9+
/// Set to 0 as we do not use `compute_cells_and_kzg_proofs` or `recover_cells_and_kzg_proofs`.
10+
///
11+
/// Learn more: <https://github.com/ethereum/c-kzg-4844/blob/dffa18ee350aeef38f749ffad24a27c1645fb4f8/README.md?plain=1#L112>
12+
const PRECOMPUTE: u64 = 0;
13+
814
/// KZG settings.
915
#[derive(Clone, Debug, Default, Eq)]
1016
pub enum EnvKzgSettings {
@@ -43,25 +49,7 @@ impl EnvKzgSettings {
4349
#[inline]
4450
pub fn get(&self) -> &KzgSettings {
4551
match self {
46-
Self::Default => {
47-
let load = || {
48-
KzgSettings::load_trusted_setup(&G1_POINTS.0, &G2_POINTS.0)
49-
.expect("failed to load default trusted setup")
50-
};
51-
#[cfg(feature = "std")]
52-
{
53-
use once_cell as _;
54-
use std::sync::OnceLock;
55-
static DEFAULT: OnceLock<KzgSettings> = OnceLock::new();
56-
DEFAULT.get_or_init(load)
57-
}
58-
#[cfg(not(feature = "std"))]
59-
{
60-
use once_cell::race::OnceBox;
61-
static DEFAULT: OnceBox<KzgSettings> = OnceBox::new();
62-
DEFAULT.get_or_init(|| alloc::boxed::Box::new(load()))
63-
}
64-
}
52+
Self::Default => c_kzg::ethereum_kzg_settings(PRECOMPUTE),
6553
Self::Custom(settings) => settings,
6654
}
6755
}
@@ -71,7 +59,7 @@ impl EnvKzgSettings {
7159
pub fn load_from_trusted_setup_file(
7260
trusted_setup_file: &std::path::Path,
7361
) -> Result<Self, c_kzg::Error> {
74-
let settings = KzgSettings::load_trusted_setup_file(trusted_setup_file)?;
62+
let settings = KzgSettings::load_trusted_setup_file(trusted_setup_file, PRECOMPUTE)?;
7563
Ok(Self::Custom(Arc::new(settings)))
7664
}
7765
}

crates/eips/src/eip4844/g1_points.bin

-192 KB
Binary file not shown.

crates/eips/src/eip4844/g2_points.bin

-6.09 KB
Binary file not shown.

crates/eips/src/eip4844/sidecar.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ impl BlobTransactionSidecarItem {
143143
let proof = c_kzg::Bytes48::from_bytes(self.kzg_proof.as_slice())
144144
.map_err(BlobTransactionValidationError::KZGError)?;
145145

146-
let result = c_kzg::KzgProof::verify_blob_kzg_proof(&blob, &commitment, &proof, settings)
146+
let result = settings
147+
.verify_blob_kzg_proof(&blob, &commitment, &proof)
147148
.map_err(BlobTransactionValidationError::KZGError)?;
148149

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

270271
// SAFETY: ALL types have the same size
271272
let res = unsafe {
272-
c_kzg::KzgProof::verify_blob_kzg_proof_batch(
273+
proof_settings.verify_blob_kzg_proof_batch(
273274
// blobs
274275
core::mem::transmute::<&[Blob], &[c_kzg::Blob]>(self.blobs.as_slice()),
275276
// commitments
276277
core::mem::transmute::<&[Bytes48], &[c_kzg::Bytes48]>(self.commitments.as_slice()),
277278
// proofs
278279
core::mem::transmute::<&[Bytes48], &[c_kzg::Bytes48]>(self.proofs.as_slice()),
279-
proof_settings,
280280
)
281281
}
282282
.map_err(BlobTransactionValidationError::KZGError)?;
@@ -339,23 +339,23 @@ impl BlobTransactionSidecar {
339339
#[cfg(all(feature = "kzg", any(test, feature = "arbitrary")))]
340340
pub fn try_from_blobs(blobs: Vec<c_kzg::Blob>) -> Result<Self, c_kzg::Error> {
341341
use crate::eip4844::env_settings::EnvKzgSettings;
342-
use c_kzg::{KzgCommitment, KzgProof};
343342

344343
let kzg_settings = EnvKzgSettings::Default;
345344

346345
let commitments = blobs
347346
.iter()
348347
.map(|blob| {
349-
KzgCommitment::blob_to_kzg_commitment(&blob.clone(), kzg_settings.get())
350-
.map(|blob| blob.to_bytes())
348+
kzg_settings.get().blob_to_kzg_commitment(&blob.clone()).map(|blob| blob.to_bytes())
351349
})
352350
.collect::<Result<Vec<_>, _>>()?;
353351

354352
let proofs = blobs
355353
.iter()
356354
.zip(commitments.iter())
357355
.map(|(blob, commitment)| {
358-
KzgProof::compute_blob_kzg_proof(blob, commitment, kzg_settings.get())
356+
kzg_settings
357+
.get()
358+
.compute_blob_kzg_proof(blob, commitment)
359359
.map(|blob| blob.to_bytes())
360360
})
361361
.collect::<Result<Vec<_>, _>>()?;

crates/eips/src/eip4844/trusted_setup_points.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use alloy_primitives::hex;
33
use core::fmt;
44
use derive_more::{AsMut, AsRef, Deref, DerefMut};
55

6-
pub use c_kzg::{BYTES_PER_G1_POINT, BYTES_PER_G2_POINT};
6+
pub use c_kzg::{BYTES_PER_COMMITMENT as BYTES_PER_G1_POINT, FIELD_ELEMENTS_PER_BLOB};
7+
8+
/// Bytes per G2 point.
9+
pub const BYTES_PER_G2_POINT: usize = 96;
710

811
/// Number of G1 Points.
912
pub const NUM_G1_POINTS: usize = 4096;
@@ -33,20 +36,6 @@ impl Default for G2Points {
3336
}
3437
}
3538

36-
/// Default G1 points.
37-
pub const G1_POINTS: &G1Points = {
38-
const BYTES: &[u8] = include_bytes!("./g1_points.bin");
39-
assert!(BYTES.len() == core::mem::size_of::<G1Points>());
40-
unsafe { &*BYTES.as_ptr().cast::<G1Points>() }
41-
};
42-
43-
/// Default G2 points.
44-
pub const G2_POINTS: &G2Points = {
45-
const BYTES: &[u8] = include_bytes!("./g2_points.bin");
46-
assert!(BYTES.len() == core::mem::size_of::<G2Points>());
47-
unsafe { &*BYTES.as_ptr().cast::<G2Points>() }
48-
};
49-
5039
/// Parses the contents of a KZG trusted setup file into a list of G1 and G2 points.
5140
///
5241
/// These can then be used to create a KZG settings object with

0 commit comments

Comments
 (0)