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: Use bls12_381::pop_prove to generate POP and add get_pop_str method to libfc #1625

Merged
merged 4 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
3 changes: 2 additions & 1 deletion libraries/libfc/include/fc/crypto/bls_private_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace fc::crypto::blslib {

bls_public_key get_public_key() const;
bls_signature sign( const std::vector<uint8_t>& message ) const;
std::string get_pop_str() const;
heifner marked this conversation as resolved.
Show resolved Hide resolved
arhag marked this conversation as resolved.
Show resolved Hide resolved

static bls_private_key generate();

Expand All @@ -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) )
FC_REFLECT(crypto::blslib::bls_private_key, (_sk) )
8 changes: 8 additions & 0 deletions libraries/libfc/src/crypto/bls_private_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
arhag marked this conversation as resolved.
Show resolved Hide resolved
constexpr bool raw = true;
std::array<uint8_t, 192> bytes = proof.toAffineBytesLE(raw);
return fc::crypto::blslib::serialize_base64<std::array<uint8_t, 192>>(bytes);
dimas1185 marked this conversation as resolved.
Show resolved Hide resolved
dimas1185 marked this conversation as resolved.
Show resolved Hide resolved
}

bls_signature bls_private_key::sign( const std::vector<uint8_t>& message ) const
{
bls12_381::g2 sig = bls12_381::sign(_sk, message);
Expand Down
14 changes: 2 additions & 12 deletions programs/leap-util/actions/bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<uint8_t, 96> msg = public_key._pkey.toAffineBytesLE(true); // true means raw
const std::vector<uint8_t> msg_vector = std::vector<uint8_t>(msg.begin(), msg.end());
const bls_signature pop = private_key.sign(msg_vector);

return pop.to_string({});
}
3 changes: 1 addition & 2 deletions tests/leap_util_bls_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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...
arhag marked this conversation as resolved.
Show resolved Hide resolved
pattern = r'(\w+[^:]*): ([^\n]+)'
matched= re.findall(pattern, rslts)

Expand Down