Skip to content

Commit

Permalink
feat: added back in private keys from BIP32 Seed (dashpay#29)
Browse files Browse the repository at this point in the history
* feat: added back in private keys from BIP32 Seed

* feat: added in unit test
  • Loading branch information
QuantumExplorer authored Mar 12, 2022
1 parent 7a690fd commit c599027
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/privatekey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,35 @@ namespace bls {

const size_t PrivateKey::PRIVATE_KEY_SIZE;

PrivateKey PrivateKey::FromSeedBIP32(const Bytes& seed) {
// "BLS private key seed" in ascii
const uint8_t hmacKey[] = {66, 76, 83, 32, 112, 114, 105, 118, 97, 116, 101,
32, 107, 101, 121, 32, 115, 101, 101, 100};

auto* hash = Util::SecAlloc<uint8_t>(
PrivateKey::PRIVATE_KEY_SIZE);

// Hash the seed into sk
md_hmac(hash, seed.begin(), (int)seed.size(), hmacKey, sizeof(hmacKey));

bn_t order;
bn_new(order);
g1_get_ord(order);

// Make sure private key is less than the curve order
bn_t* skBn = Util::SecAlloc<bn_t>(1);
bn_new(*skBn);
bn_read_bin(*skBn, hash, PrivateKey::PRIVATE_KEY_SIZE);
bn_mod_basic(*skBn, *skBn, order);

PrivateKey k;
bn_copy(k.keydata, *skBn);

Util::SecFree(skBn);
Util::SecFree(hash);
return k;
}

// Construct a private key from a bytearray.
PrivateKey PrivateKey::FromBytes(const Bytes& bytes, bool modOrder)
{
Expand Down
3 changes: 3 additions & 0 deletions src/privatekey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class PrivateKey {
// less than the group order (which is in bls.hpp).
static const size_t PRIVATE_KEY_SIZE = 32;

// Construct a private key from a BIP32 based seed.
static PrivateKey FromSeedBIP32(const Bytes& seed);

// Construct a private key from a bytearray.
static PrivateKey FromBytes(const Bytes& bytes, bool modOrder = false);

Expand Down
7 changes: 7 additions & 0 deletions src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ TEST_CASE("class PrivateKey") {
REQUIRE_THROWS(PrivateKey::FromBytes(Bytes(buffer, PrivateKey::PRIVATE_KEY_SIZE), false));
REQUIRE_NOTHROW(PrivateKey::FromBytes(Bytes(buffer, PrivateKey::PRIVATE_KEY_SIZE), true));
}
SECTION("BIP32 Seed") {
uint8_t aliceSeed[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
PrivateKey pk1 = PrivateKey::FromSeedBIP32(Bytes(aliceSeed, 10));
vector<uint8_t> privateKey = pk1.Serialize(true);
vector<uint8_t> knownPrivateKey = Util::HexToBytes("46891c2cec49593c81921e473db7480029e0fc1eb933c6b93d81f5370eb19fbd");
REQUIRE(privateKey == knownPrivateKey);
}
SECTION("keydata checks") {
PrivateKey pk1 = PrivateKey::FromByteVector(getRandomSeed(), true);
G1Element g1 = pk1.GetG1Element();
Expand Down

0 comments on commit c599027

Please sign in to comment.