instructions_test.go 24 KB

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