diff --git a/CHANGELOG.md b/CHANGELOG.md index bd9c8af..791ada8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,20 @@ VERSIONING.md. forged histories, 23 refusal vectors and a join expectation list (`vectors/join.txt`). Sources: aien-architecture causal-id-join-v0 (fa46a26) and resource-contract-v0 (ddc17b7). +- `specs/verified-crumb`: Verified Crumb V1 (VC1) contract 0.1.0, new. Canonical + byte encoding of VerifiedCrumbV1 (big-endian, domain tag + `AIEN_VERIFIED_CRUMB_V1`, sorted unique lists, no padding), VC id = SHA-256 + of the canonical bytes, resolve rule (lock, semantic id, store, identity, + receipt, transitive closure, refuse on any failure), 9 resolve refusal codes + plus 13 format refusal codes and the VC1 addition `VERIFIER_TOO_OLD`, the omega-dev (tainted) and omega-build + (verified only) domains, and the `omega.lock` text format. Receipt id binds + to the aien-proof BLAKE3 id. 17 golden vectors (4 accept, 13 refuse), a C + checker (`tools/vc1-check.c`), and `tools/check-golden.sh` with 3 vector + mutants and 9 checker mutants. No consumer pins it yet. Open design items + are listed in SPEC.md sections 4 and 11 (`digest_kind` byte, 0x01 source / 0x02 IR, is part of the encoding). +- `specs/evidence-receipt`: partial extraction of aien-proof EvidenceReceiptV1 + (`SUBSET.md`): only the identity derivation and fields VC1 binds to, + pinned to aien-sovereign-core `40dd373`. Full extraction is owed. ## [v0.1.0] - 2026-09-23 diff --git a/README.md b/README.md index 87b2862..a1d447d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ Research-grade and pre-alpha. The specifications and reference Rust crates (Apac ```text specs/ Normative specifications (Community Specification License 1.0): - agent-state, inference, evaluation + agent-state, inference, evaluation, crumb-visible, execution-transcript, + evidence-receipt (partial), verified-crumb crates/ Reference Rust crates (Apache-2.0), legacy: aien-protocol-types, aien-agent-state-abi, aien-inference-protocol, aien-inference-client, aien-action-protocol, aien-event-protocol, diff --git a/specs/evidence-receipt/SUBSET.md b/specs/evidence-receipt/SUBSET.md new file mode 100644 index 0000000..aa74a39 --- /dev/null +++ b/specs/evidence-receipt/SUBSET.md @@ -0,0 +1,49 @@ +# Evidence Receipt (EvidenceReceiptV1): the subset VC1 needs + +**Status**: Partial extraction, 2026-10-02 (VC1-FMT). This is NOT the full wire specification of the receipt. It records only what [Verified Crumb V1](../verified-crumb/SPEC.md) binds to. **The full extraction (decode rules, tier and mutation rules, ledger and lease store checks, JSON rendering) is owed.** +**Contract version**: 0.1.0 (major zero, see VERSIONING.md) +**License**: Community Specification License 1.0 +**Authority**: the Rust source below. Where this file and the source differ, the source (as pinned) wins and this file is wrong. +**Pinned source**: `aien-dev/aien-sovereign-core` commit `40dd373`, `crates/aien-proof/src/` (read 2026-10-02). Line numbers are for that commit. + +## 1. Receipt identity (what VC1 binds to) + +- The receipt id is the BLAKE3 hash (32 bytes, shown as 64 lowercase hex digits) of the canonical byte encoding. `evidence.rs:7-12` (module rule), `receipt_id` at `evidence.rs:657-660`. +- The human-readable JSON form is not canonical and never hashed. `evidence.rs:8-9`. +- The `id` field is not part of the hashed bytes. `evidence.rs:453-620` (no `id` is written). +- Domain tag: ASCII `AIEN_EVIDENCE_RECEIPT_V1` (24 bytes), `evidence.rs:22`, written first at `evidence.rs:546`. +- Strings are an 8-byte big-endian length followed by the bytes (`push_str`, `evidence.rs:433-437`); counts are 4-byte big-endian (`push_u32`, `evidence.rs:438-441`); integers are big-endian. +- Lists (`input_artifacts`, `output_artifacts`, `dependencies`, `external_refs`, `required_features`) are sorted and de-duplicated before encoding (`sorted_unique`, `evidence.rs:446-451`); assertions are sorted by id (`evidence.rs:569`). Receipt `dependencies` are written as raw 32-byte hashes (`evidence.rs:579-585`). + +## 2. Canonical field order (`evidence.rs:545-619`) + +domain tag, `u32` version (1), `kind`, `tier` (string), `result` (1 byte: PASS 0, FAIL 1, BLOCKED 2, INCOMPLETE 3, SKIPPED 4; `evidence.rs:61-69`), `repo`, `commit`, `dirty` (1 byte), `toolchain`, `procedure`, `machine`, `env_class`, `input_artifacts` (count then strings), `output_artifacts` (count then strings), assertions (count then `id`, `expected`, `observed`, `pass` byte, `source`, `note`), `dependencies` (count then raw 32-byte ids), `declared_mutation` byte, `observed_mutation` byte, `authority`, `output_digest` (32 raw bytes), `external_refs`, `ledger` (0, or 1 then `u64` index and 32 raw bytes), `lease` (0, or 1 then 32 raw bytes and `resource`), `timestamp` `u64`, `required_features`, trailing `u32` zero (`evidence.rs:619`). + +## 3. Fields VC1 reads + +Struct `Receipt`, `evidence.rs:294-343`: + +| Field | Type | VC1 use | +|---|---|---| +| `id` | BLAKE3 hex | must equal `verification.receipt_id` | +| `kind` | string | must equal `verification.verifier_profile` | +| `tier` | enum, `evidence.rs:85-102`, rank at `evidence.rs:133-143` | must satisfy the profile minimum; `TEST_ONLY_TRUST` satisfies nothing but itself (`chain.rs:259-267`) | +| `result` | verdict | must be `PASS` | +| `input_artifacts` | `:` strings, algos `blake3`, `sha256`, `sha512` (`evidence.rs:412-431`) | must list the VC's `semantic_id` and `source_or_ir_digest` as `sha256:` | +| `output_digest` | 64 hex digits, BLAKE3 of the captured output (`evidence.rs:130-131`, checked at `evidence.rs:490-494`) | must equal `verification.evidence_root` | +| `dependencies` | receipt ids | must equal the receipt ids of the VC's dependencies | + +## 4. Verification entry points VC1 relies on + +- `verify_bytes` (`evidence.rs:849-860`): parse JSON, recompute the id, refuse on mismatch. +- `verify_with_store` (`evidence.rs:907-930`): identity, consistency, ledger and lease bindings. +- `load_receipt` (`evidence.rs:932-948`): store path `/receipts/.json`, refuses a file whose bytes hash to another id. +- `verify_chain` / `verify_graph` (`chain.rs:46`, `chain.rs:157`): cycle is FAIL, missing dependency is INCOMPLETE, any non-PASS reachable receipt blocks, `TEST_ONLY_TRUST` ancestry cannot support `PRODUCTION` (`chain.rs:1-14`). + +## 5. A second, different "EvidenceReceiptV1" exists + +`tools/aien-test` in the same repo writes a receipt with schema string `aien-test/EvidenceReceiptV1`, canonical JSON, SHA-256 digest, stored as `.json` (`tools/aien-test/src/evidence.rs:1`, `:16`, `:23`, `:127-134`). It is NOT the aien-proof receipt (BLAKE3 over binary canonical bytes). VC1 binds only to the aien-proof id until the two converge. A VC whose `receipt_id` is an aien-test digest is refused as `RECEIPT_HASH_MISMATCH` by any resolver that uses the aien-proof store. + +## 6. Owed + +Full extraction of: `decode_canonical` rules (`evidence.rs:721-848`), tier/mutation limits (`evidence.rs:624-654`), ledger and lease store checks (`ledger_bind.rs`, `lease.rs`), the JSON field renderings, and golden vectors for the receipt itself. Convergence of the aien-test receipt with this one needs an owner decision. diff --git a/specs/verified-crumb/SPEC.md b/specs/verified-crumb/SPEC.md new file mode 100644 index 0000000..9a5912c --- /dev/null +++ b/specs/verified-crumb/SPEC.md @@ -0,0 +1,226 @@ +# Verified Crumb: VerifiedCrumbV1 (VC1) + +**Status**: Draft v0 (VC1-FMT, stage 1 of the Verified Crumb program, 2026-10-02). Not yet produced or consumed by any runtime. +**Contract version**: 0.1.0 (major zero: may change by minor bump, see VERSIONING.md) +**Format version field**: `1` +**License**: Community Specification License 1.0 +**Reference checker**: [`tools/vc1-check.c`](tools/vc1-check.c), C99, self-contained (own SHA-256, libc only) +**Golden vectors**: [`golden/`](golden/) with [`golden/expected.txt`](golden/expected.txt); built independently by [`tools/make-golden.sh`](tools/make-golden.sh), run by [`tools/check-golden.sh`](tools/check-golden.sh) +**Receipt binding**: [`../evidence-receipt/SUBSET.md`](../evidence-receipt/SUBSET.md) +**Decision record**: ARCH-0029, `aien-dev/aien-architecture` `docs/adr/0029-verified-crumbs-single-dependency-resolver.md` (merged at `ed0cf93`). The ADR fixes the decisions and the field names; this spec fixes the bytes. + +## 0. Names and authority + +- **Digests are authority. Names are convenience.** A name (in source, in `omega.lock`, in a store index) only helps a human find a digest. No decision to trust anything is ever made from a name. +- **A Verified Crumb is not a `.crumb` file and not Crumbline.** `.crumb` (RFC-0001) is agent coordination: who is editing which directory. Crumbline is the learning curriculum. A Verified Crumb (VC) is a third thing: a record that binds one Omega program to the receipt that proved it. They share the word "crumb" and nothing else; no tool for one reads the other. + +## 1. Purpose + +Omega already has a library of verified programs. VC1 defines the portable record that makes that library the only legal dependency resolver: a program may depend only on programs whose VC, receipt and dependencies all check out, all the way down. Resolution that cannot prove this refuses. There is no warning mode and no bypass (section 7). + +## 2. The record + +VerifiedCrumbV1 is the following, in this order: + +| Field | Type | Meaning | +|---|---|---| +| `format_version` | u32 | `1`; every other value is refused | +| `semantic_id` | 32 bytes | the Omega program id (section 4.1) | +| `contract_id` | 32 bytes | identity of the program's contract (section 4.2) | +| `digest_kind` | u8 | what `source_or_ir_digest` covers: `0x01` canonical source, `0x02` canonical IR (section 4.3); any other value is refused | +| `source_or_ir_digest` | 32 bytes | SHA-256 of the exact source or IR bytes the verifier checked (section 4.3) | +| `realization_ids[]` | 0 to 256 x 32 bytes | digests of realizations of this program (section 4.4) | +| `dependencies[]` | 0 to 256 x {`semantic_id` 32, `required_contract` 32} | what this program imports, and under which contract | +| `verification` | {`receipt_id` 32, `verifier_profile`, `verifier_version`, `evidence_root` 32} | the proof (section 5) | +| `exports[]` | 0 to 256 strings | names this program offers | +| `capabilities[]` | 0 to 256 strings | capabilities this program needs | + +No field is optional. An empty list is a count of zero. + +The record has no `name` field. ARCH-0029 Decision 1 lists `name` as human-readable metadata carried in the name index, never authority and never used in resolution; it is not part of the VC bytes and not part of the VC id. `format_version` = `1` is the number for ARCH-0029's `VerifiedCrumbV1` tag. + +## 3. Canonical byte encoding + +Explicit bytes. No host struct, no padding, no alignment, no floating point, no text formatting. The same rules as the receipt encoding (`aien-proof` `evidence.rs:433-445`: big-endian integers, length-prefixed strings, domain tag first), so one reader style serves both. + +### 3.1 Primitives + +- `u32`: 4 bytes, big-endian. Counts are `u32`. +- `id`: exactly 32 raw bytes. +- `string`: `u64` big-endian byte length, then the bytes. Length 1 to 256. Every byte in `0x21` to `0x7E` (printable ASCII, no space). + +### 3.2 Layout + +``` +domain tag 22 bytes, ASCII "AIEN_VERIFIED_CRUMB_V1" (no terminator) +format_version u32 = 1 +semantic_id id +contract_id id +digest_kind u8 (0x01 source, 0x02 IR) +source_or_ir_digest id +realization count N u32 then N x id +dependency count M u32 then M x (semantic_id id, required_contract id) +verification.receipt_id id +verification.verifier_profile string +verification.verifier_version string +verification.evidence_root id +exports count u32 then strings +capabilities count u32 then strings +(end of input: no trailing byte) +``` + +### 3.3 Ordering and uniqueness (canonical form) + +- `dependencies` are sorted ascending by `semantic_id`, compared as unsigned bytes from the first byte (`memcmp`). Two entries with the same `semantic_id` are refused even if their `required_contract` differs. +- `realization_ids` are sorted ascending as unsigned bytes and unique. +- `exports` and `capabilities` are sorted ascending as unsigned bytes and unique. +- An encoder sorts its lists, then refuses duplicates. A decoder never sorts: unsorted input is refused, because a different byte order would be a different VC id. +- `semantic_id`, `contract_id`, `source_or_ir_digest`, `evidence_root`, and both ids inside every dependency entry must not be all zero. `receipt_id` must not be all zero (a zero receipt id is "no receipt": `MISSING_RECEIPT`). +- A dependency whose `semantic_id` equals the VC's own `semantic_id` is a cycle of length one: `DEPENDENCY_CYCLE`. +- Every list holds at most 256 entries (ARCH-0029 Decision 1: no cap below library capacity; the omega library holds 128 programs, `omega/src/omega_library.h:9`). Over the limit is refused, never truncated (the current omega library silently truncates dependencies at 8: `omega/src/omega_library.c:202`, `omega_library.h:10`; VC1 forbids that). + +### 3.4 VC id + +`vc_id = SHA256(canonical bytes)`, 32 bytes, where the canonical bytes begin with the domain tag. The tag is therefore part of the hashed input; a hash of the bytes without the tag is a different value and must not be used. The VC id names one exact record; it is not the program id and not the receipt id. + +Why SHA-256: omega's identities are SHA-256 (`omega_program.h:83`) and this repo already carries a SHA-256 in C (`specs/execution-transcript/tools/trn1_verify.c:53-117`). The receipt id inside the record stays BLAKE3, because it must equal the aien-proof id (section 5). + +### 3.5 Refusal order + +A decoder reads fields in layout order and the first failure wins, so the code is a function of the bytes alone: domain tag, version, `digest_kind`, zero ids, realization order, each dependency in turn (self-dependency, zero ids, then duplicate or unsorted order against the previous entry), zero receipt id, strings, zero evidence root, exports order, capabilities order, trailing bytes. Reading past the end of input is `TRUNCATED` at that point. + +### 3.6 Format refusal codes + +| Code | When | +|---|---| +| `BAD_DOMAIN_TAG` | first 22 bytes are not the tag | +| `UNKNOWN_FORMAT_VERSION` | `format_version` is not 1 | +| `TRUNCATED` | input ends inside a field | +| `TRAILING_BYTES` | bytes remain after the last field | +| `DUPLICATE_DEPENDENCY` | equal `semantic_id` in adjacent dependency entries | +| `UNSORTED_DEPENDENCIES` | dependency `semantic_id` lower than the previous one | +| `NONCANONICAL_SET` | realizations, exports or capabilities unsorted or duplicated | +| `BAD_STRING` | length 0, length over 256, or a byte outside `0x21` to `0x7E` | +| `BAD_DIGEST_KIND` | `digest_kind` is not `0x01` or `0x02` | +| `ZERO_ID` | an all-zero id where section 3.3 forbids it | +| `TOO_MANY_ENTRIES` | a count over 256 | +| `MISSING_RECEIPT` | all-zero `receipt_id` (also a resolve code) | +| `DEPENDENCY_CYCLE` | self-dependency (also a resolve code) | + +## 4. Field definitions + +### 4.1 `semantic_id` + +IS the Omega program id: `SHA256("omega.program.v2" 0x00 || body_root_id || in_type_id || out_type_id || pre_id || post_id)`, 32 bytes. Sources: `omega/src/omega_program.h:83` (formula), `:41` (domain string), `omega/src/omega_program.c:103-125` (`omega_program_compute_id`), `omega/src/omega_types.h:17,27-28` (`OMEGA_ID_BYTES` = 32, `SemanticId`). Omega commit `6fbf316`. VC1 defines no new derivation for this field. + +### 4.2 `contract_id` + +Omega has no single contract id today (UNVERIFIED by absence: grep of `omega/src` finds no function computing one; confidence 85%). The program id already hashes the four contract components (`omega_program.c:113-123`). VC1 therefore defines: + +`contract_id = SHA256("aien.vc1.contract.v1" 0x00 || in_type_id || out_type_id || pre_id || post_id)` + +with the same four 32-byte components as the program id, in that order. Stage VC1-LIB owns implementing it in omega. OPEN: this derivation is new and needs the omega owner's agreement (report item). + +### 4.3 `source_or_ir_digest` + +`digest_kind` says which: `0x01` canonical source, `0x02` canonical IR; any other value is refused (`BAD_DIGEST_KIND`). The digest is SHA-256 of the exact bytes the verifier checked. The kind sits in the canonical bytes, so the same digest bytes with a different kind give a different VC id (golden `v01_minimal` vs `v04_ir_kind`). Decided by the queen after inspection of PR 13; closes the former open question. + +### 4.4 `realization_ids[]` + +Opaque 32-byte digests of realization artifacts of this program. The derivation is owned by VC1-LIB and VC1-STORE (UNVERIFIED which hash omega uses for realizations; confidence 50%). VC1 only fixes how they are encoded and ordered. + +## 5. The `verification` block and its receipt + +`receipt_id` MUST be the aien-proof receipt id: the 32-byte BLAKE3 of the canonical bytes with domain `AIEN_EVIDENCE_RECEIPT_V1`. Source: `aien-sovereign-core` `crates/aien-proof/src/evidence.rs:453-620` (`canonical_bytes`), `:657` (`receipt_id`), pinned commit `40dd373`; wire subset in [`../evidence-receipt/SUBSET.md`](../evidence-receipt/SUBSET.md). VC1 invents no second receipt identity. The 64 hex digits of the receipt file name are these 32 bytes. + +**Open item.** `tools/aien-test` currently writes a different receipt: schema string `aien-test/EvidenceReceiptV1`, canonical JSON, SHA-256 digest (`tools/aien-test/src/evidence.rs:1,16,23,127-134`). VC1 binds only to the aien-proof id until the two converge. A VC naming an aien-test digest as `receipt_id` is refused (`RECEIPT_HASH_MISMATCH`). + +`verifier_profile` and `verifier_version` are strings (section 3.1). A resolver holds a profile table (profile name to minimum version and minimum tier). VC1 reserves no profile names; the first real entries are defined by VC1-STORE and VC1-CI. Golden vectors use `test-profile` / `1.0.0`. + +### 5.1 Receipt binding rules (checked by the resolver, not by `vc1-check`) + +For the receipt stored at `/receipts/.json`, all of these must hold: + +1. aien-proof `verify_with_store` accepts it (`evidence.rs:907`): the id recomputes from the canonical bytes. +2. `receipt.result` is `PASS`, and `receipt.tier` is at least the profile minimum (default `HOST_TEST`), never `TEST_ONLY_TRUST` (`chain.rs:259-267`). +3. `receipt.output_digest` (32 bytes) equals `evidence_root`. +4. `receipt.input_artifacts` lists `sha256:` and `sha256:`. +5. `receipt.kind` equals `verifier_profile`. +6. `receipt.dependencies` equals the sorted set of the `receipt_id` of each dependency's VC (duplicates collapse, as aien-proof sorts and de-duplicates receipt dependencies, `evidence.rs:446-451,579`; so aien-proof `verify_chain`, `chain.rs:46`, also walks the closure). + +These are VC1 design decisions, not facts about aien-proof today (OPEN: confirm with the aien-proof owner; report item). The receipt cannot contain the VC id (the VC contains the receipt id), so the binding runs through `semantic_id` and `source_or_ir_digest`. + +## 6. Resolve rule + +Inputs: a lockfile (section 8), a store keyed by `semantic_id`, a profile table, and a domain (section 7). The resolver MUST proceed in this order and MUST refuse on the first failure. It never returns a partial closure. + +1. **Lock.** Each imported name is looked up in `omega.lock`. Absent or malformed lock: `DEPENDENCY_NOT_PINNED`. +2. **Store.** Fetch the VC by the locked `semantic_id`. The name index is never consulted for trust. Absent: `UNVERIFIED_DEPENDENCY`. +3. **Identity.** Decode the VC (format refusals, section 3.6). Its `semantic_id` must equal the key it was fetched by, and the program recomputed from the stored source or IR must have that id (`omega_program_compute_id`) and that `source_or_ir_digest`. Mismatch: `UNVERIFIED_DEPENDENCY`. The locked `receipt` must equal `receipt_id`: else `RECEIPT_HASH_MISMATCH`. +4. **Taint.** In `omega-build`, a VC that the store marks tainted, or that lists the reserved capability `omega-dev.taint`, is refused: `TAINTED_ARTIFACT`. (ARCH-0029 Decision 5: omega-dev output never enters the store, so this is defence in depth; the reserved capability name is a VC1 proposal, OPEN.) +5. **Profile.** `verifier_profile` not in the profile table: `UNKNOWN_VERIFIER_PROFILE`. `verifier_version` below the table minimum: `VERIFIER_TOO_OLD` (a VC1 addition; ARCH-0029 Decision 9 has no code for it, and an earlier draft of this spec wrongly used `STALE_RECEIPT` for it). +6. **Receipt.** Receipt missing from the store: `MISSING_RECEIPT`. Receipt fails rule 1, 3, 5 or 6 of section 5.1, or rule 4 for the `semantic_id` (it does not name this program): `RECEIPT_HASH_MISMATCH`. Receipt names this `semantic_id` but not the current `source_or_ir_digest` (it covers an older source: it predates a change to the source it covers, ARCH-0029 Decision 9 code 6): `STALE_RECEIPT`. Receipt not `PASS` or tier too low: `UNVERIFIED_DEPENDENCY`. +7. **Edges.** Each dependency entry's VC (resolved by steps 2 to 7 with the entry's `semantic_id`, and no lock) must have `contract_id` equal to `required_contract`, else `UNVERIFIED_DEPENDENCY`. A dependency already on the current resolution path: `DEPENDENCY_CYCLE`. +8. **Declared imports.** Every import the program's source or IR actually makes must be a `dependencies[]` entry; otherwise `UNDECLARED_IMPORT`. This is checked where the program is compiled or verified. +9. **Closure.** The union of all visited VCs is the transitive closure. A resolution succeeds only if every VC in it passed 2 to 7. + +### 6.1 Resolve refusal codes + +`UNVERIFIED_DEPENDENCY`, `MISSING_RECEIPT`, `RECEIPT_HASH_MISMATCH`, `DEPENDENCY_NOT_PINNED`, `DEPENDENCY_CYCLE`, `STALE_RECEIPT` (the receipt predates a change to the source it covers), `UNDECLARED_IMPORT`, `TAINTED_ARTIFACT`, `UNKNOWN_VERIFIER_PROFILE` (the nine ARCH-0029 Decision 9 codes, same meanings), plus the VC1 addition `VERIFIER_TOO_OLD`. They are names, not numbers; a consumer may map them to its own exit codes. Format refusals (section 3.6) also refuse a resolution. + +## 7. The two domains + +- **omega-dev.** Exploratory. Anything it produces is TAINTED. It may read verified crumbs. Its output is not a VC (it has no receipt), never enters the store, and can never satisfy an import in `omega-build` (ARCH-0029 Decision 5). +- **omega-build.** The verified compiler. It accepts a program only if the full closure resolves under section 6. There is no flag, environment variable, file, command line or fallback that lets it read a name, a non-VC, a tainted artifact or an unverified dependency. Any such switch is a defect in the build, not a feature. +- The boundary is one-way: omega-build may consume only VCs; nothing built in omega-dev is promoted except by being verified into a new VC through the normal path. + +## 8. Lockfile: `omega.lock` + +UTF-8 text, LF line endings, no tabs. + +``` +omega.lock v1 +# comment lines start with #; blank lines are ignored +add = semantic 1111111111111111111111111111111111111111111111111111111111111111 receipt 5555555555555555555555555555555555555555555555555555555555555555 +``` + +- Line 1 is exactly `omega.lock v1`. +- Entry grammar: `NAME = semantic HEX64 receipt HEX64`, single spaces. `NAME` is `[A-Za-z_][A-Za-z0-9_.-]{0,127}`. `HEX64` is 64 lowercase hex digits. +- Entries are sorted by `NAME` (unsigned bytes) and names are unique. +- `semantic` pins the program id; `receipt` additionally pins the receipt id (ARCH-0029 Decision 3 has the lockfile map imports to a `semantic_id`; the `receipt` pin is a VC1 addition, so a store entry swapped for another receipt is caught at the lock). Resolution requires both to match the VC found in the store (section 6, steps 2 and 3). +- The name is a label the importing source uses. The pair of digests is the dependency. +- Any other line is a malformed lock: `DEPENDENCY_NOT_PINNED`. + +## 9. Conformance + +`golden/expected.txt` has one line per vector: `NAME ACCEPT ` or `NAME REFUSE `. `tools/check-golden.sh` regenerates the vectors, runs the C checker (with ASan/UBSan when available), recomputes every ACCEPT id with `sha256sum` (independent of the C code), proves three vector mutants (flip a byte, reorder dependencies, drop the domain tag) are detected, and proves nine deliberately broken checker builds (unsorted deps, zero receipt, no domain tag in the hash, duplicates, trailing bytes, zero ids, string bytes, entry count limit, digest kind) all fail the corpus. + +| Vector | Result | +|---|---| +| `v01_minimal`, `v02_deps`, `v03_capabilities`, `v04_ir_kind` | ACCEPT with fixed ids (`v04` is `v01` with `digest_kind` 0x02: a different id) | +| `r01_duplicate_dep` | `DUPLICATE_DEPENDENCY` | +| `r02_unsorted_deps` | `UNSORTED_DEPENDENCIES` | +| `r03_zero_receipt_id` | `MISSING_RECEIPT` | +| `r04_self_dependency` | `DEPENDENCY_CYCLE` | +| `r05_trailing_byte` | `TRAILING_BYTES` | +| `r06_unknown_version` | `UNKNOWN_FORMAT_VERSION` | +| `r07_truncated` | `TRUNCATED` | +| `r08_bad_domain_tag` | `BAD_DOMAIN_TAG` | +| `r09_unsorted_exports` | `NONCANONICAL_SET` | +| `r10_bad_digest_kind` | `BAD_DIGEST_KIND` | +| `r11_zero_id` | `ZERO_ID` | +| `r12_bad_string` | `BAD_STRING` | +| `r13_too_many_entries` | `TOO_MANY_ENTRIES` | + +Vector files are lowercase hex, one line, ending in a newline. + +## 10. Versioning + +Contract 0.1.0. While major is zero, a change to the byte layout, the id derivation or a code name is a minor bump with migration notes in CHANGELOG.md (VERSIONING.md). `format_version` changes only when the layout changes; a decoder refuses versions it does not know. + +## 11. Limits (stated, not hidden) + +- `vc1-check` covers format and id only. It does not read lockfiles, stores or receipts, and does not implement BLAKE3, so section 5.1 and section 6 are specified here but not yet executed by any tool. Stages VC1-STORE, VC1-RESOLVE and VC1-CI implement them. +- Sections 4.2, 4.4, 5.1 and the profile table are design proposals awaiting owner agreement. +- Golden ids come from synthetic ids (repeated bytes), not from real programs. +- `NONCANONICAL_SET` is vectored only for exports (not realizations or capabilities). diff --git a/specs/verified-crumb/golden/expected.txt b/specs/verified-crumb/golden/expected.txt new file mode 100644 index 0000000..9b84fda --- /dev/null +++ b/specs/verified-crumb/golden/expected.txt @@ -0,0 +1,17 @@ +v01_minimal ACCEPT 27890b090acc8b8e91ecf49bb4249fc99867824d0854f931e5f8fb9ce1497325 +v02_deps ACCEPT fa5a0e8f496b9f8d81b6f942bd56604b5b9a98252999ede6cb62dff6e7374f7f +v03_capabilities ACCEPT 94b84bf74e22f684d9400b9b31ccfa579f8eec434cb75589231402e2954f3e6d +r01_duplicate_dep REFUSE DUPLICATE_DEPENDENCY +r02_unsorted_deps REFUSE UNSORTED_DEPENDENCIES +r03_zero_receipt_id REFUSE MISSING_RECEIPT +r04_self_dependency REFUSE DEPENDENCY_CYCLE +r05_trailing_byte REFUSE TRAILING_BYTES +r06_unknown_version REFUSE UNKNOWN_FORMAT_VERSION +r07_truncated REFUSE TRUNCATED +r08_bad_domain_tag REFUSE BAD_DOMAIN_TAG +r09_unsorted_exports REFUSE NONCANONICAL_SET +v04_ir_kind ACCEPT 8d7dd4699e1781b20d4a01421be6779c68da483ff3d9b1899cb1fa0163dfb5c6 +r10_bad_digest_kind REFUSE BAD_DIGEST_KIND +r11_zero_id REFUSE ZERO_ID +r12_bad_string REFUSE BAD_STRING +r13_too_many_entries REFUSE TOO_MANY_ENTRIES diff --git a/specs/verified-crumb/golden/r01_duplicate_dep.hex b/specs/verified-crumb/golden/r01_duplicate_dep.hex new file mode 100644 index 0000000..df4662b --- /dev/null +++ b/specs/verified-crumb/golden/r01_duplicate_dep.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f5631000000011111111111111111111111111111111111111111111111111111111111111111222222222222222222222222222222222222222222222222222222222222222201333333333333333333333333333333333333333333333333333333333333333300000001444444444444444444444444444444444444444444444444444444444444444400000002a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b25555555555555555555555555555555555555555555555555555555555555555000000000000000c746573742d70726f66696c650000000000000005312e302e3066666666666666666666666666666666666666666666666666666666666666660000000000000000 diff --git a/specs/verified-crumb/golden/r02_unsorted_deps.hex b/specs/verified-crumb/golden/r02_unsorted_deps.hex new file mode 100644 index 0000000..0f73d7f --- /dev/null +++ b/specs/verified-crumb/golden/r02_unsorted_deps.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f5631000000011111111111111111111111111111111111111111111111111111111111111111222222222222222222222222222222222222222222222222222222222222222201333333333333333333333333333333333333333333333333333333333333333300000001444444444444444444444444444444444444444444444444444444444444444400000002a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b15555555555555555555555555555555555555555555555555555555555555555000000000000000c746573742d70726f66696c650000000000000005312e302e3066666666666666666666666666666666666666666666666666666666666666660000000000000000 diff --git a/specs/verified-crumb/golden/r03_zero_receipt_id.hex b/specs/verified-crumb/golden/r03_zero_receipt_id.hex new file mode 100644 index 0000000..da35a4a --- /dev/null +++ b/specs/verified-crumb/golden/r03_zero_receipt_id.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f56310000000111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222013333333333333333333333333333333333333333333333333333333333333333000000014444444444444444444444444444444444444444444444444444444444444444000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c746573742d70726f66696c650000000000000005312e302e3066666666666666666666666666666666666666666666666666666666666666660000000000000000 diff --git a/specs/verified-crumb/golden/r04_self_dependency.hex b/specs/verified-crumb/golden/r04_self_dependency.hex new file mode 100644 index 0000000..3478e28 --- /dev/null +++ b/specs/verified-crumb/golden/r04_self_dependency.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f56310000000111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222013333333333333333333333333333333333333333333333333333333333333333000000014444444444444444444444444444444444444444444444444444444444444444000000011111111111111111111111111111111111111111111111111111111111111111b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b15555555555555555555555555555555555555555555555555555555555555555000000000000000c746573742d70726f66696c650000000000000005312e302e3066666666666666666666666666666666666666666666666666666666666666660000000000000000 diff --git a/specs/verified-crumb/golden/r05_trailing_byte.hex b/specs/verified-crumb/golden/r05_trailing_byte.hex new file mode 100644 index 0000000..e40e3c5 --- /dev/null +++ b/specs/verified-crumb/golden/r05_trailing_byte.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f56310000000111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222013333333333333333333333333333333333333333333333333333333333333333000000014444444444444444444444444444444444444444444444444444444444444444000000005555555555555555555555555555555555555555555555555555555555555555000000000000000c746573742d70726f66696c650000000000000005312e302e306666666666666666666666666666666666666666666666666666666666666666000000000000000000 diff --git a/specs/verified-crumb/golden/r06_unknown_version.hex b/specs/verified-crumb/golden/r06_unknown_version.hex new file mode 100644 index 0000000..ce2a8bc --- /dev/null +++ b/specs/verified-crumb/golden/r06_unknown_version.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f56310000000211111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222013333333333333333333333333333333333333333333333333333333333333333000000014444444444444444444444444444444444444444444444444444444444444444000000005555555555555555555555555555555555555555555555555555555555555555000000000000000c746573742d70726f66696c650000000000000005312e302e3066666666666666666666666666666666666666666666666666666666666666660000000000000000 diff --git a/specs/verified-crumb/golden/r07_truncated.hex b/specs/verified-crumb/golden/r07_truncated.hex new file mode 100644 index 0000000..214f35f --- /dev/null +++ b/specs/verified-crumb/golden/r07_truncated.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f56310000000111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222013333333333333333333333333333333333333333333333333333333333333333000000014444444444444444444444444444444444444444444444444444444444444444000000005555555555555555555555555555555555555555555555555555555555555555000000000000000c746573742d70726f66696c650000000000000005312e302e30666666666666666666666666666666666666666666666666666666666666666600000000000000 diff --git a/specs/verified-crumb/golden/r08_bad_domain_tag.hex b/specs/verified-crumb/golden/r08_bad_domain_tag.hex new file mode 100644 index 0000000..bf20555 --- /dev/null +++ b/specs/verified-crumb/golden/r08_bad_domain_tag.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f560a0000000111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222013333333333333333333333333333333333333333333333333333333333333333000000014444444444444444444444444444444444444444444444444444444444444444000000005555555555555555555555555555555555555555555555555555555555555555000000000000000c746573742d70726f66696c650000000000000005312e302e3066666666666666666666666666666666666666666666666666666666666666660000000000000000 diff --git a/specs/verified-crumb/golden/r09_unsorted_exports.hex b/specs/verified-crumb/golden/r09_unsorted_exports.hex new file mode 100644 index 0000000..1006c3a --- /dev/null +++ b/specs/verified-crumb/golden/r09_unsorted_exports.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f56310000000111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222013333333333333333333333333333333333333333333333333333333333333333000000014444444444444444444444444444444444444444444444444444444444444444000000005555555555555555555555555555555555555555555555555555555555555555000000000000000c746573742d70726f66696c650000000000000005312e302e306666666666666666666666666666666666666666666666666666666666666666000000020000000000000003737562000000000000000361646400000000 diff --git a/specs/verified-crumb/golden/r10_bad_digest_kind.hex b/specs/verified-crumb/golden/r10_bad_digest_kind.hex new file mode 100644 index 0000000..beb381f --- /dev/null +++ b/specs/verified-crumb/golden/r10_bad_digest_kind.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f56310000000111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222033333333333333333333333333333333333333333333333333333333333333333000000014444444444444444444444444444444444444444444444444444444444444444000000005555555555555555555555555555555555555555555555555555555555555555000000000000000c746573742d70726f66696c650000000000000005312e302e3066666666666666666666666666666666666666666666666666666666666666660000000000000000 diff --git a/specs/verified-crumb/golden/r11_zero_id.hex b/specs/verified-crumb/golden/r11_zero_id.hex new file mode 100644 index 0000000..9cb4b6d --- /dev/null +++ b/specs/verified-crumb/golden/r11_zero_id.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f56310000000100000000000000000000000000000000000000000000000000000000000000002222222222222222222222222222222222222222222222222222222222222222013333333333333333333333333333333333333333333333333333333333333333000000014444444444444444444444444444444444444444444444444444444444444444000000005555555555555555555555555555555555555555555555555555555555555555000000000000000c746573742d70726f66696c650000000000000005312e302e3066666666666666666666666666666666666666666666666666666666666666660000000000000000 diff --git a/specs/verified-crumb/golden/r12_bad_string.hex b/specs/verified-crumb/golden/r12_bad_string.hex new file mode 100644 index 0000000..79a67bd --- /dev/null +++ b/specs/verified-crumb/golden/r12_bad_string.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f56310000000111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222013333333333333333333333333333333333333333333333333333333333333333000000014444444444444444444444444444444444444444444444444444444444444444000000005555555555555555555555555555555555555555555555555555555555555555000000000000000b6261642070726f66696c650000000000000005312e302e3066666666666666666666666666666666666666666666666666666666666666660000000000000000 diff --git a/specs/verified-crumb/golden/r13_too_many_entries.hex b/specs/verified-crumb/golden/r13_too_many_entries.hex new file mode 100644 index 0000000..213cbed --- /dev/null +++ b/specs/verified-crumb/golden/r13_too_many_entries.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f5631000000011111111111111111111111111111111111111111111111111111111111111111222222222222222222222222222222222222222222222222222222222222222201333333333333333333333333333333333333333333333333333333333333333300000101 diff --git a/specs/verified-crumb/golden/v01_minimal.hex b/specs/verified-crumb/golden/v01_minimal.hex new file mode 100644 index 0000000..aa6213c --- /dev/null +++ b/specs/verified-crumb/golden/v01_minimal.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f56310000000111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222013333333333333333333333333333333333333333333333333333333333333333000000014444444444444444444444444444444444444444444444444444444444444444000000005555555555555555555555555555555555555555555555555555555555555555000000000000000c746573742d70726f66696c650000000000000005312e302e3066666666666666666666666666666666666666666666666666666666666666660000000000000000 diff --git a/specs/verified-crumb/golden/v02_deps.hex b/specs/verified-crumb/golden/v02_deps.hex new file mode 100644 index 0000000..37661ee --- /dev/null +++ b/specs/verified-crumb/golden/v02_deps.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f56310000000111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222013333333333333333333333333333333333333333333333333333333333333333000000024444444444444444444444444444444444444444444444444444444444444444454545454545454545454545454545454545454545454545454545454545454500000002a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b25555555555555555555555555555555555555555555555555555555555555555000000000000000c746573742d70726f66696c650000000000000005312e302e3066666666666666666666666666666666666666666666666666666666666666660000000100000000000000046d61696e00000000 diff --git a/specs/verified-crumb/golden/v03_capabilities.hex b/specs/verified-crumb/golden/v03_capabilities.hex new file mode 100644 index 0000000..c8be4f1 --- /dev/null +++ b/specs/verified-crumb/golden/v03_capabilities.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f5631000000011212121212121212121212121212121212121212121212121212121212121212222222222222222222222222222222222222222222222222222222222222222201333333333333333333333333333333333333333333333333333333333333333300000001444444444444444444444444444444444444444444444444444444444444444400000001a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b15656565656565656565656565656565656565656565656565656565656565656000000000000000c746573742d70726f66696c650000000000000005312e302e306767676767676767676767676767676767676767676767676767676767676767000000020000000000000003616464000000000000000373756200000002000000000000000e6361702e636c6f636b2e72656164000000000000000b6361702e66732e72656164 diff --git a/specs/verified-crumb/golden/v04_ir_kind.hex b/specs/verified-crumb/golden/v04_ir_kind.hex new file mode 100644 index 0000000..ed89021 --- /dev/null +++ b/specs/verified-crumb/golden/v04_ir_kind.hex @@ -0,0 +1 @@ +4149454e5f56455249464945445f4352554d425f56310000000111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222023333333333333333333333333333333333333333333333333333333333333333000000014444444444444444444444444444444444444444444444444444444444444444000000005555555555555555555555555555555555555555555555555555555555555555000000000000000c746573742d70726f66696c650000000000000005312e302e3066666666666666666666666666666666666666666666666666666666666666660000000000000000 diff --git a/specs/verified-crumb/tools/check-golden.sh b/specs/verified-crumb/tools/check-golden.sh new file mode 100755 index 0000000..246307a --- /dev/null +++ b/specs/verified-crumb/tools/check-golden.sh @@ -0,0 +1,90 @@ +#!/bin/bash +# check-golden.sh [CC]: prove the VC1 golden corpus and the C checker agree. +# +# 1. golden/ is byte-identical to make-golden.sh output (ignoring the crumb +# tool's .crumb and .crumb.local files). +# 2. vc1-check builds with warnings as errors (and under ASan/UBSan if the +# compiler has it) and passes every line of golden/expected.txt. +# 3. Every ACCEPT id is recomputed with sha256sum over the raw bytes, a hash +# implementation independent of the C tool. +# 4. Vector mutants (flip a byte, reorder deps, drop the domain tag) must be +# detected: the checker's answer must differ from the golden expectation. +# 5. Checker mutants (-DVC1_MUTANT=1..9) must each make the corpus FAIL. +# Host-only. Shell, xxd, sha256sum and a C99 compiler. No Python. +set -eu +export LC_ALL=C +here=$(cd "$(dirname "$0")" && pwd) +gold="$here/../golden" +CC=${1:-cc} +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT + +bash "$here/make-golden.sh" "$tmp/regen" +if ! diff -r -x .crumb -x .crumb.local "$gold" "$tmp/regen" >/dev/null; then + echo "VC1_GOLDEN_STALE: golden/ differs from make-golden.sh output" >&2 + diff -rq -x .crumb -x .crumb.local "$gold" "$tmp/regen" >&2 || true + exit 1 +fi +echo "VC1_GOLDEN_REGEN identical" + +"$CC" -std=c99 -O2 -Wall -Wextra -Werror -o "$tmp/vc1-check" "$here/vc1-check.c" +"$tmp/vc1-check" --corpus "$gold" +if "$CC" -std=c99 -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all \ + -o "$tmp/vc1-check-san" "$here/vc1-check.c" 2>/dev/null; then + "$tmp/vc1-check-san" --corpus "$gold" | sed 's/impl=c /impl=c-asan-ubsan /' +else + echo "VC1_SANITIZER NOT_RUN (compiler lacks -fsanitize=address,undefined)" +fi + +# 3. independent hash cross-check +n=0 +while read -r name verb arg; do + [ "$verb" = ACCEPT ] || continue + want=$(xxd -r -p "$gold/$name.hex" | sha256sum | cut -d' ' -f1) + got=$("$tmp/vc1-check" "$gold/$name.hex" | cut -d' ' -f2) + if [ "$want" != "$arg" ] || [ "$got" != "$arg" ]; then + echo "VC1_HASH_MISMATCH $name expected=$arg sha256sum=$want tool=$got" >&2; exit 1 + fi + n=$((n + 1)) +done < "$gold/expected.txt" +echo "VC1_INDEPENDENT_SHA256 ids=$n agree" +# same digest bytes, different digest_kind: ids must differ +id1=$(grep "^v01_minimal " "$gold/expected.txt" | cut -d" " -f3); id4=$(grep "^v04_ir_kind " "$gold/expected.txt" | cut -d" " -f3) +[ "$id1" != "$id4" ] || { echo "VC1_KIND_NOT_IN_ID" >&2; exit 1; } +echo "VC1_DIGEST_KIND_IN_ID v01(source) != v04(ir)" + +# 4. vector mutants, applied to v02_deps (hex string surgery, 2 hex chars per byte) +v=$(tr -d '\n' < "$gold/v02_deps.hex") +exp=$(grep '^v02_deps ' "$gold/expected.txt" | cut -d' ' -f2-) +tag=44 # domain tag is 22 bytes = 44 hex chars +sem=$((tag + 8)) # semantic_id starts after tag and 4-byte version +deps=$((tag + 8 + 3 * 64 + 2 + 8 + 2 * 64 + 8)) # first dep entry: tag, version, 3 ids, digest_kind byte, realization count, 2 realizations, dep count +flip=$(printf '%s' "$v" | sed "s/^\(.\{$sem\}\)../\1ee/") +d1=${v:$deps:128}; d2=${v:$((deps + 128)):128} +swap="${v:0:$deps}$d2$d1${v:$((deps + 256))}" +notag=${v:$tag} +mut=0 +for m in flip swap notag; do + case $m in flip) h=$flip;; swap) h=$swap;; notag) h=$notag;; esac + [ "$h" != "$v" ] || { echo "VC1_MUTANT_NOOP $m" >&2; exit 2; } + printf '%s\n' "$h" > "$tmp/m.hex" + set +e; got=$("$tmp/vc1-check" "$tmp/m.hex" | sed 's/ @.*//'); set -e + if [ "$got" = "$exp" ]; then echo "VC1_VECTOR_MUTANT_SURVIVED $m" >&2; exit 1; fi + echo "vector mutant $m -> $got (golden: ACCEPT ${exp#ACCEPT }) detected" + mut=$((mut + 1)) +done +echo "VC1_VECTOR_MUTANTS detected=$mut/3" + +# 5. checker mutants +surv=0 +for k in 1 2 3 4 5 6 7 8 9; do + "$CC" -std=c99 -O2 -DVC1_MUTANT=$k -o "$tmp/mut$k" "$here/vc1-check.c" + if "$tmp/mut$k" --corpus "$gold" >"$tmp/mut$k.out" 2>&1; then + echo "VC1_CHECKER_MUTANT_SURVIVED $k" >&2; surv=$((surv + 1)) + else + echo "checker mutant $k: $(tail -n 1 "$tmp/mut$k.out") (killed)" + fi +done +[ "$surv" -eq 0 ] || exit 1 +echo "VC1_CHECKER_MUTANTS killed=9/9" +echo "VC1_GOLDEN_CHECK PASS" diff --git a/specs/verified-crumb/tools/make-golden.sh b/specs/verified-crumb/tools/make-golden.sh new file mode 100755 index 0000000..5072836 --- /dev/null +++ b/specs/verified-crumb/tools/make-golden.sh @@ -0,0 +1,69 @@ +#!/bin/bash +# make-golden.sh OUT_DIR: write the VC1 golden vectors (hex files) and +# expected.txt from explicit hex fragments. It does NOT use the C tool, so the +# vectors are built independently of the encoder under test. Shell + coreutils +# + xxd only. +set -eu +export LC_ALL=C +out=${1:?usage: make-golden.sh OUT_DIR} +mkdir -p "$out" + +hexs() { printf '%s' "$1" | xxd -p -c 99999 | tr -d '\n'; } # ASCII -> hex +rep() { local i s=""; for ((i = 0; i < 32; i++)); do s+="$1"; done; printf '%s' "$s"; } # byte -> 32 bytes +u32() { printf '%08x' "$1"; } +str() { printf '%016x%s' "${#1}" "$(hexs "$1")"; } # u64 BE length + bytes +TAG=$(hexs "AIEN_VERIFIED_CRUMB_V1") + +# vc VERSION SEM CON KIND SRC "REALS" "DEPS" RECEIPT PROFILE VER ROOT "EXPORTS" "CAPS" +# ids are one byte token (e.g. 11) repeated 32 times; KIND = one byte token +# (01 source, 02 IR); REALS = tokens; +# DEPS = tokens "sem/contract" ; EXPORTS and CAPS = ASCII words. +vc() { + local ver=$1 sem=$2 con=$3 kind=$4 src=$5 reals=$6 deps=$7 rcpt=$8 prof=$9 pver=${10} root=${11} exps=${12} caps=${13} + local s="$TAG$(u32 "$ver")$(rep "$sem")$(rep "$con")$kind$(rep "$src")" r d e c n + n=0; for r in $reals; do n=$((n + 1)); done; s+=$(u32 $n) + for r in $reals; do s+=$(rep "$r"); done + n=0; for d in $deps; do n=$((n + 1)); done; s+=$(u32 $n) + for d in $deps; do s+="$(rep "${d%/*}")$(rep "${d#*/}")"; done + s+="$(rep "$rcpt")$(str "$prof")$(str "$pver")$(rep "$root")" + n=0; for e in $exps; do n=$((n + 1)); done; s+=$(u32 $n) + for e in $exps; do s+=$(str "$e"); done + n=0; for c in $caps; do n=$((n + 1)); done; s+=$(u32 $n) + for c in $caps; do s+=$(str "$c"); done + printf '%s' "$s" +} +emit() { printf '%s\n' "$2" > "$out/$1.hex"; } + +: > "$out/expected.txt" +# expected ACCEPT ids are computed with sha256sum over the raw bytes +# (independent of the C tool's own SHA-256). +accept() { + emit "$1" "$2" + local id; id=$(printf '%s' "$2" | xxd -r -p | sha256sum | cut -d' ' -f1) + echo "$1 ACCEPT $id" >> "$out/expected.txt" +} +refuse() { emit "$1" "$2"; echo "$1 REFUSE $3" >> "$out/expected.txt"; } + +P=test-profile +accept v01_minimal "$(vc 1 11 22 01 33 "44" "" 55 $P 1.0.0 66 "" "")" +accept v02_deps "$(vc 1 11 22 01 33 "44 45" "a1/b1 a2/b2" 55 $P 1.0.0 66 "main" "")" +accept v03_capabilities "$(vc 1 12 22 01 33 "44" "a1/b1" 56 $P 1.0.0 67 "add sub" "cap.clock.read cap.fs.read")" + +refuse r01_duplicate_dep "$(vc 1 11 22 01 33 "44" "a1/b1 a1/b2" 55 $P 1.0.0 66 "" "")" DUPLICATE_DEPENDENCY +refuse r02_unsorted_deps "$(vc 1 11 22 01 33 "44" "a2/b2 a1/b1" 55 $P 1.0.0 66 "" "")" UNSORTED_DEPENDENCIES +refuse r03_zero_receipt_id "$(vc 1 11 22 01 33 "44" "" 00 $P 1.0.0 66 "" "")" MISSING_RECEIPT +refuse r04_self_dependency "$(vc 1 11 22 01 33 "44" "11/b1" 55 $P 1.0.0 66 "" "")" DEPENDENCY_CYCLE +refuse r05_trailing_byte "$(vc 1 11 22 01 33 "44" "" 55 $P 1.0.0 66 "" "")00" TRAILING_BYTES +refuse r06_unknown_version "$(vc 2 11 22 01 33 "44" "" 55 $P 1.0.0 66 "" "")" UNKNOWN_FORMAT_VERSION +v=$(vc 1 11 22 01 33 "44" "" 55 $P 1.0.0 66 "" "") +refuse r07_truncated "${v%??}" TRUNCATED +refuse r08_bad_domain_tag "$(printf '%s' "$v" | sed 's/^\(.\{42\}\)../\10a/')" BAD_DOMAIN_TAG +refuse r09_unsorted_exports "$(vc 1 11 22 01 33 "44" "" 55 $P 1.0.0 66 "sub add" "")" NONCANONICAL_SET + +# digest_kind: 0x02 (IR) with the same digest bytes as v01 must give a different id +accept v04_ir_kind "$(vc 1 11 22 02 33 "44" "" 55 $P 1.0.0 66 "" "")" +refuse r10_bad_digest_kind "$(vc 1 11 22 03 33 "44" "" 55 $P 1.0.0 66 "" "")" BAD_DIGEST_KIND +refuse r11_zero_id "$(vc 1 00 22 01 33 "44" "" 55 $P 1.0.0 66 "" "")" ZERO_ID +refuse r12_bad_string "$(vc 1 11 22 01 33 "44" "" 55 "bad profile" 1.0.0 66 "" "")" BAD_STRING +# 257 realizations declared (limit 256): refused at the count, before any body +refuse r13_too_many_entries "$TAG$(u32 1)$(rep 11)$(rep 22)01$(rep 33)$(u32 257)" TOO_MANY_ENTRIES diff --git a/specs/verified-crumb/tools/vc1-check.c b/specs/verified-crumb/tools/vc1-check.c new file mode 100644 index 0000000..7868a64 --- /dev/null +++ b/specs/verified-crumb/tools/vc1-check.c @@ -0,0 +1,415 @@ +/* + * vc1-check.c: reference encoder, decoder and id computer for VerifiedCrumbV1 + * (specs/verified-crumb/SPEC.md). + * + * Self-contained C99, libc only. The SHA-256 below is copied from + * specs/execution-transcript/tools/trn1_verify.c lines 53 to 117 (FIPS 180-4). + * + * vc1-check FILE.hex print "ACCEPT " or "REFUSE @" + * vc1-check --corpus DIR check DIR/expected.txt against DIR/.hex, + * print "VC1_CONFORMANCE impl=c pass=N fail=M" + * + * Exit: 0 accept/all pass, 1 refuse/any fail, 2 usage or I/O error. + * Compile with -DVC1_MUTANT=N (1..9) to build a deliberately broken checker; + * tools/check-golden.sh proves the corpus rejects every mutant. + */ +#include +#include +#include +#include + +#ifndef VC1_MUTANT +#define VC1_MUTANT 0 +#endif + +/* ---- SHA-256 (copy of trn1_verify.c) ------------------------------------- */ +typedef struct { uint32_t h[8]; uint64_t n; uint8_t b[64]; uint32_t used; } sha256; + +static const uint32_t K256[64] = { + 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, + 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, + 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, + 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, + 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, + 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, + 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, + 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 +}; +#define ROR(x, r) (((x) >> (r)) | ((x) << (32 - (r)))) + +static void sha_block(sha256 *s, const uint8_t *p) { + uint32_t w[64], a, b, c, d, e, f, g, h; + for (int i = 0; i < 16; i++) + w[i] = (uint32_t)p[4*i] << 24 | (uint32_t)p[4*i+1] << 16 | (uint32_t)p[4*i+2] << 8 | p[4*i+3]; + for (int i = 16; i < 64; i++) { + uint32_t s0 = ROR(w[i-15], 7) ^ ROR(w[i-15], 18) ^ (w[i-15] >> 3); + uint32_t s1 = ROR(w[i-2], 17) ^ ROR(w[i-2], 19) ^ (w[i-2] >> 10); + w[i] = w[i-16] + s0 + w[i-7] + s1; + } + a = s->h[0]; b = s->h[1]; c = s->h[2]; d = s->h[3]; + e = s->h[4]; f = s->h[5]; g = s->h[6]; h = s->h[7]; + for (int i = 0; i < 64; i++) { + uint32_t t1 = h + (ROR(e, 6) ^ ROR(e, 11) ^ ROR(e, 25)) + ((e & f) ^ (~e & g)) + K256[i] + w[i]; + uint32_t t2 = (ROR(a, 2) ^ ROR(a, 13) ^ ROR(a, 22)) + ((a & b) ^ (a & c) ^ (b & c)); + h = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2; + } + s->h[0] += a; s->h[1] += b; s->h[2] += c; s->h[3] += d; + s->h[4] += e; s->h[5] += f; s->h[6] += g; s->h[7] += h; +} +static void sha_init(sha256 *s) { + static const uint32_t iv[8] = { 0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a, + 0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19 }; + memcpy(s->h, iv, sizeof iv); s->n = 0; s->used = 0; +} +static void sha_update(sha256 *s, const void *data, size_t len) { + const uint8_t *p = data; + s->n += len; + while (len) { + uint32_t k = 64 - s->used; + if (k > len) k = (uint32_t)len; + memcpy(s->b + s->used, p, k); + s->used += k; p += k; len -= k; + if (s->used == 64) { sha_block(s, s->b); s->used = 0; } + } +} +static void sha_final(sha256 *s, uint8_t out[32]) { + uint64_t bits = s->n * 8; + uint8_t pad = 0x80, z = 0, l[8]; + sha_update(s, &pad, 1); + while (s->used != 56) sha_update(s, &z, 1); + for (int i = 0; i < 8; i++) l[i] = (uint8_t)(bits >> (56 - 8 * i)); + sha_update(s, l, 8); + for (int i = 0; i < 8; i++) { + out[4*i] = (uint8_t)(s->h[i] >> 24); out[4*i+1] = (uint8_t)(s->h[i] >> 16); + out[4*i+2] = (uint8_t)(s->h[i] >> 8); out[4*i+3] = (uint8_t)s->h[i]; + } +} +static void sha2(const void *a, size_t na, uint8_t out[32]) { + sha256 s; sha_init(&s); sha_update(&s, a, na); sha_final(&s, out); +} + +/* ---- VC1 model ----------------------------------------------------------- */ +#define TAG "AIEN_VERIFIED_CRUMB_V1" +#define TAG_LEN 22 +#define VC_VERSION 1u +#define VC_MAX 256 /* max entries per list (SPEC section 3.6) */ +#define VC_STR_MAX 256 /* max bytes per string */ + +enum code { + OK = 0, + /* resolve-level names, also produced at format level */ + MISSING_RECEIPT = 1, DEPENDENCY_CYCLE = 2, + /* format-level */ + BAD_DOMAIN_TAG = 20, UNKNOWN_FORMAT_VERSION, TRUNCATED, TRAILING_BYTES, + DUPLICATE_DEPENDENCY, UNSORTED_DEPENDENCIES, NONCANONICAL_SET, BAD_STRING, + ZERO_ID, TOO_MANY_ENTRIES, BAD_DIGEST_KIND +}; +static const char *code_name(int c) { + switch (c) { + case MISSING_RECEIPT: return "MISSING_RECEIPT"; + case DEPENDENCY_CYCLE: return "DEPENDENCY_CYCLE"; + case BAD_DOMAIN_TAG: return "BAD_DOMAIN_TAG"; + case UNKNOWN_FORMAT_VERSION: return "UNKNOWN_FORMAT_VERSION"; + case TRUNCATED: return "TRUNCATED"; + case TRAILING_BYTES: return "TRAILING_BYTES"; + case DUPLICATE_DEPENDENCY: return "DUPLICATE_DEPENDENCY"; + case UNSORTED_DEPENDENCIES: return "UNSORTED_DEPENDENCIES"; + case NONCANONICAL_SET: return "NONCANONICAL_SET"; + case BAD_STRING: return "BAD_STRING"; + case ZERO_ID: return "ZERO_ID"; + case TOO_MANY_ENTRIES: return "TOO_MANY_ENTRIES"; + case BAD_DIGEST_KIND: return "BAD_DIGEST_KIND"; + } + return "?"; +} + +typedef struct { uint8_t semantic_id[32], required_contract[32]; } dep_t; +typedef struct { char s[VC_STR_MAX + 1]; } str_t; +typedef struct { + uint32_t format_version; + uint8_t semantic_id[32], contract_id[32], digest_kind, source_or_ir_digest[32]; + uint32_t n_real; uint8_t realization_ids[VC_MAX][32]; + uint32_t n_dep; dep_t deps[VC_MAX]; + uint8_t receipt_id[32]; str_t verifier_profile, verifier_version; uint8_t evidence_root[32]; + uint32_t n_exp; str_t exports[VC_MAX]; + uint32_t n_cap; str_t capabilities[VC_MAX]; +} vc_t; + +static int is_zero(const uint8_t *p) { + uint8_t acc = 0; + for (int i = 0; i < 32; i++) acc |= p[i]; + return acc == 0; +} + +/* ---- decoder ------------------------------------------------------------- */ +typedef struct { const uint8_t *b; size_t n, pos; int err; size_t err_at; } cur_t; + +static int fail(cur_t *c, int code) { + if (!c->err) { c->err = code; c->err_at = c->pos; } + return code; +} +static int need(cur_t *c, size_t k) { + if (c->err) return 0; + if (c->n - c->pos < k) { fail(c, TRUNCATED); return 0; } + return 1; +} +static uint32_t rd32(cur_t *c) { + if (!need(c, 4)) return 0; + const uint8_t *p = c->b + c->pos; c->pos += 4; + return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]; +} +static uint64_t rd64(cur_t *c) { + uint64_t hi = rd32(c), lo = rd32(c); + return hi << 32 | lo; +} +static void rd_id(cur_t *c, uint8_t out[32]) { + if (!need(c, 32)) { memset(out, 0, 32); return; } + memcpy(out, c->b + c->pos, 32); c->pos += 32; +} +static void rd_str(cur_t *c, str_t *out) { + uint64_t len = rd64(c); + out->s[0] = 0; + if (c->err) return; + if (len == 0 || len > VC_STR_MAX) { fail(c, BAD_STRING); return; } + if (!need(c, (size_t)len)) return; + for (uint64_t i = 0; i < len; i++) { + uint8_t ch = c->b[c->pos + i]; +#if VC1_MUTANT == 7 + if (0) { +#else + if (ch < 0x21 || ch > 0x7e) { +#endif + c->pos += (size_t)i; fail(c, BAD_STRING); return; } + } + memcpy(out->s, c->b + c->pos, (size_t)len); out->s[len] = 0; c->pos += (size_t)len; +} +static uint32_t rd_count(cur_t *c) { + uint32_t n = rd32(c); +#if VC1_MUTANT != 8 + if (!c->err && n > VC_MAX) { fail(c, TOO_MANY_ENTRIES); return 0; } +#endif + return n; +} + +/* Field order is the refusal order; the first failure wins. */ +static int decode(const uint8_t *b, size_t n, vc_t *v, size_t *err_at) { + cur_t c = { b, n, 0, 0, 0 }; + memset(v, 0, sizeof *v); + if (!need(&c, TAG_LEN)) goto done; + if (memcmp(b, TAG, TAG_LEN) != 0) { fail(&c, BAD_DOMAIN_TAG); goto done; } + c.pos = TAG_LEN; + v->format_version = rd32(&c); + if (!c.err && v->format_version != VC_VERSION) { c.pos -= 4; fail(&c, UNKNOWN_FORMAT_VERSION); c.pos += 4; } + rd_id(&c, v->semantic_id); rd_id(&c, v->contract_id); + if (need(&c, 1)) { + v->digest_kind = c.b[c.pos]; +#if VC1_MUTANT != 9 + if (v->digest_kind != 1 && v->digest_kind != 2) fail(&c, BAD_DIGEST_KIND); +#endif + c.pos++; + } + rd_id(&c, v->source_or_ir_digest); +#if VC1_MUTANT != 6 + if (!c.err && (is_zero(v->semantic_id) || is_zero(v->contract_id) || is_zero(v->source_or_ir_digest))) + fail(&c, ZERO_ID); +#endif + v->n_real = rd_count(&c); + for (uint32_t i = 0; i < v->n_real && !c.err; i++) { + rd_id(&c, v->realization_ids[i]); + if (!c.err && i > 0 && memcmp(v->realization_ids[i - 1], v->realization_ids[i], 32) >= 0) + fail(&c, NONCANONICAL_SET); + } + v->n_dep = rd_count(&c); + for (uint32_t i = 0; i < v->n_dep && !c.err; i++) { + rd_id(&c, v->deps[i].semantic_id); rd_id(&c, v->deps[i].required_contract); + if (c.err) break; + if (memcmp(v->deps[i].semantic_id, v->semantic_id, 32) == 0) { fail(&c, DEPENDENCY_CYCLE); break; } + if (is_zero(v->deps[i].semantic_id) || is_zero(v->deps[i].required_contract)) { fail(&c, ZERO_ID); break; } + if (i > 0) { + int cmp = memcmp(v->deps[i - 1].semantic_id, v->deps[i].semantic_id, 32); +#if VC1_MUTANT != 4 + if (cmp == 0) { fail(&c, DUPLICATE_DEPENDENCY); break; } +#endif +#if VC1_MUTANT != 1 + if (cmp > 0) { fail(&c, UNSORTED_DEPENDENCIES); break; } +#endif + } + } + rd_id(&c, v->receipt_id); +#if VC1_MUTANT != 2 + if (!c.err && is_zero(v->receipt_id)) fail(&c, MISSING_RECEIPT); +#endif + rd_str(&c, &v->verifier_profile); rd_str(&c, &v->verifier_version); + rd_id(&c, v->evidence_root); + if (!c.err && is_zero(v->evidence_root)) fail(&c, ZERO_ID); + v->n_exp = rd_count(&c); + for (uint32_t i = 0; i < v->n_exp && !c.err; i++) { + rd_str(&c, &v->exports[i]); + if (!c.err && i > 0 && strcmp(v->exports[i - 1].s, v->exports[i].s) >= 0) fail(&c, NONCANONICAL_SET); + } + v->n_cap = rd_count(&c); + for (uint32_t i = 0; i < v->n_cap && !c.err; i++) { + rd_str(&c, &v->capabilities[i]); + if (!c.err && i > 0 && strcmp(v->capabilities[i - 1].s, v->capabilities[i].s) >= 0) fail(&c, NONCANONICAL_SET); + } +#if VC1_MUTANT != 5 + if (!c.err && c.pos != c.n) fail(&c, TRAILING_BYTES); +#endif +done: + *err_at = c.err_at; + return c.err; +} + +/* ---- encoder ------------------------------------------------------------- */ +typedef struct { uint8_t *p; size_t n, cap; } buf_t; +static void put(buf_t *o, const void *d, size_t k) { + if (o->n + k > o->cap) { + o->cap = (o->n + k) * 2 + 64; + o->p = realloc(o->p, o->cap); + if (!o->p) { fputs("out of memory\n", stderr); exit(2); } + } + memcpy(o->p + o->n, d, k); o->n += k; +} +static void put32(buf_t *o, uint32_t x) { + uint8_t t[4] = { (uint8_t)(x >> 24), (uint8_t)(x >> 16), (uint8_t)(x >> 8), (uint8_t)x }; + put(o, t, 4); +} +static void put_str(buf_t *o, const char *s) { + uint64_t n = strlen(s); + put32(o, (uint32_t)(n >> 32)); put32(o, (uint32_t)n); put(o, s, (size_t)n); +} +static int cmp_dep(const void *a, const void *b) { return memcmp(a, b, 32); } +static int cmp_id(const void *a, const void *b) { return memcmp(a, b, 32); } +static int cmp_str(const void *a, const void *b) { return strcmp(((const str_t *)a)->s, ((const str_t *)b)->s); } + +/* Canonical bytes, with every list sorted (the encoder owns the sort; the + * decoder refuses unsorted input). Duplicate dependencies are refused. */ +static int encode(vc_t *v, buf_t *o) { + qsort(v->realization_ids, v->n_real, 32, cmp_id); + qsort(v->deps, v->n_dep, sizeof(dep_t), cmp_dep); + qsort(v->exports, v->n_exp, sizeof(str_t), cmp_str); + qsort(v->capabilities, v->n_cap, sizeof(str_t), cmp_str); + for (uint32_t i = 1; i < v->n_dep; i++) + if (memcmp(v->deps[i - 1].semantic_id, v->deps[i].semantic_id, 32) == 0) return DUPLICATE_DEPENDENCY; + o->n = 0; + put(o, TAG, TAG_LEN); put32(o, v->format_version); + put(o, v->semantic_id, 32); put(o, v->contract_id, 32); put(o, &v->digest_kind, 1); put(o, v->source_or_ir_digest, 32); + put32(o, v->n_real); for (uint32_t i = 0; i < v->n_real; i++) put(o, v->realization_ids[i], 32); + put32(o, v->n_dep); + for (uint32_t i = 0; i < v->n_dep; i++) { put(o, v->deps[i].semantic_id, 32); put(o, v->deps[i].required_contract, 32); } + put(o, v->receipt_id, 32); put_str(o, v->verifier_profile.s); put_str(o, v->verifier_version.s); + put(o, v->evidence_root, 32); + put32(o, v->n_exp); for (uint32_t i = 0; i < v->n_exp; i++) put_str(o, v->exports[i].s); + put32(o, v->n_cap); for (uint32_t i = 0; i < v->n_cap; i++) put_str(o, v->capabilities[i].s); + return OK; +} + +/* VC id = SHA-256 over the canonical bytes (which start with the domain tag). */ +static void vc_id(const uint8_t *canon, size_t n, uint8_t out[32]) { +#if VC1_MUTANT == 3 + sha2(canon + TAG_LEN, n - TAG_LEN, out); /* mutant: domain tag dropped from the hash */ +#else + sha2(canon, n, out); +#endif +} + +/* ---- I/O ----------------------------------------------------------------- */ +static int unhex(int ch) { + if (ch >= '0' && ch <= '9') return ch - '0'; + if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10; + return -1; +} +/* Read a lowercase-hex file (whitespace ignored). Returns malloc'd bytes or NULL. */ +static uint8_t *read_hex(const char *path, size_t *n_out) { + FILE *f = fopen(path, "rb"); + if (!f) return NULL; + size_t cap = 4096, n = 0; char *t = malloc(cap); int ch; + while ((ch = fgetc(f)) != EOF) { + if (ch == '\n' || ch == '\r' || ch == ' ') continue; + if (n + 1 > cap) { cap *= 2; t = realloc(t, cap); } + t[n++] = (char)ch; + } + fclose(f); + if (n % 2) { free(t); return NULL; } + uint8_t *b = malloc(n / 2 + 1); + for (size_t i = 0; i < n; i += 2) { + int hi = unhex((unsigned char)t[i]), lo = unhex((unsigned char)t[i + 1]); + if (hi < 0 || lo < 0) { free(t); free(b); return NULL; } + b[i / 2] = (uint8_t)(hi << 4 | lo); + } + free(t); *n_out = n / 2; return b; +} +static void hex32(const uint8_t *p, char out[65]) { + for (int i = 0; i < 32; i++) sprintf(out + 2 * i, "%02x", p[i]); +} + +static vc_t g_vc; /* large, so static */ + +/* Check one buffer. On accept fills id_hex; on refuse fills code and offset. */ +static int check(const uint8_t *b, size_t n, char id_hex[65], int *code, size_t *at) { + size_t err_at = 0; + int rc = decode(b, n, &g_vc, &err_at); + if (rc) { *code = rc; *at = err_at; return 1; } + /* canonical round trip: re-encoding must reproduce the input bytes */ + buf_t o = { 0, 0, 0 }; + vc_t *tmp = malloc(sizeof *tmp); memcpy(tmp, &g_vc, sizeof *tmp); + int erc = encode(tmp, &o); + int same = erc == OK && o.n == n && memcmp(o.p, b, n) == 0; + /* the encoder sorts: a reversed copy must encode to the same bytes */ + if (same && g_vc.n_dep > 1) { + for (uint32_t i = 0; i < g_vc.n_dep / 2; i++) { + dep_t t = tmp->deps[i]; tmp->deps[i] = tmp->deps[g_vc.n_dep - 1 - i]; tmp->deps[g_vc.n_dep - 1 - i] = t; + } + buf_t o2 = { 0, 0, 0 }; + same = encode(tmp, &o2) == OK && o2.n == o.n && memcmp(o2.p, o.p, o.n) == 0; + free(o2.p); + } + free(tmp); free(o.p); + if (!same) { *code = TRAILING_BYTES; *at = n; fputs("vc1-check: internal: encode/decode round trip differs\n", stderr); return 2; } + uint8_t id[32]; vc_id(b, n, id); hex32(id, id_hex); + return 0; +} + +static int run_file(const char *path) { + size_t n; uint8_t *b = read_hex(path, &n); + if (!b) { fprintf(stderr, "vc1-check: cannot read lowercase hex file %s\n", path); return 2; } + char id[65]; int code = 0; size_t at = 0; + int r = check(b, n, id, &code, &at); + free(b); + if (r == 0) { printf("ACCEPT %s\n", id); return 0; } + if (r == 2) return 2; + printf("REFUSE %s @%zu\n", code_name(code), at); + return 1; +} + +static int run_corpus(const char *dir) { + char path[1024], line[512]; + snprintf(path, sizeof path, "%s/expected.txt", dir); + FILE *f = fopen(path, "r"); + if (!f) { fprintf(stderr, "vc1-check: cannot open %s\n", path); return 2; } + int pass = 0, failn = 0; + while (fgets(line, sizeof line, f)) { + char name[256], verb[16], arg[128]; + if (sscanf(line, "%255s %15s %127s", name, verb, arg) != 3) continue; + snprintf(path, sizeof path, "%s/%s.hex", dir, name); + size_t n; uint8_t *b = read_hex(path, &n); + char id[65] = ""; int code = 0; size_t at = 0; int ok = 0; + if (b) { + int r = check(b, n, id, &code, &at); + if (r == 0) ok = strcmp(verb, "ACCEPT") == 0 && strcmp(arg, id) == 0; + else if (r == 1) ok = strcmp(verb, "REFUSE") == 0 && strcmp(arg, code_name(code)) == 0; + free(b); + } + if (ok) pass++; else { failn++; printf("FAIL %s (expected %s %s)\n", name, verb, arg); } + } + fclose(f); + printf("VC1_CONFORMANCE impl=c pass=%d fail=%d\n", pass, failn); + return failn ? 1 : 0; +} + +int main(int argc, char **argv) { + if (argc == 3 && strcmp(argv[1], "--corpus") == 0) return run_corpus(argv[2]); + if (argc == 2 && argv[1][0] != '-') return run_file(argv[1]); + fputs("usage: vc1-check FILE.hex | vc1-check --corpus DIR\n", stderr); + return 2; +}