github.com/core-coin/go-core/v2@v2.1.9/core/vm/instructions_test.go (about)

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