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