Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages and argument checking in .imuln() #207

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -1918,8 +1926,9 @@
var isNegNum = num < 0;
if (isNegNum) num = -num;

assert(typeof num === 'number');
assert(num < 0x4000000);
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
var carry = 0;
Expand Down
34 changes: 33 additions & 1 deletion test/arithmetic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,42 @@ 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 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);
}, /^Error: Assertion failed$/);
}, /^Error: Argument not in supported range -0x4000000 < num < 0x4000000$/);
});

it('should negate number if number is negative', function () {
Expand Down