forked from usnistgov/SP800-90B_EntropyAssessment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iid_main.py
executable file
·92 lines (78 loc) · 2.89 KB
/
iid_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# DRAFT NIST SP 800-90B (January 2016) Section 5
#
# Estimating the Min-Entropy of IID Sources
#
# NOTE: this software is made available with no guarantee - implied or otherwise -
# of correctness or completeness. See user guide for full disclaimer.
#
# Tim Hall
# tim.hall@nist.gov
#
# Kerry McKay
# kerry.mckay@nist.gov
#
# February 2016
import time
import sys
from util90b import get_parser, to_dataset
from permutation_tests import permutation_test
from chi_square_tests import pass_chi_square_tests
from mostCommonValue import most_common
from LRS import lenLRS
##################
# main program
##################
if __name__ == '__main__':
start_time = time.time()
# get command line arguments
args = get_parser('IID').parse_args()
datafile = args.datafile
bits_per_symbol = int(args.bits_per_symbol)
verbose = bool(args.verbose)
with open(datafile, 'rb') as file:
# Read in raw bytes and convert to list of output symbols
bytes_in = bytearray(file.read())
dataset = to_dataset(bytes_in, bits_per_symbol)
k = len(set(dataset))
if verbose:
# print file and dataset details
print ("Read in file %s, %d bytes long." % (datafile, len(bytes_in)))
print ("Dataset: %d %d-bit symbols, %d symbols in alphabet." % (len(dataset), bits_per_symbol, k))
print ("Output symbol values: min = %d, max = %d\n" % (min(dataset), max(dataset)))
#######################################
# STEP 1: Determine if Dataset is IID #
#######################################
# determine if dataset is IID using shuffle and Chi-square tests
passed_permutation_tests = permutation_test(dataset, verbose)
if passed_permutation_tests:
if verbose:
print ("** Passed IID permutation tests")
else:
if verbose:
print ("** Failed IID permutation tests")
print ("IID = False")
sys.exit(0)
# run chi-square tests on dataset
if pass_chi_square_tests(dataset, verbose):
if verbose:
print ("** Passed chi square tests")
else:
if verbose:
print ("** Failed chi square tests")
print ("IID = False")
sys.exit(0)
# run LRS test
if lenLRS(dataset, verbose):
if verbose:
print ("** Passed LRS test")
print ("\nIID = True")
else:
print ("** Failed LRS test")
print ("IID = False")
sys.exit(0)
############################################
# STEP 2: Calculate min-entropy of dataset #
############################################
pmax, minH = most_common(dataset)
print("min-entropy = %g" % (minH))
print("\nDon't forget to run the sanity check on a restart dataset using H_I = %g" % minH )