2014-03-31 05:47:47 +02:00
|
|
|
var assert = require('assert')
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-05-13 09:55:53 +02:00
|
|
|
var sec = require('../src/sec')
|
2014-03-31 05:47:47 +02:00
|
|
|
var ecparams = sec('secp256k1')
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-05-03 04:04:54 +02:00
|
|
|
var BigInteger = require('bigi')
|
2014-05-13 09:55:53 +02:00
|
|
|
var ECPointFp = require('../src/ec').ECPointFp
|
2014-04-16 20:10:05 +02:00
|
|
|
|
2014-05-18 11:47:39 +02:00
|
|
|
var fixtures = require('./fixtures/ec.json')
|
2014-05-16 15:17:46 +02:00
|
|
|
|
2014-04-16 20:10:05 +02:00
|
|
|
describe('ec', function() {
|
|
|
|
describe('ECPointFp', function() {
|
2014-04-21 18:04:56 +02:00
|
|
|
it('behaves correctly', function() {
|
2014-04-16 20:10:05 +02:00
|
|
|
var G = ecparams.getG()
|
|
|
|
var n = ecparams.getN()
|
|
|
|
|
|
|
|
assert.ok(G.multiply(n).isInfinity(), "Gn is infinite")
|
|
|
|
|
2014-04-21 18:04:56 +02:00
|
|
|
var k = BigInteger.ONE
|
2014-04-16 20:10:05 +02:00
|
|
|
var P = G.multiply(k)
|
|
|
|
assert.ok(!P.isInfinity(), "kG is not infinite")
|
|
|
|
assert.ok(P.isOnCurve(), "kG on curve")
|
|
|
|
assert.ok(P.multiply(n).isInfinity(), "kGn is infinite")
|
|
|
|
|
|
|
|
assert.ok(P.validate(), "kG validates as a public key")
|
|
|
|
})
|
|
|
|
|
2014-05-16 15:17:46 +02:00
|
|
|
describe('getEncoded', function() {
|
|
|
|
it('encodes a point correctly', function() {
|
|
|
|
fixtures.valid.ECPointFp.forEach(function(f) {
|
|
|
|
var curve = ecparams.getCurve()
|
|
|
|
var Q = new ECPointFp(
|
|
|
|
curve,
|
|
|
|
curve.fromBigInteger(new BigInteger(f.x)),
|
|
|
|
curve.fromBigInteger(new BigInteger(f.y))
|
|
|
|
)
|
|
|
|
|
2014-05-16 16:33:33 +02:00
|
|
|
var encoded = Q.getEncoded(f.compressed)
|
2014-05-16 15:17:46 +02:00
|
|
|
assert.equal(encoded.toString('hex'), f.hex)
|
|
|
|
})
|
|
|
|
})
|
2014-04-21 18:08:00 +02:00
|
|
|
})
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-05-16 15:17:46 +02:00
|
|
|
describe('decodeFrom', function() {
|
|
|
|
it('decodes the correct point', function() {
|
|
|
|
fixtures.valid.ECPointFp.forEach(function(f) {
|
|
|
|
var curve = ecparams.getCurve()
|
|
|
|
var buffer = new Buffer(f.hex, 'hex')
|
|
|
|
|
|
|
|
var decoded = ECPointFp.decodeFrom(curve, buffer)
|
2014-05-16 16:39:30 +02:00
|
|
|
assert.equal(decoded.Q.getX().toBigInteger().toString(), f.x)
|
|
|
|
assert.equal(decoded.Q.getY().toBigInteger().toString(), f.y)
|
|
|
|
assert.equal(decoded.compressed, f.compressed)
|
2014-05-16 15:17:46 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-05-16 16:39:30 +02:00
|
|
|
fixtures.invalid.ECPointFp.forEach(function(f) {
|
|
|
|
it('throws on ' + f.description, function() {
|
|
|
|
var curve = ecparams.getCurve()
|
|
|
|
var buffer = new Buffer(f.hex, 'hex')
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
ECPointFp.decodeFrom(curve, buffer)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-05-17 06:10:32 +02:00
|
|
|
|
|
|
|
it('supports other curves', function() {
|
|
|
|
var f = fixtures.valid.ECPointFp[1]
|
|
|
|
var ecparams2 = sec('secp256r1')
|
|
|
|
var curve = ecparams2.getCurve()
|
|
|
|
|
|
|
|
var D = BigInteger.ONE
|
|
|
|
var Q = ecparams2.getG().multiply(D)
|
|
|
|
|
|
|
|
var buffer = Q.getEncoded(true)
|
|
|
|
var decoded = ECPointFp.decodeFrom(curve, buffer)
|
|
|
|
|
|
|
|
assert(Q.equals(decoded.Q))
|
|
|
|
assert(decoded.compressed, true)
|
|
|
|
})
|
2014-04-16 20:10:05 +02:00
|
|
|
})
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-01-11 07:57:43 +01:00
|
|
|
})
|