mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-08 17:26:04 +00:00
chore: Lazy initialize the KZG struct when running tests (#6311)
* 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
This commit is contained in:
@@ -36,7 +36,7 @@ impl<E: EthSpec> Case for KZGBlobToKZGCommitment<E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn result(&self, _case_index: usize, _fork_name: ForkName) -> Result<(), Error> {
|
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| {
|
let commitment = parse_blob::<E>(&self.input.blob).and_then(|blob| {
|
||||||
blob_to_kzg_commitment::<E>(&kzg, &blob).map_err(|e| {
|
blob_to_kzg_commitment::<E>(&kzg, &blob).map_err(|e| {
|
||||||
Error::InternalError(format!("Failed to compute kzg commitment: {:?}", e))
|
Error::InternalError(format!("Failed to compute kzg commitment: {:?}", e))
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ impl<E: EthSpec> Case for KZGComputeBlobKZGProof<E> {
|
|||||||
Ok((blob, commitment))
|
Ok((blob, commitment))
|
||||||
};
|
};
|
||||||
|
|
||||||
let kzg = get_kzg()?;
|
let kzg = get_kzg();
|
||||||
let proof = parse_input(&self.input).and_then(|(blob, commitment)| {
|
let proof = parse_input(&self.input).and_then(|(blob, commitment)| {
|
||||||
compute_blob_kzg_proof::<E>(&kzg, &blob, commitment)
|
compute_blob_kzg_proof::<E>(&kzg, &blob, commitment)
|
||||||
.map_err(|e| Error::InternalError(format!("Failed to compute kzg proof: {:?}", e)))
|
.map_err(|e| Error::InternalError(format!("Failed to compute kzg proof: {:?}", e)))
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ impl<E: EthSpec> Case for KZGComputeCellsAndKZGProofs<E> {
|
|||||||
let blob = blob.as_ref().try_into().map_err(|e| {
|
let blob = blob.as_ref().try_into().map_err(|e| {
|
||||||
Error::InternalError(format!("Failed to convert blob to kzg blob: {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| {
|
kzg.compute_cells_and_proofs(blob).map_err(|e| {
|
||||||
Error::InternalError(format!("Failed to compute cells and kzg proofs: {e:?}"))
|
Error::InternalError(format!("Failed to compute cells and kzg proofs: {e:?}"))
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ impl<E: EthSpec> Case for KZGComputeKZGProof<E> {
|
|||||||
Ok((blob, z))
|
Ok((blob, z))
|
||||||
};
|
};
|
||||||
|
|
||||||
let kzg = get_kzg()?;
|
let kzg = get_kzg();
|
||||||
let proof = parse_input(&self.input).and_then(|(blob, z)| {
|
let proof = parse_input(&self.input).and_then(|(blob, z)| {
|
||||||
compute_kzg_proof::<E>(&kzg, &blob, z)
|
compute_kzg_proof::<E>(&kzg, &blob, z)
|
||||||
.map_err(|e| Error::InternalError(format!("Failed to compute kzg proof: {:?}", e)))
|
.map_err(|e| Error::InternalError(format!("Failed to compute kzg proof: {:?}", e)))
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ impl<E: EthSpec> Case for KZGRecoverCellsAndKZGProofs<E> {
|
|||||||
let result =
|
let result =
|
||||||
parse_input(&self.input).and_then(|(input_proofs, input_cells, cell_indices)| {
|
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 input_cells_ref: Vec<_> = input_cells.iter().map(|cell| &**cell).collect();
|
||||||
let kzg = get_kzg()?;
|
let kzg = get_kzg();
|
||||||
let (cells, proofs) = kzg
|
let (cells, proofs) = kzg
|
||||||
.recover_cells_and_compute_kzg_proofs(
|
.recover_cells_and_compute_kzg_proofs(
|
||||||
cell_indices.as_slice(),
|
cell_indices.as_slice(),
|
||||||
|
|||||||
@@ -5,14 +5,23 @@ use eth2_network_config::TRUSTED_SETUP_BYTES;
|
|||||||
use kzg::{Cell, Error as KzgError, Kzg, KzgCommitment, KzgProof, TrustedSetup};
|
use kzg::{Cell, Error as KzgError, Kzg, KzgCommitment, KzgProof, TrustedSetup};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::LazyLock;
|
||||||
use types::Blob;
|
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)
|
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
|
// 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)))
|
.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(
|
pub fn parse_cells_and_proofs(
|
||||||
@@ -120,7 +129,7 @@ impl<E: EthSpec> Case for KZGVerifyBlobKZGProof<E> {
|
|||||||
Ok((blob, commitment, proof))
|
Ok((blob, commitment, proof))
|
||||||
};
|
};
|
||||||
|
|
||||||
let kzg = get_kzg()?;
|
let kzg = get_kzg();
|
||||||
let result = parse_input(&self.input).and_then(|(blob, commitment, proof)| {
|
let result = parse_input(&self.input).and_then(|(blob, commitment, proof)| {
|
||||||
match validate_blob::<E>(&kzg, &blob, commitment, proof) {
|
match validate_blob::<E>(&kzg, &blob, commitment, proof) {
|
||||||
Ok(_) => Ok(true),
|
Ok(_) => Ok(true),
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ impl<E: EthSpec> Case for KZGVerifyBlobKZGProofBatch<E> {
|
|||||||
Ok((commitments, blobs, proofs))
|
Ok((commitments, blobs, proofs))
|
||||||
};
|
};
|
||||||
|
|
||||||
let kzg = get_kzg()?;
|
let kzg = get_kzg();
|
||||||
let result =
|
let result =
|
||||||
parse_input(&self.input).and_then(
|
parse_input(&self.input).and_then(
|
||||||
|(commitments, blobs, proofs)| match validate_blobs::<E>(
|
|(commitments, blobs, proofs)| match validate_blobs::<E>(
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ impl<E: EthSpec> Case for KZGVerifyCellKZGProofBatch<E> {
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(_row, col)| col)
|
.map(|(_row, col)| col)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
let kzg = get_kzg()?;
|
let kzg = get_kzg();
|
||||||
match kzg.verify_cell_proof_batch(&cells, &proofs, column_indices, &commitments) {
|
match kzg.verify_cell_proof_batch(&cells, &proofs, column_indices, &commitments) {
|
||||||
Ok(_) => Ok(true),
|
Ok(_) => Ok(true),
|
||||||
Err(KzgError::KzgVerificationFailed) => Ok(false),
|
Err(KzgError::KzgVerificationFailed) => Ok(false),
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ impl<E: EthSpec> Case for KZGVerifyKZGProof<E> {
|
|||||||
Ok((commitment, z, y, proof))
|
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)| {
|
let result = parse_input(&self.input).and_then(|(commitment, z, y, proof)| {
|
||||||
verify_kzg_proof::<E>(&kzg, commitment, proof, z, y)
|
verify_kzg_proof::<E>(&kzg, commitment, proof, z, y)
|
||||||
.map_err(|e| Error::InternalError(format!("Failed to validate proof: {:?}", e)))
|
.map_err(|e| Error::InternalError(format!("Failed to validate proof: {:?}", e)))
|
||||||
|
|||||||
Reference in New Issue
Block a user