use elliptic for Point.multiply and key regeneration
This commit is contained in:
parent
15d4328b35
commit
8fb6ccaf01
|
@ -2,6 +2,7 @@ var ECKey = require('../../browser/vendor-bundle.js').ECKey;
|
||||||
var SecureRandom = require('../SecureRandom');
|
var SecureRandom = require('../SecureRandom');
|
||||||
var Curve = require('../Curve');
|
var Curve = require('../Curve');
|
||||||
var bignum = require('bignum');
|
var bignum = require('bignum');
|
||||||
|
var elliptic = require('elliptic');
|
||||||
|
|
||||||
var Key = function() {
|
var Key = function() {
|
||||||
this._pub = null;
|
this._pub = null;
|
||||||
|
@ -85,9 +86,17 @@ Key.prototype.regenerateSync = function() {
|
||||||
throw new Error('Key does not have a private key set');
|
throw new Error('Key does not have a private key set');
|
||||||
}
|
}
|
||||||
|
|
||||||
var eck = new ECKey(this.private.toString('hex'));
|
var ec = elliptic.curves.secp256k1;
|
||||||
eck.setCompressed(this._compressed);
|
var g = ec.g;
|
||||||
this._pub = new Buffer(eck.getPub());
|
var ecp = ec.g.mul(this.private);
|
||||||
|
var x = new bignum(ecp.x.toArray());
|
||||||
|
var y = new bignum(ecp.y.toArray());
|
||||||
|
var p = new Point(x, y);
|
||||||
|
if (this.compressed)
|
||||||
|
this._pub = p.toCompressedPubKey();
|
||||||
|
else
|
||||||
|
this._pub = p.toUncompressedPubKey();
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -55,4 +55,16 @@ Point.prototype.toUncompressedPubKey = function() {
|
||||||
return pub;
|
return pub;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Point.prototype.toCompressedPubKey = function() {
|
||||||
|
var xbuf = this.x.toBuffer({size: 32});
|
||||||
|
var ybuf = this.y.toBuffer({size: 32});
|
||||||
|
if (ybuf[ybuf.length-1] % 2) { //odd
|
||||||
|
var pub = Buffer.concat([new Buffer([3]), xbuf]);
|
||||||
|
}
|
||||||
|
else { //even
|
||||||
|
var pub = Buffer.concat([new Buffer([2]), xbuf]);
|
||||||
|
}
|
||||||
|
return pub;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = (Point);
|
module.exports = (Point);
|
||||||
|
|
Loading…
Reference in New Issue