diff --git a/Tets_Python/StartUpTest.py b/Tets_Python/StartUpTest.py index 6151e8a..b624626 100644 --- a/Tets_Python/StartUpTest.py +++ b/Tets_Python/StartUpTest.py @@ -1,25 +1,30 @@ from math import fabs as fabs from math import sqrt as sqrt from scipy.special import erfc as erfc +import numpy as np +from scipy import stats class StartUPTest: @staticmethod - def monobit_test(binary_data: str): - - length_of_bit_string = len(binary_data) + def monobit_test(binary_data: bytes): + length_of_bit_string = len(binary_data) * 8 # Variable for S(n) count = 0 - # Iterate each bit in the string and compute for S(n) - for bit in binary_data: - if bit == 48: - # If bit is 0, then -1 from the S(n) - count -= 1 - elif bit == 49: - # If bit is 1, then +1 to the S(n) - count += 1 + # Iterate each byte in the string and compute for S(n) + for byte in binary_data: + # Iterate each bit in the byte + for i in range(8): + # Extract the i-th bit from the byte + bit = (byte >> i) & 1 + if bit == 0: + # If bit is 0, then -1 from the S(n) + count -= 1 + else: + # If bit is 1, then +1 to the S(n) + count += 1 # Compute the test statistic sObs = count / sqrt(length_of_bit_string) @@ -84,3 +89,14 @@ class StartUPTest: else: value = 0 return value + + @staticmethod + def chi_square(byte_data: bytes): + expected_probabilities = np.full(256, 1/256) # Assuming 256 possible byte values + total_observations = len(byte_data) + observed_data, _ = np.histogram(list(byte_data), bins=np.arange(257)) + expected_frequencies = expected_probabilities * total_observations + + chi2_statistic, p_value = stats.chisquare(observed_data, f_exp=expected_frequencies) + + return p_value, (p_value >= 0.01), chi2_statistic