Cleanups and some extra comments.

This commit is contained in:
JoelKatz
2011-12-13 12:16:54 -08:00
parent 664e1f9b7e
commit 58b727b3a7
2 changed files with 21 additions and 8 deletions

View File

@@ -8,16 +8,27 @@
#include "Serializer.h" #include "Serializer.h"
DetKeySet::DetKeySet(const std::string& phrase) 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; Serializer s;
s.addRaw((const void *) phrase.c_str(), phrase.length()); s.addRaw((const void *) phrase.c_str(), phrase.length());
mBase=s.getSHA512Half(); base=s.getSHA512Half();
s.secureErase(); s.secureErase();
} }
static EC_KEY* GenerateDeterministicKey(const uint256& base, int n) static EC_KEY* GenerateDeterministicKey(const uint256& base, int n)
{ {
BN_CTX* ctx=BN_CTX_new(); BN_CTX* ctx=BN_CTX_new();
if(!ctx) return NULL; if(!ctx) return NULL;
@@ -30,7 +41,6 @@ static EC_KEY* GenerateDeterministicKey(const uint256& base, int n)
EC_KEY_free(pkey); EC_KEY_free(pkey);
return NULL; return NULL;
} }
if(!EC_GROUP_get_order(EC_KEY_get0_group(pkey), order, ctx)) if(!EC_GROUP_get_order(EC_KEY_get0_group(pkey), order, ctx))
{ {
BN_free(order); BN_free(order);
@@ -51,12 +61,12 @@ static EC_KEY* GenerateDeterministicKey(const uint256& base, int n)
uint256 root=s.getSHA512Half(); uint256 root=s.getSHA512Half();
privKey=BN_bin2bn((const unsigned char *) &root, sizeof(root), NULL); privKey=BN_bin2bn((const unsigned char *) &root, sizeof(root), NULL);
memset(&root, 0, sizeof(root)); 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); BN_free(order);
if(!EC_KEY_set_private_key(pkey, privKey)) if(!EC_KEY_set_private_key(pkey, privKey))
{ { // set the random point as the private key
BN_free(privKey); BN_free(privKey);
BN_CTX_free(ctx); BN_CTX_free(ctx);
return NULL; 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)); 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)) if(!EC_POINT_mul(EC_KEY_get0_group(pkey), pubKey, privKey, NULL, NULL, ctx))
{ { // compute the corresponding public key point
BN_free(privKey); BN_free(privKey);
EC_POINT_free(pubKey); EC_POINT_free(pubKey);
BN_CTX_free(ctx); BN_CTX_free(ctx);
@@ -82,7 +92,6 @@ static EC_KEY* GenerateDeterministicKey(const uint256& base, int n)
BN_CTX_free(ctx); BN_CTX_free(ctx);
assert(EC_KEY_check_key(pkey)==1); assert(EC_KEY_check_key(pkey)==1);
return pkey; return pkey;
} }

View File

@@ -19,7 +19,11 @@ public:
memset(&mBase, 0, sizeof(mBase)); memset(&mBase, 0, sizeof(mBase));
} }
bool getRandom(uint256&); void reBase(const uint256& newBase) { mBase=newBase; }
uint256 getKeySetName();
static bool getRandom(uint256&);
static void getPhrase(const std::string& phrase, uint256& base);
CKey::pointer getPubKey(uint32 n); CKey::pointer getPubKey(uint32 n);
CKey::pointer getPrivKey(uint32 n); CKey::pointer getPrivKey(uint32 n);