"""Independent local witnesses, small residue sieve, and corruption controls. This verifier does not use the C tile builder or kill-graph state machine. The large exhaustive claim rests on the two C methods; local witnesses alone establish attainability, never global maximality. """ import copy import hashlib import json import sys from collections import Counter from pathlib import Path PRIMES = [5, 7, 11, 13, 17, 19, 23, 29] def old_slot(value, top): return value % 6 == 5 and all(value % p not in (0, p - 2) for p in PRIMES if p <= top) def validate(data, brute=True): witnesses = [] for fold in data['folds']: q, top = fold['q'], fold['old_top'] classes = fold['classes'] assert classes[-1]['L'] == fold['maxL'] count = mass = nodes = 0 exported = Counter() for c in classes: L = c['L'] h = dict(c['histogram']) assert len(h) == len(c['histogram']) and all(s > 0 and n > 0 for s, n in h.items()) assert sum(h.values()) == c['count'] assert max(h) == c['max_span'] and h[max(h)] == c['max_count'] count += c['count'] nodes += L * c['count'] mass += sum(s * n for s, n in h.items()) exported.update({(L, s): n for s, n in h.items()}) left, right = c['witness_left'], c['witness_left'] + c['max_span'] assert old_slot(left, top) and old_slot(right, top) assert left % q not in (0, q - 2) and right % q not in (0, q - 2) interior = [v for v in range(left + 6, right, 6) if old_slot(v, top)] assert len(interior) == L assert all(v % q in (0, q - 2) for v in interior) points = [left] + interior + [right] witnesses.append({'old_top': top, 'q': q, 'L': L, 'left': left, 'span': c['max_span'], 'old_gaps': [b - a for a, b in zip(points, points[1:])]}) assert count == fold['D_old'] * (q - 2) assert nodes == 2 * fold['D_old'] assert mass == q * fold['W_old'] if brute and top <= 13: width = q * fold['W_old'] # Directly test every possible opener against the old primes. This # includes all copy boundaries without generating or copying tiles. slots = [v for v in range(5, width, 6) if old_slot(v, top)] live_indices = [i for i, v in enumerate(slots) if v % q not in (0, q - 2)] observed = Counter() for j, i in enumerate(live_indices): nxt = live_indices[(j + 1) % len(live_indices)] L = (nxt - i - 1) % len(slots) span = (slots[nxt] - slots[i]) % width observed[(L, span)] += 1 assert observed == exported target = data['folds'][-1] assert target['old_top'] == 29 and target['q'] == 31 assert [c['count'] for c in target['classes'][1:]] == [413380422, 7999018, 12992, 4] assert [c['max_span'] for c in target['classes']] == [258, 330, 348, 330, 330] return witnesses def main(): source = Path(sys.argv[1] if len(sys.argv) > 1 else 'weighted-runs-739.json') raw = source.read_bytes() data = json.loads(raw) witnesses = validate(data) # Existing forensics gives a different representative at its least # position. Validate it locally without calling our C implementation. prior_left = 8813641451 prior_points = [v for v in range(prior_left, prior_left + 349, 6) if old_slot(v, 29)] prior_gaps = [b - a for a, b in zip(prior_points, prior_points[1:])] assert prior_gaps == [138, 60, 150] assert all(v % 31 in (0, 29) for v in prior_points[1:-1]) assert all(v % 31 not in (0, 29) for v in [prior_points[0], prior_points[-1]]) controls = [] for name in ['histogram_count', 'max_span', 'witness_left']: changed = copy.deepcopy(data) c = changed['folds'][-1]['classes'][2] if name == 'histogram_count': c['histogram'][0][1] += 1 elif name == 'max_span': c['max_span'] += 6 else: c['witness_left'] += 1 try: validate(changed, brute=False) except AssertionError: controls.append(name) else: raise AssertionError('corruption escaped: ' + name) print(json.dumps({'job_id': 739, 'input_sha256': hashlib.sha256(raw).hexdigest(), 'independent_small_full_histograms': 4, 'all_class_maximum_witnesses_validated': len(witnesses), 'negative_controls_rejected': controls, 'existing_forensics_witness': {'left': prior_left, 'old_gaps': prior_gaps, 'killed_slots': 2}, 'witnesses': witnesses}, indent=2)) if __name__ == '__main__': main()