aboutsummaryrefslogtreecommitdiff
path: root/bip39-standalone.html
diff options
context:
space:
mode:
authorIan Coleman <coleman.ian@gmail.com>2016-11-03 16:34:56 +1100
committerIan Coleman <coleman.ian@gmail.com>2016-11-04 15:14:13 +1100
commitc6624d51f4e5607202e48903352574c47571baab (patch)
treeee57075575c8f9b88e13eb7efff083bb9fa2b39c /bip39-standalone.html
parentd737abf6809622228faf7d5fe54101e2d87d72a4 (diff)
downloadBIP39-c6624d51f4e5607202e48903352574c47571baab.tar.gz
BIP39-c6624d51f4e5607202e48903352574c47571baab.tar.zst
BIP39-c6624d51f4e5607202e48903352574c47571baab.zip
Entropy can be supplied by user
Diffstat (limited to 'bip39-standalone.html')
-rw-r--r--bip39-standalone.html1936
1 files changed, 1929 insertions, 7 deletions
diff --git a/bip39-standalone.html b/bip39-standalone.html
index 5993b86..e3af3a6 100644
--- a/bip39-standalone.html
+++ b/bip39-standalone.html
@@ -69,12 +69,14 @@
69 <div class="col-md-12"> 69 <div class="col-md-12">
70 <h2>Mnemonic</h2> 70 <h2>Mnemonic</h2>
71 <form class="form-horizontal" role="form"> 71 <form class="form-horizontal" role="form">
72 <div class="col-sm-2"></div>
73 <div class="col-sm-10">
74 <p>You can enter an existing BIP39 mnemonic, or generate a new random one. Typing your own twelve words will probably not work how you expect, since the words require a particular structure (the last word is a checksum)</p>
75 <p>For more info see the <a href="https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki" target="_blank">BIP39 spec</a></p>
76 </div>
77 <div class="form-group"> 72 <div class="form-group">
73 <div class="col-sm-2"></div>
74 <div class="col-sm-10">
75 <p>You can enter an existing BIP39 mnemonic, or generate a new random one. Typing your own twelve words will probably not work how you expect, since the words require a particular structure (the last word is a checksum)</p>
76 <p>For more info see the <a href="https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki" target="_blank">BIP39 spec</a></p>
77 </div>
78 </div>
79 <div class="form-group generate-container">
78 <label class="col-sm-2 control-label"></label> 80 <label class="col-sm-2 control-label"></label>
79 <div class="col-sm-10"> 81 <div class="col-sm-10">
80 <div class="form-inline"> 82 <div class="form-inline">
@@ -96,7 +98,30 @@
96 </div> 98 </div>
97 </div> 99 </div>
98 </div> 100 </div>
99 <div class="form-group"> 101 <div class="entropy-container hidden">
102 <label for="entropy" class="col-sm-2 control-label">Entropy</label>
103 <div class="col-sm-10">
104 <input id="entropy" class="entropy form-control" placeholder="Accepts binary, base 6, 6-sided dice, base 10, hexadecimal">
105 <span class="help-block">
106 <div class="text-danger">
107 This is an advanced feature.
108 Your mnemonic may be insecure if this feature is used incorrectly.
109 <a href="#entropy-notes">Read more</a>
110 </div>
111 <div class="text-danger entropy-error"></div>
112 </span>
113 </div>
114 </div>
115 <div class="form-group">
116 <div class="col-sm-2"></div>
117 <div class="col-sm-10 checkbox">
118 <label>
119 <input type="checkbox" class="use-entropy">
120 Supply my own source of entropy
121 </label>
122 </div>
123 </div>
124 <div class="form-group">
100 <label class="col-sm-2 control-label"></label> 125 <label class="col-sm-2 control-label"></label>
101 <div class="col-sm-10 languages"> 126 <div class="col-sm-10 languages">
102 <a href="#english">English</a> 127 <a href="#english">English</a>
@@ -357,6 +382,24 @@
357 but be careful - it can be easy to make mistakes if you 382 but be careful - it can be easy to make mistakes if you
358 don't know what you're doing 383 don't know what you're doing
359 </p> 384 </p>
385 <h3 id="entropy-notes">Entropy</h3>
386 <p>
387 Entropy values must be sourced from a
388 <a href="https://en.wikipedia.org/wiki/Random_number_generation" target="_blank">strong source of randomness</a>.
389 This means flipping a fair coin, rolling a fair dice, noise measurements etc. Do <strong>NOT</strong> use
390 phrases from books, lyrics from songs, your birthday or steet address, keyboard mashing, or anything you <i>think</i>
391 is random, because chances are <em>overwhelming</em> that it isn't random enough for the needs of this tool.
392 </p>
393 <p>
394 The random mnemonic generator on this page uses a
395 <a href="https://developer.mozilla.org/en-US/docs/Web/API/RandomSource/getRandomValues" target="_blank">cryptographically secure random number generator</a>,
396 and can generally be trusted more than your own intuition about randomness.
397 If cryptographic randomness isn't available in your browser, this page will show a warning and <i>will not generate
398 random mnemonics</i>.
399 </p>
400 <p>
401 <a href="https://bitcointalk.org/index.php?topic=311000.msg3345309#msg3345309" target="_blank">You are not a good source of entropy.</a>
402 </p>
360 </div> 403 </div>
361 </div> 404 </div>
362 405
@@ -16169,6 +16212,1781 @@ var Mnemonic = function(language) {
16169 16212
16170} 16213}
16171</script> 16214</script>
16215 <script>window.Entropy = new (function() {
16216
16217 var matchers = {
16218 binary: /[0-1]/gi,
16219 base6: /[0-5]/gi,
16220 dice: /[1-6]/gi, // ie dice numbers
16221 base10: /[0-9]/gi,
16222 hex: /[0-9A-F]/gi,
16223 }
16224
16225 this.fromString = function(rawEntropyStr) {
16226 // Find type of entropy being used (binary, hex, dice etc)
16227 var base = getBase(rawEntropyStr);
16228 // Convert dice to base6 entropy (ie 1-6 to 0-5)
16229 if (base.str == "dice") {
16230 var newRawEntropyStr = "";
16231 for (var i=0; i<rawEntropyStr.length; i++) {
16232 var c = rawEntropyStr[i];
16233 if ("123456".indexOf(c) > -1) {
16234 newRawEntropyStr += (parseInt(c) - 1).toString();
16235 }
16236 else {
16237 newRawEntropyStr += c
16238 }
16239 }
16240 rawEntropyStr = newRawEntropyStr;
16241 base.str = "base 6 (dice)";
16242 base.matcher = matchers.base6;
16243 }
16244 var entropyParts = rawEntropyStr.match(base.matcher) || [];
16245 var entropyStr = entropyParts.join("");
16246 // Detect empty entropy
16247 if (entropyStr.length == 0) {
16248 return {
16249 binaryStr: "",
16250 hexStr: "",
16251 cleanStr: "",
16252 base: base,
16253 };
16254 }
16255 // Pull leading zeros off
16256 var leadingZeros = "";
16257 while (entropyStr[0] == "0") {
16258 leadingZeros += "0";
16259 entropyStr = entropyStr.substring(1);
16260 }
16261 // Convert leading zeros to binary equivalent
16262 var numBinLeadingZeros = Math.ceil(Math.log2(base.asInt) * leadingZeros.length);
16263 var binLeadingZeros = "";
16264 for (var i=0; i<numBinLeadingZeros; i++) {
16265 binLeadingZeros += "0";
16266 }
16267 // Convert leading zeros to hex equivalent
16268 var numHexLeadingZeros = Math.floor(numBinLeadingZeros / 4);
16269 var hexLeadingZeros = "";
16270 for (var i=0; i<numHexLeadingZeros; i++) {
16271 hexLeadingZeros += "0";
16272 }
16273 // Handle entropy of zero
16274 if (entropyStr == "") {
16275 return {
16276 binaryStr: binLeadingZeros,
16277 hexStr: hexLeadingZeros || "0",
16278 cleanStr: leadingZeros,
16279 base: base,
16280 }
16281 }
16282 // If using hex, should always be multiples of 4 bits, which can get
16283 // out of sync if first number has leading 0 bits, eg 2 in hex is 0010
16284 // which would show up as 10, thus missing 2 bits it should have.
16285 if (base.asInt == 16) {
16286 var firstDigit = parseInt(entropyStr[0], 16);
16287 if (firstDigit >= 4 && firstDigit < 8) {
16288 binLeadingZeros += "0";
16289 }
16290 else if (firstDigit >= 2 && firstDigit < 4) {
16291 binLeadingZeros += "00";
16292 }
16293 else if (firstDigit >= 1 && firstDigit < 2) {
16294 binLeadingZeros += "000";
16295 }
16296 }
16297 // Convert entropy to different foramts
16298 var entropyInt = BigInteger.parse(entropyStr, base.asInt);
16299 var entropyBin = binLeadingZeros + entropyInt.toString(2);
16300 var entropyHex = hexLeadingZeros + entropyInt.toString(16);
16301 var entropyClean = leadingZeros + entropyStr;
16302 var e = {
16303 binaryStr: entropyBin,
16304 hexStr: entropyHex,
16305 cleanStr: entropyClean,
16306 base: base,
16307 }
16308 return e;
16309 }
16310
16311 function getBase(str) {
16312 // Need to get the lowest base for the supplied entropy.
16313 // This prevents interpreting, say, dice rolls as hexadecimal.
16314 var binaryMatches = str.match(matchers.binary) || [];
16315 var base6Matches = str.match(matchers.base6) || [];
16316 var diceMatches = str.match(matchers.dice) || [];
16317 var base10Matches = str.match(matchers.base10) || [];
16318 var hexMatches = str.match(matchers.hex) || [];
16319 // Find the lowest base that can be used, whilst ignoring any irrelevant chars
16320 if (binaryMatches.length == hexMatches.length) {
16321 return {
16322 matcher: matchers.binary,
16323 asInt: 2,
16324 str: "binary",
16325 }
16326 }
16327 if (diceMatches.length == hexMatches.length) {
16328 return {
16329 matcher: matchers.dice,
16330 asInt: 6,
16331 str: "dice",
16332 }
16333 }
16334 if (base6Matches.length == hexMatches.length) {
16335 return {
16336 matcher: matchers.base6,
16337 asInt: 6,
16338 str: "base 6",
16339 }
16340 }
16341 if (base10Matches.length == hexMatches.length) {
16342 return {
16343 matcher: matchers.base10,
16344 asInt: 10,
16345 str: "base 10",
16346 }
16347 }
16348 return {
16349 matcher: matchers.hex,
16350 asInt: 16,
16351 str: "hexadecimal",
16352 }
16353 }
16354
16355 // Polyfill for Math.log2
16356 // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log2#Polyfill
16357 Math.log2 = Math.log2 || function(x) {
16358 return Math.log(x) * Math.LOG2E;
16359 };
16360
16361})();
16362
16363
16364// BigInteger library included here because
16365// only the entropy library depends on it
16366// so if entropy detection is removed so is the dependency
16367
16368
16369/*
16370 JavaScript BigInteger library version 0.9.1
16371 http://silentmatt.com/biginteger/
16372
16373 Copyright (c) 2009 Matthew Crumley <email@matthewcrumley.com>
16374 Copyright (c) 2010,2011 by John Tobey <John.Tobey@gmail.com>
16375 Licensed under the MIT license.
16376
16377 Support for arbitrary internal representation base was added by
16378 Vitaly Magerya.
16379*/
16380
16381/*
16382 File: biginteger.js
16383
16384 Exports:
16385
16386 <BigInteger>
16387*/
16388(function(exports) {
16389"use strict";
16390/*
16391 Class: BigInteger
16392 An arbitrarily-large integer.
16393
16394 <BigInteger> objects should be considered immutable. None of the "built-in"
16395 methods modify *this* or their arguments. All properties should be
16396 considered private.
16397
16398 All the methods of <BigInteger> instances can be called "statically". The
16399 static versions are convenient if you don't already have a <BigInteger>
16400 object.
16401
16402 As an example, these calls are equivalent.
16403
16404 > BigInteger(4).multiply(5); // returns BigInteger(20);
16405 > BigInteger.multiply(4, 5); // returns BigInteger(20);
16406
16407 > var a = 42;
16408 > var a = BigInteger.toJSValue("0b101010"); // Not completely useless...
16409*/
16410
16411var CONSTRUCT = {}; // Unique token to call "private" version of constructor
16412
16413/*
16414 Constructor: BigInteger()
16415 Convert a value to a <BigInteger>.
16416
16417 Although <BigInteger()> is the constructor for <BigInteger> objects, it is
16418 best not to call it as a constructor. If *n* is a <BigInteger> object, it is
16419 simply returned as-is. Otherwise, <BigInteger()> is equivalent to <parse>
16420 without a radix argument.
16421
16422 > var n0 = BigInteger(); // Same as <BigInteger.ZERO>
16423 > var n1 = BigInteger("123"); // Create a new <BigInteger> with value 123
16424 > var n2 = BigInteger(123); // Create a new <BigInteger> with value 123
16425 > var n3 = BigInteger(n2); // Return n2, unchanged
16426
16427 The constructor form only takes an array and a sign. *n* must be an
16428 array of numbers in little-endian order, where each digit is between 0
16429 and BigInteger.base. The second parameter sets the sign: -1 for
16430 negative, +1 for positive, or 0 for zero. The array is *not copied and
16431 may be modified*. If the array contains only zeros, the sign parameter
16432 is ignored and is forced to zero.
16433
16434 > new BigInteger([5], -1): create a new BigInteger with value -5
16435
16436 Parameters:
16437
16438 n - Value to convert to a <BigInteger>.
16439
16440 Returns:
16441
16442 A <BigInteger> value.
16443
16444 See Also:
16445
16446 <parse>, <BigInteger>
16447*/
16448function BigInteger(n, s, token) {
16449 if (token !== CONSTRUCT) {
16450 if (n instanceof BigInteger) {
16451 return n;
16452 }
16453 else if (typeof n === "undefined") {
16454 return ZERO;
16455 }
16456 return BigInteger.parse(n);
16457 }
16458
16459 n = n || []; // Provide the nullary constructor for subclasses.
16460 while (n.length && !n[n.length - 1]) {
16461 --n.length;
16462 }
16463 this._d = n;
16464 this._s = n.length ? (s || 1) : 0;
16465}
16466
16467BigInteger._construct = function(n, s) {
16468 return new BigInteger(n, s, CONSTRUCT);
16469};
16470
16471// Base-10 speedup hacks in parse, toString, exp10 and log functions
16472// require base to be a power of 10. 10^7 is the largest such power
16473// that won't cause a precision loss when digits are multiplied.
16474var BigInteger_base = 10000000;
16475var BigInteger_base_log10 = 7;
16476
16477BigInteger.base = BigInteger_base;
16478BigInteger.base_log10 = BigInteger_base_log10;
16479
16480var ZERO = new BigInteger([], 0, CONSTRUCT);
16481// Constant: ZERO
16482// <BigInteger> 0.
16483BigInteger.ZERO = ZERO;
16484
16485var ONE = new BigInteger([1], 1, CONSTRUCT);
16486// Constant: ONE
16487// <BigInteger> 1.
16488BigInteger.ONE = ONE;
16489
16490var M_ONE = new BigInteger(ONE._d, -1, CONSTRUCT);
16491// Constant: M_ONE
16492// <BigInteger> -1.
16493BigInteger.M_ONE = M_ONE;
16494
16495// Constant: _0
16496// Shortcut for <ZERO>.
16497BigInteger._0 = ZERO;
16498
16499// Constant: _1
16500// Shortcut for <ONE>.
16501BigInteger._1 = ONE;
16502
16503/*
16504 Constant: small
16505 Array of <BigIntegers> from 0 to 36.
16506
16507 These are used internally for parsing, but useful when you need a "small"
16508 <BigInteger>.
16509
16510 See Also:
16511
16512 <ZERO>, <ONE>, <_0>, <_1>
16513*/
16514BigInteger.small = [
16515 ZERO,
16516 ONE,
16517 /* Assuming BigInteger_base > 36 */
16518 new BigInteger( [2], 1, CONSTRUCT),
16519 new BigInteger( [3], 1, CONSTRUCT),
16520 new BigInteger( [4], 1, CONSTRUCT),
16521 new BigInteger( [5], 1, CONSTRUCT),
16522 new BigInteger( [6], 1, CONSTRUCT),
16523 new BigInteger( [7], 1, CONSTRUCT),
16524 new BigInteger( [8], 1, CONSTRUCT),
16525 new BigInteger( [9], 1, CONSTRUCT),
16526 new BigInteger([10], 1, CONSTRUCT),
16527 new BigInteger([11], 1, CONSTRUCT),
16528 new BigInteger([12], 1, CONSTRUCT),
16529 new BigInteger([13], 1, CONSTRUCT),
16530 new BigInteger([14], 1, CONSTRUCT),
16531 new BigInteger([15], 1, CONSTRUCT),
16532 new BigInteger([16], 1, CONSTRUCT),
16533 new BigInteger([17], 1, CONSTRUCT),
16534 new BigInteger([18], 1, CONSTRUCT),
16535 new BigInteger([19], 1, CONSTRUCT),
16536 new BigInteger([20], 1, CONSTRUCT),
16537 new BigInteger([21], 1, CONSTRUCT),
16538 new BigInteger([22], 1, CONSTRUCT),
16539 new BigInteger([23], 1, CONSTRUCT),
16540 new BigInteger([24], 1, CONSTRUCT),
16541 new BigInteger([25], 1, CONSTRUCT),
16542 new BigInteger([26], 1, CONSTRUCT),
16543 new BigInteger([27], 1, CONSTRUCT),
16544 new BigInteger([28], 1, CONSTRUCT),
16545 new BigInteger([29], 1, CONSTRUCT),
16546 new BigInteger([30], 1, CONSTRUCT),
16547 new BigInteger([31], 1, CONSTRUCT),
16548 new BigInteger([32], 1, CONSTRUCT),
16549 new BigInteger([33], 1, CONSTRUCT),
16550 new BigInteger([34], 1, CONSTRUCT),
16551 new BigInteger([35], 1, CONSTRUCT),
16552 new BigInteger([36], 1, CONSTRUCT)
16553];
16554
16555// Used for parsing/radix conversion
16556BigInteger.digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
16557
16558/*
16559 Method: toString
16560 Convert a <BigInteger> to a string.
16561
16562 When *base* is greater than 10, letters are upper case.
16563
16564 Parameters:
16565
16566 base - Optional base to represent the number in (default is base 10).
16567 Must be between 2 and 36 inclusive, or an Error will be thrown.
16568
16569 Returns:
16570
16571 The string representation of the <BigInteger>.
16572*/
16573BigInteger.prototype.toString = function(base) {
16574 base = +base || 10;
16575 if (base < 2 || base > 36) {
16576 throw new Error("illegal radix " + base + ".");
16577 }
16578 if (this._s === 0) {
16579 return "0";
16580 }
16581 if (base === 10) {
16582 var str = this._s < 0 ? "-" : "";
16583 str += this._d[this._d.length - 1].toString();
16584 for (var i = this._d.length - 2; i >= 0; i--) {
16585 var group = this._d[i].toString();
16586 while (group.length < BigInteger_base_log10) group = '0' + group;
16587 str += group;
16588 }
16589 return str;
16590 }
16591 else {
16592 var numerals = BigInteger.digits;
16593 base = BigInteger.small[base];
16594 var sign = this._s;
16595
16596 var n = this.abs();
16597 var digits = [];
16598 var digit;
16599
16600 while (n._s !== 0) {
16601 var divmod = n.divRem(base);
16602 n = divmod[0];
16603 digit = divmod[1];
16604 // TODO: This could be changed to unshift instead of reversing at the end.
16605 // Benchmark both to compare speeds.
16606 digits.push(numerals[digit.valueOf()]);
16607 }
16608 return (sign < 0 ? "-" : "") + digits.reverse().join("");
16609 }
16610};
16611
16612// Verify strings for parsing
16613BigInteger.radixRegex = [
16614 /^$/,
16615 /^$/,
16616 /^[01]*$/,
16617 /^[012]*$/,
16618 /^[0-3]*$/,
16619 /^[0-4]*$/,
16620 /^[0-5]*$/,
16621 /^[0-6]*$/,
16622 /^[0-7]*$/,
16623 /^[0-8]*$/,
16624 /^[0-9]*$/,
16625 /^[0-9aA]*$/,
16626 /^[0-9abAB]*$/,
16627 /^[0-9abcABC]*$/,
16628 /^[0-9a-dA-D]*$/,
16629 /^[0-9a-eA-E]*$/,
16630 /^[0-9a-fA-F]*$/,
16631 /^[0-9a-gA-G]*$/,
16632 /^[0-9a-hA-H]*$/,
16633 /^[0-9a-iA-I]*$/,
16634 /^[0-9a-jA-J]*$/,
16635 /^[0-9a-kA-K]*$/,
16636 /^[0-9a-lA-L]*$/,
16637 /^[0-9a-mA-M]*$/,
16638 /^[0-9a-nA-N]*$/,
16639 /^[0-9a-oA-O]*$/,
16640 /^[0-9a-pA-P]*$/,
16641 /^[0-9a-qA-Q]*$/,
16642 /^[0-9a-rA-R]*$/,
16643 /^[0-9a-sA-S]*$/,
16644 /^[0-9a-tA-T]*$/,
16645 /^[0-9a-uA-U]*$/,
16646 /^[0-9a-vA-V]*$/,
16647 /^[0-9a-wA-W]*$/,
16648 /^[0-9a-xA-X]*$/,
16649 /^[0-9a-yA-Y]*$/,
16650 /^[0-9a-zA-Z]*$/
16651];
16652
16653/*
16654 Function: parse
16655 Parse a string into a <BigInteger>.
16656
16657 *base* is optional but, if provided, must be from 2 to 36 inclusive. If
16658 *base* is not provided, it will be guessed based on the leading characters
16659 of *s* as follows:
16660
16661 - "0x" or "0X": *base* = 16
16662 - "0c" or "0C": *base* = 8
16663 - "0b" or "0B": *base* = 2
16664 - else: *base* = 10
16665
16666 If no base is provided, or *base* is 10, the number can be in exponential
16667 form. For example, these are all valid:
16668
16669 > BigInteger.parse("1e9"); // Same as "1000000000"
16670 > BigInteger.parse("1.234*10^3"); // Same as 1234
16671 > BigInteger.parse("56789 * 10 ** -2"); // Same as 567
16672
16673 If any characters fall outside the range defined by the radix, an exception
16674 will be thrown.
16675
16676 Parameters:
16677
16678 s - The string to parse.
16679 base - Optional radix (default is to guess based on *s*).
16680
16681 Returns:
16682
16683 a <BigInteger> instance.
16684*/
16685BigInteger.parse = function(s, base) {
16686 // Expands a number in exponential form to decimal form.
16687 // expandExponential("-13.441*10^5") === "1344100";
16688 // expandExponential("1.12300e-1") === "0.112300";
16689 // expandExponential(1000000000000000000000000000000) === "1000000000000000000000000000000";
16690 function expandExponential(str) {
16691 str = str.replace(/\s*[*xX]\s*10\s*(\^|\*\*)\s*/, "e");
16692
16693 return str.replace(/^([+\-])?(\d+)\.?(\d*)[eE]([+\-]?\d+)$/, function(x, s, n, f, c) {
16694 c = +c;
16695 var l = c < 0;
16696 var i = n.length + c;
16697 x = (l ? n : f).length;
16698 c = ((c = Math.abs(c)) >= x ? c - x + l : 0);
16699 var z = (new Array(c + 1)).join("0");
16700 var r = n + f;
16701 return (s || "") + (l ? r = z + r : r += z).substr(0, i += l ? z.length : 0) + (i < r.length ? "." + r.substr(i) : "");
16702 });
16703 }
16704
16705 s = s.toString();
16706 if (typeof base === "undefined" || +base === 10) {
16707 s = expandExponential(s);
16708 }
16709
16710 var prefixRE;
16711 if (typeof base === "undefined") {
16712 prefixRE = '0[xcb]';
16713 }
16714 else if (base == 16) {
16715 prefixRE = '0x';
16716 }
16717 else if (base == 8) {
16718 prefixRE = '0c';
16719 }
16720 else if (base == 2) {
16721 prefixRE = '0b';
16722 }
16723 else {
16724 prefixRE = '';
16725 }
16726 var parts = new RegExp('^([+\\-]?)(' + prefixRE + ')?([0-9a-z]*)(?:\\.\\d*)?$', 'i').exec(s);
16727 if (parts) {
16728 var sign = parts[1] || "+";
16729 var baseSection = parts[2] || "";
16730 var digits = parts[3] || "";
16731
16732 if (typeof base === "undefined") {
16733 // Guess base
16734 if (baseSection === "0x" || baseSection === "0X") { // Hex
16735 base = 16;
16736 }
16737 else if (baseSection === "0c" || baseSection === "0C") { // Octal
16738 base = 8;
16739 }
16740 else if (baseSection === "0b" || baseSection === "0B") { // Binary
16741 base = 2;
16742 }
16743 else {
16744 base = 10;
16745 }
16746 }
16747 else if (base < 2 || base > 36) {
16748 throw new Error("Illegal radix " + base + ".");
16749 }
16750
16751 base = +base;
16752
16753 // Check for digits outside the range
16754 if (!(BigInteger.radixRegex[base].test(digits))) {
16755 throw new Error("Bad digit for radix " + base);
16756 }
16757
16758 // Strip leading zeros, and convert to array
16759 digits = digits.replace(/^0+/, "").split("");
16760 if (digits.length === 0) {
16761 return ZERO;
16762 }
16763
16764 // Get the sign (we know it's not zero)
16765 sign = (sign === "-") ? -1 : 1;
16766
16767 // Optimize 10
16768 if (base == 10) {
16769 var d = [];
16770 while (digits.length >= BigInteger_base_log10) {
16771 d.push(parseInt(digits.splice(digits.length-BigInteger.base_log10, BigInteger.base_log10).join(''), 10));
16772 }
16773 d.push(parseInt(digits.join(''), 10));
16774 return new BigInteger(d, sign, CONSTRUCT);
16775 }
16776
16777 // Do the conversion
16778 var d = ZERO;
16779 base = BigInteger.small[base];
16780 var small = BigInteger.small;
16781 for (var i = 0; i < digits.length; i++) {
16782 d = d.multiply(base).add(small[parseInt(digits[i], 36)]);
16783 }
16784 return new BigInteger(d._d, sign, CONSTRUCT);
16785 }
16786 else {
16787 throw new Error("Invalid BigInteger format: " + s);
16788 }
16789};
16790
16791/*
16792 Function: add
16793 Add two <BigIntegers>.
16794
16795 Parameters:
16796
16797 n - The number to add to *this*. Will be converted to a <BigInteger>.
16798
16799 Returns:
16800
16801 The numbers added together.
16802
16803 See Also:
16804
16805 <subtract>, <multiply>, <quotient>, <next>
16806*/
16807BigInteger.prototype.add = function(n) {
16808 if (this._s === 0) {
16809 return BigInteger(n);
16810 }
16811
16812 n = BigInteger(n);
16813 if (n._s === 0) {
16814 return this;
16815 }
16816 if (this._s !== n._s) {
16817 n = n.negate();
16818 return this.subtract(n);
16819 }
16820
16821 var a = this._d;
16822 var b = n._d;
16823 var al = a.length;
16824 var bl = b.length;
16825 var sum = new Array(Math.max(al, bl) + 1);
16826 var size = Math.min(al, bl);
16827 var carry = 0;
16828 var digit;
16829
16830 for (var i = 0; i < size; i++) {
16831 digit = a[i] + b[i] + carry;
16832 sum[i] = digit % BigInteger_base;
16833 carry = (digit / BigInteger_base) | 0;
16834 }
16835 if (bl > al) {
16836 a = b;
16837 al = bl;
16838 }
16839 for (i = size; carry && i < al; i++) {
16840 digit = a[i] + carry;
16841 sum[i] = digit % BigInteger_base;
16842 carry = (digit / BigInteger_base) | 0;
16843 }
16844 if (carry) {
16845 sum[i] = carry;
16846 }
16847
16848 for ( ; i < al; i++) {
16849 sum[i] = a[i];
16850 }
16851
16852 return new BigInteger(sum, this._s, CONSTRUCT);
16853};
16854
16855/*
16856 Function: negate
16857 Get the additive inverse of a <BigInteger>.
16858
16859 Returns:
16860
16861 A <BigInteger> with the same magnatude, but with the opposite sign.
16862
16863 See Also:
16864
16865 <abs>
16866*/
16867BigInteger.prototype.negate = function() {
16868 return new BigInteger(this._d, (-this._s) | 0, CONSTRUCT);
16869};
16870
16871/*
16872 Function: abs
16873 Get the absolute value of a <BigInteger>.
16874
16875 Returns:
16876
16877 A <BigInteger> with the same magnatude, but always positive (or zero).
16878
16879 See Also:
16880
16881 <negate>
16882*/
16883BigInteger.prototype.abs = function() {
16884 return (this._s < 0) ? this.negate() : this;
16885};
16886
16887/*
16888 Function: subtract
16889 Subtract two <BigIntegers>.
16890
16891 Parameters:
16892
16893 n - The number to subtract from *this*. Will be converted to a <BigInteger>.
16894
16895 Returns:
16896
16897 The *n* subtracted from *this*.
16898
16899 See Also:
16900
16901 <add>, <multiply>, <quotient>, <prev>
16902*/
16903BigInteger.prototype.subtract = function(n) {
16904 if (this._s === 0) {
16905 return BigInteger(n).negate();
16906 }
16907
16908 n = BigInteger(n);
16909 if (n._s === 0) {
16910 return this;
16911 }
16912 if (this._s !== n._s) {
16913 n = n.negate();
16914 return this.add(n);
16915 }
16916
16917 var m = this;
16918 // negative - negative => -|a| - -|b| => -|a| + |b| => |b| - |a|
16919 if (this._s < 0) {
16920 m = new BigInteger(n._d, 1, CONSTRUCT);
16921 n = new BigInteger(this._d, 1, CONSTRUCT);
16922 }
16923
16924 // Both are positive => a - b
16925 var sign = m.compareAbs(n);
16926 if (sign === 0) {
16927 return ZERO;
16928 }
16929 else if (sign < 0) {
16930 // swap m and n
16931 var t = n;
16932 n = m;
16933 m = t;
16934 }
16935
16936 // a > b
16937 var a = m._d;
16938 var b = n._d;
16939 var al = a.length;
16940 var bl = b.length;
16941 var diff = new Array(al); // al >= bl since a > b
16942 var borrow = 0;
16943 var i;
16944 var digit;
16945
16946 for (i = 0; i < bl; i++) {
16947 digit = a[i] - borrow - b[i];
16948 if (digit < 0) {
16949 digit += BigInteger_base;
16950 borrow = 1;
16951 }
16952 else {
16953 borrow = 0;
16954 }
16955 diff[i] = digit;
16956 }
16957 for (i = bl; i < al; i++) {
16958 digit = a[i] - borrow;
16959 if (digit < 0) {
16960 digit += BigInteger_base;
16961 }
16962 else {
16963 diff[i++] = digit;
16964 break;
16965 }
16966 diff[i] = digit;
16967 }
16968 for ( ; i < al; i++) {
16969 diff[i] = a[i];
16970 }
16971
16972 return new BigInteger(diff, sign, CONSTRUCT);
16973};
16974
16975(function() {
16976 function addOne(n, sign) {
16977 var a = n._d;
16978 var sum = a.slice();
16979 var carry = true;
16980 var i = 0;
16981
16982 while (true) {
16983 var digit = (a[i] || 0) + 1;
16984 sum[i] = digit % BigInteger_base;
16985 if (digit <= BigInteger_base - 1) {
16986 break;
16987 }
16988 ++i;
16989 }
16990
16991 return new BigInteger(sum, sign, CONSTRUCT);
16992 }
16993
16994 function subtractOne(n, sign) {
16995 var a = n._d;
16996 var sum = a.slice();
16997 var borrow = true;
16998 var i = 0;
16999
17000 while (true) {
17001 var digit = (a[i] || 0) - 1;
17002 if (digit < 0) {
17003 sum[i] = digit + BigInteger_base;
17004 }
17005 else {
17006 sum[i] = digit;
17007 break;
17008 }
17009 ++i;
17010 }
17011
17012 return new BigInteger(sum, sign, CONSTRUCT);
17013 }
17014
17015 /*
17016 Function: next
17017 Get the next <BigInteger> (add one).
17018
17019 Returns:
17020
17021 *this* + 1.
17022
17023 See Also:
17024
17025 <add>, <prev>
17026 */
17027 BigInteger.prototype.next = function() {
17028 switch (this._s) {
17029 case 0:
17030 return ONE;
17031 case -1:
17032 return subtractOne(this, -1);
17033 // case 1:
17034 default:
17035 return addOne(this, 1);
17036 }
17037 };
17038
17039 /*
17040 Function: prev
17041 Get the previous <BigInteger> (subtract one).
17042
17043 Returns:
17044
17045 *this* - 1.
17046
17047 See Also:
17048
17049 <next>, <subtract>
17050 */
17051 BigInteger.prototype.prev = function() {
17052 switch (this._s) {
17053 case 0:
17054 return M_ONE;
17055 case -1:
17056 return addOne(this, -1);
17057 // case 1:
17058 default:
17059 return subtractOne(this, 1);
17060 }
17061 };
17062})();
17063
17064/*
17065 Function: compareAbs
17066 Compare the absolute value of two <BigIntegers>.
17067
17068 Calling <compareAbs> is faster than calling <abs> twice, then <compare>.
17069
17070 Parameters:
17071
17072 n - The number to compare to *this*. Will be converted to a <BigInteger>.
17073
17074 Returns:
17075
17076 -1, 0, or +1 if *|this|* is less than, equal to, or greater than *|n|*.
17077
17078 See Also:
17079
17080 <compare>, <abs>
17081*/
17082BigInteger.prototype.compareAbs = function(n) {
17083 if (this === n) {
17084 return 0;
17085 }
17086
17087 if (!(n instanceof BigInteger)) {
17088 if (!isFinite(n)) {
17089 return(isNaN(n) ? n : -1);
17090 }
17091 n = BigInteger(n);
17092 }
17093
17094 if (this._s === 0) {
17095 return (n._s !== 0) ? -1 : 0;
17096 }
17097 if (n._s === 0) {
17098 return 1;
17099 }
17100
17101 var l = this._d.length;
17102 var nl = n._d.length;
17103 if (l < nl) {
17104 return -1;
17105 }
17106 else if (l > nl) {
17107 return 1;
17108 }
17109
17110 var a = this._d;
17111 var b = n._d;
17112 for (var i = l-1; i >= 0; i--) {
17113 if (a[i] !== b[i]) {
17114 return a[i] < b[i] ? -1 : 1;
17115 }
17116 }
17117
17118 return 0;
17119};
17120
17121/*
17122 Function: compare
17123 Compare two <BigIntegers>.
17124
17125 Parameters:
17126
17127 n - The number to compare to *this*. Will be converted to a <BigInteger>.
17128
17129 Returns:
17130
17131 -1, 0, or +1 if *this* is less than, equal to, or greater than *n*.
17132
17133 See Also:
17134
17135 <compareAbs>, <isPositive>, <isNegative>, <isUnit>
17136*/
17137BigInteger.prototype.compare = function(n) {
17138 if (this === n) {
17139 return 0;
17140 }
17141
17142 n = BigInteger(n);
17143
17144 if (this._s === 0) {
17145 return -n._s;
17146 }
17147
17148 if (this._s === n._s) { // both positive or both negative
17149 var cmp = this.compareAbs(n);
17150 return cmp * this._s;
17151 }
17152 else {
17153 return this._s;
17154 }
17155};
17156
17157/*
17158 Function: isUnit
17159 Return true iff *this* is either 1 or -1.
17160
17161 Returns:
17162
17163 true if *this* compares equal to <BigInteger.ONE> or <BigInteger.M_ONE>.
17164
17165 See Also:
17166
17167 <isZero>, <isNegative>, <isPositive>, <compareAbs>, <compare>,
17168 <BigInteger.ONE>, <BigInteger.M_ONE>
17169*/
17170BigInteger.prototype.isUnit = function() {
17171 return this === ONE ||
17172 this === M_ONE ||
17173 (this._d.length === 1 && this._d[0] === 1);
17174};
17175
17176/*
17177 Function: multiply
17178 Multiply two <BigIntegers>.
17179
17180 Parameters:
17181
17182 n - The number to multiply *this* by. Will be converted to a
17183 <BigInteger>.
17184
17185 Returns:
17186
17187 The numbers multiplied together.
17188
17189 See Also:
17190
17191 <add>, <subtract>, <quotient>, <square>
17192*/
17193BigInteger.prototype.multiply = function(n) {
17194 // TODO: Consider adding Karatsuba multiplication for large numbers
17195 if (this._s === 0) {
17196 return ZERO;
17197 }
17198
17199 n = BigInteger(n);
17200 if (n._s === 0) {
17201 return ZERO;
17202 }
17203 if (this.isUnit()) {
17204 if (this._s < 0) {
17205 return n.negate();
17206 }
17207 return n;
17208 }
17209 if (n.isUnit()) {
17210 if (n._s < 0) {
17211 return this.negate();
17212 }
17213 return this;
17214 }
17215 if (this === n) {
17216 return this.square();
17217 }
17218
17219 var r = (this._d.length >= n._d.length);
17220 var a = (r ? this : n)._d; // a will be longer than b
17221 var b = (r ? n : this)._d;
17222 var al = a.length;
17223 var bl = b.length;
17224
17225 var pl = al + bl;
17226 var partial = new Array(pl);
17227 var i;
17228 for (i = 0; i < pl; i++) {
17229 partial[i] = 0;
17230 }
17231
17232 for (i = 0; i < bl; i++) {
17233 var carry = 0;
17234 var bi = b[i];
17235 var jlimit = al + i;
17236 var digit;
17237 for (var j = i; j < jlimit; j++) {
17238 digit = partial[j] + bi * a[j - i] + carry;
17239 carry = (digit / BigInteger_base) | 0;
17240 partial[j] = (digit % BigInteger_base) | 0;
17241 }
17242 if (carry) {
17243 digit = partial[j] + carry;
17244 carry = (digit / BigInteger_base) | 0;
17245 partial[j] = digit % BigInteger_base;
17246 }
17247 }
17248 return new BigInteger(partial, this._s * n._s, CONSTRUCT);
17249};
17250
17251// Multiply a BigInteger by a single-digit native number
17252// Assumes that this and n are >= 0
17253// This is not really intended to be used outside the library itself
17254BigInteger.prototype.multiplySingleDigit = function(n) {
17255 if (n === 0 || this._s === 0) {
17256 return ZERO;
17257 }
17258 if (n === 1) {
17259 return this;
17260 }
17261
17262 var digit;
17263 if (this._d.length === 1) {
17264 digit = this._d[0] * n;
17265 if (digit >= BigInteger_base) {
17266 return new BigInteger([(digit % BigInteger_base)|0,
17267 (digit / BigInteger_base)|0], 1, CONSTRUCT);
17268 }
17269 return new BigInteger([digit], 1, CONSTRUCT);
17270 }
17271
17272 if (n === 2) {
17273 return this.add(this);
17274 }
17275 if (this.isUnit()) {
17276 return new BigInteger([n], 1, CONSTRUCT);
17277 }
17278
17279 var a = this._d;
17280 var al = a.length;
17281
17282 var pl = al + 1;
17283 var partial = new Array(pl);
17284 for (var i = 0; i < pl; i++) {
17285 partial[i] = 0;
17286 }
17287
17288 var carry = 0;
17289 for (var j = 0; j < al; j++) {
17290 digit = n * a[j] + carry;
17291 carry = (digit / BigInteger_base) | 0;
17292 partial[j] = (digit % BigInteger_base) | 0;
17293 }
17294 if (carry) {
17295 partial[j] = carry;
17296 }
17297
17298 return new BigInteger(partial, 1, CONSTRUCT);
17299};
17300
17301/*
17302 Function: square
17303 Multiply a <BigInteger> by itself.
17304
17305 This is slightly faster than regular multiplication, since it removes the
17306 duplicated multiplcations.
17307
17308 Returns:
17309
17310 > this.multiply(this)
17311
17312 See Also:
17313 <multiply>
17314*/
17315BigInteger.prototype.square = function() {
17316 // Normally, squaring a 10-digit number would take 100 multiplications.
17317 // Of these 10 are unique diagonals, of the remaining 90 (100-10), 45 are repeated.
17318 // This procedure saves (N*(N-1))/2 multiplications, (e.g., 45 of 100 multiplies).
17319 // Based on code by Gary Darby, Intellitech Systems Inc., www.DelphiForFun.org
17320
17321 if (this._s === 0) {
17322 return ZERO;
17323 }
17324 if (this.isUnit()) {
17325 return ONE;
17326 }
17327
17328 var digits = this._d;
17329 var length = digits.length;
17330 var imult1 = new Array(length + length + 1);
17331 var product, carry, k;
17332 var i;
17333
17334 // Calculate diagonal
17335 for (i = 0; i < length; i++) {
17336 k = i * 2;
17337 product = digits[i] * digits[i];
17338 carry = (product / BigInteger_base) | 0;
17339 imult1[k] = product % BigInteger_base;
17340 imult1[k + 1] = carry;
17341 }
17342
17343 // Calculate repeating part
17344 for (i = 0; i < length; i++) {
17345 carry = 0;
17346 k = i * 2 + 1;
17347 for (var j = i + 1; j < length; j++, k++) {
17348 product = digits[j] * digits[i] * 2 + imult1[k] + carry;
17349 carry = (product / BigInteger_base) | 0;
17350 imult1[k] = product % BigInteger_base;
17351 }
17352 k = length + i;
17353 var digit = carry + imult1[k];
17354 carry = (digit / BigInteger_base) | 0;
17355 imult1[k] = digit % BigInteger_base;
17356 imult1[k + 1] += carry;
17357 }
17358
17359 return new BigInteger(imult1, 1, CONSTRUCT);
17360};
17361
17362/*
17363 Function: quotient
17364 Divide two <BigIntegers> and truncate towards zero.
17365
17366 <quotient> throws an exception if *n* is zero.
17367
17368 Parameters:
17369
17370 n - The number to divide *this* by. Will be converted to a <BigInteger>.
17371
17372 Returns:
17373
17374 The *this* / *n*, truncated to an integer.
17375
17376 See Also:
17377
17378 <add>, <subtract>, <multiply>, <divRem>, <remainder>
17379*/
17380BigInteger.prototype.quotient = function(n) {
17381 return this.divRem(n)[0];
17382};
17383
17384/*
17385 Function: divide
17386 Deprecated synonym for <quotient>.
17387*/
17388BigInteger.prototype.divide = BigInteger.prototype.quotient;
17389
17390/*
17391 Function: remainder
17392 Calculate the remainder of two <BigIntegers>.
17393
17394 <remainder> throws an exception if *n* is zero.
17395
17396 Parameters:
17397
17398 n - The remainder after *this* is divided *this* by *n*. Will be
17399 converted to a <BigInteger>.
17400
17401 Returns:
17402
17403 *this* % *n*.
17404
17405 See Also:
17406
17407 <divRem>, <quotient>
17408*/
17409BigInteger.prototype.remainder = function(n) {
17410 return this.divRem(n)[1];
17411};
17412
17413/*
17414 Function: divRem
17415 Calculate the integer quotient and remainder of two <BigIntegers>.
17416
17417 <divRem> throws an exception if *n* is zero.
17418
17419 Parameters:
17420
17421 n - The number to divide *this* by. Will be converted to a <BigInteger>.
17422
17423 Returns:
17424
17425 A two-element array containing the quotient and the remainder.
17426
17427 > a.divRem(b)
17428
17429 is exactly equivalent to
17430
17431 > [a.quotient(b), a.remainder(b)]
17432
17433 except it is faster, because they are calculated at the same time.
17434
17435 See Also:
17436
17437 <quotient>, <remainder>
17438*/
17439BigInteger.prototype.divRem = function(n) {
17440 n = BigInteger(n);
17441 if (n._s === 0) {
17442 throw new Error("Divide by zero");
17443 }
17444 if (this._s === 0) {
17445 return [ZERO, ZERO];
17446 }
17447 if (n._d.length === 1) {
17448 return this.divRemSmall(n._s * n._d[0]);
17449 }
17450
17451 // Test for easy cases -- |n1| <= |n2|
17452 switch (this.compareAbs(n)) {
17453 case 0: // n1 == n2
17454 return [this._s === n._s ? ONE : M_ONE, ZERO];
17455 case -1: // |n1| < |n2|
17456 return [ZERO, this];
17457 }
17458
17459 var sign = this._s * n._s;
17460 var a = n.abs();
17461 var b_digits = this._d;
17462 var b_index = b_digits.length;
17463 var digits = n._d.length;
17464 var quot = [];
17465 var guess;
17466
17467 var part = new BigInteger([], 0, CONSTRUCT);
17468
17469 while (b_index) {
17470 part._d.unshift(b_digits[--b_index]);
17471 part = new BigInteger(part._d, 1, CONSTRUCT);
17472
17473 if (part.compareAbs(n) < 0) {
17474 quot.push(0);
17475 continue;
17476 }
17477 if (part._s === 0) {
17478 guess = 0;
17479 }
17480 else {
17481 var xlen = part._d.length, ylen = a._d.length;
17482 var highx = part._d[xlen-1]*BigInteger_base + part._d[xlen-2];
17483 var highy = a._d[ylen-1]*BigInteger_base + a._d[ylen-2];
17484 if (part._d.length > a._d.length) {
17485 // The length of part._d can either match a._d length,
17486 // or exceed it by one.
17487 highx = (highx+1)*BigInteger_base;
17488 }
17489 guess = Math.ceil(highx/highy);
17490 }
17491 do {
17492 var check = a.multiplySingleDigit(guess);
17493 if (check.compareAbs(part) <= 0) {
17494 break;
17495 }
17496 guess--;
17497 } while (guess);
17498
17499 quot.push(guess);
17500 if (!guess) {
17501 continue;
17502 }
17503 var diff = part.subtract(check);
17504 part._d = diff._d.slice();
17505 }
17506
17507 return [new BigInteger(quot.reverse(), sign, CONSTRUCT),
17508 new BigInteger(part._d, this._s, CONSTRUCT)];
17509};
17510
17511// Throws an exception if n is outside of (-BigInteger.base, -1] or
17512// [1, BigInteger.base). It's not necessary to call this, since the
17513// other division functions will call it if they are able to.
17514BigInteger.prototype.divRemSmall = function(n) {
17515 var r;
17516 n = +n;
17517 if (n === 0) {
17518 throw new Error("Divide by zero");
17519 }
17520
17521 var n_s = n < 0 ? -1 : 1;
17522 var sign = this._s * n_s;
17523 n = Math.abs(n);
17524
17525 if (n < 1 || n >= BigInteger_base) {
17526 throw new Error("Argument out of range");
17527 }
17528
17529 if (this._s === 0) {
17530 return [ZERO, ZERO];
17531 }
17532
17533 if (n === 1 || n === -1) {
17534 return [(sign === 1) ? this.abs() : new BigInteger(this._d, sign, CONSTRUCT), ZERO];
17535 }
17536
17537 // 2 <= n < BigInteger_base
17538
17539 // divide a single digit by a single digit
17540 if (this._d.length === 1) {
17541 var q = new BigInteger([(this._d[0] / n) | 0], 1, CONSTRUCT);
17542 r = new BigInteger([(this._d[0] % n) | 0], 1, CONSTRUCT);
17543 if (sign < 0) {
17544 q = q.negate();
17545 }
17546 if (this._s < 0) {
17547 r = r.negate();
17548 }
17549 return [q, r];
17550 }
17551
17552 var digits = this._d.slice();
17553 var quot = new Array(digits.length);
17554 var part = 0;
17555 var diff = 0;
17556 var i = 0;
17557 var guess;
17558
17559 while (digits.length) {
17560 part = part * BigInteger_base + digits[digits.length - 1];
17561 if (part < n) {
17562 quot[i++] = 0;
17563 digits.pop();
17564 diff = BigInteger_base * diff + part;
17565 continue;
17566 }
17567 if (part === 0) {
17568 guess = 0;
17569 }
17570 else {
17571 guess = (part / n) | 0;
17572 }
17573
17574 var check = n * guess;
17575 diff = part - check;
17576 quot[i++] = guess;
17577 if (!guess) {
17578 digits.pop();
17579 continue;
17580 }
17581
17582 digits.pop();
17583 part = diff;
17584 }
17585
17586 r = new BigInteger([diff], 1, CONSTRUCT);
17587 if (this._s < 0) {
17588 r = r.negate();
17589 }
17590 return [new BigInteger(quot.reverse(), sign, CONSTRUCT), r];
17591};
17592
17593/*
17594 Function: isEven
17595 Return true iff *this* is divisible by two.
17596
17597 Note that <BigInteger.ZERO> is even.
17598
17599 Returns:
17600
17601 true if *this* is even, false otherwise.
17602
17603 See Also:
17604
17605 <isOdd>
17606*/
17607BigInteger.prototype.isEven = function() {
17608 var digits = this._d;
17609 return this._s === 0 || digits.length === 0 || (digits[0] % 2) === 0;
17610};
17611
17612/*
17613 Function: isOdd
17614 Return true iff *this* is not divisible by two.
17615
17616 Returns:
17617
17618 true if *this* is odd, false otherwise.
17619
17620 See Also:
17621
17622 <isEven>
17623*/
17624BigInteger.prototype.isOdd = function() {
17625 return !this.isEven();
17626};
17627
17628/*
17629 Function: sign
17630 Get the sign of a <BigInteger>.
17631
17632 Returns:
17633
17634 * -1 if *this* < 0
17635 * 0 if *this* == 0
17636 * +1 if *this* > 0
17637
17638 See Also:
17639
17640 <isZero>, <isPositive>, <isNegative>, <compare>, <BigInteger.ZERO>
17641*/
17642BigInteger.prototype.sign = function() {
17643 return this._s;
17644};
17645
17646/*
17647 Function: isPositive
17648 Return true iff *this* > 0.
17649
17650 Returns:
17651
17652 true if *this*.compare(<BigInteger.ZERO>) == 1.
17653
17654 See Also:
17655
17656 <sign>, <isZero>, <isNegative>, <isUnit>, <compare>, <BigInteger.ZERO>
17657*/
17658BigInteger.prototype.isPositive = function() {
17659 return this._s > 0;
17660};
17661
17662/*
17663 Function: isNegative
17664 Return true iff *this* < 0.
17665
17666 Returns:
17667
17668 true if *this*.compare(<BigInteger.ZERO>) == -1.
17669
17670 See Also:
17671
17672 <sign>, <isPositive>, <isZero>, <isUnit>, <compare>, <BigInteger.ZERO>
17673*/
17674BigInteger.prototype.isNegative = function() {
17675 return this._s < 0;
17676};
17677
17678/*
17679 Function: isZero
17680 Return true iff *this* == 0.
17681
17682 Returns:
17683
17684 true if *this*.compare(<BigInteger.ZERO>) == 0.
17685
17686 See Also:
17687
17688 <sign>, <isPositive>, <isNegative>, <isUnit>, <BigInteger.ZERO>
17689*/
17690BigInteger.prototype.isZero = function() {
17691 return this._s === 0;
17692};
17693
17694/*
17695 Function: exp10
17696 Multiply a <BigInteger> by a power of 10.
17697
17698 This is equivalent to, but faster than
17699
17700 > if (n >= 0) {
17701 > return this.multiply(BigInteger("1e" + n));
17702 > }
17703 > else { // n <= 0
17704 > return this.quotient(BigInteger("1e" + -n));
17705 > }
17706
17707 Parameters:
17708
17709 n - The power of 10 to multiply *this* by. *n* is converted to a
17710 javascipt number and must be no greater than <BigInteger.MAX_EXP>
17711 (0x7FFFFFFF), or an exception will be thrown.
17712
17713 Returns:
17714
17715 *this* * (10 ** *n*), truncated to an integer if necessary.
17716
17717 See Also:
17718
17719 <pow>, <multiply>
17720*/
17721BigInteger.prototype.exp10 = function(n) {
17722 n = +n;
17723 if (n === 0) {
17724 return this;
17725 }
17726 if (Math.abs(n) > Number(MAX_EXP)) {
17727 throw new Error("exponent too large in BigInteger.exp10");
17728 }
17729 // Optimization for this == 0. This also keeps us from having to trim zeros in the positive n case
17730 if (this._s === 0) {
17731 return ZERO;
17732 }
17733 if (n > 0) {
17734 var k = new BigInteger(this._d.slice(), this._s, CONSTRUCT);
17735
17736 for (; n >= BigInteger_base_log10; n -= BigInteger_base_log10) {
17737 k._d.unshift(0);
17738 }
17739 if (n == 0)
17740 return k;
17741 k._s = 1;
17742 k = k.multiplySingleDigit(Math.pow(10, n));
17743 return (this._s < 0 ? k.negate() : k);
17744 } else if (-n >= this._d.length*BigInteger_base_log10) {
17745 return ZERO;
17746 } else {
17747 var k = new BigInteger(this._d.slice(), this._s, CONSTRUCT);
17748
17749 for (n = -n; n >= BigInteger_base_log10; n -= BigInteger_base_log10) {
17750 k._d.shift();
17751 }
17752 return (n == 0) ? k : k.divRemSmall(Math.pow(10, n))[0];
17753 }
17754};
17755
17756/*
17757 Function: pow
17758 Raise a <BigInteger> to a power.
17759
17760 In this implementation, 0**0 is 1.
17761
17762 Parameters:
17763
17764 n - The exponent to raise *this* by. *n* must be no greater than
17765 <BigInteger.MAX_EXP> (0x7FFFFFFF), or an exception will be thrown.
17766
17767 Returns:
17768
17769 *this* raised to the *nth* power.
17770
17771 See Also:
17772
17773 <modPow>
17774*/
17775BigInteger.prototype.pow = function(n) {
17776 if (this.isUnit()) {
17777 if (this._s > 0) {
17778 return this;
17779 }
17780 else {
17781 return BigInteger(n).isOdd() ? this : this.negate();
17782 }
17783 }
17784
17785 n = BigInteger(n);
17786 if (n._s === 0) {
17787 return ONE;
17788 }
17789 else if (n._s < 0) {
17790 if (this._s === 0) {
17791 throw new Error("Divide by zero");
17792 }
17793 else {
17794 return ZERO;
17795 }
17796 }
17797 if (this._s === 0) {
17798 return ZERO;
17799 }
17800 if (n.isUnit()) {
17801 return this;
17802 }
17803
17804 if (n.compareAbs(MAX_EXP) > 0) {
17805 throw new Error("exponent too large in BigInteger.pow");
17806 }
17807 var x = this;
17808 var aux = ONE;
17809 var two = BigInteger.small[2];
17810
17811 while (n.isPositive()) {
17812 if (n.isOdd()) {
17813 aux = aux.multiply(x);
17814 if (n.isUnit()) {
17815 return aux;
17816 }
17817 }
17818 x = x.square();
17819 n = n.quotient(two);
17820 }
17821
17822 return aux;
17823};
17824
17825/*
17826 Function: modPow
17827 Raise a <BigInteger> to a power (mod m).
17828
17829 Because it is reduced by a modulus, <modPow> is not limited by
17830 <BigInteger.MAX_EXP> like <pow>.
17831
17832 Parameters:
17833
17834 exponent - The exponent to raise *this* by. Must be positive.
17835 modulus - The modulus.
17836
17837 Returns:
17838
17839 *this* ^ *exponent* (mod *modulus*).
17840
17841 See Also:
17842
17843 <pow>, <mod>
17844*/
17845BigInteger.prototype.modPow = function(exponent, modulus) {
17846 var result = ONE;
17847 var base = this;
17848
17849 while (exponent.isPositive()) {
17850 if (exponent.isOdd()) {
17851 result = result.multiply(base).remainder(modulus);
17852 }
17853
17854 exponent = exponent.quotient(BigInteger.small[2]);
17855 if (exponent.isPositive()) {
17856 base = base.square().remainder(modulus);
17857 }
17858 }
17859
17860 return result;
17861};
17862
17863/*
17864 Function: log
17865 Get the natural logarithm of a <BigInteger> as a native JavaScript number.
17866
17867 This is equivalent to
17868
17869 > Math.log(this.toJSValue())
17870
17871 but handles values outside of the native number range.
17872
17873 Returns:
17874
17875 log( *this* )
17876
17877 See Also:
17878
17879 <toJSValue>
17880*/
17881BigInteger.prototype.log = function() {
17882 switch (this._s) {
17883 case 0: return -Infinity;
17884 case -1: return NaN;
17885 default: // Fall through.
17886 }
17887
17888 var l = this._d.length;
17889
17890 if (l*BigInteger_base_log10 < 30) {
17891 return Math.log(this.valueOf());
17892 }
17893
17894 var N = Math.ceil(30/BigInteger_base_log10);
17895 var firstNdigits = this._d.slice(l - N);
17896 return Math.log((new BigInteger(firstNdigits, 1, CONSTRUCT)).valueOf()) + (l - N) * Math.log(BigInteger_base);
17897};
17898
17899/*
17900 Function: valueOf
17901 Convert a <BigInteger> to a native JavaScript integer.
17902
17903 This is called automatically by JavaScipt to convert a <BigInteger> to a
17904 native value.
17905
17906 Returns:
17907
17908 > parseInt(this.toString(), 10)
17909
17910 See Also:
17911
17912 <toString>, <toJSValue>
17913*/
17914BigInteger.prototype.valueOf = function() {
17915 return parseInt(this.toString(), 10);
17916};
17917
17918/*
17919 Function: toJSValue
17920 Convert a <BigInteger> to a native JavaScript integer.
17921
17922 This is the same as valueOf, but more explicitly named.
17923
17924 Returns:
17925
17926 > parseInt(this.toString(), 10)
17927
17928 See Also:
17929
17930 <toString>, <valueOf>
17931*/
17932BigInteger.prototype.toJSValue = function() {
17933 return parseInt(this.toString(), 10);
17934};
17935
17936var MAX_EXP = BigInteger(0x7FFFFFFF);
17937// Constant: MAX_EXP
17938// The largest exponent allowed in <pow> and <exp10> (0x7FFFFFFF or 2147483647).
17939BigInteger.MAX_EXP = MAX_EXP;
17940
17941(function() {
17942 function makeUnary(fn) {
17943 return function(a) {
17944 return fn.call(BigInteger(a));
17945 };
17946 }
17947
17948 function makeBinary(fn) {
17949 return function(a, b) {
17950 return fn.call(BigInteger(a), BigInteger(b));
17951 };
17952 }
17953
17954 function makeTrinary(fn) {
17955 return function(a, b, c) {
17956 return fn.call(BigInteger(a), BigInteger(b), BigInteger(c));
17957 };
17958 }
17959
17960 (function() {
17961 var i, fn;
17962 var unary = "toJSValue,isEven,isOdd,sign,isZero,isNegative,abs,isUnit,square,negate,isPositive,toString,next,prev,log".split(",");
17963 var binary = "compare,remainder,divRem,subtract,add,quotient,divide,multiply,pow,compareAbs".split(",");
17964 var trinary = ["modPow"];
17965
17966 for (i = 0; i < unary.length; i++) {
17967 fn = unary[i];
17968 BigInteger[fn] = makeUnary(BigInteger.prototype[fn]);
17969 }
17970
17971 for (i = 0; i < binary.length; i++) {
17972 fn = binary[i];
17973 BigInteger[fn] = makeBinary(BigInteger.prototype[fn]);
17974 }
17975
17976 for (i = 0; i < trinary.length; i++) {
17977 fn = trinary[i];
17978 BigInteger[fn] = makeTrinary(BigInteger.prototype[fn]);
17979 }
17980
17981 BigInteger.exp10 = function(x, n) {
17982 return BigInteger(x).exp10(n);
17983 };
17984 })();
17985})();
17986
17987exports.BigInteger = BigInteger;
17988})(typeof exports !== 'undefined' ? exports : this);
17989</script>
16172 <script>(function() { 17990 <script>(function() {
16173 17991
16174 // mnemonics is populated as required by getLanguage 17992 // mnemonics is populated as required by getLanguage
@@ -16185,14 +18003,20 @@ var Mnemonic = function(language) {
16185 var showPubKey = true; 18003 var showPubKey = true;
16186 var showPrivKey = true; 18004 var showPrivKey = true;
16187 18005
18006 var entropyChangeTimeoutEvent = null;
16188 var phraseChangeTimeoutEvent = null; 18007 var phraseChangeTimeoutEvent = null;
16189 var rootKeyChangedTimeoutEvent = null; 18008 var rootKeyChangedTimeoutEvent = null;
16190 18009
16191 var DOM = {}; 18010 var DOM = {};
16192 DOM.network = $(".network"); 18011 DOM.network = $(".network");
16193 DOM.phraseNetwork = $("#network-phrase"); 18012 DOM.phraseNetwork = $("#network-phrase");
18013 DOM.useEntropy = $(".use-entropy");
18014 DOM.entropyContainer = $(".entropy-container");
18015 DOM.entropy = $(".entropy");
18016 DOM.entropyError = $(".entropy-error");
16194 DOM.phrase = $(".phrase"); 18017 DOM.phrase = $(".phrase");
16195 DOM.passphrase = $(".passphrase"); 18018 DOM.passphrase = $(".passphrase");
18019 DOM.generateContainer = $(".generate-container");
16196 DOM.generate = $(".generate"); 18020 DOM.generate = $(".generate");
16197 DOM.seed = $(".seed"); 18021 DOM.seed = $(".seed");
16198 DOM.rootKey = $(".root-key"); 18022 DOM.rootKey = $(".root-key");
@@ -16224,6 +18048,8 @@ var Mnemonic = function(language) {
16224 function init() { 18048 function init() {
16225 // Events 18049 // Events
16226 DOM.network.on("change", networkChanged); 18050 DOM.network.on("change", networkChanged);
18051 DOM.useEntropy.on("change", setEntropyVisibility);
18052 DOM.entropy.on("input", delayedEntropyChanged);
16227 DOM.phrase.on("input", delayedPhraseChanged); 18053 DOM.phrase.on("input", delayedPhraseChanged);
16228 DOM.passphrase.on("input", delayedPhraseChanged); 18054 DOM.passphrase.on("input", delayedPhraseChanged);
16229 DOM.generate.on("click", generateClicked); 18055 DOM.generate.on("click", generateClicked);
@@ -16260,6 +18086,21 @@ var Mnemonic = function(language) {
16260 } 18086 }
16261 } 18087 }
16262 18088
18089 function setEntropyVisibility() {
18090 if (isUsingOwnEntropy()) {
18091 DOM.entropyContainer.removeClass("hidden");
18092 DOM.generateContainer.addClass("hidden");
18093 DOM.phrase.prop("readonly", true);
18094 DOM.entropy.focus();
18095 entropyChanged();
18096 }
18097 else {
18098 DOM.entropyContainer.addClass("hidden");
18099 DOM.generateContainer.removeClass("hidden");
18100 DOM.phrase.prop("readonly", false);
18101 }
18102 }
18103
16263 function delayedPhraseChanged() { 18104 function delayedPhraseChanged() {
16264 hideValidationError(); 18105 hideValidationError();
16265 showPending(); 18106 showPending();
@@ -16287,6 +18128,20 @@ var Mnemonic = function(language) {
16287 hidePending(); 18128 hidePending();
16288 } 18129 }
16289 18130
18131 function delayedEntropyChanged() {
18132 hideValidationError();
18133 showPending();
18134 if (entropyChangeTimeoutEvent != null) {
18135 clearTimeout(entropyChangeTimeoutEvent);
18136 }
18137 entropyChangeTimeoutEvent = setTimeout(entropyChanged, 400);
18138 }
18139
18140 function entropyChanged() {
18141 setMnemonicFromEntropy();
18142 phraseChanged();
18143 }
18144
16290 function delayedRootKeyChanged() { 18145 function delayedRootKeyChanged() {
16291 // Warn if there is an existing mnemonic or passphrase. 18146 // Warn if there is an existing mnemonic or passphrase.
16292 if (DOM.phrase.val().length > 0 || DOM.passphrase.val().length > 0) { 18147 if (DOM.phrase.val().length > 0 || DOM.passphrase.val().length > 0) {
@@ -16339,6 +18194,9 @@ var Mnemonic = function(language) {
16339 } 18194 }
16340 18195
16341 function generateClicked() { 18196 function generateClicked() {
18197 if (isUsingOwnEntropy()) {
18198 return;
18199 }
16342 clearDisplay(); 18200 clearDisplay();
16343 showPending(); 18201 showPending();
16344 setTimeout(function() { 18202 setTimeout(function() {
@@ -16770,7 +18628,12 @@ var Mnemonic = function(language) {
16770 } 18628 }
16771 18629
16772 function getLanguageFromUrl() { 18630 function getLanguageFromUrl() {
16773 return window.location.hash.substring(1); 18631 for (var language in WORDLISTS) {
18632 if (window.location.hash.indexOf(language) > -1) {
18633 return language;
18634 }
18635 }
18636 return "";
16774 } 18637 }
16775 18638
16776 function setMnemonicLanguage() { 18639 function setMnemonicLanguage() {
@@ -16821,6 +18684,65 @@ var Mnemonic = function(language) {
16821 return phrase; 18684 return phrase;
16822 } 18685 }
16823 18686
18687 function isUsingOwnEntropy() {
18688 return DOM.useEntropy.prop("checked");
18689 }
18690
18691 function setMnemonicFromEntropy() {
18692 hideEntropyError();
18693 // Work out minimum base for entropy
18694 var entropyStr = DOM.entropy.val();
18695 var entropy = Entropy.fromString(entropyStr);
18696 if (entropy.hexStr.length == 0) {
18697 return;
18698 }
18699 // Show entropy details
18700 var extraBits = 32 - (entropy.binaryStr.length % 32);
18701 var extraChars = Math.ceil(extraBits * Math.log(2) / Math.log(entropy.base.asInt));
18702 var strength = "an extremely weak";
18703 if (entropy.hexStr.length >= 8) {
18704 strength = "a very weak";
18705 }
18706 if (entropy.hexStr.length >= 12) {
18707 strength = "a weak";
18708 }
18709 if (entropy.hexStr.length >= 24) {
18710 strength = "a strong";
18711 }
18712 if (entropy.hexStr.length >= 32) {
18713 strength = "a very strong";
18714 }
18715 if (entropy.hexStr.length >= 40) {
18716 strength = "an extremely strong";
18717 }
18718 if (entropy.hexStr.length >=48) {
18719 strength = "an even stronger"
18720 }
18721 var msg = "Have " + entropy.binaryStr.length + " bits of entropy, " + extraChars + " more " + entropy.base.str + " chars required to generate " + strength + " mnemonic: " + entropy.cleanStr;
18722 showEntropyError(msg);
18723 // Discard trailing entropy
18724 var hexStr = entropy.hexStr.substring(0, Math.floor(entropy.hexStr.length / 8) * 8);
18725 // Convert entropy string to numeric array
18726 var entropyArr = [];
18727 for (var i=0; i<hexStr.length / 2; i++) {
18728 var entropyByte = parseInt(hexStr[i*2].concat(hexStr[i*2+1]), 16);
18729 entropyArr.push(entropyByte)
18730 }
18731 // Convert entropy array to mnemonic
18732 var phrase = mnemonic.toMnemonic(entropyArr);
18733 // Set the mnemonic in the UI
18734 DOM.phrase.val(phrase);
18735 }
18736
18737 function hideEntropyError() {
18738 DOM.entropyError.addClass("hidden");
18739 }
18740
18741 function showEntropyError(msg) {
18742 DOM.entropyError.text(msg);
18743 DOM.entropyError.removeClass("hidden");
18744 }
18745
16824 var networks = [ 18746 var networks = [
16825 { 18747 {
16826 name: "Bitcoin", 18748 name: "Bitcoin",