diff options
author | Felix Lange <fjl@users.noreply.github.com> | 2017-02-27 05:21:51 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2017-02-27 05:21:51 +0800 |
commit | 5c8fe28b725bd9b128edceae3215132ea741641b (patch) | |
tree | 4bcbfeb1a21536edbba06d1715752999faa7f085 /core | |
parent | 50ee279f2585e9e2eee30f0e1d4a3bf5f781bd72 (diff) | |
download | go-tangerine-5c8fe28b725bd9b128edceae3215132ea741641b.tar.gz go-tangerine-5c8fe28b725bd9b128edceae3215132ea741641b.tar.zst go-tangerine-5c8fe28b725bd9b128edceae3215132ea741641b.zip |
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
Diffstat (limited to 'core')
-rw-r--r-- | core/bench_test.go | 3 | ||||
-rw-r--r-- | core/block_validator.go | 9 | ||||
-rw-r--r-- | core/database_util_test.go | 11 | ||||
-rw-r--r-- | core/genesis.go | 45 | ||||
-rw-r--r-- | core/state_transition.go | 3 | ||||
-rw-r--r-- | core/tx_pool_test.go | 5 | ||||
-rw-r--r-- | core/types/bloom9.go | 6 | ||||
-rw-r--r-- | core/vm/common.go | 20 | ||||
-rw-r--r-- | core/vm/contracts.go | 9 | ||||
-rw-r--r-- | core/vm/instructions.go | 60 | ||||
-rw-r--r-- | core/vm/memory_table.go | 8 | ||||
-rw-r--r-- | core/vm/runtime/runtime_test.go | 4 |
12 files changed, 111 insertions, 72 deletions
diff --git a/core/bench_test.go b/core/bench_test.go index 59b5ad758..8a1600557 100644 --- a/core/bench_test.go +++ b/core/bench_test.go @@ -24,6 +24,7 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" @@ -73,7 +74,7 @@ var ( // This is the content of the genesis block used by the benchmarks. benchRootKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") benchRootAddr = crypto.PubkeyToAddress(benchRootKey.PublicKey) - benchRootFunds = common.BigPow(2, 100) + benchRootFunds = math.BigPow(2, 100) ) // genValueTx returns a block generator that includes a single diff --git a/core/block_validator.go b/core/block_validator.go index a23a4134b..117974ee6 100644 --- a/core/block_validator.go +++ b/core/block_validator.go @@ -22,6 +22,7 @@ import ( "time" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" @@ -209,7 +210,7 @@ func ValidateHeader(config *params.ChainConfig, pow pow.PoW, header *types.Heade } if uncle { - if header.Time.Cmp(common.MaxBig) == 1 { + if header.Time.Cmp(math.MaxBig256) == 1 { return BlockTSTooBigErr } } else { @@ -344,7 +345,7 @@ func calcDifficultyFrontier(time, parentTime uint64, parentNumber, parentDiff *b expDiff := periodCount.Sub(periodCount, common.Big2) expDiff.Exp(common.Big2, expDiff, nil) diff.Add(diff, expDiff) - diff = common.BigMax(diff, params.MinimumDifficulty) + diff = math.BigMax(diff, params.MinimumDifficulty) } return diff @@ -372,13 +373,13 @@ func CalcGasLimit(parent *types.Block) *big.Int { */ gl := new(big.Int).Sub(parent.GasLimit(), decay) gl = gl.Add(gl, contrib) - gl.Set(common.BigMax(gl, params.MinGasLimit)) + gl.Set(math.BigMax(gl, params.MinGasLimit)) // however, if we're now below the target (TargetGasLimit) we increase the // limit as much as we can (parentGasLimit / 1024 -1) if gl.Cmp(params.TargetGasLimit) < 0 { gl.Add(parent.GasLimit(), decay) - gl.Set(common.BigMin(gl, params.TargetGasLimit)) + gl.Set(math.BigMin(gl, params.TargetGasLimit)) } return gl } diff --git a/core/database_util_test.go b/core/database_util_test.go index d96aa71ba..0af4e4920 100644 --- a/core/database_util_test.go +++ b/core/database_util_test.go @@ -25,6 +25,7 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto/sha3" @@ -53,11 +54,11 @@ func (d *diffTest) UnmarshalJSON(b []byte) (err error) { return err } - d.ParentTimestamp = common.String2Big(ext.ParentTimestamp).Uint64() - d.ParentDifficulty = common.String2Big(ext.ParentDifficulty) - d.CurrentTimestamp = common.String2Big(ext.CurrentTimestamp).Uint64() - d.CurrentBlocknumber = common.String2Big(ext.CurrentBlocknumber) - d.CurrentDifficulty = common.String2Big(ext.CurrentDifficulty) + d.ParentTimestamp = math.MustParseUint64(ext.ParentTimestamp) + d.ParentDifficulty = math.MustParseBig256(ext.ParentDifficulty) + d.CurrentTimestamp = math.MustParseUint64(ext.CurrentTimestamp) + d.CurrentBlocknumber = math.MustParseBig256(ext.CurrentBlocknumber) + d.CurrentDifficulty = math.MustParseBig256(ext.CurrentDifficulty) return nil } diff --git a/core/genesis.go b/core/genesis.go index a015f04c1..58d3440d6 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -28,6 +28,7 @@ import ( "strings" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" @@ -67,23 +68,48 @@ func WriteGenesisBlock(chainDb ethdb.Database, reader io.Reader) (*types.Block, // creating with empty hash always works statedb, _ := state.New(common.Hash{}, chainDb) for addr, account := range genesis.Alloc { + balance, ok := math.ParseBig256(account.Balance) + if !ok { + return nil, fmt.Errorf("invalid balance for account %s: %q", addr, account.Balance) + } + nonce, ok := math.ParseUint64(account.Nonce) + if !ok { + return nil, fmt.Errorf("invalid nonce for account %s: %q", addr, account.Nonce) + } + address := common.HexToAddress(addr) - statedb.AddBalance(address, common.String2Big(account.Balance)) + statedb.AddBalance(address, balance) statedb.SetCode(address, common.FromHex(account.Code)) - statedb.SetNonce(address, common.String2Big(account.Nonce).Uint64()) + statedb.SetNonce(address, nonce) for key, value := range account.Storage { statedb.SetState(address, common.HexToHash(key), common.HexToHash(value)) } } root, stateBatch := statedb.CommitBatch(false) - difficulty := common.String2Big(genesis.Difficulty) + difficulty, ok := math.ParseBig256(genesis.Difficulty) + if !ok { + return nil, fmt.Errorf("invalid difficulty: %q", genesis.Difficulty) + } + gaslimit, ok := math.ParseUint64(genesis.GasLimit) + if !ok { + return nil, fmt.Errorf("invalid gas limit: %q", genesis.GasLimit) + } + nonce, ok := math.ParseUint64(genesis.Nonce) + if !ok { + return nil, fmt.Errorf("invalid nonce: %q", genesis.Nonce) + } + timestamp, ok := math.ParseBig256(genesis.Timestamp) + if !ok { + return nil, fmt.Errorf("invalid timestamp: %q", genesis.Timestamp) + } + block := types.NewBlock(&types.Header{ - Nonce: types.EncodeNonce(common.String2Big(genesis.Nonce).Uint64()), - Time: common.String2Big(genesis.Timestamp), + Nonce: types.EncodeNonce(nonce), + Time: timestamp, ParentHash: common.HexToHash(genesis.ParentHash), Extra: common.FromHex(genesis.ExtraData), - GasLimit: common.String2Big(genesis.GasLimit), + GasLimit: new(big.Int).SetUint64(gaslimit), Difficulty: difficulty, MixDigest: common.HexToHash(genesis.Mixhash), Coinbase: common.HexToAddress(genesis.Coinbase), @@ -153,7 +179,7 @@ func WriteGenesisBlockForTesting(db ethdb.Database, accounts ...GenesisAccount) if i != 0 { accountJson += "," } - accountJson += fmt.Sprintf(`"0x%x":{"balance":"0x%x"}`, account.Address, account.Balance.Bytes()) + accountJson += fmt.Sprintf(`"0x%x":{"balance":"%d"}`, account.Address, account.Balance) } accountJson += "}" @@ -163,7 +189,10 @@ func WriteGenesisBlockForTesting(db ethdb.Database, accounts ...GenesisAccount) "difficulty":"0x%x", "alloc": %s }`, types.EncodeNonce(0), params.GenesisGasLimit.Bytes(), params.GenesisDifficulty.Bytes(), accountJson) - block, _ := WriteGenesisBlock(db, strings.NewReader(testGenesis)) + block, err := WriteGenesisBlock(db, strings.NewReader(testGenesis)) + if err != nil { + panic(err) + } return block } diff --git a/core/state_transition.go b/core/state_transition.go index 8e7891b96..98a24af2b 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -22,6 +22,7 @@ import ( "math/big" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" @@ -281,7 +282,7 @@ func (self *StateTransition) refundGas() { // Apply refund counter, capped to half of the used gas. uhalf := remaining.Div(self.gasUsed(), common.Big2) - refund := common.BigMin(uhalf, self.state.GetRefund()) + refund := math.BigMin(uhalf, self.state.GetRefund()) self.gas += refund.Uint64() self.state.AddBalance(sender.Address(), refund.Mul(refund, self.gasPrice)) diff --git a/core/tx_pool_test.go b/core/tx_pool_test.go index 98a34b757..3a37dd2b9 100644 --- a/core/tx_pool_test.go +++ b/core/tx_pool_test.go @@ -29,6 +29,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/params" ) func transaction(nonce uint64, gaslimit *big.Int, key *ecdsa.PrivateKey) *types.Transaction { @@ -65,7 +66,7 @@ func TestStateChangeDuringPoolReset(t *testing.T) { ) // setup pool with 2 transaction in it - statedb.SetBalance(address, new(big.Int).Mul(common.Big1, common.Ether)) + statedb.SetBalance(address, new(big.Int).SetUint64(params.Ether)) tx0 := transaction(0, big.NewInt(100000), key) tx1 := transaction(1, big.NewInt(100000), key) @@ -82,7 +83,7 @@ func TestStateChangeDuringPoolReset(t *testing.T) { statedb, _ = state.New(common.Hash{}, db) // simulate that the new head block included tx0 and tx1 statedb.SetNonce(address, 2) - statedb.SetBalance(address, new(big.Int).Mul(common.Big1, common.Ether)) + statedb.SetBalance(address, new(big.Int).SetUint64(params.Ether)) trigger = false } return stdb, nil diff --git a/core/types/bloom9.go b/core/types/bloom9.go index 32aa47a41..2a37b5977 100644 --- a/core/types/bloom9.go +++ b/core/types/bloom9.go @@ -20,7 +20,6 @@ import ( "fmt" "math/big" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" ) @@ -60,7 +59,7 @@ func (b *Bloom) Add(d *big.Int) { // Big converts b to a big integer. func (b Bloom) Big() *big.Int { - return common.Bytes2Big(b[:]) + return new(big.Int).SetBytes(b[:]) } func (b Bloom) Bytes() []byte { @@ -72,7 +71,8 @@ func (b Bloom) Test(test *big.Int) bool { } func (b Bloom) TestBytes(test []byte) bool { - return b.Test(common.BytesToBig(test)) + return b.Test(new(big.Int).SetBytes(test)) + } // MarshalJSON encodes b as a hex string with 0x prefix. diff --git a/core/vm/common.go b/core/vm/common.go index b7b9a6a9c..ef40c4a95 100644 --- a/core/vm/common.go +++ b/core/vm/common.go @@ -17,15 +17,10 @@ package vm import ( - "math" "math/big" "github.com/ethereum/go-ethereum/common" -) - -var ( - U256 = common.U256 // Shortcut to common.U256 - S256 = common.S256 // Shortcut to common.S256 + "github.com/ethereum/go-ethereum/common/math" ) // calculates the memory size required for a step @@ -42,8 +37,8 @@ func calcMemSize(off, l *big.Int) *big.Int { func getData(data []byte, start, size *big.Int) []byte { dlen := big.NewInt(int64(len(data))) - s := common.BigMin(start, dlen) - e := common.BigMin(new(big.Int).Add(s, size), dlen) + s := math.BigMin(start, dlen) + e := math.BigMin(new(big.Int).Add(s, size), dlen) return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64())) } @@ -61,3 +56,12 @@ func toWordSize(size uint64) uint64 { return (size + 31) / 32 } + +func allZero(b []byte) bool { + for _, byte := range b { + if byte != 0 { + return false + } + } + return true +} diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 2793d2aa7..0479adfda 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -19,6 +19,7 @@ package vm import ( "crypto/sha256" "fmt" + "math/big" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" @@ -69,13 +70,13 @@ func (c *ecrecover) Run(in []byte) []byte { // "in" is (hash, v, r, s), each 32 bytes // but for ecrecover we want (r, s, v) - r := common.BytesToBig(in[64:96]) - s := common.BytesToBig(in[96:128]) + r := new(big.Int).SetBytes(in[64:96]) + s := new(big.Int).SetBytes(in[96:128]) v := in[63] - 27 // tighter sig s values in homestead only apply to tx sigs - if common.Bytes2Big(in[32:63]).BitLen() > 0 || !crypto.ValidateSignatureValues(v, r, s, false) { - log.Trace(fmt.Sprintf("ECRECOVER error: v, r or s value invalid")) + if !allZero(in[32:63]) || !crypto.ValidateSignatureValues(v, r, s, false) { + log.Trace("ECRECOVER error: v, r or s value invalid") return nil } // v needs to be at the end for libsecp256k1 diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 39e5c0587..8b7bcc4d6 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -31,7 +31,7 @@ var bigZero = new(big.Int) func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { x, y := stack.pop(), stack.pop() - stack.push(U256(x.Add(x, y))) + stack.push(math.U256(x.Add(x, y))) evm.interpreter.intPool.put(y) @@ -40,7 +40,7 @@ func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac func opSub(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { x, y := stack.pop(), stack.pop() - stack.push(U256(x.Sub(x, y))) + stack.push(math.U256(x.Sub(x, y))) evm.interpreter.intPool.put(y) @@ -49,7 +49,7 @@ func opSub(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac func opMul(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { x, y := stack.pop(), stack.pop() - stack.push(U256(x.Mul(x, y))) + stack.push(math.U256(x.Mul(x, y))) evm.interpreter.intPool.put(y) @@ -59,7 +59,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) { x, y := stack.pop(), stack.pop() if y.Cmp(common.Big0) != 0 { - stack.push(U256(x.Div(x, y))) + stack.push(math.U256(x.Div(x, y))) } else { stack.push(new(big.Int)) } @@ -70,7 +70,7 @@ 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) { - x, y := S256(stack.pop()), S256(stack.pop()) + x, y := math.S256(stack.pop()), math.S256(stack.pop()) if y.Cmp(common.Big0) == 0 { stack.push(new(big.Int)) return nil, nil @@ -85,7 +85,7 @@ func opSdiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta res := x.Div(x.Abs(x), y.Abs(y)) res.Mul(res, n) - stack.push(U256(res)) + stack.push(math.U256(res)) } evm.interpreter.intPool.put(y) return nil, nil @@ -96,14 +96,14 @@ func opMod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac if y.Cmp(common.Big0) == 0 { stack.push(new(big.Int)) } else { - stack.push(U256(x.Mod(x, y))) + stack.push(math.U256(x.Mod(x, y))) } evm.interpreter.intPool.put(y) return nil, nil } func opSmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - x, y := S256(stack.pop()), S256(stack.pop()) + x, y := math.S256(stack.pop()), math.S256(stack.pop()) if y.Cmp(common.Big0) == 0 { stack.push(new(big.Int)) @@ -118,7 +118,7 @@ func opSmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta res := x.Mod(x.Abs(x), y.Abs(y)) res.Mul(res, n) - stack.push(U256(res)) + stack.push(math.U256(res)) } evm.interpreter.intPool.put(y) return nil, nil @@ -140,13 +140,13 @@ func opSignExtend(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stac num := stack.pop() mask := back.Lsh(common.Big1, bit) mask.Sub(mask, common.Big1) - if common.BitTest(num, int(bit)) { + if num.Bit(int(bit)) > 0 { num.Or(num, mask.Not(mask)) } else { num.And(num, mask) } - stack.push(U256(num)) + stack.push(math.U256(num)) } evm.interpreter.intPool.put(back) @@ -155,7 +155,7 @@ func opSignExtend(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stac func opNot(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { x := stack.pop() - stack.push(U256(x.Not(x))) + stack.push(math.U256(x.Not(x))) return nil, nil } @@ -184,8 +184,8 @@ func opGt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack } func opSlt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - x, y := S256(stack.pop()), S256(stack.pop()) - if x.Cmp(S256(y)) < 0 { + x, y := math.S256(stack.pop()), math.S256(stack.pop()) + if x.Cmp(math.S256(y)) < 0 { stack.push(evm.interpreter.intPool.get().SetUint64(1)) } else { stack.push(new(big.Int)) @@ -196,7 +196,7 @@ func opSlt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac } func opSgt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - x, y := S256(stack.pop()), S256(stack.pop()) + x, y := math.S256(stack.pop()), math.S256(stack.pop()) if x.Cmp(y) > 0 { stack.push(evm.interpreter.intPool.get().SetUint64(1)) } else { @@ -269,7 +269,7 @@ func opAddmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S if z.Cmp(bigZero) > 0 { add := x.Add(x, y) add.Mod(add, z) - stack.push(U256(add)) + stack.push(math.U256(add)) } else { stack.push(new(big.Int)) } @@ -282,7 +282,7 @@ func opMulmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S if z.Cmp(bigZero) > 0 { mul := x.Mul(x, y) mul.Mod(mul, z) - stack.push(U256(mul)) + stack.push(math.U256(mul)) } else { stack.push(new(big.Int)) } @@ -300,14 +300,14 @@ func opSha3(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta evm.StateDB.AddPreimage(common.BytesToHash(hash), data) } - stack.push(common.BytesToBig(hash)) + stack.push(new(big.Int).SetBytes(hash)) evm.interpreter.intPool.put(offset, size) return nil, nil } func opAddress(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(common.Bytes2Big(contract.Address().Bytes())) + stack.push(contract.Address().Big()) return nil, nil } @@ -335,7 +335,7 @@ func opCallValue(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack } func opCalldataLoad(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(common.Bytes2Big(getData(contract.Input, stack.pop(), common.Big32))) + stack.push(new(big.Int).SetBytes(getData(contract.Input, stack.pop(), common.Big32))) return nil, nil } @@ -427,22 +427,22 @@ func opCoinbase(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack } func opTimestamp(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(U256(new(big.Int).Set(evm.Time))) + stack.push(math.U256(new(big.Int).Set(evm.Time))) return nil, nil } func opNumber(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(U256(new(big.Int).Set(evm.BlockNumber))) + stack.push(math.U256(new(big.Int).Set(evm.BlockNumber))) return nil, nil } func opDifficulty(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(U256(new(big.Int).Set(evm.Difficulty))) + stack.push(math.U256(new(big.Int).Set(evm.Difficulty))) return nil, nil } func opGasLimit(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(U256(new(big.Int).Set(evm.GasLimit))) + stack.push(math.U256(new(big.Int).Set(evm.GasLimit))) return nil, nil } @@ -453,7 +453,7 @@ func opPop(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac func opMload(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { offset := stack.pop() - val := common.BigD(memory.Get(offset.Int64(), 32)) + val := new(big.Int).SetBytes(memory.Get(offset.Int64(), 32)) stack.push(val) evm.interpreter.intPool.put(offset) @@ -463,7 +463,7 @@ func opMload(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *St func opMstore(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { // pop value of the stack mStart, val := stack.pop(), stack.pop() - memory.Set(mStart.Uint64(), 32, common.BigToBytes(val, 256)) + memory.Set(mStart.Uint64(), 32, math.PaddedBigBytes(val, 32)) evm.interpreter.intPool.put(mStart, val) return nil, nil @@ -505,7 +505,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) { pos, cond := stack.pop(), stack.pop() - if cond.Cmp(common.BigTrue) >= 0 { + if cond.BitLen() > 0 { if !contract.jumpdests.has(contract.CodeHash, contract.Code, pos) { nop := contract.GetOp(pos.Uint64()) return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos) @@ -572,7 +572,7 @@ func opCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta gas := stack.pop().Uint64() // pop gas and value of the stack. addr, value := stack.pop(), stack.pop() - value = U256(value) + value = math.U256(value) // pop input size and offset inOffset, inSize := stack.pop(), stack.pop() // pop return size and offset @@ -605,7 +605,7 @@ func opCallCode(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack gas := stack.pop().Uint64() // pop gas and value of the stack. addr, value := stack.pop(), stack.pop() - value = U256(value) + value = math.U256(value) // pop input size and offset inOffset, inSize := stack.pop(), stack.pop() // pop return size and offset @@ -711,7 +711,7 @@ func makeLog(size int) executionFunc { func makePush(size uint64, bsize *big.Int) executionFunc { return func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { byts := getData(contract.Code, evm.interpreter.intPool.get().SetUint64(*pc+1), bsize) - stack.push(common.Bytes2Big(byts)) + stack.push(new(big.Int).SetBytes(byts)) *pc += size return nil, nil } diff --git a/core/vm/memory_table.go b/core/vm/memory_table.go index 4db994837..3141a2f61 100644 --- a/core/vm/memory_table.go +++ b/core/vm/memory_table.go @@ -3,7 +3,7 @@ package vm import ( "math/big" - "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/math" ) func memorySha3(stack *Stack) *big.Int { @@ -42,20 +42,20 @@ func memoryCall(stack *Stack) *big.Int { x := calcMemSize(stack.Back(5), stack.Back(6)) y := calcMemSize(stack.Back(3), stack.Back(4)) - return common.BigMax(x, y) + return math.BigMax(x, y) } func memoryCallCode(stack *Stack) *big.Int { x := calcMemSize(stack.Back(5), stack.Back(6)) y := calcMemSize(stack.Back(3), stack.Back(4)) - return common.BigMax(x, y) + return math.BigMax(x, y) } func memoryDelegateCall(stack *Stack) *big.Int { x := calcMemSize(stack.Back(4), stack.Back(5)) y := calcMemSize(stack.Back(2), stack.Back(3)) - return common.BigMax(x, y) + return math.BigMax(x, y) } func memoryReturn(stack *Stack) *big.Int { diff --git a/core/vm/runtime/runtime_test.go b/core/vm/runtime/runtime_test.go index 8ad74a89a..fe39e97a0 100644 --- a/core/vm/runtime/runtime_test.go +++ b/core/vm/runtime/runtime_test.go @@ -87,7 +87,7 @@ func TestExecute(t *testing.T) { t.Fatal("didn't expect error", err) } - num := common.BytesToBig(ret) + num := new(big.Int).SetBytes(ret) if num.Cmp(big.NewInt(10)) != 0 { t.Error("Expected 10, got", num) } @@ -111,7 +111,7 @@ func TestCall(t *testing.T) { t.Fatal("didn't expect error", err) } - num := common.BytesToBig(ret) + num := new(big.Int).SetBytes(ret) if num.Cmp(big.NewInt(10)) != 0 { t.Error("Expected 10, got", num) } |