Skip to content

Commit

Permalink
refactor kzg logic to fork-independent module
Browse files Browse the repository at this point in the history
  • Loading branch information
ralexstokes committed May 7, 2024
1 parent 51fae41 commit 420e7b7
Show file tree
Hide file tree
Showing 22 changed files with 63 additions and 60 deletions.
2 changes: 1 addition & 1 deletion beacon-api-client/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
3 changes: 2 additions & 1 deletion ethereum-consensus/src/bin/ec/blobs/bundler.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ethereum-consensus/src/bin/ec/blobs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<BYTES_PER_COMMITMENT>;
pub type KzgProof = ByteVector<BYTES_PER_PROOF>;
Expand Down Expand Up @@ -60,8 +57,8 @@ pub struct ProofAndEvaluation {
pub evaluation: FieldElement,
}

pub fn blob_to_kzg_commitment<const BYTES_PER_BLOB: usize>(
blob: &Blob<BYTES_PER_BLOB>,
pub fn blob_to_kzg_commitment<Blob: AsRef<[u8]>>(
blob: Blob,
kzg_settings: &KzgSettings,
) -> Result<KzgCommitment, Error> {
let blob = c_kzg::Blob::from_bytes(blob.as_ref())?;
Expand All @@ -71,8 +68,8 @@ pub fn blob_to_kzg_commitment<const BYTES_PER_BLOB: usize>(
Ok(inner)
}

pub fn compute_kzg_proof<const BYTES_PER_BLOB: usize>(
blob: &Blob<BYTES_PER_BLOB>,
pub fn compute_kzg_proof<Blob: AsRef<[u8]>>(
blob: Blob,
evaluation_point: &FieldElement,
kzg_settings: &KzgSettings,
) -> Result<ProofAndEvaluation, Error> {
Expand All @@ -88,8 +85,8 @@ pub fn compute_kzg_proof<const BYTES_PER_BLOB: usize>(
Ok(result)
}

pub fn compute_blob_kzg_proof<const BYTES_PER_BLOB: usize>(
blob: &Blob<BYTES_PER_BLOB>,
pub fn compute_blob_kzg_proof<Blob: AsRef<[u8]>>(
blob: Blob,
commitment: &KzgCommitment,
kzg_settings: &KzgSettings,
) -> Result<KzgProof, Error> {
Expand Down Expand Up @@ -124,8 +121,8 @@ pub fn verify_kzg_proof(
res.then_some(()).ok_or(Error::InvalidProof)
}

pub fn verify_blob_kzg_proof<const BYTES_PER_BLOB: usize>(
blob: &Blob<BYTES_PER_BLOB>,
pub fn verify_blob_kzg_proof<Blob: AsRef<[u8]>>(
blob: Blob,
commitment: &KzgCommitment,
proof: &KzgProof,
kzg_settings: &KzgSettings,
Expand All @@ -139,8 +136,8 @@ pub fn verify_blob_kzg_proof<const BYTES_PER_BLOB: usize>(
res.then_some(()).ok_or(Error::InvalidProof)
}

pub fn verify_blob_kzg_proof_batch<const BYTES_PER_BLOB: usize>(
blobs: &[Blob<BYTES_PER_BLOB>],
pub fn verify_blob_kzg_proof_batch<Blob: AsRef<[u8]>>(
blobs: &[Blob],
commitments: &[KzgCommitment],
proofs: &[KzgProof],
kzg_settings: &KzgSettings,
Expand Down
9 changes: 9 additions & 0 deletions ethereum-consensus/src/crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
3 changes: 2 additions & 1 deletion ethereum-consensus/src/deneb/beacon_block.rs
Original file line number Diff line number Diff line change
@@ -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,
},
Expand Down
3 changes: 2 additions & 1 deletion ethereum-consensus/src/deneb/blinded_beacon_block.rs
Original file line number Diff line number Diff line change
@@ -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,
},
Expand Down
14 changes: 5 additions & 9 deletions ethereum-consensus/src/deneb/blob_sidecar.rs
Original file line number Diff line number Diff line change
@@ -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<const BYTES_PER_BLOB: usize> = ByteVector<BYTES_PER_BLOB>;

Expand Down Expand Up @@ -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#"
Expand Down
2 changes: 1 addition & 1 deletion ethereum-consensus/src/deneb/execution_engine.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down
9 changes: 4 additions & 5 deletions ethereum-consensus/src/deneb/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
1 change: 0 additions & 1 deletion ethereum-consensus/src/deneb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion ethereum-consensus/src/deneb/presets/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion ethereum-consensus/src/deneb/presets/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion ethereum-consensus/src/electra/beacon_block.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
altair::SyncAggregate,
capella::SignedBlsToExecutionChange,
deneb::polynomial_commitments::KzgCommitment,
crypto::KzgCommitment,
electra::{
execution_payload::ExecutionPayload,
operations::{Attestation, AttesterSlashing, SignedConsolidation},
Expand Down
2 changes: 1 addition & 1 deletion ethereum-consensus/src/electra/execution_engine.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
deneb::polynomial_commitments::VersionedHash, electra::ExecutionPayload,
deneb::blob_sidecar::VersionedHash, electra::ExecutionPayload,
execution_engine::PayloadRequest, primitives::Root,
};

Expand Down
9 changes: 4 additions & 5 deletions ethereum-consensus/src/error.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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),
Expand Down Expand Up @@ -62,8 +63,6 @@ pub enum Error {
UnknownPreset(String),
#[error(transparent)]
ExecutionEngine(#[from] ExecutionEngineError),
#[error(transparent)]
PolynomialCommitment(#[from] PolynomialCommitmentError),
}

#[derive(Debug, Error)]
Expand Down
7 changes: 2 additions & 5 deletions ethereum-consensus/src/state_transition/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion ethereum-consensus/src/types/beacon_block_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion ethereum-consensus/src/types/blinded_beacon_block_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
6 changes: 4 additions & 2 deletions spec-gen/src/type_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions spec-tests/runners/kzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 420e7b7

Please sign in to comment.