github.com/snowblossomcoin/go-ethereum@v1.9.25/core/vm/instructions.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package vm
    18  
    19  import (
    20  	"github.com/ethereum/go-ethereum/common"
    21  	"github.com/ethereum/go-ethereum/core/types"
    22  	"github.com/ethereum/go-ethereum/params"
    23  	"github.com/holiman/uint256"
    24  	"golang.org/x/crypto/sha3"
    25  )
    26  
    27  func opAdd(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    28  	x, y := callContext.stack.pop(), callContext.stack.peek()
    29  	y.Add(&x, y)
    30  	return nil, nil
    31  }
    32  
    33  func opSub(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    34  	x, y := callContext.stack.pop(), callContext.stack.peek()
    35  	y.Sub(&x, y)
    36  	return nil, nil
    37  }
    38  
    39  func opMul(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    40  	x, y := callContext.stack.pop(), callContext.stack.peek()
    41  	y.Mul(&x, y)
    42  	return nil, nil
    43  }
    44  
    45  func opDiv(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    46  	x, y := callContext.stack.pop(), callContext.stack.peek()
    47  	y.Div(&x, y)
    48  	return nil, nil
    49  }
    50  
    51  func opSdiv(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    52  	x, y := callContext.stack.pop(), callContext.stack.peek()
    53  	y.SDiv(&x, y)
    54  	return nil, nil
    55  }
    56  
    57  func opMod(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    58  	x, y := callContext.stack.pop(), callContext.stack.peek()
    59  	y.Mod(&x, y)
    60  	return nil, nil
    61  }
    62  
    63  func opSmod(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    64  	x, y := callContext.stack.pop(), callContext.stack.peek()
    65  	y.SMod(&x, y)
    66  	return nil, nil
    67  }
    68  
    69  func opExp(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    70  	base, exponent := callContext.stack.pop(), callContext.stack.peek()
    71  	exponent.Exp(&base, exponent)
    72  	return nil, nil
    73  }
    74  
    75  func opSignExtend(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    76  	back, num := callContext.stack.pop(), callContext.stack.peek()
    77  	num.ExtendSign(num, &back)
    78  	return nil, nil
    79  }
    80  
    81  func opNot(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    82  	x := callContext.stack.peek()
    83  	x.Not(x)
    84  	return nil, nil
    85  }
    86  
    87  func opLt(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    88  	x, y := callContext.stack.pop(), callContext.stack.peek()
    89  	if x.Lt(y) {
    90  		y.SetOne()
    91  	} else {
    92  		y.Clear()
    93  	}
    94  	return nil, nil
    95  }
    96  
    97  func opGt(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    98  	x, y := callContext.stack.pop(), callContext.stack.peek()
    99  	if x.Gt(y) {
   100  		y.SetOne()
   101  	} else {
   102  		y.Clear()
   103  	}
   104  	return nil, nil
   105  }
   106  
   107  func opSlt(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   108  	x, y := callContext.stack.pop(), callContext.stack.peek()
   109  	if x.Slt(y) {
   110  		y.SetOne()
   111  	} else {
   112  		y.Clear()
   113  	}
   114  	return nil, nil
   115  }
   116  
   117  func opSgt(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   118  	x, y := callContext.stack.pop(), callContext.stack.peek()
   119  	if x.Sgt(y) {
   120  		y.SetOne()
   121  	} else {
   122  		y.Clear()
   123  	}
   124  	return nil, nil
   125  }
   126  
   127  func opEq(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   128  	x, y := callContext.stack.pop(), callContext.stack.peek()
   129  	if x.Eq(y) {
   130  		y.SetOne()
   131  	} else {
   132  		y.Clear()
   133  	}
   134  	return nil, nil
   135  }
   136  
   137  func opIszero(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   138  	x := callContext.stack.peek()
   139  	if x.IsZero() {
   140  		x.SetOne()
   141  	} else {
   142  		x.Clear()
   143  	}
   144  	return nil, nil
   145  }
   146  
   147  func opAnd(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   148  	x, y := callContext.stack.pop(), callContext.stack.peek()
   149  	y.And(&x, y)
   150  	return nil, nil
   151  }
   152  
   153  func opOr(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   154  	x, y := callContext.stack.pop(), callContext.stack.peek()
   155  	y.Or(&x, y)
   156  	return nil, nil
   157  }
   158  
   159  func opXor(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   160  	x, y := callContext.stack.pop(), callContext.stack.peek()
   161  	y.Xor(&x, y)
   162  	return nil, nil
   163  }
   164  
   165  func opByte(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   166  	th, val := callContext.stack.pop(), callContext.stack.peek()
   167  	val.Byte(&th)
   168  	return nil, nil
   169  }
   170  
   171  func opAddmod(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   172  	x, y, z := callContext.stack.pop(), callContext.stack.pop(), callContext.stack.peek()
   173  	if z.IsZero() {
   174  		z.Clear()
   175  	} else {
   176  		z.AddMod(&x, &y, z)
   177  	}
   178  	return nil, nil
   179  }
   180  
   181  func opMulmod(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   182  	x, y, z := callContext.stack.pop(), callContext.stack.pop(), callContext.stack.peek()
   183  	z.MulMod(&x, &y, z)
   184  	return nil, nil
   185  }
   186  
   187  // opSHL implements Shift Left
   188  // The SHL instruction (shift left) pops 2 values from the stack, first arg1 and then arg2,
   189  // and pushes on the stack arg2 shifted to the left by arg1 number of bits.
   190  func opSHL(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   191  	// Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
   192  	shift, value := callContext.stack.pop(), callContext.stack.peek()
   193  	if shift.LtUint64(256) {
   194  		value.Lsh(value, uint(shift.Uint64()))
   195  	} else {
   196  		value.Clear()
   197  	}
   198  	return nil, nil
   199  }
   200  
   201  // opSHR implements Logical Shift Right
   202  // The SHR instruction (logical shift right) pops 2 values from the stack, first arg1 and then arg2,
   203  // and pushes on the stack arg2 shifted to the right by arg1 number of bits with zero fill.
   204  func opSHR(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   205  	// Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
   206  	shift, value := callContext.stack.pop(), callContext.stack.peek()
   207  	if shift.LtUint64(256) {
   208  		value.Rsh(value, uint(shift.Uint64()))
   209  	} else {
   210  		value.Clear()
   211  	}
   212  	return nil, nil
   213  }
   214  
   215  // opSAR implements Arithmetic Shift Right
   216  // The SAR instruction (arithmetic shift right) pops 2 values from the stack, first arg1 and then arg2,
   217  // and pushes on the stack arg2 shifted to the right by arg1 number of bits with sign extension.
   218  func opSAR(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   219  	shift, value := callContext.stack.pop(), callContext.stack.peek()
   220  	if shift.GtUint64(256) {
   221  		if value.Sign() >= 0 {
   222  			value.Clear()
   223  		} else {
   224  			// Max negative shift: all bits set
   225  			value.SetAllOne()
   226  		}
   227  		return nil, nil
   228  	}
   229  	n := uint(shift.Uint64())
   230  	value.SRsh(value, n)
   231  	return nil, nil
   232  }
   233  
   234  func opSha3(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   235  	offset, size := callContext.stack.pop(), callContext.stack.peek()
   236  	data := callContext.memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
   237  
   238  	if interpreter.hasher == nil {
   239  		interpreter.hasher = sha3.NewLegacyKeccak256().(keccakState)
   240  	} else {
   241  		interpreter.hasher.Reset()
   242  	}
   243  	interpreter.hasher.Write(data)
   244  	interpreter.hasher.Read(interpreter.hasherBuf[:])
   245  
   246  	evm := interpreter.evm
   247  	if evm.vmConfig.EnablePreimageRecording {
   248  		evm.StateDB.AddPreimage(interpreter.hasherBuf, data)
   249  	}
   250  
   251  	size.SetBytes(interpreter.hasherBuf[:])
   252  	return nil, nil
   253  }
   254  func opAddress(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   255  	callContext.stack.push(new(uint256.Int).SetBytes(callContext.contract.Address().Bytes()))
   256  	return nil, nil
   257  }
   258  
   259  func opBalance(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   260  	slot := callContext.stack.peek()
   261  	address := common.Address(slot.Bytes20())
   262  	slot.SetFromBig(interpreter.evm.StateDB.GetBalance(address))
   263  	return nil, nil
   264  }
   265  
   266  func opOrigin(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   267  	callContext.stack.push(new(uint256.Int).SetBytes(interpreter.evm.Origin.Bytes()))
   268  	return nil, nil
   269  }
   270  func opCaller(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   271  	callContext.stack.push(new(uint256.Int).SetBytes(callContext.contract.Caller().Bytes()))
   272  	return nil, nil
   273  }
   274  
   275  func opCallValue(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   276  	v, _ := uint256.FromBig(callContext.contract.value)
   277  	callContext.stack.push(v)
   278  	return nil, nil
   279  }
   280  
   281  func opCallDataLoad(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   282  	x := callContext.stack.peek()
   283  	if offset, overflow := x.Uint64WithOverflow(); !overflow {
   284  		data := getData(callContext.contract.Input, offset, 32)
   285  		x.SetBytes(data)
   286  	} else {
   287  		x.Clear()
   288  	}
   289  	return nil, nil
   290  }
   291  
   292  func opCallDataSize(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   293  	callContext.stack.push(new(uint256.Int).SetUint64(uint64(len(callContext.contract.Input))))
   294  	return nil, nil
   295  }
   296  
   297  func opCallDataCopy(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   298  	var (
   299  		memOffset  = callContext.stack.pop()
   300  		dataOffset = callContext.stack.pop()
   301  		length     = callContext.stack.pop()
   302  	)
   303  	dataOffset64, overflow := dataOffset.Uint64WithOverflow()
   304  	if overflow {
   305  		dataOffset64 = 0xffffffffffffffff
   306  	}
   307  	// These values are checked for overflow during gas cost calculation
   308  	memOffset64 := memOffset.Uint64()
   309  	length64 := length.Uint64()
   310  	callContext.memory.Set(memOffset64, length64, getData(callContext.contract.Input, dataOffset64, length64))
   311  
   312  	return nil, nil
   313  }
   314  
   315  func opReturnDataSize(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   316  	callContext.stack.push(new(uint256.Int).SetUint64(uint64(len(interpreter.returnData))))
   317  	return nil, nil
   318  }
   319  
   320  func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   321  	var (
   322  		memOffset  = callContext.stack.pop()
   323  		dataOffset = callContext.stack.pop()
   324  		length     = callContext.stack.pop()
   325  	)
   326  
   327  	offset64, overflow := dataOffset.Uint64WithOverflow()
   328  	if overflow {
   329  		return nil, ErrReturnDataOutOfBounds
   330  	}
   331  	// we can reuse dataOffset now (aliasing it for clarity)
   332  	var end = dataOffset
   333  	end.Add(&dataOffset, &length)
   334  	end64, overflow := end.Uint64WithOverflow()
   335  	if overflow || uint64(len(interpreter.returnData)) < end64 {
   336  		return nil, ErrReturnDataOutOfBounds
   337  	}
   338  	callContext.memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[offset64:end64])
   339  	return nil, nil
   340  }
   341  
   342  func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   343  	slot := callContext.stack.peek()
   344  	slot.SetUint64(uint64(interpreter.evm.StateDB.GetCodeSize(common.Address(slot.Bytes20()))))
   345  	return nil, nil
   346  }
   347  
   348  func opCodeSize(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   349  	l := new(uint256.Int)
   350  	l.SetUint64(uint64(len(callContext.contract.Code)))
   351  	callContext.stack.push(l)
   352  	return nil, nil
   353  }
   354  
   355  func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   356  	var (
   357  		memOffset  = callContext.stack.pop()
   358  		codeOffset = callContext.stack.pop()
   359  		length     = callContext.stack.pop()
   360  	)
   361  	uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
   362  	if overflow {
   363  		uint64CodeOffset = 0xffffffffffffffff
   364  	}
   365  	codeCopy := getData(callContext.contract.Code, uint64CodeOffset, length.Uint64())
   366  	callContext.memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
   367  
   368  	return nil, nil
   369  }
   370  
   371  func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   372  	var (
   373  		stack      = callContext.stack
   374  		a          = stack.pop()
   375  		memOffset  = stack.pop()
   376  		codeOffset = stack.pop()
   377  		length     = stack.pop()
   378  	)
   379  	uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
   380  	if overflow {
   381  		uint64CodeOffset = 0xffffffffffffffff
   382  	}
   383  	addr := common.Address(a.Bytes20())
   384  	codeCopy := getData(interpreter.evm.StateDB.GetCode(addr), uint64CodeOffset, length.Uint64())
   385  	callContext.memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
   386  
   387  	return nil, nil
   388  }
   389  
   390  // opExtCodeHash returns the code hash of a specified account.
   391  // There are several cases when the function is called, while we can relay everything
   392  // to `state.GetCodeHash` function to ensure the correctness.
   393  //   (1) Caller tries to get the code hash of a normal contract account, state
   394  // should return the relative code hash and set it as the result.
   395  //
   396  //   (2) Caller tries to get the code hash of a non-existent account, state should
   397  // return common.Hash{} and zero will be set as the result.
   398  //
   399  //   (3) Caller tries to get the code hash for an account without contract code,
   400  // state should return emptyCodeHash(0xc5d246...) as the result.
   401  //
   402  //   (4) Caller tries to get the code hash of a precompiled account, the result
   403  // should be zero or emptyCodeHash.
   404  //
   405  // It is worth noting that in order to avoid unnecessary create and clean,
   406  // all precompile accounts on mainnet have been transferred 1 wei, so the return
   407  // here should be emptyCodeHash.
   408  // If the precompile account is not transferred any amount on a private or
   409  // customized chain, the return value will be zero.
   410  //
   411  //   (5) Caller tries to get the code hash for an account which is marked as suicided
   412  // in the current transaction, the code hash of this account should be returned.
   413  //
   414  //   (6) Caller tries to get the code hash for an account which is marked as deleted,
   415  // this account should be regarded as a non-existent account and zero should be returned.
   416  func opExtCodeHash(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   417  	slot := callContext.stack.peek()
   418  	address := common.Address(slot.Bytes20())
   419  	if interpreter.evm.StateDB.Empty(address) {
   420  		slot.Clear()
   421  	} else {
   422  		slot.SetBytes(interpreter.evm.StateDB.GetCodeHash(address).Bytes())
   423  	}
   424  	return nil, nil
   425  }
   426  
   427  func opGasprice(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   428  	v, _ := uint256.FromBig(interpreter.evm.GasPrice)
   429  	callContext.stack.push(v)
   430  	return nil, nil
   431  }
   432  
   433  func opBlockhash(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   434  	num := callContext.stack.peek()
   435  	num64, overflow := num.Uint64WithOverflow()
   436  	if overflow {
   437  		num.Clear()
   438  		return nil, nil
   439  	}
   440  	var upper, lower uint64
   441  	upper = interpreter.evm.BlockNumber.Uint64()
   442  	if upper < 257 {
   443  		lower = 0
   444  	} else {
   445  		lower = upper - 256
   446  	}
   447  	if num64 >= lower && num64 < upper {
   448  		num.SetBytes(interpreter.evm.GetHash(num64).Bytes())
   449  	} else {
   450  		num.Clear()
   451  	}
   452  	return nil, nil
   453  }
   454  
   455  func opCoinbase(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   456  	callContext.stack.push(new(uint256.Int).SetBytes(interpreter.evm.Coinbase.Bytes()))
   457  	return nil, nil
   458  }
   459  
   460  func opTimestamp(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   461  	v, _ := uint256.FromBig(interpreter.evm.Time)
   462  	callContext.stack.push(v)
   463  	return nil, nil
   464  }
   465  
   466  func opNumber(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   467  	v, _ := uint256.FromBig(interpreter.evm.BlockNumber)
   468  	callContext.stack.push(v)
   469  	return nil, nil
   470  }
   471  
   472  func opDifficulty(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   473  	v, _ := uint256.FromBig(interpreter.evm.Difficulty)
   474  	callContext.stack.push(v)
   475  	return nil, nil
   476  }
   477  
   478  func opGasLimit(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   479  	callContext.stack.push(new(uint256.Int).SetUint64(interpreter.evm.GasLimit))
   480  	return nil, nil
   481  }
   482  
   483  func opPop(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   484  	callContext.stack.pop()
   485  	return nil, nil
   486  }
   487  
   488  func opMload(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   489  	v := callContext.stack.peek()
   490  	offset := int64(v.Uint64())
   491  	v.SetBytes(callContext.memory.GetPtr(offset, 32))
   492  	return nil, nil
   493  }
   494  
   495  func opMstore(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   496  	// pop value of the stack
   497  	mStart, val := callContext.stack.pop(), callContext.stack.pop()
   498  	callContext.memory.Set32(mStart.Uint64(), &val)
   499  	return nil, nil
   500  }
   501  
   502  func opMstore8(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   503  	off, val := callContext.stack.pop(), callContext.stack.pop()
   504  	callContext.memory.store[off.Uint64()] = byte(val.Uint64())
   505  	return nil, nil
   506  }
   507  
   508  func opSload(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   509  	loc := callContext.stack.peek()
   510  	hash := common.Hash(loc.Bytes32())
   511  	val := interpreter.evm.StateDB.GetState(callContext.contract.Address(), hash)
   512  	loc.SetBytes(val.Bytes())
   513  	return nil, nil
   514  }
   515  
   516  func opSstore(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   517  	loc := callContext.stack.pop()
   518  	val := callContext.stack.pop()
   519  	interpreter.evm.StateDB.SetState(callContext.contract.Address(),
   520  		common.Hash(loc.Bytes32()), common.Hash(val.Bytes32()))
   521  	return nil, nil
   522  }
   523  
   524  func opJump(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   525  	pos := callContext.stack.pop()
   526  	if !callContext.contract.validJumpdest(&pos) {
   527  		return nil, ErrInvalidJump
   528  	}
   529  	*pc = pos.Uint64()
   530  	return nil, nil
   531  }
   532  
   533  func opJumpi(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   534  	pos, cond := callContext.stack.pop(), callContext.stack.pop()
   535  	if !cond.IsZero() {
   536  		if !callContext.contract.validJumpdest(&pos) {
   537  			return nil, ErrInvalidJump
   538  		}
   539  		*pc = pos.Uint64()
   540  	} else {
   541  		*pc++
   542  	}
   543  	return nil, nil
   544  }
   545  
   546  func opJumpdest(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   547  	return nil, nil
   548  }
   549  
   550  func opBeginSub(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   551  	return nil, ErrInvalidSubroutineEntry
   552  }
   553  
   554  func opJumpSub(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   555  	if len(callContext.rstack.data) >= 1023 {
   556  		return nil, ErrReturnStackExceeded
   557  	}
   558  	pos := callContext.stack.pop()
   559  	if !pos.IsUint64() {
   560  		return nil, ErrInvalidJump
   561  	}
   562  	posU64 := pos.Uint64()
   563  	if !callContext.contract.validJumpSubdest(posU64) {
   564  		return nil, ErrInvalidJump
   565  	}
   566  	callContext.rstack.push(uint32(*pc))
   567  	*pc = posU64 + 1
   568  	return nil, nil
   569  }
   570  
   571  func opReturnSub(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   572  	if len(callContext.rstack.data) == 0 {
   573  		return nil, ErrInvalidRetsub
   574  	}
   575  	// Other than the check that the return stack is not empty, there is no
   576  	// need to validate the pc from 'returns', since we only ever push valid
   577  	//values onto it via jumpsub.
   578  	*pc = uint64(callContext.rstack.pop()) + 1
   579  	return nil, nil
   580  }
   581  
   582  func opPc(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   583  	callContext.stack.push(new(uint256.Int).SetUint64(*pc))
   584  	return nil, nil
   585  }
   586  
   587  func opMsize(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   588  	callContext.stack.push(new(uint256.Int).SetUint64(uint64(callContext.memory.Len())))
   589  	return nil, nil
   590  }
   591  
   592  func opGas(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   593  	callContext.stack.push(new(uint256.Int).SetUint64(callContext.contract.Gas))
   594  	return nil, nil
   595  }
   596  
   597  func opCreate(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   598  	var (
   599  		value        = callContext.stack.pop()
   600  		offset, size = callContext.stack.pop(), callContext.stack.pop()
   601  		input        = callContext.memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
   602  		gas          = callContext.contract.Gas
   603  	)
   604  	if interpreter.evm.chainRules.IsEIP150 {
   605  		gas -= gas / 64
   606  	}
   607  	// reuse size int for stackvalue
   608  	stackvalue := size
   609  
   610  	callContext.contract.UseGas(gas)
   611  	//TODO: use uint256.Int instead of converting with toBig()
   612  	var bigVal = big0
   613  	if !value.IsZero() {
   614  		bigVal = value.ToBig()
   615  	}
   616  
   617  	res, addr, returnGas, suberr := interpreter.evm.Create(callContext.contract, input, gas, bigVal)
   618  	// Push item on the stack based on the returned error. If the ruleset is
   619  	// homestead we must check for CodeStoreOutOfGasError (homestead only
   620  	// rule) and treat as an error, if the ruleset is frontier we must
   621  	// ignore this error and pretend the operation was successful.
   622  	if interpreter.evm.chainRules.IsHomestead && suberr == ErrCodeStoreOutOfGas {
   623  		stackvalue.Clear()
   624  	} else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
   625  		stackvalue.Clear()
   626  	} else {
   627  		stackvalue.SetBytes(addr.Bytes())
   628  	}
   629  	callContext.stack.push(&stackvalue)
   630  	callContext.contract.Gas += returnGas
   631  
   632  	if suberr == ErrExecutionReverted {
   633  		return res, nil
   634  	}
   635  	return nil, nil
   636  }
   637  
   638  func opCreate2(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   639  	var (
   640  		endowment    = callContext.stack.pop()
   641  		offset, size = callContext.stack.pop(), callContext.stack.pop()
   642  		salt         = callContext.stack.pop()
   643  		input        = callContext.memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
   644  		gas          = callContext.contract.Gas
   645  	)
   646  
   647  	// Apply EIP150
   648  	gas -= gas / 64
   649  	callContext.contract.UseGas(gas)
   650  	// reuse size int for stackvalue
   651  	stackvalue := size
   652  	//TODO: use uint256.Int instead of converting with toBig()
   653  	bigEndowment := big0
   654  	if !endowment.IsZero() {
   655  		bigEndowment = endowment.ToBig()
   656  	}
   657  	res, addr, returnGas, suberr := interpreter.evm.Create2(callContext.contract, input, gas,
   658  		bigEndowment, &salt)
   659  	// Push item on the stack based on the returned error.
   660  	if suberr != nil {
   661  		stackvalue.Clear()
   662  	} else {
   663  		stackvalue.SetBytes(addr.Bytes())
   664  	}
   665  	callContext.stack.push(&stackvalue)
   666  	callContext.contract.Gas += returnGas
   667  
   668  	if suberr == ErrExecutionReverted {
   669  		return res, nil
   670  	}
   671  	return nil, nil
   672  }
   673  
   674  func opCall(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   675  	stack := callContext.stack
   676  	// Pop gas. The actual gas in interpreter.evm.callGasTemp.
   677  	// We can use this as a temporary value
   678  	temp := stack.pop()
   679  	gas := interpreter.evm.callGasTemp
   680  	// Pop other call parameters.
   681  	addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   682  	toAddr := common.Address(addr.Bytes20())
   683  	// Get the arguments from the memory.
   684  	args := callContext.memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
   685  
   686  	var bigVal = big0
   687  	//TODO: use uint256.Int instead of converting with toBig()
   688  	// By using big0 here, we save an alloc for the most common case (non-ether-transferring contract calls),
   689  	// but it would make more sense to extend the usage of uint256.Int
   690  	if !value.IsZero() {
   691  		gas += params.CallStipend
   692  		bigVal = value.ToBig()
   693  	}
   694  
   695  	ret, returnGas, err := interpreter.evm.Call(callContext.contract, toAddr, args, gas, bigVal)
   696  
   697  	if err != nil {
   698  		temp.Clear()
   699  	} else {
   700  		temp.SetOne()
   701  	}
   702  	stack.push(&temp)
   703  	if err == nil || err == ErrExecutionReverted {
   704  		callContext.memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   705  	}
   706  	callContext.contract.Gas += returnGas
   707  
   708  	return ret, nil
   709  }
   710  
   711  func opCallCode(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   712  	// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
   713  	stack := callContext.stack
   714  	// We use it as a temporary value
   715  	temp := stack.pop()
   716  	gas := interpreter.evm.callGasTemp
   717  	// Pop other call parameters.
   718  	addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   719  	toAddr := common.Address(addr.Bytes20())
   720  	// Get arguments from the memory.
   721  	args := callContext.memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
   722  
   723  	//TODO: use uint256.Int instead of converting with toBig()
   724  	var bigVal = big0
   725  	if !value.IsZero() {
   726  		gas += params.CallStipend
   727  		bigVal = value.ToBig()
   728  	}
   729  
   730  	ret, returnGas, err := interpreter.evm.CallCode(callContext.contract, toAddr, args, gas, bigVal)
   731  	if err != nil {
   732  		temp.Clear()
   733  	} else {
   734  		temp.SetOne()
   735  	}
   736  	stack.push(&temp)
   737  	if err == nil || err == ErrExecutionReverted {
   738  		callContext.memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   739  	}
   740  	callContext.contract.Gas += returnGas
   741  
   742  	return ret, nil
   743  }
   744  
   745  func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   746  	stack := callContext.stack
   747  	// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
   748  	// We use it as a temporary value
   749  	temp := stack.pop()
   750  	gas := interpreter.evm.callGasTemp
   751  	// Pop other call parameters.
   752  	addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   753  	toAddr := common.Address(addr.Bytes20())
   754  	// Get arguments from the memory.
   755  	args := callContext.memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
   756  
   757  	ret, returnGas, err := interpreter.evm.DelegateCall(callContext.contract, toAddr, args, gas)
   758  	if err != nil {
   759  		temp.Clear()
   760  	} else {
   761  		temp.SetOne()
   762  	}
   763  	stack.push(&temp)
   764  	if err == nil || err == ErrExecutionReverted {
   765  		callContext.memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   766  	}
   767  	callContext.contract.Gas += returnGas
   768  
   769  	return ret, nil
   770  }
   771  
   772  func opStaticCall(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   773  	// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
   774  	stack := callContext.stack
   775  	// We use it as a temporary value
   776  	temp := stack.pop()
   777  	gas := interpreter.evm.callGasTemp
   778  	// Pop other call parameters.
   779  	addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
   780  	toAddr := common.Address(addr.Bytes20())
   781  	// Get arguments from the memory.
   782  	args := callContext.memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
   783  
   784  	ret, returnGas, err := interpreter.evm.StaticCall(callContext.contract, toAddr, args, gas)
   785  	if err != nil {
   786  		temp.Clear()
   787  	} else {
   788  		temp.SetOne()
   789  	}
   790  	stack.push(&temp)
   791  	if err == nil || err == ErrExecutionReverted {
   792  		callContext.memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   793  	}
   794  	callContext.contract.Gas += returnGas
   795  
   796  	return ret, nil
   797  }
   798  
   799  func opReturn(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   800  	offset, size := callContext.stack.pop(), callContext.stack.pop()
   801  	ret := callContext.memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
   802  
   803  	return ret, nil
   804  }
   805  
   806  func opRevert(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   807  	offset, size := callContext.stack.pop(), callContext.stack.pop()
   808  	ret := callContext.memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
   809  
   810  	return ret, nil
   811  }
   812  
   813  func opStop(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   814  	return nil, nil
   815  }
   816  
   817  func opSuicide(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   818  	beneficiary := callContext.stack.pop()
   819  	balance := interpreter.evm.StateDB.GetBalance(callContext.contract.Address())
   820  	interpreter.evm.StateDB.AddBalance(common.Address(beneficiary.Bytes20()), balance)
   821  	interpreter.evm.StateDB.Suicide(callContext.contract.Address())
   822  	return nil, nil
   823  }
   824  
   825  // following functions are used by the instruction jump  table
   826  
   827  // make log instruction function
   828  func makeLog(size int) executionFunc {
   829  	return func(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   830  		topics := make([]common.Hash, size)
   831  		stack := callContext.stack
   832  		mStart, mSize := stack.pop(), stack.pop()
   833  		for i := 0; i < size; i++ {
   834  			addr := stack.pop()
   835  			topics[i] = common.Hash(addr.Bytes32())
   836  		}
   837  
   838  		d := callContext.memory.GetCopy(int64(mStart.Uint64()), int64(mSize.Uint64()))
   839  		interpreter.evm.StateDB.AddLog(&types.Log{
   840  			Address: callContext.contract.Address(),
   841  			Topics:  topics,
   842  			Data:    d,
   843  			// This is a non-consensus field, but assigned here because
   844  			// core/state doesn't know the current block number.
   845  			BlockNumber: interpreter.evm.BlockNumber.Uint64(),
   846  		})
   847  
   848  		return nil, nil
   849  	}
   850  }
   851  
   852  // opPush1 is a specialized version of pushN
   853  func opPush1(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   854  	var (
   855  		codeLen = uint64(len(callContext.contract.Code))
   856  		integer = new(uint256.Int)
   857  	)
   858  	*pc += 1
   859  	if *pc < codeLen {
   860  		callContext.stack.push(integer.SetUint64(uint64(callContext.contract.Code[*pc])))
   861  	} else {
   862  		callContext.stack.push(integer.Clear())
   863  	}
   864  	return nil, nil
   865  }
   866  
   867  // make push instruction function
   868  func makePush(size uint64, pushByteSize int) executionFunc {
   869  	return func(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   870  		codeLen := len(callContext.contract.Code)
   871  
   872  		startMin := codeLen
   873  		if int(*pc+1) < startMin {
   874  			startMin = int(*pc + 1)
   875  		}
   876  
   877  		endMin := codeLen
   878  		if startMin+pushByteSize < endMin {
   879  			endMin = startMin + pushByteSize
   880  		}
   881  
   882  		integer := new(uint256.Int)
   883  		callContext.stack.push(integer.SetBytes(common.RightPadBytes(
   884  			callContext.contract.Code[startMin:endMin], pushByteSize)))
   885  
   886  		*pc += size
   887  		return nil, nil
   888  	}
   889  }
   890  
   891  // make dup instruction function
   892  func makeDup(size int64) executionFunc {
   893  	return func(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   894  		callContext.stack.dup(int(size))
   895  		return nil, nil
   896  	}
   897  }
   898  
   899  // make swap instruction function
   900  func makeSwap(size int64) executionFunc {
   901  	// switch n + 1 otherwise n would be swapped with n
   902  	size++
   903  	return func(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   904  		callContext.stack.swap(int(size))
   905  		return nil, nil
   906  	}
   907  }