@ivujs/i-utils 1.1.1 → 1.1.3

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 (140) hide show
  1. package/README.md +17 -19
  2. package/dist/cjs/id/index.cjs +26 -43
  3. package/dist/cjs/index.cjs +8 -398
  4. package/dist/es/id/index.d.ts +16 -0
  5. package/dist/es/id/index.mjs +28 -42
  6. package/dist/es/index.d.ts +2 -0
  7. package/dist/es/index.mjs +8 -58
  8. package/dist/index.d.ts +21 -0
  9. package/dist/lib/index.full.cjs.js +38 -0
  10. package/dist/lib/index.full.cjs.min.js +2 -0
  11. package/dist/lib/index.full.cjs.min.js.map +1 -0
  12. package/dist/lib/index.full.esm.js +35 -15822
  13. package/dist/lib/index.full.esm.min.js +2 -58
  14. package/dist/lib/index.full.esm.min.js.map +1 -0
  15. package/dist/lib/index.full.umd.js +39 -16176
  16. package/dist/lib/index.full.umd.min.js +2 -0
  17. package/dist/lib/index.full.umd.min.js.map +1 -0
  18. package/package.json +84 -54
  19. package/dist/cjs/ID-card/index.cjs +0 -122
  20. package/dist/cjs/array/index.cjs +0 -465
  21. package/dist/cjs/clipboard/index.cjs +0 -135
  22. package/dist/cjs/color/index.cjs +0 -210
  23. package/dist/cjs/constants/date.cjs +0 -186
  24. package/dist/cjs/constants/lang.cjs +0 -13
  25. package/dist/cjs/constants/math.cjs +0 -13
  26. package/dist/cjs/constants/regexp.cjs +0 -57
  27. package/dist/cjs/constants/sort.cjs +0 -15
  28. package/dist/cjs/cookie/index.cjs +0 -71
  29. package/dist/cjs/crypto/aes/aes.cjs +0 -533
  30. package/dist/cjs/crypto/aes/index.cjs +0 -28
  31. package/dist/cjs/crypto/base32/base32.cjs +0 -385
  32. package/dist/cjs/crypto/base32/index.cjs +0 -44
  33. package/dist/cjs/crypto/base64/base64.cjs +0 -433
  34. package/dist/cjs/crypto/base64/index.cjs +0 -64
  35. package/dist/cjs/crypto/des/des.cjs +0 -1054
  36. package/dist/cjs/crypto/des/index.cjs +0 -30
  37. package/dist/cjs/crypto/md5/index.cjs +0 -25
  38. package/dist/cjs/crypto/md5/md5.cjs +0 -944
  39. package/dist/cjs/crypto/sha/sha1/index.cjs +0 -25
  40. package/dist/cjs/crypto/sha/sha1/sha1.cjs +0 -602
  41. package/dist/cjs/crypto/sha/sha256/index.cjs +0 -46
  42. package/dist/cjs/crypto/sha/sha256/sha256.cjs +0 -654
  43. package/dist/cjs/crypto/sha/sha3/index.cjs +0 -44
  44. package/dist/cjs/crypto/sha/sha3/sha3.cjs +0 -747
  45. package/dist/cjs/crypto/sha/sha512/index.cjs +0 -88
  46. package/dist/cjs/crypto/sha/sha512/sha512.cjs +0 -1186
  47. package/dist/cjs/crypto/sm/lib/asn1.cjs +0 -167
  48. package/dist/cjs/crypto/sm/lib/ec.cjs +0 -352
  49. package/dist/cjs/crypto/sm/lib/jsbn.cjs +0 -1633
  50. package/dist/cjs/crypto/sm/lib/sm3.cjs +0 -173
  51. package/dist/cjs/crypto/sm/lib/utils.cjs +0 -198
  52. package/dist/cjs/crypto/sm/sm2/index.cjs +0 -122
  53. package/dist/cjs/crypto/sm/sm2/sm2.cjs +0 -269
  54. package/dist/cjs/crypto/sm/sm3/index.cjs +0 -15
  55. package/dist/cjs/crypto/sm/sm3/sm3.cjs +0 -103
  56. package/dist/cjs/crypto/sm/sm4/index.cjs +0 -28
  57. package/dist/cjs/crypto/sm/sm4/sm4.cjs +0 -362
  58. package/dist/cjs/crypto/tea/index.cjs +0 -26
  59. package/dist/cjs/crypto/tea/tea.cjs +0 -215
  60. package/dist/cjs/date/index.cjs +0 -1349
  61. package/dist/cjs/desensitized/index.cjs +0 -74
  62. package/dist/cjs/device/index.cjs +0 -159
  63. package/dist/cjs/dom/index.cjs +0 -176
  64. package/dist/cjs/file/index.cjs +0 -339
  65. package/dist/cjs/function/index.cjs +0 -67
  66. package/dist/cjs/keycode/index.cjs +0 -142
  67. package/dist/cjs/math/index.cjs +0 -284
  68. package/dist/cjs/number/index.cjs +0 -14
  69. package/dist/cjs/object/index.cjs +0 -216
  70. package/dist/cjs/pagination/index.cjs +0 -140
  71. package/dist/cjs/random/index.cjs +0 -25
  72. package/dist/cjs/regexp/index.cjs +0 -109
  73. package/dist/cjs/storage/index.cjs +0 -11
  74. package/dist/cjs/storage/localStorage.cjs +0 -40
  75. package/dist/cjs/storage/sessionStorage.cjs +0 -40
  76. package/dist/cjs/string/index.cjs +0 -338
  77. package/dist/cjs/url/index.cjs +0 -279
  78. package/dist/cjs/validate/index.cjs +0 -487
  79. package/dist/cjs/weapp/index.cjs +0 -151
  80. package/dist/es/ID-card/index.mjs +0 -116
  81. package/dist/es/array/index.mjs +0 -437
  82. package/dist/es/clipboard/index.mjs +0 -129
  83. package/dist/es/color/index.mjs +0 -200
  84. package/dist/es/constants/date.mjs +0 -184
  85. package/dist/es/constants/lang.mjs +0 -11
  86. package/dist/es/constants/math.mjs +0 -11
  87. package/dist/es/constants/regexp.mjs +0 -55
  88. package/dist/es/constants/sort.mjs +0 -13
  89. package/dist/es/cookie/index.mjs +0 -65
  90. package/dist/es/crypto/aes/aes.mjs +0 -529
  91. package/dist/es/crypto/aes/index.mjs +0 -25
  92. package/dist/es/crypto/base32/base32.mjs +0 -381
  93. package/dist/es/crypto/base32/index.mjs +0 -39
  94. package/dist/es/crypto/base64/base64.mjs +0 -427
  95. package/dist/es/crypto/base64/index.mjs +0 -57
  96. package/dist/es/crypto/des/des.mjs +0 -1050
  97. package/dist/es/crypto/des/index.mjs +0 -27
  98. package/dist/es/crypto/md5/index.mjs +0 -22
  99. package/dist/es/crypto/md5/md5.mjs +0 -941
  100. package/dist/es/crypto/sha/sha1/index.mjs +0 -22
  101. package/dist/es/crypto/sha/sha1/sha1.mjs +0 -599
  102. package/dist/es/crypto/sha/sha256/index.mjs +0 -41
  103. package/dist/es/crypto/sha/sha256/sha256.mjs +0 -649
  104. package/dist/es/crypto/sha/sha3/index.mjs +0 -39
  105. package/dist/es/crypto/sha/sha3/sha3.mjs +0 -742
  106. package/dist/es/crypto/sha/sha512/index.mjs +0 -79
  107. package/dist/es/crypto/sha/sha512/sha512.mjs +0 -1177
  108. package/dist/es/crypto/sm/lib/asn1.mjs +0 -164
  109. package/dist/es/crypto/sm/lib/ec.mjs +0 -349
  110. package/dist/es/crypto/sm/lib/jsbn.mjs +0 -1630
  111. package/dist/es/crypto/sm/lib/sm3.mjs +0 -170
  112. package/dist/es/crypto/sm/lib/utils.mjs +0 -186
  113. package/dist/es/crypto/sm/sm2/index.mjs +0 -111
  114. package/dist/es/crypto/sm/sm2/sm2.mjs +0 -258
  115. package/dist/es/crypto/sm/sm3/index.mjs +0 -13
  116. package/dist/es/crypto/sm/sm3/sm3.mjs +0 -99
  117. package/dist/es/crypto/sm/sm4/index.mjs +0 -25
  118. package/dist/es/crypto/sm/sm4/sm4.mjs +0 -359
  119. package/dist/es/crypto/tea/index.mjs +0 -23
  120. package/dist/es/crypto/tea/tea.mjs +0 -211
  121. package/dist/es/date/index.mjs +0 -1262
  122. package/dist/es/desensitized/index.mjs +0 -69
  123. package/dist/es/device/index.mjs +0 -145
  124. package/dist/es/dom/index.mjs +0 -164
  125. package/dist/es/file/index.mjs +0 -323
  126. package/dist/es/function/index.mjs +0 -63
  127. package/dist/es/keycode/index.mjs +0 -139
  128. package/dist/es/math/index.mjs +0 -274
  129. package/dist/es/number/index.mjs +0 -12
  130. package/dist/es/object/index.mjs +0 -203
  131. package/dist/es/pagination/index.mjs +0 -134
  132. package/dist/es/random/index.mjs +0 -22
  133. package/dist/es/regexp/index.mjs +0 -98
  134. package/dist/es/storage/index.mjs +0 -9
  135. package/dist/es/storage/localStorage.mjs +0 -35
  136. package/dist/es/storage/sessionStorage.mjs +0 -35
  137. package/dist/es/string/index.mjs +0 -321
  138. package/dist/es/url/index.mjs +0 -261
  139. package/dist/es/validate/index.mjs +0 -451
  140. package/dist/es/weapp/index.mjs +0 -140
@@ -1,1630 +0,0 @@
1
- // Copyright (c) 2005 Tom Wu
2
- // All Rights Reserved.
3
- // See "LICENSE" for details.
4
-
5
- // Basic JavaScript BN library - subset useful for RSA encryption.
6
-
7
- // Bits per digit
8
- var dbits;
9
-
10
- // JavaScript engine analysis
11
- var canary = 0xdeadbeefcafe;
12
- var j_lm = (canary & 0xffffff) == 0xefcafe;
13
-
14
- // (public) Constructor
15
- function BigInteger(a, b, c) {
16
- if (a != null)
17
- if ("number" == typeof a) this.fromNumber(a, b, c);
18
- else if (b == null && "string" != typeof a) this.fromString(a, 256);
19
- else this.fromString(a, b);
20
- }
21
-
22
- // return new, unset BigInteger
23
- function nbi() {
24
- return new BigInteger(null);
25
- }
26
-
27
- // am: Compute w_j += (x*this_i), propagate carries,
28
- // c is initial carry, returns final carry.
29
- // c < 3*dvalue, x < 2*dvalue, this_i < dvalue
30
- // We need to select the fastest one that works in this environment.
31
-
32
- // am1: use a single mult and divide to get the high bits,
33
- // max digit bits should be 26 because
34
- // max internal value = 2*dvalue^2-2*dvalue (< 2^53)
35
- function am1(i, x, w, j, c, n) {
36
- while (--n >= 0) {
37
- var v = x * this[i++] + w[j] + c;
38
- c = Math.floor(v / 0x4000000);
39
- w[j++] = v & 0x3ffffff;
40
- }
41
- return c;
42
- }
43
-
44
- // am2 avoids a big mult-and-extract completely.
45
- // Max digit bits should be <= 30 because we do bitwise ops
46
- // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
47
- function am2(i, x, w, j, c, n) {
48
- var xl = x & 0x7fff,
49
- xh = x >> 15;
50
- while (--n >= 0) {
51
- var l = this[i] & 0x7fff;
52
- var h = this[i++] >> 15;
53
- var m = xh * l + h * xl;
54
- l = xl * l + ((m & 0x7fff) << 15) + w[j] + (c & 0x3fffffff);
55
- c = (l >>> 30) + (m >>> 15) + xh * h + (c >>> 30);
56
- w[j++] = l & 0x3fffffff;
57
- }
58
- return c;
59
- }
60
-
61
- // Alternately, set max digit bits to 28 since some
62
- // browsers slow down when dealing with 32-bit numbers.
63
- function am3(i, x, w, j, c, n) {
64
- var xl = x & 0x3fff,
65
- xh = x >> 14;
66
- while (--n >= 0) {
67
- var l = this[i] & 0x3fff;
68
- var h = this[i++] >> 14;
69
- var m = xh * l + h * xl;
70
- l = xl * l + ((m & 0x3fff) << 14) + w[j] + c;
71
- c = (l >> 28) + (m >> 14) + xh * h;
72
- w[j++] = l & 0xfffffff;
73
- }
74
- return c;
75
- }
76
-
77
- var inBrowser = typeof navigator !== "undefined";
78
- if (inBrowser && j_lm && navigator.appName == "Microsoft Internet Explorer") {
79
- BigInteger.prototype.am = am2;
80
- dbits = 30;
81
- } else if (inBrowser && j_lm && navigator.appName != "Netscape") {
82
- BigInteger.prototype.am = am1;
83
- dbits = 26;
84
- } else {
85
- // Mozilla/Netscape seems to prefer am3
86
- BigInteger.prototype.am = am3;
87
- dbits = 28;
88
- }
89
-
90
- BigInteger.prototype.DB = dbits;
91
- BigInteger.prototype.DM = (1 << dbits) - 1;
92
- BigInteger.prototype.DV = 1 << dbits;
93
-
94
- var BI_FP = 52;
95
- BigInteger.prototype.FV = Math.pow(2, BI_FP);
96
- BigInteger.prototype.F1 = BI_FP - dbits;
97
- BigInteger.prototype.F2 = 2 * dbits - BI_FP;
98
-
99
- // Digit conversions
100
- var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
101
- var BI_RC = new Array();
102
- var rr, vv;
103
- rr = "0".charCodeAt(0);
104
- for (vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
105
- rr = "a".charCodeAt(0);
106
- for (vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
107
- rr = "A".charCodeAt(0);
108
- for (vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
109
-
110
- function int2char(n) {
111
- return BI_RM.charAt(n);
112
- }
113
-
114
- function intAt(s, i) {
115
- var c = BI_RC[s.charCodeAt(i)];
116
- return c == null ? -1 : c;
117
- }
118
-
119
- // (protected) copy this to r
120
- function bnpCopyTo(r) {
121
- for (var i = this.t - 1; i >= 0; --i) r[i] = this[i];
122
- r.t = this.t;
123
- r.s = this.s;
124
- }
125
-
126
- // (protected) set from integer value x, -DV <= x < DV
127
- function bnpFromInt(x) {
128
- this.t = 1;
129
- this.s = x < 0 ? -1 : 0;
130
- if (x > 0) this[0] = x;
131
- else if (x < -1) this[0] = x + this.DV;
132
- else this.t = 0;
133
- }
134
-
135
- // return bigint initialized to value
136
- function nbv(i) {
137
- var r = nbi();
138
- r.fromInt(i);
139
- return r;
140
- }
141
-
142
- // (protected) set from string and radix
143
- function bnpFromString(s, b) {
144
- var k;
145
- if (b == 16) k = 4;
146
- else if (b == 8) k = 3;
147
- else if (b == 256)
148
- k = 8; // byte array
149
- else if (b == 2) k = 1;
150
- else if (b == 32) k = 5;
151
- else if (b == 4) k = 2;
152
- else {
153
- this.fromRadix(s, b);
154
- return;
155
- }
156
- this.t = 0;
157
- this.s = 0;
158
- var i = s.length,
159
- mi = false,
160
- sh = 0;
161
- while (--i >= 0) {
162
- var x = k == 8 ? s[i] & 0xff : intAt(s, i);
163
- if (x < 0) {
164
- if (s.charAt(i) == "-") mi = true;
165
- continue;
166
- }
167
- mi = false;
168
- if (sh == 0) this[this.t++] = x;
169
- else if (sh + k > this.DB) {
170
- this[this.t - 1] |= (x & ((1 << (this.DB - sh)) - 1)) << sh;
171
- this[this.t++] = x >> (this.DB - sh);
172
- } else this[this.t - 1] |= x << sh;
173
- sh += k;
174
- if (sh >= this.DB) sh -= this.DB;
175
- }
176
- if (k == 8 && (s[0] & 0x80) != 0) {
177
- this.s = -1;
178
- if (sh > 0) this[this.t - 1] |= ((1 << (this.DB - sh)) - 1) << sh;
179
- }
180
- this.clamp();
181
- if (mi) BigInteger.ZERO.subTo(this, this);
182
- }
183
-
184
- // (protected) clamp off excess high words
185
- function bnpClamp() {
186
- var c = this.s & this.DM;
187
- while (this.t > 0 && this[this.t - 1] == c) --this.t;
188
- }
189
-
190
- // (public) return string representation in given radix
191
- function bnToString(b) {
192
- if (this.s < 0) return "-" + this.negate().toString(b);
193
- var k;
194
- if (b == 16) k = 4;
195
- else if (b == 8) k = 3;
196
- else if (b == 2) k = 1;
197
- else if (b == 32) k = 5;
198
- else if (b == 4) k = 2;
199
- else return this.toRadix(b);
200
- var km = (1 << k) - 1,
201
- d,
202
- m = false,
203
- r = "",
204
- i = this.t;
205
- var p = this.DB - ((i * this.DB) % k);
206
- if (i-- > 0) {
207
- if (p < this.DB && (d = this[i] >> p) > 0) {
208
- m = true;
209
- r = int2char(d);
210
- }
211
- while (i >= 0) {
212
- if (p < k) {
213
- d = (this[i] & ((1 << p) - 1)) << (k - p);
214
- d |= this[--i] >> (p += this.DB - k);
215
- } else {
216
- d = (this[i] >> (p -= k)) & km;
217
- if (p <= 0) {
218
- p += this.DB;
219
- --i;
220
- }
221
- }
222
- if (d > 0) m = true;
223
- if (m) r += int2char(d);
224
- }
225
- }
226
- return m ? r : "0";
227
- }
228
-
229
- // (public) -this
230
- function bnNegate() {
231
- var r = nbi();
232
- BigInteger.ZERO.subTo(this, r);
233
- return r;
234
- }
235
-
236
- // (public) |this|
237
- function bnAbs() {
238
- return this.s < 0 ? this.negate() : this;
239
- }
240
-
241
- // (public) return + if this > a, - if this < a, 0 if equal
242
- function bnCompareTo(a) {
243
- var r = this.s - a.s;
244
- if (r != 0) return r;
245
- var i = this.t;
246
- r = i - a.t;
247
- if (r != 0) return this.s < 0 ? -r : r;
248
- while (--i >= 0) if ((r = this[i] - a[i]) != 0) return r;
249
- return 0;
250
- }
251
-
252
- // returns bit length of the integer x
253
- function nbits(x) {
254
- var r = 1,
255
- t;
256
- if ((t = x >>> 16) != 0) {
257
- x = t;
258
- r += 16;
259
- }
260
- if ((t = x >> 8) != 0) {
261
- x = t;
262
- r += 8;
263
- }
264
- if ((t = x >> 4) != 0) {
265
- x = t;
266
- r += 4;
267
- }
268
- if ((t = x >> 2) != 0) {
269
- x = t;
270
- r += 2;
271
- }
272
- if ((t = x >> 1) != 0) {
273
- x = t;
274
- r += 1;
275
- }
276
- return r;
277
- }
278
-
279
- // (public) return the number of bits in "this"
280
- function bnBitLength() {
281
- if (this.t <= 0) return 0;
282
- return this.DB * (this.t - 1) + nbits(this[this.t - 1] ^ (this.s & this.DM));
283
- }
284
-
285
- // (protected) r = this << n*DB
286
- function bnpDLShiftTo(n, r) {
287
- var i;
288
- for (i = this.t - 1; i >= 0; --i) r[i + n] = this[i];
289
- for (i = n - 1; i >= 0; --i) r[i] = 0;
290
- r.t = this.t + n;
291
- r.s = this.s;
292
- }
293
-
294
- // (protected) r = this >> n*DB
295
- function bnpDRShiftTo(n, r) {
296
- for (var i = n; i < this.t; ++i) r[i - n] = this[i];
297
- r.t = Math.max(this.t - n, 0);
298
- r.s = this.s;
299
- }
300
-
301
- // (protected) r = this << n
302
- function bnpLShiftTo(n, r) {
303
- var bs = n % this.DB;
304
- var cbs = this.DB - bs;
305
- var bm = (1 << cbs) - 1;
306
- var ds = Math.floor(n / this.DB),
307
- c = (this.s << bs) & this.DM,
308
- i;
309
- for (i = this.t - 1; i >= 0; --i) {
310
- r[i + ds + 1] = (this[i] >> cbs) | c;
311
- c = (this[i] & bm) << bs;
312
- }
313
- for (i = ds - 1; i >= 0; --i) r[i] = 0;
314
- r[ds] = c;
315
- r.t = this.t + ds + 1;
316
- r.s = this.s;
317
- r.clamp();
318
- }
319
-
320
- // (protected) r = this >> n
321
- function bnpRShiftTo(n, r) {
322
- r.s = this.s;
323
- var ds = Math.floor(n / this.DB);
324
- if (ds >= this.t) {
325
- r.t = 0;
326
- return;
327
- }
328
- var bs = n % this.DB;
329
- var cbs = this.DB - bs;
330
- var bm = (1 << bs) - 1;
331
- r[0] = this[ds] >> bs;
332
- for (var i = ds + 1; i < this.t; ++i) {
333
- r[i - ds - 1] |= (this[i] & bm) << cbs;
334
- r[i - ds] = this[i] >> bs;
335
- }
336
- if (bs > 0) r[this.t - ds - 1] |= (this.s & bm) << cbs;
337
- r.t = this.t - ds;
338
- r.clamp();
339
- }
340
-
341
- // (protected) r = this - a
342
- function bnpSubTo(a, r) {
343
- var i = 0,
344
- c = 0,
345
- m = Math.min(a.t, this.t);
346
- while (i < m) {
347
- c += this[i] - a[i];
348
- r[i++] = c & this.DM;
349
- c >>= this.DB;
350
- }
351
- if (a.t < this.t) {
352
- c -= a.s;
353
- while (i < this.t) {
354
- c += this[i];
355
- r[i++] = c & this.DM;
356
- c >>= this.DB;
357
- }
358
- c += this.s;
359
- } else {
360
- c += this.s;
361
- while (i < a.t) {
362
- c -= a[i];
363
- r[i++] = c & this.DM;
364
- c >>= this.DB;
365
- }
366
- c -= a.s;
367
- }
368
- r.s = c < 0 ? -1 : 0;
369
- if (c < -1) r[i++] = this.DV + c;
370
- else if (c > 0) r[i++] = c;
371
- r.t = i;
372
- r.clamp();
373
- }
374
-
375
- // (protected) r = this * a, r != this,a (HAC 14.12)
376
- // "this" should be the larger one if appropriate.
377
- function bnpMultiplyTo(a, r) {
378
- var x = this.abs(),
379
- y = a.abs();
380
- var i = x.t;
381
- r.t = i + y.t;
382
- while (--i >= 0) r[i] = 0;
383
- for (i = 0; i < y.t; ++i) r[i + x.t] = x.am(0, y[i], r, i, 0, x.t);
384
- r.s = 0;
385
- r.clamp();
386
- if (this.s != a.s) BigInteger.ZERO.subTo(r, r);
387
- }
388
-
389
- // (protected) r = this^2, r != this (HAC 14.16)
390
- function bnpSquareTo(r) {
391
- var x = this.abs();
392
- var i = (r.t = 2 * x.t);
393
- while (--i >= 0) r[i] = 0;
394
- for (i = 0; i < x.t - 1; ++i) {
395
- var c = x.am(i, x[i], r, 2 * i, 0, 1);
396
- if ((r[i + x.t] += x.am(i + 1, 2 * x[i], r, 2 * i + 1, c, x.t - i - 1)) >= x.DV) {
397
- r[i + x.t] -= x.DV;
398
- r[i + x.t + 1] = 1;
399
- }
400
- }
401
- if (r.t > 0) r[r.t - 1] += x.am(i, x[i], r, 2 * i, 0, 1);
402
- r.s = 0;
403
- r.clamp();
404
- }
405
-
406
- // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
407
- // r != q, this != m. q or r may be null.
408
- function bnpDivRemTo(m, q, r) {
409
- var pm = m.abs();
410
- if (pm.t <= 0) return;
411
- var pt = this.abs();
412
- if (pt.t < pm.t) {
413
- if (q != null) q.fromInt(0);
414
- if (r != null) this.copyTo(r);
415
- return;
416
- }
417
- if (r == null) r = nbi();
418
- var y = nbi(),
419
- ts = this.s,
420
- ms = m.s;
421
- var nsh = this.DB - nbits(pm[pm.t - 1]); // normalize modulus
422
- if (nsh > 0) {
423
- pm.lShiftTo(nsh, y);
424
- pt.lShiftTo(nsh, r);
425
- } else {
426
- pm.copyTo(y);
427
- pt.copyTo(r);
428
- }
429
- var ys = y.t;
430
- var y0 = y[ys - 1];
431
- if (y0 == 0) return;
432
- var yt = y0 * (1 << this.F1) + (ys > 1 ? y[ys - 2] >> this.F2 : 0);
433
- var d1 = this.FV / yt,
434
- d2 = (1 << this.F1) / yt,
435
- e = 1 << this.F2;
436
- var i = r.t,
437
- j = i - ys,
438
- t = q == null ? nbi() : q;
439
- y.dlShiftTo(j, t);
440
- if (r.compareTo(t) >= 0) {
441
- r[r.t++] = 1;
442
- r.subTo(t, r);
443
- }
444
- BigInteger.ONE.dlShiftTo(ys, t);
445
- t.subTo(y, y); // "negative" y so we can replace sub with am later
446
- while (y.t < ys) y[y.t++] = 0;
447
- while (--j >= 0) {
448
- // Estimate quotient digit
449
- var qd = r[--i] == y0 ? this.DM : Math.floor(r[i] * d1 + (r[i - 1] + e) * d2);
450
- if ((r[i] += y.am(0, qd, r, j, 0, ys)) < qd) {
451
- // Try it out
452
- y.dlShiftTo(j, t);
453
- r.subTo(t, r);
454
- while (r[i] < --qd) r.subTo(t, r);
455
- }
456
- }
457
- if (q != null) {
458
- r.drShiftTo(ys, q);
459
- if (ts != ms) BigInteger.ZERO.subTo(q, q);
460
- }
461
- r.t = ys;
462
- r.clamp();
463
- if (nsh > 0) r.rShiftTo(nsh, r); // Denormalize remainder
464
- if (ts < 0) BigInteger.ZERO.subTo(r, r);
465
- }
466
-
467
- // (public) this mod a
468
- function bnMod(a) {
469
- var r = nbi();
470
- this.abs().divRemTo(a, null, r);
471
- if (this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r, r);
472
- return r;
473
- }
474
-
475
- // Modular reduction using "classic" algorithm
476
- function Classic(m) {
477
- this.m = m;
478
- }
479
-
480
- function cConvert(x) {
481
- if (x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
482
- else return x;
483
- }
484
-
485
- function cRevert(x) {
486
- return x;
487
- }
488
-
489
- function cReduce(x) {
490
- x.divRemTo(this.m, null, x);
491
- }
492
-
493
- function cMulTo(x, y, r) {
494
- x.multiplyTo(y, r);
495
- this.reduce(r);
496
- }
497
-
498
- function cSqrTo(x, r) {
499
- x.squareTo(r);
500
- this.reduce(r);
501
- }
502
-
503
- Classic.prototype.convert = cConvert;
504
- Classic.prototype.revert = cRevert;
505
- Classic.prototype.reduce = cReduce;
506
- Classic.prototype.mulTo = cMulTo;
507
- Classic.prototype.sqrTo = cSqrTo;
508
-
509
- // (protected) return "-1/this % 2^DB"; useful for Mont. reduction
510
- // justification:
511
- // xy == 1 (mod m)
512
- // xy = 1+km
513
- // xy(2-xy) = (1+km)(1-km)
514
- // x[y(2-xy)] = 1-k^2m^2
515
- // x[y(2-xy)] == 1 (mod m^2)
516
- // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
517
- // should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
518
- // JS multiply "overflows" differently from C/C++, so care is needed here.
519
- function bnpInvDigit() {
520
- if (this.t < 1) return 0;
521
- var x = this[0];
522
- if ((x & 1) == 0) return 0;
523
- var y = x & 3; // y == 1/x mod 2^2
524
- y = (y * (2 - (x & 0xf) * y)) & 0xf; // y == 1/x mod 2^4
525
- y = (y * (2 - (x & 0xff) * y)) & 0xff; // y == 1/x mod 2^8
526
- y = (y * (2 - (((x & 0xffff) * y) & 0xffff))) & 0xffff; // y == 1/x mod 2^16
527
- // last step - calculate inverse mod DV directly;
528
- // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
529
- y = (y * (2 - ((x * y) % this.DV))) % this.DV; // y == 1/x mod 2^dbits
530
- // we really want the negative inverse, and -DV < y < DV
531
- return y > 0 ? this.DV - y : -y;
532
- }
533
-
534
- // Montgomery reduction
535
- function Montgomery(m) {
536
- this.m = m;
537
- this.mp = m.invDigit();
538
- this.mpl = this.mp & 0x7fff;
539
- this.mph = this.mp >> 15;
540
- this.um = (1 << (m.DB - 15)) - 1;
541
- this.mt2 = 2 * m.t;
542
- }
543
-
544
- // xR mod m
545
- function montConvert(x) {
546
- var r = nbi();
547
- x.abs().dlShiftTo(this.m.t, r);
548
- r.divRemTo(this.m, null, r);
549
- if (x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r, r);
550
- return r;
551
- }
552
-
553
- // x/R mod m
554
- function montRevert(x) {
555
- var r = nbi();
556
- x.copyTo(r);
557
- this.reduce(r);
558
- return r;
559
- }
560
-
561
- // x = x/R mod m (HAC 14.32)
562
- function montReduce(x) {
563
- while (x.t <= this.mt2)
564
- // pad x so am has enough room later
565
- x[x.t++] = 0;
566
- for (var i = 0; i < this.m.t; ++i) {
567
- // faster way of calculating u0 = x[i]*mp mod DV
568
- var j = x[i] & 0x7fff;
569
- var u0 = (j * this.mpl + (((j * this.mph + (x[i] >> 15) * this.mpl) & this.um) << 15)) & x.DM;
570
- // use am to combine the multiply-shift-add into one call
571
- j = i + this.m.t;
572
- x[j] += this.m.am(0, u0, x, i, 0, this.m.t);
573
- // propagate carry
574
- while (x[j] >= x.DV) {
575
- x[j] -= x.DV;
576
- x[++j]++;
577
- }
578
- }
579
- x.clamp();
580
- x.drShiftTo(this.m.t, x);
581
- if (x.compareTo(this.m) >= 0) x.subTo(this.m, x);
582
- }
583
-
584
- // r = "x^2/R mod m"; x != r
585
- function montSqrTo(x, r) {
586
- x.squareTo(r);
587
- this.reduce(r);
588
- }
589
-
590
- // r = "xy/R mod m"; x,y != r
591
- function montMulTo(x, y, r) {
592
- x.multiplyTo(y, r);
593
- this.reduce(r);
594
- }
595
-
596
- Montgomery.prototype.convert = montConvert;
597
- Montgomery.prototype.revert = montRevert;
598
- Montgomery.prototype.reduce = montReduce;
599
- Montgomery.prototype.mulTo = montMulTo;
600
- Montgomery.prototype.sqrTo = montSqrTo;
601
-
602
- // (protected) true iff this is even
603
- function bnpIsEven() {
604
- return (this.t > 0 ? this[0] & 1 : this.s) == 0;
605
- }
606
-
607
- // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
608
- function bnpExp(e, z) {
609
- if (e > 0xffffffff || e < 1) return BigInteger.ONE;
610
- var r = nbi(),
611
- r2 = nbi(),
612
- g = z.convert(this),
613
- i = nbits(e) - 1;
614
- g.copyTo(r);
615
- while (--i >= 0) {
616
- z.sqrTo(r, r2);
617
- if ((e & (1 << i)) > 0) z.mulTo(r2, g, r);
618
- else {
619
- var t = r;
620
- r = r2;
621
- r2 = t;
622
- }
623
- }
624
- return z.revert(r);
625
- }
626
-
627
- // (public) this^e % m, 0 <= e < 2^32
628
- function bnModPowInt(e, m) {
629
- var z;
630
- if (e < 256 || m.isEven()) z = new Classic(m);
631
- else z = new Montgomery(m);
632
- return this.exp(e, z);
633
- }
634
-
635
- // protected
636
- BigInteger.prototype.copyTo = bnpCopyTo;
637
- BigInteger.prototype.fromInt = bnpFromInt;
638
- BigInteger.prototype.fromString = bnpFromString;
639
- BigInteger.prototype.clamp = bnpClamp;
640
- BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
641
- BigInteger.prototype.drShiftTo = bnpDRShiftTo;
642
- BigInteger.prototype.lShiftTo = bnpLShiftTo;
643
- BigInteger.prototype.rShiftTo = bnpRShiftTo;
644
- BigInteger.prototype.subTo = bnpSubTo;
645
- BigInteger.prototype.multiplyTo = bnpMultiplyTo;
646
- BigInteger.prototype.squareTo = bnpSquareTo;
647
- BigInteger.prototype.divRemTo = bnpDivRemTo;
648
- BigInteger.prototype.invDigit = bnpInvDigit;
649
- BigInteger.prototype.isEven = bnpIsEven;
650
- BigInteger.prototype.exp = bnpExp;
651
-
652
- // public
653
- BigInteger.prototype.toString = bnToString;
654
- BigInteger.prototype.negate = bnNegate;
655
- BigInteger.prototype.abs = bnAbs;
656
- BigInteger.prototype.compareTo = bnCompareTo;
657
- BigInteger.prototype.bitLength = bnBitLength;
658
- BigInteger.prototype.mod = bnMod;
659
- BigInteger.prototype.modPowInt = bnModPowInt;
660
-
661
- // "constants"
662
- BigInteger.ZERO = nbv(0);
663
- BigInteger.ONE = nbv(1);
664
-
665
- // Copyright (c) 2005-2009 Tom Wu
666
- // All Rights Reserved.
667
- // See "LICENSE" for details.
668
-
669
- // Extended JavaScript BN functions, required for RSA private ops.
670
-
671
- // Version 1.1: new BigInteger("0", 10) returns "proper" zero
672
- // Version 1.2: square() API, isProbablePrime fix
673
-
674
- // (public)
675
- function bnClone() {
676
- var r = nbi();
677
- this.copyTo(r);
678
- return r;
679
- }
680
-
681
- // (public) return value as integer
682
- function bnIntValue() {
683
- if (this.s < 0) {
684
- if (this.t == 1) return this[0] - this.DV;
685
- else if (this.t == 0) return -1;
686
- } else if (this.t == 1) return this[0];
687
- else if (this.t == 0) return 0;
688
- // assumes 16 < DB < 32
689
- return ((this[1] & ((1 << (32 - this.DB)) - 1)) << this.DB) | this[0];
690
- }
691
-
692
- // (public) return value as byte
693
- function bnByteValue() {
694
- return this.t == 0 ? this.s : (this[0] << 24) >> 24;
695
- }
696
-
697
- // (public) return value as short (assumes DB>=16)
698
- function bnShortValue() {
699
- return this.t == 0 ? this.s : (this[0] << 16) >> 16;
700
- }
701
-
702
- // (protected) return x s.t. r^x < DV
703
- function bnpChunkSize(r) {
704
- return Math.floor((Math.LN2 * this.DB) / Math.log(r));
705
- }
706
-
707
- // (public) 0 if this == 0, 1 if this > 0
708
- function bnSigNum() {
709
- if (this.s < 0) return -1;
710
- else if (this.t <= 0 || (this.t == 1 && this[0] <= 0)) return 0;
711
- else return 1;
712
- }
713
-
714
- // (protected) convert to radix string
715
- function bnpToRadix(b) {
716
- if (b == null) b = 10;
717
- if (this.signum() == 0 || b < 2 || b > 36) return "0";
718
- var cs = this.chunkSize(b);
719
- var a = Math.pow(b, cs);
720
- var d = nbv(a),
721
- y = nbi(),
722
- z = nbi(),
723
- r = "";
724
- this.divRemTo(d, y, z);
725
- while (y.signum() > 0) {
726
- r = (a + z.intValue()).toString(b).substr(1) + r;
727
- y.divRemTo(d, y, z);
728
- }
729
- return z.intValue().toString(b) + r;
730
- }
731
-
732
- // (protected) convert from radix string
733
- function bnpFromRadix(s, b) {
734
- this.fromInt(0);
735
- if (b == null) b = 10;
736
- var cs = this.chunkSize(b);
737
- var d = Math.pow(b, cs),
738
- mi = false,
739
- j = 0,
740
- w = 0;
741
- for (var i = 0; i < s.length; ++i) {
742
- var x = intAt(s, i);
743
- if (x < 0) {
744
- if (s.charAt(i) == "-" && this.signum() == 0) mi = true;
745
- continue;
746
- }
747
- w = b * w + x;
748
- if (++j >= cs) {
749
- this.dMultiply(d);
750
- this.dAddOffset(w, 0);
751
- j = 0;
752
- w = 0;
753
- }
754
- }
755
- if (j > 0) {
756
- this.dMultiply(Math.pow(b, j));
757
- this.dAddOffset(w, 0);
758
- }
759
- if (mi) BigInteger.ZERO.subTo(this, this);
760
- }
761
-
762
- // (protected) alternate constructor
763
- function bnpFromNumber(a, b, c) {
764
- if ("number" == typeof b) {
765
- // new BigInteger(int,int,RNG)
766
- if (a < 2) this.fromInt(1);
767
- else {
768
- this.fromNumber(a, c);
769
- if (!this.testBit(a - 1))
770
- // force MSB set
771
- this.bitwiseTo(BigInteger.ONE.shiftLeft(a - 1), op_or, this);
772
- if (this.isEven()) this.dAddOffset(1, 0); // force odd
773
- while (!this.isProbablePrime(b)) {
774
- this.dAddOffset(2, 0);
775
- if (this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a - 1), this);
776
- }
777
- }
778
- } else {
779
- // new BigInteger(int,RNG)
780
- var x = new Array(),
781
- t = a & 7;
782
- x.length = (a >> 3) + 1;
783
- b.nextBytes(x);
784
- if (t > 0) x[0] &= (1 << t) - 1;
785
- else x[0] = 0;
786
- this.fromString(x, 256);
787
- }
788
- }
789
-
790
- // (public) convert to bigendian byte array
791
- function bnToByteArray() {
792
- var i = this.t,
793
- r = new Array();
794
- r[0] = this.s;
795
- var p = this.DB - ((i * this.DB) % 8),
796
- d,
797
- k = 0;
798
- if (i-- > 0) {
799
- if (p < this.DB && (d = this[i] >> p) != (this.s & this.DM) >> p) r[k++] = d | (this.s << (this.DB - p));
800
- while (i >= 0) {
801
- if (p < 8) {
802
- d = (this[i] & ((1 << p) - 1)) << (8 - p);
803
- d |= this[--i] >> (p += this.DB - 8);
804
- } else {
805
- d = (this[i] >> (p -= 8)) & 0xff;
806
- if (p <= 0) {
807
- p += this.DB;
808
- --i;
809
- }
810
- }
811
- if ((d & 0x80) != 0) d |= -256;
812
- if (k == 0 && (this.s & 0x80) != (d & 0x80)) ++k;
813
- if (k > 0 || d != this.s) r[k++] = d;
814
- }
815
- }
816
- return r;
817
- }
818
-
819
- function bnEquals(a) {
820
- return this.compareTo(a) == 0;
821
- }
822
-
823
- function bnMin(a) {
824
- return this.compareTo(a) < 0 ? this : a;
825
- }
826
-
827
- function bnMax(a) {
828
- return this.compareTo(a) > 0 ? this : a;
829
- }
830
-
831
- // (protected) r = this op a (bitwise)
832
- function bnpBitwiseTo(a, op, r) {
833
- var i,
834
- f,
835
- m = Math.min(a.t, this.t);
836
- for (i = 0; i < m; ++i) r[i] = op(this[i], a[i]);
837
- if (a.t < this.t) {
838
- f = a.s & this.DM;
839
- for (i = m; i < this.t; ++i) r[i] = op(this[i], f);
840
- r.t = this.t;
841
- } else {
842
- f = this.s & this.DM;
843
- for (i = m; i < a.t; ++i) r[i] = op(f, a[i]);
844
- r.t = a.t;
845
- }
846
- r.s = op(this.s, a.s);
847
- r.clamp();
848
- }
849
-
850
- // (public) this & a
851
- function op_and(x, y) {
852
- return x & y;
853
- }
854
-
855
- function bnAnd(a) {
856
- var r = nbi();
857
- this.bitwiseTo(a, op_and, r);
858
- return r;
859
- }
860
-
861
- // (public) this | a
862
- function op_or(x, y) {
863
- return x | y;
864
- }
865
-
866
- function bnOr(a) {
867
- var r = nbi();
868
- this.bitwiseTo(a, op_or, r);
869
- return r;
870
- }
871
-
872
- // (public) this ^ a
873
- function op_xor(x, y) {
874
- return x ^ y;
875
- }
876
-
877
- function bnXor(a) {
878
- var r = nbi();
879
- this.bitwiseTo(a, op_xor, r);
880
- return r;
881
- }
882
-
883
- // (public) this & ~a
884
- function op_andnot(x, y) {
885
- return x & ~y;
886
- }
887
-
888
- function bnAndNot(a) {
889
- var r = nbi();
890
- this.bitwiseTo(a, op_andnot, r);
891
- return r;
892
- }
893
-
894
- // (public) ~this
895
- function bnNot() {
896
- var r = nbi();
897
- for (var i = 0; i < this.t; ++i) r[i] = this.DM & ~this[i];
898
- r.t = this.t;
899
- r.s = ~this.s;
900
- return r;
901
- }
902
-
903
- // (public) this << n
904
- function bnShiftLeft(n) {
905
- var r = nbi();
906
- if (n < 0) this.rShiftTo(-n, r);
907
- else this.lShiftTo(n, r);
908
- return r;
909
- }
910
-
911
- // (public) this >> n
912
- function bnShiftRight(n) {
913
- var r = nbi();
914
- if (n < 0) this.lShiftTo(-n, r);
915
- else this.rShiftTo(n, r);
916
- return r;
917
- }
918
-
919
- // return index of lowest 1-bit in x, x < 2^31
920
- function lbit(x) {
921
- if (x == 0) return -1;
922
- var r = 0;
923
- if ((x & 0xffff) == 0) {
924
- x >>= 16;
925
- r += 16;
926
- }
927
- if ((x & 0xff) == 0) {
928
- x >>= 8;
929
- r += 8;
930
- }
931
- if ((x & 0xf) == 0) {
932
- x >>= 4;
933
- r += 4;
934
- }
935
- if ((x & 3) == 0) {
936
- x >>= 2;
937
- r += 2;
938
- }
939
- if ((x & 1) == 0) ++r;
940
- return r;
941
- }
942
-
943
- // (public) returns index of lowest 1-bit (or -1 if none)
944
- function bnGetLowestSetBit() {
945
- for (var i = 0; i < this.t; ++i) if (this[i] != 0) return i * this.DB + lbit(this[i]);
946
- if (this.s < 0) return this.t * this.DB;
947
- return -1;
948
- }
949
-
950
- // return number of 1 bits in x
951
- function cbit(x) {
952
- var r = 0;
953
- while (x != 0) {
954
- x &= x - 1;
955
- ++r;
956
- }
957
- return r;
958
- }
959
-
960
- // (public) return number of set bits
961
- function bnBitCount() {
962
- var r = 0,
963
- x = this.s & this.DM;
964
- for (var i = 0; i < this.t; ++i) r += cbit(this[i] ^ x);
965
- return r;
966
- }
967
-
968
- // (public) true iff nth bit is set
969
- function bnTestBit(n) {
970
- var j = Math.floor(n / this.DB);
971
- if (j >= this.t) return this.s != 0;
972
- return (this[j] & (1 << n % this.DB)) != 0;
973
- }
974
-
975
- // (protected) this op (1<<n)
976
- function bnpChangeBit(n, op) {
977
- var r = BigInteger.ONE.shiftLeft(n);
978
- this.bitwiseTo(r, op, r);
979
- return r;
980
- }
981
-
982
- // (public) this | (1<<n)
983
- function bnSetBit(n) {
984
- return this.changeBit(n, op_or);
985
- }
986
-
987
- // (public) this & ~(1<<n)
988
- function bnClearBit(n) {
989
- return this.changeBit(n, op_andnot);
990
- }
991
-
992
- // (public) this ^ (1<<n)
993
- function bnFlipBit(n) {
994
- return this.changeBit(n, op_xor);
995
- }
996
-
997
- // (protected) r = this + a
998
- function bnpAddTo(a, r) {
999
- var i = 0,
1000
- c = 0,
1001
- m = Math.min(a.t, this.t);
1002
- while (i < m) {
1003
- c += this[i] + a[i];
1004
- r[i++] = c & this.DM;
1005
- c >>= this.DB;
1006
- }
1007
- if (a.t < this.t) {
1008
- c += a.s;
1009
- while (i < this.t) {
1010
- c += this[i];
1011
- r[i++] = c & this.DM;
1012
- c >>= this.DB;
1013
- }
1014
- c += this.s;
1015
- } else {
1016
- c += this.s;
1017
- while (i < a.t) {
1018
- c += a[i];
1019
- r[i++] = c & this.DM;
1020
- c >>= this.DB;
1021
- }
1022
- c += a.s;
1023
- }
1024
- r.s = c < 0 ? -1 : 0;
1025
- if (c > 0) r[i++] = c;
1026
- else if (c < -1) r[i++] = this.DV + c;
1027
- r.t = i;
1028
- r.clamp();
1029
- }
1030
-
1031
- // (public) this + a
1032
- function bnAdd(a) {
1033
- var r = nbi();
1034
- this.addTo(a, r);
1035
- return r;
1036
- }
1037
-
1038
- // (public) this - a
1039
- function bnSubtract(a) {
1040
- var r = nbi();
1041
- this.subTo(a, r);
1042
- return r;
1043
- }
1044
-
1045
- // (public) this * a
1046
- function bnMultiply(a) {
1047
- var r = nbi();
1048
- this.multiplyTo(a, r);
1049
- return r;
1050
- }
1051
-
1052
- // (public) this^2
1053
- function bnSquare() {
1054
- var r = nbi();
1055
- this.squareTo(r);
1056
- return r;
1057
- }
1058
-
1059
- // (public) this / a
1060
- function bnDivide(a) {
1061
- var r = nbi();
1062
- this.divRemTo(a, r, null);
1063
- return r;
1064
- }
1065
-
1066
- // (public) this % a
1067
- function bnRemainder(a) {
1068
- var r = nbi();
1069
- this.divRemTo(a, null, r);
1070
- return r;
1071
- }
1072
-
1073
- // (public) [this/a,this%a]
1074
- function bnDivideAndRemainder(a) {
1075
- var q = nbi(),
1076
- r = nbi();
1077
- this.divRemTo(a, q, r);
1078
- return new Array(q, r);
1079
- }
1080
-
1081
- // (protected) this *= n, this >= 0, 1 < n < DV
1082
- function bnpDMultiply(n) {
1083
- this[this.t] = this.am(0, n - 1, this, 0, 0, this.t);
1084
- ++this.t;
1085
- this.clamp();
1086
- }
1087
-
1088
- // (protected) this += n << w words, this >= 0
1089
- function bnpDAddOffset(n, w) {
1090
- if (n == 0) return;
1091
- while (this.t <= w) this[this.t++] = 0;
1092
- this[w] += n;
1093
- while (this[w] >= this.DV) {
1094
- this[w] -= this.DV;
1095
- if (++w >= this.t) this[this.t++] = 0;
1096
- ++this[w];
1097
- }
1098
- }
1099
-
1100
- // A "null" reducer
1101
- function NullExp() {}
1102
-
1103
- function nNop(x) {
1104
- return x;
1105
- }
1106
-
1107
- function nMulTo(x, y, r) {
1108
- x.multiplyTo(y, r);
1109
- }
1110
-
1111
- function nSqrTo(x, r) {
1112
- x.squareTo(r);
1113
- }
1114
-
1115
- NullExp.prototype.convert = nNop;
1116
- NullExp.prototype.revert = nNop;
1117
- NullExp.prototype.mulTo = nMulTo;
1118
- NullExp.prototype.sqrTo = nSqrTo;
1119
-
1120
- // (public) this^e
1121
- function bnPow(e) {
1122
- return this.exp(e, new NullExp());
1123
- }
1124
-
1125
- // (protected) r = lower n words of "this * a", a.t <= n
1126
- // "this" should be the larger one if appropriate.
1127
- function bnpMultiplyLowerTo(a, n, r) {
1128
- var i = Math.min(this.t + a.t, n);
1129
- r.s = 0; // assumes a,this >= 0
1130
- r.t = i;
1131
- while (i > 0) r[--i] = 0;
1132
- var j;
1133
- for (j = r.t - this.t; i < j; ++i) r[i + this.t] = this.am(0, a[i], r, i, 0, this.t);
1134
- for (j = Math.min(a.t, n); i < j; ++i) this.am(0, a[i], r, i, 0, n - i);
1135
- r.clamp();
1136
- }
1137
-
1138
- // (protected) r = "this * a" without lower n words, n > 0
1139
- // "this" should be the larger one if appropriate.
1140
- function bnpMultiplyUpperTo(a, n, r) {
1141
- --n;
1142
- var i = (r.t = this.t + a.t - n);
1143
- r.s = 0; // assumes a,this >= 0
1144
- while (--i >= 0) r[i] = 0;
1145
- for (i = Math.max(n - this.t, 0); i < a.t; ++i) r[this.t + i - n] = this.am(n - i, a[i], r, 0, 0, this.t + i - n);
1146
- r.clamp();
1147
- r.drShiftTo(1, r);
1148
- }
1149
-
1150
- // Barrett modular reduction
1151
- function Barrett(m) {
1152
- // setup Barrett
1153
- this.r2 = nbi();
1154
- this.q3 = nbi();
1155
- BigInteger.ONE.dlShiftTo(2 * m.t, this.r2);
1156
- this.mu = this.r2.divide(m);
1157
- this.m = m;
1158
- }
1159
-
1160
- function barrettConvert(x) {
1161
- if (x.s < 0 || x.t > 2 * this.m.t) return x.mod(this.m);
1162
- else if (x.compareTo(this.m) < 0) return x;
1163
- else {
1164
- var r = nbi();
1165
- x.copyTo(r);
1166
- this.reduce(r);
1167
- return r;
1168
- }
1169
- }
1170
-
1171
- function barrettRevert(x) {
1172
- return x;
1173
- }
1174
-
1175
- // x = x mod m (HAC 14.42)
1176
- function barrettReduce(x) {
1177
- x.drShiftTo(this.m.t - 1, this.r2);
1178
- if (x.t > this.m.t + 1) {
1179
- x.t = this.m.t + 1;
1180
- x.clamp();
1181
- }
1182
- this.mu.multiplyUpperTo(this.r2, this.m.t + 1, this.q3);
1183
- this.m.multiplyLowerTo(this.q3, this.m.t + 1, this.r2);
1184
- while (x.compareTo(this.r2) < 0) x.dAddOffset(1, this.m.t + 1);
1185
- x.subTo(this.r2, x);
1186
- while (x.compareTo(this.m) >= 0) x.subTo(this.m, x);
1187
- }
1188
-
1189
- // r = x^2 mod m; x != r
1190
- function barrettSqrTo(x, r) {
1191
- x.squareTo(r);
1192
- this.reduce(r);
1193
- }
1194
-
1195
- // r = x*y mod m; x,y != r
1196
- function barrettMulTo(x, y, r) {
1197
- x.multiplyTo(y, r);
1198
- this.reduce(r);
1199
- }
1200
-
1201
- Barrett.prototype.convert = barrettConvert;
1202
- Barrett.prototype.revert = barrettRevert;
1203
- Barrett.prototype.reduce = barrettReduce;
1204
- Barrett.prototype.mulTo = barrettMulTo;
1205
- Barrett.prototype.sqrTo = barrettSqrTo;
1206
-
1207
- // (public) this^e % m (HAC 14.85)
1208
- function bnModPow(e, m) {
1209
- var i = e.bitLength(),
1210
- k,
1211
- r = nbv(1),
1212
- z;
1213
- if (i <= 0) return r;
1214
- else if (i < 18) k = 1;
1215
- else if (i < 48) k = 3;
1216
- else if (i < 144) k = 4;
1217
- else if (i < 768) k = 5;
1218
- else k = 6;
1219
- if (i < 8) z = new Classic(m);
1220
- else if (m.isEven()) z = new Barrett(m);
1221
- else z = new Montgomery(m);
1222
-
1223
- // precomputation
1224
- var g = new Array(),
1225
- n = 3,
1226
- k1 = k - 1,
1227
- km = (1 << k) - 1;
1228
- g[1] = z.convert(this);
1229
- if (k > 1) {
1230
- var g2 = nbi();
1231
- z.sqrTo(g[1], g2);
1232
- while (n <= km) {
1233
- g[n] = nbi();
1234
- z.mulTo(g2, g[n - 2], g[n]);
1235
- n += 2;
1236
- }
1237
- }
1238
-
1239
- var j = e.t - 1,
1240
- w,
1241
- is1 = true,
1242
- r2 = nbi(),
1243
- t;
1244
- i = nbits(e[j]) - 1;
1245
- while (j >= 0) {
1246
- if (i >= k1) w = (e[j] >> (i - k1)) & km;
1247
- else {
1248
- w = (e[j] & ((1 << (i + 1)) - 1)) << (k1 - i);
1249
- if (j > 0) w |= e[j - 1] >> (this.DB + i - k1);
1250
- }
1251
-
1252
- n = k;
1253
- while ((w & 1) == 0) {
1254
- w >>= 1;
1255
- --n;
1256
- }
1257
- if ((i -= n) < 0) {
1258
- i += this.DB;
1259
- --j;
1260
- }
1261
- if (is1) {
1262
- // ret == 1, don't bother squaring or multiplying it
1263
- g[w].copyTo(r);
1264
- is1 = false;
1265
- } else {
1266
- while (n > 1) {
1267
- z.sqrTo(r, r2);
1268
- z.sqrTo(r2, r);
1269
- n -= 2;
1270
- }
1271
- if (n > 0) z.sqrTo(r, r2);
1272
- else {
1273
- t = r;
1274
- r = r2;
1275
- r2 = t;
1276
- }
1277
- z.mulTo(r2, g[w], r);
1278
- }
1279
-
1280
- while (j >= 0 && (e[j] & (1 << i)) == 0) {
1281
- z.sqrTo(r, r2);
1282
- t = r;
1283
- r = r2;
1284
- r2 = t;
1285
- if (--i < 0) {
1286
- i = this.DB - 1;
1287
- --j;
1288
- }
1289
- }
1290
- }
1291
- return z.revert(r);
1292
- }
1293
-
1294
- // (public) gcd(this,a) (HAC 14.54)
1295
- function bnGCD(a) {
1296
- var x = this.s < 0 ? this.negate() : this.clone();
1297
- var y = a.s < 0 ? a.negate() : a.clone();
1298
- if (x.compareTo(y) < 0) {
1299
- var t = x;
1300
- x = y;
1301
- y = t;
1302
- }
1303
- var i = x.getLowestSetBit(),
1304
- g = y.getLowestSetBit();
1305
- if (g < 0) return x;
1306
- if (i < g) g = i;
1307
- if (g > 0) {
1308
- x.rShiftTo(g, x);
1309
- y.rShiftTo(g, y);
1310
- }
1311
- while (x.signum() > 0) {
1312
- if ((i = x.getLowestSetBit()) > 0) x.rShiftTo(i, x);
1313
- if ((i = y.getLowestSetBit()) > 0) y.rShiftTo(i, y);
1314
- if (x.compareTo(y) >= 0) {
1315
- x.subTo(y, x);
1316
- x.rShiftTo(1, x);
1317
- } else {
1318
- y.subTo(x, y);
1319
- y.rShiftTo(1, y);
1320
- }
1321
- }
1322
- if (g > 0) y.lShiftTo(g, y);
1323
- return y;
1324
- }
1325
-
1326
- // (protected) this % n, n < 2^26
1327
- function bnpModInt(n) {
1328
- if (n <= 0) return 0;
1329
- var d = this.DV % n,
1330
- r = this.s < 0 ? n - 1 : 0;
1331
- if (this.t > 0)
1332
- if (d == 0) r = this[0] % n;
1333
- else for (var i = this.t - 1; i >= 0; --i) r = (d * r + this[i]) % n;
1334
- return r;
1335
- }
1336
-
1337
- // (public) 1/this % m (HAC 14.61)
1338
- function bnModInverse(m) {
1339
- var ac = m.isEven();
1340
- if ((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO;
1341
- var u = m.clone(),
1342
- v = this.clone();
1343
- var a = nbv(1),
1344
- b = nbv(0),
1345
- c = nbv(0),
1346
- d = nbv(1);
1347
- while (u.signum() != 0) {
1348
- while (u.isEven()) {
1349
- u.rShiftTo(1, u);
1350
- if (ac) {
1351
- if (!a.isEven() || !b.isEven()) {
1352
- a.addTo(this, a);
1353
- b.subTo(m, b);
1354
- }
1355
- a.rShiftTo(1, a);
1356
- } else if (!b.isEven()) b.subTo(m, b);
1357
- b.rShiftTo(1, b);
1358
- }
1359
- while (v.isEven()) {
1360
- v.rShiftTo(1, v);
1361
- if (ac) {
1362
- if (!c.isEven() || !d.isEven()) {
1363
- c.addTo(this, c);
1364
- d.subTo(m, d);
1365
- }
1366
- c.rShiftTo(1, c);
1367
- } else if (!d.isEven()) d.subTo(m, d);
1368
- d.rShiftTo(1, d);
1369
- }
1370
- if (u.compareTo(v) >= 0) {
1371
- u.subTo(v, u);
1372
- if (ac) a.subTo(c, a);
1373
- b.subTo(d, b);
1374
- } else {
1375
- v.subTo(u, v);
1376
- if (ac) c.subTo(a, c);
1377
- d.subTo(b, d);
1378
- }
1379
- }
1380
- if (v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
1381
- if (d.compareTo(m) >= 0) return d.subtract(m);
1382
- if (d.signum() < 0) d.addTo(m, d);
1383
- else return d;
1384
- if (d.signum() < 0) return d.add(m);
1385
- else return d;
1386
- }
1387
-
1388
- var lowprimes = [
1389
- 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109,
1390
- 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239,
1391
- 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379,
1392
- 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521,
1393
- 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661,
1394
- 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827,
1395
- 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991,
1396
- 997,
1397
- ];
1398
- var lplim = (1 << 26) / lowprimes[lowprimes.length - 1];
1399
-
1400
- // (public) test primality with certainty >= 1-.5^t
1401
- function bnIsProbablePrime(t) {
1402
- var i,
1403
- x = this.abs();
1404
- if (x.t == 1 && x[0] <= lowprimes[lowprimes.length - 1]) {
1405
- for (i = 0; i < lowprimes.length; ++i) if (x[0] == lowprimes[i]) return true;
1406
- return false;
1407
- }
1408
- if (x.isEven()) return false;
1409
- i = 1;
1410
- while (i < lowprimes.length) {
1411
- var m = lowprimes[i],
1412
- j = i + 1;
1413
- while (j < lowprimes.length && m < lplim) m *= lowprimes[j++];
1414
- m = x.modInt(m);
1415
- while (i < j) if (m % lowprimes[i++] == 0) return false;
1416
- }
1417
- return x.millerRabin(t);
1418
- }
1419
-
1420
- // (protected) true if probably prime (HAC 4.24, Miller-Rabin)
1421
- function bnpMillerRabin(t) {
1422
- var n1 = this.subtract(BigInteger.ONE);
1423
- var k = n1.getLowestSetBit();
1424
- if (k <= 0) return false;
1425
- var r = n1.shiftRight(k);
1426
- t = (t + 1) >> 1;
1427
- if (t > lowprimes.length) t = lowprimes.length;
1428
- var a = nbi();
1429
- for (var i = 0; i < t; ++i) {
1430
- // Pick bases at random, instead of starting at 2
1431
- a.fromInt(lowprimes[Math.floor(Math.random() * lowprimes.length)]);
1432
- var y = a.modPow(r, this);
1433
- if (y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
1434
- var j = 1;
1435
- while (j++ < k && y.compareTo(n1) != 0) {
1436
- y = y.modPowInt(2, this);
1437
- if (y.compareTo(BigInteger.ONE) == 0) return false;
1438
- }
1439
- if (y.compareTo(n1) != 0) return false;
1440
- }
1441
- }
1442
- return true;
1443
- }
1444
-
1445
- // protected
1446
- BigInteger.prototype.chunkSize = bnpChunkSize;
1447
- BigInteger.prototype.toRadix = bnpToRadix;
1448
- BigInteger.prototype.fromRadix = bnpFromRadix;
1449
- BigInteger.prototype.fromNumber = bnpFromNumber;
1450
- BigInteger.prototype.bitwiseTo = bnpBitwiseTo;
1451
- BigInteger.prototype.changeBit = bnpChangeBit;
1452
- BigInteger.prototype.addTo = bnpAddTo;
1453
- BigInteger.prototype.dMultiply = bnpDMultiply;
1454
- BigInteger.prototype.dAddOffset = bnpDAddOffset;
1455
- BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo;
1456
- BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
1457
- BigInteger.prototype.modInt = bnpModInt;
1458
- BigInteger.prototype.millerRabin = bnpMillerRabin;
1459
-
1460
- // public
1461
- BigInteger.prototype.clone = bnClone;
1462
- BigInteger.prototype.intValue = bnIntValue;
1463
- BigInteger.prototype.byteValue = bnByteValue;
1464
- BigInteger.prototype.shortValue = bnShortValue;
1465
- BigInteger.prototype.signum = bnSigNum;
1466
- BigInteger.prototype.toByteArray = bnToByteArray;
1467
- BigInteger.prototype.equals = bnEquals;
1468
- BigInteger.prototype.min = bnMin;
1469
- BigInteger.prototype.max = bnMax;
1470
- BigInteger.prototype.and = bnAnd;
1471
- BigInteger.prototype.or = bnOr;
1472
- BigInteger.prototype.xor = bnXor;
1473
- BigInteger.prototype.andNot = bnAndNot;
1474
- BigInteger.prototype.not = bnNot;
1475
- BigInteger.prototype.shiftLeft = bnShiftLeft;
1476
- BigInteger.prototype.shiftRight = bnShiftRight;
1477
- BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit;
1478
- BigInteger.prototype.bitCount = bnBitCount;
1479
- BigInteger.prototype.testBit = bnTestBit;
1480
- BigInteger.prototype.setBit = bnSetBit;
1481
- BigInteger.prototype.clearBit = bnClearBit;
1482
- BigInteger.prototype.flipBit = bnFlipBit;
1483
- BigInteger.prototype.add = bnAdd;
1484
- BigInteger.prototype.subtract = bnSubtract;
1485
- BigInteger.prototype.multiply = bnMultiply;
1486
- BigInteger.prototype.divide = bnDivide;
1487
- BigInteger.prototype.remainder = bnRemainder;
1488
- BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder;
1489
- BigInteger.prototype.modPow = bnModPow;
1490
- BigInteger.prototype.modInverse = bnModInverse;
1491
- BigInteger.prototype.pow = bnPow;
1492
- BigInteger.prototype.gcd = bnGCD;
1493
- BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
1494
-
1495
- // JSBN-specific extension
1496
- BigInteger.prototype.square = bnSquare;
1497
-
1498
- // Expose the Barrett function
1499
- BigInteger.prototype.Barrett = Barrett;
1500
-
1501
- // BigInteger interfaces not implemented in jsbn:
1502
-
1503
- // BigInteger(int signum, byte[] magnitude)
1504
- // double doubleValue()
1505
- // float floatValue()
1506
- // int hashCode()
1507
- // long longValue()
1508
- // static BigInteger valueOf(long val)
1509
-
1510
- // Random number generator - requires a PRNG backend, e.g. prng4.js
1511
-
1512
- // For best results, put code like
1513
- // <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'>
1514
- // in your main HTML document.
1515
-
1516
- var rng_state;
1517
- var rng_pool;
1518
- var rng_pptr;
1519
-
1520
- // Mix in a 32-bit integer into the pool
1521
- function rng_seed_int(x) {
1522
- rng_pool[rng_pptr++] ^= x & 255;
1523
- rng_pool[rng_pptr++] ^= (x >> 8) & 255;
1524
- rng_pool[rng_pptr++] ^= (x >> 16) & 255;
1525
- rng_pool[rng_pptr++] ^= (x >> 24) & 255;
1526
- if (rng_pptr >= rng_psize) rng_pptr -= rng_psize;
1527
- }
1528
-
1529
- // Mix in the current time (w/milliseconds) into the pool
1530
- function rng_seed_time() {
1531
- rng_seed_int(new Date().getTime());
1532
- }
1533
-
1534
- // Initialize the pool with junk if needed.
1535
- if (rng_pool == null) {
1536
- rng_pool = new Array();
1537
- rng_pptr = 0;
1538
- var t;
1539
- if (typeof window !== "undefined" && window.crypto) {
1540
- if (window.crypto.getRandomValues) {
1541
- // Use webcrypto if available
1542
- var ua = new Uint8Array(32);
1543
- window.crypto.getRandomValues(ua);
1544
- for (t = 0; t < 32; ++t) rng_pool[rng_pptr++] = ua[t];
1545
- } else if (navigator.appName == "Netscape" && navigator.appVersion < "5") {
1546
- // Extract entropy (256 bits) from NS4 RNG if available
1547
- var z = window.crypto.random(32);
1548
- for (t = 0; t < z.length; ++t) rng_pool[rng_pptr++] = z.charCodeAt(t) & 255;
1549
- }
1550
- }
1551
- while (rng_pptr < rng_psize) {
1552
- // extract some randomness from Math.random()
1553
- t = Math.floor(65536 * Math.random());
1554
- rng_pool[rng_pptr++] = t >>> 8;
1555
- rng_pool[rng_pptr++] = t & 255;
1556
- }
1557
- rng_pptr = 0;
1558
- rng_seed_time();
1559
- // rng_seed_int(window.screenX);
1560
- // rng_seed_int(window.screenY);
1561
- }
1562
-
1563
- function rng_get_byte() {
1564
- if (rng_state == null) {
1565
- rng_seed_time();
1566
- rng_state = prng_newstate();
1567
- rng_state.init(rng_pool);
1568
- for (rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr) rng_pool[rng_pptr] = 0;
1569
- rng_pptr = 0;
1570
- // rng_pool = null;
1571
- }
1572
- // TODO: allow reseeding after first request
1573
- return rng_state.next();
1574
- }
1575
-
1576
- function rng_get_bytes(ba) {
1577
- var i;
1578
- for (i = 0; i < ba.length; ++i) ba[i] = rng_get_byte();
1579
- }
1580
-
1581
- function SecureRandom() {}
1582
-
1583
- SecureRandom.prototype.nextBytes = rng_get_bytes;
1584
-
1585
- // prng4.js - uses Arcfour as a PRNG
1586
-
1587
- function Arcfour() {
1588
- this.i = 0;
1589
- this.j = 0;
1590
- this.S = new Array();
1591
- }
1592
-
1593
- // Initialize arcfour context from key, an array of ints, each from [0..255]
1594
- function ARC4init(key) {
1595
- var i, j, t;
1596
- for (i = 0; i < 256; ++i) this.S[i] = i;
1597
- j = 0;
1598
- for (i = 0; i < 256; ++i) {
1599
- j = (j + this.S[i] + key[i % key.length]) & 255;
1600
- t = this.S[i];
1601
- this.S[i] = this.S[j];
1602
- this.S[j] = t;
1603
- }
1604
- this.i = 0;
1605
- this.j = 0;
1606
- }
1607
-
1608
- function ARC4next() {
1609
- var t;
1610
- this.i = (this.i + 1) & 255;
1611
- this.j = (this.j + this.S[this.i]) & 255;
1612
- t = this.S[this.i];
1613
- this.S[this.i] = this.S[this.j];
1614
- this.S[this.j] = t;
1615
- return this.S[(t + this.S[this.i]) & 255];
1616
- }
1617
-
1618
- Arcfour.prototype.init = ARC4init;
1619
- Arcfour.prototype.next = ARC4next;
1620
-
1621
- // Plug in your RNG constructor here
1622
- function prng_newstate() {
1623
- return new Arcfour();
1624
- }
1625
-
1626
- // Pool size must be a multiple of 4 and greater than 32.
1627
- // An array of bytes the size of the pool will be passed to init()
1628
- var rng_psize = 256;
1629
-
1630
- export { BigInteger, SecureRandom };