v8-crypto.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  1. /*
  2. * Copyright (c) 2003-2005 Tom Wu
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  18. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  19. *
  20. * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  21. * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  22. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  23. * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  24. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  25. *
  26. * In addition, the following condition applies:
  27. *
  28. * All redistributions must retain an intact copy of this copyright notice
  29. * and disclaimer.
  30. */
  31. // The code has been adapted for use as a benchmark by Google.
  32. // Basic JavaScript BN library - subset useful for RSA encryption.
  33. // Bits per digit
  34. var dbits;
  35. var BI_DB;
  36. var BI_DM;
  37. var BI_DV;
  38. var BI_FP;
  39. var BI_FV;
  40. var BI_F1;
  41. var BI_F2;
  42. // JavaScript engine analysis
  43. var canary = 0xdeadbeefcafe;
  44. var j_lm = ((canary&0xffffff)==0xefcafe);
  45. // (public) Constructor
  46. function BigInteger(a,b,c) {
  47. this.array = new Array();
  48. if(a != null)
  49. if("number" == typeof a) this.fromNumber(a,b,c);
  50. else if(b == null && "string" != typeof a) this.fromString(a,256);
  51. else this.fromString(a,b);
  52. }
  53. // return new, unset BigInteger
  54. function nbi() { return new BigInteger(null); }
  55. // am: Compute w_j += (x*this_i), propagate carries,
  56. // c is initial carry, returns final carry.
  57. // c < 3*dvalue, x < 2*dvalue, this_i < dvalue
  58. // We need to select the fastest one that works in this environment.
  59. // am1: use a single mult and divide to get the high bits,
  60. // max digit bits should be 26 because
  61. // max internal value = 2*dvalue^2-2*dvalue (< 2^53)
  62. function am1(i,x,w,j,c,n) {
  63. var this_array = this.array;
  64. var w_array = w.array;
  65. while(--n >= 0) {
  66. var v = x*this_array[i++]+w_array[j]+c;
  67. c = Math.floor(v/0x4000000);
  68. w_array[j++] = v&0x3ffffff;
  69. }
  70. return c;
  71. }
  72. // am2 avoids a big mult-and-extract completely.
  73. // Max digit bits should be <= 30 because we do bitwise ops
  74. // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
  75. function am2(i,x,w,j,c,n) {
  76. var this_array = this.array;
  77. var w_array = w.array;
  78. var xl = x&0x7fff, xh = x>>15;
  79. while(--n >= 0) {
  80. var l = this_array[i]&0x7fff;
  81. var h = this_array[i++]>>15;
  82. var m = xh*l+h*xl;
  83. l = xl*l+((m&0x7fff)<<15)+w_array[j]+(c&0x3fffffff);
  84. c = (l>>>30)+(m>>>15)+xh*h+(c>>>30);
  85. w_array[j++] = l&0x3fffffff;
  86. }
  87. return c;
  88. }
  89. // Alternately, set max digit bits to 28 since some
  90. // browsers slow down when dealing with 32-bit numbers.
  91. function am3(i,x,w,j,c,n) {
  92. var this_array = this.array;
  93. var w_array = w.array;
  94. var xl = x&0x3fff, xh = x>>14;
  95. while(--n >= 0) {
  96. var l = this_array[i]&0x3fff;
  97. var h = this_array[i++]>>14;
  98. var m = xh*l+h*xl;
  99. l = xl*l+((m&0x3fff)<<14)+w_array[j]+c;
  100. c = (l>>28)+(m>>14)+xh*h;
  101. w_array[j++] = l&0xfffffff;
  102. }
  103. return c;
  104. }
  105. // This is tailored to VMs with 2-bit tagging. It makes sure
  106. // that all the computations stay within the 29 bits available.
  107. function am4(i,x,w,j,c,n) {
  108. var this_array = this.array;
  109. var w_array = w.array;
  110. var xl = x&0x1fff, xh = x>>13;
  111. while(--n >= 0) {
  112. var l = this_array[i]&0x1fff;
  113. var h = this_array[i++]>>13;
  114. var m = xh*l+h*xl;
  115. l = xl*l+((m&0x1fff)<<13)+w_array[j]+c;
  116. c = (l>>26)+(m>>13)+xh*h;
  117. w_array[j++] = l&0x3ffffff;
  118. }
  119. return c;
  120. }
  121. // am3/28 is best for SM, Rhino, but am4/26 is best for v8.
  122. // Kestrel (Opera 9.5) gets its best result with am4/26.
  123. // IE7 does 9% better with am3/28 than with am4/26.
  124. // Firefox (SM) gets 10% faster with am3/28 than with am4/26.
  125. setupEngine = function(fn, bits) {
  126. BigInteger.prototype.am = fn;
  127. dbits = bits;
  128. BI_DB = dbits;
  129. BI_DM = ((1<<dbits)-1);
  130. BI_DV = (1<<dbits);
  131. BI_FP = 52;
  132. BI_FV = Math.pow(2,BI_FP);
  133. BI_F1 = BI_FP-dbits;
  134. BI_F2 = 2*dbits-BI_FP;
  135. }
  136. // Digit conversions
  137. var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
  138. var BI_RC = new Array();
  139. var rr,vv;
  140. rr = "0".charCodeAt(0);
  141. for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
  142. rr = "a".charCodeAt(0);
  143. for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
  144. rr = "A".charCodeAt(0);
  145. for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
  146. function int2char(n) { return BI_RM.charAt(n); }
  147. function intAt(s,i) {
  148. var c = BI_RC[s.charCodeAt(i)];
  149. return (c==null)?-1:c;
  150. }
  151. // (protected) copy this to r
  152. function bnpCopyTo(r) {
  153. var this_array = this.array;
  154. var r_array = r.array;
  155. for(var i = this.t-1; i >= 0; --i) r_array[i] = this_array[i];
  156. r.t = this.t;
  157. r.s = this.s;
  158. }
  159. // (protected) set from integer value x, -DV <= x < DV
  160. function bnpFromInt(x) {
  161. var this_array = this.array;
  162. this.t = 1;
  163. this.s = (x<0)?-1:0;
  164. if(x > 0) this_array[0] = x;
  165. else if(x < -1) this_array[0] = x+DV;
  166. else this.t = 0;
  167. }
  168. // return bigint initialized to value
  169. function nbv(i) { var r = nbi(); r.fromInt(i); return r; }
  170. // (protected) set from string and radix
  171. function bnpFromString(s,b) {
  172. var this_array = this.array;
  173. var k;
  174. if(b == 16) k = 4;
  175. else if(b == 8) k = 3;
  176. else if(b == 256) k = 8; // byte array
  177. else if(b == 2) k = 1;
  178. else if(b == 32) k = 5;
  179. else if(b == 4) k = 2;
  180. else { this.fromRadix(s,b); return; }
  181. this.t = 0;
  182. this.s = 0;
  183. var i = s.length, mi = false, sh = 0;
  184. while(--i >= 0) {
  185. var x = (k==8)?s[i]&0xff:intAt(s,i);
  186. if(x < 0) {
  187. if(s.charAt(i) == "-") mi = true;
  188. continue;
  189. }
  190. mi = false;
  191. if(sh == 0)
  192. this_array[this.t++] = x;
  193. else if(sh+k > BI_DB) {
  194. this_array[this.t-1] |= (x&((1<<(BI_DB-sh))-1))<<sh;
  195. this_array[this.t++] = (x>>(BI_DB-sh));
  196. }
  197. else
  198. this_array[this.t-1] |= x<<sh;
  199. sh += k;
  200. if(sh >= BI_DB) sh -= BI_DB;
  201. }
  202. if(k == 8 && (s[0]&0x80) != 0) {
  203. this.s = -1;
  204. if(sh > 0) this_array[this.t-1] |= ((1<<(BI_DB-sh))-1)<<sh;
  205. }
  206. this.clamp();
  207. if(mi) BigInteger.ZERO.subTo(this,this);
  208. }
  209. // (protected) clamp off excess high words
  210. function bnpClamp() {
  211. var this_array = this.array;
  212. var c = this.s&BI_DM;
  213. while(this.t > 0 && this_array[this.t-1] == c) --this.t;
  214. }
  215. // (public) return string representation in given radix
  216. function bnToString(b) {
  217. var this_array = this.array;
  218. if(this.s < 0) return "-"+this.negate().toString(b);
  219. var k;
  220. if(b == 16) k = 4;
  221. else if(b == 8) k = 3;
  222. else if(b == 2) k = 1;
  223. else if(b == 32) k = 5;
  224. else if(b == 4) k = 2;
  225. else return this.toRadix(b);
  226. var km = (1<<k)-1, d, m = false, r = "", i = this.t;
  227. var p = BI_DB-(i*BI_DB)%k;
  228. if(i-- > 0) {
  229. if(p < BI_DB && (d = this_array[i]>>p) > 0) { m = true; r = int2char(d); }
  230. while(i >= 0) {
  231. if(p < k) {
  232. d = (this_array[i]&((1<<p)-1))<<(k-p);
  233. d |= this_array[--i]>>(p+=BI_DB-k);
  234. }
  235. else {
  236. d = (this_array[i]>>(p-=k))&km;
  237. if(p <= 0) { p += BI_DB; --i; }
  238. }
  239. if(d > 0) m = true;
  240. if(m) r += int2char(d);
  241. }
  242. }
  243. return m?r:"0";
  244. }
  245. // (public) -this
  246. function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; }
  247. // (public) |this|
  248. function bnAbs() { return (this.s<0)?this.negate():this; }
  249. // (public) return + if this > a, - if this < a, 0 if equal
  250. function bnCompareTo(a) {
  251. var this_array = this.array;
  252. var a_array = a.array;
  253. var r = this.s-a.s;
  254. if(r != 0) return r;
  255. var i = this.t;
  256. r = i-a.t;
  257. if(r != 0) return r;
  258. while(--i >= 0) if((r=this_array[i]-a_array[i]) != 0) return r;
  259. return 0;
  260. }
  261. // returns bit length of the integer x
  262. function nbits(x) {
  263. var r = 1, t;
  264. if((t=x>>>16) != 0) { x = t; r += 16; }
  265. if((t=x>>8) != 0) { x = t; r += 8; }
  266. if((t=x>>4) != 0) { x = t; r += 4; }
  267. if((t=x>>2) != 0) { x = t; r += 2; }
  268. if((t=x>>1) != 0) { x = t; r += 1; }
  269. return r;
  270. }
  271. // (public) return the number of bits in "this"
  272. function bnBitLength() {
  273. var this_array = this.array;
  274. if(this.t <= 0) return 0;
  275. return BI_DB*(this.t-1)+nbits(this_array[this.t-1]^(this.s&BI_DM));
  276. }
  277. // (protected) r = this << n*DB
  278. function bnpDLShiftTo(n,r) {
  279. var this_array = this.array;
  280. var r_array = r.array;
  281. var i;
  282. for(i = this.t-1; i >= 0; --i) r_array[i+n] = this_array[i];
  283. for(i = n-1; i >= 0; --i) r_array[i] = 0;
  284. r.t = this.t+n;
  285. r.s = this.s;
  286. }
  287. // (protected) r = this >> n*DB
  288. function bnpDRShiftTo(n,r) {
  289. var this_array = this.array;
  290. var r_array = r.array;
  291. for(var i = n; i < this.t; ++i) r_array[i-n] = this_array[i];
  292. r.t = Math.max(this.t-n,0);
  293. r.s = this.s;
  294. }
  295. // (protected) r = this << n
  296. function bnpLShiftTo(n,r) {
  297. var this_array = this.array;
  298. var r_array = r.array;
  299. var bs = n%BI_DB;
  300. var cbs = BI_DB-bs;
  301. var bm = (1<<cbs)-1;
  302. var ds = Math.floor(n/BI_DB), c = (this.s<<bs)&BI_DM, i;
  303. for(i = this.t-1; i >= 0; --i) {
  304. r_array[i+ds+1] = (this_array[i]>>cbs)|c;
  305. c = (this_array[i]&bm)<<bs;
  306. }
  307. for(i = ds-1; i >= 0; --i) r_array[i] = 0;
  308. r_array[ds] = c;
  309. r.t = this.t+ds+1;
  310. r.s = this.s;
  311. r.clamp();
  312. }
  313. // (protected) r = this >> n
  314. function bnpRShiftTo(n,r) {
  315. var this_array = this.array;
  316. var r_array = r.array;
  317. r.s = this.s;
  318. var ds = Math.floor(n/BI_DB);
  319. if(ds >= this.t) { r.t = 0; return; }
  320. var bs = n%BI_DB;
  321. var cbs = BI_DB-bs;
  322. var bm = (1<<bs)-1;
  323. r_array[0] = this_array[ds]>>bs;
  324. for(var i = ds+1; i < this.t; ++i) {
  325. r_array[i-ds-1] |= (this_array[i]&bm)<<cbs;
  326. r_array[i-ds] = this_array[i]>>bs;
  327. }
  328. if(bs > 0) r_array[this.t-ds-1] |= (this.s&bm)<<cbs;
  329. r.t = this.t-ds;
  330. r.clamp();
  331. }
  332. // (protected) r = this - a
  333. function bnpSubTo(a,r) {
  334. var this_array = this.array;
  335. var r_array = r.array;
  336. var a_array = a.array;
  337. var i = 0, c = 0, m = Math.min(a.t,this.t);
  338. while(i < m) {
  339. c += this_array[i]-a_array[i];
  340. r_array[i++] = c&BI_DM;
  341. c >>= BI_DB;
  342. }
  343. if(a.t < this.t) {
  344. c -= a.s;
  345. while(i < this.t) {
  346. c += this_array[i];
  347. r_array[i++] = c&BI_DM;
  348. c >>= BI_DB;
  349. }
  350. c += this.s;
  351. }
  352. else {
  353. c += this.s;
  354. while(i < a.t) {
  355. c -= a_array[i];
  356. r_array[i++] = c&BI_DM;
  357. c >>= BI_DB;
  358. }
  359. c -= a.s;
  360. }
  361. r.s = (c<0)?-1:0;
  362. if(c < -1) r_array[i++] = BI_DV+c;
  363. else if(c > 0) r_array[i++] = c;
  364. r.t = i;
  365. r.clamp();
  366. }
  367. // (protected) r = this * a, r != this,a (HAC 14.12)
  368. // "this" should be the larger one if appropriate.
  369. function bnpMultiplyTo(a,r) {
  370. var this_array = this.array;
  371. var r_array = r.array;
  372. var x = this.abs(), y = a.abs();
  373. var y_array = y.array;
  374. var i = x.t;
  375. r.t = i+y.t;
  376. while(--i >= 0) r_array[i] = 0;
  377. for(i = 0; i < y.t; ++i) r_array[i+x.t] = x.am(0,y_array[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. // (protected) r = this^2, r != this (HAC 14.16)
  383. function bnpSquareTo(r) {
  384. var x = this.abs();
  385. var x_array = x.array;
  386. var r_array = r.array;
  387. var i = r.t = 2*x.t;
  388. while(--i >= 0) r_array[i] = 0;
  389. for(i = 0; i < x.t-1; ++i) {
  390. var c = x.am(i,x_array[i],r,2*i,0,1);
  391. if((r_array[i+x.t]+=x.am(i+1,2*x_array[i],r,2*i+1,c,x.t-i-1)) >= BI_DV) {
  392. r_array[i+x.t] -= BI_DV;
  393. r_array[i+x.t+1] = 1;
  394. }
  395. }
  396. if(r.t > 0) r_array[r.t-1] += x.am(i,x_array[i],r,2*i,0,1);
  397. r.s = 0;
  398. r.clamp();
  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 pm_array = pm.array;
  414. var nsh = BI_DB-nbits(pm_array[pm.t-1]); // normalize modulus
  415. if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); }
  416. else { pm.copyTo(y); pt.copyTo(r); }
  417. var ys = y.t;
  418. var y_array = y.array;
  419. var y0 = y_array[ys-1];
  420. if(y0 == 0) return;
  421. var yt = y0*(1<<BI_F1)+((ys>1)?y_array[ys-2]>>BI_F2:0);
  422. var d1 = BI_FV/yt, d2 = (1<<BI_F1)/yt, e = 1<<BI_F2;
  423. var i = r.t, j = i-ys, t = (q==null)?nbi():q;
  424. y.dlShiftTo(j,t);
  425. var r_array = r.array;
  426. if(r.compareTo(t) >= 0) {
  427. r_array[r.t++] = 1;
  428. r.subTo(t,r);
  429. }
  430. BigInteger.ONE.dlShiftTo(ys,t);
  431. t.subTo(y,y); // "negative" y so we can replace sub with am later
  432. while(y.t < ys) y_array[y.t++] = 0;
  433. while(--j >= 0) {
  434. // Estimate quotient digit
  435. var qd = (r_array[--i]==y0)?BI_DM:Math.floor(r_array[i]*d1+(r_array[i-1]+e)*d2);
  436. if((r_array[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out
  437. y.dlShiftTo(j,t);
  438. r.subTo(t,r);
  439. while(r_array[i] < --qd) r.subTo(t,r);
  440. }
  441. }
  442. if(q != null) {
  443. r.drShiftTo(ys,q);
  444. if(ts != ms) BigInteger.ZERO.subTo(q,q);
  445. }
  446. r.t = ys;
  447. r.clamp();
  448. if(nsh > 0) r.rShiftTo(nsh,r); // Denormalize remainder
  449. if(ts < 0) BigInteger.ZERO.subTo(r,r);
  450. }
  451. // (public) this mod a
  452. function bnMod(a) {
  453. var r = nbi();
  454. this.abs().divRemTo(a,null,r);
  455. if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r);
  456. return r;
  457. }
  458. // Modular reduction using "classic" algorithm
  459. function Classic(m) { this.m = m; }
  460. function cConvert(x) {
  461. if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
  462. else return x;
  463. }
  464. function cRevert(x) { return x; }
  465. function cReduce(x) { x.divRemTo(this.m,null,x); }
  466. function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
  467. function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
  468. Classic.prototype.convert = cConvert;
  469. Classic.prototype.revert = cRevert;
  470. Classic.prototype.reduce = cReduce;
  471. Classic.prototype.mulTo = cMulTo;
  472. Classic.prototype.sqrTo = cSqrTo;
  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. var this_array = this.array;
  485. if(this.t < 1) return 0;
  486. var x = this_array[0];
  487. if((x&1) == 0) return 0;
  488. var y = x&3; // y == 1/x mod 2^2
  489. y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4
  490. y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8
  491. y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff; // y == 1/x mod 2^16
  492. // last step - calculate inverse mod DV directly;
  493. // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
  494. y = (y*(2-x*y%BI_DV))%BI_DV; // y == 1/x mod 2^dbits
  495. // we really want the negative inverse, and -DV < y < DV
  496. return (y>0)?BI_DV-y:-y;
  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<<(BI_DB-15))-1;
  505. this.mt2 = 2*m.t;
  506. }
  507. // xR mod m
  508. function montConvert(x) {
  509. var r = nbi();
  510. x.abs().dlShiftTo(this.m.t,r);
  511. r.divRemTo(this.m,null,r);
  512. if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r);
  513. return r;
  514. }
  515. // x/R mod m
  516. function montRevert(x) {
  517. var r = nbi();
  518. x.copyTo(r);
  519. this.reduce(r);
  520. return r;
  521. }
  522. // x = x/R mod m (HAC 14.32)
  523. function montReduce(x) {
  524. var x_array = x.array;
  525. while(x.t <= this.mt2) // pad x so am has enough room later
  526. x_array[x.t++] = 0;
  527. for(var i = 0; i < this.m.t; ++i) {
  528. // faster way of calculating u0 = x[i]*mp mod DV
  529. var j = x_array[i]&0x7fff;
  530. var u0 = (j*this.mpl+(((j*this.mph+(x_array[i]>>15)*this.mpl)&this.um)<<15))&BI_DM;
  531. // use am to combine the multiply-shift-add into one call
  532. j = i+this.m.t;
  533. x_array[j] += this.m.am(0,u0,x,i,0,this.m.t);
  534. // propagate carry
  535. while(x_array[j] >= BI_DV) { x_array[j] -= BI_DV; x_array[++j]++; }
  536. }
  537. x.clamp();
  538. x.drShiftTo(this.m.t,x);
  539. if(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
  540. }
  541. // r = "x^2/R mod m"; x != r
  542. function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
  543. // r = "xy/R mod m"; x,y != r
  544. function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
  545. Montgomery.prototype.convert = montConvert;
  546. Montgomery.prototype.revert = montRevert;
  547. Montgomery.prototype.reduce = montReduce;
  548. Montgomery.prototype.mulTo = montMulTo;
  549. Montgomery.prototype.sqrTo = montSqrTo;
  550. // (protected) true iff this is even
  551. function bnpIsEven() {
  552. var this_array = this.array;
  553. return ((this.t>0)?(this_array[0]&1):this.s) == 0;
  554. }
  555. // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
  556. function bnpExp(e,z) {
  557. if(e > 0xffffffff || e < 1) return BigInteger.ONE;
  558. var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1;
  559. g.copyTo(r);
  560. while(--i >= 0) {
  561. z.sqrTo(r,r2);
  562. if((e&(1<<i)) > 0) z.mulTo(r2,g,r);
  563. else { var t = r; r = r2; r2 = t; }
  564. }
  565. return z.revert(r);
  566. }
  567. // (public) this^e % m, 0 <= e < 2^32
  568. function bnModPowInt(e,m) {
  569. var z;
  570. if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m);
  571. return this.exp(e,z);
  572. }
  573. // protected
  574. BigInteger.prototype.copyTo = bnpCopyTo;
  575. BigInteger.prototype.fromInt = bnpFromInt;
  576. BigInteger.prototype.fromString = bnpFromString;
  577. BigInteger.prototype.clamp = bnpClamp;
  578. BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
  579. BigInteger.prototype.drShiftTo = bnpDRShiftTo;
  580. BigInteger.prototype.lShiftTo = bnpLShiftTo;
  581. BigInteger.prototype.rShiftTo = bnpRShiftTo;
  582. BigInteger.prototype.subTo = bnpSubTo;
  583. BigInteger.prototype.multiplyTo = bnpMultiplyTo;
  584. BigInteger.prototype.squareTo = bnpSquareTo;
  585. BigInteger.prototype.divRemTo = bnpDivRemTo;
  586. BigInteger.prototype.invDigit = bnpInvDigit;
  587. BigInteger.prototype.isEven = bnpIsEven;
  588. BigInteger.prototype.exp = bnpExp;
  589. // public
  590. BigInteger.prototype.toString = bnToString;
  591. BigInteger.prototype.negate = bnNegate;
  592. BigInteger.prototype.abs = bnAbs;
  593. BigInteger.prototype.compareTo = bnCompareTo;
  594. BigInteger.prototype.bitLength = bnBitLength;
  595. BigInteger.prototype.mod = bnMod;
  596. BigInteger.prototype.modPowInt = bnModPowInt;
  597. // "constants"
  598. BigInteger.ZERO = nbv(0);
  599. BigInteger.ONE = nbv(1);
  600. // Copyright (c) 2005 Tom Wu
  601. // All Rights Reserved.
  602. // See "LICENSE" for details.
  603. // Extended JavaScript BN functions, required for RSA private ops.
  604. // (public)
  605. function bnClone() { var r = nbi(); this.copyTo(r); return r; }
  606. // (public) return value as integer
  607. function bnIntValue() {
  608. var this_array = this.array;
  609. if(this.s < 0) {
  610. if(this.t == 1) return this_array[0]-BI_DV;
  611. else if(this.t == 0) return -1;
  612. }
  613. else if(this.t == 1) return this_array[0];
  614. else if(this.t == 0) return 0;
  615. // assumes 16 < DB < 32
  616. return ((this_array[1]&((1<<(32-BI_DB))-1))<<BI_DB)|this_array[0];
  617. }
  618. // (public) return value as byte
  619. function bnByteValue() {
  620. var this_array = this.array;
  621. return (this.t==0)?this.s:(this_array[0]<<24)>>24;
  622. }
  623. // (public) return value as short (assumes DB>=16)
  624. function bnShortValue() {
  625. var this_array = this.array;
  626. return (this.t==0)?this.s:(this_array[0]<<16)>>16;
  627. }
  628. // (protected) return x s.t. r^x < DV
  629. function bnpChunkSize(r) { return Math.floor(Math.LN2*BI_DB/Math.log(r)); }
  630. // (public) 0 if this == 0, 1 if this > 0
  631. function bnSigNum() {
  632. var this_array = this.array;
  633. if(this.s < 0) return -1;
  634. else if(this.t <= 0 || (this.t == 1 && this_array[0] <= 0)) return 0;
  635. else return 1;
  636. }
  637. // (protected) convert to radix string
  638. function bnpToRadix(b) {
  639. if(b == null) b = 10;
  640. if(this.signum() == 0 || b < 2 || b > 36) return "0";
  641. var cs = this.chunkSize(b);
  642. var a = Math.pow(b,cs);
  643. var d = nbv(a), y = nbi(), z = nbi(), r = "";
  644. this.divRemTo(d,y,z);
  645. while(y.signum() > 0) {
  646. r = (a+z.intValue()).toString(b).substr(1) + r;
  647. y.divRemTo(d,y,z);
  648. }
  649. return z.intValue().toString(b) + r;
  650. }
  651. // (protected) convert from radix string
  652. function bnpFromRadix(s,b) {
  653. this.fromInt(0);
  654. if(b == null) b = 10;
  655. var cs = this.chunkSize(b);
  656. var d = Math.pow(b,cs), mi = false, j = 0, w = 0;
  657. for(var i = 0; i < s.length; ++i) {
  658. var x = intAt(s,i);
  659. if(x < 0) {
  660. if(s.charAt(i) == "-" && this.signum() == 0) mi = true;
  661. continue;
  662. }
  663. w = b*w+x;
  664. if(++j >= cs) {
  665. this.dMultiply(d);
  666. this.dAddOffset(w,0);
  667. j = 0;
  668. w = 0;
  669. }
  670. }
  671. if(j > 0) {
  672. this.dMultiply(Math.pow(b,j));
  673. this.dAddOffset(w,0);
  674. }
  675. if(mi) BigInteger.ZERO.subTo(this,this);
  676. }
  677. // (protected) alternate constructor
  678. function bnpFromNumber(a,b,c) {
  679. if("number" == typeof b) {
  680. // new BigInteger(int,int,RNG)
  681. if(a < 2) this.fromInt(1);
  682. else {
  683. this.fromNumber(a,c);
  684. if(!this.testBit(a-1)) // force MSB set
  685. this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this);
  686. if(this.isEven()) this.dAddOffset(1,0); // force odd
  687. while(!this.isProbablePrime(b)) {
  688. this.dAddOffset(2,0);
  689. if(this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a-1),this);
  690. }
  691. }
  692. }
  693. else {
  694. // new BigInteger(int,RNG)
  695. var x = new Array(), t = a&7;
  696. x.length = (a>>3)+1;
  697. b.nextBytes(x);
  698. if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0;
  699. this.fromString(x,256);
  700. }
  701. }
  702. // (public) convert to bigendian byte array
  703. function bnToByteArray() {
  704. var this_array = this.array;
  705. var i = this.t, r = new Array();
  706. r[0] = this.s;
  707. var p = BI_DB-(i*BI_DB)%8, d, k = 0;
  708. if(i-- > 0) {
  709. if(p < BI_DB && (d = this_array[i]>>p) != (this.s&BI_DM)>>p)
  710. r[k++] = d|(this.s<<(BI_DB-p));
  711. while(i >= 0) {
  712. if(p < 8) {
  713. d = (this_array[i]&((1<<p)-1))<<(8-p);
  714. d |= this_array[--i]>>(p+=BI_DB-8);
  715. }
  716. else {
  717. d = (this_array[i]>>(p-=8))&0xff;
  718. if(p <= 0) { p += BI_DB; --i; }
  719. }
  720. if((d&0x80) != 0) d |= -256;
  721. if(k == 0 && (this.s&0x80) != (d&0x80)) ++k;
  722. if(k > 0 || d != this.s) r[k++] = d;
  723. }
  724. }
  725. return r;
  726. }
  727. function bnEquals(a) { return(this.compareTo(a)==0); }
  728. function bnMin(a) { return(this.compareTo(a)<0)?this:a; }
  729. function bnMax(a) { return(this.compareTo(a)>0)?this:a; }
  730. // (protected) r = this op a (bitwise)
  731. function bnpBitwiseTo(a,op,r) {
  732. var this_array = this.array;
  733. var a_array = a.array;
  734. var r_array = r.array;
  735. var i, f, m = Math.min(a.t,this.t);
  736. for(i = 0; i < m; ++i) r_array[i] = op(this_array[i],a_array[i]);
  737. if(a.t < this.t) {
  738. f = a.s&BI_DM;
  739. for(i = m; i < this.t; ++i) r_array[i] = op(this_array[i],f);
  740. r.t = this.t;
  741. }
  742. else {
  743. f = this.s&BI_DM;
  744. for(i = m; i < a.t; ++i) r_array[i] = op(f,a_array[i]);
  745. r.t = a.t;
  746. }
  747. r.s = op(this.s,a.s);
  748. r.clamp();
  749. }
  750. // (public) this & a
  751. function op_and(x,y) { return x&y; }
  752. function bnAnd(a) { var r = nbi(); this.bitwiseTo(a,op_and,r); return r; }
  753. // (public) this | a
  754. function op_or(x,y) { return x|y; }
  755. function bnOr(a) { var r = nbi(); this.bitwiseTo(a,op_or,r); return r; }
  756. // (public) this ^ a
  757. function op_xor(x,y) { return x^y; }
  758. function bnXor(a) { var r = nbi(); this.bitwiseTo(a,op_xor,r); return r; }
  759. // (public) this & ~a
  760. function op_andnot(x,y) { return x&~y; }
  761. function bnAndNot(a) { var r = nbi(); this.bitwiseTo(a,op_andnot,r); return r; }
  762. // (public) ~this
  763. function bnNot() {
  764. var this_array = this.array;
  765. var r = nbi();
  766. var r_array = r.array;
  767. for(var i = 0; i < this.t; ++i) r_array[i] = BI_DM&~this_array[i];
  768. r.t = this.t;
  769. r.s = ~this.s;
  770. return r;
  771. }
  772. // (public) this << n
  773. function bnShiftLeft(n) {
  774. var r = nbi();
  775. if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r);
  776. return r;
  777. }
  778. // (public) this >> n
  779. function bnShiftRight(n) {
  780. var r = nbi();
  781. if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r);
  782. return r;
  783. }
  784. // return index of lowest 1-bit in x, x < 2^31
  785. function lbit(x) {
  786. if(x == 0) return -1;
  787. var r = 0;
  788. if((x&0xffff) == 0) { x >>= 16; r += 16; }
  789. if((x&0xff) == 0) { x >>= 8; r += 8; }
  790. if((x&0xf) == 0) { x >>= 4; r += 4; }
  791. if((x&3) == 0) { x >>= 2; r += 2; }
  792. if((x&1) == 0) ++r;
  793. return r;
  794. }
  795. // (public) returns index of lowest 1-bit (or -1 if none)
  796. function bnGetLowestSetBit() {
  797. var this_array = this.array;
  798. for(var i = 0; i < this.t; ++i)
  799. if(this_array[i] != 0) return i*BI_DB+lbit(this_array[i]);
  800. if(this.s < 0) return this.t*BI_DB;
  801. return -1;
  802. }
  803. // return number of 1 bits in x
  804. function cbit(x) {
  805. var r = 0;
  806. while(x != 0) { x &= x-1; ++r; }
  807. return r;
  808. }
  809. // (public) return number of set bits
  810. function bnBitCount() {
  811. var r = 0, x = this.s&BI_DM;
  812. for(var i = 0; i < this.t; ++i) r += cbit(this_array[i]^x);
  813. return r;
  814. }
  815. // (public) true iff nth bit is set
  816. function bnTestBit(n) {
  817. var this_array = this.array;
  818. var j = Math.floor(n/BI_DB);
  819. if(j >= this.t) return(this.s!=0);
  820. return((this_array[j]&(1<<(n%BI_DB)))!=0);
  821. }
  822. // (protected) this op (1<<n)
  823. function bnpChangeBit(n,op) {
  824. var r = BigInteger.ONE.shiftLeft(n);
  825. this.bitwiseTo(r,op,r);
  826. return r;
  827. }
  828. // (public) this | (1<<n)
  829. function bnSetBit(n) { return this.changeBit(n,op_or); }
  830. // (public) this & ~(1<<n)
  831. function bnClearBit(n) { return this.changeBit(n,op_andnot); }
  832. // (public) this ^ (1<<n)
  833. function bnFlipBit(n) { return this.changeBit(n,op_xor); }
  834. // (protected) r = this + a
  835. function bnpAddTo(a,r) {
  836. var this_array = this.array;
  837. var a_array = a.array;
  838. var r_array = r.array;
  839. var i = 0, c = 0, m = Math.min(a.t,this.t);
  840. while(i < m) {
  841. c += this_array[i]+a_array[i];
  842. r_array[i++] = c&BI_DM;
  843. c >>= BI_DB;
  844. }
  845. if(a.t < this.t) {
  846. c += a.s;
  847. while(i < this.t) {
  848. c += this_array[i];
  849. r_array[i++] = c&BI_DM;
  850. c >>= BI_DB;
  851. }
  852. c += this.s;
  853. }
  854. else {
  855. c += this.s;
  856. while(i < a.t) {
  857. c += a_array[i];
  858. r_array[i++] = c&BI_DM;
  859. c >>= BI_DB;
  860. }
  861. c += a.s;
  862. }
  863. r.s = (c<0)?-1:0;
  864. if(c > 0) r_array[i++] = c;
  865. else if(c < -1) r_array[i++] = BI_DV+c;
  866. r.t = i;
  867. r.clamp();
  868. }
  869. // (public) this + a
  870. function bnAdd(a) { var r = nbi(); this.addTo(a,r); return r; }
  871. // (public) this - a
  872. function bnSubtract(a) { var r = nbi(); this.subTo(a,r); return r; }
  873. // (public) this * a
  874. function bnMultiply(a) { var r = nbi(); this.multiplyTo(a,r); return r; }
  875. // (public) this / a
  876. function bnDivide(a) { var r = nbi(); this.divRemTo(a,r,null); return r; }
  877. // (public) this % a
  878. function bnRemainder(a) { var r = nbi(); this.divRemTo(a,null,r); return r; }
  879. // (public) [this/a,this%a]
  880. function bnDivideAndRemainder(a) {
  881. var q = nbi(), r = nbi();
  882. this.divRemTo(a,q,r);
  883. return new Array(q,r);
  884. }
  885. // (protected) this *= n, this >= 0, 1 < n < DV
  886. function bnpDMultiply(n) {
  887. var this_array = this.array;
  888. this_array[this.t] = this.am(0,n-1,this,0,0,this.t);
  889. ++this.t;
  890. this.clamp();
  891. }
  892. // (protected) this += n << w words, this >= 0
  893. function bnpDAddOffset(n,w) {
  894. var this_array = this.array;
  895. while(this.t <= w) this_array[this.t++] = 0;
  896. this_array[w] += n;
  897. while(this_array[w] >= BI_DV) {
  898. this_array[w] -= BI_DV;
  899. if(++w >= this.t) this_array[this.t++] = 0;
  900. ++this_array[w];
  901. }
  902. }
  903. // A "null" reducer
  904. function NullExp() {}
  905. function nNop(x) { return x; }
  906. function nMulTo(x,y,r) { x.multiplyTo(y,r); }
  907. function nSqrTo(x,r) { x.squareTo(r); }
  908. NullExp.prototype.convert = nNop;
  909. NullExp.prototype.revert = nNop;
  910. NullExp.prototype.mulTo = nMulTo;
  911. NullExp.prototype.sqrTo = nSqrTo;
  912. // (public) this^e
  913. function bnPow(e) { return this.exp(e,new NullExp()); }
  914. // (protected) r = lower n words of "this * a", a.t <= n
  915. // "this" should be the larger one if appropriate.
  916. function bnpMultiplyLowerTo(a,n,r) {
  917. var r_array = r.array;
  918. var a_array = a.array;
  919. var i = Math.min(this.t+a.t,n);
  920. r.s = 0; // assumes a,this >= 0
  921. r.t = i;
  922. while(i > 0) r_array[--i] = 0;
  923. var j;
  924. for(j = r.t-this.t; i < j; ++i) r_array[i+this.t] = this.am(0,a_array[i],r,i,0,this.t);
  925. for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a_array[i],r,i,0,n-i);
  926. r.clamp();
  927. }
  928. // (protected) r = "this * a" without lower n words, n > 0
  929. // "this" should be the larger one if appropriate.
  930. function bnpMultiplyUpperTo(a,n,r) {
  931. var r_array = r.array;
  932. var a_array = a.array;
  933. --n;
  934. var i = r.t = this.t+a.t-n;
  935. r.s = 0; // assumes a,this >= 0
  936. while(--i >= 0) r_array[i] = 0;
  937. for(i = Math.max(n-this.t,0); i < a.t; ++i)
  938. r_array[this.t+i-n] = this.am(n-i,a_array[i],r,0,0,this.t+i-n);
  939. r.clamp();
  940. r.drShiftTo(1,r);
  941. }
  942. // Barrett modular reduction
  943. function Barrett(m) {
  944. // setup Barrett
  945. this.r2 = nbi();
  946. this.q3 = nbi();
  947. BigInteger.ONE.dlShiftTo(2*m.t,this.r2);
  948. this.mu = this.r2.divide(m);
  949. this.m = m;
  950. }
  951. function barrettConvert(x) {
  952. if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m);
  953. else if(x.compareTo(this.m) < 0) return x;
  954. else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; }
  955. }
  956. function barrettRevert(x) { return x; }
  957. // x = x mod m (HAC 14.42)
  958. function barrettReduce(x) {
  959. x.drShiftTo(this.m.t-1,this.r2);
  960. if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); }
  961. this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3);
  962. this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);
  963. while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1);
  964. x.subTo(this.r2,x);
  965. while(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
  966. }
  967. // r = x^2 mod m; x != r
  968. function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
  969. // r = x*y mod m; x,y != r
  970. function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
  971. Barrett.prototype.convert = barrettConvert;
  972. Barrett.prototype.revert = barrettRevert;
  973. Barrett.prototype.reduce = barrettReduce;
  974. Barrett.prototype.mulTo = barrettMulTo;
  975. Barrett.prototype.sqrTo = barrettSqrTo;
  976. // (public) this^e % m (HAC 14.85)
  977. function bnModPow(e,m) {
  978. var e_array = e.array;
  979. var i = e.bitLength(), k, r = nbv(1), z;
  980. if(i <= 0) return r;
  981. else if(i < 18) k = 1;
  982. else if(i < 48) k = 3;
  983. else if(i < 144) k = 4;
  984. else if(i < 768) k = 5;
  985. else k = 6;
  986. if(i < 8)
  987. z = new Classic(m);
  988. else if(m.isEven())
  989. z = new Barrett(m);
  990. else
  991. z = new Montgomery(m);
  992. // precomputation
  993. var g = new Array(), n = 3, k1 = k-1, km = (1<<k)-1;
  994. g[1] = z.convert(this);
  995. if(k > 1) {
  996. var g2 = nbi();
  997. z.sqrTo(g[1],g2);
  998. while(n <= km) {
  999. g[n] = nbi();
  1000. z.mulTo(g2,g[n-2],g[n]);
  1001. n += 2;
  1002. }
  1003. }
  1004. var j = e.t-1, w, is1 = true, r2 = nbi(), t;
  1005. i = nbits(e_array[j])-1;
  1006. while(j >= 0) {
  1007. if(i >= k1) w = (e_array[j]>>(i-k1))&km;
  1008. else {
  1009. w = (e_array[j]&((1<<(i+1))-1))<<(k1-i);
  1010. if(j > 0) w |= e_array[j-1]>>(BI_DB+i-k1);
  1011. }
  1012. n = k;
  1013. while((w&1) == 0) { w >>= 1; --n; }
  1014. if((i -= n) < 0) { i += BI_DB; --j; }
  1015. if(is1) { // ret == 1, don't bother squaring or multiplying it
  1016. g[w].copyTo(r);
  1017. is1 = false;
  1018. }
  1019. else {
  1020. while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; }
  1021. if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; }
  1022. z.mulTo(r2,g[w],r);
  1023. }
  1024. while(j >= 0 && (e_array[j]&(1<<i)) == 0) {
  1025. z.sqrTo(r,r2); t = r; r = r2; r2 = t;
  1026. if(--i < 0) { i = BI_DB-1; --j; }
  1027. }
  1028. }
  1029. return z.revert(r);
  1030. }
  1031. // (public) gcd(this,a) (HAC 14.54)
  1032. function bnGCD(a) {
  1033. var x = (this.s<0)?this.negate():this.clone();
  1034. var y = (a.s<0)?a.negate():a.clone();
  1035. if(x.compareTo(y) < 0) { var t = x; x = y; y = t; }
  1036. var i = x.getLowestSetBit(), g = y.getLowestSetBit();
  1037. if(g < 0) return x;
  1038. if(i < g) g = i;
  1039. if(g > 0) {
  1040. x.rShiftTo(g,x);
  1041. y.rShiftTo(g,y);
  1042. }
  1043. while(x.signum() > 0) {
  1044. if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x);
  1045. if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y);
  1046. if(x.compareTo(y) >= 0) {
  1047. x.subTo(y,x);
  1048. x.rShiftTo(1,x);
  1049. }
  1050. else {
  1051. y.subTo(x,y);
  1052. y.rShiftTo(1,y);
  1053. }
  1054. }
  1055. if(g > 0) y.lShiftTo(g,y);
  1056. return y;
  1057. }
  1058. // (protected) this % n, n < 2^26
  1059. function bnpModInt(n) {
  1060. var this_array = this.array;
  1061. if(n <= 0) return 0;
  1062. var d = BI_DV%n, r = (this.s<0)?n-1:0;
  1063. if(this.t > 0)
  1064. if(d == 0) r = this_array[0]%n;
  1065. else for(var i = this.t-1; i >= 0; --i) r = (d*r+this_array[i])%n;
  1066. return r;
  1067. }
  1068. // (public) 1/this % m (HAC 14.61)
  1069. function bnModInverse(m) {
  1070. var ac = m.isEven();
  1071. if((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO;
  1072. var u = m.clone(), v = this.clone();
  1073. var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1);
  1074. while(u.signum() != 0) {
  1075. while(u.isEven()) {
  1076. u.rShiftTo(1,u);
  1077. if(ac) {
  1078. if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); }
  1079. a.rShiftTo(1,a);
  1080. }
  1081. else if(!b.isEven()) b.subTo(m,b);
  1082. b.rShiftTo(1,b);
  1083. }
  1084. while(v.isEven()) {
  1085. v.rShiftTo(1,v);
  1086. if(ac) {
  1087. if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); }
  1088. c.rShiftTo(1,c);
  1089. }
  1090. else if(!d.isEven()) d.subTo(m,d);
  1091. d.rShiftTo(1,d);
  1092. }
  1093. if(u.compareTo(v) >= 0) {
  1094. u.subTo(v,u);
  1095. if(ac) a.subTo(c,a);
  1096. b.subTo(d,b);
  1097. }
  1098. else {
  1099. v.subTo(u,v);
  1100. if(ac) c.subTo(a,c);
  1101. d.subTo(b,d);
  1102. }
  1103. }
  1104. if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
  1105. if(d.compareTo(m) >= 0) return d.subtract(m);
  1106. if(d.signum() < 0) d.addTo(m,d); else return d;
  1107. if(d.signum() < 0) return d.add(m); else return d;
  1108. }
  1109. 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];
  1110. var lplim = (1<<26)/lowprimes[lowprimes.length-1];
  1111. // (public) test primality with certainty >= 1-.5^t
  1112. function bnIsProbablePrime(t) {
  1113. var i, x = this.abs();
  1114. var x_array = x.array;
  1115. if(x.t == 1 && x_array[0] <= lowprimes[lowprimes.length-1]) {
  1116. for(i = 0; i < lowprimes.length; ++i)
  1117. if(x_array[0] == lowprimes[i]) return true;
  1118. return false;
  1119. }
  1120. if(x.isEven()) return false;
  1121. i = 1;
  1122. while(i < lowprimes.length) {
  1123. var m = lowprimes[i], j = i+1;
  1124. while(j < lowprimes.length && m < lplim) m *= lowprimes[j++];
  1125. m = x.modInt(m);
  1126. while(i < j) if(m%lowprimes[i++] == 0) return false;
  1127. }
  1128. return x.millerRabin(t);
  1129. }
  1130. // (protected) true if probably prime (HAC 4.24, Miller-Rabin)
  1131. function bnpMillerRabin(t) {
  1132. var n1 = this.subtract(BigInteger.ONE);
  1133. var k = n1.getLowestSetBit();
  1134. if(k <= 0) return false;
  1135. var r = n1.shiftRight(k);
  1136. t = (t+1)>>1;
  1137. if(t > lowprimes.length) t = lowprimes.length;
  1138. var a = nbi();
  1139. for(var i = 0; i < t; ++i) {
  1140. a.fromInt(lowprimes[i]);
  1141. var y = a.modPow(r,this);
  1142. if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
  1143. var j = 1;
  1144. while(j++ < k && y.compareTo(n1) != 0) {
  1145. y = y.modPowInt(2,this);
  1146. if(y.compareTo(BigInteger.ONE) == 0) return false;
  1147. }
  1148. if(y.compareTo(n1) != 0) return false;
  1149. }
  1150. }
  1151. return true;
  1152. }
  1153. // protected
  1154. BigInteger.prototype.chunkSize = bnpChunkSize;
  1155. BigInteger.prototype.toRadix = bnpToRadix;
  1156. BigInteger.prototype.fromRadix = bnpFromRadix;
  1157. BigInteger.prototype.fromNumber = bnpFromNumber;
  1158. BigInteger.prototype.bitwiseTo = bnpBitwiseTo;
  1159. BigInteger.prototype.changeBit = bnpChangeBit;
  1160. BigInteger.prototype.addTo = bnpAddTo;
  1161. BigInteger.prototype.dMultiply = bnpDMultiply;
  1162. BigInteger.prototype.dAddOffset = bnpDAddOffset;
  1163. BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo;
  1164. BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
  1165. BigInteger.prototype.modInt = bnpModInt;
  1166. BigInteger.prototype.millerRabin = bnpMillerRabin;
  1167. // public
  1168. BigInteger.prototype.clone = bnClone;
  1169. BigInteger.prototype.intValue = bnIntValue;
  1170. BigInteger.prototype.byteValue = bnByteValue;
  1171. BigInteger.prototype.shortValue = bnShortValue;
  1172. BigInteger.prototype.signum = bnSigNum;
  1173. BigInteger.prototype.toByteArray = bnToByteArray;
  1174. BigInteger.prototype.equals = bnEquals;
  1175. BigInteger.prototype.min = bnMin;
  1176. BigInteger.prototype.max = bnMax;
  1177. BigInteger.prototype.and = bnAnd;
  1178. BigInteger.prototype.or = bnOr;
  1179. BigInteger.prototype.xor = bnXor;
  1180. BigInteger.prototype.andNot = bnAndNot;
  1181. BigInteger.prototype.not = bnNot;
  1182. BigInteger.prototype.shiftLeft = bnShiftLeft;
  1183. BigInteger.prototype.shiftRight = bnShiftRight;
  1184. BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit;
  1185. BigInteger.prototype.bitCount = bnBitCount;
  1186. BigInteger.prototype.testBit = bnTestBit;
  1187. BigInteger.prototype.setBit = bnSetBit;
  1188. BigInteger.prototype.clearBit = bnClearBit;
  1189. BigInteger.prototype.flipBit = bnFlipBit;
  1190. BigInteger.prototype.add = bnAdd;
  1191. BigInteger.prototype.subtract = bnSubtract;
  1192. BigInteger.prototype.multiply = bnMultiply;
  1193. BigInteger.prototype.divide = bnDivide;
  1194. BigInteger.prototype.remainder = bnRemainder;
  1195. BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder;
  1196. BigInteger.prototype.modPow = bnModPow;
  1197. BigInteger.prototype.modInverse = bnModInverse;
  1198. BigInteger.prototype.pow = bnPow;
  1199. BigInteger.prototype.gcd = bnGCD;
  1200. BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
  1201. // BigInteger interfaces not implemented in jsbn:
  1202. // BigInteger(int signum, byte[] magnitude)
  1203. // double doubleValue()
  1204. // float floatValue()
  1205. // int hashCode()
  1206. // long longValue()
  1207. // static BigInteger valueOf(long val)
  1208. // prng4.js - uses Arcfour as a PRNG
  1209. function Arcfour() {
  1210. this.i = 0;
  1211. this.j = 0;
  1212. this.S = new Array();
  1213. }
  1214. // Initialize arcfour context from key, an array of ints, each from [0..255]
  1215. function ARC4init(key) {
  1216. var i, j, t;
  1217. for(i = 0; i < 256; ++i)
  1218. this.S[i] = i;
  1219. j = 0;
  1220. for(i = 0; i < 256; ++i) {
  1221. j = (j + this.S[i] + key[i % key.length]) & 255;
  1222. t = this.S[i];
  1223. this.S[i] = this.S[j];
  1224. this.S[j] = t;
  1225. }
  1226. this.i = 0;
  1227. this.j = 0;
  1228. }
  1229. function ARC4next() {
  1230. var t;
  1231. this.i = (this.i + 1) & 255;
  1232. this.j = (this.j + this.S[this.i]) & 255;
  1233. t = this.S[this.i];
  1234. this.S[this.i] = this.S[this.j];
  1235. this.S[this.j] = t;
  1236. return this.S[(t + this.S[this.i]) & 255];
  1237. }
  1238. Arcfour.prototype.init = ARC4init;
  1239. Arcfour.prototype.next = ARC4next;
  1240. // Plug in your RNG constructor here
  1241. function prng_newstate() {
  1242. return new Arcfour();
  1243. }
  1244. // Pool size must be a multiple of 4 and greater than 32.
  1245. // An array of bytes the size of the pool will be passed to init()
  1246. var rng_psize = 256;
  1247. // Random number generator - requires a PRNG backend, e.g. prng4.js
  1248. // For best results, put code like
  1249. // <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'>
  1250. // in your main HTML document.
  1251. var rng_state;
  1252. var rng_pool;
  1253. var rng_pptr;
  1254. // Mix in a 32-bit integer into the pool
  1255. function rng_seed_int(x) {
  1256. rng_pool[rng_pptr++] ^= x & 255;
  1257. rng_pool[rng_pptr++] ^= (x >> 8) & 255;
  1258. rng_pool[rng_pptr++] ^= (x >> 16) & 255;
  1259. rng_pool[rng_pptr++] ^= (x >> 24) & 255;
  1260. if(rng_pptr >= rng_psize) rng_pptr -= rng_psize;
  1261. }
  1262. // Mix in the current time (w/milliseconds) into the pool
  1263. function rng_seed_time() {
  1264. // Use pre-computed date to avoid making the benchmark
  1265. // results dependent on the current date.
  1266. rng_seed_int(1122926989487);
  1267. }
  1268. // Initialize the pool with junk if needed.
  1269. if(rng_pool == null) {
  1270. rng_pool = new Array();
  1271. rng_pptr = 0;
  1272. var t;
  1273. while(rng_pptr < rng_psize) { // extract some randomness from Math.random()
  1274. t = Math.floor(65536 * Math.random());
  1275. rng_pool[rng_pptr++] = t >>> 8;
  1276. rng_pool[rng_pptr++] = t & 255;
  1277. }
  1278. rng_pptr = 0;
  1279. rng_seed_time();
  1280. //rng_seed_int(window.screenX);
  1281. //rng_seed_int(window.screenY);
  1282. }
  1283. function rng_get_byte() {
  1284. if(rng_state == null) {
  1285. rng_seed_time();
  1286. rng_state = prng_newstate();
  1287. rng_state.init(rng_pool);
  1288. for(rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr)
  1289. rng_pool[rng_pptr] = 0;
  1290. rng_pptr = 0;
  1291. //rng_pool = null;
  1292. }
  1293. // TODO: allow reseeding after first request
  1294. return rng_state.next();
  1295. }
  1296. function rng_get_bytes(ba) {
  1297. var i;
  1298. for(i = 0; i < ba.length; ++i) ba[i] = rng_get_byte();
  1299. }
  1300. function SecureRandom() {}
  1301. SecureRandom.prototype.nextBytes = rng_get_bytes;
  1302. // Depends on jsbn.js and rng.js
  1303. // convert a (hex) string to a bignum object
  1304. function parseBigInt(str,r) {
  1305. return new BigInteger(str,r);
  1306. }
  1307. function linebrk(s,n) {
  1308. var ret = "";
  1309. var i = 0;
  1310. while(i + n < s.length) {
  1311. ret += s.substring(i,i+n) + "\n";
  1312. i += n;
  1313. }
  1314. return ret + s.substring(i,s.length);
  1315. }
  1316. function byte2Hex(b) {
  1317. if(b < 0x10)
  1318. return "0" + b.toString(16);
  1319. else
  1320. return b.toString(16);
  1321. }
  1322. // PKCS#1 (type 2, random) pad input string s to n bytes, and return a bigint
  1323. function pkcs1pad2(s,n) {
  1324. if(n < s.length + 11) {
  1325. alert("Message too long for RSA");
  1326. return null;
  1327. }
  1328. var ba = new Array();
  1329. var i = s.length - 1;
  1330. while(i >= 0 && n > 0) ba[--n] = s.charCodeAt(i--);
  1331. ba[--n] = 0;
  1332. var rng = new SecureRandom();
  1333. var x = new Array();
  1334. while(n > 2) { // random non-zero pad
  1335. x[0] = 0;
  1336. while(x[0] == 0) rng.nextBytes(x);
  1337. ba[--n] = x[0];
  1338. }
  1339. ba[--n] = 2;
  1340. ba[--n] = 0;
  1341. return new BigInteger(ba);
  1342. }
  1343. // "empty" RSA key constructor
  1344. function RSAKey() {
  1345. this.n = null;
  1346. this.e = 0;
  1347. this.d = null;
  1348. this.p = null;
  1349. this.q = null;
  1350. this.dmp1 = null;
  1351. this.dmq1 = null;
  1352. this.coeff = null;
  1353. }
  1354. // Set the public key fields N and e from hex strings
  1355. function RSASetPublic(N,E) {
  1356. if(N != null && E != null && N.length > 0 && E.length > 0) {
  1357. this.n = parseBigInt(N,16);
  1358. this.e = parseInt(E,16);
  1359. }
  1360. else
  1361. alert("Invalid RSA public key");
  1362. }
  1363. // Perform raw public operation on "x": return x^e (mod n)
  1364. function RSADoPublic(x) {
  1365. return x.modPowInt(this.e, this.n);
  1366. }
  1367. // Return the PKCS#1 RSA encryption of "text" as an even-length hex string
  1368. function RSAEncrypt(text) {
  1369. var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3);
  1370. if(m == null) return null;
  1371. var c = this.doPublic(m);
  1372. if(c == null) return null;
  1373. var h = c.toString(16);
  1374. if((h.length & 1) == 0) return h; else return "0" + h;
  1375. }
  1376. // Return the PKCS#1 RSA encryption of "text" as a Base64-encoded string
  1377. //function RSAEncryptB64(text) {
  1378. // var h = this.encrypt(text);
  1379. // if(h) return hex2b64(h); else return null;
  1380. //}
  1381. // protected
  1382. RSAKey.prototype.doPublic = RSADoPublic;
  1383. // public
  1384. RSAKey.prototype.setPublic = RSASetPublic;
  1385. RSAKey.prototype.encrypt = RSAEncrypt;
  1386. //RSAKey.prototype.encrypt_b64 = RSAEncryptB64;
  1387. // Depends on rsa.js and jsbn2.js
  1388. // Undo PKCS#1 (type 2, random) padding and, if valid, return the plaintext
  1389. function pkcs1unpad2(d,n) {
  1390. var b = d.toByteArray();
  1391. var i = 0;
  1392. while(i < b.length && b[i] == 0) ++i;
  1393. if(b.length-i != n-1 || b[i] != 2)
  1394. return null;
  1395. ++i;
  1396. while(b[i] != 0)
  1397. if(++i >= b.length) return null;
  1398. var ret = "";
  1399. while(++i < b.length)
  1400. ret += String.fromCharCode(b[i]);
  1401. return ret;
  1402. }
  1403. // Set the private key fields N, e, and d from hex strings
  1404. function RSASetPrivate(N,E,D) {
  1405. if(N != null && E != null && N.length > 0 && E.length > 0) {
  1406. this.n = parseBigInt(N,16);
  1407. this.e = parseInt(E,16);
  1408. this.d = parseBigInt(D,16);
  1409. }
  1410. else
  1411. alert("Invalid RSA private key");
  1412. }
  1413. // Set the private key fields N, e, d and CRT params from hex strings
  1414. function RSASetPrivateEx(N,E,D,P,Q,DP,DQ,C) {
  1415. if(N != null && E != null && N.length > 0 && E.length > 0) {
  1416. this.n = parseBigInt(N,16);
  1417. this.e = parseInt(E,16);
  1418. this.d = parseBigInt(D,16);
  1419. this.p = parseBigInt(P,16);
  1420. this.q = parseBigInt(Q,16);
  1421. this.dmp1 = parseBigInt(DP,16);
  1422. this.dmq1 = parseBigInt(DQ,16);
  1423. this.coeff = parseBigInt(C,16);
  1424. }
  1425. else
  1426. alert("Invalid RSA private key");
  1427. }
  1428. // Generate a new random private key B bits long, using public expt E
  1429. function RSAGenerate(B,E) {
  1430. var rng = new SecureRandom();
  1431. var qs = B>>1;
  1432. this.e = parseInt(E,16);
  1433. var ee = new BigInteger(E,16);
  1434. for(;;) {
  1435. for(;;) {
  1436. this.p = new BigInteger(B-qs,1,rng);
  1437. if(this.p.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.p.isProbablePrime(10)) break;
  1438. }
  1439. for(;;) {
  1440. this.q = new BigInteger(qs,1,rng);
  1441. if(this.q.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.q.isProbablePrime(10)) break;
  1442. }
  1443. if(this.p.compareTo(this.q) <= 0) {
  1444. var t = this.p;
  1445. this.p = this.q;
  1446. this.q = t;
  1447. }
  1448. var p1 = this.p.subtract(BigInteger.ONE);
  1449. var q1 = this.q.subtract(BigInteger.ONE);
  1450. var phi = p1.multiply(q1);
  1451. if(phi.gcd(ee).compareTo(BigInteger.ONE) == 0) {
  1452. this.n = this.p.multiply(this.q);
  1453. this.d = ee.modInverse(phi);
  1454. this.dmp1 = this.d.mod(p1);
  1455. this.dmq1 = this.d.mod(q1);
  1456. this.coeff = this.q.modInverse(this.p);
  1457. break;
  1458. }
  1459. }
  1460. }
  1461. // Perform raw private operation on "x": return x^d (mod n)
  1462. function RSADoPrivate(x) {
  1463. if(this.p == null || this.q == null)
  1464. return x.modPow(this.d, this.n);
  1465. // TODO: re-calculate any missing CRT params
  1466. var xp = x.mod(this.p).modPow(this.dmp1, this.p);
  1467. var xq = x.mod(this.q).modPow(this.dmq1, this.q);
  1468. while(xp.compareTo(xq) < 0)
  1469. xp = xp.add(this.p);
  1470. return xp.subtract(xq).multiply(this.coeff).mod(this.p).multiply(this.q).add(xq);
  1471. }
  1472. // Return the PKCS#1 RSA decryption of "ctext".
  1473. // "ctext" is an even-length hex string and the output is a plain string.
  1474. function RSADecrypt(ctext) {
  1475. var c = parseBigInt(ctext, 16);
  1476. var m = this.doPrivate(c);
  1477. if(m == null) return null;
  1478. return pkcs1unpad2(m, (this.n.bitLength()+7)>>3);
  1479. }
  1480. // Return the PKCS#1 RSA decryption of "ctext".
  1481. // "ctext" is a Base64-encoded string and the output is a plain string.
  1482. //function RSAB64Decrypt(ctext) {
  1483. // var h = b64tohex(ctext);
  1484. // if(h) return this.decrypt(h); else return null;
  1485. //}
  1486. // protected
  1487. RSAKey.prototype.doPrivate = RSADoPrivate;
  1488. // public
  1489. RSAKey.prototype.setPrivate = RSASetPrivate;
  1490. RSAKey.prototype.setPrivateEx = RSASetPrivateEx;
  1491. RSAKey.prototype.generate = RSAGenerate;
  1492. RSAKey.prototype.decrypt = RSADecrypt;
  1493. //RSAKey.prototype.b64_decrypt = RSAB64Decrypt;
  1494. nValue="a5261939975948bb7a58dffe5ff54e65f0498f9175f5a09288810b8975871e99af3b5dd94057b0fc07535f5f97444504fa35169d461d0d30cf0192e307727c065168c788771c561a9400fb49175e9e6aa4e23fe11af69e9412dd23b0cb6684c4c2429bce139e848ab26d0829073351f4acd36074eafd036a5eb83359d2a698d3";
  1495. eValue="10001";
  1496. dValue="8e9912f6d3645894e8d38cb58c0db81ff516cf4c7e5a14c7f1eddb1459d2cded4d8d293fc97aee6aefb861859c8b6a3d1dfe710463e1f9ddc72048c09751971c4a580aa51eb523357a3cc48d31cfad1d4a165066ed92d4748fb6571211da5cb14bc11b6e2df7c1a559e6d5ac1cd5c94703a22891464fba23d0d965086277a161";
  1497. pValue="d090ce58a92c75233a6486cb0a9209bf3583b64f540c76f5294bb97d285eed33aec220bde14b2417951178ac152ceab6da7090905b478195498b352048f15e7d";
  1498. qValue="cab575dc652bb66df15a0359609d51d1db184750c00c6698b90ef3465c99655103edbf0d54c56aec0ce3c4d22592338092a126a0cc49f65a4a30d222b411e58f";
  1499. dmp1Value="1a24bca8e273df2f0e47c199bbf678604e7df7215480c77c8db39f49b000ce2cf7500038acfff5433b7d582a01f1826e6f4d42e1c57f5e1fef7b12aabc59fd25";
  1500. dmq1Value="3d06982efbbe47339e1f6d36b1216b8a741d410b0c662f54f7118b27b9a4ec9d914337eb39841d8666f3034408cf94f5b62f11c402fc994fe15a05493150d9fd";
  1501. coeffValue="3a3e731acd8960b7ff9eb81a7ff93bd1cfa74cbd56987db58b4594fb09c09084db1734c8143f98b602b981aaa9243ca28deb69b5b280ee8dcee0fd2625e53250";
  1502. setupEngine(am3, 28);
  1503. var TEXT = "The quick brown fox jumped over the extremely lazy frog! " +
  1504. "Now is the time for all good men to come to the party.";
  1505. var encrypted;
  1506. startTest("v8-crypto");
  1507. test("RSA Encrypt", function encrypt() {
  1508. var RSA = new RSAKey();
  1509. RSA.setPublic(nValue, eValue);
  1510. RSA.setPrivateEx(nValue, eValue, dValue, pValue, qValue, dmp1Value, dmq1Value, coeffValue);
  1511. encrypted = RSA.encrypt(TEXT);
  1512. });
  1513. test("RSA Decrypt", function decrypt() {
  1514. var RSA = new RSAKey();
  1515. RSA.setPublic(nValue, eValue);
  1516. RSA.setPrivateEx(nValue, eValue, dValue, pValue, qValue, dmp1Value, dmq1Value, coeffValue);
  1517. var decrypted = RSA.decrypt(encrypted);
  1518. if (decrypted != TEXT) {
  1519. throw new Error("Crypto operation failed");
  1520. }
  1521. });
  1522. endTest();