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

+2-2
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

+2-2
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

+2-4
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

+3-3
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

+2-1
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

+3-2
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

+2-1
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

+10-10
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

+1
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

+2-2
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[:])

cmd/utils/flags.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/XinFinOrg/XDPoSChain/core/txpool"
4242
"github.com/XinFinOrg/XDPoSChain/core/vm"
4343
"github.com/XinFinOrg/XDPoSChain/crypto"
44+
"github.com/XinFinOrg/XDPoSChain/crypto/kzg4844"
4445
"github.com/XinFinOrg/XDPoSChain/eth/downloader"
4546
"github.com/XinFinOrg/XDPoSChain/eth/ethconfig"
4647
"github.com/XinFinOrg/XDPoSChain/eth/filters"
@@ -310,6 +311,12 @@ var (
310311
Usage: "Raise the open file descriptor resource limit (default = system fd limit)",
311312
Category: flags.PerfCategory,
312313
}
314+
CryptoKZGFlag = &cli.StringFlag{
315+
Name: "crypto-kzg",
316+
Usage: "KZG library implementation to use; gokzg (recommended) or ckzg",
317+
Value: "gokzg",
318+
Category: flags.PerfCategory,
319+
}
313320

314321
// Miner settings
315322
MiningEnabledFlag = &cli.BoolFlag{
@@ -1445,7 +1452,14 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
14451452
cfg.GasPrice = big.NewInt(1)
14461453
}
14471454
}
1448-
// TODO(fjl): move trie cache generations into config
1455+
// Set any dangling config values
1456+
if ctx.String(CryptoKZGFlag.Name) != "gokzg" && ctx.String(CryptoKZGFlag.Name) != "ckzg" {
1457+
Fatalf("--%s flag must be 'gokzg' or 'ckzg'", CryptoKZGFlag.Name)
1458+
}
1459+
log.Info("Initializing the KZG library", "backend", ctx.String(CryptoKZGFlag.Name))
1460+
if err := kzg4844.UseCKZG(ctx.String(CryptoKZGFlag.Name) == "ckzg"); err != nil {
1461+
Fatalf("Failed to set KZG library implementation to %s: %v", ctx.String(CryptoKZGFlag.Name), err)
1462+
}
14491463
}
14501464

14511465
// SetupNetwork configures the system for either the main net or some test network.

common/types.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"reflect"
2626

2727
"github.com/XinFinOrg/XDPoSChain/common/hexutil"
28-
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
28+
"golang.org/x/crypto/sha3"
2929
)
3030

3131
const (
@@ -240,7 +240,7 @@ func (a Address) Hash() Hash { return BytesToHash(a[:]) }
240240
// Hex returns an EIP55-compliant hex string representation of the address.
241241
func (a Address) Hex() string {
242242
unchecksummed := hex.EncodeToString(a[:])
243-
sha := sha3.NewKeccak256()
243+
sha := sha3.NewLegacyKeccak256()
244244
sha.Write([]byte(unchecksummed))
245245
hash := sha.Sum(nil)
246246

consensus/XDPoS/engines/engine_v1/utils.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ 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/sha3"
1110
"github.com/XinFinOrg/XDPoSChain/params"
1211
"github.com/XinFinOrg/XDPoSChain/rlp"
1312
lru "github.com/hashicorp/golang-lru"
13+
"golang.org/x/crypto/sha3"
1414
)
1515

1616
// Get masternodes address from checkpoint Header.
@@ -59,7 +59,7 @@ func getM1M2(masternodes []common.Address, validators []int64, currentHeader *ty
5959
}
6060

6161
func sigHash(header *types.Header) (hash common.Hash) {
62-
hasher := sha3.NewKeccak256()
62+
hasher := sha3.NewLegacyKeccak256()
6363

6464
enc := []interface{}{
6565
header.ParentHash,
@@ -74,7 +74,7 @@ func sigHash(header *types.Header) (hash common.Hash) {
7474
header.GasLimit,
7575
header.GasUsed,
7676
header.Time,
77-
header.Extra[:len(header.Extra)-65], // Yes, this will panic if extra is too short
77+
header.Extra[:len(header.Extra)-crypto.SignatureLength], // Yes, this will panic if extra is too short
7878
header.MixDigest,
7979
header.Nonce,
8080
}

consensus/XDPoS/engines/engine_v2/utils.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ 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/sha3"
1514
"github.com/XinFinOrg/XDPoSChain/log"
1615
"github.com/XinFinOrg/XDPoSChain/rlp"
1716
lru "github.com/hashicorp/golang-lru"
17+
"golang.org/x/crypto/sha3"
1818
)
1919

2020
func sigHash(header *types.Header) (hash common.Hash) {
21-
hasher := sha3.NewKeccak256()
21+
hasher := sha3.NewLegacyKeccak256()
2222

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

consensus/XDPoS/utils/utils.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
"strconv"
1010

1111
"github.com/XinFinOrg/XDPoSChain/common"
12-
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
1312
"github.com/XinFinOrg/XDPoSChain/log"
1413
"github.com/XinFinOrg/XDPoSChain/rlp"
14+
"golang.org/x/crypto/sha3"
1515
)
1616

1717
func Position(list []common.Address, x common.Address) int {
@@ -91,7 +91,7 @@ func DecodeBytesExtraFields(b []byte, val interface{}) error {
9191
}
9292

9393
func rlpHash(x interface{}) (h common.Hash) {
94-
hw := sha3.NewKeccak256()
94+
hw := sha3.NewLegacyKeccak256()
9595
err := rlp.Encode(hw, x)
9696
if err != nil {
9797
log.Error("[rlpHash] Fail to hash item", "Error", err)

0 commit comments

Comments
 (0)