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