aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Coleman <coleman.ian@gmail.com>2016-11-16 11:19:49 +1100
committerIan Coleman <coleman.ian@gmail.com>2016-11-16 11:19:49 +1100
commit02f05d3e467ba8ccf3fca2811eda847fc71e511f (patch)
tree08bbfd6cd083d8b957e86fc36207460fbc69b720
parentb6dbc2a1aea8eeab2d41a4d44f9d7522ecc59a50 (diff)
downloadBIP39-02f05d3e467ba8ccf3fca2811eda847fc71e511f.tar.gz
BIP39-02f05d3e467ba8ccf3fca2811eda847fc71e511f.tar.zst
BIP39-02f05d3e467ba8ccf3fca2811eda847fc71e511f.zip
Entropy strength for cards assumes no replacement
-rw-r--r--src/js/index.js31
-rw-r--r--tests.js1
2 files changed, 31 insertions, 1 deletions
diff --git a/src/js/index.js b/src/js/index.js
index b03f2c8..89f8d0d 100644
--- a/src/js/index.js
+++ b/src/js/index.js
@@ -818,7 +818,7 @@
818 strength = strength + " - " + z.feedback.warning; 818 strength = strength + " - " + z.feedback.warning;
819 }; 819 };
820 } 820 }
821 var bitsStr = entropy.binaryStr.length; 821 var bitsStr = getNumberOfEntropyBits(entropy);
822 var wordCount = Math.floor(entropy.binaryStr.length / 32) * 3; 822 var wordCount = Math.floor(entropy.binaryStr.length / 32) * 3;
823 DOM.entropyFiltered.html(entropy.cleanHtml); 823 DOM.entropyFiltered.html(entropy.cleanHtml);
824 DOM.entropyType.text(entropy.base.str); 824 DOM.entropyType.text(entropy.base.str);
@@ -830,6 +830,35 @@
830 DOM.entropyBitsPerEvent.text(Math.log2(entropy.base.asInt).toFixed(2)); 830 DOM.entropyBitsPerEvent.text(Math.log2(entropy.base.asInt).toFixed(2));
831 } 831 }
832 832
833 function getNumberOfEntropyBits(entropy) {
834 var bitsStr = entropy.binaryStr.length.toString();
835 // If using cards, assume they are not reused, thus additional entropy
836 // decreases as more cards are used. This means entropy is measured
837 // using n!, not base^n.
838 // eg the second last card can be only one of two, not one of fifty two
839 // so the added entropy for that card is only one bit at most
840 if (entropy.base.asInt == 52) {
841 var totalCombos = factorial(52);
842 var remainingCards = 52 - entropy.base.parts.length;
843 var remainingCombos = factorial(remainingCards);
844 var currentCombos = totalCombos.divide(remainingCombos);
845 bitsStr = currentCombos.toString(2).length.toString();
846 }
847 return bitsStr
848 }
849
850 // Depends on BigInteger
851 function factorial(n) {
852 if (n == 0) {
853 return 1;
854 }
855 f = BigInteger.ONE;
856 for (var i=1; i<=n; i++) {
857 f = f.multiply(new BigInteger(i));
858 }
859 return f;
860 }
861
833 var networks = [ 862 var networks = [
834 { 863 {
835 name: "Bitcoin", 864 name: "Bitcoin",
diff --git a/tests.js b/tests.js
index 9abe573..9760d93 100644
--- a/tests.js
+++ b/tests.js
@@ -2503,6 +2503,7 @@ page.open(url, function(status) {
2503 [ "222F", "16" ], 2503 [ "222F", "16" ],
2504 [ "FFFF", "16" ], 2504 [ "FFFF", "16" ],
2505 [ "0000101017", "33" ], // 10 events at 3.32 bits per event 2505 [ "0000101017", "33" ], // 10 events at 3.32 bits per event
2506 [ "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks", "226" ], // cards are not replaced, so a full deck is not 52^52 entropy which is 296 bits, it's 52!, which is 226 bits
2506 ] 2507 ]
2507 // use entropy 2508 // use entropy
2508 page.evaluate(function(e) { 2509 page.evaluate(function(e) {