diff --git a/src/ecdsa.js b/src/ecdsa.js index 82fd419..c68daa2 100644 --- a/src/ecdsa.js +++ b/src/ecdsa.js @@ -1,3 +1,4 @@ +var assert = require('assert') var BigInteger = require('./bigi') var ECPointFp = require('./ec').ECPointFp @@ -35,7 +36,10 @@ function implShamirsTrick(P, k, Q, l) { return R } -function deterministicGenerateK(hash,key) { +function deterministicGenerateK(hash, secret) { + assert(Array.isArray(hash)) + assert(Array.isArray(secret)) + var vArr = [] var kArr = [] for (var i = 0;i < 32;i++) vArr.push(1) @@ -43,10 +47,10 @@ function deterministicGenerateK(hash,key) { var v = convert.bytesToWordArray(vArr) var k = convert.bytesToWordArray(kArr) - k = HmacSHA256(convert.bytesToWordArray(vArr.concat([0]).concat(key).concat(hash)), k) + k = HmacSHA256(convert.bytesToWordArray(vArr.concat([0]).concat(secret).concat(hash)), k) v = HmacSHA256(v, k) vArr = convert.wordArrayToBytes(v) - k = HmacSHA256(convert.bytesToWordArray(vArr.concat([1]).concat(key).concat(hash)), k) + k = HmacSHA256(convert.bytesToWordArray(vArr.concat([1]).concat(secret).concat(hash)), k) v = HmacSHA256(v,k) v = HmacSHA256(v,k) vArr = convert.wordArrayToBytes(v) @@ -54,7 +58,11 @@ function deterministicGenerateK(hash,key) { } var ecdsa = { + deterministicGenerateK: deterministicGenerateK, sign: function (hash, priv) { + if (Buffer.isBuffer(hash)) hash = Array.prototype.slice.call(hash) + if (Buffer.isBuffer(priv)) priv = Array.prototype.slice.call(priv) + var d = priv var n = ecparams.getN() var e = BigInteger.fromBuffer(hash) diff --git a/test/ecdsa.js b/test/ecdsa.js index fc5803b..bdb7a44 100644 --- a/test/ecdsa.js +++ b/test/ecdsa.js @@ -10,6 +10,18 @@ var ECPubKey = require('..').ECPubKey var Message = require('..').Message describe('ecdsa', function() { + // FIXME: needs much better tests than this + describe('deterministicGenerateK', function() { + it('produces deterministic K values', function() { + var secret = [4] + + var k1 = ecdsa.deterministicGenerateK([1], secret) + var k2 = ecdsa.deterministicGenerateK([2], secret) + + assert.notDeepEqual(k1, k2) + }) + }) + describe('recoverPubKey', function() { it('succesfully recovers a public key', function() { var addr = 'mgQK8S6CfSXKjPmnujArSmVxafeJfrZsa3' @@ -24,7 +36,7 @@ describe('ecdsa', function() { describe('sign/verify', function() { it('Signing and Verifying', function () { var s1 = ECKey.makeRandom() - var sig_a = s1.sign(BigInteger.ZERO) + var sig_a = s1.sign([0]) assert.ok(sig_a, 'Sign null') assert.ok(s1.pub.verify(BigInteger.ZERO, sig_a)) @@ -59,7 +71,7 @@ describe('ecdsa', function() { it('should sign with low S value', function() { var priv = ECKey.fromHex('ca48ec9783cf3ad0dfeff1fc254395a2e403cbbc666477b61b45e31d3b8ab458') - var message = 'Vires in numeris' + var message = new Buffer('Vires in numeris') var signature = priv.sign(message) var parsed = ecdsa.parseSig(signature) diff --git a/test/eckey.js b/test/eckey.js index 994d8e8..2549cfc 100644 --- a/test/eckey.js +++ b/test/eckey.js @@ -124,7 +124,7 @@ describe('ECKey', function() { describe('signing', function() { var hpriv = 'ca48ec9783cf3ad0dfeff1fc254395a2e403cbbc666477b61b45e31d3b8ab458' var hcpub = '024b12d9d7c77db68388b6ff7c89046174c871546436806bcd80d07c28ea811992' - var message = 'Vires in numeris' + var message = new Buffer('Vires in numeris') it('should verify against the private key', function() { var priv = ECKey.fromHex(hpriv)