ec: white space fixes
It is advised to use ?w=0 in comparing this commit.
This commit is contained in:
parent
8a2dcaaaf5
commit
79c7b68d35
1 changed files with 128 additions and 124 deletions
50
src/ec.js
50
src/ec.js
|
@ -10,12 +10,12 @@ var THREE = BigInteger.valueOf(3)
|
|||
|
||||
function ECFieldElementFp(q,x) {
|
||||
this.x = x;
|
||||
// TODO if(x.compareTo(q) >= 0) error
|
||||
// TODO if (x.compareTo(q) >= 0) error
|
||||
this.q = q;
|
||||
}
|
||||
|
||||
function feFpEquals(other) {
|
||||
if(other == this) return true;
|
||||
if (other == this) return true;
|
||||
return (this.q.equals(other.q) && this.x.equals(other.x));
|
||||
}
|
||||
|
||||
|
@ -66,7 +66,7 @@ function ECPointFp(curve,x,y,z) {
|
|||
this.y = y;
|
||||
// Projective coordinates: either zinv == null or z * zinv == 1
|
||||
// z and zinv are just BigIntegers, not fieldElements
|
||||
if(z == null) {
|
||||
if (z == null) {
|
||||
this.z = BigInteger.ONE;
|
||||
}
|
||||
else {
|
||||
|
@ -77,23 +77,24 @@ function ECPointFp(curve,x,y,z) {
|
|||
}
|
||||
|
||||
function pointFpGetX() {
|
||||
if(this.zinv == null) {
|
||||
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) {
|
||||
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();
|
||||
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);
|
||||
|
@ -113,8 +114,8 @@ function pointFpNegate() {
|
|||
}
|
||||
|
||||
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()
|
||||
|
@ -126,10 +127,11 @@ function pointFpAdd(b) {
|
|||
// 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) {
|
||||
if (v.signum() === 0) {
|
||||
if (u.signum() === 0) {
|
||||
return this.twice(); // this == b, so double
|
||||
}
|
||||
|
||||
return this.curve.getInfinity(); // this = -b, so infinity
|
||||
}
|
||||
|
||||
|
@ -149,8 +151,8 @@ function pointFpAdd(b) {
|
|||
}
|
||||
|
||||
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();
|
||||
|
@ -161,9 +163,11 @@ function pointFpTwice() {
|
|||
|
||||
// w = 3 * x1^2 + a * z1^2
|
||||
var w = x1.square().multiply(THREE);
|
||||
if(a.signum() !== 0) {
|
||||
|
||||
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);
|
||||
|
@ -178,8 +182,8 @@ function pointFpTwice() {
|
|||
// 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)
|
||||
|
@ -205,7 +209,8 @@ function pointFpMultiply(k) {
|
|||
// 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);
|
||||
}
|
||||
}
|
||||
|
@ -267,7 +272,7 @@ function curveFpGetB() {
|
|||
}
|
||||
|
||||
function curveFpEquals(other) {
|
||||
if(other == this) return true;
|
||||
if (other == this) return true;
|
||||
return(this.q.equals(other.q) && this.a.equals(other.a) && this.b.equals(other.b));
|
||||
}
|
||||
|
||||
|
@ -410,6 +415,5 @@ ECPointFp.prototype.validate = function () {
|
|||
return true;
|
||||
};
|
||||
|
||||
|
||||
module.exports = ECCurveFp;
|
||||
module.exports.ECPointFp = ECPointFp;
|
||||
|
|
Loading…
Add table
Reference in a new issue