From 7fa80cda6fc65a3fae2a16945ecb57083be729cc Mon Sep 17 00:00:00 2001 From: advaita-saha Date: Sat, 27 Sep 2025 02:50:06 +0530 Subject: [PATCH 1/4] initial constantine addition to mpt --- eth.nimble | 3 ++- eth/common/hashes.nim | 24 ++++++++++++++---------- eth/rlp/hash_writer.nim | 6 ++++-- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/eth.nimble b/eth.nimble index ea596ce2..96d1e5ff 100644 --- a/eth.nimble +++ b/eth.nimble @@ -21,7 +21,8 @@ requires "nim >= 2.0.10", "unittest2", "results", "minilru", - "snappy" + "snappy", + "constantine" let nimc = getEnv("NIMC", "nim") # Which nim compiler to use let lang = getEnv("NIMLANG", "c") # Which backend (c/cpp/js) diff --git a/eth/common/hashes.nim b/eth/common/hashes.nim index 0575ab68..d717fd1f 100644 --- a/eth/common/hashes.nim +++ b/eth/common/hashes.nim @@ -14,11 +14,12 @@ ## Its usage was limited to ethash, the proof-of-work algorithm that has been ## replaced with proof-of-stake. -import std/[typetraits, hashes], nimcrypto/keccak, ./base, stew/assign2 +import std/[typetraits, hashes], ./base, stew/assign2, constantine/hashes/h_keccak, nimcrypto/keccak -export hashes, keccak.update, keccak.finish +export hashes, keccak.update, keccak.finish, h_keccak type + KECCACK256* = h_keccak.KeccakContext[256, 0x01] Hash32* = distinct Bytes32 ## https://github.com/ethereum/execution-specs/blob/51fac24740e662844446439ceeb96a460aae0ba0/src/ethereum/crypto/hash.py#L19 Root* = Hash32 @@ -74,13 +75,13 @@ template to*(s: static string, _: type Hash32): Hash32 = template hash32*(s: static string): Hash32 = s.to(Hash32) -template to*(v: MDigest[256], _: type Hash32): Hash32 = - Hash32(v.data) +# template to*(v: MDigest[256], _: type Hash32): Hash32 = +# Hash32(v.data) -template to*(v: Hash32, _: type MDigest[256]): MDigest[256] = - var tmp {.noinit.}: MDigest[256] - assign(tmp.data, v.data) - tmp +# template to*(v: Hash32, _: type MDigest[256]): MDigest[256] = +# var tmp {.noinit.}: MDigest[256] +# assign(tmp.data, v.data) +# tmp const emptyKeccak256* = @@ -91,9 +92,12 @@ const ## of an empty MPT trie func keccak256*(input: openArray[byte]): Hash32 {.noinit.} = - var ctx: keccak.keccak256 + var + ctx: KECCACK256 + buff: array[32, byte] ctx.update(input) - ctx.finish().to(Hash32) + ctx.finish(buff) + Hash32(buff) func keccak256*(input: openArray[char]): Hash32 {.noinit.} = keccak256(input.toOpenArrayByte(0, input.high)) diff --git a/eth/rlp/hash_writer.nim b/eth/rlp/hash_writer.nim index 1728434a..644ee7e4 100644 --- a/eth/rlp/hash_writer.nim +++ b/eth/rlp/hash_writer.nim @@ -8,7 +8,7 @@ import nimcrypto/keccak, ./priv/defs, utils, ../common/hashes, length_writer type RlpHashWriter* = object - keccak: keccak.keccak256 + keccak: KECCACK256 lengths*: seq[int] wrapLengths*: seq[int] listCount: int @@ -97,7 +97,9 @@ func reInit*(self: var RlpHashWriter, tracker: var RlpLengthTracker) = self.wrapLengths = move(tracker.wrapLengths) template finish*(self: var RlpHashWriter): Hash32 = - self.keccak.finish.to(Hash32) + var buff: array[32, byte] + self.keccak.finish(buff) + Hash32(buff) func clear*(self: var RlpHashWriter) = # Prepare writer for reuse From 74d63a8c4bf35bb38f12631609f0bba6ec85e77a Mon Sep 17 00:00:00 2001 From: advaita-saha Date: Sat, 27 Sep 2025 03:39:54 +0530 Subject: [PATCH 2/4] fix tests --- eth/keyfile/keyfile.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/eth/keyfile/keyfile.nim b/eth/keyfile/keyfile.nim index bbe2b55b..118bf922 100644 --- a/eth/keyfile/keyfile.nim +++ b/eth/keyfile/keyfile.nim @@ -186,7 +186,7 @@ proc deriveKey(password: string, discard ctx.pbkdf2(password, salt, c, output) ok(output) of HashSHA3_256: - var ctx: HMAC[sha3_256] + var ctx: HMAC[keccak.sha3_256] discard ctx.pbkdf2(password, salt, c, output) ok(output) of HashSHA3_384: @@ -334,7 +334,7 @@ proc createKeyFileJson*(seckey: PrivateKey, ciphertext = ? encryptKey(seckey, cryptkind, dkey, iv) - var ctx: keccak256 + var ctx: keccak.keccak256 ctx.init() ctx.update(toOpenArray(dkey, 16, 31)) ctx.update(ciphertext) @@ -432,7 +432,7 @@ proc decodeScryptParams(params: JsonNode): KfResult[ScryptParams] = result = ok(p) func decryptPrivateKey(crypto: Crypto, dkey: DKey): KfResult[PrivateKey] = - var ctx: keccak256 + var ctx: keccak.keccak256 ctx.init() ctx.update(toOpenArray(dkey, 16, 31)) ctx.update(crypto.cipher.text) From 795647b59ace3d397a1ce95ae697d943fafef381 Mon Sep 17 00:00:00 2001 From: advaita-saha Date: Sat, 27 Sep 2025 04:13:10 +0530 Subject: [PATCH 3/4] add hash to template --- eth/common/hashes.nim | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/eth/common/hashes.nim b/eth/common/hashes.nim index d717fd1f..f20cf66d 100644 --- a/eth/common/hashes.nim +++ b/eth/common/hashes.nim @@ -103,6 +103,8 @@ func keccak256*(input: openArray[char]): Hash32 {.noinit.} = keccak256(input.toOpenArrayByte(0, input.high)) template withKeccak256*(body: untyped): Hash32 = - var h {.inject.}: keccak.keccak256 + var h {.inject.}: KECCACK256 body - h.finish().to(Hash32) + var buff: array[32, byte] + h.finish(buff) + Hash32(buff) From 97dd82703fae8ba6d8eeb3f03f657ef894199749 Mon Sep 17 00:00:00 2001 From: advaita-saha Date: Sat, 27 Sep 2025 04:41:02 +0530 Subject: [PATCH 4/4] make it eth2 compatible --- eth/common/hashes.nim | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/eth/common/hashes.nim b/eth/common/hashes.nim index f20cf66d..e796ac61 100644 --- a/eth/common/hashes.nim +++ b/eth/common/hashes.nim @@ -75,13 +75,13 @@ template to*(s: static string, _: type Hash32): Hash32 = template hash32*(s: static string): Hash32 = s.to(Hash32) -# template to*(v: MDigest[256], _: type Hash32): Hash32 = -# Hash32(v.data) +template to*(v: MDigest[256], _: type Hash32): Hash32 = + Hash32(v.data) -# template to*(v: Hash32, _: type MDigest[256]): MDigest[256] = -# var tmp {.noinit.}: MDigest[256] -# assign(tmp.data, v.data) -# tmp +template to*(v: Hash32, _: type MDigest[256]): MDigest[256] = + var tmp {.noinit.}: MDigest[256] + assign(tmp.data, v.data) + tmp const emptyKeccak256* =