go-ethereum
go-ethereum copied to clipboard
chore: Add Gnark Bn254 precompile methods for fuzzing
This adds the gnark precompile methods so that they can be used as fuzzing and perhaps in the future replace the google variant; I noticed that the google variant looked quite similar to the cloudflare one.
running BenchmarkPrecompiledBn256Add without this change:
goos: darwin
goarch: arm64
pkg: github.com/ethereum/go-ethereum/core/vm
BenchmarkPrecompiledBn256Add/chfast1-Gas=150-10 144722 11438 ns/op 150.0 gas/op 13.11 mgas/s 768 B/op 14 allocs/op
BenchmarkPrecompiledBn256Add/chfast2-Gas=150-10 143228 8027 ns/op 150.0 gas/op 18.68 mgas/s 768 B/op 14 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio1-Gas=150-10 1863967 633.8 ns/op 150.0 gas/op 236.7 mgas/s 608 B/op 9 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio2-Gas=150-10 1820971 666.8 ns/op 150.0 gas/op 224.9 mgas/s 672 B/op 10 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio3-Gas=150-10 1729078 663.7 ns/op 150.0 gas/op 226.0 mgas/s 672 B/op 10 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio4-Gas=150-10 1752135 677.8 ns/op 150.0 gas/op 221.3 mgas/s 736 B/op 11 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio5-Gas=150-10 1865620 638.2 ns/op 150.0 gas/op 235.0 mgas/s 608 B/op 9 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio6-Gas=150-10 1505930 962.8 ns/op 150.0 gas/op 155.8 mgas/s 608 B/op 9 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio7-Gas=150-10 1000000 1690 ns/op 150.0 gas/op 88.77 mgas/s 608 B/op 9 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio8-Gas=150-10 1444603 973.6 ns/op 150.0 gas/op 154.0 mgas/s 672 B/op 10 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio9-Gas=150-10 1539084 800.0 ns/op 150.0 gas/op 187.5 mgas/s 608 B/op 9 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio10-Gas=150-10 1533512 798.0 ns/op 150.0 gas/op 188.0 mgas/s 608 B/op 9 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio11-Gas=150-10 149769 8019 ns/op 150.0 gas/op 18.70 mgas/s 768 B/op 14 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio12-Gas=150-10 152294 7935 ns/op 150.0 gas/op 18.90 mgas/s 768 B/op 14 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio13-Gas=150-10 153973 7847 ns/op 150.0 gas/op 19.11 mgas/s 768 B/op 14 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio14-Gas=150-10 873187 1228 ns/op 150.0 gas/op 122.1 mgas/s 736 B/op 13 allocs/op
PASS
ok github.com/ethereum/go-ethereum/core/vm 28.161s
running BenchmarkPrecompiledBn256Add after change with Option 3:
goos: darwin
goarch: arm64
pkg: github.com/ethereum/go-ethereum/core/vm
BenchmarkPrecompiledBn256Add/chfast1-Gas=150-10 770485 1606 ns/op 150.0 gas/op 93.37 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/chfast2-Gas=150-10 690488 1590 ns/op 150.0 gas/op 94.31 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio1-Gas=150-10 2915010 412.6 ns/op 150.0 gas/op 363.5 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio2-Gas=150-10 2790193 434.4 ns/op 150.0 gas/op 345.3 mgas/s 128 B/op 2 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio3-Gas=150-10 2724303 432.6 ns/op 150.0 gas/op 346.7 mgas/s 128 B/op 2 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio4-Gas=150-10 2665974 447.3 ns/op 150.0 gas/op 335.3 mgas/s 192 B/op 3 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio5-Gas=150-10 2931380 411.6 ns/op 150.0 gas/op 364.4 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio6-Gas=150-10 2644624 455.0 ns/op 150.0 gas/op 329.6 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio7-Gas=150-10 2670866 474.8 ns/op 150.0 gas/op 315.9 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio8-Gas=150-10 2562014 463.8 ns/op 150.0 gas/op 323.4 mgas/s 128 B/op 2 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio9-Gas=150-10 2689185 449.3 ns/op 150.0 gas/op 333.9 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio10-Gas=150-10 2720560 445.7 ns/op 150.0 gas/op 336.6 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio11-Gas=150-10 749617 1602 ns/op 150.0 gas/op 93.62 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio12-Gas=150-10 756376 1618 ns/op 150.0 gas/op 92.71 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio13-Gas=150-10 759805 1575 ns/op 150.0 gas/op 95.21 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio14-Gas=150-10 2794452 429.0 ns/op 150.0 gas/op 349.6 mgas/s 64 B/op 1 allocs/op
PASS
ok github.com/ethereum/go-ethereum/core/vm 24.525s
running BenchmarkPrecompiledBn256Add after change with Option 2:
goos: darwin
goarch: arm64
pkg: github.com/ethereum/go-ethereum/core/vm
BenchmarkPrecompiledBn256Add/chfast1-Gas=150-10 678945 1795 ns/op 150.0 gas/op 83.55 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/chfast2-Gas=150-10 671589 1788 ns/op 150.0 gas/op 83.88 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio1-Gas=150-10 2918510 413.1 ns/op 150.0 gas/op 363.1 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio2-Gas=150-10 2739505 436.5 ns/op 150.0 gas/op 343.6 mgas/s 128 B/op 2 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio3-Gas=150-10 2756709 434.1 ns/op 150.0 gas/op 345.5 mgas/s 128 B/op 2 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio4-Gas=150-10 2652152 449.0 ns/op 150.0 gas/op 334.0 mgas/s 192 B/op 3 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio5-Gas=150-10 2854947 413.0 ns/op 150.0 gas/op 363.2 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio6-Gas=150-10 755706 1602 ns/op 150.0 gas/op 93.61 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio7-Gas=150-10 737630 1605 ns/op 150.0 gas/op 93.46 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio8-Gas=150-10 751152 1623 ns/op 150.0 gas/op 92.40 mgas/s 128 B/op 2 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio9-Gas=150-10 720931 1592 ns/op 150.0 gas/op 94.19 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio10-Gas=150-10 753760 1623 ns/op 150.0 gas/op 92.42 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio11-Gas=150-10 640694 1821 ns/op 150.0 gas/op 82.35 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio12-Gas=150-10 664954 1807 ns/op 150.0 gas/op 83.00 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio13-Gas=150-10 649362 1783 ns/op 150.0 gas/op 84.11 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio14-Gas=150-10 1946192 619.0 ns/op 150.0 gas/op 242.3 mgas/s 64 B/op 1 allocs/op
PASS
ok github.com/ethereum/go-ethereum/core/vm 22.711s
running BenchmarkPrecompiledBn256Add after change with Option 1:
goos: darwin
goarch: arm64
pkg: github.com/ethereum/go-ethereum/core/vm
BenchmarkPrecompiledBn256Add/chfast1-Gas=150-10 644062 1895 ns/op 150.0 gas/op 79.13 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/chfast2-Gas=150-10 631575 1888 ns/op 150.0 gas/op 79.45 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio1-Gas=150-10 2855358 420.2 ns/op 150.0 gas/op 356.9 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio2-Gas=150-10 2616627 442.2 ns/op 150.0 gas/op 339.2 mgas/s 128 B/op 2 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio3-Gas=150-10 2605514 447.3 ns/op 150.0 gas/op 335.3 mgas/s 128 B/op 2 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio4-Gas=150-10 2626849 463.2 ns/op 150.0 gas/op 323.8 mgas/s 192 B/op 3 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio5-Gas=150-10 2910724 429.5 ns/op 150.0 gas/op 349.3 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio6-Gas=150-10 755409 1597 ns/op 150.0 gas/op 93.90 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio7-Gas=150-10 735886 1626 ns/op 150.0 gas/op 92.23 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio8-Gas=150-10 756412 1609 ns/op 150.0 gas/op 93.23 mgas/s 128 B/op 2 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio9-Gas=150-10 726296 1606 ns/op 150.0 gas/op 93.38 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio10-Gas=150-10 737889 1596 ns/op 150.0 gas/op 93.96 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio11-Gas=150-10 651014 1867 ns/op 150.0 gas/op 80.33 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio12-Gas=150-10 645705 1924 ns/op 150.0 gas/op 77.96 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio13-Gas=150-10 650256 1865 ns/op 150.0 gas/op 80.43 mgas/s 64 B/op 1 allocs/op
BenchmarkPrecompiledBn256Add/cdetrio14-Gas=150-10 1675662 705.6 ns/op 150.0 gas/op 212.6 mgas/s 64 B/op 1 allocs/op
PASS
ok github.com/ethereum/go-ethereum/core/vm 22.787s
running BenchmarkPrecompiledBn256ScalarMul before this change:
goos: darwin
goarch: arm64
pkg: github.com/ethereum/go-ethereum/core/vm
BenchmarkPrecompiledBn256ScalarMul/chfast1-Gas=6000-10 22087 52429 ns/op 6000 gas/op 114.4 mgas/s 1280 B/op 25 allocs/op
BenchmarkPrecompiledBn256ScalarMul/chfast2-Gas=6000-10 21246 57075 ns/op 6000 gas/op 105.1 mgas/s 1473 B/op 26 allocs/op
BenchmarkPrecompiledBn256ScalarMul/chfast3-Gas=6000-10 21948 55421 ns/op 6000 gas/op 108.2 mgas/s 1473 B/op 26 allocs/op
BenchmarkPrecompiledBn256ScalarMul/cdetrio1-Gas=6000-10 20648 58788 ns/op 6000 gas/op 102.0 mgas/s 1505 B/op 26 allocs/op
BenchmarkPrecompiledBn256ScalarMul/cdetrio6-Gas=6000-10 20630 57878 ns/op 6000 gas/op 103.7 mgas/s 1505 B/op 26 allocs/op
BenchmarkPrecompiledBn256ScalarMul/cdetrio11-Gas=6000-10 19902 60144 ns/op 6000 gas/op 99.75 mgas/s 1505 B/op 26 allocs/op
PASS
ok github.com/ethereum/go-ethereum/core/vm 10.991s
after this change:
goos: darwin
goarch: arm64
pkg: github.com/ethereum/go-ethereum/core/vm
BenchmarkPrecompiledBn256ScalarMul/chfast1-Gas=6000-10 58022 20670 ns/op 6000 gas/op 290.3 mgas/s 328 B/op 5 allocs/op
BenchmarkPrecompiledBn256ScalarMul/chfast2-Gas=6000-10 30972 38502 ns/op 6000 gas/op 155.8 mgas/s 544 B/op 7 allocs/op
BenchmarkPrecompiledBn256ScalarMul/chfast3-Gas=6000-10 32322 37669 ns/op 6000 gas/op 159.3 mgas/s 544 B/op 7 allocs/op
BenchmarkPrecompiledBn256ScalarMul/cdetrio1-Gas=6000-10 29667 41034 ns/op 6000 gas/op 146.2 mgas/s 576 B/op 7 allocs/op
BenchmarkPrecompiledBn256ScalarMul/cdetrio6-Gas=6000-10 29694 40466 ns/op 6000 gas/op 148.3 mgas/s 576 B/op 7 allocs/op
BenchmarkPrecompiledBn256ScalarMul/cdetrio11-Gas=6000-10 29661 41006 ns/op 6000 gas/op 146.3 mgas/s 576 B/op 7 allocs/op
PASS
ok github.com/ethereum/go-ethereum/core/vm 10.148s
running BenchmarkPrecompiledBn256Pairing before this change:
goos: darwin
goarch: arm64
pkg: github.com/ethereum/go-ethereum/core/vm
BenchmarkPrecompiledBn256Pairing/jeff1-Gas=113000-10 691 1595406 ns/op 113000 gas/op 70.82 mgas/s 84533 B/op 774 allocs/op
BenchmarkPrecompiledBn256Pairing/jeff2-Gas=113000-10 747 1611983 ns/op 113000 gas/op 70.09 mgas/s 84533 B/op 774 allocs/op
BenchmarkPrecompiledBn256Pairing/jeff3-Gas=113000-10 742 1609359 ns/op 113000 gas/op 70.20 mgas/s 84528 B/op 774 allocs/op
BenchmarkPrecompiledBn256Pairing/jeff4-Gas=147000-10 547 2159726 ns/op 147000 gas/op 68.06 mgas/s 126561 B/op 1158 allocs/op
BenchmarkPrecompiledBn256Pairing/jeff5-Gas=147000-10 558 2139415 ns/op 147000 gas/op 68.70 mgas/s 126560 B/op 1158 allocs/op
BenchmarkPrecompiledBn256Pairing/jeff6-Gas=113000-10 758 1588707 ns/op 113000 gas/op 71.12 mgas/s 84496 B/op 773 allocs/op
BenchmarkPrecompiledBn256Pairing/empty_data-Gas=45000-10 2304 531138 ns/op 45000 gas/op 84.72 mgas/s 544 B/op 6 allocs/op
BenchmarkPrecompiledBn256Pairing/one_point-Gas=79000-10 984 1076808 ns/op 79000 gas/op 73.35 mgas/s 42496 B/op 389 allocs/op
BenchmarkPrecompiledBn256Pairing/two_point_match_2-Gas=113000-10 742 1612361 ns/op 113000 gas/op 70.07 mgas/s 84528 B/op 774 allocs/op
BenchmarkPrecompiledBn256Pairing/two_point_match_3-Gas=113000-10 759 1630423 ns/op 113000 gas/op 69.30 mgas/s 84528 B/op 774 allocs/op
BenchmarkPrecompiledBn256Pairing/two_point_match_4-Gas=113000-10 740 1611816 ns/op 113000 gas/op 70.10 mgas/s 84528 B/op 774 allocs/op
BenchmarkPrecompiledBn256Pairing/ten_point_match_1-Gas=385000-10 199 5874608 ns/op 385000 gas/op 65.53 mgas/s 420724 B/op 3836 allocs/op
BenchmarkPrecompiledBn256Pairing/ten_point_match_2-Gas=385000-10 204 6294504 ns/op 385000 gas/op 61.16 mgas/s 420724 B/op 3836 allocs/op
BenchmarkPrecompiledBn256Pairing/ten_point_match_3-Gas=113000-10 741 1643028 ns/op 113000 gas/op 68.77 mgas/s 84528 B/op 774 allocs/op
PASS
ok github.com/ethereum/go-ethereum/core/vm 20.131s
after this change:
goos: darwin
goarch: arm64
pkg: github.com/ethereum/go-ethereum/core/vm
BenchmarkPrecompiledBn256Pairing/jeff1-Gas=113000-10 1125 1013241 ns/op 113000 gas/op 111.5 mgas/s 2240 B/op 20 allocs/op
BenchmarkPrecompiledBn256Pairing/jeff2-Gas=113000-10 1173 1012252 ns/op 113000 gas/op 111.6 mgas/s 2240 B/op 20 allocs/op
BenchmarkPrecompiledBn256Pairing/jeff3-Gas=113000-10 1196 1011407 ns/op 113000 gas/op 111.7 mgas/s 2240 B/op 20 allocs/op
BenchmarkPrecompiledBn256Pairing/jeff4-Gas=147000-10 819 1337333 ns/op 147000 gas/op 109.9 mgas/s 3400 B/op 27 allocs/op
BenchmarkPrecompiledBn256Pairing/jeff5-Gas=147000-10 891 1324176 ns/op 147000 gas/op 111.0 mgas/s 3400 B/op 27 allocs/op
BenchmarkPrecompiledBn256Pairing/jeff6-Gas=113000-10 1194 1045490 ns/op 113000 gas/op 108.1 mgas/s 2240 B/op 20 allocs/op
BenchmarkPrecompiledBn256Pairing/empty_data-Gas=45000-10 100000000 11.94 ns/op 45000 gas/op 3769467 mgas/s 0 B/op 0 allocs/op
BenchmarkPrecompiledBn256Pairing/one_point-Gas=79000-10 1670 731080 ns/op 79000 gas/op 108.0 mgas/s 1112 B/op 13 allocs/op
BenchmarkPrecompiledBn256Pairing/two_point_match_2-Gas=113000-10 1887 610901 ns/op 113000 gas/op 185.0 mgas/s 2240 B/op 20 allocs/op
BenchmarkPrecompiledBn256Pairing/two_point_match_3-Gas=113000-10 1186 1086566 ns/op 113000 gas/op 104.0 mgas/s 2240 B/op 20 allocs/op
BenchmarkPrecompiledBn256Pairing/two_point_match_4-Gas=113000-10 1057 1039404 ns/op 113000 gas/op 108.7 mgas/s 2240 B/op 20 allocs/op
BenchmarkPrecompiledBn256Pairing/ten_point_match_1-Gas=385000-10 441 2701782 ns/op 385000 gas/op 142.5 mgas/s 11584 B/op 66 allocs/op
BenchmarkPrecompiledBn256Pairing/ten_point_match_2-Gas=385000-10 376 3183994 ns/op 385000 gas/op 120.9 mgas/s 11584 B/op 66 allocs/op
BenchmarkPrecompiledBn256Pairing/ten_point_match_3-Gas=113000-10 1152 1011952 ns/op 113000 gas/op 111.7 mgas/s 2240 B/op 20 allocs/op
PASS
ok github.com/ethereum/go-ethereum/core/vm 18.812s
Note: BenchmarkPrecompiledBn256Pairing/empty_data-Gas=45000-10 is fast because the program just exits early in that case
Could you also add it to the fuzzer already? I would kinda prefer the second option, since it uses the public api of gnark as they probably intended. The third option feels a bit unsafe to me and I would prefer to have that upstreamed to gnark before we use it. I guess it doesn't really matter since we will be using this only for fuzzing for a while
Could you also add it to the fuzzer already?
Yep, I'll modify the fuzzer code
I would kinda prefer the second option
Alright, I'll delete the g1_aff code and go with ~~option 2~~ option 1 (any discrepancy between 1 and 2 could be fixed upstream)
The third option feels a bit unsafe to me and I would prefer to have that upstreamed to gnark before we use it. I guess it doesn't really matter since we will be using this only for fuzzing for a while
I'll open up an issue in Gnark to upstream this -- its not in gnark because its catering to a specific usecase that is expensive for every other usecase.