aien-train
AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.
Brought by drakestapleton
aien-train / tests / omegese45 / eval / mkcase.py
| 1 | """Test-only builder of synthetic demo case directories for check-demo. |
| 2 | |
| 3 | Signs with throwaway test keys (private scalars below are public test data, |
| 4 | never used anywhere else). Byte layouts follow aien-protocols e705fdc9 |
| 5 | specs/microjob/SPEC.md and specs/aienet/jobpath/PROFILE.md. The layout |
| 6 | readers in eval/omegese45/oe_an0.py are proven against the real Rust-signed |
| 7 | artifacts in fixtures/baseline/, so these fixtures only exercise check-demo's |
| 8 | plumbing (episode <-> facts <-> artifacts <-> English) and its negative |
| 9 | controls. |
| 10 | """ |
| 11 | |
| 12 | import hashlib |
| 13 | import os |
| 14 | import struct |
| 15 | import sys |
| 16 | |
| 17 | sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "..", "eval", "omegese45")) |
| 18 | import oe_an0 as A # noqa: E402 |
| 19 | |
| 20 | TEST_KEYS = {"submitter": 0x5EED01, "executor": 0x5EED02, "verifier": 0x5EED03} |
| 21 | |
| 22 | |
| 23 | def pub(d): |
| 24 | x, y = A._mul(d, A.G) |
| 25 | return bytes([2 | (y & 1)]) + x.to_bytes(32, "big") |
| 26 | |
| 27 | |
| 28 | def sign(d, msg): |
| 29 | e = int.from_bytes(hashlib.sha256(msg).digest(), "big") |
| 30 | k = int.from_bytes(hashlib.sha256(d.to_bytes(32, "big") + msg).digest(), "big") % A.N or 1 |
| 31 | r = A._mul(k, A.G)[0] % A.N |
| 32 | s = pow(k, -1, A.N) * (e + r * d) % A.N |
| 33 | if s > A.N // 2: |
| 34 | s = A.N - s |
| 35 | return r.to_bytes(32, "big") + s.to_bytes(32, "big") |
| 36 | |
| 37 | |
| 38 | def document(payload, job_id, m, k, n): |
| 39 | head = bytearray(192) |
| 40 | head[0:4] = b"MJB1" |
| 41 | head[4] = 1 |
| 42 | head[6:8] = struct.pack(">H", 1) |
| 43 | head[144:152] = struct.pack(">Q", 4102444800) |
| 44 | node = bytearray(448) |
| 45 | node[0:16] = job_id |
| 46 | node[16:32] = hashlib.sha256(b"idem" + job_id).digest()[:16] |
| 47 | node[32:36] = struct.pack(">I", 1) |
| 48 | node[36], node[37], node[48], node[54], node[55] = 1, 2, 1, 0, 1 |
| 49 | node[80:88] = struct.pack(">Q", m * k * n) |
| 50 | node[288] = 3 |
| 51 | node[292:296] = struct.pack(">I", 1) |
| 52 | node[296:328] = hashlib.sha256(payload).digest() |
| 53 | return bytes(head + node) |
| 54 | |
| 55 | |
| 56 | def triple(m, k, n, seed, tag=b"", forge_output=False): |
| 57 | """forge_output: the worker reports a wrong output digest (one bit flipped) |
| 58 | and every party signs consistently over it: the receipt, the verdict |
| 59 | commitment and the countersignature are all valid, so only an independent |
| 60 | recompute can reject it.""" |
| 61 | payload = bytes([1]) + struct.pack(">HHHQ", m, k, n, seed) |
| 62 | job_id = hashlib.sha256(b"job" + payload + tag).digest()[:16] |
| 63 | doc = document(payload, job_id, m, k, n) |
| 64 | dag = A.sha(doc) |
| 65 | sk = TEST_KEYS |
| 66 | sig = sign(sk["submitter"], A.SUBMIT_DOMAIN + dag + A.sha(payload)) |
| 67 | an0b = (b"AN0B" + bytes([1, 0]) + struct.pack(">HI", len(payload), len(doc)) |
| 68 | + pub(sk["submitter"]) + sig + payload + doc) |
| 69 | out = A.matmul_u32_digest(m, k, n, seed) |
| 70 | if forge_output: |
| 71 | out = bytes([out[0] ^ 1]) + out[1:] |
| 72 | commit = A.sha(A.VERDICT_DOMAIN, dag, job_id, out) |
| 73 | ops = A.parse_document(doc)["operands"] |
| 74 | r = bytearray(404) |
| 75 | r[0:4] = b"MJR1" |
| 76 | r[4], r[5], r[6] = 1, 1, 1 |
| 77 | r[8:40] = dag |
| 78 | r[40:56] = job_id |
| 79 | r[56:72] = doc[192 + 16:192 + 32] |
| 80 | r[72:76] = struct.pack(">I", 1) |
| 81 | r[88:112] = struct.pack(">QQQ", m * k * n, 4 * (m * k + k * n + m * n), m * k * n) |
| 82 | r[112:144] = A.input_binding(ops) |
| 83 | r[144:176] = out |
| 84 | r[176:208] = hashlib.sha256(b"source").digest() |
| 85 | r[240:272] = hashlib.sha256(b"policy").digest() |
| 86 | r[272:304] = commit |
| 87 | r[304:337] = pub(sk["executor"]) |
| 88 | r[340:404] = sign(sk["executor"], bytes(r[:340])) |
| 89 | mjr1 = bytes(r) |
| 90 | v = bytearray(236) |
| 91 | v[0:4] = b"AN0V" |
| 92 | v[4] = 1 |
| 93 | v[8:40] = A.sha(mjr1) |
| 94 | v[40:72] = dag |
| 95 | v[72:104] = out |
| 96 | v[104:136] = commit |
| 97 | v[136:169] = pub(sk["verifier"]) |
| 98 | v[172:236] = sign(sk["verifier"], A.VERIFY_DOMAIN + bytes(v[:172])) |
| 99 | return dag.hex(), an0b, mjr1, bytes(v) |
| 100 | |
| 101 | |
| 102 | HEADER = "job\tattempt\tm\tk\tn\tseed\texit\tclass\tcode\tdag_id\tmjr1_sha256\tmjr1_outcome\tverifier" |
| 103 | CODE_EXIT = {"transport": 10, "replay": 11, "budget": 11, "worker": 11, "receipt": 12, |
| 104 | "recomp": 13, "verdict": 14, "io": 15} |
| 105 | |
| 106 | |
| 107 | def write_case(path, lines, dims, seeds, outcomes, response=None, request="request\n", forge_output=False): |
| 108 | """lines: episode message lines; outcomes: per dispatch (job, attempt, cls, code).""" |
| 109 | os.makedirs(os.path.join(path, "artifacts"), exist_ok=True) |
| 110 | os.makedirs(os.path.join(path, "keys"), exist_ok=True) |
| 111 | os.makedirs(os.path.join(path, "pins"), exist_ok=True) |
| 112 | os.makedirs(os.path.join(path, "logs"), exist_ok=True) |
| 113 | os.makedirs(os.path.join(path, "worker"), exist_ok=True) |
| 114 | sk = TEST_KEYS |
| 115 | with open(os.path.join(path, "keys", "coordinator-public-keys.txt"), "w") as f: |
| 116 | f.write("submitter.p256 %s\nverifier.p256 %s\n" % (pub(sk["submitter"]).hex(), pub(sk["verifier"]).hex())) |
| 117 | f.write("coordinator.ed25519 897507c5dc043871fc4592c7c723a6bf4d90d94cf56d2db8dc71bf79e283b8c6\n") |
| 118 | with open(os.path.join(path, "keys", "worker-public-keys.txt"), "w") as f: |
| 119 | f.write("executor.p256 %s\n" % pub(sk["executor"]).hex()) |
| 120 | f.write("volunteer.ed25519 058df02bc0cf534602497686ecb8e4ae6ae43d4dbb44fbf5fcb1a8c37a2ac1bd\n") |
| 121 | with open(os.path.join(path, "pins", "executor.p256"), "w") as f: |
| 122 | f.write(pub(sk["executor"]).hex() + "\n") |
| 123 | with open(os.path.join(path, "pins", "volunteer.ed25519"), "w") as f: |
| 124 | f.write("058df02bc0cf534602497686ecb8e4ae6ae43d4dbb44fbf5fcb1a8c37a2ac1bd\n") |
| 125 | with open(os.path.join(path, "runtime.env"), "w") as f: |
| 126 | f.write("RECOMPUTE_SHA256=d2cd7382e0dc64e49498ae3b1ceffc73f1ddcbd3ddcf93e6d607498ccd58ea12\n") |
| 127 | with open(os.path.join(path, "request.en"), "w") as f: |
| 128 | f.write(request) |
| 129 | with open(os.path.join(path, "goal.oe45"), "w") as f: |
| 130 | f.write(lines[0] + "\n") |
| 131 | with open(os.path.join(path, "episode.oe45"), "w") as f: |
| 132 | f.write("oe45 v1\n" + "".join(l + "\n" for l in lines)) |
| 133 | rows = [HEADER] |
| 134 | m, k, n = dims |
| 135 | dispatch = [] |
| 136 | seen = ["AIENET0-STATE-V1"] |
| 137 | for ix, (job, attempt, cls, code) in enumerate(outcomes): |
| 138 | seed = seeds[job - 1] |
| 139 | if cls == "ok" or code == "replay": |
| 140 | dag, an0b, mjr1, mjv = triple(m, k, n, seed, b"%d" % attempt, forge_output) |
| 141 | else: |
| 142 | # Transport never executes. This test-only log id needs no output |
| 143 | # recompute or signature; successful rows still get full triples. |
| 144 | dag = hashlib.sha256(repr((m, k, n, seed, attempt)).encode("ascii")).hexdigest() |
| 145 | if cls == "ok": |
| 146 | for ext, b in ((".an0b", an0b), (".mjr1", mjr1), (".mjv", mjv)): |
| 147 | with open(os.path.join(path, "artifacts", dag + ext), "wb") as f: |
| 148 | f.write(b) |
| 149 | rows.append("\t".join(map(str, [job, attempt, m, k, n, seed, 0, "ok", "-", dag, |
| 150 | A.sha(mjr1).hex(), 1, pub(sk["verifier"]).hex()]))) |
| 151 | err = "aienet0-run: dag_id=%s\naienet0-run: receipt sha256=%s\n" % (dag, A.sha(mjr1).hex()) |
| 152 | exit_code = 0 |
| 153 | else: |
| 154 | exit_code = CODE_EXIT[code] |
| 155 | rows.append("\t".join(map(str, [job, attempt, m, k, n, seed, exit_code, cls, code, |
| 156 | dag, "-", "-", "-"]))) |
| 157 | marker = {"transport": "no volunteer connected: fixture", |
| 158 | "replay": "REFUSED WORKER AN0_REPLAY", |
| 159 | "budget": "REFUSED WORKER AN0_BUDGET", "worker": "REFUSED WORKER", |
| 160 | "receipt": "REFUSED AN0_RECEIPT", "recomp": "REFUSED AN0_RECOMPUTE", |
| 161 | "verdict": "REFUSED AN0_VERDICT", "io": "REFUSED AN0_OUTPUT_EXISTS"}[code] |
| 162 | err = "aienet0-run: dag_id=%s\naienet0-run: %s\n" % (dag, marker) |
| 163 | if code == "replay": |
| 164 | seen.append("done %s %s %s %s" % (dag, A.sha(mjr1).hex(), pub(sk["executor"]).hex(), mjr1.hex())) |
| 165 | open(os.path.join(path, "logs", "fixture-%d-run.err" % ix), "w").write(err) |
| 166 | dispatch.append("outcome %d %d %d %s serve=0" % (job, attempt, exit_code, dag)) |
| 167 | open(os.path.join(path, "dispatch.log"), "w").write("\n".join(dispatch) + "\n") |
| 168 | open(os.path.join(path, "worker", "seen"), "w").write("\n".join(seen) + "\n") |
| 169 | with open(os.path.join(path, "facts.tsv"), "w") as f: |
| 170 | f.write("\n".join(rows) + "\n") |
| 171 | if response is not None: |
| 172 | with open(os.path.join(path, "response.en"), "w") as f: |
| 173 | f.write(response) |