use elliptic for Point.multiply and key regeneration

This commit is contained in:
Ryan X. Charles 2014-07-04 13:11:52 -07:00
parent 15d4328b35
commit 8fb6ccaf01
2 changed files with 24 additions and 3 deletions

View File

@ -2,6 +2,7 @@ var ECKey = require('../../browser/vendor-bundle.js').ECKey;
var SecureRandom = require('../SecureRandom');
var Curve = require('../Curve');
var bignum = require('bignum');
var elliptic = require('elliptic');
var Key = function() {
this._pub = null;
@ -85,9 +86,17 @@ Key.prototype.regenerateSync = function() {
throw new Error('Key does not have a private key set');
}
var eck = new ECKey(this.private.toString('hex'));
eck.setCompressed(this._compressed);
this._pub = new Buffer(eck.getPub());
var ec = elliptic.curves.secp256k1;
var g = ec.g;
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;
};

View File

@ -55,4 +55,16 @@ Point.prototype.toUncompressedPubKey = function() {
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);