bn.js 5.0.0 → 5.1.3

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 (4) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +2 -0
  3. package/lib/bn.js +111 -41
  4. package/package.json +22 -19
package/CHANGELOG.md CHANGED
@@ -1,3 +1,30 @@
1
+ 5.1.3 / 2020-08-14
2
+ ------------------
3
+
4
+ - Add support for defined but not implemented Symbol.for (#252)
5
+
6
+ 5.1.2 / 2020-05-20
7
+ ------------------
8
+
9
+ - Fix BN v5/v4 interoperability issue (#249)
10
+
11
+ 5.1.1 / 2019-12-24
12
+ ------------------
13
+
14
+ - Temporary workaround for BN#_move (#236)
15
+ - Add eslintrc instead config in package.json (#237)
16
+
17
+ 5.1.0 / 2019-12-23
18
+ ------------------
19
+
20
+ - Benchmark for BigInt (#226)
21
+ - Add documentation for max/min (#232)
22
+ - Update BN#inspect for Symbols (#225)
23
+ - Improve performance of toArrayLike (#222)
24
+ - temporary disable jumboMulTo in BN#mulTo (#221)
25
+ - optimize toBitArray function (#212)
26
+ - fix iaddn sign issue (#216)
27
+
1
28
  5.0.0 / 2019-07-04
2
29
  ------------------
3
30
 
package/README.md CHANGED
@@ -84,6 +84,8 @@ either `le` (little-endian) or `be` (big-endian).
84
84
  * `a.toTwos(width)` - convert to two's complement representation, where `width` is bit width
85
85
  * `a.fromTwos(width)` - convert from two's complement representation, where `width` is the bit width
86
86
  * `BN.isBN(object)` - returns true if the supplied `object` is a BN.js instance
87
+ * `BN.max(a, b)` - return `a` if `a` bigger than `b`
88
+ * `BN.min(a, b)` - return `a` if `a` less than `b`
87
89
 
88
90
  ### Arithmetics
89
91
 
package/lib/bn.js CHANGED
@@ -116,7 +116,7 @@
116
116
  number = -number;
117
117
  }
118
118
  if (number < 0x4000000) {
119
- this.words = [ number & 0x3ffffff ];
119
+ this.words = [number & 0x3ffffff];
120
120
  this.length = 1;
121
121
  } else if (number < 0x10000000000000) {
122
122
  this.words = [
@@ -144,7 +144,7 @@
144
144
  // Perhaps a Uint8Array
145
145
  assert(typeof number.length === 'number');
146
146
  if (number.length <= 0) {
147
- this.words = [ 0 ];
147
+ this.words = [0];
148
148
  this.length = 1;
149
149
  return this;
150
150
  }
@@ -274,7 +274,7 @@
274
274
 
275
275
  BN.prototype._parseBase = function _parseBase (number, base, start) {
276
276
  // Initialize as zero
277
- this.words = [ 0 ];
277
+ this.words = [0];
278
278
  this.length = 1;
279
279
 
280
280
  // Find length of limb in base
@@ -327,11 +327,15 @@
327
327
  dest.red = this.red;
328
328
  };
329
329
 
330
+ function move (dest, src) {
331
+ dest.words = src.words;
332
+ dest.length = src.length;
333
+ dest.negative = src.negative;
334
+ dest.red = src.red;
335
+ }
336
+
330
337
  BN.prototype._move = function _move (dest) {
331
- dest.words = this.words;
332
- dest.length = this.length;
333
- dest.negative = this.negative;
334
- dest.red = this.red;
338
+ move(dest, this);
335
339
  };
336
340
 
337
341
  BN.prototype.clone = function clone () {
@@ -363,9 +367,21 @@
363
367
  return this;
364
368
  };
365
369
 
366
- BN.prototype.inspect = function inspect () {
370
+ // Check Symbol.for because not everywhere where Symbol defined
371
+ // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#Browser_compatibility
372
+ if (typeof Symbol !== 'undefined' && typeof Symbol.for === 'function') {
373
+ try {
374
+ BN.prototype[Symbol.for('nodejs.util.inspect.custom')] = inspect;
375
+ } catch (e) {
376
+ BN.prototype.inspect = inspect;
377
+ }
378
+ } else {
379
+ BN.prototype.inspect = inspect;
380
+ }
381
+
382
+ function inspect () {
367
383
  return (this.red ? '<BN-R: ' : '<BN: ') + this.toString(16) + '>';
368
- };
384
+ }
369
385
 
370
386
  /*
371
387
 
@@ -540,51 +556,97 @@
540
556
  return this.toArrayLike(Array, endian, length);
541
557
  };
542
558
 
559
+ var allocate = function allocate (ArrayType, size) {
560
+ if (ArrayType.allocUnsafe) {
561
+ return ArrayType.allocUnsafe(size);
562
+ }
563
+ return new ArrayType(size);
564
+ };
565
+
543
566
  BN.prototype.toArrayLike = function toArrayLike (ArrayType, endian, length) {
567
+ this._strip();
568
+
544
569
  var byteLength = this.byteLength();
545
570
  var reqLength = length || Math.max(1, byteLength);
546
571
  assert(byteLength <= reqLength, 'byte array longer than desired length');
547
572
  assert(reqLength > 0, 'Requested array length <= 0');
548
573
 
549
- this._strip();
550
- var littleEndian = endian === 'le';
551
574
  var res = allocate(ArrayType, reqLength);
575
+ var postfix = endian === 'le' ? 'LE' : 'BE';
576
+ this['_toArrayLike' + postfix](res, byteLength);
577
+ return res;
578
+ };
579
+
580
+ BN.prototype._toArrayLikeLE = function _toArrayLikeLE (res, byteLength) {
581
+ var position = 0;
582
+ var carry = 0;
583
+
584
+ for (var i = 0, shift = 0; i < this.length; i++) {
585
+ var word = (this.words[i] << shift) | carry;
552
586
 
553
- var b, i;
554
- var q = this.clone();
555
- if (!littleEndian) {
556
- // Assume big-endian
557
- for (i = 0; i < reqLength - byteLength; i++) {
558
- res[i] = 0;
587
+ res[position++] = word & 0xff;
588
+ if (position < res.length) {
589
+ res[position++] = (word >> 8) & 0xff;
559
590
  }
591
+ if (position < res.length) {
592
+ res[position++] = (word >> 16) & 0xff;
593
+ }
594
+
595
+ if (shift === 6) {
596
+ if (position < res.length) {
597
+ res[position++] = (word >> 24) & 0xff;
598
+ }
599
+ carry = 0;
600
+ shift = 0;
601
+ } else {
602
+ carry = word >>> 24;
603
+ shift += 2;
604
+ }
605
+ }
560
606
 
561
- for (i = 0; !q.isZero(); i++) {
562
- b = q.andln(0xff);
563
- q.iushrn(8);
607
+ if (position < res.length) {
608
+ res[position++] = carry;
564
609
 
565
- res[reqLength - i - 1] = b;
610
+ while (position < res.length) {
611
+ res[position++] = 0;
566
612
  }
567
- } else {
568
- for (i = 0; !q.isZero(); i++) {
569
- b = q.andln(0xff);
570
- q.iushrn(8);
613
+ }
614
+ };
615
+
616
+ BN.prototype._toArrayLikeBE = function _toArrayLikeBE (res, byteLength) {
617
+ var position = res.length - 1;
618
+ var carry = 0;
619
+
620
+ for (var i = 0, shift = 0; i < this.length; i++) {
621
+ var word = (this.words[i] << shift) | carry;
571
622
 
572
- res[i] = b;
623
+ res[position--] = word & 0xff;
624
+ if (position >= 0) {
625
+ res[position--] = (word >> 8) & 0xff;
626
+ }
627
+ if (position >= 0) {
628
+ res[position--] = (word >> 16) & 0xff;
573
629
  }
574
630
 
575
- for (; i < reqLength; i++) {
576
- res[i] = 0;
631
+ if (shift === 6) {
632
+ if (position >= 0) {
633
+ res[position--] = (word >> 24) & 0xff;
634
+ }
635
+ carry = 0;
636
+ shift = 0;
637
+ } else {
638
+ carry = word >>> 24;
639
+ shift += 2;
577
640
  }
578
641
  }
579
642
 
580
- return res;
581
- };
643
+ if (position >= 0) {
644
+ res[position--] = carry;
582
645
 
583
- var allocate = function allocate (ArrayType, size) {
584
- if (ArrayType.allocUnsafe) {
585
- return ArrayType.allocUnsafe(size);
646
+ while (position >= 0) {
647
+ res[position--] = 0;
648
+ }
586
649
  }
587
- return new ArrayType(size);
588
650
  };
589
651
 
590
652
  if (Math.clz32) {
@@ -657,7 +719,7 @@
657
719
  var off = (bit / 26) | 0;
658
720
  var wbit = bit % 26;
659
721
 
660
- w[bit] = (num.words[off] & (1 << wbit)) >>> wbit;
722
+ w[bit] = (num.words[off] >>> wbit) & 0x01;
661
723
  }
662
724
 
663
725
  return w;
@@ -1679,8 +1741,10 @@
1679
1741
  }
1680
1742
 
1681
1743
  function jumboMulTo (self, num, out) {
1682
- var fftm = new FFTM();
1683
- return fftm.mulp(self, num, out);
1744
+ // Temporary disable, see https://github.com/indutny/bn.js/issues/211
1745
+ // var fftm = new FFTM();
1746
+ // return fftm.mulp(self, num, out);
1747
+ return bigMulTo(self, num, out);
1684
1748
  }
1685
1749
 
1686
1750
  BN.prototype.mulTo = function mulTo (num, out) {
@@ -2160,7 +2224,7 @@
2160
2224
 
2161
2225
  // Possible sign change
2162
2226
  if (this.negative !== 0) {
2163
- if (this.length === 1 && (this.words[0] | 0) < num) {
2227
+ if (this.length === 1 && (this.words[0] | 0) <= num) {
2164
2228
  this.words[0] = num - (this.words[0] | 0);
2165
2229
  this.negative = 0;
2166
2230
  return this;
@@ -2477,7 +2541,7 @@
2477
2541
  var cmp = mod.cmp(half);
2478
2542
 
2479
2543
  // Round down
2480
- if (cmp < 0 || r2 === 1 && cmp === 0) return dm.div;
2544
+ if (cmp < 0 || (r2 === 1 && cmp === 0)) return dm.div;
2481
2545
 
2482
2546
  // Round up
2483
2547
  return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1);
@@ -3013,7 +3077,13 @@
3013
3077
  } else if (cmp > 0) {
3014
3078
  r.isub(this.p);
3015
3079
  } else {
3016
- r._strip();
3080
+ if (r.strip !== undefined) {
3081
+ // r is a BN v4 instance
3082
+ r.strip();
3083
+ } else {
3084
+ // r is a BN v5 instance
3085
+ r._strip();
3086
+ }
3017
3087
  }
3018
3088
 
3019
3089
  return r;
@@ -3187,7 +3257,7 @@
3187
3257
  Red.prototype.imod = function imod (a) {
3188
3258
  if (this.prime) return this.prime.ireduce(a)._forceRed(this);
3189
3259
 
3190
- a.umod(this.m)._forceRed(this)._move(a);
3260
+ move(a, a.umod(this.m)._forceRed(this));
3191
3261
  return a;
3192
3262
  };
3193
3263
 
package/package.json CHANGED
@@ -1,39 +1,42 @@
1
1
  {
2
2
  "name": "bn.js",
3
- "version": "5.0.0",
3
+ "version": "5.1.3",
4
4
  "description": "Big number implementation in pure javascript",
5
- "main": "lib/bn.js",
6
- "scripts": {
7
- "lint": "semistandard",
8
- "unit": "mocha --reporter=spec test/*-test.js",
9
- "test": "npm run lint && npm run unit"
10
- },
11
- "repository": {
12
- "type": "git",
13
- "url": "git@github.com:indutny/bn.js"
14
- },
15
5
  "keywords": [
16
6
  "BN",
17
- "BigNum",
18
7
  "Big number",
8
+ "BigNum",
19
9
  "Modulo",
20
10
  "Montgomery"
21
11
  ],
22
- "author": "Fedor Indutny <fedor@indutny.com>",
23
- "license": "MIT",
12
+ "homepage": "https://github.com/indutny/bn.js",
24
13
  "bugs": {
25
14
  "url": "https://github.com/indutny/bn.js/issues"
26
15
  },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git@github.com:indutny/bn.js"
19
+ },
20
+ "license": "MIT",
21
+ "author": "Fedor Indutny <fedor@indutny.com>",
27
22
  "files": [
28
- "lib"
23
+ "lib/bn.js"
29
24
  ],
30
- "homepage": "https://github.com/indutny/bn.js",
25
+ "main": "lib/bn.js",
31
26
  "browser": {
32
27
  "buffer": false
33
28
  },
29
+ "scripts": {
30
+ "lint": "standardx",
31
+ "test": "npm run lint && npm run unit",
32
+ "unit": "mocha --reporter=spec test/*-test.js"
33
+ },
34
34
  "devDependencies": {
35
- "istanbul": "^0.3.5",
36
- "mocha": "^2.1.0",
37
- "semistandard": "^7.0.4"
35
+ "babel-eslint": "^10.0.3",
36
+ "mocha": "^7.0.1",
37
+ "standardx": "^5.0.0"
38
+ },
39
+ "standardx": {
40
+ "parser": "babel-eslint"
38
41
  }
39
42
  }