interplane.Get Omega
Read in your language

aien-protocols-mv

Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem

Brought by drakestapleton

branch main Branches Tags History

aien-protocols-mv / specs / execution-transcript / tools / trn1_verify.c

500 lines · 23192 bytes Raw Blame History
1/* trn1_verify.c: reference verifier for the TRN1 execution transcript,
2 * contract 0.2.0: wire schema 1 (0.1 registry) and wire schema 3 (0.2
3 * registry, adds types 17, 18, 19, 21). Spec: ../TRN1_TRANSCRIPT_SPEC.md.
4 *
5 * Self-contained C99: its own SHA-256 (FIPS 180-4), libc only, no imports
6 * from any implementation repository. It exists to prove the corpus, not to
7 * be linked into a runtime.
8 *
9 * Usage:
10 * trn1_verify FILE print one verdict line (spec section 7)
11 * trn1_verify --compare EXP ACT print one compare line (spec section 8)
12 * trn1_verify --corpus DIR check DIR/expected.txt and DIR/compare.txt,
13 * print TRN1_CONFORMANCE impl=c pass=N fail=M
14 *
15 * Exit status: 0 on accept / MATCH / all corpus lines agree, 1 otherwise,
16 * 2 on usage or I/O error.
17 *
18 * Mutation hooks: building with -DTRN1_MUTANT=K disables check K (the
19 * numbers are the CHK_* values below). tools/mutate-verifier.sh builds every
20 * mutant and requires the corpus to fail each one, which shows every check
21 * is exercised by at least one vector.
22 */
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#ifndef TRN1_MUTANT
29#define TRN1_MUTANT 0
30#endif
31#define CHECK(k) (TRN1_MUTANT != (k))
32
33enum {
34 CHK_MAGIC = 1, CHK_VERSION, CHK_HDR_FLAGS, CHK_PRODUCER, CHK_HDR_RESERVED,
35 CHK_TYPE, CHK_SUBSYS, CHK_REC_RESERVED, CHK_LIMIT, CHK_SEQ, CHK_PREV,
36 CHK_IDENT_LEN, CHK_END_SUBSYS, CHK_BODY_RESERVED, CHK_CAP_OP,
37 CHK_CRASH_SEQ, CHK_CRUMB_SHAPE, CHK_CRUMB_DIGEST, CHK_ARGUS_FLAG,
38 CHK_ARGUS_LINK, CHK_ARGUS_CONT, CHK_INPUT_DIGEST, CHK_END_ANNOT,
39 CHK_END_COUNT, CHK_TRAILING, CHK_TRUNCATED,
40 CHK_SCHEMA_GATE, CHK_CAUSE_MODE, CHK_CAUSE_KIND, CHK_CAUSE_LOCAL,
41 CHK_CAUSE_ROOT, CHK_RBIND_FORMAT, CHK_RBIND_RESERVED, CHK_RLINK_REL,
42 CHK_RLINK_RESERVED, CHK_RLINK_FIRST, CHK_RES_OP, CHK_RES_REASON,
43 CHK_RES_FIELD, CHK_RES_RESERVED, CHK_REF_KIND, CHK_REF_LOCAL, CHK_LAST
44};
45
46/* ---- refusal codes (spec section 6) ------------------------------------ */
47enum {
48 R_OK = 0, R_MAGIC = -1, R_VERSION = -2, R_LENGTH = -3, R_NONCANONICAL = -4,
49 R_UNKNOWN = -5, R_SHAPE = -6, R_GAP = -7, R_CHAIN = -8, R_DIGEST = -9,
50 R_TRUNCATED = -10
51};
52
53/* ---- SHA-256 ------------------------------------------------------------ */
54typedef struct { uint32_t h[8]; uint64_t n; uint8_t b[64]; uint32_t used; } sha256;
55
56static const uint32_t K256[64] = {
57 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
58 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
59 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
60 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
61 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
62 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
63 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
64 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
65};
66#define ROR(x, r) (((x) >> (r)) | ((x) << (32 - (r))))
67
68static void sha_block(sha256 *s, const uint8_t *p) {
69 uint32_t w[64], a, b, c, d, e, f, g, h;
70 for (int i = 0; i < 16; i++)
71 w[i] = (uint32_t)p[4*i] << 24 | (uint32_t)p[4*i+1] << 16 | (uint32_t)p[4*i+2] << 8 | p[4*i+3];
72 for (int i = 16; i < 64; i++) {
73 uint32_t s0 = ROR(w[i-15], 7) ^ ROR(w[i-15], 18) ^ (w[i-15] >> 3);
74 uint32_t s1 = ROR(w[i-2], 17) ^ ROR(w[i-2], 19) ^ (w[i-2] >> 10);
75 w[i] = w[i-16] + s0 + w[i-7] + s1;
76 }
77 a = s->h[0]; b = s->h[1]; c = s->h[2]; d = s->h[3];
78 e = s->h[4]; f = s->h[5]; g = s->h[6]; h = s->h[7];
79 for (int i = 0; i < 64; i++) {
80 uint32_t t1 = h + (ROR(e, 6) ^ ROR(e, 11) ^ ROR(e, 25)) + ((e & f) ^ (~e & g)) + K256[i] + w[i];
81 uint32_t t2 = (ROR(a, 2) ^ ROR(a, 13) ^ ROR(a, 22)) + ((a & b) ^ (a & c) ^ (b & c));
82 h = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2;
83 }
84 s->h[0] += a; s->h[1] += b; s->h[2] += c; s->h[3] += d;
85 s->h[4] += e; s->h[5] += f; s->h[6] += g; s->h[7] += h;
86}
87static void sha_init(sha256 *s) {
88 static const uint32_t iv[8] = { 0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,
89 0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19 };
90 memcpy(s->h, iv, sizeof iv); s->n = 0; s->used = 0;
91}
92static void sha_update(sha256 *s, const void *data, size_t len) {
93 const uint8_t *p = data;
94 s->n += len;
95 while (len) {
96 uint32_t k = 64 - s->used;
97 if (k > len) k = (uint32_t)len;
98 memcpy(s->b + s->used, p, k);
99 s->used += k; p += k; len -= k;
100 if (s->used == 64) { sha_block(s, s->b); s->used = 0; }
101 }
102}
103static void sha_final(sha256 *s, uint8_t out[32]) {
104 uint64_t bits = s->n * 8;
105 uint8_t pad = 0x80, z = 0, l[8];
106 sha_update(s, &pad, 1);
107 while (s->used != 56) sha_update(s, &z, 1);
108 for (int i = 0; i < 8; i++) l[i] = (uint8_t)(bits >> (56 - 8 * i));
109 sha_update(s, l, 8);
110 for (int i = 0; i < 8; i++) {
111 out[4*i] = (uint8_t)(s->h[i] >> 24); out[4*i+1] = (uint8_t)(s->h[i] >> 16);
112 out[4*i+2] = (uint8_t)(s->h[i] >> 8); out[4*i+3] = (uint8_t)s->h[i];
113 }
114}
115static void sha2(const void *a, size_t na, const void *b, size_t nb, uint8_t out[32]) {
116 sha256 s; sha_init(&s); sha_update(&s, a, na); if (nb) sha_update(&s, b, nb); sha_final(&s, out);
117}
118
119/* ---- little-endian readers ---------------------------------------------- */
120static uint16_t g16(const uint8_t *p) { return (uint16_t)(p[0] | p[1] << 8); }
121static uint32_t g32(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; }
122static uint64_t g64(const uint8_t *p) { return (uint64_t)g32(p) | (uint64_t)g32(p + 4) << 32; }
123static int all_zero(const uint8_t *p, size_t n) { while (n--) if (*p++) return 0; return 1; }
124
125/* ---- registries (spec sections 4 and 5) --------------------------------- */
126#define HDR_SIZE 48u
127#define REC_HDR_SIZE 56u
128#define MAX_IDENT 65536u
129#define MAX_ANNOT 65536u
130#define MAX_CONTENT 4096u
131#define T_END 0xFFFFu
132
133static const char *subsys_name(uint16_t s) {
134 static const char *n[] = { "transcript", "omega-world", "omega-cortex", "omega-jspace",
135 "aienos-kernel", "aienos-argus", "aienos-store", "sovcore-runtime",
136 "sovcore-scheduler", "sovcore-kv", "external" };
137 return s < sizeof n / sizeof n[0] ? n[s] : NULL;
138}
139
140/* Fixed identity length per record type; -1 = variable (rule in code), 0 = unknown type.
141 * v02 is nonzero when the file declares wire schema 3 (contract 0.2 registry). */
142#define SCHEMA_V01 1u
143#define SCHEMA_V02 3u
144static long ident_rule(uint16_t t, int v02) {
145 switch (t) {
146 case 1: return 32; /* SCHED_DECISION */
147 case 2: return 24; /* BRANCH_CREATE */
148 case 3: return 24; /* BRANCH_DESTROY */
149 case 4: return 40; /* RNG_SEED */
150 case 5: return 24; /* RNG_DRAW */
151 case 6: return 48; /* ARTIFACT_DIGEST */
152 case 7: return 32; /* CAP_TRANSITION */
153 case 8: return 56; /* CORTEX_READ */
154 case 9: return 56; /* EFFECT_INTENT */
155 case 10: return 48; /* EFFECT_COMMIT */
156 case 11: return -1; /* EXTERNAL_INPUT: 48 + content (0..4096) */
157 case 12: return 16; /* CLOCK_READ */
158 case 13: return 48; /* CRASH_BOUNDARY */
159 case 14: return 40; /* CHECKPOINT */
160 case 15: return -1; /* RX_CRUMB: 32 + canonical crumb bytes */
161 case 16: return 200; /* ARGUS_EVENT */
162 case 17: return v02 ? 80 : 0; /* CAUSE (0.2) */
163 case 18: return v02 ? 40 : 0; /* RECEIPT_BIND (0.2) */
164 case 19: return v02 ? 48 : 0; /* RUN_LINK (0.2) */
165 /* 20 TRAIN_DISPATCH: number reserved, layout not fixed in 0.2.0: unknown. */
166 case 21: return v02 ? 96 : 0; /* RESOURCE (0.2) */
167 case T_END: return 16;
168 default: return 0;
169 }
170}
171
172/* RX_CRUMB canonical bytes: the exact preimage omega hashes after the
173 * "AIEN_RX_CAUSAL_V1" tag (omega src/runtime/rx_world.c crumb_hash).
174 * Returns 1 if the bytes parse exactly, 0 otherwise. */
175static int crumb_shape(const uint8_t *c, size_t n) {
176 size_t o = 0;
177 uint64_t id;
178 uint32_t k;
179#define NEED(x) do { if (n - o < (size_t)(x)) return 0; } while (0)
180 NEED(8 + 4 + 4 + 4 + 8 + 8); id = g64(c); o += 36;
181 NEED(4); k = g32(c + o); o += 4; if (k > 8) return 0; NEED((size_t)k * 24); o += (size_t)k * 24;
182 NEED(4); k = g32(c + o); o += 4; if (k > 8) return 0; NEED((size_t)k * 16); o += (size_t)k * 16;
183 NEED(4); k = g32(c + o); o += 4; if (k > 8) return 0; NEED((size_t)k * 24); o += (size_t)k * 24;
184 NEED(4); o += 4; /* reason */
185 NEED(4); k = g32(c + o); o += 4; if (k > 65) return 0;
186 for (uint32_t i = 0; i < k; i++) {
187 uint64_t p;
188 NEED(8); p = g64(c + o); o += 8;
189 if (p >= 1 && p < id) { NEED(32); o += 32; }
190 }
191#undef NEED
192 return o == n;
193}
194
195/* ---- transcript walk ---------------------------------------------------- */
196#define MAX_RECS 4096u
197typedef struct {
198 int code; /* R_* */
199 uint64_t event; /* failing record position, 0 = header */
200 uint64_t records; /* on accept: record count including END */
201 uint8_t run_id[32];
202 uint8_t final_digest[32];
203 uint8_t (*cmp)[32]; /* compared digest per record (index 0 = record 1) */
204 uint16_t *subsys;
205} Verdict;
206
207static int refuse(Verdict *v, int code, uint64_t ev) { v->code = code; v->event = ev; return code; }
208
209static int verify(const uint8_t *b, size_t n, Verdict *v) {
210 size_t o = 0;
211 uint8_t prev[32], argus_after[32];
212 int have_argus = 0, v02 = 0;
213 uint64_t seq = 0;
214
215 memset(v, 0, sizeof *v);
216 v->cmp = calloc(MAX_RECS, 32);
217 v->subsys = calloc(MAX_RECS, sizeof *v->subsys);
218 if (!v->cmp || !v->subsys) { fprintf(stderr, "out of memory\n"); exit(2); }
219
220 /* Header, check order 1..5. */
221 if (n < 4) return refuse(v, R_LENGTH, 0);
222 if (CHECK(CHK_MAGIC) && memcmp(b, "TRN1", 4) != 0) return refuse(v, R_MAGIC, 0);
223 if (n < 6) return refuse(v, R_LENGTH, 0);
224 if (CHECK(CHK_VERSION) && g16(b + 4) != SCHEMA_V01 && g16(b + 4) != SCHEMA_V02) return refuse(v, R_VERSION, 0);
225 /* With the gate disabled (mutant) a schema 1 file also gets the 0.2 types. */
226 v02 = g16(b + 4) == SCHEMA_V02 || !CHECK(CHK_SCHEMA_GATE);
227 if (n < HDR_SIZE) return refuse(v, R_LENGTH, 0);
228 if (CHECK(CHK_HDR_FLAGS) && g16(b + 6) != 0) return refuse(v, R_NONCANONICAL, 0);
229 if (CHECK(CHK_PRODUCER) && (g16(b + 40) == 0 || !subsys_name(g16(b + 40)))) return refuse(v, R_UNKNOWN, 0);
230 if (CHECK(CHK_HDR_RESERVED) && !all_zero(b + 42, 6)) return refuse(v, R_NONCANONICAL, 0);
231 memcpy(v->run_id, b + 8, 32);
232 sha2(b, HDR_SIZE, NULL, 0, prev);
233 o = HDR_SIZE;
234
235 for (;;) {
236 const uint8_t *r, *id;
237 uint16_t type, sub;
238 uint32_t ilen, alen;
239 long rule;
240 uint64_t pos = seq + 1;
241
242 /* 1. record header present */
243 if (o == n) return refuse(v, CHECK(CHK_TRUNCATED) ? R_TRUNCATED : R_OK, pos);
244 if (n - o < REC_HDR_SIZE) return refuse(v, R_LENGTH, pos);
245 if (pos > MAX_RECS) return refuse(v, R_LENGTH, pos);
246 r = b + o;
247 type = g16(r); sub = g16(r + 2); ilen = g32(r + 4); alen = g32(r + 8);
248 /* 2. known type and subsystem */
249 rule = ident_rule(type, v02);
250 if (CHECK(CHK_TYPE) && rule == 0) return refuse(v, R_UNKNOWN, pos);
251 if (CHECK(CHK_SUBSYS) && !subsys_name(sub)) return refuse(v, R_UNKNOWN, pos);
252 /* 3. reserved */
253 if (CHECK(CHK_REC_RESERVED) && g32(r + 12) != 0) return refuse(v, R_NONCANONICAL, pos);
254 /* 4. length limits */
255 if (CHECK(CHK_LIMIT) && (ilen > MAX_IDENT || alen > MAX_ANNOT)) return refuse(v, R_LENGTH, pos);
256 /* 5. sequence */
257 if (CHECK(CHK_SEQ) && g64(r + 16) != pos) return refuse(v, R_GAP, pos);
258 /* 6. chain */
259 if (CHECK(CHK_PREV) && memcmp(r + 24, prev, 32) != 0) return refuse(v, R_CHAIN, pos);
260 /* 7. body present */
261 if ((uint64_t)(n - o - REC_HDR_SIZE) < (uint64_t)ilen + alen) return refuse(v, R_LENGTH, pos);
262 id = r + REC_HDR_SIZE;
263
264 /* 8. shape, then reserved fields, then embedded digests */
265 if (CHECK(CHK_IDENT_LEN)) {
266 if (rule > 0 && ilen != (uint32_t)rule) return refuse(v, R_SHAPE, pos);
267 if (type == 11 && (ilen < 48 || ilen > 48 + MAX_CONTENT)) return refuse(v, R_SHAPE, pos);
268 if (type == 15 && ilen < 32) return refuse(v, R_SHAPE, pos);
269 }
270 if (CHECK(CHK_END_SUBSYS) && ((type == T_END) != (sub == 0))) return refuse(v, R_SHAPE, pos);
271 if (CHECK(CHK_CAP_OP) && type == 7 && (id[4] < 1 || id[4] > 6)) return refuse(v, R_SHAPE, pos);
272 if (CHECK(CHK_CRASH_SEQ) && type == 13 && g64(id + 8) >= pos) return refuse(v, R_SHAPE, pos);
273 if (CHECK(CHK_CRUMB_SHAPE) && type == 15 && ilen >= 32 && !crumb_shape(id + 32, ilen - 32)) return refuse(v, R_SHAPE, pos);
274 if (CHECK(CHK_ARGUS_FLAG) && type == 16 && id[128] > 1) return refuse(v, R_SHAPE, pos);
275 if (CHECK(CHK_END_ANNOT) && type == T_END && alen != 0) return refuse(v, R_SHAPE, pos);
276 /* contract 0.2.0 shape rules (section 5.5); types 17..21 only reach
277 * here under schema 3 (or with the schema gate mutant). */
278 if (type == 17) {
279 uint32_t mode = g32(id + 32), kind = g32(id + 36);
280 if (CHECK(CHK_CAUSE_MODE) && (mode < 1 || mode > 3)) return refuse(v, R_SHAPE, pos);
281 if (CHECK(CHK_CAUSE_KIND) && mode != 3 && (kind < 1 || kind > 4)) return refuse(v, R_SHAPE, pos);
282 /* REF: local_kind and local are 0 exactly (arch#102 Q3). */
283 if (CHECK(CHK_REF_KIND) && mode == 3 && kind != 0) return refuse(v, R_SHAPE, pos);
284 if (CHECK(CHK_CAUSE_LOCAL) && mode != 3 && g64(id + 40) == 0) return refuse(v, R_SHAPE, pos);
285 if (CHECK(CHK_REF_LOCAL) && mode == 3 && g64(id + 40) != 0) return refuse(v, R_SHAPE, pos);
286 }
287 if (CHECK(CHK_RBIND_FORMAT) && type == 18 && (g32(id + 32) < 1 || g32(id + 32) > 3)) return refuse(v, R_SHAPE, pos);
288 if (CHECK(CHK_RLINK_REL) && type == 19 && g32(id) != 1) return refuse(v, R_SHAPE, pos);
289 if (CHECK(CHK_RLINK_FIRST) && type == 19 && pos != 1) return refuse(v, R_SHAPE, pos);
290 if (type == 21) {
291 uint8_t op = id[0], why = id[1];
292 if (CHECK(CHK_RES_OP) && (op < 1 || op > 6)) return refuse(v, R_SHAPE, pos);
293 if (CHECK(CHK_RES_REASON) && (op == 3 ? (why < 1 || why > 4) : op == 4 ? (why < 1 || why > 3) : why != 0))
294 return refuse(v, R_SHAPE, pos);
295 if (CHECK(CHK_RES_FIELD) && g16(id + 2) > 10) return refuse(v, R_SHAPE, pos);
296 }
297 if (CHECK(CHK_BODY_RESERVED)) {
298 int bad = 0;
299 switch (type) {
300 case 3: bad = g32(id + 20) != 0; break;
301 case 6: bad = g32(id + 4) != 0; break;
302 case 7: bad = !all_zero(id + 5, 3); break;
303 case 10: bad = g32(id + 12) != 0; break;
304 case 11: bad = g32(id + 4) != 0; break;
305 case 12: bad = g32(id + 4) != 0; break;
306 case 14: bad = g32(id + 4) != 0; break;
307 case 16: bad = !all_zero(id + 129, 7); break;
308 case T_END: bad = g64(id + 8) != 0; break;
309 }
310 if (bad) return refuse(v, R_NONCANONICAL, pos);
311 }
312 /* contract 0.2.0 reserved and must-be-zero fields (section 5.5) */
313 if (CHECK(CHK_CAUSE_ROOT) && type == 17 && g32(id + 32) != 1 && !all_zero(id + 48, 32)) return refuse(v, R_NONCANONICAL, pos);
314 if (CHECK(CHK_RBIND_RESERVED) && type == 18 && g32(id + 36) != 0) return refuse(v, R_NONCANONICAL, pos);
315 if (CHECK(CHK_RLINK_RESERVED) && type == 19 && g32(id + 4) != 0) return refuse(v, R_NONCANONICAL, pos);
316 if (CHECK(CHK_RES_RESERVED) && type == 21 && g32(id + 4) != 0) return refuse(v, R_NONCANONICAL, pos);
317 if (type == 15 && ilen >= 32) {
318 uint8_t d[32];
319 sha2("AIEN_RX_CAUSAL_V1", 17, id + 32, ilen - 32, d);
320 if (CHECK(CHK_CRUMB_DIGEST) && memcmp(d, id, 32) != 0) return refuse(v, R_DIGEST, pos);
321 }
322 if (type == 11 && ilen > 48) {
323 uint8_t d[32];
324 sha2(id + 48, ilen - 48, NULL, 0, d);
325 if (CHECK(CHK_INPUT_DIGEST) && memcmp(d, id + 16, 32) != 0) return refuse(v, R_DIGEST, pos);
326 }
327 if (type == 16) {
328 const uint8_t *before = id + 136, *after = id + 168;
329 if (CHECK(CHK_ARGUS_CONT) && have_argus && memcmp(before, argus_after, 32) != 0)
330 return refuse(v, R_DIGEST, pos);
331 if (CHECK(CHK_ARGUS_LINK)) {
332 uint8_t d[32];
333 if (id[128]) sha2(before, 32, id, 128, d); else memcpy(d, before, 32);
334 if (memcmp(d, after, 32) != 0) return refuse(v, R_DIGEST, pos);
335 }
336 memcpy(argus_after, after, 32);
337 have_argus = 1;
338 }
339
340 /* record digest (chain) and compared digest (replay) */
341 sha2(r, REC_HDR_SIZE + (size_t)ilen + alen, NULL, 0, prev);
342 {
343 uint8_t h[16];
344 sha256 s;
345 memcpy(h, r, 4); /* type, subsystem */
346 memcpy(h + 4, r + 16, 8); /* seq */
347 memcpy(h + 12, r + 4, 4); /* ident_len */
348 sha_init(&s);
349 sha_update(&s, "AIEN_TRN1_CMP", 13);
350 sha_update(&s, h, 16);
351 sha_update(&s, id, ilen);
352 sha_final(&s, v->cmp[seq]);
353 v->subsys[seq] = sub;
354 }
355 o += REC_HDR_SIZE + (size_t)ilen + alen;
356 seq = pos;
357
358 if (type == T_END) {
359 if (CHECK(CHK_END_COUNT) && g64(id) != pos) return refuse(v, R_GAP, pos);
360 if (CHECK(CHK_TRAILING) && o != n) return refuse(v, R_LENGTH, pos + 1);
361 v->records = pos;
362 memcpy(v->final_digest, prev, 32);
363 return R_OK;
364 }
365 }
366}
367
368/* ---- output -------------------------------------------------------------- */
369static void hex(char *out, const uint8_t *d) {
370 for (int i = 0; i < 32; i++) sprintf(out + 2 * i, "%02x", d[i]);
371}
372
373static void verdict_line(const Verdict *v, char *buf, size_t cap) {
374 if (v->code == R_OK) {
375 char rh[65], fh[65];
376 hex(rh, v->run_id); hex(fh, v->final_digest);
377 snprintf(buf, cap, "ok records=%llu run=%s final=%s", (unsigned long long)v->records, rh, fh);
378 } else {
379 snprintf(buf, cap, "refuse %d event=%llu", v->code, (unsigned long long)v->event);
380 }
381}
382
383static int compare_line(const Verdict *e, const Verdict *a, char *buf, size_t cap) {
384 if (e->code) { snprintf(buf, cap, "refuse expected %d event=%llu", e->code, (unsigned long long)e->event); return 1; }
385 if (a->code) { snprintf(buf, cap, "refuse actual %d event=%llu", a->code, (unsigned long long)a->event); return 1; }
386 uint64_t m = e->records < a->records ? e->records : a->records;
387 for (uint64_t i = 0; i < m; i++) {
388 if (memcmp(e->cmp[i], a->cmp[i], 32) != 0) {
389 char eh[65], ah[65];
390 hex(eh, e->cmp[i]); hex(ah, a->cmp[i]);
391 snprintf(buf, cap, "DIVERGENCE event=%llu expected=%s actual=%s subsystem=%s",
392 (unsigned long long)(i + 1), eh, ah, subsys_name(e->subsys[i]));
393 return 1;
394 }
395 }
396 /* Both end in END at the same position with equal compared digests, so the
397 * counts are equal here (END's identity carries the count). */
398 snprintf(buf, cap, "MATCH through %llu", (unsigned long long)m);
399 return 0;
400}
401
402static uint8_t *slurp(const char *path, size_t *n) {
403 FILE *f = fopen(path, "rb");
404 uint8_t *p = NULL;
405 size_t cap = 0, len = 0, k;
406 if (!f) return NULL;
407 do {
408 if (len == cap) {
409 uint8_t *q = realloc(p, cap = cap ? cap * 2 : 4096);
410 if (!q) { free(p); fclose(f); return NULL; }
411 p = q;
412 }
413 k = fread(p + len, 1, cap - len, f);
414 len += k;
415 } while (k);
416 fclose(f);
417 *n = len;
418 return p ? p : calloc(1, 1);
419}
420
421static int load(const char *path, Verdict *v) {
422 size_t n;
423 uint8_t *b = slurp(path, &n);
424 if (!b) { fprintf(stderr, "cannot read %s\n", path); exit(2); }
425 verify(b, n, v);
426 free(b);
427 return v->code;
428}
429static void drop(Verdict *v) { free(v->cmp); free(v->subsys); }
430
431static void rstrip(char *s) { size_t n = strlen(s); while (n && (s[n-1] == '\n' || s[n-1] == '\r')) s[--n] = 0; }
432
433static int corpus(const char *dir) {
434 char path[4096], line[8192], got[512];
435 unsigned pass = 0, fail = 0;
436 FILE *f;
437
438 snprintf(path, sizeof path, "%s/expected.txt", dir);
439 if (!(f = fopen(path, "r"))) { fprintf(stderr, "cannot read %s\n", path); return 2; }
440 while (fgets(line, sizeof line, f)) {
441 char *sp;
442 Verdict v;
443 rstrip(line);
444 if (!line[0] || line[0] == '#') continue;
445 if (!(sp = strchr(line, ' '))) { fprintf(stderr, "bad line: %s\n", line); fail++; continue; }
446 *sp = 0;
447 snprintf(path, sizeof path, "%s/%s", dir, line);
448 load(path, &v);
449 verdict_line(&v, got, sizeof got);
450 drop(&v);
451 if (strcmp(got, sp + 1) == 0) pass++;
452 else { fail++; printf("MISMATCH %s\n expected: %s\n got: %s\n", line, sp + 1, got); }
453 }
454 fclose(f);
455
456 snprintf(path, sizeof path, "%s/compare.txt", dir);
457 if (!(f = fopen(path, "r"))) { fprintf(stderr, "cannot read %s\n", path); return 2; }
458 while (fgets(line, sizeof line, f)) {
459 char *s1, *s2, p2[4096];
460 Verdict e, a;
461 rstrip(line);
462 if (!line[0] || line[0] == '#') continue;
463 if (!(s1 = strchr(line, ' ')) || !(s2 = strchr(s1 + 1, ' '))) { fprintf(stderr, "bad line: %s\n", line); fail++; continue; }
464 *s1 = 0; *s2 = 0;
465 snprintf(path, sizeof path, "%s/%s", dir, line);
466 snprintf(p2, sizeof p2, "%s/%s", dir, s1 + 1);
467 load(path, &e); load(p2, &a);
468 compare_line(&e, &a, got, sizeof got);
469 drop(&e); drop(&a);
470 if (strcmp(got, s2 + 1) == 0) pass++;
471 else { fail++; printf("MISMATCH %s %s\n expected: %s\n got: %s\n", line, s1 + 1, s2 + 1, got); }
472 }
473 fclose(f);
474 printf("TRN1_CONFORMANCE impl=c pass=%u fail=%u\n", pass, fail);
475 return fail || !pass ? 1 : 0;
476}
477
478int main(int argc, char **argv) {
479 char out[512];
480 if (argc == 2 && argv[1][0] != '-') {
481 Verdict v;
482 int rc = load(argv[1], &v);
483 verdict_line(&v, out, sizeof out);
484 puts(out);
485 drop(&v);
486 return rc ? 1 : 0;
487 }
488 if (argc == 4 && strcmp(argv[1], "--compare") == 0) {
489 Verdict e, a;
490 int rc;
491 load(argv[2], &e); load(argv[3], &a);
492 rc = compare_line(&e, &a, out, sizeof out);
493 puts(out);
494 drop(&e); drop(&a);
495 return rc;
496 }
497 if (argc == 3 && strcmp(argv[1], "--corpus") == 0) return corpus(argv[2]);
498 fprintf(stderr, "usage: trn1_verify FILE | --compare EXP ACT | --corpus DIR\n");
499 return 2;
500}