2014-03-28 18:24:23 +01:00
|
|
|
var assert = require('assert')
|
2014-05-13 09:55:53 +02:00
|
|
|
var crypto = require('../src/crypto')
|
|
|
|
var ecdsa = require('../src/ecdsa')
|
2014-05-16 07:39:17 +02:00
|
|
|
var message = require('../src/message')
|
|
|
|
var networks = require('../src/networks')
|
2014-06-25 18:42:51 +02:00
|
|
|
var sinon = require('sinon')
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2014-05-03 04:04:54 +02:00
|
|
|
var BigInteger = require('bigi')
|
2014-06-15 08:44:52 +02:00
|
|
|
var ECSignature = require('../src/ecsignature')
|
2014-03-28 18:24:23 +01:00
|
|
|
|
2014-06-07 08:24:27 +02:00
|
|
|
var ecurve = require('ecurve')
|
|
|
|
var curve = ecurve.getCurveByName('secp256k1')
|
|
|
|
|
2014-05-18 11:47:39 +02:00
|
|
|
var fixtures = require('./fixtures/ecdsa.json')
|
2014-04-23 23:45:28 +02:00
|
|
|
|
2014-03-28 18:24:23 +01:00
|
|
|
describe('ecdsa', function() {
|
2014-04-22 22:18:38 +02:00
|
|
|
describe('deterministicGenerateK', function() {
|
2015-01-04 02:46:37 +01:00
|
|
|
function checkSig() { return true }
|
|
|
|
|
2015-01-05 02:31:28 +01:00
|
|
|
fixtures.valid.ecdsa.forEach(function(f) {
|
2014-06-21 14:25:09 +02:00
|
|
|
it('for \"' + f.message + '\"', function() {
|
2014-06-07 10:24:16 +02:00
|
|
|
var d = BigInteger.fromHex(f.d)
|
2014-04-23 23:45:28 +02:00
|
|
|
var h1 = crypto.sha256(f.message)
|
|
|
|
|
2015-01-04 02:46:37 +01:00
|
|
|
var k = ecdsa.deterministicGenerateK(curve, h1, d, checkSig)
|
2014-05-16 15:11:04 +02:00
|
|
|
assert.equal(k.toHex(), f.k)
|
2014-04-23 23:45:28 +02:00
|
|
|
})
|
2014-04-22 22:18:38 +02:00
|
|
|
})
|
2014-06-25 18:42:51 +02:00
|
|
|
|
2015-01-06 05:12:34 +01:00
|
|
|
// FIXME: remove in 2.0.0
|
|
|
|
fixtures.valid.ecdsa.forEach(function(f) {
|
|
|
|
it('(deprecated) for \"' + f.message + '\"', function() {
|
|
|
|
var d = BigInteger.fromHex(f.d)
|
|
|
|
var h1 = crypto.sha256(f.message)
|
|
|
|
|
|
|
|
var k = ecdsa.deterministicGenerateK(curve, h1, d) // default checkSig
|
|
|
|
assert.equal(k.toHex(), f.k)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-06-25 18:42:51 +02:00
|
|
|
it('loops until an appropriate k value is found', sinon.test(function() {
|
|
|
|
this.mock(BigInteger).expects('fromBuffer')
|
|
|
|
.exactly(3)
|
2015-01-05 01:15:07 +01:00
|
|
|
.onCall(0).returns(new BigInteger('0')) // < 1
|
|
|
|
.onCall(1).returns(curve.n) // > n-1
|
|
|
|
.onCall(2).returns(new BigInteger('42')) // valid
|
2014-06-25 18:42:51 +02:00
|
|
|
|
|
|
|
var d = new BigInteger('1')
|
|
|
|
var h1 = new Buffer(32)
|
2015-01-04 02:46:37 +01:00
|
|
|
var k = ecdsa.deterministicGenerateK(curve, h1, d, checkSig)
|
2014-06-25 18:42:51 +02:00
|
|
|
|
|
|
|
assert.equal(k.toString(), '42')
|
|
|
|
}))
|
2015-01-05 01:15:07 +01:00
|
|
|
|
|
|
|
it('loops until a suitable signature is found', sinon.test(function() {
|
|
|
|
this.mock(BigInteger).expects('fromBuffer')
|
|
|
|
.exactly(4)
|
|
|
|
.onCall(0).returns(new BigInteger('0')) // < 1
|
|
|
|
.onCall(1).returns(curve.n) // > n-1
|
|
|
|
.onCall(2).returns(new BigInteger('42')) // valid, but 'bad' signature
|
|
|
|
.onCall(3).returns(new BigInteger('53')) // valid, good signature
|
|
|
|
|
|
|
|
var checkSig = this.mock()
|
|
|
|
checkSig.exactly(2)
|
|
|
|
checkSig.onCall(0).returns(false) // bad signature
|
|
|
|
checkSig.onCall(1).returns(true) // good signature
|
|
|
|
|
|
|
|
var d = new BigInteger('1')
|
|
|
|
var h1 = new Buffer(32)
|
|
|
|
var k = ecdsa.deterministicGenerateK(curve, h1, d, checkSig)
|
|
|
|
|
|
|
|
assert.equal(k.toString(), '53')
|
|
|
|
}))
|
2015-01-05 02:31:28 +01:00
|
|
|
|
|
|
|
fixtures.valid.rfc6979.forEach(function(f) {
|
2015-01-05 02:42:09 +01:00
|
|
|
it('produces the expected k values for ' + f.message + ' if k wasn\'t suitable', function() {
|
2015-01-05 02:31:28 +01:00
|
|
|
var d = BigInteger.fromHex(f.d)
|
|
|
|
var h1 = crypto.sha256(f.message)
|
|
|
|
|
2015-01-05 02:42:09 +01:00
|
|
|
var results = []
|
2015-01-05 02:31:28 +01:00
|
|
|
ecdsa.deterministicGenerateK(curve, h1, d, function(k) {
|
2015-01-05 02:42:09 +01:00
|
|
|
results.push(k)
|
2015-01-05 02:31:28 +01:00
|
|
|
|
2015-01-05 02:42:09 +01:00
|
|
|
return results.length === 16
|
2015-01-05 02:31:28 +01:00
|
|
|
})
|
2015-01-05 02:42:09 +01:00
|
|
|
|
|
|
|
assert.equal(results[0].toHex(), f.k0)
|
|
|
|
assert.equal(results[1].toHex(), f.k1)
|
|
|
|
assert.equal(results[15].toHex(), f.k15)
|
|
|
|
})
|
2015-01-05 02:31:28 +01:00
|
|
|
})
|
2014-04-22 22:18:38 +02:00
|
|
|
})
|
|
|
|
|
2014-03-28 18:24:23 +01:00
|
|
|
describe('recoverPubKey', function() {
|
2015-01-05 02:31:28 +01:00
|
|
|
fixtures.valid.ecdsa.forEach(function(f) {
|
2014-06-14 13:06:30 +02:00
|
|
|
it('recovers the pubKey for ' + f.d, function() {
|
|
|
|
var d = BigInteger.fromHex(f.d)
|
2014-06-15 17:36:05 +02:00
|
|
|
var Q = curve.G.multiply(d)
|
2014-06-14 13:06:30 +02:00
|
|
|
var signature = {
|
|
|
|
r: new BigInteger(f.signature.r),
|
|
|
|
s: new BigInteger(f.signature.s)
|
|
|
|
}
|
|
|
|
var h1 = crypto.sha256(f.message)
|
|
|
|
var e = BigInteger.fromBuffer(h1)
|
2014-06-15 08:44:52 +02:00
|
|
|
var Qprime = ecdsa.recoverPubKey(curve, e, signature, f.i)
|
2014-05-16 07:39:17 +02:00
|
|
|
|
2014-06-14 13:06:30 +02:00
|
|
|
assert(Qprime.equals(Q))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with i ∈ {0,1,2,3}', function() {
|
2014-05-16 07:39:17 +02:00
|
|
|
var hash = message.magicHash('1111', networks.bitcoin)
|
2014-05-10 14:30:29 +02:00
|
|
|
var e = BigInteger.fromBuffer(hash)
|
2014-06-14 13:06:30 +02:00
|
|
|
|
2014-06-15 08:44:52 +02:00
|
|
|
var signatureBuffer = new Buffer('INcvXVVEFyIfHLbDX+xoxlKFn3Wzj9g0UbhObXdMq+YMKC252o5RHFr0/cKdQe1WsBLUBi4morhgZ77obDJVuV0=', 'base64')
|
2014-06-16 16:26:16 +02:00
|
|
|
var signature = ECSignature.parseCompact(signatureBuffer).signature
|
2014-06-14 13:06:30 +02:00
|
|
|
var points = [
|
|
|
|
'03e3a8c44a8bf712f1fbacee274fb19c0239b1a9e877eff0075ea335f2be8ff380',
|
|
|
|
'0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
|
|
|
|
'03d49e765f0bc27525c51a1b98fb1c99dacd59abe85a203af90f758260550b56c5',
|
|
|
|
'027eea09d46ac7fb6aa2e96f9c576677214ffdc238eb167734a9b39d1eb4c3d30d'
|
|
|
|
]
|
|
|
|
|
|
|
|
points.forEach(function(expectedHex, i) {
|
|
|
|
it('recovers an expected point for i of ' + i, function() {
|
2014-06-15 08:44:52 +02:00
|
|
|
var Qprime = ecdsa.recoverPubKey(curve, e, signature, i)
|
2014-06-14 13:06:30 +02:00
|
|
|
var QprimeHex = Qprime.getEncoded().toString('hex')
|
2014-05-16 07:39:17 +02:00
|
|
|
|
2014-06-14 13:06:30 +02:00
|
|
|
assert.equal(QprimeHex, expectedHex)
|
|
|
|
})
|
|
|
|
})
|
2014-03-28 18:24:23 +01:00
|
|
|
})
|
2014-06-14 03:45:01 +02:00
|
|
|
|
|
|
|
fixtures.invalid.recoverPubKey.forEach(function(f) {
|
|
|
|
it('throws on ' + f.description, function() {
|
|
|
|
var e = BigInteger.fromHex(f.e)
|
2014-06-15 08:44:52 +02:00
|
|
|
var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
|
2014-06-14 03:45:01 +02:00
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
ecdsa.recoverPubKey(curve, e, signature, f.i)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
2014-03-28 18:24:23 +01:00
|
|
|
})
|
2014-04-16 20:10:05 +02:00
|
|
|
|
2014-04-23 23:45:28 +02:00
|
|
|
describe('sign', function() {
|
2015-01-05 02:31:28 +01:00
|
|
|
fixtures.valid.ecdsa.forEach(function(f) {
|
2014-06-07 11:46:06 +02:00
|
|
|
it('produces a deterministic signature for \"' + f.message + '\"', function() {
|
2014-06-07 10:24:16 +02:00
|
|
|
var d = BigInteger.fromHex(f.d)
|
2014-04-23 23:45:28 +02:00
|
|
|
var hash = crypto.sha256(f.message)
|
2014-06-07 08:24:27 +02:00
|
|
|
var signature = ecdsa.sign(curve, hash, d)
|
2014-04-16 20:10:05 +02:00
|
|
|
|
2014-05-24 08:25:38 +02:00
|
|
|
assert.equal(signature.r.toString(), f.signature.r)
|
|
|
|
assert.equal(signature.s.toString(), f.signature.s)
|
2014-04-23 23:45:28 +02:00
|
|
|
})
|
2014-04-16 20:10:05 +02:00
|
|
|
})
|
2014-04-17 06:33:35 +02:00
|
|
|
|
|
|
|
it('should sign with low S value', function() {
|
2014-04-23 23:45:28 +02:00
|
|
|
var hash = crypto.sha256('Vires in numeris')
|
2014-06-07 08:24:27 +02:00
|
|
|
var sig = ecdsa.sign(curve, hash, BigInteger.ONE)
|
2014-04-23 23:45:28 +02:00
|
|
|
|
|
|
|
// See BIP62 for more information
|
2014-06-15 17:36:05 +02:00
|
|
|
var N_OVER_TWO = curve.n.shiftRight(1)
|
2014-05-10 14:38:05 +02:00
|
|
|
assert(sig.s.compareTo(N_OVER_TWO) <= 0)
|
2014-04-23 23:45:28 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-10-11 04:47:32 +02:00
|
|
|
describe('verify/verifyRaw', function() {
|
2015-01-05 02:31:28 +01:00
|
|
|
fixtures.valid.ecdsa.forEach(function(f) {
|
2014-06-07 11:46:06 +02:00
|
|
|
it('verifies a valid signature for \"' + f.message + '\"', function() {
|
2014-06-07 10:24:16 +02:00
|
|
|
var d = BigInteger.fromHex(f.d)
|
2014-10-11 04:47:32 +02:00
|
|
|
var H = crypto.sha256(f.message)
|
|
|
|
var e = BigInteger.fromBuffer(H)
|
2014-06-15 08:44:52 +02:00
|
|
|
var signature = new ECSignature(
|
|
|
|
new BigInteger(f.signature.r),
|
|
|
|
new BigInteger(f.signature.s)
|
|
|
|
)
|
2014-06-15 17:36:05 +02:00
|
|
|
var Q = curve.G.multiply(d)
|
2014-04-17 06:33:35 +02:00
|
|
|
|
2014-10-11 04:47:32 +02:00
|
|
|
assert(ecdsa.verify(curve, H, signature, Q))
|
2014-06-07 08:24:27 +02:00
|
|
|
assert(ecdsa.verifyRaw(curve, e, signature, Q))
|
2014-04-23 23:45:28 +02:00
|
|
|
})
|
2014-04-17 06:33:35 +02:00
|
|
|
})
|
2014-05-24 05:40:20 +02:00
|
|
|
|
|
|
|
fixtures.invalid.verifyRaw.forEach(function(f) {
|
|
|
|
it('fails to verify with ' + f.description, function() {
|
2014-10-11 04:47:32 +02:00
|
|
|
var H = crypto.sha256(f.message)
|
|
|
|
var e = BigInteger.fromBuffer(H)
|
2014-06-07 10:24:16 +02:00
|
|
|
var d = BigInteger.fromHex(f.d)
|
2014-06-15 08:44:52 +02:00
|
|
|
var signature = new ECSignature(
|
|
|
|
new BigInteger(f.signature.r),
|
|
|
|
new BigInteger(f.signature.s)
|
|
|
|
)
|
2014-06-15 17:36:05 +02:00
|
|
|
var Q = curve.G.multiply(d)
|
2014-05-24 05:40:20 +02:00
|
|
|
|
2014-10-11 04:47:32 +02:00
|
|
|
assert.equal(ecdsa.verify(curve, H, signature, Q), false)
|
2014-06-07 08:24:27 +02:00
|
|
|
assert.equal(ecdsa.verifyRaw(curve, e, signature, Q), false)
|
2014-05-24 05:40:20 +02:00
|
|
|
})
|
|
|
|
})
|
2014-04-16 20:10:05 +02:00
|
|
|
})
|
2014-03-28 18:24:23 +01:00
|
|
|
})
|