|
@ -10,8 +10,7 @@ var sinon = require('sinon') |
|
|
var BigInteger = require('bigi') |
|
|
var BigInteger = require('bigi') |
|
|
var ECSignature = require('../src/ecsignature') |
|
|
var ECSignature = require('../src/ecsignature') |
|
|
|
|
|
|
|
|
var ecurve = require('ecurve') |
|
|
var curve = ecdsa.__curve |
|
|
var curve = ecurve.getCurveByName('secp256k1') |
|
|
|
|
|
|
|
|
|
|
|
var fixtures = require('./fixtures/ecdsa.json') |
|
|
var fixtures = require('./fixtures/ecdsa.json') |
|
|
|
|
|
|
|
@ -23,10 +22,10 @@ describe('ecdsa', function () { |
|
|
|
|
|
|
|
|
fixtures.valid.ecdsa.forEach(function (f) { |
|
|
fixtures.valid.ecdsa.forEach(function (f) { |
|
|
it('for "' + f.message + '"', function () { |
|
|
it('for "' + f.message + '"', function () { |
|
|
var d = BigInteger.fromHex(f.d) |
|
|
var x = BigInteger.fromHex(f.d).toBuffer(32) |
|
|
var h1 = bcrypto.sha256(f.message) |
|
|
var h1 = bcrypto.sha256(f.message) |
|
|
|
|
|
|
|
|
var k = ecdsa.deterministicGenerateK(curve, h1, d, checkSig) |
|
|
var k = ecdsa.deterministicGenerateK(h1, x, checkSig) |
|
|
assert.strictEqual(k.toHex(), f.k) |
|
|
assert.strictEqual(k.toHex(), f.k) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
@ -38,9 +37,9 @@ describe('ecdsa', function () { |
|
|
.onCall(1).returns(curve.n) // > n-1
|
|
|
.onCall(1).returns(curve.n) // > n-1
|
|
|
.onCall(2).returns(new BigInteger('42')) // valid
|
|
|
.onCall(2).returns(new BigInteger('42')) // valid
|
|
|
|
|
|
|
|
|
var d = new BigInteger('1') |
|
|
var x = new BigInteger('1').toBuffer(32) |
|
|
var h1 = new Buffer(32) |
|
|
var h1 = new Buffer(32) |
|
|
var k = ecdsa.deterministicGenerateK(curve, h1, d, checkSig) |
|
|
var k = ecdsa.deterministicGenerateK(h1, x, checkSig) |
|
|
|
|
|
|
|
|
assert.strictEqual(k.toString(), '42') |
|
|
assert.strictEqual(k.toString(), '42') |
|
|
})) |
|
|
})) |
|
@ -58,20 +57,20 @@ describe('ecdsa', function () { |
|
|
checkSig.onCall(0).returns(false) // bad signature
|
|
|
checkSig.onCall(0).returns(false) // bad signature
|
|
|
checkSig.onCall(1).returns(true) // good signature
|
|
|
checkSig.onCall(1).returns(true) // good signature
|
|
|
|
|
|
|
|
|
var d = new BigInteger('1') |
|
|
var x = new BigInteger('1').toBuffer(32) |
|
|
var h1 = new Buffer(32) |
|
|
var h1 = new Buffer(32) |
|
|
var k = ecdsa.deterministicGenerateK(curve, h1, d, checkSig) |
|
|
var k = ecdsa.deterministicGenerateK(h1, x, checkSig) |
|
|
|
|
|
|
|
|
assert.strictEqual(k.toString(), '53') |
|
|
assert.strictEqual(k.toString(), '53') |
|
|
})) |
|
|
})) |
|
|
|
|
|
|
|
|
fixtures.valid.rfc6979.forEach(function (f) { |
|
|
fixtures.valid.rfc6979.forEach(function (f) { |
|
|
it('produces the expected k values for ' + f.message + " if k wasn't suitable", function () { |
|
|
it('produces the expected k values for ' + f.message + " if k wasn't suitable", function () { |
|
|
var d = BigInteger.fromHex(f.d) |
|
|
var x = BigInteger.fromHex(f.d).toBuffer(32) |
|
|
var h1 = bcrypto.sha256(f.message) |
|
|
var h1 = bcrypto.sha256(f.message) |
|
|
|
|
|
|
|
|
var results = [] |
|
|
var results = [] |
|
|
ecdsa.deterministicGenerateK(curve, h1, d, function (k) { |
|
|
ecdsa.deterministicGenerateK(h1, x, function (k) { |
|
|
results.push(k) |
|
|
results.push(k) |
|
|
|
|
|
|
|
|
return results.length === 16 |
|
|
return results.length === 16 |
|
@ -92,7 +91,7 @@ describe('ecdsa', function () { |
|
|
var signature = ECSignature.fromDER(new Buffer(f.signature, 'hex')) |
|
|
var signature = ECSignature.fromDER(new Buffer(f.signature, 'hex')) |
|
|
var h1 = bcrypto.sha256(f.message) |
|
|
var h1 = bcrypto.sha256(f.message) |
|
|
var e = BigInteger.fromBuffer(h1) |
|
|
var e = BigInteger.fromBuffer(h1) |
|
|
var Qprime = ecdsa.recoverPubKey(curve, e, signature, f.i) |
|
|
var Qprime = ecdsa.recoverPubKey(e, signature, f.i) |
|
|
|
|
|
|
|
|
assert(Qprime.equals(Q)) |
|
|
assert(Qprime.equals(Q)) |
|
|
}) |
|
|
}) |
|
@ -113,7 +112,7 @@ describe('ecdsa', function () { |
|
|
|
|
|
|
|
|
points.forEach(function (expectedHex, i) { |
|
|
points.forEach(function (expectedHex, i) { |
|
|
it('recovers an expected point for i of ' + i, function () { |
|
|
it('recovers an expected point for i of ' + i, function () { |
|
|
var Qprime = ecdsa.recoverPubKey(curve, e, signature, i) |
|
|
var Qprime = ecdsa.recoverPubKey(e, signature, i) |
|
|
var QprimeHex = Qprime.getEncoded().toString('hex') |
|
|
var QprimeHex = Qprime.getEncoded().toString('hex') |
|
|
|
|
|
|
|
|
assert.strictEqual(QprimeHex, expectedHex) |
|
|
assert.strictEqual(QprimeHex, expectedHex) |
|
@ -127,7 +126,7 @@ describe('ecdsa', function () { |
|
|
var signature = new ECSignature(new BigInteger(f.signatureRaw.r, 16), new BigInteger(f.signatureRaw.s, 16)) |
|
|
var signature = new ECSignature(new BigInteger(f.signatureRaw.r, 16), new BigInteger(f.signatureRaw.s, 16)) |
|
|
|
|
|
|
|
|
assert.throws(function () { |
|
|
assert.throws(function () { |
|
|
ecdsa.recoverPubKey(curve, e, signature, f.i) |
|
|
ecdsa.recoverPubKey(e, signature, f.i) |
|
|
}, new RegExp(f.exception)) |
|
|
}, new RegExp(f.exception)) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
@ -138,7 +137,7 @@ describe('ecdsa', function () { |
|
|
it('produces a deterministic signature for "' + f.message + '"', function () { |
|
|
it('produces a deterministic signature for "' + f.message + '"', function () { |
|
|
var d = BigInteger.fromHex(f.d) |
|
|
var d = BigInteger.fromHex(f.d) |
|
|
var hash = bcrypto.sha256(f.message) |
|
|
var hash = bcrypto.sha256(f.message) |
|
|
var signature = ecdsa.sign(curve, hash, d).toDER() |
|
|
var signature = ecdsa.sign(hash, d).toDER() |
|
|
|
|
|
|
|
|
assert.strictEqual(signature.toString('hex'), f.signature) |
|
|
assert.strictEqual(signature.toString('hex'), f.signature) |
|
|
}) |
|
|
}) |
|
@ -146,7 +145,7 @@ describe('ecdsa', function () { |
|
|
|
|
|
|
|
|
it('should sign with low S value', function () { |
|
|
it('should sign with low S value', function () { |
|
|
var hash = bcrypto.sha256('Vires in numeris') |
|
|
var hash = bcrypto.sha256('Vires in numeris') |
|
|
var sig = ecdsa.sign(curve, hash, BigInteger.ONE) |
|
|
var sig = ecdsa.sign(hash, BigInteger.ONE) |
|
|
|
|
|
|
|
|
// See BIP62 for more information
|
|
|
// See BIP62 for more information
|
|
|
var N_OVER_TWO = curve.n.shiftRight(1) |
|
|
var N_OVER_TWO = curve.n.shiftRight(1) |
|
@ -162,7 +161,7 @@ describe('ecdsa', function () { |
|
|
var signature = ECSignature.fromDER(new Buffer(f.signature, 'hex')) |
|
|
var signature = ECSignature.fromDER(new Buffer(f.signature, 'hex')) |
|
|
var Q = curve.G.multiply(d) |
|
|
var Q = curve.G.multiply(d) |
|
|
|
|
|
|
|
|
assert(ecdsa.verify(curve, H, signature, Q)) |
|
|
assert(ecdsa.verify(H, signature, Q)) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
@ -180,7 +179,7 @@ describe('ecdsa', function () { |
|
|
|
|
|
|
|
|
var Q = curve.G.multiply(d) |
|
|
var Q = curve.G.multiply(d) |
|
|
|
|
|
|
|
|
assert.strictEqual(ecdsa.verify(curve, H, signature, Q), false) |
|
|
assert.strictEqual(ecdsa.verify(H, signature, Q), false) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|