instructions_test.go 24 KB

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