Entropy feedback in tabular format, not sentence

This commit is contained in:
Ian Coleman
2016-11-10 10:58:41 +11:00
parent 88df3739e7
commit 1cf1bbaff5
4 changed files with 149 additions and 137 deletions
+11 -36
View File
@@ -96,40 +96,6 @@ window.Entropy = new (function() {
base: base,
};
}
// Pull leading zeros off
var leadingZeros = [];
while (base.ints[0] == "0") {
leadingZeros.push("0");
base.ints.shift();
}
// Convert leading zeros to binary equivalent
var numBinLeadingZeros = Math.floor(Math.log2(base.asInt) * leadingZeros.length);
var binLeadingZeros = "";
for (var i=0; i<numBinLeadingZeros; i++) {
binLeadingZeros += "0";
}
// Handle entropy of zero
if (base.ints.length == 0) {
return {
binaryStr: binLeadingZeros,
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, so is only done for bases
// of 2^n, eg octal or hexadecimal
if (base.asInt == 16) {
var firstInt = base.ints[0];
var firstIntBits = firstInt.toString(2).length;
var maxFirstIntBits = (base.asInt-1).toString(2).length;
var missingFirstIntBits = maxFirstIntBits - firstIntBits;
for (var i=0; i<missingFirstIntBits; i++) {
binLeadingZeros += "0";
}
}
// Convert base.ints to BigInteger.
// Due to using unusual bases, eg cards of base52, this is not as simple as
// using BigInteger.parse()
@@ -140,8 +106,17 @@ window.Entropy = new (function() {
var additionalEntropy = BigInteger.parse(base.asInt).pow(power).multiply(thisInt);
entropyInt = entropyInt.add(additionalEntropy);
}
// Convert entropy to different formats
var entropyBin = binLeadingZeros + entropyInt.toString(2);
// Convert entropy to binary
var entropyBin = entropyInt.toString(2);
// 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 non-2^n bases.
var expectedBits = Math.floor(base.parts.length * Math.log2(base.asInt));
while (entropyBin.length < expectedBits) {
entropyBin = "0" + entropyBin;
}
// Supply a 'filtered' entropy string for display purposes
var entropyClean = base.parts.join("");
if (base.asInt == 52) {
entropyClean = base.parts.join(" ").toUpperCase();
+47 -32
View File
@@ -24,7 +24,14 @@
DOM.useEntropy = $(".use-entropy");
DOM.entropyContainer = $(".entropy-container");
DOM.entropy = $(".entropy");
DOM.entropyError = $(".entropy-error");
DOM.entropyFeedback = $(".entropy-feedback");
DOM.entropyFiltered = DOM.entropyFeedback.find(".filtered");
DOM.entropyType = DOM.entropyFeedback.find(".type");
DOM.entropyStrength = DOM.entropyFeedback.find(".strength");
DOM.entropyEventCount = DOM.entropyFeedback.find(".event-count");
DOM.entropyBits = DOM.entropyFeedback.find(".bits");
DOM.entropyBitsPerEvent = DOM.entropyFeedback.find(".bits-per-event");
DOM.entropyMnemonicLength = DOM.entropyFeedback.find(".mnemonic-length");
DOM.phrase = $(".phrase");
DOM.passphrase = $(".passphrase");
DOM.generateContainer = $(".generate-container");
@@ -153,7 +160,7 @@
// If blank entropy, clear mnemonic, addresses, errors
if (DOM.entropy.val().trim().length == 0) {
clearDisplay();
hideEntropyError();
hideEntropyFeedback();
DOM.phrase.val("");
showValidationError("Blank entropy");
return;
@@ -727,7 +734,7 @@
}
function setMnemonicFromEntropy() {
hideEntropyError();
hideEntropyFeedback();
// Get entropy value
var entropyStr = DOM.entropy.val();
// Work out minimum base for entropy
@@ -736,30 +743,7 @@
return;
}
// Show entropy details
var extraBits = 32 - (entropy.binaryStr.length % 32);
var extraChars = Math.ceil(extraBits * Math.log(2) / Math.log(entropy.base.asInt));
var words = Math.floor(entropy.binaryStr.length / 32) * 3;
var strength = "an extremely weak";
if (words >= 3) {
strength = "a very weak";
}
if (words >= 6) {
strength = "a weak";
}
if (words >= 9) {
strength = "a strong";
}
if (words >= 12) {
strength = "a very strong";
}
if (words >= 15) {
strength = "an extremely strong";
}
if (words >= 18) {
strength = "an even stronger"
}
var msg = "Have " + entropy.binaryStr.length + " bits of entropy, " + extraChars + " more " + entropy.base.str + " chars required to generate " + strength + " mnemonic: " + entropy.cleanStr;
showEntropyError(msg);
showEntropyFeedback(entropy);
// Discard trailing entropy
var bitsToUse = Math.floor(entropy.binaryStr.length / 32) * 32;
var binaryStr = entropy.binaryStr.substring(0, bitsToUse);
@@ -776,13 +760,44 @@
DOM.phrase.val(phrase);
}
function hideEntropyError() {
DOM.entropyError.addClass("hidden");
function hideEntropyFeedback() {
DOM.entropyFeedback.addClass("hidden");
DOM.entropyFiltered.text("");
DOM.entropyType.text("");
DOM.entropyStrength.text("");
DOM.entropyEventCount.text("");
DOM.entropyBits.text("");
DOM.entropyBitsPerEvent.text("");
}
function showEntropyError(msg) {
DOM.entropyError.text(msg);
DOM.entropyError.removeClass("hidden");
function showEntropyFeedback(entropy) {
var strength = "extremely weak";
if (entropy.binaryStr.length >= 64) {
strength = "very weak";
}
if (entropy.binaryStr.length >= 96) {
strength = "weak";
}
if (entropy.binaryStr.length >= 128) {
strength = "strong";
}
if (entropy.binaryStr.length >= 160) {
strength = "very strong";
}
if (entropy.binaryStr.length >= 192) {
strength = "extremely strong";
}
var bitsStr = entropy.binaryStr.length;
if (entropy.base.asInt != 2) {
bitsStr += " (" + entropy.binaryStr + ")";
}
DOM.entropyFiltered.text(entropy.cleanStr);
DOM.entropyType.text(entropy.base.str);
DOM.entropyStrength.text(strength);
DOM.entropyEventCount.text(entropy.base.ints.length);
DOM.entropyBits.text(bitsStr);
DOM.entropyBitsPerEvent.text(Math.log2(entropy.base.asInt).toFixed(2));
DOM.entropyFeedback.removeClass("hidden");
}
var networks = [