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