interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / checker2 / checker2_main.c

533 lines · 22160 bytes Raw Blame History
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/* checker2: the independent checker's command line (omega#446, spec/one-layer-receipts.md).
18 *
19 * checker2 check <unit.ir> judge canonical OSC IR bytes
20 * checker2 version print the checker identity
21 * checker2 prove-cnf <unit.ir> <func> <out.cnf> NO-TRAP formula of func (c2_blast.h)
22 * checker2 prove-check <unit.ir> <func> <proof> rebuild that formula, check an LRAT proof
23 * checker2 receipt issue <unit.ir> <exe> --target T --epoch E --expiry X --out-dir D
24 * [--firmware F.bin] [--requirements R] [--input I] [--producer P]
25 * [--dep ID]... [--lrat FUNC=PROOF]... (proofs checked, evidence class 2)
26 * check, sign with the TEST key, store by id
27 * checker2 receipt tbs <receipt> <out65> write the 65-byte message an owner key signs
28 * checker2 receipt attach <receipt> <sig64> --out-dir D
29 * verify an OWNER signature, store by id
30 * checker2 receipt verify <receipt> <unit.ir> <exe> --target T --epoch E
31 * [--allow-test-key | --production] [--floor N] [--firmware F.bin] [--revoked-id HEX]...
32 * [--revoked-key HEX]... [--lrat FUNC=PROOF]... (required for evidence class 2)
33 * [--producer P] [--dep ID]... [--accept-checker ID]...
34 * the receipt's checker must be this build or an --accept-checker identity;
35 * producer and dependency digests must equal the given ones (none = none)
36 * production policy by default: a TEST receipt is refused unless
37 * --allow-test-key is given (qualification and test runs only)
38 *
39 * Exit: 0 accept / done, 1 reject / refused, 2 usage or I/O error. */
40#include <errno.h>
41#include <fcntl.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <sys/stat.h>
46#include <unistd.h>
47
48#include "c2_blast.h"
49#include "c2_ir.h"
50#include "c2_lrat.h"
51#include "c2_receipt.h"
52#include "sha256.h"
53
54#define C2_CHECKER_VERSION "checker2 1 (omega#446)"
55#ifndef C2_SOURCES_SHA256
56#define C2_SOURCES_SHA256 "unset"
57#endif
58
59static void hex(const uint8_t *b, size_t n, char *out)
60{
61 static const char d[] = "0123456789abcdef";
62 for (size_t i = 0; i < n; i++) { out[2 * i] = d[b[i] >> 4]; out[2 * i + 1] = d[b[i] & 15]; }
63 out[2 * n] = 0;
64}
65
66static int unhex32(const char *s, uint8_t out[32])
67{
68 if (strlen(s) != 64) return -1;
69 for (int i = 0; i < 32; i++) {
70 unsigned v = 0;
71 for (int k = 0; k < 2; k++) {
72 char c = s[2 * i + k];
73 v <<= 4;
74 if (c >= '0' && c <= '9') v |= (unsigned)(c - '0');
75 else if (c >= 'a' && c <= 'f') v |= (unsigned)(c - 'a' + 10);
76 else return -1;
77 }
78 out[i] = (uint8_t)v;
79 }
80 return 0;
81}
82
83static uint8_t *slurp(const char *path, size_t *len)
84{
85 FILE *f = fopen(path, "rb");
86 if (!f) { fprintf(stderr, "checker2: cannot open %s: %s\n", path, strerror(errno)); return NULL; }
87 size_t cap = 1 << 16, n = 0;
88 uint8_t *b = malloc(cap);
89 while (b) {
90 size_t r = fread(b + n, 1, cap - n, f);
91 n += r;
92 if (n < cap) break;
93 if (cap >= (1u << 28)) { free(b); b = NULL; break; }
94 uint8_t *nb = realloc(b, cap * 2);
95 if (!nb) { free(b); b = NULL; break; }
96 b = nb;
97 cap *= 2;
98 }
99 int err = ferror(f);
100 fclose(f);
101 if (!b || err) { free(b); fprintf(stderr, "checker2: cannot read %s\n", path); return NULL; }
102 *len = n;
103 return b;
104}
105
106static int file_sha(const char *path, uint8_t out[32])
107{
108 size_t n;
109 uint8_t *b = slurp(path, &n);
110 if (!b) return -1;
111 sha256_hash(b, n, out);
112 free(b);
113 return 0;
114}
115
116/* identity = SHA-256(version string ‖ 0 ‖ sources digest hex) (spec section 5) */
117static void checker_id(uint8_t out[32])
118{
119 sha256_ctx c;
120 sha256_init(&c);
121 sha256_update(&c, (const uint8_t *)C2_CHECKER_VERSION, sizeof C2_CHECKER_VERSION);
122 sha256_update(&c, (const uint8_t *)C2_SOURCES_SHA256, strlen(C2_SOURCES_SHA256));
123 sha256_final(&c, out);
124}
125
126static int judge(const char *path, uint8_t ir_sha[32], int quiet)
127{
128 size_t n;
129 uint8_t *b = slurp(path, &n);
130 if (!b) return 2;
131 sha256_hash(b, n, ir_sha);
132 C2Verdict v;
133 int rc = c2_check_bytes(b, n, &v);
134 free(b);
135 char h[65];
136 hex(ir_sha, 32, h);
137 if (rc == 0) {
138 if (!quiet) printf("checker2=ACCEPT ir_sha256=%s\n", h);
139 return 0;
140 }
141 printf("checker2=REJECT rule=%s func=%d insn=%d ir_sha256=%s detail=%s\n", v.rule, v.func, v.insn, h, v.detail);
142 return 1;
143}
144
145/* create a new file with exactly these bytes; never overwrite */
146static int write_new(const char *path, const uint8_t *b, size_t n)
147{
148 int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0444);
149 if (fd < 0) { fprintf(stderr, "checker2: cannot create %s: %s\n", path, strerror(errno)); return 2; }
150 ssize_t w = write(fd, b, n);
151 if (w < 0 || (size_t)w != n || fsync(fd) != 0 || close(fd) != 0) { fprintf(stderr, "checker2: write %s failed\n", path); return 2; }
152 return 0;
153}
154
155/* write bytes to D/<id>.rcpt: exclusive create; identical existing bytes is a no-op */
156static int store(const char *dir, const uint8_t r[C2R_SIZE])
157{
158 uint8_t id[32];
159 char h[65], path[4096];
160 c2r_id(r, id);
161 hex(id, 32, h);
162 if (snprintf(path, sizeof path, "%s/%s.rcpt", dir, h) >= (int)sizeof path) return 2;
163 int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0444);
164 if (fd < 0 && errno == EEXIST) {
165 size_t n;
166 uint8_t *old = slurp(path, &n);
167 int same = old && n == C2R_SIZE && memcmp(old, r, C2R_SIZE) == 0;
168 free(old);
169 if (same) { printf("receipt=%s unchanged\n", path); return 0; }
170 fprintf(stderr, "checker2: %s exists with different bytes; receipts are never rewritten\n", path);
171 return 1;
172 }
173 if (fd < 0) { fprintf(stderr, "checker2: cannot create %s: %s\n", path, strerror(errno)); return 2; }
174 ssize_t w = write(fd, r, C2R_SIZE);
175 if (w != C2R_SIZE || fsync(fd) != 0 || close(fd) != 0) { fprintf(stderr, "checker2: write %s failed\n", path); return 2; }
176 printf("receipt=%s\n", path);
177 return 0;
178}
179
180static const char *opt(int argc, char **argv, const char *name)
181{
182 for (int i = 0; i + 1 < argc; i++)
183 if (strcmp(argv[i], name) == 0) return argv[i + 1];
184 return NULL;
185}
186
187static int flag(int argc, char **argv, const char *name)
188{
189 for (int i = 0; i < argc; i++)
190 if (strcmp(argv[i], name) == 0) return 1;
191 return 0;
192}
193
194static int opt_u64(int argc, char **argv, const char *name, uint64_t *out, int required)
195{
196 const char *s = opt(argc, argv, name);
197 if (!s) {
198 if (required) fprintf(stderr, "checker2: %s is required\n", name);
199 return required ? -1 : 0;
200 }
201 char *end;
202 errno = 0;
203 unsigned long long v = strtoull(s, &end, 10);
204 if (errno || *end || !*s || s[0] == '-') { fprintf(stderr, "checker2: bad number for %s\n", name); return -1; }
205 *out = v;
206 return 0;
207}
208
209static int opt_sha_file(int argc, char **argv, const char *name, uint8_t out[32])
210{
211 const char *s = opt(argc, argv, name);
212 memset(out, 0, 32);
213 return s ? file_sha(s, out) : 0;
214}
215
216static int property_cnf(const char *ir, const char *func, C2Cnf *cnf, C2BlastInfo *bi, uint8_t ir_sha[32]);
217
218/* Every "--lrat FUNC=PROOF" pair: rebuild FUNC's NO-TRAP formula from the IR bytes and check the
219 * proof. The requirements object (spec section 10) is
220 * "OMEGA-REQUIREMENTS-V1\n" then per pair, FUNC strictly ascending:
221 * "no-trap-except-requires <FUNC> proof_sha256=<hex>\n"
222 * Returns 0 with *count pairs and req = SHA-256 of the object (object text in obj, may be NULL),
223 * 1 if a proof is refused or the order is wrong, 2 on usage or I/O error. */
224static int lrat_requirements(int argc, char **argv, const char *ir, uint8_t req[32], int *count, char *obj, size_t objcap)
225{
226 char text[8192], prev[256] = "";
227 size_t tn = (size_t)snprintf(text, sizeof text, "OMEGA-REQUIREMENTS-V1\n");
228 *count = 0;
229 for (int i = 0; i + 1 < argc; i++) {
230 if (strcmp(argv[i], "--lrat") != 0) continue;
231 const char *arg = argv[i + 1], *eq = strchr(arg, '=');
232 if (!eq || eq == arg || (size_t)(eq - arg) >= sizeof prev) { fprintf(stderr, "checker2: --lrat wants FUNC=PROOF\n"); return 2; }
233 char func[256];
234 memcpy(func, arg, (size_t)(eq - arg));
235 func[eq - arg] = 0;
236 if (*count && strcmp(prev, func) >= 0) { fprintf(stderr, "checker2: --lrat functions must be strictly ascending\n"); return 1; }
237 snprintf(prev, sizeof prev, "%s", func);
238 C2Cnf cnf;
239 C2BlastInfo bi;
240 uint8_t s[32], ps[32];
241 int rc = property_cnf(ir, func, &cnf, &bi, s);
242 if (rc) return rc;
243 size_t n;
244 uint8_t *p = slurp(eq + 1, &n);
245 if (!p) { c2_cnf_free(&cnf); return 2; }
246 sha256_hash(p, n, ps);
247 C2LratResult r;
248 int e = c2_lrat_check(&cnf, (const char *)p, n, &r);
249 free(p);
250 c2_cnf_free(&cnf);
251 if (e) { printf("lrat=REFUSED func=%s rule=%s line=%ld\n", func, r.rule, r.line); return 1; }
252 char h[65];
253 hex(ps, 32, h);
254 int k = snprintf(text + tn, sizeof text - tn, "%s %s proof_sha256=%s\n", C2_PROPERTY_NOTRAP, func, h);
255 if (k < 0 || (size_t)k >= sizeof text - tn) { fprintf(stderr, "checker2: too many --lrat\n"); return 2; }
256 tn += (size_t)k;
257 (*count)++;
258 }
259 sha256_hash((const uint8_t *)text, tn, req);
260 if (obj) snprintf(obj, objcap, "%s", text);
261 return 0;
262}
263
264static int receipt_issue(int argc, char **argv)
265{
266 if (argc < 2) return 2;
267 const char *ir = argv[0], *exe = argv[1], *target = opt(argc, argv, "--target"), *dir = opt(argc, argv, "--out-dir");
268 const char *draft = opt(argc, argv, "--owner-draft");
269 if (!target || (!dir == !draft)) { fprintf(stderr, "checker2: --target and exactly one of --out-dir / --owner-draft are required\n"); return 2; }
270 C2Receipt r;
271 memset(&r, 0, sizeof r);
272 r.version = C2R_FORMAT_VERSION;
273 r.signer_class = C2R_CLASS_TEST;
274 r.alg = C2R_ALG_ED25519;
275 r.evidence_class = C2R_EV_IR_CHECKED;
276 int rc = judge(ir, r.program, 1);
277 if (rc) { fprintf(stderr, "checker2: refusing to issue a receipt for bytes checker2 does not accept\n"); return rc; }
278 if (file_sha(exe, r.executable)) return 2;
279 if (opt_u64(argc, argv, "--epoch", &r.policy_epoch, 1) || opt_u64(argc, argv, "--expiry", &r.expiry_epoch, 1)) return 2;
280 if (opt_sha_file(argc, argv, "--firmware", r.firmware) || opt_sha_file(argc, argv, "--requirements", r.requirements) ||
281 opt_sha_file(argc, argv, "--input", r.input) || opt_sha_file(argc, argv, "--producer", r.producer_checker))
282 return 2;
283 if (opt(argc, argv, "--lrat")) {
284 if (opt(argc, argv, "--requirements")) { fprintf(stderr, "checker2: --lrat and --requirements are exclusive\n"); return 2; }
285 int nproofs;
286 char obj[8192];
287 rc = lrat_requirements(argc, argv, ir, r.requirements, &nproofs, obj, sizeof obj);
288 if (rc) { fprintf(stderr, "checker2: refusing to issue: a proof did not check\n"); return rc; }
289 r.evidence_class = C2R_EV_IR_CHECKED_LRAT;
290 char h[65];
291 hex(r.requirements, 32, h);
292 printf("requirements_sha256=%s proofs=%d\n%s", h, nproofs, obj);
293 }
294 uint8_t deps[64][32];
295 unsigned nd = 0;
296 for (int i = 0; i + 1 < argc; i++)
297 if (strcmp(argv[i], "--dep") == 0) {
298 if (nd == 64 || unhex32(argv[i + 1], deps[nd])) { fprintf(stderr, "checker2: bad --dep\n"); return 2; }
299 nd++;
300 }
301 if (c2r_deps_digest((const uint8_t (*)[32])deps, nd, r.dependencies)) { fprintf(stderr, "checker2: --dep ids must be strictly ascending\n"); return 2; }
302 c2r_target_id(target, r.target);
303 checker_id(r.checker);
304 uint8_t buf[C2R_SIZE], seed[32];
305 if (draft) {
306 /* OWNER draft: class OWNER, key fingerprint of the owner root, signature all zero. The
307 * owner signs `receipt tbs` output on the YubiKey; `receipt attach` verifies and stores. */
308 r.signer_class = C2R_CLASS_OWNER;
309 sha256_hash(c2r_owner_root_pk, 32, r.key_fp);
310 c2r_encode(&r, buf);
311 return write_new(draft, buf, C2R_SIZE);
312 }
313 c2r_test_seed(seed);
314 int e = c2r_sign_test(&r, seed, buf);
315 if (e) { fprintf(stderr, "checker2: signing refused: %s\n", c2r_reason(e)); return 1; }
316 return store(dir, buf);
317}
318
319static int read_receipt(const char *path, uint8_t buf[C2R_SIZE], C2Receipt *r)
320{
321 size_t n;
322 uint8_t *b = slurp(path, &n);
323 if (!b) return 2;
324 int e = c2r_decode(b, n, r);
325 if (!e) memcpy(buf, b, C2R_SIZE);
326 free(b);
327 if (e) { fprintf(stderr, "checker2: not a receipt: %s\n", c2r_reason(e)); return 1; }
328 return 0;
329}
330
331static int receipt_tbs(int argc, char **argv)
332{
333 if (argc < 2) return 2;
334 uint8_t buf[C2R_SIZE], msg[C2R_SIGNED_MSG_LEN];
335 C2Receipt r;
336 int rc = read_receipt(argv[0], buf, &r);
337 if (rc) return rc;
338 c2r_signed_message(buf, msg);
339 return write_new(argv[1], msg, sizeof msg);
340}
341
342static int receipt_attach(int argc, char **argv)
343{
344 if (argc < 2) return 2;
345 const char *dir = opt(argc, argv, "--out-dir");
346 if (!dir) { fprintf(stderr, "checker2: --out-dir is required\n"); return 2; }
347 uint8_t buf[C2R_SIZE], out[C2R_SIZE];
348 C2Receipt r;
349 int rc = read_receipt(argv[0], buf, &r);
350 if (rc) return rc;
351 if (r.signer_class != C2R_CLASS_OWNER) { fprintf(stderr, "checker2: attach is for OWNER receipts\n"); return 1; }
352 size_t ns;
353 uint8_t *s = slurp(argv[1], &ns);
354 if (!s) return 2;
355 if (ns != 64) { free(s); fprintf(stderr, "checker2: signature must be 64 bytes\n"); return 2; }
356 int e = c2r_attach(&r, c2r_owner_root_pk, s, out);
357 free(s);
358 if (e) { fprintf(stderr, "checker2: attach refused: %s\n", c2r_reason(e)); return 1; }
359 return store(dir, out);
360}
361
362static int receipt_verify(int argc, char **argv)
363{
364 if (argc < 3) return 2;
365 const char *target = opt(argc, argv, "--target");
366 if (!target) { fprintf(stderr, "checker2: --target is required\n"); return 2; }
367 uint8_t tgt[32], fw[32], prog[32], exe[32], prod[32], depd[32];
368 c2r_target_id(target, tgt);
369 int rc = judge(argv[1], prog, 1);
370 if (rc == 0 && file_sha(argv[2], exe)) rc = 2;
371 if (rc == 0 && opt_sha_file(argc, argv, "--firmware", fw)) rc = 2;
372 /* producer and dependencies are always compared, like firmware: absent options mean "none"
373 * (all-zero producer, empty dependency list), exactly what `issue` writes without them */
374 if (rc == 0 && opt_sha_file(argc, argv, "--producer", prod)) rc = 2;
375 static uint8_t deps[64][32];
376 unsigned nd = 0;
377 for (int i = 0; rc == 0 && i + 1 < argc; i++)
378 if (strcmp(argv[i], "--dep") == 0) rc = nd < 64 && !unhex32(argv[i + 1], deps[nd]) ? (nd++, 0) : 2;
379 if (rc == 0 && c2r_deps_digest((const uint8_t (*)[32])deps, nd, depd)) { fprintf(stderr, "checker2: --dep ids must be strictly ascending\n"); rc = 2; }
380 C2ReceiptExpect ex = {.target = tgt, .firmware = fw, .program = prog, .executable = exe,
381 .producer_checker = prod, .dependencies = depd};
382 C2ReceiptPolicy p;
383 memset(&p, 0, sizeof p);
384 /* production is the default (fail closed): TEST receipts are refused unless the caller asks
385 * for a qualification run with --allow-test-key. --production states the default explicitly;
386 * giving both is a usage error. */
387 int allow_test = flag(argc, argv, "--allow-test-key");
388 if (allow_test && flag(argc, argv, "--production")) {
389 fprintf(stderr, "checker2: --production and --allow-test-key are exclusive\n");
390 return 2;
391 }
392 p.production = !allow_test;
393 if (!p.production) { p.test_anchors = &c2r_test_pk; p.n_test = 1; }
394 p.owner_anchors = &c2r_owner_root_pk;
395 p.n_owner = 1;
396 /* checker identities: this build's own, plus any given with --accept-checker */
397 static uint8_t checkers[16][32];
398 unsigned nchk = 1;
399 checker_id(checkers[0]);
400 for (int i = 0; rc == 0 && i + 1 < argc; i++)
401 if (strcmp(argv[i], "--accept-checker") == 0) rc = nchk < 16 && !unhex32(argv[i + 1], checkers[nchk]) ? (nchk++, 0) : 2;
402 p.checkers = (const uint8_t (*)[32])checkers;
403 p.n_checkers = nchk;
404 static uint8_t ids[16][32], keys[16][32];
405 unsigned nid = 0, nkey = 0;
406 for (int i = 0; rc == 0 && i + 1 < argc; i++) {
407 if (strcmp(argv[i], "--revoked-id") == 0) rc = nid < 16 && !unhex32(argv[i + 1], ids[nid]) ? (nid++, 0) : 2;
408 if (strcmp(argv[i], "--revoked-key") == 0) rc = nkey < 16 && !unhex32(argv[i + 1], keys[nkey]) ? (nkey++, 0) : 2;
409 }
410 p.revoked_ids = (const uint8_t (*)[32])ids;
411 p.n_revoked_ids = nid;
412 p.revoked_keys = (const uint8_t (*)[32])keys;
413 p.n_revoked_keys = nkey;
414 if (rc == 0 && (opt_u64(argc, argv, "--epoch", &p.current_epoch, 1) || opt_u64(argc, argv, "--floor", &p.epoch_floor, 0))) rc = 2;
415 if (rc) return rc;
416 size_t n;
417 uint8_t *b = slurp(argv[0], &n);
418 if (!b) return 2;
419 C2Receipt got;
420 int e = c2r_verify(b, n, &p, &ex, &got);
421 free(b);
422 if (e) { printf("receipt=%s\n", c2r_reason(e)); return 1; }
423 /* LRAT evidence is replayed, never taken on trust: the proofs must be given, must check
424 * against these IR bytes, and must hash to the receipt's requirements digest. */
425 if (got.evidence_class == C2R_EV_IR_CHECKED_LRAT || opt(argc, argv, "--lrat")) {
426 uint8_t req[32];
427 int nproofs;
428 rc = lrat_requirements(argc, argv, argv[1], req, &nproofs, NULL, 0);
429 if (rc) { printf("receipt=LRAT-REPLAY-FAILED\n"); return rc; }
430 if (got.evidence_class != C2R_EV_IR_CHECKED_LRAT) { printf("receipt=LRAT-NOT-CLAIMED\n"); return 1; }
431 if (nproofs == 0) { printf("receipt=LRAT-PROOFS-REQUIRED\n"); return 1; }
432 if (memcmp(req, got.requirements, 32)) { printf("receipt=LRAT-REQUIREMENTS-MISMATCH\n"); return 1; }
433 }
434 printf("receipt=VALID\n");
435 return 0;
436}
437
438/* decode + accept the unit, locate the function, build its NO-TRAP formula (never read from disk) */
439static int property_cnf(const char *ir, const char *func, C2Cnf *cnf, C2BlastInfo *bi, uint8_t ir_sha[32])
440{
441 size_t n;
442 uint8_t *b = slurp(ir, &n);
443 if (!b) return 2;
444 sha256_hash(b, n, ir_sha);
445 C2Unit *u = malloc(sizeof *u);
446 C2Verdict v;
447 int rc = 1;
448 if (!u) { free(b); return 2; }
449 if (c2_decode(b, n, u, &v)) printf("checker2=REJECT rule=%s detail=%s\n", v.rule, v.detail);
450 else if (c2_blast_notrap(u, c2_func_index(u, func), cnf, bi)) printf("prove=REFUSED rule=%s detail=%s\n", bi->rule, bi->detail);
451 else rc = 0;
452 c2_unit_free(u);
453 free(u);
454 free(b);
455 return rc;
456}
457
458static int prove_cnf(const char *ir, const char *func, const char *out)
459{
460 C2Cnf cnf;
461 C2BlastInfo bi;
462 uint8_t s[32];
463 int rc = property_cnf(ir, func, &cnf, &bi, s);
464 if (rc) return rc;
465 size_t n;
466 char *t = c2_cnf_print(&cnf, &n);
467 rc = t ? write_new(out, (const uint8_t *)t, n) : 2;
468 if (!rc) printf("property=%s func=%s inputs=%d paths=%d trap_sites=%d vars=%d clauses=%d\n", C2_PROPERTY_NOTRAP, func,
469 bi.inputs, bi.paths, bi.trap_sites, cnf.nvars, cnf.nclauses);
470 free(t);
471 c2_cnf_free(&cnf);
472 return rc;
473}
474
475static int prove_check(const char *ir, const char *func, const char *proof)
476{
477 C2Cnf cnf;
478 C2BlastInfo bi;
479 uint8_t s[32], ps[32];
480 int rc = property_cnf(ir, func, &cnf, &bi, s);
481 if (rc) return rc;
482 size_t n;
483 uint8_t *p = slurp(proof, &n);
484 if (!p) { c2_cnf_free(&cnf); return 2; }
485 sha256_hash(p, n, ps);
486 C2LratResult r;
487 int e = c2_lrat_check(&cnf, (const char *)p, n, &r);
488 free(p);
489 c2_cnf_free(&cnf);
490 char h1[65], h2[65];
491 hex(s, 32, h1);
492 hex(ps, 32, h2);
493 if (e) { printf("lrat=REFUSED rule=%s line=%ld\n", r.rule, r.line); return 1; }
494 printf("lrat=VERIFIED property=%s func=%s ir_sha256=%s proof_sha256=%s steps=%ld\n", C2_PROPERTY_NOTRAP, func, h1, h2, r.steps);
495 return 0;
496}
497
498static int usage(void)
499{
500 fprintf(stderr, "usage: checker2 check <unit.ir> | version | receipt issue|tbs|attach|verify ... (see source header)\n");
501 return 2;
502}
503
504int main(int argc, char **argv)
505{
506 if (argc < 2) return usage();
507 if (strcmp(argv[1], "version") == 0) {
508 uint8_t id[32];
509 char h[65];
510 checker_id(id);
511 hex(id, 32, h);
512 printf("%s\nsources_sha256=%s\nchecker_id=%s\n", C2_CHECKER_VERSION, C2_SOURCES_SHA256, h);
513 return 0;
514 }
515 if (strcmp(argv[1], "prove-cnf") == 0 && argc == 5) return prove_cnf(argv[2], argv[3], argv[4]);
516 if (strcmp(argv[1], "prove-check") == 0 && argc == 5) return prove_check(argv[2], argv[3], argv[4]);
517 if (strcmp(argv[1], "check") == 0 && argc == 3) {
518 uint8_t s[32];
519 return judge(argv[2], s, 0);
520 }
521 if (strcmp(argv[1], "receipt") == 0 && argc >= 3) {
522 int (*fn)(int, char **) = NULL;
523 if (strcmp(argv[2], "issue") == 0) fn = receipt_issue;
524 if (strcmp(argv[2], "tbs") == 0) fn = receipt_tbs;
525 if (strcmp(argv[2], "attach") == 0) fn = receipt_attach;
526 if (strcmp(argv[2], "verify") == 0) fn = receipt_verify;
527 if (fn) {
528 int rc = fn(argc - 3, argv + 3);
529 return rc == 2 && argc - 3 < 2 ? usage() : rc;
530 }
531 }
532 return usage();
533}