Browse Source

ec: white space fixes

It is advised to use ?w=0 in comparing this commit.
hk-custom-address
Daniel Cousens 11 years ago
parent
commit
79c7b68d35
  1. 260
      src/ec.js

260
src/ec.js

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

Loading…
Cancel
Save