instructions_test.go 26 KB

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