Skip to content

Commit 84408c9

Browse files
authored
Improves keccak256 perf (#3866)
* Improves keccak256 perf Compared to nimcrypto, this keccak impl from tuned Boring SSL only takes average 24 units of time, and nimcrypto takes 43 units of time. In addition to that, two hash function specialized for Address and Hash32 also further improves hashing speed. Address/20 bytes input length: takes 5 units of time compared to general hash 9 units of time. Hash32/32 bytes input length: takes 2 units of time compared to general hash 4 units of time. (Data size alignment improves hash time significantly) * Bump nim-eth * Fix tests * Oops
1 parent 1bf5ca3 commit 84408c9

File tree

9 files changed

+33
-27
lines changed

9 files changed

+33
-27
lines changed

execution_chain/networking/discoveryv4.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import
1313
std/[times, net],
1414
chronos,
1515
stint,
16-
nimcrypto/keccak,
1716
chronicles,
1817
stew/objects,
1918
results,
2019
eth/rlp,
20+
eth/keccak/keccak,
2121
eth/common/keys,
2222
eth/enode/enode,
2323
./discoveryv4/kademlia
@@ -54,7 +54,7 @@ type
5454

5555
DiscResult*[T] = Result[T, cstring]
5656

57-
keccak256 = keccak.keccak256
57+
Keccak256 = keccak.Keccak256
5858

5959
UnpackedMsg = object
6060
cmdId: CommandId
@@ -90,13 +90,13 @@ proc pack(cmdId: CommandId, payload: openArray[byte], pk: PrivateKey): seq[byte]
9090
result[MAC_SIZE ..< MAC_SIZE + SIG_SIZE] =
9191
pk.sign(result.toOpenArray(HEAD_SIZE, result.high)).toRaw()
9292
result[0 ..< MAC_SIZE] =
93-
keccak256.digest(result.toOpenArray(MAC_SIZE, result.high)).data
93+
Keccak256.digest(result.toOpenArray(MAC_SIZE, result.high)).data
9494

9595
proc validateMsgHash(msg: openArray[byte]): DiscResult[MDigest[256]] =
9696
if msg.len > HEAD_SIZE:
9797
var ret: MDigest[256]
9898
ret.data[0 .. ^1] = msg.toOpenArray(0, ret.data.high)
99-
if ret == keccak256.digest(msg.toOpenArray(MAC_SIZE, msg.high)):
99+
if ret == Keccak256.digest(msg.toOpenArray(MAC_SIZE, msg.high)):
100100
ok(ret)
101101
else:
102102
err("disc: invalid message hash")

execution_chain/networking/discoveryv4/kademlia.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import
1212
std/[tables, hashes, times, algorithm, sets, sequtils],
13-
chronos, chronicles, stint, nimcrypto/keccak, metrics, results,
13+
chronos, chronicles, stint, eth/keccak/keccak, metrics, results,
1414
eth/common/keys, eth/p2p/discoveryv5/random2,
1515
eth/enode/enode
1616

@@ -70,7 +70,7 @@ const
7070
proc len(r: RoutingTable): int
7171

7272
proc toNodeId*(pk: PublicKey): NodeId =
73-
readUintBE[256](keccak256.digest(pk.toRaw()).data)
73+
readUintBE[256](Keccak256.digest(pk.toRaw()).data)
7474

7575
proc newNode*(pk: PublicKey, address: Address): Node =
7676
result.new()

execution_chain/networking/rlpx/auth.nim

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@
1515
{.push raises: [].}
1616

1717
import
18-
nimcrypto/[rijndael, keccak, utils],
18+
nimcrypto/[rijndael, utils],
1919
stew/[arrayops, byteutils, endians2, objects],
2020
results,
2121
eth/rlp,
22+
eth/keccak/keccak,
2223
eth/common/keys,
2324
./ecies
2425

2526
export results
2627

27-
type keccak256 = keccak.keccak256
28-
2928
const
3029
# Auth message sizes
3130
MsgLenLenEIP8* = 2
@@ -66,6 +65,8 @@ const
6665
## ack-vsn = 4
6766

6867
type
68+
Keccak256 = keccak.Keccak256
69+
6970
Nonce* = array[KeyLength, byte]
7071

7172
HandshakeFlag* = enum
@@ -95,8 +96,8 @@ type
9596
ConnectionSecret* = object
9697
aesKey*: array[aes256.sizeKey, byte]
9798
macKey*: array[KeyLength, byte]
98-
egressMac*: keccak256
99-
ingressMac*: keccak256
99+
egressMac*: Keccak256
100+
ingressMac*: Keccak256
100101

101102
AuthResult*[T] = Result[T, AuthError]
102103

@@ -343,8 +344,8 @@ proc getSecrets*(
343344
## Derive secrets from handshake `h` using encrypted AuthMessage `authmsg` and
344345
## encrypted AckMessage `ackmsg`.
345346
var
346-
ctx0: keccak256
347-
ctx1: keccak256
347+
ctx0: Keccak256
348+
ctx1: Keccak256
348349
mac1: MDigest[256]
349350
secret: ConnectionSecret
350351

execution_chain/networking/rlpx/rlpxcrypt.nim

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
{.push raises: [].}
1313

1414
import
15-
nimcrypto/[bcmode, keccak, rijndael, utils], results
15+
nimcrypto/[bcmode, rijndael, utils], results,
16+
eth/keccak/keccak
1617
from auth import ConnectionSecret
1718

1819
export results
@@ -23,13 +24,15 @@ const
2324
maxUInt24 = (not uint32(0)) shl 8
2425

2526
type
27+
Keccak256 = keccak.Keccak256
28+
2629
SecretState* = object
2730
## Object represents current encryption/decryption context.
2831
aesenc*: CTR[aes256]
2932
aesdec*: CTR[aes256]
3033
macenc*: ECB[aes256]
31-
emac*: keccak256
32-
imac*: keccak256
34+
emac*: Keccak256
35+
imac*: Keccak256
3336

3437
RlpxError* = enum
3538
IncorrectMac = "rlpx: MAC verification failed"
@@ -90,7 +93,7 @@ proc encrypt*(c: var SecretState, header: openArray[byte],
9093
## `frame` must not be zero length.
9194
## `output` must be at least `encryptedLength(len(frame))` length.
9295
var
93-
tmpmac: keccak256
96+
tmpmac: Keccak256
9497
aes: array[RlpHeaderLength, byte]
9598
let length = encryptedLength(len(frame))
9699
let frameLength = roundup16(len(frame))
@@ -166,7 +169,7 @@ proc decryptHeader*(c: var SecretState, data: openArray[byte]): RlpxResult[RlpxH
166169
## `header` must be at least `RlpHeaderLength + RlpMacLength` length.
167170

168171
var
169-
tmpmac: keccak256
172+
tmpmac: Keccak256
170173
aes: array[RlpHeaderLength, byte]
171174

172175
if len(data) < RlpHeaderLength + RlpMacLength:
@@ -204,7 +207,7 @@ proc decryptBody*(c: var SecretState, data: openArray[byte], bodysize: int,
204207
##
205208
## On success completion `outlen` will hold actual size of decrypted body.
206209
var
207-
tmpmac: keccak256
210+
tmpmac: Keccak256
208211
aes: array[RlpHeaderLength, byte]
209212
let rsize = roundup16(bodysize)
210213
if len(data) < rsize + RlpMacLength:

tests/networking/fuzzing/discoveryv4/fuzz.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
import
1111
std/net,
12-
testutils/fuzzing, chronicles, nimcrypto/keccak,
13-
eth/[common/keys],
12+
testutils/fuzzing, chronicles,
13+
eth/[common/keys, keccak/keccak],
1414
results,
1515
../../../../execution_chain/networking/discoveryv4,
1616
../../p2p_test_helper
@@ -22,7 +22,7 @@ proc packData(payload: openArray[byte], pk: PrivateKey): seq[byte] =
2222
let
2323
payloadSeq = @payload
2424
signature = @(pk.sign(payload).toRaw())
25-
msgHash = keccak256.digest(signature & payloadSeq)
25+
msgHash = Keccak256.digest(signature & payloadSeq)
2626
result = @(msgHash.data) & signature & payloadSeq
2727

2828
init:

tests/networking/test_auth.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
import
1313
unittest2,
14-
nimcrypto/[utils, keccak],
14+
nimcrypto/utils,
15+
eth/keccak/keccak,
1516
eth/common/keys,
1617
../../execution_chain/networking/rlpx/auth
1718

tests/networking/test_crypt.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
import
1313
unittest2,
14-
nimcrypto/[utils, keccak, sysrand],
14+
nimcrypto/[utils, sysrand],
15+
eth/keccak/keccak,
1516
eth/common/keys,
1617
../../execution_chain/networking/rlpx/[auth, rlpxcrypt]
1718

tests/networking/test_discoveryv4.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import
1313
std/sequtils,
1414
chronos,
1515
stew/byteutils,
16-
nimcrypto/keccak,
1716
testutils/unittests,
17+
eth/keccak/keccak,
1818
eth/common/keys,
1919
./stubloglevel,
2020
../../execution_chain/networking/discoveryv4
@@ -36,7 +36,7 @@ proc packData(payload: openArray[byte], pk: PrivateKey): seq[byte] =
3636
let
3737
payloadSeq = @payload
3838
signature = @(pk.sign(payload).toRaw())
39-
msgHash = keccak256.digest(signature & payloadSeq)
39+
msgHash = Keccak256.digest(signature & payloadSeq)
4040
result = @(msgHash.data) & signature & payloadSeq
4141

4242
proc nodeIdInNodes(id: NodeId, nodes: openArray[Node]): bool =

0 commit comments

Comments
 (0)