diff options
author | Ian Coleman <coleman.ian@gmail.com> | 2016-11-10 10:58:41 +1100 |
---|---|---|
committer | Ian Coleman <coleman.ian@gmail.com> | 2016-11-10 17:53:09 +1100 |
commit | 1cf1bbaff575e647ef38b091a00edfa2e1ce024d (patch) | |
tree | 7d4cf3e19b24916d07444440b43ee506ce8882a4 /src/js/entropy.js | |
parent | 88df3739e7e8b26b461a73fe3874b195e5b03fec (diff) | |
download | BIP39-1cf1bbaff575e647ef38b091a00edfa2e1ce024d.tar.gz BIP39-1cf1bbaff575e647ef38b091a00edfa2e1ce024d.tar.zst BIP39-1cf1bbaff575e647ef38b091a00edfa2e1ce024d.zip |
Entropy feedback in tabular format, not sentence
Diffstat (limited to 'src/js/entropy.js')
-rw-r--r-- | src/js/entropy.js | 47 |
1 files changed, 11 insertions, 36 deletions
diff --git a/src/js/entropy.js b/src/js/entropy.js index db4051b..cd9b375 100644 --- a/src/js/entropy.js +++ b/src/js/entropy.js | |||
@@ -96,40 +96,6 @@ window.Entropy = new (function() { | |||
96 | base: base, | 96 | base: base, |
97 | }; | 97 | }; |
98 | } | 98 | } |
99 | // Pull leading zeros off | ||
100 | var leadingZeros = []; | ||
101 | while (base.ints[0] == "0") { | ||
102 | leadingZeros.push("0"); | ||
103 | base.ints.shift(); | ||
104 | } | ||
105 | // Convert leading zeros to binary equivalent | ||
106 | var numBinLeadingZeros = Math.floor(Math.log2(base.asInt) * leadingZeros.length); | ||
107 | var binLeadingZeros = ""; | ||
108 | for (var i=0; i<numBinLeadingZeros; i++) { | ||
109 | binLeadingZeros += "0"; | ||
110 | } | ||
111 | // Handle entropy of zero | ||
112 | if (base.ints.length == 0) { | ||
113 | return { | ||
114 | binaryStr: binLeadingZeros, | ||
115 | cleanStr: leadingZeros.join(""), | ||
116 | base: base, | ||
117 | } | ||
118 | } | ||
119 | // If the first integer is small, it must be padded with zeros. | ||
120 | // Otherwise the chance of the first bit being 1 is 100%, which is | ||
121 | // obviously incorrect. | ||
122 | // This is not perfect for unusual bases, so is only done for bases | ||
123 | // of 2^n, eg octal or hexadecimal | ||
124 | if (base.asInt == 16) { | ||
125 | var firstInt = base.ints[0]; | ||
126 | var firstIntBits = firstInt.toString(2).length; | ||
127 | var maxFirstIntBits = (base.asInt-1).toString(2).length; | ||
128 | var missingFirstIntBits = maxFirstIntBits - firstIntBits; | ||
129 | for (var i=0; i<missingFirstIntBits; i++) { | ||
130 | binLeadingZeros += "0"; | ||
131 | } | ||
132 | } | ||
133 | // Convert base.ints to BigInteger. | 99 | // Convert base.ints to BigInteger. |
134 | // Due to using unusual bases, eg cards of base52, this is not as simple as | 100 | // Due to using unusual bases, eg cards of base52, this is not as simple as |
135 | // using BigInteger.parse() | 101 | // using BigInteger.parse() |
@@ -140,8 +106,17 @@ window.Entropy = new (function() { | |||
140 | var additionalEntropy = BigInteger.parse(base.asInt).pow(power).multiply(thisInt); | 106 | var additionalEntropy = BigInteger.parse(base.asInt).pow(power).multiply(thisInt); |
141 | entropyInt = entropyInt.add(additionalEntropy); | 107 | entropyInt = entropyInt.add(additionalEntropy); |
142 | } | 108 | } |
143 | // Convert entropy to different formats | 109 | // Convert entropy to binary |
144 | var entropyBin = binLeadingZeros + entropyInt.toString(2); | 110 | var entropyBin = entropyInt.toString(2); |
111 | // If the first integer is small, it must be padded with zeros. | ||
112 | // Otherwise the chance of the first bit being 1 is 100%, which is | ||
113 | // obviously incorrect. | ||
114 | // This is not perfect for non-2^n bases. | ||
115 | var expectedBits = Math.floor(base.parts.length * Math.log2(base.asInt)); | ||
116 | while (entropyBin.length < expectedBits) { | ||
117 | entropyBin = "0" + entropyBin; | ||
118 | } | ||
119 | // Supply a 'filtered' entropy string for display purposes | ||
145 | var entropyClean = base.parts.join(""); | 120 | var entropyClean = base.parts.join(""); |
146 | if (base.asInt == 52) { | 121 | if (base.asInt == 52) { |
147 | entropyClean = base.parts.join(" ").toUpperCase(); | 122 | entropyClean = base.parts.join(" ").toUpperCase(); |