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

GitHub aien-dev/omega-mv#302 (code only)

closed derived feb38195910d10fb #302

Imported from GitHub aien-dev/omega-mv#302 (code only)

GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed).

Base c62f47b5ac178311d7d8f91c4f52a298a1754ec8 · Head d368c3f8f0a7dbc282677116d5b284727c078dc6

Apply it

git checkout -b patch-feb38195910d10fb c62f47b5ac178311d7d8f91c4f52a298a1754ec8
curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/feb38195910d10fb.patch | git am

Raw patch (mbox) · Diff

Provenance

This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record:

github-import 1
aien-dev/omega-mv
302
d368c3f8f0a7dbc282677116d5b284727c078dc6
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":302,"url":"https://github.com/aien-dev/omega-mv/pull/302","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:37:13Z","metadata":"unavailable","state_derived":true}

Changes

diff --git a/.crumb b/.crumb
index b49b64941..cadbf5b71 100644
--- a/.crumb
+++ b/.crumb
@@ -63,8 +63,8 @@
           "purpose": "Directory doctrine. Purpose not yet described by a human."
         },
         {
-          "digest": "7a280ae41e1051f14529ab2c8b2749f4712ea37df87254fd90bfbc3e1bba2048",
-          "last_commit": "38a6a56",
+          "digest": "6c956a398f4be99822da86ad271d602dfdf10c425d5b5c5b79d1b3e99bdf556a",
+          "last_commit": "737770d",
           "name": "mk",
           "purpose": "Directory mk. Purpose not yet described by a human."
         },
@@ -82,7 +82,7 @@
         },
         {
           "digest": "4dde878eff597b3669b7dafeb572ba3a560c51ac52d99681524745684c45b258",
-          "last_commit": "b3f7c97",
+          "last_commit": "c62f47b",
           "name": "spec",
           "purpose": "Directory spec. Purpose not yet described by a human."
         },
@@ -105,15 +105,15 @@
           "purpose": "Directory tools. Purpose not yet described by a human."
         }
       ],
-      "children_root": "sha256:b4d9b70b960605d70ed3af66cf2e04c5017da671b305a7307625d54ec1926b8f",
-      "compiled_at": "2026-10-05T17:35:25Z",
+      "children_root": "sha256:ceb059a05199a880cc1d7dc8f0396fd61640d98ef3f622f81f6b197a9bb1cb5e",
+      "compiled_at": "2026-10-05T18:30:00Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:281a1e88b478b833888ae2969f763bfc97fc70f68d7eba9043ddcf8eb5097e32",
+      "digest": "sha256:afa4f4c5b4e2b8c5f697932a002d9a3cd7eaad892d81613af6a99ac3a353629b",
       "evidence_files": 2773,
       "evidence_root": "sha256:9b7e635d548d619fa71619fe08c4e37fc207feeb04bf6f9c6b0faa7ea4c3a533",
-      "files": 4576,
-      "generated_at_commit": "b3f7c97a22a105748d4dc4cf5bd34ca2e912f853",
-      "source_tree": "sha256:9a0c9c14e43aaf9eea8b55f8f32cd1dcefd64ef5f4e7c70a683dc81c1350be7a"
+      "files": 4582,
+      "generated_at_commit": "409d0bb927ef525c9df30269f3224448e200efd8",
+      "source_tree": "sha256:4a1b1ffee5f84b58811ebf3b66cf9d84ad539d157ff7457375754805f55c392e"
     },
     "owner_lane": "",
     "provenance": {
diff --git a/bench/prime_race/.crumb b/bench/prime_race/.crumb
new file mode 100644
index 000000000..494be2165
--- /dev/null
+++ b/bench/prime_race/.crumb
@@ -0,0 +1,22 @@
+{
+  "extensions": {
+    "generated": {
+      "children": [],
+      "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
+      "compiled_at": "2026-10-05T18:30:00Z",
+      "compiler": "crumb-compile/1",
+      "digest": "sha256:2588e63c0619f4dd7d77d84d754a7a2ebacb84ccd41070007db4ed164ddb58bf",
+      "evidence_files": 0,
+      "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
+      "files": 5,
+      "generated_at_commit": "409d0bb927ef525c9df30269f3224448e200efd8",
+      "source_tree": "sha256:08889fa1d154b54acf2c8e6c83ca28025f92975cf98b7f5b3ea8b43a362d5a4d"
+    }
+  },
+  "invariants": [
+    "canonical bitmap format is the contract; never reuse previous pass results; no CUDA symbols in AIEN binaries"
+  ],
+  "name": "bench/prime_race",
+  "purpose": "AIEN Prime Drag Race implementation side: the shared protocol helper (command line, timed loop, report, canonical bitmap export) and the conventional CPU baseline sieve that other entries are checked and compared against",
+  "schema_version": "1.0.0"
+}
diff --git a/bench/prime_race/README.md b/bench/prime_race/README.md
new file mode 100644
index 000000000..b5fe80388
--- /dev/null
+++ b/bench/prime_race/README.md
@@ -0,0 +1,47 @@
+# AIEN Prime Drag Race: implementation side
+
+This folder holds the code every Prime Drag Race entry is built on. The Drag Race is a benchmark where
+each entry sieves for prime numbers as many times as it can in a fixed time, and the runner checks every
+answer before any speed is believed.
+
+What is here:
+
+- `prime_race_impl.h` and `prime_race_impl.c`: the shared protocol. It reads the command line, runs the
+  timed loop, writes the report file and the one-line result. Every entry uses this one copy.
+- `cpu_base.c`: the plain, conventional CPU sieve (one thread, one bit per odd number, built the way the
+  upstream "base" rules describe). It is the yardstick the faster entries are compared to.
+- `tests/prime_race_host_test.c`: checks cpu_base against a slow, independent trial-division oracle for
+  every limit from 0 to 3000, the design limit list up to 2,000,000, and known prime counts.
+
+## Build and run
+
+From the repository root:
+
+    make prime-race-cpu
+    ./bench/prime_race/build/cpu_base --limit 1000000 --min-seconds 5
+
+    make prime-race-host-test
+
+Options: `--limit N` (default 1000000, at most 2^40), `--min-seconds S` (default 5), `--bitmap-out PATH`,
+`--report-out PATH`, `--audit-passes K`. Exit code 0 is success, 2 is an execution failure (no result
+line is printed), 64 is a usage mistake. Audit mode runs K passes and saves each pass's answer so the
+runner can prove every pass really recomputed; it prints no result line because it makes no timing claim.
+
+## The result line and the labels
+
+On success the program prints one line in the upstream format:
+
+    aien-<name>;<passes>;<elapsed seconds>;<threads>;algorithm=..,faithful=..,bits=..
+
+The labels say honestly what the entry does. `cpu_base` is `algorithm=base`, `faithful=yes` (it builds a
+fresh sieve each pass, like upstream), `bits=1`, one thread.
+
+The line is upstream compatible, but the entry is NONCONFORMING for upstream submission (different
+license and not an upstream language), so results are kept as AIEN receipts, not submitted upstream.
+
+## The answer format
+
+The canonical answer is the contract: bit k of the output is set exactly when 2k+1 is prime. The number 2
+is implied. Unused bits at the end are zero. A previous pass's result is never reused.
+
+The full contract will live in the aien-dev/benchmarks repository once it exists (link to be added).
diff --git a/bench/prime_race/cpu_base.c b/bench/prime_race/cpu_base.c
new file mode 100644
index 000000000..605cd3a4e
--- /dev/null
+++ b/bench/prime_race/cpu_base.c
@@ -0,0 +1,112 @@
+/* aien-cpu-c-base: conventional single-thread CPU sieve following the upstream "base" rules.
+ * Faithful: a struct holds the whole state, one calloc per pass sized to the limit, odd factors searched
+ * sequentially from 3 up to sqrt(limit), every multiple cleared individually starting at factor*factor.
+ * Storage: 1 bit per odd number, bit k <-> number 2k+1, inverted (set = composite). Stepping 2*factor in
+ * number space is stepping `factor` in odd-index space. */
+#include "prime_race_impl.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct cpu_sieve {
+    uint64_t limit;
+    uint64_t *composite; /* set bit = composite, indexed by (n-1)/2 */
+    size_t nwords;
+} cpu_sieve;
+
+static uint64_t isqrt_u64(uint64_t n) {
+    uint64_t r = 0;
+    uint64_t lo = 0, hi = 1ULL << 21; /* sqrt(2^40) = 2^20 < hi */
+    while (lo <= hi) {
+        uint64_t mid = lo + (hi - lo) / 2;
+        if (mid * mid <= n) { r = mid; lo = mid + 1; }
+        else hi = mid - 1;
+    }
+    return r;
+}
+
+static inline int is_comp(const cpu_sieve *s, uint64_t n) { return (int)((s->composite[(n - 1) / 2 / 64] >> (((n - 1) / 2) % 64)) & 1u); }
+static inline void mark_comp(cpu_sieve *s, uint64_t n) { s->composite[(n - 1) / 2 / 64] |= 1ULL << (((n - 1) / 2) % 64); }
+
+static void sieve_destroy(cpu_sieve *s) {
+    free(s->composite);
+    s->composite = NULL;
+    s->nwords = 0;
+}
+
+static int sieve_create(cpu_sieve *s, uint64_t limit) {
+    s->limit = limit;
+    s->nwords = pr_words64(limit);
+    s->composite = (uint64_t *)calloc(s->nwords ? s->nwords : 1, sizeof(uint64_t));
+    return s->composite ? 0 : -1;
+}
+
+static void sieve_run(cpu_sieve *s) {
+    const uint64_t limit = s->limit;
+    const uint64_t q = isqrt_u64(limit);
+    uint64_t factor = 3;
+    while (factor <= q) {
+        /* next unmarked odd number at or above factor */
+        for (uint64_t n = factor; n <= limit; n += 2) {
+            if (!is_comp(s, n)) { factor = n; break; }
+        }
+        for (uint64_t num = factor * factor; num <= limit; num += factor * 2) mark_comp(s, num);
+        factor += 2;
+    }
+}
+
+static int cpu_setup(void **state, uint64_t limit) {
+    (void)limit;
+    cpu_sieve *s = (cpu_sieve *)calloc(1, sizeof *s);
+    if (!s) return -1;
+    *state = s;
+    return 0;
+}
+
+static int cpu_pass(void *state, uint64_t limit) {
+    cpu_sieve *s = (cpu_sieve *)state;
+    sieve_destroy(s); /* previous pass's sieve is destroyed inside the timed pass */
+    if (sieve_create(s, limit) != 0) return -1;
+    sieve_run(s);
+    return 0;
+}
+
+static int cpu_export(void *state, uint64_t limit, uint64_t *out) {
+    cpu_sieve *s = (cpu_sieve *)state;
+    if (!s->composite || s->limit != limit) return -1;
+    const size_t nw = pr_words64(limit);
+    const uint64_t odd = pr_odd_count(limit);
+    for (size_t w = 0; w < nw; w++) out[w] = ~s->composite[w];
+    if (nw) {
+        out[0] &= ~1ULL; /* number 1 is not prime */
+        if (odd % 64) out[nw - 1] &= (1ULL << (odd % 64)) - 1; /* tail bits zero */
+    }
+    return 0;
+}
+
+static uint64_t cpu_threads(void *state) { (void)state; return 1; }
+
+static void cpu_teardown(void *state) {
+    cpu_sieve *s = (cpu_sieve *)state;
+    if (!s) return;
+    sieve_destroy(s);
+    free(s);
+}
+
+const pr_impl pr_cpu_base_impl = {
+    .name = "cpu-c-base",
+    .algorithm = "base",
+    .faithful = "yes",
+    .bits = 1,
+    .environment = "linux-host-cpu",
+    .setup = cpu_setup,
+    .pass = cpu_pass,
+    .export_bitmap = cpu_export,
+    .threads = cpu_threads,
+    .detail_json = NULL,
+    .teardown = cpu_teardown,
+};
+
+#ifndef PR_NO_MAIN
+int main(int argc, char **argv) { return pr_main(argc, argv, &pr_cpu_base_impl); }
+#endif
diff --git a/bench/prime_race/prime_race_impl.c b/bench/prime_race/prime_race_impl.c
new file mode 100644
index 000000000..34e94765a
--- /dev/null
+++ b/bench/prime_race/prime_race_impl.c
@@ -0,0 +1,276 @@
+/* AIEN Prime Drag Race: implementation-side protocol. See prime_race_impl.h and bench/prime_race/README.md. */
+#include "prime_race_impl.h"
+
+#include <errno.h>
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#ifndef PR_SOURCE_COMMIT
+#define PR_SOURCE_COMMIT "unknown"
+#endif
+#ifndef PR_CC
+#define PR_CC "unknown"
+#endif
+#ifndef PR_CFLAGS
+#define PR_CFLAGS "unknown"
+#endif
+
+static void set_err(char *err, size_t n, const char *fmt, const char *a) {
+    if (err && n) snprintf(err, n, fmt, a ? a : "");
+}
+
+static int parse_u64(const char *s, uint64_t *out) {
+    char *end = NULL;
+    if (!s || s[0] < '0' || s[0] > '9') return -1; /* rejects empty, sign, space */
+    errno = 0;
+    unsigned long long v = strtoull(s, &end, 10);
+    if (errno != 0 || end == s || *end != '\0') return -1;
+    *out = (uint64_t)v;
+    return 0;
+}
+
+static int parse_seconds(const char *s, double *out) {
+    char *end = NULL;
+    if (!s || !((s[0] >= '0' && s[0] <= '9') || s[0] == '.')) return -1; /* rejects nan/inf/sign */
+    errno = 0;
+    double v = strtod(s, &end);
+    if (errno != 0 || end == s || *end != '\0' || !isfinite(v) || v < 0.0) return -1;
+    *out = v;
+    return 0;
+}
+
+int pr_parse_args(int argc, char **argv, pr_args *a, char *err, size_t errn) {
+    a->limit = 1000000;
+    a->min_seconds = 5.0;
+    a->bitmap_out = NULL;
+    a->report_out = NULL;
+    a->audit_passes = 0;
+    int audit_given = 0;
+    for (int i = 1; i < argc; i++) {
+        const char *opt = argv[i];
+        const char *val = (i + 1 < argc) ? argv[i + 1] : NULL;
+        if (strcmp(opt, "--limit") == 0 || strcmp(opt, "--min-seconds") == 0 || strcmp(opt, "--bitmap-out") == 0 ||
+            strcmp(opt, "--report-out") == 0 || strcmp(opt, "--audit-passes") == 0) {
+            if (!val) { set_err(err, errn, "missing value for %s", opt); return -1; }
+            i++;
+            if (strcmp(opt, "--limit") == 0) {
+                if (parse_u64(val, &a->limit) != 0) { set_err(err, errn, "invalid --limit '%s'", val); return -1; }
+                if (a->limit > PR_MAX_LIMIT) { set_err(err, errn, "--limit '%s' exceeds 2^40", val); return -1; }
+            } else if (strcmp(opt, "--min-seconds") == 0) {
+                if (parse_seconds(val, &a->min_seconds) != 0) { set_err(err, errn, "invalid --min-seconds '%s'", val); return -1; }
+            } else if (strcmp(opt, "--audit-passes") == 0) {
+                if (parse_u64(val, &a->audit_passes) != 0 || a->audit_passes == 0) {
+                    set_err(err, errn, "invalid --audit-passes '%s' (need integer >= 1)", val);
+                    return -1;
+                }
+                audit_given = 1;
+            } else if (strcmp(opt, "--bitmap-out") == 0) {
+                if (val[0] == '\0') { set_err(err, errn, "empty %s", opt); return -1; }
+                a->bitmap_out = val;
+            } else {
+                if (val[0] == '\0') { set_err(err, errn, "empty %s", opt); return -1; }
+                a->report_out = val;
+            }
+        } else {
+            set_err(err, errn, "unknown argument '%s'", opt);
+            return -1;
+        }
+    }
+    if (audit_given && !a->bitmap_out) {
+        set_err(err, errn, "%s", "--audit-passes requires --bitmap-out");
+        return -1;
+    }
+    return 0;
+}
+
+int pr_format_line(char *buf, size_t n, const pr_impl *impl, uint64_t passes, double elapsed_s, uint64_t threads) {
+    int w = snprintf(buf, n, "aien-%s;%llu;%.6f;%llu;algorithm=%s,faithful=%s,bits=%d", impl->name,
+                     (unsigned long long)passes, elapsed_s, (unsigned long long)threads, impl->algorithm,
+                     impl->faithful, impl->bits);
+    if (w < 0 || (size_t)w >= n) return -1;
+    return w;
+}
+
+int pr_line_allowed(int failed, uint64_t passes, double elapsed_s, double min_seconds) {
+    if (failed) return 0;
+    if (passes == 0) return 0;
+    if (!isfinite(elapsed_s) || elapsed_s < min_seconds) return 0;
+    return 1;
+}
+
+void pr_json_string(FILE *f, const char *s) {
+    fputc('"', f);
+    for (const unsigned char *p = (const unsigned char *)(s ? s : ""); *p; p++) {
+        switch (*p) {
+        case '"': fputs("\\\"", f); break;
+        case '\\': fputs("\\\\", f); break;
+        case '\n': fputs("\\n", f); break;
+        case '\r': fputs("\\r", f); break;
+        case '\t': fputs("\\t", f); break;
+        default:
+            if (*p < 0x20) fprintf(f, "\\u%04x", (unsigned)*p);
+            else fputc(*p, f);
+        }
+    }
+    fputc('"', f);
+}
+
+int pr_write_report(FILE *f, const pr_report *r) {
+    const pr_impl *im = r->impl;
+    char label[256];
+    snprintf(label, sizeof label, "aien-%s", im->name);
+    fputs("{\n  \"schema\": \"aien-prime-race/impl-report/v1\",\n  \"impl\": ", f);
+    pr_json_string(f, im->name);
+    fputs(",\n  \"label\": ", f);
+    pr_json_string(f, label);
+    fputs(",\n  \"algorithm\": ", f);
+    pr_json_string(f, im->algorithm);
+    fputs(",\n  \"faithful\": ", f);
+    pr_json_string(f, im->faithful);
+    fprintf(f, ",\n  \"bits\": %d,\n  \"threads\": %llu,\n  \"environment\": ", im->bits, (unsigned long long)r->threads);
+    pr_json_string(f, im->environment);
+    fprintf(f, ",\n  \"limit\": %llu,\n  \"min_seconds\": %.6f,\n  \"mode\": ", (unsigned long long)r->limit, r->min_seconds);
+    pr_json_string(f, r->mode);
+    fprintf(f, ",\n  \"passes\": %llu,\n  \"elapsed_s\": %.6f,\n  \"status\": ", (unsigned long long)r->passes, r->elapsed_s);
+    pr_json_string(f, r->status);
+    fputs(",\n  \"error\": ", f);
+    if (r->error) pr_json_string(f, r->error); else fputs("null", f);
+    fputs(",\n  \"build\": {\"source_commit\": ", f);
+    pr_json_string(f, PR_SOURCE_COMMIT);
+    fputs(", \"cc\": ", f);
+    pr_json_string(f, PR_CC);
+    fputs(", \"cflags\": ", f);
+    pr_json_string(f, PR_CFLAGS);
+    fputs("},\n  \"detail\": ", f);
+    if (im->detail_json && r->state) {
+        if (im->detail_json(r->state, f) != 0) fputs("null", f);
+    } else {
+        fputs("{}", f);
+    }
+    fputs("\n}\n", f);
+    fflush(f);
+    return ferror(f) ? -1 : 0;
+}
+
+static int64_t now_ns(void) {
+    struct timespec ts;
+    if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) return -1;
+    return (int64_t)ts.tv_sec * 1000000000LL + (int64_t)ts.tv_nsec;
+}
+
+static int write_words(FILE *f, const uint64_t *w, size_t n) {
+    return (n == 0 || fwrite(w, sizeof(uint64_t), n, f) == n) ? 0 : -1;
+}
+
+int pr_main(int argc, char **argv, const pr_impl *impl) {
+    pr_args a;
+    char err[256] = "";
+    if (pr_parse_args(argc, argv, &a, err, sizeof err) != 0) {
+        fprintf(stderr, "usage error: %s\n", err);
+        fprintf(stderr, "usage: %s [--limit N=1000000] [--min-seconds S=5] [--bitmap-out PATH] "
+                        "[--report-out PATH] [--audit-passes K]\n", argc > 0 ? argv[0] : "impl");
+        return 64;
+    }
+
+    const int audit = a.audit_passes > 0;
+    char error[256] = "";
+    int failed = 0;
+    uint64_t passes = 0;
+    double elapsed = 0.0;
+    void *st = NULL;
+    int have_state = 0;
+    uint64_t *words = NULL;
+    FILE *bf = NULL;
+    const size_t nw = pr_words64(a.limit);
+
+    words = (uint64_t *)calloc(nw ? nw : 1, sizeof(uint64_t));
+    if (!words) { failed = 1; snprintf(error, sizeof error, "cannot allocate export buffer"); goto report; }
+    if (impl->setup(&st, a.limit) != 0) { failed = 1; snprintf(error, sizeof error, "setup failed"); goto report; }
+    have_state = 1;
+    if (a.bitmap_out && audit) {
+        bf = fopen(a.bitmap_out, "wb");
+        if (!bf) { failed = 1; snprintf(error, sizeof error, "cannot open bitmap-out"); goto report; }
+    }
+
+    if (audit) {
+        for (uint64_t k = 0; k < a.audit_passes; k++) {
+            if (impl->pass(st, a.limit) != 0) { failed = 1; snprintf(error, sizeof error, "pass %llu failed", (unsigned long long)k); break; }
+            passes++;
+            if (impl->export_bitmap(st, a.limit, words) != 0) { failed = 1; snprintf(error, sizeof error, "export failed on pass %llu", (unsigned long long)k); break; }
+            if (write_words(bf, words, nw) != 0 || fflush(bf) != 0) { failed = 1; snprintf(error, sizeof error, "bitmap write failed"); break; }
+        }
+    } else {
+        int64_t t0 = now_ns(), t1 = t0;
+        if (t0 < 0) { failed = 1; snprintf(error, sizeof error, "clock failure"); goto report; }
+        for (;;) {
+            if (impl->pass(st, a.limit) != 0) { failed = 1; snprintf(error, sizeof error, "pass %llu failed", (unsigned long long)passes); break; }
+            passes++;
+            t1 = now_ns();
+            if (t1 < 0) { failed = 1; snprintf(error, sizeof error, "clock failure"); break; }
+            if ((double)(t1 - t0) / 1e9 >= a.min_seconds) break;
+        }
+        elapsed = (double)(t1 - t0) / 1e9;
+        if (!failed && a.bitmap_out) {
+            /* untimed export of the final pass */
+            FILE *of = fopen(a.bitmap_out, "wb");
+            if (!of) { failed = 1; snprintf(error, sizeof error, "cannot open bitmap-out"); }
+            else {
+                if (impl->export_bitmap(st, a.limit, words) != 0) { failed = 1; snprintf(error, sizeof error, "export failed"); }
+                else if (write_words(of, words, nw) != 0) { failed = 1; snprintf(error, sizeof error, "bitmap write failed"); }
+                if (fclose(of) != 0 && !failed) { failed = 1; snprintf(error, sizeof error, "bitmap close failed"); }
+            }
+        }
+    }
+    if (bf) {
+        if (fclose(bf) != 0 && !failed) { failed = 1; snprintf(error, sizeof error, "bitmap close failed"); }
+        bf = NULL;
+    }
+    if (!failed && !audit && !pr_line_allowed(0, passes, elapsed, a.min_seconds)) {
+        failed = 1;
+        snprintf(error, sizeof error, "timed run did not satisfy passes>0 and elapsed>=min_seconds");
+    }
+    if (!failed && audit && passes != a.audit_passes) { failed = 1; snprintf(error, sizeof error, "audit pass count mismatch"); }
+
+report:;
+    if (bf) fclose(bf);
+    pr_report rep;
+    memset(&rep, 0, sizeof rep);
+    rep.impl = impl;
+    rep.state = have_state ? st : NULL;
+    rep.threads = have_state && impl->threads ? impl->threads(st) : 0;
+    rep.limit = a.limit;
+    rep.min_seconds = a.min_seconds;
+    rep.mode = audit ? "audit" : "timed";
+    rep.passes = passes;
+    rep.elapsed_s = elapsed;
+    rep.status = failed ? "EXEC_FAILED" : "COMPLETE";
+    rep.error = failed ? error : NULL;
+
+    if (a.report_out) {
+        FILE *rf = fopen(a.report_out, "w");
+        if (!rf || pr_write_report(rf, &rep) != 0 || fclose(rf) != 0) {
+            fprintf(stderr, "cannot write report to %s\n", a.report_out);
+            if (!failed) { failed = 1; snprintf(error, sizeof error, "report write failed"); }
+        }
+    }
+    uint64_t threads = rep.threads;
+    if (have_state && impl->teardown) impl->teardown(st);
+    free(words);
+
+    if (failed) {
+        fprintf(stderr, "EXEC_FAILED: %s\n", error);
+        return 2;
+    }
+    if (!audit) { /* audit mode makes no timing claims: no upstream line */
+        char line[512];
+        if (pr_format_line(line, sizeof line, impl, passes, elapsed, threads) < 0) {
+            fprintf(stderr, "EXEC_FAILED: line too long\n");
+            return 2;
+        }
+        printf("%s\n", line);
+        fflush(stdout);
+    }
+    return 0;
+}
diff --git a/bench/prime_race/prime_race_impl.h b/bench/prime_race/prime_race_impl.h
new file mode 100644
index 000000000..110d66737
--- /dev/null
+++ b/bench/prime_race/prime_race_impl.h
@@ -0,0 +1,72 @@
+/* AIEN Prime Drag Race: implementation-side protocol (one copy for every C/CUDA implementation).
+ * Contract: canonical bitmap, bit k (u64 word k/64, bit k%64) set iff 2k+1 is prime, 0 <= k < (limit+1)/2,
+ * tail bits zero; prime 2 is implicit when limit >= 2. */
+#ifndef PRIME_RACE_IMPL_H
+#define PRIME_RACE_IMPL_H
+
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+
+typedef struct pr_impl {
+    const char *name;        /* e.g. "cpu-c-base"; label printed as "aien-<name>" */
+    const char *algorithm;   /* base|wheel|other */
+    const char *faithful;    /* yes|no */
+    int bits;                /* storage bits per flag */
+    const char *environment; /* linux-host-cpu | linux-hosted-gb10-native | linux-hosted-gb10-cuda */
+    int  (*setup)(void **state, uint64_t limit);          /* untimed one-time setup, 0 = ok */
+    int  (*pass)(void *state, uint64_t limit);            /* timed: fresh sieve from scratch, complete, host-readable; 0 = ok */
+    int  (*export_bitmap)(void *state, uint64_t limit, uint64_t *out); /* canonical words of last pass, 0 = ok */
+    uint64_t (*threads)(void *state);                     /* threads that ran the sieve in last pass */
+    int  (*detail_json)(void *state, FILE *f);            /* optional (may be NULL): writes one JSON object */
+    void (*teardown)(void *state);
+} pr_impl;
+
+static inline uint64_t pr_odd_count(uint64_t limit) { return (limit + 1) / 2; }
+static inline size_t pr_words64(uint64_t limit) { return (size_t)((pr_odd_count(limit) + 63) / 64); }
+
+#define PR_MAX_LIMIT (1ULL << 40)
+
+/* Full CLI: <impl> [--limit N=1000000] [--min-seconds S=5] [--bitmap-out PATH] [--report-out PATH]
+ * [--audit-passes K]. Exit 0 ok, 2 execution failure (no stdout line), 64 usage error. */
+int pr_main(int argc, char **argv, const pr_impl *impl);
+
+/* ---- hooks exposed for host tests (pure functions, no process state) ---- */
+typedef struct pr_args {
+    uint64_t limit;
+    double min_seconds;
+    const char *bitmap_out;
+    const char *report_out;
+    uint64_t audit_passes; /* 0 = timed mode */
+} pr_args;
+
+/* 0 = ok; -1 = usage error with a message in err. */
+int pr_parse_args(int argc, char **argv, pr_args *a, char *err, size_t errn);
+
+/* Upstream line "aien-<name>;<passes>;<elapsed %.6f>;<threads>;algorithm=..,faithful=..,bits=..".
+ * Returns length written, or -1 if it does not fit. */
+int pr_format_line(char *buf, size_t n, const pr_impl *impl, uint64_t passes, double elapsed_s, uint64_t threads);
+
+/* 1 only if the stdout line may be printed: no failure, passes > 0, elapsed >= min_seconds. */
+int pr_line_allowed(int failed, uint64_t passes, double elapsed_s, double min_seconds);
+
+/* JSON string literal with hand-written escaping (quotes included). */
+void pr_json_string(FILE *f, const char *s);
+
+typedef struct pr_report {
+    const pr_impl *impl;
+    void *state;            /* for threads()/detail_json(); may be NULL */
+    uint64_t threads;
+    uint64_t limit;
+    double min_seconds;
+    const char *mode;       /* "timed" | "audit" */
+    uint64_t passes;
+    double elapsed_s;
+    const char *status;     /* "COMPLETE" | "EXEC_FAILED" */
+    const char *error;      /* NULL when ok */
+} pr_report;
+
+/* Writes the "aien-prime-race/impl-report/v1" JSON object. Returns 0 if the stream had no error. */
+int pr_write_report(FILE *f, const pr_report *r);
+
+#endif
diff --git a/bench/prime_race/tests/prime_race_host_test.c b/bench/prime_race/tests/prime_race_host_test.c
new file mode 100644
index 000000000..cdfa95ec8
--- /dev/null
+++ b/bench/prime_race/tests/prime_race_host_test.c
@@ -0,0 +1,232 @@
+/* Host test for the prime race protocol helper and the cpu_base baseline.
+ * Oracle: independent trial division (no sieve). Compares canonical bitmaps word for word. */
+#include "../prime_race_impl.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+extern const pr_impl pr_cpu_base_impl;
+
+static int g_fail = 0, g_checks = 0;
+#define CHECK(c, ...) do { g_checks++; if (!(c)) { g_fail++; fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); } } while (0)
+
+/* ---- independent oracle: canonical bitmap by trial division ---- */
+static int is_prime_td(uint64_t n) {
+    if (n < 2) return 0;
+    if (n % 2 == 0) return n == 2;
+    for (uint64_t d = 3; d * d <= n; d += 2) if (n % d == 0) return 0;
+    return 1;
+}
+static void oracle(uint64_t limit, uint64_t *out) {
+    size_t nw = pr_words64(limit);
+    memset(out, 0, (nw ? nw : 1) * 8);
+    for (uint64_t k = 0; k < pr_odd_count(limit); k++)
+        if (is_prime_td(2 * k + 1)) out[k / 64] |= 1ULL << (k % 64);
+}
+static uint64_t count_primes(const uint64_t *bm, uint64_t limit) {
+    uint64_t c = (limit >= 2);
+    for (size_t w = 0; w < pr_words64(limit); w++) c += (uint64_t)__builtin_popcountll(bm[w]);
+    return c;
+}
+/* comparator: returns -1 if identical, else index of first differing word */
+static long first_diff(const uint64_t *a, const uint64_t *b, size_t nw) {
+    for (size_t w = 0; w < nw; w++) if (a[w] != b[w]) return (long)w;
+    return -1;
+}
+
+static uint64_t *run_cpu(uint64_t limit, void **stp) {
+    const pr_impl *im = &pr_cpu_base_impl;
+    void *st = NULL;
+    if (im->setup(&st, limit) != 0 || im->pass(st, limit) != 0) return NULL;
+    uint64_t *out = (uint64_t *)calloc(pr_words64(limit) ? pr_words64(limit) : 1, 8);
+    if (!out || im->export_bitmap(st, limit, out) != 0) return NULL;
+    *stp = st;
+    return out;
+}
+
+static void check_limit(uint64_t limit, const uint64_t *expect) {
+    void *st = NULL;
+    uint64_t *got = run_cpu(limit, &st);
+    CHECK(got != NULL, "cpu_base failed at limit %llu", (unsigned long long)limit);
+    if (!got) return;
+    long d = first_diff(expect, got, pr_words64(limit));
+    CHECK(d < 0, "limit %llu: first differing word %ld", (unsigned long long)limit, d);
+    pr_cpu_base_impl.teardown(st);
+    free(got);
+}
+
+static void test_all_small(void) {
+    uint64_t *exp = (uint64_t *)malloc(64 * 8);
+    for (uint64_t limit = 0; limit <= 3000; limit++) {
+        oracle(limit, exp);
+        check_limit(limit, exp);
+    }
+    free(exp);
+}
+
+/* design limits up to 2,000,000: expected bitmap derived from one trial-division master (prefix + tail mask) */
+
+static size_t g_nlim = 0;
+static uint64_t g_lims[8192];
+static void addlim(uint64_t l) { if (l <= 2000000 && g_nlim < 8192) g_lims[g_nlim++] = l; }
+static void addrange(uint64_t a, uint64_t b) { for (uint64_t l = a; l <= b; l++) addlim(l); }
+
+static void test_design_limits(void) {
+    static const uint64_t one[] = {0, 1, 2, 3, 4, 5, 8, 9, 15, 25, 49, 121, 169, 289, 361, 961, 1000000, 2000000};
+    for (size_t i = 0; i < sizeof one / sizeof one[0]; i++) addlim(one[i]);
+    addrange(31, 34); addrange(63, 66); addrange(95, 97); addrange(127, 130); addrange(255, 258);
+    addrange(1023, 1025); addrange(1999, 2001); addrange(4095, 4097);
+    addrange(999983, 1000003);
+    for (uint64_t k = 1; 2048 * k <= 2000000; k++) { addlim(2048 * k - 1); addlim(2048 * k); addlim(2048 * k + 1); }
+    for (uint64_t p = 2; p <= 997; p++) if (is_prime_td(p)) { addlim(p * p - 1); addlim(p * p); addlim(p * p + 1); }
+
+    size_t mw = pr_words64(2000000);
+    uint64_t *m = (uint64_t *)malloc(mw * 8);
+    oracle(2000000, m);
+    uint64_t *exp = (uint64_t *)malloc(mw * 8);
+    for (size_t i = 0; i < g_nlim; i++) {
+        uint64_t limit = g_lims[i];
+        size_t nw = pr_words64(limit);
+        uint64_t odd = pr_odd_count(limit);
+        for (size_t w = 0; w < nw; w++) exp[w] = m[w];
+        if (nw && odd % 64) exp[nw - 1] &= (1ULL << (odd % 64)) - 1;
+        if (limit <= 3000) { /* master prefix must agree with direct oracle */
+            uint64_t d[64]; oracle(limit, d);
+            CHECK(first_diff(d, exp, nw) < 0, "master prefix disagrees with direct oracle at %llu", (unsigned long long)limit);
+        }
+        check_limit(limit, exp);
+    }
+    printf("design limits checked: %zu\n", g_nlim);
+    free(m); free(exp);
+}
+
+static void test_pi_table(void) {
+    static const struct { uint64_t limit, pi; } t[] = {{10, 4}, {100, 25}, {1000, 168}, {10000, 1229}, {100000, 9592}, {1000000, 78498}, {10000000, 664579}};
+    for (size_t i = 0; i < sizeof t / sizeof t[0]; i++) {
+        void *st = NULL;
+        uint64_t *bm = run_cpu(t[i].limit, &st);
+        CHECK(bm != NULL, "run failed at %llu", (unsigned long long)t[i].limit);
+        if (!bm) continue;
+        uint64_t c = count_primes(bm, t[i].limit);
+        CHECK(c == t[i].pi, "pi(%llu)=%llu expected %llu", (unsigned long long)t[i].limit, (unsigned long long)c, (unsigned long long)t[i].pi);
+        pr_cpu_base_impl.teardown(st);
+        free(bm);
+    }
+}
+
+/* negative self-checks: the comparator must catch corruption */
+static void test_negative(void) {
+    static const uint64_t lims[] = {100, 1000, 4097, 1000000};
+    for (size_t i = 0; i < sizeof lims / sizeof lims[0]; i++) {
+        uint64_t limit = lims[i];
+        size_t nw = pr_words64(limit);
+        uint64_t *exp = (uint64_t *)malloc(nw * 8), *bad = (uint64_t *)malloc(nw * 8);
+        oracle(limit, exp);
+        memcpy(bad, exp, nw * 8);
+        CHECK(first_diff(exp, bad, nw) < 0, "identical copies must compare equal");
+        bad[nw / 2] ^= 1ULL << 1; /* flip one bit */
+        CHECK(first_diff(exp, bad, nw) == (long)(nw / 2), "one flipped bit not caught at %llu", (unsigned long long)limit);
+        memcpy(bad, exp, nw * 8);
+        uint64_t odd = pr_odd_count(limit);
+        if (odd % 64) { bad[nw - 1] |= 1ULL << 63; CHECK(first_diff(exp, bad, nw) >= 0, "tail garbage not caught"); }
+        memcpy(bad, exp, nw * 8);
+        memset(bad, 0, nw * 8);
+        CHECK(first_diff(exp, bad, nw) >= 0, "all-zero not caught");
+        free(exp); free(bad);
+    }
+}
+
+/* each pass recomputes; second pass on same state gives the same canonical result */
+static void test_repeat_pass(void) {
+    void *st = NULL;
+    const pr_impl *im = &pr_cpu_base_impl;
+    uint64_t limit = 12345, a[256], b[256];
+    CHECK(im->setup(&st, limit) == 0, "setup");
+    CHECK(im->pass(st, limit) == 0 && im->export_bitmap(st, limit, a) == 0, "pass1");
+    CHECK(im->pass(st, limit) == 0 && im->export_bitmap(st, limit, b) == 0, "pass2");
+    CHECK(first_diff(a, b, pr_words64(limit)) < 0, "pass1 != pass2");
+    CHECK(im->threads(st) == 1, "threads");
+    im->teardown(st);
+}
+
+static void test_formatting(void) {
+    char buf[512];
+    const pr_impl *im = &pr_cpu_base_impl;
+    int n = pr_format_line(buf, sizeof buf, im, 1234, 5.000123456, 1);
+    CHECK(n > 0 && strcmp(buf, "aien-cpu-c-base;1234;5.000123;1;algorithm=base,faithful=yes,bits=1") == 0, "line: %s", buf);
+    char tiny[8];
+    CHECK(pr_format_line(tiny, sizeof tiny, im, 1, 1.0, 1) < 0, "truncated line must be rejected");
+    CHECK(pr_line_allowed(0, 1, 5.0, 5.0) == 1, "allowed");
+    CHECK(pr_line_allowed(1, 1, 5.0, 5.0) == 0, "failed -> no line");
+    CHECK(pr_line_allowed(0, 0, 5.0, 5.0) == 0, "passes 0 -> no line");
+    CHECK(pr_line_allowed(0, 3, 4.999, 5.0) == 0, "short elapsed -> no line");
+
+    char *mem = NULL; size_t sz = 0;
+    FILE *f = open_memstream(&mem, &sz);
+    pr_json_string(f, "a\"b\\c\n\t\x01z");
+    fclose(f);
+    CHECK(strcmp(mem, "\"a\\\"b\\\\c\\n\\t\\u0001z\"") == 0, "json escape: %s", mem);
+    free(mem);
+
+    void *st = NULL;
+    im->setup(&st, 100);
+    pr_report r = {im, st, 1, 100, 5.0, "timed", 7, 5.25, "COMPLETE", NULL};
+    f = open_memstream(&mem, &sz);
+    CHECK(pr_write_report(f, &r) == 0, "report write");
+    fclose(f);
+    CHECK(strstr(mem, "\"schema\": \"aien-prime-race/impl-report/v1\"") != NULL, "schema");
+    CHECK(strstr(mem, "\"label\": \"aien-cpu-c-base\"") != NULL, "label");
+    CHECK(strstr(mem, "\"status\": \"COMPLETE\"") != NULL && strstr(mem, "\"error\": null") != NULL, "status/error");
+    CHECK(strstr(mem, "\"passes\": 7") != NULL && strstr(mem, "\"elapsed_s\": 5.250000") != NULL, "passes/elapsed");
+    CHECK(strstr(mem, "\"environment\": \"linux-host-cpu\"") != NULL, "environment");
+    CHECK(strstr(mem, "\"build\": {\"source_commit\"") != NULL, "build");
+    free(mem);
+    r.status = "EXEC_FAILED"; r.error = "boom \"x\"";
+    f = open_memstream(&mem, &sz);
+    pr_write_report(f, &r);
+    fclose(f);
+    CHECK(strstr(mem, "\"error\": \"boom \\\"x\\\"\"") != NULL, "failure report error escaped");
+    free(mem);
+    im->teardown(st);
+}
+
+static void test_args(void) {
+    pr_args a; char err[128];
+    char *ok[] = {"x", "--limit", "1000", "--min-seconds", "0.5", "--audit-passes", "3", "--bitmap-out", "/tmp/b"};
+    CHECK(pr_parse_args(9, ok, &a, err, sizeof err) == 0 && a.limit == 1000 && a.min_seconds == 0.5 && a.audit_passes == 3, "valid args");
+    char *def[] = {"x"};
+    CHECK(pr_parse_args(1, def, &a, err, sizeof err) == 0 && a.limit == 1000000 && a.min_seconds == 5.0 && a.audit_passes == 0, "defaults");
+    char *bad1[] = {"x", "--limit", "12abc"};
+    char *bad2[] = {"x", "--limit", "-5"};
+    char *bad3[] = {"x", "--limit", "1099511627777"}; /* 2^40 + 1 */
+    char *bad4[] = {"x", "--limit"};
+    char *bad5[] = {"x", "--min-seconds", "nan"};
+    char *bad6[] = {"x", "--audit-passes", "0", "--bitmap-out", "/tmp/b"};
+    char *bad7[] = {"x", "--bogus"};
+    char *bad8[] = {"x", "--audit-passes", "2"};
+    char *bad9[] = {"x", "--limit", ""};
+    char *good40[] = {"x", "--limit", "1099511627776"};
+    CHECK(pr_parse_args(3, bad1, &a, err, sizeof err) != 0, "trailing junk");
+    CHECK(pr_parse_args(3, bad2, &a, err, sizeof err) != 0, "negative");
+    CHECK(pr_parse_args(3, bad3, &a, err, sizeof err) != 0, "above 2^40");
+    CHECK(pr_parse_args(2, bad4, &a, err, sizeof err) != 0, "missing value");
+    CHECK(pr_parse_args(3, bad5, &a, err, sizeof err) != 0, "nan");
+    CHECK(pr_parse_args(5, bad6, &a, err, sizeof err) != 0, "audit 0");
+    CHECK(pr_parse_args(2, bad7, &a, err, sizeof err) != 0, "unknown option");
+    CHECK(pr_parse_args(3, bad8, &a, err, sizeof err) != 0, "audit without bitmap-out");
+    CHECK(pr_parse_args(3, bad9, &a, err, sizeof err) != 0, "empty limit");
+    CHECK(pr_parse_args(3, good40, &a, err, sizeof err) == 0, "2^40 allowed");
+}
+
+int main(void) {
+    test_formatting();
+    test_args();
+    test_negative();
+    test_repeat_pass();
+    test_pi_table();
+    test_all_small();
+    test_design_limits();
+    printf("prime_race_host_test: %d checks, %d failures\n", g_checks, g_fail);
+    printf(g_fail ? "PRIME_RACE_HOST_TEST FAIL\n" : "PRIME_RACE_HOST_TEST PASS\n");
+    return g_fail ? 1 : 0;
+}
diff --git a/mk/.crumb b/mk/.crumb
index 716798072..73eeef5c2 100644
--- a/mk/.crumb
+++ b/mk/.crumb
@@ -7,14 +7,14 @@
     "generated": {
       "children": [],
       "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
-      "compiled_at": "2026-10-05T16:48:41Z",
+      "compiled_at": "2026-10-05T18:29:44Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:7a280ae41e1051f14529ab2c8b2749f4712ea37df87254fd90bfbc3e1bba2048",
+      "digest": "sha256:6c956a398f4be99822da86ad271d602dfdf10c425d5b5c5b79d1b3e99bdf556a",
       "evidence_files": 0,
       "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
-      "files": 56,
-      "generated_at_commit": "678ed89f7b0213130c92d20d481bf429414772e2",
-      "source_tree": "sha256:0fcc8e338cb26be5ab3c9d7d009c5cb65055a68e4e20ccaab359691ac79afc78"
+      "files": 57,
+      "generated_at_commit": "737770d4334b0fe10940b5f557abd8c784578689",
+      "source_tree": "sha256:5103132b815f818202d70ca6199a43189a6a81a20b227b7855c3baae1222ccbe"
     },
     "owner_lane": "",
     "provenance": {
diff --git a/mk/prime_race.mk b/mk/prime_race.mk
new file mode 100644
index 000000000..e3072a4f7
--- /dev/null
+++ b/mk/prime_race.mk
@@ -0,0 +1,28 @@
+# AIEN Prime Drag Race (bench/prime_race/): implementation-side protocol + conventional CPU baseline.
+# Standalone: new files only under bench/prime_race/. Picked up by `-include mk/*.mk`; not part of `all`
+# or `test`. No physics, no GPU, no libraries.
+#
+# prime-race-cpu        builds bench/prime_race/build/cpu_base (-O3 -mcpu=native; flags baked in via -D)
+# prime-race-host-test  trial-division oracle test of cpu_base + protocol formatting/parsing
+ifndef PRIME_RACE_MK
+PRIME_RACE_MK := 1
+.PHONY: prime-race-cpu prime-race-host-test
+PR_DIR = bench/prime_race
+PR_BUILD = $(PR_DIR)/build
+PR_WARN = -std=gnu11 -Wall -Wextra -Werror -D_GNU_SOURCE
+PR_OPT = -O3 -mcpu=native
+PR_SOURCE_COMMIT_VAL := $(shell git rev-parse HEAD 2>/dev/null || echo unknown)
+PR_DEFS = -DPR_SOURCE_COMMIT='"$(PR_SOURCE_COMMIT_VAL)"' -DPR_CC='"$(CC)"' -DPR_CFLAGS='"$(PR_WARN) $(PR_OPT)"'
+PR_HDRS = $(PR_DIR)/prime_race_impl.h
+
+$(PR_BUILD)/cpu_base: $(PR_DIR)/cpu_base.c $(PR_DIR)/prime_race_impl.c $(PR_HDRS)
+	@mkdir -p $(PR_BUILD)
+	$(CC) $(PR_WARN) $(PR_OPT) $(PR_DEFS) -o $@ $(PR_DIR)/cpu_base.c $(PR_DIR)/prime_race_impl.c -lm
+prime-race-cpu: $(PR_BUILD)/cpu_base
+
+$(PR_BUILD)/prime_race_host_test: $(PR_DIR)/tests/prime_race_host_test.c $(PR_DIR)/cpu_base.c $(PR_DIR)/prime_race_impl.c $(PR_HDRS)
+	@mkdir -p $(PR_BUILD)
+	$(CC) $(PR_WARN) -O2 -DPR_NO_MAIN -o $@ $(PR_DIR)/tests/prime_race_host_test.c $(PR_DIR)/cpu_base.c $(PR_DIR)/prime_race_impl.c -lm
+prime-race-host-test: $(PR_BUILD)/prime_race_host_test
+	./$(PR_BUILD)/prime_race_host_test
+endif