From 6563ea9c981e184e58c35187f4bd10bb5d1ef91d Mon Sep 17 00:00:00 2001 From: greg7mdp Date: Tue, 2 Apr 2024 16:44:06 -0400 Subject: [PATCH 1/2] Update for new version of bls library with change in to/fromBytes params. --- benchmark/bls.cpp | 22 +++++----- libraries/chain/webassembly/crypto.cpp | 59 +++++++++++++------------- 2 files changed, 41 insertions(+), 40 deletions(-) diff --git a/benchmark/bls.cpp b/benchmark/bls.cpp index 4c86e78ba1..b1fcc35e6e 100644 --- a/benchmark/bls.cpp +++ b/benchmark/bls.cpp @@ -152,7 +152,7 @@ void benchmark_bls_g1_add() { // prepare g1 operand in Jacobian LE format g1 p = random_g1(); std::array op; - p.toAffineBytesLE(std::span((uint8_t*)op.data(), 96), false); + p.toAffineBytesLE(std::span((uint8_t*)op.data(), 96), from_mont::yes); // prepare result operand std::array result; @@ -171,7 +171,7 @@ void benchmark_bls_g2_add() { // prepare g2 operand in Jacobian LE format g2 p = random_g2(); std::array op; - p.toAffineBytesLE(std::span((uint8_t*)op.data(), 192), false); + p.toAffineBytesLE(std::span((uint8_t*)op.data(), 192), from_mont::yes); // prepare result operand std::array result; @@ -191,7 +191,7 @@ void benchmark_bls_g1_weighted_sum_impl(const std::string& test_name, uint32_t n std::vector g1_buf(96*num_points); for (auto i=0u; i < num_points; ++i) { g1 p = random_g1(); - p.toAffineBytesLE(std::span((uint8_t*)g1_buf.data() + i * 96, 96), false); + p.toAffineBytesLE(std::span((uint8_t*)g1_buf.data() + i * 96, 96), from_mont::yes); } chain::span g1_points(g1_buf.data(), g1_buf.size()); @@ -236,7 +236,7 @@ void benchmark_bls_g2_weighted_sum_impl(const std::string& test_name, uint32_t n std::vector g2_buf(192*num_points); for (auto i=0u; i < num_points; ++i) { g2 p = random_g2(); - p.toAffineBytesLE(std::span((uint8_t*)g2_buf.data() + i * 192, 192), false); + p.toAffineBytesLE(std::span((uint8_t*)g2_buf.data() + i * 192, 192), from_mont::yes); } eosio::chain::span g2_points(g2_buf.data(), g2_buf.size()); @@ -281,7 +281,7 @@ void benchmark_bls_pairing_impl(const std::string& test_name, uint32_t num_pairs std::vector g1_buf(96*num_pairs); for (auto i=0u; i < num_pairs; ++i) { g1 p = random_g1(); - p.toAffineBytesLE(std::span((uint8_t*)g1_buf.data() + i * 96, 96), false); + p.toAffineBytesLE(std::span((uint8_t*)g1_buf.data() + i * 96, 96), from_mont::yes); } eosio::chain::span g1_points(g1_buf.data(), g1_buf.size()); @@ -289,7 +289,7 @@ void benchmark_bls_pairing_impl(const std::string& test_name, uint32_t num_pairs std::vector g2_buf(192*num_pairs); for (auto i=0u; i < num_pairs; ++i) { g2 p2 = random_g2(); - p2.toAffineBytesLE(std::span((uint8_t*)g2_buf.data() + i * 192, (192)), false); + p2.toAffineBytesLE(std::span((uint8_t*)g2_buf.data() + i * 192, (192)), from_mont::yes); } eosio::chain::span g2_points(g2_buf.data(), g2_buf.size()); @@ -320,7 +320,7 @@ void benchmark_bls_g1_map() { // prepare e operand. Must be fp LE. std::array e; fp a = random_fe(); - a.toBytesLE(std::span((uint8_t*)e.data(), 48), false); + a.toBytesLE(std::span((uint8_t*)e.data(), 48), from_mont::yes); // prepare result operand std::array result; @@ -338,7 +338,7 @@ void benchmark_bls_g1_map() { void benchmark_bls_g2_map() { std::array e; fp2 a = random_fe2(); - a.toBytesLE(std::span((uint8_t*)e.data(), 96), false); + a.toBytesLE(std::span((uint8_t*)e.data(), 96), from_mont::yes); // prepare result operand std::array result; @@ -378,12 +378,12 @@ void benchmark_bls_fp_mul() { // prepare op1 std::array op1; fp a = random_fe(); - a.toBytesLE(std::span((uint8_t*)op1.data(), 48), false); + a.toBytesLE(std::span((uint8_t*)op1.data(), 48), from_mont::yes); // prepare op2 std::array op2; fp b = random_fe(); - b.toBytesLE(std::span((uint8_t*)op2.data(), 48), false); + b.toBytesLE(std::span((uint8_t*)op2.data(), 48), from_mont::yes); // prepare result operand std::array result; @@ -401,7 +401,7 @@ void benchmark_bls_fp_exp() { // prepare base std::array base; fp a = random_fe(); - a.toBytesLE(std::span((uint8_t*)base.data(), 48), false); + a.toBytesLE(std::span((uint8_t*)base.data(), 48), from_mont::yes); // prepare exp operand std::array exp; diff --git a/libraries/chain/webassembly/crypto.cpp b/libraries/chain/webassembly/crypto.cpp index 470cc3b28a..4b1fef0028 100644 --- a/libraries/chain/webassembly/crypto.cpp +++ b/libraries/chain/webassembly/crypto.cpp @@ -25,9 +25,10 @@ namespace { namespace { using eosio::chain::span; using eosio::chain::webassembly::return_code; + using bls12_381::from_mont; } -namespace eosio { namespace chain { namespace webassembly { +namespace eosio::chain::webassembly { void interface::assert_recover_key( legacy_ptr digest, legacy_span sig, @@ -262,24 +263,24 @@ namespace eosio { namespace chain { namespace webassembly { int32_t interface::bls_g1_add(span op1, span op2, span result) const { if(op1.size() != 96 || op2.size() != 96 || result.size() != 96) return return_code::failure; - std::optional a = bls12_381::g1::fromAffineBytesLE(std::span((const uint8_t*)op1.data(), 96), true, false); - std::optional b = bls12_381::g1::fromAffineBytesLE(std::span((const uint8_t*)op2.data(), 96), true, false); + std::optional a = bls12_381::g1::fromAffineBytesLE(std::span((const uint8_t*)op1.data(), 96), {.check_valid = true, .to_mont = true}); + std::optional b = bls12_381::g1::fromAffineBytesLE(std::span((const uint8_t*)op2.data(), 96), {.check_valid = true, .to_mont = true}); if(!a || !b) return return_code::failure; bls12_381::g1 c = a->add(*b); - c.toAffineBytesLE(std::span((uint8_t*)result.data(), 96), false); + c.toAffineBytesLE(std::span((uint8_t*)result.data(), 96), from_mont::yes); return return_code::success; } int32_t interface::bls_g2_add(span op1, span op2, span result) const { if(op1.size() != 192 || op2.size() != 192 || result.size() != 192) return return_code::failure; - std::optional a = bls12_381::g2::fromAffineBytesLE(std::span((const uint8_t*)op1.data(), 192), true, false); - std::optional b = bls12_381::g2::fromAffineBytesLE(std::span((const uint8_t*)op2.data(), 192), true, false); + std::optional a = bls12_381::g2::fromAffineBytesLE(std::span((const uint8_t*)op1.data(), 192), {.check_valid = true, .to_mont = true}); + std::optional b = bls12_381::g2::fromAffineBytesLE(std::span((const uint8_t*)op2.data(), 192), {.check_valid = true, .to_mont = true}); if(!a || !b) return return_code::failure; bls12_381::g2 c = a->add(*b); - c.toAffineBytesLE(std::span((uint8_t*)result.data(), 192), false); + c.toAffineBytesLE(std::span((uint8_t*)result.data(), 192), from_mont::yes); return return_code::success; } @@ -289,12 +290,12 @@ namespace eosio { namespace chain { namespace webassembly { // Use much efficient scale for the special case of n == 1. if (1 == n) { - std::optional a = bls12_381::g1::fromAffineBytesLE(std::span((const uint8_t*)points.data(), 96), true, false); + std::optional a = bls12_381::g1::fromAffineBytesLE(std::span((const uint8_t*)points.data(), 96), {.check_valid = true, .to_mont = true}); if(!a) return return_code::failure; std::array b = bls12_381::scalar::fromBytesLE<4>(std::span((uint8_t*)scalars.data(), 32)); bls12_381::g1 c = a->scale(b); - c.toAffineBytesLE(std::span((uint8_t*)result.data(), 96), false); + c.toAffineBytesLE(std::span((uint8_t*)result.data(), 96), from_mont::yes); return return_code::success; } @@ -304,7 +305,7 @@ namespace eosio { namespace chain { namespace webassembly { sv.reserve(n); for(uint32_t i = 0; i < n; i++) { - std::optional p = bls12_381::g1::fromAffineBytesLE(std::span((const uint8_t*)points.data() + i*96, 96), true, false); + std::optional p = bls12_381::g1::fromAffineBytesLE(std::span((const uint8_t*)points.data() + i*96, 96), {.check_valid = true, .to_mont = true}); if(!p.has_value()) return return_code::failure; std::array s = bls12_381::scalar::fromBytesLE<4>(std::span((const uint8_t*)scalars.data() + i*32, 32)); @@ -314,7 +315,7 @@ namespace eosio { namespace chain { namespace webassembly { 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*)result.data(), 96), false); + r.toAffineBytesLE(std::span((uint8_t*)result.data(), 96), from_mont::yes); return return_code::success; } @@ -324,12 +325,12 @@ namespace eosio { namespace chain { namespace webassembly { // Use much efficient scale for the special case of n == 1. if (1 == n) { - std::optional a = bls12_381::g2::fromAffineBytesLE(std::span((const uint8_t*)points.data(), 192), true, false); + std::optional a = bls12_381::g2::fromAffineBytesLE(std::span((const uint8_t*)points.data(), 192), {.check_valid = true, .to_mont = true}); if(!a) return return_code::failure; std::array b = bls12_381::scalar::fromBytesLE<4>(std::span((uint8_t*)scalars.data(), 32)); bls12_381::g2 c = a->scale(b); - c.toAffineBytesLE(std::span((uint8_t*)result.data(), 192), false); + c.toAffineBytesLE(std::span((uint8_t*)result.data(), 192), from_mont::yes); return return_code::success; } @@ -339,7 +340,7 @@ namespace eosio { namespace chain { namespace webassembly { sv.reserve(n); for(uint32_t i = 0; i < n; i++) { - std::optional p = bls12_381::g2::fromAffineBytesLE(std::span((const uint8_t*)points.data() + i*192, 192), true, false); + std::optional p = bls12_381::g2::fromAffineBytesLE(std::span((const uint8_t*)points.data() + i*192, 192), {.check_valid = true, .to_mont = true}); if(!p) return return_code::failure; std::array s = bls12_381::scalar::fromBytesLE<4>(std::span((const uint8_t*)scalars.data() + i*32, 32)); @@ -349,7 +350,7 @@ namespace eosio { namespace chain { namespace webassembly { context.trx_context.checktime(); } bls12_381::g2 r = bls12_381::g2::weightedSum(pv, sv, [this](){ context.trx_context.checktime();}); // accessing value is safe - r.toAffineBytesLE(std::span((uint8_t*)result.data(), 192), false); + r.toAffineBytesLE(std::span((uint8_t*)result.data(), 192), from_mont::yes); return return_code::success; } @@ -360,8 +361,8 @@ namespace eosio { namespace chain { namespace webassembly { v.reserve(n); for(uint32_t i = 0; i < n; i++) { - std::optional p_g1 = bls12_381::g1::fromAffineBytesLE(std::span((const uint8_t*)g1_points.data() + i*96, 96), true, false); - std::optional p_g2 = bls12_381::g2::fromAffineBytesLE(std::span((const uint8_t*)g2_points.data() + i*192, 192), true, false); + std::optional p_g1 = bls12_381::g1::fromAffineBytesLE(std::span((const uint8_t*)g1_points.data() + i*96, 96), {.check_valid = true, .to_mont = true}); + std::optional p_g2 = bls12_381::g2::fromAffineBytesLE(std::span((const uint8_t*)g2_points.data() + i*192, 192), {.check_valid = true, .to_mont = true}); if(!p_g1 || !p_g2) return return_code::failure; bls12_381::pairing::add_pair(v, *p_g1, *p_g2); @@ -369,29 +370,29 @@ namespace eosio { namespace chain { namespace webassembly { context.trx_context.checktime(); } bls12_381::fp12 r = bls12_381::pairing::calculate(v, [this](){ context.trx_context.checktime();}); - r.toBytesLE(std::span((uint8_t*)result.data(), 576), false); + r.toBytesLE(std::span((uint8_t*)result.data(), 576), from_mont::yes); return return_code::success; } int32_t interface::bls_g1_map(span e, span result) const { if(e.size() != 48 || result.size() != 96) return return_code::failure; - std::optional a = bls12_381::fp::fromBytesLE(std::span((const uint8_t*)e.data(), 48), true, false); + std::optional a = bls12_381::fp::fromBytesLE(std::span((const uint8_t*)e.data(), 48), {.check_valid = true, .to_mont = true}); if(!a) return return_code::failure; bls12_381::g1 c = bls12_381::g1::mapToCurve(*a); - c.toAffineBytesLE(std::span((uint8_t*)result.data(), 96), false); + c.toAffineBytesLE(std::span((uint8_t*)result.data(), 96), from_mont::yes); return return_code::success; } int32_t interface::bls_g2_map(span e, span result) const { if(e.size() != 96 || result.size() != 192) return return_code::failure; - std::optional a = bls12_381::fp2::fromBytesLE(std::span((const uint8_t*)e.data(), 96), true, false); + std::optional a = bls12_381::fp2::fromBytesLE(std::span((const uint8_t*)e.data(), 96), {.check_valid = true, .to_mont = true}); if(!a) return return_code::failure; bls12_381::g2 c = bls12_381::g2::mapToCurve(*a); - c.toAffineBytesLE(std::span((uint8_t*)result.data(), 192), false); + c.toAffineBytesLE(std::span((uint8_t*)result.data(), 192), from_mont::yes); return return_code::success; } @@ -401,19 +402,19 @@ namespace eosio { namespace chain { namespace webassembly { return return_code::failure; std::array k = bls12_381::scalar::fromBytesLE<8>(std::span((const uint8_t*)s.data(), 64)); bls12_381::fp e = bls12_381::fp::modPrime<8>(k); - e.toBytesLE(std::span((uint8_t*)result.data(), 48), false); + e.toBytesLE(std::span((uint8_t*)result.data(), 48), from_mont::yes); return return_code::success; } int32_t interface::bls_fp_mul(span op1, span op2, span result) const { if(op1.size() != 48 || op2.size() != 48 || result.size() != 48) return return_code::failure; - std::optional a = bls12_381::fp::fromBytesLE(std::span((const uint8_t*)op1.data(), 48), true, false); - std::optional b = bls12_381::fp::fromBytesLE(std::span((const uint8_t*)op2.data(), 48), true, false); + std::optional a = bls12_381::fp::fromBytesLE(std::span((const uint8_t*)op1.data(), 48), {.check_valid = true, .to_mont = true}); + std::optional b = bls12_381::fp::fromBytesLE(std::span((const uint8_t*)op2.data(), 48), {.check_valid = true, .to_mont = true}); if(!a || !b) return return_code::failure; bls12_381::fp c = a->multiply(*b); - c.toBytesLE(std::span((uint8_t*)result.data(), 48), false); + c.toBytesLE(std::span((uint8_t*)result.data(), 48), from_mont::yes); return return_code::success; } @@ -421,13 +422,13 @@ namespace eosio { namespace chain { namespace webassembly { // exp is scalar. if(base.size() != 48 || exp.size() != 64 || result.size() != 48) return return_code::failure; - std::optional a = bls12_381::fp::fromBytesLE(std::span((const uint8_t*)base.data(), 48), true, false); + std::optional a = bls12_381::fp::fromBytesLE(std::span((const uint8_t*)base.data(), 48), {.check_valid = true, .to_mont = true}); if(!a) return return_code::failure; std::array b = bls12_381::scalar::fromBytesLE<8>(std::span((const uint8_t*)exp.data(), 64)); bls12_381::fp c = a->exp<8>(b); - c.toBytesLE(std::span((uint8_t*)result.data(), 48), false); + c.toBytesLE(std::span((uint8_t*)result.data(), 48), from_mont::yes); return return_code::success; } -}}} // ns eosio::chain::webassembly +} // ns eosio::chain::webassembly From 3a3fe8c050c6575869253d500848eb75456ad543 Mon Sep 17 00:00:00 2001 From: greg7mdp Date: Tue, 2 Apr 2024 20:39:48 -0400 Subject: [PATCH 2/2] Update to latest bls12 commit. --- libraries/libfc/libraries/bls12-381 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/libfc/libraries/bls12-381 b/libraries/libfc/libraries/bls12-381 index 397cfe1bb3..382cb5f57b 160000 --- a/libraries/libfc/libraries/bls12-381 +++ b/libraries/libfc/libraries/bls12-381 @@ -1 +1 @@ -Subproject commit 397cfe1bb3b8d109d26944ee083a79579c7b34a0 +Subproject commit 382cb5f57bcc32c45a3dacceed8afe219d239133