github.com/bcskill/bcschain/v3@v3.4.9-beta2/core/vm/instructions_test.go (about)

     1  // Copyright 2017 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  	"bytes"
    21  	"math/big"
    22  	"testing"
    23  
    24  	"github.com/bcskill/bcschain/v3/common"
    25  	"github.com/bcskill/bcschain/v3/crypto"
    26  	"github.com/bcskill/bcschain/v3/params"
    27  )
    28  
    29  type TwoOperandTestcase struct {
    30  	X        string
    31  	Y        string
    32  	Expected string
    33  }
    34  
    35  type twoOperandParams struct {
    36  	x string
    37  	y string
    38  }
    39  
    40  var alphabetSoup = "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
    41  var commonParams []*twoOperandParams
    42  var twoOpMethods map[string]executionFunc
    43  
    44  func init() {
    45  
    46  	// Params is a list of common edgecases that should be used for some common tests
    47  	params := []string{
    48  		"0000000000000000000000000000000000000000000000000000000000000000", // 0
    49  		"0000000000000000000000000000000000000000000000000000000000000001", // +1
    50  		"0000000000000000000000000000000000000000000000000000000000000005", // +5
    51  		"7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe", // + max -1
    52  		"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // + max
    53  		"8000000000000000000000000000000000000000000000000000000000000000", // - max
    54  		"8000000000000000000000000000000000000000000000000000000000000001", // - max+1
    55  		"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", // - 5
    56  		"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // - 1
    57  	}
    58  	// Params are combined so each param is used on each 'side'
    59  	commonParams = make([]*twoOperandParams, len(params)*len(params))
    60  	for i, x := range params {
    61  		for j, y := range params {
    62  			commonParams[i*len(params)+j] = &twoOperandParams{x, y}
    63  		}
    64  	}
    65  	twoOpMethods = map[string]executionFunc{
    66  		"add":     opAdd,
    67  		"sub":     opSub,
    68  		"mul":     opMul,
    69  		"div":     opDiv,
    70  		"sdiv":    opSdiv,
    71  		"mod":     opMod,
    72  		"smod":    opSmod,
    73  		"exp":     opExp,
    74  		"signext": opSignExtend,
    75  		"lt":      opLt,
    76  		"gt":      opGt,
    77  		"slt":     opSlt,
    78  		"sgt":     opSgt,
    79  		"eq":      opEq,
    80  		"and":     opAnd,
    81  		"or":      opOr,
    82  		"xor":     opXor,
    83  		"byte":    opByte,
    84  		"shl":     opSHL,
    85  		"shr":     opSHR,
    86  		"sar":     opSAR,
    87  	}
    88  }
    89  func testTwoOperandOp(t *testing.T, tests []TwoOperandTestcase, opFn func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error)) {
    90  	var (
    91  		env            = NewEVM(Context{}, nil, params.TestChainConfig, Config{})
    92  		stack          = newstack()
    93  		pc             = uint64(0)
    94  		evmInterpreter = NewEVMInterpreter(env, env.vmConfig)
    95  	)
    96  
    97  	env.interpreter = evmInterpreter
    98  	evmInterpreter.intPool = poolOfIntPools.get()
    99  	for i, test := range tests {
   100  		x := new(big.Int).SetBytes(common.Hex2Bytes(test.X))
   101  		shift := new(big.Int).SetBytes(common.Hex2Bytes(test.Y))
   102  		expected := new(big.Int).SetBytes(common.Hex2Bytes(test.Expected))
   103  		stack.push(x)
   104  		stack.push(shift)
   105  		opFn(&pc, evmInterpreter, nil, nil, stack)
   106  		actual := stack.pop()
   107  		if actual.Cmp(expected) != 0 {
   108  			t.Errorf("Testcase %d, expected  %v, got %v", i, expected, actual)
   109  		}
   110  		// Check pool usage
   111  		// 1.pool is not allowed to contain anything on the stack
   112  		// 2.pool is not allowed to contain the same pointers twice
   113  		if evmInterpreter.intPool.pool.len() > 0 {
   114  
   115  			poolvals := make(map[*big.Int]struct{})
   116  			poolvals[actual] = struct{}{}
   117  
   118  			for evmInterpreter.intPool.pool.len() > 0 {
   119  				key := evmInterpreter.intPool.get()
   120  				if _, exist := poolvals[key]; exist {
   121  					t.Errorf("Testcase %d, pool contains double-entry", i)
   122  				}
   123  				poolvals[key] = struct{}{}
   124  			}
   125  		}
   126  	}
   127  	poolOfIntPools.put(evmInterpreter.intPool)
   128  }
   129  
   130  func TestByteOp(t *testing.T) {
   131  	var (
   132  		env            = NewEVM(Context{}, nil, params.TestChainConfig, Config{})
   133  		stack          = newstack()
   134  		evmInterpreter = NewEVMInterpreter(env, env.vmConfig)
   135  	)
   136  
   137  	env.interpreter = evmInterpreter
   138  	evmInterpreter.intPool = poolOfIntPools.get()
   139  	tests := []struct {
   140  		v        string
   141  		th       uint64
   142  		expected *big.Int
   143  	}{
   144  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 0, big.NewInt(0xAB)},
   145  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 1, big.NewInt(0xCD)},
   146  		{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 0, big.NewInt(0x00)},
   147  		{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 1, big.NewInt(0xCD)},
   148  		{"0000000000000000000000000000000000000000000000000000000000102030", 31, big.NewInt(0x30)},
   149  		{"0000000000000000000000000000000000000000000000000000000000102030", 30, big.NewInt(0x20)},
   150  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32, big.NewInt(0x0)},
   151  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0xFFFFFFFFFFFFFFFF, big.NewInt(0x0)},
   152  	}
   153  	pc := uint64(0)
   154  	for _, test := range tests {
   155  		val := new(big.Int).SetBytes(common.Hex2Bytes(test.v))
   156  		th := new(big.Int).SetUint64(test.th)
   157  		stack.push(val)
   158  		stack.push(th)
   159  		opByte(&pc, evmInterpreter, nil, nil, stack)
   160  		actual := stack.pop()
   161  		if actual.Cmp(test.expected) != 0 {
   162  			t.Fatalf("Expected  [%v] %v:th byte to be %v, was %v.", test.v, test.th, test.expected, actual)
   163  		}
   164  	}
   165  	poolOfIntPools.put(evmInterpreter.intPool)
   166  }
   167  
   168  func TestSHL(t *testing.T) {
   169  	// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shl-shift-left
   170  	tests := []TwoOperandTestcase{
   171  		{"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"},
   172  		{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000002"},
   173  		{"0000000000000000000000000000000000000000000000000000000000000001", "ff", "8000000000000000000000000000000000000000000000000000000000000000"},
   174  		{"0000000000000000000000000000000000000000000000000000000000000001", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
   175  		{"0000000000000000000000000000000000000000000000000000000000000001", "0101", "0000000000000000000000000000000000000000000000000000000000000000"},
   176  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   177  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"},
   178  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "8000000000000000000000000000000000000000000000000000000000000000"},
   179  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
   180  		{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
   181  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"},
   182  	}
   183  	testTwoOperandOp(t, tests, opSHL)
   184  }
   185  
   186  func TestSHR(t *testing.T) {
   187  	// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shr-logical-shift-right
   188  	tests := []TwoOperandTestcase{
   189  		{"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"},
   190  		{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
   191  		{"8000000000000000000000000000000000000000000000000000000000000000", "01", "4000000000000000000000000000000000000000000000000000000000000000"},
   192  		{"8000000000000000000000000000000000000000000000000000000000000000", "ff", "0000000000000000000000000000000000000000000000000000000000000001"},
   193  		{"8000000000000000000000000000000000000000000000000000000000000000", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
   194  		{"8000000000000000000000000000000000000000000000000000000000000000", "0101", "0000000000000000000000000000000000000000000000000000000000000000"},
   195  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   196  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   197  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000001"},
   198  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
   199  		{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
   200  	}
   201  	testTwoOperandOp(t, tests, opSHR)
   202  }
   203  
   204  func TestSAR(t *testing.T) {
   205  	// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#sar-arithmetic-shift-right
   206  	tests := []TwoOperandTestcase{
   207  		{"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"},
   208  		{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
   209  		{"8000000000000000000000000000000000000000000000000000000000000000", "01", "c000000000000000000000000000000000000000000000000000000000000000"},
   210  		{"8000000000000000000000000000000000000000000000000000000000000000", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   211  		{"8000000000000000000000000000000000000000000000000000000000000000", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   212  		{"8000000000000000000000000000000000000000000000000000000000000000", "0101", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   213  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   214  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   215  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   216  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   217  		{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
   218  		{"4000000000000000000000000000000000000000000000000000000000000000", "fe", "0000000000000000000000000000000000000000000000000000000000000001"},
   219  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "f8", "000000000000000000000000000000000000000000000000000000000000007f"},
   220  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "fe", "0000000000000000000000000000000000000000000000000000000000000001"},
   221  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000000"},
   222  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
   223  	}
   224  
   225  	testTwoOperandOp(t, tests, opSAR)
   226  }
   227  
   228  func TestSGT(t *testing.T) {
   229  	tests := []TwoOperandTestcase{
   230  
   231  		{"0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"},
   232  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"},
   233  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"},
   234  		{"0000000000000000000000000000000000000000000000000000000000000001", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001"},
   235  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"},
   236  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001"},
   237  		{"0000000000000000000000000000000000000000000000000000000000000001", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"},
   238  		{"8000000000000000000000000000000000000000000000000000000000000001", "8000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"},
   239  		{"8000000000000000000000000000000000000000000000000000000000000001", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001"},
   240  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "8000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"},
   241  		{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd", "0000000000000000000000000000000000000000000000000000000000000001"},
   242  		{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", "0000000000000000000000000000000000000000000000000000000000000000"},
   243  	}
   244  	testTwoOperandOp(t, tests, opSgt)
   245  }
   246  
   247  func TestSLT(t *testing.T) {
   248  	tests := []TwoOperandTestcase{
   249  		{"0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"},
   250  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"},
   251  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"},
   252  		{"0000000000000000000000000000000000000000000000000000000000000001", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"},
   253  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001"},
   254  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"},
   255  		{"0000000000000000000000000000000000000000000000000000000000000001", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001"},
   256  		{"8000000000000000000000000000000000000000000000000000000000000001", "8000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"},
   257  		{"8000000000000000000000000000000000000000000000000000000000000001", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"},
   258  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "8000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001"},
   259  		{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd", "0000000000000000000000000000000000000000000000000000000000000000"},
   260  		{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", "0000000000000000000000000000000000000000000000000000000000000001"},
   261  	}
   262  	testTwoOperandOp(t, tests, opSlt)
   263  }
   264  
   265  func opBenchmark(bench *testing.B, op func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error), args ...string) {
   266  	var (
   267  		env            = NewEVM(Context{}, nil, params.TestChainConfig, Config{})
   268  		stack          = newstack()
   269  		evmInterpreter = NewEVMInterpreter(env, env.vmConfig)
   270  	)
   271  
   272  	env.interpreter = evmInterpreter
   273  	evmInterpreter.intPool = poolOfIntPools.get()
   274  	// convert args
   275  	byteArgs := make([][]byte, len(args))
   276  	for i, arg := range args {
   277  		byteArgs[i] = common.Hex2Bytes(arg)
   278  	}
   279  	pc := uint64(0)
   280  	bench.ResetTimer()
   281  	for i := 0; i < bench.N; i++ {
   282  		for _, arg := range byteArgs {
   283  			a := new(big.Int).SetBytes(arg)
   284  			stack.push(a)
   285  		}
   286  		op(&pc, evmInterpreter, nil, nil, stack)
   287  		stack.pop()
   288  	}
   289  	poolOfIntPools.put(evmInterpreter.intPool)
   290  }
   291  
   292  func BenchmarkOpAdd64(b *testing.B) {
   293  	x := "ffffffff"
   294  	y := "fd37f3e2bba2c4f"
   295  
   296  	opBenchmark(b, opAdd, x, y)
   297  }
   298  
   299  func BenchmarkOpAdd128(b *testing.B) {
   300  	x := "ffffffffffffffff"
   301  	y := "f5470b43c6549b016288e9a65629687"
   302  
   303  	opBenchmark(b, opAdd, x, y)
   304  }
   305  
   306  func BenchmarkOpAdd256(b *testing.B) {
   307  	x := "0802431afcbce1fc194c9eaa417b2fb67dc75a95db0bc7ec6b1c8af11df6a1da9"
   308  	y := "a1f5aac137876480252e5dcac62c354ec0d42b76b0642b6181ed099849ea1d57"
   309  
   310  	opBenchmark(b, opAdd, x, y)
   311  }
   312  
   313  func BenchmarkOpSub64(b *testing.B) {
   314  	x := "51022b6317003a9d"
   315  	y := "a20456c62e00753a"
   316  
   317  	opBenchmark(b, opSub, x, y)
   318  }
   319  
   320  func BenchmarkOpSub128(b *testing.B) {
   321  	x := "4dde30faaacdc14d00327aac314e915d"
   322  	y := "9bbc61f5559b829a0064f558629d22ba"
   323  
   324  	opBenchmark(b, opSub, x, y)
   325  }
   326  
   327  func BenchmarkOpSub256(b *testing.B) {
   328  	x := "4bfcd8bb2ac462735b48a17580690283980aa2d679f091c64364594df113ea37"
   329  	y := "97f9b1765588c4e6b69142eb00d20507301545acf3e1238c86c8b29be227d46e"
   330  
   331  	opBenchmark(b, opSub, x, y)
   332  }
   333  
   334  func BenchmarkOpMul(b *testing.B) {
   335  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   336  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   337  
   338  	opBenchmark(b, opMul, x, y)
   339  }
   340  
   341  func BenchmarkOpDiv256(b *testing.B) {
   342  	x := "ff3f9014f20db29ae04af2c2d265de17"
   343  	y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611"
   344  	opBenchmark(b, opDiv, x, y)
   345  }
   346  
   347  func BenchmarkOpDiv128(b *testing.B) {
   348  	x := "fdedc7f10142ff97"
   349  	y := "fbdfda0e2ce356173d1993d5f70a2b11"
   350  	opBenchmark(b, opDiv, x, y)
   351  }
   352  
   353  func BenchmarkOpDiv64(b *testing.B) {
   354  	x := "fcb34eb3"
   355  	y := "f97180878e839129"
   356  	opBenchmark(b, opDiv, x, y)
   357  }
   358  
   359  func BenchmarkOpSdiv(b *testing.B) {
   360  	x := "ff3f9014f20db29ae04af2c2d265de17"
   361  	y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611"
   362  
   363  	opBenchmark(b, opSdiv, x, y)
   364  }
   365  
   366  func BenchmarkOpMod(b *testing.B) {
   367  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   368  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   369  
   370  	opBenchmark(b, opMod, x, y)
   371  }
   372  
   373  func BenchmarkOpSmod(b *testing.B) {
   374  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   375  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   376  
   377  	opBenchmark(b, opSmod, x, y)
   378  }
   379  
   380  func BenchmarkOpExp(b *testing.B) {
   381  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   382  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   383  
   384  	opBenchmark(b, opExp, x, y)
   385  }
   386  
   387  func BenchmarkOpSignExtend(b *testing.B) {
   388  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   389  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   390  
   391  	opBenchmark(b, opSignExtend, x, y)
   392  }
   393  
   394  func BenchmarkOpLt(b *testing.B) {
   395  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   396  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   397  
   398  	opBenchmark(b, opLt, x, y)
   399  }
   400  
   401  func BenchmarkOpGt(b *testing.B) {
   402  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   403  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   404  
   405  	opBenchmark(b, opGt, x, y)
   406  }
   407  
   408  func BenchmarkOpSlt(b *testing.B) {
   409  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   410  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   411  
   412  	opBenchmark(b, opSlt, x, y)
   413  }
   414  
   415  func BenchmarkOpSgt(b *testing.B) {
   416  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   417  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   418  
   419  	opBenchmark(b, opSgt, x, y)
   420  }
   421  
   422  func BenchmarkOpEq(b *testing.B) {
   423  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   424  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   425  
   426  	opBenchmark(b, opEq, x, y)
   427  }
   428  func BenchmarkOpEq2(b *testing.B) {
   429  	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
   430  	y := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201fffffffe"
   431  	opBenchmark(b, opEq, x, y)
   432  }
   433  func BenchmarkOpAnd(b *testing.B) {
   434  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   435  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   436  
   437  	opBenchmark(b, opAnd, x, y)
   438  }
   439  
   440  func BenchmarkOpOr(b *testing.B) {
   441  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   442  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   443  
   444  	opBenchmark(b, opOr, x, y)
   445  }
   446  
   447  func BenchmarkOpXor(b *testing.B) {
   448  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   449  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   450  
   451  	opBenchmark(b, opXor, x, y)
   452  }
   453  
   454  func BenchmarkOpByte(b *testing.B) {
   455  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   456  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   457  
   458  	opBenchmark(b, opByte, x, y)
   459  }
   460  
   461  func BenchmarkOpAddmod(b *testing.B) {
   462  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   463  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   464  	z := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   465  
   466  	opBenchmark(b, opAddmod, x, y, z)
   467  }
   468  
   469  func BenchmarkOpMulmod(b *testing.B) {
   470  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   471  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   472  	z := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   473  
   474  	opBenchmark(b, opMulmod, x, y, z)
   475  }
   476  
   477  func BenchmarkOpSHL(b *testing.B) {
   478  	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
   479  	y := "ff"
   480  
   481  	opBenchmark(b, opSHL, x, y)
   482  }
   483  func BenchmarkOpSHR(b *testing.B) {
   484  	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
   485  	y := "ff"
   486  
   487  	opBenchmark(b, opSHR, x, y)
   488  }
   489  func BenchmarkOpSAR(b *testing.B) {
   490  	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
   491  	y := "ff"
   492  
   493  	opBenchmark(b, opSAR, x, y)
   494  }
   495  func BenchmarkOpIsZero(b *testing.B) {
   496  	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
   497  	opBenchmark(b, opIszero, x)
   498  }
   499  
   500  func TestOpMstore(t *testing.T) {
   501  	var (
   502  		env            = NewEVM(Context{}, nil, params.TestChainConfig, Config{})
   503  		stack          = newstack()
   504  		mem            = NewMemory()
   505  		evmInterpreter = NewEVMInterpreter(env, env.vmConfig)
   506  	)
   507  
   508  	env.interpreter = evmInterpreter
   509  	evmInterpreter.intPool = poolOfIntPools.get()
   510  	mem.Resize(64)
   511  	pc := uint64(0)
   512  	v := "abcdef00000000000000abba000000000deaf000000c0de00100000000133700"
   513  	stack.pushN(new(big.Int).SetBytes(common.Hex2Bytes(v)), big.NewInt(0))
   514  	opMstore(&pc, evmInterpreter, nil, mem, stack)
   515  	if got := common.Bytes2Hex(mem.Get(0, 32)); got != v {
   516  		t.Fatalf("Mstore fail, got %v, expected %v", got, v)
   517  	}
   518  	stack.pushN(big.NewInt(0x1), big.NewInt(0))
   519  	opMstore(&pc, evmInterpreter, nil, mem, stack)
   520  	if common.Bytes2Hex(mem.Get(0, 32)) != "0000000000000000000000000000000000000000000000000000000000000001" {
   521  		t.Fatalf("Mstore failed to overwrite previous value")
   522  	}
   523  	poolOfIntPools.put(evmInterpreter.intPool)
   524  }
   525  
   526  func BenchmarkOpMstore(bench *testing.B) {
   527  	var (
   528  		env            = NewEVM(Context{}, nil, params.TestChainConfig, Config{})
   529  		stack          = newstack()
   530  		mem            = NewMemory()
   531  		evmInterpreter = NewEVMInterpreter(env, env.vmConfig)
   532  	)
   533  
   534  	env.interpreter = evmInterpreter
   535  	evmInterpreter.intPool = poolOfIntPools.get()
   536  	mem.Resize(64)
   537  	pc := uint64(0)
   538  	memStart := big.NewInt(0)
   539  	value := big.NewInt(0x1337)
   540  
   541  	bench.ResetTimer()
   542  	for i := 0; i < bench.N; i++ {
   543  		stack.pushN(value, memStart)
   544  		opMstore(&pc, evmInterpreter, nil, mem, stack)
   545  	}
   546  	poolOfIntPools.put(evmInterpreter.intPool)
   547  }
   548  
   549  func BenchmarkOpSHA3(bench *testing.B) {
   550  	var (
   551  		env            = NewEVM(Context{}, nil, params.TestChainConfig, Config{})
   552  		stack          = newstack()
   553  		mem            = NewMemory()
   554  		evmInterpreter = NewEVMInterpreter(env, env.vmConfig)
   555  	)
   556  	env.interpreter = evmInterpreter
   557  	evmInterpreter.intPool = poolOfIntPools.get()
   558  	mem.Resize(32)
   559  	pc := uint64(0)
   560  	start := big.NewInt(0)
   561  
   562  	bench.ResetTimer()
   563  	for i := 0; i < bench.N; i++ {
   564  		stack.pushN(big.NewInt(32), start)
   565  		opSha3(&pc, evmInterpreter, nil, mem, stack)
   566  	}
   567  	poolOfIntPools.put(evmInterpreter.intPool)
   568  }
   569  
   570  func TestCreate2Addreses(t *testing.T) {
   571  	type testcase struct {
   572  		origin   string
   573  		salt     string
   574  		code     string
   575  		expected string
   576  	}
   577  
   578  	for i, tt := range []testcase{
   579  		{
   580  			origin:   "0x0000000000000000000000000000000000000000",
   581  			salt:     "0x0000000000000000000000000000000000000000",
   582  			code:     "0x00",
   583  			expected: "0x4d1a2e2bb4f88f0250f26ffff098b0b30b26bf38",
   584  		},
   585  		{
   586  			origin:   "0xdeadbeef00000000000000000000000000000000",
   587  			salt:     "0x0000000000000000000000000000000000000000",
   588  			code:     "0x00",
   589  			expected: "0xB928f69Bb1D91Cd65274e3c79d8986362984fDA3",
   590  		},
   591  		{
   592  			origin:   "0xdeadbeef00000000000000000000000000000000",
   593  			salt:     "0xfeed000000000000000000000000000000000000",
   594  			code:     "0x00",
   595  			expected: "0xD04116cDd17beBE565EB2422F2497E06cC1C9833",
   596  		},
   597  		{
   598  			origin:   "0x0000000000000000000000000000000000000000",
   599  			salt:     "0x0000000000000000000000000000000000000000",
   600  			code:     "0xdeadbeef",
   601  			expected: "0x70f2b2914A2a4b783FaEFb75f459A580616Fcb5e",
   602  		},
   603  		{
   604  			origin:   "0x00000000000000000000000000000000deadbeef",
   605  			salt:     "0xcafebabe",
   606  			code:     "0xdeadbeef",
   607  			expected: "0x60f3f640a8508fC6a86d45DF051962668E1e8AC7",
   608  		},
   609  		{
   610  			origin:   "0x00000000000000000000000000000000deadbeef",
   611  			salt:     "0xcafebabe",
   612  			code:     "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
   613  			expected: "0x1d8bfDC5D46DC4f61D6b6115972536eBE6A8854C",
   614  		},
   615  		{
   616  			origin:   "0x0000000000000000000000000000000000000000",
   617  			salt:     "0x0000000000000000000000000000000000000000",
   618  			code:     "0x",
   619  			expected: "0xE33C0C7F7df4809055C3ebA6c09CFe4BaF1BD9e0",
   620  		},
   621  	} {
   622  
   623  		origin := common.BytesToAddress(common.FromHex(tt.origin))
   624  		salt := common.BytesToHash(common.FromHex(tt.salt))
   625  		code := common.FromHex(tt.code)
   626  		codeHash := crypto.Keccak256(code)
   627  		address := crypto.CreateAddress2(origin, salt, codeHash)
   628  		/*
   629  			stack          := newstack()
   630  			// salt, but we don't need that for this test
   631  			stack.push(big.NewInt(int64(len(code)))) //size
   632  			stack.push(big.NewInt(0)) // memstart
   633  			stack.push(big.NewInt(0)) // value
   634  			gas, _ := gasCreate2(params.GasTable{}, nil, nil, stack, nil, 0)
   635  			fmt.Printf("Example %d\n* address `0x%x`\n* salt `0x%x`\n* init_code `0x%x`\n* gas (assuming no mem expansion): `%v`\n* result: `%s`\n\n", i,origin, salt, code, gas, address.String())
   636  		*/
   637  		expected := common.BytesToAddress(common.FromHex(tt.expected))
   638  		if !bytes.Equal(expected.Bytes(), address.Bytes()) {
   639  			t.Errorf("test %d: expected %s, got %s", i, expected.String(), address.String())
   640  		}
   641  
   642  	}
   643  }