diff --git a/pysetup/spec_builders/eip8321.py b/pysetup/spec_builders/eip8321.py index 20612326108..626ac73a9a4 100644 --- a/pysetup/spec_builders/eip8321.py +++ b/pysetup/spec_builders/eip8321.py @@ -9,7 +9,7 @@ class EIP8321SpecBuilder(BaseSpecBuilder): @classmethod def imports(cls, preset_name: str): return f""" -from eth_consensus_specs.utils.hash_function import blake3 +from blake3 import blake3 as blake3_hash from eth_consensus_specs.heze import {preset_name} as heze """ diff --git a/pysetup/spec_builders/phase0.py b/pysetup/spec_builders/phase0.py index 04c67fed9e9..cc9aa5cc3fe 100644 --- a/pysetup/spec_builders/phase0.py +++ b/pysetup/spec_builders/phase0.py @@ -25,6 +25,7 @@ def imports(cls, preset_name: str) -> str: dataclass, field, ) +from hashlib import sha256 as sha256_hash from typing import ( Any, Callable, Dict, DefaultDict, Set, Sequence, Tuple, Optional, TypeAlias, TypeVar, NamedTuple, Final ) @@ -35,7 +36,6 @@ def imports(cls, preset_name: str) -> str: Bytes1, Bytes4, Bytes20, Bytes32, Bytes48, Bytes96, BitList) from eth_consensus_specs.utils.ssz.ssz_typing import BitVector # noqa: F401 from eth_consensus_specs.utils import bls -from eth_consensus_specs.utils.hash_function import hash """ @classmethod diff --git a/specs/_features/eip8321/beacon-chain.md b/specs/_features/eip8321/beacon-chain.md index de3f6607f93..fe383d84fcd 100644 --- a/specs/_features/eip8321/beacon-chain.md +++ b/specs/_features/eip8321/beacon-chain.md @@ -261,12 +261,13 @@ class BeaconState(ProgressiveContainer(active_fields=[1] * 48)): #### New `blake3` -`def blake3(data: bytes) -> Bytes32` is the BLAKE3 hash function in its default -unkeyed hash mode, with no derive-key context, restricted to its default 32-byte -output. - -All hashing introduced by this upgrade uses `blake3`; the `hash` helper -continues to serve the legacy reveal path. +```python +def blake3(data: bytes) -> Bytes32: + """ + Return the BLAKE3 hash of ``data``. + """ + return Bytes32(blake3_hash(data).digest()) +``` ### Validator registry @@ -409,7 +410,7 @@ def process_randao(state: BeaconState, body: BeaconBlockBody) -> None: state.randao_commitments[proposer_index] = body.hash_chain_reveal else: verify_bls_randao_reveal(state, body, proposer_index) - mix = xor(get_randao_mix(state, epoch), hash(body.randao_reveal)) + mix = xor(get_randao_mix(state, epoch), sha256(body.randao_reveal)) state.randao_mixes[epoch % EPOCHS_PER_HISTORICAL_VECTOR] = mix ``` diff --git a/specs/altair/beacon-chain.md b/specs/altair/beacon-chain.md index d0d2b6fe360..66e4e65f473 100644 --- a/specs/altair/beacon-chain.md +++ b/specs/altair/beacon-chain.md @@ -326,7 +326,7 @@ def get_next_sync_committee_indices(state: BeaconState) -> Sequence[ValidatorInd Uint64(i % active_validator_count), active_validator_count, seed ) candidate_index = active_validator_indices[shuffled_index] - random_byte = hash(seed + uint_to_bytes(Uint64(i // 32)))[i % 32] + random_byte = sha256(seed + uint_to_bytes(Uint64(i // 32)))[i % 32] effective_balance = state.validators[candidate_index].effective_balance if effective_balance * MAX_RANDOM_BYTE >= MAX_EFFECTIVE_BALANCE * random_byte: sync_committee_indices.append(candidate_index) diff --git a/specs/altair/validator.md b/specs/altair/validator.md index 833e60a181f..edca0e50a13 100644 --- a/specs/altair/validator.md +++ b/specs/altair/validator.md @@ -454,7 +454,7 @@ def is_sync_committee_aggregator(signature: BLSSignature) -> bool: // SYNC_COMMITTEE_SUBNET_COUNT // TARGET_AGGREGATORS_PER_SYNC_SUBCOMMITTEE, ) - return bytes_to_uint64(hash(signature)[0:8]) % modulo == 0 + return bytes_to_uint64(sha256(signature)[0:8]) % modulo == 0 ``` *Note*: The set of aggregators generally changes every slot; however, the diff --git a/specs/capella/beacon-chain.md b/specs/capella/beacon-chain.md index 9e06d74812f..b55c8d53c65 100644 --- a/specs/capella/beacon-chain.md +++ b/specs/capella/beacon-chain.md @@ -622,7 +622,7 @@ def process_bls_to_execution_change( validator = state.validators[address_change.validator_index] assert validator.withdrawal_credentials[:1] == BLS_WITHDRAWAL_PREFIX - assert validator.withdrawal_credentials[1:] == hash(address_change.from_bls_pubkey)[1:] + assert validator.withdrawal_credentials[1:] == sha256(address_change.from_bls_pubkey)[1:] # Fork-agnostic domain since address changes are valid across forks domain = compute_domain( diff --git a/specs/capella/p2p-interface.md b/specs/capella/p2p-interface.md index 4a953fe4919..e808dd7c3ff 100644 --- a/specs/capella/p2p-interface.md +++ b/specs/capella/p2p-interface.md @@ -237,7 +237,8 @@ def validate_bls_to_execution_change_gossip( raise GossipReject("validator does not have BLS withdrawal credentials") # [REJECT] The bls_to_execution_change is for the validator's withdrawal pubkey - if validator.withdrawal_credentials[1:] != hash(bls_to_execution_change.from_bls_pubkey)[1:]: + pubkey = bls_to_execution_change.from_bls_pubkey + if validator.withdrawal_credentials[1:] != sha256(pubkey)[1:]: raise GossipReject("pubkey does not match validator withdrawal credentials") # [REJECT] The signature is valid @@ -245,11 +246,7 @@ def validate_bls_to_execution_change_gossip( DOMAIN_BLS_TO_EXECUTION_CHANGE, genesis_validators_root=state.genesis_validators_root ) signing_root = compute_signing_root(bls_to_execution_change, domain) - if not bls.Verify( - bls_to_execution_change.from_bls_pubkey, - signing_root, - signed_bls_to_execution_change.signature, - ): + if not bls.Verify(pubkey, signing_root, signed_bls_to_execution_change.signature): raise GossipReject("invalid BLS to execution change signature") # Mark this bls_to_execution_change as seen diff --git a/specs/deneb/beacon-chain.md b/specs/deneb/beacon-chain.md index 489b392b8f9..d0d4696dbed 100644 --- a/specs/deneb/beacon-chain.md +++ b/specs/deneb/beacon-chain.md @@ -281,7 +281,7 @@ class BeaconState(Container): ```python def kzg_commitment_to_versioned_hash(kzg_commitment: KZGCommitment) -> VersionedHash: - return VERSIONED_HASH_VERSION_KZG + hash(kzg_commitment)[1:] + return VERSIONED_HASH_VERSION_KZG + sha256(kzg_commitment)[1:] ``` ### Beacon state accessors diff --git a/specs/electra/beacon-chain.md b/specs/electra/beacon-chain.md index c54735d5e40..35a23cd3f43 100644 --- a/specs/electra/beacon-chain.md +++ b/specs/electra/beacon-chain.md @@ -581,7 +581,7 @@ def compute_proposer_index( while True: candidate_index = indices[compute_shuffled_index(i % total, total, seed)] # [Modified in Electra] - random_bytes = hash(seed + uint_to_bytes(i // 16)) + random_bytes = sha256(seed + uint_to_bytes(i // 16)) offset = i % 16 * 2 random_value = bytes_to_uint64(random_bytes[offset : offset + 2]) effective_balance = state.validators[candidate_index].effective_balance @@ -815,7 +815,7 @@ def get_next_sync_committee_indices(state: BeaconState) -> Sequence[ValidatorInd ) candidate_index = active_validator_indices[shuffled_index] # [Modified in Electra] - random_bytes = hash(seed + uint_to_bytes(i // 16)) + random_bytes = sha256(seed + uint_to_bytes(i // 16)) offset = i % 16 * 2 random_value = bytes_to_uint64(random_bytes[offset : offset + 2]) effective_balance = state.validators[candidate_index].effective_balance diff --git a/specs/fulu/beacon-chain.md b/specs/fulu/beacon-chain.md index 9fad7746c28..7400fa5ea76 100644 --- a/specs/fulu/beacon-chain.md +++ b/specs/fulu/beacon-chain.md @@ -326,7 +326,7 @@ def compute_fork_digest( bytes( xor( base_digest, - hash( + sha256( uint_to_bytes(Uint64(blob_parameters.epoch)) + uint_to_bytes(Uint64(blob_parameters.max_blobs_per_block)) ), @@ -345,7 +345,7 @@ def compute_proposer_indices( Return the proposer indices for the given ``epoch``. """ start_slot = compute_start_slot_at_epoch(epoch) - seeds = [hash(seed + uint_to_bytes(Slot(start_slot + i))) for i in range(SLOTS_PER_EPOCH)] + seeds = [sha256(seed + uint_to_bytes(Slot(start_slot + i))) for i in range(SLOTS_PER_EPOCH)] return ProposerIndices(compute_proposer_index(state, indices, seed) for seed in seeds) ``` diff --git a/specs/fulu/das-core.md b/specs/fulu/das-core.md index eb279bcbcb0..cd80efe8165 100644 --- a/specs/fulu/das-core.md +++ b/specs/fulu/das-core.md @@ -203,7 +203,7 @@ def get_custody_groups(node_id: NodeID, custody_group_count: Uint64) -> Sequence custody_groups: list[CustodyIndex] = [] while len(custody_groups) < custody_group_count: custody_group = CustodyIndex( - bytes_to_uint64(hash(uint_to_bytes(current_id))[0:8]) % NUMBER_OF_CUSTODY_GROUPS + bytes_to_uint64(sha256(uint_to_bytes(current_id))[0:8]) % NUMBER_OF_CUSTODY_GROUPS ) if custody_group not in custody_groups: custody_groups.append(custody_group) diff --git a/specs/gloas/beacon-chain.md b/specs/gloas/beacon-chain.md index ff49c89c991..5ac477bcb1a 100644 --- a/specs/gloas/beacon-chain.md +++ b/specs/gloas/beacon-chain.md @@ -1174,7 +1174,7 @@ def compute_balance_weighted_selection( while len(selected) < size: offset = i % 16 * 2 if offset == 0: - random_bytes = hash(seed + uint_to_bytes(i // 16)) + random_bytes = sha256(seed + uint_to_bytes(i // 16)) next_index = i % total if shuffle_indices: next_index = compute_shuffled_index(next_index, total, seed) @@ -1201,7 +1201,7 @@ def compute_proposer_indices( Return the proposer indices for the given ``epoch``. """ start_slot = compute_start_slot_at_epoch(epoch) - seeds = [hash(seed + uint_to_bytes(Slot(start_slot + i))) for i in range(SLOTS_PER_EPOCH)] + seeds = [sha256(seed + uint_to_bytes(Slot(start_slot + i))) for i in range(SLOTS_PER_EPOCH)] # [Modified in Gloas:EIP7732] return ProposerIndices( compute_balance_weighted_selection(state, indices, seed, size=1, shuffle_indices=True)[0] @@ -1217,7 +1217,7 @@ def compute_ptc(state: BeaconState, slot: Slot) -> PayloadTimelinessCommittee: Get the payload timeliness committee, with possible duplicates, for the given ``slot``. """ epoch = compute_epoch_at_slot(slot) - seed = hash(get_seed(state, epoch, DOMAIN_PTC_ATTESTER) + uint_to_bytes(slot)) + seed = sha256(get_seed(state, epoch, DOMAIN_PTC_ATTESTER) + uint_to_bytes(slot)) indices: list[ValidatorIndex] = [] # Concatenate all committees for this slot in order committees_per_slot = get_committee_count_per_slot(state, epoch) diff --git a/specs/phase0/beacon-chain.md b/specs/phase0/beacon-chain.md index 5939a55a295..70bffa47d1d 100644 --- a/specs/phase0/beacon-chain.md +++ b/specs/phase0/beacon-chain.md @@ -90,7 +90,7 @@ - [`uint_to_bytes`](#uint_to_bytes) - [`bytes_to_uint64`](#bytes_to_uint64) - [Crypto](#crypto) - - [`hash`](#hash) + - [`sha256`](#sha256) - [`hash_tree_root`](#hash_tree_root) - [BLS signatures](#bls-signatures) - [Predicates](#predicates) @@ -985,9 +985,15 @@ def bytes_to_uint64(data: bytes) -> Uint64: ### Crypto -#### `hash` +#### `sha256` -`def hash(data: bytes) -> Bytes32` is SHA256. +```python +def sha256(data: bytes) -> Bytes32: + """ + Return the SHA256 hash of ``data``. + """ + return Bytes32(sha256_hash(data).digest()) +``` #### `hash_tree_root` @@ -1111,9 +1117,9 @@ def compute_merkle_branch_root( value = leaf for i in range(depth): if index // (2**i) % 2: - value = hash(branch[i] + value) + value = sha256(branch[i] + value) else: - value = hash(value + branch[i]) + value = sha256(value + branch[i]) return Root(value) ``` @@ -1145,14 +1151,14 @@ def compute_shuffled_permutation(index_count: Uint64, seed: Bytes32) -> Sequence indices = [Uint64(i) for i in range(index_count)] for current_round in range(SHUFFLE_ROUND_COUNT): round_bytes = current_round.to_bytes(1, "little") - pivot = int.from_bytes(hash(seed + round_bytes)[0:8], "little") % index_count + pivot = int.from_bytes(sha256(seed + round_bytes)[0:8], "little") % index_count source_by_bucket: Dict[Uint64, Bytes32] = {} for i in range(index_count): flip = (pivot + index_count - indices[i]) % index_count position = max(indices[i], flip) position_bucket = position // 256 if position_bucket not in source_by_bucket: - source_by_bucket[position_bucket] = hash( + source_by_bucket[position_bucket] = sha256( seed + round_bytes + position_bucket.to_bytes(4, "little") ) source = source_by_bucket[position_bucket] @@ -1188,7 +1194,7 @@ def compute_proposer_index( total = Uint64(len(indices)) while True: candidate_index = indices[compute_shuffled_index(i % total, total, seed)] - random_byte = hash(seed + uint_to_bytes(Uint64(i // 32)))[i % 32] + random_byte = sha256(seed + uint_to_bytes(Uint64(i // 32)))[i % 32] effective_balance = state.validators[candidate_index].effective_balance if effective_balance * MAX_RANDOM_BYTE >= MAX_EFFECTIVE_BALANCE * random_byte: return candidate_index @@ -1391,7 +1397,7 @@ def get_seed(state: BeaconState, epoch: Epoch, domain_type: DomainType) -> Bytes mix = get_randao_mix( state, Epoch(epoch + EPOCHS_PER_HISTORICAL_VECTOR - MIN_SEED_LOOKAHEAD - 1) ) # Avoid underflow - return hash(domain_type + uint_to_bytes(epoch) + mix) + return sha256(domain_type + uint_to_bytes(epoch) + mix) ``` #### `get_committee_count_per_slot` @@ -1439,7 +1445,7 @@ def get_beacon_proposer_index(state: BeaconState) -> ValidatorIndex: Return the beacon proposer index at the current slot. """ epoch = get_current_epoch(state) - seed = hash(get_seed(state, epoch, DOMAIN_BEACON_PROPOSER) + uint_to_bytes(state.slot)) + seed = sha256(get_seed(state, epoch, DOMAIN_BEACON_PROPOSER) + uint_to_bytes(state.slot)) indices = get_active_validator_indices(state, epoch) return compute_proposer_index(state, indices, seed) ``` @@ -2236,7 +2242,7 @@ def process_randao(state: BeaconState, body: BeaconBlockBody) -> None: signing_root = compute_signing_root(epoch, get_domain(state, DOMAIN_RANDAO)) assert bls.Verify(proposer.pubkey, signing_root, body.randao_reveal) # Mix in RANDAO reveal - mix = xor(get_randao_mix(state, epoch), hash(body.randao_reveal)) + mix = xor(get_randao_mix(state, epoch), sha256(body.randao_reveal)) state.randao_mixes[epoch % EPOCHS_PER_HISTORICAL_VECTOR] = mix ``` diff --git a/specs/phase0/p2p-interface.md b/specs/phase0/p2p-interface.md index 97e3d61efab..310a476b45c 100644 --- a/specs/phase0/p2p-interface.md +++ b/specs/phase0/p2p-interface.md @@ -1792,7 +1792,7 @@ def compute_subscribed_subnet(node_id: NodeID, epoch: Epoch, index: int) -> Subn prefix_bits = int(compute_attestation_subnet_prefix_bits()) node_id_prefix = node_id >> int(NODE_ID_BITS - prefix_bits) node_offset = Uint64(node_id % Uint256(EPOCHS_PER_SUBNET_SUBSCRIPTION)) - permutation_seed = hash( + permutation_seed = sha256( uint_to_bytes(Uint64((epoch + node_offset) // EPOCHS_PER_SUBNET_SUBSCRIPTION)) ) permutated_prefix = compute_shuffled_index( diff --git a/specs/phase0/validator.md b/specs/phase0/validator.md index fa6735058d7..454849e363c 100644 --- a/specs/phase0/validator.md +++ b/specs/phase0/validator.md @@ -172,7 +172,7 @@ Withdrawal credentials with the BLS withdrawal prefix allow a BLS key pair `withdrawal_credentials` field must be such that: - `withdrawal_credentials[:1] == BLS_WITHDRAWAL_PREFIX` -- `withdrawal_credentials[1:] == hash(bls_withdrawal_pubkey)[1:]` +- `withdrawal_credentials[1:] == sha256(bls_withdrawal_pubkey)[1:]` *Note*: The `bls_withdrawal_privkey` is not required for validating and can be kept in cold storage. @@ -735,7 +735,7 @@ def is_aggregator( ) -> bool: committee = get_beacon_committee(state, slot, index) modulo = max(1, len(committee) // TARGET_AGGREGATORS_PER_COMMITTEE) - return bytes_to_uint64(hash(slot_signature)[0:8]) % modulo == 0 + return bytes_to_uint64(sha256(slot_signature)[0:8]) % modulo == 0 ``` #### Construct aggregate diff --git a/tests/core/pyspec/eth_consensus_specs/test/altair/unittests/validator/test_validator.py b/tests/core/pyspec/eth_consensus_specs/test/altair/unittests/validator/test_validator.py index 31789121133..e89aa5efb97 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/altair/unittests/validator/test_validator.py +++ b/tests/core/pyspec/eth_consensus_specs/test/altair/unittests/validator/test_validator.py @@ -259,7 +259,7 @@ def test_is_sync_committee_aggregator(spec, state): sample_count = int(spec.SYNC_COMMITTEE_SIZE // spec.SYNC_COMMITTEE_SUBNET_COUNT) * 100 is_aggregator_count = 0 for i in range(sample_count): - signature = spec.hash(i.to_bytes(32, byteorder="little")) + signature = spec.sha256(i.to_bytes(32, byteorder="little")) if spec.is_sync_committee_aggregator(signature): is_aggregator_count += 1 diff --git a/tests/core/pyspec/eth_consensus_specs/test/bellatrix/sync/test_optimistic.py b/tests/core/pyspec/eth_consensus_specs/test/bellatrix/sync/test_optimistic.py index 0c557ac2fe9..dd6188f8c8f 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/bellatrix/sync/test_optimistic.py +++ b/tests/core/pyspec/eth_consensus_specs/test/bellatrix/sync/test_optimistic.py @@ -69,7 +69,7 @@ def test_from_syncing_to_invalid(spec, state): block.body.execution_payload.parent_hash = ( block_hashes[f"chain_a_{i - 1}"] if i != 0 else block_hashes["block_0"] ) - block.body.execution_payload.extra_data = spec.hash(bytes(f"chain_a_{i}", "UTF-8")) + block.body.execution_payload.extra_data = spec.sha256(bytes(f"chain_a_{i}", "UTF-8")) block.body.execution_payload.block_hash = compute_el_block_hash( spec, block.body.execution_payload, state ) @@ -90,7 +90,7 @@ def test_from_syncing_to_invalid(spec, state): block.body.execution_payload.parent_hash = ( block_hashes[f"chain_b_{i - 1}"] if i != 0 else block_hashes["block_0"] ) - block.body.execution_payload.extra_data = spec.hash(bytes(f"chain_b_{i}", "UTF-8")) + block.body.execution_payload.extra_data = spec.sha256(bytes(f"chain_b_{i}", "UTF-8")) block.body.execution_payload.block_hash = compute_el_block_hash( spec, block.body.execution_payload, state ) @@ -110,7 +110,7 @@ def test_from_syncing_to_invalid(spec, state): block.body.execution_payload.parent_hash = signed_blocks_b[ -1 ].message.body.execution_payload.block_hash - block.body.execution_payload.extra_data = spec.hash(bytes(f"chain_b_{i}", "UTF-8")) + block.body.execution_payload.extra_data = spec.sha256(bytes(f"chain_b_{i}", "UTF-8")) block.body.execution_payload.block_hash = compute_el_block_hash( spec, block.body.execution_payload, state ) diff --git a/tests/core/pyspec/eth_consensus_specs/test/capella/block_processing/test_process_bls_to_execution_change.py b/tests/core/pyspec/eth_consensus_specs/test/capella/block_processing/test_process_bls_to_execution_change.py index ba634c23e5b..a5c46568ad1 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/capella/block_processing/test_process_bls_to_execution_change.py +++ b/tests/core/pyspec/eth_consensus_specs/test/capella/block_processing/test_process_bls_to_execution_change.py @@ -273,7 +273,7 @@ def test_valid_signature_from_staking_deposit_cli(spec, state): "4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95" ) validator = state.validators[validator_index] - validator.withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(from_bls_pubkey)[1:] + validator.withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.sha256(from_bls_pubkey)[1:] address_change = spec.BLSToExecutionChange( validator_index=validator_index, diff --git a/tests/core/pyspec/eth_consensus_specs/test/deneb/unittests/validator/test_validator.py b/tests/core/pyspec/eth_consensus_specs/test/deneb/unittests/validator/test_validator.py index 94fc173ddea..ff0dec362cb 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/deneb/unittests/validator/test_validator.py +++ b/tests/core/pyspec/eth_consensus_specs/test/deneb/unittests/validator/test_validator.py @@ -56,7 +56,7 @@ def test_blob_sidecar_inclusion_proof_incorrect_wrong_body(spec, state): for blob_sidecar in blob_sidecars: block = blob_sidecar.signed_block_header.message - block.body_root = spec.hash(block.body_root) # mutate body root to break proof + block.body_root = spec.sha256(block.body_root) # mutate body root to break proof assert not spec.verify_blob_sidecar_inclusion_proof(blob_sidecar) diff --git a/tests/core/pyspec/eth_consensus_specs/test/eip8321/block_processing/test_process_randao.py b/tests/core/pyspec/eth_consensus_specs/test/eip8321/block_processing/test_process_randao.py index 7fe23e83544..7bfaea86882 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/eip8321/block_processing/test_process_randao.py +++ b/tests/core/pyspec/eth_consensus_specs/test/eip8321/block_processing/test_process_randao.py @@ -89,7 +89,7 @@ def test_unregistered_proposer_uses_bls_reveal(spec, state): yield from run_process_randao(spec, state, block) assert spec.get_randao_mix(state, epoch) == spec.xor( - pre_mix, spec.hash(block.body.randao_reveal) + pre_mix, spec.sha256(block.body.randao_reveal) ) # An unregistered validator stays unregistered assert state.randao_commitments[proposer_index] == spec.Bytes32() diff --git a/tests/core/pyspec/eth_consensus_specs/test/electra/epoch_processing/pending_deposits/test_apply_pending_deposit.py b/tests/core/pyspec/eth_consensus_specs/test/electra/epoch_processing/pending_deposits/test_apply_pending_deposit.py index a8ce343d18e..41c29f89cd3 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/electra/epoch_processing/pending_deposits/test_apply_pending_deposit.py +++ b/tests/core/pyspec/eth_consensus_specs/test/electra/epoch_processing/pending_deposits/test_apply_pending_deposit.py @@ -356,7 +356,7 @@ def test_apply_pending_deposit_incorrect_sig_top_up(spec, state): def test_apply_pending_deposit_incorrect_withdrawal_credentials_top_up(spec, state): validator_index = 0 amount = spec.MIN_ACTIVATION_BALANCE // 4 - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(b"junk")[1:] + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.sha256(b"junk")[1:] pending_deposit = prepare_pending_deposit( spec, validator_index, amount, signed=True, withdrawal_credentials=withdrawal_credentials ) diff --git a/tests/core/pyspec/eth_consensus_specs/test/electra/sanity/blocks/test_deposit_transition.py b/tests/core/pyspec/eth_consensus_specs/test/electra/sanity/blocks/test_deposit_transition.py index a9c259a9543..2b4e354f14e 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/electra/sanity/blocks/test_deposit_transition.py +++ b/tests/core/pyspec/eth_consensus_specs/test/electra/sanity/blocks/test_deposit_transition.py @@ -104,7 +104,7 @@ def prepare_state_and_block( # use min activation balance spec.MIN_ACTIVATION_BALANCE, # insecurely use pubkey as withdrawal key - spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkeys[keypair_index])[1:], + spec.BLS_WITHDRAWAL_PREFIX + spec.sha256(pubkeys[keypair_index])[1:], signed=True, ) deposit_data_list.append(deposit_data) diff --git a/tests/core/pyspec/eth_consensus_specs/test/gloas/block_processing/test_process_payload_attestation.py b/tests/core/pyspec/eth_consensus_specs/test/gloas/block_processing/test_process_payload_attestation.py index ce47eef9da1..38f582376b9 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/gloas/block_processing/test_process_payload_attestation.py +++ b/tests/core/pyspec/eth_consensus_specs/test/gloas/block_processing/test_process_payload_attestation.py @@ -114,7 +114,7 @@ def prepare_signed_payload_attestation( def _get_ptc_from_indices(spec, state, slot, indices): slot = spec.Slot(slot) epoch = spec.compute_epoch_at_slot(slot) - seed = spec.hash( + seed = spec.sha256( spec.get_seed(state, epoch, spec.DOMAIN_PTC_ATTESTER) + spec.uint_to_bytes(slot) ) return spec.compute_balance_weighted_selection( @@ -131,7 +131,7 @@ def _compute_selection_with_acceptance_iterations(spec, state, indices, seed, si while len(selected) < size: offset = i % 16 * 2 if offset == 0: - random_bytes = spec.hash(seed + spec.uint_to_bytes(spec.Uint64(i // 16))) + random_bytes = spec.sha256(seed + spec.uint_to_bytes(spec.Uint64(i // 16))) candidate_index = indices[i % total] effective_balance = state.validators[candidate_index].effective_balance random_value = spec.bytes_to_uint64(random_bytes[offset : offset + 2]) @@ -401,7 +401,7 @@ def test_process_payload_attestation_sampling_not_capped(spec, state): for i in range(committees_per_slot): indices.extend(spec.get_beacon_committee(state, slot, spec.CommitteeIndex(i))) - seed = spec.hash( + seed = spec.sha256( spec.get_seed(state, epoch, spec.DOMAIN_PTC_ATTESTER) + spec.uint_to_bytes(slot) ) diff --git a/tests/core/pyspec/eth_consensus_specs/test/gloas/fork/test_gloas_fork_onboard_builders.py b/tests/core/pyspec/eth_consensus_specs/test/gloas/fork/test_gloas_fork_onboard_builders.py index 0eb4fbd5043..799c64bd9ab 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/gloas/fork/test_gloas_fork_onboard_builders.py +++ b/tests/core/pyspec/eth_consensus_specs/test/gloas/fork/test_gloas_fork_onboard_builders.py @@ -23,7 +23,7 @@ def get_builder_withdrawal_credentials(spec, pubkey): """Create builder withdrawal credentials from a pubkey.""" - return spec.BUILDER_WITHDRAWAL_PREFIX + b"\x00" * 11 + spec.hash(pubkey)[12:] + return spec.BUILDER_WITHDRAWAL_PREFIX + b"\x00" * 11 + spec.sha256(pubkey)[12:] def create_pending_deposit_for_builder(spec, pubkey, amount, signed=True): diff --git a/tests/core/pyspec/eth_consensus_specs/test/helpers/builder_deposit_requests.py b/tests/core/pyspec/eth_consensus_specs/test/helpers/builder_deposit_requests.py index c20aa06cd19..8dd265bbed3 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/helpers/builder_deposit_requests.py +++ b/tests/core/pyspec/eth_consensus_specs/test/helpers/builder_deposit_requests.py @@ -72,7 +72,7 @@ def prepare_process_builder_deposit_request( else: # Builder withdrawal prefix followed by an eth1 address derived from the pubkey effective_withdrawal_credentials = ( - spec.BUILDER_WITHDRAWAL_PREFIX + b"\x00" * 11 + spec.hash(effective_pubkey)[12:] + spec.BUILDER_WITHDRAWAL_PREFIX + b"\x00" * 11 + spec.sha256(effective_pubkey)[12:] ) # Phase 3: Apply state overrides (before creating request) diff --git a/tests/core/pyspec/eth_consensus_specs/test/helpers/deposit_requests.py b/tests/core/pyspec/eth_consensus_specs/test/helpers/deposit_requests.py index f55726fa1da..5a235c91081 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/helpers/deposit_requests.py +++ b/tests/core/pyspec/eth_consensus_specs/test/helpers/deposit_requests.py @@ -52,7 +52,7 @@ def prepare_process_deposit_request( validator. pubkey: Explicit BLSPubkey. Default: derived from validator_index. withdrawal_credentials: Explicit Bytes32 credentials. Default: BLS prefix (0x00) + - hash(pubkey)[1:]. + sha256(pubkey)[1:]. amount: Deposit amount in Gwei. Default: MIN_ACTIVATION_BALANCE. signed: If True, sign with valid BLS signature. @@ -70,12 +70,12 @@ def prepare_process_deposit_request( effective_privkey = privkeys[index] effective_amount = amount if amount is not None else spec.MIN_ACTIVATION_BALANCE - # Default withdrawal credentials: BLS prefix + hash(pubkey)[1:] + # Default withdrawal credentials: BLS prefix + sha256(pubkey)[1:] if withdrawal_credentials is not None: effective_withdrawal_credentials = withdrawal_credentials else: effective_withdrawal_credentials = ( - spec.BLS_WITHDRAWAL_PREFIX + spec.hash(effective_pubkey)[1:] + spec.BLS_WITHDRAWAL_PREFIX + spec.sha256(effective_pubkey)[1:] ) # Phase 3: Build deposit data and optionally sign diff --git a/tests/core/pyspec/eth_consensus_specs/test/helpers/deposits.py b/tests/core/pyspec/eth_consensus_specs/test/helpers/deposits.py index c609e9a5e54..f1b4758685f 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/helpers/deposits.py +++ b/tests/core/pyspec/eth_consensus_specs/test/helpers/deposits.py @@ -103,7 +103,7 @@ def prepare_full_genesis_deposits( pubkey = pubkeys[pubkey_index] privkey = privkeys[pubkey_index] # insecurely use pubkey as withdrawal key if no credentials provided - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkey)[1:] + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.sha256(pubkey)[1:] deposit, root, deposit_data_list = build_deposit( spec, deposit_data_list=deposit_data_list, @@ -143,7 +143,7 @@ def prepare_random_genesis_deposits( privkey = privkeys[pubkey_index] amount = rng.randint(min_amount, max_amount) random_byte = bytes([rng.randint(0, 255)]) - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(random_byte)[1:] + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.sha256(random_byte)[1:] deposit, root, deposit_data_list = build_deposit( spec, deposit_data_list=deposit_data_list, @@ -180,7 +180,7 @@ def prepare_state_and_deposit( # insecurely use pubkey as withdrawal key if no credentials provided if withdrawal_credentials is None: - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkey)[1:] + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.sha256(pubkey)[1:] deposit, root, deposit_data_list = build_deposit( spec, @@ -222,7 +222,7 @@ def prepare_deposit_request( # insecurely use pubkey as withdrawal key if no credentials provided if withdrawal_credentials is None: - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkey)[1:] + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.sha256(pubkey)[1:] deposit_data = build_deposit_data( spec, pubkey, privkey, amount, withdrawal_credentials, signed=signed @@ -282,7 +282,7 @@ def prepare_builder_deposit_request( if withdrawal_credentials is None: # Builder withdrawal prefix followed by an eth1 address derived from the pubkey withdrawal_credentials = ( - spec.BUILDER_WITHDRAWAL_PREFIX + b"\x00" * 11 + spec.hash(pubkey)[12:] + spec.BUILDER_WITHDRAWAL_PREFIX + b"\x00" * 11 + spec.sha256(pubkey)[12:] ) request = spec.BuilderDepositRequest( @@ -317,7 +317,7 @@ def prepare_pending_deposit( # insecurely use pubkey as withdrawal key if no credentials provided if withdrawal_credentials is None: - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkey)[1:] + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.sha256(pubkey)[1:] # use GENESIS_SLOT which is always finalized if no slot provided if slot is None: @@ -428,7 +428,7 @@ def run_deposit_processing_with_specific_fork_version( pubkey = pubkeys[validator_index] privkey = privkeys[validator_index] - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkey)[1:] + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.sha256(pubkey)[1:] deposit_message = spec.DepositMessage( pubkey=pubkey, withdrawal_credentials=withdrawal_credentials, amount=amount diff --git a/tests/core/pyspec/eth_consensus_specs/test/helpers/genesis.py b/tests/core/pyspec/eth_consensus_specs/test/helpers/genesis.py index 2eb59404361..e84f8cc1bf3 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/helpers/genesis.py +++ b/tests/core/pyspec/eth_consensus_specs/test/helpers/genesis.py @@ -31,7 +31,7 @@ def build_mock_builder(spec, i: int, balance: int): return spec.Builder( pubkey=builder_pubkeys[i], - execution_address=spec.ExecutionAddress(spec.hash(builder_pubkeys[i])[12:]), + execution_address=spec.ExecutionAddress(spec.sha256(builder_pubkeys[i])[12:]), balance=balance, deposit_epoch=0, withdrawable_epoch=spec.FAR_FUTURE_EPOCH, @@ -47,15 +47,15 @@ def build_mock_validator(spec, i: int, balance: int): withdrawal_credentials = ( spec.COMPOUNDING_WITHDRAWAL_PREFIX + b"\x00" * 11 - + spec.hash(withdrawal_pubkey)[12:] + + spec.sha256(withdrawal_pubkey)[12:] ) else: # insecurely use pubkey as withdrawal key as well - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(withdrawal_pubkey)[1:] + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.sha256(withdrawal_pubkey)[1:] max_effective_balance = spec.MAX_EFFECTIVE_BALANCE_ELECTRA else: # insecurely use pubkey as withdrawal key as well - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(withdrawal_pubkey)[1:] + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.sha256(withdrawal_pubkey)[1:] max_effective_balance = spec.MAX_EFFECTIVE_BALANCE validator = spec.Validator( diff --git a/tests/core/pyspec/eth_consensus_specs/test/helpers/multi_operations.py b/tests/core/pyspec/eth_consensus_specs/test/helpers/multi_operations.py index de83c2ed084..396912e8423 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/helpers/multi_operations.py +++ b/tests/core/pyspec/eth_consensus_specs/test/helpers/multi_operations.py @@ -143,7 +143,7 @@ def get_random_deposits(spec, state, rng, num_deposits=None): for i in range(num_deposits): index = len(state.validators) + i withdrawal_pubkey = pubkeys[((32 * 256) - 1 - index) % len(pubkeys)] - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(withdrawal_pubkey)[1:] + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.sha256(withdrawal_pubkey)[1:] _, root, deposit_data_leaves = build_deposit( spec, deposit_data_leaves, @@ -317,7 +317,7 @@ def get_random_deposit_requests(spec, state, rng, num_deposits=None): for _ in range(num_deposits): index = rng.randrange(0, num_deposits) withdrawal_pubkey = pubkeys[index] - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(withdrawal_pubkey)[1:] + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.sha256(withdrawal_pubkey)[1:] deposit, _, _ = build_deposit( spec, deposit_data_leaves, diff --git a/tests/core/pyspec/eth_consensus_specs/test/helpers/pow_block.py b/tests/core/pyspec/eth_consensus_specs/test/helpers/pow_block.py index e60b47c888d..bc039a49f8b 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/helpers/pow_block.py +++ b/tests/core/pyspec/eth_consensus_specs/test/helpers/pow_block.py @@ -22,8 +22,8 @@ def prepare_random_pow_block(spec, rng=None): if rng is None: rng = Random(3131) return spec.PowBlock( - block_hash=spec.Hash32(spec.hash(bytearray(rng.getrandbits(8) for _ in range(32)))), - parent_hash=spec.Hash32(spec.hash(bytearray(rng.getrandbits(8) for _ in range(32)))), + block_hash=spec.Hash32(spec.sha256(bytearray(rng.getrandbits(8) for _ in range(32)))), + parent_hash=spec.Hash32(spec.sha256(bytearray(rng.getrandbits(8) for _ in range(32)))), total_difficulty=Uint256(0), ) diff --git a/tests/core/pyspec/eth_consensus_specs/test/helpers/state.py b/tests/core/pyspec/eth_consensus_specs/test/helpers/state.py index d226f778ef2..53931fdc01b 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/helpers/state.py +++ b/tests/core/pyspec/eth_consensus_specs/test/helpers/state.py @@ -11,7 +11,6 @@ ) from eth_consensus_specs.test.helpers.forks import is_post_altair from eth_consensus_specs.test.helpers.voluntary_exits import get_unslashed_exited_validators -from eth_consensus_specs.utils.hash_function import hash from eth_consensus_specs.utils.ssz.ssz_impl import uint_to_bytes @@ -243,7 +242,7 @@ def get_beacon_proposer_index_and_threshold(spec, state) -> tuple[Uint64, Uint64 along with the threshold for that index. """ epoch = spec.get_current_epoch(state) - seed = hash( + seed = spec.sha256( spec.get_seed(state, epoch, spec.DOMAIN_BEACON_PROPOSER) + uint_to_bytes(state.slot) ) indices = spec.get_active_validator_indices(state, epoch) @@ -264,7 +263,7 @@ def electra_compute_proposer_index_and_threshold( while True: candidate_index = indices[spec.compute_shuffled_index(i % total, total, seed)] # [Modified in Electra] - random_bytes = hash(seed + uint_to_bytes(i // 16)) + random_bytes = spec.sha256(seed + uint_to_bytes(i // 16)) offset = i % 16 * 2 random_value = spec.bytes_to_uint64(random_bytes[offset : offset + 2]) effective_balance = state.validators[candidate_index].effective_balance diff --git a/tests/core/pyspec/eth_consensus_specs/test/phase0/block_processing/test_process_deposit.py b/tests/core/pyspec/eth_consensus_specs/test/phase0/block_processing/test_process_deposit.py index 4bfe77184c9..25230dc791c 100644 --- a/tests/core/pyspec/eth_consensus_specs/test/phase0/block_processing/test_process_deposit.py +++ b/tests/core/pyspec/eth_consensus_specs/test/phase0/block_processing/test_process_deposit.py @@ -188,7 +188,7 @@ def test_incorrect_sig_top_up(spec, state): def test_incorrect_withdrawal_credentials_top_up(spec, state): validator_index = 0 amount = spec.MAX_EFFECTIVE_BALANCE // 4 - withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(b"junk")[1:] + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.sha256(b"junk")[1:] deposit = prepare_state_and_deposit( spec, state, validator_index, amount, withdrawal_credentials=withdrawal_credentials ) diff --git a/tests/core/pyspec/eth_consensus_specs/utils/hash_function.py b/tests/core/pyspec/eth_consensus_specs/utils/hash_function.py deleted file mode 100644 index 4940a442f93..00000000000 --- a/tests/core/pyspec/eth_consensus_specs/utils/hash_function.py +++ /dev/null @@ -1,14 +0,0 @@ -from hashlib import sha256 - -from blake3 import blake3 as _blake3 -from remerkleable.byte_arrays import Bytes32 - -ZERO_BYTES32 = b"\x00" * 32 - - -def hash(x: bytes | bytearray | memoryview) -> Bytes32: - return bytes.__new__(Bytes32, sha256(x).digest()) - - -def blake3(x: bytes | bytearray | memoryview) -> Bytes32: - return bytes.__new__(Bytes32, _blake3(x).digest()) diff --git a/tests/core/pyspec/eth_consensus_specs/utils/merkle_minimal.py b/tests/core/pyspec/eth_consensus_specs/utils/merkle_minimal.py index 0e67dfc7c6f..8ad2b185a22 100644 --- a/tests/core/pyspec/eth_consensus_specs/utils/merkle_minimal.py +++ b/tests/core/pyspec/eth_consensus_specs/utils/merkle_minimal.py @@ -1,12 +1,11 @@ +from hashlib import sha256 from math import log2 -from eth_consensus_specs.utils.hash_function import hash - ZERO_BYTES32 = b"\x00" * 32 zerohashes = [ZERO_BYTES32] for layer in range(1, 100): - zerohashes.append(hash(zerohashes[layer - 1] + zerohashes[layer - 1])) + zerohashes.append(sha256(zerohashes[layer - 1] + zerohashes[layer - 1]).digest()) def calc_merkle_tree_from_leaves(values, layer_count=32): @@ -15,7 +14,7 @@ def calc_merkle_tree_from_leaves(values, layer_count=32): for h in range(layer_count): if len(values) % 2 == 1: values.append(zerohashes[h]) - values = [hash(values[i] + values[i + 1]) for i in range(0, len(values), 2)] + values = [sha256(values[i] + values[i + 1]).digest() for i in range(0, len(values), 2)] tree.append(values[::]) return tree @@ -66,13 +65,13 @@ def merge(h, i): while True: if i & (1 << j) == 0: if i == count and j < depth: - h = hash( + h = sha256( h + zerohashes[j] - ) # keep going if we are complementing the void to the next power of 2 + ).digest() # keep going if we are complementing the void to the next power of 2 else: break else: - h = hash(tmp[j] + h) + h = sha256(tmp[j] + h).digest() j += 1 tmp[j] = h @@ -86,6 +85,6 @@ def merge(h, i): # the next power of two may be smaller than the ultimate virtual size, complement with zero-hashes at each depth. for j in range(depth, max_depth): - tmp[j + 1] = hash(tmp[j] + zerohashes[j]) + tmp[j + 1] = sha256(tmp[j] + zerohashes[j]).digest() return tmp[max_depth] diff --git a/tests/core/pyspec/eth_consensus_specs/utils/test_merkle_minimal.py b/tests/core/pyspec/eth_consensus_specs/utils/test_merkle_minimal.py index bdd6ab2a692..73844e7e2c4 100644 --- a/tests/core/pyspec/eth_consensus_specs/utils/test_merkle_minimal.py +++ b/tests/core/pyspec/eth_consensus_specs/utils/test_merkle_minimal.py @@ -1,11 +1,12 @@ +from hashlib import sha256 + import pytest -from .hash_function import hash from .merkle_minimal import get_merkle_root, merkleize_chunks, zerohashes def h(a: bytes, b: bytes) -> bytes: - return hash(a + b) + return sha256(a + b).digest() def e(v: int) -> bytes: