From 07cdbea157a798d1a07086c55b01398a5ff09c08 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Fri, 2 Oct 2026 07:13:34 -0500 Subject: [PATCH] VC1-LIB: harden omega_library admission (receipt required, bootstrap kind, deps must exist) Verified Crumb stage 2. omega_library_insert now refuses NULL or all-zero receipt hashes, unknown dependencies, and dep_count > OMEGA_LIB_MAX_DEPS (raised to 32; no more silent truncation). New omega_library_insert_bootstrap for the manually audited genesis set only; entries record admission_kind (VERIFIED/BOOTSTRAP) and it is part of the state digest. The digest is now streamed (the old 64 KB buffer would silently drop entries with 32 deps). Gate callers that passed NULL receipts (omegatool M10, test_program_id) now pass a non-zero test receipt. New tests/test_omega_library.c with 13 checks and 13 sed-built mutants of omega_library.c; `make test-library` requires every mutant to be killed. Co-Authored-By: Claude Fable 5.1 --- .github/workflows/host-suites-2.yml | 2 +- Makefile | 38 +++++++ src/omega_discovery.c | 2 +- src/omega_library.c | 152 +++++++++++++------------- src/omega_library.h | 32 +++++- tests/program/test_program_id.c | 13 ++- tests/test_omega_library.c | 163 ++++++++++++++++++++++++++++ tools/omegatool.c | 37 ++++--- 8 files changed, 336 insertions(+), 103 deletions(-) create mode 100644 tests/test_omega_library.c diff --git a/.github/workflows/host-suites-2.yml b/.github/workflows/host-suites-2.yml index 53aba4d20..30957cadd 100644 --- a/.github/workflows/host-suites-2.yml +++ b/.github/workflows/host-suites-2.yml @@ -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 + run: make PHYSICS_DIR=/nonexistent PHYSICS_LOCK_CHECK=0 test-crumbline test-library runtime: needs: changes diff --git a/Makefile b/Makefile index 2d504bb12..47c9f9bfa 100644 --- a/Makefile +++ b/Makefile @@ -1670,3 +1670,41 @@ build/test_omega_unwritten_trap: tests/test_omega_unwritten_trap.c src/omega_unw gcc -std=gnu11 -O2 -Wall -Wextra -Werror -Isrc -o $@ tests/test_omega_unwritten_trap.c src/omega_unwritten_trap.c test-unwritten-trap-host: build/test_omega_unwritten_trap ./build/test_omega_unwritten_trap + +# VC1-LIB: unit test for the library admission gate (receipt required, bootstrap kind, +# dependencies must exist, no truncation) plus mutation proof. Each mutant is a copy of +# src/omega_library.c with one tagged guard line (VC1:) deleted or weakened; the test +# must exit 1 (KILLED) for every one. A mutant whose sed did not change the file fails the build. +# Physics-free; CPU only. +.PHONY: test-library +LIBTEST_CORE = src/sha256.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_realize_synth.c \ + src/omega_machine.c src/omega_exec.c src/omega_verify.c src/omega_program.c src/omega_synthesis.c +LIBTEST_FLAGS = -std=gnu11 -Wall -Wextra -Werror -D_GNU_SOURCE -O2 -Isrc +LIBTEST_DIR = $(OUT_DIR)/library-test +test-library: tests/test_omega_library.c src/omega_library.c src/omega_library.h + @mkdir -p $(LIBTEST_DIR) + $(CC) $(LIBTEST_FLAGS) -o $(LIBTEST_DIR)/test_omega_library tests/test_omega_library.c src/omega_library.c $(LIBTEST_CORE) + $(LIBTEST_DIR)/test_omega_library + @set -eu; build_mutant() { name=$$1; tag=$$2; repl=$$3; \ + sed "/VC1:$$tag/c\\$$repl" src/omega_library.c > $(LIBTEST_DIR)/mut_$$name.c; \ + if cmp -s src/omega_library.c $(LIBTEST_DIR)/mut_$$name.c; then echo "mutant $$name: sed changed nothing"; exit 1; fi; \ + $(CC) $(LIBTEST_FLAGS) -o $(LIBTEST_DIR)/mut_$$name tests/test_omega_library.c $(LIBTEST_DIR)/mut_$$name.c $(LIBTEST_CORE); \ + rc=0; $(LIBTEST_DIR)/mut_$$name $$name > $(LIBTEST_DIR)/mut_$$name.out 2>&1 || rc=$$?; \ + if [ $$rc -ne 1 ] || ! grep -q "^MUTANT $$name KILLED" $(LIBTEST_DIR)/mut_$$name.out; then \ + echo "mutant $$name NOT killed (exit $$rc)"; cat $(LIBTEST_DIR)/mut_$$name.out; exit 1; fi; \ + echo "mutant $$name: killed (exit 1)"; }; \ + build_mutant allow-null-receipt null-receipt ' if (!receipt_hash) receipt_hash = (const uint8_t *)"0123456789abcdef0123456789abcdef";'; \ + build_mutant allow-zero-receipt zero-receipt ' ;'; \ + build_mutant bootstrap-null-audit bootstrap-null-audit ' if (!audit_hash) audit_hash = (const uint8_t *)"0123456789abcdef0123456789abcdef";'; \ + build_mutant bootstrap-zero-audit bootstrap-zero-audit ' ;'; \ + build_mutant allow-unverified verified-gate ' ;'; \ + build_mutant allow-duplicate duplicate ' ;'; \ + build_mutant allow-unknown-dep dep-unknown ' ;'; \ + build_mutant truncate-deps dep-overflow ' if (dep_count > OMEGA_LIB_MAX_DEPS) dep_count = OMEGA_LIB_MAX_DEPS;'; \ + build_mutant max-deps-off-by-one dep-overflow ' if (dep_count >= OMEGA_LIB_MAX_DEPS) return -1;'; \ + build_mutant digest-ignores-kind digest-kind ' uint8_t kind = 0;'; \ + build_mutant bootstrap-as-verified kind-bootstrap ' return lib_admit(lib, prog, deps, dep_count, audit_hash, OMEGA_LIB_ADMISSION_VERIFIED);'; \ + build_mutant refuse-valid capacity ' if (prog->name[3] == (char)55) return -1;'; \ + build_mutant refuse-deps dep-null ' if (dep_count > 0) return -1;' + @echo "test-library: PASS (all checks, all mutants killed)" diff --git a/src/omega_discovery.c b/src/omega_discovery.c index 8c2a9a322..b9da5563f 100644 --- a/src/omega_discovery.c +++ b/src/omega_discovery.c @@ -293,7 +293,7 @@ bool omega_verify_semantic_preservation(const OmegaProgram *orig, const OmegaPro int omega_discovery_admit_to_library(OmegaLibrary *lib, const OmegaAbstractionCandidate *cand, const uint8_t receipt_hash[32]) { - if (!lib || !cand) return -1; + 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); diff --git a/src/omega_library.c b/src/omega_library.c index 487c6000e..fa58fec25 100644 --- a/src/omega_library.c +++ b/src/omega_library.c @@ -20,69 +20,51 @@ void omega_library_destroy(OmegaLibrary *lib) { memset(lib, 0, sizeof(OmegaLibrary)); } +static void lib_digest_u32(sha256_ctx *ctx, uint32_t v) { + uint8_t b[4]; + b[0] = (uint8_t)((v >> 24) & 0xFF); + b[1] = (uint8_t)((v >> 16) & 0xFF); + b[2] = (uint8_t)((v >> 8) & 0xFF); + b[3] = (uint8_t)(v & 0xFF); + sha256_update(ctx, b, sizeof b); +} + +/* Streaming digest over the whole library: no fixed buffer, so no entry can ever fall + * outside the digest however many entries or dependencies the library holds. */ int omega_library_compute_digest(OmegaLibrary *lib) { if (!lib) return -1; - uint8_t buffer[65536]; - size_t pos = 0; + sha256_ctx ctx; + sha256_init(&ctx); /* Magic "LIB1" */ - buffer[pos++] = 'L'; buffer[pos++] = 'I'; buffer[pos++] = 'B'; buffer[pos++] = '1'; + sha256_update(&ctx, (const uint8_t *)"LIB1", 4); - /* Version (4 bytes BE) */ - buffer[pos++] = (uint8_t)((lib->version >> 24) & 0xFF); - buffer[pos++] = (uint8_t)((lib->version >> 16) & 0xFF); - buffer[pos++] = (uint8_t)((lib->version >> 8) & 0xFF); - buffer[pos++] = (uint8_t)(lib->version & 0xFF); - - /* Count (4 bytes BE) */ - uint32_t cnt = (uint32_t)lib->count; - buffer[pos++] = (uint8_t)((cnt >> 24) & 0xFF); - buffer[pos++] = (uint8_t)((cnt >> 16) & 0xFF); - buffer[pos++] = (uint8_t)((cnt >> 8) & 0xFF); - buffer[pos++] = (uint8_t)(cnt & 0xFF); + lib_digest_u32(&ctx, lib->version); + lib_digest_u32(&ctx, (uint32_t)lib->count); for (size_t i = 0; i < lib->count; ++i) { const OmegaLibraryEntry *e = &lib->entries[i]; - /* Program ID */ - memcpy(&buffer[pos], e->program.program_id.bytes, OMEGA_ID_BYTES); - pos += OMEGA_ID_BYTES; - - /* Realization ID */ - memcpy(&buffer[pos], e->program.realization.realization_id.bytes, OMEGA_ID_BYTES); - pos += OMEGA_ID_BYTES; - - /* Version introduced */ - buffer[pos++] = (uint8_t)((e->version_introduced >> 24) & 0xFF); - buffer[pos++] = (uint8_t)((e->version_introduced >> 16) & 0xFF); - buffer[pos++] = (uint8_t)((e->version_introduced >> 8) & 0xFF); - buffer[pos++] = (uint8_t)(e->version_introduced & 0xFF); - - /* Dependency count */ - uint32_t dc = (uint32_t)e->dep_count; - buffer[pos++] = (uint8_t)((dc >> 24) & 0xFF); - buffer[pos++] = (uint8_t)((dc >> 16) & 0xFF); - buffer[pos++] = (uint8_t)((dc >> 8) & 0xFF); - buffer[pos++] = (uint8_t)(dc & 0xFF); - - /* Dependency IDs */ + sha256_update(&ctx, e->program.program_id.bytes, OMEGA_ID_BYTES); + sha256_update(&ctx, e->program.realization.realization_id.bytes, OMEGA_ID_BYTES); + lib_digest_u32(&ctx, e->version_introduced); + { + uint8_t kind = e->admission_kind; /* VC1:digest-kind */ + sha256_update(&ctx, &kind, 1); + } + lib_digest_u32(&ctx, (uint32_t)e->dep_count); for (size_t d = 0; d < e->dep_count && d < OMEGA_LIB_MAX_DEPS; ++d) { - memcpy(&buffer[pos], e->dependency_ids[d].bytes, OMEGA_ID_BYTES); - pos += OMEGA_ID_BYTES; + sha256_update(&ctx, e->dependency_ids[d].bytes, OMEGA_ID_BYTES); } - - /* Evidence receipt hash */ - memcpy(&buffer[pos], e->evidence_receipt_hash, 32); - pos += 32; - - if (pos + 256 > sizeof(buffer)) break; + sha256_update(&ctx, e->evidence_receipt_hash, 32); } - sha256_hash(buffer, pos, lib->state_digest); + sha256_final(&ctx, lib->state_digest); return 0; } + int omega_library_advance_version(OmegaLibrary *lib) { if (!lib) return -1; lib->version++; @@ -158,37 +140,46 @@ bool omega_library_has_cycle(const OmegaLibrary *lib, const SemanticId *target_i return false; } -int omega_library_insert(OmegaLibrary *lib, const OmegaProgram *prog, - const SemanticId *deps, size_t dep_count, - const uint8_t receipt_hash[32]) { +static bool lib_hash_is_zero(const uint8_t h[32]) { + uint8_t acc = 0; + for (size_t i = 0; i < 32; ++i) acc |= h[i]; + return acc == 0; +} + +/* Shared admission path. Every refusal returns -1 before the library is touched. + * Each guard sits on one tagged line: the VC1-LIB mutation test (Makefile test-library) + * deletes or weakens exactly that line in a copy of this file and expects the unit test + * to fail. Keep one guard per line when editing. */ +static int lib_admit(OmegaLibrary *lib, const OmegaProgram *prog, + const SemanticId *deps, size_t dep_count, + const uint8_t hash[32], uint8_t kind) { if (!lib || !prog) return -1; /* Fail-closed verification gating: only verified programs admitted */ - if (!prog->is_verified || !prog->is_realized) { - return -1; - } + if (!prog->is_verified || !prog->is_realized) return -1; /* VC1:verified-gate */ /* Capacity check */ - if (lib->count >= OMEGA_LIB_MAX_PROGRAMS) { - return -1; - } + if (lib->count >= OMEGA_LIB_MAX_PROGRAMS) return -1; /* VC1:capacity */ /* No identity (no semantic body): refuse (spec/program-identity.md 2.3) */ { static const uint8_t zero[OMEGA_ID_BYTES]; - if (memcmp(prog->program_id.bytes, zero, OMEGA_ID_BYTES) == 0) return -1; + if (memcmp(prog->program_id.bytes, zero, OMEGA_ID_BYTES) == 0) return -1; /* VC1:zero-id */ } /* Duplicate check */ - if (omega_library_find_by_id(lib, &prog->program_id) != NULL) { - return -1; + if (omega_library_find_by_id(lib, &prog->program_id) != NULL) return -1; /* VC1:duplicate */ + + /* Dependencies: never truncated, every one must already be in the library */ + if (dep_count > OMEGA_LIB_MAX_DEPS) return -1; /* VC1:dep-overflow */ + if (dep_count > 0 && !deps) return -1; /* VC1:dep-null */ + for (size_t i = 0; i < dep_count; ++i) { + if (omega_library_find_by_id(lib, &deps[i]) == NULL) return -1; /* VC1:dep-unknown */ } - /* Cycle check */ - if (deps && dep_count > 0) { - if (omega_library_has_cycle(lib, &prog->program_id, deps, dep_count)) { - return -1; - } + /* Cycle check (kept: defence in depth; unreachable while deps must pre-exist) */ + if (dep_count > 0) { + if (omega_library_has_cycle(lib, &prog->program_id, deps, dep_count)) return -1; /* VC1:cycle */ } OmegaLibraryEntry *e = &lib->entries[lib->count]; @@ -197,23 +188,36 @@ int omega_library_insert(OmegaLibrary *lib, const OmegaProgram *prog, e->program = *prog; e->version_introduced = lib->version; e->timestamp_added = (uint64_t)time(NULL); - - if (deps && dep_count > 0) { - size_t to_copy = dep_count < OMEGA_LIB_MAX_DEPS ? dep_count : OMEGA_LIB_MAX_DEPS; - e->dep_count = to_copy; - for (size_t i = 0; i < to_copy; ++i) { - e->dependency_ids[i] = deps[i]; - } - } - - if (receipt_hash) { - memcpy(e->evidence_receipt_hash, receipt_hash, 32); + e->admission_kind = kind; + e->dep_count = dep_count; + for (size_t i = 0; i < dep_count; ++i) { + e->dependency_ids[i] = deps[i]; } + memcpy(e->evidence_receipt_hash, hash, 32); lib->count++; return omega_library_compute_digest(lib); } +int omega_library_insert(OmegaLibrary *lib, const OmegaProgram *prog, + const SemanticId *deps, size_t dep_count, + const uint8_t receipt_hash[32]) { + /* Evidence: a receipt hash is mandatory */ + if (!receipt_hash) return -1; /* VC1:null-receipt */ + if (lib_hash_is_zero(receipt_hash)) return -1; /* VC1:zero-receipt */ + return lib_admit(lib, prog, deps, dep_count, receipt_hash, OMEGA_LIB_ADMISSION_VERIFIED); +} + +/* Genesis loading only. See the contract in omega_library.h. No flag unlocks this. */ +int omega_library_insert_bootstrap(OmegaLibrary *lib, const OmegaProgram *prog, + const SemanticId *deps, size_t dep_count, + const uint8_t audit_hash[32]) { + /* Evidence: the audit hash of the manual genesis audit is mandatory */ + if (!audit_hash) return -1; /* VC1:bootstrap-null-audit */ + if (lib_hash_is_zero(audit_hash)) return -1; /* VC1:bootstrap-zero-audit */ + return lib_admit(lib, prog, deps, dep_count, audit_hash, OMEGA_LIB_ADMISSION_BOOTSTRAP); /* VC1:kind-bootstrap */ +} + int omega_library_export_primitives(const OmegaLibrary *lib, SynthPrimitiveBank *bank) { if (!lib || !bank) return -1; memset(bank, 0, sizeof(SynthPrimitiveBank)); diff --git a/src/omega_library.h b/src/omega_library.h index 00a6eda89..391be540f 100644 --- a/src/omega_library.h +++ b/src/omega_library.h @@ -7,7 +7,12 @@ #include #define OMEGA_LIB_MAX_PROGRAMS 128 -#define OMEGA_LIB_MAX_DEPS 8 +#define OMEGA_LIB_MAX_DEPS 32 + +/* How an entry was admitted. Part of the state digest, so a bootstrap entry and a verified + * entry with identical bytes never share a digest. 0 is never a valid kind. */ +#define OMEGA_LIB_ADMISSION_VERIFIED 1u /* omega_library_insert: verified + receipt */ +#define OMEGA_LIB_ADMISSION_BOOTSTRAP 2u /* omega_library_insert_bootstrap: genesis set only */ typedef struct { OmegaProgram program; @@ -15,7 +20,8 @@ typedef struct { uint64_t timestamp_added; size_t dep_count; SemanticId dependency_ids[OMEGA_LIB_MAX_DEPS]; - uint8_t evidence_receipt_hash[32]; + uint8_t evidence_receipt_hash[32]; /* verified: receipt hash; bootstrap: audit hash */ + uint8_t admission_kind; /* OMEGA_LIB_ADMISSION_* */ } OmegaLibraryEntry; typedef struct { @@ -31,17 +37,33 @@ int omega_library_init(OmegaLibrary *lib); /* Free all resources owned by the library */ void omega_library_destroy(OmegaLibrary *lib); -/* Insert a verified program into the library. - * Fails if: - * - prog is NULL or not verified (prog->is_verified == false) +/* Insert a verified program into the library (admission kind VERIFIED). + * Returns -1 and leaves the library unchanged if: + * - lib or prog is NULL, or prog is not verified and realized + * - receipt_hash is NULL or all zero (a verified admission needs evidence) + * - prog has no identity (all-zero program_id) * - duplicate SemanticId already exists * - capacity exceeded + * - dep_count > OMEGA_LIB_MAX_DEPS (never truncated), or deps is NULL with dep_count > 0 + * - any dependency id is not already in the library * - dependency cycle detected */ int omega_library_insert(OmegaLibrary *lib, const OmegaProgram *prog, const SemanticId *deps, size_t dep_count, const uint8_t receipt_hash[32]); +/* Insert one program of the manually audited GENESIS set (admission kind BOOTSTRAP). + * Same refusals as omega_library_insert, with audit_hash (NULL or all zero refused) in + * place of the receipt hash. + * + * MUST NEVER be called outside genesis loading. There is no runtime flag, environment + * variable or build option that unlocks it: reviewers must reject any caller that is not + * the genesis loader (VC1 stage 6). The admission kind is recorded in the entry and in the + * state digest, so bootstrap entries are always distinguishable from verified ones. */ +int omega_library_insert_bootstrap(OmegaLibrary *lib, const OmegaProgram *prog, + const SemanticId *deps, size_t dep_count, + const uint8_t audit_hash[32]); + /* Content-addressed lookup by program SemanticId */ const OmegaLibraryEntry* omega_library_find_by_id(const OmegaLibrary *lib, const SemanticId *prog_id); diff --git a/tests/program/test_program_id.c b/tests/program/test_program_id.c index 0fe054a91..707811338 100644 --- a/tests/program/test_program_id.c +++ b/tests/program/test_program_id.c @@ -480,6 +480,9 @@ static void test_mutation(void) { printf("mutations: %ld single mutations over %d programs\n", mut, MUT_N); } +/* Non-zero evidence hash: omega_library_insert refuses NULL or all-zero. */ +static const uint8_t OMEGA_TEST_LIB_RECEIPT[32] = { 0xA5, 0x5A, 0xC3, 0x3C }; + /* -------------------------------------------------------- LIBRARY_REGRESSION */ static void test_library(void) { OmegaLibrary *lib = calloc(1, sizeof *lib); @@ -492,10 +495,10 @@ static void test_library(void) { omega_program_build_unary_op(&b, "plus_seven", OP_ADD, 7); omega_program_build_unary_op(&c, "add7", OP_ADD, 8); a.is_verified = b.is_verified = c.is_verified = true; - CHECK(G_LIB, omega_library_insert(lib, &a, NULL, 0, NULL) == 0, "insert add7"); - CHECK(G_LIB, omega_library_insert(lib, &b, NULL, 0, NULL) != 0 && lib->count == 1, + CHECK(G_LIB, omega_library_insert(lib, &a, NULL, 0, OMEGA_TEST_LIB_RECEIPT) == 0, "insert add7"); + CHECK(G_LIB, omega_library_insert(lib, &b, NULL, 0, OMEGA_TEST_LIB_RECEIPT) != 0 && lib->count == 1, "same body + contract under another name is a duplicate"); - CHECK(G_LIB, omega_library_insert(lib, &c, NULL, 0, NULL) == 0 && lib->count == 2, + CHECK(G_LIB, omega_library_insert(lib, &c, NULL, 0, OMEGA_TEST_LIB_RECEIPT) == 0 && lib->count == 2, "different body under the same name is admitted"); CHECK(G_LIB, omega_library_find_by_id(lib, &a.program_id) && omega_library_find_by_id(lib, &c.program_id) && omega_library_find_by_id(lib, &a.program_id) != omega_library_find_by_id(lib, &c.program_id), @@ -503,12 +506,12 @@ static void test_library(void) { omega_program_init(&d, "opaque"); d.contract = a.contract; d.is_verified = d.is_realized = true; omega_program_compute_id(&d); - CHECK(G_LIB, omega_library_insert(lib, &d, NULL, 0, NULL) != 0 && lib->count == 2, "no-body program refused"); + CHECK(G_LIB, omega_library_insert(lib, &d, NULL, 0, OMEGA_TEST_LIB_RECEIPT) != 0 && lib->count == 2, "no-body program refused"); /* dependency DAG */ CHECK(G_LIB, omega_program_compose(&a, &c, &comp, err, sizeof err) == 0, "compose"); comp.is_verified = true; SemanticId deps[2] = { a.program_id, c.program_id }; - CHECK(G_LIB, omega_library_insert(lib, &comp, deps, 2, NULL) == 0, "insert composite with deps"); + CHECK(G_LIB, omega_library_insert(lib, &comp, deps, 2, OMEGA_TEST_LIB_RECEIPT) == 0, "insert composite with deps"); SemanticId cyc[1] = { comp.program_id }; CHECK(G_LIB, omega_library_has_cycle(lib, &a.program_id, cyc, 1), "cycle detected"); omega_library_destroy(lib); diff --git a/tests/test_omega_library.c b/tests/test_omega_library.c new file mode 100644 index 000000000..4b08dbc1d --- /dev/null +++ b/tests/test_omega_library.c @@ -0,0 +1,163 @@ +/* test_omega_library.c -- VC1-LIB: the verified program library as the only admission gate. + * + * Usage: + * test_omega_library run every check; exit 0 only if all pass + * test_omega_library the binary was linked against a deliberately broken copy of + * src/omega_library.c (built by `make test-library`); run every + * check and require the mapped check to FAIL. Exit 1 = mutant + * killed (what the Makefile asserts), 3 = mutant survived, + * 2 = unknown mutant name. + * + * Programs are built the way tests/program/test_program_id.c builds them: + * omega_program_build_unary_op(), then is_verified = true. Physics-free, host-portable. + */ +#include +#include +#include +#include + +#include "omega_core.h" +#include "omega_program.h" +#include "omega_library.h" + +/* check -> mutant that must kill it. Each mutant is one deleted/weakened guard line in + * omega_library.c (see the sed table in the Makefile test-library target). */ +static const struct { const char *mutant, *check; } MUTANTS[] = { + { "allow-null-receipt", "null-receipt-refused" }, + { "allow-zero-receipt", "zero-receipt-refused" }, + { "allow-unknown-dep", "unknown-dep-refused" }, + { "truncate-deps", "dep-overflow-refused" }, + { "allow-duplicate", "duplicate-refused" }, + { "allow-unverified", "unverified-refused" }, + { "digest-ignores-kind", "bootstrap-vs-verified-digest-differ" }, + { "bootstrap-as-verified", "bootstrap-kind-recorded" }, + { "refuse-valid", "valid-verified-accepted" }, + { "refuse-deps", "existing-dep-accepted" }, + { "max-deps-off-by-one", "max-deps-accepted" }, + { "bootstrap-null-audit", "bootstrap-null-audit-refused" }, + { "bootstrap-zero-audit", "bootstrap-zero-audit-refused" }, +}; +#define N_MUT (sizeof MUTANTS / sizeof MUTANTS[0]) + +static const char *g_mutant; /* NULL in normal mode */ +static const char *g_expect_check; /* check that must fail under the mutant */ +static int g_failed, g_expect_failed; + +static void check(const char *name, int ok) { + printf("%s %s\n", ok ? "PASS" : "FAIL", name); + if (!ok) { + g_failed++; + if (g_expect_check && strcmp(name, g_expect_check) == 0) g_expect_failed = 1; + } +} + +static const uint8_t RECEIPT[32] = { 0xA5, 0x5A, 0xC3, 0x3C, 1 }; +static const uint8_t ZERO[32]; + +static OmegaLibrary *new_lib(void) { + OmegaLibrary *l = calloc(1, sizeof *l); + if (!l || omega_library_init(l) != 0) { fprintf(stderr, "setup failed\n"); exit(2); } + return l; +} + +static OmegaLibrary *reset(OmegaLibrary *l) { + omega_library_destroy(l); + free(l); + return new_lib(); +} + +static void mk(OmegaProgram *p, const char *name, OpCode op, uint64_t imm) { + if (omega_program_build_unary_op(p, name, op, imm) != 0) { fprintf(stderr, "build failed\n"); exit(2); } + p->is_verified = true; +} + +int main(int argc, char **argv) { + if (argc > 1) { + g_mutant = argv[1]; + for (size_t i = 0; i < N_MUT; i++) + if (strcmp(MUTANTS[i].mutant, g_mutant) == 0) g_expect_check = MUTANTS[i].check; + if (!g_expect_check) { fprintf(stderr, "unknown mutant '%s'\n", g_mutant); return 2; } + } + + OmegaLibrary *lib = new_lib(); + OmegaProgram a, b, unv; + mk(&a, "add7", OP_ADD, 7); + mk(&b, "add8", OP_ADD, 8); + mk(&unv, "add9", OP_ADD, 9); + unv.is_verified = false; + + /* Evidence is mandatory. Each refusal runs on a fresh library so a weakened guard in one + * path cannot hide behind state left by another (count must stay 0). */ + check("null-receipt-refused", omega_library_insert(lib, &a, NULL, 0, NULL) != 0 && lib->count == 0); + lib = reset(lib); + check("zero-receipt-refused", omega_library_insert(lib, &a, NULL, 0, ZERO) != 0 && lib->count == 0); + lib = reset(lib); + check("bootstrap-null-audit-refused", omega_library_insert_bootstrap(lib, &a, NULL, 0, NULL) != 0 && lib->count == 0); + lib = reset(lib); + check("bootstrap-zero-audit-refused", omega_library_insert_bootstrap(lib, &a, NULL, 0, ZERO) != 0 && lib->count == 0); + lib = reset(lib); + check("unverified-refused", omega_library_insert(lib, &unv, NULL, 0, RECEIPT) != 0 && lib->count == 0); + lib = reset(lib); + + /* valid verified insert */ + int rc = omega_library_insert(lib, &a, NULL, 0, RECEIPT); + check("valid-verified-accepted", rc == 0 && lib->count == 1 && + lib->entries[0].admission_kind == OMEGA_LIB_ADMISSION_VERIFIED && + memcmp(lib->entries[0].evidence_receipt_hash, RECEIPT, 32) == 0); + + /* duplicate */ + size_t before = lib->count; + check("duplicate-refused", omega_library_insert(lib, &a, NULL, 0, RECEIPT) != 0 && lib->count == before); + + /* dependencies must exist */ + SemanticId unknown; + memset(&unknown, 0xEE, sizeof unknown); + before = lib->count; + check("unknown-dep-refused", omega_library_insert(lib, &b, &unknown, 1, RECEIPT) != 0 && lib->count == before); + SemanticId dep = a.program_id; + rc = omega_library_insert(lib, &b, &dep, 1, RECEIPT); + check("existing-dep-accepted", rc == 0 && lib->count == before + 1 && lib->entries[before].dep_count == 1 && + memcmp(lib->entries[before].dependency_ids[0].bytes, dep.bytes, OMEGA_ID_BYTES) == 0); + + /* dep_count boundary: MAX accepted, MAX+1 refused (not truncated). OMEGA_LIB_MAX_DEPS distinct + * base programs + 2 composites; the deps array repeats existing ids, which is legal input. */ + OmegaLibrary *l2 = new_lib(); + SemanticId ids[OMEGA_LIB_MAX_DEPS + 1]; + for (int i = 0; i < OMEGA_LIB_MAX_DEPS; i++) { + OmegaProgram p; + mk(&p, "base", OP_ADD, 100 + (uint64_t)i); + ids[i] = p.program_id; + if (omega_library_insert(l2, &p, NULL, 0, RECEIPT) != 0) { fprintf(stderr, "setup insert failed\n"); return 2; } + } + ids[OMEGA_LIB_MAX_DEPS] = ids[0]; + OmegaProgram top; + mk(&top, "top", OP_MUL, 3); + size_t c2 = l2->count; + check("dep-overflow-refused", + omega_library_insert(l2, &top, ids, OMEGA_LIB_MAX_DEPS + 1, RECEIPT) != 0 && l2->count == c2); + rc = omega_library_insert(l2, &top, ids, OMEGA_LIB_MAX_DEPS, RECEIPT); + check("max-deps-accepted", rc == 0 && l2->count == c2 + 1 && l2->entries[c2].dep_count == OMEGA_LIB_MAX_DEPS); + + /* bootstrap vs verified: same program bytes, same hash bytes, different admission kind */ + OmegaLibrary *lv = new_lib(), *lb = new_lib(); + int rv = omega_library_insert(lv, &a, NULL, 0, RECEIPT); + int rb = omega_library_insert_bootstrap(lb, &a, NULL, 0, RECEIPT); + check("bootstrap-kind-recorded", rv == 0 && rb == 0 && + lb->entries[0].admission_kind == OMEGA_LIB_ADMISSION_BOOTSTRAP && + lv->entries[0].admission_kind == OMEGA_LIB_ADMISSION_VERIFIED); + check("bootstrap-vs-verified-digest-differ", rv == 0 && rb == 0 && + memcmp(lv->state_digest, lb->state_digest, 32) != 0); + + omega_library_destroy(lib); free(lib); + omega_library_destroy(l2); free(l2); + omega_library_destroy(lv); free(lv); + omega_library_destroy(lb); free(lb); + + if (!g_mutant) { + printf("%s: %d check(s) failed\n", g_failed ? "FAIL" : "PASS", g_failed); + return g_failed ? 1 : 0; + } + if (g_expect_failed) { printf("MUTANT %s KILLED by %s\n", g_mutant, g_expect_check); return 1; } + printf("MUTANT %s SURVIVED (check %s did not fail)\n", g_mutant, g_expect_check); + return 3; +} diff --git a/tools/omegatool.c b/tools/omegatool.c index d47bd76c6..f4b7bb28f 100644 --- a/tools/omegatool.c +++ b/tools/omegatool.c @@ -1600,6 +1600,9 @@ static void run_demonstration_synthesis(void) { printf("================================================================================\n"); } +/* Non-zero evidence hash for the gate programs: omega_library_insert refuses NULL or all-zero. */ +static const uint8_t OMEGA_TEST_LIB_RECEIPT[32] = { 0xA5, 0x5A, 0xC3, 0x3C }; + /* ========================================================================= * MILESTONE 10 GATES: OMEGA_LIBRARY_V1 * ========================================================================= */ @@ -1638,7 +1641,7 @@ static bool test_m10_lookup_id(void) { VerifyReport rep; omega_program_verify(&p, &rep); - omega_library_insert(&lib, &p, NULL, 0, NULL); + omega_library_insert(&lib, &p, NULL, 0, OMEGA_TEST_LIB_RECEIPT); const OmegaLibraryEntry *e = omega_library_find_by_id(&lib, &p.program_id); bool ok = (e != NULL && omega_compare_semantic_id(&e->program.program_id, &p.program_id) == 0); @@ -1659,14 +1662,14 @@ static bool test_m10_lookup_type(void) { omega_program_build_unary_op(&p1, "u64_add3", OP_ADD, 3); VerifyReport rep; omega_program_verify(&p1, &rep); - omega_library_insert(&lib, &p1, NULL, 0, NULL); + omega_library_insert(&lib, &p1, NULL, 0, OMEGA_TEST_LIB_RECEIPT); omega_program_build_unary_op(&p2, "u32_sub1", OP_SUB, 1); p2.contract.input_width = 32; p2.contract.output_width = 32; omega_program_compute_id(&p2); omega_program_verify(&p2, &rep); - omega_library_insert(&lib, &p2, NULL, 0, NULL); + omega_library_insert(&lib, &p2, NULL, 0, OMEGA_TEST_LIB_RECEIPT); const OmegaLibraryEntry *results[4]; size_t found = omega_library_query_by_type(&lib, TYPE_UNSIGNED_INT, 64, TYPE_UNSIGNED_INT, 64, results, 4); @@ -1686,15 +1689,15 @@ static bool test_m10_dependency_dag(void) { VerifyReport rep; omega_program_verify(&p1, &rep); omega_program_verify(&p2, &rep); - omega_library_insert(&lib, &p1, NULL, 0, NULL); - omega_library_insert(&lib, &p2, NULL, 0, NULL); + omega_library_insert(&lib, &p1, NULL, 0, OMEGA_TEST_LIB_RECEIPT); + omega_library_insert(&lib, &p2, NULL, 0, OMEGA_TEST_LIB_RECEIPT); char err[256]; omega_program_compose(&p1, &p2, &c, err, sizeof(err)); omega_program_verify(&c, &rep); SemanticId deps[2] = { p1.program_id, p2.program_id }; - int rc = omega_library_insert(&lib, &c, deps, 2, NULL); + int rc = omega_library_insert(&lib, &c, deps, 2, OMEGA_TEST_LIB_RECEIPT); if (rc != 0) { omega_library_destroy(&lib); return false; } const OmegaLibraryEntry *e = omega_library_find_by_id(&lib, &c.program_id); @@ -1718,7 +1721,7 @@ static bool test_m10_immutability(void) { omega_program_build_unary_op(&p, "mul3", OP_MUL, 3); VerifyReport rep; omega_program_verify(&p, &rep); - omega_library_insert(&lib, &p, NULL, 0, NULL); + omega_library_insert(&lib, &p, NULL, 0, OMEGA_TEST_LIB_RECEIPT); uint8_t digest1[32]; memcpy(digest1, lib.state_digest, 32); @@ -1741,7 +1744,7 @@ static bool test_m10_unverified_refusal(void) { omega_program_build_unary_op(&unverified, "unverified_prog", OP_ADD, 10); unverified.is_verified = false; - int rc = omega_library_insert(&lib, &unverified, NULL, 0, NULL); + int rc = omega_library_insert(&lib, &unverified, NULL, 0, OMEGA_TEST_LIB_RECEIPT); bool ok = (rc != 0 && lib.count == 0); omega_program_destroy(&unverified); @@ -1758,8 +1761,8 @@ static bool test_m10_duplicate_refusal(void) { VerifyReport rep; omega_program_verify(&p, &rep); - int rc1 = omega_library_insert(&lib, &p, NULL, 0, NULL); - int rc2 = omega_library_insert(&lib, &p, NULL, 0, NULL); + int rc1 = omega_library_insert(&lib, &p, NULL, 0, OMEGA_TEST_LIB_RECEIPT); + int rc2 = omega_library_insert(&lib, &p, NULL, 0, OMEGA_TEST_LIB_RECEIPT); bool ok = (rc1 == 0 && rc2 != 0 && lib.count == 1); @@ -1779,13 +1782,13 @@ static bool test_m10_synthesis_reuse(void) { omega_program_compose(&m2, &a1, &comp_2x1, err, sizeof(err)); VerifyReport rep; omega_program_verify(&comp_2x1, &rep); - omega_library_insert(&lib, &comp_2x1, NULL, 0, NULL); + omega_library_insert(&lib, &comp_2x1, NULL, 0, OMEGA_TEST_LIB_RECEIPT); /* Insert base primitive B = add_5 */ OmegaProgram a5; omega_program_build_unary_op(&a5, "add5", OP_ADD, 5); omega_program_verify(&a5, &rep); - omega_library_insert(&lib, &a5, NULL, 0, NULL); + omega_library_insert(&lib, &a5, NULL, 0, OMEGA_TEST_LIB_RECEIPT); /* Export library as primitive bank */ SynthPrimitiveBank bank; @@ -1829,7 +1832,7 @@ static bool test_m10_receipt(void) { omega_program_build_unary_op(&p, "test_receipt_p", OP_ADD, 1); VerifyReport rep; omega_program_verify(&p, &rep); - omega_library_insert(&lib, &p, NULL, 0, NULL); + omega_library_insert(&lib, &p, NULL, 0, OMEGA_TEST_LIB_RECEIPT); bool ok = (lib.count == 1 && lib.version >= 1); omega_library_destroy(&lib); @@ -1857,15 +1860,15 @@ static void run_demonstration_library(void) { omega_program_verify(&a1, &rep); omega_program_verify(&a5, &rep); - omega_library_insert(&lib, &m2, NULL, 0, NULL); - omega_library_insert(&lib, &a1, NULL, 0, NULL); - omega_library_insert(&lib, &a5, NULL, 0, NULL); + omega_library_insert(&lib, &m2, NULL, 0, OMEGA_TEST_LIB_RECEIPT); + omega_library_insert(&lib, &a1, NULL, 0, OMEGA_TEST_LIB_RECEIPT); + omega_library_insert(&lib, &a5, NULL, 0, OMEGA_TEST_LIB_RECEIPT); char err[256]; omega_program_compose(&m2, &a1, &c_2x1, err, sizeof(err)); omega_program_verify(&c_2x1, &rep); SemanticId deps[2] = { m2.program_id, a1.program_id }; - omega_library_insert(&lib, &c_2x1, deps, 2, NULL); + omega_library_insert(&lib, &c_2x1, deps, 2, OMEGA_TEST_LIB_RECEIPT); printf(" [2] Components Inserted with Dependency Tracking:\n"); for (size_t i = 0; i < lib.count; ++i) {