X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2Fjs%2Fentropy.js;h=db4051bd35fa55777361693c93d59e244addbf86;hb=88df3739e7e8b26b461a73fe3874b195e5b03fec;hp=92300afa352f27b48a1f600705fe5e04fe1383bc;hpb=adc8ce127d4f8ea0d7e5ede6a82c2791d60ff4d2;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git diff --git a/src/js/entropy.js b/src/js/entropy.js index 92300af..db4051b 100644 --- a/src/js/entropy.js +++ b/src/js/entropy.js @@ -68,20 +68,24 @@ window.Entropy = new (function() { // Find type of entropy being used (binary, hex, dice etc) var base = getBase(rawEntropyStr); // Convert dice to base6 entropy (ie 1-6 to 0-5) + // This is done by changing all 6s to 0s if (base.str == "dice") { - var newRawEntropyStr = ""; - for (var i=0; i -1) { - newRawEntropyStr += (parseInt(c) - 1).toString(); + var newParts = []; + var newInts = []; + for (var i=0; i -1) { + newParts[i] = base.parts[i]; + newInts[i] = base.ints[i]; } else { - newRawEntropyStr += c + newParts[i] = "0"; + newInts[i] = 0; } } - rawEntropyStr = newRawEntropyStr; base.str = "base 6 (dice)"; - base.parts = matchers.base6(rawEntropyStr); + base.ints = newInts; + base.parts = newParts; base.matcher = matchers.base6; } // Detect empty entropy @@ -108,25 +112,23 @@ window.Entropy = new (function() { if (base.ints.length == 0) { return { binaryStr: binLeadingZeros, - cleanStr: leadingZeros, + cleanStr: leadingZeros.join(""), base: base, } } // 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 // obviously incorrect. - // This is not perfect for unusual bases, eg base 6 has 2.6 bits, so is - // slightly biased toward having leading zeros, but it's still better - // than ignoring it completely. - // TODO: revise this, it seems very fishy. For example, in base 10, there are - // 8 opportunities to start with 0 but only 2 to start with 1 - var firstInt = base.ints[0]; - var firstIntBits = Math.floor(Math.log2(firstInt))+1; - var maxFirstIntBits = Math.floor(Math.log2(base.asInt-1))+1; - var missingFirstIntBits = maxFirstIntBits - firstIntBits; - var firstIntLeadingZeros = ""; - for (var i=0; i