instructions_test.go 23 KB

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