diff options
author | Ian Coleman <coleman.ian@gmail.com> | 2016-11-30 18:55:12 +1100 |
---|---|---|
committer | Ian Coleman <coleman.ian@gmail.com> | 2016-11-30 18:55:12 +1100 |
commit | 94959756408aa60be4118ab1dceff4e71c6afdbf (patch) | |
tree | 8be863b99fd20341993d2afe0c70e16303157b06 | |
parent | dedae1a9d604773143d13b496feeafd7b85084f4 (diff) | |
download | BIP39-94959756408aa60be4118ab1dceff4e71c6afdbf.tar.gz BIP39-94959756408aa60be4118ab1dceff4e71c6afdbf.tar.zst BIP39-94959756408aa60be4118ab1dceff4e71c6afdbf.zip |
bitsPerEvent correct for multiple decks of cards
-rw-r--r-- | bip39-standalone.html | 21 | ||||
-rw-r--r-- | src/js/entropy.js | 16 | ||||
-rw-r--r-- | src/js/index.js | 5 |
3 files changed, 28 insertions, 14 deletions
diff --git a/bip39-standalone.html b/bip39-standalone.html index e433d31..4bad422 100644 --- a/bip39-standalone.html +++ b/bip39-standalone.html | |||
@@ -18324,9 +18324,13 @@ window.Entropy = new (function() { | |||
18324 | while (entropyBin.length < expectedBits) { | 18324 | while (entropyBin.length < expectedBits) { |
18325 | entropyBin = "0" + entropyBin; | 18325 | entropyBin = "0" + entropyBin; |
18326 | } | 18326 | } |
18327 | // Calculate the number of bits per event | ||
18328 | var bitsPerEvent = Math.log2(base.asInt); | ||
18327 | // Cards binary must be handled differently, since they're not replaced | 18329 | // Cards binary must be handled differently, since they're not replaced |
18328 | if (base.asInt == 52) { | 18330 | if (base.asInt == 52) { |
18329 | entropyBin = getCardBinary(base.parts); | 18331 | var cardEntropy = processCardEntropy(base.parts); |
18332 | entropyBin = cardEntropy.binaryStr; | ||
18333 | bitsPerEvent = cardEntropy.bitsPerEvent; | ||
18330 | } | 18334 | } |
18331 | // Supply a 'filtered' entropy string for display purposes | 18335 | // Supply a 'filtered' entropy string for display purposes |
18332 | var entropyClean = base.parts.join(""); | 18336 | var entropyClean = base.parts.join(""); |
@@ -18348,6 +18352,7 @@ window.Entropy = new (function() { | |||
18348 | binaryStr: entropyBin, | 18352 | binaryStr: entropyBin, |
18349 | cleanStr: entropyClean, | 18353 | cleanStr: entropyClean, |
18350 | cleanHtml: entropyHtml, | 18354 | cleanHtml: entropyHtml, |
18355 | bitsPerEvent: bitsPerEvent, | ||
18351 | base: base, | 18356 | base: base, |
18352 | } | 18357 | } |
18353 | return e; | 18358 | return e; |
@@ -18440,7 +18445,7 @@ window.Entropy = new (function() { | |||
18440 | // total possible entropy is measured using n!, not base^n. | 18445 | // total possible entropy is measured using n!, not base^n. |
18441 | // eg the second last card can be only one of two, not one of fifty two | 18446 | // eg the second last card can be only one of two, not one of fifty two |
18442 | // so the added entropy for that card is only one bit at most | 18447 | // so the added entropy for that card is only one bit at most |
18443 | function getCardBinary(cards) { | 18448 | function processCardEntropy(cards) { |
18444 | // Track how many instances of each card have been used, and thus | 18449 | // Track how many instances of each card have been used, and thus |
18445 | // how many decks are in use. | 18450 | // how many decks are in use. |
18446 | var cardCounts = {}; | 18451 | var cardCounts = {}; |
@@ -18509,7 +18514,12 @@ window.Entropy = new (function() { | |||
18509 | } | 18514 | } |
18510 | // Truncate to the appropriate number of bits. | 18515 | // Truncate to the appropriate number of bits. |
18511 | entropyBin = entropyBin.substring(0, numberOfBits); | 18516 | entropyBin = entropyBin.substring(0, numberOfBits); |
18512 | return entropyBin; | 18517 | // Get the number of bits per event |
18518 | bitsPerEvent = maxBits / totalCards; | ||
18519 | return { | ||
18520 | binaryStr: entropyBin, | ||
18521 | bitsPerEvent: bitsPerEvent, | ||
18522 | } | ||
18513 | } | 18523 | } |
18514 | 18524 | ||
18515 | // Polyfill for Math.log2 | 18525 | // Polyfill for Math.log2 |
@@ -19366,10 +19376,7 @@ window.Entropy = new (function() { | |||
19366 | } | 19376 | } |
19367 | var entropyTypeStr = getEntropyTypeStr(entropy); | 19377 | var entropyTypeStr = getEntropyTypeStr(entropy); |
19368 | var wordCount = Math.floor(numberOfBits / 32) * 3; | 19378 | var wordCount = Math.floor(numberOfBits / 32) * 3; |
19369 | var bitsPerEvent = Math.log2(entropy.base.asInt).toFixed(2); | 19379 | var bitsPerEvent = entropy.bitsPerEvent.toFixed(2); |
19370 | if (entropy.base.asInt == 52) { | ||
19371 | bitsPerEvent = (4.3381).toFixed(2); // log2(52! / 52) | ||
19372 | } | ||
19373 | DOM.entropyFiltered.html(entropy.cleanHtml); | 19380 | DOM.entropyFiltered.html(entropy.cleanHtml); |
19374 | DOM.entropyType.text(entropyTypeStr); | 19381 | DOM.entropyType.text(entropyTypeStr); |
19375 | DOM.entropyStrength.text(strength); | 19382 | DOM.entropyStrength.text(strength); |
diff --git a/src/js/entropy.js b/src/js/entropy.js index d04d861..b7274bb 100644 --- a/src/js/entropy.js +++ b/src/js/entropy.js | |||
@@ -120,9 +120,13 @@ window.Entropy = new (function() { | |||
120 | while (entropyBin.length < expectedBits) { | 120 | while (entropyBin.length < expectedBits) { |
121 | entropyBin = "0" + entropyBin; | 121 | entropyBin = "0" + entropyBin; |
122 | } | 122 | } |
123 | // Calculate the number of bits per event | ||
124 | var bitsPerEvent = Math.log2(base.asInt); | ||
123 | // Cards binary must be handled differently, since they're not replaced | 125 | // Cards binary must be handled differently, since they're not replaced |
124 | if (base.asInt == 52) { | 126 | if (base.asInt == 52) { |
125 | entropyBin = getCardBinary(base.parts); | 127 | var cardEntropy = processCardEntropy(base.parts); |
128 | entropyBin = cardEntropy.binaryStr; | ||
129 | bitsPerEvent = cardEntropy.bitsPerEvent; | ||
126 | } | 130 | } |
127 | // Supply a 'filtered' entropy string for display purposes | 131 | // Supply a 'filtered' entropy string for display purposes |
128 | var entropyClean = base.parts.join(""); | 132 | var entropyClean = base.parts.join(""); |
@@ -144,6 +148,7 @@ window.Entropy = new (function() { | |||
144 | binaryStr: entropyBin, | 148 | binaryStr: entropyBin, |
145 | cleanStr: entropyClean, | 149 | cleanStr: entropyClean, |
146 | cleanHtml: entropyHtml, | 150 | cleanHtml: entropyHtml, |
151 | bitsPerEvent: bitsPerEvent, | ||
147 | base: base, | 152 | base: base, |
148 | } | 153 | } |
149 | return e; | 154 | return e; |
@@ -236,7 +241,7 @@ window.Entropy = new (function() { | |||
236 | // total possible entropy is measured using n!, not base^n. | 241 | // total possible entropy is measured using n!, not base^n. |
237 | // eg the second last card can be only one of two, not one of fifty two | 242 | // eg the second last card can be only one of two, not one of fifty two |
238 | // so the added entropy for that card is only one bit at most | 243 | // so the added entropy for that card is only one bit at most |
239 | function getCardBinary(cards) { | 244 | function processCardEntropy(cards) { |
240 | // Track how many instances of each card have been used, and thus | 245 | // Track how many instances of each card have been used, and thus |
241 | // how many decks are in use. | 246 | // how many decks are in use. |
242 | var cardCounts = {}; | 247 | var cardCounts = {}; |
@@ -305,7 +310,12 @@ window.Entropy = new (function() { | |||
305 | } | 310 | } |
306 | // Truncate to the appropriate number of bits. | 311 | // Truncate to the appropriate number of bits. |
307 | entropyBin = entropyBin.substring(0, numberOfBits); | 312 | entropyBin = entropyBin.substring(0, numberOfBits); |
308 | return entropyBin; | 313 | // Get the number of bits per event |
314 | bitsPerEvent = maxBits / totalCards; | ||
315 | return { | ||
316 | binaryStr: entropyBin, | ||
317 | bitsPerEvent: bitsPerEvent, | ||
318 | } | ||
309 | } | 319 | } |
310 | 320 | ||
311 | // Polyfill for Math.log2 | 321 | // Polyfill for Math.log2 |
diff --git a/src/js/index.js b/src/js/index.js index 12788ab..82e2161 100644 --- a/src/js/index.js +++ b/src/js/index.js | |||
@@ -828,10 +828,7 @@ | |||
828 | } | 828 | } |
829 | var entropyTypeStr = getEntropyTypeStr(entropy); | 829 | var entropyTypeStr = getEntropyTypeStr(entropy); |
830 | var wordCount = Math.floor(numberOfBits / 32) * 3; | 830 | var wordCount = Math.floor(numberOfBits / 32) * 3; |
831 | var bitsPerEvent = Math.log2(entropy.base.asInt).toFixed(2); | 831 | var bitsPerEvent = entropy.bitsPerEvent.toFixed(2); |
832 | if (entropy.base.asInt == 52) { | ||
833 | bitsPerEvent = (4.3381).toFixed(2); // log2(52! / 52) | ||
834 | } | ||
835 | DOM.entropyFiltered.html(entropy.cleanHtml); | 832 | DOM.entropyFiltered.html(entropy.cleanHtml); |
836 | DOM.entropyType.text(entropyTypeStr); | 833 | DOM.entropyType.text(entropyTypeStr); |
837 | DOM.entropyStrength.text(strength); | 834 | DOM.entropyStrength.text(strength); |