2014-03-31 05:47:47 +02:00
|
|
|
var sec = require('./jsbn/sec')
|
|
|
|
var rng = require('secure-random')
|
|
|
|
var BigInteger = require('./jsbn/jsbn')
|
2014-03-11 02:52:48 +01:00
|
|
|
var convert = require('./convert')
|
2014-03-31 05:47:47 +02:00
|
|
|
var HmacSHA256 = require('crypto-js/hmac-sha256')
|
|
|
|
var ECPointFp = require('./jsbn/ec').ECPointFp
|
|
|
|
var ecparams = sec("secp256k1")
|
|
|
|
var P_OVER_FOUR = null
|
2011-05-04 18:02:56 +02:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
function implShamirsTrick(P, k, Q, l) {
|
|
|
|
var m = Math.max(k.bitLength(), l.bitLength())
|
|
|
|
var Z = P.add2D(Q)
|
|
|
|
var R = P.curve.getInfinity()
|
2011-05-04 18:02:56 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
for (var i = m - 1; i >= 0; --i) {
|
2014-03-31 05:47:47 +02:00
|
|
|
R = R.twice2D()
|
2011-05-04 18:02:56 +02:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
R.z = BigInteger.ONE
|
2012-01-11 02:40:45 +01:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
if (k.testBit(i)) {
|
|
|
|
if (l.testBit(i)) {
|
2014-03-31 05:47:47 +02:00
|
|
|
R = R.add2D(Z)
|
2013-02-17 06:39:15 +01:00
|
|
|
} else {
|
2014-03-31 05:47:47 +02:00
|
|
|
R = R.add2D(P)
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
2012-01-11 02:40:45 +01:00
|
|
|
} else {
|
2013-02-17 06:39:15 +01:00
|
|
|
if (l.testBit(i)) {
|
2014-03-31 05:47:47 +02:00
|
|
|
R = R.add2D(Q)
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
2012-01-11 02:40:45 +01:00
|
|
|
}
|
|
|
|
}
|
2011-05-08 15:36:11 +02:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
return R
|
|
|
|
}
|
2011-05-04 18:02:56 +02:00
|
|
|
|
2014-01-04 19:26:03 +01:00
|
|
|
function deterministicGenerateK(hash,key) {
|
2014-03-31 05:47:47 +02:00
|
|
|
var vArr = []
|
|
|
|
var kArr = []
|
|
|
|
for (var i = 0;i < 32;i++) vArr.push(1)
|
|
|
|
for (var i = 0;i < 32;i++) kArr.push(0)
|
|
|
|
var v = convert.bytesToWordArray(vArr)
|
|
|
|
var k = convert.bytesToWordArray(kArr)
|
|
|
|
|
|
|
|
k = HmacSHA256(convert.bytesToWordArray(vArr.concat([0]).concat(key).concat(hash)), k)
|
|
|
|
v = HmacSHA256(v, k)
|
|
|
|
vArr = convert.wordArrayToBytes(v)
|
|
|
|
k = HmacSHA256(convert.bytesToWordArray(vArr.concat([1]).concat(key).concat(hash)), k)
|
|
|
|
v = HmacSHA256(v,k)
|
|
|
|
v = HmacSHA256(v,k)
|
|
|
|
vArr = convert.wordArrayToBytes(v)
|
|
|
|
return BigInteger.fromByteArrayUnsigned(vArr)
|
2014-01-04 19:26:03 +01:00
|
|
|
}
|
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
var ECDSA = {
|
|
|
|
getBigRandom: function (limit) {
|
2014-03-31 05:47:47 +02:00
|
|
|
return new BigInteger(limit.bitLength(), rng).
|
|
|
|
mod(limit.subtract(BigInteger.ONE)).
|
|
|
|
add(BigInteger.ONE)
|
2013-02-17 06:39:15 +01:00
|
|
|
},
|
|
|
|
sign: function (hash, priv) {
|
2014-03-31 05:47:47 +02:00
|
|
|
var d = priv
|
|
|
|
var n = ecparams.getN()
|
|
|
|
var e = BigInteger.fromByteArrayUnsigned(hash)
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-01-04 19:26:03 +01:00
|
|
|
var k = deterministicGenerateK(hash,priv.toByteArrayUnsigned())
|
2014-03-31 05:47:47 +02:00
|
|
|
var G = ecparams.getG()
|
|
|
|
var Q = G.multiply(k)
|
|
|
|
var r = Q.getX().toBigInteger().mod(n)
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var s = k.modInverse(n).multiply(e.add(d.multiply(r))).mod(n)
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
return ECDSA.serializeSig(r, s)
|
2013-02-17 06:39:15 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
verify: function (hash, sig, pubkey) {
|
2014-03-31 05:47:47 +02:00
|
|
|
var r,s
|
2014-03-03 04:27:19 +01:00
|
|
|
if (Array.isArray(sig)) {
|
2014-03-31 05:47:47 +02:00
|
|
|
var obj = ECDSA.parseSig(sig)
|
|
|
|
r = obj.r
|
|
|
|
s = obj.s
|
2013-02-17 06:39:15 +01:00
|
|
|
} else if ("object" === typeof sig && sig.r && sig.s) {
|
2014-03-31 05:47:47 +02:00
|
|
|
r = sig.r
|
|
|
|
s = sig.s
|
2013-02-17 06:39:15 +01:00
|
|
|
} else {
|
2014-03-31 05:47:47 +02:00
|
|
|
throw new Error("Invalid value for signature")
|
2012-01-11 02:40:45 +01:00
|
|
|
}
|
2011-05-04 18:02:56 +02:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var Q
|
2013-02-17 06:39:15 +01:00
|
|
|
if (pubkey instanceof ECPointFp) {
|
2014-03-31 05:47:47 +02:00
|
|
|
Q = pubkey
|
2014-03-03 04:27:19 +01:00
|
|
|
} else if (Array.isArray(pubkey)) {
|
2014-03-31 05:47:47 +02:00
|
|
|
Q = ECPointFp.decodeFrom(ecparams.getCurve(), pubkey)
|
2013-02-17 06:39:15 +01:00
|
|
|
} else {
|
2014-03-31 05:47:47 +02:00
|
|
|
throw new Error("Invalid format for pubkey value, must be byte array or ECPointFp")
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
var e = BigInteger.fromByteArrayUnsigned(hash)
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
return ECDSA.verifyRaw(e, r, s, Q)
|
2013-02-17 06:39:15 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
verifyRaw: function (e, r, s, Q) {
|
2014-03-31 05:47:47 +02:00
|
|
|
var n = ecparams.getN()
|
|
|
|
var G = ecparams.getG()
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
if (r.compareTo(BigInteger.ONE) < 0 || r.compareTo(n) >= 0) {
|
|
|
|
return false
|
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
if (s.compareTo(BigInteger.ONE) < 0 || s.compareTo(n) >= 0) {
|
|
|
|
return false
|
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var c = s.modInverse(n)
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var u1 = e.multiply(c).mod(n)
|
|
|
|
var u2 = r.multiply(c).mod(n)
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
|
|
// TODO(!!!): For some reason Shamir's trick isn't working with
|
|
|
|
// signed message verification!? Probably an implementation
|
|
|
|
// error!
|
2014-03-31 05:47:47 +02:00
|
|
|
//var point = implShamirsTrick(G, u1, Q, u2)
|
|
|
|
var point = G.multiply(u1).add(Q.multiply(u2))
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var v = point.getX().toBigInteger().mod(n)
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
return v.equals(r)
|
2013-02-17 06:39:15 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize a signature into DER format.
|
|
|
|
*
|
|
|
|
* Takes two BigIntegers representing r and s and returns a byte array.
|
|
|
|
*/
|
|
|
|
serializeSig: function (r, s) {
|
2014-03-31 05:47:47 +02:00
|
|
|
var rBa = r.toByteArraySigned()
|
|
|
|
var sBa = s.toByteArraySigned()
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var sequence = []
|
2013-02-17 06:39:15 +01:00
|
|
|
sequence.push(0x02); // INTEGER
|
2014-03-31 05:47:47 +02:00
|
|
|
sequence.push(rBa.length)
|
|
|
|
sequence = sequence.concat(rBa)
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
|
|
sequence.push(0x02); // INTEGER
|
2014-03-31 05:47:47 +02:00
|
|
|
sequence.push(sBa.length)
|
|
|
|
sequence = sequence.concat(sBa)
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
sequence.unshift(sequence.length)
|
2013-02-17 06:39:15 +01:00
|
|
|
sequence.unshift(0x30); // SEQUENCE
|
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
return sequence
|
2013-02-17 06:39:15 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a byte array containing a DER-encoded signature.
|
|
|
|
*
|
|
|
|
* This function will return an object of the form:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* r: BigInteger,
|
|
|
|
* s: BigInteger
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
parseSig: function (sig) {
|
2014-03-31 05:47:47 +02:00
|
|
|
var cursor
|
|
|
|
if (sig[0] != 0x30) {
|
|
|
|
throw new Error("Signature not a valid DERSequence")
|
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
cursor = 2
|
|
|
|
if (sig[cursor] != 0x02) {
|
|
|
|
throw new Error("First element in signature must be a DERInteger")
|
|
|
|
}
|
|
|
|
var rBa = sig.slice(cursor+2, cursor+2+sig[cursor+1])
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
cursor += 2+sig[cursor+1]
|
|
|
|
if (sig[cursor] != 0x02) {
|
|
|
|
throw new Error("Second element in signature must be a DERInteger")
|
|
|
|
}
|
|
|
|
var sBa = sig.slice(cursor+2, cursor+2+sig[cursor+1])
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
cursor += 2+sig[cursor+1]
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
|
|
//if (cursor != sig.length)
|
2014-03-31 05:47:47 +02:00
|
|
|
// throw new Error("Extra bytes in signature")
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var r = BigInteger.fromByteArrayUnsigned(rBa)
|
|
|
|
var s = BigInteger.fromByteArrayUnsigned(sBa)
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
return {r: r, s: s}
|
2013-02-17 06:39:15 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
parseSigCompact: function (sig) {
|
|
|
|
if (sig.length !== 65) {
|
2014-03-31 05:47:47 +02:00
|
|
|
throw new Error("Signature has the wrong length")
|
2012-01-11 02:40:45 +01:00
|
|
|
}
|
2011-08-26 21:43:08 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
// Signature is prefixed with a type byte storing three bits of
|
|
|
|
// information.
|
2014-03-31 05:47:47 +02:00
|
|
|
var i = sig[0] - 27
|
2013-02-17 06:39:15 +01:00
|
|
|
if (i < 0 || i > 7) {
|
2014-03-31 05:47:47 +02:00
|
|
|
throw new Error("Invalid signature type")
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
2012-08-16 00:25:06 +02:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var n = ecparams.getN()
|
|
|
|
var r = BigInteger.fromByteArrayUnsigned(sig.slice(1, 33)).mod(n)
|
|
|
|
var s = BigInteger.fromByteArrayUnsigned(sig.slice(33, 65)).mod(n)
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
return {r: r, s: s, i: i}
|
2013-02-17 06:39:15 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recover a public key from a signature.
|
|
|
|
*
|
|
|
|
* See SEC 1: Elliptic Curve Cryptography, section 4.1.6, "Public
|
|
|
|
* Key Recovery Operation".
|
|
|
|
*
|
|
|
|
* http://www.secg.org/download/aid-780/sec1-v2.pdf
|
|
|
|
*/
|
|
|
|
recoverPubKey: function (r, s, hash, i) {
|
|
|
|
// The recovery parameter i has two bits.
|
2014-03-31 05:47:47 +02:00
|
|
|
i = i & 3
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
|
|
// The less significant bit specifies whether the y coordinate
|
|
|
|
// of the compressed point is even or not.
|
2014-03-31 05:47:47 +02:00
|
|
|
var isYEven = i & 1
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
|
|
// The more significant bit specifies whether we should use the
|
|
|
|
// first or second candidate key.
|
2014-03-31 05:47:47 +02:00
|
|
|
var isSecondKey = i >> 1
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var n = ecparams.getN()
|
|
|
|
var G = ecparams.getG()
|
|
|
|
var curve = ecparams.getCurve()
|
|
|
|
var p = curve.getQ()
|
|
|
|
var a = curve.getA().toBigInteger()
|
|
|
|
var b = curve.getB().toBigInteger()
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
|
|
// We precalculate (p + 1) / 4 where p is if the field order
|
|
|
|
if (!P_OVER_FOUR) {
|
2014-03-31 05:47:47 +02:00
|
|
|
P_OVER_FOUR = p.add(BigInteger.ONE).divide(BigInteger.valueOf(4))
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
2011-08-26 21:43:08 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
// 1.1 Compute x
|
2014-03-31 05:47:47 +02:00
|
|
|
var x = isSecondKey ? r.add(n) : r
|
2011-08-26 21:43:08 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
// 1.3 Convert x to point
|
2014-03-31 05:47:47 +02:00
|
|
|
var alpha = x.multiply(x).multiply(x).add(a.multiply(x)).add(b).mod(p)
|
|
|
|
var beta = alpha.modPow(P_OVER_FOUR, p)
|
2011-08-26 21:43:08 +02:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
// var xorOdd = beta.isEven() ? (i % 2) : ((i+1) % 2)
|
2013-02-17 06:39:15 +01:00
|
|
|
// If beta is even, but y isn't or vice versa, then convert it,
|
|
|
|
// otherwise we're done and y == beta.
|
2014-03-31 05:47:47 +02:00
|
|
|
var y = (beta.isEven() ? !isYEven : isYEven) ? beta : p.subtract(beta)
|
2011-08-26 21:43:08 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
// 1.4 Check that nR is at infinity
|
2014-03-31 05:47:47 +02:00
|
|
|
var R = new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y))
|
|
|
|
R.validate()
|
2011-05-04 18:02:56 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
// 1.5 Compute e from M
|
2014-03-31 05:47:47 +02:00
|
|
|
var e = BigInteger.fromByteArrayUnsigned(hash)
|
|
|
|
var eNeg = BigInteger.ZERO.subtract(e).mod(n)
|
2011-05-04 18:02:56 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
// 1.6 Compute Q = r^-1 (sR - eG)
|
2014-03-31 05:47:47 +02:00
|
|
|
var rInv = r.modInverse(n)
|
|
|
|
var Q = implShamirsTrick(R, s, G, eNeg).multiply(rInv)
|
2012-01-11 02:40:45 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
Q.validate()
|
2013-02-17 06:39:15 +01:00
|
|
|
if (!ECDSA.verifyRaw(e, r, s, Q)) {
|
2014-03-31 05:47:47 +02:00
|
|
|
throw new Error("Pubkey recovery unsuccessful")
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
2012-08-17 01:38:29 +02:00
|
|
|
|
2014-03-29 07:44:03 +01:00
|
|
|
return Q
|
2013-02-17 06:39:15 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate pubkey extraction parameter.
|
|
|
|
*
|
|
|
|
* When extracting a pubkey from a signature, we have to
|
|
|
|
* distinguish four different cases. Rather than putting this
|
|
|
|
* burden on the verifier, Bitcoin includes a 2-bit value with the
|
|
|
|
* signature.
|
|
|
|
*
|
|
|
|
* This function simply tries all four cases and returns the value
|
|
|
|
* that resulted in a successful pubkey recovery.
|
|
|
|
*/
|
2014-03-28 18:26:19 +01:00
|
|
|
calcPubKeyRecoveryParam: function (origPubKey, r, s, hash) {
|
2013-02-17 06:39:15 +01:00
|
|
|
for (var i = 0; i < 4; i++) {
|
2014-03-28 18:26:19 +01:00
|
|
|
var pubKey = ECDSA.recoverPubKey(r, s, hash, i)
|
|
|
|
|
2014-03-29 07:44:03 +01:00
|
|
|
if (pubKey.equals(origPubKey)) {
|
2014-03-28 18:26:19 +01:00
|
|
|
return i
|
2013-03-02 18:28:13 +01:00
|
|
|
}
|
2012-01-11 02:40:45 +01:00
|
|
|
}
|
2013-03-02 18:28:13 +01:00
|
|
|
|
2014-03-28 18:26:19 +01:00
|
|
|
throw new Error("Unable to find valid recovery factor")
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2012-01-11 02:40:45 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
module.exports = ECDSA
|