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: Clean up of bls_private_key, bls_public_key and bls_signature, … #1538

Merged
merged 20 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2cfd42c
Clean up of bls_private_key, bls_public_key and bls_signature, remove…
systemzax Aug 21, 2023
4b373a0
Added wif import / export + checksum based on the fc::private_key model
systemzax Aug 21, 2023
23d049d
remove cout statements
systemzax Aug 21, 2023
a1ced9c
Added back prefixes for bls_private_key, bls_public_key and bls_signa…
systemzax Aug 23, 2023
a82c129
Renamed serialization tests in test_bls.cpp
systemzax Aug 23, 2023
5f5ebe1
Fixed serialization / deserialization and checksum, key / signatures …
systemzax Aug 25, 2023
cf2920e
Changed private key storage type from vector<uint8_t> to array<uint64…
systemzax Aug 25, 2023
3cef6f1
Removed commented code
systemzax Aug 25, 2023
fae6f9e
Merge branch 'hotstuff_integration' into bls_cleanup_test_changes
systemzax Aug 25, 2023
44f76c4
Fixed prefix verfication + various cosmetic / style improvements
systemzax Aug 25, 2023
14fc742
Added correct prefix verification for bls signature
systemzax Aug 25, 2023
0b181fb
Merge branch 'bls_cleanup_test_changes' of https://github.com/Antelop…
systemzax Aug 25, 2023
f198385
Added == operator to bls_private_key + unit test for binary construct…
systemzax Aug 25, 2023
02abeb4
Added tests for public key encoding, use std::string for key prefixes
systemzax Aug 26, 2023
8966a29
Expanded bls unit tests to include additional format + checksum tests…
systemzax Aug 27, 2023
a0650f4
Updated bls_public_key and bls_signature == operator to use the corre…
systemzax Aug 27, 2023
d169dee
Updated bls_public_key and bls_signature == operator to use the corre…
systemzax Aug 27, 2023
1a05a40
Update libraries/libfc/src/crypto/bls_signature.cpp
systemzax Aug 27, 2023
98e1ae8
Update libraries/libfc/include/fc/crypto/bls_signature.hpp
systemzax Aug 27, 2023
22aac3b
Replace constexpr with const keyword for bls_signature_prefix declara…
systemzax Aug 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
greg7mdp marked this conversation as resolved.
Show resolved Hide resolved

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);
systemzax marked this conversation as resolved.
Show resolved Hide resolved

/* 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";
constexpr std::string bls_signature_prefix = "SIG_BLS_";
};

class bls_signature
greg7mdp marked this conversation as resolved.
Show resolved Hide resolved
{
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) )
systemzax marked this conversation as resolved.
Show resolved Hide resolved
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
2 changes: 1 addition & 1 deletion libraries/libfc/libraries/bls12-381
Loading
Loading