Skip to content

Commit

Permalink
lib: deprecate .modn() introduce .modrn()
Browse files Browse the repository at this point in the history
Fix: #112
Fix: #129
Fix: #130
  • Loading branch information
indutny committed Nov 29, 2017
1 parent 9827640 commit da8516f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ is the list of them in the order of appearance in the function name:

### Postfixes

The only available postfix at the moment is:

* `n` - which means that the argument of the function must be a plain JavaScript
* `n` - the argument of the function must be a plain JavaScript
Number. Decimals are not supported.
* `rn` - both argument and return value of the function are plain JavaScript
Numbers. Decimals are not supported.

### Examples

Expand Down
13 changes: 9 additions & 4 deletions lib/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@
var c = this.clone();
c.negative = 0;
while (!c.isZero()) {
var r = c.modn(groupBase).toString(base);
var r = c.modrn(groupBase).toString(base);
c = c.idivn(groupBase);

if (!c.isZero()) {
Expand Down Expand Up @@ -2417,13 +2417,13 @@
if (mode === 'mod') {
return {
div: null,
mod: new BN(this.modn(num.words[0]))
mod: new BN(this.modrn(num.words[0]))
};
}

return {
div: this.divn(num.words[0]),
mod: new BN(this.modn(num.words[0]))
mod: new BN(this.modrn(num.words[0]))
};
}

Expand Down Expand Up @@ -2464,7 +2464,7 @@
return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1);
};

BN.prototype.modn = function modn (num) {
BN.prototype.modrn = function modrn (num) {
assert(num <= 0x3ffffff);
var p = (1 << 26) % num;

Expand All @@ -2476,6 +2476,11 @@
return acc;
};

// WARNING: DEPRECATED
BN.prototype.modn = function modn (num) {
return this.modrn(num);
};

// In-place division by number
BN.prototype.idivn = function idivn (num) {
assert(num <= 0x3ffffff);
Expand Down
14 changes: 7 additions & 7 deletions test/arithmetic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,15 +527,15 @@ describe('BN.js/Arithmetic', function () {
});
});

describe('.modn()', function () {
describe('.modrn()', function () {
it('should act like .mod() on small numbers', function () {
assert.equal(new BN('10', 16).modn(256).toString(16), '10');
assert.equal(new BN('100', 16).modn(256).toString(16), '0');
assert.equal(new BN('1001', 16).modn(256).toString(16), '1');
assert.equal(new BN('100000000001', 16).modn(256).toString(16), '1');
assert.equal(new BN('100000000001', 16).modn(257).toString(16),
assert.equal(new BN('10', 16).modrn(256).toString(16), '10');
assert.equal(new BN('100', 16).modrn(256).toString(16), '0');
assert.equal(new BN('1001', 16).modrn(256).toString(16), '1');
assert.equal(new BN('100000000001', 16).modrn(256).toString(16), '1');
assert.equal(new BN('100000000001', 16).modrn(257).toString(16),
new BN('100000000001', 16).mod(new BN(257)).toString(16));
assert.equal(new BN('123456789012', 16).modn(3).toString(16),
assert.equal(new BN('123456789012', 16).modrn(3).toString(16),
new BN('123456789012', 16).mod(new BN(3)).toString(16));
});
});
Expand Down

0 comments on commit da8516f

Please sign in to comment.