Skip to content

Commit

Permalink
chore: Lazy initialize the KZG struct when running tests (#6311)
Browse files Browse the repository at this point in the history
* use LazyLock when initializing KZG struct

* update get_kzg to return Arc<KZG> and not a Result<Arc<KZG>>

* Revert orthogonal changes to `kzg_verify_cell_kzg_proof_batch`

* add back map_err
  • Loading branch information
kevaundray authored Aug 28, 2024
1 parent 5e9cc60 commit 5a96687
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion testing/ef_tests/src/cases/kzg_blob_to_kzg_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<E: EthSpec> Case for KZGBlobToKZGCommitment<E> {
}

fn result(&self, _case_index: usize, _fork_name: ForkName) -> Result<(), Error> {
let kzg = get_kzg()?;
let kzg = get_kzg();
let commitment = parse_blob::<E>(&self.input.blob).and_then(|blob| {
blob_to_kzg_commitment::<E>(&kzg, &blob).map_err(|e| {
Error::InternalError(format!("Failed to compute kzg commitment: {:?}", e))
Expand Down
2 changes: 1 addition & 1 deletion testing/ef_tests/src/cases/kzg_compute_blob_kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<E: EthSpec> Case for KZGComputeBlobKZGProof<E> {
Ok((blob, commitment))
};

let kzg = get_kzg()?;
let kzg = get_kzg();
let proof = parse_input(&self.input).and_then(|(blob, commitment)| {
compute_blob_kzg_proof::<E>(&kzg, &blob, commitment)
.map_err(|e| Error::InternalError(format!("Failed to compute kzg proof: {:?}", e)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<E: EthSpec> Case for KZGComputeCellsAndKZGProofs<E> {
let blob = blob.as_ref().try_into().map_err(|e| {
Error::InternalError(format!("Failed to convert blob to kzg blob: {e:?}"))
})?;
let kzg = get_kzg()?;
let kzg = get_kzg();
kzg.compute_cells_and_proofs(blob).map_err(|e| {
Error::InternalError(format!("Failed to compute cells and kzg proofs: {e:?}"))
})
Expand Down
2 changes: 1 addition & 1 deletion testing/ef_tests/src/cases/kzg_compute_kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<E: EthSpec> Case for KZGComputeKZGProof<E> {
Ok((blob, z))
};

let kzg = get_kzg()?;
let kzg = get_kzg();
let proof = parse_input(&self.input).and_then(|(blob, z)| {
compute_kzg_proof::<E>(&kzg, &blob, z)
.map_err(|e| Error::InternalError(format!("Failed to compute kzg proof: {:?}", e)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<E: EthSpec> Case for KZGRecoverCellsAndKZGProofs<E> {
let result =
parse_input(&self.input).and_then(|(input_proofs, input_cells, cell_indices)| {
let input_cells_ref: Vec<_> = input_cells.iter().map(|cell| &**cell).collect();
let kzg = get_kzg()?;
let kzg = get_kzg();
let (cells, proofs) = kzg
.recover_cells_and_compute_kzg_proofs(
cell_indices.as_slice(),
Expand Down
17 changes: 13 additions & 4 deletions testing/ef_tests/src/cases/kzg_verify_blob_kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ use eth2_network_config::TRUSTED_SETUP_BYTES;
use kzg::{Cell, Error as KzgError, Kzg, KzgCommitment, KzgProof, TrustedSetup};
use serde::Deserialize;
use std::marker::PhantomData;
use std::sync::Arc;
use std::sync::LazyLock;
use types::Blob;

pub fn get_kzg() -> Result<Kzg, Error> {
static KZG: LazyLock<Arc<Kzg>> = LazyLock::new(|| {
let trusted_setup: TrustedSetup = serde_json::from_reader(TRUSTED_SETUP_BYTES)
.map_err(|e| Error::InternalError(format!("Failed to initialize kzg: {:?}", e)))?;
.map_err(|e| Error::InternalError(format!("Failed to initialize trusted setup: {:?}", e)))
.expect("failed to initialize trusted setup");
// TODO(das): need to enable these tests when rayon issues in rust_eth_kzg are fixed
Kzg::new_from_trusted_setup(trusted_setup)
let kzg = Kzg::new_from_trusted_setup(trusted_setup)
.map_err(|e| Error::InternalError(format!("Failed to initialize kzg: {:?}", e)))
.expect("failed to initialize kzg");
Arc::new(kzg)
});

pub fn get_kzg() -> Arc<Kzg> {
Arc::clone(&KZG)
}

pub fn parse_cells_and_proofs(
Expand Down Expand Up @@ -120,7 +129,7 @@ impl<E: EthSpec> Case for KZGVerifyBlobKZGProof<E> {
Ok((blob, commitment, proof))
};

let kzg = get_kzg()?;
let kzg = get_kzg();
let result = parse_input(&self.input).and_then(|(blob, commitment, proof)| {
match validate_blob::<E>(&kzg, &blob, commitment, proof) {
Ok(_) => Ok(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<E: EthSpec> Case for KZGVerifyBlobKZGProofBatch<E> {
Ok((commitments, blobs, proofs))
};

let kzg = get_kzg()?;
let kzg = get_kzg();
let result =
parse_input(&self.input).and_then(
|(commitments, blobs, proofs)| match validate_blobs::<E>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<E: EthSpec> Case for KZGVerifyCellKZGProofBatch<E> {
.into_iter()
.map(|(_row, col)| col)
.collect::<Vec<_>>();
let kzg = get_kzg()?;
let kzg = get_kzg();
match kzg.verify_cell_proof_batch(&cells, &proofs, column_indices, &commitments) {
Ok(_) => Ok(true),
Err(KzgError::KzgVerificationFailed) => Ok(false),
Expand Down
2 changes: 1 addition & 1 deletion testing/ef_tests/src/cases/kzg_verify_kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<E: EthSpec> Case for KZGVerifyKZGProof<E> {
Ok((commitment, z, y, proof))
};

let kzg = get_kzg()?;
let kzg = get_kzg();
let result = parse_input(&self.input).and_then(|(commitment, z, y, proof)| {
verify_kzg_proof::<E>(&kzg, commitment, proof, z, y)
.map_err(|e| Error::InternalError(format!("Failed to validate proof: {:?}", e)))
Expand Down

0 comments on commit 5a96687

Please sign in to comment.