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
omega-mv / tests / vc_fixture.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* vc_fixture.h -- real Omega programs for Verified Crumb tests (VC1 stage 6). |
| 18 | * |
| 19 | * Since stage 6 the build-domain resolver recomputes the program id from the stored IR |
| 20 | * (SPEC 6 step 3), so a test record can no longer name a made-up 32-byte id. Fixture n is the real |
| 21 | * program "x + (n + 1)" over u64, built the way omega_program_build_unary_op builds every unary |
| 22 | * program. Its semantic id is the real omega_program_compute_id, its IR blob is the real |
| 23 | * omega_program_ir_encode, its source_or_ir_digest is the SHA-256 of that blob. |
| 24 | * |
| 25 | * Two extra blobs are served by vcfx_fetch_blob: a canonical-source stand-in (for records whose |
| 26 | * digest kind is OSC source) and bytes that are not an IR at all. Include in ONE translation unit. */ |
| 27 | #ifndef VC_FIXTURE_H |
| 28 | #define VC_FIXTURE_H |
| 29 | |
| 30 | #include <stdint.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #include "omega_program.h" |
| 36 | #include "omega_program_ir.h" |
| 37 | #include "sha256.h" |
| 38 | |
| 39 | #define VCFX_UNUSED __attribute__((unused)) |
| 40 | |
| 41 | typedef struct { uint8_t id[32], digest[32]; uint8_t *ir; size_t ir_len; int built; } VcFx; |
| 42 | static VcFx g_vcfx[256]; |
| 43 | |
| 44 | static const VcFx *vcfx(uint8_t n) VCFX_UNUSED; |
| 45 | static const VcFx *vcfx(uint8_t n) { |
| 46 | VcFx *f = &g_vcfx[n]; |
| 47 | if (f->built) return f; |
| 48 | OmegaProgram *p = calloc(1, sizeof *p); |
| 49 | if (!p || omega_program_build_unary_op(p, "vcfx", OP_ADD, (uint64_t)n + 1u) != 0 || |
| 50 | omega_program_ir_encode(p, &f->ir, &f->ir_len) != 0) { |
| 51 | fprintf(stderr, "vc_fixture: cannot build program %u\n", (unsigned)n); |
| 52 | exit(2); |
| 53 | } |
| 54 | memcpy(f->id, p->program_id.bytes, 32); |
| 55 | sha256_hash(f->ir, f->ir_len, f->digest); |
| 56 | f->built = 1; |
| 57 | omega_program_destroy(p); |
| 58 | free(p); |
| 59 | return f; |
| 60 | } |
| 61 | |
| 62 | /* canonical-source stand-in and non-IR bytes */ |
| 63 | static const char VCFX_SOURCE[] = "fn add(a: u32, b: u32) -> u32 { return a + b; }\n"; |
| 64 | static const char VCFX_GARBAGE[] = "these bytes are not an Omega program IR"; |
| 65 | static void vcfx_source_digest(uint8_t out[32]) VCFX_UNUSED; |
| 66 | static void vcfx_source_digest(uint8_t out[32]) { sha256_hash((const uint8_t *)VCFX_SOURCE, sizeof VCFX_SOURCE - 1, out); } |
| 67 | static void vcfx_garbage_digest(uint8_t out[32]) VCFX_UNUSED; |
| 68 | static void vcfx_garbage_digest(uint8_t out[32]) { sha256_hash((const uint8_t *)VCFX_GARBAGE, sizeof VCFX_GARBAGE - 1, out); } |
| 69 | |
| 70 | static int vcfx_copy(const void *p, size_t n, uint8_t **bytes, size_t *len) { |
| 71 | *bytes = malloc(n + 1); |
| 72 | if (!*bytes) return 2; |
| 73 | memcpy(*bytes, p, n); |
| 74 | *len = n; |
| 75 | return 0; |
| 76 | } |
| 77 | /* OmegaBlobFetch over every fixture built so far plus the two extra blobs */ |
| 78 | static int vcfx_fetch_blob(void *ctx, const uint8_t digest[32], uint8_t **bytes, size_t *len) VCFX_UNUSED; |
| 79 | static int vcfx_fetch_blob(void *ctx, const uint8_t digest[32], uint8_t **bytes, size_t *len) { |
| 80 | (void)ctx; |
| 81 | uint8_t d[32]; |
| 82 | for (int n = 0; n < 256; n++) |
| 83 | 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); |
| 84 | vcfx_source_digest(d); |
| 85 | if (memcmp(d, digest, 32) == 0) return vcfx_copy(VCFX_SOURCE, sizeof VCFX_SOURCE - 1, bytes, len); |
| 86 | vcfx_garbage_digest(d); |
| 87 | if (memcmp(d, digest, 32) == 0) return vcfx_copy(VCFX_GARBAGE, sizeof VCFX_GARBAGE - 1, bytes, len); |
| 88 | return 1; |
| 89 | } |
| 90 | /* a blob store that answers every digest with the same wrong bytes */ |
| 91 | static int vcfx_fetch_wrong(void *ctx, const uint8_t digest[32], uint8_t **bytes, size_t *len) VCFX_UNUSED; |
| 92 | static int vcfx_fetch_wrong(void *ctx, const uint8_t digest[32], uint8_t **bytes, size_t *len) { |
| 93 | (void)ctx; (void)digest; |
| 94 | return vcfx_copy("not the source", 14, bytes, len); |
| 95 | } |
| 96 | |
| 97 | #endif /* VC_FIXTURE_H */ |