xref: /xnu-8020.121.3/tools/entropy_health_test_bounds.py (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1*fdd8201dSApple OSS Distributions#!/usr/bin/env python3
2*fdd8201dSApple OSS Distributions
3*fdd8201dSApple OSS Distributionsfrom fractions import Fraction
4*fdd8201dSApple OSS Distributionsfrom math import ceil
5*fdd8201dSApple OSS Distributionsfrom math import comb
6*fdd8201dSApple OSS Distributions
7*fdd8201dSApple OSS Distributions
8*fdd8201dSApple OSS Distributions# The inverse of 2, i.e. 2^-1. To be used as a base in exponentiations
9*fdd8201dSApple OSS Distributions# representing probabilities.
10*fdd8201dSApple OSS DistributionsINV2 = Fraction(1, 2)
11*fdd8201dSApple OSS Distributions
12*fdd8201dSApple OSS Distributions
13*fdd8201dSApple OSS Distributions# The probability of a false positive health test failure expressed as
14*fdd8201dSApple OSS Distributions# the negative logarithm of the *actual* probability. In simpler
15*fdd8201dSApple OSS Distributions# terms, the actual probability is:
16*fdd8201dSApple OSS Distributions#
17*fdd8201dSApple OSS Distributions# INV2 ** A
18*fdd8201dSApple OSS Distributions#
19*fdd8201dSApple OSS Distributions# It is simpler to keep this representation when computing the bound
20*fdd8201dSApple OSS Distributions# of the Repetition Count Test (below).
21*fdd8201dSApple OSS DistributionsA = 40
22*fdd8201dSApple OSS Distributions
23*fdd8201dSApple OSS Distributions
24*fdd8201dSApple OSS Distributions# The estimated min-entropy per sample in bits. Min-entropy is the
25*fdd8201dSApple OSS Distributions# negative logarithm of the probability of the *most likely* outcome.
26*fdd8201dSApple OSS Distributions#
27*fdd8201dSApple OSS Distributions# We consider this estimate to be conservative.
28*fdd8201dSApple OSS DistributionsH = 1
29*fdd8201dSApple OSS Distributions
30*fdd8201dSApple OSS Distributions
31*fdd8201dSApple OSS Distributions# The probability of the most likely outcome occurring in a given
32*fdd8201dSApple OSS Distributions# sample. This derives from the definition of min-entropy (see above).
33*fdd8201dSApple OSS DistributionsP = INV2 ** H
34*fdd8201dSApple OSS Distributions
35*fdd8201dSApple OSS Distributions
36*fdd8201dSApple OSS Distributions# 4.4.1 Repetition Count Test
37*fdd8201dSApple OSS Distributions#
38*fdd8201dSApple OSS Distributions# The Repetition Count Test (RCT) detects catastrophic failures in the
39*fdd8201dSApple OSS Distributions# noise source when it becomes "stuck" generating a single value over
40*fdd8201dSApple OSS Distributions# many consecutive samples.
41*fdd8201dSApple OSS Distributions#
42*fdd8201dSApple OSS Distributions# The probability of generating C consecutive identical samples is:
43*fdd8201dSApple OSS Distributions#
44*fdd8201dSApple OSS Distributions# P^(C-1)
45*fdd8201dSApple OSS Distributions#
46*fdd8201dSApple OSS Distributions# Or equivalently:
47*fdd8201dSApple OSS Distributions#
48*fdd8201dSApple OSS Distributions# 2^(-H * (C-1))
49*fdd8201dSApple OSS Distributions#
50*fdd8201dSApple OSS Distributions# To keep this under our rate of acceptable false positives, we need
51*fdd8201dSApple OSS Distributions# to satisfy this inequality:
52*fdd8201dSApple OSS Distributions#
53*fdd8201dSApple OSS Distributions# 2^-A >= 2^(-H * (C-1))
54*fdd8201dSApple OSS Distributions#
55*fdd8201dSApple OSS Distributions# Taking the logarithm of both sides, we have:
56*fdd8201dSApple OSS Distributions#
57*fdd8201dSApple OSS Distributions# -A >= -H * (C-1)
58*fdd8201dSApple OSS Distributions#
59*fdd8201dSApple OSS Distributions# Solving for C, we have:
60*fdd8201dSApple OSS Distributions#
61*fdd8201dSApple OSS Distributions# (A / H) + 1 >= C
62*fdd8201dSApple OSS Distributionsdef repetition_count_bound():
63*fdd8201dSApple OSS Distributions    return 1 + ceil(Fraction(A, H))
64*fdd8201dSApple OSS Distributions
65*fdd8201dSApple OSS Distributions
66*fdd8201dSApple OSS Distributions# 4.4.2 Adaptive Proportion Test
67*fdd8201dSApple OSS Distributions#
68*fdd8201dSApple OSS Distributions# The Adaptive Proportion Test (APT) tries to detect more subtle noise
69*fdd8201dSApple OSS Distributions# source failures causing certain values to occur with unexpected
70*fdd8201dSApple OSS Distributions# frequency. It does this by taking a sample from the noise source and
71*fdd8201dSApple OSS Distributions# counting how many times the same sample occurs within a fixed-size
72*fdd8201dSApple OSS Distributions# window.
73*fdd8201dSApple OSS Distributions
74*fdd8201dSApple OSS Distributions
75*fdd8201dSApple OSS Distributions# The size of the window for non-binary alphabets for the APT.
76*fdd8201dSApple OSS DistributionsW = 512
77*fdd8201dSApple OSS Distributions
78*fdd8201dSApple OSS Distributions
79*fdd8201dSApple OSS Distributions# The probability mass function measuring the probability of exactly k
80*fdd8201dSApple OSS Distributions# occurrences of a given value within the observation window of size
81*fdd8201dSApple OSS Distributions# W. We use the probability of the most likely event (as above).
82*fdd8201dSApple OSS Distributions#
83*fdd8201dSApple OSS Distributions# There are three terms:
84*fdd8201dSApple OSS Distributions#
85*fdd8201dSApple OSS Distributions# 1. The binomial coefficient of k, i.e. W-choose-k. Simply, how many
86*fdd8201dSApple OSS Distributions# ways are there to get exactly k outcomes given W chances.
87*fdd8201dSApple OSS Distributions#
88*fdd8201dSApple OSS Distributions# 2. The probability of each of those k events occurring.
89*fdd8201dSApple OSS Distributions#
90*fdd8201dSApple OSS Distributions# 3. The probability that the other W-k events have some other
91*fdd8201dSApple OSS Distributions# outcome.
92*fdd8201dSApple OSS Distributionsdef pmf(k):
93*fdd8201dSApple OSS Distributions    return comb(W, k) * P**k * (1 - P)**(W-k)
94*fdd8201dSApple OSS Distributions
95*fdd8201dSApple OSS Distributions
96*fdd8201dSApple OSS Distributions# The sum of probabilties of all possible counts of occurrences is 1.
97*fdd8201dSApple OSS Distributionsassert sum(map(pmf, range(W+1))) == 1
98*fdd8201dSApple OSS Distributions
99*fdd8201dSApple OSS Distributions
100*fdd8201dSApple OSS Distributions# We want to find the minimal count of occurrences such that the
101*fdd8201dSApple OSS Distributions# cumulative probability of seeing *at least* that count of
102*fdd8201dSApple OSS Distributions# occurrences (but possibly more) is no more than our false
103*fdd8201dSApple OSS Distributions# positive threshold.
104*fdd8201dSApple OSS Distributionsdef adaptive_proportion_bound():
105*fdd8201dSApple OSS Distributions    # The list of probabilities for each of the possible counts of
106*fdd8201dSApple OSS Distributions    # occurrences.
107*fdd8201dSApple OSS Distributions    probs = [pmf(x) for x in range(W+1)]
108*fdd8201dSApple OSS Distributions
109*fdd8201dSApple OSS Distributions    # The list of cumulative distributions for each of the possible
110*fdd8201dSApple OSS Distributions    # counts of occurrences.
111*fdd8201dSApple OSS Distributions    #
112*fdd8201dSApple OSS Distributions    # Whereas probs is a list of probabilities of *exactly* k
113*fdd8201dSApple OSS Distributions    # occurrences, this is a list of probabilities of *k or more*
114*fdd8201dSApple OSS Distributions    # occurrences.
115*fdd8201dSApple OSS Distributions    #
116*fdd8201dSApple OSS Distributions    # These are just sums of probabilities across a range of counts.
117*fdd8201dSApple OSS Distributions    dists = [sum(probs[x:]) for x in range(W+1)]
118*fdd8201dSApple OSS Distributions
119*fdd8201dSApple OSS Distributions    # Because we have constructed dists as an ordered list of
120*fdd8201dSApple OSS Distributions    # cumulative probabilities, we can simply return the index of the
121*fdd8201dSApple OSS Distributions    # first value that is below our threshold.
122*fdd8201dSApple OSS Distributions    for i, d in enumerate(dists):
123*fdd8201dSApple OSS Distributions        if d <= INV2**A:
124*fdd8201dSApple OSS Distributions            return i
125*fdd8201dSApple OSS Distributions
126*fdd8201dSApple OSS Distributions
127*fdd8201dSApple OSS Distributionsdef main():
128*fdd8201dSApple OSS Distributions    print('Estimated min-entropy:', H)
129*fdd8201dSApple OSS Distributions    print('False positive rate: 2^-{}'.format(A))
130*fdd8201dSApple OSS Distributions    print('Repetition Count Test bound:', repetition_count_bound())
131*fdd8201dSApple OSS Distributions    print('Adaptive Proportion Test bound:', adaptive_proportion_bound())
132*fdd8201dSApple OSS Distributions
133*fdd8201dSApple OSS Distributions
134*fdd8201dSApple OSS Distributionsif __name__ == '__main__':
135*fdd8201dSApple OSS Distributions    main()
136