Browse Source

ecdsa: opt for shiftRight, pow and square

In the given situations, these offer better readability, or in the case
of shiftRight, a substantial performance increase.
hk-custom-address
Daniel Cousens 11 years ago
parent
commit
5ec9504ed6
  1. 13
      src/ec.js
  2. 4
      src/ecdsa.js
  3. 3
      test/ecdsa.js

13
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);
};

4
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,

3
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)
})
})

Loading…
Cancel
Save