From c62a93bbda1e2807a59b743757391f567df2c09a Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 29 Nov 2018 15:18:57 +0100 Subject: [PATCH 1/3] Improve error messages for imuln argument range --- lib/bn.js | 2 +- test/arithmetic-test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/bn.js b/lib/bn.js index 0854c1c..b775150 100644 --- a/lib/bn.js +++ b/lib/bn.js @@ -1919,7 +1919,7 @@ if (isNegNum) num = -num; assert(typeof num === 'number'); - assert(num < 0x4000000); + assert(num < 0x4000000, 'Argument not in supported range -0x4000000 < num < 0x4000000'); // Carry var carry = 0; diff --git a/test/arithmetic-test.js b/test/arithmetic-test.js index 67956ff..df2545a 100644 --- a/test/arithmetic-test.js +++ b/test/arithmetic-test.js @@ -288,7 +288,7 @@ describe('BN.js/Arithmetic', function () { it('should throw error with num eq 0x4000000', function () { assert.throws(function () { new BN(0).imuln(0x4000000); - }, /^Error: Assertion failed$/); + }, /^Error: Argument not in supported range -0x4000000 < num < 0x4000000$/); }); it('should negate number if number is negative', function () { From 1d299e55d909b8f1213e0f59619dce7575ce8fb7 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 29 Nov 2018 15:24:17 +0100 Subject: [PATCH 2/3] Improve error message and add test for imuln argument type --- lib/bn.js | 2 +- test/arithmetic-test.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/bn.js b/lib/bn.js index b775150..7fe0dd0 100644 --- a/lib/bn.js +++ b/lib/bn.js @@ -1918,7 +1918,7 @@ var isNegNum = num < 0; if (isNegNum) num = -num; - assert(typeof num === 'number'); + assert(typeof num === 'number', 'Argument num must be a number'); assert(num < 0x4000000, 'Argument not in supported range -0x4000000 < num < 0x4000000'); // Carry diff --git a/test/arithmetic-test.js b/test/arithmetic-test.js index df2545a..1603a36 100644 --- a/test/arithmetic-test.js +++ b/test/arithmetic-test.js @@ -285,6 +285,16 @@ describe('BN.js/Arithmetic', function () { assert.equal(a.muln(0xdead).toString(16), c.toString(16)); }); + it('should throw error with num of type other than number', function () { + assert.throws(function () { + new BN(0).imuln('1'); + }, /^Error: Argument num must be a number$/); + + assert.throws(function () { + new BN(0).imuln(new BN(1)); + }, /^Error: Argument num must be a number$/); + }); + it('should throw error with num eq 0x4000000', function () { assert.throws(function () { new BN(0).imuln(0x4000000); From c8766d653129e4f5679a4375f36ac4bd7a092b07 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 29 Nov 2018 15:38:14 +0100 Subject: [PATCH 3/3] Ensure argument of imuln is an integer --- lib/bn.js | 9 +++++++++ test/arithmetic-test.js | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/bn.js b/lib/bn.js index 7fe0dd0..67c7b42 100644 --- a/lib/bn.js +++ b/lib/bn.js @@ -6,6 +6,14 @@ if (!val) throw new Error(msg || 'Assertion failed'); } + // compatibility replacement for Number.isInteger + // https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger#Polyfill + function isInteger (value) { + return typeof value === 'number' && + isFinite(value) && + Math.floor(value) === value; + } + // Could use `inherits` module, but don't want to move from single file // architecture yet. function inherits (ctor, superCtor) { @@ -1919,6 +1927,7 @@ if (isNegNum) num = -num; assert(typeof num === 'number', 'Argument num must be a number'); + assert(isInteger(num), 'Argument must be an integer'); assert(num < 0x4000000, 'Argument not in supported range -0x4000000 < num < 0x4000000'); // Carry diff --git a/test/arithmetic-test.js b/test/arithmetic-test.js index 1603a36..b45f3ec 100644 --- a/test/arithmetic-test.js +++ b/test/arithmetic-test.js @@ -295,6 +295,28 @@ describe('BN.js/Arithmetic', function () { }, /^Error: Argument num must be a number$/); }); + it('should throw error with num eq 1.5, -1.5 and NaN', function () { + assert.throws(function () { + new BN(0).imuln(1.5); + }, /^Error: Argument must be an integer$/); + + assert.throws(function () { + new BN(0).imuln(-1.5); + }, /^Error: Argument must be an integer$/); + + assert.throws(function () { + new BN(0).imuln(Infinity); + }, /^Error: Argument must be an integer$/); + + assert.throws(function () { + new BN(0).imuln(-Infinity); + }, /^Error: Argument must be an integer$/); + + assert.throws(function () { + new BN(0).imuln(NaN); + }, /^Error: Argument must be an integer$/); + }); + it('should throw error with num eq 0x4000000', function () { assert.throws(function () { new BN(0).imuln(0x4000000);