Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ultraviolet
GitHub Repository: ultraviolet/bitaddress.org
Path: blob/master/src/biginteger.js
248 views
1
// Upstream 'BigInteger' here:
2
// Original Author: http://www-cs-students.stanford.edu/~tjw/jsbn/
3
// Follows 'jsbn' on Github: https://github.com/jasondavies/jsbn
4
// Review and Testing: https://github.com/cryptocoinjs/bigi/
5
/*!
6
* Basic JavaScript BN library - subset useful for RSA encryption. v1.4
7
*
8
* Copyright (c) 2005 Tom Wu
9
* All Rights Reserved.
10
* BSD License
11
* http://www-cs-students.stanford.edu/~tjw/jsbn/LICENSE
12
*
13
* Copyright Stephan Thomas
14
* Copyright pointbiz
15
*/
16
17
(function () {
18
19
// (public) Constructor function of Global BigInteger object
20
var BigInteger = window.BigInteger = function BigInteger(a, b, c) {
21
if (!(this instanceof BigInteger))
22
return new BigInteger(a, b, c);
23
24
if (a != null)
25
if ("number" == typeof a) this.fromNumber(a, b, c);
26
else if (b == null && "string" != typeof a) this.fromString(a, 256);
27
else this.fromString(a, b);
28
};
29
30
// Bits per digit
31
var dbits;
32
33
// JavaScript engine analysis
34
var canary = 0xdeadbeefcafe;
35
var j_lm = ((canary & 0xffffff) == 0xefcafe);
36
37
// return new, unset BigInteger
38
function nbi() { return new BigInteger(null); }
39
40
// am: Compute w_j += (x*this_i), propagate carries,
41
// c is initial carry, returns final carry.
42
// c < 3*dvalue, x < 2*dvalue, this_i < dvalue
43
// We need to select the fastest one that works in this environment.
44
45
// am1: use a single mult and divide to get the high bits,
46
// max digit bits should be 26 because
47
// max internal value = 2*dvalue^2-2*dvalue (< 2^53)
48
function am1(i, x, w, j, c, n) {
49
while (--n >= 0) {
50
var v = x * this[i++] + w[j] + c;
51
c = Math.floor(v / 0x4000000);
52
w[j++] = v & 0x3ffffff;
53
}
54
return c;
55
}
56
// am2 avoids a big mult-and-extract completely.
57
// Max digit bits should be <= 30 because we do bitwise ops
58
// on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
59
function am2(i, x, w, j, c, n) {
60
var xl = x & 0x7fff, xh = x >> 15;
61
while (--n >= 0) {
62
var l = this[i] & 0x7fff;
63
var h = this[i++] >> 15;
64
var m = xh * l + h * xl;
65
l = xl * l + ((m & 0x7fff) << 15) + w[j] + (c & 0x3fffffff);
66
c = (l >>> 30) + (m >>> 15) + xh * h + (c >>> 30);
67
w[j++] = l & 0x3fffffff;
68
}
69
return c;
70
}
71
// Alternately, set max digit bits to 28 since some
72
// browsers slow down when dealing with 32-bit numbers.
73
function am3(i, x, w, j, c, n) {
74
var xl = x & 0x3fff, xh = x >> 14;
75
while (--n >= 0) {
76
var l = this[i] & 0x3fff;
77
var h = this[i++] >> 14;
78
var m = xh * l + h * xl;
79
l = xl * l + ((m & 0x3fff) << 14) + w[j] + c;
80
c = (l >> 28) + (m >> 14) + xh * h;
81
w[j++] = l & 0xfffffff;
82
}
83
return c;
84
}
85
if (j_lm && (navigator.appName == "Microsoft Internet Explorer")) {
86
BigInteger.prototype.am = am2;
87
dbits = 30;
88
}
89
else if (j_lm && (navigator.appName != "Netscape")) {
90
BigInteger.prototype.am = am1;
91
dbits = 26;
92
}
93
else { // Mozilla/Netscape seems to prefer am3
94
BigInteger.prototype.am = am3;
95
dbits = 28;
96
}
97
98
BigInteger.prototype.DB = dbits;
99
BigInteger.prototype.DM = ((1 << dbits) - 1);
100
BigInteger.prototype.DV = (1 << dbits);
101
102
var BI_FP = 52;
103
BigInteger.prototype.FV = Math.pow(2, BI_FP);
104
BigInteger.prototype.F1 = BI_FP - dbits;
105
BigInteger.prototype.F2 = 2 * dbits - BI_FP;
106
107
// Digit conversions
108
var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
109
var BI_RC = new Array();
110
var rr, vv;
111
rr = "0".charCodeAt(0);
112
for (vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
113
rr = "a".charCodeAt(0);
114
for (vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
115
rr = "A".charCodeAt(0);
116
for (vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
117
118
function int2char(n) { return BI_RM.charAt(n); }
119
function intAt(s, i) {
120
var c = BI_RC[s.charCodeAt(i)];
121
return (c == null) ? -1 : c;
122
}
123
124
125
126
// return bigint initialized to value
127
function nbv(i) { var r = nbi(); r.fromInt(i); return r; }
128
129
130
// returns bit length of the integer x
131
function nbits(x) {
132
var r = 1, t;
133
if ((t = x >>> 16) != 0) { x = t; r += 16; }
134
if ((t = x >> 8) != 0) { x = t; r += 8; }
135
if ((t = x >> 4) != 0) { x = t; r += 4; }
136
if ((t = x >> 2) != 0) { x = t; r += 2; }
137
if ((t = x >> 1) != 0) { x = t; r += 1; }
138
return r;
139
}
140
141
142
143
144
145
146
147
// (protected) copy this to r
148
BigInteger.prototype.copyTo = function (r) {
149
for (var i = this.t - 1; i >= 0; --i) r[i] = this[i];
150
r.t = this.t;
151
r.s = this.s;
152
};
153
154
155
// (protected) set from integer value x, -DV <= x < DV
156
BigInteger.prototype.fromInt = function (x) {
157
this.t = 1;
158
this.s = (x < 0) ? -1 : 0;
159
if (x > 0) this[0] = x;
160
else if (x < -1) this[0] = x + this.DV;
161
else this.t = 0;
162
};
163
164
// (protected) set from string and radix
165
BigInteger.prototype.fromString = function (s, b) {
166
var k;
167
if (b == 16) k = 4;
168
else if (b == 8) k = 3;
169
else if (b == 256) k = 8; // byte array
170
else if (b == 2) k = 1;
171
else if (b == 32) k = 5;
172
else if (b == 4) k = 2;
173
else { this.fromRadix(s, b); return; }
174
this.t = 0;
175
this.s = 0;
176
var i = s.length, mi = false, sh = 0;
177
while (--i >= 0) {
178
var x = (k == 8) ? s[i] & 0xff : intAt(s, i);
179
if (x < 0) {
180
if (s.charAt(i) == "-") mi = true;
181
continue;
182
}
183
mi = false;
184
if (sh == 0)
185
this[this.t++] = x;
186
else if (sh + k > this.DB) {
187
this[this.t - 1] |= (x & ((1 << (this.DB - sh)) - 1)) << sh;
188
this[this.t++] = (x >> (this.DB - sh));
189
}
190
else
191
this[this.t - 1] |= x << sh;
192
sh += k;
193
if (sh >= this.DB) sh -= this.DB;
194
}
195
if (k == 8 && (s[0] & 0x80) != 0) {
196
this.s = -1;
197
if (sh > 0) this[this.t - 1] |= ((1 << (this.DB - sh)) - 1) << sh;
198
}
199
this.clamp();
200
if (mi) BigInteger.ZERO.subTo(this, this);
201
};
202
203
204
// (protected) clamp off excess high words
205
BigInteger.prototype.clamp = function () {
206
var c = this.s & this.DM;
207
while (this.t > 0 && this[this.t - 1] == c) --this.t;
208
};
209
210
// (protected) r = this << n*DB
211
BigInteger.prototype.dlShiftTo = function (n, r) {
212
var i;
213
for (i = this.t - 1; i >= 0; --i) r[i + n] = this[i];
214
for (i = n - 1; i >= 0; --i) r[i] = 0;
215
r.t = this.t + n;
216
r.s = this.s;
217
};
218
219
// (protected) r = this >> n*DB
220
BigInteger.prototype.drShiftTo = function (n, r) {
221
for (var i = n; i < this.t; ++i) r[i - n] = this[i];
222
r.t = Math.max(this.t - n, 0);
223
r.s = this.s;
224
};
225
226
227
// (protected) r = this << n
228
BigInteger.prototype.lShiftTo = function (n, r) {
229
var bs = n % this.DB;
230
var cbs = this.DB - bs;
231
var bm = (1 << cbs) - 1;
232
var ds = Math.floor(n / this.DB), c = (this.s << bs) & this.DM, i;
233
for (i = this.t - 1; i >= 0; --i) {
234
r[i + ds + 1] = (this[i] >> cbs) | c;
235
c = (this[i] & bm) << bs;
236
}
237
for (i = ds - 1; i >= 0; --i) r[i] = 0;
238
r[ds] = c;
239
r.t = this.t + ds + 1;
240
r.s = this.s;
241
r.clamp();
242
};
243
244
245
// (protected) r = this >> n
246
BigInteger.prototype.rShiftTo = function (n, r) {
247
r.s = this.s;
248
var ds = Math.floor(n / this.DB);
249
if (ds >= this.t) { r.t = 0; return; }
250
var bs = n % this.DB;
251
var cbs = this.DB - bs;
252
var bm = (1 << bs) - 1;
253
r[0] = this[ds] >> bs;
254
for (var i = ds + 1; i < this.t; ++i) {
255
r[i - ds - 1] |= (this[i] & bm) << cbs;
256
r[i - ds] = this[i] >> bs;
257
}
258
if (bs > 0) r[this.t - ds - 1] |= (this.s & bm) << cbs;
259
r.t = this.t - ds;
260
r.clamp();
261
};
262
263
264
// (protected) r = this - a
265
BigInteger.prototype.subTo = function (a, r) {
266
var i = 0, c = 0, m = Math.min(a.t, this.t);
267
while (i < m) {
268
c += this[i] - a[i];
269
r[i++] = c & this.DM;
270
c >>= this.DB;
271
}
272
if (a.t < this.t) {
273
c -= a.s;
274
while (i < this.t) {
275
c += this[i];
276
r[i++] = c & this.DM;
277
c >>= this.DB;
278
}
279
c += this.s;
280
}
281
else {
282
c += this.s;
283
while (i < a.t) {
284
c -= a[i];
285
r[i++] = c & this.DM;
286
c >>= this.DB;
287
}
288
c -= a.s;
289
}
290
r.s = (c < 0) ? -1 : 0;
291
if (c < -1) r[i++] = this.DV + c;
292
else if (c > 0) r[i++] = c;
293
r.t = i;
294
r.clamp();
295
};
296
297
298
// (protected) r = this * a, r != this,a (HAC 14.12)
299
// "this" should be the larger one if appropriate.
300
BigInteger.prototype.multiplyTo = function (a, r) {
301
var x = this.abs(), y = a.abs();
302
var i = x.t;
303
r.t = i + y.t;
304
while (--i >= 0) r[i] = 0;
305
for (i = 0; i < y.t; ++i) r[i + x.t] = x.am(0, y[i], r, i, 0, x.t);
306
r.s = 0;
307
r.clamp();
308
if (this.s != a.s) BigInteger.ZERO.subTo(r, r);
309
};
310
311
312
// (protected) r = this^2, r != this (HAC 14.16)
313
BigInteger.prototype.squareTo = function (r) {
314
var x = this.abs();
315
var i = r.t = 2 * x.t;
316
while (--i >= 0) r[i] = 0;
317
for (i = 0; i < x.t - 1; ++i) {
318
var c = x.am(i, x[i], r, 2 * i, 0, 1);
319
if ((r[i + x.t] += x.am(i + 1, 2 * x[i], r, 2 * i + 1, c, x.t - i - 1)) >= x.DV) {
320
r[i + x.t] -= x.DV;
321
r[i + x.t + 1] = 1;
322
}
323
}
324
if (r.t > 0) r[r.t - 1] += x.am(i, x[i], r, 2 * i, 0, 1);
325
r.s = 0;
326
r.clamp();
327
};
328
329
330
331
// (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
332
// r != q, this != m. q or r may be null.
333
BigInteger.prototype.divRemTo = function (m, q, r) {
334
var pm = m.abs();
335
if (pm.t <= 0) return;
336
var pt = this.abs();
337
if (pt.t < pm.t) {
338
if (q != null) q.fromInt(0);
339
if (r != null) this.copyTo(r);
340
return;
341
}
342
if (r == null) r = nbi();
343
var y = nbi(), ts = this.s, ms = m.s;
344
var nsh = this.DB - nbits(pm[pm.t - 1]); // normalize modulus
345
if (nsh > 0) { pm.lShiftTo(nsh, y); pt.lShiftTo(nsh, r); }
346
else { pm.copyTo(y); pt.copyTo(r); }
347
var ys = y.t;
348
var y0 = y[ys - 1];
349
if (y0 == 0) return;
350
var yt = y0 * (1 << this.F1) + ((ys > 1) ? y[ys - 2] >> this.F2 : 0);
351
var d1 = this.FV / yt, d2 = (1 << this.F1) / yt, e = 1 << this.F2;
352
var i = r.t, j = i - ys, t = (q == null) ? nbi() : q;
353
y.dlShiftTo(j, t);
354
if (r.compareTo(t) >= 0) {
355
r[r.t++] = 1;
356
r.subTo(t, r);
357
}
358
BigInteger.ONE.dlShiftTo(ys, t);
359
t.subTo(y, y); // "negative" y so we can replace sub with am later
360
while (y.t < ys) y[y.t++] = 0;
361
while (--j >= 0) {
362
// Estimate quotient digit
363
var qd = (r[--i] == y0) ? this.DM : Math.floor(r[i] * d1 + (r[i - 1] + e) * d2);
364
if ((r[i] += y.am(0, qd, r, j, 0, ys)) < qd) { // Try it out
365
y.dlShiftTo(j, t);
366
r.subTo(t, r);
367
while (r[i] < --qd) r.subTo(t, r);
368
}
369
}
370
if (q != null) {
371
r.drShiftTo(ys, q);
372
if (ts != ms) BigInteger.ZERO.subTo(q, q);
373
}
374
r.t = ys;
375
r.clamp();
376
if (nsh > 0) r.rShiftTo(nsh, r); // Denormalize remainder
377
if (ts < 0) BigInteger.ZERO.subTo(r, r);
378
};
379
380
381
// (protected) return "-1/this % 2^DB"; useful for Mont. reduction
382
// justification:
383
// xy == 1 (mod m)
384
// xy = 1+km
385
// xy(2-xy) = (1+km)(1-km)
386
// x[y(2-xy)] = 1-k^2m^2
387
// x[y(2-xy)] == 1 (mod m^2)
388
// if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
389
// should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
390
// JS multiply "overflows" differently from C/C++, so care is needed here.
391
BigInteger.prototype.invDigit = function () {
392
if (this.t < 1) return 0;
393
var x = this[0];
394
if ((x & 1) == 0) return 0;
395
var y = x & 3; // y == 1/x mod 2^2
396
y = (y * (2 - (x & 0xf) * y)) & 0xf; // y == 1/x mod 2^4
397
y = (y * (2 - (x & 0xff) * y)) & 0xff; // y == 1/x mod 2^8
398
y = (y * (2 - (((x & 0xffff) * y) & 0xffff))) & 0xffff; // y == 1/x mod 2^16
399
// last step - calculate inverse mod DV directly;
400
// assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
401
y = (y * (2 - x * y % this.DV)) % this.DV; // y == 1/x mod 2^dbits
402
// we really want the negative inverse, and -DV < y < DV
403
return (y > 0) ? this.DV - y : -y;
404
};
405
406
407
// (protected) true iff this is even
408
BigInteger.prototype.isEven = function () { return ((this.t > 0) ? (this[0] & 1) : this.s) == 0; };
409
410
411
// (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
412
BigInteger.prototype.exp = function (e, z) {
413
if (e > 0xffffffff || e < 1) return BigInteger.ONE;
414
var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e) - 1;
415
g.copyTo(r);
416
while (--i >= 0) {
417
z.sqrTo(r, r2);
418
if ((e & (1 << i)) > 0) z.mulTo(r2, g, r);
419
else { var t = r; r = r2; r2 = t; }
420
}
421
return z.revert(r);
422
};
423
424
425
// (public) return string representation in given radix
426
BigInteger.prototype.toString = function (b) {
427
if (this.s < 0) return "-" + this.negate().toString(b);
428
var k;
429
if (b == 16) k = 4;
430
else if (b == 8) k = 3;
431
else if (b == 2) k = 1;
432
else if (b == 32) k = 5;
433
else if (b == 4) k = 2;
434
else return this.toRadix(b);
435
var km = (1 << k) - 1, d, m = false, r = "", i = this.t;
436
var p = this.DB - (i * this.DB) % k;
437
if (i-- > 0) {
438
if (p < this.DB && (d = this[i] >> p) > 0) { m = true; r = int2char(d); }
439
while (i >= 0) {
440
if (p < k) {
441
d = (this[i] & ((1 << p) - 1)) << (k - p);
442
d |= this[--i] >> (p += this.DB - k);
443
}
444
else {
445
d = (this[i] >> (p -= k)) & km;
446
if (p <= 0) { p += this.DB; --i; }
447
}
448
if (d > 0) m = true;
449
if (m) r += int2char(d);
450
}
451
}
452
return m ? r : "0";
453
};
454
455
456
// (public) -this
457
BigInteger.prototype.negate = function () { var r = nbi(); BigInteger.ZERO.subTo(this, r); return r; };
458
459
// (public) |this|
460
BigInteger.prototype.abs = function () { return (this.s < 0) ? this.negate() : this; };
461
462
// (public) return + if this > a, - if this < a, 0 if equal
463
BigInteger.prototype.compareTo = function (a) {
464
var r = this.s - a.s;
465
if (r != 0) return r;
466
var i = this.t;
467
r = i - a.t;
468
if (r != 0) return (this.s < 0) ? -r : r;
469
while (--i >= 0) if ((r = this[i] - a[i]) != 0) return r;
470
return 0;
471
}
472
473
// (public) return the number of bits in "this"
474
BigInteger.prototype.bitLength = function () {
475
if (this.t <= 0) return 0;
476
return this.DB * (this.t - 1) + nbits(this[this.t - 1] ^ (this.s & this.DM));
477
};
478
479
// (public) this mod a
480
BigInteger.prototype.mod = function (a) {
481
var r = nbi();
482
this.abs().divRemTo(a, null, r);
483
if (this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r, r);
484
return r;
485
}
486
487
// (public) this^e % m, 0 <= e < 2^32
488
BigInteger.prototype.modPowInt = function (e, m) {
489
var z;
490
if (e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m);
491
return this.exp(e, z);
492
};
493
494
// "constants"
495
BigInteger.ZERO = nbv(0);
496
BigInteger.ONE = nbv(1);
497
498
499
500
501
502
503
504
// Copyright (c) 2005-2009 Tom Wu
505
// All Rights Reserved.
506
// See "LICENSE" for details.
507
// Extended JavaScript BN functions, required for RSA private ops.
508
// Version 1.1: new BigInteger("0", 10) returns "proper" zero
509
// Version 1.2: square() API, isProbablePrime fix
510
511
512
// return index of lowest 1-bit in x, x < 2^31
513
function lbit(x) {
514
if (x == 0) return -1;
515
var r = 0;
516
if ((x & 0xffff) == 0) { x >>= 16; r += 16; }
517
if ((x & 0xff) == 0) { x >>= 8; r += 8; }
518
if ((x & 0xf) == 0) { x >>= 4; r += 4; }
519
if ((x & 3) == 0) { x >>= 2; r += 2; }
520
if ((x & 1) == 0) ++r;
521
return r;
522
}
523
524
// return number of 1 bits in x
525
function cbit(x) {
526
var r = 0;
527
while (x != 0) { x &= x - 1; ++r; }
528
return r;
529
}
530
531
var lowprimes = [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, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997];
532
var lplim = (1 << 26) / lowprimes[lowprimes.length - 1];
533
534
535
536
// (protected) return x s.t. r^x < DV
537
BigInteger.prototype.chunkSize = function (r) { return Math.floor(Math.LN2 * this.DB / Math.log(r)); };
538
539
// (protected) convert to radix string
540
BigInteger.prototype.toRadix = function (b) {
541
if (b == null) b = 10;
542
if (this.signum() == 0 || b < 2 || b > 36) return "0";
543
var cs = this.chunkSize(b);
544
var a = Math.pow(b, cs);
545
var d = nbv(a), y = nbi(), z = nbi(), r = "";
546
this.divRemTo(d, y, z);
547
while (y.signum() > 0) {
548
r = (a + z.intValue()).toString(b).substr(1) + r;
549
y.divRemTo(d, y, z);
550
}
551
return z.intValue().toString(b) + r;
552
};
553
554
// (protected) convert from radix string
555
BigInteger.prototype.fromRadix = function (s, b) {
556
this.fromInt(0);
557
if (b == null) b = 10;
558
var cs = this.chunkSize(b);
559
var d = Math.pow(b, cs), mi = false, j = 0, w = 0;
560
for (var i = 0; i < s.length; ++i) {
561
var x = intAt(s, i);
562
if (x < 0) {
563
if (s.charAt(i) == "-" && this.signum() == 0) mi = true;
564
continue;
565
}
566
w = b * w + x;
567
if (++j >= cs) {
568
this.dMultiply(d);
569
this.dAddOffset(w, 0);
570
j = 0;
571
w = 0;
572
}
573
}
574
if (j > 0) {
575
this.dMultiply(Math.pow(b, j));
576
this.dAddOffset(w, 0);
577
}
578
if (mi) BigInteger.ZERO.subTo(this, this);
579
};
580
581
// (protected) alternate constructor
582
BigInteger.prototype.fromNumber = function (a, b, c) {
583
if ("number" == typeof b) {
584
// new BigInteger(int,int,RNG)
585
if (a < 2) this.fromInt(1);
586
else {
587
this.fromNumber(a, c);
588
if (!this.testBit(a - 1)) // force MSB set
589
this.bitwiseTo(BigInteger.ONE.shiftLeft(a - 1), op_or, this);
590
if (this.isEven()) this.dAddOffset(1, 0); // force odd
591
while (!this.isProbablePrime(b)) {
592
this.dAddOffset(2, 0);
593
if (this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a - 1), this);
594
}
595
}
596
}
597
else {
598
// new BigInteger(int,RNG)
599
var x = new Array(), t = a & 7;
600
x.length = (a >> 3) + 1;
601
b.nextBytes(x);
602
if (t > 0) x[0] &= ((1 << t) - 1); else x[0] = 0;
603
this.fromString(x, 256);
604
}
605
};
606
607
// (protected) r = this op a (bitwise)
608
BigInteger.prototype.bitwiseTo = function (a, op, r) {
609
var i, f, m = Math.min(a.t, this.t);
610
for (i = 0; i < m; ++i) r[i] = op(this[i], a[i]);
611
if (a.t < this.t) {
612
f = a.s & this.DM;
613
for (i = m; i < this.t; ++i) r[i] = op(this[i], f);
614
r.t = this.t;
615
}
616
else {
617
f = this.s & this.DM;
618
for (i = m; i < a.t; ++i) r[i] = op(f, a[i]);
619
r.t = a.t;
620
}
621
r.s = op(this.s, a.s);
622
r.clamp();
623
};
624
625
// (protected) this op (1<<n)
626
BigInteger.prototype.changeBit = function (n, op) {
627
var r = BigInteger.ONE.shiftLeft(n);
628
this.bitwiseTo(r, op, r);
629
return r;
630
};
631
632
// (protected) r = this + a
633
BigInteger.prototype.addTo = function (a, r) {
634
var i = 0, c = 0, m = Math.min(a.t, this.t);
635
while (i < m) {
636
c += this[i] + a[i];
637
r[i++] = c & this.DM;
638
c >>= this.DB;
639
}
640
if (a.t < this.t) {
641
c += a.s;
642
while (i < this.t) {
643
c += this[i];
644
r[i++] = c & this.DM;
645
c >>= this.DB;
646
}
647
c += this.s;
648
}
649
else {
650
c += this.s;
651
while (i < a.t) {
652
c += a[i];
653
r[i++] = c & this.DM;
654
c >>= this.DB;
655
}
656
c += a.s;
657
}
658
r.s = (c < 0) ? -1 : 0;
659
if (c > 0) r[i++] = c;
660
else if (c < -1) r[i++] = this.DV + c;
661
r.t = i;
662
r.clamp();
663
};
664
665
// (protected) this *= n, this >= 0, 1 < n < DV
666
BigInteger.prototype.dMultiply = function (n) {
667
this[this.t] = this.am(0, n - 1, this, 0, 0, this.t);
668
++this.t;
669
this.clamp();
670
};
671
672
// (protected) this += n << w words, this >= 0
673
BigInteger.prototype.dAddOffset = function (n, w) {
674
if (n == 0) return;
675
while (this.t <= w) this[this.t++] = 0;
676
this[w] += n;
677
while (this[w] >= this.DV) {
678
this[w] -= this.DV;
679
if (++w >= this.t) this[this.t++] = 0;
680
++this[w];
681
}
682
};
683
684
// (protected) r = lower n words of "this * a", a.t <= n
685
// "this" should be the larger one if appropriate.
686
BigInteger.prototype.multiplyLowerTo = function (a, n, r) {
687
var i = Math.min(this.t + a.t, n);
688
r.s = 0; // assumes a,this >= 0
689
r.t = i;
690
while (i > 0) r[--i] = 0;
691
var j;
692
for (j = r.t - this.t; i < j; ++i) r[i + this.t] = this.am(0, a[i], r, i, 0, this.t);
693
for (j = Math.min(a.t, n); i < j; ++i) this.am(0, a[i], r, i, 0, n - i);
694
r.clamp();
695
};
696
697
698
// (protected) r = "this * a" without lower n words, n > 0
699
// "this" should be the larger one if appropriate.
700
BigInteger.prototype.multiplyUpperTo = function (a, n, r) {
701
--n;
702
var i = r.t = this.t + a.t - n;
703
r.s = 0; // assumes a,this >= 0
704
while (--i >= 0) r[i] = 0;
705
for (i = Math.max(n - this.t, 0); i < a.t; ++i)
706
r[this.t + i - n] = this.am(n - i, a[i], r, 0, 0, this.t + i - n);
707
r.clamp();
708
r.drShiftTo(1, r);
709
};
710
711
// (protected) this % n, n < 2^26
712
BigInteger.prototype.modInt = function (n) {
713
if (n <= 0) return 0;
714
var d = this.DV % n, r = (this.s < 0) ? n - 1 : 0;
715
if (this.t > 0)
716
if (d == 0) r = this[0] % n;
717
else for (var i = this.t - 1; i >= 0; --i) r = (d * r + this[i]) % n;
718
return r;
719
};
720
721
722
// (protected) true if probably prime (HAC 4.24, Miller-Rabin)
723
BigInteger.prototype.millerRabin = function (t) {
724
var n1 = this.subtract(BigInteger.ONE);
725
var k = n1.getLowestSetBit();
726
if (k <= 0) return false;
727
var r = n1.shiftRight(k);
728
t = (t + 1) >> 1;
729
if (t > lowprimes.length) t = lowprimes.length;
730
var a = nbi();
731
for (var i = 0; i < t; ++i) {
732
//Pick bases at random, instead of starting at 2
733
a.fromInt(lowprimes[Math.floor(Math.random() * lowprimes.length)]);
734
var y = a.modPow(r, this);
735
if (y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
736
var j = 1;
737
while (j++ < k && y.compareTo(n1) != 0) {
738
y = y.modPowInt(2, this);
739
if (y.compareTo(BigInteger.ONE) == 0) return false;
740
}
741
if (y.compareTo(n1) != 0) return false;
742
}
743
}
744
return true;
745
};
746
747
748
749
// (public)
750
BigInteger.prototype.clone = function () { var r = nbi(); this.copyTo(r); return r; };
751
752
// (public) return value as integer
753
BigInteger.prototype.intValue = function () {
754
if (this.s < 0) {
755
if (this.t == 1) return this[0] - this.DV;
756
else if (this.t == 0) return -1;
757
}
758
else if (this.t == 1) return this[0];
759
else if (this.t == 0) return 0;
760
// assumes 16 < DB < 32
761
return ((this[1] & ((1 << (32 - this.DB)) - 1)) << this.DB) | this[0];
762
};
763
764
765
// (public) return value as byte
766
BigInteger.prototype.byteValue = function () { return (this.t == 0) ? this.s : (this[0] << 24) >> 24; };
767
768
// (public) return value as short (assumes DB>=16)
769
BigInteger.prototype.shortValue = function () { return (this.t == 0) ? this.s : (this[0] << 16) >> 16; };
770
771
// (public) 0 if this == 0, 1 if this > 0
772
BigInteger.prototype.signum = function () {
773
if (this.s < 0) return -1;
774
else if (this.t <= 0 || (this.t == 1 && this[0] <= 0)) return 0;
775
else return 1;
776
};
777
778
779
// (public) convert to bigendian byte array
780
BigInteger.prototype.toByteArray = function () {
781
var i = this.t, r = new Array();
782
r[0] = this.s;
783
var p = this.DB - (i * this.DB) % 8, d, k = 0;
784
if (i-- > 0) {
785
if (p < this.DB && (d = this[i] >> p) != (this.s & this.DM) >> p)
786
r[k++] = d | (this.s << (this.DB - p));
787
while (i >= 0) {
788
if (p < 8) {
789
d = (this[i] & ((1 << p) - 1)) << (8 - p);
790
d |= this[--i] >> (p += this.DB - 8);
791
}
792
else {
793
d = (this[i] >> (p -= 8)) & 0xff;
794
if (p <= 0) { p += this.DB; --i; }
795
}
796
if ((d & 0x80) != 0) d |= -256;
797
if (k == 0 && (this.s & 0x80) != (d & 0x80)) ++k;
798
if (k > 0 || d != this.s) r[k++] = d;
799
}
800
}
801
return r;
802
};
803
804
BigInteger.prototype.equals = function (a) { return (this.compareTo(a) == 0); };
805
BigInteger.prototype.min = function (a) { return (this.compareTo(a) < 0) ? this : a; };
806
BigInteger.prototype.max = function (a) { return (this.compareTo(a) > 0) ? this : a; };
807
808
// (public) this & a
809
function op_and(x, y) { return x & y; }
810
BigInteger.prototype.and = function (a) { var r = nbi(); this.bitwiseTo(a, op_and, r); return r; };
811
812
// (public) this | a
813
function op_or(x, y) { return x | y; }
814
BigInteger.prototype.or = function (a) { var r = nbi(); this.bitwiseTo(a, op_or, r); return r; };
815
816
// (public) this ^ a
817
function op_xor(x, y) { return x ^ y; }
818
BigInteger.prototype.xor = function (a) { var r = nbi(); this.bitwiseTo(a, op_xor, r); return r; };
819
820
// (public) this & ~a
821
function op_andnot(x, y) { return x & ~y; }
822
BigInteger.prototype.andNot = function (a) { var r = nbi(); this.bitwiseTo(a, op_andnot, r); return r; };
823
824
// (public) ~this
825
BigInteger.prototype.not = function () {
826
var r = nbi();
827
for (var i = 0; i < this.t; ++i) r[i] = this.DM & ~this[i];
828
r.t = this.t;
829
r.s = ~this.s;
830
return r;
831
};
832
833
// (public) this << n
834
BigInteger.prototype.shiftLeft = function (n) {
835
var r = nbi();
836
if (n < 0) this.rShiftTo(-n, r); else this.lShiftTo(n, r);
837
return r;
838
};
839
840
// (public) this >> n
841
BigInteger.prototype.shiftRight = function (n) {
842
var r = nbi();
843
if (n < 0) this.lShiftTo(-n, r); else this.rShiftTo(n, r);
844
return r;
845
};
846
847
// (public) returns index of lowest 1-bit (or -1 if none)
848
BigInteger.prototype.getLowestSetBit = function () {
849
for (var i = 0; i < this.t; ++i)
850
if (this[i] != 0) return i * this.DB + lbit(this[i]);
851
if (this.s < 0) return this.t * this.DB;
852
return -1;
853
};
854
855
// (public) return number of set bits
856
BigInteger.prototype.bitCount = function () {
857
var r = 0, x = this.s & this.DM;
858
for (var i = 0; i < this.t; ++i) r += cbit(this[i] ^ x);
859
return r;
860
};
861
862
// (public) true iff nth bit is set
863
BigInteger.prototype.testBit = function (n) {
864
var j = Math.floor(n / this.DB);
865
if (j >= this.t) return (this.s != 0);
866
return ((this[j] & (1 << (n % this.DB))) != 0);
867
};
868
869
// (public) this | (1<<n)
870
BigInteger.prototype.setBit = function (n) { return this.changeBit(n, op_or); };
871
// (public) this & ~(1<<n)
872
BigInteger.prototype.clearBit = function (n) { return this.changeBit(n, op_andnot); };
873
// (public) this ^ (1<<n)
874
BigInteger.prototype.flipBit = function (n) { return this.changeBit(n, op_xor); };
875
// (public) this + a
876
BigInteger.prototype.add = function (a) { var r = nbi(); this.addTo(a, r); return r; };
877
// (public) this - a
878
BigInteger.prototype.subtract = function (a) { var r = nbi(); this.subTo(a, r); return r; };
879
// (public) this * a
880
BigInteger.prototype.multiply = function (a) { var r = nbi(); this.multiplyTo(a, r); return r; };
881
// (public) this / a
882
BigInteger.prototype.divide = function (a) { var r = nbi(); this.divRemTo(a, r, null); return r; };
883
// (public) this % a
884
BigInteger.prototype.remainder = function (a) { var r = nbi(); this.divRemTo(a, null, r); return r; };
885
// (public) [this/a,this%a]
886
BigInteger.prototype.divideAndRemainder = function (a) {
887
var q = nbi(), r = nbi();
888
this.divRemTo(a, q, r);
889
return new Array(q, r);
890
};
891
892
// (public) this^e % m (HAC 14.85)
893
BigInteger.prototype.modPow = function (e, m) {
894
var i = e.bitLength(), k, r = nbv(1), z;
895
if (i <= 0) return r;
896
else if (i < 18) k = 1;
897
else if (i < 48) k = 3;
898
else if (i < 144) k = 4;
899
else if (i < 768) k = 5;
900
else k = 6;
901
if (i < 8)
902
z = new Classic(m);
903
else if (m.isEven())
904
z = new Barrett(m);
905
else
906
z = new Montgomery(m);
907
908
// precomputation
909
var g = new Array(), n = 3, k1 = k - 1, km = (1 << k) - 1;
910
g[1] = z.convert(this);
911
if (k > 1) {
912
var g2 = nbi();
913
z.sqrTo(g[1], g2);
914
while (n <= km) {
915
g[n] = nbi();
916
z.mulTo(g2, g[n - 2], g[n]);
917
n += 2;
918
}
919
}
920
921
var j = e.t - 1, w, is1 = true, r2 = nbi(), t;
922
i = nbits(e[j]) - 1;
923
while (j >= 0) {
924
if (i >= k1) w = (e[j] >> (i - k1)) & km;
925
else {
926
w = (e[j] & ((1 << (i + 1)) - 1)) << (k1 - i);
927
if (j > 0) w |= e[j - 1] >> (this.DB + i - k1);
928
}
929
930
n = k;
931
while ((w & 1) == 0) { w >>= 1; --n; }
932
if ((i -= n) < 0) { i += this.DB; --j; }
933
if (is1) { // ret == 1, don't bother squaring or multiplying it
934
g[w].copyTo(r);
935
is1 = false;
936
}
937
else {
938
while (n > 1) { z.sqrTo(r, r2); z.sqrTo(r2, r); n -= 2; }
939
if (n > 0) z.sqrTo(r, r2); else { t = r; r = r2; r2 = t; }
940
z.mulTo(r2, g[w], r);
941
}
942
943
while (j >= 0 && (e[j] & (1 << i)) == 0) {
944
z.sqrTo(r, r2); t = r; r = r2; r2 = t;
945
if (--i < 0) { i = this.DB - 1; --j; }
946
}
947
}
948
return z.revert(r);
949
};
950
951
// (public) 1/this % m (HAC 14.61)
952
BigInteger.prototype.modInverse = function (m) {
953
var ac = m.isEven();
954
if (this.signum() === 0) throw new Error('division by zero');
955
if ((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO;
956
var u = m.clone(), v = this.clone();
957
var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1);
958
while (u.signum() != 0) {
959
while (u.isEven()) {
960
u.rShiftTo(1, u);
961
if (ac) {
962
if (!a.isEven() || !b.isEven()) { a.addTo(this, a); b.subTo(m, b); }
963
a.rShiftTo(1, a);
964
}
965
else if (!b.isEven()) b.subTo(m, b);
966
b.rShiftTo(1, b);
967
}
968
while (v.isEven()) {
969
v.rShiftTo(1, v);
970
if (ac) {
971
if (!c.isEven() || !d.isEven()) { c.addTo(this, c); d.subTo(m, d); }
972
c.rShiftTo(1, c);
973
}
974
else if (!d.isEven()) d.subTo(m, d);
975
d.rShiftTo(1, d);
976
}
977
if (u.compareTo(v) >= 0) {
978
u.subTo(v, u);
979
if (ac) a.subTo(c, a);
980
b.subTo(d, b);
981
}
982
else {
983
v.subTo(u, v);
984
if (ac) c.subTo(a, c);
985
d.subTo(b, d);
986
}
987
}
988
if (v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
989
while (d.compareTo(m) >= 0) d.subTo(m, d);
990
while (d.signum() < 0) d.addTo(m, d);
991
return d;
992
};
993
994
995
// (public) this^e
996
BigInteger.prototype.pow = function (e) { return this.exp(e, new NullExp()); };
997
998
// (public) gcd(this,a) (HAC 14.54)
999
BigInteger.prototype.gcd = function (a) {
1000
var x = (this.s < 0) ? this.negate() : this.clone();
1001
var y = (a.s < 0) ? a.negate() : a.clone();
1002
if (x.compareTo(y) < 0) { var t = x; x = y; y = t; }
1003
var i = x.getLowestSetBit(), g = y.getLowestSetBit();
1004
if (g < 0) return x;
1005
if (i < g) g = i;
1006
if (g > 0) {
1007
x.rShiftTo(g, x);
1008
y.rShiftTo(g, y);
1009
}
1010
while (x.signum() > 0) {
1011
if ((i = x.getLowestSetBit()) > 0) x.rShiftTo(i, x);
1012
if ((i = y.getLowestSetBit()) > 0) y.rShiftTo(i, y);
1013
if (x.compareTo(y) >= 0) {
1014
x.subTo(y, x);
1015
x.rShiftTo(1, x);
1016
}
1017
else {
1018
y.subTo(x, y);
1019
y.rShiftTo(1, y);
1020
}
1021
}
1022
if (g > 0) y.lShiftTo(g, y);
1023
return y;
1024
};
1025
1026
// (public) test primality with certainty >= 1-.5^t
1027
BigInteger.prototype.isProbablePrime = function (t) {
1028
var i, x = this.abs();
1029
if (x.t == 1 && x[0] <= lowprimes[lowprimes.length - 1]) {
1030
for (i = 0; i < lowprimes.length; ++i)
1031
if (x[0] == lowprimes[i]) return true;
1032
return false;
1033
}
1034
if (x.isEven()) return false;
1035
i = 1;
1036
while (i < lowprimes.length) {
1037
var m = lowprimes[i], j = i + 1;
1038
while (j < lowprimes.length && m < lplim) m *= lowprimes[j++];
1039
m = x.modInt(m);
1040
while (i < j) if (m % lowprimes[i++] == 0) return false;
1041
}
1042
return x.millerRabin(t);
1043
};
1044
1045
1046
// JSBN-specific extension
1047
1048
// (public) this^2
1049
BigInteger.prototype.square = function () { var r = nbi(); this.squareTo(r); return r; };
1050
1051
1052
// NOTE: BigInteger interfaces not implemented in jsbn:
1053
// BigInteger(int signum, byte[] magnitude)
1054
// double doubleValue()
1055
// float floatValue()
1056
// int hashCode()
1057
// long longValue()
1058
// static BigInteger valueOf(long val)
1059
1060
1061
1062
// Copyright Stephan Thomas (start) --- //
1063
// https://raw.github.com/bitcoinjs/bitcoinjs-lib/07f9d55ccb6abd962efb6befdd37671f85ea4ff9/src/util.js
1064
// BigInteger monkey patching
1065
BigInteger.valueOf = nbv;
1066
1067
/**
1068
* Returns a byte array representation of the big integer.
1069
*
1070
* This returns the absolute of the contained value in big endian
1071
* form. A value of zero results in an empty array.
1072
*/
1073
BigInteger.prototype.toByteArrayUnsigned = function () {
1074
var ba = this.abs().toByteArray();
1075
if (ba.length) {
1076
if (ba[0] == 0) {
1077
ba = ba.slice(1);
1078
}
1079
return ba.map(function (v) {
1080
return (v < 0) ? v + 256 : v;
1081
});
1082
} else {
1083
// Empty array, nothing to do
1084
return ba;
1085
}
1086
};
1087
1088
/**
1089
* Turns a byte array into a big integer.
1090
*
1091
* This function will interpret a byte array as a big integer in big
1092
* endian notation and ignore leading zeros.
1093
*/
1094
BigInteger.fromByteArrayUnsigned = function (ba) {
1095
if (!ba.length) {
1096
return ba.valueOf(0);
1097
} else if (ba[0] & 0x80) {
1098
// Prepend a zero so the BigInteger class doesn't mistake this
1099
// for a negative integer.
1100
return new BigInteger([0].concat(ba));
1101
} else {
1102
return new BigInteger(ba);
1103
}
1104
};
1105
1106
/**
1107
* Converts big integer to signed byte representation.
1108
*
1109
* The format for this value uses a the most significant bit as a sign
1110
* bit. If the most significant bit is already occupied by the
1111
* absolute value, an extra byte is prepended and the sign bit is set
1112
* there.
1113
*
1114
* Examples:
1115
*
1116
* 0 => 0x00
1117
* 1 => 0x01
1118
* -1 => 0x81
1119
* 127 => 0x7f
1120
* -127 => 0xff
1121
* 128 => 0x0080
1122
* -128 => 0x8080
1123
* 255 => 0x00ff
1124
* -255 => 0x80ff
1125
* 16300 => 0x3fac
1126
* -16300 => 0xbfac
1127
* 62300 => 0x00f35c
1128
* -62300 => 0x80f35c
1129
*/
1130
BigInteger.prototype.toByteArraySigned = function () {
1131
var val = this.abs().toByteArrayUnsigned();
1132
var neg = this.compareTo(BigInteger.ZERO) < 0;
1133
1134
if (neg) {
1135
if (val[0] & 0x80) {
1136
val.unshift(0x80);
1137
} else {
1138
val[0] |= 0x80;
1139
}
1140
} else {
1141
if (val[0] & 0x80) {
1142
val.unshift(0x00);
1143
}
1144
}
1145
1146
return val;
1147
};
1148
1149
/**
1150
* Parse a signed big integer byte representation.
1151
*
1152
* For details on the format please see BigInteger.toByteArraySigned.
1153
*/
1154
BigInteger.fromByteArraySigned = function (ba) {
1155
// Check for negative value
1156
if (ba[0] & 0x80) {
1157
// Remove sign bit
1158
ba[0] &= 0x7f;
1159
1160
return BigInteger.fromByteArrayUnsigned(ba).negate();
1161
} else {
1162
return BigInteger.fromByteArrayUnsigned(ba);
1163
}
1164
};
1165
// Copyright Stephan Thomas (end) --- //
1166
1167
1168
1169
1170
// ****** REDUCTION ******* //
1171
1172
// Modular reduction using "classic" algorithm
1173
var Classic = window.Classic = function Classic(m) { this.m = m; }
1174
Classic.prototype.convert = function (x) {
1175
if (x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
1176
else return x;
1177
};
1178
Classic.prototype.revert = function (x) { return x; };
1179
Classic.prototype.reduce = function (x) { x.divRemTo(this.m, null, x); };
1180
Classic.prototype.mulTo = function (x, y, r) { x.multiplyTo(y, r); this.reduce(r); };
1181
Classic.prototype.sqrTo = function (x, r) { x.squareTo(r); this.reduce(r); };
1182
1183
1184
1185
1186
1187
// Montgomery reduction
1188
var Montgomery = window.Montgomery = function Montgomery(m) {
1189
this.m = m;
1190
this.mp = m.invDigit();
1191
this.mpl = this.mp & 0x7fff;
1192
this.mph = this.mp >> 15;
1193
this.um = (1 << (m.DB - 15)) - 1;
1194
this.mt2 = 2 * m.t;
1195
}
1196
// xR mod m
1197
Montgomery.prototype.convert = function (x) {
1198
var r = nbi();
1199
x.abs().dlShiftTo(this.m.t, r);
1200
r.divRemTo(this.m, null, r);
1201
if (x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r, r);
1202
return r;
1203
}
1204
// x/R mod m
1205
Montgomery.prototype.revert = function (x) {
1206
var r = nbi();
1207
x.copyTo(r);
1208
this.reduce(r);
1209
return r;
1210
};
1211
// x = x/R mod m (HAC 14.32)
1212
Montgomery.prototype.reduce = function (x) {
1213
while (x.t <= this.mt2) // pad x so am has enough room later
1214
x[x.t++] = 0;
1215
for (var i = 0; i < this.m.t; ++i) {
1216
// faster way of calculating u0 = x[i]*mp mod DV
1217
var j = x[i] & 0x7fff;
1218
var u0 = (j * this.mpl + (((j * this.mph + (x[i] >> 15) * this.mpl) & this.um) << 15)) & x.DM;
1219
// use am to combine the multiply-shift-add into one call
1220
j = i + this.m.t;
1221
x[j] += this.m.am(0, u0, x, i, 0, this.m.t);
1222
// propagate carry
1223
while (x[j] >= x.DV) { x[j] -= x.DV; x[++j]++; }
1224
}
1225
x.clamp();
1226
x.drShiftTo(this.m.t, x);
1227
if (x.compareTo(this.m) >= 0) x.subTo(this.m, x);
1228
};
1229
// r = "xy/R mod m"; x,y != r
1230
Montgomery.prototype.mulTo = function (x, y, r) { x.multiplyTo(y, r); this.reduce(r); };
1231
// r = "x^2/R mod m"; x != r
1232
Montgomery.prototype.sqrTo = function (x, r) { x.squareTo(r); this.reduce(r); };
1233
1234
1235
1236
1237
1238
// A "null" reducer
1239
var NullExp = window.NullExp = function NullExp() { }
1240
NullExp.prototype.convert = function (x) { return x; };
1241
NullExp.prototype.revert = function (x) { return x; };
1242
NullExp.prototype.mulTo = function (x, y, r) { x.multiplyTo(y, r); };
1243
NullExp.prototype.sqrTo = function (x, r) { x.squareTo(r); };
1244
1245
1246
1247
1248
1249
// Barrett modular reduction
1250
var Barrett = window.Barrett = function Barrett(m) {
1251
// setup Barrett
1252
this.r2 = nbi();
1253
this.q3 = nbi();
1254
BigInteger.ONE.dlShiftTo(2 * m.t, this.r2);
1255
this.mu = this.r2.divide(m);
1256
this.m = m;
1257
}
1258
Barrett.prototype.convert = function (x) {
1259
if (x.s < 0 || x.t > 2 * this.m.t) return x.mod(this.m);
1260
else if (x.compareTo(this.m) < 0) return x;
1261
else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; }
1262
};
1263
Barrett.prototype.revert = function (x) { return x; };
1264
// x = x mod m (HAC 14.42)
1265
Barrett.prototype.reduce = function (x) {
1266
x.drShiftTo(this.m.t - 1, this.r2);
1267
if (x.t > this.m.t + 1) { x.t = this.m.t + 1; x.clamp(); }
1268
this.mu.multiplyUpperTo(this.r2, this.m.t + 1, this.q3);
1269
this.m.multiplyLowerTo(this.q3, this.m.t + 1, this.r2);
1270
while (x.compareTo(this.r2) < 0) x.dAddOffset(1, this.m.t + 1);
1271
x.subTo(this.r2, x);
1272
while (x.compareTo(this.m) >= 0) x.subTo(this.m, x);
1273
};
1274
// r = x*y mod m; x,y != r
1275
Barrett.prototype.mulTo = function (x, y, r) { x.multiplyTo(y, r); this.reduce(r); };
1276
// r = x^2 mod m; x != r
1277
Barrett.prototype.sqrTo = function (x, r) { x.squareTo(r); this.reduce(r); };
1278
1279
})();
1280