Skip to content

Commit

Permalink
GH-2286 Use const members, clean up construction
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Mar 27, 2024
1 parent 3a65b3f commit eb3e2dd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
9 changes: 5 additions & 4 deletions libraries/libfc/include/fc/crypto/bls_public_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ namespace fc::crypto::blslib {
bls_public_key() = default;
bls_public_key(bls_public_key&&) = default;
bls_public_key(const bls_public_key&) = default;
bls_public_key& operator=(const bls_public_key&) = default;
bls_public_key& operator=(bls_public_key&&) = default;

// Would prefer to not have this to enforce immutablity. Needed so keys can be copied around.
bls_public_key& operator=(const bls_public_key& rhs);

// throws if unable to convert to valid bls12_381::g1
explicit bls_public_key(std::span<const uint8_t, 96> affine_non_montgomery_le);
Expand Down Expand Up @@ -54,8 +55,8 @@ namespace fc::crypto::blslib {
friend struct fc::has_reflector_init<bls_public_key>;
void reflector_init();

std::array<uint8_t, 96> _affine_non_montgomery_le{};
bls12_381::g1 _jacobian_montgomery_le; // cached g1
const std::array<uint8_t, 96> _affine_non_montgomery_le{};
const bls12_381::g1 _jacobian_montgomery_le; // cached g1
};

} // fc::crypto::blslib
Expand Down
46 changes: 29 additions & 17 deletions libraries/libfc/src/crypto/bls_public_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,44 @@

namespace fc::crypto::blslib {

bls_public_key::bls_public_key(std::span<const uint8_t, 96> affine_non_montgomery_le) {
std::ranges::copy(affine_non_montgomery_le, _affine_non_montgomery_le.begin());
constexpr bool check = true; // verify
constexpr bool raw = false; // to montgomery
auto g1 = bls12_381::g1::fromAffineBytesLE(affine_non_montgomery_le, check, raw);
FC_ASSERT(g1, "Invalid bls_public_key");
_jacobian_montgomery_le = *g1;
}

static std::tuple<bls12_381::g1, std::array<uint8_t, 96>> pub_parse_base64url(const std::string& base64urlstr) {
inline std::array<uint8_t, 96> deserialize_base64url(const std::string& base64urlstr) {
auto res = std::mismatch(config::bls_public_key_prefix.begin(), config::bls_public_key_prefix.end(),
base64urlstr.begin());
FC_ASSERT(res.first == config::bls_public_key_prefix.end(), "BLS Public Key has invalid format : ${str}", ("str", base64urlstr));

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

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

return fc::crypto::blslib::deserialize_base64url<std::array<uint8_t, 96>>(data_str);
}

inline bls12_381::g1 from_affine_bytes_le(const std::array<uint8_t, 96>& affine_non_montgomery_le) {
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);
std::optional<bls12_381::g1> g1 = bls12_381::g1::fromAffineBytesLE(affine_non_montgomery_le, check, raw);
FC_ASSERT(g1);
return {*g1, bytes};
return *g1;
}

inline std::array<uint8_t, 96> from_span(std::span<const uint8_t, 96> affine_non_montgomery_le) {
std::array<uint8_t, 96> r;
std::ranges::copy(affine_non_montgomery_le, r.begin());
return r;
}

bls_public_key::bls_public_key(std::span<const uint8_t, 96> affine_non_montgomery_le)
: _affine_non_montgomery_le(from_span(affine_non_montgomery_le))
, _jacobian_montgomery_le(from_affine_bytes_le(_affine_non_montgomery_le)) {
}

bls_public_key::bls_public_key(const std::string& base64urlstr)
: _affine_non_montgomery_le(deserialize_base64url(base64urlstr))
, _jacobian_montgomery_le(from_affine_bytes_le(_affine_non_montgomery_le)) {
}

bls_public_key::bls_public_key(const std::string& base64urlstr) {
std::tie(_jacobian_montgomery_le, _affine_non_montgomery_le) = pub_parse_base64url(base64urlstr);
bls_public_key& bls_public_key::operator=(const bls_public_key& rhs) {
const_cast<std::array<uint8_t, 96>&>(_affine_non_montgomery_le) = rhs._affine_non_montgomery_le;
const_cast<bls12_381::g1&>(_jacobian_montgomery_le) = rhs._jacobian_montgomery_le;
return *this;
}

std::string bls_public_key::to_string() const {
Expand All @@ -45,7 +56,8 @@ namespace fc::crypto::blslib {
"FC unpack needs to call reflector_init otherwise unpacked_trx will not be initialized");
std::optional<bls12_381::g1> g1 = bls12_381::g1::fromAffineBytesLE(_affine_non_montgomery_le);
FC_ASSERT(g1, "Invalid bls public key ${k}", ("k", _affine_non_montgomery_le));
_jacobian_montgomery_le = *g1;
// reflector_init is private and only called during construction
const_cast<bls12_381::g1&>(_jacobian_montgomery_le) = *g1;
}

} // fc::crypto::blslib
Expand Down

0 comments on commit eb3e2dd

Please sign in to comment.