instructions_test.go 24 KB

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