diff --git a/eth/common/eth_hash.nim b/eth/common/eth_hash.nim index eb0de80..f38c540 100644 --- a/eth/common/eth_hash.nim +++ b/eth/common/eth_hash.nim @@ -30,10 +30,16 @@ template withKeccakHash*(body: untyped): KeccakHash = body finish(h) -func keccakHash*(input: openArray[byte]): KeccakHash = - keccak256.digest(input) -func keccakHash*(input: openArray[char]): KeccakHash = - keccak256.digest(input) +func keccakHash*(input: openArray[byte]): KeccakHash {.noinit.} = + # We use the init-update-finish interface to avoid + # the expensive burning/clearing memory (20~30% perf) + var ctx: keccak256 + ctx.init() + ctx.update(input) + ctx.finish() + +func keccakHash*(input: openArray[char]): KeccakHash {.noinit.} = + keccakHash(input.toOpenArrayByte(0, input.high())) func keccakHash*(a, b: openArray[byte]): KeccakHash = withKeccakHash: