rsasync-rails 13.2.11.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rsasync-rails.rb +11 -0
- data/lib/rsasync-rails/engine.rb +6 -0
- data/lib/rsasync-rails/railtie.rb +6 -0
- data/lib/rsasync-rails/version.rb +5 -0
- data/vendor/assets/javascripts/base64.js +71 -0
- data/vendor/assets/javascripts/jsbn.js +559 -0
- data/vendor/assets/javascripts/jsbn2.js +656 -0
- data/vendor/assets/javascripts/prng4.js +45 -0
- data/vendor/assets/javascripts/rng.js +68 -0
- data/vendor/assets/javascripts/rsa.js +112 -0
- data/vendor/assets/javascripts/rsa2.js +132 -0
- data/vendor/assets/javascripts/rsasync-rails.js +2 -0
- data/vendor/assets/javascripts/rsasync.js +152 -0
- data/vendor/assets/javascripts/ssh-format-rails.coffee +139 -0
- metadata +125 -0
@@ -0,0 +1,656 @@
|
|
1
|
+
// Copyright (c) 2005-2009 Tom Wu
|
2
|
+
// All Rights Reserved.
|
3
|
+
// See "LICENSE" for details.
|
4
|
+
|
5
|
+
// Extended JavaScript BN functions, required for RSA private ops.
|
6
|
+
|
7
|
+
// Version 1.1: new BigInteger("0", 10) returns "proper" zero
|
8
|
+
// Version 1.2: square() API, isProbablePrime fix
|
9
|
+
|
10
|
+
// (public)
|
11
|
+
function bnClone() { var r = nbi(); this.copyTo(r); return r; }
|
12
|
+
|
13
|
+
// (public) return value as integer
|
14
|
+
function bnIntValue() {
|
15
|
+
if(this.s < 0) {
|
16
|
+
if(this.t == 1) return this[0]-this.DV;
|
17
|
+
else if(this.t == 0) return -1;
|
18
|
+
}
|
19
|
+
else if(this.t == 1) return this[0];
|
20
|
+
else if(this.t == 0) return 0;
|
21
|
+
// assumes 16 < DB < 32
|
22
|
+
return ((this[1]&((1<<(32-this.DB))-1))<<this.DB)|this[0];
|
23
|
+
}
|
24
|
+
|
25
|
+
// (public) return value as byte
|
26
|
+
function bnByteValue() { return (this.t==0)?this.s:(this[0]<<24)>>24; }
|
27
|
+
|
28
|
+
// (public) return value as short (assumes DB>=16)
|
29
|
+
function bnShortValue() { return (this.t==0)?this.s:(this[0]<<16)>>16; }
|
30
|
+
|
31
|
+
// (protected) return x s.t. r^x < DV
|
32
|
+
function bnpChunkSize(r) { return Math.floor(Math.LN2*this.DB/Math.log(r)); }
|
33
|
+
|
34
|
+
// (public) 0 if this == 0, 1 if this > 0
|
35
|
+
function bnSigNum() {
|
36
|
+
if(this.s < 0) return -1;
|
37
|
+
else if(this.t <= 0 || (this.t == 1 && this[0] <= 0)) return 0;
|
38
|
+
else return 1;
|
39
|
+
}
|
40
|
+
|
41
|
+
// (protected) convert to radix string
|
42
|
+
function bnpToRadix(b) {
|
43
|
+
if(b == null) b = 10;
|
44
|
+
if(this.signum() == 0 || b < 2 || b > 36) return "0";
|
45
|
+
var cs = this.chunkSize(b);
|
46
|
+
var a = Math.pow(b,cs);
|
47
|
+
var d = nbv(a), y = nbi(), z = nbi(), r = "";
|
48
|
+
this.divRemTo(d,y,z);
|
49
|
+
while(y.signum() > 0) {
|
50
|
+
r = (a+z.intValue()).toString(b).substr(1) + r;
|
51
|
+
y.divRemTo(d,y,z);
|
52
|
+
}
|
53
|
+
return z.intValue().toString(b) + r;
|
54
|
+
}
|
55
|
+
|
56
|
+
// (protected) convert from radix string
|
57
|
+
function bnpFromRadix(s,b) {
|
58
|
+
this.fromInt(0);
|
59
|
+
if(b == null) b = 10;
|
60
|
+
var cs = this.chunkSize(b);
|
61
|
+
var d = Math.pow(b,cs), mi = false, j = 0, w = 0;
|
62
|
+
for(var i = 0; i < s.length; ++i) {
|
63
|
+
var x = intAt(s,i);
|
64
|
+
if(x < 0) {
|
65
|
+
if(s.charAt(i) == "-" && this.signum() == 0) mi = true;
|
66
|
+
continue;
|
67
|
+
}
|
68
|
+
w = b*w+x;
|
69
|
+
if(++j >= cs) {
|
70
|
+
this.dMultiply(d);
|
71
|
+
this.dAddOffset(w,0);
|
72
|
+
j = 0;
|
73
|
+
w = 0;
|
74
|
+
}
|
75
|
+
}
|
76
|
+
if(j > 0) {
|
77
|
+
this.dMultiply(Math.pow(b,j));
|
78
|
+
this.dAddOffset(w,0);
|
79
|
+
}
|
80
|
+
if(mi) BigInteger.ZERO.subTo(this,this);
|
81
|
+
}
|
82
|
+
|
83
|
+
// (protected) alternate constructor
|
84
|
+
function bnpFromNumber(a,b,c) {
|
85
|
+
if("number" == typeof b) {
|
86
|
+
// new BigInteger(int,int,RNG)
|
87
|
+
if(a < 2) this.fromInt(1);
|
88
|
+
else {
|
89
|
+
this.fromNumber(a,c);
|
90
|
+
if(!this.testBit(a-1)) // force MSB set
|
91
|
+
this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this);
|
92
|
+
if(this.isEven()) this.dAddOffset(1,0); // force odd
|
93
|
+
while(!this.isProbablePrime(b)) {
|
94
|
+
this.dAddOffset(2,0);
|
95
|
+
if(this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a-1),this);
|
96
|
+
}
|
97
|
+
}
|
98
|
+
}
|
99
|
+
else {
|
100
|
+
// new BigInteger(int,RNG)
|
101
|
+
var x = new Array(), t = a&7;
|
102
|
+
x.length = (a>>3)+1;
|
103
|
+
b.nextBytes(x);
|
104
|
+
if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0;
|
105
|
+
this.fromString(x,256);
|
106
|
+
}
|
107
|
+
}
|
108
|
+
|
109
|
+
// (public) convert to bigendian byte array
|
110
|
+
function bnToByteArray() {
|
111
|
+
var i = this.t, r = new Array();
|
112
|
+
r[0] = this.s;
|
113
|
+
var p = this.DB-(i*this.DB)%8, d, k = 0;
|
114
|
+
if(i-- > 0) {
|
115
|
+
if(p < this.DB && (d = this[i]>>p) != (this.s&this.DM)>>p)
|
116
|
+
r[k++] = d|(this.s<<(this.DB-p));
|
117
|
+
while(i >= 0) {
|
118
|
+
if(p < 8) {
|
119
|
+
d = (this[i]&((1<<p)-1))<<(8-p);
|
120
|
+
d |= this[--i]>>(p+=this.DB-8);
|
121
|
+
}
|
122
|
+
else {
|
123
|
+
d = (this[i]>>(p-=8))&0xff;
|
124
|
+
if(p <= 0) { p += this.DB; --i; }
|
125
|
+
}
|
126
|
+
if((d&0x80) != 0) d |= -256;
|
127
|
+
if(k == 0 && (this.s&0x80) != (d&0x80)) ++k;
|
128
|
+
if(k > 0 || d != this.s) r[k++] = d;
|
129
|
+
}
|
130
|
+
}
|
131
|
+
return r;
|
132
|
+
}
|
133
|
+
|
134
|
+
function bnEquals(a) { return(this.compareTo(a)==0); }
|
135
|
+
function bnMin(a) { return(this.compareTo(a)<0)?this:a; }
|
136
|
+
function bnMax(a) { return(this.compareTo(a)>0)?this:a; }
|
137
|
+
|
138
|
+
// (protected) r = this op a (bitwise)
|
139
|
+
function bnpBitwiseTo(a,op,r) {
|
140
|
+
var i, f, m = Math.min(a.t,this.t);
|
141
|
+
for(i = 0; i < m; ++i) r[i] = op(this[i],a[i]);
|
142
|
+
if(a.t < this.t) {
|
143
|
+
f = a.s&this.DM;
|
144
|
+
for(i = m; i < this.t; ++i) r[i] = op(this[i],f);
|
145
|
+
r.t = this.t;
|
146
|
+
}
|
147
|
+
else {
|
148
|
+
f = this.s&this.DM;
|
149
|
+
for(i = m; i < a.t; ++i) r[i] = op(f,a[i]);
|
150
|
+
r.t = a.t;
|
151
|
+
}
|
152
|
+
r.s = op(this.s,a.s);
|
153
|
+
r.clamp();
|
154
|
+
}
|
155
|
+
|
156
|
+
// (public) this & a
|
157
|
+
function op_and(x,y) { return x&y; }
|
158
|
+
function bnAnd(a) { var r = nbi(); this.bitwiseTo(a,op_and,r); return r; }
|
159
|
+
|
160
|
+
// (public) this | a
|
161
|
+
function op_or(x,y) { return x|y; }
|
162
|
+
function bnOr(a) { var r = nbi(); this.bitwiseTo(a,op_or,r); return r; }
|
163
|
+
|
164
|
+
// (public) this ^ a
|
165
|
+
function op_xor(x,y) { return x^y; }
|
166
|
+
function bnXor(a) { var r = nbi(); this.bitwiseTo(a,op_xor,r); return r; }
|
167
|
+
|
168
|
+
// (public) this & ~a
|
169
|
+
function op_andnot(x,y) { return x&~y; }
|
170
|
+
function bnAndNot(a) { var r = nbi(); this.bitwiseTo(a,op_andnot,r); return r; }
|
171
|
+
|
172
|
+
// (public) ~this
|
173
|
+
function bnNot() {
|
174
|
+
var r = nbi();
|
175
|
+
for(var i = 0; i < this.t; ++i) r[i] = this.DM&~this[i];
|
176
|
+
r.t = this.t;
|
177
|
+
r.s = ~this.s;
|
178
|
+
return r;
|
179
|
+
}
|
180
|
+
|
181
|
+
// (public) this << n
|
182
|
+
function bnShiftLeft(n) {
|
183
|
+
var r = nbi();
|
184
|
+
if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r);
|
185
|
+
return r;
|
186
|
+
}
|
187
|
+
|
188
|
+
// (public) this >> n
|
189
|
+
function bnShiftRight(n) {
|
190
|
+
var r = nbi();
|
191
|
+
if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r);
|
192
|
+
return r;
|
193
|
+
}
|
194
|
+
|
195
|
+
// return index of lowest 1-bit in x, x < 2^31
|
196
|
+
function lbit(x) {
|
197
|
+
if(x == 0) return -1;
|
198
|
+
var r = 0;
|
199
|
+
if((x&0xffff) == 0) { x >>= 16; r += 16; }
|
200
|
+
if((x&0xff) == 0) { x >>= 8; r += 8; }
|
201
|
+
if((x&0xf) == 0) { x >>= 4; r += 4; }
|
202
|
+
if((x&3) == 0) { x >>= 2; r += 2; }
|
203
|
+
if((x&1) == 0) ++r;
|
204
|
+
return r;
|
205
|
+
}
|
206
|
+
|
207
|
+
// (public) returns index of lowest 1-bit (or -1 if none)
|
208
|
+
function bnGetLowestSetBit() {
|
209
|
+
for(var i = 0; i < this.t; ++i)
|
210
|
+
if(this[i] != 0) return i*this.DB+lbit(this[i]);
|
211
|
+
if(this.s < 0) return this.t*this.DB;
|
212
|
+
return -1;
|
213
|
+
}
|
214
|
+
|
215
|
+
// return number of 1 bits in x
|
216
|
+
function cbit(x) {
|
217
|
+
var r = 0;
|
218
|
+
while(x != 0) { x &= x-1; ++r; }
|
219
|
+
return r;
|
220
|
+
}
|
221
|
+
|
222
|
+
// (public) return number of set bits
|
223
|
+
function bnBitCount() {
|
224
|
+
var r = 0, x = this.s&this.DM;
|
225
|
+
for(var i = 0; i < this.t; ++i) r += cbit(this[i]^x);
|
226
|
+
return r;
|
227
|
+
}
|
228
|
+
|
229
|
+
// (public) true iff nth bit is set
|
230
|
+
function bnTestBit(n) {
|
231
|
+
var j = Math.floor(n/this.DB);
|
232
|
+
if(j >= this.t) return(this.s!=0);
|
233
|
+
return((this[j]&(1<<(n%this.DB)))!=0);
|
234
|
+
}
|
235
|
+
|
236
|
+
// (protected) this op (1<<n)
|
237
|
+
function bnpChangeBit(n,op) {
|
238
|
+
var r = BigInteger.ONE.shiftLeft(n);
|
239
|
+
this.bitwiseTo(r,op,r);
|
240
|
+
return r;
|
241
|
+
}
|
242
|
+
|
243
|
+
// (public) this | (1<<n)
|
244
|
+
function bnSetBit(n) { return this.changeBit(n,op_or); }
|
245
|
+
|
246
|
+
// (public) this & ~(1<<n)
|
247
|
+
function bnClearBit(n) { return this.changeBit(n,op_andnot); }
|
248
|
+
|
249
|
+
// (public) this ^ (1<<n)
|
250
|
+
function bnFlipBit(n) { return this.changeBit(n,op_xor); }
|
251
|
+
|
252
|
+
// (protected) r = this + a
|
253
|
+
function bnpAddTo(a,r) {
|
254
|
+
var i = 0, c = 0, m = Math.min(a.t,this.t);
|
255
|
+
while(i < m) {
|
256
|
+
c += this[i]+a[i];
|
257
|
+
r[i++] = c&this.DM;
|
258
|
+
c >>= this.DB;
|
259
|
+
}
|
260
|
+
if(a.t < this.t) {
|
261
|
+
c += a.s;
|
262
|
+
while(i < this.t) {
|
263
|
+
c += this[i];
|
264
|
+
r[i++] = c&this.DM;
|
265
|
+
c >>= this.DB;
|
266
|
+
}
|
267
|
+
c += this.s;
|
268
|
+
}
|
269
|
+
else {
|
270
|
+
c += this.s;
|
271
|
+
while(i < a.t) {
|
272
|
+
c += a[i];
|
273
|
+
r[i++] = c&this.DM;
|
274
|
+
c >>= this.DB;
|
275
|
+
}
|
276
|
+
c += a.s;
|
277
|
+
}
|
278
|
+
r.s = (c<0)?-1:0;
|
279
|
+
if(c > 0) r[i++] = c;
|
280
|
+
else if(c < -1) r[i++] = this.DV+c;
|
281
|
+
r.t = i;
|
282
|
+
r.clamp();
|
283
|
+
}
|
284
|
+
|
285
|
+
// (public) this + a
|
286
|
+
function bnAdd(a) { var r = nbi(); this.addTo(a,r); return r; }
|
287
|
+
|
288
|
+
// (public) this - a
|
289
|
+
function bnSubtract(a) { var r = nbi(); this.subTo(a,r); return r; }
|
290
|
+
|
291
|
+
// (public) this * a
|
292
|
+
function bnMultiply(a) { var r = nbi(); this.multiplyTo(a,r); return r; }
|
293
|
+
|
294
|
+
// (public) this^2
|
295
|
+
function bnSquare() { var r = nbi(); this.squareTo(r); return r; }
|
296
|
+
|
297
|
+
// (public) this / a
|
298
|
+
function bnDivide(a) { var r = nbi(); this.divRemTo(a,r,null); return r; }
|
299
|
+
|
300
|
+
// (public) this % a
|
301
|
+
function bnRemainder(a) { var r = nbi(); this.divRemTo(a,null,r); return r; }
|
302
|
+
|
303
|
+
// (public) [this/a,this%a]
|
304
|
+
function bnDivideAndRemainder(a) {
|
305
|
+
var q = nbi(), r = nbi();
|
306
|
+
this.divRemTo(a,q,r);
|
307
|
+
return new Array(q,r);
|
308
|
+
}
|
309
|
+
|
310
|
+
// (protected) this *= n, this >= 0, 1 < n < DV
|
311
|
+
function bnpDMultiply(n) {
|
312
|
+
this[this.t] = this.am(0,n-1,this,0,0,this.t);
|
313
|
+
++this.t;
|
314
|
+
this.clamp();
|
315
|
+
}
|
316
|
+
|
317
|
+
// (protected) this += n << w words, this >= 0
|
318
|
+
function bnpDAddOffset(n,w) {
|
319
|
+
if(n == 0) return;
|
320
|
+
while(this.t <= w) this[this.t++] = 0;
|
321
|
+
this[w] += n;
|
322
|
+
while(this[w] >= this.DV) {
|
323
|
+
this[w] -= this.DV;
|
324
|
+
if(++w >= this.t) this[this.t++] = 0;
|
325
|
+
++this[w];
|
326
|
+
}
|
327
|
+
}
|
328
|
+
|
329
|
+
// A "null" reducer
|
330
|
+
function NullExp() {}
|
331
|
+
function nNop(x) { return x; }
|
332
|
+
function nMulTo(x,y,r) { x.multiplyTo(y,r); }
|
333
|
+
function nSqrTo(x,r) { x.squareTo(r); }
|
334
|
+
|
335
|
+
NullExp.prototype.convert = nNop;
|
336
|
+
NullExp.prototype.revert = nNop;
|
337
|
+
NullExp.prototype.mulTo = nMulTo;
|
338
|
+
NullExp.prototype.sqrTo = nSqrTo;
|
339
|
+
|
340
|
+
// (public) this^e
|
341
|
+
function bnPow(e) { return this.exp(e,new NullExp()); }
|
342
|
+
|
343
|
+
// (protected) r = lower n words of "this * a", a.t <= n
|
344
|
+
// "this" should be the larger one if appropriate.
|
345
|
+
function bnpMultiplyLowerTo(a,n,r) {
|
346
|
+
var i = Math.min(this.t+a.t,n);
|
347
|
+
r.s = 0; // assumes a,this >= 0
|
348
|
+
r.t = i;
|
349
|
+
while(i > 0) r[--i] = 0;
|
350
|
+
var j;
|
351
|
+
for(j = r.t-this.t; i < j; ++i) r[i+this.t] = this.am(0,a[i],r,i,0,this.t);
|
352
|
+
for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a[i],r,i,0,n-i);
|
353
|
+
r.clamp();
|
354
|
+
}
|
355
|
+
|
356
|
+
// (protected) r = "this * a" without lower n words, n > 0
|
357
|
+
// "this" should be the larger one if appropriate.
|
358
|
+
function bnpMultiplyUpperTo(a,n,r) {
|
359
|
+
--n;
|
360
|
+
var i = r.t = this.t+a.t-n;
|
361
|
+
r.s = 0; // assumes a,this >= 0
|
362
|
+
while(--i >= 0) r[i] = 0;
|
363
|
+
for(i = Math.max(n-this.t,0); i < a.t; ++i)
|
364
|
+
r[this.t+i-n] = this.am(n-i,a[i],r,0,0,this.t+i-n);
|
365
|
+
r.clamp();
|
366
|
+
r.drShiftTo(1,r);
|
367
|
+
}
|
368
|
+
|
369
|
+
// Barrett modular reduction
|
370
|
+
function Barrett(m) {
|
371
|
+
// setup Barrett
|
372
|
+
this.r2 = nbi();
|
373
|
+
this.q3 = nbi();
|
374
|
+
BigInteger.ONE.dlShiftTo(2*m.t,this.r2);
|
375
|
+
this.mu = this.r2.divide(m);
|
376
|
+
this.m = m;
|
377
|
+
}
|
378
|
+
|
379
|
+
function barrettConvert(x) {
|
380
|
+
if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m);
|
381
|
+
else if(x.compareTo(this.m) < 0) return x;
|
382
|
+
else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; }
|
383
|
+
}
|
384
|
+
|
385
|
+
function barrettRevert(x) { return x; }
|
386
|
+
|
387
|
+
// x = x mod m (HAC 14.42)
|
388
|
+
function barrettReduce(x) {
|
389
|
+
x.drShiftTo(this.m.t-1,this.r2);
|
390
|
+
if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); }
|
391
|
+
this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3);
|
392
|
+
this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);
|
393
|
+
while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1);
|
394
|
+
x.subTo(this.r2,x);
|
395
|
+
while(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
|
396
|
+
}
|
397
|
+
|
398
|
+
// r = x^2 mod m; x != r
|
399
|
+
function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
|
400
|
+
|
401
|
+
// r = x*y mod m; x,y != r
|
402
|
+
function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
|
403
|
+
|
404
|
+
Barrett.prototype.convert = barrettConvert;
|
405
|
+
Barrett.prototype.revert = barrettRevert;
|
406
|
+
Barrett.prototype.reduce = barrettReduce;
|
407
|
+
Barrett.prototype.mulTo = barrettMulTo;
|
408
|
+
Barrett.prototype.sqrTo = barrettSqrTo;
|
409
|
+
|
410
|
+
// (public) this^e % m (HAC 14.85)
|
411
|
+
function bnModPow(e,m) {
|
412
|
+
var i = e.bitLength(), k, r = nbv(1), z;
|
413
|
+
if(i <= 0) return r;
|
414
|
+
else if(i < 18) k = 1;
|
415
|
+
else if(i < 48) k = 3;
|
416
|
+
else if(i < 144) k = 4;
|
417
|
+
else if(i < 768) k = 5;
|
418
|
+
else k = 6;
|
419
|
+
if(i < 8)
|
420
|
+
z = new Classic(m);
|
421
|
+
else if(m.isEven())
|
422
|
+
z = new Barrett(m);
|
423
|
+
else
|
424
|
+
z = new Montgomery(m);
|
425
|
+
|
426
|
+
// precomputation
|
427
|
+
var g = new Array(), n = 3, k1 = k-1, km = (1<<k)-1;
|
428
|
+
g[1] = z.convert(this);
|
429
|
+
if(k > 1) {
|
430
|
+
var g2 = nbi();
|
431
|
+
z.sqrTo(g[1],g2);
|
432
|
+
while(n <= km) {
|
433
|
+
g[n] = nbi();
|
434
|
+
z.mulTo(g2,g[n-2],g[n]);
|
435
|
+
n += 2;
|
436
|
+
}
|
437
|
+
}
|
438
|
+
|
439
|
+
var j = e.t-1, w, is1 = true, r2 = nbi(), t;
|
440
|
+
i = nbits(e[j])-1;
|
441
|
+
while(j >= 0) {
|
442
|
+
if(i >= k1) w = (e[j]>>(i-k1))&km;
|
443
|
+
else {
|
444
|
+
w = (e[j]&((1<<(i+1))-1))<<(k1-i);
|
445
|
+
if(j > 0) w |= e[j-1]>>(this.DB+i-k1);
|
446
|
+
}
|
447
|
+
|
448
|
+
n = k;
|
449
|
+
while((w&1) == 0) { w >>= 1; --n; }
|
450
|
+
if((i -= n) < 0) { i += this.DB; --j; }
|
451
|
+
if(is1) { // ret == 1, don't bother squaring or multiplying it
|
452
|
+
g[w].copyTo(r);
|
453
|
+
is1 = false;
|
454
|
+
}
|
455
|
+
else {
|
456
|
+
while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; }
|
457
|
+
if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; }
|
458
|
+
z.mulTo(r2,g[w],r);
|
459
|
+
}
|
460
|
+
|
461
|
+
while(j >= 0 && (e[j]&(1<<i)) == 0) {
|
462
|
+
z.sqrTo(r,r2); t = r; r = r2; r2 = t;
|
463
|
+
if(--i < 0) { i = this.DB-1; --j; }
|
464
|
+
}
|
465
|
+
}
|
466
|
+
return z.revert(r);
|
467
|
+
}
|
468
|
+
|
469
|
+
// (public) gcd(this,a) (HAC 14.54)
|
470
|
+
function bnGCD(a) {
|
471
|
+
var x = (this.s<0)?this.negate():this.clone();
|
472
|
+
var y = (a.s<0)?a.negate():a.clone();
|
473
|
+
if(x.compareTo(y) < 0) { var t = x; x = y; y = t; }
|
474
|
+
var i = x.getLowestSetBit(), g = y.getLowestSetBit();
|
475
|
+
if(g < 0) return x;
|
476
|
+
if(i < g) g = i;
|
477
|
+
if(g > 0) {
|
478
|
+
x.rShiftTo(g,x);
|
479
|
+
y.rShiftTo(g,y);
|
480
|
+
}
|
481
|
+
while(x.signum() > 0) {
|
482
|
+
if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x);
|
483
|
+
if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y);
|
484
|
+
if(x.compareTo(y) >= 0) {
|
485
|
+
x.subTo(y,x);
|
486
|
+
x.rShiftTo(1,x);
|
487
|
+
}
|
488
|
+
else {
|
489
|
+
y.subTo(x,y);
|
490
|
+
y.rShiftTo(1,y);
|
491
|
+
}
|
492
|
+
}
|
493
|
+
if(g > 0) y.lShiftTo(g,y);
|
494
|
+
return y;
|
495
|
+
}
|
496
|
+
|
497
|
+
// (protected) this % n, n < 2^26
|
498
|
+
function bnpModInt(n) {
|
499
|
+
if(n <= 0) return 0;
|
500
|
+
var d = this.DV%n, r = (this.s<0)?n-1:0;
|
501
|
+
if(this.t > 0)
|
502
|
+
if(d == 0) r = this[0]%n;
|
503
|
+
else for(var i = this.t-1; i >= 0; --i) r = (d*r+this[i])%n;
|
504
|
+
return r;
|
505
|
+
}
|
506
|
+
|
507
|
+
// (public) 1/this % m (HAC 14.61)
|
508
|
+
function bnModInverse(m) {
|
509
|
+
var ac = m.isEven();
|
510
|
+
if((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO;
|
511
|
+
var u = m.clone(), v = this.clone();
|
512
|
+
var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1);
|
513
|
+
while(u.signum() != 0) {
|
514
|
+
while(u.isEven()) {
|
515
|
+
u.rShiftTo(1,u);
|
516
|
+
if(ac) {
|
517
|
+
if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); }
|
518
|
+
a.rShiftTo(1,a);
|
519
|
+
}
|
520
|
+
else if(!b.isEven()) b.subTo(m,b);
|
521
|
+
b.rShiftTo(1,b);
|
522
|
+
}
|
523
|
+
while(v.isEven()) {
|
524
|
+
v.rShiftTo(1,v);
|
525
|
+
if(ac) {
|
526
|
+
if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); }
|
527
|
+
c.rShiftTo(1,c);
|
528
|
+
}
|
529
|
+
else if(!d.isEven()) d.subTo(m,d);
|
530
|
+
d.rShiftTo(1,d);
|
531
|
+
}
|
532
|
+
if(u.compareTo(v) >= 0) {
|
533
|
+
u.subTo(v,u);
|
534
|
+
if(ac) a.subTo(c,a);
|
535
|
+
b.subTo(d,b);
|
536
|
+
}
|
537
|
+
else {
|
538
|
+
v.subTo(u,v);
|
539
|
+
if(ac) c.subTo(a,c);
|
540
|
+
d.subTo(b,d);
|
541
|
+
}
|
542
|
+
}
|
543
|
+
if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
|
544
|
+
if(d.compareTo(m) >= 0) return d.subtract(m);
|
545
|
+
if(d.signum() < 0) d.addTo(m,d); else return d;
|
546
|
+
if(d.signum() < 0) return d.add(m); else return d;
|
547
|
+
}
|
548
|
+
|
549
|
+
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];
|
550
|
+
var lplim = (1<<26)/lowprimes[lowprimes.length-1];
|
551
|
+
|
552
|
+
// (public) test primality with certainty >= 1-.5^t
|
553
|
+
function bnIsProbablePrime(t) {
|
554
|
+
var i, x = this.abs();
|
555
|
+
if(x.t == 1 && x[0] <= lowprimes[lowprimes.length-1]) {
|
556
|
+
for(i = 0; i < lowprimes.length; ++i)
|
557
|
+
if(x[0] == lowprimes[i]) return true;
|
558
|
+
return false;
|
559
|
+
}
|
560
|
+
if(x.isEven()) return false;
|
561
|
+
i = 1;
|
562
|
+
while(i < lowprimes.length) {
|
563
|
+
var m = lowprimes[i], j = i+1;
|
564
|
+
while(j < lowprimes.length && m < lplim) m *= lowprimes[j++];
|
565
|
+
m = x.modInt(m);
|
566
|
+
while(i < j) if(m%lowprimes[i++] == 0) return false;
|
567
|
+
}
|
568
|
+
return x.millerRabin(t);
|
569
|
+
}
|
570
|
+
|
571
|
+
// (protected) true if probably prime (HAC 4.24, Miller-Rabin)
|
572
|
+
function bnpMillerRabin(t) {
|
573
|
+
var n1 = this.subtract(BigInteger.ONE);
|
574
|
+
var k = n1.getLowestSetBit();
|
575
|
+
if(k <= 0) return false;
|
576
|
+
var r = n1.shiftRight(k);
|
577
|
+
t = (t+1)>>1;
|
578
|
+
if(t > lowprimes.length) t = lowprimes.length;
|
579
|
+
var a = nbi();
|
580
|
+
for(var i = 0; i < t; ++i) {
|
581
|
+
//Pick bases at random, instead of starting at 2
|
582
|
+
a.fromInt(lowprimes[Math.floor(Math.random()*lowprimes.length)]);
|
583
|
+
var y = a.modPow(r,this);
|
584
|
+
if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
|
585
|
+
var j = 1;
|
586
|
+
while(j++ < k && y.compareTo(n1) != 0) {
|
587
|
+
y = y.modPowInt(2,this);
|
588
|
+
if(y.compareTo(BigInteger.ONE) == 0) return false;
|
589
|
+
}
|
590
|
+
if(y.compareTo(n1) != 0) return false;
|
591
|
+
}
|
592
|
+
}
|
593
|
+
return true;
|
594
|
+
}
|
595
|
+
|
596
|
+
// protected
|
597
|
+
BigInteger.prototype.chunkSize = bnpChunkSize;
|
598
|
+
BigInteger.prototype.toRadix = bnpToRadix;
|
599
|
+
BigInteger.prototype.fromRadix = bnpFromRadix;
|
600
|
+
BigInteger.prototype.fromNumber = bnpFromNumber;
|
601
|
+
BigInteger.prototype.bitwiseTo = bnpBitwiseTo;
|
602
|
+
BigInteger.prototype.changeBit = bnpChangeBit;
|
603
|
+
BigInteger.prototype.addTo = bnpAddTo;
|
604
|
+
BigInteger.prototype.dMultiply = bnpDMultiply;
|
605
|
+
BigInteger.prototype.dAddOffset = bnpDAddOffset;
|
606
|
+
BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo;
|
607
|
+
BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
|
608
|
+
BigInteger.prototype.modInt = bnpModInt;
|
609
|
+
BigInteger.prototype.millerRabin = bnpMillerRabin;
|
610
|
+
|
611
|
+
// public
|
612
|
+
BigInteger.prototype.clone = bnClone;
|
613
|
+
BigInteger.prototype.intValue = bnIntValue;
|
614
|
+
BigInteger.prototype.byteValue = bnByteValue;
|
615
|
+
BigInteger.prototype.shortValue = bnShortValue;
|
616
|
+
BigInteger.prototype.signum = bnSigNum;
|
617
|
+
BigInteger.prototype.toByteArray = bnToByteArray;
|
618
|
+
BigInteger.prototype.equals = bnEquals;
|
619
|
+
BigInteger.prototype.min = bnMin;
|
620
|
+
BigInteger.prototype.max = bnMax;
|
621
|
+
BigInteger.prototype.and = bnAnd;
|
622
|
+
BigInteger.prototype.or = bnOr;
|
623
|
+
BigInteger.prototype.xor = bnXor;
|
624
|
+
BigInteger.prototype.andNot = bnAndNot;
|
625
|
+
BigInteger.prototype.not = bnNot;
|
626
|
+
BigInteger.prototype.shiftLeft = bnShiftLeft;
|
627
|
+
BigInteger.prototype.shiftRight = bnShiftRight;
|
628
|
+
BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit;
|
629
|
+
BigInteger.prototype.bitCount = bnBitCount;
|
630
|
+
BigInteger.prototype.testBit = bnTestBit;
|
631
|
+
BigInteger.prototype.setBit = bnSetBit;
|
632
|
+
BigInteger.prototype.clearBit = bnClearBit;
|
633
|
+
BigInteger.prototype.flipBit = bnFlipBit;
|
634
|
+
BigInteger.prototype.add = bnAdd;
|
635
|
+
BigInteger.prototype.subtract = bnSubtract;
|
636
|
+
BigInteger.prototype.multiply = bnMultiply;
|
637
|
+
BigInteger.prototype.divide = bnDivide;
|
638
|
+
BigInteger.prototype.remainder = bnRemainder;
|
639
|
+
BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder;
|
640
|
+
BigInteger.prototype.modPow = bnModPow;
|
641
|
+
BigInteger.prototype.modInverse = bnModInverse;
|
642
|
+
BigInteger.prototype.pow = bnPow;
|
643
|
+
BigInteger.prototype.gcd = bnGCD;
|
644
|
+
BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
|
645
|
+
|
646
|
+
// JSBN-specific extension
|
647
|
+
BigInteger.prototype.square = bnSquare;
|
648
|
+
|
649
|
+
// BigInteger interfaces not implemented in jsbn:
|
650
|
+
|
651
|
+
// BigInteger(int signum, byte[] magnitude)
|
652
|
+
// double doubleValue()
|
653
|
+
// float floatValue()
|
654
|
+
// int hashCode()
|
655
|
+
// long longValue()
|
656
|
+
// static BigInteger valueOf(long val)
|