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/src/omega_program_ir.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 / src / omega_program_ir.c` 124 lines ยท 4399 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 /* omega_program_ir.c -- see omega_program_ir.h. */ 18 #include "omega_program_ir.h" 19 20 #include 21 #include 22 23 24 typedef struct { uint8_t *p; size_t n, cap; int bad; } W; 25 26 static void wput(W *w, const void *d, size_t k) 27 { 28 if (w->bad) return; 29 if (w->n + k > w->cap) { 30 size_t nc = (w->n + k) * 2 + 64; 31 uint8_t *t = realloc(w->p, nc); 32 if (!t) { w->bad = 1; return; } 33 w->p = t; 34 w->cap = nc; 35 } 36 memcpy(w->p + w->n, d, k); 37 w->n += k; 38 } 39 static void wu8(W *w, uint8_t v) { wput(w, &v, 1); } 40 static void wu16(W *w, uint16_t v) { uint8_t t[2] = { (uint8_t)(v >> 8), (uint8_t)v }; wput(w, t, 2); } 41 static void wu64(W *w, uint64_t v) 42 { 43 uint8_t t[8]; 44 for (int i = 0; i < 8; i++) t[i] = (uint8_t)(v >> (56 - 8 * i)); 45 wput(w, t, 8); 46 } 47 48 int omega_program_ir_encode(const OmegaProgram *prog, uint8_t **out, size_t *len) 49 { 50 if (!prog || !out || !len || !prog->body.has_body || prog->body.step_count > OMEGA_PROGRAM_MAX_STEPS) return -1; 51 W w = { NULL, 0, 0, 0 }; 52 wput(&w, OMEGA_PROGRAM_IR_TAG, OMEGA_PROGRAM_IR_TAG_LEN); 53 wu8(&w, (uint8_t)prog->contract.input_type); 54 wu16(&w, prog->contract.input_width); 55 wu8(&w, (uint8_t)prog->contract.output_type); 56 wu16(&w, prog->contract.output_width); 57 wput(&w, prog->contract.precondition_id.bytes, 32); 58 wput(&w, prog->contract.postcondition_id.bytes, 32); 59 wu16(&w, prog->body.step_count); 60 for (uint16_t i = 0; i < prog->body.step_count; i++) { 61 wu8(&w, prog->body.steps[i].op); 62 wu64(&w, prog->body.steps[i].imm); 63 } 64 if (w.bad) { free(w.p); return -1; } 65 *out = w.p; 66 *len = w.n; 67 return 0; 68 } 69 70 typedef struct { const uint8_t *b; size_t n, pos; int bad; } R; 71 static const uint8_t *rtake(R *r, size_t k) 72 { 73 if (r->bad || r->n - r->pos < k) { r->bad = 1; return NULL; } /* VC1I:truncated */ 74 const uint8_t *p = r->b + r->pos; 75 r->pos += k; 76 return p; 77 } 78 static uint64_t rbe(R *r, size_t k) 79 { 80 const uint8_t *p = rtake(r, k); 81 uint64_t v = 0; 82 if (!p) return 0; 83 for (size_t i = 0; i < k; i++) v = v << 8 | p[i]; 84 return v; 85 } 86 87 int omega_program_ir_decode(const uint8_t *bytes, size_t len, OmegaProgram *out) 88 { 89 if (!bytes || !out) return -1; 90 memset(out, 0, sizeof *out); 91 R r = { bytes, len, 0, 0 }; 92 const uint8_t *tag = rtake(&r, OMEGA_PROGRAM_IR_TAG_LEN); 93 if (!tag || memcmp(tag, OMEGA_PROGRAM_IR_TAG, OMEGA_PROGRAM_IR_TAG_LEN) != 0) return -1; /* VC1I:tag */ 94 OmegaProgram *p = out; 95 p->contract.input_type = (TypeTag)rbe(&r, 1); 96 p->contract.input_width = (uint16_t)rbe(&r, 2); 97 p->contract.output_type = (TypeTag)rbe(&r, 1); 98 p->contract.output_width = (uint16_t)rbe(&r, 2); 99 const uint8_t *pre = rtake(&r, 32), *post = rtake(&r, 32); 100 uint64_t steps = rbe(&r, 2); 101 if (r.bad || steps > OMEGA_PROGRAM_MAX_STEPS) { memset(out, 0, sizeof *out); return -1; } /* VC1I:steps */ 102 memcpy(p->contract.precondition_id.bytes, pre, 32); 103 memcpy(p->contract.postcondition_id.bytes, post, 32); 104 p->body.has_body = true; 105 p->body.step_count = (uint16_t)steps; 106 for (uint64_t i = 0; i < steps; i++) { 107 p->body.steps[i].op = (uint8_t)rbe(&r, 1); 108 p->body.steps[i].imm = rbe(&r, 8); 109 } 110 if (r.bad || r.pos != r.n) { memset(out, 0, sizeof *out); return -1; } /* VC1I:trailing */ 111 if (omega_program_compute_id(p) != 0) { memset(out, 0, sizeof *out); return -1; } /* VC1I:id */ 112 return 0; 113 } 114 115 int omega_program_ir_recompute_id(const uint8_t *bytes, size_t len, uint8_t id[32]) 116 { 117 OmegaProgram *p = malloc(sizeof *p); 118 if (!p) return -1; 119 int rc = omega_program_ir_decode(bytes, len, p); 120 if (rc == 0) memcpy(id, p->program_id.bytes, 32); 121 free(p); 122 return rc; 123 } 124