UNPKG

167 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3 typeof define === 'function' && define.amd ? define(['exports'], factory) :
4 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.polkadotUtil = {}));
5})(this, (function (exports) { 'use strict';
6
7 const global = typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : window;
8
9 var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
10 const packageInfo$3 = { name: '@polkadot/util', path: (({ url: (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle-polkadot-util.js', document.baseURI).href)) }) && (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle-polkadot-util.js', document.baseURI).href))) ? new URL((typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle-polkadot-util.js', document.baseURI).href))).pathname.substring(0, new URL((typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle-polkadot-util.js', document.baseURI).href))).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '12.5.1' };
11
12 function arrayChunk(array, chunkSize) {
13 const outputSize = Math.ceil(array.length / chunkSize);
14 if (outputSize === 1) {
15 return [array];
16 }
17 const output = Array(outputSize);
18 for (let i = 0; i < outputSize; i++) {
19 const offset = i * chunkSize;
20 output[i] = array.slice(offset, offset + chunkSize);
21 }
22 return output;
23 }
24
25 function arrayFilter(array, allowNulls = true) {
26 return array.filter((v) => v !== undefined &&
27 (allowNulls || v !== null));
28 }
29
30 function arrayFlatten(arrays) {
31 const num = arrays.length;
32 if (num === 0) {
33 return [];
34 }
35 else if (num === 1) {
36 return arrays[0];
37 }
38 let size = 0;
39 for (let i = 0; i < num; i++) {
40 size += arrays[i].length;
41 }
42 const output = new Array(size);
43 let i = -1;
44 for (let j = 0; j < num; j++) {
45 const a = arrays[j];
46 for (let e = 0, count = a.length; e < count; e++) {
47 output[++i] = a[e];
48 }
49 }
50 return output;
51 }
52
53 function arrayRange(size, startAt = 0) {
54 if (size <= 0) {
55 throw new Error('Expected non-zero, positive number as a range size');
56 }
57 const result = new Array(size);
58 for (let i = 0; i < size; i++) {
59 result[i] = i + startAt;
60 }
61 return result;
62 }
63
64 function arrayShuffle(input) {
65 const result = input.slice();
66 let curr = result.length;
67 if (curr === 1) {
68 return result;
69 }
70 while (curr !== 0) {
71 const rand = ~~(Math.random() * curr);
72 curr--;
73 [result[curr], result[rand]] = [result[rand], result[curr]];
74 }
75 return result;
76 }
77
78 function arrayUnzip(entries) {
79 const count = entries.length;
80 const keys = new Array(count);
81 const values = new Array(count);
82 for (let i = 0; i < count; i++) {
83 [keys[i], values[i]] = entries[i];
84 }
85 return [keys, values];
86 }
87
88 function arrayZip(keys, values) {
89 const count = keys.length;
90 const result = new Array(count);
91 for (let i = 0; i < count; i++) {
92 result[i] = [keys[i], values[i]];
93 }
94 return result;
95 }
96
97 function isFunction(value) {
98 return typeof value === 'function';
99 }
100
101 function assert(condition, message) {
102 if (!condition) {
103 throw new Error(isFunction(message)
104 ? message()
105 : message);
106 }
107 }
108 function assertReturn(value, message) {
109 assert(value !== undefined && value !== null, message);
110 return value;
111 }
112 function assertUnreachable(x) {
113 throw new Error(`This codepath should be unreachable. Unhandled input: ${x}`);
114 }
115
116 function createCmp(cmp) {
117 return (...items) => {
118 const count = items.length;
119 if (count === 0) {
120 throw new Error('Must provide one or more arguments');
121 }
122 let result = items[0];
123 for (let i = 1; i < count; i++) {
124 if (cmp(items[i], result)) {
125 result = items[i];
126 }
127 }
128 return result;
129 };
130 }
131
132 const nMax = createCmp((a, b) => a > b);
133 const nMin = createCmp((a, b) => a < b);
134
135 const packageInfo$2 = { name: '@polkadot/x-global', path: (({ url: (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle-polkadot-util.js', document.baseURI).href)) }) && (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle-polkadot-util.js', document.baseURI).href))) ? new URL((typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle-polkadot-util.js', document.baseURI).href))).pathname.substring(0, new URL((typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle-polkadot-util.js', document.baseURI).href))).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '12.5.1' };
136
137 function evaluateThis(fn) {
138 return fn('return this');
139 }
140 const xglobal = (typeof globalThis !== 'undefined'
141 ? globalThis
142 : typeof global !== 'undefined'
143 ? global
144 : typeof self !== 'undefined'
145 ? self
146 : typeof window !== 'undefined'
147 ? window
148 : evaluateThis(Function));
149 function extractGlobal(name, fallback) {
150 return typeof xglobal[name] === 'undefined'
151 ? fallback
152 : xglobal[name];
153 }
154 function exposeGlobal(name, fallback) {
155 if (typeof xglobal[name] === 'undefined') {
156 xglobal[name] = fallback;
157 }
158 }
159
160 const build = /*#__PURE__*/Object.freeze({
161 __proto__: null,
162 exposeGlobal: exposeGlobal,
163 extractGlobal: extractGlobal,
164 packageInfo: packageInfo$2,
165 xglobal: xglobal
166 });
167
168 function invalidFallback() {
169 return Number.NaN;
170 }
171 const BigInt = extractGlobal('BigInt', invalidFallback);
172
173 const _0n = BigInt(0);
174 const _1n = BigInt(1);
175 const _2n = BigInt(2);
176 const _3n = BigInt(3);
177 const _4n = BigInt(4);
178 const _5n = BigInt(5);
179 const _6n = BigInt(6);
180 const _7n = BigInt(7);
181 const _8n = BigInt(8);
182 const _9n = BigInt(9);
183 const _10n = BigInt(10);
184 const _100n = BigInt(100);
185 const _1000n = BigInt(1000);
186 const _1Mn = BigInt(1000000);
187 const _1Bn = BigInt(1000000000);
188 const _1Qn = _1Bn * _1Bn;
189 const _2pow53n = BigInt(Number.MAX_SAFE_INTEGER);
190 const _sqrt2pow53n = BigInt(94906265);
191
192 const U8_MAX = BigInt(256);
193 const U16_MAX = BigInt(256 * 256);
194 const U64_MAX = BigInt('0x10000000000000000');
195 function u8aToBigInt(value, { isLe = true, isNegative = false } = {}) {
196 if (!isLe) {
197 value = value.slice().reverse();
198 }
199 const count = value.length;
200 if (isNegative && count && (value[count - 1] & 0x80)) {
201 switch (count) {
202 case 0:
203 return BigInt(0);
204 case 1:
205 return BigInt(((value[0] ^ 255) * -1) - 1);
206 case 2:
207 return BigInt((((value[0] + (value[1] << 8)) ^ 65535) * -1) - 1);
208 case 4:
209 return BigInt((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) * -1) - 1);
210 }
211 const dvI = new DataView(value.buffer, value.byteOffset);
212 if (count === 8) {
213 return dvI.getBigInt64(0, true);
214 }
215 let result = BigInt(0);
216 const mod = count % 2;
217 for (let i = count - 2; i >= mod; i -= 2) {
218 result = (result * U16_MAX) + BigInt(dvI.getUint16(i, true) ^ 0xffff);
219 }
220 if (mod) {
221 result = (result * U8_MAX) + BigInt(value[0] ^ 0xff);
222 }
223 return (result * -_1n) - _1n;
224 }
225 switch (count) {
226 case 0:
227 return BigInt(0);
228 case 1:
229 return BigInt(value[0]);
230 case 2:
231 return BigInt(value[0] + (value[1] << 8));
232 case 4:
233 return BigInt(value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216));
234 }
235 const dvI = new DataView(value.buffer, value.byteOffset);
236 switch (count) {
237 case 8:
238 return dvI.getBigUint64(0, true);
239 case 16:
240 return (dvI.getBigUint64(8, true) * U64_MAX) + dvI.getBigUint64(0, true);
241 default: {
242 let result = BigInt(0);
243 const mod = count % 2;
244 for (let i = count - 2; i >= mod; i -= 2) {
245 result = (result * U16_MAX) + BigInt(dvI.getUint16(i, true));
246 }
247 if (mod) {
248 result = (result * U8_MAX) + BigInt(value[0]);
249 }
250 return result;
251 }
252 }
253 }
254
255 const CHR = '0123456789abcdef';
256 const U8$1 = new Uint8Array(256);
257 const U16$1 = new Uint8Array(256 * 256);
258 for (let i = 0, count = CHR.length; i < count; i++) {
259 U8$1[CHR[i].charCodeAt(0) | 0] = i | 0;
260 if (i > 9) {
261 U8$1[CHR[i].toUpperCase().charCodeAt(0) | 0] = i | 0;
262 }
263 }
264 for (let i = 0; i < 256; i++) {
265 const s = i << 8;
266 for (let j = 0; j < 256; j++) {
267 U16$1[s | j] = (U8$1[i] << 4) | U8$1[j];
268 }
269 }
270 function hexToU8a(value, bitLength = -1) {
271 if (!value) {
272 return new Uint8Array();
273 }
274 let s = value.startsWith('0x')
275 ? 2
276 : 0;
277 const decLength = Math.ceil((value.length - s) / 2);
278 const endLength = Math.ceil(bitLength === -1
279 ? decLength
280 : bitLength / 8);
281 const result = new Uint8Array(endLength);
282 const offset = endLength > decLength
283 ? endLength - decLength
284 : 0;
285 for (let i = offset; i < endLength; i++, s += 2) {
286 result[i] = U16$1[(value.charCodeAt(s) << 8) | value.charCodeAt(s + 1)];
287 }
288 return result;
289 }
290
291 function hexToBigInt(value, { isLe = false, isNegative = false } = {}) {
292 return !value || value === '0x'
293 ? BigInt(0)
294 : u8aToBigInt(hexToU8a(value), { isLe, isNegative });
295 }
296
297 var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
298
299 function getDefaultExportFromCjs (x) {
300 return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
301 }
302
303 function getAugmentedNamespace(n) {
304 if (n.__esModule) return n;
305 var f = n.default;
306 if (typeof f == "function") {
307 var a = function a () {
308 if (this instanceof a) {
309 return Reflect.construct(f, arguments, this.constructor);
310 }
311 return f.apply(this, arguments);
312 };
313 a.prototype = f.prototype;
314 } else a = {};
315 Object.defineProperty(a, '__esModule', {value: true});
316 Object.keys(n).forEach(function (k) {
317 var d = Object.getOwnPropertyDescriptor(n, k);
318 Object.defineProperty(a, k, d.get ? d : {
319 enumerable: true,
320 get: function () {
321 return n[k];
322 }
323 });
324 });
325 return a;
326 }
327
328 var bn = {exports: {}};
329
330 const _nodeResolve_empty = {};
331
332 const _nodeResolve_empty$1 = /*#__PURE__*/Object.freeze({
333 __proto__: null,
334 default: _nodeResolve_empty
335 });
336
337 const require$$0$1 = /*@__PURE__*/getAugmentedNamespace(_nodeResolve_empty$1);
338
339 bn.exports;
340 (function (module) {
341 (function (module, exports) {
342 function assert (val, msg) {
343 if (!val) throw new Error(msg || 'Assertion failed');
344 }
345 function inherits (ctor, superCtor) {
346 ctor.super_ = superCtor;
347 var TempCtor = function () {};
348 TempCtor.prototype = superCtor.prototype;
349 ctor.prototype = new TempCtor();
350 ctor.prototype.constructor = ctor;
351 }
352 function BN (number, base, endian) {
353 if (BN.isBN(number)) {
354 return number;
355 }
356 this.negative = 0;
357 this.words = null;
358 this.length = 0;
359 this.red = null;
360 if (number !== null) {
361 if (base === 'le' || base === 'be') {
362 endian = base;
363 base = 10;
364 }
365 this._init(number || 0, base || 10, endian || 'be');
366 }
367 }
368 if (typeof module === 'object') {
369 module.exports = BN;
370 } else {
371 exports.BN = BN;
372 }
373 BN.BN = BN;
374 BN.wordSize = 26;
375 var Buffer;
376 try {
377 if (typeof window !== 'undefined' && typeof window.Buffer !== 'undefined') {
378 Buffer = window.Buffer;
379 } else {
380 Buffer = require$$0$1.Buffer;
381 }
382 } catch (e) {
383 }
384 BN.isBN = function isBN (num) {
385 if (num instanceof BN) {
386 return true;
387 }
388 return num !== null && typeof num === 'object' &&
389 num.constructor.wordSize === BN.wordSize && Array.isArray(num.words);
390 };
391 BN.max = function max (left, right) {
392 if (left.cmp(right) > 0) return left;
393 return right;
394 };
395 BN.min = function min (left, right) {
396 if (left.cmp(right) < 0) return left;
397 return right;
398 };
399 BN.prototype._init = function init (number, base, endian) {
400 if (typeof number === 'number') {
401 return this._initNumber(number, base, endian);
402 }
403 if (typeof number === 'object') {
404 return this._initArray(number, base, endian);
405 }
406 if (base === 'hex') {
407 base = 16;
408 }
409 assert(base === (base | 0) && base >= 2 && base <= 36);
410 number = number.toString().replace(/\s+/g, '');
411 var start = 0;
412 if (number[0] === '-') {
413 start++;
414 this.negative = 1;
415 }
416 if (start < number.length) {
417 if (base === 16) {
418 this._parseHex(number, start, endian);
419 } else {
420 this._parseBase(number, base, start);
421 if (endian === 'le') {
422 this._initArray(this.toArray(), base, endian);
423 }
424 }
425 }
426 };
427 BN.prototype._initNumber = function _initNumber (number, base, endian) {
428 if (number < 0) {
429 this.negative = 1;
430 number = -number;
431 }
432 if (number < 0x4000000) {
433 this.words = [number & 0x3ffffff];
434 this.length = 1;
435 } else if (number < 0x10000000000000) {
436 this.words = [
437 number & 0x3ffffff,
438 (number / 0x4000000) & 0x3ffffff
439 ];
440 this.length = 2;
441 } else {
442 assert(number < 0x20000000000000);
443 this.words = [
444 number & 0x3ffffff,
445 (number / 0x4000000) & 0x3ffffff,
446 1
447 ];
448 this.length = 3;
449 }
450 if (endian !== 'le') return;
451 this._initArray(this.toArray(), base, endian);
452 };
453 BN.prototype._initArray = function _initArray (number, base, endian) {
454 assert(typeof number.length === 'number');
455 if (number.length <= 0) {
456 this.words = [0];
457 this.length = 1;
458 return this;
459 }
460 this.length = Math.ceil(number.length / 3);
461 this.words = new Array(this.length);
462 for (var i = 0; i < this.length; i++) {
463 this.words[i] = 0;
464 }
465 var j, w;
466 var off = 0;
467 if (endian === 'be') {
468 for (i = number.length - 1, j = 0; i >= 0; i -= 3) {
469 w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16);
470 this.words[j] |= (w << off) & 0x3ffffff;
471 this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff;
472 off += 24;
473 if (off >= 26) {
474 off -= 26;
475 j++;
476 }
477 }
478 } else if (endian === 'le') {
479 for (i = 0, j = 0; i < number.length; i += 3) {
480 w = number[i] | (number[i + 1] << 8) | (number[i + 2] << 16);
481 this.words[j] |= (w << off) & 0x3ffffff;
482 this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff;
483 off += 24;
484 if (off >= 26) {
485 off -= 26;
486 j++;
487 }
488 }
489 }
490 return this._strip();
491 };
492 function parseHex4Bits (string, index) {
493 var c = string.charCodeAt(index);
494 if (c >= 48 && c <= 57) {
495 return c - 48;
496 } else if (c >= 65 && c <= 70) {
497 return c - 55;
498 } else if (c >= 97 && c <= 102) {
499 return c - 87;
500 } else {
501 assert(false, 'Invalid character in ' + string);
502 }
503 }
504 function parseHexByte (string, lowerBound, index) {
505 var r = parseHex4Bits(string, index);
506 if (index - 1 >= lowerBound) {
507 r |= parseHex4Bits(string, index - 1) << 4;
508 }
509 return r;
510 }
511 BN.prototype._parseHex = function _parseHex (number, start, endian) {
512 this.length = Math.ceil((number.length - start) / 6);
513 this.words = new Array(this.length);
514 for (var i = 0; i < this.length; i++) {
515 this.words[i] = 0;
516 }
517 var off = 0;
518 var j = 0;
519 var w;
520 if (endian === 'be') {
521 for (i = number.length - 1; i >= start; i -= 2) {
522 w = parseHexByte(number, start, i) << off;
523 this.words[j] |= w & 0x3ffffff;
524 if (off >= 18) {
525 off -= 18;
526 j += 1;
527 this.words[j] |= w >>> 26;
528 } else {
529 off += 8;
530 }
531 }
532 } else {
533 var parseLength = number.length - start;
534 for (i = parseLength % 2 === 0 ? start + 1 : start; i < number.length; i += 2) {
535 w = parseHexByte(number, start, i) << off;
536 this.words[j] |= w & 0x3ffffff;
537 if (off >= 18) {
538 off -= 18;
539 j += 1;
540 this.words[j] |= w >>> 26;
541 } else {
542 off += 8;
543 }
544 }
545 }
546 this._strip();
547 };
548 function parseBase (str, start, end, mul) {
549 var r = 0;
550 var b = 0;
551 var len = Math.min(str.length, end);
552 for (var i = start; i < len; i++) {
553 var c = str.charCodeAt(i) - 48;
554 r *= mul;
555 if (c >= 49) {
556 b = c - 49 + 0xa;
557 } else if (c >= 17) {
558 b = c - 17 + 0xa;
559 } else {
560 b = c;
561 }
562 assert(c >= 0 && b < mul, 'Invalid character');
563 r += b;
564 }
565 return r;
566 }
567 BN.prototype._parseBase = function _parseBase (number, base, start) {
568 this.words = [0];
569 this.length = 1;
570 for (var limbLen = 0, limbPow = 1; limbPow <= 0x3ffffff; limbPow *= base) {
571 limbLen++;
572 }
573 limbLen--;
574 limbPow = (limbPow / base) | 0;
575 var total = number.length - start;
576 var mod = total % limbLen;
577 var end = Math.min(total, total - mod) + start;
578 var word = 0;
579 for (var i = start; i < end; i += limbLen) {
580 word = parseBase(number, i, i + limbLen, base);
581 this.imuln(limbPow);
582 if (this.words[0] + word < 0x4000000) {
583 this.words[0] += word;
584 } else {
585 this._iaddn(word);
586 }
587 }
588 if (mod !== 0) {
589 var pow = 1;
590 word = parseBase(number, i, number.length, base);
591 for (i = 0; i < mod; i++) {
592 pow *= base;
593 }
594 this.imuln(pow);
595 if (this.words[0] + word < 0x4000000) {
596 this.words[0] += word;
597 } else {
598 this._iaddn(word);
599 }
600 }
601 this._strip();
602 };
603 BN.prototype.copy = function copy (dest) {
604 dest.words = new Array(this.length);
605 for (var i = 0; i < this.length; i++) {
606 dest.words[i] = this.words[i];
607 }
608 dest.length = this.length;
609 dest.negative = this.negative;
610 dest.red = this.red;
611 };
612 function move (dest, src) {
613 dest.words = src.words;
614 dest.length = src.length;
615 dest.negative = src.negative;
616 dest.red = src.red;
617 }
618 BN.prototype._move = function _move (dest) {
619 move(dest, this);
620 };
621 BN.prototype.clone = function clone () {
622 var r = new BN(null);
623 this.copy(r);
624 return r;
625 };
626 BN.prototype._expand = function _expand (size) {
627 while (this.length < size) {
628 this.words[this.length++] = 0;
629 }
630 return this;
631 };
632 BN.prototype._strip = function strip () {
633 while (this.length > 1 && this.words[this.length - 1] === 0) {
634 this.length--;
635 }
636 return this._normSign();
637 };
638 BN.prototype._normSign = function _normSign () {
639 if (this.length === 1 && this.words[0] === 0) {
640 this.negative = 0;
641 }
642 return this;
643 };
644 if (typeof Symbol !== 'undefined' && typeof Symbol.for === 'function') {
645 try {
646 BN.prototype[Symbol.for('nodejs.util.inspect.custom')] = inspect;
647 } catch (e) {
648 BN.prototype.inspect = inspect;
649 }
650 } else {
651 BN.prototype.inspect = inspect;
652 }
653 function inspect () {
654 return (this.red ? '<BN-R: ' : '<BN: ') + this.toString(16) + '>';
655 }
656 var zeros = [
657 '',
658 '0',
659 '00',
660 '000',
661 '0000',
662 '00000',
663 '000000',
664 '0000000',
665 '00000000',
666 '000000000',
667 '0000000000',
668 '00000000000',
669 '000000000000',
670 '0000000000000',
671 '00000000000000',
672 '000000000000000',
673 '0000000000000000',
674 '00000000000000000',
675 '000000000000000000',
676 '0000000000000000000',
677 '00000000000000000000',
678 '000000000000000000000',
679 '0000000000000000000000',
680 '00000000000000000000000',
681 '000000000000000000000000',
682 '0000000000000000000000000'
683 ];
684 var groupSizes = [
685 0, 0,
686 25, 16, 12, 11, 10, 9, 8,
687 8, 7, 7, 7, 7, 6, 6,
688 6, 6, 6, 6, 6, 5, 5,
689 5, 5, 5, 5, 5, 5, 5,
690 5, 5, 5, 5, 5, 5, 5
691 ];
692 var groupBases = [
693 0, 0,
694 33554432, 43046721, 16777216, 48828125, 60466176, 40353607, 16777216,
695 43046721, 10000000, 19487171, 35831808, 62748517, 7529536, 11390625,
696 16777216, 24137569, 34012224, 47045881, 64000000, 4084101, 5153632,
697 6436343, 7962624, 9765625, 11881376, 14348907, 17210368, 20511149,
698 24300000, 28629151, 33554432, 39135393, 45435424, 52521875, 60466176
699 ];
700 BN.prototype.toString = function toString (base, padding) {
701 base = base || 10;
702 padding = padding | 0 || 1;
703 var out;
704 if (base === 16 || base === 'hex') {
705 out = '';
706 var off = 0;
707 var carry = 0;
708 for (var i = 0; i < this.length; i++) {
709 var w = this.words[i];
710 var word = (((w << off) | carry) & 0xffffff).toString(16);
711 carry = (w >>> (24 - off)) & 0xffffff;
712 off += 2;
713 if (off >= 26) {
714 off -= 26;
715 i--;
716 }
717 if (carry !== 0 || i !== this.length - 1) {
718 out = zeros[6 - word.length] + word + out;
719 } else {
720 out = word + out;
721 }
722 }
723 if (carry !== 0) {
724 out = carry.toString(16) + out;
725 }
726 while (out.length % padding !== 0) {
727 out = '0' + out;
728 }
729 if (this.negative !== 0) {
730 out = '-' + out;
731 }
732 return out;
733 }
734 if (base === (base | 0) && base >= 2 && base <= 36) {
735 var groupSize = groupSizes[base];
736 var groupBase = groupBases[base];
737 out = '';
738 var c = this.clone();
739 c.negative = 0;
740 while (!c.isZero()) {
741 var r = c.modrn(groupBase).toString(base);
742 c = c.idivn(groupBase);
743 if (!c.isZero()) {
744 out = zeros[groupSize - r.length] + r + out;
745 } else {
746 out = r + out;
747 }
748 }
749 if (this.isZero()) {
750 out = '0' + out;
751 }
752 while (out.length % padding !== 0) {
753 out = '0' + out;
754 }
755 if (this.negative !== 0) {
756 out = '-' + out;
757 }
758 return out;
759 }
760 assert(false, 'Base should be between 2 and 36');
761 };
762 BN.prototype.toNumber = function toNumber () {
763 var ret = this.words[0];
764 if (this.length === 2) {
765 ret += this.words[1] * 0x4000000;
766 } else if (this.length === 3 && this.words[2] === 0x01) {
767 ret += 0x10000000000000 + (this.words[1] * 0x4000000);
768 } else if (this.length > 2) {
769 assert(false, 'Number can only safely store up to 53 bits');
770 }
771 return (this.negative !== 0) ? -ret : ret;
772 };
773 BN.prototype.toJSON = function toJSON () {
774 return this.toString(16, 2);
775 };
776 if (Buffer) {
777 BN.prototype.toBuffer = function toBuffer (endian, length) {
778 return this.toArrayLike(Buffer, endian, length);
779 };
780 }
781 BN.prototype.toArray = function toArray (endian, length) {
782 return this.toArrayLike(Array, endian, length);
783 };
784 var allocate = function allocate (ArrayType, size) {
785 if (ArrayType.allocUnsafe) {
786 return ArrayType.allocUnsafe(size);
787 }
788 return new ArrayType(size);
789 };
790 BN.prototype.toArrayLike = function toArrayLike (ArrayType, endian, length) {
791 this._strip();
792 var byteLength = this.byteLength();
793 var reqLength = length || Math.max(1, byteLength);
794 assert(byteLength <= reqLength, 'byte array longer than desired length');
795 assert(reqLength > 0, 'Requested array length <= 0');
796 var res = allocate(ArrayType, reqLength);
797 var postfix = endian === 'le' ? 'LE' : 'BE';
798 this['_toArrayLike' + postfix](res, byteLength);
799 return res;
800 };
801 BN.prototype._toArrayLikeLE = function _toArrayLikeLE (res, byteLength) {
802 var position = 0;
803 var carry = 0;
804 for (var i = 0, shift = 0; i < this.length; i++) {
805 var word = (this.words[i] << shift) | carry;
806 res[position++] = word & 0xff;
807 if (position < res.length) {
808 res[position++] = (word >> 8) & 0xff;
809 }
810 if (position < res.length) {
811 res[position++] = (word >> 16) & 0xff;
812 }
813 if (shift === 6) {
814 if (position < res.length) {
815 res[position++] = (word >> 24) & 0xff;
816 }
817 carry = 0;
818 shift = 0;
819 } else {
820 carry = word >>> 24;
821 shift += 2;
822 }
823 }
824 if (position < res.length) {
825 res[position++] = carry;
826 while (position < res.length) {
827 res[position++] = 0;
828 }
829 }
830 };
831 BN.prototype._toArrayLikeBE = function _toArrayLikeBE (res, byteLength) {
832 var position = res.length - 1;
833 var carry = 0;
834 for (var i = 0, shift = 0; i < this.length; i++) {
835 var word = (this.words[i] << shift) | carry;
836 res[position--] = word & 0xff;
837 if (position >= 0) {
838 res[position--] = (word >> 8) & 0xff;
839 }
840 if (position >= 0) {
841 res[position--] = (word >> 16) & 0xff;
842 }
843 if (shift === 6) {
844 if (position >= 0) {
845 res[position--] = (word >> 24) & 0xff;
846 }
847 carry = 0;
848 shift = 0;
849 } else {
850 carry = word >>> 24;
851 shift += 2;
852 }
853 }
854 if (position >= 0) {
855 res[position--] = carry;
856 while (position >= 0) {
857 res[position--] = 0;
858 }
859 }
860 };
861 if (Math.clz32) {
862 BN.prototype._countBits = function _countBits (w) {
863 return 32 - Math.clz32(w);
864 };
865 } else {
866 BN.prototype._countBits = function _countBits (w) {
867 var t = w;
868 var r = 0;
869 if (t >= 0x1000) {
870 r += 13;
871 t >>>= 13;
872 }
873 if (t >= 0x40) {
874 r += 7;
875 t >>>= 7;
876 }
877 if (t >= 0x8) {
878 r += 4;
879 t >>>= 4;
880 }
881 if (t >= 0x02) {
882 r += 2;
883 t >>>= 2;
884 }
885 return r + t;
886 };
887 }
888 BN.prototype._zeroBits = function _zeroBits (w) {
889 if (w === 0) return 26;
890 var t = w;
891 var r = 0;
892 if ((t & 0x1fff) === 0) {
893 r += 13;
894 t >>>= 13;
895 }
896 if ((t & 0x7f) === 0) {
897 r += 7;
898 t >>>= 7;
899 }
900 if ((t & 0xf) === 0) {
901 r += 4;
902 t >>>= 4;
903 }
904 if ((t & 0x3) === 0) {
905 r += 2;
906 t >>>= 2;
907 }
908 if ((t & 0x1) === 0) {
909 r++;
910 }
911 return r;
912 };
913 BN.prototype.bitLength = function bitLength () {
914 var w = this.words[this.length - 1];
915 var hi = this._countBits(w);
916 return (this.length - 1) * 26 + hi;
917 };
918 function toBitArray (num) {
919 var w = new Array(num.bitLength());
920 for (var bit = 0; bit < w.length; bit++) {
921 var off = (bit / 26) | 0;
922 var wbit = bit % 26;
923 w[bit] = (num.words[off] >>> wbit) & 0x01;
924 }
925 return w;
926 }
927 BN.prototype.zeroBits = function zeroBits () {
928 if (this.isZero()) return 0;
929 var r = 0;
930 for (var i = 0; i < this.length; i++) {
931 var b = this._zeroBits(this.words[i]);
932 r += b;
933 if (b !== 26) break;
934 }
935 return r;
936 };
937 BN.prototype.byteLength = function byteLength () {
938 return Math.ceil(this.bitLength() / 8);
939 };
940 BN.prototype.toTwos = function toTwos (width) {
941 if (this.negative !== 0) {
942 return this.abs().inotn(width).iaddn(1);
943 }
944 return this.clone();
945 };
946 BN.prototype.fromTwos = function fromTwos (width) {
947 if (this.testn(width - 1)) {
948 return this.notn(width).iaddn(1).ineg();
949 }
950 return this.clone();
951 };
952 BN.prototype.isNeg = function isNeg () {
953 return this.negative !== 0;
954 };
955 BN.prototype.neg = function neg () {
956 return this.clone().ineg();
957 };
958 BN.prototype.ineg = function ineg () {
959 if (!this.isZero()) {
960 this.negative ^= 1;
961 }
962 return this;
963 };
964 BN.prototype.iuor = function iuor (num) {
965 while (this.length < num.length) {
966 this.words[this.length++] = 0;
967 }
968 for (var i = 0; i < num.length; i++) {
969 this.words[i] = this.words[i] | num.words[i];
970 }
971 return this._strip();
972 };
973 BN.prototype.ior = function ior (num) {
974 assert((this.negative | num.negative) === 0);
975 return this.iuor(num);
976 };
977 BN.prototype.or = function or (num) {
978 if (this.length > num.length) return this.clone().ior(num);
979 return num.clone().ior(this);
980 };
981 BN.prototype.uor = function uor (num) {
982 if (this.length > num.length) return this.clone().iuor(num);
983 return num.clone().iuor(this);
984 };
985 BN.prototype.iuand = function iuand (num) {
986 var b;
987 if (this.length > num.length) {
988 b = num;
989 } else {
990 b = this;
991 }
992 for (var i = 0; i < b.length; i++) {
993 this.words[i] = this.words[i] & num.words[i];
994 }
995 this.length = b.length;
996 return this._strip();
997 };
998 BN.prototype.iand = function iand (num) {
999 assert((this.negative | num.negative) === 0);
1000 return this.iuand(num);
1001 };
1002 BN.prototype.and = function and (num) {
1003 if (this.length > num.length) return this.clone().iand(num);
1004 return num.clone().iand(this);
1005 };
1006 BN.prototype.uand = function uand (num) {
1007 if (this.length > num.length) return this.clone().iuand(num);
1008 return num.clone().iuand(this);
1009 };
1010 BN.prototype.iuxor = function iuxor (num) {
1011 var a;
1012 var b;
1013 if (this.length > num.length) {
1014 a = this;
1015 b = num;
1016 } else {
1017 a = num;
1018 b = this;
1019 }
1020 for (var i = 0; i < b.length; i++) {
1021 this.words[i] = a.words[i] ^ b.words[i];
1022 }
1023 if (this !== a) {
1024 for (; i < a.length; i++) {
1025 this.words[i] = a.words[i];
1026 }
1027 }
1028 this.length = a.length;
1029 return this._strip();
1030 };
1031 BN.prototype.ixor = function ixor (num) {
1032 assert((this.negative | num.negative) === 0);
1033 return this.iuxor(num);
1034 };
1035 BN.prototype.xor = function xor (num) {
1036 if (this.length > num.length) return this.clone().ixor(num);
1037 return num.clone().ixor(this);
1038 };
1039 BN.prototype.uxor = function uxor (num) {
1040 if (this.length > num.length) return this.clone().iuxor(num);
1041 return num.clone().iuxor(this);
1042 };
1043 BN.prototype.inotn = function inotn (width) {
1044 assert(typeof width === 'number' && width >= 0);
1045 var bytesNeeded = Math.ceil(width / 26) | 0;
1046 var bitsLeft = width % 26;
1047 this._expand(bytesNeeded);
1048 if (bitsLeft > 0) {
1049 bytesNeeded--;
1050 }
1051 for (var i = 0; i < bytesNeeded; i++) {
1052 this.words[i] = ~this.words[i] & 0x3ffffff;
1053 }
1054 if (bitsLeft > 0) {
1055 this.words[i] = ~this.words[i] & (0x3ffffff >> (26 - bitsLeft));
1056 }
1057 return this._strip();
1058 };
1059 BN.prototype.notn = function notn (width) {
1060 return this.clone().inotn(width);
1061 };
1062 BN.prototype.setn = function setn (bit, val) {
1063 assert(typeof bit === 'number' && bit >= 0);
1064 var off = (bit / 26) | 0;
1065 var wbit = bit % 26;
1066 this._expand(off + 1);
1067 if (val) {
1068 this.words[off] = this.words[off] | (1 << wbit);
1069 } else {
1070 this.words[off] = this.words[off] & ~(1 << wbit);
1071 }
1072 return this._strip();
1073 };
1074 BN.prototype.iadd = function iadd (num) {
1075 var r;
1076 if (this.negative !== 0 && num.negative === 0) {
1077 this.negative = 0;
1078 r = this.isub(num);
1079 this.negative ^= 1;
1080 return this._normSign();
1081 } else if (this.negative === 0 && num.negative !== 0) {
1082 num.negative = 0;
1083 r = this.isub(num);
1084 num.negative = 1;
1085 return r._normSign();
1086 }
1087 var a, b;
1088 if (this.length > num.length) {
1089 a = this;
1090 b = num;
1091 } else {
1092 a = num;
1093 b = this;
1094 }
1095 var carry = 0;
1096 for (var i = 0; i < b.length; i++) {
1097 r = (a.words[i] | 0) + (b.words[i] | 0) + carry;
1098 this.words[i] = r & 0x3ffffff;
1099 carry = r >>> 26;
1100 }
1101 for (; carry !== 0 && i < a.length; i++) {
1102 r = (a.words[i] | 0) + carry;
1103 this.words[i] = r & 0x3ffffff;
1104 carry = r >>> 26;
1105 }
1106 this.length = a.length;
1107 if (carry !== 0) {
1108 this.words[this.length] = carry;
1109 this.length++;
1110 } else if (a !== this) {
1111 for (; i < a.length; i++) {
1112 this.words[i] = a.words[i];
1113 }
1114 }
1115 return this;
1116 };
1117 BN.prototype.add = function add (num) {
1118 var res;
1119 if (num.negative !== 0 && this.negative === 0) {
1120 num.negative = 0;
1121 res = this.sub(num);
1122 num.negative ^= 1;
1123 return res;
1124 } else if (num.negative === 0 && this.negative !== 0) {
1125 this.negative = 0;
1126 res = num.sub(this);
1127 this.negative = 1;
1128 return res;
1129 }
1130 if (this.length > num.length) return this.clone().iadd(num);
1131 return num.clone().iadd(this);
1132 };
1133 BN.prototype.isub = function isub (num) {
1134 if (num.negative !== 0) {
1135 num.negative = 0;
1136 var r = this.iadd(num);
1137 num.negative = 1;
1138 return r._normSign();
1139 } else if (this.negative !== 0) {
1140 this.negative = 0;
1141 this.iadd(num);
1142 this.negative = 1;
1143 return this._normSign();
1144 }
1145 var cmp = this.cmp(num);
1146 if (cmp === 0) {
1147 this.negative = 0;
1148 this.length = 1;
1149 this.words[0] = 0;
1150 return this;
1151 }
1152 var a, b;
1153 if (cmp > 0) {
1154 a = this;
1155 b = num;
1156 } else {
1157 a = num;
1158 b = this;
1159 }
1160 var carry = 0;
1161 for (var i = 0; i < b.length; i++) {
1162 r = (a.words[i] | 0) - (b.words[i] | 0) + carry;
1163 carry = r >> 26;
1164 this.words[i] = r & 0x3ffffff;
1165 }
1166 for (; carry !== 0 && i < a.length; i++) {
1167 r = (a.words[i] | 0) + carry;
1168 carry = r >> 26;
1169 this.words[i] = r & 0x3ffffff;
1170 }
1171 if (carry === 0 && i < a.length && a !== this) {
1172 for (; i < a.length; i++) {
1173 this.words[i] = a.words[i];
1174 }
1175 }
1176 this.length = Math.max(this.length, i);
1177 if (a !== this) {
1178 this.negative = 1;
1179 }
1180 return this._strip();
1181 };
1182 BN.prototype.sub = function sub (num) {
1183 return this.clone().isub(num);
1184 };
1185 function smallMulTo (self, num, out) {
1186 out.negative = num.negative ^ self.negative;
1187 var len = (self.length + num.length) | 0;
1188 out.length = len;
1189 len = (len - 1) | 0;
1190 var a = self.words[0] | 0;
1191 var b = num.words[0] | 0;
1192 var r = a * b;
1193 var lo = r & 0x3ffffff;
1194 var carry = (r / 0x4000000) | 0;
1195 out.words[0] = lo;
1196 for (var k = 1; k < len; k++) {
1197 var ncarry = carry >>> 26;
1198 var rword = carry & 0x3ffffff;
1199 var maxJ = Math.min(k, num.length - 1);
1200 for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) {
1201 var i = (k - j) | 0;
1202 a = self.words[i] | 0;
1203 b = num.words[j] | 0;
1204 r = a * b + rword;
1205 ncarry += (r / 0x4000000) | 0;
1206 rword = r & 0x3ffffff;
1207 }
1208 out.words[k] = rword | 0;
1209 carry = ncarry | 0;
1210 }
1211 if (carry !== 0) {
1212 out.words[k] = carry | 0;
1213 } else {
1214 out.length--;
1215 }
1216 return out._strip();
1217 }
1218 var comb10MulTo = function comb10MulTo (self, num, out) {
1219 var a = self.words;
1220 var b = num.words;
1221 var o = out.words;
1222 var c = 0;
1223 var lo;
1224 var mid;
1225 var hi;
1226 var a0 = a[0] | 0;
1227 var al0 = a0 & 0x1fff;
1228 var ah0 = a0 >>> 13;
1229 var a1 = a[1] | 0;
1230 var al1 = a1 & 0x1fff;
1231 var ah1 = a1 >>> 13;
1232 var a2 = a[2] | 0;
1233 var al2 = a2 & 0x1fff;
1234 var ah2 = a2 >>> 13;
1235 var a3 = a[3] | 0;
1236 var al3 = a3 & 0x1fff;
1237 var ah3 = a3 >>> 13;
1238 var a4 = a[4] | 0;
1239 var al4 = a4 & 0x1fff;
1240 var ah4 = a4 >>> 13;
1241 var a5 = a[5] | 0;
1242 var al5 = a5 & 0x1fff;
1243 var ah5 = a5 >>> 13;
1244 var a6 = a[6] | 0;
1245 var al6 = a6 & 0x1fff;
1246 var ah6 = a6 >>> 13;
1247 var a7 = a[7] | 0;
1248 var al7 = a7 & 0x1fff;
1249 var ah7 = a7 >>> 13;
1250 var a8 = a[8] | 0;
1251 var al8 = a8 & 0x1fff;
1252 var ah8 = a8 >>> 13;
1253 var a9 = a[9] | 0;
1254 var al9 = a9 & 0x1fff;
1255 var ah9 = a9 >>> 13;
1256 var b0 = b[0] | 0;
1257 var bl0 = b0 & 0x1fff;
1258 var bh0 = b0 >>> 13;
1259 var b1 = b[1] | 0;
1260 var bl1 = b1 & 0x1fff;
1261 var bh1 = b1 >>> 13;
1262 var b2 = b[2] | 0;
1263 var bl2 = b2 & 0x1fff;
1264 var bh2 = b2 >>> 13;
1265 var b3 = b[3] | 0;
1266 var bl3 = b3 & 0x1fff;
1267 var bh3 = b3 >>> 13;
1268 var b4 = b[4] | 0;
1269 var bl4 = b4 & 0x1fff;
1270 var bh4 = b4 >>> 13;
1271 var b5 = b[5] | 0;
1272 var bl5 = b5 & 0x1fff;
1273 var bh5 = b5 >>> 13;
1274 var b6 = b[6] | 0;
1275 var bl6 = b6 & 0x1fff;
1276 var bh6 = b6 >>> 13;
1277 var b7 = b[7] | 0;
1278 var bl7 = b7 & 0x1fff;
1279 var bh7 = b7 >>> 13;
1280 var b8 = b[8] | 0;
1281 var bl8 = b8 & 0x1fff;
1282 var bh8 = b8 >>> 13;
1283 var b9 = b[9] | 0;
1284 var bl9 = b9 & 0x1fff;
1285 var bh9 = b9 >>> 13;
1286 out.negative = self.negative ^ num.negative;
1287 out.length = 19;
1288 lo = Math.imul(al0, bl0);
1289 mid = Math.imul(al0, bh0);
1290 mid = (mid + Math.imul(ah0, bl0)) | 0;
1291 hi = Math.imul(ah0, bh0);
1292 var w0 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1293 c = (((hi + (mid >>> 13)) | 0) + (w0 >>> 26)) | 0;
1294 w0 &= 0x3ffffff;
1295 lo = Math.imul(al1, bl0);
1296 mid = Math.imul(al1, bh0);
1297 mid = (mid + Math.imul(ah1, bl0)) | 0;
1298 hi = Math.imul(ah1, bh0);
1299 lo = (lo + Math.imul(al0, bl1)) | 0;
1300 mid = (mid + Math.imul(al0, bh1)) | 0;
1301 mid = (mid + Math.imul(ah0, bl1)) | 0;
1302 hi = (hi + Math.imul(ah0, bh1)) | 0;
1303 var w1 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1304 c = (((hi + (mid >>> 13)) | 0) + (w1 >>> 26)) | 0;
1305 w1 &= 0x3ffffff;
1306 lo = Math.imul(al2, bl0);
1307 mid = Math.imul(al2, bh0);
1308 mid = (mid + Math.imul(ah2, bl0)) | 0;
1309 hi = Math.imul(ah2, bh0);
1310 lo = (lo + Math.imul(al1, bl1)) | 0;
1311 mid = (mid + Math.imul(al1, bh1)) | 0;
1312 mid = (mid + Math.imul(ah1, bl1)) | 0;
1313 hi = (hi + Math.imul(ah1, bh1)) | 0;
1314 lo = (lo + Math.imul(al0, bl2)) | 0;
1315 mid = (mid + Math.imul(al0, bh2)) | 0;
1316 mid = (mid + Math.imul(ah0, bl2)) | 0;
1317 hi = (hi + Math.imul(ah0, bh2)) | 0;
1318 var w2 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1319 c = (((hi + (mid >>> 13)) | 0) + (w2 >>> 26)) | 0;
1320 w2 &= 0x3ffffff;
1321 lo = Math.imul(al3, bl0);
1322 mid = Math.imul(al3, bh0);
1323 mid = (mid + Math.imul(ah3, bl0)) | 0;
1324 hi = Math.imul(ah3, bh0);
1325 lo = (lo + Math.imul(al2, bl1)) | 0;
1326 mid = (mid + Math.imul(al2, bh1)) | 0;
1327 mid = (mid + Math.imul(ah2, bl1)) | 0;
1328 hi = (hi + Math.imul(ah2, bh1)) | 0;
1329 lo = (lo + Math.imul(al1, bl2)) | 0;
1330 mid = (mid + Math.imul(al1, bh2)) | 0;
1331 mid = (mid + Math.imul(ah1, bl2)) | 0;
1332 hi = (hi + Math.imul(ah1, bh2)) | 0;
1333 lo = (lo + Math.imul(al0, bl3)) | 0;
1334 mid = (mid + Math.imul(al0, bh3)) | 0;
1335 mid = (mid + Math.imul(ah0, bl3)) | 0;
1336 hi = (hi + Math.imul(ah0, bh3)) | 0;
1337 var w3 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1338 c = (((hi + (mid >>> 13)) | 0) + (w3 >>> 26)) | 0;
1339 w3 &= 0x3ffffff;
1340 lo = Math.imul(al4, bl0);
1341 mid = Math.imul(al4, bh0);
1342 mid = (mid + Math.imul(ah4, bl0)) | 0;
1343 hi = Math.imul(ah4, bh0);
1344 lo = (lo + Math.imul(al3, bl1)) | 0;
1345 mid = (mid + Math.imul(al3, bh1)) | 0;
1346 mid = (mid + Math.imul(ah3, bl1)) | 0;
1347 hi = (hi + Math.imul(ah3, bh1)) | 0;
1348 lo = (lo + Math.imul(al2, bl2)) | 0;
1349 mid = (mid + Math.imul(al2, bh2)) | 0;
1350 mid = (mid + Math.imul(ah2, bl2)) | 0;
1351 hi = (hi + Math.imul(ah2, bh2)) | 0;
1352 lo = (lo + Math.imul(al1, bl3)) | 0;
1353 mid = (mid + Math.imul(al1, bh3)) | 0;
1354 mid = (mid + Math.imul(ah1, bl3)) | 0;
1355 hi = (hi + Math.imul(ah1, bh3)) | 0;
1356 lo = (lo + Math.imul(al0, bl4)) | 0;
1357 mid = (mid + Math.imul(al0, bh4)) | 0;
1358 mid = (mid + Math.imul(ah0, bl4)) | 0;
1359 hi = (hi + Math.imul(ah0, bh4)) | 0;
1360 var w4 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1361 c = (((hi + (mid >>> 13)) | 0) + (w4 >>> 26)) | 0;
1362 w4 &= 0x3ffffff;
1363 lo = Math.imul(al5, bl0);
1364 mid = Math.imul(al5, bh0);
1365 mid = (mid + Math.imul(ah5, bl0)) | 0;
1366 hi = Math.imul(ah5, bh0);
1367 lo = (lo + Math.imul(al4, bl1)) | 0;
1368 mid = (mid + Math.imul(al4, bh1)) | 0;
1369 mid = (mid + Math.imul(ah4, bl1)) | 0;
1370 hi = (hi + Math.imul(ah4, bh1)) | 0;
1371 lo = (lo + Math.imul(al3, bl2)) | 0;
1372 mid = (mid + Math.imul(al3, bh2)) | 0;
1373 mid = (mid + Math.imul(ah3, bl2)) | 0;
1374 hi = (hi + Math.imul(ah3, bh2)) | 0;
1375 lo = (lo + Math.imul(al2, bl3)) | 0;
1376 mid = (mid + Math.imul(al2, bh3)) | 0;
1377 mid = (mid + Math.imul(ah2, bl3)) | 0;
1378 hi = (hi + Math.imul(ah2, bh3)) | 0;
1379 lo = (lo + Math.imul(al1, bl4)) | 0;
1380 mid = (mid + Math.imul(al1, bh4)) | 0;
1381 mid = (mid + Math.imul(ah1, bl4)) | 0;
1382 hi = (hi + Math.imul(ah1, bh4)) | 0;
1383 lo = (lo + Math.imul(al0, bl5)) | 0;
1384 mid = (mid + Math.imul(al0, bh5)) | 0;
1385 mid = (mid + Math.imul(ah0, bl5)) | 0;
1386 hi = (hi + Math.imul(ah0, bh5)) | 0;
1387 var w5 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1388 c = (((hi + (mid >>> 13)) | 0) + (w5 >>> 26)) | 0;
1389 w5 &= 0x3ffffff;
1390 lo = Math.imul(al6, bl0);
1391 mid = Math.imul(al6, bh0);
1392 mid = (mid + Math.imul(ah6, bl0)) | 0;
1393 hi = Math.imul(ah6, bh0);
1394 lo = (lo + Math.imul(al5, bl1)) | 0;
1395 mid = (mid + Math.imul(al5, bh1)) | 0;
1396 mid = (mid + Math.imul(ah5, bl1)) | 0;
1397 hi = (hi + Math.imul(ah5, bh1)) | 0;
1398 lo = (lo + Math.imul(al4, bl2)) | 0;
1399 mid = (mid + Math.imul(al4, bh2)) | 0;
1400 mid = (mid + Math.imul(ah4, bl2)) | 0;
1401 hi = (hi + Math.imul(ah4, bh2)) | 0;
1402 lo = (lo + Math.imul(al3, bl3)) | 0;
1403 mid = (mid + Math.imul(al3, bh3)) | 0;
1404 mid = (mid + Math.imul(ah3, bl3)) | 0;
1405 hi = (hi + Math.imul(ah3, bh3)) | 0;
1406 lo = (lo + Math.imul(al2, bl4)) | 0;
1407 mid = (mid + Math.imul(al2, bh4)) | 0;
1408 mid = (mid + Math.imul(ah2, bl4)) | 0;
1409 hi = (hi + Math.imul(ah2, bh4)) | 0;
1410 lo = (lo + Math.imul(al1, bl5)) | 0;
1411 mid = (mid + Math.imul(al1, bh5)) | 0;
1412 mid = (mid + Math.imul(ah1, bl5)) | 0;
1413 hi = (hi + Math.imul(ah1, bh5)) | 0;
1414 lo = (lo + Math.imul(al0, bl6)) | 0;
1415 mid = (mid + Math.imul(al0, bh6)) | 0;
1416 mid = (mid + Math.imul(ah0, bl6)) | 0;
1417 hi = (hi + Math.imul(ah0, bh6)) | 0;
1418 var w6 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1419 c = (((hi + (mid >>> 13)) | 0) + (w6 >>> 26)) | 0;
1420 w6 &= 0x3ffffff;
1421 lo = Math.imul(al7, bl0);
1422 mid = Math.imul(al7, bh0);
1423 mid = (mid + Math.imul(ah7, bl0)) | 0;
1424 hi = Math.imul(ah7, bh0);
1425 lo = (lo + Math.imul(al6, bl1)) | 0;
1426 mid = (mid + Math.imul(al6, bh1)) | 0;
1427 mid = (mid + Math.imul(ah6, bl1)) | 0;
1428 hi = (hi + Math.imul(ah6, bh1)) | 0;
1429 lo = (lo + Math.imul(al5, bl2)) | 0;
1430 mid = (mid + Math.imul(al5, bh2)) | 0;
1431 mid = (mid + Math.imul(ah5, bl2)) | 0;
1432 hi = (hi + Math.imul(ah5, bh2)) | 0;
1433 lo = (lo + Math.imul(al4, bl3)) | 0;
1434 mid = (mid + Math.imul(al4, bh3)) | 0;
1435 mid = (mid + Math.imul(ah4, bl3)) | 0;
1436 hi = (hi + Math.imul(ah4, bh3)) | 0;
1437 lo = (lo + Math.imul(al3, bl4)) | 0;
1438 mid = (mid + Math.imul(al3, bh4)) | 0;
1439 mid = (mid + Math.imul(ah3, bl4)) | 0;
1440 hi = (hi + Math.imul(ah3, bh4)) | 0;
1441 lo = (lo + Math.imul(al2, bl5)) | 0;
1442 mid = (mid + Math.imul(al2, bh5)) | 0;
1443 mid = (mid + Math.imul(ah2, bl5)) | 0;
1444 hi = (hi + Math.imul(ah2, bh5)) | 0;
1445 lo = (lo + Math.imul(al1, bl6)) | 0;
1446 mid = (mid + Math.imul(al1, bh6)) | 0;
1447 mid = (mid + Math.imul(ah1, bl6)) | 0;
1448 hi = (hi + Math.imul(ah1, bh6)) | 0;
1449 lo = (lo + Math.imul(al0, bl7)) | 0;
1450 mid = (mid + Math.imul(al0, bh7)) | 0;
1451 mid = (mid + Math.imul(ah0, bl7)) | 0;
1452 hi = (hi + Math.imul(ah0, bh7)) | 0;
1453 var w7 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1454 c = (((hi + (mid >>> 13)) | 0) + (w7 >>> 26)) | 0;
1455 w7 &= 0x3ffffff;
1456 lo = Math.imul(al8, bl0);
1457 mid = Math.imul(al8, bh0);
1458 mid = (mid + Math.imul(ah8, bl0)) | 0;
1459 hi = Math.imul(ah8, bh0);
1460 lo = (lo + Math.imul(al7, bl1)) | 0;
1461 mid = (mid + Math.imul(al7, bh1)) | 0;
1462 mid = (mid + Math.imul(ah7, bl1)) | 0;
1463 hi = (hi + Math.imul(ah7, bh1)) | 0;
1464 lo = (lo + Math.imul(al6, bl2)) | 0;
1465 mid = (mid + Math.imul(al6, bh2)) | 0;
1466 mid = (mid + Math.imul(ah6, bl2)) | 0;
1467 hi = (hi + Math.imul(ah6, bh2)) | 0;
1468 lo = (lo + Math.imul(al5, bl3)) | 0;
1469 mid = (mid + Math.imul(al5, bh3)) | 0;
1470 mid = (mid + Math.imul(ah5, bl3)) | 0;
1471 hi = (hi + Math.imul(ah5, bh3)) | 0;
1472 lo = (lo + Math.imul(al4, bl4)) | 0;
1473 mid = (mid + Math.imul(al4, bh4)) | 0;
1474 mid = (mid + Math.imul(ah4, bl4)) | 0;
1475 hi = (hi + Math.imul(ah4, bh4)) | 0;
1476 lo = (lo + Math.imul(al3, bl5)) | 0;
1477 mid = (mid + Math.imul(al3, bh5)) | 0;
1478 mid = (mid + Math.imul(ah3, bl5)) | 0;
1479 hi = (hi + Math.imul(ah3, bh5)) | 0;
1480 lo = (lo + Math.imul(al2, bl6)) | 0;
1481 mid = (mid + Math.imul(al2, bh6)) | 0;
1482 mid = (mid + Math.imul(ah2, bl6)) | 0;
1483 hi = (hi + Math.imul(ah2, bh6)) | 0;
1484 lo = (lo + Math.imul(al1, bl7)) | 0;
1485 mid = (mid + Math.imul(al1, bh7)) | 0;
1486 mid = (mid + Math.imul(ah1, bl7)) | 0;
1487 hi = (hi + Math.imul(ah1, bh7)) | 0;
1488 lo = (lo + Math.imul(al0, bl8)) | 0;
1489 mid = (mid + Math.imul(al0, bh8)) | 0;
1490 mid = (mid + Math.imul(ah0, bl8)) | 0;
1491 hi = (hi + Math.imul(ah0, bh8)) | 0;
1492 var w8 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1493 c = (((hi + (mid >>> 13)) | 0) + (w8 >>> 26)) | 0;
1494 w8 &= 0x3ffffff;
1495 lo = Math.imul(al9, bl0);
1496 mid = Math.imul(al9, bh0);
1497 mid = (mid + Math.imul(ah9, bl0)) | 0;
1498 hi = Math.imul(ah9, bh0);
1499 lo = (lo + Math.imul(al8, bl1)) | 0;
1500 mid = (mid + Math.imul(al8, bh1)) | 0;
1501 mid = (mid + Math.imul(ah8, bl1)) | 0;
1502 hi = (hi + Math.imul(ah8, bh1)) | 0;
1503 lo = (lo + Math.imul(al7, bl2)) | 0;
1504 mid = (mid + Math.imul(al7, bh2)) | 0;
1505 mid = (mid + Math.imul(ah7, bl2)) | 0;
1506 hi = (hi + Math.imul(ah7, bh2)) | 0;
1507 lo = (lo + Math.imul(al6, bl3)) | 0;
1508 mid = (mid + Math.imul(al6, bh3)) | 0;
1509 mid = (mid + Math.imul(ah6, bl3)) | 0;
1510 hi = (hi + Math.imul(ah6, bh3)) | 0;
1511 lo = (lo + Math.imul(al5, bl4)) | 0;
1512 mid = (mid + Math.imul(al5, bh4)) | 0;
1513 mid = (mid + Math.imul(ah5, bl4)) | 0;
1514 hi = (hi + Math.imul(ah5, bh4)) | 0;
1515 lo = (lo + Math.imul(al4, bl5)) | 0;
1516 mid = (mid + Math.imul(al4, bh5)) | 0;
1517 mid = (mid + Math.imul(ah4, bl5)) | 0;
1518 hi = (hi + Math.imul(ah4, bh5)) | 0;
1519 lo = (lo + Math.imul(al3, bl6)) | 0;
1520 mid = (mid + Math.imul(al3, bh6)) | 0;
1521 mid = (mid + Math.imul(ah3, bl6)) | 0;
1522 hi = (hi + Math.imul(ah3, bh6)) | 0;
1523 lo = (lo + Math.imul(al2, bl7)) | 0;
1524 mid = (mid + Math.imul(al2, bh7)) | 0;
1525 mid = (mid + Math.imul(ah2, bl7)) | 0;
1526 hi = (hi + Math.imul(ah2, bh7)) | 0;
1527 lo = (lo + Math.imul(al1, bl8)) | 0;
1528 mid = (mid + Math.imul(al1, bh8)) | 0;
1529 mid = (mid + Math.imul(ah1, bl8)) | 0;
1530 hi = (hi + Math.imul(ah1, bh8)) | 0;
1531 lo = (lo + Math.imul(al0, bl9)) | 0;
1532 mid = (mid + Math.imul(al0, bh9)) | 0;
1533 mid = (mid + Math.imul(ah0, bl9)) | 0;
1534 hi = (hi + Math.imul(ah0, bh9)) | 0;
1535 var w9 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1536 c = (((hi + (mid >>> 13)) | 0) + (w9 >>> 26)) | 0;
1537 w9 &= 0x3ffffff;
1538 lo = Math.imul(al9, bl1);
1539 mid = Math.imul(al9, bh1);
1540 mid = (mid + Math.imul(ah9, bl1)) | 0;
1541 hi = Math.imul(ah9, bh1);
1542 lo = (lo + Math.imul(al8, bl2)) | 0;
1543 mid = (mid + Math.imul(al8, bh2)) | 0;
1544 mid = (mid + Math.imul(ah8, bl2)) | 0;
1545 hi = (hi + Math.imul(ah8, bh2)) | 0;
1546 lo = (lo + Math.imul(al7, bl3)) | 0;
1547 mid = (mid + Math.imul(al7, bh3)) | 0;
1548 mid = (mid + Math.imul(ah7, bl3)) | 0;
1549 hi = (hi + Math.imul(ah7, bh3)) | 0;
1550 lo = (lo + Math.imul(al6, bl4)) | 0;
1551 mid = (mid + Math.imul(al6, bh4)) | 0;
1552 mid = (mid + Math.imul(ah6, bl4)) | 0;
1553 hi = (hi + Math.imul(ah6, bh4)) | 0;
1554 lo = (lo + Math.imul(al5, bl5)) | 0;
1555 mid = (mid + Math.imul(al5, bh5)) | 0;
1556 mid = (mid + Math.imul(ah5, bl5)) | 0;
1557 hi = (hi + Math.imul(ah5, bh5)) | 0;
1558 lo = (lo + Math.imul(al4, bl6)) | 0;
1559 mid = (mid + Math.imul(al4, bh6)) | 0;
1560 mid = (mid + Math.imul(ah4, bl6)) | 0;
1561 hi = (hi + Math.imul(ah4, bh6)) | 0;
1562 lo = (lo + Math.imul(al3, bl7)) | 0;
1563 mid = (mid + Math.imul(al3, bh7)) | 0;
1564 mid = (mid + Math.imul(ah3, bl7)) | 0;
1565 hi = (hi + Math.imul(ah3, bh7)) | 0;
1566 lo = (lo + Math.imul(al2, bl8)) | 0;
1567 mid = (mid + Math.imul(al2, bh8)) | 0;
1568 mid = (mid + Math.imul(ah2, bl8)) | 0;
1569 hi = (hi + Math.imul(ah2, bh8)) | 0;
1570 lo = (lo + Math.imul(al1, bl9)) | 0;
1571 mid = (mid + Math.imul(al1, bh9)) | 0;
1572 mid = (mid + Math.imul(ah1, bl9)) | 0;
1573 hi = (hi + Math.imul(ah1, bh9)) | 0;
1574 var w10 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1575 c = (((hi + (mid >>> 13)) | 0) + (w10 >>> 26)) | 0;
1576 w10 &= 0x3ffffff;
1577 lo = Math.imul(al9, bl2);
1578 mid = Math.imul(al9, bh2);
1579 mid = (mid + Math.imul(ah9, bl2)) | 0;
1580 hi = Math.imul(ah9, bh2);
1581 lo = (lo + Math.imul(al8, bl3)) | 0;
1582 mid = (mid + Math.imul(al8, bh3)) | 0;
1583 mid = (mid + Math.imul(ah8, bl3)) | 0;
1584 hi = (hi + Math.imul(ah8, bh3)) | 0;
1585 lo = (lo + Math.imul(al7, bl4)) | 0;
1586 mid = (mid + Math.imul(al7, bh4)) | 0;
1587 mid = (mid + Math.imul(ah7, bl4)) | 0;
1588 hi = (hi + Math.imul(ah7, bh4)) | 0;
1589 lo = (lo + Math.imul(al6, bl5)) | 0;
1590 mid = (mid + Math.imul(al6, bh5)) | 0;
1591 mid = (mid + Math.imul(ah6, bl5)) | 0;
1592 hi = (hi + Math.imul(ah6, bh5)) | 0;
1593 lo = (lo + Math.imul(al5, bl6)) | 0;
1594 mid = (mid + Math.imul(al5, bh6)) | 0;
1595 mid = (mid + Math.imul(ah5, bl6)) | 0;
1596 hi = (hi + Math.imul(ah5, bh6)) | 0;
1597 lo = (lo + Math.imul(al4, bl7)) | 0;
1598 mid = (mid + Math.imul(al4, bh7)) | 0;
1599 mid = (mid + Math.imul(ah4, bl7)) | 0;
1600 hi = (hi + Math.imul(ah4, bh7)) | 0;
1601 lo = (lo + Math.imul(al3, bl8)) | 0;
1602 mid = (mid + Math.imul(al3, bh8)) | 0;
1603 mid = (mid + Math.imul(ah3, bl8)) | 0;
1604 hi = (hi + Math.imul(ah3, bh8)) | 0;
1605 lo = (lo + Math.imul(al2, bl9)) | 0;
1606 mid = (mid + Math.imul(al2, bh9)) | 0;
1607 mid = (mid + Math.imul(ah2, bl9)) | 0;
1608 hi = (hi + Math.imul(ah2, bh9)) | 0;
1609 var w11 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1610 c = (((hi + (mid >>> 13)) | 0) + (w11 >>> 26)) | 0;
1611 w11 &= 0x3ffffff;
1612 lo = Math.imul(al9, bl3);
1613 mid = Math.imul(al9, bh3);
1614 mid = (mid + Math.imul(ah9, bl3)) | 0;
1615 hi = Math.imul(ah9, bh3);
1616 lo = (lo + Math.imul(al8, bl4)) | 0;
1617 mid = (mid + Math.imul(al8, bh4)) | 0;
1618 mid = (mid + Math.imul(ah8, bl4)) | 0;
1619 hi = (hi + Math.imul(ah8, bh4)) | 0;
1620 lo = (lo + Math.imul(al7, bl5)) | 0;
1621 mid = (mid + Math.imul(al7, bh5)) | 0;
1622 mid = (mid + Math.imul(ah7, bl5)) | 0;
1623 hi = (hi + Math.imul(ah7, bh5)) | 0;
1624 lo = (lo + Math.imul(al6, bl6)) | 0;
1625 mid = (mid + Math.imul(al6, bh6)) | 0;
1626 mid = (mid + Math.imul(ah6, bl6)) | 0;
1627 hi = (hi + Math.imul(ah6, bh6)) | 0;
1628 lo = (lo + Math.imul(al5, bl7)) | 0;
1629 mid = (mid + Math.imul(al5, bh7)) | 0;
1630 mid = (mid + Math.imul(ah5, bl7)) | 0;
1631 hi = (hi + Math.imul(ah5, bh7)) | 0;
1632 lo = (lo + Math.imul(al4, bl8)) | 0;
1633 mid = (mid + Math.imul(al4, bh8)) | 0;
1634 mid = (mid + Math.imul(ah4, bl8)) | 0;
1635 hi = (hi + Math.imul(ah4, bh8)) | 0;
1636 lo = (lo + Math.imul(al3, bl9)) | 0;
1637 mid = (mid + Math.imul(al3, bh9)) | 0;
1638 mid = (mid + Math.imul(ah3, bl9)) | 0;
1639 hi = (hi + Math.imul(ah3, bh9)) | 0;
1640 var w12 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1641 c = (((hi + (mid >>> 13)) | 0) + (w12 >>> 26)) | 0;
1642 w12 &= 0x3ffffff;
1643 lo = Math.imul(al9, bl4);
1644 mid = Math.imul(al9, bh4);
1645 mid = (mid + Math.imul(ah9, bl4)) | 0;
1646 hi = Math.imul(ah9, bh4);
1647 lo = (lo + Math.imul(al8, bl5)) | 0;
1648 mid = (mid + Math.imul(al8, bh5)) | 0;
1649 mid = (mid + Math.imul(ah8, bl5)) | 0;
1650 hi = (hi + Math.imul(ah8, bh5)) | 0;
1651 lo = (lo + Math.imul(al7, bl6)) | 0;
1652 mid = (mid + Math.imul(al7, bh6)) | 0;
1653 mid = (mid + Math.imul(ah7, bl6)) | 0;
1654 hi = (hi + Math.imul(ah7, bh6)) | 0;
1655 lo = (lo + Math.imul(al6, bl7)) | 0;
1656 mid = (mid + Math.imul(al6, bh7)) | 0;
1657 mid = (mid + Math.imul(ah6, bl7)) | 0;
1658 hi = (hi + Math.imul(ah6, bh7)) | 0;
1659 lo = (lo + Math.imul(al5, bl8)) | 0;
1660 mid = (mid + Math.imul(al5, bh8)) | 0;
1661 mid = (mid + Math.imul(ah5, bl8)) | 0;
1662 hi = (hi + Math.imul(ah5, bh8)) | 0;
1663 lo = (lo + Math.imul(al4, bl9)) | 0;
1664 mid = (mid + Math.imul(al4, bh9)) | 0;
1665 mid = (mid + Math.imul(ah4, bl9)) | 0;
1666 hi = (hi + Math.imul(ah4, bh9)) | 0;
1667 var w13 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1668 c = (((hi + (mid >>> 13)) | 0) + (w13 >>> 26)) | 0;
1669 w13 &= 0x3ffffff;
1670 lo = Math.imul(al9, bl5);
1671 mid = Math.imul(al9, bh5);
1672 mid = (mid + Math.imul(ah9, bl5)) | 0;
1673 hi = Math.imul(ah9, bh5);
1674 lo = (lo + Math.imul(al8, bl6)) | 0;
1675 mid = (mid + Math.imul(al8, bh6)) | 0;
1676 mid = (mid + Math.imul(ah8, bl6)) | 0;
1677 hi = (hi + Math.imul(ah8, bh6)) | 0;
1678 lo = (lo + Math.imul(al7, bl7)) | 0;
1679 mid = (mid + Math.imul(al7, bh7)) | 0;
1680 mid = (mid + Math.imul(ah7, bl7)) | 0;
1681 hi = (hi + Math.imul(ah7, bh7)) | 0;
1682 lo = (lo + Math.imul(al6, bl8)) | 0;
1683 mid = (mid + Math.imul(al6, bh8)) | 0;
1684 mid = (mid + Math.imul(ah6, bl8)) | 0;
1685 hi = (hi + Math.imul(ah6, bh8)) | 0;
1686 lo = (lo + Math.imul(al5, bl9)) | 0;
1687 mid = (mid + Math.imul(al5, bh9)) | 0;
1688 mid = (mid + Math.imul(ah5, bl9)) | 0;
1689 hi = (hi + Math.imul(ah5, bh9)) | 0;
1690 var w14 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1691 c = (((hi + (mid >>> 13)) | 0) + (w14 >>> 26)) | 0;
1692 w14 &= 0x3ffffff;
1693 lo = Math.imul(al9, bl6);
1694 mid = Math.imul(al9, bh6);
1695 mid = (mid + Math.imul(ah9, bl6)) | 0;
1696 hi = Math.imul(ah9, bh6);
1697 lo = (lo + Math.imul(al8, bl7)) | 0;
1698 mid = (mid + Math.imul(al8, bh7)) | 0;
1699 mid = (mid + Math.imul(ah8, bl7)) | 0;
1700 hi = (hi + Math.imul(ah8, bh7)) | 0;
1701 lo = (lo + Math.imul(al7, bl8)) | 0;
1702 mid = (mid + Math.imul(al7, bh8)) | 0;
1703 mid = (mid + Math.imul(ah7, bl8)) | 0;
1704 hi = (hi + Math.imul(ah7, bh8)) | 0;
1705 lo = (lo + Math.imul(al6, bl9)) | 0;
1706 mid = (mid + Math.imul(al6, bh9)) | 0;
1707 mid = (mid + Math.imul(ah6, bl9)) | 0;
1708 hi = (hi + Math.imul(ah6, bh9)) | 0;
1709 var w15 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1710 c = (((hi + (mid >>> 13)) | 0) + (w15 >>> 26)) | 0;
1711 w15 &= 0x3ffffff;
1712 lo = Math.imul(al9, bl7);
1713 mid = Math.imul(al9, bh7);
1714 mid = (mid + Math.imul(ah9, bl7)) | 0;
1715 hi = Math.imul(ah9, bh7);
1716 lo = (lo + Math.imul(al8, bl8)) | 0;
1717 mid = (mid + Math.imul(al8, bh8)) | 0;
1718 mid = (mid + Math.imul(ah8, bl8)) | 0;
1719 hi = (hi + Math.imul(ah8, bh8)) | 0;
1720 lo = (lo + Math.imul(al7, bl9)) | 0;
1721 mid = (mid + Math.imul(al7, bh9)) | 0;
1722 mid = (mid + Math.imul(ah7, bl9)) | 0;
1723 hi = (hi + Math.imul(ah7, bh9)) | 0;
1724 var w16 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1725 c = (((hi + (mid >>> 13)) | 0) + (w16 >>> 26)) | 0;
1726 w16 &= 0x3ffffff;
1727 lo = Math.imul(al9, bl8);
1728 mid = Math.imul(al9, bh8);
1729 mid = (mid + Math.imul(ah9, bl8)) | 0;
1730 hi = Math.imul(ah9, bh8);
1731 lo = (lo + Math.imul(al8, bl9)) | 0;
1732 mid = (mid + Math.imul(al8, bh9)) | 0;
1733 mid = (mid + Math.imul(ah8, bl9)) | 0;
1734 hi = (hi + Math.imul(ah8, bh9)) | 0;
1735 var w17 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1736 c = (((hi + (mid >>> 13)) | 0) + (w17 >>> 26)) | 0;
1737 w17 &= 0x3ffffff;
1738 lo = Math.imul(al9, bl9);
1739 mid = Math.imul(al9, bh9);
1740 mid = (mid + Math.imul(ah9, bl9)) | 0;
1741 hi = Math.imul(ah9, bh9);
1742 var w18 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1743 c = (((hi + (mid >>> 13)) | 0) + (w18 >>> 26)) | 0;
1744 w18 &= 0x3ffffff;
1745 o[0] = w0;
1746 o[1] = w1;
1747 o[2] = w2;
1748 o[3] = w3;
1749 o[4] = w4;
1750 o[5] = w5;
1751 o[6] = w6;
1752 o[7] = w7;
1753 o[8] = w8;
1754 o[9] = w9;
1755 o[10] = w10;
1756 o[11] = w11;
1757 o[12] = w12;
1758 o[13] = w13;
1759 o[14] = w14;
1760 o[15] = w15;
1761 o[16] = w16;
1762 o[17] = w17;
1763 o[18] = w18;
1764 if (c !== 0) {
1765 o[19] = c;
1766 out.length++;
1767 }
1768 return out;
1769 };
1770 if (!Math.imul) {
1771 comb10MulTo = smallMulTo;
1772 }
1773 function bigMulTo (self, num, out) {
1774 out.negative = num.negative ^ self.negative;
1775 out.length = self.length + num.length;
1776 var carry = 0;
1777 var hncarry = 0;
1778 for (var k = 0; k < out.length - 1; k++) {
1779 var ncarry = hncarry;
1780 hncarry = 0;
1781 var rword = carry & 0x3ffffff;
1782 var maxJ = Math.min(k, num.length - 1);
1783 for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) {
1784 var i = k - j;
1785 var a = self.words[i] | 0;
1786 var b = num.words[j] | 0;
1787 var r = a * b;
1788 var lo = r & 0x3ffffff;
1789 ncarry = (ncarry + ((r / 0x4000000) | 0)) | 0;
1790 lo = (lo + rword) | 0;
1791 rword = lo & 0x3ffffff;
1792 ncarry = (ncarry + (lo >>> 26)) | 0;
1793 hncarry += ncarry >>> 26;
1794 ncarry &= 0x3ffffff;
1795 }
1796 out.words[k] = rword;
1797 carry = ncarry;
1798 ncarry = hncarry;
1799 }
1800 if (carry !== 0) {
1801 out.words[k] = carry;
1802 } else {
1803 out.length--;
1804 }
1805 return out._strip();
1806 }
1807 function jumboMulTo (self, num, out) {
1808 return bigMulTo(self, num, out);
1809 }
1810 BN.prototype.mulTo = function mulTo (num, out) {
1811 var res;
1812 var len = this.length + num.length;
1813 if (this.length === 10 && num.length === 10) {
1814 res = comb10MulTo(this, num, out);
1815 } else if (len < 63) {
1816 res = smallMulTo(this, num, out);
1817 } else if (len < 1024) {
1818 res = bigMulTo(this, num, out);
1819 } else {
1820 res = jumboMulTo(this, num, out);
1821 }
1822 return res;
1823 };
1824 BN.prototype.mul = function mul (num) {
1825 var out = new BN(null);
1826 out.words = new Array(this.length + num.length);
1827 return this.mulTo(num, out);
1828 };
1829 BN.prototype.mulf = function mulf (num) {
1830 var out = new BN(null);
1831 out.words = new Array(this.length + num.length);
1832 return jumboMulTo(this, num, out);
1833 };
1834 BN.prototype.imul = function imul (num) {
1835 return this.clone().mulTo(num, this);
1836 };
1837 BN.prototype.imuln = function imuln (num) {
1838 var isNegNum = num < 0;
1839 if (isNegNum) num = -num;
1840 assert(typeof num === 'number');
1841 assert(num < 0x4000000);
1842 var carry = 0;
1843 for (var i = 0; i < this.length; i++) {
1844 var w = (this.words[i] | 0) * num;
1845 var lo = (w & 0x3ffffff) + (carry & 0x3ffffff);
1846 carry >>= 26;
1847 carry += (w / 0x4000000) | 0;
1848 carry += lo >>> 26;
1849 this.words[i] = lo & 0x3ffffff;
1850 }
1851 if (carry !== 0) {
1852 this.words[i] = carry;
1853 this.length++;
1854 }
1855 return isNegNum ? this.ineg() : this;
1856 };
1857 BN.prototype.muln = function muln (num) {
1858 return this.clone().imuln(num);
1859 };
1860 BN.prototype.sqr = function sqr () {
1861 return this.mul(this);
1862 };
1863 BN.prototype.isqr = function isqr () {
1864 return this.imul(this.clone());
1865 };
1866 BN.prototype.pow = function pow (num) {
1867 var w = toBitArray(num);
1868 if (w.length === 0) return new BN(1);
1869 var res = this;
1870 for (var i = 0; i < w.length; i++, res = res.sqr()) {
1871 if (w[i] !== 0) break;
1872 }
1873 if (++i < w.length) {
1874 for (var q = res.sqr(); i < w.length; i++, q = q.sqr()) {
1875 if (w[i] === 0) continue;
1876 res = res.mul(q);
1877 }
1878 }
1879 return res;
1880 };
1881 BN.prototype.iushln = function iushln (bits) {
1882 assert(typeof bits === 'number' && bits >= 0);
1883 var r = bits % 26;
1884 var s = (bits - r) / 26;
1885 var carryMask = (0x3ffffff >>> (26 - r)) << (26 - r);
1886 var i;
1887 if (r !== 0) {
1888 var carry = 0;
1889 for (i = 0; i < this.length; i++) {
1890 var newCarry = this.words[i] & carryMask;
1891 var c = ((this.words[i] | 0) - newCarry) << r;
1892 this.words[i] = c | carry;
1893 carry = newCarry >>> (26 - r);
1894 }
1895 if (carry) {
1896 this.words[i] = carry;
1897 this.length++;
1898 }
1899 }
1900 if (s !== 0) {
1901 for (i = this.length - 1; i >= 0; i--) {
1902 this.words[i + s] = this.words[i];
1903 }
1904 for (i = 0; i < s; i++) {
1905 this.words[i] = 0;
1906 }
1907 this.length += s;
1908 }
1909 return this._strip();
1910 };
1911 BN.prototype.ishln = function ishln (bits) {
1912 assert(this.negative === 0);
1913 return this.iushln(bits);
1914 };
1915 BN.prototype.iushrn = function iushrn (bits, hint, extended) {
1916 assert(typeof bits === 'number' && bits >= 0);
1917 var h;
1918 if (hint) {
1919 h = (hint - (hint % 26)) / 26;
1920 } else {
1921 h = 0;
1922 }
1923 var r = bits % 26;
1924 var s = Math.min((bits - r) / 26, this.length);
1925 var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r);
1926 var maskedWords = extended;
1927 h -= s;
1928 h = Math.max(0, h);
1929 if (maskedWords) {
1930 for (var i = 0; i < s; i++) {
1931 maskedWords.words[i] = this.words[i];
1932 }
1933 maskedWords.length = s;
1934 }
1935 if (s === 0) ; else if (this.length > s) {
1936 this.length -= s;
1937 for (i = 0; i < this.length; i++) {
1938 this.words[i] = this.words[i + s];
1939 }
1940 } else {
1941 this.words[0] = 0;
1942 this.length = 1;
1943 }
1944 var carry = 0;
1945 for (i = this.length - 1; i >= 0 && (carry !== 0 || i >= h); i--) {
1946 var word = this.words[i] | 0;
1947 this.words[i] = (carry << (26 - r)) | (word >>> r);
1948 carry = word & mask;
1949 }
1950 if (maskedWords && carry !== 0) {
1951 maskedWords.words[maskedWords.length++] = carry;
1952 }
1953 if (this.length === 0) {
1954 this.words[0] = 0;
1955 this.length = 1;
1956 }
1957 return this._strip();
1958 };
1959 BN.prototype.ishrn = function ishrn (bits, hint, extended) {
1960 assert(this.negative === 0);
1961 return this.iushrn(bits, hint, extended);
1962 };
1963 BN.prototype.shln = function shln (bits) {
1964 return this.clone().ishln(bits);
1965 };
1966 BN.prototype.ushln = function ushln (bits) {
1967 return this.clone().iushln(bits);
1968 };
1969 BN.prototype.shrn = function shrn (bits) {
1970 return this.clone().ishrn(bits);
1971 };
1972 BN.prototype.ushrn = function ushrn (bits) {
1973 return this.clone().iushrn(bits);
1974 };
1975 BN.prototype.testn = function testn (bit) {
1976 assert(typeof bit === 'number' && bit >= 0);
1977 var r = bit % 26;
1978 var s = (bit - r) / 26;
1979 var q = 1 << r;
1980 if (this.length <= s) return false;
1981 var w = this.words[s];
1982 return !!(w & q);
1983 };
1984 BN.prototype.imaskn = function imaskn (bits) {
1985 assert(typeof bits === 'number' && bits >= 0);
1986 var r = bits % 26;
1987 var s = (bits - r) / 26;
1988 assert(this.negative === 0, 'imaskn works only with positive numbers');
1989 if (this.length <= s) {
1990 return this;
1991 }
1992 if (r !== 0) {
1993 s++;
1994 }
1995 this.length = Math.min(s, this.length);
1996 if (r !== 0) {
1997 var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r);
1998 this.words[this.length - 1] &= mask;
1999 }
2000 return this._strip();
2001 };
2002 BN.prototype.maskn = function maskn (bits) {
2003 return this.clone().imaskn(bits);
2004 };
2005 BN.prototype.iaddn = function iaddn (num) {
2006 assert(typeof num === 'number');
2007 assert(num < 0x4000000);
2008 if (num < 0) return this.isubn(-num);
2009 if (this.negative !== 0) {
2010 if (this.length === 1 && (this.words[0] | 0) <= num) {
2011 this.words[0] = num - (this.words[0] | 0);
2012 this.negative = 0;
2013 return this;
2014 }
2015 this.negative = 0;
2016 this.isubn(num);
2017 this.negative = 1;
2018 return this;
2019 }
2020 return this._iaddn(num);
2021 };
2022 BN.prototype._iaddn = function _iaddn (num) {
2023 this.words[0] += num;
2024 for (var i = 0; i < this.length && this.words[i] >= 0x4000000; i++) {
2025 this.words[i] -= 0x4000000;
2026 if (i === this.length - 1) {
2027 this.words[i + 1] = 1;
2028 } else {
2029 this.words[i + 1]++;
2030 }
2031 }
2032 this.length = Math.max(this.length, i + 1);
2033 return this;
2034 };
2035 BN.prototype.isubn = function isubn (num) {
2036 assert(typeof num === 'number');
2037 assert(num < 0x4000000);
2038 if (num < 0) return this.iaddn(-num);
2039 if (this.negative !== 0) {
2040 this.negative = 0;
2041 this.iaddn(num);
2042 this.negative = 1;
2043 return this;
2044 }
2045 this.words[0] -= num;
2046 if (this.length === 1 && this.words[0] < 0) {
2047 this.words[0] = -this.words[0];
2048 this.negative = 1;
2049 } else {
2050 for (var i = 0; i < this.length && this.words[i] < 0; i++) {
2051 this.words[i] += 0x4000000;
2052 this.words[i + 1] -= 1;
2053 }
2054 }
2055 return this._strip();
2056 };
2057 BN.prototype.addn = function addn (num) {
2058 return this.clone().iaddn(num);
2059 };
2060 BN.prototype.subn = function subn (num) {
2061 return this.clone().isubn(num);
2062 };
2063 BN.prototype.iabs = function iabs () {
2064 this.negative = 0;
2065 return this;
2066 };
2067 BN.prototype.abs = function abs () {
2068 return this.clone().iabs();
2069 };
2070 BN.prototype._ishlnsubmul = function _ishlnsubmul (num, mul, shift) {
2071 var len = num.length + shift;
2072 var i;
2073 this._expand(len);
2074 var w;
2075 var carry = 0;
2076 for (i = 0; i < num.length; i++) {
2077 w = (this.words[i + shift] | 0) + carry;
2078 var right = (num.words[i] | 0) * mul;
2079 w -= right & 0x3ffffff;
2080 carry = (w >> 26) - ((right / 0x4000000) | 0);
2081 this.words[i + shift] = w & 0x3ffffff;
2082 }
2083 for (; i < this.length - shift; i++) {
2084 w = (this.words[i + shift] | 0) + carry;
2085 carry = w >> 26;
2086 this.words[i + shift] = w & 0x3ffffff;
2087 }
2088 if (carry === 0) return this._strip();
2089 assert(carry === -1);
2090 carry = 0;
2091 for (i = 0; i < this.length; i++) {
2092 w = -(this.words[i] | 0) + carry;
2093 carry = w >> 26;
2094 this.words[i] = w & 0x3ffffff;
2095 }
2096 this.negative = 1;
2097 return this._strip();
2098 };
2099 BN.prototype._wordDiv = function _wordDiv (num, mode) {
2100 var shift = this.length - num.length;
2101 var a = this.clone();
2102 var b = num;
2103 var bhi = b.words[b.length - 1] | 0;
2104 var bhiBits = this._countBits(bhi);
2105 shift = 26 - bhiBits;
2106 if (shift !== 0) {
2107 b = b.ushln(shift);
2108 a.iushln(shift);
2109 bhi = b.words[b.length - 1] | 0;
2110 }
2111 var m = a.length - b.length;
2112 var q;
2113 if (mode !== 'mod') {
2114 q = new BN(null);
2115 q.length = m + 1;
2116 q.words = new Array(q.length);
2117 for (var i = 0; i < q.length; i++) {
2118 q.words[i] = 0;
2119 }
2120 }
2121 var diff = a.clone()._ishlnsubmul(b, 1, m);
2122 if (diff.negative === 0) {
2123 a = diff;
2124 if (q) {
2125 q.words[m] = 1;
2126 }
2127 }
2128 for (var j = m - 1; j >= 0; j--) {
2129 var qj = (a.words[b.length + j] | 0) * 0x4000000 +
2130 (a.words[b.length + j - 1] | 0);
2131 qj = Math.min((qj / bhi) | 0, 0x3ffffff);
2132 a._ishlnsubmul(b, qj, j);
2133 while (a.negative !== 0) {
2134 qj--;
2135 a.negative = 0;
2136 a._ishlnsubmul(b, 1, j);
2137 if (!a.isZero()) {
2138 a.negative ^= 1;
2139 }
2140 }
2141 if (q) {
2142 q.words[j] = qj;
2143 }
2144 }
2145 if (q) {
2146 q._strip();
2147 }
2148 a._strip();
2149 if (mode !== 'div' && shift !== 0) {
2150 a.iushrn(shift);
2151 }
2152 return {
2153 div: q || null,
2154 mod: a
2155 };
2156 };
2157 BN.prototype.divmod = function divmod (num, mode, positive) {
2158 assert(!num.isZero());
2159 if (this.isZero()) {
2160 return {
2161 div: new BN(0),
2162 mod: new BN(0)
2163 };
2164 }
2165 var div, mod, res;
2166 if (this.negative !== 0 && num.negative === 0) {
2167 res = this.neg().divmod(num, mode);
2168 if (mode !== 'mod') {
2169 div = res.div.neg();
2170 }
2171 if (mode !== 'div') {
2172 mod = res.mod.neg();
2173 if (positive && mod.negative !== 0) {
2174 mod.iadd(num);
2175 }
2176 }
2177 return {
2178 div: div,
2179 mod: mod
2180 };
2181 }
2182 if (this.negative === 0 && num.negative !== 0) {
2183 res = this.divmod(num.neg(), mode);
2184 if (mode !== 'mod') {
2185 div = res.div.neg();
2186 }
2187 return {
2188 div: div,
2189 mod: res.mod
2190 };
2191 }
2192 if ((this.negative & num.negative) !== 0) {
2193 res = this.neg().divmod(num.neg(), mode);
2194 if (mode !== 'div') {
2195 mod = res.mod.neg();
2196 if (positive && mod.negative !== 0) {
2197 mod.isub(num);
2198 }
2199 }
2200 return {
2201 div: res.div,
2202 mod: mod
2203 };
2204 }
2205 if (num.length > this.length || this.cmp(num) < 0) {
2206 return {
2207 div: new BN(0),
2208 mod: this
2209 };
2210 }
2211 if (num.length === 1) {
2212 if (mode === 'div') {
2213 return {
2214 div: this.divn(num.words[0]),
2215 mod: null
2216 };
2217 }
2218 if (mode === 'mod') {
2219 return {
2220 div: null,
2221 mod: new BN(this.modrn(num.words[0]))
2222 };
2223 }
2224 return {
2225 div: this.divn(num.words[0]),
2226 mod: new BN(this.modrn(num.words[0]))
2227 };
2228 }
2229 return this._wordDiv(num, mode);
2230 };
2231 BN.prototype.div = function div (num) {
2232 return this.divmod(num, 'div', false).div;
2233 };
2234 BN.prototype.mod = function mod (num) {
2235 return this.divmod(num, 'mod', false).mod;
2236 };
2237 BN.prototype.umod = function umod (num) {
2238 return this.divmod(num, 'mod', true).mod;
2239 };
2240 BN.prototype.divRound = function divRound (num) {
2241 var dm = this.divmod(num);
2242 if (dm.mod.isZero()) return dm.div;
2243 var mod = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod;
2244 var half = num.ushrn(1);
2245 var r2 = num.andln(1);
2246 var cmp = mod.cmp(half);
2247 if (cmp < 0 || (r2 === 1 && cmp === 0)) return dm.div;
2248 return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1);
2249 };
2250 BN.prototype.modrn = function modrn (num) {
2251 var isNegNum = num < 0;
2252 if (isNegNum) num = -num;
2253 assert(num <= 0x3ffffff);
2254 var p = (1 << 26) % num;
2255 var acc = 0;
2256 for (var i = this.length - 1; i >= 0; i--) {
2257 acc = (p * acc + (this.words[i] | 0)) % num;
2258 }
2259 return isNegNum ? -acc : acc;
2260 };
2261 BN.prototype.modn = function modn (num) {
2262 return this.modrn(num);
2263 };
2264 BN.prototype.idivn = function idivn (num) {
2265 var isNegNum = num < 0;
2266 if (isNegNum) num = -num;
2267 assert(num <= 0x3ffffff);
2268 var carry = 0;
2269 for (var i = this.length - 1; i >= 0; i--) {
2270 var w = (this.words[i] | 0) + carry * 0x4000000;
2271 this.words[i] = (w / num) | 0;
2272 carry = w % num;
2273 }
2274 this._strip();
2275 return isNegNum ? this.ineg() : this;
2276 };
2277 BN.prototype.divn = function divn (num) {
2278 return this.clone().idivn(num);
2279 };
2280 BN.prototype.egcd = function egcd (p) {
2281 assert(p.negative === 0);
2282 assert(!p.isZero());
2283 var x = this;
2284 var y = p.clone();
2285 if (x.negative !== 0) {
2286 x = x.umod(p);
2287 } else {
2288 x = x.clone();
2289 }
2290 var A = new BN(1);
2291 var B = new BN(0);
2292 var C = new BN(0);
2293 var D = new BN(1);
2294 var g = 0;
2295 while (x.isEven() && y.isEven()) {
2296 x.iushrn(1);
2297 y.iushrn(1);
2298 ++g;
2299 }
2300 var yp = y.clone();
2301 var xp = x.clone();
2302 while (!x.isZero()) {
2303 for (var i = 0, im = 1; (x.words[0] & im) === 0 && i < 26; ++i, im <<= 1);
2304 if (i > 0) {
2305 x.iushrn(i);
2306 while (i-- > 0) {
2307 if (A.isOdd() || B.isOdd()) {
2308 A.iadd(yp);
2309 B.isub(xp);
2310 }
2311 A.iushrn(1);
2312 B.iushrn(1);
2313 }
2314 }
2315 for (var j = 0, jm = 1; (y.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1);
2316 if (j > 0) {
2317 y.iushrn(j);
2318 while (j-- > 0) {
2319 if (C.isOdd() || D.isOdd()) {
2320 C.iadd(yp);
2321 D.isub(xp);
2322 }
2323 C.iushrn(1);
2324 D.iushrn(1);
2325 }
2326 }
2327 if (x.cmp(y) >= 0) {
2328 x.isub(y);
2329 A.isub(C);
2330 B.isub(D);
2331 } else {
2332 y.isub(x);
2333 C.isub(A);
2334 D.isub(B);
2335 }
2336 }
2337 return {
2338 a: C,
2339 b: D,
2340 gcd: y.iushln(g)
2341 };
2342 };
2343 BN.prototype._invmp = function _invmp (p) {
2344 assert(p.negative === 0);
2345 assert(!p.isZero());
2346 var a = this;
2347 var b = p.clone();
2348 if (a.negative !== 0) {
2349 a = a.umod(p);
2350 } else {
2351 a = a.clone();
2352 }
2353 var x1 = new BN(1);
2354 var x2 = new BN(0);
2355 var delta = b.clone();
2356 while (a.cmpn(1) > 0 && b.cmpn(1) > 0) {
2357 for (var i = 0, im = 1; (a.words[0] & im) === 0 && i < 26; ++i, im <<= 1);
2358 if (i > 0) {
2359 a.iushrn(i);
2360 while (i-- > 0) {
2361 if (x1.isOdd()) {
2362 x1.iadd(delta);
2363 }
2364 x1.iushrn(1);
2365 }
2366 }
2367 for (var j = 0, jm = 1; (b.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1);
2368 if (j > 0) {
2369 b.iushrn(j);
2370 while (j-- > 0) {
2371 if (x2.isOdd()) {
2372 x2.iadd(delta);
2373 }
2374 x2.iushrn(1);
2375 }
2376 }
2377 if (a.cmp(b) >= 0) {
2378 a.isub(b);
2379 x1.isub(x2);
2380 } else {
2381 b.isub(a);
2382 x2.isub(x1);
2383 }
2384 }
2385 var res;
2386 if (a.cmpn(1) === 0) {
2387 res = x1;
2388 } else {
2389 res = x2;
2390 }
2391 if (res.cmpn(0) < 0) {
2392 res.iadd(p);
2393 }
2394 return res;
2395 };
2396 BN.prototype.gcd = function gcd (num) {
2397 if (this.isZero()) return num.abs();
2398 if (num.isZero()) return this.abs();
2399 var a = this.clone();
2400 var b = num.clone();
2401 a.negative = 0;
2402 b.negative = 0;
2403 for (var shift = 0; a.isEven() && b.isEven(); shift++) {
2404 a.iushrn(1);
2405 b.iushrn(1);
2406 }
2407 do {
2408 while (a.isEven()) {
2409 a.iushrn(1);
2410 }
2411 while (b.isEven()) {
2412 b.iushrn(1);
2413 }
2414 var r = a.cmp(b);
2415 if (r < 0) {
2416 var t = a;
2417 a = b;
2418 b = t;
2419 } else if (r === 0 || b.cmpn(1) === 0) {
2420 break;
2421 }
2422 a.isub(b);
2423 } while (true);
2424 return b.iushln(shift);
2425 };
2426 BN.prototype.invm = function invm (num) {
2427 return this.egcd(num).a.umod(num);
2428 };
2429 BN.prototype.isEven = function isEven () {
2430 return (this.words[0] & 1) === 0;
2431 };
2432 BN.prototype.isOdd = function isOdd () {
2433 return (this.words[0] & 1) === 1;
2434 };
2435 BN.prototype.andln = function andln (num) {
2436 return this.words[0] & num;
2437 };
2438 BN.prototype.bincn = function bincn (bit) {
2439 assert(typeof bit === 'number');
2440 var r = bit % 26;
2441 var s = (bit - r) / 26;
2442 var q = 1 << r;
2443 if (this.length <= s) {
2444 this._expand(s + 1);
2445 this.words[s] |= q;
2446 return this;
2447 }
2448 var carry = q;
2449 for (var i = s; carry !== 0 && i < this.length; i++) {
2450 var w = this.words[i] | 0;
2451 w += carry;
2452 carry = w >>> 26;
2453 w &= 0x3ffffff;
2454 this.words[i] = w;
2455 }
2456 if (carry !== 0) {
2457 this.words[i] = carry;
2458 this.length++;
2459 }
2460 return this;
2461 };
2462 BN.prototype.isZero = function isZero () {
2463 return this.length === 1 && this.words[0] === 0;
2464 };
2465 BN.prototype.cmpn = function cmpn (num) {
2466 var negative = num < 0;
2467 if (this.negative !== 0 && !negative) return -1;
2468 if (this.negative === 0 && negative) return 1;
2469 this._strip();
2470 var res;
2471 if (this.length > 1) {
2472 res = 1;
2473 } else {
2474 if (negative) {
2475 num = -num;
2476 }
2477 assert(num <= 0x3ffffff, 'Number is too big');
2478 var w = this.words[0] | 0;
2479 res = w === num ? 0 : w < num ? -1 : 1;
2480 }
2481 if (this.negative !== 0) return -res | 0;
2482 return res;
2483 };
2484 BN.prototype.cmp = function cmp (num) {
2485 if (this.negative !== 0 && num.negative === 0) return -1;
2486 if (this.negative === 0 && num.negative !== 0) return 1;
2487 var res = this.ucmp(num);
2488 if (this.negative !== 0) return -res | 0;
2489 return res;
2490 };
2491 BN.prototype.ucmp = function ucmp (num) {
2492 if (this.length > num.length) return 1;
2493 if (this.length < num.length) return -1;
2494 var res = 0;
2495 for (var i = this.length - 1; i >= 0; i--) {
2496 var a = this.words[i] | 0;
2497 var b = num.words[i] | 0;
2498 if (a === b) continue;
2499 if (a < b) {
2500 res = -1;
2501 } else if (a > b) {
2502 res = 1;
2503 }
2504 break;
2505 }
2506 return res;
2507 };
2508 BN.prototype.gtn = function gtn (num) {
2509 return this.cmpn(num) === 1;
2510 };
2511 BN.prototype.gt = function gt (num) {
2512 return this.cmp(num) === 1;
2513 };
2514 BN.prototype.gten = function gten (num) {
2515 return this.cmpn(num) >= 0;
2516 };
2517 BN.prototype.gte = function gte (num) {
2518 return this.cmp(num) >= 0;
2519 };
2520 BN.prototype.ltn = function ltn (num) {
2521 return this.cmpn(num) === -1;
2522 };
2523 BN.prototype.lt = function lt (num) {
2524 return this.cmp(num) === -1;
2525 };
2526 BN.prototype.lten = function lten (num) {
2527 return this.cmpn(num) <= 0;
2528 };
2529 BN.prototype.lte = function lte (num) {
2530 return this.cmp(num) <= 0;
2531 };
2532 BN.prototype.eqn = function eqn (num) {
2533 return this.cmpn(num) === 0;
2534 };
2535 BN.prototype.eq = function eq (num) {
2536 return this.cmp(num) === 0;
2537 };
2538 BN.red = function red (num) {
2539 return new Red(num);
2540 };
2541 BN.prototype.toRed = function toRed (ctx) {
2542 assert(!this.red, 'Already a number in reduction context');
2543 assert(this.negative === 0, 'red works only with positives');
2544 return ctx.convertTo(this)._forceRed(ctx);
2545 };
2546 BN.prototype.fromRed = function fromRed () {
2547 assert(this.red, 'fromRed works only with numbers in reduction context');
2548 return this.red.convertFrom(this);
2549 };
2550 BN.prototype._forceRed = function _forceRed (ctx) {
2551 this.red = ctx;
2552 return this;
2553 };
2554 BN.prototype.forceRed = function forceRed (ctx) {
2555 assert(!this.red, 'Already a number in reduction context');
2556 return this._forceRed(ctx);
2557 };
2558 BN.prototype.redAdd = function redAdd (num) {
2559 assert(this.red, 'redAdd works only with red numbers');
2560 return this.red.add(this, num);
2561 };
2562 BN.prototype.redIAdd = function redIAdd (num) {
2563 assert(this.red, 'redIAdd works only with red numbers');
2564 return this.red.iadd(this, num);
2565 };
2566 BN.prototype.redSub = function redSub (num) {
2567 assert(this.red, 'redSub works only with red numbers');
2568 return this.red.sub(this, num);
2569 };
2570 BN.prototype.redISub = function redISub (num) {
2571 assert(this.red, 'redISub works only with red numbers');
2572 return this.red.isub(this, num);
2573 };
2574 BN.prototype.redShl = function redShl (num) {
2575 assert(this.red, 'redShl works only with red numbers');
2576 return this.red.shl(this, num);
2577 };
2578 BN.prototype.redMul = function redMul (num) {
2579 assert(this.red, 'redMul works only with red numbers');
2580 this.red._verify2(this, num);
2581 return this.red.mul(this, num);
2582 };
2583 BN.prototype.redIMul = function redIMul (num) {
2584 assert(this.red, 'redMul works only with red numbers');
2585 this.red._verify2(this, num);
2586 return this.red.imul(this, num);
2587 };
2588 BN.prototype.redSqr = function redSqr () {
2589 assert(this.red, 'redSqr works only with red numbers');
2590 this.red._verify1(this);
2591 return this.red.sqr(this);
2592 };
2593 BN.prototype.redISqr = function redISqr () {
2594 assert(this.red, 'redISqr works only with red numbers');
2595 this.red._verify1(this);
2596 return this.red.isqr(this);
2597 };
2598 BN.prototype.redSqrt = function redSqrt () {
2599 assert(this.red, 'redSqrt works only with red numbers');
2600 this.red._verify1(this);
2601 return this.red.sqrt(this);
2602 };
2603 BN.prototype.redInvm = function redInvm () {
2604 assert(this.red, 'redInvm works only with red numbers');
2605 this.red._verify1(this);
2606 return this.red.invm(this);
2607 };
2608 BN.prototype.redNeg = function redNeg () {
2609 assert(this.red, 'redNeg works only with red numbers');
2610 this.red._verify1(this);
2611 return this.red.neg(this);
2612 };
2613 BN.prototype.redPow = function redPow (num) {
2614 assert(this.red && !num.red, 'redPow(normalNum)');
2615 this.red._verify1(this);
2616 return this.red.pow(this, num);
2617 };
2618 var primes = {
2619 k256: null,
2620 p224: null,
2621 p192: null,
2622 p25519: null
2623 };
2624 function MPrime (name, p) {
2625 this.name = name;
2626 this.p = new BN(p, 16);
2627 this.n = this.p.bitLength();
2628 this.k = new BN(1).iushln(this.n).isub(this.p);
2629 this.tmp = this._tmp();
2630 }
2631 MPrime.prototype._tmp = function _tmp () {
2632 var tmp = new BN(null);
2633 tmp.words = new Array(Math.ceil(this.n / 13));
2634 return tmp;
2635 };
2636 MPrime.prototype.ireduce = function ireduce (num) {
2637 var r = num;
2638 var rlen;
2639 do {
2640 this.split(r, this.tmp);
2641 r = this.imulK(r);
2642 r = r.iadd(this.tmp);
2643 rlen = r.bitLength();
2644 } while (rlen > this.n);
2645 var cmp = rlen < this.n ? -1 : r.ucmp(this.p);
2646 if (cmp === 0) {
2647 r.words[0] = 0;
2648 r.length = 1;
2649 } else if (cmp > 0) {
2650 r.isub(this.p);
2651 } else {
2652 if (r.strip !== undefined) {
2653 r.strip();
2654 } else {
2655 r._strip();
2656 }
2657 }
2658 return r;
2659 };
2660 MPrime.prototype.split = function split (input, out) {
2661 input.iushrn(this.n, 0, out);
2662 };
2663 MPrime.prototype.imulK = function imulK (num) {
2664 return num.imul(this.k);
2665 };
2666 function K256 () {
2667 MPrime.call(
2668 this,
2669 'k256',
2670 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f');
2671 }
2672 inherits(K256, MPrime);
2673 K256.prototype.split = function split (input, output) {
2674 var mask = 0x3fffff;
2675 var outLen = Math.min(input.length, 9);
2676 for (var i = 0; i < outLen; i++) {
2677 output.words[i] = input.words[i];
2678 }
2679 output.length = outLen;
2680 if (input.length <= 9) {
2681 input.words[0] = 0;
2682 input.length = 1;
2683 return;
2684 }
2685 var prev = input.words[9];
2686 output.words[output.length++] = prev & mask;
2687 for (i = 10; i < input.length; i++) {
2688 var next = input.words[i] | 0;
2689 input.words[i - 10] = ((next & mask) << 4) | (prev >>> 22);
2690 prev = next;
2691 }
2692 prev >>>= 22;
2693 input.words[i - 10] = prev;
2694 if (prev === 0 && input.length > 10) {
2695 input.length -= 10;
2696 } else {
2697 input.length -= 9;
2698 }
2699 };
2700 K256.prototype.imulK = function imulK (num) {
2701 num.words[num.length] = 0;
2702 num.words[num.length + 1] = 0;
2703 num.length += 2;
2704 var lo = 0;
2705 for (var i = 0; i < num.length; i++) {
2706 var w = num.words[i] | 0;
2707 lo += w * 0x3d1;
2708 num.words[i] = lo & 0x3ffffff;
2709 lo = w * 0x40 + ((lo / 0x4000000) | 0);
2710 }
2711 if (num.words[num.length - 1] === 0) {
2712 num.length--;
2713 if (num.words[num.length - 1] === 0) {
2714 num.length--;
2715 }
2716 }
2717 return num;
2718 };
2719 function P224 () {
2720 MPrime.call(
2721 this,
2722 'p224',
2723 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001');
2724 }
2725 inherits(P224, MPrime);
2726 function P192 () {
2727 MPrime.call(
2728 this,
2729 'p192',
2730 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff');
2731 }
2732 inherits(P192, MPrime);
2733 function P25519 () {
2734 MPrime.call(
2735 this,
2736 '25519',
2737 '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed');
2738 }
2739 inherits(P25519, MPrime);
2740 P25519.prototype.imulK = function imulK (num) {
2741 var carry = 0;
2742 for (var i = 0; i < num.length; i++) {
2743 var hi = (num.words[i] | 0) * 0x13 + carry;
2744 var lo = hi & 0x3ffffff;
2745 hi >>>= 26;
2746 num.words[i] = lo;
2747 carry = hi;
2748 }
2749 if (carry !== 0) {
2750 num.words[num.length++] = carry;
2751 }
2752 return num;
2753 };
2754 BN._prime = function prime (name) {
2755 if (primes[name]) return primes[name];
2756 var prime;
2757 if (name === 'k256') {
2758 prime = new K256();
2759 } else if (name === 'p224') {
2760 prime = new P224();
2761 } else if (name === 'p192') {
2762 prime = new P192();
2763 } else if (name === 'p25519') {
2764 prime = new P25519();
2765 } else {
2766 throw new Error('Unknown prime ' + name);
2767 }
2768 primes[name] = prime;
2769 return prime;
2770 };
2771 function Red (m) {
2772 if (typeof m === 'string') {
2773 var prime = BN._prime(m);
2774 this.m = prime.p;
2775 this.prime = prime;
2776 } else {
2777 assert(m.gtn(1), 'modulus must be greater than 1');
2778 this.m = m;
2779 this.prime = null;
2780 }
2781 }
2782 Red.prototype._verify1 = function _verify1 (a) {
2783 assert(a.negative === 0, 'red works only with positives');
2784 assert(a.red, 'red works only with red numbers');
2785 };
2786 Red.prototype._verify2 = function _verify2 (a, b) {
2787 assert((a.negative | b.negative) === 0, 'red works only with positives');
2788 assert(a.red && a.red === b.red,
2789 'red works only with red numbers');
2790 };
2791 Red.prototype.imod = function imod (a) {
2792 if (this.prime) return this.prime.ireduce(a)._forceRed(this);
2793 move(a, a.umod(this.m)._forceRed(this));
2794 return a;
2795 };
2796 Red.prototype.neg = function neg (a) {
2797 if (a.isZero()) {
2798 return a.clone();
2799 }
2800 return this.m.sub(a)._forceRed(this);
2801 };
2802 Red.prototype.add = function add (a, b) {
2803 this._verify2(a, b);
2804 var res = a.add(b);
2805 if (res.cmp(this.m) >= 0) {
2806 res.isub(this.m);
2807 }
2808 return res._forceRed(this);
2809 };
2810 Red.prototype.iadd = function iadd (a, b) {
2811 this._verify2(a, b);
2812 var res = a.iadd(b);
2813 if (res.cmp(this.m) >= 0) {
2814 res.isub(this.m);
2815 }
2816 return res;
2817 };
2818 Red.prototype.sub = function sub (a, b) {
2819 this._verify2(a, b);
2820 var res = a.sub(b);
2821 if (res.cmpn(0) < 0) {
2822 res.iadd(this.m);
2823 }
2824 return res._forceRed(this);
2825 };
2826 Red.prototype.isub = function isub (a, b) {
2827 this._verify2(a, b);
2828 var res = a.isub(b);
2829 if (res.cmpn(0) < 0) {
2830 res.iadd(this.m);
2831 }
2832 return res;
2833 };
2834 Red.prototype.shl = function shl (a, num) {
2835 this._verify1(a);
2836 return this.imod(a.ushln(num));
2837 };
2838 Red.prototype.imul = function imul (a, b) {
2839 this._verify2(a, b);
2840 return this.imod(a.imul(b));
2841 };
2842 Red.prototype.mul = function mul (a, b) {
2843 this._verify2(a, b);
2844 return this.imod(a.mul(b));
2845 };
2846 Red.prototype.isqr = function isqr (a) {
2847 return this.imul(a, a.clone());
2848 };
2849 Red.prototype.sqr = function sqr (a) {
2850 return this.mul(a, a);
2851 };
2852 Red.prototype.sqrt = function sqrt (a) {
2853 if (a.isZero()) return a.clone();
2854 var mod3 = this.m.andln(3);
2855 assert(mod3 % 2 === 1);
2856 if (mod3 === 3) {
2857 var pow = this.m.add(new BN(1)).iushrn(2);
2858 return this.pow(a, pow);
2859 }
2860 var q = this.m.subn(1);
2861 var s = 0;
2862 while (!q.isZero() && q.andln(1) === 0) {
2863 s++;
2864 q.iushrn(1);
2865 }
2866 assert(!q.isZero());
2867 var one = new BN(1).toRed(this);
2868 var nOne = one.redNeg();
2869 var lpow = this.m.subn(1).iushrn(1);
2870 var z = this.m.bitLength();
2871 z = new BN(2 * z * z).toRed(this);
2872 while (this.pow(z, lpow).cmp(nOne) !== 0) {
2873 z.redIAdd(nOne);
2874 }
2875 var c = this.pow(z, q);
2876 var r = this.pow(a, q.addn(1).iushrn(1));
2877 var t = this.pow(a, q);
2878 var m = s;
2879 while (t.cmp(one) !== 0) {
2880 var tmp = t;
2881 for (var i = 0; tmp.cmp(one) !== 0; i++) {
2882 tmp = tmp.redSqr();
2883 }
2884 assert(i < m);
2885 var b = this.pow(c, new BN(1).iushln(m - i - 1));
2886 r = r.redMul(b);
2887 c = b.redSqr();
2888 t = t.redMul(c);
2889 m = i;
2890 }
2891 return r;
2892 };
2893 Red.prototype.invm = function invm (a) {
2894 var inv = a._invmp(this.m);
2895 if (inv.negative !== 0) {
2896 inv.negative = 0;
2897 return this.imod(inv).redNeg();
2898 } else {
2899 return this.imod(inv);
2900 }
2901 };
2902 Red.prototype.pow = function pow (a, num) {
2903 if (num.isZero()) return new BN(1).toRed(this);
2904 if (num.cmpn(1) === 0) return a.clone();
2905 var windowSize = 4;
2906 var wnd = new Array(1 << windowSize);
2907 wnd[0] = new BN(1).toRed(this);
2908 wnd[1] = a;
2909 for (var i = 2; i < wnd.length; i++) {
2910 wnd[i] = this.mul(wnd[i - 1], a);
2911 }
2912 var res = wnd[0];
2913 var current = 0;
2914 var currentLen = 0;
2915 var start = num.bitLength() % 26;
2916 if (start === 0) {
2917 start = 26;
2918 }
2919 for (i = num.length - 1; i >= 0; i--) {
2920 var word = num.words[i];
2921 for (var j = start - 1; j >= 0; j--) {
2922 var bit = (word >> j) & 1;
2923 if (res !== wnd[0]) {
2924 res = this.sqr(res);
2925 }
2926 if (bit === 0 && current === 0) {
2927 currentLen = 0;
2928 continue;
2929 }
2930 current <<= 1;
2931 current |= bit;
2932 currentLen++;
2933 if (currentLen !== windowSize && (i !== 0 || j !== 0)) continue;
2934 res = this.mul(res, wnd[current]);
2935 currentLen = 0;
2936 current = 0;
2937 }
2938 start = 26;
2939 }
2940 return res;
2941 };
2942 Red.prototype.convertTo = function convertTo (num) {
2943 var r = num.umod(this.m);
2944 return r === num ? r.clone() : r;
2945 };
2946 Red.prototype.convertFrom = function convertFrom (num) {
2947 var res = num.clone();
2948 res.red = null;
2949 return res;
2950 };
2951 BN.mont = function mont (num) {
2952 return new Mont(num);
2953 };
2954 function Mont (m) {
2955 Red.call(this, m);
2956 this.shift = this.m.bitLength();
2957 if (this.shift % 26 !== 0) {
2958 this.shift += 26 - (this.shift % 26);
2959 }
2960 this.r = new BN(1).iushln(this.shift);
2961 this.r2 = this.imod(this.r.sqr());
2962 this.rinv = this.r._invmp(this.m);
2963 this.minv = this.rinv.mul(this.r).isubn(1).div(this.m);
2964 this.minv = this.minv.umod(this.r);
2965 this.minv = this.r.sub(this.minv);
2966 }
2967 inherits(Mont, Red);
2968 Mont.prototype.convertTo = function convertTo (num) {
2969 return this.imod(num.ushln(this.shift));
2970 };
2971 Mont.prototype.convertFrom = function convertFrom (num) {
2972 var r = this.imod(num.mul(this.rinv));
2973 r.red = null;
2974 return r;
2975 };
2976 Mont.prototype.imul = function imul (a, b) {
2977 if (a.isZero() || b.isZero()) {
2978 a.words[0] = 0;
2979 a.length = 1;
2980 return a;
2981 }
2982 var t = a.imul(b);
2983 var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m);
2984 var u = t.isub(c).iushrn(this.shift);
2985 var res = u;
2986 if (u.cmp(this.m) >= 0) {
2987 res = u.isub(this.m);
2988 } else if (u.cmpn(0) < 0) {
2989 res = u.iadd(this.m);
2990 }
2991 return res._forceRed(this);
2992 };
2993 Mont.prototype.mul = function mul (a, b) {
2994 if (a.isZero() || b.isZero()) return new BN(0)._forceRed(this);
2995 var t = a.mul(b);
2996 var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m);
2997 var u = t.isub(c).iushrn(this.shift);
2998 var res = u;
2999 if (u.cmp(this.m) >= 0) {
3000 res = u.isub(this.m);
3001 } else if (u.cmpn(0) < 0) {
3002 res = u.iadd(this.m);
3003 }
3004 return res._forceRed(this);
3005 };
3006 Mont.prototype.invm = function invm (a) {
3007 var res = this.imod(a._invmp(this.m).mul(this.r2));
3008 return res._forceRed(this);
3009 };
3010 })(module, commonjsGlobal);
3011 } (bn));
3012 var bnExports = bn.exports;
3013 const BN = getDefaultExportFromCjs(bnExports);
3014
3015 function isBn(value) {
3016 return BN.isBN(value);
3017 }
3018
3019 const REGEX_HEX_PREFIXED = /^0x[\da-fA-F]+$/;
3020 const REGEX_HEX_NOPREFIX = /^[\da-fA-F]+$/;
3021 function isHex(value, bitLength = -1, ignoreLength) {
3022 return (typeof value === 'string' && (value === '0x' ||
3023 REGEX_HEX_PREFIXED.test(value))) && (bitLength === -1
3024 ? (ignoreLength || (value.length % 2 === 0))
3025 : (value.length === (2 + Math.ceil(bitLength / 4))));
3026 }
3027
3028 function isObject(value) {
3029 return !!value && typeof value === 'object';
3030 }
3031
3032 function isOn(...fns) {
3033 return (value) => (isObject(value) || isFunction(value)) &&
3034 fns.every((f) => isFunction(value[f]));
3035 }
3036 function isOnFunction(...fns) {
3037 return (value) => isFunction(value) &&
3038 fns.every((f) => isFunction(value[f]));
3039 }
3040 function isOnObject(...fns) {
3041 return (value) => isObject(value) &&
3042 fns.every((f) => isFunction(value[f]));
3043 }
3044
3045 const isToBigInt = isOn('toBigInt');
3046
3047 const isToBn = isOn('toBn');
3048
3049 function nToBigInt(value) {
3050 return typeof value === 'bigint'
3051 ? value
3052 : !value
3053 ? BigInt(0)
3054 : isHex(value)
3055 ? hexToBigInt(value.toString())
3056 : isBn(value)
3057 ? BigInt(value.toString())
3058 : isToBigInt(value)
3059 ? value.toBigInt()
3060 : isToBn(value)
3061 ? BigInt(value.toBn().toString())
3062 : BigInt(value);
3063 }
3064
3065 function nSqrt(value) {
3066 const n = nToBigInt(value);
3067 if (n < _0n) {
3068 throw new Error('square root of negative numbers is not supported');
3069 }
3070 if (n <= _2pow53n) {
3071 return BigInt(~~Math.sqrt(Number(n)));
3072 }
3073 let x0 = _sqrt2pow53n;
3074 while (true) {
3075 const x1 = ((n / x0) + x0) >> _1n;
3076 if (x0 === x1 || (x0 === (x1 - _1n))) {
3077 return x0;
3078 }
3079 x0 = x1;
3080 }
3081 }
3082
3083 const hasBigInt = typeof BigInt === 'function' && typeof BigInt.asIntN === 'function';
3084 const hasCjs = typeof require === 'function' && typeof module !== 'undefined';
3085 const hasDirname = typeof __dirname !== 'undefined';
3086 const hasEsm = !hasCjs;
3087 const hasWasm = typeof WebAssembly !== 'undefined';
3088 const hasBuffer = typeof xglobal.Buffer === 'function' && typeof xglobal.Buffer.isBuffer === 'function';
3089 const hasProcess = typeof xglobal.process === 'object';
3090
3091 function isBuffer(value) {
3092 return hasBuffer && !!value && isFunction(value.readDoubleLE) && xglobal.Buffer.isBuffer(value);
3093 }
3094
3095 function isU8a(value) {
3096 return (((value && value.constructor) === Uint8Array) ||
3097 value instanceof Uint8Array);
3098 }
3099
3100 var browser$1 = {};
3101
3102 const require$$0 = /*@__PURE__*/getAugmentedNamespace(build);
3103
3104 var fallback$1 = {};
3105
3106 Object.defineProperty(fallback$1, "__esModule", { value: true });
3107 fallback$1.TextEncoder = void 0;
3108 class TextEncoder {
3109 encode(value) {
3110 const count = value.length;
3111 const u8a = new Uint8Array(count);
3112 for (let i = 0; i < count; i++) {
3113 u8a[i] = value.charCodeAt(i);
3114 }
3115 return u8a;
3116 }
3117 }
3118 fallback$1.TextEncoder = TextEncoder;
3119
3120 var packageInfo$1 = {};
3121
3122 Object.defineProperty(packageInfo$1, "__esModule", { value: true });
3123 packageInfo$1.packageInfo = void 0;
3124 packageInfo$1.packageInfo = { name: '@polkadot/x-textencoder', path: typeof __dirname === 'string' ? __dirname : 'auto', type: 'cjs', version: '12.5.1' };
3125
3126 (function (exports) {
3127 Object.defineProperty(exports, "__esModule", { value: true });
3128 exports.TextEncoder = exports.packageInfo = void 0;
3129 const x_global_1 = require$$0;
3130 const fallback_js_1 = fallback$1;
3131 var packageInfo_js_1 = packageInfo$1;
3132 Object.defineProperty(exports, "packageInfo", { enumerable: true, get: function () { return packageInfo_js_1.packageInfo; } });
3133 exports.TextEncoder = (0, x_global_1.extractGlobal)('TextEncoder', fallback_js_1.TextEncoder);
3134 } (browser$1));
3135 getDefaultExportFromCjs(browser$1);
3136
3137 const encoder = new browser$1.TextEncoder();
3138 function stringToU8a(value) {
3139 return value
3140 ? encoder.encode(value.toString())
3141 : new Uint8Array();
3142 }
3143
3144 function u8aToU8a(value) {
3145 return isU8a(value)
3146 ? isBuffer(value)
3147 ? new Uint8Array(value)
3148 : value
3149 : isHex(value)
3150 ? hexToU8a(value)
3151 : Array.isArray(value)
3152 ? new Uint8Array(value)
3153 : stringToU8a(value);
3154 }
3155
3156 function u8aCmp(a, b) {
3157 const u8aa = u8aToU8a(a);
3158 const u8ab = u8aToU8a(b);
3159 let i = 0;
3160 while (true) {
3161 const overA = i >= u8aa.length;
3162 const overB = i >= u8ab.length;
3163 if (overA && overB) {
3164 return 0;
3165 }
3166 else if (overA) {
3167 return -1;
3168 }
3169 else if (overB) {
3170 return 1;
3171 }
3172 else if (u8aa[i] !== u8ab[i]) {
3173 return u8aa[i] > u8ab[i]
3174 ? 1
3175 : -1;
3176 }
3177 i++;
3178 }
3179 }
3180
3181 function u8aConcat(...list) {
3182 const count = list.length;
3183 const u8as = new Array(count);
3184 let length = 0;
3185 for (let i = 0; i < count; i++) {
3186 u8as[i] = u8aToU8a(list[i]);
3187 length += u8as[i].length;
3188 }
3189 return u8aConcatStrict(u8as, length);
3190 }
3191 function u8aConcatStrict(u8as, length = 0) {
3192 const count = u8as.length;
3193 let offset = 0;
3194 if (!length) {
3195 for (let i = 0; i < count; i++) {
3196 length += u8as[i].length;
3197 }
3198 }
3199 const result = new Uint8Array(length);
3200 for (let i = 0; i < count; i++) {
3201 result.set(u8as[i], offset);
3202 offset += u8as[i].length;
3203 }
3204 return result;
3205 }
3206
3207 function u8aEmpty(value) {
3208 const len = value.length | 0;
3209 for (let i = 0; i < len; i++) {
3210 if (value[i] | 0) {
3211 return false;
3212 }
3213 }
3214 return true;
3215 }
3216
3217 function u8aEq(a, b) {
3218 const u8aa = u8aToU8a(a);
3219 const u8ab = u8aToU8a(b);
3220 if (u8aa.length === u8ab.length) {
3221 const dvA = new DataView(u8aa.buffer, u8aa.byteOffset);
3222 const dvB = new DataView(u8ab.buffer, u8ab.byteOffset);
3223 const mod = (u8aa.length % 4) | 0;
3224 const length = (u8aa.length - mod) | 0;
3225 for (let i = 0; i < length; i += 4) {
3226 if (dvA.getUint32(i) !== dvB.getUint32(i)) {
3227 return false;
3228 }
3229 }
3230 for (let i = length, count = u8aa.length; i < count; i++) {
3231 if (u8aa[i] !== u8ab[i]) {
3232 return false;
3233 }
3234 }
3235 return true;
3236 }
3237 return false;
3238 }
3239
3240 function u8aFixLength(value, bitLength = -1, atStart = false) {
3241 const byteLength = Math.ceil(bitLength / 8);
3242 if (bitLength === -1 || value.length === byteLength) {
3243 return value;
3244 }
3245 else if (value.length > byteLength) {
3246 return value.subarray(0, byteLength);
3247 }
3248 const result = new Uint8Array(byteLength);
3249 result.set(value, atStart ? 0 : (byteLength - value.length));
3250 return result;
3251 }
3252
3253 function u8aSorted(u8as) {
3254 return u8as.sort(u8aCmp);
3255 }
3256
3257 function u8aToBn(value, { isLe = true, isNegative = false } = {}) {
3258 if (!isLe) {
3259 value = value.slice().reverse();
3260 }
3261 const count = value.length;
3262 if (isNegative && count && (value[count - 1] & 0x80)) {
3263 switch (count) {
3264 case 0:
3265 return new BN(0);
3266 case 1:
3267 return new BN(((value[0] ^ 255) * -1) - 1);
3268 case 2:
3269 return new BN((((value[0] + (value[1] << 8)) ^ 65535) * -1) - 1);
3270 case 3:
3271 return new BN((((value[0] + (value[1] << 8) + (value[2] << 16)) ^ 16777215) * -1) - 1);
3272 case 4:
3273 return new BN((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) * -1) - 1);
3274 case 5:
3275 return new BN(((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + ((value[4] ^ 0xff) * 4294967296)) * -1) - 1);
3276 case 6:
3277 return new BN(((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + (((value[4] + (value[5] << 8)) ^ 65535) * 4294967296)) * -1) - 1);
3278 default:
3279 return new BN(value, 'le').fromTwos(count * 8);
3280 }
3281 }
3282 switch (count) {
3283 case 0:
3284 return new BN(0);
3285 case 1:
3286 return new BN(value[0]);
3287 case 2:
3288 return new BN(value[0] + (value[1] << 8));
3289 case 3:
3290 return new BN(value[0] + (value[1] << 8) + (value[2] << 16));
3291 case 4:
3292 return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216));
3293 case 5:
3294 return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8)) * 16777216));
3295 case 6:
3296 return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8) + (value[5] << 16)) * 16777216));
3297 default:
3298 return new BN(value, 'le');
3299 }
3300 }
3301
3302 function u8aToBuffer(value) {
3303 return hasBuffer
3304 ? xglobal.Buffer.from(value || [])
3305 : new Uint8Array(value || []);
3306 }
3307
3308 function u8aToFloat(value, { bitLength = 32, isLe = true } = {}) {
3309 if (bitLength !== 32 && bitLength !== 64) {
3310 throw new Error('Invalid bitLength provided, expected 32 or 64');
3311 }
3312 else if (value.length < (bitLength / 8)) {
3313 throw new Error(`Invalid input buffer provided, expected at least ${bitLength / 8} bytes, found ${value.length}`);
3314 }
3315 const dv = new DataView(value.buffer, value.byteOffset);
3316 return bitLength === 32
3317 ? dv.getFloat32(0, isLe)
3318 : dv.getFloat64(0, isLe);
3319 }
3320
3321 const U8 = new Array(256);
3322 const U16 = new Array(256 * 256);
3323 for (let n = 0; n < 256; n++) {
3324 U8[n] = n.toString(16).padStart(2, '0');
3325 }
3326 for (let i = 0; i < 256; i++) {
3327 const s = i << 8;
3328 for (let j = 0; j < 256; j++) {
3329 U16[s | j] = U8[i] + U8[j];
3330 }
3331 }
3332 function hex(value, result) {
3333 const mod = (value.length % 2) | 0;
3334 const length = (value.length - mod) | 0;
3335 for (let i = 0; i < length; i += 2) {
3336 result += U16[(value[i] << 8) | value[i + 1]];
3337 }
3338 if (mod) {
3339 result += U8[value[length] | 0];
3340 }
3341 return result;
3342 }
3343 function u8aToHex(value, bitLength = -1, isPrefixed = true) {
3344 const empty = isPrefixed
3345 ? '0x'
3346 : '';
3347 if (!value?.length) {
3348 return empty;
3349 }
3350 else if (bitLength > 0) {
3351 const length = Math.ceil(bitLength / 8);
3352 if (value.length > length) {
3353 return `${hex(value.subarray(0, length / 2), empty)}…${hex(value.subarray(value.length - length / 2), '')}`;
3354 }
3355 }
3356 return hex(value, empty);
3357 }
3358
3359 function u8aToNumber(value, { isLe = true, isNegative = false } = {}) {
3360 if (!isLe) {
3361 value = value.slice().reverse();
3362 }
3363 const count = value.length;
3364 if (isNegative && count && (value[count - 1] & 0x80)) {
3365 switch (count) {
3366 case 0:
3367 return 0;
3368 case 1:
3369 return (((value[0] ^ 255) * -1) - 1);
3370 case 2:
3371 return ((((value[0] + (value[1] << 8)) ^ 65535) * -1) - 1);
3372 case 3:
3373 return ((((value[0] + (value[1] << 8) + (value[2] << 16)) ^ 16777215) * -1) - 1);
3374 case 4:
3375 return ((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) * -1) - 1);
3376 case 5:
3377 return (((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + ((value[4] ^ 0xff) * 4294967296)) * -1) - 1);
3378 case 6:
3379 return (((((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216)) ^ 4294967295) + (((value[4] + (value[5] << 8)) ^ 65535) * 4294967296)) * -1) - 1);
3380 default:
3381 throw new Error('Value more than 48-bits cannot be reliably converted');
3382 }
3383 }
3384 switch (count) {
3385 case 0:
3386 return 0;
3387 case 1:
3388 return value[0];
3389 case 2:
3390 return value[0] + (value[1] << 8);
3391 case 3:
3392 return value[0] + (value[1] << 8) + (value[2] << 16);
3393 case 4:
3394 return value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 16777216);
3395 case 5:
3396 return value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8)) * 16777216);
3397 case 6:
3398 return value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8) + (value[5] << 16)) * 16777216);
3399 default:
3400 throw new Error('Value more than 48-bits cannot be reliably converted');
3401 }
3402 }
3403
3404 var browser = {};
3405
3406 var fallback = {};
3407
3408 Object.defineProperty(fallback, "__esModule", { value: true });
3409 fallback.TextDecoder = void 0;
3410 class TextDecoder {
3411 constructor(encoding) {
3412 this.__encoding = encoding;
3413 }
3414 decode(value) {
3415 let result = '';
3416 for (let i = 0, count = value.length; i < count; i++) {
3417 result += String.fromCharCode(value[i]);
3418 }
3419 return result;
3420 }
3421 }
3422 fallback.TextDecoder = TextDecoder;
3423
3424 var packageInfo = {};
3425
3426 Object.defineProperty(packageInfo, "__esModule", { value: true });
3427 packageInfo.packageInfo = void 0;
3428 packageInfo.packageInfo = { name: '@polkadot/x-textdecoder', path: typeof __dirname === 'string' ? __dirname : 'auto', type: 'cjs', version: '12.5.1' };
3429
3430 (function (exports) {
3431 Object.defineProperty(exports, "__esModule", { value: true });
3432 exports.TextDecoder = exports.packageInfo = void 0;
3433 const x_global_1 = require$$0;
3434 const fallback_js_1 = fallback;
3435 var packageInfo_js_1 = packageInfo;
3436 Object.defineProperty(exports, "packageInfo", { enumerable: true, get: function () { return packageInfo_js_1.packageInfo; } });
3437 exports.TextDecoder = (0, x_global_1.extractGlobal)('TextDecoder', fallback_js_1.TextDecoder);
3438 } (browser));
3439 getDefaultExportFromCjs(browser);
3440
3441 const decoder = new browser.TextDecoder('utf-8');
3442 function u8aToString(value) {
3443 return value
3444 ? decoder.decode(value)
3445 : '';
3446 }
3447
3448 const U8A_WRAP_ETHEREUM = u8aToU8a('\x19Ethereum Signed Message:\n');
3449 const U8A_WRAP_PREFIX = u8aToU8a('<Bytes>');
3450 const U8A_WRAP_POSTFIX = u8aToU8a('</Bytes>');
3451 const WRAP_LEN = U8A_WRAP_PREFIX.length + U8A_WRAP_POSTFIX.length;
3452 function u8aIsWrapped(u8a, withEthereum) {
3453 return ((u8a.length >= WRAP_LEN &&
3454 u8aEq(u8a.subarray(0, U8A_WRAP_PREFIX.length), U8A_WRAP_PREFIX) &&
3455 u8aEq(u8a.slice(-U8A_WRAP_POSTFIX.length), U8A_WRAP_POSTFIX)) ||
3456 (withEthereum &&
3457 u8a.length >= U8A_WRAP_ETHEREUM.length &&
3458 u8aEq(u8a.subarray(0, U8A_WRAP_ETHEREUM.length), U8A_WRAP_ETHEREUM)));
3459 }
3460 function u8aUnwrapBytes(bytes) {
3461 const u8a = u8aToU8a(bytes);
3462 return u8aIsWrapped(u8a, false)
3463 ? u8a.subarray(U8A_WRAP_PREFIX.length, u8a.length - U8A_WRAP_POSTFIX.length)
3464 : u8a;
3465 }
3466 function u8aWrapBytes(bytes) {
3467 const u8a = u8aToU8a(bytes);
3468 return u8aIsWrapped(u8a, true)
3469 ? u8a
3470 : u8aConcatStrict([U8A_WRAP_PREFIX, u8a, U8A_WRAP_POSTFIX]);
3471 }
3472
3473 const DIV = BigInt(256);
3474 const NEG_MASK = BigInt(0xff);
3475 function toU8a(value, isLe, isNegative) {
3476 const arr = [];
3477 const withSigned = isNegative && (value < _0n);
3478 if (withSigned) {
3479 value = (value + _1n) * -_1n;
3480 }
3481 while (value !== _0n) {
3482 const mod = value % DIV;
3483 const val = Number(withSigned
3484 ? mod ^ NEG_MASK
3485 : mod);
3486 if (isLe) {
3487 arr.push(val);
3488 }
3489 else {
3490 arr.unshift(val);
3491 }
3492 value = (value - mod) / DIV;
3493 }
3494 return Uint8Array.from(arr);
3495 }
3496 function nToU8a(value, { bitLength = -1, isLe = true, isNegative = false } = {}) {
3497 const valueBi = nToBigInt(value);
3498 if (valueBi === _0n) {
3499 return bitLength === -1
3500 ? new Uint8Array(1)
3501 : new Uint8Array(Math.ceil((bitLength || 0) / 8));
3502 }
3503 const u8a = toU8a(valueBi, isLe, isNegative);
3504 if (bitLength === -1) {
3505 return u8a;
3506 }
3507 const byteLength = Math.ceil((bitLength || 0) / 8);
3508 const output = new Uint8Array(byteLength);
3509 if (isNegative) {
3510 output.fill(0xff);
3511 }
3512 output.set(u8a, isLe ? 0 : byteLength - u8a.length);
3513 return output;
3514 }
3515
3516 function nToHex(value, { bitLength = -1, isLe = false, isNegative = false } = {}) {
3517 return u8aToHex(nToU8a(value || 0, { bitLength, isLe, isNegative }));
3518 }
3519
3520 function hexStripPrefix(value) {
3521 if (!value || value === '0x') {
3522 return '';
3523 }
3524 else if (REGEX_HEX_PREFIXED.test(value)) {
3525 return value.substring(2);
3526 }
3527 else if (REGEX_HEX_NOPREFIX.test(value)) {
3528 return value;
3529 }
3530 throw new Error(`Expected hex value to convert, found '${value}'`);
3531 }
3532
3533 function hexToBn(value, { isLe = false, isNegative = false } = {}) {
3534 if (!value || value === '0x') {
3535 return new BN(0);
3536 }
3537 const stripped = hexStripPrefix(value);
3538 const bn = new BN(stripped, 16, isLe ? 'le' : 'be');
3539 return isNegative
3540 ? bn.fromTwos(stripped.length * 4)
3541 : bn;
3542 }
3543
3544 const bnMax = createCmp((a, b) => a.gt(b));
3545 const bnMin = createCmp((a, b) => a.lt(b));
3546
3547 const BN_ZERO = new BN(0);
3548 const BN_ONE = new BN(1);
3549 const BN_TWO = new BN(2);
3550 const BN_THREE = new BN(3);
3551 const BN_FOUR = new BN(4);
3552 const BN_FIVE = new BN(5);
3553 const BN_SIX = new BN(6);
3554 const BN_SEVEN = new BN(7);
3555 const BN_EIGHT = new BN(8);
3556 const BN_NINE = new BN(9);
3557 const BN_TEN = new BN(10);
3558 const BN_HUNDRED = new BN(100);
3559 const BN_THOUSAND = new BN(1000);
3560 const BN_MILLION = new BN(1000000);
3561 const BN_BILLION = new BN(1000000000);
3562 const BN_QUINTILL = BN_BILLION.mul(BN_BILLION);
3563 const BN_MAX_INTEGER = new BN(Number.MAX_SAFE_INTEGER);
3564 const BN_SQRT_MAX_INTEGER = new BN(94906265);
3565
3566 function isBigInt(value) {
3567 return typeof value === 'bigint';
3568 }
3569
3570 function bnToBn(value) {
3571 return value
3572 ? BN.isBN(value)
3573 ? value
3574 : isHex(value)
3575 ? hexToBn(value.toString())
3576 : isBigInt(value)
3577 ? new BN(value.toString())
3578 : isToBn(value)
3579 ? value.toBn()
3580 : isToBigInt(value)
3581 ? new BN(value.toBigInt().toString())
3582 : new BN(value)
3583 : new BN(0);
3584 }
3585
3586 function bnSqrt(value) {
3587 const n = bnToBn(value);
3588 if (n.isNeg()) {
3589 throw new Error('square root of negative numbers is not supported');
3590 }
3591 if (n.lte(BN_MAX_INTEGER)) {
3592 return new BN(~~Math.sqrt(n.toNumber()));
3593 }
3594 let x0 = BN_SQRT_MAX_INTEGER.clone();
3595 while (true) {
3596 const x1 = n.div(x0).iadd(x0).ishrn(1);
3597 if (x0.eq(x1) || x0.eq(x1.sub(BN_ONE))) {
3598 return x0;
3599 }
3600 x0 = x1;
3601 }
3602 }
3603
3604 const DEFAULT_OPTS = { bitLength: -1, isLe: true, isNegative: false };
3605 function bnToU8a(value, { bitLength = -1, isLe = true, isNegative = false } = DEFAULT_OPTS) {
3606 const valueBn = bnToBn(value);
3607 const byteLength = bitLength === -1
3608 ? Math.ceil(valueBn.bitLength() / 8)
3609 : Math.ceil((bitLength || 0) / 8);
3610 if (!value) {
3611 return bitLength === -1
3612 ? new Uint8Array(1)
3613 : new Uint8Array(byteLength);
3614 }
3615 const output = new Uint8Array(byteLength);
3616 const bn = isNegative
3617 ? valueBn.toTwos(byteLength * 8)
3618 : valueBn;
3619 output.set(bn.toArray(isLe ? 'le' : 'be', byteLength), 0);
3620 return output;
3621 }
3622
3623 function bnToHex(value, { bitLength = -1, isLe = false, isNegative = false } = {}) {
3624 return u8aToHex(bnToU8a(value, { bitLength, isLe, isNegative }));
3625 }
3626
3627 function bufferToU8a(buffer) {
3628 return new Uint8Array(buffer || []);
3629 }
3630
3631 const MAX_U8 = BN_TWO.pow(new BN(8 - 2)).isub(BN_ONE);
3632 const MAX_U16 = BN_TWO.pow(new BN(16 - 2)).isub(BN_ONE);
3633 const MAX_U32 = BN_TWO.pow(new BN(32 - 2)).isub(BN_ONE);
3634 const BL_16 = { bitLength: 16 };
3635 const BL_32 = { bitLength: 32 };
3636 function compactToU8a(value) {
3637 const bn = bnToBn(value);
3638 if (bn.lte(MAX_U8)) {
3639 return new Uint8Array([bn.toNumber() << 2]);
3640 }
3641 else if (bn.lte(MAX_U16)) {
3642 return bnToU8a(bn.shln(2).iadd(BN_ONE), BL_16);
3643 }
3644 else if (bn.lte(MAX_U32)) {
3645 return bnToU8a(bn.shln(2).iadd(BN_TWO), BL_32);
3646 }
3647 const u8a = bnToU8a(bn);
3648 let length = u8a.length;
3649 while (u8a[length - 1] === 0) {
3650 length--;
3651 }
3652 if (length < 4) {
3653 throw new Error('Invalid length, previous checks match anything less than 2^30');
3654 }
3655 return u8aConcatStrict([
3656 new Uint8Array([((length - 4) << 2) + 0b11]),
3657 u8a.subarray(0, length)
3658 ]);
3659 }
3660
3661 function compactAddLength(input) {
3662 return u8aConcatStrict([
3663 compactToU8a(input.length),
3664 input
3665 ]);
3666 }
3667
3668 function compactFromU8a(input) {
3669 const u8a = u8aToU8a(input);
3670 switch (u8a[0] & 0b11) {
3671 case 0b00:
3672 return [1, new BN(u8a[0] >>> 2)];
3673 case 0b01:
3674 return [2, new BN((u8a[0] + (u8a[1] << 8)) >>> 2)];
3675 case 0b10:
3676 return [4, new BN((u8a[0] + (u8a[1] << 8) + (u8a[2] << 16) + (u8a[3] * 16777216)) >>> 2)];
3677 default: {
3678 const offset = (u8a[0] >>> 2) + 5;
3679 switch (offset) {
3680 case 5:
3681 return [5, new BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] * 16777216))];
3682 case 6:
3683 return [6, new BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + ((u8a[4] + (u8a[5] << 8)) * 16777216))];
3684 case 7:
3685 return [7, new BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + ((u8a[4] + (u8a[5] << 8) + (u8a[6] << 16)) * 16777216))];
3686 default:
3687 return [offset, u8aToBn(u8a.subarray(1, offset))];
3688 }
3689 }
3690 }
3691 }
3692 function compactFromU8aLim(u8a) {
3693 switch (u8a[0] & 0b11) {
3694 case 0b00:
3695 return [1, u8a[0] >>> 2];
3696 case 0b01:
3697 return [2, (u8a[0] + (u8a[1] << 8)) >>> 2];
3698 case 0b10:
3699 return [4, (u8a[0] + (u8a[1] << 8) + (u8a[2] << 16) + (u8a[3] * 16777216)) >>> 2];
3700 default: {
3701 switch ((u8a[0] >>> 2) + 5) {
3702 case 5:
3703 return [5, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] * 16777216)];
3704 case 6:
3705 return [6, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + ((u8a[4] + (u8a[5] << 8)) * 16777216)];
3706 case 7:
3707 return [7, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + ((u8a[4] + (u8a[5] << 8) + (u8a[6] << 16)) * 16777216)];
3708 default:
3709 throw new Error('Compact input is > Number.MAX_SAFE_INTEGER');
3710 }
3711 }
3712 }
3713 }
3714
3715 function compactStripLength(input) {
3716 const [offset, length] = compactFromU8a(input);
3717 const total = offset + length.toNumber();
3718 return [
3719 total,
3720 input.subarray(offset, total)
3721 ];
3722 }
3723
3724 const MIN_MS = 60 * 1000;
3725 const HR_MS = MIN_MS * 60;
3726 const DAY_MS = HR_MS * 24;
3727 const ZERO = { days: 0, hours: 0, milliseconds: 0, minutes: 0, seconds: 0 };
3728 function add(a, b) {
3729 return {
3730 days: (a.days || 0) + b.days,
3731 hours: (a.hours || 0) + b.hours,
3732 milliseconds: (a.milliseconds || 0) + b.milliseconds,
3733 minutes: (a.minutes || 0) + b.minutes,
3734 seconds: (a.seconds || 0) + b.seconds
3735 };
3736 }
3737 function extractSecs(ms) {
3738 const s = ms / 1000;
3739 if (s < 60) {
3740 const seconds = ~~s;
3741 return add({ seconds }, extractTime(ms - (seconds * 1000)));
3742 }
3743 const m = s / 60;
3744 if (m < 60) {
3745 const minutes = ~~m;
3746 return add({ minutes }, extractTime(ms - (minutes * MIN_MS)));
3747 }
3748 const h = m / 60;
3749 if (h < 24) {
3750 const hours = ~~h;
3751 return add({ hours }, extractTime(ms - (hours * HR_MS)));
3752 }
3753 const days = ~~(h / 24);
3754 return add({ days }, extractTime(ms - (days * DAY_MS)));
3755 }
3756 function extractTime(milliseconds) {
3757 return !milliseconds
3758 ? ZERO
3759 : milliseconds < 1000
3760 ? add({ milliseconds }, ZERO)
3761 : extractSecs(milliseconds);
3762 }
3763
3764 function floatToU8a(value = 0.0, { bitLength = 32, isLe = true } = {}) {
3765 if (bitLength !== 32 && bitLength !== 64) {
3766 throw new Error('Invalid bitLength provided, expected 32 or 64');
3767 }
3768 const result = new Uint8Array(bitLength / 8);
3769 const dv = new DataView(result.buffer, result.byteOffset);
3770 if (bitLength === 32) {
3771 dv.setFloat32(0, Number(value), isLe);
3772 }
3773 else {
3774 dv.setFloat64(0, Number(value), isLe);
3775 }
3776 return result;
3777 }
3778
3779 function isBoolean(value) {
3780 return typeof value === 'boolean';
3781 }
3782
3783 const NUMBER_REGEX = new RegExp('(\\d+?)(?=(\\d{3})+(?!\\d)|$)', 'g');
3784 function formatDecimal(value, separator = ',') {
3785 const isNegative = value[0].startsWith('-');
3786 const matched = isNegative
3787 ? value.substring(1).match(NUMBER_REGEX)
3788 : value.match(NUMBER_REGEX);
3789 return matched
3790 ? `${isNegative ? '-' : ''}${matched.join(separator)}`
3791 : value;
3792 }
3793
3794 function getSeparator(locale) {
3795 return {
3796 decimal: (0.1).toLocaleString(locale).substring(1, 2),
3797 thousand: (1000).toLocaleString(locale).substring(1, 2)
3798 };
3799 }
3800
3801 const SI_MID = 8;
3802 const SI = [
3803 { power: -24, text: 'yocto', value: 'y' },
3804 { power: -21, text: 'zepto', value: 'z' },
3805 { power: -18, text: 'atto', value: 'a' },
3806 { power: -15, text: 'femto', value: 'f' },
3807 { power: -12, text: 'pico', value: 'p' },
3808 { power: -9, text: 'nano', value: 'n' },
3809 { power: -6, text: 'micro', value: 'µ' },
3810 { power: -3, text: 'milli', value: 'm' },
3811 { power: 0, text: 'Unit', value: '-' },
3812 { power: 3, text: 'Kilo', value: 'k' },
3813 { power: 6, text: 'Mill', value: 'M' },
3814 { power: 9, text: 'Bill', value: 'B' },
3815 { power: 12, text: 'Tril', value: 'T' },
3816 { power: 15, text: 'Peta', value: 'P' },
3817 { power: 18, text: 'Exa', value: 'E' },
3818 { power: 21, text: 'Zeta', value: 'Z' },
3819 { power: 24, text: 'Yotta', value: 'Y' }
3820 ];
3821 function findSi(type) {
3822 for (let i = 0, count = SI.length; i < count; i++) {
3823 if (SI[i].value === type) {
3824 return SI[i];
3825 }
3826 }
3827 return SI[SI_MID];
3828 }
3829 function calcSi(text, decimals, forceUnit) {
3830 if (forceUnit) {
3831 return findSi(forceUnit);
3832 }
3833 const siDefIndex = (SI_MID - 1) + Math.ceil((text.length - decimals) / 3);
3834 return SI[siDefIndex] || SI[siDefIndex < 0 ? 0 : SI.length - 1];
3835 }
3836
3837 const DEFAULT_DECIMALS = 0;
3838 const DEFAULT_UNIT = SI[SI_MID].text;
3839 let defaultDecimals = DEFAULT_DECIMALS;
3840 let defaultUnit = DEFAULT_UNIT;
3841 function _formatBalance(input, { decimals = defaultDecimals, forceUnit, locale = 'en', withAll = false, withSi = true, withSiFull = false, withUnit = true, withZero = true } = {}) {
3842 let text = bnToBn(input).toString();
3843 if (text.length === 0 || text === '0') {
3844 return '0';
3845 }
3846 let sign = '';
3847 if (text[0].startsWith('-')) {
3848 sign = '-';
3849 text = text.substring(1);
3850 }
3851 const si = calcSi(text, decimals, forceUnit);
3852 const mid = text.length - (decimals + si.power);
3853 const pre = mid <= 0 ? '0' : text.substring(0, mid);
3854 let post = text
3855 .padStart(mid < 0 ? decimals : 1, '0')
3856 .substring(mid < 0 ? 0 : mid)
3857 .padEnd(withAll ? Math.max(decimals, 4) : 4, '0')
3858 .substring(0, withAll ? Math.max(4, decimals + si.power) : 4);
3859 if (!withZero) {
3860 let end = post.length - 1;
3861 do {
3862 if (post[end] === '0') {
3863 end--;
3864 }
3865 } while (post[end] === '0');
3866 post = post.substring(0, end + 1);
3867 }
3868 const unit = isBoolean(withUnit)
3869 ? SI[SI_MID].text
3870 : withUnit;
3871 const units = withSi || withSiFull
3872 ? si.value === '-'
3873 ? withUnit
3874 ? ` ${unit}`
3875 : ''
3876 : ` ${withSiFull ? `${si.text}${withUnit ? ' ' : ''}` : si.value}${withUnit ? unit : ''}`
3877 : '';
3878 const { decimal, thousand } = getSeparator(locale);
3879 return `${sign}${formatDecimal(pre, thousand)}${post && `${decimal}${post}`}${units}`;
3880 }
3881 const formatBalance = _formatBalance;
3882 formatBalance.calcSi = (text, decimals = defaultDecimals) => calcSi(text, decimals);
3883 formatBalance.findSi = findSi;
3884 formatBalance.getDefaults = () => {
3885 return {
3886 decimals: defaultDecimals,
3887 unit: defaultUnit
3888 };
3889 };
3890 formatBalance.getOptions = (decimals = defaultDecimals) => {
3891 return SI.filter(({ power }) => power < 0
3892 ? (decimals + power) >= 0
3893 : true);
3894 };
3895 formatBalance.setDefaults = ({ decimals, unit }) => {
3896 defaultDecimals = (Array.isArray(decimals)
3897 ? decimals[0]
3898 : decimals) ?? defaultDecimals;
3899 defaultUnit = (Array.isArray(unit)
3900 ? unit[0]
3901 : unit) ?? defaultUnit;
3902 SI[SI_MID].text = defaultUnit;
3903 };
3904
3905 function zeroPad(value) {
3906 return value.toString().padStart(2, '0');
3907 }
3908 function formatDate(date) {
3909 const year = date.getFullYear().toString();
3910 const month = zeroPad((date.getMonth() + 1));
3911 const day = zeroPad(date.getDate());
3912 const hour = zeroPad(date.getHours());
3913 const minute = zeroPad(date.getMinutes());
3914 const second = zeroPad(date.getSeconds());
3915 return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
3916 }
3917
3918 function formatValue(elapsed) {
3919 if (elapsed < 15) {
3920 return `${elapsed.toFixed(1)}s`;
3921 }
3922 else if (elapsed < 60) {
3923 return `${elapsed | 0}s`;
3924 }
3925 else if (elapsed < 3600) {
3926 return `${elapsed / 60 | 0}m`;
3927 }
3928 return `${elapsed / 3600 | 0}h`;
3929 }
3930 function formatElapsed(now, value) {
3931 const tsNow = now?.getTime() || 0;
3932 const tsValue = value instanceof Date
3933 ? value.getTime()
3934 : bnToBn(value).toNumber();
3935 return (tsNow && tsValue)
3936 ? formatValue(Math.max(Math.abs(tsNow - tsValue), 0) / 1000)
3937 : '0.0s';
3938 }
3939
3940 function formatNumber(value, { locale = 'en' } = {}) {
3941 const { thousand } = getSeparator(locale);
3942 return formatDecimal(bnToBn(value).toString(), thousand);
3943 }
3944
3945 function hexHasPrefix(value) {
3946 return !!value && isHex(value, -1);
3947 }
3948
3949 function hexAddPrefix(value) {
3950 return value && hexHasPrefix(value)
3951 ? value
3952 : `0x${value && value.length % 2 === 1 ? '0' : ''}${value || ''}`;
3953 }
3954
3955 function hexFixLength(value, bitLength = -1, withPadding = false) {
3956 const strLength = Math.ceil(bitLength / 4);
3957 const hexLength = strLength + 2;
3958 return hexAddPrefix((bitLength === -1 || value.length === hexLength || (!withPadding && value.length < hexLength))
3959 ? hexStripPrefix(value)
3960 : (value.length > hexLength)
3961 ? hexStripPrefix(value).slice(-1 * strLength)
3962 : `${'0'.repeat(strLength)}${hexStripPrefix(value)}`.slice(-1 * strLength));
3963 }
3964
3965 function hexToNumber(value) {
3966 return value
3967 ? hexToBn(value).toNumber()
3968 : NaN;
3969 }
3970
3971 function hexToString(_value) {
3972 return u8aToString(hexToU8a(_value));
3973 }
3974
3975 function isArray(value) {
3976 return Array.isArray(value);
3977 }
3978
3979 function isString(value) {
3980 return typeof value === 'string' || value instanceof String;
3981 }
3982
3983 function isAsciiStr(str) {
3984 for (let i = 0, count = str.length; i < count; i++) {
3985 const b = str.charCodeAt(i);
3986 if (b < 32 || b > 126) {
3987 return false;
3988 }
3989 }
3990 return true;
3991 }
3992 function isAsciiBytes(u8a) {
3993 for (let i = 0, count = u8a.length; i < count; i++) {
3994 const b = u8a[i] | 0;
3995 if (b < 32 || b > 126) {
3996 return false;
3997 }
3998 }
3999 return true;
4000 }
4001 function isAscii(value) {
4002 return isString(value)
4003 ? isHex(value)
4004 ? isAsciiBytes(u8aToU8a(value))
4005 : isAsciiStr(value)
4006 : value
4007 ? isAsciiBytes(value)
4008 : false;
4009 }
4010
4011 const isClass = isOnFunction('isPrototypeOf', 'hasOwnProperty');
4012
4013 function isChildClass(Parent, Child) {
4014 return isClass(Child) && isClass(Parent)
4015 ? Parent === Child || Parent.isPrototypeOf(Child)
4016 : false;
4017 }
4018
4019 const checkCodec = isOnObject('toHex', 'toHuman', 'toU8a');
4020 const checkRegistry = isOnObject('get');
4021 function isCodec(value) {
4022 return checkCodec(value) && checkRegistry(value.registry);
4023 }
4024
4025 const isCompact = isOnObject('toBigInt', 'toBn', 'toNumber', 'unwrap');
4026
4027 function isError(value) {
4028 return (((value && value.constructor) === Error) ||
4029 value instanceof Error);
4030 }
4031
4032 function isInstanceOf(value, Clazz) {
4033 return (((value && value.constructor) === Clazz) ||
4034 value instanceof Clazz);
4035 }
4036
4037 const v4 = '(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}';
4038 const v6s = '[a-fA-F\\d]{1,4}';
4039 const v6 = `
4040(?:
4041(?:${v6s}:){7}(?:${v6s}|:)| // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8
4042(?:${v6s}:){6}(?:${v4}|:${v6s}|:)| // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6::1.2.3.4
4043(?:${v6s}:){5}(?::${v4}|(?::${v6s}){1,2}|:)| // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5::7:1.2.3.4
4044(?:${v6s}:){4}(?:(?::${v6s}){0,1}:${v4}|(?::${v6s}){1,3}|:)| // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4::6:7:1.2.3.4
4045(?:${v6s}:){3}(?:(?::${v6s}){0,2}:${v4}|(?::${v6s}){1,4}|:)| // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3::5:6:7:1.2.3.4
4046(?:${v6s}:){2}(?:(?::${v6s}){0,3}:${v4}|(?::${v6s}){1,5}|:)| // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2::4:5:6:7:1.2.3.4
4047(?:${v6s}:){1}(?:(?::${v6s}){0,4}:${v4}|(?::${v6s}){1,6}|:)| // 1:: 1::3:4:5:6:7:8 1::8 1::3:4:5:6:7:1.2.3.4
4048(?::(?:(?::${v6s}){0,5}:${v4}|(?::${v6s}){1,7}|:)) // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::1.2.3.4
4049)(?:%[0-9a-zA-Z]{1,})? // %eth0 %1
4050`.replace(/\s*\/\/.*$/gm, '').replace(/\n/g, '').trim();
4051 const v46Exact = new RegExp(`(?:^${v4}$)|(?:^${v6}$)`);
4052 const v4exact = new RegExp(`^${v4}$`);
4053 const v6exact = new RegExp(`^${v6}$`);
4054 function isIp(value, type) {
4055 switch (type) {
4056 case 'v4': return v4exact.test(value);
4057 case 'v6': return v6exact.test(value);
4058 default: return v46Exact.test(value);
4059 }
4060 }
4061
4062 function replacer(_, v) {
4063 return isBigInt(v)
4064 ? v.toString()
4065 : v;
4066 }
4067 function stringify(value, space) {
4068 return JSON.stringify(value, replacer, space);
4069 }
4070
4071 function isJsonObject(value) {
4072 const str = typeof value !== 'string'
4073 ? stringify(value)
4074 : value;
4075 try {
4076 const obj = JSON.parse(str);
4077 return typeof obj === 'object' && obj !== null;
4078 }
4079 catch {
4080 return false;
4081 }
4082 }
4083
4084 function isNull(value) {
4085 return value === null;
4086 }
4087
4088 function isNumber(value) {
4089 return typeof value === 'number';
4090 }
4091
4092 const isObservable = isOn('next');
4093
4094 const isPromise = isOnObject('catch', 'then');
4095
4096 const ELF_MAGIC = new Uint8Array([0x7f, 0x45, 0x4c, 0x46]);
4097 function isRiscV(bytes) {
4098 return isU8a(bytes) && u8aEq(bytes.subarray(0, 4), ELF_MAGIC);
4099 }
4100
4101 const REGEX_DEV = /(Development|Local Testnet)$/;
4102 function isTestChain(chain) {
4103 if (!chain) {
4104 return false;
4105 }
4106 return !!REGEX_DEV.test(chain.toString());
4107 }
4108
4109 function isUndefined(value) {
4110 return value === undefined;
4111 }
4112
4113 function isUtf8(value) {
4114 if (!value) {
4115 return isString(value);
4116 }
4117 const u8a = u8aToU8a(value);
4118 const len = u8a.length;
4119 let i = 0;
4120 while (i < len) {
4121 if (u8a[i] <= 0x7F) {
4122 i += 1;
4123 }
4124 else if (u8a[i] >= 0xC2 && u8a[i] <= 0xDF) {
4125 if (i + 1 < len) {
4126 if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0xBF) {
4127 return false;
4128 }
4129 }
4130 else {
4131 return false;
4132 }
4133 i += 2;
4134 }
4135 else if (u8a[i] === 0xE0) {
4136 if (i + 2 < len) {
4137 if (u8a[i + 1] < 0xA0 || u8a[i + 1] > 0xBF) {
4138 return false;
4139 }
4140 if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
4141 return false;
4142 }
4143 }
4144 else {
4145 return false;
4146 }
4147 i += 3;
4148 }
4149 else if (u8a[i] >= 0xE1 && u8a[i] <= 0xEC) {
4150 if (i + 2 < len) {
4151 if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0xBF) {
4152 return false;
4153 }
4154 if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
4155 return false;
4156 }
4157 }
4158 else {
4159 return false;
4160 }
4161 i += 3;
4162 }
4163 else if (u8a[i] === 0xED) {
4164 if (i + 2 < len) {
4165 if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0x9F) {
4166 return false;
4167 }
4168 if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
4169 return false;
4170 }
4171 }
4172 else {
4173 return false;
4174 }
4175 i += 3;
4176 }
4177 else if (u8a[i] >= 0xEE && u8a[i] <= 0xEF) {
4178 if (i + 2 < len) {
4179 if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0xBF) {
4180 return false;
4181 }
4182 if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
4183 return false;
4184 }
4185 }
4186 else {
4187 return false;
4188 }
4189 i += 3;
4190 }
4191 else if (u8a[i] === 0xF0) {
4192 if (i + 3 < len) {
4193 if (u8a[i + 1] < 0x90 || u8a[i + 1] > 0xBF) {
4194 return false;
4195 }
4196 if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
4197 return false;
4198 }
4199 if (u8a[i + 3] < 0x80 || u8a[i + 3] > 0xBF) {
4200 return false;
4201 }
4202 }
4203 else {
4204 return false;
4205 }
4206 i += 4;
4207 }
4208 else if (u8a[i] >= 0xF1 && u8a[i] <= 0xF3) {
4209 if (i + 3 < len) {
4210 if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0xBF) {
4211 return false;
4212 }
4213 if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
4214 return false;
4215 }
4216 if (u8a[i + 3] < 0x80 || u8a[i + 3] > 0xBF) {
4217 return false;
4218 }
4219 }
4220 else {
4221 return false;
4222 }
4223 i += 4;
4224 }
4225 else if (u8a[i] === 0xF4) {
4226 if (i + 3 < len) {
4227 if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0x8F) {
4228 return false;
4229 }
4230 if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
4231 return false;
4232 }
4233 if (u8a[i + 3] < 0x80 || u8a[i + 3] > 0xBF) {
4234 return false;
4235 }
4236 }
4237 else {
4238 return false;
4239 }
4240 i += 4;
4241 }
4242 else {
4243 return false;
4244 }
4245 }
4246 return true;
4247 }
4248
4249 const WASM_MAGIC = new Uint8Array([0, 97, 115, 109]);
4250 function isWasm(value) {
4251 return isU8a(value) && u8aEq(value.subarray(0, 4), WASM_MAGIC);
4252 }
4253
4254 function lazyMethod(result, item, creator, getName, index = 0) {
4255 const name = getName
4256 ? getName(item, index)
4257 : item.toString();
4258 let value;
4259 Object.defineProperty(result, name, {
4260 configurable: true,
4261 enumerable: true,
4262 get: function () {
4263 if (value === undefined) {
4264 value = creator(item, index, this);
4265 try {
4266 Object.defineProperty(this, name, { value });
4267 }
4268 catch {
4269 }
4270 }
4271 return value;
4272 }
4273 });
4274 }
4275 function lazyMethods(result, items, creator, getName) {
4276 for (let i = 0, count = items.length; i < count; i++) {
4277 lazyMethod(result, items[i], creator, getName, i);
4278 }
4279 return result;
4280 }
4281
4282 function identity(value) {
4283 return value;
4284 }
4285 function noop() {
4286 }
4287
4288 const logTo = {
4289 debug: 'log',
4290 error: 'error',
4291 log: 'log',
4292 warn: 'warn'
4293 };
4294 function formatOther(value) {
4295 if (value && isObject(value) && value.constructor === Object) {
4296 const result = {};
4297 for (const [k, v] of Object.entries(value)) {
4298 result[k] = loggerFormat(v);
4299 }
4300 return result;
4301 }
4302 return value;
4303 }
4304 function loggerFormat(value) {
4305 if (Array.isArray(value)) {
4306 return value.map(loggerFormat);
4307 }
4308 else if (isBn(value)) {
4309 return value.toString();
4310 }
4311 else if (isU8a(value) || isBuffer(value)) {
4312 return u8aToHex(u8aToU8a(value));
4313 }
4314 return formatOther(value);
4315 }
4316 function formatWithLength(maxLength) {
4317 return (v) => {
4318 if (maxLength <= 0) {
4319 return v;
4320 }
4321 const r = `${v}`;
4322 return r.length < maxLength
4323 ? v
4324 : `${r.substring(0, maxLength)} ...`;
4325 };
4326 }
4327 function apply(log, type, values, maxSize = -1) {
4328 if (values.length === 1 && isFunction(values[0])) {
4329 const fnResult = values[0]();
4330 return apply(log, type, Array.isArray(fnResult) ? fnResult : [fnResult], maxSize);
4331 }
4332 console[logTo[log]](formatDate(new Date()), type, ...values
4333 .map(loggerFormat)
4334 .map(formatWithLength(maxSize)));
4335 }
4336 function isDebugOn(e, type) {
4337 return !!e && (e === '*' ||
4338 type === e ||
4339 (e.endsWith('*') &&
4340 type.startsWith(e.slice(0, -1))));
4341 }
4342 function isDebugOff(e, type) {
4343 return !!e && (e.startsWith('-') &&
4344 (type === e.slice(1) ||
4345 (e.endsWith('*') &&
4346 type.startsWith(e.slice(1, -1)))));
4347 }
4348 function getDebugFlag(env, type) {
4349 let flag = false;
4350 for (const e of env) {
4351 if (isDebugOn(e, type)) {
4352 flag = true;
4353 }
4354 else if (isDebugOff(e, type)) {
4355 flag = false;
4356 }
4357 }
4358 return flag;
4359 }
4360 function parseEnv(type) {
4361 const env = (hasProcess ? xglobal.process : {}).env || {};
4362 const maxSize = parseInt(env['DEBUG_MAX'] || '-1', 10);
4363 return [
4364 getDebugFlag((env['DEBUG'] || '').toLowerCase().split(','), type),
4365 isNaN(maxSize)
4366 ? -1
4367 : maxSize
4368 ];
4369 }
4370 function logger(origin) {
4371 const type = `${origin.toUpperCase()}:`.padStart(16);
4372 const [isDebug, maxSize] = parseEnv(origin.toLowerCase());
4373 return {
4374 debug: isDebug
4375 ? (...values) => apply('debug', type, values, maxSize)
4376 : noop,
4377 error: (...values) => apply('error', type, values),
4378 log: (...values) => apply('log', type, values),
4379 noop,
4380 warn: (...values) => apply('warn', type, values)
4381 };
4382 }
4383
4384 function defaultGetId() {
4385 return 'none';
4386 }
4387 function memoize(fn, { getInstanceId = defaultGetId } = {}) {
4388 const cache = {};
4389 const memoized = (...args) => {
4390 const stringParams = stringify(args);
4391 const instanceId = getInstanceId();
4392 if (!cache[instanceId]) {
4393 cache[instanceId] = {};
4394 }
4395 if (cache[instanceId][stringParams] === undefined) {
4396 cache[instanceId][stringParams] = fn(...args);
4397 }
4398 return cache[instanceId][stringParams];
4399 };
4400 memoized.unmemoize = (...args) => {
4401 const stringParams = stringify(args);
4402 const instanceId = getInstanceId();
4403 if (cache[instanceId]?.[stringParams] !== undefined) {
4404 delete cache[instanceId][stringParams];
4405 }
4406 };
4407 return memoized;
4408 }
4409
4410 function nextTick(onExec, onError) {
4411 setTimeout(() => {
4412 Promise
4413 .resolve()
4414 .then(() => {
4415 onExec();
4416 })
4417 .catch((error) => {
4418 if (onError) {
4419 onError(error);
4420 }
4421 else {
4422 console.error(error);
4423 }
4424 });
4425 }, 0);
4426 }
4427
4428 function numberToHex(value, bitLength = -1) {
4429 const hex = (!value || Number.isNaN(value) ? 0 : value).toString(16);
4430 return hexFixLength(hex.length % 2 ? `0${hex}` : hex, bitLength, true);
4431 }
4432
4433 function numberToU8a(value, bitLength = -1) {
4434 return hexToU8a(numberToHex(value, bitLength));
4435 }
4436
4437 function objectClear(value) {
4438 const keys = Object.keys(value);
4439 for (let i = 0, count = keys.length; i < count; i++) {
4440 delete value[keys[i]];
4441 }
4442 return value;
4443 }
4444
4445 function objectSpread(dest, ...sources) {
4446 for (let i = 0, count = sources.length; i < count; i++) {
4447 const src = sources[i];
4448 if (src) {
4449 if (typeof src.entries === 'function') {
4450 for (const [key, value] of src.entries()) {
4451 dest[key] = value;
4452 }
4453 }
4454 else {
4455 Object.assign(dest, src);
4456 }
4457 }
4458 }
4459 return dest;
4460 }
4461
4462 function objectCopy(source) {
4463 return objectSpread({}, source);
4464 }
4465
4466 function objectEntries(obj) {
4467 return Object.entries(obj);
4468 }
4469
4470 function objectKeys(value) {
4471 return Object.keys(value);
4472 }
4473
4474 function objectProperty(that, key, getter, getName, index = 0) {
4475 const name = getName
4476 ? getName(key, index)
4477 : key;
4478 if (!(name in that)) {
4479 Object.defineProperty(that, name, {
4480 enumerable: true,
4481 get: function () {
4482 return getter(key, index, this);
4483 }
4484 });
4485 }
4486 }
4487 function objectProperties(that, keys, getter, getName) {
4488 for (let i = 0, count = keys.length; i < count; i++) {
4489 objectProperty(that, keys[i], getter, getName, i);
4490 }
4491 }
4492
4493 function objectValues(obj) {
4494 return Object.values(obj);
4495 }
4496
4497 function promisify(self, fn, ...params) {
4498 return new Promise((resolve, reject) => {
4499 fn.apply(self, params.concat((error, result) => {
4500 if (error) {
4501 reject(error);
4502 }
4503 else {
4504 resolve(result);
4505 }
4506 }));
4507 });
4508 }
4509
4510 const CC_TO_UP = new Array(256);
4511 const CC_TO_LO = new Array(256);
4512 for (let i = 0, count = CC_TO_UP.length; i < count; i++) {
4513 CC_TO_LO[i] = String.fromCharCode(i).toLowerCase();
4514 CC_TO_UP[i] = String.fromCharCode(i).toUpperCase();
4515 }
4516 function formatAllCaps(w) {
4517 return w.slice(0, w.length - 1).toLowerCase() + CC_TO_UP[w.charCodeAt(w.length - 1)];
4518 }
4519 function converter$1(format) {
4520 return (value) => {
4521 const parts = value
4522 .replace(/[-_., ]+/g, ' ')
4523 .trim()
4524 .split(' ');
4525 let result = '';
4526 for (let i = 0, count = parts.length; i < count; i++) {
4527 const w = parts[i];
4528 result += format(/^[\dA-Z]+$/.test(w)
4529 ? w.toLowerCase()
4530 : w.replace(/^[\dA-Z]{2,}[^a-z]/, formatAllCaps), i);
4531 }
4532 return result;
4533 };
4534 }
4535 const stringCamelCase = converter$1((w, i) =>
4536 (i ? CC_TO_UP[w.charCodeAt(0)] : CC_TO_LO[w.charCodeAt(0)]) + w.slice(1));
4537 const stringPascalCase = converter$1((w) =>
4538 CC_TO_UP[w.charCodeAt(0)] + w.slice(1));
4539
4540 function converter(map) {
4541 return (value) => value
4542 ? map[value.charCodeAt(0)] + value.slice(1)
4543 : '';
4544 }
4545 const stringLowerFirst = converter(CC_TO_LO);
4546 const stringUpperFirst = converter(CC_TO_UP);
4547
4548 function stringShorten(value, prefixLength = 6) {
4549 return value.length <= 2 + 2 * prefixLength
4550 ? value.toString()
4551 : `${value.substring(0, prefixLength)}${value.slice(-prefixLength)}`;
4552 }
4553
4554 function stringToHex(value) {
4555 return u8aToHex(stringToU8a(value));
4556 }
4557
4558 const DEDUPE = 'Either remove and explicitly install matching versions or dedupe using your package manager.\nThe following conflicting packages were found:';
4559 function getEntry(name) {
4560 const _global = xglobal;
4561 if (!_global.__polkadotjs) {
4562 _global.__polkadotjs = {};
4563 }
4564 if (!_global.__polkadotjs[name]) {
4565 _global.__polkadotjs[name] = [];
4566 }
4567 return _global.__polkadotjs[name];
4568 }
4569 function formatDisplay(all, fmt) {
4570 let max = 0;
4571 for (let i = 0, count = all.length; i < count; i++) {
4572 max = Math.max(max, all[i].version.length);
4573 }
4574 return all
4575 .map((d) => `\t${fmt(d.version.padEnd(max), d).join('\t')}`)
4576 .join('\n');
4577 }
4578 function formatInfo(version, { name }) {
4579 return [
4580 version,
4581 name
4582 ];
4583 }
4584 function formatVersion(version, { path, type }) {
4585 let extracted;
4586 if (path && path.length >= 5) {
4587 const nmIndex = path.indexOf('node_modules');
4588 extracted = nmIndex === -1
4589 ? path
4590 : path.substring(nmIndex);
4591 }
4592 else {
4593 extracted = '<unknown>';
4594 }
4595 return [
4596 `${`${type || ''}`.padStart(3)} ${version}`,
4597 extracted
4598 ];
4599 }
4600 function getPath(infoPath, pathOrFn) {
4601 if (infoPath) {
4602 return infoPath;
4603 }
4604 else if (isFunction(pathOrFn)) {
4605 try {
4606 return pathOrFn() || '';
4607 }
4608 catch {
4609 return '';
4610 }
4611 }
4612 return pathOrFn || '';
4613 }
4614 function warn(pre, all, fmt) {
4615 console.warn(`${pre}\n${DEDUPE}\n${formatDisplay(all, fmt)}`);
4616 }
4617 function detectPackage({ name, path, type, version }, pathOrFn, deps = []) {
4618 if (!name.startsWith('@polkadot')) {
4619 throw new Error(`Invalid package descriptor ${name}`);
4620 }
4621 const entry = getEntry(name);
4622 entry.push({ path: getPath(path, pathOrFn), type, version });
4623 if (entry.length !== 1) {
4624 warn(`${name} has multiple versions, ensure that there is only one installed.`, entry, formatVersion);
4625 }
4626 else {
4627 const mismatches = deps.filter((d) => d && d.version !== version);
4628 if (mismatches.length) {
4629 warn(`${name} requires direct dependencies exactly matching version ${version}.`, mismatches, formatInfo);
4630 }
4631 }
4632 }
4633
4634 exports.BN = BN;
4635 exports.BN_BILLION = BN_BILLION;
4636 exports.BN_EIGHT = BN_EIGHT;
4637 exports.BN_FIVE = BN_FIVE;
4638 exports.BN_FOUR = BN_FOUR;
4639 exports.BN_HUNDRED = BN_HUNDRED;
4640 exports.BN_MAX_INTEGER = BN_MAX_INTEGER;
4641 exports.BN_MILLION = BN_MILLION;
4642 exports.BN_NINE = BN_NINE;
4643 exports.BN_ONE = BN_ONE;
4644 exports.BN_QUINTILL = BN_QUINTILL;
4645 exports.BN_SEVEN = BN_SEVEN;
4646 exports.BN_SIX = BN_SIX;
4647 exports.BN_SQRT_MAX_INTEGER = BN_SQRT_MAX_INTEGER;
4648 exports.BN_TEN = BN_TEN;
4649 exports.BN_THOUSAND = BN_THOUSAND;
4650 exports.BN_THREE = BN_THREE;
4651 exports.BN_TWO = BN_TWO;
4652 exports.BN_ZERO = BN_ZERO;
4653 exports.U8A_WRAP_ETHEREUM = U8A_WRAP_ETHEREUM;
4654 exports.U8A_WRAP_POSTFIX = U8A_WRAP_POSTFIX;
4655 exports.U8A_WRAP_PREFIX = U8A_WRAP_PREFIX;
4656 exports._0n = _0n;
4657 exports._1000n = _1000n;
4658 exports._100n = _100n;
4659 exports._10n = _10n;
4660 exports._1Bn = _1Bn;
4661 exports._1Mn = _1Mn;
4662 exports._1Qn = _1Qn;
4663 exports._1n = _1n;
4664 exports._2n = _2n;
4665 exports._2pow53n = _2pow53n;
4666 exports._3n = _3n;
4667 exports._4n = _4n;
4668 exports._5n = _5n;
4669 exports._6n = _6n;
4670 exports._7n = _7n;
4671 exports._8n = _8n;
4672 exports._9n = _9n;
4673 exports._sqrt2pow53n = _sqrt2pow53n;
4674 exports.arrayChunk = arrayChunk;
4675 exports.arrayFilter = arrayFilter;
4676 exports.arrayFlatten = arrayFlatten;
4677 exports.arrayRange = arrayRange;
4678 exports.arrayShuffle = arrayShuffle;
4679 exports.arrayUnzip = arrayUnzip;
4680 exports.arrayZip = arrayZip;
4681 exports.assert = assert;
4682 exports.assertReturn = assertReturn;
4683 exports.assertUnreachable = assertUnreachable;
4684 exports.bnFromHex = hexToBn;
4685 exports.bnMax = bnMax;
4686 exports.bnMin = bnMin;
4687 exports.bnSqrt = bnSqrt;
4688 exports.bnToBn = bnToBn;
4689 exports.bnToHex = bnToHex;
4690 exports.bnToU8a = bnToU8a;
4691 exports.bufferToU8a = bufferToU8a;
4692 exports.calcSi = calcSi;
4693 exports.compactAddLength = compactAddLength;
4694 exports.compactFromU8a = compactFromU8a;
4695 exports.compactFromU8aLim = compactFromU8aLim;
4696 exports.compactStripLength = compactStripLength;
4697 exports.compactToU8a = compactToU8a;
4698 exports.detectPackage = detectPackage;
4699 exports.extractTime = extractTime;
4700 exports.findSi = findSi;
4701 exports.floatToU8a = floatToU8a;
4702 exports.formatBalance = formatBalance;
4703 exports.formatDate = formatDate;
4704 exports.formatDecimal = formatDecimal;
4705 exports.formatElapsed = formatElapsed;
4706 exports.formatNumber = formatNumber;
4707 exports.hasBigInt = hasBigInt;
4708 exports.hasBuffer = hasBuffer;
4709 exports.hasCjs = hasCjs;
4710 exports.hasDirname = hasDirname;
4711 exports.hasEsm = hasEsm;
4712 exports.hasProcess = hasProcess;
4713 exports.hasWasm = hasWasm;
4714 exports.hexAddPrefix = hexAddPrefix;
4715 exports.hexFixLength = hexFixLength;
4716 exports.hexHasPrefix = hexHasPrefix;
4717 exports.hexStripPrefix = hexStripPrefix;
4718 exports.hexToBigInt = hexToBigInt;
4719 exports.hexToBn = hexToBn;
4720 exports.hexToNumber = hexToNumber;
4721 exports.hexToString = hexToString;
4722 exports.hexToU8a = hexToU8a;
4723 exports.identity = identity;
4724 exports.isArray = isArray;
4725 exports.isAscii = isAscii;
4726 exports.isBigInt = isBigInt;
4727 exports.isBn = isBn;
4728 exports.isBoolean = isBoolean;
4729 exports.isBuffer = isBuffer;
4730 exports.isChildClass = isChildClass;
4731 exports.isClass = isClass;
4732 exports.isCodec = isCodec;
4733 exports.isCompact = isCompact;
4734 exports.isError = isError;
4735 exports.isFunction = isFunction;
4736 exports.isHex = isHex;
4737 exports.isInstanceOf = isInstanceOf;
4738 exports.isIp = isIp;
4739 exports.isJsonObject = isJsonObject;
4740 exports.isNull = isNull;
4741 exports.isNumber = isNumber;
4742 exports.isObject = isObject;
4743 exports.isObservable = isObservable;
4744 exports.isPromise = isPromise;
4745 exports.isRiscV = isRiscV;
4746 exports.isString = isString;
4747 exports.isTestChain = isTestChain;
4748 exports.isToBigInt = isToBigInt;
4749 exports.isToBn = isToBn;
4750 exports.isU8a = isU8a;
4751 exports.isUndefined = isUndefined;
4752 exports.isUtf8 = isUtf8;
4753 exports.isWasm = isWasm;
4754 exports.lazyMethod = lazyMethod;
4755 exports.lazyMethods = lazyMethods;
4756 exports.logger = logger;
4757 exports.loggerFormat = loggerFormat;
4758 exports.memoize = memoize;
4759 exports.nMax = nMax;
4760 exports.nMin = nMin;
4761 exports.nSqrt = nSqrt;
4762 exports.nToBigInt = nToBigInt;
4763 exports.nToHex = nToHex;
4764 exports.nToU8a = nToU8a;
4765 exports.nextTick = nextTick;
4766 exports.noop = noop;
4767 exports.numberToHex = numberToHex;
4768 exports.numberToU8a = numberToU8a;
4769 exports.objectClear = objectClear;
4770 exports.objectCopy = objectCopy;
4771 exports.objectEntries = objectEntries;
4772 exports.objectKeys = objectKeys;
4773 exports.objectProperties = objectProperties;
4774 exports.objectProperty = objectProperty;
4775 exports.objectSpread = objectSpread;
4776 exports.objectValues = objectValues;
4777 exports.packageInfo = packageInfo$3;
4778 exports.promisify = promisify;
4779 exports.stringCamelCase = stringCamelCase;
4780 exports.stringLowerFirst = stringLowerFirst;
4781 exports.stringPascalCase = stringPascalCase;
4782 exports.stringShorten = stringShorten;
4783 exports.stringToHex = stringToHex;
4784 exports.stringToU8a = stringToU8a;
4785 exports.stringUpperFirst = stringUpperFirst;
4786 exports.stringify = stringify;
4787 exports.u8aCmp = u8aCmp;
4788 exports.u8aConcat = u8aConcat;
4789 exports.u8aConcatStrict = u8aConcatStrict;
4790 exports.u8aEmpty = u8aEmpty;
4791 exports.u8aEq = u8aEq;
4792 exports.u8aFixLength = u8aFixLength;
4793 exports.u8aIsWrapped = u8aIsWrapped;
4794 exports.u8aSorted = u8aSorted;
4795 exports.u8aToBigInt = u8aToBigInt;
4796 exports.u8aToBn = u8aToBn;
4797 exports.u8aToBuffer = u8aToBuffer;
4798 exports.u8aToFloat = u8aToFloat;
4799 exports.u8aToHex = u8aToHex;
4800 exports.u8aToNumber = u8aToNumber;
4801 exports.u8aToString = u8aToString;
4802 exports.u8aToU8a = u8aToU8a;
4803 exports.u8aUnwrapBytes = u8aUnwrapBytes;
4804 exports.u8aWrapBytes = u8aWrapBytes;
4805
4806}));