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/782546aee8ed93fc # 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#224 (code only) closed derived `782546aee8ed93fc` #224 Imported from GitHub aien-dev/omega-mv#224 (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 `a06f484ec4b763cce4b6bdb2da69e31f0bb06f7d` · Head `b6e9089dfdbae98bf6ab4b08d0c76110c979c9fe` ### Apply it ``` git checkout -b patch-782546aee8ed93fc a06f484ec4b763cce4b6bdb2da69e31f0bb06f7d curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/782546aee8ed93fc.patch | git am ``` Raw patch (mbox) · 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 224 b6e9089dfdbae98bf6ab4b08d0c76110c979c9fe ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":224,"url":"https://github.com/aien-dev/omega-mv/pull/224","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:51Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.github/workflows/host-suites-2.yml b/.github/workflows/host-suites-2.yml index 589c1922d..c7e145d76 100644 --- a/.github/workflows/host-suites-2.yml +++ b/.github/workflows/host-suites-2.yml @@ -143,7 +143,7 @@ jobs: sel polyglot "^src/(polyglot|algebra)/|^tests/polyglot/|^mk/polyglot[^/]*\.mk$|^src/(aarch64_|sha256\.)|^spec/polyglot-0\.md$|^evidence/POLYGLOT/" sel algebra "^src/(algebra|turing)/|^tests/algebra/|^src/(omega_canonical|omega_types|sha256)\.|^evidence/MIXED_ALGEBRA/" sel path "^src/path/|^tests/path/|^src/(omega_types|sha256)\." - sel crumbline "$core|^src/crumbline/|^tests/crumbline/|^tools/crumbline_learner\.c$|^data/crumbline/" + sel crumbline "$core|^src/crumbline/|^tests/crumbline/|^tools/crumbline_learner\.c$|^data/crumbline/|^tests/(test_omega_[a-z_]+\.c|vc_fixture\.h|genesis_variant/)|^docs/osc/VC-GENESIS-1\.md$" sel runtime "$core|^src/(runtime|fabric)/|^tests/runtime/|^mk/fabric\.mk$|^tests/(r15_receipt_test\.sh|r16_[a-z_]+/)|^tools/(r15_|r16_|omegatool\.c)|^spec/r16-|^docs/r16-|^evidence/R15/|^(physics|aienos)\.lock$" sel argus "^src/runtime/|^tests/runtime/|^tests/bench_rx_argus\.c$|^mk/machine-identity\.mk$|^src/(sha256|omega_evidence)\.|^(physics|aienos|argus)\.lock$" sel numeric2 "^tests/(test_omega_reduce|test_omega_divsqrt_gb10)\.c$|^tests/numeric_oracle\.h$|^src/(omega_numeric|omega_blackwell_(encoder|codegen|matmul|qmd)|sha256)" @@ -261,7 +261,7 @@ jobs: with: fetch-depth: 0 - name: Crumbline learner conformance vectors - run: make PHYSICS_DIR=/nonexistent PHYSICS_LOCK_CHECK=0 test-crumbline test-library test-vcstore test-resolve + run: make PHYSICS_DIR=/nonexistent PHYSICS_LOCK_CHECK=0 test-crumbline test-library test-vcstore test-resolve test-vcstore-genesis test-resolve-genesis test-genesis test-genesis-real test-program-ir test-vc-bridge runtime: needs: changes diff --git a/Makefile b/Makefile index eeadc8c53..dcd6e3b3f 100644 --- a/Makefile +++ b/Makefile @@ -1853,6 +1853,7 @@ test-resolve: tests/test_omega_resolve.c $(OUT_DIR)/compiler/oscv $(RESOLVE_SRC) build_mutant missing-receipt missing-receipt resolve refuse-missing-receipt ' if (fr != 0) return fail(err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, sid, "receipt unavailable");'; \ build_mutant rule1-name rule1-name resolve refuse-receipt-file-holds-another-receipt ' if (0) {'; \ build_mutant rule-clean rule-clean resolve refuse-dirty-receipt-in-build ' if (0) {'; \ + build_mutant rule-clean-admit rule-clean resolve admit-refuses-dirty-receipt-in-build ' if (0) {'; \ build_mutant rule4-semantic rule4-semantic resolve refuse-receipt-does-not-name-program ' if (0) {'; \ build_mutant rule4-source rule4-source resolve refuse-stale-receipt ' if (0) {'; \ build_mutant rule3 rule3 resolve refuse-receipt-output-digest-is-not-evidence-root ' if (0) {'; \ @@ -1903,4 +1904,181 @@ test-resolve: tests/test_omega_resolve.c $(OUT_DIR)/compiler/oscv $(RESOLVE_SRC) build_mutant prod-authority-trim prod-authority receipt receipt-refuses-production-blank-authority ' if (b == 0) { ef(&e, "receipt: PRODUCTION receipts require an authority reference%s", ""); goto done; }'; \ build_mutant prod-spelling prod-spelling receipt receipt-refuses-production-test-only-authority ' int bad = strstr(low, "test-only") || strstr(low, "testonly");'; \ build_mutant prod-bad-refused prod-bad-refused receipt receipt-refuses-production-test-only-authority ' if (bad && 0) { ef(&e, "receipt: a TEST_ONLY signer cannot produce a PRODUCTION receipt%s", ""); goto done; }' - @echo "test-resolve: PASS (all checks, ASan/UBSan clean, all 65 mutants killed)" + @echo "test-resolve: PASS (all checks, ASan/UBSan clean, all 66 mutants killed)" + +# VC1-GENESIS (VC1 stage 6): the pinned Genesis Set, the private store doors, the mandatory +# source/IR recheck in the build domain. The resolver and the store now rebuild the program from +# its IR (src/omega_program_ir.c), so the resolver link set grows by the program stack. These +# additions are at the end on purpose: `+=` on a recursive variable is read by every recipe above. +RESOLVE_PROGRAM_STACK = src/omega_program_ir.c src/omega_program.c src/omega_canonical.c src/omega_validate.c src/omega_core.c src/omega_codec.c src/aarch64_encoder.c src/aarch64_decoder.c src/omega_realize.c src/omega_exec.c src/omega_verify.c src/omega_realize_synth.c src/omega_machine.c +RESOLVE_REST += $(RESOLVE_PROGRAM_STACK) +RESOLVE_HDRS += src/omega_genesis.h src/omega_vcstore_priv.h src/omega_program_ir.h tests/vc_fixture.h + +# test-genesis: the LISTED-member behavior of VC-GENESIS-1, which the empty real set cannot show. +# The variant header is GENERATED from src/omega_genesis.h (same functions, byte for byte; only the +# count, the set name and one table row differ), and omega_vcstore.c and omega_resolve.c are copied +# with ONE rewritten include line so they read it. A quote include looks in the including file's own +# directory first, which is why -I cannot swap the real header and why the copy is made. Nothing in +# production source can select the variant. Mutants below break one guard each in the copies. +.PHONY: test-genesis +GENESIS_DIR = $(OUT_DIR)/genesis-test +GENESIS_STACK = src/omega_receipt.c src/omega_blake3.c src/sha256.c $(RESOLVE_PROGRAM_STACK) +GENESIS_CFLAGS = $(RESOLVE_FLAGS) -I$(GENESIS_DIR) -Itests +test-genesis: tests/test_omega_genesis_set.c tests/genesis_variant/member.inc src/omega_vcstore.c src/omega_resolve.c src/omega_genesis.h $(RESOLVE_HDRS) + @set -eu; mkdir -p $(GENESIS_DIR)/genesis_variant; \ + sed -e 's|^#define OMEGA_GENESIS_1_COUNT 0u|#define OMEGA_GENESIS_1_COUNT 1u|' -e 's|^#define OMEGA_GENESIS_SET_NAME "VC-GENESIS-1"|#define OMEGA_GENESIS_SET_NAME "VC-GENESIS-1-TEST-VARIANT"|' src/omega_genesis.h \ + | awk -v f=tests/genesis_variant/member.inc '/^ \{ 0 \}$$/ { while ((getline l < f) > 0) print l } { print }' > $(GENESIS_DIR)/genesis_variant/omega_genesis.h; \ + grep -q '^#define OMEGA_GENESIS_1_COUNT 1u' $(GENESIS_DIR)/genesis_variant/omega_genesis.h; \ + grep -q 'TEST-VARIANT' $(GENESIS_DIR)/genesis_variant/omega_genesis.h; \ + [ "$$(grep -c '0x8f, 0x0b' $(GENESIS_DIR)/genesis_variant/omega_genesis.h)" = 1 ]; \ + for f in omega_vcstore omega_resolve; do \ + sed 's|#include "omega_genesis.h"|#include "genesis_variant/omega_genesis.h"|' src/$$f.c > $(GENESIS_DIR)/$$f.c; \ + if cmp -s src/$$f.c $(GENESIS_DIR)/$$f.c; then echo "test-genesis: include line not found in $$f.c"; exit 1; fi; done + $(CC) $(GENESIS_CFLAGS) -o $(GENESIS_DIR)/test_omega_genesis_set tests/test_omega_genesis_set.c $(GENESIS_DIR)/omega_vcstore.c $(GENESIS_DIR)/omega_resolve.c $(GENESIS_STACK) + $(GENESIS_DIR)/test_omega_genesis_set + $(CC) $(GENESIS_CFLAGS) -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all -o $(GENESIS_DIR)/test_omega_genesis_set_asan tests/test_omega_genesis_set.c $(GENESIS_DIR)/omega_vcstore.c $(GENESIS_DIR)/omega_resolve.c $(GENESIS_STACK) + $(GENESIS_DIR)/test_omega_genesis_set_asan + @set -eu; gmut() { name=$$1; which=$$2; tag=$$3; chk=$$4; repl=$$5; \ + f=$(GENESIS_DIR)/$$which.c; sed "/$$tag/c\\$$repl" $$f > $(GENESIS_DIR)/mut_$$name.c; \ + if cmp -s $$f $(GENESIS_DIR)/mut_$$name.c; then echo "mutant $$name: sed changed nothing"; exit 1; fi; \ + if [ $$which = omega_vcstore ]; then a=$(GENESIS_DIR)/mut_$$name.c; b=$(GENESIS_DIR)/omega_resolve.c; else a=$(GENESIS_DIR)/omega_vcstore.c; b=$(GENESIS_DIR)/mut_$$name.c; fi; \ + $(CC) $(GENESIS_CFLAGS) -o $(GENESIS_DIR)/mut_$$name tests/test_omega_genesis_set.c $$a $$b $(GENESIS_STACK); \ + rc=0; $(GENESIS_DIR)/mut_$$name > $(GENESIS_DIR)/mut_$$name.out 2>&1 || rc=$$?; \ + if [ $$rc -eq 0 ] || ! grep -q "^FAIL $$chk\$$" $(GENESIS_DIR)/mut_$$name.out; then echo "mutant $$name NOT killed by $$chk (exit $$rc)"; tail -5 $(GENESIS_DIR)/mut_$$name.out; exit 1; fi; \ + echo "mutant $$name: killed by $$chk (exit $$rc)"; }; \ + gmut gv-insert-refuses-listed omega_vcstore VC1S:insert-genesis store-inserts-a-listed-bootstrap-record ' if (kind == OMEGA_VCS_ADMISSION_BOOTSTRAP) { free(rec); return OMEGA_VCS_GENESIS_NOT_LISTED; }'; \ + gmut gv-insert-accepts-unlisted omega_vcstore VC1S:insert-genesis store-refuses-an-unlisted-bootstrap-record ' if (0) { free(rec); return OMEGA_VCS_GENESIS_NOT_LISTED; }'; \ + gmut gv-load-refuses-listed omega_vcstore VC1S:load-genesis store-saves-listed-bootstrap-and-verified-and-loads-them ' if (kind == OMEGA_VCS_ADMISSION_BOOTSTRAP) { rc = OMEGA_VCS_GENESIS_NOT_LISTED; break; }'; \ + gmut gv-load-accepts-unlisted omega_vcstore VC1S:load-genesis store-load-refuses-a-file-with-an-unlisted-bootstrap-record ' if (0) { rc = OMEGA_VCS_GENESIS_NOT_LISTED; break; }'; \ + gmut gv-gate-refuses-listed omega_resolve VC1R:genesis-gate resolver-lets-a-listed-bootstrap-record-satisfy-an-import-in-the-build-domain ' if (rec->admission_kind == OMEGA_VCS_ADMISSION_BOOTSTRAP) {'; \ + gmut gv-gate-accepts-unlisted omega_resolve VC1R:genesis-gate resolver-refuses-an-unlisted-bootstrap-record-even-with-the-list-non-empty ' if (0) {'; \ + gmut gv-admit-refuses-listed omega_resolve VC1R:admit-genesis-list admit-genesis-admits-a-listed-member ' if (1) {'; \ + gmut gv-admit-accepts-unlisted omega_resolve VC1R:admit-genesis-list admit-genesis-refuses-a-record-that-is-not-a-member ' if (0) {' + @echo "test-genesis: PASS (listed and unlisted at every door, ASan/UBSan clean, 8 mutants killed)" + +# test-program-ir: the canonical program IR (src/omega_program_ir.c). Each VC1I-tagged guard is +# broken in a copy and the named check must FAIL. +.PHONY: test-program-ir +PIR_DIR = $(OUT_DIR)/program-ir-test +PIR_STACK = src/sha256.c $(filter-out src/omega_program_ir.c,$(RESOLVE_PROGRAM_STACK)) +test-program-ir: tests/test_omega_program_ir.c src/omega_program_ir.c src/omega_program_ir.h + @mkdir -p $(PIR_DIR) + $(CC) $(RESOLVE_FLAGS) -Wno-pedantic -o $(PIR_DIR)/test_omega_program_ir tests/test_omega_program_ir.c src/omega_program_ir.c $(PIR_STACK) + $(PIR_DIR)/test_omega_program_ir + $(CC) $(RESOLVE_FLAGS) -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all -o $(PIR_DIR)/test_omega_program_ir_asan tests/test_omega_program_ir.c src/omega_program_ir.c $(PIR_STACK) + $(PIR_DIR)/test_omega_program_ir_asan + @set -eu; pmut() { name=$$1; tag=$$2; chk=$$3; repl=$$4; \ + sed "/VC1I:$$tag/c\\$$repl" src/omega_program_ir.c > $(PIR_DIR)/mut_$$name.c; \ + if cmp -s src/omega_program_ir.c $(PIR_DIR)/mut_$$name.c; then echo "mutant $$name: sed changed nothing"; exit 1; fi; \ + $(CC) $(RESOLVE_FLAGS) -Isrc -o $(PIR_DIR)/mut_$$name tests/test_omega_program_ir.c $(PIR_DIR)/mut_$$name.c $(PIR_STACK); \ + rc=0; $(PIR_DIR)/mut_$$name > $(PIR_DIR)/mut_$$name.out 2>&1 || rc=$$?; \ + if [ $$rc -eq 0 ] || ! grep -q "^FAIL $$chk\$$" $(PIR_DIR)/mut_$$name.out; then echo "mutant $$name NOT killed by $$chk (exit $$rc)"; tail -5 $(PIR_DIR)/mut_$$name.out; exit 1; fi; \ + echo "mutant $$name: killed by $$chk (exit $$rc)"; }; \ + pmut ir-tag tag ir-refuses-wrong-tag ' if (!tag) return -1;'; \ + pmut ir-trailing trailing ir-refuses-trailing-bytes ' if (r.bad) { memset(out, 0, sizeof *out); return -1; }'; \ + pmut ir-id id ir-refuses-a-type-the-program-id-cannot-take ' (void)0;' + @set -eu; sed '/VC1I:truncated/c\ if (r->bad) { r->bad = 1; return NULL; }' src/omega_program_ir.c > $(PIR_DIR)/mut_ir-truncated.c; \ + if cmp -s src/omega_program_ir.c $(PIR_DIR)/mut_ir-truncated.c; then echo "mutant ir-truncated: sed changed nothing"; exit 1; fi; \ + $(CC) $(RESOLVE_FLAGS) -Isrc -O1 -g -fsanitize=address -o $(PIR_DIR)/mut_ir-truncated tests/test_omega_program_ir.c $(PIR_DIR)/mut_ir-truncated.c $(PIR_STACK); \ + rc=0; $(PIR_DIR)/mut_ir-truncated > $(PIR_DIR)/mut_ir-truncated.out 2>&1 || rc=$$?; \ + if [ $$rc -eq 0 ] || ! grep -q 'AddressSanitizer: heap-buffer-overflow' $(PIR_DIR)/mut_ir-truncated.out; then echo "mutant ir-truncated NOT killed (exit $$rc)"; tail -5 $(PIR_DIR)/mut_ir-truncated.out; exit 1; fi; \ + echo "mutant ir-truncated: killed by AddressSanitizer heap-buffer-overflow on a truncated blob (exit $$rc)" + @set -eu; sed '/VC1I:steps/c\ if (r.bad) { memset(out, 0, sizeof *out); return -1; }' src/omega_program_ir.c > $(PIR_DIR)/mut_ir-steps.c; \ + if cmp -s src/omega_program_ir.c $(PIR_DIR)/mut_ir-steps.c; then echo "mutant ir-steps: sed changed nothing"; exit 1; fi; \ + $(CC) $(RESOLVE_FLAGS) -Isrc -O1 -g -fsanitize=address -o $(PIR_DIR)/mut_ir-steps tests/test_omega_program_ir.c $(PIR_DIR)/mut_ir-steps.c $(PIR_STACK); \ + rc=0; $(PIR_DIR)/mut_ir-steps > $(PIR_DIR)/mut_ir-steps.out 2>&1 || rc=$$?; \ + if [ $$rc -eq 0 ] || ! grep -q 'AddressSanitizer: heap-buffer-overflow' $(PIR_DIR)/mut_ir-steps.out; then echo "mutant ir-steps NOT killed (exit $$rc)"; tail -5 $(PIR_DIR)/mut_ir-steps.out; exit 1; fi; \ + echo "mutant ir-steps: killed by AddressSanitizer heap-buffer-overflow on a huge step count (exit $$rc)" + @echo "test-program-ir: PASS (round trip, refusals, ASan/UBSan clean, 5 mutants killed)" + +# The resolver mutants for the VC1 stage 6 guards (genesis list at admission, mandatory blob store +# in the build domain, program-id recompute, IR-only digest kind in the build domain), plus the two +# genesis guards in the store. Same method as test-resolve and test-vcstore: copy ONE source file, +# break ONE tagged line, require the named check to fail. Run after the two main targets. +.PHONY: test-resolve-genesis test-vcstore-genesis +test-resolve-genesis: test-resolve + @set -eu; build_mutant() { name=$$1; tag=$$2; chk=$$3; repl=$$4; f=src/omega_resolve.c; \ + sed "/VC1R:$$tag/c\\$$repl" $$f > $(RESOLVE_DIR)/mut_$$name.c; \ + if cmp -s $$f $(RESOLVE_DIR)/mut_$$name.c; then echo "mutant $$name: sed changed nothing"; exit 1; fi; \ + srcs=""; for s in $(RESOLVE_SRC); do if [ "$$s" = "$$f" ]; then srcs="$$srcs $(RESOLVE_DIR)/mut_$$name.c"; else srcs="$$srcs $$s"; fi; done; \ + $(CC) $(RESOLVE_FLAGS) -DOSCV_PATH='"$(OUT_DIR)/compiler/oscv"' -o $(RESOLVE_DIR)/mut_$$name tests/test_omega_resolve.c $$srcs $(RESOLVE_REST); \ + rc=0; $(RESOLVE_DIR)/mut_$$name $$name > $(RESOLVE_DIR)/mut_$$name.out 2>&1 || rc=$$?; \ + if [ $$rc -ne 1 ] || ! grep -q "^MUTANT $$name KILLED by $$chk\$$" $(RESOLVE_DIR)/mut_$$name.out; then \ + echo "mutant $$name NOT killed by $$chk (exit $$rc)"; tail -5 $(RESOLVE_DIR)/mut_$$name.out; exit 1; fi; \ + echo "mutant $$name: killed by $$chk (exit 1)"; }; \ + build_mutant admit-genesis-list admit-genesis-list admit-genesis-refuses-unlisted-record ' if (0) { /* VC1R:admit-genesis-list */'; \ + build_mutant build-needs-blobs build-needs-blobs refuse-build-domain-without-blob-store ' if (0 && !r->fetch_blob) { /* VC1R:build-needs-blobs */'; \ + build_mutant program-id program-id refuse-ir-that-does-not-recompute-to-semantic-id ' if (omega_program_ir_recompute_id(bb, bl, pid) != 0) { /* VC1R:program-id */'; \ + build_mutant ir-kind ir-kind refuse-source-digest-record-in-build-domain ' } else if (0) { /* VC1R:ir-kind */' + @echo "test-resolve-genesis: PASS (4 mutants killed)" +test-vcstore-genesis: test-vcstore + @set -eu; build_mutant() { name=$$1; tag=$$2; chk=$$3; repl=$$4; \ + sed "/VC1S:$$tag/c\\$$repl" src/omega_vcstore.c > $(VCSTEST_DIR)/mut_$$name.c; \ + if cmp -s src/omega_vcstore.c $(VCSTEST_DIR)/mut_$$name.c; then echo "mutant $$name: sed changed nothing"; exit 1; fi; \ + $(CC) $(VCSTEST_FLAGS) -o $(VCSTEST_DIR)/mut_$$name tests/test_omega_vcstore.c $(VCSTEST_DIR)/mut_$$name.c src/sha256.c; \ + rc=0; $(VCSTEST_DIR)/mut_$$name $$name > $(VCSTEST_DIR)/mut_$$name.out 2>&1 || rc=$$?; \ + if [ $$rc -ne 1 ] || ! grep -q "^MUTANT $$name KILLED by $$chk\$$" $(VCSTEST_DIR)/mut_$$name.out; then \ + echo "mutant $$name NOT killed by $$chk (exit $$rc)"; tail -5 $(VCSTEST_DIR)/mut_$$name.out; exit 1; fi; \ + echo "mutant $$name: killed by $$chk (exit 1)"; }; \ + build_mutant insert-genesis insert-genesis insert-bootstrap-unlisted-refused ' if (0) { free(rec); return OMEGA_VCS_GENESIS_NOT_LISTED; }'; \ + build_mutant load-genesis load-genesis load-bootstrap-unlisted-refused ' if (0) { rc = OMEGA_VCS_GENESIS_NOT_LISTED; break; }' + @echo "test-vcstore-genesis: PASS (2 mutants killed)" + +# VC1 stage 6: library admissions in Crumbline and in omegatool go through the Verified Crumb bridge +# (src/omega_vc_bridge.c), so both link the bridge, the resolver, the receipt reader and the store. +# `+=` at the end is read by the recipes above (they use the variables lazily); the extra +# prerequisite rules below make sure the new objects are built first. +VC_BRIDGE_CORE = omega_program_ir omega_vc_bridge omega_resolve omega_receipt omega_blake3 omega_vcstore +VC_BRIDGE_OBJS = $(addprefix $(OUT_DIR)/,$(addsuffix .o,$(VC_BRIDGE_CORE))) +LEARNER_CORE += $(VC_BRIDGE_CORE) +$(LEARNER): $(VC_BRIDGE_OBJS) +SRCS += $(patsubst %,src/%.c,$(VC_BRIDGE_CORE)) +$(TARGET): $(VC_BRIDGE_OBJS) +-include $(VC_BRIDGE_OBJS:.o=.d) + +# test-genesis-real: the REAL VC-GENESIS-1 (src/omega_genesis.h) pinned against its audit record +# docs/osc/VC-GENESIS-1.md. Two mutants of the real header must be caught. +.PHONY: test-genesis-real +GENREAL_DIR = $(OUT_DIR)/genesis-real-test +GENREAL_STACK = src/sha256.c $(RESOLVE_PROGRAM_STACK) +test-genesis-real: tests/test_omega_genesis.c src/omega_genesis.h docs/osc/VC-GENESIS-1.md tests/vc_fixture.h + @mkdir -p $(GENREAL_DIR) + $(CC) $(RESOLVE_FLAGS) -o $(GENREAL_DIR)/test_omega_genesis tests/test_omega_genesis.c $(GENREAL_STACK) + $(GENREAL_DIR)/test_omega_genesis + @set -eu; gmut() { name=$$1; chk=$$2; expr=$$3; \ + mkdir -p $(GENREAL_DIR)/$$name; sed "$$expr" src/omega_genesis.h > $(GENREAL_DIR)/$$name/omega_genesis.h; \ + if cmp -s src/omega_genesis.h $(GENREAL_DIR)/$$name/omega_genesis.h; then echo "mutant $$name: sed changed nothing"; exit 1; fi; \ + $(CC) -I$(GENREAL_DIR)/$$name $(RESOLVE_FLAGS) -o $(GENREAL_DIR)/mut_$$name tests/test_omega_genesis.c $(GENREAL_STACK); \ + rc=0; $(GENREAL_DIR)/mut_$$name > $(GENREAL_DIR)/mut_$$name.out 2>&1 || rc=$$?; \ + if [ $$rc -ne 1 ] || ! grep -q "^FAIL $$chk\$$" $(GENREAL_DIR)/mut_$$name.out; then echo "mutant $$name NOT killed by $$chk (exit $$rc)"; tail -5 $(GENREAL_DIR)/mut_$$name.out; exit 1; fi; \ + echo "mutant $$name: killed by $$chk (exit 1)"; }; \ + gmut gr-count-one member-count-is-the-pinned-audit-result 's|^#define OMEGA_GENESIS_1_COUNT 0u|#define OMEGA_GENESIS_1_COUNT 1u|'; \ + gmut gr-contains-everything no-program-we-know-and-no-junk-id-is-a-member 's|if (!id \|\| memcmp(id, zero, 32) == 0) return 0;|(void)zero; return 1;|'; \ + gmut gr-name-changed set-name-is-vc-genesis-1 's|#define OMEGA_GENESIS_SET_NAME "VC-GENESIS-1"|#define OMEGA_GENESIS_SET_NAME "VC-GENESIS-2"|' + @echo "test-genesis-real: PASS (pinned set and audit record agree, 3 mutants killed)" + +# test-vc-bridge (VC1 stage 6 fix): the bridge verifies and recomputes the id itself, and what it +# mints cannot satisfy a build import. Each VC1B-tagged guard is broken in a copy and the named +# check must FAIL. The tag lines are single lines, so sed replaces whole lines. +.PHONY: test-vc-bridge +VCB_DIR = $(OUT_DIR)/vc-bridge-test +VCB_STACK = src/omega_library.c src/omega_receipt.c src/omega_blake3.c src/sha256.c src/omega_vcstore.c $(RESOLVE_PROGRAM_STACK) +test-vc-bridge: tests/test_omega_vc_bridge.c src/omega_vc_bridge.c src/omega_vc_bridge.h src/omega_resolve.c src/omega_resolve.h + @mkdir -p $(VCB_DIR) + $(CC) $(RESOLVE_FLAGS) -o $(VCB_DIR)/test_omega_vc_bridge tests/test_omega_vc_bridge.c src/omega_vc_bridge.c src/omega_resolve.c $(VCB_STACK) + $(VCB_DIR)/test_omega_vc_bridge + $(CC) $(RESOLVE_FLAGS) -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all -o $(VCB_DIR)/test_omega_vc_bridge_asan tests/test_omega_vc_bridge.c src/omega_vc_bridge.c src/omega_resolve.c $(VCB_STACK) + $(VCB_DIR)/test_omega_vc_bridge_asan + @set -eu; vmut() { name=$$1; which=$$2; chk=$$3; repl=$$4; \ + sed "/VC1B:$$name/c\\$$repl" src/$$which.c > $(VCB_DIR)/mut_$$name.c; \ + if cmp -s src/$$which.c $(VCB_DIR)/mut_$$name.c; then echo "mutant $$name: sed changed nothing"; exit 1; fi; \ + if [ $$which = omega_vc_bridge ]; then a=$(VCB_DIR)/mut_$$name.c; b=src/omega_resolve.c; else a=src/omega_vc_bridge.c; b=$(VCB_DIR)/mut_$$name.c; fi; \ + $(CC) $(RESOLVE_FLAGS) -o $(VCB_DIR)/mut_$$name tests/test_omega_vc_bridge.c $$a $$b $(VCB_STACK); \ + rc=0; $(VCB_DIR)/mut_$$name > $(VCB_DIR)/mut_$$name.out 2>&1 || rc=$$?; \ + if [ $$rc -eq 0 ] || ! grep -q "^FAIL $$chk\$$" $(VCB_DIR)/mut_$$name.out; then echo "mutant $$name NOT killed by $$chk (exit $$rc)"; tail -5 $(VCB_DIR)/mut_$$name.out; exit 1; fi; \ + echo "mutant $$name: killed by $$chk (exit $$rc)"; }; \ + vmut verify omega_vc_bridge refuse-corrupted-program-even-with-flags-set ' (void)rep;'; \ + vmut id omega_vc_bridge refuse-forged-program-id-at-admit ' (void)rid_;'; \ + vmut cap omega_vc_bridge refuse-bridge-record-in-build-domain ' ou32(&o, 0);'; \ + vmut build-refuses-selfminted omega_resolve refuse-bridge-record-in-build-domain ' (void)lists_selfminted_cap;' + @echo "test-vc-bridge: PASS (bridge verifies and recomputes the id, bridge records refused in build, ASan/UBSan clean, 4 mutants killed)" diff --git a/docs/osc/OSC-VC1-IMPORT.md b/docs/osc/OSC-VC1-IMPORT.md index 0da14d676..b6919296b 100644 --- a/docs/osc/OSC-VC1-IMPORT.md +++ b/docs/osc/OSC-VC1-IMPORT.md @@ -1,9 +1,12 @@ -# OSC import resolution (VC1 stage 4) +# OSC import resolution (VC1 stages 4 and 6) Authority: ADR 0029 and aien-protocols `specs/verified-crumb/SPEC.md` (sections 5 to 8). This file records what the C -implementation does and where it stops. Code: `src/omega_resolve.c` (resolver, lock, identity), `src/omega_receipt.c` -(receipt reader), `src/omega_blake3.c`, `src/omega_resolve_osc.c` (front end adapter), `src/oscv_main.c` (`oscv` driver). -Tests: `make test-resolve`. +implementation does and where it stops. Code: `src/omega_resolve.c` (resolver, lock, identity, the admission door), +`src/omega_receipt.c` (receipt reader), `src/omega_blake3.c`, `src/omega_genesis.h` (the pinned Genesis Set), +`src/omega_program_ir.c` (the IR the program id is recomputed from), `src/omega_resolve_osc.c` (front end adapter), +`src/oscv_main.c` (`oscv` driver), `src/omega_vc_bridge.c` (how Crumbline and discovery admit programs). +Tests: `make test-resolve test-resolve-genesis test-vcstore test-vcstore-genesis test-genesis test-genesis-real +test-program-ir`. ## Grammar @@ -21,16 +24,37 @@ compile even with zero imports (so a lock line with no import is caught). 1. Declaration check, before any store access: every import needs a lock line (DEPENDENCY_NOT_PINNED); every lock line needs an import (UNDECLARED_IMPORT). Direction chosen: an extra lock line is refused, not ignored. -2. Per node: lock line -> store get by semantic id -> identity (record id, optional blob digest, lock receipt equals - record receipt) -> kind (BOOTSTRAP needs `allow_genesis`) -> taint capability -> verifier profile and version -> - receipt check (SPEC 5.1 rules 1 to 6) -> edges (each dependency repeats the chain; contract ids must match; a node - already on the path is DEPENDENCY_CYCLE). +2. Per node: lock line -> store get by semantic id -> identity (the record names the same semantic id) -> + source/IR recheck (below) -> the lock's receipt equals the record's receipt -> taint capability -> verifier profile and version -> receipt check (SPEC 5.1 rules 1 to + 6) -> edges (each dependency repeats the chain; contract ids must match; a node already on the path is + DEPENDENCY_CYCLE) -> genesis gate for a BOOTSTRAP record (below, checked last so cycle and dependency refusals + still come first). 3. The closure is sorted by semantic id; its digest is `SHA-256("OMEGA.CLOSURE.V1" 0x00 || u32be n || n x (semantic_id || receipt_id || kind))`. Any refusal returns no closure. Refusal codes: UNVERIFIED_DEPENDENCY, MISSING_RECEIPT, RECEIPT_HASH_MISMATCH, DEPENDENCY_NOT_PINNED, DEPENDENCY_CYCLE, STALE_RECEIPT, UNDECLARED_IMPORT, TAINTED_ARTIFACT, UNKNOWN_VERIFIER_PROFILE, VERIFIER_TOO_OLD (enum in -`src/omega_resolve.h`). +`src/omega_resolve.h`). A BOOTSTRAP record that is not on the Genesis Set comes back as UNVERIFIED_DEPENDENCY with the +store code GENESIS_NOT_LISTED. + +## Mandatory source/IR recheck (SPEC 6 step 3) + +In the build domain the resolver must be given a source/IR blob store (`fetch_blob`; `oscv` reads `blobs/` next to the +source, or `--blobs DIR`). Without one, every node is refused ("needs the source or IR store"). For each record: + +1. The blob named by `source_or_ir_digest` is fetched and SHA-256 of its bytes must equal that digest. +2. A record whose digest kind is OSC source is refused in the build domain: a source digest cannot be turned back into + a program id by this resolver, so only an IR digest can satisfy the recheck. +3. For an IR digest, the program id is RECOMPUTED from the IR bytes (`omega_program_ir_recompute_id`, which decodes the + IR exactly and calls `omega_program_compute_id`) and compared with the record's semantic id. A mismatch, or bytes + that are not a valid IR, are UNVERIFIED_DEPENDENCY ("does not recompute to its semantic id"). + +The IR layout is in `src/omega_program_ir.h`. It carries exactly the fields the program id binds, so recomputing it is +possible from the blob alone. + +The dev domain may leave the store out. Then none of the three steps runs and the output stays tainted (as every dev +output is). If a store is given, the dev domain runs steps 1 and 3 too; step 2 (the source kind refusal) is a build +domain rule only. ## Build identity @@ -39,20 +63,69 @@ the one place the closure digest enters a build's identity. `oscv` prints it. ## Domains -`--domain build` (default) and `--domain dev` run the same checks; the domain never loosens one. omega-dev output -is TAINTED: its artifact header says `tainted 1`, its build id commits to the domain, and `omega_resolve_admit` refuses -to insert it (and refuses any record that lists the capability `omega-dev.taint`). A record listing that capability -can never satisfy an import in either domain. - -BOOTSTRAP (genesis) records satisfy an import only with `allow_genesis`. That is a field inside the resolver library. -The `oscv` driver has no option, file or environment variable that sets it (the old `--allow-genesis` flag was removed), so a store -that holds only BOOTSTRAP records always yields UNVERIFIED_DEPENDENCY in both domains. The stage 6 genesis loader will be the -only caller that sets the field. A BOOTSTRAP record may NOT depend on a -VERIFIED record (the audited base must be closed over itself); a VERIFIED record may depend on BOOTSTRAP. +`--domain build` (default) and `--domain dev` run the same checks except the blob store requirement above; the domain +never loosens any other one. omega-dev output is TAINTED: its artifact header says `tainted 1`, its build id commits to +the domain, and `omega_resolve_admit` refuses to insert it (and refuses any record that lists the capability +`omega-dev.taint`). A record listing that capability can never satisfy an import in either domain. + +## The artifact header is unauthenticated; the receipt is the gate + +The text header `omega_artifact_meta_text` writes (domain, tainted mark, IR digest, closure digest, build id) is +integrity checked only against itself: `omega_artifact_meta_parse` recomputes the build id and checks the taint mark +agrees with the domain. Anyone who can write the file can rewrite the domain and the mark and recompute the build id, +and the parse will accept it. Nothing in the store or the resolver trusts that header to GRANT anything. What gates a +record into the store is the receipt (SPEC 5.1), checked by `omega_resolve_admit`; the `origin` argument of admit is +optional and only ever makes admission stricter (a tainted origin refuses). So the header is a label and a tripwire, +not a credential. If it is ever made to gate something, it must be signed or bound to the receipt first; until then it +must not be read as proof of the domain it names. (ADR 0029 carry item 6: documented, not changed.) + +## Genesis (BOOTSTRAP) records + +A BOOTSTRAP record is trusted without a receipt, so which ids may be BOOTSTRAP is a pinned list in source: +`src/omega_genesis.h`, set name VC-GENESIS-1, described and audited in `docs/osc/VC-GENESIS-1.md`. It is currently +EMPTY (the audit found no program that must exist without a receipt). The list is the only input: + +- the store refuses to insert or to load a BOOTSTRAP record whose id is not listed (GENESIS_NOT_LISTED); +- `omega_resolve_admit_genesis` refuses an unlisted id and is the only code that may create a BOOTSTRAP record; +- the resolver refuses a BOOTSTRAP record that is not listed, whatever put it in the store. + +`allow_genesis` no longer exists in `OmegaResolver`, `oscv` has no genesis option, and nothing is read from a file, a flag +or the environment. A BOOTSTRAP record may NOT depend on a VERIFIED record (the audited base must be closed over itself); +a VERIFIED record may depend on BOOTSTRAP. + +### The two raw insert functions are private + +`omega_vcstore_insert` and `omega_vcstore_insert_bootstrap` are declared only in `src/omega_vcstore_priv.h`. A record +enters a store only through `omega_resolve_admit` (receipt checked) or `omega_resolve_admit_genesis` (list checked). +`make test-resolve` scans `src/` and `tools/` and fails if any file other than the store and the resolver names either +function or includes that header, or if anything names the admit door, the list or a genesis switch outside its owners. + +### Crumbline and discovery + +`src/crumbline/cl_program.c` and `src/omega_discovery.c` no longer call `omega_library_insert` with a bare hash. Both go +through `src/omega_vc_bridge.c`, which builds the program's canonical Verified Crumb, mints an evidence receipt of kind +`host-v1`, tier HOST_TEST, and lets `omega_resolve_admit` check it before the library entry is made (ADR 0029 Decision +11). The bridge itself runs `omega_program_verify` on a private copy of the program and recomputes the program id from +the IR it stores, and refuses on failure; it does not trust the caller's `is_verified` flag or `program_id`. The +receipt is still SELF-MINTED: the code asking for admission writes it, so it proves the record is well formed and +accepted by the resolver chain, not that an independent verifier re-ran anything. For that reason every record the +bridge mints lists the capability `omega-bridge.selfminted`, and the build-domain resolver refuses any record that +lists it: a bridge record can satisfy a dev import (tainted) but never `oscv --domain build`. `make test-vc-bridge` +reproduces the hand test that found the earlier hole (corrupt the machine code, flip the flags, forge an id) and kills +four mutants. This is also why VC-GENESIS-1 can be empty (see its audit). The test +`library-insert-in-src-is-named-only-by-the-library-and-the-bridge` keeps new direct inserts out of `src/`. + +## The compiler hook cannot be swapped for another + +`osc_compile_imports` takes any function pointer, so the compiler cannot enforce what is installed. The check lives in +CI: `import-hook-is-installed-only-by-the-real-driver` fails if any file in `src/` or `tools/` other than the compiler +front end and `oscv` calls it, and `the-real-driver-installs-the-real-resolver-hook` fails unless `oscv` installs +`omega_osc_import_hook`. `src/compiler` itself is unchanged. ## No escape hatch -`oscv --blobs` is optional: leaving it out skips only the recheck of source digests against stored blobs. +`oscv --blobs` points the resolver at a source/IR store. Leaving it out is allowed only in the dev domain, where the +output is tainted; in the build domain the default `blobs/` is used and a missing store is a refusal. No `unsafe import`, no skip switch, no environment variable. `make test-resolve` greps the compiler and resolver sources for forbidden words and runs `oscv` with unknown switches and with environment variables set. @@ -61,8 +134,17 @@ sources for forbidden words and runs `oscv` with unknown switches and with envir - Receipts are hashes, not signatures. The receipt reader recomputes the BLAKE3 id from the canonical bytes (verified against three receipts written by the Rust implementation) but does not replicate ledger or lease store - bindings or chain walking of aien-proof. -- The program id is not recomputed from source; only the source digest is checked, and only when a blob store is given - (stage 6 supplies it). + bindings or chain walking of aien-proof. The Rust cross-check for the lease, ledger and mutation cases the three + fixtures do not cover is not in this stage. +- UNDECLARED_IMPORT still means "lock line the source never imports" here. Reconciling that with SPEC 6 step 8 and + aien-closure, and implementing step 8 proper (the record's stored source imports against `dependencies[]`), is not in + this stage. +- A source digest cannot satisfy the build domain (it cannot be recomputed to a program id here), so a record kept only + as OSC source needs an IR record beside it. +- The bridge receipt is self-minted HOST_TEST evidence (see above), not an independent verification. It cannot + satisfy a build import. Real aien-test receipts for Crumbline and discovery programs are owed (ADR 0029 Decisions 5 + and 11), as is a resolver-side minimum profile for the build domain (a host-v1 receipt written by hand still + satisfies a build import for a record that does not carry the bridge capability). +- `omega_library_insert_bootstrap` still exists on the in-memory library and is not tied to the Genesis Set. No code calls + it; it is not a way into the Verified Crumb Store. - A dependency cycle cannot be built through the store API; it is refused when a tampered store holds one. -- `cl_program.c` and `omega_discovery.c` are not migrated here (stage 6). diff --git a/docs/osc/VC-GENESIS-1.md b/docs/osc/VC-GENESIS-1.md new file mode 100644 index 000000000..1fe44992a --- /dev/null +++ b/docs/osc/VC-GENESIS-1.md @@ -0,0 +1,109 @@ +# VC-GENESIS-1: the Genesis Set + +Authority: ADR 0029 Decision 8 (BOOTSTRAP admission) and Decision 11 (no direct library insert). +Code: `src/omega_genesis.h` (the pinned table, nothing else), enforced in `src/omega_vcstore.c` and +`src/omega_resolve.c`. Tests: `make test-genesis-real test-genesis test-vcstore test-resolve test-vc-bridge`. + +Members: 0 + +## What the Genesis Set is + +The Verified Crumb Store holds two kinds of record. A VERIFIED record is let in only together with a +receipt that the resolver checked (SPEC 5.1). A BOOTSTRAP record is let in with no receipt, because +something has to start the chain of trust, and it is trusted only because a person audited it by hand. +The Genesis Set is the complete list of semantic ids (Omega program ids) that are allowed to be +BOOTSTRAP records. It is the one start of trust that does not come from a receipt, so it is part of the +trusted computing base and is kept as small as the audit allows. + +How it is enforced, and the only ways: + +- `omega_vcstore_insert_bootstrap` refuses a record whose semantic id is not listed + (`GENESIS_NOT_LISTED`). +- `omega_vcstore_load` refuses a store file that holds a BOOTSTRAP record whose id is not listed + (`GENESIS_NOT_LISTED`), so a hand edited file cannot add a member. +- `omega_resolve_admit_genesis` (the only door a BOOTSTRAP record can use to enter a store) refuses an + unlisted id before the store is touched. +- The resolver, when it walks an import, refuses any BOOTSTRAP record that is not listed, whatever put it + in the store. +- The resolver has no genesis permission field. Membership of this list is the only input. There is no + flag, no environment variable, no file and no store content that adds a member. The list is a + compile time constant in `src/omega_genesis.h`. +- No wildcard and no prefix match: an id is a member only if all 32 bytes equal a row. The all-zero id is + never a member. + +Changing the set (adding or removing a member, or renaming it) is an ADR level change. Two tests pin it: +`tests/test_omega_genesis.c` fails until the table and this file name exactly the same members, and until +the count matches the number in the "Members:" line above. + +## Members + +None. VC-GENESIS-1 is empty. + +| id (64 hex) | what it is | why it is trusted without a receipt | audit | +|---|---|---|---| +| (no rows) | | | | + +## Audit + +Question: which programs must exist in the store without a receipt so that Crumbline and discovery can +start? The audit looked at every place that creates or admits a program and at every place that asks for +a BOOTSTRAP record. + +Method: read the code, do not assume. Searches run over `src/`, `tools/` and `tests/` for +`omega_library_insert_bootstrap`, `omega_vcstore_insert_bootstrap`, `omega_resolve_admit_genesis`, +`OMEGA_LIB_ADMISSION_BOOTSTRAP`, `OMEGA_VCS_ADMISSION_BOOTSTRAP` and `bootstrap` generally. + +Findings: + +1. `src/crumbline/cl_program.c` (the Crumbline learner) admits programs it has just synthesized and + checked with `omega_program_verify`. Before stage 6 it called `omega_library_insert` with a bare hash. + It never calls a bootstrap function. It starts with an EMPTY library and builds up. Stage 6 migrates it + through `src/omega_vc_bridge.c`, which now runs `omega_program_verify` and recomputes the program id + itself, and then writes a SELF-MINTED receipt (see the next paragraph). It needs no BOOTSTRAP record. +2. `src/omega_discovery.c` mines abstractions from a corpus of programs the caller already holds. It + admitted a discovered abstraction with a bare hash. A discovered abstraction is a program with a + verification result, so it too goes through the bridge. It needs no BOOTSTRAP record. +3. Nothing in `src/`, `tools/` or the test drivers calls `omega_library_insert_bootstrap`, + `omega_vcstore_insert_bootstrap` (outside the store and the resolver) or `omega_resolve_admit_genesis`. + The only users of the BOOTSTRAP kind before stage 6 were unit tests that exercised the kind itself. +4. The OSC import path (`oscv`) resolves imports from a store that someone else filled. It never creates + a record. Its test store holds VERIFIED records with receipts. +5. The primitive operations and the type constructors that every program is built from are part of the C + program (`omega_program.c`), not records in the store. A record exists only for a program that has a + body, a contract and evidence. There is no record that the language needs in order to compile its + first program. + +WHY THE SET IS EMPTY, stated plainly: it is empty because the bridge supplies Crumbline and discovery +programs with a receipt that the bridge writes itself (kind host-v1, tier HOST_TEST). That receipt is +SELF-MINTED: the code asking for admission writes it, and no independent run (aien-test) backs it. +Without the bridge those programs would have needed either a real receipt, which does not exist yet, or +BOOTSTRAP status, which would have made them audited Genesis members. So the empty set is honest only +together with these two facts, both enforced by tests (`make test-vc-bridge`): the bridge verifies the +program and recomputes its id before it mints anything, and every record the bridge mints lists the +capability `omega-bridge.selfminted`, which the build domain refuses. A bridge record can sit in a +store and satisfy a dev import, but it can never satisfy `oscv --domain build`. Real aien-test receipts +for these programs are owed (ADR 0029 Decisions 5 and 11); until they exist, the store admission of +Crumbline and discovery programs is a dev-grade fact, not a build-grade one. + +Decision: the minimum that bootstraps Crumbline and discovery is nothing. The set is EMPTY. No BOOTSTRAP +record is trusted without being listed, and a BOOTSTRAP record in any store is refused by every door. + +What an empty set costs: the BOOTSTRAP admission path exists and is tested (with a one member variant +build, `make test-genesis`) but nothing uses it. The day a real member is needed it is added by an ADR +level change that edits `src/omega_genesis.h` and adds a row and an audit entry to this file; the test +refuses any other order of events. + +Audit record: this section, dated 2026-10-02, stage 6 of VC1. The audit hash a future member would carry +in its `receipt_id` field is the SHA-256 of that member's audit entry text in this file. + +## What is NOT claimed + +- The empty set does not mean the learner's programs are independently qualified. The bridge is a named + trust root: it mints self-attested HOST_TEST receipts that the build domain refuses. Removing the + bridge from the store path, or replacing its receipts with real aien-test receipts, is the carry item + that would make the claim stronger. +- The empty set does not make the library's own `omega_library_insert_bootstrap` safe: that function + works on the in-memory library, not on the Verified Crumb Store, and no caller uses it. It is listed as + an open item in OSC-VC1-IMPORT.md. +- Nothing here checks a signature. A BOOTSTRAP record would be trusted on the strength of a human audit + of the program and the commit that added its id to the table. diff --git a/mk/program-id.mk b/mk/program-id.mk index d1f6292f5..1a28bbf6b 100644 --- a/mk/program-id.mk +++ b/mk/program-id.mk @@ -1,11 +1,14 @@ # Program identity v2 (spec/program-identity.md). Physics-free; links the Visor core # object set + discovery + src/visor + src/language. Run from the repo root. +# The test admits discovery candidates through the Verified Crumb bridge (VC1 stage 6), so it also +# links the bridge, the resolver, the receipt reader and the store. PROGRAM_ID_TEST = $(OUT_DIR)/tests-program/test_program_id +PROGRAM_ID_BRIDGE_OBJS = $(addprefix $(OUT_DIR)/,$(addsuffix .o,omega_program_ir omega_vc_bridge omega_resolve omega_receipt omega_blake3 omega_vcstore)) $(PROGRAM_ID_TEST): tests/program/test_program_id.c $(VISOR_CORE_OBJS) $(OUT_DIR)/omega_discovery.o \ - $(VISOR_OBJS) $(LANG_OBJS) + $(VISOR_OBJS) $(LANG_OBJS) $(PROGRAM_ID_BRIDGE_OBJS) mkdir -p $(dir $@) - $(CC) $(VISOR_CFLAGS) -o $@ $< $(VISOR_CORE_OBJS) $(OUT_DIR)/omega_discovery.o $(VISOR_OBJS) $(LANG_OBJS) + $(CC) $(VISOR_CFLAGS) -o $@ $< $(VISOR_CORE_OBJS) $(OUT_DIR)/omega_discovery.o $(VISOR_OBJS) $(LANG_OBJS) $(PROGRAM_ID_BRIDGE_OBJS) .PHONY: test-program-id test-program-id: $(PROGRAM_ID_TEST) diff --git a/src/crumbline/cl_program.c b/src/crumbline/cl_program.c index 8d313f0c3..abfa97ac0 100644 --- a/src/crumbline/cl_program.c +++ b/src/crumbline/cl_program.c @@ -177,10 +177,18 @@ void cl_bank_digest(const ClBank *bank, uint8_t out[CL_DIGEST_BYTES]) { int cl_library_init(ClLearnerLibrary *l) { memset(l, 0, sizeof(*l)); - return omega_library_init(&l->lib); + if (omega_vc_bridge_init(&l->bridge) != 0) return -1; + if (omega_library_init(&l->lib) != 0) { + omega_vc_bridge_destroy(&l->bridge); + return -1; + } + return 0; } -void cl_library_destroy(ClLearnerLibrary *l) { omega_library_destroy(&l->lib); } +void cl_library_destroy(ClLearnerLibrary *l) { + omega_library_destroy(&l->lib); + omega_vc_bridge_destroy(&l->bridge); +} int cl_library_admit(ClLearnerLibrary *l, const ClSteps *steps, uint8_t scope_bits, uint32_t op_ref) { if (!l || !steps || steps->n == 0 || l->lib.count >= OMEGA_LIB_MAX_PROGRAMS) return -1; @@ -189,12 +197,12 @@ int cl_library_admit(ClLearnerLibrary *l, const ClSteps *steps, uint8_t scope_bi VerifyReport rep; int rc = -1; if (omega_program_verify(&prog, &rep) == 0 && cl_steps_differential(steps, &prog, NULL, 0, NULL) == 0) { - uint8_t pre[4 + CL_DIGEST_BYTES], receipt[CL_DIGEST_BYTES]; + uint8_t pre[4 + CL_DIGEST_BYTES], evidence[CL_DIGEST_BYTES]; for (int k = 0; k < 4; ++k) pre[k] = (uint8_t)(op_ref >> (8 * k)); cl_steps_digest(steps, pre + 4); - cl_digest("CRUMBLINE-OMEGA-ADMISSION", pre, sizeof(pre), receipt); + cl_digest("CRUMBLINE-OMEGA-ADMISSION", pre, sizeof(pre), evidence); size_t slot = l->lib.count; - if (omega_library_insert(&l->lib, &prog, NULL, 0, receipt) == 0) { + if (omega_vc_bridge_admit(&l->bridge, &l->lib, &prog, NULL, 0, evidence) == 0) { l->steps[slot] = *steps; l->scope_bits[slot] = scope_bits; l->op_ref[slot] = op_ref; diff --git a/src/crumbline/cl_program.h b/src/crumbline/cl_program.h index c1dd822d4..3e29ec0ee 100644 --- a/src/crumbline/cl_program.h +++ b/src/crumbline/cl_program.h @@ -18,6 +18,7 @@ #include "cl_common.h" #include "omega_program.h" #include "omega_library.h" +#include "omega_vc_bridge.h" #define CL_MAX_STEPS 24 #define CL_BANK_MAX 96 @@ -91,6 +92,7 @@ void cl_bank_digest(const ClBank *bank, uint8_t out[CL_DIGEST_BYTES]); typedef struct { OmegaLibrary lib; /* the canonical semantic-object store */ + OmegaVcBridge bridge; /* the Verified Crumb Store and receipts behind lib */ ClSteps steps[OMEGA_LIB_MAX_PROGRAMS]; /* step chain of lib.entries[i] */ uint8_t scope_bits[OMEGA_LIB_MAX_PROGRAMS]; /* widest input magnitude (bits) it was verified over */ uint32_t op_ref[OMEGA_LIB_MAX_PROGRAMS]; /* sealed-side admission reference */ @@ -99,8 +101,10 @@ typedef struct { int cl_library_init(ClLearnerLibrary *l); void cl_library_destroy(ClLearnerLibrary *l); -/* Build, verify (V0+V2 + differential) and insert via omega_library_insert. - * The library receipt hash binds the admission reference and the program. */ +/* Build, verify (V0+V2 + differential), then admit through omega_vc_bridge_admit: the program + * enters the Verified Crumb Store through omega_resolve_admit with a (self-minted HOST_TEST) + * receipt, and the library entry carries that receipt id (ADR 0029 Decision 11). The evidence + * digest binds the admission reference and the program. */ int cl_library_admit(ClLearnerLibrary *l, const ClSteps *steps, uint8_t scope_bits, uint32_t op_ref); /* Add every entry whose verified scope covers inputs of `input_bits` bits. diff --git a/src/omega_discovery.c b/src/omega_discovery.c index b9da5563f..b7dedb31a 100644 --- a/src/omega_discovery.c +++ b/src/omega_discovery.c @@ -291,14 +291,6 @@ bool omega_verify_semantic_preservation(const OmegaProgram *orig, const OmegaPro return true; } -int omega_discovery_admit_to_library(OmegaLibrary *lib, const OmegaAbstractionCandidate *cand, - const uint8_t receipt_hash[32]) { - if (!lib || !cand || !receipt_hash) return -1; /* evidence is mandatory (VC1-LIB) */ - if (!cand->is_verified || !cand->is_nontrivial || cand->compression_score <= 0) return -1; - - return omega_library_insert(lib, &cand->abstraction, NULL, 0, receipt_hash); -} - int omega_demonstrate_search_acceleration(const OmegaProgram *discovered_abstraction, size_t *out_candidates_without, size_t *out_candidates_with) { diff --git a/src/omega_discovery.h b/src/omega_discovery.h index 839efc16d..f5114f221 100644 --- a/src/omega_discovery.h +++ b/src/omega_discovery.h @@ -61,9 +61,8 @@ int omega_refactor_program(const OmegaProgram *orig, const OmegaProgram *abstrac bool omega_verify_semantic_preservation(const OmegaProgram *orig, const OmegaProgram *refactored, const uint64_t *test_inputs, size_t input_count); -/* Admit discovered abstraction into library */ -int omega_discovery_admit_to_library(OmegaLibrary *lib, const OmegaAbstractionCandidate *cand, - const uint8_t receipt_hash[32]); +/* Admitting a discovered abstraction into a library goes through omega_vc_bridge_admit_abstraction + * (src/omega_vc_bridge.h): the Verified Crumb Store admit path with a receipt (VC1 stage 6). */ /* Demonstrate search acceleration on held-out task */ int omega_demonstrate_search_acceleration(const OmegaProgram *discovered_abstraction, diff --git a/src/omega_genesis.h b/src/omega_genesis.h new file mode 100644 index 000000000..2300ef412 --- /dev/null +++ b/src/omega_genesis.h @@ -0,0 +1,67 @@ +#ifndef OMEGA_GENESIS_H +#define OMEGA_GENESIS_H + +/* VC-GENESIS-1: the Genesis Set of ADR 0029 Decision 8 (VC1 stage 6). + * + * The Genesis Set is the enumerated list of semantic ids (Omega program ids) that may exist in + * the Verified Crumb Store as BOOTSTRAP records, that is, without a receipt. It is the only start + * of trust that does not come from a receipt, so it is part of the trusted computing base and is + * kept as small as the audit allows. The audit record, the members and the reason for each are + * in docs/osc/VC-GENESIS-1.md. + * + * HOW IT IS USED (and the only ways): + * - omega_vcstore.c refuses to insert or to load a BOOTSTRAP record whose semantic id is not + * listed here (OMEGA_VCS_GENESIS_NOT_LISTED); + * - omega_resolve.c refuses omega_resolve_admit_genesis for an unlisted id and refuses to let an + * unlisted BOOTSTRAP record satisfy an import. The resolver has no genesis switch of any kind: whether + * a BOOTSTRAP record counts is decided by membership in this table and by nothing else. + * + * NO WAY AROUND IT: the table is a compile-time constant. There is no flag, no environment + * variable, no preprocessor switch, no file and no store content that adds a member. Changing + * the set means editing this file, and every edit is an ADR-level change reviewed as one (the + * source test tests/test_omega_genesis.c pins the count and the table below, and the audit record). + * + * No wildcard and no prefix match: an id is a member only if all 32 bytes equal a row. The + * all-zero id is never a member, whatever the table holds. */ + +#include +#include +#include + +#define OMEGA_GENESIS_SET_NAME "VC-GENESIS-1" + +/* Number of members. The audit (docs/osc/VC-GENESIS-1.md, section "Audit") found that neither + * bootstrap caller of the library (src/crumbline/cl_program.c and src/omega_discovery.c) needs a + * record without a receipt, so the set is empty. */ +#define OMEGA_GENESIS_1_COUNT 0u + +/* One row per member, strictly ascending by memcmp. The row after the last member is an all-zero + * terminator that is never a member (so the array is never zero-sized). */ +static const uint8_t OMEGA_GENESIS_1[OMEGA_GENESIS_1_COUNT + 1u][32] = { + { 0 } +}; + +static inline size_t omega_genesis_count(void) +{ + return OMEGA_GENESIS_1_COUNT; +} + +/* The i-th member (0 <= i < count), or NULL. */ +static inline const uint8_t *omega_genesis_member(size_t i) +{ + const size_t n = OMEGA_GENESIS_1_COUNT; + return i < n ? OMEGA_GENESIS_1[i] : NULL; +} + +/* 1 when id (32 bytes) is a member of VC-GENESIS-1, else 0. NULL and the zero id are not members. */ +static inline int omega_genesis_contains(const uint8_t *id) +{ + static const uint8_t zero[32] = { 0 }; + const size_t n = OMEGA_GENESIS_1_COUNT; + if (!id || memcmp(id, zero, 32) == 0) return 0; + for (size_t i = 0; i < n; i++) + if (memcmp(OMEGA_GENESIS_1[i], id, 32) == 0) return 1; + return 0; +} + +#endif /* OMEGA_GENESIS_H */ diff --git a/src/omega_program.c b/src/omega_program.c index d4a482539..678f08f83 100644 --- a/src/omega_program.c +++ b/src/omega_program.c @@ -100,6 +100,25 @@ static int type_id_for(TypeTag tag, uint16_t width, SemanticId *out) { return rc; } +int omega_program_contract_id(const OmegaProgram *prog, uint8_t out[OMEGA_ID_BYTES]) { + SemanticId tin, tout; + if (!prog || !out || + type_id_for(prog->contract.input_type, prog->contract.input_width, &tin) != 0 || + type_id_for(prog->contract.output_type, prog->contract.output_width, &tout) != 0) + return -1; + static const char domain[] = "aien.vc1.contract.v1"; + uint8_t buf[sizeof(domain) + 4 * OMEGA_ID_BYTES]; + size_t pos = 0; + memcpy(buf, domain, sizeof(domain)); /* includes the terminating 0x00 */ + pos += sizeof(domain); + memcpy(&buf[pos], tin.bytes, OMEGA_ID_BYTES); pos += OMEGA_ID_BYTES; + memcpy(&buf[pos], tout.bytes, OMEGA_ID_BYTES); pos += OMEGA_ID_BYTES; + memcpy(&buf[pos], prog->contract.precondition_id.bytes, OMEGA_ID_BYTES); pos += OMEGA_ID_BYTES; + memcpy(&buf[pos], prog->contract.postcondition_id.bytes, OMEGA_ID_BYTES); pos += OMEGA_ID_BYTES; + sha256_hash(buf, pos, out); + return 0; +} + int omega_program_compute_id(OmegaProgram *prog) { if (!prog) return -1; memset(prog->program_id.bytes, 0, OMEGA_ID_BYTES); diff --git a/src/omega_program.h b/src/omega_program.h index 389fea25e..2b482438a 100644 --- a/src/omega_program.h +++ b/src/omega_program.h @@ -86,6 +86,12 @@ void omega_program_destroy(OmegaProgram *prog); * Returns -1 and zeroes program_id when the program has no body (no identity). */ int omega_program_compute_id(OmegaProgram *prog); +/* Verified Crumb contract_id (aien-protocols specs/verified-crumb/SPEC.md 4.2): + * SHA256("aien.vc1.contract.v1" 0x00 || in_type_id || out_type_id || pre_id || post_id) + * the same four components the program id hashes, with the same type ids. 0 or -1. */ +int omega_program_contract_id(const OmegaProgram *prog, uint8_t out[32]); + + /* The canonical body root id alone (the SemanticId of the body's root object). */ int omega_program_body_root_id(const OmegaProgram *prog, SemanticId *out_root); diff --git a/src/omega_program_ir.c b/src/omega_program_ir.c new file mode 100644 index 000000000..49477529e --- /dev/null +++ b/src/omega_program_ir.c @@ -0,0 +1,108 @@ +/* omega_program_ir.c -- see omega_program_ir.h. */ +#include "omega_program_ir.h" + +#include +#include + + +typedef struct { uint8_t *p; size_t n, cap; int bad; } W; + +static void wput(W *w, const void *d, size_t k) +{ + if (w->bad) return; + if (w->n + k > w->cap) { + size_t nc = (w->n + k) * 2 + 64; + uint8_t *t = realloc(w->p, nc); + if (!t) { w->bad = 1; return; } + w->p = t; + w->cap = nc; + } + memcpy(w->p + w->n, d, k); + w->n += k; +} +static void wu8(W *w, uint8_t v) { wput(w, &v, 1); } +static void wu16(W *w, uint16_t v) { uint8_t t[2] = { (uint8_t)(v >> 8), (uint8_t)v }; wput(w, t, 2); } +static void wu64(W *w, uint64_t v) +{ + uint8_t t[8]; + for (int i = 0; i < 8; i++) t[i] = (uint8_t)(v >> (56 - 8 * i)); + wput(w, t, 8); +} + +int omega_program_ir_encode(const OmegaProgram *prog, uint8_t **out, size_t *len) +{ + if (!prog || !out || !len || !prog->body.has_body || prog->body.step_count > OMEGA_PROGRAM_MAX_STEPS) return -1; + W w = { NULL, 0, 0, 0 }; + wput(&w, OMEGA_PROGRAM_IR_TAG, OMEGA_PROGRAM_IR_TAG_LEN); + wu8(&w, (uint8_t)prog->contract.input_type); + wu16(&w, prog->contract.input_width); + wu8(&w, (uint8_t)prog->contract.output_type); + wu16(&w, prog->contract.output_width); + wput(&w, prog->contract.precondition_id.bytes, 32); + wput(&w, prog->contract.postcondition_id.bytes, 32); + wu16(&w, prog->body.step_count); + for (uint16_t i = 0; i < prog->body.step_count; i++) { + wu8(&w, prog->body.steps[i].op); + wu64(&w, prog->body.steps[i].imm); + } + if (w.bad) { free(w.p); return -1; } + *out = w.p; + *len = w.n; + return 0; +} + +typedef struct { const uint8_t *b; size_t n, pos; int bad; } R; +static const uint8_t *rtake(R *r, size_t k) +{ + if (r->bad || r->n - r->pos < k) { r->bad = 1; return NULL; } /* VC1I:truncated */ + const uint8_t *p = r->b + r->pos; + r->pos += k; + return p; +} +static uint64_t rbe(R *r, size_t k) +{ + const uint8_t *p = rtake(r, k); + uint64_t v = 0; + if (!p) return 0; + for (size_t i = 0; i < k; i++) v = v << 8 | p[i]; + return v; +} + +int omega_program_ir_decode(const uint8_t *bytes, size_t len, OmegaProgram *out) +{ + if (!bytes || !out) return -1; + memset(out, 0, sizeof *out); + R r = { bytes, len, 0, 0 }; + const uint8_t *tag = rtake(&r, OMEGA_PROGRAM_IR_TAG_LEN); + if (!tag || memcmp(tag, OMEGA_PROGRAM_IR_TAG, OMEGA_PROGRAM_IR_TAG_LEN) != 0) return -1; /* VC1I:tag */ + OmegaProgram *p = out; + p->contract.input_type = (TypeTag)rbe(&r, 1); + p->contract.input_width = (uint16_t)rbe(&r, 2); + p->contract.output_type = (TypeTag)rbe(&r, 1); + p->contract.output_width = (uint16_t)rbe(&r, 2); + const uint8_t *pre = rtake(&r, 32), *post = rtake(&r, 32); + uint64_t steps = rbe(&r, 2); + if (r.bad || steps > OMEGA_PROGRAM_MAX_STEPS) { memset(out, 0, sizeof *out); return -1; } /* VC1I:steps */ + memcpy(p->contract.precondition_id.bytes, pre, 32); + memcpy(p->contract.postcondition_id.bytes, post, 32); + p->body.has_body = true; + p->body.step_count = (uint16_t)steps; + for (uint64_t i = 0; i < steps; i++) { + p->body.steps[i].op = (uint8_t)rbe(&r, 1); + p->body.steps[i].imm = rbe(&r, 8); + } + if (r.bad || r.pos != r.n) { memset(out, 0, sizeof *out); return -1; } /* VC1I:trailing */ + if (omega_program_compute_id(p) != 0) { memset(out, 0, sizeof *out); return -1; } /* VC1I:id */ + return 0; +} + +int omega_program_ir_recompute_id(const uint8_t *bytes, size_t len, uint8_t id[32]) +{ + OmegaProgram *p = malloc(sizeof *p); + if (!p) return -1; + int rc = omega_program_ir_decode(bytes, len, p); + if (rc == 0) memcpy(id, p->program_id.bytes, 32); + free(p); + return rc; +} + diff --git a/src/omega_program_ir.h b/src/omega_program_ir.h new file mode 100644 index 000000000..6ccf7f70f --- /dev/null +++ b/src/omega_program_ir.h @@ -0,0 +1,40 @@ +#ifndef OMEGA_PROGRAM_IR_H +#define OMEGA_PROGRAM_IR_H + +/* omega_program_ir: the canonical IR of an Omega program, the bytes behind the Verified Crumb + * field source_or_ir_digest when digest_kind is OMEGA_VC_DIGEST_IR (VC1 stage 6, SPEC 6 step 3). + * + * The program id (omega_program_compute_id) binds exactly: the body steps, the input and output + * types and widths, and the precondition and postcondition ids. The IR carries exactly those + * fields and nothing else (no name, cost or realization: they are metadata), so the id can be + * RECOMPUTED from the IR bytes alone. That recomputation is what lets the resolver check that a + * stored blob really is the program its semantic id names. + * + * Layout (all integers big-endian, no padding): + * "AIEN_OMEGA_PROGRAM_IR_V1" 24 bytes + * u8 input_type u16 input_width u8 output_type u16 output_width + * 32 precondition_id 32 postcondition_id + * u16 step_count (0..OMEGA_PROGRAM_MAX_STEPS) + * step_count x { u8 op, u64 imm } + * Decoding is exact: wrong tag, truncation, trailing bytes, too many steps, or an op or type the + * program id does not accept are refusals. */ + +#include +#include + +#include "omega_program.h" + +#define OMEGA_PROGRAM_IR_TAG "AIEN_OMEGA_PROGRAM_IR_V1" +#define OMEGA_PROGRAM_IR_TAG_LEN 24 + +/* Canonical IR of prog (malloc'd, caller frees). Refuses a program with no body. 0 or -1. */ +int omega_program_ir_encode(const OmegaProgram *prog, uint8_t **out, size_t *len); + +/* Decode the IR into *out (zeroed first, program_id recomputed with omega_program_compute_id). + * 0 or -1; on -1 *out holds nothing to free. */ +int omega_program_ir_decode(const uint8_t *bytes, size_t len, OmegaProgram *out); + +/* Recompute the program id from the IR alone. 0 with id set, or -1. */ +int omega_program_ir_recompute_id(const uint8_t *bytes, size_t len, uint8_t id[32]); + +#endif /* OMEGA_PROGRAM_IR_H */ diff --git a/src/omega_resolve.c b/src/omega_resolve.c index a18ed3b1b..256103a98 100644 --- a/src/omega_resolve.c +++ b/src/omega_resolve.c @@ -9,7 +9,10 @@ #include #include +#include "omega_genesis.h" +#include "omega_program_ir.h" #include "omega_receipt.h" +#include "omega_vcstore_priv.h" #include "sha256.h" /* the shared codes must keep the store's numbers */ @@ -425,6 +428,14 @@ static long wfind(const Walk *w, const uint8_t id[32]) return -1; } +static int lists_selfminted_cap(const OmegaVcView *v) +{ + for (uint32_t i = 0; i < v->n_capabilities; i++) + if (v->capabilities[i].len == sizeof OMEGA_BRIDGE_SELFMINTED_CAPABILITY - 1 && + memcmp(v->capabilities[i].p, OMEGA_BRIDGE_SELFMINTED_CAPABILITY, sizeof OMEGA_BRIDGE_SELFMINTED_CAPABILITY - 1) == 0) return 1; + return 0; +} + static int resolve_node(Walk *w, const uint8_t id[32], const uint8_t *lock_receipt, const char *name, size_t depth) { const OmegaResolver *r = w->r; @@ -453,6 +464,10 @@ static int resolve_node(Walk *w, const uint8_t id[32], const uint8_t *lock_recei rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "the record fetched for %s names a different semantic id", sid); goto out; } + if (!r->fetch_blob && r->domain == OMEGA_DOMAIN_BUILD) { /* VC1R:build-needs-blobs */ + rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "the build domain needs the source or IR store to recompute %s (SPEC 6 step 3), and none was given", sid); + goto out; + } if (r->fetch_blob) { uint8_t *bb = NULL; size_t bl = 0; @@ -460,11 +475,24 @@ static int resolve_node(Walk *w, const uint8_t id[32], const uint8_t *lock_recei int fr = r->fetch_blob(r->blob_ctx, vc->source_or_ir_digest, &bb, &bl); if (fr != 0) { rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "the source or IR named by %s is not available to recompute its digest", sid); goto out; } sha256_hash(bb, bl, dg); - free(bb); if (memcmp(dg, vc->source_or_ir_digest, 32) != 0) { /* VC1R:blob-digest */ + free(bb); rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "the stored source or IR of %s does not hash to its source_or_ir_digest", sid); goto out; } + if (vc->digest_kind == OMEGA_VC_DIGEST_IR) { + uint8_t pid[32]; + if (omega_program_ir_recompute_id(bb, bl, pid) != 0 || memcmp(pid, id, 32) != 0) { /* VC1R:program-id */ + free(bb); + rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "the stored IR of %s does not recompute to its semantic id", sid); + goto out; + } + } else if (r->domain == OMEGA_DOMAIN_BUILD) { /* VC1R:ir-kind */ + free(bb); + rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "%s was recorded from OSC source, and only an IR digest can be recomputed to its program id in the build domain", sid); + goto out; + } + free(bb); } if (lock_receipt && memcmp(lock_receipt, vc->receipt_id, 32) != 0) { /* VC1R:lock-receipt */ rc = fail(w->err, OMEGA_RES_RECEIPT_HASH_MISMATCH, 0, subj, "the lock pins another receipt than the record carries for %s", sid); @@ -474,16 +502,13 @@ static int resolve_node(Walk *w, const uint8_t id[32], const uint8_t *lock_recei rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "%s has an unknown admission kind", sid); goto out; } - if (rec->admission_kind == OMEGA_VCS_ADMISSION_BOOTSTRAP && !r->allow_genesis) { /* VC1R:genesis-gate */ - rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "%s is a genesis (bootstrap) record and genesis records are not permitted in this resolution", sid); - goto out; - } for (uint32_t i = 0; i < vc->n_capabilities; i++) if (vc->capabilities[i].len == sizeof OMEGA_TAINT_CAPABILITY - 1 && memcmp(vc->capabilities[i].p, OMEGA_TAINT_CAPABILITY, sizeof OMEGA_TAINT_CAPABILITY - 1) == 0) { /* VC1R:taint-cap */ rc = fail(w->err, OMEGA_RES_TAINTED_ARTIFACT, 0, subj, "%s lists the capability %s: an omega-dev output can never satisfy an import", sid, OMEGA_TAINT_CAPABILITY); goto out; } + if (r->domain == OMEGA_DOMAIN_BUILD && lists_selfminted_cap(vc)) { rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "%s was minted by the in-process bridge (capability %s) with no independent qualification run, so it cannot satisfy a build import", sid, OMEGA_BRIDGE_SELFMINTED_CAPABILITY); goto out; } /* VC1B:build-refuses-selfminted */ if (rec->admission_kind == OMEGA_VCS_ADMISSION_VERIFIED) { /* VC1R:verified-needs-receipt */ rc = omega_resolve_check_record_receipt(r, vc, w->err); if (rc) { if (name && w->err) snprintf(w->err->subject, sizeof w->err->subject, "%s", name); goto out; } @@ -518,6 +543,12 @@ static int resolve_node(Walk *w, const uint8_t id[32], const uint8_t *lock_recei goto out; } } + /* THE genesis gate: reached only after the whole subtree resolved, and refusing any BOOTSTRAP record whose id the pinned + * VC-GENESIS-1 does not list. Nothing but list membership can satisfy it (no flag, no variable, nothing from the store). */ + if (rec->admission_kind == OMEGA_VCS_ADMISSION_BOOTSTRAP && !omega_genesis_contains(vc->semantic_id)) { /* VC1R:genesis-gate */ + rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, OMEGA_VCS_GENESIS_NOT_LISTED, subj, "%s is a genesis (bootstrap) record whose id is not in the pinned set %s", sid, OMEGA_GENESIS_SET_NAME); + goto out; + } w->nodes[me].state = 2; out: free(rec); @@ -709,6 +740,10 @@ int omega_resolve_admit_genesis(OmegaVcStore *s, const uint8_t *canonical, size_ return fail(err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, sid, "a genesis record may depend only on genesis records already in the store"); } } + if (!omega_genesis_contains(view->semantic_id)) { /* VC1R:admit-genesis-list */ + free(view); + return fail(err, OMEGA_RES_UNVERIFIED_DEPENDENCY, OMEGA_VCS_GENESIS_NOT_LISTED, sid, "%s is not a member of the pinned genesis set %s", sid, OMEGA_GENESIS_SET_NAME); + } free(view); rc = omega_vcstore_insert_bootstrap(s, canonical, len, claimed_vc_id); if (rc) return map_store(err, rc, sid); diff --git a/src/omega_resolve.h b/src/omega_resolve.h index 8c6b5e325..f6b3ae32b 100644 --- a/src/omega_resolve.h +++ b/src/omega_resolve.h @@ -12,10 +12,12 @@ * Per node, in this order (SPEC 6; the first failure wins): * 1 lock name has a lock line, else DEPENDENCY_NOT_PINNED * 2 store omega_vcstore_get(semantic_id); absent or integrity failure: UNVERIFIED_DEPENDENCY - * 3 identity key == record id; optional blob digest (below); lock receipt == record + * 3 identity key == record id; source or IR recheck (below); lock receipt == record * receipt_id, else RECEIPT_HASH_MISMATCH - * - kind BOOTSTRAP records satisfy an import only when allow_genesis is set, else - * UNVERIFIED_DEPENDENCY + * - kind a BOOTSTRAP record satisfies an import only when its id is a member of the + * pinned genesis set VC-GENESIS-1 (omega_genesis.h), else UNVERIFIED_DEPENDENCY + * (store code GENESIS_NOT_LISTED). Membership is the ONLY input: no flag, no + * environment variable, nothing read from the store. * 4 taint capability "omega-dev.taint": TAINTED_ARTIFACT (both domains) * 5 profile not in the table: UNKNOWN_VERIFIER_PROFILE; version below the minimum (or not a * dotted number): VERIFIER_TOO_OLD @@ -31,11 +33,19 @@ * * DECISIONS (also in the PR body): * - BOOTSTRAP records (the audited genesis set) carry an audit hash in receipt_id, not a - * receipt. When allow_genesis is set they skip steps 5 and 6 (the lock must still pin the - * audit hash). A BOOTSTRAP record may NOT depend on a VERIFIED record: the audited base must - * be closed over itself, so the resolver refuses it (UNVERIFIED_DEPENDENCY) and - * omega_resolve_admit_genesis refuses to insert it. A VERIFIED record may depend on BOOTSTRAP - * (the genesis set is where trust starts), and only resolves with allow_genesis. + * receipt. A listed one skips steps 5 and 6 (the lock must still pin the audit hash). A + * BOOTSTRAP record may NOT depend on a VERIFIED record: the audited base must be closed over + * itself, so the resolver refuses it (UNVERIFIED_DEPENDENCY) and omega_resolve_admit_genesis + * refuses to insert it. A VERIFIED record may depend on BOOTSTRAP (the genesis set is where + * trust starts). The set is pinned in the source (omega_genesis.h, docs/osc/VC-GENESIS-1.md); + * changing it is a reviewed source change, never a runtime switch. + * - SOURCE/IR RECHECK (SPEC 6 step 3). In the build domain the source or IR store is + * MANDATORY: with no fetch_blob every node is refused (UNVERIFIED_DEPENDENCY). The blob must + * hash to source_or_ir_digest, and when the digest kind is IR the program id is recomputed + * with omega_program_compute_id (omega_program_ir.h) and must equal the record's semantic id. + * A record whose digest kind is OSC source cannot be recomputed to a program id here, so the + * build domain refuses it. The dev domain may omit the blob store (its output is tainted); + * when it supplies one the same checks run. * - omega-dev and omega-build run the SAME checks. The domain never loosens a check (ADR 0029 * Decision 4: flags cannot change what is legal, only the closure can). It labels the output: * an omega-dev artifact is TAINTED, its identity (build id) commits to that, and the resolver @@ -44,9 +54,8 @@ * compiler. A test greps for them. * * LIMITS (stated, not hidden): receipts are hashes, not signatures (see omega_receipt.h); the - * blob check below is optional until stage 6 supplies the source/IR store; the receipt binding - * (rule 4) ties semantic_id and source digest, but the program id is not recomputed from source - * here (SPEC 6 step 3 first half is done only for the digest). */ + * receipt binding (rule 4) ties semantic_id and source digest; contract_id is not recomputed + * from the IR (the edge check compares the stored contract ids only). */ #include #include @@ -117,10 +126,11 @@ typedef struct { size_t n_profiles; OmegaReceiptFetch fetch_receipt; /* required for any VERIFIED record */ void *fetch_ctx; - OmegaBlobFetch fetch_blob; /* optional: when set, SHA-256 of the blob named by - source_or_ir_digest must equal that digest */ + OmegaBlobFetch fetch_blob; /* REQUIRED in the build domain (without it every node + is refused); optional in dev. SHA-256 of the blob named + by source_or_ir_digest must equal that digest, and an + IR blob must recompute to the semantic id */ void *blob_ctx; - int allow_genesis; /* BOOTSTRAP records may satisfy imports (stage 6) */ } OmegaResolver; typedef struct { uint8_t semantic_id[32], receipt_id[32], admission_kind; } OmegaClosureEntry; @@ -167,17 +177,25 @@ int omega_artifact_meta_text(const OmegaArtifactMeta *m, char *buf, size_t cap); * BAD_ARGUMENT for malformed text). */ int omega_artifact_meta_parse(const char *text, size_t len, OmegaArtifactMeta *m, OmegaResolveError *err); -/* Insert a Verified Crumb into the store, only together with the receipt that qualifies it: +/* Insert a Verified Crumb into the store, only together with the receipt that qualifies it + * (THE only way a VERIFIED record enters a store; the raw insert is private to this file, see + * omega_vcstore_priv.h): * origin (may be NULL when the record did not come from a compile) tainted: TAINTED_ARTIFACT; * the record lists omega-dev.taint: TAINTED_ARTIFACT; then steps 5 and 6 above; then - * omega_vcstore_insert (VERIFIED). The store is unchanged on every refusal. */ + * the private raw insert (VERIFIED). The store is unchanged on every refusal. */ int omega_resolve_admit(const OmegaResolver *r, OmegaVcStore *s, const uint8_t *canonical, size_t len, const uint8_t claimed_vc_id[32], const OmegaArtifactMeta *origin, OmegaResolveError *err); -/* Genesis loading (stage 6): same taint refusals, no receipt (audit hash), refuses any VERIFIED - * dependency, inserts with omega_vcstore_insert_bootstrap. */ +/* Genesis loading: same taint refusals, no receipt (audit hash), refuses any VERIFIED + * dependency, and refuses any record whose semantic id is not a member of VC-GENESIS-1 + * (UNVERIFIED_DEPENDENCY with store code GENESIS_NOT_LISTED). THE only way a BOOTSTRAP record + * enters a store. */ int omega_resolve_admit_genesis(OmegaVcStore *s, const uint8_t *canonical, size_t len, const uint8_t claimed_vc_id[32], const OmegaArtifactMeta *origin, OmegaResolveError *err); #define OMEGA_TAINT_CAPABILITY "omega-dev.taint" +/* A record minted by omega_vc_bridge (no independent qualification run) lists this capability. The + * build-domain resolver refuses it, so a bridge record can sit in a store but can never satisfy a + * build import (VC1 stage 6 fix). The dev domain accepts it (the artifact is tainted anyway). */ +#define OMEGA_BRIDGE_SELFMINTED_CAPABILITY "omega-bridge.selfminted" #endif /* OMEGA_RESOLVE_H */ diff --git a/src/omega_vc_bridge.c b/src/omega_vc_bridge.c new file mode 100644 index 000000000..54b2bf4b6 --- /dev/null +++ b/src/omega_vc_bridge.c @@ -0,0 +1,275 @@ +/* omega_vc_bridge.c -- see omega_vc_bridge.h. */ +#include "omega_vc_bridge.h" + +#include +#include +#include + +#include "omega_program_ir.h" +#include "omega_receipt.h" +#include "sha256.h" + +#define PROFILE "host-v1" +#define PROFILE_VERSION "1.0.0" + +int omega_vc_bridge_init(OmegaVcBridge *b) +{ + if (!b) return -1; + memset(b, 0, sizeof *b); + return omega_vcstore_init(&b->store) == 0 ? 0 : -1; +} + +void omega_vc_bridge_destroy(OmegaVcBridge *b) +{ + if (!b) return; + for (size_t i = 0; i < b->n_receipts; i++) free(b->receipts[i].json); + for (size_t i = 0; i < b->n_blobs; i++) free(b->blobs[i].bytes); + free(b->receipts); + free(b->blobs); + omega_vcstore_destroy(&b->store); + memset(b, 0, sizeof *b); +} + +static int fetch_copy(const uint8_t *p, size_t n, uint8_t **bytes, size_t *len) +{ + *bytes = malloc(n + 1); + if (!*bytes) return 2; + memcpy(*bytes, p, n); + *len = n; + return 0; +} +int omega_vc_bridge_fetch_receipt(void *ctx, const uint8_t id[32], uint8_t **bytes, size_t *len) +{ + const OmegaVcBridge *b = ctx; + for (size_t i = 0; b && i < b->n_receipts; i++) + if (memcmp(b->receipts[i].id, id, 32) == 0) return fetch_copy((const uint8_t *)b->receipts[i].json, b->receipts[i].len, bytes, len); + return 1; +} +int omega_vc_bridge_fetch_blob(void *ctx, const uint8_t digest[32], uint8_t **bytes, size_t *len) +{ + const OmegaVcBridge *b = ctx; + for (size_t i = 0; b && i < b->n_blobs; i++) + if (memcmp(b->blobs[i].digest, digest, 32) == 0) return fetch_copy(b->blobs[i].bytes, b->blobs[i].len, bytes, len); + return 1; +} + +static void hex32(const uint8_t *in, char out[65]) +{ + static const char h[] = "0123456789abcdef"; + for (int i = 0; i < 32; i++) { out[2 * i] = h[in[i] >> 4]; out[2 * i + 1] = h[in[i] & 15]; } + out[64] = 0; +} + +/* the receipt JSON, with idhex in the "id" field. dep_hex is n x 65 chars. Returns bytes written or -1. */ +static int emit_receipt(char *o, size_t cap, const char *idhex, const char *sid, const char *dig, const char *root, + const char (*dep_hex)[65], size_t nd) +{ + size_t n = 0; +#define PF(...) do { int w_ = snprintf(o + n, cap - n, __VA_ARGS__); if (w_ < 0 || (size_t)w_ >= cap - n) return -1; n += (size_t)w_; } while (0) + PF("{\n\"schema\": \"EvidenceReceiptV1\",\n\"version\": 1,\n\"id\": \"%s\",\n\"kind\": \"" PROFILE "\",\n", idhex); + PF("\"tier\": \"HOST_TEST\",\n\"result\": \"PASS\",\n\"timestamp\": 1,\n"); + PF("\"repo\": \"omega-vc-bridge\",\n\"commit\": \"0000000000000000000000000000000000000000\",\n\"dirty\": false,\n"); + PF("\"toolchain\": \"omega-vc-bridge\",\n\"procedure\": \"in-process omega_program_verify and program id recompute (self-minted HOST_TEST receipt, not an independent run)\",\n"); + PF("\"machine\": \"host\",\n\"env_class\": \"HOST_TEST\",\n"); + PF("\"input_artifacts\": [\"sha256:%s\", \"sha256:%s\"],\n\"output_artifacts\": [],\n", sid, dig); + PF("\"assertions\": [{\"id\": \"omega_program_verify\", \"expected\": \"pass\", \"observed\": \"pass\", \"pass\": true, \"source\": \"omega_vc_bridge\", \"note\": \"\"}],\n"); + PF("\"dependencies\": ["); + for (size_t i = 0; i < nd; i++) PF("%s\"%s\"", i ? ", " : "", dep_hex[i]); + PF("],\n\"declared_mutation\": \"NONE\",\n\"observed_mutation\": \"NONE\",\n\"authority\": \"\",\n\"output_digest\": \"%s\",\n\"external_refs\": [],\n\"ledger\": null,\n\"lease\": null,\n\"required_features\": [],\n\"reserved\": \"\"\n}\n", root); +#undef PF + return (int)n; +} + +typedef struct { uint8_t *p; size_t n, cap; int bad; } Out; +static void oput(Out *o, const void *d, size_t k) +{ + if (o->bad) return; + if (o->n + k > o->cap) { + size_t nc = (o->n + k) * 2 + 64; + uint8_t *t = realloc(o->p, nc); + if (!t) { o->bad = 1; return; } + o->p = t; o->cap = nc; + } + memcpy(o->p + o->n, d, k); + o->n += k; +} +static void ou32(Out *o, uint32_t v) { uint8_t t[4] = { (uint8_t)(v >> 24), (uint8_t)(v >> 16), (uint8_t)(v >> 8), (uint8_t)v }; oput(o, t, 4); } +static void ostr(Out *o, const char *s) +{ + size_t n = strlen(s); + uint8_t l[8]; + for (int i = 0; i < 8; i++) l[i] = (uint8_t)((uint64_t)n >> (56 - 8 * i)); + oput(o, l, 8); + oput(o, s, n); +} + +static int put_receipt(OmegaVcBridge *b, const uint8_t id[32], const char *json, size_t len) +{ + for (size_t i = 0; i < b->n_receipts; i++) if (memcmp(b->receipts[i].id, id, 32) == 0) return 0; + if (b->n_receipts == b->cap_receipts) { + size_t nc = b->cap_receipts ? b->cap_receipts * 2 : 16; + OmegaVcBridgeReceipt *t = realloc(b->receipts, nc * sizeof *t); + if (!t) return -1; + b->receipts = t; b->cap_receipts = nc; + } + char *c = malloc(len + 1); + if (!c) return -1; + memcpy(c, json, len); c[len] = 0; + memcpy(b->receipts[b->n_receipts].id, id, 32); + b->receipts[b->n_receipts].json = c; + b->receipts[b->n_receipts].len = len; + b->n_receipts++; + return 0; +} +static int put_blob(OmegaVcBridge *b, const uint8_t digest[32], const uint8_t *bytes, size_t len) +{ + for (size_t i = 0; i < b->n_blobs; i++) if (memcmp(b->blobs[i].digest, digest, 32) == 0) return 0; + if (b->n_blobs == b->cap_blobs) { + size_t nc = b->cap_blobs ? b->cap_blobs * 2 : 16; + OmegaVcBridgeBlob *t = realloc(b->blobs, nc * sizeof *t); + if (!t) return -1; + b->blobs = t; b->cap_blobs = nc; + } + uint8_t *c = malloc(len ? len : 1); + if (!c) return -1; + memcpy(c, bytes, len); + memcpy(b->blobs[b->n_blobs].digest, digest, 32); + b->blobs[b->n_blobs].bytes = c; + b->blobs[b->n_blobs].len = len; + b->n_blobs++; + return 0; +} + +static int cmp_dep(const void *a, const void *b) { return memcmp(a, b, 32); } + +int omega_vc_bridge_admit(OmegaVcBridge *b, OmegaLibrary *lib, const OmegaProgram *prog, + const SemanticId *deps, size_t dep_count, const uint8_t evidence[32]) +{ + static const uint8_t zero[32] = { 0 }; + if (!b || !lib || !prog || !evidence || memcmp(evidence, zero, 32) == 0 || dep_count > OMEGA_LIB_MAX_DEPS || (dep_count && !deps)) return -1; + if (!prog->is_realized || !prog->body.has_body || memcmp(prog->program_id.bytes, zero, 32) == 0) return -1; + int rc = -1; + uint8_t *ir = NULL, *vc = NULL; + size_t ir_len = 0; + uint8_t digest[32], contract[32], root[32], evid[32]; + uint8_t (*dep_sorted)[32] = NULL, (*dep_con)[32] = NULL, (*dep_rcpt)[32] = NULL; + char (*dep_hex)[65] = NULL; + char *json = NULL; + Out o = { NULL, 0, 0, 0 }; + OmegaVcRecord *rec = NULL; + OmegaProgram *cp = NULL; + + /* The bridge checks for itself; it never trusts the caller's is_verified flag or program_id. It + * verifies a private copy (omega_program_verify writes is_verified) and recomputes the id from the + * IR it is about to store. */ + cp = malloc(sizeof *cp); + if (!cp) goto done; + *cp = *prog; + { VerifyReport rep; + if (omega_program_verify(cp, &rep) != 0) goto done; /* VC1B:verify */ + } + if (omega_program_ir_encode(prog, &ir, &ir_len) != 0) goto done; + { uint8_t rid_[32]; + if (omega_program_ir_recompute_id(ir, ir_len, rid_) != 0 || memcmp(rid_, prog->program_id.bytes, 32) != 0) goto done; /* VC1B:id */ + } + sha256_hash(ir, ir_len, digest); + if (omega_program_contract_id(prog, contract) != 0) goto done; + { /* evidence root binds the caller's evidence digest to this program */ + static const uint8_t dom[] = "aien.vc1.bridge.evidence.v1"; + uint8_t buf[sizeof dom + 64]; + memcpy(buf, dom, sizeof dom); + memcpy(buf + sizeof dom, evidence, 32); + memcpy(buf + sizeof dom + 32, prog->program_id.bytes, 32); + sha256_hash(buf, sizeof dom + 64, root); + memcpy(evid, root, 32); + } + size_t nd = dep_count; + dep_sorted = malloc((nd ? nd : 1) * 32); + dep_con = malloc((nd ? nd : 1) * 32); + dep_rcpt = malloc((nd ? nd : 1) * 32); + dep_hex = malloc((nd ? nd : 1) * 65); + rec = malloc(sizeof *rec); + if (!dep_sorted || !dep_con || !dep_rcpt || !dep_hex || !rec) goto done; + for (size_t i = 0; i < nd; i++) memcpy(dep_sorted[i], deps[i].bytes, 32); + qsort(dep_sorted, nd, 32, cmp_dep); + for (size_t i = 0; i < nd; i++) { + if (i && memcmp(dep_sorted[i - 1], dep_sorted[i], 32) == 0) goto done; /* duplicate dependency */ + if (omega_vcstore_get(&b->store, dep_sorted[i], rec) != 0) goto done; /* must be admitted already */ + memcpy(dep_con[i], rec->vc.contract_id, 32); + memcpy(dep_rcpt[i], rec->vc.receipt_id, 32); + hex32(dep_rcpt[i], dep_hex[i]); + } + char sid_h[65], dig_h[65], root_h[65], zero_h[65]; + hex32(prog->program_id.bytes, sid_h); + hex32(digest, dig_h); + hex32(root, root_h); + memset(zero_h, '0', 64); zero_h[64] = 0; + json = malloc(8192); + if (!json) goto done; + int n0 = emit_receipt(json, 8192, zero_h, sid_h, dig_h, root_h, (const char (*)[65])dep_hex, nd); + if (n0 < 0) goto done; + uint8_t rid[32]; + { + OmegaReceipt r; + char err[200]; + if (omega_receipt_parse((const uint8_t *)json, (size_t)n0, &r, err, sizeof err) != 0) goto done; + int cr = omega_receipt_compute_id(&r, rid); + omega_receipt_free(&r); + if (cr != 0) goto done; + } + char rid_h[65]; + hex32(rid, rid_h); + int n1 = emit_receipt(json, 8192, rid_h, sid_h, dig_h, root_h, (const char (*)[65])dep_hex, nd); + if (n1 < 0) goto done; + /* the canonical Verified Crumb */ + oput(&o, "AIEN_VERIFIED_CRUMB_V1", 22); + ou32(&o, OMEGA_VC_FORMAT_VERSION); + oput(&o, prog->program_id.bytes, 32); + oput(&o, contract, 32); + { uint8_t k = OMEGA_VC_DIGEST_IR; oput(&o, &k, 1); } + oput(&o, digest, 32); + ou32(&o, 0); /* realizations */ + ou32(&o, (uint32_t)nd); + for (size_t i = 0; i < nd; i++) { oput(&o, dep_sorted[i], 32); oput(&o, dep_con[i], 32); } + oput(&o, rid, 32); + ostr(&o, PROFILE); + ostr(&o, PROFILE_VERSION); + oput(&o, evid, 32); + ou32(&o, 0); /* exports */ + ou32(&o, 1); ostr(&o, OMEGA_BRIDGE_SELFMINTED_CAPABILITY); /* capabilities: one, so the build domain refuses this record */ /* VC1B:cap */ + if (o.bad) goto done; + vc = o.p; + uint8_t vcid[32]; + omega_vc_compute_id(vc, o.n, vcid); + if (put_receipt(b, rid, json, (size_t)n1) != 0 || put_blob(b, digest, ir, ir_len) != 0) goto done; + OmegaResolver r; + memset(&r, 0, sizeof r); + r.store = &b->store; + r.domain = OMEGA_DOMAIN_BUILD; + r.fetch_receipt = omega_vc_bridge_fetch_receipt; + r.fetch_ctx = b; + r.fetch_blob = omega_vc_bridge_fetch_blob; + r.blob_ctx = b; + OmegaResolveError err; + if (omega_resolve_admit(&r, &b->store, vc, o.n, vcid, NULL, &err) != 0) goto done; + /* store first, library second: the library entry carries the receipt id as its evidence hash. + * Insert the copy the bridge verified itself (the library copies it by value); the caller's + * is_verified flag is never consulted. */ + rc = omega_library_insert(lib, cp, deps, dep_count, rid); +done: + free(ir); + free(vc); + free(dep_sorted); free(dep_con); free(dep_rcpt); free(dep_hex); + free(json); + free(rec); + free(cp); + return rc; +} + +int omega_vc_bridge_admit_abstraction(OmegaVcBridge *b, OmegaLibrary *lib, const OmegaAbstractionCandidate *cand, + const uint8_t evidence[32]) +{ + if (!b || !lib || !cand || !evidence) return -1; + if (!cand->is_verified || !cand->is_nontrivial || cand->compression_score <= 0) return -1; + return omega_vc_bridge_admit(b, lib, &cand->abstraction, NULL, 0, evidence); +} diff --git a/src/omega_vc_bridge.h b/src/omega_vc_bridge.h new file mode 100644 index 000000000..f6ecf72c3 --- /dev/null +++ b/src/omega_vc_bridge.h @@ -0,0 +1,68 @@ +#ifndef OMEGA_VC_BRIDGE_H +#define OMEGA_VC_BRIDGE_H + +/* omega_vc_bridge: the one in-process way a program enters an OmegaLibrary together with the + * Verified Crumb Store (VC1 stage 6, ADR 0029 Decisions 8 and 11). + * + * Before this existed Crumbline and discovery called omega_library_insert directly with a bare + * 32-byte hash. Now every program goes through omega_resolve_admit (the only door into the + * store): the bridge builds the canonical Verified Crumb of the program (semantic id = + * omega_program_compute_id, contract id = omega_program_contract_id, digest kind IR, the IR of + * omega_program_ir.h), writes an evidence receipt for it, and lets the resolver run SPEC 5.1 on + * that receipt. Only then is the library entry inserted, with the RECEIPT ID as its evidence hash. + * + * WHAT THE RECEIPT IS (stated, not hidden): the bridge itself runs omega_program_verify on a private + * copy of the program and recomputes the program id from the IR it stores, and refuses (-1) when + * either fails. It does NOT trust the caller's is_verified flag or program_id. It then records that + * as an EvidenceReceiptV1 of kind host-v1, tier HOST_TEST, with the evidence digest the caller + * supplies as its output_digest (the evidence_root). It is still a SELF-MINTED HOST_TEST receipt: + * the same code that asks for admission writes it, and no independent verifier re-ran anything. + * So the record it makes lists the capability OMEGA_BRIDGE_SELFMINTED_CAPABILITY, which the + * build-domain resolver refuses: a bridge record can sit in a store, but it can never satisfy a + * build import (oscv --domain build). The dev domain accepts it. Real aien-test receipts for + * Crumbline and discovery programs are owed (ADR 0029 Decision 5 and 11) and are not here. + * + * NOT A GENESIS PATH: the bridge never calls omega_resolve_admit_genesis. Nothing here can create + * a BOOTSTRAP record. */ + +#include +#include + +#include "omega_discovery.h" +#include "omega_library.h" +#include "omega_program.h" +#include "omega_resolve.h" +#include "omega_vcstore.h" + +typedef struct { uint8_t id[32]; char *json; size_t len; } OmegaVcBridgeReceipt; +typedef struct { uint8_t digest[32]; uint8_t *bytes; size_t len; } OmegaVcBridgeBlob; + +typedef struct { + OmegaVcStore store; + OmegaVcBridgeReceipt *receipts; size_t n_receipts, cap_receipts; + OmegaVcBridgeBlob *blobs; size_t n_blobs, cap_blobs; +} OmegaVcBridge; + +int omega_vc_bridge_init(OmegaVcBridge *b); +void omega_vc_bridge_destroy(OmegaVcBridge *b); + +/* Receipt and IR sources over the bridge's own tables, in the resolver's fetch signatures + * (ctx = the OmegaVcBridge *), so a resolver can be pointed at the same store. */ +int omega_vc_bridge_fetch_receipt(void *ctx, const uint8_t id[32], uint8_t **bytes, size_t *len); +int omega_vc_bridge_fetch_blob(void *ctx, const uint8_t digest[32], uint8_t **bytes, size_t *len); + +/* Admit prog (realized by the caller; verified and id-recomputed here) into the store through omega_resolve_admit, + * then into lib with the receipt id as its evidence hash. deps/dep_count are library + * dependencies; each must already be admitted through this bridge. evidence is the caller's + * non-zero evidence digest (it becomes the record's evidence_root). 0 on success; -1 and nothing + * inserted into lib on any refusal (a record that reached the store stays there: records are + * immutable and identical bytes are a no-op). */ +int omega_vc_bridge_admit(OmegaVcBridge *b, OmegaLibrary *lib, const OmegaProgram *prog, + const SemanticId *deps, size_t dep_count, const uint8_t evidence[32]); + +/* Discovery: the abstraction of cand, with the same guards omega_discovery_admit_to_library had + * (verified, non-trivial, positive compression). */ +int omega_vc_bridge_admit_abstraction(OmegaVcBridge *b, OmegaLibrary *lib, const OmegaAbstractionCandidate *cand, + const uint8_t evidence[32]); + +#endif /* OMEGA_VC_BRIDGE_H */ diff --git a/src/omega_vcstore.c b/src/omega_vcstore.c index 2d9538dd0..a54be91ac 100644 --- a/src/omega_vcstore.c +++ b/src/omega_vcstore.c @@ -1,4 +1,6 @@ #include "omega_vcstore.h" +#include "omega_vcstore_priv.h" +#include "omega_genesis.h" #include "sha256.h" #include #include @@ -49,6 +51,7 @@ const char *omega_vcstore_code_name(int code) { case OMEGA_VCS_VCSTORE_CAPACITY: return "VCSTORE_CAPACITY"; case OMEGA_VCS_VCSTORE_NOMEM: return "VCSTORE_NOMEM"; case OMEGA_VCS_VCSTORE_IO: return "VCSTORE_IO"; + case OMEGA_VCS_GENESIS_NOT_LISTED: return "GENESIS_NOT_LISTED"; } return "UNKNOWN_CODE"; } @@ -232,6 +235,7 @@ static int vcs_insert(OmegaVcStore *s, const uint8_t *canon, size_t len, const u uint8_t id[32]; omega_vc_compute_id(canon, len, id); if (memcmp(id, claimed, 32) != 0) { free(rec); return OMEGA_VCS_VCSTORE_ID_MISMATCH; } /* VC1S:insert-idcheck */ + if (kind == OMEGA_VCS_ADMISSION_BOOTSTRAP && !omega_genesis_contains(rec->vc.semantic_id)) { free(rec); return OMEGA_VCS_GENESIS_NOT_LISTED; } /* VC1S:insert-genesis */ size_t pos; if (vcs_find(s, rec->vc.semantic_id, &pos)) { const OmegaVcObject *o = &s->objs[pos]; @@ -628,6 +632,7 @@ static int parse_store(OmegaVcStore *t, const uint8_t *b, size_t n) { const uint8_t *ob = r_take(&r, (size_t)len); rc = omega_vc_decode(ob, (size_t)len, &rec->vc); if (rc) break; + if (kind == OMEGA_VCS_ADMISSION_BOOTSTRAP && !omega_genesis_contains(rec->vc.semantic_id)) { rc = OMEGA_VCS_GENESIS_NOT_LISTED; break; } /* VC1S:load-genesis */ if (t->count > 0 && memcmp(t->objs[t->count - 1].semantic_id, rec->vc.semantic_id, 32) >= 0) { rc = OMEGA_VCS_VCSTORE_MALFORMED; break; } /* VC1S:load-order */ rec->canonical = ob; rec->canonical_len = (size_t)len; diff --git a/src/omega_vcstore.h b/src/omega_vcstore.h index 25a4ed33c..239aafb0a 100644 --- a/src/omega_vcstore.h +++ b/src/omega_vcstore.h @@ -26,12 +26,14 @@ * reserved for that verifier and the resolver. * - It does not read lockfiles and does not decide trust from a name. * - * ADMISSION KIND: two insert functions, as in omega_library (#221). VERIFIED is the normal path. - * BOOTSTRAP is for the manually audited GENESIS set only (VC1 stage 6). There is no flag, - * environment variable or build option that unlocks it or skips any refusal; reviewers must - * reject any caller of omega_vcstore_insert_bootstrap that is not the genesis loader. For a - * bootstrap record the receipt_id field carries the hash of the audit record. The kind is part - * of the state digest. + * ADMISSION KIND: VERIFIED is the normal path. BOOTSTRAP is for the manually audited Genesis Set + * VC-GENESIS-1 only (src/omega_genesis.h, docs/osc/VC-GENESIS-1.md). The two raw insert functions + * are private (omega_vcstore_priv.h) and reachable only through omega_resolve_admit and + * omega_resolve_admit_genesis. There is no flag, environment variable or build option that unlocks + * BOOTSTRAP or skips any refusal: a BOOTSTRAP record whose semantic id is not on the list is + * refused when it is inserted and when a store file holding it is loaded (GENESIS_NOT_LISTED). + * For a bootstrap record the receipt_id field carries the hash of the audit record. The kind is + * part of the state digest. * * NAME INDEX: a separate map name -> semantic_id. Many names may point at one id. A bound name * changes only through the explicit omega_vcstore_name_rebind(). Nothing in get, closure, @@ -86,7 +88,8 @@ enum { OMEGA_VCS_VCSTORE_NAME_EXISTS, /* name already bound to another id; use name_rebind */ OMEGA_VCS_VCSTORE_CAPACITY, /* closure output array too small (needed count returned) */ OMEGA_VCS_VCSTORE_NOMEM, - OMEGA_VCS_VCSTORE_IO + OMEGA_VCS_VCSTORE_IO, + OMEGA_VCS_GENESIS_NOT_LISTED /* BOOTSTRAP record whose semantic id is not in VC-GENESIS-1 */ }; const char *omega_vcstore_code_name(int code); @@ -155,24 +158,9 @@ int omega_vcstore_init(OmegaVcStore *s); void omega_vcstore_destroy(OmegaVcStore *s); size_t omega_vcstore_count(const OmegaVcStore *s); -/* Insert a verified Verified Crumb (admission kind VERIFIED). claimed_vc_id is required (NULL is - * MALFORMED, never "skip the check"). The store is unchanged on every refusal. Refusals: - * - NULL argument, zero length: VCSTORE_MALFORMED - * - any SPEC 3.6 format refusal (includes MISSING_RECEIPT for a zero receipt_id and - * DEPENDENCY_CYCLE for a self dependency) - * - SHA-256(bytes) != claimed_vc_id: VCSTORE_ID_MISMATCH - * - semantic_id already present with different bytes or another admission kind: - * VCSTORE_IMMUTABLE_CONFLICT (identical bytes and kind: success, no change) - * - a dependency semantic_id not in the store, or whose contract_id differs from the entry's - * required_contract: UNVERIFIED_DEPENDENCY - * - out of memory: VCSTORE_NOMEM - * There is no capacity limit: the store grows. */ -int omega_vcstore_insert(OmegaVcStore *s, const uint8_t *canonical, size_t len, - const uint8_t claimed_vc_id[32]); - -/* Genesis loading only. Same refusals; admission kind BOOTSTRAP. See the header comment. */ -int omega_vcstore_insert_bootstrap(OmegaVcStore *s, const uint8_t *canonical, size_t len, - const uint8_t claimed_vc_id[32]); +/* There is no public insert. Records enter only through omega_resolve_admit and + * omega_resolve_admit_genesis (src/omega_resolve.h); the raw functions are declared in the private + * header omega_vcstore_priv.h (VC1 stage 6). */ /* Fetch by semantic_id. Recomputes the VC id of the stored bytes and re-decodes them; any * mismatch is VCSTORE_ID_MISMATCH. Absent: UNVERIFIED_DEPENDENCY. */ diff --git a/src/omega_vcstore_priv.h b/src/omega_vcstore_priv.h new file mode 100644 index 000000000..d5106a3b3 --- /dev/null +++ b/src/omega_vcstore_priv.h @@ -0,0 +1,42 @@ +#ifndef OMEGA_VCSTORE_PRIV_H +#define OMEGA_VCSTORE_PRIV_H + +/* PRIVATE to the Verified Crumb Store and its one admission door (VC1 stage 6, ADR 0029 + * Decisions 5, 7 and 8). + * + * The two raw insert functions are not in omega_vcstore.h on purpose. A record may enter a store + * only through omega_resolve_admit (receipt checked) or omega_resolve_admit_genesis (member of + * VC-GENESIS-1 only), both in omega_resolve.c. The only files that may include this header are + * src/omega_vcstore.c (defines them) + * src/omega_resolve.c (the admission door) + * and test code under tests/. tests/test_omega_resolve.c scans src/ and tools/ and fails if any + * other file includes this header or calls either function. + * + * The raw functions themselves enforce what the store can see: format, immutability, + * dependencies present, and (BOOTSTRAP only) membership of the semantic id in VC-GENESIS-1. They + * do NOT check receipts: that is the admission door's job. */ + +#include "omega_vcstore.h" + +/* Insert a Verified Crumb (admission kind VERIFIED). claimed_vc_id is required (NULL is + * MALFORMED, never "skip the check"). The store is unchanged on every refusal. Refusals: + * - NULL argument, zero length: VCSTORE_MALFORMED + * - any SPEC 3.6 format refusal (includes MISSING_RECEIPT for a zero receipt_id and + * DEPENDENCY_CYCLE for a self dependency) + * - SHA-256(bytes) != claimed_vc_id: VCSTORE_ID_MISMATCH + * - semantic_id already present with different bytes or another admission kind: + * VCSTORE_IMMUTABLE_CONFLICT (identical bytes and kind: success, no change) + * - a dependency semantic_id not in the store, or whose contract_id differs from the entry's + * required_contract: UNVERIFIED_DEPENDENCY + * - out of memory: VCSTORE_NOMEM + * There is no capacity limit: the store grows. */ +int omega_vcstore_insert(OmegaVcStore *s, const uint8_t *canonical, size_t len, + const uint8_t claimed_vc_id[32]); + +/* Genesis loading only. Same refusals, plus GENESIS_NOT_LISTED when the record's semantic id is + * not a member of VC-GENESIS-1 (src/omega_genesis.h). Admission kind BOOTSTRAP. For a bootstrap + * record the receipt_id field carries the hash of the audit record. */ +int omega_vcstore_insert_bootstrap(OmegaVcStore *s, const uint8_t *canonical, size_t len, + const uint8_t claimed_vc_id[32]); + +#endif /* OMEGA_VCSTORE_PRIV_H */ diff --git a/src/oscv_main.c b/src/oscv_main.c index 8a4674ef1..2f76abc53 100644 --- a/src/oscv_main.c +++ b/src/oscv_main.c @@ -6,10 +6,14 @@ * * Compiles an OSC unit exactly as oscc does and ALSO resolves its `import NAME;` lines, only * through omega.lock -> semantic id -> Verified Crumb Store -> receipt check -> closure. Defaults - * sit next to the source file: omega.lock, omega.vcstore, receipts/. There is no option that - * skips a check (--blobs is optional: leaving it out skips only the source digest recheck, which - * stage 6 makes mandatory), and nothing is read from the environment. There is no genesis option. The only way to change what is legal - * is to change the closure. + * sit next to the source file: omega.lock, omega.vcstore, receipts/ and (build domain only) + * blobs/. There is no option that skips a check, and nothing is read from the environment. In + * the build domain the source/IR store is MANDATORY (SPEC 6 step 3): every imported record's + * blob must be there, hash to its digest and, for an IR digest, recompute to its semantic id, + * else the import is refused (UNVERIFIED_DEPENDENCY). The dev domain may leave --blobs out; its + * output is tainted either way. There is no genesis option: a BOOTSTRAP record satisfies an + * import only if its id is a member of the pinned set VC-GENESIS-1 compiled into the resolver + * (docs/osc/VC-GENESIS-1.md). The only way to change what is legal is to change the closure. * * Output (success): ir_sha256= code_sha256= funcs= domain= tainted= imports= closure_entries= * closure_sha256= build_id= (build_id folds the domain and the closure digest into the identity). @@ -94,10 +98,11 @@ int main(int argc, char **argv) fprintf(stderr, "usage: oscv [--domain dev|build] [--lock F] [--store F] [--receipts D] [--blobs D] [--meta-out F] \n"); return 2; } - char lockd[4096], stored[4096], recd[4096]; + char lockd[4096], stored[4096], recd[4096], blobd[4096]; if (!lockp) { join_dir(lockd, sizeof lockd, file, "omega.lock"); lockp = lockd; } if (!storep) { join_dir(stored, sizeof stored, file, "omega.vcstore"); storep = stored; } if (!recdir) { join_dir(recd, sizeof recd, file, "receipts"); recdir = recd; } + if (!blobdir && domain == OMEGA_DOMAIN_BUILD) { join_dir(blobd, sizeof blobd, file, "blobs"); blobdir = blobd; } size_t slen = 0; int missing; @@ -139,7 +144,7 @@ int main(int argc, char **argv) h.resolver.fetch_receipt = omega_receipt_dir_fetch; h.resolver.fetch_ctx = (void *)recdir; if (blobdir) { h.resolver.fetch_blob = omega_blob_dir_fetch; h.resolver.blob_ctx = (void *)blobdir; } - /* genesis (BOOTSTRAP) records are never permitted here: no option, file or variable turns that on */ + /* a BOOTSTRAP record is accepted only if the resolver's pinned VC-GENESIS-1 lists it: no option, file or variable turns that on */ h.lock = have_lock ? &lock : NULL; OscUnit *u = malloc(sizeof *u); diff --git a/tests/genesis_variant/member.inc b/tests/genesis_variant/member.inc new file mode 100644 index 000000000..aaae6b9ba --- /dev/null +++ b/tests/genesis_variant/member.inc @@ -0,0 +1,2 @@ + { 0x8f, 0x0b, 0x38, 0x8a, 0xad, 0x27, 0xd8, 0x16, 0x5f, 0x88, 0xbd, 0x4b, 0xf9, 0x80, 0xb8, 0x5d, + 0x75, 0xa6, 0x3b, 0x4d, 0x25, 0x32, 0x35, 0xb9, 0x84, 0x91, 0xc1, 0xa3, 0x8a, 0x18, 0xc0, 0x93 }, diff --git a/tests/program/test_program_id.c b/tests/program/test_program_id.c index 707811338..9e2178aaa 100644 --- a/tests/program/test_program_id.c +++ b/tests/program/test_program_id.c @@ -22,6 +22,7 @@ #include "omega_program.h" #include "omega_library.h" #include "omega_discovery.h" +#include "omega_vc_bridge.h" #include "omega_synthesis.h" #include "omega_machine.h" #include "omega_realize.h" @@ -543,7 +544,11 @@ static void test_library(void) { OmegaLibrary *l2 = calloc(1, sizeof *l2); omega_library_init(l2); uint8_t rh[32] = { 1 }; - CHECK(G_LIB, omega_discovery_admit_to_library(l2, cand, rh) == 0 && l2->count == 1, "abstraction admitted"); + OmegaVcBridge *br = calloc(1, sizeof *br); + CHECK(G_LIB, br && omega_vc_bridge_init(br) == 0 && omega_vc_bridge_admit_abstraction(br, l2, cand, rh) == 0 && l2->count == 1, + "abstraction admitted through the Verified Crumb admit path"); + if (br) omega_vc_bridge_destroy(br); + free(br); omega_library_destroy(l2); free(l2); } diff --git a/tests/test_omega_genesis.c b/tests/test_omega_genesis.c new file mode 100644 index 000000000..4f113de8a --- /dev/null +++ b/tests/test_omega_genesis.c @@ -0,0 +1,102 @@ +/* test_omega_genesis.c: the REAL Genesis Set VC-GENESIS-1 (src/omega_genesis.h), pinned. + * + * Changing the set (adding, removing or editing a member, or the set's name) is an ADR-level + * change. This test makes it impossible to do by accident: it fails until the pinned count and + * the audit record docs/osc/VC-GENESIS-1.md name exactly the same members as the table. + * What a LISTED member does is test_omega_genesis_set.c (a variant build with one member). + * Exit 0 only when every check passed. */ +#include +#include +#include + +#include "omega_genesis.h" +#include "vc_fixture.h" + +#define PINNED_COUNT 0u /* the audit result; edit only together with docs/osc/VC-GENESIS-1.md */ + +static int g_total, g_failed; +static void check(const char *name, int ok) { + printf("%s %s\n", ok ? "PASS" : "FAIL", name); + g_total++; + if (!ok) g_failed++; +} +static void hx(const uint8_t *d, char out[65]) { + static const char h[] = "0123456789abcdef"; + for (int i = 0; i < 32; i++) { out[2 * i] = h[d[i] >> 4]; out[2 * i + 1] = h[d[i] & 15]; } + out[64] = 0; +} +static char *slurp(const char *path, size_t *n) { + FILE *f = fopen(path, "rb"); + if (!f) return NULL; + size_t cap = 1 << 16, len = 0; + char *b = malloc(cap + 1); + size_t r; + while (b && (r = fread(b + len, 1, cap - len, f)) > 0) { len += r; if (len == cap) { cap *= 2; b = realloc(b, cap + 1); } } + fclose(f); + if (!b) return NULL; + b[len] = 0; *n = len; + return b; +} + +int main(void) { + check("set-name-is-vc-genesis-1", strcmp(OMEGA_GENESIS_SET_NAME, "VC-GENESIS-1") == 0); + check("member-count-is-the-pinned-audit-result", omega_genesis_count() == PINNED_COUNT && OMEGA_GENESIS_1_COUNT == PINNED_COUNT); + check("member-access-is-bounded", omega_genesis_member(omega_genesis_count()) == NULL && omega_genesis_member((size_t)-1) == NULL); + + int sorted = 1, nonzero = 1, listed = 1; + static const uint8_t zero[32] = { 0 }; + for (size_t i = 0; i < omega_genesis_count(); i++) { + if (memcmp(omega_genesis_member(i), zero, 32) == 0) nonzero = 0; + if (i && memcmp(omega_genesis_member(i - 1), omega_genesis_member(i), 32) >= 0) sorted = 0; + if (!omega_genesis_contains(omega_genesis_member(i))) listed = 0; + } + check("table-rows-are-strictly-ascending-so-no-duplicates", sorted); + check("no-member-is-the-zero-id", nonzero); + check("every-member-is-contained", listed); + check("the-terminator-row-after-the-last-member-is-zero", memcmp(OMEGA_GENESIS_1[OMEGA_GENESIS_1_COUNT], zero, 32) == 0); + check("zero-id-and-null-are-never-members", omega_genesis_contains(zero) == 0 && omega_genesis_contains(NULL) == 0); + + int none = 1; + for (int n = 0; n < 40; n++) if (omega_genesis_contains(vcfx((uint8_t)n)->id)) none = 0; + uint8_t junk[32]; memset(junk, 0xA5, 32); + check("no-program-we-know-and-no-junk-id-is-a-member", none && omega_genesis_contains(junk) == 0); + + /* the audit record names exactly the table */ + size_t dn = 0; + char *doc = slurp("docs/osc/VC-GENESIS-1.md", &dn); + check("audit-record-exists", doc != NULL); + if (doc) { + char want[64]; + snprintf(want, sizeof want, "\nMembers: %zu\n", omega_genesis_count()); + check("audit-record-states-the-same-member-count", strstr(doc, want) != NULL && strstr(doc, "\nMembers: ") == strstr(doc, want)); + int all = 1; + for (size_t i = 0; i < omega_genesis_count(); i++) { char h[65]; hx(omega_genesis_member(i), h); if (!strstr(doc, h)) all = 0; } + check("audit-record-names-every-member-by-its-id", all); + /* no 64-hex id in the record that is not in the table: count them */ + size_t hexruns = 0, run = 0; + for (size_t i = 0; i <= dn; i++) { + char c = doc[i]; + if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) run++; + else { if (run == 64) hexruns++; run = 0; } + } + check("audit-record-lists-no-id-that-is-not-in-the-table", hexruns == omega_genesis_count()); + free(doc); + } + + /* the header cannot be steered from outside: no environment, no files, no switches */ + size_t hn = 0; + char *hdr = slurp("src/omega_genesis.h", &hn); + check("header-is-readable", hdr != NULL); + if (hdr) { + static const char *const bad[] = { "getenv", "fopen", "#ifdef", "#ifndef OMEGA_GENESIS_1", "#if ", "#elif", "#include \"", "extern ", "argv", "secure_getenv" }; + int clean = 1; + for (size_t i = 0; i < sizeof bad / sizeof bad[0]; i++) if (strstr(hdr, bad[i])) { printf("note: header contains %s\n", bad[i]); clean = 0; } + check("header-has-no-environment-file-or-preprocessor-switch", clean); + free(hdr); + } + + printf("%d checks, %d failed\n", g_total, g_failed); + if (g_failed) return 1; + printf("test-genesis-real: PASS\n"); + return 0; +} diff --git a/tests/test_omega_genesis_set.c b/tests/test_omega_genesis_set.c new file mode 100644 index 000000000..568570a3f --- /dev/null +++ b/tests/test_omega_genesis_set.c @@ -0,0 +1,212 @@ +/* test_omega_genesis_set.c: what a LISTED Genesis Set member does (VC1 stage 6). + * + * The real VC-GENESIS-1 is empty (src/omega_genesis.h, docs/osc/VC-GENESIS-1.md), so the listed + * path cannot run against it. This test is built from copies of omega_vcstore.c and + * omega_resolve.c whose single genesis include line is rewritten to + * tests/genesis_variant/omega_genesis.h (one member: the id of fixture 0x10). No hook exists in + * production source: the variant is a different file chosen by the build, never by a flag. + * + * It proves, with a member on the list: the store inserts, saves and loads it; omega_resolve_admit_genesis + * admits it; the resolver lets it satisfy an import in the build domain (with the mandatory source/IR + * recheck); and with the SAME list an id that is not a member is refused at every one of those doors. + * Exit 0 only when every check passed. */ +#include +#include +#include +#include + +#include "genesis_variant/omega_genesis.h" +#include "omega_resolve.h" +#include "omega_vcstore.h" +#include "omega_vcstore_priv.h" +#include "vc_fixture.h" + +static int g_total, g_failed; +static void check(const char *name, int ok) { + printf("%s %s\n", ok ? "PASS" : "FAIL", name); + g_total++; + if (!ok) g_failed++; +} +static void die(const char *what) { fprintf(stderr, "setup failed: %s\n", what); exit(2); } + +typedef struct { uint8_t *p; size_t n, cap; } B; +static void bput(B *b, const void *d, size_t k) { + if (b->n + k > b->cap) { b->cap = (b->n + k) * 2 + 64; b->p = realloc(b->p, b->cap); if (!b->p) die("oom"); } + memcpy(b->p + b->n, d, k); b->n += k; +} +static void bu32(B *b, uint32_t v) { uint8_t t[4] = { (uint8_t)(v >> 24), (uint8_t)(v >> 16), (uint8_t)(v >> 8), (uint8_t)v }; bput(b, t, 4); } +static void bu64(B *b, uint64_t v) { bu32(b, (uint32_t)(v >> 32)); bu32(b, (uint32_t)v); } +static void bstr(B *b, const char *s) { size_t n = strlen(s); bu64(b, n); bput(b, s, n); } +static void hx(const uint8_t *d, char out[65]) { + static const char h[] = "0123456789abcdef"; + for (int i = 0; i < 32; i++) { out[2 * i] = h[d[i] >> 4]; out[2 * i + 1] = h[d[i] & 15]; } + out[64] = 0; +} + +/* a record for fixture n: real program, IR digest, audit-style nonzero receipt_id, no dependencies */ +static void make_record(uint8_t n, const uint8_t *dep64, uint32_t n_dep, B *o) { + const VcFx *f = vcfx(n); + uint8_t con[32], rcpt[32], root[32], kind = OMEGA_VC_DIGEST_IR; + memcpy(con, f->id, 32); con[0] ^= 0x80; + memset(rcpt, 0xA0, 32); rcpt[0] = n; /* for BOOTSTRAP this is the audit record hash */ + memset(root, 0x66, 32); + o->n = 0; + bput(o, "AIEN_VERIFIED_CRUMB_V1", 22); + bu32(o, 1); + bput(o, f->id, 32); bput(o, con, 32); bput(o, &kind, 1); bput(o, f->digest, 32); + bu32(o, 0); + bu32(o, n_dep); if (n_dep) bput(o, dep64, 64 * (size_t)n_dep); + bput(o, rcpt, 32); bstr(o, "host-v1"); bstr(o, "1.0.0"); bput(o, root, 32); + bu32(o, 0); + bu32(o, 0); +} +static int raw_boot(OmegaVcStore *s, uint8_t n) { + B b = { 0, 0, 0 }; uint8_t id[32]; + make_record(n, NULL, 0, &b); + omega_vc_compute_id(b.p, b.n, id); + int rc = omega_vcstore_insert_bootstrap(s, b.p, b.n, id); + free(b.p); + return rc; +} +static int raw_verified(OmegaVcStore *s, uint8_t n) { + B b = { 0, 0, 0 }; uint8_t id[32]; + make_record(n, NULL, 0, &b); + omega_vc_compute_id(b.p, b.n, id); + int rc = omega_vcstore_insert(s, b.p, b.n, id); + free(b.p); + return rc; +} +static int admit_genesis(OmegaVcStore *s, uint8_t n, OmegaResolveError *e) { + B b = { 0, 0, 0 }; uint8_t id[32]; + make_record(n, NULL, 0, &b); + omega_vc_compute_id(b.p, b.n, id); + int rc = omega_resolve_admit_genesis(s, b.p, b.n, id, NULL, e); + free(b.p); + return rc; +} +static void flip(OmegaVcStore *s, uint8_t n, uint8_t kind) { + for (size_t i = 0; i < s->count; i++) + if (memcmp(s->objs[i].semantic_id, vcfx(n)->id, 32) == 0) { s->objs[i].admission_kind = kind; return; } + die("flip: no such object"); +} +static int kind_of(const OmegaVcStore *s, uint8_t n) { + uint8_t k = 0; + return omega_vcstore_admission_kind(s, vcfx(n)->id, &k) == 0 ? (int)k : -1; +} + +static void t_list(void) { + check("variant-list-has-one-member", omega_genesis_count() == 1 && omega_genesis_member(0) != NULL && omega_genesis_member(1) == NULL); + check("variant-member-is-the-program-id-of-fixture-0x10", memcmp(omega_genesis_member(0), vcfx(0x10)->id, 32) == 0); + check("listed-id-is-contained", omega_genesis_contains(vcfx(0x10)->id) == 1); + check("other-real-programs-are-not-contained", omega_genesis_contains(vcfx(0x11)->id) == 0 && omega_genesis_contains(vcfx(0x00)->id) == 0); + uint8_t x[32]; int all = 1; + for (int i = 0; i < 32; i++) { /* no prefix or partial match: one bit anywhere makes it a non-member */ + memcpy(x, vcfx(0x10)->id, 32); x[i] ^= 1; + if (omega_genesis_contains(x)) all = 0; + } + check("one-bit-different-id-is-not-contained-at-any-position", all); + uint8_t z[32] = { 0 }; + check("zero-id-and-null-are-never-members", omega_genesis_contains(z) == 0 && omega_genesis_contains(NULL) == 0); +} + +static void t_store(void) { + OmegaVcStore s; if (omega_vcstore_init(&s)) die("init"); + check("store-inserts-a-listed-bootstrap-record", raw_boot(&s, 0x10) == 0 && kind_of(&s, 0x10) == (int)OMEGA_VCS_ADMISSION_BOOTSTRAP); + check("store-refuses-an-unlisted-bootstrap-record", raw_boot(&s, 0x11) == OMEGA_VCS_GENESIS_NOT_LISTED && omega_vcstore_count(&s) == 1); + check("store-still-inserts-an-unlisted-verified-record", raw_verified(&s, 0x11) == 0 && kind_of(&s, 0x11) == (int)OMEGA_VCS_ADMISSION_VERIFIED); + check("store-refuses-a-bootstrap-record-over-a-verified-one", raw_boot(&s, 0x11) == OMEGA_VCS_GENESIS_NOT_LISTED); + char path[] = "/tmp/omega-gset-XXXXXX"; + int fd = mkstemp(path); if (fd < 0) die("mkstemp"); close(fd); + OmegaVcStore l; + check("store-saves-listed-bootstrap-and-verified-and-loads-them", omega_vcstore_save(&s, path) == 0 && omega_vcstore_init(&l) == 0 && + omega_vcstore_load(&l, path) == 0 && omega_vcstore_count(&l) == 2 && kind_of(&l, 0x10) == (int)OMEGA_VCS_ADMISSION_BOOTSTRAP); + omega_vcstore_destroy(&l); + flip(&s, 0x11, OMEGA_VCS_ADMISSION_BOOTSTRAP); /* an unlisted id marked BOOTSTRAP, written to a file */ + int sv = omega_vcstore_save(&s, path); + int init = omega_vcstore_init(&l); + int ld = omega_vcstore_load(&l, path); + check("store-load-refuses-a-file-with-an-unlisted-bootstrap-record", sv == 0 && init == 0 && ld == OMEGA_VCS_GENESIS_NOT_LISTED && omega_vcstore_count(&l) == 0); + omega_vcstore_destroy(&l); + unlink(path); + omega_vcstore_destroy(&s); +} + +static void t_admit(void) { + OmegaVcStore s; OmegaResolveError e; + if (omega_vcstore_init(&s)) die("init"); + memset(&e, 0, sizeof e); + check("admit-genesis-admits-a-listed-member", admit_genesis(&s, 0x10, &e) == 0 && kind_of(&s, 0x10) == (int)OMEGA_VCS_ADMISSION_BOOTSTRAP); + memset(&e, 0, sizeof e); + int rc = admit_genesis(&s, 0x11, &e); + check("admit-genesis-refuses-a-record-that-is-not-a-member", rc == OMEGA_RES_UNVERIFIED_DEPENDENCY && e.store_code == OMEGA_VCS_GENESIS_NOT_LISTED && strstr(e.message, "pinned genesis set") != NULL && + omega_vcstore_count(&s) == 1); + omega_vcstore_destroy(&s); +} + +static const char *g_lock_name = "gen"; +static void lock_for(const OmegaVcStore *s, uint8_t n, char *buf, size_t cap) { + uint8_t rid[32]; char a[65], b[65]; + if (omega_vcstore_receipt_of(s, vcfx(n)->id, rid) != 0) die("receipt_of"); + hx(vcfx(n)->id, a); hx(rid, b); + snprintf(buf, cap, "omega.lock v1\n%s = semantic %s receipt %s\n", g_lock_name, a, b); +} +static int resolve_one(const OmegaVcStore *s, OmegaDomain d, int blobs, char *lock_text, OmegaResolveError *e) { + OmegaResolver r; memset(&r, 0, sizeof r); + r.store = s; r.domain = d; + if (blobs) r.fetch_blob = vcfx_fetch_blob; + OmegaLock lock; OmegaResolveError le; + if (omega_lock_parse(lock_text, strlen(lock_text), &lock, &le)) die("lock"); + const char *names[1] = { g_lock_name }; + OmegaClosure c; memset(&c, 0, sizeof c); + memset(e, 0, sizeof *e); + int rc = omega_resolve_imports(&r, &lock, names, 1, &c, e); + int closure_ok = rc == 0 && c.n == 1 && c.entries[0].admission_kind == OMEGA_VCS_ADMISSION_BOOTSTRAP && + memcmp(c.entries[0].semantic_id, vcfx(0x10)->id, 32) == 0; + omega_closure_free(&c); + omega_lock_free(&lock); + return rc == 0 ? (closure_ok ? 0 : -1) : rc; +} +static void t_resolve(void) { + OmegaVcStore s; OmegaResolveError e; char lock[400]; + if (omega_vcstore_init(&s)) die("init"); + if (admit_genesis(&s, 0x10, &e) != 0) die("admit listed"); + lock_for(&s, 0x10, lock, sizeof lock); + check("resolver-lets-a-listed-bootstrap-record-satisfy-an-import-in-the-build-domain", resolve_one(&s, OMEGA_DOMAIN_BUILD, 1, lock, &e) == 0); + check("a-listed-bootstrap-record-still-needs-the-blob-store-in-the-build-domain", resolve_one(&s, OMEGA_DOMAIN_BUILD, 0, lock, &e) == OMEGA_RES_UNVERIFIED_DEPENDENCY); + check("resolver-lets-a-listed-bootstrap-record-satisfy-an-import-in-the-dev-domain", resolve_one(&s, OMEGA_DOMAIN_DEV, 0, lock, &e) == 0); + omega_vcstore_destroy(&s); + + /* the same list, an id that is not on it, kind BOOTSTRAP made by tampering: refused by the resolver itself */ + if (omega_vcstore_init(&s)) die("init"); + if (raw_verified(&s, 0x10) != 0) die("insert verified 0x10"); + flip(&s, 0x10, OMEGA_VCS_ADMISSION_BOOTSTRAP); /* listed id: fine */ + lock_for(&s, 0x10, lock, sizeof lock); + check("resolver-accepts-a-listed-id-whatever-route-put-it-in-the-store", resolve_one(&s, OMEGA_DOMAIN_BUILD, 1, lock, &e) == 0); + omega_vcstore_destroy(&s); + + if (omega_vcstore_init(&s)) die("init"); + if (raw_verified(&s, 0x11) != 0) die("insert verified 0x11"); + flip(&s, 0x11, OMEGA_VCS_ADMISSION_BOOTSTRAP); + lock_for(&s, 0x11, lock, sizeof lock); + OmegaResolver r; memset(&r, 0, sizeof r); r.store = &s; r.domain = OMEGA_DOMAIN_BUILD; r.fetch_blob = vcfx_fetch_blob; + OmegaLock lk; OmegaResolveError le; + if (omega_lock_parse(lock, strlen(lock), &lk, &le)) die("lock"); + const char *names[1] = { g_lock_name }; + OmegaClosure c; memset(&c, 0, sizeof c); memset(&e, 0, sizeof e); + int rc = omega_resolve_imports(&r, &lk, names, 1, &c, &e); + check("resolver-refuses-an-unlisted-bootstrap-record-even-with-the-list-non-empty", + rc == OMEGA_RES_UNVERIFIED_DEPENDENCY && e.store_code == OMEGA_VCS_GENESIS_NOT_LISTED && c.n == 0); + omega_closure_free(&c); omega_lock_free(&lk); + omega_vcstore_destroy(&s); +} + +int main(void) { + t_list(); + t_store(); + t_admit(); + t_resolve(); + printf("%d checks, %d failed\n", g_total, g_failed); + if (g_failed) return 1; + printf("test-genesis-set: PASS\n"); + return 0; +} diff --git a/tests/test_omega_program_ir.c b/tests/test_omega_program_ir.c new file mode 100644 index 000000000..dd216d8ff --- /dev/null +++ b/tests/test_omega_program_ir.c @@ -0,0 +1,126 @@ +/* test_omega_program_ir.c: the canonical program IR and the id recomputation behind the + * mandatory source/IR recheck (VC1 stage 6, SPEC 6 step 3). Each guard in + * src/omega_program_ir.c carries a VC1I tag; the Makefile target test-program-ir breaks each tag + * in a copy and requires the named check to FAIL. CPU only. */ +#include +#include +#include + +#include "omega_program.h" +#include "omega_program_ir.h" + +static int g_total, g_failed; +static void check(const char *name, int ok) { + printf("%s %s\n", ok ? "PASS" : "FAIL", name); + g_total++; + if (!ok) g_failed++; +} +static OmegaProgram *newprog(int op, uint64_t imm) { + OmegaProgram *p = calloc(1, sizeof *p); + if (!p || omega_program_build_unary_op(p, "ir-test", op, imm) != 0) { fprintf(stderr, "setup failed\n"); exit(2); } + return p; +} +/* fixed offsets of the layout in omega_program_ir.h */ +enum { OFF_IN_TYPE = 24, OFF_STEPS = 24 + 1 + 2 + 1 + 2 + 32 + 32, OFF_FIRST_STEP = OFF_STEPS + 2 }; + +static void t_roundtrip(void) { + static const int ops[5] = { OP_ADD, OP_SUB, OP_MUL, OP_AND, OP_OR }; + static const uint64_t imms[4] = { 0, 1, 0x01234567ULL, 0xffffffffULL }; + int all = 1, same_id = 1, canon = 1, distinct = 1; + uint8_t seen[20][32]; int ns = 0; + for (int a = 0; a < 5; a++) + for (int b = 0; b < 4; b++) { + OmegaProgram *p = newprog(ops[a], imms[b]); + uint8_t *ir = NULL; size_t n = 0; + OmegaProgram *q = calloc(1, sizeof *q); + if (omega_program_ir_encode(p, &ir, &n) != 0) { all = 0; goto next; } + if (omega_program_ir_decode(ir, n, q) != 0) { all = 0; goto next; } + if (memcmp(q->program_id.bytes, p->program_id.bytes, 32) != 0) same_id = 0; + uint8_t id[32]; + if (omega_program_ir_recompute_id(ir, n, id) != 0 || memcmp(id, p->program_id.bytes, 32) != 0) same_id = 0; + uint8_t *ir2 = NULL; size_t n2 = 0; + if (omega_program_ir_encode(q, &ir2, &n2) != 0 || n2 != n || memcmp(ir, ir2, n) != 0) canon = 0; + free(ir2); + for (int k = 0; k < ns; k++) if (memcmp(seen[k], id, 32) == 0) distinct = 0; + memcpy(seen[ns++], id, 32); +next: + free(ir); free(q); free(p); + } + check("ir-roundtrips-every-op-and-immediate", all); + check("ir-recomputes-the-program-id-exactly", same_id); + check("ir-encoding-is-canonical-decode-then-encode-is-identical", canon); + check("ir-different-programs-have-different-recomputed-ids", distinct); +} + +static void t_refusals(void) { + OmegaProgram *p = newprog(OP_ADD, 17); + uint8_t *ir = NULL; size_t n = 0; + if (omega_program_ir_encode(p, &ir, &n) != 0) { fprintf(stderr, "setup failed\n"); exit(2); } + OmegaProgram *q = calloc(1, sizeof *q); + uint8_t id[32]; + + check("ir-has-the-documented-length-for-one-step", n == (size_t)OFF_FIRST_STEP + 9); + check("ir-refuses-null-arguments", omega_program_ir_decode(NULL, n, q) != 0 && omega_program_ir_decode(ir, n, NULL) != 0 && omega_program_ir_recompute_id(NULL, 0, id) != 0); + + int all = 1; + for (size_t k = 0; k < n; k++) { /* an exact-size heap copy, so a read past the end is a sanitizer error, not a silent success */ + uint8_t *cut = malloc(k ? k : 1); + if (!cut) { fprintf(stderr, "setup failed\n"); exit(2); } + memcpy(cut, ir, k); + if (omega_program_ir_decode(cut, k, q) == 0) all = 0; + free(cut); + } + check("ir-refuses-truncation-at-every-length", all); + + uint8_t *t = malloc(n + 1); memcpy(t, ir, n); t[n] = 0; + check("ir-refuses-trailing-bytes", omega_program_ir_decode(t, n + 1, q) != 0 && omega_program_ir_decode(ir, n, q) == 0); + + memcpy(t, ir, n); t[0] ^= 1; + check("ir-refuses-wrong-tag", omega_program_ir_decode(t, n, q) != 0); + + /* 65 steps, all of them present: the limit refuses, not truncation */ + { + size_t m = (size_t)OFF_STEPS + 2 + 65 * 9; + uint8_t *big = calloc(1, m); + memcpy(big, ir, OFF_STEPS); + big[OFF_STEPS] = 0; big[OFF_STEPS + 1] = 65; + for (int i = 0; i < 65; i++) big[OFF_FIRST_STEP + 9 * i] = OP_ADD; + check("ir-refuses-more-steps-than-the-program-limit", omega_program_ir_decode(big, m, q) != 0); + free(big); + } + /* far over the limit with all the data present: the decoder must stop before it writes past its output */ + { + size_t m = (size_t)OFF_STEPS + 2 + 4000 * 9; + uint8_t *big = calloc(1, m); + memcpy(big, ir, OFF_STEPS); + big[OFF_STEPS] = (uint8_t)(4000 >> 8); big[OFF_STEPS + 1] = (uint8_t)(4000 & 255); + for (int i = 0; i < 4000; i++) big[OFF_FIRST_STEP + 9 * i] = OP_ADD; + check("ir-refuses-a-huge-step-count-without-writing-past-its-output", omega_program_ir_decode(big, m, q) != 0); + free(big); + } + + /* an input type the program id does not accept: refused, never a made-up id */ + memcpy(t, ir, n); t[OFF_IN_TYPE] = 0xEE; + check("ir-refuses-a-type-the-program-id-cannot-take", omega_program_ir_decode(t, n, q) != 0); + + /* injectivity: flipping any single bit either refuses or changes the id (one program, one byte string) */ + int inj = 1; + for (size_t i = 0; i < n; i++) + for (int b = 0; b < 8; b++) { + memcpy(t, ir, n); t[i] ^= (uint8_t)(1u << b); + if (omega_program_ir_recompute_id(t, n, id) == 0 && memcmp(id, p->program_id.bytes, 32) == 0) inj = 0; + } + check("ir-no-single-bit-change-keeps-the-same-id", inj); + + check("ir-encode-refuses-a-program-with-no-body", ({ OmegaProgram *e = calloc(1, sizeof *e); uint8_t *o = NULL; size_t l = 0; int r = omega_program_ir_encode(e, &o, &l); free(e); r != 0; })); + free(t); free(q); free(ir); free(p); +} + +int main(void) { + t_roundtrip(); + t_refusals(); + printf("%d checks, %d failed\n", g_total, g_failed); + if (g_failed) return 1; + printf("test-program-ir: PASS\n"); + return 0; +} diff --git a/tests/test_omega_resolve.c b/tests/test_omega_resolve.c index d41dbdb11..56e5cd74d 100644 --- a/tests/test_omega_resolve.c +++ b/tests/test_omega_resolve.c @@ -18,6 +18,7 @@ * test, which is anchored by the Rust receipts above. */ #include +#include #include #include #include @@ -26,13 +27,16 @@ #include #include "omega_blake3.h" +#include "omega_genesis.h" #include "omega_receipt.h" #include "omega_resolve.h" #include "omega_resolve_osc.h" #include "omega_vcstore.h" +#include "omega_vcstore_priv.h" #include "osc_front.h" #include "osc_cg.h" #include "sha256.h" +#include "vc_fixture.h" #ifndef OSCV_PATH #define OSCV_PATH "build/compiler/oscv" @@ -72,7 +76,8 @@ static void hx(const uint8_t *d, char out[65]) { for (int i = 0; i < 32; i++) { out[2 * i] = h[d[i] >> 4]; out[2 * i + 1] = h[d[i] & 15]; } out[64] = 0; } -static void mkid(uint8_t out[32], uint8_t b0) { memset(out, 0x77, 32); out[0] = b0; } +/* fixture program b0: a real program (tests/vc_fixture.h), so the build-domain recompute can hold */ +static void mkid(uint8_t out[32], uint8_t b0) { memcpy(out, vcfx(b0)->id, 32); } static void con_of(const uint8_t sid[32], uint8_t out[32]) { memcpy(out, sid, 32); out[0] ^= 0x80; } /* ---- test-side Verified Crumb encoder (writes lists exactly as given) ---- */ @@ -188,7 +193,8 @@ typedef struct { OmegaVcStore *st; RTab rt; uint8_t last_rid[32]; - int allow_genesis; + int no_blobs; /* the resolver gets no source/IR store */ + int wrong_blobs; /* the source/IR store answers every digest with the same wrong bytes */ } W; static void w_init(W *w) { memset(w, 0, sizeof *w); @@ -204,26 +210,39 @@ static void w_free(W *w) { typedef struct { const char *prof, *ver, *tier, *result; int assert_fail, skip_sid, stale_src, wrong_root, extra_rdep, wrong_kind, no_store, swap, taint, boot, lease, dirty; + int src_of; /* the record's digest is the IR digest of fixture src_of (another program) */ + int kind_source; /* digest kind OSC source, digest of the source stand-in */ + int kind_garbage; /* digest kind IR, digest of bytes that are not an IR */ } Opt; static Opt opt0(void) { Opt o; memset(&o, 0, sizeof o); o.prof = "host-v1"; o.ver = "1.0.0"; o.tier = "HOST_TEST"; o.result = "PASS"; return o; } +/* VC-GENESIS-1 is EMPTY in the real source, so a BOOTSTRAP record cannot be inserted here. A fixture that + * needs the BOOTSTRAP kind (to show the resolver refuses an unlisted one) inserts the record as VERIFIED and + * flips the kind byte in the store, whose fields the header declares public for tampering. Listed members, + * with a non-empty list, are test_omega_genesis_set.c. */ +static void flip_boot(OmegaVcStore *st, const uint8_t sid[32]) { + for (size_t i = 0; i < st->count; i++) + if (memcmp(st->objs[i].semantic_id, sid, 32) == 0) { st->objs[i].admission_kind = OMEGA_VCS_ADMISSION_BOOTSTRAP; return; } + setup_fail("flip_boot: no such object"); +} static int ins_spec(OmegaVcStore *st, const Spec *sp, int boot) { B b = { 0, 0, 0 }; encode(&b, sp); uint8_t id[32]; omega_vc_compute_id(b.p, b.n, id); - int rc = boot ? omega_vcstore_insert_bootstrap(st, b.p, b.n, id) : omega_vcstore_insert(st, b.p, b.n, id); + int rc = omega_vcstore_insert(st, b.p, b.n, id); + if (rc == 0 && boot) flip_boot(st, sp->sem); free(b.p); return rc; } static void spec_for(Spec *sp, uint8_t b0, const uint8_t (*deps)[32], uint32_t nd, uint8_t (*dbuf)[64]) { memset(sp, 0, sizeof *sp); - sp->ver = 1; sp->kind = 1; + sp->ver = 1; sp->kind = OMEGA_VC_DIGEST_IR; mkid(sp->sem, b0); con_of(sp->sem, sp->con); - memset(sp->src, 0x33, 32); sp->src[0] = b0; + memcpy(sp->src, vcfx(b0)->digest, 32); memset(sp->root, 0x66, 32); sp->root[0] = b0; memset(sp->rcpt, 0x55, 32); for (uint32_t i = 0; i < nd; i++) { memcpy(dbuf[i], deps[i], 32); con_of(deps[i], dbuf[i] + 32); } @@ -231,11 +250,21 @@ static void spec_for(Spec *sp, uint8_t b0, const uint8_t (*deps)[32], uint32_t n sp->prof = "host-v1"; sp->pver = "1.0.0"; } /* add one component; returns the insert result. Its receipt id is left in w->last_rid. */ -static int add_comp(W *w, uint8_t b0, const uint8_t (*deps)[32], uint32_t nd, const Opt *o) { +static int add_comp(W *w, uint8_t b0, const uint8_t (*deps_in)[32], uint32_t nd, const Opt *o) { uint8_t dbuf[8][64]; + uint8_t deps[8][32]; /* real ids have no useful order: keep the dependency list ascending, as the store requires */ + if (nd > 8) setup_fail("too many dependencies"); + if (nd) memcpy(deps, deps_in, 32 * (size_t)nd); + for (uint32_t a = 1; a < nd; a++) + for (uint32_t b = a; b > 0 && memcmp(deps[b - 1], deps[b], 32) > 0; b--) { + uint8_t t[32]; memcpy(t, deps[b - 1], 32); memcpy(deps[b - 1], deps[b], 32); memcpy(deps[b], t, 32); + } Spec sp; spec_for(&sp, b0, deps, nd, dbuf); sp.prof = o->prof; sp.pver = o->ver; + if (o->src_of) memcpy(sp.src, vcfx((uint8_t)o->src_of)->digest, 32); + if (o->kind_source) { sp.kind = OMEGA_VC_DIGEST_SOURCE; vcfx_source_digest(sp.src); } + if (o->kind_garbage) vcfx_garbage_digest(sp.src); const char *taintcap[1] = { OMEGA_TAINT_CAPABILITY }; if (o->taint) { sp.n_cap = 1; sp.cap = taintcap; } if (o->boot) { @@ -326,7 +355,7 @@ static void run_compile(W *w, const char *src, OmegaDomain d, const char *lockte r->h.resolver.domain = d; r->h.resolver.fetch_receipt = tab_fetch; r->h.resolver.fetch_ctx = &w->rt; - r->h.resolver.allow_genesis = w->allow_genesis; + if (!w->no_blobs) { r->h.resolver.fetch_blob = w->wrong_blobs ? vcfx_fetch_wrong : vcfx_fetch_blob; } r->rc = osc_compile_imports(src, strlen(src), r->u, &r->dg, NULL, omega_osc_import_hook, &r->h); } static void run_free(Run *r) { @@ -693,6 +722,7 @@ static void t_refusals(void) { o = opt0(); o.dirty = 1; one_foo(&o, OMEGA_DOMAIN_BUILD, &r, &w); check("refuse-dirty-receipt-in-build", refused(&r, "UNVERIFIED_DEPENDENCY", OMEGA_RES_UNVERIFIED_DEPENDENCY)); + check("refuse-dirty-receipt-in-build-says-dirty", refused_msg(&r, "UNVERIFIED_DEPENDENCY", OMEGA_RES_UNVERIFIED_DEPENDENCY, "dirty checkout")); run_free(&r); w_free(&w); one_foo(&o, OMEGA_DOMAIN_DEV, &r, &w); check("allow-dirty-receipt-in-dev", r.rc == 0); @@ -848,63 +878,68 @@ static void t_refusals(void) { check("accept-hardware-receipt-with-lease", r.rc == 0); run_free(&r); w_free(&w); - /* blob check (optional source/IR store of stage 6): a blob that does not hash to the digest */ + /* SPEC 6 step 3: the source/IR recheck. The build domain REQUIRES the store, the blob must hash to its + * digest, and an IR blob must recompute to the semantic id. */ { - w_init(&w); - o = opt0(); add_comp(&w, 0x51, NULL, 0, &o); + W w2; Run r2; Opt oo = opt0(); + w_init(&w2); add_comp(&w2, 0x51, NULL, 0, &oo); Pin p[1] = { { "foo", 0x51, 0 } }; const char *nm[1] = { "foo" }; - const char *lk = lock_text(&w, p, 1); - char dir[] = "/tmp/omega-resolve-blobs-XXXXXX"; - if (!mkdtemp(dir)) setup_fail("mkdtemp"); - uint8_t src[32]; memset(src, 0x33, 32); src[0] = 0x51; - char hh[65], path[300]; hx(src, hh); - snprintf(path, sizeof path, "%s/%s.blob", dir, hh); - FILE *f = fopen(path, "wb"); fputs("not the source", f); fclose(f); - (void)src_imports(nm, 1); - memset(&r, 0, sizeof r); - r.u = malloc(sizeof *r.u); - OmegaResolveError le; omega_lock_parse(lk, strlen(lk), &r.lock, &le); - r.h.lock = &r.lock; r.h.resolver.store = w.st; r.h.resolver.domain = OMEGA_DOMAIN_BUILD; - r.h.resolver.fetch_receipt = tab_fetch; r.h.resolver.fetch_ctx = &w.rt; - r.h.resolver.fetch_blob = omega_blob_dir_fetch; r.h.resolver.blob_ctx = dir; - r.rc = osc_compile_imports(g_src, strlen(g_src), r.u, &r.dg, NULL, omega_osc_import_hook, &r.h); - check("refuse-blob-does-not-hash-to-digest", refused(&r, "UNVERIFIED_DEPENDENCY", OMEGA_RES_UNVERIFIED_DEPENDENCY)); - run_free(&r); - /* a blob that does hash to the digest passes */ - uint8_t blobdig[32]; const char *content = "the real source"; - sha256_hash((const uint8_t *)content, strlen(content), blobdig); - /* record 0x52 whose source_or_ir_digest is the digest of the blob we write */ - { - uint8_t dbuf[1][64]; Spec sp; spec_for(&sp, 0x52, NULL, 0, dbuf); - memcpy(sp.src, blobdig, 32); - char sid_h[65], src_h[65], root_h[65], inp0[80], inp1[80]; hx(sp.sem, sid_h); hx(sp.src, src_h); hx(sp.root, root_h); - RJ j; rj_default(&j); - snprintf(inp0, sizeof inp0, "sha256:%s", sid_h); snprintf(inp1, sizeof inp1, "sha256:%s", src_h); - j.inputs[0] = inp0; j.inputs[1] = inp1; j.n_in = 2; memcpy(j.root, root_h, 65); - uint8_t rid[32]; char *json; size_t jl; - if (rj_make(&j, rid, &json, &jl)) setup_fail("blob receipt"); - memcpy(sp.rcpt, rid, 32); tab_put(&w.rt, rid, json, jl); free(json); - if (ins_spec(w.st, &sp, 0) != 0) setup_fail("blob record"); - snprintf(path, sizeof path, "%s/%s.blob", dir, src_h); - f = fopen(path, "wb"); fputs(content, f); fclose(f); - } - Pin p2[1] = { { "foo", 0x52, 0 } }; - const char *lk2 = lock_text(&w, p2, 1); - memset(&r, 0, sizeof r); - r.u = malloc(sizeof *r.u); - omega_lock_parse(lk2, strlen(lk2), &r.lock, &le); - r.h.lock = &r.lock; r.h.resolver.store = w.st; r.h.resolver.domain = OMEGA_DOMAIN_BUILD; - r.h.resolver.fetch_receipt = tab_fetch; r.h.resolver.fetch_ctx = &w.rt; - r.h.resolver.fetch_blob = omega_blob_dir_fetch; r.h.resolver.blob_ctx = dir; - r.rc = osc_compile_imports(g_src, strlen(g_src), r.u, &r.dg, NULL, omega_osc_import_hook, &r.h); - check("accept-blob-that-hashes-to-digest", r.rc == 0); - run_free(&r); w_free(&w); - char cmd[400]; snprintf(cmd, sizeof cmd, "rm -rf %s", dir); - if (system(cmd) != 0) setup_fail("cleanup"); + w2.no_blobs = 1; + run_compile(&w2, src_imports(nm, 1), OMEGA_DOMAIN_BUILD, lock_text(&w2, p, 1), &r2); + check("refuse-build-domain-without-blob-store", refused_msg(&r2, "UNVERIFIED_DEPENDENCY", OMEGA_RES_UNVERIFIED_DEPENDENCY, "needs the source or IR store") && r2.h.closure.n == 0); + run_free(&r2); + run_compile(&w2, src_imports(nm, 1), OMEGA_DOMAIN_DEV, lock_text(&w2, p, 1), &r2); + check("accept-dev-domain-without-blob-store", r2.rc == 0 && r2.h.closure.n == 1); + run_free(&r2); + w2.no_blobs = 0; w2.wrong_blobs = 1; + run_compile(&w2, src_imports(nm, 1), OMEGA_DOMAIN_BUILD, lock_text(&w2, p, 1), &r2); + check("refuse-blob-does-not-hash-to-digest", refused_msg(&r2, "UNVERIFIED_DEPENDENCY", OMEGA_RES_UNVERIFIED_DEPENDENCY, "does not hash")); + run_free(&r2); + run_compile(&w2, src_imports(nm, 1), OMEGA_DOMAIN_DEV, lock_text(&w2, p, 1), &r2); + check("refuse-blob-does-not-hash-in-dev-domain-too", refused(&r2, "UNVERIFIED_DEPENDENCY", OMEGA_RES_UNVERIFIED_DEPENDENCY)); + run_free(&r2); + w2.wrong_blobs = 0; + run_compile(&w2, src_imports(nm, 1), OMEGA_DOMAIN_BUILD, lock_text(&w2, p, 1), &r2); + check("accept-ir-blob-that-hashes-and-recomputes", r2.rc == 0 && r2.h.closure.n == 1); + run_free(&r2); w_free(&w2); + } + { /* a record whose digest is the IR digest of ANOTHER program: the blob hashes fine, the id does not recompute */ + W w2; Run r2; Opt oo = opt0(); oo.src_of = 0x77; + w_init(&w2); (void)vcfx(0x77); + if (add_comp(&w2, 0x51, NULL, 0, &oo) != 0) setup_fail("mismatch insert"); + Pin p[1] = { { "foo", 0x51, 0 } }; const char *nm[1] = { "foo" }; + run_compile(&w2, src_imports(nm, 1), OMEGA_DOMAIN_BUILD, lock_text(&w2, p, 1), &r2); + check("refuse-ir-that-does-not-recompute-to-semantic-id", refused_msg(&r2, "UNVERIFIED_DEPENDENCY", OMEGA_RES_UNVERIFIED_DEPENDENCY, "does not recompute") && r2.h.closure.n == 0); + run_free(&r2); + run_compile(&w2, src_imports(nm, 1), OMEGA_DOMAIN_DEV, lock_text(&w2, p, 1), &r2); + check("refuse-ir-mismatch-in-dev-domain-when-blobs-are-given", refused_msg(&r2, "UNVERIFIED_DEPENDENCY", OMEGA_RES_UNVERIFIED_DEPENDENCY, "does not recompute")); + run_free(&r2); w_free(&w2); + } + { /* bytes that hash to the digest but are not an IR at all */ + W w2; Run r2; Opt oo = opt0(); oo.kind_garbage = 1; + w_init(&w2); + if (add_comp(&w2, 0x51, NULL, 0, &oo) != 0) setup_fail("garbage insert"); + Pin p[1] = { { "foo", 0x51, 0 } }; const char *nm[1] = { "foo" }; + run_compile(&w2, src_imports(nm, 1), OMEGA_DOMAIN_BUILD, lock_text(&w2, p, 1), &r2); + check("refuse-blob-that-is-not-an-ir", refused_msg(&r2, "UNVERIFIED_DEPENDENCY", OMEGA_RES_UNVERIFIED_DEPENDENCY, "does not recompute")); + run_free(&r2); w_free(&w2); + } + { /* an OSC-source digest cannot be recomputed to a program id: the build domain refuses it, dev checks the digest only */ + W w2; Run r2; Opt oo = opt0(); oo.kind_source = 1; + w_init(&w2); + if (add_comp(&w2, 0x51, NULL, 0, &oo) != 0) setup_fail("source insert"); + Pin p[1] = { { "foo", 0x51, 0 } }; const char *nm[1] = { "foo" }; + run_compile(&w2, src_imports(nm, 1), OMEGA_DOMAIN_BUILD, lock_text(&w2, p, 1), &r2); + check("refuse-source-digest-record-in-build-domain", refused_msg(&r2, "UNVERIFIED_DEPENDENCY", OMEGA_RES_UNVERIFIED_DEPENDENCY, "OSC source") && r2.h.closure.n == 0); + run_free(&r2); + run_compile(&w2, src_imports(nm, 1), OMEGA_DOMAIN_DEV, lock_text(&w2, p, 1), &r2); + check("accept-source-digest-record-in-dev-domain", r2.rc == 0 && r2.h.closure.n == 1); + run_free(&r2); w_free(&w2); } } /* ---- graphs: closure, cycle, edges, genesis ---- */ +static int cmp_ent32(const void *a, const void *b) { return memcmp(a, b, 32); } static void t_graphs(void) { W w; Run r; Opt o = opt0(); @@ -921,13 +956,16 @@ static void t_graphs(void) { int ok = r.rc == 0 && r.h.closure.n == 4; OmegaClosureEntry want[4]; static const uint8_t ids[4] = { 0x10, 0x20, 0x30, 0x40 }; - for (int i = 0; i < 4 && ok; i++) { + for (int i = 0; i < 4; i++) { mkid(want[i].semantic_id, ids[i]); omega_vcstore_receipt_of(w.st, want[i].semantic_id, want[i].receipt_id); want[i].admission_kind = 1; + } + _Static_assert(offsetof(OmegaClosureEntry, semantic_id) == 0, "sorted by the first 32 bytes"); + qsort(want, 4, sizeof want[0], cmp_ent32); /* the reference order: ascending real semantic id */ + for (int i = 0; i < 4 && ok; i++) if (memcmp(r.h.closure.entries[i].semantic_id, want[i].semantic_id, 32) || memcmp(r.h.closure.entries[i].receipt_id, want[i].receipt_id, 32) || r.h.closure.entries[i].admission_kind != 1) ok = 0; - } uint8_t rd[32]; if (ok) ref_closure_digest(want, 4, rd); check("closure-diamond-has-each-node-once-in-id-order", ok); @@ -944,16 +982,21 @@ static void t_graphs(void) { /* resolution order differs from id order: names alpha -> 0x90, zeta -> 0x10 */ w_free(&w); w_init(&w); - add_comp(&w, 0x90, NULL, 0, &o); add_comp(&w, 0x10, NULL, 0, &o); - { Pin p[2] = { { "alpha", 0x90, 0 }, { "zeta", 0x10, 0 } }; const char *nm[2] = { "alpha", "zeta" }; + /* alpha (resolved first) gets the LARGER real id, zeta the smaller one */ + uint8_t big = 0x90, small = 0x10; + if (memcmp(vcfx(big)->id, vcfx(small)->id, 32) < 0) { big = 0x10; small = 0x90; } + add_comp(&w, big, NULL, 0, &o); add_comp(&w, small, NULL, 0, &o); + { Pin p[2] = { { "alpha", big, 0 }, { "zeta", small, 0 } }; const char *nm[2] = { "alpha", "zeta" }; run_compile(&w, src_imports(nm, 2), OMEGA_DOMAIN_BUILD, lock_text(&w, p, 2), &r); } int sorted = r.rc == 0 && r.h.closure.n == 2 && memcmp(r.h.closure.entries[0].semantic_id, r.h.closure.entries[1].semantic_id, 32) < 0 && - r.h.closure.entries[0].semantic_id[0] == 0x10; + memcmp(r.h.closure.entries[0].semantic_id, vcfx(small)->id, 32) == 0; check("closure-sorted-ascending-by-semantic-id", sorted); run_free(&r); w_free(&w); - /* DEPENDENCY_CYCLE: only a tampered store can hold one (insert refuses it), shown with genesis records */ - w_init(&w); w.allow_genesis = 1; + /* DEPENDENCY_CYCLE: only a tampered store can hold one (insert refuses it), shown with genesis-kind records + * (no receipts to contradict a cycle). The gate that refuses unlisted genesis records sits AFTER the walk, so the + * cycle is still found first. */ + w_init(&w); { Opt b = opt0(); b.boot = 1; uint8_t d[1][32]; add_comp(&w, 0x71, NULL, 0, &b); mkid(d[0], 0x71); add_comp(&w, 0x72, d, 1, &b); @@ -979,34 +1022,30 @@ static void t_graphs(void) { check("refuse-edge-contract-mismatch", refused(&r, "UNVERIFIED_DEPENDENCY", OMEGA_RES_UNVERIFIED_DEPENDENCY) && strstr(r.h.err.message, "contract") != NULL); run_free(&r); w_free(&w); - /* genesis (BOOTSTRAP) records */ + /* genesis (BOOTSTRAP) records. VC-GENESIS-1 is empty, so every BOOTSTRAP record in this file is an unlisted one + * (a flipped fixture) and must be refused wherever it sits in a closure. Listed members: test_omega_genesis_set.c. */ { Opt b = opt0(); b.boot = 1; w_init(&w); add_comp(&w, 0x91, NULL, 0, &b); Pin p[1] = { { "base", 0x91, 0 } }; const char *nm[1] = { "base" }; const char *lk = lock_text(&w, p, 1); run_compile(&w, src_imports(nm, 1), OMEGA_DOMAIN_BUILD, lk, &r); - check("refuse-genesis-record-without-permission", refused_msg(&r, "UNVERIFIED_DEPENDENCY", OMEGA_RES_UNVERIFIED_DEPENDENCY, "genesis")); + check("refuse-genesis-record-without-permission", refused_msg(&r, "UNVERIFIED_DEPENDENCY", OMEGA_RES_UNVERIFIED_DEPENDENCY, "genesis") && + r.h.err.store_code == OMEGA_VCS_GENESIS_NOT_LISTED && !omega_genesis_contains(vcfx(0x91)->id) && r.h.closure.n == 0); run_free(&r); - w.allow_genesis = 1; - run_compile(&w, src_imports(nm, 1), OMEGA_DOMAIN_BUILD, lk, &r); - check("accept-genesis-record-with-permission", r.rc == 0 && r.h.closure.n == 1 && r.h.closure.entries[0].admission_kind == 2); + run_compile(&w, src_imports(nm, 1), OMEGA_DOMAIN_DEV, lk, &r); + check("refuse-genesis-record-in-dev-domain-too", refused(&r, "UNVERIFIED_DEPENDENCY", OMEGA_RES_UNVERIFIED_DEPENDENCY) && r.h.err.store_code == OMEGA_VCS_GENESIS_NOT_LISTED); run_free(&r); - /* a verified record on top of a genesis record needs the permission too */ + /* a verified record on top of an unlisted genesis record is refused too */ uint8_t d[1][32]; mkid(d[0], 0x91); add_comp(&w, 0x92, d, 1, &o); Pin p2[1] = { { "top", 0x92, 0 } }; const char *nm2[1] = { "top" }; - const char *lk2 = lock_text(&w, p2, 1); - run_compile(&w, src_imports(nm2, 1), OMEGA_DOMAIN_BUILD, lk2, &r); - check("accept-verified-on-genesis-with-permission", r.rc == 0 && r.h.closure.n == 2); - run_free(&r); - w.allow_genesis = 0; - run_compile(&w, src_imports(nm2, 1), OMEGA_DOMAIN_BUILD, lk2, &r); - check("refuse-verified-on-genesis-without-permission", refused(&r, "UNVERIFIED_DEPENDENCY", OMEGA_RES_UNVERIFIED_DEPENDENCY)); + run_compile(&w, src_imports(nm2, 1), OMEGA_DOMAIN_BUILD, lock_text(&w, p2, 1), &r); + check("refuse-verified-on-unlisted-genesis-record", refused(&r, "UNVERIFIED_DEPENDENCY", OMEGA_RES_UNVERIFIED_DEPENDENCY) && r.h.err.store_code == OMEGA_VCS_GENESIS_NOT_LISTED); run_free(&r); - /* a genesis record may NOT depend on a verified record */ - w.allow_genesis = 1; - mkid(d[0], 0x92); + /* a genesis record may NOT depend on a verified record (the verified leaf 0x94 resolves; the edge is what is refused) */ + add_comp(&w, 0x94, NULL, 0, &o); + mkid(d[0], 0x94); add_comp(&w, 0x93, d, 1, &b); Pin p3[1] = { { "bad", 0x93, 0 } }; const char *nm3[1] = { "bad" }; run_compile(&w, src_imports(nm3, 1), OMEGA_DOMAIN_BUILD, lock_text(&w, p3, 1), &r); @@ -1030,7 +1069,7 @@ static int take(W *w, uint8_t b0, uint8_t **bytes, size_t *len, uint8_t id[32]) } static OmegaResolver resolver_of(W *dst, RTab *rt) { OmegaResolver r; memset(&r, 0, sizeof r); - r.store = dst->st; r.domain = OMEGA_DOMAIN_BUILD; r.fetch_receipt = tab_fetch; r.fetch_ctx = rt; + r.store = dst->st; r.domain = OMEGA_DOMAIN_BUILD; r.fetch_receipt = tab_fetch; r.fetch_ctx = rt; r.fetch_blob = vcfx_fetch_blob; return r; } static void t_admit(void) { @@ -1062,6 +1101,27 @@ static void t_admit(void) { check("admit-accepts-record-from-build-origin", rc == 0 && omega_vcstore_count(dst2.st) == 1); free(b); w_free(&dst2); w_free(&dst); w_free(&src); } + { /* a dirty-checkout receipt is refused at admission in BUILD (#226); a clean receipt on the same fixture admits (control) */ + W src, dst; Opt o = opt0(); o.dirty = 1; + w_init(&src); w_init(&dst); + add_comp(&src, 0x54, NULL, 0, &o); + uint8_t *b; size_t n; uint8_t id[32]; + if (take(&src, 0x54, &b, &n, id)) setup_fail("take"); + OmegaResolver r = resolver_of(&dst, &src.rt); + int rc = omega_resolve_admit(&r, dst.st, b, n, id, NULL, &e); + check("admit-refuses-dirty-receipt-in-build", rc == OMEGA_RES_UNVERIFIED_DEPENDENCY && omega_vcstore_count(dst.st) == 0 && + strstr(e.message, "dirty checkout") != NULL); + Opt c = opt0(); + W src2, dst2; w_init(&src2); w_init(&dst2); + add_comp(&src2, 0x54, NULL, 0, &c); + uint8_t *b2; size_t n2; uint8_t id2[32]; + if (take(&src2, 0x54, &b2, &n2, id2)) setup_fail("take"); + OmegaResolver r2 = resolver_of(&dst2, &src2.rt); + rc = omega_resolve_admit(&r2, dst2.st, b2, n2, id2, NULL, &e); + check("admit-accepts-clean-receipt-same-fixture", rc == 0 && omega_vcstore_count(dst2.st) == 1); + free(b2); w_free(&dst2); w_free(&src2); + free(b); w_free(&dst); w_free(&src); + } { /* a record carrying the dev taint capability never enters */ W src, dst; Opt o = opt0(); o.taint = 1; w_init(&src); w_init(&dst); @@ -1094,37 +1154,30 @@ static void t_admit(void) { free(b); w_free(&dst); w_free(&src); } } - { /* genesis loading: no receipt, but it may depend only on genesis records */ + { /* genesis loading: VC-GENESIS-1 is empty, so no record is admissible; a verified dependency is refused for ITS reason first */ W src, dst; Opt b = opt0(); b.boot = 1; Opt v = opt0(); w_init(&src); w_init(&dst); add_comp(&src, 0x61, NULL, 0, &b); uint8_t *g; size_t gn; uint8_t gid[32]; if (take(&src, 0x61, &g, &gn, gid)) setup_fail("take"); int rc = omega_resolve_admit_genesis(dst.st, g, gn, gid, NULL, &e); - uint8_t kind = 0; uint8_t sid[32]; mkid(sid, 0x61); - check("admit-genesis-records-bootstrap-kind", rc == 0 && omega_vcstore_admission_kind(dst.st, sid, &kind) == 0 && kind == OMEGA_VCS_ADMISSION_BOOTSTRAP); - /* depends on a genesis record already loaded: fine */ - uint8_t d[1][32]; mkid(d[0], 0x61); - add_comp(&src, 0x62, d, 1, &b); - uint8_t *g2; size_t g2n; uint8_t g2id[32]; - if (take(&src, 0x62, &g2, &g2n, g2id)) setup_fail("take"); - rc = omega_resolve_admit_genesis(dst.st, g2, g2n, g2id, NULL, &e); - check("admit-genesis-may-depend-on-genesis", rc == 0 && omega_vcstore_count(dst.st) == 2); - /* depends on a verified record: refused */ + check("admit-genesis-refuses-unlisted-record", rc == OMEGA_RES_UNVERIFIED_DEPENDENCY && e.store_code == OMEGA_VCS_GENESIS_NOT_LISTED && strstr(e.message, "pinned genesis set") != NULL && + omega_vcstore_count(dst.st) == 0 && !omega_genesis_contains(vcfx(0x61)->id)); + /* depends on a verified record: refused for that reason (store code 0), before the list is consulted */ add_comp(&dst, 0x63, NULL, 0, &v); /* verified record in the target store */ add_comp(&src, 0x63, NULL, 0, &v); - mkid(d[0], 0x63); + uint8_t d[1][32]; mkid(d[0], 0x63); add_comp(&src, 0x64, d, 1, &b); uint8_t *g3; size_t g3n; uint8_t g3id[32]; if (take(&src, 0x64, &g3, &g3n, g3id)) setup_fail("take"); size_t before = omega_vcstore_count(dst.st); rc = omega_resolve_admit_genesis(dst.st, g3, g3n, g3id, NULL, &e); - check("admit-genesis-refuses-verified-dependency", rc == OMEGA_RES_UNVERIFIED_DEPENDENCY && omega_vcstore_count(dst.st) == before); + check("admit-genesis-refuses-verified-dependency", rc == OMEGA_RES_UNVERIFIED_DEPENDENCY && e.store_code == 0 && omega_vcstore_count(dst.st) == before); OmegaArtifactMeta m; uint8_t ir[32], cl[32]; memset(ir, 1, 32); memset(cl, 2, 32); omega_artifact_meta_make(&m, OMEGA_DOMAIN_DEV, ir, cl); rc = omega_resolve_admit_genesis(dst.st, g, gn, gid, &m, &e); check("admit-genesis-refuses-tainted-origin", rc == OMEGA_RES_TAINTED_ARTIFACT); - free(g); free(g2); free(g3); w_free(&dst); w_free(&src); + free(g); free(g3); w_free(&dst); w_free(&src); } } @@ -1257,7 +1310,8 @@ static int scan_file(const char *path, const char **hit) { } static void t_no_escape(void) { static const char *const files[] = { "src/omega_resolve.c", "src/omega_resolve.h", "src/omega_receipt.c", "src/omega_receipt.h", - "src/omega_blake3.c", "src/omega_blake3.h", "src/omega_resolve_osc.c", "src/omega_resolve_osc.h", "src/oscv_main.c" }; + "src/omega_blake3.c", "src/omega_blake3.h", "src/omega_resolve_osc.c", "src/omega_resolve_osc.h", "src/oscv_main.c", + "src/omega_genesis.h", "src/omega_vcstore_priv.h", "src/omega_program_ir.c", "src/omega_program_ir.h", "src/omega_vc_bridge.c", "src/omega_vc_bridge.h" }; const char *hit = NULL; int found = 0, nfiles = 0; for (size_t i = 0; i < sizeof files / sizeof files[0]; i++) { nfiles++; if (scan_file(files[i], &hit)) { found = 1; printf("note: %s contains \"%s\"\n", files[i], hit); } } @@ -1311,6 +1365,85 @@ static void t_no_escape(void) { check("no-escape-scanner-detects-every-forbidden-word", control && !scan_buf("clean text", 10, &h2)); } +/* ---- the doors: who may call what (VC1 stage 6) ---- */ +static const char *g_tok; +static const char *const *g_allow; +static size_t g_n_allow; +static int g_hits, g_unreadable; +static int has_tok(const uint8_t *b, size_t n, const char *tok) { + size_t k = strlen(tok); + for (size_t a = 0; a + k <= n; a++) if (memcmp(b + a, tok, k) == 0) return 1; + return 0; +} +static int scan_cb(const char *path, const struct stat *sb, int tf, struct FTW *ftw) { + (void)sb; (void)ftw; + if (tf != FTW_F) return 0; + size_t l = strlen(path); + if (l < 3 || (strcmp(path + l - 2, ".c") && strcmp(path + l - 2, ".h"))) return 0; + for (size_t i = 0; i < g_n_allow; i++) if (strcmp(path, g_allow[i]) == 0) return 0; + size_t n; uint8_t *b = slurp_file(path, &n); + if (!b) { g_unreadable++; return 0; } + if (has_tok(b, n, g_tok)) { g_hits++; printf("note: %s mentions %s\n", path, g_tok); } + free(b); + return 0; +} +/* how many .c/.h files under the roots, outside the allow-list, contain tok; -1 if one was unreadable or no file was seen */ +static int outside_hits(const char *tok, const char *const *allow, size_t n_allow, const char *const *roots, size_t n_roots) { + g_tok = tok; g_allow = allow; g_n_allow = n_allow; g_hits = 0; g_unreadable = 0; + for (size_t i = 0; i < n_roots; i++) if (nftw(roots[i], scan_cb, 16, FTW_PHYS) != 0) return -1; + return g_unreadable ? -1 : g_hits; +} +static int wfile(const char *path, const void *d, size_t n); +static void t_doors(void) { + static const char *const src_tools[2] = { "src", "tools" }; + static const char *const src_only[1] = { "src" }; + { /* the raw store inserts */ + static const char *const ok[] = { "src/omega_vcstore.c", "src/omega_vcstore.h", "src/omega_vcstore_priv.h", "src/omega_resolve.c", "src/omega_resolve.h" }; + check("raw-store-insert-is-named-only-by-the-store-and-the-resolver", outside_hits("omega_vcstore_insert", ok, 5, src_tools, 2) == 0); + static const char *const ok2[] = { "src/omega_vcstore.c", "src/omega_vcstore.h", "src/omega_vcstore_priv.h", "src/omega_resolve.c", "src/omega_resolve.h" }; + check("private-store-header-is-included-only-by-the-store-and-the-resolver", outside_hits("omega_vcstore_priv.h", ok2, 5, src_tools, 2) == 0); + } + { /* genesis admission and the list */ + /* the resolver defines it; four headers only DESCRIBE it in a comment (that they never call it) */ + static const char *const ok[] = { "src/omega_resolve.c", "src/omega_resolve.h", "src/omega_genesis.h", "src/omega_vcstore.h", "src/omega_vcstore_priv.h", "src/omega_vc_bridge.h" }; + check("genesis-admission-is-named-only-by-the-resolver", outside_hits("omega_resolve_admit_genesis", ok, 6, src_tools, 2) == 0); + static const char *const ok2[] = { "src/omega_genesis.h", "src/omega_vcstore.c", "src/omega_resolve.c" }; + check("genesis-list-is-consulted-only-by-the-store-and-the-resolver", outside_hits("omega_genesis_contains", ok2, 3, src_tools, 2) == 0); + static const char *const ok3[] = { "src/omega_genesis.h" }; + check("genesis-table-is-named-only-by-its-header", outside_hits("OMEGA_GENESIS_1", ok3, 1, src_tools, 2) == 0); + check("no-allow-genesis-field-or-switch-anywhere-in-src-or-tools", outside_hits("allow_genesis", NULL, 0, src_tools, 2) == 0 && + outside_hits("allow-genesis", NULL, 0, src_tools, 2) == 0); + } + { /* library inserts: the migrated callers (Crumbline, discovery) go through the bridge */ + static const char *const ok[] = { "src/omega_library.c", "src/omega_library.h", "src/omega_vc_bridge.c", "src/omega_vc_bridge.h" }; + check("library-insert-in-src-is-named-only-by-the-library-and-the-bridge", outside_hits("omega_library_insert", ok, 4, src_only, 1) == 0); + } + { /* the compiler's import hook: only the real driver installs one, and it installs the real resolver */ + static const char *const ok[] = { "src/compiler/osc_front.c", "src/compiler/osc_front.h", "src/compiler/osc_parse.h", "src/oscv_main.c" }; + check("import-hook-is-installed-only-by-the-real-driver", outside_hits("osc_compile_imports", ok, 4, src_tools, 2) == 0); + size_t n; uint8_t *b = slurp_file("src/oscv_main.c", &n); + if (!b) setup_fail("src/oscv_main.c"); + check("the-real-driver-installs-the-real-resolver-hook", has_tok(b, n, "osc_compile_imports(src, slen, u, &d, NULL, omega_osc_import_hook, &h)")); + free(b); + } + { /* the scanner itself: a violation outside the allow-list is seen, one inside is not */ + char dir[] = "/tmp/omega-doors-XXXXXX"; + if (!mkdtemp(dir)) setup_fail("mkdtemp"); + char p1[300], p2[300]; + snprintf(p1, sizeof p1, "%s/rogue.c", dir); snprintf(p2, sizeof p2, "%s/fine.c", dir); + wfile(p1, "int f(void) { return omega_vcstore_insert(0, 0, 0, 0); }\n", strlen("int f(void) { return omega_vcstore_insert(0, 0, 0, 0); }\n")); + wfile(p2, "int g(void) { return 1; }\n", strlen("int g(void) { return 1; }\n")); + const char *roots[1] = { dir }; + const char *allow[1] = { p1 }; + int seen = outside_hits("omega_vcstore_insert", NULL, 0, roots, 1); + int allowed = outside_hits("omega_vcstore_insert", allow, 1, roots, 1); + int missing = outside_hits("omega_vcstore_insert", NULL, 0, (const char *const[]){ "/tmp/omega-doors-no-such-root" }, 1); + check("door-scanner-sees-a-rogue-caller-and-honors-the-allow-list", seen == 1 && allowed == 0 && missing == -1); + char cmd[400]; snprintf(cmd, sizeof cmd, "rm -rf %s", dir); + if (system(cmd) != 0) setup_fail("cleanup"); + } +} + /* ---- the real driver, end to end ---- */ static int sh(const char *cmd, char *out, size_t cap) { FILE *p = popen(cmd, "r"); @@ -1360,6 +1493,14 @@ static void t_cli(void) { add_comp(&w, 0x51, NULL, 0, &o); Opt t = opt0(); t.taint = 1; add_comp(&w, 0x52, NULL, 0, &t); write_receipts(&w, rdir); + char bdir[700]; snprintf(bdir, sizeof bdir, "%s/blobs", dir); + if (mkdir(bdir, 0700)) setup_fail("mkdir blobs"); + { static const uint8_t bs[2] = { 0x51, 0x52 }; /* the build domain's source/IR store: blobs/ next to the source */ + for (int k = 0; k < 2; k++) { + char bh[65]; hx(vcfx(bs[k])->digest, bh); + snprintf(path, sizeof path, "%.200s/%s.blob", bdir, bh); + if (wfile(path, vcfx(bs[k])->ir, vcfx(bs[k])->ir_len)) setup_fail("write blob"); + } } snprintf(path, sizeof path, "%s/omega.vcstore", dir); if (omega_vcstore_save(w.st, path)) setup_fail("save store"); Pin pin[1] = { { "foo", 0x51, 0 } }; @@ -1397,6 +1538,19 @@ static void t_cli(void) { sh(cmd, out, sizeof out); kv(out, "tainted", v, sizeof v); check("cli-build-domain-is-not-tainted", strcmp(v, "0") == 0); + /* the build domain REQUIRES the source/IR store; the dev domain does not */ + snprintf(path, sizeof path, "%s/blobs-off", dir); + if (rename(bdir, path)) setup_fail("rename blobs away"); + snprintf(cmd, sizeof cmd, "%s %s/a.osc 2>&1", OSCV_PATH, dir); + rc = sh(cmd, out, sizeof out); + check("cli-refuses-build-domain-without-the-blob-store", rc == 1 && strstr(out, "resolve_code=UNVERIFIED_DEPENDENCY") != NULL && strstr(out, "build_id") == NULL); + snprintf(cmd, sizeof cmd, "%s --domain dev %s/a.osc 2>&1", OSCV_PATH, dir); + rc = sh(cmd, out, sizeof out); + check("cli-dev-domain-runs-without-the-blob-store", rc == 0 && kv(out, "tainted", v, sizeof v) == 0 && strcmp(v, "1") == 0); + snprintf(cmd, sizeof cmd, "%s --blobs %s/blobs-off %s/a.osc 2>&1", OSCV_PATH, dir, dir); + rc = sh(cmd, out, sizeof out); + check("cli-blobs-option-points-the-build-domain-at-a-store", rc == 0 && kv(out, "tainted", v, sizeof v) == 0 && strcmp(v, "0") == 0); + if (rename(path, bdir)) setup_fail("rename blobs back"); /* meta header file */ snprintf(cmd, sizeof cmd, "%s --domain dev --meta-out %s/meta.txt %s/a.osc 2>&1", OSCV_PATH, dir, dir); rc = sh(cmd, out, sizeof out); @@ -1480,9 +1634,9 @@ static void t_cli(void) { for (int e2 = 0; e2 < 2; e2++) { snprintf(cmd, sizeof cmd, "%s%s %s --store %s/g.vcstore --lock %s/g.lock --receipts %s %s/g.osc 2>&1", envs[e2], OSCV_PATH, flags[f], dir, dir, rdir, dir); rc = sh(cmd, out, sizeof out); - if (!(rc == 1 && strstr(out, "resolve_code=UNVERIFIED_DEPENDENCY") != NULL && strstr(out, "genesis") != NULL && strstr(out, "build_id") == NULL)) all = 0; + if (!(rc == 1 && strstr(out, "resolve_code=UNVERIFIED_DEPENDENCY") != NULL && strstr(out, "GENESIS_NOT_LISTED") != NULL && strstr(out, "build_id") == NULL)) all = 0; } - check("cli-genesis-only-store-is-refused-in-every-mode", all); + check("cli-store-holding-an-unlisted-genesis-record-is-refused-in-every-mode", all); w_free(&g); } snprintf(cmd, sizeof cmd, "%s --domain prod %s/a.osc 2>&1", OSCV_PATH, dir); @@ -1511,6 +1665,7 @@ static const struct { const char *mutant, *check; } MUTANTS[] = { { "missing-receipt", "refuse-missing-receipt" }, { "rule1-name", "refuse-receipt-file-holds-another-receipt" }, { "rule-clean", "refuse-dirty-receipt-in-build" }, + { "rule-clean-admit", "admit-refuses-dirty-receipt-in-build" }, { "rule4-semantic", "refuse-receipt-does-not-name-program" }, { "rule4-source", "refuse-stale-receipt" }, { "rule3", "refuse-receipt-output-digest-is-not-evidence-root" }, @@ -1542,6 +1697,10 @@ static const struct { const char *mutant, *check; } MUTANTS[] = { { "admit-receipt", "admit-refuses-mismatched-receipt" }, { "genesis-dep", "admit-genesis-refuses-verified-dependency" }, { "blob-digest", "refuse-blob-does-not-hash-to-digest" }, + { "admit-genesis-list", "admit-genesis-refuses-unlisted-record" }, + { "build-needs-blobs", "refuse-build-domain-without-blob-store" }, + { "program-id", "refuse-ir-that-does-not-recompute-to-semantic-id" }, + { "ir-kind", "refuse-source-digest-record-in-build-domain" }, { "json-dup-key", "receipt-refuses-duplicate-key" }, { "deny-unknown", "receipt-refuses-unknown-field" }, { "schema", "receipt-refuses-unknown-schema" }, @@ -1581,6 +1740,7 @@ int main(int argc, char **argv) { t_guards(); t_identity(); t_no_escape(); + t_doors(); t_cli(); if (g_mutant) { if (g_expect_failed) { printf("MUTANT %s KILLED by %s\n", g_mutant, g_expect_check); return 1; } diff --git a/tests/test_omega_vc_bridge.c b/tests/test_omega_vc_bridge.c new file mode 100644 index 000000000..4d7e8da83 --- /dev/null +++ b/tests/test_omega_vc_bridge.c @@ -0,0 +1,123 @@ +/* test_omega_vc_bridge.c: the bridge (src/omega_vc_bridge.c) is not a self-minting escape hatch + * (VC1 stage 6 fix, from the inspector's hand test on omega PR #224). + * 1. The bridge runs omega_program_verify itself and recomputes the program id itself. A program + * whose real verification fails is refused even when the caller flips is_verified by hand; a + * program whose id was forged is refused. + * 2. A record the bridge mints lists OMEGA_BRIDGE_SELFMINTED_CAPABILITY, so a build-domain import + * of it is refused. The dev domain still accepts it. + * Each guard carries a VC1B tag; the Makefile target test-vc-bridge breaks each tag in a copy and + * the named check must FAIL. CPU only. Exit 0 only when every check passed. */ +#include +#include +#include + +#include "omega_library.h" +#include "omega_program.h" +#include "omega_resolve.h" +#include "omega_vc_bridge.h" +#include "omega_vcstore.h" + +static int g_total, g_failed; +static void check(const char *name, int ok) { + printf("%s %s\n", ok ? "PASS" : "FAIL", name); + g_total++; + if (!ok) g_failed++; +} +static void hx(const uint8_t *d, char out[65]) { + static const char h[] = "0123456789abcdef"; + for (int i = 0; i < 32; i++) { out[2 * i] = h[d[i] >> 4]; out[2 * i + 1] = h[d[i] & 15]; } + out[64] = 0; +} +static OmegaProgram *mkprog(uint64_t imm) { + OmegaProgram *p = calloc(1, sizeof *p); + if (!p || omega_program_build_unary_op(p, "bridge-test", OP_ADD, imm) != 0) { fprintf(stderr, "setup failed\n"); exit(2); } + if (!p->is_realized && omega_program_realize(p) != 0) { fprintf(stderr, "realize failed\n"); exit(2); } + return p; +} +typedef struct { OmegaVcBridge br; OmegaLibrary lib; } Rig; +static Rig *rig_new(void) { + Rig *r = calloc(1, sizeof *r); + if (!r || omega_vc_bridge_init(&r->br) != 0) { fprintf(stderr, "setup failed\n"); exit(2); } + omega_library_init(&r->lib); + return r; +} +static void rig_free(Rig *r) { omega_library_destroy(&r->lib); omega_vc_bridge_destroy(&r->br); free(r); } + +/* resolve one import of the program in the bridge's own store, in domain d */ +static int import_one(Rig *r, const OmegaProgram *p, OmegaDomain d, OmegaClosure *cl, OmegaResolveError *e) { + uint8_t rcpt[32]; + if (omega_vcstore_receipt_of(&r->br.store, p->program_id.bytes, rcpt) != 0) return -99; + char sh[65], rh[65], text[400]; + hx(p->program_id.bytes, sh); hx(rcpt, rh); + snprintf(text, sizeof text, "omega.lock v1\nfoo = semantic %s receipt %s\n", sh, rh); + OmegaLock lock; + if (omega_lock_parse(text, strlen(text), &lock, e) != 0) return -98; + OmegaResolver rv; + memset(&rv, 0, sizeof rv); + rv.store = &r->br.store; rv.domain = d; + rv.fetch_receipt = omega_vc_bridge_fetch_receipt; rv.fetch_ctx = &r->br; + rv.fetch_blob = omega_vc_bridge_fetch_blob; rv.blob_ctx = &r->br; + const char *names[1] = { "foo" }; + memset(cl, 0, sizeof *cl); + int rc = omega_resolve_imports(&rv, &lock, names, 1, cl, e); + omega_lock_free(&lock); + return rc; +} + +int main(void) { + static const uint8_t ev[32] = { 7 }; + + /* the genuine program is admitted */ + { + Rig *r = rig_new(); + OmegaProgram *p = mkprog(5); + check("bridge-admits-a-genuine-program", omega_vc_bridge_admit(&r->br, &r->lib, p, NULL, 0, ev) == 0 && + r->lib.count == 1 && omega_vcstore_count(&r->br.store) == 1); + OmegaClosure cl; OmegaResolveError e; + memset(&e, 0, sizeof e); + int rc = import_one(r, p, OMEGA_DOMAIN_BUILD, &cl, &e); + check("refuse-bridge-record-in-build-domain", rc == OMEGA_RES_UNVERIFIED_DEPENDENCY && cl.n == 0 && strstr(e.message, "bridge") != NULL); + omega_closure_free(&cl); + memset(&e, 0, sizeof e); + rc = import_one(r, p, OMEGA_DOMAIN_DEV, &cl, &e); + check("accept-bridge-record-in-dev-domain", rc == 0 && cl.n == 1); + omega_closure_free(&cl); + omega_program_destroy(p); free(p); rig_free(r); + } + /* the caller never needs to set is_verified: the bridge verifies for itself */ + { + Rig *r = rig_new(); + OmegaProgram *p = mkprog(6); + p->is_verified = false; + check("bridge-does-not-need-the-callers-verified-flag", omega_vc_bridge_admit(&r->br, &r->lib, p, NULL, 0, ev) == 0); + omega_program_destroy(p); free(p); rig_free(r); + } + /* the inspector's hand test: corrupt the machine code, flip the flags, ask the bridge */ + { + Rig *r = rig_new(); + OmegaProgram *p = mkprog(5); + memset(p->realization.code_bytes, 0xFF, p->realization.code_len); + OmegaProgram *probe = calloc(1, sizeof *probe); + *probe = *p; + VerifyReport rep; + int premise = omega_program_verify(probe, &rep) != 0; /* real verification fails on this program */ + free(probe); + p->is_realized = true; p->is_verified = true; /* the caller's lie */ + int rc = omega_vc_bridge_admit(&r->br, &r->lib, p, NULL, 0, ev); + check("premise-corrupted-program-fails-real-verification", premise); + check("refuse-corrupted-program-even-with-flags-set", rc == -1 && r->lib.count == 0 && omega_vcstore_count(&r->br.store) == 0); + omega_program_destroy(p); free(p); rig_free(r); + } + /* a forged program id with the flags set */ + { + Rig *r = rig_new(); + OmegaProgram *p = mkprog(5); + p->is_realized = true; p->is_verified = true; + p->program_id.bytes[0] ^= 1; + int rc = omega_vc_bridge_admit(&r->br, &r->lib, p, NULL, 0, ev); + check("refuse-forged-program-id-at-admit", rc == -1 && r->lib.count == 0 && omega_vcstore_count(&r->br.store) == 0); + omega_program_destroy(p); free(p); rig_free(r); + } + printf("%d checks, %d failed\n", g_total, g_failed); + return g_failed ? 1 : 0; +} diff --git a/tests/test_omega_vcstore.c b/tests/test_omega_vcstore.c index a0f437dd6..2954b8d41 100644 --- a/tests/test_omega_vcstore.c +++ b/tests/test_omega_vcstore.c @@ -18,7 +18,9 @@ #include #include +#include "omega_genesis.h" #include "omega_vcstore.h" +#include "omega_vcstore_priv.h" #include "sha256.h" #define GOLDEN "tests/vcstore/golden" @@ -59,7 +61,9 @@ static const struct { const char *mutant, *check; } MUTANTS[] = { { "dep-contract", "dep-contract-mismatch-refused" }, { "no-cap", "store-grows-past-old-caps" }, { "sorted-insert", "digest-independent-of-insert-order" }, - { "kind-bootstrap", "bootstrap-kind-recorded" }, + { "kind-bootstrap", "insert-bootstrap-unlisted-refused" }, + { "insert-genesis", "insert-bootstrap-unlisted-refused" }, + { "load-genesis", "load-bootstrap-unlisted-refused" }, /* get, receipt */ { "get-recompute", "get-detects-corrupt-bytes" }, { "get-key", "get-detects-key-mismatch" }, @@ -172,12 +176,22 @@ static void encode(B *o, const Spec *s) { bu32(o, s->n_exp); for (uint32_t i = 0; i < s->n_exp; i++) bstr(o, s->exp[i]); bu32(o, s->n_cap); for (uint32_t i = 0; i < s->n_cap; i++) bstr(o, s->cap[i]); } +/* VC-GENESIS-1 is EMPTY in the real source, so no BOOTSTRAP record can be inserted here. A test + * that needs a BOOTSTRAP-kind object (digest and persistence checks) inserts it as VERIFIED and + * then flips the kind byte in the store, which the header declares public for exactly this + * (tamper) purpose. Real BOOTSTRAP admission, with a non-empty list, is test_omega_genesis_set. */ +static void flip_to_bootstrap(OmegaVcStore *st, const uint8_t sid[32]) { + for (size_t i = 0; i < st->count; i++) + if (memcmp(st->objs[i].semantic_id, sid, 32) == 0) { st->objs[i].admission_kind = OMEGA_VCS_ADMISSION_BOOTSTRAP; return; } + setup_fail("flip_to_bootstrap: no such object"); +} static int ins_kind(OmegaVcStore *st, const Spec *sp, int bootstrap) { B b = { 0, 0, 0 }; encode(&b, sp); uint8_t id[32]; omega_vc_compute_id(b.p, b.n, id); - int rc = bootstrap ? omega_vcstore_insert_bootstrap(st, b.p, b.n, id) : omega_vcstore_insert(st, b.p, b.n, id); + int rc = omega_vcstore_insert(st, b.p, b.n, id); + if (rc == 0 && bootstrap) flip_to_bootstrap(st, sp->sem); free(b.p); return rc; } @@ -361,11 +375,30 @@ static void insert_checks(void) { rc == OMEGA_VCS_VCSTORE_IMMUTABLE_CONFLICT && g == 0 && memcmp(rec->vc_id, id, 32) == 0 && memcmp(d1, d2, 32) == 0 && omega_vcstore_count(s) == 1); free(rec); - /* same bytes, other admission kind: also a conflict, never a silent relabel */ + /* same bytes, other admission kind: also a conflict, never a silent relabel (the stored + * record is flipped to BOOTSTRAP, then the identical bytes are offered as VERIFIED) */ + flip_to_bootstrap(s, sem); uint8_t kind = 0; - rc = omega_vcstore_insert_bootstrap(s, b.p, b.n, id); + rc = omega_vcstore_insert(s, b.p, b.n, id); int g2 = omega_vcstore_admission_kind(s, sem, &kind); - check("kind-switch-refused", rc == OMEGA_VCS_VCSTORE_IMMUTABLE_CONFLICT && g2 == 0 && kind == OMEGA_VCS_ADMISSION_VERIFIED); + check("kind-switch-refused", rc == OMEGA_VCS_VCSTORE_IMMUTABLE_CONFLICT && g2 == 0 && kind == OMEGA_VCS_ADMISSION_BOOTSTRAP); + free_store(s); + + /* VC-GENESIS-1 gate: a BOOTSTRAP record whose id is not on the list is refused with its own + * code and leaves the store untouched; the list is the only thing that decides */ + s = new_store(); + uint8_t g0[32]; dig(s, g0); + uint8_t ug[32]; mkid(ug, 0x12); + Spec spg; spec_default(&spg, ug); + B bg = { 0, 0, 0 }; + encode(&bg, &spg); + uint8_t gid[32]; omega_vc_compute_id(bg.p, bg.n, gid); + int rg = omega_vcstore_insert_bootstrap(s, bg.p, bg.n, gid), rv = omega_vcstore_insert(s, bg.p, bg.n, gid); + uint8_t g1[32]; dig(s, g1); + check("insert-bootstrap-unlisted-refused", + !omega_genesis_contains(ug) && rg == OMEGA_VCS_GENESIS_NOT_LISTED && strcmp(omega_vcstore_code_name(rg), "GENESIS_NOT_LISTED") == 0 && + rv == 0 && omega_vcstore_count(s) == 1 && memcmp(g0, g1, 32) != 0); + free(bg.p); free_store(s); /* receipt_of: the record's receipt_id, nothing else; absent id is refused */ @@ -629,7 +662,7 @@ static void digest_checks(void) { /* reference digest equals the library digest (verified and bootstrap mixed) */ OmegaVcStore *s = new_store(); uint8_t dD[1][32], dR[2][32]; memcpy(dD[0], D, 32); memcpy(dR[0], Bn, 32); memcpy(dR[1], C, 32); - int rc = node_kind(s, D, NULL, 0, 1); + int rc = node_kind(s, D, NULL, 0, 1); /* D is flipped to BOOTSTRAP (see flip_to_bootstrap) */ rc |= node(s, Bn, dD, 1); rc |= node(s, C, dD, 1); rc |= node(s, R, dR, 2); if (rc) setup_fail("digest store"); FObj objs[4]; Spec sp; B enc[4] = { {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0} }; @@ -665,7 +698,7 @@ static void digest_checks(void) { int rv = node_kind(v, D, NULL, 0, 0), rb = node_kind(bo, D, NULL, 0, 1); uint8_t kv = 0, kb = 0; omega_vcstore_admission_kind(v, D, &kv); omega_vcstore_admission_kind(bo, D, &kb); - check("bootstrap-kind-recorded", rv == 0 && rb == 0 && kv == OMEGA_VCS_ADMISSION_VERIFIED && kb == OMEGA_VCS_ADMISSION_BOOTSTRAP); + check("bootstrap-kind-reported", rv == 0 && rb == 0 && kv == OMEGA_VCS_ADMISSION_VERIFIED && kb == OMEGA_VCS_ADMISSION_BOOTSTRAP); check("bootstrap-vs-verified-digest-differ", rv == 0 && rb == 0 && !same_digest(v, bo)); free_store(v); free_store(bo); @@ -749,7 +782,7 @@ static void persistence_checks(void) { OmegaVcStore *s = new_store(); uint8_t dD[1][32]; memcpy(dD[0], D, 32); uint8_t dR[2][32]; memcpy(dR[0], Bn, 32); memcpy(dR[1], C, 32); - int rc = node_kind(s, D, NULL, 0, 1); + int rc = node(s, D, NULL, 0); rc |= node(s, Bn, dD, 1); rc |= node(s, C, dD, 1); rc |= node(s, R, dR, 2); rc |= omega_vcstore_name_bind(s, "root", R); rc |= omega_vcstore_name_bind(s, "leaf", D); if (rc) setup_fail("persist store"); @@ -770,7 +803,7 @@ static void persistence_checks(void) { int a5 = omega_vcstore_closure(t, R, cl, 4, &ncl); check("save-load-round-trip", sv == 0 && ld == 0 && omega_vcstore_count(t) == 4 && memcmp(o1, o2, 32) == 0 && memcmp(n1, n2, 32) == 0 && a1 == 0 && a2 == 0 && memcmp(rc1, rc2, 32) == 0 && a3 == 0 && memcmp(res, R, 32) == 0 && - a4 == 0 && kind == OMEGA_VCS_ADMISSION_BOOTSTRAP && a5 == 0 && ncl == 4 && memcmp(cl, D, 32) == 0); + a4 == 0 && kind == OMEGA_VCS_ADMISSION_VERIFIED && a5 == 0 && ncl == 4 && memcmp(cl, D, 32) == 0); free_store(t); size_t fn; uint8_t *file = slurp(g_path, &fn); @@ -873,10 +906,16 @@ static void persistence_checks(void) { t = new_store(); int r = omega_vcstore_load(t, g_path2); check("load-name-unknown-id-refused", r == OMEGA_VCS_UNVERIFIED_DEPENDENCY); free_store(t); } /* and a valid hand-built file does load (the reference writer and the loader agree) */ - { FObj o[2] = { { ba.p, ba.n, 1 }, { bb.p, bb.n, 2 } }; + { FObj o[2] = { { ba.p, ba.n, 1 }, { bb.p, bb.n, 1 } }; write_file(g_path2, o, 2, NULL, 0); t = new_store(); int r = omega_vcstore_load(t, g_path2); check("load-reference-written-file", r == 0 && omega_vcstore_count(t) == 2); free_store(t); } + /* a file that labels an unlisted record BOOTSTRAP is refused whole, and the target store keeps what it had */ + { FObj o[2] = { { ba.p, ba.n, 1 }, { bb.p, bb.n, 2 } }; + write_file(g_path2, o, 2, NULL, 0); + t = new_store(); node(t, other, NULL, 0); uint8_t t1[32], t2[32]; dig(t, t1); + int r = omega_vcstore_load(t, g_path2); dig(t, t2); + check("load-bootstrap-unlisted-refused", !omega_genesis_contains(other2) && r == OMEGA_VCS_GENESIS_NOT_LISTED && memcmp(t1, t2, 32) == 0 && omega_vcstore_count(t) == 1); free_store(t); } free(bx.p); free(by.p); free(byc.p); free(ba.p); free(bb.p); remove(g_path); remove(g_path2); diff --git a/tests/vc_fixture.h b/tests/vc_fixture.h new file mode 100644 index 000000000..dd10cb2a7 --- /dev/null +++ b/tests/vc_fixture.h @@ -0,0 +1,81 @@ +/* vc_fixture.h -- real Omega programs for Verified Crumb tests (VC1 stage 6). + * + * Since stage 6 the build-domain resolver recomputes the program id from the stored IR + * (SPEC 6 step 3), so a test record can no longer name a made-up 32-byte id. Fixture n is the real + * program "x + (n + 1)" over u64, built the way omega_program_build_unary_op builds every unary + * program. Its semantic id is the real omega_program_compute_id, its IR blob is the real + * omega_program_ir_encode, its source_or_ir_digest is the SHA-256 of that blob. + * + * Two extra blobs are served by vcfx_fetch_blob: a canonical-source stand-in (for records whose + * digest kind is OSC source) and bytes that are not an IR at all. Include in ONE translation unit. */ +#ifndef VC_FIXTURE_H +#define VC_FIXTURE_H + +#include +#include +#include +#include + +#include "omega_program.h" +#include "omega_program_ir.h" +#include "sha256.h" + +#define VCFX_UNUSED __attribute__((unused)) + +typedef struct { uint8_t id[32], digest[32]; uint8_t *ir; size_t ir_len; int built; } VcFx; +static VcFx g_vcfx[256]; + +static const VcFx *vcfx(uint8_t n) VCFX_UNUSED; +static const VcFx *vcfx(uint8_t n) { + VcFx *f = &g_vcfx[n]; + if (f->built) return f; + OmegaProgram *p = calloc(1, sizeof *p); + if (!p || omega_program_build_unary_op(p, "vcfx", OP_ADD, (uint64_t)n + 1u) != 0 || + omega_program_ir_encode(p, &f->ir, &f->ir_len) != 0) { + fprintf(stderr, "vc_fixture: cannot build program %u\n", (unsigned)n); + exit(2); + } + memcpy(f->id, p->program_id.bytes, 32); + sha256_hash(f->ir, f->ir_len, f->digest); + f->built = 1; + omega_program_destroy(p); + free(p); + return f; +} + +/* canonical-source stand-in and non-IR bytes */ +static const char VCFX_SOURCE[] = "fn add(a: u32, b: u32) -> u32 { return a + b; }\n"; +static const char VCFX_GARBAGE[] = "these bytes are not an Omega program IR"; +static void vcfx_source_digest(uint8_t out[32]) VCFX_UNUSED; +static void vcfx_source_digest(uint8_t out[32]) { sha256_hash((const uint8_t *)VCFX_SOURCE, sizeof VCFX_SOURCE - 1, out); } +static void vcfx_garbage_digest(uint8_t out[32]) VCFX_UNUSED; +static void vcfx_garbage_digest(uint8_t out[32]) { sha256_hash((const uint8_t *)VCFX_GARBAGE, sizeof VCFX_GARBAGE - 1, out); } + +static int vcfx_copy(const void *p, size_t n, uint8_t **bytes, size_t *len) { + *bytes = malloc(n + 1); + if (!*bytes) return 2; + memcpy(*bytes, p, n); + *len = n; + return 0; +} +/* OmegaBlobFetch over every fixture built so far plus the two extra blobs */ +static int vcfx_fetch_blob(void *ctx, const uint8_t digest[32], uint8_t **bytes, size_t *len) VCFX_UNUSED; +static int vcfx_fetch_blob(void *ctx, const uint8_t digest[32], uint8_t **bytes, size_t *len) { + (void)ctx; + uint8_t d[32]; + for (int n = 0; n < 256; n++) + if (g_vcfx[n].built && memcmp(g_vcfx[n].digest, digest, 32) == 0) return vcfx_copy(g_vcfx[n].ir, g_vcfx[n].ir_len, bytes, len); + vcfx_source_digest(d); + if (memcmp(d, digest, 32) == 0) return vcfx_copy(VCFX_SOURCE, sizeof VCFX_SOURCE - 1, bytes, len); + vcfx_garbage_digest(d); + if (memcmp(d, digest, 32) == 0) return vcfx_copy(VCFX_GARBAGE, sizeof VCFX_GARBAGE - 1, bytes, len); + return 1; +} +/* a blob store that answers every digest with the same wrong bytes */ +static int vcfx_fetch_wrong(void *ctx, const uint8_t digest[32], uint8_t **bytes, size_t *len) VCFX_UNUSED; +static int vcfx_fetch_wrong(void *ctx, const uint8_t digest[32], uint8_t **bytes, size_t *len) { + (void)ctx; (void)digest; + return vcfx_copy("not the source", 14, bytes, len); +} + +#endif /* VC_FIXTURE_H */ diff --git a/tools/omegatool.c b/tools/omegatool.c index f4b7bb28f..e075ed3f0 100644 --- a/tools/omegatool.c +++ b/tools/omegatool.c @@ -15,6 +15,7 @@ #include "omega_synthesis.h" #include "omega_library.h" #include "omega_discovery.h" +#include "omega_vc_bridge.h" #include "omega_machine.h" #include "omega_realize_synth.h" #include "omega_matvec.h" @@ -2082,13 +2083,16 @@ static bool test_m11_library_admission(void) { if (res.best_candidate_index >= 0) { OmegaLibrary lib; omega_library_init(&lib); + OmegaVcBridge br; + omega_vc_bridge_init(&br); - uint8_t dummy_receipt[32] = { 0xBB }; - int rc = omega_discovery_admit_to_library(&lib, &res.candidates[res.best_candidate_index], dummy_receipt); + uint8_t evidence[32] = { 0xBB }; + int rc = omega_vc_bridge_admit_abstraction(&br, &lib, &res.candidates[res.best_candidate_index], evidence); const OmegaLibraryEntry *e = omega_library_find_by_name(&lib, "discovered_abs_2x_plus_1"); ok = (rc == 0 && lib.count == 1 && e != NULL); omega_library_destroy(&lib); + omega_vc_bridge_destroy(&br); } omega_corpus_destroy(&corpus); @@ -2198,10 +2202,13 @@ static void run_demonstration_discovery(void) { printf("\n [5] Library Admission:\n"); OmegaLibrary lib; omega_library_init(&lib); - uint8_t dummy_receipt[32] = { 0xDE, 0xAD }; - omega_discovery_admit_to_library(&lib, cand, dummy_receipt); + OmegaVcBridge br; + omega_vc_bridge_init(&br); + uint8_t evidence[32] = { 0xDE, 0xAD }; + omega_vc_bridge_admit_abstraction(&br, &lib, cand, evidence); printf(" Library Catalog Updated: Count = %zu, Version = %u\n", lib.count, lib.version); omega_library_destroy(&lib); + omega_vc_bridge_destroy(&br); /* Search Acceleration */ printf("\n [6] Search Acceleration on Held-Out Task (g(x) = 2x + 6):\n"); ```