diff --git a/research/selected-bit-mixed-descriptor/PROOF.md b/research/selected-bit-mixed-descriptor/PROOF.md new file mode 100644 index 0000000000..f4090832e1 --- /dev/null +++ b/research/selected-bit-mixed-descriptor/PROOF.md @@ -0,0 +1,224 @@ +# One mixed good/fallback weight descriptor + +The contribution is an explicit descriptor and its executable continuation, +not a new normal-form or fallback theorem. Diagonal covariance is retained +from source527 and Ross Richardson's PR #2159. Jose Manuel's PR #2171 +implements good-only ancestral weights and asks for a mixed path. This note +gives the row and atom indices for its diagonal-pair subcase. The selected +good-child binding is from commdata2338's PR #2160. Exact heads and proof +hashes are in [SOURCES.json](SOURCES.json). + +## 1. Conditional interface + +Let `R = Z/q^w`, with q an odd prime and w >= 1. An ADDRESS residual on +`N = 2m` coarse fields acts independently on f atom columns. Its full +unweighted matrix M is invertible over R, with determinant +1 or -1. +At column j its weighted matrix is + +``` +M_j = B_j M B_j^-1, +B_j = diag(b_0(j), ..., b_(N-1)(j)), +b_i(j) = A[i*f+j] if i < m, and 1 otherwise. +``` + +Every A entry is a unit. M and A depend only on exposed parent/ancestor +fields and fixed labels, never active ADDRESS values, VALUE payloads, or +fresh descendant stock. These are explicit premises. The native selected +to stopped-GL domain bridge is not proved here. + +For n incoming head atoms set `f=floor(n/m)>0`, use the first mf head +atoms and the corresponding reversed tail block, and retain the peeled +remainder as spectators under the inherited peeling contract. + +The first field of a diagonal pair has index i < N-1; its partner is N-1. +Physical atom t represents logical column `sigma_i(t)=t` or `f-1-t`. +The selected layout uses the first orientation for heads and the second +for tails. This map matters even when both fields are reversed. + +## 2. The row-permutation quotient + +Run unnormalized unit-pivot Gaussian elimination on M, clearing all other +rows at each stage and retaining zero shear slots. Choose the first unit +in the current pivot column. Let D be the final diagonal, with entries +d_i. Track original row labels through swaps: `pi(i)` is the original row +occupying final row i, not the list of pivot choices. + +For one column abbreviate B_j to B. During elimination the weighted and +unweighted matrices satisfy + +``` +T_weighted = L T_unweighted B^-1, +``` + +where L is diagonal and initially equals B. A row swap S changes L to +`S L S^-1`. A shear clearing row i using pivot row k has weighted +coefficient `(L_i/L_k)` times the unweighted coefficient: the right +diagonal entry cancels from the quotient of entries in the same column. +This shear preserves the displayed relation. + +Unit scaling preserves the low-residue zero pattern, so both runs choose +the same first unit row at every step. At termination `L_i=b_pi(i)(j)`: + +``` +d_i(j) = d_i * b_pi(i)(j) / b_i(j). (1) +``` + +All divisors are units. The row itinerary is determined by M modulo q, +but d_i must be computed over the full ring. Replacing M by its low +representatives can change d_i with no change of itinerary. Incoming A +values likewise need their full ancestors. + +The physical descriptor for the weighted reversed child is + +``` +a_child[t] = d_i * b_pi(i)(sigma_i(t)) / b_i(sigma_i(t)). (2) +``` + +`FallbackPair.value` implements (2). Its possible incoming reads are +`pi(i)*f+sigma_i(t)` and `i*f+sigma_i(t)`; a tail-bank read becomes the +constant 1. Both head indices lie in the parent's first mf atoms. The +fallback generally has no same-flat-index product rule. + +## 3. Composition with good ancestors + +For the retained northeast good normal form whose active head coordinates +are `0,...,r-1`, source527 and PR #2160 give + +``` +A_good[k*f+j] = A_parent[k*f+j] * delta_k. (3) +``` + +Delta is the unweighted full-ring pivot vector. `Good.value` implements +(3); no different `i_k` map or unbound local-frame label is assumed. +Starting with a unit root weight, (2) and (3) inductively give only units. +Every node reads older permitted controls and at most two entries of the +previous descriptor. A requested weight can therefore be generated +without a supplied ancestor-weight oracle or access to unused child stock. +This also covers a good node after a fallback node: its incoming descriptor +is the fallback's, not the original root array. + +This closure statement covers the named good normal forms and fallback +diagonal-pair children. It does not compile all fallback shear coefficients, +arbitrary role labels, or complete selected invocations. + +## 4. Source-bound rank-42 example + +The pinned selected template has idle child 6, rank 42, from level 2 to +level 8 on pair 0. Copy 213 hosts no part of it. Its retained projector is +`P0=diag(0_22,I_42)`; the selected receipt contains 288 rank-42 children. +The verifier authenticates the template, its decoded hash and selected-word +binding, and the PR1094 assembly's Git-blob citation of PR589's receipt. + +For the GL-domain examples, S sends P0's last 42 axes to the first 42. +Each chart is explicitly `g=h S`. This is a chart in the assumed GL orbit, +not an uncharged physical route or an emitted orthogonal-cover invocation. + +Modulo 25, a good ancestor has disjoint blocks `[[1,1],[c,1+c]]` on axes +`(0,43),(1,42),(25,44)`, with c=5,10,15. All determinants are one. Exact +northeast elimination gives `delta_0=6`, `delta_1=11`, `delta_25=16`, all +others one. At root column width 5 its child has 210 atoms and +`A[x]=delta_floor(x/5)`. + +The next parent uses f=3 and peels 18 atoms. Its chart has blocks +`[[5,4],[1,1]]` on `(0,43)` and `[[1,0],[1,1]]` on `(1,42)`, both of +determinant one. Its projector has `P[0,0]=5`, a nonzero nonunit, so the +generic good branch is unavailable. Its full 128-coordinate residual is + +``` +M = [[I-P, P C_64], [C_64 P, I-C_64 P C_64]]. +``` + +This is an involution of determinant one. Its exact fallback plan gives +`pi(1)=42, d_1=-1` and `pi(126)=1, d_126=1`. +For the first pair, (2) reads two different ancestor locations: + +``` +numerator: A[126], A[127], A[128] = 16,16,16 +denominator: A[3], A[4], A[5] = 6, 6,11 +child: -16/6, -16/6, -16/11 = 14,14,19 mod 25. +``` + +Field 126 has denominator one and reversed first-field orientation, so +its descriptor is `A[5],A[4],A[3] = 11,6,6`. Omitting orientation gives +`6,6,11`; using only the good ancestor's low residues gives `1,1,1`. + +A further fixture starts with 6,400 atoms. Its good child has 4,200; the +bad parent uses f=65 and peels 40; the diagonal-pair child has 65. The next +good parent uses f=1, peels one atom and produces 42 weights. Entries +`0,1,29,30,41` are `16,21,11,6,6`. A separate forward-elimination oracle +computes all 65 incoming fallback weights before the final comparison. +This exercises a good/fallback/good descriptor chain, with callees still +conditional. + +## 5. Chronology, reversals and charges + +The completed weighted reversed-child premise is + +``` +F_a(H,D) = (diag(a) C_f D, C_f diag(a)^-1 H), +``` + +preserving exposed controls and complete spectators and restoring its row. +A logical diagonal pair is `J_a J_1`: execute J_1 first, then J_a. +For equal field orientations, each ordinary swap is `R_tail F_a R_tail`. +Each reversal is one unweighted reversed child on the first/last +`floor(f/2)` atoms, with a possible middle atom fixed. The tested tail126 +pair at f=3 thus pays six children of widths `1,3,1,1,3,1`. For opposite +orientations each swap is already a reversed child: field1's pair uses two +width-three children. In both cases the physical weights are (2). + +No reversal or variable scaling is free. The inherited fallback allowance +remains + +``` +6N(N-1) + 3N + 6(N-1) = 98,682 < 32*64^2 = 131,072, +``` + +with every child at most f atoms. This is the prior conservative allowance, +not a selected cost improvement. The preceding good call still pays its +one 42f child and adapters. Later-target shifts, the fixed determinant +sign, preparation and required routing remain charged. + +For fixed q and m=64, an unweighted plan needs a fixed number of ring operations +on O(w)-digit values and is prepared from the full exposed chart once. +A good descriptor follows one parent index; (2) follows at most two. With +D ancestors a direct depth-first evaluator uses at most +`O(2^D poly(w,log n_root,D))` control work per value, and +`O(D(w+log n_root))` storage for fixed-dimensional prepared data and its +stack. The executable realizes this finite recursion; no memoization or +unit-cost arbitrary-prefix oracle is assumed. + +As in source527 and PR2171, impose `n_root<=E0`, +`D<=c(1+log2 E0)`, and `w=ceil(E0^s)` for fixed `0 0 and all(len(row) == n for row in matrix), "square matrix") + a = [[x % modulus for x in row] for row in matrix] + pi, pivots, operations = list(range(n)), [], [] + for k in range(n): + p = next((i for i in range(k, n) if gcd(a[i][k], modulus) == 1), None) + require(p is not None, "fallback unit pivot") + pivots.append(p) + if p != k: + a[k], a[p] = a[p], a[k] + pi[k], pi[p] = pi[p], pi[k] + operations.append(("swap", k, p, 0)) + inv = pow(a[k][k], -1, modulus) + for i in range(n): + if i == k: + continue + c = -a[i][k] * inv % modulus + operations.append(("add", i, k, c)) # Keep zero slots. + if c: + a[i] = [(x + c*y) % modulus for x, y in zip(a[i], a[k])] + require(all(a[i][j] == 0 for i in range(n) for j in range(n) if i != j), + "fallback diagonal endpoint") + return Plan(modulus, tuple(a[i][i] for i in range(n)), tuple(pi), + tuple(pivots), tuple(operations)) + + +@dataclass(frozen=True) +class Root: + size: int + modulus: int + unit: int = 1 + + def value(self, index): + require(0 <= index < self.size, "root index extent") + require(gcd(self.unit, self.modulus) == 1, "root weight unit") + return self.unit % self.modulus + + +@dataclass(frozen=True) +class Good: + parent: object + dimension: int + columns: int + pivots: tuple + + def __post_init__(self): + require(self.dimension > len(self.pivots) > 0, "good rank") + require(self.columns == self.parent.size // self.dimension and self.columns > 0, + "good coarse child width") + require(all(gcd(x, self.modulus) == 1 for x in self.pivots), "good pivot units") + + @property + def modulus(self): + return self.parent.modulus + + @property + def size(self): + return len(self.pivots) * self.columns + + def value(self, index): + require(0 <= index < self.size, "good child index extent") + k, _ = divmod(index, self.columns) + return self.parent.value(index) * self.pivots[k] % self.modulus + + +@dataclass(frozen=True) +class FallbackPair: + """Weights for F_a within a weighted diagonal-pair J_a J_1. + + The first physical field is `field`; its atom orientation can be reversed. + The head-bank conjugator has parent weights, the tail-bank conjugator 1. + `plan` is prepared once from the FULL UNWEIGHTED parent residual. + """ + parent: object + dimension: int + columns: int + plan: Plan + field: int + reverse: bool + + def __post_init__(self): + n = 2 * self.dimension + require(self.columns == self.parent.size // self.dimension and self.columns > 0, + "fallback coarse child width") + require(self.plan.modulus == self.modulus, "fallback plan modulus") + require(len(self.plan.diagonal) == n and len(self.plan.permutation) == n, + "fallback plan dimension") + require(sorted(self.plan.permutation) == list(range(n)), "fallback row permutation") + require(0 <= self.field < n-1, "paired diagonal field") + require(all(gcd(d, self.modulus) == 1 for d in self.plan.diagonal), "fallback diagonal units") + + @property + def modulus(self): + return self.parent.modulus + + @property + def size(self): + return self.columns + + def bank_weight(self, row, column): + if row < self.dimension: + return self.parent.value(row * self.columns + column) + return 1 + + def value(self, index): + require(0 <= index < self.size, "fallback child index extent") + j = self.columns - 1 - index if self.reverse else index + numerator = self.bank_weight(self.plan.permutation[self.field], j) + denominator = self.bank_weight(self.field, j) + return (self.plan.diagonal[self.field] * numerator * + pow(denominator, -1, self.modulus)) % self.modulus + + +def weighted_residual(matrix, weights, modulus): + n = len(matrix) // 2 + require(len(matrix) == 2*n and len(weights) == n, "weighted residual dimensions") + b = list(weights) + [1] * n + require(all(gcd(x, modulus) == 1 for x in b), "diagonal conjugator units") + return [[b[i] * x * pow(b[j], -1, modulus) % modulus + for j, x in enumerate(row)] for i, row in enumerate(matrix)] + + +def reversed_child(head, tail, weights, modulus): + require(len(head) == len(tail) == len(weights), "child extent") + require(all(gcd(a, modulus) == 1 for a in weights), "child weight units") + f = len(weights) + return ([weights[t] * tail[f-1-t] % modulus for t in range(f)], + [pow(weights[f-1-t], -1, modulus) * head[f-1-t] % modulus for t in range(f)]) diff --git a/research/selected-bit-mixed-descriptor/test_verify.py b/research/selected-bit-mixed-descriptor/test_verify.py new file mode 100644 index 0000000000..ffa3449bc1 --- /dev/null +++ b/research/selected-bit-mixed-descriptor/test_verify.py @@ -0,0 +1,71 @@ +"""Behavioral corruption/relocation checks, confined to temporary copies. + +Prepared for commdata2338 with substantial OpenAI Codex assistance. +Apache-2.0. +""" + +from pathlib import Path +import json +import shutil +import subprocess +import sys +import tempfile + +HERE = Path(__file__).resolve().parent +ROOT = HERE.parents[1] + + +def require(condition, message): + if not condition: + raise ValueError(message) + + +def main(): + # Honour the caller's ordinary temporary-directory policy. + with tempfile.TemporaryDirectory(prefix="mixed-descriptor-") as temp: + root = Path(temp)/"relocated" + package = root/"research"/HERE.name + shutil.copytree(HERE,package,ignore=shutil.ignore_patterns("__pycache__")) + pins = json.loads((package/"SOURCES.json").read_text()) + for pin in pins["files"]: + if pin["usage"] == "input": + target = root/pin["path"] + target.parent.mkdir(parents=True,exist_ok=True) + shutil.copyfile(ROOT/pin["path"],target) + + def run(ok, expected, *args): + result = subprocess.run([sys.executable,"-I","-O","-B",str(package/"verify.py"),*args], + cwd=root,capture_output=True,text=True,timeout=180) + output = result.stdout+result.stderr + require((result.returncode == 0) == ok and expected in output, + "unexpected subprocess behavior: " + output[-3000:]) + + # Relocation must succeed with no predecessor checkout or executable. + run(True,"PASS_SELECTED_BIT_MIXED_DESCRIPTOR") + receipt = package/"RESULT.json" + original = receipt.read_bytes() + value = json.loads(original) + value["fallback_child_allowance"] = float(value["fallback_child_allowance"]) + receipt.write_text(json.dumps(value)) + run(False,"committed result mismatch") + receipt.write_bytes(original) + receipt.unlink() + run(False,"FileNotFoundError") + receipt.write_bytes(original) + receipt.write_bytes(b'{"scope":"duplicate",'+original[1:]) + run(False,"duplicate receipt key") + receipt.write_bytes(original) + source = root/"research/e8-bit-p111-tuned/tune/EXPECTED.json" + source_bytes = source.read_bytes() + source.write_bytes(source_bytes+b" ") + run(False,"source pin:") + source.write_bytes(source_bytes) + output = root/"already-exists.json" + output.write_text("retain this exact file\n") + run(False,"FileExistsError","--write-result",str(output)) + require(output.read_text()=="retain this exact file\n","candidate output overwritten") + print("PASS_MIXED_DESCRIPTOR_BEHAVIOR groups=6 optimized=True") + + +if __name__ == "__main__": + main() diff --git a/research/selected-bit-mixed-descriptor/verify.py b/research/selected-bit-mixed-descriptor/verify.py new file mode 100644 index 0000000000..f76ed6e73a --- /dev/null +++ b/research/selected-bit-mixed-descriptor/verify.py @@ -0,0 +1,398 @@ +"""Source-bound exact checks for one mixed good/fallback descriptor interface. + +Prepared for commdata2338 with substantial OpenAI Codex assistance. +Apache-2.0. No predecessor executable is imported. +""" + +import argparse +from dataclasses import replace +import gzip +from hashlib import sha1, sha256 +from itertools import product +import json +from math import gcd +from pathlib import Path +import sys + +HERE = Path(__file__).resolve().parent +ROOT = HERE.parents[1] +sys.path.insert(0, str(HERE)) +from descriptor import (FallbackPair, Good, Root, elimination, good_pivots, + identity, inverse, multiply, projector, require, + residual, reversed_child, weighted_residual) + + +def triangular_oracle(matrix, modulus): + """Separate forward normalized Gaussian elimination, no descriptor calls. + + Record the pivot BEFORE normalization. Clearing only BELOW the pivot + gives the same diagonal as unnormalized all-row clearing, because later + pivot rows have zeros in earlier columns. + """ + a = [row[:] for row in matrix] + n = len(a) + diagonal, itinerary = [], [] + for k in range(n): + candidates = [i for i in range(k, n) if gcd(a[i][k], modulus) == 1] + require(bool(candidates), "oracle unit pivot") + p = min(candidates) + itinerary.append(p) + a[k], a[p] = a[p], a[k] + d = a[k][k] % modulus + diagonal.append(d) + a[k] = [x * pow(d, -1, modulus) % modulus for x in a[k]] + for i in range(k+1, n): + c = a[i][k] + for j in range(k, n): + a[i][j] = (a[i][j] - c * a[k][j]) % modulus + return tuple(diagonal), tuple(itinerary) + + +def reconstructed_matrix(plan): + """Reconstruct full coefficients from D and reversed inverse row actions.""" + n, modulus = len(plan.diagonal), plan.modulus + out = [[int(i == j) * plan.diagonal[i] for j in range(n)] for i in range(n)] + for kind, i, k, c in reversed(plan.operations): + if kind == "swap": + out[i], out[k] = out[k], out[i] + else: + out[i] = [(x - c*y) % modulus for x, y in zip(out[i], out[k])] + return out + + +def check_pins(): + data = {} + for pin in json.loads((HERE / "SOURCES.json").read_text())["files"]: + if pin["usage"] == "input": + raw = (ROOT / pin["path"]).read_bytes() + require(sha256(raw).hexdigest() == pin["sha256"], "source pin: " + pin["path"]) + data[pin["path"]] = raw + base = "research/community-hyperplane599-audit/pr589/bit589/v9/hosted/" + template_raw = gzip.decompress(data[base + "GLOBAL-TEMPLATE.json.gz"]) + template = json.loads(template_raw) + expected_path = "research/e8-bit-p111-tuned/tune/EXPECTED.json" + expected = json.loads(data[expected_path]) + assembly = json.loads(data["research/community-frontier1094-audit/ASSEMBLY.json"]) + global_receipt = json.loads(data[base + "GLOBAL.json"]) + raw = data[expected_path] + blob = sha1(b"blob " + str(len(raw)).encode() + b"\0" + raw).hexdigest() + require(assembly["bit_supplier"]["pr"] == 589 and + assembly["bit_supplier"]["git_blob_sha1"] == blob, "selected bit binding") + require(sha256(template_raw).hexdigest() == global_receipt["template_sha256"], + "decoded template binding") + require(global_receipt["word_sha256"] == expected["word"]["file_sha256"], + "selected word binding") + require((template["m"], template["h"], template["stages"], template["replicas"]) + == (64, 8, 9, 32), "selected dimensions") + children = [x for x in template["schedule"] if x["kind"] == "idle" and x["idx"] == 6] + require(len(children) == 1 and children[0] == { + "idx": 6, "kind": "idle", "pair": 0, "rank": 42, + "source_level": 2, "target_level": 8}, "selected idle label") + used = [slot for slot in template["hosting"]["slots"] if slot[0] == 6 and slot[1] == 213] + require(not used and template["hosting"]["remainders"]["6"] == {"2": 213, "42": 9}, + "selected unhosted copy") + require(expected["nine"]["profile"]["42"] == 288, "selected rank42 count") + return {"idle_child": 6, "copy": 213, "rank": 42, "axes": list(range(22, 64)), + "cover_bridge": "GL64 implementation domain remains conditional"} + + +def selected_chart(blocks): + """h S, where S sends the selected last42 axes to the first42 axes.""" + h = identity(64) + for i, j, a, b, c, d in blocks: + h[i][i], h[i][j], h[j][i], h[j][j] = a, b, c, d + order = list(range(22, 64)) + list(range(22)) + g = [[0] * 64 for _ in range(64)] + for i in range(64): + for j, original_column in enumerate(order): + g[i][original_column] = h[i][j] + return g + + +def fixture(q=5, width=2, root_columns=5, branching=True): + modulus = q**width + good_chart = selected_chart([ + (0, 43, 1, 1, q, 1+q), + (1, 42, 1, 1, 2*q, 1+2*q), + (25, 44, 1, 1, 3*q, 1+3*q)]) + p = projector(good_chart, range(22, 64), modulus) + pivots = good_pivots(p, 42, modulus) + require([pivots[k] for k in (0, 1, 25)] == [(1+t*q) % modulus for t in (1, 2, 3)], + "good full-chart pivots") + good = Good(Root(64*root_columns, modulus), 64, root_columns, pivots) + blocks = [(0, 43, q, q-1, 1, 1)] + if branching: + blocks.append((1, 42, 1, 0, 1, 1)) + bad_chart = selected_chart(blocks) + bad_p = projector(bad_chart, range(22, 64), modulus) + require(bad_p[0][0] == q % modulus and gcd(bad_p[0][0], modulus) != 1, + "nongeneric rank42 projector") + require(multiply(bad_p, bad_p, modulus) == bad_p, "split projector") + matrix = residual(bad_p, modulus) + require(multiply(matrix, matrix, modulus) == identity(128), "full residual involution") + plan = elimination(matrix, modulus) + require(reconstructed_matrix(plan) == matrix, "full fallback reconstruction") + oracle_d, oracle_pivots = triangular_oracle(matrix, modulus) + require((plan.diagonal, plan.pivots) == (oracle_d, oracle_pivots), "unweighted oracle") + return good, matrix, plan + + +def expected_weights_from_oracle(good, matrix, field, reverse): + f = good.size // 64 + values = [] + for t in range(f): + j = f-1-t if reverse else t + a = [good.value(k*f+j) for k in range(64)] + weighted = weighted_residual(matrix, a, good.modulus) + d, _ = triangular_oracle(weighted, good.modulus) + values.append(d[field]) + return values + + +def selected_checks(): + records = [] + columns = diagonal_entries = 0 + for q, width, f0 in [(3, 2, 2), (5, 2, 5), (5, 3, 8), (7, 1, 4)]: + good, matrix, plan = fixture(q, width, f0) + f, modulus = good.size // 64, good.modulus + determinant = (-1) ** sum(op[0] == "swap" for op in plan.operations) + for d in plan.diagonal: + determinant = determinant*d % modulus + require(determinant == 1, "selected even-rank residual determinant") + # Every paired-diagonal descriptor, both physical orientations. + for j in range(f): + weights = [good.value(k*f+j) for k in range(64)] + weighted = weighted_residual(matrix, weights, modulus) + direct_d, direct_pivots = triangular_oracle(weighted, modulus) + require(direct_pivots == plan.pivots, "weight-independent pivot itinerary") + # Check the weighted entire ADDRESS matrix by a second reconstruction. + weighted_plan = elimination(weighted, modulus) + require(reconstructed_matrix(weighted_plan) == weighted, "weighted whole-operator reconstruction") + for i in range(127): + for reverse in (False, True): + child = FallbackPair(good, 64, f, plan, i, reverse) + t = f-1-j if reverse else j + require(child.value(t) == direct_d[i], "mixed descriptor vs independent elimination") + diagonal_entries += 1 + columns += 1 + records.append({"q": q, "width": width, "good_columns": f0, + "good_child_atoms": good.size, "bad_columns": f, + "peeled_spectators": good.size-64*f, + "tail126_weights": [FallbackPair(good,64,f,plan,126,True).value(t) + for t in range(f)]}) + good, matrix, plan = fixture() + head = FallbackPair(good,64,3,plan,1,False) + tail = FallbackPair(good,64,3,plan,126,True) + require([head.value(t) for t in range(3)] == [14,14,19], "two-index quotient witness") + require([tail.value(t) for t in range(3)] == [11,6,6], "tail orientation witness") + return {"fixtures": records, "weighted_columns": columns, + "descriptor_entries": diagonal_entries, + "quotient_witness": {"field":1, "original_row":plan.permutation[1], + "weights":[14,14,19]}, + "orientation_witness": {"field":126,"original_row":plan.permutation[126], + "weights":[11,6,6]}} + + +def small_covariance_checks(): + """Exhaust all determinant +/-1 matrices and unit diagonal weights at 3,9.""" + matrices, weighted_cases, nontrivial_swaps = 0, 0, 0 + for modulus in (3, 9): + units = [x for x in range(modulus) if gcd(x, modulus) == 1] + for a,b,c,d in product(range(modulus), repeat=4): + if (a*d-b*c) % modulus not in (1, modulus-1): + continue + matrix = [[a,b],[c,d]] + plan = elimination(matrix, modulus) + require(reconstructed_matrix(plan) == matrix, "small full reconstruction") + matrices += 1 + nontrivial_swaps += plan.permutation != (0,1) + for weights in product(units, repeat=2): + weighted = [[weights[i]*matrix[i][j]*pow(weights[j],-1,modulus) % modulus + for j in range(2)] for i in range(2)] + diag, pivots = triangular_oracle(weighted, modulus) + predicted = tuple(plan.diagonal[i] * weights[plan.permutation[i]] * + pow(weights[i],-1,modulus) % modulus for i in range(2)) + require(diag == predicted and pivots == plan.pivots, "exhaustive covariance") + weighted_cases += 1 + require((matrices, weighted_cases) == (1344, 46848), "small exhaustive coverage") + return {"matrices": matrices, "weighted_cases": weighted_cases, + "matrices_with_row_swap": nontrivial_swaps} + + +def ordinary_via_children(head, tail, weights, modulus): + """R_tail F_A R_tail; each reversal is one smaller unweighted child.""" + def reverse_with_child(values): + h = len(values)//2 + if not h: + return list(values), 0 + left, right = reversed_child(values[:h], values[-h:], [1]*h, modulus) + return left + list(values[h:len(values)-h]) + right, 1 + tail, before = reverse_with_child(tail) + head, tail = reversed_child(head, tail, weights, modulus) + tail, after = reverse_with_child(tail) + return head, tail, 1+before+after + + +def child_checks(): + good, _, plan = fixture() + child = FallbackPair(good,64,3,plan,126,True) + weights = [child.value(t) for t in range(3)] + modulus, f = 25, 3 + count = 0 + # Each coordinate pair exhausts its complete two-field fiber. Other + # coordinates are arbitrary nonzero values; spectator labels stay present. + for t in range(f): + images = set() + for x, y, stock in product(range(modulus), range(modulus), range(3)): + h, d = [2,3,4], [7,8,9] + h[t], d[t] = x, y + before = (stock, tuple(h), ("middle",17), tuple(d), ("suffix",23)) + h1,d1,n1 = ordinary_via_children(h,d,[1]*f,modulus) + h2,d2,n2 = ordinary_via_children(h1,d1,weights,modulus) + after = (stock,tuple(h2),before[2],tuple(d2),before[4]) + expected_h = tuple(weights[k]*h[k]%modulus for k in range(f)) + expected_d = tuple(pow(weights[k],-1,modulus)*d[k]%modulus for k in range(f)) + require(after == (stock,expected_h,before[2],expected_d,before[4]), + "weighted diagonal pair with complete spectators") + require(n1+n2 == 6, "paid same-orientation child count") + images.add((after[0], after[1][t], after[3][t])) + count += 1 + require(len(images) == 3*modulus**2, "complete pair fibers preserved") + head_child = FallbackPair(good,64,3,plan,1,False) + opposite_weights = [head_child.value(t) for t in range(3)] + basis_count = 0 + for k in range(2*f): + basis = [int(i==k) for i in range(2*f)] + h,d = reversed_child(basis[:f],basis[f:],[1]*f,modulus) + h,d = reversed_child(h,d,opposite_weights,modulus) + require(h == [opposite_weights[t]*basis[t]%modulus for t in range(f)] and + d == [pow(opposite_weights[f-1-t],-1,modulus)*basis[f+t]%modulus + for t in range(f)], "opposite-orientation diagonal pair") + basis_count += 1 + return {"complete_pair_and_stock_cases":count,"pair_children":6, + "pair_child_widths":[1,3,1,1,3,1],"weights":weights, + "opposite_orientation_basis_columns":basis_count,"opposite_pair_children":2} + + +def mixed_chain_check(): + """A fallback child large enough to become the next selected good parent.""" + older, matrix, plan = fixture(root_columns=100) + bad = FallbackPair(older,64,65,plan,126,True) + reference = expected_weights_from_oracle(older,matrix,126,True) + require([bad.value(t) for t in range(65)] == reference, "mixed ancestor vector") + # The next good normal form is independently prepared from its full chart. + current_chart = selected_chart([(0,43,1,1,5,6), (1,42,1,1,10,11)]) + pivots = good_pivots(projector(current_chart,range(22,64),25),42,25) + current = Good(bad,64,1,pivots) + actual = [current.value(t) for t in range(current.size)] + require(actual == [reference[t]*pivots[t]%25 for t in range(42)], + "good-after-fallback generated weights") + require((actual[0],actual[1],actual[29],actual[30],actual[41]) == (16,21,11,6,6), + "mixed depth-three witness") + return {"root_atoms":6400,"first_good_atoms":4200,"fallback_columns":65, + "first_peeled_atoms":40,"fallback_child_atoms":65, + "current_good_columns":1,"current_peeled_atoms":1, + "current_good_child_atoms":42, + "current_weights":actual,"oracle_weight_entries":65} + + +def controls(): + names = [] + + def rejects(name, action): + try: + action() + except ValueError: + names.append(name) + else: + raise ValueError("control did not reject: " + name) + + good, matrix, plan = fixture() + child = FallbackPair(good,64,3,plan,126,True) + expected = expected_weights_from_oracle(good,matrix,126,True) + def compare(candidate, wanted=expected): + require([candidate.value(t) for t in range(candidate.size)] == wanted, "descriptor mismatch") + + rejects("omit_physical_orientation", lambda: compare(replace(child,reverse=False))) + pi = list(plan.permutation) + a,b = 126,pi.index(126) + pi[a],pi[b] = pi[b],pi[a] + rejects("wrong_original_row", lambda: compare(replace(child,plan=replace(plan,permutation=tuple(pi))))) + low_good = replace(good,pivots=tuple(x%5 for x in good.pivots)) + rejects("low_only_good_ancestor", lambda: compare(replace(child,parent=low_good))) + head = FallbackPair(good,64,3,plan,1,False) + wanted = expected_weights_from_oracle(good,matrix,1,False) + omitted_denominator = [plan.diagonal[1] * good.value(42*3+j)%25 for j in range(3)] + rejects("omit_denominator", lambda: require(omitted_denominator==wanted,"denominator needed")) + wrong_flat = [plan.diagonal[126]*good.value(t)%25 for t in range(3)] + rejects("reuse_good_only_flat_index", lambda: require(wrong_flat==expected,"fallback reindexes")) + wrong_sign = replace(plan,diagonal=tuple((-x if i==1 else x)%25 for i,x in enumerate(plan.diagonal))) + rejects("lost_diagonal_sign", lambda: compare(replace(head,plan=wrong_sign),wanted)) + low_matrix = [[x%5 for x in row] for row in matrix] + low_plan = elimination(low_matrix,25) + low_child = FallbackPair(good,64,3,low_plan,0,False) + rejects("low_only_bad_ancestor", lambda: compare(low_child,expected_weights_from_oracle(good,matrix,0,False))) + rejects("wrong_coarse_width", lambda: replace(child,columns=4)) + rejects("wrong_plan_modulus", lambda: replace(child,plan=replace(plan,modulus=9))) + rejects("nonunit_root_weight", lambda: replace(good.parent,unit=5).value(0)) + rejects("nonunit_diagonal", lambda: replace(child,plan=replace(plan,diagonal=(5,)+plan.diagonal[1:]))) + rejects("out_of_range_child_index", lambda: child.value(3)) + rejects("invalid_row_permutation", lambda: replace(child,plan=replace(plan,permutation=(0,)*128))) + weights = [child.value(t) for t in range(3)] + h,d = [1,2,3],[4,5,6] + # Swapping the chronological order of J_1 then J_a inverts the scaling. + h1,d1,_ = ordinary_via_children(h,d,weights,25) + h2,d2,_ = ordinary_via_children(h1,d1,[1]*3,25) + rejects("reversed_diagonal_pair_order", lambda: require( + h2==[weights[k]*h[k]%25 for k in range(3)],"pair order")) + rejects("unpaid_tail_reversal", lambda: require( + reversed_child(h,d,weights,25) == + ([weights[k]*d[k]%25 for k in range(3)], + [pow(weights[k],-1,25)*h[k]%25 for k in range(3)]), "ordinary vs reversed swap")) + return names + + +def build_result(): + return {"selected":check_pins(), "selected_checks":selected_checks(), + "exhaustive_covariance":small_covariance_checks(), "child":child_checks(), + "good_fallback_good":mixed_chain_check(), + "rejected_controls":controls(), + "fallback_child_allowance":6*128*127+3*128+6*127, + "charged_fallback_allowance":32*64**2, + "scope":"conditional mixed descriptor interface; no supplier or new kappa"} + + +def read_receipt(path): + def unique(pairs): + out = {} + for key, value in pairs: + require(key not in out, "duplicate receipt key: " + key) + out[key] = value + return out + def reject_constant(value): + raise ValueError("nonfinite receipt number: " + value) + return json.loads(path.read_text(),object_pairs_hook=unique,parse_constant=reject_constant) + + +def exact_json(value): + return json.dumps(value,sort_keys=True,separators=(",",":"),allow_nan=False) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--write-result", type=Path, help="write a new candidate receipt, exclusively") + args = parser.parse_args() + result = build_result() + if args.write_result: + with args.write_result.open("x") as handle: + json.dump(result,handle,indent=2) + handle.write("\n") + print("WROTE_CANDIDATE_RESULT (not a committed-receipt comparison)") + else: + require(exact_json(read_receipt(HERE/"RESULT.json")) == exact_json(result), + "committed result mismatch") + print("PASS_SELECTED_BIT_MIXED_DESCRIPTOR covariance=46848 mixed_columns=11 controls=15 no_new_kappa") + + +if __name__ == "__main__": + main()