Browse Source

ecdsa: remove implicit ecparams

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
bcfd62fdb5
  1. 24
      src/ecdsa.js
  2. 2
      src/eckey.js
  3. 2
      src/ecpubkey.js
  4. 7
      src/message.js
  5. 10
      test/ecdsa.js

24
src/ecdsa.js

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

2
src/eckey.js

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

2
src/ecpubkey.js

@ -37,7 +37,7 @@ ECPubKey.prototype.getAddress = function(version) {
} }
ECPubKey.prototype.verify = function(hash, signature) { 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 // Export functions

7
src/message.js

@ -9,6 +9,9 @@ var networks = require('./networks')
var Address = require('./address') var Address = require('./address')
var ECPubKey = require('./ecpubkey') var ECPubKey = require('./ecpubkey')
var sec = require('./sec')
var ecparams = sec('secp256k1')
function magicHash(message, network) { function magicHash(message, network) {
var magicPrefix = new Buffer(network.magicPrefix) var magicPrefix = new Buffer(network.magicPrefix)
var messageBuffer = new Buffer(message) var messageBuffer = new Buffer(message)
@ -27,7 +30,7 @@ function sign(key, message, network) {
var hash = magicHash(message, network) var hash = magicHash(message, network)
var sig = key.sign(hash) var sig = key.sign(hash)
var e = BigInteger.fromBuffer(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) 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 hash = magicHash(message, network)
var sig = ecdsa.parseSigCompact(compactSig) var sig = ecdsa.parseSigCompact(compactSig)
var e = BigInteger.fromBuffer(hash) 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) var pubKey = new ECPubKey(Q, sig.compressed)
return pubKey.getAddress(address.version).toString() === address.toString() return pubKey.getAddress(address.version).toString() === address.toString()

10
test/ecdsa.js

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

Loading…
Cancel
Save