github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/core/vm/instructions_test.go (about)

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