2018-11-27 17:10:09 -06:00
|
|
|
# beacon_chain
|
2019-03-09 12:34:08 -08:00
|
|
|
# Copyright (c) 2018-2019 Status Research & Development GmbH
|
2018-11-27 17:10:09 -06:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
# Serenity hash function / digest
|
|
|
|
#
|
initial 0.9.0 spec sync (#509)
* rename compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* remove some unnecessary imports; remove some crosslink-related code and tests; complete renaming of compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* rm more transfer-related code and tests; rm more unnecessary strutils imports
* rm remaining unused imports
* remove useless get_empty_per_epoch_cache(...)/compute_start_slot_of_epoch(...) calls
* rename compute_start_slot_of_epoch(...) to compute_start_slot_at_epoch(...)
* rename ACTIVATION_EXIT_DELAY to MAX_SEED_LOOKAHEAD
* update domain types to 0.9.0
* mark AttesterSlashing, IndexedAttestation, AttestationDataAndCustodyBit, DepositData, BeaconBlockHeader, Fork, integer_squareroot(...), and process_voluntary_exit(...) as 0.9.0
* mark increase_balance(...), decrease_balance(...), get_block_root(...), CheckPoint, Deposit, PendingAttestation, HistoricalBatch, is_active_validator(...), and is_slashable_attestation_data(...) as 0.9.0
* mark compute_activation_exit_epoch(...), bls_verify(...), Validator, get_active_validator_indices(...), get_current_epoch(...), get_total_active_balance(...), and get_previous_epoch(...) as 0.9.0
* mark get_block_root_at_slot(...), ProposerSlashing, get_domain(...), VoluntaryExit, mainnet preset Gwei values, minimal preset max operations, process_block_header(...), and is_slashable_validator(...) as 0.9.0
* mark makeWithdrawalCredentials(...), get_validator_churn_limit(...), get_total_balance(...), is_valid_indexed_attestation(...), bls_aggregate_pubkeys(...), initial genesis value/constants, Attestation, get_randao_mix(...), mainnet preset max operations per block constants, minimal preset Gwei values and time parameters, process_eth1_data(...), get_shuffled_seq(...), compute_committee(...), and process_slots(...) as 0.9.0; partially update get_indexed_attestation(...) to 0.9.0 by removing crosslink refs and associated tests
* mark initiate_validator_exit(...), process_registry_updates(...), BeaconBlock, Eth1Data, compute_domain(...), process_randao(...), process_attester_slashing(...), get_base_reward(...), and process_slot(...) as 0.9.0
2019-10-30 19:41:19 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.9.0/specs/core/0_beacon-chain.md#hash
|
2018-11-27 17:10:09 -06:00
|
|
|
#
|
2019-05-10 10:14:01 +02:00
|
|
|
# In Phase 0 the beacon chain is deployed with SHA256 (SHA2-256).
|
|
|
|
# Note that is is different from Keccak256 (often mistakenly called SHA3-256)
|
|
|
|
# and SHA3-256.
|
2019-03-09 12:34:08 -08:00
|
|
|
#
|
2019-05-10 10:14:01 +02:00
|
|
|
# In Eth1.0, the default hash function is Keccak256 and SHA256 is available as a precompiled contract.
|
2019-03-09 12:34:08 -08:00
|
|
|
#
|
2019-05-10 10:14:01 +02:00
|
|
|
# In our code base, to enable a smooth transition
|
|
|
|
# (already did Blake2b --> Keccak256 --> SHA2-256),
|
|
|
|
# we call this function `eth2hash`, and it outputs a `Eth2Digest`. Easy to sed :)
|
2018-11-27 17:10:09 -06:00
|
|
|
|
2018-12-19 14:58:53 +02:00
|
|
|
import
|
2019-09-01 17:02:49 +02:00
|
|
|
chronicles,
|
|
|
|
nimcrypto/[sha2, hash, utils], eth/common/eth_types_json_serialization,
|
2019-01-08 18:28:21 +01:00
|
|
|
hashes
|
2018-11-27 17:10:09 -06:00
|
|
|
|
2018-12-19 14:58:53 +02:00
|
|
|
export
|
|
|
|
eth_types_json_serialization, hash.`$`
|
2018-12-13 10:00:55 -06:00
|
|
|
|
2018-11-27 17:10:09 -06:00
|
|
|
type
|
|
|
|
Eth2Digest* = MDigest[32 * 8] ## `hash32` from spec
|
2019-05-10 10:14:01 +02:00
|
|
|
Eth2Hash* = sha256 ## Context for hash function
|
2018-11-27 17:10:09 -06:00
|
|
|
|
2019-09-01 17:02:49 +02:00
|
|
|
chronicles.formatIt Eth2Digest:
|
|
|
|
mixin toHex
|
|
|
|
it.data.toHex(true)
|
|
|
|
|
2019-02-28 15:21:29 -06:00
|
|
|
func shortLog*(x: Eth2Digest): string =
|
2019-09-01 17:02:49 +02:00
|
|
|
x.data.toHex(true)[0..7]
|
2019-02-28 15:21:29 -06:00
|
|
|
|
2019-05-10 10:14:01 +02:00
|
|
|
# TODO: expose an in-place digest function
|
|
|
|
# when hashing in loop or into a buffer
|
|
|
|
# See: https://github.com/cheatfate/nimcrypto/blob/b90ba3abd/nimcrypto/sha2.nim#L570
|
|
|
|
func eth2hash*(v: openArray[byte]): Eth2Digest {.inline.} =
|
|
|
|
# We use the init-update-finish interface to avoid
|
|
|
|
# the expensive burning/clearing memory (20~30% perf)
|
|
|
|
# TODO: security implication?
|
|
|
|
var ctx: sha256
|
|
|
|
ctx.init()
|
2019-03-28 22:38:41 +02:00
|
|
|
ctx.update(v)
|
2019-07-03 10:35:05 +03:00
|
|
|
ctx.finish()
|
2018-11-27 17:10:09 -06:00
|
|
|
|
2019-08-28 14:07:00 +02:00
|
|
|
proc update*(ctx: var Sha2Context; digest: Eth2Digest) =
|
|
|
|
ctx.update digest.data
|
|
|
|
|
2018-11-27 17:10:09 -06:00
|
|
|
template withEth2Hash*(body: untyped): Eth2Digest =
|
|
|
|
## This little helper will init the hash function and return the sliced
|
|
|
|
## hash:
|
|
|
|
## let hashOfData = withHash: h.update(data)
|
2019-08-28 14:07:00 +02:00
|
|
|
var h {.inject.}: sha256
|
|
|
|
init(h)
|
2018-11-27 17:10:09 -06:00
|
|
|
body
|
2019-08-28 14:07:00 +02:00
|
|
|
var res = finish(h)
|
|
|
|
res
|
2019-01-08 18:28:21 +01:00
|
|
|
|
|
|
|
func hash*(x: Eth2Digest): Hash =
|
2019-05-10 10:14:01 +02:00
|
|
|
## Hash for digests for Nim hash tables
|
2019-01-08 18:28:21 +01:00
|
|
|
# Stub for BeaconChainDB
|
|
|
|
|
|
|
|
# We just slice the first 4 or 8 bytes of the block hash
|
|
|
|
# depending of if we are on a 32 or 64-bit platform
|
2019-01-11 18:41:57 +02:00
|
|
|
result = cast[ptr Hash](unsafeAddr x)[]
|