instructions_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. "math/big"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/params"
  22. )
  23. func TestByteOp(t *testing.T) {
  24. var (
  25. env = NewEVM(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
  26. stack = newstack()
  27. )
  28. tests := []struct {
  29. v string
  30. th uint64
  31. expected *big.Int
  32. }{
  33. {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 0, big.NewInt(0xAB)},
  34. {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 1, big.NewInt(0xCD)},
  35. {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 0, big.NewInt(0x00)},
  36. {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 1, big.NewInt(0xCD)},
  37. {"0000000000000000000000000000000000000000000000000000000000102030", 31, big.NewInt(0x30)},
  38. {"0000000000000000000000000000000000000000000000000000000000102030", 30, big.NewInt(0x20)},
  39. {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32, big.NewInt(0x0)},
  40. {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0xFFFFFFFFFFFFFFFF, big.NewInt(0x0)},
  41. }
  42. pc := uint64(0)
  43. for _, test := range tests {
  44. val := new(big.Int).SetBytes(common.Hex2Bytes(test.v))
  45. th := new(big.Int).SetUint64(test.th)
  46. stack.push(val)
  47. stack.push(th)
  48. opByte(&pc, env, nil, nil, stack)
  49. actual := stack.pop()
  50. if actual.Cmp(test.expected) != 0 {
  51. t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.v, test.th, test.expected, actual)
  52. }
  53. }
  54. }
  55. func opBenchmark(bench *testing.B, op func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error), args ...string) {
  56. var (
  57. env = NewEVM(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
  58. stack = newstack()
  59. )
  60. // convert args
  61. byteArgs := make([][]byte, len(args))
  62. for i, arg := range args {
  63. byteArgs[i] = common.Hex2Bytes(arg)
  64. }
  65. pc := uint64(0)
  66. bench.ResetTimer()
  67. for i := 0; i < bench.N; i++ {
  68. for _, arg := range byteArgs {
  69. a := new(big.Int).SetBytes(arg)
  70. stack.push(a)
  71. }
  72. op(&pc, env, nil, nil, stack)
  73. stack.pop()
  74. }
  75. }
  76. func BenchmarkOpAdd64(b *testing.B) {
  77. x := "ffffffff"
  78. y := "fd37f3e2bba2c4f"
  79. opBenchmark(b, opAdd, x, y)
  80. }
  81. func BenchmarkOpAdd128(b *testing.B) {
  82. x := "ffffffffffffffff"
  83. y := "f5470b43c6549b016288e9a65629687"
  84. opBenchmark(b, opAdd, x, y)
  85. }
  86. func BenchmarkOpAdd256(b *testing.B) {
  87. x := "0802431afcbce1fc194c9eaa417b2fb67dc75a95db0bc7ec6b1c8af11df6a1da9"
  88. y := "a1f5aac137876480252e5dcac62c354ec0d42b76b0642b6181ed099849ea1d57"
  89. opBenchmark(b, opAdd, x, y)
  90. }
  91. func BenchmarkOpSub64(b *testing.B) {
  92. x := "51022b6317003a9d"
  93. y := "a20456c62e00753a"
  94. opBenchmark(b, opSub, x, y)
  95. }
  96. func BenchmarkOpSub128(b *testing.B) {
  97. x := "4dde30faaacdc14d00327aac314e915d"
  98. y := "9bbc61f5559b829a0064f558629d22ba"
  99. opBenchmark(b, opSub, x, y)
  100. }
  101. func BenchmarkOpSub256(b *testing.B) {
  102. x := "4bfcd8bb2ac462735b48a17580690283980aa2d679f091c64364594df113ea37"
  103. y := "97f9b1765588c4e6b69142eb00d20507301545acf3e1238c86c8b29be227d46e"
  104. opBenchmark(b, opSub, x, y)
  105. }
  106. func BenchmarkOpMul(b *testing.B) {
  107. x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  108. y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  109. opBenchmark(b, opMul, x, y)
  110. }
  111. func BenchmarkOpDiv256(b *testing.B) {
  112. x := "ff3f9014f20db29ae04af2c2d265de17"
  113. y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611"
  114. opBenchmark(b, opDiv, x, y)
  115. }
  116. func BenchmarkOpDiv128(b *testing.B) {
  117. x := "fdedc7f10142ff97"
  118. y := "fbdfda0e2ce356173d1993d5f70a2b11"
  119. opBenchmark(b, opDiv, x, y)
  120. }
  121. func BenchmarkOpDiv64(b *testing.B) {
  122. x := "fcb34eb3"
  123. y := "f97180878e839129"
  124. opBenchmark(b, opDiv, x, y)
  125. }
  126. func BenchmarkOpSdiv(b *testing.B) {
  127. x := "ff3f9014f20db29ae04af2c2d265de17"
  128. y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611"
  129. opBenchmark(b, opSdiv, x, y)
  130. }
  131. func BenchmarkOpMod(b *testing.B) {
  132. x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  133. y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  134. opBenchmark(b, opMod, x, y)
  135. }
  136. func BenchmarkOpSmod(b *testing.B) {
  137. x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  138. y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  139. opBenchmark(b, opSmod, x, y)
  140. }
  141. func BenchmarkOpExp(b *testing.B) {
  142. x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  143. y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  144. opBenchmark(b, opExp, x, y)
  145. }
  146. func BenchmarkOpSignExtend(b *testing.B) {
  147. x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  148. y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  149. opBenchmark(b, opSignExtend, x, y)
  150. }
  151. func BenchmarkOpLt(b *testing.B) {
  152. x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  153. y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  154. opBenchmark(b, opLt, x, y)
  155. }
  156. func BenchmarkOpGt(b *testing.B) {
  157. x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  158. y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  159. opBenchmark(b, opGt, x, y)
  160. }
  161. func BenchmarkOpSlt(b *testing.B) {
  162. x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  163. y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  164. opBenchmark(b, opSlt, x, y)
  165. }
  166. func BenchmarkOpSgt(b *testing.B) {
  167. x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  168. y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  169. opBenchmark(b, opSgt, x, y)
  170. }
  171. func BenchmarkOpEq(b *testing.B) {
  172. x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  173. y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  174. opBenchmark(b, opEq, x, y)
  175. }
  176. func BenchmarkOpAnd(b *testing.B) {
  177. x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  178. y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  179. opBenchmark(b, opAnd, x, y)
  180. }
  181. func BenchmarkOpOr(b *testing.B) {
  182. x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  183. y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  184. opBenchmark(b, opOr, x, y)
  185. }
  186. func BenchmarkOpXor(b *testing.B) {
  187. x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  188. y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  189. opBenchmark(b, opXor, x, y)
  190. }
  191. func BenchmarkOpByte(b *testing.B) {
  192. x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  193. y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  194. opBenchmark(b, opByte, x, y)
  195. }
  196. func BenchmarkOpAddmod(b *testing.B) {
  197. x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  198. y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  199. z := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  200. opBenchmark(b, opAddmod, x, y, z)
  201. }
  202. func BenchmarkOpMulmod(b *testing.B) {
  203. x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  204. y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  205. z := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
  206. opBenchmark(b, opMulmod, x, y, z)
  207. }