instructions_test.go 23.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

17 18 19
package vm

import (
20
	"bytes"
21 22
	"encoding/json"
	"fmt"
23
	"math/big"
24
	"os"
25 26 27
	"testing"

	"github.com/ethereum/go-ethereum/common"
28
	"github.com/ethereum/go-ethereum/crypto"
29
	"github.com/ethereum/go-ethereum/params"
30
	"github.com/holiman/uint256"
31 32
)

33 34 35 36
type TwoOperandTestcase struct {
	X        string
	Y        string
	Expected string
37 38
}

39 40 41 42 43
type twoOperandParams struct {
	x string
	y string
}

44
var alphabetSoup = "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
var commonParams []*twoOperandParams
var twoOpMethods map[string]executionFunc

func init() {

	// Params is a list of common edgecases that should be used for some common tests
	params := []string{
		"0000000000000000000000000000000000000000000000000000000000000000", // 0
		"0000000000000000000000000000000000000000000000000000000000000001", // +1
		"0000000000000000000000000000000000000000000000000000000000000005", // +5
		"7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe", // + max -1
		"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // + max
		"8000000000000000000000000000000000000000000000000000000000000000", // - max
		"8000000000000000000000000000000000000000000000000000000000000001", // - max+1
		"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", // - 5
		"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // - 1
	}
	// Params are combined so each param is used on each 'side'
	commonParams = make([]*twoOperandParams, len(params)*len(params))
	for i, x := range params {
		for j, y := range params {
			commonParams[i*len(params)+j] = &twoOperandParams{x, y}
		}
	}
	twoOpMethods = map[string]executionFunc{
		"add":     opAdd,
		"sub":     opSub,
		"mul":     opMul,
		"div":     opDiv,
		"sdiv":    opSdiv,
		"mod":     opMod,
		"smod":    opSmod,
		"exp":     opExp,
		"signext": opSignExtend,
		"lt":      opLt,
		"gt":      opGt,
		"slt":     opSlt,
		"sgt":     opSgt,
		"eq":      opEq,
		"and":     opAnd,
		"or":      opOr,
		"xor":     opXor,
		"byte":    opByte,
		"shl":     opSHL,
		"shr":     opSHR,
		"sar":     opSAR,
	}
}

func testTwoOperandOp(t *testing.T, tests []TwoOperandTestcase, opFn executionFunc, name string) {

96
	var (
97
		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
98 99
		stack          = newstack()
		pc             = uint64(0)
100
		evmInterpreter = env.interpreter
101
	)
102

103
	for i, test := range tests {
104 105 106
		x := new(uint256.Int).SetBytes(common.Hex2Bytes(test.X))
		y := new(uint256.Int).SetBytes(common.Hex2Bytes(test.Y))
		expected := new(uint256.Int).SetBytes(common.Hex2Bytes(test.Expected))
107
		stack.push(x)
108
		stack.push(y)
109
		opFn(&pc, evmInterpreter, &ScopeContext{nil, stack, nil})
110 111 112
		if len(stack.data) != 1 {
			t.Errorf("Expected one item on stack after %v, got %d: ", name, len(stack.data))
		}
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 123 124 125 126 127 128 129 130
	tests := []TwoOperandTestcase{
		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", "00", "AB"},
		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", "01", "CD"},
		{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", "00", "00"},
		{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", "01", "CD"},
		{"0000000000000000000000000000000000000000000000000000000000102030", "1F", "30"},
		{"0000000000000000000000000000000000000000000000000000000000102030", "1E", "20"},
		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "20", "00"},
		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "FFFFFFFFFFFFFFFF", "00"},
131
	}
132
	testTwoOperandOp(t, tests, opByte, "byte")
133
}
134

135 136
func TestSHL(t *testing.T) {
	// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shl-shift-left
137
	tests := []TwoOperandTestcase{
138 139 140 141 142 143 144 145 146 147 148
		{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000002"},
		{"0000000000000000000000000000000000000000000000000000000000000001", "ff", "8000000000000000000000000000000000000000000000000000000000000000"},
		{"0000000000000000000000000000000000000000000000000000000000000001", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
		{"0000000000000000000000000000000000000000000000000000000000000001", "0101", "0000000000000000000000000000000000000000000000000000000000000000"},
		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"},
		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "8000000000000000000000000000000000000000000000000000000000000000"},
		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
		{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"},
	}
149
	testTwoOperandOp(t, tests, opSHL, "shl")
150 151 152 153
}

func TestSHR(t *testing.T) {
	// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shr-logical-shift-right
154
	tests := []TwoOperandTestcase{
155 156 157 158 159 160 161 162 163 164 165 166
		{"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"},
		{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
		{"8000000000000000000000000000000000000000000000000000000000000000", "01", "4000000000000000000000000000000000000000000000000000000000000000"},
		{"8000000000000000000000000000000000000000000000000000000000000000", "ff", "0000000000000000000000000000000000000000000000000000000000000001"},
		{"8000000000000000000000000000000000000000000000000000000000000000", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
		{"8000000000000000000000000000000000000000000000000000000000000000", "0101", "0000000000000000000000000000000000000000000000000000000000000000"},
		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000001"},
		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
		{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
	}
167
	testTwoOperandOp(t, tests, opSHR, "shr")
168 169 170 171
}

func TestSAR(t *testing.T) {
	// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#sar-arithmetic-shift-right
172
	tests := []TwoOperandTestcase{
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
		{"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"},
		{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
		{"8000000000000000000000000000000000000000000000000000000000000000", "01", "c000000000000000000000000000000000000000000000000000000000000000"},
		{"8000000000000000000000000000000000000000000000000000000000000000", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
		{"8000000000000000000000000000000000000000000000000000000000000000", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
		{"8000000000000000000000000000000000000000000000000000000000000000", "0101", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
		{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
		{"4000000000000000000000000000000000000000000000000000000000000000", "fe", "0000000000000000000000000000000000000000000000000000000000000001"},
		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "f8", "000000000000000000000000000000000000000000000000000000000000007f"},
		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "fe", "0000000000000000000000000000000000000000000000000000000000000001"},
		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000000"},
		{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
	}

191 192 193
	testTwoOperandOp(t, tests, opSAR, "sar")
}

194 195
func TestAddMod(t *testing.T) {
	var (
196
		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
197
		stack          = newstack()
198
		evmInterpreter = NewEVMInterpreter(env, env.Config)
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
		pc             = uint64(0)
	)
	tests := []struct {
		x        string
		y        string
		z        string
		expected string
	}{
		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
			"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
			"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
			"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
		},
	}
	// x + y = 0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd
	// in 256 bit repr, fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd

	for i, test := range tests {
		x := new(uint256.Int).SetBytes(common.Hex2Bytes(test.x))
		y := new(uint256.Int).SetBytes(common.Hex2Bytes(test.y))
		z := new(uint256.Int).SetBytes(common.Hex2Bytes(test.z))
		expected := new(uint256.Int).SetBytes(common.Hex2Bytes(test.expected))
		stack.push(z)
		stack.push(y)
		stack.push(x)
224
		opAddmod(&pc, evmInterpreter, &ScopeContext{nil, stack, nil})
225 226 227 228 229 230 231
		actual := stack.pop()
		if actual.Cmp(expected) != 0 {
			t.Errorf("Testcase %d, expected  %x, got %x", i, expected, actual)
		}
	}
}

232 233 234
// getResult is a convenience function to generate the expected values
func getResult(args []*twoOperandParams, opFn executionFunc) []TwoOperandTestcase {
	var (
235 236 237
		env         = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
		stack       = newstack()
		pc          = uint64(0)
238
		interpreter = env.interpreter
239 240 241
	)
	result := make([]TwoOperandTestcase, len(args))
	for i, param := range args {
242 243
		x := new(uint256.Int).SetBytes(common.Hex2Bytes(param.x))
		y := new(uint256.Int).SetBytes(common.Hex2Bytes(param.y))
244 245
		stack.push(x)
		stack.push(y)
246
		opFn(&pc, interpreter, &ScopeContext{nil, stack, nil})
247 248 249 250
		actual := stack.pop()
		result[i] = TwoOperandTestcase{param.x, param.y, fmt.Sprintf("%064x", actual)}
	}
	return result
251 252
}

253 254
// utility function to fill the json-file with testcases
// Enable this test to generate the 'testcases_xx.json' files
255 256 257
func TestWriteExpectedValues(t *testing.T) {
	t.Skip("Enable this test to create json test cases.")

258 259 260 261 262
	for name, method := range twoOpMethods {
		data, err := json.Marshal(getResult(commonParams, method))
		if err != nil {
			t.Fatal(err)
		}
263
		_ = os.WriteFile(fmt.Sprintf("testdata/testcases_%v.json", name), data, 0644)
264 265 266
		if err != nil {
			t.Fatal(err)
		}
267 268 269
	}
}

270 271 272
// TestJsonTestcases runs through all the testcases defined as json-files
func TestJsonTestcases(t *testing.T) {
	for name := range twoOpMethods {
273
		data, err := os.ReadFile(fmt.Sprintf("testdata/testcases_%v.json", name))
274 275 276 277 278 279
		if err != nil {
			t.Fatal("Failed to read file", err)
		}
		var testcases []TwoOperandTestcase
		json.Unmarshal(data, &testcases)
		testTwoOperandOp(t, testcases, twoOpMethods[name], name)
280 281 282
	}
}

283
func opBenchmark(bench *testing.B, op executionFunc, args ...string) {
284
	var (
285
		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
286
		stack          = newstack()
287
		scope          = &ScopeContext{nil, stack, nil}
288
		evmInterpreter = NewEVMInterpreter(env, env.Config)
289
	)
290 291

	env.interpreter = evmInterpreter
292
	// convert args
293
	intArgs := make([]*uint256.Int, len(args))
294
	for i, arg := range args {
295
		intArgs[i] = new(uint256.Int).SetBytes(common.Hex2Bytes(arg))
296 297 298 299
	}
	pc := uint64(0)
	bench.ResetTimer()
	for i := 0; i < bench.N; i++ {
300 301
		for _, arg := range intArgs {
			stack.push(arg)
302
		}
303
		op(&pc, evmInterpreter, scope)
304 305
		stack.pop()
	}
306 307 308 309 310 311 312 313
	bench.StopTimer()

	for i, arg := range args {
		want := new(uint256.Int).SetBytes(common.Hex2Bytes(arg))
		if have := intArgs[i]; !want.Eq(have) {
			bench.Fatalf("input #%d mutated, have %x want %x", i, have, want)
		}
	}
314 315
}

316 317 318
func BenchmarkOpAdd64(b *testing.B) {
	x := "ffffffff"
	y := "fd37f3e2bba2c4f"
319

320
	opBenchmark(b, opAdd, x, y)
321
}
322

323 324 325
func BenchmarkOpAdd128(b *testing.B) {
	x := "ffffffffffffffff"
	y := "f5470b43c6549b016288e9a65629687"
326

327
	opBenchmark(b, opAdd, x, y)
328
}
329

330 331 332
func BenchmarkOpAdd256(b *testing.B) {
	x := "0802431afcbce1fc194c9eaa417b2fb67dc75a95db0bc7ec6b1c8af11df6a1da9"
	y := "a1f5aac137876480252e5dcac62c354ec0d42b76b0642b6181ed099849ea1d57"
333

334
	opBenchmark(b, opAdd, x, y)
335 336
}

337 338 339
func BenchmarkOpSub64(b *testing.B) {
	x := "51022b6317003a9d"
	y := "a20456c62e00753a"
340

341
	opBenchmark(b, opSub, x, y)
342 343
}

344 345 346
func BenchmarkOpSub128(b *testing.B) {
	x := "4dde30faaacdc14d00327aac314e915d"
	y := "9bbc61f5559b829a0064f558629d22ba"
347

348
	opBenchmark(b, opSub, x, y)
349
}
350

351 352 353
func BenchmarkOpSub256(b *testing.B) {
	x := "4bfcd8bb2ac462735b48a17580690283980aa2d679f091c64364594df113ea37"
	y := "97f9b1765588c4e6b69142eb00d20507301545acf3e1238c86c8b29be227d46e"
354 355 356

	opBenchmark(b, opSub, x, y)
}
357

358
func BenchmarkOpMul(b *testing.B) {
359 360
	x := alphabetSoup
	y := alphabetSoup
361 362 363

	opBenchmark(b, opMul, x, y)
}
364

365 366 367 368 369
func BenchmarkOpDiv256(b *testing.B) {
	x := "ff3f9014f20db29ae04af2c2d265de17"
	y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611"
	opBenchmark(b, opDiv, x, y)
}
370

371 372 373 374 375 376 377 378 379
func BenchmarkOpDiv128(b *testing.B) {
	x := "fdedc7f10142ff97"
	y := "fbdfda0e2ce356173d1993d5f70a2b11"
	opBenchmark(b, opDiv, x, y)
}

func BenchmarkOpDiv64(b *testing.B) {
	x := "fcb34eb3"
	y := "f97180878e839129"
380 381
	opBenchmark(b, opDiv, x, y)
}
382

383
func BenchmarkOpSdiv(b *testing.B) {
384 385
	x := "ff3f9014f20db29ae04af2c2d265de17"
	y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611"
386 387 388

	opBenchmark(b, opSdiv, x, y)
}
389

390
func BenchmarkOpMod(b *testing.B) {
391 392
	x := alphabetSoup
	y := alphabetSoup
393 394 395

	opBenchmark(b, opMod, x, y)
}
396

397
func BenchmarkOpSmod(b *testing.B) {
398 399
	x := alphabetSoup
	y := alphabetSoup
400 401 402

	opBenchmark(b, opSmod, x, y)
}
403

404
func BenchmarkOpExp(b *testing.B) {
405 406
	x := alphabetSoup
	y := alphabetSoup
407 408 409

	opBenchmark(b, opExp, x, y)
}
410

411
func BenchmarkOpSignExtend(b *testing.B) {
412 413
	x := alphabetSoup
	y := alphabetSoup
414 415 416

	opBenchmark(b, opSignExtend, x, y)
}
417

418
func BenchmarkOpLt(b *testing.B) {
419 420
	x := alphabetSoup
	y := alphabetSoup
421 422 423

	opBenchmark(b, opLt, x, y)
}
424

425
func BenchmarkOpGt(b *testing.B) {
426 427
	x := alphabetSoup
	y := alphabetSoup
428 429 430

	opBenchmark(b, opGt, x, y)
}
431

432
func BenchmarkOpSlt(b *testing.B) {
433 434
	x := alphabetSoup
	y := alphabetSoup
435 436 437

	opBenchmark(b, opSlt, x, y)
}
438

439
func BenchmarkOpSgt(b *testing.B) {
440 441
	x := alphabetSoup
	y := alphabetSoup
442 443 444

	opBenchmark(b, opSgt, x, y)
}
445

446
func BenchmarkOpEq(b *testing.B) {
447 448
	x := alphabetSoup
	y := alphabetSoup
449 450 451

	opBenchmark(b, opEq, x, y)
}
452 453 454 455 456
func BenchmarkOpEq2(b *testing.B) {
	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
	y := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201fffffffe"
	opBenchmark(b, opEq, x, y)
}
457
func BenchmarkOpAnd(b *testing.B) {
458 459
	x := alphabetSoup
	y := alphabetSoup
460 461 462

	opBenchmark(b, opAnd, x, y)
}
463

464
func BenchmarkOpOr(b *testing.B) {
465 466
	x := alphabetSoup
	y := alphabetSoup
467 468 469

	opBenchmark(b, opOr, x, y)
}
470

471
func BenchmarkOpXor(b *testing.B) {
472 473
	x := alphabetSoup
	y := alphabetSoup
474 475 476

	opBenchmark(b, opXor, x, y)
}
477

478
func BenchmarkOpByte(b *testing.B) {
479 480
	x := alphabetSoup
	y := alphabetSoup
481 482 483 484 485

	opBenchmark(b, opByte, x, y)
}

func BenchmarkOpAddmod(b *testing.B) {
486 487 488
	x := alphabetSoup
	y := alphabetSoup
	z := alphabetSoup
489 490 491

	opBenchmark(b, opAddmod, x, y, z)
}
492

493
func BenchmarkOpMulmod(b *testing.B) {
494 495 496
	x := alphabetSoup
	y := alphabetSoup
	z := alphabetSoup
497 498 499

	opBenchmark(b, opMulmod, x, y, z)
}
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518

func BenchmarkOpSHL(b *testing.B) {
	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
	y := "ff"

	opBenchmark(b, opSHL, x, y)
}
func BenchmarkOpSHR(b *testing.B) {
	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
	y := "ff"

	opBenchmark(b, opSHR, x, y)
}
func BenchmarkOpSAR(b *testing.B) {
	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
	y := "ff"

	opBenchmark(b, opSAR, x, y)
}
519 520 521 522
func BenchmarkOpIsZero(b *testing.B) {
	x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
	opBenchmark(b, opIszero, x)
}
523 524 525

func TestOpMstore(t *testing.T) {
	var (
526
		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
527
		stack          = newstack()
528
		mem            = NewMemory()
529
		evmInterpreter = NewEVMInterpreter(env, env.Config)
530
	)
531 532

	env.interpreter = evmInterpreter
533 534 535
	mem.Resize(64)
	pc := uint64(0)
	v := "abcdef00000000000000abba000000000deaf000000c0de00100000000133700"
536 537
	stack.push(new(uint256.Int).SetBytes(common.Hex2Bytes(v)))
	stack.push(new(uint256.Int))
538
	opMstore(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})
539
	if got := common.Bytes2Hex(mem.GetCopy(0, 32)); got != v {
540 541
		t.Fatalf("Mstore fail, got %v, expected %v", got, v)
	}
542 543
	stack.push(new(uint256.Int).SetUint64(0x1))
	stack.push(new(uint256.Int))
544
	opMstore(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})
545
	if common.Bytes2Hex(mem.GetCopy(0, 32)) != "0000000000000000000000000000000000000000000000000000000000000001" {
546 547 548 549 550 551
		t.Fatalf("Mstore failed to overwrite previous value")
	}
}

func BenchmarkOpMstore(bench *testing.B) {
	var (
552
		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
553
		stack          = newstack()
554
		mem            = NewMemory()
555
		evmInterpreter = NewEVMInterpreter(env, env.Config)
556
	)
557 558

	env.interpreter = evmInterpreter
559 560
	mem.Resize(64)
	pc := uint64(0)
561 562
	memStart := new(uint256.Int)
	value := new(uint256.Int).SetUint64(0x1337)
563 564 565

	bench.ResetTimer()
	for i := 0; i < bench.N; i++ {
566 567
		stack.push(value)
		stack.push(memStart)
568
		opMstore(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})
569 570
	}
}
571

572
func BenchmarkOpKeccak256(bench *testing.B) {
573
	var (
574
		env            = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
575
		stack          = newstack()
576
		mem            = NewMemory()
577
		evmInterpreter = NewEVMInterpreter(env, env.Config)
578 579 580 581
	)
	env.interpreter = evmInterpreter
	mem.Resize(32)
	pc := uint64(0)
582
	start := new(uint256.Int)
583 584 585

	bench.ResetTimer()
	for i := 0; i < bench.N; i++ {
586 587
		stack.push(uint256.NewInt(32))
		stack.push(start)
588
		opKeccak256(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})
589 590 591
	}
}

592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
func TestCreate2Addreses(t *testing.T) {
	type testcase struct {
		origin   string
		salt     string
		code     string
		expected string
	}

	for i, tt := range []testcase{
		{
			origin:   "0x0000000000000000000000000000000000000000",
			salt:     "0x0000000000000000000000000000000000000000",
			code:     "0x00",
			expected: "0x4d1a2e2bb4f88f0250f26ffff098b0b30b26bf38",
		},
		{
			origin:   "0xdeadbeef00000000000000000000000000000000",
			salt:     "0x0000000000000000000000000000000000000000",
			code:     "0x00",
			expected: "0xB928f69Bb1D91Cd65274e3c79d8986362984fDA3",
		},
		{
			origin:   "0xdeadbeef00000000000000000000000000000000",
			salt:     "0xfeed000000000000000000000000000000000000",
			code:     "0x00",
			expected: "0xD04116cDd17beBE565EB2422F2497E06cC1C9833",
		},
		{
			origin:   "0x0000000000000000000000000000000000000000",
			salt:     "0x0000000000000000000000000000000000000000",
			code:     "0xdeadbeef",
			expected: "0x70f2b2914A2a4b783FaEFb75f459A580616Fcb5e",
		},
		{
			origin:   "0x00000000000000000000000000000000deadbeef",
			salt:     "0xcafebabe",
			code:     "0xdeadbeef",
			expected: "0x60f3f640a8508fC6a86d45DF051962668E1e8AC7",
		},
		{
			origin:   "0x00000000000000000000000000000000deadbeef",
			salt:     "0xcafebabe",
			code:     "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
			expected: "0x1d8bfDC5D46DC4f61D6b6115972536eBE6A8854C",
		},
		{
			origin:   "0x0000000000000000000000000000000000000000",
			salt:     "0x0000000000000000000000000000000000000000",
			code:     "0x",
			expected: "0xE33C0C7F7df4809055C3ebA6c09CFe4BaF1BD9e0",
		},
	} {

		origin := common.BytesToAddress(common.FromHex(tt.origin))
		salt := common.BytesToHash(common.FromHex(tt.salt))
		code := common.FromHex(tt.code)
648 649
		codeHash := crypto.Keccak256(code)
		address := crypto.CreateAddress2(origin, salt, codeHash)
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
		/*
			stack          := newstack()
			// salt, but we don't need that for this test
			stack.push(big.NewInt(int64(len(code)))) //size
			stack.push(big.NewInt(0)) // memstart
			stack.push(big.NewInt(0)) // value
			gas, _ := gasCreate2(params.GasTable{}, nil, nil, stack, nil, 0)
			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())
		*/
		expected := common.BytesToAddress(common.FromHex(tt.expected))
		if !bytes.Equal(expected.Bytes(), address.Bytes()) {
			t.Errorf("test %d: expected %s, got %s", i, expected.String(), address.String())
		}
	}
}
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697

func TestRandom(t *testing.T) {
	type testcase struct {
		name   string
		random common.Hash
	}

	for _, tt := range []testcase{
		{name: "empty hash", random: common.Hash{}},
		{name: "1", random: common.Hash{0}},
		{name: "emptyCodeHash", random: emptyCodeHash},
		{name: "hash(0x010203)", random: crypto.Keccak256Hash([]byte{0x01, 0x02, 0x03})},
	} {
		var (
			env            = NewEVM(BlockContext{Random: &tt.random}, TxContext{}, nil, params.TestChainConfig, Config{})
			stack          = newstack()
			pc             = uint64(0)
			evmInterpreter = env.interpreter
		)
		opRandom(&pc, evmInterpreter, &ScopeContext{nil, stack, nil})
		if len(stack.data) != 1 {
			t.Errorf("Expected one item on stack after %v, got %d: ", tt.name, len(stack.data))
		}
		actual := stack.pop()
		expected, overflow := uint256.FromBig(new(big.Int).SetBytes(tt.random.Bytes()))
		if overflow {
			t.Errorf("Testcase %v: invalid overflow", tt.name)
		}
		if actual.Cmp(expected) != 0 {
			t.Errorf("Testcase %v: expected  %x, got %x", tt.name, expected, actual)
		}
	}
}