Skip to content

Commit

Permalink
rework msm
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Sep 17, 2024
1 parent 1c57a19 commit 1e3d448
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions lib/evmone_precompiles/bls.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "bls.hpp"
#include <blst.h>
#include <memory>
#include <optional>
#include <vector>

Expand Down Expand Up @@ -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]);
Expand All @@ -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;
}
Expand All @@ -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<limb_t[]>(scratch_size);
blst_p1 out;
blst_p1s_mult_pippenger(&out, p1_affine_ptrs.data(), npoints, scalars_ptrs.data(), 256,
std::vector<limb_t>(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);
Expand Down Expand Up @@ -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]);
Expand All @@ -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;
}
Expand All @@ -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<limb_t[]>(scratch_size);
blst_p2 out;
blst_p2s_mult_pippenger(&out, p2_affine_ptrs.data(), npoints, scalars_ptrs.data(), 256,
std::vector<limb_t>(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);
Expand Down

0 comments on commit 1e3d448

Please sign in to comment.