"""Tests for sp80090b_health.py. Run: /Volumes/M2-Dev/conda/envs/toptech/bin/python -m pytest -q""" from __future__ import annotations import numpy as np import pytest import sp80090b_health as H def test_rct_cutoff_examples_from_the_standard() -> None: # SP 800-90B 4.4.1: alpha = 2^-20 and H = 2 give 1 + 20/2 = 11. assert H.rct_cutoff(2.0) == 11 # 4.4.1: H = 8 needs a cutoff of 6 for a false-positive rate near 1e-12 (alpha = 2^-40). assert H.rct_cutoff(8.0, 2.0 ** -40) == 6 @pytest.mark.parametrize(("h", "c"), [(0.2, 941), (0.4, 840), (0.6, 748), (0.8, 664), (1.0, 589)]) def test_apt_cutoff_table2_binary(h: float, c: int) -> None: assert H.apt_cutoff(h, 1024) == c @pytest.mark.parametrize(("h", "c"), [(0.5, 410), (1, 311), (2, 177), (4, 62), (8, 13)]) def test_apt_cutoff_table2_nonbinary(h: float, c: int) -> None: assert H.apt_cutoff(h, 512) == c def test_rct_small_example() -> None: s = np.array([0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1]) assert H.rct_failures(s, 4) == [5, 10, 11] def test_apt_small_example() -> None: # Window 4, cutoff 3: windows [1,1,1,0] fails at index 2, [0,1,0,0] fails at index 7. s = np.array([1, 1, 1, 0, 0, 1, 0, 0, 1, 0]) assert H.apt_failures(s, 3, 4) == [2, 7] def test_stuck_source_fails_at_the_cutoff() -> None: s = H.stuck_bits(1, 2000) assert H.rct_failures(s, 21)[0] == 20 assert H.apt_failures(s, 589, 1024)[0] == 588 def test_fair_source_passes_seeded_run() -> None: s = H.biased_bits(0.5, 1 << 16, 1) assert H.rct_failures(s, H.rct_cutoff(1.0)) == [] assert H.apt_failures(s, H.apt_cutoff(1.0, 1024), 1024) == [] def test_bias_is_caught_by_apt_not_rct() -> None: s = H.biased_bits(0.6, 1 << 16, 2) assert H.rct_failures(s, H.rct_cutoff(1.0)) == [] assert len(H.apt_failures(s, H.apt_cutoff(1.0, 1024), 1024)) > 0 def test_alternating_pattern_passes_both() -> None: # A periodic failure is invisible to both approved tests (SP 800-90B 4.3, item 8, # asks for developer-defined tests for such failure modes). s = H.periodic_bits([0, 1], 1 << 14) assert H.rct_failures(s, 21) == [] and H.apt_failures(s, 589, 1024) == [] def test_min_entropy_binary() -> None: assert H.min_entropy_iid_binary(0.5) == pytest.approx(1.0) assert H.min_entropy_iid_binary(0.75) == pytest.approx(0.415037, abs=1e-6)