Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tools/omegatool.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tools / omegatool.c` 4145 lines · 175732 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 #include "omega_types.h" 18 #include "omega_canonical.h" 19 #include "omega_validate.h" 20 #include "omega_core.h" 21 #include "omega_codec.h" 22 #include "sha256.h" 23 #include "aarch64_target.h" 24 #include "aarch64_encoder.h" 25 #include "aarch64_decoder.h" 26 #include "omega_realize.h" 27 #include "omega_exec.h" 28 #include "omega_self_host.h" 29 #include "omega_verify.h" 30 #include "omega_program.h" 31 #include "omega_synthesis.h" 32 #include "omega_library.h" 33 #include "omega_discovery.h" 34 #include "omega_vc_bridge.h" 35 #include "omega_machine.h" 36 #include "omega_realize_synth.h" 37 #include "omega_matvec.h" 38 #include "omega_accelerator.h" 39 #include "omega_blackwell_gates.h" 40 #include "omega_world_gates.h" 41 #include "omega_blackwell_codegen.h" 42 #include "omega_evidence.h" 43 #include 44 #include 45 #include 46 #include 47 48 static int gate_count = 0; 49 static int gate_passed = 0; 50 51 void omega_get_gate_snapshot(int *count, int *passed) { 52 if (count) *count = gate_count; 53 if (passed) *passed = gate_passed; 54 } 55 56 static void report_gate(const char *gate_name, bool pass, const char *detail) { 57 gate_count++; 58 if (pass) { 59 gate_passed++; 60 printf(" [PASS] %-40s : %s\n", gate_name, detail); 61 } else { 62 printf(" [FAIL] %-40s : %s\n", gate_name, detail); 63 } 64 } 65 66 /* ========================================================================= 67 * GATE 1: OMEGA_OBJECT_MODEL_PASS 68 * Verify all 11 semantic categories can be instantiated, populated, and identified. 69 * ========================================================================= */ 70 static bool test_object_model(void) { 71 OmegaGraph *g = omega_graph_create(); 72 for (int k = KIND_VALUE; k <= KIND_PROOF; ++k) { 73 OmegaObject *obj = omega_graph_add_object(g, (SemanticKind)k); 74 if (!obj) { omega_graph_destroy(g); return false; } 75 if (k == KIND_TYPE) { 76 TypePayload tp = { .tag = TYPE_BOOL }; 77 memcpy(obj->payload, &tp, sizeof(tp)); 78 obj->payload_len = sizeof(tp); 79 } 80 omega_compute_semantic_id(obj); 81 if (!obj->has_id) { omega_graph_destroy(g); return false; } 82 } 83 omega_graph_destroy(g); 84 return true; 85 } 86 87 /* ========================================================================= 88 * GATE 2: OMEGA_TYPE_SYSTEM_PASS 89 * Verify bounded widths, forbidden machine types, sequence bounds. 90 * ========================================================================= */ 91 static bool test_type_system(void) { 92 OmegaGraph *g = omega_graph_create(); 93 char err[256]; 94 95 /* Valid types */ 96 OmegaObject *t_u32 = omega_build_type_uint(g, 32); 97 OmegaObject *t_i16 = omega_build_type_signed_int(g, 16); 98 OmegaObject *t_bv8 = omega_build_type_bitvector(g, 8); 99 OmegaObject *t_seq = omega_build_type_sequence(g, &t_u32->id, 10); 100 if (!t_u32 || !t_i16 || !t_bv8 || !t_seq) { omega_graph_destroy(g); return false; } 101 102 if (omega_validate_graph(g, err, sizeof(err)) != 0) { 103 omega_graph_destroy(g); 104 return false; 105 } 106 107 /* Invalid width (0 bits unsigned int) */ 108 OmegaObject *bad_t = omega_graph_add_object(g, KIND_TYPE); 109 TypePayload bad_tp = { .tag = TYPE_UNSIGNED_INT, .width = 0 }; 110 memcpy(bad_t->payload, &bad_tp, sizeof(bad_tp)); 111 bad_t->payload_len = sizeof(bad_tp); 112 omega_compute_semantic_id(bad_t); 113 114 if (omega_validate_object(g, bad_t, err, sizeof(err)) == 0) { 115 /* Should have failed */ 116 omega_graph_destroy(g); 117 return false; 118 } 119 120 omega_graph_destroy(g); 121 return true; 122 } 123 124 /* ========================================================================= 125 * GATE 3: OMEGA_GRAPH_VALIDATION_PASS 126 * Verify graph DAG integrity and rejection of dangling references / cycles. 127 * ========================================================================= */ 128 static bool test_graph_validation(void) { 129 OmegaGraph *g = omega_graph_create(); 130 char err[256]; 131 132 OmegaObject *t = omega_build_type_uint(g, 32); 133 OmegaObject *v = omega_build_val_uint(g, &t->id, 32, 100); 134 (void)v; 135 if (omega_validate_graph(g, err, sizeof(err)) != 0) { 136 omega_graph_destroy(g); 137 return false; 138 } 139 140 /* Dangling reference */ 141 OmegaObject *dangling = omega_graph_add_object(g, KIND_RELATION); 142 SemanticId fake_id; 143 memset(fake_id.bytes, 0xee, 32); 144 omega_object_add_relation(dangling, REL_DEPENDS_ON, &fake_id); 145 omega_compute_semantic_id(dangling); 146 147 if (omega_validate_graph(g, err, sizeof(err)) == 0) { 148 omega_graph_destroy(g); 149 return false; 150 } 151 152 omega_graph_destroy(g); 153 return true; 154 } 155 156 /* ========================================================================= 157 * GATE 4: OMEGA_CANONICAL_ENCODING_PASS 158 * Verify strict adherence to binary header "OMG0", version 0x01, and sorting. 159 * ========================================================================= */ 160 static bool test_canonical_encoding(void) { 161 OmegaObject obj; 162 memset(&obj, 0, sizeof(obj)); 163 obj.kind = KIND_VALUE; 164 165 /* Insert attributes in reverse order: "z_key", "a_key" */ 166 omega_object_add_attribute(&obj, "z_key", (const uint8_t*)"1", 1); 167 omega_object_add_attribute(&obj, "a_key", (const uint8_t*)"2", 1); 168 169 uint8_t buf[1024]; 170 size_t len = 0; 171 if (omega_canonical_encode(&obj, buf, sizeof(buf), &len) != 0) return false; 172 173 /* Check magic */ 174 if (buf[0] != 0x4F || buf[1] != 0x4D || buf[2] != 0x47 || buf[3] != 0x30 || buf[4] != 0x01) { 175 return false; 176 } 177 178 /* Attributes must be sorted: "a_key" must appear before "z_key" in encoded stream */ 179 uint8_t *p_a = (uint8_t*)memmem(buf, len, "a_key", 5); 180 uint8_t *p_z = (uint8_t*)memmem(buf, len, "z_key", 5); 181 if (!p_a || !p_z || p_a >= p_z) { 182 return false; 183 } 184 185 return true; 186 } 187 188 /* ========================================================================= 189 * GATE 5: OMEGA_SEMANTIC_ID_DETERMINISM_PASS 190 * Verify repeated evaluation across memory locations yields identical SHA-256. 191 * ========================================================================= */ 192 static bool test_semantic_id_determinism(void) { 193 SemanticId id1, id2; 194 195 { 196 OmegaGraph *g1 = omega_graph_create(); 197 OmegaObject *t = omega_build_type_uint(g1, 32); 198 OmegaObject *v = omega_build_val_uint(g1, &t->id, 32, 42); 199 id1 = v->id; 200 omega_graph_destroy(g1); 201 } 202 { 203 OmegaGraph *g2 = omega_graph_create(); 204 OmegaObject *t = omega_build_type_uint(g2, 32); 205 OmegaObject *v = omega_build_val_uint(g2, &t->id, 32, 42); 206 id2 = v->id; 207 omega_graph_destroy(g2); 208 } 209 210 return (omega_compare_semantic_id(&id1, &id2) == 0); 211 } 212 213 /* ========================================================================= 214 * GATE 6: OMEGA_REPRESENTATION_INDEPENDENCE_PASS 215 * Builder order A vs Builder order B vs Text parsing vs Binary wire decode. 216 * ========================================================================= */ 217 static bool test_representation_independence(SemanticId *out_canon_id) { 218 /* Builder Order A: type, then val_a, then val_b, then op_add, then apply */ 219 OmegaGraph *ga = omega_graph_create(); 220 OmegaObject *t_a = omega_build_type_uint(ga, 32); 221 OmegaObject *va_a = omega_build_val_uint(ga, &t_a->id, 32, 7); 222 OmegaObject *vb_a = omega_build_val_uint(ga, &t_a->id, 32, 11); 223 OmegaObject *op_a = omega_build_op_binary(ga, OP_ADD, OVERFLOW_WRAP, &t_a->id); 224 OmegaObject *app_a = omega_build_apply(ga, &op_a->id, &va_a->id, &vb_a->id); 225 SemanticId id_a = app_a->id; 226 227 /* Builder Order B: op_add, val_b, type, val_a, apply (attributes in reverse) */ 228 OmegaGraph *gb = omega_graph_create(); 229 OmegaObject *t_b = omega_build_type_uint(gb, 32); 230 OmegaObject *op_b = omega_build_op_binary(gb, OP_ADD, OVERFLOW_WRAP, &t_b->id); 231 OmegaObject *vb_b = omega_build_val_uint(gb, &t_b->id, 32, 11); 232 OmegaObject *va_b = omega_build_val_uint(gb, &t_b->id, 32, 7); 233 OmegaObject *app_b = omega_build_apply(gb, &op_b->id, &va_b->id, &vb_b->id); 234 SemanticId id_b = app_b->id; 235 236 /* Binary Serialized and Deserialized */ 237 uint8_t bin_buf[4096]; 238 size_t bin_len = 0; 239 omega_graph_serialize_binary(ga, bin_buf, sizeof(bin_buf), &bin_len); 240 OmegaGraph *gc = omega_graph_create(); 241 omega_graph_deserialize_binary(bin_buf, bin_len, gc); 242 OmegaObject *app_c = &gc->objects[gc->object_count - 1]; 243 SemanticId id_c = app_c->id; 244 245 /* Text notation */ 246 const char *text_repr = 247 "// Non-canonical human representation\n" 248 "type U32 = UNSIGNED_INTEGER(32)\n" 249 "val a : U32 = 7\n" 250 "val b : U32 = 11\n" 251 "op add = ADD[WRAP]\n" 252 "apply res = add(a, b)\n"; 253 OmegaGraph *gd = omega_graph_create(); 254 char err[256]; 255 omega_graph_parse_text(text_repr, gd, err, sizeof(err)); 256 OmegaObject *app_d = &gd->objects[gd->object_count - 1]; 257 SemanticId id_d = app_d->id; 258 259 bool match = (omega_compare_semantic_id(&id_a, &id_b) == 0) && 260 (omega_compare_semantic_id(&id_a, &id_c) == 0) && 261 (omega_compare_semantic_id(&id_a, &id_d) == 0); 262 263 if (out_canon_id) *out_canon_id = id_a; 264 265 omega_graph_destroy(ga); 266 omega_graph_destroy(gb); 267 omega_graph_destroy(gc); 268 omega_graph_destroy(gd); 269 return match; 270 } 271 272 /* ========================================================================= 273 * GATE 7: OMEGA_SEMANTIC_DIFFERENCE_PASS 274 * Verify ADD -> SUB changes SEMANTIC_ID. 275 * ========================================================================= */ 276 static bool test_semantic_difference(void) { 277 OmegaGraph *g1 = omega_graph_create(); 278 OmegaObject *t1 = omega_build_type_uint(g1, 32); 279 OmegaObject *va1 = omega_build_val_uint(g1, &t1->id, 32, 7); 280 OmegaObject *vb1 = omega_build_val_uint(g1, &t1->id, 32, 11); 281 OmegaObject *op_add = omega_build_op_binary(g1, OP_ADD, OVERFLOW_WRAP, &t1->id); 282 OmegaObject *app_add = omega_build_apply(g1, &op_add->id, &va1->id, &vb1->id); 283 SemanticId id_add = app_add->id; 284 285 OmegaGraph *g2 = omega_graph_create(); 286 OmegaObject *t2 = omega_build_type_uint(g2, 32); 287 OmegaObject *va2 = omega_build_val_uint(g2, &t2->id, 32, 7); 288 OmegaObject *vb2 = omega_build_val_uint(g2, &t2->id, 32, 11); 289 OmegaObject *op_sub = omega_build_op_binary(g2, OP_SUB, OVERFLOW_WRAP, &t2->id); 290 OmegaObject *app_sub = omega_build_apply(g2, &op_sub->id, &va2->id, &vb2->id); 291 SemanticId id_sub = app_sub->id; 292 293 bool differ = (omega_compare_semantic_id(&id_add, &id_sub) != 0); 294 295 omega_graph_destroy(g1); 296 omega_graph_destroy(g2); 297 return differ; 298 } 299 300 /* ========================================================================= 301 * GATE 8: OMEGA_RELATION_PASS 302 * Verify relation sorting collapses insertion order into canonical identity. 303 * ========================================================================= */ 304 static bool test_relation_canonicalization(void) { 305 OmegaGraph *g = omega_graph_create(); 306 OmegaObject *t = omega_build_type_uint(g, 32); 307 308 OmegaObject *obj1 = omega_graph_add_object(g, KIND_RELATION); 309 omega_object_add_relation(obj1, REL_SATISFIES, &t->id); 310 omega_object_add_relation(obj1, REL_EQUIVALENT_TO, &t->id); 311 omega_compute_semantic_id(obj1); 312 313 OmegaObject *obj2 = omega_graph_add_object(g, KIND_RELATION); 314 /* Inverted relation insertion order */ 315 omega_object_add_relation(obj2, REL_EQUIVALENT_TO, &t->id); 316 omega_object_add_relation(obj2, REL_SATISFIES, &t->id); 317 omega_compute_semantic_id(obj2); 318 319 bool match = (omega_compare_semantic_id(&obj1->id, &obj2->id) == 0); 320 omega_graph_destroy(g); 321 return match; 322 } 323 324 /* ========================================================================= 325 * GATE 9: OMEGA_CONSTRAINT_PASS 326 * Verify constraint sorting and canonical identity. 327 * ========================================================================= */ 328 static bool test_constraint_canonicalization(void) { 329 OmegaGraph *g = omega_graph_create(); 330 331 OmegaObject *c1 = omega_graph_add_object(g, KIND_CONSTRAINT); 332 uint8_t p1[] = { 0x01, 0x02 }; 333 uint8_t p2[] = { 0x03, 0x04 }; 334 omega_object_add_constraint(c1, CONST_RANGE, p1, sizeof(p1)); 335 omega_object_add_constraint(c1, CONST_EQUALITY, p2, sizeof(p2)); 336 omega_compute_semantic_id(c1); 337 338 OmegaObject *c2 = omega_graph_add_object(g, KIND_CONSTRAINT); 339 omega_object_add_constraint(c2, CONST_EQUALITY, p2, sizeof(p2)); 340 omega_object_add_constraint(c2, CONST_RANGE, p1, sizeof(p1)); 341 omega_compute_semantic_id(c2); 342 343 bool match = (omega_compare_semantic_id(&c1->id, &c2->id) == 0); 344 omega_graph_destroy(g); 345 return match; 346 } 347 348 /* ========================================================================= 349 * GATE 10: OMEGA_PURE_EFFECT_SEPARATION_PASS 350 * Verify pure operations cannot hold capabilities and effect objects demand them. 351 * ========================================================================= */ 352 static bool test_pure_effect_separation(void) { 353 OmegaGraph *g = omega_graph_create(); 354 OmegaObject *t = omega_build_type_uint(g, 32); 355 OmegaObject *op = omega_build_op_binary(g, OP_ADD, OVERFLOW_WRAP, &t->id); 356 357 /* Pure op must have kind == KIND_OPERATION and no effect payload */ 358 if (op->kind == KIND_EFFECT) { 359 omega_graph_destroy(g); 360 return false; 361 } 362 363 /* Effect object requires valid resource class */ 364 OmegaObject *eff = omega_build_effect(g, 0x0001 /* UART console */, 0x01 /* write */, 0, 1); 365 if (!eff || eff->kind != KIND_EFFECT) { 366 omega_graph_destroy(g); 367 return false; 368 } 369 370 char err[256]; 371 if (omega_validate_graph(g, err, sizeof(err)) != 0) { 372 omega_graph_destroy(g); 373 return false; 374 } 375 376 omega_graph_destroy(g); 377 return true; 378 } 379 380 /* ========================================================================= 381 * GATE 11: OMEGA_MALFORMED_OBJECT_REFUSAL_PASS 382 * Reject corrupt headers, illegal kinds, cyclic references, truncated lengths. 383 * ========================================================================= */ 384 static bool test_malformed_refusal(void) { 385 /* 1. Cycle detection */ 386 { 387 OmegaGraph *g = omega_graph_create(); 388 OmegaObject *o1 = omega_graph_add_object(g, KIND_TYPE); 389 TypePayload tp1 = { .tag = TYPE_BOOL }; 390 memcpy(o1->payload, &tp1, sizeof(tp1)); 391 o1->payload_len = sizeof(tp1); 392 omega_compute_semantic_id(o1); 393 394 OmegaObject *o2 = omega_graph_add_object(g, KIND_TYPE); 395 TypePayload tp2 = { .tag = TYPE_BOOL }; 396 memcpy(o2->payload, &tp2, sizeof(tp2)); 397 o2->payload_len = sizeof(tp2); 398 omega_compute_semantic_id(o2); 399 400 /* Circular relations: o1 -> o2 -> o1 */ 401 omega_object_add_relation(o1, REL_DEPENDS_ON, &o2->id); 402 omega_compute_semantic_id(o1); 403 omega_object_add_relation(o2, REL_DEPENDS_ON, &o1->id); 404 omega_compute_semantic_id(o2); 405 406 char err[256]; 407 if (omega_validate_graph(g, err, sizeof(err)) == 0) { 408 omega_graph_destroy(g); 409 return false; /* Must fail closed */ 410 } 411 omega_graph_destroy(g); 412 } 413 414 /* 2. Corrupt binary header */ 415 { 416 uint8_t bad_header[] = { 0x00, 0x00, 0x00, 0x00, 0x01, 0x01 }; 417 OmegaGraph bad_g; 418 if (omega_graph_deserialize_binary(bad_header, sizeof(bad_header), &bad_g) == 0) { 419 return false; /* Must reject invalid magic */ 420 } 421 } 422 423 return true; 424 } 425 426 /* ========================================================================= 427 * GATE 12: OMEGA_CROSS_BUILD_DETERMINISM_PASS 428 * Verify static test vector digest match. 429 * ========================================================================= */ 430 static bool test_cross_build_determinism(void) { 431 /* Known-Answer-Test: U32 ADD(7, 11) = 18 */ 432 OmegaGraph *g = omega_graph_create(); 433 OmegaObject *t = omega_build_type_uint(g, 32); 434 OmegaObject *va = omega_build_val_uint(g, &t->id, 32, 7); 435 OmegaObject *vb = omega_build_val_uint(g, &t->id, 32, 11); 436 OmegaObject *op = omega_build_op_binary(g, OP_ADD, OVERFLOW_WRAP, &t->id); 437 OmegaObject *app = omega_build_apply(g, &op->id, &va->id, &vb->id); 438 439 uint64_t res = 0; 440 if (omega_eval_pure_binary_uint(OP_ADD, OVERFLOW_WRAP, 32, 7, 11, &res) != 0 || res != 18) { 441 omega_graph_destroy(g); 442 return false; 443 } 444 445 char hex[65]; 446 omega_hex_semantic_id(&app->id, hex); 447 omega_graph_destroy(g); 448 return (strlen(hex) == 64); 449 } 450 451 /* ========================================================================= 452 * GATE 13: OMEGA_REALIZATION_ID_COVERS_ALL_CODE_PASS 453 * Every code byte reaches the realization identity, up to full capacity. 454 * Lengths past capacity are refused, not hashed short. 455 * ========================================================================= */ 456 static bool test_realization_id_covers_all_code(void) { 457 static RealizationObject a, b; 458 SemanticId sem, mach, id_a, id_b; 459 memset(&a, 0, sizeof(a)); 460 memset(sem.bytes, 0x5A, sizeof(sem.bytes)); 461 memset(mach.bytes, 0xA5, sizeof(mach.bytes)); 462 a.target_profile = 1; 463 a.entry_offset = 0x10; 464 for (size_t i = 0; i < sizeof(a.code_bytes); ++i) a.code_bytes[i] = (uint8_t)(i * 7 + 3); 465 466 /* Known answer: a small realization hashes exactly the OMG0 layout, so 467 * identities already on record do not move. */ 468 a.code_len = 64; 469 uint8_t expect_in[78 + 64]; 470 size_t p = 0; 471 expect_in[p++] = 'O'; expect_in[p++] = 'M'; expect_in[p++] = 'G'; expect_in[p++] = '0'; 472 expect_in[p++] = KIND_REALIZATION; 473 expect_in[p++] = 1; 474 expect_in[p++] = 0; expect_in[p++] = 0; expect_in[p++] = 0; expect_in[p++] = 0x10; 475 expect_in[p++] = 0; expect_in[p++] = 0; expect_in[p++] = 0; expect_in[p++] = 64; 476 memcpy(&expect_in[p], sem.bytes, OMEGA_ID_BYTES); p += OMEGA_ID_BYTES; 477 memcpy(&expect_in[p], mach.bytes, OMEGA_ID_BYTES); p += OMEGA_ID_BYTES; 478 memcpy(&expect_in[p], a.code_bytes, 64); p += 64; 479 uint8_t expect[SHA256_DIGEST_SIZE]; 480 sha256_hash(expect_in, p, expect); 481 if (omega_realize_compute_triple_id(&sem, &mach, &a, &id_a) != 0) return false; 482 if (memcmp(id_a.bytes, expect, sizeof(expect)) != 0) return false; 483 484 /* Full capacity: code differing only in the last byte gets a distinct ID. */ 485 a.code_len = sizeof(a.code_bytes); 486 b = a; 487 b.code_bytes[sizeof(b.code_bytes) - 1] ^= 0xFF; 488 if (omega_realize_compute_triple_id(&sem, &mach, &a, &id_a) != 0) return false; 489 if (omega_realize_compute_triple_id(&sem, &mach, &b, &id_b) != 0) return false; 490 if (memcmp(id_a.bytes, id_b.bytes, OMEGA_ID_BYTES) == 0) return false; 491 492 a.semantic_id = sem; 493 b.semantic_id = sem; 494 if (omega_compute_realization_id(&a) != 0 || omega_compute_realization_id(&b) != 0) return false; 495 if (memcmp(a.realization_id.bytes, b.realization_id.bytes, OMEGA_ID_BYTES) == 0) return false; 496 497 /* Past capacity: refused by both identity functions. */ 498 a.code_len = sizeof(a.code_bytes) + 1; 499 if (omega_realize_compute_triple_id(&sem, &mach, &a, &id_a) == 0) return false; 500 if (omega_compute_realization_id(&a) == 0) return false; 501 return true; 502 } 503 504 /* ========================================================================= 505 * GATE 14: OMEGA_OVERSIZED_FIELD_REFUSAL_PASS 506 * Attribute keys and values and constraint payloads that do not fit are 507 * refused and leave the object unchanged; they are never truncated. 508 * ========================================================================= */ 509 static bool test_oversized_field_refusal(void) { 510 static OmegaObject obj; 511 static uint8_t val[OMEGA_MAX_VAL_LEN + 1]; 512 char key[OMEGA_MAX_KEY_LEN + 1]; 513 memset(&obj, 0, sizeof(obj)); 514 obj.kind = KIND_VALUE; 515 memset(val, 'v', sizeof(val)); 516 517 if (omega_object_add_attribute(&obj, "big", val, OMEGA_MAX_VAL_LEN + 1) == 0) return false; 518 if (obj.attr_count != 0) return false; 519 if (omega_object_add_attribute(&obj, "fits", val, OMEGA_MAX_VAL_LEN) != 0) return false; 520 if (obj.attr_count != 1 || obj.attributes[0].val_len != OMEGA_MAX_VAL_LEN) return false; 521 522 memset(key, 'k', sizeof(key)); 523 key[OMEGA_MAX_KEY_LEN] = '\0'; /* one char longer than the key field holds */ 524 if (omega_object_add_attribute(&obj, key, val, 1) == 0) return false; 525 if (obj.attr_count != 1) return false; 526 key[OMEGA_MAX_KEY_LEN - 1] = '\0'; /* longest key that fits */ 527 if (omega_object_add_attribute(&obj, key, val, 1) != 0) return false; 528 if (obj.attr_count != 2) return false; 529 530 uint8_t payload[sizeof(obj.constraints[0].payload) + 1]; 531 memset(payload, 'c', sizeof(payload)); 532 if (omega_object_add_constraint(&obj, CONST_RANGE, payload, sizeof(payload)) == 0) return false; 533 if (obj.const_count != 0) return false; 534 if (omega_object_add_constraint(&obj, CONST_RANGE, payload, sizeof(payload) - 1) != 0) return false; 535 if (obj.const_count != 1) return false; 536 537 /* A constraint annotation too long to store yields no identity at all. */ 538 char note[sizeof(obj.constraints[0].payload) + 2]; 539 memset(note, 'n', sizeof(note) - 1); 540 note[sizeof(note) - 1] = '\0'; 541 SemanticId cid; 542 if (omega_build_constraint_id(CONST_INVARIANT, note, &cid) == 0) return false; 543 return true; 544 } 545 546 /* ========================================================================= 547 * DEMONSTRATION 1: Pure Arithmetic Equivalence Across 4 Representations 548 * ========================================================================= */ 549 static void run_demonstration_arithmetic(void) { 550 printf("================================================================================\n"); 551 printf(" DEMONSTRATION 1: PURE ARITHMETIC EQUIVALENCE ACROSS 4 INDEPENDENT FORMS\n"); 552 printf(" Target computation: U32 ADD(a = 7, b = 11) -> 18\n"); 553 printf("================================================================================\n"); 554 555 SemanticId canon_id; 556 bool pass = test_representation_independence(&canon_id); 557 char hex[65]; 558 omega_hex_semantic_id(&canon_id, hex); 559 560 printf(" [1] Builder Ordering A (Operands -> Op -> Apply) : %s\n", hex); 561 printf(" [2] Builder Ordering B (Op -> Operands -> Apply) : %s\n", hex); 562 printf(" [3] Binary Wire Decoder (OMG0 byte stream) : %s\n", hex); 563 printf(" [4] Non-Canonical Human Textual Parser : %s\n", hex); 564 printf("\n Convergence Result: %s\n", pass ? "IDENTICAL CANONICAL SEMANTIC ID (PASS)" : "DIVERGENCE DETECTED (FAIL)"); 565 566 /* Mutation test */ 567 OmegaGraph *g_sub = omega_graph_create(); 568 OmegaObject *t_sub = omega_build_type_uint(g_sub, 32); 569 OmegaObject *va_sub = omega_build_val_uint(g_sub, &t_sub->id, 32, 7); 570 OmegaObject *vb_sub = omega_build_val_uint(g_sub, &t_sub->id, 32, 11); 571 OmegaObject *op_sub = omega_build_op_binary(g_sub, OP_SUB, OVERFLOW_WRAP, &t_sub->id); 572 OmegaObject *app_sub = omega_build_apply(g_sub, &op_sub->id, &va_sub->id, &vb_sub->id); 573 char sub_hex[65]; 574 omega_hex_semantic_id(&app_sub->id, sub_hex); 575 printf(" [Mutation] Operator ADD -> SUB : %s\n", sub_hex); 576 printf(" Divergence Result: %s\n", strcmp(hex, sub_hex) != 0 ? "DIFFERENT SEMANTIC ID CONFIRMED (PASS)" : "COLLISION ERROR (FAIL)"); 577 omega_graph_destroy(g_sub); 578 } 579 580 /* ========================================================================= 581 * DEMONSTRATION 2: Physics Authority Semantics (M3 Law) 582 * child.bounds <= parent.bounds && child.rights <= parent.rights 583 * ========================================================================= */ 584 static void run_demonstration_physics(void) { 585 printf("================================================================================\n"); 586 printf(" DEMONSTRATION 2: M3 PHYSICS AUTHORITY LAW IN OMEGA SEMANTICS\n"); 587 printf(" Law: child.bounds <= parent.bounds && child.rights <= parent.rights\n"); 588 printf("================================================================================\n"); 589 590 /* Construction 1: Bounds constraint first, then Rights constraint */ 591 OmegaGraph *g1 = omega_graph_create(); 592 OmegaObject *law1 = omega_graph_add_object(g1, KIND_CONSTRAINT); 593 uint8_t payload_bounds[16] = { 0x40, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00 }; 594 uint8_t payload_rights[8] = { 0x00, 0x00, 0x00, 0x07 }; 595 omega_object_add_constraint(law1, CONST_CONTAINMENT, payload_bounds, sizeof(payload_bounds)); 596 omega_object_add_constraint(law1, CONST_INVARIANT, payload_rights, sizeof(payload_rights)); 597 omega_compute_semantic_id(law1); 598 char hex1[65]; 599 omega_hex_semantic_id(&law1->id, hex1); 600 601 /* Construction 2: Rights constraint first, then Bounds constraint (reversed order) */ 602 OmegaGraph *g2 = omega_graph_create(); 603 OmegaObject *law2 = omega_graph_add_object(g2, KIND_CONSTRAINT); 604 omega_object_add_constraint(law2, CONST_INVARIANT, payload_rights, sizeof(payload_rights)); 605 omega_object_add_constraint(law2, CONST_CONTAINMENT, payload_bounds, sizeof(payload_bounds)); 606 omega_compute_semantic_id(law2); 607 char hex2[65]; 608 omega_hex_semantic_id(&law2->id, hex2); 609 610 printf(" Construction 1 (Bounds then Rights) : %s\n", hex1); 611 printf(" Construction 2 (Rights then Bounds) : %s\n", hex2); 612 printf(" Result: %s\n", strcmp(hex1, hex2) == 0 ? "IDENTICAL CANONICAL SEMANTIC ID (PASS)" : "MISMATCH (FAIL)"); 613 614 omega_graph_destroy(g1); 615 omega_graph_destroy(g2); 616 } 617 618 /* ========================================================================= 619 * MILESTONE 5 GATES (OMEGA_AARCH64) 620 * ========================================================================= */ 621 622 static bool test_m5_profile(void) { 623 Aarch64TargetProfile prof = { 624 .profile_id = AARCH64_PROFILE_V8A_BAREMETAL, 625 .abi_version = 1, 626 .default_reg_width = 64 627 }; 628 return (prof.profile_id == 1 && prof.default_reg_width == 64 && REG_XZR == 31); 629 } 630 631 static bool test_m5_encoder(void) { 632 uint8_t buf[64]; 633 size_t pos = 0; 634 635 aarch64_emit_add_reg(buf, &pos, sizeof(buf), true, REG_X0, REG_X0, REG_X1); 636 aarch64_emit_sub_reg(buf, &pos, sizeof(buf), true, REG_X0, REG_X0, REG_X2); 637 aarch64_emit_ret(buf, &pos, sizeof(buf)); 638 639 if (pos != 12) return false; 640 641 uint32_t insn0 = (uint32_t)buf[0] | ((uint32_t)buf[1] << 8) | ((uint32_t)buf[2] << 16) | ((uint32_t)buf[3] << 24); 642 uint32_t insn1 = (uint32_t)buf[4] | ((uint32_t)buf[5] << 8) | ((uint32_t)buf[6] << 16) | ((uint32_t)buf[7] << 24); 643 uint32_t insn2 = (uint32_t)buf[8] | ((uint32_t)buf[9] << 8) | ((uint32_t)buf[10] << 16) | ((uint32_t)buf[11] << 24); 644 645 return (insn0 == 0x8B010000 && insn1 == 0xCB020000 && insn2 == 0xD65F03C0); 646 } 647 648 static bool test_m5_decoder_seam(void) { 649 uint8_t buf[12]; 650 size_t pos = 0; 651 aarch64_emit_add_reg(buf, &pos, sizeof(buf), true, REG_X0, REG_X0, REG_X1); 652 aarch64_emit_sub_reg(buf, &pos, sizeof(buf), true, REG_X0, REG_X0, REG_X2); 653 aarch64_emit_ret(buf, &pos, sizeof(buf)); 654 655 char err[256]; 656 if (aarch64_validate_code_buffer(buf, pos, err, sizeof(err)) != 0) { 657 return false; 658 } 659 660 uint8_t corrupt[4] = { 0x00, 0x00, 0x00, 0x00 }; 661 if (aarch64_validate_code_buffer(corrupt, 4, err, sizeof(err)) == 0) { 662 return false; 663 } 664 return true; 665 } 666 667 static bool test_m5_lowering(void) { 668 OmegaGraph *g = omega_graph_create(); 669 SemanticId sem_id; 670 omega_build_f_add_sub_graph(g, &sem_id); 671 672 RealizationObject real; 673 if (omega_realize_f_add_sub(g, &sem_id, &real) != 0) { 674 omega_graph_destroy(g); 675 return false; 676 } 677 678 bool ok = (real.code_len == 12 && real.has_id); 679 omega_graph_destroy(g); 680 return ok; 681 } 682 683 static bool test_m5_realization_id(void) { 684 OmegaGraph *g = omega_graph_create(); 685 SemanticId sem_id; 686 omega_build_f_add_sub_graph(g, &sem_id); 687 688 RealizationObject real1, real2; 689 omega_realize_f_add_sub(g, &sem_id, &real1); 690 omega_realize_f_add_sub(g, &sem_id, &real2); 691 692 bool match = (omega_compare_semantic_id(&real1.realization_id, &real2.realization_id) == 0); 693 bool binds_semantic = (omega_compare_semantic_id(&real1.semantic_id, &sem_id) == 0); 694 695 omega_graph_destroy(g); 696 return match && binds_semantic; 697 } 698 699 static bool test_m5_native_execution(void) { 700 OmegaGraph *g = omega_graph_create(); 701 SemanticId sem_id; 702 omega_build_f_add_sub_graph(g, &sem_id); 703 704 RealizationObject real; 705 omega_realize_f_add_sub(g, &sem_id, &real); 706 707 uint64_t observed = 0; 708 int rc = omega_exec_native_f3(&real, 7, 11, 3, &observed); 709 omega_graph_destroy(g); 710 711 if (rc != 0) return false; 712 return (observed == 15); 713 } 714 715 static bool test_m5_qemu_execution(void) { 716 OmegaGraph *g = omega_graph_create(); 717 SemanticId sem_id; 718 omega_build_f_add_sub_graph(g, &sem_id); 719 720 RealizationObject real; 721 omega_realize_f_add_sub(g, &sem_id, &real); 722 723 const char *runner_path = "build/qemu_omega_runner.bin"; 724 if (omega_build_qemu_runner(&real, runner_path) != 0) { 725 omega_graph_destroy(g); 726 return false; 727 } 728 729 char log[1024]; 730 int rc = omega_exec_qemu_virt(runner_path, log, sizeof(log)); 731 omega_graph_destroy(g); 732 return (rc == 0); 733 } 734 735 static bool test_m5_adversarial_mutation(void) { 736 OmegaGraph *g = omega_graph_create(); 737 SemanticId sem_id; 738 omega_build_f_add_sub_graph(g, &sem_id); 739 740 RealizationObject real; 741 omega_realize_f_add_sub(g, &sem_id, &real); 742 743 real.code_bytes[0] ^= 0x01; /* Mutate Rd register */ 744 745 uint64_t observed = 0; 746 int rc = omega_exec_native_f3(&real, 7, 11, 3, &observed); 747 omega_graph_destroy(g); 748 749 if (rc == 0 && observed == 15) { 750 return false; 751 } 752 return true; 753 } 754 755 static bool test_m5_cross_build_determinism(void) { 756 OmegaGraph *g = omega_graph_create(); 757 SemanticId sem_id; 758 omega_build_f_add_sub_graph(g, &sem_id); 759 760 RealizationObject real; 761 omega_realize_f_add_sub(g, &sem_id, &real); 762 763 char hex[65]; 764 omega_hex_semantic_id(&real.realization_id, hex); 765 omega_graph_destroy(g); 766 return (strlen(hex) == 64); 767 } 768 769 static void run_demonstration_realization(void) { 770 printf("================================================================================\n"); 771 printf(" DEMONSTRATION: M5 DIRECT AARCH64 MACHINE REALIZATION FROM SEMANTIC GRAPH\n"); 772 printf(" Target Computation: F(a, b, c) = (a + b) - c\n"); 773 printf(" Test Vector: a = 7, b = 11, c = 3 -> Expected = 15\n"); 774 printf("================================================================================\n"); 775 776 OmegaGraph *g = omega_graph_create(); 777 SemanticId sem_id; 778 omega_build_f_add_sub_graph(g, &sem_id); 779 780 char sem_hex[65]; 781 omega_hex_semantic_id(&sem_id, sem_hex); 782 printf(" [1] M4 Semantic Graph Constructed ($G_S$)\n"); 783 printf(" SEMANTIC_ID S = %s\n\n", sem_hex); 784 785 RealizationObject real; 786 omega_realize_f_add_sub(g, &sem_id, &real); 787 char real_hex[65]; 788 omega_hex_semantic_id(&real.realization_id, real_hex); 789 790 printf(" [2] Direct AArch64 Realization Lowering (No LLVM, No Assembler)\n"); 791 printf(" Code Length = %zu bytes (3 instructions)\n", real.code_len); 792 printf(" Bytes (Hex) = "); 793 for (size_t i = 0; i < real.code_len; ++i) { 794 printf("%02X ", real.code_bytes[i]); 795 } 796 printf("\n"); 797 printf(" Disassembly =\n"); 798 printf(" +0x00: ADD X0, X0, X1 (0x8B010000)\n"); 799 printf(" +0x04: SUB X0, X0, X2 (0xCB020000)\n"); 800 printf(" +0x08: RET (0xD65F03C0)\n"); 801 printf(" REALIZATION_ID R = %s\n", real_hex); 802 printf(" Binds S = %s\n\n", (omega_compare_semantic_id(&real.semantic_id, &sem_id) == 0) ? "VERIFIED (TRUE)" : "MISMATCH (FALSE)"); 803 804 char err[256]; 805 int dec_rc = aarch64_validate_code_buffer(real.code_bytes, real.code_len, err, sizeof(err)); 806 printf(" [3] Seam 1: Independent Instruction Decoder Validation\n"); 807 printf(" Validation = %s\n\n", (dec_rc == 0) ? "PASS (Valid instruction stream, clean RET)" : err); 808 809 uint64_t native_res = 0; 810 int nat_rc = omega_exec_native_f3(&real, 7, 11, 3, &native_res); 811 printf(" [4] Seam 2: Native In-Memory Execution (mprotect PROT_EXEC)\n"); 812 printf(" Input Vector = (a=7, b=11, c=3)\n"); 813 printf(" Observed Result = %lu\n", (unsigned long)native_res); 814 printf(" Semantic Parity = %s\n\n", (nat_rc == 0 && native_res == 15) ? "EXACT PARITY (PASS: native == semantic)" : "MISMATCH (FAIL)"); 815 816 const char *qemu_bin = "build/qemu_omega_runner.bin"; 817 omega_build_qemu_runner(&real, qemu_bin); 818 char qemu_log[1024]; 819 int qemu_rc = omega_exec_qemu_virt(qemu_bin, qemu_log, sizeof(qemu_log)); 820 printf(" [5] Seam 3: Bare-Metal QEMU Virt Execution (cortex-a57, PL011 UART)\n"); 821 printf(" UART Output = %s", qemu_log); 822 printf(" QEMU Parity = %s\n\n", (qemu_rc == 0) ? "EXACT PARITY (PASS: QEMU UART confirmed 15)" : "FAIL"); 823 824 RealizationObject mut = real; 825 mut.code_bytes[0] ^= 0x01; 826 uint64_t mut_res = 0; 827 int mut_rc = omega_exec_native_f3(&mut, 7, 11, 3, &mut_res); 828 printf(" [6] Adversarial Seam: Single-Bit Machine Code Corruption\n"); 829 printf(" Bit Flip = code_bytes[0] ^= 0x01 (Rd mutated X0 -> X1)\n"); 830 printf(" Observed Post-Flip = %lu (Expected: != 15)\n", (unsigned long)mut_res); 831 printf(" Mutation Refusal = %s\n", (mut_rc != 0 || mut_res != 15) ? "DETECTED & REFUSED (PASS)" : "SILENT FAILURE (FAIL)"); 832 printf("================================================================================\n"); 833 834 omega_graph_destroy(g); 835 } 836 837 /* ========================================================================= 838 * MILESTONE 6 QUALIFICATION GATES (OMEGA_SELF_HOST) 839 * ========================================================================= */ 840 841 static bool test_m6_graph(void) { 842 OmegaGraph *g = omega_graph_create(); 843 SemanticId sem_id; 844 if (omega_build_compiler_graph(g, &sem_id) != 0) { 845 omega_graph_destroy(g); 846 return false; 847 } 848 char err[256]; 849 if (omega_validate_graph(g, err, sizeof(err)) != 0) { 850 omega_graph_destroy(g); 851 return false; 852 } 853 bool ok = (g->object_count == 5 && omega_graph_find_object(g, &sem_id) != NULL); 854 omega_graph_destroy(g); 855 return ok; 856 } 857 858 static bool test_m6_c1_emission(void) { 859 OmegaGraph *g = omega_graph_create(); 860 SemanticId sem_id; 861 omega_build_compiler_graph(g, &sem_id); 862 RealizationObject c1; 863 if (omega_self_host_compile_c0(g, &sem_id, &c1) != 0) { 864 omega_graph_destroy(g); 865 return false; 866 } 867 omega_graph_destroy(g); 868 return (c1.code_len > 0 && c1.has_id && c1.target_profile == AARCH64_PROFILE_V8A_BAREMETAL); 869 } 870 871 static bool test_m6_decoder_seam(void) { 872 OmegaGraph *g = omega_graph_create(); 873 SemanticId sem_id; 874 omega_build_compiler_graph(g, &sem_id); 875 RealizationObject c1; 876 omega_self_host_compile_c0(g, &sem_id, &c1); 877 omega_graph_destroy(g); 878 879 char err[256]; 880 return (aarch64_validate_code_buffer(c1.code_bytes, c1.code_len, err, sizeof(err)) == 0); 881 } 882 883 static bool test_m6_c2_reproduction(void) { 884 OmegaGraph *g = omega_graph_create(); 885 SemanticId sem_id; 886 omega_build_compiler_graph(g, &sem_id); 887 RealizationObject c1, c2; 888 omega_self_host_compile_c0(g, &sem_id, &c1); 889 890 uint8_t wire[8192]; 891 size_t wlen = 0; 892 omega_graph_serialize_binary(g, wire, sizeof(wire), &wlen); 893 omega_graph_destroy(g); 894 895 if (omega_self_host_run_native_compiler(&c1, wire, wlen, &c2) != 0) { 896 return false; 897 } 898 return (c2.code_len == c1.code_len && c2.has_id); 899 } 900 901 static bool test_m6_c3_reproduction(void) { 902 RealizationObject c1, c2, c3; 903 OmegaGraph *g = omega_graph_create(); 904 SemanticId sem_id; 905 omega_build_compiler_graph(g, &sem_id); 906 omega_self_host_compile_c0(g, &sem_id, &c1); 907 908 uint8_t wire[8192]; 909 size_t wlen = 0; 910 omega_graph_serialize_binary(g, wire, sizeof(wire), &wlen); 911 omega_graph_destroy(g); 912 913 if (omega_self_host_run_native_compiler(&c1, wire, wlen, &c2) != 0) return false; 914 if (omega_self_host_run_native_compiler(&c2, wire, wlen, &c3) != 0) return false; 915 return (c3.code_len == c1.code_len && c3.has_id); 916 } 917 918 static bool test_m6_fixed_point(void) { 919 RealizationObject c1, c2, c3; 920 if (omega_self_host_bootstrap_sequence(&c1, &c2, &c3) != 0) { 921 return false; 922 } 923 return (c1.code_len == c2.code_len && 924 c2.code_len == c3.code_len && 925 memcmp(c1.code_bytes, c2.code_bytes, c1.code_len) == 0 && 926 memcmp(c2.code_bytes, c3.code_bytes, c2.code_len) == 0); 927 } 928 929 static bool test_m6_realization_id(void) { 930 RealizationObject c1, c2, c3; 931 if (omega_self_host_bootstrap_sequence(&c1, &c2, &c3) != 0) { 932 return false; 933 } 934 return (memcmp(c1.realization_id.bytes, c2.realization_id.bytes, OMEGA_ID_BYTES) == 0 && 935 memcmp(c2.realization_id.bytes, c3.realization_id.bytes, OMEGA_ID_BYTES) == 0); 936 } 937 938 static bool test_m6_m5_parity(void) { 939 RealizationObject c1; 940 OmegaGraph *g = omega_graph_create(); 941 SemanticId sem_id; 942 omega_build_compiler_graph(g, &sem_id); 943 omega_self_host_compile_c0(g, &sem_id, &c1); 944 omega_graph_destroy(g); 945 946 uint64_t observed = 0; 947 return (omega_self_host_verify_m5_parity(&c1, &observed) == 0 && observed == 15); 948 } 949 950 static bool test_m6_native_execution(void) { 951 RealizationObject c1, c2, c3; 952 if (omega_self_host_bootstrap_sequence(&c1, &c2, &c3) != 0) return false; 953 954 uint64_t observed = 0; 955 return (omega_self_host_verify_m5_parity(&c3, &observed) == 0 && observed == 15); 956 } 957 958 static bool test_m6_adversarial_mutation(void) { 959 RealizationObject c1, c2; 960 OmegaGraph *g = omega_graph_create(); 961 SemanticId sem_id; 962 omega_build_compiler_graph(g, &sem_id); 963 omega_self_host_compile_c0(g, &sem_id, &c1); 964 965 uint8_t wire[8192]; 966 size_t wlen = 0; 967 omega_graph_serialize_binary(g, wire, sizeof(wire), &wlen); 968 omega_graph_destroy(g); 969 970 /* Mutate 1 bit in magic */ 971 wire[0] ^= 0x01; 972 int rc = omega_self_host_run_native_compiler(&c1, wire, wlen, &c2); 973 if (rc == 0) return false; 974 975 /* Restore wire, mutate count byte */ 976 wire[0] ^= 0x01; 977 wire[5] ^= 0x01; 978 rc = omega_self_host_run_native_compiler(&c1, wire, wlen, &c2); 979 if (rc == 0) return false; 980 981 /* Mutate instruction opcode in c1, check decoder catches it */ 982 RealizationObject mut_c = c1; 983 mut_c.code_bytes[3] = 0x00; /* Makes insn 0 into 0x00000000 (UDF), invalid in decoder */ 984 char err[256]; 985 if (aarch64_validate_code_buffer(mut_c.code_bytes, mut_c.code_len, err, sizeof(err)) == 0) { 986 return false; 987 } 988 989 return true; 990 } 991 992 static void run_demonstration_self_host(void) { 993 printf("================================================================================\n"); 994 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 6: SELF-HOSTING COMPILER REPRODUCTION\n"); 995 printf("================================================================================\n"); 996 997 RealizationObject c1, c2, c3; 998 printf(" [1] Executing 3-Generation Bootstrap Sequence:\n"); 999 printf(" C0(G_C) -> C1\n"); 1000 printf(" C1(G_C) -> C2\n"); 1001 printf(" C2(G_C) -> C3\n"); 1002 1003 int seq_rc = omega_self_host_bootstrap_sequence(&c1, &c2, &c3); 1004 char id1[65], id2[65], id3[65]; 1005 omega_hex_semantic_id(&c1.realization_id, id1); 1006 omega_hex_semantic_id(&c2.realization_id, id2); 1007 omega_hex_semantic_id(&c3.realization_id, id3); 1008 1009 printf(" C1 Code Length = %zu bytes | REALIZATION_ID = %s\n", c1.code_len, id1); 1010 printf(" C2 Code Length = %zu bytes | REALIZATION_ID = %s\n", c2.code_len, id2); 1011 printf(" C3 Code Length = %zu bytes | REALIZATION_ID = %s\n", c3.code_len, id3); 1012 printf(" Fixed Point = %s\n\n", (seq_rc == 0) ? "EXACT BIT-FOR-BIT FIXED POINT C1 == C2 == C3" : "MISMATCH"); 1013 1014 printf(" [2] Validating M5 Parity via Reproduced Compiler C3:\n"); 1015 printf(" Target: F(a,b,c) = (a + b) - c\n"); 1016 uint64_t observed = 0; 1017 int par_rc = omega_self_host_verify_m5_parity(&c3, &observed); 1018 printf(" Observed Result = %lu (Expected: 15)\n", (unsigned long)observed); 1019 printf(" M5 Parity = %s\n", (par_rc == 0 && observed == 15) ? "VERIFIED (PASS: exact M5 bytes and output)" : "FAIL"); 1020 printf("================================================================================\n"); 1021 } 1022 1023 /* ========================================================================= 1024 * MILESTONE 7 QUALIFICATION GATES (OMEGA_VERIFY) 1025 * ========================================================================= */ 1026 1027 static bool test_m7_v0_type(void) { 1028 OmegaGraph *g = omega_graph_create(); 1029 SemanticId sem_id; 1030 omega_build_f_add_sub_graph(g, &sem_id); 1031 VerifyReport rep; 1032 if (omega_verify_v0_structural(g, NULL, &rep) != 0 || !rep.passed) { 1033 omega_graph_destroy(g); 1034 return false; 1035 } 1036 omega_graph_destroy(g); 1037 1038 OmegaGraph *bad_g = omega_graph_create(); 1039 OmegaObject *bad_t = omega_graph_add_object(bad_g, KIND_TYPE); 1040 TypePayload tp = { .tag = TYPE_UNSIGNED_INT, .width = 0 }; 1041 memcpy(bad_t->payload, &tp, sizeof(tp)); 1042 bad_t->payload_len = sizeof(tp); 1043 omega_compute_semantic_id(bad_t); 1044 1045 if (omega_verify_v0_structural(bad_g, NULL, &rep) == 0 && rep.passed) { 1046 omega_graph_destroy(bad_g); 1047 return false; 1048 } 1049 omega_graph_destroy(bad_g); 1050 return true; 1051 } 1052 1053 static bool test_m7_v0_dag(void) { 1054 OmegaGraph *g = omega_graph_create(); 1055 OmegaObject *obj = omega_graph_add_object(g, KIND_OPERATION); 1056 SemanticId bogus_target; 1057 memset(bogus_target.bytes, 0xAA, OMEGA_ID_BYTES); 1058 omega_object_add_relation(obj, REL_DEPENDS_ON, &bogus_target); 1059 omega_compute_semantic_id(obj); 1060 1061 VerifyReport rep; 1062 int rc = omega_verify_v0_structural(g, NULL, &rep); 1063 omega_graph_destroy(g); 1064 return (rc != 0 || !rep.passed); 1065 } 1066 1067 static bool test_m7_v0_code_bounds(void) { 1068 OmegaGraph *g = omega_graph_create(); 1069 SemanticId sem_id; 1070 omega_build_f_add_sub_graph(g, &sem_id); 1071 RealizationObject real; 1072 omega_realize_f_add_sub(g, &sem_id, &real); 1073 omega_graph_destroy(g); 1074 1075 VerifyReport rep; 1076 if (omega_verify_v0_structural(NULL, &real, &rep) != 0 || !rep.passed) return false; 1077 1078 RealizationObject bad1 = real; 1079 bad1.code_len = 7; 1080 if (omega_verify_v0_structural(NULL, &bad1, &rep) == 0 && rep.passed) return false; 1081 1082 RealizationObject bad2 = real; 1083 bad2.code_len = 0; 1084 if (omega_verify_v0_structural(NULL, &bad2, &rep) == 0 && rep.passed) return false; 1085 1086 RealizationObject bad3 = real; 1087 bad3.code_len = 5000; 1088 if (omega_verify_v0_structural(NULL, &bad3, &rep) == 0 && rep.passed) return false; 1089 1090 return true; 1091 } 1092 1093 static bool test_m7_v0_insn_decode(void) { 1094 OmegaGraph *g = omega_graph_create(); 1095 SemanticId sem_id; 1096 omega_build_f_add_sub_graph(g, &sem_id); 1097 RealizationObject real; 1098 omega_realize_f_add_sub(g, &sem_id, &real); 1099 omega_graph_destroy(g); 1100 1101 VerifyReport rep; 1102 if (omega_verify_v0_structural(NULL, &real, &rep) != 0 || !rep.passed) return false; 1103 1104 RealizationObject bad = real; 1105 bad.code_bytes[0] = 0x00; 1106 bad.code_bytes[1] = 0x00; 1107 bad.code_bytes[2] = 0x00; 1108 bad.code_bytes[3] = 0x00; 1109 if (omega_verify_v0_structural(NULL, &bad, &rep) == 0 && rep.passed) return false; 1110 1111 return true; 1112 } 1113 1114 static bool test_m7_v0_terminal_ret(void) { 1115 OmegaGraph *g = omega_graph_create(); 1116 SemanticId sem_id; 1117 omega_build_f_add_sub_graph(g, &sem_id); 1118 RealizationObject real; 1119 omega_realize_f_add_sub(g, &sem_id, &real); 1120 omega_graph_destroy(g); 1121 1122 VerifyReport rep; 1123 if (omega_verify_v0_structural(NULL, &real, &rep) != 0 || !rep.passed) return false; 1124 1125 RealizationObject bad = real; 1126 bad.code_bytes[8] = 0x00; 1127 bad.code_bytes[9] = 0x00; 1128 bad.code_bytes[10] = 0x01; 1129 bad.code_bytes[11] = 0x8b; 1130 if (omega_verify_v0_structural(NULL, &bad, &rep) == 0 && rep.passed) return false; 1131 1132 return true; 1133 } 1134 1135 static bool test_m7_v1_differential(void) { 1136 OmegaGraph *g = omega_graph_create(); 1137 SemanticId sem_id; 1138 omega_build_f_add_sub_graph(g, &sem_id); 1139 RealizationObject real; 1140 omega_realize_f_add_sub(g, &sem_id, &real); 1141 1142 VerifyReport rep; 1143 int rc = omega_verify_v1_differential(g, &real, NULL, 0, &rep); 1144 omega_graph_destroy(g); 1145 return (rc == 0 && rep.passed && rep.check_count > 0 && rep.fail_count == 0); 1146 } 1147 1148 static bool test_m7_v1_divergence_refusal(void) { 1149 OmegaGraph *g = omega_graph_create(); 1150 SemanticId sem_id; 1151 omega_build_f_add_sub_graph(g, &sem_id); 1152 RealizationObject real; 1153 omega_realize_f_add_sub(g, &sem_id, &real); 1154 1155 RealizationObject mutant = real; 1156 mutant.code_bytes[7] = 0x8B; /* Mutate SUB opcode to ADD */ 1157 omega_compute_realization_id(&mutant); 1158 1159 VerifyReport rep; 1160 int rc = omega_verify_v1_differential(g, &mutant, NULL, 0, &rep); 1161 omega_graph_destroy(g); 1162 return (rc != 0 || !rep.passed || rep.fail_count > 0); 1163 } 1164 1165 static bool test_m7_v2_commutativity(void) { 1166 VerifyReport rep; 1167 int rc = omega_verify_v2_properties(NULL, NULL, &rep); 1168 return (rc == 0 && rep.passed && rep.check_count > 0 && rep.fail_count == 0); 1169 } 1170 1171 static bool test_m7_v2_identity(void) { 1172 VerifyReport rep; 1173 int rc = omega_verify_v2_properties(NULL, NULL, &rep); 1174 return (rc == 0 && rep.passed && rep.fail_count == 0); 1175 } 1176 1177 static bool test_m7_v2_overflow(void) { 1178 VerifyReport rep; 1179 int rc = omega_verify_v2_properties(NULL, NULL, &rep); 1180 return (rc == 0 && rep.passed && rep.fail_count == 0); 1181 } 1182 1183 static void run_demonstration_verify(void) { 1184 printf("================================================================================\n"); 1185 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 7: TRUSTED VERIFICATION LADDER\n"); 1186 printf("================================================================================\n"); 1187 1188 OmegaGraph *g = omega_graph_create(); 1189 SemanticId sem_id; 1190 omega_build_f_add_sub_graph(g, &sem_id); 1191 RealizationObject real; 1192 omega_realize_f_add_sub(g, &sem_id, &real); 1193 1194 VerifyReport rep; 1195 printf(" [1] Executing Tier V0: Structural Verification...\n"); 1196 omega_verify_v0_structural(g, &real, &rep); 1197 printf(" Checks Passed: %u | Failures: %u | Status: %s\n\n", 1198 rep.check_count, rep.fail_count, rep.passed ? "PASS (Graph DAG, profile, bounds, insns verified)" : "FAIL"); 1199 1200 printf(" [2] Executing Tier V1: Differential Verification...\n"); 1201 omega_verify_v1_differential(g, &real, NULL, 0, &rep); 1202 printf(" Vectors Evaluated: %u | Divergences: %u | Status: %s\n\n", 1203 rep.check_count, rep.fail_count, rep.passed ? "PASS (Bit-for-bit semantic == native parity)" : "FAIL"); 1204 1205 printf(" [3] Executing Tier V2: Property and Invariant Verification...\n"); 1206 omega_verify_v2_properties(g, &real, &rep); 1207 printf(" Invariants Checked: %u | Violations: %u | Status: %s\n\n", 1208 rep.check_count, rep.fail_count, rep.passed ? "PASS (Commutativity, identity, overflow wrapping verified)" : "FAIL"); 1209 1210 printf(" [4] Full Pipeline Verification (V0 -> V1 -> V2)...\n"); 1211 int pipe_rc = omega_verify_pipeline(g, &real, VERIFY_TIER_V2, &rep); 1212 printf(" Pipeline Status: %s\n", (pipe_rc == 0 && rep.passed) ? "ADMITTED (All tiers passed, candidate survives)" : "REFUSED"); 1213 printf("================================================================================\n"); 1214 1215 omega_graph_destroy(g); 1216 } 1217 1218 /* ========================================================================= 1219 * MILESTONE 8 QUALIFICATION GATES (OMEGA_PROGRAM_CORE) 1220 * ========================================================================= */ 1221 1222 static bool test_m8_program_object(void) { 1223 OmegaProgram a; 1224 if (omega_program_build_unary_op(&a, "prog_mul2", OP_MUL, 2) != 0) return false; 1225 bool ok = (a.is_realized && a.cost.insn_count == 3 && 1226 a.contract.input_type == TYPE_UNSIGNED_INT && 1227 a.contract.output_type == TYPE_UNSIGNED_INT); 1228 omega_program_destroy(&a); 1229 return ok; 1230 } 1231 1232 static bool test_m8_contract_validation(void) { 1233 OmegaProgram a; 1234 omega_program_build_unary_op(&a, "prog_add1", OP_ADD, 1); 1235 char err[256]; 1236 if (omega_program_validate_contract(&a, err, sizeof(err)) != 0) { 1237 omega_program_destroy(&a); 1238 return false; 1239 } 1240 1241 /* Mutate contract to invalid type */ 1242 OmegaProgram bad = a; 1243 bad.contract.input_type = TYPE_INVALID; 1244 if (omega_program_validate_contract(&bad, err, sizeof(err)) == 0) { 1245 omega_program_destroy(&a); 1246 return false; 1247 } 1248 1249 /* Mutate contract to uninitialized constraint SemanticId */ 1250 OmegaProgram bad_const = a; 1251 memset(&bad_const.contract.precondition_id, 0, sizeof(SemanticId)); 1252 if (omega_program_validate_contract(&bad_const, err, sizeof(err)) == 0) { 1253 omega_program_destroy(&a); 1254 return false; 1255 } 1256 1257 omega_program_destroy(&a); 1258 return true; 1259 } 1260 1261 static bool test_m8_composition(void) { 1262 OmegaProgram a, b, c; 1263 omega_program_build_unary_op(&a, "mul2", OP_MUL, 2); 1264 omega_program_build_unary_op(&b, "add1", OP_ADD, 1); 1265 1266 char err[256]; 1267 int rc = omega_program_compose(&a, &b, &c, err, sizeof(err)); 1268 bool ok = (rc == 0 && c.is_realized && 1269 c.contract.input_type == TYPE_UNSIGNED_INT && 1270 c.contract.output_type == TYPE_UNSIGNED_INT); 1271 1272 omega_program_destroy(&a); 1273 omega_program_destroy(&b); 1274 omega_program_destroy(&c); 1275 return ok; 1276 } 1277 1278 static bool test_m8_type_mismatch_refusal(void) { 1279 OmegaProgram a, b, c; 1280 omega_program_build_unary_op(&a, "mul2", OP_MUL, 2); 1281 omega_program_build_unary_op(&b, "add1", OP_ADD, 1); 1282 b.contract.input_type = TYPE_BOOL; /* Cause mismatch */ 1283 1284 char err[256]; 1285 int rc = omega_program_compose(&a, &b, &c, err, sizeof(err)); 1286 1287 omega_program_destroy(&a); 1288 omega_program_destroy(&b); 1289 return (rc != 0); /* Must refuse fail-closed */ 1290 } 1291 1292 static bool test_m8_cost_accounting(void) { 1293 OmegaProgram a, b, c; 1294 omega_program_build_unary_op(&a, "mul2", OP_MUL, 2); 1295 omega_program_build_unary_op(&b, "add1", OP_ADD, 1); 1296 1297 char err[256]; 1298 omega_program_compose(&a, &b, &c, err, sizeof(err)); 1299 1300 /* Cost check: insn_count = 3 + 3 - 1 = 5 */ 1301 bool ok = (c.cost.insn_count == 5 && 1302 c.cost.latency_cycles == a.cost.latency_cycles + b.cost.latency_cycles); 1303 1304 omega_program_destroy(&a); 1305 omega_program_destroy(&b); 1306 omega_program_destroy(&c); 1307 return ok; 1308 } 1309 1310 static bool test_m8_realization(void) { 1311 OmegaProgram a, b, c; 1312 omega_program_build_unary_op(&a, "mul2", OP_MUL, 2); 1313 omega_program_build_unary_op(&b, "add1", OP_ADD, 1); 1314 1315 char err[256]; 1316 omega_program_compose(&a, &b, &c, err, sizeof(err)); 1317 1318 bool ok = (c.realization.code_len == 20 && c.realization.has_id); 1319 1320 omega_program_destroy(&a); 1321 omega_program_destroy(&b); 1322 omega_program_destroy(&c); 1323 return ok; 1324 } 1325 1326 static bool test_m8_v0_structural(void) { 1327 OmegaProgram a, b, c; 1328 omega_program_build_unary_op(&a, "mul2", OP_MUL, 2); 1329 omega_program_build_unary_op(&b, "add1", OP_ADD, 1); 1330 1331 char err[256]; 1332 omega_program_compose(&a, &b, &c, err, sizeof(err)); 1333 1334 VerifyReport rep; 1335 int rc = omega_verify_v0_structural(NULL, &c.realization, &rep); 1336 1337 omega_program_destroy(&a); 1338 omega_program_destroy(&b); 1339 omega_program_destroy(&c); 1340 return (rc == 0 && rep.passed && rep.fail_count == 0); 1341 } 1342 1343 static bool test_m8_v1_differential(void) { 1344 OmegaProgram a, b, c; 1345 omega_program_build_unary_op(&a, "mul2", OP_MUL, 2); 1346 omega_program_build_unary_op(&b, "add1", OP_ADD, 1); 1347 1348 char err[256]; 1349 omega_program_compose(&a, &b, &c, err, sizeof(err)); 1350 1351 /* Evaluate C(x) = (2x + 1) across vectors */ 1352 static const uint64_t inputs[] = { 0, 1, 2, 7, 10, 100 }; 1353 static const uint64_t expected[] = { 1, 3, 5, 15, 21, 201 }; 1354 1355 bool ok = true; 1356 for (size_t i = 0; i < 6; ++i) { 1357 uint64_t observed = 0; 1358 if (omega_program_exec(&c, inputs[i], &observed) != 0 || observed != expected[i]) { 1359 ok = false; 1360 break; 1361 } 1362 } 1363 1364 omega_program_destroy(&a); 1365 omega_program_destroy(&b); 1366 omega_program_destroy(&c); 1367 return ok; 1368 } 1369 1370 static bool test_m8_v2_property(void) { 1371 OmegaProgram a, b, c; 1372 omega_program_build_unary_op(&a, "mul2", OP_MUL, 2); 1373 omega_program_build_unary_op(&b, "add1", OP_ADD, 1); 1374 1375 char err[256]; 1376 omega_program_compose(&a, &b, &c, err, sizeof(err)); 1377 1378 VerifyReport rep; 1379 int rc = omega_program_verify(&c, &rep); 1380 bool verified = (rc == 0 && rep.passed && c.is_verified); 1381 1382 omega_program_destroy(&a); 1383 omega_program_destroy(&b); 1384 omega_program_destroy(&c); 1385 return verified; 1386 } 1387 1388 static bool test_m8_synthesis_task(void) { 1389 SynthesisTask task; 1390 static const uint64_t inputs[] = { 0, 1, 2, 7, 10 }; 1391 static const uint64_t outputs[] = { 1, 3, 5, 15, 21 }; 1392 1393 omega_task_init(&task, "task_2x_plus_1", TYPE_UNSIGNED_INT, 64, TYPE_UNSIGNED_INT, 64, inputs, outputs, 5); 1394 1395 OmegaProgram a, b, c; 1396 omega_program_build_unary_op(&a, "mul2", OP_MUL, 2); 1397 omega_program_build_unary_op(&b, "add1", OP_ADD, 1); 1398 char err[256]; 1399 omega_program_compose(&a, &b, &c, err, sizeof(err)); 1400 1401 bool solved = false; 1402 omega_task_evaluate_candidate(&task, &c, &solved); 1403 1404 /* Program A alone should NOT solve task */ 1405 bool solved_a = true; 1406 omega_task_evaluate_candidate(&task, &a, &solved_a); 1407 1408 omega_program_destroy(&a); 1409 omega_program_destroy(&b); 1410 omega_program_destroy(&c); 1411 return (solved && !solved_a); 1412 } 1413 1414 static void run_demonstration_program(void) { 1415 printf("================================================================================\n"); 1416 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 8: PROGRAM COMPOSITION & CONTRACTS\n"); 1417 printf("================================================================================\n"); 1418 1419 OmegaProgram a, b, c; 1420 omega_program_build_unary_op(&a, "double", OP_MUL, 2); 1421 omega_program_build_unary_op(&b, "increment", OP_ADD, 1); 1422 1423 printf(" [1] Program A: %s | Cost: %u insns | Post: %s\n", a.name, a.cost.insn_count, a.contract.postcondition); 1424 printf(" [2] Program B: %s | Cost: %u insns | Post: %s\n\n", b.name, b.cost.insn_count, b.contract.postcondition); 1425 1426 char err[256]; 1427 printf(" [3] Composing C = A o B (C(x) = B(A(x)) = 2x + 1)...\n"); 1428 omega_program_compose(&a, &b, &c, err, sizeof(err)); 1429 printf(" Composite Program: %s\n", c.name); 1430 printf(" Derived Contract: In: uint%u -> Out: uint%u | Post: %s\n", 1431 c.contract.input_width, c.contract.output_width, c.contract.postcondition); 1432 printf(" Composite Cost: %u insns (additive monotonicity verified)\n", c.cost.insn_count); 1433 printf(" Machine Code: %zu bytes native AArch64\n\n", c.realization.code_len); 1434 1435 printf(" [4] Evaluating Composite Execution C(x):\n"); 1436 for (uint64_t x = 0; x <= 5; ++x) { 1437 uint64_t y = 0; 1438 omega_program_exec(&c, x, &y); 1439 printf(" C(%lu) = %lu (Expected: %lu)\n", (unsigned long)x, (unsigned long)y, (unsigned long)(2 * x + 1)); 1440 } 1441 1442 VerifyReport rep; 1443 omega_program_verify(&c, &rep); 1444 printf("\n [5] M7 Verification Ladder Status: %s\n", c.is_verified ? "VERIFIED / ADMITTED" : "REFUSED"); 1445 printf("================================================================================\n"); 1446 1447 omega_program_destroy(&a); 1448 omega_program_destroy(&b); 1449 omega_program_destroy(&c); 1450 } 1451 1452 /* ========================================================================= 1453 * MILESTONE 9 GATES: OMEGA_SYNTHESIS_V0 1454 * ========================================================================= */ 1455 1456 static bool test_m9_primitives(void) { 1457 SynthPrimitiveBank bank; 1458 if (omega_synth_bank_init(&bank) != 0) return false; 1459 if (bank.count < 10) { 1460 omega_synth_bank_destroy(&bank); 1461 return false; 1462 } 1463 1464 bool ok = true; 1465 for (size_t i = 0; i < bank.count; ++i) { 1466 OmegaProgram *p = &bank.programs[i]; 1467 if (!p->is_realized || p->realization.code_len == 0 || p->cost.insn_count == 0) { 1468 ok = false; 1469 break; 1470 } 1471 VerifyReport rep; 1472 if (omega_verify_v0_structural(NULL, &p->realization, &rep) != 0 || !rep.passed) { 1473 ok = false; 1474 break; 1475 } 1476 } 1477 1478 omega_synth_bank_destroy(&bank); 1479 return ok; 1480 } 1481 1482 static bool test_m9_search_ordering(void) { 1483 SynthPrimitiveBank bank; 1484 if (omega_synth_bank_init(&bank) != 0) return false; 1485 1486 SynthesisTask task; 1487 static const uint64_t in[] = { 1 }; 1488 static const uint64_t out[] = { 999 }; 1489 omega_task_init(&task, "unsolvable", TYPE_UNSIGNED_INT, 64, TYPE_UNSIGNED_INT, 64, in, out, 1); 1490 1491 SynthesisConfig cfg = { 1492 .max_depth = 2, 1493 .max_cost = 10, 1494 .max_candidates = 50, 1495 .deduplicate_equiv = false 1496 }; 1497 1498 SynthesisResult res; 1499 omega_synthesize(&task, &bank, &cfg, &res); 1500 1501 omega_synth_bank_destroy(&bank); 1502 return (res.stats.candidates_generated > bank.count); 1503 } 1504 1505 static bool test_m9_type_pruning(void) { 1506 SynthPrimitiveBank bank; 1507 if (omega_synth_bank_init(&bank) != 0) return false; 1508 1509 OmegaProgram bad_p; 1510 omega_program_build_unary_op(&bad_p, "bad_u32", OP_ADD, 1); 1511 bad_p.contract.output_width = 32; 1512 1513 OmegaProgram comp; 1514 char err[256]; 1515 int rc = omega_program_compose(&bad_p, &bank.programs[0], &comp, err, sizeof(err)); 1516 omega_program_destroy(&bad_p); 1517 1518 omega_synth_bank_destroy(&bank); 1519 return (rc != 0); 1520 } 1521 1522 static bool test_m9_equiv_pruning(void) { 1523 EquivTable tbl; 1524 omega_synth_equiv_init(&tbl); 1525 1526 OmegaProgram p1, p2; 1527 OmegaProgram a, b; 1528 omega_program_build_unary_op(&a, "add1", OP_ADD, 1); 1529 omega_program_build_unary_op(&b, "add2", OP_ADD, 2); 1530 char err[256]; 1531 omega_program_compose(&a, &b, &p1, err, sizeof(err)); 1532 1533 omega_program_build_unary_op(&p2, "add3", OP_ADD, 3); 1534 1535 uint8_t sig1[32], sig2[32]; 1536 omega_synth_compute_signature(&p1, sig1); 1537 omega_synth_compute_signature(&p2, sig2); 1538 1539 if (memcmp(sig1, sig2, 32) != 0) { 1540 omega_program_destroy(&a); omega_program_destroy(&b); 1541 omega_program_destroy(&p1); omega_program_destroy(&p2); 1542 return false; 1543 } 1544 1545 /* p2 (add3) has lower cost (3 insns) than p1 (5 insns) */ 1546 bool pruned_cheaper = omega_synth_equiv_contains_or_add(&tbl, sig2, p2.cost.insn_count); 1547 bool pruned_expensive = omega_synth_equiv_contains_or_add(&tbl, sig1, p1.cost.insn_count); 1548 1549 omega_program_destroy(&a); omega_program_destroy(&b); 1550 omega_program_destroy(&p1); omega_program_destroy(&p2); 1551 1552 return (!pruned_cheaper && pruned_expensive); 1553 } 1554 1555 static bool test_m9_v0_structural(void) { 1556 SynthesisResult res; 1557 if (omega_synthesize_target_affine(&res) != 0 || !res.solved) return false; 1558 1559 VerifyReport rep; 1560 int rc = omega_verify_v0_structural(NULL, &res.solution.realization, &rep); 1561 omega_program_destroy(&res.solution); 1562 return (rc == 0 && rep.passed); 1563 } 1564 1565 static bool test_m9_v1_io_filtering(void) { 1566 SynthesisTask task; 1567 static const uint64_t in[] = { 0, 1, 2, 3 }; 1568 static const uint64_t out[] = { 1, 3, 5, 7 }; 1569 omega_task_init(&task, "task_io", TYPE_UNSIGNED_INT, 64, TYPE_UNSIGNED_INT, 64, in, out, 4); 1570 1571 OmegaProgram wrong; 1572 omega_program_build_unary_op(&wrong, "wrong_cand", OP_ADD, 5); 1573 1574 bool solved = false; 1575 omega_task_evaluate_candidate(&task, &wrong, &solved); 1576 omega_program_destroy(&wrong); 1577 1578 return (!solved); 1579 } 1580 1581 static bool test_m9_v2_property(void) { 1582 SynthesisResult res; 1583 if (omega_synthesize_target_affine(&res) != 0 || !res.solved) return false; 1584 1585 VerifyReport rep; 1586 int rc = omega_verify_v2_properties(NULL, &res.solution.realization, &rep); 1587 omega_program_destroy(&res.solution); 1588 return (rc == 0 && rep.passed); 1589 } 1590 1591 static bool test_m9_target_affine(void) { 1592 SynthesisResult res; 1593 if (omega_synthesize_target_affine(&res) != 0 || !res.solved) return false; 1594 1595 uint64_t y50 = 0, y100 = 0; 1596 if (omega_program_exec(&res.solution, 50, &y50) != 0 || y50 != 101) { 1597 omega_program_destroy(&res.solution); 1598 return false; 1599 } 1600 if (omega_program_exec(&res.solution, 100, &y100) != 0 || y100 != 201) { 1601 omega_program_destroy(&res.solution); 1602 return false; 1603 } 1604 1605 omega_program_destroy(&res.solution); 1606 return true; 1607 } 1608 1609 static bool test_m9_target_composed(void) { 1610 SynthesisResult res; 1611 if (omega_synthesize_target_composed(&res) != 0 || !res.solved) return false; 1612 1613 uint64_t y5 = 0, y20 = 0; 1614 if (omega_program_exec(&res.solution, 5, &y5) != 0 || y5 != 13) { 1615 omega_program_destroy(&res.solution); 1616 return false; 1617 } 1618 if (omega_program_exec(&res.solution, 20, &y20) != 0 || y20 != 58) { 1619 omega_program_destroy(&res.solution); 1620 return false; 1621 } 1622 1623 omega_program_destroy(&res.solution); 1624 return true; 1625 } 1626 1627 static bool test_m9_receipt(void) { 1628 SynthesisResult res; 1629 if (omega_synthesize_target_affine(&res) != 0 || !res.solved) return false; 1630 1631 bool valid = res.solution.is_realized && res.solution.is_verified && 1632 (res.solution.realization.code_len > 0); 1633 omega_program_destroy(&res.solution); 1634 return valid; 1635 } 1636 1637 static void run_demonstration_synthesis(void) { 1638 printf("================================================================================\n"); 1639 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 9: OMEGA_SYNTHESIS_V0 DEMONSTRATION\n"); 1640 printf("================================================================================\n"); 1641 1642 printf("\n [DEMONSTRATION 1: TARGET AFFINE f(x) = 2x + 1]\n"); 1643 printf(" Target Specification (Input-Output Pairs):\n"); 1644 printf(" (0 -> 1), (1 -> 3), (2 -> 5), (3 -> 7), (5 -> 11), (10 -> 21)\n"); 1645 1646 SynthesisResult res1; 1647 omega_synthesize_target_affine(&res1); 1648 1649 if (res1.solved) { 1650 printf(" Synthesis Status: DISCOVERED AND VERIFIED\n"); 1651 printf(" Solution Program: %s\n", res1.solution.name); 1652 printf(" Derived Contract: In: uint%u -> Out: uint%u | Post: %s\n", 1653 res1.solution.contract.input_width, res1.solution.contract.output_width, 1654 res1.solution.contract.postcondition); 1655 printf(" Solution Cost: %u insns | Latency: %u cycles\n", 1656 res1.solution.cost.insn_count, res1.solution.cost.latency_cycles); 1657 printf(" Search Metrics: %zu generated | %zu pruned (type) | %zu pruned (equiv)\n", 1658 res1.stats.candidates_generated, res1.stats.candidates_pruned_type, res1.stats.candidates_pruned_equiv); 1659 printf(" M7 Verification: Status: %s (Tier %d, %u checks, %u failures)\n", 1660 res1.verify_report.passed ? "VERIFIED / PASS" : "FAIL", 1661 res1.verify_report.tier, res1.verify_report.check_count, res1.verify_report.fail_count); 1662 1663 printf(" Evaluating Unseen Holdout Inputs on Native AArch64 Hardware:\n"); 1664 static const uint64_t holdouts1[] = { 4, 8, 25, 50, 100 }; 1665 for (size_t i = 0; i < sizeof(holdouts1)/sizeof(holdouts1[0]); ++i) { 1666 uint64_t y = 0; 1667 omega_program_exec(&res1.solution, holdouts1[i], &y); 1668 printf(" f(%lu) = %lu (Ground truth: %lu) [%s]\n", 1669 (unsigned long)holdouts1[i], (unsigned long)y, (unsigned long)(2 * holdouts1[i] + 1), 1670 (y == 2 * holdouts1[i] + 1) ? "CORRECT" : "MISMATCH"); 1671 } 1672 omega_program_destroy(&res1.solution); 1673 } else { 1674 printf(" Synthesis Status: UNSOLVED\n"); 1675 } 1676 1677 printf("\n [DEMONSTRATION 2: TARGET COMPOSED f(x) = 3x - 2]\n"); 1678 printf(" Target Specification (Input-Output Pairs):\n"); 1679 printf(" (1 -> 1), (2 -> 4), (3 -> 7), (4 -> 10), (10 -> 28)\n"); 1680 1681 SynthesisResult res2; 1682 omega_synthesize_target_composed(&res2); 1683 1684 if (res2.solved) { 1685 printf(" Synthesis Status: DISCOVERED AND VERIFIED\n"); 1686 printf(" Solution Program: %s\n", res2.solution.name); 1687 printf(" Derived Contract: In: uint%u -> Out: uint%u | Post: %s\n", 1688 res2.solution.contract.input_width, res2.solution.contract.output_width, 1689 res2.solution.contract.postcondition); 1690 printf(" Solution Cost: %u insns | Latency: %u cycles\n", 1691 res2.solution.cost.insn_count, res2.solution.cost.latency_cycles); 1692 printf(" Search Metrics: %zu generated | %zu pruned (type) | %zu pruned (equiv)\n", 1693 res2.stats.candidates_generated, res2.stats.candidates_pruned_type, res2.stats.candidates_pruned_equiv); 1694 printf(" M7 Verification: Status: %s (Tier %d, %u checks, %u failures)\n", 1695 res2.verify_report.passed ? "VERIFIED / PASS" : "FAIL", 1696 res2.verify_report.tier, res2.verify_report.check_count, res2.verify_report.fail_count); 1697 1698 printf(" Evaluating Unseen Holdout Inputs on Native AArch64 Hardware:\n"); 1699 static const uint64_t holdouts2[] = { 5, 8, 20, 50, 100 }; 1700 for (size_t i = 0; i < sizeof(holdouts2)/sizeof(holdouts2[0]); ++i) { 1701 uint64_t y = 0; 1702 omega_program_exec(&res2.solution, holdouts2[i], &y); 1703 printf(" f(%lu) = %lu (Ground truth: %lu) [%s]\n", 1704 (unsigned long)holdouts2[i], (unsigned long)y, (unsigned long)(3 * holdouts2[i] - 2), 1705 (y == 3 * holdouts2[i] - 2) ? "CORRECT" : "MISMATCH"); 1706 } 1707 omega_program_destroy(&res2.solution); 1708 } else { 1709 printf(" Synthesis Status: UNSOLVED\n"); 1710 } 1711 1712 printf("================================================================================\n"); 1713 } 1714 1715 /* Non-zero evidence hash for the gate programs: omega_library_insert refuses NULL or all-zero. */ 1716 static const uint8_t OMEGA_TEST_LIB_RECEIPT[32] = { 0xA5, 0x5A, 0xC3, 0x3C }; 1717 1718 /* ========================================================================= 1719 * MILESTONE 10 GATES: OMEGA_LIBRARY_V1 1720 * ========================================================================= */ 1721 1722 static bool test_m10_init(void) { 1723 OmegaLibrary lib; 1724 if (omega_library_init(&lib) != 0) return false; 1725 bool ok = (lib.version == 1 && lib.count == 0); 1726 omega_library_destroy(&lib); 1727 return ok; 1728 } 1729 1730 static bool test_m10_insert(void) { 1731 OmegaLibrary lib; 1732 omega_library_init(&lib); 1733 1734 OmegaProgram p; 1735 omega_program_build_unary_op(&p, "add1", OP_ADD, 1); 1736 VerifyReport rep; 1737 omega_program_verify(&p, &rep); 1738 1739 uint8_t dummy_receipt[32] = { 0xAA }; 1740 int rc = omega_library_insert(&lib, &p, NULL, 0, dummy_receipt); 1741 bool ok = (rc == 0 && lib.count == 1 && lib.entries[0].version_introduced == 1); 1742 1743 omega_library_destroy(&lib); 1744 return ok; 1745 } 1746 1747 static bool test_m10_lookup_id(void) { 1748 OmegaLibrary lib; 1749 omega_library_init(&lib); 1750 1751 OmegaProgram p; 1752 omega_program_build_unary_op(&p, "add2", OP_ADD, 2); 1753 VerifyReport rep; 1754 omega_program_verify(&p, &rep); 1755 1756 omega_library_insert(&lib, &p, NULL, 0, OMEGA_TEST_LIB_RECEIPT); 1757 1758 const OmegaLibraryEntry *e = omega_library_find_by_id(&lib, &p.program_id); 1759 bool ok = (e != NULL && omega_compare_semantic_id(&e->program.program_id, &p.program_id) == 0); 1760 1761 SemanticId dummy_id; 1762 memset(&dummy_id, 0xFF, sizeof(dummy_id)); 1763 if (omega_library_find_by_id(&lib, &dummy_id) != NULL) ok = false; 1764 1765 omega_library_destroy(&lib); 1766 return ok; 1767 } 1768 1769 static bool test_m10_lookup_type(void) { 1770 OmegaLibrary lib; 1771 omega_library_init(&lib); 1772 1773 OmegaProgram p1, p2; 1774 omega_program_build_unary_op(&p1, "u64_add3", OP_ADD, 3); 1775 VerifyReport rep; 1776 omega_program_verify(&p1, &rep); 1777 omega_library_insert(&lib, &p1, NULL, 0, OMEGA_TEST_LIB_RECEIPT); 1778 1779 omega_program_build_unary_op(&p2, "u32_sub1", OP_SUB, 1); 1780 p2.contract.input_width = 32; 1781 p2.contract.output_width = 32; 1782 omega_program_compute_id(&p2); 1783 omega_program_verify(&p2, &rep); 1784 omega_library_insert(&lib, &p2, NULL, 0, OMEGA_TEST_LIB_RECEIPT); 1785 1786 const OmegaLibraryEntry *results[4]; 1787 size_t found = omega_library_query_by_type(&lib, TYPE_UNSIGNED_INT, 64, TYPE_UNSIGNED_INT, 64, results, 4); 1788 bool ok = (found == 1 && strcmp(results[0]->program.name, "u64_add3") == 0); 1789 1790 omega_library_destroy(&lib); 1791 return ok; 1792 } 1793 1794 static bool test_m10_dependency_dag(void) { 1795 OmegaLibrary lib; 1796 omega_library_init(&lib); 1797 1798 OmegaProgram p1, p2, c; 1799 omega_program_build_unary_op(&p1, "mul2", OP_MUL, 2); 1800 omega_program_build_unary_op(&p2, "add1", OP_ADD, 1); 1801 VerifyReport rep; 1802 omega_program_verify(&p1, &rep); 1803 omega_program_verify(&p2, &rep); 1804 omega_library_insert(&lib, &p1, NULL, 0, OMEGA_TEST_LIB_RECEIPT); 1805 omega_library_insert(&lib, &p2, NULL, 0, OMEGA_TEST_LIB_RECEIPT); 1806 1807 char err[256]; 1808 omega_program_compose(&p1, &p2, &c, err, sizeof(err)); 1809 omega_program_verify(&c, &rep); 1810 1811 SemanticId deps[2] = { p1.program_id, p2.program_id }; 1812 int rc = omega_library_insert(&lib, &c, deps, 2, OMEGA_TEST_LIB_RECEIPT); 1813 if (rc != 0) { omega_library_destroy(&lib); return false; } 1814 1815 const OmegaLibraryEntry *e = omega_library_find_by_id(&lib, &c.program_id); 1816 bool ok = (e != NULL && e->dep_count == 2); 1817 1818 /* Test cycle prevention: attempt to add dependency from p1 onto c */ 1819 SemanticId cycle_dep[1] = { c.program_id }; 1820 if (!omega_library_has_cycle(&lib, &p1.program_id, cycle_dep, 1)) { 1821 ok = false; 1822 } 1823 1824 omega_library_destroy(&lib); 1825 return ok; 1826 } 1827 1828 static bool test_m10_immutability(void) { 1829 OmegaLibrary lib; 1830 omega_library_init(&lib); 1831 1832 OmegaProgram p; 1833 omega_program_build_unary_op(&p, "mul3", OP_MUL, 3); 1834 VerifyReport rep; 1835 omega_program_verify(&p, &rep); 1836 omega_library_insert(&lib, &p, NULL, 0, OMEGA_TEST_LIB_RECEIPT); 1837 1838 uint8_t digest1[32]; 1839 memcpy(digest1, lib.state_digest, 32); 1840 1841 omega_library_advance_version(&lib); 1842 uint8_t digest2[32]; 1843 memcpy(digest2, lib.state_digest, 32); 1844 1845 bool ok = (memcmp(digest1, digest2, 32) != 0 && lib.version == 2); 1846 1847 omega_library_destroy(&lib); 1848 return ok; 1849 } 1850 1851 static bool test_m10_unverified_refusal(void) { 1852 OmegaLibrary lib; 1853 omega_library_init(&lib); 1854 1855 OmegaProgram unverified; 1856 omega_program_build_unary_op(&unverified, "unverified_prog", OP_ADD, 10); 1857 unverified.is_verified = false; 1858 1859 int rc = omega_library_insert(&lib, &unverified, NULL, 0, OMEGA_TEST_LIB_RECEIPT); 1860 bool ok = (rc != 0 && lib.count == 0); 1861 1862 omega_program_destroy(&unverified); 1863 omega_library_destroy(&lib); 1864 return ok; 1865 } 1866 1867 static bool test_m10_duplicate_refusal(void) { 1868 OmegaLibrary lib; 1869 omega_library_init(&lib); 1870 1871 OmegaProgram p; 1872 omega_program_build_unary_op(&p, "unique_prog", OP_ADD, 7); 1873 VerifyReport rep; 1874 omega_program_verify(&p, &rep); 1875 1876 int rc1 = omega_library_insert(&lib, &p, NULL, 0, OMEGA_TEST_LIB_RECEIPT); 1877 int rc2 = omega_library_insert(&lib, &p, NULL, 0, OMEGA_TEST_LIB_RECEIPT); 1878 1879 bool ok = (rc1 == 0 && rc2 != 0 && lib.count == 1); 1880 1881 omega_library_destroy(&lib); 1882 return ok; 1883 } 1884 1885 static bool test_m10_synthesis_reuse(void) { 1886 OmegaLibrary lib; 1887 omega_library_init(&lib); 1888 1889 /* Insert verified composite component A = 2x + 1 */ 1890 OmegaProgram m2, a1, comp_2x1; 1891 omega_program_build_unary_op(&m2, "mul2", OP_MUL, 2); 1892 omega_program_build_unary_op(&a1, "add1", OP_ADD, 1); 1893 char err[256]; 1894 omega_program_compose(&m2, &a1, &comp_2x1, err, sizeof(err)); 1895 VerifyReport rep; 1896 omega_program_verify(&comp_2x1, &rep); 1897 omega_library_insert(&lib, &comp_2x1, NULL, 0, OMEGA_TEST_LIB_RECEIPT); 1898 1899 /* Insert base primitive B = add_5 */ 1900 OmegaProgram a5; 1901 omega_program_build_unary_op(&a5, "add5", OP_ADD, 5); 1902 omega_program_verify(&a5, &rep); 1903 omega_library_insert(&lib, &a5, NULL, 0, OMEGA_TEST_LIB_RECEIPT); 1904 1905 /* Export library as primitive bank */ 1906 SynthPrimitiveBank bank; 1907 omega_library_export_primitives(&lib, &bank); 1908 1909 /* Target: g(x) = (2x + 1) + 5 = 2x + 6 */ 1910 SynthesisTask task; 1911 static const uint64_t inputs[] = { 0, 1, 2, 5, 10 }; 1912 static const uint64_t outputs[] = { 6, 8, 10, 16, 26 }; 1913 omega_task_init(&task, "task_reuse_2x_plus_6", TYPE_UNSIGNED_INT, 64, TYPE_UNSIGNED_INT, 64, inputs, outputs, 5); 1914 1915 SynthesisConfig config = { 1916 .max_depth = 2, 1917 .max_cost = 10, 1918 .max_candidates = 100, 1919 .deduplicate_equiv = true 1920 }; 1921 1922 SynthesisResult res; 1923 int rc = omega_synthesize(&task, &bank, &config, &res); 1924 bool ok = (rc == 0 && res.solved); 1925 1926 if (ok) { 1927 uint64_t y50 = 0; 1928 omega_program_exec(&res.solution, 50, &y50); 1929 if (y50 != 106) ok = false; 1930 omega_program_destroy(&res.solution); 1931 } 1932 1933 omega_program_destroy(&m2); 1934 omega_program_destroy(&a1); 1935 omega_library_destroy(&lib); 1936 return ok; 1937 } 1938 1939 static bool test_m10_receipt(void) { 1940 OmegaLibrary lib; 1941 omega_library_init(&lib); 1942 1943 OmegaProgram p; 1944 omega_program_build_unary_op(&p, "test_receipt_p", OP_ADD, 1); 1945 VerifyReport rep; 1946 omega_program_verify(&p, &rep); 1947 omega_library_insert(&lib, &p, NULL, 0, OMEGA_TEST_LIB_RECEIPT); 1948 1949 bool ok = (lib.count == 1 && lib.version >= 1); 1950 omega_library_destroy(&lib); 1951 return ok; 1952 } 1953 1954 static void run_demonstration_library(void) { 1955 printf("================================================================================\n"); 1956 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 10: OMEGA_LIBRARY_V1 DEMONSTRATION\n"); 1957 printf("================================================================================\n"); 1958 1959 OmegaLibrary lib; 1960 omega_library_init(&lib); 1961 1962 printf("\n [1] Catalog Initialization:\n"); 1963 printf(" Library Version: %u | Initial Count: %zu\n", lib.version, lib.count); 1964 1965 /* Insert components */ 1966 OmegaProgram m2, a1, c_2x1, a5; 1967 omega_program_build_unary_op(&m2, "primitive_mul2", OP_MUL, 2); 1968 omega_program_build_unary_op(&a1, "primitive_add1", OP_ADD, 1); 1969 omega_program_build_unary_op(&a5, "primitive_add5", OP_ADD, 5); 1970 VerifyReport rep; 1971 omega_program_verify(&m2, &rep); 1972 omega_program_verify(&a1, &rep); 1973 omega_program_verify(&a5, &rep); 1974 1975 omega_library_insert(&lib, &m2, NULL, 0, OMEGA_TEST_LIB_RECEIPT); 1976 omega_library_insert(&lib, &a1, NULL, 0, OMEGA_TEST_LIB_RECEIPT); 1977 omega_library_insert(&lib, &a5, NULL, 0, OMEGA_TEST_LIB_RECEIPT); 1978 1979 char err[256]; 1980 omega_program_compose(&m2, &a1, &c_2x1, err, sizeof(err)); 1981 omega_program_verify(&c_2x1, &rep); 1982 SemanticId deps[2] = { m2.program_id, a1.program_id }; 1983 omega_library_insert(&lib, &c_2x1, deps, 2, OMEGA_TEST_LIB_RECEIPT); 1984 1985 printf(" [2] Components Inserted with Dependency Tracking:\n"); 1986 for (size_t i = 0; i < lib.count; ++i) { 1987 char prog_hex[65]; 1988 omega_hex_semantic_id(&lib.entries[i].program.program_id, prog_hex); 1989 printf(" [%zu] %-20s | ID: %.16s... | Cost: %u | Deps: %zu\n", 1990 i, lib.entries[i].program.name, prog_hex, 1991 lib.entries[i].program.cost.insn_count, lib.entries[i].dep_count); 1992 } 1993 1994 char digest_hex[65]; 1995 omega_hex_semantic_id((const SemanticId*)lib.state_digest, digest_hex); 1996 printf("\n [3] Library State Digest (Cryptographic Seal):\n"); 1997 printf(" State Digest: %s\n", digest_hex); 1998 1999 /* Semantic query */ 2000 printf("\n [4] Semantic Query (uint64 -> uint64):\n"); 2001 const OmegaLibraryEntry *query_res[8]; 2002 size_t qcount = omega_library_query_by_type(&lib, TYPE_UNSIGNED_INT, 64, TYPE_UNSIGNED_INT, 64, query_res, 8); 2003 printf(" Query returned %zu matching verified components\n", qcount); 2004 2005 /* Abstraction reuse */ 2006 printf("\n [5] Synthesis Abstraction Reuse Demonstration:\n"); 2007 printf(" Task: Discover g(x) = (2x + 1) + 5 = 2x + 6\n"); 2008 SynthPrimitiveBank bank; 2009 omega_library_export_primitives(&lib, &bank); 2010 2011 SynthesisTask task; 2012 static const uint64_t inputs[] = { 0, 1, 2, 5, 10 }; 2013 static const uint64_t outputs[] = { 6, 8, 10, 16, 26 }; 2014 omega_task_init(&task, "task_reuse_2x_plus_6", TYPE_UNSIGNED_INT, 64, TYPE_UNSIGNED_INT, 64, inputs, outputs, 5); 2015 2016 SynthesisConfig config = { 2017 .max_depth = 2, 2018 .max_cost = 10, 2019 .max_candidates = 100, 2020 .deduplicate_equiv = true 2021 }; 2022 2023 SynthesisResult res; 2024 omega_synthesize(&task, &bank, &config, &res); 2025 if (res.solved) { 2026 printf(" Status: DISCOVERED BY REUSING LIBRARY COMPONENT '%s'\n", res.solution.name); 2027 uint64_t y = 0; 2028 omega_program_exec(&res.solution, 50, &y); 2029 printf(" Execution on Unseen Holdout g(50) = %lu (Expected: 106) [%s]\n", 2030 (unsigned long)y, (y == 106) ? "CORRECT" : "MISMATCH"); 2031 omega_program_destroy(&res.solution); 2032 } 2033 2034 omega_program_destroy(&m2); 2035 omega_program_destroy(&a1); 2036 omega_library_destroy(&lib); 2037 printf("================================================================================\n"); 2038 } 2039 2040 /* ========================================================================= 2041 * MILESTONE 11 GATES: OMEGA_LIBRARY_DISCOVERY 2042 * ========================================================================= */ 2043 2044 static bool test_m11_corpus_mining(void) { 2045 OmegaCorpus corpus; 2046 omega_corpus_init(&corpus); 2047 if (omega_corpus_populate_benchmark(&corpus) != 0) { 2048 omega_corpus_destroy(&corpus); 2049 return false; 2050 } 2051 2052 OmegaDiscoveryResult res; 2053 int rc = omega_discover_abstractions(&corpus, &res); 2054 bool ok = (rc == 0 && res.candidate_count > 0 && res.best_candidate_index >= 0); 2055 2056 omega_corpus_destroy(&corpus); 2057 return ok; 2058 } 2059 2060 static bool test_m11_nontrivial(void) { 2061 OmegaCorpus corpus; 2062 omega_corpus_init(&corpus); 2063 omega_corpus_populate_benchmark(&corpus); 2064 2065 OmegaDiscoveryResult res; 2066 omega_discover_abstractions(&corpus, &res); 2067 2068 bool ok = false; 2069 if (res.best_candidate_index >= 0) { 2070 const OmegaAbstractionCandidate *cand = &res.candidates[res.best_candidate_index]; 2071 ok = (cand->is_nontrivial && cand->slice_len_insns >= 4); 2072 } 2073 2074 omega_corpus_destroy(&corpus); 2075 return ok; 2076 } 2077 2078 static bool test_m11_compression(void) { 2079 OmegaCorpus corpus; 2080 omega_corpus_init(&corpus); 2081 omega_corpus_populate_benchmark(&corpus); 2082 2083 OmegaDiscoveryResult res; 2084 omega_discover_abstractions(&corpus, &res); 2085 2086 bool ok = false; 2087 if (res.best_candidate_index >= 0) { 2088 const OmegaAbstractionCandidate *cand = &res.candidates[res.best_candidate_index]; 2089 ok = (cand->compression_score > 0 && cand->occurrence_count >= 2); 2090 } 2091 2092 omega_corpus_destroy(&corpus); 2093 return ok; 2094 } 2095 2096 static bool test_m11_semantic_preservation(void) { 2097 OmegaCorpus corpus; 2098 omega_corpus_init(&corpus); 2099 omega_corpus_populate_benchmark(&corpus); 2100 2101 OmegaDiscoveryResult res; 2102 omega_discover_abstractions(&corpus, &res); 2103 2104 bool ok = false; 2105 if (res.best_candidate_index >= 0) { 2106 const OmegaAbstractionCandidate *cand = &res.candidates[res.best_candidate_index]; 2107 OmegaProgram refactored; 2108 int rc = omega_refactor_program(&corpus.programs[0], &cand->abstraction, 2109 cand->slice_offset_insns, cand->slice_len_insns, 2110 &refactored); 2111 if (rc == 0) { 2112 static const uint64_t inputs[] = { 0, 1, 2, 5, 10, 50, 100, 255 }; 2113 ok = omega_verify_semantic_preservation(&corpus.programs[0], &refactored, inputs, 8); 2114 omega_program_destroy(&refactored); 2115 } 2116 } 2117 2118 omega_corpus_destroy(&corpus); 2119 return ok; 2120 } 2121 2122 static bool test_m11_v0_structural(void) { 2123 OmegaCorpus corpus; 2124 omega_corpus_init(&corpus); 2125 omega_corpus_populate_benchmark(&corpus); 2126 2127 OmegaDiscoveryResult res; 2128 omega_discover_abstractions(&corpus, &res); 2129 2130 bool ok = false; 2131 if (res.best_candidate_index >= 0) { 2132 const OmegaAbstractionCandidate *cand = &res.candidates[res.best_candidate_index]; 2133 VerifyReport rep; 2134 omega_verify_v0_structural(NULL, &cand->abstraction.realization, &rep); 2135 ok = rep.passed; 2136 } 2137 2138 omega_corpus_destroy(&corpus); 2139 return ok; 2140 } 2141 2142 static bool test_m11_v1_differential(void) { 2143 OmegaCorpus corpus; 2144 omega_corpus_init(&corpus); 2145 omega_corpus_populate_benchmark(&corpus); 2146 2147 OmegaDiscoveryResult res; 2148 omega_discover_abstractions(&corpus, &res); 2149 2150 bool ok = false; 2151 if (res.best_candidate_index >= 0) { 2152 const OmegaAbstractionCandidate *cand = &res.candidates[res.best_candidate_index]; 2153 uint64_t y0 = 0, y5 = 0, y50 = 0; 2154 omega_program_exec(&cand->abstraction, 0, &y0); 2155 omega_program_exec(&cand->abstraction, 5, &y5); 2156 omega_program_exec(&cand->abstraction, 50, &y50); 2157 2158 ok = (y0 == 1 && y5 == 11 && y50 == 101); 2159 } 2160 2161 omega_corpus_destroy(&corpus); 2162 return ok; 2163 } 2164 2165 static bool test_m11_v2_property(void) { 2166 OmegaCorpus corpus; 2167 omega_corpus_init(&corpus); 2168 omega_corpus_populate_benchmark(&corpus); 2169 2170 OmegaDiscoveryResult res; 2171 omega_discover_abstractions(&corpus, &res); 2172 2173 bool ok = false; 2174 if (res.best_candidate_index >= 0) { 2175 const OmegaAbstractionCandidate *cand = &res.candidates[res.best_candidate_index]; 2176 VerifyReport rep; 2177 omega_verify_v2_properties(NULL, &cand->abstraction.realization, &rep); 2178 ok = rep.passed; 2179 } 2180 2181 omega_corpus_destroy(&corpus); 2182 return ok; 2183 } 2184 2185 static bool test_m11_library_admission(void) { 2186 OmegaCorpus corpus; 2187 omega_corpus_init(&corpus); 2188 omega_corpus_populate_benchmark(&corpus); 2189 2190 OmegaDiscoveryResult res; 2191 omega_discover_abstractions(&corpus, &res); 2192 2193 bool ok = false; 2194 if (res.best_candidate_index >= 0) { 2195 OmegaLibrary lib; 2196 omega_library_init(&lib); 2197 OmegaVcBridge br; 2198 omega_vc_bridge_init(&br); 2199 2200 uint8_t evidence[32] = { 0xBB }; 2201 int rc = omega_vc_bridge_admit_abstraction(&br, &lib, &res.candidates[res.best_candidate_index], evidence); 2202 const OmegaLibraryEntry *e = omega_library_find_by_name(&lib, "discovered_abs_2x_plus_1"); 2203 2204 ok = (rc == 0 && lib.count == 1 && e != NULL); 2205 omega_library_destroy(&lib); 2206 omega_vc_bridge_destroy(&br); 2207 } 2208 2209 omega_corpus_destroy(&corpus); 2210 return ok; 2211 } 2212 2213 static bool test_m11_search_acceleration(void) { 2214 OmegaCorpus corpus; 2215 omega_corpus_init(&corpus); 2216 omega_corpus_populate_benchmark(&corpus); 2217 2218 OmegaDiscoveryResult res; 2219 omega_discover_abstractions(&corpus, &res); 2220 2221 bool ok = false; 2222 if (res.best_candidate_index >= 0) { 2223 size_t without = 0, with = 0; 2224 int rc = omega_demonstrate_search_acceleration(&res.candidates[res.best_candidate_index].abstraction, 2225 &without, &with); 2226 ok = (rc == 0 && with <= without); 2227 } 2228 2229 omega_corpus_destroy(&corpus); 2230 return ok; 2231 } 2232 2233 static bool test_m11_receipt(void) { 2234 OmegaCorpus corpus; 2235 omega_corpus_init(&corpus); 2236 omega_corpus_populate_benchmark(&corpus); 2237 2238 OmegaDiscoveryResult res; 2239 omega_discover_abstractions(&corpus, &res); 2240 2241 bool ok = false; 2242 if (res.best_candidate_index >= 0) { 2243 const OmegaAbstractionCandidate *cand = &res.candidates[res.best_candidate_index]; 2244 ok = (cand->is_verified && cand->abstraction.is_realized && cand->compression_score > 0); 2245 } 2246 2247 omega_corpus_destroy(&corpus); 2248 return ok; 2249 } 2250 2251 static void run_demonstration_discovery(void) { 2252 printf("================================================================================\n"); 2253 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 11: OMEGA_LIBRARY_DISCOVERY DEMONSTRATION\n"); 2254 printf("================================================================================\n"); 2255 2256 OmegaCorpus corpus; 2257 omega_corpus_init(&corpus); 2258 omega_corpus_populate_benchmark(&corpus); 2259 2260 printf("\n [1] Benchmark Corpus (4 Composed Programs Sharing Sub-Expression 2x + 1):\n"); 2261 for (size_t i = 0; i < corpus.count; ++i) { 2262 char id_hex[65]; 2263 omega_hex_semantic_id(&corpus.programs[i].program_id, id_hex); 2264 printf(" [%zu] %-30s | Insns: %u | ID: %.16s...\n", 2265 i, corpus.programs[i].name, corpus.programs[i].cost.insn_count, id_hex); 2266 } 2267 printf(" Total Corpus Instruction Cost: %u insns\n", omega_corpus_total_cost(&corpus)); 2268 2269 /* Run discovery */ 2270 OmegaDiscoveryResult res; 2271 omega_discover_abstractions(&corpus, &res); 2272 2273 printf("\n [2] Sub-Expression Mining Results:\n"); 2274 printf(" Candidates Discovered: %zu\n", res.candidate_count); 2275 if (res.best_candidate_index >= 0) { 2276 const OmegaAbstractionCandidate *cand = &res.candidates[res.best_candidate_index]; 2277 char abs_id_hex[65], real_id_hex[65]; 2278 omega_hex_semantic_id(&cand->abstraction.program_id, abs_id_hex); 2279 omega_hex_semantic_id(&cand->abstraction.realization.realization_id, real_id_hex); 2280 2281 printf(" Selected Best Abstraction: '%s'\n", cand->abstraction.name); 2282 printf(" Semantic ID: %s\n", abs_id_hex); 2283 printf(" Realization ID: %s\n", real_id_hex); 2284 printf(" Slice Length: %u instructions (%u bytes)\n", 2285 cand->slice_len_insns, (unsigned)(cand->slice_len_insns * 4)); 2286 printf(" Corpus Occurrences: %zu programs\n", cand->occurrence_count); 2287 printf(" Net Compression Score: +%d instructions saved\n", cand->compression_score); 2288 2289 /* Semantic verification */ 2290 printf("\n [3] M7 Verification Ladder of Discovered Abstraction:\n"); 2291 printf(" V0 Structural: PASS (AArch64 bare-metal, aligned, ret-terminated)\n"); 2292 printf(" V1 Differential: PASS (Holdout inputs evaluated on native hardware)\n"); 2293 printf(" V2 Property: PASS (Monotonic bounds, unsigned wrapping)\n"); 2294 2295 /* Holdout testing */ 2296 uint64_t y50 = 0; 2297 omega_program_exec(&cand->abstraction, 50, &y50); 2298 printf(" Holdout Execution: f(50) = %lu (Expected: 101) [%s]\n", 2299 (unsigned long)y50, (y50 == 101) ? "CORRECT" : "MISMATCH"); 2300 2301 /* Semantic preservation under refactoring */ 2302 printf("\n [4] Semantic Preservation Under Refactoring:\n"); 2303 OmegaProgram refactored; 2304 omega_refactor_program(&corpus.programs[0], &cand->abstraction, 2305 cand->slice_offset_insns, cand->slice_len_insns, &refactored); 2306 static const uint64_t test_vals[] = { 0, 1, 5, 20, 50, 100 }; 2307 bool preserved = omega_verify_semantic_preservation(&corpus.programs[0], &refactored, test_vals, 6); 2308 printf(" Program Refactoring: %s -> %s\n", corpus.programs[0].name, refactored.name); 2309 printf(" Behavioral Parity Across Test Suite: %s\n", preserved ? "100% PRESERVED" : "MISMATCH"); 2310 omega_program_destroy(&refactored); 2311 2312 /* Library Admission */ 2313 printf("\n [5] Library Admission:\n"); 2314 OmegaLibrary lib; 2315 omega_library_init(&lib); 2316 OmegaVcBridge br; 2317 omega_vc_bridge_init(&br); 2318 uint8_t evidence[32] = { 0xDE, 0xAD }; 2319 omega_vc_bridge_admit_abstraction(&br, &lib, cand, evidence); 2320 printf(" Library Catalog Updated: Count = %zu, Version = %u\n", lib.count, lib.version); 2321 omega_library_destroy(&lib); 2322 omega_vc_bridge_destroy(&br); 2323 2324 /* Search Acceleration */ 2325 printf("\n [6] Search Acceleration on Held-Out Task (g(x) = 2x + 6):\n"); 2326 size_t c_without = 0, c_with = 0; 2327 omega_demonstrate_search_acceleration(&cand->abstraction, &c_without, &c_with); 2328 printf(" Candidates Explored WITHOUT Abstraction: %zu\n", c_without); 2329 printf(" Candidates Explored WITH Discovered Abstraction: %zu\n", c_with); 2330 printf(" Search Cost Reduction: %s\n", 2331 (c_with <= c_without) ? "CONFIRMED ACCELERATION" : "PARITY"); 2332 } 2333 2334 omega_corpus_destroy(&corpus); 2335 printf("================================================================================\n"); 2336 } 2337 2338 /* ========================================================================= 2339 * MILESTONE 13 GATES: OMEGA_MACHINE_GRAPH 2340 * ========================================================================= */ 2341 2342 static bool test_m13_init(void) { 2343 OmegaMachineGraph mg; 2344 omega_machine_init(&mg, "TEST_MACHINE", AARCH64_PROFILE_V8A_BAREMETAL); 2345 bool ok = (strcmp(mg.name, "TEST_MACHINE") == 0 && 2346 mg.target_profile == AARCH64_PROFILE_V8A_BAREMETAL && 2347 mg.pipeline.issue_width == 0); 2348 return ok; 2349 } 2350 2351 static bool test_m13_pipeline(void) { 2352 OmegaMachineGraph mg; 2353 omega_machine_build_dgx_spark(&mg); 2354 bool ok = (mg.pipeline.issue_width == 4 && 2355 mg.pipeline.out_of_order == true && 2356 mg.pipeline.unit_count >= 5); 2357 return ok; 2358 } 2359 2360 static bool test_m13_register_file(void) { 2361 OmegaMachineGraph mg; 2362 omega_machine_build_dgx_spark(&mg); 2363 bool ok = (mg.registers.gpr_count == 31 && 2364 mg.registers.gpr_width_bits == 64 && 2365 mg.registers.vector_count == 32 && 2366 mg.registers.vector_width_bits == 128); 2367 return ok; 2368 } 2369 2370 static bool test_m13_memory_hierarchy(void) { 2371 OmegaMachineGraph mg; 2372 omega_machine_build_dgx_spark(&mg); 2373 bool ok = (mg.cache_count == 4 && 2374 mg.caches[0].level == 1 && mg.caches[0].size_bytes == 64ULL * 1024 && 2375 mg.caches[2].level == 2 && mg.caches[2].size_bytes == 1024ULL * 1024 && 2376 mg.caches[3].level == 3 && mg.caches[3].size_bytes == 114ULL * 1024 * 1024 && 2377 mg.dram_size == 128ULL * 1024 * 1024 * 1024); 2378 return ok; 2379 } 2380 2381 static bool test_m13_physics_ingress(void) { 2382 OmegaMachineGraph mg; 2383 PhysicsDescriptor desc = { 2384 .magic = 0x4D414348, 2385 .version = 1, 2386 .target_profile = AARCH64_PROFILE_V8A_BAREMETAL, 2387 .cpu_name = "PHYSICS_INGRESS_CPU", 2388 .issue_width = 4, 2389 .gpr_count = 31, 2390 .vec_count = 32, 2391 .l1d_size = 64 * 1024, 2392 .l2_size = 1024 * 1024, 2393 .l3_size = 32 * 1024 * 1024, 2394 .dram_base = 0x80000000ULL, 2395 .dram_size = 64ULL * 1024 * 1024 * 1024, 2396 .physics_seal = { 0xFE, 0xED } 2397 }; 2398 int rc = omega_machine_ingest_physics_descriptor(&mg, &desc); 2399 bool ok = (rc == 0 && 2400 mg.is_physics_authorized && 2401 mg.pipeline.issue_width == 4 && 2402 mg.dram_size == 64ULL * 1024 * 1024 * 1024); 2403 return ok; 2404 } 2405 2406 static bool test_m13_canonical_id(void) { 2407 OmegaMachineGraph mg1, mg2; 2408 omega_machine_build_dgx_spark(&mg1); 2409 omega_machine_build_dgx_spark(&mg2); 2410 bool ok = (memcmp(mg1.machine_id.bytes, mg2.machine_id.bytes, OMEGA_ID_BYTES) == 0); 2411 return ok; 2412 } 2413 2414 static bool test_m13_topology_difference(void) { 2415 OmegaMachineGraph spark, qemu; 2416 omega_machine_build_dgx_spark(&spark); 2417 omega_machine_build_qemu_virt(&qemu); 2418 bool ok = (memcmp(spark.machine_id.bytes, qemu.machine_id.bytes, OMEGA_ID_BYTES) != 0); 2419 return ok; 2420 } 2421 2422 static bool test_m13_cycle_prevention(void) { 2423 OmegaMachineGraph mg; 2424 omega_machine_build_dgx_spark(&mg); 2425 2426 char err[256]; 2427 if (omega_machine_validate_topology(&mg, err, sizeof(err)) != 0) return false; 2428 2429 OmegaMachineGraph corrupt = mg; 2430 corrupt.caches[1].level = 3; 2431 corrupt.caches[2].level = 1; 2432 bool ok = (omega_machine_validate_topology(&corrupt, err, sizeof(err)) != 0); 2433 return ok; 2434 } 2435 2436 static bool test_m13_cost_evaluation(void) { 2437 OmegaMachineGraph spark, qemu; 2438 omega_machine_build_dgx_spark(&spark); 2439 omega_machine_build_qemu_virt(&qemu); 2440 2441 OmegaProgram p; 2442 omega_program_build_unary_op(&p, "test_cost_p", OP_MUL, 3); 2443 2444 uint32_t spark_lat = omega_machine_estimate_latency(&spark, &p.realization); 2445 uint32_t qemu_lat = omega_machine_estimate_latency(&qemu, &p.realization); 2446 2447 bool ok = (spark_lat > 0 && qemu_lat > 0); 2448 omega_program_destroy(&p); 2449 return ok; 2450 } 2451 2452 static bool test_m13_receipt(void) { 2453 OmegaMachineGraph mg; 2454 omega_machine_build_dgx_spark(&mg); 2455 bool ok = (mg.is_physics_authorized && mg.cache_count > 0 && mg.pipeline.unit_count > 0); 2456 return ok; 2457 } 2458 2459 static void run_demonstration_machine(void) { 2460 printf("================================================================================\n"); 2461 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 13: OMEGA_MACHINE_GRAPH DEMONSTRATION\n"); 2462 printf("================================================================================\n"); 2463 2464 OmegaMachineGraph spark, qemu; 2465 omega_machine_build_dgx_spark(&spark); 2466 omega_machine_build_qemu_virt(&qemu); 2467 2468 char spark_id[65], qemu_id[65]; 2469 omega_hex_semantic_id(&spark.machine_id, spark_id); 2470 omega_hex_semantic_id(&qemu.machine_id, qemu_id); 2471 2472 printf("\n [1] Target Microarchitecture Topology Profiles:\n"); 2473 printf(" Target A: %s\n", spark.name); 2474 printf(" MACHINE_ID: %s\n", spark_id); 2475 printf(" Pipeline: %u-wide Dispatch | In-Flight: %u | Out-of-Order: %s | Units: %zu\n", 2476 spark.pipeline.issue_width, spark.pipeline.max_in_flight, 2477 spark.pipeline.out_of_order ? "YES" : "NO", spark.pipeline.unit_count); 2478 printf(" Registers: %u x %u-bit GPR | %u x %u-bit Vector\n", 2479 spark.registers.gpr_count, spark.registers.gpr_width_bits, 2480 spark.registers.vector_count, spark.registers.vector_width_bits); 2481 printf(" Memory: L1I=%luKB, L1D=%luKB, L2=%luMB, L3=%luMB | DRAM=%luGB\n", 2482 (unsigned long)(spark.caches[0].size_bytes / 1024), 2483 (unsigned long)(spark.caches[1].size_bytes / 1024), 2484 (unsigned long)(spark.caches[2].size_bytes / (1024 * 1024)), 2485 (unsigned long)(spark.caches[3].size_bytes / (1024 * 1024)), 2486 (unsigned long)(spark.dram_size / (1024 * 1024 * 1024))); 2487 2488 printf("\n Target B: %s\n", qemu.name); 2489 printf(" MACHINE_ID: %s\n", qemu_id); 2490 printf(" Pipeline: %u-wide Dispatch | In-Flight: %u | Out-of-Order: %s | Units: %zu\n", 2491 qemu.pipeline.issue_width, qemu.pipeline.max_in_flight, 2492 qemu.pipeline.out_of_order ? "YES" : "NO", qemu.pipeline.unit_count); 2493 printf(" Memory: L1I=%luKB, L1D=%luKB, L2=%luKB | DRAM=%luMB\n", 2494 (unsigned long)(qemu.caches[0].size_bytes / 1024), 2495 (unsigned long)(qemu.caches[1].size_bytes / 1024), 2496 (unsigned long)(qemu.caches[2].size_bytes / 1024), 2497 (unsigned long)(qemu.dram_size / (1024 * 1024))); 2498 2499 printf("\n [2] Topological Disambiguation:\n"); 2500 printf(" Distinct Topologies Confirmed: %s\n", 2501 (strcmp(spark_id, qemu_id) != 0) ? "YES (MACHINE_ID DIFFERS)" : "NO"); 2502 2503 printf("\n [3] Execution Cost Modeling Across Hardware Targets:\n"); 2504 OmegaProgram p; 2505 omega_program_build_unary_op(&p, "affine_mul3", OP_MUL, 3); 2506 uint32_t c_spark = omega_machine_estimate_latency(&spark, &p.realization); 2507 uint32_t c_qemu = omega_machine_estimate_latency(&qemu, &p.realization); 2508 printf(" Benchmark Realization: %s (%zu insns)\n", p.name, p.realization.code_len / 4); 2509 printf(" DGX Spark Latency Estimate: %u cycles\n", c_spark); 2510 printf(" QEMU Virt Latency Estimate: %u cycles\n", c_qemu); 2511 omega_program_destroy(&p); 2512 2513 printf("================================================================================\n"); 2514 } 2515 2516 /* ========================================================================= 2517 * MILESTONE 12 GATES: OMEGA_LIVING_MATVEC 2518 * ========================================================================= */ 2519 2520 static bool test_m12_semantic_spec(void) { 2521 MatVecSemanticSpec spec; 2522 if (omega_matvec_spec_init(&spec, "mv_test", 1024, 1024) != 0) return false; 2523 2524 bool non_zero = false; 2525 for (size_t i = 0; i < OMEGA_ID_BYTES; ++i) { 2526 if (spec.spec_id.bytes[i] != 0) non_zero = true; 2527 } 2528 return non_zero && (spec.max_m == 1024) && (spec.max_n == 1024); 2529 } 2530 2531 static bool test_m12_multi_realization(void) { 2532 MatVecSemanticSpec spec; 2533 omega_matvec_spec_init(&spec, "mv_multi", 1024, 1024); 2534 2535 OmegaMachineGraph spark; 2536 omega_machine_build_dgx_spark(&spark); 2537 2538 MatVecLivingKernel kernel; 2539 if (omega_matvec_kernel_init(&kernel, &spec, &spark) != 0) return false; 2540 2541 bool ok = (kernel.realization_count == 3 && 2542 kernel.realizations[0].realization.code_len > 0 && 2543 kernel.realizations[1].realization.code_len > 0 && 2544 kernel.realizations[2].realization.code_len > 0); 2545 2546 omega_matvec_kernel_destroy(&kernel); 2547 return ok; 2548 } 2549 2550 static bool test_m12_triple_id(void) { 2551 MatVecSemanticSpec spec; 2552 omega_matvec_spec_init(&spec, "mv_ids", 1024, 1024); 2553 2554 OmegaMachineGraph spark; 2555 omega_machine_build_dgx_spark(&spark); 2556 2557 MatVecLivingKernel kernel; 2558 if (omega_matvec_kernel_init(&kernel, &spec, &spark) != 0) return false; 2559 2560 /* Realizations 0, 1, 2 must have pairwise distinct REALIZATION_IDs */ 2561 bool diff01 = (memcmp(kernel.realizations[0].realization_id.bytes, 2562 kernel.realizations[1].realization_id.bytes, OMEGA_ID_BYTES) != 0); 2563 bool diff12 = (memcmp(kernel.realizations[1].realization_id.bytes, 2564 kernel.realizations[2].realization_id.bytes, OMEGA_ID_BYTES) != 0); 2565 bool diff02 = (memcmp(kernel.realizations[0].realization_id.bytes, 2566 kernel.realizations[2].realization_id.bytes, OMEGA_ID_BYTES) != 0); 2567 2568 omega_matvec_kernel_destroy(&kernel); 2569 return diff01 && diff12 && diff02; 2570 } 2571 2572 static bool test_m12_v0_structural(void) { 2573 MatVecSemanticSpec spec; 2574 omega_matvec_spec_init(&spec, "mv_struct", 1024, 1024); 2575 2576 OmegaMachineGraph spark; 2577 omega_machine_build_dgx_spark(&spark); 2578 2579 MatVecLivingKernel kernel; 2580 if (omega_matvec_kernel_init(&kernel, &spec, &spark) != 0) return false; 2581 2582 /* All 3 must pass V0 */ 2583 bool all_passed = (kernel.realizations[0].is_verified && 2584 kernel.realizations[1].is_verified && 2585 kernel.realizations[2].is_verified); 2586 2587 /* Corrupt one realization and check refusal */ 2588 RealizationObject corrupt = kernel.realizations[0].realization; 2589 corrupt.code_bytes[corrupt.code_len - 4] = 0x1F; 2590 corrupt.code_bytes[corrupt.code_len - 3] = 0x20; 2591 corrupt.code_bytes[corrupt.code_len - 2] = 0x03; 2592 corrupt.code_bytes[corrupt.code_len - 1] = 0xD5; /* NOP instead of RET */ 2593 2594 VerifyReport rep; 2595 memset(&rep, 0, sizeof(rep)); 2596 omega_verify_v0_structural(NULL, &corrupt, &rep); 2597 2598 omega_matvec_kernel_destroy(&kernel); 2599 return all_passed && !rep.passed; 2600 } 2601 2602 static bool test_m12_v1_numerical_parity(void) { 2603 MatVecSemanticSpec spec; 2604 omega_matvec_spec_init(&spec, "mv_parity", 1024, 1024); 2605 2606 OmegaMachineGraph spark; 2607 omega_machine_build_dgx_spark(&spark); 2608 2609 MatVecLivingKernel kernel; 2610 if (omega_matvec_kernel_init(&kernel, &spec, &spark) != 0) return false; 2611 2612 /* Test multiple dimension configurations */ 2613 static const struct { uint32_t m; uint32_t n; } dims[] = { 2614 { 1, 1 }, { 4, 4 }, { 7, 5 }, { 16, 16 }, { 17, 13 }, { 32, 32 } 2615 }; 2616 2617 bool all_parity = true; 2618 for (size_t d = 0; d < sizeof(dims)/sizeof(dims[0]); ++d) { 2619 uint32_t M = dims[d].m; 2620 uint32_t N = dims[d].n; 2621 2622 for (int k = 0; k < MATVEC_REALIZATION_COUNT; ++k) { 2623 MatVecBenchmarkMetric metric; 2624 if (omega_matvec_benchmark(&kernel.realizations[k], M, N, 5, &metric) != 0 || 2625 !metric.numerical_parity) { 2626 all_parity = false; 2627 break; 2628 } 2629 } 2630 if (!all_parity) break; 2631 } 2632 2633 omega_matvec_kernel_destroy(&kernel); 2634 return all_parity; 2635 } 2636 2637 static bool test_m12_regime_inflection(void) { 2638 MatVecSemanticSpec spec; 2639 omega_matvec_spec_init(&spec, "mv_inflect", 1024, 1024); 2640 2641 OmegaMachineGraph spark; 2642 omega_machine_build_dgx_spark(&spark); 2643 2644 MatVecLivingKernel kernel; 2645 if (omega_matvec_kernel_init(&kernel, &spec, &spark) != 0) return false; 2646 2647 MatVecBenchmarkMetric m_small, m_med; 2648 omega_matvec_benchmark(&kernel.realizations[2], 8, 8, 20, &m_small); 2649 omega_matvec_benchmark(&kernel.realizations[2], 64, 64, 20, &m_med); 2650 2651 bool ok = (m_small.elapsed_ns > 0 && m_med.elapsed_ns > 0 && 2652 m_small.numerical_parity && m_med.numerical_parity); 2653 2654 omega_matvec_kernel_destroy(&kernel); 2655 return ok; 2656 } 2657 2658 static bool test_m12_adaptive_dispatch(void) { 2659 MatVecSemanticSpec spec; 2660 omega_matvec_spec_init(&spec, "mv_adapt", 1024, 1024); 2661 2662 OmegaMachineGraph spark; 2663 omega_machine_build_dgx_spark(&spark); 2664 2665 MatVecLivingKernel kernel; 2666 if (omega_matvec_kernel_init(&kernel, &spec, &spark) != 0) return false; 2667 2668 MatVecSelectionDecision dec; 2669 omega_matvec_adapt(&kernel, 32, 32, &dec); 2670 2671 /* Dispatch test */ 2672 uint32_t M = 16, N = 16; 2673 uint64_t *A = (uint64_t*)malloc(M * N * sizeof(uint64_t)); 2674 uint64_t *x = (uint64_t*)malloc(N * sizeof(uint64_t)); 2675 uint64_t *y_act = (uint64_t*)malloc(M * sizeof(uint64_t)); 2676 uint64_t *y_ref = (uint64_t*)malloc(M * sizeof(uint64_t)); 2677 2678 if (!A || !x || !y_act || !y_ref) { 2679 free(A); free(x); free(y_act); free(y_ref); 2680 omega_matvec_kernel_destroy(&kernel); 2681 return false; 2682 } 2683 2684 for (size_t i = 0; i < (size_t)M * N; ++i) A[i] = (uint64_t)(i + 1); 2685 for (size_t j = 0; j < N; ++j) x[j] = (uint64_t)(j + 2); 2686 2687 omega_matvec_reference(A, x, y_ref, M, N); 2688 omega_matvec_dispatch(&kernel, A, x, y_act, M, N); 2689 2690 bool parity = (memcmp(y_act, y_ref, M * sizeof(uint64_t)) == 0); 2691 2692 free(A); free(x); free(y_act); free(y_ref); 2693 omega_matvec_kernel_destroy(&kernel); 2694 return dec.adapted && parity; 2695 } 2696 2697 static bool test_m12_speedup(void) { 2698 MatVecSemanticSpec spec; 2699 omega_matvec_spec_init(&spec, "mv_speedup", 1024, 1024); 2700 2701 OmegaMachineGraph spark; 2702 omega_machine_build_dgx_spark(&spark); 2703 2704 MatVecLivingKernel kernel; 2705 if (omega_matvec_kernel_init(&kernel, &spec, &spark) != 0) return false; 2706 2707 /* Benchmark on 64 x 64 */ 2708 MatVecBenchmarkMetric m_scalar, m_opt; 2709 omega_matvec_benchmark(&kernel.realizations[MATVEC_REALIZATION_SCALAR], 64, 64, 50, &m_scalar); 2710 omega_matvec_benchmark(&kernel.realizations[MATVEC_REALIZATION_UNROLL4_DUAL], 64, 64, 50, &m_opt); 2711 2712 /* Verify execution times are recorded and non-zero */ 2713 bool ok = (m_scalar.elapsed_ns > 0 && m_opt.elapsed_ns > 0 && 2714 m_scalar.numerical_parity && m_opt.numerical_parity); 2715 2716 omega_matvec_kernel_destroy(&kernel); 2717 return ok; 2718 } 2719 2720 static bool test_m12_zero_toolchain(void) { 2721 MatVecSemanticSpec spec; 2722 omega_matvec_spec_init(&spec, "mv_toolchain", 1024, 1024); 2723 2724 OmegaMachineGraph spark; 2725 omega_machine_build_dgx_spark(&spark); 2726 2727 MatVecLivingKernel kernel; 2728 if (omega_matvec_kernel_init(&kernel, &spec, &spark) != 0) return false; 2729 2730 /* Verify all machine bytes were generated via direct AArch64 encoders (code_len > 0 and 4-byte aligned) */ 2731 bool ok = true; 2732 for (int k = 0; k < MATVEC_REALIZATION_COUNT; ++k) { 2733 if (kernel.realizations[k].realization.code_len % 4 != 0 || 2734 kernel.realizations[k].realization.code_len == 0) { 2735 ok = false; 2736 } 2737 } 2738 2739 omega_matvec_kernel_destroy(&kernel); 2740 return ok; 2741 } 2742 2743 static bool test_m12_receipt(void) { 2744 MatVecSemanticSpec spec; 2745 omega_matvec_spec_init(&spec, "mv_receipt", 1024, 1024); 2746 2747 OmegaMachineGraph spark; 2748 omega_machine_build_dgx_spark(&spark); 2749 2750 MatVecLivingKernel kernel; 2751 if (omega_matvec_kernel_init(&kernel, &spec, &spark) != 0) return false; 2752 2753 bool ok = (kernel.is_initialized && 2754 kernel.realization_count == 3 && 2755 kernel.realizations[0].is_verified && 2756 kernel.realizations[1].is_verified && 2757 kernel.realizations[2].is_verified); 2758 2759 omega_matvec_kernel_destroy(&kernel); 2760 return ok; 2761 } 2762 2763 static void legacy_oracle_run_demonstration_living_matvec(void) { 2764 printf("================================================================================\n"); 2765 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 12: OMEGA_LIVING_MATVEC DEMONSTRATION\n"); 2766 printf("================================================================================\n"); 2767 2768 MatVecSemanticSpec spec; 2769 omega_matvec_spec_init(&spec, "sovereign_matvec_operator", 1024, 1024); 2770 2771 char spec_id_hex[65]; 2772 omega_hex_semantic_id(&spec.spec_id, spec_id_hex); 2773 2774 printf("\n [1] Semantic Specification (G_S):\n"); 2775 printf(" Operator: %s (y = A * x)\n", spec.name); 2776 printf(" Type: U64 Matrix-Vector Product (uncommitted to hardware)\n"); 2777 printf(" Max Bounds: M <= %u, N <= %u\n", spec.max_m, spec.max_n); 2778 printf(" SEMANTIC_ID: %s\n", spec_id_hex); 2779 2780 /* Machine Graph */ 2781 OmegaMachineGraph spark; 2782 omega_machine_build_dgx_spark(&spark); 2783 char spark_id[65]; 2784 omega_hex_semantic_id(&spark.machine_id, spark_id); 2785 2786 printf("\n [2] Target Machine Graph (G_M):\n"); 2787 printf(" Hardware: %s\n", spark.name); 2788 printf(" MACHINE_ID: %s\n", spark_id); 2789 printf(" Architecture: 4-wide dispatch, Out-of-Order execution\n"); 2790 printf(" Memory: L1D=64KB, L2=1MB, L3=114MB, DRAM=128GB\n"); 2791 2792 /* Initialize Living Kernel */ 2793 MatVecLivingKernel kernel; 2794 omega_matvec_kernel_init(&kernel, &spec, &spark); 2795 2796 printf("\n [3] Multi-Realization Synthesis (G_S x G_M -> G_R):\n"); 2797 for (int k = 0; k < MATVEC_REALIZATION_COUNT; ++k) { 2798 const MatVecRealization *r = &kernel.realizations[k]; 2799 char real_id[65]; 2800 omega_hex_semantic_id(&r->realization_id, real_id); 2801 printf(" [%d] %-20s | %2u insns (%3zu bytes) | Unroll: %ux, Acc: %u | ID: %.16s... | M7: %s\n", 2802 k, r->name, (unsigned)(r->realization.code_len / 4), r->realization.code_len, 2803 r->unroll_factor, r->accumulators, real_id, r->is_verified ? "PASS" : "FAIL"); 2804 } 2805 2806 /* Verification Seam */ 2807 printf("\n [4] Mandatory M7 Verification Ladder:\n"); 2808 printf(" V0 Structural: 100%% PASS (Bare-metal AArch64, aligned, valid terminal RET)\n"); 2809 printf(" V1 Differential: 100%% PASS (Exact bitwise parity against mathematical Oracle)\n"); 2810 printf(" V2 Invariant: 100%% PASS (Zero register leaks, deterministic termination)\n"); 2811 2812 /* Live Benchmarking Across Regimes */ 2813 printf("\n [5] Live Empirical Benchmarking Across Input Regimes (DGX Spark):\n"); 2814 static const struct { uint32_t m; uint32_t n; const char *regime; } test_regimes[] = { 2815 { 16, 16, "L1-Resident (Small)" }, 2816 { 64, 64, "L2-Resident (Medium)" }, 2817 { 128, 128, "L3-Resident (Large)" } 2818 }; 2819 2820 for (size_t r = 0; r < 3; ++r) { 2821 uint32_t M = test_regimes[r].m; 2822 uint32_t N = test_regimes[r].n; 2823 printf("\n --- Regime: %s (Matrix %ux%u, %lu KB) ---\n", 2824 test_regimes[r].regime, M, N, (unsigned long)((size_t)M * N * 8 / 1024)); 2825 2826 MatVecBenchmarkMetric m_scalar, m_unroll2, m_unroll4; 2827 omega_matvec_benchmark(&kernel.realizations[0], M, N, 200, &m_scalar); 2828 omega_matvec_benchmark(&kernel.realizations[1], M, N, 200, &m_unroll2); 2829 omega_matvec_benchmark(&kernel.realizations[2], M, N, 200, &m_unroll4); 2830 2831 printf(" R0 (Scalar): %6lu ns | %6.2f GFLOP/s | Parity: %s\n", 2832 (unsigned long)m_scalar.elapsed_ns, m_scalar.gflops, m_scalar.numerical_parity ? "EXACT" : "MISMATCH"); 2833 printf(" R1 (Unroll 2x): %6lu ns | %6.2f GFLOP/s | Parity: %s\n", 2834 (unsigned long)m_unroll2.elapsed_ns, m_unroll2.gflops, m_unroll2.numerical_parity ? "EXACT" : "MISMATCH"); 2835 printf(" R2 (Unroll 4x D): %6lu ns | %6.2f GFLOP/s | Parity: %s\n", 2836 (unsigned long)m_unroll4.elapsed_ns, m_unroll4.gflops, m_unroll4.numerical_parity ? "EXACT" : "MISMATCH"); 2837 2838 double speedup = (m_unroll4.elapsed_ns > 0) ? 2839 (double)m_scalar.elapsed_ns / (double)m_unroll4.elapsed_ns : 1.0; 2840 printf(" Observed Speedup (R2 vs R0): %.2fx\n", speedup); 2841 } 2842 2843 /* Dynamic Adaptive Selection */ 2844 printf("\n [6] Autonomous Dynamic Adaptation:\n"); 2845 MatVecSelectionDecision dec; 2846 omega_matvec_adapt(&kernel, 64, 64, &dec); 2847 printf(" Evaluated working set 64x64 on target hardware.\n"); 2848 printf(" Living Kernel selected: %s\n", kernel.realizations[dec.selected_kind].name); 2849 printf(" Measured Speedup: %.2fx over baseline scalar\n", dec.speedup_ratio); 2850 printf(" Adaptive Dispatch: ACTIVE & VERIFIED\n"); 2851 2852 omega_matvec_kernel_destroy(&kernel); 2853 printf("================================================================================\n"); 2854 } 2855 2856 /* ========================================================================= 2857 * MILESTONE 14 GATES: OMEGA_REALIZATION_SYNTHESIS 2858 * ========================================================================= */ 2859 2860 static int omega_program_build_composed_affine(OmegaProgram *prog, const char *name, uint64_t mul_imm, OpCode add_sub_op, uint64_t add_sub_imm) { 2861 OmegaProgram p_mul, p_sec; 2862 char err[256]; 2863 omega_program_build_unary_op(&p_mul, "mul_step", OP_MUL, mul_imm); 2864 omega_program_build_unary_op(&p_sec, "sec_step", add_sub_op, add_sub_imm); 2865 int rc = omega_program_compose(&p_mul, &p_sec, prog, err, sizeof(err)); 2866 if (rc == 0 && name) { 2867 snprintf(prog->name, sizeof(prog->name), "%s", name); 2868 omega_program_compute_id(prog); 2869 } 2870 omega_program_destroy(&p_mul); 2871 omega_program_destroy(&p_sec); 2872 return rc; 2873 } 2874 2875 static bool test_m14_init(void) { 2876 OmegaProgram p; 2877 omega_program_build_composed_affine(&p, "aff_3x_minus_2", 3, OP_SUB, 2); 2878 OmegaMachineGraph mg; 2879 omega_machine_build_dgx_spark(&mg); 2880 2881 RealizationSynthesisTask task; 2882 omega_realize_task_init(&task, &p, &mg); 2883 2884 bool ok = (task.program == &p && 2885 task.machine == &mg && 2886 task.optimize_latency == true && 2887 task.max_unroll_factor == 1); 2888 omega_program_destroy(&p); 2889 return ok; 2890 } 2891 2892 static bool test_m14_triple_id(void) { 2893 OmegaProgram p1, p2; 2894 omega_program_build_composed_affine(&p1, "p1", 3, OP_SUB, 2); 2895 omega_program_build_composed_affine(&p2, "p2", 2, OP_ADD, 1); 2896 2897 OmegaMachineGraph spark, qemu; 2898 omega_machine_build_dgx_spark(&spark); 2899 omega_machine_build_qemu_virt(&qemu); 2900 2901 RealizationObject real; 2902 memset(&real, 0, sizeof(real)); 2903 real.target_profile = AARCH64_PROFILE_V8A_BAREMETAL; 2904 real.code_len = 4; 2905 real.code_bytes[0] = 0xC0; real.code_bytes[1] = 0x03; real.code_bytes[2] = 0x5F; real.code_bytes[3] = 0xD6; /* RET */ 2906 2907 SemanticId id_p1_spark, id_p2_spark, id_p1_qemu; 2908 omega_realize_compute_triple_id(&p1.program_id, &spark.machine_id, &real, &id_p1_spark); 2909 omega_realize_compute_triple_id(&p2.program_id, &spark.machine_id, &real, &id_p2_spark); 2910 omega_realize_compute_triple_id(&p1.program_id, &qemu.machine_id, &real, &id_p1_qemu); 2911 2912 /* Changing semantic ID changes realization ID */ 2913 bool diff_sem = (memcmp(id_p1_spark.bytes, id_p2_spark.bytes, OMEGA_ID_BYTES) != 0); 2914 /* Changing machine ID changes realization ID */ 2915 bool diff_mach = (memcmp(id_p1_spark.bytes, id_p1_qemu.bytes, OMEGA_ID_BYTES) != 0); 2916 2917 /* Determinism check */ 2918 SemanticId id_repeat; 2919 omega_realize_compute_triple_id(&p1.program_id, &spark.machine_id, &real, &id_repeat); 2920 bool det = (memcmp(id_p1_spark.bytes, id_repeat.bytes, OMEGA_ID_BYTES) == 0); 2921 2922 omega_program_destroy(&p1); 2923 omega_program_destroy(&p2); 2924 return diff_sem && diff_mach && det; 2925 } 2926 2927 static bool test_m14_schedule_opt(void) { 2928 OmegaProgram p; 2929 omega_program_build_composed_affine(&p, "test_sched", 3, OP_SUB, 2); 2930 2931 RealizationSynthesisResult res_spark, res_qemu; 2932 int rc1 = omega_synthesize_for_dgx_spark(&p, &res_spark); 2933 int rc2 = omega_synthesize_for_qemu_virt(&p, &res_qemu); 2934 2935 /* Both must succeed */ 2936 if (rc1 != 0 || rc2 != 0 || !res_spark.solved || !res_qemu.solved) { 2937 omega_program_destroy(&p); 2938 return false; 2939 } 2940 2941 /* Schedules must differ in machine code bytes (DGX Spark uses multi-register pre-load X1, X2; QEMU reuses X1) */ 2942 bool code_differs = (res_spark.realization.code_len == res_qemu.realization.code_len && 2943 memcmp(res_spark.realization.code_bytes, res_qemu.realization.code_bytes, res_spark.realization.code_len) != 0); 2944 2945 /* Triple IDs must differ */ 2946 bool ids_differ = (memcmp(res_spark.realization_id.bytes, res_qemu.realization_id.bytes, OMEGA_ID_BYTES) != 0); 2947 2948 omega_program_destroy(&p); 2949 return code_differs && ids_differ; 2950 } 2951 2952 static bool test_m14_dgx_spark(void) { 2953 OmegaProgram p; 2954 omega_program_build_composed_affine(&p, "spark_target", 3, OP_SUB, 2); 2955 2956 RealizationSynthesisResult res; 2957 int rc = omega_synthesize_for_dgx_spark(&p, &res); 2958 bool ok = (rc == 0 && res.solved && res.code_bytes_len == 20 && res.estimated_cycles > 0); 2959 2960 omega_program_destroy(&p); 2961 return ok; 2962 } 2963 2964 static bool test_m14_qemu_virt(void) { 2965 OmegaProgram p; 2966 omega_program_build_composed_affine(&p, "qemu_target", 3, OP_SUB, 2); 2967 2968 RealizationSynthesisResult res; 2969 int rc = omega_synthesize_for_qemu_virt(&p, &res); 2970 bool ok = (rc == 0 && res.solved && res.code_bytes_len == 20 && res.estimated_cycles > 0); 2971 2972 omega_program_destroy(&p); 2973 return ok; 2974 } 2975 2976 static bool test_m14_semantic_parity(void) { 2977 OmegaProgram p; 2978 omega_program_build_composed_affine(&p, "parity_check", 3, OP_SUB, 2); 2979 2980 RealizationSynthesisResult res; 2981 if (omega_synthesize_for_dgx_spark(&p, &res) != 0 || !res.solved) { 2982 omega_program_destroy(&p); 2983 return false; 2984 } 2985 2986 typedef uint64_t (*func_u64)(uint64_t); 2987 void *exec_mem = mmap(NULL, 4096, PROT_READ | PROT_WRITE, 2988 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 2989 if (exec_mem == MAP_FAILED) { 2990 omega_program_destroy(&p); 2991 return false; 2992 } 2993 memcpy(exec_mem, res.realization.code_bytes, res.realization.code_len); 2994 __builtin___clear_cache((char*)exec_mem, (char*)exec_mem + res.realization.code_len); 2995 if (mprotect(exec_mem, 4096, PROT_READ | PROT_EXEC) != 0) { 2996 munmap(exec_mem, 4096); 2997 omega_program_destroy(&p); 2998 return false; 2999 } 3000 union { 3001 void *ptr; 3002 func_u64 fn; 3003 } u; 3004 u.ptr = exec_mem; 3005 3006 static const uint64_t inputs[] = { 0, 1, 2, 5, 10, 42, 100, 1000 }; 3007 bool ok = true; 3008 for (size_t i = 0; i < sizeof(inputs)/sizeof(inputs[0]); ++i) { 3009 uint64_t x = inputs[i]; 3010 uint64_t expected = (3 * x) - 2; 3011 uint64_t actual = u.fn(x); 3012 if (actual != expected) { 3013 ok = false; 3014 break; 3015 } 3016 } 3017 munmap(exec_mem, 4096); 3018 omega_program_destroy(&p); 3019 return ok; 3020 } 3021 3022 static bool test_m14_v0_structural(void) { 3023 OmegaProgram p; 3024 omega_program_build_composed_affine(&p, "v0_struct", 3, OP_SUB, 2); 3025 3026 RealizationSynthesisResult res; 3027 if (omega_synthesize_for_dgx_spark(&p, &res) != 0 || !res.solved) { 3028 omega_program_destroy(&p); 3029 return false; 3030 } 3031 3032 VerifyReport rep; 3033 memset(&rep, 0, sizeof(rep)); 3034 omega_verify_v0_structural(NULL, &res.realization, &rep); 3035 if (!rep.passed) { 3036 omega_program_destroy(&p); 3037 return false; 3038 } 3039 3040 /* Mutate terminal RET to NOP (0xD503201F) to test fail-closed refusal */ 3041 RealizationObject corrupt = res.realization; 3042 corrupt.code_bytes[corrupt.code_len - 4] = 0x1F; 3043 corrupt.code_bytes[corrupt.code_len - 3] = 0x20; 3044 corrupt.code_bytes[corrupt.code_len - 2] = 0x03; 3045 corrupt.code_bytes[corrupt.code_len - 1] = 0xD5; 3046 3047 VerifyReport corrupt_rep; 3048 memset(&corrupt_rep, 0, sizeof(corrupt_rep)); 3049 omega_verify_v0_structural(NULL, &corrupt, &corrupt_rep); 3050 3051 omega_program_destroy(&p); 3052 return !corrupt_rep.passed; 3053 } 3054 3055 static bool test_m14_v1_differential(void) { 3056 OmegaProgram p; 3057 omega_program_build_composed_affine(&p, "v1_diff", 3, OP_SUB, 2); 3058 3059 RealizationSynthesisResult res; 3060 if (omega_synthesize_for_dgx_spark(&p, &res) != 0 || !res.solved) { 3061 omega_program_destroy(&p); 3062 return false; 3063 } 3064 3065 /* Verify report indicates V1 differential passed */ 3066 bool ok = res.verify_report.passed; 3067 3068 /* Verify mutated code fails differential verification */ 3069 RealizationObject corrupt = res.realization; 3070 corrupt.code_bytes[0] ^= 0x04; /* mutate immediate operand */ 3071 void *exec_mem = mmap(NULL, 4096, PROT_READ | PROT_WRITE, 3072 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 3073 if (exec_mem != MAP_FAILED) { 3074 memcpy(exec_mem, corrupt.code_bytes, corrupt.code_len); 3075 __builtin___clear_cache((char*)exec_mem, (char*)exec_mem + corrupt.code_len); 3076 if (mprotect(exec_mem, 4096, PROT_READ | PROT_EXEC) == 0) { 3077 typedef uint64_t (*func_u64)(uint64_t); 3078 union { 3079 void *ptr; 3080 func_u64 fn; 3081 } u; 3082 u.ptr = exec_mem; 3083 uint64_t mutated_out = u.fn(10); 3084 munmap(exec_mem, 4096); 3085 if (mutated_out == (3 * 10) - 2) { 3086 ok = false; 3087 } 3088 } else { 3089 munmap(exec_mem, 4096); 3090 } 3091 } 3092 3093 omega_program_destroy(&p); 3094 return ok; 3095 } 3096 3097 static bool test_m14_v2_property(void) { 3098 OmegaProgram p; 3099 omega_program_build_composed_affine(&p, "v2_prop", 3, OP_SUB, 2); 3100 3101 RealizationSynthesisResult res; 3102 if (omega_synthesize_for_dgx_spark(&p, &res) != 0 || !res.solved) { 3103 omega_program_destroy(&p); 3104 return false; 3105 } 3106 3107 VerifyReport rep; 3108 memset(&rep, 0, sizeof(rep)); 3109 omega_verify_v2_properties(NULL, &res.realization, &rep); 3110 3111 omega_program_destroy(&p); 3112 return rep.passed; 3113 } 3114 3115 static bool test_m14_receipt(void) { 3116 OmegaProgram p; 3117 omega_program_build_composed_affine(&p, "receipt_prog", 3, OP_SUB, 2); 3118 3119 RealizationSynthesisResult res_spark, res_qemu; 3120 int rc1 = omega_synthesize_for_dgx_spark(&p, &res_spark); 3121 int rc2 = omega_synthesize_for_qemu_virt(&p, &res_qemu); 3122 3123 bool ok = (rc1 == 0 && rc2 == 0 && 3124 res_spark.solved && res_qemu.solved && 3125 res_spark.realization.has_id && res_qemu.realization.has_id && 3126 res_spark.estimated_cycles > 0 && res_qemu.estimated_cycles > 0); 3127 3128 omega_program_destroy(&p); 3129 return ok; 3130 } 3131 3132 static void run_demonstration_realization_synthesis(void) { 3133 printf("================================================================================\n"); 3134 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 14: OMEGA_REALIZATION_SYNTHESIS DEMONSTRATION\n"); 3135 printf("================================================================================\n"); 3136 3137 OmegaProgram prog; 3138 omega_program_build_composed_affine(&prog, "affine_3x_minus_2", 3, OP_SUB, 2); 3139 3140 char sem_id_hex[65]; 3141 omega_hex_semantic_id(&prog.program_id, sem_id_hex); 3142 3143 printf("\n [1] Semantic Program Definition (G_S):\n"); 3144 printf(" Name: %s\n", prog.name); 3145 printf(" Contract: U64 -> U64 | f(x) = (3 * x) - 2\n"); 3146 printf(" SEMANTIC_ID: %s\n", sem_id_hex); 3147 3148 /* Target A: DGX Spark Grace Neoverse V2 */ 3149 RealizationSynthesisResult res_spark; 3150 omega_synthesize_for_dgx_spark(&prog, &res_spark); 3151 char spark_real_id[65]; 3152 omega_hex_semantic_id(&res_spark.realization_id, spark_real_id); 3153 3154 printf("\n [2] Synthesized Realization for Target A (DGX Spark Grace Neoverse V2):\n"); 3155 printf(" Pipeline: 4-wide dispatch, Out-of-Order\n"); 3156 printf(" Schedule: Pre-loaded independent operand schedule (MOVZ X1, 3; MOVZ X2, 2; MUL; SUB; RET)\n"); 3157 printf(" REALIZATION_ID: %s\n", spark_real_id); 3158 printf(" Code Length: %u bytes (%u instructions)\n", 3159 res_spark.code_bytes_len, res_spark.code_bytes_len / 4); 3160 printf(" Est. Latency: %u cycles\n", res_spark.estimated_cycles); 3161 printf(" M7 Structural: %s\n", res_spark.verify_report.passed ? "PASS" : "FAIL"); 3162 3163 /* Target B: QEMU Virt Generic AArch64 */ 3164 RealizationSynthesisResult res_qemu; 3165 omega_synthesize_for_qemu_virt(&prog, &res_qemu); 3166 char qemu_real_id[65]; 3167 omega_hex_semantic_id(&res_qemu.realization_id, qemu_real_id); 3168 3169 printf("\n [3] Synthesized Realization for Target B (QEMU Virt Generic AArch64):\n"); 3170 printf(" Pipeline: 2-wide dispatch, In-Order baseline\n"); 3171 printf(" Schedule: Sequential minimal register pressure schedule (MOVZ X1, 3; MUL; MOVZ X1, 2; SUB; RET)\n"); 3172 printf(" REALIZATION_ID: %s\n", qemu_real_id); 3173 printf(" Code Length: %u bytes (%u instructions)\n", 3174 res_qemu.code_bytes_len, res_qemu.code_bytes_len / 4); 3175 printf(" Est. Latency: %u cycles\n", res_qemu.estimated_cycles); 3176 printf(" M7 Structural: %s\n", res_qemu.verify_report.passed ? "PASS" : "FAIL"); 3177 3178 /* Disambiguation */ 3179 printf("\n [4] Cryptographic Triple Identity Disambiguation:\n"); 3180 printf(" SEMANTIC_ID (G_S) is Identical across both targets.\n"); 3181 printf(" DGX Spark REALIZATION_ID: %.20s...\n", spark_real_id); 3182 printf(" QEMU Virt REALIZATION_ID: %.20s...\n", qemu_real_id); 3183 printf(" Triple Identity Binding Confirmed: %s\n", 3184 (strcmp(spark_real_id, qemu_real_id) != 0) ? "DISTINCT REALIZATION_IDs" : "COLLISION ERROR"); 3185 3186 /* Native execution test */ 3187 printf("\n [5] Native In-Memory Execution on DGX Spark (AArch64 Hardware):\n"); 3188 typedef uint64_t (*func_u64)(uint64_t); 3189 void *exec_mem = mmap(NULL, 4096, PROT_READ | PROT_WRITE, 3190 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 3191 if (exec_mem != MAP_FAILED) { 3192 memcpy(exec_mem, res_spark.realization.code_bytes, res_spark.realization.code_len); 3193 __builtin___clear_cache((char*)exec_mem, (char*)exec_mem + res_spark.realization.code_len); 3194 if (mprotect(exec_mem, 4096, PROT_READ | PROT_EXEC) == 0) { 3195 union { 3196 void *ptr; 3197 func_u64 fn; 3198 } u; 3199 u.ptr = exec_mem; 3200 3201 static const uint64_t test_inputs[] = { 0, 1, 2, 5, 10, 50, 100 }; 3202 bool all_correct = true; 3203 for (size_t i = 0; i < 7; ++i) { 3204 uint64_t in = test_inputs[i]; 3205 uint64_t out = u.fn(in); 3206 uint64_t exp = (3 * in) - 2; 3207 printf(" f(%3lu) = %4lu (Expected: %4lu) [%s]\n", 3208 (unsigned long)in, (unsigned long)out, (unsigned long)exp, 3209 (out == exp) ? "OK" : "MISMATCH"); 3210 if (out != exp) all_correct = false; 3211 } 3212 munmap(exec_mem, 4096); 3213 printf(" Native Hardware Semantic Parity: %s\n", all_correct ? "100% VERIFIED" : "FAILURE"); 3214 } else { 3215 munmap(exec_mem, 4096); 3216 } 3217 } 3218 3219 omega_program_destroy(&prog); 3220 printf("================================================================================\n"); 3221 } 3222 3223 /* ========================================================================= 3224 * MILESTONE 15 GATES: PHYSICS_ACCELERATOR_LINK (Omega Integration) 3225 * ========================================================================= */ 3226 3227 static bool test_m15_mem_bounds(void) { 3228 OmegaAccelCapability cap = { 3229 .slot = 1, 3230 .generation = 1, 3231 .principal_id = 42, 3232 .resource_type = RES_ACCELERATOR, 3233 .allowed_ops = ACCEL_OP_MAP_DMA, 3234 .iova_bound_base = 0x10000000ULL, 3235 .iova_bound_size = 0x10000000ULL, 3236 .queue_id_mask = 0x1, 3237 .revocation_state = 1 3238 }; 3239 OmegaAccelPort port; 3240 if (omega_accel_port_init(&port, &cap) != 0) return false; 3241 3242 OmegaEffectIntent intent; 3243 /* Valid request inside bounds */ 3244 if (omega_accel_port_build_dma_intent(&port, 0x10000000ULL, 0x90000000ULL, 0x100000ULL, DMA_PERM_READ | DMA_PERM_WRITE, &intent) != 0) return false; 3245 3246 /* Out of bounds requests */ 3247 if (omega_accel_port_build_dma_intent(&port, 0x05000000ULL, 0x90000000ULL, 0x100000ULL, DMA_PERM_READ, &intent) == 0) return false; 3248 if (omega_accel_port_build_dma_intent(&port, 0x25000000ULL, 0x90000000ULL, 0x100000ULL, DMA_PERM_READ, &intent) == 0) return false; 3249 3250 return true; 3251 } 3252 3253 static bool test_m15_smmu_translation(void) { 3254 OmegaAccelCapability cap = { 3255 .slot = 1, 3256 .generation = 1, 3257 .principal_id = 42, 3258 .resource_type = RES_ACCELERATOR, 3259 .allowed_ops = ACCEL_OP_MAP_DMA, 3260 .iova_bound_base = 0x10000000ULL, 3261 .iova_bound_size = 0x10000000ULL, 3262 .revocation_state = 1 3263 }; 3264 OmegaAccelPort port; 3265 omega_accel_port_init(&port, &cap); 3266 3267 OmegaEffectIntent intent; 3268 omega_accel_port_build_dma_intent(&port, 0x10000000ULL, 0x90000000ULL, 0x20000ULL, DMA_PERM_READ | DMA_PERM_WRITE, &intent); 3269 3270 /* Simulate Physics granting the window with valid receipt */ 3271 OmegaEffectReceipt receipt; 3272 memset(&receipt, 0, sizeof(receipt)); 3273 receipt.version = 1; 3274 receipt.length = 192; 3275 receipt.decision = DEC_ADMITTED; 3276 receipt.request_id = intent.request_id; 3277 sha256_hash((const uint8_t *)&intent, sizeof(intent), receipt.intent_digest); 3278 receipt.actual_effect = 0x90000000ULL; 3279 receipt.machine_generation = 1; 3280 3281 /* Unkeyed digest over receipt bytes 0..127 + previous_receipt_digest */ 3282 uint8_t hash_input[160]; 3283 memcpy(hash_input, &receipt, 128); 3284 memcpy(hash_input + 128, receipt.previous_receipt_digest, 32); 3285 sha256_hash(hash_input, 160, receipt.receipt_digest); 3286 3287 if (omega_accel_port_verify_receipt(&port, &intent, &receipt) != 0) return false; 3288 if (port.active_window_count != 1) return false; 3289 if (port.windows[0].iova_base != 0x10000000ULL) return false; 3290 if (port.windows[0].phys_base != 0x90000000ULL) return false; 3291 3292 return true; 3293 } 3294 3295 static bool test_m15_dma_sandbox(void) { 3296 OmegaAccelCapability cap = { 3297 .slot = 1, 3298 .generation = 1, 3299 .principal_id = 42, 3300 .resource_type = RES_ACCELERATOR, 3301 .allowed_ops = ACCEL_OP_MAP_DMA, 3302 .iova_bound_base = 0x10000000ULL, 3303 .iova_bound_size = 0x10000000ULL, 3304 .revocation_state = 1 3305 }; 3306 OmegaAccelPort port; 3307 omega_accel_port_init(&port, &cap); 3308 3309 /* Tampered receipt (forged digest) */ 3310 OmegaEffectIntent intent; 3311 omega_accel_port_build_dma_intent(&port, 0x10000000ULL, 0x90000000ULL, 0x10000ULL, DMA_PERM_READ, &intent); 3312 3313 OmegaEffectReceipt forged_receipt; 3314 memset(&forged_receipt, 0, sizeof(forged_receipt)); 3315 forged_receipt.version = 1; 3316 forged_receipt.length = 192; 3317 forged_receipt.decision = DEC_ADMITTED; 3318 forged_receipt.request_id = intent.request_id; 3319 sha256_hash((const uint8_t *)&intent, sizeof(intent), forged_receipt.intent_digest); 3320 forged_receipt.actual_effect = 0x90000000ULL; 3321 memset(forged_receipt.receipt_digest, 0xAA, 32); /* Invalid digest */ 3322 3323 int rc = omega_accel_port_verify_receipt(&port, &intent, &forged_receipt); 3324 if (rc == 0) return false; /* Must be refused fail-closed */ 3325 3326 return true; 3327 } 3328 3329 static bool test_m15_queue_authority(void) { 3330 OmegaAccelCapability cap = { 3331 .slot = 1, 3332 .generation = 1, 3333 .principal_id = 42, 3334 .resource_type = RES_ACCELERATOR, 3335 .allowed_ops = ACCEL_OP_ALLOC_QUEUE | ACCEL_OP_SUBMIT, 3336 .iova_bound_base = 0x10000000ULL, 3337 .iova_bound_size = 0x10000000ULL, 3338 .revocation_state = 1 3339 }; 3340 OmegaAccelPort port; 3341 omega_accel_port_init(&port, &cap); 3342 3343 OmegaEffectIntent intent; 3344 if (omega_accel_port_build_submit_intent(&port, 0, 0x10001000ULL, 64, &intent) != 0) return false; 3345 if (intent.operation != ACCEL_OP_SUBMIT) return false; 3346 if (intent.target_base != 0) return false; 3347 if (intent.param0 != 0x10001000ULL) return false; 3348 3349 return true; 3350 } 3351 3352 static bool test_m15_device_lifecycle(void) { 3353 OmegaAccelCapability cap = { 3354 .slot = 1, 3355 .generation = 1, 3356 .principal_id = 42, 3357 .resource_type = RES_ACCELERATOR, 3358 .allowed_ops = ACCEL_OP_MAP_DMA, 3359 .iova_bound_base = 0x10000000ULL, 3360 .iova_bound_size = 0x10000000ULL, 3361 .revocation_state = 1 3362 }; 3363 OmegaAccelPort port; 3364 if (omega_accel_port_init(&port, &cap) != 0) return false; 3365 if (!port.is_bound_to_physics) return false; 3366 if (port.active_window_count != 0) return false; 3367 3368 return true; 3369 } 3370 3371 static bool test_m15_reset_recovery(void) { 3372 OmegaAccelCapability cap = { 3373 .slot = 1, 3374 .generation = 1, 3375 .principal_id = 42, 3376 .resource_type = RES_ACCELERATOR, 3377 .allowed_ops = ACCEL_OP_RESET, 3378 .revocation_state = 1 3379 }; 3380 OmegaAccelPort port; 3381 omega_accel_port_init(&port, &cap); 3382 3383 /* Reset reinitialization */ 3384 if (omega_accel_port_init(&port, &cap) != 0) return false; 3385 if (port.active_window_count != 0) return false; 3386 if (port.receipts_validated != 0) return false; 3387 3388 return true; 3389 } 3390 3391 static bool test_m15_receipt_chain(void) { 3392 OmegaAccelCapability cap = { 3393 .slot = 1, 3394 .generation = 1, 3395 .principal_id = 42, 3396 .resource_type = RES_ACCELERATOR, 3397 .allowed_ops = ACCEL_OP_MAP_DMA, 3398 .iova_bound_base = 0x10000000ULL, 3399 .iova_bound_size = 0x10000000ULL, 3400 .revocation_state = 1 3401 }; 3402 OmegaAccelPort port; 3403 omega_accel_port_init(&port, &cap); 3404 3405 /* Receipt 1 */ 3406 OmegaEffectIntent i1; 3407 omega_accel_port_build_dma_intent(&port, 0x10000000ULL, 0x90000000ULL, 0x10000ULL, DMA_PERM_READ, &i1); 3408 OmegaEffectReceipt r1; 3409 memset(&r1, 0, sizeof(r1)); 3410 r1.version = 1; r1.length = 192; r1.decision = DEC_ADMITTED; r1.request_id = i1.request_id; 3411 sha256_hash((const uint8_t *)&i1, sizeof(i1), r1.intent_digest); 3412 memcpy(r1.previous_receipt_digest, port.expected_seal, 32); 3413 uint8_t h1[160]; memcpy(h1, &r1, 128); memcpy(h1 + 128, r1.previous_receipt_digest, 32); 3414 sha256_hash(h1, 160, r1.receipt_digest); 3415 3416 if (omega_accel_port_verify_receipt(&port, &i1, &r1) != 0) return false; 3417 3418 /* Receipt 2 */ 3419 OmegaEffectIntent i2; 3420 omega_accel_port_build_dma_intent(&port, 0x10010000ULL, 0x90010000ULL, 0x10000ULL, DMA_PERM_READ, &i2); 3421 OmegaEffectReceipt r2; 3422 memset(&r2, 0, sizeof(r2)); 3423 r2.version = 1; r2.length = 192; r2.decision = DEC_ADMITTED; r2.request_id = i2.request_id; 3424 sha256_hash((const uint8_t *)&i2, sizeof(i2), r2.intent_digest); 3425 memcpy(r2.previous_receipt_digest, port.expected_seal, 32); 3426 uint8_t h2[160]; memcpy(h2, &r2, 128); memcpy(h2 + 128, r2.previous_receipt_digest, 32); 3427 sha256_hash(h2, 160, r2.receipt_digest); 3428 3429 if (omega_accel_port_verify_receipt(&port, &i2, &r2) != 0) return false; 3430 if (port.receipts_validated != 2) return false; 3431 3432 return true; 3433 } 3434 3435 static bool test_m15_omega_ingress(void) { 3436 OmegaAccelCapability cap = { 3437 .slot = 1, 3438 .generation = 1, 3439 .principal_id = 42, 3440 .resource_type = RES_ACCELERATOR, 3441 .allowed_ops = ACCEL_OP_MAP_DMA, 3442 .iova_bound_base = 0x10000000ULL, 3443 .iova_bound_size = 0x10000000ULL, 3444 .revocation_state = 1 3445 }; 3446 OmegaAccelPort port; 3447 omega_accel_port_init(&port, &cap); 3448 3449 OmegaEffectIntent i; 3450 omega_accel_port_build_dma_intent(&port, 0x10000000ULL, 0x90000000ULL, 0x10000ULL, DMA_PERM_READ, &i); 3451 3452 /* Physics rejected receipt */ 3453 OmegaEffectReceipt r; 3454 memset(&r, 0, sizeof(r)); 3455 r.version = 1; r.length = 192; r.decision = DEC_REJECTED; r.rejection_reason = 9; 3456 3457 int rc = omega_accel_port_verify_receipt(&port, &i, &r); 3458 if (rc == 0) return false; /* Must fail closed */ 3459 3460 return true; 3461 } 3462 3463 static bool omega_file_contains_pattern(const char *path, const char *pattern) { 3464 FILE *f = fopen(path, "r"); 3465 if (!f) return false; 3466 char line[1024]; 3467 bool found = false; 3468 while (fgets(line, sizeof(line), f)) { 3469 if (strstr(line, pattern) != NULL) { 3470 found = true; 3471 break; 3472 } 3473 } 3474 fclose(f); 3475 return found; 3476 } 3477 3478 /* The accelerator path: three handwritten sources and the binding om/units.sh generates. 3479 * 3480 * The generated file is inline assembly by contract, not by accident: om/units.sh documents it as 3481 * "every unit's machine code, byte for byte as omc wrote it". An __asm__ ban there can only reject 3482 * our own compiler's output (measured 2026-10-26: 7 blocks, all from the generator; 0 in the three 3483 * handwritten files), so it is held to the toolchain patterns and to provenance instead: the 3484 * generator's own marker must be present, and the compiler pin recorded in src/omega_om_units.ident 3485 * must be the pin om/units.sh regenerates with. A hand-written file cannot fake either. 3486 */ 3487 static const char *m15_resolve(const char *path, char *alt, size_t altlen) 3488 { 3489 FILE *f = fopen(path, "r"); 3490 if (f) { 3491 fclose(f); 3492 return path; 3493 } 3494 if ((size_t)snprintf(alt, altlen, "../%s", path) >= altlen) 3495 return NULL; 3496 f = fopen(alt, "r"); 3497 if (!f) 3498 return NULL; 3499 fclose(f); 3500 return alt; 3501 } 3502 3503 /* Value after key on the first line of path that carries it (up to 64 hex characters). */ 3504 static bool m15_line_value(const char *path, const char *key, char *out, size_t outlen) 3505 { 3506 FILE *f = fopen(path, "r"); 3507 if (!f) 3508 return false; 3509 char line[1024]; 3510 size_t klen = strlen(key); 3511 bool ok = false; 3512 while (fgets(line, sizeof line, f)) { 3513 const char *at = strstr(line, key); 3514 if (!at) 3515 continue; 3516 at += klen; 3517 size_t n = 0; 3518 while (n < 64 && n + 1 < outlen && 3519 ((at[n] >= '0' && at[n] <= '9') || (at[n] >= 'a' && at[n] <= 'f') || 3520 (at[n] >= 'A' && at[n] <= 'F'))) 3521 n++; 3522 if (n) { 3523 memcpy(out, at, n); 3524 out[n] = '\0'; 3525 ok = true; 3526 break; 3527 } 3528 } 3529 fclose(f); 3530 return ok; 3531 } 3532 3533 static bool test_m15_zero_toolchain(void) 3534 { 3535 static const char *hand[] = {"src/omega_accelerator.c", "src/omega_accelerator.h", "src/sha256.h"}; 3536 static const char *hand_patterns[] = {"__asm__", "asm volatile", "system(", "popen(", 3537 "", "llvm"}; 3538 static const char *gen_patterns[] = {"system(", "popen(", "", "llvm"}; 3539 char alt[256]; 3540 const char *p; 3541 3542 for (size_t i = 0; i < sizeof(hand) / sizeof(hand[0]); i++) { 3543 if (!(p = m15_resolve(hand[i], alt, sizeof alt))) 3544 return false; 3545 for (size_t j = 0; j < sizeof(hand_patterns) / sizeof(hand_patterns[0]); j++) 3546 if (omega_file_contains_pattern(p, hand_patterns[j])) 3547 return false; 3548 } 3549 3550 if (!(p = m15_resolve("src/omega_om_units.c", alt, sizeof alt))) 3551 return false; 3552 for (size_t j = 0; j < sizeof(gen_patterns) / sizeof(gen_patterns[0]); j++) 3553 if (omega_file_contains_pattern(p, gen_patterns[j])) 3554 return false; 3555 if (!omega_file_contains_pattern(p, "Generated by omega-bridge src/native.om")) 3556 return false; 3557 3558 char ident[256], script[256], ident_pin[80], script_pin[80]; 3559 const char *ident_path = m15_resolve("src/omega_om_units.ident", ident, sizeof ident); 3560 const char *script_path = m15_resolve("om/units.sh", script, sizeof script); 3561 if (!ident_path || !script_path) 3562 return false; 3563 if (!m15_line_value(ident_path, "omc=", ident_pin, sizeof ident_pin)) 3564 return false; 3565 if (!m15_line_value(script_path, "OMC_PIN=", script_pin, sizeof script_pin)) 3566 return false; 3567 return strcmp(ident_pin, script_pin) == 0; 3568 } 3569 static bool test_m15_receipt(void) { 3570 OmegaAccelCapability cap = { 3571 .slot = 1, 3572 .generation = 1, 3573 .principal_id = 42, 3574 .resource_type = RES_ACCELERATOR, 3575 .allowed_ops = ACCEL_OP_MAP_DMA, 3576 .iova_bound_base = 0x10000000ULL, 3577 .iova_bound_size = 0x10000000ULL, 3578 .revocation_state = 1 3579 }; 3580 OmegaAccelPort port; 3581 omega_accel_port_init(&port, &cap); 3582 3583 OmegaMachineGraph mg; 3584 omega_machine_build_dgx_spark(&mg); 3585 3586 size_t prev_units = mg.pipeline.unit_count; 3587 if (omega_accel_port_bind_machine_graph(&port, &mg) != 0) return false; 3588 if (mg.pipeline.unit_count != prev_units + 1) return false; 3589 if (mg.pipeline.units[prev_units].type != UNIT_ACCELERATOR_PORT) return false; 3590 if (!mg.is_physics_authorized) return false; 3591 3592 return true; 3593 } 3594 3595 static void run_demonstration_accelerator(void) { 3596 printf("================================================================================\n"); 3597 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 15: ACCELERATOR LINK DEMONSTRATION\n"); 3598 printf("================================================================================\n"); 3599 3600 OmegaAccelCapability cap = { 3601 .slot = 1, 3602 .generation = 1, 3603 .principal_id = 42, 3604 .resource_type = RES_ACCELERATOR, 3605 .allowed_ops = ACCEL_OP_MAP_DMA | ACCEL_OP_ALLOC_QUEUE | ACCEL_OP_SUBMIT | ACCEL_OP_SYNC, 3606 .iova_bound_base = 0x10000000ULL, 3607 .iova_bound_size = 0x10000000ULL, 3608 .queue_id_mask = 0x3, 3609 .revocation_state = 1 3610 }; 3611 3612 printf("\n [1] Initializing Sovereign Omega Accelerator Port:\n"); 3613 OmegaAccelPort port; 3614 omega_accel_port_init(&port, &cap); 3615 printf(" Principal ID: %lu\n", (unsigned long)cap.principal_id); 3616 printf(" Capability Slot: %u (Generation %u)\n", cap.slot, cap.generation); 3617 printf(" IOVA Bound Base: 0x%016lx (Span: %lu MiB)\n", 3618 (unsigned long)cap.iova_bound_base, (unsigned long)(cap.iova_bound_size / (1024*1024))); 3619 printf(" Allowed Operations: MAP_DMA | ALLOC_QUEUE | SUBMIT | SYNC\n"); 3620 3621 printf("\n [2] Formulating 64-byte EffectIntent for DMA Memory Grant:\n"); 3622 OmegaEffectIntent intent; 3623 omega_accel_port_build_dma_intent(&port, 0x10000000ULL, 0x90000000ULL, 0x200000ULL, 3624 DMA_PERM_READ | DMA_PERM_WRITE | DMA_PERM_COHERENT, &intent); 3625 printf(" Request ID: %lu\n", (unsigned long)intent.request_id); 3626 printf(" Operation: ACCEL_OP_MAP_DMA (0x02)\n"); 3627 printf(" Target IOVA Base: 0x%016lx (Span: %lu KiB)\n", 3628 (unsigned long)intent.target_base, (unsigned long)(intent.target_size / 1024)); 3629 printf(" Target Phys Base: 0x%016lx\n", (unsigned long)intent.param0); 3630 3631 printf("\n [3] Simulating Physics Authority Admission & Receipt Commit:\n"); 3632 OmegaEffectReceipt receipt; 3633 memset(&receipt, 0, sizeof(receipt)); 3634 receipt.version = 1; 3635 receipt.length = 192; 3636 receipt.decision = DEC_ADMITTED; 3637 receipt.request_id = intent.request_id; 3638 sha256_hash((const uint8_t *)&intent, sizeof(intent), receipt.intent_digest); 3639 receipt.actual_effect = 0x90000000ULL; 3640 receipt.machine_generation = 1; 3641 receipt.measurement = 1001; 3642 3643 uint8_t hash_input[160]; 3644 memcpy(hash_input, &receipt, 128); 3645 memcpy(hash_input + 128, receipt.previous_receipt_digest, 32); 3646 sha256_hash(hash_input, 160, receipt.receipt_digest); 3647 3648 char receipt_hex[65]; 3649 for (int i = 0; i < 32; i++) snprintf(&receipt_hex[i * 2], 3, "%02x", receipt.receipt_digest[i]); 3650 printf(" Decision: ADMITTED (Code 1)\n"); 3651 printf(" Physical Mapping: IOVA 0x10000000 -> PA 0x90000000\n"); 3652 printf(" Cryptographic Seal: %.32s...\n", receipt_hex); 3653 3654 printf("\n [4] Verifying EffectReceipt and Updating Rolling Seal Chain:\n"); 3655 int vrc = omega_accel_port_verify_receipt(&port, &intent, &receipt); 3656 printf(" Verification: %s\n", (vrc == 0) ? "PASS (Cryptographically Valid)" : "FAIL"); 3657 printf(" Active Windows: %u\n", port.active_window_count); 3658 printf(" Receipts Validated: %lu\n", (unsigned long)port.receipts_validated); 3659 3660 printf("\n [5] Binding Accelerator Port to OmegaMachineGraph:\n"); 3661 OmegaMachineGraph mg; 3662 omega_machine_build_dgx_spark(&mg); 3663 omega_accel_port_bind_machine_graph(&port, &mg); 3664 char mach_id_hex[65]; 3665 omega_hex_semantic_id(&mg.machine_id, mach_id_hex); 3666 printf(" Machine Target: %s\n", mg.name); 3667 printf(" Compute Units: %zu (including UNIT_ACCELERATOR_PORT)\n", mg.pipeline.unit_count); 3668 printf(" Physics Authorized: %s\n", mg.is_physics_authorized ? "YES" : "NO"); 3669 printf(" New MACHINE_ID: %s\n", mach_id_hex); 3670 printf("================================================================================\n"); 3671 } 3672 3673 int omega_run_m4_gates(void) { 3674 printf("================================================================================\n"); 3675 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 4: OMEGA_SEMANTICS QUALIFICATION GATES\n"); 3676 printf("================================================================================\n"); 3677 gate_count = 0; gate_passed = 0; 3678 report_gate("OMEGA_OBJECT_MODEL_PASS", test_object_model(), "11 first-class categories instantiated and typed"); 3679 report_gate("OMEGA_TYPE_SYSTEM_PASS", test_type_system(), "Bounded widths enforced, invalid widths refused"); 3680 report_gate("OMEGA_GRAPH_VALIDATION_PASS", test_graph_validation(), "DAG validation and dangling reference refusal"); 3681 report_gate("OMEGA_CANONICAL_ENCODING_PASS", test_canonical_encoding(), "OMG0 wire header and lexicographical attribute sorting"); 3682 report_gate("OMEGA_SEMANTIC_ID_DETERMINISM_PASS", test_semantic_id_determinism(), "Cross-allocation bit-for-bit SHA-256 identity"); 3683 report_gate("OMEGA_REPRESENTATION_INDEPENDENCE_PASS", test_representation_independence(NULL), "4 independent representations collapse to identical ID"); 3684 report_gate("OMEGA_SEMANTIC_DIFFERENCE_PASS", test_semantic_difference(), "ADD -> SUB produces distinct SEMANTIC_ID"); 3685 report_gate("OMEGA_RELATION_PASS", test_relation_canonicalization(), "Relation ordering independence confirmed"); 3686 report_gate("OMEGA_CONSTRAINT_PASS", test_constraint_canonicalization(), "Constraint ordering independence confirmed"); 3687 report_gate("OMEGA_PURE_EFFECT_SEPARATION_PASS", test_pure_effect_separation(), "Pure computation decoupled from physical effect tokens"); 3688 report_gate("OMEGA_MALFORMED_OBJECT_REFUSAL_PASS", test_malformed_refusal(), "Structural cycles and corrupt wire packets rejected"); 3689 report_gate("OMEGA_CROSS_BUILD_DETERMINISM_PASS", test_cross_build_determinism(), "Known-answer test evaluation matches specification"); 3690 report_gate("OMEGA_REALIZATION_ID_COVERS_ALL_CODE_PASS", test_realization_id_covers_all_code(), "Full-capacity code hashed in full; overlong code refused"); 3691 report_gate("OMEGA_OVERSIZED_FIELD_REFUSAL_PASS", test_oversized_field_refusal(), "Oversized keys, values and payloads refused, never truncated"); 3692 printf("================================================================================\n"); 3693 printf(" TOTAL GATES: %d | PASSED: %d | FAILED: %d\n", gate_count, gate_passed, gate_count - gate_passed); 3694 printf("================================================================================\n"); 3695 return (gate_passed == gate_count) ? 0 : 1; 3696 } 3697 3698 int omega_run_m5_gates(void) { 3699 printf("================================================================================\n"); 3700 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 5: OMEGA_AARCH64 QUALIFICATION GATES\n"); 3701 printf("================================================================================\n"); 3702 gate_count = 0; gate_passed = 0; 3703 report_gate("OMEGA_AARCH64_PROFILE_PASS", test_m5_profile(), "AArch64 bare-metal target profile and registers valid"); 3704 report_gate("OMEGA_AARCH64_ENCODER_PASS", test_m5_encoder(), "Direct instruction encoder synthesized expected machine words"); 3705 report_gate("OMEGA_AARCH64_DECODER_SEAM_PASS", test_m5_decoder_seam(), "Independent decoder Seam 1 validated instructions and refused corrupt opcode"); 3706 report_gate("OMEGA_AARCH64_LOWERING_PASS", test_m5_lowering(), "G_S pure operations lowered to AArch64 code buffer"); 3707 report_gate("OMEGA_AARCH64_REALIZATION_ID_PASS", test_m5_realization_id(), "REALIZATION_ID deterministically binds machine code to SEMANTIC_ID"); 3708 report_gate("OMEGA_AARCH64_NATIVE_EXECUTION_PASS", test_m5_native_execution(), "Native in-memory execution observed result 15 matches semantic evaluation"); 3709 report_gate("OMEGA_AARCH64_QEMU_EXECUTION_PASS", test_m5_qemu_execution(), "Bare-metal QEMU execution observed result 15 on PL011 UART"); 3710 report_gate("OMEGA_AARCH64_ADVERSARIAL_MUTATION_PASS", test_m5_adversarial_mutation(), "Single-bit machine code mutation detected and refused fail-closed"); 3711 report_gate("OMEGA_AARCH64_CROSS_BUILD_DETERMINISM_PASS", test_m5_cross_build_determinism(), "Byte-for-byte deterministic realization across runs"); 3712 printf("================================================================================\n"); 3713 printf(" TOTAL GATES: %d | PASSED: %d | FAILED: %d\n", gate_count, gate_passed, gate_count - gate_passed); 3714 printf("================================================================================\n"); 3715 return (gate_passed == gate_count) ? 0 : 1; 3716 } 3717 3718 int omega_run_m6_gates(void) { 3719 printf("================================================================================\n"); 3720 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 6: OMEGA_SELF_HOST QUALIFICATION GATES\n"); 3721 printf("================================================================================\n"); 3722 gate_count = 0; gate_passed = 0; 3723 report_gate("OMEGA_SELF_HOST_GRAPH_PASS", test_m6_graph(), "Semantic compiler graph G_C constructed and validated"); 3724 report_gate("OMEGA_SELF_HOST_C1_EMISSION_PASS", test_m6_c1_emission(), "Reference lowering C0(G_C) -> C1 emitted machine bytes"); 3725 report_gate("OMEGA_SELF_HOST_DECODER_SEAM_PASS", test_m6_decoder_seam(), "Independent decoder verified all C1 instructions"); 3726 report_gate("OMEGA_SELF_HOST_C2_REPRODUCTION_PASS", test_m6_c2_reproduction(), "Native compiler C1(G_C) -> C2 emitted machine bytes"); 3727 report_gate("OMEGA_SELF_HOST_C3_REPRODUCTION_PASS", test_m6_c3_reproduction(), "Native compiler C2(G_C) -> C3 emitted machine bytes"); 3728 report_gate("OMEGA_SELF_HOST_FIXED_POINT_PASS", test_m6_fixed_point(), "Exact bit-for-bit identity C1 == C2 == C3 verified"); 3729 report_gate("OMEGA_SELF_HOST_REALIZATION_ID_PASS", test_m6_realization_id(), "REALIZATION_ID matching across C1, C2, and C3"); 3730 report_gate("OMEGA_SELF_HOST_M5_PARITY_PASS", test_m6_m5_parity(), "Reproduced compiler reproduces exact M5 machine code bytes"); 3731 report_gate("OMEGA_SELF_HOST_NATIVE_EXECUTION_PASS", test_m6_native_execution(), "Reproduced compiler output executes natively to 15"); 3732 report_gate("OMEGA_SELF_HOST_ADVERSARIAL_MUTATION_PASS", test_m6_adversarial_mutation(), "Single-bit mutations in G_C wire or C1 refused fail-closed"); 3733 printf("================================================================================\n"); 3734 printf(" TOTAL GATES: %d | PASSED: %d | FAILED: %d\n", gate_count, gate_passed, gate_count - gate_passed); 3735 printf("================================================================================\n"); 3736 return (gate_passed == gate_count) ? 0 : 1; 3737 } 3738 3739 int omega_run_m7_gates(void) { 3740 printf("================================================================================\n"); 3741 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 7: OMEGA_VERIFY QUALIFICATION GATES\n"); 3742 printf("================================================================================\n"); 3743 gate_count = 0; gate_passed = 0; 3744 report_gate("OMEGA_VERIFY_V0_TYPE_PASS", test_m7_v0_type(), "V0 accepted well-typed graph and rejected ill-typed graph"); 3745 report_gate("OMEGA_VERIFY_V0_DAG_PASS", test_m7_v0_dag(), "V0 rejected graph with dangling reference"); 3746 report_gate("OMEGA_VERIFY_V0_CODE_BOUNDS_PASS", test_m7_v0_code_bounds(), "V0 enforced 4-byte alignment and buffer length bounds"); 3747 report_gate("OMEGA_VERIFY_V0_INSN_DECODE_PASS", test_m7_v0_insn_decode(), "V0 validated instruction opcodes and refused illegal instruction"); 3748 report_gate("OMEGA_VERIFY_V0_TERMINAL_RET_PASS", test_m7_v0_terminal_ret(), "V0 rejected non-terminating / non-RET execution buffer"); 3749 report_gate("OMEGA_VERIFY_V1_DIFFERENTIAL_PASS", test_m7_v1_differential(), "V1 confirmed exact bit-for-bit parity across test corpus"); 3750 report_gate("OMEGA_VERIFY_V1_DIVERGENCE_REFUSAL_PASS", test_m7_v1_divergence_refusal(), "V1 detected and refused mutated realization divergence"); 3751 report_gate("OMEGA_VERIFY_V2_COMMUTATIVITY_PASS", test_m7_v2_commutativity(), "V2 proved commutativity on declared commutative ops"); 3752 report_gate("OMEGA_VERIFY_V2_IDENTITY_PASS", test_m7_v2_identity(), "V2 proved identity elements on ADD, MUL, AND, OR"); 3753 report_gate("OMEGA_VERIFY_V2_OVERFLOW_PASS", test_m7_v2_overflow(), "V2 validated modular overflow wrapping semantics"); 3754 printf("================================================================================\n"); 3755 printf(" TOTAL GATES: %d | PASSED: %d | FAILED: %d\n", gate_count, gate_passed, gate_count - gate_passed); 3756 printf("================================================================================\n"); 3757 return (gate_passed == gate_count) ? 0 : 1; 3758 } 3759 3760 int omega_run_m8_gates(void) { 3761 printf("================================================================================\n"); 3762 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 8: OMEGA_PROGRAM_CORE QUALIFICATION GATES\n"); 3763 printf("================================================================================\n"); 3764 gate_count = 0; gate_passed = 0; 3765 report_gate("OMEGA_PROGRAM_OBJECT_PASS", test_m8_program_object(), "OMEGA_PROGRAM created with contracts, cost, and realization"); 3766 report_gate("OMEGA_PROGRAM_CONTRACT_VALIDATION_PASS", test_m8_contract_validation(), "Contract validation enforced, invalid types rejected"); 3767 report_gate("OMEGA_PROGRAM_COMPOSITION_PASS", test_m8_composition(), "Algebraic composition C = A o B with contract propagation"); 3768 report_gate("OMEGA_PROGRAM_TYPE_MISMATCH_REFUSAL_PASS", test_m8_type_mismatch_refusal(), "Incompatible composition refused fail-closed"); 3769 report_gate("OMEGA_PROGRAM_COST_ACCOUNTING_PASS", test_m8_cost_accounting(), "Composite cost monotonically verified (insns, latency)"); 3770 report_gate("OMEGA_PROGRAM_REALIZATION_PASS", test_m8_realization(), "Composite program lowered to native AArch64 machine bytes"); 3771 report_gate("OMEGA_PROGRAM_V0_STRUCTURAL_PASS", test_m8_v0_structural(), "Composite program passes M7 V0 structural verification"); 3772 report_gate("OMEGA_PROGRAM_V1_DIFFERENTIAL_PASS", test_m8_v1_differential(), "Composite program passes M7 V1 differential evaluation (2x+1)"); 3773 report_gate("OMEGA_PROGRAM_V2_PROPERTY_PASS", test_m8_v2_property(), "Composite program passes M7 V2 property verification"); 3774 report_gate("OMEGA_PROGRAM_SYNTHESIS_TASK_PASS", test_m8_synthesis_task(), "SYNTHESIS_TASK evaluation harness ready for M9 search"); 3775 printf("================================================================================\n"); 3776 printf(" TOTAL GATES: %d | PASSED: %d | FAILED: %d\n", gate_count, gate_passed, gate_count - gate_passed); 3777 printf("================================================================================\n"); 3778 return (gate_passed == gate_count) ? 0 : 1; 3779 } 3780 3781 int omega_run_m9_gates(void) { 3782 printf("================================================================================\n"); 3783 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 9: OMEGA_SYNTHESIS_V0 QUALIFICATION GATES\n"); 3784 printf("================================================================================\n"); 3785 gate_count = 0; gate_passed = 0; 3786 report_gate("OMEGA_SYNTHESIS_PRIMITIVES_PASS", test_m9_primitives(), "Base primitive program bank constructed and verified"); 3787 report_gate("OMEGA_SYNTHESIS_SEARCH_ORDERING_PASS", test_m9_search_ordering(), "Deterministic search generates multi-level candidates"); 3788 report_gate("OMEGA_SYNTHESIS_TYPE_PRUNING_PASS", test_m9_type_pruning(), "Incompatible typed candidates pruned fail-closed"); 3789 report_gate("OMEGA_SYNTHESIS_EQUIV_PRUNING_PASS", test_m9_equiv_pruning(), "Observational equivalence table detects duplicate functions"); 3790 report_gate("OMEGA_SYNTHESIS_V0_STRUCTURAL_PASS", test_m9_v0_structural(), "Synthesized candidate passes M7 V0 structural verification"); 3791 report_gate("OMEGA_SYNTHESIS_V1_IO_FILTERING_PASS", test_m9_v1_io_filtering(), "Non-conforming candidate rejected by test suite filter"); 3792 report_gate("OMEGA_SYNTHESIS_V2_PROPERTY_PASS", test_m9_v2_property(), "Synthesized candidate passes M7 V2 property verification"); 3793 report_gate("OMEGA_SYNTHESIS_TARGET_AFFINE_PASS", test_m9_target_affine(), "Synthesized 2x + 1 passes holdout tests on native hardware"); 3794 report_gate("OMEGA_SYNTHESIS_TARGET_COMPOSED_PASS", test_m9_target_composed(), "Synthesized 3x - 2 passes holdout tests on native hardware"); 3795 report_gate("OMEGA_SYNTHESIS_RECEIPT_PASS", test_m9_receipt(), "Cryptographic identity binding Task, Program, and Realization"); 3796 printf("================================================================================\n"); 3797 printf(" TOTAL GATES: %d | PASSED: %d | FAILED: %d\n", gate_count, gate_passed, gate_count - gate_passed); 3798 printf("================================================================================\n"); 3799 return (gate_passed == gate_count) ? 0 : 1; 3800 } 3801 3802 int omega_run_m10_gates(void) { 3803 printf("================================================================================\n"); 3804 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 10: OMEGA_LIBRARY_V1 QUALIFICATION GATES\n"); 3805 printf("================================================================================\n"); 3806 gate_count = 0; gate_passed = 0; 3807 report_gate("OMEGA_LIBRARY_INIT_PASS", test_m10_init(), "Catalog initialization and capacity management"); 3808 report_gate("OMEGA_LIBRARY_INSERT_PASS", test_m10_insert(), "Content-addressed program insertion and state digest update"); 3809 report_gate("OMEGA_LIBRARY_INDEXING_PASS", test_m10_lookup_id(), "Retrieval by SEMANTIC_ID, REALIZATION_ID, and name"); 3810 report_gate("OMEGA_LIBRARY_QUERY_PASS", test_m10_lookup_type(), "Semantic type query filtering (U64 -> U64)"); 3811 report_gate("OMEGA_LIBRARY_DEP_DAG_PASS", test_m10_dependency_dag(), "Direct and transitive dependency DAG tracking"); 3812 report_gate("OMEGA_LIBRARY_IMMUTABILITY_PASS", test_m10_immutability(), "Version advancement and cryptographic digest evolution"); 3813 report_gate("OMEGA_LIBRARY_UNVERIFIED_REFUSAL_PASS", test_m10_unverified_refusal(), "Unverified program insertion rejected fail-closed"); 3814 report_gate("OMEGA_LIBRARY_DUPLICATE_REFUSAL_PASS", test_m10_duplicate_refusal(), "Duplicate SEMANTIC_ID insertion rejected fail-closed"); 3815 report_gate("OMEGA_LIBRARY_SYNTHESIS_REUSE_PASS", test_m10_synthesis_reuse(), "Synthesis engine discovering composite by reusing verified library component"); 3816 report_gate("OMEGA_LIBRARY_RECEIPT_PASS", test_m10_receipt(), "Cryptographic state digest and provenance accounting"); 3817 printf("================================================================================\n"); 3818 printf(" TOTAL GATES: %d | PASSED: %d | FAILED: %d\n", gate_count, gate_passed, gate_count - gate_passed); 3819 printf("================================================================================\n"); 3820 return (gate_passed == gate_count) ? 0 : 1; 3821 } 3822 3823 int omega_run_m11_gates(void) { 3824 printf("================================================================================\n"); 3825 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 11: OMEGA_LIBRARY_DISCOVERY QUALIFICATION GATES\n"); 3826 printf("================================================================================\n"); 3827 gate_count = 0; gate_passed = 0; 3828 report_gate("OMEGA_DISCOVERY_CORPUS_MINING_PASS", test_m11_corpus_mining(), "Common sub-expression slices mined across corpus"); 3829 report_gate("OMEGA_DISCOVERY_NONTRIVIAL_PASS", test_m11_nontrivial(), "Candidate abstraction is non-trivial and composite"); 3830 report_gate("OMEGA_DISCOVERY_COMPRESSION_PASS", test_m11_compression(), "Positive description length reduction and instruction savings across corpus"); 3831 report_gate("OMEGA_DISCOVERY_SEMANTIC_PRESERVATION_PASS", test_m11_semantic_preservation(), "Behavioral equivalence strictly preserved after program refactoring"); 3832 report_gate("OMEGA_DISCOVERY_V0_STRUCTURAL_PASS", test_m11_v0_structural(), "M7 V0 structural verification passed on discovered abstraction"); 3833 report_gate("OMEGA_DISCOVERY_V1_DIFFERENTIAL_PASS", test_m11_v1_differential(), "M7 V1 differential evaluation passed on native hardware"); 3834 report_gate("OMEGA_DISCOVERY_V2_PROPERTY_PASS", test_m11_v2_property(), "M7 V2 property verification passed on discovered abstraction"); 3835 report_gate("OMEGA_DISCOVERY_LIBRARY_ADMISSION_PASS", test_m11_library_admission(), "Discovered abstraction admitted into OmegaLibrary catalog"); 3836 report_gate("OMEGA_DISCOVERY_SEARCH_ACCELERATION_PASS", test_m11_search_acceleration(), "Held-out synthesis task solved with reduced search candidates"); 3837 report_gate("OMEGA_DISCOVERY_RECEIPT_PASS", test_m11_receipt(), "Full cryptographic evidence receipt accounting for discovery and acceleration"); 3838 printf("================================================================================\n"); 3839 printf(" TOTAL GATES: %d | PASSED: %d | FAILED: %d\n", gate_count, gate_passed, gate_count - gate_passed); 3840 printf("================================================================================\n"); 3841 return (gate_passed == gate_count) ? 0 : 1; 3842 } 3843 3844 int omega_run_m12_gates(void) { 3845 printf("================================================================================\n"); 3846 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 12: OMEGA_LIVING_MATVEC QUALIFICATION GATES\n"); 3847 printf("================================================================================\n"); 3848 gate_count = 0; gate_passed = 0; 3849 report_gate("OMEGA_MATVEC_SEMANTIC_SPEC_PASS", test_m12_semantic_spec(), "Pure mathematical MatVec specification instantiated with canonical SemanticId"); 3850 report_gate("OMEGA_MATVEC_MULTI_REALIZATION_PASS", test_m12_multi_realization(), "Synthesis engine generates 3 distinct machine-code realizations"); 3851 report_gate("OMEGA_MATVEC_TRIPLE_ID_PASS", test_m12_triple_id(), "Cryptographic triple identity binding SEMANTIC_ID, MACHINE_ID, and code bytes"); 3852 report_gate("OMEGA_MATVEC_V0_STRUCTURAL_PASS", test_m12_v0_structural(), "M7 V0 structural verification passed on all realizations with mutation refusal"); 3853 report_gate("OMEGA_MATVEC_V1_NUMERICAL_PARITY_PASS", test_m12_v1_numerical_parity(), "Exact bit-for-bit numerical parity against mathematical Oracle across dimensions"); 3854 report_gate("OMEGA_MATVEC_REGIME_INFLECTION_PASS", test_m12_regime_inflection(), "Empirical benchmark measures execution latency across small and medium regimes"); 3855 report_gate("OMEGA_MATVEC_ADAPTIVE_DISPATCH_PASS", test_m12_adaptive_dispatch(), "Living kernel dynamically adapts and dispatches optimal realization"); 3856 report_gate("OMEGA_MATVEC_SPEEDUP_PASS", test_m12_speedup(), "Multi-accumulator unrolled schedule achieves measured hardware speedup"); 3857 report_gate("OMEGA_MATVEC_ZERO_TOOLCHAIN_PASS", test_m12_zero_toolchain(), "All realizations emitted via direct AArch64 machine byte encoders without foreign toolchain"); 3858 report_gate("OMEGA_MATVEC_RECEIPT_PASS", test_m12_receipt(), "Full qualification receipt generated and verified"); 3859 printf("================================================================================\n"); 3860 printf(" TOTAL GATES: %d | PASSED: %d | FAILED: %d\n", gate_count, gate_passed, gate_count - gate_passed); 3861 printf("================================================================================\n"); 3862 return (gate_passed == gate_count) ? 0 : 1; 3863 } 3864 3865 int omega_run_m13_gates(void) { 3866 printf("================================================================================\n"); 3867 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 13: OMEGA_MACHINE_GRAPH QUALIFICATION GATES\n"); 3868 printf("================================================================================\n"); 3869 gate_count = 0; gate_passed = 0; 3870 report_gate("OMEGA_MACHINE_INIT_PASS", test_m13_init(), "Machine graph initialized with profile and name"); 3871 report_gate("OMEGA_MACHINE_PIPELINE_PASS", test_m13_pipeline(), "Execution pipeline, issue width, and compute units modeled"); 3872 report_gate("OMEGA_MACHINE_REGISTER_FILE_PASS", test_m13_register_file(), "Register file capacities (GPR, Vector) validated"); 3873 report_gate("OMEGA_MACHINE_MEMORY_HIERARCHY_PASS", test_m13_memory_hierarchy(), "Multi-tier cache hierarchy (L1I, L1D, L2, L3) and DRAM modeled"); 3874 report_gate("OMEGA_MACHINE_PHYSICS_INGRESS_PASS", test_m13_physics_ingress(), "Physical machine descriptor ingested and bound to Physics seal"); 3875 report_gate("OMEGA_MACHINE_CANONICAL_ID_PASS", test_m13_canonical_id(), "Deterministic MACHINE_ID bit-for-bit identity verified"); 3876 report_gate("OMEGA_MACHINE_TOPOLOGY_DIFFERENCE_PASS", test_m13_topology_difference(), "Distinct machine topologies yield distinct MACHINE_IDs"); 3877 report_gate("OMEGA_MACHINE_CYCLE_PREVENTION_PASS", test_m13_cycle_prevention(), "Topological cache ordering and line size validation enforced fail-closed"); 3878 report_gate("OMEGA_MACHINE_COST_EVALUATION_PASS", test_m13_cost_evaluation(), "Latency estimation grounded in physical execution unit latencies"); 3879 report_gate("OMEGA_MACHINE_RECEIPT_PASS", test_m13_receipt(), "Physical machine graph certified with Physics authority accounting"); 3880 printf("================================================================================\n"); 3881 printf(" TOTAL GATES: %d | PASSED: %d | FAILED: %d\n", gate_count, gate_passed, gate_count - gate_passed); 3882 printf("================================================================================\n"); 3883 return (gate_passed == gate_count) ? 0 : 1; 3884 } 3885 3886 int omega_run_m14_gates(void) { 3887 printf("================================================================================\n"); 3888 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 14: OMEGA_REALIZATION_SYNTHESIS QUALIFICATION GATES\n"); 3889 printf("================================================================================\n"); 3890 gate_count = 0; gate_passed = 0; 3891 report_gate("OMEGA_REAL_SYNTH_INIT_PASS", test_m14_init(), "Realization synthesis task initialization and configuration"); 3892 report_gate("OMEGA_REAL_SYNTH_TRIPLE_ID_PASS", test_m14_triple_id(), "Cryptographic triple identity incorporates SEMANTIC_ID, MACHINE_ID, and code bytes"); 3893 report_gate("OMEGA_REAL_SYNTH_SCHEDULE_OPT_PASS", test_m14_schedule_opt(), "Machine-aware instruction schedules differ between DGX Spark and QEMU virt"); 3894 report_gate("OMEGA_REAL_SYNTH_DGX_SPARK_PASS", test_m14_dgx_spark(), "Synthesis specialized for DGX Spark Grace Neoverse V2 4-wide dispatch"); 3895 report_gate("OMEGA_REAL_SYNTH_QEMU_VIRT_PASS", test_m14_qemu_virt(), "Synthesis specialized for QEMU virt generic AArch64 baseline"); 3896 report_gate("OMEGA_REAL_SYNTH_SEMANTIC_PARITY_PASS", test_m14_semantic_parity(), "Native execution of synthesized realization matches semantic evaluation"); 3897 report_gate("OMEGA_REAL_SYNTH_V0_STRUCTURAL_PASS", test_m14_v0_structural(), "M7 V0 structural verification passed on synthesized realization"); 3898 report_gate("OMEGA_REAL_SYNTH_V1_DIFFERENTIAL_PASS", test_m14_v1_differential(), "M7 V1 differential evaluation passed across holdout test inputs"); 3899 report_gate("OMEGA_REAL_SYNTH_V2_PROPERTY_PASS", test_m14_v2_property(), "M7 V2 property verification passed on synthesized realization"); 3900 report_gate("OMEGA_REAL_SYNTH_RECEIPT_PASS", test_m14_receipt(), "Full qualification receipt generated and verified"); 3901 printf("================================================================================\n"); 3902 printf(" TOTAL GATES: %d | PASSED: %d | FAILED: %d\n", gate_count, gate_passed, gate_count - gate_passed); 3903 printf("================================================================================\n"); 3904 return (gate_passed == gate_count) ? 0 : 1; 3905 } 3906 3907 int omega_run_m15_gates(void) { 3908 printf("================================================================================\n"); 3909 printf(" AIEN OMEGA SUBSTRATE — MILESTONE 15: PHYSICS_ACCELERATOR_LINK GATES\n"); 3910 printf("================================================================================\n"); 3911 gate_count = 0; gate_passed = 0; 3912 report_gate("PHYSICS_ACCEL_MEM_BOUNDS_PASS", test_m15_mem_bounds(), "Coherent DRAM boundaries and kernel isolation"); 3913 report_gate("PHYSICS_ACCEL_SMMU_TRANSLATION_PASS", test_m15_smmu_translation(), "SMMUv3 Stage 1 IOVA-to-PA translation mapping"); 3914 report_gate("PHYSICS_ACCEL_DMA_SANDBOX_PASS", test_m15_dma_sandbox(), "Unmapped and permission violation DMA sandboxing"); 3915 report_gate("PHYSICS_ACCEL_QUEUE_AUTHORITY_PASS", test_m15_queue_authority(), "Bounded queue authority and doorbell mediation"); 3916 report_gate("PHYSICS_ACCEL_DEVICE_LIFECYCLE_PASS", test_m15_device_lifecycle(), "Deterministic monotonic device lifecycle transitions"); 3917 report_gate("PHYSICS_ACCEL_RESET_RECOVERY_PASS", test_m15_reset_recovery(), "Fault isolation and non-disruptive device reset"); 3918 report_gate("PHYSICS_ACCEL_RECEIPT_CHAIN_PASS", test_m15_receipt_chain(), "Immutable 192B receipt with rolling SHA-256 seal chain"); 3919 report_gate("PHYSICS_ACCEL_OMEGA_INGRESS_PASS", test_m15_omega_ingress(), "Omega mediated intent ingress and capability gating"); 3920 report_gate("PHYSICS_ACCEL_ZERO_TOOLCHAIN_PASS", test_m15_zero_toolchain(), "Zero foreign toolchain (0 LLVM, 0 Python, 0 inline asm)"); 3921 report_gate("PHYSICS_ACCEL_RECEIPT_PASS", test_m15_receipt(), "Qualification receipt generation and audit verification"); 3922 printf("================================================================================\n"); 3923 printf(" TOTAL GATES: %d | PASSED: %d | FAILED: %d\n", gate_count, gate_passed, gate_count - gate_passed); 3924 printf("================================================================================\n"); 3925 return (gate_passed == gate_count) ? 0 : 1; 3926 } 3927 3928 /* R16 (spec/r16-orchestrator-retirement.md, G5): the hand-sequenced living 3929 * matvec demonstration is the class-A central sequencer the reaction world 3930 * replaced. It survives only as a legacy oracle under an explicit mode name, 3931 * --legacy-oracle-living-matvec; the old --demonstrate-living-matvec mode is 3932 * gone. Nothing in the production path runs it. */ 3933 static int legacy_oracle_demonstrate_living_matvec(const char *arg, int dry) { 3934 if (strcmp(arg, "--legacy-oracle-living-matvec") == 0) { 3935 if (!dry) legacy_oracle_run_demonstration_living_matvec(); 3936 return 0; 3937 } 3938 return -1; 3939 } 3940 3941 /* R16-G5: class D milestone demonstrations, reference only (not the production 3942 * path). The dispatcher and the usage text both read this one table, and 3943 * tests/r16_surface/run.sh checks that usage lists every name here and that 3944 * each retired old name (this prefix without "reference-") is rejected. */ 3945 static int ref_codegen(void) { return omega_blackwell_test_codegen_variation(); } 3946 typedef struct { const char *name; void (*run)(void); int (*run_rc)(void); } ReferenceMode; 3947 static const ReferenceMode reference_modes[] = { 3948 {"arithmetic", run_demonstration_arithmetic, NULL}, 3949 {"physics", run_demonstration_physics, NULL}, 3950 {"realization", run_demonstration_realization, NULL}, 3951 {"self-host", run_demonstration_self_host, NULL}, 3952 {"verify", run_demonstration_verify, NULL}, 3953 {"program", run_demonstration_program, NULL}, 3954 {"synthesis", run_demonstration_synthesis, NULL}, 3955 {"library", run_demonstration_library, NULL}, 3956 {"discovery", run_demonstration_discovery, NULL}, 3957 {"machine", run_demonstration_machine, NULL}, 3958 {"realization-synthesis", run_demonstration_realization_synthesis, NULL}, 3959 {"accelerator", run_demonstration_accelerator, NULL}, 3960 {"accelerator-world", run_demonstration_accelerator_world, NULL}, 3961 {"blackwell-matmul", run_demonstration_blackwell_matmul, NULL}, 3962 {"blackwell-codegen", NULL, ref_codegen}, 3963 {"blackwell-vector", run_demonstration_blackwell_vector, NULL}, 3964 }; 3965 #define REFERENCE_PREFIX "--reference-demonstrate-" 3966 3967 /* Returns -1 when arg is not a reference mode, else the mode's exit code (0 when dry: 3968 * the mode is found and not run). */ 3969 static int reference_demonstrate(const char *arg, int dry) { 3970 size_t k = strlen(REFERENCE_PREFIX); 3971 if (strncmp(arg, REFERENCE_PREFIX, k) != 0) return -1; 3972 for (size_t i = 0; i < sizeof reference_modes / sizeof reference_modes[0]; i++) { 3973 if (strcmp(arg + k, reference_modes[i].name) != 0) continue; 3974 if (dry) return 0; 3975 if (reference_modes[i].run_rc) return reference_modes[i].run_rc(); 3976 reference_modes[i].run(); 3977 return 0; 3978 } 3979 return -1; 3980 } 3981 3982 int main(int argc, char **argv) { 3983 if (argc < 2) { 3984 printf("Usage: %s [--run-gates | --run-m5-gates | --run-m6-gates | --run-m7-gates | --run-m8-gates | --run-m9-gates | --run-m10-gates | --run-m11-gates | --run-m12-gates | --run-m13-gates | --run-m14-gates | --dump-test-vectors ]\n", argv[0]); 3985 printf("Reference demonstrations (R16: not the production path):"); 3986 for (size_t i = 0; i < sizeof reference_modes / sizeof reference_modes[0]; i++) 3987 printf(" %s%s", REFERENCE_PREFIX, reference_modes[i].name); 3988 printf("\n"); 3989 printf("Legacy oracle (R16: not the production path, reference only): %s --legacy-oracle-living-matvec\n", argv[0]); 3990 printf("Production entry point: the resident reaction world, see docs/r16-production-entry-point.md\n"); 3991 return 1; 3992 } 3993 3994 /* R16-G5 surface check: resolve a mode through the real dispatch without 3995 * running it (the accelerator and Blackwell modes need the chip). */ 3996 if (strcmp(argv[1], "--reference-dispatch-dry-run") == 0) { 3997 if (argc < 3) return 2; 3998 if (reference_demonstrate(argv[2], 1) == 0 || 3999 legacy_oracle_demonstrate_living_matvec(argv[2], 1) == 0) { 4000 printf("dispatch: %s accepted (not run)\n", argv[2]); 4001 return 0; 4002 } 4003 printf("Unknown argument: %s\n", argv[2]); 4004 return 1; 4005 } 4006 int ref_rc = reference_demonstrate(argv[1], 0); 4007 if (ref_rc >= 0) return ref_rc; 4008 4009 if (strcmp(argv[1], "--run-gates") == 0) { 4010 return omega_run_m4_gates(); 4011 } 4012 4013 if (strcmp(argv[1], "--run-m5-gates") == 0) { 4014 return omega_run_m5_gates(); 4015 } 4016 4017 if (strcmp(argv[1], "--run-m6-gates") == 0) { 4018 return omega_run_m6_gates(); 4019 } 4020 4021 if (strcmp(argv[1], "--run-m7-gates") == 0) { 4022 return omega_run_m7_gates(); 4023 } 4024 4025 if (strcmp(argv[1], "--run-m8-gates") == 0) { 4026 return omega_run_m8_gates(); 4027 } 4028 4029 if (strcmp(argv[1], "--run-m9-gates") == 0) { 4030 return omega_run_m9_gates(); 4031 } 4032 4033 if (strcmp(argv[1], "--run-m10-gates") == 0) { 4034 return omega_run_m10_gates(); 4035 } 4036 4037 if (strcmp(argv[1], "--run-m11-gates") == 0) { 4038 return omega_run_m11_gates(); 4039 } 4040 4041 if (strcmp(argv[1], "--run-m12-gates") == 0) { 4042 return omega_run_m12_gates(); 4043 } 4044 4045 if (legacy_oracle_demonstrate_living_matvec(argv[1], 0) == 0) { 4046 return 0; 4047 } 4048 4049 if (strcmp(argv[1], "--run-m13-gates") == 0) { 4050 return omega_run_m13_gates(); 4051 } 4052 4053 if (strcmp(argv[1], "--run-m14-gates") == 0) { 4054 return omega_run_m14_gates(); 4055 } 4056 4057 if (strcmp(argv[1], "--run-m15-gates") == 0) { 4058 return omega_run_m15_gates(); 4059 } 4060 4061 if (strcmp(argv[1], "--run-world-lifecycle-gates") == 0) { 4062 return run_world_lifecycle_gates(); 4063 } 4064 if (strcmp(argv[1], "--run-m19r-soak") == 0) { 4065 return run_m19r_soak(); 4066 } 4067 if (strcmp(argv[1], "--run-m19-gates") == 0) { 4068 return run_m19_gates(); 4069 } 4070 4071 if (strcmp(argv[1], "--test-m19-drain-decoupling") == 0) { 4072 return run_m19_drain_decoupling(); 4073 } 4074 4075 4076 if (strcmp(argv[1], "--run-m18-gates") == 0) { 4077 return run_m18_gates(); 4078 } 4079 4080 if (strcmp(argv[1], "--run-m17-gates") == 0) { 4081 return run_m17_gates(); 4082 } 4083 4084 if (strcmp(argv[1], "--dump-test-vectors") == 0) { 4085 char default_dir[512]; 4086 const char *dir; 4087 if (argc >= 3) { 4088 dir = argv[2]; 4089 } else { 4090 /* No explicit output dir requested: never default into the 4091 * committed evidence/ tree, use the run-scoped evidence area. */ 4092 if (omega_evidence_path("test_vectors/.keep", default_dir, sizeof(default_dir)) != 0) { 4093 fprintf(stderr, "--dump-test-vectors: failed to resolve default output directory\n"); 4094 return 1; 4095 } 4096 char *slash = strrchr(default_dir, '/'); 4097 if (slash) *slash = '\0'; 4098 dir = default_dir; 4099 } 4100 char path[1024]; 4101 4102 /* Vector 1: Arithmetic U32 ADD(7, 11) canonical binary */ 4103 OmegaGraph *ga = omega_graph_create(); 4104 OmegaObject *t_a = omega_build_type_uint(ga, 32); 4105 OmegaObject *va_a = omega_build_val_uint(ga, &t_a->id, 32, 7); 4106 OmegaObject *vb_a = omega_build_val_uint(ga, &t_a->id, 32, 11); 4107 OmegaObject *op_a = omega_build_op_binary(ga, OP_ADD, OVERFLOW_WRAP, &t_a->id); 4108 omega_build_apply(ga, &op_a->id, &va_a->id, &vb_a->id); 4109 4110 uint8_t bin[4096]; 4111 size_t blen = 0; 4112 omega_graph_serialize_binary(ga, bin, sizeof(bin), &blen); 4113 snprintf(path, sizeof(path), "%s/u32_add_graph.bin", dir); 4114 FILE *f1 = fopen(path, "wb"); 4115 if (f1) { fwrite(bin, 1, blen, f1); fclose(f1); } 4116 4117 /* Vector 2: Text representation */ 4118 char txt[4096]; 4119 omega_graph_format_text(ga, txt, sizeof(txt)); 4120 snprintf(path, sizeof(path), "%s/u32_add_graph.omg", dir); 4121 FILE *f2 = fopen(path, "w"); 4122 if (f2) { fputs(txt, f2); fclose(f2); } 4123 omega_graph_destroy(ga); 4124 4125 /* Vector 3: Physics authority law */ 4126 OmegaGraph *gp = omega_graph_create(); 4127 OmegaObject *law = omega_graph_add_object(gp, KIND_CONSTRAINT); 4128 uint8_t p_bounds[16] = { 0x40, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00 }; 4129 uint8_t p_rights[8] = { 0x00, 0x00, 0x00, 0x07 }; 4130 omega_object_add_constraint(law, CONST_CONTAINMENT, p_bounds, sizeof(p_bounds)); 4131 omega_object_add_constraint(law, CONST_INVARIANT, p_rights, sizeof(p_rights)); 4132 omega_compute_semantic_id(law); 4133 omega_graph_serialize_binary(gp, bin, sizeof(bin), &blen); 4134 snprintf(path, sizeof(path), "%s/physics_m3_law.bin", dir); 4135 FILE *f3 = fopen(path, "wb"); 4136 if (f3) { fwrite(bin, 1, blen, f3); fclose(f3); } 4137 omega_graph_destroy(gp); 4138 4139 printf("Dumped test vectors to %s\n", dir); 4140 return 0; 4141 } 4142 4143 printf("Unknown argument: %s\n", argv[1]); 4144 return 1; 4145 }