Skip to content

Commit

Permalink
GH-2286 Remove const as FC_REFLECT does not support const members
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Mar 27, 2024
1 parent eb3e2dd commit aea1aa1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
12 changes: 6 additions & 6 deletions libraries/libfc/include/fc/crypto/bls_public_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace fc::crypto::blslib {
const std::string bls_public_key_prefix = "PUB_BLS_";
};

// Immutable after construction.
// Immutable after construction (although operator= is provided)
// Atributes are not const because FC_REFLECT only works for non-const members.
// Provides an efficient wrapper around bls12_381::g1.
// Serialization form:
// Non-Montgomery form and little-endian encoding for the field elements.
Expand All @@ -22,9 +23,8 @@ namespace fc::crypto::blslib {
bls_public_key() = default;
bls_public_key(bls_public_key&&) = default;
bls_public_key(const 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);
bls_public_key& operator=(const bls_public_key& rhs) = default;
bls_public_key& operator=(bls_public_key&& rhs) = default;

// 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 @@ -55,8 +55,8 @@ namespace fc::crypto::blslib {
friend struct fc::has_reflector_init<bls_public_key>;
void reflector_init();

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

} // fc::crypto::blslib
Expand Down
8 changes: 1 addition & 7 deletions libraries/libfc/src/crypto/bls_public_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ namespace fc::crypto::blslib {
, _jacobian_montgomery_le(from_affine_bytes_le(_affine_non_montgomery_le)) {
}

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 {
std::string data_str = fc::crypto::blslib::serialize_base64url<std::array<uint8_t, 96>>(_affine_non_montgomery_le);
return config::bls_public_key_prefix + data_str;
Expand All @@ -57,7 +51,7 @@ namespace fc::crypto::blslib {
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));
// reflector_init is private and only called during construction
const_cast<bls12_381::g1&>(_jacobian_montgomery_le) = *g1;
_jacobian_montgomery_le = *g1;
}

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

0 comments on commit aea1aa1

Please sign in to comment.