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/patches/382c33a401b11ff2 # 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 ## GitHub aien-dev/omega-mv#762 (code only) closed derived `382c33a401b11ff2` #762 Imported from GitHub aien-dev/omega-mv#762 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `62d1d0d668f897386a0e311374f8dbcc5c32dde8` · Head `d0cd04d4e3e6b79c2f4049400dcba2d0fd36d161` drakestapleton reviewed (COMMENTED) on 2026-10-30 11:34 UTC #726 receipt port. Same as #765: mark it draft and attach the parity output. — Grokbot ### Fetch it A git bundle of the pull request's head, 36585 bytes. ``` curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/382c33a401b11ff2.bundle -o 382c33a401b11ff2.bundle git fetch 382c33a401b11ff2.bundle refs/pull/762/head ``` Raw bundle · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-mv 762 d0cd04d4e3e6b79c2f4049400dcba2d0fd36d161 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":762,"url":"https://github.com/aien-dev/omega-mv/pull/762","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:38:30Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index f3e51fe46..5c89131fc 100644 --- a/.crumb +++ b/.crumb @@ -72,7 +72,7 @@ }, { "digest": "f765ad87fdddd9b47f690b07298c951a0f7e7675dc5de4ffe44e33a0c14b1986", - "last_commit": "a8a6a3d3c", + "last_commit": "d94a503f7", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, @@ -84,26 +84,26 @@ }, { "digest": "5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", - "last_commit": "a8a6a3d3c", + "last_commit": "dcccc0211", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { "digest": "308db0e6cf45c0a48f01f5f5f228450f191389de68006915b9ecf9c65b9a2e29", - "last_commit": "a8a6a3d3c", + "last_commit": "a50c3bd41", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], "children_root": "sha256:07bf1706dcffbad66c0353871ff67a6749055da03cee8d8ebdf9a4c91e36bddc", - "compiled_at": "2026-10-30T12:31:50Z", + "compiled_at": "2026-10-30T16:29:06Z", "compiler": "crumb-compile/1", - "digest": "sha256:904fca8c0e0cf3e3a5e5adb73393e01c7018b920bb542d289904240f70e400d6", + "digest": "sha256:cd86ec1f581b8e8b6c843dad8b92ea02e4d2586b7af4514ba4259f11cc0fca6f", "evidence_files": 12622, "evidence_root": "sha256:b1a08e2ce56e6feada0d8a31f06fbd7adee57adc1c1ad8dec220293ee5d439f9", - "files": 17849, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:75458da6553a0ee5ea2af95f46afb658e8a3f6e53cca6e3b4b9874035f78c2ed" + "files": 17855, + "generated_at_commit": "17efbc0035723dc8f6bdacca045df3db66766acd", + "source_tree": "sha256:ac618103ded5b8ddcb25aa7cf3d41d457c090f0c62d45f71ee28427b38ac7eda" }, "owner_lane": "", "provenance": { diff --git a/om/receipt/README.md b/om/receipt/README.md new file mode 100644 index 000000000..bc0e01de1 --- /dev/null +++ b/om/receipt/README.md @@ -0,0 +1,161 @@ +# receipt.om: the EvidenceReceiptV1 reader in seeded Omega + +`receipt.om` ports `src/omega_receipt.c` and `src/omega_receipt.h` to seeded Omega. These files are the +C reader of the aien-proof EvidenceReceiptV1 wire contract (VC1 stage 4). The port does what the C +does: + +- parses the receipt JSON with serde's shape rules (no unknown fields, the same required and + defaulted fields); +- applies every `canonical_bytes` validation in the C order; +- builds the canonical byte encoding; +- computes the id as BLAKE3 over those bytes; +- `verify_bytes` requires the `id` field to equal that BLAKE3 value, in either hex case. + +It also covers `omega_tier_rank` and `omega_tier_satisfies`. Part of #726, +aien-dev/aien-architecture-mv#300 and #316. No C file is changed or removed by this port. + +## Layout + +The library is everything above the line `note ==== parity harness`. It uses no globals and no +`text`, and every array length is a literal, because function-mode units refuse `const` (error 9). +`parity/unit.py` can cut any entry point into a function-mode unit for the omega-bridge native call +ABI v1: + +| entry | signature (ABI v1) | C counterpart | +|---|---|---| +| `rcparse js n wk` | `js view uchar 4194304`, `wk edit ulong 65536` → long | `omega_receipt_parse` | +| `rccanon js n wk out pos` | `out edit uchar 1049632` → ulong (total length) | `omega_receipt_canonical` + `omega_receipt_compute_id` | +| `rcverify js n wk out` | → long | `omega_receipt_verify_bytes` | +| `rctier s n wk` | → long | `omega_tier_rank` | +| `rcsat need have` | → long | `omega_tier_satisfies` | + +`rcparse` and `rcverify` return 0 or -1, as the C does. A refusal is stored as three numbers: + +- `wk[0]`: a reason code, 1..69, one for each distinct C message; +- `wk[1]`: a field-name index, for messages that name a field; +- `wk[2]`: the byte offset, for JSON syntax messages. + +The program-mode harness renders these numbers as the exact C message text. `rccanon` streams the +canonical bytes: + +- bytes `[pos, pos + 1 MiB)` go to `out[0..]`; +- the 32-byte id goes to `out[1049600..]`; +- the return value is the total length. + +Canonical bytes over 1 MiB need one call per window, and each call re-encodes the whole receipt. + +There is no DOM. Pass 1 checks the JSON syntax with the C reader's exact rules, errors and byte +offsets. It is iterative, because function mode refuses call cycles. Pass 2 reads fields by walking +the validated text, and decodes strings on the fly for comparisons, sorting and output. The whole +`wk` layout is documented at the top of `receipt.om`. + +BLAKE3 is copied from `om/blake3/b3unit.om`, because Omegese has no `import`. The task named +`blake3.om`, but that file uses globals, so it cannot become a function-mode unit; `b3unit.om` is the +array-parameter form from the same merged work. Three locations changed; the compression code is +unchanged: + +- the chaining-value stack lives in `wk` at 512 (depth 952, chunk count 953); +- the chunk is read from `out` at 1048576; +- the root is written to `out` at 1049600. + +## Build and run + +``` +OMC=/path/to/seed/out/omc # pinned: sha256 62775d2a391cfef7cde64e387655caa9809a267093112910ec76bfa568f3712b +$OMC < om/receipt/receipt.om > receipt && chmod +x receipt +python3 om/receipt/parity/corpus.py /tmp/corp && ./receipt < /tmp/corp/rust.bin +``` + +The harness reads records from stdin and writes one line per record. The record format and the +line format are described in the notes at the end of `receipt.om`. Exit 65 means a short record. + +## Parity + +`OMEGA_OMC=$OMC [OMEGA_BRIDGE=] sh om/receipt/parity/run.sh` + +This command compares the .om output with **recorded** C oracle hashes in `parity/expected.txt`. It +does not build or run any C. It then applies 20 mutants to `receipt.om`. Each mutant must run to exit +0 and change its stream's sha256. With `OMEGA_BRIDGE` set, it also cuts the five entry points into +units, binds them with the pinned `native.om`, and compiles the generated C. + +**Oracle.** `src/omega_receipt.c` (sha256 `88cfe0c5…1651`) and `omega_receipt.h` (`d8043519…1cbf`) at +omega-mv `acb3f15c`. Built with gcc 13.3.0 and `-std=gnu11 -Wall -Wextra -Werror -D_GNU_SOURCE -O2`, +the Makefile CFLAGS that apply to these files. It is linked with the product BLAKE3 +(`src/omega_blake3.c` + `src/sha256.c`, which is itself the bound `b3unit.om`). A second build with +the pre-port C BLAKE3 (`633a1735:src/omega_blake3.c`, sha256 `c89ef819…0f21`) gave byte-identical +output for every stream. The three receipts written by the Rust implementation verify to their +file-name ids. The driver is kept out of the tree. + +**Corpus.** `parity/corpus.py` is deterministic. It has 9,261 records (45 MB) and is regenerated on +every run. + +| stream | records | content | result | +|---|---|---|---| +| rust | 18 | the 3 Rust receipts, each with an upper-case id, compact, indented, keys reversed, and a `\u` escape | 18/18 lines identical | +| shapes | 96 | every receipt shape of `tests/test_omega_resolve.c` `t_receipts`, plus one receipt that uses every field; each as R (placeholder id), M (id computed, then verified) and M with an upper-case id | 96/96 | +| fields | 1,634 | each top-level key missing or given 17 wrong values; escapes and UTF-8 in every string; version and timestamp edges; commit, tier, result and digest forms; the full declared × observed mutation × tier matrix (648); ledger, lease and assertion shapes; production authority spellings; sorting and dedup with multi-byte and escaped strings; formatting variants | 1,634/1,634 | +| json | 3,412 | 110 handwritten syntax cases (every JSON message), 1,500 grammar-fuzz documents and a one-byte corruption of each, 300 random byte strings, a 3,000-key object and its duplicate-key twin | 3,412/3,412 | +| mutants | 4,062 | mutated serialised receipts: every byte of Rust receipt 1 with bit 0x20 flipped (1,168), every truncation of Rust receipt 2 (1,094), 600 random byte flips of the Rust receipts, and 1,200 flips or truncations of receipts built with computed ids | 4,062/4,062 | +| big | 21 | lists of 4,096 entries and 4,097 entries (each list field), 4,096 and 4,097 assertions, 4.1 MB canonical bytes (5 windows), a 1 MiB authority, 3 MB UTF-8 and 3.6 MB `\u` strings, inputs of exactly 4 MiB and 4 MiB + 1, empty input, a 2,000,000-digit timestamp, a 3 MB array | 21/21 | +| tier | 18 | `omega_tier_rank` for 17 strings and the `omega_tier_satisfies` table over -3, -1, 0..8 | 18/18 | + +Every line holds: + +- the parse return code, and the C error message or the canonical bytes in hex; +- the computed id; +- the verify return code, and the message or the id. + +All 69 C messages are covered except three that are unreachable (see below). The mutants stream +alone has 59 receipts that still verify, 211 that parse but fail the id check, and 3,792 that are +refused. + +**Mutants.** Twenty mutants: five JSON (duplicate key, depth, surrogate UTF-8, pair offset, leading +zero) and eleven receipt rules (deny unknown, schema, env_class, PASS needs an assertion, test-only +authority, canonical sort, id compare, `\f` decoding, set dedup, mutation order, u64 overflow). Four +more cover upper-case hex, the BLAKE3 root flag, the canonical window edge and the test-only tier +rule. Every one is killed. + +**ABI check.** This was run outside the tree, by a /tmp driver linking the generated bindings. It ran +every stream through the five bound units and rendered messages from the reason codes. The output +hashed to `expected.txt`. + +On the Spark (arm64), the whole corpus takes 8.7 s for the .om harness and about 1.5 s for the -O2 +C driver. + +## Where the C and the task text differ + +- The task asks to port "receipt building, serialisation, parsing and verification". The C has no + JSON writer and no receipt builder. It parses, validates, builds the canonical bytes, hashes and + verifies. The port follows the C. "Building" in the corpus is the test's `rj_make` step (placeholder + id, compute, patch), done by the harness's M records in the same way as the C driver. +- The C reports errors as `-1` plus a text message, with no numeric error codes. Parity therefore + compares the return code and the full message text, including JSON byte offsets. +- Some code cannot run: + - "a read-only run cannot claim that mutation" is unreachable in the C. At tier rank 4, any + observed mutation with index ≥ 4 has severity ≥ 4 and is refused one line earlier. + - The `need >= 4 && have < 4` clause of `omega_tier_satisfies` is implied by the final + `have >= need`, so removing it changes nothing. + - "out of memory" and "cannot compute canonical identity" occur only when the C's malloc fails. + The port has no heap. + + The port keeps all of these as written. + +## Limits + +- **Struct not exposed.** The parsed `OmegaReceipt` is not returned. Callers get the return code, the + reason, the canonical bytes and the id. `wk` holds offsets into `js`, not decoded strings. + `omega_resolve.c` and `omega_vc_bridge.c` read struct fields: tier_rank, dependencies in input + order, and others. An adapter therefore still needs field accessors before any C can be deleted. + `omega_receipt_free` has no counterpart. +- **Message text.** Function-mode units cannot hold `text`, so units return reason codes. Today the + 69-message table lives only in the harness, and a C adapter would need the same table. +- **Capacities match the C.** Input is limited to 4 MiB (`OMEGA_RECEIPT_MAX_BYTES`), lists and + assertions to 4,096 entries, and nesting to depth 6. Callers provide scratch: `wk` is 512 KiB and + `out` is 1 MiB + 1 KiB. The harness program needs about 5.7 MiB of its 8 MiB stack. +- **Duplicate-key cost.** The duplicate-key check rescans earlier members of the object, which costs + O(keys × object bytes). The C costs O(keys²) strcmp calls. Both are quadratic, and only + pathological objects notice. +- **Tier names.** `rctier` compares all n bytes. The C stops at a NUL, so an adapter passes + `strlen`. +- **Message truncation.** The C truncates messages to `errcap`. The harness and the oracle use 256, + and no message is longer than 160 bytes. diff --git a/om/receipt/parity/corpus.py b/om/receipt/parity/corpus.py new file mode 100755 index 000000000..88098e836 --- /dev/null +++ b/om/receipt/parity/corpus.py @@ -0,0 +1,386 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +"""corpus.py OUTDIR: write the receipt parity corpus, one record stream per file (OUTDIR/.bin), +and print " " per stream. + +A record is an op byte, then the payload length, a and b as 8 decimal digits each, then the payload +(see the harness notes at the end of om/receipt/receipt.om). Everything is deterministic: the +random parts use fixed seeds, so the same corpus comes out on every run. The three Rust-written +receipts are read from tests/resolve/receipts/ of the checkout this file lives in. +""" +import json +import os +import random +import sys + +ROOT = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "..")) +ZERO = "0" * 64 +RUST = ["54f597d22b3753e47d6bd7706b6a7a422c189024912ec4626347e860b5f44a8c", + "7fdff014cb20a0af5b5a5b377532292be29fc15da36d434944ad4521e69de5b3", + "8e885832de2e7123555c47803547161c02196646b270d41078d8b16d4beaf765"] +TIERS = ["TEST_ONLY_TRUST", "HOST_TEST", "QEMU", "QEMU_SECURITY", "MACHINE1_READ_ONLY", "MACHINE1_ATTENDED", + "MACHINE1_MUTATING", "PRODUCTION"] +MUTS = ["NONE", "VOLATILE_ONLY", "REMOVABLE_MEDIA_ONLY", "ONE_TIME_BOOT_SELECTION", "BOUNDED_TEST_REGION_WRITE", + "BOOT_CONFIGURATION_CHANGE", "TRUST_ROOT_CHANGE", "TPM_POLICY_CHANGE", "DESTRUCTIVE_STORAGE"] +MAX = 4 * 1024 * 1024 + + +def rec(op, payload, a=0, b=0): + if isinstance(payload, str): + payload = payload.encode() + return op.encode() + b"%08d%08d%08d" % (len(payload), a, b) + payload + + +def js(s): + """a JSON string literal for s (ASCII escapes for non-ASCII, like serde would accept)""" + return json.dumps(s) + + +# ---- the receipt shape of tests/test_omega_resolve.c (rj_emit), as an ordered list of (key, raw JSON) +def rj(kind="host-v1", tier="HOST_TEST", env_class=None, result="PASS", schema="EvidenceReceiptV1", + decl="NONE", obs="NONE", procedure="fixture", reserved="", feature=None, version=1, inputs=(), + deps=(), root="6" * 64, assert_pass=True, extra=False, dup=False, lease=False, no_assertion=False, + dirty=False, authority="", idhex=ZERO): + f = [("schema", js(schema)), ("version", str(version)), ("id", js(idhex)), ("kind", js(kind))] + if dup: + f.append(("kind", js(kind))) + if extra: + f.append(("surprise", "1")) + f += [("tier", js(tier)), ("result", js(result)), ("timestamp", "1786000000"), ("repo", js("fixture")), + ("commit", js("a" * 40)), ("dirty", "true" if dirty else "false"), ("toolchain", js("fixture")), + ("procedure", js(procedure)), ("machine", js("host")), ("env_class", js(env_class or tier)), + ("input_artifacts", "[" + ", ".join(js(x) for x in inputs) + "]"), ("output_artifacts", "[]")] + if no_assertion: + f.append(("assertions", "[]")) + else: + f.append(("assertions", '[{"id": "fixture_check", "expected": "pass", "observed": "%s", "pass": %s, ' + '"source": "test", "note": ""}]' % ("pass" if assert_pass else "fail", + "true" if assert_pass else "false"))) + f += [("dependencies", "[" + ", ".join(js(x) for x in deps) + "]"), ("declared_mutation", js(decl)), + ("observed_mutation", js(obs)), ("authority", js(authority)), ("output_digest", js(root)), + ("external_refs", "[]"), ("ledger", "null"), + ("lease", '{"hold_id": "%s", "resource": "fixture-lease"}' % ("1" * 64) if lease else "null"), + ("required_features", "[%s]" % js(feature) if feature is not None else "[]"), + ("reserved", js(reserved))] + return f + + +def emit(fields, sep=",\n", kv=": ", head="{\n", tail="\n}\n"): + return head + sep.join(js(k) + kv + v for k, v in fields) + tail + + +def setf(fields, key, raw): + return [(k, raw if k == key else v) for k, v in fields] + + +def dropf(fields, key): + return [(k, v) for k, v in fields if k != key] + + +def sha(c): + return "sha256:" + c * 64 + + +def full(): + """a receipt that uses every field: ledger, lease, lists, several assertions, unicode""" + f = rj(inputs=[sha("1"), sha("2"), "blake3:" + "ab" * 32, "C" * 64], + deps=["8e885832de2e7123555c47803547161c02196646b270d41078d8b16d4beaf765", + "54F597D22B3753E47D6BD7706B6A7A422C189024912EC4626347E860B5F44A8C"]) + f = setf(f, "output_artifacts", "[%s, %s]" % (js("sha512:" + "e" * 128), js("blake3:" + "0f" * 16))) + f = setf(f, "assertions", '[{"id": "zeta", "expected": "1", "observed": "1", "pass": true, "source": "s", ' + '"note": "n\\u00e9"}, {"id": "alpha", "pass": false}, {"id": "\\u03b1lpha", "expected": "x", ' + '"pass": true, "note": "\\ud83d\\ude00"}]') + f = setf(f, "external_refs", '["https://example.invalid/b", "https://example.invalid/a", ' + '"https://example.invalid/a"]') + f = setf(f, "ledger", '{"index": 18446744073709551615, "hash": "%s"}' % ("Ab" * 32)) + f = setf(f, "lease", '{"resource": " r ", "hold_id": "%s"}' % ("c" * 64)) + f = setf(f, "required_features", '["v1-base", "v1-base"]') + f = setf(f, "kind", js("kind \u00e9\u4e2d\U0001F600 \"q\" \\ / \b\f\n\r\t")) + return f + + +def s_rust(): + out = [] + for h in RUST: + raw = open(os.path.join(ROOT, "tests", "resolve", "receipts", h + ".json"), "rb").read() + out.append(rec("R", raw)) + out.append(rec("R", raw.replace(h.encode(), h.upper().encode()))) + doc = json.loads(raw) + out.append(rec("R", json.dumps(doc, separators=(",", ":")))) + out.append(rec("R", json.dumps(doc, indent=4))) + out.append(rec("R", json.dumps(dict(reversed(list(doc.items())))))) + out.append(rec("R", json.dumps(doc, ensure_ascii=True).replace('"PASS"', '"P\\u0041SS"'))) + return out + + +def shape_variants(): + v = [rj(), rj(dup=True), rj(extra=True), rj(schema="EvidenceReceiptV2"), rj(version=2), + rj(feature="v2-extension"), rj(feature="v1-base"), rj(reserved="x"), rj(env_class="QEMU"), + rj(obs="VOLATILE_ONLY"), rj(decl="VOLATILE_ONLY", obs="VOLATILE_ONLY"), rj(no_assertion=True), + rj(no_assertion=True, result="FAIL"), rj(tier="PRODUCTION", kind="production-v1", authority="release-key-1"), + rj(lease=True), rj(dirty=True), rj(assert_pass=False, result="FAIL"), + rj(inputs=[sha("1"), sha("2")]), rj(inputs=[sha("2"), sha("1")]), rj(inputs=[sha("1"), sha("2")], procedure="another procedure"), + rj(deps=RUST[:2]), rj(deps=[RUST[1], RUST[0], RUST[1]])] + for a in ["", " ", " ", "\t", "TEST-ONLY signer", "a test_only key", "TestOnly", "test-only", "TEST_ONLY"]: + v.append(rj(tier="PRODUCTION", kind="production-v1", authority=a)) + return v + + +def s_shapes(): + out = [] + for f in shape_variants(): + d = emit(f) + out += [rec("R", d), rec("M", d), rec("M", d, 0, 257)] + d = emit(full()) + out += [rec("R", d), rec("M", d), rec("M", d, 0, 257)] + return out + + +BADVALS = ["1", "-1", "1.5", "1e3", "\"x\"", "\"\"", "[]", "[1]", "[\"x\"]", "{}", "null", "true", "false", + "18446744073709551615", "18446744073709551616", "0", "-0"] + + +def s_fields(): + out = [] + base = rj() + keys = [k for k, _ in base] + for k in keys: + out.append(rec("M", emit(dropf(base, k)))) + for bv in BADVALS: + out.append(rec("M", emit(setf(base, k, bv)))) + for k in ["schema", "kind", "tier", "repo", "commit", "toolchain", "procedure", "machine", "env_class", + "authority", "reserved"]: + for s in ["", " ", "\\u0041", "h\\u00e9llo", "\\ud83d\\ude00", "\\/\\\\\\\"", "raw \u00e9\u4e2d\U0001F600"]: + out.append(rec("M", emit(setf(base, k, '"%s"' % s)))) + for n in ["0", "1", "2", "01", "00", "4294967295", "4294967296", "18446744073709551615", + "18446744073709551616", "99999999999999999999", "100000000000000000000", "1.0", "1e0", "1E0", + "-1", "-0", "1x", "1.", "1e+-.5", " 7 "]: + out.append(rec("M", emit(setf(base, "version", n)))) + out.append(rec("M", emit(setf(base, "timestamp", n)))) + for c in ["a" * 40, "a" * 64, "a" * 39, "a" * 41, "A" * 40, "g" * 40, "0123456789abcdef" * 4, "\\u0061" * 40, "a" * 63]: + out.append(rec("M", emit(setf(base, "commit", js(c) if "\\" not in c else '"%s"' % c)))) + for t in TIERS + ["host_test", "HOST_TEST ", "", "PRODUCTIO", "PRODUCTIONS"]: + out.append(rec("M", emit(setf(setf(base, "tier", js(t)), "env_class", js(t))))) + out.append(rec("M", emit(setf(base, "tier", js(t))))) + for r in ["PASS", "FAIL", "BLOCKED", "INCOMPLETE", "SKIPPED", "pass", "", "PASSED"]: + out.append(rec("M", emit(setf(base, "result", js(r))))) + out.append(rec("M", emit(setf(setf(base, "result", js(r)), "assertions", "[]")))) + # the mutation and tier rules: every declared x observed x tier + for t in TIERS: + for d in MUTS: + for o in MUTS: + f = rj(tier=t, decl=d, obs=o, authority="release-key" if t == "PRODUCTION" else "") + out.append(rec("M", emit(f))) + for m in ["none", "", "NONE ", "DESTRUCTIVE"]: + out.append(rec("M", emit(setf(base, "declared_mutation", js(m))))) + out.append(rec("M", emit(setf(base, "observed_mutation", js(m))))) + digests = [sha("1"), "sha512:" + "a" * 128, "blake3:" + "a" * 32, "blake3:" + "a" * 31, "blake3:" + "a" * 33, + "blake3:" + "A" * 34, "blake3:", "blake3:" + "g" * 32, "blake3:" + "a" * 32 + ":", "md5:" + "a" * 32, + "sha256x:" + "a" * 64, "sha25:" + "a" * 64, ":" + "a" * 64, "a" * 64, "A" * 64, "a" * 63, "a" * 65, + "g" * 64, "", "BLAKE3:" + "a" * 64, "sha256::" + "a" * 64, "\\u0073ha256:" + "a" * 64] + for d in digests: + lit = '"%s"' % d + out.append(rec("M", emit(setf(base, "input_artifacts", "[%s]" % lit)))) + out.append(rec("M", emit(setf(base, "output_artifacts", "[%s, %s]" % (js(sha("3")), lit))))) + for h in ["a" * 64, "A" * 64, "a" * 63, "a" * 65, "g" * 64, "", "\\u0061" * 64]: + lit = '"%s"' % h + out.append(rec("M", emit(setf(base, "output_digest", lit)))) + out.append(rec("M", emit(setf(base, "dependencies", "[%s]" % lit)))) + out.append(rec("M", emit(setf(base, "ledger", '{"index": 3, "hash": %s}' % lit)))) + out.append(rec("M", emit(setf(base, "lease", '{"hold_id": %s, "resource": "x"}' % lit)))) + # the id field itself (R: the placeholder stays, verify reports the mismatch or the parse error) + for h in ["a" * 63, "a" * 65, "g" * 64, "", "A" * 64]: + out.append(rec("R", emit(setf(base, "id", '"%s"' % h)))) + for lg in ["null", "1", "\"x\"", "[]", "{}", '{"index": 1}', '{"hash": "%s"}' % ("a" * 64), + '{"index": -1, "hash": "%s"}' % ("a" * 64), '{"index": 1, "hash": 5}', + '{"index": 1, "hash": "%s", "x": 1}' % ("a" * 64), '{"index": 0, "hash": "%s"}' % ("a" * 64)]: + out.append(rec("M", emit(setf(base, "ledger", lg)))) + for ls in ["null", "1", "[]", "{}", '{"hold_id": "%s"}' % ("1" * 64), '{"resource": "x"}', + '{"hold_id": "%s", "resource": ""}' % ("1" * 64), '{"hold_id": "%s", "resource": " \\t\\n\\r"}' % ("1" * 64), + '{"hold_id": "%s", "resource": " x"}' % ("1" * 64), '{"hold_id": "%s", "resource": 1}' % ("1" * 64), + '{"hold_id": 1, "resource": "x"}', '{"hold_id": "%s", "resource": "x", "y": 2}' % ("1" * 64)]: + out.append(rec("M", emit(setf(base, "lease", ls)))) + for a in ['[]', '[1]', '"x"', '{}', '[{}]', '[{"id": "a"}]', '[{"pass": true}]', '[{"id": "", "pass": true}]', + '[{"id": "a", "pass": true}, {"id": "a", "pass": false}]', '[{"id": "a", "pass": 1}]', + '[{"id": 1, "pass": true}]', '[{"id": "a", "pass": true, "extra": 1}]', + '[{"id": "a", "pass": true, "note": null}]', '[{"id": "b", "pass": true}, {"id": "a", "pass": true}]', + '[{"id": "a", "pass": true}, {"id": "\\u0061", "pass": true}]', '[{"id": "a", "pass": true}, 7]', + '[{"id": "a", "pass": true, "expected": "e", "observed": "o", "source": "s", "note": "n"}]', + '[{"note": "n", "source": "s", "pass": false, "observed": "o", "expected": "e", "id": "z"}]', + '[{"id": "a", "pass": true, "expected": 1}]', '[{"id": "a", "pass": true, "observed": []}]', + '[{"id": "a", "pass": true, "source": {}}]', '[{"id": "a", "pass": null}]']: + out.append(rec("M", emit(setf(base, "assertions", a)))) + out.append(rec("M", emit(setf(setf(base, "assertions", a), "result", '"FAIL"')))) + for fl in ['[]', '["v1-base"]', '["v1-base", "x"]', '["x"]', '[1]', '["v1-bas"]', '["v1-base "]', '"v1-base"']: + out.append(rec("M", emit(setf(base, "required_features", fl)))) + for ex in ['[]', '["b", "a", "b", "", "\\u00e9", "\\u00e8", "z"]', '[1]', '"x"', 'null']: + out.append(rec("M", emit(setf(base, "external_refs", ex)))) + # sorting: byte order of the decoded strings, multi-byte UTF-8 and escapes, duplicates + rnd = random.Random(7) + alpha = ["a", "b", "A", "Z", "0", ":", "~", "\u00e9", "\u4e2d", "\U0001F600", "\\u0062", "\\/"] + for i in range(40): + items = [] + for j in range(rnd.randint(0, 12)): + items.append("".join(rnd.choice(alpha) for _ in range(rnd.randint(0, 5)))) + lit = "[" + ", ".join('"%s"' % x for x in items) + "]" + out.append(rec("M", emit(setf(base, "external_refs", lit)))) + deps = [rnd.choice(RUST) for _ in range(rnd.randint(0, 6))] + deps = [d.upper() if rnd.random() < 0.4 else d for d in deps] + out.append(rec("M", emit(setf(base, "dependencies", "[" + ", ".join(js(d) for d in deps) + "]")))) + asr = ", ".join('{"id": "%s", "pass": true, "note": "%d"}' % (x, k) for k, x in enumerate(dict.fromkeys(items))) + out.append(rec("M", emit(setf(base, "assertions", "[" + asr + "]")))) + for a in ["release-key", "", " ", " \t\r\n", "x", "test-only", "TEST-ONLY", "Test_Only", "testonly", "TESTONLY", + "my-test-onl", "test only", "testonl", "xtest-onlyx", "\\u0074est-only", "t\\u0045st_only", + "aaaaaaaaaatestonly", "tes\u00e9tonly", "\u00e9testonly"]: + out.append(rec("M", emit(rj(tier="PRODUCTION", authority=a)))) + out.append(rec("M", emit(rj(tier="MACHINE1_MUTATING", authority=a)))) + out.append(rec("M", emit(dropf(rj(tier="PRODUCTION"), "authority")))) + # formatting: compact, other whitespace, key order (canonical bytes do not depend on them) + for f in [base, full()]: + out.append(rec("M", emit(f, sep=",", kv=":", head="{", tail="}"))) + out.append(rec("M", emit(f, sep=" ,\r\n\t", kv=" :\t", head=" \n\t{ ", tail=" } \r\n "))) + out.append(rec("M", emit(list(reversed(f))))) + out.append(rec("M", emit(base) + " x")) + out.append(rec("M", emit(base, tail="\n}\n,"))) + return out + + +JSON_CASES = [ + "", " ", "{", "}", "[", "]", "{}", "[]", "null", "true", "false", "nul", "tru", "fals", "0", "-", "-0", "01", "-01", + "1.", "1e", "1x", "\"", "\"abc", "\"a\\", "\"\\x\"", "\"\\u12\"", "\"\\u12g4\"", "\"\\ud800\"", "\"\\ud800\\u0041\"", + "\"\\ud800\\udc00\"", "\"\\udc00\"", "\"\\u0000\"", "\"\\ud800x\"", "\"\\ud800\\\"", "\"\\ud800\\udbff\"", + "\"\\ud800\\ue000\"", "\"\x01\"", "\"\x1f\"", "\"\x7f\"", "\"\xc0\x80\"", "\"\xc2\x80\"", "\"\xc2\"", "\"\xe0\x80\x80\"", + "\"\xe0\xa0\x80\"", "\"\xed\xa0\x80\"", "\"\xef\xbf\xbf\"", "\"\xf0\x80\x80\x80\"", "\"\xf0\x90\x80\x80\"", + "\"\xf4\x8f\xbf\xbf\"", "\"\xf4\x90\x80\x80\"", "\"\xf5\x80\x80\x80\"", "\"\xff\"", "\"\xe2\x82\"", "\"\xf0\x9f\x98\"", + "{\"a\"}", "{\"a\" 1}", "{\"a\":}", "{\"a\":1,}", "{\"a\":1 \"b\":2}", "{\"a\":1,\"a\":2}", "{\"a\":1,\"\\u0061\":2}", + "{\"a\":{\"a\":1},\"b\":{\"a\":2,\"a\":3}}", "{1:2}", "{,}", "[1,]", "[,1]", "[1 2]", "[1,,2]", "{\"a\":[1,{\"b\":2}],\"c\":3}", + "[[[[[[1]]]]]]", "[[[[[[[1]]]]]]]", "[[[[[[]]]]]]", "[[[[[[[]]]]]]]", "{\"a\":{\"b\":{\"c\":{\"d\":{\"e\":{}}}}}}", + "{\"a\":{\"b\":{\"c\":{\"d\":{\"e\":{\"f\":1}}}}}}", "1 2", "{} {}", "{}x", " {} ", "\t\n\r{}\r\n\t", "{}\x00", + "\x00", "\xef\xbb\xbf{}", "{\"a\":\"\\b\\f\\n\\r\\t\\/\\\\\\\"\"}", "{\"a\":1e5}", "{\"a\":-1.5e-3}", "{\"a\":1E+2}", + "{\"a\":nul}", "{\"a\":truex}", "{\"a\":\"x\"}", "[\"a\",\"a\"]", "{\"\":1,\"\":2}", "{\"a\":1}", "[1]", "\"x\"", "7", + "{\"schema\":1}", "{\"zzz\":1}", "[{}]", "{\"a\":[1,2,3,{\"b\":[4,{\"c\":5}]}]}", "{\"a\":01}", "{\"a\":-}", + "{\"a\":.5}", "{\"a\":+1}", "{\"a\":\"\\u00\"}", "{\"a\":\"\\uD83D\\uDE00\"}", "{\"a\":\"\\uDE00\\uD83D\"}", + "{\"a\" : 1 , \"b\" : [ ] , \"c\" : { } }", "{\"a\":1,\"b\":2,\"c\":3,\"b\":4}", +] + + +def fuzz_json(rnd, depth=0): + r = rnd.random() + if depth > 7 or r < 0.3: + return rnd.choice(["1", "-2", "0", "3.5", "1e9", "true", "false", "null", "\"s\"", "\"\\u00e9\"", + "\"\\ud83d\\ude00\"", "\"a\\nb\"", "18446744073709551616", "\"k\""]) + if r < 0.65: + return "[" + ",".join(fuzz_json(rnd, depth + 1) for _ in range(rnd.randint(0, 4))) + "]" + keys = ["a", "b", "c", "\\u0061", "id", "\u00e9"] + return "{" + ",".join('"%s":%s' % (rnd.choice(keys), fuzz_json(rnd, depth + 1)) + for _ in range(rnd.randint(0, 4))) + "}" + + +def s_json(): + out = [rec("R", c.encode("latin-1")) for c in JSON_CASES] + rnd = random.Random(11) + for _ in range(1500): + d = fuzz_json(rnd).encode() + out.append(rec("R", d)) + if d: + b = bytearray(d) + p = rnd.randrange(len(b)) + b[p] = rnd.choice(b' {}[]:,"\\x0\x01\xff-') + out.append(rec("R", bytes(b))) + for _ in range(300): + out.append(rec("R", bytes(rnd.randrange(256) for _ in range(rnd.randint(1, 40))))) + big = "{" + ",".join('"k%d":%d' % (i, i) for i in range(3000)) + "}" + out.append(rec("R", big)) + out.append(rec("R", big[:-1] + ',"k2999":1}')) + return out + + +def s_mutants(): + out = [] + rnd = random.Random(1726) + raws = [open(os.path.join(ROOT, "tests", "resolve", "receipts", h + ".json"), "rb").read() for h in RUST] + # every position of one Rust receipt with its 0x20 bit flipped (case flips, quote/space swaps) + r0 = raws[0] + for p in range(len(r0)): + b = bytearray(r0) + b[p] ^= 0x20 + out.append(rec("R", bytes(b))) + # every truncation of another + r1 = raws[1] + for n in range(len(r1)): + out.append(rec("R", r1[:n])) + # random single-byte flips of all three + for _ in range(600): + r = rnd.choice(raws) + b = bytearray(r) + p = rnd.randrange(len(b)) + b[p] ^= rnd.randint(1, 255) + out.append(rec("R", bytes(b))) + # receipts built here (id computed by the reader under test), then flipped or truncated + bases = [emit(f) for f in shape_variants()[:4]] + [emit(full()), emit(rj(lease=True, inputs=[sha("9")]))] + for _ in range(1200): + d = rnd.choice(bases) + if rnd.random() < 0.2: + out.append(rec("M", d, rnd.randrange(len(d)), 256)) + else: + out.append(rec("M", d, rnd.randrange(len(d)), rnd.randint(1, 255))) + return out + + +def s_big(): + out = [] + many = [sha("%x" % (i % 16)) [:7] + "%064x" % (i * 2654435761) for i in range(4096)] + out.append(rec("M", emit(setf(rj(), "input_artifacts", "[" + ", ".join(js(x) for x in many) + "]")))) + out.append(rec("M", emit(setf(rj(), "input_artifacts", "[" + ", ".join(js(x) for x in many + [sha("f")]) + "]")))) + out.append(rec("M", emit(setf(rj(), "external_refs", "[" + ", ".join(js(x) for x in many[::-1] + many[:100]) + "]")))) + over = "[" + ", ".join(js(x) for x in many + [sha("f")]) + "]" + for k in ["output_artifacts", "dependencies", "required_features"]: + out.append(rec("M", emit(setf(rj(), k, over)))) + asr = ", ".join('{"id": "a%05d", "pass": true}' % ((i * 7919) % 4096) for i in range(4096)) + out.append(rec("M", emit(setf(rj(), "assertions", "[" + asr + "]")))) + out.append(rec("M", emit(setf(rj(), "assertions", "[" + asr + ', {"id": "x", "pass": true}]')))) + # canonical bytes over 1 MiB (several harness windows): 4096 long digests + longd = ["sha512:" + ("%04x" % i) * 250 for i in range(4096)] + out.append(rec("M", emit(setf(rj(), "output_artifacts", "[" + ", ".join(js(x) for x in longd) + "]")))) + out.append(rec("M", emit(rj(tier="PRODUCTION", authority="k" * (1 << 20))))) + out.append(rec("M", emit(rj(tier="PRODUCTION", authority="k" * (1 << 20) + "test-only")))) + out.append(rec("M", emit(setf(rj(), "toolchain", '"' + "\u00e9" * 1500000 + '"')))) + out.append(rec("M", emit(setf(rj(), "toolchain", '"' + "\\u4e2d" * 600000 + '"')))) + d = emit(rj()) + out.append(rec("M", d + " " * (MAX - len(d)))) + out.append(rec("M", d + " " * (MAX - len(d) + 1))) + out.append(rec("R", d + " " * (MAX - len(d) + 1))) + out.append(rec("R", b"")) + out.append(rec("M", emit(setf(rj(), "timestamp", "1" + "0" * 2000000)))) + out.append(rec("M", emit(setf(rj(), "version", "1." + "0" * 1500000)))) + out.append(rec("R", "[" + "0," * 1500000 + "0]")) + out.append(rec("R", " " * (MAX - 2) + "{}")) + return out + + +def s_tier(): + out = [rec("T", t) for t in TIERS] + out += [rec("T", t) for t in ["", "host_test", "HOST_TEST ", " HOST_TEST", "PRODUCTIO", "PRODUCTIONX", "QEMU_", + "TEST_ONLY_TRUST\xe9", "x" * 300]] + out.append(rec("S", b"")) + return out + + +STREAMS = [("rust", s_rust), ("shapes", s_shapes), ("fields", s_fields), ("json", s_json), + ("mutants", s_mutants), ("big", s_big), ("tier", s_tier)] + + +def main(): + if len(sys.argv) != 2: + sys.exit(__doc__) + os.makedirs(sys.argv[1], exist_ok=True) + for name, fn in STREAMS: + recs = fn() + with open(os.path.join(sys.argv[1], name + ".bin"), "wb") as f: + for r in recs: + f.write(r) + print(name, len(recs)) + + +if __name__ == "__main__": + main() diff --git a/om/receipt/parity/expected.txt b/om/receipt/parity/expected.txt new file mode 100644 index 000000000..a8ee5425e --- /dev/null +++ b/om/receipt/parity/expected.txt @@ -0,0 +1,10 @@ +# stream lines sha256 of the C oracle driver's output (src/omega_receipt.c 88cfe0c5...1651 at omega-mv acb3f15c, +# gcc 13.3.0 -std=gnu11 -Wall -Wextra -Werror -D_GNU_SOURCE -O2, BLAKE3 from src/omega_blake3.c + src/sha256.c). +# Recorded once; parity/run.sh compares receipt.om against these hashes and builds no C. +rust 18 a86b56cb4d8de65b280ab528d233c6bdbeaa8251a2f77250068bdf8051be972c +shapes 96 7d77d8daa4127f24cca49304bc3cd7981c0e8f2ab363abb7af701843be2f3d0a +fields 1634 bf2c85950cfc8cc9905495719ac9c2e22dcd80b15eeb65f1d08dcf8e66b54e65 +json 3412 dceb67e08df6d67f7a3ec01e1406dedba160b1dca4ff2896d397fc93010fb293 +mutants 4062 5c8f513c1e4dbb7968f59920fe938cf175102465ef7d0e44a0816e1404e16749 +big 21 5aefacd53d4f5da8cdbfec4b56b0b786594eae5f56b45e94b22d0dbde6d6f678 +tier 18 dd9dff1784451257c48f755c7cf90c50374748065cd1c0a1bb3657475bf1894d diff --git a/om/receipt/parity/run.sh b/om/receipt/parity/run.sh new file mode 100755 index 000000000..ab0c574e9 --- /dev/null +++ b/om/receipt/parity/run.sh @@ -0,0 +1,96 @@ +#!/bin/sh +# SPDX-License-Identifier: Apache-2.0 +# Parity of om/receipt/receipt.om against the C it ports (src/omega_receipt.c + omega_receipt.h), by +# RECORDED oracle output: this script does not build or run any C. corpus.py regenerates the corpus, +# the .om harness runs every stream, and each stream's output must have the line count and sha256 in +# parity/expected.txt, which is the C oracle driver's output recorded when the port was made (the +# driver is not in the tree; README.md says how to rerun it). Then small mutants of receipt.om must +# each run to completion and change their stream's hash (the corpus discriminates). With OMEGA_BRIDGE +# set, the five entry points are also cut into function-mode units (unit.py), bound by the pinned +# omega-bridge generator and compiled as C (bindability). +# OMEGA_OMC seed-born omc, sha256 pinned below (exit 3 NOT_RUN without it) +# OMEGA_BRIDGE optional: an aien-dev/omega-bridge checkout whose src/native.om matches the pin +set -eu +OMC_PIN=62775d2a391cfef7cde64e387655caa9809a267093112910ec76bfa568f3712b +NATIVE_PIN=2c2c53f61d4ab302f1a2092c3c32189335dd53f6cf2b388f47a475f99f2c3940 +P=$(cd "$(dirname "$0")" && pwd) +OM=$P/../receipt.om +[ -n "${OMEGA_OMC:-}" ] || { echo "RECEIPT-PARITY NOT_RUN: set OMEGA_OMC"; exit 3; } +[ "$(sha256sum < "$OMEGA_OMC" | cut -c1-64)" = "$OMC_PIN" ] || { echo "RECEIPT-PARITY NOT_RUN: omc is not the pinned compiler"; exit 3; } +command -v python3 > /dev/null || { echo "RECEIPT-PARITY NOT_RUN: python3 missing"; exit 3; } +T=$(mktemp -d); trap 'rm -rf "$T"' EXIT +python3 "$P/corpus.py" "$T/corp" > "$T/counts" +"$OMEGA_OMC" < "$OM" > "$T/rc"; chmod +x "$T/rc" +grep -v '^#' "$P/expected.txt" | while read -r s n want; do + rc=0; "$T/rc" < "$T/corp/$s.bin" > "$T/$s.out" || rc=$? + got=$(sha256sum < "$T/$s.out" | cut -c1-64) + lines=$(wc -l < "$T/$s.out") + if [ "$rc" = 0 ] && [ "$got" = "$want" ] && [ "$lines" = "$n" ]; then + echo "PASS $s: $n lines identical to the recorded C oracle output ($(grep "^$s " "$T/counts" | cut -d' ' -f2) records)" + else + echo "FAIL $s: exit $rc, $lines lines, sha256 $got (want $n, $want)"; echo x >> "$T/failed" + fi +done +[ ! -e "$T/failed" ] || { echo "RECEIPT-PARITY FAIL"; exit 1; } + +# Mutants: name | stream | exact text in receipt.om (\n for a newline) | replacement. +# Killed = the mutant harness exits 0 with the expected line count and a different sha256. +cat > "$T/mutants" <<'EOF' +json_dup_key|json|if dupkey js n f k|if eq k 0 +json_depth|json|if gt d 6|if gt d 7 +json_surrogate_utf8|json|if and ge cp 2048 not and ge cp 55296 le cp 57343|if ge cp 2048 +json_pair_offset|json|ret fail wk 7 add p 4|ret fail wk 7 p +json_leading_zero|json|if gt sub q d0 1|if gt sub q d0 2 +deny_unknown|shapes|if eq k 999\nret failk wk 22 0|if false\nret failk wk 22 0 +schema|shapes|if not nameq js wk get wk 64 59|if false +env_class|shapes|if ne dcmp js get wk 77 get wk 68 0|if false +pass_needs_assertion|shapes|if and eq m 0 eq get wk 103 0|if false +prod_test_only|shapes|if eq get a 8 1\nret fail wk 60 0|if false\nret fail wk 60 0 +canon_sort|shapes|put wk 7 0\nmsort js wk m|put wk 7 0 +id_compare|shapes|if not same|if false +escape_f|shapes|if eq e 102\nret 12|if eq e 102\nret 13 +set_dedup|fields|ret ne dcmp js x y 0|ret true +mutation_order|fields|if gt so sev get wk 105|if false +u64_overflow|fields|if gt x div sub 18446744073709551615 dg 10|if false +upper_hex|rust|ret sub c 55|ret 16 +b3_root|rust|put w 74 or get w 74 8|put w 74 or get w 74 0 +canon_window|big|if and ge t lo lt t add lo 1048576|if and ge t lo lt t add lo 1048575 +tier_test_only|tier|if eq need 0\nret 1|if eq need 0\nret 0 +EOF +while IFS='|' read -r name s old new; do + python3 - "$OM" "$T/m.om" "$old" "$new" <<'PY' +import sys +src, dst, old, new = sys.argv[1], sys.argv[2], sys.argv[3].replace("\\n", "\n"), sys.argv[4].replace("\\n", "\n") +s = open(src).read() +assert s.count(old + "\n") == 1, "mutant text not found exactly once: " + old +open(dst, "w").write(s.replace(old + "\n", new + "\n")) +PY + "$OMEGA_OMC" < "$T/m.om" > "$T/m"; chmod +x "$T/m" + rc=0; "$T/m" < "$T/corp/$s.bin" > "$T/m.out" 2> /dev/null || rc=$? + got=$(sha256sum < "$T/m.out" | cut -c1-64) + lines=$(wc -l < "$T/m.out") + want=$(grep "^$s " "$P/expected.txt" | cut -d' ' -f3) + n=$(grep "^$s " "$P/expected.txt" | cut -d' ' -f2) + if [ "$rc" != 0 ] || [ "$lines" != "$n" ] || [ "$got" = "$want" ]; then + echo "mutant $name NOT killed on $s (exit $rc, $lines lines, sha256 $got)"; echo x >> "$T/failed"; continue + fi + dl=$(paste -d'\n' "$T/m.out" "$T/$s.out" | awk 'NR % 2 == 1 { a = $0; next } a != $0' | wc -l) + echo "mutant $name killed on $s (exit 0, $dl of $n lines differ)" +done < "$T/mutants" +[ ! -e "$T/failed" ] || { echo "RECEIPT-PARITY FAIL"; exit 1; } + +if [ -z "${OMEGA_BRIDGE:-}" ]; then + echo "RECEIPT-PARITY PASS (bindability NOT_RUN: set OMEGA_BRIDGE)"; exit 0 +fi +[ "$(sha256sum < "$OMEGA_BRIDGE/src/native.om" | cut -c1-64)" = "$NATIVE_PIN" ] || { echo "RECEIPT-PARITY PASS (bindability NOT_RUN: native.om is not the pinned generator)"; exit 0; } +"$OMEGA_OMC" < "$OMEGA_BRIDGE/src/native.om" > "$T/native"; chmod +x "$T/native" +: > "$T/units" +for u in rcparse rccanon rcverify rctier rcsat; do + python3 "$P/unit.py" "$OM" "$u" > "$T/u.om" + "$OMEGA_OMC" < "$T/u.om" > "$T/u.bin" + { cat "$T/u.om"; printf '\0'; printf '%08d' "$(wc -c < "$T/u.bin")"; cat "$T/u.bin"; } >> "$T/units" +done +"$T/native" < "$T/units" > "$T/units.c" +cc -std=gnu11 -O2 -Wall -Wextra -Werror -c "$T/units.c" -o "$T/units.o" +echo "bindability: 5 function-mode units accepted by native.om and compiled ($(grep -c ') uint32_t omega_[a-z0-9]*(uint64_t \*result' "$T/units.c") exported functions)" +echo "RECEIPT-PARITY PASS" diff --git a/om/receipt/parity/unit.py b/om/receipt/parity/unit.py new file mode 100644 index 000000000..2c6b24eae --- /dev/null +++ b/om/receipt/parity/unit.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +"""unit.py RECEIPT.om NAME: print the function-mode unit that exports NAME. + +Copied from om/gpumath/parity/unit.py (aien-dev/omega-mv#751); only the file names differ. + +The unit is NAME and every library function it reaches through calls (the part of receipt.om +before the parity-harness marker), in file order, with NAME moved to the end: the seeded compiler +enters a function-mode unit at its last function and the omega-bridge native call ABI v1 binds that +one; the others stay private helpers. Note lines before a kept function travel with it. +""" +import sys + +MARK = "note ==== parity harness" + + +def functions(lib): + """[(name, first line incl. leading notes, end line)] of the library.""" + out = [] + i = 0 + while i < len(lib): + w = lib[i].split() + if w[:1] == ["fn"]: + j = i + while j > 0 and lib[j - 1].startswith("note") and not lib[j - 1].startswith("note ----"): + j -= 1 + depth = 0 + for k in range(i, len(lib)): + t = lib[k].split()[:1] + if t and t[0] in ("fn", "if", "while", "for"): + depth += 1 + elif t == ["end"]: + depth -= 1 + if depth == 0: + break + else: + sys.exit("unit.py: %s has no closing end" % w[1]) + out.append((w[1], j, k)) + i = k + 1 + else: + i += 1 + return out + + +def main(): + if len(sys.argv) != 3: + sys.exit(__doc__) + src, name = sys.argv[1], sys.argv[2] + lines = open(src).read().split("\n") + lib = lines[:next(i for i, l in enumerate(lines) if l.startswith(MARK))] + fns = functions(lib) + span = {n: (a, b) for n, a, b in fns} + if name not in span: + sys.exit("unit.py: no function %s" % name) + calls = {} + for n, a, b in fns: + words = set() + for l in lib[a:b + 1]: + if not l.startswith("note"): + words.update(l.split()) + calls[n] = {m for m in words if m in span and m != n} + keep = {name} + todo = [name] + while todo: + for m in calls[todo.pop()]: + if m not in keep: + keep.add(m) + todo.append(m) + order = [n for n, _, _ in fns if n in keep and n != name] + [name] + parts = [] + for n in order: + a, b = span[n] + parts.append("\n".join(lib[a:b + 1])) + sys.stdout.write("note SPDX-License-Identifier: Apache-2.0\nnote generated from om/receipt/receipt.om by parity/unit.py %s\n\n" % name) + sys.stdout.write("\n\n".join(parts) + "\n") + + +if __name__ == "__main__": + main() diff --git a/om/receipt/receipt.om b/om/receipt/receipt.om new file mode 100644 index 000000000..ea5153db9 --- /dev/null +++ b/om/receipt/receipt.om @@ -0,0 +1,2698 @@ +note SPDX-License-Identifier: Apache-2.0 +note receipt.om: seeded Omega port of src/omega_receipt.c (+ omega_receipt.h), the reader of the +note aien-proof EvidenceReceiptV1 wire contract: strict JSON parse (serde shape rules, deny unknown +note fields), every canonical_bytes validation, the canonical byte encoding and id = BLAKE3(canonical). +note Part of aien-dev/omega-mv#726, aien-dev/aien-architecture-mv#300 and #316. +note +note Library (everything above the parity-harness marker) uses no globals and no text, so each entry +note point can be cut into a function-mode unit for the omega-bridge native call ABI v1: +note rcparse js n wk 0 ok, -1 refused (reason in wk 0..2) +note rccanon js n wk out pos after rcparse returned 0: total canonical length; canonical bytes +note [pos, pos + 1048576) into out 0.., id = BLAKE3(canonical) into out 1049600.. +note rcverify js n wk out parse, recompute, require the id field to equal it (any hex case) +note rctier s n wk tier rank of the bytes s[0..n), -1 unknown +note rcsat need have the aien-proof tier_satisfies rule +note js is the receipt JSON (n bytes, at most 4 MiB, the C OMEGA_RECEIPT_MAX_BYTES). +note A refusal is a reason code (wk 0), a field-name index (wk 1, for messages that name a field) and a +note byte offset (wk 2, for JSON syntax messages). The parity harness renders the exact C message text. +note There is no DOM: pass 1 checks the JSON syntax exactly as the C reader does (same errors, same +note byte offsets); pass 2 reads fields by walking the validated text; strings are decoded on the fly. +note +note wk layout (ulong 65536): 0 code, 1 field, 2 byte offset, 3 n, 4 root offset, 5..7 sort scratch, +note 8..10 canonical emitter (bytes emitted, window start, chunk fill), 16+k value offset of top-level +note key k (0 absent), 64+k string content offset of top-level key k (0 = absent, reads as ""), +note 100 version, 101 timestamp, 102 dirty, 103 result, 104 tier rank, 105 declared mutation, +note 106 observed mutation, 107 has ledger, 108 ledger index, 109 ledger hash, 110 has lease, +note 111 lease hold_id, 112 lease resource, 113 assertion count, 114+j count of list j +note (0 input_artifacts, 1 output_artifacts, 2 dependencies, 3 external_refs, 4 required_features), +note 128.. name table (65 entries of 5 words: length, then up to 32 bytes packed little endian), +note 512..959 BLAKE3 chaining-value stack (952 depth, 953 chunks absorbed), +note 1024 + 4096 j list j string offsets, 21504 + 6 i assertion i (id expected observed pass source note), +note 46080 sort area, 50176 merge scratch. +note out layout (uchar 1049632): 0..1048575 canonical window, 1048576..1049599 BLAKE3 chunk, +note 1049600..1049631 computed id. + +note Array lengths are literal numbers: function-mode units refuse `const` (error 9). + +note ---- BLAKE3: copied from om/blake3/b3unit.om (no import in Omegese). Changes: the chaining-value +note ---- stack lives in wk at 512 (depth 952, count 953), the chunk is read from out at 1048576, and +note ---- the root bytes go to out at 1049600. The compression code is unchanged. +fn initw w edit ulong 128 to void +put w 83 1779033703 +put w 84 3144134277 +put w 85 1013904242 +put w 86 2773480762 +put w 87 1359893119 +put w 88 2600822924 +put w 89 528734635 +put w 90 1541459225 +put w 91 2 +put w 92 6 +put w 93 3 +put w 94 10 +put w 95 7 +put w 96 0 +put w 97 4 +put w 98 13 +put w 99 1 +put w 100 11 +put w 101 12 +put w 102 5 +put w 103 9 +put w 104 14 +put w 105 15 +put w 106 8 +end + +note the G mixing function on state words a b c d with message words 2j and 2j+1 +fn g w edit ulong 128 a ulong b ulong c ulong d ulong j ulong to void +x = get w add 16 mul j 2 +t = add get w a get w b +put w a and add t x 4294967295 +t = xor get w d get w a +put w d and or shr t 16 shl t 16 4294967295 +put w c and add get w c get w d 4294967295 +t = xor get w b get w c +put w b and or shr t 12 shl t 20 4294967295 +x = get w add 17 mul j 2 +t = add get w a get w b +put w a and add t x 4294967295 +t = xor get w d get w a +put w d and or shr t 8 shl t 24 4294967295 +put w c and add get w c get w d 4294967295 +t = xor get w b get w c +put w b and or shr t 7 shl t 25 4294967295 +end + +fn round w edit ulong 128 to void +g w 0 4 8 12 0 +g w 1 5 9 13 1 +g w 2 6 10 14 2 +g w 3 7 11 15 3 +g w 0 5 10 15 4 +g w 1 6 11 12 5 +g w 2 7 8 13 6 +g w 3 4 9 14 7 +end + +fn permute w edit ulong 128 to void +for i ulong 16 +put w add 32 i get w add 16 get w add 91 i +end +for i ulong 16 +put w add 16 i get w add 32 i +end +end + +note the 16 little-endian words of blk into m +fn loadm w edit ulong 128 blk edit uchar 64 to void +for i ulong 16 +o = mul i 4 +v = ulong get blk add o 3 +v = or shl v 8 ulong get blk add o 2 +v = or shl v 8 ulong get blk add o 1 +v = or shl v 8 ulong get blk o +put w add 16 i v +end +end + +note compress (input chaining value, blk, block length, counter, flags) into the output words +fn compress w edit ulong 128 blk edit uchar 64 to void +loadm w blk +for i ulong 8 +put w i get w add 48 i +put w add i 8 get w add 83 i +end +put w 12 and get w 73 4294967295 +put w 13 and shr get w 73 32 4294967295 +put w 14 get w 72 +put w 15 get w 74 +for r ulong 7 +round w +permute w +end +for i ulong 8 +put w add 56 i xor get w i get w add i 8 +put w add 64 i xor get w add i 8 get w add 48 i +end +end + +note compress, then the node chaining value is the first 8 output words +fn nodecv w edit ulong 128 blk edit uchar 64 to void +compress w blk +for i ulong 8 +put w add 75 i get w add 56 i +end +end + +note copy cnt bytes of b from offset o into blk, zero the rest +fn loadblk blk edit uchar 64 b edit uchar 1049632 o ulong cnt ulong to void +for i ulong 64 +if lt i cnt +put blk i get b add 1048576 add o i +else +put blk i 0 +end +end +end + +fn setnb w edit ulong 128 bl ulong ctr ulong fl ulong to void +put w 72 bl +put w 73 ctr +put w 74 fl +end + +note chunk ctr holding b[0..cnt): every block but the last compressed, the last left set up +fn chunk w edit ulong 128 blk edit uchar 64 b edit uchar 1049632 cnt ulong ctr ulong to void +for i ulong 8 +put w add 48 i get w add 83 i +end +nblk = ulong 1 +if gt cnt 0 +nblk = div add cnt 63 64 +end +k = ulong 0 +while lt add k 1 nblk bound 16 +loadblk blk b mul k 64 64 +fl = ulong 0 +if eq k 0 +fl = 1 +end +setnb w 64 ctr fl +compress w blk +for i ulong 8 +put w add 48 i get w add 56 i +end +k = add k 1 +end +bo = mul sub nblk 1 64 +loadblk blk b bo sub cnt bo +fl = ulong 2 +if eq nblk 1 +fl = 3 +end +setnb w sub cnt bo ctr fl +end + +note parent node of stack row r (left) and the node chaining value (right), set up to compress +fn parentn w edit ulong 128 blk edit uchar 64 st edit ulong 65536 r ulong to void +for i ulong 8 +put w add 48 i get w add 83 i +end +for i ulong 8 +l = get st add 512 add mul r 8 i +v = get w add 75 i +for k ulong 4 +sh = mul k 8 +q = add mul i 4 k +put blk q uchar and shr l sh 255 +put blk add 32 q uchar and shr v sh 255 +end +end +setnb w 64 0 4 +end + +fn push w edit ulong 128 st edit ulong 65536 to void +p = get st 952 +for i ulong 8 +put st add 512 add mul p 8 i get w add 75 i +end +put st 952 add p 1 +end + +note a whole chunk that is not the last: merge completed subtrees, then push its chaining value +fn midchk w edit ulong 128 blk edit uchar 64 st edit ulong 65536 b edit uchar 1049632 to void +ctr = get st 953 +chunk w blk b 1024 ctr +nodecv w blk +tot = add ctr 1 +while eq and tot 1 0 bound 64 +d = sub get st 952 1 +put st 952 d +parentn w blk st d +nodecv w blk +tot = shr tot 1 +end +push w st +put st 953 add ctr 1 +end + +note the last chunk: fold the stack into the root and write its 32 little-endian bytes into b at 1049600 +fn lastchk w edit ulong 128 blk edit uchar 64 st edit ulong 65536 b edit uchar 1049632 n ulong to void +chunk w blk b n get st 953 +while gt get st 952 0 bound 64 +nodecv w blk +d = sub get st 952 1 +put st 952 d +parentn w blk st d +end +put w 74 or get w 74 8 +compress w blk +outle w b +end + +note the first 8 output words, little endian, into o at 1049600 (the id slot) +fn outle w edit ulong 128 o edit uchar 1049632 to void +for i ulong 8 +v = get w add 56 i +for k ulong 4 +q = add mul i 4 k +put o add 1049600 q uchar and shr v mul k 8 255 +end +end +end + +note ---- name table: the strings the C compares against, packed 8 bytes per word +fn nminit wk edit ulong 65536 to void +note 0 schema +put wk 128 6 +put wk 129 107122480669555 +note 1 version +put wk 133 7 +put wk 134 31084746137298294 +note 2 id +put wk 138 2 +put wk 139 25705 +note 3 kind +put wk 143 4 +put wk 144 1684957547 +note 4 tier +put wk 148 4 +put wk 149 1919248756 +note 5 result +put wk 153 6 +put wk 154 128009175786866 +note 6 timestamp +put wk 158 9 +put wk 159 7881708861846808948 +put wk 160 112 +note 7 repo +put wk 163 4 +put wk 164 1869636978 +note 8 commit +put wk 168 6 +put wk 169 127996156276579 +note 9 dirty +put wk 173 5 +put wk 174 521644697956 +note 10 toolchain +put wk 178 9 +put wk 179 7593465222953660276 +put wk 180 110 +note 11 procedure +put wk 183 9 +put wk 184 8247608679221654128 +put wk 185 101 +note 12 machine +put wk 188 7 +put wk 189 28550371649741165 +note 13 env_class +put wk 193 9 +put wk 194 8314045561161805413 +put wk 195 115 +note 14 input_artifacts +put wk 198 15 +put wk 199 8241973746855865961 +put wk 200 32497592506411380 +note 15 output_artifacts +put wk 203 16 +put wk 204 7016454792213198191 +put wk 205 8319383681641313394 +note 16 assertions +put wk 208 10 +put wk 209 8028075845441778529 +put wk 210 29550 +note 17 dependencies +put wk 213 12 +put wk 214 7954874742130894180 +put wk 215 1936025955 +note 18 declared_mutation +put wk 218 17 +put wk 219 7234314139196417380 +put wk 220 8028075772679122271 +put wk 221 110 +note 19 observed_mutation +put wk 223 17 +put wk 224 7234318610140979823 +put wk 225 8028075772679122271 +put wk 226 110 +note 20 authority +put wk 228 9 +put wk 229 8388361603773658465 +put wk 230 121 +note 21 output_digest +put wk 233 13 +put wk 234 7232627574326981999 +put wk 235 500152231785 +note 22 external_refs +put wk 238 13 +put wk 239 7809644666444609637 +put wk 240 495639163487 +note 23 ledger +put wk 243 6 +put wk 244 125779851896172 +note 24 lease +put wk 248 5 +put wk 249 435727459692 +note 25 required_features +put wk 253 17 +put wk 254 7234314173708068210 +put wk 255 7310034287886427743 +put wk 256 115 +note 26 reserved +put wk 258 8 +put wk 259 7234318610140980594 +note 27 id +put wk 263 2 +put wk 264 25705 +note 28 expected +put wk 268 8 +put wk 269 7234316346693023845 +note 29 observed +put wk 273 8 +put wk 274 7234318610140979823 +note 30 pass +put wk 278 4 +put wk 279 1936941424 +note 31 source +put wk 283 6 +put wk 284 111477796466547 +note 32 note +put wk 288 4 +put wk 289 1702129518 +note 33 index +put wk 293 5 +put wk 294 517097156201 +note 34 hash +put wk 298 4 +put wk 299 1752392040 +note 35 hold_id +put wk 303 7 +put wk 304 28263356098703208 +note 36 resource +put wk 308 8 +put wk 309 7305808869231650162 +note 37 TEST_ONLY_TRUST +put wk 313 15 +put wk 314 5498419465921185108 +put wk 315 23735523962281817 +note 38 HOST_TEST +put wk 318 9 +put wk 319 6000294846954426184 +put wk 320 84 +note 39 QEMU +put wk 323 4 +put wk 324 1431127377 +note 40 QEMU_SECURITY +put wk 328 13 +put wk 329 4847372242852332881 +put wk 330 383666180693 +note 41 MACHINE1_READ_ONLY +put wk 333 18 +put wk 334 3550329957903450445 +put wk 335 5642833604875997791 +put wk 336 22860 +note 42 MACHINE1_ATTENDED +put wk 338 17 +put wk 339 3550329957903450445 +put wk 340 4991200346707870047 +put wk 341 68 +note 43 MACHINE1_MUTATING +put wk 343 17 +put wk 344 3550329957903450445 +put wk 345 5641132647822740831 +put wk 346 71 +note 44 PRODUCTION +put wk 348 10 +put wk 349 5283922296309764688 +put wk 350 20047 +note 45 PASS +put wk 353 4 +put wk 354 1397965136 +note 46 FAIL +put wk 358 4 +put wk 359 1279869254 +note 47 BLOCKED +put wk 363 7 +put wk 364 19216487970458690 +note 48 INCOMPLETE +put wk 368 10 +put wk 369 4993454379819552329 +put wk 370 17748 +note 49 SKIPPED +put wk 373 7 +put wk 374 19216509663005523 +note 50 NONE +put wk 378 4 +put wk 379 1162760014 +note 51 VOLATILE_ONLY +put wk 383 13 +put wk 384 4993446713068638038 +put wk 385 383532289887 +note 52 REMOVABLE_MEDIA_ONLY +put wk 388 20 +put wk 389 5495026334298883410 +put wk 390 6863847864658648901 +put wk 391 1498173007 +note 53 ONE_TIME_BOOT_SELECTION +put wk 393 23 +put wk 394 4993728188548206159 +put wk 395 4995441227396498015 +put wk 396 22042224548332876 +note 54 BOUNDED_TEST_REGION_WRITE +put wk 398 25 +put wk 399 6864687891693784898 +put wk 400 5135601519476032852 +put wk 401 6073476107700424521 +put wk 402 69 +note 55 BOOT_CONFIGURATION_CHANGE +put wk 403 25 +put wk 404 5642802934833958722 +put wk 405 5283920084686293318 +put wk 406 5138116003500084815 +put wk 407 69 +note 56 TRUST_ROOT_CHANGE +put wk 408 17 +put wk 409 5715735692866572884 +put wk 410 5138116003500086351 +put wk 411 69 +note 57 TPM_POLICY_CHANGE +put wk 413 17 +put wk 414 5281683669613629524 +put wk 415 5138116003500087619 +put wk 416 69 +note 58 DESTRUCTIVE_STORAGE +put wk 418 19 +put wk 419 6071790534715983172 +put wk 420 5931051951327630921 +put wk 421 4540225 +note 59 EvidenceReceiptV1 +put wk 423 17 +put wk 424 7305804402280461893 +put wk 425 6229727782535128402 +put wk 426 49 +note 60 v1-base +put wk 428 7 +put wk 429 28555834743927158 +note 61 AIEN_EVIDENCE_RECEIPT_V1 +put wk 433 24 +put wk 434 5284487488403228993 +put wk 435 4995159687793886532 +put wk 436 3555133771584456003 +note 62 blake3 +put wk 438 6 +put wk 439 56510686260322 +note 63 sha256 +put wk 443 6 +put wk 444 59602106411123 +note 64 sha512 +put wk 448 6 +put wk 449 55186930362483 +end + +fn fail wk edit ulong 65536 code ulong pos ulong to long +put wk 0 code +put wk 2 pos +ret -1 +end + +fn failk wk edit ulong 65536 code ulong key ulong to long +put wk 0 code +put wk 1 key +ret -1 +end + +fn hexv c ulong to ulong +if and ge c 48 le c 57 +ret sub c 48 +end +if and ge c 97 le c 102 +ret sub c 87 +end +if and ge c 65 le c 70 +ret sub c 55 +end +ret 16 +end + +fn isws b uchar to bool +ret or or eq b 32 eq b 9 or eq b 10 eq b 13 +end + +fn isdig b uchar to bool +ret and ge b 48 le b 57 +end + +fn umin a ulong b ulong to ulong +if lt a b +ret a +end +ret b +end + +note ---- string decoder (input already validated by pass 1); cursor c: 0 next byte, 1 pending count, +note ---- 2..4 pending UTF-8 bytes, 5 done. dnext gives a byte, or 256 at the closing quote. +fn dinit c edit ulong 8 so ulong to void +put c 0 so +put c 1 0 +put c 5 0 +if eq so 0 +put c 5 1 +end +end + +fn hex4v js view uchar 4194304 p ulong to ulong +v = hexv ulong get js p +v = or shl v 4 hexv ulong get js add p 1 +v = or shl v 4 hexv ulong get js add p 2 +v = or shl v 4 hexv ulong get js add p 3 +ret v +end + +fn descv e uchar to ulong +if eq e 98 +ret 8 +end +if eq e 102 +ret 12 +end +if eq e 110 +ret 10 +end +if eq e 114 +ret 13 +end +if eq e 116 +ret 9 +end +ret ulong e +end + +fn dutf c edit ulong 8 cp ulong to ulong +if lt cp 128 +ret cp +end +if lt cp 2048 +put c 2 or 128 and cp 63 +put c 1 1 +ret or 192 shr cp 6 +end +if lt cp 65536 +put c 2 or 128 and shr cp 6 63 +put c 3 or 128 and cp 63 +put c 1 2 +ret or 224 shr cp 12 +end +put c 2 or 128 and shr cp 12 63 +put c 3 or 128 and shr cp 6 63 +put c 4 or 128 and cp 63 +put c 1 3 +ret or 240 shr cp 18 +end + +fn dunic js view uchar 4194304 c edit ulong 8 to ulong +p = get c 0 +cp = hex4v js p +put c 0 add p 4 +if and ge cp 55296 le cp 56319 +lo = hex4v js add p 6 +put c 0 add p 10 +cp = add 65536 add shl sub cp 55296 10 sub lo 56320 +end +ret dutf c cp +end + +fn dnext js view uchar 4194304 c edit ulong 8 to ulong +k = get c 1 +if gt k 0 +v = get c 2 +put c 2 get c 3 +put c 3 get c 4 +put c 1 sub k 1 +ret v +end +if eq get c 5 1 +ret 256 +end +p = get c 0 +b = get js p +if eq b 34 +put c 5 1 +ret 256 +end +if ne b 92 +put c 0 add p 1 +ret ulong b +end +e = get js add p 1 +put c 0 add p 2 +if eq e 117 +ret dunic js c +end +ret descv e +end + +note strcmp order: the end of a string sorts before every byte +fn dkey v ulong to ulong +if eq v 256 +ret 0 +end +ret add v 1 +end + +note strcmp of the decoded strings at content offsets x and y: -1, 0, 1 +fn dcmp js view uchar 4194304 x ulong y ulong to long +ca = new ulong 8 0 +cb = new ulong 8 0 +dinit ca x +dinit cb y +for o ulong 16 +k = ulong 0 +while lt k 500000 bound 999999 +u = dkey dnext js ca +v = dkey dnext js cb +if ne u v +if lt u v +ret -1 +end +ret 1 +end +if eq u 0 +ret 0 +end +k = add k 1 +end +end +trap 4 +end + +note byte i of name-table entry whose length word is at wk b +fn nmbyte wk edit ulong 65536 b ulong i ulong to ulong +w = get wk add add b 1 shr i 3 +sh = shl and i 7 3 +ret and shr w sh 255 +end + +note decoded string at so equals name-table entry k +fn nameq js view uchar 4194304 wk edit ulong 65536 so ulong k ulong to bool +c = new ulong 8 0 +dinit c so +b = add 128 mul k 5 +nl = get wk b +i = ulong 0 +while true bound 64 +v = dnext js c +if eq v 256 +ret eq i nl +end +if ge i nl +ret false +end +if ne v nmbyte wk b i +ret false +end +i = add i 1 +end +ret false +end + +note the raw bytes s[0..n) equal name-table entry k (tier lookup by C string) +fn rawname s view uchar 4194304 n ulong wk edit ulong 65536 k ulong to bool +b = add 128 mul k 5 +if ne n get wk b +ret false +end +for i ulong n +if ne ulong get s i nmbyte wk b i +ret false +end +end +ret true +end + +note statistics of one decoded string: 0 length, 1 index of the first ':', 2 non-hex bytes, +note 3 non-hex bytes after the first ':', 4 bytes outside [0-9a-f], 5 bytes that are not JSON +note whitespace, 6 first 8 bytes packed, 7 last 8 bytes lowercased, 8 holds test-only/test_only/testonly, +note 10 a ':' was seen +fn sstep a edit ulong 16 v ulong to void +i = get a 0 +if eq hexv v 16 +put a 2 add get a 2 1 +if eq get a 10 1 +put a 3 add get a 3 1 +end +end +if not or and ge v 48 le v 57 and ge v 97 le v 102 +put a 4 add get a 4 1 +end +if not or or eq v 32 eq v 9 or eq v 10 eq v 13 +put a 5 add get a 5 1 +end +if lt i 8 +put a 6 or get a 6 shl v shl i 3 +end +if and eq v 58 eq get a 10 0 +put a 10 1 +put a 1 i +end +lc = v +if and ge v 65 le v 90 +lc = add v 32 +end +top = shr get a 7 56 +w = or shl get a 7 8 lc +put a 7 w +if eq w 8387236824970325113 +put a 8 1 +end +if and eq top 116 or eq w 7310314358644305017 eq w 7310314573392669817 +put a 8 1 +end +put a 0 add i 1 +end + +fn sstat js view uchar 4194304 so ulong a edit ulong 16 to void +for i ulong 16 +put a i 0 +end +c = new ulong 8 0 +dinit c so +for o ulong 16 +k = ulong 0 +while lt k 500000 bound 999999 +v = dnext js c +if eq v 256 +ret +end +sstep a v +k = add k 1 +end +end +trap 4 +end + +note raw "" is the only JSON string that decodes empty (\u0000 is refused) +fn isempty js view uchar 4194304 so ulong to bool +if eq so 0 +ret true +end +ret eq get js so 34 +end + +fn hex32ok js view uchar 4194304 so ulong to bool +a = new ulong 16 0 +sstat js so a +ret and eq get a 0 64 eq get a 2 0 +end + +note ---- pass 1: the JSON syntax of the C reader (jvalue/jcontainer/jstring), iterative, same errors +note ---- and byte offsets. Codes 2..19 are its messages; nesting deeper than 6 is refused. +fn skipws js view uchar 4194304 n ulong p ulong to ulong +for o ulong 16 +k = ulong 0 +while lt k 500000 bound 999999 +if ge p n +ret p +end +if not isws get js p +ret p +end +p = add p 1 +k = add k 1 +end +end +ret p +end + +fn iscont js view uchar 4194304 p ulong to bool +ret eq and get js p 192 128 +end + +fn u3cp js view uchar 4194304 p ulong to ulong +a = shl and ulong get js p 15 12 +b = shl and ulong get js add p 1 63 6 +ret or or a b and ulong get js add p 2 63 +end + +fn u4cp js view uchar 4194304 p ulong to ulong +a = shl and ulong get js p 7 18 +b = shl and ulong get js add p 1 63 12 +c = shl and ulong get js add p 2 63 6 +ret or or a b or c and ulong get js add p 3 63 +end + +note utf8_len: bytes of one valid UTF-8 sequence at p, 0 when invalid +fn utf8l js view uchar 4194304 n ulong p ulong to ulong +s = ulong get js p +if lt s 128 +ret 1 +end +av = sub n p +if and and ge s 194 le s 223 ge av 2 +if iscont js add p 1 +ret 2 +end +end +if and and ge s 224 le s 239 ge av 3 +if and iscont js add p 1 iscont js add p 2 +cp = u3cp js p +if and ge cp 2048 not and ge cp 55296 le cp 57343 +ret 3 +end +end +end +if and and ge s 240 le s 244 ge av 4 +if and and iscont js add p 1 iscont js add p 2 iscont js add p 3 +cp = u4cp js p +if and ge cp 65536 le cp 1114111 +ret 4 +end +end +end +ret 0 +end + +note hex4 with its bounds check: the value, or 65536 when bad +fn hex4c js view uchar 4194304 n ulong p ulong to ulong +if gt add p 4 n +ret 65536 +end +v = ulong 0 +for k ulong 4 +h = hexv ulong get js add p k +if eq h 16 +ret 65536 +end +v = or shl v 4 h +end +ret v +end + +note \u escape, p just past the u: new offset or -1 +fn junic js view uchar 4194304 n ulong wk edit ulong 65536 p ulong to long +cp = hex4c js n p +if eq cp 65536 +ret fail wk 5 p +end +p = add p 4 +if and ge cp 55296 le cp 56319 +if gt add p 2 n +ret fail wk 6 p +end +if or ne get js p 92 ne get js add p 1 117 +ret fail wk 6 p +end +p = add p 2 +lo = hex4c js n p +if eq lo 65536 +ret fail wk 7 p +end +if or lt lo 56320 gt lo 57343 +ret fail wk 7 add p 4 +end +ret long add p 4 +end +if and ge cp 56320 le cp 57343 +ret fail wk 6 p +end +if eq cp 0 +ret fail wk 8 p +end +ret long p +end + +fn simpesc e uchar to bool +x = or or eq e 34 eq e 92 or eq e 47 eq e 98 +y = or or eq e 102 eq e 110 or eq e 114 eq e 116 +ret or x y +end + +note escape, p just past the backslash: new offset or -1 +fn jesc js view uchar 4194304 n ulong wk edit ulong 65536 p ulong to long +if ge p n +ret fail wk 4 p +end +e = get js p +p = add p 1 +if simpesc e +ret long p +end +if ne e 117 +ret fail wk 4 p +end +ret junic js n wk p +end + +note jstring, p just past the opening quote: offset after the closing quote, or -1 +fn jstr js view uchar 4194304 n ulong wk edit ulong 65536 p ulong to long +for o ulong 16 +k = ulong 0 +while lt k 500000 bound 999999 +if ge p n +ret fail wk 2 p +end +c = get js p +if eq c 34 +ret long add p 1 +end +if lt c 32 +ret fail wk 3 p +end +if eq c 92 +r = jesc js n wk add p 1 +if lt r 0 +ret r +end +p = ulong r +else +l = utf8l js n p +if eq l 0 +ret fail wk 9 p +end +p = add p l +end +k = add k 1 +end +end +trap 4 +end + +fn skipdig js view uchar 4194304 n ulong p ulong to ulong +for o ulong 16 +k = ulong 0 +while lt k 500000 bound 999999 +if ge p n +ret p +end +if not isdig get js p +ret p +end +p = add p 1 +k = add k 1 +end +end +ret p +end + +fn isnx c uchar to bool +x = or isdig c or eq c 43 eq c 45 +y = or or eq c 101 eq c 69 eq c 46 +ret or x y +end + +note the C number tail after '.', 'e' or 'E': digits + - e E . +fn skipnx js view uchar 4194304 n ulong p ulong to ulong +for o ulong 16 +k = ulong 0 +while lt k 500000 bound 999999 +if ge p n +ret p +end +c = get js p +if not isnx c +ret p +end +p = add p 1 +k = add k 1 +end +end +ret p +end + +fn lit4 js view uchar 4194304 p ulong a uchar b uchar c uchar d uchar to bool +x = and eq get js p a eq get js add p 1 b +y = and eq get js add p 2 c eq get js add p 3 d +ret and x y +end + +note null / true / false at p: the offset after it, 0 when none +fn jlit js view uchar 4194304 n ulong p ulong to ulong +r = sub n p +if ge r 4 +if lit4 js p 110 117 108 108 +ret add p 4 +end +if lit4 js p 116 114 117 101 +ret add p 4 +end +end +if ge r 5 +if lit4 js p 102 97 108 115 +if eq get js add p 4 101 +ret add p 5 +end +end +end +ret 0 +end + +note parser state st: 0 offset, 1 depth, 2 next step (0 value, 1 member, 2 after a value), +note 8+d container kind at depth d (1 object), 16+d content offset of that object's first key +fn jnum js view uchar 4194304 n ulong wk edit ulong 65536 st edit ulong 32 p ulong to long +q = p +if eq get js q 45 +q = add q 1 +end +d0 = q +q = skipdig js n q +if eq q d0 +ret fail wk 16 q +end +if eq get js d0 48 +if gt sub q d0 1 +ret fail wk 17 q +end +end +if lt q n +c = get js q +if or eq c 46 or eq c 101 eq c 69 +q = skipnx js n add q 1 +end +end +put st 0 q +ret 0 +end + +fn jopen js view uchar 4194304 n ulong wk edit ulong 65536 st edit ulong 32 p ulong to long +d = add get st 1 1 +put st 1 d +q = add p 1 +if gt d 6 +ret fail wk 10 q +end +cl = uchar 93 +put st add 8 d 0 +if eq get js p 123 +cl = 125 +put st add 8 d 1 +end +put st add 16 d 0 +q = skipws js n q +if lt q n +if eq get js q cl +put st 0 add q 1 +put st 1 sub d 1 +put st 2 2 +ret 0 +end +end +put st 0 q +put st 2 1 +ret 0 +end + +fn jval js view uchar 4194304 n ulong wk edit ulong 65536 st edit ulong 32 to long +p = skipws js n get st 0 +if ge p n +ret fail wk 15 p +end +c = get js p +if or eq c 123 eq c 91 +ret jopen js n wk st p +end +put st 2 2 +if eq c 34 +r = jstr js n wk add p 1 +if lt r 0 +ret r +end +put st 0 ulong r +ret 0 +end +q = jlit js n p +if gt q 0 +put st 0 q +ret 0 +end +if or eq c 45 isdig c +ret jnum js n wk st p +end +ret fail wk 18 p +end + +note ---- skipping over text pass 1 has already accepted +fn skipstr js view uchar 4194304 q ulong to ulong +for o ulong 16 +k = ulong 0 +while lt k 500000 bound 999999 +b = get js q +if eq b 34 +ret add q 1 +end +if eq b 92 +q = add q 2 +else +q = add q 1 +end +k = add k 1 +end +end +trap 4 +end + +fn skipbox js view uchar 4194304 q ulong to ulong +d = ulong 0 +for o ulong 16 +k = ulong 0 +while lt k 500000 bound 999999 +b = get js q +if eq b 34 +q = skipstr js add q 1 +else +if or eq b 123 eq b 91 +d = add d 1 +end +if or eq b 125 eq b 93 +d = sub d 1 +if eq d 0 +ret add q 1 +end +end +q = add q 1 +end +k = add k 1 +end +end +trap 4 +end + +fn skipval js view uchar 4194304 n ulong v ulong to ulong +c = get js v +if eq c 34 +ret skipstr js add v 1 +end +if or eq c 123 eq c 91 +ret skipbox js v +end +ret skipsc js n v +end + +note a number or literal: up to the next whitespace, ',', ']' or '}' +fn skipsc js view uchar 4194304 n ulong v ulong to ulong +q = v +for o ulong 16 +k = ulong 0 +while lt k 500000 bound 999999 +if ge q n +ret q +end +c = get js q +if or or isws c eq c 44 or eq c 93 eq c 125 +ret q +end +q = add q 1 +k = add k 1 +end +end +ret q +end + +note content offset of the key after the key at content offset q (both in one object) +fn nextkey js view uchar 4194304 n ulong q ulong to ulong +e = skipstr js q +e = skipws js n e +e = skipws js n add e 1 +e = skipval js n e +e = skipws js n e +e = skipws js n add e 1 +ret add e 1 +end + +note an earlier key of the object (first key at content offset f) equals the key at k +fn dupkey js view uchar 4194304 n ulong f ulong k ulong to bool +q = f +for o ulong 16 +i = ulong 0 +while lt i 500000 bound 999999 +if eq q k +ret false +end +if eq dcmp js q k 0 +ret true +end +q = nextkey js n q +i = add i 1 +end +end +trap 4 +end + +fn jmem js view uchar 4194304 n ulong wk edit ulong 65536 st edit ulong 32 to long +p = skipws js n get st 0 +d = get st 1 +if eq get st add 8 d 0 +put st 0 p +put st 2 0 +ret 0 +end +if ge p n +ret fail wk 11 p +end +if ne get js p 34 +ret fail wk 11 p +end +k = add p 1 +r = jstr js n wk k +if lt r 0 +ret r +end +e = ulong r +f = get st add 16 d +if eq f 0 +put st add 16 d k +else +if dupkey js n f k +ret fail wk 12 e +end +end +q = skipws js n e +if ge q n +ret fail wk 13 q +end +if ne get js q 58 +ret fail wk 13 q +end +put st 0 add q 1 +put st 2 0 +ret 0 +end + +fn jaft js view uchar 4194304 n ulong wk edit ulong 65536 st edit ulong 32 to long +d = get st 1 +if eq d 0 +ret 1 +end +p = skipws js n get st 0 +if lt p n +c = get js p +if eq c 44 +put st 0 add p 1 +put st 2 1 +ret 0 +end +cl = uchar 93 +if eq get st add 8 d 1 +cl = 125 +end +if eq c cl +put st 0 add p 1 +put st 1 sub d 1 +put st 2 2 +ret 0 +end +end +ret fail wk 14 p +end + +fn jcheck js view uchar 4194304 n ulong wk edit ulong 65536 to long +st = new ulong 32 0 +for o ulong 32 +k = ulong 0 +while lt k 500000 bound 999999 +s = get st 2 +r = long 0 +if eq s 0 +r = jval js n wk st +else +if eq s 1 +r = jmem js n wk st +else +r = jaft js n wk st +end +end +if lt r 0 +ret r +end +if eq r 1 +p = skipws js n get st 0 +if ne p n +ret fail wk 19 p +end +ret 0 +end +k = add k 1 +end +end +trap 4 +end + +note ---- pass 2: walking validated containers +fn objfirst js view uchar 4194304 n ulong o ulong to ulong +q = skipws js n add o 1 +if eq get js q 125 +ret 0 +end +ret q +end + +note value offset of the member whose key quote is at q +fn keyvalp js view uchar 4194304 n ulong q ulong to ulong +e = skipstr js add q 1 +e = skipws js n e +ret skipws js n add e 1 +end + +fn objnext js view uchar 4194304 n ulong q ulong to ulong +e = skipval js n keyvalp js n q +e = skipws js n e +if eq get js e 44 +ret skipws js n add e 1 +end +ret 0 +end + +fn arrfirst js view uchar 4194304 n ulong o ulong to ulong +q = skipws js n add o 1 +if eq get js q 93 +ret 0 +end +ret q +end + +fn arrnext js view uchar 4194304 n ulong v ulong to ulong +e = skipws js n skipval js n v +if eq get js e 44 +ret skipws js n add e 1 +end +ret 0 +end + +note index of the name in [lo, hi) equal to the key whose quote is at q, 999 when none +fn keyidx js view uchar 4194304 wk edit ulong 65536 q ulong lo ulong hi ulong to ulong +k = lo +while lt k hi bound 64 +if nameq js wk add q 1 k +ret k +end +k = add k 1 +end +ret 999 +end + +note number of array elements at v, stopping once past 4096 +fn arrcnt js view uchar 4194304 n ulong v ulong to ulong +m = ulong 0 +e = arrfirst js n v +while gt e 0 bound 4098 +m = add m 1 +if gt m 4096 +ret m +end +e = arrnext js n e +end +ret m +end + +fn topscan js view uchar 4194304 n ulong wk edit ulong 65536 to long +r = get wk 4 +if ne get js r 123 +ret failk wk 21 0 +end +for k ulong 27 +put wk add 16 k 0 +end +q = objfirst js n r +while gt q 0 bound 64 +k = keyidx js wk q 0 27 +if eq k 999 +ret failk wk 22 0 +end +put wk add 16 k keyvalp js n q +q = objnext js n q +end +ret 0 +end + +note ---- the C want_* readers +fn wstr js view uchar 4194304 wk edit ulong 65536 v ulong key ulong req bool slot ulong to long +if eq v 0 +if req +ret failk wk 23 key +end +put wk slot 0 +ret 0 +end +if ne get js v 34 +ret failk wk 24 key +end +put wk slot add v 1 +ret 0 +end + +fn wbool js view uchar 4194304 wk edit ulong 65536 v ulong key ulong slot ulong to long +if eq v 0 +ret failk wk 23 key +end +c = get js v +if and ne c 116 ne c 102 +ret failk wk 28 key +end +put wk slot 0 +if eq c 116 +put wk slot 1 +end +ret 0 +end + +fn vnum c uchar to bool +ret not or or or eq c 34 eq c 123 or eq c 91 eq c 110 or eq c 116 eq c 102 +end + +note want_u64: a plain number (no sign, fraction or exponent) of at most 20 digits below 2^64 +fn wu64 js view uchar 4194304 n ulong wk edit ulong 65536 v ulong key ulong slot ulong to long +if eq v 0 +ret failk wk 23 key +end +c = get js v +if or not vnum c eq c 45 +ret failk wk 29 key +end +q = v +while lt q n bound 64 +if or not isdig get js q gt sub q v 21 +break +end +q = add q 1 +end +if lt q n +c = get js q +if or eq c 46 or eq c 101 eq c 69 +ret failk wk 29 key +end +end +if gt sub q v 20 +ret failk wk 29 key +end +x = ulong 0 +i = v +while lt i q bound 64 +dg = ulong sub get js i 48 +if gt x div sub 18446744073709551615 dg 10 +ret failk wk 29 key +end +x = add mul x 10 dg +i = add i 1 +end +put wk slot x +ret 0 +end + +note want_strlist into list j (key names it); external_refs and required_features may be absent +fn wlist js view uchar 4194304 n ulong wk edit ulong 65536 v ulong key ulong j ulong to long +put wk add 114 j 0 +if eq v 0 +if or eq key 22 eq key 25 +ret 0 +end +ret failk wk 23 key +end +if ne get js v 91 +ret failk wk 25 key +end +m = arrcnt js n v +if gt m 4096 +ret failk wk 26 key +end +b = add 1024 mul j 4096 +i = ulong 0 +e = arrfirst js n v +while gt e 0 bound 4097 +if ne get js e 34 +ret failk wk 27 key +end +put wk add b i add e 1 +i = add i 1 +e = arrnext js n e +end +put wk add 114 j m +ret 0 +end + +note want_mut: the mutation's table index into slot +fn wmut js view uchar 4194304 wk edit ulong 65536 v ulong key ulong slot ulong to long +r = wstr js wk v key true slot +if lt r 0 +ret r +end +so = get wk slot +k = ulong 50 +while lt k 59 bound 16 +if nameq js wk so k +put wk slot sub k 50 +ret 0 +end +k = add k 1 +end +ret failk wk 30 key +end + +note the C want chain, in its order +fn fields1 js view uchar 4194304 n ulong wk edit ulong 65536 to long +r = wstr js wk get wk 16 0 true 64 +if lt r 0 +ret r +end +r = wu64 js n wk get wk 17 1 100 +if lt r 0 +ret r +end +for k ulong 4 +r = wstr js wk get wk add 18 k add 2 k true add 66 k +if lt r 0 +ret r +end +end +r = wu64 js n wk get wk 22 6 101 +if lt r 0 +ret r +end +r = wstr js wk get wk 23 7 true 71 +if lt r 0 +ret r +end +r = wstr js wk get wk 24 8 true 72 +if lt r 0 +ret r +end +r = wbool js wk get wk 25 9 102 +if lt r 0 +ret r +end +for k ulong 4 +r = wstr js wk get wk add 26 k add 10 k true add 74 k +if lt r 0 +ret r +end +end +r = wlist js n wk get wk 30 14 0 +if lt r 0 +ret r +end +r = wlist js n wk get wk 31 15 1 +if lt r 0 +ret r +end +r = wlist js n wk get wk 33 17 2 +if lt r 0 +ret r +end +ret 0 +end + +fn fields2 js view uchar 4194304 n ulong wk edit ulong 65536 to long +r = wmut js wk get wk 34 18 105 +if lt r 0 +ret r +end +r = wmut js wk get wk 35 19 106 +if lt r 0 +ret r +end +r = wstr js wk get wk 36 20 false 84 +if lt r 0 +ret r +end +r = wstr js wk get wk 37 21 true 85 +if lt r 0 +ret r +end +r = wlist js n wk get wk 38 22 3 +if lt r 0 +ret r +end +r = wlist js n wk get wk 41 25 4 +if lt r 0 +ret r +end +ret wstr js wk get wk 42 26 false 90 +end + +note name-table index in [lo, hi) equal to the decoded string at so, 999 when none +fn nameidx js view uchar 4194304 wk edit ulong 65536 so ulong lo ulong hi ulong to ulong +k = lo +while lt k hi bound 64 +if nameq js wk so k +ret k +end +k = add k 1 +end +ret 999 +end + +note check_digest_list, the : form (a: sstat of the item) +fn dgcolon wk edit ulong 65536 a edit ulong 16 key ulong to long +p6 = and get a 6 281474976710655 +ok = eq p6 get wk 439 +ok = or ok eq p6 get wk 444 +ok = or ok eq p6 get wk 449 +if not and eq get a 1 6 ok +ret failk wk 44 key +end +hl = sub sub get a 0 get a 1 1 +bad = or lt hl 32 ne and hl 1 0 +if or bad ne get a 3 0 +ret failk wk 45 key +end +ret 0 +end + +note check_digest_list for one item +fn dgchk js view uchar 4194304 wk edit ulong 65536 so ulong key ulong to long +a = new ulong 16 0 +sstat js so a +if eq get a 10 1 +ret dgcolon wk a key +end +if and eq get a 0 64 eq get a 2 0 +ret 0 +end +ret failk wk 46 key +end + +fn dglist js view uchar 4194304 wk edit ulong 65536 j ulong key ulong to long +b = add 1024 mul j 4096 +for i ulong get wk add 114 j +r = dgchk js wk get wk add b i key +if lt r 0 +ret r +end +end +ret 0 +end + +note the canonical_bytes validations up to the id, in the C order +fn checks1 js view uchar 4194304 wk edit ulong 65536 to long +if gt get wk 100 4294967295 +ret fail wk 31 0 +end +if not nameq js wk get wk 64 59 +ret fail wk 32 0 +end +if ne get wk 100 1 +ret fail wk 33 0 +end +for i ulong get wk 118 +if not nameq js wk get wk add 17408 i 60 +ret fail wk 34 0 +end +end +if not isempty js get wk 90 +ret fail wk 35 0 +end +if isempty js get wk 67 +ret fail wk 36 0 +end +if isempty js get wk 71 +ret fail wk 37 0 +end +a = new ulong 16 0 +sstat js get wk 72 a +if not and or eq get a 0 40 eq get a 0 64 eq get a 4 0 +ret fail wk 38 0 +end +if isempty js get wk 75 +ret fail wk 39 0 +end +if isempty js get wk 76 +ret fail wk 40 0 +end +ret 0 +end + +fn checks2 js view uchar 4194304 wk edit ulong 65536 to long +t = nameidx js wk get wk 68 37 45 +if eq t 999 +ret fail wk 41 0 +end +put wk 104 sub t 37 +t = nameidx js wk get wk 69 45 50 +if eq t 999 +ret fail wk 42 0 +end +put wk 103 sub t 45 +if ne dcmp js get wk 77 get wk 68 0 +ret fail wk 43 0 +end +r = dglist js wk 0 14 +if lt r 0 +ret r +end +r = dglist js wk 1 15 +if lt r 0 +ret r +end +if not hex32ok js get wk 85 +ret fail wk 47 0 +end +if not hex32ok js get wk 66 +ret fail wk 48 0 +end +ret 0 +end + +note one assertion object at e, index i +fn asone js view uchar 4194304 n ulong wk edit ulong 65536 e ulong i ulong to long +if ne get js e 123 +ret fail wk 50 0 +end +t = new ulong 8 0 +q = objfirst js n e +while gt q 0 bound 16 +k = keyidx js wk q 27 33 +if eq k 999 +ret fail wk 51 0 +end +put t sub k 27 keyvalp js n q +q = objnext js n q +end +b = add 21504 mul i 6 +r = wstr js wk get t 0 27 true b +if lt r 0 +ret r +end +r = wstr js wk get t 1 28 false add b 1 +if lt r 0 +ret r +end +r = wstr js wk get t 2 29 false add b 2 +if lt r 0 +ret r +end +r = wbool js wk get t 3 30 add b 3 +if lt r 0 +ret r +end +r = wstr js wk get t 4 31 false add b 4 +if lt r 0 +ret r +end +ret wstr js wk get t 5 32 false add b 5 +end + +fn asall js view uchar 4194304 n ulong wk edit ulong 65536 to long +put wk 113 0 +v = get wk 32 +if eq v 0 +ret failk wk 23 16 +end +if ne get js v 91 +ret fail wk 49 0 +end +m = arrcnt js n v +if gt m 4096 +ret fail wk 49 0 +end +i = ulong 0 +e = arrfirst js n v +while gt e 0 bound 4097 +r = asone js n wk e i +if lt r 0 +ret r +end +i = add i 1 +e = arrnext js n e +end +put wk 113 m +if and eq m 0 eq get wk 103 0 +ret fail wk 52 0 +end +ret asids js wk m +end + +note assertion ids: not empty, no duplicates (each against the earlier ones, as the C) +fn asids js view uchar 4194304 wk edit ulong 65536 m ulong to long +for i ulong m +x = get wk add 21504 mul i 6 +if isempty js x +ret fail wk 53 0 +end +for k ulong i +y = get wk add 21504 mul k 6 +if eq dcmp js y x 0 +ret fail wk 54 0 +end +end +end +ret 0 +end + +fn sev k ulong to ulong +if le k 6 +ret k +end +ret sub k 1 +end + +fn checks3 js view uchar 4194304 wk edit ulong 65536 to long +a = new ulong 16 0 +for i ulong get wk 116 +if not hex32ok js get wk add 9216 i +ret fail wk 55 0 +end +end +so = sev get wk 106 +if gt so sev get wk 105 +ret fail wk 56 0 +end +rk = get wk 104 +mx = ulong 4 +if eq rk 4 +mx = 2 +end +if eq rk 5 +mx = 5 +end +if ge rk 6 +mx = 255 +end +if gt so mx +ret fail wk 57 0 +end +if and eq rk 4 ge get wk 106 4 +ret fail wk 58 0 +end +if eq rk 7 +sstat js get wk 84 a +if eq get a 5 0 +ret fail wk 59 0 +end +if eq get a 8 1 +ret fail wk 60 0 +end +end +ret 0 +end + +fn ledger js view uchar 4194304 n ulong wk edit ulong 65536 to long +put wk 107 0 +v = get wk 39 +if eq v 0 +ret 0 +end +if eq get js v 110 +ret 0 +end +if ne get js v 123 +ret fail wk 61 0 +end +t = new ulong 4 0 +q = objfirst js n v +while gt q 0 bound 8 +k = keyidx js wk q 33 35 +if eq k 999 +ret fail wk 62 0 +end +put t sub k 33 keyvalp js n q +q = objnext js n q +end +r = wu64 js n wk get t 0 33 108 +if lt r 0 +ret r +end +r = wstr js wk get t 1 34 true 109 +if lt r 0 +ret r +end +if not hex32ok js get wk 109 +ret fail wk 63 0 +end +put wk 107 1 +ret 0 +end + +fn lease js view uchar 4194304 n ulong wk edit ulong 65536 to long +put wk 110 0 +v = get wk 40 +if eq v 0 +ret 0 +end +if eq get js v 110 +ret 0 +end +if ne get js v 123 +ret fail wk 64 0 +end +t = new ulong 4 0 +q = objfirst js n v +while gt q 0 bound 8 +k = keyidx js wk q 35 37 +if eq k 999 +ret fail wk 65 0 +end +put t sub k 35 keyvalp js n q +q = objnext js n q +end +r = wstr js wk get t 0 35 true 111 +if lt r 0 +ret r +end +r = wstr js wk get t 1 36 true 112 +if lt r 0 +ret r +end +if not hex32ok js get wk 111 +ret fail wk 66 0 +end +a = new ulong 16 0 +sstat js get wk 112 a +if eq get a 5 0 +ret fail wk 67 0 +end +put wk 110 1 +ret 0 +end + +note omega_receipt_parse: 0 ok, -1 with the reason in wk 0..2 +fn rcparse js view uchar 4194304 n ulong wk edit ulong 65536 to long +put wk 0 0 +put wk 1 0 +put wk 2 0 +put wk 107 0 +put wk 110 0 +put wk 113 0 +nminit wk +if or eq n 0 gt n 4194304 +ret fail wk 20 0 +end +put wk 3 n +r = jcheck js n wk +if lt r 0 +ret r +end +put wk 4 skipws js n 0 +r = topscan js n wk +if lt r 0 +ret r +end +r = fields1 js n wk +if lt r 0 +ret r +end +r = fields2 js n wk +if lt r 0 +ret r +end +r = checks1 js wk +if lt r 0 +ret r +end +r = checks2 js wk +if lt r 0 +ret r +end +r = asall js n wk +if lt r 0 +ret r +end +r = checks3 js wk +if lt r 0 +ret r +end +r = ledger js n wk +if lt r 0 +ret r +end +ret lease js n wk +end + +note ---- canonical bytes, streamed: each byte goes to the window (when inside it) and to BLAKE3 +fn b3mid wk edit ulong 65536 out edit uchar 1049632 to void +w = new ulong 128 0 +blk = new uchar 64 0 +initw w +midchk w blk wk out +end + +fn b3fin wk edit ulong 65536 out edit uchar 1049632 to void +w = new ulong 128 0 +blk = new uchar 64 0 +initw w +lastchk w blk wk out get wk 10 +end + +fn eb wk edit ulong 65536 out edit uchar 1049632 v uchar to void +t = get wk 8 +lo = get wk 9 +if and ge t lo lt t add lo 1048576 +put out sub t lo v +end +f = get wk 10 +if eq f 1024 +b3mid wk out +f = 0 +end +put out add 1048576 f v +put wk 10 add f 1 +put wk 8 add t 1 +end + +fn eu32 wk edit ulong 65536 out edit uchar 1049632 v ulong to void +eb wk out uchar and shr v 24 255 +eb wk out uchar and shr v 16 255 +eb wk out uchar and shr v 8 255 +eb wk out uchar and v 255 +end + +fn eu64 wk edit ulong 65536 out edit uchar 1049632 v ulong to void +eu32 wk out shr v 32 +eu32 wk out and v 4294967295 +end + +fn dlen js view uchar 4194304 so ulong to ulong +a = new ulong 16 0 +sstat js so a +ret get a 0 +end + +note u64 length, then the decoded bytes +fn estr js view uchar 4194304 wk edit ulong 65536 out edit uchar 1049632 so ulong to void +eu64 wk out dlen js so +c = new ulong 8 0 +dinit c so +for o ulong 16 +k = ulong 0 +while lt k 500000 bound 999999 +v = dnext js c +if eq v 256 +ret +end +eb wk out uchar v +k = add k 1 +end +end +trap 4 +end + +note 64 hex digits (any case) as 32 raw bytes +fn ehex js view uchar 4194304 wk edit ulong 65536 out edit uchar 1049632 so ulong to void +c = new ulong 8 0 +dinit c so +for i ulong 32 +h = hexv dnext js c +l = hexv dnext js c +eb wk out uchar or shl h 4 l +end +end + +note sort key of item it: mode wk 7 = 0 the item is a string offset, 1 an assertion index (its id) +fn skey wk edit ulong 65536 it ulong to ulong +if eq get wk 7 0 +ret it +end +ret get wk add 21504 mul it 6 +end + +note take the left run's head next: i in the left run and (j past the right run or key i <= key j) +fn mleft js view uchar 4194304 wk edit ulong 65536 i ulong j ulong mid ulong hi ulong to bool +if ge i mid +ret false +end +if ge j hi +ret true +end +s = get wk 5 +x = skey wk get wk add s i +y = skey wk get wk add s j +ret le dcmp js x y 0 +end + +fn mput wk edit ulong 65536 k ulong from ulong to void +put wk add get wk 6 k get wk add get wk 5 from +end + +note merge wk[src+lo, src+mid) and wk[src+mid, src+hi) into dst (src wk 5, dst wk 6) +fn mrg js view uchar 4194304 wk edit ulong 65536 lo ulong mid ulong hi ulong to void +i = lo +j = mid +k = lo +while lt k hi bound 4097 +if mleft js wk i j mid hi +mput wk k i +i = add i 1 +else +mput wk k j +j = add j 1 +end +k = add k 1 +end +end + +note bottom-up merge sort of wk[46080, 46080 + m) by skey, strcmp order +fn msort js view uchar 4194304 wk edit ulong 65536 m ulong to void +put wk 5 46080 +put wk 6 50176 +w = ulong 1 +while lt w m bound 16 +i = ulong 0 +while lt i m bound 4097 +mid = umin add i w m +hi = umin add i mul 2 w m +mrg js wk i mid hi +i = add i mul 2 w +end +s = get wk 5 +put wk 5 get wk 6 +put wk 6 s +w = mul w 2 +end +if ne get wk 5 46080 +for i ulong m +put wk add 46080 i get wk add 50176 i +end +end +end + +note sorted item i differs from item i - 1 (BTreeSet: equal strings kept once) +fn isnew js view uchar 4194304 wk edit ulong 65536 i ulong to bool +if eq i 0 +ret true +end +x = get wk add 46079 i +y = get wk add 46080 i +ret ne dcmp js x y 0 +end + +note sort list j into the sort area; the number of distinct strings +fn lsort js view uchar 4194304 wk edit ulong 65536 j ulong to ulong +m = get wk add 114 j +b = add 1024 mul j 4096 +for i ulong m +put wk add 46080 i get wk add b i +end +put wk 7 0 +msort js wk m +u = ulong 0 +for i ulong m +if isnew js wk i +u = add u 1 +end +end +ret u +end + +note a sorted string set: u32 count, then each distinct string (hx 1: as 32 raw bytes) +fn eset js view uchar 4194304 wk edit ulong 65536 out edit uchar 1049632 j ulong hx bool to void +u = lsort js wk j +eu32 wk out u +for i ulong get wk add 114 j +if isnew js wk i +if hx +ehex js wk out get wk add 46080 i +else +estr js wk out get wk add 46080 i +end +end +end +end + +fn eassert js view uchar 4194304 wk edit ulong 65536 out edit uchar 1049632 to void +m = get wk 113 +for i ulong m +put wk add 46080 i i +end +put wk 7 1 +msort js wk m +eu32 wk out m +for i ulong m +b = add 21504 mul get wk add 46080 i 6 +estr js wk out get wk b +estr js wk out get wk add b 1 +estr js wk out get wk add b 2 +eb wk out uchar get wk add b 3 +estr js wk out get wk add b 4 +estr js wk out get wk add b 5 +end +end + +fn edomain wk edit ulong 65536 out edit uchar 1049632 to void +for i ulong 24 +eb wk out uchar nmbyte wk 433 i +end +end + +fn ehead js view uchar 4194304 wk edit ulong 65536 out edit uchar 1049632 to void +edomain wk out +eu32 wk out 1 +estr js wk out get wk 67 +estr js wk out get wk 68 +eb wk out uchar get wk 103 +estr js wk out get wk 71 +estr js wk out get wk 72 +eb wk out uchar get wk 102 +estr js wk out get wk 74 +estr js wk out get wk 75 +estr js wk out get wk 76 +estr js wk out get wk 77 +end + +fn etail js view uchar 4194304 wk edit ulong 65536 out edit uchar 1049632 to void +if eq get wk 107 1 +eb wk out 1 +eu64 wk out get wk 108 +ehex js wk out get wk 109 +else +eb wk out 0 +end +if eq get wk 110 1 +eb wk out 1 +ehex js wk out get wk 111 +estr js wk out get wk 112 +else +eb wk out 0 +end +eu64 wk out get wk 101 +eset js wk out 4 false +eu32 wk out 0 +end + +note omega_receipt_canonical + compute_id, after rcparse returned 0 on the same js and wk +fn rccanon js view uchar 4194304 n ulong wk edit ulong 65536 out edit uchar 1049632 pos ulong to ulong +put wk 8 0 +put wk 9 pos +put wk 10 0 +for i ulong 448 +put wk add 512 i 0 +end +ehead js wk out +eset js wk out 0 false +eset js wk out 1 false +eassert js wk out +eset js wk out 2 true +eb wk out uchar get wk 105 +eb wk out uchar get wk 106 +estr js wk out get wk 84 +ehex js wk out get wk 85 +eset js wk out 3 false +etail js wk out +b3fin wk out +ret get wk 8 +end + +note omega_receipt_verify_bytes: parse, recompute, the id field must equal the result (any case) +fn rcverify js view uchar 4194304 n ulong wk edit ulong 65536 out edit uchar 1049632 to long +r = rcparse js n wk +if lt r 0 +ret r +end +t = rccanon js n wk out 0 +c = new ulong 8 0 +dinit c get wk 66 +same = true +for i ulong 32 +h = hexv dnext js c +h = or shl h 4 hexv dnext js c +if ne h ulong get out add 1049600 i +same = false +end +end +if not same +ret fail wk 69 0 +end +ret 0 +end + +note omega_tier_rank of the C string s[0..n) +fn rctier s view uchar 4194304 n ulong wk edit ulong 65536 to long +nminit wk +for k ulong 8 +if rawname s n wk add 37 k +ret long k +end +end +ret -1 +end + +note omega_tier_satisfies (aien-proof chain.rs tier_satisfies) +fn rcsat need long have long to long +if or lt need 0 lt have 0 +ret 0 +end +if eq have 0 +if eq need 0 +ret 1 +end +ret 0 +end +if and ge need 4 lt have 4 +ret 0 +end +if ge have need +ret 1 +end +ret 0 +end + +note ==== parity harness (program mode; not part of any unit: unit cutting stops at this line) +note stdin: records of a 25-byte header (op byte, then length, a, b as 8 decimal digits each) and +note length payload bytes. One output line per record, the same format as the C oracle driver: +note R +note op R: the payload as is. op M: the payload holds the placeholder id (64 '0'): parse it, compute +note the id, write it over the placeholder (b 257: upper case), then apply the mutation (b 1..255: +note xor byte a with b; b 256: truncate to a bytes) and print the R line of the result; when the +note placeholder is absent or the payload does not parse, the R line of the payload. +note op T: T . op S: S then omega_tier_satisfies over -3 -1 0..8. +note Exit 65 on a short record. + +text tmsgs "out of memory\nunterminated string\ncontrol character in string\nbad escape\nbad \\u escape\nlone surrogate\nbad surrogate pair\nNUL in string\ninvalid UTF-8\nnesting too deep\nexpected object key\nduplicate key\nexpected ':'\nexpected ',' or closing bracket\nunexpected end\nbad number\nleading zero\nunexpected character\ntrailing bytes after the receipt\nreceipt: empty or oversize input\nreceipt: top level must be an object\nreceipt: unknown field\nreceipt: missing field %\nreceipt: field % must be a string\nreceipt: field % must be a list\nreceipt: field % is too long\nreceipt: list % must hold strings\nreceipt: field % must be true or false\nreceipt: field % must be an unsigned integer\nreceipt: unknown mutation in %\nreceipt: version out of range\nreceipt: unknown receipt schema\nreceipt: unsupported receipt version\nreceipt: unknown required receipt feature\nreceipt: reserved field must be empty\nreceipt: kind must not be empty\nreceipt: repo must not be empty\nreceipt: commit must be 40 or 64 lowercase hex digits\nreceipt: procedure identity must not be empty\nreceipt: machine identity must not be empty\nreceipt: unknown tier\nreceipt: unknown result\nreceipt: env_class contradicts tier\nreceipt: unknown digest algorithm in %\nreceipt: bad digest hex in %\nreceipt: digest must be : or 64 hex digits in %\nreceipt: output_digest must be 64 hex digits\nreceipt: id must be 64 hex digits\nreceipt: assertions must be a list\nreceipt: an assertion must be an object\nreceipt: unknown assertion field\nreceipt: PASS receipts must carry at least one assertion\nreceipt: assertion id must not be empty\nreceipt: duplicate assertion id\nreceipt: dependency is not a 64 digit receipt hash\nreceipt: observed mutation exceeds declared mutation\nreceipt: tier cannot claim that observed mutation\nreceipt: a read-only run cannot claim that mutation\nreceipt: PRODUCTION receipts require an authority reference\nreceipt: a TEST_ONLY signer cannot produce a PRODUCTION receipt\nreceipt: ledger must be null or an object\nreceipt: unknown ledger field\nreceipt: ledger hash must be 64 hex digits\nreceipt: lease must be null or an object\nreceipt: unknown lease field\nreceipt: lease hold_id must be 64 hex digits\nreceipt: lease resource must not be empty\nreceipt: cannot compute canonical identity\nreceipt id mismatch: the file claims one id, its canonical bytes hash to another\n" +text tkeys "schema\nversion\nid\nkind\ntier\nresult\ntimestamp\nrepo\ncommit\ndirty\ntoolchain\nprocedure\nmachine\nenv_class\ninput_artifacts\noutput_artifacts\nassertions\ndependencies\ndeclared_mutation\nobserved_mutation\nauthority\noutput_digest\nexternal_refs\nledger\nlease\nrequired_features\nreserved\nid\nexpected\nobserved\npass\nsource\nnote\nindex\nhash\nhold_id\nresource\n" +text tjs "receipt JSON: " +text tat " at byte " +text ttab "\t" +text tnl "\n" +text tdash "-" +text tr "R" +text tt "T" +text ts "S" + +fn wkey k ulong to void +s = ulong 0 +ln = ulong 0 +while lt ln k bound 64 +while ne get tkeys s 10 bound 64 +s = add s 1 +end +s = add s 1 +ln = add ln 1 +end +e = s +while ne get tkeys e 10 bound 64 +e = add e 1 +end +r = write 1 tkeys s sub e s +end + +note line k of tmsgs, '%' replaced by key name f +fn wmline k ulong f ulong to void +s = ulong 0 +ln = ulong 0 +while lt ln k bound 128 +while ne get tmsgs s 10 bound 128 +s = add s 1 +end +s = add s 1 +ln = add ln 1 +end +b = s +while ne get tmsgs s 10 bound 128 +if eq get tmsgs s 37 +r = write 1 tmsgs b sub s b +wkey f +b = add s 1 +end +s = add s 1 +end +r = write 1 tmsgs b sub s b +end + +fn wdec v ulong to void +d = new uchar 24 0 +i = ulong 24 +while true bound 24 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +r = write 1 d i sub 24 i +end + +fn wsdec v long to void +if lt v 0 +r = write 1 tdash 0 1 +wdec ulong neg v +ret +end +wdec ulong v +end + +fn wtab to void +r = write 1 ttab 0 1 +end + +fn wmsg wk edit ulong 65536 to void +c = get wk 0 +if le c 19 +r = write 1 tjs 0 len tjs +wmline sub c 1 0 +r = write 1 tat 0 len tat +wdec get wk 2 +ret +end +wmline sub c 1 get wk 1 +end + +fn hexc v ulong up bool to uchar +if lt v 10 +ret uchar add v 48 +end +if up +ret uchar add v 55 +end +ret uchar add v 87 +end + +note out[off, off + cnt) as lower-case hex on stdout +fn ohex out edit uchar 1049632 off ulong cnt ulong hb edit uchar 65536 to void +p = off +e = add off cnt +while lt p e bound 64 +m = umin sub e p 32768 +for i ulong m +b = ulong get out add p i +put hb mul i 2 hexc shr b 4 false +put hb add mul i 2 1 hexc and b 15 false +end +r = write 1 hb 0 mul m 2 +p = add p m +end +end + +fn rline js view uchar 4194304 n ulong wk edit ulong 65536 out edit uchar 1049632 hb edit uchar 65536 to void +r = write 1 tr 0 1 +wtab +pr = rcparse js n wk +wsdec pr +wtab +if lt pr 0 +wmsg wk +wtab +r = write 1 tdash 0 1 +else +t = rccanon js n wk out 0 +ohex out 0 umin t 1048576 hb +pos = ulong 1048576 +while lt pos t bound 16 +t2 = rccanon js n wk out pos +ohex out 0 umin sub t pos 1048576 hb +pos = add pos 1048576 +end +wtab +ohex out 1049600 32 hb +end +wtab +vr = rcverify js n wk out +wsdec vr +wtab +if lt vr 0 +wmsg wk +else +ohex out 1049600 32 hb +end +r = write 1 tnl 0 1 +end + +note offset of the first run of 64 '0' bytes, n when none +fn findz js view uchar 4194304 n ulong to ulong +if gt n 4194304 +ret n +end +run = ulong 0 +i = ulong 0 +for o ulong 16 +k = ulong 0 +while lt k 500000 bound 999999 +if ge i n +ret n +end +if eq get js i 48 +run = add run 1 +if eq run 64 +ret sub i 63 +end +else +run = 0 +end +i = add i 1 +k = add k 1 +end +end +ret n +end + +note write the computed id (out 1049600) as hex over the placeholder at z +fn patchid js edit uchar 4194304 out edit uchar 1049632 z ulong up bool to void +for i ulong 32 +v = ulong get out add 1049600 i +q = add z mul i 2 +put js q hexc shr v 4 up +put js add q 1 hexc and v 15 up +end +end + +note the mutation of record header (a, b); the new length +fn mutate js edit uchar 4194304 n ulong a ulong b ulong to ulong +if ge a n +ret n +end +if and ge b 1 le b 255 +put js a xor get js a uchar b +end +if eq b 256 +ret a +end +ret n +end + +fn mline js edit uchar 4194304 n ulong wk edit ulong 65536 out edit uchar 1049632 hb edit uchar 65536 to void +z = findz js n +pr = long -1 +if lt z n +pr = rcparse js n wk +end +if lt pr 0 +rline js n wk out hb +ret +end +t = rccanon js n wk out 0 +patchid js out z eq get wk 61 257 +m = mutate js n get wk 60 get wk 61 +rline js m wk out hb +end + +fn tval i ulong to long +if eq i 0 +ret -3 +end +ret sub long i 2 +end + +fn sline to void +r = write 1 ts 0 1 +for i ulong 11 +for j ulong 11 +wtab +wsdec rcsat tval i tval j +end +end +r = write 1 tnl 0 1 +end + +fn dorec js edit uchar 4194304 n ulong op uchar a ulong b ulong to void +wk = new ulong 65536 0 +out = new uchar 1049632 0 +hb = new uchar 65536 0 +if eq op 84 +r = write 1 tt 0 1 +wtab +wsdec rctier js n wk +r = write 1 tnl 0 1 +ret +end +if eq op 83 +sline +ret +end +if eq op 77 +put wk 60 a +put wk 61 b +mline js n wk out hb +ret +end +rline js n wk out hb +end + +fn dec8 hd edit uchar 32 o ulong to ulong +v = ulong 0 +for i ulong 8 +v = add mul v 10 ulong sub get hd add o i 48 +end +ret v +end + +note read exactly n bytes into js; the count read +fn rdjs js edit uchar 4194304 n ulong to ulong +t = ulong 0 +while lt t n bound 999999 +r = read 0 js t sub n t +if le r 0 +ret t +end +t = add t ulong r +end +ret t +end + +fn rdhd hd edit uchar 32 to ulong +t = ulong 0 +while lt t 25 bound 64 +r = read 0 hd t sub 25 t +if le r 0 +ret t +end +t = add t ulong r +end +ret t +end + +fn drain n ulong to ulong +bf = new uchar 65536 0 +t = ulong 0 +while lt t n bound 999999 +r = read 0 bf 0 umin sub n t 65536 +if le r 0 +ret t +end +t = add t ulong r +end +ret t +end + +note one record: 0 done, 1 end of input, 2 short record +fn onerec js edit uchar 4194304 to ulong +hd = new uchar 32 0 +h = rdhd hd +if eq h 0 +ret 1 +end +if ne h 25 +ret 2 +end +n = dec8 hd 1 +if le n 4194304 +if ne rdjs js n n +ret 2 +end +else +if ne drain n n +ret 2 +end +end +dorec js n get hd 0 dec8 hd 9 dec8 hd 17 +ret 0 +end + +fn main to int +js = new uchar 4194304 0 +while true bound 999999 +s = onerec js +if eq s 1 +ret 0 +end +if eq s 2 +ret 65 +end +end +ret 0 +end ```