Debian Codegraph @

dojox/math/BigInteger.js

raw symbols SCIP
1 // AMD-ID "dojox/math/BigInteger"
2 define(["dojo", "dojox", "dojo/has"], function(dojo, dojox, has) {
3
4 dojo.getObject("math.BigInteger", true, dojox);
5 dojo.experimental("dojox.math.BigInteger");
6
7 // Contributed under CLA by Tom Wu <[email protected]>
8 // See http://www-cs-students.stanford.edu/~tjw/jsbn/ for details.
9
10 // Basic JavaScript BN library - subset useful for RSA encryption.
11 // The API for dojox.math.BigInteger closely resembles that of the java.math.BigInteger class in Java.
12
13 // Bits per digit
14 var dbits;
15
16 // JavaScript engine analysis
17 var canary = 0xdeadbeefcafe;
18 var j_lm = ((canary&0xffffff)==0xefcafe);
19
20 // (public) Constructor
21 function BigInteger(a,b,c) {
22 if(a != null)
23 if("number" == typeof a) this._fromNumber(a,b,c);
24 else if(!b && "string" != typeof a) this._fromString(a,256);
25 else this._fromString(a,b);
26 }
27
28 // return new, unset BigInteger
29 function nbi() { return new BigInteger(null); }
30
31 // am: Compute w_j += (x*this_i), propagate carries,
32 // c is initial carry, returns final carry.
33 // c < 3*dvalue, x < 2*dvalue, this_i < dvalue
34 // We need to select the fastest one that works in this environment.
35
36 // am1: use a single mult and divide to get the high bits,
37 // max digit bits should be 26 because
38 // max internal value = 2*dvalue^2-2*dvalue (< 2^53)
39 function am1(i,x,w,j,c,n) {
40 while(--n >= 0) {
41 var v = x*this[i++]+w[j]+c;
42 c = Math.floor(v/0x4000000);
43 w[j++] = v&0x3ffffff;
44 }
45 return c;
46 }
47 // am2 avoids a big mult-and-extract completely.
48 // Max digit bits should be <= 30 because we do bitwise ops
49 // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
50 function am2(i,x,w,j,c,n) {
51 var xl = x&0x7fff, xh = x>>15;
52 while(--n >= 0) {
53 var l = this[i]&0x7fff;
54 var h = this[i++]>>15;
55 var m = xh*l+h*xl;
56 l = xl*l+((m&0x7fff)<<15)+w[j]+(c&0x3fffffff);
57 c = (l>>>30)+(m>>>15)+xh*h+(c>>>30);
58 w[j++] = l&0x3fffffff;
59 }
60 return c;
61 }
62 // Alternately, set max digit bits to 28 since some
63 // browsers slow down when dealing with 32-bit numbers.
64 function am3(i,x,w,j,c,n) {
65 var xl = x&0x3fff, 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 if(j_lm && has("ie")) {
77 BigInteger.prototype.am = am2;
78 dbits = 30;
79 }
80 // had another guard navigator.appName != "Netscape"
81 // this was removed since
82 // https://stackoverflow.com/questions/14573881/why-does-javascript-navigator-appname-return-netscape-for-safari-firefox-and-ch
83 else if(j_lm) {
84 BigInteger.prototype.am = am1;
85 dbits = 26;
86 }
87 else { // Mozilla/Netscape seems to prefer am3
88 BigInteger.prototype.am = am3;
89 dbits = 28;
90 }
91
92 var BI_FP = 52;
93
94 // Digit conversions
95 var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
96 var BI_RC = [];
97 var rr,vv;
98 rr = "0".charCodeAt(0);
99 for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
100 rr = "a".charCodeAt(0);
101 for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
102 rr = "A".charCodeAt(0);
103 for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
104
105 function int2char(n) { return BI_RM.charAt(n); }
106 function intAt(s,i) {
107 var c = BI_RC[s.charCodeAt(i)];
108 return (c==null)?-1:c;
109 }
110
111 // (protected) copy this to r
112 function bnpCopyTo(r) {
113 for(var i = this.t-1; i >= 0; --i) r[i] = this[i];
114 r.t = this.t;
115 r.s = this.s;
116 }
117
118 // (protected) set from integer value x, -DV <= x < DV
119 function bnpFromInt(x) {
120 this.t = 1;
121 this.s = (x<0)?-1:0;
122 if(x > 0) this[0] = x;
123 else if(x < -1) this[0] = x+_DV;
124 else this.t = 0;
125 }
126
127 // return bigint initialized to value
128 function nbv(i) { var r = nbi(); r._fromInt(i); return r; }
129
130 // (protected) set from string and radix
131 function bnpFromString(s,b) {
132 var k;
133 if(b == 16) k = 4;
134 else if(b == 8) k = 3;
135 else if(b == 256) k = 8; // byte array
136 else if(b == 2) k = 1;
137 else if(b == 32) k = 5;
138 else if(b == 4) k = 2;
139 else { this._fromRadix(s,b); return; }
140 this.t = 0;
141 this.s = 0;
142 var i = s.length, mi = false, sh = 0;
143 while(--i >= 0) {
144 var x = (k==8)?s[i]&0xff:intAt(s,i);
145 if(x < 0) {
146 if(s.charAt(i) == "-") mi = true;
147 continue;
148 }
149 mi = false;
150 if(sh == 0)
151 this[this.t++] = x;
152 else if(sh+k > this._DB) {
153 this[this.t-1] |= (x&((1<<(this._DB-sh))-1))<<sh;
154 this[this.t++] = (x>>(this._DB-sh));
155 }
156 else
157 this[this.t-1] |= x<<sh;
158 sh += k;
159 if(sh >= this._DB) sh -= this._DB;
160 }
161 if(k == 8 && (s[0]&0x80) != 0) {
162 this.s = -1;
163 if(sh > 0) this[this.t-1] |= ((1<<(this._DB-sh))-1)<<sh;
164 }
165 this._clamp();
166 if(mi) BigInteger.ZERO._subTo(this,this);
167 }
168
169 // (protected) clamp off excess high words
170 function bnpClamp() {
171 var c = this.s&this._DM;
172 while(this.t > 0 && this[this.t-1] == c) --this.t;
173 this.t = (this.t === 0) ? 1 : this.t;
174 }
175
176 // (public) return string representation in given radix
177 function bnToString(b) {
178 if(this.s < 0) return "-"+this.negate().toString(b);
179 var k;
180 if(b == 16) k = 4;
181 else if(b == 8) k = 3;
182 else if(b == 2) k = 1;
183 else if(b == 32) k = 5;
184 else if(b == 4) k = 2;
185 else return this._toRadix(b);
186 var km = (1<<k)-1, d, m = false, r = "", i = this.t;
187 var p = this._DB-(i*this._DB)%k;
188 if(i-- > 0) {
189 if(p < this._DB && (d = this[i]>>p) > 0) { m = true; r = int2char(d); }
190 while(i >= 0) {
191 if(p < k) {
192 d = (this[i]&((1<<p)-1))<<(k-p);
193 d |= this[--i]>>(p+=this._DB-k);
194 }
195 else {
196 d = (this[i]>>(p-=k))&km;
197 if(p <= 0) { p += this._DB; --i; }
198 }
199 if(d > 0) m = true;
200 if(m) r += int2char(d);
201 }
202 }
203 return m?r:"0";
204 }
205
206 // (public) -this
207 function bnNegate() { var r = nbi(); BigInteger.ZERO._subTo(this,r); return r; }
208
209 // (public) |this|
210 function bnAbs() { return (this.s<0)?this.negate():this; }
211
212 // (public) return +1 if this > a, -1 if this < a, 0 if equal
213 function bnCompareTo(a) {
214 if(this.s !== a.s) return this.s > a.s ? 1 : -1; // check sign
215 if(this.t !== a.t) return (this.s === 0) ? (this.t > a.t ? 1 : -1) : (this.t < a.t ? 1 : -1); // check size
216 var i = this.t;
217 while(--i >= 0) if(this[i] !== a[i]) return (this.s === 0) ? (this[i] > a[i] ? 1 : -1) : (this[i] > a[i] ? 1 : -1); // check indivitual bytes
218 return 0;
219 }
220
221 // returns bit length of the integer x
222 function nbits(x) {
223 var r = 1, t;
224 if((t=x>>>16)) { x = t; r += 16; }
225 if((t=x>>8)) { x = t; r += 8; }
226 if((t=x>>4)) { x = t; r += 4; }
227 if((t=x>>2)) { x = t; r += 2; }
228 if((t=x>>1)) { x = t; r += 1; }
229 return r;
230 }
231
232 // (public) return the number of bits in "this"
233 function bnBitLength() {
234 if(this.t <= 0) return 0;
235 return this._DB*(this.t-1)+nbits(this[this.t-1]^(this.s&this._DM));
236 }
237
238 // (protected) r = this << n*DB
239 function bnpDLShiftTo(n,r) {
240 var i;
241 for(i = this.t-1; i >= 0; --i) r[i+n] = this[i];
242 for(i = n-1; i >= 0; --i) r[i] = 0;
243 r.t = this.t+n;
244 r.s = this.s;
245 }
246
247 // (protected) r = this >> n*DB
248 function bnpDRShiftTo(n,r) {
249 for(var i = n; i < this.t; ++i) r[i-n] = this[i];
250 r.t = Math.max(this.t-n,0);
251 r.s = this.s;
252 }
253
254 // (protected) r = this << n
255 function bnpLShiftTo(n,r) {
256 var bs = n%this._DB;
257 var cbs = this._DB-bs;
258 var bm = (1<<cbs)-1;
259 var ds = Math.floor(n/this._DB), c = (this.s<<bs)&this._DM, i;
260 for(i = this.t-1; i >= 0; --i) {
261 r[i+ds+1] = (this[i]>>cbs)|c;
262 c = (this[i]&bm)<<bs;
263 }
264 for(i = ds-1; i >= 0; --i) r[i] = 0;
265 r[ds] = c;
266 r.t = this.t+ds+1;
267 r.s = this.s;
268 r._clamp();
269 }
270
271 // (protected) r = this >> n
272 function bnpRShiftTo(n,r) {
273 r.s = this.s;
274 var ds = Math.floor(n/this._DB);
275 if(ds >= this.t) { r.t = 0; return; }
276 var bs = n%this._DB;
277 var cbs = this._DB-bs;
278 var bm = (1<<bs)-1;
279 r[0] = this[ds]>>bs;
280 for(var i = ds+1; i < this.t; ++i) {
281 r[i-ds-1] |= (this[i]&bm)<<cbs;
282 r[i-ds] = this[i]>>bs;
283 }
284 if(bs > 0) r[this.t-ds-1] |= (this.s&bm)<<cbs;
285 r.t = this.t-ds;
286 r._clamp();
287 }
288
289 // (protected) r = this - a
290 function bnpSubTo(a,r) {
291 var i = 0, c = 0, m = Math.min(a.t,this.t);
292 while(i < m) {
293 c += this[i]-a[i];
294 r[i++] = c&this._DM;
295 c >>= this._DB;
296 }
297 if(a.t < this.t) {
298 c -= a.s;
299 while(i < this.t) {
300 c += this[i];
301 r[i++] = c&this._DM;
302 c >>= this._DB;
303 }
304 c += this.s;
305 }
306 else {
307 c += this.s;
308 while(i < a.t) {
309 c -= a[i];
310 r[i++] = c&this._DM;
311 c >>= this._DB;
312 }
313 c -= a.s;
314 }
315 r.s = (c<0)?-1:0;
316 if(c < -1) r[i++] = this._DV+c;
317 else if(c > 0) r[i++] = c;
318 r.t = i;
319 r._clamp();
320 }
321
322 // (protected) r = this * a, r != this,a (HAC 14.12)
323 // "this" should be the larger one if appropriate.
324 function bnpMultiplyTo(a,r) {
325 var x = this.abs(), y = a.abs();
326 var i = x.t;
327 r.t = i+y.t;
328 while(--i >= 0) r[i] = 0;
329 for(i = 0; i < y.t; ++i) r[i+x.t] = x.am(0,y[i],r,i,0,x.t);
330 r.s = 0;
331 r._clamp();
332 if(this.s != a.s) BigInteger.ZERO._subTo(r,r);
333 }
334
335 // (protected) r = this^2, r != this (HAC 14.16)
336 function bnpSquareTo(r) {
337 var x = this.abs();
338 var i = r.t = 2*x.t;
339 while(--i >= 0) r[i] = 0;
340 for(i = 0; i < x.t-1; ++i) {
341 var c = x.am(i,x[i],r,2*i,0,1);
342 if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1)) >= x._DV) {
343 r[i+x.t] -= x._DV;
344 r[i+x.t+1] = 1;
345 }
346 }
347 if(r.t > 0) r[r.t-1] += x.am(i,x[i],r,2*i,0,1);
348 r.s = 0;
349 r._clamp();
350 }
351
352 // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
353 // r != q, this != m. q or r may be null.
354 function bnpDivRemTo(m,q,r) {
355 var pm = m.abs();
356 if(pm.t <= 0) return;
357 var pt = this.abs();
358 if(pt.t < pm.t) {
359 if(q != null) q._fromInt(0);
360 if(r != null) this._copyTo(r);
361 return;
362 }
363 if(r == null) r = nbi();
364 var y = nbi(), ts = this.s, ms = m.s;
365 var nsh = this._DB-nbits(pm[pm.t-1]); // normalize modulus
366 if(nsh > 0) { pm._lShiftTo(nsh,y); pt._lShiftTo(nsh,r); }
367 else { pm._copyTo(y); pt._copyTo(r); }
368 var ys = y.t;
369 var y0 = y[ys-1];
370 if(y0 == 0) return;
371 var yt = y0*(1<<this._F1)+((ys>1)?y[ys-2]>>this._F2:0);
372 var d1 = this._FV/yt, d2 = (1<<this._F1)/yt, e = 1<<this._F2;
373 var i = r.t, j = i-ys, t = (q==null)?nbi():q;
374 y._dlShiftTo(j,t);
375 if(r.compareTo(t) >= 0) {
376 r[r.t++] = 1;
377 r._subTo(t,r);
378 }
379 BigInteger.ONE._dlShiftTo(ys,t);
380 t._subTo(y,y); // "negative" y so we can replace sub with am later
381 while(y.t < ys) y[y.t++] = 0;
382 while(--j >= 0) {
383 // Estimate quotient digit
384 var qd = (r[--i]==y0)?this._DM:Math.floor(r[i]*d1+(r[i-1]+e)*d2);
385 if((r[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out
386 y._dlShiftTo(j,t);
387 r._subTo(t,r);
388 while(r[i] < --qd) r._subTo(t,r);
389 }
390 }
391 if(q != null) {
392 r._drShiftTo(ys,q);
393 if(ts != ms) BigInteger.ZERO._subTo(q,q);
394 }
395 r.t = ys;
396 r._clamp();
397 if(nsh > 0) r._rShiftTo(nsh,r); // Denormalize remainder
398 if(ts < 0) BigInteger.ZERO._subTo(r,r);
399 }
400
401 // (public) this mod a
402 function bnMod(a) {
403 var r = nbi();
404 this.abs()._divRemTo(a,null,r);
405 if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a._subTo(r,r);
406 return r;
407 }
408
409 // Modular reduction using "classic" algorithm
410 function Classic(m) { this.m = m; }
411 function cConvert(x) {
412 if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
413 else return x;
414 }
415 function cRevert(x) { return x; }
416 function cReduce(x) { x._divRemTo(this.m,null,x); }
417 function cMulTo(x,y,r) { x._multiplyTo(y,r); this.reduce(r); }
418 function cSqrTo(x,r) { x._squareTo(r); this.reduce(r); }
419
420 dojo.extend(Classic, {
421 convert: cConvert,
422 revert: cRevert,
423 reduce: cReduce,
424 mulTo: cMulTo,
425 sqrTo: cSqrTo
426 });
427
428 // (protected) return "-1/this % 2^DB"; useful for Mont. reduction
429 // justification:
430 // xy == 1 (mod m)
431 // xy = 1+km
432 // xy(2-xy) = (1+km)(1-km)
433 // x[y(2-xy)] = 1-k^2m^2
434 // x[y(2-xy)] == 1 (mod m^2)
435 // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
436 // should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
437 // JS multiply "overflows" differently from C/C++, so care is needed here.
438 function bnpInvDigit() {
439 if(this.t < 1) return 0;
440 var x = this[0];
441 if((x&1) == 0) return 0;
442 var y = x&3; // y == 1/x mod 2^2
443 y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4
444 y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8
445 y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff; // y == 1/x mod 2^16
446 // last step - calculate inverse mod DV directly;
447 // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
448 y = (y*(2-x*y%this._DV))%this._DV; // y == 1/x mod 2^dbits
449 // we really want the negative inverse, and -DV < y < DV
450 return (y>0)?this._DV-y:-y;
451 }
452
453 // Montgomery reduction
454 function Montgomery(m) {
455 this.m = m;
456 this.mp = m._invDigit();
457 this.mpl = this.mp&0x7fff;
458 this.mph = this.mp>>15;
459 this.um = (1<<(m._DB-15))-1;
460 this.mt2 = 2*m.t;
461 }
462
463 // xR mod m
464 function montConvert(x) {
465 var r = nbi();
466 x.abs()._dlShiftTo(this.m.t,r);
467 r._divRemTo(this.m,null,r);
468 if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m._subTo(r,r);
469 return r;
470 }
471
472 // x/R mod m
473 function montRevert(x) {
474 var r = nbi();
475 x._copyTo(r);
476 this.reduce(r);
477 return r;
478 }
479
480 // x = x/R mod m (HAC 14.32)
481 function montReduce(x) {
482 while(x.t <= this.mt2) // pad x so am has enough room later
483 x[x.t++] = 0;
484 for(var i = 0; i < this.m.t; ++i) {
485 // faster way of calculating u0 = x[i]*mp mod DV
486 var j = x[i]&0x7fff;
487 var u0 = (j*this.mpl+(((j*this.mph+(x[i]>>15)*this.mpl)&this.um)<<15))&x._DM;
488 // use am to combine the multiply-shift-add into one call
489 j = i+this.m.t;
490 x[j] += this.m.am(0,u0,x,i,0,this.m.t);
491 // propagate carry
492 while(x[j] >= x._DV) { x[j] -= x._DV; x[++j]++; }
493 }
494 x._clamp();
495 x._drShiftTo(this.m.t,x);
496 if(x.compareTo(this.m) >= 0) x._subTo(this.m,x);
497 }
498
499 // r = "x^2/R mod m"; x != r
500 function montSqrTo(x,r) { x._squareTo(r); this.reduce(r); }
501
502 // r = "xy/R mod m"; x,y != r
503 function montMulTo(x,y,r) { x._multiplyTo(y,r); this.reduce(r); }
504
505 dojo.extend(Montgomery, {
506 convert: montConvert,
507 revert: montRevert,
508 reduce: montReduce,
509 mulTo: montMulTo,
510 sqrTo: montSqrTo
511 });
512
513 // (protected) true iff this is even
514 function bnpIsEven() { return ((this.t>0)?(this[0]&1):this.s) == 0; }
515
516 // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
517 function bnpExp(e,z) {
518 if(e > 0xffffffff || e < 1) return BigInteger.ONE;
519 var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1;
520 g._copyTo(r);
521 while(--i >= 0) {
522 z.sqrTo(r,r2);
523 if((e&(1<<i)) > 0) z.mulTo(r2,g,r);
524 else { var t = r; r = r2; r2 = t; }
525 }
526 return z.revert(r);
527 }
528
529 // (public) this^e % m, 0 <= e < 2^32
530 function bnModPowInt(e,m) {
531 var z;
532 if(e < 256 || m._isEven()) z = new Classic(m); else z = new Montgomery(m);
533 return this._exp(e,z);
534 }
535
536 dojo.extend(BigInteger, {
537 // protected, not part of the official API
538 _DB: dbits,
539 _DM: (1 << dbits) - 1,
540 _DV: 1 << dbits,
541
542 _FV: Math.pow(2, BI_FP),
543 _F1: BI_FP - dbits,
544 _F2: 2 * dbits-BI_FP,
545
546 // protected
547 _copyTo: bnpCopyTo,
548 _fromInt: bnpFromInt,
549 _fromString: bnpFromString,
550 _clamp: bnpClamp,
551 _dlShiftTo: bnpDLShiftTo,
552 _drShiftTo: bnpDRShiftTo,
553 _lShiftTo: bnpLShiftTo,
554 _rShiftTo: bnpRShiftTo,
555 _subTo: bnpSubTo,
556 _multiplyTo: bnpMultiplyTo,
557 _squareTo: bnpSquareTo,
558 _divRemTo: bnpDivRemTo,
559 _invDigit: bnpInvDigit,
560 _isEven: bnpIsEven,
561 _exp: bnpExp,
562 _intAt: intAt,
563
564 // public
565 toString: bnToString,
566 negate: bnNegate,
567 abs: bnAbs,
568 compareTo: bnCompareTo,
569 bitLength: bnBitLength,
570 mod: bnMod,
571 modPowInt: bnModPowInt
572 });
573
574 dojo._mixin(BigInteger, {
575 // "constants"
576 ZERO: nbv(0),
577 ONE: nbv(1),
578
579 // internal functions
580 _nbi: nbi,
581 _nbv: nbv,
582 _nbits: nbits,
583
584 // internal classes
585 _Montgomery: Montgomery
586 });
587
588 // export to DojoX
589 dojox.math.BigInteger = BigInteger;
590
591 return dojox.math.BigInteger;
592 });