nearcore icon indicating copy to clipboard operation
nearcore copied to clipboard

Precompiles for BLS12-381 curve operations

Open olga24912 opened this issue 1 year ago • 1 comments

Implementation for NEP-488: https://github.com/near/NEPs/pull/488

Gas Estimation

The test vectors for gas estimation were taken from EIP-2537 and adopted for our input format: https://eips.ethereum.org/assets/eip-2537/bench_vectors

  • bls12381_p1_sum_0_100 — the empty input
  • bls12381_p1_sum_500_100 — two points from G1 addition example input form EIP-2537(https://eips.ethereum.org/assets/eip-2537/bench_vectors) duplicated 250 times.
  • bls12381_p2_sum_0_100 — the empty input
  • bls12381_p2_sum_500_100 — two points from G2 addition example input form EIP-2537(https://eips.ethereum.org/assets/eip-2537/bench_vectors) duplicated 250 times.
  • bls12381_p1_multiexp_0_100 — the empty input
  • bls12381_p1_multiexp_500_100 — the data from G1 mul double and add worst case from EIP-2537(https://eips.ethereum.org/assets/eip-2537/bench_vectors) duplicated 500 times.
  • bls12381_p2_multiexp_0_100 — the empty input
  • bls12381_p2_multiexp_500_100 — the data from G2 mul double and add worst case from EIP-2537(https://eips.ethereum.org/assets/eip-2537/bench_vectors) duplicated 500 times.
  • bls12381_map_fp_to_g1_0_100 — the empty input
  • bls12381_map_fp_to_g1_500_100 — https://github.com/matter-labs/eip1962/blob/master/src/test/test_vectors/eip2537/fp_to_g1.csv — the first test input duplicate 500 times
  • bls12381_map_fp2_to_g2_0_100 — the empty input
  • bls12381_map_fp2_to_g2_100_100 — https://github.com/matter-labs/eip1962/blob/master/src/test/test_vectors/eip2537/fp2_to_g2.csv — the first input duplicate 1— times
  • bls12381_pairing_0_100 — the empty input
  • bls12381_pairing_10_100 — the data from Pairing case for 2 pairs from EIP-2537(https://eips.ethereum.org/assets/eip-2537/bench_vectors) duplicated 5 times.
  • bls12381_p1_decompress_0_100 — the empty input
  • bls12381_p1_decompress_500_100 — some random point duplicated 500 times
  • bls12381_p2_decompress_0_100 — the empty input
  • bls12381_p2_decompress_500_100 — some random point duplicated 500 times

Command for gas estimation (from runtime/runtime-params-estimator/emu-cost folder):

cargo run --release --package runtime-params-estimator --features required --bin runtime-params-estimator -- --home "./tmp_home_dir" --accounts-num 20000 --additional-accounts-num 200000 --iters 1 --warmup-iters 1 --docker --metric icount

Gas Estimation results:

Bls12381MapFpToG1Base                                  429_295_312 gas [     3434.36i 0.00r 0.00w] UNCERTAIN (computed in 80.63s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381MapFpToG1Element                            83_966_509_235 gas [   671732.07i 0.00r 0.00w] UNCERTAIN (computed in 4026.67s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381MapFp2ToG2Base                                 419_023_687 gas [     3352.19i 0.00r 0.00w] UNCERTAIN (computed in 22.73s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381MapFp2ToG2Element                          296_426_876_275 gas [  2371415.01i 0.00r 0.00w] UNCERTAIN (computed in 2721.62s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381PairingBase                                703_404_906_937 gas [  5627239.26i 0.00r 0.00w] UNCERTAIN (computed in 84.20s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381PairingElement                             780_117_299_425 gas [  6240938.40i 0.00r 0.00w] UNCERTAIN (computed in 764.44s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381P1SumBase                                    5_323_753_562 gas [    42590.03i 0.00r 0.00w] UNCERTAIN (computed in 23.04s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381P1SumElement                                 1_849_277_517 gas [    14794.22i 0.00r 0.00w] UNCERTAIN (computed in 102.23s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381P2SumBase                                    6_127_214_375 gas [    49017.71i 0.00r 0.00w] UNCERTAIN (computed in 23.53s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381P2SumElement                                 4_795_078_317 gas [    38360.63i 0.00r 0.00w] UNCERTAIN (computed in 234.46s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381P1MultiexpBase                               5_343_057_937 gas [    42744.46i 0.00r 0.00w] UNCERTAIN (computed in 23.03s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381P1MultiexpElement                          227_964_264_224 gas [  1823714.11i 0.00r 0.00w] UNCERTAIN (computed in 9953.47s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381P2MultiexpBase                               6_125_490_750 gas [    49003.93i 0.00r 0.00w] UNCERTAIN (computed in 22.57s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381P2MultiexpElement                          579_026_575_661 gas [  4632212.61i 0.00r 0.00w] UNCERTAIN (computed in 25917.74s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381P1DecompressBase                               432_901_062 gas [     3463.21i 0.00r 0.00w] UNCERTAIN (computed in 22.09s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381P1DecompressElement                         26_847_602_796 gas [   214780.82i 0.00r 0.00w] UNCERTAIN (computed in 1085.32s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381P2DecompressBase                               460_172_000 gas [     3681.38i 0.00r 0.00w] UNCERTAIN (computed in 22.19s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
Bls12381P2DecompressElement                         54_562_325_721 gas [   436498.61i 0.00r 0.00w] UNCERTAIN (computed in 2175.10s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24
ActionReceiptCreation                              465_406_203_750 gas [  3723249.63i 0.00r 0.00w] UNCERTAIN (computed in 25.85s) HIGH-VARIANCE: runtime/runtime-params-estimator/src/utils.rs:350:24

olga24912 avatar Jul 17 '23 10:07 olga24912

@Ekleog-NEAR: can you help review please?

akhi3030 avatar Mar 11 '24 10:03 akhi3030

Just to manage expectations correctly: since 3 weeks ago we have been working on congestion on the protocol level. As such, work on reviewing this has currently been deprioritized to the backlog, until work there finishes.

I literally skimmed through the changes, and can just say one thing for now: the +982 change to runtime/near-vm-runner/src/logic/logic.rs does not make me super happy. The file is already way too big as is at ~3k loc, but this would bring it up to ~4k. Can we instead, have a separate file that’d contain the actual implementation of the BLS-related functions (eg. a runtime/near-vm-runner/src/logic/logic/bls.rs module), and have the logic.rs file just delegate to it?

Ekleog-NEAR avatar Apr 02 '24 17:04 Ekleog-NEAR

Can we instead, have a separate file that’d contain the actual implementation of the BLS-related functions (eg. a runtime/near-vm-runner/src/logic/logic/bls.rs module), and have the logic.rs file just delegate to it?

fixed: https://github.com/near/nearcore/pull/9317/commits/ac6d4c78f7307f11c3b0d4fa74072d3822363040 https://github.com/near/nearcore/pull/9317/commits/c863395add1a7ac1b003e6800670c7d93c6e1285 https://github.com/near/nearcore/pull/9317/commits/3bd33cdd55a77ae493a34a3ba32d5debd835d32a https://github.com/near/nearcore/pull/9317/commits/98c7548e05bd1db85b8712bc6a06269d35e53641

olga24912 avatar Apr 11 '24 15:04 olga24912