Merge pull request #280 from ryanxcharles/feature/improve-bip32-speed
BIP32 speed improvement
This commit is contained in:
commit
2c553c0dd9
92
lib/BIP32.js
92
lib/BIP32.js
|
@ -10,6 +10,11 @@ var networks = require('../networks');
|
||||||
var secp256k1_n = new bignum("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16);
|
var secp256k1_n = new bignum("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16);
|
||||||
var secp256k1_Gx = new bignum("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 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) {
|
var BIP32 = function(bytes) {
|
||||||
if (typeof bytes == 'undefined' || bytes == 'mainnet' || bytes == 'livenet') {
|
if (typeof bytes == 'undefined' || bytes == 'mainnet' || bytes == 'livenet') {
|
||||||
bytes = 'livenet';
|
bytes = 'livenet';
|
||||||
|
@ -46,7 +51,7 @@ var BIP32 = function(bytes) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bytes !== undefined)
|
if (bytes !== undefined && bytes !== null)
|
||||||
this.initFromBytes(bytes);
|
this.initFromBytes(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +65,7 @@ BIP32.seed = function(bytes, network) {
|
||||||
return false; //need more entropy
|
return false; //need more entropy
|
||||||
var hash = coinUtil.sha512hmac(bytes, new Buffer("Bitcoin seed"));
|
var hash = coinUtil.sha512hmac(bytes, new Buffer("Bitcoin seed"));
|
||||||
|
|
||||||
var bip32 = new BIP32();
|
var bip32 = new BIP32(null);
|
||||||
bip32.depth = 0x00;
|
bip32.depth = 0x00;
|
||||||
bip32.parentFingerprint = new Buffer([0, 0, 0, 0]);
|
bip32.parentFingerprint = new Buffer([0, 0, 0, 0]);
|
||||||
bip32.childIndex = new Buffer([0, 0, 0, 0]);
|
bip32.childIndex = new Buffer([0, 0, 0, 0]);
|
||||||
|
@ -136,28 +141,20 @@ BIP32.prototype.buildExtendedPublicKey = function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Version
|
// Version
|
||||||
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, new Buffer([v >> 24])]);
|
this.extendedPublicKey = Buffer.concat([
|
||||||
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, new Buffer([(v >> 16) & 0xff])]);
|
new Buffer([v >> 24]),
|
||||||
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, new Buffer([(v >> 8) & 0xff])]);
|
new Buffer([(v >> 16) & 0xff]),
|
||||||
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, new Buffer([v & 0xff])]);
|
new Buffer([(v >> 8) & 0xff]),
|
||||||
|
new Buffer([v & 0xff]),
|
||||||
// Depth
|
new Buffer([this.depth]),
|
||||||
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, new Buffer([this.depth])]);
|
this.parentFingerprint,
|
||||||
|
new Buffer([this.childIndex >>> 24]),
|
||||||
// Parent fingerprint
|
new Buffer([(this.childIndex >>> 16) & 0xff]),
|
||||||
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, this.parentFingerprint]);
|
new Buffer([(this.childIndex >>> 8) & 0xff]),
|
||||||
|
new Buffer([this.childIndex & 0xff]),
|
||||||
// Child index
|
this.chainCode,
|
||||||
this.extendedPublicKey = Buffer.concat([this.extendedPublicKey, new Buffer([this.childIndex >>> 24])]);
|
this.eckey.public
|
||||||
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]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BIP32.prototype.extendedPublicKeyString = function(format) {
|
BIP32.prototype.extendedPublicKeyString = function(format) {
|
||||||
|
@ -179,30 +176,21 @@ BIP32.prototype.buildExtendedPrivateKey = function() {
|
||||||
|
|
||||||
var v = this.version;
|
var v = this.version;
|
||||||
|
|
||||||
// Version
|
this.extendedPrivateKey = Buffer.concat([
|
||||||
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, new Buffer([v >> 24])]);
|
new Buffer([v >> 24]),
|
||||||
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, new Buffer([(v >> 16) & 0xff])]);
|
new Buffer([(v >> 16) & 0xff]),
|
||||||
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, new Buffer([(v >> 8) & 0xff])]);
|
new Buffer([(v >> 8) & 0xff]),
|
||||||
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, new Buffer([v & 0xff])]);
|
new Buffer([v & 0xff]),
|
||||||
|
new Buffer([this.depth]),
|
||||||
// Depth
|
this.parentFingerprint,
|
||||||
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, new Buffer([this.depth])]);
|
new Buffer([this.childIndex >>> 24]),
|
||||||
|
new Buffer([(this.childIndex >>> 16) & 0xff]),
|
||||||
// Parent fingerprint
|
new Buffer([(this.childIndex >>> 8) & 0xff]),
|
||||||
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, this.parentFingerprint]);
|
new Buffer([this.childIndex & 0xff]),
|
||||||
|
this.chainCode,
|
||||||
// Child index
|
new Buffer([0]),
|
||||||
this.extendedPrivateKey = Buffer.concat([this.extendedPrivateKey, new Buffer([this.childIndex >>> 24])]);
|
this.eckey.private
|
||||||
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]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BIP32.prototype.extendedPrivateKeyString = function(format) {
|
BIP32.prototype.extendedPrivateKeyString = function(format) {
|
||||||
|
@ -282,7 +270,7 @@ BIP32.prototype.deriveChild = function(i) {
|
||||||
var priv = bignum.fromBuffer(this.eckey.private, {size: 32});
|
var priv = bignum.fromBuffer(this.eckey.private, {size: 32});
|
||||||
var k = il.add(priv).mod(secp256k1_n);
|
var k = il.add(priv).mod(secp256k1_n);
|
||||||
|
|
||||||
ret = new BIP32();
|
ret = new BIP32(null);
|
||||||
ret.chainCode = ir;
|
ret.chainCode = ir;
|
||||||
|
|
||||||
ret.eckey = new Key();
|
ret.eckey = new Key();
|
||||||
|
@ -293,12 +281,12 @@ BIP32.prototype.deriveChild = function(i) {
|
||||||
} else {
|
} else {
|
||||||
var data = Buffer.concat([this.eckey.public, ib]);
|
var data = Buffer.concat([this.eckey.public, ib]);
|
||||||
var hash = coinUtil.sha512hmac(data, this.chainCode);
|
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);
|
var ir = hash.slice(32, 64);
|
||||||
|
|
||||||
// Ki = (IL + kpar)*G = IL*G + Kpar
|
// Ki = (IL + kpar)*G = IL*G + Kpar
|
||||||
var ilGkey = new Key();
|
var ilGkey = new Key();
|
||||||
ilGkey.private = il.toBuffer({size: 32});
|
ilGkey.private = il;
|
||||||
ilGkey.regenerateSync();
|
ilGkey.regenerateSync();
|
||||||
var ilG = Point.fromKey(ilGkey);
|
var ilG = Point.fromKey(ilGkey);
|
||||||
var oldkey = new Key();
|
var oldkey = new Key();
|
||||||
|
@ -306,7 +294,7 @@ BIP32.prototype.deriveChild = function(i) {
|
||||||
var Kpar = Point.fromKey(oldkey);
|
var Kpar = Point.fromKey(oldkey);
|
||||||
var newpub = Point.add(ilG, Kpar).toKey().public;
|
var newpub = Point.add(ilG, Kpar).toKey().public;
|
||||||
|
|
||||||
ret = new BIP32();
|
ret = new BIP32(null);
|
||||||
ret.chainCode = new Buffer(ir);
|
ret.chainCode = new Buffer(ir);
|
||||||
|
|
||||||
var eckey = new Key();
|
var eckey = new Key();
|
||||||
|
|
Loading…
Reference in New Issue