From cfd11e600877cafb80398c27700beb61b304a6df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 9 Oct 2024 15:36:44 +0200 Subject: [PATCH] precompiles: Fallback to single mul in BLS MSM If the number of valid and non-zero inputs to BLS MSM is 1 use simpler single point multiplication. --- lib/evmone_precompiles/bls.cpp | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/evmone_precompiles/bls.cpp b/lib/evmone_precompiles/bls.cpp index 9a377c44f7..ca1e0bde2e 100644 --- a/lib/evmone_precompiles/bls.cpp +++ b/lib/evmone_precompiles/bls.cpp @@ -242,11 +242,20 @@ void store(uint8_t _rx[128], const blst_fp2& _x) noexcept return true; } - const auto scratch_size = blst_p1s_mult_pippenger_scratch_sizeof(npoints) / sizeof(limb_t); - const auto scratch_space = std::make_unique_for_overwrite(scratch_size); blst_p1 out; - blst_p1s_mult_pippenger( - &out, p1_affine_ptrs.data(), npoints, scalars_ptrs.data(), 256, scratch_space.get()); + if (npoints == 1) + { + blst_p1 p; + blst_p1_from_affine(&p, &p1_affines[0]); + blst_p1_mult(&out, &p, scalars[0].b, 256); + } + else + { + const auto scratch_size = blst_p1s_mult_pippenger_scratch_sizeof(npoints) / sizeof(limb_t); + const auto scratch_space = std::make_unique_for_overwrite(scratch_size); + blst_p1s_mult_pippenger( + &out, p1_affine_ptrs.data(), npoints, scalars_ptrs.data(), 256, scratch_space.get()); + } blst_p1_affine result; blst_p1_to_affine(&result, &out); @@ -307,11 +316,20 @@ void store(uint8_t _rx[128], const blst_fp2& _x) noexcept return true; } - const auto scratch_size = blst_p2s_mult_pippenger_scratch_sizeof(npoints) / sizeof(limb_t); - const auto scratch_space = std::make_unique_for_overwrite(scratch_size); blst_p2 out; - blst_p2s_mult_pippenger( - &out, p2_affine_ptrs.data(), npoints, scalars_ptrs.data(), 256, scratch_space.get()); + if (npoints == 1) + { + blst_p2 p; + blst_p2_from_affine(&p, &p2_affines[0]); + blst_p2_mult(&out, &p, scalars[0].b, 256); + } + else + { + const auto scratch_size = blst_p2s_mult_pippenger_scratch_sizeof(npoints) / sizeof(limb_t); + const auto scratch_space = std::make_unique_for_overwrite(scratch_size); + blst_p2s_mult_pippenger( + &out, p2_affine_ptrs.data(), npoints, scalars_ptrs.data(), 256, scratch_space.get()); + } blst_p2_affine result; blst_p2_to_affine(&result, &out);