interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / omega_verify.c

221 lines · 8196 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/* Structural, differential, and algebraic property verification backed by pure seeded OM-2
18 * (om/visor/verify_om2.om, om/visor/validate_om2.om, om/aarch64/aarch64_om2.om). */
19#include "omega_verify.h"
20#include "omega_validate.h"
21#include "omega_canonical.h"
22#include "omega_core.h"
23#include "aarch64_decoder.h"
24#include "omega_exec.h"
25#include "omega_native.h"
26#include <string.h>
27#include <stdio.h>
28#include <stdlib.h>
29
30int omega_verify_v0_structural(const OmegaGraph *graph, const RealizationObject *real, VerifyReport *report) {
31 if (!report) return -1;
32 memset(report, 0, sizeof(VerifyReport));
33 report->tier = VERIFY_TIER_V0;
34 report->passed = true;
35
36 if (graph) {
37 char err[256];
38 report->check_count++;
39 if (omega_validate_graph(graph, err, sizeof(err)) != 0) {
40 report->passed = false;
41 report->fail_count++;
42 snprintf(report->error_detail, sizeof(report->error_detail), "V0 Graph Validation Failed: %.200s", err);
43 return -1;
44 }
45 report->check_count++;
46 if (graph->object_count == 0) {
47 report->passed = false;
48 report->fail_count++;
49 snprintf(report->error_detail, sizeof(report->error_detail), "V0 Graph Validation Failed: Empty graph");
50 return -1;
51 }
52 }
53
54 if (real) {
55 uint64_t r = 0;
56 if (omega2_om_ver_v0_real(&r, (uint64_t)real->target_profile, (uint64_t)real->code_len,
57 AARCH64_MAX_CODE_BYTES, real->has_id ? 1ULL : 0ULL) != OMEGA_OK) abort();
58 report->check_count++;
59 if ((int64_t)r == 1) {
60 report->passed = false;
61 report->fail_count++;
62 snprintf(report->error_detail, sizeof(report->error_detail),
63 "V0 Realization Profile Failed: Unknown profile 0x%02x", real->target_profile);
64 return -1;
65 }
66 report->check_count++;
67 if ((int64_t)r == 2) {
68 report->passed = false;
69 report->fail_count++;
70 snprintf(report->error_detail, sizeof(report->error_detail),
71 "V0 Realization Code Bounds Failed: Invalid length %zu bytes", real->code_len);
72 return -1;
73 }
74
75 report->check_count++;
76 char dec_err[256];
77 if (aarch64_validate_code_buffer(real->code_bytes, real->code_len, dec_err, sizeof(dec_err)) != 0) {
78 report->passed = false;
79 report->fail_count++;
80 snprintf(report->error_detail, sizeof(report->error_detail),
81 "V0 Instruction Stream Failed: %.200s", dec_err);
82 return -1;
83 }
84
85 report->check_count++;
86 if ((int64_t)r == 3) {
87 report->passed = false;
88 report->fail_count++;
89 snprintf(report->error_detail, sizeof(report->error_detail),
90 "V0 Realization ID Failed: Object missing REALIZATION_ID");
91 return -1;
92 }
93
94 RealizationObject check_id = *real;
95 bool id_matches = false;
96 if (real->has_machine_id) {
97 SemanticId triple_id;
98 if (omega_realize_compute_triple_id(&real->semantic_id, &real->machine_id, real, &triple_id) == 0 &&
99 omega_compare_semantic_id(&triple_id, &real->realization_id) == 0) {
100 id_matches = true;
101 }
102 } else {
103 if (omega_compute_realization_id(&check_id) == 0 &&
104 omega_compare_semantic_id(&check_id.realization_id, &real->realization_id) == 0) {
105 id_matches = true;
106 }
107 }
108
109 if (!id_matches) {
110 report->passed = false;
111 report->fail_count++;
112 snprintf(report->error_detail, sizeof(report->error_detail),
113 "V0 Realization ID Failed: REALIZATION_ID cryptographic mismatch");
114 return -1;
115 }
116 }
117
118 return 0;
119}
120
121int omega_verify_v1_differential(const OmegaGraph *graph, const RealizationObject *real,
122 const uint64_t *test_inputs, size_t input_triplet_count,
123 VerifyReport *report) {
124 if (!report || !real) return -1;
125 (void)graph;
126 memset(report, 0, sizeof(VerifyReport));
127 report->tier = VERIFY_TIER_V1;
128 report->passed = true;
129
130 if (omega_verify_v0_structural(graph, real, report) != 0) {
131 return -1;
132 }
133
134 report->tier = VERIFY_TIER_V1;
135 size_t count = test_inputs ? input_triplet_count : 7;
136
137 for (size_t i = 0; i < count; ++i) {
138 uint64_t a = 0, b = 0, c = 0;
139 if (test_inputs) {
140 a = test_inputs[i * 3];
141 b = test_inputs[i * 3 + 1];
142 c = test_inputs[i * 3 + 2];
143 } else {
144 uint64_t vec[3] = {0, 0, 0};
145 uint64_t vr = 0;
146 if (omega2_om_ver_v1_defvec(&vr, (uint64_t)i, vec, 3) != OMEGA_OK || (int64_t)vr != 0) abort();
147 a = vec[0]; b = vec[1]; c = vec[2];
148 }
149
150 uint64_t expected = 0;
151 if (omega2_om_ver_v1_ref(&expected, a, b, c) != OMEGA_OK) abort();
152
153 uint64_t observed = 0;
154 report->check_count++;
155 int rc = omega_exec_native_f3(real, a, b, c, &observed);
156 if (rc != 0) {
157 report->passed = false;
158 report->fail_count++;
159 snprintf(report->error_detail, sizeof(report->error_detail),
160 "V1 Differential Execution Failed: Vector %zu (a=%lu, b=%lu, c=%lu) returned rc=%d",
161 i, (unsigned long)a, (unsigned long)b, (unsigned long)c, rc);
162 return -1;
163 }
164
165 if (observed != expected) {
166 report->passed = false;
167 report->fail_count++;
168 snprintf(report->error_detail, sizeof(report->error_detail),
169 "V1 Differential Mismatch: Vector %zu (a=%lu, b=%lu, c=%lu) expected=%lu observed=%lu",
170 i, (unsigned long)a, (unsigned long)b, (unsigned long)c,
171 (unsigned long)expected, (unsigned long)observed);
172 return -1;
173 }
174 }
175
176 return 0;
177}
178
179int omega_verify_v2_properties(const OmegaGraph *graph, const RealizationObject *real, VerifyReport *report) {
180 if (!report) return -1;
181 (void)graph;
182 (void)real;
183 memset(report, 0, sizeof(VerifyReport));
184 report->tier = VERIFY_TIER_V2;
185 report->passed = true;
186
187 uint64_t info[4] = {0, 0, 0, 0};
188 uint64_t r = 0;
189 if (omega2_om_ver_v2_props(&r, info, 4) != OMEGA_OK) abort();
190 if ((int64_t)r < 0) {
191 report->passed = false;
192 report->fail_count++;
193 snprintf(report->error_detail, sizeof(report->error_detail),
194 "V2 Property Violation kind=%lu op=0x%02lx (%lu, %lu)",
195 (unsigned long)info[0], (unsigned long)info[1],
196 (unsigned long)info[2], (unsigned long)info[3]);
197 return -1;
198 }
199 report->check_count = (uint32_t)r;
200 return 0;
201}
202
203int omega_verify_pipeline(const OmegaGraph *graph, const RealizationObject *real,
204 VerifyTier max_tier, VerifyReport *report) {
205 if (!report) return -1;
206
207 if (omega_verify_v0_structural(graph, real, report) != 0) {
208 return -1;
209 }
210 if (max_tier >= VERIFY_TIER_V1) {
211 if (omega_verify_v1_differential(graph, real, NULL, 0, report) != 0) {
212 return -1;
213 }
214 }
215 if (max_tier >= VERIFY_TIER_V2) {
216 if (omega_verify_v2_properties(graph, real, report) != 0) {
217 return -1;
218 }
219 }
220 return 0;
221}