Skip to content

Commit d444487

Browse files
authored
Merge pull request #751 from gzliudan/upgrade-crypto
upgrade package crypto and support KZG cryptography
2 parents 0dbe819 + 3fbbc9d commit d444487

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+5593
-2080
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/sha3"
98
"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.NewKeccak256()
139+
sha := sha3.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/sha3"
1413
"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.NewKeccak256()
311+
sha := sha3.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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
1313
"github.com/XinFinOrg/XDPoSChain/core/state"
1414
"github.com/XinFinOrg/XDPoSChain/crypto"
15-
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
1615
"github.com/XinFinOrg/XDPoSChain/rpc"
16+
"golang.org/x/crypto/sha3"
1717
)
1818

1919
func TestLendingItem_VerifyLendingSide(t *testing.T) {
@@ -568,9 +568,8 @@ func sendOrder(nonce uint64) {
568568
}
569569

570570
func computeHash(l *LendingOrderMsg) common.Hash {
571-
sha := sha3.NewKeccak256()
571+
sha := sha3.NewLegacyKeccak256()
572572
if l.Status == LendingStatusCancelled {
573-
sha := sha3.NewKeccak256()
574573
sha.Write(l.Hash.Bytes())
575574
sha.Write(common.BigToHash(big.NewInt(int64(l.AccountNonce))).Bytes())
576575
sha.Write(l.UserAddress.Bytes())
@@ -593,5 +592,4 @@ func computeHash(l *LendingOrderMsg) common.Hash {
593592
sha.Write(common.BigToHash(big.NewInt(int64(l.AccountNonce))).Bytes())
594593
}
595594
return common.BytesToHash(sha.Sum(nil))
596-
597595
}

XDCxlending/lendingstate/trade.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package lendingstate
22

33
import (
44
"fmt"
5-
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
6-
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
75
"math/big"
86
"strconv"
97
"time"
108

9+
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
1110
"github.com/XinFinOrg/XDPoSChain/common"
1211
"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.NewKeccak256()
186+
sha := sha3.NewLegacyKeccak256()
187187
sha.Write(t.InvestingOrderHash.Bytes())
188188
sha.Write(t.BorrowingOrderHash.Bytes())
189189
return common.BytesToHash(sha.Sum(nil))

accounts/abi/bind/backends/simulated.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"github.com/XinFinOrg/XDPoSChain/core/state"
4343
"github.com/XinFinOrg/XDPoSChain/core/types"
4444
"github.com/XinFinOrg/XDPoSChain/core/vm"
45+
"github.com/XinFinOrg/XDPoSChain/crypto"
4546
"github.com/XinFinOrg/XDPoSChain/eth/filters"
4647
"github.com/XinFinOrg/XDPoSChain/ethdb"
4748
"github.com/XinFinOrg/XDPoSChain/event"
@@ -101,7 +102,7 @@ func NewXDCSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64, chainConfi
101102
GasLimit: gasLimit, // need this big, support initial smart contract
102103
Config: chainConfig,
103104
Alloc: alloc,
104-
ExtraData: append(make([]byte, 32), make([]byte, 65)...),
105+
ExtraData: append(make([]byte, 32), make([]byte, crypto.SignatureLength)...),
105106
}
106107
genesis.MustCommit(database)
107108
consensus := XDPoS.NewFaker(database, chainConfig)

accounts/usbwallet/ledger.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/XinFinOrg/XDPoSChain/common"
3333
"github.com/XinFinOrg/XDPoSChain/common/hexutil"
3434
"github.com/XinFinOrg/XDPoSChain/core/types"
35+
"github.com/XinFinOrg/XDPoSChain/crypto"
3536
"github.com/XinFinOrg/XDPoSChain/log"
3637
"github.com/XinFinOrg/XDPoSChain/rlp"
3738
)
@@ -341,7 +342,7 @@ func (w *ledgerDriver) ledgerSign(derivationPath []uint32, tx *types.Transaction
341342
op = ledgerP1ContTransactionData
342343
}
343344
// Extract the Ethereum signature and do a sanity validation
344-
if len(reply) != 65 {
345+
if len(reply) != crypto.SignatureLength {
345346
return common.Address{}, nil, errors.New("reply lacks signature")
346347
}
347348
signature := append(reply[1:], reply[0])
@@ -352,7 +353,7 @@ func (w *ledgerDriver) ledgerSign(derivationPath []uint32, tx *types.Transaction
352353
signer = new(types.HomesteadSigner)
353354
} else {
354355
signer = types.NewEIP155Signer(chainID)
355-
signature[64] = signature[64] - byte(chainID.Uint64()*2+35)
356+
signature[crypto.RecoveryIDOffset] = signature[crypto.RecoveryIDOffset] - byte(chainID.Uint64()*2+35)
356357
}
357358
signed, err := tx.WithSignature(signer, signature)
358359
if err != nil {

accounts/usbwallet/trezor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/XinFinOrg/XDPoSChain/common"
3333
"github.com/XinFinOrg/XDPoSChain/common/hexutil"
3434
"github.com/XinFinOrg/XDPoSChain/core/types"
35+
"github.com/XinFinOrg/XDPoSChain/crypto"
3536
"github.com/XinFinOrg/XDPoSChain/log"
3637
"github.com/golang/protobuf/proto"
3738
)
@@ -222,7 +223,7 @@ func (w *trezorDriver) trezorSign(derivationPath []uint32, tx *types.Transaction
222223
} else {
223224
// Trezor backend does not support typed transactions yet.
224225
signer = types.NewEIP155Signer(chainID)
225-
signature[64] = signature[64] - byte(chainID.Uint64()*2+35)
226+
signature[crypto.RecoveryIDOffset] = signature[crypto.RecoveryIDOffset] - byte(chainID.Uint64()*2+35)
226227
}
227228

228229
// Inject the final signature into the transaction and sanity check the sender

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-
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
32+
"golang.org/x/crypto/sha3"
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.NewKeccak256
42+
hashFunc := sha3.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.NewKeccak256
215+
hasher := sha3.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.NewKeccak256
242+
hasher := sha3.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.NewKeccak256
261+
hasher := sha3.NewLegacyKeccak256
262262
pool := NewTreePool(hasher, 128, maxproccnt)
263263
defer pool.Drain(0)
264264
wg := sync.WaitGroup{}
@@ -379,7 +379,7 @@ func benchmarkBMTBaseline(n int, t *testing.B) {
379379
tdata := testDataReader(64)
380380
data := make([]byte, 64)
381381
tdata.Read(data)
382-
hasher := sha3.NewKeccak256
382+
hasher := sha3.NewLegacyKeccak256
383383

384384
t.ReportAllocs()
385385
t.ResetTimer()
@@ -409,7 +409,7 @@ func benchmarkHasher(n int, t *testing.B) {
409409
tdata.Read(data)
410410

411411
size := 1
412-
hasher := sha3.NewKeccak256
412+
hasher := sha3.NewLegacyKeccak256
413413
segmentCount := 128
414414
pool := NewTreePool(hasher, segmentCount, size)
415415
bmt := New(pool)
@@ -428,7 +428,7 @@ func benchmarkHasherReuse(poolsize, n int, t *testing.B) {
428428
data := make([]byte, n)
429429
tdata.Read(data)
430430

431-
hasher := sha3.NewKeccak256
431+
hasher := sha3.NewLegacyKeccak256
432432
segmentCount := 128
433433
pool := NewTreePool(hasher, segmentCount, poolsize)
434434
cycles := 200
@@ -455,7 +455,7 @@ func benchmarkSHA3(n int, t *testing.B) {
455455
data := make([]byte, n)
456456
tdata := testDataReader(n)
457457
tdata.Read(data)
458-
hasher := sha3.NewKeccak256
458+
hasher := sha3.NewLegacyKeccak256
459459
h := hasher()
460460

461461
t.ReportAllocs()
@@ -471,7 +471,7 @@ func benchmarkRefHasher(n int, t *testing.B) {
471471
data := make([]byte, n)
472472
tdata := testDataReader(n)
473473
tdata.Read(data)
474-
hasher := sha3.NewKeccak256
474+
hasher := sha3.NewLegacyKeccak256
475475
rbmt := NewRefHasher(hasher, 128)
476476

477477
t.ReportAllocs()

cmd/XDC/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ var (
9898
//utils.TrieCacheGenFlag,
9999
utils.CacheLogSizeFlag,
100100
utils.FDLimitFlag,
101+
utils.CryptoKZGFlag,
101102
utils.ListenPortFlag,
102103
utils.MaxPeersFlag,
103104
utils.MaxPendingPeersFlag,

cmd/puppeth/wizard_genesis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (w *wizard) makeGenesis() {
105105
}
106106
}
107107
}
108-
genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+65)
108+
genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+crypto.SignatureLength)
109109
for i, signer := range signers {
110110
copy(genesis.ExtraData[32+i*common.AddressLength:], signer[:])
111111
}
@@ -179,7 +179,7 @@ func (w *wizard) makeGenesis() {
179179
validatorCap := new(big.Int)
180180
validatorCap.SetString("50000000000000000000000", 10)
181181
var validatorCaps []*big.Int
182-
genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+65)
182+
genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+crypto.SignatureLength)
183183
for i, signer := range signers {
184184
validatorCaps = append(validatorCaps, validatorCap)
185185
copy(genesis.ExtraData[32+i*common.AddressLength:], signer[:])

0 commit comments

Comments
 (0)