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
omega-mv / src / omega_canonical.c
| 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 | /* Canonical object encoding and SemanticId operations backed by pure seeded OM-2 |
| 18 | * (om/visor/canonical_om2.om and om/sha256/sha256-om2.om). */ |
| 19 | #include "omega_canonical.h" |
| 20 | #include "omega_native.h" |
| 21 | #include "sha256.h" |
| 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
| 24 | |
| 25 | int omega_compare_semantic_id(const SemanticId *a, const SemanticId *b) { |
| 26 | uint64_t r = 0; |
| 27 | if (omega2_om_canon_cmp_id(&r, a->bytes, OMEGA_ID_BYTES, b->bytes, OMEGA_ID_BYTES) != OMEGA_OK) abort(); |
| 28 | return (int)(int64_t)r; |
| 29 | } |
| 30 | |
| 31 | void omega_hex_semantic_id(const SemanticId *id, char out_hex[65]) { |
| 32 | uint64_t unused = 0; |
| 33 | if (omega2_om_canon_hex_id(&unused, id->bytes, OMEGA_ID_BYTES, (uint8_t *)out_hex, 65) != OMEGA_OK) abort(); |
| 34 | } |
| 35 | |
| 36 | int omega_parse_hex_semantic_id(const char *hex, SemanticId *out_id) { |
| 37 | if (!hex || !out_id) return -1; |
| 38 | size_t n = strlen(hex); |
| 39 | if (n != 64) return -1; |
| 40 | uint64_t r = 0; |
| 41 | if (omega2_om_canon_parse_hex(&r, (const uint8_t *)hex, n, n, out_id->bytes, OMEGA_ID_BYTES) != OMEGA_OK) abort(); |
| 42 | return (int)(int64_t)r; |
| 43 | } |
| 44 | |
| 45 | uint8_t omega_canonical_object_version(uint8_t kind) { |
| 46 | uint64_t r = 0; |
| 47 | if (omega2_om_canon_obj_ver(&r, (uint64_t)kind) != OMEGA_OK) abort(); |
| 48 | return (uint8_t)r; |
| 49 | } |
| 50 | |
| 51 | int omega_canonical_check_header(uint8_t version, uint8_t kind) { |
| 52 | uint64_t r = 0; |
| 53 | if (omega2_om_canon_chk_hdr(&r, (uint64_t)version, (uint64_t)kind) != OMEGA_OK) abort(); |
| 54 | return (int)(int64_t)r; |
| 55 | } |
| 56 | |
| 57 | static int put_u16_be(uint8_t *buf, size_t *pos, size_t max_len, uint16_t v) { |
| 58 | uint64_t r = 0; |
| 59 | if (omega2_om_canon_write_u16be(&r, (uint64_t)v, buf, max_len, *pos, max_len) != OMEGA_OK) abort(); |
| 60 | int64_t np = (int64_t)r; |
| 61 | if (np < 0) return -1; |
| 62 | *pos = (size_t)np; |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static int put_u32_be(uint8_t *buf, size_t *pos, size_t max_len, uint32_t v) { |
| 67 | uint64_t r = 0; |
| 68 | if (omega2_om_canon_write_u32be(&r, (uint64_t)v, buf, max_len, *pos, max_len) != OMEGA_OK) abort(); |
| 69 | int64_t np = (int64_t)r; |
| 70 | if (np < 0) return -1; |
| 71 | *pos = (size_t)np; |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | int omega_canonical_encode(const OmegaObject *obj, uint8_t *out_buf, size_t max_len, size_t *out_len) { |
| 76 | if (!obj || !out_buf || !out_len || max_len == 0) return -1; |
| 77 | |
| 78 | uint64_t r = 0; |
| 79 | if (omega2_om_canon_write_hdr(&r, (uint64_t)obj->kind, out_buf, max_len, max_len) != OMEGA_OK) abort(); |
| 80 | if ((int64_t)r < 0) return -1; |
| 81 | size_t pos = (size_t)r; |
| 82 | |
| 83 | /* 2. Attributes (canonical sort in OM-2) */ |
| 84 | uint16_t n_attrs = obj->attr_count; |
| 85 | if (n_attrs > OMEGA_MAX_ATTRIBUTES) return -1; |
| 86 | uint16_t attr_indices[OMEGA_MAX_ATTRIBUTES] = {0}; |
| 87 | if (n_attrs > 0) { |
| 88 | uint8_t keys[OMEGA_MAX_ATTRIBUTES * OMEGA_MAX_KEY_LEN]; |
| 89 | memset(keys, 0, sizeof(keys)); |
| 90 | for (uint16_t i = 0; i < n_attrs; ++i) { |
| 91 | memcpy(&keys[i * OMEGA_MAX_KEY_LEN], obj->attributes[i].key, OMEGA_MAX_KEY_LEN); |
| 92 | } |
| 93 | if (omega2_om_canon_sort_attr(&r, keys, sizeof(keys), n_attrs, attr_indices, OMEGA_MAX_ATTRIBUTES) != OMEGA_OK) abort(); |
| 94 | if ((int64_t)r != 0) return -1; |
| 95 | } |
| 96 | if (put_u16_be(out_buf, &pos, max_len, n_attrs) != 0) return -1; |
| 97 | for (uint16_t i = 0; i < n_attrs; ++i) { |
| 98 | const OmegaAttribute *attr = &obj->attributes[attr_indices[i]]; |
| 99 | size_t klen = strlen(attr->key); |
| 100 | if (klen > 255) return -1; |
| 101 | if (pos + 1 + klen + 2 + attr->val_len > max_len) return -1; |
| 102 | out_buf[pos++] = (uint8_t)klen; |
| 103 | memcpy(&out_buf[pos], attr->key, klen); |
| 104 | pos += klen; |
| 105 | if (put_u16_be(out_buf, &pos, max_len, attr->val_len) != 0) return -1; |
| 106 | if (attr->val_len > 0) { |
| 107 | memcpy(&out_buf[pos], attr->value, attr->val_len); |
| 108 | pos += attr->val_len; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /* 3. Relations (canonical sort in OM-2) */ |
| 113 | uint16_t n_rels = obj->rel_count; |
| 114 | if (n_rels > OMEGA_MAX_RELATIONS) return -1; |
| 115 | uint16_t rel_indices[OMEGA_MAX_RELATIONS] = {0}; |
| 116 | if (n_rels > 0) { |
| 117 | uint16_t rkinds[OMEGA_MAX_RELATIONS] = {0}; |
| 118 | uint8_t rtgts[OMEGA_MAX_RELATIONS * OMEGA_ID_BYTES]; |
| 119 | memset(rtgts, 0, sizeof(rtgts)); |
| 120 | for (uint16_t i = 0; i < n_rels; ++i) { |
| 121 | rkinds[i] = obj->relations[i].kind; |
| 122 | memcpy(&rtgts[i * OMEGA_ID_BYTES], obj->relations[i].target_id.bytes, OMEGA_ID_BYTES); |
| 123 | } |
| 124 | if (omega2_om_canon_sort_rel(&r, rkinds, OMEGA_MAX_RELATIONS, rtgts, sizeof(rtgts), n_rels, rel_indices, OMEGA_MAX_RELATIONS) != OMEGA_OK) abort(); |
| 125 | if ((int64_t)r != 0) return -1; |
| 126 | } |
| 127 | if (put_u16_be(out_buf, &pos, max_len, n_rels) != 0) return -1; |
| 128 | for (uint16_t i = 0; i < n_rels; ++i) { |
| 129 | const OmegaRelation *rel = &obj->relations[rel_indices[i]]; |
| 130 | if (pos + 2 + OMEGA_ID_BYTES > max_len) return -1; |
| 131 | if (put_u16_be(out_buf, &pos, max_len, rel->kind) != 0) return -1; |
| 132 | memcpy(&out_buf[pos], rel->target_id.bytes, OMEGA_ID_BYTES); |
| 133 | pos += OMEGA_ID_BYTES; |
| 134 | } |
| 135 | |
| 136 | /* 4. Constraints (canonical sort in OM-2) */ |
| 137 | uint16_t n_consts = obj->const_count; |
| 138 | if (n_consts > OMEGA_MAX_CONSTRAINTS) return -1; |
| 139 | uint16_t const_indices[OMEGA_MAX_CONSTRAINTS] = {0}; |
| 140 | if (n_consts > 0) { |
| 141 | uint16_t cmeta[OMEGA_MAX_CONSTRAINTS * 2] = {0}; |
| 142 | uint8_t cpays[OMEGA_MAX_CONSTRAINTS * 128]; |
| 143 | memset(cpays, 0, sizeof(cpays)); |
| 144 | for (uint16_t i = 0; i < n_consts; ++i) { |
| 145 | cmeta[i * 2] = obj->constraints[i].kind; |
| 146 | cmeta[i * 2 + 1] = obj->constraints[i].payload_len; |
| 147 | uint16_t cl = obj->constraints[i].payload_len <= 128 ? obj->constraints[i].payload_len : 128; |
| 148 | if (cl > 0) memcpy(&cpays[i * 128], obj->constraints[i].payload, cl); |
| 149 | } |
| 150 | if (omega2_om_canon_sort_const(&r, cmeta, OMEGA_MAX_CONSTRAINTS * 2, cpays, sizeof(cpays), n_consts, const_indices, OMEGA_MAX_CONSTRAINTS) != OMEGA_OK) abort(); |
| 151 | if ((int64_t)r != 0) return -1; |
| 152 | } |
| 153 | if (put_u16_be(out_buf, &pos, max_len, n_consts) != 0) return -1; |
| 154 | for (uint16_t i = 0; i < n_consts; ++i) { |
| 155 | const OmegaConstraint *c = &obj->constraints[const_indices[i]]; |
| 156 | if (pos + 4 + c->payload_len > max_len) return -1; |
| 157 | if (put_u16_be(out_buf, &pos, max_len, c->kind) != 0) return -1; |
| 158 | if (put_u16_be(out_buf, &pos, max_len, c->payload_len) != 0) return -1; |
| 159 | if (c->payload_len > 0) { |
| 160 | memcpy(&out_buf[pos], c->payload, c->payload_len); |
| 161 | pos += c->payload_len; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /* 5. Payload */ |
| 166 | if (pos + 4 + obj->payload_len > max_len) return -1; |
| 167 | if (put_u32_be(out_buf, &pos, max_len, obj->payload_len) != 0) return -1; |
| 168 | if (obj->payload_len > 0) { |
| 169 | memcpy(&out_buf[pos], obj->payload, obj->payload_len); |
| 170 | pos += obj->payload_len; |
| 171 | } |
| 172 | |
| 173 | *out_len = pos; |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | int omega_compute_semantic_id(OmegaObject *obj) { |
| 178 | if (!obj) return -1; |
| 179 | uint8_t canon_buf[4096]; |
| 180 | size_t canon_len = 0; |
| 181 | if (omega_canonical_encode(obj, canon_buf, sizeof(canon_buf), &canon_len) != 0) { |
| 182 | return -1; |
| 183 | } |
| 184 | sha256_hash(canon_buf, canon_len, obj->id.bytes); |
| 185 | obj->has_id = true; |
| 186 | return 0; |
| 187 | } |