From 6020c0740d2b945e6ef8635aa2873af80e598295 Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Wed, 4 Jun 2014 13:57:07 +1000
Subject: [PATCH 1/8] JSHint: add config

---
 jshint.json  | 18 ++++++++++++++++++
 package.json |  4 +++-
 2 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 jshint.json

diff --git a/jshint.json b/jshint.json
new file mode 100644
index 0000000..0823462
--- /dev/null
+++ b/jshint.json
@@ -0,0 +1,18 @@
+{
+  "asi": true,
+  "camelcase": true,
+  "freeze": true,
+  "immed": true,
+  "indent": 2,
+  "latedef": true,
+  "maxcomplexity": 10,
+  "noarg": true,
+  "noempty": true,
+  "nonbsp": true,
+  "node": true,
+  "nonew": true,
+  "undef": true,
+  "unused": true,
+  "strict": false,
+  "trailing": true
+}
diff --git a/package.json b/package.json
index 3f89c32..5a78b4a 100644
--- a/package.json
+++ b/package.json
@@ -23,7 +23,8 @@
     "mocha-lcov-reporter": "0.0.1",
     "helloblock-js": "^0.2.1",
     "request": "~2.34.0",
-    "browserify": "~4.1.5"
+    "browserify": "~4.1.5",
+    "jshint": "2.5.1"
   },
   "testling": {
     "browsers": [
@@ -42,6 +43,7 @@
     "unit": "./node_modules/.bin/istanbul test ./node_modules/.bin/_mocha -- --reporter list `find test -maxdepth 1 -not -type d`",
     "test": "npm run-script unit",
     "integration": "./node_modules/.bin/_mocha --reporter list test/integration/*.js",
+    "jshint": "./node_modules/.bin/jshint --config jshint.json src/*.js ; true",
     "coverage": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --reporter list test/*.js",
     "coveralls": "npm run-script coverage && node ./node_modules/.bin/coveralls < coverage/lcov.info",
     "compile": "./node_modules/.bin/browserify ./src/index.js -s Bitcoin | ./node_modules/.bin/uglifyjs > bitcoinjs-min.js"

From e735a9182b241f52a19ea3739141ee9ccece168a Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Wed, 4 Jun 2014 13:55:09 +1000
Subject: [PATCH 2/8] ec: use x1/x2, y1/y2 variables

No logic change, just using the variables that are defined instead of
repeating code.
---
 src/ec.js | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/src/ec.js b/src/ec.js
index f68977c..83ebc3a 100644
--- a/src/ec.js
+++ b/src/ec.js
@@ -117,10 +117,15 @@ function pointFpAdd(b) {
     if(this.isInfinity()) return b;
     if(b.isInfinity()) return this;
 
+    var x1 = this.x.toBigInteger()
+    var y1 = this.y.toBigInteger()
+    var x2 = b.x.toBigInteger()
+    var y2 = b.y.toBigInteger()
+
     // u = Y2 * Z1 - Y1 * Z2
-    var u = b.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(b.z)).mod(this.curve.q);
+    var u = y2.multiply(this.z).subtract(y1.multiply(b.z)).mod(this.curve.q)
     // v = X2 * Z1 - X1 * Z2
-    var v = b.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(b.z)).mod(this.curve.q);
+    var v = x2.multiply(this.z).subtract(x1.multiply(b.z)).mod(this.curve.q)
 
     if(v.signum() === 0) {
         if(u.signum() === 0) {
@@ -129,11 +134,6 @@ function pointFpAdd(b) {
 	return this.curve.getInfinity(); // this = -b, so infinity
     }
 
-    var x1 = this.x.toBigInteger();
-    var y1 = this.y.toBigInteger();
-    var x2 = b.x.toBigInteger();
-    var y2 = b.y.toBigInteger();
-
     var v2 = v.square();
     var v3 = v2.multiply(v);
     var x1v2 = x1.multiply(v2);

From f5005299d14c8ddb99684e3ffacd68c5c6aad882 Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Wed, 4 Jun 2014 14:07:29 +1000
Subject: [PATCH 3/8] JSHint: remove defined but never used

---
 src/ec.js          |  1 -
 src/index.js       |  1 -
 src/transaction.js | 11 +++++------
 src/wallet.js      |  7 ++++---
 4 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/src/ec.js b/src/ec.js
index 83ebc3a..38e693d 100644
--- a/src/ec.js
+++ b/src/ec.js
@@ -6,7 +6,6 @@ var assert = require('assert')
 var BigInteger = require('bigi')
 
 // constants
-var TWO = BigInteger.valueOf(2)
 var THREE = BigInteger.valueOf(3)
 
 function ECFieldElementFp(q,x) {
diff --git a/src/index.js b/src/index.js
index fb4c80b..69ea066 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,5 +1,4 @@
 var ec = require('./ec')
-var Key = require('./eckey')
 var T = require('./transaction')
 
 module.exports = {
diff --git a/src/transaction.js b/src/transaction.js
index 0135a1b..be0d0bb 100644
--- a/src/transaction.js
+++ b/src/transaction.js
@@ -1,16 +1,15 @@
 // FIXME: To all ye that enter here, be weary of Buffers, Arrays and Hex interchanging between the outpoints
 
 var assert = require('assert')
-var Address = require('./address')
-var BigInteger = require('bigi')
 var bufferutils = require('./bufferutils')
-var Script = require('./script')
-var convert = require('./convert')
 var crypto = require('./crypto')
-var ECKey = require('./eckey')
 var ecdsa = require('./ecdsa')
 var opcodes = require('./opcodes')
 
+var Address = require('./address')
+var Script = require('./script')
+var ECKey = require('./eckey')
+
 var DEFAULT_SEQUENCE = 0xffffffff
 
 function Transaction(doc) {
@@ -154,7 +153,7 @@ Transaction.prototype.toBuffer = function () {
   writeUInt32(this.version)
   writeVarInt(this.ins.length)
 
-  this.ins.forEach(function(txin, i) {
+  this.ins.forEach(function(txin) {
     var hash = new Buffer(txin.outpoint.hash, 'hex') // FIXME: Performance: convert on tx.addInput instead
 
     // TxHash hex is big-endian, we need little-endian
diff --git a/src/wallet.js b/src/wallet.js
index 2616272..b144206 100644
--- a/src/wallet.js
+++ b/src/wallet.js
@@ -205,11 +205,12 @@ function Wallet(seed, network) {
     return tx
   }
 
-  function getCandidateOutputs(value){
+  function getCandidateOutputs() {
     var unspent = []
-    for (var key in me.outputs){
+
+    for (var key in me.outputs) {
       var output = me.outputs[key]
-      if(!output.spend) unspent.push(output)
+      if (!output.spend) unspent.push(output)
     }
 
     var sortByValueDesc = unspent.sort(function(o1, o2){

From 8a2dcaaaf506d0db36671ea8edf158269683c96f Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Wed, 4 Jun 2014 15:55:25 +1000
Subject: [PATCH 4/8] JSHint: add max line length 120

---
 jshint.json | 1 +
 1 file changed, 1 insertion(+)

diff --git a/jshint.json b/jshint.json
index 0823462..ab6fd1b 100644
--- a/jshint.json
+++ b/jshint.json
@@ -6,6 +6,7 @@
   "indent": 2,
   "latedef": true,
   "maxcomplexity": 10,
+  "maxlen": 120,
   "noarg": true,
   "noempty": true,
   "nonbsp": true,

From 79c7b68d35aeb8dd65539f098b281985c29e8184 Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Wed, 4 Jun 2014 16:04:33 +1000
Subject: [PATCH 5/8] ec: white space fixes

It is advised to use ?w=0 in comparing this commit.
---
 src/ec.js | 252 +++++++++++++++++++++++++++---------------------------
 1 file changed, 128 insertions(+), 124 deletions(-)

diff --git a/src/ec.js b/src/ec.js
index 38e693d..2981817 100644
--- a/src/ec.js
+++ b/src/ec.js
@@ -9,42 +9,42 @@ var BigInteger = require('bigi')
 var THREE = BigInteger.valueOf(3)
 
 function ECFieldElementFp(q,x) {
-    this.x = x;
-    // TODO if(x.compareTo(q) >= 0) error
-    this.q = q;
+  this.x = x;
+  // TODO if (x.compareTo(q) >= 0) error
+  this.q = q;
 }
 
 function feFpEquals(other) {
-    if(other == this) return true;
-    return (this.q.equals(other.q) && this.x.equals(other.x));
+  if (other == this) return true;
+  return (this.q.equals(other.q) && this.x.equals(other.x));
 }
 
 function feFpToBigInteger() {
-    return this.x;
+  return this.x;
 }
 
 function feFpNegate() {
-    return new ECFieldElementFp(this.q, this.x.negate().mod(this.q));
+  return new ECFieldElementFp(this.q, this.x.negate().mod(this.q));
 }
 
 function feFpAdd(b) {
-    return new ECFieldElementFp(this.q, this.x.add(b.toBigInteger()).mod(this.q));
+  return new ECFieldElementFp(this.q, this.x.add(b.toBigInteger()).mod(this.q));
 }
 
 function feFpSubtract(b) {
-    return new ECFieldElementFp(this.q, this.x.subtract(b.toBigInteger()).mod(this.q));
+  return new ECFieldElementFp(this.q, this.x.subtract(b.toBigInteger()).mod(this.q));
 }
 
 function feFpMultiply(b) {
-    return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger()).mod(this.q));
+  return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger()).mod(this.q));
 }
 
 function feFpSquare() {
-    return new ECFieldElementFp(this.q, this.x.square().mod(this.q));
+  return new ECFieldElementFp(this.q, this.x.square().mod(this.q));
 }
 
 function feFpDivide(b) {
-    return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger().modInverse(this.q)).mod(this.q));
+  return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger().modInverse(this.q)).mod(this.q));
 }
 
 ECFieldElementFp.prototype.equals = feFpEquals;
@@ -61,151 +61,156 @@ ECFieldElementFp.prototype.divide = feFpDivide;
 
 // constructor
 function ECPointFp(curve,x,y,z) {
-    this.curve = curve;
-    this.x = x;
-    this.y = y;
-    // Projective coordinates: either zinv == null or z * zinv == 1
-    // z and zinv are just BigIntegers, not fieldElements
-    if(z == null) {
-      this.z = BigInteger.ONE;
-    }
-    else {
-      this.z = z;
-    }
-    this.zinv = null;
-    //TODO: compression flag
+  this.curve = curve;
+  this.x = x;
+  this.y = y;
+  // Projective coordinates: either zinv == null or z * zinv == 1
+  // z and zinv are just BigIntegers, not fieldElements
+  if (z == null) {
+    this.z = BigInteger.ONE;
+  }
+  else {
+    this.z = z;
+  }
+  this.zinv = null;
+  //TODO: compression flag
 }
 
 function pointFpGetX() {
-    if(this.zinv == null) {
-      this.zinv = this.z.modInverse(this.curve.q);
-    }
-    return this.curve.fromBigInteger(this.x.toBigInteger().multiply(this.zinv).mod(this.curve.q));
+  if (this.zinv == null) {
+    this.zinv = this.z.modInverse(this.curve.q);
+  }
+  return this.curve.fromBigInteger(this.x.toBigInteger().multiply(this.zinv).mod(this.curve.q));
 }
 
 function pointFpGetY() {
-    if(this.zinv == null) {
-      this.zinv = this.z.modInverse(this.curve.q);
-    }
-    return this.curve.fromBigInteger(this.y.toBigInteger().multiply(this.zinv).mod(this.curve.q));
+  if (this.zinv == null) {
+    this.zinv = this.z.modInverse(this.curve.q);
+  }
+  return this.curve.fromBigInteger(this.y.toBigInteger().multiply(this.zinv).mod(this.curve.q));
 }
 
 function pointFpEquals(other) {
-    if(other == this) return true;
-    if(this.isInfinity()) return other.isInfinity();
-    if(other.isInfinity()) return this.isInfinity();
-    var u, v;
-    // u = Y2 * Z1 - Y1 * Z2
-    u = other.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(other.z)).mod(this.curve.q);
-    if (u.signum() !== 0) return false;
-    // v = X2 * Z1 - X1 * Z2
-    v = other.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(other.z)).mod(this.curve.q);
-    return v.signum() === 0;
+  if (other == this) return true;
+  if (this.isInfinity()) return other.isInfinity();
+  if (other.isInfinity()) return this.isInfinity();
+
+  var u, v;
+  // u = Y2 * Z1 - Y1 * Z2
+  u = other.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(other.z)).mod(this.curve.q);
+  if (u.signum() !== 0) return false;
+  // v = X2 * Z1 - X1 * Z2
+  v = other.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(other.z)).mod(this.curve.q);
+  return v.signum() === 0;
 }
 
 function pointFpIsInfinity() {
-    if ((this.x == null) && (this.y == null)) return true;
-    return this.z.signum() === 0 && this.y.toBigInteger().signum() !== 0;
+  if ((this.x == null) && (this.y == null)) return true;
+  return this.z.signum() === 0 && this.y.toBigInteger().signum() !== 0;
 }
 
 function pointFpNegate() {
-    return new ECPointFp(this.curve, this.x, this.y.negate(), this.z);
+  return new ECPointFp(this.curve, this.x, this.y.negate(), this.z);
 }
 
 function pointFpAdd(b) {
-    if(this.isInfinity()) return b;
-    if(b.isInfinity()) return this;
+  if (this.isInfinity()) return b;
+  if (b.isInfinity()) return this;
 
-    var x1 = this.x.toBigInteger()
-    var y1 = this.y.toBigInteger()
-    var x2 = b.x.toBigInteger()
-    var y2 = b.y.toBigInteger()
+  var x1 = this.x.toBigInteger()
+  var y1 = this.y.toBigInteger()
+  var x2 = b.x.toBigInteger()
+  var y2 = b.y.toBigInteger()
 
-    // u = Y2 * Z1 - Y1 * Z2
-    var u = y2.multiply(this.z).subtract(y1.multiply(b.z)).mod(this.curve.q)
-    // v = X2 * Z1 - X1 * Z2
-    var v = x2.multiply(this.z).subtract(x1.multiply(b.z)).mod(this.curve.q)
+  // u = Y2 * Z1 - Y1 * Z2
+  var u = y2.multiply(this.z).subtract(y1.multiply(b.z)).mod(this.curve.q)
+  // v = X2 * Z1 - X1 * Z2
+  var v = x2.multiply(this.z).subtract(x1.multiply(b.z)).mod(this.curve.q)
 
-    if(v.signum() === 0) {
-        if(u.signum() === 0) {
-            return this.twice(); // this == b, so double
-        }
-	return this.curve.getInfinity(); // this = -b, so infinity
-    }
+  if (v.signum() === 0) {
+      if (u.signum() === 0) {
+          return this.twice(); // this == b, so double
+      }
 
-    var v2 = v.square();
-    var v3 = v2.multiply(v);
-    var x1v2 = x1.multiply(v2);
-    var zu2 = u.square().multiply(this.z);
+      return this.curve.getInfinity(); // this = -b, so infinity
+  }
 
-    // x3 = v * (z2 * (z1 * u^2 - 2 * x1 * v^2) - v^3)
-    var x3 = zu2.subtract(x1v2.shiftLeft(1)).multiply(b.z).subtract(v3).multiply(v).mod(this.curve.q);
-    // y3 = z2 * (3 * x1 * u * v^2 - y1 * v^3 - z1 * u^3) + u * v^3
-    var y3 = x1v2.multiply(THREE).multiply(u).subtract(y1.multiply(v3)).subtract(zu2.multiply(u)).multiply(b.z).add(u.multiply(v3)).mod(this.curve.q);
-    // z3 = v^3 * z1 * z2
-    var z3 = v3.multiply(this.z).multiply(b.z).mod(this.curve.q);
+  var v2 = v.square();
+  var v3 = v2.multiply(v);
+  var x1v2 = x1.multiply(v2);
+  var zu2 = u.square().multiply(this.z);
 
-    return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
+  // x3 = v * (z2 * (z1 * u^2 - 2 * x1 * v^2) - v^3)
+  var x3 = zu2.subtract(x1v2.shiftLeft(1)).multiply(b.z).subtract(v3).multiply(v).mod(this.curve.q);
+  // y3 = z2 * (3 * x1 * u * v^2 - y1 * v^3 - z1 * u^3) + u * v^3
+  var y3 = x1v2.multiply(THREE).multiply(u).subtract(y1.multiply(v3)).subtract(zu2.multiply(u)).multiply(b.z).add(u.multiply(v3)).mod(this.curve.q);
+  // z3 = v^3 * z1 * z2
+  var z3 = v3.multiply(this.z).multiply(b.z).mod(this.curve.q);
+
+  return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
 }
 
 function pointFpTwice() {
-    if(this.isInfinity()) return this;
-    if(this.y.toBigInteger().signum() === 0) return this.curve.getInfinity();
+  if (this.isInfinity()) return this;
+  if (this.y.toBigInteger().signum() === 0) return this.curve.getInfinity();
 
-    var x1 = this.x.toBigInteger();
-    var y1 = this.y.toBigInteger();
+  var x1 = this.x.toBigInteger();
+  var y1 = this.y.toBigInteger();
 
-    var y1z1 = y1.multiply(this.z);
-    var y1sqz1 = y1z1.multiply(y1).mod(this.curve.q);
-    var a = this.curve.a.toBigInteger();
+  var y1z1 = y1.multiply(this.z);
+  var y1sqz1 = y1z1.multiply(y1).mod(this.curve.q);
+  var a = this.curve.a.toBigInteger();
 
-    // w = 3 * x1^2 + a * z1^2
-    var w = x1.square().multiply(THREE);
-    if(a.signum() !== 0) {
-      w = w.add(this.z.square().multiply(a));
-    }
-    w = w.mod(this.curve.q);
-    // x3 = 2 * y1 * z1 * (w^2 - 8 * x1 * y1^2 * z1)
-    var x3 = w.square().subtract(x1.shiftLeft(3).multiply(y1sqz1)).shiftLeft(1).multiply(y1z1).mod(this.curve.q);
-    // y3 = 4 * y1^2 * z1 * (3 * w * x1 - 2 * y1^2 * z1) - w^3
-    var y3 = w.multiply(THREE).multiply(x1).subtract(y1sqz1.shiftLeft(1)).shiftLeft(2).multiply(y1sqz1).subtract(w.pow(3)).mod(this.curve.q);
-    // z3 = 8 * (y1 * z1)^3
-    var z3 = y1z1.pow(3).shiftLeft(3).mod(this.curve.q);
+  // w = 3 * x1^2 + a * z1^2
+  var w = x1.square().multiply(THREE);
 
-    return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
+  if (a.signum() !== 0) {
+    w = w.add(this.z.square().multiply(a));
+  }
+
+  w = w.mod(this.curve.q);
+  // x3 = 2 * y1 * z1 * (w^2 - 8 * x1 * y1^2 * z1)
+  var x3 = w.square().subtract(x1.shiftLeft(3).multiply(y1sqz1)).shiftLeft(1).multiply(y1z1).mod(this.curve.q);
+  // y3 = 4 * y1^2 * z1 * (3 * w * x1 - 2 * y1^2 * z1) - w^3
+  var y3 = w.multiply(THREE).multiply(x1).subtract(y1sqz1.shiftLeft(1)).shiftLeft(2).multiply(y1sqz1).subtract(w.pow(3)).mod(this.curve.q);
+  // z3 = 8 * (y1 * z1)^3
+  var z3 = y1z1.pow(3).shiftLeft(3).mod(this.curve.q);
+
+  return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
 }
 
 // Simple NAF (Non-Adjacent Form) multiplication algorithm
 // TODO: modularize the multiplication algorithm
 function pointFpMultiply(k) {
-    if(this.isInfinity()) return this;
-    if(k.signum() === 0) return this.curve.getInfinity()
+  if (this.isInfinity()) return this;
+  if (k.signum() === 0) return this.curve.getInfinity()
 
-    var e = k;
-    var h = e.multiply(THREE)
+  var e = k;
+  var h = e.multiply(THREE)
 
-    var neg = this.negate();
-    var R = this;
+  var neg = this.negate();
+  var R = this;
 
-    var i;
-    for(i = h.bitLength() - 2; i > 0; --i) {
-	R = R.twice();
+  var i;
+  for(i = h.bitLength() - 2; i > 0; --i) {
+    R = R.twice();
 
-	var hBit = h.testBit(i);
-	var eBit = e.testBit(i);
+    var hBit = h.testBit(i);
+    var eBit = e.testBit(i);
 
-	if (hBit != eBit) {
-	    R = R.add(hBit ? this : neg);
-	}
+    if (hBit != eBit) {
+        R = R.add(hBit ? this : neg);
     }
+  }
 
-    return R;
+  return R;
 }
 
 // Compute this*j + x*k (simultaneous multiplication)
 function pointFpMultiplyTwo(j,x,k) {
   var i;
-  if(j.bitLength() > k.bitLength())
+
+  if (j.bitLength() > k.bitLength())
     i = j.bitLength() - 1;
   else
     i = k.bitLength() - 1;
@@ -214,8 +219,8 @@ function pointFpMultiplyTwo(j,x,k) {
   var both = this.add(x);
   while(i >= 0) {
     R = R.twice();
-    if(j.testBit(i)) {
-      if(k.testBit(i)) {
+    if (j.testBit(i)) {
+      if (k.testBit(i)) {
         R = R.add(both);
       }
       else {
@@ -223,7 +228,7 @@ function pointFpMultiplyTwo(j,x,k) {
       }
     }
     else {
-      if(k.testBit(i)) {
+      if (k.testBit(i)) {
         R = R.add(x);
       }
     }
@@ -248,35 +253,35 @@ ECPointFp.prototype.multiplyTwo = pointFpMultiplyTwo;
 
 // constructor
 function ECCurveFp(q,a,b) {
-    this.q = q;
-    this.a = this.fromBigInteger(a);
-    this.b = this.fromBigInteger(b);
-    this.infinity = new ECPointFp(this, null, null);
+  this.q = q;
+  this.a = this.fromBigInteger(a);
+  this.b = this.fromBigInteger(b);
+  this.infinity = new ECPointFp(this, null, null);
 }
 
 function curveFpGetQ() {
-    return this.q;
+  return this.q;
 }
 
 function curveFpGetA() {
-    return this.a;
+  return this.a;
 }
 
 function curveFpGetB() {
-    return this.b;
+  return this.b;
 }
 
 function curveFpEquals(other) {
-    if(other == this) return true;
-    return(this.q.equals(other.q) && this.a.equals(other.a) && this.b.equals(other.b));
+  if (other == this) return true;
+  return(this.q.equals(other.q) && this.a.equals(other.a) && this.b.equals(other.b));
 }
 
 function curveFpGetInfinity() {
-    return this.infinity;
+  return this.infinity;
 }
 
 function curveFpFromBigInteger(x) {
-    return new ECFieldElementFp(this.q, x);
+  return new ECFieldElementFp(this.q, x);
 }
 
 ECCurveFp.prototype.getQ = curveFpGetQ;
@@ -410,6 +415,5 @@ ECPointFp.prototype.validate = function () {
   return true;
 };
 
-
 module.exports = ECCurveFp;
 module.exports.ECPointFp = ECPointFp;

From 525b053e3957f9e470511158ee6fe498bf4df0d2 Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Wed, 4 Jun 2014 16:08:46 +1000
Subject: [PATCH 6/8] ec: remove semi-colons

---
 src/ec.js | 281 +++++++++++++++++++++++++++---------------------------
 1 file changed, 142 insertions(+), 139 deletions(-)

diff --git a/src/ec.js b/src/ec.js
index 2981817..894c53e 100644
--- a/src/ec.js
+++ b/src/ec.js
@@ -9,113 +9,117 @@ var BigInteger = require('bigi')
 var THREE = BigInteger.valueOf(3)
 
 function ECFieldElementFp(q,x) {
-  this.x = x;
+  this.x = x
   // TODO if (x.compareTo(q) >= 0) error
-  this.q = q;
+  this.q = q
 }
 
 function feFpEquals(other) {
-  if (other == this) return true;
-  return (this.q.equals(other.q) && this.x.equals(other.x));
+  if (other == this) return true
+  return (this.q.equals(other.q) && this.x.equals(other.x))
 }
 
 function feFpToBigInteger() {
-  return this.x;
+  return this.x
 }
 
 function feFpNegate() {
-  return new ECFieldElementFp(this.q, this.x.negate().mod(this.q));
+  return new ECFieldElementFp(this.q, this.x.negate().mod(this.q))
 }
 
 function feFpAdd(b) {
-  return new ECFieldElementFp(this.q, this.x.add(b.toBigInteger()).mod(this.q));
+  return new ECFieldElementFp(this.q, this.x.add(b.toBigInteger()).mod(this.q))
 }
 
 function feFpSubtract(b) {
-  return new ECFieldElementFp(this.q, this.x.subtract(b.toBigInteger()).mod(this.q));
+  return new ECFieldElementFp(this.q, this.x.subtract(b.toBigInteger()).mod(this.q))
 }
 
 function feFpMultiply(b) {
-  return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger()).mod(this.q));
+  return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger()).mod(this.q))
 }
 
 function feFpSquare() {
-  return new ECFieldElementFp(this.q, this.x.square().mod(this.q));
+  return new ECFieldElementFp(this.q, this.x.square().mod(this.q))
 }
 
 function feFpDivide(b) {
-  return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger().modInverse(this.q)).mod(this.q));
+  return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger().modInverse(this.q)).mod(this.q))
 }
 
-ECFieldElementFp.prototype.equals = feFpEquals;
-ECFieldElementFp.prototype.toBigInteger = feFpToBigInteger;
-ECFieldElementFp.prototype.negate = feFpNegate;
-ECFieldElementFp.prototype.add = feFpAdd;
-ECFieldElementFp.prototype.subtract = feFpSubtract;
-ECFieldElementFp.prototype.multiply = feFpMultiply;
-ECFieldElementFp.prototype.square = feFpSquare;
-ECFieldElementFp.prototype.divide = feFpDivide;
+ECFieldElementFp.prototype.equals = feFpEquals
+ECFieldElementFp.prototype.toBigInteger = feFpToBigInteger
+ECFieldElementFp.prototype.negate = feFpNegate
+ECFieldElementFp.prototype.add = feFpAdd
+ECFieldElementFp.prototype.subtract = feFpSubtract
+ECFieldElementFp.prototype.multiply = feFpMultiply
+ECFieldElementFp.prototype.square = feFpSquare
+ECFieldElementFp.prototype.divide = feFpDivide
 
 // ----------------
 // ECPointFp
 
 // constructor
 function ECPointFp(curve,x,y,z) {
-  this.curve = curve;
-  this.x = x;
-  this.y = y;
+  this.curve = curve
+  this.x = x
+  this.y = y
+
   // Projective coordinates: either zinv == null or z * zinv == 1
   // z and zinv are just BigIntegers, not fieldElements
   if (z == null) {
-    this.z = BigInteger.ONE;
+    this.z = BigInteger.ONE
+
+  } else {
+    this.z = z
   }
-  else {
-    this.z = z;
-  }
-  this.zinv = null;
+  this.zinv = null
+
   //TODO: compression flag
 }
 
 function pointFpGetX() {
   if (this.zinv == null) {
-    this.zinv = this.z.modInverse(this.curve.q);
+    this.zinv = this.z.modInverse(this.curve.q)
   }
-  return this.curve.fromBigInteger(this.x.toBigInteger().multiply(this.zinv).mod(this.curve.q));
+
+  return this.curve.fromBigInteger(this.x.toBigInteger().multiply(this.zinv).mod(this.curve.q))
 }
 
 function pointFpGetY() {
   if (this.zinv == null) {
-    this.zinv = this.z.modInverse(this.curve.q);
+    this.zinv = this.z.modInverse(this.curve.q)
   }
-  return this.curve.fromBigInteger(this.y.toBigInteger().multiply(this.zinv).mod(this.curve.q));
+
+  return this.curve.fromBigInteger(this.y.toBigInteger().multiply(this.zinv).mod(this.curve.q))
 }
 
 function pointFpEquals(other) {
-  if (other == this) return true;
-  if (this.isInfinity()) return other.isInfinity();
-  if (other.isInfinity()) return this.isInfinity();
+  if (other == this) return true
+  if (this.isInfinity()) return other.isInfinity()
+  if (other.isInfinity()) return this.isInfinity()
 
-  var u, v;
   // u = Y2 * Z1 - Y1 * Z2
-  u = other.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(other.z)).mod(this.curve.q);
-  if (u.signum() !== 0) return false;
+  u = other.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(other.z)).mod(this.curve.q)
+  if (u.signum() !== 0) return false
+
   // v = X2 * Z1 - X1 * Z2
-  v = other.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(other.z)).mod(this.curve.q);
-  return v.signum() === 0;
+  v = other.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(other.z)).mod(this.curve.q)
+  return v.signum() === 0
 }
 
 function pointFpIsInfinity() {
-  if ((this.x == null) && (this.y == null)) return true;
-  return this.z.signum() === 0 && this.y.toBigInteger().signum() !== 0;
+  if ((this.x == null) && (this.y == null)) return true
+  return this.z.signum() === 0 && this.y.toBigInteger().signum() !== 0
 }
 
 function pointFpNegate() {
-  return new ECPointFp(this.curve, this.x, this.y.negate(), this.z);
+  return new ECPointFp(this.curve, this.x, this.y.negate(), this.z)
 }
 
 function pointFpAdd(b) {
-  if (this.isInfinity()) return b;
-  if (b.isInfinity()) return this;
+  if (this.isInfinity()) return b
+  if (b.isInfinity()) return this
 
   var x1 = this.x.toBigInteger()
   var y1 = this.y.toBigInteger()
@@ -129,171 +133,170 @@ function pointFpAdd(b) {
 
   if (v.signum() === 0) {
       if (u.signum() === 0) {
-          return this.twice(); // this == b, so double
+          return this.twice() // this == b, so double
       }
 
-      return this.curve.getInfinity(); // this = -b, so infinity
+      return this.curve.getInfinity() // this = -b, so infinity
   }
 
-  var v2 = v.square();
-  var v3 = v2.multiply(v);
-  var x1v2 = x1.multiply(v2);
-  var zu2 = u.square().multiply(this.z);
+  var v2 = v.square()
+  var v3 = v2.multiply(v)
+  var x1v2 = x1.multiply(v2)
+  var zu2 = u.square().multiply(this.z)
 
   // x3 = v * (z2 * (z1 * u^2 - 2 * x1 * v^2) - v^3)
-  var x3 = zu2.subtract(x1v2.shiftLeft(1)).multiply(b.z).subtract(v3).multiply(v).mod(this.curve.q);
+  var x3 = zu2.subtract(x1v2.shiftLeft(1)).multiply(b.z).subtract(v3).multiply(v).mod(this.curve.q)
   // y3 = z2 * (3 * x1 * u * v^2 - y1 * v^3 - z1 * u^3) + u * v^3
-  var y3 = x1v2.multiply(THREE).multiply(u).subtract(y1.multiply(v3)).subtract(zu2.multiply(u)).multiply(b.z).add(u.multiply(v3)).mod(this.curve.q);
+  var y3 = x1v2.multiply(THREE).multiply(u).subtract(y1.multiply(v3)).subtract(zu2.multiply(u)).multiply(b.z).add(u.multiply(v3)).mod(this.curve.q)
   // z3 = v^3 * z1 * z2
-  var z3 = v3.multiply(this.z).multiply(b.z).mod(this.curve.q);
+  var z3 = v3.multiply(this.z).multiply(b.z).mod(this.curve.q)
 
-  return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
+  return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3)
 }
 
 function pointFpTwice() {
-  if (this.isInfinity()) return this;
-  if (this.y.toBigInteger().signum() === 0) return this.curve.getInfinity();
+  if (this.isInfinity()) return this
+  if (this.y.toBigInteger().signum() === 0) return this.curve.getInfinity()
 
-  var x1 = this.x.toBigInteger();
-  var y1 = this.y.toBigInteger();
+  var x1 = this.x.toBigInteger()
+  var y1 = this.y.toBigInteger()
 
-  var y1z1 = y1.multiply(this.z);
-  var y1sqz1 = y1z1.multiply(y1).mod(this.curve.q);
-  var a = this.curve.a.toBigInteger();
+  var y1z1 = y1.multiply(this.z)
+  var y1sqz1 = y1z1.multiply(y1).mod(this.curve.q)
+  var a = this.curve.a.toBigInteger()
 
   // w = 3 * x1^2 + a * z1^2
-  var w = x1.square().multiply(THREE);
+  var w = x1.square().multiply(THREE)
 
   if (a.signum() !== 0) {
-    w = w.add(this.z.square().multiply(a));
+    w = w.add(this.z.square().multiply(a))
   }
 
-  w = w.mod(this.curve.q);
+  w = w.mod(this.curve.q)
   // x3 = 2 * y1 * z1 * (w^2 - 8 * x1 * y1^2 * z1)
-  var x3 = w.square().subtract(x1.shiftLeft(3).multiply(y1sqz1)).shiftLeft(1).multiply(y1z1).mod(this.curve.q);
+  var x3 = w.square().subtract(x1.shiftLeft(3).multiply(y1sqz1)).shiftLeft(1).multiply(y1z1).mod(this.curve.q)
   // y3 = 4 * y1^2 * z1 * (3 * w * x1 - 2 * y1^2 * z1) - w^3
-  var y3 = w.multiply(THREE).multiply(x1).subtract(y1sqz1.shiftLeft(1)).shiftLeft(2).multiply(y1sqz1).subtract(w.pow(3)).mod(this.curve.q);
+  var y3 = w.multiply(THREE).multiply(x1).subtract(y1sqz1.shiftLeft(1)).shiftLeft(2).multiply(y1sqz1).subtract(w.pow(3)).mod(this.curve.q)
   // z3 = 8 * (y1 * z1)^3
-  var z3 = y1z1.pow(3).shiftLeft(3).mod(this.curve.q);
+  var z3 = y1z1.pow(3).shiftLeft(3).mod(this.curve.q)
 
-  return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
+  return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3)
 }
 
 // Simple NAF (Non-Adjacent Form) multiplication algorithm
 // TODO: modularize the multiplication algorithm
 function pointFpMultiply(k) {
-  if (this.isInfinity()) return this;
+  if (this.isInfinity()) return this
   if (k.signum() === 0) return this.curve.getInfinity()
 
-  var e = k;
+  var e = k
   var h = e.multiply(THREE)
 
-  var neg = this.negate();
-  var R = this;
+  var neg = this.negate()
+  var R = this
 
-  var i;
-  for(i = h.bitLength() - 2; i > 0; --i) {
-    R = R.twice();
+  for (var i = h.bitLength() - 2; i > 0; --i) {
+    R = R.twice()
 
-    var hBit = h.testBit(i);
-    var eBit = e.testBit(i);
+    var hBit = h.testBit(i)
+    var eBit = e.testBit(i)
 
     if (hBit != eBit) {
-        R = R.add(hBit ? this : neg);
+        R = R.add(hBit ? this : neg)
     }
   }
 
-  return R;
+  return R
 }
 
 // Compute this*j + x*k (simultaneous multiplication)
 function pointFpMultiplyTwo(j,x,k) {
-  var i;
+  var i
 
   if (j.bitLength() > k.bitLength())
-    i = j.bitLength() - 1;
+    i = j.bitLength() - 1
   else
-    i = k.bitLength() - 1;
+    i = k.bitLength() - 1
 
-  var R = this.curve.getInfinity();
-  var both = this.add(x);
-  while(i >= 0) {
-    R = R.twice();
+  var R = this.curve.getInfinity()
+  var both = this.add(x)
+  while (i >= 0) {
+    R = R.twice()
     if (j.testBit(i)) {
       if (k.testBit(i)) {
-        R = R.add(both);
+        R = R.add(both)
       }
       else {
-        R = R.add(this);
+        R = R.add(this)
       }
     }
     else {
       if (k.testBit(i)) {
-        R = R.add(x);
+        R = R.add(x)
       }
     }
-    --i;
+    --i
   }
 
-  return R;
+  return R
 }
 
-ECPointFp.prototype.getX = pointFpGetX;
-ECPointFp.prototype.getY = pointFpGetY;
-ECPointFp.prototype.equals = pointFpEquals;
-ECPointFp.prototype.isInfinity = pointFpIsInfinity;
-ECPointFp.prototype.negate = pointFpNegate;
-ECPointFp.prototype.add = pointFpAdd;
-ECPointFp.prototype.twice = pointFpTwice;
-ECPointFp.prototype.multiply = pointFpMultiply;
-ECPointFp.prototype.multiplyTwo = pointFpMultiplyTwo;
+ECPointFp.prototype.getX = pointFpGetX
+ECPointFp.prototype.getY = pointFpGetY
+ECPointFp.prototype.equals = pointFpEquals
+ECPointFp.prototype.isInfinity = pointFpIsInfinity
+ECPointFp.prototype.negate = pointFpNegate
+ECPointFp.prototype.add = pointFpAdd
+ECPointFp.prototype.twice = pointFpTwice
+ECPointFp.prototype.multiply = pointFpMultiply
+ECPointFp.prototype.multiplyTwo = pointFpMultiplyTwo
 
 // ----------------
 // ECCurveFp
 
 // constructor
 function ECCurveFp(q,a,b) {
-  this.q = q;
-  this.a = this.fromBigInteger(a);
-  this.b = this.fromBigInteger(b);
-  this.infinity = new ECPointFp(this, null, null);
+  this.q = q
+  this.a = this.fromBigInteger(a)
+  this.b = this.fromBigInteger(b)
+  this.infinity = new ECPointFp(this, null, null)
 }
 
 function curveFpGetQ() {
-  return this.q;
+  return this.q
 }
 
 function curveFpGetA() {
-  return this.a;
+  return this.a
 }
 
 function curveFpGetB() {
-  return this.b;
+  return this.b
 }
 
 function curveFpEquals(other) {
-  if (other == this) return true;
-  return(this.q.equals(other.q) && this.a.equals(other.a) && this.b.equals(other.b));
+  if (other == this) return true
+  return(this.q.equals(other.q) && this.a.equals(other.a) && this.b.equals(other.b))
 }
 
 function curveFpGetInfinity() {
-  return this.infinity;
+  return this.infinity
 }
 
 function curveFpFromBigInteger(x) {
-  return new ECFieldElementFp(this.q, x);
+  return new ECFieldElementFp(this.q, x)
 }
 
-ECCurveFp.prototype.getQ = curveFpGetQ;
-ECCurveFp.prototype.getA = curveFpGetA;
-ECCurveFp.prototype.getB = curveFpGetB;
-ECCurveFp.prototype.equals = curveFpEquals;
-ECCurveFp.prototype.getInfinity = curveFpGetInfinity;
-ECCurveFp.prototype.fromBigInteger = curveFpFromBigInteger;
+ECCurveFp.prototype.getQ = curveFpGetQ
+ECCurveFp.prototype.getA = curveFpGetA
+ECCurveFp.prototype.getB = curveFpGetB
+ECCurveFp.prototype.equals = curveFpEquals
+ECCurveFp.prototype.getInfinity = curveFpGetInfinity
+ECCurveFp.prototype.fromBigInteger = curveFpFromBigInteger
 
 ECFieldElementFp.prototype.getByteLength = function () {
-  return Math.floor((this.toBigInteger().bitLength() + 7) / 8);
-};
+  return Math.floor((this.toBigInteger().bitLength() + 7) / 8)
+}
 
 ECPointFp.prototype.getEncoded = function(compressed) {
   var x = this.getX().toBigInteger()
@@ -361,20 +364,20 @@ ECPointFp.decodeFrom = function (curve, buffer) {
 }
 
 ECPointFp.prototype.isOnCurve = function () {
-  var x = this.getX().toBigInteger();
-  var y = this.getY().toBigInteger();
-  var a = this.curve.getA().toBigInteger();
-  var b = this.curve.getB().toBigInteger();
+  var x = this.getX().toBigInteger()
+  var y = this.getY().toBigInteger()
+  var a = this.curve.getA().toBigInteger()
+  var b = this.curve.getB().toBigInteger()
   var p = this.curve.getQ()
   var lhs = y.square().mod(p)
   var rhs = x.pow(3).add(a.multiply(x)).add(b).mod(p)
-  return lhs.equals(rhs);
-};
+  return lhs.equals(rhs)
+}
 
 ECPointFp.prototype.toString = function () {
   return '('+this.getX().toBigInteger().toString()+','+
-    this.getY().toBigInteger().toString()+')';
-};
+    this.getY().toBigInteger().toString()+')'
+}
 
 /**
  * Validate an elliptic curve point.
@@ -382,38 +385,38 @@ ECPointFp.prototype.toString = function () {
  * See SEC 1, section 3.2.2.1: Elliptic Curve Public Key Validation Primitive
  */
 ECPointFp.prototype.validate = function () {
-  var n = this.curve.getQ();
+  var n = this.curve.getQ()
 
   // Check Q != O
   if (this.isInfinity()) {
-    throw new Error("Point is at infinity.");
+    throw new Error("Point is at infinity.")
   }
 
   // Check coordinate bounds
-  var x = this.getX().toBigInteger();
-  var y = this.getY().toBigInteger();
+  var x = this.getX().toBigInteger()
+  var y = this.getY().toBigInteger()
   if (x.compareTo(BigInteger.ONE) < 0 ||
       x.compareTo(n.subtract(BigInteger.ONE)) > 0) {
-    throw new Error('x coordinate out of bounds');
+    throw new Error('x coordinate out of bounds')
   }
   if (y.compareTo(BigInteger.ONE) < 0 ||
       y.compareTo(n.subtract(BigInteger.ONE)) > 0) {
-    throw new Error('y coordinate out of bounds');
+    throw new Error('y coordinate out of bounds')
   }
 
   // Check y^2 = x^3 + ax + b (mod n)
   if (!this.isOnCurve()) {
-    throw new Error("Point is not on the curve.");
+    throw new Error("Point is not on the curve.")
   }
 
   // Check nQ = 0 (Q is a scalar multiple of G)
   if (this.multiply(n).isInfinity()) {
     // TODO: This check doesn't work - fix.
-    throw new Error("Point is not a scalar multiple of G.");
+    throw new Error("Point is not a scalar multiple of G.")
   }
 
-  return true;
-};
+  return true
+}
 
-module.exports = ECCurveFp;
-module.exports.ECPointFp = ECPointFp;
+module.exports = ECCurveFp
+module.exports.ECPointFp = ECPointFp

From dc3d9aec655bc52f1f2b9583e43861de88cd8444 Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Wed, 4 Jun 2014 16:06:11 +1000
Subject: [PATCH 7/8] ec: compare strictly against null

---
 src/ec.js | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/src/ec.js b/src/ec.js
index 894c53e..47cdb49 100644
--- a/src/ec.js
+++ b/src/ec.js
@@ -67,19 +67,14 @@ function ECPointFp(curve,x,y,z) {
 
   // Projective coordinates: either zinv == null or z * zinv == 1
   // z and zinv are just BigIntegers, not fieldElements
-  if (z == null) {
-    this.z = BigInteger.ONE
-
-  } else {
-    this.z = z
-  }
+  this.z = (z == undefined) ? BigInteger.ONE : z
   this.zinv = null
 
   //TODO: compression flag
 }
 
 function pointFpGetX() {
-  if (this.zinv == null) {
+  if (this.zinv === null) {
     this.zinv = this.z.modInverse(this.curve.q)
   }
 
@@ -87,7 +82,7 @@ function pointFpGetX() {
 }
 
 function pointFpGetY() {
-  if (this.zinv == null) {
+  if (this.zinv === null) {
     this.zinv = this.z.modInverse(this.curve.q)
   }
 
@@ -109,7 +104,7 @@ function pointFpEquals(other) {
 }
 
 function pointFpIsInfinity() {
-  if ((this.x == null) && (this.y == null)) return true
+  if ((this.x === null) && (this.y === null)) return true
   return this.z.signum() === 0 && this.y.toBigInteger().signum() !== 0
 }
 

From 42e7197c46a7371e9e39d613772fa1d072b7cf74 Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Wed, 4 Jun 2014 16:06:30 +1000
Subject: [PATCH 8/8] ec: define on use

---
 src/ec.js | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/ec.js b/src/ec.js
index 47cdb49..5e37f32 100644
--- a/src/ec.js
+++ b/src/ec.js
@@ -95,11 +95,13 @@ function pointFpEquals(other) {
   if (other.isInfinity()) return this.isInfinity()
 
   // u = Y2 * Z1 - Y1 * Z2
-  u = other.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(other.z)).mod(this.curve.q)
+  var u = other.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(other.z)).mod(this.curve.q)
+
   if (u.signum() !== 0) return false
 
   // v = X2 * Z1 - X1 * Z2
-  v = other.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(other.z)).mod(this.curve.q)
+  var v = other.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(other.z)).mod(this.curve.q)
+
   return v.signum() === 0
 }