3 changed files with 125 additions and 1 deletions
@ -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; |
@ -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); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
}); |
Loading…
Reference in new issue