diff --git a/beacon-api-client/src/types.rs b/beacon-api-client/src/types.rs index c3e89448e..ee5912008 100644 --- a/beacon-api-client/src/types.rs +++ b/beacon-api-client/src/types.rs @@ -2,7 +2,7 @@ use crate::ApiError; use ethereum_consensus::{ altair::networking::MetaData, capella::Withdrawal, - deneb::polynomial_commitments::KzgProof, + crypto::KzgProof, networking::{Enr, Multiaddr, PeerId}, phase0::{Checkpoint, SignedBeaconBlockHeader, Validator}, primitives::{ diff --git a/ethereum-consensus/src/bin/ec/blobs/bundler.rs b/ethereum-consensus/src/bin/ec/blobs/bundler.rs index 7c2ba86d0..b82403065 100644 --- a/ethereum-consensus/src/bin/ec/blobs/bundler.rs +++ b/ethereum-consensus/src/bin/ec/blobs/bundler.rs @@ -1,6 +1,7 @@ use crate::blobs::{Blob, Error}; use ethereum_consensus::{ - deneb::{self, polynomial_commitments as spec, presets::TRUSTED_SETUP_JSON}, + crypto::kzg as spec, + deneb::{self, presets::TRUSTED_SETUP_JSON}, Error as ConsensusError, }; use std::io::Read; diff --git a/ethereum-consensus/src/bin/ec/blobs/mod.rs b/ethereum-consensus/src/bin/ec/blobs/mod.rs index 115b49667..4854ed123 100644 --- a/ethereum-consensus/src/bin/ec/blobs/mod.rs +++ b/ethereum-consensus/src/bin/ec/blobs/mod.rs @@ -10,7 +10,7 @@ use thiserror::Error; pub(crate) const BYTES_PER_BLOB: usize = spec::mainnet::BYTES_PER_BLOB; pub(crate) const BYTES_PER_FIELD_ELEMENT: usize = - spec::polynomial_commitments::BYTES_PER_FIELD_ELEMENT; + ethereum_consensus::crypto::kzg::BYTES_PER_FIELD_ELEMENT; // Number of bits in a valid field element. pub(crate) const BITS_PER_FIELD_ELEMENT: usize = 254; diff --git a/ethereum-consensus/src/crypto.rs b/ethereum-consensus/src/crypto/bls.rs similarity index 100% rename from ethereum-consensus/src/crypto.rs rename to ethereum-consensus/src/crypto/bls.rs diff --git a/ethereum-consensus/src/deneb/polynomial_commitments.rs b/ethereum-consensus/src/crypto/kzg.rs similarity index 89% rename from ethereum-consensus/src/deneb/polynomial_commitments.rs rename to ethereum-consensus/src/crypto/kzg.rs index 0f2db82f9..e7605cbee 100644 --- a/ethereum-consensus/src/deneb/polynomial_commitments.rs +++ b/ethereum-consensus/src/crypto/kzg.rs @@ -1,6 +1,4 @@ -use crate::{ - deneb::blob_sidecar::Blob, primitives::Bytes32, ssz::prelude::*, Error as ConsensusError, -}; +use crate::{primitives::Bytes32, ssz::prelude::*, Error as ConsensusError}; pub use c_kzg::KzgSettings; use thiserror::Error; @@ -10,7 +8,6 @@ pub const BYTES_PER_PROOF: usize = 48; pub const BYTES_PER_G1_POINT: usize = 48; pub const BYTES_PER_G2_POINT: usize = 96; -pub type VersionedHash = Bytes32; pub type FieldElement = Bytes32; pub type KzgCommitment = ByteVector; pub type KzgProof = ByteVector; @@ -60,8 +57,8 @@ pub struct ProofAndEvaluation { pub evaluation: FieldElement, } -pub fn blob_to_kzg_commitment( - blob: &Blob, +pub fn blob_to_kzg_commitment>( + blob: Blob, kzg_settings: &KzgSettings, ) -> Result { let blob = c_kzg::Blob::from_bytes(blob.as_ref())?; @@ -71,8 +68,8 @@ pub fn blob_to_kzg_commitment( Ok(inner) } -pub fn compute_kzg_proof( - blob: &Blob, +pub fn compute_kzg_proof>( + blob: Blob, evaluation_point: &FieldElement, kzg_settings: &KzgSettings, ) -> Result { @@ -88,8 +85,8 @@ pub fn compute_kzg_proof( Ok(result) } -pub fn compute_blob_kzg_proof( - blob: &Blob, +pub fn compute_blob_kzg_proof>( + blob: Blob, commitment: &KzgCommitment, kzg_settings: &KzgSettings, ) -> Result { @@ -124,8 +121,8 @@ pub fn verify_kzg_proof( res.then_some(()).ok_or(Error::InvalidProof) } -pub fn verify_blob_kzg_proof( - blob: &Blob, +pub fn verify_blob_kzg_proof>( + blob: Blob, commitment: &KzgCommitment, proof: &KzgProof, kzg_settings: &KzgSettings, @@ -139,8 +136,8 @@ pub fn verify_blob_kzg_proof( res.then_some(()).ok_or(Error::InvalidProof) } -pub fn verify_blob_kzg_proof_batch( - blobs: &[Blob], +pub fn verify_blob_kzg_proof_batch>( + blobs: &[Blob], commitments: &[KzgCommitment], proofs: &[KzgProof], kzg_settings: &KzgSettings, diff --git a/ethereum-consensus/src/crypto/mod.rs b/ethereum-consensus/src/crypto/mod.rs new file mode 100644 index 000000000..4f2a2c010 --- /dev/null +++ b/ethereum-consensus/src/crypto/mod.rs @@ -0,0 +1,9 @@ +pub mod bls; +pub mod kzg; + +pub use bls::{ + aggregate, aggregate_verify, eth_aggregate_public_keys, eth_fast_aggregate_verify, + fast_aggregate_verify, hash, verify_signature, Error as BlsError, PublicKey, SecretKey, + Signature, +}; +pub use kzg::{kzg_settings_from_json, Error as KzgError, KzgCommitment, KzgProof, KzgSettings}; diff --git a/ethereum-consensus/src/deneb/beacon_block.rs b/ethereum-consensus/src/deneb/beacon_block.rs index b2cbbf366..7eddc3bba 100644 --- a/ethereum-consensus/src/deneb/beacon_block.rs +++ b/ethereum-consensus/src/deneb/beacon_block.rs @@ -1,7 +1,8 @@ use crate::{ altair::SyncAggregate, capella::SignedBlsToExecutionChange, - deneb::{polynomial_commitments::KzgCommitment, ExecutionPayload}, + crypto::KzgCommitment, + deneb::ExecutionPayload, phase0::{ Attestation, AttesterSlashing, Deposit, Eth1Data, ProposerSlashing, SignedVoluntaryExit, }, diff --git a/ethereum-consensus/src/deneb/blinded_beacon_block.rs b/ethereum-consensus/src/deneb/blinded_beacon_block.rs index 400da66f2..6e22d5b0c 100644 --- a/ethereum-consensus/src/deneb/blinded_beacon_block.rs +++ b/ethereum-consensus/src/deneb/blinded_beacon_block.rs @@ -1,7 +1,8 @@ use crate::{ altair::SyncAggregate, capella::SignedBlsToExecutionChange, - deneb::{polynomial_commitments::KzgCommitment, ExecutionPayloadHeader}, + crypto::KzgCommitment, + deneb::ExecutionPayloadHeader, phase0::{ Attestation, AttesterSlashing, Deposit, Eth1Data, ProposerSlashing, SignedVoluntaryExit, }, diff --git a/ethereum-consensus/src/deneb/blob_sidecar.rs b/ethereum-consensus/src/deneb/blob_sidecar.rs index c845e4ca4..3b06618c2 100644 --- a/ethereum-consensus/src/deneb/blob_sidecar.rs +++ b/ethereum-consensus/src/deneb/blob_sidecar.rs @@ -1,15 +1,14 @@ use crate::{ - deneb::{ - polynomial_commitments::{KzgCommitment, KzgProof}, - SignedBeaconBlockHeader, - }, - primitives::{BlobIndex, Root}, + crypto::{KzgCommitment, KzgProof}, + deneb::SignedBeaconBlockHeader, + primitives::{BlobIndex, Bytes32, Root}, ssz::prelude::*, Error, }; use ssz_rs::proofs::get_subtree_index; pub const VERSIONED_HASH_VERSION_KZG: u8 = 1; +pub type VersionedHash = Bytes32; pub type Blob = ByteVector; @@ -66,10 +65,7 @@ pub fn verify_blob_sidecar_inclusion_proof< #[cfg(test)] mod tests { - use crate::{ - deneb::{mainnet as spec, polynomial_commitments as crypto}, - state_transition::Context, - }; + use crate::{crypto::kzg as crypto, deneb::mainnet as spec, state_transition::Context}; // blob side car from `sepolia` testnet const BLOB_SIDECAR_JSON: &str = r#" diff --git a/ethereum-consensus/src/deneb/execution_engine.rs b/ethereum-consensus/src/deneb/execution_engine.rs index fc6367b71..1b0522e5a 100644 --- a/ethereum-consensus/src/deneb/execution_engine.rs +++ b/ethereum-consensus/src/deneb/execution_engine.rs @@ -1,5 +1,5 @@ use crate::{ - deneb::{execution_payload::ExecutionPayload, polynomial_commitments::VersionedHash}, + deneb::{blob_sidecar::VersionedHash, execution_payload::ExecutionPayload}, execution_engine::PayloadRequest, primitives::Root, }; diff --git a/ethereum-consensus/src/deneb/helpers.rs b/ethereum-consensus/src/deneb/helpers.rs index bae519675..c4b37cf7d 100644 --- a/ethereum-consensus/src/deneb/helpers.rs +++ b/ethereum-consensus/src/deneb/helpers.rs @@ -2,12 +2,11 @@ use crate::{ altair::constants::{ TIMELY_HEAD_FLAG_INDEX, TIMELY_SOURCE_FLAG_INDEX, TIMELY_TARGET_FLAG_INDEX, }, - crypto::hash, + crypto::{hash, KzgCommitment}, deneb::{ - beacon_state::BeaconState, - get_block_root, get_block_root_at_slot, get_current_epoch, get_validator_churn_limit, - polynomial_commitments::{KzgCommitment, VersionedHash}, - AttestationData, VERSIONED_HASH_VERSION_KZG, + beacon_state::BeaconState, blob_sidecar::VersionedHash, get_block_root, + get_block_root_at_slot, get_current_epoch, get_validator_churn_limit, AttestationData, + VERSIONED_HASH_VERSION_KZG, }, error::{invalid_operation_error, InvalidAttestation, InvalidOperation}, state_transition::{Context, Result}, diff --git a/ethereum-consensus/src/deneb/mod.rs b/ethereum-consensus/src/deneb/mod.rs index e2f1d9462..3e5ef4b0c 100644 --- a/ethereum-consensus/src/deneb/mod.rs +++ b/ethereum-consensus/src/deneb/mod.rs @@ -14,7 +14,6 @@ pub mod genesis; pub mod helpers; pub mod light_client; pub mod networking; -pub mod polynomial_commitments; pub mod presets; pub mod spec; diff --git a/ethereum-consensus/src/deneb/presets/mainnet.rs b/ethereum-consensus/src/deneb/presets/mainnet.rs index cefe52981..d2d9dd61e 100644 --- a/ethereum-consensus/src/deneb/presets/mainnet.rs +++ b/ethereum-consensus/src/deneb/presets/mainnet.rs @@ -22,7 +22,7 @@ pub const MAX_BLOBS_PER_BLOCK: usize = 6; pub const KZG_COMMITMENT_INCLUSION_PROOF_DEPTH: usize = 17; pub const BYTES_PER_BLOB: usize = - crate::deneb::polynomial_commitments::BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_BLOB; + crate::crypto::kzg::BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_BLOB; pub const MAX_REQUEST_BLOB_SIDECARS: usize = MAX_REQUEST_BLOCKS_DENEB * MAX_BLOBS_PER_BLOCK; diff --git a/ethereum-consensus/src/deneb/presets/minimal.rs b/ethereum-consensus/src/deneb/presets/minimal.rs index 6b8525051..afa275f72 100644 --- a/ethereum-consensus/src/deneb/presets/minimal.rs +++ b/ethereum-consensus/src/deneb/presets/minimal.rs @@ -22,7 +22,7 @@ pub const MAX_BLOBS_PER_BLOCK: usize = 6; pub const KZG_COMMITMENT_INCLUSION_PROOF_DEPTH: usize = 9; pub const BYTES_PER_BLOB: usize = - crate::deneb::polynomial_commitments::BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_BLOB; + crate::crypto::kzg::BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_BLOB; pub const MAX_REQUEST_BLOB_SIDECARS: usize = MAX_REQUEST_BLOCKS_DENEB * MAX_BLOBS_PER_BLOCK; diff --git a/ethereum-consensus/src/electra/beacon_block.rs b/ethereum-consensus/src/electra/beacon_block.rs index 3d9f692a7..eb861c91d 100644 --- a/ethereum-consensus/src/electra/beacon_block.rs +++ b/ethereum-consensus/src/electra/beacon_block.rs @@ -1,7 +1,7 @@ use crate::{ altair::SyncAggregate, capella::SignedBlsToExecutionChange, - deneb::polynomial_commitments::KzgCommitment, + crypto::KzgCommitment, electra::{ execution_payload::ExecutionPayload, operations::{Attestation, AttesterSlashing, SignedConsolidation}, diff --git a/ethereum-consensus/src/electra/execution_engine.rs b/ethereum-consensus/src/electra/execution_engine.rs index 7e111c581..f772fa68e 100644 --- a/ethereum-consensus/src/electra/execution_engine.rs +++ b/ethereum-consensus/src/electra/execution_engine.rs @@ -1,5 +1,5 @@ use crate::{ - deneb::polynomial_commitments::VersionedHash, electra::ExecutionPayload, + deneb::blob_sidecar::VersionedHash, electra::ExecutionPayload, execution_engine::PayloadRequest, primitives::Root, }; diff --git a/ethereum-consensus/src/error.rs b/ethereum-consensus/src/error.rs index 1bc1cb431..df6543e5c 100644 --- a/ethereum-consensus/src/error.rs +++ b/ethereum-consensus/src/error.rs @@ -1,7 +1,6 @@ use crate::{ capella::Withdrawal, - crypto::Error as CryptoError, - deneb::polynomial_commitments::Error as PolynomialCommitmentError, + crypto::{BlsError, KzgError}, phase0::{AttestationData, BeaconBlockHeader, Checkpoint}, primitives::{ BlsPublicKey, BlsSignature, Bytes32, Epoch, ExecutionAddress, Hash32, Root, Slot, @@ -19,7 +18,9 @@ pub enum Error { #[error("{0}")] SimpleSerialize(#[from] SimpleSerializeError), #[error("{0}")] - Crypto(#[from] CryptoError), + Bls(#[from] BlsError), + #[error("{0}")] + Kzg(#[from] KzgError), #[cfg(feature = "serde")] #[error("{0}")] Io(#[from] std::io::Error), @@ -62,8 +63,6 @@ pub enum Error { UnknownPreset(String), #[error(transparent)] ExecutionEngine(#[from] ExecutionEngineError), - #[error(transparent)] - PolynomialCommitment(#[from] PolynomialCommitmentError), } #[derive(Debug, Error)] diff --git a/ethereum-consensus/src/state_transition/context.rs b/ethereum-consensus/src/state_transition/context.rs index a9561f0e5..6fb910ae2 100644 --- a/ethereum-consensus/src/state_transition/context.rs +++ b/ethereum-consensus/src/state_transition/context.rs @@ -2,11 +2,8 @@ use crate::{ altair, bellatrix, capella, clock::{self, Clock, SystemTimeProvider}, configs::{self, Config}, - deneb::{ - self, - polynomial_commitments::{kzg_settings_from_json, KzgSettings}, - presets::TRUSTED_SETUP_JSON, - }, + crypto::{kzg_settings_from_json, KzgSettings}, + deneb::{self, presets::TRUSTED_SETUP_JSON}, electra, execution_engine::ExecutionEngine, networks::Network, diff --git a/ethereum-consensus/src/types/beacon_block_body.rs b/ethereum-consensus/src/types/beacon_block_body.rs index 67aadf5ea..876faaa67 100644 --- a/ethereum-consensus/src/types/beacon_block_body.rs +++ b/ethereum-consensus/src/types/beacon_block_body.rs @@ -3,7 +3,8 @@ use crate::{ altair::{beacon_block as altair, SyncAggregate}, bellatrix::beacon_block as bellatrix, capella::{beacon_block as capella, SignedBlsToExecutionChange}, - deneb::{beacon_block as deneb, polynomial_commitments::KzgCommitment}, + crypto::KzgCommitment, + deneb::beacon_block as deneb, phase0::{ beacon_block as phase0, Attestation, AttesterSlashing, Deposit, Eth1Data, ProposerSlashing, SignedVoluntaryExit, diff --git a/ethereum-consensus/src/types/blinded_beacon_block_body.rs b/ethereum-consensus/src/types/blinded_beacon_block_body.rs index c161ebb8f..262c4fe29 100644 --- a/ethereum-consensus/src/types/blinded_beacon_block_body.rs +++ b/ethereum-consensus/src/types/blinded_beacon_block_body.rs @@ -3,7 +3,8 @@ use crate::{ altair::SyncAggregate, bellatrix::blinded_beacon_block as bellatrix, capella::{blinded_beacon_block as capella, SignedBlsToExecutionChange}, - deneb::{blinded_beacon_block as deneb, polynomial_commitments::KzgCommitment}, + crypto::KzgCommitment, + deneb::blinded_beacon_block as deneb, phase0::{ Attestation, AttesterSlashing, Deposit, Eth1Data, ProposerSlashing, SignedVoluntaryExit, }, diff --git a/spec-gen/src/type_generator.rs b/spec-gen/src/type_generator.rs index 44980edd6..ef1d41dd8 100644 --- a/spec-gen/src/type_generator.rs +++ b/spec-gen/src/type_generator.rs @@ -123,7 +123,8 @@ impl Type { altair::beacon_block as altair, bellatrix::beacon_block as bellatrix, capella::beacon_block as capella, - deneb::{beacon_block as deneb, polynomial_commitments::KzgCommitment}, + crypto::KzgCommitment, + deneb::{beacon_block as deneb}, phase0::{ beacon_block as phase0, Attestation, AttesterSlashing, Deposit, Eth1Data, ProposerSlashing, SignedVoluntaryExit, @@ -140,7 +141,8 @@ impl Type { use crate::{ bellatrix::blinded_beacon_block as bellatrix, capella::blinded_beacon_block as capella, - deneb::{blinded_beacon_block as deneb, polynomial_commitments::KzgCommitment}, + crypto::KzgCommitment, + deneb::{blinded_beacon_block as deneb}, phase0::{ Attestation, AttesterSlashing, Deposit, Eth1Data, ProposerSlashing, SignedVoluntaryExit, diff --git a/spec-tests/runners/kzg.rs b/spec-tests/runners/kzg.rs index 67836f393..1bcedb29d 100644 --- a/spec-tests/runners/kzg.rs +++ b/spec-tests/runners/kzg.rs @@ -2,13 +2,13 @@ use crate::{ test_case::TestCase, test_utils::{load_yaml, Error}, }; -use ethereum_consensus::deneb::{ - mainnet::Blob, - polynomial_commitments::{ +use ethereum_consensus::{ + crypto::kzg::{ blob_to_kzg_commitment, compute_blob_kzg_proof, compute_kzg_proof, verify_blob_kzg_proof, verify_blob_kzg_proof_batch, verify_kzg_proof, FieldElement, KzgCommitment, KzgProof, KzgSettings, ProofAndEvaluation, }, + deneb::mainnet::Blob, }; pub fn dispatch(test: &TestCase) -> Result<(), Error> { @@ -47,7 +47,7 @@ fn run_blob_to_kzg_commitment_test( } }; - let result = blob_to_kzg_commitment(&blob, kzg_settings); + let result = blob_to_kzg_commitment(blob, kzg_settings); if let Some(expected_commitment) = output { // some `output` was present, use inner value to determine if the spec code should succeed // or fail @@ -88,7 +88,7 @@ fn run_compute_kzg_proof_test(test: &TestCase, kzg_settings: &KzgSettings) -> Re } }; - let result = compute_kzg_proof(&blob, &z, kzg_settings); + let result = compute_kzg_proof(blob, &z, kzg_settings); if let Some(outputs) = output { let proof_and_evaluation = result.unwrap(); let expected_proof_and_evaluation = @@ -189,7 +189,7 @@ fn run_compute_blob_kzg_proof_test( } }; - let result = compute_blob_kzg_proof(&blob, &commitment, kzg_settings); + let result = compute_blob_kzg_proof(blob, &commitment, kzg_settings); if let Some(expected_proof) = output { let proof = result.unwrap(); assert_eq!(proof, expected_proof); @@ -238,7 +238,7 @@ fn run_verify_blob_kzg_proof_test( } }; - let result = verify_blob_kzg_proof(&blob, &commitment, &proof, kzg_settings); + let result = verify_blob_kzg_proof(blob, &commitment, &proof, kzg_settings); if let Some(expected_validity) = output { if expected_validity { assert!(result.is_ok());