ecdsa: remove implicit ecparams

This commit is contained in:
Daniel Cousens 2014-05-17 15:06:52 +10:00
parent ab2ca395ac
commit bcfd62fdb5
5 changed files with 23 additions and 22 deletions

View file

@ -1,14 +1,12 @@
var assert = require('assert')
var crypto = require('./crypto')
var sec = require('./sec')
var ecparams = sec("secp256k1")
var BigInteger = require('bigi')
var ECPointFp = require('./ec').ECPointFp
var ecdsa = {
deterministicGenerateK: function(hash, D) {
assert(Buffer.isBuffer(hash), 'Hash must be a Buffer')
deterministicGenerateK: function(ecparams, hash, D) {
assert(Buffer.isBuffer(hash), 'Hash must be a Buffer, not ' + hash)
assert.equal(hash.length, 32, 'Hash must be 256 bit')
assert(D instanceof BigInteger, 'Private key must be a BigInteger')
@ -33,8 +31,8 @@ var ecdsa = {
return kB
},
sign: function (hash, D) {
var k = ecdsa.deterministicGenerateK(hash, D)
sign: function (ecparams, hash, D) {
var k = ecdsa.deterministicGenerateK(ecparams, hash, D)
var n = ecparams.getN()
var G = ecparams.getG()
@ -57,13 +55,13 @@ var ecdsa = {
return {r: r, s: s}
},
verify: function (hash, r, s, Q) {
verify: function (ecparams, hash, r, s, Q) {
var e = BigInteger.fromBuffer(hash)
return ecdsa.verifyRaw(e, r, s, Q)
return ecdsa.verifyRaw(ecparams, e, r, s, Q)
},
verifyRaw: function (e, r, s, Q) {
verifyRaw: function (ecparams, e, r, s, Q) {
var n = ecparams.getN()
var G = ecparams.getG()
@ -183,7 +181,7 @@ var ecdsa = {
*
* http://www.secg.org/download/aid-780/sec1-v2.pdf
*/
recoverPubKey: function (e, r, s, i) {
recoverPubKey: function (ecparams, e, r, s, i) {
assert.strictEqual(i & 3, i, 'The recovery param is more than two bits')
// A set LSB signifies that the y-coordinate is odd
@ -231,7 +229,7 @@ var ecdsa = {
var Q = R.multiplyTwo(s, G, eNeg).multiply(rInv)
Q.validate()
if (!ecdsa.verifyRaw(e, r, s, Q)) {
if (!ecdsa.verifyRaw(ecparams, e, r, s, Q)) {
throw new Error("Pubkey recovery unsuccessful")
}
@ -249,9 +247,9 @@ var ecdsa = {
* This function simply tries all four cases and returns the value
* that resulted in a successful pubkey recovery.
*/
calcPubKeyRecoveryParam: function (e, r, s, Q) {
calcPubKeyRecoveryParam: function (ecparams, e, r, s, Q) {
for (var i = 0; i < 4; i++) {
var Qprime = ecdsa.recoverPubKey(e, r, s, i)
var Qprime = ecdsa.recoverPubKey(ecparams, e, r, s, i)
if (Qprime.equals(Q)) {
return i

View file

@ -63,7 +63,7 @@ ECKey.prototype.toWIF = function(version) {
// Operations
ECKey.prototype.sign = function(hash) {
return ecdsa.sign(hash, this.D)
return ecdsa.sign(ecparams, hash, this.D)
}
module.exports = ECKey

View file

@ -37,7 +37,7 @@ ECPubKey.prototype.getAddress = function(version) {
}
ECPubKey.prototype.verify = function(hash, signature) {
return ecdsa.verify(hash, signature.r, signature.s, this.Q)
return ecdsa.verify(ecparams, hash, signature.r, signature.s, this.Q)
}
// Export functions

View file

@ -9,6 +9,9 @@ var networks = require('./networks')
var Address = require('./address')
var ECPubKey = require('./ecpubkey')
var sec = require('./sec')
var ecparams = sec('secp256k1')
function magicHash(message, network) {
var magicPrefix = new Buffer(network.magicPrefix)
var messageBuffer = new Buffer(message)
@ -27,7 +30,7 @@ function sign(key, message, network) {
var hash = magicHash(message, network)
var sig = key.sign(hash)
var e = BigInteger.fromBuffer(hash)
var i = ecdsa.calcPubKeyRecoveryParam(e, sig.r, sig.s, key.pub.Q)
var i = ecdsa.calcPubKeyRecoveryParam(ecparams, e, sig.r, sig.s, key.pub.Q)
return ecdsa.serializeSigCompact(sig.r, sig.s, i, key.pub.compressed)
}
@ -43,7 +46,7 @@ function verify(address, compactSig, message, network) {
var hash = magicHash(message, network)
var sig = ecdsa.parseSigCompact(compactSig)
var e = BigInteger.fromBuffer(hash)
var Q = ecdsa.recoverPubKey(e, sig.r, sig.s, sig.i)
var Q = ecdsa.recoverPubKey(ecparams, e, sig.r, sig.s, sig.i)
var pubKey = new ECPubKey(Q, sig.compressed)
return pubKey.getAddress(address.version).toString() === address.toString()

View file

@ -20,7 +20,7 @@ describe('ecdsa', function() {
var D = BigInteger.fromHex(f.D)
var h1 = crypto.sha256(f.message)
var k = ecdsa.deterministicGenerateK(h1, D)
var k = ecdsa.deterministicGenerateK(ecparams, h1, D)
assert.equal(k.toHex(), f.k)
})
})
@ -36,7 +36,7 @@ describe('ecdsa', function() {
var e = BigInteger.fromBuffer(hash)
var psig = ecdsa.parseSigCompact(signature)
var Qprime = ecdsa.recoverPubKey(e, psig.r, psig.s, psig.i)
var Qprime = ecdsa.recoverPubKey(ecparams, e, psig.r, psig.s, psig.i)
assert(Q.equals(Qprime))
})
})
@ -46,7 +46,7 @@ describe('ecdsa', function() {
fixtures.valid.forEach(function(f) {
var D = BigInteger.fromHex(f.D)
var hash = crypto.sha256(f.message)
var sig = ecdsa.sign(hash, D)
var sig = ecdsa.sign(ecparams, hash, D)
assert.equal(sig.r.toString(), f.signature.r)
assert.equal(sig.s.toString(), f.signature.s)
@ -55,7 +55,7 @@ describe('ecdsa', function() {
it('should sign with low S value', function() {
var hash = crypto.sha256('Vires in numeris')
var sig = ecdsa.sign(hash, BigInteger.ONE)
var sig = ecdsa.sign(ecparams, hash, BigInteger.ONE)
// See BIP62 for more information
var N_OVER_TWO = ecparams.getN().shiftRight(1)
@ -73,7 +73,7 @@ describe('ecdsa', function() {
var s = new BigInteger(f.signature.s)
var e = BigInteger.fromBuffer(crypto.sha256(f.message))
assert(ecdsa.verifyRaw(e, r, s, Q))
assert(ecdsa.verifyRaw(ecparams, e, r, s, Q))
})
})
})