// Entropy of generated secrets: the size of the generator's output space in bits. // Not a guessability score for human-chosen passwords (that is the strength checker's job). /** Bits of entropy for `length` independent uniform picks from `poolSize` symbols. */ export function entropyBits(poolSize: number, length: number): number { if (poolSize < 1 || length < 0) throw new RangeError('poolSize must be >= 1 and length >= 0'); return length * Math.log2(poolSize); } /** log2 of a positive BigInt, accurate to ~1e-12 bits even for numbers far beyond 2^1024. */ export function log2BigInt(value: bigint): number { if (value <= 0n) throw new RangeError('value must be positive'); const hex = value.toString(16); const lead = Math.min(hex.length, 13); return Math.log2(parseInt(hex.slice(0, lead), 16)) + 4 * (hex.length - lead); }