diff --git a/src/ec.js b/src/ec.js index d21f073..c31ce43 100644 --- a/src/ec.js +++ b/src/ec.js @@ -170,9 +170,9 @@ function pointFpTwice() { // 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.square().multiply(w)).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.square().multiply(y1z1).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); } @@ -348,7 +348,7 @@ ECPointFp.decodeFrom = function (curve, buffer) { } // Convert x to point - var alpha = x.square().multiply(x).add(SEVEN).mod(p) + var alpha = x.pow(3).add(SEVEN).mod(p) var beta = alpha.modPow(curve.P_OVER_FOUR, p) // If beta is even, but y isn't, or vice versa, then convert it, @@ -440,10 +440,9 @@ ECPointFp.prototype.isOnCurve = function () { var y = this.getY().toBigInteger(); var a = this.curve.getA().toBigInteger(); var b = this.curve.getB().toBigInteger(); - var n = this.curve.getQ(); - var lhs = y.multiply(y).mod(n); - var rhs = x.multiply(x).multiply(x) - .add(a.multiply(x)).add(b).mod(n); + 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); }; diff --git a/src/ecdsa.js b/src/ecdsa.js index 6d78516..acf88c3 100644 --- a/src/ecdsa.js +++ b/src/ecdsa.js @@ -73,7 +73,7 @@ var ecdsa = { var s = k.modInverse(n).multiply(e.add(D.multiply(r))).mod(n) assert.notEqual(s.signum(), 0, 'Invalid S value') - var N_OVER_TWO = n.divide(BigInteger.valueOf(2)) + var N_OVER_TWO = n.shiftRight(1) // enforce low S values, see bip62: 'low s values in signatures' if (s.compareTo(N_OVER_TWO) > 0) { @@ -263,7 +263,7 @@ var ecdsa = { var x = isSecondKey ? r.add(n) : r // 1.3 Convert x to point - var alpha = x.multiply(x).multiply(x).add(a.multiply(x)).add(b).mod(p) + var alpha = x.pow(3).add(a.multiply(x)).add(b).mod(p) var beta = alpha.modPow(curve.P_OVER_FOUR, p) // If beta is even, but y isn't, or vice versa, then convert it, diff --git a/test/ecdsa.js b/test/ecdsa.js index aef4f2d..9decb27 100644 --- a/test/ecdsa.js +++ b/test/ecdsa.js @@ -59,7 +59,8 @@ describe('ecdsa', function() { var psig = ecdsa.parseSig(signature) // See BIP62 for more information - assert(psig.s.compareTo(ecparams.getN().divide(BigInteger.valueOf(2))) <= 0) + var N_OVER_TWO = ecparams.getN().shiftRight(1) + assert(psig.s.compareTo(N_OVER_TWO) <= 0) }) })