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 / verified-crumb / tools / vc1-check.c

415 lines · 17602 bytes Raw Blame History
1/*
2 * vc1-check.c: reference encoder, decoder and id computer for VerifiedCrumbV1
3 * (specs/verified-crumb/SPEC.md).
4 *
5 * Self-contained C99, libc only. The SHA-256 below is copied from
6 * specs/execution-transcript/tools/trn1_verify.c lines 53 to 117 (FIPS 180-4).
7 *
8 * vc1-check FILE.hex print "ACCEPT <vc id>" or "REFUSE <CODE> @<offset>"
9 * vc1-check --corpus DIR check DIR/expected.txt against DIR/<name>.hex,
10 * print "VC1_CONFORMANCE impl=c pass=N fail=M"
11 *
12 * Exit: 0 accept/all pass, 1 refuse/any fail, 2 usage or I/O error.
13 * Compile with -DVC1_MUTANT=N (1..9) to build a deliberately broken checker;
14 * tools/check-golden.sh proves the corpus rejects every mutant.
15 */
16#include <stdint.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20
21#ifndef VC1_MUTANT
22#define VC1_MUTANT 0
23#endif
24
25/* ---- SHA-256 (copy of trn1_verify.c) ------------------------------------- */
26typedef struct { uint32_t h[8]; uint64_t n; uint8_t b[64]; uint32_t used; } sha256;
27
28static const uint32_t K256[64] = {
29 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
30 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
31 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
32 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
33 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
34 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
35 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
36 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
37};
38#define ROR(x, r) (((x) >> (r)) | ((x) << (32 - (r))))
39
40static void sha_block(sha256 *s, const uint8_t *p) {
41 uint32_t w[64], a, b, c, d, e, f, g, h;
42 for (int i = 0; i < 16; i++)
43 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];
44 for (int i = 16; i < 64; i++) {
45 uint32_t s0 = ROR(w[i-15], 7) ^ ROR(w[i-15], 18) ^ (w[i-15] >> 3);
46 uint32_t s1 = ROR(w[i-2], 17) ^ ROR(w[i-2], 19) ^ (w[i-2] >> 10);
47 w[i] = w[i-16] + s0 + w[i-7] + s1;
48 }
49 a = s->h[0]; b = s->h[1]; c = s->h[2]; d = s->h[3];
50 e = s->h[4]; f = s->h[5]; g = s->h[6]; h = s->h[7];
51 for (int i = 0; i < 64; i++) {
52 uint32_t t1 = h + (ROR(e, 6) ^ ROR(e, 11) ^ ROR(e, 25)) + ((e & f) ^ (~e & g)) + K256[i] + w[i];
53 uint32_t t2 = (ROR(a, 2) ^ ROR(a, 13) ^ ROR(a, 22)) + ((a & b) ^ (a & c) ^ (b & c));
54 h = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2;
55 }
56 s->h[0] += a; s->h[1] += b; s->h[2] += c; s->h[3] += d;
57 s->h[4] += e; s->h[5] += f; s->h[6] += g; s->h[7] += h;
58}
59static void sha_init(sha256 *s) {
60 static const uint32_t iv[8] = { 0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,
61 0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19 };
62 memcpy(s->h, iv, sizeof iv); s->n = 0; s->used = 0;
63}
64static void sha_update(sha256 *s, const void *data, size_t len) {
65 const uint8_t *p = data;
66 s->n += len;
67 while (len) {
68 uint32_t k = 64 - s->used;
69 if (k > len) k = (uint32_t)len;
70 memcpy(s->b + s->used, p, k);
71 s->used += k; p += k; len -= k;
72 if (s->used == 64) { sha_block(s, s->b); s->used = 0; }
73 }
74}
75static void sha_final(sha256 *s, uint8_t out[32]) {
76 uint64_t bits = s->n * 8;
77 uint8_t pad = 0x80, z = 0, l[8];
78 sha_update(s, &pad, 1);
79 while (s->used != 56) sha_update(s, &z, 1);
80 for (int i = 0; i < 8; i++) l[i] = (uint8_t)(bits >> (56 - 8 * i));
81 sha_update(s, l, 8);
82 for (int i = 0; i < 8; i++) {
83 out[4*i] = (uint8_t)(s->h[i] >> 24); out[4*i+1] = (uint8_t)(s->h[i] >> 16);
84 out[4*i+2] = (uint8_t)(s->h[i] >> 8); out[4*i+3] = (uint8_t)s->h[i];
85 }
86}
87static void sha2(const void *a, size_t na, uint8_t out[32]) {
88 sha256 s; sha_init(&s); sha_update(&s, a, na); sha_final(&s, out);
89}
90
91/* ---- VC1 model ----------------------------------------------------------- */
92#define TAG "AIEN_VERIFIED_CRUMB_V1"
93#define TAG_LEN 22
94#define VC_VERSION 1u
95#define VC_MAX 256 /* max entries per list (SPEC section 3.6) */
96#define VC_STR_MAX 256 /* max bytes per string */
97
98enum code {
99 OK = 0,
100 /* resolve-level names, also produced at format level */
101 MISSING_RECEIPT = 1, DEPENDENCY_CYCLE = 2,
102 /* format-level */
103 BAD_DOMAIN_TAG = 20, UNKNOWN_FORMAT_VERSION, TRUNCATED, TRAILING_BYTES,
104 DUPLICATE_DEPENDENCY, UNSORTED_DEPENDENCIES, NONCANONICAL_SET, BAD_STRING,
105 ZERO_ID, TOO_MANY_ENTRIES, BAD_DIGEST_KIND
106};
107static const char *code_name(int c) {
108 switch (c) {
109 case MISSING_RECEIPT: return "MISSING_RECEIPT";
110 case DEPENDENCY_CYCLE: return "DEPENDENCY_CYCLE";
111 case BAD_DOMAIN_TAG: return "BAD_DOMAIN_TAG";
112 case UNKNOWN_FORMAT_VERSION: return "UNKNOWN_FORMAT_VERSION";
113 case TRUNCATED: return "TRUNCATED";
114 case TRAILING_BYTES: return "TRAILING_BYTES";
115 case DUPLICATE_DEPENDENCY: return "DUPLICATE_DEPENDENCY";
116 case UNSORTED_DEPENDENCIES: return "UNSORTED_DEPENDENCIES";
117 case NONCANONICAL_SET: return "NONCANONICAL_SET";
118 case BAD_STRING: return "BAD_STRING";
119 case ZERO_ID: return "ZERO_ID";
120 case TOO_MANY_ENTRIES: return "TOO_MANY_ENTRIES";
121 case BAD_DIGEST_KIND: return "BAD_DIGEST_KIND";
122 }
123 return "?";
124}
125
126typedef struct { uint8_t semantic_id[32], required_contract[32]; } dep_t;
127typedef struct { char s[VC_STR_MAX + 1]; } str_t;
128typedef struct {
129 uint32_t format_version;
130 uint8_t semantic_id[32], contract_id[32], digest_kind, source_or_ir_digest[32];
131 uint32_t n_real; uint8_t realization_ids[VC_MAX][32];
132 uint32_t n_dep; dep_t deps[VC_MAX];
133 uint8_t receipt_id[32]; str_t verifier_profile, verifier_version; uint8_t evidence_root[32];
134 uint32_t n_exp; str_t exports[VC_MAX];
135 uint32_t n_cap; str_t capabilities[VC_MAX];
136} vc_t;
137
138static int is_zero(const uint8_t *p) {
139 uint8_t acc = 0;
140 for (int i = 0; i < 32; i++) acc |= p[i];
141 return acc == 0;
142}
143
144/* ---- decoder ------------------------------------------------------------- */
145typedef struct { const uint8_t *b; size_t n, pos; int err; size_t err_at; } cur_t;
146
147static int fail(cur_t *c, int code) {
148 if (!c->err) { c->err = code; c->err_at = c->pos; }
149 return code;
150}
151static int need(cur_t *c, size_t k) {
152 if (c->err) return 0;
153 if (c->n - c->pos < k) { fail(c, TRUNCATED); return 0; }
154 return 1;
155}
156static uint32_t rd32(cur_t *c) {
157 if (!need(c, 4)) return 0;
158 const uint8_t *p = c->b + c->pos; c->pos += 4;
159 return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3];
160}
161static uint64_t rd64(cur_t *c) {
162 uint64_t hi = rd32(c), lo = rd32(c);
163 return hi << 32 | lo;
164}
165static void rd_id(cur_t *c, uint8_t out[32]) {
166 if (!need(c, 32)) { memset(out, 0, 32); return; }
167 memcpy(out, c->b + c->pos, 32); c->pos += 32;
168}
169static void rd_str(cur_t *c, str_t *out) {
170 uint64_t len = rd64(c);
171 out->s[0] = 0;
172 if (c->err) return;
173 if (len == 0 || len > VC_STR_MAX) { fail(c, BAD_STRING); return; }
174 if (!need(c, (size_t)len)) return;
175 for (uint64_t i = 0; i < len; i++) {
176 uint8_t ch = c->b[c->pos + i];
177#if VC1_MUTANT == 7
178 if (0) {
179#else
180 if (ch < 0x21 || ch > 0x7e) {
181#endif
182 c->pos += (size_t)i; fail(c, BAD_STRING); return; }
183 }
184 memcpy(out->s, c->b + c->pos, (size_t)len); out->s[len] = 0; c->pos += (size_t)len;
185}
186static uint32_t rd_count(cur_t *c) {
187 uint32_t n = rd32(c);
188#if VC1_MUTANT != 8
189 if (!c->err && n > VC_MAX) { fail(c, TOO_MANY_ENTRIES); return 0; }
190#endif
191 return n;
192}
193
194/* Field order is the refusal order; the first failure wins. */
195static int decode(const uint8_t *b, size_t n, vc_t *v, size_t *err_at) {
196 cur_t c = { b, n, 0, 0, 0 };
197 memset(v, 0, sizeof *v);
198 if (!need(&c, TAG_LEN)) goto done;
199 if (memcmp(b, TAG, TAG_LEN) != 0) { fail(&c, BAD_DOMAIN_TAG); goto done; }
200 c.pos = TAG_LEN;
201 v->format_version = rd32(&c);
202 if (!c.err && v->format_version != VC_VERSION) { c.pos -= 4; fail(&c, UNKNOWN_FORMAT_VERSION); c.pos += 4; }
203 rd_id(&c, v->semantic_id); rd_id(&c, v->contract_id);
204 if (need(&c, 1)) {
205 v->digest_kind = c.b[c.pos];
206#if VC1_MUTANT != 9
207 if (v->digest_kind != 1 && v->digest_kind != 2) fail(&c, BAD_DIGEST_KIND);
208#endif
209 c.pos++;
210 }
211 rd_id(&c, v->source_or_ir_digest);
212#if VC1_MUTANT != 6
213 if (!c.err && (is_zero(v->semantic_id) || is_zero(v->contract_id) || is_zero(v->source_or_ir_digest)))
214 fail(&c, ZERO_ID);
215#endif
216 v->n_real = rd_count(&c);
217 for (uint32_t i = 0; i < v->n_real && !c.err; i++) {
218 rd_id(&c, v->realization_ids[i]);
219 if (!c.err && i > 0 && memcmp(v->realization_ids[i - 1], v->realization_ids[i], 32) >= 0)
220 fail(&c, NONCANONICAL_SET);
221 }
222 v->n_dep = rd_count(&c);
223 for (uint32_t i = 0; i < v->n_dep && !c.err; i++) {
224 rd_id(&c, v->deps[i].semantic_id); rd_id(&c, v->deps[i].required_contract);
225 if (c.err) break;
226 if (memcmp(v->deps[i].semantic_id, v->semantic_id, 32) == 0) { fail(&c, DEPENDENCY_CYCLE); break; }
227 if (is_zero(v->deps[i].semantic_id) || is_zero(v->deps[i].required_contract)) { fail(&c, ZERO_ID); break; }
228 if (i > 0) {
229 int cmp = memcmp(v->deps[i - 1].semantic_id, v->deps[i].semantic_id, 32);
230#if VC1_MUTANT != 4
231 if (cmp == 0) { fail(&c, DUPLICATE_DEPENDENCY); break; }
232#endif
233#if VC1_MUTANT != 1
234 if (cmp > 0) { fail(&c, UNSORTED_DEPENDENCIES); break; }
235#endif
236 }
237 }
238 rd_id(&c, v->receipt_id);
239#if VC1_MUTANT != 2
240 if (!c.err && is_zero(v->receipt_id)) fail(&c, MISSING_RECEIPT);
241#endif
242 rd_str(&c, &v->verifier_profile); rd_str(&c, &v->verifier_version);
243 rd_id(&c, v->evidence_root);
244 if (!c.err && is_zero(v->evidence_root)) fail(&c, ZERO_ID);
245 v->n_exp = rd_count(&c);
246 for (uint32_t i = 0; i < v->n_exp && !c.err; i++) {
247 rd_str(&c, &v->exports[i]);
248 if (!c.err && i > 0 && strcmp(v->exports[i - 1].s, v->exports[i].s) >= 0) fail(&c, NONCANONICAL_SET);
249 }
250 v->n_cap = rd_count(&c);
251 for (uint32_t i = 0; i < v->n_cap && !c.err; i++) {
252 rd_str(&c, &v->capabilities[i]);
253 if (!c.err && i > 0 && strcmp(v->capabilities[i - 1].s, v->capabilities[i].s) >= 0) fail(&c, NONCANONICAL_SET);
254 }
255#if VC1_MUTANT != 5
256 if (!c.err && c.pos != c.n) fail(&c, TRAILING_BYTES);
257#endif
258done:
259 *err_at = c.err_at;
260 return c.err;
261}
262
263/* ---- encoder ------------------------------------------------------------- */
264typedef struct { uint8_t *p; size_t n, cap; } buf_t;
265static void put(buf_t *o, const void *d, size_t k) {
266 if (o->n + k > o->cap) {
267 o->cap = (o->n + k) * 2 + 64;
268 o->p = realloc(o->p, o->cap);
269 if (!o->p) { fputs("out of memory\n", stderr); exit(2); }
270 }
271 memcpy(o->p + o->n, d, k); o->n += k;
272}
273static void put32(buf_t *o, uint32_t x) {
274 uint8_t t[4] = { (uint8_t)(x >> 24), (uint8_t)(x >> 16), (uint8_t)(x >> 8), (uint8_t)x };
275 put(o, t, 4);
276}
277static void put_str(buf_t *o, const char *s) {
278 uint64_t n = strlen(s);
279 put32(o, (uint32_t)(n >> 32)); put32(o, (uint32_t)n); put(o, s, (size_t)n);
280}
281static int cmp_dep(const void *a, const void *b) { return memcmp(a, b, 32); }
282static int cmp_id(const void *a, const void *b) { return memcmp(a, b, 32); }
283static int cmp_str(const void *a, const void *b) { return strcmp(((const str_t *)a)->s, ((const str_t *)b)->s); }
284
285/* Canonical bytes, with every list sorted (the encoder owns the sort; the
286 * decoder refuses unsorted input). Duplicate dependencies are refused. */
287static int encode(vc_t *v, buf_t *o) {
288 qsort(v->realization_ids, v->n_real, 32, cmp_id);
289 qsort(v->deps, v->n_dep, sizeof(dep_t), cmp_dep);
290 qsort(v->exports, v->n_exp, sizeof(str_t), cmp_str);
291 qsort(v->capabilities, v->n_cap, sizeof(str_t), cmp_str);
292 for (uint32_t i = 1; i < v->n_dep; i++)
293 if (memcmp(v->deps[i - 1].semantic_id, v->deps[i].semantic_id, 32) == 0) return DUPLICATE_DEPENDENCY;
294 o->n = 0;
295 put(o, TAG, TAG_LEN); put32(o, v->format_version);
296 put(o, v->semantic_id, 32); put(o, v->contract_id, 32); put(o, &v->digest_kind, 1); put(o, v->source_or_ir_digest, 32);
297 put32(o, v->n_real); for (uint32_t i = 0; i < v->n_real; i++) put(o, v->realization_ids[i], 32);
298 put32(o, v->n_dep);
299 for (uint32_t i = 0; i < v->n_dep; i++) { put(o, v->deps[i].semantic_id, 32); put(o, v->deps[i].required_contract, 32); }
300 put(o, v->receipt_id, 32); put_str(o, v->verifier_profile.s); put_str(o, v->verifier_version.s);
301 put(o, v->evidence_root, 32);
302 put32(o, v->n_exp); for (uint32_t i = 0; i < v->n_exp; i++) put_str(o, v->exports[i].s);
303 put32(o, v->n_cap); for (uint32_t i = 0; i < v->n_cap; i++) put_str(o, v->capabilities[i].s);
304 return OK;
305}
306
307/* VC id = SHA-256 over the canonical bytes (which start with the domain tag). */
308static void vc_id(const uint8_t *canon, size_t n, uint8_t out[32]) {
309#if VC1_MUTANT == 3
310 sha2(canon + TAG_LEN, n - TAG_LEN, out); /* mutant: domain tag dropped from the hash */
311#else
312 sha2(canon, n, out);
313#endif
314}
315
316/* ---- I/O ----------------------------------------------------------------- */
317static int unhex(int ch) {
318 if (ch >= '0' && ch <= '9') return ch - '0';
319 if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10;
320 return -1;
321}
322/* Read a lowercase-hex file (whitespace ignored). Returns malloc'd bytes or NULL. */
323static uint8_t *read_hex(const char *path, size_t *n_out) {
324 FILE *f = fopen(path, "rb");
325 if (!f) return NULL;
326 size_t cap = 4096, n = 0; char *t = malloc(cap); int ch;
327 while ((ch = fgetc(f)) != EOF) {
328 if (ch == '\n' || ch == '\r' || ch == ' ') continue;
329 if (n + 1 > cap) { cap *= 2; t = realloc(t, cap); }
330 t[n++] = (char)ch;
331 }
332 fclose(f);
333 if (n % 2) { free(t); return NULL; }
334 uint8_t *b = malloc(n / 2 + 1);
335 for (size_t i = 0; i < n; i += 2) {
336 int hi = unhex((unsigned char)t[i]), lo = unhex((unsigned char)t[i + 1]);
337 if (hi < 0 || lo < 0) { free(t); free(b); return NULL; }
338 b[i / 2] = (uint8_t)(hi << 4 | lo);
339 }
340 free(t); *n_out = n / 2; return b;
341}
342static void hex32(const uint8_t *p, char out[65]) {
343 for (int i = 0; i < 32; i++) sprintf(out + 2 * i, "%02x", p[i]);
344}
345
346static vc_t g_vc; /* large, so static */
347
348/* Check one buffer. On accept fills id_hex; on refuse fills code and offset. */
349static int check(const uint8_t *b, size_t n, char id_hex[65], int *code, size_t *at) {
350 size_t err_at = 0;
351 int rc = decode(b, n, &g_vc, &err_at);
352 if (rc) { *code = rc; *at = err_at; return 1; }
353 /* canonical round trip: re-encoding must reproduce the input bytes */
354 buf_t o = { 0, 0, 0 };
355 vc_t *tmp = malloc(sizeof *tmp); memcpy(tmp, &g_vc, sizeof *tmp);
356 int erc = encode(tmp, &o);
357 int same = erc == OK && o.n == n && memcmp(o.p, b, n) == 0;
358 /* the encoder sorts: a reversed copy must encode to the same bytes */
359 if (same && g_vc.n_dep > 1) {
360 for (uint32_t i = 0; i < g_vc.n_dep / 2; i++) {
361 dep_t t = tmp->deps[i]; tmp->deps[i] = tmp->deps[g_vc.n_dep - 1 - i]; tmp->deps[g_vc.n_dep - 1 - i] = t;
362 }
363 buf_t o2 = { 0, 0, 0 };
364 same = encode(tmp, &o2) == OK && o2.n == o.n && memcmp(o2.p, o.p, o.n) == 0;
365 free(o2.p);
366 }
367 free(tmp); free(o.p);
368 if (!same) { *code = TRAILING_BYTES; *at = n; fputs("vc1-check: internal: encode/decode round trip differs\n", stderr); return 2; }
369 uint8_t id[32]; vc_id(b, n, id); hex32(id, id_hex);
370 return 0;
371}
372
373static int run_file(const char *path) {
374 size_t n; uint8_t *b = read_hex(path, &n);
375 if (!b) { fprintf(stderr, "vc1-check: cannot read lowercase hex file %s\n", path); return 2; }
376 char id[65]; int code = 0; size_t at = 0;
377 int r = check(b, n, id, &code, &at);
378 free(b);
379 if (r == 0) { printf("ACCEPT %s\n", id); return 0; }
380 if (r == 2) return 2;
381 printf("REFUSE %s @%zu\n", code_name(code), at);
382 return 1;
383}
384
385static int run_corpus(const char *dir) {
386 char path[1024], line[512];
387 snprintf(path, sizeof path, "%s/expected.txt", dir);
388 FILE *f = fopen(path, "r");
389 if (!f) { fprintf(stderr, "vc1-check: cannot open %s\n", path); return 2; }
390 int pass = 0, failn = 0;
391 while (fgets(line, sizeof line, f)) {
392 char name[256], verb[16], arg[128];
393 if (sscanf(line, "%255s %15s %127s", name, verb, arg) != 3) continue;
394 snprintf(path, sizeof path, "%s/%s.hex", dir, name);
395 size_t n; uint8_t *b = read_hex(path, &n);
396 char id[65] = ""; int code = 0; size_t at = 0; int ok = 0;
397 if (b) {
398 int r = check(b, n, id, &code, &at);
399 if (r == 0) ok = strcmp(verb, "ACCEPT") == 0 && strcmp(arg, id) == 0;
400 else if (r == 1) ok = strcmp(verb, "REFUSE") == 0 && strcmp(arg, code_name(code)) == 0;
401 free(b);
402 }
403 if (ok) pass++; else { failn++; printf("FAIL %s (expected %s %s)\n", name, verb, arg); }
404 }
405 fclose(f);
406 printf("VC1_CONFORMANCE impl=c pass=%d fail=%d\n", pass, failn);
407 return failn ? 1 : 0;
408}
409
410int main(int argc, char **argv) {
411 if (argc == 3 && strcmp(argv[1], "--corpus") == 0) return run_corpus(argv[2]);
412 if (argc == 2 && argv[1][0] != '-') return run_file(argv[1]);
413 fputs("usage: vc1-check FILE.hex | vc1-check --corpus DIR\n", stderr);
414 return 2;
415}