BigInteger-ext.js 17 KB

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