bitcoinjs-lib/test/ec.js

85 lines
2.5 KiB
JavaScript
Raw Normal View History

2014-03-31 05:47:47 +02:00
var assert = require('assert')
var sec = require('../src/sec')
2014-03-31 05:47:47 +02:00
var ecparams = sec('secp256k1')
2014-05-03 04:04:54 +02:00
var BigInteger = require('bigi')
var ECPointFp = require('../src/ec').ECPointFp
2014-05-18 11:47:39 +02:00
var fixtures = require('./fixtures/ec.json')
describe('ec', function() {
describe('ECPointFp', function() {
2014-04-21 18:04:56 +02:00
it('behaves correctly', function() {
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
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")
})
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)
assert.equal(encoded.toString('hex'), f.hex)
})
})
})
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)
assert.equal(decoded.Q.getX().toBigInteger().toString(), f.x)
assert.equal(decoded.Q.getY().toBigInteger().toString(), f.y)
assert.equal(decoded.compressed, f.compressed)
})
})
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-29 07:42:12 +02:00
}, /Invalid sequence length|Invalid sequence tag/)
})
})
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-03-31 05:47:47 +02:00
})
})