From fe6fbc31ac764bb215c2f904cafc94708bfa7e26 Mon Sep 17 00:00:00 2001 From: Guillaume Babin-Tremblay Date: Thu, 31 Aug 2023 18:33:48 +0000 Subject: [PATCH 1/2] Changed bls private, public key / signature encoding from base58 to base64 --- .../libfc/include/fc/crypto/bls_common.hpp | 10 +-- .../include/fc/crypto/bls_private_key.hpp | 2 +- .../include/fc/crypto/bls_public_key.hpp | 2 +- .../libfc/include/fc/crypto/bls_signature.hpp | 2 +- .../libfc/src/crypto/bls_private_key.cpp | 16 ++--- libraries/libfc/src/crypto/bls_public_key.cpp | 16 ++--- libraries/libfc/src/crypto/bls_signature.cpp | 18 ++--- libraries/libfc/test/test_bls.cpp | 66 ++++++++++--------- 8 files changed, 69 insertions(+), 63 deletions(-) diff --git a/libraries/libfc/include/fc/crypto/bls_common.hpp b/libraries/libfc/include/fc/crypto/bls_common.hpp index e071c5d505..7f7b4862be 100644 --- a/libraries/libfc/include/fc/crypto/bls_common.hpp +++ b/libraries/libfc/include/fc/crypto/bls_common.hpp @@ -4,17 +4,17 @@ namespace fc::crypto::blslib { template - static Container serialize_base58(const std::string& data_str) + static Container serialize_base64(const std::string& data_str) { using wrapper = checksummed_data; wrapper wrapped; - auto bin = fc::from_base58(data_str); + auto bin = fc::base64_decode(data_str); fc::datastream unpacker(bin.data(), bin.size()); fc::raw::unpack(unpacker, wrapped); - FC_ASSERT(!unpacker.remaining(), "decoded base58 length too long"); + FC_ASSERT(!unpacker.remaining(), "decoded base64 length too long"); auto checksum = wrapper::calculate_checksum(wrapped.data, nullptr); FC_ASSERT(checksum == wrapped.check); @@ -22,7 +22,7 @@ namespace fc::crypto::blslib { } template - static std::string deserialize_base58( Container data, const yield_function_t& yield) { + static std::string deserialize_base64( Container data, const yield_function_t& yield) { using wrapper = checksummed_data; @@ -34,7 +34,7 @@ namespace fc::crypto::blslib { yield(); auto packed = raw::pack( wrapped ); yield(); - auto data_str = to_base58( packed.data(), packed.size(), yield ); + auto data_str = fc::base64_encode( packed.data(), packed.size()); yield(); return data_str; diff --git a/libraries/libfc/include/fc/crypto/bls_private_key.hpp b/libraries/libfc/include/fc/crypto/bls_private_key.hpp index deff417e85..f4d3b3e3ca 100644 --- a/libraries/libfc/include/fc/crypto/bls_private_key.hpp +++ b/libraries/libfc/include/fc/crypto/bls_private_key.hpp @@ -20,7 +20,7 @@ namespace fc::crypto::blslib { explicit bls_private_key(const std::vector& seed ) { _sk = bls12_381::secret_key(seed); } - explicit bls_private_key(const std::string& base58str); + explicit bls_private_key(const std::string& base64str); bls_private_key& operator=( const bls_private_key& ) = default; diff --git a/libraries/libfc/include/fc/crypto/bls_public_key.hpp b/libraries/libfc/include/fc/crypto/bls_public_key.hpp index b2d552888d..baaab65273 100644 --- a/libraries/libfc/include/fc/crypto/bls_public_key.hpp +++ b/libraries/libfc/include/fc/crypto/bls_public_key.hpp @@ -18,7 +18,7 @@ namespace fc::crypto::blslib { bls_public_key( bls_public_key&& ) = default; bls_public_key( const bls_public_key& ) = default; explicit bls_public_key( const bls12_381::g1& pkey ) {_pkey = pkey;} - explicit bls_public_key(const std::string& base58str); + explicit bls_public_key(const std::string& base64str); bls_public_key& operator=(const bls_public_key&) = default; std::string to_string(const yield_function_t& yield = yield_function_t()) const; diff --git a/libraries/libfc/include/fc/crypto/bls_signature.hpp b/libraries/libfc/include/fc/crypto/bls_signature.hpp index ef2b80b725..e87f2f6253 100644 --- a/libraries/libfc/include/fc/crypto/bls_signature.hpp +++ b/libraries/libfc/include/fc/crypto/bls_signature.hpp @@ -22,7 +22,7 @@ namespace fc::crypto::blslib { bls_signature( bls_signature&& ) = default; bls_signature( const bls_signature& ) = default; explicit bls_signature( const bls12_381::g2& sig ){_sig = sig;} - explicit bls_signature(const std::string& base58str); + explicit bls_signature(const std::string& base64str); bls_signature& operator= (const bls_signature& ) = default; std::string to_string(const yield_function_t& yield = yield_function_t()) const; diff --git a/libraries/libfc/src/crypto/bls_private_key.cpp b/libraries/libfc/src/crypto/bls_private_key.cpp index 1380462d05..2a9c16cdf0 100644 --- a/libraries/libfc/src/crypto/bls_private_key.cpp +++ b/libraries/libfc/src/crypto/bls_private_key.cpp @@ -25,26 +25,26 @@ namespace fc::crypto::blslib { return bls_private_key(v); } - static std::array priv_parse_base58(const std::string& base58str) + static std::array priv_parse_base64(const std::string& base64str) { auto res = std::mismatch(config::bls_private_key_prefix.begin(), config::bls_private_key_prefix.end(), - base58str.begin()); - FC_ASSERT(res.first == config::bls_private_key_prefix.end(), "BLS Private Key has invalid format : ${str}", ("str", base58str)); + base64str.begin()); + FC_ASSERT(res.first == config::bls_private_key_prefix.end(), "BLS Private Key has invalid format : ${str}", ("str", base64str)); - auto data_str = base58str.substr(config::bls_private_key_prefix.size()); + auto data_str = base64str.substr(config::bls_private_key_prefix.size()); - std::array bytes = fc::crypto::blslib::serialize_base58>(data_str); + std::array bytes = fc::crypto::blslib::serialize_base64>(data_str); return bytes; } - bls_private_key::bls_private_key(const std::string& base58str) - :_sk(priv_parse_base58(base58str)) + bls_private_key::bls_private_key(const std::string& base64str) + :_sk(priv_parse_base64(base64str)) {} std::string bls_private_key::to_string(const yield_function_t& yield) const { - std::string data_str = fc::crypto::blslib::deserialize_base58>(_sk, yield); + std::string data_str = fc::crypto::blslib::deserialize_base64>(_sk, yield); return config::bls_private_key_prefix + data_str; } diff --git a/libraries/libfc/src/crypto/bls_public_key.cpp b/libraries/libfc/src/crypto/bls_public_key.cpp index 9da57e4f7a..6777afb477 100644 --- a/libraries/libfc/src/crypto/bls_public_key.cpp +++ b/libraries/libfc/src/crypto/bls_public_key.cpp @@ -5,30 +5,30 @@ namespace fc::crypto::blslib { - static bls12_381::g1 pub_parse_base58(const std::string& base58str) + static bls12_381::g1 pub_parse_base64(const std::string& base64str) { auto res = std::mismatch(config::bls_public_key_prefix.begin(), config::bls_public_key_prefix.end(), - base58str.begin()); - FC_ASSERT(res.first == config::bls_public_key_prefix.end(), "BLS Public Key has invalid format : ${str}", ("str", base58str)); + base64str.begin()); + FC_ASSERT(res.first == config::bls_public_key_prefix.end(), "BLS Public Key has invalid format : ${str}", ("str", base64str)); - auto data_str = base58str.substr(config::bls_public_key_prefix.size()); + auto data_str = base64str.substr(config::bls_public_key_prefix.size()); - std::array bytes = fc::crypto::blslib::serialize_base58>(data_str); + std::array bytes = fc::crypto::blslib::serialize_base64>(data_str); std::optional g1 = bls12_381::g1::fromCompressedBytesBE(bytes); FC_ASSERT(g1); return *g1; } - bls_public_key::bls_public_key(const std::string& base58str) - :_pkey(pub_parse_base58(base58str)) + bls_public_key::bls_public_key(const std::string& base64str) + :_pkey(pub_parse_base64(base64str)) {} std::string bls_public_key::to_string(const yield_function_t& yield)const { std::array bytes = _pkey.toCompressedBytesBE(); - std::string data_str = fc::crypto::blslib::deserialize_base58>(bytes, yield); + std::string data_str = fc::crypto::blslib::deserialize_base64>(bytes, yield); return config::bls_public_key_prefix + data_str; diff --git a/libraries/libfc/src/crypto/bls_signature.cpp b/libraries/libfc/src/crypto/bls_signature.cpp index cb9df7298c..4fdff74174 100644 --- a/libraries/libfc/src/crypto/bls_signature.cpp +++ b/libraries/libfc/src/crypto/bls_signature.cpp @@ -5,27 +5,27 @@ namespace fc::crypto::blslib { - static bls12_381::g2 sig_parse_base58(const std::string& base58str) + static bls12_381::g2 sig_parse_base64(const std::string& base64str) { try { auto res = std::mismatch(config::bls_signature_prefix.begin(), config::bls_signature_prefix.end(), - base58str.begin()); - FC_ASSERT(res.first == config::bls_signature_prefix.end(), "BLS Signature has invalid format : ${str}", ("str", base58str)); + base64str.begin()); + FC_ASSERT(res.first == config::bls_signature_prefix.end(), "BLS Signature has invalid format : ${str}", ("str", base64str)); - auto data_str = base58str.substr(config::bls_signature_prefix.size()); + auto data_str = base64str.substr(config::bls_signature_prefix.size()); - std::array bytes = fc::crypto::blslib::serialize_base58>(data_str); + std::array bytes = fc::crypto::blslib::serialize_base64>(data_str); std::optional g2 = bls12_381::g2::fromCompressedBytesBE(bytes); FC_ASSERT(g2); return *g2; - } FC_RETHROW_EXCEPTIONS( warn, "error parsing bls_signature", ("str", base58str ) ) + } FC_RETHROW_EXCEPTIONS( warn, "error parsing bls_signature", ("str", base64str ) ) } - bls_signature::bls_signature(const std::string& base58str) - :_sig(sig_parse_base58(base58str)) + bls_signature::bls_signature(const std::string& base64str) + :_sig(sig_parse_base64(base64str)) {} std::string bls_signature::to_string(const yield_function_t& yield) const @@ -33,7 +33,7 @@ namespace fc::crypto::blslib { std::array bytes = _sig.toCompressedBytesBE(); - std::string data_str = fc::crypto::blslib::deserialize_base58>(bytes, yield); + std::string data_str = fc::crypto::blslib::deserialize_base64>(bytes, yield); return config::bls_signature_prefix + data_str; diff --git a/libraries/libfc/test/test_bls.cpp b/libraries/libfc/test/test_bls.cpp index 9aa9c27f08..ca5767da34 100644 --- a/libraries/libfc/test/test_bls.cpp +++ b/libraries/libfc/test/test_bls.cpp @@ -245,6 +245,8 @@ BOOST_AUTO_TEST_CASE(bls_binary_keys_encoding_check) try { std::string priv_str = sk.to_string(); + std::cout << priv_str << "\n"; + bool ok2 = bls_private_key(priv_str).to_string() == priv_str; bls_public_key pk = sk.get_public_key(); @@ -253,6 +255,8 @@ BOOST_AUTO_TEST_CASE(bls_binary_keys_encoding_check) try { std::string pub_str = pk.to_string(); + std::cout << pub_str << "\n"; + bool ok4 = bls_public_key(pub_str).to_string() == pub_str; bls_signature sig = sk.sign(message_1); @@ -261,6 +265,8 @@ BOOST_AUTO_TEST_CASE(bls_binary_keys_encoding_check) try { std::string sig_str = sig.to_string(); + std::cout << sig_str << "\n"; + bool ok6 = bls_signature(sig_str).to_string() == sig_str; bool ok7 = verify(pk, message_1, bls_signature(sig.to_string())); @@ -280,48 +286,48 @@ BOOST_AUTO_TEST_CASE(bls_binary_keys_encoding_check) try { BOOST_AUTO_TEST_CASE(bls_prefix_encoding_check) try { //test no_throw for correctly encoded keys - BOOST_CHECK_NO_THROW(bls_private_key("PVT_BLS_M6m7EUvzEbQErhkKUrsA96VGpdM3R3MTDszXnywcwPCt3XAcG")); - BOOST_CHECK_NO_THROW(bls_public_key("PUB_BLS_ZCYDaAqkbBChfXcFaa6QKvy3eiGuHtF3oZ9qJUqedttU9xQFESheHMjw1wEzFTXfoJaTHsu")); - BOOST_CHECK_NO_THROW(bls_signature("SIG_BLS_7dJV81MchymhckRBjZzJGPq5hySbAMrvhhWpvAou86YjhbpMuTm2RTcij1kxHuf1M1ew3PW3dVxKv8LZxntYF5c7S7TsoemqmJmnUUyGUpd8Pvs58eDREExQoHE5q2PZwaXiPVN3o")); + BOOST_CHECK_NO_THROW(bls_private_key("PVT_BLS_LaNRcYuQxSm/tRrMofQduPa5U2xUfdrCO0Yo5/CRcDeeHO+x")); + BOOST_CHECK_NO_THROW(bls_public_key("PUB_BLS_hiQykLvL/ZrnW97OeYGWU1AgjrXpmwTVzSTpVa2pYfjAoWLe50C+e9xsPAYTui6xbEYFCA==")); + BOOST_CHECK_NO_THROW(bls_signature("SIG_BLS_qn0BzfxSR4D6TK5c0MCYkX/hG4hp7NPwkEHvws4zoToZgPatfhqP8A62sEZd9gQ4FB95uVAQX04ZDj7nx85fsUdv4RtW6fxzUV2ZudfNUWRdjPX8ytXXnMEBAs6RRoF1TfiS9g==")); //test no pivot delimiter - BOOST_CHECK_THROW(bls_private_key("PVTBLSM6m7EUvzEbQErhkKUrsA96VGpdM3R3MTDszXnywcwPCt3XAcG"), fc::assert_exception); - BOOST_CHECK_THROW(bls_public_key("PUBBLSZCYDaAqkbBChfXcFaa6QKvy3eiGuHtF3oZ9qJUqedttU9xQFESheHMjw1wEzFTXfoJaTHsu"), fc::assert_exception); - BOOST_CHECK_THROW(bls_signature("SIGBLS7dJV81MchymhckRBjZzJGPq5hySbAMrvhhWpvAou86YjhbpMuTm2RTcij1kxHuf1M1ew3PW3dVxKv8LZxntYF5c7S7TsoemqmJmnUUyGUpd8Pvs58eDREExQoHE5q2PZwaXiPVN3o"), fc::assert_exception); + BOOST_CHECK_THROW(bls_private_key("PVTBLSLaNRcYuQxSm/tRrMofQduPa5U2xUfdrCO0Yo5/CRcDeeHO+x"), fc::assert_exception); + BOOST_CHECK_THROW(bls_public_key("PUBBLShiQykLvL/ZrnW97OeYGWU1AgjrXpmwTVzSTpVa2pYfjAoWLe50C+e9xsPAYTui6xbEYFCA=="), fc::assert_exception); + BOOST_CHECK_THROW(bls_signature("SIGBLSqn0BzfxSR4D6TK5c0MCYkX/hG4hp7NPwkEHvws4zoToZgPatfhqP8A62sEZd9gQ4FB95uVAQX04ZDj7nx85fsUdv4RtW6fxzUV2ZudfNUWRdjPX8ytXXnMEBAs6RRoF1TfiS9g=="), fc::assert_exception); //test first prefix validation - BOOST_CHECK_THROW(bls_private_key("XYZ_BLS_M6m7EUvzEbQErhkKUrsA96VGpdM3R3MTDszXnywcwPCt3XAcG"), fc::assert_exception); - BOOST_CHECK_THROW(bls_public_key("XYZ_BLS_ZCYDaAqkbBChfXcFaa6QKvy3eiGuHtF3oZ9qJUqedttU9xQFESheHMjw1wEzFTXfoJaTHsu"), fc::assert_exception); - BOOST_CHECK_THROW(bls_signature("XYZ_BLS_7dJV81MchymhckRBjZzJGPq5hySbAMrvhhWpvAou86YjhbpMuTm2RTcij1kxHuf1M1ew3PW3dVxKv8LZxntYF5c7S7TsoemqmJmnUUyGUpd8Pvs58eDREExQoHE5q2PZwaXiPVN3o"), fc::assert_exception); + BOOST_CHECK_THROW(bls_private_key("XYZ_BLS_LaNRcYuQxSm/tRrMofQduPa5U2xUfdrCO0Yo5/CRcDeeHO+x"), fc::assert_exception); + BOOST_CHECK_THROW(bls_public_key("XYZ_BLS_hiQykLvL/ZrnW97OeYGWU1AgjrXpmwTVzSTpVa2pYfjAoWLe50C+e9xsPAYTui6xbEYFCA=="), fc::assert_exception); + BOOST_CHECK_THROW(bls_signature("XYZ_BLS_qn0BzfxSR4D6TK5c0MCYkX/hG4hp7NPwkEHvws4zoToZgPatfhqP8A62sEZd9gQ4FB95uVAQX04ZDj7nx85fsUdv4RtW6fxzUV2ZudfNUWRdjPX8ytXXnMEBAs6RRoF1TfiS9g=="), fc::assert_exception); //test second prefix validation - BOOST_CHECK_THROW(bls_private_key("PVT_XYZ_M6m7EUvzEbQErhkKUrsA96VGpdM3R3MTDszXnywcwPCt3XAcG"), fc::assert_exception); - BOOST_CHECK_THROW(bls_public_key("PUB_XYZ_ZCYDaAqkbBChfXcFaa6QKvy3eiGuHtF3oZ9qJUqedttU9xQFESheHMjw1wEzFTXfoJaTHsu"), fc::assert_exception); - BOOST_CHECK_THROW(bls_signature("SIG_XYZ_7dJV81MchymhckRBjZzJGPq5hySbAMrvhhWpvAou86YjhbpMuTm2RTcij1kxHuf1M1ew3PW3dVxKv8LZxntYF5c7S7TsoemqmJmnUUyGUpd8Pvs58eDREExQoHE5q2PZwaXiPVN3o"), fc::assert_exception); + BOOST_CHECK_THROW(bls_private_key("PVT_XYZ_LaNRcYuQxSm/tRrMofQduPa5U2xUfdrCO0Yo5/CRcDeeHO+x"), fc::assert_exception); + BOOST_CHECK_THROW(bls_public_key("PUB_XYZ_hiQykLvL/ZrnW97OeYGWU1AgjrXpmwTVzSTpVa2pYfjAoWLe50C+e9xsPAYTui6xbEYFCA=="), fc::assert_exception); + BOOST_CHECK_THROW(bls_signature("SIG_XYZ_qn0BzfxSR4D6TK5c0MCYkX/hG4hp7NPwkEHvws4zoToZgPatfhqP8A62sEZd9gQ4FB95uVAQX04ZDj7nx85fsUdv4RtW6fxzUV2ZudfNUWRdjPX8ytXXnMEBAs6RRoF1TfiS9g=="), fc::assert_exception); //test missing prefix - BOOST_CHECK_THROW(bls_private_key("M6m7EUvzEbQErhkKUrsA96VGpdM3R3MTDszXnywcwPCt3XAcG"), fc::assert_exception); - BOOST_CHECK_THROW(bls_public_key("ZCYDaAqkbBChfXcFaa6QKvy3eiGuHtF3oZ9qJUqedttU9xQFESheHMjw1wEzFTXfoJaTHsu"), fc::assert_exception); - BOOST_CHECK_THROW(bls_signature("7dJV81MchymhckRBjZzJGPq5hySbAMrvhhWpvAou86YjhbpMuTm2RTcij1kxHuf1M1ew3PW3dVxKv8LZxntYF5c7S7TsoemqmJmnUUyGUpd8Pvs58eDREExQoHE5q2PZwaXiPVN3o"), fc::assert_exception); + BOOST_CHECK_THROW(bls_private_key("LaNRcYuQxSm/tRrMofQduPa5U2xUfdrCO0Yo5/CRcDeeHO+x"), fc::assert_exception); + BOOST_CHECK_THROW(bls_public_key("hiQykLvL/ZrnW97OeYGWU1AgjrXpmwTVzSTpVa2pYfjAoWLe50C+e9xsPAYTui6xbEYFCA=="), fc::assert_exception); + BOOST_CHECK_THROW(bls_signature("qn0BzfxSR4D6TK5c0MCYkX/hG4hp7NPwkEHvws4zoToZgPatfhqP8A62sEZd9gQ4FB95uVAQX04ZDj7nx85fsUdv4RtW6fxzUV2ZudfNUWRdjPX8ytXXnMEBAs6RRoF1TfiS9g=="), fc::assert_exception); //test incomplete prefix - BOOST_CHECK_THROW(bls_private_key("PVT_M6m7EUvzEbQErhkKUrsA96VGpdM3R3MTDszXnywcwPCt3XAcG"), fc::assert_exception); - BOOST_CHECK_THROW(bls_public_key("PUB_ZCYDaAqkbBChfXcFaa6QKvy3eiGuHtF3oZ9qJUqedttU9xQFESheHMjw1wEzFTXfoJaTHsu"), fc::assert_exception); - BOOST_CHECK_THROW(bls_signature("PUB_7dJV81MchymhckRBjZzJGPq5hySbAMrvhhWpvAou86YjhbpMuTm2RTcij1kxHuf1M1ew3PW3dVxKv8LZxntYF5c7S7TsoemqmJmnUUyGUpd8Pvs58eDREExQoHE5q2PZwaXiPVN3o"), fc::assert_exception); - BOOST_CHECK_THROW(bls_private_key("BLS_M6m7EUvzEbQErhkKUrsA96VGpdM3R3MTDszXnywcwPCt3XAcG"), fc::assert_exception); - BOOST_CHECK_THROW(bls_public_key("BLS_ZCYDaAqkbBChfXcFaa6QKvy3eiGuHtF3oZ9qJUqedttU9xQFESheHMjw1wEzFTXfoJaTHsu"), fc::assert_exception); - BOOST_CHECK_THROW(bls_signature("BLS_7dJV81MchymhckRBjZzJGPq5hySbAMrvhhWpvAou86YjhbpMuTm2RTcij1kxHuf1M1ew3PW3dVxKv8LZxntYF5c7S7TsoemqmJmnUUyGUpd8Pvs58eDREExQoHE5q2PZwaXiPVN3o"), fc::assert_exception); + BOOST_CHECK_THROW(bls_private_key("PVT_LaNRcYuQxSm/tRrMofQduPa5U2xUfdrCO0Yo5/CRcDeeHO+x"), fc::assert_exception); + BOOST_CHECK_THROW(bls_public_key("PUB_hiQykLvL/ZrnW97OeYGWU1AgjrXpmwTVzSTpVa2pYfjAoWLe50C+e9xsPAYTui6xbEYFCA=="), fc::assert_exception); + BOOST_CHECK_THROW(bls_signature("SIG_qn0BzfxSR4D6TK5c0MCYkX/hG4hp7NPwkEHvws4zoToZgPatfhqP8A62sEZd9gQ4FB95uVAQX04ZDj7nx85fsUdv4RtW6fxzUV2ZudfNUWRdjPX8ytXXnMEBAs6RRoF1TfiS9g=="), fc::assert_exception); + BOOST_CHECK_THROW(bls_private_key("BLS_LaNRcYuQxSm/tRrMofQduPa5U2xUfdrCO0Yo5/CRcDeeHO+x"), fc::assert_exception); + BOOST_CHECK_THROW(bls_public_key("BLS_hiQykLvL/ZrnW97OeYGWU1AgjrXpmwTVzSTpVa2pYfjAoWLe50C+e9xsPAYTui6xbEYFCA=="), fc::assert_exception); + BOOST_CHECK_THROW(bls_signature("BLS_qn0BzfxSR4D6TK5c0MCYkX/hG4hp7NPwkEHvws4zoToZgPatfhqP8A62sEZd9gQ4FB95uVAQX04ZDj7nx85fsUdv4RtW6fxzUV2ZudfNUWRdjPX8ytXXnMEBAs6RRoF1TfiS9g=="), fc::assert_exception); //test invalid data / invalid checksum - BOOST_CHECK_THROW(bls_private_key("PVT_BLS_M6m7EUvzEbQErhkKUrsA96VGpdM3R3MTDszXnywcwPCt3XAcH"), fc::assert_exception); - BOOST_CHECK_THROW(bls_public_key("PUB_BLS_ZCYDaAqkbBChfXcFaa6QKvy3eiGuHtF3oZ9qJUqedttU9xQFESheHMjw1wEzFTXfoJaTHsv"), fc::assert_exception); - BOOST_CHECK_THROW(bls_signature("PUB_BLS_7dJV81MchymhckRBjZzJGPq5hySbAMrvhhWpvAou86YjhbpMuTm2RTcij1kxHuf1M1ew3PW3dVxKv8LZxntYF5c7S7TsoemqmJmnUUyGUpd8Pvs58eDREExQoHE5q2PZwaXiPVN3p"), fc::assert_exception); - BOOST_CHECK_THROW(bls_private_key("PVT_BLS_N6m7EUvzEbQErhkKUrsA96VGpdM3R3MTDszXnywcwPCt3XAcG"), fc::assert_exception); - BOOST_CHECK_THROW(bls_public_key("PUB_BLS_ACYDaAqkbBChfXcFaa6QKvy3eiGuHtF3oZ9qJUqedttU9xQFESheHMjw1wEzFTXfoJaTHsu"), fc::assert_exception); - BOOST_CHECK_THROW(bls_signature("PUB_BLS_6dJV81MchymhckRBjZzJGPq5hySbAMrvhhWpvAou86YjhbpMuTm2RTcij1kxHuf1M1ew3PW3dVxKv8LZxntYF5c7S7TsoemqmJmnUUyGUpd8Pvs58eDREExQoHE5q2PZwaXiPVN3o"), fc::assert_exception); - BOOST_CHECK_THROW(bls_private_key("PVT_BLS_M6m7EUvzEbQErhkKUrsA96VGqdM3R3MTDszXnywcwPCt3XAcG"), fc::assert_exception); - BOOST_CHECK_THROW(bls_public_key("PUB_BLS_ZCYDaAqkbBChfXcFaa6QKvy3eiGuHtE3oZ9qJUqedttU9xQFESheHMjw1wEzFTXfoJaTHsu"), fc::assert_exception); - BOOST_CHECK_THROW(bls_signature("PUB_BLS_7dJV81MchymhckRBjZzJGPq5hySbAMrvhhWpvAou86YjhbpMuTm2RTcij1kxHug1M1ew3PW3dVxKv8LZxntYF5c7S7TsoemqmJmnUUyGUpd8Pvs58eDREExQoHE5q2PZwaXiPVN3o"), fc::assert_exception); + BOOST_CHECK_THROW(bls_private_key("PVT_BLS_LaNRcYuQxSm/tRrMofQduPa5U2xUfdrCO0Yo5/CRcDeeHO+y"), fc::assert_exception); + BOOST_CHECK_THROW(bls_public_key("PUB_BLS_hiQykLvL/ZrnW97OeYGWU1AgjrXpmwTVzSTpVa2pYfjAoWLe50C+e9xsPAYTui6xbEYFBA=="), fc::assert_exception); + BOOST_CHECK_THROW(bls_signature("SIG_BLS_qn0BzfxSR4D6TK5c0MCYkX/hG4hp7NPwkEHvws4zoToZgPatfhqP8A62sEZd9gQ4FB95uVAQX04ZDj7nx85fsUdv4RtW6fxzUV2ZudfNUWRdjPX8ytXXnMEBAs6RRoF1TfiS8g=="), fc::assert_exception); + BOOST_CHECK_THROW(bls_private_key("PVT_BLS_LaNRcYuQxSm/tRrMofQduPb5U2xUfdrCO0Yo5/CRcDeeHO+x"), fc::assert_exception); + BOOST_CHECK_THROW(bls_public_key("PUB_BLS_hiQykLvL/ZrnW97OeYGWU1AgjrXpnwTVzSTpVa2pYfjAoWLe50C+e9xsPAYTui6xbEYFCA=="), fc::assert_exception); + BOOST_CHECK_THROW(bls_signature("SIG_BLS_qn0BzfxSR4D6TK5c0MCYkX/hG4hp7NPwkEHvws4zoToZgPatfhqQ8A62sEZd9gQ4FB95uVAQX04ZDj7nx85fsUdv4RtW6fxzUV2ZudfNUWRdjPX8ytXXnMEBAs6RRoF1TfiS9g=="), fc::assert_exception); + BOOST_CHECK_THROW(bls_private_key("PVT_BLS_MaNRcYuQxSm/tRrMofQduPa5U2xUfdrCO0Yo5/CRcDeeHO+x"), fc::assert_exception); + BOOST_CHECK_THROW(bls_public_key("PUB_BLS_iiQykLvL/ZrnW97OeYGWU1AgjrXpmwTVzSTpVa2pYfjAoWLe50C+e9xsPAYTui6xbEYFCA=="), fc::assert_exception); + BOOST_CHECK_THROW(bls_signature("SIG_BLS_rn0BzfxSR4D6TK5c0MCYkX/hG4hp7NPwkEHvws4zoToZgPatfhqP8A62sEZd9gQ4FB95uVAQX04ZDj7nx85fsUdv4RtW6fxzUV2ZudfNUWRdjPX8ytXXnMEBAs6RRoF1TfiS9g=="), fc::assert_exception); } FC_LOG_AND_RETHROW(); From 361c1b2963305ea86c3b2eb2143b03f1cdda6f87 Mon Sep 17 00:00:00 2001 From: Guillaume Babin-Tremblay Date: Thu, 31 Aug 2023 20:07:23 +0000 Subject: [PATCH 2/2] Removed unnecessary yield function parameter, corrected semantic inversion of serialize / deserialize --- libraries/libfc/include/fc/crypto/bls_common.hpp | 10 +++------- libraries/libfc/src/crypto/bls_private_key.cpp | 4 ++-- libraries/libfc/src/crypto/bls_public_key.cpp | 4 ++-- libraries/libfc/src/crypto/bls_signature.cpp | 4 ++-- libraries/libfc/test/test_bls.cpp | 6 ------ 5 files changed, 9 insertions(+), 19 deletions(-) diff --git a/libraries/libfc/include/fc/crypto/bls_common.hpp b/libraries/libfc/include/fc/crypto/bls_common.hpp index 7f7b4862be..86e54f1e1d 100644 --- a/libraries/libfc/include/fc/crypto/bls_common.hpp +++ b/libraries/libfc/include/fc/crypto/bls_common.hpp @@ -4,7 +4,7 @@ namespace fc::crypto::blslib { template - static Container serialize_base64(const std::string& data_str) + static Container deserialize_base64(const std::string& data_str) { using wrapper = checksummed_data; @@ -22,21 +22,17 @@ namespace fc::crypto::blslib { } template - static std::string deserialize_base64( Container data, const yield_function_t& yield) { + static std::string serialize_base64( Container data) { using wrapper = checksummed_data; wrapper wrapped; wrapped.data = data; - yield(); wrapped.check = wrapper::calculate_checksum(wrapped.data, nullptr); - yield(); auto packed = raw::pack( wrapped ); - yield(); auto data_str = fc::base64_encode( packed.data(), packed.size()); - yield(); - + return data_str; } diff --git a/libraries/libfc/src/crypto/bls_private_key.cpp b/libraries/libfc/src/crypto/bls_private_key.cpp index 2a9c16cdf0..ced3d429da 100644 --- a/libraries/libfc/src/crypto/bls_private_key.cpp +++ b/libraries/libfc/src/crypto/bls_private_key.cpp @@ -33,7 +33,7 @@ namespace fc::crypto::blslib { auto data_str = base64str.substr(config::bls_private_key_prefix.size()); - std::array bytes = fc::crypto::blslib::serialize_base64>(data_str); + std::array bytes = fc::crypto::blslib::deserialize_base64>(data_str); return bytes; } @@ -44,7 +44,7 @@ namespace fc::crypto::blslib { std::string bls_private_key::to_string(const yield_function_t& yield) const { - std::string data_str = fc::crypto::blslib::deserialize_base64>(_sk, yield); + std::string data_str = fc::crypto::blslib::serialize_base64>(_sk); return config::bls_private_key_prefix + data_str; } diff --git a/libraries/libfc/src/crypto/bls_public_key.cpp b/libraries/libfc/src/crypto/bls_public_key.cpp index 6777afb477..f137a1cce6 100644 --- a/libraries/libfc/src/crypto/bls_public_key.cpp +++ b/libraries/libfc/src/crypto/bls_public_key.cpp @@ -13,7 +13,7 @@ namespace fc::crypto::blslib { auto data_str = base64str.substr(config::bls_public_key_prefix.size()); - std::array bytes = fc::crypto::blslib::serialize_base64>(data_str); + std::array bytes = fc::crypto::blslib::deserialize_base64>(data_str); std::optional g1 = bls12_381::g1::fromCompressedBytesBE(bytes); FC_ASSERT(g1); @@ -28,7 +28,7 @@ namespace fc::crypto::blslib { std::array bytes = _pkey.toCompressedBytesBE(); - std::string data_str = fc::crypto::blslib::deserialize_base64>(bytes, yield); + std::string data_str = fc::crypto::blslib::serialize_base64>(bytes); return config::bls_public_key_prefix + data_str; diff --git a/libraries/libfc/src/crypto/bls_signature.cpp b/libraries/libfc/src/crypto/bls_signature.cpp index 4fdff74174..bab437b521 100644 --- a/libraries/libfc/src/crypto/bls_signature.cpp +++ b/libraries/libfc/src/crypto/bls_signature.cpp @@ -15,7 +15,7 @@ namespace fc::crypto::blslib { auto data_str = base64str.substr(config::bls_signature_prefix.size()); - std::array bytes = fc::crypto::blslib::serialize_base64>(data_str); + std::array bytes = fc::crypto::blslib::deserialize_base64>(data_str); std::optional g2 = bls12_381::g2::fromCompressedBytesBE(bytes); FC_ASSERT(g2); @@ -33,7 +33,7 @@ namespace fc::crypto::blslib { std::array bytes = _sig.toCompressedBytesBE(); - std::string data_str = fc::crypto::blslib::deserialize_base64>(bytes, yield); + std::string data_str = fc::crypto::blslib::serialize_base64>(bytes); return config::bls_signature_prefix + data_str; diff --git a/libraries/libfc/test/test_bls.cpp b/libraries/libfc/test/test_bls.cpp index ca5767da34..790e440d90 100644 --- a/libraries/libfc/test/test_bls.cpp +++ b/libraries/libfc/test/test_bls.cpp @@ -245,8 +245,6 @@ BOOST_AUTO_TEST_CASE(bls_binary_keys_encoding_check) try { std::string priv_str = sk.to_string(); - std::cout << priv_str << "\n"; - bool ok2 = bls_private_key(priv_str).to_string() == priv_str; bls_public_key pk = sk.get_public_key(); @@ -255,8 +253,6 @@ BOOST_AUTO_TEST_CASE(bls_binary_keys_encoding_check) try { std::string pub_str = pk.to_string(); - std::cout << pub_str << "\n"; - bool ok4 = bls_public_key(pub_str).to_string() == pub_str; bls_signature sig = sk.sign(message_1); @@ -265,8 +261,6 @@ BOOST_AUTO_TEST_CASE(bls_binary_keys_encoding_check) try { std::string sig_str = sig.to_string(); - std::cout << sig_str << "\n"; - bool ok6 = bls_signature(sig_str).to_string() == sig_str; bool ok7 = verify(pk, message_1, bls_signature(sig.to_string()));