Skip to content

Commit 2d97756

Browse files
committed
crypto/keccak: vendor in golang.org/x/crypto/sha3
The upstream libray has removed the assembly-based implementation of keccak. We need to maintain our own library to avoid a peformance regression.
1 parent 28376ae commit 2d97756

File tree

10 files changed

+6374
-3
lines changed

10 files changed

+6374
-3
lines changed

crypto/keccak.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ import (
2222
"sync"
2323

2424
"github.com/ethereum/go-ethereum/common"
25-
"golang.org/x/crypto/sha3"
25+
"github.com/ethereum/go-ethereum/crypto/keccak"
2626
)
2727

2828
// NewKeccakState creates a new KeccakState
2929
func NewKeccakState() KeccakState {
30-
return sha3.NewLegacyKeccak256().(KeccakState)
30+
return keccak.NewLegacyKeccak256().(KeccakState)
3131
}
3232

3333
var hasherPool = sync.Pool{
3434
New: func() any {
35-
return sha3.NewLegacyKeccak256().(KeccakState)
35+
return keccak.NewLegacyKeccak256().(KeccakState)
3636
},
3737
}
3838

crypto/keccak/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This is a vendored and modified copy of golang.org/x/crypto/sha3, with an assembly
2+
implementation of keccak256.
3+
4+
Ethereum uses a 'legacy' variant of Keccak, which was defined before it became SHA3. As
5+
such, we cannot use the standard library crypto/sha3 package.

crypto/keccak/hashes.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2014 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package keccak
6+
7+
// This file provides functions for creating instances of the SHA-3
8+
// and SHAKE hash functions, as well as utility functions for hashing
9+
// bytes.
10+
11+
import (
12+
"hash"
13+
)
14+
15+
const (
16+
dsbyteSHA3 = 0b00000110
17+
dsbyteKeccak = 0b00000001
18+
dsbyteShake = 0b00011111
19+
dsbyteCShake = 0b00000100
20+
21+
// rateK[c] is the rate in bytes for Keccak[c] where c is the capacity in
22+
// bits. Given the sponge size is 1600 bits, the rate is 1600 - c bits.
23+
rateK256 = (1600 - 256) / 8
24+
rateK448 = (1600 - 448) / 8
25+
rateK512 = (1600 - 512) / 8
26+
rateK768 = (1600 - 768) / 8
27+
rateK1024 = (1600 - 1024) / 8
28+
)
29+
30+
// NewLegacyKeccak256 creates a new Keccak-256 hash.
31+
//
32+
// Only use this function if you require compatibility with an existing cryptosystem
33+
// that uses non-standard padding. All other users should use New256 instead.
34+
func NewLegacyKeccak256() hash.Hash {
35+
return &state{rate: rateK512, outputLen: 32, dsbyte: dsbyteKeccak}
36+
}
37+
38+
// NewLegacyKeccak512 creates a new Keccak-512 hash.
39+
//
40+
// Only use this function if you require compatibility with an existing cryptosystem
41+
// that uses non-standard padding. All other users should use New512 instead.
42+
func NewLegacyKeccak512() hash.Hash {
43+
return &state{rate: rateK1024, outputLen: 64, dsbyte: dsbyteKeccak}
44+
}

crypto/keccak/keccak.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
package keccak

0 commit comments

Comments
 (0)