Skip to content

Commit

Permalink
precompiles: Investigate missing BLS test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Sep 18, 2024
1 parent d3de72d commit 23e95b9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/evmone_precompiles/bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace evmone::crypto::bls
{
using namespace intx;
namespace
{
/// Offset of the beginning of field element. First 16 bytes must be zero according to spec
Expand Down Expand Up @@ -203,6 +204,7 @@ void store(uint8_t _rx[128], const blst_fp2& _x) noexcept
scalars_ptrs.reserve(npoints);

auto ptr = _xycs;
[[maybe_unused]] const auto x_org_npoints = npoints;
for (size_t i = 0; i < npoints; ++i)
{
const auto p_affine = validate_p1(ptr, &ptr[64]);
Expand All @@ -214,11 +216,18 @@ void store(uint8_t _rx[128], const blst_fp2& _x) noexcept

// Point at infinity must be filtered out for BLST library.
if (blst_p1_affine_is_inf(&*p_affine))
{
[[maybe_unused]] const auto xs = be::unsafe::load<uint256>(ptr + 128);
// assert(xs == 0);
// assert(xs != 0);
assert(xs != 1);
continue;
}

const auto& p = p1_affines.emplace_back(*p_affine);
p1_affine_ptrs.emplace_back(&p);

// assert((ptr[128] & 0x80) != 0);
blst_scalar scalar;
blst_scalar_from_bendian(&scalar, &ptr[128]);
const auto& s = scalars.emplace_back(scalar);
Expand All @@ -231,6 +240,8 @@ void store(uint8_t _rx[128], const blst_fp2& _x) noexcept

if (npoints == 0)
{
// assert(x_org_npoints == 0);
assert(x_org_npoints != 0);
memset(_rx, 0, 64);
memset(_ry, 0, 64);
return true;
Expand Down Expand Up @@ -267,6 +278,7 @@ void store(uint8_t _rx[128], const blst_fp2& _x) noexcept
scalars.reserve(npoints);
scalars_ptrs.reserve(npoints);

[[maybe_unused]] const auto x_org_npoints = npoints;
auto ptr = _xycs;
for (size_t i = 0; i < npoints; ++i)
{
Expand All @@ -279,11 +291,18 @@ void store(uint8_t _rx[128], const blst_fp2& _x) noexcept

// Point at infinity must be filtered out for BLST library.
if (blst_p2_affine_is_inf(&*p_affine))
{
[[maybe_unused]] const auto xs = be::unsafe::load<uint256>(ptr + 256);
// assert(xs == 0);
// assert(xs != 0);
assert(xs != 1);
continue;
}

const auto& p = p2_affines.emplace_back(*p_affine);
p2_affine_ptrs.emplace_back(&p);

// assert((ptr[256] & 0x80) != 0);
blst_scalar scalar;
blst_scalar_from_bendian(&scalar, &ptr[256]);
const auto& s = scalars.emplace_back(scalar);
Expand All @@ -296,6 +315,8 @@ void store(uint8_t _rx[128], const blst_fp2& _x) noexcept

if (npoints == 0)
{
// assert(x_org_npoints == 0);
assert(x_org_npoints != 0);
memset(_rx, 0, 128);
memset(_ry, 0, 128);
return true;
Expand Down
4 changes: 4 additions & 0 deletions test/state/precompiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ PrecompileAnalysis bls12_g1mul_analyze(bytes_view, evmc_revision) noexcept

PrecompileAnalysis bls12_g1msm_analyze(bytes_view input, evmc_revision) noexcept
{
// assert(!input.empty());
// assert(input.size() % 160 == 0);
if (input.empty() || input.size() % 160 != 0)
return {GasCostMax, 0};

Expand All @@ -208,6 +210,8 @@ PrecompileAnalysis bls12_g2mul_analyze(bytes_view, evmc_revision) noexcept

PrecompileAnalysis bls12_g2msm_analyze(bytes_view input, evmc_revision) noexcept
{
// assert(!input.empty());
// assert(input.size() % 288 == 0);
if (input.empty() || input.size() % 288 != 0)
return {GasCostMax, 0};

Expand Down

0 comments on commit 23e95b9

Please sign in to comment.