From 657f992e7b3a473257183b14e9206efa74752bfa Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Wed, 6 Aug 2014 21:02:42 -0700 Subject: [PATCH] point --- index.js | 2 +- lib/point.js | 32 ++++++++++++++++ test/test.point.js | 92 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 lib/point.js create mode 100644 test/test.point.js diff --git a/index.js b/index.js index 6a2b336..3eadd93 100644 --- a/index.js +++ b/index.js @@ -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'); diff --git a/lib/point.js b/lib/point.js new file mode 100644 index 0000000..2275495 --- /dev/null +++ b/lib/point.js @@ -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; diff --git a/test/test.point.js b/test/test.point.js new file mode 100644 index 0000000..71efd5b --- /dev/null +++ b/test/test.point.js @@ -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); + }); + + }); + +});