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