Skip to content

Commit

Permalink
crypto: switch over to upstream sha3 package (ethereum#18390)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan authored and JukLee0ira committed Jan 1, 2025
1 parent 94ff01f commit 9631011
Show file tree
Hide file tree
Showing 37 changed files with 74 additions and 1,746 deletions.
4 changes: 2 additions & 2 deletions XDCx/tradingstate/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"time"

"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
"github.com/globalsign/mgo/bson"
"golang.org/x/crypto/sha3"
)

const (
Expand Down Expand Up @@ -136,7 +136,7 @@ func (t *Trade) SetBSON(raw bson.Raw) error {
// The OrderHash, Amount, Taker and TradeNonce attributes must be
// set before attempting to compute the trade orderBookHash
func (t *Trade) ComputeHash() common.Hash {
sha := sha3.NewKeccak256()
sha := sha3.NewLegacyKeccak256()
sha.Write(t.MakerOrderHash.Bytes())
sha.Write(t.TakerOrderHash.Bytes())
return common.BytesToHash(sha.Sum(nil))
Expand Down
4 changes: 2 additions & 2 deletions XDCxlending/lendingstate/lendingitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/state"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
"github.com/globalsign/mgo/bson"
"golang.org/x/crypto/sha3"
)

const (
Expand Down Expand Up @@ -308,7 +308,7 @@ func (l *LendingItem) VerifyLendingStatus() error {
}

func (l *LendingItem) ComputeHash() common.Hash {
sha := sha3.NewKeccak256()
sha := sha3.NewLegacyKeccak256()
if l.Status == LendingStatusNew {
sha.Write(l.Relayer.Bytes())
sha.Write(l.UserAddress.Bytes())
Expand Down
6 changes: 2 additions & 4 deletions XDCxlending/lendingstate/lendingitem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
"github.com/XinFinOrg/XDPoSChain/core/state"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
"github.com/XinFinOrg/XDPoSChain/rpc"
"golang.org/x/crypto/sha3"
)

func TestLendingItem_VerifyLendingSide(t *testing.T) {
Expand Down Expand Up @@ -568,9 +568,8 @@ func sendOrder(nonce uint64) {
}

func computeHash(l *LendingOrderMsg) common.Hash {
sha := sha3.NewKeccak256()
sha := sha3.NewLegacyKeccak256()
if l.Status == LendingStatusCancelled {
sha := sha3.NewKeccak256()
sha.Write(l.Hash.Bytes())
sha.Write(common.BigToHash(big.NewInt(int64(l.AccountNonce))).Bytes())
sha.Write(l.UserAddress.Bytes())
Expand All @@ -593,5 +592,4 @@ func computeHash(l *LendingOrderMsg) common.Hash {
sha.Write(common.BigToHash(big.NewInt(int64(l.AccountNonce))).Bytes())
}
return common.BytesToHash(sha.Sum(nil))

}
6 changes: 3 additions & 3 deletions XDCxlending/lendingstate/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package lendingstate

import (
"fmt"
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
"math/big"
"strconv"
"time"

"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/globalsign/mgo/bson"
"golang.org/x/crypto/sha3"
)

const (
Expand Down Expand Up @@ -183,7 +183,7 @@ func (t *LendingTrade) SetBSON(raw bson.Raw) error {
}

func (t *LendingTrade) ComputeHash() common.Hash {
sha := sha3.NewKeccak256()
sha := sha3.NewLegacyKeccak256()
sha.Write(t.InvestingOrderHash.Bytes())
sha.Write(t.BorrowingOrderHash.Bytes())
return common.BytesToHash(sha.Sum(nil))
Expand Down
20 changes: 10 additions & 10 deletions bmt/bmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"testing"
"time"

"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
"golang.org/x/crypto/sha3"
)

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

sha3 := func(data ...[]byte) []byte {
h := hashFunc()
Expand Down Expand Up @@ -212,7 +212,7 @@ func testHasher(f func(BaseHasher, []byte, int, int) error) error {
tdata := testDataReader(4128)
data := make([]byte, 4128)
tdata.Read(data)
hasher := sha3.NewKeccak256
hasher := sha3.NewLegacyKeccak256
size := hasher().Size()
counts := []int{1, 2, 3, 4, 5, 8, 16, 32, 64, 128}

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

func testHasherReuse(i int, t *testing.T) {
hasher := sha3.NewKeccak256
hasher := sha3.NewLegacyKeccak256
pool := NewTreePool(hasher, 128, i)
defer pool.Drain(0)
bmt := New(pool)
Expand All @@ -258,7 +258,7 @@ func testHasherReuse(i int, t *testing.T) {
}

func TestHasherConcurrency(t *testing.T) {
hasher := sha3.NewKeccak256
hasher := sha3.NewLegacyKeccak256
pool := NewTreePool(hasher, 128, maxproccnt)
defer pool.Drain(0)
wg := sync.WaitGroup{}
Expand Down Expand Up @@ -379,7 +379,7 @@ func benchmarkBMTBaseline(n int, t *testing.B) {
tdata := testDataReader(64)
data := make([]byte, 64)
tdata.Read(data)
hasher := sha3.NewKeccak256
hasher := sha3.NewLegacyKeccak256

t.ReportAllocs()
t.ResetTimer()
Expand Down Expand Up @@ -409,7 +409,7 @@ func benchmarkHasher(n int, t *testing.B) {
tdata.Read(data)

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

hasher := sha3.NewKeccak256
hasher := sha3.NewLegacyKeccak256
segmentCount := 128
pool := NewTreePool(hasher, segmentCount, poolsize)
cycles := 200
Expand All @@ -455,7 +455,7 @@ func benchmarkSHA3(n int, t *testing.B) {
data := make([]byte, n)
tdata := testDataReader(n)
tdata.Read(data)
hasher := sha3.NewKeccak256
hasher := sha3.NewLegacyKeccak256
h := hasher()

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

t.ReportAllocs()
Expand Down
4 changes: 2 additions & 2 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"reflect"

"github.com/XinFinOrg/XDPoSChain/common/hexutil"
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
"golang.org/x/crypto/sha3"
)

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

Expand Down
4 changes: 2 additions & 2 deletions consensus/XDPoS/engines/engine_v1/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
"github.com/XinFinOrg/XDPoSChain/params"
"github.com/XinFinOrg/XDPoSChain/rlp"
lru "github.com/hashicorp/golang-lru"
"golang.org/x/crypto/sha3"
)

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

func sigHash(header *types.Header) (hash common.Hash) {
hasher := sha3.NewKeccak256()
hasher := sha3.NewLegacyKeccak256()

enc := []interface{}{
header.ParentHash,
Expand Down
4 changes: 2 additions & 2 deletions consensus/XDPoS/engines/engine_v2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
"github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/rlp"
lru "github.com/hashicorp/golang-lru"
"golang.org/x/crypto/sha3"
)

func sigHash(header *types.Header) (hash common.Hash) {
hasher := sha3.NewKeccak256()
hasher := sha3.NewLegacyKeccak256()

enc := []interface{}{
header.ParentHash,
Expand Down
4 changes: 2 additions & 2 deletions consensus/XDPoS/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"strconv"

"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
"github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/rlp"
"golang.org/x/crypto/sha3"
)

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

func rlpHash(x interface{}) (h common.Hash) {
hw := sha3.NewKeccak256()
hw := sha3.NewLegacyKeccak256()
err := rlp.Encode(hw, x)
if err != nil {
log.Error("[rlpHash] Fail to hash item", "Error", err)
Expand Down
4 changes: 2 additions & 2 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ import (
"github.com/XinFinOrg/XDPoSChain/core/state"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
"github.com/XinFinOrg/XDPoSChain/ethdb"
"github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/params"
"github.com/XinFinOrg/XDPoSChain/rlp"
"github.com/XinFinOrg/XDPoSChain/rpc"
lru "github.com/hashicorp/golang-lru"
"golang.org/x/crypto/sha3"
)

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

enc := []interface{}{
header.ParentHash,
Expand Down
10 changes: 5 additions & 5 deletions consensus/ethash/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/bitutil"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
"github.com/XinFinOrg/XDPoSChain/log"
"golang.org/x/crypto/sha3"
)

const (
Expand Down Expand Up @@ -112,7 +112,7 @@ func seedHash(block uint64) []byte {
if block < epochLength {
return seed
}
keccak256 := makeHasher(sha3.NewKeccak256())
keccak256 := makeHasher(sha3.NewLegacyKeccak256())
for i := 0; i < int(block/epochLength); i++ {
keccak256(seed, seed)
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func generateCache(dest []uint32, epoch uint64, seed []byte) {
}
}()
// Create a hasher to reuse between invocations
keccak512 := makeHasher(sha3.NewKeccak512())
keccak512 := makeHasher(sha3.NewLegacyKeccak512())

// Sequentially produce the initial dataset
keccak512(cache, seed)
Expand Down Expand Up @@ -303,7 +303,7 @@ func generateDataset(dest []uint32, epoch uint64, cache []uint32) {
defer pend.Done()

// Create a hasher to reuse between invocations
keccak512 := makeHasher(sha3.NewKeccak512())
keccak512 := makeHasher(sha3.NewLegacyKeccak512())

// Calculate the data segment this thread should generate
batch := uint32((size + hashBytes*uint64(threads) - 1) / (hashBytes * uint64(threads)))
Expand Down Expand Up @@ -377,7 +377,7 @@ func hashimoto(hash []byte, nonce uint64, size uint64, lookup func(index uint32)
// in-memory cache) in order to produce our final value for a particular header
// hash and nonce.
func hashimotoLight(size uint64, cache []uint32, hash []byte, nonce uint64) ([]byte, []byte) {
keccak512 := makeHasher(sha3.NewKeccak512())
keccak512 := makeHasher(sha3.NewLegacyKeccak512())

lookup := func(index uint32) []uint32 {
rawData := generateDatasetItem(cache, index, keccak512)
Expand Down
8 changes: 4 additions & 4 deletions core/database_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
"golang.org/x/crypto/sha3"
"github.com/XinFinOrg/XDPoSChain/rlp"
)

Expand All @@ -47,7 +47,7 @@ func TestHeaderStorage(t *testing.T) {
if entry := GetHeaderRLP(db, header.Hash(), header.Number.Uint64()); entry == nil {
t.Fatalf("Stored header RLP not found")
} else {
hasher := sha3.NewKeccak256()
hasher := sha3.NewLegacyKeccak256()
hasher.Write(entry)

if hash := common.BytesToHash(hasher.Sum(nil)); hash != header.Hash() {
Expand All @@ -68,7 +68,7 @@ func TestBodyStorage(t *testing.T) {
// Create a test body to move around the database and make sure it's really new
body := &types.Body{Uncles: []*types.Header{{Extra: []byte("test header")}}}

hasher := sha3.NewKeccak256()
hasher := sha3.NewLegacyKeccak256()
rlp.Encode(hasher, body)
hash := common.BytesToHash(hasher.Sum(nil))

Expand All @@ -85,7 +85,7 @@ func TestBodyStorage(t *testing.T) {
if entry := GetBodyRLP(db, hash, 0); entry == nil {
t.Fatalf("Stored body RLP not found")
} else {
hasher := sha3.NewKeccak256()
hasher := sha3.NewLegacyKeccak256()
hasher.Write(entry)

if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash {
Expand Down
6 changes: 3 additions & 3 deletions core/txpool/lending_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/crypto/sha3"
"github.com/XinFinOrg/XDPoSChain/ethclient"
"github.com/XinFinOrg/XDPoSChain/rpc"
"golang.org/x/crypto/sha3"
)

type LendingMsg struct {
Expand Down Expand Up @@ -63,7 +63,7 @@ func getLendingNonce(userAddress common.Address) (uint64, error) {

func (l *LendingMsg) computeHash() common.Hash {
borrowing := l.Side == lendingstate.Borrowing
sha := sha3.NewKeccak256()
sha := sha3.NewLegacyKeccak256()
if l.Type == lendingstate.Repay {
sha.Write(common.BigToHash(big.NewInt(int64(l.AccountNonce))).Bytes())
sha.Write([]byte(l.Status))
Expand All @@ -83,7 +83,7 @@ func (l *LendingMsg) computeHash() common.Hash {
sha.Write(common.BigToHash(l.Quantity).Bytes())
} else {
if l.Status == lendingstate.LendingStatusCancelled {
sha := sha3.NewKeccak256()
sha := sha3.NewLegacyKeccak256()
sha.Write(l.Hash.Bytes())
sha.Write(common.BigToHash(big.NewInt(int64(l.AccountNonce))).Bytes())
sha.Write(l.UserAddress.Bytes())
Expand Down
Loading

0 comments on commit 9631011

Please sign in to comment.