Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/universal/source-aware/v2_reference.py # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / universal / source-aware / v2_reference.py` 478 lines ยท 22324 bytes Raw Blame History 1 #!/usr/bin/env python3 2 """Independent source-aware v2 reference decoder (#623), written from spec/universal/source-aware-v2.md 3 (section 5 decode order, normative for v1 and v2) and the v1 typed registry it keeps; no codec source is 4 read. `--sa` runs a differential against a native codec (build/L832/sa): every request goes to the native 5 binary as one process (mode byte + envelope on stdin) and to this decoder; exit status and stdout must 6 agree exactly. Modes compared: 1 encode, 2 decode, 3 identity, 4 provenance, 5 replay, plus the mode and 7 size checks of `perform`. Mode 6 legality is not modelled (its per-operation reply is not specified 8 byte for byte in either spec) and is reported NOT_RUN. 9 10 Corpus: every F1 import (C importer), every F2 import of each available importer, R(U, M) for the six 11 family members of each, the v2 corner cases (version/profile pairing, wrong dialect, obligation 4 on 12 profiles 7..9, division, ops 6/7/8, every version byte value, every truncation, header fields) and seeded 13 random byte mutations of valid envelopes (raw, with the sidecar hashes resealed, and semantic-only). 14 Exit 0 when every comparison agrees, 1 on any disagreement, 3 when the codec or a pin is absent. 15 """ 16 from pathlib import Path 17 import argparse, hashlib, json, os, random, struct, subprocess, sys 18 19 sys.dont_write_bytecode = True 20 ROOT = Path(__file__).resolve().parents[3] 21 SPEC = ('spec/universal/source-aware-v2.md', '`2f894c7b923db7e4e89d7ed7339dae97cdb2969742e7b1ed2026637007edb5c7`') 22 SEMANTICS = 'spec/universal/semantics.md' 23 U = 65535 24 MAX_INPUT, MAX_SEM, MAX_SIDE, MAX_SRC = 8192, 1324, 4682, 4096 25 # profile -> (language, allowed (runtime, ABI) pairs, allowed types) 26 PROFILES = {1: (1, {(0, 1)}, {1}), 2: (2, {(0, 1)}, {1}), 3: (3, {(1, 1), (2, 1)}, {2}), 27 4: (3, {(0, 1)}, {1}), 5: (4, {(0, 1)}, {1}), 6: (3, {(1, 2), (2, 2)}, {1, 2, 3, 4}), 28 7: (5, {(0, 1)}, {1}), 8: (6, {(0, 1)}, {1}), 9: (7, {(0, 1)}, {1})} 29 # family members (translation-compose-v2 section 1): OMSA version, language, profile, dialect, arith obligations 30 MEMBERS = {'c': (1, 1, 1, 1, 1), 'rust': (1, 2, 2, 2, 1), 'python': (1, 3, 4, 3, 5), 31 'cpp': (2, 5, 7, 5, 1), 'zig': (2, 6, 8, 6, 1), 'go': (2, 7, 9, 7, 1)} 32 33 34 def semantics_digest(root=ROOT): 35 return hashlib.sha256((root / SEMANTICS).read_bytes()).digest() 36 37 38 class Ref: 39 def __init__(self, digest): 40 self.digest = digest 41 42 def decode(self, b): 43 """Returns (status, fields). Section 5 order; the first failure wins.""" 44 if len(b) > MAX_INPUT: 45 return 67, None 46 if len(b) < 13 or b[:4] != b'OMSB': 47 return 65, None 48 if b[4] != 1: 49 return 66, None 50 ns, np_ = struct.unpack_from(' MAX_SEM or np_ > MAX_SIDE: 52 return 67, None 53 if len(b) != 13 + ns + np_ or ns < 44: 54 return 65, None 55 s = b[13:13 + ns] 56 if s[:4] != b'OMSA': 57 return 65, None 58 if s[4] not in (1, 2) or s[7] != 1: 59 return 66, None 60 count = struct.unpack_from(' 64: 62 return 67, None 63 if ns != 44 + 20 * count: 64 return 65, None 65 if s[12:44] != self.digest: 66 return 78, None 67 version, lang, prof, rt, abi = s[4], s[5], s[6], s[8], s[9] 68 if prof == 0 or prof > 9 or lang != PROFILES[prof][0] or (version == 2) != (prof >= 7) \ 69 or (rt, abi) not in PROFILES[prof][1]: 70 return 68, None 71 types, slots, ops, aliases = [], set(), [], [] 72 for i in range(count): 73 d, v, op, t, attr, eff, alias, obl, A, B, imm = struct.unpack_from('<8BHHQ', s, 44 + 20 * i) 74 if d != lang: 75 return 68, None 76 if v != 1: 77 return 66, None 78 if t == 0 or t > 4: 79 return 70, None 80 if t not in PROFILES[prof][2]: 81 return 68, None 82 if op == 0 or op > 9: 83 return 69, None 84 st = self.row(prof, i, op, t, attr, eff, alias, obl, A, B, imm, types, slots, aliases) 85 if st: 86 return st, None 87 types.append(t) 88 aliases.append(alias) 89 ops.append((op, A, B, imm)) 90 side = b[13 + ns:] 91 if np_: 92 if np_ < 74 or side[:4] != b'OMSP': 93 return 65, None 94 if side[4] != 1: 95 return 66, None 96 if side[5] > 1: 97 return 76, None 98 n = struct.unpack_from(' MAX_SRC: 100 return 67, None 101 if np_ != 74 + 8 * count + n: 102 return 65, None 103 src = side[74 + 8 * count:] 104 if side[10:42] != hashlib.sha256(s).digest() or side[42:74] != hashlib.sha256(src).digest(): 105 return 74, None 106 if side[5] == 1: 107 try: 108 src.decode('utf-8', errors='strict') # strict: no overlong, surrogate or > U+10FFFF 109 except UnicodeDecodeError: 110 return 76, None 111 for k in range(count): 112 lo, hi = struct.unpack_from(' hi or hi > n: 114 return 75, None 115 if side[5] == 1 and any(x != n and 0x80 <= src[x] <= 0xBF for x in (lo, hi)): 116 return 75, None 117 else: 118 src = None 119 return 0, {'sem': s, 'source': src, 'ops': ops, 'profile': prof, 'version': version} 120 121 @staticmethod 122 def row(prof, i, op, t, attr, eff, alias, obl, A, B, imm, types, slots, aliases): 123 """One operation row, transcribed check by check from source-aware-v2.md section 5 step 6 124 (sha256 `2f894c7b`...); within a row the first failing check decides.""" 125 def earlier(x, want): # an operand index < i whose record has type `want` (65535 is never one) 126 return x < i and types[x] == want 127 128 if op == 1: 129 if t > 2 or attr != t or A != U or B != U: 130 return 70 131 if obl != 0: 132 return 72 133 if eff != 0 or alias != 0: 134 return 71 135 return 0 136 if op in (2, 3, 4, 5): 137 if t > 2 or attr != t or not earlier(A, t) or not earlier(B, t): 138 return 70 139 if imm != 0: 140 return 65 141 if t == 1 and (eff != 0 or alias != 0): 142 return 71 143 if t == 2 and (eff != 20 or alias != 255): 144 return 71 145 want = (1 | (4 if prof in (4, 6) else 0)) if t == 1 else 8 146 if op == 5: 147 want += 2 148 return 72 if obl != want else 0 149 if op == 6: 150 if prof != 6: 151 return 68 152 if t != 3 or attr != 3 or not earlier(A, 3) or B != U: 153 return 70 154 if imm != 0: 155 return 65 156 if eff != 63 or alias != 255: 157 return 71 158 return 72 if obl != 8 else 0 159 if op in (7, 8): 160 if prof != 6: 161 return 68 162 if t != 1 or attr != 4 or not earlier(A, 4) or not earlier(B, 1): 163 return 70 164 if not (1 <= alias <= 8 or alias == 255) or alias != aliases[A]: 165 return 71 166 if op == 8 and not earlier(imm, 1): 167 return 70 168 if op == 7 and imm != 0: 169 return 65 170 if eff != (17 if op == 7 else 19): 171 return 71 172 return 72 if obl != 24 else 0 173 # op 9 (input) 174 if attr != 0 or A != U or B != U: 175 return 70 176 if imm > 5: 177 return 67 178 if imm in slots: 179 return 70 180 slots.add(imm) 181 if obl != 1: 182 return 72 183 if eff != 0: 184 return 71 185 if (t == 4 and not (1 <= alias <= 8 or alias == 255)) or (t != 4 and alias != 0): 186 return 71 187 return 0 188 189 def perform(self, mode, b): 190 """(status, stdout) of `sa` for mode byte + envelope.""" 191 if len(b) > MAX_INPUT: 192 return 67, b'' 193 if mode == 0 or mode > 6: 194 return 64, b'' 195 st, f = self.decode(b) 196 if st: 197 return st, b'' 198 if mode in (1, 2): 199 return 0, b 200 if mode == 3: 201 return 0, hashlib.sha256(b'omega.source.semantic.v1' + f['sem']).digest() 202 if mode == 4: 203 return 0, hashlib.sha256(b'omega.source.provenance.v1' + b).digest() 204 if mode == 5: 205 return (73, b'') if f['source'] is None else (0, f['source']) 206 raise ValueError('legality not modelled') 207 208 209 # ---------------------------------------------------------------- corpus construction 210 211 def envelope(sem, side=b''): 212 return b'OMSB\x01' + struct.pack('= 74 and len(sem) >= 12: 238 count = struct.unpack_from(' 17 and b[:5] == b'OMSB\x01' and ref.decode(b)[0] == 0 and b[17] == 2: 456 v1rows['total'] += 1 457 got = run([a.sa_v1], b'\x02' + b) 458 if got == (66, b''): 459 v1rows['agree'] += 1 460 else: 461 v1rows['disagree'].append({'case': label, 'status': got[0], 'stdout_bytes': len(got[1])}) 462 ev.update({'cases': len(cases), 'comparisons': agree + len(dis), 'agree': agree, 'disagree': len(dis), 463 'agreements_by_status': {str(k): v for k, v in sorted(by_status.items())}, 464 'v1_codec_refuses_v2_66': v1rows if a.sa_v1 else 'NOT_RUN', 'disagreements': dis[:400]}) 465 v1ok = not a.sa_v1 or not v1rows['disagree'] 466 ev['result'] = 'FAIL' if dis or not v1ok else 'PASS' 467 (out / 'evidence.json').write_text(json.dumps(ev, indent=1) + '\n') 468 print(json.dumps({k: ev[k] for k in ('result', 'cases', 'comparisons', 'agree', 'disagree', 469 'agreements_by_status')}, indent=1)) 470 if a.sa_v1: 471 print('v1 codec refuses v2 with 66: %d/%d' % (v1rows['agree'], v1rows['total'])) 472 for d in dis[:20]: 473 print('DISAGREE', d['case'], 'mode', d['mode'], 'ref', d['reference'][0], 'native', d['native'][0]) 474 return 1 if ev['result'] == 'FAIL' else 0 475 476 477 if __name__ == '__main__': 478 raise SystemExit(main())