contracts_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. "time"
  24. "github.com/ethereum/go-ethereum/common"
  25. )
  26. // precompiledTest defines the input/output pairs for precompiled contract tests.
  27. type precompiledTest struct {
  28. Input, Expected string
  29. Name string
  30. NoBenchmark bool // Benchmark primarily the worst-cases
  31. }
  32. // precompiledFailureTest defines the input/error pairs for precompiled
  33. // contract failure tests.
  34. type precompiledFailureTest struct {
  35. Input string
  36. ExpectedError string
  37. Name string
  38. }
  39. var allPrecompiles = PrecompiledContractsYoloV1
  40. // EIP-152 test vectors
  41. var blake2FMalformedInputTests = []precompiledFailureTest{
  42. {
  43. Input: "",
  44. ExpectedError: errBlake2FInvalidInputLength.Error(),
  45. Name: "vector 0: empty input",
  46. },
  47. {
  48. Input: "00000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001",
  49. ExpectedError: errBlake2FInvalidInputLength.Error(),
  50. Name: "vector 1: less than 213 bytes input",
  51. },
  52. {
  53. Input: "000000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001",
  54. ExpectedError: errBlake2FInvalidInputLength.Error(),
  55. Name: "vector 2: more than 213 bytes input",
  56. },
  57. {
  58. Input: "0000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000002",
  59. ExpectedError: errBlake2FInvalidFinalFlag.Error(),
  60. Name: "vector 3: malformed final block indicator flag",
  61. },
  62. }
  63. func testPrecompiled(addr string, test precompiledTest, t *testing.T) {
  64. p := allPrecompiles[common.HexToAddress(addr)]
  65. in := common.Hex2Bytes(test.Input)
  66. gas := p.RequiredGas(in)
  67. t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) {
  68. if res, _, err := RunPrecompiledContract(p, in, gas); err != nil {
  69. t.Error(err)
  70. } else if common.Bytes2Hex(res) != test.Expected {
  71. t.Errorf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res))
  72. }
  73. // Verify that the precompile did not touch the input buffer
  74. exp := common.Hex2Bytes(test.Input)
  75. if !bytes.Equal(in, exp) {
  76. t.Errorf("Precompiled %v modified input data", addr)
  77. }
  78. })
  79. }
  80. func testPrecompiledOOG(addr string, test precompiledTest, t *testing.T) {
  81. p := allPrecompiles[common.HexToAddress(addr)]
  82. in := common.Hex2Bytes(test.Input)
  83. gas := p.RequiredGas(in) - 1
  84. t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) {
  85. _, _, err := RunPrecompiledContract(p, in, gas)
  86. if err.Error() != "out of gas" {
  87. t.Errorf("Expected error [out of gas], got [%v]", err)
  88. }
  89. // Verify that the precompile did not touch the input buffer
  90. exp := common.Hex2Bytes(test.Input)
  91. if !bytes.Equal(in, exp) {
  92. t.Errorf("Precompiled %v modified input data", addr)
  93. }
  94. })
  95. }
  96. func testPrecompiledFailure(addr string, test precompiledFailureTest, t *testing.T) {
  97. p := allPrecompiles[common.HexToAddress(addr)]
  98. in := common.Hex2Bytes(test.Input)
  99. gas := p.RequiredGas(in)
  100. t.Run(test.Name, func(t *testing.T) {
  101. _, _, err := RunPrecompiledContract(p, in, gas)
  102. if err.Error() != test.ExpectedError {
  103. t.Errorf("Expected error [%v], got [%v]", test.ExpectedError, err)
  104. }
  105. // Verify that the precompile did not touch the input buffer
  106. exp := common.Hex2Bytes(test.Input)
  107. if !bytes.Equal(in, exp) {
  108. t.Errorf("Precompiled %v modified input data", addr)
  109. }
  110. })
  111. }
  112. func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) {
  113. if test.NoBenchmark {
  114. return
  115. }
  116. p := allPrecompiles[common.HexToAddress(addr)]
  117. in := common.Hex2Bytes(test.Input)
  118. reqGas := p.RequiredGas(in)
  119. var (
  120. res []byte
  121. err error
  122. data = make([]byte, len(in))
  123. )
  124. bench.Run(fmt.Sprintf("%s-Gas=%d", test.Name, reqGas), func(bench *testing.B) {
  125. bench.ReportAllocs()
  126. start := time.Now().Nanosecond()
  127. bench.ResetTimer()
  128. for i := 0; i < bench.N; i++ {
  129. copy(data, in)
  130. res, _, err = RunPrecompiledContract(p, data, reqGas)
  131. }
  132. bench.StopTimer()
  133. elapsed := float64(time.Now().Nanosecond() - start)
  134. if elapsed < 1 {
  135. elapsed = 1
  136. }
  137. gasUsed := reqGas * uint64(bench.N)
  138. bench.ReportMetric(float64(reqGas), "gas/op")
  139. bench.ReportMetric(float64(gasUsed*1000)/elapsed, "mgas/s")
  140. //Check if it is correct
  141. if err != nil {
  142. bench.Error(err)
  143. return
  144. }
  145. if common.Bytes2Hex(res) != test.Expected {
  146. bench.Error(fmt.Sprintf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res)))
  147. return
  148. }
  149. })
  150. }
  151. // Benchmarks the sample inputs from the ECRECOVER precompile.
  152. func BenchmarkPrecompiledEcrecover(bench *testing.B) {
  153. t := precompiledTest{
  154. Input: "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02",
  155. Expected: "000000000000000000000000ceaccac640adf55b2028469bd36ba501f28b699d",
  156. Name: "",
  157. }
  158. benchmarkPrecompiled("01", t, bench)
  159. }
  160. // Benchmarks the sample inputs from the SHA256 precompile.
  161. func BenchmarkPrecompiledSha256(bench *testing.B) {
  162. t := precompiledTest{
  163. Input: "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02",
  164. Expected: "811c7003375852fabd0d362e40e68607a12bdabae61a7d068fe5fdd1dbbf2a5d",
  165. Name: "128",
  166. }
  167. benchmarkPrecompiled("02", t, bench)
  168. }
  169. // Benchmarks the sample inputs from the RIPEMD precompile.
  170. func BenchmarkPrecompiledRipeMD(bench *testing.B) {
  171. t := precompiledTest{
  172. Input: "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02",
  173. Expected: "0000000000000000000000009215b8d9882ff46f0dfde6684d78e831467f65e6",
  174. Name: "128",
  175. }
  176. benchmarkPrecompiled("03", t, bench)
  177. }
  178. // Benchmarks the sample inputs from the identiy precompile.
  179. func BenchmarkPrecompiledIdentity(bench *testing.B) {
  180. t := precompiledTest{
  181. Input: "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02",
  182. Expected: "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02",
  183. Name: "128",
  184. }
  185. benchmarkPrecompiled("04", t, bench)
  186. }
  187. // Tests the sample inputs from the ModExp EIP 198.
  188. func TestPrecompiledModExp(t *testing.T) { testJson("modexp", "05", t) }
  189. func BenchmarkPrecompiledModExp(b *testing.B) { benchJson("modexp", "05", b) }
  190. // Tests the sample inputs from the elliptic curve addition EIP 213.
  191. func TestPrecompiledBn256Add(t *testing.T) { testJson("bn256Add", "06", t) }
  192. func BenchmarkPrecompiledBn256Add(b *testing.B) { benchJson("bn256Add", "06", b) }
  193. // Tests OOG
  194. func TestPrecompiledModExpOOG(t *testing.T) {
  195. modexpTests, err := loadJson("modexp")
  196. if err != nil {
  197. t.Fatal(err)
  198. }
  199. for _, test := range modexpTests {
  200. testPrecompiledOOG("05", test, t)
  201. }
  202. }
  203. // Tests the sample inputs from the elliptic curve scalar multiplication EIP 213.
  204. func TestPrecompiledBn256ScalarMul(t *testing.T) { testJson("bn256ScalarMul", "07", t) }
  205. func BenchmarkPrecompiledBn256ScalarMul(b *testing.B) { benchJson("bn256ScalarMul", "07", b) }
  206. // Tests the sample inputs from the elliptic curve pairing check EIP 197.
  207. func TestPrecompiledBn256Pairing(t *testing.T) { testJson("bn256Pairing", "08", t) }
  208. func BenchmarkPrecompiledBn256Pairing(b *testing.B) { benchJson("bn256Pairing", "08", b) }
  209. func TestPrecompiledBlake2F(t *testing.T) { testJson("blake2F", "09", t) }
  210. func BenchmarkPrecompiledBlake2F(b *testing.B) { benchJson("blake2F", "09", b) }
  211. func TestPrecompileBlake2FMalformedInput(t *testing.T) {
  212. for _, test := range blake2FMalformedInputTests {
  213. testPrecompiledFailure("09", test, t)
  214. }
  215. }
  216. func TestPrecompiledEcrecover(t *testing.T) { testJson("ecRecover", "01", t) }
  217. func testJson(name, addr string, t *testing.T) {
  218. tests, err := loadJson(name)
  219. if err != nil {
  220. t.Fatal(err)
  221. }
  222. for _, test := range tests {
  223. testPrecompiled(addr, test, t)
  224. }
  225. }
  226. func testJsonFail(name, addr string, t *testing.T) {
  227. tests, err := loadJsonFail(name)
  228. if err != nil {
  229. t.Fatal(err)
  230. }
  231. for _, test := range tests {
  232. testPrecompiledFailure(addr, test, t)
  233. }
  234. }
  235. func benchJson(name, addr string, b *testing.B) {
  236. tests, err := loadJson(name)
  237. if err != nil {
  238. b.Fatal(err)
  239. }
  240. for _, test := range tests {
  241. benchmarkPrecompiled(addr, test, b)
  242. }
  243. }
  244. func TestPrecompiledBLS12381G1Add(t *testing.T) { testJson("blsG1Add", "0a", t) }
  245. func TestPrecompiledBLS12381G1Mul(t *testing.T) { testJson("blsG1Mul", "0b", t) }
  246. func TestPrecompiledBLS12381G1MultiExp(t *testing.T) { testJson("blsG1MultiExp", "0c", t) }
  247. func TestPrecompiledBLS12381G2Add(t *testing.T) { testJson("blsG2Add", "0d", t) }
  248. func TestPrecompiledBLS12381G2Mul(t *testing.T) { testJson("blsG2Mul", "0e", t) }
  249. func TestPrecompiledBLS12381G2MultiExp(t *testing.T) { testJson("blsG2MultiExp", "0f", t) }
  250. func TestPrecompiledBLS12381Pairing(t *testing.T) { testJson("blsPairing", "10", t) }
  251. func TestPrecompiledBLS12381MapG1(t *testing.T) { testJson("blsMapG1", "11", t) }
  252. func TestPrecompiledBLS12381MapG2(t *testing.T) { testJson("blsMapG2", "12", t) }
  253. func BenchmarkPrecompiledBLS12381G1Add(b *testing.B) { benchJson("blsG1Add", "0a", b) }
  254. func BenchmarkPrecompiledBLS12381G1Mul(b *testing.B) { benchJson("blsG1Mul", "0b", b) }
  255. func BenchmarkPrecompiledBLS12381G1MultiExp(b *testing.B) { benchJson("blsG1MultiExp", "0c", b) }
  256. func BenchmarkPrecompiledBLS12381G2Add(b *testing.B) { benchJson("blsG2Add", "0d", b) }
  257. func BenchmarkPrecompiledBLS12381G2Mul(b *testing.B) { benchJson("blsG2Mul", "0e", b) }
  258. func BenchmarkPrecompiledBLS12381G2MultiExp(b *testing.B) { benchJson("blsG2MultiExp", "0f", b) }
  259. func BenchmarkPrecompiledBLS12381Pairing(b *testing.B) { benchJson("blsPairing", "10", b) }
  260. func BenchmarkPrecompiledBLS12381MapG1(b *testing.B) { benchJson("blsMapG1", "11", b) }
  261. func BenchmarkPrecompiledBLS12381MapG2(b *testing.B) { benchJson("blsMapG2", "12", b) }
  262. // Failure tests
  263. func TestPrecompiledBLS12381G1AddFail(t *testing.T) { testJsonFail("blsG1Add", "0a", t) }
  264. func TestPrecompiledBLS12381G1MulFail(t *testing.T) { testJsonFail("blsG1Mul", "0b", t) }
  265. func TestPrecompiledBLS12381G1MultiExpFail(t *testing.T) { testJsonFail("blsG1MultiExp", "0c", t) }
  266. func TestPrecompiledBLS12381G2AddFail(t *testing.T) { testJsonFail("blsG2Add", "0d", t) }
  267. func TestPrecompiledBLS12381G2MulFail(t *testing.T) { testJsonFail("blsG2Mul", "0e", t) }
  268. func TestPrecompiledBLS12381G2MultiExpFail(t *testing.T) { testJsonFail("blsG2MultiExp", "0f", t) }
  269. func TestPrecompiledBLS12381PairingFail(t *testing.T) { testJsonFail("blsPairing", "10", t) }
  270. func TestPrecompiledBLS12381MapG1Fail(t *testing.T) { testJsonFail("blsMapG1", "11", t) }
  271. func TestPrecompiledBLS12381MapG2Fail(t *testing.T) { testJsonFail("blsMapG2", "12", t) }
  272. func loadJson(name string) ([]precompiledTest, error) {
  273. data, err := ioutil.ReadFile(fmt.Sprintf("testdata/precompiles/%v.json", name))
  274. if err != nil {
  275. return nil, err
  276. }
  277. var testcases []precompiledTest
  278. err = json.Unmarshal(data, &testcases)
  279. return testcases, err
  280. }
  281. func loadJsonFail(name string) ([]precompiledFailureTest, error) {
  282. data, err := ioutil.ReadFile(fmt.Sprintf("testdata/precompiles/fail-%v.json", name))
  283. if err != nil {
  284. return nil, err
  285. }
  286. var testcases []precompiledFailureTest
  287. err = json.Unmarshal(data, &testcases)
  288. return testcases, err
  289. }
  290. // BenchmarkPrecompiledBLS12381G1MultiExpWorstCase benchmarks the worst case we could find that still fits a gaslimit of 10MGas.
  291. func BenchmarkPrecompiledBLS12381G1MultiExpWorstCase(b *testing.B) {
  292. task := "0000000000000000000000000000000008d8c4a16fb9d8800cce987c0eadbb6b3b005c213d44ecb5adeed713bae79d606041406df26169c35df63cf972c94be1" +
  293. "0000000000000000000000000000000011bc8afe71676e6730702a46ef817060249cd06cd82e6981085012ff6d013aa4470ba3a2c71e13ef653e1e223d1ccfe9" +
  294. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  295. input := task
  296. for i := 0; i < 4787; i++ {
  297. input = input + task
  298. }
  299. testcase := precompiledTest{
  300. Input: input,
  301. Expected: "0000000000000000000000000000000005a6310ea6f2a598023ae48819afc292b4dfcb40aabad24a0c2cb6c19769465691859eeb2a764342a810c5038d700f18000000000000000000000000000000001268ac944437d15923dc0aec00daa9250252e43e4b35ec7a19d01f0d6cd27f6e139d80dae16ba1c79cc7f57055a93ff5",
  302. Name: "WorstCaseG1",
  303. NoBenchmark: false,
  304. }
  305. benchmarkPrecompiled("0c", testcase, b)
  306. }
  307. // BenchmarkPrecompiledBLS12381G2MultiExpWorstCase benchmarks the worst case we could find that still fits a gaslimit of 10MGas.
  308. func BenchmarkPrecompiledBLS12381G2MultiExpWorstCase(b *testing.B) {
  309. task := "000000000000000000000000000000000d4f09acd5f362e0a516d4c13c5e2f504d9bd49fdfb6d8b7a7ab35a02c391c8112b03270d5d9eefe9b659dd27601d18f" +
  310. "000000000000000000000000000000000fd489cb75945f3b5ebb1c0e326d59602934c8f78fe9294a8877e7aeb95de5addde0cb7ab53674df8b2cfbb036b30b99" +
  311. "00000000000000000000000000000000055dbc4eca768714e098bbe9c71cf54b40f51c26e95808ee79225a87fb6fa1415178db47f02d856fea56a752d185f86b" +
  312. "000000000000000000000000000000001239b7640f416eb6e921fe47f7501d504fadc190d9cf4e89ae2b717276739a2f4ee9f637c35e23c480df029fd8d247c7" +
  313. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  314. input := task
  315. for i := 0; i < 1040; i++ {
  316. input = input + task
  317. }
  318. testcase := precompiledTest{
  319. Input: input,
  320. Expected: "0000000000000000000000000000000018f5ea0c8b086095cfe23f6bb1d90d45de929292006dba8cdedd6d3203af3c6bbfd592e93ecb2b2c81004961fdcbb46c00000000000000000000000000000000076873199175664f1b6493a43c02234f49dc66f077d3007823e0343ad92e30bd7dc209013435ca9f197aca44d88e9dac000000000000000000000000000000000e6f07f4b23b511eac1e2682a0fc224c15d80e122a3e222d00a41fab15eba645a700b9ae84f331ae4ed873678e2e6c9b000000000000000000000000000000000bcb4849e460612aaed79617255fd30c03f51cf03d2ed4163ca810c13e1954b1e8663157b957a601829bb272a4e6c7b8",
  321. Name: "WorstCaseG2",
  322. NoBenchmark: false,
  323. }
  324. benchmarkPrecompiled("0f", testcase, b)
  325. }