From eb0c12811253174a6a7834c118e2cf827cc9b706 Mon Sep 17 00:00:00 2001 From: Nick Petalas Date: Fri, 23 Oct 2026 10:41:17 +0100 Subject: [PATCH] feat(research): optimize 66-grade extraction to 68 runs --- research/grade-sign-extraction/PROOF.md | 147 +++++++++++++ research/grade-sign-extraction/README.md | 97 +++++++++ research/grade-sign-extraction/SOURCES.json | 30 +++ research/grade-sign-extraction/TASK.json | 78 +++++++ research/grade-sign-extraction/VERIFY.json | 34 +++ research/grade-sign-extraction/verify.py | 216 ++++++++++++++++++++ 6 files changed, 602 insertions(+) create mode 100644 research/grade-sign-extraction/PROOF.md create mode 100644 research/grade-sign-extraction/README.md create mode 100644 research/grade-sign-extraction/SOURCES.json create mode 100644 research/grade-sign-extraction/TASK.json create mode 100644 research/grade-sign-extraction/VERIFY.json create mode 100644 research/grade-sign-extraction/verify.py diff --git a/research/grade-sign-extraction/PROOF.md b/research/grade-sign-extraction/PROOF.md new file mode 100644 index 0000000000..6cdff80f1e --- /dev/null +++ b/research/grade-sign-extraction/PROOF.md @@ -0,0 +1,147 @@ +# The exact 66-grade equal-weight sign tariff is 68 executions + +This scoped proof and finite sign certificate were produced by Nick Petalas. +Apache-2.0. + +## Contract and theorem + +Let `V = V_0 direct-sum ... direct-sum V_65`, with each grade nonzero and +of arbitrary finite dimension over a field of characteristic zero. A completed +linear program W acts on V. A root wrapper may run W K times, conjugating +execution t by a grade-constant sign map D_t, and return the equal average + +``` + (1/K) sum_t D_t W D_t. +``` + +The same signs must extract the block diagonal of W for every W. The signs +are independent of W and its input. Signs have entries +1 or -1, and each +execution has the same positive weight 1/K. Each complete W is one charged +execution. The target is the smallest K in this contract. + +**Theorem.** The exact minimum is K=68. For dyadic W and dyadic inputs, +the wrapper can retain a common exact per-execution output grid and recover +the diagonal output by an exact binary shift and an exact integer division +by 17. It does not require a persistent odd denominator in the recovered output. + +This concerns the extraction wrapper, not a native implementation of W. +The sign design and its arithmetic are independent of the dimensions of the +grades. The conclusion also holds when the allowed W are arbitrary block +lower-triangular operators: each unordered grade pair still has an allowed +off-diagonal block whose cancellation is necessary. + +## Lower bound + +Write s_th for the sign on grade h in execution t, and S for the K-by-66 +sign array. On block (h,j) the multiplier is + +``` + (1/K) sum_t s_th s_tj. +``` + +Testing one nonzero off-diagonal block forces distinct columns of S to be +orthogonal. Thus `S^T S=K I_66`, and rank gives K>=66. + +Take any three columns. Multiplying each row by its first-column sign +normalizes the first column to all ones without changing dot products. +The other two columns are balanced. If their four joint sign counts are +`n_++, n_+-, n_-+, n_--`, balance and their mutual orthogonality give +`n_++=n_+-=n_-+=n_--=K/4`. Consequently 4 divides K. The least multiple +of four at least 66 is 68. + +No lower bound is asserted for unequal weights, independent input/output +maps, complex phases, W-dependent signs or exploiting missing blocks of a +particular W. + +## Attainment from the classical Paley construction + +Use the prime p=67 and its quadratic character chi, with chi(0)=0. Index +rows and columns of Q by the residues modulo 67 and set + +``` + Q_ij = chi(i-j), H = [1 1^T] + [1 Q-I]. +``` + +This is the classical Paley order-68 Hadamard matrix. Paley's construction, +Hadamard orthogonality and character averaging are credited methods, not +new constructions claimed here. See R. E. A. C. Paley, [On Orthogonal +Matrices](https://doi.org/10.1002/sapm1933121311), 1933, pp. 311-320. + +Here is a self-contained proof at the required prime. The nonzero squares +are half the nonzero residues, so each Q row sums to zero. Since 67 is 3 +modulo 4, chi(-1)=-1 and Q is skew-symmetric. For a != b, + +``` + sum_x chi(x-a) chi(x-b) = -1. +``` + +Indeed, the number of pairs (x,y) satisfying +`y^2=(x-a)(x-b)` equals 67 plus that sum. The invertible change of +variables `u=2x-a-b-2y`, `v=2x-a-b+2y` gives +`uv=(a-b)^2`, which has exactly 66 solutions, one per nonzero u. +For a=b the character dot product is 66. Therefore +`Q Q^T=67I-J`. Skew-symmetry now gives + +``` + (Q-I)(Q-I)^T = 68I-J, H H^T = 68I. +``` + +The top/bottom cross terms vanish because each Q row sums to zero. +As H is square, `H^T H=68I` also follows. Take its first 66 columns as S. +These columns give exactly the required extraction by the block multiplier +formula. The verifier independently enumerates the quadratic residues, +constructs H and checks every entry of its upper-triangular Gram matrix. + +## Exact dyadic recovery and the extra arithmetic + +Suppose a chosen common grid `2^-b` represents all execution outputs and +the desired block-diagonal output y. For dyadic operator entries and inputs, +choose b at least the sum of their denominator exponents, and any larger +exponent needed by the intermediate-value contract. This represents y because +its coordinates are sums of a subset of the same matrix-entry/input products. +A grid guaranteed only for one W/input pair by cancellation is insufficient. + +Write `y=2^-b z`, with z an integer vector. Orthogonality proves that the +signed-output accumulator is exactly `2^-b (68z)`. +Thus its numerator is divisible by four and then by 17. Divide first by +four and then divide the integer numerator exactly by 17. The quotient is +z on the same grid. Division by 68 is not an arbitrary free dyadic scalar; +the divisibility proof makes this implementation legitimate. + +Signed binary long division by 17 is explicit. Scan the magnitude bits +from left to right, maintaining `0<=r<17`; replace r by `2r+bit`, emit one +quotient bit if this is at least 17, and subtract 17 in that case. Restore +the input sign. The final remainder is zero on the proved accumulator. +This takes one pass over the numerator with a bounded remainder alphabet. +`verify.py` implements and tests this procedure, including negative values +and rejection of inexact division. Its 512-bit test cap bounds finite +corroboration only; the written recurrence works for every finite width. + +If every signed execution output numerator has magnitude at most M, the +accumulator magnitude is at most 68M, so seven extra integer bits suffice. +Signs preserve input magnitudes and exact grids. Use the original preserved +input, one reusable complete-run workspace and one accumulator. The cost is +68 complete executions plus sign/copy/accumulation passes and exact division. +For N scalar coordinates of bounded numerator width B, the wrapper's own +bit work is O(68NB), including division, under sequential binary storage. +No full fixed-tape instruction tariff is claimed. Only the original inputs +receive productive credit, and the wrapper is placed once at the root. + +## Application to the cited bounded-lane interface + +Yiyang's [PR #5037](https://github.com/CrocSwap/integer-mult-bounds/pull/5037) +supplies the root-only same-grade extraction contract in +`research/bounded-lane-border-core/precision/EXTRACTION.md`. At 66 grades, +its Walsh wrapper uses `2^ceil(log2(66))=128` complete executions. The above +wrapper uses 68, saving 60 complete executions, a fraction 15/32 of the +previous wrapper's count. It is optimal in the declared equal-weight sign +contract. The seven-bit accumulator bound is unchanged. + +This does not prove a wall-clock speedup or change the local native invoice. +The actual native shears, recursive source/exit matching, rounded execution, +physical ownership/movement/restoration, stock, BIT transfer and outer admission +remain obligations of the predecessor program. No new kappa or complete +multiplication theorem follows. The Paley construction itself is classical; +the contribution is the sharp extraction count and exact-divisibility wrapper +for this declared graded interface. diff --git a/research/grade-sign-extraction/README.md b/research/grade-sign-extraction/README.md new file mode 100644 index 0000000000..a1aca86e23 --- /dev/null +++ b/research/grade-sign-extraction/README.md @@ -0,0 +1,97 @@ +# Optimal 66-grade sign extraction in 68 complete executions + +For 66 nonzero grades, the exact minimum is **68 complete executions** when +extracting arbitrary diagonal blocks by an equally weighted average of +grade-sign conjugations. Yiyang's [PR #5037](https://github.com/CrocSwap/integer-mult-bounds/pull/5037) +uses a 128-run Walsh wrapper at this grade count. The new wrapper saves 60 +complete executions. This is a scoped algebraic proof with an exact finite +sign certificate, not a new kappa or a complete multiplication theorem. + +[PROOF.md](PROOF.md) proves the lower bound by rank and three-column sign +divisibility, and attains it using the classical Paley order-68 Hadamard +matrix. Paley's construction is credited, not claimed as new. Each execution +uses the same complete program with signed input and output. Only original +independent inputs receive productive credit. + +The average includes a factor 1/68, which is not dyadic. The proof establishes +that the common-grid integer accumulator is exactly divisible by 68. A binary +shift and exact integer division by 17 recover the result on the same dyadic +grid. The verifier includes a bit-by-bit signed divider. Seven extra accumulator +bits suffice, as for 128-run Walsh averaging. Copying, signs, accumulation and +division remain additional work; a lower total runtime is not established. + +## Evidence and scope + +The standalone standard-library verifier constructs every sign and checks +2,346 upper-triangular entries of the full 68-column Gram matrix. It executes +28 dense/triangular real-component fixtures with unequal grade dimensions, +covering 1,712 output components. These real-component checks also apply +separately to both components of Gaussian dyadic data. A separate six-coordinate +variable-denominator execution checks exact dyadic recovery. The binary divider +passes 8,201 signed exact-division fixtures, and 11 corruption/domain controls +reject. These are finite corroboration; the universal conclusion is the proof. + +The contract fixes equal positive weights and sign-only conjugations that +extract every possible off-diagonal block. Unequal weights, arbitrary complex +phases, independently chosen input/output maps and special sparsity in a +particular program can change the problem. No optimality is asserted there. +This wrapper is applied once at the root. Native shears, recursive matching, +rounded arithmetic, physical implementation, row stock, BIT and outer admission +are not supplied or replayed here. The selected frontier is unchanged. + +The original card is `complex-rank-shape-decoder`; [TASK.json](TASK.json) +records the changed scope to this extraction side result and the stop rule. +The scientific base is `81e9bd69e4a5b00ba9c713a1620e36d576d5522e`, priority +revision 7. [SOURCES.json](SOURCES.json) pins the predecessor extraction note, +README, selected record and original card as Git provenance. No predecessor +executable or live input is needed. The [advance claim](https://github.com/CrocSwap/integer-mult-bounds/discussions/1166#discussioncomment-19142748) +records the target and bounded overlap checks. + +## Reproduce + +From the repository root, with CPython 3.12: + +```sh +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -B scripts/research_package.py --package research/grade-sign-extraction +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -W error -B research/grade-sign-extraction/verify.py +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -B scripts/check_research_commit.py --package research/grade-sign-extraction --check-source-history +``` + +Substitute the local interpreter path on another host. Expected direct output +contains `PASS_GRADE_SIGN_EXTRACTION`, `runs: 68`, `literal_cases: 28` and +`signed_exact_divisions: 8201`. Registered replay runs both normal and optimized +Python with warnings as errors. The verifier writes no files and has no imports +from predecessor packages. + +All verifier loops have fixed finite bounds. The largest literal operator is +102 by 102, with 68 executions; the binary divider has a 512-bit evidence cap. +The functions are under 60 lines and failures raise explicit exceptions, so +optimized Python retains every check. Power-of-Ten rule 3 has a managed-runtime +deviation: Python allocates objects during execution. Fixed matrix, integer and +execution capacities bound the live mathematical data; garbage collector and +interpreter allocation are not inspected. Rule 10 uses warning-strict normal +and optimized replay plus AST parsing; no separate static analyzer is required +by this package. A native port would need its own allocation and diagnostic +policy. No physical runtime measurement is inferred from verifier timings. + +## Open next questions + +- Bind one actual graded native program to the complete root extraction + contract. Start with a literal source/exit-matched caller from #5037 and its + same-grade Gaussian blocks. Coherent native shears and recursive matching + remain blockers; another sign design does not discharge them. +- Price this 68-run wrapper and the 128-run Walsh wrapper at that completed + physical caller. Include all signed copies, workspace reuse, accumulation + and exact division by 17. A complete ownership, grid and movement contract + is required before the count improvement can imply a complete cost gain. + +## Credit + +This contribution was produced by Nick Petalas. Credit Yiyang / Lyy-iiis +#5037 for the bounded-lane and root-only extraction setting, Dillon / #4623 +for border recovery, Cao Yifan / #4610 for the native-word context, Jacob Alan +Sussman and the finite-precompiler contributors, and the retained native, +helper, semantic precision and outer lineage. The classical Paley/Hadamard +construction and sign orthogonality retain their credit. No literature-priority +claim is made for those methods. Predecessor files and notices are unchanged. +Apache-2.0. Independent human and formal proof-kernel review are unclaimed. diff --git a/research/grade-sign-extraction/SOURCES.json b/research/grade-sign-extraction/SOURCES.json new file mode 100644 index 0000000000..2eb39181b2 --- /dev/null +++ b/research/grade-sign-extraction/SOURCES.json @@ -0,0 +1,30 @@ +{ + "schema_version": 1, + "base_commit": "81e9bd69e4a5b00ba9c713a1620e36d576d5522e", + "files": [ + { + "path": "certificates/selected-result.json", + "usage": "provenance", + "git_blob": "c35482cd9035843b15a634cc8276a7e1e40e8395", + "sha256": "6fe0a099b89aa2aed796a13c3a21dafc63f3388ebf397464e44e0451893b9375" + }, + { + "path": "docs/research/agent-task-catalog.json", + "usage": "provenance", + "git_blob": "f126dd2e2dad2b669e75d1d0d0bd05c600725602", + "sha256": "f7a48bfa2659b88378282a8eee7d9fe52cdf505594124c4d1686b58510fffd72" + }, + { + "path": "research/bounded-lane-border-core/README.md", + "usage": "provenance", + "git_blob": "30449eb1e9f70bbce76ee4db3046d79ea8e593c7", + "sha256": "78b85269d7f931e080e3ca263584761859656ea76d8ba509023101d3b04340d7" + }, + { + "path": "research/bounded-lane-border-core/precision/EXTRACTION.md", + "usage": "provenance", + "git_blob": "2a8d8ccc27c3e8e4e6ab743ff230738284f32d36", + "sha256": "69120b010241ea40244753304da0fb39744c941b1c39a70273a4c88707e3222d" + } + ] +} diff --git a/research/grade-sign-extraction/TASK.json b/research/grade-sign-extraction/TASK.json new file mode 100644 index 0000000000..cb97187a49 --- /dev/null +++ b/research/grade-sign-extraction/TASK.json @@ -0,0 +1,78 @@ +{ + "schema_version": 1, + "task_id": "complex-rank-shape-decoder", + "priority_revision": 7, + "base_commit": "81e9bd69e4a5b00ba9c713a1620e36d576d5522e", + "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": "Prove that arbitrary 66-grade block-diagonal extraction by equally weighted grade-sign conjugations costs exactly 68 complete executions, and construct exact dyadic recovery by integer division.", + "scope_change_reason": "A new root-extraction wrapper side result for Yiyang PR5037, rather than a fixed-support decoder or a replay of predecessor counts. The original native shears and recursive matching remain open.", + "bottleneck_link": "The scoped wrapper cuts the cited 66-grade root extraction from 128 complete executions to 68. This affects an outer constant only; local invoices, native admission and kappa are unchanged.", + "budget_minutes": 60, + "stop_rule": "Stop and package the first proved original wrapper improvement; do not extend the design search or attempt native admission.", + "outcome": "completed", + "evidence": "PROOF.md proves the universal sign-only lower bound, classical Paley attainment and integer divisibility. verify.py constructs the 68-row sign certificate and executes independent exact block/input and long-division checks.", + "author": "Nick Petalas", + "ai_assistance": "Independent human and formal proof-kernel review are unclaimed.", + "open_questions": [ + { + "question": "Can the actual graded native program satisfy the complete root extraction contract?", + "why_it_matters": "The wrapper cannot supply absent coherent shears or recursive matching.", + "first_step": "Bind one literal source/exit-matched caller from PR5037 to its same-grade Gaussian blocks.", + "blocked_by": "Actual native and whole-job recursive contracts remain open." + }, + { + "question": "What is the complete physical cost of this 68-run wrapper versus the 128-run Walsh wrapper?", + "why_it_matters": "Execution count alone is not wall-clock speed or a full tape tariff.", + "first_step": "Price the signed copies, complete workspace reuse, accumulator and exact division by 17 at one physical caller.", + "blocked_by": "Requires a completed native program and its ownership, grid and movement contracts." + } + ] +} diff --git a/research/grade-sign-extraction/VERIFY.json b/research/grade-sign-extraction/VERIFY.json new file mode 100644 index 0000000000..342aa89d63 --- /dev/null +++ b/research/grade-sign-extraction/VERIFY.json @@ -0,0 +1,34 @@ +{ + "schema_version": 1, + "status": "ready", + "python": "3.12", + "requirements": [], + "inputs": [], + "commands": [ + { + "argv": [ + "{python}", + "-I", + "-W", + "error", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 120, + "checks": "Exact Paley Gram certificate, literal dense and triangular unequal-grade extraction, independent variable-denominator execution, signed binary division and rejection controls." + }, + { + "argv": [ + "{python}", + "-I", + "-O", + "-W", + "error", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 120, + "checks": "The same exact evidence with Python assertions disabled." + } + ] +} diff --git a/research/grade-sign-extraction/verify.py b/research/grade-sign-extraction/verify.py new file mode 100644 index 0000000000..6e3aa4cc35 --- /dev/null +++ b/research/grade-sign-extraction/verify.py @@ -0,0 +1,216 @@ +#!/usr/bin/env python3 +"""Exact finite evidence for the optimal 66-grade sign extraction wrapper.""" + +import json +from fractions import Fraction +from hashlib import sha256 +from pathlib import Path + +MAX_GRADES = 68 +MAX_COORDINATES = 102 +MAX_BITS = 512 + + +def require(condition, message): + if not condition: + raise ValueError(message) + + +def paley(): + """Classical Paley matrix at the fixed prime 67, including its border.""" + squares = {x * x % 67 for x in range(1, 67)} + result = [[1] * 68] + for row in range(67): + core = [-1 if row == col else + (1 if (row - col) % 67 in squares else -1) + for col in range(67)] + result.append([1] + core) + return tuple(tuple(row) for row in result) + + +def check_signs(signs, grades): + require(type(grades) is int and 1 <= grades <= MAX_GRADES, + "grade capacity") + require(type(signs) is tuple and 1 <= len(signs) <= 128, + "execution capacity") + require(all(type(row) is tuple and len(row) == grades for row in signs), + "rectangular sign array") + require(all(type(x) is int and x in (-1, 1) for row in signs for x in row), + "literal signs") + for a in range(grades): + for b in range(a + 1): + dot = sum(row[a] * row[b] for row in signs) + require(dot == (len(signs) if a == b else 0), + "nonorthogonal grade columns") + + +def divide_by_17(value): + """Signed exact binary long division, with a bounded 17-state remainder.""" + require(type(value) is int and abs(value).bit_length() <= MAX_BITS, + "integer capacity") + negative = value < 0 + magnitude = abs(value) + quotient = remainder = 0 + for position in range(magnitude.bit_length() - 1, -1, -1): + remainder = 2 * remainder + ((magnitude >> position) & 1) + bit = int(remainder >= 17) + remainder -= 17 * bit + quotient = 2 * quotient + bit + require(0 <= remainder < 17, "division remainder state") + require(remainder == 0, "inexact odd division") + result = -quotient if negative else quotient + require(17 * result == value, "division postcondition") + return result + + +def average_accumulator(value, runs): + require(runs == 68, "this exact divider is for the 68-run wrapper") + require(type(value) is int and value % 4 == 0, "inexact binary shift") + return divide_by_17(value // 4) + + +def literal_wrapper(matrix, vector, labels, signs): + """Execute each signed dense integer matrix-vector call before averaging.""" + n = len(vector) + require(1 <= n <= MAX_COORDINATES and len(labels) == n, "coordinate cap") + require(len(matrix) == n and all(len(row) == n for row in matrix), + "square operator") + require(all(type(x) is int for row in matrix for x in row), + "integer common-grid operator") + require(all(type(x) is int for x in vector), "integer common-grid input") + require(all(type(h) is int and 0 <= h < len(signs[0]) for h in labels), + "grade labels") + accum = [0] * n + for pattern in signs: + signed_input = [pattern[labels[i]] * vector[i] for i in range(n)] + output = [sum(a * b for a, b in zip(row, signed_input, strict=True)) + for row in matrix] + for i in range(n): + accum[i] += pattern[labels[i]] * output[i] + return tuple(average_accumulator(x, len(signs)) for x in accum) + + +def diagonal_reference(matrix, vector, labels): + return tuple(sum(matrix[i][j] * vector[j] for j in range(len(vector)) + if labels[i] == labels[j]) for i in range(len(vector))) + + +def fixtures(signs): + cases = components = 0 + digest = sha256() + for grades in (1, 3, 17, 65, 66, 67, 68): + labels = tuple(h for h in range(grades) for _ in range(1 + h % 2)) + n = len(labels) + patterns = tuple(tuple(row[:grades]) for row in signs) + check_signs(patterns, grades) + for triangular in (False, True): + for component in (0, 1): + matrix = tuple(tuple( + 0 if triangular and labels[i] < labels[j] else + ((37 * i + 19 * j + 7 * component + i * j) % 23 - 11) + for j in range(n)) for i in range(n)) + vector = tuple((i * i + 3 * i + component) % 13 - 6 + for i in range(n)) + actual = literal_wrapper(matrix, vector, labels, patterns) + expected = diagonal_reference(matrix, vector, labels) + require(actual == expected, "literal block extraction") + digest.update(repr((grades, triangular, component, actual)).encode()) + cases += 1 + components += n + return cases, components, digest.hexdigest() + + +def fractional_fixture(signs): + """Compare rational averaging with integrated common-grid integer recovery.""" + labels = (0, 1, 1, 2, 2, 2) + vector = tuple(Fraction(i - 3, 2 ** (i % 3)) for i in range(6)) + matrix = tuple(tuple(Fraction(3 * i - 2 * j + 1, 2 ** ((i + j) % 4)) + for j in range(6)) for i in range(6)) + accumulated = [Fraction(0)] * 6 + for row in signs: + incoming = tuple(row[labels[j]] * vector[j] for j in range(6)) + for i in range(6): + accumulated[i] += row[labels[i]] * sum( + matrix[i][j] * incoming[j] for j in range(6)) + expected = diagonal_reference(matrix, vector, labels) + actual = tuple(value / 68 for value in accumulated) + require(actual == expected, "independent fractional execution") + scaled = tuple(value * 32 for value in accumulated) + require(all(value.denominator == 1 for value in scaled), + "one common exact accumulator grid") + recovered = tuple(Fraction(average_accumulator(value.numerator, 68), 32) + for value in scaled) + require(recovered == expected, "common-grid integer division recovery") + require(all(value.denominator & (value.denominator - 1) == 0 + for value in actual), "no residual odd denominator") + return len(actual) + + +def rejection_controls(signs): + rejected = 0 + bad = [list(row[:66]) for row in signs] + bad[0][0] *= -1 + duplicate = tuple(tuple(row[:65]) + (row[0],) for row in signs) + candidates = ( + (tuple(tuple(row) for row in bad), 66), + (tuple(tuple(row[:66]) for row in signs[:-1]), 66), + (duplicate, 66), + (tuple(tuple(row[:66]) for row in signs), 0), + (((True,),), 1), + ) + for candidate, grades in candidates: + try: + check_signs(candidate, grades) + except ValueError: + rejected += 1 + else: + raise ValueError("sign control was accepted") + for value in (1, -1, 18, 1 << (MAX_BITS + 1), True): + try: + divide_by_17(value) + except ValueError: + rejected += 1 + else: + raise ValueError("division control was accepted") + try: + average_accumulator(17, 68) + except ValueError: + rejected += 1 + else: + raise ValueError("inexact shift accepted") + return rejected + + +def main(): + signs = paley() + check_signs(signs, 68) + cases, components, digest = fixtures(signs) + divisions = 0 + for quotient in range(-4096, 4097): + require(divide_by_17(17 * quotient) == quotient, "signed exact division") + divisions += 1 + for bits in (64, 127, 255, 500): + for sign in (-1, 1): + quotient = sign * ((1 << bits) - 1) + require(divide_by_17(17 * quotient) == quotient, "wide exact division") + divisions += 1 + # Rank plus the elementary three-column divisibility obstruction. + candidates = tuple(k for k in range(66, 69) if k % 4 == 0) + require(candidates == (68,), "minimum eligible execution count") + report = { + "status": "PASS_GRADE_SIGN_EXTRACTION", + "grades": 66, "runs": 68, "previous_walsh_runs": 128, + "paley_gram_entries": 68 * 69 // 2, + "literal_cases": cases, "output_components": components, + "fractional_output_components": fractional_fixture(signs), + "signed_exact_divisions": divisions, + "rejection_controls": rejection_controls(signs), + "fixture_sha256": digest, + "sign_array_sha256": sha256(repr(signs).encode()).hexdigest(), + "verifier_sha256": sha256(Path(__file__).read_bytes()).hexdigest(), + } + print(json.dumps(report, sort_keys=True)) + + +if __name__ == "__main__": + main()