Skip to content

Commit

Permalink
use digest function
Browse files Browse the repository at this point in the history
  • Loading branch information
PatStiles committed Aug 9, 2023
1 parent 107e10c commit d0de8c5
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions crates/primitives/src/eip4844.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
//! Helpers for working with EIP-4844 blob fee
use crate::{kzg::KzgCommitment, H256, constants::eip4844::{TARGET_DATA_GAS_PER_BLOCK, VERSIONED_HASH_VERSION_KZG}};

/// Calculates the versioned hash for a KzgCommitment
///
/// Specified in [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#header-extension)
pub fn kzg_to_versioned_hash(commitment: KzgCommitment) -> H256 {
use sha2::{Sha256, Digest};
let mut res = Sha256::digest(commitment.as_slice());
res[0] = VERSIONED_HASH_VERSION_KZG;
H256::from_slice(&res)
}

use crate::{H256, constants::eip4844::{TARGET_DATA_GAS_PER_BLOCK, VERSIONED_HASH_VERSION_KZG}};
use c_kzg::KzgCommitment;
/// Calculates the excess data gas for the next block, after applying the current set of blobs on
/// top of the excess data gas.
///
Expand All @@ -10,15 +19,3 @@ pub fn calculate_excess_blob_gas(parent_excess_blob_gas: u64, parent_blob_gas_us
let excess_blob_gas = parent_excess_blob_gas + parent_blob_gas_used;
excess_blob_gas.saturating_sub(TARGET_DATA_GAS_PER_BLOCK)
}

/// Calculates the versioned hash for a KzgCommitment
///
/// Specified in [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#header-extension)
pub fn kzg_to_versioned_hash(commitment: KzgCommitment) -> H256 {
use sha2::Digest;
let mut hasher = sha2::Sha256::new();
hasher.update(commitment.as_slice());
let res = &mut hasher.finalize()[..];
res[0] = VERSIONED_HASH_VERSION_KZG;
H256::from_slice(&res)
}

0 comments on commit d0de8c5

Please sign in to comment.