Fix various build errors and bugs

- bug in hex decoding in bls12381 package
- correct interfaces
- missing test variables
This commit is contained in:
Roy Crihfield 2023-06-23 00:30:34 +08:00
parent 5c57181c36
commit 80a4c5f4c5
6 changed files with 97 additions and 25 deletions

View File

@ -1,21 +1,12 @@
package bls12381 package bls12381
import ( import (
"encoding/hex"
"bytes" "bytes"
"crypto/rand" "crypto/rand"
"math/big" "math/big"
"testing" "testing"
) )
func fromHex(s string) []byte {
b, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return b
}
func (g *G1) one() *PointG1 { func (g *G1) one() *PointG1 {
one, _ := g.fromBytesUnchecked( one, _ := g.fromBytesUnchecked(
fromHex("" + fromHex("" +

View File

@ -17,15 +17,34 @@
package bls12381 package bls12381
import ( import (
"strings"
"encoding/hex" "encoding/hex"
"errors" "errors"
"math/big" "math/big"
) )
func bigFromHex(hex string) *big.Int { // fromHex returns the bytes represented by the hexadecimal string s.
b, _ := hex.DecodeString(strings.TrimPrefix(hex, "0x")) // s may be prefixed with "0x".
return new(big.Int).SetBytes(b) func fromHex(s string) []byte {
if has0xPrefix(s) {
s = s[2:]
}
if len(s)%2 == 1 {
s = "0" + s
}
h, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return h
}
// has0xPrefix validates str begins with '0x' or '0X'.
func has0xPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
}
func bigFromHex(s string) *big.Int {
return new(big.Int).SetBytes(fromHex(s))
} }
// decodeFieldElement expects 64 byte input with zero top 16 bytes, // decodeFieldElement expects 64 byte input with zero top 16 bytes,

View File

@ -17,11 +17,11 @@
package hasher package hasher
import ( import (
"fmt"
"bufio" "bufio"
"bytes" "bytes"
"encoding/gob" "encoding/gob"
"errors" "errors"
"fmt"
"io" "io"
"sync" "sync"
@ -214,7 +214,7 @@ func (st *StackTrie) TryUpdate(key, value []byte) error {
func (st *StackTrie) Update(key, value []byte) { func (st *StackTrie) Update(key, value []byte) {
if err := st.TryUpdate(key, value); err != nil { if err := st.TryUpdate(key, value); err != nil {
fmt.Errorf("Unhandled trie error in StackTrie.Update", "err", err) fmt.Println("Unhandled trie error in StackTrie.Update:", err)
} }
} }

View File

@ -34,6 +34,36 @@ var (
CalaverasGenesisHash = core.HexToHash("0xeb9233d066c275efcdfed8037f4fc082770176aefdbcb7691c71da412a5670f2") CalaverasGenesisHash = core.HexToHash("0xeb9233d066c275efcdfed8037f4fc082770176aefdbcb7691c71da412a5670f2")
) )
var (
// TestChainConfig contains every protocol change (EIPs) introduced
// and accepted by the Ethereum core developers for testing proposes.
TestChainConfig = &ChainConfig{
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
DAOForkSupport: false,
EIP150Block: big.NewInt(0),
EIP155Block: big.NewInt(0),
EIP158Block: big.NewInt(0),
ByzantiumBlock: big.NewInt(0),
ConstantinopleBlock: big.NewInt(0),
PetersburgBlock: big.NewInt(0),
IstanbulBlock: big.NewInt(0),
MuirGlacierBlock: big.NewInt(0),
BerlinBlock: big.NewInt(0),
LondonBlock: big.NewInt(0),
ArrowGlacierBlock: big.NewInt(0),
GrayGlacierBlock: big.NewInt(0),
MergeNetsplitBlock: nil,
ShanghaiTime: nil,
CancunTime: nil,
PragueTime: nil,
TerminalTotalDifficulty: nil,
TerminalTotalDifficultyPassed: false,
Ethash: new(EthashConfig),
Clique: nil,
}
)
// TrustedCheckpoint represents a set of post-processed trie roots (CHT and // TrustedCheckpoint represents a set of post-processed trie roots (CHT and
// BloomTrie) associated with the appropriate section index and head hash. It is // BloomTrie) associated with the appropriate section index and head hash. It is

View File

@ -22,6 +22,37 @@ import (
"testing" "testing"
) )
var (
// AllEthashProtocolChanges contains every protocol change (EIPs) introduced
// and accepted by the Ethereum core developers into the Ethash consensus.
AllEthashProtocolChanges = &ChainConfig{
ChainID: big.NewInt(1337),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
DAOForkSupport: false,
EIP150Block: big.NewInt(0),
EIP155Block: big.NewInt(0),
EIP158Block: big.NewInt(0),
ByzantiumBlock: big.NewInt(0),
ConstantinopleBlock: big.NewInt(0),
PetersburgBlock: big.NewInt(0),
IstanbulBlock: big.NewInt(0),
MuirGlacierBlock: big.NewInt(0),
BerlinBlock: big.NewInt(0),
LondonBlock: big.NewInt(0),
ArrowGlacierBlock: big.NewInt(0),
GrayGlacierBlock: big.NewInt(0),
MergeNetsplitBlock: nil,
ShanghaiTime: nil,
CancunTime: nil,
PragueTime: nil,
TerminalTotalDifficulty: nil,
TerminalTotalDifficultyPassed: false,
Ethash: new(EthashConfig),
Clique: nil,
}
)
func TestCheckCompatible(t *testing.T) { func TestCheckCompatible(t *testing.T) {
type test struct { type test struct {
stored, new *ChainConfig stored, new *ChainConfig

View File

@ -26,10 +26,10 @@ import (
"testing" "testing"
"github.com/openrelayxyz/plugeth-utils/core" "github.com/openrelayxyz/plugeth-utils/core"
"github.com/openrelayxyz/plugeth-utils/restricted/hexutil"
"github.com/openrelayxyz/plugeth-utils/restricted/types"
"github.com/openrelayxyz/plugeth-utils/restricted/crypto" "github.com/openrelayxyz/plugeth-utils/restricted/crypto"
"github.com/openrelayxyz/plugeth-utils/restricted/hexutil"
"github.com/openrelayxyz/plugeth-utils/restricted/rlp" "github.com/openrelayxyz/plugeth-utils/restricted/rlp"
"github.com/openrelayxyz/plugeth-utils/restricted/types"
) )
func fromHex(data string) []byte { func fromHex(data string) []byte {
@ -136,9 +136,10 @@ func (d *hashToHumanReadable) Reset() {
d.data = make([]byte, 0) d.data = make([]byte, 0)
} }
func (d *hashToHumanReadable) Update(i []byte, i2 []byte) { func (d *hashToHumanReadable) Update(i []byte, i2 []byte) error {
l := fmt.Sprintf("%x %x\n", i, i2) l := fmt.Sprintf("%x %x\n", i, i2)
d.data = append(d.data, []byte(l)...) d.data = append(d.data, []byte(l)...)
return nil
} }
func (d *hashToHumanReadable) Hash() core.Hash { func (d *hashToHumanReadable) Hash() core.Hash {