Skip to main content

Crate openentropy_tests

Crate openentropy_tests 

Source
Expand description

NIST SP 800-22 inspired randomness test battery.

Provides 31 statistical tests for evaluating the quality of random byte sequences. Each test returns a TestResult with a p-value (where applicable), a pass/fail determination, and a letter grade (A through F).

Structs§

TestResult
Result of a single randomness test.

Functions§

anderson_darling
Test 20: Anderson-Darling – A-squared statistic for uniform. Critical values: 1.933 (5%), 2.492 (2.5%), 3.857 (1%).
approximate_entropy
Test 7: Approximate entropy – compare m and m+1 bit pattern frequencies (m=3).
autocorrelation
Test 15: Autocorrelation – at lags 1-50. Count violations of 2/sqrt(n) threshold.
binary_matrix_rank
Test 24: Binary matrix rank – GF(2) Gaussian elimination on 32x32 binary matrices.
birthday_spacing
Test 28: Birthday spacing – spacing between repeated values, Poisson test.
bit_avalanche
Test 29: Bit avalanche – adjacent bytes should differ by ~4 bits (50%).
block_frequency
Test 2: Block frequency – frequency within 128-bit blocks. Chi-squared test.
byte_frequency
Test 3: Byte frequency – chi-squared on byte value distribution (256 bins).
calculate_quality_score
Calculate overall quality score (0-100) from test results.
compression_ratio
Test 13: Compression ratio – zlib compression ratio (random ~ 1.0+).
cross_correlation
Test 18: Cross-correlation – even vs odd byte independence. Pearson r.
cusum_test
Test 26: Cumulative sums (CUSUM) – detect drift/bias.
dft_spectral
Test 8: DFT spectral – detect periodic features via FFT.
kolmogorov_complexity
Test 14: Kolmogorov complexity – compression at levels 1 and 9, compute complexity and spread.
ks_test
Test 19: Kolmogorov-Smirnov test vs uniform distribution.
lag_n_correlation
Test 17: Lag-N correlation – correlation at lags [1, 2, 4, 8, 16, 32].
linear_complexity
Test 25: Linear complexity – Berlekamp-Massey LFSR complexity on 200-bit blocks.
longest_run_of_ones
Test 5: Longest run of ones – within 8-bit blocks, chi-squared against theoretical probs.
maurers_universal
Test 23: Maurer’s universal statistical test (L=6, Q=640).
mean_variance
Test 31: Mean and variance – mean (~127.5) and variance (~5461.25) of uniform bytes.
min_entropy
Test 11: Min-entropy (NIST SP 800-90B): -log2(p_max).
monobit_frequency
Test 1: Monobit frequency – proportion of 1s vs 0s should be ~50%.
monte_carlo_pi
Test 30: Monte Carlo pi – estimate pi using (x,y) pairs in unit circle.
non_overlapping_template
Test 22: Non-overlapping template – non-overlapping occurrences of (0,0,1,1).
overlapping_template
Test 21: Overlapping template – frequency of overlapping bit pattern (1,1,1,1).
permutation_entropy
Test 12: Permutation entropy – complexity of ordinal patterns (order=4).
random_excursions
Test 27: Random excursions – cycles in cumulative sum random walk.
run_all_tests
Run the complete 31-test battery on a byte slice.
runs_test
Test 4: Runs test – number of uninterrupted runs of 0s or 1s.
serial_correlation
Test 16: Serial correlation – adjacent value correlation. Z-test.
serial_test
Test 6: Serial test – frequency of overlapping m-bit patterns (m=4).
shannon_entropy
Test 10: Shannon entropy – bits per byte (max 8.0).
spectral_flatness
Test 9: Spectral flatness – geometric/arithmetic mean ratio of power spectrum.