Skip to content

Commit

Permalink
Merge pull request #1538 from AntelopeIO/bls_cleanup_test_changes
Browse files Browse the repository at this point in the history
IF: Clean up of bls_private_key, bls_public_key and bls_signature, …
  • Loading branch information
heifner authored Aug 29, 2023
2 parents 5dfa47d + 22aac3b commit ddb9ac4
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 450 deletions.
42 changes: 0 additions & 42 deletions libraries/hotstuff/qc_chain.cpp
Original file line number Diff line number Diff line change
@@ -1,48 +1,6 @@
#include <eosio/hotstuff/qc_chain.hpp>

#include <fc/scoped_exit.hpp>

/*
Todo list / notes:
- fork tests in unittests
- network plugin versioning
- handshake_message.network_version
- independant of protocol feature activation
- separate library for hotstuff (look at SHIP libray used by state history plugin )
- boost tests producer plugin test
- regression tests python framework as a base
- performance testing
- complete proposer / leader differentiation
- integration with new bls implementation
- hotstuff as a library with its own tests (model on state history plugin + state_history library )
- unit / integration tests -> producer_plugin + fork_tests tests as a model
- test deterministic sequence
- test non-replica participation
- test finality vioaltion
- test loss of liveness
- test split chain
- store schedules and transition view height, and prune on commit
- integration with fork_db / LIB overhaul
- integration with performance testing
- regression testing ci/cd -> python regression tests
- implement bitset for efficiency
- add APIs for proof data
- add election proposal in block header
- map proposers / finalizers / leader to new host functions
- support pause / resume producer
- keep track of proposals sent to peers
- allow syncing of proposals
- versioning of net protocol version
- protocol feature activation HOTSTUFF_CONSENSUS
- system contract update 1
-- allow BPs to register + prove their aggregate pub key.
-- Allow existing BPs to unreg + reg without new aggregate key.
-- Prevent new BPs from registering without proving aggregate pub key
- system contract update 2 (once all or at least overwhelming majority of BPs added a bls key)
-- skip BPs without a bls key in the selection, new host functions are available
*/

namespace eosio { namespace hotstuff {

const hs_proposal_message* qc_chain::get_proposal(const fc::sha256& proposal_id) {
Expand Down
43 changes: 43 additions & 0 deletions libraries/libfc/include/fc/crypto/bls_common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once
#include <fc/crypto/common.hpp>

namespace fc::crypto::blslib {

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

using wrapper = checksummed_data<Container>;

wrapper wrapped;

auto bin = fc::from_base58(data_str);
fc::datastream<const char*> unpacker(bin.data(), bin.size());
fc::raw::unpack(unpacker, wrapped);
FC_ASSERT(!unpacker.remaining(), "decoded base58 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 deserialize_base58( Container data, const yield_function_t& yield) {

using wrapper = checksummed_data<Container>;

wrapper wrapped;

wrapped.data = data;
yield();
wrapped.check = wrapper::calculate_checksum(wrapped.data, nullptr);
yield();
auto packed = raw::pack( wrapped );
yield();
auto data_str = to_base58( packed.data(), packed.size(), yield );
yield();

return data_str;
}

} // fc::crypto::blslib
31 changes: 13 additions & 18 deletions libraries/libfc/include/fc/crypto/bls_private_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,42 @@
namespace fc::crypto::blslib {

namespace config {
constexpr const char* bls_private_key_base_prefix = "PVT";
constexpr const char* bls_private_key_prefix = "BLS";
const std::string bls_private_key_prefix = "PVT_BLS_";
};

class bls_private_key
{
public:

bls_private_key() = default;
bls_private_key( bls_private_key&& ) = default;
bls_private_key( const bls_private_key& ) = default;
bls_private_key& operator=( const bls_private_key& ) = default;
explicit bls_private_key( std::vector<uint8_t> seed ) {
_seed = std::move(seed);
explicit bls_private_key(const std::vector<uint8_t>& seed ) {
_sk = bls12_381::secret_key(seed);
}
explicit bls_private_key(const std::string& base58str);

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

// serialize to/from string
// TODO: determine format to use for string of private key
explicit bls_private_key(const string& base58str);
std::string to_string(const fc::yield_function_t& yield = fc::yield_function_t()) const;
std::string to_string(const yield_function_t& yield = yield_function_t()) const;

bls_public_key get_public_key() const;
bls_signature sign( const vector<uint8_t>& message ) const;
bls_signature sign( const std::vector<uint8_t>& message ) const;

static bls_private_key generate();

static bls_private_key regenerate( vector<uint8_t> seed ) {
return bls_private_key(std::move(seed));
}

private:
std::vector<uint8_t> _seed;

std::array<uint64_t, 4> _sk;
friend bool operator == ( const bls_private_key& pk1, const bls_private_key& pk2);
friend struct reflector<bls_private_key>;
}; // bls_private_key

} // fc::crypto::blslib

namespace fc {
void to_variant(const crypto::blslib::bls_private_key& var, variant& vo, const fc::yield_function_t& yield = fc::yield_function_t());
void to_variant(const crypto::blslib::bls_private_key& var, variant& vo, const yield_function_t& yield = yield_function_t());

void from_variant(const variant& var, crypto::blslib::bls_private_key& vo);
} // namespace fc

FC_REFLECT(fc::crypto::blslib::bls_private_key, (_seed) )
FC_REFLECT(crypto::blslib::bls_private_key, (_sk) )
55 changes: 10 additions & 45 deletions libraries/libfc/include/fc/crypto/bls_public_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@
#include <fc/static_variant.hpp>
#include <bls12-381/bls12-381.hpp>

namespace fc { namespace crypto { namespace blslib {

using namespace std;
namespace fc::crypto::blslib {

namespace config {
constexpr const char* bls_public_key_legacy_prefix = "EOS";
constexpr const char* bls_public_key_base_prefix = "PUB";
constexpr const char* bls_public_key_prefix = "BLS";

const std::string bls_public_key_prefix = "PUB_BLS_";
};

class bls_public_key
Expand All @@ -26,54 +21,24 @@ namespace fc { namespace crypto { namespace 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( bls12_381::g1 pkey ){
_pkey = pkey;
}

/* bls_public_key( G1Element pkey ){
_pkey = pkey.Serialize();
}
*/
//bls_public_key( const bls_signature& c, const sha256& digest, bool check_canonical = true );
explicit bls_public_key( const bls12_381::g1& pkey ){_pkey = pkey;}
explicit bls_public_key(const std::string& base58str);

/* bls_public_key( storage_type&& other_storage )
:_storage(forward<storage_type>(other_storage))
{}
*/
bool valid()const;

size_t which()const;

// serialize to/from string
explicit bls_public_key(const string& base58str);
//std::string to_string() const;
//std::string to_string() ;

std::string to_string(const fc::yield_function_t& yield = fc::yield_function_t()) const;
bls_public_key& operator= (const bls_public_key& ) = default;
std::string to_string(const yield_function_t& yield = yield_function_t()) const;
friend bool operator == ( const bls_public_key& p1, const bls_public_key& p2);

//storage_type _storage;
bls12_381::g1 _pkey;

private:


friend std::ostream& operator<< (std::ostream& s, const bls_public_key& k);
//friend bool operator == ( const bls_public_key& p1, const bls_public_key& p2);
//friend bool operator != ( const bls_public_key& p1, const bls_public_key& p2);
//friend bool operator < ( const bls_public_key& p1, const bls_public_key& p2);
friend struct reflector<bls_public_key>;
friend class bls_private_key;
}; // bls_public_key

} } } // fc::crypto::blslib
} // fc::crypto::blslib

namespace fc {
void to_variant(const crypto::blslib::bls_public_key& var, variant& vo, const fc::yield_function_t& yield = fc::yield_function_t());
void to_variant(const crypto::blslib::bls_public_key& var, variant& vo, const yield_function_t& yield = yield_function_t());

void from_variant(const variant& var, crypto::blslib::bls_public_key& vo);
} // namespace fc

FC_REFLECT(bls12_381::g1, (x)(y)(z))
FC_REFLECT(fc::crypto::blslib::bls_public_key, (_pkey) )
FC_REFLECT(crypto::blslib::bls_public_key, (_pkey) )
68 changes: 12 additions & 56 deletions libraries/libfc/include/fc/crypto/bls_signature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,83 +8,39 @@
#include <fc/reflect/variant.hpp>
#include <bls12-381/bls12-381.hpp>


namespace fc { namespace crypto { namespace blslib {

using namespace std;
namespace fc::crypto::blslib {

namespace config {
constexpr const char* bls_signature_base_prefix = "SIG";
constexpr const char* bls_signature_prefix = "BLS";
const std::string bls_signature_prefix = "SIG_BLS_";
};

class bls_signature
{
public:
//using storage_type = ecc::signature_shim;//std::variant<ecc::signature_shim, r1::signature_shim, webauthn::signature>;

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

bls_signature( bls12_381::g2 sig ){
_sig = sig;
}

/* bls_signature( G2Element sig ){
_sig = sig.Serialize();
}
*/
// serialize to/from string
explicit bls_signature(const string& base58str);
std::string to_string(const fc::yield_function_t& yield = fc::yield_function_t()) const;

// size_t which() const;

size_t variable_size() const;
explicit bls_signature( const bls12_381::g2& sig ){_sig = sig;}
explicit bls_signature(const std::string& base58str);

bls_signature& operator= (const bls_signature& ) = default;
std::string to_string(const yield_function_t& yield = yield_function_t()) const;
friend bool operator == ( const bls_signature& p1, const bls_signature& p2);

bls12_381::g2 _sig;

private:

//storage_type _storage;

/* bls_signature( storage_type&& other_storage )
:_storage(std::forward<storage_type>(other_storage))
{}
*/
//friend bool operator == ( const bls_signature& p1, const bls_signature& p2);
//friend bool operator != ( const bls_signature& p1, const bls_signature& p2);
//friend bool operator < ( const bls_signature& p1, const bls_signature& p2);
friend std::size_t hash_value(const bls_signature& b); //not cryptographic; for containers
friend bool operator == ( const bls_signature& p1, const bls_signature& p2);
friend struct reflector<bls_signature>;
friend class bls_private_key;
friend class bls_public_key;
}; // bls_public_key

//size_t hash_value(const bls_signature& b);
}; // bls_signature

} } } // fc::crypto::blslib
} // fc::crypto::blslib

namespace fc {
void to_variant(const crypto::blslib::bls_signature& var, variant& vo, const fc::yield_function_t& yield = fc::yield_function_t());
void to_variant(const crypto::blslib::bls_signature& var, variant& vo, const yield_function_t& yield = yield_function_t());

void from_variant(const variant& var, crypto::blslib::bls_signature& vo);
} // namespace fc

namespace std {
template <> struct hash<fc::crypto::blslib::bls_signature> {
std::size_t operator()(const fc::crypto::blslib::bls_signature& k) const {
//return fc::crypto::hash_value(k);
return 0;
}
};
} // std

FC_REFLECT(bls12_381::fp, (d))
FC_REFLECT(bls12_381::fp2, (c0)(c1))
FC_REFLECT(bls12_381::g2, (x)(y)(z))
FC_REFLECT(fc::crypto::blslib::bls_signature, (_sig) )
FC_REFLECT(crypto::blslib::bls_signature, (_sig) )
10 changes: 5 additions & 5 deletions libraries/libfc/include/fc/crypto/bls_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
namespace fc::crypto::blslib {

bool verify(const bls_public_key& pubkey,
const vector<uint8_t>& message,
const std::vector<uint8_t>& message,
const bls_signature& signature);

bls_public_key aggregate(const vector<bls_public_key>& keys);
bls_public_key aggregate(const std::vector<bls_public_key>& keys);

bls_signature aggregate(const vector<bls_signature>& signatures);
bls_signature aggregate(const std::vector<bls_signature>& signatures);

bool aggregate_verify(const vector<bls_public_key>& pubkeys,
const vector<vector<uint8_t>>& messages,
bool aggregate_verify(const std::vector<bls_public_key>& pubkeys,
const std::vector<std::vector<uint8_t>>& messages,
const bls_signature& signature);

} // fc::crypto::blslib
Loading

0 comments on commit ddb9ac4

Please sign in to comment.