github.com/gobitfly/go-ethereum@v1.8.12/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 "math/big" 21 "testing" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/params" 25 ) 26 27 type twoOperandTest struct { 28 x string 29 y string 30 expected string 31 } 32 33 func testTwoOperandOp(t *testing.T, tests []twoOperandTest, opFn func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error)) { 34 var ( 35 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 36 stack = newstack() 37 pc = uint64(0) 38 ) 39 env.interpreter.intPool = poolOfIntPools.get() 40 for i, test := range tests { 41 x := new(big.Int).SetBytes(common.Hex2Bytes(test.x)) 42 shift := new(big.Int).SetBytes(common.Hex2Bytes(test.y)) 43 expected := new(big.Int).SetBytes(common.Hex2Bytes(test.expected)) 44 stack.push(x) 45 stack.push(shift) 46 opFn(&pc, env, nil, nil, stack) 47 actual := stack.pop() 48 if actual.Cmp(expected) != 0 { 49 t.Errorf("Testcase %d, expected %v, got %v", i, expected, actual) 50 } 51 // Check pool usage 52 // 1.pool is not allowed to contain anything on the stack 53 // 2.pool is not allowed to contain the same pointers twice 54 if env.interpreter.intPool.pool.len() > 0 { 55 56 poolvals := make(map[*big.Int]struct{}) 57 poolvals[actual] = struct{}{} 58 59 for env.interpreter.intPool.pool.len() > 0 { 60 key := env.interpreter.intPool.get() 61 if _, exist := poolvals[key]; exist { 62 t.Errorf("Testcase %d, pool contains double-entry", i) 63 } 64 poolvals[key] = struct{}{} 65 } 66 } 67 } 68 poolOfIntPools.put(env.interpreter.intPool) 69 } 70 71 func TestByteOp(t *testing.T) { 72 var ( 73 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 74 stack = newstack() 75 ) 76 env.interpreter.intPool = poolOfIntPools.get() 77 tests := []struct { 78 v string 79 th uint64 80 expected *big.Int 81 }{ 82 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 0, big.NewInt(0xAB)}, 83 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 1, big.NewInt(0xCD)}, 84 {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 0, big.NewInt(0x00)}, 85 {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 1, big.NewInt(0xCD)}, 86 {"0000000000000000000000000000000000000000000000000000000000102030", 31, big.NewInt(0x30)}, 87 {"0000000000000000000000000000000000000000000000000000000000102030", 30, big.NewInt(0x20)}, 88 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32, big.NewInt(0x0)}, 89 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0xFFFFFFFFFFFFFFFF, big.NewInt(0x0)}, 90 } 91 pc := uint64(0) 92 for _, test := range tests { 93 val := new(big.Int).SetBytes(common.Hex2Bytes(test.v)) 94 th := new(big.Int).SetUint64(test.th) 95 stack.push(val) 96 stack.push(th) 97 opByte(&pc, env, nil, nil, stack) 98 actual := stack.pop() 99 if actual.Cmp(test.expected) != 0 { 100 t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.v, test.th, test.expected, actual) 101 } 102 } 103 poolOfIntPools.put(env.interpreter.intPool) 104 } 105 106 func TestSHL(t *testing.T) { 107 // Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shl-shift-left 108 tests := []twoOperandTest{ 109 {"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"}, 110 {"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000002"}, 111 {"0000000000000000000000000000000000000000000000000000000000000001", "ff", "8000000000000000000000000000000000000000000000000000000000000000"}, 112 {"0000000000000000000000000000000000000000000000000000000000000001", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 113 {"0000000000000000000000000000000000000000000000000000000000000001", "0101", "0000000000000000000000000000000000000000000000000000000000000000"}, 114 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 115 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"}, 116 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "8000000000000000000000000000000000000000000000000000000000000000"}, 117 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 118 {"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 119 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"}, 120 } 121 testTwoOperandOp(t, tests, opSHL) 122 } 123 124 func TestSHR(t *testing.T) { 125 // Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shr-logical-shift-right 126 tests := []twoOperandTest{ 127 {"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"}, 128 {"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 129 {"8000000000000000000000000000000000000000000000000000000000000000", "01", "4000000000000000000000000000000000000000000000000000000000000000"}, 130 {"8000000000000000000000000000000000000000000000000000000000000000", "ff", "0000000000000000000000000000000000000000000000000000000000000001"}, 131 {"8000000000000000000000000000000000000000000000000000000000000000", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 132 {"8000000000000000000000000000000000000000000000000000000000000000", "0101", "0000000000000000000000000000000000000000000000000000000000000000"}, 133 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 134 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 135 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000001"}, 136 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 137 {"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 138 } 139 testTwoOperandOp(t, tests, opSHR) 140 } 141 142 func TestSAR(t *testing.T) { 143 // Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#sar-arithmetic-shift-right 144 tests := []twoOperandTest{ 145 {"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"}, 146 {"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 147 {"8000000000000000000000000000000000000000000000000000000000000000", "01", "c000000000000000000000000000000000000000000000000000000000000000"}, 148 {"8000000000000000000000000000000000000000000000000000000000000000", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 149 {"8000000000000000000000000000000000000000000000000000000000000000", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 150 {"8000000000000000000000000000000000000000000000000000000000000000", "0101", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 151 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 152 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 153 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 154 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 155 {"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 156 {"4000000000000000000000000000000000000000000000000000000000000000", "fe", "0000000000000000000000000000000000000000000000000000000000000001"}, 157 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "f8", "000000000000000000000000000000000000000000000000000000000000007f"}, 158 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "fe", "0000000000000000000000000000000000000000000000000000000000000001"}, 159 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000000"}, 160 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 161 } 162 163 testTwoOperandOp(t, tests, opSAR) 164 } 165 166 func TestSGT(t *testing.T) { 167 tests := []twoOperandTest{ 168 169 {"0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"}, 170 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"}, 171 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"}, 172 {"0000000000000000000000000000000000000000000000000000000000000001", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001"}, 173 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"}, 174 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001"}, 175 {"0000000000000000000000000000000000000000000000000000000000000001", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"}, 176 {"8000000000000000000000000000000000000000000000000000000000000001", "8000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"}, 177 {"8000000000000000000000000000000000000000000000000000000000000001", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001"}, 178 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "8000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"}, 179 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd", "0000000000000000000000000000000000000000000000000000000000000001"}, 180 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", "0000000000000000000000000000000000000000000000000000000000000000"}, 181 } 182 testTwoOperandOp(t, tests, opSgt) 183 } 184 185 func TestSLT(t *testing.T) { 186 tests := []twoOperandTest{ 187 {"0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"}, 188 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"}, 189 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"}, 190 {"0000000000000000000000000000000000000000000000000000000000000001", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"}, 191 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001"}, 192 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"}, 193 {"0000000000000000000000000000000000000000000000000000000000000001", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001"}, 194 {"8000000000000000000000000000000000000000000000000000000000000001", "8000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"}, 195 {"8000000000000000000000000000000000000000000000000000000000000001", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"}, 196 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "8000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001"}, 197 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd", "0000000000000000000000000000000000000000000000000000000000000000"}, 198 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", "0000000000000000000000000000000000000000000000000000000000000001"}, 199 } 200 testTwoOperandOp(t, tests, opSlt) 201 } 202 203 func opBenchmark(bench *testing.B, op func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error), args ...string) { 204 var ( 205 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 206 stack = newstack() 207 ) 208 // convert args 209 byteArgs := make([][]byte, len(args)) 210 for i, arg := range args { 211 byteArgs[i] = common.Hex2Bytes(arg) 212 } 213 pc := uint64(0) 214 bench.ResetTimer() 215 for i := 0; i < bench.N; i++ { 216 for _, arg := range byteArgs { 217 a := new(big.Int).SetBytes(arg) 218 stack.push(a) 219 } 220 op(&pc, env, nil, nil, stack) 221 stack.pop() 222 } 223 } 224 225 func BenchmarkOpAdd64(b *testing.B) { 226 x := "ffffffff" 227 y := "fd37f3e2bba2c4f" 228 229 opBenchmark(b, opAdd, x, y) 230 } 231 232 func BenchmarkOpAdd128(b *testing.B) { 233 x := "ffffffffffffffff" 234 y := "f5470b43c6549b016288e9a65629687" 235 236 opBenchmark(b, opAdd, x, y) 237 } 238 239 func BenchmarkOpAdd256(b *testing.B) { 240 x := "0802431afcbce1fc194c9eaa417b2fb67dc75a95db0bc7ec6b1c8af11df6a1da9" 241 y := "a1f5aac137876480252e5dcac62c354ec0d42b76b0642b6181ed099849ea1d57" 242 243 opBenchmark(b, opAdd, x, y) 244 } 245 246 func BenchmarkOpSub64(b *testing.B) { 247 x := "51022b6317003a9d" 248 y := "a20456c62e00753a" 249 250 opBenchmark(b, opSub, x, y) 251 } 252 253 func BenchmarkOpSub128(b *testing.B) { 254 x := "4dde30faaacdc14d00327aac314e915d" 255 y := "9bbc61f5559b829a0064f558629d22ba" 256 257 opBenchmark(b, opSub, x, y) 258 } 259 260 func BenchmarkOpSub256(b *testing.B) { 261 x := "4bfcd8bb2ac462735b48a17580690283980aa2d679f091c64364594df113ea37" 262 y := "97f9b1765588c4e6b69142eb00d20507301545acf3e1238c86c8b29be227d46e" 263 264 opBenchmark(b, opSub, x, y) 265 } 266 267 func BenchmarkOpMul(b *testing.B) { 268 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 269 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 270 271 opBenchmark(b, opMul, x, y) 272 } 273 274 func BenchmarkOpDiv256(b *testing.B) { 275 x := "ff3f9014f20db29ae04af2c2d265de17" 276 y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611" 277 opBenchmark(b, opDiv, x, y) 278 } 279 280 func BenchmarkOpDiv128(b *testing.B) { 281 x := "fdedc7f10142ff97" 282 y := "fbdfda0e2ce356173d1993d5f70a2b11" 283 opBenchmark(b, opDiv, x, y) 284 } 285 286 func BenchmarkOpDiv64(b *testing.B) { 287 x := "fcb34eb3" 288 y := "f97180878e839129" 289 opBenchmark(b, opDiv, x, y) 290 } 291 292 func BenchmarkOpSdiv(b *testing.B) { 293 x := "ff3f9014f20db29ae04af2c2d265de17" 294 y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611" 295 296 opBenchmark(b, opSdiv, x, y) 297 } 298 299 func BenchmarkOpMod(b *testing.B) { 300 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 301 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 302 303 opBenchmark(b, opMod, x, y) 304 } 305 306 func BenchmarkOpSmod(b *testing.B) { 307 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 308 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 309 310 opBenchmark(b, opSmod, x, y) 311 } 312 313 func BenchmarkOpExp(b *testing.B) { 314 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 315 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 316 317 opBenchmark(b, opExp, x, y) 318 } 319 320 func BenchmarkOpSignExtend(b *testing.B) { 321 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 322 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 323 324 opBenchmark(b, opSignExtend, x, y) 325 } 326 327 func BenchmarkOpLt(b *testing.B) { 328 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 329 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 330 331 opBenchmark(b, opLt, x, y) 332 } 333 334 func BenchmarkOpGt(b *testing.B) { 335 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 336 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 337 338 opBenchmark(b, opGt, x, y) 339 } 340 341 func BenchmarkOpSlt(b *testing.B) { 342 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 343 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 344 345 opBenchmark(b, opSlt, x, y) 346 } 347 348 func BenchmarkOpSgt(b *testing.B) { 349 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 350 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 351 352 opBenchmark(b, opSgt, x, y) 353 } 354 355 func BenchmarkOpEq(b *testing.B) { 356 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 357 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 358 359 opBenchmark(b, opEq, x, y) 360 } 361 func BenchmarkOpEq2(b *testing.B) { 362 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 363 y := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201fffffffe" 364 opBenchmark(b, opEq, x, y) 365 } 366 func BenchmarkOpAnd(b *testing.B) { 367 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 368 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 369 370 opBenchmark(b, opAnd, x, y) 371 } 372 373 func BenchmarkOpOr(b *testing.B) { 374 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 375 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 376 377 opBenchmark(b, opOr, x, y) 378 } 379 380 func BenchmarkOpXor(b *testing.B) { 381 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 382 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 383 384 opBenchmark(b, opXor, x, y) 385 } 386 387 func BenchmarkOpByte(b *testing.B) { 388 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 389 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 390 391 opBenchmark(b, opByte, x, y) 392 } 393 394 func BenchmarkOpAddmod(b *testing.B) { 395 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 396 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 397 z := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 398 399 opBenchmark(b, opAddmod, x, y, z) 400 } 401 402 func BenchmarkOpMulmod(b *testing.B) { 403 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 404 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 405 z := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 406 407 opBenchmark(b, opMulmod, x, y, z) 408 } 409 410 func BenchmarkOpSHL(b *testing.B) { 411 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 412 y := "ff" 413 414 opBenchmark(b, opSHL, x, y) 415 } 416 func BenchmarkOpSHR(b *testing.B) { 417 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 418 y := "ff" 419 420 opBenchmark(b, opSHR, x, y) 421 } 422 func BenchmarkOpSAR(b *testing.B) { 423 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 424 y := "ff" 425 426 opBenchmark(b, opSAR, x, y) 427 } 428 func BenchmarkOpIsZero(b *testing.B) { 429 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 430 opBenchmark(b, opIszero, x) 431 } 432 433 func TestOpMstore(t *testing.T) { 434 var ( 435 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 436 stack = newstack() 437 mem = NewMemory() 438 ) 439 env.interpreter.intPool = poolOfIntPools.get() 440 mem.Resize(64) 441 pc := uint64(0) 442 v := "abcdef00000000000000abba000000000deaf000000c0de00100000000133700" 443 stack.pushN(new(big.Int).SetBytes(common.Hex2Bytes(v)), big.NewInt(0)) 444 opMstore(&pc, env, nil, mem, stack) 445 if got := common.Bytes2Hex(mem.Get(0, 32)); got != v { 446 t.Fatalf("Mstore fail, got %v, expected %v", got, v) 447 } 448 stack.pushN(big.NewInt(0x1), big.NewInt(0)) 449 opMstore(&pc, env, nil, mem, stack) 450 if common.Bytes2Hex(mem.Get(0, 32)) != "0000000000000000000000000000000000000000000000000000000000000001" { 451 t.Fatalf("Mstore failed to overwrite previous value") 452 } 453 poolOfIntPools.put(env.interpreter.intPool) 454 } 455 456 func BenchmarkOpMstore(bench *testing.B) { 457 var ( 458 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 459 stack = newstack() 460 mem = NewMemory() 461 ) 462 mem.Resize(64) 463 pc := uint64(0) 464 memStart := big.NewInt(0) 465 value := big.NewInt(0x1337) 466 467 bench.ResetTimer() 468 for i := 0; i < bench.N; i++ { 469 stack.pushN(value, memStart) 470 opMstore(&pc, env, nil, mem, stack) 471 } 472 }