@leofcoin/chain 1.7.50 → 1.7.53

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.
@@ -1,4205 +1,3 @@
1
- var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
2
-
3
- function getDefaultExportFromCjs (x) {
4
- return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
5
- }
6
-
7
- function getAugmentedNamespace(n) {
8
- if (n.__esModule) return n;
9
- var f = n.default;
10
- if (typeof f == "function") {
11
- var a = function a () {
12
- if (this instanceof a) {
13
- return Reflect.construct(f, arguments, this.constructor);
14
- }
15
- return f.apply(this, arguments);
16
- };
17
- a.prototype = f.prototype;
18
- } else a = {};
19
- Object.defineProperty(a, '__esModule', {value: true});
20
- Object.keys(n).forEach(function (k) {
21
- var d = Object.getOwnPropertyDescriptor(n, k);
22
- Object.defineProperty(a, k, d.get ? d : {
23
- enumerable: true,
24
- get: function () {
25
- return n[k];
26
- }
27
- });
28
- });
29
- return a;
30
- }
31
-
32
- var bn = {exports: {}};
33
-
34
- var _nodeResolve_empty = {};
35
-
36
- var _nodeResolve_empty$1 = /*#__PURE__*/Object.freeze({
37
- __proto__: null,
38
- default: _nodeResolve_empty
39
- });
40
-
41
- var require$$0 = /*@__PURE__*/getAugmentedNamespace(_nodeResolve_empty$1);
42
-
43
- bn.exports;
44
-
45
- (function (module) {
46
- (function (module, exports) {
47
-
48
- // Utils
49
- function assert (val, msg) {
50
- if (!val) throw new Error(msg || 'Assertion failed');
51
- }
52
-
53
- // Could use `inherits` module, but don't want to move from single file
54
- // architecture yet.
55
- function inherits (ctor, superCtor) {
56
- ctor.super_ = superCtor;
57
- var TempCtor = function () {};
58
- TempCtor.prototype = superCtor.prototype;
59
- ctor.prototype = new TempCtor();
60
- ctor.prototype.constructor = ctor;
61
- }
62
-
63
- // BN
64
-
65
- function BN (number, base, endian) {
66
- if (BN.isBN(number)) {
67
- return number;
68
- }
69
-
70
- this.negative = 0;
71
- this.words = null;
72
- this.length = 0;
73
-
74
- // Reduction context
75
- this.red = null;
76
-
77
- if (number !== null) {
78
- if (base === 'le' || base === 'be') {
79
- endian = base;
80
- base = 10;
81
- }
82
-
83
- this._init(number || 0, base || 10, endian || 'be');
84
- }
85
- }
86
- if (typeof module === 'object') {
87
- module.exports = BN;
88
- } else {
89
- exports.BN = BN;
90
- }
91
-
92
- BN.BN = BN;
93
- BN.wordSize = 26;
94
-
95
- var Buffer;
96
- try {
97
- if (typeof window !== 'undefined' && typeof window.Buffer !== 'undefined') {
98
- Buffer = window.Buffer;
99
- } else {
100
- Buffer = require$$0.Buffer;
101
- }
102
- } catch (e) {
103
- }
104
-
105
- BN.isBN = function isBN (num) {
106
- if (num instanceof BN) {
107
- return true;
108
- }
109
-
110
- return num !== null && typeof num === 'object' &&
111
- num.constructor.wordSize === BN.wordSize && Array.isArray(num.words);
112
- };
113
-
114
- BN.max = function max (left, right) {
115
- if (left.cmp(right) > 0) return left;
116
- return right;
117
- };
118
-
119
- BN.min = function min (left, right) {
120
- if (left.cmp(right) < 0) return left;
121
- return right;
122
- };
123
-
124
- BN.prototype._init = function init (number, base, endian) {
125
- if (typeof number === 'number') {
126
- return this._initNumber(number, base, endian);
127
- }
128
-
129
- if (typeof number === 'object') {
130
- return this._initArray(number, base, endian);
131
- }
132
-
133
- if (base === 'hex') {
134
- base = 16;
135
- }
136
- assert(base === (base | 0) && base >= 2 && base <= 36);
137
-
138
- number = number.toString().replace(/\s+/g, '');
139
- var start = 0;
140
- if (number[0] === '-') {
141
- start++;
142
- this.negative = 1;
143
- }
144
-
145
- if (start < number.length) {
146
- if (base === 16) {
147
- this._parseHex(number, start, endian);
148
- } else {
149
- this._parseBase(number, base, start);
150
- if (endian === 'le') {
151
- this._initArray(this.toArray(), base, endian);
152
- }
153
- }
154
- }
155
- };
156
-
157
- BN.prototype._initNumber = function _initNumber (number, base, endian) {
158
- if (number < 0) {
159
- this.negative = 1;
160
- number = -number;
161
- }
162
- if (number < 0x4000000) {
163
- this.words = [number & 0x3ffffff];
164
- this.length = 1;
165
- } else if (number < 0x10000000000000) {
166
- this.words = [
167
- number & 0x3ffffff,
168
- (number / 0x4000000) & 0x3ffffff
169
- ];
170
- this.length = 2;
171
- } else {
172
- assert(number < 0x20000000000000); // 2 ^ 53 (unsafe)
173
- this.words = [
174
- number & 0x3ffffff,
175
- (number / 0x4000000) & 0x3ffffff,
176
- 1
177
- ];
178
- this.length = 3;
179
- }
180
-
181
- if (endian !== 'le') return;
182
-
183
- // Reverse the bytes
184
- this._initArray(this.toArray(), base, endian);
185
- };
186
-
187
- BN.prototype._initArray = function _initArray (number, base, endian) {
188
- // Perhaps a Uint8Array
189
- assert(typeof number.length === 'number');
190
- if (number.length <= 0) {
191
- this.words = [0];
192
- this.length = 1;
193
- return this;
194
- }
195
-
196
- this.length = Math.ceil(number.length / 3);
197
- this.words = new Array(this.length);
198
- for (var i = 0; i < this.length; i++) {
199
- this.words[i] = 0;
200
- }
201
-
202
- var j, w;
203
- var off = 0;
204
- if (endian === 'be') {
205
- for (i = number.length - 1, j = 0; i >= 0; i -= 3) {
206
- w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16);
207
- this.words[j] |= (w << off) & 0x3ffffff;
208
- this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff;
209
- off += 24;
210
- if (off >= 26) {
211
- off -= 26;
212
- j++;
213
- }
214
- }
215
- } else if (endian === 'le') {
216
- for (i = 0, j = 0; i < number.length; i += 3) {
217
- w = number[i] | (number[i + 1] << 8) | (number[i + 2] << 16);
218
- this.words[j] |= (w << off) & 0x3ffffff;
219
- this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff;
220
- off += 24;
221
- if (off >= 26) {
222
- off -= 26;
223
- j++;
224
- }
225
- }
226
- }
227
- return this._strip();
228
- };
229
-
230
- function parseHex4Bits (string, index) {
231
- var c = string.charCodeAt(index);
232
- // '0' - '9'
233
- if (c >= 48 && c <= 57) {
234
- return c - 48;
235
- // 'A' - 'F'
236
- } else if (c >= 65 && c <= 70) {
237
- return c - 55;
238
- // 'a' - 'f'
239
- } else if (c >= 97 && c <= 102) {
240
- return c - 87;
241
- } else {
242
- assert(false, 'Invalid character in ' + string);
243
- }
244
- }
245
-
246
- function parseHexByte (string, lowerBound, index) {
247
- var r = parseHex4Bits(string, index);
248
- if (index - 1 >= lowerBound) {
249
- r |= parseHex4Bits(string, index - 1) << 4;
250
- }
251
- return r;
252
- }
253
-
254
- BN.prototype._parseHex = function _parseHex (number, start, endian) {
255
- // Create possibly bigger array to ensure that it fits the number
256
- this.length = Math.ceil((number.length - start) / 6);
257
- this.words = new Array(this.length);
258
- for (var i = 0; i < this.length; i++) {
259
- this.words[i] = 0;
260
- }
261
-
262
- // 24-bits chunks
263
- var off = 0;
264
- var j = 0;
265
-
266
- var w;
267
- if (endian === 'be') {
268
- for (i = number.length - 1; i >= start; i -= 2) {
269
- w = parseHexByte(number, start, i) << off;
270
- this.words[j] |= w & 0x3ffffff;
271
- if (off >= 18) {
272
- off -= 18;
273
- j += 1;
274
- this.words[j] |= w >>> 26;
275
- } else {
276
- off += 8;
277
- }
278
- }
279
- } else {
280
- var parseLength = number.length - start;
281
- for (i = parseLength % 2 === 0 ? start + 1 : start; i < number.length; i += 2) {
282
- w = parseHexByte(number, start, i) << off;
283
- this.words[j] |= w & 0x3ffffff;
284
- if (off >= 18) {
285
- off -= 18;
286
- j += 1;
287
- this.words[j] |= w >>> 26;
288
- } else {
289
- off += 8;
290
- }
291
- }
292
- }
293
-
294
- this._strip();
295
- };
296
-
297
- function parseBase (str, start, end, mul) {
298
- var r = 0;
299
- var b = 0;
300
- var len = Math.min(str.length, end);
301
- for (var i = start; i < len; i++) {
302
- var c = str.charCodeAt(i) - 48;
303
-
304
- r *= mul;
305
-
306
- // 'a'
307
- if (c >= 49) {
308
- b = c - 49 + 0xa;
309
-
310
- // 'A'
311
- } else if (c >= 17) {
312
- b = c - 17 + 0xa;
313
-
314
- // '0' - '9'
315
- } else {
316
- b = c;
317
- }
318
- assert(c >= 0 && b < mul, 'Invalid character');
319
- r += b;
320
- }
321
- return r;
322
- }
323
-
324
- BN.prototype._parseBase = function _parseBase (number, base, start) {
325
- // Initialize as zero
326
- this.words = [0];
327
- this.length = 1;
328
-
329
- // Find length of limb in base
330
- for (var limbLen = 0, limbPow = 1; limbPow <= 0x3ffffff; limbPow *= base) {
331
- limbLen++;
332
- }
333
- limbLen--;
334
- limbPow = (limbPow / base) | 0;
335
-
336
- var total = number.length - start;
337
- var mod = total % limbLen;
338
- var end = Math.min(total, total - mod) + start;
339
-
340
- var word = 0;
341
- for (var i = start; i < end; i += limbLen) {
342
- word = parseBase(number, i, i + limbLen, base);
343
-
344
- this.imuln(limbPow);
345
- if (this.words[0] + word < 0x4000000) {
346
- this.words[0] += word;
347
- } else {
348
- this._iaddn(word);
349
- }
350
- }
351
-
352
- if (mod !== 0) {
353
- var pow = 1;
354
- word = parseBase(number, i, number.length, base);
355
-
356
- for (i = 0; i < mod; i++) {
357
- pow *= base;
358
- }
359
-
360
- this.imuln(pow);
361
- if (this.words[0] + word < 0x4000000) {
362
- this.words[0] += word;
363
- } else {
364
- this._iaddn(word);
365
- }
366
- }
367
-
368
- this._strip();
369
- };
370
-
371
- BN.prototype.copy = function copy (dest) {
372
- dest.words = new Array(this.length);
373
- for (var i = 0; i < this.length; i++) {
374
- dest.words[i] = this.words[i];
375
- }
376
- dest.length = this.length;
377
- dest.negative = this.negative;
378
- dest.red = this.red;
379
- };
380
-
381
- function move (dest, src) {
382
- dest.words = src.words;
383
- dest.length = src.length;
384
- dest.negative = src.negative;
385
- dest.red = src.red;
386
- }
387
-
388
- BN.prototype._move = function _move (dest) {
389
- move(dest, this);
390
- };
391
-
392
- BN.prototype.clone = function clone () {
393
- var r = new BN(null);
394
- this.copy(r);
395
- return r;
396
- };
397
-
398
- BN.prototype._expand = function _expand (size) {
399
- while (this.length < size) {
400
- this.words[this.length++] = 0;
401
- }
402
- return this;
403
- };
404
-
405
- // Remove leading `0` from `this`
406
- BN.prototype._strip = function strip () {
407
- while (this.length > 1 && this.words[this.length - 1] === 0) {
408
- this.length--;
409
- }
410
- return this._normSign();
411
- };
412
-
413
- BN.prototype._normSign = function _normSign () {
414
- // -0 = 0
415
- if (this.length === 1 && this.words[0] === 0) {
416
- this.negative = 0;
417
- }
418
- return this;
419
- };
420
-
421
- // Check Symbol.for because not everywhere where Symbol defined
422
- // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#Browser_compatibility
423
- if (typeof Symbol !== 'undefined' && typeof Symbol.for === 'function') {
424
- try {
425
- BN.prototype[Symbol.for('nodejs.util.inspect.custom')] = inspect;
426
- } catch (e) {
427
- BN.prototype.inspect = inspect;
428
- }
429
- } else {
430
- BN.prototype.inspect = inspect;
431
- }
432
-
433
- function inspect () {
434
- return (this.red ? '<BN-R: ' : '<BN: ') + this.toString(16) + '>';
435
- }
436
-
437
- /*
438
-
439
- var zeros = [];
440
- var groupSizes = [];
441
- var groupBases = [];
442
-
443
- var s = '';
444
- var i = -1;
445
- while (++i < BN.wordSize) {
446
- zeros[i] = s;
447
- s += '0';
448
- }
449
- groupSizes[0] = 0;
450
- groupSizes[1] = 0;
451
- groupBases[0] = 0;
452
- groupBases[1] = 0;
453
- var base = 2 - 1;
454
- while (++base < 36 + 1) {
455
- var groupSize = 0;
456
- var groupBase = 1;
457
- while (groupBase < (1 << BN.wordSize) / base) {
458
- groupBase *= base;
459
- groupSize += 1;
460
- }
461
- groupSizes[base] = groupSize;
462
- groupBases[base] = groupBase;
463
- }
464
-
465
- */
466
-
467
- var zeros = [
468
- '',
469
- '0',
470
- '00',
471
- '000',
472
- '0000',
473
- '00000',
474
- '000000',
475
- '0000000',
476
- '00000000',
477
- '000000000',
478
- '0000000000',
479
- '00000000000',
480
- '000000000000',
481
- '0000000000000',
482
- '00000000000000',
483
- '000000000000000',
484
- '0000000000000000',
485
- '00000000000000000',
486
- '000000000000000000',
487
- '0000000000000000000',
488
- '00000000000000000000',
489
- '000000000000000000000',
490
- '0000000000000000000000',
491
- '00000000000000000000000',
492
- '000000000000000000000000',
493
- '0000000000000000000000000'
494
- ];
495
-
496
- var groupSizes = [
497
- 0, 0,
498
- 25, 16, 12, 11, 10, 9, 8,
499
- 8, 7, 7, 7, 7, 6, 6,
500
- 6, 6, 6, 6, 6, 5, 5,
501
- 5, 5, 5, 5, 5, 5, 5,
502
- 5, 5, 5, 5, 5, 5, 5
503
- ];
504
-
505
- var groupBases = [
506
- 0, 0,
507
- 33554432, 43046721, 16777216, 48828125, 60466176, 40353607, 16777216,
508
- 43046721, 10000000, 19487171, 35831808, 62748517, 7529536, 11390625,
509
- 16777216, 24137569, 34012224, 47045881, 64000000, 4084101, 5153632,
510
- 6436343, 7962624, 9765625, 11881376, 14348907, 17210368, 20511149,
511
- 24300000, 28629151, 33554432, 39135393, 45435424, 52521875, 60466176
512
- ];
513
-
514
- BN.prototype.toString = function toString (base, padding) {
515
- base = base || 10;
516
- padding = padding | 0 || 1;
517
-
518
- var out;
519
- if (base === 16 || base === 'hex') {
520
- out = '';
521
- var off = 0;
522
- var carry = 0;
523
- for (var i = 0; i < this.length; i++) {
524
- var w = this.words[i];
525
- var word = (((w << off) | carry) & 0xffffff).toString(16);
526
- carry = (w >>> (24 - off)) & 0xffffff;
527
- off += 2;
528
- if (off >= 26) {
529
- off -= 26;
530
- i--;
531
- }
532
- if (carry !== 0 || i !== this.length - 1) {
533
- out = zeros[6 - word.length] + word + out;
534
- } else {
535
- out = word + out;
536
- }
537
- }
538
- if (carry !== 0) {
539
- out = carry.toString(16) + out;
540
- }
541
- while (out.length % padding !== 0) {
542
- out = '0' + out;
543
- }
544
- if (this.negative !== 0) {
545
- out = '-' + out;
546
- }
547
- return out;
548
- }
549
-
550
- if (base === (base | 0) && base >= 2 && base <= 36) {
551
- // var groupSize = Math.floor(BN.wordSize * Math.LN2 / Math.log(base));
552
- var groupSize = groupSizes[base];
553
- // var groupBase = Math.pow(base, groupSize);
554
- var groupBase = groupBases[base];
555
- out = '';
556
- var c = this.clone();
557
- c.negative = 0;
558
- while (!c.isZero()) {
559
- var r = c.modrn(groupBase).toString(base);
560
- c = c.idivn(groupBase);
561
-
562
- if (!c.isZero()) {
563
- out = zeros[groupSize - r.length] + r + out;
564
- } else {
565
- out = r + out;
566
- }
567
- }
568
- if (this.isZero()) {
569
- out = '0' + out;
570
- }
571
- while (out.length % padding !== 0) {
572
- out = '0' + out;
573
- }
574
- if (this.negative !== 0) {
575
- out = '-' + out;
576
- }
577
- return out;
578
- }
579
-
580
- assert(false, 'Base should be between 2 and 36');
581
- };
582
-
583
- BN.prototype.toNumber = function toNumber () {
584
- var ret = this.words[0];
585
- if (this.length === 2) {
586
- ret += this.words[1] * 0x4000000;
587
- } else if (this.length === 3 && this.words[2] === 0x01) {
588
- // NOTE: at this stage it is known that the top bit is set
589
- ret += 0x10000000000000 + (this.words[1] * 0x4000000);
590
- } else if (this.length > 2) {
591
- assert(false, 'Number can only safely store up to 53 bits');
592
- }
593
- return (this.negative !== 0) ? -ret : ret;
594
- };
595
-
596
- BN.prototype.toJSON = function toJSON () {
597
- return this.toString(16, 2);
598
- };
599
-
600
- if (Buffer) {
601
- BN.prototype.toBuffer = function toBuffer (endian, length) {
602
- return this.toArrayLike(Buffer, endian, length);
603
- };
604
- }
605
-
606
- BN.prototype.toArray = function toArray (endian, length) {
607
- return this.toArrayLike(Array, endian, length);
608
- };
609
-
610
- var allocate = function allocate (ArrayType, size) {
611
- if (ArrayType.allocUnsafe) {
612
- return ArrayType.allocUnsafe(size);
613
- }
614
- return new ArrayType(size);
615
- };
616
-
617
- BN.prototype.toArrayLike = function toArrayLike (ArrayType, endian, length) {
618
- this._strip();
619
-
620
- var byteLength = this.byteLength();
621
- var reqLength = length || Math.max(1, byteLength);
622
- assert(byteLength <= reqLength, 'byte array longer than desired length');
623
- assert(reqLength > 0, 'Requested array length <= 0');
624
-
625
- var res = allocate(ArrayType, reqLength);
626
- var postfix = endian === 'le' ? 'LE' : 'BE';
627
- this['_toArrayLike' + postfix](res, byteLength);
628
- return res;
629
- };
630
-
631
- BN.prototype._toArrayLikeLE = function _toArrayLikeLE (res, byteLength) {
632
- var position = 0;
633
- var carry = 0;
634
-
635
- for (var i = 0, shift = 0; i < this.length; i++) {
636
- var word = (this.words[i] << shift) | carry;
637
-
638
- res[position++] = word & 0xff;
639
- if (position < res.length) {
640
- res[position++] = (word >> 8) & 0xff;
641
- }
642
- if (position < res.length) {
643
- res[position++] = (word >> 16) & 0xff;
644
- }
645
-
646
- if (shift === 6) {
647
- if (position < res.length) {
648
- res[position++] = (word >> 24) & 0xff;
649
- }
650
- carry = 0;
651
- shift = 0;
652
- } else {
653
- carry = word >>> 24;
654
- shift += 2;
655
- }
656
- }
657
-
658
- if (position < res.length) {
659
- res[position++] = carry;
660
-
661
- while (position < res.length) {
662
- res[position++] = 0;
663
- }
664
- }
665
- };
666
-
667
- BN.prototype._toArrayLikeBE = function _toArrayLikeBE (res, byteLength) {
668
- var position = res.length - 1;
669
- var carry = 0;
670
-
671
- for (var i = 0, shift = 0; i < this.length; i++) {
672
- var word = (this.words[i] << shift) | carry;
673
-
674
- res[position--] = word & 0xff;
675
- if (position >= 0) {
676
- res[position--] = (word >> 8) & 0xff;
677
- }
678
- if (position >= 0) {
679
- res[position--] = (word >> 16) & 0xff;
680
- }
681
-
682
- if (shift === 6) {
683
- if (position >= 0) {
684
- res[position--] = (word >> 24) & 0xff;
685
- }
686
- carry = 0;
687
- shift = 0;
688
- } else {
689
- carry = word >>> 24;
690
- shift += 2;
691
- }
692
- }
693
-
694
- if (position >= 0) {
695
- res[position--] = carry;
696
-
697
- while (position >= 0) {
698
- res[position--] = 0;
699
- }
700
- }
701
- };
702
-
703
- if (Math.clz32) {
704
- BN.prototype._countBits = function _countBits (w) {
705
- return 32 - Math.clz32(w);
706
- };
707
- } else {
708
- BN.prototype._countBits = function _countBits (w) {
709
- var t = w;
710
- var r = 0;
711
- if (t >= 0x1000) {
712
- r += 13;
713
- t >>>= 13;
714
- }
715
- if (t >= 0x40) {
716
- r += 7;
717
- t >>>= 7;
718
- }
719
- if (t >= 0x8) {
720
- r += 4;
721
- t >>>= 4;
722
- }
723
- if (t >= 0x02) {
724
- r += 2;
725
- t >>>= 2;
726
- }
727
- return r + t;
728
- };
729
- }
730
-
731
- BN.prototype._zeroBits = function _zeroBits (w) {
732
- // Short-cut
733
- if (w === 0) return 26;
734
-
735
- var t = w;
736
- var r = 0;
737
- if ((t & 0x1fff) === 0) {
738
- r += 13;
739
- t >>>= 13;
740
- }
741
- if ((t & 0x7f) === 0) {
742
- r += 7;
743
- t >>>= 7;
744
- }
745
- if ((t & 0xf) === 0) {
746
- r += 4;
747
- t >>>= 4;
748
- }
749
- if ((t & 0x3) === 0) {
750
- r += 2;
751
- t >>>= 2;
752
- }
753
- if ((t & 0x1) === 0) {
754
- r++;
755
- }
756
- return r;
757
- };
758
-
759
- // Return number of used bits in a BN
760
- BN.prototype.bitLength = function bitLength () {
761
- var w = this.words[this.length - 1];
762
- var hi = this._countBits(w);
763
- return (this.length - 1) * 26 + hi;
764
- };
765
-
766
- function toBitArray (num) {
767
- var w = new Array(num.bitLength());
768
-
769
- for (var bit = 0; bit < w.length; bit++) {
770
- var off = (bit / 26) | 0;
771
- var wbit = bit % 26;
772
-
773
- w[bit] = (num.words[off] >>> wbit) & 0x01;
774
- }
775
-
776
- return w;
777
- }
778
-
779
- // Number of trailing zero bits
780
- BN.prototype.zeroBits = function zeroBits () {
781
- if (this.isZero()) return 0;
782
-
783
- var r = 0;
784
- for (var i = 0; i < this.length; i++) {
785
- var b = this._zeroBits(this.words[i]);
786
- r += b;
787
- if (b !== 26) break;
788
- }
789
- return r;
790
- };
791
-
792
- BN.prototype.byteLength = function byteLength () {
793
- return Math.ceil(this.bitLength() / 8);
794
- };
795
-
796
- BN.prototype.toTwos = function toTwos (width) {
797
- if (this.negative !== 0) {
798
- return this.abs().inotn(width).iaddn(1);
799
- }
800
- return this.clone();
801
- };
802
-
803
- BN.prototype.fromTwos = function fromTwos (width) {
804
- if (this.testn(width - 1)) {
805
- return this.notn(width).iaddn(1).ineg();
806
- }
807
- return this.clone();
808
- };
809
-
810
- BN.prototype.isNeg = function isNeg () {
811
- return this.negative !== 0;
812
- };
813
-
814
- // Return negative clone of `this`
815
- BN.prototype.neg = function neg () {
816
- return this.clone().ineg();
817
- };
818
-
819
- BN.prototype.ineg = function ineg () {
820
- if (!this.isZero()) {
821
- this.negative ^= 1;
822
- }
823
-
824
- return this;
825
- };
826
-
827
- // Or `num` with `this` in-place
828
- BN.prototype.iuor = function iuor (num) {
829
- while (this.length < num.length) {
830
- this.words[this.length++] = 0;
831
- }
832
-
833
- for (var i = 0; i < num.length; i++) {
834
- this.words[i] = this.words[i] | num.words[i];
835
- }
836
-
837
- return this._strip();
838
- };
839
-
840
- BN.prototype.ior = function ior (num) {
841
- assert((this.negative | num.negative) === 0);
842
- return this.iuor(num);
843
- };
844
-
845
- // Or `num` with `this`
846
- BN.prototype.or = function or (num) {
847
- if (this.length > num.length) return this.clone().ior(num);
848
- return num.clone().ior(this);
849
- };
850
-
851
- BN.prototype.uor = function uor (num) {
852
- if (this.length > num.length) return this.clone().iuor(num);
853
- return num.clone().iuor(this);
854
- };
855
-
856
- // And `num` with `this` in-place
857
- BN.prototype.iuand = function iuand (num) {
858
- // b = min-length(num, this)
859
- var b;
860
- if (this.length > num.length) {
861
- b = num;
862
- } else {
863
- b = this;
864
- }
865
-
866
- for (var i = 0; i < b.length; i++) {
867
- this.words[i] = this.words[i] & num.words[i];
868
- }
869
-
870
- this.length = b.length;
871
-
872
- return this._strip();
873
- };
874
-
875
- BN.prototype.iand = function iand (num) {
876
- assert((this.negative | num.negative) === 0);
877
- return this.iuand(num);
878
- };
879
-
880
- // And `num` with `this`
881
- BN.prototype.and = function and (num) {
882
- if (this.length > num.length) return this.clone().iand(num);
883
- return num.clone().iand(this);
884
- };
885
-
886
- BN.prototype.uand = function uand (num) {
887
- if (this.length > num.length) return this.clone().iuand(num);
888
- return num.clone().iuand(this);
889
- };
890
-
891
- // Xor `num` with `this` in-place
892
- BN.prototype.iuxor = function iuxor (num) {
893
- // a.length > b.length
894
- var a;
895
- var b;
896
- if (this.length > num.length) {
897
- a = this;
898
- b = num;
899
- } else {
900
- a = num;
901
- b = this;
902
- }
903
-
904
- for (var i = 0; i < b.length; i++) {
905
- this.words[i] = a.words[i] ^ b.words[i];
906
- }
907
-
908
- if (this !== a) {
909
- for (; i < a.length; i++) {
910
- this.words[i] = a.words[i];
911
- }
912
- }
913
-
914
- this.length = a.length;
915
-
916
- return this._strip();
917
- };
918
-
919
- BN.prototype.ixor = function ixor (num) {
920
- assert((this.negative | num.negative) === 0);
921
- return this.iuxor(num);
922
- };
923
-
924
- // Xor `num` with `this`
925
- BN.prototype.xor = function xor (num) {
926
- if (this.length > num.length) return this.clone().ixor(num);
927
- return num.clone().ixor(this);
928
- };
929
-
930
- BN.prototype.uxor = function uxor (num) {
931
- if (this.length > num.length) return this.clone().iuxor(num);
932
- return num.clone().iuxor(this);
933
- };
934
-
935
- // Not ``this`` with ``width`` bitwidth
936
- BN.prototype.inotn = function inotn (width) {
937
- assert(typeof width === 'number' && width >= 0);
938
-
939
- var bytesNeeded = Math.ceil(width / 26) | 0;
940
- var bitsLeft = width % 26;
941
-
942
- // Extend the buffer with leading zeroes
943
- this._expand(bytesNeeded);
944
-
945
- if (bitsLeft > 0) {
946
- bytesNeeded--;
947
- }
948
-
949
- // Handle complete words
950
- for (var i = 0; i < bytesNeeded; i++) {
951
- this.words[i] = ~this.words[i] & 0x3ffffff;
952
- }
953
-
954
- // Handle the residue
955
- if (bitsLeft > 0) {
956
- this.words[i] = ~this.words[i] & (0x3ffffff >> (26 - bitsLeft));
957
- }
958
-
959
- // And remove leading zeroes
960
- return this._strip();
961
- };
962
-
963
- BN.prototype.notn = function notn (width) {
964
- return this.clone().inotn(width);
965
- };
966
-
967
- // Set `bit` of `this`
968
- BN.prototype.setn = function setn (bit, val) {
969
- assert(typeof bit === 'number' && bit >= 0);
970
-
971
- var off = (bit / 26) | 0;
972
- var wbit = bit % 26;
973
-
974
- this._expand(off + 1);
975
-
976
- if (val) {
977
- this.words[off] = this.words[off] | (1 << wbit);
978
- } else {
979
- this.words[off] = this.words[off] & ~(1 << wbit);
980
- }
981
-
982
- return this._strip();
983
- };
984
-
985
- // Add `num` to `this` in-place
986
- BN.prototype.iadd = function iadd (num) {
987
- var r;
988
-
989
- // negative + positive
990
- if (this.negative !== 0 && num.negative === 0) {
991
- this.negative = 0;
992
- r = this.isub(num);
993
- this.negative ^= 1;
994
- return this._normSign();
995
-
996
- // positive + negative
997
- } else if (this.negative === 0 && num.negative !== 0) {
998
- num.negative = 0;
999
- r = this.isub(num);
1000
- num.negative = 1;
1001
- return r._normSign();
1002
- }
1003
-
1004
- // a.length > b.length
1005
- var a, b;
1006
- if (this.length > num.length) {
1007
- a = this;
1008
- b = num;
1009
- } else {
1010
- a = num;
1011
- b = this;
1012
- }
1013
-
1014
- var carry = 0;
1015
- for (var i = 0; i < b.length; i++) {
1016
- r = (a.words[i] | 0) + (b.words[i] | 0) + carry;
1017
- this.words[i] = r & 0x3ffffff;
1018
- carry = r >>> 26;
1019
- }
1020
- for (; carry !== 0 && i < a.length; i++) {
1021
- r = (a.words[i] | 0) + carry;
1022
- this.words[i] = r & 0x3ffffff;
1023
- carry = r >>> 26;
1024
- }
1025
-
1026
- this.length = a.length;
1027
- if (carry !== 0) {
1028
- this.words[this.length] = carry;
1029
- this.length++;
1030
- // Copy the rest of the words
1031
- } else if (a !== this) {
1032
- for (; i < a.length; i++) {
1033
- this.words[i] = a.words[i];
1034
- }
1035
- }
1036
-
1037
- return this;
1038
- };
1039
-
1040
- // Add `num` to `this`
1041
- BN.prototype.add = function add (num) {
1042
- var res;
1043
- if (num.negative !== 0 && this.negative === 0) {
1044
- num.negative = 0;
1045
- res = this.sub(num);
1046
- num.negative ^= 1;
1047
- return res;
1048
- } else if (num.negative === 0 && this.negative !== 0) {
1049
- this.negative = 0;
1050
- res = num.sub(this);
1051
- this.negative = 1;
1052
- return res;
1053
- }
1054
-
1055
- if (this.length > num.length) return this.clone().iadd(num);
1056
-
1057
- return num.clone().iadd(this);
1058
- };
1059
-
1060
- // Subtract `num` from `this` in-place
1061
- BN.prototype.isub = function isub (num) {
1062
- // this - (-num) = this + num
1063
- if (num.negative !== 0) {
1064
- num.negative = 0;
1065
- var r = this.iadd(num);
1066
- num.negative = 1;
1067
- return r._normSign();
1068
-
1069
- // -this - num = -(this + num)
1070
- } else if (this.negative !== 0) {
1071
- this.negative = 0;
1072
- this.iadd(num);
1073
- this.negative = 1;
1074
- return this._normSign();
1075
- }
1076
-
1077
- // At this point both numbers are positive
1078
- var cmp = this.cmp(num);
1079
-
1080
- // Optimization - zeroify
1081
- if (cmp === 0) {
1082
- this.negative = 0;
1083
- this.length = 1;
1084
- this.words[0] = 0;
1085
- return this;
1086
- }
1087
-
1088
- // a > b
1089
- var a, b;
1090
- if (cmp > 0) {
1091
- a = this;
1092
- b = num;
1093
- } else {
1094
- a = num;
1095
- b = this;
1096
- }
1097
-
1098
- var carry = 0;
1099
- for (var i = 0; i < b.length; i++) {
1100
- r = (a.words[i] | 0) - (b.words[i] | 0) + carry;
1101
- carry = r >> 26;
1102
- this.words[i] = r & 0x3ffffff;
1103
- }
1104
- for (; carry !== 0 && i < a.length; i++) {
1105
- r = (a.words[i] | 0) + carry;
1106
- carry = r >> 26;
1107
- this.words[i] = r & 0x3ffffff;
1108
- }
1109
-
1110
- // Copy rest of the words
1111
- if (carry === 0 && i < a.length && a !== this) {
1112
- for (; i < a.length; i++) {
1113
- this.words[i] = a.words[i];
1114
- }
1115
- }
1116
-
1117
- this.length = Math.max(this.length, i);
1118
-
1119
- if (a !== this) {
1120
- this.negative = 1;
1121
- }
1122
-
1123
- return this._strip();
1124
- };
1125
-
1126
- // Subtract `num` from `this`
1127
- BN.prototype.sub = function sub (num) {
1128
- return this.clone().isub(num);
1129
- };
1130
-
1131
- function smallMulTo (self, num, out) {
1132
- out.negative = num.negative ^ self.negative;
1133
- var len = (self.length + num.length) | 0;
1134
- out.length = len;
1135
- len = (len - 1) | 0;
1136
-
1137
- // Peel one iteration (compiler can't do it, because of code complexity)
1138
- var a = self.words[0] | 0;
1139
- var b = num.words[0] | 0;
1140
- var r = a * b;
1141
-
1142
- var lo = r & 0x3ffffff;
1143
- var carry = (r / 0x4000000) | 0;
1144
- out.words[0] = lo;
1145
-
1146
- for (var k = 1; k < len; k++) {
1147
- // Sum all words with the same `i + j = k` and accumulate `ncarry`,
1148
- // note that ncarry could be >= 0x3ffffff
1149
- var ncarry = carry >>> 26;
1150
- var rword = carry & 0x3ffffff;
1151
- var maxJ = Math.min(k, num.length - 1);
1152
- for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) {
1153
- var i = (k - j) | 0;
1154
- a = self.words[i] | 0;
1155
- b = num.words[j] | 0;
1156
- r = a * b + rword;
1157
- ncarry += (r / 0x4000000) | 0;
1158
- rword = r & 0x3ffffff;
1159
- }
1160
- out.words[k] = rword | 0;
1161
- carry = ncarry | 0;
1162
- }
1163
- if (carry !== 0) {
1164
- out.words[k] = carry | 0;
1165
- } else {
1166
- out.length--;
1167
- }
1168
-
1169
- return out._strip();
1170
- }
1171
-
1172
- // TODO(indutny): it may be reasonable to omit it for users who don't need
1173
- // to work with 256-bit numbers, otherwise it gives 20% improvement for 256-bit
1174
- // multiplication (like elliptic secp256k1).
1175
- var comb10MulTo = function comb10MulTo (self, num, out) {
1176
- var a = self.words;
1177
- var b = num.words;
1178
- var o = out.words;
1179
- var c = 0;
1180
- var lo;
1181
- var mid;
1182
- var hi;
1183
- var a0 = a[0] | 0;
1184
- var al0 = a0 & 0x1fff;
1185
- var ah0 = a0 >>> 13;
1186
- var a1 = a[1] | 0;
1187
- var al1 = a1 & 0x1fff;
1188
- var ah1 = a1 >>> 13;
1189
- var a2 = a[2] | 0;
1190
- var al2 = a2 & 0x1fff;
1191
- var ah2 = a2 >>> 13;
1192
- var a3 = a[3] | 0;
1193
- var al3 = a3 & 0x1fff;
1194
- var ah3 = a3 >>> 13;
1195
- var a4 = a[4] | 0;
1196
- var al4 = a4 & 0x1fff;
1197
- var ah4 = a4 >>> 13;
1198
- var a5 = a[5] | 0;
1199
- var al5 = a5 & 0x1fff;
1200
- var ah5 = a5 >>> 13;
1201
- var a6 = a[6] | 0;
1202
- var al6 = a6 & 0x1fff;
1203
- var ah6 = a6 >>> 13;
1204
- var a7 = a[7] | 0;
1205
- var al7 = a7 & 0x1fff;
1206
- var ah7 = a7 >>> 13;
1207
- var a8 = a[8] | 0;
1208
- var al8 = a8 & 0x1fff;
1209
- var ah8 = a8 >>> 13;
1210
- var a9 = a[9] | 0;
1211
- var al9 = a9 & 0x1fff;
1212
- var ah9 = a9 >>> 13;
1213
- var b0 = b[0] | 0;
1214
- var bl0 = b0 & 0x1fff;
1215
- var bh0 = b0 >>> 13;
1216
- var b1 = b[1] | 0;
1217
- var bl1 = b1 & 0x1fff;
1218
- var bh1 = b1 >>> 13;
1219
- var b2 = b[2] | 0;
1220
- var bl2 = b2 & 0x1fff;
1221
- var bh2 = b2 >>> 13;
1222
- var b3 = b[3] | 0;
1223
- var bl3 = b3 & 0x1fff;
1224
- var bh3 = b3 >>> 13;
1225
- var b4 = b[4] | 0;
1226
- var bl4 = b4 & 0x1fff;
1227
- var bh4 = b4 >>> 13;
1228
- var b5 = b[5] | 0;
1229
- var bl5 = b5 & 0x1fff;
1230
- var bh5 = b5 >>> 13;
1231
- var b6 = b[6] | 0;
1232
- var bl6 = b6 & 0x1fff;
1233
- var bh6 = b6 >>> 13;
1234
- var b7 = b[7] | 0;
1235
- var bl7 = b7 & 0x1fff;
1236
- var bh7 = b7 >>> 13;
1237
- var b8 = b[8] | 0;
1238
- var bl8 = b8 & 0x1fff;
1239
- var bh8 = b8 >>> 13;
1240
- var b9 = b[9] | 0;
1241
- var bl9 = b9 & 0x1fff;
1242
- var bh9 = b9 >>> 13;
1243
-
1244
- out.negative = self.negative ^ num.negative;
1245
- out.length = 19;
1246
- /* k = 0 */
1247
- lo = Math.imul(al0, bl0);
1248
- mid = Math.imul(al0, bh0);
1249
- mid = (mid + Math.imul(ah0, bl0)) | 0;
1250
- hi = Math.imul(ah0, bh0);
1251
- var w0 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1252
- c = (((hi + (mid >>> 13)) | 0) + (w0 >>> 26)) | 0;
1253
- w0 &= 0x3ffffff;
1254
- /* k = 1 */
1255
- lo = Math.imul(al1, bl0);
1256
- mid = Math.imul(al1, bh0);
1257
- mid = (mid + Math.imul(ah1, bl0)) | 0;
1258
- hi = Math.imul(ah1, bh0);
1259
- lo = (lo + Math.imul(al0, bl1)) | 0;
1260
- mid = (mid + Math.imul(al0, bh1)) | 0;
1261
- mid = (mid + Math.imul(ah0, bl1)) | 0;
1262
- hi = (hi + Math.imul(ah0, bh1)) | 0;
1263
- var w1 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1264
- c = (((hi + (mid >>> 13)) | 0) + (w1 >>> 26)) | 0;
1265
- w1 &= 0x3ffffff;
1266
- /* k = 2 */
1267
- lo = Math.imul(al2, bl0);
1268
- mid = Math.imul(al2, bh0);
1269
- mid = (mid + Math.imul(ah2, bl0)) | 0;
1270
- hi = Math.imul(ah2, bh0);
1271
- lo = (lo + Math.imul(al1, bl1)) | 0;
1272
- mid = (mid + Math.imul(al1, bh1)) | 0;
1273
- mid = (mid + Math.imul(ah1, bl1)) | 0;
1274
- hi = (hi + Math.imul(ah1, bh1)) | 0;
1275
- lo = (lo + Math.imul(al0, bl2)) | 0;
1276
- mid = (mid + Math.imul(al0, bh2)) | 0;
1277
- mid = (mid + Math.imul(ah0, bl2)) | 0;
1278
- hi = (hi + Math.imul(ah0, bh2)) | 0;
1279
- var w2 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1280
- c = (((hi + (mid >>> 13)) | 0) + (w2 >>> 26)) | 0;
1281
- w2 &= 0x3ffffff;
1282
- /* k = 3 */
1283
- lo = Math.imul(al3, bl0);
1284
- mid = Math.imul(al3, bh0);
1285
- mid = (mid + Math.imul(ah3, bl0)) | 0;
1286
- hi = Math.imul(ah3, bh0);
1287
- lo = (lo + Math.imul(al2, bl1)) | 0;
1288
- mid = (mid + Math.imul(al2, bh1)) | 0;
1289
- mid = (mid + Math.imul(ah2, bl1)) | 0;
1290
- hi = (hi + Math.imul(ah2, bh1)) | 0;
1291
- lo = (lo + Math.imul(al1, bl2)) | 0;
1292
- mid = (mid + Math.imul(al1, bh2)) | 0;
1293
- mid = (mid + Math.imul(ah1, bl2)) | 0;
1294
- hi = (hi + Math.imul(ah1, bh2)) | 0;
1295
- lo = (lo + Math.imul(al0, bl3)) | 0;
1296
- mid = (mid + Math.imul(al0, bh3)) | 0;
1297
- mid = (mid + Math.imul(ah0, bl3)) | 0;
1298
- hi = (hi + Math.imul(ah0, bh3)) | 0;
1299
- var w3 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1300
- c = (((hi + (mid >>> 13)) | 0) + (w3 >>> 26)) | 0;
1301
- w3 &= 0x3ffffff;
1302
- /* k = 4 */
1303
- lo = Math.imul(al4, bl0);
1304
- mid = Math.imul(al4, bh0);
1305
- mid = (mid + Math.imul(ah4, bl0)) | 0;
1306
- hi = Math.imul(ah4, bh0);
1307
- lo = (lo + Math.imul(al3, bl1)) | 0;
1308
- mid = (mid + Math.imul(al3, bh1)) | 0;
1309
- mid = (mid + Math.imul(ah3, bl1)) | 0;
1310
- hi = (hi + Math.imul(ah3, bh1)) | 0;
1311
- lo = (lo + Math.imul(al2, bl2)) | 0;
1312
- mid = (mid + Math.imul(al2, bh2)) | 0;
1313
- mid = (mid + Math.imul(ah2, bl2)) | 0;
1314
- hi = (hi + Math.imul(ah2, bh2)) | 0;
1315
- lo = (lo + Math.imul(al1, bl3)) | 0;
1316
- mid = (mid + Math.imul(al1, bh3)) | 0;
1317
- mid = (mid + Math.imul(ah1, bl3)) | 0;
1318
- hi = (hi + Math.imul(ah1, bh3)) | 0;
1319
- lo = (lo + Math.imul(al0, bl4)) | 0;
1320
- mid = (mid + Math.imul(al0, bh4)) | 0;
1321
- mid = (mid + Math.imul(ah0, bl4)) | 0;
1322
- hi = (hi + Math.imul(ah0, bh4)) | 0;
1323
- var w4 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1324
- c = (((hi + (mid >>> 13)) | 0) + (w4 >>> 26)) | 0;
1325
- w4 &= 0x3ffffff;
1326
- /* k = 5 */
1327
- lo = Math.imul(al5, bl0);
1328
- mid = Math.imul(al5, bh0);
1329
- mid = (mid + Math.imul(ah5, bl0)) | 0;
1330
- hi = Math.imul(ah5, bh0);
1331
- lo = (lo + Math.imul(al4, bl1)) | 0;
1332
- mid = (mid + Math.imul(al4, bh1)) | 0;
1333
- mid = (mid + Math.imul(ah4, bl1)) | 0;
1334
- hi = (hi + Math.imul(ah4, bh1)) | 0;
1335
- lo = (lo + Math.imul(al3, bl2)) | 0;
1336
- mid = (mid + Math.imul(al3, bh2)) | 0;
1337
- mid = (mid + Math.imul(ah3, bl2)) | 0;
1338
- hi = (hi + Math.imul(ah3, bh2)) | 0;
1339
- lo = (lo + Math.imul(al2, bl3)) | 0;
1340
- mid = (mid + Math.imul(al2, bh3)) | 0;
1341
- mid = (mid + Math.imul(ah2, bl3)) | 0;
1342
- hi = (hi + Math.imul(ah2, bh3)) | 0;
1343
- lo = (lo + Math.imul(al1, bl4)) | 0;
1344
- mid = (mid + Math.imul(al1, bh4)) | 0;
1345
- mid = (mid + Math.imul(ah1, bl4)) | 0;
1346
- hi = (hi + Math.imul(ah1, bh4)) | 0;
1347
- lo = (lo + Math.imul(al0, bl5)) | 0;
1348
- mid = (mid + Math.imul(al0, bh5)) | 0;
1349
- mid = (mid + Math.imul(ah0, bl5)) | 0;
1350
- hi = (hi + Math.imul(ah0, bh5)) | 0;
1351
- var w5 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1352
- c = (((hi + (mid >>> 13)) | 0) + (w5 >>> 26)) | 0;
1353
- w5 &= 0x3ffffff;
1354
- /* k = 6 */
1355
- lo = Math.imul(al6, bl0);
1356
- mid = Math.imul(al6, bh0);
1357
- mid = (mid + Math.imul(ah6, bl0)) | 0;
1358
- hi = Math.imul(ah6, bh0);
1359
- lo = (lo + Math.imul(al5, bl1)) | 0;
1360
- mid = (mid + Math.imul(al5, bh1)) | 0;
1361
- mid = (mid + Math.imul(ah5, bl1)) | 0;
1362
- hi = (hi + Math.imul(ah5, bh1)) | 0;
1363
- lo = (lo + Math.imul(al4, bl2)) | 0;
1364
- mid = (mid + Math.imul(al4, bh2)) | 0;
1365
- mid = (mid + Math.imul(ah4, bl2)) | 0;
1366
- hi = (hi + Math.imul(ah4, bh2)) | 0;
1367
- lo = (lo + Math.imul(al3, bl3)) | 0;
1368
- mid = (mid + Math.imul(al3, bh3)) | 0;
1369
- mid = (mid + Math.imul(ah3, bl3)) | 0;
1370
- hi = (hi + Math.imul(ah3, bh3)) | 0;
1371
- lo = (lo + Math.imul(al2, bl4)) | 0;
1372
- mid = (mid + Math.imul(al2, bh4)) | 0;
1373
- mid = (mid + Math.imul(ah2, bl4)) | 0;
1374
- hi = (hi + Math.imul(ah2, bh4)) | 0;
1375
- lo = (lo + Math.imul(al1, bl5)) | 0;
1376
- mid = (mid + Math.imul(al1, bh5)) | 0;
1377
- mid = (mid + Math.imul(ah1, bl5)) | 0;
1378
- hi = (hi + Math.imul(ah1, bh5)) | 0;
1379
- lo = (lo + Math.imul(al0, bl6)) | 0;
1380
- mid = (mid + Math.imul(al0, bh6)) | 0;
1381
- mid = (mid + Math.imul(ah0, bl6)) | 0;
1382
- hi = (hi + Math.imul(ah0, bh6)) | 0;
1383
- var w6 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1384
- c = (((hi + (mid >>> 13)) | 0) + (w6 >>> 26)) | 0;
1385
- w6 &= 0x3ffffff;
1386
- /* k = 7 */
1387
- lo = Math.imul(al7, bl0);
1388
- mid = Math.imul(al7, bh0);
1389
- mid = (mid + Math.imul(ah7, bl0)) | 0;
1390
- hi = Math.imul(ah7, bh0);
1391
- lo = (lo + Math.imul(al6, bl1)) | 0;
1392
- mid = (mid + Math.imul(al6, bh1)) | 0;
1393
- mid = (mid + Math.imul(ah6, bl1)) | 0;
1394
- hi = (hi + Math.imul(ah6, bh1)) | 0;
1395
- lo = (lo + Math.imul(al5, bl2)) | 0;
1396
- mid = (mid + Math.imul(al5, bh2)) | 0;
1397
- mid = (mid + Math.imul(ah5, bl2)) | 0;
1398
- hi = (hi + Math.imul(ah5, bh2)) | 0;
1399
- lo = (lo + Math.imul(al4, bl3)) | 0;
1400
- mid = (mid + Math.imul(al4, bh3)) | 0;
1401
- mid = (mid + Math.imul(ah4, bl3)) | 0;
1402
- hi = (hi + Math.imul(ah4, bh3)) | 0;
1403
- lo = (lo + Math.imul(al3, bl4)) | 0;
1404
- mid = (mid + Math.imul(al3, bh4)) | 0;
1405
- mid = (mid + Math.imul(ah3, bl4)) | 0;
1406
- hi = (hi + Math.imul(ah3, bh4)) | 0;
1407
- lo = (lo + Math.imul(al2, bl5)) | 0;
1408
- mid = (mid + Math.imul(al2, bh5)) | 0;
1409
- mid = (mid + Math.imul(ah2, bl5)) | 0;
1410
- hi = (hi + Math.imul(ah2, bh5)) | 0;
1411
- lo = (lo + Math.imul(al1, bl6)) | 0;
1412
- mid = (mid + Math.imul(al1, bh6)) | 0;
1413
- mid = (mid + Math.imul(ah1, bl6)) | 0;
1414
- hi = (hi + Math.imul(ah1, bh6)) | 0;
1415
- lo = (lo + Math.imul(al0, bl7)) | 0;
1416
- mid = (mid + Math.imul(al0, bh7)) | 0;
1417
- mid = (mid + Math.imul(ah0, bl7)) | 0;
1418
- hi = (hi + Math.imul(ah0, bh7)) | 0;
1419
- var w7 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1420
- c = (((hi + (mid >>> 13)) | 0) + (w7 >>> 26)) | 0;
1421
- w7 &= 0x3ffffff;
1422
- /* k = 8 */
1423
- lo = Math.imul(al8, bl0);
1424
- mid = Math.imul(al8, bh0);
1425
- mid = (mid + Math.imul(ah8, bl0)) | 0;
1426
- hi = Math.imul(ah8, bh0);
1427
- lo = (lo + Math.imul(al7, bl1)) | 0;
1428
- mid = (mid + Math.imul(al7, bh1)) | 0;
1429
- mid = (mid + Math.imul(ah7, bl1)) | 0;
1430
- hi = (hi + Math.imul(ah7, bh1)) | 0;
1431
- lo = (lo + Math.imul(al6, bl2)) | 0;
1432
- mid = (mid + Math.imul(al6, bh2)) | 0;
1433
- mid = (mid + Math.imul(ah6, bl2)) | 0;
1434
- hi = (hi + Math.imul(ah6, bh2)) | 0;
1435
- lo = (lo + Math.imul(al5, bl3)) | 0;
1436
- mid = (mid + Math.imul(al5, bh3)) | 0;
1437
- mid = (mid + Math.imul(ah5, bl3)) | 0;
1438
- hi = (hi + Math.imul(ah5, bh3)) | 0;
1439
- lo = (lo + Math.imul(al4, bl4)) | 0;
1440
- mid = (mid + Math.imul(al4, bh4)) | 0;
1441
- mid = (mid + Math.imul(ah4, bl4)) | 0;
1442
- hi = (hi + Math.imul(ah4, bh4)) | 0;
1443
- lo = (lo + Math.imul(al3, bl5)) | 0;
1444
- mid = (mid + Math.imul(al3, bh5)) | 0;
1445
- mid = (mid + Math.imul(ah3, bl5)) | 0;
1446
- hi = (hi + Math.imul(ah3, bh5)) | 0;
1447
- lo = (lo + Math.imul(al2, bl6)) | 0;
1448
- mid = (mid + Math.imul(al2, bh6)) | 0;
1449
- mid = (mid + Math.imul(ah2, bl6)) | 0;
1450
- hi = (hi + Math.imul(ah2, bh6)) | 0;
1451
- lo = (lo + Math.imul(al1, bl7)) | 0;
1452
- mid = (mid + Math.imul(al1, bh7)) | 0;
1453
- mid = (mid + Math.imul(ah1, bl7)) | 0;
1454
- hi = (hi + Math.imul(ah1, bh7)) | 0;
1455
- lo = (lo + Math.imul(al0, bl8)) | 0;
1456
- mid = (mid + Math.imul(al0, bh8)) | 0;
1457
- mid = (mid + Math.imul(ah0, bl8)) | 0;
1458
- hi = (hi + Math.imul(ah0, bh8)) | 0;
1459
- var w8 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1460
- c = (((hi + (mid >>> 13)) | 0) + (w8 >>> 26)) | 0;
1461
- w8 &= 0x3ffffff;
1462
- /* k = 9 */
1463
- lo = Math.imul(al9, bl0);
1464
- mid = Math.imul(al9, bh0);
1465
- mid = (mid + Math.imul(ah9, bl0)) | 0;
1466
- hi = Math.imul(ah9, bh0);
1467
- lo = (lo + Math.imul(al8, bl1)) | 0;
1468
- mid = (mid + Math.imul(al8, bh1)) | 0;
1469
- mid = (mid + Math.imul(ah8, bl1)) | 0;
1470
- hi = (hi + Math.imul(ah8, bh1)) | 0;
1471
- lo = (lo + Math.imul(al7, bl2)) | 0;
1472
- mid = (mid + Math.imul(al7, bh2)) | 0;
1473
- mid = (mid + Math.imul(ah7, bl2)) | 0;
1474
- hi = (hi + Math.imul(ah7, bh2)) | 0;
1475
- lo = (lo + Math.imul(al6, bl3)) | 0;
1476
- mid = (mid + Math.imul(al6, bh3)) | 0;
1477
- mid = (mid + Math.imul(ah6, bl3)) | 0;
1478
- hi = (hi + Math.imul(ah6, bh3)) | 0;
1479
- lo = (lo + Math.imul(al5, bl4)) | 0;
1480
- mid = (mid + Math.imul(al5, bh4)) | 0;
1481
- mid = (mid + Math.imul(ah5, bl4)) | 0;
1482
- hi = (hi + Math.imul(ah5, bh4)) | 0;
1483
- lo = (lo + Math.imul(al4, bl5)) | 0;
1484
- mid = (mid + Math.imul(al4, bh5)) | 0;
1485
- mid = (mid + Math.imul(ah4, bl5)) | 0;
1486
- hi = (hi + Math.imul(ah4, bh5)) | 0;
1487
- lo = (lo + Math.imul(al3, bl6)) | 0;
1488
- mid = (mid + Math.imul(al3, bh6)) | 0;
1489
- mid = (mid + Math.imul(ah3, bl6)) | 0;
1490
- hi = (hi + Math.imul(ah3, bh6)) | 0;
1491
- lo = (lo + Math.imul(al2, bl7)) | 0;
1492
- mid = (mid + Math.imul(al2, bh7)) | 0;
1493
- mid = (mid + Math.imul(ah2, bl7)) | 0;
1494
- hi = (hi + Math.imul(ah2, bh7)) | 0;
1495
- lo = (lo + Math.imul(al1, bl8)) | 0;
1496
- mid = (mid + Math.imul(al1, bh8)) | 0;
1497
- mid = (mid + Math.imul(ah1, bl8)) | 0;
1498
- hi = (hi + Math.imul(ah1, bh8)) | 0;
1499
- lo = (lo + Math.imul(al0, bl9)) | 0;
1500
- mid = (mid + Math.imul(al0, bh9)) | 0;
1501
- mid = (mid + Math.imul(ah0, bl9)) | 0;
1502
- hi = (hi + Math.imul(ah0, bh9)) | 0;
1503
- var w9 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1504
- c = (((hi + (mid >>> 13)) | 0) + (w9 >>> 26)) | 0;
1505
- w9 &= 0x3ffffff;
1506
- /* k = 10 */
1507
- lo = Math.imul(al9, bl1);
1508
- mid = Math.imul(al9, bh1);
1509
- mid = (mid + Math.imul(ah9, bl1)) | 0;
1510
- hi = Math.imul(ah9, bh1);
1511
- lo = (lo + Math.imul(al8, bl2)) | 0;
1512
- mid = (mid + Math.imul(al8, bh2)) | 0;
1513
- mid = (mid + Math.imul(ah8, bl2)) | 0;
1514
- hi = (hi + Math.imul(ah8, bh2)) | 0;
1515
- lo = (lo + Math.imul(al7, bl3)) | 0;
1516
- mid = (mid + Math.imul(al7, bh3)) | 0;
1517
- mid = (mid + Math.imul(ah7, bl3)) | 0;
1518
- hi = (hi + Math.imul(ah7, bh3)) | 0;
1519
- lo = (lo + Math.imul(al6, bl4)) | 0;
1520
- mid = (mid + Math.imul(al6, bh4)) | 0;
1521
- mid = (mid + Math.imul(ah6, bl4)) | 0;
1522
- hi = (hi + Math.imul(ah6, bh4)) | 0;
1523
- lo = (lo + Math.imul(al5, bl5)) | 0;
1524
- mid = (mid + Math.imul(al5, bh5)) | 0;
1525
- mid = (mid + Math.imul(ah5, bl5)) | 0;
1526
- hi = (hi + Math.imul(ah5, bh5)) | 0;
1527
- lo = (lo + Math.imul(al4, bl6)) | 0;
1528
- mid = (mid + Math.imul(al4, bh6)) | 0;
1529
- mid = (mid + Math.imul(ah4, bl6)) | 0;
1530
- hi = (hi + Math.imul(ah4, bh6)) | 0;
1531
- lo = (lo + Math.imul(al3, bl7)) | 0;
1532
- mid = (mid + Math.imul(al3, bh7)) | 0;
1533
- mid = (mid + Math.imul(ah3, bl7)) | 0;
1534
- hi = (hi + Math.imul(ah3, bh7)) | 0;
1535
- lo = (lo + Math.imul(al2, bl8)) | 0;
1536
- mid = (mid + Math.imul(al2, bh8)) | 0;
1537
- mid = (mid + Math.imul(ah2, bl8)) | 0;
1538
- hi = (hi + Math.imul(ah2, bh8)) | 0;
1539
- lo = (lo + Math.imul(al1, bl9)) | 0;
1540
- mid = (mid + Math.imul(al1, bh9)) | 0;
1541
- mid = (mid + Math.imul(ah1, bl9)) | 0;
1542
- hi = (hi + Math.imul(ah1, bh9)) | 0;
1543
- var w10 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1544
- c = (((hi + (mid >>> 13)) | 0) + (w10 >>> 26)) | 0;
1545
- w10 &= 0x3ffffff;
1546
- /* k = 11 */
1547
- lo = Math.imul(al9, bl2);
1548
- mid = Math.imul(al9, bh2);
1549
- mid = (mid + Math.imul(ah9, bl2)) | 0;
1550
- hi = Math.imul(ah9, bh2);
1551
- lo = (lo + Math.imul(al8, bl3)) | 0;
1552
- mid = (mid + Math.imul(al8, bh3)) | 0;
1553
- mid = (mid + Math.imul(ah8, bl3)) | 0;
1554
- hi = (hi + Math.imul(ah8, bh3)) | 0;
1555
- lo = (lo + Math.imul(al7, bl4)) | 0;
1556
- mid = (mid + Math.imul(al7, bh4)) | 0;
1557
- mid = (mid + Math.imul(ah7, bl4)) | 0;
1558
- hi = (hi + Math.imul(ah7, bh4)) | 0;
1559
- lo = (lo + Math.imul(al6, bl5)) | 0;
1560
- mid = (mid + Math.imul(al6, bh5)) | 0;
1561
- mid = (mid + Math.imul(ah6, bl5)) | 0;
1562
- hi = (hi + Math.imul(ah6, bh5)) | 0;
1563
- lo = (lo + Math.imul(al5, bl6)) | 0;
1564
- mid = (mid + Math.imul(al5, bh6)) | 0;
1565
- mid = (mid + Math.imul(ah5, bl6)) | 0;
1566
- hi = (hi + Math.imul(ah5, bh6)) | 0;
1567
- lo = (lo + Math.imul(al4, bl7)) | 0;
1568
- mid = (mid + Math.imul(al4, bh7)) | 0;
1569
- mid = (mid + Math.imul(ah4, bl7)) | 0;
1570
- hi = (hi + Math.imul(ah4, bh7)) | 0;
1571
- lo = (lo + Math.imul(al3, bl8)) | 0;
1572
- mid = (mid + Math.imul(al3, bh8)) | 0;
1573
- mid = (mid + Math.imul(ah3, bl8)) | 0;
1574
- hi = (hi + Math.imul(ah3, bh8)) | 0;
1575
- lo = (lo + Math.imul(al2, bl9)) | 0;
1576
- mid = (mid + Math.imul(al2, bh9)) | 0;
1577
- mid = (mid + Math.imul(ah2, bl9)) | 0;
1578
- hi = (hi + Math.imul(ah2, bh9)) | 0;
1579
- var w11 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1580
- c = (((hi + (mid >>> 13)) | 0) + (w11 >>> 26)) | 0;
1581
- w11 &= 0x3ffffff;
1582
- /* k = 12 */
1583
- lo = Math.imul(al9, bl3);
1584
- mid = Math.imul(al9, bh3);
1585
- mid = (mid + Math.imul(ah9, bl3)) | 0;
1586
- hi = Math.imul(ah9, bh3);
1587
- lo = (lo + Math.imul(al8, bl4)) | 0;
1588
- mid = (mid + Math.imul(al8, bh4)) | 0;
1589
- mid = (mid + Math.imul(ah8, bl4)) | 0;
1590
- hi = (hi + Math.imul(ah8, bh4)) | 0;
1591
- lo = (lo + Math.imul(al7, bl5)) | 0;
1592
- mid = (mid + Math.imul(al7, bh5)) | 0;
1593
- mid = (mid + Math.imul(ah7, bl5)) | 0;
1594
- hi = (hi + Math.imul(ah7, bh5)) | 0;
1595
- lo = (lo + Math.imul(al6, bl6)) | 0;
1596
- mid = (mid + Math.imul(al6, bh6)) | 0;
1597
- mid = (mid + Math.imul(ah6, bl6)) | 0;
1598
- hi = (hi + Math.imul(ah6, bh6)) | 0;
1599
- lo = (lo + Math.imul(al5, bl7)) | 0;
1600
- mid = (mid + Math.imul(al5, bh7)) | 0;
1601
- mid = (mid + Math.imul(ah5, bl7)) | 0;
1602
- hi = (hi + Math.imul(ah5, bh7)) | 0;
1603
- lo = (lo + Math.imul(al4, bl8)) | 0;
1604
- mid = (mid + Math.imul(al4, bh8)) | 0;
1605
- mid = (mid + Math.imul(ah4, bl8)) | 0;
1606
- hi = (hi + Math.imul(ah4, bh8)) | 0;
1607
- lo = (lo + Math.imul(al3, bl9)) | 0;
1608
- mid = (mid + Math.imul(al3, bh9)) | 0;
1609
- mid = (mid + Math.imul(ah3, bl9)) | 0;
1610
- hi = (hi + Math.imul(ah3, bh9)) | 0;
1611
- var w12 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1612
- c = (((hi + (mid >>> 13)) | 0) + (w12 >>> 26)) | 0;
1613
- w12 &= 0x3ffffff;
1614
- /* k = 13 */
1615
- lo = Math.imul(al9, bl4);
1616
- mid = Math.imul(al9, bh4);
1617
- mid = (mid + Math.imul(ah9, bl4)) | 0;
1618
- hi = Math.imul(ah9, bh4);
1619
- lo = (lo + Math.imul(al8, bl5)) | 0;
1620
- mid = (mid + Math.imul(al8, bh5)) | 0;
1621
- mid = (mid + Math.imul(ah8, bl5)) | 0;
1622
- hi = (hi + Math.imul(ah8, bh5)) | 0;
1623
- lo = (lo + Math.imul(al7, bl6)) | 0;
1624
- mid = (mid + Math.imul(al7, bh6)) | 0;
1625
- mid = (mid + Math.imul(ah7, bl6)) | 0;
1626
- hi = (hi + Math.imul(ah7, bh6)) | 0;
1627
- lo = (lo + Math.imul(al6, bl7)) | 0;
1628
- mid = (mid + Math.imul(al6, bh7)) | 0;
1629
- mid = (mid + Math.imul(ah6, bl7)) | 0;
1630
- hi = (hi + Math.imul(ah6, bh7)) | 0;
1631
- lo = (lo + Math.imul(al5, bl8)) | 0;
1632
- mid = (mid + Math.imul(al5, bh8)) | 0;
1633
- mid = (mid + Math.imul(ah5, bl8)) | 0;
1634
- hi = (hi + Math.imul(ah5, bh8)) | 0;
1635
- lo = (lo + Math.imul(al4, bl9)) | 0;
1636
- mid = (mid + Math.imul(al4, bh9)) | 0;
1637
- mid = (mid + Math.imul(ah4, bl9)) | 0;
1638
- hi = (hi + Math.imul(ah4, bh9)) | 0;
1639
- var w13 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1640
- c = (((hi + (mid >>> 13)) | 0) + (w13 >>> 26)) | 0;
1641
- w13 &= 0x3ffffff;
1642
- /* k = 14 */
1643
- lo = Math.imul(al9, bl5);
1644
- mid = Math.imul(al9, bh5);
1645
- mid = (mid + Math.imul(ah9, bl5)) | 0;
1646
- hi = Math.imul(ah9, bh5);
1647
- lo = (lo + Math.imul(al8, bl6)) | 0;
1648
- mid = (mid + Math.imul(al8, bh6)) | 0;
1649
- mid = (mid + Math.imul(ah8, bl6)) | 0;
1650
- hi = (hi + Math.imul(ah8, bh6)) | 0;
1651
- lo = (lo + Math.imul(al7, bl7)) | 0;
1652
- mid = (mid + Math.imul(al7, bh7)) | 0;
1653
- mid = (mid + Math.imul(ah7, bl7)) | 0;
1654
- hi = (hi + Math.imul(ah7, bh7)) | 0;
1655
- lo = (lo + Math.imul(al6, bl8)) | 0;
1656
- mid = (mid + Math.imul(al6, bh8)) | 0;
1657
- mid = (mid + Math.imul(ah6, bl8)) | 0;
1658
- hi = (hi + Math.imul(ah6, bh8)) | 0;
1659
- lo = (lo + Math.imul(al5, bl9)) | 0;
1660
- mid = (mid + Math.imul(al5, bh9)) | 0;
1661
- mid = (mid + Math.imul(ah5, bl9)) | 0;
1662
- hi = (hi + Math.imul(ah5, bh9)) | 0;
1663
- var w14 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1664
- c = (((hi + (mid >>> 13)) | 0) + (w14 >>> 26)) | 0;
1665
- w14 &= 0x3ffffff;
1666
- /* k = 15 */
1667
- lo = Math.imul(al9, bl6);
1668
- mid = Math.imul(al9, bh6);
1669
- mid = (mid + Math.imul(ah9, bl6)) | 0;
1670
- hi = Math.imul(ah9, bh6);
1671
- lo = (lo + Math.imul(al8, bl7)) | 0;
1672
- mid = (mid + Math.imul(al8, bh7)) | 0;
1673
- mid = (mid + Math.imul(ah8, bl7)) | 0;
1674
- hi = (hi + Math.imul(ah8, bh7)) | 0;
1675
- lo = (lo + Math.imul(al7, bl8)) | 0;
1676
- mid = (mid + Math.imul(al7, bh8)) | 0;
1677
- mid = (mid + Math.imul(ah7, bl8)) | 0;
1678
- hi = (hi + Math.imul(ah7, bh8)) | 0;
1679
- lo = (lo + Math.imul(al6, bl9)) | 0;
1680
- mid = (mid + Math.imul(al6, bh9)) | 0;
1681
- mid = (mid + Math.imul(ah6, bl9)) | 0;
1682
- hi = (hi + Math.imul(ah6, bh9)) | 0;
1683
- var w15 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1684
- c = (((hi + (mid >>> 13)) | 0) + (w15 >>> 26)) | 0;
1685
- w15 &= 0x3ffffff;
1686
- /* k = 16 */
1687
- lo = Math.imul(al9, bl7);
1688
- mid = Math.imul(al9, bh7);
1689
- mid = (mid + Math.imul(ah9, bl7)) | 0;
1690
- hi = Math.imul(ah9, bh7);
1691
- lo = (lo + Math.imul(al8, bl8)) | 0;
1692
- mid = (mid + Math.imul(al8, bh8)) | 0;
1693
- mid = (mid + Math.imul(ah8, bl8)) | 0;
1694
- hi = (hi + Math.imul(ah8, bh8)) | 0;
1695
- lo = (lo + Math.imul(al7, bl9)) | 0;
1696
- mid = (mid + Math.imul(al7, bh9)) | 0;
1697
- mid = (mid + Math.imul(ah7, bl9)) | 0;
1698
- hi = (hi + Math.imul(ah7, bh9)) | 0;
1699
- var w16 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1700
- c = (((hi + (mid >>> 13)) | 0) + (w16 >>> 26)) | 0;
1701
- w16 &= 0x3ffffff;
1702
- /* k = 17 */
1703
- lo = Math.imul(al9, bl8);
1704
- mid = Math.imul(al9, bh8);
1705
- mid = (mid + Math.imul(ah9, bl8)) | 0;
1706
- hi = Math.imul(ah9, bh8);
1707
- lo = (lo + Math.imul(al8, bl9)) | 0;
1708
- mid = (mid + Math.imul(al8, bh9)) | 0;
1709
- mid = (mid + Math.imul(ah8, bl9)) | 0;
1710
- hi = (hi + Math.imul(ah8, bh9)) | 0;
1711
- var w17 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1712
- c = (((hi + (mid >>> 13)) | 0) + (w17 >>> 26)) | 0;
1713
- w17 &= 0x3ffffff;
1714
- /* k = 18 */
1715
- lo = Math.imul(al9, bl9);
1716
- mid = Math.imul(al9, bh9);
1717
- mid = (mid + Math.imul(ah9, bl9)) | 0;
1718
- hi = Math.imul(ah9, bh9);
1719
- var w18 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1720
- c = (((hi + (mid >>> 13)) | 0) + (w18 >>> 26)) | 0;
1721
- w18 &= 0x3ffffff;
1722
- o[0] = w0;
1723
- o[1] = w1;
1724
- o[2] = w2;
1725
- o[3] = w3;
1726
- o[4] = w4;
1727
- o[5] = w5;
1728
- o[6] = w6;
1729
- o[7] = w7;
1730
- o[8] = w8;
1731
- o[9] = w9;
1732
- o[10] = w10;
1733
- o[11] = w11;
1734
- o[12] = w12;
1735
- o[13] = w13;
1736
- o[14] = w14;
1737
- o[15] = w15;
1738
- o[16] = w16;
1739
- o[17] = w17;
1740
- o[18] = w18;
1741
- if (c !== 0) {
1742
- o[19] = c;
1743
- out.length++;
1744
- }
1745
- return out;
1746
- };
1747
-
1748
- // Polyfill comb
1749
- if (!Math.imul) {
1750
- comb10MulTo = smallMulTo;
1751
- }
1752
-
1753
- function bigMulTo (self, num, out) {
1754
- out.negative = num.negative ^ self.negative;
1755
- out.length = self.length + num.length;
1756
-
1757
- var carry = 0;
1758
- var hncarry = 0;
1759
- for (var k = 0; k < out.length - 1; k++) {
1760
- // Sum all words with the same `i + j = k` and accumulate `ncarry`,
1761
- // note that ncarry could be >= 0x3ffffff
1762
- var ncarry = hncarry;
1763
- hncarry = 0;
1764
- var rword = carry & 0x3ffffff;
1765
- var maxJ = Math.min(k, num.length - 1);
1766
- for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) {
1767
- var i = k - j;
1768
- var a = self.words[i] | 0;
1769
- var b = num.words[j] | 0;
1770
- var r = a * b;
1771
-
1772
- var lo = r & 0x3ffffff;
1773
- ncarry = (ncarry + ((r / 0x4000000) | 0)) | 0;
1774
- lo = (lo + rword) | 0;
1775
- rword = lo & 0x3ffffff;
1776
- ncarry = (ncarry + (lo >>> 26)) | 0;
1777
-
1778
- hncarry += ncarry >>> 26;
1779
- ncarry &= 0x3ffffff;
1780
- }
1781
- out.words[k] = rword;
1782
- carry = ncarry;
1783
- ncarry = hncarry;
1784
- }
1785
- if (carry !== 0) {
1786
- out.words[k] = carry;
1787
- } else {
1788
- out.length--;
1789
- }
1790
-
1791
- return out._strip();
1792
- }
1793
-
1794
- function jumboMulTo (self, num, out) {
1795
- // Temporary disable, see https://github.com/indutny/bn.js/issues/211
1796
- // var fftm = new FFTM();
1797
- // return fftm.mulp(self, num, out);
1798
- return bigMulTo(self, num, out);
1799
- }
1800
-
1801
- BN.prototype.mulTo = function mulTo (num, out) {
1802
- var res;
1803
- var len = this.length + num.length;
1804
- if (this.length === 10 && num.length === 10) {
1805
- res = comb10MulTo(this, num, out);
1806
- } else if (len < 63) {
1807
- res = smallMulTo(this, num, out);
1808
- } else if (len < 1024) {
1809
- res = bigMulTo(this, num, out);
1810
- } else {
1811
- res = jumboMulTo(this, num, out);
1812
- }
1813
-
1814
- return res;
1815
- };
1816
-
1817
- // Multiply `this` by `num`
1818
- BN.prototype.mul = function mul (num) {
1819
- var out = new BN(null);
1820
- out.words = new Array(this.length + num.length);
1821
- return this.mulTo(num, out);
1822
- };
1823
-
1824
- // Multiply employing FFT
1825
- BN.prototype.mulf = function mulf (num) {
1826
- var out = new BN(null);
1827
- out.words = new Array(this.length + num.length);
1828
- return jumboMulTo(this, num, out);
1829
- };
1830
-
1831
- // In-place Multiplication
1832
- BN.prototype.imul = function imul (num) {
1833
- return this.clone().mulTo(num, this);
1834
- };
1835
-
1836
- BN.prototype.imuln = function imuln (num) {
1837
- var isNegNum = num < 0;
1838
- if (isNegNum) num = -num;
1839
-
1840
- assert(typeof num === 'number');
1841
- assert(num < 0x4000000);
1842
-
1843
- // Carry
1844
- var carry = 0;
1845
- for (var i = 0; i < this.length; i++) {
1846
- var w = (this.words[i] | 0) * num;
1847
- var lo = (w & 0x3ffffff) + (carry & 0x3ffffff);
1848
- carry >>= 26;
1849
- carry += (w / 0x4000000) | 0;
1850
- // NOTE: lo is 27bit maximum
1851
- carry += lo >>> 26;
1852
- this.words[i] = lo & 0x3ffffff;
1853
- }
1854
-
1855
- if (carry !== 0) {
1856
- this.words[i] = carry;
1857
- this.length++;
1858
- }
1859
-
1860
- return isNegNum ? this.ineg() : this;
1861
- };
1862
-
1863
- BN.prototype.muln = function muln (num) {
1864
- return this.clone().imuln(num);
1865
- };
1866
-
1867
- // `this` * `this`
1868
- BN.prototype.sqr = function sqr () {
1869
- return this.mul(this);
1870
- };
1871
-
1872
- // `this` * `this` in-place
1873
- BN.prototype.isqr = function isqr () {
1874
- return this.imul(this.clone());
1875
- };
1876
-
1877
- // Math.pow(`this`, `num`)
1878
- BN.prototype.pow = function pow (num) {
1879
- var w = toBitArray(num);
1880
- if (w.length === 0) return new BN(1);
1881
-
1882
- // Skip leading zeroes
1883
- var res = this;
1884
- for (var i = 0; i < w.length; i++, res = res.sqr()) {
1885
- if (w[i] !== 0) break;
1886
- }
1887
-
1888
- if (++i < w.length) {
1889
- for (var q = res.sqr(); i < w.length; i++, q = q.sqr()) {
1890
- if (w[i] === 0) continue;
1891
-
1892
- res = res.mul(q);
1893
- }
1894
- }
1895
-
1896
- return res;
1897
- };
1898
-
1899
- // Shift-left in-place
1900
- BN.prototype.iushln = function iushln (bits) {
1901
- assert(typeof bits === 'number' && bits >= 0);
1902
- var r = bits % 26;
1903
- var s = (bits - r) / 26;
1904
- var carryMask = (0x3ffffff >>> (26 - r)) << (26 - r);
1905
- var i;
1906
-
1907
- if (r !== 0) {
1908
- var carry = 0;
1909
-
1910
- for (i = 0; i < this.length; i++) {
1911
- var newCarry = this.words[i] & carryMask;
1912
- var c = ((this.words[i] | 0) - newCarry) << r;
1913
- this.words[i] = c | carry;
1914
- carry = newCarry >>> (26 - r);
1915
- }
1916
-
1917
- if (carry) {
1918
- this.words[i] = carry;
1919
- this.length++;
1920
- }
1921
- }
1922
-
1923
- if (s !== 0) {
1924
- for (i = this.length - 1; i >= 0; i--) {
1925
- this.words[i + s] = this.words[i];
1926
- }
1927
-
1928
- for (i = 0; i < s; i++) {
1929
- this.words[i] = 0;
1930
- }
1931
-
1932
- this.length += s;
1933
- }
1934
-
1935
- return this._strip();
1936
- };
1937
-
1938
- BN.prototype.ishln = function ishln (bits) {
1939
- // TODO(indutny): implement me
1940
- assert(this.negative === 0);
1941
- return this.iushln(bits);
1942
- };
1943
-
1944
- // Shift-right in-place
1945
- // NOTE: `hint` is a lowest bit before trailing zeroes
1946
- // NOTE: if `extended` is present - it will be filled with destroyed bits
1947
- BN.prototype.iushrn = function iushrn (bits, hint, extended) {
1948
- assert(typeof bits === 'number' && bits >= 0);
1949
- var h;
1950
- if (hint) {
1951
- h = (hint - (hint % 26)) / 26;
1952
- } else {
1953
- h = 0;
1954
- }
1955
-
1956
- var r = bits % 26;
1957
- var s = Math.min((bits - r) / 26, this.length);
1958
- var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r);
1959
- var maskedWords = extended;
1960
-
1961
- h -= s;
1962
- h = Math.max(0, h);
1963
-
1964
- // Extended mode, copy masked part
1965
- if (maskedWords) {
1966
- for (var i = 0; i < s; i++) {
1967
- maskedWords.words[i] = this.words[i];
1968
- }
1969
- maskedWords.length = s;
1970
- }
1971
-
1972
- if (s === 0) ; else if (this.length > s) {
1973
- this.length -= s;
1974
- for (i = 0; i < this.length; i++) {
1975
- this.words[i] = this.words[i + s];
1976
- }
1977
- } else {
1978
- this.words[0] = 0;
1979
- this.length = 1;
1980
- }
1981
-
1982
- var carry = 0;
1983
- for (i = this.length - 1; i >= 0 && (carry !== 0 || i >= h); i--) {
1984
- var word = this.words[i] | 0;
1985
- this.words[i] = (carry << (26 - r)) | (word >>> r);
1986
- carry = word & mask;
1987
- }
1988
-
1989
- // Push carried bits as a mask
1990
- if (maskedWords && carry !== 0) {
1991
- maskedWords.words[maskedWords.length++] = carry;
1992
- }
1993
-
1994
- if (this.length === 0) {
1995
- this.words[0] = 0;
1996
- this.length = 1;
1997
- }
1998
-
1999
- return this._strip();
2000
- };
2001
-
2002
- BN.prototype.ishrn = function ishrn (bits, hint, extended) {
2003
- // TODO(indutny): implement me
2004
- assert(this.negative === 0);
2005
- return this.iushrn(bits, hint, extended);
2006
- };
2007
-
2008
- // Shift-left
2009
- BN.prototype.shln = function shln (bits) {
2010
- return this.clone().ishln(bits);
2011
- };
2012
-
2013
- BN.prototype.ushln = function ushln (bits) {
2014
- return this.clone().iushln(bits);
2015
- };
2016
-
2017
- // Shift-right
2018
- BN.prototype.shrn = function shrn (bits) {
2019
- return this.clone().ishrn(bits);
2020
- };
2021
-
2022
- BN.prototype.ushrn = function ushrn (bits) {
2023
- return this.clone().iushrn(bits);
2024
- };
2025
-
2026
- // Test if n bit is set
2027
- BN.prototype.testn = function testn (bit) {
2028
- assert(typeof bit === 'number' && bit >= 0);
2029
- var r = bit % 26;
2030
- var s = (bit - r) / 26;
2031
- var q = 1 << r;
2032
-
2033
- // Fast case: bit is much higher than all existing words
2034
- if (this.length <= s) return false;
2035
-
2036
- // Check bit and return
2037
- var w = this.words[s];
2038
-
2039
- return !!(w & q);
2040
- };
2041
-
2042
- // Return only lowers bits of number (in-place)
2043
- BN.prototype.imaskn = function imaskn (bits) {
2044
- assert(typeof bits === 'number' && bits >= 0);
2045
- var r = bits % 26;
2046
- var s = (bits - r) / 26;
2047
-
2048
- assert(this.negative === 0, 'imaskn works only with positive numbers');
2049
-
2050
- if (this.length <= s) {
2051
- return this;
2052
- }
2053
-
2054
- if (r !== 0) {
2055
- s++;
2056
- }
2057
- this.length = Math.min(s, this.length);
2058
-
2059
- if (r !== 0) {
2060
- var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r);
2061
- this.words[this.length - 1] &= mask;
2062
- }
2063
-
2064
- return this._strip();
2065
- };
2066
-
2067
- // Return only lowers bits of number
2068
- BN.prototype.maskn = function maskn (bits) {
2069
- return this.clone().imaskn(bits);
2070
- };
2071
-
2072
- // Add plain number `num` to `this`
2073
- BN.prototype.iaddn = function iaddn (num) {
2074
- assert(typeof num === 'number');
2075
- assert(num < 0x4000000);
2076
- if (num < 0) return this.isubn(-num);
2077
-
2078
- // Possible sign change
2079
- if (this.negative !== 0) {
2080
- if (this.length === 1 && (this.words[0] | 0) <= num) {
2081
- this.words[0] = num - (this.words[0] | 0);
2082
- this.negative = 0;
2083
- return this;
2084
- }
2085
-
2086
- this.negative = 0;
2087
- this.isubn(num);
2088
- this.negative = 1;
2089
- return this;
2090
- }
2091
-
2092
- // Add without checks
2093
- return this._iaddn(num);
2094
- };
2095
-
2096
- BN.prototype._iaddn = function _iaddn (num) {
2097
- this.words[0] += num;
2098
-
2099
- // Carry
2100
- for (var i = 0; i < this.length && this.words[i] >= 0x4000000; i++) {
2101
- this.words[i] -= 0x4000000;
2102
- if (i === this.length - 1) {
2103
- this.words[i + 1] = 1;
2104
- } else {
2105
- this.words[i + 1]++;
2106
- }
2107
- }
2108
- this.length = Math.max(this.length, i + 1);
2109
-
2110
- return this;
2111
- };
2112
-
2113
- // Subtract plain number `num` from `this`
2114
- BN.prototype.isubn = function isubn (num) {
2115
- assert(typeof num === 'number');
2116
- assert(num < 0x4000000);
2117
- if (num < 0) return this.iaddn(-num);
2118
-
2119
- if (this.negative !== 0) {
2120
- this.negative = 0;
2121
- this.iaddn(num);
2122
- this.negative = 1;
2123
- return this;
2124
- }
2125
-
2126
- this.words[0] -= num;
2127
-
2128
- if (this.length === 1 && this.words[0] < 0) {
2129
- this.words[0] = -this.words[0];
2130
- this.negative = 1;
2131
- } else {
2132
- // Carry
2133
- for (var i = 0; i < this.length && this.words[i] < 0; i++) {
2134
- this.words[i] += 0x4000000;
2135
- this.words[i + 1] -= 1;
2136
- }
2137
- }
2138
-
2139
- return this._strip();
2140
- };
2141
-
2142
- BN.prototype.addn = function addn (num) {
2143
- return this.clone().iaddn(num);
2144
- };
2145
-
2146
- BN.prototype.subn = function subn (num) {
2147
- return this.clone().isubn(num);
2148
- };
2149
-
2150
- BN.prototype.iabs = function iabs () {
2151
- this.negative = 0;
2152
-
2153
- return this;
2154
- };
2155
-
2156
- BN.prototype.abs = function abs () {
2157
- return this.clone().iabs();
2158
- };
2159
-
2160
- BN.prototype._ishlnsubmul = function _ishlnsubmul (num, mul, shift) {
2161
- var len = num.length + shift;
2162
- var i;
2163
-
2164
- this._expand(len);
2165
-
2166
- var w;
2167
- var carry = 0;
2168
- for (i = 0; i < num.length; i++) {
2169
- w = (this.words[i + shift] | 0) + carry;
2170
- var right = (num.words[i] | 0) * mul;
2171
- w -= right & 0x3ffffff;
2172
- carry = (w >> 26) - ((right / 0x4000000) | 0);
2173
- this.words[i + shift] = w & 0x3ffffff;
2174
- }
2175
- for (; i < this.length - shift; i++) {
2176
- w = (this.words[i + shift] | 0) + carry;
2177
- carry = w >> 26;
2178
- this.words[i + shift] = w & 0x3ffffff;
2179
- }
2180
-
2181
- if (carry === 0) return this._strip();
2182
-
2183
- // Subtraction overflow
2184
- assert(carry === -1);
2185
- carry = 0;
2186
- for (i = 0; i < this.length; i++) {
2187
- w = -(this.words[i] | 0) + carry;
2188
- carry = w >> 26;
2189
- this.words[i] = w & 0x3ffffff;
2190
- }
2191
- this.negative = 1;
2192
-
2193
- return this._strip();
2194
- };
2195
-
2196
- BN.prototype._wordDiv = function _wordDiv (num, mode) {
2197
- var shift = this.length - num.length;
2198
-
2199
- var a = this.clone();
2200
- var b = num;
2201
-
2202
- // Normalize
2203
- var bhi = b.words[b.length - 1] | 0;
2204
- var bhiBits = this._countBits(bhi);
2205
- shift = 26 - bhiBits;
2206
- if (shift !== 0) {
2207
- b = b.ushln(shift);
2208
- a.iushln(shift);
2209
- bhi = b.words[b.length - 1] | 0;
2210
- }
2211
-
2212
- // Initialize quotient
2213
- var m = a.length - b.length;
2214
- var q;
2215
-
2216
- if (mode !== 'mod') {
2217
- q = new BN(null);
2218
- q.length = m + 1;
2219
- q.words = new Array(q.length);
2220
- for (var i = 0; i < q.length; i++) {
2221
- q.words[i] = 0;
2222
- }
2223
- }
2224
-
2225
- var diff = a.clone()._ishlnsubmul(b, 1, m);
2226
- if (diff.negative === 0) {
2227
- a = diff;
2228
- if (q) {
2229
- q.words[m] = 1;
2230
- }
2231
- }
2232
-
2233
- for (var j = m - 1; j >= 0; j--) {
2234
- var qj = (a.words[b.length + j] | 0) * 0x4000000 +
2235
- (a.words[b.length + j - 1] | 0);
2236
-
2237
- // NOTE: (qj / bhi) is (0x3ffffff * 0x4000000 + 0x3ffffff) / 0x2000000 max
2238
- // (0x7ffffff)
2239
- qj = Math.min((qj / bhi) | 0, 0x3ffffff);
2240
-
2241
- a._ishlnsubmul(b, qj, j);
2242
- while (a.negative !== 0) {
2243
- qj--;
2244
- a.negative = 0;
2245
- a._ishlnsubmul(b, 1, j);
2246
- if (!a.isZero()) {
2247
- a.negative ^= 1;
2248
- }
2249
- }
2250
- if (q) {
2251
- q.words[j] = qj;
2252
- }
2253
- }
2254
- if (q) {
2255
- q._strip();
2256
- }
2257
- a._strip();
2258
-
2259
- // Denormalize
2260
- if (mode !== 'div' && shift !== 0) {
2261
- a.iushrn(shift);
2262
- }
2263
-
2264
- return {
2265
- div: q || null,
2266
- mod: a
2267
- };
2268
- };
2269
-
2270
- // NOTE: 1) `mode` can be set to `mod` to request mod only,
2271
- // to `div` to request div only, or be absent to
2272
- // request both div & mod
2273
- // 2) `positive` is true if unsigned mod is requested
2274
- BN.prototype.divmod = function divmod (num, mode, positive) {
2275
- assert(!num.isZero());
2276
-
2277
- if (this.isZero()) {
2278
- return {
2279
- div: new BN(0),
2280
- mod: new BN(0)
2281
- };
2282
- }
2283
-
2284
- var div, mod, res;
2285
- if (this.negative !== 0 && num.negative === 0) {
2286
- res = this.neg().divmod(num, mode);
2287
-
2288
- if (mode !== 'mod') {
2289
- div = res.div.neg();
2290
- }
2291
-
2292
- if (mode !== 'div') {
2293
- mod = res.mod.neg();
2294
- if (positive && mod.negative !== 0) {
2295
- mod.iadd(num);
2296
- }
2297
- }
2298
-
2299
- return {
2300
- div: div,
2301
- mod: mod
2302
- };
2303
- }
2304
-
2305
- if (this.negative === 0 && num.negative !== 0) {
2306
- res = this.divmod(num.neg(), mode);
2307
-
2308
- if (mode !== 'mod') {
2309
- div = res.div.neg();
2310
- }
2311
-
2312
- return {
2313
- div: div,
2314
- mod: res.mod
2315
- };
2316
- }
2317
-
2318
- if ((this.negative & num.negative) !== 0) {
2319
- res = this.neg().divmod(num.neg(), mode);
2320
-
2321
- if (mode !== 'div') {
2322
- mod = res.mod.neg();
2323
- if (positive && mod.negative !== 0) {
2324
- mod.isub(num);
2325
- }
2326
- }
2327
-
2328
- return {
2329
- div: res.div,
2330
- mod: mod
2331
- };
2332
- }
2333
-
2334
- // Both numbers are positive at this point
2335
-
2336
- // Strip both numbers to approximate shift value
2337
- if (num.length > this.length || this.cmp(num) < 0) {
2338
- return {
2339
- div: new BN(0),
2340
- mod: this
2341
- };
2342
- }
2343
-
2344
- // Very short reduction
2345
- if (num.length === 1) {
2346
- if (mode === 'div') {
2347
- return {
2348
- div: this.divn(num.words[0]),
2349
- mod: null
2350
- };
2351
- }
2352
-
2353
- if (mode === 'mod') {
2354
- return {
2355
- div: null,
2356
- mod: new BN(this.modrn(num.words[0]))
2357
- };
2358
- }
2359
-
2360
- return {
2361
- div: this.divn(num.words[0]),
2362
- mod: new BN(this.modrn(num.words[0]))
2363
- };
2364
- }
2365
-
2366
- return this._wordDiv(num, mode);
2367
- };
2368
-
2369
- // Find `this` / `num`
2370
- BN.prototype.div = function div (num) {
2371
- return this.divmod(num, 'div', false).div;
2372
- };
2373
-
2374
- // Find `this` % `num`
2375
- BN.prototype.mod = function mod (num) {
2376
- return this.divmod(num, 'mod', false).mod;
2377
- };
2378
-
2379
- BN.prototype.umod = function umod (num) {
2380
- return this.divmod(num, 'mod', true).mod;
2381
- };
2382
-
2383
- // Find Round(`this` / `num`)
2384
- BN.prototype.divRound = function divRound (num) {
2385
- var dm = this.divmod(num);
2386
-
2387
- // Fast case - exact division
2388
- if (dm.mod.isZero()) return dm.div;
2389
-
2390
- var mod = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod;
2391
-
2392
- var half = num.ushrn(1);
2393
- var r2 = num.andln(1);
2394
- var cmp = mod.cmp(half);
2395
-
2396
- // Round down
2397
- if (cmp < 0 || (r2 === 1 && cmp === 0)) return dm.div;
2398
-
2399
- // Round up
2400
- return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1);
2401
- };
2402
-
2403
- BN.prototype.modrn = function modrn (num) {
2404
- var isNegNum = num < 0;
2405
- if (isNegNum) num = -num;
2406
-
2407
- assert(num <= 0x3ffffff);
2408
- var p = (1 << 26) % num;
2409
-
2410
- var acc = 0;
2411
- for (var i = this.length - 1; i >= 0; i--) {
2412
- acc = (p * acc + (this.words[i] | 0)) % num;
2413
- }
2414
-
2415
- return isNegNum ? -acc : acc;
2416
- };
2417
-
2418
- // WARNING: DEPRECATED
2419
- BN.prototype.modn = function modn (num) {
2420
- return this.modrn(num);
2421
- };
2422
-
2423
- // In-place division by number
2424
- BN.prototype.idivn = function idivn (num) {
2425
- var isNegNum = num < 0;
2426
- if (isNegNum) num = -num;
2427
-
2428
- assert(num <= 0x3ffffff);
2429
-
2430
- var carry = 0;
2431
- for (var i = this.length - 1; i >= 0; i--) {
2432
- var w = (this.words[i] | 0) + carry * 0x4000000;
2433
- this.words[i] = (w / num) | 0;
2434
- carry = w % num;
2435
- }
2436
-
2437
- this._strip();
2438
- return isNegNum ? this.ineg() : this;
2439
- };
2440
-
2441
- BN.prototype.divn = function divn (num) {
2442
- return this.clone().idivn(num);
2443
- };
2444
-
2445
- BN.prototype.egcd = function egcd (p) {
2446
- assert(p.negative === 0);
2447
- assert(!p.isZero());
2448
-
2449
- var x = this;
2450
- var y = p.clone();
2451
-
2452
- if (x.negative !== 0) {
2453
- x = x.umod(p);
2454
- } else {
2455
- x = x.clone();
2456
- }
2457
-
2458
- // A * x + B * y = x
2459
- var A = new BN(1);
2460
- var B = new BN(0);
2461
-
2462
- // C * x + D * y = y
2463
- var C = new BN(0);
2464
- var D = new BN(1);
2465
-
2466
- var g = 0;
2467
-
2468
- while (x.isEven() && y.isEven()) {
2469
- x.iushrn(1);
2470
- y.iushrn(1);
2471
- ++g;
2472
- }
2473
-
2474
- var yp = y.clone();
2475
- var xp = x.clone();
2476
-
2477
- while (!x.isZero()) {
2478
- for (var i = 0, im = 1; (x.words[0] & im) === 0 && i < 26; ++i, im <<= 1);
2479
- if (i > 0) {
2480
- x.iushrn(i);
2481
- while (i-- > 0) {
2482
- if (A.isOdd() || B.isOdd()) {
2483
- A.iadd(yp);
2484
- B.isub(xp);
2485
- }
2486
-
2487
- A.iushrn(1);
2488
- B.iushrn(1);
2489
- }
2490
- }
2491
-
2492
- for (var j = 0, jm = 1; (y.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1);
2493
- if (j > 0) {
2494
- y.iushrn(j);
2495
- while (j-- > 0) {
2496
- if (C.isOdd() || D.isOdd()) {
2497
- C.iadd(yp);
2498
- D.isub(xp);
2499
- }
2500
-
2501
- C.iushrn(1);
2502
- D.iushrn(1);
2503
- }
2504
- }
2505
-
2506
- if (x.cmp(y) >= 0) {
2507
- x.isub(y);
2508
- A.isub(C);
2509
- B.isub(D);
2510
- } else {
2511
- y.isub(x);
2512
- C.isub(A);
2513
- D.isub(B);
2514
- }
2515
- }
2516
-
2517
- return {
2518
- a: C,
2519
- b: D,
2520
- gcd: y.iushln(g)
2521
- };
2522
- };
2523
-
2524
- // This is reduced incarnation of the binary EEA
2525
- // above, designated to invert members of the
2526
- // _prime_ fields F(p) at a maximal speed
2527
- BN.prototype._invmp = function _invmp (p) {
2528
- assert(p.negative === 0);
2529
- assert(!p.isZero());
2530
-
2531
- var a = this;
2532
- var b = p.clone();
2533
-
2534
- if (a.negative !== 0) {
2535
- a = a.umod(p);
2536
- } else {
2537
- a = a.clone();
2538
- }
2539
-
2540
- var x1 = new BN(1);
2541
- var x2 = new BN(0);
2542
-
2543
- var delta = b.clone();
2544
-
2545
- while (a.cmpn(1) > 0 && b.cmpn(1) > 0) {
2546
- for (var i = 0, im = 1; (a.words[0] & im) === 0 && i < 26; ++i, im <<= 1);
2547
- if (i > 0) {
2548
- a.iushrn(i);
2549
- while (i-- > 0) {
2550
- if (x1.isOdd()) {
2551
- x1.iadd(delta);
2552
- }
2553
-
2554
- x1.iushrn(1);
2555
- }
2556
- }
2557
-
2558
- for (var j = 0, jm = 1; (b.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1);
2559
- if (j > 0) {
2560
- b.iushrn(j);
2561
- while (j-- > 0) {
2562
- if (x2.isOdd()) {
2563
- x2.iadd(delta);
2564
- }
2565
-
2566
- x2.iushrn(1);
2567
- }
2568
- }
2569
-
2570
- if (a.cmp(b) >= 0) {
2571
- a.isub(b);
2572
- x1.isub(x2);
2573
- } else {
2574
- b.isub(a);
2575
- x2.isub(x1);
2576
- }
2577
- }
2578
-
2579
- var res;
2580
- if (a.cmpn(1) === 0) {
2581
- res = x1;
2582
- } else {
2583
- res = x2;
2584
- }
2585
-
2586
- if (res.cmpn(0) < 0) {
2587
- res.iadd(p);
2588
- }
2589
-
2590
- return res;
2591
- };
2592
-
2593
- BN.prototype.gcd = function gcd (num) {
2594
- if (this.isZero()) return num.abs();
2595
- if (num.isZero()) return this.abs();
2596
-
2597
- var a = this.clone();
2598
- var b = num.clone();
2599
- a.negative = 0;
2600
- b.negative = 0;
2601
-
2602
- // Remove common factor of two
2603
- for (var shift = 0; a.isEven() && b.isEven(); shift++) {
2604
- a.iushrn(1);
2605
- b.iushrn(1);
2606
- }
2607
-
2608
- do {
2609
- while (a.isEven()) {
2610
- a.iushrn(1);
2611
- }
2612
- while (b.isEven()) {
2613
- b.iushrn(1);
2614
- }
2615
-
2616
- var r = a.cmp(b);
2617
- if (r < 0) {
2618
- // Swap `a` and `b` to make `a` always bigger than `b`
2619
- var t = a;
2620
- a = b;
2621
- b = t;
2622
- } else if (r === 0 || b.cmpn(1) === 0) {
2623
- break;
2624
- }
2625
-
2626
- a.isub(b);
2627
- } while (true);
2628
-
2629
- return b.iushln(shift);
2630
- };
2631
-
2632
- // Invert number in the field F(num)
2633
- BN.prototype.invm = function invm (num) {
2634
- return this.egcd(num).a.umod(num);
2635
- };
2636
-
2637
- BN.prototype.isEven = function isEven () {
2638
- return (this.words[0] & 1) === 0;
2639
- };
2640
-
2641
- BN.prototype.isOdd = function isOdd () {
2642
- return (this.words[0] & 1) === 1;
2643
- };
2644
-
2645
- // And first word and num
2646
- BN.prototype.andln = function andln (num) {
2647
- return this.words[0] & num;
2648
- };
2649
-
2650
- // Increment at the bit position in-line
2651
- BN.prototype.bincn = function bincn (bit) {
2652
- assert(typeof bit === 'number');
2653
- var r = bit % 26;
2654
- var s = (bit - r) / 26;
2655
- var q = 1 << r;
2656
-
2657
- // Fast case: bit is much higher than all existing words
2658
- if (this.length <= s) {
2659
- this._expand(s + 1);
2660
- this.words[s] |= q;
2661
- return this;
2662
- }
2663
-
2664
- // Add bit and propagate, if needed
2665
- var carry = q;
2666
- for (var i = s; carry !== 0 && i < this.length; i++) {
2667
- var w = this.words[i] | 0;
2668
- w += carry;
2669
- carry = w >>> 26;
2670
- w &= 0x3ffffff;
2671
- this.words[i] = w;
2672
- }
2673
- if (carry !== 0) {
2674
- this.words[i] = carry;
2675
- this.length++;
2676
- }
2677
- return this;
2678
- };
2679
-
2680
- BN.prototype.isZero = function isZero () {
2681
- return this.length === 1 && this.words[0] === 0;
2682
- };
2683
-
2684
- BN.prototype.cmpn = function cmpn (num) {
2685
- var negative = num < 0;
2686
-
2687
- if (this.negative !== 0 && !negative) return -1;
2688
- if (this.negative === 0 && negative) return 1;
2689
-
2690
- this._strip();
2691
-
2692
- var res;
2693
- if (this.length > 1) {
2694
- res = 1;
2695
- } else {
2696
- if (negative) {
2697
- num = -num;
2698
- }
2699
-
2700
- assert(num <= 0x3ffffff, 'Number is too big');
2701
-
2702
- var w = this.words[0] | 0;
2703
- res = w === num ? 0 : w < num ? -1 : 1;
2704
- }
2705
- if (this.negative !== 0) return -res | 0;
2706
- return res;
2707
- };
2708
-
2709
- // Compare two numbers and return:
2710
- // 1 - if `this` > `num`
2711
- // 0 - if `this` == `num`
2712
- // -1 - if `this` < `num`
2713
- BN.prototype.cmp = function cmp (num) {
2714
- if (this.negative !== 0 && num.negative === 0) return -1;
2715
- if (this.negative === 0 && num.negative !== 0) return 1;
2716
-
2717
- var res = this.ucmp(num);
2718
- if (this.negative !== 0) return -res | 0;
2719
- return res;
2720
- };
2721
-
2722
- // Unsigned comparison
2723
- BN.prototype.ucmp = function ucmp (num) {
2724
- // At this point both numbers have the same sign
2725
- if (this.length > num.length) return 1;
2726
- if (this.length < num.length) return -1;
2727
-
2728
- var res = 0;
2729
- for (var i = this.length - 1; i >= 0; i--) {
2730
- var a = this.words[i] | 0;
2731
- var b = num.words[i] | 0;
2732
-
2733
- if (a === b) continue;
2734
- if (a < b) {
2735
- res = -1;
2736
- } else if (a > b) {
2737
- res = 1;
2738
- }
2739
- break;
2740
- }
2741
- return res;
2742
- };
2743
-
2744
- BN.prototype.gtn = function gtn (num) {
2745
- return this.cmpn(num) === 1;
2746
- };
2747
-
2748
- BN.prototype.gt = function gt (num) {
2749
- return this.cmp(num) === 1;
2750
- };
2751
-
2752
- BN.prototype.gten = function gten (num) {
2753
- return this.cmpn(num) >= 0;
2754
- };
2755
-
2756
- BN.prototype.gte = function gte (num) {
2757
- return this.cmp(num) >= 0;
2758
- };
2759
-
2760
- BN.prototype.ltn = function ltn (num) {
2761
- return this.cmpn(num) === -1;
2762
- };
2763
-
2764
- BN.prototype.lt = function lt (num) {
2765
- return this.cmp(num) === -1;
2766
- };
2767
-
2768
- BN.prototype.lten = function lten (num) {
2769
- return this.cmpn(num) <= 0;
2770
- };
2771
-
2772
- BN.prototype.lte = function lte (num) {
2773
- return this.cmp(num) <= 0;
2774
- };
2775
-
2776
- BN.prototype.eqn = function eqn (num) {
2777
- return this.cmpn(num) === 0;
2778
- };
2779
-
2780
- BN.prototype.eq = function eq (num) {
2781
- return this.cmp(num) === 0;
2782
- };
2783
-
2784
- //
2785
- // A reduce context, could be using montgomery or something better, depending
2786
- // on the `m` itself.
2787
- //
2788
- BN.red = function red (num) {
2789
- return new Red(num);
2790
- };
2791
-
2792
- BN.prototype.toRed = function toRed (ctx) {
2793
- assert(!this.red, 'Already a number in reduction context');
2794
- assert(this.negative === 0, 'red works only with positives');
2795
- return ctx.convertTo(this)._forceRed(ctx);
2796
- };
2797
-
2798
- BN.prototype.fromRed = function fromRed () {
2799
- assert(this.red, 'fromRed works only with numbers in reduction context');
2800
- return this.red.convertFrom(this);
2801
- };
2802
-
2803
- BN.prototype._forceRed = function _forceRed (ctx) {
2804
- this.red = ctx;
2805
- return this;
2806
- };
2807
-
2808
- BN.prototype.forceRed = function forceRed (ctx) {
2809
- assert(!this.red, 'Already a number in reduction context');
2810
- return this._forceRed(ctx);
2811
- };
2812
-
2813
- BN.prototype.redAdd = function redAdd (num) {
2814
- assert(this.red, 'redAdd works only with red numbers');
2815
- return this.red.add(this, num);
2816
- };
2817
-
2818
- BN.prototype.redIAdd = function redIAdd (num) {
2819
- assert(this.red, 'redIAdd works only with red numbers');
2820
- return this.red.iadd(this, num);
2821
- };
2822
-
2823
- BN.prototype.redSub = function redSub (num) {
2824
- assert(this.red, 'redSub works only with red numbers');
2825
- return this.red.sub(this, num);
2826
- };
2827
-
2828
- BN.prototype.redISub = function redISub (num) {
2829
- assert(this.red, 'redISub works only with red numbers');
2830
- return this.red.isub(this, num);
2831
- };
2832
-
2833
- BN.prototype.redShl = function redShl (num) {
2834
- assert(this.red, 'redShl works only with red numbers');
2835
- return this.red.shl(this, num);
2836
- };
2837
-
2838
- BN.prototype.redMul = function redMul (num) {
2839
- assert(this.red, 'redMul works only with red numbers');
2840
- this.red._verify2(this, num);
2841
- return this.red.mul(this, num);
2842
- };
2843
-
2844
- BN.prototype.redIMul = function redIMul (num) {
2845
- assert(this.red, 'redMul works only with red numbers');
2846
- this.red._verify2(this, num);
2847
- return this.red.imul(this, num);
2848
- };
2849
-
2850
- BN.prototype.redSqr = function redSqr () {
2851
- assert(this.red, 'redSqr works only with red numbers');
2852
- this.red._verify1(this);
2853
- return this.red.sqr(this);
2854
- };
2855
-
2856
- BN.prototype.redISqr = function redISqr () {
2857
- assert(this.red, 'redISqr works only with red numbers');
2858
- this.red._verify1(this);
2859
- return this.red.isqr(this);
2860
- };
2861
-
2862
- // Square root over p
2863
- BN.prototype.redSqrt = function redSqrt () {
2864
- assert(this.red, 'redSqrt works only with red numbers');
2865
- this.red._verify1(this);
2866
- return this.red.sqrt(this);
2867
- };
2868
-
2869
- BN.prototype.redInvm = function redInvm () {
2870
- assert(this.red, 'redInvm works only with red numbers');
2871
- this.red._verify1(this);
2872
- return this.red.invm(this);
2873
- };
2874
-
2875
- // Return negative clone of `this` % `red modulo`
2876
- BN.prototype.redNeg = function redNeg () {
2877
- assert(this.red, 'redNeg works only with red numbers');
2878
- this.red._verify1(this);
2879
- return this.red.neg(this);
2880
- };
2881
-
2882
- BN.prototype.redPow = function redPow (num) {
2883
- assert(this.red && !num.red, 'redPow(normalNum)');
2884
- this.red._verify1(this);
2885
- return this.red.pow(this, num);
2886
- };
2887
-
2888
- // Prime numbers with efficient reduction
2889
- var primes = {
2890
- k256: null,
2891
- p224: null,
2892
- p192: null,
2893
- p25519: null
2894
- };
2895
-
2896
- // Pseudo-Mersenne prime
2897
- function MPrime (name, p) {
2898
- // P = 2 ^ N - K
2899
- this.name = name;
2900
- this.p = new BN(p, 16);
2901
- this.n = this.p.bitLength();
2902
- this.k = new BN(1).iushln(this.n).isub(this.p);
2903
-
2904
- this.tmp = this._tmp();
2905
- }
2906
-
2907
- MPrime.prototype._tmp = function _tmp () {
2908
- var tmp = new BN(null);
2909
- tmp.words = new Array(Math.ceil(this.n / 13));
2910
- return tmp;
2911
- };
2912
-
2913
- MPrime.prototype.ireduce = function ireduce (num) {
2914
- // Assumes that `num` is less than `P^2`
2915
- // num = HI * (2 ^ N - K) + HI * K + LO = HI * K + LO (mod P)
2916
- var r = num;
2917
- var rlen;
2918
-
2919
- do {
2920
- this.split(r, this.tmp);
2921
- r = this.imulK(r);
2922
- r = r.iadd(this.tmp);
2923
- rlen = r.bitLength();
2924
- } while (rlen > this.n);
2925
-
2926
- var cmp = rlen < this.n ? -1 : r.ucmp(this.p);
2927
- if (cmp === 0) {
2928
- r.words[0] = 0;
2929
- r.length = 1;
2930
- } else if (cmp > 0) {
2931
- r.isub(this.p);
2932
- } else {
2933
- if (r.strip !== undefined) {
2934
- // r is a BN v4 instance
2935
- r.strip();
2936
- } else {
2937
- // r is a BN v5 instance
2938
- r._strip();
2939
- }
2940
- }
2941
-
2942
- return r;
2943
- };
2944
-
2945
- MPrime.prototype.split = function split (input, out) {
2946
- input.iushrn(this.n, 0, out);
2947
- };
2948
-
2949
- MPrime.prototype.imulK = function imulK (num) {
2950
- return num.imul(this.k);
2951
- };
2952
-
2953
- function K256 () {
2954
- MPrime.call(
2955
- this,
2956
- 'k256',
2957
- 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f');
2958
- }
2959
- inherits(K256, MPrime);
2960
-
2961
- K256.prototype.split = function split (input, output) {
2962
- // 256 = 9 * 26 + 22
2963
- var mask = 0x3fffff;
2964
-
2965
- var outLen = Math.min(input.length, 9);
2966
- for (var i = 0; i < outLen; i++) {
2967
- output.words[i] = input.words[i];
2968
- }
2969
- output.length = outLen;
2970
-
2971
- if (input.length <= 9) {
2972
- input.words[0] = 0;
2973
- input.length = 1;
2974
- return;
2975
- }
2976
-
2977
- // Shift by 9 limbs
2978
- var prev = input.words[9];
2979
- output.words[output.length++] = prev & mask;
2980
-
2981
- for (i = 10; i < input.length; i++) {
2982
- var next = input.words[i] | 0;
2983
- input.words[i - 10] = ((next & mask) << 4) | (prev >>> 22);
2984
- prev = next;
2985
- }
2986
- prev >>>= 22;
2987
- input.words[i - 10] = prev;
2988
- if (prev === 0 && input.length > 10) {
2989
- input.length -= 10;
2990
- } else {
2991
- input.length -= 9;
2992
- }
2993
- };
2994
-
2995
- K256.prototype.imulK = function imulK (num) {
2996
- // K = 0x1000003d1 = [ 0x40, 0x3d1 ]
2997
- num.words[num.length] = 0;
2998
- num.words[num.length + 1] = 0;
2999
- num.length += 2;
3000
-
3001
- // bounded at: 0x40 * 0x3ffffff + 0x3d0 = 0x100000390
3002
- var lo = 0;
3003
- for (var i = 0; i < num.length; i++) {
3004
- var w = num.words[i] | 0;
3005
- lo += w * 0x3d1;
3006
- num.words[i] = lo & 0x3ffffff;
3007
- lo = w * 0x40 + ((lo / 0x4000000) | 0);
3008
- }
3009
-
3010
- // Fast length reduction
3011
- if (num.words[num.length - 1] === 0) {
3012
- num.length--;
3013
- if (num.words[num.length - 1] === 0) {
3014
- num.length--;
3015
- }
3016
- }
3017
- return num;
3018
- };
3019
-
3020
- function P224 () {
3021
- MPrime.call(
3022
- this,
3023
- 'p224',
3024
- 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001');
3025
- }
3026
- inherits(P224, MPrime);
3027
-
3028
- function P192 () {
3029
- MPrime.call(
3030
- this,
3031
- 'p192',
3032
- 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff');
3033
- }
3034
- inherits(P192, MPrime);
3035
-
3036
- function P25519 () {
3037
- // 2 ^ 255 - 19
3038
- MPrime.call(
3039
- this,
3040
- '25519',
3041
- '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed');
3042
- }
3043
- inherits(P25519, MPrime);
3044
-
3045
- P25519.prototype.imulK = function imulK (num) {
3046
- // K = 0x13
3047
- var carry = 0;
3048
- for (var i = 0; i < num.length; i++) {
3049
- var hi = (num.words[i] | 0) * 0x13 + carry;
3050
- var lo = hi & 0x3ffffff;
3051
- hi >>>= 26;
3052
-
3053
- num.words[i] = lo;
3054
- carry = hi;
3055
- }
3056
- if (carry !== 0) {
3057
- num.words[num.length++] = carry;
3058
- }
3059
- return num;
3060
- };
3061
-
3062
- // Exported mostly for testing purposes, use plain name instead
3063
- BN._prime = function prime (name) {
3064
- // Cached version of prime
3065
- if (primes[name]) return primes[name];
3066
-
3067
- var prime;
3068
- if (name === 'k256') {
3069
- prime = new K256();
3070
- } else if (name === 'p224') {
3071
- prime = new P224();
3072
- } else if (name === 'p192') {
3073
- prime = new P192();
3074
- } else if (name === 'p25519') {
3075
- prime = new P25519();
3076
- } else {
3077
- throw new Error('Unknown prime ' + name);
3078
- }
3079
- primes[name] = prime;
3080
-
3081
- return prime;
3082
- };
3083
-
3084
- //
3085
- // Base reduction engine
3086
- //
3087
- function Red (m) {
3088
- if (typeof m === 'string') {
3089
- var prime = BN._prime(m);
3090
- this.m = prime.p;
3091
- this.prime = prime;
3092
- } else {
3093
- assert(m.gtn(1), 'modulus must be greater than 1');
3094
- this.m = m;
3095
- this.prime = null;
3096
- }
3097
- }
3098
-
3099
- Red.prototype._verify1 = function _verify1 (a) {
3100
- assert(a.negative === 0, 'red works only with positives');
3101
- assert(a.red, 'red works only with red numbers');
3102
- };
3103
-
3104
- Red.prototype._verify2 = function _verify2 (a, b) {
3105
- assert((a.negative | b.negative) === 0, 'red works only with positives');
3106
- assert(a.red && a.red === b.red,
3107
- 'red works only with red numbers');
3108
- };
3109
-
3110
- Red.prototype.imod = function imod (a) {
3111
- if (this.prime) return this.prime.ireduce(a)._forceRed(this);
3112
-
3113
- move(a, a.umod(this.m)._forceRed(this));
3114
- return a;
3115
- };
3116
-
3117
- Red.prototype.neg = function neg (a) {
3118
- if (a.isZero()) {
3119
- return a.clone();
3120
- }
3121
-
3122
- return this.m.sub(a)._forceRed(this);
3123
- };
3124
-
3125
- Red.prototype.add = function add (a, b) {
3126
- this._verify2(a, b);
3127
-
3128
- var res = a.add(b);
3129
- if (res.cmp(this.m) >= 0) {
3130
- res.isub(this.m);
3131
- }
3132
- return res._forceRed(this);
3133
- };
3134
-
3135
- Red.prototype.iadd = function iadd (a, b) {
3136
- this._verify2(a, b);
3137
-
3138
- var res = a.iadd(b);
3139
- if (res.cmp(this.m) >= 0) {
3140
- res.isub(this.m);
3141
- }
3142
- return res;
3143
- };
3144
-
3145
- Red.prototype.sub = function sub (a, b) {
3146
- this._verify2(a, b);
3147
-
3148
- var res = a.sub(b);
3149
- if (res.cmpn(0) < 0) {
3150
- res.iadd(this.m);
3151
- }
3152
- return res._forceRed(this);
3153
- };
3154
-
3155
- Red.prototype.isub = function isub (a, b) {
3156
- this._verify2(a, b);
3157
-
3158
- var res = a.isub(b);
3159
- if (res.cmpn(0) < 0) {
3160
- res.iadd(this.m);
3161
- }
3162
- return res;
3163
- };
3164
-
3165
- Red.prototype.shl = function shl (a, num) {
3166
- this._verify1(a);
3167
- return this.imod(a.ushln(num));
3168
- };
3169
-
3170
- Red.prototype.imul = function imul (a, b) {
3171
- this._verify2(a, b);
3172
- return this.imod(a.imul(b));
3173
- };
3174
-
3175
- Red.prototype.mul = function mul (a, b) {
3176
- this._verify2(a, b);
3177
- return this.imod(a.mul(b));
3178
- };
3179
-
3180
- Red.prototype.isqr = function isqr (a) {
3181
- return this.imul(a, a.clone());
3182
- };
3183
-
3184
- Red.prototype.sqr = function sqr (a) {
3185
- return this.mul(a, a);
3186
- };
3187
-
3188
- Red.prototype.sqrt = function sqrt (a) {
3189
- if (a.isZero()) return a.clone();
3190
-
3191
- var mod3 = this.m.andln(3);
3192
- assert(mod3 % 2 === 1);
3193
-
3194
- // Fast case
3195
- if (mod3 === 3) {
3196
- var pow = this.m.add(new BN(1)).iushrn(2);
3197
- return this.pow(a, pow);
3198
- }
3199
-
3200
- // Tonelli-Shanks algorithm (Totally unoptimized and slow)
3201
- //
3202
- // Find Q and S, that Q * 2 ^ S = (P - 1)
3203
- var q = this.m.subn(1);
3204
- var s = 0;
3205
- while (!q.isZero() && q.andln(1) === 0) {
3206
- s++;
3207
- q.iushrn(1);
3208
- }
3209
- assert(!q.isZero());
3210
-
3211
- var one = new BN(1).toRed(this);
3212
- var nOne = one.redNeg();
3213
-
3214
- // Find quadratic non-residue
3215
- // NOTE: Max is such because of generalized Riemann hypothesis.
3216
- var lpow = this.m.subn(1).iushrn(1);
3217
- var z = this.m.bitLength();
3218
- z = new BN(2 * z * z).toRed(this);
3219
-
3220
- while (this.pow(z, lpow).cmp(nOne) !== 0) {
3221
- z.redIAdd(nOne);
3222
- }
3223
-
3224
- var c = this.pow(z, q);
3225
- var r = this.pow(a, q.addn(1).iushrn(1));
3226
- var t = this.pow(a, q);
3227
- var m = s;
3228
- while (t.cmp(one) !== 0) {
3229
- var tmp = t;
3230
- for (var i = 0; tmp.cmp(one) !== 0; i++) {
3231
- tmp = tmp.redSqr();
3232
- }
3233
- assert(i < m);
3234
- var b = this.pow(c, new BN(1).iushln(m - i - 1));
3235
-
3236
- r = r.redMul(b);
3237
- c = b.redSqr();
3238
- t = t.redMul(c);
3239
- m = i;
3240
- }
3241
-
3242
- return r;
3243
- };
3244
-
3245
- Red.prototype.invm = function invm (a) {
3246
- var inv = a._invmp(this.m);
3247
- if (inv.negative !== 0) {
3248
- inv.negative = 0;
3249
- return this.imod(inv).redNeg();
3250
- } else {
3251
- return this.imod(inv);
3252
- }
3253
- };
3254
-
3255
- Red.prototype.pow = function pow (a, num) {
3256
- if (num.isZero()) return new BN(1).toRed(this);
3257
- if (num.cmpn(1) === 0) return a.clone();
3258
-
3259
- var windowSize = 4;
3260
- var wnd = new Array(1 << windowSize);
3261
- wnd[0] = new BN(1).toRed(this);
3262
- wnd[1] = a;
3263
- for (var i = 2; i < wnd.length; i++) {
3264
- wnd[i] = this.mul(wnd[i - 1], a);
3265
- }
3266
-
3267
- var res = wnd[0];
3268
- var current = 0;
3269
- var currentLen = 0;
3270
- var start = num.bitLength() % 26;
3271
- if (start === 0) {
3272
- start = 26;
3273
- }
3274
-
3275
- for (i = num.length - 1; i >= 0; i--) {
3276
- var word = num.words[i];
3277
- for (var j = start - 1; j >= 0; j--) {
3278
- var bit = (word >> j) & 1;
3279
- if (res !== wnd[0]) {
3280
- res = this.sqr(res);
3281
- }
3282
-
3283
- if (bit === 0 && current === 0) {
3284
- currentLen = 0;
3285
- continue;
3286
- }
3287
-
3288
- current <<= 1;
3289
- current |= bit;
3290
- currentLen++;
3291
- if (currentLen !== windowSize && (i !== 0 || j !== 0)) continue;
3292
-
3293
- res = this.mul(res, wnd[current]);
3294
- currentLen = 0;
3295
- current = 0;
3296
- }
3297
- start = 26;
3298
- }
3299
-
3300
- return res;
3301
- };
3302
-
3303
- Red.prototype.convertTo = function convertTo (num) {
3304
- var r = num.umod(this.m);
3305
-
3306
- return r === num ? r.clone() : r;
3307
- };
3308
-
3309
- Red.prototype.convertFrom = function convertFrom (num) {
3310
- var res = num.clone();
3311
- res.red = null;
3312
- return res;
3313
- };
3314
-
3315
- //
3316
- // Montgomery method engine
3317
- //
3318
-
3319
- BN.mont = function mont (num) {
3320
- return new Mont(num);
3321
- };
3322
-
3323
- function Mont (m) {
3324
- Red.call(this, m);
3325
-
3326
- this.shift = this.m.bitLength();
3327
- if (this.shift % 26 !== 0) {
3328
- this.shift += 26 - (this.shift % 26);
3329
- }
3330
-
3331
- this.r = new BN(1).iushln(this.shift);
3332
- this.r2 = this.imod(this.r.sqr());
3333
- this.rinv = this.r._invmp(this.m);
3334
-
3335
- this.minv = this.rinv.mul(this.r).isubn(1).div(this.m);
3336
- this.minv = this.minv.umod(this.r);
3337
- this.minv = this.r.sub(this.minv);
3338
- }
3339
- inherits(Mont, Red);
3340
-
3341
- Mont.prototype.convertTo = function convertTo (num) {
3342
- return this.imod(num.ushln(this.shift));
3343
- };
3344
-
3345
- Mont.prototype.convertFrom = function convertFrom (num) {
3346
- var r = this.imod(num.mul(this.rinv));
3347
- r.red = null;
3348
- return r;
3349
- };
3350
-
3351
- Mont.prototype.imul = function imul (a, b) {
3352
- if (a.isZero() || b.isZero()) {
3353
- a.words[0] = 0;
3354
- a.length = 1;
3355
- return a;
3356
- }
3357
-
3358
- var t = a.imul(b);
3359
- var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m);
3360
- var u = t.isub(c).iushrn(this.shift);
3361
- var res = u;
3362
-
3363
- if (u.cmp(this.m) >= 0) {
3364
- res = u.isub(this.m);
3365
- } else if (u.cmpn(0) < 0) {
3366
- res = u.iadd(this.m);
3367
- }
3368
-
3369
- return res._forceRed(this);
3370
- };
3371
-
3372
- Mont.prototype.mul = function mul (a, b) {
3373
- if (a.isZero() || b.isZero()) return new BN(0)._forceRed(this);
3374
-
3375
- var t = a.mul(b);
3376
- var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m);
3377
- var u = t.isub(c).iushrn(this.shift);
3378
- var res = u;
3379
- if (u.cmp(this.m) >= 0) {
3380
- res = u.isub(this.m);
3381
- } else if (u.cmpn(0) < 0) {
3382
- res = u.iadd(this.m);
3383
- }
3384
-
3385
- return res._forceRed(this);
3386
- };
3387
-
3388
- Mont.prototype.invm = function invm (a) {
3389
- // (AR)^-1 * R^2 = (A^-1 * R^-1) * R^2 = A^-1 * R
3390
- var res = this.imod(a._invmp(this.m).mul(this.r2));
3391
- return res._forceRed(this);
3392
- };
3393
- })(module, commonjsGlobal);
3394
- } (bn));
3395
-
3396
- var bnExports = bn.exports;
3397
- var _BN = /*@__PURE__*/getDefaultExportFromCjs(bnExports);
3398
-
3399
- const version$2 = "logger/5.7.0";
3400
-
3401
- let _permanentCensorErrors = false;
3402
- let _censorErrors = false;
3403
- const LogLevels = { debug: 1, "default": 2, info: 2, warning: 3, error: 4, off: 5 };
3404
- let _logLevel = LogLevels["default"];
3405
- let _globalLogger = null;
3406
- function _checkNormalize() {
3407
- try {
3408
- const missing = [];
3409
- // Make sure all forms of normalization are supported
3410
- ["NFD", "NFC", "NFKD", "NFKC"].forEach((form) => {
3411
- try {
3412
- if ("test".normalize(form) !== "test") {
3413
- throw new Error("bad normalize");
3414
- }
3415
- ;
3416
- }
3417
- catch (error) {
3418
- missing.push(form);
3419
- }
3420
- });
3421
- if (missing.length) {
3422
- throw new Error("missing " + missing.join(", "));
3423
- }
3424
- if (String.fromCharCode(0xe9).normalize("NFD") !== String.fromCharCode(0x65, 0x0301)) {
3425
- throw new Error("broken implementation");
3426
- }
3427
- }
3428
- catch (error) {
3429
- return error.message;
3430
- }
3431
- return null;
3432
- }
3433
- const _normalizeError = _checkNormalize();
3434
- var LogLevel;
3435
- (function (LogLevel) {
3436
- LogLevel["DEBUG"] = "DEBUG";
3437
- LogLevel["INFO"] = "INFO";
3438
- LogLevel["WARNING"] = "WARNING";
3439
- LogLevel["ERROR"] = "ERROR";
3440
- LogLevel["OFF"] = "OFF";
3441
- })(LogLevel || (LogLevel = {}));
3442
- var ErrorCode;
3443
- (function (ErrorCode) {
3444
- ///////////////////
3445
- // Generic Errors
3446
- // Unknown Error
3447
- ErrorCode["UNKNOWN_ERROR"] = "UNKNOWN_ERROR";
3448
- // Not Implemented
3449
- ErrorCode["NOT_IMPLEMENTED"] = "NOT_IMPLEMENTED";
3450
- // Unsupported Operation
3451
- // - operation
3452
- ErrorCode["UNSUPPORTED_OPERATION"] = "UNSUPPORTED_OPERATION";
3453
- // Network Error (i.e. Ethereum Network, such as an invalid chain ID)
3454
- // - event ("noNetwork" is not re-thrown in provider.ready; otherwise thrown)
3455
- ErrorCode["NETWORK_ERROR"] = "NETWORK_ERROR";
3456
- // Some sort of bad response from the server
3457
- ErrorCode["SERVER_ERROR"] = "SERVER_ERROR";
3458
- // Timeout
3459
- ErrorCode["TIMEOUT"] = "TIMEOUT";
3460
- ///////////////////
3461
- // Operational Errors
3462
- // Buffer Overrun
3463
- ErrorCode["BUFFER_OVERRUN"] = "BUFFER_OVERRUN";
3464
- // Numeric Fault
3465
- // - operation: the operation being executed
3466
- // - fault: the reason this faulted
3467
- ErrorCode["NUMERIC_FAULT"] = "NUMERIC_FAULT";
3468
- ///////////////////
3469
- // Argument Errors
3470
- // Missing new operator to an object
3471
- // - name: The name of the class
3472
- ErrorCode["MISSING_NEW"] = "MISSING_NEW";
3473
- // Invalid argument (e.g. value is incompatible with type) to a function:
3474
- // - argument: The argument name that was invalid
3475
- // - value: The value of the argument
3476
- ErrorCode["INVALID_ARGUMENT"] = "INVALID_ARGUMENT";
3477
- // Missing argument to a function:
3478
- // - count: The number of arguments received
3479
- // - expectedCount: The number of arguments expected
3480
- ErrorCode["MISSING_ARGUMENT"] = "MISSING_ARGUMENT";
3481
- // Too many arguments
3482
- // - count: The number of arguments received
3483
- // - expectedCount: The number of arguments expected
3484
- ErrorCode["UNEXPECTED_ARGUMENT"] = "UNEXPECTED_ARGUMENT";
3485
- ///////////////////
3486
- // Blockchain Errors
3487
- // Call exception
3488
- // - transaction: the transaction
3489
- // - address?: the contract address
3490
- // - args?: The arguments passed into the function
3491
- // - method?: The Solidity method signature
3492
- // - errorSignature?: The EIP848 error signature
3493
- // - errorArgs?: The EIP848 error parameters
3494
- // - reason: The reason (only for EIP848 "Error(string)")
3495
- ErrorCode["CALL_EXCEPTION"] = "CALL_EXCEPTION";
3496
- // Insufficient funds (< value + gasLimit * gasPrice)
3497
- // - transaction: the transaction attempted
3498
- ErrorCode["INSUFFICIENT_FUNDS"] = "INSUFFICIENT_FUNDS";
3499
- // Nonce has already been used
3500
- // - transaction: the transaction attempted
3501
- ErrorCode["NONCE_EXPIRED"] = "NONCE_EXPIRED";
3502
- // The replacement fee for the transaction is too low
3503
- // - transaction: the transaction attempted
3504
- ErrorCode["REPLACEMENT_UNDERPRICED"] = "REPLACEMENT_UNDERPRICED";
3505
- // The gas limit could not be estimated
3506
- // - transaction: the transaction passed to estimateGas
3507
- ErrorCode["UNPREDICTABLE_GAS_LIMIT"] = "UNPREDICTABLE_GAS_LIMIT";
3508
- // The transaction was replaced by one with a higher gas price
3509
- // - reason: "cancelled", "replaced" or "repriced"
3510
- // - cancelled: true if reason == "cancelled" or reason == "replaced")
3511
- // - hash: original transaction hash
3512
- // - replacement: the full TransactionsResponse for the replacement
3513
- // - receipt: the receipt of the replacement
3514
- ErrorCode["TRANSACTION_REPLACED"] = "TRANSACTION_REPLACED";
3515
- ///////////////////
3516
- // Interaction Errors
3517
- // The user rejected the action, such as signing a message or sending
3518
- // a transaction
3519
- ErrorCode["ACTION_REJECTED"] = "ACTION_REJECTED";
3520
- })(ErrorCode || (ErrorCode = {}));
3521
- const HEX = "0123456789abcdef";
3522
- class Logger {
3523
- constructor(version) {
3524
- Object.defineProperty(this, "version", {
3525
- enumerable: true,
3526
- value: version,
3527
- writable: false
3528
- });
3529
- }
3530
- _log(logLevel, args) {
3531
- const level = logLevel.toLowerCase();
3532
- if (LogLevels[level] == null) {
3533
- this.throwArgumentError("invalid log level name", "logLevel", logLevel);
3534
- }
3535
- if (_logLevel > LogLevels[level]) {
3536
- return;
3537
- }
3538
- console.log.apply(console, args);
3539
- }
3540
- debug(...args) {
3541
- this._log(Logger.levels.DEBUG, args);
3542
- }
3543
- info(...args) {
3544
- this._log(Logger.levels.INFO, args);
3545
- }
3546
- warn(...args) {
3547
- this._log(Logger.levels.WARNING, args);
3548
- }
3549
- makeError(message, code, params) {
3550
- // Errors are being censored
3551
- if (_censorErrors) {
3552
- return this.makeError("censored error", code, {});
3553
- }
3554
- if (!code) {
3555
- code = Logger.errors.UNKNOWN_ERROR;
3556
- }
3557
- if (!params) {
3558
- params = {};
3559
- }
3560
- const messageDetails = [];
3561
- Object.keys(params).forEach((key) => {
3562
- const value = params[key];
3563
- try {
3564
- if (value instanceof Uint8Array) {
3565
- let hex = "";
3566
- for (let i = 0; i < value.length; i++) {
3567
- hex += HEX[value[i] >> 4];
3568
- hex += HEX[value[i] & 0x0f];
3569
- }
3570
- messageDetails.push(key + "=Uint8Array(0x" + hex + ")");
3571
- }
3572
- else {
3573
- messageDetails.push(key + "=" + JSON.stringify(value));
3574
- }
3575
- }
3576
- catch (error) {
3577
- messageDetails.push(key + "=" + JSON.stringify(params[key].toString()));
3578
- }
3579
- });
3580
- messageDetails.push(`code=${code}`);
3581
- messageDetails.push(`version=${this.version}`);
3582
- const reason = message;
3583
- let url = "";
3584
- switch (code) {
3585
- case ErrorCode.NUMERIC_FAULT: {
3586
- url = "NUMERIC_FAULT";
3587
- const fault = message;
3588
- switch (fault) {
3589
- case "overflow":
3590
- case "underflow":
3591
- case "division-by-zero":
3592
- url += "-" + fault;
3593
- break;
3594
- case "negative-power":
3595
- case "negative-width":
3596
- url += "-unsupported";
3597
- break;
3598
- case "unbound-bitwise-result":
3599
- url += "-unbound-result";
3600
- break;
3601
- }
3602
- break;
3603
- }
3604
- case ErrorCode.CALL_EXCEPTION:
3605
- case ErrorCode.INSUFFICIENT_FUNDS:
3606
- case ErrorCode.MISSING_NEW:
3607
- case ErrorCode.NONCE_EXPIRED:
3608
- case ErrorCode.REPLACEMENT_UNDERPRICED:
3609
- case ErrorCode.TRANSACTION_REPLACED:
3610
- case ErrorCode.UNPREDICTABLE_GAS_LIMIT:
3611
- url = code;
3612
- break;
3613
- }
3614
- if (url) {
3615
- message += " [ See: https:/\/links.ethers.org/v5-errors-" + url + " ]";
3616
- }
3617
- if (messageDetails.length) {
3618
- message += " (" + messageDetails.join(", ") + ")";
3619
- }
3620
- // @TODO: Any??
3621
- const error = new Error(message);
3622
- error.reason = reason;
3623
- error.code = code;
3624
- Object.keys(params).forEach(function (key) {
3625
- error[key] = params[key];
3626
- });
3627
- return error;
3628
- }
3629
- throwError(message, code, params) {
3630
- throw this.makeError(message, code, params);
3631
- }
3632
- throwArgumentError(message, name, value) {
3633
- return this.throwError(message, Logger.errors.INVALID_ARGUMENT, {
3634
- argument: name,
3635
- value: value
3636
- });
3637
- }
3638
- assert(condition, message, code, params) {
3639
- if (!!condition) {
3640
- return;
3641
- }
3642
- this.throwError(message, code, params);
3643
- }
3644
- assertArgument(condition, message, name, value) {
3645
- if (!!condition) {
3646
- return;
3647
- }
3648
- this.throwArgumentError(message, name, value);
3649
- }
3650
- checkNormalize(message) {
3651
- if (_normalizeError) {
3652
- this.throwError("platform missing String.prototype.normalize", Logger.errors.UNSUPPORTED_OPERATION, {
3653
- operation: "String.prototype.normalize", form: _normalizeError
3654
- });
3655
- }
3656
- }
3657
- checkSafeUint53(value, message) {
3658
- if (typeof (value) !== "number") {
3659
- return;
3660
- }
3661
- if (message == null) {
3662
- message = "value not safe";
3663
- }
3664
- if (value < 0 || value >= 0x1fffffffffffff) {
3665
- this.throwError(message, Logger.errors.NUMERIC_FAULT, {
3666
- operation: "checkSafeInteger",
3667
- fault: "out-of-safe-range",
3668
- value: value
3669
- });
3670
- }
3671
- if (value % 1) {
3672
- this.throwError(message, Logger.errors.NUMERIC_FAULT, {
3673
- operation: "checkSafeInteger",
3674
- fault: "non-integer",
3675
- value: value
3676
- });
3677
- }
3678
- }
3679
- checkArgumentCount(count, expectedCount, message) {
3680
- if (message) {
3681
- message = ": " + message;
3682
- }
3683
- else {
3684
- message = "";
3685
- }
3686
- if (count < expectedCount) {
3687
- this.throwError("missing argument" + message, Logger.errors.MISSING_ARGUMENT, {
3688
- count: count,
3689
- expectedCount: expectedCount
3690
- });
3691
- }
3692
- if (count > expectedCount) {
3693
- this.throwError("too many arguments" + message, Logger.errors.UNEXPECTED_ARGUMENT, {
3694
- count: count,
3695
- expectedCount: expectedCount
3696
- });
3697
- }
3698
- }
3699
- checkNew(target, kind) {
3700
- if (target === Object || target == null) {
3701
- this.throwError("missing new", Logger.errors.MISSING_NEW, { name: kind.name });
3702
- }
3703
- }
3704
- checkAbstract(target, kind) {
3705
- if (target === kind) {
3706
- this.throwError("cannot instantiate abstract class " + JSON.stringify(kind.name) + " directly; use a sub-class", Logger.errors.UNSUPPORTED_OPERATION, { name: target.name, operation: "new" });
3707
- }
3708
- else if (target === Object || target == null) {
3709
- this.throwError("missing new", Logger.errors.MISSING_NEW, { name: kind.name });
3710
- }
3711
- }
3712
- static globalLogger() {
3713
- if (!_globalLogger) {
3714
- _globalLogger = new Logger(version$2);
3715
- }
3716
- return _globalLogger;
3717
- }
3718
- static setCensorship(censorship, permanent) {
3719
- if (!censorship && permanent) {
3720
- this.globalLogger().throwError("cannot permanently disable censorship", Logger.errors.UNSUPPORTED_OPERATION, {
3721
- operation: "setCensorship"
3722
- });
3723
- }
3724
- if (_permanentCensorErrors) {
3725
- if (!censorship) {
3726
- return;
3727
- }
3728
- this.globalLogger().throwError("error censorship permanent", Logger.errors.UNSUPPORTED_OPERATION, {
3729
- operation: "setCensorship"
3730
- });
3731
- }
3732
- _censorErrors = !!censorship;
3733
- _permanentCensorErrors = !!permanent;
3734
- }
3735
- static setLogLevel(logLevel) {
3736
- const level = LogLevels[logLevel.toLowerCase()];
3737
- if (level == null) {
3738
- Logger.globalLogger().warn("invalid log level - " + logLevel);
3739
- return;
3740
- }
3741
- _logLevel = level;
3742
- }
3743
- static from(version) {
3744
- return new Logger(version);
3745
- }
3746
- }
3747
- Logger.errors = ErrorCode;
3748
- Logger.levels = LogLevel;
3749
-
3750
- const version$1 = "bytes/5.7.0";
3751
-
3752
- const logger$1 = new Logger(version$1);
3753
- ///////////////////////////////
3754
- function isHexable(value) {
3755
- return !!(value.toHexString);
3756
- }
3757
- function addSlice(array) {
3758
- if (array.slice) {
3759
- return array;
3760
- }
3761
- array.slice = function () {
3762
- const args = Array.prototype.slice.call(arguments);
3763
- return addSlice(new Uint8Array(Array.prototype.slice.apply(array, args)));
3764
- };
3765
- return array;
3766
- }
3767
- function isInteger(value) {
3768
- return (typeof (value) === "number" && value == value && (value % 1) === 0);
3769
- }
3770
- function isBytes(value) {
3771
- if (value == null) {
3772
- return false;
3773
- }
3774
- if (value.constructor === Uint8Array) {
3775
- return true;
3776
- }
3777
- if (typeof (value) === "string") {
3778
- return false;
3779
- }
3780
- if (!isInteger(value.length) || value.length < 0) {
3781
- return false;
3782
- }
3783
- for (let i = 0; i < value.length; i++) {
3784
- const v = value[i];
3785
- if (!isInteger(v) || v < 0 || v >= 256) {
3786
- return false;
3787
- }
3788
- }
3789
- return true;
3790
- }
3791
- function arrayify(value, options) {
3792
- if (!options) {
3793
- options = {};
3794
- }
3795
- if (typeof (value) === "number") {
3796
- logger$1.checkSafeUint53(value, "invalid arrayify value");
3797
- const result = [];
3798
- while (value) {
3799
- result.unshift(value & 0xff);
3800
- value = parseInt(String(value / 256));
3801
- }
3802
- if (result.length === 0) {
3803
- result.push(0);
3804
- }
3805
- return addSlice(new Uint8Array(result));
3806
- }
3807
- if (options.allowMissingPrefix && typeof (value) === "string" && value.substring(0, 2) !== "0x") {
3808
- value = "0x" + value;
3809
- }
3810
- if (isHexable(value)) {
3811
- value = value.toHexString();
3812
- }
3813
- if (isHexString(value)) {
3814
- let hex = value.substring(2);
3815
- if (hex.length % 2) {
3816
- if (options.hexPad === "left") {
3817
- hex = "0" + hex;
3818
- }
3819
- else if (options.hexPad === "right") {
3820
- hex += "0";
3821
- }
3822
- else {
3823
- logger$1.throwArgumentError("hex data is odd-length", "value", value);
3824
- }
3825
- }
3826
- const result = [];
3827
- for (let i = 0; i < hex.length; i += 2) {
3828
- result.push(parseInt(hex.substring(i, i + 2), 16));
3829
- }
3830
- return addSlice(new Uint8Array(result));
3831
- }
3832
- if (isBytes(value)) {
3833
- return addSlice(new Uint8Array(value));
3834
- }
3835
- return logger$1.throwArgumentError("invalid arrayify value", "value", value);
3836
- }
3837
- function isHexString(value, length) {
3838
- if (typeof (value) !== "string" || !value.match(/^0x[0-9A-Fa-f]*$/)) {
3839
- return false;
3840
- }
3841
- return true;
3842
- }
3843
- const HexCharacters = "0123456789abcdef";
3844
- function hexlify(value, options) {
3845
- if (!options) {
3846
- options = {};
3847
- }
3848
- if (typeof (value) === "number") {
3849
- logger$1.checkSafeUint53(value, "invalid hexlify value");
3850
- let hex = "";
3851
- while (value) {
3852
- hex = HexCharacters[value & 0xf] + hex;
3853
- value = Math.floor(value / 16);
3854
- }
3855
- if (hex.length) {
3856
- if (hex.length % 2) {
3857
- hex = "0" + hex;
3858
- }
3859
- return "0x" + hex;
3860
- }
3861
- return "0x00";
3862
- }
3863
- if (typeof (value) === "bigint") {
3864
- value = value.toString(16);
3865
- if (value.length % 2) {
3866
- return ("0x0" + value);
3867
- }
3868
- return "0x" + value;
3869
- }
3870
- if (options.allowMissingPrefix && typeof (value) === "string" && value.substring(0, 2) !== "0x") {
3871
- value = "0x" + value;
3872
- }
3873
- if (isHexable(value)) {
3874
- return value.toHexString();
3875
- }
3876
- if (isHexString(value)) {
3877
- if (value.length % 2) {
3878
- if (options.hexPad === "left") {
3879
- value = "0x0" + value.substring(2);
3880
- }
3881
- else if (options.hexPad === "right") {
3882
- value += "0";
3883
- }
3884
- else {
3885
- logger$1.throwArgumentError("hex data is odd-length", "value", value);
3886
- }
3887
- }
3888
- return value.toLowerCase();
3889
- }
3890
- if (isBytes(value)) {
3891
- let result = "0x";
3892
- for (let i = 0; i < value.length; i++) {
3893
- let v = value[i];
3894
- result += HexCharacters[(v & 0xf0) >> 4] + HexCharacters[v & 0x0f];
3895
- }
3896
- return result;
3897
- }
3898
- return logger$1.throwArgumentError("invalid hexlify value", "value", value);
3899
- }
3900
- function hexZeroPad(value, length) {
3901
- if (typeof (value) !== "string") {
3902
- value = hexlify(value);
3903
- }
3904
- else if (!isHexString(value)) {
3905
- logger$1.throwArgumentError("invalid hex string", "value", value);
3906
- }
3907
- if (value.length > 2 * length + 2) {
3908
- logger$1.throwArgumentError("value out of range", "value", arguments[1]);
3909
- }
3910
- while (value.length < 2 * length + 2) {
3911
- value = "0x0" + value.substring(2);
3912
- }
3913
- return value;
3914
- }
3915
-
3916
- const version = "bignumber/5.7.0";
3917
-
3918
- var BN = _BN.BN;
3919
- const logger = new Logger(version);
3920
- const _constructorGuard = {};
3921
- const MAX_SAFE = 0x1fffffffffffff;
3922
- function isBigNumberish(value) {
3923
- return (value != null) && (BigNumber.isBigNumber(value) ||
3924
- (typeof (value) === "number" && (value % 1) === 0) ||
3925
- (typeof (value) === "string" && !!value.match(/^-?[0-9]+$/)) ||
3926
- isHexString(value) ||
3927
- (typeof (value) === "bigint") ||
3928
- isBytes(value));
3929
- }
3930
- // Only warn about passing 10 into radix once
3931
- let _warnedToStringRadix = false;
3932
- class BigNumber {
3933
- constructor(constructorGuard, hex) {
3934
- if (constructorGuard !== _constructorGuard) {
3935
- logger.throwError("cannot call constructor directly; use BigNumber.from", Logger.errors.UNSUPPORTED_OPERATION, {
3936
- operation: "new (BigNumber)"
3937
- });
3938
- }
3939
- this._hex = hex;
3940
- this._isBigNumber = true;
3941
- Object.freeze(this);
3942
- }
3943
- fromTwos(value) {
3944
- return toBigNumber(toBN(this).fromTwos(value));
3945
- }
3946
- toTwos(value) {
3947
- return toBigNumber(toBN(this).toTwos(value));
3948
- }
3949
- abs() {
3950
- if (this._hex[0] === "-") {
3951
- return BigNumber.from(this._hex.substring(1));
3952
- }
3953
- return this;
3954
- }
3955
- add(other) {
3956
- return toBigNumber(toBN(this).add(toBN(other)));
3957
- }
3958
- sub(other) {
3959
- return toBigNumber(toBN(this).sub(toBN(other)));
3960
- }
3961
- div(other) {
3962
- const o = BigNumber.from(other);
3963
- if (o.isZero()) {
3964
- throwFault("division-by-zero", "div");
3965
- }
3966
- return toBigNumber(toBN(this).div(toBN(other)));
3967
- }
3968
- mul(other) {
3969
- return toBigNumber(toBN(this).mul(toBN(other)));
3970
- }
3971
- mod(other) {
3972
- const value = toBN(other);
3973
- if (value.isNeg()) {
3974
- throwFault("division-by-zero", "mod");
3975
- }
3976
- return toBigNumber(toBN(this).umod(value));
3977
- }
3978
- pow(other) {
3979
- const value = toBN(other);
3980
- if (value.isNeg()) {
3981
- throwFault("negative-power", "pow");
3982
- }
3983
- return toBigNumber(toBN(this).pow(value));
3984
- }
3985
- and(other) {
3986
- const value = toBN(other);
3987
- if (this.isNegative() || value.isNeg()) {
3988
- throwFault("unbound-bitwise-result", "and");
3989
- }
3990
- return toBigNumber(toBN(this).and(value));
3991
- }
3992
- or(other) {
3993
- const value = toBN(other);
3994
- if (this.isNegative() || value.isNeg()) {
3995
- throwFault("unbound-bitwise-result", "or");
3996
- }
3997
- return toBigNumber(toBN(this).or(value));
3998
- }
3999
- xor(other) {
4000
- const value = toBN(other);
4001
- if (this.isNegative() || value.isNeg()) {
4002
- throwFault("unbound-bitwise-result", "xor");
4003
- }
4004
- return toBigNumber(toBN(this).xor(value));
4005
- }
4006
- mask(value) {
4007
- if (this.isNegative() || value < 0) {
4008
- throwFault("negative-width", "mask");
4009
- }
4010
- return toBigNumber(toBN(this).maskn(value));
4011
- }
4012
- shl(value) {
4013
- if (this.isNegative() || value < 0) {
4014
- throwFault("negative-width", "shl");
4015
- }
4016
- return toBigNumber(toBN(this).shln(value));
4017
- }
4018
- shr(value) {
4019
- if (this.isNegative() || value < 0) {
4020
- throwFault("negative-width", "shr");
4021
- }
4022
- return toBigNumber(toBN(this).shrn(value));
4023
- }
4024
- eq(other) {
4025
- return toBN(this).eq(toBN(other));
4026
- }
4027
- lt(other) {
4028
- return toBN(this).lt(toBN(other));
4029
- }
4030
- lte(other) {
4031
- return toBN(this).lte(toBN(other));
4032
- }
4033
- gt(other) {
4034
- return toBN(this).gt(toBN(other));
4035
- }
4036
- gte(other) {
4037
- return toBN(this).gte(toBN(other));
4038
- }
4039
- isNegative() {
4040
- return (this._hex[0] === "-");
4041
- }
4042
- isZero() {
4043
- return toBN(this).isZero();
4044
- }
4045
- toNumber() {
4046
- try {
4047
- return toBN(this).toNumber();
4048
- }
4049
- catch (error) {
4050
- throwFault("overflow", "toNumber", this.toString());
4051
- }
4052
- return null;
4053
- }
4054
- toBigInt() {
4055
- try {
4056
- return BigInt(this.toString());
4057
- }
4058
- catch (e) { }
4059
- return logger.throwError("this platform does not support BigInt", Logger.errors.UNSUPPORTED_OPERATION, {
4060
- value: this.toString()
4061
- });
4062
- }
4063
- toString() {
4064
- // Lots of people expect this, which we do not support, so check (See: #889)
4065
- if (arguments.length > 0) {
4066
- if (arguments[0] === 10) {
4067
- if (!_warnedToStringRadix) {
4068
- _warnedToStringRadix = true;
4069
- logger.warn("BigNumber.toString does not accept any parameters; base-10 is assumed");
4070
- }
4071
- }
4072
- else if (arguments[0] === 16) {
4073
- logger.throwError("BigNumber.toString does not accept any parameters; use bigNumber.toHexString()", Logger.errors.UNEXPECTED_ARGUMENT, {});
4074
- }
4075
- else {
4076
- logger.throwError("BigNumber.toString does not accept parameters", Logger.errors.UNEXPECTED_ARGUMENT, {});
4077
- }
4078
- }
4079
- return toBN(this).toString(10);
4080
- }
4081
- toHexString() {
4082
- return this._hex;
4083
- }
4084
- toJSON(key) {
4085
- return { type: "BigNumber", hex: this.toHexString() };
4086
- }
4087
- static from(value) {
4088
- if (value instanceof BigNumber) {
4089
- return value;
4090
- }
4091
- if (typeof (value) === "string") {
4092
- if (value.match(/^-?0x[0-9a-f]+$/i)) {
4093
- return new BigNumber(_constructorGuard, toHex$1(value));
4094
- }
4095
- if (value.match(/^-?[0-9]+$/)) {
4096
- return new BigNumber(_constructorGuard, toHex$1(new BN(value)));
4097
- }
4098
- return logger.throwArgumentError("invalid BigNumber string", "value", value);
4099
- }
4100
- if (typeof (value) === "number") {
4101
- if (value % 1) {
4102
- throwFault("underflow", "BigNumber.from", value);
4103
- }
4104
- if (value >= MAX_SAFE || value <= -MAX_SAFE) {
4105
- throwFault("overflow", "BigNumber.from", value);
4106
- }
4107
- return BigNumber.from(String(value));
4108
- }
4109
- const anyValue = value;
4110
- if (typeof (anyValue) === "bigint") {
4111
- return BigNumber.from(anyValue.toString());
4112
- }
4113
- if (isBytes(anyValue)) {
4114
- return BigNumber.from(hexlify(anyValue));
4115
- }
4116
- if (anyValue) {
4117
- // Hexable interface (takes priority)
4118
- if (anyValue.toHexString) {
4119
- const hex = anyValue.toHexString();
4120
- if (typeof (hex) === "string") {
4121
- return BigNumber.from(hex);
4122
- }
4123
- }
4124
- else {
4125
- // For now, handle legacy JSON-ified values (goes away in v6)
4126
- let hex = anyValue._hex;
4127
- // New-form JSON
4128
- if (hex == null && anyValue.type === "BigNumber") {
4129
- hex = anyValue.hex;
4130
- }
4131
- if (typeof (hex) === "string") {
4132
- if (isHexString(hex) || (hex[0] === "-" && isHexString(hex.substring(1)))) {
4133
- return BigNumber.from(hex);
4134
- }
4135
- }
4136
- }
4137
- }
4138
- return logger.throwArgumentError("invalid BigNumber value", "value", value);
4139
- }
4140
- static isBigNumber(value) {
4141
- return !!(value && value._isBigNumber);
4142
- }
4143
- }
4144
- // Normalize the hex string
4145
- function toHex$1(value) {
4146
- // For BN, call on the hex string
4147
- if (typeof (value) !== "string") {
4148
- return toHex$1(value.toString(16));
4149
- }
4150
- // If negative, prepend the negative sign to the normalized positive value
4151
- if (value[0] === "-") {
4152
- // Strip off the negative sign
4153
- value = value.substring(1);
4154
- // Cannot have multiple negative signs (e.g. "--0x04")
4155
- if (value[0] === "-") {
4156
- logger.throwArgumentError("invalid hex", "value", value);
4157
- }
4158
- // Call toHex on the positive component
4159
- value = toHex$1(value);
4160
- // Do not allow "-0x00"
4161
- if (value === "0x00") {
4162
- return value;
4163
- }
4164
- // Negate the value
4165
- return "-" + value;
4166
- }
4167
- // Add a "0x" prefix if missing
4168
- if (value.substring(0, 2) !== "0x") {
4169
- value = "0x" + value;
4170
- }
4171
- // Normalize zero
4172
- if (value === "0x") {
4173
- return "0x00";
4174
- }
4175
- // Make the string even length
4176
- if (value.length % 2) {
4177
- value = "0x0" + value.substring(2);
4178
- }
4179
- // Trim to smallest even-length string
4180
- while (value.length > 4 && value.substring(0, 4) === "0x00") {
4181
- value = "0x" + value.substring(4);
4182
- }
4183
- return value;
4184
- }
4185
- function toBigNumber(value) {
4186
- return BigNumber.from(toHex$1(value));
4187
- }
4188
- function toBN(value) {
4189
- const hex = BigNumber.from(value).toHexString();
4190
- if (hex[0] === "-") {
4191
- return (new BN("-" + hex.substring(3), 16));
4192
- }
4193
- return new BN(hex.substring(2), 16);
4194
- }
4195
- function throwFault(fault, operation, value) {
4196
- const params = { fault: fault, operation: operation };
4197
- if (value != null) {
4198
- params.value = value;
4199
- }
4200
- return logger.throwError(fault, Logger.errors.NUMERIC_FAULT, params);
4201
- }
4202
-
4203
1
  // base-x encoding / decoding
4204
2
  // Copyright (c) 2018 base-x contributors
4205
3
  // Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
@@ -11666,14 +7464,14 @@ const isString = (type) => type === 'string';
11666
7464
  const isNumber = (type) => type === 'number';
11667
7465
  const isBoolean = (type) => type === 'boolean';
11668
7466
  const isUint8Array = (type) => type === 'uint8Array';
11669
- const isBigNumber = (type) => type === 'bigNumber';
7467
+ const isBigInt = (type) => type === 'bigint';
11670
7468
  const tokenize = (key, value) => {
11671
7469
  const optional = key.endsWith('?');
11672
7470
  let type = value === undefined ? key : value;
11673
7471
  if (type instanceof Uint8Array)
11674
7472
  type = 'uint8Array';
11675
- else if (type instanceof BigNumber)
11676
- type = 'bigNumber';
7473
+ else if (type instanceof BigInt)
7474
+ type = 'bigint';
11677
7475
  else
11678
7476
  type = Array.isArray(type) ? 'array' : typeof type;
11679
7477
  const parts = key.split('?');
@@ -11684,19 +7482,19 @@ const toType = (data) => {
11684
7482
  // always return uint8Arrays as they are
11685
7483
  if (data instanceof Uint8Array)
11686
7484
  return data;
11687
- // returns the ArrayBuffer as a UintArray
7485
+ // returns the ArrayBuffer as UintArray
11688
7486
  if (data instanceof ArrayBuffer)
11689
7487
  return new Uint8Array(data);
11690
- // returns the bigNumbers hex as a UintArray
11691
- if (data instanceof BigNumber)
11692
- return new TextEncoder().encode(data._hex || data.toHexString());
11693
- // returns the string as a UintArray
7488
+ // returns the BigTnt string as UintArray
7489
+ if (typeof data === 'bigint')
7490
+ return new TextEncoder().encode(data.toString());
7491
+ // returns the string as UintArray
11694
7492
  if (typeof data === 'string')
11695
7493
  return new TextEncoder().encode(data);
11696
- // returns the object as a UintArray
7494
+ // returns the object as UintArray
11697
7495
  if (typeof data === 'object')
11698
7496
  return new TextEncoder().encode(JSON.stringify(data));
11699
- // returns the number as a UintArray
7497
+ // returns the number as UintArray
11700
7498
  if (typeof data === 'number' || typeof data === 'boolean')
11701
7499
  return new TextEncoder().encode(data.toString());
11702
7500
  throw new Error(`unsuported type ${typeof data || data}`);
@@ -11738,8 +7536,8 @@ const decode = (proto, uint8Array, compressed) => {
11738
7536
  output[token.key] = new TextDecoder().decode(deconcated[i]) === 'true';
11739
7537
  else if (isNumber(token.type))
11740
7538
  output[token.key] = Number(new TextDecoder().decode(deconcated[i]));
11741
- else if (isBigNumber(token.type))
11742
- output[token.key] = BigNumber.from(new TextDecoder().decode(deconcated[i]));
7539
+ else if (isBigInt(token.type))
7540
+ output[token.key] = BigInt(new TextDecoder().decode(deconcated[i]));
11743
7541
  else if (isJson(token.type))
11744
7542
  output[token.key] = JSON.parse(new TextDecoder().decode(deconcated[i]));
11745
7543
  if (token.optional) {
@@ -12450,7 +8248,7 @@ const FormatInterface = FormatInterface$1;
12450
8248
 
12451
8249
  var proto$6 = {
12452
8250
  address: String(),
12453
- reward: BigNumber.from(0)
8251
+ reward: BigInt(0)
12454
8252
  };
12455
8253
 
12456
8254
  class ValidatorMessage extends FormatInterface {
@@ -12469,8 +8267,8 @@ var proto$5 = {
12469
8267
  index: Number(),
12470
8268
  previousHash: String(),
12471
8269
  timestamp: Number(),
12472
- reward: BigNumber.from(0),
12473
- fees: BigNumber.from(0),
8270
+ reward: BigInt(0),
8271
+ fees: BigInt(0),
12474
8272
  transactions: Array(),
12475
8273
  validators: new Uint8Array()
12476
8274
  };
@@ -12608,4 +8406,4 @@ class RawTransactionMessage extends FormatInterface {
12608
8406
  }
12609
8407
  }
12610
8408
 
12611
- export { BigNumber as B, ContractMessage as C, FormatInterface as F, Logger as L, RawTransactionMessage as R, TransactionMessage as T, ValidatorMessage as V, arrayify as a, isBytes as b, BlockMessage as c, BWMessage as d, BWRequestMessage as e, getDefaultExportFromCjs as g, hexZeroPad as h, isBigNumberish as i, toBase58 as t, version as v };
8409
+ export { BlockMessage as B, ContractMessage as C, FormatInterface as F, RawTransactionMessage as R, TransactionMessage as T, ValidatorMessage as V, BWMessage as a, BWRequestMessage as b, toBase58 as t };