2018-11-27 23:10:09 +00:00
|
|
|
# beacon_chain
|
2019-03-09 20:34:08 +00:00
|
|
|
# Copyright (c) 2018-2019 Status Research & Development GmbH
|
2018-11-27 23:10:09 +00: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
|
|
|
|
#
|
2019-03-16 19:52:37 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.5.0/specs/core/0_beacon-chain.md#hash
|
2018-11-27 23:10:09 +00:00
|
|
|
#
|
2019-03-09 20:34:08 +00:00
|
|
|
# In Phase 0 the beacon chain is deployed with the same hash function as
|
|
|
|
# Ethereum 1.0, i.e. Keccak-256 (also incorrectly known as SHA3).
|
|
|
|
#
|
|
|
|
# Note: We aim to migrate to a S[T/N]ARK-friendly hash function in a future
|
|
|
|
# Ethereum 2.0 deployment phase.
|
|
|
|
#
|
|
|
|
# https://crypto.stackexchange.com/questions/15727/what-are-the-key-differences-between-the-draft-sha-3-standard-and-the-keccak-sub
|
2018-11-27 23:10:09 +00:00
|
|
|
#
|
|
|
|
# In our code base, to enable a smooth transition, we call this function
|
|
|
|
# `eth2hash`, and it outputs a `Eth2Digest`. Easy to sed :)
|
|
|
|
|
2018-12-19 12:58:53 +00:00
|
|
|
import
|
2019-03-01 23:50:01 +00:00
|
|
|
nimcrypto/[keccak, hash], eth/common/eth_types_json_serialization,
|
2019-01-08 17:28:21 +00:00
|
|
|
hashes
|
2018-11-27 23:10:09 +00:00
|
|
|
|
2018-12-19 12:58:53 +00:00
|
|
|
export
|
|
|
|
eth_types_json_serialization, hash.`$`
|
2018-12-13 16:00:55 +00:00
|
|
|
|
2018-11-27 23:10:09 +00:00
|
|
|
type
|
|
|
|
Eth2Digest* = MDigest[32 * 8] ## `hash32` from spec
|
2019-03-01 23:50:01 +00:00
|
|
|
Eth2Hash* = keccak256 ## Context for hash function
|
2018-11-27 23:10:09 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
func shortLog*(x: Eth2Digest): string =
|
2019-03-26 14:49:42 +00:00
|
|
|
# result = is needed to fix https://github.com/status-im/nim-beacon-chain/issues/209
|
2019-03-26 14:48:04 +00:00
|
|
|
result = ($x)[0..7]
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2018-11-27 23:10:09 +00:00
|
|
|
func eth2hash*(v: openArray[byte]): Eth2Digest =
|
2019-04-03 15:46:22 +00:00
|
|
|
var ctx: keccak256 # use explicit type so we can rely on init being useless
|
2019-03-28 21:18:36 +00:00
|
|
|
# We can avoid this step for Keccak/SHA3 digests because `ctx` is already
|
|
|
|
# empty, but if digest will be changed next line must be enabled.
|
|
|
|
# ctx.init()
|
2019-03-28 20:38:41 +00:00
|
|
|
ctx.update(v)
|
|
|
|
result = ctx.finish()
|
2018-11-27 23:10:09 +00: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-04-03 15:46:22 +00:00
|
|
|
var h {.inject.}: keccak256
|
|
|
|
# TODO no need, as long as using keccak256: h.init()
|
2018-11-27 23:10:09 +00:00
|
|
|
body
|
2019-03-28 20:38:41 +00:00
|
|
|
var res = h.finish()
|
2018-11-27 23:10:09 +00:00
|
|
|
res
|
2019-01-08 17:28:21 +00:00
|
|
|
|
|
|
|
func hash*(x: Eth2Digest): Hash =
|
|
|
|
## Hash for Keccak digests for Nim hash tables
|
|
|
|
# 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 16:41:57 +00:00
|
|
|
result = cast[ptr Hash](unsafeAddr x)[]
|