Upgrade rust-eth-kzg to 0.8.0 (#7870)

#7864

The main breaking change in v0.8.0 is the `TrustedSetup` initialisation - it now requires a json string via `PeerDASTrustedSetup::from_json`.
This commit is contained in:
Jimmy Chen
2025-08-18 12:52:39 +10:00
committed by GitHub
parent 9200042910
commit aa8cba3741
14 changed files with 185 additions and 192 deletions

View File

@@ -4,13 +4,14 @@ use kzg::{trusted_setup::get_trusted_setup, TrustedSetup, NO_PRECOMPUTE};
use rust_eth_kzg::{DASContext, TrustedSetup as PeerDASTrustedSetup};
pub fn bench_init_context(c: &mut Criterion) {
let trusted_setup: TrustedSetup = serde_json::from_reader(get_trusted_setup().as_slice())
let trusted_setup_bytes = get_trusted_setup();
let trusted_setup_json = std::str::from_utf8(&trusted_setup_bytes)
.map_err(|e| format!("Unable to read trusted setup file: {}", e))
.expect("should have trusted setup");
c.bench_function("Initialize context rust_eth_kzg", |b| {
b.iter(|| {
let trusted_setup = PeerDASTrustedSetup::from(&trusted_setup);
let trusted_setup = PeerDASTrustedSetup::from_json(trusted_setup_json);
DASContext::new(
&trusted_setup,
rust_eth_kzg::UsePrecomp::Yes {
@@ -22,7 +23,7 @@ pub fn bench_init_context(c: &mut Criterion) {
c.bench_function("Initialize context c-kzg (4844)", |b| {
b.iter(|| {
let trusted_setup: TrustedSetup =
serde_json::from_reader(get_trusted_setup().as_slice())
serde_json::from_reader(trusted_setup_bytes.as_slice())
.map_err(|e| format!("Unable to read trusted setup file: {}", e))
.expect("should have trusted setup");
KzgSettings::load_trusted_setup(

View File

@@ -16,6 +16,7 @@ pub use c_kzg::{
BYTES_PER_FIELD_ELEMENT, BYTES_PER_PROOF, FIELD_ELEMENTS_PER_BLOB,
};
use crate::trusted_setup::load_trusted_setup;
pub use rust_eth_kzg::{
constants::{BYTES_PER_CELL, CELLS_PER_EXT_BLOB},
Cell, CellIndex as CellID, CellRef, TrustedSetup as PeerDASTrustedSetup,
@@ -37,6 +38,8 @@ pub type KzgBlobRef<'a> = &'a [u8; BYTES_PER_BLOB];
#[derive(Debug)]
pub enum Error {
/// An error from initialising the trusted setup.
TrustedSetupError(String),
/// An error from the underlying kzg library.
Kzg(c_kzg::Error),
/// A prover/verifier error from the rust-eth-kzg library.
@@ -65,16 +68,15 @@ pub struct Kzg {
}
impl Kzg {
pub fn new_from_trusted_setup_no_precomp(trusted_setup: TrustedSetup) -> Result<Self, Error> {
let peerdas_trusted_setup = PeerDASTrustedSetup::from(&trusted_setup);
let context = DASContext::new(&peerdas_trusted_setup, rust_eth_kzg::UsePrecomp::No);
pub fn new_from_trusted_setup_no_precomp(trusted_setup: &[u8]) -> Result<Self, Error> {
let (ckzg_trusted_setup, rkzg_trusted_setup) = load_trusted_setup(trusted_setup)?;
let context = DASContext::new(&rkzg_trusted_setup, rust_eth_kzg::UsePrecomp::No);
Ok(Self {
trusted_setup: KzgSettings::load_trusted_setup(
&trusted_setup.g1_monomial(),
&trusted_setup.g1_lagrange(),
&trusted_setup.g2_monomial(),
&ckzg_trusted_setup.g1_monomial(),
&ckzg_trusted_setup.g1_lagrange(),
&ckzg_trusted_setup.g2_monomial(),
NO_PRECOMPUTE,
)?,
context,
@@ -82,39 +84,14 @@ impl Kzg {
}
/// Load the kzg trusted setup parameters from a vec of G1 and G2 points.
pub fn new_from_trusted_setup(trusted_setup: TrustedSetup) -> Result<Self, Error> {
let peerdas_trusted_setup = PeerDASTrustedSetup::from(&trusted_setup);
let context = DASContext::new(
&peerdas_trusted_setup,
rust_eth_kzg::UsePrecomp::Yes {
width: rust_eth_kzg::constants::RECOMMENDED_PRECOMP_WIDTH,
},
);
Ok(Self {
trusted_setup: KzgSettings::load_trusted_setup(
&trusted_setup.g1_monomial(),
&trusted_setup.g1_lagrange(),
&trusted_setup.g2_monomial(),
NO_PRECOMPUTE,
)?,
context,
})
}
pub fn new_from_trusted_setup_das_enabled(trusted_setup: TrustedSetup) -> Result<Self, Error> {
// Initialize the trusted setup using default parameters
//
// Note: One can also use `from_json` to initialize it from the consensus-specs
// json string.
let peerdas_trusted_setup = PeerDASTrustedSetup::from(&trusted_setup);
pub fn new_from_trusted_setup(trusted_setup: &[u8]) -> Result<Self, Error> {
let (ckzg_trusted_setup, rkzg_trusted_setup) = load_trusted_setup(trusted_setup)?;
// It's not recommended to change the config parameter for precomputation as storage
// grows exponentially, but the speedup is exponential - after a while the speedup
// starts to become sublinear.
let context = DASContext::new(
&peerdas_trusted_setup,
&rkzg_trusted_setup,
rust_eth_kzg::UsePrecomp::Yes {
width: rust_eth_kzg::constants::RECOMMENDED_PRECOMP_WIDTH,
},
@@ -122,9 +99,9 @@ impl Kzg {
Ok(Self {
trusted_setup: KzgSettings::load_trusted_setup(
&trusted_setup.g1_monomial(),
&trusted_setup.g1_lagrange(),
&trusted_setup.g2_monomial(),
&ckzg_trusted_setup.g1_monomial(),
&ckzg_trusted_setup.g1_lagrange(),
&ckzg_trusted_setup.g2_monomial(),
NO_PRECOMPUTE,
)?,
context,
@@ -266,7 +243,7 @@ impl Kzg {
.collect();
let verification_result = self.context().verify_cell_kzg_proof_batch(
commitments.to_vec(),
columns,
&columns,
cells.to_vec(),
proofs.to_vec(),
);
@@ -274,7 +251,7 @@ impl Kzg {
// Modify the result so it matches roughly what the previous method was doing.
match verification_result {
Ok(_) => Ok(()),
Err(e) if e.invalid_proof() => Err(Error::KzgVerificationFailed),
Err(e) if e.is_proof_invalid() => Err(Error::KzgVerificationFailed),
Err(e) => Err(Error::PeerDASKZG(e)),
}
}

View File

@@ -1,4 +1,4 @@
use crate::PeerDASTrustedSetup;
use crate::{Error, PeerDASTrustedSetup};
use serde::{
de::{self, Deserializer, Visitor},
Deserialize, Serialize,
@@ -55,28 +55,6 @@ impl TrustedSetup {
}
}
impl From<&TrustedSetup> for PeerDASTrustedSetup {
fn from(trusted_setup: &TrustedSetup) -> Self {
Self {
g1_monomial: trusted_setup
.g1_monomial
.iter()
.map(|g1_point| format!("0x{}", hex::encode(g1_point.0)))
.collect::<Vec<_>>(),
g1_lagrange: trusted_setup
.g1_lagrange
.iter()
.map(|g1_point| format!("0x{}", hex::encode(g1_point.0)))
.collect::<Vec<_>>(),
g2_monomial: trusted_setup
.g2_monomial
.iter()
.map(|g2_point| format!("0x{}", hex::encode(g2_point.0)))
.collect::<Vec<_>>(),
}
}
}
impl Serialize for G1Point {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
@@ -176,3 +154,20 @@ fn strip_prefix(s: &str) -> &str {
s
}
}
/// Loads the trusted setup from JSON.
///
/// ## Note:
/// Currently we load both c-kzg and rust-eth-kzg trusted setup structs, because c-kzg is still being
/// used for 4844. Longer term we're planning to switch all KZG operations to the rust-eth-kzg
/// crate, and we'll be able to maintain a single trusted setup struct.
pub(crate) fn load_trusted_setup(
trusted_setup: &[u8],
) -> Result<(TrustedSetup, PeerDASTrustedSetup), Error> {
let ckzg_trusted_setup: TrustedSetup = serde_json::from_slice(trusted_setup)
.map_err(|e| Error::TrustedSetupError(format!("{e:?}")))?;
let trusted_setup_json = std::str::from_utf8(trusted_setup)
.map_err(|e| Error::TrustedSetupError(format!("{e:?}")))?;
let rkzg_trusted_setup = PeerDASTrustedSetup::from_json(trusted_setup_json);
Ok((ckzg_trusted_setup, rkzg_trusted_setup))
}