bn.js 5.2.3 → 5.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/bn.js +17 -6
  2. package/package.json +1 -1
package/lib/bn.js CHANGED
@@ -910,6 +910,12 @@
910
910
  // Handle the residue
911
911
  if (bitsLeft > 0) {
912
912
  this.words[i] = ~this.words[i] & (0x3ffffff >> (26 - bitsLeft));
913
+ i++;
914
+ }
915
+
916
+ // Clear words above the requested width so the result stays below 2 ** width
917
+ for (; i < this.length; i++) {
918
+ this.words[i] = 0;
913
919
  }
914
920
 
915
921
  // And remove leading zeroes
@@ -2008,7 +2014,10 @@
2008
2014
  this.words[i] = carry;
2009
2015
  this.length++;
2010
2016
  }
2011
- this.length = num === 0 ? 1 : this.length;
2017
+ if (num === 0) {
2018
+ this.length = 1;
2019
+ this._normSign();
2020
+ }
2012
2021
 
2013
2022
  return isNegNum ? this.ineg() : this;
2014
2023
  };
@@ -2547,17 +2556,19 @@
2547
2556
  // Fast case - exact division
2548
2557
  if (dm.mod.isZero()) return dm.div;
2549
2558
 
2550
- var mod = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod;
2559
+ var mod = dm.mod.abs();
2551
2560
 
2552
- var half = num.ushrn(1);
2553
- var r2 = num.andln(1);
2561
+ var half = num.abs().iushrn(1);
2562
+ var r2 = num.words[0] & 1;
2554
2563
  var cmp = mod.cmp(half);
2555
2564
 
2556
2565
  // Round down
2557
2566
  if (cmp < 0 || (r2 === 1 && cmp === 0)) return dm.div;
2558
2567
 
2559
- // Round up
2560
- return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1);
2568
+ // Round up, away from zero
2569
+ var up = new BN(1);
2570
+ up.negative = this.negative ^ num.negative;
2571
+ return dm.div.iadd(up);
2561
2572
  };
2562
2573
 
2563
2574
  BN.prototype.modrn = function modrn (num) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bn.js",
3
- "version": "5.2.3",
3
+ "version": "5.2.5",
4
4
  "description": "Big number implementation in pure javascript",
5
5
  "keywords": [
6
6
  "BN",