This commit is contained in:
Ryan X. Charles 2014-08-06 21:02:42 -07:00
parent ca4131ab05
commit 657f992e7b
3 changed files with 125 additions and 1 deletions

View File

@ -13,9 +13,9 @@ privsec.base58check = require('./lib/base58check');
privsec.bn = require('./lib/bn');
privsec.constants = require('./lib/constants');
privsec.hash = require('./lib/hash');
privsec.point = require('./lib/point');
//privsec.key = require('lib/key');
//privsec.point = require('lib/point');
//privsec.privkey = require('lib/privkey');
//privsec.pubkey = require('lib/pubkey');
//privsec.script = require('lib/script');

32
lib/point.js Normal file
View File

@ -0,0 +1,32 @@
var bn = require('./bn');
var elliptic = require('elliptic');
var ec = elliptic.curves.secp256k1;
var Point = ec.curve.point.bind(ec.curve)
var p = ec.curve.point();
var Curve = Object.getPrototypeOf(ec.curve);
Point.prototype = Object.getPrototypeOf(p);
Point.pointFromX = ec.curve.pointFromX.bind(ec.curve);
Point.getG = function() {
var p = Point(ec.curve.g.getX(), ec.curve.g.getY());
return p;
};
Point.getN = function() {
return bn(ec.curve.n.toArray());
};
Point.prototype._getX = Point.prototype.getX;
Point.prototype.getX = function() {
var n = bn(this._getX().toArray());
return bn(this._getX().toArray());
};
Point.prototype._getY = Point.prototype.getY;
Point.prototype.getY = function() {
return bn(this._getY().toArray());
};
module.exports = Point;

92
test/test.point.js Normal file
View File

@ -0,0 +1,92 @@
var should = require('chai').should();
var point = require('../lib/point');
var bn = require('../lib/bn');
describe('point', function() {
it('should create a point', function() {
var p = point();
should.exist(p);
});
describe('#getX', function() {
it('should return 0', function() {
var p = point();
p.getX().toString().should.equal('0');
});
it('should be convertable to a buffer', function() {
var p = point();
p.getX().toBuffer({size: 32}).length.should.equal(32);
});
});
describe('#getY', function() {
it('should return 0', function() {
var p = point();
p.getY().toString().should.equal('0');
});
it('should be convertable to a buffer', function() {
var p = point();
p.getY().toBuffer({size: 32}).length.should.equal(32);
});
});
describe('#add', function() {
it('should accurately add g to itself', function() {
var p1 = point.getG();
var p2 = point.getG();
var p3 = p1.add(p2);
p3.getX().toString().should.equal('89565891926547004231252920425935692360644145829622209833684329913297188986597');
p3.getY().toString().should.equal('12158399299693830322967808612713398636155367887041628176798871954788371653930');
});
});
describe('#mul', function() {
it('should accurately multiply g by 2', function() {
var g = point.getG();
var b = g.mul(bn(2));
b.getX().toString().should.equal('89565891926547004231252920425935692360644145829622209833684329913297188986597');
b.getY().toString().should.equal('12158399299693830322967808612713398636155367887041628176798871954788371653930');
});
it('should accurately multiply g by n-1', function() {
var g = point.getG();
var n = point.getN();
var b = g.mul(n.sub(1));
b.getX().toString().should.equal('55066263022277343669578718895168534326250603453777594175500187360389116729240');
b.getY().toString().should.equal('83121579216557378445487899878180864668798711284981320763518679672151497189239');
});
//not sure if this is technically accurate or not...
//normally, you should always multiply g by something less than n
//but it is the same result in OpenSSL
it('should accurately multiply g by n+1', function() {
var g = point.getG();
var n = point.getN();
var b = g.mul(n.add(1));
b.getX().toString().should.equal('55066263022277343669578718895168534326250603453777594175500187360389116729240');
b.getY().toString().should.equal('32670510020758816978083085130507043184471273380659243275938904335757337482424');
});
});
describe('#pointFromX', function() {
it('should return g', function() {
var g = point.getG();
var p = point.pointFromX(false, g.getX());
g.eq(p).should.equal(true);
});
});
});