contracts_test.go 16 KB

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