From 58b727b3a74c67f088d591e2c93d7718c2d4a8ea Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Tue, 13 Dec 2011 12:16:54 -0800 Subject: [PATCH] Cleanups and some extra comments. --- DeterministicKeys.cpp | 23 ++++++++++++++++------- DeterministicKeys.h | 6 +++++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/DeterministicKeys.cpp b/DeterministicKeys.cpp index 77c2c2e9b3..c298050a21 100644 --- a/DeterministicKeys.cpp +++ b/DeterministicKeys.cpp @@ -8,16 +8,27 @@ #include "Serializer.h" DetKeySet::DetKeySet(const std::string& phrase) +{ // create a deterministic key set based on a phrase + getPhrase(phrase, mBase); +} + +uint256 DetKeySet::getKeySetName() +{ // get the name of a deterministic key set + Serializer s; + s.add256(mBase); + return s.getSHA512Half(); +} + +void DetKeySet::getPhrase(const std::string& phrase, uint256& base) { Serializer s; s.addRaw((const void *) phrase.c_str(), phrase.length()); - mBase=s.getSHA512Half(); + base=s.getSHA512Half(); s.secureErase(); } static EC_KEY* GenerateDeterministicKey(const uint256& base, int n) { - BN_CTX* ctx=BN_CTX_new(); if(!ctx) return NULL; @@ -30,7 +41,6 @@ static EC_KEY* GenerateDeterministicKey(const uint256& base, int n) EC_KEY_free(pkey); return NULL; } - if(!EC_GROUP_get_order(EC_KEY_get0_group(pkey), order, ctx)) { BN_free(order); @@ -51,12 +61,12 @@ static EC_KEY* GenerateDeterministicKey(const uint256& base, int n) uint256 root=s.getSHA512Half(); privKey=BN_bin2bn((const unsigned char *) &root, sizeof(root), NULL); memset(&root, 0, sizeof(root)); - } while(BN_is_zero(privKey) || (BN_cmp(privKey, order)>0)); + } while(BN_is_zero(privKey) || (BN_cmp(privKey, order)>=0)); BN_free(order); if(!EC_KEY_set_private_key(pkey, privKey)) - { + { // set the random point as the private key BN_free(privKey); BN_CTX_free(ctx); return NULL; @@ -64,7 +74,7 @@ static EC_KEY* GenerateDeterministicKey(const uint256& base, int n) EC_POINT *pubKey=EC_POINT_new(EC_KEY_get0_group(pkey)); if(!EC_POINT_mul(EC_KEY_get0_group(pkey), pubKey, privKey, NULL, NULL, ctx)) - { + { // compute the corresponding public key point BN_free(privKey); EC_POINT_free(pubKey); BN_CTX_free(ctx); @@ -82,7 +92,6 @@ static EC_KEY* GenerateDeterministicKey(const uint256& base, int n) BN_CTX_free(ctx); assert(EC_KEY_check_key(pkey)==1); - return pkey; } diff --git a/DeterministicKeys.h b/DeterministicKeys.h index 5bdf034ac4..d67cf77eb8 100644 --- a/DeterministicKeys.h +++ b/DeterministicKeys.h @@ -18,8 +18,12 @@ public: { memset(&mBase, 0, sizeof(mBase)); } + + void reBase(const uint256& newBase) { mBase=newBase; } + uint256 getKeySetName(); - bool getRandom(uint256&); + static bool getRandom(uint256&); + static void getPhrase(const std::string& phrase, uint256& base); CKey::pointer getPubKey(uint32 n); CKey::pointer getPrivKey(uint32 n);