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

IF: change BLS key/signature encoding to base64url for easier copy/paste #2255

Merged
merged 11 commits into from
Feb 27, 2024
Merged
10 changes: 5 additions & 5 deletions libraries/libfc/include/fc/crypto/bls_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
namespace fc::crypto::blslib {

template <typename Container>
static Container deserialize_base64(const std::string& data_str)
static Container deserialize_base64url(const std::string& data_str)
{

using wrapper = checksummed_data<Container>;

wrapper wrapped;

auto bin = fc::base64_decode(data_str);
auto bin = fc::base64url_decode(data_str);
fc::datastream<const char*> unpacker(bin.data(), bin.size());
fc::raw::unpack(unpacker, wrapped);
FC_ASSERT(!unpacker.remaining(), "decoded base64 length too long");
FC_ASSERT(!unpacker.remaining(), "decoded base64url length too long");
auto checksum = wrapper::calculate_checksum(wrapped.data, nullptr);
FC_ASSERT(checksum == wrapped.check);

return wrapped.data;
}

template <typename Container>
static std::string serialize_base64( Container data) {
static std::string serialize_base64url( Container data) {

using wrapper = checksummed_data<Container>;

Expand All @@ -31,7 +31,7 @@ namespace fc::crypto::blslib {
wrapped.data = data;
wrapped.check = wrapper::calculate_checksum(wrapped.data, nullptr);
auto packed = raw::pack( wrapped );
auto data_str = fc::base64_encode( packed.data(), packed.size());
auto data_str = fc::base64url_encode( packed.data(), packed.size());

return data_str;
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/libfc/include/fc/crypto/bls_private_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace fc::crypto::blslib {
explicit bls_private_key(std::span<const uint8_t> seed ) {
_sk = bls12_381::secret_key(seed);
}
explicit bls_private_key(const std::string& base64str);
explicit bls_private_key(const std::string& base64urlstr);

bls_private_key& operator=( const bls_private_key& ) = default;

Expand Down
6 changes: 3 additions & 3 deletions libraries/libfc/include/fc/crypto/bls_public_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ 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;}
// affine non-montgomery base64 with bls_public_key_prefix
explicit bls_public_key(const std::string& base64str);
// affine non-montgomery base64url with bls_public_key_prefix
explicit bls_public_key(const std::string& base64urlstr);

bls_public_key& operator=(const bls_public_key&) = default;

// affine non-montgomery base64 with bls_public_key_prefix
// affine non-montgomery base64url with bls_public_key_prefix
std::string to_string(const yield_function_t& yield = yield_function_t()) const;

bool equal(const bls_public_key& pkey) const;
Expand Down
6 changes: 3 additions & 3 deletions libraries/libfc/include/fc/crypto/bls_signature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ namespace fc::crypto::blslib {
bls_signature( const bls_signature& ) = default;
explicit bls_signature( const bls12_381::g2& sig ){_sig = sig;}

// affine non-montgomery base64 with bls_signature_prefix
explicit bls_signature(const std::string& base64str);
// affine non-montgomery base64url with bls_signature_prefix
explicit bls_signature(const std::string& base64urlstr);

bls_signature& operator= (const bls_signature& ) = default;

// affine non-montgomery base64 with bls_signature_prefix
// affine non-montgomery base64url with bls_signature_prefix
std::string to_string(const yield_function_t& yield = yield_function_t()) const;

bool equal( const bls_signature& sig ) const;
Expand Down
16 changes: 8 additions & 8 deletions libraries/libfc/src/crypto/bls_private_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,26 @@ namespace fc::crypto::blslib {
return bls_private_key(v);
}

static std::array<uint64_t, 4> priv_parse_base64(const std::string& base64str)
static std::array<uint64_t, 4> priv_parse_base64url(const std::string& base64urlstr)
{
auto res = std::mismatch(config::bls_private_key_prefix.begin(), config::bls_private_key_prefix.end(),
base64str.begin());
FC_ASSERT(res.first == config::bls_private_key_prefix.end(), "BLS Private Key has invalid format : ${str}", ("str", base64str));
base64urlstr.begin());
FC_ASSERT(res.first == config::bls_private_key_prefix.end(), "BLS Private Key has invalid format : ${str}", ("str", base64urlstr));

auto data_str = base64str.substr(config::bls_private_key_prefix.size());
auto data_str = base64urlstr.substr(config::bls_private_key_prefix.size());

std::array<uint64_t, 4> bytes = fc::crypto::blslib::deserialize_base64<std::array<uint64_t, 4>>(data_str);
std::array<uint64_t, 4> bytes = fc::crypto::blslib::deserialize_base64url<std::array<uint64_t, 4>>(data_str);

return bytes;
}

bls_private_key::bls_private_key(const std::string& base64str)
:_sk(priv_parse_base64(base64str))
bls_private_key::bls_private_key(const std::string& base64urlstr)
:_sk(priv_parse_base64url(base64urlstr))
{}

std::string bls_private_key::to_string(const yield_function_t& yield) const
{
std::string data_str = fc::crypto::blslib::serialize_base64<std::array<uint64_t, 4>>(_sk);
std::string data_str = fc::crypto::blslib::serialize_base64url<std::array<uint64_t, 4>>(_sk);

return config::bls_private_key_prefix + data_str;
}
Expand Down
18 changes: 9 additions & 9 deletions libraries/libfc/src/crypto/bls_public_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@

namespace fc::crypto::blslib {

static bls12_381::g1 pub_parse_base64(const std::string& base64str)
static bls12_381::g1 pub_parse_base64url(const std::string& base64urlstr)
{
auto res = std::mismatch(config::bls_public_key_prefix.begin(), config::bls_public_key_prefix.end(),
base64str.begin());
FC_ASSERT(res.first == config::bls_public_key_prefix.end(), "BLS Public Key has invalid format : ${str}", ("str", base64str));
base64urlstr.begin());
FC_ASSERT(res.first == config::bls_public_key_prefix.end(), "BLS Public Key has invalid format : ${str}", ("str", base64urlstr));

auto data_str = base64str.substr(config::bls_public_key_prefix.size());
auto data_str = base64urlstr.substr(config::bls_public_key_prefix.size());

std::array<uint8_t, 96> bytes = fc::crypto::blslib::deserialize_base64<std::array<uint8_t, 96>>(data_str);
std::array<uint8_t, 96> bytes = fc::crypto::blslib::deserialize_base64url<std::array<uint8_t, 96>>(data_str);

constexpr bool check = true; // check if base64str is invalid
constexpr bool check = true; // check if base64urlstr is invalid
constexpr bool raw = false; // non-montgomery
std::optional<bls12_381::g1> g1 = bls12_381::g1::fromAffineBytesLE(bytes, check, raw);
FC_ASSERT(g1);
return *g1;
}

bls_public_key::bls_public_key(const std::string& base64str)
:_pkey(pub_parse_base64(base64str))
bls_public_key::bls_public_key(const std::string& base64urlstr)
:_pkey(pub_parse_base64url(base64urlstr))
{}

std::string bls_public_key::to_string(const yield_function_t& yield)const {

constexpr bool raw = false; // non-montgomery
std::array<uint8_t, 96> bytes = _pkey.toAffineBytesLE(raw);

std::string data_str = fc::crypto::blslib::serialize_base64<std::array<uint8_t, 96>>(bytes);
std::string data_str = fc::crypto::blslib::serialize_base64url<std::array<uint8_t, 96>>(bytes);

return config::bls_public_key_prefix + data_str;

Expand Down
20 changes: 10 additions & 10 deletions libraries/libfc/src/crypto/bls_signature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@

namespace fc::crypto::blslib {

static bls12_381::g2 sig_parse_base64(const std::string& base64str)
static bls12_381::g2 sig_parse_base64url(const std::string& base64urlstr)
{
try {

auto res = std::mismatch(config::bls_signature_prefix.begin(), config::bls_signature_prefix.end(),
base64str.begin());
FC_ASSERT(res.first == config::bls_signature_prefix.end(), "BLS Signature has invalid format : ${str}", ("str", base64str));
base64urlstr.begin());
FC_ASSERT(res.first == config::bls_signature_prefix.end(), "BLS Signature has invalid format : ${str}", ("str", base64urlstr));

auto data_str = base64str.substr(config::bls_signature_prefix.size());
auto data_str = base64urlstr.substr(config::bls_signature_prefix.size());

std::array<uint8_t, 192> bytes = fc::crypto::blslib::deserialize_base64<std::array<uint8_t, 192>>(data_str);
std::array<uint8_t, 192> bytes = fc::crypto::blslib::deserialize_base64url<std::array<uint8_t, 192>>(data_str);

constexpr bool check = true; // check if base64str is invalid
constexpr bool check = true; // check if base64urlstr is invalid
constexpr bool raw = false; // non-montgomery
std::optional<bls12_381::g2> g2 = bls12_381::g2::fromAffineBytesLE(bytes, check, raw);
FC_ASSERT(g2);
return *g2;

} FC_RETHROW_EXCEPTIONS( warn, "error parsing bls_signature", ("str", base64str ) )
} FC_RETHROW_EXCEPTIONS( warn, "error parsing bls_signature", ("str", base64urlstr ) )
}

bls_signature::bls_signature(const std::string& base64str)
:_sig(sig_parse_base64(base64str))
bls_signature::bls_signature(const std::string& base64urlstr)
:_sig(sig_parse_base64url(base64urlstr))
{}

std::string bls_signature::to_string(const yield_function_t& yield) const
Expand All @@ -36,7 +36,7 @@ namespace fc::crypto::blslib {
constexpr bool raw = false; // non-montgomery
std::array<uint8_t, 192> bytes = _sig.toAffineBytesLE(raw);

std::string data_str = fc::crypto::blslib::serialize_base64<std::array<uint8_t, 192>>(bytes);
std::string data_str = fc::crypto::blslib::serialize_base64url<std::array<uint8_t, 192>>(bytes);

return config::bls_signature_prefix + data_str;

Expand Down
Loading
Loading