diff --git a/research/complex-toom3-five-product-screen/README.md b/research/complex-toom3-five-product-screen/README.md new file mode 100644 index 0000000000..a390e64b68 --- /dev/null +++ b/research/complex-toom3-five-product-screen/README.md @@ -0,0 +1,47 @@ +# Exact Toom-3 five-product screen for the selected complex profile + +**Exact tensor identity and optimistic profile screen. No selected-provider construction or new κ is claimed.** + +## Result + +For degree-two polynomials, evaluation at 0, 1, -1, 2 and infinity gives an exact five-product formula for all five output coefficients. The verifier reconstructs the interpolation matrix over the rationals and checks all nine input monomial pairs. + +If one such block replaced nine rank-one recursive products by five, it would save four rank-one units. The pinned selected local profile has 2,412 rank-one units. Its exact ceiling calculator gives: + +| Hypothetical realized blocks | Rank-one count | Local mass | Complex-side a ceiling | +|---:|---:|---:|---:| +| 0 | 2,412 | 5,586 | 0.001690054109089 | +| 64 | 2,156 | 5,330 | [0.001775054925982, 0.001775054925983] | +| 210 | 1,572 | 4,746 | [0.002005153888714, 0.002005153888715] | +| 254 | 1,396 | 4,570 | [0.002086688163384, 0.002086688163385] | + +Sixty-four blocks are the first count whose ideal ceiling clears the task card’s five-percent gate. Two hundred ten are the first count whose complex-side transfer cap exceeds 0.002. The selected forward and backward traces each admit 254 disjoint groups of nine rank-one climbs under the phase-local dependency-window movement model used in PR #1817. This establishes enough candidate event slots under that relaxed model; it does not establish that any group is a Toom-3 tensor. + +A straightforward scalar evaluation/interpolation schedule uses 19 additions and 13 rational scales per block. At 210 blocks that is 3,990 additions and 2,730 scales. Copies, scratch allocation, dirty-state restoration, precision, native hosting and the new output map are still unpriced. + +The selected bit saving is 0.001733427039589844234749. Under the retained transfer its planning cap is 0.0017304274698236029, below 0.002. Therefore this complex-side idea alone cannot produce an overall κ above 0.002. The selected κ remains 0.001672660197076918. + +## Boundary and prior work + +The exact Toom-3 identity changes the bilinear tensor, extending the two-coefficient Karatsuba screen in Discussion #1987. It differs from diagonal rank-16 child batching in PR #1779 and its dependency-window audit in PR #1817, and from the two-root E8 rank-16 decoder currently proposed in PR #3895. The fresh overlap snapshot covered 672 open PRs and 2,573 Discussion records; these matches are leads, not a novelty certificate. The PR #1817 event-window rule is reimplemented here and credited to Josie Elliston. + +The selected-source event records name frame climbs and register touches. They do not identify a three-by-three coefficient convolution, a five-child output decoder, or a dirty-state memory contract. The 254 groups are eligible event windows only. No selected provider block, complete local profile, supplier admission or kappa improvement is claimed. + +The family expansion gate therefore permits only one next source-bound witness: bind one candidate group to its exact paired input forms and outputs, then charge its complete interface. Do not extrapolate the 254 event count into a multi-block construction. + +## Reproduction + +From the repository root, run: + + python3 scripts/research_package.py --package research/complex-toom3-five-product-screen --check-only + python3.12 scripts/research_package.py --package research/complex-toom3-five-product-screen + +The registered replay checks both orientations, exact rational identities, dependency-window movement, profile ceilings, negative controls and the unchanged bit-side cap. The pinned sources and exact base are recorded in SOURCES.json. No full supplier cold replay, Lean proof, or native compiler replay is claimed. + +## Open next questions + +TASK.json records the exact first source-binding experiment and the separate bit-side requirement for an overall κ above 0.002. The immediate blocker is the absent mapping from selected child climbs to Toom coefficient inputs and output coefficients. + +## Credit + +Credit the selected #1094 provider lineage; Josie Elliston / PR #1817 for the dependency-window batching model; the contributors to Discussion #1987 for the two-coefficient Karatsuba screen; Arham Tahir / PR #1779 for diagonal rank-16 batches; and the contributors to PR #3895 for the two-root E8 block decoder. Prepared by Gonzalo Mier with substantial OpenAI Codex assistance. Apache-2.0; no independent human review claimed. diff --git a/research/complex-toom3-five-product-screen/SOURCES.json b/research/complex-toom3-five-product-screen/SOURCES.json new file mode 100644 index 0000000000..af6a5c2e8e --- /dev/null +++ b/research/complex-toom3-five-product-screen/SOURCES.json @@ -0,0 +1,42 @@ +{ + "schema_version": 1, + "base_commit": "20ecf2891d80b5acf18d980b2164fb44ea7faa90", + "files": [ + { + "path": "certificates/selected-result.json", + "usage": "input", + "git_blob": "b1fea0c94436b236cc2b7d41a7be032811b94c81", + "sha256": "bb2eaa58647b353f88d0aec2eafb9d30df1be53589231b23e12ceef3e4bad129" + }, + { + "path": "docs/research/agent-task-catalog.json", + "usage": "provenance", + "git_blob": "67f085acd86acd7d4e03922d9cf4b26bca66fed5", + "sha256": "47b42433f5a85809a3aadd29c2c339da2c6ed728e4f7762df6b88bccb7874cf0" + }, + { + "path": "research/community-frontier1094-audit/selected-replay.tar.gz", + "usage": "input", + "git_blob": "c24daf067f9c543421d89ca2eeaa4eb78a57fb6f", + "sha256": "f1da80610bf33c58527f18ad63b4350d0b864e1d4486141ee17ac9bed4d529d6" + }, + { + "path": "research/complex-local-word-ceiling/RESULT.json", + "usage": "input", + "git_blob": "77f55043a679dd722330f1e2499863e36376d9f9", + "sha256": "d91d6c46509875abf1f61de67c2609656d3dc685451299f83caa6f922493692d" + }, + { + "path": "research/complex-local-word-ceiling/ceiling.py", + "usage": "input", + "git_blob": "6c0f54527d879396f689523bfde19928a9b6157c", + "sha256": "86a1432870b3b21d8454106ec9162fddc0471472d9214c590a1420f2426cedfc" + }, + { + "path": "scripts/research_baseline.py", + "usage": "input", + "git_blob": "6b607b36eefd65718120ade3c8187681f11bf879", + "sha256": "59d9b04dc33e1bb3f81506d187243435de4902138a038b935e36ebaa5f37db67" + } + ] +} diff --git a/research/complex-toom3-five-product-screen/TASK.json b/research/complex-toom3-five-product-screen/TASK.json new file mode 100644 index 0000000000..55146ff1e1 --- /dev/null +++ b/research/complex-toom3-five-product-screen/TASK.json @@ -0,0 +1,78 @@ +{ + "schema_version": 1, + "task_id": "complex-rank-shape-decoder", + "priority_revision": 6, + "base_commit": "20ecf2891d80b5acf18d980b2164fb44ea7faa90", + "original_card": { + "id": "complex-rank-shape-decoder", + "question": "Can a decoder deliver the local paid mass in fewer, larger-rank children (block frame climbs instead of rank-1 to rank-3 steps)?", + "starting_points": [ + "docs/research/research-family-status.md", + "research/complex-local-word-ceiling/README.md", + "research/complex-decoder-field-extension-seed44/README.md", + "research/complex-frame-pair-decoders/README.md", + "research/selected-k8-unrestricted-ends/README.md", + "research/eisenstein-reduction-copy-floor/README.md" + ], + "methods": [ + "Find one exact small decoding identity whose frame path climbs in blocks, and price its complete per-call profile.", + "Bound the best effective child rank achievable for one fixed support alphabet, including copies, frames and restoration." + ], + "acceptance_gate": "A per-call local profile, with all copied information, frames and restoration counted, whose ceiling exceeds the selected word's by at least 5%; masked or overcomplete supports must change an assumption of the eight-term all-pairs bound. A scoped local result may be retained without a complete supplier. Expanding it into further construction rounds requires the family-level budget and obligation gate in research-family-status.md; an optimistic passing relaxation alone is not acceptance.", + "first_step": { + "command": [ + "python3", + "-B", + "research/complex-local-word-ceiling/verify.py" + ], + "expected": "PASS_COMPLEX_LOCAL_WORD_CEILING", + "inspect": "Read the selected word's per-call local profile, its packing ceiling and the lever table; run levers.py for the ideal budget of the lever you target.", + "scope": "Positive control and planning budget only; this is not a new construction or full cold replay." + }, + "first_experiment": "Before any synthesis, write the candidate per-call local profile (and centre charge / unpaid coordinates if changed) and screen it with ceiling.ceiling(); reject it if even the ideal ceiling does not beat the selected word's by your stated margin. Ideal budget: rank-16 delivery is 2.31x (levers.py).", + "bottleneck_link": "Rank-1 children are about half of recursive cost; the selected word's effective child rank is about 3.9. Larger-rank delivery lowers recursive cost per unit of paid mass.", + "allowed_variations": [ + "One masked, overcomplete or block decoder family with exact labels and supports.", + "A scoped lower bound on effective rank for one alphabet.", + "If the coverage law, template or guard changes, derive an applicable fully charged replacement bound instead of applying the incumbent ceiling outside its scope." + ], + "scope_change_triggers": [ + "Repacking idle remainders of an unchanged word (covered by the packing ceiling).", + "Repeating the eight-term all-pairs search inside its proved scope.", + "Only changing helper terminal subspaces and monotone frames within the fixed scalar word, entrances and cleanup contract already covered by #1995/#2013.", + "Searching below C=40 in the #2017 Eisenstein binary scalar-rectangle or fixed-support block-linear additive pairing model; specify a changed premise instead. This bound does not cover every complex decoder.", + "Continuing a partial cost improvement while leaving the same unpriced consumer, restoration or central-readout obligation unresolved; refresh the family expansion decision first. Local product-retiming evidence has a narrow scope and is not a ban on changed graphs or contracts." + ], + "overlap_terms": [ + "rank shape", + "block climb", + "masked decoder", + "overcomplete", + "effective rank" + ] + }, + "scope_status": "changed", + "actual_scope": "Construct and exactly verify the degree-two Toom-Cook tensor identity (nine coefficient products replaced by five), measure the ideal selected-word profile change, and count phase-local rank-one event groups under the dependency-window movement model of PR #1817.", + "scope_change_reason": "The card starts from frame-climb decoders. This experiment changes the bilinear tensor by applying a five-product convolution factorization, then binds only its ideal rank-profile effect and a permissive source-event grouping screen. It does not claim a selected-provider tensor embedding.", + "bottleneck_link": "The selected local word contains 2,412 rank-one child units. Every Toom-3 block would replace nine such products by five, saving four units; the pinned ceiling screen says 210 realized blocks could move the complex-side cap above 0.002.", + "budget_minutes": 60, + "stop_rule": "Do not expand into a many-block synthesis until one nine-child source-bound Toom-3 block has exact left/right/output maps, arbitrary-dirty restoration, source acquisition, frame and scratch costs. Stop this route at the first exact embedding obstruction or an invoice that exhausts the profile margin. The current 254 phase-local windows are only eligible slots, not Toom blocks.", + "outcome": "partial", + "evidence": "Exact rational interpolation verifies all nine basis products of degree-two polynomial convolution. A pinned zero-interface profile screen crosses the complex-side 5% gate at 64 blocks and the complex-side kappa=0.002 threshold at 210. The selected trace admits 254 phase-local groups of nine rank-one climbs under the movement model from PR #1817. No group has been shown to implement the Toom tensor, so no source-bound profile, supplier or kappa improvement is claimed. Under the unchanged bit-side saving, the retained transfer remains capped below 0.002.", + "author": "Gonzalo Mier", + "ai_assistance": "Substantial OpenAI Codex assistance in selecting the Toom-3 extension, deriving and checking the exact tensor, reproducing the selected-profile screen, adapting the event-window census, and writing the verifier and package.", + "open_questions": [ + { + "question": "Can one of the 254 event groups be bound to an exact nine-term convolution sub-tensor of the selected provider, with a five-child output map?", + "why_it_matters": "The ideal profile only improves the complex-side cap if a real selected-source block replaces nine child products while preserving the full local operator.", + "first_step": "Choose one phase-local group, reconstruct its paired left and right child forms and output coefficients from the pinned word and recipes, then verify the proposed block on all basis inputs and outputs.", + "blocked_by": "The event trace identifies frame climbs and register conflicts but supplies no Toom coefficient assignment, output decoder, native scratch contract, or dirty-state restoration schedule." + }, + { + "question": "Can the selected bit supplier exceed the primitive threshold 1/499 needed for kappa above 0.002 under the retained transfer?", + "why_it_matters": "Even the ideal Toom-3 complex cap is limited globally by the unchanged bit-side cap, currently below 0.002.", + "first_step": "Take up the attachment\u2019s bit-side extension route only after the complex block has a complete source-bound invoice.", + "blocked_by": "The current selected bit saving is 0.001733427039589844234749, below 1/499." + } + ] +} diff --git a/research/complex-toom3-five-product-screen/VERIFY.json b/research/complex-toom3-five-product-screen/VERIFY.json new file mode 100644 index 0000000000..4b8d07f165 --- /dev/null +++ b/research/complex-toom3-five-product-screen/VERIFY.json @@ -0,0 +1,34 @@ +{ + "schema_version": 1, + "status": "ready", + "python": "3.12", + "requirements": [], + "inputs": [ + "certificates/selected-result.json", + "research/community-frontier1094-audit/selected-replay.tar.gz", + "research/complex-local-word-ceiling/RESULT.json", + "research/complex-local-word-ceiling/ceiling.py", + "scripts/research_baseline.py" + ], + "commands": [ + { + "argv": [ + "{python}", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 120, + "checks": "Exact selected-source pins and rank profile; exact Toom-3 evaluation/interpolation identity on all nine basis products; rational linear-map counts and malformed-point/omitted-product controls; dependency-window reordering with frame semantics and 254 disjoint groups of nine; exact selected-profile ceiling brackets and first block counts for the 5% and complex-side kappa=0.002 thresholds; selected bit-side cap; no supplier or new-kappa claim." + }, + { + "argv": [ + "{python}", + "-O", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 120, + "checks": "Repeat the complete exact identity, source schedule, ceiling and global bit-cap receipt under optimized Python." + } + ] +} diff --git a/research/complex-toom3-five-product-screen/verify.py b/research/complex-toom3-five-product-screen/verify.py new file mode 100644 index 0000000000..a66a2716fc --- /dev/null +++ b/research/complex-toom3-five-product-screen/verify.py @@ -0,0 +1,507 @@ +#!/usr/bin/env python3 +"""Exact Toom-3 tensor and optimistic selected-profile/source-window screen.""" +from __future__ import annotations + +import bisect +import collections +import gzip +import hashlib +import io +import json +import subprocess +import sys +import tarfile +from fractions import Fraction as Q +from pathlib import Path + +HERE = Path(__file__).resolve().parent +ROOT = HERE.parents[1] +sys.path.insert(0, str(ROOT / "research/complex-local-word-ceiling")) +import ceiling as C # noqa: E402 + +ARCHIVE = ROOT / "research/community-frontier1094-audit/selected-replay.tar.gz" +ARCHIVE_SHA256 = "f1da80610bf33c58527f18ad63b4350d0b864e1d4486141ee17ac9bed4d529d6" +PROGRAM_MEMBER = "candidate/strict/global/base/providers/gauge44/LOCAL-PROGRAMS.json.gz" +PROGRAM_MEMBER_SHA256 = "241fde27d4e43f5a9cb6b128f9f76c5131a53d09f0371676163f914d0b0fef3a" +WORD_MEMBER = "candidate/strict/global/base/providers/gauge44/CANONICAL-WORD.json.gz" +SELECTED = ROOT / "certificates/selected-result.json" +SELECTED_SHA256 = "bb2eaa58647b353f88d0aec2eafb9d30df1be53589231b23e12ceef3e4bad129" +BASE_PROFILE = {1: 2412, 2: 418, 3: 432, 4: 142, 5: 82, 8: 8} +GRID = Q(1, 10**15) +TARGET_KAPPA = Q(1, 500) +TARGET_A = Q(1, 499) +BATCH = 9 + + +def require(condition: bool, message: str) -> None: + if not condition: + raise RuntimeError(message) + + +def sha256(data: bytes) -> str: + return hashlib.sha256(data).hexdigest() + + +def source_objects() -> tuple[dict[str, list], dict]: + archive = ARCHIVE.read_bytes() + require(sha256(archive) == ARCHIVE_SHA256, "selected replay archive pin") + with tarfile.open(fileobj=io.BytesIO(archive), mode="r:gz") as tar: + indexed = {entry.name: entry for entry in tar.getmembers()} + require(PROGRAM_MEMBER in indexed and WORD_MEMBER in indexed, + "selected provider members present") + program_raw = tar.extractfile(indexed[PROGRAM_MEMBER]).read() + word_raw = tar.extractfile(indexed[WORD_MEMBER]).read() + require(sha256(program_raw) == PROGRAM_MEMBER_SHA256, + "selected LOCAL-PROGRAMS member pin") + programs = json.loads(gzip.decompress(program_raw)) + word = json.loads(gzip.decompress(word_raw)) + require(set(programs) == {"forward", "backward"}, "both orientations present") + return programs, word + + +def selected_baseline() -> tuple[dict, Q]: + selected_raw = SELECTED.read_bytes() + require(sha256(selected_raw) == SELECTED_SHA256, "selected record pin") + selected = json.loads(selected_raw) + require(selected["decimal"] == "0.001672660197076918", + "selected kappa value") + completed = subprocess.run( + [sys.executable, str(ROOT / "scripts/research_baseline.py")], + cwd=ROOT, check=True, capture_output=True, text=True, + ) + baseline = json.loads(completed.stdout) + require(baseline["selected_record_sha256"] == SELECTED_SHA256, + "baseline report binds selected record") + bit_a = Q(baseline["bit_saving"]) + require(bit_a == Q(1733427039589844234749, 10**24), + "selected bit saving pin") + return baseline, bit_a + + +def inverse(matrix: list[list[Q]]) -> list[list[Q]]: + n = len(matrix) + require(all(len(row) == n for row in matrix), "square matrix") + work = [ + [Q(value) for value in row] + + [Q(int(i == j)) for j in range(n)] + for i, row in enumerate(matrix) + ] + for col in range(n): + pivot = next((row for row in range(col, n) if work[row][col]), None) + if pivot is None: + raise ValueError("singular interpolation matrix") + work[col], work[pivot] = work[pivot], work[col] + scale = work[col][col] + work[col] = [value / scale for value in work[col]] + for row in range(n): + if row == col: + continue + factor = work[row][col] + if factor: + work[row] = [ + left - factor * right + for left, right in zip(work[row], work[col]) + ] + return [row[n:] for row in work] + + +def matrix_rank(matrix: list[list[Q]]) -> int: + work = [[Q(value) for value in row] for row in matrix] + if not work: + return 0 + rows, cols = len(work), len(work[0]) + pivot_row = 0 + for col in range(cols): + pivot = next((row for row in range(pivot_row, rows) + if work[row][col]), None) + if pivot is None: + continue + work[pivot_row], work[pivot] = work[pivot], work[pivot_row] + scale = work[pivot_row][col] + work[pivot_row] = [value / scale for value in work[pivot_row]] + for row in range(rows): + if row == pivot_row: + continue + factor = work[row][col] + if factor: + work[row] = [ + left - factor * right + for left, right in zip(work[row], work[pivot_row]) + ] + pivot_row += 1 + if pivot_row == rows: + break + return pivot_row + + +def interpolation_matrix(points: tuple[int, ...]) -> list[list[Q]]: + finite = [[Q(x**power) for power in range(5)] for x in points] + return finite + [[Q(0), Q(0), Q(0), Q(0), Q(1)]] + + +def check_toom3() -> dict: + points = (0, 1, -1, 2) + interpolation = inverse(interpolation_matrix(points)) + evaluations = [ + [Q(1), Q(0), Q(0)], + [Q(1), Q(1), Q(1)], + [Q(1), Q(-1), Q(1)], + [Q(1), Q(2), Q(4)], + [Q(0), Q(0), Q(1)], + ] + require(matrix_rank(evaluations) == 3, "five evaluation forms span three coefficients") + + basis_pairs = 0 + for left_power in range(3): + for right_power in range(3): + products = [ + Q(x**left_power * x**right_power) for x in points + ] + [Q(int(left_power == 2 and right_power == 2))] + output = [ + sum(interpolation[k][j] * products[j] for j in range(5)) + for k in range(5) + ] + expected = [ + Q(int(k == left_power + right_power)) for k in range(5) + ] + require(output == expected, "Toom-3 basis product identity") + basis_pairs += 1 + + duplicate_rejected = False + try: + inverse(interpolation_matrix((0, 1, -1, -1))) + except ValueError: + duplicate_rejected = True + require(duplicate_rejected, "duplicate evaluation point rejected") + + wrong_products = [ + Q(0), Q(1), Q(-1), Q(0), Q(0) + ] + wrong_output = [ + sum(interpolation[k][j] * wrong_products[j] for j in range(5)) + for k in range(5) + ] + require(wrong_output != [Q(0), Q(1), Q(0), Q(0), Q(0)], + "omitted evaluation product rejected") + + wrong_sign = [Q(0), Q(1), Q(1), Q(2), Q(0)] + wrong_sign_output = [ + sum(interpolation[k][j] * wrong_sign[j] for j in range(5)) + for k in range(5) + ] + require(wrong_sign_output != [Q(0), Q(1), Q(0), Q(0), Q(0)], + "wrong evaluation sign rejected") + + support = [sum(value != 0 for value in row) for row in interpolation] + nonunit_scales = sum( + value not in (0, 1, -1) + for row in interpolation for value in row + ) + decoder_adds = sum(max(0, count - 1) for count in support) + require(support == [1, 5, 4, 5, 1], "interpolation support profile") + require((decoder_adds, nonunit_scales) == (11, 11), + "direct interpolation arithmetic count") + # Per operand: s=a0+a2, then s+a1, s-a1, s+2*a1. + input_adds, input_scales = 8, 2 + require((input_adds + decoder_adds, input_scales + nonunit_scales) + == (19, 13), "two-sided evaluation/interpolation count") + return { + "basis_pairs": basis_pairs, + "products": 5, + "input_evaluation_rank": 3, + "decoder_support": support, + "linear_adds_per_block": 19, + "rational_scales_per_block": 13, + } + + +def profile_of(events: list) -> dict[int, int]: + counts: collections.Counter[int] = collections.Counter() + for event in events: + if event[0] == "climb": + counts[int(event[5])] += 1 + elif event[0] == "center": + counts[int(event[4])] += 1 + return dict(sorted(counts.items())) + + +def is_rank_one(event: list) -> bool: + return event[0] == "climb" and event[5] == 1 + + +def touched(event: list) -> set[tuple[str, int]]: + tag = event[0] + if tag == "climb": + return {("L", event[2])} + if tag in ("adapter", "residual_adapter"): + return {("L", event[2])} + if tag == "center": + return {("C", event[1])} + if tag == "scalar": + operation = event[1] + registers = {tuple(operation[1])} + if operation[0] in ("copy", "add"): + registers.add(tuple(operation[2])) + return registers + return set() + + +def split_phases(events: list) -> list[tuple[list | None, list]]: + result: list[tuple[list | None, list]] = [(None, [])] + for event in events: + if event[0] == "phase": + result.append((event, [])) + else: + result[-1][1].append(event) + return result + + +def batch_phase(body: list) -> tuple[list, list[list], int]: + skeleton = [event for event in body if not is_rank_one(event)] + skeleton_size = len(skeleton) + climbs = [] + gap = 0 + for event in body: + if is_rank_one(event): + climbs.append((len(climbs), gap, event)) + else: + gap += 1 + + touch_positions: dict[tuple[str, int], list[int]] = collections.defaultdict(list) + for index, event in enumerate(skeleton): + for register in touched(event): + touch_positions[register].append(index) + + windows = {} + original_gap = {} + event_by_id = {} + for key, gap, event in climbs: + positions = touch_positions[("L", event[2])] + position = bisect.bisect_left(positions, gap) + lo = positions[position - 1] + 1 if position > 0 else 0 + hi = positions[position] if position < len(positions) else skeleton_size + windows[key] = (lo, hi) + original_gap[key] = gap + event_by_id[key] = event + + same_register_window = collections.Counter( + (event[2], windows[key]) for key, gap, event in climbs + ) + movable = [ + key for key, gap, event in climbs + if same_register_window[(event[2], windows[key])] == 1 + ] + starts: dict[int, list[int]] = collections.defaultdict(list) + for key in movable: + starts[windows[key][0]].append(key) + + available: set[int] = set() + batches: dict[int, list[list[int]]] = collections.defaultdict(list) + batched: set[int] = set() + for gap in range(skeleton_size + 1): + available.update(starts[gap]) + while (any(windows[key][1] == gap for key in available) + and len(available) >= BATCH): + chosen = sorted(available, key=lambda key: (windows[key][1], key))[:BATCH] + batches[gap].append(chosen) + batched.update(chosen) + available.difference_update(chosen) + available = { + key for key in available if windows[key][1] > gap + } + + stays: dict[int, list[int]] = collections.defaultdict(list) + for key, gap, event in climbs: + if key not in batched: + stays[gap].append(key) + + reordered = [] + for gap in range(skeleton_size + 1): + for group in batches[gap]: + destinations = [event_by_id[key][2] for key in group] + require(len(set(destinations)) == BATCH, + "batch destinations are distinct") + reordered.extend(event_by_id[key] for key in group) + reordered.extend(event_by_id[key] for key in stays[gap]) + if gap < skeleton_size: + reordered.append(skeleton[gap]) + + groups = [ + [event_by_id[key] for key in group] + for gap in sorted(batches) for group in batches[gap] + ] + return reordered, groups, len(climbs) + + +def frame_contains(frames: list[list[int]], old: int, new: int) -> bool: + for vector in frames[old]: + reduced = vector + for target in frames[new]: + if reduced >> (target.bit_length() - 1) & 1: + reduced ^= target + if reduced: + return False + return True + + +def replay_frames(events: list, word: dict) -> None: + frames = word["frames"] + current = list(word["start"]) + phase = None + phase_order = [] + for event in events: + tag = event[0] + if tag == "phase": + phase = event[1] + phase_order.append(phase) + elif tag == "climb": + _, part, register, old, new, rank = event + require(part == phase, "climb remains inside its phase") + require(current[register] == old, "rank-one climb old frame") + require(frame_contains(frames, old, new), "nested child frame") + require(rank == len(frames[new]) - len(frames[old]), + "child rank equals frame increment") + current[register] = new + elif tag == "scalar": + operation, frame = event[1], event[2] + registers = [operation[1]] + if operation[0] in ("copy", "add"): + registers.append(operation[2]) + for register in registers: + if register[0] == "L": + require(current[register[1]] == frame, + "scalar event retains its local frame") + elif tag == "center": + require(phase == "centers", "centre events stay in centre phase") + require(current == word["final"], "terminal frame inventory") + + +def dependency_window_batches(programs: dict[str, list], word: dict) -> dict: + receipts = {} + for orientation, events in programs.items(): + require(len(events) == 24767, orientation + " event count") + replay_frames(events, word) + reordered = [] + phase_batches = collections.Counter() + rank_one_count = sum(is_rank_one(event) for event in events) + for marker, body in split_phases(events): + if marker is not None: + reordered.append(marker) + new_body, groups, _ = batch_phase(body) + reordered.extend(new_body) + if marker is not None: + phase_batches[marker[1]] += len(groups) + require(rank_one_count == BASE_PROFILE[1], + orientation + " rank-one climb count") + require( + collections.Counter(json.dumps(event, separators=(",", ":")) + for event in reordered) + == collections.Counter(json.dumps(event, separators=(",", ":")) + for event in events), + orientation + " event multiset preserved", + ) + require( + [event for event in reordered if not is_rank_one(event)] + == [event for event in events if not is_rank_one(event)], + orientation + " every non-rank-one event stays ordered", + ) + replay_frames(reordered, word) + require( + {phase: count for phase, count in phase_batches.items() if count} + == { + "A": 103, "B": 107, "cleanup_tail": 8, "final": 36, + }, + orientation + " phase-local batch counts", + ) + receipts[orientation] = { + "groups": sum(phase_batches.values()), + "by_phase": dict(sorted(phase_batches.items())), + } + require(receipts["forward"] == receipts["backward"], + "orientation window schedules agree") + return receipts + + +def transformed_profile(blocks: int) -> dict[int, int]: + require(0 <= blocks <= BASE_PROFILE[1] // 9, + "Toom block count fits nine original products per block") + profile = dict(BASE_PROFILE) + profile[1] -= 4 * blocks + return dict(sorted(profile.items())) + + +def ceiling(profile: dict[int, int]) -> tuple[Q, Q]: + return tuple(C.ceiling(profile, 73, 896, 9, 64, grid=GRID)) + + +def verify_ceilings() -> dict: + expected = { + 0: ( + Q(52814190909, 31250000000000), + Q(1690054109089, 10**15), + ), + 63: ( + Q(886830512167, 500000000000000), + Q(354732204867, 200000000000000), + ), + 64: ( + Q(887527462991, 500000000000000), + Q(1775054925983, 10**15), + ), + 209: ( + Q(500843727417, 250000000000000), + Q(2003374909669, 10**15), + ), + 210: ( + Q(1002576944357, 500000000000000), + Q(401030777743, 200000000000000), + ), + 254: ( + Q(260836020423, 125000000000000), + Q(417337632677, 200000000000000), + ), + } + brackets = {} + for blocks, wanted in expected.items(): + got = ceiling(transformed_profile(blocks)) + require(got == wanted, "exact ceiling bracket at " + str(blocks)) + brackets[blocks] = got + + five_percent = brackets[0][1] * Q(105, 100) + require(brackets[63][1] < five_percent, "63 blocks miss five-percent gate") + require(brackets[64][0] > five_percent, "64 blocks pass five-percent gate") + require(brackets[209][1] < TARGET_A, "209 blocks miss complex kappa 0.002") + require(brackets[210][0] > TARGET_A, "210 blocks pass complex kappa 0.002") + require(brackets[254][0] > TARGET_A, "254 ideal blocks pass complex threshold") + require(sum(r * n for r, n in transformed_profile(254).items()) == 4570, + "254-block rank mass") + return brackets + + +def main() -> None: + programs, word = source_objects() + selected, bit_a = selected_baseline() + for orientation, events in programs.items(): + require(profile_of(events) == BASE_PROFILE, + orientation + " selected paid profile") + + toom = check_toom3() + windows = dependency_window_batches(programs, word) + brackets = verify_ceilings() + bit_kappa_cap = bit_a / (1 + bit_a) + require(bit_kappa_cap < TARGET_KAPPA, + "unchanged bit side remains below overall target") + + print( + "PASS_COMPLEX_TOOM3_FIVE_PRODUCT_SCREEN " + f"basis_pairs={toom['basis_pairs']} products={toom['products']} " + f"source_groups={windows['forward']['groups']} " + f"min_blocks_5pct=64 min_blocks_complex_kappa002=210 " + f"a254_hi={float(brackets[254][1]):.15f} " + f"complex_kappa254_hi={float(brackets[254][1] / (1 + brackets[254][1])):.15f} " + f"bit_kappa_cap={float(bit_kappa_cap):.15f} controls=3 no_new_kappa" + ) + + +if __name__ == "__main__": + main()