@leofcoin/chain 1.7.70 → 1.7.72

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