No leading zeros for first char unless hex

This commit is contained in:
Ian Coleman
2016-11-08 21:59:08 +11:00
parent 425b75a925
commit 0d0f07f937
2 changed files with 54 additions and 32 deletions

View File

@@ -71,17 +71,21 @@ window.Entropy = new (function() {
// This is done by changing all 6s to 0s // This is done by changing all 6s to 0s
if (base.str == "dice") { if (base.str == "dice") {
var newRawEntropyStr = ""; var newRawEntropyStr = "";
var newInts = [];
for (var i=0; i<rawEntropyStr.length; i++) { for (var i=0; i<rawEntropyStr.length; i++) {
var c = rawEntropyStr[i]; var c = rawEntropyStr[i];
if ("12345".indexOf(c) > -1) { if ("12345".indexOf(c) > -1) {
newRawEntropyStr += c; newRawEntropyStr += c;
newInts[i] = base.ints[i];
} }
else { else {
newRawEntropyStr += "0"; newRawEntropyStr += "0";
newInts[i] = 0;
} }
} }
rawEntropyStr = newRawEntropyStr; rawEntropyStr = newRawEntropyStr;
base.str = "base 6 (dice)"; base.str = "base 6 (dice)";
base.ints = newInts;
base.parts = matchers.base6(rawEntropyStr); base.parts = matchers.base6(rawEntropyStr);
base.matcher = matchers.base6; base.matcher = matchers.base6;
} }
@@ -109,25 +113,23 @@ window.Entropy = new (function() {
if (base.ints.length == 0) { if (base.ints.length == 0) {
return { return {
binaryStr: binLeadingZeros, binaryStr: binLeadingZeros,
cleanStr: leadingZeros, cleanStr: leadingZeros.join(""),
base: base, base: base,
} }
} }
// If the first integer is small, it must be padded with zeros. // If the first integer is small, it must be padded with zeros.
// Otherwise the chance of the first bit being 1 is 100%, which is // Otherwise the chance of the first bit being 1 is 100%, which is
// obviously incorrect. // obviously incorrect.
// This is not perfect for unusual bases, eg base 6 has 2.6 bits, so is // This is not perfect for unusual bases, so is only done for bases
// slightly biased toward having leading zeros, but it's still better // of 2^n, eg octal or hexadecimal
// than ignoring it completely. if (base.asInt == 16) {
// TODO: revise this, it seems very fishy. For example, in base 10, there are var firstInt = base.ints[0];
// 8 opportunities to start with 0 but only 2 to start with 1 var firstIntBits = firstInt.toString(2).length;
var firstInt = base.ints[0]; var maxFirstIntBits = (base.asInt-1).toString(2).length;
var firstIntBits = Math.floor(Math.log2(firstInt))+1; var missingFirstIntBits = maxFirstIntBits - firstIntBits;
var maxFirstIntBits = Math.floor(Math.log2(base.asInt-1))+1; for (var i=0; i<missingFirstIntBits; i++) {
var missingFirstIntBits = maxFirstIntBits - firstIntBits; binLeadingZeros += "0";
var firstIntLeadingZeros = ""; }
for (var i=0; i<missingFirstIntBits; i++) {
binLeadingZeros += "0";
} }
// Convert base.ints to BigInteger. // Convert base.ints to BigInteger.
// Due to using unusual bases, eg cards of base52, this is not as simple as // Due to using unusual bases, eg cards of base52, this is not as simple as

View File

@@ -2130,11 +2130,31 @@ page.open(url, function(status) {
catch (e) { catch (e) {
return e.message; return e.message;
} }
// Leading zeros are correctly preserved for base 6 in binary string // Leading zeros are not used for base 6 as binary string
try { try {
e = Entropy.fromString("2"); e = Entropy.fromString("2");
if (e.binaryStr != "010") { if (e.binaryStr != "10") {
return "Base 6 leading zeros are not correct in binary"; return "Base 6 as binary has leading zeros";
}
}
catch (e) {
return e.message;
}
// Leading zeros are not used for base 10 as binary string
try {
e = Entropy.fromString("7");
if (e.binaryStr != "111") {
return "Base 10 as binary has leading zeros";
}
}
catch (e) {
return e.message;
}
// Leading zeros are not used for card entropy as binary string
try {
e = Entropy.fromString("2c");
if (e.binaryStr != "1") {
return "Card entropy as binary has leading zeros";
} }
} }
catch (e) { catch (e) {
@@ -2167,19 +2187,19 @@ page.open(url, function(status) {
var cards = [ var cards = [
[ "ac", "00000" ], [ "ac", "00000" ],
[ "acac", "00000000000" ], [ "acac", "00000000000" ],
[ "acac2c", "00000000000000001" ], [ "acac2c", "000000000001" ],
[ "acks", "00000110011" ], [ "acks", "00000110011" ],
[ "acacks", "00000000000110011" ], [ "acacks", "00000000000110011" ],
[ "2c", "000001" ], [ "2c", "1" ],
[ "3d", "001111" ], [ "3d", "1111" ],
[ "4h", "011101" ], [ "4h", "11101" ],
[ "5s", "101011" ], [ "5s", "101011" ],
[ "6c", "000101" ], [ "6c", "101" ],
[ "7d", "010011" ], [ "7d", "10011" ],
[ "8h", "100001" ], [ "8h", "100001" ],
[ "9s", "101111" ], [ "9s", "101111" ],
[ "tc", "001001" ], [ "tc", "1001" ],
[ "jd", "010111" ], [ "jd", "10111" ],
[ "qh", "100101" ], [ "qh", "100101" ],
[ "ks", "110011" ], [ "ks", "110011" ],
[ "ks2c", "101001011101" ], [ "ks2c", "101001011101" ],
@@ -2465,11 +2485,11 @@ page.open(url, function(status) {
[ "0000 0000 0000 0000 0000", "20" ], [ "0000 0000 0000 0000 0000", "20" ],
[ "0", "1" ], [ "0", "1" ],
[ "0000", "4" ], [ "0000", "4" ],
[ "6", "3" ], [ "6", "2" ], // 6 in card is 0 in base 6, 0 in base 6 is 2.6 bits (rounded down to 2 bits)
[ "7", "4" ], [ "7", "3" ], // 7 in base 10 is 111 in base 2, no leading zeros
[ "8", "4" ], [ "8", "4" ],
[ "F", "4" ], [ "F", "4" ],
[ "29", "7" ], [ "29", "5" ],
[ "0A", "8" ], [ "0A", "8" ],
[ "1A", "8" ], // hex is always multiple of 4 bits of entropy [ "1A", "8" ], // hex is always multiple of 4 bits of entropy
[ "2A", "8" ], [ "2A", "8" ],
@@ -2477,9 +2497,9 @@ page.open(url, function(status) {
[ "8A", "8" ], [ "8A", "8" ],
[ "FA", "8" ], [ "FA", "8" ],
[ "000A", "16" ], [ "000A", "16" ],
[ "2220", "11" ], [ "5555", "11" ],
[ "2221", "11" ], // uses dice, so entropy is actually 1110 [ "6666", "10" ], // uses dice, so entropy is actually 0000 in base 6, which is 4 lots of 2.58 bits, which is 10.32 bits (rounded down to 10 bits)
[ "2227", "14" ], [ "2227", "12" ],
[ "222F", "16" ], [ "222F", "16" ],
[ "FFFF", "16" ], [ "FFFF", "16" ],
] ]
@@ -2710,11 +2730,11 @@ page.open(url, function(status) {
// base 20 // base 20
function() { function() {
page.open(url, function(status) { page.open(url, function(status) {
var expected = "defy trip fatal jaguar mean rack rifle survey satisfy drift twist champion steel wife state furnace night consider glove olympic oblige donor novel left"; var expected = "train then jungle barely whip fiber purpose puppy eagle cloud clump hospital robot brave balcony utility detect estate old green desk skill multiply virus";
// use entropy // use entropy
page.evaluate(function() { page.evaluate(function() {
$(".use-entropy").prop("checked", true).trigger("change"); $(".use-entropy").prop("checked", true).trigger("change");
var entropy = "123450123450123450123450123450123450123450123450123450123450123450123450123450123450123450123450123"; var entropy = "543210543210543210543210543210543210543210543210543210543210543210543210543210543210543210543210543";
$(".entropy").val(entropy).trigger("input"); $(".entropy").val(entropy).trigger("input");
}); });
// check the mnemonic matches the expected value from bip32jp // check the mnemonic matches the expected value from bip32jp