@leofcoin/chain 1.7.50 → 1.7.52

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