"""Ideal independent-uniform hash model; not a device or cryptanalytic benchmark.""" from decimal import Decimal, localcontext from fractions import Fraction import json PRECISION = 220 CASES = [(64, 10**6), (64, 10**9), (128, 10**9), (256, 10**9)] def validate(bits, n): if type(bits) is not int or not 1 <= bits <= 256: raise ValueError("bits must be an integer from 1 to 256") if type(n) is not int or not 0 <= n <= 10**18: raise ValueError("n must be an integer from 0 to 10**18") def exact(bits, n): validate(bits, n) m = 2**bits if n > m: return Fraction(1) if n > 1000: raise ValueError("exact product is limited to 1000 distinct inputs") complement = Fraction(1) for i in range(n): complement *= Fraction(m-i, m) return 1-complement def estimates(bits, n): validate(bits, n) with localcontext() as ctx: ctx.prec = PRECISION m = Decimal(2**bits) count = Decimal(n) pair = 1/m expected_pairs = count*(count-1)/(2*m) # High precision preserves complements as small as 2**-256. target = 1 - (count * (1-pair).ln()).exp() approx = 1 - (-expected_pairs).exp() return { "bits": bits, "distinct_inputs": str(n), "one_pair_probability": str(pair), "target_hit_probability": str(target), "expected_colliding_pairs": str(expected_pairs), "birthday_exponential_approximation": str(approx) if n <= 2**bits else None, "union_bound": str(min(Decimal(1), expected_pairs)), "exact_collision_probability": str(exact(bits, n)) if n <= 1000 or n > 2**bits else None, } def results(): return { "schema_version": 1, "model": "Independent uniform b-bit outputs for distinct inputs; no adversary", "decimal_precision": PRECISION, "cases": [estimates(b, n) for b, n in CASES], "small_space": {"bits": 2, "distinct_inputs": 4, "collision_assignments": 232, "all_assignments": 256, "probability": str(exact(2, 4))}, "limitations": [ "Probabilities describe an idealized hash mapping, not a fixed archive measurement.", "Repeated identical byte strings are not new distinct inputs.", "The exponential expression is an approximation, not an exact probability.", "The union bound is an upper bound, not a confidence interval.", "No actual hash implementation, cryptanalytic attack or storage failure rate is tested." ], } if __name__ == "__main__": print(json.dumps(results(), indent=2))