Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade package crypto and support KZG cryptography #751

Merged
merged 39 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
d3fc08d
update golang.org/x/crypto to v0.29.0
gzliudan Nov 29, 2024
92fc843
crypto: switch over to upstream sha3 package (#18390)
gzliudan Nov 29, 2024
9d9bd65
crypto/ecies: remove unused function (#19096)
gzliudan Nov 29, 2024
d2ac7d0
crypto: fix build when CGO_ENABLED=0 (#19121)
gzliudan Nov 29, 2024
66dd846
crypto/bn256/cloudflare: fix comments to describe the updated curve p…
gzliudan Nov 29, 2024
b9054b2
crypto: replace fmt.Println calls with t.Log in tests (#19670)
gzliudan Nov 29, 2024
8dcea8d
crypto: replace t.Log(); t.FailNow() with t.Fatal() (#19849)
gzliudan Nov 29, 2024
ad5e7d6
crypto: add SignatureLength constant and use it everywhere (#19996)
gzliudan Nov 29, 2024
e1eb00e
crypto: make unit tests work with Go 1.13 (#20053)
gzliudan Nov 29, 2024
6033722
crypto: use golangci-lint (#20295)
gzliudan Nov 29, 2024
b2be754
crypto/bn256: fix import line (#20723)
gzliudan Nov 29, 2024
ee303c9
crypto/ecies: improve concatKDF (#20836)
gzliudan Nov 29, 2024
f948466
crypto: less allocations when hashing and tx handling (#21265)
gzliudan Nov 29, 2024
a2eb855
crypto/bn256: better comments for u, P and Order (#21836)
gzliudan Nov 29, 2024
a64e424
crypto/bn256: refine comments according to #19577, #21595, and #21836…
gzliudan Nov 29, 2024
ddc5e61
crypto/bn256: fix bn256Mul fuzzer to not hang on large input (#21872)
gzliudan Nov 29, 2024
b1c7190
crypto: improve trie updates (#21047)
gzliudan Nov 29, 2024
ae95cea
bn256: added consensys/gurvy bn256 implementation (#21812)
gzliudan Nov 29, 2024
7711f4b
tests/fuzzers: crypto/bn256 tests against gnark-crypto (#22755)
gzliudan Nov 29, 2024
5ee26e0
crypto/secp256k1: fix undefined behavior in BitCurve.Add (#22621)
gzliudan Nov 29, 2024
9dab065
crypto: gofuzz build directives (#23137)
gzliudan Nov 29, 2024
146bc2b
crypto: add go:build lines (#23468)
gzliudan Nov 29, 2024
949fa63
crypto/ecies: use AES-192 for curve P384 (#24139)
gzliudan Nov 29, 2024
cb3edac
crypto: use btcec/v2 for no-cgo (#24533)
gzliudan Nov 29, 2024
797efe7
crypto: more linters and fix typo (#24783)
gzliudan Nov 29, 2024
bedd571
crypto/kzg4844: pull in the C and Go libs for KZG cryptography (#27155)
gzliudan Nov 29, 2024
ed03a99
crypto/kzg4844: upgrade c-kzg-4844 to v0.2.0 (#27257)
gzliudan Nov 29, 2024
bd93c59
crypto: replace noarg fmt.Errorf with errors.New (#27333)
gzliudan Nov 29, 2024
17c0480
tests/fuzzers/bn256: add PairingCheck fuzzer (#27252)
gzliudan Nov 29, 2024
541ddee
go.mod: update kzg libraries to use big-endian (#27510)
gzliudan Nov 29, 2024
247ebd6
crypto/kzg4844: do lazy init in all ckzg funcs (#27679)
gzliudan Nov 29, 2024
8f57d6c
go.mod: upgrade c-kzg-4844 (#27907)
gzliudan Nov 29, 2024
f7b6ad6
crypto, tests: update fuzzers to native go fuzzing (#28352)
gzliudan Nov 29, 2024
b5cc7e6
crypto/blake2b: put architecture-dependent features behind build-tag …
gzliudan Nov 29, 2024
824dea6
crypto/kzg4844: use the new trusted setup file and format (#28383)
gzliudan Nov 29, 2024
cae53aa
crypto/kzg4844: add helpers for versioned blob hashes (#28827)
gzliudan Nov 29, 2024
fabfcc7
crypto: fix docstring names (#28923)
gzliudan Nov 29, 2024
7278557
crypto: add support for blobs in eth_fillTransaction (#28839)
gzliudan Nov 29, 2024
3fbbc9d
crypto: fix typos in comments (#29186)
gzliudan Nov 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 2 additions & 1 deletion accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/XinFinOrg/XDPoSChain/core/state"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/core/vm"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/eth/filters"
"github.com/XinFinOrg/XDPoSChain/ethdb"
"github.com/XinFinOrg/XDPoSChain/event"
Expand Down Expand Up @@ -101,7 +102,7 @@ func NewXDCSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64, chainConfi
GasLimit: gasLimit, // need this big, support initial smart contract
Config: chainConfig,
Alloc: alloc,
ExtraData: append(make([]byte, 32), make([]byte, 65)...),
ExtraData: append(make([]byte, 32), make([]byte, crypto.SignatureLength)...),
}
genesis.MustCommit(database)
consensus := XDPoS.NewFaker(database, chainConfig)
Expand Down
5 changes: 3 additions & 2 deletions accounts/usbwallet/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/hexutil"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/rlp"
)
Expand Down Expand Up @@ -341,7 +342,7 @@ func (w *ledgerDriver) ledgerSign(derivationPath []uint32, tx *types.Transaction
op = ledgerP1ContTransactionData
}
// Extract the Ethereum signature and do a sanity validation
if len(reply) != 65 {
if len(reply) != crypto.SignatureLength {
return common.Address{}, nil, errors.New("reply lacks signature")
}
signature := append(reply[1:], reply[0])
Expand All @@ -352,7 +353,7 @@ func (w *ledgerDriver) ledgerSign(derivationPath []uint32, tx *types.Transaction
signer = new(types.HomesteadSigner)
} else {
signer = types.NewEIP155Signer(chainID)
signature[64] = signature[64] - byte(chainID.Uint64()*2+35)
signature[crypto.RecoveryIDOffset] = signature[crypto.RecoveryIDOffset] - byte(chainID.Uint64()*2+35)
}
signed, err := tx.WithSignature(signer, signature)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion accounts/usbwallet/trezor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/hexutil"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/log"
"github.com/golang/protobuf/proto"
)
Expand Down Expand Up @@ -222,7 +223,7 @@ func (w *trezorDriver) trezorSign(derivationPath []uint32, tx *types.Transaction
} else {
// Trezor backend does not support typed transactions yet.
signer = types.NewEIP155Signer(chainID)
signature[64] = signature[64] - byte(chainID.Uint64()*2+35)
signature[crypto.RecoveryIDOffset] = signature[crypto.RecoveryIDOffset] - byte(chainID.Uint64()*2+35)
}

// Inject the final signature into the transaction and sanity check the sender
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
1 change: 1 addition & 0 deletions cmd/XDC/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ var (
//utils.TrieCacheGenFlag,
utils.CacheLogSizeFlag,
utils.FDLimitFlag,
utils.CryptoKZGFlag,
utils.ListenPortFlag,
utils.MaxPeersFlag,
utils.MaxPendingPeersFlag,
Expand Down
4 changes: 2 additions & 2 deletions cmd/puppeth/wizard_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (w *wizard) makeGenesis() {
}
}
}
genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+65)
genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+crypto.SignatureLength)
for i, signer := range signers {
copy(genesis.ExtraData[32+i*common.AddressLength:], signer[:])
}
Expand Down Expand Up @@ -179,7 +179,7 @@ func (w *wizard) makeGenesis() {
validatorCap := new(big.Int)
validatorCap.SetString("50000000000000000000000", 10)
var validatorCaps []*big.Int
genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+65)
genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+crypto.SignatureLength)
for i, signer := range signers {
validatorCaps = append(validatorCaps, validatorCap)
copy(genesis.ExtraData[32+i*common.AddressLength:], signer[:])
Expand Down
16 changes: 15 additions & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/XinFinOrg/XDPoSChain/core/txpool"
"github.com/XinFinOrg/XDPoSChain/core/vm"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/crypto/kzg4844"
"github.com/XinFinOrg/XDPoSChain/eth/downloader"
"github.com/XinFinOrg/XDPoSChain/eth/ethconfig"
"github.com/XinFinOrg/XDPoSChain/eth/filters"
Expand Down Expand Up @@ -310,6 +311,12 @@ var (
Usage: "Raise the open file descriptor resource limit (default = system fd limit)",
Category: flags.PerfCategory,
}
CryptoKZGFlag = &cli.StringFlag{
Name: "crypto-kzg",
Usage: "KZG library implementation to use; gokzg (recommended) or ckzg",
Value: "gokzg",
Category: flags.PerfCategory,
}

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

// SetupNetwork configures the system for either the main net or some test network.
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
6 changes: 3 additions & 3 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 All @@ -74,7 +74,7 @@ func sigHash(header *types.Header) (hash common.Hash) {
header.GasLimit,
header.GasUsed,
header.Time,
header.Extra[:len(header.Extra)-65], // Yes, this will panic if extra is too short
header.Extra[:len(header.Extra)-crypto.SignatureLength], // Yes, this will panic if extra is too short
header.MixDigest,
header.Nonce,
}
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
Loading
Loading