2015-03-17 02:31:53 +01:00
|
|
|
|
var createHmac = require('create-hmac')
|
2015-08-11 09:01:47 +02:00
|
|
|
|
var typeforce = require('typeforce')
|
|
|
|
|
var types = require('./types')
|
2014-04-21 18:19:30 +02:00
|
|
|
|
|
2014-05-03 04:04:54 +02:00
|
|
|
|
var BigInteger = require('bigi')
|
2014-06-15 08:44:52 +02:00
|
|
|
|
var ECSignature = require('./ecsignature')
|
2014-04-23 23:45:28 +02:00
|
|
|
|
|
2014-12-08 01:24:16 +01:00
|
|
|
|
var ZERO = new Buffer([0])
|
|
|
|
|
var ONE = new Buffer([1])
|
|
|
|
|
|
2015-08-25 12:43:32 +02:00
|
|
|
|
var ecurve = require('ecurve')
|
|
|
|
|
var secp256k1 = ecurve.getCurveByName('secp256k1')
|
|
|
|
|
|
2014-06-21 14:25:09 +02:00
|
|
|
|
// https://tools.ietf.org/html/rfc6979#section-3.2
|
2015-08-25 12:43:32 +02:00
|
|
|
|
function deterministicGenerateK (hash, x, checkSig) {
|
2015-08-11 09:01:47 +02:00
|
|
|
|
typeforce(types.tuple(
|
|
|
|
|
types.Hash256bit,
|
2015-08-25 12:43:32 +02:00
|
|
|
|
types.Buffer256bit,
|
2015-08-11 09:01:47 +02:00
|
|
|
|
types.Function
|
|
|
|
|
), arguments)
|
2014-05-23 09:18:32 +02:00
|
|
|
|
|
|
|
|
|
var k = new Buffer(32)
|
|
|
|
|
var v = new Buffer(32)
|
2014-06-21 14:25:09 +02:00
|
|
|
|
|
2015-01-04 02:29:01 +01:00
|
|
|
|
// Step A, ignored as hash already provided
|
2014-06-21 14:25:09 +02:00
|
|
|
|
// Step B
|
2014-05-23 09:18:32 +02:00
|
|
|
|
v.fill(1)
|
|
|
|
|
|
2014-06-21 14:25:09 +02:00
|
|
|
|
// Step C
|
|
|
|
|
k.fill(0)
|
|
|
|
|
|
|
|
|
|
// Step D
|
2015-03-17 02:31:53 +01:00
|
|
|
|
k = createHmac('sha256', k)
|
2014-12-08 01:24:16 +01:00
|
|
|
|
.update(v)
|
|
|
|
|
.update(ZERO)
|
|
|
|
|
.update(x)
|
|
|
|
|
.update(hash)
|
|
|
|
|
.digest()
|
2014-06-21 14:25:09 +02:00
|
|
|
|
|
|
|
|
|
// Step E
|
2015-03-17 02:31:53 +01:00
|
|
|
|
v = createHmac('sha256', k).update(v).digest()
|
2014-05-23 09:18:32 +02:00
|
|
|
|
|
2014-06-21 14:25:09 +02:00
|
|
|
|
// Step F
|
2015-03-17 02:31:53 +01:00
|
|
|
|
k = createHmac('sha256', k)
|
2014-12-08 01:24:16 +01:00
|
|
|
|
.update(v)
|
|
|
|
|
.update(ONE)
|
|
|
|
|
.update(x)
|
|
|
|
|
.update(hash)
|
|
|
|
|
.digest()
|
2014-06-21 14:25:09 +02:00
|
|
|
|
|
|
|
|
|
// Step G
|
2015-03-17 02:31:53 +01:00
|
|
|
|
v = createHmac('sha256', k).update(v).digest()
|
2014-06-21 14:25:09 +02:00
|
|
|
|
|
|
|
|
|
// Step H1/H2a, ignored as tlen === qlen (256 bit)
|
|
|
|
|
// Step H2b
|
2015-03-17 02:31:53 +01:00
|
|
|
|
v = createHmac('sha256', k).update(v).digest()
|
2014-05-23 09:18:32 +02:00
|
|
|
|
|
2014-06-21 14:25:09 +02:00
|
|
|
|
var T = BigInteger.fromBuffer(v)
|
|
|
|
|
|
2015-01-04 02:46:37 +01:00
|
|
|
|
// Step H3, repeat until T is within the interval [1, n - 1] and is suitable for ECDSA
|
2015-08-25 12:43:32 +02:00
|
|
|
|
while (T.signum() <= 0 || T.compareTo(secp256k1.n) >= 0 || !checkSig(T)) {
|
2015-03-17 02:31:53 +01:00
|
|
|
|
k = createHmac('sha256', k)
|
2014-12-08 01:24:16 +01:00
|
|
|
|
.update(v)
|
|
|
|
|
.update(ZERO)
|
|
|
|
|
.digest()
|
|
|
|
|
|
2015-03-17 02:31:53 +01:00
|
|
|
|
v = createHmac('sha256', k).update(v).digest()
|
2014-06-21 14:25:09 +02:00
|
|
|
|
|
2015-01-04 02:46:37 +01:00
|
|
|
|
// Step H1/H2a, again, ignored as tlen === qlen (256 bit)
|
|
|
|
|
// Step H2b again
|
2015-03-17 02:31:53 +01:00
|
|
|
|
v = createHmac('sha256', k).update(v).digest()
|
2014-06-21 14:25:09 +02:00
|
|
|
|
T = BigInteger.fromBuffer(v)
|
|
|
|
|
}
|
2014-05-23 09:18:32 +02:00
|
|
|
|
|
2014-06-21 14:25:09 +02:00
|
|
|
|
return T
|
2014-05-23 09:18:32 +02:00
|
|
|
|
}
|
2014-04-23 23:45:28 +02:00
|
|
|
|
|
2015-08-25 12:43:32 +02:00
|
|
|
|
var N_OVER_TWO = secp256k1.n.shiftRight(1)
|
|
|
|
|
|
|
|
|
|
function sign (hash, d) {
|
|
|
|
|
typeforce(types.tuple(types.Hash256bit, types.BigInt), arguments)
|
2015-06-23 07:32:26 +02:00
|
|
|
|
|
2015-08-25 12:43:32 +02:00
|
|
|
|
var x = d.toBuffer(32)
|
2015-01-04 02:46:37 +01:00
|
|
|
|
var e = BigInteger.fromBuffer(hash)
|
2015-08-25 12:43:32 +02:00
|
|
|
|
var n = secp256k1.n
|
|
|
|
|
var G = secp256k1.G
|
2014-04-23 23:45:28 +02:00
|
|
|
|
|
2015-04-10 03:07:08 +02:00
|
|
|
|
var r, s
|
2015-08-25 12:43:32 +02:00
|
|
|
|
deterministicGenerateK(hash, x, function (k) {
|
2015-01-04 02:46:37 +01:00
|
|
|
|
var Q = G.multiply(k)
|
|
|
|
|
|
2015-08-25 12:43:32 +02:00
|
|
|
|
if (secp256k1.isInfinity(Q)) return false
|
2015-01-04 02:46:37 +01:00
|
|
|
|
|
|
|
|
|
r = Q.affineX.mod(n)
|
2015-04-10 03:07:08 +02:00
|
|
|
|
if (r.signum() === 0) return false
|
2015-01-04 02:46:37 +01:00
|
|
|
|
|
|
|
|
|
s = k.modInverse(n).multiply(e.add(d.multiply(r))).mod(n)
|
2015-04-10 03:07:08 +02:00
|
|
|
|
if (s.signum() === 0) return false
|
2014-04-23 23:45:28 +02:00
|
|
|
|
|
2015-01-04 02:46:37 +01:00
|
|
|
|
return true
|
|
|
|
|
})
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
2014-05-23 09:18:32 +02:00
|
|
|
|
// enforce low S values, see bip62: 'low s values in signatures'
|
|
|
|
|
if (s.compareTo(N_OVER_TWO) > 0) {
|
|
|
|
|
s = n.subtract(s)
|
2014-05-17 07:11:43 +02:00
|
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
2014-06-15 08:44:52 +02:00
|
|
|
|
return new ECSignature(r, s)
|
2014-05-23 09:18:32 +02:00
|
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
2015-08-25 12:43:32 +02:00
|
|
|
|
function verify (hash, signature, Q) {
|
2015-08-14 00:27:57 +02:00
|
|
|
|
typeforce(types.tuple(
|
2015-08-11 09:01:47 +02:00
|
|
|
|
types.Hash256bit,
|
|
|
|
|
types.ECSignature,
|
|
|
|
|
types.ECPoint
|
|
|
|
|
), arguments)
|
2015-06-23 07:32:26 +02:00
|
|
|
|
|
2015-08-25 12:43:32 +02:00
|
|
|
|
var n = secp256k1.n
|
|
|
|
|
var G = secp256k1.G
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
2014-05-24 08:25:38 +02:00
|
|
|
|
var r = signature.r
|
|
|
|
|
var s = signature.s
|
2014-05-23 09:18:32 +02:00
|
|
|
|
|
2014-07-30 07:01:43 +02:00
|
|
|
|
// 1.4.1 Enforce r and s are both integers in the interval [1, n − 1]
|
2014-07-29 15:45:10 +02:00
|
|
|
|
if (r.signum() <= 0 || r.compareTo(n) >= 0) return false
|
|
|
|
|
if (s.signum() <= 0 || s.compareTo(n) >= 0) return false
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
2015-04-10 09:22:00 +02:00
|
|
|
|
// 1.4.2 H = Hash(M), already done by the user
|
|
|
|
|
// 1.4.3 e = H
|
|
|
|
|
var e = BigInteger.fromBuffer(hash)
|
|
|
|
|
|
2015-04-10 09:20:22 +02:00
|
|
|
|
// Compute s^-1
|
|
|
|
|
var sInv = s.modInverse(n)
|
2014-05-24 06:33:02 +02:00
|
|
|
|
|
2014-07-30 07:01:43 +02:00
|
|
|
|
// 1.4.4 Compute u1 = es^−1 mod n
|
|
|
|
|
// u2 = rs^−1 mod n
|
2015-04-10 09:20:22 +02:00
|
|
|
|
var u1 = e.multiply(sInv).mod(n)
|
|
|
|
|
var u2 = r.multiply(sInv).mod(n)
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
2015-04-10 09:20:22 +02:00
|
|
|
|
// 1.4.5 Compute R = (xR, yR)
|
|
|
|
|
// R = u1G + u2Q
|
2014-07-30 07:01:43 +02:00
|
|
|
|
var R = G.multiplyTwo(u1, Q, u2)
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
2014-07-30 07:01:43 +02:00
|
|
|
|
// 1.4.5 (cont.) Enforce R is not at infinity
|
2015-08-25 12:43:32 +02:00
|
|
|
|
if (secp256k1.isInfinity(R)) return false
|
2014-07-30 07:01:43 +02:00
|
|
|
|
|
2015-04-10 09:20:22 +02:00
|
|
|
|
// 1.4.6 Convert the field element R.x to an integer
|
|
|
|
|
var xR = R.affineX
|
|
|
|
|
|
|
|
|
|
// 1.4.7 Set v = xR mod n
|
|
|
|
|
var v = xR.mod(n)
|
|
|
|
|
|
2015-03-02 03:25:09 +01:00
|
|
|
|
// 1.4.8 If v = r, output "valid", and if v != r, output "invalid"
|
2014-05-23 09:18:32 +02:00
|
|
|
|
return v.equals(r)
|
|
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
2014-05-17 07:11:43 +02:00
|
|
|
|
module.exports = {
|
2014-05-24 08:25:38 +02:00
|
|
|
|
deterministicGenerateK: deterministicGenerateK,
|
|
|
|
|
sign: sign,
|
2015-08-25 12:43:32 +02:00
|
|
|
|
verify: verify,
|
|
|
|
|
|
|
|
|
|
// TODO: remove
|
|
|
|
|
__curve: secp256k1
|
2014-05-17 07:11:43 +02:00
|
|
|
|
}
|