Skip to content

Commit

Permalink
precompiles: Fallback to single mul in BLS MSM
Browse files Browse the repository at this point in the history
If the number of valid and non-zero inputs to BLS MSM is 1 use
simpler single point multiplication.
  • Loading branch information
chfast committed Oct 9, 2024
1 parent 400f137 commit cfd11e6
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions lib/evmone_precompiles/bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<limb_t[]>(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<limb_t[]>(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);
Expand Down Expand Up @@ -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<limb_t[]>(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<limb_t[]>(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);
Expand Down

0 comments on commit cfd11e6

Please sign in to comment.