PIVX Core  5.6.99
P2P Digital Currency
key_io.cpp
Go to the documentation of this file.
1 // Copyright (c) 2022 The PIVX Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://www.opensource.org/licenses/mit-license.php.
4 
5 #include "bls/key_io.h"
6 
7 #include "chainparams.h"
8 #include "bech32.h"
9 #include "bls/bls_wrapper.h"
10 
11 namespace bls {
12 
13 template<typename BLSKey>
14 static std::string EncodeBLS(const CChainParams& params,
15  const BLSKey& key,
17 {
18  if (!key.IsValid()) return "";
19  std::vector<unsigned char> vec{key.ToByteVector()};
20  std::vector<unsigned char> data;
21  data.reserve((vec.size() * 8 + 4) / 5);
22  ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, vec.begin(), vec.end());
23  auto res = bech32::Encode(params.Bech32HRP(type), data);
24  memory_cleanse(vec.data(), vec.size());
25  memory_cleanse(data.data(), data.size());
26  return res;
27 }
28 
29 template<typename BLSKey>
30 static Optional<BLSKey> DecodeBLS(const CChainParams& params,
31  const std::string& keyStr,
33  unsigned int keySize)
34 {
35  if (keyStr.empty()) return nullopt;
36  auto bech = bech32::Decode(keyStr);
37  if (bech.first == params.Bech32HRP(type) && bech.second.size() == keySize) {
38  std::vector<unsigned char> data;
39  data.reserve((bech.second.size() * 5) / 8);
40  if (ConvertBits<5, 8, false>([&](unsigned char c) { data.push_back(c); }, bech.second.begin(), bech.second.end())) {
41  CDataStream ss(data, SER_NETWORK, PROTOCOL_VERSION);
42  BLSKey key;
43  ss >> key;
44  if (key.IsValid()) return {key};
45  }
46  }
47  return nullopt;
48 }
49 
50 std::string EncodeSecret(const CChainParams& params, const CBLSSecretKey& key)
51 {
52  return EncodeBLS<CBLSSecretKey>(params, key, CChainParams::BLS_SECRET_KEY);
53 }
54 
55 std::string EncodePublic(const CChainParams& params, const CBLSPublicKey& pk)
56 {
57  return EncodeBLS<CBLSPublicKey>(params, pk, CChainParams::BLS_PUBLIC_KEY);
58 }
59 
60 const size_t ConvertedBlsSkSize = (BLS_CURVE_SECKEY_SIZE * 8 + 4) / 5;
61 const size_t ConvertedBlsPkSize = (BLS_CURVE_PUBKEY_SIZE * 8 + 4) / 5;
62 
63 Optional<CBLSSecretKey> DecodeSecret(const CChainParams& params, const std::string& keyStr)
64 {
65  return DecodeBLS<CBLSSecretKey>(params, keyStr, CChainParams::BLS_SECRET_KEY, ConvertedBlsSkSize);
66 }
67 
68 Optional<CBLSPublicKey> DecodePublic(const CChainParams& params, const std::string& keyStr)
69 {
70  return DecodeBLS<CBLSPublicKey>(params, keyStr, CChainParams::BLS_PUBLIC_KEY, ConvertedBlsPkSize);
71 }
72 
73 } // end bls namespace
#define BLS_CURVE_SECKEY_SIZE
Definition: bls_wrapper.h:30
#define BLS_CURVE_PUBKEY_SIZE
Definition: bls_wrapper.h:31
CChainParams defines various tweakable parameters of a given instance of the PIVX system.
Definition: chainparams.h:43
const std::string & Bech32HRP(Bech32Type type) const
Definition: chainparams.h:94
void memory_cleanse(void *ptr, size_t len)
Definition: cleanse.cpp:27
std::pair< std::string, data > Decode(const std::string &str)
Decode a Bech32 string.
Definition: bech32.cpp:158
std::string Encode(const std::string &hrp, const data &values)
Encode a Bech32 string.
Definition: bech32.cpp:143
Definition: key_io.cpp:11
const size_t ConvertedBlsPkSize
Definition: key_io.cpp:61
const size_t ConvertedBlsSkSize
Definition: key_io.cpp:60
Optional< CBLSSecretKey > DecodeSecret(const CChainParams &params, const std::string &keyStr)
Definition: key_io.cpp:63
std::string EncodeSecret(const CChainParams &params, const CBLSSecretKey &key)
Definition: key_io.cpp:50
Optional< CBLSPublicKey > DecodePublic(const CChainParams &params, const std::string &keyStr)
Definition: key_io.cpp:68
std::string EncodePublic(const CChainParams &params, const CBLSPublicKey &pk)
Definition: key_io.cpp:55
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:12
@ SER_NETWORK
Definition: serialize.h:174