From 134f4a8aa5ae7032424c8c32a4b9d1754f463818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Martin?= Date: Mon, 30 Sep 2019 11:05:29 +0200 Subject: [PATCH] create Points with not only hex strings pointFromX, pointFromY and initialization functions for points got an additional optional function parameter. This can be used for providing strings containing numbers in other bases than 16 (hex). --- lib/elliptic/curve/edwards.js | 18 +++++++++--------- lib/elliptic/curve/mont.js | 6 +++--- lib/elliptic/curve/short.js | 22 +++++++++++----------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/elliptic/curve/edwards.js b/lib/elliptic/curve/edwards.js index 1c53fe9..320abfc 100644 --- a/lib/elliptic/curve/edwards.js +++ b/lib/elliptic/curve/edwards.js @@ -47,8 +47,8 @@ EdwardsCurve.prototype.jpoint = function jpoint(x, y, z, t) { return this.point(x, y, z, t); }; -EdwardsCurve.prototype.pointFromX = function pointFromX(x, odd) { - x = new BN(x, 16); +EdwardsCurve.prototype.pointFromX = function pointFromX(x, odd, base = 16) { + x = new BN(x, base); if (!x.red) x = x.toRed(this.red); @@ -68,8 +68,8 @@ EdwardsCurve.prototype.pointFromX = function pointFromX(x, odd) { return this.point(x, y); }; -EdwardsCurve.prototype.pointFromY = function pointFromY(y, odd) { - y = new BN(y, 16); +EdwardsCurve.prototype.pointFromY = function pointFromY(y, odd, base = 16) { + y = new BN(y, base); if (!y.red) y = y.toRed(this.red); @@ -111,7 +111,7 @@ EdwardsCurve.prototype.validate = function validate(point) { return lhs.cmp(rhs) === 0; }; -function Point(curve, x, y, z, t) { +function Point(curve, x, y, z, t, base = 16) { Base.BasePoint.call(this, curve, 'projective'); if (x === null && y === null && z === null) { this.x = this.curve.zero; @@ -120,10 +120,10 @@ function Point(curve, x, y, z, t) { this.t = this.curve.zero; this.zOne = true; } else { - this.x = new BN(x, 16); - this.y = new BN(y, 16); - this.z = z ? new BN(z, 16) : this.curve.one; - this.t = t && new BN(t, 16); + this.x = new BN(x, base); + this.y = new BN(y, base); + this.z = z ? new BN(z, base) : this.curve.one; + this.t = t && new BN(t, base); if (!this.x.red) this.x = this.x.toRed(this.curve.red); if (!this.y.red) diff --git a/lib/elliptic/curve/mont.js b/lib/elliptic/curve/mont.js index 4b9f80f..86c699c 100644 --- a/lib/elliptic/curve/mont.js +++ b/lib/elliptic/curve/mont.js @@ -27,14 +27,14 @@ MontCurve.prototype.validate = function validate(point) { return y.redSqr().cmp(rhs) === 0; }; -function Point(curve, x, z) { +function Point(curve, x, z, base = 16) { Base.BasePoint.call(this, curve, 'projective'); if (x === null && z === null) { this.x = this.curve.one; this.z = this.curve.zero; } else { - this.x = new BN(x, 16); - this.z = new BN(z, 16); + this.x = new BN(x, base); + this.z = new BN(z, base); if (!this.x.red) this.x = this.x.toRed(this.curve.red); if (!this.z.red) diff --git a/lib/elliptic/curve/short.js b/lib/elliptic/curve/short.js index b263ecf..ef1d458 100644 --- a/lib/elliptic/curve/short.js +++ b/lib/elliptic/curve/short.js @@ -184,8 +184,8 @@ ShortCurve.prototype._endoSplit = function _endoSplit(k) { return { k1: k1, k2: k2 }; }; -ShortCurve.prototype.pointFromX = function pointFromX(x, odd) { - x = new BN(x, 16); +ShortCurve.prototype.pointFromX = function pointFromX(x, odd, base = 16) { + x = new BN(x, base); if (!x.red) x = x.toRed(this.red); @@ -248,15 +248,15 @@ ShortCurve.prototype._endoWnafMulAdd = return res; }; -function Point(curve, x, y, isRed) { +function Point(curve, x, y, isRed, base = 16) { Base.BasePoint.call(this, curve, 'affine'); if (x === null && y === null) { this.x = null; this.y = null; this.inf = true; } else { - this.x = new BN(x, 16); - this.y = new BN(y, 16); + this.x = new BN(x, base); + this.y = new BN(y, base); // Force redgomery representation when loading from JSON if (isRed) { this.x.forceRed(this.curve.red); @@ -419,8 +419,8 @@ Point.prototype.getY = function getY() { return this.y.fromRed(); }; -Point.prototype.mul = function mul(k) { - k = new BN(k, 16); +Point.prototype.mul = function mul(k, base = 16) { + k = new BN(k, base); if (this.isInfinity()) return this; else if (this._hasDoubles(k)) @@ -487,16 +487,16 @@ Point.prototype.toJ = function toJ() { return res; }; -function JPoint(curve, x, y, z) { +function JPoint(curve, x, y, z, base = 16) { Base.BasePoint.call(this, curve, 'jacobian'); if (x === null && y === null && z === null) { this.x = this.curve.one; this.y = this.curve.one; this.z = new BN(0); } else { - this.x = new BN(x, 16); - this.y = new BN(y, 16); - this.z = new BN(z, 16); + this.x = new BN(x, base); + this.y = new BN(y, base); + this.z = new BN(z, base); } if (!this.x.red) this.x = this.x.toRed(this.curve.red);