"""Literal norm checks, input corruption, and bounded implicit endpoint controls.""" from pathlib import Path import subprocess import sys import numpy as np import pytest from theory.code import paley_t_product as product @pytest.mark.parametrize('prime', [3, 7, 11, 19, 23, 31, 43, 47]) @pytest.mark.parametrize('t', [3, 7]) def test_full_small_integer_gram_in_both_prime_residue_classes(prime, t): endpoint = product.ImplicitPaleyTEndpoint(product.literal_t_rows(t), prime) dense = endpoint.dense() assert dense.shape == (t*(prime+1),)*2 assert np.all(abs(dense) == 1) assert np.array_equal(dense @ dense.T, endpoint.order*np.eye(endpoint.order, dtype=np.int64)) proof = endpoint.proof() assert proof['full_endpoint_Gram_verified_algebraically'] assert proof['full_dense_endpoint_Gram_checked'] and proof['dense_endpoint_allocated'] assert proof['Paley_block_verification']['all_sixteen_negacyclic_blocks_checked'] for sample in proof['literal_entry_samples']: assert sample['value'] == dense[sample['row'], sample['column']] @pytest.mark.parametrize('prime,t', [(7, 3), (11, 7)]) def test_every_implicit_entry_agrees_with_independent_dense_block_construction(prime, t): endpoint = product.ImplicitPaleyTEndpoint(product.literal_t_rows(t), prime) dense = endpoint.dense() literal = np.array([[endpoint.entry(i, j) for j in range(endpoint.order)] for i in range(endpoint.order)], dtype=np.int64) assert np.array_equal(literal, dense) def test_actual_t631_inputs_prove_implicit_h27764_without_large_allocation(monkeypatch): root = Path(__file__).resolve().parents[2] if not (root/'artifacts/hadamard-7572/t631.json').is_file(): root = Path.cwd() # permits the same controls from a temporary review directory rows, record = product.load_t631(root/'artifacts/hadamard-7572/t631.json') def forbidden(*args, **kwargs): raise AssertionError('dense Kronecker endpoint allocation reached') monkeypatch.setattr(np, 'kron', forbidden) endpoint = product.ImplicitPaleyTEndpoint(rows, 43, record) report = endpoint.proof() assert endpoint.order == 27764 assert report['endpoint_Gram_scale'] == 27764 assert report['T_verification']['integer_periodic_shifts_checked'] == 631 assert report['Paley_block_verification']['input_Hadamard_order'] == 44 assert report['full_endpoint_Gram_verified_algebraically'] assert not report['dense_endpoint_allocated'] and not report['full_dense_endpoint_Gram_checked'] assert record['source_record_sha256'] == product.T631_SHA256 assert all(x['value'] in (-1, 1) for x in report['literal_entry_samples']) with pytest.raises(ValueError, match='budget'): endpoint.dense() assert isinstance(endpoint.entry(np.int64(1), np.int64(2)), int) for bad in (True, False, 1.0, -1, endpoint.order): with pytest.raises(ValueError, match='index'): endpoint.entry(bad, 0) with pytest.raises(ValueError, match='index'): endpoint.entry(0, bad) def test_changed_periodic_source_or_block_is_rejected(): rows = product.literal_t_rows(7) rows[0, 1] *= -1 with pytest.raises(ValueError, match='correlation'): product.checked_t_rows(rows) rows = product.literal_t_rows(7) rows[0, 0] = 0 with pytest.raises(ValueError, match='support'): product.checked_t_rows(rows) blocks, _ = product.paley_negacyclic_blocks(43) bad = blocks.copy() bad[0, 0, 0, 0] *= -1 with pytest.raises(ValueError, match='negacyclic|Gram'): product.checked_negacyclic_blocks(bad, 43) @pytest.mark.parametrize('bad', [True, 3.0, 1, 9, 13, 27, 223]) def test_invalid_or_nonprime_field_parameters_fail_before_source_allocation(bad, monkeypatch): def forbidden(*args, **kwargs): raise AssertionError('invalid prime reached matrix allocation') monkeypatch.setattr(np, 'eye', forbidden) with pytest.raises(ValueError, match='prime|budget'): product.paley_negacyclic_blocks(bad) def test_exact_alphabet_and_budget_gates_precede_expensive_work(monkeypatch): for raw in (product.literal_t_rows(3).astype(float), np.ones((4, 3), dtype=bool)): with pytest.raises(ValueError, match='exact integer'): product.checked_t_rows(raw) too_large = np.zeros((4, product.MAXIMUM_T_LENGTH+1), dtype=np.int8) def forbidden(*args, **kwargs): raise AssertionError('oversized T source reached correlation enumeration') monkeypatch.setattr(np, 'roll', forbidden) with pytest.raises(ValueError, match='budget'): product.checked_t_rows(too_large) endpoint = product.ImplicitPaleyTEndpoint.__new__(product.ImplicitPaleyTEndpoint) endpoint.order = 10000 with pytest.raises(ValueError, match='budget'): endpoint.dense() with pytest.raises(ValueError, match='budget'): endpoint.dense(maximum_order=True) with pytest.raises(ValueError, match='budget'): endpoint.dense(maximum_order=513) def test_changed_retained_source_record_is_rejected_before_json_or_gram(tmp_path): fake = tmp_path/'t631.json' fake.write_text('not a source record') with pytest.raises(ValueError, match='hash mismatch'): product.load_t631(fake) def test_optimized_python_keeps_the_prime_field_gate(): run = subprocess.run([sys.executable, '-O', product.__file__, '--prime', '27', '--toy-t', '3'], capture_output=True, text=True, check=False) assert run.returncode != 0 and 'prime congruent3mod4' in run.stderr assert 'full_endpoint_Gram_verified_algebraically' not in run.stdout def test_distinct_union_count_has_an_independent_trial_division_control(): from theory.code.existence_product_closure import prime_power limit, t = 200000, 631 first, second = set(), set() for q in range(3, limit // t): if not prime_power(q): continue if q % 4 == 3: second.add(t*(q+1)) if q % 4 == 1: n = 2*t*(q+1) while n <= limit: first.add(n) n *= q report = product.finite_order_union(limit, t) assert report['Spence_family_distinct_orders'] == len(first) assert report['Paley_T_family_distinct_orders'] == len(second) assert report['union_distinct_orders'] == len(first | second) assert report['intersection_distinct_orders'] == len(first & second) assert report['additional_relative_to_Spence_family'] == len(second-first) def test_order_count_budget_is_checked_before_sieve_allocation(monkeypatch): from theory.code import density_checks def forbidden(*args): raise AssertionError('counting reached oversized prime sieve') monkeypatch.setattr(density_checks, 'primes_through', forbidden) with pytest.raises(ValueError, match='budget'): product.finite_order_union(2_000_002*631) for limit, t in ((True,631),(-1,631),(100,False),(100,0)): with pytest.raises(ValueError): product.finite_order_union(limit,t)