Merge pull request #460 from bitcoinjs/modulo

Modulo tests
This commit is contained in:
Daniel Cousens 2015-09-09 12:18:14 +10:00
commit 0f8bce50f6
2 changed files with 16 additions and 18 deletions

View file

@ -89,7 +89,7 @@ ECPair.makeRandom = function (options) {
typeforce(types.Buffer256bit, buffer) typeforce(types.Buffer256bit, buffer)
d = BigInteger.fromBuffer(buffer) d = BigInteger.fromBuffer(buffer)
} while (d.compareTo(secp256k1.n) >= 0) } while (d.signum() <= 0 || d.compareTo(secp256k1.n) >= 0)
return new ECPair(d, null, options) return new ECPair(d, null, options)
} }

View file

@ -11,7 +11,7 @@ var BigInteger = require('bigi')
var ECPair = require('../src/ecpair') var ECPair = require('../src/ecpair')
var fixtures = require('./fixtures/ecpair.json') var fixtures = require('./fixtures/ecpair.json')
var secp256k1 = ecurve.getCurveByName('secp256k1') var curve = ecdsa.__curve
var NETWORKS = require('../src/networks') var NETWORKS = require('../src/networks')
var NETWORKS_LIST = [] // Object.values(NETWORKS) var NETWORKS_LIST = [] // Object.values(NETWORKS)
@ -54,7 +54,7 @@ describe('ECPair', function () {
it('throws if public and private key given', function () { it('throws if public and private key given', function () {
var qBuffer = new Buffer('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', 'hex') var qBuffer = new Buffer('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', 'hex')
var Q = ecurve.Point.decodeFrom(secp256k1, qBuffer) var Q = ecurve.Point.decodeFrom(curve, qBuffer)
assert.throws(function () { assert.throws(function () {
new ECPair(BigInteger.ONE, Q) new ECPair(BigInteger.ONE, Q)
@ -100,7 +100,7 @@ describe('ECPair', function () {
it('wraps Q.getEncoded', sinon.test(function () { it('wraps Q.getEncoded', sinon.test(function () {
this.mock(keyPair.Q).expects('getEncoded') this.mock(keyPair.Q).expects('getEncoded')
.once().calledWith(keyPair.compressed) .once().withArgs(keyPair.compressed)
keyPair.getPublicKeyBuffer() keyPair.getPublicKeyBuffer()
})) }))
@ -160,18 +160,6 @@ describe('ECPair', function () {
var keyPair = ProxiedECPair.makeRandom() var keyPair = ProxiedECPair.makeRandom()
assert.strictEqual(keyPair.toWIF(), exWIF) assert.strictEqual(keyPair.toWIF(), exWIF)
}) })
it('passes the options param', sinon.test(function () {
var options = {
compressed: true
}
// FIXME: waiting on https://github.com/cjohansen/Sinon.JS/issues/613
// this.mock(ECPair).expects('constructor')
// .once().calledWith(options)
ECPair.makeRandom(options)
}))
}) })
it('allows a custom RNG to be used', function () { it('allows a custom RNG to be used', function () {
@ -181,6 +169,16 @@ describe('ECPair', function () {
assert.strictEqual(keyPair.toWIF(), exWIF) assert.strictEqual(keyPair.toWIF(), exWIF)
}) })
it('loops until d is within interval [1, n - 1]', sinon.test(function () {
var rng = this.mock()
rng.exactly(3)
rng.onCall(0).returns(new BigInteger('0').toBuffer(32)) // < 1
rng.onCall(1).returns(curve.n.toBuffer(32)) // > n-1
rng.onCall(2).returns(new BigInteger('42').toBuffer(32)) // valid
ECPair.makeRandom({ rng: rng })
}))
}) })
describe('getAddress', function () { describe('getAddress', function () {
@ -204,7 +202,7 @@ describe('ECPair', function () {
describe('signing', function () { describe('signing', function () {
it('wraps ecdsa.sign', sinon.test(function () { it('wraps ecdsa.sign', sinon.test(function () {
this.mock(ecdsa).expects('sign') this.mock(ecdsa).expects('sign')
.once().calledWith(secp256k1, hash, keyPair.d) .once().withArgs(hash, keyPair.d)
keyPair.sign(hash) keyPair.sign(hash)
})) }))
@ -227,7 +225,7 @@ describe('ECPair', function () {
it('wraps ecdsa.verify', sinon.test(function () { it('wraps ecdsa.verify', sinon.test(function () {
this.mock(ecdsa).expects('verify') this.mock(ecdsa).expects('verify')
.once().calledWith(secp256k1, hash, signature, keyPair.Q) .once().withArgs(hash, signature, keyPair.Q)
keyPair.verify(hash, signature) keyPair.verify(hash, signature)
})) }))