ec: use signum() not equals(BigInteger.ZERO)
This commit is contained in:
parent
d14b08efd1
commit
d9ce4bee6b
1 changed files with 11 additions and 11 deletions
22
src/ec.js
22
src/ec.js
|
@ -94,15 +94,15 @@ function pointFpEquals(other) {
|
||||||
var u, v;
|
var u, v;
|
||||||
// u = Y2 * Z1 - Y1 * Z2
|
// u = Y2 * Z1 - Y1 * Z2
|
||||||
u = other.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(other.z)).mod(this.curve.q);
|
u = other.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(other.z)).mod(this.curve.q);
|
||||||
if(!u.equals(BigInteger.ZERO)) return false;
|
if (u.signum() !== 0) return false;
|
||||||
// v = X2 * Z1 - X1 * Z2
|
// v = X2 * Z1 - X1 * Z2
|
||||||
v = other.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(other.z)).mod(this.curve.q);
|
v = other.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(other.z)).mod(this.curve.q);
|
||||||
return v.equals(BigInteger.ZERO);
|
return v.signum() === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function pointFpIsInfinity() {
|
function pointFpIsInfinity() {
|
||||||
if((this.x == null) && (this.y == null)) return true;
|
if ((this.x == null) && (this.y == null)) return true;
|
||||||
return this.z.equals(BigInteger.ZERO) && !this.y.toBigInteger().equals(BigInteger.ZERO);
|
return this.z.signum() === 0 && this.y.toBigInteger().signum() !== 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function pointFpNegate() {
|
function pointFpNegate() {
|
||||||
|
@ -118,8 +118,8 @@ function pointFpAdd(b) {
|
||||||
// v = X2 * Z1 - X1 * Z2
|
// 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 = b.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(b.z)).mod(this.curve.q);
|
||||||
|
|
||||||
if(BigInteger.ZERO.equals(v)) {
|
if(v.signum() === 0) {
|
||||||
if(BigInteger.ZERO.equals(u)) {
|
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
|
||||||
|
@ -148,7 +148,7 @@ function pointFpAdd(b) {
|
||||||
|
|
||||||
function pointFpTwice() {
|
function pointFpTwice() {
|
||||||
if(this.isInfinity()) return this;
|
if(this.isInfinity()) return this;
|
||||||
if(this.y.toBigInteger().signum() == 0) return this.curve.getInfinity();
|
if(this.y.toBigInteger().signum() === 0) return this.curve.getInfinity();
|
||||||
|
|
||||||
// TODO: optimized handling of constants
|
// TODO: optimized handling of constants
|
||||||
var THREE = new BigInteger("3");
|
var THREE = new BigInteger("3");
|
||||||
|
@ -161,7 +161,7 @@ function pointFpTwice() {
|
||||||
|
|
||||||
// w = 3 * x1^2 + a * z1^2
|
// w = 3 * x1^2 + a * z1^2
|
||||||
var w = x1.square().multiply(THREE);
|
var w = x1.square().multiply(THREE);
|
||||||
if(!BigInteger.ZERO.equals(a)) {
|
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);
|
||||||
|
@ -179,7 +179,7 @@ function pointFpTwice() {
|
||||||
// TODO: modularize the multiplication algorithm
|
// TODO: modularize the multiplication algorithm
|
||||||
function pointFpMultiply(k) {
|
function pointFpMultiply(k) {
|
||||||
if(this.isInfinity()) return this;
|
if(this.isInfinity()) return this;
|
||||||
if(k.signum() == 0) return this.curve.getInfinity();
|
if(k.signum() === 0) return this.curve.getInfinity()
|
||||||
|
|
||||||
var e = k;
|
var e = k;
|
||||||
var h = e.multiply(new BigInteger("3"));
|
var h = e.multiply(new BigInteger("3"));
|
||||||
|
@ -392,7 +392,7 @@ ECPointFp.prototype.add2D = function (b) {
|
||||||
|
|
||||||
ECPointFp.prototype.twice2D = function () {
|
ECPointFp.prototype.twice2D = function () {
|
||||||
if (this.isInfinity()) return this;
|
if (this.isInfinity()) return this;
|
||||||
if (this.y.toBigInteger().signum() == 0) {
|
if (this.y.toBigInteger().signum() === 0) {
|
||||||
// if y1 == 0, then (x1, y1) == (x1, -y1)
|
// if y1 == 0, then (x1, y1) == (x1, -y1)
|
||||||
// and hence this = -this and thus 2(x1, y1) == infinity
|
// and hence this = -this and thus 2(x1, y1) == infinity
|
||||||
return this.curve.getInfinity();
|
return this.curve.getInfinity();
|
||||||
|
@ -410,7 +410,7 @@ ECPointFp.prototype.twice2D = function () {
|
||||||
|
|
||||||
ECPointFp.prototype.multiply2D = function (k) {
|
ECPointFp.prototype.multiply2D = function (k) {
|
||||||
if(this.isInfinity()) return this;
|
if(this.isInfinity()) return this;
|
||||||
if(k.signum() == 0) return this.curve.getInfinity();
|
if (k.signum() === 0) return this.curve.getInfinity()
|
||||||
|
|
||||||
var e = k;
|
var e = k;
|
||||||
var h = e.multiply(new BigInteger("3"));
|
var h = e.multiply(new BigInteger("3"));
|
||||||
|
|
Loading…
Add table
Reference in a new issue