crypto.js 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117
  1. // Copyright 2013 the V8 project authors. All rights reserved.
  2. // Redistribution and use in source and binary forms, with or without
  3. // modification, are permitted provided that the following conditions are
  4. // met:
  5. //
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above
  9. // copyright notice, this list of conditions and the following
  10. // disclaimer in the documentation and/or other materials provided
  11. // with the distribution.
  12. // * Neither the name of Google Inc. nor the names of its
  13. // contributors may be used to endorse or promote products derived
  14. // from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. // Performance.now is used in latency benchmarks, the fallback is Date.now.
  28. var performance = performance || {};
  29. performance.now = (function() {
  30. return performance.now ||
  31. performance.mozNow ||
  32. performance.msNow ||
  33. performance.oNow ||
  34. performance.webkitNow ||
  35. Date.now;
  36. })();
  37. // Simple framework for running the benchmark suites and
  38. // computing a score based on the timing measurements.
  39. // A benchmark has a name (string) and a function that will be run to
  40. // do the performance measurement. The optional setup and tearDown
  41. // arguments are functions that will be invoked before and after
  42. // running the benchmark, but the running time of these functions will
  43. // not be accounted for in the benchmark score.
  44. function Benchmark(name, doWarmup, doDeterministic, deterministicIterations,
  45. run, setup, tearDown, rmsResult, minIterations) {
  46. this.name = name;
  47. this.doWarmup = doWarmup;
  48. this.doDeterministic = doDeterministic;
  49. this.deterministicIterations = deterministicIterations;
  50. this.run = run;
  51. this.Setup = setup ? setup : function() { };
  52. this.TearDown = tearDown ? tearDown : function() { };
  53. this.rmsResult = rmsResult ? rmsResult : null;
  54. this.minIterations = minIterations ? minIterations : 32;
  55. }
  56. // Benchmark results hold the benchmark and the measured time used to
  57. // run the benchmark. The benchmark score is computed later once a
  58. // full benchmark suite has run to completion. If latency is set to 0
  59. // then there is no latency score for this benchmark.
  60. function BenchmarkResult(benchmark, time, latency) {
  61. this.benchmark = benchmark;
  62. this.time = time;
  63. this.latency = latency;
  64. }
  65. // Automatically convert results to numbers. Used by the geometric
  66. // mean computation.
  67. BenchmarkResult.prototype.valueOf = function() {
  68. return this.time;
  69. }
  70. // Suites of benchmarks consist of a name and the set of benchmarks in
  71. // addition to the reference timing that the final score will be based
  72. // on. This way, all scores are relative to a reference run and higher
  73. // scores implies better performance.
  74. function BenchmarkSuite(name, reference, benchmarks) {
  75. this.name = name;
  76. this.reference = reference;
  77. this.benchmarks = benchmarks;
  78. BenchmarkSuite.suites.push(this);
  79. }
  80. // Keep track of all declared benchmark suites.
  81. BenchmarkSuite.suites = [];
  82. // Scores are not comparable across versions. Bump the version if
  83. // you're making changes that will affect that scores, e.g. if you add
  84. // a new benchmark or change an existing one.
  85. BenchmarkSuite.version = '9';
  86. // Defines global benchsuite running mode that overrides benchmark suite
  87. // behavior. Intended to be set by the benchmark driver. Undefined
  88. // values here allow a benchmark to define behaviour itself.
  89. BenchmarkSuite.config = {
  90. doWarmup: undefined,
  91. doDeterministic: undefined
  92. };
  93. // Override the alert function to throw an exception instead.
  94. alert = function(s) {
  95. throw "Alert called with argument: " + s;
  96. };
  97. // To make the benchmark results predictable, we replace Math.random
  98. // with a 100% deterministic alternative.
  99. BenchmarkSuite.ResetRNG = function() {
  100. Math.random = (function() {
  101. var seed = 49734321;
  102. return function() {
  103. // Robert Jenkins' 32 bit integer hash function.
  104. seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff;
  105. seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff;
  106. seed = ((seed + 0x165667b1) + (seed << 5)) & 0xffffffff;
  107. seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff;
  108. seed = ((seed + 0xfd7046c5) + (seed << 3)) & 0xffffffff;
  109. seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff;
  110. return (seed & 0xfffffff) / 0x10000000;
  111. };
  112. })();
  113. }
  114. // Runs all registered benchmark suites and optionally yields between
  115. // each individual benchmark to avoid running for too long in the
  116. // context of browsers. Once done, the final score is reported to the
  117. // runner.
  118. BenchmarkSuite.RunSuites = function(runner, skipBenchmarks) {
  119. skipBenchmarks = typeof skipBenchmarks === 'undefined' ? [] : skipBenchmarks;
  120. var continuation = null;
  121. var suites = BenchmarkSuite.suites;
  122. var length = suites.length;
  123. BenchmarkSuite.scores = [];
  124. var index = 0;
  125. function RunStep() {
  126. while (continuation || index < length) {
  127. if (continuation) {
  128. continuation = continuation();
  129. } else {
  130. var suite = suites[index++];
  131. if (runner.NotifyStart) runner.NotifyStart(suite.name);
  132. if (skipBenchmarks.indexOf(suite.name) > -1) {
  133. suite.NotifySkipped(runner);
  134. } else {
  135. continuation = suite.RunStep(runner);
  136. }
  137. }
  138. if (continuation && typeof window != 'undefined' && window.setTimeout) {
  139. window.setTimeout(RunStep, 25);
  140. return;
  141. }
  142. }
  143. // show final result
  144. if (runner.NotifyScore) {
  145. var score = BenchmarkSuite.GeometricMean(BenchmarkSuite.scores);
  146. var formatted = BenchmarkSuite.FormatScore(100 * score);
  147. runner.NotifyScore(formatted);
  148. }
  149. }
  150. RunStep();
  151. }
  152. // Counts the total number of registered benchmarks. Useful for
  153. // showing progress as a percentage.
  154. BenchmarkSuite.CountBenchmarks = function() {
  155. var result = 0;
  156. var suites = BenchmarkSuite.suites;
  157. for (var i = 0; i < suites.length; i++) {
  158. result += suites[i].benchmarks.length;
  159. }
  160. return result;
  161. }
  162. // Computes the geometric mean of a set of numbers.
  163. BenchmarkSuite.GeometricMean = function(numbers) {
  164. var log = 0;
  165. for (var i = 0; i < numbers.length; i++) {
  166. log += Math.log(numbers[i]);
  167. }
  168. return Math.pow(Math.E, log / numbers.length);
  169. }
  170. // Computes the geometric mean of a set of throughput time measurements.
  171. BenchmarkSuite.GeometricMeanTime = function(measurements) {
  172. var log = 0;
  173. for (var i = 0; i < measurements.length; i++) {
  174. log += Math.log(measurements[i].time);
  175. }
  176. return Math.pow(Math.E, log / measurements.length);
  177. }
  178. // Computes the geometric mean of a set of rms measurements.
  179. BenchmarkSuite.GeometricMeanLatency = function(measurements) {
  180. var log = 0;
  181. var hasLatencyResult = false;
  182. for (var i = 0; i < measurements.length; i++) {
  183. if (measurements[i].latency != 0) {
  184. log += Math.log(measurements[i].latency);
  185. hasLatencyResult = true;
  186. }
  187. }
  188. if (hasLatencyResult) {
  189. return Math.pow(Math.E, log / measurements.length);
  190. } else {
  191. return 0;
  192. }
  193. }
  194. // Converts a score value to a string with at least three significant
  195. // digits.
  196. BenchmarkSuite.FormatScore = function(value) {
  197. if (value > 100) {
  198. return value.toFixed(0);
  199. } else {
  200. return value.toPrecision(3);
  201. }
  202. }
  203. // Notifies the runner that we're done running a single benchmark in
  204. // the benchmark suite. This can be useful to report progress.
  205. BenchmarkSuite.prototype.NotifyStep = function(result) {
  206. this.results.push(result);
  207. if (this.runner.NotifyStep) this.runner.NotifyStep(result.benchmark.name);
  208. }
  209. // Notifies the runner that we're done with running a suite and that
  210. // we have a result which can be reported to the user if needed.
  211. BenchmarkSuite.prototype.NotifyResult = function() {
  212. var mean = BenchmarkSuite.GeometricMeanTime(this.results);
  213. var score = this.reference[0] / mean;
  214. BenchmarkSuite.scores.push(score);
  215. if (this.runner.NotifyResult) {
  216. var formatted = BenchmarkSuite.FormatScore(100 * score);
  217. this.runner.NotifyResult(this.name, formatted);
  218. }
  219. if (this.reference.length == 2) {
  220. var meanLatency = BenchmarkSuite.GeometricMeanLatency(this.results);
  221. if (meanLatency != 0) {
  222. var scoreLatency = this.reference[1] / meanLatency;
  223. BenchmarkSuite.scores.push(scoreLatency);
  224. if (this.runner.NotifyResult) {
  225. var formattedLatency = BenchmarkSuite.FormatScore(100 * scoreLatency)
  226. this.runner.NotifyResult(this.name + "Latency", formattedLatency);
  227. }
  228. }
  229. }
  230. }
  231. BenchmarkSuite.prototype.NotifySkipped = function(runner) {
  232. BenchmarkSuite.scores.push(1); // push default reference score.
  233. if (runner.NotifyResult) {
  234. runner.NotifyResult(this.name, "Skipped");
  235. }
  236. }
  237. // Notifies the runner that running a benchmark resulted in an error.
  238. BenchmarkSuite.prototype.NotifyError = function(error) {
  239. if (this.runner.NotifyError) {
  240. this.runner.NotifyError(this.name, error);
  241. }
  242. if (this.runner.NotifyStep) {
  243. this.runner.NotifyStep(this.name);
  244. }
  245. }
  246. // Runs a single benchmark for at least a second and computes the
  247. // average time it takes to run a single iteration.
  248. BenchmarkSuite.prototype.RunSingleBenchmark = function(benchmark, data) {
  249. var config = BenchmarkSuite.config;
  250. var doWarmup = config.doWarmup !== undefined
  251. ? config.doWarmup
  252. : benchmark.doWarmup;
  253. var doDeterministic = config.doDeterministic !== undefined
  254. ? config.doDeterministic
  255. : benchmark.doDeterministic;
  256. function Measure(data) {
  257. var elapsed = 0;
  258. var start = new Date();
  259. // Run either for 1 second or for the number of iterations specified
  260. // by minIterations, depending on the config flag doDeterministic.
  261. for (var i = 0; (doDeterministic ?
  262. i<benchmark.deterministicIterations : elapsed < 1000); i++) {
  263. benchmark.run();
  264. elapsed = new Date() - start;
  265. }
  266. if (data != null) {
  267. data.runs += i;
  268. data.elapsed += elapsed;
  269. }
  270. }
  271. // Sets up data in order to skip or not the warmup phase.
  272. if (!doWarmup && data == null) {
  273. data = { runs: 0, elapsed: 0 };
  274. }
  275. if (data == null) {
  276. Measure(null);
  277. return { runs: 0, elapsed: 0 };
  278. } else {
  279. Measure(data);
  280. // If we've run too few iterations, we continue for another second.
  281. if (data.runs < benchmark.minIterations) return data;
  282. var usec = (data.elapsed * 1000) / data.runs;
  283. var rms = (benchmark.rmsResult != null) ? benchmark.rmsResult() : 0;
  284. this.NotifyStep(new BenchmarkResult(benchmark, usec, rms));
  285. return null;
  286. }
  287. }
  288. // This function starts running a suite, but stops between each
  289. // individual benchmark in the suite and returns a continuation
  290. // function which can be invoked to run the next benchmark. Once the
  291. // last benchmark has been executed, null is returned.
  292. BenchmarkSuite.prototype.RunStep = function(runner) {
  293. BenchmarkSuite.ResetRNG();
  294. this.results = [];
  295. this.runner = runner;
  296. var length = this.benchmarks.length;
  297. var index = 0;
  298. var suite = this;
  299. var data;
  300. // Run the setup, the actual benchmark, and the tear down in three
  301. // separate steps to allow the framework to yield between any of the
  302. // steps.
  303. function RunNextSetup() {
  304. if (index < length) {
  305. try {
  306. suite.benchmarks[index].Setup();
  307. } catch (e) {
  308. suite.NotifyError(e);
  309. return null;
  310. }
  311. return RunNextBenchmark;
  312. }
  313. suite.NotifyResult();
  314. return null;
  315. }
  316. function RunNextBenchmark() {
  317. try {
  318. data = suite.RunSingleBenchmark(suite.benchmarks[index], data);
  319. } catch (e) {
  320. suite.NotifyError(e);
  321. return null;
  322. }
  323. // If data is null, we're done with this benchmark.
  324. return (data == null) ? RunNextTearDown : RunNextBenchmark();
  325. }
  326. function RunNextTearDown() {
  327. try {
  328. suite.benchmarks[index++].TearDown();
  329. } catch (e) {
  330. suite.NotifyError(e);
  331. return null;
  332. }
  333. return RunNextSetup;
  334. }
  335. // Start out running the setup.
  336. return RunNextSetup();
  337. }
  338. /*
  339. * Copyright (c) 2003-2005 Tom Wu
  340. * All Rights Reserved.
  341. *
  342. * Permission is hereby granted, free of charge, to any person obtaining
  343. * a copy of this software and associated documentation files (the
  344. * "Software"), to deal in the Software without restriction, including
  345. * without limitation the rights to use, copy, modify, merge, publish,
  346. * distribute, sublicense, and/or sell copies of the Software, and to
  347. * permit persons to whom the Software is furnished to do so, subject to
  348. * the following conditions:
  349. *
  350. * The above copyright notice and this permission notice shall be
  351. * included in all copies or substantial portions of the Software.
  352. *
  353. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  354. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  355. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  356. *
  357. * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  358. * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  359. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  360. * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  361. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  362. *
  363. * In addition, the following condition applies:
  364. *
  365. * All redistributions must retain an intact copy of this copyright notice
  366. * and disclaimer.
  367. */
  368. // The code has been adapted for use as a benchmark by Google.
  369. var Crypto = new BenchmarkSuite('Crypto', [266181], [
  370. new Benchmark("Encrypt", true, false, 3900, encrypt),
  371. new Benchmark("Decrypt", true, false, 220, decrypt)
  372. ]);
  373. // Basic JavaScript BN library - subset useful for RSA encryption.
  374. // Bits per digit
  375. var dbits;
  376. var BI_DB;
  377. var BI_DM;
  378. var BI_DV;
  379. var BI_FP;
  380. var BI_FV;
  381. var BI_F1;
  382. var BI_F2;
  383. // JavaScript engine analysis
  384. var canary = 0xdeadbeefcafe;
  385. var j_lm = ((canary&0xffffff)==0xefcafe);
  386. // (public) Constructor
  387. function BigInteger(a,b,c) {
  388. this.array = new Array();
  389. if(a != null)
  390. if("number" == typeof a) this.fromNumber(a,b,c);
  391. else if(b == null && "string" != typeof a) this.fromString(a,256);
  392. else this.fromString(a,b);
  393. }
  394. // return new, unset BigInteger
  395. function nbi() { return new BigInteger(null); }
  396. // am: Compute w_j += (x*this_i), propagate carries,
  397. // c is initial carry, returns final carry.
  398. // c < 3*dvalue, x < 2*dvalue, this_i < dvalue
  399. // We need to select the fastest one that works in this environment.
  400. // am1: use a single mult and divide to get the high bits,
  401. // max digit bits should be 26 because
  402. // max internal value = 2*dvalue^2-2*dvalue (< 2^53)
  403. function am1(i,x,w,j,c,n) {
  404. var this_array = this.array;
  405. var w_array = w.array;
  406. while(--n >= 0) {
  407. var v = x*this_array[i++]+w_array[j]+c;
  408. c = Math.floor(v/0x4000000);
  409. w_array[j++] = v&0x3ffffff;
  410. }
  411. return c;
  412. }
  413. // am2 avoids a big mult-and-extract completely.
  414. // Max digit bits should be <= 30 because we do bitwise ops
  415. // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
  416. function am2(i,x,w,j,c,n) {
  417. var this_array = this.array;
  418. var w_array = w.array;
  419. var xl = x&0x7fff, xh = x>>15;
  420. while(--n >= 0) {
  421. var l = this_array[i]&0x7fff;
  422. var h = this_array[i++]>>15;
  423. var m = xh*l+h*xl;
  424. l = xl*l+((m&0x7fff)<<15)+w_array[j]+(c&0x3fffffff);
  425. c = (l>>>30)+(m>>>15)+xh*h+(c>>>30);
  426. w_array[j++] = l&0x3fffffff;
  427. }
  428. return c;
  429. }
  430. // Alternately, set max digit bits to 28 since some
  431. // browsers slow down when dealing with 32-bit numbers.
  432. function am3(i,x,w,j,c,n) {
  433. var this_array = this.array;
  434. var w_array = w.array;
  435. var xl = x&0x3fff, xh = x>>14;
  436. while(--n >= 0) {
  437. var l = this_array[i]&0x3fff;
  438. var h = this_array[i++]>>14;
  439. var m = xh*l+h*xl;
  440. l = xl*l+((m&0x3fff)<<14)+w_array[j]+c;
  441. c = (l>>28)+(m>>14)+xh*h;
  442. w_array[j++] = l&0xfffffff;
  443. }
  444. return c;
  445. }
  446. // This is tailored to VMs with 2-bit tagging. It makes sure
  447. // that all the computations stay within the 29 bits available.
  448. function am4(i,x,w,j,c,n) {
  449. var this_array = this.array;
  450. var w_array = w.array;
  451. var xl = x&0x1fff, xh = x>>13;
  452. while(--n >= 0) {
  453. var l = this_array[i]&0x1fff;
  454. var h = this_array[i++]>>13;
  455. var m = xh*l+h*xl;
  456. l = xl*l+((m&0x1fff)<<13)+w_array[j]+c;
  457. c = (l>>26)+(m>>13)+xh*h;
  458. w_array[j++] = l&0x3ffffff;
  459. }
  460. return c;
  461. }
  462. // am3/28 is best for SM, Rhino, but am4/26 is best for v8.
  463. // Kestrel (Opera 9.5) gets its best result with am4/26.
  464. // IE7 does 9% better with am3/28 than with am4/26.
  465. // Firefox (SM) gets 10% faster with am3/28 than with am4/26.
  466. setupEngine = function(fn, bits) {
  467. BigInteger.prototype.am = fn;
  468. dbits = bits;
  469. BI_DB = dbits;
  470. BI_DM = ((1<<dbits)-1);
  471. BI_DV = (1<<dbits);
  472. BI_FP = 52;
  473. BI_FV = Math.pow(2,BI_FP);
  474. BI_F1 = BI_FP-dbits;
  475. BI_F2 = 2*dbits-BI_FP;
  476. }
  477. // Digit conversions
  478. var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
  479. var BI_RC = new Array();
  480. var rr,vv;
  481. rr = "0".charCodeAt(0);
  482. for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
  483. rr = "a".charCodeAt(0);
  484. for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
  485. rr = "A".charCodeAt(0);
  486. for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
  487. function int2char(n) { return BI_RM.charAt(n); }
  488. function intAt(s,i) {
  489. var c = BI_RC[s.charCodeAt(i)];
  490. return (c==null)?-1:c;
  491. }
  492. // (protected) copy this to r
  493. function bnpCopyTo(r) {
  494. var this_array = this.array;
  495. var r_array = r.array;
  496. for(var i = this.t-1; i >= 0; --i) r_array[i] = this_array[i];
  497. r.t = this.t;
  498. r.s = this.s;
  499. }
  500. // (protected) set from integer value x, -DV <= x < DV
  501. function bnpFromInt(x) {
  502. var this_array = this.array;
  503. this.t = 1;
  504. this.s = (x<0)?-1:0;
  505. if(x > 0) this_array[0] = x;
  506. else if(x < -1) this_array[0] = x+DV;
  507. else this.t = 0;
  508. }
  509. // return bigint initialized to value
  510. function nbv(i) { var r = nbi(); r.fromInt(i); return r; }
  511. // (protected) set from string and radix
  512. function bnpFromString(s,b) {
  513. var this_array = this.array;
  514. var k;
  515. if(b == 16) k = 4;
  516. else if(b == 8) k = 3;
  517. else if(b == 256) k = 8; // byte array
  518. else if(b == 2) k = 1;
  519. else if(b == 32) k = 5;
  520. else if(b == 4) k = 2;
  521. else { this.fromRadix(s,b); return; }
  522. this.t = 0;
  523. this.s = 0;
  524. var i = s.length, mi = false, sh = 0;
  525. while(--i >= 0) {
  526. var x = (k==8)?s[i]&0xff:intAt(s,i);
  527. if(x < 0) {
  528. if(s.charAt(i) == "-") mi = true;
  529. continue;
  530. }
  531. mi = false;
  532. if(sh == 0)
  533. this_array[this.t++] = x;
  534. else if(sh+k > BI_DB) {
  535. this_array[this.t-1] |= (x&((1<<(BI_DB-sh))-1))<<sh;
  536. this_array[this.t++] = (x>>(BI_DB-sh));
  537. }
  538. else
  539. this_array[this.t-1] |= x<<sh;
  540. sh += k;
  541. if(sh >= BI_DB) sh -= BI_DB;
  542. }
  543. if(k == 8 && (s[0]&0x80) != 0) {
  544. this.s = -1;
  545. if(sh > 0) this_array[this.t-1] |= ((1<<(BI_DB-sh))-1)<<sh;
  546. }
  547. this.clamp();
  548. if(mi) BigInteger.ZERO.subTo(this,this);
  549. }
  550. // (protected) clamp off excess high words
  551. function bnpClamp() {
  552. var this_array = this.array;
  553. var c = this.s&BI_DM;
  554. while(this.t > 0 && this_array[this.t-1] == c) --this.t;
  555. }
  556. // (public) return string representation in given radix
  557. function bnToString(b) {
  558. var this_array = this.array;
  559. if(this.s < 0) return "-"+this.negate().toString(b);
  560. var k;
  561. if(b == 16) k = 4;
  562. else if(b == 8) k = 3;
  563. else if(b == 2) k = 1;
  564. else if(b == 32) k = 5;
  565. else if(b == 4) k = 2;
  566. else return this.toRadix(b);
  567. var km = (1<<k)-1, d, m = false, r = "", i = this.t;
  568. var p = BI_DB-(i*BI_DB)%k;
  569. if(i-- > 0) {
  570. if(p < BI_DB && (d = this_array[i]>>p) > 0) { m = true; r = int2char(d); }
  571. while(i >= 0) {
  572. if(p < k) {
  573. d = (this_array[i]&((1<<p)-1))<<(k-p);
  574. d |= this_array[--i]>>(p+=BI_DB-k);
  575. }
  576. else {
  577. d = (this_array[i]>>(p-=k))&km;
  578. if(p <= 0) { p += BI_DB; --i; }
  579. }
  580. if(d > 0) m = true;
  581. if(m) r += int2char(d);
  582. }
  583. }
  584. return m?r:"0";
  585. }
  586. // (public) -this
  587. function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; }
  588. // (public) |this|
  589. function bnAbs() { return (this.s<0)?this.negate():this; }
  590. // (public) return + if this > a, - if this < a, 0 if equal
  591. function bnCompareTo(a) {
  592. var this_array = this.array;
  593. var a_array = a.array;
  594. var r = this.s-a.s;
  595. if(r != 0) return r;
  596. var i = this.t;
  597. r = i-a.t;
  598. if(r != 0) return r;
  599. while(--i >= 0) if((r=this_array[i]-a_array[i]) != 0) return r;
  600. return 0;
  601. }
  602. // returns bit length of the integer x
  603. function nbits(x) {
  604. var r = 1, t;
  605. if((t=x>>>16) != 0) { x = t; r += 16; }
  606. if((t=x>>8) != 0) { x = t; r += 8; }
  607. if((t=x>>4) != 0) { x = t; r += 4; }
  608. if((t=x>>2) != 0) { x = t; r += 2; }
  609. if((t=x>>1) != 0) { x = t; r += 1; }
  610. return r;
  611. }
  612. // (public) return the number of bits in "this"
  613. function bnBitLength() {
  614. var this_array = this.array;
  615. if(this.t <= 0) return 0;
  616. return BI_DB*(this.t-1)+nbits(this_array[this.t-1]^(this.s&BI_DM));
  617. }
  618. // (protected) r = this << n*DB
  619. function bnpDLShiftTo(n,r) {
  620. var this_array = this.array;
  621. var r_array = r.array;
  622. var i;
  623. for(i = this.t-1; i >= 0; --i) r_array[i+n] = this_array[i];
  624. for(i = n-1; i >= 0; --i) r_array[i] = 0;
  625. r.t = this.t+n;
  626. r.s = this.s;
  627. }
  628. // (protected) r = this >> n*DB
  629. function bnpDRShiftTo(n,r) {
  630. var this_array = this.array;
  631. var r_array = r.array;
  632. for(var i = n; i < this.t; ++i) r_array[i-n] = this_array[i];
  633. r.t = Math.max(this.t-n,0);
  634. r.s = this.s;
  635. }
  636. // (protected) r = this << n
  637. function bnpLShiftTo(n,r) {
  638. var this_array = this.array;
  639. var r_array = r.array;
  640. var bs = n%BI_DB;
  641. var cbs = BI_DB-bs;
  642. var bm = (1<<cbs)-1;
  643. var ds = Math.floor(n/BI_DB), c = (this.s<<bs)&BI_DM, i;
  644. for(i = this.t-1; i >= 0; --i) {
  645. r_array[i+ds+1] = (this_array[i]>>cbs)|c;
  646. c = (this_array[i]&bm)<<bs;
  647. }
  648. for(i = ds-1; i >= 0; --i) r_array[i] = 0;
  649. r_array[ds] = c;
  650. r.t = this.t+ds+1;
  651. r.s = this.s;
  652. r.clamp();
  653. }
  654. // (protected) r = this >> n
  655. function bnpRShiftTo(n,r) {
  656. var this_array = this.array;
  657. var r_array = r.array;
  658. r.s = this.s;
  659. var ds = Math.floor(n/BI_DB);
  660. if(ds >= this.t) { r.t = 0; return; }
  661. var bs = n%BI_DB;
  662. var cbs = BI_DB-bs;
  663. var bm = (1<<bs)-1;
  664. r_array[0] = this_array[ds]>>bs;
  665. for(var i = ds+1; i < this.t; ++i) {
  666. r_array[i-ds-1] |= (this_array[i]&bm)<<cbs;
  667. r_array[i-ds] = this_array[i]>>bs;
  668. }
  669. if(bs > 0) r_array[this.t-ds-1] |= (this.s&bm)<<cbs;
  670. r.t = this.t-ds;
  671. r.clamp();
  672. }
  673. // (protected) r = this - a
  674. function bnpSubTo(a,r) {
  675. var this_array = this.array;
  676. var r_array = r.array;
  677. var a_array = a.array;
  678. var i = 0, c = 0, m = Math.min(a.t,this.t);
  679. while(i < m) {
  680. c += this_array[i]-a_array[i];
  681. r_array[i++] = c&BI_DM;
  682. c >>= BI_DB;
  683. }
  684. if(a.t < this.t) {
  685. c -= a.s;
  686. while(i < this.t) {
  687. c += this_array[i];
  688. r_array[i++] = c&BI_DM;
  689. c >>= BI_DB;
  690. }
  691. c += this.s;
  692. }
  693. else {
  694. c += this.s;
  695. while(i < a.t) {
  696. c -= a_array[i];
  697. r_array[i++] = c&BI_DM;
  698. c >>= BI_DB;
  699. }
  700. c -= a.s;
  701. }
  702. r.s = (c<0)?-1:0;
  703. if(c < -1) r_array[i++] = BI_DV+c;
  704. else if(c > 0) r_array[i++] = c;
  705. r.t = i;
  706. r.clamp();
  707. }
  708. // (protected) r = this * a, r != this,a (HAC 14.12)
  709. // "this" should be the larger one if appropriate.
  710. function bnpMultiplyTo(a,r) {
  711. var this_array = this.array;
  712. var r_array = r.array;
  713. var x = this.abs(), y = a.abs();
  714. var y_array = y.array;
  715. var i = x.t;
  716. r.t = i+y.t;
  717. while(--i >= 0) r_array[i] = 0;
  718. for(i = 0; i < y.t; ++i) r_array[i+x.t] = x.am(0,y_array[i],r,i,0,x.t);
  719. r.s = 0;
  720. r.clamp();
  721. if(this.s != a.s) BigInteger.ZERO.subTo(r,r);
  722. }
  723. // (protected) r = this^2, r != this (HAC 14.16)
  724. function bnpSquareTo(r) {
  725. var x = this.abs();
  726. var x_array = x.array;
  727. var r_array = r.array;
  728. var i = r.t = 2*x.t;
  729. while(--i >= 0) r_array[i] = 0;
  730. for(i = 0; i < x.t-1; ++i) {
  731. var c = x.am(i,x_array[i],r,2*i,0,1);
  732. 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) {
  733. r_array[i+x.t] -= BI_DV;
  734. r_array[i+x.t+1] = 1;
  735. }
  736. }
  737. if(r.t > 0) r_array[r.t-1] += x.am(i,x_array[i],r,2*i,0,1);
  738. r.s = 0;
  739. r.clamp();
  740. }
  741. // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
  742. // r != q, this != m. q or r may be null.
  743. function bnpDivRemTo(m,q,r) {
  744. var pm = m.abs();
  745. if(pm.t <= 0) return;
  746. var pt = this.abs();
  747. if(pt.t < pm.t) {
  748. if(q != null) q.fromInt(0);
  749. if(r != null) this.copyTo(r);
  750. return;
  751. }
  752. if(r == null) r = nbi();
  753. var y = nbi(), ts = this.s, ms = m.s;
  754. var pm_array = pm.array;
  755. var nsh = BI_DB-nbits(pm_array[pm.t-1]); // normalize modulus
  756. if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); }
  757. else { pm.copyTo(y); pt.copyTo(r); }
  758. var ys = y.t;
  759. var y_array = y.array;
  760. var y0 = y_array[ys-1];
  761. if(y0 == 0) return;
  762. var yt = y0*(1<<BI_F1)+((ys>1)?y_array[ys-2]>>BI_F2:0);
  763. var d1 = BI_FV/yt, d2 = (1<<BI_F1)/yt, e = 1<<BI_F2;
  764. var i = r.t, j = i-ys, t = (q==null)?nbi():q;
  765. y.dlShiftTo(j,t);
  766. var r_array = r.array;
  767. if(r.compareTo(t) >= 0) {
  768. r_array[r.t++] = 1;
  769. r.subTo(t,r);
  770. }
  771. BigInteger.ONE.dlShiftTo(ys,t);
  772. t.subTo(y,y); // "negative" y so we can replace sub with am later
  773. while(y.t < ys) y_array[y.t++] = 0;
  774. while(--j >= 0) {
  775. // Estimate quotient digit
  776. var qd = (r_array[--i]==y0)?BI_DM:Math.floor(r_array[i]*d1+(r_array[i-1]+e)*d2);
  777. if((r_array[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out
  778. y.dlShiftTo(j,t);
  779. r.subTo(t,r);
  780. while(r_array[i] < --qd) r.subTo(t,r);
  781. }
  782. }
  783. if(q != null) {
  784. r.drShiftTo(ys,q);
  785. if(ts != ms) BigInteger.ZERO.subTo(q,q);
  786. }
  787. r.t = ys;
  788. r.clamp();
  789. if(nsh > 0) r.rShiftTo(nsh,r); // Denormalize remainder
  790. if(ts < 0) BigInteger.ZERO.subTo(r,r);
  791. }
  792. // (public) this mod a
  793. function bnMod(a) {
  794. var r = nbi();
  795. this.abs().divRemTo(a,null,r);
  796. if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r);
  797. return r;
  798. }
  799. // Modular reduction using "classic" algorithm
  800. function Classic(m) { this.m = m; }
  801. function cConvert(x) {
  802. if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
  803. else return x;
  804. }
  805. function cRevert(x) { return x; }
  806. function cReduce(x) { x.divRemTo(this.m,null,x); }
  807. function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
  808. function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
  809. Classic.prototype.convert = cConvert;
  810. Classic.prototype.revert = cRevert;
  811. Classic.prototype.reduce = cReduce;
  812. Classic.prototype.mulTo = cMulTo;
  813. Classic.prototype.sqrTo = cSqrTo;
  814. // (protected) return "-1/this % 2^DB"; useful for Mont. reduction
  815. // justification:
  816. // xy == 1 (mod m)
  817. // xy = 1+km
  818. // xy(2-xy) = (1+km)(1-km)
  819. // x[y(2-xy)] = 1-k^2m^2
  820. // x[y(2-xy)] == 1 (mod m^2)
  821. // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
  822. // should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
  823. // JS multiply "overflows" differently from C/C++, so care is needed here.
  824. function bnpInvDigit() {
  825. var this_array = this.array;
  826. if(this.t < 1) return 0;
  827. var x = this_array[0];
  828. if((x&1) == 0) return 0;
  829. var y = x&3; // y == 1/x mod 2^2
  830. y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4
  831. y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8
  832. y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff; // y == 1/x mod 2^16
  833. // last step - calculate inverse mod DV directly;
  834. // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
  835. y = (y*(2-x*y%BI_DV))%BI_DV; // y == 1/x mod 2^dbits
  836. // we really want the negative inverse, and -DV < y < DV
  837. return (y>0)?BI_DV-y:-y;
  838. }
  839. // Montgomery reduction
  840. function Montgomery(m) {
  841. this.m = m;
  842. this.mp = m.invDigit();
  843. this.mpl = this.mp&0x7fff;
  844. this.mph = this.mp>>15;
  845. this.um = (1<<(BI_DB-15))-1;
  846. this.mt2 = 2*m.t;
  847. }
  848. // xR mod m
  849. function montConvert(x) {
  850. var r = nbi();
  851. x.abs().dlShiftTo(this.m.t,r);
  852. r.divRemTo(this.m,null,r);
  853. if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r);
  854. return r;
  855. }
  856. // x/R mod m
  857. function montRevert(x) {
  858. var r = nbi();
  859. x.copyTo(r);
  860. this.reduce(r);
  861. return r;
  862. }
  863. // x = x/R mod m (HAC 14.32)
  864. function montReduce(x) {
  865. var x_array = x.array;
  866. while(x.t <= this.mt2) // pad x so am has enough room later
  867. x_array[x.t++] = 0;
  868. for(var i = 0; i < this.m.t; ++i) {
  869. // faster way of calculating u0 = x[i]*mp mod DV
  870. var j = x_array[i]&0x7fff;
  871. var u0 = (j*this.mpl+(((j*this.mph+(x_array[i]>>15)*this.mpl)&this.um)<<15))&BI_DM;
  872. // use am to combine the multiply-shift-add into one call
  873. j = i+this.m.t;
  874. x_array[j] += this.m.am(0,u0,x,i,0,this.m.t);
  875. // propagate carry
  876. while(x_array[j] >= BI_DV) { x_array[j] -= BI_DV; x_array[++j]++; }
  877. }
  878. x.clamp();
  879. x.drShiftTo(this.m.t,x);
  880. if(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
  881. }
  882. // r = "x^2/R mod m"; x != r
  883. function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
  884. // r = "xy/R mod m"; x,y != r
  885. function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
  886. Montgomery.prototype.convert = montConvert;
  887. Montgomery.prototype.revert = montRevert;
  888. Montgomery.prototype.reduce = montReduce;
  889. Montgomery.prototype.mulTo = montMulTo;
  890. Montgomery.prototype.sqrTo = montSqrTo;
  891. // (protected) true iff this is even
  892. function bnpIsEven() {
  893. var this_array = this.array;
  894. return ((this.t>0)?(this_array[0]&1):this.s) == 0;
  895. }
  896. // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
  897. function bnpExp(e,z) {
  898. if(e > 0xffffffff || e < 1) return BigInteger.ONE;
  899. var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1;
  900. g.copyTo(r);
  901. while(--i >= 0) {
  902. z.sqrTo(r,r2);
  903. if((e&(1<<i)) > 0) z.mulTo(r2,g,r);
  904. else { var t = r; r = r2; r2 = t; }
  905. }
  906. return z.revert(r);
  907. }
  908. // (public) this^e % m, 0 <= e < 2^32
  909. function bnModPowInt(e,m) {
  910. var z;
  911. if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m);
  912. return this.exp(e,z);
  913. }
  914. // protected
  915. BigInteger.prototype.copyTo = bnpCopyTo;
  916. BigInteger.prototype.fromInt = bnpFromInt;
  917. BigInteger.prototype.fromString = bnpFromString;
  918. BigInteger.prototype.clamp = bnpClamp;
  919. BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
  920. BigInteger.prototype.drShiftTo = bnpDRShiftTo;
  921. BigInteger.prototype.lShiftTo = bnpLShiftTo;
  922. BigInteger.prototype.rShiftTo = bnpRShiftTo;
  923. BigInteger.prototype.subTo = bnpSubTo;
  924. BigInteger.prototype.multiplyTo = bnpMultiplyTo;
  925. BigInteger.prototype.squareTo = bnpSquareTo;
  926. BigInteger.prototype.divRemTo = bnpDivRemTo;
  927. BigInteger.prototype.invDigit = bnpInvDigit;
  928. BigInteger.prototype.isEven = bnpIsEven;
  929. BigInteger.prototype.exp = bnpExp;
  930. // public
  931. BigInteger.prototype.toString = bnToString;
  932. BigInteger.prototype.negate = bnNegate;
  933. BigInteger.prototype.abs = bnAbs;
  934. BigInteger.prototype.compareTo = bnCompareTo;
  935. BigInteger.prototype.bitLength = bnBitLength;
  936. BigInteger.prototype.mod = bnMod;
  937. BigInteger.prototype.modPowInt = bnModPowInt;
  938. // "constants"
  939. BigInteger.ZERO = nbv(0);
  940. BigInteger.ONE = nbv(1);
  941. // Copyright (c) 2005 Tom Wu
  942. // All Rights Reserved.
  943. // See "LICENSE" for details.
  944. // Extended JavaScript BN functions, required for RSA private ops.
  945. // (public)
  946. function bnClone() { var r = nbi(); this.copyTo(r); return r; }
  947. // (public) return value as integer
  948. function bnIntValue() {
  949. var this_array = this.array;
  950. if(this.s < 0) {
  951. if(this.t == 1) return this_array[0]-BI_DV;
  952. else if(this.t == 0) return -1;
  953. }
  954. else if(this.t == 1) return this_array[0];
  955. else if(this.t == 0) return 0;
  956. // assumes 16 < DB < 32
  957. return ((this_array[1]&((1<<(32-BI_DB))-1))<<BI_DB)|this_array[0];
  958. }
  959. // (public) return value as byte
  960. function bnByteValue() {
  961. var this_array = this.array;
  962. return (this.t==0)?this.s:(this_array[0]<<24)>>24;
  963. }
  964. // (public) return value as short (assumes DB>=16)
  965. function bnShortValue() {
  966. var this_array = this.array;
  967. return (this.t==0)?this.s:(this_array[0]<<16)>>16;
  968. }
  969. // (protected) return x s.t. r^x < DV
  970. function bnpChunkSize(r) { return Math.floor(Math.LN2*BI_DB/Math.log(r)); }
  971. // (public) 0 if this == 0, 1 if this > 0
  972. function bnSigNum() {
  973. var this_array = this.array;
  974. if(this.s < 0) return -1;
  975. else if(this.t <= 0 || (this.t == 1 && this_array[0] <= 0)) return 0;
  976. else return 1;
  977. }
  978. // (protected) convert to radix string
  979. function bnpToRadix(b) {
  980. if(b == null) b = 10;
  981. if(this.signum() == 0 || b < 2 || b > 36) return "0";
  982. var cs = this.chunkSize(b);
  983. var a = Math.pow(b,cs);
  984. var d = nbv(a), y = nbi(), z = nbi(), r = "";
  985. this.divRemTo(d,y,z);
  986. while(y.signum() > 0) {
  987. r = (a+z.intValue()).toString(b).substr(1) + r;
  988. y.divRemTo(d,y,z);
  989. }
  990. return z.intValue().toString(b) + r;
  991. }
  992. // (protected) convert from radix string
  993. function bnpFromRadix(s,b) {
  994. this.fromInt(0);
  995. if(b == null) b = 10;
  996. var cs = this.chunkSize(b);
  997. var d = Math.pow(b,cs), mi = false, j = 0, w = 0;
  998. for(var i = 0; i < s.length; ++i) {
  999. var x = intAt(s,i);
  1000. if(x < 0) {
  1001. if(s.charAt(i) == "-" && this.signum() == 0) mi = true;
  1002. continue;
  1003. }
  1004. w = b*w+x;
  1005. if(++j >= cs) {
  1006. this.dMultiply(d);
  1007. this.dAddOffset(w,0);
  1008. j = 0;
  1009. w = 0;
  1010. }
  1011. }
  1012. if(j > 0) {
  1013. this.dMultiply(Math.pow(b,j));
  1014. this.dAddOffset(w,0);
  1015. }
  1016. if(mi) BigInteger.ZERO.subTo(this,this);
  1017. }
  1018. // (protected) alternate constructor
  1019. function bnpFromNumber(a,b,c) {
  1020. if("number" == typeof b) {
  1021. // new BigInteger(int,int,RNG)
  1022. if(a < 2) this.fromInt(1);
  1023. else {
  1024. this.fromNumber(a,c);
  1025. if(!this.testBit(a-1)) // force MSB set
  1026. this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this);
  1027. if(this.isEven()) this.dAddOffset(1,0); // force odd
  1028. while(!this.isProbablePrime(b)) {
  1029. this.dAddOffset(2,0);
  1030. if(this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a-1),this);
  1031. }
  1032. }
  1033. }
  1034. else {
  1035. // new BigInteger(int,RNG)
  1036. var x = new Array(), t = a&7;
  1037. x.length = (a>>3)+1;
  1038. b.nextBytes(x);
  1039. if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0;
  1040. this.fromString(x,256);
  1041. }
  1042. }
  1043. // (public) convert to bigendian byte array
  1044. function bnToByteArray() {
  1045. var this_array = this.array;
  1046. var i = this.t, r = new Array();
  1047. r[0] = this.s;
  1048. var p = BI_DB-(i*BI_DB)%8, d, k = 0;
  1049. if(i-- > 0) {
  1050. if(p < BI_DB && (d = this_array[i]>>p) != (this.s&BI_DM)>>p)
  1051. r[k++] = d|(this.s<<(BI_DB-p));
  1052. while(i >= 0) {
  1053. if(p < 8) {
  1054. d = (this_array[i]&((1<<p)-1))<<(8-p);
  1055. d |= this_array[--i]>>(p+=BI_DB-8);
  1056. }
  1057. else {
  1058. d = (this_array[i]>>(p-=8))&0xff;
  1059. if(p <= 0) { p += BI_DB; --i; }
  1060. }
  1061. if((d&0x80) != 0) d |= -256;
  1062. if(k == 0 && (this.s&0x80) != (d&0x80)) ++k;
  1063. if(k > 0 || d != this.s) r[k++] = d;
  1064. }
  1065. }
  1066. return r;
  1067. }
  1068. function bnEquals(a) { return(this.compareTo(a)==0); }
  1069. function bnMin(a) { return(this.compareTo(a)<0)?this:a; }
  1070. function bnMax(a) { return(this.compareTo(a)>0)?this:a; }
  1071. // (protected) r = this op a (bitwise)
  1072. function bnpBitwiseTo(a,op,r) {
  1073. var this_array = this.array;
  1074. var a_array = a.array;
  1075. var r_array = r.array;
  1076. var i, f, m = Math.min(a.t,this.t);
  1077. for(i = 0; i < m; ++i) r_array[i] = op(this_array[i],a_array[i]);
  1078. if(a.t < this.t) {
  1079. f = a.s&BI_DM;
  1080. for(i = m; i < this.t; ++i) r_array[i] = op(this_array[i],f);
  1081. r.t = this.t;
  1082. }
  1083. else {
  1084. f = this.s&BI_DM;
  1085. for(i = m; i < a.t; ++i) r_array[i] = op(f,a_array[i]);
  1086. r.t = a.t;
  1087. }
  1088. r.s = op(this.s,a.s);
  1089. r.clamp();
  1090. }
  1091. // (public) this & a
  1092. function op_and(x,y) { return x&y; }
  1093. function bnAnd(a) { var r = nbi(); this.bitwiseTo(a,op_and,r); return r; }
  1094. // (public) this | a
  1095. function op_or(x,y) { return x|y; }
  1096. function bnOr(a) { var r = nbi(); this.bitwiseTo(a,op_or,r); return r; }
  1097. // (public) this ^ a
  1098. function op_xor(x,y) { return x^y; }
  1099. function bnXor(a) { var r = nbi(); this.bitwiseTo(a,op_xor,r); return r; }
  1100. // (public) this & ~a
  1101. function op_andnot(x,y) { return x&~y; }
  1102. function bnAndNot(a) { var r = nbi(); this.bitwiseTo(a,op_andnot,r); return r; }
  1103. // (public) ~this
  1104. function bnNot() {
  1105. var this_array = this.array;
  1106. var r = nbi();
  1107. var r_array = r.array;
  1108. for(var i = 0; i < this.t; ++i) r_array[i] = BI_DM&~this_array[i];
  1109. r.t = this.t;
  1110. r.s = ~this.s;
  1111. return r;
  1112. }
  1113. // (public) this << n
  1114. function bnShiftLeft(n) {
  1115. var r = nbi();
  1116. if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r);
  1117. return r;
  1118. }
  1119. // (public) this >> n
  1120. function bnShiftRight(n) {
  1121. var r = nbi();
  1122. if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r);
  1123. return r;
  1124. }
  1125. // return index of lowest 1-bit in x, x < 2^31
  1126. function lbit(x) {
  1127. if(x == 0) return -1;
  1128. var r = 0;
  1129. if((x&0xffff) == 0) { x >>= 16; r += 16; }
  1130. if((x&0xff) == 0) { x >>= 8; r += 8; }
  1131. if((x&0xf) == 0) { x >>= 4; r += 4; }
  1132. if((x&3) == 0) { x >>= 2; r += 2; }
  1133. if((x&1) == 0) ++r;
  1134. return r;
  1135. }
  1136. // (public) returns index of lowest 1-bit (or -1 if none)
  1137. function bnGetLowestSetBit() {
  1138. var this_array = this.array;
  1139. for(var i = 0; i < this.t; ++i)
  1140. if(this_array[i] != 0) return i*BI_DB+lbit(this_array[i]);
  1141. if(this.s < 0) return this.t*BI_DB;
  1142. return -1;
  1143. }
  1144. // return number of 1 bits in x
  1145. function cbit(x) {
  1146. var r = 0;
  1147. while(x != 0) { x &= x-1; ++r; }
  1148. return r;
  1149. }
  1150. // (public) return number of set bits
  1151. function bnBitCount() {
  1152. var r = 0, x = this.s&BI_DM;
  1153. for(var i = 0; i < this.t; ++i) r += cbit(this_array[i]^x);
  1154. return r;
  1155. }
  1156. // (public) true iff nth bit is set
  1157. function bnTestBit(n) {
  1158. var this_array = this.array;
  1159. var j = Math.floor(n/BI_DB);
  1160. if(j >= this.t) return(this.s!=0);
  1161. return((this_array[j]&(1<<(n%BI_DB)))!=0);
  1162. }
  1163. // (protected) this op (1<<n)
  1164. function bnpChangeBit(n,op) {
  1165. var r = BigInteger.ONE.shiftLeft(n);
  1166. this.bitwiseTo(r,op,r);
  1167. return r;
  1168. }
  1169. // (public) this | (1<<n)
  1170. function bnSetBit(n) { return this.changeBit(n,op_or); }
  1171. // (public) this & ~(1<<n)
  1172. function bnClearBit(n) { return this.changeBit(n,op_andnot); }
  1173. // (public) this ^ (1<<n)
  1174. function bnFlipBit(n) { return this.changeBit(n,op_xor); }
  1175. // (protected) r = this + a
  1176. function bnpAddTo(a,r) {
  1177. var this_array = this.array;
  1178. var a_array = a.array;
  1179. var r_array = r.array;
  1180. var i = 0, c = 0, m = Math.min(a.t,this.t);
  1181. while(i < m) {
  1182. c += this_array[i]+a_array[i];
  1183. r_array[i++] = c&BI_DM;
  1184. c >>= BI_DB;
  1185. }
  1186. if(a.t < this.t) {
  1187. c += a.s;
  1188. while(i < this.t) {
  1189. c += this_array[i];
  1190. r_array[i++] = c&BI_DM;
  1191. c >>= BI_DB;
  1192. }
  1193. c += this.s;
  1194. }
  1195. else {
  1196. c += this.s;
  1197. while(i < a.t) {
  1198. c += a_array[i];
  1199. r_array[i++] = c&BI_DM;
  1200. c >>= BI_DB;
  1201. }
  1202. c += a.s;
  1203. }
  1204. r.s = (c<0)?-1:0;
  1205. if(c > 0) r_array[i++] = c;
  1206. else if(c < -1) r_array[i++] = BI_DV+c;
  1207. r.t = i;
  1208. r.clamp();
  1209. }
  1210. // (public) this + a
  1211. function bnAdd(a) { var r = nbi(); this.addTo(a,r); return r; }
  1212. // (public) this - a
  1213. function bnSubtract(a) { var r = nbi(); this.subTo(a,r); return r; }
  1214. // (public) this * a
  1215. function bnMultiply(a) { var r = nbi(); this.multiplyTo(a,r); return r; }
  1216. // (public) this / a
  1217. function bnDivide(a) { var r = nbi(); this.divRemTo(a,r,null); return r; }
  1218. // (public) this % a
  1219. function bnRemainder(a) { var r = nbi(); this.divRemTo(a,null,r); return r; }
  1220. // (public) [this/a,this%a]
  1221. function bnDivideAndRemainder(a) {
  1222. var q = nbi(), r = nbi();
  1223. this.divRemTo(a,q,r);
  1224. return new Array(q,r);
  1225. }
  1226. // (protected) this *= n, this >= 0, 1 < n < DV
  1227. function bnpDMultiply(n) {
  1228. var this_array = this.array;
  1229. this_array[this.t] = this.am(0,n-1,this,0,0,this.t);
  1230. ++this.t;
  1231. this.clamp();
  1232. }
  1233. // (protected) this += n << w words, this >= 0
  1234. function bnpDAddOffset(n,w) {
  1235. var this_array = this.array;
  1236. while(this.t <= w) this_array[this.t++] = 0;
  1237. this_array[w] += n;
  1238. while(this_array[w] >= BI_DV) {
  1239. this_array[w] -= BI_DV;
  1240. if(++w >= this.t) this_array[this.t++] = 0;
  1241. ++this_array[w];
  1242. }
  1243. }
  1244. // A "null" reducer
  1245. function NullExp() {}
  1246. function nNop(x) { return x; }
  1247. function nMulTo(x,y,r) { x.multiplyTo(y,r); }
  1248. function nSqrTo(x,r) { x.squareTo(r); }
  1249. NullExp.prototype.convert = nNop;
  1250. NullExp.prototype.revert = nNop;
  1251. NullExp.prototype.mulTo = nMulTo;
  1252. NullExp.prototype.sqrTo = nSqrTo;
  1253. // (public) this^e
  1254. function bnPow(e) { return this.exp(e,new NullExp()); }
  1255. // (protected) r = lower n words of "this * a", a.t <= n
  1256. // "this" should be the larger one if appropriate.
  1257. function bnpMultiplyLowerTo(a,n,r) {
  1258. var r_array = r.array;
  1259. var a_array = a.array;
  1260. var i = Math.min(this.t+a.t,n);
  1261. r.s = 0; // assumes a,this >= 0
  1262. r.t = i;
  1263. while(i > 0) r_array[--i] = 0;
  1264. var j;
  1265. 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);
  1266. for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a_array[i],r,i,0,n-i);
  1267. r.clamp();
  1268. }
  1269. // (protected) r = "this * a" without lower n words, n > 0
  1270. // "this" should be the larger one if appropriate.
  1271. function bnpMultiplyUpperTo(a,n,r) {
  1272. var r_array = r.array;
  1273. var a_array = a.array;
  1274. --n;
  1275. var i = r.t = this.t+a.t-n;
  1276. r.s = 0; // assumes a,this >= 0
  1277. while(--i >= 0) r_array[i] = 0;
  1278. for(i = Math.max(n-this.t,0); i < a.t; ++i)
  1279. r_array[this.t+i-n] = this.am(n-i,a_array[i],r,0,0,this.t+i-n);
  1280. r.clamp();
  1281. r.drShiftTo(1,r);
  1282. }
  1283. // Barrett modular reduction
  1284. function Barrett(m) {
  1285. // setup Barrett
  1286. this.r2 = nbi();
  1287. this.q3 = nbi();
  1288. BigInteger.ONE.dlShiftTo(2*m.t,this.r2);
  1289. this.mu = this.r2.divide(m);
  1290. this.m = m;
  1291. }
  1292. function barrettConvert(x) {
  1293. if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m);
  1294. else if(x.compareTo(this.m) < 0) return x;
  1295. else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; }
  1296. }
  1297. function barrettRevert(x) { return x; }
  1298. // x = x mod m (HAC 14.42)
  1299. function barrettReduce(x) {
  1300. x.drShiftTo(this.m.t-1,this.r2);
  1301. if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); }
  1302. this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3);
  1303. this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);
  1304. while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1);
  1305. x.subTo(this.r2,x);
  1306. while(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
  1307. }
  1308. // r = x^2 mod m; x != r
  1309. function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
  1310. // r = x*y mod m; x,y != r
  1311. function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
  1312. Barrett.prototype.convert = barrettConvert;
  1313. Barrett.prototype.revert = barrettRevert;
  1314. Barrett.prototype.reduce = barrettReduce;
  1315. Barrett.prototype.mulTo = barrettMulTo;
  1316. Barrett.prototype.sqrTo = barrettSqrTo;
  1317. // (public) this^e % m (HAC 14.85)
  1318. function bnModPow(e,m) {
  1319. var e_array = e.array;
  1320. var i = e.bitLength(), k, r = nbv(1), z;
  1321. if(i <= 0) return r;
  1322. else if(i < 18) k = 1;
  1323. else if(i < 48) k = 3;
  1324. else if(i < 144) k = 4;
  1325. else if(i < 768) k = 5;
  1326. else k = 6;
  1327. if(i < 8)
  1328. z = new Classic(m);
  1329. else if(m.isEven())
  1330. z = new Barrett(m);
  1331. else
  1332. z = new Montgomery(m);
  1333. // precomputation
  1334. var g = new Array(), n = 3, k1 = k-1, km = (1<<k)-1;
  1335. g[1] = z.convert(this);
  1336. if(k > 1) {
  1337. var g2 = nbi();
  1338. z.sqrTo(g[1],g2);
  1339. while(n <= km) {
  1340. g[n] = nbi();
  1341. z.mulTo(g2,g[n-2],g[n]);
  1342. n += 2;
  1343. }
  1344. }
  1345. var j = e.t-1, w, is1 = true, r2 = nbi(), t;
  1346. i = nbits(e_array[j])-1;
  1347. while(j >= 0) {
  1348. if(i >= k1) w = (e_array[j]>>(i-k1))&km;
  1349. else {
  1350. w = (e_array[j]&((1<<(i+1))-1))<<(k1-i);
  1351. if(j > 0) w |= e_array[j-1]>>(BI_DB+i-k1);
  1352. }
  1353. n = k;
  1354. while((w&1) == 0) { w >>= 1; --n; }
  1355. if((i -= n) < 0) { i += BI_DB; --j; }
  1356. if(is1) { // ret == 1, don't bother squaring or multiplying it
  1357. g[w].copyTo(r);
  1358. is1 = false;
  1359. }
  1360. else {
  1361. while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; }
  1362. if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; }
  1363. z.mulTo(r2,g[w],r);
  1364. }
  1365. while(j >= 0 && (e_array[j]&(1<<i)) == 0) {
  1366. z.sqrTo(r,r2); t = r; r = r2; r2 = t;
  1367. if(--i < 0) { i = BI_DB-1; --j; }
  1368. }
  1369. }
  1370. return z.revert(r);
  1371. }
  1372. // (public) gcd(this,a) (HAC 14.54)
  1373. function bnGCD(a) {
  1374. var x = (this.s<0)?this.negate():this.clone();
  1375. var y = (a.s<0)?a.negate():a.clone();
  1376. if(x.compareTo(y) < 0) { var t = x; x = y; y = t; }
  1377. var i = x.getLowestSetBit(), g = y.getLowestSetBit();
  1378. if(g < 0) return x;
  1379. if(i < g) g = i;
  1380. if(g > 0) {
  1381. x.rShiftTo(g,x);
  1382. y.rShiftTo(g,y);
  1383. }
  1384. while(x.signum() > 0) {
  1385. if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x);
  1386. if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y);
  1387. if(x.compareTo(y) >= 0) {
  1388. x.subTo(y,x);
  1389. x.rShiftTo(1,x);
  1390. }
  1391. else {
  1392. y.subTo(x,y);
  1393. y.rShiftTo(1,y);
  1394. }
  1395. }
  1396. if(g > 0) y.lShiftTo(g,y);
  1397. return y;
  1398. }
  1399. // (protected) this % n, n < 2^26
  1400. function bnpModInt(n) {
  1401. var this_array = this.array;
  1402. if(n <= 0) return 0;
  1403. var d = BI_DV%n, r = (this.s<0)?n-1:0;
  1404. if(this.t > 0)
  1405. if(d == 0) r = this_array[0]%n;
  1406. else for(var i = this.t-1; i >= 0; --i) r = (d*r+this_array[i])%n;
  1407. return r;
  1408. }
  1409. // (public) 1/this % m (HAC 14.61)
  1410. function bnModInverse(m) {
  1411. var ac = m.isEven();
  1412. if((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO;
  1413. var u = m.clone(), v = this.clone();
  1414. var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1);
  1415. while(u.signum() != 0) {
  1416. while(u.isEven()) {
  1417. u.rShiftTo(1,u);
  1418. if(ac) {
  1419. if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); }
  1420. a.rShiftTo(1,a);
  1421. }
  1422. else if(!b.isEven()) b.subTo(m,b);
  1423. b.rShiftTo(1,b);
  1424. }
  1425. while(v.isEven()) {
  1426. v.rShiftTo(1,v);
  1427. if(ac) {
  1428. if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); }
  1429. c.rShiftTo(1,c);
  1430. }
  1431. else if(!d.isEven()) d.subTo(m,d);
  1432. d.rShiftTo(1,d);
  1433. }
  1434. if(u.compareTo(v) >= 0) {
  1435. u.subTo(v,u);
  1436. if(ac) a.subTo(c,a);
  1437. b.subTo(d,b);
  1438. }
  1439. else {
  1440. v.subTo(u,v);
  1441. if(ac) c.subTo(a,c);
  1442. d.subTo(b,d);
  1443. }
  1444. }
  1445. if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
  1446. if(d.compareTo(m) >= 0) return d.subtract(m);
  1447. if(d.signum() < 0) d.addTo(m,d); else return d;
  1448. if(d.signum() < 0) return d.add(m); else return d;
  1449. }
  1450. 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];
  1451. var lplim = (1<<26)/lowprimes[lowprimes.length-1];
  1452. // (public) test primality with certainty >= 1-.5^t
  1453. function bnIsProbablePrime(t) {
  1454. var i, x = this.abs();
  1455. var x_array = x.array;
  1456. if(x.t == 1 && x_array[0] <= lowprimes[lowprimes.length-1]) {
  1457. for(i = 0; i < lowprimes.length; ++i)
  1458. if(x_array[0] == lowprimes[i]) return true;
  1459. return false;
  1460. }
  1461. if(x.isEven()) return false;
  1462. i = 1;
  1463. while(i < lowprimes.length) {
  1464. var m = lowprimes[i], j = i+1;
  1465. while(j < lowprimes.length && m < lplim) m *= lowprimes[j++];
  1466. m = x.modInt(m);
  1467. while(i < j) if(m%lowprimes[i++] == 0) return false;
  1468. }
  1469. return x.millerRabin(t);
  1470. }
  1471. // (protected) true if probably prime (HAC 4.24, Miller-Rabin)
  1472. function bnpMillerRabin(t) {
  1473. var n1 = this.subtract(BigInteger.ONE);
  1474. var k = n1.getLowestSetBit();
  1475. if(k <= 0) return false;
  1476. var r = n1.shiftRight(k);
  1477. t = (t+1)>>1;
  1478. if(t > lowprimes.length) t = lowprimes.length;
  1479. var a = nbi();
  1480. for(var i = 0; i < t; ++i) {
  1481. a.fromInt(lowprimes[i]);
  1482. var y = a.modPow(r,this);
  1483. if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
  1484. var j = 1;
  1485. while(j++ < k && y.compareTo(n1) != 0) {
  1486. y = y.modPowInt(2,this);
  1487. if(y.compareTo(BigInteger.ONE) == 0) return false;
  1488. }
  1489. if(y.compareTo(n1) != 0) return false;
  1490. }
  1491. }
  1492. return true;
  1493. }
  1494. // protected
  1495. BigInteger.prototype.chunkSize = bnpChunkSize;
  1496. BigInteger.prototype.toRadix = bnpToRadix;
  1497. BigInteger.prototype.fromRadix = bnpFromRadix;
  1498. BigInteger.prototype.fromNumber = bnpFromNumber;
  1499. BigInteger.prototype.bitwiseTo = bnpBitwiseTo;
  1500. BigInteger.prototype.changeBit = bnpChangeBit;
  1501. BigInteger.prototype.addTo = bnpAddTo;
  1502. BigInteger.prototype.dMultiply = bnpDMultiply;
  1503. BigInteger.prototype.dAddOffset = bnpDAddOffset;
  1504. BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo;
  1505. BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
  1506. BigInteger.prototype.modInt = bnpModInt;
  1507. BigInteger.prototype.millerRabin = bnpMillerRabin;
  1508. // public
  1509. BigInteger.prototype.clone = bnClone;
  1510. BigInteger.prototype.intValue = bnIntValue;
  1511. BigInteger.prototype.byteValue = bnByteValue;
  1512. BigInteger.prototype.shortValue = bnShortValue;
  1513. BigInteger.prototype.signum = bnSigNum;
  1514. BigInteger.prototype.toByteArray = bnToByteArray;
  1515. BigInteger.prototype.equals = bnEquals;
  1516. BigInteger.prototype.min = bnMin;
  1517. BigInteger.prototype.max = bnMax;
  1518. BigInteger.prototype.and = bnAnd;
  1519. BigInteger.prototype.or = bnOr;
  1520. BigInteger.prototype.xor = bnXor;
  1521. BigInteger.prototype.andNot = bnAndNot;
  1522. BigInteger.prototype.not = bnNot;
  1523. BigInteger.prototype.shiftLeft = bnShiftLeft;
  1524. BigInteger.prototype.shiftRight = bnShiftRight;
  1525. BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit;
  1526. BigInteger.prototype.bitCount = bnBitCount;
  1527. BigInteger.prototype.testBit = bnTestBit;
  1528. BigInteger.prototype.setBit = bnSetBit;
  1529. BigInteger.prototype.clearBit = bnClearBit;
  1530. BigInteger.prototype.flipBit = bnFlipBit;
  1531. BigInteger.prototype.add = bnAdd;
  1532. BigInteger.prototype.subtract = bnSubtract;
  1533. BigInteger.prototype.multiply = bnMultiply;
  1534. BigInteger.prototype.divide = bnDivide;
  1535. BigInteger.prototype.remainder = bnRemainder;
  1536. BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder;
  1537. BigInteger.prototype.modPow = bnModPow;
  1538. BigInteger.prototype.modInverse = bnModInverse;
  1539. BigInteger.prototype.pow = bnPow;
  1540. BigInteger.prototype.gcd = bnGCD;
  1541. BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
  1542. // BigInteger interfaces not implemented in jsbn:
  1543. // BigInteger(int signum, byte[] magnitude)
  1544. // double doubleValue()
  1545. // float floatValue()
  1546. // int hashCode()
  1547. // long longValue()
  1548. // static BigInteger valueOf(long val)
  1549. // prng4.js - uses Arcfour as a PRNG
  1550. function Arcfour() {
  1551. this.i = 0;
  1552. this.j = 0;
  1553. this.S = new Array();
  1554. }
  1555. // Initialize arcfour context from key, an array of ints, each from [0..255]
  1556. function ARC4init(key) {
  1557. var i, j, t;
  1558. for(i = 0; i < 256; ++i)
  1559. this.S[i] = i;
  1560. j = 0;
  1561. for(i = 0; i < 256; ++i) {
  1562. j = (j + this.S[i] + key[i % key.length]) & 255;
  1563. t = this.S[i];
  1564. this.S[i] = this.S[j];
  1565. this.S[j] = t;
  1566. }
  1567. this.i = 0;
  1568. this.j = 0;
  1569. }
  1570. function ARC4next() {
  1571. var t;
  1572. this.i = (this.i + 1) & 255;
  1573. this.j = (this.j + this.S[this.i]) & 255;
  1574. t = this.S[this.i];
  1575. this.S[this.i] = this.S[this.j];
  1576. this.S[this.j] = t;
  1577. return this.S[(t + this.S[this.i]) & 255];
  1578. }
  1579. Arcfour.prototype.init = ARC4init;
  1580. Arcfour.prototype.next = ARC4next;
  1581. // Plug in your RNG constructor here
  1582. function prng_newstate() {
  1583. return new Arcfour();
  1584. }
  1585. // Pool size must be a multiple of 4 and greater than 32.
  1586. // An array of bytes the size of the pool will be passed to init()
  1587. var rng_psize = 256;
  1588. // Random number generator - requires a PRNG backend, e.g. prng4.js
  1589. // For best results, put code like
  1590. // <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'>
  1591. // in your main HTML document.
  1592. var rng_state;
  1593. var rng_pool;
  1594. var rng_pptr;
  1595. // Mix in a 32-bit integer into the pool
  1596. function rng_seed_int(x) {
  1597. rng_pool[rng_pptr++] ^= x & 255;
  1598. rng_pool[rng_pptr++] ^= (x >> 8) & 255;
  1599. rng_pool[rng_pptr++] ^= (x >> 16) & 255;
  1600. rng_pool[rng_pptr++] ^= (x >> 24) & 255;
  1601. if(rng_pptr >= rng_psize) rng_pptr -= rng_psize;
  1602. }
  1603. // Mix in the current time (w/milliseconds) into the pool
  1604. function rng_seed_time() {
  1605. // Use pre-computed date to avoid making the benchmark
  1606. // results dependent on the current date.
  1607. rng_seed_int(1122926989487);
  1608. }
  1609. // Initialize the pool with junk if needed.
  1610. if(rng_pool == null) {
  1611. rng_pool = new Array();
  1612. rng_pptr = 0;
  1613. var t;
  1614. while(rng_pptr < rng_psize) { // extract some randomness from Math.random()
  1615. t = Math.floor(65536 * Math.random());
  1616. rng_pool[rng_pptr++] = t >>> 8;
  1617. rng_pool[rng_pptr++] = t & 255;
  1618. }
  1619. rng_pptr = 0;
  1620. rng_seed_time();
  1621. //rng_seed_int(window.screenX);
  1622. //rng_seed_int(window.screenY);
  1623. }
  1624. function rng_get_byte() {
  1625. if(rng_state == null) {
  1626. rng_seed_time();
  1627. rng_state = prng_newstate();
  1628. rng_state.init(rng_pool);
  1629. for(rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr)
  1630. rng_pool[rng_pptr] = 0;
  1631. rng_pptr = 0;
  1632. //rng_pool = null;
  1633. }
  1634. // TODO: allow reseeding after first request
  1635. return rng_state.next();
  1636. }
  1637. function rng_get_bytes(ba) {
  1638. var i;
  1639. for(i = 0; i < ba.length; ++i) ba[i] = rng_get_byte();
  1640. }
  1641. function SecureRandom() {}
  1642. SecureRandom.prototype.nextBytes = rng_get_bytes;
  1643. // Depends on jsbn.js and rng.js
  1644. // convert a (hex) string to a bignum object
  1645. function parseBigInt(str,r) {
  1646. return new BigInteger(str,r);
  1647. }
  1648. function linebrk(s,n) {
  1649. var ret = "";
  1650. var i = 0;
  1651. while(i + n < s.length) {
  1652. ret += s.substring(i,i+n) + "\n";
  1653. i += n;
  1654. }
  1655. return ret + s.substring(i,s.length);
  1656. }
  1657. function byte2Hex(b) {
  1658. if(b < 0x10)
  1659. return "0" + b.toString(16);
  1660. else
  1661. return b.toString(16);
  1662. }
  1663. // PKCS#1 (type 2, random) pad input string s to n bytes, and return a bigint
  1664. function pkcs1pad2(s,n) {
  1665. if(n < s.length + 11) {
  1666. alert("Message too long for RSA");
  1667. return null;
  1668. }
  1669. var ba = new Array();
  1670. var i = s.length - 1;
  1671. while(i >= 0 && n > 0) ba[--n] = s.charCodeAt(i--);
  1672. ba[--n] = 0;
  1673. var rng = new SecureRandom();
  1674. var x = new Array();
  1675. while(n > 2) { // random non-zero pad
  1676. x[0] = 0;
  1677. while(x[0] == 0) rng.nextBytes(x);
  1678. ba[--n] = x[0];
  1679. }
  1680. ba[--n] = 2;
  1681. ba[--n] = 0;
  1682. return new BigInteger(ba);
  1683. }
  1684. // "empty" RSA key constructor
  1685. function RSAKey() {
  1686. this.n = null;
  1687. this.e = 0;
  1688. this.d = null;
  1689. this.p = null;
  1690. this.q = null;
  1691. this.dmp1 = null;
  1692. this.dmq1 = null;
  1693. this.coeff = null;
  1694. }
  1695. // Set the public key fields N and e from hex strings
  1696. function RSASetPublic(N,E) {
  1697. if(N != null && E != null && N.length > 0 && E.length > 0) {
  1698. this.n = parseBigInt(N,16);
  1699. this.e = parseInt(E,16);
  1700. }
  1701. else
  1702. alert("Invalid RSA public key");
  1703. }
  1704. // Perform raw public operation on "x": return x^e (mod n)
  1705. function RSADoPublic(x) {
  1706. return x.modPowInt(this.e, this.n);
  1707. }
  1708. // Return the PKCS#1 RSA encryption of "text" as an even-length hex string
  1709. function RSAEncrypt(text) {
  1710. var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3);
  1711. if(m == null) return null;
  1712. var c = this.doPublic(m);
  1713. if(c == null) return null;
  1714. var h = c.toString(16);
  1715. if((h.length & 1) == 0) return h; else return "0" + h;
  1716. }
  1717. // Return the PKCS#1 RSA encryption of "text" as a Base64-encoded string
  1718. //function RSAEncryptB64(text) {
  1719. // var h = this.encrypt(text);
  1720. // if(h) return hex2b64(h); else return null;
  1721. //}
  1722. // protected
  1723. RSAKey.prototype.doPublic = RSADoPublic;
  1724. // public
  1725. RSAKey.prototype.setPublic = RSASetPublic;
  1726. RSAKey.prototype.encrypt = RSAEncrypt;
  1727. //RSAKey.prototype.encrypt_b64 = RSAEncryptB64;
  1728. // Depends on rsa.js and jsbn2.js
  1729. // Undo PKCS#1 (type 2, random) padding and, if valid, return the plaintext
  1730. function pkcs1unpad2(d,n) {
  1731. var b = d.toByteArray();
  1732. var i = 0;
  1733. while(i < b.length && b[i] == 0) ++i;
  1734. if(b.length-i != n-1 || b[i] != 2)
  1735. return null;
  1736. ++i;
  1737. while(b[i] != 0)
  1738. if(++i >= b.length) return null;
  1739. var ret = "";
  1740. while(++i < b.length)
  1741. ret += String.fromCharCode(b[i]);
  1742. return ret;
  1743. }
  1744. // Set the private key fields N, e, and d from hex strings
  1745. function RSASetPrivate(N,E,D) {
  1746. if(N != null && E != null && N.length > 0 && E.length > 0) {
  1747. this.n = parseBigInt(N,16);
  1748. this.e = parseInt(E,16);
  1749. this.d = parseBigInt(D,16);
  1750. }
  1751. else
  1752. alert("Invalid RSA private key");
  1753. }
  1754. // Set the private key fields N, e, d and CRT params from hex strings
  1755. function RSASetPrivateEx(N,E,D,P,Q,DP,DQ,C) {
  1756. if(N != null && E != null && N.length > 0 && E.length > 0) {
  1757. this.n = parseBigInt(N,16);
  1758. this.e = parseInt(E,16);
  1759. this.d = parseBigInt(D,16);
  1760. this.p = parseBigInt(P,16);
  1761. this.q = parseBigInt(Q,16);
  1762. this.dmp1 = parseBigInt(DP,16);
  1763. this.dmq1 = parseBigInt(DQ,16);
  1764. this.coeff = parseBigInt(C,16);
  1765. }
  1766. else
  1767. alert("Invalid RSA private key");
  1768. }
  1769. // Generate a new random private key B bits long, using public expt E
  1770. function RSAGenerate(B,E) {
  1771. var rng = new SecureRandom();
  1772. var qs = B>>1;
  1773. this.e = parseInt(E,16);
  1774. var ee = new BigInteger(E,16);
  1775. for(;;) {
  1776. for(;;) {
  1777. this.p = new BigInteger(B-qs,1,rng);
  1778. if(this.p.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.p.isProbablePrime(10)) break;
  1779. }
  1780. for(;;) {
  1781. this.q = new BigInteger(qs,1,rng);
  1782. if(this.q.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.q.isProbablePrime(10)) break;
  1783. }
  1784. if(this.p.compareTo(this.q) <= 0) {
  1785. var t = this.p;
  1786. this.p = this.q;
  1787. this.q = t;
  1788. }
  1789. var p1 = this.p.subtract(BigInteger.ONE);
  1790. var q1 = this.q.subtract(BigInteger.ONE);
  1791. var phi = p1.multiply(q1);
  1792. if(phi.gcd(ee).compareTo(BigInteger.ONE) == 0) {
  1793. this.n = this.p.multiply(this.q);
  1794. this.d = ee.modInverse(phi);
  1795. this.dmp1 = this.d.mod(p1);
  1796. this.dmq1 = this.d.mod(q1);
  1797. this.coeff = this.q.modInverse(this.p);
  1798. break;
  1799. }
  1800. }
  1801. }
  1802. // Perform raw private operation on "x": return x^d (mod n)
  1803. function RSADoPrivate(x) {
  1804. if(this.p == null || this.q == null)
  1805. return x.modPow(this.d, this.n);
  1806. // TODO: re-calculate any missing CRT params
  1807. var xp = x.mod(this.p).modPow(this.dmp1, this.p);
  1808. var xq = x.mod(this.q).modPow(this.dmq1, this.q);
  1809. while(xp.compareTo(xq) < 0)
  1810. xp = xp.add(this.p);
  1811. return xp.subtract(xq).multiply(this.coeff).mod(this.p).multiply(this.q).add(xq);
  1812. }
  1813. // Return the PKCS#1 RSA decryption of "ctext".
  1814. // "ctext" is an even-length hex string and the output is a plain string.
  1815. function RSADecrypt(ctext) {
  1816. var c = parseBigInt(ctext, 16);
  1817. var m = this.doPrivate(c);
  1818. if(m == null) return null;
  1819. return pkcs1unpad2(m, (this.n.bitLength()+7)>>3);
  1820. }
  1821. // Return the PKCS#1 RSA decryption of "ctext".
  1822. // "ctext" is a Base64-encoded string and the output is a plain string.
  1823. //function RSAB64Decrypt(ctext) {
  1824. // var h = b64tohex(ctext);
  1825. // if(h) return this.decrypt(h); else return null;
  1826. //}
  1827. // protected
  1828. RSAKey.prototype.doPrivate = RSADoPrivate;
  1829. // public
  1830. RSAKey.prototype.setPrivate = RSASetPrivate;
  1831. RSAKey.prototype.setPrivateEx = RSASetPrivateEx;
  1832. RSAKey.prototype.generate = RSAGenerate;
  1833. RSAKey.prototype.decrypt = RSADecrypt;
  1834. //RSAKey.prototype.b64_decrypt = RSAB64Decrypt;
  1835. nValue="a5261939975948bb7a58dffe5ff54e65f0498f9175f5a09288810b8975871e99af3b5dd94057b0fc07535f5f97444504fa35169d461d0d30cf0192e307727c065168c788771c561a9400fb49175e9e6aa4e23fe11af69e9412dd23b0cb6684c4c2429bce139e848ab26d0829073351f4acd36074eafd036a5eb83359d2a698d3";
  1836. eValue="10001";
  1837. dValue="8e9912f6d3645894e8d38cb58c0db81ff516cf4c7e5a14c7f1eddb1459d2cded4d8d293fc97aee6aefb861859c8b6a3d1dfe710463e1f9ddc72048c09751971c4a580aa51eb523357a3cc48d31cfad1d4a165066ed92d4748fb6571211da5cb14bc11b6e2df7c1a559e6d5ac1cd5c94703a22891464fba23d0d965086277a161";
  1838. pValue="d090ce58a92c75233a6486cb0a9209bf3583b64f540c76f5294bb97d285eed33aec220bde14b2417951178ac152ceab6da7090905b478195498b352048f15e7d";
  1839. qValue="cab575dc652bb66df15a0359609d51d1db184750c00c6698b90ef3465c99655103edbf0d54c56aec0ce3c4d22592338092a126a0cc49f65a4a30d222b411e58f";
  1840. dmp1Value="1a24bca8e273df2f0e47c199bbf678604e7df7215480c77c8db39f49b000ce2cf7500038acfff5433b7d582a01f1826e6f4d42e1c57f5e1fef7b12aabc59fd25";
  1841. dmq1Value="3d06982efbbe47339e1f6d36b1216b8a741d410b0c662f54f7118b27b9a4ec9d914337eb39841d8666f3034408cf94f5b62f11c402fc994fe15a05493150d9fd";
  1842. coeffValue="3a3e731acd8960b7ff9eb81a7ff93bd1cfa74cbd56987db58b4594fb09c09084db1734c8143f98b602b981aaa9243ca28deb69b5b280ee8dcee0fd2625e53250";
  1843. setupEngine(am3, 28);
  1844. var TEXT = "The quick brown fox jumped over the extremely lazy frog! " +
  1845. "Now is the time for all good men to come to the party.";
  1846. var encrypted;
  1847. function encrypt() {
  1848. var RSA = new RSAKey();
  1849. RSA.setPublic(nValue, eValue);
  1850. RSA.setPrivateEx(nValue, eValue, dValue, pValue, qValue, dmp1Value, dmq1Value, coeffValue);
  1851. encrypted = RSA.encrypt(TEXT);
  1852. }
  1853. function decrypt() {
  1854. var RSA = new RSAKey();
  1855. RSA.setPublic(nValue, eValue);
  1856. RSA.setPrivateEx(nValue, eValue, dValue, pValue, qValue, dmp1Value, dmq1Value, coeffValue);
  1857. var decrypted = RSA.decrypt(encrypted);
  1858. if (decrypted != TEXT) {
  1859. throw new Error("Crypto operation failed");
  1860. }
  1861. }
  1862. ////////////////////////////////////////////////////////////////////////////////
  1863. // Runner
  1864. ////////////////////////////////////////////////////////////////////////////////
  1865. var success = true;
  1866. function NotifyStart(name) {
  1867. }
  1868. function NotifyError(name, error) {
  1869. WScript.Echo(name + " : ERROR : " + error.stack);
  1870. success = false;
  1871. }
  1872. function NotifyResult(name, score) {
  1873. if (success) {
  1874. WScript.Echo("### SCORE:", score);
  1875. }
  1876. }
  1877. function NotifyScore(score) {
  1878. }
  1879. BenchmarkSuite.RunSuites({
  1880. NotifyStart: NotifyStart,
  1881. NotifyError: NotifyError,
  1882. NotifyResult: NotifyResult,
  1883. NotifyScore: NotifyScore
  1884. });