Skip to content

Commit

Permalink
Additional changes to support bls12 API change.
Browse files Browse the repository at this point in the history
  • Loading branch information
greg7mdp committed Apr 3, 2024
1 parent c10bcaa commit ccc79c5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
10 changes: 6 additions & 4 deletions libraries/libfc/include/fc/crypto/bls_signature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ namespace fc::crypto::blslib {

template<typename T>
friend T& operator<<(T& ds, const bls_aggregate_signature& sig) {
constexpr bool raw = false;
std::array<uint8_t, 192> affine_non_montgomery_le = sig._jacobian_montgomery_le.toAffineBytesLE(raw);
// Serialization as variable length array when it is stored as a fixed length array. This makes for easier deserialization by external tools
std::array<uint8_t, 192> affine_non_montgomery_le =
sig._jacobian_montgomery_le.toAffineBytesLE(bls12_381::from_mont::yes);
// Serialization as variable length array when it is stored as a fixed length array.
// This makes for easier deserialization by external tools
fc::raw::pack(ds, fc::unsigned_int(static_cast<uint32_t>(sizeof(affine_non_montgomery_le))));
ds.write(reinterpret_cast<const char*>(affine_non_montgomery_le.data()), sizeof(affine_non_montgomery_le));
return ds;
Expand All @@ -117,7 +118,8 @@ namespace fc::crypto::blslib {
// Could use FC_REFLECT, but to make it obvious serialization matches bls_signature implement via operator
template<typename T>
friend T& operator>>(T& ds, bls_aggregate_signature& sig) {
// Serialization as variable length array when it is stored as a fixed length array. This makes for easier deserialization by external tools
// Serialization as variable length array when it is stored as a fixed length array.
// This makes for easier deserialization by external tools
fc::unsigned_int size;
fc::raw::unpack( ds, size );
std::array<uint8_t, 192> affine_non_montgomery_le;
Expand Down
11 changes: 5 additions & 6 deletions libraries/libfc/src/crypto/bls_private_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@

namespace fc::crypto::blslib {

using from_mont = bls12_381::from_mont;

bls_public_key bls_private_key::get_public_key() const
{
bls12_381::g1 pk = bls12_381::public_key(_sk);
constexpr bool raw = false;
return bls_public_key(pk.toAffineBytesLE(raw));
return bls_public_key(pk.toAffineBytesLE(from_mont::yes));
}

bls_signature bls_private_key::proof_of_possession() const
{
bls12_381::g2 proof = bls12_381::pop_prove(_sk);
constexpr bool raw = false;
return bls_signature(proof.toAffineBytesLE(raw));
return bls_signature(proof.toAffineBytesLE(from_mont::yes));
}

bls_signature bls_private_key::sign( std::span<const uint8_t> message ) const
{
bls12_381::g2 sig = bls12_381::sign(_sk, message);
constexpr bool raw = false;
return bls_signature(sig.toAffineBytesLE(raw));
return bls_signature(sig.toAffineBytesLE(from_mont::yes));
}

bls_private_key bls_private_key::generate() {
Expand Down
5 changes: 2 additions & 3 deletions libraries/libfc/src/crypto/bls_public_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ namespace fc::crypto::blslib {
}

bls12_381::g1 bls_public_key::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(affine_non_montgomery_le, check, raw);
std::optional<bls12_381::g1> g1 =
bls12_381::g1::fromAffineBytesLE(affine_non_montgomery_le, {.check_valid = true, .to_mont = true});
FC_ASSERT(g1);
return *g1;
}
Expand Down
7 changes: 2 additions & 5 deletions libraries/libfc/src/crypto/bls_signature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
namespace fc::crypto::blslib {

bls12_381::g2 bls_signature::to_jacobian_montgomery_le(const std::array<uint8_t, 192>& affine_non_montgomery_le) {
constexpr bool check = true; // verify
constexpr bool raw = false; // to montgomery
auto g2 = bls12_381::g2::fromAffineBytesLE(affine_non_montgomery_le, check, raw);
auto g2 = bls12_381::g2::fromAffineBytesLE(affine_non_montgomery_le, {.check_valid = true, .to_mont = true});
FC_ASSERT(g2, "Invalid bls_signature");
return *g2;
}
Expand Down Expand Up @@ -48,8 +46,7 @@ namespace fc::crypto::blslib {
}

std::string bls_aggregate_signature::to_string() const {
constexpr bool raw = false;
std::array<uint8_t, 192> affine_non_montgomery_le = _jacobian_montgomery_le.toAffineBytesLE(raw);
std::array<uint8_t, 192> affine_non_montgomery_le = _jacobian_montgomery_le.toAffineBytesLE(bls12_381::from_mont::yes);
std::string data_str = fc::crypto::blslib::serialize_base64url<std::array<uint8_t, 192>>(affine_non_montgomery_le);
return config::bls_signature_prefix + data_str;
}
Expand Down
4 changes: 2 additions & 2 deletions unittests/finality_test_cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void finality_test_cluster::node1_corrupt_vote_finalizer_key() {
// corrupt the finalizer_key (manipulate so it is different)
auto g1 = node1.votes[0].finalizer_key.jacobian_montgomery_le();
g1 = bls12_381::aggregate_public_keys(std::array{g1, g1});
auto affine = g1.toAffineBytesLE(false);
auto affine = g1.toAffineBytesLE(bls12_381::from_mont::yes);
node1.votes[0].finalizer_key = fc::crypto::blslib::bls_public_key(affine);
}

Expand All @@ -128,7 +128,7 @@ void finality_test_cluster::node1_corrupt_vote_signature() {
// corrupt the signature
auto g2 = node1.votes[0].sig.jacobian_montgomery_le();
g2 = bls12_381::aggregate_signatures(std::array{g2, g2});
auto affine = g2.toAffineBytesLE(false);
auto affine = g2.toAffineBytesLE(bls12_381::from_mont::yes);
node1.votes[0].sig = fc::crypto::blslib::bls_signature(affine);
}

Expand Down

0 comments on commit ccc79c5

Please sign in to comment.