interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / src / omega_canonical.c

277 lines · 9967 bytes Raw Blame History
1#ifndef CUDAPP_MIGRATION_PENDING_C_CORE
2/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
3#include "omega_canonical.h"
4#include "sha256.h"
5#include "omega_core_binding.h"
6#include <stdlib.h>
7#include <string.h>
8_Static_assert(sizeof(OmegaObject) == 25972, "canonical bootstrap object ABI");
9_Static_assert(offsetof(OmegaObject, payload) == 24948, "canonical payload ABI");
10static void canon_status(uint32_t s) { if (s != OMEGA_OK) CORE_STOP(); }
11int omega_compare_semantic_id(const SemanticId *a, const SemanticId *b) {
12 uint64_t r;
13 canon_status(CORE_CALL(omcidcmp, &r, a->bytes, 32, b->bytes, 32));
14 return (int)(int64_t)r;
15}
16void omega_hex_semantic_id(const SemanticId *a, char out[65]) {
17 canon_status(CORE_VOID(omcidhex, a->bytes, 32, out, 65));
18}
19int omega_parse_hex_semantic_id(const char *s, SemanticId *a) {
20 uint64_t r;
21 if (!s || strlen(s) != 64) return -1;
22 canon_status(CORE_CALL(omparse, &r, s, 65, a->bytes, 32));
23 return -(int)r;
24}
25uint8_t omega_canonical_object_version(uint8_t k) {
26 uint64_t r; canon_status(CORE_CALL(omcver, &r, k)); return (uint8_t)r;
27}
28int omega_canonical_check_header(uint8_t v, uint8_t k) {
29 uint64_t r; canon_status(CORE_CALL(omcheck, &r, k, v)); return -(int)r;
30}
31int omega_canonical_encode(const OmegaObject *o, uint8_t *out, size_t cap, size_t *len) {
32 if (!o || !out || !len) return -1;
33 uint8_t staging[32768];
34 size_t limit = cap > sizeof staging ? sizeof staging : cap;
35 /* Preserve partial-output semantics on refusal without lending a short view. */
36 memcpy(staging, out, limit);
37 uint64_t r;
38 canon_status(CORE_CALL(omcanon, &r, o, sizeof *o, staging, sizeof staging, limit));
39 if (r == 65536) { memcpy(out, staging, limit); return -1; }
40 memcpy(out, staging, (size_t)r);
41 *len = (size_t)r;
42 return 0;
43}
44int omega_compute_semantic_id(OmegaObject *o) {
45 uint8_t canon[4096]; size_t n;
46 if (omega_canonical_encode(o, canon, sizeof canon, &n)) return -1;
47 sha256_hash(canon, n, o->id.bytes);
48 o->has_id = true;
49 return 0;
50}
51#else
52/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
53 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
54 *
55 * Licensed under the Apache License, Version 2.0 (the "License");
56 * you may not use this file except in compliance with the License.
57 * You may obtain a copy of the License at
58 *
59 * http://www.apache.org/licenses/LICENSE-2.0
60 *
61 * Unless required by applicable law or agreed to in writing, software
62 * distributed under the License is distributed on an "AS IS" BASIS,
63 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
64 * See the License for the specific language governing permissions and
65 * limitations under the License.
66 */
67
68#include "omega_canonical.h"
69#include "sha256.h"
70#include <string.h>
71#include <stdio.h>
72#include <stdlib.h>
73
74static void write_u16_be(uint8_t *p, uint16_t v) {
75 p[0] = (uint8_t)((v >> 8) & 0xff);
76 p[1] = (uint8_t)(v & 0xff);
77}
78
79static void write_u32_be(uint8_t *p, uint32_t v) {
80 p[0] = (uint8_t)((v >> 24) & 0xff);
81 p[1] = (uint8_t)((v >> 16) & 0xff);
82 p[2] = (uint8_t)((v >> 8) & 0xff);
83 p[3] = (uint8_t)(v & 0xff);
84}
85
86int omega_compare_semantic_id(const SemanticId *a, const SemanticId *b) {
87 return memcmp(a->bytes, b->bytes, OMEGA_ID_BYTES);
88}
89
90void omega_hex_semantic_id(const SemanticId *id, char out_hex[65]) {
91 static const char hex[] = "0123456789abcdef";
92 for (int i = 0; i < OMEGA_ID_BYTES; ++i) {
93 out_hex[i * 2] = hex[(id->bytes[i] >> 4) & 0x0f];
94 out_hex[i * 2 + 1] = hex[id->bytes[i] & 0x0f];
95 }
96 out_hex[64] = '\0';
97}
98
99int omega_parse_hex_semantic_id(const char *hex, SemanticId *out_id) {
100 if (!hex || strlen(hex) != 64) return -1;
101 for (int i = 0; i < 32; ++i) {
102 char buf[3] = { hex[i * 2], hex[i * 2 + 1], 0 };
103 char *end = NULL;
104 long val = strtol(buf, &end, 16);
105 if (end != buf + 2) return -1;
106 out_id->bytes[i] = (uint8_t)val;
107 }
108 return 0;
109}
110
111uint8_t omega_canonical_object_version(uint8_t kind) {
112 return kind == KIND_EFFECT ? OMEGA_EFFECT_VERSION : OMEGA_VERSION;
113}
114
115int omega_canonical_check_header(uint8_t version, uint8_t kind) {
116 if (kind == KIND_EFFECT && version == OMEGA_VERSION) return OMEGA_CANON_ERR_EFFECT_V1;
117 if (version != omega_canonical_object_version(kind)) return OMEGA_CANON_ERR_VERSION;
118 return OMEGA_CANON_OK;
119}
120
121int omega_canonical_encode(const OmegaObject *obj, uint8_t *out_buf, size_t max_len, size_t *out_len) {
122 if (!obj || !out_buf || !out_len) return -1;
123
124 size_t pos = 0;
125
126 /* 1. Header: magic "OMG0" (0x4F, 0x4D, 0x47, 0x30), object encoding
127 * version of the kind (0x02 effect, 0x01 otherwise), kind */
128 if (pos + 6 > max_len) return -1;
129 out_buf[pos++] = 0x4F;
130 out_buf[pos++] = 0x4D;
131 out_buf[pos++] = 0x47;
132 out_buf[pos++] = 0x30;
133 out_buf[pos++] = omega_canonical_object_version((uint8_t)obj->kind);
134 out_buf[pos++] = (uint8_t)obj->kind;
135
136 /* 2. Attributes (sorted by key ascending) */
137 uint16_t attr_indices[OMEGA_MAX_ATTRIBUTES];
138 uint16_t n_attrs = obj->attr_count;
139 if (n_attrs > OMEGA_MAX_ATTRIBUTES) return -1;
140 for (uint16_t i = 0; i < n_attrs; ++i) attr_indices[i] = i;
141
142 /* Insertion sort for stable canonical attribute order */
143 for (uint16_t i = 1; i < n_attrs; ++i) {
144 uint16_t key_idx = attr_indices[i];
145 int j = i - 1;
146 while (j >= 0 && strcmp(obj->attributes[attr_indices[j]].key, obj->attributes[key_idx].key) > 0) {
147 attr_indices[j + 1] = attr_indices[j];
148 j--;
149 }
150 attr_indices[j + 1] = key_idx;
151 }
152
153 if (pos + 2 > max_len) return -1;
154 write_u16_be(&out_buf[pos], n_attrs);
155 pos += 2;
156
157 for (uint16_t i = 0; i < n_attrs; ++i) {
158 const OmegaAttribute *attr = &obj->attributes[attr_indices[i]];
159 size_t klen = strlen(attr->key);
160 if (klen > 255) return -1;
161 if (pos + 1 + klen + 2 + attr->val_len > max_len) return -1;
162 out_buf[pos++] = (uint8_t)klen;
163 memcpy(&out_buf[pos], attr->key, klen);
164 pos += klen;
165 write_u16_be(&out_buf[pos], attr->val_len);
166 pos += 2;
167 if (attr->val_len > 0) {
168 memcpy(&out_buf[pos], attr->value, attr->val_len);
169 pos += attr->val_len;
170 }
171 }
172
173 /* 3. Relations (sorted primarily by kind, secondarily by target_id) */
174 uint16_t rel_indices[OMEGA_MAX_RELATIONS];
175 uint16_t n_rels = obj->rel_count;
176 if (n_rels > OMEGA_MAX_RELATIONS) return -1;
177 for (uint16_t i = 0; i < n_rels; ++i) rel_indices[i] = i;
178
179 for (uint16_t i = 1; i < n_rels; ++i) {
180 uint16_t cur = rel_indices[i];
181 int j = i - 1;
182 while (j >= 0) {
183 const OmegaRelation *ra = &obj->relations[rel_indices[j]];
184 const OmegaRelation *rb = &obj->relations[cur];
185 int cmp = (int)ra->kind - (int)rb->kind;
186 if (cmp == 0) {
187 cmp = memcmp(ra->target_id.bytes, rb->target_id.bytes, OMEGA_ID_BYTES);
188 }
189 if (cmp <= 0) break;
190 rel_indices[j + 1] = rel_indices[j];
191 j--;
192 }
193 rel_indices[j + 1] = cur;
194 }
195
196 if (pos + 2 > max_len) return -1;
197 write_u16_be(&out_buf[pos], n_rels);
198 pos += 2;
199
200 for (uint16_t i = 0; i < n_rels; ++i) {
201 const OmegaRelation *rel = &obj->relations[rel_indices[i]];
202 if (pos + 2 + OMEGA_ID_BYTES > max_len) return -1;
203 write_u16_be(&out_buf[pos], rel->kind);
204 pos += 2;
205 memcpy(&out_buf[pos], rel->target_id.bytes, OMEGA_ID_BYTES);
206 pos += OMEGA_ID_BYTES;
207 }
208
209 /* 4. Constraints (sorted primarily by kind, secondarily by payload) */
210 uint16_t const_indices[OMEGA_MAX_CONSTRAINTS];
211 uint16_t n_consts = obj->const_count;
212 if (n_consts > OMEGA_MAX_CONSTRAINTS) return -1;
213 for (uint16_t i = 0; i < n_consts; ++i) const_indices[i] = i;
214
215 for (uint16_t i = 1; i < n_consts; ++i) {
216 uint16_t cur = const_indices[i];
217 int j = i - 1;
218 while (j >= 0) {
219 const OmegaConstraint *ca = &obj->constraints[const_indices[j]];
220 const OmegaConstraint *cb = &obj->constraints[cur];
221 int cmp = (int)ca->kind - (int)cb->kind;
222 if (cmp == 0) {
223 uint16_t min_len = ca->payload_len < cb->payload_len ? ca->payload_len : cb->payload_len;
224 cmp = memcmp(ca->payload, cb->payload, min_len);
225 if (cmp == 0) {
226 cmp = (int)ca->payload_len - (int)cb->payload_len;
227 }
228 }
229 if (cmp <= 0) break;
230 const_indices[j + 1] = const_indices[j];
231 j--;
232 }
233 const_indices[j + 1] = cur;
234 }
235
236 if (pos + 2 > max_len) return -1;
237 write_u16_be(&out_buf[pos], n_consts);
238 pos += 2;
239
240 for (uint16_t i = 0; i < n_consts; ++i) {
241 const OmegaConstraint *c = &obj->constraints[const_indices[i]];
242 if (pos + 2 + 2 + c->payload_len > max_len) return -1;
243 write_u16_be(&out_buf[pos], c->kind);
244 pos += 2;
245 write_u16_be(&out_buf[pos], c->payload_len);
246 pos += 2;
247 if (c->payload_len > 0) {
248 memcpy(&out_buf[pos], c->payload, c->payload_len);
249 pos += c->payload_len;
250 }
251 }
252
253 /* 5. Payload */
254 if (pos + 4 + obj->payload_len > max_len) return -1;
255 write_u32_be(&out_buf[pos], obj->payload_len);
256 pos += 4;
257 if (obj->payload_len > 0) {
258 memcpy(&out_buf[pos], obj->payload, obj->payload_len);
259 pos += obj->payload_len;
260 }
261
262 *out_len = pos;
263 return 0;
264}
265
266int omega_compute_semantic_id(OmegaObject *obj) {
267 if (!obj) return -1;
268 uint8_t canon_buf[4096];
269 size_t canon_len = 0;
270 if (omega_canonical_encode(obj, canon_buf, sizeof(canon_buf), &canon_len) != 0) {
271 return -1;
272 }
273 sha256_hash(canon_buf, canon_len, obj->id.bytes);
274 obj->has_id = true;
275 return 0;
276}
277#endif