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§
- Test
Result - 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.