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 / gen / protocol / famsig.c
| 1 | /* |
| 2 | * famsig.c -- GEN family signatures and the split disjointness rule (aien-spark/omega#476). |
| 3 | * Contract: tests/one_layer/generalization/families/FAMSIG_V1.md (frozen; this tool is its reference). |
| 4 | * Deterministic: no clock, no randomness, no environment; output depends only on the arguments and files. |
| 5 | * |
| 6 | * famsig canon <shape> print "gs1:<canonical form>" of one nesting shape |
| 7 | * famsig coarse <shape> print the coarse signature "gc1:<coarse form>" (rule R6) |
| 8 | * famsig token <shape> print the family token "gs1-<16 hex>" (record family field) |
| 9 | * famsig edges <shape> print the composition edges "parent>child", one per line, sorted |
| 10 | * famsig meta <file>... validate gen-meta / gen-split files; one line per task: |
| 11 | * "<task_id>\t<split>\t<signature>" |
| 12 | * famsig rule [--show-heldout] <file>... |
| 13 | * apply the disjointness rule (R1..R8) over every task of every file; |
| 14 | * report, then "RULE PASS" (exit 0) or "RULE FAIL" (exit 1). Heldout |
| 15 | * family signatures are printed only with --show-heldout. |
| 16 | * famsig split <gen-split file> validate a sealed split file; print split_sha256, famlist_sha256, |
| 17 | * tasks, families (no signature, no task metadata) |
| 18 | * famsig famlist <gen-split file> print the FAMLIST bytes whose sha256 is famlist_sha256 |
| 19 | * famsig compare <SPLIT> <REDECLARED> |
| 20 | * the reviewer's blind re-declaration check: both gen-split files with the |
| 21 | * same task ids; prints only "tasks=<n> mismatches=<k>" (no signature, no |
| 22 | * task id); exit 0 when every task has the same signature, else 1 |
| 23 | * Exit: 0 ok, 1 refused (reason on stderr) or rule failure, 2 usage. |
| 24 | */ |
| 25 | #include "famsig_lib.h" |
| 26 | |
| 27 | static int usage(void) |
| 28 | { |
| 29 | fprintf(stderr, "usage: famsig canon <shape> | coarse <shape> | token <shape> | edges <shape> | meta <file>... | rule [--show-heldout] <file>... | split <SPLIT> | famlist <SPLIT> | compare <SPLIT> <REDECLARED>\n"); |
| 30 | return 2; |
| 31 | } |
| 32 | |
| 33 | static int read_all(int argc, char **argv, int from, FsSet *set) |
| 34 | { |
| 35 | char err[512]; |
| 36 | for (int i = from; i < argc; i++) |
| 37 | if (fs_read(argv[i], set, err, sizeof err)) { fprintf(stderr, "famsig: %s\n", err); return -1; } |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | static int read_split(const char *path, FsSet *set) |
| 42 | { |
| 43 | char err[512]; |
| 44 | if (fs_read(path, set, err, sizeof err)) { fprintf(stderr, "famsig: %s\n", err); return -1; } |
| 45 | if (!set->is_split) { fprintf(stderr, "famsig: %s: not a gen-split file\n", path); return -1; } |
| 46 | if (set->n == 0) { fprintf(stderr, "famsig: %s: no task lines\n", path); return -1; } |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | int main(int argc, char **argv) |
| 51 | { |
| 52 | if (argc < 2) return usage(); |
| 53 | const char *cmd = argv[1]; |
| 54 | if (!strcmp(cmd, "coarse")) { |
| 55 | if (argc != 3) return usage(); |
| 56 | FsShape sh; |
| 57 | if (fs_shape(argv[2], &sh)) { fprintf(stderr, "famsig: shape: %s\n", sh.err); return 1; } |
| 58 | char c[FS_SIGMAX + 4]; |
| 59 | fs_coarse(&sh, c); |
| 60 | printf("%s\n", c); |
| 61 | return 0; |
| 62 | } |
| 63 | if (!strcmp(cmd, "canon") || !strcmp(cmd, "edges") || !strcmp(cmd, "token")) { |
| 64 | if (argc != 3) return usage(); |
| 65 | FsShape sh; |
| 66 | if (fs_shape(argv[2], &sh)) { fprintf(stderr, "famsig: shape: %s\n", sh.err); return 1; } |
| 67 | if (cmd[0] == 'c') { printf("gs1:%s\n", sh.canon); return 0; } |
| 68 | if (cmd[0] == 't') { char sig[FS_SIGMAX + 4], tok[24]; snprintf(sig, sizeof sig, "gs1:%s", sh.canon); fs_token(sig, tok); printf("%s\n", tok); return 0; } |
| 69 | char e[64][16]; |
| 70 | int ne = fs_edges(&sh, e, 64); |
| 71 | for (int i = 0; i < ne; i++) printf("%s\n", e[i]); |
| 72 | return 0; |
| 73 | } |
| 74 | if (!strcmp(cmd, "meta")) { |
| 75 | if (argc < 3) return usage(); |
| 76 | FsSet set = {0}; |
| 77 | if (read_all(argc, argv, 2, &set)) return 1; |
| 78 | for (int i = 0; i < set.n; i++) printf("%s\t%s\t%s\n", set.m[i].id, fs_split[set.m[i].split], set.m[i].sig); |
| 79 | return 0; |
| 80 | } |
| 81 | if (!strcmp(cmd, "rule")) { |
| 82 | int show = 0, from = 2; |
| 83 | if (argc > 2 && !strcmp(argv[2], "--show-heldout")) { show = 1; from = 3; } |
| 84 | if (argc <= from) return usage(); |
| 85 | FsSet set = {0}; |
| 86 | if (read_all(argc, argv, from, &set)) { printf("RULE FAIL\n"); return 1; } |
| 87 | int f = fs_rule(&set, stdout, show); |
| 88 | printf(f ? "RULE FAIL\n" : "RULE PASS\n"); |
| 89 | return f ? 1 : 0; |
| 90 | } |
| 91 | if (!strcmp(cmd, "split") || !strcmp(cmd, "famlist")) { |
| 92 | if (argc != 3) return usage(); |
| 93 | FsSet set = {0}; |
| 94 | if (read_split(argv[2], &set)) return 1; |
| 95 | int nf; |
| 96 | char *fl = fs_famlist(&set, &nf); |
| 97 | if (cmd[0] == 'f') { fputs(fl, stdout); return 0; } |
| 98 | unsigned char d[32]; |
| 99 | char hex[65]; |
| 100 | sha256_hash((const unsigned char *)fl, strlen(fl), d); |
| 101 | fs_hex(d, hex); |
| 102 | printf("split_sha256=%s\nfamlist_sha256=%s\ntasks=%d\nfamilies=%d\n", set.split_sha256, hex, set.n, nf); |
| 103 | free(fl); |
| 104 | return 0; |
| 105 | } |
| 106 | if (!strcmp(cmd, "compare")) { |
| 107 | if (argc != 4) return usage(); |
| 108 | FsSet a = {0}, b = {0}; |
| 109 | if (read_split(argv[2], &a) || read_split(argv[3], &b)) return 1; |
| 110 | if (a.n != b.n) { fprintf(stderr, "famsig: compare: %d tasks vs %d tasks\n", a.n, b.n); return 1; } |
| 111 | int mis = 0; |
| 112 | for (int i = 0; i < a.n; i++) { |
| 113 | if (strcmp(a.m[i].id, b.m[i].id)) { fprintf(stderr, "famsig: compare: task lists differ at line %d\n", i + 3); return 1; } |
| 114 | mis += strcmp(a.m[i].sig, b.m[i].sig) != 0; |
| 115 | } |
| 116 | printf("tasks=%d mismatches=%d\n", a.n, mis); |
| 117 | return mis ? 1 : 0; |
| 118 | } |
| 119 | return usage(); |
| 120 | } |