]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/commitdiff
Entropy feedback in tabular format, not sentence
authorIan Coleman <coleman.ian@gmail.com>
Wed, 9 Nov 2016 23:58:41 +0000 (10:58 +1100)
committerIan Coleman <coleman.ian@gmail.com>
Thu, 10 Nov 2016 06:53:09 +0000 (17:53 +1100)
src/index.html
src/js/entropy.js
src/js/index.js
tests.js

index cb7a7817aca290fbeb72204d79f80998db700010..feefbd83fa47b2c5aa4cbb51bc074d512a47e406 100644 (file)
                             </div>
                         </div>
                         <div class="entropy-container hidden">
-                            <label for="entropy" class="col-sm-2 control-label">Entropy</label>
-                            <div class="col-sm-10">
-                                <input id="entropy" class="entropy form-control" placeholder="Accepts binary, base 6, 6-sided dice, base 10, hexadecimal">
-                                <span class="help-block">
-                                    <div class="text-danger">
-                                    This is an advanced feature.
-                                    Your mnemonic may be insecure if this feature is used incorrectly.
-                                    <a href="#entropy-notes">Read more</a>
+                            <div class="form-group">
+                                <label for="entropy" class="col-sm-2 control-label">Entropy</label>
+                                <div class="col-sm-10">
+                                    <input id="entropy" class="entropy form-control" placeholder="Accepts binary, base 6, 6-sided dice, base 10, hexadecimal">
+                                    <span class="help-block">
+                                        <div class="text-danger">
+                                        This is an advanced feature.
+                                        Your mnemonic may be insecure if this feature is used incorrectly.
+                                        <a href="#entropy-notes">Read more</a>
+                                        </div>
+                                   </span>
+                                </div>
+                            </div>
+                            <div class="entropy-feedback">
+                                <div class="form-group">
+                                    <label class="col-sm-2 control-label">Filtered</label>
+                                    <div class="filtered col-sm-10 form-control-static"></div>
+                                </div>
+                                <div class="form-group">
+                                    <label class="col-sm-2 control-label">Type</label>
+                                    <div class="type col-sm-10 form-control-static"></div>
+                                </div>
+                                <div class="form-group">
+                                    <label class="col-sm-2 control-label">Strength</label>
+                                    <div class="strength col-sm-10 form-control-static"></div>
+                                </div>
+                                <div class="form-group">
+                                    <label class="col-sm-2 control-label">Event Count</label>
+                                    <div class="event-count col-sm-10 form-control-static"></div>
+                                </div>
+                                <div class="form-group">
+                                    <label class="col-sm-2 control-label">Bits Per Event</label>
+                                    <div class="bits-per-event col-sm-10 form-control-static"></div>
+                                </div>
+                                <div class="form-group">
+                                    <label class="col-sm-2 control-label">Bits</label>
+                                    <div class="bits col-sm-10 form-control-static"></div>
+                                </div>
+                                <div class="form-group">
+                                    <label class="col-sm-2 control-label">Mnemonic Length</label>
+                                    <div class="col-sm-10">
+                                        <select class="mnemonic-length form-control">
+                                            <option value="raw">From entropy length</option>
+                                            <option value="12">12 Words</option>
+                                            <option value="15">15 Words</option>
+                                            <option value="18">18 Words</option>
+                                            <option value="21">21 Words</option>
+                                            <option value="24">24 Words</option>
+                                        </select>
                                     </div>
-                                    <div class="text-danger entropy-error"></div>
-                               </span>
+                                </div>
                             </div>
                         </div>
                         <div class="form-group">
index db4051bd35fa55777361693c93d59e244addbf86..cd9b37557580bc0eaad85451de24e6cbb4a86fcf 100644 (file)
@@ -96,40 +96,6 @@ window.Entropy = new (function() {
                 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.join(""),
-                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, so is only done for bases
-        // of 2^n, eg octal or hexadecimal
-        if (base.asInt == 16) {
-            var firstInt = base.ints[0];
-            var firstIntBits = firstInt.toString(2).length;
-            var maxFirstIntBits = (base.asInt-1).toString(2).length;
-            var missingFirstIntBits = maxFirstIntBits - firstIntBits;
-            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()
@@ -140,8 +106,17 @@ 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("");
         if (base.asInt == 52) {
             entropyClean = base.parts.join(" ").toUpperCase();
index 45db7b139861814e603903c68c5a583c5dd4fa6e..45f378de867174fb710f54c5c8dd0b9ab93c6a8b 100644 (file)
     DOM.useEntropy = $(".use-entropy");
     DOM.entropyContainer = $(".entropy-container");
     DOM.entropy = $(".entropy");
-    DOM.entropyError = $(".entropy-error");
+    DOM.entropyFeedback = $(".entropy-feedback");
+    DOM.entropyFiltered = DOM.entropyFeedback.find(".filtered");
+    DOM.entropyType = DOM.entropyFeedback.find(".type");
+    DOM.entropyStrength = DOM.entropyFeedback.find(".strength");
+    DOM.entropyEventCount = DOM.entropyFeedback.find(".event-count");
+    DOM.entropyBits = DOM.entropyFeedback.find(".bits");
+    DOM.entropyBitsPerEvent = DOM.entropyFeedback.find(".bits-per-event");
+    DOM.entropyMnemonicLength = DOM.entropyFeedback.find(".mnemonic-length");
     DOM.phrase = $(".phrase");
     DOM.passphrase = $(".passphrase");
     DOM.generateContainer = $(".generate-container");
         // If blank entropy, clear mnemonic, addresses, errors
         if (DOM.entropy.val().trim().length == 0) {
             clearDisplay();
-            hideEntropyError();
+            hideEntropyFeedback();
             DOM.phrase.val("");
             showValidationError("Blank entropy");
             return;
     }
 
     function setMnemonicFromEntropy() {
-        hideEntropyError();
+        hideEntropyFeedback();
         // Get entropy value
         var entropyStr = DOM.entropy.val();
         // Work out minimum base for entropy
             return;
         }
         // Show entropy details
-        var extraBits = 32 - (entropy.binaryStr.length % 32);
-        var extraChars = Math.ceil(extraBits * Math.log(2) / Math.log(entropy.base.asInt));
-        var words = Math.floor(entropy.binaryStr.length / 32) * 3;
-        var strength = "an extremely weak";
-        if (words >= 3) {
-            strength = "a very weak";
-        }
-        if (words >= 6) {
-            strength = "a weak";
-        }
-        if (words >= 9) {
-            strength = "a strong";
-        }
-        if (words >= 12) {
-            strength = "a very strong";
-        }
-        if (words >= 15) {
-            strength = "an extremely strong";
-        }
-        if (words >= 18) {
-            strength = "an even stronger"
-        }
-        var msg = "Have " + entropy.binaryStr.length + " bits of entropy, " + extraChars + " more " + entropy.base.str + " chars required to generate " + strength + " mnemonic: " + entropy.cleanStr;
-        showEntropyError(msg);
+        showEntropyFeedback(entropy);
         // Discard trailing entropy
         var bitsToUse = Math.floor(entropy.binaryStr.length / 32) * 32;
         var binaryStr = entropy.binaryStr.substring(0, bitsToUse);
         DOM.phrase.val(phrase);
     }
 
-    function hideEntropyError() {
-        DOM.entropyError.addClass("hidden");
+    function hideEntropyFeedback() {
+        DOM.entropyFeedback.addClass("hidden");
+        DOM.entropyFiltered.text("");
+        DOM.entropyType.text("");
+        DOM.entropyStrength.text("");
+        DOM.entropyEventCount.text("");
+        DOM.entropyBits.text("");
+        DOM.entropyBitsPerEvent.text("");
     }
 
-    function showEntropyError(msg) {
-        DOM.entropyError.text(msg);
-        DOM.entropyError.removeClass("hidden");
+    function showEntropyFeedback(entropy) {
+        var strength = "extremely weak";
+        if (entropy.binaryStr.length >= 64) {
+            strength = "very weak";
+        }
+        if (entropy.binaryStr.length >= 96) {
+            strength = "weak";
+        }
+        if (entropy.binaryStr.length >= 128) {
+            strength = "strong";
+        }
+        if (entropy.binaryStr.length >= 160) {
+            strength = "very strong";
+        }
+        if (entropy.binaryStr.length >= 192) {
+            strength = "extremely strong";
+        }
+        var bitsStr = entropy.binaryStr.length;
+        if (entropy.base.asInt != 2) {
+            bitsStr += " (" + entropy.binaryStr + ")";
+        }
+        DOM.entropyFiltered.text(entropy.cleanStr);
+        DOM.entropyType.text(entropy.base.str);
+        DOM.entropyStrength.text(strength);
+        DOM.entropyEventCount.text(entropy.base.ints.length);
+        DOM.entropyBits.text(bitsStr);
+        DOM.entropyBitsPerEvent.text(Math.log2(entropy.base.asInt).toFixed(2));
+        DOM.entropyFeedback.removeClass("hidden");
     }
 
     var networks = [
index 102144fad25bb8e7907e431ba92b3cd8eb1d3fae..420873dc11bd2a126df003b5f15d62a08f6734aa 100644 (file)
--- a/tests.js
+++ b/tests.js
@@ -80,7 +80,7 @@ function waitForEntropyFeedback(fn, maxTime) {
         maxTime = testMaxTime;
     }
     var origFeedback = page.evaluate(function() {
-        return $(".entropy-error").text();
+        return $(".entropy-feedback").text();
     });
     var start = new Date().getTime();
     var wait = function keepWaiting() {
@@ -92,11 +92,7 @@ function waitForEntropyFeedback(fn, maxTime) {
             return;
         }
         var feedback = page.evaluate(function() {
-            var feedback = $(".entropy-error");
-            if (feedback.css("display") == "none") {
-                return "";
-            }
-            return feedback.text();
+            return $(".entropy-feedback").text();
         });
         var hasFinished = feedback != origFeedback;
         if (hasFinished) {
@@ -2130,30 +2126,33 @@ page.open(url, function(status) {
         catch (e) {
             return e.message;
         }
-        // Leading zeros are not used for base 6 as binary string
+        // Leading zeros for base 6 as binary string
+        // 20 = 2 events at 2.58 bits per event = 5 bits
+        // 20 in base 6 = 12 in base 10 = 1100 in base 2
+        // so it needs 1 bit of padding to be the right bit length
         try {
-            e = Entropy.fromString("2");
-            if (e.binaryStr != "10") {
+            e = Entropy.fromString("20");
+            if (e.binaryStr != "01100") {
                 return "Base 6 as binary has leading zeros";
             }
         }
         catch (e) {
             return e.message;
         }
-        // Leading zeros are not used for base 10 as binary string
+        // Leading zeros for base 10 as binary string
         try {
-            e = Entropy.fromString("7");
-            if (e.binaryStr != "111") {
+            e = Entropy.fromString("17");
+            if (e.binaryStr != "010001") {
                 return "Base 10 as binary has leading zeros";
             }
         }
         catch (e) {
             return e.message;
         }
-        // Leading zeros are not used for card entropy as binary string
+        // Leading zeros for card entropy as binary string
         try {
             e = Entropy.fromString("2c");
-            if (e.binaryStr != "1") {
+            if (e.binaryStr != "00001") {
                 return "Card entropy as binary has leading zeros";
             }
         }
@@ -2187,18 +2186,18 @@ page.open(url, function(status) {
             var cards = [
                 [ "ac", "00000" ],
                 [ "acac", "00000000000" ],
-                [ "acac2c", "000000000001" ],
+                [ "acac2c", "00000000000000001" ],
                 [ "acks", "00000110011" ],
                 [ "acacks", "00000000000110011" ],
-                [ "2c", "1" ],
-                [ "3d", "1111" ],
+                [ "2c", "00001" ],
+                [ "3d", "01111" ],
                 [ "4h", "11101" ],
                 [ "5s", "101011" ],
-                [ "6c", "101" ],
+                [ "6c", "00101" ],
                 [ "7d", "10011" ],
                 [ "8h", "100001" ],
                 [ "9s", "101111" ],
-                [ "tc", "1001" ],
+                [ "tc", "01001" ],
                 [ "jd", "10111" ],
                 [ "qh", "100101" ],
                 [ "ks", "110011" ],
@@ -2489,7 +2488,7 @@ page.open(url, function(status) {
         [ "7", "3" ], // 7 in base 10 is 111 in base 2, no leading zeros
         [ "8", "4" ],
         [ "F", "4" ],
-        [ "29", "5" ],
+        [ "29", "6" ],
         [ "0A", "8" ],
         [ "1A", "8" ], // hex is always multiple of 4 bits of entropy
         [ "2A", "8" ],
@@ -2499,9 +2498,10 @@ page.open(url, function(status) {
         [ "000A", "16" ],
         [ "5555", "11" ],
         [ "6666", "10" ], // uses dice, so entropy is actually 0000 in base 6, which is 4 lots of 2.58 bits, which is 10.32 bits (rounded down to 10 bits)
-        [ "2227", "12" ],
+        [ "2227", "13" ], // Uses base 10, which is 4 lots of 3.32 bits, which is 13.3 bits (rounded down to 13)
         [ "222F", "16" ],
         [ "FFFF", "16" ],
+        [ "0000101017", "33" ], // 10 events at 3.32 bits per event
     ]
     // use entropy
     page.evaluate(function(e) {
@@ -2518,11 +2518,10 @@ page.open(url, function(status) {
         // check the number of bits of entropy is shown
         waitForEntropyFeedback(function() {
             var entropyText = page.evaluate(function() {
-                return $(".entropy-error").text();
+                return $(".entropy-feedback").text();
             });
-            if (entropyText.indexOf("Have " + expected + " bits of entropy") == -1) {
+            if (entropyText.replace(/\s/g,"").indexOf("Bits" + expected) == -1) {
                 console.log("Accumulated entropy is not shown correctly for " + entropy);
-                console.log(entropyText);
                 fail();
             }
             var isLastTest = i == tests.length - 1;
@@ -2538,28 +2537,6 @@ page.open(url, function(status) {
 });
 },
 
-// The number of bits of entropy to reach the next mnemonic strength is shown
-function() {
-page.open(url, function(status) {
-    // use entropy
-    page.evaluate(function() {
-        $(".use-entropy").prop("checked", true).trigger("change");
-        $(".entropy").val("7654321").trigger("input");
-    });
-    // check the amount of additional entropy required is shown
-    waitForEntropyFeedback(function() {
-        var entropyText = page.evaluate(function() {
-            return $(".entropy-container").text();
-        });
-        if (entropyText.indexOf("3 more base 10 chars required") == -1) {
-            console.log("Additional entropy requirement is not shown");
-            fail();
-        }
-        next();
-    });
-});
-},
-
 // The next strength above 0-word mnemonics is considered extremely weak
 // The next strength above 3-word mnemonics is considered very weak
 // The next strength above 6-word mnemonics is considered weak
@@ -2572,37 +2549,42 @@ page.open(url, function(status) {
         {
             entropy: "A",
             words: 0,
-            nextStrength: "an extremely weak",
+            strength: "extremely weak",
         },
         {
             entropy: "AAAAAAAA",
             words: 3,
-            nextStrength: "a very weak",
+            strength: "extremely weak",
         },
         {
             entropy: "AAAAAAAA B",
             words: 3,
-            nextStrength: "a very weak",
+            strength: "extremely weak",
         },
         {
             entropy: "AAAAAAAA BBBBBBBB",
             words: 6,
-            nextStrength: "a weak",
+            strength: "very weak",
         },
         {
             entropy: "AAAAAAAA BBBBBBBB CCCCCCCC",
             words: 9,
-            nextStrength: "a strong",
+            strength: "weak",
         },
         {
             entropy: "AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD",
             words: 12,
-            nextStrength: "a very strong",
+            strength: "strong",
         },
         {
             entropy: "AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD EEEEEEEE",
             words: 15,
-            nextStrength: "an extremely strong",
+            strength: "very strong",
+        },
+        {
+            entropy: "AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD EEEEEEEE FFFFFFFF",
+            words: 18,
+            strength: "extremely strong",
         }
     ];
     // use entropy
@@ -2621,7 +2603,7 @@ page.open(url, function(status) {
                 return $(".phrase").val();
             });
             if (mnemonic.length > 0) {
-                console.log("Mnemonic length for " + test.nextStrength + " strength is not " + test.words);
+                console.log("Mnemonic length for " + test.strength + " strength is not " + test.words);
                 console.log("Mnemonic: " + mnemonic);
                 fail();
             }
@@ -2635,21 +2617,21 @@ page.open(url, function(status) {
         }
         else {
             waitForGenerate(function() {
-                // check the strength of the current mnemonic
+                // check the number of words in the current mnemonic
                 var mnemonic = page.evaluate(function() {
                     return $(".phrase").val();
                 });
                 if (mnemonic.split(" ").length != test.words) {
-                    console.log("Mnemonic length for " + test.nextStrength + " strength is not " + test.words);
+                    console.log("Mnemonic length for " + test.strength + " strength is not " + test.words);
                     console.log("Mnemonic: " + mnemonic);
                     fail();
                 }
-                // check the strength of the next mnemonic is shown
+                // check the strength of the mnemonic is shown
                 var entropyText = page.evaluate(function() {
                     return $(".entropy-container").text();
                 });
-                if (entropyText.indexOf("required to generate " + test.nextStrength + " mnemonic") == -1) {
-                    console.log("Strength indicator for " + test.nextStrength + " mnemonic is incorrect");
+                if (entropyText.indexOf(test.strength) == -1) {
+                    console.log("Strength indicator for " + test.strength + " mnemonic is incorrect");
                     fail();
                 }
                 var isLastTest = i == tests.length - 1;