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 / tools / turing_cal_dev.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 | /* EXP-001 lane A: development-data loader shared by tools/turing_cal_candidates.c |
| 18 | * and calibration/scripts/power_simulation.c. |
| 19 | * |
| 20 | * Reads the TY-2 trace manifest (evidence/TURING_YIELD/trace_manifest_rep10_control.sha256) |
| 21 | * but only ever opens entries 1-7 (dev seeds 1-7 of exp-20260927-rep10 control). |
| 22 | * Entries 8-10 are burned held-out seeds: their lines are parsed for count only |
| 23 | * and their paths are never opened. Every dev file is re-hashed (SHA-256) and |
| 24 | * compared with the manifest before it is read. |
| 25 | */ |
| 26 | #ifndef TURING_CAL_DEV_H |
| 27 | #define TURING_CAL_DEV_H |
| 28 | |
| 29 | #include "turing/ty_record.h" |
| 30 | |
| 31 | #include <inttypes.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <sys/stat.h> |
| 36 | |
| 37 | #define TCD_NSEED 10 |
| 38 | #define TCD_NDEV 7 |
| 39 | |
| 40 | typedef struct { |
| 41 | char hex[65]; |
| 42 | uint64_t size; |
| 43 | char rel[256]; |
| 44 | char path[1024]; |
| 45 | ty_digest d; |
| 46 | } tcd_entry; |
| 47 | |
| 48 | static tcd_entry TCD_M[TCD_NSEED]; |
| 49 | static ty_stream TCD_S[TCD_NDEV]; |
| 50 | static int TCD_loaded[TCD_NDEV]; |
| 51 | |
| 52 | static inline int tcd_load_manifest(const char *mpath, const char *root) { |
| 53 | FILE *f = fopen(mpath, "r"); |
| 54 | if (!f) return TY_E_IO; |
| 55 | char line[1400]; |
| 56 | int n = 0; |
| 57 | while (fgets(line, sizeof line, f) && n < TCD_NSEED) { |
| 58 | if (line[0] == '#' || line[0] == '\n') continue; |
| 59 | tcd_entry *e = &TCD_M[n]; |
| 60 | if (sscanf(line, "%64s %" SCNu64 " %255s", e->hex, &e->size, e->rel) != 3 || ty_parse_hex(e->hex, &e->d) != TY_OK) { |
| 61 | fclose(f); |
| 62 | return TY_E_FORMAT; |
| 63 | } |
| 64 | char want[32]; |
| 65 | snprintf(want, sizeof want, "/seed-%d/control/", n + 1); |
| 66 | if (!strstr(e->rel, want)) { |
| 67 | fclose(f); |
| 68 | return TY_E_FORMAT; /* entries must be seeds 1..10 in order */ |
| 69 | } |
| 70 | size_t lr = strlen(root), ll = strlen(e->rel); |
| 71 | if (lr + 1 + ll >= sizeof e->path) { |
| 72 | fclose(f); |
| 73 | return TY_E_FORMAT; |
| 74 | } |
| 75 | memcpy(e->path, root, lr); |
| 76 | e->path[lr] = '/'; |
| 77 | memcpy(e->path + lr + 1, e->rel, ll + 1); |
| 78 | ++n; |
| 79 | } |
| 80 | fclose(f); |
| 81 | return n == TCD_NSEED ? TY_OK : TY_E_FORMAT; |
| 82 | } |
| 83 | |
| 84 | /* seed is 1..7. Re-hash, then read. Any other seed number is refused. */ |
| 85 | static inline int tcd_load_seed(int seed) { |
| 86 | if (seed < 1 || seed > TCD_NDEV) return TY_E_LEAK; |
| 87 | int i = seed - 1; |
| 88 | if (TCD_loaded[i]) return TY_OK; |
| 89 | struct stat st; |
| 90 | if (stat(TCD_M[i].path, &st) != 0 || (uint64_t)st.st_size != TCD_M[i].size) return TY_E_IO; |
| 91 | ty_digest d; |
| 92 | int rc = ty_file_sha256(TCD_M[i].path, &d); |
| 93 | if (rc != TY_OK) return rc; |
| 94 | if (!ty_digest_eq(&d, &TCD_M[i].d)) return TY_E_DIGEST; |
| 95 | char why[256] = ""; |
| 96 | ty_stream_init(&TCD_S[i]); |
| 97 | int64_t n = ty_ctr1_read(TCD_M[i].path, &TCD_S[i], why, sizeof why); |
| 98 | if (n < 0) { |
| 99 | fprintf(stderr, "seed %d: %s\n", seed, why); |
| 100 | return (int)n; |
| 101 | } |
| 102 | fprintf(stderr, "dev seed %d: %lld events, %u crumbs, sha256 ok\n", seed, (long long)n, TCD_S[i].ncrumb); |
| 103 | TCD_loaded[i] = 1; |
| 104 | return TY_OK; |
| 105 | } |
| 106 | |
| 107 | static inline void tcd_hex(const uint8_t d[32], char out[65]) { |
| 108 | for (int i = 0; i < 32; ++i) snprintf(out + 2 * i, 3, "%02x", d[i]); |
| 109 | } |
| 110 | |
| 111 | #endif /* TURING_CAL_DEV_H */ |