Skip to content

Commit

Permalink
GH-215 Use affine non-montgomery form for proof of possesion
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Dec 5, 2023
1 parent b124284 commit 8cdc0f4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 152 deletions.
165 changes: 31 additions & 134 deletions libraries/eosiolib/core/eosio/crypto_bls_ext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <array>
#include <vector>
#include <span>

namespace bls12_381 {
class sha256 {
Expand Down Expand Up @@ -348,6 +349,7 @@ namespace detail {

return Prefix + eosio::base64_encode({g1_with_checksum.data(), g1_with_checksum.size()});
}

template<typename T, const std::string& Prefix>
T bls_base64_to_type(const char* data, size_t size) {
eosio::check(size > Prefix.size(), "encoded base64 key is too short");
Expand All @@ -370,102 +372,19 @@ namespace detail {
}

const inline std::string POP_CIPHERSUITE_ID = "BLS_POP_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_";
const inline std::vector<uint8_t> G1_ONE_NEG = {0x16, 0x0c, 0x53, 0xfd, 0x90, 0x87, 0xb3, 0x5c,
0xf5, 0xff, 0x76, 0x99, 0x67, 0xfc, 0x17, 0x78,
0xc1, 0xa1, 0x3b, 0x14, 0xc7, 0x95, 0x4f, 0x15,
0x47, 0xe7, 0xd0, 0xf3, 0xcd, 0x6a, 0xae, 0xf0,
0x40, 0xf4, 0xdb, 0x21, 0xcc, 0x6e, 0xce, 0xed,
0x75, 0xfb, 0x0b, 0x9e, 0x41, 0x77, 0x01, 0x12,
0x3a, 0x88, 0x18, 0xf3, 0x2a, 0x6c, 0x52, 0xff,
0x70, 0x02, 0x3b, 0x38, 0xe4, 0x9c, 0x89, 0x92,
0x55, 0xd0, 0xa9, 0x9f, 0x8d, 0x73, 0xd7, 0x89,
0x2a, 0xc1, 0x44, 0xa3, 0x5b, 0xf3, 0xca, 0x12,
0x17, 0x53, 0x4b, 0x96, 0x76, 0x1b, 0xff, 0x3c,
0x30, 0x44, 0x77, 0xe9, 0xed, 0xd2, 0x44, 0x0e,
0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76,
0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb,
0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f,
0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77,
0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c,
0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15}; //18x8 = 144 bytes
const inline std::vector<uint8_t> GT_ONE = {0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76,
0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb,
0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f,
0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77,
0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c,
0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // 73x8 = 584 bytes
const inline std::array<uint8_t, 48> R1 = {0xfd, 0xff, 0x02, 0x00, 0x00, 0x00, 0x09, 0x76,
0x02, 0x00, 0x0c, 0xc4, 0x0b, 0x00, 0xf4, 0xeb,
0xba, 0x58, 0xc7, 0x53, 0x57, 0x98, 0x48, 0x5f,
0x45, 0x57, 0x52, 0x70, 0x53, 0x58, 0xce, 0x77,
0x6d, 0xec, 0x56, 0xa2, 0x97, 0x1a, 0x07, 0x5c,
0x93, 0xe4, 0x80, 0xfa, 0xc3, 0x5e, 0xf6, 0x15}; // 6x8 = 48 bytes

// g1::one().negate().toAffineBytesLE(), 96 bytes
const inline std::vector<uint8_t> G1_ONE_NEG = {0xbb, 0xc6, 0x22, 0xdb, 0xa, 0xf0, 0x3a, 0xfb, 0xef, 0x1a, 0x7a, 0xf9,
0x3f, 0xe8, 0x55, 0x6c, 0x58, 0xac, 0x1b, 0x17, 0x3f, 0x3a, 0x4e, 0xa1,
0x5, 0xb9, 0x74, 0x97, 0x4f, 0x8c, 0x68, 0xc3, 0xf, 0xac, 0xa9, 0x4f,
0x8c, 0x63, 0x95, 0x26, 0x94, 0xd7, 0x97, 0x31, 0xa7, 0xd3, 0xf1, 0x17,
0xca, 0xc2, 0x39, 0xb9, 0xd6, 0xdc, 0x54, 0xad, 0x1b, 0x75, 0xcb, 0xe,
0xba, 0x38, 0x6f, 0x4e, 0x36, 0x42, 0xac, 0xca, 0xd5, 0xb9, 0x55, 0x66,
0xc9, 0x7, 0xb5, 0x1d, 0xef, 0x6a, 0x81, 0x67, 0xf2, 0x21, 0x2e, 0xcf,
0xc8, 0x76, 0x7d, 0xaa, 0xa8, 0x45, 0xd5, 0x55, 0x68, 0x1d, 0x4d, 0x11};

// fp12::one().toBytesLE(), 576 bytes
const inline std::vector<uint8_t> GT_ONE = []{ std::vector<uint8_t> r(576, 0); r[0] = 1; return r; }();

// Construct an extensible-output function based on SHA256
void xmd_sh256(
Expand Down Expand Up @@ -514,6 +433,7 @@ namespace detail {
memcpy(buf + (i - 1) * SHA256HashSize, b_i, copy_len);
}
}

bls_s scalar_fromBE(const bls_s& in) {
bls_s out;
constexpr size_t last_index = sizeof(out) - 1;
Expand All @@ -523,7 +443,8 @@ namespace detail {
}
return out;
}
void g2_fromMessage(const bls_g1_affine& msg, const std::string& dst, bls_g2& res) {

void g2_fromMessage(std::span<const char> msg, const std::string& dst, bls_g2& res) {

std::array<bls_s, 4> buf;
xmd_sh256(buf.data()->data(), sizeof(buf), msg.data(), msg.size(), dst.data(), dst.length());
Expand All @@ -549,59 +470,35 @@ namespace detail {
}
}

inline std::string encode_g1_to_bls_public_key(const bls_g1_affine& g1) {
return detail::bls_type_to_base64<bls_g1_affine, detail::bls_public_key_prefix>(g1);
inline std::string encode_g1_to_bls_public_key(const bls_g1& g1) {
return detail::bls_type_to_base64<bls_g1, detail::bls_public_key_prefix>(g1);
}
inline bls_g1_affine decode_bls_public_key_to_g1(std::string_view public_key) {
return detail::bls_base64_to_type<bls_g1_affine, detail::bls_public_key_prefix>(public_key.data(), public_key.size());
inline bls_g1 decode_bls_public_key_to_g1(std::string_view public_key) {
return detail::bls_base64_to_type<bls_g1, detail::bls_public_key_prefix>(public_key.data(), public_key.size());
}
inline std::string encode_g2_to_bls_signature(const bls_g2_affine& g2) {
return detail::bls_type_to_base64<bls_g2_affine, detail::bls_signature_prefix>(g2);
inline std::string encode_g2_to_bls_signature(const bls_g2& g2) {
return detail::bls_type_to_base64<bls_g2, detail::bls_signature_prefix>(g2);
}
inline bls_g2_affine decode_bls_signature_to_g2(std::string_view public_key) {
return detail::bls_base64_to_type<bls_g2_affine, detail::bls_signature_prefix>(public_key.data(), public_key.size());
}

inline bls_g1 g1_affine_to_jacobian(const bls_g1_affine& pubkey) {
using namespace detail;

static_assert(sizeof(bls_g1_affine) + sizeof(R1) == sizeof(bls_g1));
// add z coordinate (R1) to pubkey and signature_proof
bls_g1 pubkey_jacobian;
auto insert_it = std::copy(pubkey.begin(), pubkey.end(), pubkey_jacobian.begin());
std::copy(R1.begin(), R1.end(), insert_it);

return pubkey_jacobian;
}

inline bls_g2 g2_affine_to_jacobian(const bls_g2_affine& signature_proof) {
using namespace detail;

bls_g2 sig_ex = {0};
auto insert_it = std::copy(signature_proof.begin(), signature_proof.end(), sig_ex.begin());
std::copy(R1.begin(), R1.end(), insert_it);

return sig_ex;
inline bls_g2 decode_bls_signature_to_g2(std::string_view public_key) {
return detail::bls_base64_to_type<bls_g2, detail::bls_signature_prefix>(public_key.data(), public_key.size());
}

// pubkey and signature are assumed to be in RAW affine little-endian bytes
bool bls_pop_verify(const bls_g1_affine& pubkey, const bls_g2_affine& signature_proof) {
bool bls_pop_verify(const bls_g1& pubkey, const bls_g2& signature_proof) {
using namespace detail;

bls_g1 g1_points[2] = {0};
bls_g2 g2_points[2] = {0};

bls_g1 pubkey_ex = g1_affine_to_jacobian(pubkey);
bls_g2 sig_ex = g2_affine_to_jacobian(signature_proof);
memcpy(g1_points[0].data(), G1_ONE_NEG.data(), G1_ONE_NEG.size());
memcpy(g2_points[0].data(), signature_proof.data(), signature_proof.size());

memcpy(&g1_points[0], G1_ONE_NEG.data(), G1_ONE_NEG.size());
memcpy(&g2_points[0], sig_ex.data(), sig_ex.size());

memcpy(&g1_points[1], pubkey_ex.data(), pubkey_ex.size());
memcpy(g1_points[1].data(), pubkey.data(), pubkey.size());
g2_fromMessage(pubkey, POP_CIPHERSUITE_ID, g2_points[1]);

bls_gt r;
bls_pairing(g1_points, g2_points, 2, r);

return 0 == std::memcmp(r.data(), GT_ONE.data(), sizeof(bls_gt));
}
}
Expand Down
9 changes: 5 additions & 4 deletions tests/integration/bls_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,12 @@ BOOST_FIXTURE_TEST_CASE( sig_verify_test, bls_primitives_tester ) try {

BOOST_FIXTURE_TEST_CASE( sig_pop_verify_test, bls_primitives_tester ) try {
push_action("eosio"_n, "popverifyraw"_n, "test"_n, mvo()
("pk", "220ef5c49c1868e85b82658df9766fc2cee4bd0b5d9880d4cdee9139edd3c4ebdae4074f1d3db3f3c9213962942eef091b00d9f2a9b837015c8dbe507f242aee459272589b6b2973bcf33eb4608722c70c1ac3d1ade40c845b9e15ea6380080a")
("sig", "2f6bc758f0eec23b9218b665e72b522804c2c941dd3a18c4a6686f1f329bb4f6a0cc75ce532f366fc50962d0f58ce40f4ce009e581fe0dde6a13eff94eb041f266e8e386b1036dc866e941345b96dad688039d3859dcb767e261462c9e6d0300598567f1d3e47e3d01a81588cff6a0a8fb24aeb3d725cdb83effd1483c12780e75702508692ae64083358f42fab19d10410ccd774c25d38b4fc9066f7fc2ba00c312ef35b9ddbe4aa9b932d4a516291e2da04da856c3216d852336a672bf2a06"));
("pk", "3b08e73bbda9c3eff597fc60fbb1bd8c81af3659024f62829082438ef52a02a71f99658087ea58108e12543c08980a0f1950c7f3085ab4b49e3137e8d35adfcbed616dd37cd011435e940a74f53f99ab1d1fd220a089d4e8517df0267ecb0802")
("sig", "3a1e5689abddbd0ed54c48c88dcef7ac9bba70abdd7a8e965fb807a188cfc8a7e21ca5c9ecc58df7681d158cc6aced13d50bbe35a5aa7848c32a290b096e1f7a61b2817660bd6e6d5686180a1c00716d47ee996ced081fdb6c4417d6cc8dbd06f26f037331c1b4703e94454374ba04e71fb7571159299b9020c124e9ecee777c2b5c16a51ca883b716082fdf2e6c150551a82eaca5efaf761053d6998a439cc696366fe82eb93f19aac34893610698b37d11f0d608fdc1befc50a7e565ea4813"));

push_action("eosio"_n, "popverify"_n, "test"_n, mvo()
("pk", "PUB_BLS_2/RLYxVb3RsST5FV7kCwCsYm1ciqP4jp2l3YkN8AbaFCgzhG/yM+E6QMkLWWqgcZC7TeY6JluYSAeMrno9f8p3hsFg0S1wUyOnWISUVer4ilomRvr7uBHaRQ3QdpXxsAwOc0YQ==")
("sig", "SIG_BLS_a9pjyTpvY4/aKbOTzsi86lE23S4nzsUEJUY/IesfLwmlNHAkpCfjAtSGRlZRCfQYHIYSme3CQBW2LMFG7XsX1EUQ/L+TPvDq52TceT9o9z/iA6TBjs582dMQOmKZ8hUZUUZgzPlPDqIh0DNbTT2vVX3es6iBnHE291Yl4p4brCMLiKKQ/3kA+xvjFHykouQP5ostyMk48j4FTxdgOVNlnizUYmWmst+G65ngEqtTTUIZqc3I0OUDixGIhTNQPC8FRe+RNQ=="));
("pk", "PUB_BLS_OwjnO72pw+/1l/xg+7G9jIGvNlkCT2KCkIJDjvUqAqcfmWWAh+pYEI4SVDwImAoPGVDH8whatLSeMTfo01rfy+1hbdN80BFDXpQKdPU/masdH9IgoInU6FF98CZ+ywgCD2yuNg==")
("sig", "SIG_BLS_Oh5WiavdvQ7VTEjIjc73rJu6cKvdeo6WX7gHoYjPyKfiHKXJ7MWN92gdFYzGrO0T1Qu+NaWqeEjDKikLCW4femGygXZgvW5tVoYYChwAcW1H7pls7Qgf22xEF9bMjb0G8m8DczHBtHA+lEVDdLoE5x+3VxFZKZuQIMEk6ezud3wrXBalHKiDtxYIL98ubBUFUagurKXvr3YQU9aZikOcxpY2b+guuT8ZqsNIk2EGmLN9EfDWCP3BvvxQp+Vl6kgTpacydw=="));
} FC_LOG_AND_RETHROW()

BOOST_AUTO_TEST_SUITE_END()
Loading

0 comments on commit 8cdc0f4

Please sign in to comment.