Skip to content

Commit

Permalink
Merge pull request #15 from dcousens/curveapi
Browse files Browse the repository at this point in the history
#14 continued
  • Loading branch information
jprichardson committed Jun 10, 2014
2 parents 8ce6520 + 6e814a5 commit a5d234c
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/curve.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function Curve(p, a, b, Gx, Gy, n, h) {
this.p = p
this.a = a
this.b = b
this.infinity = new Point(this, null, null)
this.infinity = new Point(this, null, null, BigInteger.ZERO)

this.params = {
G: Point.fromAffine(this, Gx, Gy),
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
var Point = require('./point')
var Curve = require('./curve')

var getECParams = require('./names')
var getCurveByName = require('./names')

//for legacy compatibility, remove in the future
Curve.Point = Point

module.exports = {
Curve: Curve,
Point: Point,
getECParams: getECParams
getCurveByName: getCurveByName
}

2 changes: 1 addition & 1 deletion lib/names.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,6 @@ var namedCurves = {
}
}

module.exports = function getECParams(name) {
module.exports = function getCurveByName(name) {
return (typeof namedCurves[name] == 'function')? namedCurves[name]() : null
}
2 changes: 2 additions & 0 deletions lib/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var BigInteger = require('bigi')
var THREE = BigInteger.valueOf(3)

function Point(curve, x, y, z) {
assert.notStrictEqual(z, undefined, 'Missing Z coordinate')

this.curve = curve
this.x = x
this.y = y
Expand Down
8 changes: 4 additions & 4 deletions test/curve.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var assert = require('assert')
var ecurve = require('../')
var getECParams = ecurve.getECParams
var getCurveByName = ecurve.getCurveByName

var BigInteger = require('bigi')
var Curve = ecurve.Curve
Expand Down Expand Up @@ -32,7 +32,7 @@ describe('Ecurve', function() {

fixtures.valid.forEach(function(f) {
it('calculates a public point for ' + f.D, function() {
var curve = ecurve.getECParams(f.Q.curve)
var curve = ecurve.getCurveByName(f.Q.curve)

var d = new BigInteger(f.D)
var Q = curve.params.G.multiply(d)
Expand Down Expand Up @@ -161,7 +161,7 @@ describe('Ecurve', function() {
})

describe('isOnCurve', function() {
var curve = getECParams('secp256k1')
var curve = getCurveByName('secp256k1')

it('should return true for a point on the curve', function() {
var d = BigInteger.ONE
Expand All @@ -187,7 +187,7 @@ describe('Ecurve', function() {
})

describe('validate', function() {
var curve = getECParams('secp256k1')
var curve = getCurveByName('secp256k1')

it('should validate a point on the curve', function() {
var d = BigInteger.ONE
Expand Down
10 changes: 5 additions & 5 deletions test/names.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
var assert = require('assert')
var BigInteger = require('bigi')

var getECParams = require('../lib/names')
var getCurveByName = require('../lib/names')

var curves = ['secp128r1', 'secp160k1', 'secp160r1', 'secp192k1', 'secp192r1', 'secp224r1', 'secp256k1', 'secp256r1']

describe('+ getECParams(curveName)', function() {
describe('+ getCurveByName(curveName)', function() {
describe('> when the secp256k1 curve is passed', function() {
it('should return the proper curve', function() {
var curve = getECParams('secp256k1')
var curve = getCurveByName('secp256k1')
assert(curve)
assert.equal(curve.p.toBuffer().toString('hex'), 'fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f')
assert.equal(curve.a.toBuffer().toString('hex'), '') // 0 becomes ''
Expand All @@ -22,15 +22,15 @@ describe('+ getECParams(curveName)', function() {
curves.forEach(function(c) {
describe('> when ' + c, function() {
it('should return the curve', function() {
var ecparams = getECParams(c)
var ecparams = getCurveByName(c)
assert(ecparams)
})
})
})

describe('> when null is passed', function() {
it('should return null for unknown curves', function() {
assert.equal(getECParams('foobar'), null)
assert.equal(getCurveByName('foobar'), null)
})
})
})
26 changes: 13 additions & 13 deletions test/point.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var assert= require('assert')
var ecurve = require('../')
var getECParams = ecurve.getECParams
var getCurveByName = ecurve.getCurveByName

var BigInteger = require('bigi')
var Curve = ecurve.Curve
Expand All @@ -19,13 +19,13 @@ describe('Point', function() {
var pubHexCompressed = '02d6d48c4a66a303856d9584a6ad49ce0965e9f0a5e4dcae878a3d017bd58ad7af'

it('should work with uncompressed keys', function(){
var curve = getECParams('secp256k1')
var curve = getCurveByName('secp256k1')
var pubPoint = Point.decodeFrom(curve, pubKey)
assert.equal(pubHex, pubPoint.getEncoded(false).toString('hex'))
})

it('should work with compressed keys', function() {
var curve = getECParams('secp256k1')
var curve = getCurveByName('secp256k1')
var pubPoint = Point.decodeFrom(curve, pubKey)
var pubKeyCompressed = pubPoint.getEncoded(true)
var pubPointCompressed = Point.decodeFrom(curve, pubKeyCompressed)
Expand All @@ -36,7 +36,7 @@ describe('Point', function() {

fixtures.valid.forEach(function(f) {
it('decodes ' + f.hex + ' correctly', function() {
var curve = getECParams(f.curve)
var curve = getCurveByName(f.curve)
var buffer = new Buffer(f.hex, 'hex')

var decoded = Point.decodeFrom(curve, buffer)
Expand All @@ -48,7 +48,7 @@ describe('Point', function() {

fixtures.invalid.forEach(function(f) {
it('throws on ' + f.description, function() {
var curve = getECParams('secp256k1')
var curve = getCurveByName('secp256k1')
var buffer = new Buffer(f.hex, 'hex')

assert.throws(function() {
Expand All @@ -61,7 +61,7 @@ describe('Point', function() {
describe('- getEncoded()', function() {
fixtures.valid.forEach(function(f) {
it('encode ' + f.hex + ' correctly', function() {
var curve = getECParams(f.curve)
var curve = getCurveByName(f.curve)
var Q = Point.fromAffine(curve, new BigInteger(f.x), new BigInteger(f.y))

var encoded = Q.getEncoded(f.compressed)
Expand All @@ -75,7 +75,7 @@ describe('Point', function() {
var x = "55066263022277343669578718895168534326250603453777594175500187360389116729240"
var y = "32670510020758816978083085130507043184471273380659243275938904335757337482424"
var res = "0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"
var curve = getECParams('secp256k1')
var curve = getCurveByName('secp256k1')
var doCompress = false

var Q = Point.fromAffine(curve, new BigInteger(x), new BigInteger(y))
Expand All @@ -91,7 +91,7 @@ describe('Point', function() {
var x = "55066263022277343669578718895168534326250603453777594175500187360389116729240"
var y = "32670510020758816978083085130507043184471273380659243275938904335757337482424"
var res = "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
var curve = getECParams('secp256k1')
var curve = getCurveByName('secp256k1')
var doCompress = true

var Q = Point.fromAffine(curve, new BigInteger(x), new BigInteger(y))
Expand All @@ -109,7 +109,7 @@ describe('Point', function() {
var x = "55066263022277343669578718895168534326250603453777594175500187360389116729240"
var y = "32670510020758816978083085130507043184471273380659243275938904335757337482424"
var res = "0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"
var curve = getECParams('secp256k1')
var curve = getCurveByName('secp256k1')
var doCompress = false

var Q = Point.fromAffine(curve, new BigInteger(x), new BigInteger(y))
Expand All @@ -125,7 +125,7 @@ describe('Point', function() {
var x = "55066263022277343669578718895168534326250603453777594175500187360389116729240"
var y = "32670510020758816978083085130507043184471273380659243275938904335757337482424"
var res = "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
var curve = getECParams('secp256k1')
var curve = getCurveByName('secp256k1')
var doCompress = true

var Q = Point.fromAffine(curve, new BigInteger(x), new BigInteger(y))
Expand All @@ -143,7 +143,7 @@ describe('Point', function() {
var x = "55066263022277343669578718895168534326250603453777594175500187360389116729240"
var y = "32670510020758816978083085130507043184471273380659243275938904335757337482424"
var res = "0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"
var curve = getECParams('secp256k1')
var curve = getCurveByName('secp256k1')

var Q = Point.fromAffine(curve, new BigInteger(x), new BigInteger(y))
Q.compressed = false
Expand All @@ -158,7 +158,7 @@ describe('Point', function() {
var x = "55066263022277343669578718895168534326250603453777594175500187360389116729240"
var y = "32670510020758816978083085130507043184471273380659243275938904335757337482424"
var res = "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
var curve = getECParams('secp256k1')
var curve = getCurveByName('secp256k1')

var Q = Point.fromAffine(curve, new BigInteger(x), new BigInteger(y))
Q.compressed = true
Expand All @@ -171,7 +171,7 @@ describe('Point', function() {
})

describe('- equals()', function() {
var curve = getECParams('secp256k1')
var curve = getCurveByName('secp256k1')

it('should return true when points are equal', function() {
var x1 = BigInteger.fromHex("FFFF")
Expand Down

0 comments on commit a5d234c

Please sign in to comment.