bn.js 4.11.9 → 5.1.1

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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,35 @@
1
+ 5.1.1 / 2019-12-24
2
+ ------------------
3
+
4
+ - Temporary workaround for BN#_move (#236)
5
+ - Add eslintrc instead config in package.json (#237)
6
+
7
+ 5.1.0 / 2019-12-23
8
+ ------------------
9
+
10
+ - Benchmark for BigInt (#226)
11
+ - Add documentation for max/min (#232)
12
+ - Update BN#inspect for Symbols (#225)
13
+ - Improve performance of toArrayLike (#222)
14
+ - temporary disable jumboMulTo in BN#mulTo (#221)
15
+ - optimize toBitArray function (#212)
16
+ - fix iaddn sign issue (#216)
17
+
18
+ 5.0.0 / 2019-07-04
19
+ ------------------
20
+
21
+ - travis: update node versions (#205)
22
+ - Refactor buffer constructor (#200)
23
+ - lib: fix for negative numbers: imuln, modrn, idivn (#185)
24
+ - bn: fix Red#imod (#178)
25
+ - check unexpected high bits for invalid characters (#173)
26
+ - document support very large integers (#158)
27
+ - only define toBuffer if Buffer is defined (#172)
28
+ - lib: better validation of string input (#151)
29
+ - tests: reject decimal input in constructor (#91)
30
+ - bn: make .strip() an internal method (#105)
31
+ - lib: deprecate `.modn()` introduce `.modrn()` (#112 #129 #130)
32
+ - bn: don't accept invalid characters (#141)
33
+ - package: use `files` insteadof `.npmignore` (#152)
34
+ - bn: improve allocation speed for buffers (#167)
35
+ - toJSON to default to interoperable hex (length % 2) (#164)
package/README.md CHANGED
@@ -37,10 +37,10 @@ is the list of them in the order of appearance in the function name:
37
37
 
38
38
  ### Postfixes
39
39
 
40
- The only available postfix at the moment is:
41
-
42
- * `n` - which means that the argument of the function must be a plain JavaScript
40
+ * `n` - the argument of the function must be a plain JavaScript
43
41
  Number. Decimals are not supported.
42
+ * `rn` - both argument and return value of the function are plain JavaScript
43
+ Numbers. Decimals are not supported.
44
44
 
45
45
  ### Examples
46
46
 
@@ -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
 
@@ -192,6 +194,11 @@ counterparts in red context:
192
194
  * `a.redNeg()`
193
195
  * `a.redPow(b)` - modular exponentiation
194
196
 
197
+ ### Number Size
198
+
199
+ Optimized for elliptic curves that work with 256-bit numbers.
200
+ There is no limitation on the size of the numbers.
201
+
195
202
  ## LICENSE
196
203
 
197
204
  This software is licensed under the MIT License.
package/lib/bn.js CHANGED
@@ -103,7 +103,7 @@
103
103
  this.negative = 1;
104
104
  }
105
105
 
106
- this.strip();
106
+ this._strip();
107
107
 
108
108
  if (endian !== 'le') return;
109
109
 
@@ -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
  }
@@ -180,30 +180,38 @@
180
180
  }
181
181
  }
182
182
  }
183
- return this.strip();
183
+ return this._strip();
184
184
  };
185
185
 
186
186
  function parseHex (str, start, end) {
187
187
  var r = 0;
188
188
  var len = Math.min(str.length, end);
189
+ var z = 0;
189
190
  for (var i = start; i < len; i++) {
190
191
  var c = str.charCodeAt(i) - 48;
191
192
 
192
193
  r <<= 4;
193
194
 
195
+ var b;
196
+
194
197
  // 'a' - 'f'
195
198
  if (c >= 49 && c <= 54) {
196
- r |= c - 49 + 0xa;
199
+ b = c - 49 + 0xa;
197
200
 
198
201
  // 'A' - 'F'
199
202
  } else if (c >= 17 && c <= 22) {
200
- r |= c - 17 + 0xa;
203
+ b = c - 17 + 0xa;
201
204
 
202
205
  // '0' - '9'
203
206
  } else {
204
- r |= c & 0xf;
207
+ b = c;
205
208
  }
209
+
210
+ r |= b;
211
+ z |= b;
206
212
  }
213
+
214
+ assert(!(z & 0xf0), 'Invalid character in ' + str);
207
215
  return r;
208
216
  }
209
217
 
@@ -234,11 +242,12 @@
234
242
  this.words[j] |= (w << off) & 0x3ffffff;
235
243
  this.words[j + 1] |= w >>> (26 - off) & 0x3fffff;
236
244
  }
237
- this.strip();
245
+ this._strip();
238
246
  };
239
247
 
240
248
  function parseBase (str, start, end, mul) {
241
249
  var r = 0;
250
+ var b = 0;
242
251
  var len = Math.min(str.length, end);
243
252
  for (var i = start; i < len; i++) {
244
253
  var c = str.charCodeAt(i) - 48;
@@ -247,23 +256,25 @@
247
256
 
248
257
  // 'a'
249
258
  if (c >= 49) {
250
- r += c - 49 + 0xa;
259
+ b = c - 49 + 0xa;
251
260
 
252
261
  // 'A'
253
262
  } else if (c >= 17) {
254
- r += c - 17 + 0xa;
263
+ b = c - 17 + 0xa;
255
264
 
256
265
  // '0' - '9'
257
266
  } else {
258
- r += c;
267
+ b = c;
259
268
  }
269
+ assert(c >= 0 && b < mul, 'Invalid character');
270
+ r += b;
260
271
  }
261
272
  return r;
262
273
  }
263
274
 
264
275
  BN.prototype._parseBase = function _parseBase (number, base, start) {
265
276
  // Initialize as zero
266
- this.words = [ 0 ];
277
+ this.words = [0];
267
278
  this.length = 1;
268
279
 
269
280
  // Find length of limb in base
@@ -316,6 +327,17 @@
316
327
  dest.red = this.red;
317
328
  };
318
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
+
337
+ BN.prototype._move = function _move (dest) {
338
+ move(dest, this);
339
+ };
340
+
319
341
  BN.prototype.clone = function clone () {
320
342
  var r = new BN(null);
321
343
  this.copy(r);
@@ -330,7 +352,7 @@
330
352
  };
331
353
 
332
354
  // Remove leading `0` from `this`
333
- BN.prototype.strip = function strip () {
355
+ BN.prototype._strip = function strip () {
334
356
  while (this.length > 1 && this.words[this.length - 1] === 0) {
335
357
  this.length--;
336
358
  }
@@ -345,9 +367,17 @@
345
367
  return this;
346
368
  };
347
369
 
348
- 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
+ BN.prototype[Symbol.for('nodejs.util.inspect.custom')] = inspect;
374
+ } else {
375
+ BN.prototype.inspect = inspect;
376
+ }
377
+
378
+ function inspect () {
349
379
  return (this.red ? '<BN-R: ' : '<BN: ') + this.toString(16) + '>';
350
- };
380
+ }
351
381
 
352
382
  /*
353
383
 
@@ -471,7 +501,7 @@
471
501
  var c = this.clone();
472
502
  c.negative = 0;
473
503
  while (!c.isZero()) {
474
- var r = c.modn(groupBase).toString(base);
504
+ var r = c.modrn(groupBase).toString(base);
475
505
  c = c.idivn(groupBase);
476
506
 
477
507
  if (!c.isZero()) {
@@ -509,56 +539,110 @@
509
539
  };
510
540
 
511
541
  BN.prototype.toJSON = function toJSON () {
512
- return this.toString(16);
542
+ return this.toString(16, 2);
513
543
  };
514
544
 
515
- BN.prototype.toBuffer = function toBuffer (endian, length) {
516
- assert(typeof Buffer !== 'undefined');
517
- return this.toArrayLike(Buffer, endian, length);
518
- };
545
+ if (Buffer) {
546
+ BN.prototype.toBuffer = function toBuffer (endian, length) {
547
+ return this.toArrayLike(Buffer, endian, length);
548
+ };
549
+ }
519
550
 
520
551
  BN.prototype.toArray = function toArray (endian, length) {
521
552
  return this.toArrayLike(Array, endian, length);
522
553
  };
523
554
 
555
+ var allocate = function allocate (ArrayType, size) {
556
+ if (ArrayType.allocUnsafe) {
557
+ return ArrayType.allocUnsafe(size);
558
+ }
559
+ return new ArrayType(size);
560
+ };
561
+
524
562
  BN.prototype.toArrayLike = function toArrayLike (ArrayType, endian, length) {
563
+ this._strip();
564
+
525
565
  var byteLength = this.byteLength();
526
566
  var reqLength = length || Math.max(1, byteLength);
527
567
  assert(byteLength <= reqLength, 'byte array longer than desired length');
528
568
  assert(reqLength > 0, 'Requested array length <= 0');
529
569
 
530
- this.strip();
531
- var littleEndian = endian === 'le';
532
- var res = new ArrayType(reqLength);
570
+ var res = allocate(ArrayType, reqLength);
571
+ var postfix = endian === 'le' ? 'LE' : 'BE';
572
+ this['_toArrayLike' + postfix](res, byteLength);
573
+ return res;
574
+ };
575
+
576
+ BN.prototype._toArrayLikeLE = function _toArrayLikeLE (res, byteLength) {
577
+ var position = 0;
578
+ var carry = 0;
579
+
580
+ for (var i = 0, shift = 0; i < this.length; i++) {
581
+ var word = (this.words[i] << shift) | carry;
533
582
 
534
- var b, i;
535
- var q = this.clone();
536
- if (!littleEndian) {
537
- // Assume big-endian
538
- for (i = 0; i < reqLength - byteLength; i++) {
539
- res[i] = 0;
583
+ res[position++] = word & 0xff;
584
+ if (position < res.length) {
585
+ res[position++] = (word >> 8) & 0xff;
586
+ }
587
+ if (position < res.length) {
588
+ res[position++] = (word >> 16) & 0xff;
540
589
  }
541
590
 
542
- for (i = 0; !q.isZero(); i++) {
543
- b = q.andln(0xff);
544
- q.iushrn(8);
591
+ if (shift === 6) {
592
+ if (position < res.length) {
593
+ res[position++] = (word >> 24) & 0xff;
594
+ }
595
+ carry = 0;
596
+ shift = 0;
597
+ } else {
598
+ carry = word >>> 24;
599
+ shift += 2;
600
+ }
601
+ }
545
602
 
546
- res[reqLength - i - 1] = b;
603
+ if (position < res.length) {
604
+ res[position++] = carry;
605
+
606
+ while (position < res.length) {
607
+ res[position++] = 0;
547
608
  }
548
- } else {
549
- for (i = 0; !q.isZero(); i++) {
550
- b = q.andln(0xff);
551
- q.iushrn(8);
609
+ }
610
+ };
611
+
612
+ BN.prototype._toArrayLikeBE = function _toArrayLikeBE (res, byteLength) {
613
+ var position = res.length - 1;
614
+ var carry = 0;
615
+
616
+ for (var i = 0, shift = 0; i < this.length; i++) {
617
+ var word = (this.words[i] << shift) | carry;
552
618
 
553
- res[i] = b;
619
+ res[position--] = word & 0xff;
620
+ if (position >= 0) {
621
+ res[position--] = (word >> 8) & 0xff;
622
+ }
623
+ if (position >= 0) {
624
+ res[position--] = (word >> 16) & 0xff;
554
625
  }
555
626
 
556
- for (; i < reqLength; i++) {
557
- res[i] = 0;
627
+ if (shift === 6) {
628
+ if (position >= 0) {
629
+ res[position--] = (word >> 24) & 0xff;
630
+ }
631
+ carry = 0;
632
+ shift = 0;
633
+ } else {
634
+ carry = word >>> 24;
635
+ shift += 2;
558
636
  }
559
637
  }
560
638
 
561
- return res;
639
+ if (position >= 0) {
640
+ res[position--] = carry;
641
+
642
+ while (position >= 0) {
643
+ res[position--] = 0;
644
+ }
645
+ }
562
646
  };
563
647
 
564
648
  if (Math.clz32) {
@@ -631,7 +715,7 @@
631
715
  var off = (bit / 26) | 0;
632
716
  var wbit = bit % 26;
633
717
 
634
- w[bit] = (num.words[off] & (1 << wbit)) >>> wbit;
718
+ w[bit] = (num.words[off] >>> wbit) & 0x01;
635
719
  }
636
720
 
637
721
  return w;
@@ -695,7 +779,7 @@
695
779
  this.words[i] = this.words[i] | num.words[i];
696
780
  }
697
781
 
698
- return this.strip();
782
+ return this._strip();
699
783
  };
700
784
 
701
785
  BN.prototype.ior = function ior (num) {
@@ -730,7 +814,7 @@
730
814
 
731
815
  this.length = b.length;
732
816
 
733
- return this.strip();
817
+ return this._strip();
734
818
  };
735
819
 
736
820
  BN.prototype.iand = function iand (num) {
@@ -774,7 +858,7 @@
774
858
 
775
859
  this.length = a.length;
776
860
 
777
- return this.strip();
861
+ return this._strip();
778
862
  };
779
863
 
780
864
  BN.prototype.ixor = function ixor (num) {
@@ -818,7 +902,7 @@
818
902
  }
819
903
 
820
904
  // And remove leading zeroes
821
- return this.strip();
905
+ return this._strip();
822
906
  };
823
907
 
824
908
  BN.prototype.notn = function notn (width) {
@@ -840,7 +924,7 @@
840
924
  this.words[off] = this.words[off] & ~(1 << wbit);
841
925
  }
842
926
 
843
- return this.strip();
927
+ return this._strip();
844
928
  };
845
929
 
846
930
  // Add `num` to `this` in-place
@@ -981,7 +1065,7 @@
981
1065
  this.negative = 1;
982
1066
  }
983
1067
 
984
- return this.strip();
1068
+ return this._strip();
985
1069
  };
986
1070
 
987
1071
  // Subtract `num` from `this`
@@ -1027,7 +1111,7 @@
1027
1111
  out.length--;
1028
1112
  }
1029
1113
 
1030
- return out.strip();
1114
+ return out._strip();
1031
1115
  }
1032
1116
 
1033
1117
  // TODO(indutny): it may be reasonable to omit it for users who don't need
@@ -1649,12 +1733,14 @@
1649
1733
  out.length--;
1650
1734
  }
1651
1735
 
1652
- return out.strip();
1736
+ return out._strip();
1653
1737
  }
1654
1738
 
1655
1739
  function jumboMulTo (self, num, out) {
1656
- var fftm = new FFTM();
1657
- return fftm.mulp(self, num, out);
1740
+ // Temporary disable, see https://github.com/indutny/bn.js/issues/211
1741
+ // var fftm = new FFTM();
1742
+ // return fftm.mulp(self, num, out);
1743
+ return bigMulTo(self, num, out);
1658
1744
  }
1659
1745
 
1660
1746
  BN.prototype.mulTo = function mulTo (num, out) {
@@ -1866,7 +1952,7 @@
1866
1952
 
1867
1953
  out.negative = x.negative ^ y.negative;
1868
1954
  out.length = x.length + y.length;
1869
- return out.strip();
1955
+ return out._strip();
1870
1956
  };
1871
1957
 
1872
1958
  // Multiply `this` by `num`
@@ -1889,6 +1975,9 @@
1889
1975
  };
1890
1976
 
1891
1977
  BN.prototype.imuln = function imuln (num) {
1978
+ var isNegNum = num < 0;
1979
+ if (isNegNum) num = -num;
1980
+
1892
1981
  assert(typeof num === 'number');
1893
1982
  assert(num < 0x4000000);
1894
1983
 
@@ -1909,7 +1998,7 @@
1909
1998
  this.length++;
1910
1999
  }
1911
2000
 
1912
- return this;
2001
+ return isNegNum ? this.ineg() : this;
1913
2002
  };
1914
2003
 
1915
2004
  BN.prototype.muln = function muln (num) {
@@ -1984,7 +2073,7 @@
1984
2073
  this.length += s;
1985
2074
  }
1986
2075
 
1987
- return this.strip();
2076
+ return this._strip();
1988
2077
  };
1989
2078
 
1990
2079
  BN.prototype.ishln = function ishln (bits) {
@@ -2050,7 +2139,7 @@
2050
2139
  this.length = 1;
2051
2140
  }
2052
2141
 
2053
- return this.strip();
2142
+ return this._strip();
2054
2143
  };
2055
2144
 
2056
2145
  BN.prototype.ishrn = function ishrn (bits, hint, extended) {
@@ -2115,7 +2204,7 @@
2115
2204
  this.words[this.length - 1] &= mask;
2116
2205
  }
2117
2206
 
2118
- return this.strip();
2207
+ return this._strip();
2119
2208
  };
2120
2209
 
2121
2210
  // Return only lowers bits of number
@@ -2131,7 +2220,7 @@
2131
2220
 
2132
2221
  // Possible sign change
2133
2222
  if (this.negative !== 0) {
2134
- if (this.length === 1 && (this.words[0] | 0) < num) {
2223
+ if (this.length === 1 && (this.words[0] | 0) <= num) {
2135
2224
  this.words[0] = num - (this.words[0] | 0);
2136
2225
  this.negative = 0;
2137
2226
  return this;
@@ -2190,7 +2279,7 @@
2190
2279
  }
2191
2280
  }
2192
2281
 
2193
- return this.strip();
2282
+ return this._strip();
2194
2283
  };
2195
2284
 
2196
2285
  BN.prototype.addn = function addn (num) {
@@ -2232,7 +2321,7 @@
2232
2321
  this.words[i + shift] = w & 0x3ffffff;
2233
2322
  }
2234
2323
 
2235
- if (carry === 0) return this.strip();
2324
+ if (carry === 0) return this._strip();
2236
2325
 
2237
2326
  // Subtraction overflow
2238
2327
  assert(carry === -1);
@@ -2244,7 +2333,7 @@
2244
2333
  }
2245
2334
  this.negative = 1;
2246
2335
 
2247
- return this.strip();
2336
+ return this._strip();
2248
2337
  };
2249
2338
 
2250
2339
  BN.prototype._wordDiv = function _wordDiv (num, mode) {
@@ -2306,9 +2395,9 @@
2306
2395
  }
2307
2396
  }
2308
2397
  if (q) {
2309
- q.strip();
2398
+ q._strip();
2310
2399
  }
2311
- a.strip();
2400
+ a._strip();
2312
2401
 
2313
2402
  // Denormalize
2314
2403
  if (mode !== 'div' && shift !== 0) {
@@ -2407,13 +2496,13 @@
2407
2496
  if (mode === 'mod') {
2408
2497
  return {
2409
2498
  div: null,
2410
- mod: new BN(this.modn(num.words[0]))
2499
+ mod: new BN(this.modrn(num.words[0]))
2411
2500
  };
2412
2501
  }
2413
2502
 
2414
2503
  return {
2415
2504
  div: this.divn(num.words[0]),
2416
- mod: new BN(this.modn(num.words[0]))
2505
+ mod: new BN(this.modrn(num.words[0]))
2417
2506
  };
2418
2507
  }
2419
2508
 
@@ -2448,13 +2537,16 @@
2448
2537
  var cmp = mod.cmp(half);
2449
2538
 
2450
2539
  // Round down
2451
- if (cmp < 0 || r2 === 1 && cmp === 0) return dm.div;
2540
+ if (cmp < 0 || (r2 === 1 && cmp === 0)) return dm.div;
2452
2541
 
2453
2542
  // Round up
2454
2543
  return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1);
2455
2544
  };
2456
2545
 
2457
- BN.prototype.modn = function modn (num) {
2546
+ BN.prototype.modrn = function modrn (num) {
2547
+ var isNegNum = num < 0;
2548
+ if (isNegNum) num = -num;
2549
+
2458
2550
  assert(num <= 0x3ffffff);
2459
2551
  var p = (1 << 26) % num;
2460
2552
 
@@ -2463,11 +2555,19 @@
2463
2555
  acc = (p * acc + (this.words[i] | 0)) % num;
2464
2556
  }
2465
2557
 
2466
- return acc;
2558
+ return isNegNum ? -acc : acc;
2559
+ };
2560
+
2561
+ // WARNING: DEPRECATED
2562
+ BN.prototype.modn = function modn (num) {
2563
+ return this.modrn(num);
2467
2564
  };
2468
2565
 
2469
2566
  // In-place division by number
2470
2567
  BN.prototype.idivn = function idivn (num) {
2568
+ var isNegNum = num < 0;
2569
+ if (isNegNum) num = -num;
2570
+
2471
2571
  assert(num <= 0x3ffffff);
2472
2572
 
2473
2573
  var carry = 0;
@@ -2477,7 +2577,8 @@
2477
2577
  carry = w % num;
2478
2578
  }
2479
2579
 
2480
- return this.strip();
2580
+ this._strip();
2581
+ return isNegNum ? this.ineg() : this;
2481
2582
  };
2482
2583
 
2483
2584
  BN.prototype.divn = function divn (num) {
@@ -2729,7 +2830,7 @@
2729
2830
  if (this.negative !== 0 && !negative) return -1;
2730
2831
  if (this.negative === 0 && negative) return 1;
2731
2832
 
2732
- this.strip();
2833
+ this._strip();
2733
2834
 
2734
2835
  var res;
2735
2836
  if (this.length > 1) {
@@ -2972,13 +3073,7 @@
2972
3073
  } else if (cmp > 0) {
2973
3074
  r.isub(this.p);
2974
3075
  } else {
2975
- if (r.strip !== undefined) {
2976
- // r is BN v4 instance
2977
- r.strip();
2978
- } else {
2979
- // r is BN v5 instance
2980
- r._strip();
2981
- }
3076
+ r._strip();
2982
3077
  }
2983
3078
 
2984
3079
  return r;
@@ -3151,7 +3246,9 @@
3151
3246
 
3152
3247
  Red.prototype.imod = function imod (a) {
3153
3248
  if (this.prime) return this.prime.ireduce(a)._forceRed(this);
3154
- return a.umod(this.m)._forceRed(this);
3249
+
3250
+ move(a, a.umod(this.m)._forceRed(this));
3251
+ return a;
3155
3252
  };
3156
3253
 
3157
3254
  Red.prototype.neg = function neg (a) {
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "bn.js",
3
- "version": "4.11.9",
3
+ "version": "5.1.1",
4
4
  "description": "Big number implementation in pure javascript",
5
5
  "main": "lib/bn.js",
6
6
  "scripts": {
7
- "lint": "semistandard",
7
+ "lint": "standardx",
8
8
  "unit": "mocha --reporter=spec test/*-test.js",
9
9
  "test": "npm run lint && npm run unit"
10
10
  },
@@ -24,13 +24,19 @@
24
24
  "bugs": {
25
25
  "url": "https://github.com/indutny/bn.js/issues"
26
26
  },
27
+ "files": [
28
+ "lib"
29
+ ],
27
30
  "homepage": "https://github.com/indutny/bn.js",
28
31
  "browser": {
29
32
  "buffer": false
30
33
  },
31
34
  "devDependencies": {
32
- "istanbul": "^0.3.5",
33
- "mocha": "^2.1.0",
34
- "semistandard": "^7.0.4"
35
+ "babel-eslint": "^10.0.2",
36
+ "mocha": "^6.1.4",
37
+ "standardx": "^4.0.0"
38
+ },
39
+ "standardx": {
40
+ "parser": "babel-eslint"
35
41
  }
36
42
  }
@@ -1,65 +0,0 @@
1
- 'use strict';
2
-
3
- // NOTE: This could be potentionally used to generate loop-less multiplications
4
- function genCombMulTo (alen, blen) {
5
- var len = alen + blen - 1;
6
- var src = [
7
- 'var a = self.words;',
8
- 'var b = num.words;',
9
- 'var o = out.words;',
10
- 'var c = 0;',
11
- 'var lo;',
12
- 'var mid;',
13
- 'var hi;'
14
- ];
15
- for (var i = 0; i < alen; i++) {
16
- src.push('var a' + i + ' = a[' + i + '] | 0;');
17
- src.push('var al' + i + ' = a' + i + ' & 0x1fff;');
18
- src.push('var ah' + i + ' = a' + i + ' >>> 13;');
19
- }
20
- for (i = 0; i < blen; i++) {
21
- src.push('var b' + i + ' = b[' + i + '] | 0;');
22
- src.push('var bl' + i + ' = b' + i + ' & 0x1fff;');
23
- src.push('var bh' + i + ' = b' + i + ' >>> 13;');
24
- }
25
- src.push('');
26
- src.push('out.negative = self.negative ^ num.negative;');
27
- src.push('out.length = ' + len + ';');
28
-
29
- for (var k = 0; k < len; k++) {
30
- var minJ = Math.max(0, k - alen + 1);
31
- var maxJ = Math.min(k, blen - 1);
32
-
33
- src.push('\/* k = ' + k + ' *\/');
34
- src.push('var w' + k + ' = c;');
35
- src.push('c = 0;');
36
- for (var j = minJ; j <= maxJ; j++) {
37
- i = k - j;
38
-
39
- src.push('lo = Math.imul(al' + i + ', bl' + j + ');');
40
- src.push('mid = Math.imul(al' + i + ', bh' + j + ');');
41
- src.push('mid = (mid + Math.imul(ah' + i + ', bl' + j + ')) | 0;');
42
- src.push('hi = Math.imul(ah' + i + ', bh' + j + ');');
43
-
44
- src.push('w' + k + ' = (w' + k + ' + lo) | 0;');
45
- src.push('w' + k + ' = (w' + k + ' + ((mid & 0x1fff) << 13)) | 0;');
46
- src.push('c = (c + hi) | 0;');
47
- src.push('c = (c + (mid >>> 13)) | 0;');
48
- src.push('c = (c + (w' + k + ' >>> 26)) | 0;');
49
- src.push('w' + k + ' &= 0x3ffffff;');
50
- }
51
- }
52
- // Store in separate step for better memory access
53
- for (k = 0; k < len; k++) {
54
- src.push('o[' + k + '] = w' + k + ';');
55
- }
56
- src.push('if (c !== 0) {',
57
- ' o[' + k + '] = c;',
58
- ' out.length++;',
59
- '}',
60
- 'return out;');
61
-
62
- return src.join('\n');
63
- }
64
-
65
- console.log(genCombMulTo(10, 10));
@@ -1,65 +0,0 @@
1
- 'use strict';
2
-
3
- function genCombMulTo (alen, blen) {
4
- var len = alen + blen - 1;
5
- var src = [
6
- 'var a = self.words;',
7
- 'var b = num.words;',
8
- 'var o = out.words;',
9
- 'var c = 0;',
10
- 'var lo;',
11
- 'var mid;',
12
- 'var hi;'
13
- ];
14
- for (var i = 0; i < alen; i++) {
15
- src.push('var a' + i + ' = a[' + i + '] | 0;');
16
- src.push('var al' + i + ' = a' + i + ' & 0x1fff;');
17
- src.push('var ah' + i + ' = a' + i + ' >>> 13;');
18
- }
19
- for (i = 0; i < blen; i++) {
20
- src.push('var b' + i + ' = b[' + i + '] | 0;');
21
- src.push('var bl' + i + ' = b' + i + ' & 0x1fff;');
22
- src.push('var bh' + i + ' = b' + i + ' >>> 13;');
23
- }
24
- src.push('');
25
- src.push('out.negative = self.negative ^ num.negative;');
26
- src.push('out.length = ' + len + ';');
27
-
28
- for (var k = 0; k < len; k++) {
29
- var minJ = Math.max(0, k - alen + 1);
30
- var maxJ = Math.min(k, blen - 1);
31
-
32
- src.push('\/* k = ' + k + ' *\/');
33
- src.push('lo = Math.imul(al' + (k - minJ) + ', bl' + minJ + ');');
34
- src.push('mid = Math.imul(al' + (k - minJ) + ', bh' + minJ + ');');
35
- src.push(
36
- 'mid = (mid + Math.imul(ah' + (k - minJ) + ', bl' + minJ + ')) | 0;');
37
- src.push('hi = Math.imul(ah' + (k - minJ) + ', bh' + minJ + ');');
38
-
39
- for (var j = minJ + 1; j <= maxJ; j++) {
40
- i = k - j;
41
-
42
- src.push('lo = (lo + Math.imul(al' + i + ', bl' + j + ')) | 0;');
43
- src.push('mid = (mid + Math.imul(al' + i + ', bh' + j + ')) | 0;');
44
- src.push('mid = (mid + Math.imul(ah' + i + ', bl' + j + ')) | 0;');
45
- src.push('hi = (hi + Math.imul(ah' + i + ', bh' + j + ')) | 0;');
46
- }
47
-
48
- src.push('var w' + k + ' = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;');
49
- src.push('c = (((hi + (mid >>> 13)) | 0) + (w' + k + ' >>> 26)) | 0;');
50
- src.push('w' + k + ' &= 0x3ffffff;');
51
- }
52
- // Store in separate step for better memory access
53
- for (k = 0; k < len; k++) {
54
- src.push('o[' + k + '] = w' + k + ';');
55
- }
56
- src.push('if (c !== 0) {',
57
- ' o[' + k + '] = c;',
58
- ' out.length++;',
59
- '}',
60
- 'return out;');
61
-
62
- return src.join('\n');
63
- }
64
-
65
- console.log(genCombMulTo(10, 10));