@leofcoin/chain 1.4.36 → 1.4.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/exports/browser/chain.js +135 -4326
  2. package/exports/browser/client-de444350-a666f9b6.js +41075 -0
  3. package/exports/browser/contract-32687834.js +6071 -0
  4. package/exports/browser/{index-1ce30988-44319c80.js → index-712688fc-ad40bdec.js} +2 -2
  5. package/exports/browser/{messages-e025829d-a0354268.js → messages-3f370680-fb7e5b5b.js} +2 -2
  6. package/exports/browser/{node-browser-13cbe71a.js → node-browser-bd6a5de3.js} +4290 -187
  7. package/exports/browser/node-browser.js +2 -2
  8. package/exports/browser/workers/block-worker.js +6069 -0
  9. package/exports/browser/workers/machine-worker.js +6279 -0
  10. package/exports/chain.js +117 -96
  11. package/exports/node.js +2 -1
  12. package/exports/typings/chain.d.ts +5 -2
  13. package/exports/typings/contract.d.ts +1 -0
  14. package/exports/typings/node.d.ts +1 -0
  15. package/exports/typings/transaction.d.ts +1 -1
  16. package/package.json +4 -2
  17. package/exports/browser/browser-10ffabe1-10ffabe1.js +0 -56
  18. package/exports/browser/browser-e1cd4e67-e1cd4e67.js +0 -198
  19. package/exports/browser/client-111c93a3-b0dcdc19.js +0 -612
  20. package/exports/browser/client-1d0234a7-df40059a.js +0 -624
  21. package/exports/browser/client-21cee984-77440b6b.js +0 -624
  22. package/exports/browser/contract-163f031f.js +0 -1859
  23. package/exports/browser/index-3d3f56ca-064d16ce.js +0 -37
  24. package/exports/browser/index-543bc6a3-be806e11.js +0 -37
  25. package/exports/browser/messages-48deb2c3-73e8ace1.js +0 -205
  26. package/exports/browser/messages-af41e873-479a93a8.js +0 -205
  27. package/exports/browser/node-browser-63c7cb33.js +0 -16877
  28. package/exports/browser/node-browser-64b82fc8.js +0 -16877
  29. package/exports/browser/pako.esm-aa674ebf-aa674ebf.js +0 -6876
  30. package/exports/browser/simple-peer-743c19fe-03a0830e.js +0 -7856
@@ -1,4 +1,4 @@
1
- import { C as ContractMessage, T as TransactionMessage, B as BlockMessage, b as BWMessage, c as BWRequestMessage, V as ValidatorMessage } from './contract-163f031f.js';
1
+ import { C as ContractMessage, T as TransactionMessage, d as BlockMessage, e as BWMessage, f as BWRequestMessage, V as ValidatorMessage } from './contract-32687834.js';
2
2
 
3
3
  var nodeConfig = async (config = {
4
4
  network: 'leofcoin:peach',
@@ -566,7 +566,7 @@ const fromHex = (string) => Uint8Array.from(string.match(/.{1,2}/g).map((byte) =
566
566
  * @param bytes number[]
567
567
  * @returns hexString
568
568
  */
569
- const toHex$1 = (bytes) => bytes.reduce((string, byte) => string + byte.toString(16).padStart(2, '0'), '');
569
+ const toHex$2 = (bytes) => bytes.reduce((string, byte) => string + byte.toString(16).padStart(2, '0'), '');
570
570
  /**
571
571
  * number[] -> Uint8Array
572
572
  * @param array number[]
@@ -713,7 +713,7 @@ var index$2 = {
713
713
  fromString: fromString$1,
714
714
  toString: toString$1,
715
715
  fromHex,
716
- toHex: toHex$1,
716
+ toHex: toHex$2,
717
717
  fromArrayLike,
718
718
  toArrayLike,
719
719
  fromUintArrayString,
@@ -729,88 +729,4225 @@ var index$2 = {
729
729
  FormatInterface: FormatInterface$2
730
730
  };
731
731
 
732
- const { fromString, toString } = index$2;
733
- const isJson = (type) => type === 'object' || 'array';
734
- const isString = (type) => type === 'string';
735
- const isNumber = (type) => type === 'number';
736
- const isBoolean = (type) => type === 'boolean';
737
- const isUint8Array$1 = (type) => type === 'uint8Array';
738
- const tokenize = (key, value) => {
739
- const optional = key.endsWith('?');
740
- let type = value;
741
- type = Array.isArray(type) ? 'array' : typeof type;
742
- if (value instanceof Uint8Array)
743
- type = 'uint8Array';
744
- const parts = key.split('?');
745
- const minimumLength = parts[2]?.includes('min') ? parts[2].split['min:'][1] : 0;
746
- return { type, optional, key: parts[0], minimumLength };
747
- };
748
- const toType = (data) => {
749
- // always return uint8Arrays as they are
750
- if (data instanceof Uint8Array)
751
- return data;
752
- // returns the ArrayBuffer as a UintArray
753
- if (data instanceof ArrayBuffer)
754
- return new Uint8Array(data);
755
- // returns the string as a UintArray
756
- if (typeof data === 'string')
757
- return new TextEncoder().encode(data);
758
- // returns the object as a UintArray
759
- if (typeof data === 'object')
760
- return new TextEncoder().encode(JSON.stringify(data));
761
- // returns the number as a UintArray
762
- if (typeof data === 'number' || typeof data === 'boolean')
763
- return new TextEncoder().encode(data.toString());
764
- throw new Error(`unsuported type ${typeof data || data}`);
765
- };
766
- const encode$3 = (proto, input) => {
767
- const keys = Object.keys(proto);
768
- const values = Object.values(proto);
769
- const set = [];
770
- for (let i = 0; i < keys.length; i++) {
771
- const token = tokenize(keys[i], values[i]);
772
- const data = input[token.key];
773
- if (!token.optional && data === undefined)
774
- throw new Error(`missing required property: ${token.key}`);
775
- if (token.type === 'array' && token.minimumLength > data.length || token.type === 'object' && token.minimumLength > Object.keys(data).length)
776
- throw new Error(`minimumLength for ${token.key} is set to ${token.minimumLength} but got ${data.length}`);
777
- // always push data to the set.
778
- // when data is undefined push the default value of the proto
779
- set.push(toType(data || values[i]));
780
- }
781
- return index$6(set);
782
- };
783
- const decode$4 = (proto, uint8Array) => {
784
- let deconcated = index$5(uint8Array);
785
- const output = {};
786
- const keys = Object.keys(proto);
787
- const values = Object.values(proto);
788
- if (keys.length !== deconcated.length)
789
- console.warn(`length mismatch: expected ${keys.length} got ${uint8Array.length}`);
790
- for (let i = 0; i < keys.length; i++) {
791
- const token = tokenize(keys[i], values[i]);
792
- if (isUint8Array$1(token.type))
793
- output[token.key] = deconcated[i];
794
- else if (isString(token.type))
795
- output[token.key] = toString(deconcated[i]);
796
- else if (isBoolean(token.type))
797
- output[token.key] = Boolean(new TextDecoder().decode(deconcated[i]));
798
- else if (isNumber(token.type))
799
- output[token.key] = Number(new TextDecoder().decode(deconcated[i]));
800
- else if (isJson(token.type))
801
- output[token.key] = JSON.parse(new TextDecoder().decode(deconcated[i]));
802
- if (token.optional) {
803
- if (!output[token.key] || output[token.key].length === 0)
804
- delete output[token.key];
805
- }
806
- if (!token.optional && output[token.key] === undefined)
807
- throw new Error(`missing required property: ${token.key}`);
808
- }
809
- return output;
810
- };
811
- var index$1 = {
812
- encode: encode$3,
813
- decode: decode$4
732
+ var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
733
+
734
+ function getDefaultExportFromCjs (x) {
735
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
736
+ }
737
+
738
+ function getAugmentedNamespace(n) {
739
+ if (n.__esModule) return n;
740
+ var f = n.default;
741
+ if (typeof f == "function") {
742
+ var a = function a () {
743
+ if (this instanceof a) {
744
+ var args = [null];
745
+ args.push.apply(args, arguments);
746
+ var Ctor = Function.bind.apply(f, args);
747
+ return new Ctor();
748
+ }
749
+ return f.apply(this, arguments);
750
+ };
751
+ a.prototype = f.prototype;
752
+ } else a = {};
753
+ Object.defineProperty(a, '__esModule', {value: true});
754
+ Object.keys(n).forEach(function (k) {
755
+ var d = Object.getOwnPropertyDescriptor(n, k);
756
+ Object.defineProperty(a, k, d.get ? d : {
757
+ enumerable: true,
758
+ get: function () {
759
+ return n[k];
760
+ }
761
+ });
762
+ });
763
+ return a;
764
+ }
765
+
766
+ var bnExports$1 = {};
767
+ var bn$1 = {
768
+ get exports(){ return bnExports$1; },
769
+ set exports(v){ bnExports$1 = v; },
770
+ };
771
+
772
+ var _nodeResolve_empty = {};
773
+
774
+ var _nodeResolve_empty$1 = /*#__PURE__*/Object.freeze({
775
+ __proto__: null,
776
+ default: _nodeResolve_empty
777
+ });
778
+
779
+ var require$$0$1 = /*@__PURE__*/getAugmentedNamespace(_nodeResolve_empty$1);
780
+
781
+ (function (module) {
782
+ (function (module, exports) {
783
+
784
+ // Utils
785
+ function assert (val, msg) {
786
+ if (!val) throw new Error(msg || 'Assertion failed');
787
+ }
788
+
789
+ // Could use `inherits` module, but don't want to move from single file
790
+ // architecture yet.
791
+ function inherits (ctor, superCtor) {
792
+ ctor.super_ = superCtor;
793
+ var TempCtor = function () {};
794
+ TempCtor.prototype = superCtor.prototype;
795
+ ctor.prototype = new TempCtor();
796
+ ctor.prototype.constructor = ctor;
797
+ }
798
+
799
+ // BN
800
+
801
+ function BN (number, base, endian) {
802
+ if (BN.isBN(number)) {
803
+ return number;
804
+ }
805
+
806
+ this.negative = 0;
807
+ this.words = null;
808
+ this.length = 0;
809
+
810
+ // Reduction context
811
+ this.red = null;
812
+
813
+ if (number !== null) {
814
+ if (base === 'le' || base === 'be') {
815
+ endian = base;
816
+ base = 10;
817
+ }
818
+
819
+ this._init(number || 0, base || 10, endian || 'be');
820
+ }
821
+ }
822
+ if (typeof module === 'object') {
823
+ module.exports = BN;
824
+ } else {
825
+ exports.BN = BN;
826
+ }
827
+
828
+ BN.BN = BN;
829
+ BN.wordSize = 26;
830
+
831
+ var Buffer;
832
+ try {
833
+ if (typeof window !== 'undefined' && typeof window.Buffer !== 'undefined') {
834
+ Buffer = window.Buffer;
835
+ } else {
836
+ Buffer = require$$0$1.Buffer;
837
+ }
838
+ } catch (e) {
839
+ }
840
+
841
+ BN.isBN = function isBN (num) {
842
+ if (num instanceof BN) {
843
+ return true;
844
+ }
845
+
846
+ return num !== null && typeof num === 'object' &&
847
+ num.constructor.wordSize === BN.wordSize && Array.isArray(num.words);
848
+ };
849
+
850
+ BN.max = function max (left, right) {
851
+ if (left.cmp(right) > 0) return left;
852
+ return right;
853
+ };
854
+
855
+ BN.min = function min (left, right) {
856
+ if (left.cmp(right) < 0) return left;
857
+ return right;
858
+ };
859
+
860
+ BN.prototype._init = function init (number, base, endian) {
861
+ if (typeof number === 'number') {
862
+ return this._initNumber(number, base, endian);
863
+ }
864
+
865
+ if (typeof number === 'object') {
866
+ return this._initArray(number, base, endian);
867
+ }
868
+
869
+ if (base === 'hex') {
870
+ base = 16;
871
+ }
872
+ assert(base === (base | 0) && base >= 2 && base <= 36);
873
+
874
+ number = number.toString().replace(/\s+/g, '');
875
+ var start = 0;
876
+ if (number[0] === '-') {
877
+ start++;
878
+ this.negative = 1;
879
+ }
880
+
881
+ if (start < number.length) {
882
+ if (base === 16) {
883
+ this._parseHex(number, start, endian);
884
+ } else {
885
+ this._parseBase(number, base, start);
886
+ if (endian === 'le') {
887
+ this._initArray(this.toArray(), base, endian);
888
+ }
889
+ }
890
+ }
891
+ };
892
+
893
+ BN.prototype._initNumber = function _initNumber (number, base, endian) {
894
+ if (number < 0) {
895
+ this.negative = 1;
896
+ number = -number;
897
+ }
898
+ if (number < 0x4000000) {
899
+ this.words = [number & 0x3ffffff];
900
+ this.length = 1;
901
+ } else if (number < 0x10000000000000) {
902
+ this.words = [
903
+ number & 0x3ffffff,
904
+ (number / 0x4000000) & 0x3ffffff
905
+ ];
906
+ this.length = 2;
907
+ } else {
908
+ assert(number < 0x20000000000000); // 2 ^ 53 (unsafe)
909
+ this.words = [
910
+ number & 0x3ffffff,
911
+ (number / 0x4000000) & 0x3ffffff,
912
+ 1
913
+ ];
914
+ this.length = 3;
915
+ }
916
+
917
+ if (endian !== 'le') return;
918
+
919
+ // Reverse the bytes
920
+ this._initArray(this.toArray(), base, endian);
921
+ };
922
+
923
+ BN.prototype._initArray = function _initArray (number, base, endian) {
924
+ // Perhaps a Uint8Array
925
+ assert(typeof number.length === 'number');
926
+ if (number.length <= 0) {
927
+ this.words = [0];
928
+ this.length = 1;
929
+ return this;
930
+ }
931
+
932
+ this.length = Math.ceil(number.length / 3);
933
+ this.words = new Array(this.length);
934
+ for (var i = 0; i < this.length; i++) {
935
+ this.words[i] = 0;
936
+ }
937
+
938
+ var j, w;
939
+ var off = 0;
940
+ if (endian === 'be') {
941
+ for (i = number.length - 1, j = 0; i >= 0; i -= 3) {
942
+ w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16);
943
+ this.words[j] |= (w << off) & 0x3ffffff;
944
+ this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff;
945
+ off += 24;
946
+ if (off >= 26) {
947
+ off -= 26;
948
+ j++;
949
+ }
950
+ }
951
+ } else if (endian === 'le') {
952
+ for (i = 0, j = 0; i < number.length; i += 3) {
953
+ w = number[i] | (number[i + 1] << 8) | (number[i + 2] << 16);
954
+ this.words[j] |= (w << off) & 0x3ffffff;
955
+ this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff;
956
+ off += 24;
957
+ if (off >= 26) {
958
+ off -= 26;
959
+ j++;
960
+ }
961
+ }
962
+ }
963
+ return this._strip();
964
+ };
965
+
966
+ function parseHex4Bits (string, index) {
967
+ var c = string.charCodeAt(index);
968
+ // '0' - '9'
969
+ if (c >= 48 && c <= 57) {
970
+ return c - 48;
971
+ // 'A' - 'F'
972
+ } else if (c >= 65 && c <= 70) {
973
+ return c - 55;
974
+ // 'a' - 'f'
975
+ } else if (c >= 97 && c <= 102) {
976
+ return c - 87;
977
+ } else {
978
+ assert(false, 'Invalid character in ' + string);
979
+ }
980
+ }
981
+
982
+ function parseHexByte (string, lowerBound, index) {
983
+ var r = parseHex4Bits(string, index);
984
+ if (index - 1 >= lowerBound) {
985
+ r |= parseHex4Bits(string, index - 1) << 4;
986
+ }
987
+ return r;
988
+ }
989
+
990
+ BN.prototype._parseHex = function _parseHex (number, start, endian) {
991
+ // Create possibly bigger array to ensure that it fits the number
992
+ this.length = Math.ceil((number.length - start) / 6);
993
+ this.words = new Array(this.length);
994
+ for (var i = 0; i < this.length; i++) {
995
+ this.words[i] = 0;
996
+ }
997
+
998
+ // 24-bits chunks
999
+ var off = 0;
1000
+ var j = 0;
1001
+
1002
+ var w;
1003
+ if (endian === 'be') {
1004
+ for (i = number.length - 1; i >= start; i -= 2) {
1005
+ w = parseHexByte(number, start, i) << off;
1006
+ this.words[j] |= w & 0x3ffffff;
1007
+ if (off >= 18) {
1008
+ off -= 18;
1009
+ j += 1;
1010
+ this.words[j] |= w >>> 26;
1011
+ } else {
1012
+ off += 8;
1013
+ }
1014
+ }
1015
+ } else {
1016
+ var parseLength = number.length - start;
1017
+ for (i = parseLength % 2 === 0 ? start + 1 : start; i < number.length; i += 2) {
1018
+ w = parseHexByte(number, start, i) << off;
1019
+ this.words[j] |= w & 0x3ffffff;
1020
+ if (off >= 18) {
1021
+ off -= 18;
1022
+ j += 1;
1023
+ this.words[j] |= w >>> 26;
1024
+ } else {
1025
+ off += 8;
1026
+ }
1027
+ }
1028
+ }
1029
+
1030
+ this._strip();
1031
+ };
1032
+
1033
+ function parseBase (str, start, end, mul) {
1034
+ var r = 0;
1035
+ var b = 0;
1036
+ var len = Math.min(str.length, end);
1037
+ for (var i = start; i < len; i++) {
1038
+ var c = str.charCodeAt(i) - 48;
1039
+
1040
+ r *= mul;
1041
+
1042
+ // 'a'
1043
+ if (c >= 49) {
1044
+ b = c - 49 + 0xa;
1045
+
1046
+ // 'A'
1047
+ } else if (c >= 17) {
1048
+ b = c - 17 + 0xa;
1049
+
1050
+ // '0' - '9'
1051
+ } else {
1052
+ b = c;
1053
+ }
1054
+ assert(c >= 0 && b < mul, 'Invalid character');
1055
+ r += b;
1056
+ }
1057
+ return r;
1058
+ }
1059
+
1060
+ BN.prototype._parseBase = function _parseBase (number, base, start) {
1061
+ // Initialize as zero
1062
+ this.words = [0];
1063
+ this.length = 1;
1064
+
1065
+ // Find length of limb in base
1066
+ for (var limbLen = 0, limbPow = 1; limbPow <= 0x3ffffff; limbPow *= base) {
1067
+ limbLen++;
1068
+ }
1069
+ limbLen--;
1070
+ limbPow = (limbPow / base) | 0;
1071
+
1072
+ var total = number.length - start;
1073
+ var mod = total % limbLen;
1074
+ var end = Math.min(total, total - mod) + start;
1075
+
1076
+ var word = 0;
1077
+ for (var i = start; i < end; i += limbLen) {
1078
+ word = parseBase(number, i, i + limbLen, base);
1079
+
1080
+ this.imuln(limbPow);
1081
+ if (this.words[0] + word < 0x4000000) {
1082
+ this.words[0] += word;
1083
+ } else {
1084
+ this._iaddn(word);
1085
+ }
1086
+ }
1087
+
1088
+ if (mod !== 0) {
1089
+ var pow = 1;
1090
+ word = parseBase(number, i, number.length, base);
1091
+
1092
+ for (i = 0; i < mod; i++) {
1093
+ pow *= base;
1094
+ }
1095
+
1096
+ this.imuln(pow);
1097
+ if (this.words[0] + word < 0x4000000) {
1098
+ this.words[0] += word;
1099
+ } else {
1100
+ this._iaddn(word);
1101
+ }
1102
+ }
1103
+
1104
+ this._strip();
1105
+ };
1106
+
1107
+ BN.prototype.copy = function copy (dest) {
1108
+ dest.words = new Array(this.length);
1109
+ for (var i = 0; i < this.length; i++) {
1110
+ dest.words[i] = this.words[i];
1111
+ }
1112
+ dest.length = this.length;
1113
+ dest.negative = this.negative;
1114
+ dest.red = this.red;
1115
+ };
1116
+
1117
+ function move (dest, src) {
1118
+ dest.words = src.words;
1119
+ dest.length = src.length;
1120
+ dest.negative = src.negative;
1121
+ dest.red = src.red;
1122
+ }
1123
+
1124
+ BN.prototype._move = function _move (dest) {
1125
+ move(dest, this);
1126
+ };
1127
+
1128
+ BN.prototype.clone = function clone () {
1129
+ var r = new BN(null);
1130
+ this.copy(r);
1131
+ return r;
1132
+ };
1133
+
1134
+ BN.prototype._expand = function _expand (size) {
1135
+ while (this.length < size) {
1136
+ this.words[this.length++] = 0;
1137
+ }
1138
+ return this;
1139
+ };
1140
+
1141
+ // Remove leading `0` from `this`
1142
+ BN.prototype._strip = function strip () {
1143
+ while (this.length > 1 && this.words[this.length - 1] === 0) {
1144
+ this.length--;
1145
+ }
1146
+ return this._normSign();
1147
+ };
1148
+
1149
+ BN.prototype._normSign = function _normSign () {
1150
+ // -0 = 0
1151
+ if (this.length === 1 && this.words[0] === 0) {
1152
+ this.negative = 0;
1153
+ }
1154
+ return this;
1155
+ };
1156
+
1157
+ // Check Symbol.for because not everywhere where Symbol defined
1158
+ // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#Browser_compatibility
1159
+ if (typeof Symbol !== 'undefined' && typeof Symbol.for === 'function') {
1160
+ try {
1161
+ BN.prototype[Symbol.for('nodejs.util.inspect.custom')] = inspect;
1162
+ } catch (e) {
1163
+ BN.prototype.inspect = inspect;
1164
+ }
1165
+ } else {
1166
+ BN.prototype.inspect = inspect;
1167
+ }
1168
+
1169
+ function inspect () {
1170
+ return (this.red ? '<BN-R: ' : '<BN: ') + this.toString(16) + '>';
1171
+ }
1172
+
1173
+ /*
1174
+
1175
+ var zeros = [];
1176
+ var groupSizes = [];
1177
+ var groupBases = [];
1178
+
1179
+ var s = '';
1180
+ var i = -1;
1181
+ while (++i < BN.wordSize) {
1182
+ zeros[i] = s;
1183
+ s += '0';
1184
+ }
1185
+ groupSizes[0] = 0;
1186
+ groupSizes[1] = 0;
1187
+ groupBases[0] = 0;
1188
+ groupBases[1] = 0;
1189
+ var base = 2 - 1;
1190
+ while (++base < 36 + 1) {
1191
+ var groupSize = 0;
1192
+ var groupBase = 1;
1193
+ while (groupBase < (1 << BN.wordSize) / base) {
1194
+ groupBase *= base;
1195
+ groupSize += 1;
1196
+ }
1197
+ groupSizes[base] = groupSize;
1198
+ groupBases[base] = groupBase;
1199
+ }
1200
+
1201
+ */
1202
+
1203
+ var zeros = [
1204
+ '',
1205
+ '0',
1206
+ '00',
1207
+ '000',
1208
+ '0000',
1209
+ '00000',
1210
+ '000000',
1211
+ '0000000',
1212
+ '00000000',
1213
+ '000000000',
1214
+ '0000000000',
1215
+ '00000000000',
1216
+ '000000000000',
1217
+ '0000000000000',
1218
+ '00000000000000',
1219
+ '000000000000000',
1220
+ '0000000000000000',
1221
+ '00000000000000000',
1222
+ '000000000000000000',
1223
+ '0000000000000000000',
1224
+ '00000000000000000000',
1225
+ '000000000000000000000',
1226
+ '0000000000000000000000',
1227
+ '00000000000000000000000',
1228
+ '000000000000000000000000',
1229
+ '0000000000000000000000000'
1230
+ ];
1231
+
1232
+ var groupSizes = [
1233
+ 0, 0,
1234
+ 25, 16, 12, 11, 10, 9, 8,
1235
+ 8, 7, 7, 7, 7, 6, 6,
1236
+ 6, 6, 6, 6, 6, 5, 5,
1237
+ 5, 5, 5, 5, 5, 5, 5,
1238
+ 5, 5, 5, 5, 5, 5, 5
1239
+ ];
1240
+
1241
+ var groupBases = [
1242
+ 0, 0,
1243
+ 33554432, 43046721, 16777216, 48828125, 60466176, 40353607, 16777216,
1244
+ 43046721, 10000000, 19487171, 35831808, 62748517, 7529536, 11390625,
1245
+ 16777216, 24137569, 34012224, 47045881, 64000000, 4084101, 5153632,
1246
+ 6436343, 7962624, 9765625, 11881376, 14348907, 17210368, 20511149,
1247
+ 24300000, 28629151, 33554432, 39135393, 45435424, 52521875, 60466176
1248
+ ];
1249
+
1250
+ BN.prototype.toString = function toString (base, padding) {
1251
+ base = base || 10;
1252
+ padding = padding | 0 || 1;
1253
+
1254
+ var out;
1255
+ if (base === 16 || base === 'hex') {
1256
+ out = '';
1257
+ var off = 0;
1258
+ var carry = 0;
1259
+ for (var i = 0; i < this.length; i++) {
1260
+ var w = this.words[i];
1261
+ var word = (((w << off) | carry) & 0xffffff).toString(16);
1262
+ carry = (w >>> (24 - off)) & 0xffffff;
1263
+ off += 2;
1264
+ if (off >= 26) {
1265
+ off -= 26;
1266
+ i--;
1267
+ }
1268
+ if (carry !== 0 || i !== this.length - 1) {
1269
+ out = zeros[6 - word.length] + word + out;
1270
+ } else {
1271
+ out = word + out;
1272
+ }
1273
+ }
1274
+ if (carry !== 0) {
1275
+ out = carry.toString(16) + out;
1276
+ }
1277
+ while (out.length % padding !== 0) {
1278
+ out = '0' + out;
1279
+ }
1280
+ if (this.negative !== 0) {
1281
+ out = '-' + out;
1282
+ }
1283
+ return out;
1284
+ }
1285
+
1286
+ if (base === (base | 0) && base >= 2 && base <= 36) {
1287
+ // var groupSize = Math.floor(BN.wordSize * Math.LN2 / Math.log(base));
1288
+ var groupSize = groupSizes[base];
1289
+ // var groupBase = Math.pow(base, groupSize);
1290
+ var groupBase = groupBases[base];
1291
+ out = '';
1292
+ var c = this.clone();
1293
+ c.negative = 0;
1294
+ while (!c.isZero()) {
1295
+ var r = c.modrn(groupBase).toString(base);
1296
+ c = c.idivn(groupBase);
1297
+
1298
+ if (!c.isZero()) {
1299
+ out = zeros[groupSize - r.length] + r + out;
1300
+ } else {
1301
+ out = r + out;
1302
+ }
1303
+ }
1304
+ if (this.isZero()) {
1305
+ out = '0' + out;
1306
+ }
1307
+ while (out.length % padding !== 0) {
1308
+ out = '0' + out;
1309
+ }
1310
+ if (this.negative !== 0) {
1311
+ out = '-' + out;
1312
+ }
1313
+ return out;
1314
+ }
1315
+
1316
+ assert(false, 'Base should be between 2 and 36');
1317
+ };
1318
+
1319
+ BN.prototype.toNumber = function toNumber () {
1320
+ var ret = this.words[0];
1321
+ if (this.length === 2) {
1322
+ ret += this.words[1] * 0x4000000;
1323
+ } else if (this.length === 3 && this.words[2] === 0x01) {
1324
+ // NOTE: at this stage it is known that the top bit is set
1325
+ ret += 0x10000000000000 + (this.words[1] * 0x4000000);
1326
+ } else if (this.length > 2) {
1327
+ assert(false, 'Number can only safely store up to 53 bits');
1328
+ }
1329
+ return (this.negative !== 0) ? -ret : ret;
1330
+ };
1331
+
1332
+ BN.prototype.toJSON = function toJSON () {
1333
+ return this.toString(16, 2);
1334
+ };
1335
+
1336
+ if (Buffer) {
1337
+ BN.prototype.toBuffer = function toBuffer (endian, length) {
1338
+ return this.toArrayLike(Buffer, endian, length);
1339
+ };
1340
+ }
1341
+
1342
+ BN.prototype.toArray = function toArray (endian, length) {
1343
+ return this.toArrayLike(Array, endian, length);
1344
+ };
1345
+
1346
+ var allocate = function allocate (ArrayType, size) {
1347
+ if (ArrayType.allocUnsafe) {
1348
+ return ArrayType.allocUnsafe(size);
1349
+ }
1350
+ return new ArrayType(size);
1351
+ };
1352
+
1353
+ BN.prototype.toArrayLike = function toArrayLike (ArrayType, endian, length) {
1354
+ this._strip();
1355
+
1356
+ var byteLength = this.byteLength();
1357
+ var reqLength = length || Math.max(1, byteLength);
1358
+ assert(byteLength <= reqLength, 'byte array longer than desired length');
1359
+ assert(reqLength > 0, 'Requested array length <= 0');
1360
+
1361
+ var res = allocate(ArrayType, reqLength);
1362
+ var postfix = endian === 'le' ? 'LE' : 'BE';
1363
+ this['_toArrayLike' + postfix](res, byteLength);
1364
+ return res;
1365
+ };
1366
+
1367
+ BN.prototype._toArrayLikeLE = function _toArrayLikeLE (res, byteLength) {
1368
+ var position = 0;
1369
+ var carry = 0;
1370
+
1371
+ for (var i = 0, shift = 0; i < this.length; i++) {
1372
+ var word = (this.words[i] << shift) | carry;
1373
+
1374
+ res[position++] = word & 0xff;
1375
+ if (position < res.length) {
1376
+ res[position++] = (word >> 8) & 0xff;
1377
+ }
1378
+ if (position < res.length) {
1379
+ res[position++] = (word >> 16) & 0xff;
1380
+ }
1381
+
1382
+ if (shift === 6) {
1383
+ if (position < res.length) {
1384
+ res[position++] = (word >> 24) & 0xff;
1385
+ }
1386
+ carry = 0;
1387
+ shift = 0;
1388
+ } else {
1389
+ carry = word >>> 24;
1390
+ shift += 2;
1391
+ }
1392
+ }
1393
+
1394
+ if (position < res.length) {
1395
+ res[position++] = carry;
1396
+
1397
+ while (position < res.length) {
1398
+ res[position++] = 0;
1399
+ }
1400
+ }
1401
+ };
1402
+
1403
+ BN.prototype._toArrayLikeBE = function _toArrayLikeBE (res, byteLength) {
1404
+ var position = res.length - 1;
1405
+ var carry = 0;
1406
+
1407
+ for (var i = 0, shift = 0; i < this.length; i++) {
1408
+ var word = (this.words[i] << shift) | carry;
1409
+
1410
+ res[position--] = word & 0xff;
1411
+ if (position >= 0) {
1412
+ res[position--] = (word >> 8) & 0xff;
1413
+ }
1414
+ if (position >= 0) {
1415
+ res[position--] = (word >> 16) & 0xff;
1416
+ }
1417
+
1418
+ if (shift === 6) {
1419
+ if (position >= 0) {
1420
+ res[position--] = (word >> 24) & 0xff;
1421
+ }
1422
+ carry = 0;
1423
+ shift = 0;
1424
+ } else {
1425
+ carry = word >>> 24;
1426
+ shift += 2;
1427
+ }
1428
+ }
1429
+
1430
+ if (position >= 0) {
1431
+ res[position--] = carry;
1432
+
1433
+ while (position >= 0) {
1434
+ res[position--] = 0;
1435
+ }
1436
+ }
1437
+ };
1438
+
1439
+ if (Math.clz32) {
1440
+ BN.prototype._countBits = function _countBits (w) {
1441
+ return 32 - Math.clz32(w);
1442
+ };
1443
+ } else {
1444
+ BN.prototype._countBits = function _countBits (w) {
1445
+ var t = w;
1446
+ var r = 0;
1447
+ if (t >= 0x1000) {
1448
+ r += 13;
1449
+ t >>>= 13;
1450
+ }
1451
+ if (t >= 0x40) {
1452
+ r += 7;
1453
+ t >>>= 7;
1454
+ }
1455
+ if (t >= 0x8) {
1456
+ r += 4;
1457
+ t >>>= 4;
1458
+ }
1459
+ if (t >= 0x02) {
1460
+ r += 2;
1461
+ t >>>= 2;
1462
+ }
1463
+ return r + t;
1464
+ };
1465
+ }
1466
+
1467
+ BN.prototype._zeroBits = function _zeroBits (w) {
1468
+ // Short-cut
1469
+ if (w === 0) return 26;
1470
+
1471
+ var t = w;
1472
+ var r = 0;
1473
+ if ((t & 0x1fff) === 0) {
1474
+ r += 13;
1475
+ t >>>= 13;
1476
+ }
1477
+ if ((t & 0x7f) === 0) {
1478
+ r += 7;
1479
+ t >>>= 7;
1480
+ }
1481
+ if ((t & 0xf) === 0) {
1482
+ r += 4;
1483
+ t >>>= 4;
1484
+ }
1485
+ if ((t & 0x3) === 0) {
1486
+ r += 2;
1487
+ t >>>= 2;
1488
+ }
1489
+ if ((t & 0x1) === 0) {
1490
+ r++;
1491
+ }
1492
+ return r;
1493
+ };
1494
+
1495
+ // Return number of used bits in a BN
1496
+ BN.prototype.bitLength = function bitLength () {
1497
+ var w = this.words[this.length - 1];
1498
+ var hi = this._countBits(w);
1499
+ return (this.length - 1) * 26 + hi;
1500
+ };
1501
+
1502
+ function toBitArray (num) {
1503
+ var w = new Array(num.bitLength());
1504
+
1505
+ for (var bit = 0; bit < w.length; bit++) {
1506
+ var off = (bit / 26) | 0;
1507
+ var wbit = bit % 26;
1508
+
1509
+ w[bit] = (num.words[off] >>> wbit) & 0x01;
1510
+ }
1511
+
1512
+ return w;
1513
+ }
1514
+
1515
+ // Number of trailing zero bits
1516
+ BN.prototype.zeroBits = function zeroBits () {
1517
+ if (this.isZero()) return 0;
1518
+
1519
+ var r = 0;
1520
+ for (var i = 0; i < this.length; i++) {
1521
+ var b = this._zeroBits(this.words[i]);
1522
+ r += b;
1523
+ if (b !== 26) break;
1524
+ }
1525
+ return r;
1526
+ };
1527
+
1528
+ BN.prototype.byteLength = function byteLength () {
1529
+ return Math.ceil(this.bitLength() / 8);
1530
+ };
1531
+
1532
+ BN.prototype.toTwos = function toTwos (width) {
1533
+ if (this.negative !== 0) {
1534
+ return this.abs().inotn(width).iaddn(1);
1535
+ }
1536
+ return this.clone();
1537
+ };
1538
+
1539
+ BN.prototype.fromTwos = function fromTwos (width) {
1540
+ if (this.testn(width - 1)) {
1541
+ return this.notn(width).iaddn(1).ineg();
1542
+ }
1543
+ return this.clone();
1544
+ };
1545
+
1546
+ BN.prototype.isNeg = function isNeg () {
1547
+ return this.negative !== 0;
1548
+ };
1549
+
1550
+ // Return negative clone of `this`
1551
+ BN.prototype.neg = function neg () {
1552
+ return this.clone().ineg();
1553
+ };
1554
+
1555
+ BN.prototype.ineg = function ineg () {
1556
+ if (!this.isZero()) {
1557
+ this.negative ^= 1;
1558
+ }
1559
+
1560
+ return this;
1561
+ };
1562
+
1563
+ // Or `num` with `this` in-place
1564
+ BN.prototype.iuor = function iuor (num) {
1565
+ while (this.length < num.length) {
1566
+ this.words[this.length++] = 0;
1567
+ }
1568
+
1569
+ for (var i = 0; i < num.length; i++) {
1570
+ this.words[i] = this.words[i] | num.words[i];
1571
+ }
1572
+
1573
+ return this._strip();
1574
+ };
1575
+
1576
+ BN.prototype.ior = function ior (num) {
1577
+ assert((this.negative | num.negative) === 0);
1578
+ return this.iuor(num);
1579
+ };
1580
+
1581
+ // Or `num` with `this`
1582
+ BN.prototype.or = function or (num) {
1583
+ if (this.length > num.length) return this.clone().ior(num);
1584
+ return num.clone().ior(this);
1585
+ };
1586
+
1587
+ BN.prototype.uor = function uor (num) {
1588
+ if (this.length > num.length) return this.clone().iuor(num);
1589
+ return num.clone().iuor(this);
1590
+ };
1591
+
1592
+ // And `num` with `this` in-place
1593
+ BN.prototype.iuand = function iuand (num) {
1594
+ // b = min-length(num, this)
1595
+ var b;
1596
+ if (this.length > num.length) {
1597
+ b = num;
1598
+ } else {
1599
+ b = this;
1600
+ }
1601
+
1602
+ for (var i = 0; i < b.length; i++) {
1603
+ this.words[i] = this.words[i] & num.words[i];
1604
+ }
1605
+
1606
+ this.length = b.length;
1607
+
1608
+ return this._strip();
1609
+ };
1610
+
1611
+ BN.prototype.iand = function iand (num) {
1612
+ assert((this.negative | num.negative) === 0);
1613
+ return this.iuand(num);
1614
+ };
1615
+
1616
+ // And `num` with `this`
1617
+ BN.prototype.and = function and (num) {
1618
+ if (this.length > num.length) return this.clone().iand(num);
1619
+ return num.clone().iand(this);
1620
+ };
1621
+
1622
+ BN.prototype.uand = function uand (num) {
1623
+ if (this.length > num.length) return this.clone().iuand(num);
1624
+ return num.clone().iuand(this);
1625
+ };
1626
+
1627
+ // Xor `num` with `this` in-place
1628
+ BN.prototype.iuxor = function iuxor (num) {
1629
+ // a.length > b.length
1630
+ var a;
1631
+ var b;
1632
+ if (this.length > num.length) {
1633
+ a = this;
1634
+ b = num;
1635
+ } else {
1636
+ a = num;
1637
+ b = this;
1638
+ }
1639
+
1640
+ for (var i = 0; i < b.length; i++) {
1641
+ this.words[i] = a.words[i] ^ b.words[i];
1642
+ }
1643
+
1644
+ if (this !== a) {
1645
+ for (; i < a.length; i++) {
1646
+ this.words[i] = a.words[i];
1647
+ }
1648
+ }
1649
+
1650
+ this.length = a.length;
1651
+
1652
+ return this._strip();
1653
+ };
1654
+
1655
+ BN.prototype.ixor = function ixor (num) {
1656
+ assert((this.negative | num.negative) === 0);
1657
+ return this.iuxor(num);
1658
+ };
1659
+
1660
+ // Xor `num` with `this`
1661
+ BN.prototype.xor = function xor (num) {
1662
+ if (this.length > num.length) return this.clone().ixor(num);
1663
+ return num.clone().ixor(this);
1664
+ };
1665
+
1666
+ BN.prototype.uxor = function uxor (num) {
1667
+ if (this.length > num.length) return this.clone().iuxor(num);
1668
+ return num.clone().iuxor(this);
1669
+ };
1670
+
1671
+ // Not ``this`` with ``width`` bitwidth
1672
+ BN.prototype.inotn = function inotn (width) {
1673
+ assert(typeof width === 'number' && width >= 0);
1674
+
1675
+ var bytesNeeded = Math.ceil(width / 26) | 0;
1676
+ var bitsLeft = width % 26;
1677
+
1678
+ // Extend the buffer with leading zeroes
1679
+ this._expand(bytesNeeded);
1680
+
1681
+ if (bitsLeft > 0) {
1682
+ bytesNeeded--;
1683
+ }
1684
+
1685
+ // Handle complete words
1686
+ for (var i = 0; i < bytesNeeded; i++) {
1687
+ this.words[i] = ~this.words[i] & 0x3ffffff;
1688
+ }
1689
+
1690
+ // Handle the residue
1691
+ if (bitsLeft > 0) {
1692
+ this.words[i] = ~this.words[i] & (0x3ffffff >> (26 - bitsLeft));
1693
+ }
1694
+
1695
+ // And remove leading zeroes
1696
+ return this._strip();
1697
+ };
1698
+
1699
+ BN.prototype.notn = function notn (width) {
1700
+ return this.clone().inotn(width);
1701
+ };
1702
+
1703
+ // Set `bit` of `this`
1704
+ BN.prototype.setn = function setn (bit, val) {
1705
+ assert(typeof bit === 'number' && bit >= 0);
1706
+
1707
+ var off = (bit / 26) | 0;
1708
+ var wbit = bit % 26;
1709
+
1710
+ this._expand(off + 1);
1711
+
1712
+ if (val) {
1713
+ this.words[off] = this.words[off] | (1 << wbit);
1714
+ } else {
1715
+ this.words[off] = this.words[off] & ~(1 << wbit);
1716
+ }
1717
+
1718
+ return this._strip();
1719
+ };
1720
+
1721
+ // Add `num` to `this` in-place
1722
+ BN.prototype.iadd = function iadd (num) {
1723
+ var r;
1724
+
1725
+ // negative + positive
1726
+ if (this.negative !== 0 && num.negative === 0) {
1727
+ this.negative = 0;
1728
+ r = this.isub(num);
1729
+ this.negative ^= 1;
1730
+ return this._normSign();
1731
+
1732
+ // positive + negative
1733
+ } else if (this.negative === 0 && num.negative !== 0) {
1734
+ num.negative = 0;
1735
+ r = this.isub(num);
1736
+ num.negative = 1;
1737
+ return r._normSign();
1738
+ }
1739
+
1740
+ // a.length > b.length
1741
+ var a, b;
1742
+ if (this.length > num.length) {
1743
+ a = this;
1744
+ b = num;
1745
+ } else {
1746
+ a = num;
1747
+ b = this;
1748
+ }
1749
+
1750
+ var carry = 0;
1751
+ for (var i = 0; i < b.length; i++) {
1752
+ r = (a.words[i] | 0) + (b.words[i] | 0) + carry;
1753
+ this.words[i] = r & 0x3ffffff;
1754
+ carry = r >>> 26;
1755
+ }
1756
+ for (; carry !== 0 && i < a.length; i++) {
1757
+ r = (a.words[i] | 0) + carry;
1758
+ this.words[i] = r & 0x3ffffff;
1759
+ carry = r >>> 26;
1760
+ }
1761
+
1762
+ this.length = a.length;
1763
+ if (carry !== 0) {
1764
+ this.words[this.length] = carry;
1765
+ this.length++;
1766
+ // Copy the rest of the words
1767
+ } else if (a !== this) {
1768
+ for (; i < a.length; i++) {
1769
+ this.words[i] = a.words[i];
1770
+ }
1771
+ }
1772
+
1773
+ return this;
1774
+ };
1775
+
1776
+ // Add `num` to `this`
1777
+ BN.prototype.add = function add (num) {
1778
+ var res;
1779
+ if (num.negative !== 0 && this.negative === 0) {
1780
+ num.negative = 0;
1781
+ res = this.sub(num);
1782
+ num.negative ^= 1;
1783
+ return res;
1784
+ } else if (num.negative === 0 && this.negative !== 0) {
1785
+ this.negative = 0;
1786
+ res = num.sub(this);
1787
+ this.negative = 1;
1788
+ return res;
1789
+ }
1790
+
1791
+ if (this.length > num.length) return this.clone().iadd(num);
1792
+
1793
+ return num.clone().iadd(this);
1794
+ };
1795
+
1796
+ // Subtract `num` from `this` in-place
1797
+ BN.prototype.isub = function isub (num) {
1798
+ // this - (-num) = this + num
1799
+ if (num.negative !== 0) {
1800
+ num.negative = 0;
1801
+ var r = this.iadd(num);
1802
+ num.negative = 1;
1803
+ return r._normSign();
1804
+
1805
+ // -this - num = -(this + num)
1806
+ } else if (this.negative !== 0) {
1807
+ this.negative = 0;
1808
+ this.iadd(num);
1809
+ this.negative = 1;
1810
+ return this._normSign();
1811
+ }
1812
+
1813
+ // At this point both numbers are positive
1814
+ var cmp = this.cmp(num);
1815
+
1816
+ // Optimization - zeroify
1817
+ if (cmp === 0) {
1818
+ this.negative = 0;
1819
+ this.length = 1;
1820
+ this.words[0] = 0;
1821
+ return this;
1822
+ }
1823
+
1824
+ // a > b
1825
+ var a, b;
1826
+ if (cmp > 0) {
1827
+ a = this;
1828
+ b = num;
1829
+ } else {
1830
+ a = num;
1831
+ b = this;
1832
+ }
1833
+
1834
+ var carry = 0;
1835
+ for (var i = 0; i < b.length; i++) {
1836
+ r = (a.words[i] | 0) - (b.words[i] | 0) + carry;
1837
+ carry = r >> 26;
1838
+ this.words[i] = r & 0x3ffffff;
1839
+ }
1840
+ for (; carry !== 0 && i < a.length; i++) {
1841
+ r = (a.words[i] | 0) + carry;
1842
+ carry = r >> 26;
1843
+ this.words[i] = r & 0x3ffffff;
1844
+ }
1845
+
1846
+ // Copy rest of the words
1847
+ if (carry === 0 && i < a.length && a !== this) {
1848
+ for (; i < a.length; i++) {
1849
+ this.words[i] = a.words[i];
1850
+ }
1851
+ }
1852
+
1853
+ this.length = Math.max(this.length, i);
1854
+
1855
+ if (a !== this) {
1856
+ this.negative = 1;
1857
+ }
1858
+
1859
+ return this._strip();
1860
+ };
1861
+
1862
+ // Subtract `num` from `this`
1863
+ BN.prototype.sub = function sub (num) {
1864
+ return this.clone().isub(num);
1865
+ };
1866
+
1867
+ function smallMulTo (self, num, out) {
1868
+ out.negative = num.negative ^ self.negative;
1869
+ var len = (self.length + num.length) | 0;
1870
+ out.length = len;
1871
+ len = (len - 1) | 0;
1872
+
1873
+ // Peel one iteration (compiler can't do it, because of code complexity)
1874
+ var a = self.words[0] | 0;
1875
+ var b = num.words[0] | 0;
1876
+ var r = a * b;
1877
+
1878
+ var lo = r & 0x3ffffff;
1879
+ var carry = (r / 0x4000000) | 0;
1880
+ out.words[0] = lo;
1881
+
1882
+ for (var k = 1; k < len; k++) {
1883
+ // Sum all words with the same `i + j = k` and accumulate `ncarry`,
1884
+ // note that ncarry could be >= 0x3ffffff
1885
+ var ncarry = carry >>> 26;
1886
+ var rword = carry & 0x3ffffff;
1887
+ var maxJ = Math.min(k, num.length - 1);
1888
+ for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) {
1889
+ var i = (k - j) | 0;
1890
+ a = self.words[i] | 0;
1891
+ b = num.words[j] | 0;
1892
+ r = a * b + rword;
1893
+ ncarry += (r / 0x4000000) | 0;
1894
+ rword = r & 0x3ffffff;
1895
+ }
1896
+ out.words[k] = rword | 0;
1897
+ carry = ncarry | 0;
1898
+ }
1899
+ if (carry !== 0) {
1900
+ out.words[k] = carry | 0;
1901
+ } else {
1902
+ out.length--;
1903
+ }
1904
+
1905
+ return out._strip();
1906
+ }
1907
+
1908
+ // TODO(indutny): it may be reasonable to omit it for users who don't need
1909
+ // to work with 256-bit numbers, otherwise it gives 20% improvement for 256-bit
1910
+ // multiplication (like elliptic secp256k1).
1911
+ var comb10MulTo = function comb10MulTo (self, num, out) {
1912
+ var a = self.words;
1913
+ var b = num.words;
1914
+ var o = out.words;
1915
+ var c = 0;
1916
+ var lo;
1917
+ var mid;
1918
+ var hi;
1919
+ var a0 = a[0] | 0;
1920
+ var al0 = a0 & 0x1fff;
1921
+ var ah0 = a0 >>> 13;
1922
+ var a1 = a[1] | 0;
1923
+ var al1 = a1 & 0x1fff;
1924
+ var ah1 = a1 >>> 13;
1925
+ var a2 = a[2] | 0;
1926
+ var al2 = a2 & 0x1fff;
1927
+ var ah2 = a2 >>> 13;
1928
+ var a3 = a[3] | 0;
1929
+ var al3 = a3 & 0x1fff;
1930
+ var ah3 = a3 >>> 13;
1931
+ var a4 = a[4] | 0;
1932
+ var al4 = a4 & 0x1fff;
1933
+ var ah4 = a4 >>> 13;
1934
+ var a5 = a[5] | 0;
1935
+ var al5 = a5 & 0x1fff;
1936
+ var ah5 = a5 >>> 13;
1937
+ var a6 = a[6] | 0;
1938
+ var al6 = a6 & 0x1fff;
1939
+ var ah6 = a6 >>> 13;
1940
+ var a7 = a[7] | 0;
1941
+ var al7 = a7 & 0x1fff;
1942
+ var ah7 = a7 >>> 13;
1943
+ var a8 = a[8] | 0;
1944
+ var al8 = a8 & 0x1fff;
1945
+ var ah8 = a8 >>> 13;
1946
+ var a9 = a[9] | 0;
1947
+ var al9 = a9 & 0x1fff;
1948
+ var ah9 = a9 >>> 13;
1949
+ var b0 = b[0] | 0;
1950
+ var bl0 = b0 & 0x1fff;
1951
+ var bh0 = b0 >>> 13;
1952
+ var b1 = b[1] | 0;
1953
+ var bl1 = b1 & 0x1fff;
1954
+ var bh1 = b1 >>> 13;
1955
+ var b2 = b[2] | 0;
1956
+ var bl2 = b2 & 0x1fff;
1957
+ var bh2 = b2 >>> 13;
1958
+ var b3 = b[3] | 0;
1959
+ var bl3 = b3 & 0x1fff;
1960
+ var bh3 = b3 >>> 13;
1961
+ var b4 = b[4] | 0;
1962
+ var bl4 = b4 & 0x1fff;
1963
+ var bh4 = b4 >>> 13;
1964
+ var b5 = b[5] | 0;
1965
+ var bl5 = b5 & 0x1fff;
1966
+ var bh5 = b5 >>> 13;
1967
+ var b6 = b[6] | 0;
1968
+ var bl6 = b6 & 0x1fff;
1969
+ var bh6 = b6 >>> 13;
1970
+ var b7 = b[7] | 0;
1971
+ var bl7 = b7 & 0x1fff;
1972
+ var bh7 = b7 >>> 13;
1973
+ var b8 = b[8] | 0;
1974
+ var bl8 = b8 & 0x1fff;
1975
+ var bh8 = b8 >>> 13;
1976
+ var b9 = b[9] | 0;
1977
+ var bl9 = b9 & 0x1fff;
1978
+ var bh9 = b9 >>> 13;
1979
+
1980
+ out.negative = self.negative ^ num.negative;
1981
+ out.length = 19;
1982
+ /* k = 0 */
1983
+ lo = Math.imul(al0, bl0);
1984
+ mid = Math.imul(al0, bh0);
1985
+ mid = (mid + Math.imul(ah0, bl0)) | 0;
1986
+ hi = Math.imul(ah0, bh0);
1987
+ var w0 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
1988
+ c = (((hi + (mid >>> 13)) | 0) + (w0 >>> 26)) | 0;
1989
+ w0 &= 0x3ffffff;
1990
+ /* k = 1 */
1991
+ lo = Math.imul(al1, bl0);
1992
+ mid = Math.imul(al1, bh0);
1993
+ mid = (mid + Math.imul(ah1, bl0)) | 0;
1994
+ hi = Math.imul(ah1, bh0);
1995
+ lo = (lo + Math.imul(al0, bl1)) | 0;
1996
+ mid = (mid + Math.imul(al0, bh1)) | 0;
1997
+ mid = (mid + Math.imul(ah0, bl1)) | 0;
1998
+ hi = (hi + Math.imul(ah0, bh1)) | 0;
1999
+ var w1 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2000
+ c = (((hi + (mid >>> 13)) | 0) + (w1 >>> 26)) | 0;
2001
+ w1 &= 0x3ffffff;
2002
+ /* k = 2 */
2003
+ lo = Math.imul(al2, bl0);
2004
+ mid = Math.imul(al2, bh0);
2005
+ mid = (mid + Math.imul(ah2, bl0)) | 0;
2006
+ hi = Math.imul(ah2, bh0);
2007
+ lo = (lo + Math.imul(al1, bl1)) | 0;
2008
+ mid = (mid + Math.imul(al1, bh1)) | 0;
2009
+ mid = (mid + Math.imul(ah1, bl1)) | 0;
2010
+ hi = (hi + Math.imul(ah1, bh1)) | 0;
2011
+ lo = (lo + Math.imul(al0, bl2)) | 0;
2012
+ mid = (mid + Math.imul(al0, bh2)) | 0;
2013
+ mid = (mid + Math.imul(ah0, bl2)) | 0;
2014
+ hi = (hi + Math.imul(ah0, bh2)) | 0;
2015
+ var w2 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2016
+ c = (((hi + (mid >>> 13)) | 0) + (w2 >>> 26)) | 0;
2017
+ w2 &= 0x3ffffff;
2018
+ /* k = 3 */
2019
+ lo = Math.imul(al3, bl0);
2020
+ mid = Math.imul(al3, bh0);
2021
+ mid = (mid + Math.imul(ah3, bl0)) | 0;
2022
+ hi = Math.imul(ah3, bh0);
2023
+ lo = (lo + Math.imul(al2, bl1)) | 0;
2024
+ mid = (mid + Math.imul(al2, bh1)) | 0;
2025
+ mid = (mid + Math.imul(ah2, bl1)) | 0;
2026
+ hi = (hi + Math.imul(ah2, bh1)) | 0;
2027
+ lo = (lo + Math.imul(al1, bl2)) | 0;
2028
+ mid = (mid + Math.imul(al1, bh2)) | 0;
2029
+ mid = (mid + Math.imul(ah1, bl2)) | 0;
2030
+ hi = (hi + Math.imul(ah1, bh2)) | 0;
2031
+ lo = (lo + Math.imul(al0, bl3)) | 0;
2032
+ mid = (mid + Math.imul(al0, bh3)) | 0;
2033
+ mid = (mid + Math.imul(ah0, bl3)) | 0;
2034
+ hi = (hi + Math.imul(ah0, bh3)) | 0;
2035
+ var w3 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2036
+ c = (((hi + (mid >>> 13)) | 0) + (w3 >>> 26)) | 0;
2037
+ w3 &= 0x3ffffff;
2038
+ /* k = 4 */
2039
+ lo = Math.imul(al4, bl0);
2040
+ mid = Math.imul(al4, bh0);
2041
+ mid = (mid + Math.imul(ah4, bl0)) | 0;
2042
+ hi = Math.imul(ah4, bh0);
2043
+ lo = (lo + Math.imul(al3, bl1)) | 0;
2044
+ mid = (mid + Math.imul(al3, bh1)) | 0;
2045
+ mid = (mid + Math.imul(ah3, bl1)) | 0;
2046
+ hi = (hi + Math.imul(ah3, bh1)) | 0;
2047
+ lo = (lo + Math.imul(al2, bl2)) | 0;
2048
+ mid = (mid + Math.imul(al2, bh2)) | 0;
2049
+ mid = (mid + Math.imul(ah2, bl2)) | 0;
2050
+ hi = (hi + Math.imul(ah2, bh2)) | 0;
2051
+ lo = (lo + Math.imul(al1, bl3)) | 0;
2052
+ mid = (mid + Math.imul(al1, bh3)) | 0;
2053
+ mid = (mid + Math.imul(ah1, bl3)) | 0;
2054
+ hi = (hi + Math.imul(ah1, bh3)) | 0;
2055
+ lo = (lo + Math.imul(al0, bl4)) | 0;
2056
+ mid = (mid + Math.imul(al0, bh4)) | 0;
2057
+ mid = (mid + Math.imul(ah0, bl4)) | 0;
2058
+ hi = (hi + Math.imul(ah0, bh4)) | 0;
2059
+ var w4 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2060
+ c = (((hi + (mid >>> 13)) | 0) + (w4 >>> 26)) | 0;
2061
+ w4 &= 0x3ffffff;
2062
+ /* k = 5 */
2063
+ lo = Math.imul(al5, bl0);
2064
+ mid = Math.imul(al5, bh0);
2065
+ mid = (mid + Math.imul(ah5, bl0)) | 0;
2066
+ hi = Math.imul(ah5, bh0);
2067
+ lo = (lo + Math.imul(al4, bl1)) | 0;
2068
+ mid = (mid + Math.imul(al4, bh1)) | 0;
2069
+ mid = (mid + Math.imul(ah4, bl1)) | 0;
2070
+ hi = (hi + Math.imul(ah4, bh1)) | 0;
2071
+ lo = (lo + Math.imul(al3, bl2)) | 0;
2072
+ mid = (mid + Math.imul(al3, bh2)) | 0;
2073
+ mid = (mid + Math.imul(ah3, bl2)) | 0;
2074
+ hi = (hi + Math.imul(ah3, bh2)) | 0;
2075
+ lo = (lo + Math.imul(al2, bl3)) | 0;
2076
+ mid = (mid + Math.imul(al2, bh3)) | 0;
2077
+ mid = (mid + Math.imul(ah2, bl3)) | 0;
2078
+ hi = (hi + Math.imul(ah2, bh3)) | 0;
2079
+ lo = (lo + Math.imul(al1, bl4)) | 0;
2080
+ mid = (mid + Math.imul(al1, bh4)) | 0;
2081
+ mid = (mid + Math.imul(ah1, bl4)) | 0;
2082
+ hi = (hi + Math.imul(ah1, bh4)) | 0;
2083
+ lo = (lo + Math.imul(al0, bl5)) | 0;
2084
+ mid = (mid + Math.imul(al0, bh5)) | 0;
2085
+ mid = (mid + Math.imul(ah0, bl5)) | 0;
2086
+ hi = (hi + Math.imul(ah0, bh5)) | 0;
2087
+ var w5 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2088
+ c = (((hi + (mid >>> 13)) | 0) + (w5 >>> 26)) | 0;
2089
+ w5 &= 0x3ffffff;
2090
+ /* k = 6 */
2091
+ lo = Math.imul(al6, bl0);
2092
+ mid = Math.imul(al6, bh0);
2093
+ mid = (mid + Math.imul(ah6, bl0)) | 0;
2094
+ hi = Math.imul(ah6, bh0);
2095
+ lo = (lo + Math.imul(al5, bl1)) | 0;
2096
+ mid = (mid + Math.imul(al5, bh1)) | 0;
2097
+ mid = (mid + Math.imul(ah5, bl1)) | 0;
2098
+ hi = (hi + Math.imul(ah5, bh1)) | 0;
2099
+ lo = (lo + Math.imul(al4, bl2)) | 0;
2100
+ mid = (mid + Math.imul(al4, bh2)) | 0;
2101
+ mid = (mid + Math.imul(ah4, bl2)) | 0;
2102
+ hi = (hi + Math.imul(ah4, bh2)) | 0;
2103
+ lo = (lo + Math.imul(al3, bl3)) | 0;
2104
+ mid = (mid + Math.imul(al3, bh3)) | 0;
2105
+ mid = (mid + Math.imul(ah3, bl3)) | 0;
2106
+ hi = (hi + Math.imul(ah3, bh3)) | 0;
2107
+ lo = (lo + Math.imul(al2, bl4)) | 0;
2108
+ mid = (mid + Math.imul(al2, bh4)) | 0;
2109
+ mid = (mid + Math.imul(ah2, bl4)) | 0;
2110
+ hi = (hi + Math.imul(ah2, bh4)) | 0;
2111
+ lo = (lo + Math.imul(al1, bl5)) | 0;
2112
+ mid = (mid + Math.imul(al1, bh5)) | 0;
2113
+ mid = (mid + Math.imul(ah1, bl5)) | 0;
2114
+ hi = (hi + Math.imul(ah1, bh5)) | 0;
2115
+ lo = (lo + Math.imul(al0, bl6)) | 0;
2116
+ mid = (mid + Math.imul(al0, bh6)) | 0;
2117
+ mid = (mid + Math.imul(ah0, bl6)) | 0;
2118
+ hi = (hi + Math.imul(ah0, bh6)) | 0;
2119
+ var w6 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2120
+ c = (((hi + (mid >>> 13)) | 0) + (w6 >>> 26)) | 0;
2121
+ w6 &= 0x3ffffff;
2122
+ /* k = 7 */
2123
+ lo = Math.imul(al7, bl0);
2124
+ mid = Math.imul(al7, bh0);
2125
+ mid = (mid + Math.imul(ah7, bl0)) | 0;
2126
+ hi = Math.imul(ah7, bh0);
2127
+ lo = (lo + Math.imul(al6, bl1)) | 0;
2128
+ mid = (mid + Math.imul(al6, bh1)) | 0;
2129
+ mid = (mid + Math.imul(ah6, bl1)) | 0;
2130
+ hi = (hi + Math.imul(ah6, bh1)) | 0;
2131
+ lo = (lo + Math.imul(al5, bl2)) | 0;
2132
+ mid = (mid + Math.imul(al5, bh2)) | 0;
2133
+ mid = (mid + Math.imul(ah5, bl2)) | 0;
2134
+ hi = (hi + Math.imul(ah5, bh2)) | 0;
2135
+ lo = (lo + Math.imul(al4, bl3)) | 0;
2136
+ mid = (mid + Math.imul(al4, bh3)) | 0;
2137
+ mid = (mid + Math.imul(ah4, bl3)) | 0;
2138
+ hi = (hi + Math.imul(ah4, bh3)) | 0;
2139
+ lo = (lo + Math.imul(al3, bl4)) | 0;
2140
+ mid = (mid + Math.imul(al3, bh4)) | 0;
2141
+ mid = (mid + Math.imul(ah3, bl4)) | 0;
2142
+ hi = (hi + Math.imul(ah3, bh4)) | 0;
2143
+ lo = (lo + Math.imul(al2, bl5)) | 0;
2144
+ mid = (mid + Math.imul(al2, bh5)) | 0;
2145
+ mid = (mid + Math.imul(ah2, bl5)) | 0;
2146
+ hi = (hi + Math.imul(ah2, bh5)) | 0;
2147
+ lo = (lo + Math.imul(al1, bl6)) | 0;
2148
+ mid = (mid + Math.imul(al1, bh6)) | 0;
2149
+ mid = (mid + Math.imul(ah1, bl6)) | 0;
2150
+ hi = (hi + Math.imul(ah1, bh6)) | 0;
2151
+ lo = (lo + Math.imul(al0, bl7)) | 0;
2152
+ mid = (mid + Math.imul(al0, bh7)) | 0;
2153
+ mid = (mid + Math.imul(ah0, bl7)) | 0;
2154
+ hi = (hi + Math.imul(ah0, bh7)) | 0;
2155
+ var w7 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2156
+ c = (((hi + (mid >>> 13)) | 0) + (w7 >>> 26)) | 0;
2157
+ w7 &= 0x3ffffff;
2158
+ /* k = 8 */
2159
+ lo = Math.imul(al8, bl0);
2160
+ mid = Math.imul(al8, bh0);
2161
+ mid = (mid + Math.imul(ah8, bl0)) | 0;
2162
+ hi = Math.imul(ah8, bh0);
2163
+ lo = (lo + Math.imul(al7, bl1)) | 0;
2164
+ mid = (mid + Math.imul(al7, bh1)) | 0;
2165
+ mid = (mid + Math.imul(ah7, bl1)) | 0;
2166
+ hi = (hi + Math.imul(ah7, bh1)) | 0;
2167
+ lo = (lo + Math.imul(al6, bl2)) | 0;
2168
+ mid = (mid + Math.imul(al6, bh2)) | 0;
2169
+ mid = (mid + Math.imul(ah6, bl2)) | 0;
2170
+ hi = (hi + Math.imul(ah6, bh2)) | 0;
2171
+ lo = (lo + Math.imul(al5, bl3)) | 0;
2172
+ mid = (mid + Math.imul(al5, bh3)) | 0;
2173
+ mid = (mid + Math.imul(ah5, bl3)) | 0;
2174
+ hi = (hi + Math.imul(ah5, bh3)) | 0;
2175
+ lo = (lo + Math.imul(al4, bl4)) | 0;
2176
+ mid = (mid + Math.imul(al4, bh4)) | 0;
2177
+ mid = (mid + Math.imul(ah4, bl4)) | 0;
2178
+ hi = (hi + Math.imul(ah4, bh4)) | 0;
2179
+ lo = (lo + Math.imul(al3, bl5)) | 0;
2180
+ mid = (mid + Math.imul(al3, bh5)) | 0;
2181
+ mid = (mid + Math.imul(ah3, bl5)) | 0;
2182
+ hi = (hi + Math.imul(ah3, bh5)) | 0;
2183
+ lo = (lo + Math.imul(al2, bl6)) | 0;
2184
+ mid = (mid + Math.imul(al2, bh6)) | 0;
2185
+ mid = (mid + Math.imul(ah2, bl6)) | 0;
2186
+ hi = (hi + Math.imul(ah2, bh6)) | 0;
2187
+ lo = (lo + Math.imul(al1, bl7)) | 0;
2188
+ mid = (mid + Math.imul(al1, bh7)) | 0;
2189
+ mid = (mid + Math.imul(ah1, bl7)) | 0;
2190
+ hi = (hi + Math.imul(ah1, bh7)) | 0;
2191
+ lo = (lo + Math.imul(al0, bl8)) | 0;
2192
+ mid = (mid + Math.imul(al0, bh8)) | 0;
2193
+ mid = (mid + Math.imul(ah0, bl8)) | 0;
2194
+ hi = (hi + Math.imul(ah0, bh8)) | 0;
2195
+ var w8 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2196
+ c = (((hi + (mid >>> 13)) | 0) + (w8 >>> 26)) | 0;
2197
+ w8 &= 0x3ffffff;
2198
+ /* k = 9 */
2199
+ lo = Math.imul(al9, bl0);
2200
+ mid = Math.imul(al9, bh0);
2201
+ mid = (mid + Math.imul(ah9, bl0)) | 0;
2202
+ hi = Math.imul(ah9, bh0);
2203
+ lo = (lo + Math.imul(al8, bl1)) | 0;
2204
+ mid = (mid + Math.imul(al8, bh1)) | 0;
2205
+ mid = (mid + Math.imul(ah8, bl1)) | 0;
2206
+ hi = (hi + Math.imul(ah8, bh1)) | 0;
2207
+ lo = (lo + Math.imul(al7, bl2)) | 0;
2208
+ mid = (mid + Math.imul(al7, bh2)) | 0;
2209
+ mid = (mid + Math.imul(ah7, bl2)) | 0;
2210
+ hi = (hi + Math.imul(ah7, bh2)) | 0;
2211
+ lo = (lo + Math.imul(al6, bl3)) | 0;
2212
+ mid = (mid + Math.imul(al6, bh3)) | 0;
2213
+ mid = (mid + Math.imul(ah6, bl3)) | 0;
2214
+ hi = (hi + Math.imul(ah6, bh3)) | 0;
2215
+ lo = (lo + Math.imul(al5, bl4)) | 0;
2216
+ mid = (mid + Math.imul(al5, bh4)) | 0;
2217
+ mid = (mid + Math.imul(ah5, bl4)) | 0;
2218
+ hi = (hi + Math.imul(ah5, bh4)) | 0;
2219
+ lo = (lo + Math.imul(al4, bl5)) | 0;
2220
+ mid = (mid + Math.imul(al4, bh5)) | 0;
2221
+ mid = (mid + Math.imul(ah4, bl5)) | 0;
2222
+ hi = (hi + Math.imul(ah4, bh5)) | 0;
2223
+ lo = (lo + Math.imul(al3, bl6)) | 0;
2224
+ mid = (mid + Math.imul(al3, bh6)) | 0;
2225
+ mid = (mid + Math.imul(ah3, bl6)) | 0;
2226
+ hi = (hi + Math.imul(ah3, bh6)) | 0;
2227
+ lo = (lo + Math.imul(al2, bl7)) | 0;
2228
+ mid = (mid + Math.imul(al2, bh7)) | 0;
2229
+ mid = (mid + Math.imul(ah2, bl7)) | 0;
2230
+ hi = (hi + Math.imul(ah2, bh7)) | 0;
2231
+ lo = (lo + Math.imul(al1, bl8)) | 0;
2232
+ mid = (mid + Math.imul(al1, bh8)) | 0;
2233
+ mid = (mid + Math.imul(ah1, bl8)) | 0;
2234
+ hi = (hi + Math.imul(ah1, bh8)) | 0;
2235
+ lo = (lo + Math.imul(al0, bl9)) | 0;
2236
+ mid = (mid + Math.imul(al0, bh9)) | 0;
2237
+ mid = (mid + Math.imul(ah0, bl9)) | 0;
2238
+ hi = (hi + Math.imul(ah0, bh9)) | 0;
2239
+ var w9 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2240
+ c = (((hi + (mid >>> 13)) | 0) + (w9 >>> 26)) | 0;
2241
+ w9 &= 0x3ffffff;
2242
+ /* k = 10 */
2243
+ lo = Math.imul(al9, bl1);
2244
+ mid = Math.imul(al9, bh1);
2245
+ mid = (mid + Math.imul(ah9, bl1)) | 0;
2246
+ hi = Math.imul(ah9, bh1);
2247
+ lo = (lo + Math.imul(al8, bl2)) | 0;
2248
+ mid = (mid + Math.imul(al8, bh2)) | 0;
2249
+ mid = (mid + Math.imul(ah8, bl2)) | 0;
2250
+ hi = (hi + Math.imul(ah8, bh2)) | 0;
2251
+ lo = (lo + Math.imul(al7, bl3)) | 0;
2252
+ mid = (mid + Math.imul(al7, bh3)) | 0;
2253
+ mid = (mid + Math.imul(ah7, bl3)) | 0;
2254
+ hi = (hi + Math.imul(ah7, bh3)) | 0;
2255
+ lo = (lo + Math.imul(al6, bl4)) | 0;
2256
+ mid = (mid + Math.imul(al6, bh4)) | 0;
2257
+ mid = (mid + Math.imul(ah6, bl4)) | 0;
2258
+ hi = (hi + Math.imul(ah6, bh4)) | 0;
2259
+ lo = (lo + Math.imul(al5, bl5)) | 0;
2260
+ mid = (mid + Math.imul(al5, bh5)) | 0;
2261
+ mid = (mid + Math.imul(ah5, bl5)) | 0;
2262
+ hi = (hi + Math.imul(ah5, bh5)) | 0;
2263
+ lo = (lo + Math.imul(al4, bl6)) | 0;
2264
+ mid = (mid + Math.imul(al4, bh6)) | 0;
2265
+ mid = (mid + Math.imul(ah4, bl6)) | 0;
2266
+ hi = (hi + Math.imul(ah4, bh6)) | 0;
2267
+ lo = (lo + Math.imul(al3, bl7)) | 0;
2268
+ mid = (mid + Math.imul(al3, bh7)) | 0;
2269
+ mid = (mid + Math.imul(ah3, bl7)) | 0;
2270
+ hi = (hi + Math.imul(ah3, bh7)) | 0;
2271
+ lo = (lo + Math.imul(al2, bl8)) | 0;
2272
+ mid = (mid + Math.imul(al2, bh8)) | 0;
2273
+ mid = (mid + Math.imul(ah2, bl8)) | 0;
2274
+ hi = (hi + Math.imul(ah2, bh8)) | 0;
2275
+ lo = (lo + Math.imul(al1, bl9)) | 0;
2276
+ mid = (mid + Math.imul(al1, bh9)) | 0;
2277
+ mid = (mid + Math.imul(ah1, bl9)) | 0;
2278
+ hi = (hi + Math.imul(ah1, bh9)) | 0;
2279
+ var w10 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2280
+ c = (((hi + (mid >>> 13)) | 0) + (w10 >>> 26)) | 0;
2281
+ w10 &= 0x3ffffff;
2282
+ /* k = 11 */
2283
+ lo = Math.imul(al9, bl2);
2284
+ mid = Math.imul(al9, bh2);
2285
+ mid = (mid + Math.imul(ah9, bl2)) | 0;
2286
+ hi = Math.imul(ah9, bh2);
2287
+ lo = (lo + Math.imul(al8, bl3)) | 0;
2288
+ mid = (mid + Math.imul(al8, bh3)) | 0;
2289
+ mid = (mid + Math.imul(ah8, bl3)) | 0;
2290
+ hi = (hi + Math.imul(ah8, bh3)) | 0;
2291
+ lo = (lo + Math.imul(al7, bl4)) | 0;
2292
+ mid = (mid + Math.imul(al7, bh4)) | 0;
2293
+ mid = (mid + Math.imul(ah7, bl4)) | 0;
2294
+ hi = (hi + Math.imul(ah7, bh4)) | 0;
2295
+ lo = (lo + Math.imul(al6, bl5)) | 0;
2296
+ mid = (mid + Math.imul(al6, bh5)) | 0;
2297
+ mid = (mid + Math.imul(ah6, bl5)) | 0;
2298
+ hi = (hi + Math.imul(ah6, bh5)) | 0;
2299
+ lo = (lo + Math.imul(al5, bl6)) | 0;
2300
+ mid = (mid + Math.imul(al5, bh6)) | 0;
2301
+ mid = (mid + Math.imul(ah5, bl6)) | 0;
2302
+ hi = (hi + Math.imul(ah5, bh6)) | 0;
2303
+ lo = (lo + Math.imul(al4, bl7)) | 0;
2304
+ mid = (mid + Math.imul(al4, bh7)) | 0;
2305
+ mid = (mid + Math.imul(ah4, bl7)) | 0;
2306
+ hi = (hi + Math.imul(ah4, bh7)) | 0;
2307
+ lo = (lo + Math.imul(al3, bl8)) | 0;
2308
+ mid = (mid + Math.imul(al3, bh8)) | 0;
2309
+ mid = (mid + Math.imul(ah3, bl8)) | 0;
2310
+ hi = (hi + Math.imul(ah3, bh8)) | 0;
2311
+ lo = (lo + Math.imul(al2, bl9)) | 0;
2312
+ mid = (mid + Math.imul(al2, bh9)) | 0;
2313
+ mid = (mid + Math.imul(ah2, bl9)) | 0;
2314
+ hi = (hi + Math.imul(ah2, bh9)) | 0;
2315
+ var w11 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2316
+ c = (((hi + (mid >>> 13)) | 0) + (w11 >>> 26)) | 0;
2317
+ w11 &= 0x3ffffff;
2318
+ /* k = 12 */
2319
+ lo = Math.imul(al9, bl3);
2320
+ mid = Math.imul(al9, bh3);
2321
+ mid = (mid + Math.imul(ah9, bl3)) | 0;
2322
+ hi = Math.imul(ah9, bh3);
2323
+ lo = (lo + Math.imul(al8, bl4)) | 0;
2324
+ mid = (mid + Math.imul(al8, bh4)) | 0;
2325
+ mid = (mid + Math.imul(ah8, bl4)) | 0;
2326
+ hi = (hi + Math.imul(ah8, bh4)) | 0;
2327
+ lo = (lo + Math.imul(al7, bl5)) | 0;
2328
+ mid = (mid + Math.imul(al7, bh5)) | 0;
2329
+ mid = (mid + Math.imul(ah7, bl5)) | 0;
2330
+ hi = (hi + Math.imul(ah7, bh5)) | 0;
2331
+ lo = (lo + Math.imul(al6, bl6)) | 0;
2332
+ mid = (mid + Math.imul(al6, bh6)) | 0;
2333
+ mid = (mid + Math.imul(ah6, bl6)) | 0;
2334
+ hi = (hi + Math.imul(ah6, bh6)) | 0;
2335
+ lo = (lo + Math.imul(al5, bl7)) | 0;
2336
+ mid = (mid + Math.imul(al5, bh7)) | 0;
2337
+ mid = (mid + Math.imul(ah5, bl7)) | 0;
2338
+ hi = (hi + Math.imul(ah5, bh7)) | 0;
2339
+ lo = (lo + Math.imul(al4, bl8)) | 0;
2340
+ mid = (mid + Math.imul(al4, bh8)) | 0;
2341
+ mid = (mid + Math.imul(ah4, bl8)) | 0;
2342
+ hi = (hi + Math.imul(ah4, bh8)) | 0;
2343
+ lo = (lo + Math.imul(al3, bl9)) | 0;
2344
+ mid = (mid + Math.imul(al3, bh9)) | 0;
2345
+ mid = (mid + Math.imul(ah3, bl9)) | 0;
2346
+ hi = (hi + Math.imul(ah3, bh9)) | 0;
2347
+ var w12 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2348
+ c = (((hi + (mid >>> 13)) | 0) + (w12 >>> 26)) | 0;
2349
+ w12 &= 0x3ffffff;
2350
+ /* k = 13 */
2351
+ lo = Math.imul(al9, bl4);
2352
+ mid = Math.imul(al9, bh4);
2353
+ mid = (mid + Math.imul(ah9, bl4)) | 0;
2354
+ hi = Math.imul(ah9, bh4);
2355
+ lo = (lo + Math.imul(al8, bl5)) | 0;
2356
+ mid = (mid + Math.imul(al8, bh5)) | 0;
2357
+ mid = (mid + Math.imul(ah8, bl5)) | 0;
2358
+ hi = (hi + Math.imul(ah8, bh5)) | 0;
2359
+ lo = (lo + Math.imul(al7, bl6)) | 0;
2360
+ mid = (mid + Math.imul(al7, bh6)) | 0;
2361
+ mid = (mid + Math.imul(ah7, bl6)) | 0;
2362
+ hi = (hi + Math.imul(ah7, bh6)) | 0;
2363
+ lo = (lo + Math.imul(al6, bl7)) | 0;
2364
+ mid = (mid + Math.imul(al6, bh7)) | 0;
2365
+ mid = (mid + Math.imul(ah6, bl7)) | 0;
2366
+ hi = (hi + Math.imul(ah6, bh7)) | 0;
2367
+ lo = (lo + Math.imul(al5, bl8)) | 0;
2368
+ mid = (mid + Math.imul(al5, bh8)) | 0;
2369
+ mid = (mid + Math.imul(ah5, bl8)) | 0;
2370
+ hi = (hi + Math.imul(ah5, bh8)) | 0;
2371
+ lo = (lo + Math.imul(al4, bl9)) | 0;
2372
+ mid = (mid + Math.imul(al4, bh9)) | 0;
2373
+ mid = (mid + Math.imul(ah4, bl9)) | 0;
2374
+ hi = (hi + Math.imul(ah4, bh9)) | 0;
2375
+ var w13 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2376
+ c = (((hi + (mid >>> 13)) | 0) + (w13 >>> 26)) | 0;
2377
+ w13 &= 0x3ffffff;
2378
+ /* k = 14 */
2379
+ lo = Math.imul(al9, bl5);
2380
+ mid = Math.imul(al9, bh5);
2381
+ mid = (mid + Math.imul(ah9, bl5)) | 0;
2382
+ hi = Math.imul(ah9, bh5);
2383
+ lo = (lo + Math.imul(al8, bl6)) | 0;
2384
+ mid = (mid + Math.imul(al8, bh6)) | 0;
2385
+ mid = (mid + Math.imul(ah8, bl6)) | 0;
2386
+ hi = (hi + Math.imul(ah8, bh6)) | 0;
2387
+ lo = (lo + Math.imul(al7, bl7)) | 0;
2388
+ mid = (mid + Math.imul(al7, bh7)) | 0;
2389
+ mid = (mid + Math.imul(ah7, bl7)) | 0;
2390
+ hi = (hi + Math.imul(ah7, bh7)) | 0;
2391
+ lo = (lo + Math.imul(al6, bl8)) | 0;
2392
+ mid = (mid + Math.imul(al6, bh8)) | 0;
2393
+ mid = (mid + Math.imul(ah6, bl8)) | 0;
2394
+ hi = (hi + Math.imul(ah6, bh8)) | 0;
2395
+ lo = (lo + Math.imul(al5, bl9)) | 0;
2396
+ mid = (mid + Math.imul(al5, bh9)) | 0;
2397
+ mid = (mid + Math.imul(ah5, bl9)) | 0;
2398
+ hi = (hi + Math.imul(ah5, bh9)) | 0;
2399
+ var w14 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2400
+ c = (((hi + (mid >>> 13)) | 0) + (w14 >>> 26)) | 0;
2401
+ w14 &= 0x3ffffff;
2402
+ /* k = 15 */
2403
+ lo = Math.imul(al9, bl6);
2404
+ mid = Math.imul(al9, bh6);
2405
+ mid = (mid + Math.imul(ah9, bl6)) | 0;
2406
+ hi = Math.imul(ah9, bh6);
2407
+ lo = (lo + Math.imul(al8, bl7)) | 0;
2408
+ mid = (mid + Math.imul(al8, bh7)) | 0;
2409
+ mid = (mid + Math.imul(ah8, bl7)) | 0;
2410
+ hi = (hi + Math.imul(ah8, bh7)) | 0;
2411
+ lo = (lo + Math.imul(al7, bl8)) | 0;
2412
+ mid = (mid + Math.imul(al7, bh8)) | 0;
2413
+ mid = (mid + Math.imul(ah7, bl8)) | 0;
2414
+ hi = (hi + Math.imul(ah7, bh8)) | 0;
2415
+ lo = (lo + Math.imul(al6, bl9)) | 0;
2416
+ mid = (mid + Math.imul(al6, bh9)) | 0;
2417
+ mid = (mid + Math.imul(ah6, bl9)) | 0;
2418
+ hi = (hi + Math.imul(ah6, bh9)) | 0;
2419
+ var w15 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2420
+ c = (((hi + (mid >>> 13)) | 0) + (w15 >>> 26)) | 0;
2421
+ w15 &= 0x3ffffff;
2422
+ /* k = 16 */
2423
+ lo = Math.imul(al9, bl7);
2424
+ mid = Math.imul(al9, bh7);
2425
+ mid = (mid + Math.imul(ah9, bl7)) | 0;
2426
+ hi = Math.imul(ah9, bh7);
2427
+ lo = (lo + Math.imul(al8, bl8)) | 0;
2428
+ mid = (mid + Math.imul(al8, bh8)) | 0;
2429
+ mid = (mid + Math.imul(ah8, bl8)) | 0;
2430
+ hi = (hi + Math.imul(ah8, bh8)) | 0;
2431
+ lo = (lo + Math.imul(al7, bl9)) | 0;
2432
+ mid = (mid + Math.imul(al7, bh9)) | 0;
2433
+ mid = (mid + Math.imul(ah7, bl9)) | 0;
2434
+ hi = (hi + Math.imul(ah7, bh9)) | 0;
2435
+ var w16 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2436
+ c = (((hi + (mid >>> 13)) | 0) + (w16 >>> 26)) | 0;
2437
+ w16 &= 0x3ffffff;
2438
+ /* k = 17 */
2439
+ lo = Math.imul(al9, bl8);
2440
+ mid = Math.imul(al9, bh8);
2441
+ mid = (mid + Math.imul(ah9, bl8)) | 0;
2442
+ hi = Math.imul(ah9, bh8);
2443
+ lo = (lo + Math.imul(al8, bl9)) | 0;
2444
+ mid = (mid + Math.imul(al8, bh9)) | 0;
2445
+ mid = (mid + Math.imul(ah8, bl9)) | 0;
2446
+ hi = (hi + Math.imul(ah8, bh9)) | 0;
2447
+ var w17 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2448
+ c = (((hi + (mid >>> 13)) | 0) + (w17 >>> 26)) | 0;
2449
+ w17 &= 0x3ffffff;
2450
+ /* k = 18 */
2451
+ lo = Math.imul(al9, bl9);
2452
+ mid = Math.imul(al9, bh9);
2453
+ mid = (mid + Math.imul(ah9, bl9)) | 0;
2454
+ hi = Math.imul(ah9, bh9);
2455
+ var w18 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
2456
+ c = (((hi + (mid >>> 13)) | 0) + (w18 >>> 26)) | 0;
2457
+ w18 &= 0x3ffffff;
2458
+ o[0] = w0;
2459
+ o[1] = w1;
2460
+ o[2] = w2;
2461
+ o[3] = w3;
2462
+ o[4] = w4;
2463
+ o[5] = w5;
2464
+ o[6] = w6;
2465
+ o[7] = w7;
2466
+ o[8] = w8;
2467
+ o[9] = w9;
2468
+ o[10] = w10;
2469
+ o[11] = w11;
2470
+ o[12] = w12;
2471
+ o[13] = w13;
2472
+ o[14] = w14;
2473
+ o[15] = w15;
2474
+ o[16] = w16;
2475
+ o[17] = w17;
2476
+ o[18] = w18;
2477
+ if (c !== 0) {
2478
+ o[19] = c;
2479
+ out.length++;
2480
+ }
2481
+ return out;
2482
+ };
2483
+
2484
+ // Polyfill comb
2485
+ if (!Math.imul) {
2486
+ comb10MulTo = smallMulTo;
2487
+ }
2488
+
2489
+ function bigMulTo (self, num, out) {
2490
+ out.negative = num.negative ^ self.negative;
2491
+ out.length = self.length + num.length;
2492
+
2493
+ var carry = 0;
2494
+ var hncarry = 0;
2495
+ for (var k = 0; k < out.length - 1; k++) {
2496
+ // Sum all words with the same `i + j = k` and accumulate `ncarry`,
2497
+ // note that ncarry could be >= 0x3ffffff
2498
+ var ncarry = hncarry;
2499
+ hncarry = 0;
2500
+ var rword = carry & 0x3ffffff;
2501
+ var maxJ = Math.min(k, num.length - 1);
2502
+ for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) {
2503
+ var i = k - j;
2504
+ var a = self.words[i] | 0;
2505
+ var b = num.words[j] | 0;
2506
+ var r = a * b;
2507
+
2508
+ var lo = r & 0x3ffffff;
2509
+ ncarry = (ncarry + ((r / 0x4000000) | 0)) | 0;
2510
+ lo = (lo + rword) | 0;
2511
+ rword = lo & 0x3ffffff;
2512
+ ncarry = (ncarry + (lo >>> 26)) | 0;
2513
+
2514
+ hncarry += ncarry >>> 26;
2515
+ ncarry &= 0x3ffffff;
2516
+ }
2517
+ out.words[k] = rword;
2518
+ carry = ncarry;
2519
+ ncarry = hncarry;
2520
+ }
2521
+ if (carry !== 0) {
2522
+ out.words[k] = carry;
2523
+ } else {
2524
+ out.length--;
2525
+ }
2526
+
2527
+ return out._strip();
2528
+ }
2529
+
2530
+ function jumboMulTo (self, num, out) {
2531
+ // Temporary disable, see https://github.com/indutny/bn.js/issues/211
2532
+ // var fftm = new FFTM();
2533
+ // return fftm.mulp(self, num, out);
2534
+ return bigMulTo(self, num, out);
2535
+ }
2536
+
2537
+ BN.prototype.mulTo = function mulTo (num, out) {
2538
+ var res;
2539
+ var len = this.length + num.length;
2540
+ if (this.length === 10 && num.length === 10) {
2541
+ res = comb10MulTo(this, num, out);
2542
+ } else if (len < 63) {
2543
+ res = smallMulTo(this, num, out);
2544
+ } else if (len < 1024) {
2545
+ res = bigMulTo(this, num, out);
2546
+ } else {
2547
+ res = jumboMulTo(this, num, out);
2548
+ }
2549
+
2550
+ return res;
2551
+ };
2552
+
2553
+ // Multiply `this` by `num`
2554
+ BN.prototype.mul = function mul (num) {
2555
+ var out = new BN(null);
2556
+ out.words = new Array(this.length + num.length);
2557
+ return this.mulTo(num, out);
2558
+ };
2559
+
2560
+ // Multiply employing FFT
2561
+ BN.prototype.mulf = function mulf (num) {
2562
+ var out = new BN(null);
2563
+ out.words = new Array(this.length + num.length);
2564
+ return jumboMulTo(this, num, out);
2565
+ };
2566
+
2567
+ // In-place Multiplication
2568
+ BN.prototype.imul = function imul (num) {
2569
+ return this.clone().mulTo(num, this);
2570
+ };
2571
+
2572
+ BN.prototype.imuln = function imuln (num) {
2573
+ var isNegNum = num < 0;
2574
+ if (isNegNum) num = -num;
2575
+
2576
+ assert(typeof num === 'number');
2577
+ assert(num < 0x4000000);
2578
+
2579
+ // Carry
2580
+ var carry = 0;
2581
+ for (var i = 0; i < this.length; i++) {
2582
+ var w = (this.words[i] | 0) * num;
2583
+ var lo = (w & 0x3ffffff) + (carry & 0x3ffffff);
2584
+ carry >>= 26;
2585
+ carry += (w / 0x4000000) | 0;
2586
+ // NOTE: lo is 27bit maximum
2587
+ carry += lo >>> 26;
2588
+ this.words[i] = lo & 0x3ffffff;
2589
+ }
2590
+
2591
+ if (carry !== 0) {
2592
+ this.words[i] = carry;
2593
+ this.length++;
2594
+ }
2595
+
2596
+ return isNegNum ? this.ineg() : this;
2597
+ };
2598
+
2599
+ BN.prototype.muln = function muln (num) {
2600
+ return this.clone().imuln(num);
2601
+ };
2602
+
2603
+ // `this` * `this`
2604
+ BN.prototype.sqr = function sqr () {
2605
+ return this.mul(this);
2606
+ };
2607
+
2608
+ // `this` * `this` in-place
2609
+ BN.prototype.isqr = function isqr () {
2610
+ return this.imul(this.clone());
2611
+ };
2612
+
2613
+ // Math.pow(`this`, `num`)
2614
+ BN.prototype.pow = function pow (num) {
2615
+ var w = toBitArray(num);
2616
+ if (w.length === 0) return new BN(1);
2617
+
2618
+ // Skip leading zeroes
2619
+ var res = this;
2620
+ for (var i = 0; i < w.length; i++, res = res.sqr()) {
2621
+ if (w[i] !== 0) break;
2622
+ }
2623
+
2624
+ if (++i < w.length) {
2625
+ for (var q = res.sqr(); i < w.length; i++, q = q.sqr()) {
2626
+ if (w[i] === 0) continue;
2627
+
2628
+ res = res.mul(q);
2629
+ }
2630
+ }
2631
+
2632
+ return res;
2633
+ };
2634
+
2635
+ // Shift-left in-place
2636
+ BN.prototype.iushln = function iushln (bits) {
2637
+ assert(typeof bits === 'number' && bits >= 0);
2638
+ var r = bits % 26;
2639
+ var s = (bits - r) / 26;
2640
+ var carryMask = (0x3ffffff >>> (26 - r)) << (26 - r);
2641
+ var i;
2642
+
2643
+ if (r !== 0) {
2644
+ var carry = 0;
2645
+
2646
+ for (i = 0; i < this.length; i++) {
2647
+ var newCarry = this.words[i] & carryMask;
2648
+ var c = ((this.words[i] | 0) - newCarry) << r;
2649
+ this.words[i] = c | carry;
2650
+ carry = newCarry >>> (26 - r);
2651
+ }
2652
+
2653
+ if (carry) {
2654
+ this.words[i] = carry;
2655
+ this.length++;
2656
+ }
2657
+ }
2658
+
2659
+ if (s !== 0) {
2660
+ for (i = this.length - 1; i >= 0; i--) {
2661
+ this.words[i + s] = this.words[i];
2662
+ }
2663
+
2664
+ for (i = 0; i < s; i++) {
2665
+ this.words[i] = 0;
2666
+ }
2667
+
2668
+ this.length += s;
2669
+ }
2670
+
2671
+ return this._strip();
2672
+ };
2673
+
2674
+ BN.prototype.ishln = function ishln (bits) {
2675
+ // TODO(indutny): implement me
2676
+ assert(this.negative === 0);
2677
+ return this.iushln(bits);
2678
+ };
2679
+
2680
+ // Shift-right in-place
2681
+ // NOTE: `hint` is a lowest bit before trailing zeroes
2682
+ // NOTE: if `extended` is present - it will be filled with destroyed bits
2683
+ BN.prototype.iushrn = function iushrn (bits, hint, extended) {
2684
+ assert(typeof bits === 'number' && bits >= 0);
2685
+ var h;
2686
+ if (hint) {
2687
+ h = (hint - (hint % 26)) / 26;
2688
+ } else {
2689
+ h = 0;
2690
+ }
2691
+
2692
+ var r = bits % 26;
2693
+ var s = Math.min((bits - r) / 26, this.length);
2694
+ var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r);
2695
+ var maskedWords = extended;
2696
+
2697
+ h -= s;
2698
+ h = Math.max(0, h);
2699
+
2700
+ // Extended mode, copy masked part
2701
+ if (maskedWords) {
2702
+ for (var i = 0; i < s; i++) {
2703
+ maskedWords.words[i] = this.words[i];
2704
+ }
2705
+ maskedWords.length = s;
2706
+ }
2707
+
2708
+ if (s === 0) ; else if (this.length > s) {
2709
+ this.length -= s;
2710
+ for (i = 0; i < this.length; i++) {
2711
+ this.words[i] = this.words[i + s];
2712
+ }
2713
+ } else {
2714
+ this.words[0] = 0;
2715
+ this.length = 1;
2716
+ }
2717
+
2718
+ var carry = 0;
2719
+ for (i = this.length - 1; i >= 0 && (carry !== 0 || i >= h); i--) {
2720
+ var word = this.words[i] | 0;
2721
+ this.words[i] = (carry << (26 - r)) | (word >>> r);
2722
+ carry = word & mask;
2723
+ }
2724
+
2725
+ // Push carried bits as a mask
2726
+ if (maskedWords && carry !== 0) {
2727
+ maskedWords.words[maskedWords.length++] = carry;
2728
+ }
2729
+
2730
+ if (this.length === 0) {
2731
+ this.words[0] = 0;
2732
+ this.length = 1;
2733
+ }
2734
+
2735
+ return this._strip();
2736
+ };
2737
+
2738
+ BN.prototype.ishrn = function ishrn (bits, hint, extended) {
2739
+ // TODO(indutny): implement me
2740
+ assert(this.negative === 0);
2741
+ return this.iushrn(bits, hint, extended);
2742
+ };
2743
+
2744
+ // Shift-left
2745
+ BN.prototype.shln = function shln (bits) {
2746
+ return this.clone().ishln(bits);
2747
+ };
2748
+
2749
+ BN.prototype.ushln = function ushln (bits) {
2750
+ return this.clone().iushln(bits);
2751
+ };
2752
+
2753
+ // Shift-right
2754
+ BN.prototype.shrn = function shrn (bits) {
2755
+ return this.clone().ishrn(bits);
2756
+ };
2757
+
2758
+ BN.prototype.ushrn = function ushrn (bits) {
2759
+ return this.clone().iushrn(bits);
2760
+ };
2761
+
2762
+ // Test if n bit is set
2763
+ BN.prototype.testn = function testn (bit) {
2764
+ assert(typeof bit === 'number' && bit >= 0);
2765
+ var r = bit % 26;
2766
+ var s = (bit - r) / 26;
2767
+ var q = 1 << r;
2768
+
2769
+ // Fast case: bit is much higher than all existing words
2770
+ if (this.length <= s) return false;
2771
+
2772
+ // Check bit and return
2773
+ var w = this.words[s];
2774
+
2775
+ return !!(w & q);
2776
+ };
2777
+
2778
+ // Return only lowers bits of number (in-place)
2779
+ BN.prototype.imaskn = function imaskn (bits) {
2780
+ assert(typeof bits === 'number' && bits >= 0);
2781
+ var r = bits % 26;
2782
+ var s = (bits - r) / 26;
2783
+
2784
+ assert(this.negative === 0, 'imaskn works only with positive numbers');
2785
+
2786
+ if (this.length <= s) {
2787
+ return this;
2788
+ }
2789
+
2790
+ if (r !== 0) {
2791
+ s++;
2792
+ }
2793
+ this.length = Math.min(s, this.length);
2794
+
2795
+ if (r !== 0) {
2796
+ var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r);
2797
+ this.words[this.length - 1] &= mask;
2798
+ }
2799
+
2800
+ return this._strip();
2801
+ };
2802
+
2803
+ // Return only lowers bits of number
2804
+ BN.prototype.maskn = function maskn (bits) {
2805
+ return this.clone().imaskn(bits);
2806
+ };
2807
+
2808
+ // Add plain number `num` to `this`
2809
+ BN.prototype.iaddn = function iaddn (num) {
2810
+ assert(typeof num === 'number');
2811
+ assert(num < 0x4000000);
2812
+ if (num < 0) return this.isubn(-num);
2813
+
2814
+ // Possible sign change
2815
+ if (this.negative !== 0) {
2816
+ if (this.length === 1 && (this.words[0] | 0) <= num) {
2817
+ this.words[0] = num - (this.words[0] | 0);
2818
+ this.negative = 0;
2819
+ return this;
2820
+ }
2821
+
2822
+ this.negative = 0;
2823
+ this.isubn(num);
2824
+ this.negative = 1;
2825
+ return this;
2826
+ }
2827
+
2828
+ // Add without checks
2829
+ return this._iaddn(num);
2830
+ };
2831
+
2832
+ BN.prototype._iaddn = function _iaddn (num) {
2833
+ this.words[0] += num;
2834
+
2835
+ // Carry
2836
+ for (var i = 0; i < this.length && this.words[i] >= 0x4000000; i++) {
2837
+ this.words[i] -= 0x4000000;
2838
+ if (i === this.length - 1) {
2839
+ this.words[i + 1] = 1;
2840
+ } else {
2841
+ this.words[i + 1]++;
2842
+ }
2843
+ }
2844
+ this.length = Math.max(this.length, i + 1);
2845
+
2846
+ return this;
2847
+ };
2848
+
2849
+ // Subtract plain number `num` from `this`
2850
+ BN.prototype.isubn = function isubn (num) {
2851
+ assert(typeof num === 'number');
2852
+ assert(num < 0x4000000);
2853
+ if (num < 0) return this.iaddn(-num);
2854
+
2855
+ if (this.negative !== 0) {
2856
+ this.negative = 0;
2857
+ this.iaddn(num);
2858
+ this.negative = 1;
2859
+ return this;
2860
+ }
2861
+
2862
+ this.words[0] -= num;
2863
+
2864
+ if (this.length === 1 && this.words[0] < 0) {
2865
+ this.words[0] = -this.words[0];
2866
+ this.negative = 1;
2867
+ } else {
2868
+ // Carry
2869
+ for (var i = 0; i < this.length && this.words[i] < 0; i++) {
2870
+ this.words[i] += 0x4000000;
2871
+ this.words[i + 1] -= 1;
2872
+ }
2873
+ }
2874
+
2875
+ return this._strip();
2876
+ };
2877
+
2878
+ BN.prototype.addn = function addn (num) {
2879
+ return this.clone().iaddn(num);
2880
+ };
2881
+
2882
+ BN.prototype.subn = function subn (num) {
2883
+ return this.clone().isubn(num);
2884
+ };
2885
+
2886
+ BN.prototype.iabs = function iabs () {
2887
+ this.negative = 0;
2888
+
2889
+ return this;
2890
+ };
2891
+
2892
+ BN.prototype.abs = function abs () {
2893
+ return this.clone().iabs();
2894
+ };
2895
+
2896
+ BN.prototype._ishlnsubmul = function _ishlnsubmul (num, mul, shift) {
2897
+ var len = num.length + shift;
2898
+ var i;
2899
+
2900
+ this._expand(len);
2901
+
2902
+ var w;
2903
+ var carry = 0;
2904
+ for (i = 0; i < num.length; i++) {
2905
+ w = (this.words[i + shift] | 0) + carry;
2906
+ var right = (num.words[i] | 0) * mul;
2907
+ w -= right & 0x3ffffff;
2908
+ carry = (w >> 26) - ((right / 0x4000000) | 0);
2909
+ this.words[i + shift] = w & 0x3ffffff;
2910
+ }
2911
+ for (; i < this.length - shift; i++) {
2912
+ w = (this.words[i + shift] | 0) + carry;
2913
+ carry = w >> 26;
2914
+ this.words[i + shift] = w & 0x3ffffff;
2915
+ }
2916
+
2917
+ if (carry === 0) return this._strip();
2918
+
2919
+ // Subtraction overflow
2920
+ assert(carry === -1);
2921
+ carry = 0;
2922
+ for (i = 0; i < this.length; i++) {
2923
+ w = -(this.words[i] | 0) + carry;
2924
+ carry = w >> 26;
2925
+ this.words[i] = w & 0x3ffffff;
2926
+ }
2927
+ this.negative = 1;
2928
+
2929
+ return this._strip();
2930
+ };
2931
+
2932
+ BN.prototype._wordDiv = function _wordDiv (num, mode) {
2933
+ var shift = this.length - num.length;
2934
+
2935
+ var a = this.clone();
2936
+ var b = num;
2937
+
2938
+ // Normalize
2939
+ var bhi = b.words[b.length - 1] | 0;
2940
+ var bhiBits = this._countBits(bhi);
2941
+ shift = 26 - bhiBits;
2942
+ if (shift !== 0) {
2943
+ b = b.ushln(shift);
2944
+ a.iushln(shift);
2945
+ bhi = b.words[b.length - 1] | 0;
2946
+ }
2947
+
2948
+ // Initialize quotient
2949
+ var m = a.length - b.length;
2950
+ var q;
2951
+
2952
+ if (mode !== 'mod') {
2953
+ q = new BN(null);
2954
+ q.length = m + 1;
2955
+ q.words = new Array(q.length);
2956
+ for (var i = 0; i < q.length; i++) {
2957
+ q.words[i] = 0;
2958
+ }
2959
+ }
2960
+
2961
+ var diff = a.clone()._ishlnsubmul(b, 1, m);
2962
+ if (diff.negative === 0) {
2963
+ a = diff;
2964
+ if (q) {
2965
+ q.words[m] = 1;
2966
+ }
2967
+ }
2968
+
2969
+ for (var j = m - 1; j >= 0; j--) {
2970
+ var qj = (a.words[b.length + j] | 0) * 0x4000000 +
2971
+ (a.words[b.length + j - 1] | 0);
2972
+
2973
+ // NOTE: (qj / bhi) is (0x3ffffff * 0x4000000 + 0x3ffffff) / 0x2000000 max
2974
+ // (0x7ffffff)
2975
+ qj = Math.min((qj / bhi) | 0, 0x3ffffff);
2976
+
2977
+ a._ishlnsubmul(b, qj, j);
2978
+ while (a.negative !== 0) {
2979
+ qj--;
2980
+ a.negative = 0;
2981
+ a._ishlnsubmul(b, 1, j);
2982
+ if (!a.isZero()) {
2983
+ a.negative ^= 1;
2984
+ }
2985
+ }
2986
+ if (q) {
2987
+ q.words[j] = qj;
2988
+ }
2989
+ }
2990
+ if (q) {
2991
+ q._strip();
2992
+ }
2993
+ a._strip();
2994
+
2995
+ // Denormalize
2996
+ if (mode !== 'div' && shift !== 0) {
2997
+ a.iushrn(shift);
2998
+ }
2999
+
3000
+ return {
3001
+ div: q || null,
3002
+ mod: a
3003
+ };
3004
+ };
3005
+
3006
+ // NOTE: 1) `mode` can be set to `mod` to request mod only,
3007
+ // to `div` to request div only, or be absent to
3008
+ // request both div & mod
3009
+ // 2) `positive` is true if unsigned mod is requested
3010
+ BN.prototype.divmod = function divmod (num, mode, positive) {
3011
+ assert(!num.isZero());
3012
+
3013
+ if (this.isZero()) {
3014
+ return {
3015
+ div: new BN(0),
3016
+ mod: new BN(0)
3017
+ };
3018
+ }
3019
+
3020
+ var div, mod, res;
3021
+ if (this.negative !== 0 && num.negative === 0) {
3022
+ res = this.neg().divmod(num, mode);
3023
+
3024
+ if (mode !== 'mod') {
3025
+ div = res.div.neg();
3026
+ }
3027
+
3028
+ if (mode !== 'div') {
3029
+ mod = res.mod.neg();
3030
+ if (positive && mod.negative !== 0) {
3031
+ mod.iadd(num);
3032
+ }
3033
+ }
3034
+
3035
+ return {
3036
+ div: div,
3037
+ mod: mod
3038
+ };
3039
+ }
3040
+
3041
+ if (this.negative === 0 && num.negative !== 0) {
3042
+ res = this.divmod(num.neg(), mode);
3043
+
3044
+ if (mode !== 'mod') {
3045
+ div = res.div.neg();
3046
+ }
3047
+
3048
+ return {
3049
+ div: div,
3050
+ mod: res.mod
3051
+ };
3052
+ }
3053
+
3054
+ if ((this.negative & num.negative) !== 0) {
3055
+ res = this.neg().divmod(num.neg(), mode);
3056
+
3057
+ if (mode !== 'div') {
3058
+ mod = res.mod.neg();
3059
+ if (positive && mod.negative !== 0) {
3060
+ mod.isub(num);
3061
+ }
3062
+ }
3063
+
3064
+ return {
3065
+ div: res.div,
3066
+ mod: mod
3067
+ };
3068
+ }
3069
+
3070
+ // Both numbers are positive at this point
3071
+
3072
+ // Strip both numbers to approximate shift value
3073
+ if (num.length > this.length || this.cmp(num) < 0) {
3074
+ return {
3075
+ div: new BN(0),
3076
+ mod: this
3077
+ };
3078
+ }
3079
+
3080
+ // Very short reduction
3081
+ if (num.length === 1) {
3082
+ if (mode === 'div') {
3083
+ return {
3084
+ div: this.divn(num.words[0]),
3085
+ mod: null
3086
+ };
3087
+ }
3088
+
3089
+ if (mode === 'mod') {
3090
+ return {
3091
+ div: null,
3092
+ mod: new BN(this.modrn(num.words[0]))
3093
+ };
3094
+ }
3095
+
3096
+ return {
3097
+ div: this.divn(num.words[0]),
3098
+ mod: new BN(this.modrn(num.words[0]))
3099
+ };
3100
+ }
3101
+
3102
+ return this._wordDiv(num, mode);
3103
+ };
3104
+
3105
+ // Find `this` / `num`
3106
+ BN.prototype.div = function div (num) {
3107
+ return this.divmod(num, 'div', false).div;
3108
+ };
3109
+
3110
+ // Find `this` % `num`
3111
+ BN.prototype.mod = function mod (num) {
3112
+ return this.divmod(num, 'mod', false).mod;
3113
+ };
3114
+
3115
+ BN.prototype.umod = function umod (num) {
3116
+ return this.divmod(num, 'mod', true).mod;
3117
+ };
3118
+
3119
+ // Find Round(`this` / `num`)
3120
+ BN.prototype.divRound = function divRound (num) {
3121
+ var dm = this.divmod(num);
3122
+
3123
+ // Fast case - exact division
3124
+ if (dm.mod.isZero()) return dm.div;
3125
+
3126
+ var mod = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod;
3127
+
3128
+ var half = num.ushrn(1);
3129
+ var r2 = num.andln(1);
3130
+ var cmp = mod.cmp(half);
3131
+
3132
+ // Round down
3133
+ if (cmp < 0 || (r2 === 1 && cmp === 0)) return dm.div;
3134
+
3135
+ // Round up
3136
+ return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1);
3137
+ };
3138
+
3139
+ BN.prototype.modrn = function modrn (num) {
3140
+ var isNegNum = num < 0;
3141
+ if (isNegNum) num = -num;
3142
+
3143
+ assert(num <= 0x3ffffff);
3144
+ var p = (1 << 26) % num;
3145
+
3146
+ var acc = 0;
3147
+ for (var i = this.length - 1; i >= 0; i--) {
3148
+ acc = (p * acc + (this.words[i] | 0)) % num;
3149
+ }
3150
+
3151
+ return isNegNum ? -acc : acc;
3152
+ };
3153
+
3154
+ // WARNING: DEPRECATED
3155
+ BN.prototype.modn = function modn (num) {
3156
+ return this.modrn(num);
3157
+ };
3158
+
3159
+ // In-place division by number
3160
+ BN.prototype.idivn = function idivn (num) {
3161
+ var isNegNum = num < 0;
3162
+ if (isNegNum) num = -num;
3163
+
3164
+ assert(num <= 0x3ffffff);
3165
+
3166
+ var carry = 0;
3167
+ for (var i = this.length - 1; i >= 0; i--) {
3168
+ var w = (this.words[i] | 0) + carry * 0x4000000;
3169
+ this.words[i] = (w / num) | 0;
3170
+ carry = w % num;
3171
+ }
3172
+
3173
+ this._strip();
3174
+ return isNegNum ? this.ineg() : this;
3175
+ };
3176
+
3177
+ BN.prototype.divn = function divn (num) {
3178
+ return this.clone().idivn(num);
3179
+ };
3180
+
3181
+ BN.prototype.egcd = function egcd (p) {
3182
+ assert(p.negative === 0);
3183
+ assert(!p.isZero());
3184
+
3185
+ var x = this;
3186
+ var y = p.clone();
3187
+
3188
+ if (x.negative !== 0) {
3189
+ x = x.umod(p);
3190
+ } else {
3191
+ x = x.clone();
3192
+ }
3193
+
3194
+ // A * x + B * y = x
3195
+ var A = new BN(1);
3196
+ var B = new BN(0);
3197
+
3198
+ // C * x + D * y = y
3199
+ var C = new BN(0);
3200
+ var D = new BN(1);
3201
+
3202
+ var g = 0;
3203
+
3204
+ while (x.isEven() && y.isEven()) {
3205
+ x.iushrn(1);
3206
+ y.iushrn(1);
3207
+ ++g;
3208
+ }
3209
+
3210
+ var yp = y.clone();
3211
+ var xp = x.clone();
3212
+
3213
+ while (!x.isZero()) {
3214
+ for (var i = 0, im = 1; (x.words[0] & im) === 0 && i < 26; ++i, im <<= 1);
3215
+ if (i > 0) {
3216
+ x.iushrn(i);
3217
+ while (i-- > 0) {
3218
+ if (A.isOdd() || B.isOdd()) {
3219
+ A.iadd(yp);
3220
+ B.isub(xp);
3221
+ }
3222
+
3223
+ A.iushrn(1);
3224
+ B.iushrn(1);
3225
+ }
3226
+ }
3227
+
3228
+ for (var j = 0, jm = 1; (y.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1);
3229
+ if (j > 0) {
3230
+ y.iushrn(j);
3231
+ while (j-- > 0) {
3232
+ if (C.isOdd() || D.isOdd()) {
3233
+ C.iadd(yp);
3234
+ D.isub(xp);
3235
+ }
3236
+
3237
+ C.iushrn(1);
3238
+ D.iushrn(1);
3239
+ }
3240
+ }
3241
+
3242
+ if (x.cmp(y) >= 0) {
3243
+ x.isub(y);
3244
+ A.isub(C);
3245
+ B.isub(D);
3246
+ } else {
3247
+ y.isub(x);
3248
+ C.isub(A);
3249
+ D.isub(B);
3250
+ }
3251
+ }
3252
+
3253
+ return {
3254
+ a: C,
3255
+ b: D,
3256
+ gcd: y.iushln(g)
3257
+ };
3258
+ };
3259
+
3260
+ // This is reduced incarnation of the binary EEA
3261
+ // above, designated to invert members of the
3262
+ // _prime_ fields F(p) at a maximal speed
3263
+ BN.prototype._invmp = function _invmp (p) {
3264
+ assert(p.negative === 0);
3265
+ assert(!p.isZero());
3266
+
3267
+ var a = this;
3268
+ var b = p.clone();
3269
+
3270
+ if (a.negative !== 0) {
3271
+ a = a.umod(p);
3272
+ } else {
3273
+ a = a.clone();
3274
+ }
3275
+
3276
+ var x1 = new BN(1);
3277
+ var x2 = new BN(0);
3278
+
3279
+ var delta = b.clone();
3280
+
3281
+ while (a.cmpn(1) > 0 && b.cmpn(1) > 0) {
3282
+ for (var i = 0, im = 1; (a.words[0] & im) === 0 && i < 26; ++i, im <<= 1);
3283
+ if (i > 0) {
3284
+ a.iushrn(i);
3285
+ while (i-- > 0) {
3286
+ if (x1.isOdd()) {
3287
+ x1.iadd(delta);
3288
+ }
3289
+
3290
+ x1.iushrn(1);
3291
+ }
3292
+ }
3293
+
3294
+ for (var j = 0, jm = 1; (b.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1);
3295
+ if (j > 0) {
3296
+ b.iushrn(j);
3297
+ while (j-- > 0) {
3298
+ if (x2.isOdd()) {
3299
+ x2.iadd(delta);
3300
+ }
3301
+
3302
+ x2.iushrn(1);
3303
+ }
3304
+ }
3305
+
3306
+ if (a.cmp(b) >= 0) {
3307
+ a.isub(b);
3308
+ x1.isub(x2);
3309
+ } else {
3310
+ b.isub(a);
3311
+ x2.isub(x1);
3312
+ }
3313
+ }
3314
+
3315
+ var res;
3316
+ if (a.cmpn(1) === 0) {
3317
+ res = x1;
3318
+ } else {
3319
+ res = x2;
3320
+ }
3321
+
3322
+ if (res.cmpn(0) < 0) {
3323
+ res.iadd(p);
3324
+ }
3325
+
3326
+ return res;
3327
+ };
3328
+
3329
+ BN.prototype.gcd = function gcd (num) {
3330
+ if (this.isZero()) return num.abs();
3331
+ if (num.isZero()) return this.abs();
3332
+
3333
+ var a = this.clone();
3334
+ var b = num.clone();
3335
+ a.negative = 0;
3336
+ b.negative = 0;
3337
+
3338
+ // Remove common factor of two
3339
+ for (var shift = 0; a.isEven() && b.isEven(); shift++) {
3340
+ a.iushrn(1);
3341
+ b.iushrn(1);
3342
+ }
3343
+
3344
+ do {
3345
+ while (a.isEven()) {
3346
+ a.iushrn(1);
3347
+ }
3348
+ while (b.isEven()) {
3349
+ b.iushrn(1);
3350
+ }
3351
+
3352
+ var r = a.cmp(b);
3353
+ if (r < 0) {
3354
+ // Swap `a` and `b` to make `a` always bigger than `b`
3355
+ var t = a;
3356
+ a = b;
3357
+ b = t;
3358
+ } else if (r === 0 || b.cmpn(1) === 0) {
3359
+ break;
3360
+ }
3361
+
3362
+ a.isub(b);
3363
+ } while (true);
3364
+
3365
+ return b.iushln(shift);
3366
+ };
3367
+
3368
+ // Invert number in the field F(num)
3369
+ BN.prototype.invm = function invm (num) {
3370
+ return this.egcd(num).a.umod(num);
3371
+ };
3372
+
3373
+ BN.prototype.isEven = function isEven () {
3374
+ return (this.words[0] & 1) === 0;
3375
+ };
3376
+
3377
+ BN.prototype.isOdd = function isOdd () {
3378
+ return (this.words[0] & 1) === 1;
3379
+ };
3380
+
3381
+ // And first word and num
3382
+ BN.prototype.andln = function andln (num) {
3383
+ return this.words[0] & num;
3384
+ };
3385
+
3386
+ // Increment at the bit position in-line
3387
+ BN.prototype.bincn = function bincn (bit) {
3388
+ assert(typeof bit === 'number');
3389
+ var r = bit % 26;
3390
+ var s = (bit - r) / 26;
3391
+ var q = 1 << r;
3392
+
3393
+ // Fast case: bit is much higher than all existing words
3394
+ if (this.length <= s) {
3395
+ this._expand(s + 1);
3396
+ this.words[s] |= q;
3397
+ return this;
3398
+ }
3399
+
3400
+ // Add bit and propagate, if needed
3401
+ var carry = q;
3402
+ for (var i = s; carry !== 0 && i < this.length; i++) {
3403
+ var w = this.words[i] | 0;
3404
+ w += carry;
3405
+ carry = w >>> 26;
3406
+ w &= 0x3ffffff;
3407
+ this.words[i] = w;
3408
+ }
3409
+ if (carry !== 0) {
3410
+ this.words[i] = carry;
3411
+ this.length++;
3412
+ }
3413
+ return this;
3414
+ };
3415
+
3416
+ BN.prototype.isZero = function isZero () {
3417
+ return this.length === 1 && this.words[0] === 0;
3418
+ };
3419
+
3420
+ BN.prototype.cmpn = function cmpn (num) {
3421
+ var negative = num < 0;
3422
+
3423
+ if (this.negative !== 0 && !negative) return -1;
3424
+ if (this.negative === 0 && negative) return 1;
3425
+
3426
+ this._strip();
3427
+
3428
+ var res;
3429
+ if (this.length > 1) {
3430
+ res = 1;
3431
+ } else {
3432
+ if (negative) {
3433
+ num = -num;
3434
+ }
3435
+
3436
+ assert(num <= 0x3ffffff, 'Number is too big');
3437
+
3438
+ var w = this.words[0] | 0;
3439
+ res = w === num ? 0 : w < num ? -1 : 1;
3440
+ }
3441
+ if (this.negative !== 0) return -res | 0;
3442
+ return res;
3443
+ };
3444
+
3445
+ // Compare two numbers and return:
3446
+ // 1 - if `this` > `num`
3447
+ // 0 - if `this` == `num`
3448
+ // -1 - if `this` < `num`
3449
+ BN.prototype.cmp = function cmp (num) {
3450
+ if (this.negative !== 0 && num.negative === 0) return -1;
3451
+ if (this.negative === 0 && num.negative !== 0) return 1;
3452
+
3453
+ var res = this.ucmp(num);
3454
+ if (this.negative !== 0) return -res | 0;
3455
+ return res;
3456
+ };
3457
+
3458
+ // Unsigned comparison
3459
+ BN.prototype.ucmp = function ucmp (num) {
3460
+ // At this point both numbers have the same sign
3461
+ if (this.length > num.length) return 1;
3462
+ if (this.length < num.length) return -1;
3463
+
3464
+ var res = 0;
3465
+ for (var i = this.length - 1; i >= 0; i--) {
3466
+ var a = this.words[i] | 0;
3467
+ var b = num.words[i] | 0;
3468
+
3469
+ if (a === b) continue;
3470
+ if (a < b) {
3471
+ res = -1;
3472
+ } else if (a > b) {
3473
+ res = 1;
3474
+ }
3475
+ break;
3476
+ }
3477
+ return res;
3478
+ };
3479
+
3480
+ BN.prototype.gtn = function gtn (num) {
3481
+ return this.cmpn(num) === 1;
3482
+ };
3483
+
3484
+ BN.prototype.gt = function gt (num) {
3485
+ return this.cmp(num) === 1;
3486
+ };
3487
+
3488
+ BN.prototype.gten = function gten (num) {
3489
+ return this.cmpn(num) >= 0;
3490
+ };
3491
+
3492
+ BN.prototype.gte = function gte (num) {
3493
+ return this.cmp(num) >= 0;
3494
+ };
3495
+
3496
+ BN.prototype.ltn = function ltn (num) {
3497
+ return this.cmpn(num) === -1;
3498
+ };
3499
+
3500
+ BN.prototype.lt = function lt (num) {
3501
+ return this.cmp(num) === -1;
3502
+ };
3503
+
3504
+ BN.prototype.lten = function lten (num) {
3505
+ return this.cmpn(num) <= 0;
3506
+ };
3507
+
3508
+ BN.prototype.lte = function lte (num) {
3509
+ return this.cmp(num) <= 0;
3510
+ };
3511
+
3512
+ BN.prototype.eqn = function eqn (num) {
3513
+ return this.cmpn(num) === 0;
3514
+ };
3515
+
3516
+ BN.prototype.eq = function eq (num) {
3517
+ return this.cmp(num) === 0;
3518
+ };
3519
+
3520
+ //
3521
+ // A reduce context, could be using montgomery or something better, depending
3522
+ // on the `m` itself.
3523
+ //
3524
+ BN.red = function red (num) {
3525
+ return new Red(num);
3526
+ };
3527
+
3528
+ BN.prototype.toRed = function toRed (ctx) {
3529
+ assert(!this.red, 'Already a number in reduction context');
3530
+ assert(this.negative === 0, 'red works only with positives');
3531
+ return ctx.convertTo(this)._forceRed(ctx);
3532
+ };
3533
+
3534
+ BN.prototype.fromRed = function fromRed () {
3535
+ assert(this.red, 'fromRed works only with numbers in reduction context');
3536
+ return this.red.convertFrom(this);
3537
+ };
3538
+
3539
+ BN.prototype._forceRed = function _forceRed (ctx) {
3540
+ this.red = ctx;
3541
+ return this;
3542
+ };
3543
+
3544
+ BN.prototype.forceRed = function forceRed (ctx) {
3545
+ assert(!this.red, 'Already a number in reduction context');
3546
+ return this._forceRed(ctx);
3547
+ };
3548
+
3549
+ BN.prototype.redAdd = function redAdd (num) {
3550
+ assert(this.red, 'redAdd works only with red numbers');
3551
+ return this.red.add(this, num);
3552
+ };
3553
+
3554
+ BN.prototype.redIAdd = function redIAdd (num) {
3555
+ assert(this.red, 'redIAdd works only with red numbers');
3556
+ return this.red.iadd(this, num);
3557
+ };
3558
+
3559
+ BN.prototype.redSub = function redSub (num) {
3560
+ assert(this.red, 'redSub works only with red numbers');
3561
+ return this.red.sub(this, num);
3562
+ };
3563
+
3564
+ BN.prototype.redISub = function redISub (num) {
3565
+ assert(this.red, 'redISub works only with red numbers');
3566
+ return this.red.isub(this, num);
3567
+ };
3568
+
3569
+ BN.prototype.redShl = function redShl (num) {
3570
+ assert(this.red, 'redShl works only with red numbers');
3571
+ return this.red.shl(this, num);
3572
+ };
3573
+
3574
+ BN.prototype.redMul = function redMul (num) {
3575
+ assert(this.red, 'redMul works only with red numbers');
3576
+ this.red._verify2(this, num);
3577
+ return this.red.mul(this, num);
3578
+ };
3579
+
3580
+ BN.prototype.redIMul = function redIMul (num) {
3581
+ assert(this.red, 'redMul works only with red numbers');
3582
+ this.red._verify2(this, num);
3583
+ return this.red.imul(this, num);
3584
+ };
3585
+
3586
+ BN.prototype.redSqr = function redSqr () {
3587
+ assert(this.red, 'redSqr works only with red numbers');
3588
+ this.red._verify1(this);
3589
+ return this.red.sqr(this);
3590
+ };
3591
+
3592
+ BN.prototype.redISqr = function redISqr () {
3593
+ assert(this.red, 'redISqr works only with red numbers');
3594
+ this.red._verify1(this);
3595
+ return this.red.isqr(this);
3596
+ };
3597
+
3598
+ // Square root over p
3599
+ BN.prototype.redSqrt = function redSqrt () {
3600
+ assert(this.red, 'redSqrt works only with red numbers');
3601
+ this.red._verify1(this);
3602
+ return this.red.sqrt(this);
3603
+ };
3604
+
3605
+ BN.prototype.redInvm = function redInvm () {
3606
+ assert(this.red, 'redInvm works only with red numbers');
3607
+ this.red._verify1(this);
3608
+ return this.red.invm(this);
3609
+ };
3610
+
3611
+ // Return negative clone of `this` % `red modulo`
3612
+ BN.prototype.redNeg = function redNeg () {
3613
+ assert(this.red, 'redNeg works only with red numbers');
3614
+ this.red._verify1(this);
3615
+ return this.red.neg(this);
3616
+ };
3617
+
3618
+ BN.prototype.redPow = function redPow (num) {
3619
+ assert(this.red && !num.red, 'redPow(normalNum)');
3620
+ this.red._verify1(this);
3621
+ return this.red.pow(this, num);
3622
+ };
3623
+
3624
+ // Prime numbers with efficient reduction
3625
+ var primes = {
3626
+ k256: null,
3627
+ p224: null,
3628
+ p192: null,
3629
+ p25519: null
3630
+ };
3631
+
3632
+ // Pseudo-Mersenne prime
3633
+ function MPrime (name, p) {
3634
+ // P = 2 ^ N - K
3635
+ this.name = name;
3636
+ this.p = new BN(p, 16);
3637
+ this.n = this.p.bitLength();
3638
+ this.k = new BN(1).iushln(this.n).isub(this.p);
3639
+
3640
+ this.tmp = this._tmp();
3641
+ }
3642
+
3643
+ MPrime.prototype._tmp = function _tmp () {
3644
+ var tmp = new BN(null);
3645
+ tmp.words = new Array(Math.ceil(this.n / 13));
3646
+ return tmp;
3647
+ };
3648
+
3649
+ MPrime.prototype.ireduce = function ireduce (num) {
3650
+ // Assumes that `num` is less than `P^2`
3651
+ // num = HI * (2 ^ N - K) + HI * K + LO = HI * K + LO (mod P)
3652
+ var r = num;
3653
+ var rlen;
3654
+
3655
+ do {
3656
+ this.split(r, this.tmp);
3657
+ r = this.imulK(r);
3658
+ r = r.iadd(this.tmp);
3659
+ rlen = r.bitLength();
3660
+ } while (rlen > this.n);
3661
+
3662
+ var cmp = rlen < this.n ? -1 : r.ucmp(this.p);
3663
+ if (cmp === 0) {
3664
+ r.words[0] = 0;
3665
+ r.length = 1;
3666
+ } else if (cmp > 0) {
3667
+ r.isub(this.p);
3668
+ } else {
3669
+ if (r.strip !== undefined) {
3670
+ // r is a BN v4 instance
3671
+ r.strip();
3672
+ } else {
3673
+ // r is a BN v5 instance
3674
+ r._strip();
3675
+ }
3676
+ }
3677
+
3678
+ return r;
3679
+ };
3680
+
3681
+ MPrime.prototype.split = function split (input, out) {
3682
+ input.iushrn(this.n, 0, out);
3683
+ };
3684
+
3685
+ MPrime.prototype.imulK = function imulK (num) {
3686
+ return num.imul(this.k);
3687
+ };
3688
+
3689
+ function K256 () {
3690
+ MPrime.call(
3691
+ this,
3692
+ 'k256',
3693
+ 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f');
3694
+ }
3695
+ inherits(K256, MPrime);
3696
+
3697
+ K256.prototype.split = function split (input, output) {
3698
+ // 256 = 9 * 26 + 22
3699
+ var mask = 0x3fffff;
3700
+
3701
+ var outLen = Math.min(input.length, 9);
3702
+ for (var i = 0; i < outLen; i++) {
3703
+ output.words[i] = input.words[i];
3704
+ }
3705
+ output.length = outLen;
3706
+
3707
+ if (input.length <= 9) {
3708
+ input.words[0] = 0;
3709
+ input.length = 1;
3710
+ return;
3711
+ }
3712
+
3713
+ // Shift by 9 limbs
3714
+ var prev = input.words[9];
3715
+ output.words[output.length++] = prev & mask;
3716
+
3717
+ for (i = 10; i < input.length; i++) {
3718
+ var next = input.words[i] | 0;
3719
+ input.words[i - 10] = ((next & mask) << 4) | (prev >>> 22);
3720
+ prev = next;
3721
+ }
3722
+ prev >>>= 22;
3723
+ input.words[i - 10] = prev;
3724
+ if (prev === 0 && input.length > 10) {
3725
+ input.length -= 10;
3726
+ } else {
3727
+ input.length -= 9;
3728
+ }
3729
+ };
3730
+
3731
+ K256.prototype.imulK = function imulK (num) {
3732
+ // K = 0x1000003d1 = [ 0x40, 0x3d1 ]
3733
+ num.words[num.length] = 0;
3734
+ num.words[num.length + 1] = 0;
3735
+ num.length += 2;
3736
+
3737
+ // bounded at: 0x40 * 0x3ffffff + 0x3d0 = 0x100000390
3738
+ var lo = 0;
3739
+ for (var i = 0; i < num.length; i++) {
3740
+ var w = num.words[i] | 0;
3741
+ lo += w * 0x3d1;
3742
+ num.words[i] = lo & 0x3ffffff;
3743
+ lo = w * 0x40 + ((lo / 0x4000000) | 0);
3744
+ }
3745
+
3746
+ // Fast length reduction
3747
+ if (num.words[num.length - 1] === 0) {
3748
+ num.length--;
3749
+ if (num.words[num.length - 1] === 0) {
3750
+ num.length--;
3751
+ }
3752
+ }
3753
+ return num;
3754
+ };
3755
+
3756
+ function P224 () {
3757
+ MPrime.call(
3758
+ this,
3759
+ 'p224',
3760
+ 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001');
3761
+ }
3762
+ inherits(P224, MPrime);
3763
+
3764
+ function P192 () {
3765
+ MPrime.call(
3766
+ this,
3767
+ 'p192',
3768
+ 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff');
3769
+ }
3770
+ inherits(P192, MPrime);
3771
+
3772
+ function P25519 () {
3773
+ // 2 ^ 255 - 19
3774
+ MPrime.call(
3775
+ this,
3776
+ '25519',
3777
+ '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed');
3778
+ }
3779
+ inherits(P25519, MPrime);
3780
+
3781
+ P25519.prototype.imulK = function imulK (num) {
3782
+ // K = 0x13
3783
+ var carry = 0;
3784
+ for (var i = 0; i < num.length; i++) {
3785
+ var hi = (num.words[i] | 0) * 0x13 + carry;
3786
+ var lo = hi & 0x3ffffff;
3787
+ hi >>>= 26;
3788
+
3789
+ num.words[i] = lo;
3790
+ carry = hi;
3791
+ }
3792
+ if (carry !== 0) {
3793
+ num.words[num.length++] = carry;
3794
+ }
3795
+ return num;
3796
+ };
3797
+
3798
+ // Exported mostly for testing purposes, use plain name instead
3799
+ BN._prime = function prime (name) {
3800
+ // Cached version of prime
3801
+ if (primes[name]) return primes[name];
3802
+
3803
+ var prime;
3804
+ if (name === 'k256') {
3805
+ prime = new K256();
3806
+ } else if (name === 'p224') {
3807
+ prime = new P224();
3808
+ } else if (name === 'p192') {
3809
+ prime = new P192();
3810
+ } else if (name === 'p25519') {
3811
+ prime = new P25519();
3812
+ } else {
3813
+ throw new Error('Unknown prime ' + name);
3814
+ }
3815
+ primes[name] = prime;
3816
+
3817
+ return prime;
3818
+ };
3819
+
3820
+ //
3821
+ // Base reduction engine
3822
+ //
3823
+ function Red (m) {
3824
+ if (typeof m === 'string') {
3825
+ var prime = BN._prime(m);
3826
+ this.m = prime.p;
3827
+ this.prime = prime;
3828
+ } else {
3829
+ assert(m.gtn(1), 'modulus must be greater than 1');
3830
+ this.m = m;
3831
+ this.prime = null;
3832
+ }
3833
+ }
3834
+
3835
+ Red.prototype._verify1 = function _verify1 (a) {
3836
+ assert(a.negative === 0, 'red works only with positives');
3837
+ assert(a.red, 'red works only with red numbers');
3838
+ };
3839
+
3840
+ Red.prototype._verify2 = function _verify2 (a, b) {
3841
+ assert((a.negative | b.negative) === 0, 'red works only with positives');
3842
+ assert(a.red && a.red === b.red,
3843
+ 'red works only with red numbers');
3844
+ };
3845
+
3846
+ Red.prototype.imod = function imod (a) {
3847
+ if (this.prime) return this.prime.ireduce(a)._forceRed(this);
3848
+
3849
+ move(a, a.umod(this.m)._forceRed(this));
3850
+ return a;
3851
+ };
3852
+
3853
+ Red.prototype.neg = function neg (a) {
3854
+ if (a.isZero()) {
3855
+ return a.clone();
3856
+ }
3857
+
3858
+ return this.m.sub(a)._forceRed(this);
3859
+ };
3860
+
3861
+ Red.prototype.add = function add (a, b) {
3862
+ this._verify2(a, b);
3863
+
3864
+ var res = a.add(b);
3865
+ if (res.cmp(this.m) >= 0) {
3866
+ res.isub(this.m);
3867
+ }
3868
+ return res._forceRed(this);
3869
+ };
3870
+
3871
+ Red.prototype.iadd = function iadd (a, b) {
3872
+ this._verify2(a, b);
3873
+
3874
+ var res = a.iadd(b);
3875
+ if (res.cmp(this.m) >= 0) {
3876
+ res.isub(this.m);
3877
+ }
3878
+ return res;
3879
+ };
3880
+
3881
+ Red.prototype.sub = function sub (a, b) {
3882
+ this._verify2(a, b);
3883
+
3884
+ var res = a.sub(b);
3885
+ if (res.cmpn(0) < 0) {
3886
+ res.iadd(this.m);
3887
+ }
3888
+ return res._forceRed(this);
3889
+ };
3890
+
3891
+ Red.prototype.isub = function isub (a, b) {
3892
+ this._verify2(a, b);
3893
+
3894
+ var res = a.isub(b);
3895
+ if (res.cmpn(0) < 0) {
3896
+ res.iadd(this.m);
3897
+ }
3898
+ return res;
3899
+ };
3900
+
3901
+ Red.prototype.shl = function shl (a, num) {
3902
+ this._verify1(a);
3903
+ return this.imod(a.ushln(num));
3904
+ };
3905
+
3906
+ Red.prototype.imul = function imul (a, b) {
3907
+ this._verify2(a, b);
3908
+ return this.imod(a.imul(b));
3909
+ };
3910
+
3911
+ Red.prototype.mul = function mul (a, b) {
3912
+ this._verify2(a, b);
3913
+ return this.imod(a.mul(b));
3914
+ };
3915
+
3916
+ Red.prototype.isqr = function isqr (a) {
3917
+ return this.imul(a, a.clone());
3918
+ };
3919
+
3920
+ Red.prototype.sqr = function sqr (a) {
3921
+ return this.mul(a, a);
3922
+ };
3923
+
3924
+ Red.prototype.sqrt = function sqrt (a) {
3925
+ if (a.isZero()) return a.clone();
3926
+
3927
+ var mod3 = this.m.andln(3);
3928
+ assert(mod3 % 2 === 1);
3929
+
3930
+ // Fast case
3931
+ if (mod3 === 3) {
3932
+ var pow = this.m.add(new BN(1)).iushrn(2);
3933
+ return this.pow(a, pow);
3934
+ }
3935
+
3936
+ // Tonelli-Shanks algorithm (Totally unoptimized and slow)
3937
+ //
3938
+ // Find Q and S, that Q * 2 ^ S = (P - 1)
3939
+ var q = this.m.subn(1);
3940
+ var s = 0;
3941
+ while (!q.isZero() && q.andln(1) === 0) {
3942
+ s++;
3943
+ q.iushrn(1);
3944
+ }
3945
+ assert(!q.isZero());
3946
+
3947
+ var one = new BN(1).toRed(this);
3948
+ var nOne = one.redNeg();
3949
+
3950
+ // Find quadratic non-residue
3951
+ // NOTE: Max is such because of generalized Riemann hypothesis.
3952
+ var lpow = this.m.subn(1).iushrn(1);
3953
+ var z = this.m.bitLength();
3954
+ z = new BN(2 * z * z).toRed(this);
3955
+
3956
+ while (this.pow(z, lpow).cmp(nOne) !== 0) {
3957
+ z.redIAdd(nOne);
3958
+ }
3959
+
3960
+ var c = this.pow(z, q);
3961
+ var r = this.pow(a, q.addn(1).iushrn(1));
3962
+ var t = this.pow(a, q);
3963
+ var m = s;
3964
+ while (t.cmp(one) !== 0) {
3965
+ var tmp = t;
3966
+ for (var i = 0; tmp.cmp(one) !== 0; i++) {
3967
+ tmp = tmp.redSqr();
3968
+ }
3969
+ assert(i < m);
3970
+ var b = this.pow(c, new BN(1).iushln(m - i - 1));
3971
+
3972
+ r = r.redMul(b);
3973
+ c = b.redSqr();
3974
+ t = t.redMul(c);
3975
+ m = i;
3976
+ }
3977
+
3978
+ return r;
3979
+ };
3980
+
3981
+ Red.prototype.invm = function invm (a) {
3982
+ var inv = a._invmp(this.m);
3983
+ if (inv.negative !== 0) {
3984
+ inv.negative = 0;
3985
+ return this.imod(inv).redNeg();
3986
+ } else {
3987
+ return this.imod(inv);
3988
+ }
3989
+ };
3990
+
3991
+ Red.prototype.pow = function pow (a, num) {
3992
+ if (num.isZero()) return new BN(1).toRed(this);
3993
+ if (num.cmpn(1) === 0) return a.clone();
3994
+
3995
+ var windowSize = 4;
3996
+ var wnd = new Array(1 << windowSize);
3997
+ wnd[0] = new BN(1).toRed(this);
3998
+ wnd[1] = a;
3999
+ for (var i = 2; i < wnd.length; i++) {
4000
+ wnd[i] = this.mul(wnd[i - 1], a);
4001
+ }
4002
+
4003
+ var res = wnd[0];
4004
+ var current = 0;
4005
+ var currentLen = 0;
4006
+ var start = num.bitLength() % 26;
4007
+ if (start === 0) {
4008
+ start = 26;
4009
+ }
4010
+
4011
+ for (i = num.length - 1; i >= 0; i--) {
4012
+ var word = num.words[i];
4013
+ for (var j = start - 1; j >= 0; j--) {
4014
+ var bit = (word >> j) & 1;
4015
+ if (res !== wnd[0]) {
4016
+ res = this.sqr(res);
4017
+ }
4018
+
4019
+ if (bit === 0 && current === 0) {
4020
+ currentLen = 0;
4021
+ continue;
4022
+ }
4023
+
4024
+ current <<= 1;
4025
+ current |= bit;
4026
+ currentLen++;
4027
+ if (currentLen !== windowSize && (i !== 0 || j !== 0)) continue;
4028
+
4029
+ res = this.mul(res, wnd[current]);
4030
+ currentLen = 0;
4031
+ current = 0;
4032
+ }
4033
+ start = 26;
4034
+ }
4035
+
4036
+ return res;
4037
+ };
4038
+
4039
+ Red.prototype.convertTo = function convertTo (num) {
4040
+ var r = num.umod(this.m);
4041
+
4042
+ return r === num ? r.clone() : r;
4043
+ };
4044
+
4045
+ Red.prototype.convertFrom = function convertFrom (num) {
4046
+ var res = num.clone();
4047
+ res.red = null;
4048
+ return res;
4049
+ };
4050
+
4051
+ //
4052
+ // Montgomery method engine
4053
+ //
4054
+
4055
+ BN.mont = function mont (num) {
4056
+ return new Mont(num);
4057
+ };
4058
+
4059
+ function Mont (m) {
4060
+ Red.call(this, m);
4061
+
4062
+ this.shift = this.m.bitLength();
4063
+ if (this.shift % 26 !== 0) {
4064
+ this.shift += 26 - (this.shift % 26);
4065
+ }
4066
+
4067
+ this.r = new BN(1).iushln(this.shift);
4068
+ this.r2 = this.imod(this.r.sqr());
4069
+ this.rinv = this.r._invmp(this.m);
4070
+
4071
+ this.minv = this.rinv.mul(this.r).isubn(1).div(this.m);
4072
+ this.minv = this.minv.umod(this.r);
4073
+ this.minv = this.r.sub(this.minv);
4074
+ }
4075
+ inherits(Mont, Red);
4076
+
4077
+ Mont.prototype.convertTo = function convertTo (num) {
4078
+ return this.imod(num.ushln(this.shift));
4079
+ };
4080
+
4081
+ Mont.prototype.convertFrom = function convertFrom (num) {
4082
+ var r = this.imod(num.mul(this.rinv));
4083
+ r.red = null;
4084
+ return r;
4085
+ };
4086
+
4087
+ Mont.prototype.imul = function imul (a, b) {
4088
+ if (a.isZero() || b.isZero()) {
4089
+ a.words[0] = 0;
4090
+ a.length = 1;
4091
+ return a;
4092
+ }
4093
+
4094
+ var t = a.imul(b);
4095
+ var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m);
4096
+ var u = t.isub(c).iushrn(this.shift);
4097
+ var res = u;
4098
+
4099
+ if (u.cmp(this.m) >= 0) {
4100
+ res = u.isub(this.m);
4101
+ } else if (u.cmpn(0) < 0) {
4102
+ res = u.iadd(this.m);
4103
+ }
4104
+
4105
+ return res._forceRed(this);
4106
+ };
4107
+
4108
+ Mont.prototype.mul = function mul (a, b) {
4109
+ if (a.isZero() || b.isZero()) return new BN(0)._forceRed(this);
4110
+
4111
+ var t = a.mul(b);
4112
+ var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m);
4113
+ var u = t.isub(c).iushrn(this.shift);
4114
+ var res = u;
4115
+ if (u.cmp(this.m) >= 0) {
4116
+ res = u.isub(this.m);
4117
+ } else if (u.cmpn(0) < 0) {
4118
+ res = u.iadd(this.m);
4119
+ }
4120
+
4121
+ return res._forceRed(this);
4122
+ };
4123
+
4124
+ Mont.prototype.invm = function invm (a) {
4125
+ // (AR)^-1 * R^2 = (A^-1 * R^-1) * R^2 = A^-1 * R
4126
+ var res = this.imod(a._invmp(this.m).mul(this.r2));
4127
+ return res._forceRed(this);
4128
+ };
4129
+ })(module, commonjsGlobal);
4130
+ } (bn$1));
4131
+
4132
+ var _BN = bnExports$1;
4133
+
4134
+ const version$4 = "logger/5.7.0";
4135
+
4136
+ let _permanentCensorErrors = false;
4137
+ let _censorErrors = false;
4138
+ const LogLevels = { debug: 1, "default": 2, info: 2, warning: 3, error: 4, off: 5 };
4139
+ let _logLevel = LogLevels["default"];
4140
+ let _globalLogger = null;
4141
+ function _checkNormalize() {
4142
+ try {
4143
+ const missing = [];
4144
+ // Make sure all forms of normalization are supported
4145
+ ["NFD", "NFC", "NFKD", "NFKC"].forEach((form) => {
4146
+ try {
4147
+ if ("test".normalize(form) !== "test") {
4148
+ throw new Error("bad normalize");
4149
+ }
4150
+ ;
4151
+ }
4152
+ catch (error) {
4153
+ missing.push(form);
4154
+ }
4155
+ });
4156
+ if (missing.length) {
4157
+ throw new Error("missing " + missing.join(", "));
4158
+ }
4159
+ if (String.fromCharCode(0xe9).normalize("NFD") !== String.fromCharCode(0x65, 0x0301)) {
4160
+ throw new Error("broken implementation");
4161
+ }
4162
+ }
4163
+ catch (error) {
4164
+ return error.message;
4165
+ }
4166
+ return null;
4167
+ }
4168
+ const _normalizeError = _checkNormalize();
4169
+ var LogLevel;
4170
+ (function (LogLevel) {
4171
+ LogLevel["DEBUG"] = "DEBUG";
4172
+ LogLevel["INFO"] = "INFO";
4173
+ LogLevel["WARNING"] = "WARNING";
4174
+ LogLevel["ERROR"] = "ERROR";
4175
+ LogLevel["OFF"] = "OFF";
4176
+ })(LogLevel || (LogLevel = {}));
4177
+ var ErrorCode;
4178
+ (function (ErrorCode) {
4179
+ ///////////////////
4180
+ // Generic Errors
4181
+ // Unknown Error
4182
+ ErrorCode["UNKNOWN_ERROR"] = "UNKNOWN_ERROR";
4183
+ // Not Implemented
4184
+ ErrorCode["NOT_IMPLEMENTED"] = "NOT_IMPLEMENTED";
4185
+ // Unsupported Operation
4186
+ // - operation
4187
+ ErrorCode["UNSUPPORTED_OPERATION"] = "UNSUPPORTED_OPERATION";
4188
+ // Network Error (i.e. Ethereum Network, such as an invalid chain ID)
4189
+ // - event ("noNetwork" is not re-thrown in provider.ready; otherwise thrown)
4190
+ ErrorCode["NETWORK_ERROR"] = "NETWORK_ERROR";
4191
+ // Some sort of bad response from the server
4192
+ ErrorCode["SERVER_ERROR"] = "SERVER_ERROR";
4193
+ // Timeout
4194
+ ErrorCode["TIMEOUT"] = "TIMEOUT";
4195
+ ///////////////////
4196
+ // Operational Errors
4197
+ // Buffer Overrun
4198
+ ErrorCode["BUFFER_OVERRUN"] = "BUFFER_OVERRUN";
4199
+ // Numeric Fault
4200
+ // - operation: the operation being executed
4201
+ // - fault: the reason this faulted
4202
+ ErrorCode["NUMERIC_FAULT"] = "NUMERIC_FAULT";
4203
+ ///////////////////
4204
+ // Argument Errors
4205
+ // Missing new operator to an object
4206
+ // - name: The name of the class
4207
+ ErrorCode["MISSING_NEW"] = "MISSING_NEW";
4208
+ // Invalid argument (e.g. value is incompatible with type) to a function:
4209
+ // - argument: The argument name that was invalid
4210
+ // - value: The value of the argument
4211
+ ErrorCode["INVALID_ARGUMENT"] = "INVALID_ARGUMENT";
4212
+ // Missing argument to a function:
4213
+ // - count: The number of arguments received
4214
+ // - expectedCount: The number of arguments expected
4215
+ ErrorCode["MISSING_ARGUMENT"] = "MISSING_ARGUMENT";
4216
+ // Too many arguments
4217
+ // - count: The number of arguments received
4218
+ // - expectedCount: The number of arguments expected
4219
+ ErrorCode["UNEXPECTED_ARGUMENT"] = "UNEXPECTED_ARGUMENT";
4220
+ ///////////////////
4221
+ // Blockchain Errors
4222
+ // Call exception
4223
+ // - transaction: the transaction
4224
+ // - address?: the contract address
4225
+ // - args?: The arguments passed into the function
4226
+ // - method?: The Solidity method signature
4227
+ // - errorSignature?: The EIP848 error signature
4228
+ // - errorArgs?: The EIP848 error parameters
4229
+ // - reason: The reason (only for EIP848 "Error(string)")
4230
+ ErrorCode["CALL_EXCEPTION"] = "CALL_EXCEPTION";
4231
+ // Insufficient funds (< value + gasLimit * gasPrice)
4232
+ // - transaction: the transaction attempted
4233
+ ErrorCode["INSUFFICIENT_FUNDS"] = "INSUFFICIENT_FUNDS";
4234
+ // Nonce has already been used
4235
+ // - transaction: the transaction attempted
4236
+ ErrorCode["NONCE_EXPIRED"] = "NONCE_EXPIRED";
4237
+ // The replacement fee for the transaction is too low
4238
+ // - transaction: the transaction attempted
4239
+ ErrorCode["REPLACEMENT_UNDERPRICED"] = "REPLACEMENT_UNDERPRICED";
4240
+ // The gas limit could not be estimated
4241
+ // - transaction: the transaction passed to estimateGas
4242
+ ErrorCode["UNPREDICTABLE_GAS_LIMIT"] = "UNPREDICTABLE_GAS_LIMIT";
4243
+ // The transaction was replaced by one with a higher gas price
4244
+ // - reason: "cancelled", "replaced" or "repriced"
4245
+ // - cancelled: true if reason == "cancelled" or reason == "replaced")
4246
+ // - hash: original transaction hash
4247
+ // - replacement: the full TransactionsResponse for the replacement
4248
+ // - receipt: the receipt of the replacement
4249
+ ErrorCode["TRANSACTION_REPLACED"] = "TRANSACTION_REPLACED";
4250
+ ///////////////////
4251
+ // Interaction Errors
4252
+ // The user rejected the action, such as signing a message or sending
4253
+ // a transaction
4254
+ ErrorCode["ACTION_REJECTED"] = "ACTION_REJECTED";
4255
+ })(ErrorCode || (ErrorCode = {}));
4256
+ const HEX = "0123456789abcdef";
4257
+ class Logger {
4258
+ constructor(version) {
4259
+ Object.defineProperty(this, "version", {
4260
+ enumerable: true,
4261
+ value: version,
4262
+ writable: false
4263
+ });
4264
+ }
4265
+ _log(logLevel, args) {
4266
+ const level = logLevel.toLowerCase();
4267
+ if (LogLevels[level] == null) {
4268
+ this.throwArgumentError("invalid log level name", "logLevel", logLevel);
4269
+ }
4270
+ if (_logLevel > LogLevels[level]) {
4271
+ return;
4272
+ }
4273
+ console.log.apply(console, args);
4274
+ }
4275
+ debug(...args) {
4276
+ this._log(Logger.levels.DEBUG, args);
4277
+ }
4278
+ info(...args) {
4279
+ this._log(Logger.levels.INFO, args);
4280
+ }
4281
+ warn(...args) {
4282
+ this._log(Logger.levels.WARNING, args);
4283
+ }
4284
+ makeError(message, code, params) {
4285
+ // Errors are being censored
4286
+ if (_censorErrors) {
4287
+ return this.makeError("censored error", code, {});
4288
+ }
4289
+ if (!code) {
4290
+ code = Logger.errors.UNKNOWN_ERROR;
4291
+ }
4292
+ if (!params) {
4293
+ params = {};
4294
+ }
4295
+ const messageDetails = [];
4296
+ Object.keys(params).forEach((key) => {
4297
+ const value = params[key];
4298
+ try {
4299
+ if (value instanceof Uint8Array) {
4300
+ let hex = "";
4301
+ for (let i = 0; i < value.length; i++) {
4302
+ hex += HEX[value[i] >> 4];
4303
+ hex += HEX[value[i] & 0x0f];
4304
+ }
4305
+ messageDetails.push(key + "=Uint8Array(0x" + hex + ")");
4306
+ }
4307
+ else {
4308
+ messageDetails.push(key + "=" + JSON.stringify(value));
4309
+ }
4310
+ }
4311
+ catch (error) {
4312
+ messageDetails.push(key + "=" + JSON.stringify(params[key].toString()));
4313
+ }
4314
+ });
4315
+ messageDetails.push(`code=${code}`);
4316
+ messageDetails.push(`version=${this.version}`);
4317
+ const reason = message;
4318
+ let url = "";
4319
+ switch (code) {
4320
+ case ErrorCode.NUMERIC_FAULT: {
4321
+ url = "NUMERIC_FAULT";
4322
+ const fault = message;
4323
+ switch (fault) {
4324
+ case "overflow":
4325
+ case "underflow":
4326
+ case "division-by-zero":
4327
+ url += "-" + fault;
4328
+ break;
4329
+ case "negative-power":
4330
+ case "negative-width":
4331
+ url += "-unsupported";
4332
+ break;
4333
+ case "unbound-bitwise-result":
4334
+ url += "-unbound-result";
4335
+ break;
4336
+ }
4337
+ break;
4338
+ }
4339
+ case ErrorCode.CALL_EXCEPTION:
4340
+ case ErrorCode.INSUFFICIENT_FUNDS:
4341
+ case ErrorCode.MISSING_NEW:
4342
+ case ErrorCode.NONCE_EXPIRED:
4343
+ case ErrorCode.REPLACEMENT_UNDERPRICED:
4344
+ case ErrorCode.TRANSACTION_REPLACED:
4345
+ case ErrorCode.UNPREDICTABLE_GAS_LIMIT:
4346
+ url = code;
4347
+ break;
4348
+ }
4349
+ if (url) {
4350
+ message += " [ See: https:/\/links.ethers.org/v5-errors-" + url + " ]";
4351
+ }
4352
+ if (messageDetails.length) {
4353
+ message += " (" + messageDetails.join(", ") + ")";
4354
+ }
4355
+ // @TODO: Any??
4356
+ const error = new Error(message);
4357
+ error.reason = reason;
4358
+ error.code = code;
4359
+ Object.keys(params).forEach(function (key) {
4360
+ error[key] = params[key];
4361
+ });
4362
+ return error;
4363
+ }
4364
+ throwError(message, code, params) {
4365
+ throw this.makeError(message, code, params);
4366
+ }
4367
+ throwArgumentError(message, name, value) {
4368
+ return this.throwError(message, Logger.errors.INVALID_ARGUMENT, {
4369
+ argument: name,
4370
+ value: value
4371
+ });
4372
+ }
4373
+ assert(condition, message, code, params) {
4374
+ if (!!condition) {
4375
+ return;
4376
+ }
4377
+ this.throwError(message, code, params);
4378
+ }
4379
+ assertArgument(condition, message, name, value) {
4380
+ if (!!condition) {
4381
+ return;
4382
+ }
4383
+ this.throwArgumentError(message, name, value);
4384
+ }
4385
+ checkNormalize(message) {
4386
+ if (_normalizeError) {
4387
+ this.throwError("platform missing String.prototype.normalize", Logger.errors.UNSUPPORTED_OPERATION, {
4388
+ operation: "String.prototype.normalize", form: _normalizeError
4389
+ });
4390
+ }
4391
+ }
4392
+ checkSafeUint53(value, message) {
4393
+ if (typeof (value) !== "number") {
4394
+ return;
4395
+ }
4396
+ if (message == null) {
4397
+ message = "value not safe";
4398
+ }
4399
+ if (value < 0 || value >= 0x1fffffffffffff) {
4400
+ this.throwError(message, Logger.errors.NUMERIC_FAULT, {
4401
+ operation: "checkSafeInteger",
4402
+ fault: "out-of-safe-range",
4403
+ value: value
4404
+ });
4405
+ }
4406
+ if (value % 1) {
4407
+ this.throwError(message, Logger.errors.NUMERIC_FAULT, {
4408
+ operation: "checkSafeInteger",
4409
+ fault: "non-integer",
4410
+ value: value
4411
+ });
4412
+ }
4413
+ }
4414
+ checkArgumentCount(count, expectedCount, message) {
4415
+ if (message) {
4416
+ message = ": " + message;
4417
+ }
4418
+ else {
4419
+ message = "";
4420
+ }
4421
+ if (count < expectedCount) {
4422
+ this.throwError("missing argument" + message, Logger.errors.MISSING_ARGUMENT, {
4423
+ count: count,
4424
+ expectedCount: expectedCount
4425
+ });
4426
+ }
4427
+ if (count > expectedCount) {
4428
+ this.throwError("too many arguments" + message, Logger.errors.UNEXPECTED_ARGUMENT, {
4429
+ count: count,
4430
+ expectedCount: expectedCount
4431
+ });
4432
+ }
4433
+ }
4434
+ checkNew(target, kind) {
4435
+ if (target === Object || target == null) {
4436
+ this.throwError("missing new", Logger.errors.MISSING_NEW, { name: kind.name });
4437
+ }
4438
+ }
4439
+ checkAbstract(target, kind) {
4440
+ if (target === kind) {
4441
+ this.throwError("cannot instantiate abstract class " + JSON.stringify(kind.name) + " directly; use a sub-class", Logger.errors.UNSUPPORTED_OPERATION, { name: target.name, operation: "new" });
4442
+ }
4443
+ else if (target === Object || target == null) {
4444
+ this.throwError("missing new", Logger.errors.MISSING_NEW, { name: kind.name });
4445
+ }
4446
+ }
4447
+ static globalLogger() {
4448
+ if (!_globalLogger) {
4449
+ _globalLogger = new Logger(version$4);
4450
+ }
4451
+ return _globalLogger;
4452
+ }
4453
+ static setCensorship(censorship, permanent) {
4454
+ if (!censorship && permanent) {
4455
+ this.globalLogger().throwError("cannot permanently disable censorship", Logger.errors.UNSUPPORTED_OPERATION, {
4456
+ operation: "setCensorship"
4457
+ });
4458
+ }
4459
+ if (_permanentCensorErrors) {
4460
+ if (!censorship) {
4461
+ return;
4462
+ }
4463
+ this.globalLogger().throwError("error censorship permanent", Logger.errors.UNSUPPORTED_OPERATION, {
4464
+ operation: "setCensorship"
4465
+ });
4466
+ }
4467
+ _censorErrors = !!censorship;
4468
+ _permanentCensorErrors = !!permanent;
4469
+ }
4470
+ static setLogLevel(logLevel) {
4471
+ const level = LogLevels[logLevel.toLowerCase()];
4472
+ if (level == null) {
4473
+ Logger.globalLogger().warn("invalid log level - " + logLevel);
4474
+ return;
4475
+ }
4476
+ _logLevel = level;
4477
+ }
4478
+ static from(version) {
4479
+ return new Logger(version);
4480
+ }
4481
+ }
4482
+ Logger.errors = ErrorCode;
4483
+ Logger.levels = LogLevel;
4484
+
4485
+ const version$3 = "bytes/5.7.0";
4486
+
4487
+ const logger$1 = new Logger(version$3);
4488
+ ///////////////////////////////
4489
+ function isHexable(value) {
4490
+ return !!(value.toHexString);
4491
+ }
4492
+ function isInteger(value) {
4493
+ return (typeof (value) === "number" && value == value && (value % 1) === 0);
4494
+ }
4495
+ function isBytes(value) {
4496
+ if (value == null) {
4497
+ return false;
4498
+ }
4499
+ if (value.constructor === Uint8Array) {
4500
+ return true;
4501
+ }
4502
+ if (typeof (value) === "string") {
4503
+ return false;
4504
+ }
4505
+ if (!isInteger(value.length) || value.length < 0) {
4506
+ return false;
4507
+ }
4508
+ for (let i = 0; i < value.length; i++) {
4509
+ const v = value[i];
4510
+ if (!isInteger(v) || v < 0 || v >= 256) {
4511
+ return false;
4512
+ }
4513
+ }
4514
+ return true;
4515
+ }
4516
+ function isHexString(value, length) {
4517
+ if (typeof (value) !== "string" || !value.match(/^0x[0-9A-Fa-f]*$/)) {
4518
+ return false;
4519
+ }
4520
+ if (length && value.length !== 2 + 2 * length) {
4521
+ return false;
4522
+ }
4523
+ return true;
4524
+ }
4525
+ const HexCharacters = "0123456789abcdef";
4526
+ function hexlify(value, options) {
4527
+ if (!options) {
4528
+ options = {};
4529
+ }
4530
+ if (typeof (value) === "number") {
4531
+ logger$1.checkSafeUint53(value, "invalid hexlify value");
4532
+ let hex = "";
4533
+ while (value) {
4534
+ hex = HexCharacters[value & 0xf] + hex;
4535
+ value = Math.floor(value / 16);
4536
+ }
4537
+ if (hex.length) {
4538
+ if (hex.length % 2) {
4539
+ hex = "0" + hex;
4540
+ }
4541
+ return "0x" + hex;
4542
+ }
4543
+ return "0x00";
4544
+ }
4545
+ if (typeof (value) === "bigint") {
4546
+ value = value.toString(16);
4547
+ if (value.length % 2) {
4548
+ return ("0x0" + value);
4549
+ }
4550
+ return "0x" + value;
4551
+ }
4552
+ if (options.allowMissingPrefix && typeof (value) === "string" && value.substring(0, 2) !== "0x") {
4553
+ value = "0x" + value;
4554
+ }
4555
+ if (isHexable(value)) {
4556
+ return value.toHexString();
4557
+ }
4558
+ if (isHexString(value)) {
4559
+ if (value.length % 2) {
4560
+ if (options.hexPad === "left") {
4561
+ value = "0x0" + value.substring(2);
4562
+ }
4563
+ else if (options.hexPad === "right") {
4564
+ value += "0";
4565
+ }
4566
+ else {
4567
+ logger$1.throwArgumentError("hex data is odd-length", "value", value);
4568
+ }
4569
+ }
4570
+ return value.toLowerCase();
4571
+ }
4572
+ if (isBytes(value)) {
4573
+ let result = "0x";
4574
+ for (let i = 0; i < value.length; i++) {
4575
+ let v = value[i];
4576
+ result += HexCharacters[(v & 0xf0) >> 4] + HexCharacters[v & 0x0f];
4577
+ }
4578
+ return result;
4579
+ }
4580
+ return logger$1.throwArgumentError("invalid hexlify value", "value", value);
4581
+ }
4582
+
4583
+ const version$2 = "bignumber/5.7.0";
4584
+
4585
+ var BN$9 = _BN.BN;
4586
+ const logger = new Logger(version$2);
4587
+ const _constructorGuard = {};
4588
+ const MAX_SAFE = 0x1fffffffffffff;
4589
+ // Only warn about passing 10 into radix once
4590
+ let _warnedToStringRadix = false;
4591
+ class BigNumber {
4592
+ constructor(constructorGuard, hex) {
4593
+ if (constructorGuard !== _constructorGuard) {
4594
+ logger.throwError("cannot call constructor directly; use BigNumber.from", Logger.errors.UNSUPPORTED_OPERATION, {
4595
+ operation: "new (BigNumber)"
4596
+ });
4597
+ }
4598
+ this._hex = hex;
4599
+ this._isBigNumber = true;
4600
+ Object.freeze(this);
4601
+ }
4602
+ fromTwos(value) {
4603
+ return toBigNumber(toBN(this).fromTwos(value));
4604
+ }
4605
+ toTwos(value) {
4606
+ return toBigNumber(toBN(this).toTwos(value));
4607
+ }
4608
+ abs() {
4609
+ if (this._hex[0] === "-") {
4610
+ return BigNumber.from(this._hex.substring(1));
4611
+ }
4612
+ return this;
4613
+ }
4614
+ add(other) {
4615
+ return toBigNumber(toBN(this).add(toBN(other)));
4616
+ }
4617
+ sub(other) {
4618
+ return toBigNumber(toBN(this).sub(toBN(other)));
4619
+ }
4620
+ div(other) {
4621
+ const o = BigNumber.from(other);
4622
+ if (o.isZero()) {
4623
+ throwFault("division-by-zero", "div");
4624
+ }
4625
+ return toBigNumber(toBN(this).div(toBN(other)));
4626
+ }
4627
+ mul(other) {
4628
+ return toBigNumber(toBN(this).mul(toBN(other)));
4629
+ }
4630
+ mod(other) {
4631
+ const value = toBN(other);
4632
+ if (value.isNeg()) {
4633
+ throwFault("division-by-zero", "mod");
4634
+ }
4635
+ return toBigNumber(toBN(this).umod(value));
4636
+ }
4637
+ pow(other) {
4638
+ const value = toBN(other);
4639
+ if (value.isNeg()) {
4640
+ throwFault("negative-power", "pow");
4641
+ }
4642
+ return toBigNumber(toBN(this).pow(value));
4643
+ }
4644
+ and(other) {
4645
+ const value = toBN(other);
4646
+ if (this.isNegative() || value.isNeg()) {
4647
+ throwFault("unbound-bitwise-result", "and");
4648
+ }
4649
+ return toBigNumber(toBN(this).and(value));
4650
+ }
4651
+ or(other) {
4652
+ const value = toBN(other);
4653
+ if (this.isNegative() || value.isNeg()) {
4654
+ throwFault("unbound-bitwise-result", "or");
4655
+ }
4656
+ return toBigNumber(toBN(this).or(value));
4657
+ }
4658
+ xor(other) {
4659
+ const value = toBN(other);
4660
+ if (this.isNegative() || value.isNeg()) {
4661
+ throwFault("unbound-bitwise-result", "xor");
4662
+ }
4663
+ return toBigNumber(toBN(this).xor(value));
4664
+ }
4665
+ mask(value) {
4666
+ if (this.isNegative() || value < 0) {
4667
+ throwFault("negative-width", "mask");
4668
+ }
4669
+ return toBigNumber(toBN(this).maskn(value));
4670
+ }
4671
+ shl(value) {
4672
+ if (this.isNegative() || value < 0) {
4673
+ throwFault("negative-width", "shl");
4674
+ }
4675
+ return toBigNumber(toBN(this).shln(value));
4676
+ }
4677
+ shr(value) {
4678
+ if (this.isNegative() || value < 0) {
4679
+ throwFault("negative-width", "shr");
4680
+ }
4681
+ return toBigNumber(toBN(this).shrn(value));
4682
+ }
4683
+ eq(other) {
4684
+ return toBN(this).eq(toBN(other));
4685
+ }
4686
+ lt(other) {
4687
+ return toBN(this).lt(toBN(other));
4688
+ }
4689
+ lte(other) {
4690
+ return toBN(this).lte(toBN(other));
4691
+ }
4692
+ gt(other) {
4693
+ return toBN(this).gt(toBN(other));
4694
+ }
4695
+ gte(other) {
4696
+ return toBN(this).gte(toBN(other));
4697
+ }
4698
+ isNegative() {
4699
+ return (this._hex[0] === "-");
4700
+ }
4701
+ isZero() {
4702
+ return toBN(this).isZero();
4703
+ }
4704
+ toNumber() {
4705
+ try {
4706
+ return toBN(this).toNumber();
4707
+ }
4708
+ catch (error) {
4709
+ throwFault("overflow", "toNumber", this.toString());
4710
+ }
4711
+ return null;
4712
+ }
4713
+ toBigInt() {
4714
+ try {
4715
+ return BigInt(this.toString());
4716
+ }
4717
+ catch (e) { }
4718
+ return logger.throwError("this platform does not support BigInt", Logger.errors.UNSUPPORTED_OPERATION, {
4719
+ value: this.toString()
4720
+ });
4721
+ }
4722
+ toString() {
4723
+ // Lots of people expect this, which we do not support, so check (See: #889)
4724
+ if (arguments.length > 0) {
4725
+ if (arguments[0] === 10) {
4726
+ if (!_warnedToStringRadix) {
4727
+ _warnedToStringRadix = true;
4728
+ logger.warn("BigNumber.toString does not accept any parameters; base-10 is assumed");
4729
+ }
4730
+ }
4731
+ else if (arguments[0] === 16) {
4732
+ logger.throwError("BigNumber.toString does not accept any parameters; use bigNumber.toHexString()", Logger.errors.UNEXPECTED_ARGUMENT, {});
4733
+ }
4734
+ else {
4735
+ logger.throwError("BigNumber.toString does not accept parameters", Logger.errors.UNEXPECTED_ARGUMENT, {});
4736
+ }
4737
+ }
4738
+ return toBN(this).toString(10);
4739
+ }
4740
+ toHexString() {
4741
+ return this._hex;
4742
+ }
4743
+ toJSON(key) {
4744
+ return { type: "BigNumber", hex: this.toHexString() };
4745
+ }
4746
+ static from(value) {
4747
+ if (value instanceof BigNumber) {
4748
+ return value;
4749
+ }
4750
+ if (typeof (value) === "string") {
4751
+ if (value.match(/^-?0x[0-9a-f]+$/i)) {
4752
+ return new BigNumber(_constructorGuard, toHex$1(value));
4753
+ }
4754
+ if (value.match(/^-?[0-9]+$/)) {
4755
+ return new BigNumber(_constructorGuard, toHex$1(new BN$9(value)));
4756
+ }
4757
+ return logger.throwArgumentError("invalid BigNumber string", "value", value);
4758
+ }
4759
+ if (typeof (value) === "number") {
4760
+ if (value % 1) {
4761
+ throwFault("underflow", "BigNumber.from", value);
4762
+ }
4763
+ if (value >= MAX_SAFE || value <= -MAX_SAFE) {
4764
+ throwFault("overflow", "BigNumber.from", value);
4765
+ }
4766
+ return BigNumber.from(String(value));
4767
+ }
4768
+ const anyValue = value;
4769
+ if (typeof (anyValue) === "bigint") {
4770
+ return BigNumber.from(anyValue.toString());
4771
+ }
4772
+ if (isBytes(anyValue)) {
4773
+ return BigNumber.from(hexlify(anyValue));
4774
+ }
4775
+ if (anyValue) {
4776
+ // Hexable interface (takes priority)
4777
+ if (anyValue.toHexString) {
4778
+ const hex = anyValue.toHexString();
4779
+ if (typeof (hex) === "string") {
4780
+ return BigNumber.from(hex);
4781
+ }
4782
+ }
4783
+ else {
4784
+ // For now, handle legacy JSON-ified values (goes away in v6)
4785
+ let hex = anyValue._hex;
4786
+ // New-form JSON
4787
+ if (hex == null && anyValue.type === "BigNumber") {
4788
+ hex = anyValue.hex;
4789
+ }
4790
+ if (typeof (hex) === "string") {
4791
+ if (isHexString(hex) || (hex[0] === "-" && isHexString(hex.substring(1)))) {
4792
+ return BigNumber.from(hex);
4793
+ }
4794
+ }
4795
+ }
4796
+ }
4797
+ return logger.throwArgumentError("invalid BigNumber value", "value", value);
4798
+ }
4799
+ static isBigNumber(value) {
4800
+ return !!(value && value._isBigNumber);
4801
+ }
4802
+ }
4803
+ // Normalize the hex string
4804
+ function toHex$1(value) {
4805
+ // For BN, call on the hex string
4806
+ if (typeof (value) !== "string") {
4807
+ return toHex$1(value.toString(16));
4808
+ }
4809
+ // If negative, prepend the negative sign to the normalized positive value
4810
+ if (value[0] === "-") {
4811
+ // Strip off the negative sign
4812
+ value = value.substring(1);
4813
+ // Cannot have multiple negative signs (e.g. "--0x04")
4814
+ if (value[0] === "-") {
4815
+ logger.throwArgumentError("invalid hex", "value", value);
4816
+ }
4817
+ // Call toHex on the positive component
4818
+ value = toHex$1(value);
4819
+ // Do not allow "-0x00"
4820
+ if (value === "0x00") {
4821
+ return value;
4822
+ }
4823
+ // Negate the value
4824
+ return "-" + value;
4825
+ }
4826
+ // Add a "0x" prefix if missing
4827
+ if (value.substring(0, 2) !== "0x") {
4828
+ value = "0x" + value;
4829
+ }
4830
+ // Normalize zero
4831
+ if (value === "0x") {
4832
+ return "0x00";
4833
+ }
4834
+ // Make the string even length
4835
+ if (value.length % 2) {
4836
+ value = "0x0" + value.substring(2);
4837
+ }
4838
+ // Trim to smallest even-length string
4839
+ while (value.length > 4 && value.substring(0, 4) === "0x00") {
4840
+ value = "0x" + value.substring(4);
4841
+ }
4842
+ return value;
4843
+ }
4844
+ function toBigNumber(value) {
4845
+ return BigNumber.from(toHex$1(value));
4846
+ }
4847
+ function toBN(value) {
4848
+ const hex = BigNumber.from(value).toHexString();
4849
+ if (hex[0] === "-") {
4850
+ return (new BN$9("-" + hex.substring(3), 16));
4851
+ }
4852
+ return new BN$9(hex.substring(2), 16);
4853
+ }
4854
+ function throwFault(fault, operation, value) {
4855
+ const params = { fault: fault, operation: operation };
4856
+ if (value != null) {
4857
+ params.value = value;
4858
+ }
4859
+ return logger.throwError(fault, Logger.errors.NUMERIC_FAULT, params);
4860
+ }
4861
+
4862
+ const { fromString, toString } = index$2;
4863
+ const isJson = (type) => type === 'object' || 'array';
4864
+ const isString = (type) => type === 'string';
4865
+ const isNumber = (type) => type === 'number';
4866
+ const isBoolean = (type) => type === 'boolean';
4867
+ const isUint8Array$1 = (type) => type === 'uint8Array';
4868
+ const isBigNumber = (type) => type === 'bigNumber';
4869
+ const tokenize = (key, value) => {
4870
+ const optional = key.endsWith('?');
4871
+ let type = value;
4872
+ type = Array.isArray(type) ? 'array' : typeof type;
4873
+ if (value instanceof Uint8Array)
4874
+ type = 'uint8Array';
4875
+ else if (value._isBigNumber)
4876
+ type = 'bigNumber';
4877
+ const parts = key.split('?');
4878
+ const minimumLength = parts[2]?.includes('min') ? parts[2].split['min:'][1] : 0;
4879
+ return { type, optional, key: parts[0], minimumLength };
4880
+ };
4881
+ const toType = (data) => {
4882
+ // always return uint8Arrays as they are
4883
+ if (data instanceof Uint8Array)
4884
+ return data;
4885
+ // returns the ArrayBuffer as a UintArray
4886
+ if (data instanceof ArrayBuffer)
4887
+ return new Uint8Array(data);
4888
+ if (data._isBigNumber)
4889
+ return new TextEncoder().encode(data._hex || data.toHexString());
4890
+ // returns the string as a UintArray
4891
+ if (typeof data === 'string')
4892
+ return new TextEncoder().encode(data);
4893
+ // returns the object as a UintArray
4894
+ if (typeof data === 'object')
4895
+ return new TextEncoder().encode(JSON.stringify(data));
4896
+ // returns the number as a UintArray
4897
+ if (typeof data === 'number' || typeof data === 'boolean')
4898
+ return new TextEncoder().encode(data.toString());
4899
+ throw new Error(`unsuported type ${typeof data || data}`);
4900
+ };
4901
+ const encode$3 = (proto, input) => {
4902
+ const keys = Object.keys(proto);
4903
+ const values = Object.values(proto);
4904
+ const set = [];
4905
+ for (let i = 0; i < keys.length; i++) {
4906
+ const token = tokenize(keys[i], values[i]);
4907
+ const data = input[token.key];
4908
+ if (!token.optional && data === undefined)
4909
+ throw new Error(`missing required property: ${token.key}`);
4910
+ if (token.type === 'array' && token.minimumLength > data.length || token.type === 'object' && token.minimumLength > Object.keys(data).length)
4911
+ throw new Error(`minimumLength for ${token.key} is set to ${token.minimumLength} but got ${data.length}`);
4912
+ // always push data to the set.
4913
+ // when data is undefined push the default value of the proto
4914
+ set.push(toType(data || values[i]));
4915
+ }
4916
+ return index$6(set);
4917
+ };
4918
+ const decode$4 = (proto, uint8Array) => {
4919
+ let deconcated = index$5(uint8Array);
4920
+ const output = {};
4921
+ const keys = Object.keys(proto);
4922
+ const values = Object.values(proto);
4923
+ if (keys.length !== deconcated.length)
4924
+ console.warn(`length mismatch: expected ${keys.length} got ${uint8Array.length}`);
4925
+ for (let i = 0; i < keys.length; i++) {
4926
+ const token = tokenize(keys[i], values[i]);
4927
+ if (isUint8Array$1(token.type))
4928
+ output[token.key] = deconcated[i];
4929
+ else if (isString(token.type))
4930
+ output[token.key] = toString(deconcated[i]);
4931
+ else if (isBoolean(token.type))
4932
+ output[token.key] = Boolean(new TextDecoder().decode(deconcated[i]));
4933
+ else if (isNumber(token.type))
4934
+ output[token.key] = Number(new TextDecoder().decode(deconcated[i]));
4935
+ else if (isBigNumber(token.type))
4936
+ output[token.key] = BigNumber.from(new TextDecoder().decode(deconcated[i]));
4937
+ else if (isJson(token.type))
4938
+ output[token.key] = JSON.parse(new TextDecoder().decode(deconcated[i]));
4939
+ if (token.optional) {
4940
+ if (!output[token.key] || output[token.key].length === 0)
4941
+ delete output[token.key];
4942
+ }
4943
+ if (!token.optional && output[token.key] === undefined)
4944
+ throw new Error(`missing required property: ${token.key}`);
4945
+ }
4946
+ return output;
4947
+ };
4948
+ var index$1 = {
4949
+ encode: encode$3,
4950
+ decode: decode$4
814
4951
  };
815
4952
 
816
4953
  /*!
@@ -1624,7 +5761,7 @@ let BasicInterface$1 = class BasicInterface {
1624
5761
  toHex() {
1625
5762
  if (!this.encoded)
1626
5763
  this.encode();
1627
- return toHex$1(this.encoded.toString().split(',').map(number => Number(number)));
5764
+ return toHex$2(this.encoded.toString().split(',').map(number => Number(number)));
1628
5765
  }
1629
5766
  /**
1630
5767
  * @return {String} encoded
@@ -2206,7 +6343,7 @@ class DhtEarth {
2206
6343
  const address = await getAddress();
2207
6344
  const peerLoc = await this.getCoordinates(address);
2208
6345
  for (const provider of providers) {
2209
- if (provider.address === '127.0.0.1')
6346
+ if (provider.address === '127.0.0.1' || provider.address === '::1')
2210
6347
  all.push({ provider, distance: 0 });
2211
6348
  else
2212
6349
  all.push(this.getDistance(peerLoc, provider));
@@ -2273,9 +6410,9 @@ class MessageHandler {
2273
6410
  const dataHandler = async (message) => {
2274
6411
  if (!message)
2275
6412
  return;
2276
- const { data, id, from } = message;
6413
+ const { data, id, from, peer } = message;
2277
6414
  const proto = await protoFor(data);
2278
- peernet._protoHandler({ id, proto }, peernet.client.connections[from], from);
6415
+ peernet._protoHandler({ id, proto }, peernet.connections[from] || peer, from);
2279
6416
  };
2280
6417
 
2281
6418
  const dhtError = (proto) => {
@@ -2681,40 +6818,6 @@ var index = {
2681
6818
  }
2682
6819
  };
2683
6820
 
2684
- var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
2685
-
2686
- function getDefaultExportFromCjs (x) {
2687
- return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
2688
- }
2689
-
2690
- function getAugmentedNamespace(n) {
2691
- if (n.__esModule) return n;
2692
- var f = n.default;
2693
- if (typeof f == "function") {
2694
- var a = function a () {
2695
- if (this instanceof a) {
2696
- var args = [null];
2697
- args.push.apply(args, arguments);
2698
- var Ctor = Function.bind.apply(f, args);
2699
- return new Ctor();
2700
- }
2701
- return f.apply(this, arguments);
2702
- };
2703
- a.prototype = f.prototype;
2704
- } else a = {};
2705
- Object.defineProperty(a, '__esModule', {value: true});
2706
- Object.keys(n).forEach(function (k) {
2707
- var d = Object.getOwnPropertyDescriptor(n, k);
2708
- Object.defineProperty(a, k, d.get ? d : {
2709
- enumerable: true,
2710
- get: function () {
2711
- return n[k];
2712
- }
2713
- });
2714
- });
2715
- return a;
2716
- }
2717
-
2718
6821
  const errors = {
2719
6822
  IMPOSSIBLE_CASE: 'Impossible case. Please create issue.',
2720
6823
  TWEAK_ADD:
@@ -3133,15 +7236,6 @@ var bn = {
3133
7236
  set exports(v){ bnExports = v; },
3134
7237
  };
3135
7238
 
3136
- var _nodeResolve_empty = {};
3137
-
3138
- var _nodeResolve_empty$1 = /*#__PURE__*/Object.freeze({
3139
- __proto__: null,
3140
- default: _nodeResolve_empty
3141
- });
3142
-
3143
- var require$$3 = /*@__PURE__*/getAugmentedNamespace(_nodeResolve_empty$1);
3144
-
3145
7239
  (function (module) {
3146
7240
  (function (module, exports) {
3147
7241
 
@@ -3197,7 +7291,7 @@ var require$$3 = /*@__PURE__*/getAugmentedNamespace(_nodeResolve_empty$1);
3197
7291
  if (typeof window !== 'undefined' && typeof window.Buffer !== 'undefined') {
3198
7292
  Buffer = window.Buffer;
3199
7293
  } else {
3200
- Buffer = require$$3.Buffer;
7294
+ Buffer = require$$0$1.Buffer;
3201
7295
  }
3202
7296
  } catch (e) {
3203
7297
  }
@@ -6843,7 +10937,7 @@ if (typeof self === 'object') {
6843
10937
  } else {
6844
10938
  // Node.js or Web worker with no crypto support
6845
10939
  try {
6846
- var crypto$1 = require$$3;
10940
+ var crypto$1 = require$$0$1;
6847
10941
  if (typeof crypto$1.randomBytes !== 'function')
6848
10942
  throw new Error('Not supported');
6849
10943
 
@@ -16169,7 +20263,7 @@ class Identity {
16169
20263
  globalThis.peernet.selectedAccount = new TextDecoder().decode(selected);
16170
20264
  }
16171
20265
  else {
16172
- const importee = await import(/* webpackChunkName: "generate-account" */ './index-3d3f56ca-064d16ce.js');
20266
+ const importee = await import(/* webpackChunkName: "generate-account" */ './index-712688fc-ad40bdec.js');
16173
20267
  const { identity, accounts } = await importee.default(password, this.network);
16174
20268
  await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
16175
20269
  await globalThis.walletStore.put('version', String(1));
@@ -16233,6 +20327,7 @@ class Peernet {
16233
20327
  autoStart = true;
16234
20328
  #starting = false;
16235
20329
  #started = false;
20330
+ #connections = {};
16236
20331
  /**
16237
20332
  * @access public
16238
20333
  * @param {Object} options
@@ -16311,13 +20406,13 @@ class Peernet {
16311
20406
  * @return {Array} peerId
16312
20407
  */
16313
20408
  get peers() {
16314
- return Object.keys(this.client.connections);
20409
+ return Object.entries(this.#connections);
16315
20410
  }
16316
20411
  get connections() {
16317
- return Object.values(this.client.connections);
20412
+ return Object.values(this.#connections);
16318
20413
  }
16319
20414
  get peerEntries() {
16320
- return Object.entries(this.client.connections);
20415
+ return Object.values(this.#connections);
16321
20416
  }
16322
20417
  /**
16323
20418
  * @return {String} id - peerId
@@ -16339,7 +20434,7 @@ class Peernet {
16339
20434
  this.root = options.root;
16340
20435
  const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
16341
20436
  // FolderMessageResponse
16342
- } = await import(/* webpackChunkName: "messages" */ './messages-af41e873-479a93a8.js');
20437
+ } = await import(/* webpackChunkName: "messages" */ './messages-3f370680-fb7e5b5b.js');
16343
20438
  /**
16344
20439
  * proto Object containing protos
16345
20440
  * @type {Object}
@@ -16373,28 +20468,33 @@ class Peernet {
16373
20468
  await this.identity.load(password);
16374
20469
  this._peerHandler = new PeerDiscovery(this.id);
16375
20470
  this.peerId = this.id;
16376
- pubsub.subscribe('peer:connected', async (peer) => {
20471
+ this.addRequestHandler('handshake', () => {
20472
+ return new peernet.protos['peernet-response']({ response: { peerId: this.id } });
20473
+ });
20474
+ pubsub.subscribe('peer:discovered', async (peer) => {
16377
20475
  // console.log(peer);
16378
- // console.log({connected: peer.id, as: this._getPeerId(peer.id) });
16379
- // peer.on('peernet.data', async (message) => {
16380
- // const id = message.id
16381
- // message = new PeernetMessage(Buffer.from(message.data.data))
16382
- // const proto = protoFor(message.decoded.data)
16383
- // this._protoHandler({id, proto}, peer)
16384
- // })
20476
+ let data = await new globalThis.peernet.protos['peernet-request']({ request: 'handshake' });
20477
+ let node = await globalThis.peernet.prepareMessage(data);
20478
+ let response = await peer.request(node.encoded);
20479
+ response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
20480
+ // todo: response.decoded should be the response and not response.peerId
20481
+ this.#connections[response.decoded.response.peerId] = peer;
20482
+ pubsub.publish('peer:connected', peer);
20483
+ // todo: cleanup discovered
16385
20484
  });
20485
+ pubsub.subscribe('peer:left', this.#peerLeft.bind(this));
16386
20486
  /**
16387
20487
  * converts data -> message -> proto
16388
20488
  * @see DataHandler
16389
20489
  */
16390
20490
  pubsub.subscribe('peer:data', dataHandler);
16391
20491
  if (globalThis.navigator) {
16392
- globalThis.addEventListener('beforeunload', async () => this.client.close());
20492
+ globalThis.addEventListener('beforeunload', async () => this.client.destroy());
16393
20493
  }
16394
20494
  else {
16395
20495
  process.on('SIGTERM', async () => {
16396
20496
  process.stdin.resume();
16397
- await this.client.close();
20497
+ await this.client.destroy();
16398
20498
  process.exit();
16399
20499
  });
16400
20500
  }
@@ -16406,7 +20506,7 @@ class Peernet {
16406
20506
  if (this.#starting || this.#started)
16407
20507
  return;
16408
20508
  this.#starting = true;
16409
- const importee = await import('./client-111c93a3-b0dcdc19.js');
20509
+ const importee = await import('./client-de444350-a666f9b6.js');
16410
20510
  /**
16411
20511
  * @access public
16412
20512
  * @type {PeernetClient}
@@ -16415,15 +20515,24 @@ class Peernet {
16415
20515
  this.#started = true;
16416
20516
  this.#starting = false;
16417
20517
  }
20518
+ #peerLeft(peer) {
20519
+ for (const [id, _peer] of Object.entries(this.#connections)) {
20520
+ if (peer.id === peer.id) {
20521
+ delete this.#connections[id];
20522
+ this.removePeer(_peer);
20523
+ }
20524
+ }
20525
+ }
16418
20526
  addRequestHandler(name, method) {
16419
20527
  this.requestProtos[name] = method;
16420
20528
  }
16421
- sendMessage(peer, id, data) {
16422
- if (peer.readyState === 'open') {
16423
- peer.send(data, id);
20529
+ async sendMessage(peer, id, data) {
20530
+ if (peer.connected) {
20531
+ await peer.send(data, id);
16424
20532
  this.bw.up += data.length;
16425
20533
  }
16426
- else if (peer.readyState === 'closed') ;
20534
+ else
20535
+ this.removePeer(peer);
16427
20536
  }
16428
20537
  async handleDHT(peer, id, proto) {
16429
20538
  let { hash, store } = proto.decoded;
@@ -16480,6 +20589,7 @@ class Peernet {
16480
20589
  }
16481
20590
  case 'peernet-request': {
16482
20591
  this.handleRequest(peer, id, proto);
20592
+ break;
16483
20593
  }
16484
20594
  case 'peernet-ps': {
16485
20595
  if (peer.peerId !== this.id)
@@ -16496,33 +20606,24 @@ class Peernet {
16496
20606
  if (!hash)
16497
20607
  throw new Error('hash expected, received undefined');
16498
20608
  const data = await new globalThis.peernet.protos['peernet-dht']({ hash });
16499
- this.client.id;
16500
- const walk = async (peer) => {
20609
+ const walk = async (peer, peerId) => {
16501
20610
  const node = await this.prepareMessage(data);
16502
20611
  let result = await peer.request(node.encoded);
16503
20612
  result = new Uint8Array(Object.values(result));
16504
20613
  const proto = await protoFor(result);
16505
20614
  if (proto.name !== 'peernet-dht-response')
16506
20615
  throw dhtError(proto.name);
16507
- // TODO: give ip and port (just used for location)
16508
- if (!peer.connection.remoteAddress || !peer.connection.localAddress) {
16509
- peer.connection.remoteFamily = 'ipv4';
16510
- peer.connection.remoteAddress = '127.0.0.1';
16511
- peer.connection.remotePort = '0000';
16512
- }
16513
20616
  const peerInfo = {
16514
- family: peer.connection.remoteFamily || peer.connection.localFamily,
16515
- address: peer.connection.remoteAddress || peer.connection.localAddress,
16516
- port: peer.connection.remotePort || peer.connection.localPort,
16517
- id: peer.peerId,
20617
+ ...peer.connectionStats,
20618
+ id: peerId,
16518
20619
  };
16519
20620
  if (proto.decoded.has)
16520
20621
  this.dht.addProvider(peerInfo, proto.decoded.hash);
16521
20622
  };
16522
20623
  let walks = [];
16523
- for (const peer of this.connections) {
16524
- if (peer.peerId !== this.id) {
16525
- walks.push(walk(peer));
20624
+ for (const [peerId, peer] of Object.entries(this.#connections)) {
20625
+ if (peerId !== this.id) {
20626
+ walks.push(walk(peer, peerId));
16526
20627
  }
16527
20628
  }
16528
20629
  return Promise.all(walks);
@@ -16532,7 +20633,7 @@ class Peernet {
16532
20633
  *
16533
20634
  * @param {String} hash
16534
20635
  */
16535
- async providersFor(hash) {
20636
+ async providersFor(hash, store) {
16536
20637
  let providers = await this.dht.providersFor(hash);
16537
20638
  // walk the network to find a provider
16538
20639
  if (!providers || providers.length === 0) {
@@ -16595,23 +20696,25 @@ class Peernet {
16595
20696
  if (!closestPeer || !closestPeer.id)
16596
20697
  return this.requestData(hash, store?.name || store);
16597
20698
  const id = closestPeer.id;
16598
- if (this.connections) {
16599
- let closest = this.connections.filter((peer) => {
16600
- if (peer.peerId === id)
16601
- return peer;
16602
- });
20699
+ if (this.#connections[id]) {
20700
+ const peer = this.#connections[id];
16603
20701
  let data = await new globalThis.peernet.protos['peernet-data']({ hash, store: store?.name || store });
16604
20702
  const node = await this.prepareMessage(data);
16605
- if (closest[0])
16606
- data = await closest[0].request(node.encoded);
20703
+ if (peer)
20704
+ data = await peer.request(node.encoded);
16607
20705
  else {
16608
- closest = this.connections.filter((peer) => {
16609
- if (peer.peerId === id)
16610
- return peer;
16611
- });
16612
- if (closest[0])
16613
- data = await closest[0].request(node.encoded);
20706
+ // fallback and try every provider found
20707
+ const promises = [];
20708
+ const providers = await this.providersFor(hash, store);
20709
+ for (const provider of providers) {
20710
+ const peer = this.#connections[provider.id];
20711
+ if (peer)
20712
+ promises.push(peer.request(node.encoded));
20713
+ }
20714
+ data = await Promise.race(promises);
16614
20715
  }
20716
+ if (data)
20717
+ data = new Uint8Array(Object.values(data));
16615
20718
  const proto = await protoFor(data);
16616
20719
  // TODO: store data automaticly or not
16617
20720
  return BufferToUint8Array(proto.decoded.data);
@@ -16812,8 +20915,8 @@ class Peernet {
16812
20915
  // globalSub.publish(topic, data)
16813
20916
  const id = Math.random().toString(36).slice(-12);
16814
20917
  data = await new globalThis.peernet.protos['peernet-ps']({ data, topic });
16815
- for (const peer of this.connections) {
16816
- if (peer.peerId !== this.peerId) {
20918
+ for (const [peerId, peer] of Object.entries(this.#connections)) {
20919
+ if (peerId !== this.id) {
16817
20920
  const node = await this.prepareMessage(data);
16818
20921
  this.sendMessage(peer, id, node.encoded);
16819
20922
  }
@@ -16833,7 +20936,7 @@ class Peernet {
16833
20936
  globalSub.subscribe(topic, callback);
16834
20937
  }
16835
20938
  async removePeer(peer) {
16836
- return this.client.removePeer(peer);
20939
+ return this.client._removePeer(peer);
16837
20940
  }
16838
20941
  get Buffer() {
16839
20942
  return Buffer;
@@ -16878,4 +20981,4 @@ class Node {
16878
20981
  }
16879
20982
  }
16880
20983
 
16881
- export { FormatInterface as F, LittlePubSub as L, MultiWallet as M, Node as N, base58$1 as b, commonjsGlobal as c, encrypt as e, getDefaultExportFromCjs as g, inherits_browserExports as i, require$$3 as r };
20984
+ export { FormatInterface as F, MultiWallet as M, Node as N, base58$1 as b, commonjsGlobal as c, encrypt as e, getDefaultExportFromCjs as g };