Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

precompiles: Investigate missing BLS test cases #1015

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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