github.com/dim4egster/coreth@v0.10.2/core/vm/contracts_test.go (about) 1 // (c) 2019-2021, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2017 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package vm 28 29 import ( 30 "bytes" 31 "encoding/json" 32 "fmt" 33 "os" 34 "testing" 35 "time" 36 37 "github.com/ethereum/go-ethereum/common" 38 ) 39 40 // precompiledTest defines the input/output pairs for precompiled contract tests. 41 type precompiledTest struct { 42 Input, Expected string 43 Gas uint64 44 Name string 45 NoBenchmark bool // Benchmark primarily the worst-cases 46 } 47 48 // precompiledFailureTest defines the input/error pairs for precompiled 49 // contract failure tests. 50 type precompiledFailureTest struct { 51 Input string 52 ExpectedError string 53 Name string 54 } 55 56 // allPrecompiles does not map to the actual set of precompiles, as it also contains 57 // repriced versions of precompiles at certain slots 58 var allPrecompiles = map[common.Address]PrecompiledContract{ 59 common.BytesToAddress([]byte{1}): &ecrecover{}, 60 common.BytesToAddress([]byte{2}): &sha256hash{}, 61 common.BytesToAddress([]byte{3}): &ripemd160hash{}, 62 common.BytesToAddress([]byte{4}): &dataCopy{}, 63 common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false}, 64 common.BytesToAddress([]byte{0xf5}): &bigModExp{eip2565: true}, 65 common.BytesToAddress([]byte{6}): &bn256AddIstanbul{}, 66 common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{}, 67 common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{}, 68 common.BytesToAddress([]byte{9}): &blake2F{}, 69 common.BytesToAddress([]byte{10}): &bls12381G1Add{}, 70 common.BytesToAddress([]byte{11}): &bls12381G1Mul{}, 71 common.BytesToAddress([]byte{12}): &bls12381G1MultiExp{}, 72 common.BytesToAddress([]byte{13}): &bls12381G2Add{}, 73 common.BytesToAddress([]byte{14}): &bls12381G2Mul{}, 74 common.BytesToAddress([]byte{15}): &bls12381G2MultiExp{}, 75 common.BytesToAddress([]byte{16}): &bls12381Pairing{}, 76 common.BytesToAddress([]byte{17}): &bls12381MapG1{}, 77 common.BytesToAddress([]byte{18}): &bls12381MapG2{}, 78 } 79 80 // EIP-152 test vectors 81 var blake2FMalformedInputTests = []precompiledFailureTest{ 82 { 83 Input: "", 84 ExpectedError: errBlake2FInvalidInputLength.Error(), 85 Name: "vector 0: empty input", 86 }, 87 { 88 Input: "00000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001", 89 ExpectedError: errBlake2FInvalidInputLength.Error(), 90 Name: "vector 1: less than 213 bytes input", 91 }, 92 { 93 Input: "000000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001", 94 ExpectedError: errBlake2FInvalidInputLength.Error(), 95 Name: "vector 2: more than 213 bytes input", 96 }, 97 { 98 Input: "0000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000002", 99 ExpectedError: errBlake2FInvalidFinalFlag.Error(), 100 Name: "vector 3: malformed final block indicator flag", 101 }, 102 } 103 104 func testPrecompiled(addr string, test precompiledTest, t *testing.T) { 105 p := allPrecompiles[common.HexToAddress(addr)] 106 in := common.Hex2Bytes(test.Input) 107 gas := p.RequiredGas(in) 108 t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) { 109 if res, _, err := RunPrecompiledContract(p, in, gas); err != nil { 110 t.Error(err) 111 } else if common.Bytes2Hex(res) != test.Expected { 112 t.Errorf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res)) 113 } 114 if expGas := test.Gas; expGas != gas { 115 t.Errorf("%v: gas wrong, expected %d, got %d", test.Name, expGas, gas) 116 } 117 // Verify that the precompile did not touch the input buffer 118 exp := common.Hex2Bytes(test.Input) 119 if !bytes.Equal(in, exp) { 120 t.Errorf("Precompiled %v modified input data", addr) 121 } 122 }) 123 } 124 125 func testPrecompiledOOG(addr string, test precompiledTest, t *testing.T) { 126 p := allPrecompiles[common.HexToAddress(addr)] 127 in := common.Hex2Bytes(test.Input) 128 gas := p.RequiredGas(in) - 1 129 130 t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) { 131 _, _, err := RunPrecompiledContract(p, in, gas) 132 if err.Error() != "out of gas" { 133 t.Errorf("Expected error [out of gas], got [%v]", err) 134 } 135 // Verify that the precompile did not touch the input buffer 136 exp := common.Hex2Bytes(test.Input) 137 if !bytes.Equal(in, exp) { 138 t.Errorf("Precompiled %v modified input data", addr) 139 } 140 }) 141 } 142 143 func testPrecompiledFailure(addr string, test precompiledFailureTest, t *testing.T) { 144 p := allPrecompiles[common.HexToAddress(addr)] 145 in := common.Hex2Bytes(test.Input) 146 gas := p.RequiredGas(in) 147 t.Run(test.Name, func(t *testing.T) { 148 _, _, err := RunPrecompiledContract(p, in, gas) 149 if err.Error() != test.ExpectedError { 150 t.Errorf("Expected error [%v], got [%v]", test.ExpectedError, err) 151 } 152 // Verify that the precompile did not touch the input buffer 153 exp := common.Hex2Bytes(test.Input) 154 if !bytes.Equal(in, exp) { 155 t.Errorf("Precompiled %v modified input data", addr) 156 } 157 }) 158 } 159 160 func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) { 161 if test.NoBenchmark { 162 return 163 } 164 p := allPrecompiles[common.HexToAddress(addr)] 165 in := common.Hex2Bytes(test.Input) 166 reqGas := p.RequiredGas(in) 167 168 var ( 169 res []byte 170 err error 171 data = make([]byte, len(in)) 172 ) 173 174 bench.Run(fmt.Sprintf("%s-Gas=%d", test.Name, reqGas), func(bench *testing.B) { 175 bench.ReportAllocs() 176 start := time.Now() 177 bench.ResetTimer() 178 for i := 0; i < bench.N; i++ { 179 copy(data, in) 180 res, _, err = RunPrecompiledContract(p, data, reqGas) 181 } 182 bench.StopTimer() 183 elapsed := uint64(time.Since(start)) 184 if elapsed < 1 { 185 elapsed = 1 186 } 187 gasUsed := reqGas * uint64(bench.N) 188 bench.ReportMetric(float64(reqGas), "gas/op") 189 // Keep it as uint64, multiply 100 to get two digit float later 190 mgasps := (100 * 1000 * gasUsed) / elapsed 191 bench.ReportMetric(float64(mgasps)/100, "mgas/s") 192 //Check if it is correct 193 if err != nil { 194 bench.Error(err) 195 return 196 } 197 if common.Bytes2Hex(res) != test.Expected { 198 bench.Errorf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res)) 199 return 200 } 201 }) 202 } 203 204 // Benchmarks the sample inputs from the ECRECOVER precompile. 205 func BenchmarkPrecompiledEcrecover(bench *testing.B) { 206 t := precompiledTest{ 207 Input: "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02", 208 Expected: "000000000000000000000000ceaccac640adf55b2028469bd36ba501f28b699d", 209 Name: "", 210 } 211 benchmarkPrecompiled("01", t, bench) 212 } 213 214 // Benchmarks the sample inputs from the SHA256 precompile. 215 func BenchmarkPrecompiledSha256(bench *testing.B) { 216 t := precompiledTest{ 217 Input: "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02", 218 Expected: "811c7003375852fabd0d362e40e68607a12bdabae61a7d068fe5fdd1dbbf2a5d", 219 Name: "128", 220 } 221 benchmarkPrecompiled("02", t, bench) 222 } 223 224 // Benchmarks the sample inputs from the RIPEMD precompile. 225 func BenchmarkPrecompiledRipeMD(bench *testing.B) { 226 t := precompiledTest{ 227 Input: "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02", 228 Expected: "0000000000000000000000009215b8d9882ff46f0dfde6684d78e831467f65e6", 229 Name: "128", 230 } 231 benchmarkPrecompiled("03", t, bench) 232 } 233 234 // Benchmarks the sample inputs from the identiy precompile. 235 func BenchmarkPrecompiledIdentity(bench *testing.B) { 236 t := precompiledTest{ 237 Input: "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02", 238 Expected: "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02", 239 Name: "128", 240 } 241 benchmarkPrecompiled("04", t, bench) 242 } 243 244 // Tests the sample inputs from the ModExp EIP 198. 245 func TestPrecompiledModExp(t *testing.T) { testJson("modexp", "05", t) } 246 func BenchmarkPrecompiledModExp(b *testing.B) { benchJson("modexp", "05", b) } 247 248 func TestPrecompiledModExpEip2565(t *testing.T) { testJson("modexp_eip2565", "f5", t) } 249 func BenchmarkPrecompiledModExpEip2565(b *testing.B) { benchJson("modexp_eip2565", "f5", b) } 250 251 // Tests the sample inputs from the elliptic curve addition EIP 213. 252 func TestPrecompiledBn256Add(t *testing.T) { testJson("bn256Add", "06", t) } 253 func BenchmarkPrecompiledBn256Add(b *testing.B) { benchJson("bn256Add", "06", b) } 254 255 // Tests OOG 256 func TestPrecompiledModExpOOG(t *testing.T) { 257 modexpTests, err := loadJson("modexp") 258 if err != nil { 259 t.Fatal(err) 260 } 261 for _, test := range modexpTests { 262 testPrecompiledOOG("05", test, t) 263 } 264 } 265 266 // Tests the sample inputs from the elliptic curve scalar multiplication EIP 213. 267 func TestPrecompiledBn256ScalarMul(t *testing.T) { testJson("bn256ScalarMul", "07", t) } 268 func BenchmarkPrecompiledBn256ScalarMul(b *testing.B) { benchJson("bn256ScalarMul", "07", b) } 269 270 // Tests the sample inputs from the elliptic curve pairing check EIP 197. 271 func TestPrecompiledBn256Pairing(t *testing.T) { testJson("bn256Pairing", "08", t) } 272 func BenchmarkPrecompiledBn256Pairing(b *testing.B) { benchJson("bn256Pairing", "08", b) } 273 274 func TestPrecompiledBlake2F(t *testing.T) { testJson("blake2F", "09", t) } 275 func BenchmarkPrecompiledBlake2F(b *testing.B) { benchJson("blake2F", "09", b) } 276 277 func TestPrecompileBlake2FMalformedInput(t *testing.T) { 278 for _, test := range blake2FMalformedInputTests { 279 testPrecompiledFailure("09", test, t) 280 } 281 } 282 283 func TestPrecompiledEcrecover(t *testing.T) { testJson("ecRecover", "01", t) } 284 285 func testJson(name, addr string, t *testing.T) { 286 tests, err := loadJson(name) 287 if err != nil { 288 t.Fatal(err) 289 } 290 for _, test := range tests { 291 testPrecompiled(addr, test, t) 292 } 293 } 294 295 func testJsonFail(name, addr string, t *testing.T) { 296 tests, err := loadJsonFail(name) 297 if err != nil { 298 t.Fatal(err) 299 } 300 for _, test := range tests { 301 testPrecompiledFailure(addr, test, t) 302 } 303 } 304 305 func benchJson(name, addr string, b *testing.B) { 306 tests, err := loadJson(name) 307 if err != nil { 308 b.Fatal(err) 309 } 310 for _, test := range tests { 311 benchmarkPrecompiled(addr, test, b) 312 } 313 } 314 315 func TestPrecompiledBLS12381G1Add(t *testing.T) { testJson("blsG1Add", "0a", t) } 316 func TestPrecompiledBLS12381G1Mul(t *testing.T) { testJson("blsG1Mul", "0b", t) } 317 func TestPrecompiledBLS12381G1MultiExp(t *testing.T) { testJson("blsG1MultiExp", "0c", t) } 318 func TestPrecompiledBLS12381G2Add(t *testing.T) { testJson("blsG2Add", "0d", t) } 319 func TestPrecompiledBLS12381G2Mul(t *testing.T) { testJson("blsG2Mul", "0e", t) } 320 func TestPrecompiledBLS12381G2MultiExp(t *testing.T) { testJson("blsG2MultiExp", "0f", t) } 321 func TestPrecompiledBLS12381Pairing(t *testing.T) { testJson("blsPairing", "10", t) } 322 func TestPrecompiledBLS12381MapG1(t *testing.T) { testJson("blsMapG1", "11", t) } 323 func TestPrecompiledBLS12381MapG2(t *testing.T) { testJson("blsMapG2", "12", t) } 324 325 func BenchmarkPrecompiledBLS12381G1Add(b *testing.B) { benchJson("blsG1Add", "0a", b) } 326 func BenchmarkPrecompiledBLS12381G1Mul(b *testing.B) { benchJson("blsG1Mul", "0b", b) } 327 func BenchmarkPrecompiledBLS12381G1MultiExp(b *testing.B) { benchJson("blsG1MultiExp", "0c", b) } 328 func BenchmarkPrecompiledBLS12381G2Add(b *testing.B) { benchJson("blsG2Add", "0d", b) } 329 func BenchmarkPrecompiledBLS12381G2Mul(b *testing.B) { benchJson("blsG2Mul", "0e", b) } 330 func BenchmarkPrecompiledBLS12381G2MultiExp(b *testing.B) { benchJson("blsG2MultiExp", "0f", b) } 331 func BenchmarkPrecompiledBLS12381Pairing(b *testing.B) { benchJson("blsPairing", "10", b) } 332 func BenchmarkPrecompiledBLS12381MapG1(b *testing.B) { benchJson("blsMapG1", "11", b) } 333 func BenchmarkPrecompiledBLS12381MapG2(b *testing.B) { benchJson("blsMapG2", "12", b) } 334 335 // Failure tests 336 func TestPrecompiledBLS12381G1AddFail(t *testing.T) { testJsonFail("blsG1Add", "0a", t) } 337 func TestPrecompiledBLS12381G1MulFail(t *testing.T) { testJsonFail("blsG1Mul", "0b", t) } 338 func TestPrecompiledBLS12381G1MultiExpFail(t *testing.T) { testJsonFail("blsG1MultiExp", "0c", t) } 339 func TestPrecompiledBLS12381G2AddFail(t *testing.T) { testJsonFail("blsG2Add", "0d", t) } 340 func TestPrecompiledBLS12381G2MulFail(t *testing.T) { testJsonFail("blsG2Mul", "0e", t) } 341 func TestPrecompiledBLS12381G2MultiExpFail(t *testing.T) { testJsonFail("blsG2MultiExp", "0f", t) } 342 func TestPrecompiledBLS12381PairingFail(t *testing.T) { testJsonFail("blsPairing", "10", t) } 343 func TestPrecompiledBLS12381MapG1Fail(t *testing.T) { testJsonFail("blsMapG1", "11", t) } 344 func TestPrecompiledBLS12381MapG2Fail(t *testing.T) { testJsonFail("blsMapG2", "12", t) } 345 346 func loadJson(name string) ([]precompiledTest, error) { 347 data, err := os.ReadFile(fmt.Sprintf("testdata/precompiles/%v.json", name)) 348 if err != nil { 349 return nil, err 350 } 351 var testcases []precompiledTest 352 err = json.Unmarshal(data, &testcases) 353 return testcases, err 354 } 355 356 func loadJsonFail(name string) ([]precompiledFailureTest, error) { 357 data, err := os.ReadFile(fmt.Sprintf("testdata/precompiles/fail-%v.json", name)) 358 if err != nil { 359 return nil, err 360 } 361 var testcases []precompiledFailureTest 362 err = json.Unmarshal(data, &testcases) 363 return testcases, err 364 } 365 366 // BenchmarkPrecompiledBLS12381G1MultiExpWorstCase benchmarks the worst case we could find that still fits a gaslimit of 10MGas. 367 func BenchmarkPrecompiledBLS12381G1MultiExpWorstCase(b *testing.B) { 368 task := "0000000000000000000000000000000008d8c4a16fb9d8800cce987c0eadbb6b3b005c213d44ecb5adeed713bae79d606041406df26169c35df63cf972c94be1" + 369 "0000000000000000000000000000000011bc8afe71676e6730702a46ef817060249cd06cd82e6981085012ff6d013aa4470ba3a2c71e13ef653e1e223d1ccfe9" + 370 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 371 input := task 372 for i := 0; i < 4787; i++ { 373 input = input + task 374 } 375 testcase := precompiledTest{ 376 Input: input, 377 Expected: "0000000000000000000000000000000005a6310ea6f2a598023ae48819afc292b4dfcb40aabad24a0c2cb6c19769465691859eeb2a764342a810c5038d700f18000000000000000000000000000000001268ac944437d15923dc0aec00daa9250252e43e4b35ec7a19d01f0d6cd27f6e139d80dae16ba1c79cc7f57055a93ff5", 378 Name: "WorstCaseG1", 379 NoBenchmark: false, 380 } 381 benchmarkPrecompiled("0c", testcase, b) 382 } 383 384 // BenchmarkPrecompiledBLS12381G2MultiExpWorstCase benchmarks the worst case we could find that still fits a gaslimit of 10MGas. 385 func BenchmarkPrecompiledBLS12381G2MultiExpWorstCase(b *testing.B) { 386 task := "000000000000000000000000000000000d4f09acd5f362e0a516d4c13c5e2f504d9bd49fdfb6d8b7a7ab35a02c391c8112b03270d5d9eefe9b659dd27601d18f" + 387 "000000000000000000000000000000000fd489cb75945f3b5ebb1c0e326d59602934c8f78fe9294a8877e7aeb95de5addde0cb7ab53674df8b2cfbb036b30b99" + 388 "00000000000000000000000000000000055dbc4eca768714e098bbe9c71cf54b40f51c26e95808ee79225a87fb6fa1415178db47f02d856fea56a752d185f86b" + 389 "000000000000000000000000000000001239b7640f416eb6e921fe47f7501d504fadc190d9cf4e89ae2b717276739a2f4ee9f637c35e23c480df029fd8d247c7" + 390 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 391 input := task 392 for i := 0; i < 1040; i++ { 393 input = input + task 394 } 395 396 testcase := precompiledTest{ 397 Input: input, 398 Expected: "0000000000000000000000000000000018f5ea0c8b086095cfe23f6bb1d90d45de929292006dba8cdedd6d3203af3c6bbfd592e93ecb2b2c81004961fdcbb46c00000000000000000000000000000000076873199175664f1b6493a43c02234f49dc66f077d3007823e0343ad92e30bd7dc209013435ca9f197aca44d88e9dac000000000000000000000000000000000e6f07f4b23b511eac1e2682a0fc224c15d80e122a3e222d00a41fab15eba645a700b9ae84f331ae4ed873678e2e6c9b000000000000000000000000000000000bcb4849e460612aaed79617255fd30c03f51cf03d2ed4163ca810c13e1954b1e8663157b957a601829bb272a4e6c7b8", 399 Name: "WorstCaseG2", 400 NoBenchmark: false, 401 } 402 benchmarkPrecompiled("0f", testcase, b) 403 }