From 350191572a68616a9ed20d23a49895bd87275305 Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Tue, 12 Sep 2023 17:24:06 -0400 Subject: [PATCH 1/4] Use bls12_381::pop_prove to generate POP and add get_pop_str method to libfc --- .../libfc/include/fc/crypto/bls_private_key.hpp | 3 ++- libraries/libfc/src/crypto/bls_private_key.cpp | 8 ++++++++ programs/leap-util/actions/bls.cpp | 14 ++------------ tests/leap_util_bls_test.py | 3 +-- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/libraries/libfc/include/fc/crypto/bls_private_key.hpp b/libraries/libfc/include/fc/crypto/bls_private_key.hpp index f4d3b3e3ca..98799b42c5 100644 --- a/libraries/libfc/include/fc/crypto/bls_private_key.hpp +++ b/libraries/libfc/include/fc/crypto/bls_private_key.hpp @@ -28,6 +28,7 @@ namespace fc::crypto::blslib { bls_public_key get_public_key() const; bls_signature sign( const std::vector& message ) const; + std::string get_pop_str() const; static bls_private_key generate(); @@ -45,4 +46,4 @@ namespace fc { void from_variant(const variant& var, crypto::blslib::bls_private_key& vo); } // namespace fc -FC_REFLECT(crypto::blslib::bls_private_key, (_sk) ) \ No newline at end of file +FC_REFLECT(crypto::blslib::bls_private_key, (_sk) ) diff --git a/libraries/libfc/src/crypto/bls_private_key.cpp b/libraries/libfc/src/crypto/bls_private_key.cpp index ced3d429da..27db3c9baf 100644 --- a/libraries/libfc/src/crypto/bls_private_key.cpp +++ b/libraries/libfc/src/crypto/bls_private_key.cpp @@ -13,6 +13,14 @@ namespace fc::crypto::blslib { return bls_public_key(pk); } + std::string bls_private_key::get_pop_str() const + { + bls12_381::g2 proof = bls12_381::pop_prove(_sk); + constexpr bool raw = true; + std::array bytes = proof.toAffineBytesLE(raw); + return fc::crypto::blslib::serialize_base64>(bytes); + } + bls_signature bls_private_key::sign( const std::vector& message ) const { bls12_381::g2 sig = bls12_381::sign(_sk, message); diff --git a/programs/leap-util/actions/bls.cpp b/programs/leap-util/actions/bls.cpp index 4ed94834b7..abff8fe624 100644 --- a/programs/leap-util/actions/bls.cpp +++ b/programs/leap-util/actions/bls.cpp @@ -55,7 +55,7 @@ int bls_actions::create_key() { const bls_public_key public_key = private_key.get_public_key(); // generate pop - const std::string pop_str = generate_pop_str(private_key); + const std::string pop_str = private_key.get_pop_str(); // prepare output std::string out_str = "Private key: " + private_key.to_string({}) + "\n"; @@ -106,20 +106,10 @@ int bls_actions::create_pop() { // create private key object using input private key string const bls_private_key private_key = bls_private_key(private_key_str); const bls_public_key public_key = private_key.get_public_key(); - std::string pop_str = generate_pop_str(private_key); + std::string pop_str = private_key.get_pop_str(); std::cout << "Proof of Possession: " << pop_str << "\n"; std::cout << "Public key: " << public_key.to_string({}) << "\n"; return 0; } - -std::string bls_actions::generate_pop_str(const bls_private_key& private_key) { - const bls_public_key public_key = private_key.get_public_key(); - - const std::array msg = public_key._pkey.toAffineBytesLE(true); // true means raw - const std::vector msg_vector = std::vector(msg.begin(), msg.end()); - const bls_signature pop = private_key.sign(msg_vector); - - return pop.to_string({}); -} diff --git a/tests/leap_util_bls_test.py b/tests/leap_util_bls_test.py index e4a2e28f99..6c07cb7787 100755 --- a/tests/leap_util_bls_test.py +++ b/tests/leap_util_bls_test.py @@ -100,13 +100,12 @@ def check_create_key_results(rslts): # check each output has valid value assert "PVT_BLS_" in results["Private key"] assert "PUB_BLS_" in results["Public key"] - assert "SIG_BLS_" in results["Proof of Possession"] def get_results(rslts): # sample output looks like # Private key: PVT_BLS_kRhJJ2MsM+/CddO... # Public key: PUB_BLS_lbUE8922wUfX0Iy5... - # Proof of Possession: SIG_BLS_olZfcFw... + # Proof of Possession: 3jwkVUUYahHgsnmnEA... pattern = r'(\w+[^:]*): ([^\n]+)' matched= re.findall(pattern, rslts) From 1fae38ec5862e01f565da310b425f899746dd642 Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Tue, 12 Sep 2023 18:35:24 -0400 Subject: [PATCH 2/4] incorporate review comments to simplify the code --- libraries/libfc/include/fc/crypto/bls_private_key.hpp | 4 +++- libraries/libfc/src/crypto/bls_private_key.cpp | 6 ++---- programs/leap-util/actions/bls.cpp | 10 +++++----- tests/leap_util_bls_test.py | 3 ++- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/libraries/libfc/include/fc/crypto/bls_private_key.hpp b/libraries/libfc/include/fc/crypto/bls_private_key.hpp index 98799b42c5..c3cd2004ac 100644 --- a/libraries/libfc/include/fc/crypto/bls_private_key.hpp +++ b/libraries/libfc/include/fc/crypto/bls_private_key.hpp @@ -28,7 +28,9 @@ namespace fc::crypto::blslib { bls_public_key get_public_key() const; bls_signature sign( const std::vector& message ) const; - std::string get_pop_str() const; + + // Returns proof of possession + bls_signature pop_proof() const; static bls_private_key generate(); diff --git a/libraries/libfc/src/crypto/bls_private_key.cpp b/libraries/libfc/src/crypto/bls_private_key.cpp index 27db3c9baf..3fc4092967 100644 --- a/libraries/libfc/src/crypto/bls_private_key.cpp +++ b/libraries/libfc/src/crypto/bls_private_key.cpp @@ -13,12 +13,10 @@ namespace fc::crypto::blslib { return bls_public_key(pk); } - std::string bls_private_key::get_pop_str() const + bls_signature bls_private_key::pop_proof() const { bls12_381::g2 proof = bls12_381::pop_prove(_sk); - constexpr bool raw = true; - std::array bytes = proof.toAffineBytesLE(raw); - return fc::crypto::blslib::serialize_base64>(bytes); + return bls_signature(proof); } bls_signature bls_private_key::sign( const std::vector& message ) const diff --git a/programs/leap-util/actions/bls.cpp b/programs/leap-util/actions/bls.cpp index abff8fe624..4d43894aed 100644 --- a/programs/leap-util/actions/bls.cpp +++ b/programs/leap-util/actions/bls.cpp @@ -54,13 +54,13 @@ int bls_actions::create_key() { const bls_private_key private_key = bls_private_key::generate(); const bls_public_key public_key = private_key.get_public_key(); - // generate pop - const std::string pop_str = private_key.get_pop_str(); + // generate proof of possession + const bls_signature pop = private_key.pop_proof(); // prepare output std::string out_str = "Private key: " + private_key.to_string({}) + "\n"; out_str += "Public key: " + public_key.to_string({}) + "\n"; - out_str += "Proof of Possession: " + pop_str + "\n"; + out_str += "Proof of Possession: " + pop.to_string({}) + "\n"; if (opt->print_console) { std::cout << out_str; } else { @@ -106,9 +106,9 @@ int bls_actions::create_pop() { // create private key object using input private key string const bls_private_key private_key = bls_private_key(private_key_str); const bls_public_key public_key = private_key.get_public_key(); - std::string pop_str = private_key.get_pop_str(); + const bls_signature pop = private_key.pop_proof(); - std::cout << "Proof of Possession: " << pop_str << "\n"; + std::cout << "Proof of Possession: " << pop.to_string({})<< "\n"; std::cout << "Public key: " << public_key.to_string({}) << "\n"; return 0; diff --git a/tests/leap_util_bls_test.py b/tests/leap_util_bls_test.py index 6c07cb7787..fb7c77037f 100755 --- a/tests/leap_util_bls_test.py +++ b/tests/leap_util_bls_test.py @@ -100,12 +100,13 @@ def check_create_key_results(rslts): # check each output has valid value assert "PVT_BLS_" in results["Private key"] assert "PUB_BLS_" in results["Public key"] + assert "SIG_BLS_" in results["Proof of Possession"] def get_results(rslts): # sample output looks like # Private key: PVT_BLS_kRhJJ2MsM+/CddO... # Public key: PUB_BLS_lbUE8922wUfX0Iy5... - # Proof of Possession: 3jwkVUUYahHgsnmnEA... + # Proof of Possession: SIG_BLS_3jwkVUUYahHgsnmnEA... pattern = r'(\w+[^:]*): ([^\n]+)' matched= re.findall(pattern, rslts) From 375504bb4d3f9f0d0ad286662da261babf6ee61b Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Tue, 12 Sep 2023 20:50:55 -0400 Subject: [PATCH 3/4] Bump bls12-381 to the head of main --- libraries/libfc/libraries/bls12-381 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/libfc/libraries/bls12-381 b/libraries/libfc/libraries/bls12-381 index d79a183664..5b18ef4f8a 160000 --- a/libraries/libfc/libraries/bls12-381 +++ b/libraries/libfc/libraries/bls12-381 @@ -1 +1 @@ -Subproject commit d79a1836649d6f9b7494ccc3e9a1320a7470acaa +Subproject commit 5b18ef4f8add17d17aa8fe9233aa363829aca90a From e30d1a62491250f94828580cf89673da284e3425 Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Tue, 12 Sep 2023 20:52:16 -0400 Subject: [PATCH 4/4] Change pop_proof() to proof_of_possession() --- libraries/libfc/include/fc/crypto/bls_private_key.hpp | 4 +--- libraries/libfc/src/crypto/bls_private_key.cpp | 2 +- programs/leap-util/actions/bls.cpp | 4 ++-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/libraries/libfc/include/fc/crypto/bls_private_key.hpp b/libraries/libfc/include/fc/crypto/bls_private_key.hpp index c3cd2004ac..71f1e8bae8 100644 --- a/libraries/libfc/include/fc/crypto/bls_private_key.hpp +++ b/libraries/libfc/include/fc/crypto/bls_private_key.hpp @@ -28,9 +28,7 @@ namespace fc::crypto::blslib { bls_public_key get_public_key() const; bls_signature sign( const std::vector& message ) const; - - // Returns proof of possession - bls_signature pop_proof() const; + bls_signature proof_of_possession() const; static bls_private_key generate(); diff --git a/libraries/libfc/src/crypto/bls_private_key.cpp b/libraries/libfc/src/crypto/bls_private_key.cpp index 3fc4092967..95da8abdfa 100644 --- a/libraries/libfc/src/crypto/bls_private_key.cpp +++ b/libraries/libfc/src/crypto/bls_private_key.cpp @@ -13,7 +13,7 @@ namespace fc::crypto::blslib { return bls_public_key(pk); } - bls_signature bls_private_key::pop_proof() const + bls_signature bls_private_key::proof_of_possession() const { bls12_381::g2 proof = bls12_381::pop_prove(_sk); return bls_signature(proof); diff --git a/programs/leap-util/actions/bls.cpp b/programs/leap-util/actions/bls.cpp index 4d43894aed..972de6db89 100644 --- a/programs/leap-util/actions/bls.cpp +++ b/programs/leap-util/actions/bls.cpp @@ -55,7 +55,7 @@ int bls_actions::create_key() { const bls_public_key public_key = private_key.get_public_key(); // generate proof of possession - const bls_signature pop = private_key.pop_proof(); + const bls_signature pop = private_key.proof_of_possession(); // prepare output std::string out_str = "Private key: " + private_key.to_string({}) + "\n"; @@ -106,7 +106,7 @@ int bls_actions::create_pop() { // create private key object using input private key string const bls_private_key private_key = bls_private_key(private_key_str); const bls_public_key public_key = private_key.get_public_key(); - const bls_signature pop = private_key.pop_proof(); + const bls_signature pop = private_key.proof_of_possession(); std::cout << "Proof of Possession: " << pop.to_string({})<< "\n"; std::cout << "Public key: " << public_key.to_string({}) << "\n";