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