instructions_test.go 24 KB

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