2015-02-23 10:36:57 +11:00
|
|
|
/* global describe, it */
|
|
|
|
|
2014-03-29 04:24:23 +11:00
|
|
|
var assert = require('assert')
|
2015-08-20 13:37:19 +10:00
|
|
|
var bcrypto = require('../src/crypto')
|
2014-05-13 17:55:53 +10:00
|
|
|
var ecdsa = require('../src/ecdsa')
|
2018-03-20 14:49:10 +11:00
|
|
|
var hoodwink = require('hoodwink')
|
2014-05-13 16:35:07 +10:00
|
|
|
|
2014-05-03 10:04:54 +08:00
|
|
|
var BigInteger = require('bigi')
|
2014-03-29 04:24:23 +11:00
|
|
|
|
2015-08-25 20:43:32 +10:00
|
|
|
var curve = ecdsa.__curve
|
2014-06-07 16:24:27 +10:00
|
|
|
|
2014-05-18 19:47:39 +10:00
|
|
|
var fixtures = require('./fixtures/ecdsa.json')
|
2014-04-24 07:45:28 +10:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
describe('ecdsa', function () {
|
2016-10-13 23:45:08 +11:00
|
|
|
function fromRaw (signature) {
|
|
|
|
return {
|
|
|
|
r: new BigInteger(signature.r, 16),
|
|
|
|
s: new BigInteger(signature.s, 16)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function toRaw (signature) {
|
|
|
|
return {
|
|
|
|
r: signature.r.toHex(),
|
|
|
|
s: signature.s.toHex()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
describe('deterministicGenerateK', function () {
|
|
|
|
function checkSig () {
|
|
|
|
return true
|
|
|
|
}
|
2015-01-04 12:46:37 +11:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
fixtures.valid.ecdsa.forEach(function (f) {
|
|
|
|
it('for "' + f.message + '"', function () {
|
2015-08-25 20:43:32 +10:00
|
|
|
var x = BigInteger.fromHex(f.d).toBuffer(32)
|
2015-08-20 13:37:19 +10:00
|
|
|
var h1 = bcrypto.sha256(f.message)
|
2014-04-24 07:45:28 +10:00
|
|
|
|
2015-08-25 20:43:32 +10:00
|
|
|
var k = ecdsa.deterministicGenerateK(h1, x, checkSig)
|
2015-05-07 11:29:20 +10:00
|
|
|
assert.strictEqual(k.toHex(), f.k)
|
2014-04-24 07:45:28 +10:00
|
|
|
})
|
2014-04-23 06:18:38 +10:00
|
|
|
})
|
2014-06-26 02:42:51 +10:00
|
|
|
|
2018-03-20 14:49:10 +11:00
|
|
|
it('loops until an appropriate k value is found', hoodwink(function () {
|
2018-03-20 13:19:39 +11:00
|
|
|
this.mock(BigInteger, 'fromBuffer', function f (b) {
|
|
|
|
assert.strictEqual(b.length, 32)
|
|
|
|
if (f.calls === 0) return BigInteger.ZERO // < 1
|
|
|
|
if (f.calls === 1) return curve.n // > n - 1
|
|
|
|
if (f.calls === 2) return new BigInteger('42') // valid
|
|
|
|
}, 3)
|
2014-06-26 02:42:51 +10:00
|
|
|
|
2015-08-25 20:43:32 +10:00
|
|
|
var x = new BigInteger('1').toBuffer(32)
|
2017-04-19 17:39:16 +10:00
|
|
|
var h1 = Buffer.alloc(32)
|
2015-08-25 20:43:32 +10:00
|
|
|
var k = ecdsa.deterministicGenerateK(h1, x, checkSig)
|
2014-06-26 02:42:51 +10:00
|
|
|
|
2015-05-07 11:29:20 +10:00
|
|
|
assert.strictEqual(k.toString(), '42')
|
2014-06-26 02:42:51 +10:00
|
|
|
}))
|
2015-01-05 11:15:07 +11:00
|
|
|
|
2018-03-20 14:49:10 +11:00
|
|
|
it('loops until a suitable signature is found', hoodwink(function () {
|
2018-03-20 13:19:39 +11:00
|
|
|
var checkSigStub = this.stub(function f () {
|
|
|
|
if (f.calls === 0) return false // bad signature
|
|
|
|
if (f.calls === 1) return true // good signature
|
|
|
|
}, 2)
|
2015-01-05 11:15:07 +11:00
|
|
|
|
2018-03-20 13:19:39 +11:00
|
|
|
var x = BigInteger.ONE.toBuffer(32)
|
2017-04-19 17:39:16 +10:00
|
|
|
var h1 = Buffer.alloc(32)
|
2018-03-20 13:19:39 +11:00
|
|
|
var k = ecdsa.deterministicGenerateK(h1, x, checkSigStub)
|
2015-01-05 11:15:07 +11:00
|
|
|
|
2018-03-20 13:19:39 +11:00
|
|
|
assert.strictEqual(k.toHex(), 'a9b1a1a84a4c2f96b6158ed7a81404c50cb74373c22e8d9e02d0411d719acae2')
|
2015-01-05 11:15:07 +11:00
|
|
|
}))
|
2015-01-05 12:31:28 +11:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
fixtures.valid.rfc6979.forEach(function (f) {
|
|
|
|
it('produces the expected k values for ' + f.message + " if k wasn't suitable", function () {
|
2015-08-25 20:43:32 +10:00
|
|
|
var x = BigInteger.fromHex(f.d).toBuffer(32)
|
2015-08-20 13:37:19 +10:00
|
|
|
var h1 = bcrypto.sha256(f.message)
|
2015-01-05 12:31:28 +11:00
|
|
|
|
2015-01-05 12:42:09 +11:00
|
|
|
var results = []
|
2015-08-25 20:43:32 +10:00
|
|
|
ecdsa.deterministicGenerateK(h1, x, function (k) {
|
2015-01-05 12:42:09 +11:00
|
|
|
results.push(k)
|
2015-01-05 12:31:28 +11:00
|
|
|
|
2015-01-05 12:42:09 +11:00
|
|
|
return results.length === 16
|
2015-01-05 12:31:28 +11:00
|
|
|
})
|
2015-01-05 12:42:09 +11:00
|
|
|
|
2015-05-07 11:29:20 +10:00
|
|
|
assert.strictEqual(results[0].toHex(), f.k0)
|
|
|
|
assert.strictEqual(results[1].toHex(), f.k1)
|
|
|
|
assert.strictEqual(results[15].toHex(), f.k15)
|
2015-01-05 12:42:09 +11:00
|
|
|
})
|
2015-01-05 12:31:28 +11:00
|
|
|
})
|
2014-04-23 06:18:38 +10:00
|
|
|
})
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
describe('sign', function () {
|
|
|
|
fixtures.valid.ecdsa.forEach(function (f) {
|
|
|
|
it('produces a deterministic signature for "' + f.message + '"', function () {
|
2014-06-07 18:24:16 +10:00
|
|
|
var d = BigInteger.fromHex(f.d)
|
2015-08-20 13:37:19 +10:00
|
|
|
var hash = bcrypto.sha256(f.message)
|
2016-10-13 23:45:08 +11:00
|
|
|
var signature = ecdsa.sign(hash, d)
|
2014-04-17 04:10:05 +10:00
|
|
|
|
2016-10-13 23:45:08 +11:00
|
|
|
assert.deepEqual(toRaw(signature), f.signature)
|
2014-04-24 07:45:28 +10:00
|
|
|
})
|
2014-04-17 04:10:05 +10:00
|
|
|
})
|
2014-04-17 06:33:35 +02:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
it('should sign with low S value', function () {
|
2015-08-20 13:37:19 +10:00
|
|
|
var hash = bcrypto.sha256('Vires in numeris')
|
2015-08-25 20:43:32 +10:00
|
|
|
var sig = ecdsa.sign(hash, BigInteger.ONE)
|
2014-04-24 07:45:28 +10:00
|
|
|
|
|
|
|
// See BIP62 for more information
|
2014-06-16 01:36:05 +10:00
|
|
|
var N_OVER_TWO = curve.n.shiftRight(1)
|
2014-05-10 22:38:05 +10:00
|
|
|
assert(sig.s.compareTo(N_OVER_TWO) <= 0)
|
2014-04-24 07:45:28 +10:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-04-10 17:22:00 +10:00
|
|
|
describe('verify', function () {
|
2015-02-23 10:36:57 +11:00
|
|
|
fixtures.valid.ecdsa.forEach(function (f) {
|
|
|
|
it('verifies a valid signature for "' + f.message + '"', function () {
|
2014-06-07 18:24:16 +10:00
|
|
|
var d = BigInteger.fromHex(f.d)
|
2015-08-20 13:37:19 +10:00
|
|
|
var H = bcrypto.sha256(f.message)
|
2016-10-13 23:45:08 +11:00
|
|
|
var signature = fromRaw(f.signature)
|
2014-06-16 01:36:05 +10:00
|
|
|
var Q = curve.G.multiply(d)
|
2014-04-17 06:33:35 +02:00
|
|
|
|
2015-08-25 20:43:32 +10:00
|
|
|
assert(ecdsa.verify(H, signature, Q))
|
2014-04-24 07:45:28 +10:00
|
|
|
})
|
2014-04-17 06:33:35 +02:00
|
|
|
})
|
2014-05-24 13:40:20 +10:00
|
|
|
|
2015-04-10 17:22:00 +10:00
|
|
|
fixtures.invalid.verify.forEach(function (f) {
|
2015-02-23 10:36:57 +11:00
|
|
|
it('fails to verify with ' + f.description, function () {
|
2015-08-20 13:37:19 +10:00
|
|
|
var H = bcrypto.sha256(f.message)
|
2014-06-07 18:24:16 +10:00
|
|
|
var d = BigInteger.fromHex(f.d)
|
2016-10-13 23:45:08 +11:00
|
|
|
var signature = fromRaw(f.signature)
|
2014-06-16 01:36:05 +10:00
|
|
|
var Q = curve.G.multiply(d)
|
2014-05-24 13:40:20 +10:00
|
|
|
|
2015-08-25 20:43:32 +10:00
|
|
|
assert.strictEqual(ecdsa.verify(H, signature, Q), false)
|
2014-05-24 13:40:20 +10:00
|
|
|
})
|
|
|
|
})
|
2014-04-17 04:10:05 +10:00
|
|
|
})
|
2014-03-29 04:24:23 +11:00
|
|
|
})
|