From 02f05d3e467ba8ccf3fca2811eda847fc71e511f Mon Sep 17 00:00:00 2001 From: Ian Coleman Date: Wed, 16 Nov 2016 11:19:49 +1100 Subject: [PATCH] Entropy strength for cards assumes no replacement --- src/js/index.js | 31 ++++++++++++++++++++++++++++++- tests.js | 1 + 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/js/index.js b/src/js/index.js index b03f2c8..89f8d0d 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -818,7 +818,7 @@ strength = strength + " - " + z.feedback.warning; }; } - var bitsStr = entropy.binaryStr.length; + var bitsStr = getNumberOfEntropyBits(entropy); var wordCount = Math.floor(entropy.binaryStr.length / 32) * 3; DOM.entropyFiltered.html(entropy.cleanHtml); DOM.entropyType.text(entropy.base.str); @@ -830,6 +830,35 @@ DOM.entropyBitsPerEvent.text(Math.log2(entropy.base.asInt).toFixed(2)); } + function getNumberOfEntropyBits(entropy) { + var bitsStr = entropy.binaryStr.length.toString(); + // If using cards, assume they are not reused, thus additional entropy + // decreases as more cards are used. This means entropy is measured + // using n!, not base^n. + // eg the second last card can be only one of two, not one of fifty two + // so the added entropy for that card is only one bit at most + if (entropy.base.asInt == 52) { + var totalCombos = factorial(52); + var remainingCards = 52 - entropy.base.parts.length; + var remainingCombos = factorial(remainingCards); + var currentCombos = totalCombos.divide(remainingCombos); + bitsStr = currentCombos.toString(2).length.toString(); + } + return bitsStr + } + + // Depends on BigInteger + function factorial(n) { + if (n == 0) { + return 1; + } + f = BigInteger.ONE; + for (var i=1; i<=n; i++) { + f = f.multiply(new BigInteger(i)); + } + return f; + } + var networks = [ { name: "Bitcoin", diff --git a/tests.js b/tests.js index 9abe573..9760d93 100644 --- a/tests.js +++ b/tests.js @@ -2503,6 +2503,7 @@ page.open(url, function(status) { [ "222F", "16" ], [ "FFFF", "16" ], [ "0000101017", "33" ], // 10 events at 3.32 bits per event + [ "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks", "226" ], // cards are not replaced, so a full deck is not 52^52 entropy which is 296 bits, it's 52!, which is 226 bits ] // use entropy page.evaluate(function(e) { -- 2.41.0