Skip to content

Commit 01e0cfb

Browse files
committed
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.
1 parent 8e4a055 commit 01e0cfb

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

lib/evmone_precompiles/bls.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,20 @@ void store(uint8_t _rx[128], const blst_fp2& _x) noexcept
242242
return true;
243243
}
244244

245-
const auto scratch_size = blst_p1s_mult_pippenger_scratch_sizeof(npoints) / sizeof(limb_t);
246-
const auto scratch_space = std::make_unique_for_overwrite<limb_t[]>(scratch_size);
247245
blst_p1 out;
248-
blst_p1s_mult_pippenger(
249-
&out, p1_affine_ptrs.data(), npoints, scalars_ptrs.data(), 256, scratch_space.get());
246+
if (npoints == 1)
247+
{
248+
blst_p1 p;
249+
blst_p1_from_affine(&p, &p1_affines[0]);
250+
blst_p1_mult(&out, &p, scalars[0].b, 256);
251+
}
252+
else
253+
{
254+
const auto scratch_size = blst_p1s_mult_pippenger_scratch_sizeof(npoints) / sizeof(limb_t);
255+
const auto scratch_space = std::make_unique_for_overwrite<limb_t[]>(scratch_size);
256+
blst_p1s_mult_pippenger(
257+
&out, p1_affine_ptrs.data(), npoints, scalars_ptrs.data(), 256, scratch_space.get());
258+
}
250259

251260
blst_p1_affine result;
252261
blst_p1_to_affine(&result, &out);
@@ -307,11 +316,20 @@ void store(uint8_t _rx[128], const blst_fp2& _x) noexcept
307316
return true;
308317
}
309318

310-
const auto scratch_size = blst_p2s_mult_pippenger_scratch_sizeof(npoints) / sizeof(limb_t);
311-
const auto scratch_space = std::make_unique_for_overwrite<limb_t[]>(scratch_size);
312319
blst_p2 out;
313-
blst_p2s_mult_pippenger(
314-
&out, p2_affine_ptrs.data(), npoints, scalars_ptrs.data(), 256, scratch_space.get());
320+
if (npoints == 1)
321+
{
322+
blst_p2 p;
323+
blst_p2_from_affine(&p, &p2_affines[0]);
324+
blst_p2_mult(&out, &p, scalars[0].b, 256);
325+
}
326+
else
327+
{
328+
const auto scratch_size = blst_p2s_mult_pippenger_scratch_sizeof(npoints) / sizeof(limb_t);
329+
const auto scratch_space = std::make_unique_for_overwrite<limb_t[]>(scratch_size);
330+
blst_p2s_mult_pippenger(
331+
&out, p2_affine_ptrs.data(), npoints, scalars_ptrs.data(), 256, scratch_space.get());
332+
}
315333

316334
blst_p2_affine result;
317335
blst_p2_to_affine(&result, &out);

0 commit comments

Comments
 (0)