aboutsummaryrefslogtreecommitdiff
path: root/src/js/entropy.js
diff options
context:
space:
mode:
authorIan Coleman <coleman.ian@gmail.com>2016-11-30 18:55:12 +1100
committerIan Coleman <coleman.ian@gmail.com>2016-11-30 18:55:12 +1100
commit94959756408aa60be4118ab1dceff4e71c6afdbf (patch)
tree8be863b99fd20341993d2afe0c70e16303157b06 /src/js/entropy.js
parentdedae1a9d604773143d13b496feeafd7b85084f4 (diff)
downloadBIP39-94959756408aa60be4118ab1dceff4e71c6afdbf.tar.gz
BIP39-94959756408aa60be4118ab1dceff4e71c6afdbf.tar.zst
BIP39-94959756408aa60be4118ab1dceff4e71c6afdbf.zip
bitsPerEvent correct for multiple decks of cards
Diffstat (limited to 'src/js/entropy.js')
-rw-r--r--src/js/entropy.js16
1 files changed, 13 insertions, 3 deletions
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