Merge pull request #280 from ryanxcharles/feature/improve-bip32-speed

BIP32 speed improvement
This commit is contained in:
Ryan X. Charles 2014-04-22 20:06:34 -03:00
commit 2c553c0dd9
1 changed files with 40 additions and 52 deletions

View File

@ -10,6 +10,11 @@ var networks = require('../networks');
var secp256k1_n = new bignum("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16);
var secp256k1_Gx = new bignum("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16);
/*
random new BIP32: new BIP32();
from extended public or private key: new BIP32(str);
new blank BIP32: new BIP32(null);
*/
var BIP32 = function(bytes) {
if (typeof bytes == 'undefined' || bytes == 'mainnet' || bytes == 'livenet') {
bytes = 'livenet';
@ -46,7 +51,7 @@ var BIP32 = function(bytes) {
}
}
if (bytes !== undefined)
if (bytes !== undefined && bytes !== null)
this.initFromBytes(bytes);
}
@ -60,7 +65,7 @@ BIP32.seed = function(bytes, network) {
return false; //need more entropy
var hash = coinUtil.sha512hmac(bytes, new Buffer("Bitcoin seed"));
var bip32 = new BIP32();
var bip32 = new BIP32(null);
bip32.depth = 0x00;
bip32.parentFingerprint = new Buffer([0, 0, 0, 0]);
bip32.childIndex = new Buffer([0, 0, 0, 0]);
@ -136,28 +141,20 @@ BIP32.prototype.buildExtendedPublicKey = function() {
}
// Version
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, new Buffer([v >> 24])]);
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, new Buffer([(v >> 16) & 0xff])]);
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, new Buffer([(v >> 8) & 0xff])]);
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, new Buffer([v & 0xff])]);
// Depth
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, new Buffer([this.depth])]);
// Parent fingerprint
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, this.parentFingerprint]);
// Child index
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, new Buffer([this.childIndex >>> 24])]);
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, new Buffer([(this.childIndex >>> 16) & 0xff])]);
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, new Buffer([(this.childIndex >>> 8) & 0xff])]);
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, new Buffer([this.childIndex & 0xff])]);
// Chain code
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, this.chainCode]);
// Public key
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, this.eckey.public]);
this.extendedPublicKey = Buffer.concat([
new Buffer([v >> 24]),
new Buffer([(v >> 16) & 0xff]),
new Buffer([(v >> 8) & 0xff]),
new Buffer([v & 0xff]),
new Buffer([this.depth]),
this.parentFingerprint,
new Buffer([this.childIndex >>> 24]),
new Buffer([(this.childIndex >>> 16) & 0xff]),
new Buffer([(this.childIndex >>> 8) & 0xff]),
new Buffer([this.childIndex & 0xff]),
this.chainCode,
this.eckey.public
]);
}
BIP32.prototype.extendedPublicKeyString = function(format) {
@ -179,30 +176,21 @@ BIP32.prototype.buildExtendedPrivateKey = function() {
var v = this.version;
// Version
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, new Buffer([v >> 24])]);
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, new Buffer([(v >> 16) & 0xff])]);
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, new Buffer([(v >> 8) & 0xff])]);
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, new Buffer([v & 0xff])]);
// Depth
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, new Buffer([this.depth])]);
// Parent fingerprint
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, this.parentFingerprint]);
// Child index
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, new Buffer([this.childIndex >>> 24])]);
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, new Buffer([(this.childIndex >>> 16) & 0xff])]);
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, new Buffer([(this.childIndex >>> 8) & 0xff])]);
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, new Buffer([this.childIndex & 0xff])]);
// Chain code
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, this.chainCode]);
// Private key
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, new Buffer([0])]);
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, this.eckey.private]);
this.extendedPrivateKey = Buffer.concat([
new Buffer([v >> 24]),
new Buffer([(v >> 16) & 0xff]),
new Buffer([(v >> 8) & 0xff]),
new Buffer([v & 0xff]),
new Buffer([this.depth]),
this.parentFingerprint,
new Buffer([this.childIndex >>> 24]),
new Buffer([(this.childIndex >>> 16) & 0xff]),
new Buffer([(this.childIndex >>> 8) & 0xff]),
new Buffer([this.childIndex & 0xff]),
this.chainCode,
new Buffer([0]),
this.eckey.private
]);
}
BIP32.prototype.extendedPrivateKeyString = function(format) {
@ -282,7 +270,7 @@ BIP32.prototype.deriveChild = function(i) {
var priv = bignum.fromBuffer(this.eckey.private, {size: 32});
var k = il.add(priv).mod(secp256k1_n);
ret = new BIP32();
ret = new BIP32(null);
ret.chainCode = ir;
ret.eckey = new Key();
@ -293,12 +281,12 @@ BIP32.prototype.deriveChild = function(i) {
} else {
var data = Buffer.concat([this.eckey.public, ib]);
var hash = coinUtil.sha512hmac(data, this.chainCode);
var il = bignum.fromBuffer(hash.slice(0, 32), {size: 32});
var il = hash.slice(0, 32);
var ir = hash.slice(32, 64);
// Ki = (IL + kpar)*G = IL*G + Kpar
var ilGkey = new Key();
ilGkey.private = il.toBuffer({size: 32});
ilGkey.private = il;
ilGkey.regenerateSync();
var ilG = Point.fromKey(ilGkey);
var oldkey = new Key();
@ -306,7 +294,7 @@ BIP32.prototype.deriveChild = function(i) {
var Kpar = Point.fromKey(oldkey);
var newpub = Point.add(ilG, Kpar).toKey().public;
ret = new BIP32();
ret = new BIP32(null);
ret.chainCode = new Buffer(ir);
var eckey = new Key();