- // Convert base.ints to BigInteger.
- // Due to using unusual bases, eg cards of base52, this is not as simple as
- // using BigInteger.parse()
- var entropyInt = BigInteger.ZERO;
- for (var i=base.ints.length-1; i>=0; i--) {
- var thisInt = BigInteger.parse(base.ints[i]);
- var power = (base.ints.length - 1) - i;
- var additionalEntropy = BigInteger.parse(base.asInt).pow(power).multiply(thisInt);
- entropyInt = entropyInt.add(additionalEntropy);
- }
- // 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;
- }
+ // Convert entropy events to binary
+ var entropyBin = base.events.map(function(e) {
+ return eventBits[base.str][e.toLowerCase()];
+ }).join("");
+ // Get average bits per event
+ // which may be adjusted for bias if log2(base) is fractional
+ var bitsPerEvent = base.bitsPerEvent;