diff options
author | Ian Coleman <coleman.ian@gmail.com> | 2016-11-08 21:59:08 +1100 |
---|---|---|
committer | Ian Coleman <coleman.ian@gmail.com> | 2016-11-08 21:59:08 +1100 |
commit | 0d0f07f9374078ba71cf7f81cd6c2ab8df8d4693 (patch) | |
tree | 0c4c900044065e6b4838375619d881145bdd2064 /src | |
parent | 425b75a925717eb6f9813503569592a8160c5f34 (diff) | |
download | BIP39-0d0f07f9374078ba71cf7f81cd6c2ab8df8d4693.tar.gz BIP39-0d0f07f9374078ba71cf7f81cd6c2ab8df8d4693.tar.zst BIP39-0d0f07f9374078ba71cf7f81cd6c2ab8df8d4693.zip |
No leading zeros for first char unless hex
Diffstat (limited to 'src')
-rw-r--r-- | src/js/entropy.js | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/src/js/entropy.js b/src/js/entropy.js index 8e29d40..c804fda 100644 --- a/src/js/entropy.js +++ b/src/js/entropy.js | |||
@@ -71,17 +71,21 @@ window.Entropy = new (function() { | |||
71 | // This is done by changing all 6s to 0s | 71 | // This is done by changing all 6s to 0s |
72 | if (base.str == "dice") { | 72 | if (base.str == "dice") { |
73 | var newRawEntropyStr = ""; | 73 | var newRawEntropyStr = ""; |
74 | var newInts = []; | ||
74 | for (var i=0; i<rawEntropyStr.length; i++) { | 75 | for (var i=0; i<rawEntropyStr.length; i++) { |
75 | var c = rawEntropyStr[i]; | 76 | var c = rawEntropyStr[i]; |
76 | if ("12345".indexOf(c) > -1) { | 77 | if ("12345".indexOf(c) > -1) { |
77 | newRawEntropyStr += c; | 78 | newRawEntropyStr += c; |
79 | newInts[i] = base.ints[i]; | ||
78 | } | 80 | } |
79 | else { | 81 | else { |
80 | newRawEntropyStr += "0"; | 82 | newRawEntropyStr += "0"; |
83 | newInts[i] = 0; | ||
81 | } | 84 | } |
82 | } | 85 | } |
83 | rawEntropyStr = newRawEntropyStr; | 86 | rawEntropyStr = newRawEntropyStr; |
84 | base.str = "base 6 (dice)"; | 87 | base.str = "base 6 (dice)"; |
88 | base.ints = newInts; | ||
85 | base.parts = matchers.base6(rawEntropyStr); | 89 | base.parts = matchers.base6(rawEntropyStr); |
86 | base.matcher = matchers.base6; | 90 | base.matcher = matchers.base6; |
87 | } | 91 | } |
@@ -109,25 +113,23 @@ window.Entropy = new (function() { | |||
109 | if (base.ints.length == 0) { | 113 | if (base.ints.length == 0) { |
110 | return { | 114 | return { |
111 | binaryStr: binLeadingZeros, | 115 | binaryStr: binLeadingZeros, |
112 | cleanStr: leadingZeros, | 116 | cleanStr: leadingZeros.join(""), |
113 | base: base, | 117 | base: base, |
114 | } | 118 | } |
115 | } | 119 | } |
116 | // If the first integer is small, it must be padded with zeros. | 120 | // If the first integer is small, it must be padded with zeros. |
117 | // Otherwise the chance of the first bit being 1 is 100%, which is | 121 | // Otherwise the chance of the first bit being 1 is 100%, which is |
118 | // obviously incorrect. | 122 | // obviously incorrect. |
119 | // This is not perfect for unusual bases, eg base 6 has 2.6 bits, so is | 123 | // This is not perfect for unusual bases, so is only done for bases |
120 | // slightly biased toward having leading zeros, but it's still better | 124 | // of 2^n, eg octal or hexadecimal |
121 | // than ignoring it completely. | 125 | if (base.asInt == 16) { |
122 | // TODO: revise this, it seems very fishy. For example, in base 10, there are | 126 | var firstInt = base.ints[0]; |
123 | // 8 opportunities to start with 0 but only 2 to start with 1 | 127 | var firstIntBits = firstInt.toString(2).length; |
124 | var firstInt = base.ints[0]; | 128 | var maxFirstIntBits = (base.asInt-1).toString(2).length; |
125 | var firstIntBits = Math.floor(Math.log2(firstInt))+1; | 129 | var missingFirstIntBits = maxFirstIntBits - firstIntBits; |
126 | var maxFirstIntBits = Math.floor(Math.log2(base.asInt-1))+1; | 130 | for (var i=0; i<missingFirstIntBits; i++) { |
127 | var missingFirstIntBits = maxFirstIntBits - firstIntBits; | 131 | binLeadingZeros += "0"; |
128 | var firstIntLeadingZeros = ""; | 132 | } |
129 | for (var i=0; i<missingFirstIntBits; i++) { | ||
130 | binLeadingZeros += "0"; | ||
131 | } | 133 | } |
132 | // Convert base.ints to BigInteger. | 134 | // Convert base.ints to BigInteger. |
133 | // Due to using unusual bases, eg cards of base52, this is not as simple as | 135 | // Due to using unusual bases, eg cards of base52, this is not as simple as |