forked from cerc-io/plugeth
all: unify big.Int zero checks, use common/math in more places (#3716)
* common/math: optimize PaddedBigBytes, use it more name old time/op new time/op delta PaddedBigBytes-8 71.1ns ± 5% 46.1ns ± 1% -35.15% (p=0.000 n=20+19) name old alloc/op new alloc/op delta PaddedBigBytes-8 48.0B ± 0% 32.0B ± 0% -33.33% (p=0.000 n=20+20) * all: unify big.Int zero checks Various checks were in use. This commit replaces them all with Int.Sign, which is cheaper and less code. eg templates: func before(x *big.Int) bool { return x.BitLen() == 0 } func after(x *big.Int) bool { return x.Sign() == 0 } func before(x *big.Int) bool { return x.BitLen() > 0 } func after(x *big.Int) bool { return x.Sign() != 0 } func before(x *big.Int) int { return x.Cmp(common.Big0) } func after(x *big.Int) int { return x.Sign() } * common/math, crypto/secp256k1: make ReadBits public in package math
This commit is contained in:
parent
d4f60d362b
commit
5f7826270c
@ -237,7 +237,7 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM
|
|||||||
if call.GasPrice == nil {
|
if call.GasPrice == nil {
|
||||||
call.GasPrice = big.NewInt(1)
|
call.GasPrice = big.NewInt(1)
|
||||||
}
|
}
|
||||||
if call.Gas == nil || call.Gas.BitLen() == 0 {
|
if call.Gas == nil || call.Gas.Sign() == 0 {
|
||||||
call.Gas = big.NewInt(50000000)
|
call.Gas = big.NewInt(50000000)
|
||||||
}
|
}
|
||||||
if call.Value == nil {
|
if call.Value == nil {
|
||||||
|
@ -59,7 +59,7 @@ var (
|
|||||||
|
|
||||||
// U256 converts a big Int into a 256bit EVM number.
|
// U256 converts a big Int into a 256bit EVM number.
|
||||||
func U256(n *big.Int) []byte {
|
func U256(n *big.Int) []byte {
|
||||||
return common.LeftPadBytes(math.U256(n).Bytes(), 32)
|
return math.PaddedBigBytes(math.U256(n), 32)
|
||||||
}
|
}
|
||||||
|
|
||||||
// packNum packs the given number (using the reflect value) and will cast it to appropriate number representation
|
// packNum packs the given number (using the reflect value) and will cast it to appropriate number representation
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/common/math"
|
||||||
)
|
)
|
||||||
|
|
||||||
// packBytesSlice packs the given bytes as [L, V] as the canonical representation
|
// packBytesSlice packs the given bytes as [L, V] as the canonical representation
|
||||||
@ -45,9 +46,9 @@ func packElement(t Type, reflectValue reflect.Value) []byte {
|
|||||||
return common.LeftPadBytes(reflectValue.Bytes(), 32)
|
return common.LeftPadBytes(reflectValue.Bytes(), 32)
|
||||||
case BoolTy:
|
case BoolTy:
|
||||||
if reflectValue.Bool() {
|
if reflectValue.Bool() {
|
||||||
return common.LeftPadBytes(common.Big1.Bytes(), 32)
|
return math.PaddedBigBytes(common.Big1, 32)
|
||||||
} else {
|
} else {
|
||||||
return common.LeftPadBytes(common.Big0.Bytes(), 32)
|
return math.PaddedBigBytes(common.Big0, 32)
|
||||||
}
|
}
|
||||||
case BytesTy:
|
case BytesTy:
|
||||||
if reflectValue.Kind() == reflect.Array {
|
if reflectValue.Kind() == reflect.Array {
|
||||||
|
@ -36,6 +36,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/common/math"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/crypto/randentropy"
|
"github.com/ethereum/go-ethereum/crypto/randentropy"
|
||||||
"github.com/pborman/uuid"
|
"github.com/pborman/uuid"
|
||||||
@ -115,8 +116,7 @@ func EncryptKey(key *Key, auth string, scryptN, scryptP int) ([]byte, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
encryptKey := derivedKey[:16]
|
encryptKey := derivedKey[:16]
|
||||||
keyBytes0 := crypto.FromECDSA(key.PrivateKey)
|
keyBytes := math.PaddedBigBytes(key.PrivateKey.D, 32)
|
||||||
keyBytes := common.LeftPadBytes(keyBytes0, 32)
|
|
||||||
|
|
||||||
iv := randentropy.GetEntropyCSPRNG(aes.BlockSize) // 16
|
iv := randentropy.GetEntropyCSPRNG(aes.BlockSize) // 16
|
||||||
cipherText, err := aesCTRXOR(encryptKey, keyBytes, iv)
|
cipherText, err := aesCTRXOR(encryptKey, keyBytes, iv)
|
||||||
|
@ -416,7 +416,7 @@ func (w *ledgerWallet) selfDerive() {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
// If the next account is empty, stop self-derivation, but add it nonetheless
|
// If the next account is empty, stop self-derivation, but add it nonetheless
|
||||||
if balance.BitLen() == 0 && nonce == 0 {
|
if balance.Sign() == 0 && nonce == 0 {
|
||||||
empty = true
|
empty = true
|
||||||
}
|
}
|
||||||
// We've just self-derived a new account, start tracking it locally
|
// We've just self-derived a new account, start tracking it locally
|
||||||
|
@ -28,6 +28,13 @@ var (
|
|||||||
MaxBig256 = new(big.Int).Set(tt256m1)
|
MaxBig256 = new(big.Int).Set(tt256m1)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// number of bits in a big.Word
|
||||||
|
wordBits = 32 << (uint64(^big.Word(0)) >> 63)
|
||||||
|
// number of bytes in a big.Word
|
||||||
|
wordBytes = wordBits / 8
|
||||||
|
)
|
||||||
|
|
||||||
// ParseBig256 parses s as a 256 bit integer in decimal or hexadecimal syntax.
|
// ParseBig256 parses s as a 256 bit integer in decimal or hexadecimal syntax.
|
||||||
// Leading zeros are accepted. The empty string parses as zero.
|
// Leading zeros are accepted. The empty string parses as zero.
|
||||||
func ParseBig256(s string) (*big.Int, bool) {
|
func ParseBig256(s string) (*big.Int, bool) {
|
||||||
@ -91,12 +98,25 @@ func FirstBitSet(v *big.Int) int {
|
|||||||
// PaddedBigBytes encodes a big integer as a big-endian byte slice. The length
|
// PaddedBigBytes encodes a big integer as a big-endian byte slice. The length
|
||||||
// of the slice is at least n bytes.
|
// of the slice is at least n bytes.
|
||||||
func PaddedBigBytes(bigint *big.Int, n int) []byte {
|
func PaddedBigBytes(bigint *big.Int, n int) []byte {
|
||||||
bytes := bigint.Bytes()
|
if bigint.BitLen()/8 >= n {
|
||||||
if len(bytes) >= n {
|
return bigint.Bytes()
|
||||||
return bytes
|
|
||||||
}
|
}
|
||||||
ret := make([]byte, n)
|
ret := make([]byte, n)
|
||||||
return append(ret[:len(ret)-len(bytes)], bytes...)
|
ReadBits(bigint, ret)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadBits encodes the absolute value of bigint as big-endian bytes. Callers must ensure
|
||||||
|
// that buf has enough space. If buf is too short the result will be incomplete.
|
||||||
|
func ReadBits(bigint *big.Int, buf []byte) {
|
||||||
|
i := len(buf)
|
||||||
|
for _, d := range bigint.Bits() {
|
||||||
|
for j := 0; j < wordBytes && i > 0; j++ {
|
||||||
|
i--
|
||||||
|
buf[i] = byte(d)
|
||||||
|
d >>= 8
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// U256 encodes as a 256 bit two's complement number. This operation is destructive.
|
// U256 encodes as a 256 bit two's complement number. This operation is destructive.
|
||||||
@ -119,9 +139,6 @@ func S256(x *big.Int) *big.Int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// wordSize is the size number of bits in a big.Word.
|
|
||||||
const wordSize = 32 << (uint64(^big.Word(0)) >> 63)
|
|
||||||
|
|
||||||
// Exp implements exponentiation by squaring.
|
// Exp implements exponentiation by squaring.
|
||||||
// Exp returns a newly-allocated big integer and does not change
|
// Exp returns a newly-allocated big integer and does not change
|
||||||
// base or exponent. The result is truncated to 256 bits.
|
// base or exponent. The result is truncated to 256 bits.
|
||||||
@ -131,7 +148,7 @@ func Exp(base, exponent *big.Int) *big.Int {
|
|||||||
result := big.NewInt(1)
|
result := big.NewInt(1)
|
||||||
|
|
||||||
for _, word := range exponent.Bits() {
|
for _, word := range exponent.Bits() {
|
||||||
for i := 0; i < wordSize; i++ {
|
for i := 0; i < wordBits; i++ {
|
||||||
if word&1 == 1 {
|
if word&1 == 1 {
|
||||||
U256(result.Mul(result, base))
|
U256(result.Mul(result, base))
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ package math
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/hex"
|
||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -131,6 +132,28 @@ func TestPaddedBigBytes(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkPaddedBigBytes(b *testing.B) {
|
||||||
|
bigint := MustParseBig256("123456789123456789123456789123456789")
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
PaddedBigBytes(bigint, 32)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadBits(t *testing.T) {
|
||||||
|
check := func(input string) {
|
||||||
|
want, _ := hex.DecodeString(input)
|
||||||
|
int, _ := new(big.Int).SetString(input, 16)
|
||||||
|
buf := make([]byte, len(want))
|
||||||
|
ReadBits(int, buf)
|
||||||
|
if !bytes.Equal(buf, want) {
|
||||||
|
t.Errorf("have: %x\nwant: %x", buf, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
check("000000000000000000000000000000000000000000000000000000FEFCF3F8F0")
|
||||||
|
check("0000000000012345000000000000000000000000000000000000FEFCF3F8F0")
|
||||||
|
check("18F8F8F1000111000110011100222004330052300000000000000000FEFCF3F8F0")
|
||||||
|
}
|
||||||
|
|
||||||
func TestU256(t *testing.T) {
|
func TestU256(t *testing.T) {
|
||||||
tests := []struct{ x, y *big.Int }{
|
tests := []struct{ x, y *big.Int }{
|
||||||
{x: big.NewInt(0), y: big.NewInt(0)},
|
{x: big.NewInt(0), y: big.NewInt(0)},
|
||||||
|
@ -247,7 +247,7 @@ func (self *Chequebook) Issue(beneficiary common.Address, amount *big.Int) (ch *
|
|||||||
defer self.lock.Unlock()
|
defer self.lock.Unlock()
|
||||||
self.lock.Lock()
|
self.lock.Lock()
|
||||||
|
|
||||||
if amount.Cmp(common.Big0) <= 0 {
|
if amount.Sign() <= 0 {
|
||||||
return nil, fmt.Errorf("amount must be greater than zero (%v)", amount)
|
return nil, fmt.Errorf("amount must be greater than zero (%v)", amount)
|
||||||
}
|
}
|
||||||
if self.balance.Cmp(amount) < 0 {
|
if self.balance.Cmp(amount) < 0 {
|
||||||
@ -515,7 +515,7 @@ func (self *Inbox) autoCash(cashInterval time.Duration) {
|
|||||||
self.quit = nil
|
self.quit = nil
|
||||||
}
|
}
|
||||||
// if maxUncashed is set to 0, then autocash on receipt
|
// if maxUncashed is set to 0, then autocash on receipt
|
||||||
if cashInterval == time.Duration(0) || self.maxUncashed != nil && self.maxUncashed.Cmp(common.Big0) == 0 {
|
if cashInterval == time.Duration(0) || self.maxUncashed != nil && self.maxUncashed.Sign() == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -597,7 +597,7 @@ func (self *Cheque) Verify(signerKey *ecdsa.PublicKey, contract, beneficiary com
|
|||||||
amount := new(big.Int).Set(self.Amount)
|
amount := new(big.Int).Set(self.Amount)
|
||||||
if sum != nil {
|
if sum != nil {
|
||||||
amount.Sub(amount, sum)
|
amount.Sub(amount, sum)
|
||||||
if amount.Cmp(common.Big0) <= 0 {
|
if amount.Sign() <= 0 {
|
||||||
return nil, fmt.Errorf("incorrect amount: %v <= 0", amount)
|
return nil, fmt.Errorf("incorrect amount: %v <= 0", amount)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ func TestIssueAndReceive(t *testing.T) {
|
|||||||
t.Fatalf("expected no error, got %v", err)
|
t.Fatalf("expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if chbook.Balance().Cmp(common.Big0) != 0 {
|
if chbook.Balance().Sign() != 0 {
|
||||||
t.Errorf("expected: %v, got %v", "0", chbook.Balance())
|
t.Errorf("expected: %v, got %v", "0", chbook.Balance())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -359,7 +359,7 @@ func (hc *HeaderChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []co
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
chain = append(chain, next)
|
chain = append(chain, next)
|
||||||
if header.Number.Cmp(common.Big0) == 0 {
|
if header.Number.Sign() == 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ type stateObject struct {
|
|||||||
|
|
||||||
// empty returns whether the account is considered empty.
|
// empty returns whether the account is considered empty.
|
||||||
func (s *stateObject) empty() bool {
|
func (s *stateObject) empty() bool {
|
||||||
return s.data.Nonce == 0 && s.data.Balance.BitLen() == 0 && bytes.Equal(s.data.CodeHash, emptyCodeHash)
|
return s.data.Nonce == 0 && s.data.Balance.Sign() == 0 && bytes.Equal(s.data.CodeHash, emptyCodeHash)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Account is the Ethereum consensus representation of accounts.
|
// Account is the Ethereum consensus representation of accounts.
|
||||||
@ -243,7 +243,7 @@ func (self *stateObject) CommitTrie(db trie.Database, dbw trie.DatabaseWriter) e
|
|||||||
func (c *stateObject) AddBalance(amount *big.Int) {
|
func (c *stateObject) AddBalance(amount *big.Int) {
|
||||||
// EIP158: We must check emptiness for the objects such that the account
|
// EIP158: We must check emptiness for the objects such that the account
|
||||||
// clearing (0,0,0 objects) can take effect.
|
// clearing (0,0,0 objects) can take effect.
|
||||||
if amount.Cmp(common.Big0) == 0 {
|
if amount.Sign() == 0 {
|
||||||
if c.empty() {
|
if c.empty() {
|
||||||
c.touch()
|
c.touch()
|
||||||
}
|
}
|
||||||
@ -260,7 +260,7 @@ func (c *stateObject) AddBalance(amount *big.Int) {
|
|||||||
// SubBalance removes amount from c's balance.
|
// SubBalance removes amount from c's balance.
|
||||||
// It is used to remove funds from the origin account of a transfer.
|
// It is used to remove funds from the origin account of a transfer.
|
||||||
func (c *stateObject) SubBalance(amount *big.Int) {
|
func (c *stateObject) SubBalance(amount *big.Int) {
|
||||||
if amount.Cmp(common.Big0) == 0 {
|
if amount.Sign() == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.SetBalance(new(big.Int).Sub(c.Balance(), amount))
|
c.SetBalance(new(big.Int).Sub(c.Balance(), amount))
|
||||||
|
@ -299,7 +299,7 @@ func (pool *TxPool) validateTx(tx *types.Transaction) error {
|
|||||||
// Transactions can't be negative. This may never happen
|
// Transactions can't be negative. This may never happen
|
||||||
// using RLP decoded transactions but may occur if you create
|
// using RLP decoded transactions but may occur if you create
|
||||||
// a transaction using the RPC for example.
|
// a transaction using the RPC for example.
|
||||||
if tx.Value().Cmp(common.Big0) < 0 {
|
if tx.Value().Sign() < 0 {
|
||||||
return ErrNegativeValue
|
return ErrNegativeValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ var (
|
|||||||
|
|
||||||
// deriveSigner makes a *best* guess about which signer to use.
|
// deriveSigner makes a *best* guess about which signer to use.
|
||||||
func deriveSigner(V *big.Int) Signer {
|
func deriveSigner(V *big.Int) Signer {
|
||||||
if V.BitLen() > 0 && isProtectedV(V) {
|
if V.Sign() != 0 && isProtectedV(V) {
|
||||||
return EIP155Signer{chainId: deriveChainId(V)}
|
return EIP155Signer{chainId: deriveChainId(V)}
|
||||||
} else {
|
} else {
|
||||||
return HomesteadSigner{}
|
return HomesteadSigner{}
|
||||||
|
@ -167,7 +167,7 @@ func (s EIP155Signer) WithSignature(tx *Transaction, sig []byte) (*Transaction,
|
|||||||
cpy.data.R = new(big.Int).SetBytes(sig[:32])
|
cpy.data.R = new(big.Int).SetBytes(sig[:32])
|
||||||
cpy.data.S = new(big.Int).SetBytes(sig[32:64])
|
cpy.data.S = new(big.Int).SetBytes(sig[32:64])
|
||||||
cpy.data.V = new(big.Int).SetBytes([]byte{sig[64]})
|
cpy.data.V = new(big.Int).SetBytes([]byte{sig[64]})
|
||||||
if s.chainId.BitLen() > 0 {
|
if s.chainId.Sign() != 0 {
|
||||||
cpy.data.V = big.NewInt(int64(sig[64] + 35))
|
cpy.data.V = big.NewInt(int64(sig[64] + 35))
|
||||||
cpy.data.V.Add(cpy.data.V, s.chainIdMul)
|
cpy.data.V.Add(cpy.data.V, s.chainIdMul)
|
||||||
}
|
}
|
||||||
|
@ -71,7 +71,7 @@ func TestEIP155ChainId(t *testing.T) {
|
|||||||
t.Error("didn't expect tx to be protected")
|
t.Error("didn't expect tx to be protected")
|
||||||
}
|
}
|
||||||
|
|
||||||
if tx.ChainId().BitLen() > 0 {
|
if tx.ChainId().Sign() != 0 {
|
||||||
t.Error("expected chain id to be 0 got", tx.ChainId())
|
t.Error("expected chain id to be 0 got", tx.ChainId())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ import (
|
|||||||
|
|
||||||
// calculates the memory size required for a step
|
// calculates the memory size required for a step
|
||||||
func calcMemSize(off, l *big.Int) *big.Int {
|
func calcMemSize(off, l *big.Int) *big.Int {
|
||||||
if l.Cmp(common.Big0) == 0 {
|
if l.Sign() == 0 {
|
||||||
return common.Big0
|
return common.Big0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,7 +120,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
|
|||||||
snapshot = evm.StateDB.Snapshot()
|
snapshot = evm.StateDB.Snapshot()
|
||||||
)
|
)
|
||||||
if !evm.StateDB.Exist(addr) {
|
if !evm.StateDB.Exist(addr) {
|
||||||
if PrecompiledContracts[addr] == nil && evm.ChainConfig().IsEIP158(evm.BlockNumber) && value.BitLen() == 0 {
|
if PrecompiledContracts[addr] == nil && evm.ChainConfig().IsEIP158(evm.BlockNumber) && value.Sign() == 0 {
|
||||||
return nil, gas, nil
|
return nil, gas, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -273,7 +273,7 @@ func gasExp(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem
|
|||||||
func gasCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
|
func gasCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
|
||||||
var (
|
var (
|
||||||
gas = gt.Calls
|
gas = gt.Calls
|
||||||
transfersValue = stack.Back(2).BitLen() > 0
|
transfersValue = stack.Back(2).Sign() != 0
|
||||||
address = common.BigToAddress(stack.Back(1))
|
address = common.BigToAddress(stack.Back(1))
|
||||||
eip158 = evm.ChainConfig().IsEIP158(evm.BlockNumber)
|
eip158 = evm.ChainConfig().IsEIP158(evm.BlockNumber)
|
||||||
)
|
)
|
||||||
@ -316,7 +316,7 @@ func gasCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem
|
|||||||
|
|
||||||
func gasCallCode(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
|
func gasCallCode(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
|
||||||
gas := gt.Calls
|
gas := gt.Calls
|
||||||
if stack.Back(2).BitLen() > 0 {
|
if stack.Back(2).Sign() != 0 {
|
||||||
gas += params.CallValueTransferGas
|
gas += params.CallValueTransferGas
|
||||||
}
|
}
|
||||||
memoryGas, err := memoryGasCost(mem, memorySize)
|
memoryGas, err := memoryGasCost(mem, memorySize)
|
||||||
@ -362,7 +362,7 @@ func gasSuicide(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack,
|
|||||||
|
|
||||||
if eip158 {
|
if eip158 {
|
||||||
// if empty and transfers value
|
// if empty and transfers value
|
||||||
if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).BitLen() > 0 {
|
if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 {
|
||||||
gas += gt.CreateBySuicide
|
gas += gt.CreateBySuicide
|
||||||
}
|
}
|
||||||
} else if !evm.StateDB.Exist(address) {
|
} else if !evm.StateDB.Exist(address) {
|
||||||
|
@ -58,7 +58,7 @@ func opMul(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
|
|||||||
|
|
||||||
func opDiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
func opDiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
||||||
x, y := stack.pop(), stack.pop()
|
x, y := stack.pop(), stack.pop()
|
||||||
if y.Cmp(common.Big0) != 0 {
|
if y.Sign() != 0 {
|
||||||
stack.push(math.U256(x.Div(x, y)))
|
stack.push(math.U256(x.Div(x, y)))
|
||||||
} else {
|
} else {
|
||||||
stack.push(new(big.Int))
|
stack.push(new(big.Int))
|
||||||
@ -71,12 +71,12 @@ func opDiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
|
|||||||
|
|
||||||
func opSdiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
func opSdiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
||||||
x, y := math.S256(stack.pop()), math.S256(stack.pop())
|
x, y := math.S256(stack.pop()), math.S256(stack.pop())
|
||||||
if y.Cmp(common.Big0) == 0 {
|
if y.Sign() == 0 {
|
||||||
stack.push(new(big.Int))
|
stack.push(new(big.Int))
|
||||||
return nil, nil
|
return nil, nil
|
||||||
} else {
|
} else {
|
||||||
n := new(big.Int)
|
n := new(big.Int)
|
||||||
if evm.interpreter.intPool.get().Mul(x, y).Cmp(common.Big0) < 0 {
|
if evm.interpreter.intPool.get().Mul(x, y).Sign() < 0 {
|
||||||
n.SetInt64(-1)
|
n.SetInt64(-1)
|
||||||
} else {
|
} else {
|
||||||
n.SetInt64(1)
|
n.SetInt64(1)
|
||||||
@ -93,7 +93,7 @@ func opSdiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
|
|||||||
|
|
||||||
func opMod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
func opMod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
||||||
x, y := stack.pop(), stack.pop()
|
x, y := stack.pop(), stack.pop()
|
||||||
if y.Cmp(common.Big0) == 0 {
|
if y.Sign() == 0 {
|
||||||
stack.push(new(big.Int))
|
stack.push(new(big.Int))
|
||||||
} else {
|
} else {
|
||||||
stack.push(math.U256(x.Mod(x, y)))
|
stack.push(math.U256(x.Mod(x, y)))
|
||||||
@ -105,11 +105,11 @@ func opMod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
|
|||||||
func opSmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
func opSmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
||||||
x, y := math.S256(stack.pop()), math.S256(stack.pop())
|
x, y := math.S256(stack.pop()), math.S256(stack.pop())
|
||||||
|
|
||||||
if y.Cmp(common.Big0) == 0 {
|
if y.Sign() == 0 {
|
||||||
stack.push(new(big.Int))
|
stack.push(new(big.Int))
|
||||||
} else {
|
} else {
|
||||||
n := new(big.Int)
|
n := new(big.Int)
|
||||||
if x.Cmp(common.Big0) < 0 {
|
if x.Sign() < 0 {
|
||||||
n.SetInt64(-1)
|
n.SetInt64(-1)
|
||||||
} else {
|
} else {
|
||||||
n.SetInt64(1)
|
n.SetInt64(1)
|
||||||
@ -221,7 +221,7 @@ func opEq(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack
|
|||||||
|
|
||||||
func opIszero(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
func opIszero(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
||||||
x := stack.pop()
|
x := stack.pop()
|
||||||
if x.Cmp(common.Big0) > 0 {
|
if x.Sign() > 0 {
|
||||||
stack.push(new(big.Int))
|
stack.push(new(big.Int))
|
||||||
} else {
|
} else {
|
||||||
stack.push(evm.interpreter.intPool.get().SetUint64(1))
|
stack.push(evm.interpreter.intPool.get().SetUint64(1))
|
||||||
@ -252,10 +252,11 @@ func opXor(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
|
|||||||
evm.interpreter.intPool.put(y)
|
evm.interpreter.intPool.put(y)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func opByte(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
func opByte(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
||||||
th, val := stack.pop(), stack.pop()
|
th, val := stack.pop(), stack.pop()
|
||||||
if th.Cmp(big.NewInt(32)) < 0 {
|
if th.Cmp(big.NewInt(32)) < 0 {
|
||||||
byte := evm.interpreter.intPool.get().SetInt64(int64(common.LeftPadBytes(val.Bytes(), 32)[th.Int64()]))
|
byte := evm.interpreter.intPool.get().SetInt64(int64(math.PaddedBigBytes(val, 32)[th.Int64()]))
|
||||||
stack.push(byte)
|
stack.push(byte)
|
||||||
} else {
|
} else {
|
||||||
stack.push(new(big.Int))
|
stack.push(new(big.Int))
|
||||||
@ -505,7 +506,7 @@ func opJump(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
|
|||||||
}
|
}
|
||||||
func opJumpi(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
func opJumpi(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
||||||
pos, cond := stack.pop(), stack.pop()
|
pos, cond := stack.pop(), stack.pop()
|
||||||
if cond.BitLen() > 0 {
|
if cond.Sign() != 0 {
|
||||||
if !contract.jumpdests.has(contract.CodeHash, contract.Code, pos) {
|
if !contract.jumpdests.has(contract.CodeHash, contract.Code, pos) {
|
||||||
nop := contract.GetOp(pos.Uint64())
|
nop := contract.GetOp(pos.Uint64())
|
||||||
return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos)
|
return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos)
|
||||||
@ -583,7 +584,7 @@ func opCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
|
|||||||
// Get the arguments from the memory
|
// Get the arguments from the memory
|
||||||
args := memory.Get(inOffset.Int64(), inSize.Int64())
|
args := memory.Get(inOffset.Int64(), inSize.Int64())
|
||||||
|
|
||||||
if value.BitLen() > 0 {
|
if value.Sign() != 0 {
|
||||||
gas += params.CallStipend
|
gas += params.CallStipend
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -616,7 +617,7 @@ func opCallCode(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack
|
|||||||
// Get the arguments from the memory
|
// Get the arguments from the memory
|
||||||
args := memory.Get(inOffset.Int64(), inSize.Int64())
|
args := memory.Get(inOffset.Int64(), inSize.Int64())
|
||||||
|
|
||||||
if value.BitLen() > 0 {
|
if value.Sign() != 0 {
|
||||||
gas += params.CallStipend
|
gas += params.CallStipend
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/common/math"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Storage map[common.Hash]common.Hash
|
type Storage map[common.Hash]common.Hash
|
||||||
@ -180,7 +181,7 @@ func StdErrFormat(logs []StructLog) {
|
|||||||
fmt.Fprintln(os.Stderr, "STACK =", len(log.Stack))
|
fmt.Fprintln(os.Stderr, "STACK =", len(log.Stack))
|
||||||
|
|
||||||
for i := len(log.Stack) - 1; i >= 0; i-- {
|
for i := len(log.Stack) - 1; i >= 0; i-- {
|
||||||
fmt.Fprintf(os.Stderr, "%04d: %x\n", len(log.Stack)-i-1, common.LeftPadBytes(log.Stack[i].Bytes(), 32))
|
fmt.Fprintf(os.Stderr, "%04d: %x\n", len(log.Stack)-i-1, math.PaddedBigBytes(log.Stack[i], 32))
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxMem = 10
|
const maxMem = 10
|
||||||
|
@ -36,6 +36,8 @@ import (
|
|||||||
"math/big"
|
"math/big"
|
||||||
"sync"
|
"sync"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common/math"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -230,8 +232,8 @@ func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int,
|
|||||||
|
|
||||||
// Do the multiplication in C, updating point.
|
// Do the multiplication in C, updating point.
|
||||||
point := make([]byte, 64)
|
point := make([]byte, 64)
|
||||||
readBits(point[:32], Bx)
|
math.ReadBits(Bx, point[:32])
|
||||||
readBits(point[32:], By)
|
math.ReadBits(By, point[32:])
|
||||||
pointPtr := (*C.uchar)(unsafe.Pointer(&point[0]))
|
pointPtr := (*C.uchar)(unsafe.Pointer(&point[0]))
|
||||||
scalarPtr := (*C.uchar)(unsafe.Pointer(&scalar[0]))
|
scalarPtr := (*C.uchar)(unsafe.Pointer(&scalar[0]))
|
||||||
res := C.secp256k1_pubkey_scalar_mul(context, pointPtr, scalarPtr)
|
res := C.secp256k1_pubkey_scalar_mul(context, pointPtr, scalarPtr)
|
||||||
|
@ -1,39 +0,0 @@
|
|||||||
// Copyright 2015 The go-ethereum Authors
|
|
||||||
// This file is part of the go-ethereum library.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package secp256k1
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/hex"
|
|
||||||
"math/big"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestReadBits(t *testing.T) {
|
|
||||||
check := func(input string) {
|
|
||||||
want, _ := hex.DecodeString(input)
|
|
||||||
int, _ := new(big.Int).SetString(input, 16)
|
|
||||||
buf := make([]byte, len(want))
|
|
||||||
readBits(buf, int)
|
|
||||||
if !bytes.Equal(buf, want) {
|
|
||||||
t.Errorf("have: %x\nwant: %x", buf, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
check("000000000000000000000000000000000000000000000000000000FEFCF3F8F0")
|
|
||||||
check("0000000000012345000000000000000000000000000000000000FEFCF3F8F0")
|
|
||||||
check("18F8F8F1000111000110011100222004330052300000000000000000FEFCF3F8F0")
|
|
||||||
}
|
|
@ -38,7 +38,6 @@ import "C"
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"math/big"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -129,16 +128,3 @@ func checkSignature(sig []byte) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// reads num into buf as big-endian bytes.
|
|
||||||
func readBits(buf []byte, num *big.Int) {
|
|
||||||
const wordLen = int(unsafe.Sizeof(big.Word(0)))
|
|
||||||
i := len(buf)
|
|
||||||
for _, d := range num.Bits() {
|
|
||||||
for j := 0; j < wordLen && i > 0; j++ {
|
|
||||||
i--
|
|
||||||
buf[i] = byte(d)
|
|
||||||
d >>= 8
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -24,6 +24,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common/math"
|
||||||
"github.com/ethereum/go-ethereum/crypto/randentropy"
|
"github.com/ethereum/go-ethereum/crypto/randentropy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -35,9 +36,7 @@ func generateKeyPair() (pubkey, privkey []byte) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
pubkey = elliptic.Marshal(S256(), key.X, key.Y)
|
pubkey = elliptic.Marshal(S256(), key.X, key.Y)
|
||||||
privkey = make([]byte, 32)
|
return pubkey, math.PaddedBigBytes(key.D, 32)
|
||||||
readBits(privkey, key.D)
|
|
||||||
return pubkey, privkey
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func randSig() []byte {
|
func randSig() []byte {
|
||||||
|
@ -23,7 +23,7 @@ import (
|
|||||||
"crypto/elliptic"
|
"crypto/elliptic"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common/math"
|
||||||
"github.com/ethereum/go-ethereum/crypto/secp256k1"
|
"github.com/ethereum/go-ethereum/crypto/secp256k1"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -53,7 +53,7 @@ func Sign(hash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {
|
|||||||
if len(hash) != 32 {
|
if len(hash) != 32 {
|
||||||
return nil, fmt.Errorf("hash is required to be exactly 32 bytes (%d)", len(hash))
|
return nil, fmt.Errorf("hash is required to be exactly 32 bytes (%d)", len(hash))
|
||||||
}
|
}
|
||||||
seckey := common.LeftPadBytes(prv.D.Bytes(), prv.Params().BitSize/8)
|
seckey := math.PaddedBigBytes(prv.D, prv.Params().BitSize/8)
|
||||||
defer zeroBytes(seckey)
|
defer zeroBytes(seckey)
|
||||||
return secp256k1.Sign(hash, seckey)
|
return secp256k1.Sign(hash, seckey)
|
||||||
}
|
}
|
||||||
|
@ -596,10 +596,10 @@ func (s *PublicBlockChainAPI) doCall(ctx context.Context, args CallArgs, blockNr
|
|||||||
}
|
}
|
||||||
// Set default gas & gas price if none were set
|
// Set default gas & gas price if none were set
|
||||||
gas, gasPrice := args.Gas.ToInt(), args.GasPrice.ToInt()
|
gas, gasPrice := args.Gas.ToInt(), args.GasPrice.ToInt()
|
||||||
if gas.BitLen() == 0 {
|
if gas.Sign() == 0 {
|
||||||
gas = big.NewInt(50000000)
|
gas = big.NewInt(50000000)
|
||||||
}
|
}
|
||||||
if gasPrice.BitLen() == 0 {
|
if gasPrice.Sign() == 0 {
|
||||||
gasPrice = new(big.Int).SetUint64(defaultGasPrice)
|
gasPrice = new(big.Int).SetUint64(defaultGasPrice)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -653,7 +653,7 @@ func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNr r
|
|||||||
func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs) (*hexutil.Big, error) {
|
func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs) (*hexutil.Big, error) {
|
||||||
// Binary search the gas requirement, as it may be higher than the amount used
|
// Binary search the gas requirement, as it may be higher than the amount used
|
||||||
var lo, hi uint64
|
var lo, hi uint64
|
||||||
if (*big.Int)(&args.Gas).BitLen() > 0 {
|
if (*big.Int)(&args.Gas).Sign() != 0 {
|
||||||
hi = (*big.Int)(&args.Gas).Uint64()
|
hi = (*big.Int)(&args.Gas).Uint64()
|
||||||
} else {
|
} else {
|
||||||
// Retrieve the current pending block to act as the gas ceiling
|
// Retrieve the current pending block to act as the gas ceiling
|
||||||
@ -720,7 +720,7 @@ func FormatLogs(structLogs []vm.StructLog) []StructLogRes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i, stackValue := range trace.Stack {
|
for i, stackValue := range trace.Stack {
|
||||||
formattedStructLogs[index].Stack[i] = fmt.Sprintf("%x", common.LeftPadBytes(stackValue.Bytes(), 32))
|
formattedStructLogs[index].Stack[i] = fmt.Sprintf("%x", math.PaddedBigBytes(stackValue, 32))
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i+32 <= len(trace.Memory); i += 32 {
|
for i := 0; i+32 <= len(trace.Memory); i += 32 {
|
||||||
|
@ -202,7 +202,7 @@ func (self *StateObject) Copy() *StateObject {
|
|||||||
|
|
||||||
// empty returns whether the account is considered empty.
|
// empty returns whether the account is considered empty.
|
||||||
func (self *StateObject) empty() bool {
|
func (self *StateObject) empty() bool {
|
||||||
return self.nonce == 0 && self.balance.BitLen() == 0 && bytes.Equal(self.codeHash, emptyCodeHash)
|
return self.nonce == 0 && self.balance.Sign() == 0 && bytes.Equal(self.codeHash, emptyCodeHash)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Balance returns the account balance
|
// Balance returns the account balance
|
||||||
|
@ -365,7 +365,7 @@ func (pool *TxPool) validateTx(ctx context.Context, tx *types.Transaction) error
|
|||||||
// Transactions can't be negative. This may never happen
|
// Transactions can't be negative. This may never happen
|
||||||
// using RLP decoded transactions but may occur if you create
|
// using RLP decoded transactions but may occur if you create
|
||||||
// a transaction using the RPC for example.
|
// a transaction using the RPC for example.
|
||||||
if tx.Value().Cmp(common.Big0) < 0 {
|
if tx.Value().Sign() < 0 {
|
||||||
return core.ErrNegativeValue
|
return core.ErrNegativeValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +22,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -120,11 +119,11 @@ func (self *Swap) SetRemote(remote *Profile) {
|
|||||||
self.lock.Lock()
|
self.lock.Lock()
|
||||||
|
|
||||||
self.remote = remote
|
self.remote = remote
|
||||||
if self.Sells && (remote.BuyAt.Cmp(common.Big0) <= 0 || self.local.SellAt.Cmp(common.Big0) <= 0 || remote.BuyAt.Cmp(self.local.SellAt) < 0) {
|
if self.Sells && (remote.BuyAt.Sign() <= 0 || self.local.SellAt.Sign() <= 0 || remote.BuyAt.Cmp(self.local.SellAt) < 0) {
|
||||||
self.Out.Stop()
|
self.Out.Stop()
|
||||||
self.Sells = false
|
self.Sells = false
|
||||||
}
|
}
|
||||||
if self.Buys && (remote.SellAt.Cmp(common.Big0) <= 0 || self.local.BuyAt.Cmp(common.Big0) <= 0 || self.local.BuyAt.Cmp(self.remote.SellAt) < 0) {
|
if self.Buys && (remote.SellAt.Sign() <= 0 || self.local.BuyAt.Sign() <= 0 || self.local.BuyAt.Cmp(self.remote.SellAt) < 0) {
|
||||||
self.In.Stop()
|
self.In.Stop()
|
||||||
self.Buys = false
|
self.Buys = false
|
||||||
}
|
}
|
||||||
|
@ -71,7 +71,7 @@ func checkLogs(tlog []Log, logs []*types.Log) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
genBloom := common.LeftPadBytes(types.LogsBloom([]*types.Log{logs[i]}).Bytes(), 256)
|
genBloom := math.PaddedBigBytes(types.LogsBloom([]*types.Log{logs[i]}), 256)
|
||||||
|
|
||||||
if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
|
if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
|
||||||
return fmt.Errorf("bloom mismatch")
|
return fmt.Errorf("bloom mismatch")
|
||||||
|
Loading…
Reference in New Issue
Block a user