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

[5.0] Return error for num == 0 for bls_g1_weighted_sum, bls_g2_weighted_sum and bls_pairing #1954

Merged
merged 2 commits into from
Dec 6, 2023

Conversation

heifner
Copy link
Member

@heifner heifner commented Dec 6, 2023

Return error for num == 0 for host functions bls_g1_weighted_sum, bls_g2_weighted_sum, and bls_pairing.

Resolves #1953

@heifner heifner requested review from arhag and greg7mdp December 6, 2023 13:27
@heifner heifner added consensus OCI Work exclusive to OCI team labels Dec 6, 2023
@linh2931
Copy link
Member

linh2931 commented Dec 6, 2023

n == 0 tests need to be updates.

greg7mdp
greg7mdp approved these changes Dec 6, 2023
@@ -319,7 +319,7 @@ namespace eosio { namespace chain { namespace webassembly {
}
Copy link
Contributor

@greg7mdp greg7mdp Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to your change, and probably not something we want to do in 5.0, but maybe in the future we can make the code a little cleaner by converting the eos span<char> -> std::span<uint8_t> only once, as in:

   int32_t interface::bls_g1_weighted_sum(span<const char> _points, span<const char> _scalars, const uint32_t n, span<char> result) const {
      if(n == 0 || _points.size() != n*96 ||  _scalars.size() != n*32 ||  result.size() != 96)
         return return_code::failure;

      std::span<const uint8_t> points((const uint8_t*)_points.data(), n*96);
      std::span<const uint8_t> scalars((const uint8_t*)_scalars.data(), n*32);

      // Use much efficient scale for the special case of n == 1.
      if (1 == n) {
         std::optional<bls12_381::g1> a = bls12_381::g1::fromAffineBytesLE(points.first<96>(), true, false);
         if(!a)
            return return_code::failure;
         std::array<uint64_t, 4> b = bls12_381::scalar::fromBytesLE<4>(scalars.first<32>());
         bls12_381::g1 c = a->scale(b);
         c.toAffineBytesLE(std::span<uint8_t, 96>((uint8_t*)result.data(), 96), false);
         return return_code::success;
      }

      std::vector<bls12_381::g1> pv;
      std::vector<std::array<uint64_t, 4>> sv;
      pv.reserve(n);
      sv.reserve(n);
      for(uint32_t i = 0; i < n; i++)
      {
         std::optional<bls12_381::g1> p = bls12_381::g1::fromAffineBytesLE(points.subspan(i*96).first<96>(), true, false);
         if(!p.has_value())
            return return_code::failure;
         std::array<uint64_t, 4> s = bls12_381::scalar::fromBytesLE<4>(scalars.subspan(i*32).first<32>());
         pv.push_back(p.value());
         sv.push_back(s);
         if(i%10 == 0)
            context.trx_context.checktime();
      }
      bls12_381::g1 r = bls12_381::g1::weightedSum(pv, sv, [this](){ context.trx_context.checktime();}); // accessing value is safe
      r.toAffineBytesLE(std::span<uint8_t, 96>((uint8_t*)result.data(), 96), false);
      return return_code::success;
   }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a code style change like this appropriate at this point.

@heifner heifner merged commit 1a029ea into release/5.0 Dec 6, 2023
29 checks passed
@heifner heifner deleted the GH-1953-bls-n0-5.0 branch December 6, 2023 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
consensus OCI Work exclusive to OCI team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Return error for num == 0 for bls_g1_weighted_sum, bls_g2_weighted_sum and bls_pairing
4 participants