Revert speedups (#718)

* Revert "hash compatibility fix (#717)"

This reverts commit 047de2b788.

* Revert "port eth2_digest speedups to eth_hash (#716)"

This reverts commit 864d54467e.
This commit is contained in:
Jacek Sieka 2024-08-09 07:20:35 +02:00 committed by GitHub
parent 047de2b788
commit 56f72c7a66
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 19 additions and 37 deletions

View File

@ -2,7 +2,7 @@ import stint, ./common/eth_hash
type UInt2048 = StUint[2048]
iterator chunksForBloom(h: KeccakHash): array[2, uint8] =
iterator chunksForBloom(h: MDigest[256]): array[2, uint8] =
yield [h.data[0], h.data[1]]
yield [h.data[2], h.data[3]]
yield [h.data[4], h.data[5]]
@ -12,25 +12,25 @@ proc chunkToBloomBits(chunk: array[2, uint8]): UInt2048 =
let l = chunk[1].int
one(UInt2048) shl ((l + (h shl 8)) and 2047)
iterator bloomBits(h: KeccakHash): UInt2048 =
iterator bloomBits(h: MDigest[256]): UInt2048 =
for chunk in chunksForBloom(h):
yield chunkToBloomBits(chunk)
type BloomFilter* = object
value*: UInt2048
proc incl*(f: var BloomFilter, h: KeccakHash) =
proc incl*(f: var BloomFilter, h: MDigest[256]) =
for bits in bloomBits(h):
f.value = f.value or bits
proc init*(_: type BloomFilter, h: KeccakHash): BloomFilter =
proc init*(_: type BloomFilter, h: MDigest[256]): BloomFilter =
result.incl(h)
# TODO: The following 2 procs should be one genric, but it doesn't compile. Nim bug?
proc incl*(f: var BloomFilter, v: string) = f.incl(keccakHash(v))
proc incl*(f: var BloomFilter, v: openArray[byte]) = f.incl(keccakHash(v))
proc contains*(f: BloomFilter, h: KeccakHash): bool =
proc contains*(f: BloomFilter, h: MDigest[256]): bool =
for bits in bloomBits(h):
if (f.value and bits).isZero: return false
return true

View File

@ -9,19 +9,16 @@
## keccak256 is used across ethereum as the "default" hash function and this
## module provides a type and some helpers to produce such hashes
import std/hashes
import nimcrypto/[keccak, utils]
import nimcrypto/hash except `$`
from nimcrypto/utils import bytesToHex
import
nimcrypto/[keccak, hash]
export
keccak.update, keccak.finish, hash.fromHex, hash.toDigest, hashes.Hash
keccak.update, keccak.finish, hash
type
KeccakHash* = MDigest[256]
## A hash value computed using keccak256
## note: this aliases Eth2Digest too, which uses a different hash function!
## note: this aliases Eth2Digest too, which uses a different hash!
template withKeccakHash*(body: untyped): KeccakHash =
## This little helper will init the hash function and return the sliced
@ -47,23 +44,3 @@ func keccakHash*(a, b: openArray[byte]): KeccakHash =
withKeccakHash:
h.update a
h.update b
func `$`*(v: KeccakHash, dummy = false): string =
var res = newString((len(v.data) shl 1))
discard bytesToHex(v.data, res, {HexFlags.LowerCase})
res
template hash*(x: KeccakHash): Hash =
## Hash for digests for Nim hash tables
# digests are already good hashes
var h {.noinit.}: Hash
copyMem(addr h, unsafeAddr x.data[0], static(sizeof(Hash)))
h
func `==`*(a, b: KeccakHash, dummy = false): bool =
when nimvm:
a.data == b.data
else:
# nimcrypto uses a constant-time comparison for all MDigest types which for
# KeccakHash is unnecessary - the type should never hold a secret!
equalMem(unsafeAddr a.data[0], unsafeAddr b.data[0], sizeof(a.data))

View File

@ -10,8 +10,8 @@ import
export eth_hash, rlp
proc read*(rlp: var Rlp, T: typedesc[KeccakHash]): T =
proc read*(rlp: var Rlp, T: typedesc[MDigest]): T =
result.data = rlp.read(type(result.data))
proc append*(rlpWriter: var RlpWriter, a: KeccakHash) =
proc append*(rlpWriter: var RlpWriter, a: MDigest) =
rlpWriter.append(a.data)

View File

@ -17,8 +17,6 @@ import
./eth_hash,
./eth_times
from nimcrypto/hash import MDigest
export
results,
stint,

View File

@ -302,7 +302,7 @@ proc appendImpl(self: var RlpWriter, data: tuple) {.inline.} =
# score in order to facilitate easier overloading with user types:
template append*[T](w: var RlpWriter; data: T) =
when data is (SomeSignedInt|enum|bool):
when false and data is SomeSignedInt:
when data is SomeSignedInt:
# TODO potentially remove signed integer support - we should never make it
# this far!
{.warning: "Signed integers cannot reliably be encoded using RLP".}
@ -314,6 +314,7 @@ proc initRlpList*(listSize: int): RlpWriter =
result = initRlpWriter()
startList(result, listSize)
# TODO: This should return a lent value
template finish*(self: RlpWriter): seq[byte] =
doAssert self.pendingLists.len == 0, "Insufficient number of elements written to a started list"
self.output

View File

@ -1,4 +1,5 @@
import
stew/byteutils,
./trie_defs
export trie_defs
@ -9,3 +10,8 @@ template checkValidHashZ*(x: untyped) =
template isZeroHash*(x: openArray[byte]): bool =
x.len == 0
proc hashFromHex*(bits: static[int], input: string): MDigest[bits] =
MDigest(data: hexToByteArray[bits div 8](input))
template hashFromHex*(s: static[string]): untyped = hashFromHex(s.len * 4, s)