js/src/devtools/jint/v8/crypto.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/devtools/jint/v8/crypto.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,2135 @@
     1.4 +// Copyright 2008 the V8 project authors. All rights reserved.
     1.5 +// Redistribution and use in source and binary forms, with or without
     1.6 +// modification, are permitted provided that the following conditions are
     1.7 +// met:
     1.8 +//
     1.9 +//     * Redistributions of source code must retain the above copyright
    1.10 +//       notice, this list of conditions and the following disclaimer.
    1.11 +//     * Redistributions in binary form must reproduce the above
    1.12 +//       copyright notice, this list of conditions and the following
    1.13 +//       disclaimer in the documentation and/or other materials provided
    1.14 +//       with the distribution.
    1.15 +//     * Neither the name of Google Inc. nor the names of its
    1.16 +//       contributors may be used to endorse or promote products derived
    1.17 +//       from this software without specific prior written permission.
    1.18 +//
    1.19 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.20 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.21 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.22 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.23 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.24 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.25 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.26 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.27 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.28 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.29 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.30 +
    1.31 +
    1.32 +// Simple framework for running the benchmark suites and
    1.33 +// computing a score based on the timing measurements.
    1.34 +
    1.35 +
    1.36 +// A benchmark has a name (string) and a function that will be run to
    1.37 +// do the performance measurement. The optional setup and tearDown
    1.38 +// arguments are functions that will be invoked before and after
    1.39 +// running the benchmark, but the running time of these functions will
    1.40 +// not be accounted for in the benchmark score.
    1.41 +function Benchmark(name, run, setup, tearDown) {
    1.42 +  this.name = name;
    1.43 +  this.run = run;
    1.44 +  this.Setup = setup ? setup : function() { };
    1.45 +  this.TearDown = tearDown ? tearDown : function() { };
    1.46 +}
    1.47 +
    1.48 +
    1.49 +// Benchmark results hold the benchmark and the measured time used to
    1.50 +// run the benchmark. The benchmark score is computed later once a
    1.51 +// full benchmark suite has run to completion.
    1.52 +function BenchmarkResult(benchmark, time) {
    1.53 +  this.benchmark = benchmark;
    1.54 +  this.time = time;
    1.55 +}
    1.56 +
    1.57 +
    1.58 +// Automatically convert results to numbers. Used by the geometric
    1.59 +// mean computation.
    1.60 +BenchmarkResult.prototype.valueOf = function() {
    1.61 +  return this.time;
    1.62 +}
    1.63 +
    1.64 +
    1.65 +// Suites of benchmarks consist of a name and the set of benchmarks in
    1.66 +// addition to the reference timing that the final score will be based
    1.67 +// on. This way, all scores are relative to a reference run and higher
    1.68 +// scores implies better performance.
    1.69 +function BenchmarkSuite(name, reference, benchmarks) {
    1.70 +  this.name = name;
    1.71 +  this.reference = reference;
    1.72 +  this.benchmarks = benchmarks;
    1.73 +  BenchmarkSuite.suites.push(this);
    1.74 +}
    1.75 +
    1.76 +
    1.77 +// Keep track of all declared benchmark suites.
    1.78 +BenchmarkSuite.suites = [];
    1.79 +
    1.80 +
    1.81 +// Scores are not comparable across versions. Bump the version if
    1.82 +// you're making changes that will affect that scores, e.g. if you add
    1.83 +// a new benchmark or change an existing one.
    1.84 +BenchmarkSuite.version = '5';
    1.85 +
    1.86 +
    1.87 +// To make the benchmark results predictable, we replace Math.random
    1.88 +// with a 100% deterministic alternative.
    1.89 +Math.random = (function() {
    1.90 +  var seed = 49734321;
    1.91 +  return function() {
    1.92 +    // Robert Jenkins' 32 bit integer hash function.
    1.93 +    seed = ((seed + 0x7ed55d16) + (seed << 12))  & 0xffffffff;
    1.94 +    seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff;
    1.95 +    seed = ((seed + 0x165667b1) + (seed << 5))   & 0xffffffff;
    1.96 +    seed = ((seed + 0xd3a2646c) ^ (seed << 9))   & 0xffffffff;
    1.97 +    seed = ((seed + 0xfd7046c5) + (seed << 3))   & 0xffffffff;
    1.98 +    seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff;
    1.99 +    return (seed & 0xfffffff) / 0x10000000;
   1.100 +  };
   1.101 +})();
   1.102 +
   1.103 +
   1.104 +// Runs all registered benchmark suites and optionally yields between
   1.105 +// each individual benchmark to avoid running for too long in the
   1.106 +// context of browsers. Once done, the final score is reported to the
   1.107 +// runner.
   1.108 +BenchmarkSuite.RunSuites = function(runner) {
   1.109 +  var continuation = null;
   1.110 +  var suites = BenchmarkSuite.suites;
   1.111 +  var length = suites.length;
   1.112 +  BenchmarkSuite.scores = [];
   1.113 +  var index = 0;
   1.114 +  function RunStep() {
   1.115 +    while (continuation || index < length) {
   1.116 +      if (continuation) {
   1.117 +        continuation = continuation();
   1.118 +      } else {
   1.119 +        var suite = suites[index++];
   1.120 +        if (runner.NotifyStart) runner.NotifyStart(suite.name);
   1.121 +        continuation = suite.RunStep(runner);
   1.122 +      }
   1.123 +      if (continuation && typeof window != 'undefined' && window.setTimeout) {
   1.124 +        window.setTimeout(RunStep, 25);
   1.125 +        return;
   1.126 +      }
   1.127 +    }
   1.128 +    if (runner.NotifyScore) {
   1.129 +      var score = BenchmarkSuite.GeometricMean(BenchmarkSuite.scores);
   1.130 +      var formatted = BenchmarkSuite.FormatScore(100 * score);
   1.131 +      runner.NotifyScore(formatted);
   1.132 +    }
   1.133 +  }
   1.134 +  RunStep();
   1.135 +}
   1.136 +
   1.137 +
   1.138 +// Counts the total number of registered benchmarks. Useful for
   1.139 +// showing progress as a percentage.
   1.140 +BenchmarkSuite.CountBenchmarks = function() {
   1.141 +  var result = 0;
   1.142 +  var suites = BenchmarkSuite.suites;
   1.143 +  for (var i = 0; i < suites.length; i++) {
   1.144 +    result += suites[i].benchmarks.length;
   1.145 +  }
   1.146 +  return result;
   1.147 +}
   1.148 +
   1.149 +
   1.150 +// Computes the geometric mean of a set of numbers.
   1.151 +BenchmarkSuite.GeometricMean = function(numbers) {
   1.152 +  var log = 0;
   1.153 +  for (var i = 0; i < numbers.length; i++) {
   1.154 +    log += Math.log(numbers[i]);
   1.155 +  }
   1.156 +  return Math.pow(Math.E, log / numbers.length);
   1.157 +}
   1.158 +
   1.159 +
   1.160 +// Converts a score value to a string with at least three significant
   1.161 +// digits.
   1.162 +BenchmarkSuite.FormatScore = function(value) {
   1.163 +  if (value > 100) {
   1.164 +    return value.toFixed(0);
   1.165 +  } else {
   1.166 +    return value.toPrecision(3);
   1.167 +  }
   1.168 +}
   1.169 +
   1.170 +// Notifies the runner that we're done running a single benchmark in
   1.171 +// the benchmark suite. This can be useful to report progress.
   1.172 +BenchmarkSuite.prototype.NotifyStep = function(result) {
   1.173 +  this.results.push(result);
   1.174 +  if (this.runner.NotifyStep) this.runner.NotifyStep(result.benchmark.name);
   1.175 +}
   1.176 +
   1.177 +
   1.178 +// Notifies the runner that we're done with running a suite and that
   1.179 +// we have a result which can be reported to the user if needed.
   1.180 +BenchmarkSuite.prototype.NotifyResult = function() {
   1.181 +  var mean = BenchmarkSuite.GeometricMean(this.results);
   1.182 +  var score = this.reference / mean;
   1.183 +  BenchmarkSuite.scores.push(score);
   1.184 +  if (this.runner.NotifyResult) {
   1.185 +    var formatted = BenchmarkSuite.FormatScore(100 * score);
   1.186 +    this.runner.NotifyResult(this.name, formatted);
   1.187 +  }
   1.188 +}
   1.189 +
   1.190 +
   1.191 +// Notifies the runner that running a benchmark resulted in an error.
   1.192 +BenchmarkSuite.prototype.NotifyError = function(error) {
   1.193 +  if (this.runner.NotifyError) {
   1.194 +    this.runner.NotifyError(this.name, error);
   1.195 +  }
   1.196 +  if (this.runner.NotifyStep) {
   1.197 +    this.runner.NotifyStep(this.name);
   1.198 +  }
   1.199 +}
   1.200 +
   1.201 +
   1.202 +// Runs a single benchmark for at least a second and computes the
   1.203 +// average time it takes to run a single iteration.
   1.204 +BenchmarkSuite.prototype.RunSingleBenchmark = function(benchmark) {
   1.205 +  var elapsed = 0;
   1.206 +  var start = new Date();
   1.207 +  for (var n = 0; elapsed < 20; n++) {
   1.208 +    benchmark.run();
   1.209 +    elapsed = new Date() - start;
   1.210 +  }
   1.211 +  var usec = (elapsed * 1000) / n;
   1.212 +  this.NotifyStep(new BenchmarkResult(benchmark, usec));
   1.213 +}
   1.214 +
   1.215 +
   1.216 +// This function starts running a suite, but stops between each
   1.217 +// individual benchmark in the suite and returns a continuation
   1.218 +// function which can be invoked to run the next benchmark. Once the
   1.219 +// last benchmark has been executed, null is returned.
   1.220 +BenchmarkSuite.prototype.RunStep = function(runner) {
   1.221 +  this.results = [];
   1.222 +  this.runner = runner;
   1.223 +  var length = this.benchmarks.length;
   1.224 +  var index = 0;
   1.225 +  var suite = this;
   1.226 +
   1.227 +  // Run the setup, the actual benchmark, and the tear down in three
   1.228 +  // separate steps to allow the framework to yield between any of the
   1.229 +  // steps.
   1.230 +
   1.231 +  function RunNextSetup() {
   1.232 +    if (index < length) {
   1.233 +      try {
   1.234 +        suite.benchmarks[index].Setup();
   1.235 +      } catch (e) {
   1.236 +        suite.NotifyError(e);
   1.237 +        return null;
   1.238 +      }
   1.239 +      return RunNextBenchmark;
   1.240 +    }
   1.241 +    suite.NotifyResult();
   1.242 +    return null;
   1.243 +  }
   1.244 +
   1.245 +  function RunNextBenchmark() {
   1.246 +    try {
   1.247 +      suite.RunSingleBenchmark(suite.benchmarks[index]);
   1.248 +    } catch (e) {
   1.249 +      suite.NotifyError(e);
   1.250 +      return null;
   1.251 +    }
   1.252 +    return RunNextTearDown;
   1.253 +  }
   1.254 +
   1.255 +  function RunNextTearDown() {
   1.256 +    try {
   1.257 +      suite.benchmarks[index++].TearDown();
   1.258 +    } catch (e) {
   1.259 +      suite.NotifyError(e);
   1.260 +      return null;
   1.261 +    }
   1.262 +    return RunNextSetup;
   1.263 +  }
   1.264 +
   1.265 +  // Start out running the setup.
   1.266 +  return RunNextSetup();
   1.267 +}
   1.268 +
   1.269 +/*
   1.270 + * Copyright (c) 2003-2005  Tom Wu
   1.271 + * All Rights Reserved.
   1.272 + *
   1.273 + * Permission is hereby granted, free of charge, to any person obtaining
   1.274 + * a copy of this software and associated documentation files (the
   1.275 + * "Software"), to deal in the Software without restriction, including
   1.276 + * without limitation the rights to use, copy, modify, merge, publish,
   1.277 + * distribute, sublicense, and/or sell copies of the Software, and to
   1.278 + * permit persons to whom the Software is furnished to do so, subject to
   1.279 + * the following conditions:
   1.280 + *
   1.281 + * The above copyright notice and this permission notice shall be
   1.282 + * included in all copies or substantial portions of the Software.
   1.283 + *
   1.284 + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
   1.285 + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
   1.286 + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   1.287 + *
   1.288 + * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
   1.289 + * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
   1.290 + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
   1.291 + * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
   1.292 + * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   1.293 + *
   1.294 + * In addition, the following condition applies:
   1.295 + *
   1.296 + * All redistributions must retain an intact copy of this copyright notice
   1.297 + * and disclaimer.
   1.298 + */
   1.299 +
   1.300 +
   1.301 +// The code has been adapted for use as a benchmark by Google.
   1.302 +var Crypto = new BenchmarkSuite('Crypto', 203037, [
   1.303 +  new Benchmark("Encrypt", encrypt),
   1.304 +  new Benchmark("Decrypt", decrypt)
   1.305 +]);
   1.306 +
   1.307 +
   1.308 +// Basic JavaScript BN library - subset useful for RSA encryption.
   1.309 +
   1.310 +// Bits per digit
   1.311 +var dbits;
   1.312 +var BI_DB;
   1.313 +var BI_DM;
   1.314 +var BI_DV;
   1.315 +
   1.316 +var BI_FP;
   1.317 +var BI_FV;
   1.318 +var BI_F1;
   1.319 +var BI_F2;
   1.320 +
   1.321 +// JavaScript engine analysis
   1.322 +var canary = 0xdeadbeefcafe;
   1.323 +var j_lm = ((canary&0xffffff)==0xefcafe);
   1.324 +
   1.325 +// (public) Constructor
   1.326 +function BigInteger(a,b,c) {
   1.327 +  this.array = new Array();
   1.328 +  if(a != null)
   1.329 +    if("number" == typeof a) this.fromNumber(a,b,c);
   1.330 +    else if(b == null && "string" != typeof a) this.fromString(a,256);
   1.331 +    else this.fromString(a,b);
   1.332 +}
   1.333 +
   1.334 +// return new, unset BigInteger
   1.335 +function nbi() { return new BigInteger(null); }
   1.336 +
   1.337 +// am: Compute w_j += (x*this_i), propagate carries,
   1.338 +// c is initial carry, returns final carry.
   1.339 +// c < 3*dvalue, x < 2*dvalue, this_i < dvalue
   1.340 +// We need to select the fastest one that works in this environment.
   1.341 +
   1.342 +// am1: use a single mult and divide to get the high bits,
   1.343 +// max digit bits should be 26 because
   1.344 +// max internal value = 2*dvalue^2-2*dvalue (< 2^53)
   1.345 +function am1(i,x,w,j,c,n) {
   1.346 +  var this_array = this.array;
   1.347 +  var w_array    = w.array;
   1.348 +  /* BEGIN LOOP */
   1.349 +  while(--n >= 0) {
   1.350 +    var v = x*this_array[i++]+w_array[j]+c;
   1.351 +    c = Math.floor(v/0x4000000);
   1.352 +    w_array[j++] = v&0x3ffffff;
   1.353 +  }
   1.354 +  /* END LOOP */
   1.355 +  return c;
   1.356 +}
   1.357 +
   1.358 +// am2 avoids a big mult-and-extract completely.
   1.359 +// Max digit bits should be <= 30 because we do bitwise ops
   1.360 +// on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
   1.361 +function am2(i,x,w,j,c,n) {
   1.362 +  var this_array = this.array;
   1.363 +  var w_array    = w.array;
   1.364 +  var xl = x&0x7fff, xh = x>>15;
   1.365 +  /* BEGIN LOOP */
   1.366 +  while(--n >= 0) {
   1.367 +    var l = this_array[i]&0x7fff;
   1.368 +    var h = this_array[i++]>>15;
   1.369 +    var m = xh*l+h*xl;
   1.370 +    l = xl*l+((m&0x7fff)<<15)+w_array[j]+(c&0x3fffffff);
   1.371 +    c = (l>>>30)+(m>>>15)+xh*h+(c>>>30);
   1.372 +    w_array[j++] = l&0x3fffffff;
   1.373 +  }
   1.374 +  /* END LOOP */
   1.375 +  return c;
   1.376 +}
   1.377 +
   1.378 +// Alternately, set max digit bits to 28 since some
   1.379 +// browsers slow down when dealing with 32-bit numbers.
   1.380 +function am3(i,x,w,j,c,n) {
   1.381 +  var this_array = this.array;
   1.382 +  var w_array    = w.array;
   1.383 +
   1.384 +  var xl = x&0x3fff, xh = x>>14;
   1.385 +  /* BEGIN LOOP */
   1.386 +  while(--n >= 0) {
   1.387 +    var l = this_array[i]&0x3fff;
   1.388 +    var h = this_array[i++]>>14;
   1.389 +    var m = xh*l+h*xl;
   1.390 +    l = xl*l+((m&0x3fff)<<14)+w_array[j]+c;
   1.391 +    c = (l>>28)+(m>>14)+xh*h;
   1.392 +    w_array[j++] = l&0xfffffff;
   1.393 +  }
   1.394 +  /* END LOOP */
   1.395 +  return c;
   1.396 +}
   1.397 +
   1.398 +// This is tailored to VMs with 2-bit tagging. It makes sure
   1.399 +// that all the computations stay within the 29 bits available.
   1.400 +function am4(i,x,w,j,c,n) {
   1.401 +  var this_array = this.array;
   1.402 +  var w_array    = w.array;
   1.403 +
   1.404 +  var xl = x&0x1fff, xh = x>>13;
   1.405 +  /* BEGIN LOOP */
   1.406 +  while(--n >= 0) {
   1.407 +    var l = this_array[i]&0x1fff;
   1.408 +    var h = this_array[i++]>>13;
   1.409 +    var m = xh*l+h*xl;
   1.410 +    l = xl*l+((m&0x1fff)<<13)+w_array[j]+c;
   1.411 +    c = (l>>26)+(m>>13)+xh*h;
   1.412 +    w_array[j++] = l&0x3ffffff;
   1.413 +  }
   1.414 +  /* END LOOP */
   1.415 +  return c;
   1.416 +}
   1.417 +
   1.418 +// am3/28 is best for SM, Rhino, but am4/26 is best for v8.
   1.419 +// Kestrel (Opera 9.5) gets its best result with am4/26.
   1.420 +// IE7 does 9% better with am3/28 than with am4/26.
   1.421 +// Firefox (SM) gets 10% faster with am3/28 than with am4/26.
   1.422 +
   1.423 +setupEngine = function(fn, bits) {
   1.424 +  BigInteger.prototype.am = fn;
   1.425 +  dbits = bits;
   1.426 +
   1.427 +  BI_DB = dbits;
   1.428 +  BI_DM = ((1<<dbits)-1);
   1.429 +  BI_DV = (1<<dbits);
   1.430 +
   1.431 +  BI_FP = 52;
   1.432 +  BI_FV = Math.pow(2,BI_FP);
   1.433 +  BI_F1 = BI_FP-dbits;
   1.434 +  BI_F2 = 2*dbits-BI_FP;
   1.435 +}
   1.436 +
   1.437 +
   1.438 +// Digit conversions
   1.439 +var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
   1.440 +var BI_RC = new Array();
   1.441 +var rr,vv;
   1.442 +rr = "0".charCodeAt(0);
   1.443 +  /* BEGIN LOOP */
   1.444 +for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
   1.445 +  /* END LOOP */
   1.446 +rr = "a".charCodeAt(0);
   1.447 +  /* BEGIN LOOP */
   1.448 +for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
   1.449 +  /* END LOOP */
   1.450 +rr = "A".charCodeAt(0);
   1.451 +  /* BEGIN LOOP */
   1.452 +for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
   1.453 +  /* END LOOP */
   1.454 +
   1.455 +function int2char(n) { return BI_RM.charAt(n); }
   1.456 +function intAt(s,i) {
   1.457 +  var c = BI_RC[s.charCodeAt(i)];
   1.458 +  return (c==null)?-1:c;
   1.459 +}
   1.460 +
   1.461 +// (protected) copy this to r
   1.462 +function bnpCopyTo(r) {
   1.463 +  var this_array = this.array;
   1.464 +  var r_array    = r.array;
   1.465 +
   1.466 +  /* BEGIN LOOP */
   1.467 +  for(var i = this.t-1; i >= 0; --i) r_array[i] = this_array[i];
   1.468 +  /* END LOOP */
   1.469 +  r.t = this.t;
   1.470 +  r.s = this.s;
   1.471 +}
   1.472 +
   1.473 +// (protected) set from integer value x, -DV <= x < DV
   1.474 +function bnpFromInt(x) {
   1.475 +  var this_array = this.array;
   1.476 +  this.t = 1;
   1.477 +  this.s = (x<0)?-1:0;
   1.478 +  if(x > 0) this_array[0] = x;
   1.479 +  else if(x < -1) this_array[0] = x+DV;
   1.480 +  else this.t = 0;
   1.481 +}
   1.482 +
   1.483 +// return bigint initialized to value
   1.484 +function nbv(i) { var r = nbi(); r.fromInt(i); return r; }
   1.485 +
   1.486 +// (protected) set from string and radix
   1.487 +function bnpFromString(s,b) {
   1.488 +  var this_array = this.array;
   1.489 +  var k;
   1.490 +  if(b == 16) k = 4;
   1.491 +  else if(b == 8) k = 3;
   1.492 +  else if(b == 256) k = 8; // byte array
   1.493 +  else if(b == 2) k = 1;
   1.494 +  else if(b == 32) k = 5;
   1.495 +  else if(b == 4) k = 2;
   1.496 +  else { this.fromRadix(s,b); return; }
   1.497 +  this.t = 0;
   1.498 +  this.s = 0;
   1.499 +  var i = s.length, mi = false, sh = 0;
   1.500 +  /* BEGIN LOOP */
   1.501 +  while(--i >= 0) {
   1.502 +    var x = (k==8)?s[i]&0xff:intAt(s,i);
   1.503 +    if(x < 0) {
   1.504 +      if(s.charAt(i) == "-") mi = true;
   1.505 +      continue;
   1.506 +    }
   1.507 +    mi = false;
   1.508 +    if(sh == 0)
   1.509 +      this_array[this.t++] = x;
   1.510 +    else if(sh+k > BI_DB) {
   1.511 +      this_array[this.t-1] |= (x&((1<<(BI_DB-sh))-1))<<sh;
   1.512 +      this_array[this.t++] = (x>>(BI_DB-sh));
   1.513 +    }
   1.514 +    else
   1.515 +      this_array[this.t-1] |= x<<sh;
   1.516 +    sh += k;
   1.517 +    if(sh >= BI_DB) sh -= BI_DB;
   1.518 +  }
   1.519 +  /* END LOOP */
   1.520 +  if(k == 8 && (s[0]&0x80) != 0) {
   1.521 +    this.s = -1;
   1.522 +    if(sh > 0) this_array[this.t-1] |= ((1<<(BI_DB-sh))-1)<<sh;
   1.523 +  }
   1.524 +  this.clamp();
   1.525 +  if(mi) BigInteger.ZERO.subTo(this,this);
   1.526 +}
   1.527 +
   1.528 +// (protected) clamp off excess high words
   1.529 +function bnpClamp() {
   1.530 +  var this_array = this.array;
   1.531 +  var c = this.s&BI_DM;
   1.532 +  /* BEGIN LOOP */
   1.533 +  while(this.t > 0 && this_array[this.t-1] == c) --this.t;
   1.534 +  /* END LOOP */
   1.535 +}
   1.536 +
   1.537 +// (public) return string representation in given radix
   1.538 +function bnToString(b) {
   1.539 +  var this_array = this.array;
   1.540 +  if(this.s < 0) return "-"+this.negate().toString(b);
   1.541 +  var k;
   1.542 +  if(b == 16) k = 4;
   1.543 +  else if(b == 8) k = 3;
   1.544 +  else if(b == 2) k = 1;
   1.545 +  else if(b == 32) k = 5;
   1.546 +  else if(b == 4) k = 2;
   1.547 +  else return this.toRadix(b);
   1.548 +  var km = (1<<k)-1, d, m = false, r = "", i = this.t;
   1.549 +  var p = BI_DB-(i*BI_DB)%k;
   1.550 +  if(i-- > 0) {
   1.551 +    if(p < BI_DB && (d = this_array[i]>>p) > 0) { m = true; r = int2char(d); }
   1.552 +  /* BEGIN LOOP */
   1.553 +    while(i >= 0) {
   1.554 +      if(p < k) {
   1.555 +        d = (this_array[i]&((1<<p)-1))<<(k-p);
   1.556 +        d |= this_array[--i]>>(p+=BI_DB-k);
   1.557 +      }
   1.558 +      else {
   1.559 +        d = (this_array[i]>>(p-=k))&km;
   1.560 +        if(p <= 0) { p += BI_DB; --i; }
   1.561 +      }
   1.562 +      if(d > 0) m = true;
   1.563 +      if(m) r += int2char(d);
   1.564 +    }
   1.565 +  /* END LOOP */
   1.566 +  }
   1.567 +  return m?r:"0";
   1.568 +}
   1.569 +
   1.570 +// (public) -this
   1.571 +function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; }
   1.572 +
   1.573 +// (public) |this|
   1.574 +function bnAbs() { return (this.s<0)?this.negate():this; }
   1.575 +
   1.576 +// (public) return + if this > a, - if this < a, 0 if equal
   1.577 +function bnCompareTo(a) {
   1.578 +  var this_array = this.array;
   1.579 +  var a_array = a.array;
   1.580 +
   1.581 +  var r = this.s-a.s;
   1.582 +  if(r != 0) return r;
   1.583 +  var i = this.t;
   1.584 +  r = i-a.t;
   1.585 +  if(r != 0) return r;
   1.586 +  /* BEGIN LOOP */
   1.587 +  while(--i >= 0) if((r=this_array[i]-a_array[i]) != 0) return r;
   1.588 +  /* END LOOP */
   1.589 +  return 0;
   1.590 +}
   1.591 +
   1.592 +// returns bit length of the integer x
   1.593 +function nbits(x) {
   1.594 +  var r = 1, t;
   1.595 +  if((t=x>>>16) != 0) { x = t; r += 16; }
   1.596 +  if((t=x>>8) != 0) { x = t; r += 8; }
   1.597 +  if((t=x>>4) != 0) { x = t; r += 4; }
   1.598 +  if((t=x>>2) != 0) { x = t; r += 2; }
   1.599 +  if((t=x>>1) != 0) { x = t; r += 1; }
   1.600 +  return r;
   1.601 +}
   1.602 +
   1.603 +// (public) return the number of bits in "this"
   1.604 +function bnBitLength() {
   1.605 +  var this_array = this.array;
   1.606 +  if(this.t <= 0) return 0;
   1.607 +  return BI_DB*(this.t-1)+nbits(this_array[this.t-1]^(this.s&BI_DM));
   1.608 +}
   1.609 +
   1.610 +// (protected) r = this << n*DB
   1.611 +function bnpDLShiftTo(n,r) {
   1.612 +  var this_array = this.array;
   1.613 +  var r_array = r.array;
   1.614 +  var i;
   1.615 +  /* BEGIN LOOP */
   1.616 +  for(i = this.t-1; i >= 0; --i) r_array[i+n] = this_array[i];
   1.617 +  /* END LOOP */
   1.618 +  /* BEGIN LOOP */
   1.619 +  for(i = n-1; i >= 0; --i) r_array[i] = 0;
   1.620 +  /* END LOOP */
   1.621 +  r.t = this.t+n;
   1.622 +  r.s = this.s;
   1.623 +}
   1.624 +
   1.625 +// (protected) r = this >> n*DB
   1.626 +function bnpDRShiftTo(n,r) {
   1.627 +  var this_array = this.array;
   1.628 +  var r_array = r.array;
   1.629 +  /* BEGIN LOOP */
   1.630 +  for(var i = n; i < this.t; ++i) r_array[i-n] = this_array[i];
   1.631 +  /* END LOOP */
   1.632 +  r.t = Math.max(this.t-n,0);
   1.633 +  r.s = this.s;
   1.634 +}
   1.635 +
   1.636 +// (protected) r = this << n
   1.637 +function bnpLShiftTo(n,r) {
   1.638 +  var this_array = this.array;
   1.639 +  var r_array = r.array;
   1.640 +  var bs = n%BI_DB;
   1.641 +  var cbs = BI_DB-bs;
   1.642 +  var bm = (1<<cbs)-1;
   1.643 +  var ds = Math.floor(n/BI_DB), c = (this.s<<bs)&BI_DM, i;
   1.644 +  /* BEGIN LOOP */
   1.645 +  for(i = this.t-1; i >= 0; --i) {
   1.646 +    r_array[i+ds+1] = (this_array[i]>>cbs)|c;
   1.647 +    c = (this_array[i]&bm)<<bs;
   1.648 +  }
   1.649 +  /* END LOOP */
   1.650 +  /* BEGIN LOOP */
   1.651 +  for(i = ds-1; i >= 0; --i) r_array[i] = 0;
   1.652 +  /* END LOOP */
   1.653 +  r_array[ds] = c;
   1.654 +  r.t = this.t+ds+1;
   1.655 +  r.s = this.s;
   1.656 +  r.clamp();
   1.657 +}
   1.658 +
   1.659 +// (protected) r = this >> n
   1.660 +function bnpRShiftTo(n,r) {
   1.661 +  var this_array = this.array;
   1.662 +  var r_array = r.array;
   1.663 +  r.s = this.s;
   1.664 +  var ds = Math.floor(n/BI_DB);
   1.665 +  if(ds >= this.t) { r.t = 0; return; }
   1.666 +  var bs = n%BI_DB;
   1.667 +  var cbs = BI_DB-bs;
   1.668 +  var bm = (1<<bs)-1;
   1.669 +  r_array[0] = this_array[ds]>>bs;
   1.670 +  /* BEGIN LOOP */
   1.671 +  for(var i = ds+1; i < this.t; ++i) {
   1.672 +    r_array[i-ds-1] |= (this_array[i]&bm)<<cbs;
   1.673 +    r_array[i-ds] = this_array[i]>>bs;
   1.674 +  }
   1.675 +  /* END LOOP */
   1.676 +  if(bs > 0) r_array[this.t-ds-1] |= (this.s&bm)<<cbs;
   1.677 +  r.t = this.t-ds;
   1.678 +  r.clamp();
   1.679 +}
   1.680 +
   1.681 +// (protected) r = this - a
   1.682 +function bnpSubTo(a,r) {
   1.683 +  var this_array = this.array;
   1.684 +  var r_array = r.array;
   1.685 +  var a_array = a.array;
   1.686 +  var i = 0, c = 0, m = Math.min(a.t,this.t);
   1.687 +  /* BEGIN LOOP */
   1.688 +  while(i < m) {
   1.689 +    c += this_array[i]-a_array[i];
   1.690 +    r_array[i++] = c&BI_DM;
   1.691 +    c >>= BI_DB;
   1.692 +  }
   1.693 +  /* END LOOP */
   1.694 +  if(a.t < this.t) {
   1.695 +    c -= a.s;
   1.696 +  /* BEGIN LOOP */
   1.697 +    while(i < this.t) {
   1.698 +      c += this_array[i];
   1.699 +      r_array[i++] = c&BI_DM;
   1.700 +      c >>= BI_DB;
   1.701 +    }
   1.702 +  /* END LOOP */
   1.703 +    c += this.s;
   1.704 +  }
   1.705 +  else {
   1.706 +    c += this.s;
   1.707 +  /* BEGIN LOOP */
   1.708 +    while(i < a.t) {
   1.709 +      c -= a_array[i];
   1.710 +      r_array[i++] = c&BI_DM;
   1.711 +      c >>= BI_DB;
   1.712 +    }
   1.713 +  /* END LOOP */
   1.714 +    c -= a.s;
   1.715 +  }
   1.716 +  r.s = (c<0)?-1:0;
   1.717 +  if(c < -1) r_array[i++] = BI_DV+c;
   1.718 +  else if(c > 0) r_array[i++] = c;
   1.719 +  r.t = i;
   1.720 +  r.clamp();
   1.721 +}
   1.722 +
   1.723 +// (protected) r = this * a, r != this,a (HAC 14.12)
   1.724 +// "this" should be the larger one if appropriate.
   1.725 +function bnpMultiplyTo(a,r) {
   1.726 +  var this_array = this.array;
   1.727 +  var r_array = r.array;
   1.728 +  var x = this.abs(), y = a.abs();
   1.729 +  var y_array = y.array;
   1.730 +
   1.731 +  var i = x.t;
   1.732 +  r.t = i+y.t;
   1.733 +  /* BEGIN LOOP */
   1.734 +  while(--i >= 0) r_array[i] = 0;
   1.735 +  /* END LOOP */
   1.736 +  /* BEGIN LOOP */
   1.737 +  for(i = 0; i < y.t; ++i) r_array[i+x.t] = x.am(0,y_array[i],r,i,0,x.t);
   1.738 +  r.s = 0;
   1.739 +  r.clamp();
   1.740 +  if(this.s != a.s) BigInteger.ZERO.subTo(r,r);
   1.741 +}
   1.742 +
   1.743 +// (protected) r = this^2, r != this (HAC 14.16)
   1.744 +function bnpSquareTo(r) {
   1.745 +  var x = this.abs();
   1.746 +  var x_array = x.array;
   1.747 +  var r_array = r.array;
   1.748 +
   1.749 +  var i = r.t = 2*x.t;
   1.750 +  /* BEGIN LOOP */
   1.751 +  while(--i >= 0) r_array[i] = 0;
   1.752 +  /* END LOOP */
   1.753 +  /* BEGIN LOOP */
   1.754 +  for(i = 0; i < x.t-1; ++i) {
   1.755 +    var c = x.am(i,x_array[i],r,2*i,0,1);
   1.756 +    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) {
   1.757 +      r_array[i+x.t] -= BI_DV;
   1.758 +      r_array[i+x.t+1] = 1;
   1.759 +    }
   1.760 +  }
   1.761 +  /* END LOOP */
   1.762 +  if(r.t > 0) r_array[r.t-1] += x.am(i,x_array[i],r,2*i,0,1);
   1.763 +  r.s = 0;
   1.764 +  r.clamp();
   1.765 +}
   1.766 +
   1.767 +// (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
   1.768 +// r != q, this != m.  q or r may be null.
   1.769 +function bnpDivRemTo(m,q,r) {
   1.770 +  var pm = m.abs();
   1.771 +  if(pm.t <= 0) return;
   1.772 +  var pt = this.abs();
   1.773 +  if(pt.t < pm.t) {
   1.774 +    if(q != null) q.fromInt(0);
   1.775 +    if(r != null) this.copyTo(r);
   1.776 +    return;
   1.777 +  }
   1.778 +  if(r == null) r = nbi();
   1.779 +  var y = nbi(), ts = this.s, ms = m.s;
   1.780 +  var pm_array = pm.array;
   1.781 +  var nsh = BI_DB-nbits(pm_array[pm.t-1]);	// normalize modulus
   1.782 +  if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); }
   1.783 +  else { pm.copyTo(y); pt.copyTo(r); }
   1.784 +  var ys = y.t;
   1.785 +
   1.786 +  var y_array = y.array;
   1.787 +  var y0 = y_array[ys-1];
   1.788 +  if(y0 == 0) return;
   1.789 +  var yt = y0*(1<<BI_F1)+((ys>1)?y_array[ys-2]>>BI_F2:0);
   1.790 +  var d1 = BI_FV/yt, d2 = (1<<BI_F1)/yt, e = 1<<BI_F2;
   1.791 +  var i = r.t, j = i-ys, t = (q==null)?nbi():q;
   1.792 +  y.dlShiftTo(j,t);
   1.793 +
   1.794 +  var r_array = r.array;
   1.795 +  if(r.compareTo(t) >= 0) {
   1.796 +    r_array[r.t++] = 1;
   1.797 +    r.subTo(t,r);
   1.798 +  }
   1.799 +  BigInteger.ONE.dlShiftTo(ys,t);
   1.800 +  t.subTo(y,y);	// "negative" y so we can replace sub with am later
   1.801 +  /* BEGIN LOOP */
   1.802 +  while(y.t < ys) y_array[y.t++] = 0;
   1.803 +  /* END LOOP */
   1.804 +  /* BEGIN LOOP */
   1.805 +  while(--j >= 0) {
   1.806 +    // Estimate quotient digit
   1.807 +    var qd = (r_array[--i]==y0)?BI_DM:Math.floor(r_array[i]*d1+(r_array[i-1]+e)*d2);
   1.808 +    if((r_array[i]+=y.am(0,qd,r,j,0,ys)) < qd) {	// Try it out
   1.809 +      y.dlShiftTo(j,t);
   1.810 +      r.subTo(t,r);
   1.811 +  /* BEGIN LOOP */
   1.812 +      while(r_array[i] < --qd) r.subTo(t,r);
   1.813 +  /* END LOOP */
   1.814 +    }
   1.815 +  }
   1.816 +  /* END LOOP */
   1.817 +  if(q != null) {
   1.818 +    r.drShiftTo(ys,q);
   1.819 +    if(ts != ms) BigInteger.ZERO.subTo(q,q);
   1.820 +  }
   1.821 +  r.t = ys;
   1.822 +  r.clamp();
   1.823 +  if(nsh > 0) r.rShiftTo(nsh,r);	// Denormalize remainder
   1.824 +  if(ts < 0) BigInteger.ZERO.subTo(r,r);
   1.825 +}
   1.826 +
   1.827 +// (public) this mod a
   1.828 +function bnMod(a) {
   1.829 +  var r = nbi();
   1.830 +  this.abs().divRemTo(a,null,r);
   1.831 +  if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r);
   1.832 +  return r;
   1.833 +}
   1.834 +
   1.835 +// Modular reduction using "classic" algorithm
   1.836 +function Classic(m) { this.m = m; }
   1.837 +function cConvert(x) {
   1.838 +  if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
   1.839 +  else return x;
   1.840 +}
   1.841 +function cRevert(x) { return x; }
   1.842 +function cReduce(x) { x.divRemTo(this.m,null,x); }
   1.843 +function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
   1.844 +function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
   1.845 +
   1.846 +Classic.prototype.convert = cConvert;
   1.847 +Classic.prototype.revert = cRevert;
   1.848 +Classic.prototype.reduce = cReduce;
   1.849 +Classic.prototype.mulTo = cMulTo;
   1.850 +Classic.prototype.sqrTo = cSqrTo;
   1.851 +
   1.852 +// (protected) return "-1/this % 2^DB"; useful for Mont. reduction
   1.853 +// justification:
   1.854 +//         xy == 1 (mod m)
   1.855 +//         xy =  1+km
   1.856 +//   xy(2-xy) = (1+km)(1-km)
   1.857 +// x[y(2-xy)] = 1-k^2m^2
   1.858 +// x[y(2-xy)] == 1 (mod m^2)
   1.859 +// if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
   1.860 +// should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
   1.861 +// JS multiply "overflows" differently from C/C++, so care is needed here.
   1.862 +function bnpInvDigit() {
   1.863 +  var this_array = this.array;
   1.864 +  if(this.t < 1) return 0;
   1.865 +  var x = this_array[0];
   1.866 +  if((x&1) == 0) return 0;
   1.867 +  var y = x&3;		// y == 1/x mod 2^2
   1.868 +  y = (y*(2-(x&0xf)*y))&0xf;	// y == 1/x mod 2^4
   1.869 +  y = (y*(2-(x&0xff)*y))&0xff;	// y == 1/x mod 2^8
   1.870 +  y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff;	// y == 1/x mod 2^16
   1.871 +  // last step - calculate inverse mod DV directly;
   1.872 +  // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
   1.873 +  y = (y*(2-x*y%BI_DV))%BI_DV;		// y == 1/x mod 2^dbits
   1.874 +  // we really want the negative inverse, and -DV < y < DV
   1.875 +  return (y>0)?BI_DV-y:-y;
   1.876 +}
   1.877 +
   1.878 +// Montgomery reduction
   1.879 +function Montgomery(m) {
   1.880 +  this.m = m;
   1.881 +  this.mp = m.invDigit();
   1.882 +  this.mpl = this.mp&0x7fff;
   1.883 +  this.mph = this.mp>>15;
   1.884 +  this.um = (1<<(BI_DB-15))-1;
   1.885 +  this.mt2 = 2*m.t;
   1.886 +}
   1.887 +
   1.888 +// xR mod m
   1.889 +function montConvert(x) {
   1.890 +  var r = nbi();
   1.891 +  x.abs().dlShiftTo(this.m.t,r);
   1.892 +  r.divRemTo(this.m,null,r);
   1.893 +  if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r);
   1.894 +  return r;
   1.895 +}
   1.896 +
   1.897 +// x/R mod m
   1.898 +function montRevert(x) {
   1.899 +  var r = nbi();
   1.900 +  x.copyTo(r);
   1.901 +  this.reduce(r);
   1.902 +  return r;
   1.903 +}
   1.904 +
   1.905 +// x = x/R mod m (HAC 14.32)
   1.906 +function montReduce(x) {
   1.907 +  var x_array = x.array;
   1.908 +  /* BEGIN LOOP */
   1.909 +  while(x.t <= this.mt2)	// pad x so am has enough room later
   1.910 +    x_array[x.t++] = 0;
   1.911 +  /* END LOOP */
   1.912 +  /* BEGIN LOOP */
   1.913 +  for(var i = 0; i < this.m.t; ++i) {
   1.914 +    // faster way of calculating u0 = x[i]*mp mod DV
   1.915 +    var j = x_array[i]&0x7fff;
   1.916 +    var u0 = (j*this.mpl+(((j*this.mph+(x_array[i]>>15)*this.mpl)&this.um)<<15))&BI_DM;
   1.917 +    // use am to combine the multiply-shift-add into one call
   1.918 +    j = i+this.m.t;
   1.919 +    x_array[j] += this.m.am(0,u0,x,i,0,this.m.t);
   1.920 +    // propagate carry
   1.921 +  /* BEGIN LOOP */
   1.922 +    while(x_array[j] >= BI_DV) { x_array[j] -= BI_DV; x_array[++j]++; }
   1.923 +  /* BEGIN LOOP */
   1.924 +  }
   1.925 +  /* END LOOP */
   1.926 +  x.clamp();
   1.927 +  x.drShiftTo(this.m.t,x);
   1.928 +  if(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
   1.929 +}
   1.930 +
   1.931 +// r = "x^2/R mod m"; x != r
   1.932 +function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
   1.933 +
   1.934 +// r = "xy/R mod m"; x,y != r
   1.935 +function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
   1.936 +
   1.937 +Montgomery.prototype.convert = montConvert;
   1.938 +Montgomery.prototype.revert = montRevert;
   1.939 +Montgomery.prototype.reduce = montReduce;
   1.940 +Montgomery.prototype.mulTo = montMulTo;
   1.941 +Montgomery.prototype.sqrTo = montSqrTo;
   1.942 +
   1.943 +// (protected) true iff this is even
   1.944 +function bnpIsEven() {
   1.945 +  var this_array = this.array;
   1.946 +  return ((this.t>0)?(this_array[0]&1):this.s) == 0;
   1.947 +}
   1.948 +
   1.949 +// (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
   1.950 +function bnpExp(e,z) {
   1.951 +  if(e > 0xffffffff || e < 1) return BigInteger.ONE;
   1.952 +  var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1;
   1.953 +  g.copyTo(r);
   1.954 +  /* BEGIN LOOP */
   1.955 +  while(--i >= 0) {
   1.956 +    z.sqrTo(r,r2);
   1.957 +    if((e&(1<<i)) > 0) z.mulTo(r2,g,r);
   1.958 +    else { var t = r; r = r2; r2 = t; }
   1.959 +  }
   1.960 +  /* END LOOP */
   1.961 +  return z.revert(r);
   1.962 +}
   1.963 +
   1.964 +// (public) this^e % m, 0 <= e < 2^32
   1.965 +function bnModPowInt(e,m) {
   1.966 +  var z;
   1.967 +  if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m);
   1.968 +  return this.exp(e,z);
   1.969 +}
   1.970 +
   1.971 +// protected
   1.972 +BigInteger.prototype.copyTo = bnpCopyTo;
   1.973 +BigInteger.prototype.fromInt = bnpFromInt;
   1.974 +BigInteger.prototype.fromString = bnpFromString;
   1.975 +BigInteger.prototype.clamp = bnpClamp;
   1.976 +BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
   1.977 +BigInteger.prototype.drShiftTo = bnpDRShiftTo;
   1.978 +BigInteger.prototype.lShiftTo = bnpLShiftTo;
   1.979 +BigInteger.prototype.rShiftTo = bnpRShiftTo;
   1.980 +BigInteger.prototype.subTo = bnpSubTo;
   1.981 +BigInteger.prototype.multiplyTo = bnpMultiplyTo;
   1.982 +BigInteger.prototype.squareTo = bnpSquareTo;
   1.983 +BigInteger.prototype.divRemTo = bnpDivRemTo;
   1.984 +BigInteger.prototype.invDigit = bnpInvDigit;
   1.985 +BigInteger.prototype.isEven = bnpIsEven;
   1.986 +BigInteger.prototype.exp = bnpExp;
   1.987 +
   1.988 +// public
   1.989 +BigInteger.prototype.toString = bnToString;
   1.990 +BigInteger.prototype.negate = bnNegate;
   1.991 +BigInteger.prototype.abs = bnAbs;
   1.992 +BigInteger.prototype.compareTo = bnCompareTo;
   1.993 +BigInteger.prototype.bitLength = bnBitLength;
   1.994 +BigInteger.prototype.mod = bnMod;
   1.995 +BigInteger.prototype.modPowInt = bnModPowInt;
   1.996 +
   1.997 +// "constants"
   1.998 +BigInteger.ZERO = nbv(0);
   1.999 +BigInteger.ONE = nbv(1);
  1.1000 +// Copyright (c) 2005  Tom Wu
  1.1001 +// All Rights Reserved.
  1.1002 +// See "LICENSE" for details.
  1.1003 +
  1.1004 +// Extended JavaScript BN functions, required for RSA private ops.
  1.1005 +
  1.1006 +// (public)
  1.1007 +function bnClone() { var r = nbi(); this.copyTo(r); return r; }
  1.1008 +
  1.1009 +// (public) return value as integer
  1.1010 +function bnIntValue() {
  1.1011 +  var this_array = this.array;
  1.1012 +  if(this.s < 0) {
  1.1013 +    if(this.t == 1) return this_array[0]-BI_DV;
  1.1014 +    else if(this.t == 0) return -1;
  1.1015 +  }
  1.1016 +  else if(this.t == 1) return this_array[0];
  1.1017 +  else if(this.t == 0) return 0;
  1.1018 +  // assumes 16 < DB < 32
  1.1019 +  return ((this_array[1]&((1<<(32-BI_DB))-1))<<BI_DB)|this_array[0];
  1.1020 +}
  1.1021 +
  1.1022 +// (public) return value as byte
  1.1023 +function bnByteValue() {
  1.1024 +  var this_array = this.array;
  1.1025 +  return (this.t==0)?this.s:(this_array[0]<<24)>>24;
  1.1026 +}
  1.1027 +
  1.1028 +// (public) return value as short (assumes DB>=16)
  1.1029 +function bnShortValue() {
  1.1030 +  var this_array = this.array;
  1.1031 +  return (this.t==0)?this.s:(this_array[0]<<16)>>16;
  1.1032 +}
  1.1033 +
  1.1034 +// (protected) return x s.t. r^x < DV
  1.1035 +function bnpChunkSize(r) { return Math.floor(Math.LN2*BI_DB/Math.log(r)); }
  1.1036 +
  1.1037 +// (public) 0 if this == 0, 1 if this > 0
  1.1038 +function bnSigNum() {
  1.1039 +  var this_array = this.array;
  1.1040 +  if(this.s < 0) return -1;
  1.1041 +  else if(this.t <= 0 || (this.t == 1 && this_array[0] <= 0)) return 0;
  1.1042 +  else return 1;
  1.1043 +}
  1.1044 +
  1.1045 +// (protected) convert to radix string
  1.1046 +function bnpToRadix(b) {
  1.1047 +  if(b == null) b = 10;
  1.1048 +  if(this.signum() == 0 || b < 2 || b > 36) return "0";
  1.1049 +  var cs = this.chunkSize(b);
  1.1050 +  var a = Math.pow(b,cs);
  1.1051 +  var d = nbv(a), y = nbi(), z = nbi(), r = "";
  1.1052 +  this.divRemTo(d,y,z);
  1.1053 +  /* BEGIN LOOP */
  1.1054 +  while(y.signum() > 0) {
  1.1055 +    r = (a+z.intValue()).toString(b).substr(1) + r;
  1.1056 +    y.divRemTo(d,y,z);
  1.1057 +  }
  1.1058 +  /* END LOOP */
  1.1059 +  return z.intValue().toString(b) + r;
  1.1060 +}
  1.1061 +
  1.1062 +// (protected) convert from radix string
  1.1063 +function bnpFromRadix(s,b) {
  1.1064 +  this.fromInt(0);
  1.1065 +  if(b == null) b = 10;
  1.1066 +  var cs = this.chunkSize(b);
  1.1067 +  var d = Math.pow(b,cs), mi = false, j = 0, w = 0;
  1.1068 +  /* BEGIN LOOP */
  1.1069 +  for(var i = 0; i < s.length; ++i) {
  1.1070 +    var x = intAt(s,i);
  1.1071 +    if(x < 0) {
  1.1072 +      if(s.charAt(i) == "-" && this.signum() == 0) mi = true;
  1.1073 +      continue;
  1.1074 +    }
  1.1075 +    w = b*w+x;
  1.1076 +    if(++j >= cs) {
  1.1077 +      this.dMultiply(d);
  1.1078 +      this.dAddOffset(w,0);
  1.1079 +      j = 0;
  1.1080 +      w = 0;
  1.1081 +    }
  1.1082 +  }
  1.1083 +  /* END LOOP */
  1.1084 +  if(j > 0) {
  1.1085 +    this.dMultiply(Math.pow(b,j));
  1.1086 +    this.dAddOffset(w,0);
  1.1087 +  }
  1.1088 +  if(mi) BigInteger.ZERO.subTo(this,this);
  1.1089 +}
  1.1090 +
  1.1091 +// (protected) alternate constructor
  1.1092 +function bnpFromNumber(a,b,c) {
  1.1093 +  if("number" == typeof b) {
  1.1094 +    // new BigInteger(int,int,RNG)
  1.1095 +    if(a < 2) this.fromInt(1);
  1.1096 +    else {
  1.1097 +      this.fromNumber(a,c);
  1.1098 +      if(!this.testBit(a-1))	// force MSB set
  1.1099 +        this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this);
  1.1100 +      if(this.isEven()) this.dAddOffset(1,0); // force odd
  1.1101 +  /* BEGIN LOOP */
  1.1102 +      while(!this.isProbablePrime(b)) {
  1.1103 +        this.dAddOffset(2,0);
  1.1104 +        if(this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a-1),this);
  1.1105 +      }
  1.1106 +  /* END LOOP */
  1.1107 +    }
  1.1108 +  }
  1.1109 +  else {
  1.1110 +    // new BigInteger(int,RNG)
  1.1111 +    var x = new Array(), t = a&7;
  1.1112 +    x.length = (a>>3)+1;
  1.1113 +    b.nextBytes(x);
  1.1114 +    if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0;
  1.1115 +    this.fromString(x,256);
  1.1116 +  }
  1.1117 +}
  1.1118 +
  1.1119 +// (public) convert to bigendian byte array
  1.1120 +function bnToByteArray() {
  1.1121 +  var this_array = this.array;
  1.1122 +  var i = this.t, r = new Array();
  1.1123 +  r[0] = this.s;
  1.1124 +  var p = BI_DB-(i*BI_DB)%8, d, k = 0;
  1.1125 +  if(i-- > 0) {
  1.1126 +    if(p < BI_DB && (d = this_array[i]>>p) != (this.s&BI_DM)>>p)
  1.1127 +      r[k++] = d|(this.s<<(BI_DB-p));
  1.1128 +  /* BEGIN LOOP */
  1.1129 +    while(i >= 0) {
  1.1130 +      if(p < 8) {
  1.1131 +        d = (this_array[i]&((1<<p)-1))<<(8-p);
  1.1132 +        d |= this_array[--i]>>(p+=BI_DB-8);
  1.1133 +      }
  1.1134 +      else {
  1.1135 +        d = (this_array[i]>>(p-=8))&0xff;
  1.1136 +        if(p <= 0) { p += BI_DB; --i; }
  1.1137 +      }
  1.1138 +      if((d&0x80) != 0) d |= -256;
  1.1139 +      if(k == 0 && (this.s&0x80) != (d&0x80)) ++k;
  1.1140 +      if(k > 0 || d != this.s) r[k++] = d;
  1.1141 +    }
  1.1142 +  /* END LOOP */
  1.1143 +  }
  1.1144 +  return r;
  1.1145 +}
  1.1146 +
  1.1147 +function bnEquals(a) { return(this.compareTo(a)==0); }
  1.1148 +function bnMin(a) { return(this.compareTo(a)<0)?this:a; }
  1.1149 +function bnMax(a) { return(this.compareTo(a)>0)?this:a; }
  1.1150 +
  1.1151 +// (protected) r = this op a (bitwise)
  1.1152 +function bnpBitwiseTo(a,op,r) {
  1.1153 +  var this_array = this.array;
  1.1154 +  var a_array    = a.array;
  1.1155 +  var r_array    = r.array;
  1.1156 +  var i, f, m = Math.min(a.t,this.t);
  1.1157 +  /* BEGIN LOOP */
  1.1158 +  for(i = 0; i < m; ++i) r_array[i] = op(this_array[i],a_array[i]);
  1.1159 +  /* END LOOP */
  1.1160 +  if(a.t < this.t) {
  1.1161 +    f = a.s&BI_DM;
  1.1162 +  /* BEGIN LOOP */
  1.1163 +    for(i = m; i < this.t; ++i) r_array[i] = op(this_array[i],f);
  1.1164 +  /* END LOOP */
  1.1165 +    r.t = this.t;
  1.1166 +  }
  1.1167 +  else {
  1.1168 +    f = this.s&BI_DM;
  1.1169 +  /* BEGIN LOOP */
  1.1170 +    for(i = m; i < a.t; ++i) r_array[i] = op(f,a_array[i]);
  1.1171 +  /* END LOOP */
  1.1172 +    r.t = a.t;
  1.1173 +  }
  1.1174 +  r.s = op(this.s,a.s);
  1.1175 +  r.clamp();
  1.1176 +}
  1.1177 +
  1.1178 +// (public) this & a
  1.1179 +function op_and(x,y) { return x&y; }
  1.1180 +function bnAnd(a) { var r = nbi(); this.bitwiseTo(a,op_and,r); return r; }
  1.1181 +
  1.1182 +// (public) this | a
  1.1183 +function op_or(x,y) { return x|y; }
  1.1184 +function bnOr(a) { var r = nbi(); this.bitwiseTo(a,op_or,r); return r; }
  1.1185 +
  1.1186 +// (public) this ^ a
  1.1187 +function op_xor(x,y) { return x^y; }
  1.1188 +function bnXor(a) { var r = nbi(); this.bitwiseTo(a,op_xor,r); return r; }
  1.1189 +
  1.1190 +// (public) this & ~a
  1.1191 +function op_andnot(x,y) { return x&~y; }
  1.1192 +function bnAndNot(a) { var r = nbi(); this.bitwiseTo(a,op_andnot,r); return r; }
  1.1193 +
  1.1194 +// (public) ~this
  1.1195 +function bnNot() {
  1.1196 +  var this_array = this.array;
  1.1197 +  var r = nbi();
  1.1198 +  var r_array = r.array;
  1.1199 +
  1.1200 +  /* BEGIN LOOP */
  1.1201 +  for(var i = 0; i < this.t; ++i) r_array[i] = BI_DM&~this_array[i];
  1.1202 +  /* END LOOP */
  1.1203 +  r.t = this.t;
  1.1204 +  r.s = ~this.s;
  1.1205 +  return r;
  1.1206 +}
  1.1207 +
  1.1208 +// (public) this << n
  1.1209 +function bnShiftLeft(n) {
  1.1210 +  var r = nbi();
  1.1211 +  if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r);
  1.1212 +  return r;
  1.1213 +}
  1.1214 +
  1.1215 +// (public) this >> n
  1.1216 +function bnShiftRight(n) {
  1.1217 +  var r = nbi();
  1.1218 +  if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r);
  1.1219 +  return r;
  1.1220 +}
  1.1221 +
  1.1222 +// return index of lowest 1-bit in x, x < 2^31
  1.1223 +function lbit(x) {
  1.1224 +  if(x == 0) return -1;
  1.1225 +  var r = 0;
  1.1226 +  if((x&0xffff) == 0) { x >>= 16; r += 16; }
  1.1227 +  if((x&0xff) == 0) { x >>= 8; r += 8; }
  1.1228 +  if((x&0xf) == 0) { x >>= 4; r += 4; }
  1.1229 +  if((x&3) == 0) { x >>= 2; r += 2; }
  1.1230 +  if((x&1) == 0) ++r;
  1.1231 +  return r;
  1.1232 +}
  1.1233 +
  1.1234 +// (public) returns index of lowest 1-bit (or -1 if none)
  1.1235 +function bnGetLowestSetBit() {
  1.1236 +  var this_array = this.array;
  1.1237 +  /* BEGIN LOOP */
  1.1238 +  for(var i = 0; i < this.t; ++i)
  1.1239 +    if(this_array[i] != 0) return i*BI_DB+lbit(this_array[i]);
  1.1240 +  /* END LOOP */
  1.1241 +  if(this.s < 0) return this.t*BI_DB;
  1.1242 +  return -1;
  1.1243 +}
  1.1244 +
  1.1245 +// return number of 1 bits in x
  1.1246 +function cbit(x) {
  1.1247 +  var r = 0;
  1.1248 +  /* BEGIN LOOP */
  1.1249 +  while(x != 0) { x &= x-1; ++r; }
  1.1250 +  /* END LOOP */
  1.1251 +  return r;
  1.1252 +}
  1.1253 +
  1.1254 +// (public) return number of set bits
  1.1255 +function bnBitCount() {
  1.1256 +  var r = 0, x = this.s&BI_DM;
  1.1257 +  /* BEGIN LOOP */
  1.1258 +  for(var i = 0; i < this.t; ++i) r += cbit(this_array[i]^x);
  1.1259 +  /* END LOOP */
  1.1260 +  return r;
  1.1261 +}
  1.1262 +
  1.1263 +// (public) true iff nth bit is set
  1.1264 +function bnTestBit(n) {
  1.1265 +  var this_array = this.array;
  1.1266 +  var j = Math.floor(n/BI_DB);
  1.1267 +  if(j >= this.t) return(this.s!=0);
  1.1268 +  return((this_array[j]&(1<<(n%BI_DB)))!=0);
  1.1269 +}
  1.1270 +
  1.1271 +// (protected) this op (1<<n)
  1.1272 +function bnpChangeBit(n,op) {
  1.1273 +  var r = BigInteger.ONE.shiftLeft(n);
  1.1274 +  this.bitwiseTo(r,op,r);
  1.1275 +  return r;
  1.1276 +}
  1.1277 +
  1.1278 +// (public) this | (1<<n)
  1.1279 +function bnSetBit(n) { return this.changeBit(n,op_or); }
  1.1280 +
  1.1281 +// (public) this & ~(1<<n)
  1.1282 +function bnClearBit(n) { return this.changeBit(n,op_andnot); }
  1.1283 +
  1.1284 +// (public) this ^ (1<<n)
  1.1285 +function bnFlipBit(n) { return this.changeBit(n,op_xor); }
  1.1286 +
  1.1287 +// (protected) r = this + a
  1.1288 +function bnpAddTo(a,r) {
  1.1289 +  var this_array = this.array;
  1.1290 +  var a_array = a.array;
  1.1291 +  var r_array = r.array;
  1.1292 +  var i = 0, c = 0, m = Math.min(a.t,this.t);
  1.1293 +  /* BEGIN LOOP */
  1.1294 +  while(i < m) {
  1.1295 +    c += this_array[i]+a_array[i];
  1.1296 +    r_array[i++] = c&BI_DM;
  1.1297 +    c >>= BI_DB;
  1.1298 +  }
  1.1299 +  /* END LOOP */
  1.1300 +  if(a.t < this.t) {
  1.1301 +    c += a.s;
  1.1302 +  /* BEGIN LOOP */
  1.1303 +    while(i < this.t) {
  1.1304 +      c += this_array[i];
  1.1305 +      r_array[i++] = c&BI_DM;
  1.1306 +      c >>= BI_DB;
  1.1307 +    }
  1.1308 +  /* END LOOP */
  1.1309 +    c += this.s;
  1.1310 +  }
  1.1311 +  else {
  1.1312 +    c += this.s;
  1.1313 +  /* BEGIN LOOP */
  1.1314 +    while(i < a.t) {
  1.1315 +      c += a_array[i];
  1.1316 +      r_array[i++] = c&BI_DM;
  1.1317 +      c >>= BI_DB;
  1.1318 +    }
  1.1319 +  /* END LOOP */
  1.1320 +    c += a.s;
  1.1321 +  }
  1.1322 +  r.s = (c<0)?-1:0;
  1.1323 +  if(c > 0) r_array[i++] = c;
  1.1324 +  else if(c < -1) r_array[i++] = BI_DV+c;
  1.1325 +  r.t = i;
  1.1326 +  r.clamp();
  1.1327 +}
  1.1328 +
  1.1329 +// (public) this + a
  1.1330 +function bnAdd(a) { var r = nbi(); this.addTo(a,r); return r; }
  1.1331 +
  1.1332 +// (public) this - a
  1.1333 +function bnSubtract(a) { var r = nbi(); this.subTo(a,r); return r; }
  1.1334 +
  1.1335 +// (public) this * a
  1.1336 +function bnMultiply(a) { var r = nbi(); this.multiplyTo(a,r); return r; }
  1.1337 +
  1.1338 +// (public) this / a
  1.1339 +function bnDivide(a) { var r = nbi(); this.divRemTo(a,r,null); return r; }
  1.1340 +
  1.1341 +// (public) this % a
  1.1342 +function bnRemainder(a) { var r = nbi(); this.divRemTo(a,null,r); return r; }
  1.1343 +
  1.1344 +// (public) [this/a,this%a]
  1.1345 +function bnDivideAndRemainder(a) {
  1.1346 +  var q = nbi(), r = nbi();
  1.1347 +  this.divRemTo(a,q,r);
  1.1348 +  return new Array(q,r);
  1.1349 +}
  1.1350 +
  1.1351 +// (protected) this *= n, this >= 0, 1 < n < DV
  1.1352 +function bnpDMultiply(n) {
  1.1353 +  var this_array = this.array;
  1.1354 +  this_array[this.t] = this.am(0,n-1,this,0,0,this.t);
  1.1355 +  ++this.t;
  1.1356 +  this.clamp();
  1.1357 +}
  1.1358 +
  1.1359 +// (protected) this += n << w words, this >= 0
  1.1360 +function bnpDAddOffset(n,w) {
  1.1361 +  var this_array = this.array;
  1.1362 +  /* BEGIN LOOP */
  1.1363 +  while(this.t <= w) this_array[this.t++] = 0;
  1.1364 +  /* END LOOP */
  1.1365 +  this_array[w] += n;
  1.1366 +  /* BEGIN LOOP */
  1.1367 +  while(this_array[w] >= BI_DV) {
  1.1368 +    this_array[w] -= BI_DV;
  1.1369 +    if(++w >= this.t) this_array[this.t++] = 0;
  1.1370 +    ++this_array[w];
  1.1371 +  }
  1.1372 +  /* END LOOP */
  1.1373 +}
  1.1374 +
  1.1375 +// A "null" reducer
  1.1376 +function NullExp() {}
  1.1377 +function nNop(x) { return x; }
  1.1378 +function nMulTo(x,y,r) { x.multiplyTo(y,r); }
  1.1379 +function nSqrTo(x,r) { x.squareTo(r); }
  1.1380 +
  1.1381 +NullExp.prototype.convert = nNop;
  1.1382 +NullExp.prototype.revert = nNop;
  1.1383 +NullExp.prototype.mulTo = nMulTo;
  1.1384 +NullExp.prototype.sqrTo = nSqrTo;
  1.1385 +
  1.1386 +// (public) this^e
  1.1387 +function bnPow(e) { return this.exp(e,new NullExp()); }
  1.1388 +
  1.1389 +// (protected) r = lower n words of "this * a", a.t <= n
  1.1390 +// "this" should be the larger one if appropriate.
  1.1391 +function bnpMultiplyLowerTo(a,n,r) {
  1.1392 +  var r_array = r.array;
  1.1393 +  var a_array = a.array;
  1.1394 +  var i = Math.min(this.t+a.t,n);
  1.1395 +  r.s = 0; // assumes a,this >= 0
  1.1396 +  r.t = i;
  1.1397 +  /* BEGIN LOOP */
  1.1398 +  while(i > 0) r_array[--i] = 0;
  1.1399 +  /* END LOOP */
  1.1400 +  var j;
  1.1401 +  /* BEGIN LOOP */
  1.1402 +  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);
  1.1403 +  /* END LOOP */
  1.1404 +  /* BEGIN LOOP */
  1.1405 +  for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a_array[i],r,i,0,n-i);
  1.1406 +  /* END LOOP */
  1.1407 +  r.clamp();
  1.1408 +}
  1.1409 +
  1.1410 +// (protected) r = "this * a" without lower n words, n > 0
  1.1411 +// "this" should be the larger one if appropriate.
  1.1412 +function bnpMultiplyUpperTo(a,n,r) {
  1.1413 +  var r_array = r.array;
  1.1414 +  var a_array = a.array;
  1.1415 +  --n;
  1.1416 +  var i = r.t = this.t+a.t-n;
  1.1417 +  r.s = 0; // assumes a,this >= 0
  1.1418 +  /* BEGIN LOOP */
  1.1419 +  while(--i >= 0) r_array[i] = 0;
  1.1420 +  /* END LOOP */
  1.1421 +  /* BEGIN LOOP */
  1.1422 +  for(i = Math.max(n-this.t,0); i < a.t; ++i)
  1.1423 +    r_array[this.t+i-n] = this.am(n-i,a_array[i],r,0,0,this.t+i-n);
  1.1424 +  /* END LOOP */
  1.1425 +  r.clamp();
  1.1426 +  r.drShiftTo(1,r);
  1.1427 +}
  1.1428 +
  1.1429 +// Barrett modular reduction
  1.1430 +function Barrett(m) {
  1.1431 +  // setup Barrett
  1.1432 +  this.r2 = nbi();
  1.1433 +  this.q3 = nbi();
  1.1434 +  BigInteger.ONE.dlShiftTo(2*m.t,this.r2);
  1.1435 +  this.mu = this.r2.divide(m);
  1.1436 +  this.m = m;
  1.1437 +}
  1.1438 +
  1.1439 +function barrettConvert(x) {
  1.1440 +  if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m);
  1.1441 +  else if(x.compareTo(this.m) < 0) return x;
  1.1442 +  else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; }
  1.1443 +}
  1.1444 +
  1.1445 +function barrettRevert(x) { return x; }
  1.1446 +
  1.1447 +// x = x mod m (HAC 14.42)
  1.1448 +function barrettReduce(x) {
  1.1449 +  x.drShiftTo(this.m.t-1,this.r2);
  1.1450 +  if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); }
  1.1451 +  this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3);
  1.1452 +  this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);
  1.1453 +  /* BEGIN LOOP */
  1.1454 +  while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1);
  1.1455 +  /* END LOOP */
  1.1456 +  x.subTo(this.r2,x);
  1.1457 +  /* BEGIN LOOP */
  1.1458 +  while(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
  1.1459 +  /* END LOOP */
  1.1460 +}
  1.1461 +
  1.1462 +// r = x^2 mod m; x != r
  1.1463 +function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
  1.1464 +
  1.1465 +// r = x*y mod m; x,y != r
  1.1466 +function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
  1.1467 +
  1.1468 +Barrett.prototype.convert = barrettConvert;
  1.1469 +Barrett.prototype.revert = barrettRevert;
  1.1470 +Barrett.prototype.reduce = barrettReduce;
  1.1471 +Barrett.prototype.mulTo = barrettMulTo;
  1.1472 +Barrett.prototype.sqrTo = barrettSqrTo;
  1.1473 +
  1.1474 +// (public) this^e % m (HAC 14.85)
  1.1475 +function bnModPow(e,m) {
  1.1476 +  var e_array = e.array;
  1.1477 +  var i = e.bitLength(), k, r = nbv(1), z;
  1.1478 +  if(i <= 0) return r;
  1.1479 +  else if(i < 18) k = 1;
  1.1480 +  else if(i < 48) k = 3;
  1.1481 +  else if(i < 144) k = 4;
  1.1482 +  else if(i < 768) k = 5;
  1.1483 +  else k = 6;
  1.1484 +  if(i < 8)
  1.1485 +    z = new Classic(m);
  1.1486 +  else if(m.isEven())
  1.1487 +    z = new Barrett(m);
  1.1488 +  else
  1.1489 +    z = new Montgomery(m);
  1.1490 +
  1.1491 +  // precomputation
  1.1492 +  var g = new Array(), n = 3, k1 = k-1, km = (1<<k)-1;
  1.1493 +  g[1] = z.convert(this);
  1.1494 +  if(k > 1) {
  1.1495 +    var g2 = nbi();
  1.1496 +    z.sqrTo(g[1],g2);
  1.1497 +  /* BEGIN LOOP */
  1.1498 +    while(n <= km) {
  1.1499 +      g[n] = nbi();
  1.1500 +      z.mulTo(g2,g[n-2],g[n]);
  1.1501 +      n += 2;
  1.1502 +    }
  1.1503 +  /* END LOOP */
  1.1504 +  }
  1.1505 +
  1.1506 +  var j = e.t-1, w, is1 = true, r2 = nbi(), t;
  1.1507 +  i = nbits(e_array[j])-1;
  1.1508 +  /* BEGIN LOOP */
  1.1509 +  while(j >= 0) {
  1.1510 +    if(i >= k1) w = (e_array[j]>>(i-k1))&km;
  1.1511 +    else {
  1.1512 +      w = (e_array[j]&((1<<(i+1))-1))<<(k1-i);
  1.1513 +      if(j > 0) w |= e_array[j-1]>>(BI_DB+i-k1);
  1.1514 +    }
  1.1515 +
  1.1516 +    n = k;
  1.1517 +  /* BEGIN LOOP */
  1.1518 +    while((w&1) == 0) { w >>= 1; --n; }
  1.1519 +  /* END LOOP */
  1.1520 +    if((i -= n) < 0) { i += BI_DB; --j; }
  1.1521 +    if(is1) {	// ret == 1, don't bother squaring or multiplying it
  1.1522 +      g[w].copyTo(r);
  1.1523 +      is1 = false;
  1.1524 +    }
  1.1525 +    else {
  1.1526 +  /* BEGIN LOOP */
  1.1527 +      while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; }
  1.1528 +  /* END LOOP */
  1.1529 +      if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; }
  1.1530 +      z.mulTo(r2,g[w],r);
  1.1531 +    }
  1.1532 +
  1.1533 +  /* BEGIN LOOP */
  1.1534 +    while(j >= 0 && (e_array[j]&(1<<i)) == 0) {
  1.1535 +      z.sqrTo(r,r2); t = r; r = r2; r2 = t;
  1.1536 +      if(--i < 0) { i = BI_DB-1; --j; }
  1.1537 +    }
  1.1538 +  /* END LOOP */
  1.1539 +  }
  1.1540 +  /* END LOOP */
  1.1541 +  return z.revert(r);
  1.1542 +}
  1.1543 +
  1.1544 +// (public) gcd(this,a) (HAC 14.54)
  1.1545 +function bnGCD(a) {
  1.1546 +  var x = (this.s<0)?this.negate():this.clone();
  1.1547 +  var y = (a.s<0)?a.negate():a.clone();
  1.1548 +  if(x.compareTo(y) < 0) { var t = x; x = y; y = t; }
  1.1549 +  var i = x.getLowestSetBit(), g = y.getLowestSetBit();
  1.1550 +  if(g < 0) return x;
  1.1551 +  if(i < g) g = i;
  1.1552 +  if(g > 0) {
  1.1553 +    x.rShiftTo(g,x);
  1.1554 +    y.rShiftTo(g,y);
  1.1555 +  }
  1.1556 +  /* BEGIN LOOP */
  1.1557 +  while(x.signum() > 0) {
  1.1558 +    if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x);
  1.1559 +    if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y);
  1.1560 +    if(x.compareTo(y) >= 0) {
  1.1561 +      x.subTo(y,x);
  1.1562 +      x.rShiftTo(1,x);
  1.1563 +    }
  1.1564 +    else {
  1.1565 +      y.subTo(x,y);
  1.1566 +      y.rShiftTo(1,y);
  1.1567 +    }
  1.1568 +  }
  1.1569 +  /* END LOOP */
  1.1570 +  if(g > 0) y.lShiftTo(g,y);
  1.1571 +  return y;
  1.1572 +}
  1.1573 +
  1.1574 +// (protected) this % n, n < 2^26
  1.1575 +function bnpModInt(n) {
  1.1576 +  var this_array = this.array;
  1.1577 +  if(n <= 0) return 0;
  1.1578 +  var d = BI_DV%n, r = (this.s<0)?n-1:0;
  1.1579 +  if(this.t > 0)
  1.1580 +    if(d == 0) r = this_array[0]%n;
  1.1581 +    else for(var i = this.t-1; i >= 0; --i) r = (d*r+this_array[i])%n;
  1.1582 +  return r;
  1.1583 +}
  1.1584 +
  1.1585 +// (public) 1/this % m (HAC 14.61)
  1.1586 +function bnModInverse(m) {
  1.1587 +  var ac = m.isEven();
  1.1588 +  if((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO;
  1.1589 +  var u = m.clone(), v = this.clone();
  1.1590 +  var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1);
  1.1591 +  /* BEGIN LOOP */
  1.1592 +  while(u.signum() != 0) {
  1.1593 +  /* BEGIN LOOP */
  1.1594 +    while(u.isEven()) {
  1.1595 +      u.rShiftTo(1,u);
  1.1596 +      if(ac) {
  1.1597 +        if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); }
  1.1598 +        a.rShiftTo(1,a);
  1.1599 +      }
  1.1600 +      else if(!b.isEven()) b.subTo(m,b);
  1.1601 +      b.rShiftTo(1,b);
  1.1602 +    }
  1.1603 +  /* END LOOP */
  1.1604 +  /* BEGIN LOOP */
  1.1605 +    while(v.isEven()) {
  1.1606 +      v.rShiftTo(1,v);
  1.1607 +      if(ac) {
  1.1608 +        if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); }
  1.1609 +        c.rShiftTo(1,c);
  1.1610 +      }
  1.1611 +      else if(!d.isEven()) d.subTo(m,d);
  1.1612 +      d.rShiftTo(1,d);
  1.1613 +    }
  1.1614 +  /* END LOOP */
  1.1615 +    if(u.compareTo(v) >= 0) {
  1.1616 +      u.subTo(v,u);
  1.1617 +      if(ac) a.subTo(c,a);
  1.1618 +      b.subTo(d,b);
  1.1619 +    }
  1.1620 +    else {
  1.1621 +      v.subTo(u,v);
  1.1622 +      if(ac) c.subTo(a,c);
  1.1623 +      d.subTo(b,d);
  1.1624 +    }
  1.1625 +  }
  1.1626 +  /* END LOOP */
  1.1627 +  if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
  1.1628 +  if(d.compareTo(m) >= 0) return d.subtract(m);
  1.1629 +  if(d.signum() < 0) d.addTo(m,d); else return d;
  1.1630 +  if(d.signum() < 0) return d.add(m); else return d;
  1.1631 +}
  1.1632 +
  1.1633 +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];
  1.1634 +var lplim = (1<<26)/lowprimes[lowprimes.length-1];
  1.1635 +
  1.1636 +// (public) test primality with certainty >= 1-.5^t
  1.1637 +function bnIsProbablePrime(t) {
  1.1638 +  var i, x = this.abs();
  1.1639 +  var x_array = x.array;
  1.1640 +  if(x.t == 1 && x_array[0] <= lowprimes[lowprimes.length-1]) {
  1.1641 +    for(i = 0; i < lowprimes.length; ++i)
  1.1642 +      if(x_array[0] == lowprimes[i]) return true;
  1.1643 +    return false;
  1.1644 +  }
  1.1645 +  if(x.isEven()) return false;
  1.1646 +  i = 1;
  1.1647 +  /* BEGIN LOOP */
  1.1648 +  while(i < lowprimes.length) {
  1.1649 +    var m = lowprimes[i], j = i+1;
  1.1650 +  /* BEGIN LOOP */
  1.1651 +    while(j < lowprimes.length && m < lplim) m *= lowprimes[j++];
  1.1652 +  /* END LOOP */
  1.1653 +    m = x.modInt(m);
  1.1654 +  /* BEGIN LOOP */
  1.1655 +    while(i < j) if(m%lowprimes[i++] == 0) return false;
  1.1656 +  /* END LOOP */
  1.1657 +  }
  1.1658 +  /* END LOOP */
  1.1659 +  return x.millerRabin(t);
  1.1660 +}
  1.1661 +
  1.1662 +// (protected) true if probably prime (HAC 4.24, Miller-Rabin)
  1.1663 +function bnpMillerRabin(t) {
  1.1664 +  var n1 = this.subtract(BigInteger.ONE);
  1.1665 +  var k = n1.getLowestSetBit();
  1.1666 +  if(k <= 0) return false;
  1.1667 +  var r = n1.shiftRight(k);
  1.1668 +  t = (t+1)>>1;
  1.1669 +  if(t > lowprimes.length) t = lowprimes.length;
  1.1670 +  var a = nbi();
  1.1671 +  /* BEGIN LOOP */
  1.1672 +  for(var i = 0; i < t; ++i) {
  1.1673 +    a.fromInt(lowprimes[i]);
  1.1674 +    var y = a.modPow(r,this);
  1.1675 +    if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
  1.1676 +      var j = 1;
  1.1677 +  /* BEGIN LOOP */
  1.1678 +      while(j++ < k && y.compareTo(n1) != 0) {
  1.1679 +        y = y.modPowInt(2,this);
  1.1680 +        if(y.compareTo(BigInteger.ONE) == 0) return false;
  1.1681 +      }
  1.1682 +  /* END LOOP */
  1.1683 +      if(y.compareTo(n1) != 0) return false;
  1.1684 +    }
  1.1685 +  }
  1.1686 +  /* END LOOP */
  1.1687 +  return true;
  1.1688 +}
  1.1689 +
  1.1690 +// protected
  1.1691 +BigInteger.prototype.chunkSize = bnpChunkSize;
  1.1692 +BigInteger.prototype.toRadix = bnpToRadix;
  1.1693 +BigInteger.prototype.fromRadix = bnpFromRadix;
  1.1694 +BigInteger.prototype.fromNumber = bnpFromNumber;
  1.1695 +BigInteger.prototype.bitwiseTo = bnpBitwiseTo;
  1.1696 +BigInteger.prototype.changeBit = bnpChangeBit;
  1.1697 +BigInteger.prototype.addTo = bnpAddTo;
  1.1698 +BigInteger.prototype.dMultiply = bnpDMultiply;
  1.1699 +BigInteger.prototype.dAddOffset = bnpDAddOffset;
  1.1700 +BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo;
  1.1701 +BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
  1.1702 +BigInteger.prototype.modInt = bnpModInt;
  1.1703 +BigInteger.prototype.millerRabin = bnpMillerRabin;
  1.1704 +
  1.1705 +// public
  1.1706 +BigInteger.prototype.clone = bnClone;
  1.1707 +BigInteger.prototype.intValue = bnIntValue;
  1.1708 +BigInteger.prototype.byteValue = bnByteValue;
  1.1709 +BigInteger.prototype.shortValue = bnShortValue;
  1.1710 +BigInteger.prototype.signum = bnSigNum;
  1.1711 +BigInteger.prototype.toByteArray = bnToByteArray;
  1.1712 +BigInteger.prototype.equals = bnEquals;
  1.1713 +BigInteger.prototype.min = bnMin;
  1.1714 +BigInteger.prototype.max = bnMax;
  1.1715 +BigInteger.prototype.and = bnAnd;
  1.1716 +BigInteger.prototype.or = bnOr;
  1.1717 +BigInteger.prototype.xor = bnXor;
  1.1718 +BigInteger.prototype.andNot = bnAndNot;
  1.1719 +BigInteger.prototype.not = bnNot;
  1.1720 +BigInteger.prototype.shiftLeft = bnShiftLeft;
  1.1721 +BigInteger.prototype.shiftRight = bnShiftRight;
  1.1722 +BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit;
  1.1723 +BigInteger.prototype.bitCount = bnBitCount;
  1.1724 +BigInteger.prototype.testBit = bnTestBit;
  1.1725 +BigInteger.prototype.setBit = bnSetBit;
  1.1726 +BigInteger.prototype.clearBit = bnClearBit;
  1.1727 +BigInteger.prototype.flipBit = bnFlipBit;
  1.1728 +BigInteger.prototype.add = bnAdd;
  1.1729 +BigInteger.prototype.subtract = bnSubtract;
  1.1730 +BigInteger.prototype.multiply = bnMultiply;
  1.1731 +BigInteger.prototype.divide = bnDivide;
  1.1732 +BigInteger.prototype.remainder = bnRemainder;
  1.1733 +BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder;
  1.1734 +BigInteger.prototype.modPow = bnModPow;
  1.1735 +BigInteger.prototype.modInverse = bnModInverse;
  1.1736 +BigInteger.prototype.pow = bnPow;
  1.1737 +BigInteger.prototype.gcd = bnGCD;
  1.1738 +BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
  1.1739 +
  1.1740 +// BigInteger interfaces not implemented in jsbn:
  1.1741 +
  1.1742 +// BigInteger(int signum, byte[] magnitude)
  1.1743 +// double doubleValue()
  1.1744 +// float floatValue()
  1.1745 +// int hashCode()
  1.1746 +// long longValue()
  1.1747 +// static BigInteger valueOf(long val)
  1.1748 +// prng4.js - uses Arcfour as a PRNG
  1.1749 +
  1.1750 +function Arcfour() {
  1.1751 +  this.i = 0;
  1.1752 +  this.j = 0;
  1.1753 +  this.S = new Array();
  1.1754 +}
  1.1755 +
  1.1756 +// Initialize arcfour context from key, an array of ints, each from [0..255]
  1.1757 +function ARC4init(key) {
  1.1758 +  var i, j, t;
  1.1759 +  /* BEGIN LOOP */
  1.1760 +  for(i = 0; i < 256; ++i)
  1.1761 +    this.S[i] = i;
  1.1762 +  /* END LOOP */
  1.1763 +  j = 0;
  1.1764 +  /* BEGIN LOOP */
  1.1765 +  for(i = 0; i < 256; ++i) {
  1.1766 +    j = (j + this.S[i] + key[i % key.length]) & 255;
  1.1767 +    t = this.S[i];
  1.1768 +    this.S[i] = this.S[j];
  1.1769 +    this.S[j] = t;
  1.1770 +  }
  1.1771 +  /* END LOOP */
  1.1772 +  this.i = 0;
  1.1773 +  this.j = 0;
  1.1774 +}
  1.1775 +
  1.1776 +function ARC4next() {
  1.1777 +  var t;
  1.1778 +  this.i = (this.i + 1) & 255;
  1.1779 +  this.j = (this.j + this.S[this.i]) & 255;
  1.1780 +  t = this.S[this.i];
  1.1781 +  this.S[this.i] = this.S[this.j];
  1.1782 +  this.S[this.j] = t;
  1.1783 +  return this.S[(t + this.S[this.i]) & 255];
  1.1784 +}
  1.1785 +
  1.1786 +Arcfour.prototype.init = ARC4init;
  1.1787 +Arcfour.prototype.next = ARC4next;
  1.1788 +
  1.1789 +// Plug in your RNG constructor here
  1.1790 +function prng_newstate() {
  1.1791 +  return new Arcfour();
  1.1792 +}
  1.1793 +
  1.1794 +// Pool size must be a multiple of 4 and greater than 32.
  1.1795 +// An array of bytes the size of the pool will be passed to init()
  1.1796 +var rng_psize = 256;
  1.1797 +// Random number generator - requires a PRNG backend, e.g. prng4.js
  1.1798 +
  1.1799 +// For best results, put code like
  1.1800 +// <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'>
  1.1801 +// in your main HTML document.
  1.1802 +
  1.1803 +var rng_state;
  1.1804 +var rng_pool;
  1.1805 +var rng_pptr;
  1.1806 +
  1.1807 +// Mix in a 32-bit integer into the pool
  1.1808 +function rng_seed_int(x) {
  1.1809 +  rng_pool[rng_pptr++] ^= x & 255;
  1.1810 +  rng_pool[rng_pptr++] ^= (x >> 8) & 255;
  1.1811 +  rng_pool[rng_pptr++] ^= (x >> 16) & 255;
  1.1812 +  rng_pool[rng_pptr++] ^= (x >> 24) & 255;
  1.1813 +  if(rng_pptr >= rng_psize) rng_pptr -= rng_psize;
  1.1814 +}
  1.1815 +
  1.1816 +// Mix in the current time (w/milliseconds) into the pool
  1.1817 +function rng_seed_time() {
  1.1818 +  rng_seed_int(new Date().getTime());
  1.1819 +}
  1.1820 +
  1.1821 +// Initialize the pool with junk if needed.
  1.1822 +if(rng_pool == null) {
  1.1823 +  rng_pool = new Array();
  1.1824 +  rng_pptr = 0;
  1.1825 +  var t;
  1.1826 +  /* BEGIN LOOP */
  1.1827 +  while(rng_pptr < rng_psize) {  // extract some randomness from Math.random()
  1.1828 +    t = Math.floor(65536 * Math.random());
  1.1829 +    rng_pool[rng_pptr++] = t >>> 8;
  1.1830 +    rng_pool[rng_pptr++] = t & 255;
  1.1831 +  }
  1.1832 +  /* END LOOP */
  1.1833 +  rng_pptr = 0;
  1.1834 +  rng_seed_time();
  1.1835 +  //rng_seed_int(window.screenX);
  1.1836 +  //rng_seed_int(window.screenY);
  1.1837 +}
  1.1838 +
  1.1839 +function rng_get_byte() {
  1.1840 +  if(rng_state == null) {
  1.1841 +    rng_seed_time();
  1.1842 +    rng_state = prng_newstate();
  1.1843 +    rng_state.init(rng_pool);
  1.1844 +  /* BEGIN LOOP */
  1.1845 +    for(rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr)
  1.1846 +      rng_pool[rng_pptr] = 0;
  1.1847 +  /* END LOOP */
  1.1848 +    rng_pptr = 0;
  1.1849 +    //rng_pool = null;
  1.1850 +  }
  1.1851 +  // TODO: allow reseeding after first request
  1.1852 +  return rng_state.next();
  1.1853 +}
  1.1854 +
  1.1855 +function rng_get_bytes(ba) {
  1.1856 +  var i;
  1.1857 +  /* BEGIN LOOP */
  1.1858 +  for(i = 0; i < ba.length; ++i) ba[i] = rng_get_byte();
  1.1859 +  /* END LOOP */
  1.1860 +}
  1.1861 +
  1.1862 +function SecureRandom() {}
  1.1863 +
  1.1864 +SecureRandom.prototype.nextBytes = rng_get_bytes;
  1.1865 +// Depends on jsbn.js and rng.js
  1.1866 +
  1.1867 +// convert a (hex) string to a bignum object
  1.1868 +function parseBigInt(str,r) {
  1.1869 +  return new BigInteger(str,r);
  1.1870 +}
  1.1871 +
  1.1872 +function linebrk(s,n) {
  1.1873 +  var ret = "";
  1.1874 +  var i = 0;
  1.1875 +  /* BEGIN LOOP */
  1.1876 +  while(i + n < s.length) {
  1.1877 +    ret += s.substring(i,i+n) + "\n";
  1.1878 +    i += n;
  1.1879 +  }
  1.1880 +  /* END LOOP */
  1.1881 +  return ret + s.substring(i,s.length);
  1.1882 +}
  1.1883 +
  1.1884 +function byte2Hex(b) {
  1.1885 +  if(b < 0x10)
  1.1886 +    return "0" + b.toString(16);
  1.1887 +  else
  1.1888 +    return b.toString(16);
  1.1889 +}
  1.1890 +
  1.1891 +// PKCS#1 (type 2, random) pad input string s to n bytes, and return a bigint
  1.1892 +function pkcs1pad2(s,n) {
  1.1893 +  if(n < s.length + 11) {
  1.1894 +    alert("Message too long for RSA");
  1.1895 +    return null;
  1.1896 +  }
  1.1897 +  var ba = new Array();
  1.1898 +  var i = s.length - 1;
  1.1899 +  /* BEGIN LOOP */
  1.1900 +  while(i >= 0 && n > 0) ba[--n] = s.charCodeAt(i--);
  1.1901 +  /* END LOOP */
  1.1902 +  ba[--n] = 0;
  1.1903 +  var rng = new SecureRandom();
  1.1904 +  var x = new Array();
  1.1905 +  /* BEGIN LOOP */
  1.1906 +  while(n > 2) { // random non-zero pad
  1.1907 +    x[0] = 0;
  1.1908 +  /* BEGIN LOOP */
  1.1909 +    while(x[0] == 0) rng.nextBytes(x);
  1.1910 +  /* END LOOP */
  1.1911 +    ba[--n] = x[0];
  1.1912 +  }
  1.1913 +  /* END LOOP */
  1.1914 +  ba[--n] = 2;
  1.1915 +  ba[--n] = 0;
  1.1916 +  return new BigInteger(ba);
  1.1917 +}
  1.1918 +
  1.1919 +// "empty" RSA key constructor
  1.1920 +function RSAKey() {
  1.1921 +  this.n = null;
  1.1922 +  this.e = 0;
  1.1923 +  this.d = null;
  1.1924 +  this.p = null;
  1.1925 +  this.q = null;
  1.1926 +  this.dmp1 = null;
  1.1927 +  this.dmq1 = null;
  1.1928 +  this.coeff = null;
  1.1929 +}
  1.1930 +
  1.1931 +// Set the public key fields N and e from hex strings
  1.1932 +function RSASetPublic(N,E) {
  1.1933 +  if(N != null && E != null && N.length > 0 && E.length > 0) {
  1.1934 +    this.n = parseBigInt(N,16);
  1.1935 +    this.e = parseInt(E,16);
  1.1936 +  }
  1.1937 +  else
  1.1938 +    alert("Invalid RSA public key");
  1.1939 +}
  1.1940 +
  1.1941 +// Perform raw public operation on "x": return x^e (mod n)
  1.1942 +function RSADoPublic(x) {
  1.1943 +  return x.modPowInt(this.e, this.n);
  1.1944 +}
  1.1945 +
  1.1946 +// Return the PKCS#1 RSA encryption of "text" as an even-length hex string
  1.1947 +function RSAEncrypt(text) {
  1.1948 +  var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3);
  1.1949 +  if(m == null) return null;
  1.1950 +  var c = this.doPublic(m);
  1.1951 +  if(c == null) return null;
  1.1952 +  var h = c.toString(16);
  1.1953 +  if((h.length & 1) == 0) return h; else return "0" + h;
  1.1954 +}
  1.1955 +
  1.1956 +// Return the PKCS#1 RSA encryption of "text" as a Base64-encoded string
  1.1957 +//function RSAEncryptB64(text) {
  1.1958 +//  var h = this.encrypt(text);
  1.1959 +//  if(h) return hex2b64(h); else return null;
  1.1960 +//}
  1.1961 +
  1.1962 +// protected
  1.1963 +RSAKey.prototype.doPublic = RSADoPublic;
  1.1964 +
  1.1965 +// public
  1.1966 +RSAKey.prototype.setPublic = RSASetPublic;
  1.1967 +RSAKey.prototype.encrypt = RSAEncrypt;
  1.1968 +//RSAKey.prototype.encrypt_b64 = RSAEncryptB64;
  1.1969 +// Depends on rsa.js and jsbn2.js
  1.1970 +
  1.1971 +// Undo PKCS#1 (type 2, random) padding and, if valid, return the plaintext
  1.1972 +function pkcs1unpad2(d,n) {
  1.1973 +  var b = d.toByteArray();
  1.1974 +  var i = 0;
  1.1975 +  /* BEGIN LOOP */
  1.1976 +  while(i < b.length && b[i] == 0) ++i;
  1.1977 +  /* END LOOP */
  1.1978 +  if(b.length-i != n-1 || b[i] != 2)
  1.1979 +    return null;
  1.1980 +  ++i;
  1.1981 +  /* BEGIN LOOP */
  1.1982 +  while(b[i] != 0)
  1.1983 +    if(++i >= b.length) return null;
  1.1984 +  /* END LOOP */
  1.1985 +  var ret = "";
  1.1986 +  /* BEGIN LOOP */
  1.1987 +  while(++i < b.length)
  1.1988 +    ret += String.fromCharCode(b[i]);
  1.1989 +  /* END LOOP */
  1.1990 +  return ret;
  1.1991 +}
  1.1992 +
  1.1993 +// Set the private key fields N, e, and d from hex strings
  1.1994 +function RSASetPrivate(N,E,D) {
  1.1995 +  if(N != null && E != null && N.length > 0 && E.length > 0) {
  1.1996 +    this.n = parseBigInt(N,16);
  1.1997 +    this.e = parseInt(E,16);
  1.1998 +    this.d = parseBigInt(D,16);
  1.1999 +  }
  1.2000 +  else
  1.2001 +    alert("Invalid RSA private key");
  1.2002 +}
  1.2003 +
  1.2004 +// Set the private key fields N, e, d and CRT params from hex strings
  1.2005 +function RSASetPrivateEx(N,E,D,P,Q,DP,DQ,C) {
  1.2006 +  if(N != null && E != null && N.length > 0 && E.length > 0) {
  1.2007 +    this.n = parseBigInt(N,16);
  1.2008 +    this.e = parseInt(E,16);
  1.2009 +    this.d = parseBigInt(D,16);
  1.2010 +    this.p = parseBigInt(P,16);
  1.2011 +    this.q = parseBigInt(Q,16);
  1.2012 +    this.dmp1 = parseBigInt(DP,16);
  1.2013 +    this.dmq1 = parseBigInt(DQ,16);
  1.2014 +    this.coeff = parseBigInt(C,16);
  1.2015 +  }
  1.2016 +  else
  1.2017 +    alert("Invalid RSA private key");
  1.2018 +}
  1.2019 +
  1.2020 +// Generate a new random private key B bits long, using public expt E
  1.2021 +function RSAGenerate(B,E) {
  1.2022 +  var rng = new SecureRandom();
  1.2023 +  var qs = B>>1;
  1.2024 +  this.e = parseInt(E,16);
  1.2025 +  var ee = new BigInteger(E,16);
  1.2026 +  /* BEGIN LOOP */
  1.2027 +  for(;;) {
  1.2028 +  /* BEGIN LOOP */
  1.2029 +    for(;;) {
  1.2030 +      this.p = new BigInteger(B-qs,1,rng);
  1.2031 +      if(this.p.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.p.isProbablePrime(10)) break;
  1.2032 +    }
  1.2033 +  /* END LOOP */
  1.2034 +  /* BEGIN LOOP */
  1.2035 +    for(;;) {
  1.2036 +      this.q = new BigInteger(qs,1,rng);
  1.2037 +      if(this.q.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.q.isProbablePrime(10)) break;
  1.2038 +    }
  1.2039 +  /* END LOOP */
  1.2040 +    if(this.p.compareTo(this.q) <= 0) {
  1.2041 +      var t = this.p;
  1.2042 +      this.p = this.q;
  1.2043 +      this.q = t;
  1.2044 +    }
  1.2045 +    var p1 = this.p.subtract(BigInteger.ONE);
  1.2046 +    var q1 = this.q.subtract(BigInteger.ONE);
  1.2047 +    var phi = p1.multiply(q1);
  1.2048 +    if(phi.gcd(ee).compareTo(BigInteger.ONE) == 0) {
  1.2049 +      this.n = this.p.multiply(this.q);
  1.2050 +      this.d = ee.modInverse(phi);
  1.2051 +      this.dmp1 = this.d.mod(p1);
  1.2052 +      this.dmq1 = this.d.mod(q1);
  1.2053 +      this.coeff = this.q.modInverse(this.p);
  1.2054 +      break;
  1.2055 +    }
  1.2056 +  }
  1.2057 +  /* END LOOP */
  1.2058 +}
  1.2059 +
  1.2060 +// Perform raw private operation on "x": return x^d (mod n)
  1.2061 +function RSADoPrivate(x) {
  1.2062 +  if(this.p == null || this.q == null)
  1.2063 +    return x.modPow(this.d, this.n);
  1.2064 +
  1.2065 +  // TODO: re-calculate any missing CRT params
  1.2066 +  var xp = x.mod(this.p).modPow(this.dmp1, this.p);
  1.2067 +  var xq = x.mod(this.q).modPow(this.dmq1, this.q);
  1.2068 +
  1.2069 +  /* BEGIN LOOP */
  1.2070 +  while(xp.compareTo(xq) < 0)
  1.2071 +    xp = xp.add(this.p);
  1.2072 +  /* END LOOP */
  1.2073 +  return xp.subtract(xq).multiply(this.coeff).mod(this.p).multiply(this.q).add(xq);
  1.2074 +}
  1.2075 +
  1.2076 +// Return the PKCS#1 RSA decryption of "ctext".
  1.2077 +// "ctext" is an even-length hex string and the output is a plain string.
  1.2078 +function RSADecrypt(ctext) {
  1.2079 +  var c = parseBigInt(ctext, 16);
  1.2080 +  var m = this.doPrivate(c);
  1.2081 +  if(m == null) return null;
  1.2082 +  return pkcs1unpad2(m, (this.n.bitLength()+7)>>3);
  1.2083 +}
  1.2084 +
  1.2085 +// Return the PKCS#1 RSA decryption of "ctext".
  1.2086 +// "ctext" is a Base64-encoded string and the output is a plain string.
  1.2087 +//function RSAB64Decrypt(ctext) {
  1.2088 +//  var h = b64tohex(ctext);
  1.2089 +//  if(h) return this.decrypt(h); else return null;
  1.2090 +//}
  1.2091 +
  1.2092 +// protected
  1.2093 +RSAKey.prototype.doPrivate = RSADoPrivate;
  1.2094 +
  1.2095 +// public
  1.2096 +RSAKey.prototype.setPrivate = RSASetPrivate;
  1.2097 +RSAKey.prototype.setPrivateEx = RSASetPrivateEx;
  1.2098 +RSAKey.prototype.generate = RSAGenerate;
  1.2099 +RSAKey.prototype.decrypt = RSADecrypt;
  1.2100 +//RSAKey.prototype.b64_decrypt = RSAB64Decrypt;
  1.2101 +
  1.2102 +
  1.2103 +nValue="a5261939975948bb7a58dffe5ff54e65f0498f9175f5a09288810b8975871e99af3b5dd94057b0fc07535f5f97444504fa35169d461d0d30cf0192e307727c065168c788771c561a9400fb49175e9e6aa4e23fe11af69e9412dd23b0cb6684c4c2429bce139e848ab26d0829073351f4acd36074eafd036a5eb83359d2a698d3";
  1.2104 +eValue="10001";
  1.2105 +dValue="8e9912f6d3645894e8d38cb58c0db81ff516cf4c7e5a14c7f1eddb1459d2cded4d8d293fc97aee6aefb861859c8b6a3d1dfe710463e1f9ddc72048c09751971c4a580aa51eb523357a3cc48d31cfad1d4a165066ed92d4748fb6571211da5cb14bc11b6e2df7c1a559e6d5ac1cd5c94703a22891464fba23d0d965086277a161";
  1.2106 +pValue="d090ce58a92c75233a6486cb0a9209bf3583b64f540c76f5294bb97d285eed33aec220bde14b2417951178ac152ceab6da7090905b478195498b352048f15e7d";
  1.2107 +qValue="cab575dc652bb66df15a0359609d51d1db184750c00c6698b90ef3465c99655103edbf0d54c56aec0ce3c4d22592338092a126a0cc49f65a4a30d222b411e58f";
  1.2108 +dmp1Value="1a24bca8e273df2f0e47c199bbf678604e7df7215480c77c8db39f49b000ce2cf7500038acfff5433b7d582a01f1826e6f4d42e1c57f5e1fef7b12aabc59fd25";
  1.2109 +dmq1Value="3d06982efbbe47339e1f6d36b1216b8a741d410b0c662f54f7118b27b9a4ec9d914337eb39841d8666f3034408cf94f5b62f11c402fc994fe15a05493150d9fd";
  1.2110 +coeffValue="3a3e731acd8960b7ff9eb81a7ff93bd1cfa74cbd56987db58b4594fb09c09084db1734c8143f98b602b981aaa9243ca28deb69b5b280ee8dcee0fd2625e53250";
  1.2111 +
  1.2112 +setupEngine(am3, 28);
  1.2113 +
  1.2114 +var RSA = new RSAKey();
  1.2115 +var TEXT = "The quick brown fox jumped over the extremely lazy frogs!";
  1.2116 +
  1.2117 +RSA.setPublic(nValue, eValue);
  1.2118 +RSA.setPrivateEx(nValue, eValue, dValue, pValue, qValue, dmp1Value, dmq1Value, coeffValue);
  1.2119 +
  1.2120 +function encrypt() {
  1.2121 +  return RSA.encrypt(TEXT);
  1.2122 +}
  1.2123 +
  1.2124 +function decrypt() {
  1.2125 +  return RSA.decrypt(TEXT);
  1.2126 +}
  1.2127 +
  1.2128 +function PrintResult(name, result) {
  1.2129 +}
  1.2130 +
  1.2131 +
  1.2132 +function PrintScore(score) {
  1.2133 +//  print(score);
  1.2134 +}
  1.2135 +
  1.2136 +
  1.2137 +BenchmarkSuite.RunSuites({ NotifyResult: PrintResult,
  1.2138 +                           NotifyScore: PrintScore });

mercurial