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 / physics0 / pd0_wire.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 | #include "physics0/pd0_wire.h" |
| 18 | #include "omega_native.h" |
| 19 | |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include "sha256.h" |
| 23 | |
| 24 | void pd0_put_u16(uint8_t *p, uint16_t v) { uint64_t u = 0; (void)omega2_pd0_om_put_u16(&u, p, 2, 0, v); } |
| 25 | void pd0_put_u32(uint8_t *p, uint32_t v) { uint64_t u = 0; (void)omega2_pd0_om_put_u32(&u, p, 4, 0, v); } |
| 26 | void pd0_put_u64(uint8_t *p, uint64_t v) { uint64_t u = 0; (void)omega2_pd0_om_put_u64(&u, p, 8, 0, v); } |
| 27 | uint16_t pd0_get_u16(const uint8_t *p) { uint64_t v = 0; (void)omega2_pd0_om_get_u16(&v, p, 2, 0); return (uint16_t)v; } |
| 28 | uint32_t pd0_get_u32(const uint8_t *p) { uint64_t v = 0; (void)omega2_pd0_om_get_u32(&v, p, 4, 0); return (uint32_t)v; } |
| 29 | uint64_t pd0_get_u64(const uint8_t *p) { uint64_t v = 0; (void)omega2_pd0_om_get_u64(&v, p, 8, 0); return v; } |
| 30 | |
| 31 | static const uint8_t DESC_MAGIC[8] = {'P', 'D', '0', 'D', 'E', 'S', 'C', '2'}; |
| 32 | static const uint8_t REC_MAGIC[8] = {'P', 'D', '0', 'R', 'E', 'C', '1', 0}; |
| 33 | |
| 34 | size_t pd0_desc_encode(const pd0_desc *d, uint8_t *out, size_t cap) { |
| 35 | if (!d || d->n_obs < 1 || d->n_obs > PD0_MAX_OBS || d->n_channels < 1 || d->n_channels > PD0_MAX_CH) return 0; |
| 36 | size_t need = 8 + 2 + 8 + 16u * d->n_channels + 16u * d->n_obs + 12; |
| 37 | if (cap < need) return 0; |
| 38 | uint8_t *p = out; |
| 39 | memcpy(p, DESC_MAGIC, 8); p += 8; |
| 40 | *p++ = d->n_obs; *p++ = d->n_channels; |
| 41 | pd0_put_u64(p, (uint64_t)d->dt_micro); p += 8; |
| 42 | for (unsigned i = 0; i < d->n_channels; i++) { pd0_put_u64(p, (uint64_t)d->chan_min[i]); p += 8; } |
| 43 | for (unsigned i = 0; i < d->n_channels; i++) { pd0_put_u64(p, (uint64_t)d->chan_max[i]); p += 8; } |
| 44 | for (unsigned i = 0; i < d->n_obs; i++) { pd0_put_u64(p, (uint64_t)d->reset_min[i]); p += 8; } |
| 45 | for (unsigned i = 0; i < d->n_obs; i++) { pd0_put_u64(p, (uint64_t)d->reset_max[i]); p += 8; } |
| 46 | pd0_put_u32(p, d->episode_max_steps); p += 4; |
| 47 | pd0_put_u32(p, d->budget_steps); p += 4; |
| 48 | pd0_put_u32(p, d->budget_episodes); p += 4; |
| 49 | return (size_t)(p - out); |
| 50 | } |
| 51 | |
| 52 | int pd0_desc_decode(const uint8_t *in, size_t len, pd0_desc *d) { |
| 53 | uint64_t mrc = 2; |
| 54 | if (len > 0) (void)omega2_pd0_om_check_desc_magic(&mrc, in, len, len); |
| 55 | if (mrc == 1) return PD0_DESC_REFUSED_V1; |
| 56 | if (mrc != 0) return -1; |
| 57 | memset(d, 0, sizeof *d); |
| 58 | d->n_obs = in[8]; d->n_channels = in[9]; |
| 59 | if (d->n_obs < 1 || d->n_obs > PD0_MAX_OBS || d->n_channels < 1 || d->n_channels > PD0_MAX_CH) return -1; |
| 60 | size_t need = 8 + 2 + 8 + 16u * d->n_channels + 16u * d->n_obs + 12; |
| 61 | if (len != need) return -1; |
| 62 | const uint8_t *p = in + 10; |
| 63 | d->dt_micro = (int64_t)pd0_get_u64(p); p += 8; |
| 64 | for (unsigned i = 0; i < d->n_channels; i++) { d->chan_min[i] = (int64_t)pd0_get_u64(p); p += 8; } |
| 65 | for (unsigned i = 0; i < d->n_channels; i++) { d->chan_max[i] = (int64_t)pd0_get_u64(p); p += 8; } |
| 66 | for (unsigned i = 0; i < d->n_obs; i++) { d->reset_min[i] = (int64_t)pd0_get_u64(p); p += 8; } |
| 67 | for (unsigned i = 0; i < d->n_obs; i++) { d->reset_max[i] = (int64_t)pd0_get_u64(p); p += 8; } |
| 68 | d->episode_max_steps = pd0_get_u32(p); p += 4; |
| 69 | d->budget_steps = pd0_get_u32(p); p += 4; |
| 70 | d->budget_episodes = pd0_get_u32(p); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static size_t rec_body(const pd0_rec *r, uint8_t *out) { |
| 75 | uint8_t *p = out; |
| 76 | memcpy(p, REC_MAGIC, 8); p += 8; |
| 77 | pd0_put_u16(p, 1); p += 2; |
| 78 | *p++ = r->n_obs; *p++ = r->status; *p++ = r->kind; *p++ = r->channel; |
| 79 | *p++ = 0; *p++ = 0; |
| 80 | pd0_put_u64(p, r->seq); p += 8; |
| 81 | pd0_put_u32(p, r->episode); p += 4; |
| 82 | pd0_put_u32(p, r->step_in_episode); p += 4; |
| 83 | pd0_put_u64(p, (uint64_t)r->time_micro); p += 8; |
| 84 | pd0_put_u64(p, (uint64_t)r->requested); p += 8; |
| 85 | pd0_put_u64(p, (uint64_t)r->applied); p += 8; |
| 86 | for (unsigned i = 0; i < r->n_obs; i++) { pd0_put_u64(p, (uint64_t)r->before[i]); p += 8; } |
| 87 | for (unsigned i = 0; i < r->n_obs; i++) { pd0_put_u64(p, (uint64_t)r->after[i]); p += 8; } |
| 88 | memcpy(p, r->prev_hash, PD0_HASH); p += PD0_HASH; |
| 89 | return (size_t)(p - out); |
| 90 | } |
| 91 | |
| 92 | size_t pd0_rec_encode(pd0_rec *r, uint8_t *out, size_t cap) { |
| 93 | if (!r || r->n_obs < 1 || r->n_obs > PD0_MAX_OBS) return 0; |
| 94 | size_t need = PD0_REC_SIZE(r->n_obs); |
| 95 | if (cap < need) return 0; |
| 96 | size_t n = rec_body(r, out); |
| 97 | sha256_hash(out, n, r->hash); |
| 98 | memcpy(out + n, r->hash, PD0_HASH); |
| 99 | return n + PD0_HASH; |
| 100 | } |
| 101 | |
| 102 | int pd0_rec_decode(const uint8_t *in, size_t len, pd0_rec *r) { |
| 103 | uint64_t mrc = 1; |
| 104 | if (len > 0) (void)omega2_pd0_om_check_rec_magic(&mrc, in, len, len); |
| 105 | if (mrc != 0) return -1; |
| 106 | memset(r, 0, sizeof *r); |
| 107 | r->n_obs = in[10]; |
| 108 | if (r->n_obs < 1 || r->n_obs > PD0_MAX_OBS || len != PD0_REC_SIZE(r->n_obs)) return -1; |
| 109 | r->status = in[11]; r->kind = in[12]; r->channel = in[13]; |
| 110 | if (in[14] || in[15]) return -1; |
| 111 | const uint8_t *p = in + 16; |
| 112 | r->seq = pd0_get_u64(p); p += 8; |
| 113 | r->episode = pd0_get_u32(p); p += 4; |
| 114 | r->step_in_episode = pd0_get_u32(p); p += 4; |
| 115 | r->time_micro = (int64_t)pd0_get_u64(p); p += 8; |
| 116 | r->requested = (int64_t)pd0_get_u64(p); p += 8; |
| 117 | r->applied = (int64_t)pd0_get_u64(p); p += 8; |
| 118 | for (unsigned i = 0; i < r->n_obs; i++) { r->before[i] = (int64_t)pd0_get_u64(p); p += 8; } |
| 119 | for (unsigned i = 0; i < r->n_obs; i++) { r->after[i] = (int64_t)pd0_get_u64(p); p += 8; } |
| 120 | memcpy(r->prev_hash, p, PD0_HASH); p += PD0_HASH; |
| 121 | uint8_t h[PD0_HASH]; |
| 122 | sha256_hash(in, (size_t)(p - in), h); |
| 123 | if (memcmp(h, p, PD0_HASH) != 0) return -1; |
| 124 | memcpy(r->hash, h, PD0_HASH); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | int pd0_rec_chained(const pd0_rec *r, const pd0_rec *prev) { |
| 129 | static const uint8_t zero[PD0_HASH] = {0}; |
| 130 | if (!prev) return r->seq == 0 && memcmp(r->prev_hash, zero, PD0_HASH) == 0; |
| 131 | return r->seq == prev->seq + 1 && memcmp(r->prev_hash, prev->hash, PD0_HASH) == 0; |
| 132 | } |
| 133 | |
| 134 | size_t pd0_req_describe(uint8_t *out) { out[0] = PD0_OP_DESCRIBE; return 1; } |
| 135 | size_t pd0_req_reset(uint8_t n_obs, const int64_t *vals, uint8_t *out, size_t cap) { |
| 136 | if (n_obs < 1 || n_obs > PD0_MAX_OBS || !vals || !out) return 0; |
| 137 | size_t need = 2 + 8u * n_obs; |
| 138 | if (cap < need) return 0; |
| 139 | out[0] = PD0_OP_RESET; out[1] = n_obs; |
| 140 | for (unsigned i = 0; i < n_obs; i++) pd0_put_u64(out + 2 + 8 * i, (uint64_t)vals[i]); |
| 141 | return need; |
| 142 | } |
| 143 | size_t pd0_req_step(uint8_t channel, int64_t value, uint8_t *out) { |
| 144 | out[0] = PD0_OP_STEP; out[1] = channel; |
| 145 | pd0_put_u64(out + 2, (uint64_t)value); |
| 146 | return 10; |
| 147 | } |