]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blobdiff - src/js/entropy.js
Card suits have color and use larger font size
[perso/Immae/Projets/Cryptomonnaies/BIP39.git] / src / js / entropy.js
index 8e29d40824237365b41dd543d21b5e90f91f78ab..0b76dcfef0afbdfb8f8de5103bb090828718fa9f 100644 (file)
@@ -70,19 +70,22 @@ window.Entropy = new (function() {
         // Convert dice to base6 entropy (ie 1-6 to 0-5)
         // This is done by changing all 6s to 0s
         if (base.str == "dice") {
-            var newRawEntropyStr = "";
-            for (var i=0; i<rawEntropyStr.length; i++) {
-                var c = rawEntropyStr[i];
+            var newParts = [];
+            var newInts = [];
+            for (var i=0; i<base.parts.length; i++) {
+                var c = base.parts[i];
                 if ("12345".indexOf(c) > -1) {
-                    newRawEntropyStr += c;
+                    newParts[i] = base.parts[i];
+                    newInts[i] = base.ints[i];
                 }
                 else {
-                    newRawEntropyStr += "0";
+                    newParts[i] = "0";
+                    newInts[i] = 0;
                 }
             }
-            rawEntropyStr = newRawEntropyStr;
             base.str = "base 6 (dice)";
-            base.parts = matchers.base6(rawEntropyStr);
+            base.ints = newInts;
+            base.parts = newParts;
             base.matcher = matchers.base6;
         }
         // Detect empty entropy
@@ -90,45 +93,10 @@ window.Entropy = new (function() {
             return {
                 binaryStr: "",
                 cleanStr: "",
+                cleanHtml: "",
                 base: base,
             };
         }
-        // Pull leading zeros off
-        var leadingZeros = [];
-        while (base.ints[0] == "0") {
-            leadingZeros.push("0");
-            base.ints.shift();
-        }
-        // Convert leading zeros to binary equivalent
-        var numBinLeadingZeros = Math.floor(Math.log2(base.asInt) * leadingZeros.length);
-        var binLeadingZeros = "";
-        for (var i=0; i<numBinLeadingZeros; i++) {
-            binLeadingZeros += "0";
-        }
-        // Handle entropy of zero
-        if (base.ints.length == 0) {
-            return {
-                binaryStr: binLeadingZeros,
-                cleanStr: leadingZeros,
-                base: base,
-            }
-        }
-        // If the first integer is small, it must be padded with zeros.
-        // Otherwise the chance of the first bit being 1 is 100%, which is
-        // obviously incorrect.
-        // This is not perfect for unusual bases, eg base 6 has 2.6 bits, so is
-        // slightly biased toward having leading zeros, but it's still better
-        // than ignoring it completely.
-        // TODO: revise this, it seems very fishy. For example, in base 10, there are
-        // 8 opportunities to start with 0 but only 2 to start with 1
-        var firstInt = base.ints[0];
-        var firstIntBits = Math.floor(Math.log2(firstInt))+1;
-        var maxFirstIntBits = Math.floor(Math.log2(base.asInt-1))+1;
-        var missingFirstIntBits = maxFirstIntBits - firstIntBits;
-        var firstIntLeadingZeros = "";
-        for (var i=0; i<missingFirstIntBits; i++) {
-            binLeadingZeros += "0";
-        }
         // Convert base.ints to BigInteger.
         // Due to using unusual bases, eg cards of base52, this is not as simple as
         // using BigInteger.parse()
@@ -139,12 +107,35 @@ window.Entropy = new (function() {
             var additionalEntropy = BigInteger.parse(base.asInt).pow(power).multiply(thisInt);
             entropyInt = entropyInt.add(additionalEntropy);
         }
-        // Convert entropy to different formats
-        var entropyBin = binLeadingZeros + entropyInt.toString(2);
+        // Convert entropy to binary
+        var entropyBin = entropyInt.toString(2);
+        // If the first integer is small, it must be padded with zeros.
+        // Otherwise the chance of the first bit being 1 is 100%, which is
+        // obviously incorrect.
+        // This is not perfect for non-2^n bases.
+        var expectedBits = Math.floor(base.parts.length * Math.log2(base.asInt));
+        while (entropyBin.length < expectedBits) {
+            entropyBin = "0" + entropyBin;
+        }
+        // Supply a 'filtered' entropy string for display purposes
         var entropyClean = base.parts.join("");
+        var entropyHtml = base.parts.join("");
+        if (base.asInt == 52) {
+            entropyClean = base.parts.join(" ").toUpperCase();
+            entropyClean = entropyClean.replace(/C/g, "\u2663");
+            entropyClean = entropyClean.replace(/D/g, "\u2666");
+            entropyClean = entropyClean.replace(/H/g, "\u2665");
+            entropyClean = entropyClean.replace(/S/g, "\u2660");
+            entropyHtml = base.parts.join(" ").toUpperCase();
+            entropyHtml = entropyHtml.replace(/C/g, "<span class='card-suit club'>\u2663</span>");
+            entropyHtml = entropyHtml.replace(/D/g, "<span class='card-suit diamond'>\u2666</span>");
+            entropyHtml = entropyHtml.replace(/H/g, "<span class='card-suit heart'>\u2665</span>");
+            entropyHtml = entropyHtml.replace(/S/g, "<span class='card-suit spade'>\u2660</span>");
+        }
         var e = {
             binaryStr: entropyBin,
             cleanStr: entropyClean,
+            cleanHtml: entropyHtml,
             base: base,
         }
         return e;