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_vector.c

113 lines · 4241 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_vector.h"
18#include "omega_core.h"
19#include "omega_canonical.h"
20#include "sha256.h"
21#include <string.h>
22
23int omega_vector_spec_init(OmegaVectorSpec *spec, const char *name, uint32_t n) {
24 if (!spec || !name || n == 0) return -1;
25 memset(spec, 0, sizeof(*spec));
26 strncpy(spec->name, name, sizeof(spec->name) - 1);
27 spec->element_type = TYPE_UNSIGNED_INT;
28 spec->element_width = 32;
29 spec->element_count = n;
30 spec->overflow = OVERFLOW_WRAP;
31
32 /* Canonical spec digest: SHA-256("OMEGA_VECADD_U32" || N || overflow) */
33 sha256_ctx ctx;
34 sha256_init(&ctx);
35 sha256_update(&ctx, (const uint8_t *)"OMEGA_VECADD_U32", 16);
36 sha256_update(&ctx, (const uint8_t *)&spec->element_width, sizeof(spec->element_width));
37 sha256_update(&ctx, (const uint8_t *)&spec->element_count, sizeof(spec->element_count));
38 uint8_t ov = (uint8_t)spec->overflow;
39 sha256_update(&ctx, &ov, sizeof(ov));
40 sha256_final(&ctx, spec->spec_digest);
41
42 memcpy(spec->spec_id.bytes, spec->spec_digest, OMEGA_ID_BYTES);
43 return 0;
44}
45
46void omega_vector_oracle_u32(const uint32_t *a, const uint32_t *b, uint32_t *c, uint32_t n) {
47 if (!a || !b || !c || n == 0) return;
48 for (uint32_t i = 0; i < n; i++) {
49 c[i] = (uint32_t)(a[i] + b[i]);
50 }
51}
52
53int omega_vector_verify_oracle(const uint32_t *a, const uint32_t *b, const uint32_t *actual_c,
54 uint32_t n, size_t *first_mismatch_idx) {
55 if (!a || !b || !actual_c || n == 0) return -1;
56 for (size_t i = 0; i < n; i++) {
57 uint32_t expected = (uint32_t)(a[i] + b[i]);
58 if (actual_c[i] != expected) {
59 if (first_mismatch_idx) *first_mismatch_idx = i;
60 return -1;
61 }
62 }
63 return 0;
64}
65
66void omega_vector_generate_deterministic(uint32_t *a, uint32_t *b, uint32_t *c_poison, uint32_t n) {
67 if (!a || !b || !c_poison || n == 0) return;
68 for (uint32_t i = 0; i < n; i++) {
69 /* Deterministic vectors exercising small values, offsets, and high-bit modulo wrapping */
70 if (i == 0) {
71 a[i] = 0xFFFFFFFEU;
72 b[i] = 3U; /* modulo 2^32 wrap check: 0xFFFFFFFE + 3 = 1 */
73 } else if (i == 1) {
74 a[i] = 0x80000000U;
75 b[i] = 0x80000000U; /* modulo 2^32 wrap check: 2^31 + 2^31 = 0 */
76 } else {
77 a[i] = 1000U + i;
78 b[i] = 2000U + i * 2;
79 }
80 c_poison[i] = OMEGA_VECTOR_POISON_VALUE;
81 }
82}
83
84int omega_vector_build_spec_graph(OmegaGraph *g, uint32_t n, SemanticId *out_spec_id) {
85 if (!g || n == 0 || !out_spec_id) return -1;
86
87 OmegaObject *t_u32 = omega_build_type_uint(g, 32);
88 if (!t_u32) return -1;
89
90 OmegaObject *t_seq = omega_build_type_sequence(g, &t_u32->id, n);
91 if (!t_seq) return -1;
92
93 OmegaObject *op_add = omega_build_op_binary(g, OP_ADD, OVERFLOW_WRAP, &t_u32->id);
94 if (!op_add) return -1;
95
96 OmegaObject *spec_obj = omega_graph_add_object(g, KIND_OPERATION);
97 if (!spec_obj) return -1;
98
99 omega_object_add_attribute(spec_obj, "name", (const uint8_t *)"omega_blackwell_vector_add", 26);
100 omega_object_add_relation(spec_obj, REL_DERIVED_FROM, &op_add->id);
101 omega_object_add_relation(spec_obj, REL_DEPENDS_ON, &t_seq->id);
102
103 uint8_t payload[8];
104 memcpy(payload, &n, sizeof(n));
105 uint32_t elem_sz = 4;
106 memcpy(payload + 4, &elem_sz, sizeof(elem_sz));
107 memcpy(spec_obj->payload, payload, sizeof(payload));
108 spec_obj->payload_len = sizeof(payload);
109
110 if (omega_compute_semantic_id(spec_obj) != 0) return -1;
111 *out_spec_id = spec_obj->id;
112 return 0;
113}