github.com/electroneum/electroneum-sc@v0.0.0-20230105223411-3bc1d078281e/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  	"math/big"
    24  	"os"
    25  	"testing"
    26  
    27  	"github.com/electroneum/electroneum-sc/common"
    28  	"github.com/electroneum/electroneum-sc/crypto"
    29  	"github.com/electroneum/electroneum-sc/params"
    30  	"github.com/holiman/uint256"
    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 alphabetSoup = "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
    45  var commonParams []*twoOperandParams
    46  var twoOpMethods map[string]executionFunc
    47  
    48  func init() {
    49  	params := []string{ // Params is a list of common edgecases that should be used for some common tests
    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  	var (
    94  		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
    95  		stack          = newstack()
    96  		pc             = uint64(0)
    97  		evmInterpreter = env.interpreter
    98  	)
    99  
   100  	for i, test := range tests {
   101  		x := new(uint256.Int).SetBytes(common.Hex2Bytes(test.X))
   102  		y := new(uint256.Int).SetBytes(common.Hex2Bytes(test.Y))
   103  		expected := new(uint256.Int).SetBytes(common.Hex2Bytes(test.Expected))
   104  		stack.push(x)
   105  		stack.push(y)
   106  		opFn(&pc, evmInterpreter, &ScopeContext{nil, stack, nil})
   107  		if len(stack.data) != 1 {
   108  			t.Errorf("Expected one item on stack after %v, got %d: ", name, len(stack.data))
   109  		}
   110  		actual := stack.pop()
   111  
   112  		if actual.Cmp(expected) != 0 {
   113  			t.Errorf("Testcase %v %d, %v(%x, %x): expected  %x, got %x", name, i, name, x, y, expected, actual)
   114  		}
   115  	}
   116  }
   117  
   118  func TestByteOp(t *testing.T) {
   119  	tests := []TwoOperandTestcase{
   120  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", "00", "AB"},
   121  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", "01", "CD"},
   122  		{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", "00", "00"},
   123  		{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", "01", "CD"},
   124  		{"0000000000000000000000000000000000000000000000000000000000102030", "1F", "30"},
   125  		{"0000000000000000000000000000000000000000000000000000000000102030", "1E", "20"},
   126  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "20", "00"},
   127  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "FFFFFFFFFFFFFFFF", "00"},
   128  	}
   129  	testTwoOperandOp(t, tests, opByte, "byte")
   130  }
   131  
   132  func TestSHL(t *testing.T) {
   133  	// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shl-shift-left
   134  	tests := []TwoOperandTestcase{
   135  		{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000002"},
   136  		{"0000000000000000000000000000000000000000000000000000000000000001", "ff", "8000000000000000000000000000000000000000000000000000000000000000"},
   137  		{"0000000000000000000000000000000000000000000000000000000000000001", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
   138  		{"0000000000000000000000000000000000000000000000000000000000000001", "0101", "0000000000000000000000000000000000000000000000000000000000000000"},
   139  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   140  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"},
   141  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "8000000000000000000000000000000000000000000000000000000000000000"},
   142  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
   143  		{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
   144  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"},
   145  	}
   146  	testTwoOperandOp(t, tests, opSHL, "shl")
   147  }
   148  
   149  func TestSHR(t *testing.T) {
   150  	// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shr-logical-shift-right
   151  	tests := []TwoOperandTestcase{
   152  		{"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"},
   153  		{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
   154  		{"8000000000000000000000000000000000000000000000000000000000000000", "01", "4000000000000000000000000000000000000000000000000000000000000000"},
   155  		{"8000000000000000000000000000000000000000000000000000000000000000", "ff", "0000000000000000000000000000000000000000000000000000000000000001"},
   156  		{"8000000000000000000000000000000000000000000000000000000000000000", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
   157  		{"8000000000000000000000000000000000000000000000000000000000000000", "0101", "0000000000000000000000000000000000000000000000000000000000000000"},
   158  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   159  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   160  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000001"},
   161  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
   162  		{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
   163  	}
   164  	testTwoOperandOp(t, tests, opSHR, "shr")
   165  }
   166  
   167  func TestSAR(t *testing.T) {
   168  	// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#sar-arithmetic-shift-right
   169  	tests := []TwoOperandTestcase{
   170  		{"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"},
   171  		{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
   172  		{"8000000000000000000000000000000000000000000000000000000000000000", "01", "c000000000000000000000000000000000000000000000000000000000000000"},
   173  		{"8000000000000000000000000000000000000000000000000000000000000000", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   174  		{"8000000000000000000000000000000000000000000000000000000000000000", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   175  		{"8000000000000000000000000000000000000000000000000000000000000000", "0101", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   176  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   177  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   178  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   179  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   180  		{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
   181  		{"4000000000000000000000000000000000000000000000000000000000000000", "fe", "0000000000000000000000000000000000000000000000000000000000000001"},
   182  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "f8", "000000000000000000000000000000000000000000000000000000000000007f"},
   183  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "fe", "0000000000000000000000000000000000000000000000000000000000000001"},
   184  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000000"},
   185  		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
   186  	}
   187  
   188  	testTwoOperandOp(t, tests, opSAR, "sar")
   189  }
   190  
   191  func TestAddMod(t *testing.T) {
   192  	var (
   193  		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
   194  		stack          = newstack()
   195  		evmInterpreter = NewEVMInterpreter(env, env.Config)
   196  		pc             = uint64(0)
   197  	)
   198  	tests := []struct {
   199  		x        string
   200  		y        string
   201  		z        string
   202  		expected string
   203  	}{
   204  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
   205  			"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
   206  			"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
   207  			"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
   208  		},
   209  	}
   210  	// x + y = 0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd
   211  	// in 256 bit repr, fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd
   212  
   213  	for i, test := range tests {
   214  		x := new(uint256.Int).SetBytes(common.Hex2Bytes(test.x))
   215  		y := new(uint256.Int).SetBytes(common.Hex2Bytes(test.y))
   216  		z := new(uint256.Int).SetBytes(common.Hex2Bytes(test.z))
   217  		expected := new(uint256.Int).SetBytes(common.Hex2Bytes(test.expected))
   218  		stack.push(z)
   219  		stack.push(y)
   220  		stack.push(x)
   221  		opAddmod(&pc, evmInterpreter, &ScopeContext{nil, stack, nil})
   222  		actual := stack.pop()
   223  		if actual.Cmp(expected) != 0 {
   224  			t.Errorf("Testcase %d, expected  %x, got %x", i, expected, actual)
   225  		}
   226  	}
   227  }
   228  
   229  // utility function to fill the json-file with testcases
   230  // Enable this test to generate the 'testcases_xx.json' files
   231  func TestWriteExpectedValues(t *testing.T) {
   232  	t.Skip("Enable this test to create json test cases.")
   233  
   234  	// getResult is a convenience function to generate the expected values
   235  	getResult := func(args []*twoOperandParams, opFn executionFunc) []TwoOperandTestcase {
   236  		var (
   237  			env         = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
   238  			stack       = newstack()
   239  			pc          = uint64(0)
   240  			interpreter = env.interpreter
   241  		)
   242  		result := make([]TwoOperandTestcase, len(args))
   243  		for i, param := range args {
   244  			x := new(uint256.Int).SetBytes(common.Hex2Bytes(param.x))
   245  			y := new(uint256.Int).SetBytes(common.Hex2Bytes(param.y))
   246  			stack.push(x)
   247  			stack.push(y)
   248  			opFn(&pc, interpreter, &ScopeContext{nil, stack, nil})
   249  			actual := stack.pop()
   250  			result[i] = TwoOperandTestcase{param.x, param.y, fmt.Sprintf("%064x", actual)}
   251  		}
   252  		return result
   253  	}
   254  
   255  	for name, method := range twoOpMethods {
   256  		data, err := json.Marshal(getResult(commonParams, method))
   257  		if err != nil {
   258  			t.Fatal(err)
   259  		}
   260  		_ = os.WriteFile(fmt.Sprintf("testdata/testcases_%v.json", name), data, 0644)
   261  		if err != nil {
   262  			t.Fatal(err)
   263  		}
   264  	}
   265  }
   266  
   267  // TestJsonTestcases runs through all the testcases defined as json-files
   268  func TestJsonTestcases(t *testing.T) {
   269  	for name := range twoOpMethods {
   270  		data, err := os.ReadFile(fmt.Sprintf("testdata/testcases_%v.json", name))
   271  		if err != nil {
   272  			t.Fatal("Failed to read file", err)
   273  		}
   274  		var testcases []TwoOperandTestcase
   275  		json.Unmarshal(data, &testcases)
   276  		testTwoOperandOp(t, testcases, twoOpMethods[name], name)
   277  	}
   278  }
   279  
   280  func opBenchmark(bench *testing.B, op executionFunc, args ...string) {
   281  	var (
   282  		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
   283  		stack          = newstack()
   284  		scope          = &ScopeContext{nil, stack, nil}
   285  		evmInterpreter = NewEVMInterpreter(env, env.Config)
   286  	)
   287  
   288  	env.interpreter = evmInterpreter
   289  	// convert args
   290  	intArgs := make([]*uint256.Int, len(args))
   291  	for i, arg := range args {
   292  		intArgs[i] = new(uint256.Int).SetBytes(common.Hex2Bytes(arg))
   293  	}
   294  	pc := uint64(0)
   295  	bench.ResetTimer()
   296  	for i := 0; i < bench.N; i++ {
   297  		for _, arg := range intArgs {
   298  			stack.push(arg)
   299  		}
   300  		op(&pc, evmInterpreter, scope)
   301  		stack.pop()
   302  	}
   303  	bench.StopTimer()
   304  
   305  	for i, arg := range args {
   306  		want := new(uint256.Int).SetBytes(common.Hex2Bytes(arg))
   307  		if have := intArgs[i]; !want.Eq(have) {
   308  			bench.Fatalf("input #%d mutated, have %x want %x", i, have, want)
   309  		}
   310  	}
   311  }
   312  
   313  func BenchmarkOpAdd64(b *testing.B) {
   314  	x := "ffffffff"
   315  	y := "fd37f3e2bba2c4f"
   316  
   317  	opBenchmark(b, opAdd, x, y)
   318  }
   319  
   320  func BenchmarkOpAdd128(b *testing.B) {
   321  	x := "ffffffffffffffff"
   322  	y := "f5470b43c6549b016288e9a65629687"
   323  
   324  	opBenchmark(b, opAdd, x, y)
   325  }
   326  
   327  func BenchmarkOpAdd256(b *testing.B) {
   328  	x := "0802431afcbce1fc194c9eaa417b2fb67dc75a95db0bc7ec6b1c8af11df6a1da9"
   329  	y := "a1f5aac137876480252e5dcac62c354ec0d42b76b0642b6181ed099849ea1d57"
   330  
   331  	opBenchmark(b, opAdd, x, y)
   332  }
   333  
   334  func BenchmarkOpSub64(b *testing.B) {
   335  	x := "51022b6317003a9d"
   336  	y := "a20456c62e00753a"
   337  
   338  	opBenchmark(b, opSub, x, y)
   339  }
   340  
   341  func BenchmarkOpSub128(b *testing.B) {
   342  	x := "4dde30faaacdc14d00327aac314e915d"
   343  	y := "9bbc61f5559b829a0064f558629d22ba"
   344  
   345  	opBenchmark(b, opSub, x, y)
   346  }
   347  
   348  func BenchmarkOpSub256(b *testing.B) {
   349  	x := "4bfcd8bb2ac462735b48a17580690283980aa2d679f091c64364594df113ea37"
   350  	y := "97f9b1765588c4e6b69142eb00d20507301545acf3e1238c86c8b29be227d46e"
   351  
   352  	opBenchmark(b, opSub, x, y)
   353  }
   354  
   355  func BenchmarkOpMul(b *testing.B) {
   356  	x := alphabetSoup
   357  	y := alphabetSoup
   358  
   359  	opBenchmark(b, opMul, x, y)
   360  }
   361  
   362  func BenchmarkOpDiv256(b *testing.B) {
   363  	x := "ff3f9014f20db29ae04af2c2d265de17"
   364  	y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611"
   365  	opBenchmark(b, opDiv, x, y)
   366  }
   367  
   368  func BenchmarkOpDiv128(b *testing.B) {
   369  	x := "fdedc7f10142ff97"
   370  	y := "fbdfda0e2ce356173d1993d5f70a2b11"
   371  	opBenchmark(b, opDiv, x, y)
   372  }
   373  
   374  func BenchmarkOpDiv64(b *testing.B) {
   375  	x := "fcb34eb3"
   376  	y := "f97180878e839129"
   377  	opBenchmark(b, opDiv, x, y)
   378  }
   379  
   380  func BenchmarkOpSdiv(b *testing.B) {
   381  	x := "ff3f9014f20db29ae04af2c2d265de17"
   382  	y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611"
   383  
   384  	opBenchmark(b, opSdiv, x, y)
   385  }
   386  
   387  func BenchmarkOpMod(b *testing.B) {
   388  	x := alphabetSoup
   389  	y := alphabetSoup
   390  
   391  	opBenchmark(b, opMod, x, y)
   392  }
   393  
   394  func BenchmarkOpSmod(b *testing.B) {
   395  	x := alphabetSoup
   396  	y := alphabetSoup
   397  
   398  	opBenchmark(b, opSmod, x, y)
   399  }
   400  
   401  func BenchmarkOpExp(b *testing.B) {
   402  	x := alphabetSoup
   403  	y := alphabetSoup
   404  
   405  	opBenchmark(b, opExp, x, y)
   406  }
   407  
   408  func BenchmarkOpSignExtend(b *testing.B) {
   409  	x := alphabetSoup
   410  	y := alphabetSoup
   411  
   412  	opBenchmark(b, opSignExtend, x, y)
   413  }
   414  
   415  func BenchmarkOpLt(b *testing.B) {
   416  	x := alphabetSoup
   417  	y := alphabetSoup
   418  
   419  	opBenchmark(b, opLt, x, y)
   420  }
   421  
   422  func BenchmarkOpGt(b *testing.B) {
   423  	x := alphabetSoup
   424  	y := alphabetSoup
   425  
   426  	opBenchmark(b, opGt, x, y)
   427  }
   428  
   429  func BenchmarkOpSlt(b *testing.B) {
   430  	x := alphabetSoup
   431  	y := alphabetSoup
   432  
   433  	opBenchmark(b, opSlt, x, y)
   434  }
   435  
   436  func BenchmarkOpSgt(b *testing.B) {
   437  	x := alphabetSoup
   438  	y := alphabetSoup
   439  
   440  	opBenchmark(b, opSgt, x, y)
   441  }
   442  
   443  func BenchmarkOpEq(b *testing.B) {
   444  	x := alphabetSoup
   445  	y := alphabetSoup
   446  
   447  	opBenchmark(b, opEq, x, y)
   448  }
   449  func BenchmarkOpEq2(b *testing.B) {
   450  	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
   451  	y := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201fffffffe"
   452  	opBenchmark(b, opEq, x, y)
   453  }
   454  func BenchmarkOpAnd(b *testing.B) {
   455  	x := alphabetSoup
   456  	y := alphabetSoup
   457  
   458  	opBenchmark(b, opAnd, x, y)
   459  }
   460  
   461  func BenchmarkOpOr(b *testing.B) {
   462  	x := alphabetSoup
   463  	y := alphabetSoup
   464  
   465  	opBenchmark(b, opOr, x, y)
   466  }
   467  
   468  func BenchmarkOpXor(b *testing.B) {
   469  	x := alphabetSoup
   470  	y := alphabetSoup
   471  
   472  	opBenchmark(b, opXor, x, y)
   473  }
   474  
   475  func BenchmarkOpByte(b *testing.B) {
   476  	x := alphabetSoup
   477  	y := alphabetSoup
   478  
   479  	opBenchmark(b, opByte, x, y)
   480  }
   481  
   482  func BenchmarkOpAddmod(b *testing.B) {
   483  	x := alphabetSoup
   484  	y := alphabetSoup
   485  	z := alphabetSoup
   486  
   487  	opBenchmark(b, opAddmod, x, y, z)
   488  }
   489  
   490  func BenchmarkOpMulmod(b *testing.B) {
   491  	x := alphabetSoup
   492  	y := alphabetSoup
   493  	z := alphabetSoup
   494  
   495  	opBenchmark(b, opMulmod, x, y, z)
   496  }
   497  
   498  func BenchmarkOpSHL(b *testing.B) {
   499  	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
   500  	y := "ff"
   501  
   502  	opBenchmark(b, opSHL, x, y)
   503  }
   504  func BenchmarkOpSHR(b *testing.B) {
   505  	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
   506  	y := "ff"
   507  
   508  	opBenchmark(b, opSHR, x, y)
   509  }
   510  func BenchmarkOpSAR(b *testing.B) {
   511  	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
   512  	y := "ff"
   513  
   514  	opBenchmark(b, opSAR, x, y)
   515  }
   516  func BenchmarkOpIsZero(b *testing.B) {
   517  	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
   518  	opBenchmark(b, opIszero, x)
   519  }
   520  
   521  func TestOpMstore(t *testing.T) {
   522  	var (
   523  		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
   524  		stack          = newstack()
   525  		mem            = NewMemory()
   526  		evmInterpreter = NewEVMInterpreter(env, env.Config)
   527  	)
   528  
   529  	env.interpreter = evmInterpreter
   530  	mem.Resize(64)
   531  	pc := uint64(0)
   532  	v := "abcdef00000000000000abba000000000deaf000000c0de00100000000133700"
   533  	stack.push(new(uint256.Int).SetBytes(common.Hex2Bytes(v)))
   534  	stack.push(new(uint256.Int))
   535  	opMstore(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})
   536  	if got := common.Bytes2Hex(mem.GetCopy(0, 32)); got != v {
   537  		t.Fatalf("Mstore fail, got %v, expected %v", got, v)
   538  	}
   539  	stack.push(new(uint256.Int).SetUint64(0x1))
   540  	stack.push(new(uint256.Int))
   541  	opMstore(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})
   542  	if common.Bytes2Hex(mem.GetCopy(0, 32)) != "0000000000000000000000000000000000000000000000000000000000000001" {
   543  		t.Fatalf("Mstore failed to overwrite previous value")
   544  	}
   545  }
   546  
   547  func BenchmarkOpMstore(bench *testing.B) {
   548  	var (
   549  		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
   550  		stack          = newstack()
   551  		mem            = NewMemory()
   552  		evmInterpreter = NewEVMInterpreter(env, env.Config)
   553  	)
   554  
   555  	env.interpreter = evmInterpreter
   556  	mem.Resize(64)
   557  	pc := uint64(0)
   558  	memStart := new(uint256.Int)
   559  	value := new(uint256.Int).SetUint64(0x1337)
   560  
   561  	bench.ResetTimer()
   562  	for i := 0; i < bench.N; i++ {
   563  		stack.push(value)
   564  		stack.push(memStart)
   565  		opMstore(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})
   566  	}
   567  }
   568  
   569  func BenchmarkOpKeccak256(bench *testing.B) {
   570  	var (
   571  		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
   572  		stack          = newstack()
   573  		mem            = NewMemory()
   574  		evmInterpreter = NewEVMInterpreter(env, env.Config)
   575  	)
   576  	env.interpreter = evmInterpreter
   577  	mem.Resize(32)
   578  	pc := uint64(0)
   579  	start := new(uint256.Int)
   580  
   581  	bench.ResetTimer()
   582  	for i := 0; i < bench.N; i++ {
   583  		stack.push(uint256.NewInt(32))
   584  		stack.push(start)
   585  		opKeccak256(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})
   586  	}
   587  }
   588  
   589  func TestCreate2Addreses(t *testing.T) {
   590  	type testcase struct {
   591  		origin   string
   592  		salt     string
   593  		code     string
   594  		expected string
   595  	}
   596  
   597  	for i, tt := range []testcase{
   598  		{
   599  			origin:   "0x0000000000000000000000000000000000000000",
   600  			salt:     "0x0000000000000000000000000000000000000000",
   601  			code:     "0x00",
   602  			expected: "0x4d1a2e2bb4f88f0250f26ffff098b0b30b26bf38",
   603  		},
   604  		{
   605  			origin:   "0xdeadbeef00000000000000000000000000000000",
   606  			salt:     "0x0000000000000000000000000000000000000000",
   607  			code:     "0x00",
   608  			expected: "0xB928f69Bb1D91Cd65274e3c79d8986362984fDA3",
   609  		},
   610  		{
   611  			origin:   "0xdeadbeef00000000000000000000000000000000",
   612  			salt:     "0xfeed000000000000000000000000000000000000",
   613  			code:     "0x00",
   614  			expected: "0xD04116cDd17beBE565EB2422F2497E06cC1C9833",
   615  		},
   616  		{
   617  			origin:   "0x0000000000000000000000000000000000000000",
   618  			salt:     "0x0000000000000000000000000000000000000000",
   619  			code:     "0xdeadbeef",
   620  			expected: "0x70f2b2914A2a4b783FaEFb75f459A580616Fcb5e",
   621  		},
   622  		{
   623  			origin:   "0x00000000000000000000000000000000deadbeef",
   624  			salt:     "0xcafebabe",
   625  			code:     "0xdeadbeef",
   626  			expected: "0x60f3f640a8508fC6a86d45DF051962668E1e8AC7",
   627  		},
   628  		{
   629  			origin:   "0x00000000000000000000000000000000deadbeef",
   630  			salt:     "0xcafebabe",
   631  			code:     "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
   632  			expected: "0x1d8bfDC5D46DC4f61D6b6115972536eBE6A8854C",
   633  		},
   634  		{
   635  			origin:   "0x0000000000000000000000000000000000000000",
   636  			salt:     "0x0000000000000000000000000000000000000000",
   637  			code:     "0x",
   638  			expected: "0xE33C0C7F7df4809055C3ebA6c09CFe4BaF1BD9e0",
   639  		},
   640  	} {
   641  		origin := common.BytesToAddress(common.FromHex(tt.origin))
   642  		salt := common.BytesToHash(common.FromHex(tt.salt))
   643  		code := common.FromHex(tt.code)
   644  		codeHash := crypto.Keccak256(code)
   645  		address := crypto.CreateAddress2(origin, salt, codeHash)
   646  		/*
   647  			stack          := newstack()
   648  			// salt, but we don't need that for this test
   649  			stack.push(big.NewInt(int64(len(code)))) //size
   650  			stack.push(big.NewInt(0)) // memstart
   651  			stack.push(big.NewInt(0)) // value
   652  			gas, _ := gasCreate2(params.GasTable{}, nil, nil, stack, nil, 0)
   653  			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())
   654  		*/
   655  		expected := common.BytesToAddress(common.FromHex(tt.expected))
   656  		if !bytes.Equal(expected.Bytes(), address.Bytes()) {
   657  			t.Errorf("test %d: expected %s, got %s", i, expected.String(), address.String())
   658  		}
   659  	}
   660  }
   661  
   662  func TestRandom(t *testing.T) {
   663  	type testcase struct {
   664  		name   string
   665  		random common.Hash
   666  	}
   667  
   668  	for _, tt := range []testcase{
   669  		{name: "empty hash", random: common.Hash{}},
   670  		{name: "1", random: common.Hash{0}},
   671  		{name: "emptyCodeHash", random: emptyCodeHash},
   672  		{name: "hash(0x010203)", random: crypto.Keccak256Hash([]byte{0x01, 0x02, 0x03})},
   673  	} {
   674  		var (
   675  			env            = NewEVM(BlockContext{Random: &tt.random}, TxContext{}, nil, params.TestChainConfig, Config{})
   676  			stack          = newstack()
   677  			pc             = uint64(0)
   678  			evmInterpreter = env.interpreter
   679  		)
   680  		opRandom(&pc, evmInterpreter, &ScopeContext{nil, stack, nil})
   681  		if len(stack.data) != 1 {
   682  			t.Errorf("Expected one item on stack after %v, got %d: ", tt.name, len(stack.data))
   683  		}
   684  		actual := stack.pop()
   685  		expected, overflow := uint256.FromBig(new(big.Int).SetBytes(tt.random.Bytes()))
   686  		if overflow {
   687  			t.Errorf("Testcase %v: invalid overflow", tt.name)
   688  		}
   689  		if actual.Cmp(expected) != 0 {
   690  			t.Errorf("Testcase %v: expected  %x, got %x", tt.name, expected, actual)
   691  		}
   692  	}
   693  }