ecdsa: fix indentation

This commit is contained in:
Daniel Cousens 2014-05-23 17:18:32 +10:00
parent d05d661aea
commit 087ca551f5

View file

@ -4,7 +4,7 @@ var crypto = require('./crypto')
var BigInteger = require('bigi')
var ECPointFp = require('./ec').ECPointFp
function deterministicGenerateK(ecparams, hash, D) {
function deterministicGenerateK(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')
@ -28,9 +28,9 @@ var ECPointFp = require('./ec').ECPointFp
assert(kB.compareTo(ecparams.getN()) < 0, 'Invalid k value')
return kB
}
}
function sign(ecparams, hash, D) {
function sign(ecparams, hash, D) {
var k = deterministicGenerateK(ecparams, hash, D)
var n = ecparams.getN()
@ -52,15 +52,15 @@ var ECPointFp = require('./ec').ECPointFp
}
return {r: r, s: s}
}
}
function verify(ecparams, hash, r, s, Q) {
function verify(ecparams, hash, r, s, Q) {
var e = BigInteger.fromBuffer(hash)
return verifyRaw(ecparams, e, r, s, Q)
}
}
function verifyRaw(ecparams, e, r, s, Q) {
function verifyRaw(ecparams, e, r, s, Q) {
var n = ecparams.getN()
var G = ecparams.getG()
@ -80,14 +80,14 @@ var ECPointFp = require('./ec').ECPointFp
var v = point.getX().toBigInteger().mod(n)
return v.equals(r)
}
}
/**
/**
* Serialize a signature into DER format.
*
* Takes two BigIntegers representing r and s and returns a byte array.
*/
function serializeSig(r, s) {
function serializeSig(r, s) {
var rBa = r.toByteArraySigned()
var sBa = s.toByteArraySigned()
@ -104,9 +104,9 @@ var ECPointFp = require('./ec').ECPointFp
sequence.unshift(0x30); // SEQUENCE
return sequence
}
}
/**
/**
* Parses a buffer containing a DER-encoded signature.
*
* This function will return an object of the form:
@ -116,7 +116,7 @@ var ECPointFp = require('./ec').ECPointFp
* s: BigInteger
* }
*/
function parseSig(buffer) {
function parseSig(buffer) {
assert.equal(buffer.readUInt8(0), 0x30, 'Not a DER sequence')
assert.equal(buffer.readUInt8(1), buffer.length - 2, 'Invalid sequence length')
@ -133,9 +133,9 @@ var ECPointFp = require('./ec').ECPointFp
r: BigInteger.fromByteArraySigned(rB),
s: BigInteger.fromByteArraySigned(sB)
}
}
}
function serializeSigCompact(r, s, i, compressed) {
function serializeSigCompact(r, s, i, compressed) {
if (compressed) {
i += 4
}
@ -148,9 +148,9 @@ var ECPointFp = require('./ec').ECPointFp
s.toBuffer(32).copy(buffer, 33)
return buffer
}
}
function parseSigCompact(buffer) {
function parseSigCompact(buffer) {
assert.equal(buffer.length, 65, 'Invalid signature length')
var i = buffer.readUInt8(0) - 27
@ -170,9 +170,9 @@ var ECPointFp = require('./ec').ECPointFp
i: i,
compressed: compressed
}
}
}
/**
/**
* Recover a public key from a signature.
*
* See SEC 1: Elliptic Curve Cryptography, section 4.1.6, "Public
@ -180,7 +180,7 @@ var ECPointFp = require('./ec').ECPointFp
*
* http://www.secg.org/download/aid-780/sec1-v2.pdf
*/
function recoverPubKey(ecparams, e, r, s, i) {
function recoverPubKey(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
@ -233,9 +233,9 @@ var ECPointFp = require('./ec').ECPointFp
}
return Q
}
}
/**
/**
* Calculate pubkey extraction parameter.
*
* When extracting a pubkey from a signature, we have to
@ -246,7 +246,7 @@ var ECPointFp = require('./ec').ECPointFp
* This function simply tries all four cases and returns the value
* that resulted in a successful pubkey recovery.
*/
function calcPubKeyRecoveryParam(ecparams, e, r, s, Q) {
function calcPubKeyRecoveryParam(ecparams, e, r, s, Q) {
for (var i = 0; i < 4; i++) {
var Qprime = recoverPubKey(ecparams, e, r, s, i)
@ -256,17 +256,17 @@ var ECPointFp = require('./ec').ECPointFp
}
throw new Error('Unable to find valid recovery factor')
}
}
module.exports = {
calcPubKeyRecoveryParam: calcPubKeyRecoveryParam,
deterministicGenerateK: deterministicGenerateK,
recoverPubKey: recoverPubKey,
sign: sign,
verify: verify,
verifyRaw: verifyRaw,
serializeSig: serializeSig,
parseSig: parseSig,
serializeSigCompact: serializeSigCompact,
parseSigCompact: parseSigCompact
calcPubKeyRecoveryParam: calcPubKeyRecoveryParam,
deterministicGenerateK: deterministicGenerateK,
recoverPubKey: recoverPubKey,
sign: sign,
verify: verify,
verifyRaw: verifyRaw,
serializeSig: serializeSig,
parseSig: parseSig,
serializeSigCompact: serializeSigCompact,
parseSigCompact: parseSigCompact
}