Skip to content

Commit 630c702

Browse files
fjllightclient
authored andcommitted
refactor(crypto): vendor in golang.org/x/crypto/sha3 ethereum#33323
The upstream libray has removed the assembly-based implementation of keccak. We need to maintain our own library to avoid a peformance regression. --------- Co-authored-by: lightclient <lightclient@protonmail.com>
1 parent 50210d9 commit 630c702

39 files changed

+6466
-96
lines changed

XDCx/tradingstate/trade.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"time"
66

77
"github.com/XinFinOrg/XDPoSChain/common"
8+
"github.com/XinFinOrg/XDPoSChain/crypto/keccak"
89
"github.com/globalsign/mgo/bson"
9-
"golang.org/x/crypto/sha3"
1010
)
1111

1212
const (
@@ -136,7 +136,7 @@ func (t *Trade) SetBSON(raw bson.Raw) error {
136136
// The OrderHash, Amount, Taker and TradeNonce attributes must be
137137
// set before attempting to compute the trade orderBookHash
138138
func (t *Trade) ComputeHash() common.Hash {
139-
sha := sha3.NewLegacyKeccak256()
139+
sha := keccak.NewLegacyKeccak256()
140140
sha.Write(t.MakerOrderHash.Bytes())
141141
sha.Write(t.TakerOrderHash.Bytes())
142142
return common.BytesToHash(sha.Sum(nil))

XDCxlending/lendingstate/lendingitem.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"github.com/XinFinOrg/XDPoSChain/common"
1111
"github.com/XinFinOrg/XDPoSChain/core/state"
1212
"github.com/XinFinOrg/XDPoSChain/core/types"
13+
"github.com/XinFinOrg/XDPoSChain/crypto/keccak"
1314
"github.com/globalsign/mgo/bson"
14-
"golang.org/x/crypto/sha3"
1515
)
1616

1717
const (
@@ -308,7 +308,7 @@ func (l *LendingItem) VerifyLendingStatus() error {
308308
}
309309

310310
func (l *LendingItem) ComputeHash() common.Hash {
311-
sha := sha3.NewLegacyKeccak256()
311+
sha := keccak.NewLegacyKeccak256()
312312
if l.Status == LendingStatusNew {
313313
sha.Write(l.Relayer.Bytes())
314314
sha.Write(l.UserAddress.Bytes())

XDCxlending/lendingstate/lendingitem_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"github.com/XinFinOrg/XDPoSChain/core/state"
1313
"github.com/XinFinOrg/XDPoSChain/core/types"
1414
"github.com/XinFinOrg/XDPoSChain/crypto"
15+
"github.com/XinFinOrg/XDPoSChain/crypto/keccak"
1516
"github.com/XinFinOrg/XDPoSChain/rpc"
16-
"golang.org/x/crypto/sha3"
1717
)
1818

1919
func TestLendingItem_VerifyLendingSide(t *testing.T) {
@@ -567,7 +567,7 @@ func sendOrder(nonce uint64) {
567567
}
568568

569569
func computeHash(l *LendingOrderMsg) common.Hash {
570-
sha := sha3.NewLegacyKeccak256()
570+
sha := keccak.NewLegacyKeccak256()
571571
if l.Status == LendingStatusCancelled {
572572
sha.Write(l.Hash.Bytes())
573573
sha.Write(common.BigToHash(big.NewInt(int64(l.AccountNonce))).Bytes())

XDCxlending/lendingstate/trade.go

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

99
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
1010
"github.com/XinFinOrg/XDPoSChain/common"
11+
"github.com/XinFinOrg/XDPoSChain/crypto/keccak"
1112
"github.com/globalsign/mgo/bson"
12-
"golang.org/x/crypto/sha3"
1313
)
1414

1515
const (
@@ -183,7 +183,7 @@ func (t *LendingTrade) SetBSON(raw bson.Raw) error {
183183
}
184184

185185
func (t *LendingTrade) ComputeHash() common.Hash {
186-
sha := sha3.NewLegacyKeccak256()
186+
sha := keccak.NewLegacyKeccak256()
187187
sha.Write(t.InvestingOrderHash.Bytes())
188188
sha.Write(t.BorrowingOrderHash.Bytes())
189189
return common.BytesToHash(sha.Sum(nil))

accounts/accounts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
ethereum "github.com/XinFinOrg/XDPoSChain"
2525
"github.com/XinFinOrg/XDPoSChain/common"
2626
"github.com/XinFinOrg/XDPoSChain/core/types"
27+
"github.com/XinFinOrg/XDPoSChain/crypto/keccak"
2728
"github.com/XinFinOrg/XDPoSChain/event"
28-
"golang.org/x/crypto/sha3"
2929
)
3030

3131
// Account represents an Ethereum account located at a specific location defined
@@ -209,7 +209,7 @@ func TextHash(data []byte) []byte {
209209
// This gives context to the signed message and prevents signing of transactions.
210210
func TextAndHash(data []byte) ([]byte, string) {
211211
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
212-
hasher := sha3.NewLegacyKeccak256()
212+
hasher := keccak.NewLegacyKeccak256()
213213
hasher.Write([]byte(msg))
214214
return hasher.Sum(nil), msg
215215
}

bmt/bmt_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"testing"
3030
"time"
3131

32-
"golang.org/x/crypto/sha3"
32+
"github.com/XinFinOrg/XDPoSChain/crypto/keccak"
3333
)
3434

3535
const (
@@ -39,7 +39,7 @@ const (
3939
// TestRefHasher tests that the RefHasher computes the expected BMT hash for
4040
// all data lengths between 0 and 256 bytes
4141
func TestRefHasher(t *testing.T) {
42-
hashFunc := sha3.NewLegacyKeccak256
42+
hashFunc := keccak.NewLegacyKeccak256
4343

4444
sha3 := func(data ...[]byte) []byte {
4545
h := hashFunc()
@@ -212,7 +212,7 @@ func testHasher(f func(BaseHasher, []byte, int, int) error) error {
212212
tdata := testDataReader(4128)
213213
data := make([]byte, 4128)
214214
tdata.Read(data)
215-
hasher := sha3.NewLegacyKeccak256
215+
hasher := keccak.NewLegacyKeccak256
216216
size := hasher().Size()
217217
counts := []int{1, 2, 3, 4, 5, 8, 16, 32, 64, 128}
218218

@@ -239,7 +239,7 @@ func TestHasherReuseWithRelease(t *testing.T) {
239239
}
240240

241241
func testHasherReuse(i int, t *testing.T) {
242-
hasher := sha3.NewLegacyKeccak256
242+
hasher := keccak.NewLegacyKeccak256
243243
pool := NewTreePool(hasher, 128, i)
244244
defer pool.Drain(0)
245245
bmt := New(pool)
@@ -258,7 +258,7 @@ func testHasherReuse(i int, t *testing.T) {
258258
}
259259

260260
func TestHasherConcurrency(t *testing.T) {
261-
hasher := sha3.NewLegacyKeccak256
261+
hasher := keccak.NewLegacyKeccak256
262262
pool := NewTreePool(hasher, 128, maxproccnt)
263263
defer pool.Drain(0)
264264
wg := sync.WaitGroup{}
@@ -377,7 +377,7 @@ func benchmarkBMTBaseline(n int, t *testing.B) {
377377
tdata := testDataReader(64)
378378
data := make([]byte, 64)
379379
tdata.Read(data)
380-
hasher := sha3.NewLegacyKeccak256
380+
hasher := keccak.NewLegacyKeccak256
381381

382382
t.ReportAllocs()
383383
t.ResetTimer()
@@ -405,7 +405,7 @@ func benchmarkHasher(n int, t *testing.B) {
405405
tdata.Read(data)
406406

407407
size := 1
408-
hasher := sha3.NewLegacyKeccak256
408+
hasher := keccak.NewLegacyKeccak256
409409
segmentCount := 128
410410
pool := NewTreePool(hasher, segmentCount, size)
411411
bmt := New(pool)
@@ -424,7 +424,7 @@ func benchmarkHasherReuse(poolsize, n int, t *testing.B) {
424424
data := make([]byte, n)
425425
tdata.Read(data)
426426

427-
hasher := sha3.NewLegacyKeccak256
427+
hasher := keccak.NewLegacyKeccak256
428428
segmentCount := 128
429429
pool := NewTreePool(hasher, segmentCount, poolsize)
430430
cycles := 200
@@ -449,7 +449,7 @@ func benchmarkSHA3(n int, t *testing.B) {
449449
data := make([]byte, n)
450450
tdata := testDataReader(n)
451451
tdata.Read(data)
452-
hasher := sha3.NewLegacyKeccak256
452+
hasher := keccak.NewLegacyKeccak256
453453
h := hasher()
454454

455455
t.ReportAllocs()
@@ -465,7 +465,7 @@ func benchmarkRefHasher(n int, t *testing.B) {
465465
data := make([]byte, n)
466466
tdata := testDataReader(n)
467467
tdata.Read(data)
468-
hasher := sha3.NewLegacyKeccak256
468+
hasher := keccak.NewLegacyKeccak256
469469
rbmt := NewRefHasher(hasher, 128)
470470

471471
t.ReportAllocs()

common/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"strconv"
2828

2929
"github.com/XinFinOrg/XDPoSChain/common/hexutil"
30-
"golang.org/x/crypto/sha3"
30+
"github.com/XinFinOrg/XDPoSChain/crypto/keccak"
3131
)
3232

3333
const (
@@ -277,7 +277,7 @@ func (a *Address) checksumHex() []byte {
277277
buf := a.hex()
278278

279279
// compute checksum
280-
sha := sha3.NewLegacyKeccak256()
280+
sha := keccak.NewLegacyKeccak256()
281281
sha.Write(buf[2:])
282282
hash := sha.Sum(nil)
283283
for i := 2; i < len(buf); i++ {

consensus/XDPoS/engines/engine_v1/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
88
"github.com/XinFinOrg/XDPoSChain/core/types"
99
"github.com/XinFinOrg/XDPoSChain/crypto"
10+
"github.com/XinFinOrg/XDPoSChain/crypto/keccak"
1011
"github.com/XinFinOrg/XDPoSChain/params"
1112
"github.com/XinFinOrg/XDPoSChain/rlp"
12-
"golang.org/x/crypto/sha3"
1313
)
1414

1515
// Get masternodes address from checkpoint Header.
@@ -61,7 +61,7 @@ func getM1M2(masternodes []common.Address, validators []int64, currentHeader *ty
6161
}
6262

6363
func sigHash(header *types.Header) (hash common.Hash) {
64-
hasher := sha3.NewLegacyKeccak256()
64+
hasher := keccak.NewLegacyKeccak256()
6565

6666
enc := []interface{}{
6767
header.ParentHash,

consensus/XDPoS/engines/engine_v2/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import (
1111
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
1212
"github.com/XinFinOrg/XDPoSChain/core/types"
1313
"github.com/XinFinOrg/XDPoSChain/crypto"
14+
"github.com/XinFinOrg/XDPoSChain/crypto/keccak"
1415
"github.com/XinFinOrg/XDPoSChain/log"
1516
"github.com/XinFinOrg/XDPoSChain/rlp"
16-
"golang.org/x/crypto/sha3"
1717
)
1818

1919
func sigHash(header *types.Header) (hash common.Hash) {
20-
hasher := sha3.NewLegacyKeccak256()
20+
hasher := keccak.NewLegacyKeccak256()
2121

2222
enc := []interface{}{
2323
header.ParentHash,

consensus/clique/clique.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ import (
3636
"github.com/XinFinOrg/XDPoSChain/core/types"
3737
"github.com/XinFinOrg/XDPoSChain/core/vm"
3838
"github.com/XinFinOrg/XDPoSChain/crypto"
39+
"github.com/XinFinOrg/XDPoSChain/crypto/keccak"
3940
"github.com/XinFinOrg/XDPoSChain/ethdb"
4041
"github.com/XinFinOrg/XDPoSChain/log"
4142
"github.com/XinFinOrg/XDPoSChain/params"
4243
"github.com/XinFinOrg/XDPoSChain/rlp"
4344
"github.com/XinFinOrg/XDPoSChain/rpc"
4445
"github.com/XinFinOrg/XDPoSChain/trie"
45-
"golang.org/x/crypto/sha3"
4646
)
4747

4848
const (
@@ -146,7 +146,7 @@ type SignerFn func(accounts.Account, []byte) ([]byte, error)
146146
// panics. This is done to avoid accidentally using both forms (signature present
147147
// or not), which could be abused to produce different hashes for the same header.
148148
func sigHash(header *types.Header) (hash common.Hash) {
149-
hasher := sha3.NewLegacyKeccak256()
149+
hasher := keccak.NewLegacyKeccak256()
150150

151151
enc := []interface{}{
152152
header.ParentHash,

0 commit comments

Comments
 (0)