From 7ec66ffa255079260126d87b1762a59ea10de5ea Mon Sep 17 00:00:00 2001 From: Benjamin Diamond <30356252+benediamond@users.noreply.github.com> Date: Tue, 3 Sep 2019 22:33:44 -0400 Subject: [PATCH] short: add infinity check before multiplying --- lib/elliptic/curve/short.js | 5 +++-- test/curve-test.js | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/elliptic/curve/short.js b/lib/elliptic/curve/short.js index b631e7c..b263ecf 100644 --- a/lib/elliptic/curve/short.js +++ b/lib/elliptic/curve/short.js @@ -421,8 +421,9 @@ Point.prototype.getY = function getY() { Point.prototype.mul = function mul(k) { k = new BN(k, 16); - - if (this._hasDoubles(k)) + if (this.isInfinity()) + return this; + else if (this._hasDoubles(k)) return this.curve._fixedNafMul(this, k); else if (this.curve.endo) return this.curve._endoWnafMulAdd([ this ], [ k ]); diff --git a/test/curve-test.js b/test/curve-test.js index 2c6167d..fcfa84e 100644 --- a/test/curve-test.js +++ b/test/curve-test.js @@ -253,6 +253,13 @@ describe('Curve', function() { var neg2 = neg.neg(true); assert(p.eq(neg2)); }); + + it('should correctly handle scalar multiplication of zero', function() { + var curve = elliptic.curves.secp256k1.curve; + var p1 = curve.g.mul('0'); + var p2 = p1.mul('2'); + assert(p1.eq(p2)); + }); }); describe('Point codec', function () {