Skip to content

Commit 0729349

Browse files
rvaggrjan90
authored andcommitted
chore(deps): replace golang.org/x/crypto/sha3 with go-keccak, upgrade x/crypto (#13477)
Replace all usage of golang.org/x/crypto/sha3.NewLegacyKeccak256() with github.com/filecoin-project/go-keccak, which vendors the assembly-optimised Keccak permutation from x/crypto@v0.43.0. Starting with x/crypto v0.44.0, the upstream package removed its amd64 assembly in favor of Go's standard library crypto/sha3, which does not provide an assembly fast path for legacy Keccak functions. With the keccak dependency decoupled, upgrade golang.org/x/crypto to v0.47.0. Ref: filecoin-project/go-f3#1055 Ref: ethereum/go-ethereum#33323 Closes: #13476 Closes: #13443
1 parent 7f16d5f commit 0729349

8 files changed

Lines changed: 22 additions & 23 deletions

File tree

chain/types/ethtypes/eth_1559_transactions_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"testing"
99

1010
"github.com/stretchr/testify/require"
11-
"golang.org/x/crypto/sha3"
1211

1312
"github.com/filecoin-project/go-address"
1413
gocrypto "github.com/filecoin-project/go-crypto"
14+
"github.com/filecoin-project/go-keccak"
1515
actorstypes "github.com/filecoin-project/go-state-types/actors"
1616
builtintypes "github.com/filecoin-project/go-state-types/builtin"
1717
"github.com/filecoin-project/go-state-types/builtin/v10/evm"
@@ -163,7 +163,7 @@ func TestEcRecover(t *testing.T) {
163163
sig = append(sig, v)
164164
require.Equal(t, 65, len(sig))
165165

166-
sha := sha3.NewLegacyKeccak256()
166+
sha := keccak.NewLegacyKeccak256()
167167
sha.Write(msg)
168168
h := sha.Sum(nil)
169169

chain/types/ethtypes/eth_transactions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
mathbig "math/big"
99

1010
cbg "github.com/whyrusleeping/cbor-gen"
11-
"golang.org/x/crypto/sha3"
1211

1312
"github.com/filecoin-project/go-address"
1413
gocrypto "github.com/filecoin-project/go-crypto"
14+
"github.com/filecoin-project/go-keccak"
1515
"github.com/filecoin-project/go-state-types/abi"
1616
"github.com/filecoin-project/go-state-types/big"
1717
builtintypes "github.com/filecoin-project/go-state-types/builtin"
@@ -561,7 +561,7 @@ func sender(tx EthTransaction) (address.Address, error) {
561561
return address.Undef, fmt.Errorf("failed to get rlp unsigned msg: %w", err)
562562
}
563563

564-
hasher := sha3.NewLegacyKeccak256()
564+
hasher := keccak.NewLegacyKeccak256()
565565
hasher.Write(msg)
566566
hash := hasher.Sum(nil)
567567

chain/types/ethtypes/eth_types.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import (
1515
"github.com/multiformats/go-multihash"
1616
mh "github.com/multiformats/go-multihash"
1717
"github.com/multiformats/go-varint"
18-
"golang.org/x/crypto/sha3"
1918
"golang.org/x/xerrors"
2019

2120
"github.com/filecoin-project/go-address"
21+
"github.com/filecoin-project/go-keccak"
2222
"github.com/filecoin-project/go-state-types/abi"
2323
"github.com/filecoin-project/go-state-types/big"
2424
builtintypes "github.com/filecoin-project/go-state-types/builtin"
@@ -425,7 +425,7 @@ func EthAddressFromPubKey(pubk []byte) ([]byte, error) {
425425
pubk = pubk[1:]
426426

427427
// Calculate the Ethereum address based on the keccak hash of the pubkey.
428-
hasher := sha3.NewLegacyKeccak256()
428+
hasher := keccak.NewLegacyKeccak256()
429429
hasher.Write(pubk)
430430
ethAddr := hasher.Sum(nil)[12:]
431431
return ethAddr, nil
@@ -643,7 +643,7 @@ func ParseEthHash(s string) (EthHash, error) {
643643
}
644644

645645
func EthHashFromTxBytes(b []byte) EthHash {
646-
hasher := sha3.NewLegacyKeccak256()
646+
hasher := keccak.NewLegacyKeccak256()
647647
hasher.Write(b)
648648
hash := hasher.Sum(nil)
649649

@@ -653,7 +653,7 @@ func EthHashFromTxBytes(b []byte) EthHash {
653653
}
654654

655655
func EthBloomSet(f EthBytes, data []byte) {
656-
hasher := sha3.NewLegacyKeccak256()
656+
hasher := keccak.NewLegacyKeccak256()
657657
hasher.Write(data)
658658
hash := hasher.Sum(nil)
659659

@@ -908,7 +908,7 @@ type EthSubscriptionResponse struct {
908908
}
909909

910910
func GetContractEthAddressFromCode(sender EthAddress, salt [32]byte, initcode []byte) (EthAddress, error) {
911-
hasher := sha3.NewLegacyKeccak256()
911+
hasher := keccak.NewLegacyKeccak256()
912912
hasher.Write(initcode)
913913
inithash := hasher.Sum(nil)
914914

itests/eth_deploy_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"time"
1212

1313
"github.com/stretchr/testify/require"
14-
"golang.org/x/crypto/sha3"
1514

15+
"github.com/filecoin-project/go-keccak"
1616
"github.com/filecoin-project/go-state-types/big"
1717
"github.com/filecoin-project/go-state-types/manifest"
1818
gstStore "github.com/filecoin-project/go-state-types/store"
@@ -273,7 +273,7 @@ func TestDeployment(t *testing.T) {
273273
byteCodeHashChain, err := evmSt.GetBytecodeHash()
274274
require.NoError(t, err)
275275

276-
hasher := sha3.NewLegacyKeccak256()
276+
hasher := keccak.NewLegacyKeccak256()
277277
hasher.Write(byteCode.RawData())
278278
byteCodeHash := hasher.Sum(nil)
279279
require.Equal(t, byteCodeHashChain[:], byteCodeHash)

itests/fevm_address_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"testing"
88

99
"github.com/stretchr/testify/require"
10-
"golang.org/x/crypto/sha3"
1110

1211
"github.com/filecoin-project/go-address"
12+
"github.com/filecoin-project/go-keccak"
1313
"github.com/filecoin-project/go-state-types/abi"
1414
"github.com/filecoin-project/go-state-types/big"
1515
builtintypes "github.com/filecoin-project/go-state-types/builtin"
@@ -24,7 +24,7 @@ import (
2424
func effectiveEthAddressForCreate(t *testing.T, sender address.Address) ethtypes.EthAddress {
2525
switch sender.Protocol() {
2626
case address.SECP256K1, address.BLS:
27-
hasher := sha3.NewLegacyKeccak256()
27+
hasher := keccak.NewLegacyKeccak256()
2828
hasher.Write(sender.Bytes())
2929
addr, err := ethtypes.CastEthAddress(hasher.Sum(nil)[12:])
3030
require.NoError(t, err)

itests/kit/evm.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import (
1616
"github.com/multiformats/go-varint"
1717
"github.com/stretchr/testify/require"
1818
cbg "github.com/whyrusleeping/cbor-gen"
19-
"golang.org/x/crypto/sha3"
2019
"golang.org/x/xerrors"
2120

2221
"github.com/filecoin-project/go-address"
2322
amt4 "github.com/filecoin-project/go-amt-ipld/v4"
23+
"github.com/filecoin-project/go-keccak"
2424
"github.com/filecoin-project/go-state-types/abi"
2525
"github.com/filecoin-project/go-state-types/big"
2626
builtintypes "github.com/filecoin-project/go-state-types/builtin"
@@ -286,7 +286,7 @@ func (e *EVM) ComputeContractAddress(deployer ethtypes.EthAddress, nonce uint64)
286286
})
287287
require.NoError(e.t, err)
288288

289-
hasher := sha3.NewLegacyKeccak256()
289+
hasher := keccak.NewLegacyKeccak256()
290290
hasher.Write(encoded)
291291
return *(*ethtypes.EthAddress)(hasher.Sum(nil)[12:])
292292
}
@@ -353,7 +353,7 @@ func (e *EVM) WaitTransaction(ctx context.Context, hash ethtypes.EthHash) (*etht
353353

354354
// CalcFuncSignature returns the first 4 bytes of the hash of the function name and types
355355
func CalcFuncSignature(funcName string) []byte {
356-
hasher := sha3.NewLegacyKeccak256()
356+
hasher := keccak.NewLegacyKeccak256()
357357
hasher.Write([]byte(funcName))
358358
hash := hasher.Sum(nil)
359359
return hash[:4]

itests/kit/solidity.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package kit
22

33
import (
4-
"golang.org/x/crypto/sha3"
4+
"github.com/filecoin-project/go-keccak"
55

66
"github.com/filecoin-project/lotus/chain/types/ethtypes"
77
)
88

99
func EthTopicHash(sig string) ethtypes.EthHash {
10-
hasher := sha3.NewLegacyKeccak256()
10+
hasher := keccak.NewLegacyKeccak256()
1111
hasher.Write([]byte(sig))
1212
var hash ethtypes.EthHash
1313
copy(hash[:], hasher.Sum(nil))
1414
return hash
1515
}
1616

1717
func EthFunctionHash(sig string) []byte {
18-
hasher := sha3.NewLegacyKeccak256()
18+
hasher := keccak.NewLegacyKeccak256()
1919
hasher.Write([]byte(sig))
2020
return hasher.Sum(nil)[:4]
2121
}

lib/sigs/delegated/init.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ package delegated
33
import (
44
"fmt"
55

6-
"golang.org/x/crypto/sha3"
7-
86
"github.com/filecoin-project/go-address"
97
gocrypto "github.com/filecoin-project/go-crypto"
8+
"github.com/filecoin-project/go-keccak"
109
"github.com/filecoin-project/go-state-types/builtin"
1110
crypto1 "github.com/filecoin-project/go-state-types/crypto"
1211

@@ -28,7 +27,7 @@ func (delegatedSigner) ToPublic(pk []byte) ([]byte, error) {
2827
}
2928

3029
func (s delegatedSigner) Sign(pk []byte, msg []byte) ([]byte, error) {
31-
hasher := sha3.NewLegacyKeccak256()
30+
hasher := keccak.NewLegacyKeccak256()
3231
hasher.Write(msg)
3332
hashSum := hasher.Sum(nil)
3433
sig, err := gocrypto.Sign(pk, hashSum)
@@ -40,7 +39,7 @@ func (s delegatedSigner) Sign(pk []byte, msg []byte) ([]byte, error) {
4039
}
4140

4241
func (delegatedSigner) Verify(sig []byte, a address.Address, msg []byte) error {
43-
hasher := sha3.NewLegacyKeccak256()
42+
hasher := keccak.NewLegacyKeccak256()
4443
hasher.Write(msg)
4544
hash := hasher.Sum(nil)
4645

0 commit comments

Comments
 (0)