Added EC Point multiplication to browser/Point.js

This commit is contained in:
olalonde 2014-05-05 15:20:59 +08:00
parent 5f064cddec
commit e2655f553d
2 changed files with 65 additions and 0 deletions

41
lib/Armory.js Normal file
View File

@ -0,0 +1,41 @@
var Point = require('./browser/Point'),
twoSha256 = require('../util').twoSha256,
BigInteger = require('../browser/vendor-bundle.js').BigInteger;
// TODO: use native modules instead of browser libraries
/**
* For now, this class can only supports derivation from public key
* It doesn't support private key derivation (TODO).
*
* @example examples/Armory.js
*/
function Armory (chaincode, pubkey) {
this.chaincode = new Buffer(chaincode, 'hex');
this.pubkey = new Buffer(pubkey, 'hex');
}
Armory.prototype.generatePubKey = function () {
var pubKey = this.pubkey;
var chainCode = this.chaincode;
var chainXor = twoSha256(pubKey);
for (var i = 0; i < 32; i++)
chainXor[i] ^= chainCode[i];
var A = new BigInteger(chainXor.toString('hex'), 16);
var pt = Point.fromUncompressedPubKey(pubKey);
pt = Point.multiply(pt, A);
var new_pubkey = pt.toUncompressedPubKey();
return new_pubkey;
};
Armory.prototype.next = function () {
var next_pubkey = this.generatePubKey();
return new Armory(this.chaincode, next_pubkey);
};
module.exports = Armory;

View File

@ -48,6 +48,30 @@ Point.add = function(p1, p2) {
return point;
};
Point.multiply = function(p1, A) {
var ecparams = getSECCurveByName('secp256k1');
var p1xhex = p1.x.toBuffer({size: 32}).toString('hex');
var p1x = new BigInteger(p1xhex, 16);
var p1yhex = p1.y.toBuffer({size: 32}).toString('hex');
var p1y = new BigInteger(p1yhex, 16);
var p1px = new ECFieldElementFp(ecparams.getCurve().getQ(), p1x);
var p1py = new ECFieldElementFp(ecparams.getCurve().getQ(), p1y);
var p1p = new ECPointFp(ecparams.getCurve(), p1px, p1py);
var p = p1p.multiply(A);
var point = new Point();
var pointxbuf = new Buffer(p.getX().toBigInteger().toByteArrayUnsigned());
point.x = bignum.fromBuffer(pointxbuf, {size: pointxbuf.length});
assert(pointxbuf.length <= 32);
var pointybuf = new Buffer(p.getY().toBigInteger().toByteArrayUnsigned());
assert(pointybuf.length <= 32);
point.y = bignum.fromBuffer(pointybuf, {size: pointybuf.length});
return point;
};
//convert the public key of a Key into a Point
Point.fromUncompressedPubKey = function(pubkey) {
var point = new Point();