Skip to content

Commit

Permalink
create Points with not only hex strings
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
RMartinInfAI committed Sep 30, 2019
1 parent 71e4e8e commit 134f4a8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
18 changes: 9 additions & 9 deletions lib/elliptic/curve/edwards.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions lib/elliptic/curve/mont.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions lib/elliptic/curve/short.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 134f4a8

Please sign in to comment.