nim-eth/eth/trie/trie_utils.nim

35 lines
907 B
Nim
Raw Normal View History

2019-02-05 12:01:10 +00:00
import
2020-04-18 08:17:59 +00:00
stew/byteutils,
nimcrypto/[hash, keccak],
trie_defs
2019-02-05 12:01:10 +00:00
template checkValidHashZ*(x: untyped) =
when x.type isnot KeccakHash:
2019-03-13 22:15:26 +00:00
doAssert(x.len == 32 or x.len == 0)
2019-02-05 12:01:10 +00:00
template isZeroHash*(x: openArray[byte]): bool =
2019-02-05 12:01:10 +00:00
x.len == 0
proc hashFromHex*(bits: static[int], input: string): MDigest[bits] =
2020-04-18 08:17:59 +00:00
MDigest(data: hexToByteArray[bits div 8](input))
2019-02-05 12:01:10 +00:00
template hashFromHex*(s: static[string]): untyped = hashFromHex(s.len * 4, s)
proc keccakHash*(input: openArray[byte]): KeccakHash =
keccak256.digest(input)
2019-02-05 12:01:10 +00:00
proc keccakHash*(dest: var openArray[byte], a, b: openArray[byte]) =
var ctx: keccak256
ctx.init()
if a.len != 0:
ctx.update(a[0].unsafeAddr, uint(a.len))
if b.len != 0:
ctx.update(b[0].unsafeAddr, uint(b.len))
ctx.finish dest
ctx.clear()
proc keccakHash*(a, b: openArray[byte]): KeccakHash =
var s: array[32, byte]
2019-02-05 12:01:10 +00:00
keccakHash(s, a, b)
KeccakHash(data: s)