From 1e3d4486882825437f5d056faa22747d3d92c326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 17 Sep 2024 18:57:22 +0200 Subject: [PATCH] rework msm --- lib/evmone_precompiles/bls.cpp | 40 ++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/lib/evmone_precompiles/bls.cpp b/lib/evmone_precompiles/bls.cpp index b2b9e15d0a..6afa04e670 100644 --- a/lib/evmone_precompiles/bls.cpp +++ b/lib/evmone_precompiles/bls.cpp @@ -1,5 +1,6 @@ #include "bls.hpp" #include +#include #include #include @@ -202,7 +203,6 @@ void store(uint8_t _rx[128], const blst_fp2& _x) noexcept scalars_ptrs.reserve(npoints); auto ptr = _xycs; - for (size_t i = 0; i < npoints; ++i) { const auto p_affine = validate_p1(ptr, &ptr[64]); @@ -212,16 +212,17 @@ void store(uint8_t _rx[128], const blst_fp2& _x) noexcept if (!blst_p1_affine_in_g1(&*p_affine)) return false; + // Point at infinity must be filtered out for BLST library. if (blst_p1_affine_is_inf(&*p_affine)) continue; - p1_affines.emplace_back(*p_affine); - p1_affine_ptrs.emplace_back(&p1_affines.back()); + const auto& p = p1_affines.emplace_back(*p_affine); + p1_affine_ptrs.emplace_back(&p); blst_scalar scalar; blst_scalar_from_bendian(&scalar, &ptr[128]); - scalars.emplace_back(scalar); - scalars_ptrs.emplace_back(scalars.back().b); + const auto& s = scalars.emplace_back(scalar); + scalars_ptrs.emplace_back(s.b); ptr += SINGLE_ENTRY_SIZE; } @@ -235,10 +236,11 @@ void store(uint8_t _rx[128], const blst_fp2& _x) noexcept return true; } - const auto sz = blst_p1s_mult_pippenger_scratch_sizeof(npoints) / 8; + 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, - std::vector(sz).data()); + 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); @@ -266,7 +268,6 @@ void store(uint8_t _rx[128], const blst_fp2& _x) noexcept scalars_ptrs.reserve(npoints); auto ptr = _xycs; - for (size_t i = 0; i < npoints; ++i) { const auto p_affine = validate_p2(ptr, &ptr[128]); @@ -276,17 +277,17 @@ void store(uint8_t _rx[128], const blst_fp2& _x) noexcept if (!blst_p2_affine_in_g2(&*p_affine)) return false; - blst_scalar scalar; - blst_scalar_from_bendian(&scalar, &ptr[256]); - + // Point at infinity must be filtered out for BLST library. if (blst_p2_affine_is_inf(&*p_affine)) continue; - p2_affines.emplace_back(*p_affine); - p2_affine_ptrs.emplace_back(&p2_affines.back()); + const auto& p = p2_affines.emplace_back(*p_affine); + p2_affine_ptrs.emplace_back(&p); - scalars.emplace_back(scalar); - scalars_ptrs.emplace_back(scalars.back().b); + blst_scalar scalar; + blst_scalar_from_bendian(&scalar, &ptr[256]); + const auto& s = scalars.emplace_back(scalar); + scalars_ptrs.emplace_back(s.b); ptr += SINGLE_ENTRY_SIZE; } @@ -300,10 +301,11 @@ void store(uint8_t _rx[128], const blst_fp2& _x) noexcept return true; } - const auto sz = blst_p2s_mult_pippenger_scratch_sizeof(npoints) / 8; + 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, - std::vector(sz).data()); + 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);