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 / physics0 / ladder / pd0_fmt.c

367 lines · 21485 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 "pd0_fmt.h"
18#include "pd0_codes.h"
19#include "sha256.h"
20#include <string.h>
21
22void pd0_put_u16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); }
23void pd0_put_u32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); }
24void pd0_put_u64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); }
25uint16_t pd0_get_u16(const uint8_t *p) { return (uint16_t)(p[0] | (p[1] << 8)); }
26uint32_t pd0_get_u32(const uint8_t *p) { uint32_t v = 0; for (int i = 3; i >= 0; i--) v = (v << 8) | p[i]; return v; }
27uint64_t pd0_get_u64(const uint8_t *p) { uint64_t v = 0; for (int i = 7; i >= 0; i--) v = (v << 8) | p[i]; return v; }
28static void put_i64(uint8_t *p, int64_t v) { pd0_put_u64(p, (uint64_t)v); }
29static int64_t get_i64(const uint8_t *p) { return (int64_t)pd0_get_u64(p); }
30
31int64_t pd0_mul(int64_t a, int64_t b)
32{
33 __extension__ typedef __int128 i128;
34 i128 p = (i128)a * (i128)b;
35 return (int64_t)(p / PD0_MICRO); /* truncation toward zero */
36}
37
38/* ---------------- PD0REC1 ---------------- */
39static const uint8_t REC_MAGIC[8] = { 'P', 'D', '0', 'R', 'E', 'C', '1', 0 };
40size_t pd0_rec_size(uint8_t n_obs) { return 56u + 16u * n_obs + 2u * PD0_HASH; }
41
42size_t pd0_rec_write(const pd0_rec *r, uint8_t *out, size_t cap)
43{
44 size_t n = pd0_rec_size(r->n_obs);
45 if (r->n_obs == 0 || r->n_obs > PD0_MAX_OBS || cap < n) return 0;
46 memcpy(out, REC_MAGIC, 8); pd0_put_u16(out + 8, 1); out[10] = r->n_obs; out[11] = r->status;
47 out[12] = r->kind; out[13] = r->channel; out[14] = 0; out[15] = 0;
48 pd0_put_u64(out + 16, r->seq); pd0_put_u32(out + 24, r->episode); pd0_put_u32(out + 28, r->step_in_episode);
49 put_i64(out + 32, r->time_micro); put_i64(out + 40, r->requested); put_i64(out + 48, r->applied);
50 size_t o = 56;
51 for (int i = 0; i < r->n_obs; i++, o += 8) put_i64(out + o, r->before[i]);
52 for (int i = 0; i < r->n_obs; i++, o += 8) put_i64(out + o, r->after[i]);
53 memcpy(out + o, r->prev_hash, PD0_HASH); o += PD0_HASH;
54 sha256_hash(out, o, out + o);
55 memcpy(((pd0_rec *)r)->record_hash, out + o, PD0_HASH);
56 return n;
57}
58
59int pd0_rec_parse(const uint8_t *in, size_t len, pd0_rec *r, size_t *used)
60{
61 if (len < 16) return PD0V_TRUNCATED;
62 if (memcmp(in, REC_MAGIC, 8) != 0) return PD0V_BAD_MAGIC;
63 if (pd0_get_u16(in + 8) != 1) return PD0V_BAD_VERSION;
64 memset(r, 0, sizeof *r);
65 r->n_obs = in[10];
66 if (r->n_obs == 0 || r->n_obs > PD0_MAX_OBS) return PD0V_BAD_FIELD;
67 size_t n = pd0_rec_size(r->n_obs);
68 if (len < n) return PD0V_TRUNCATED;
69 r->status = in[11]; r->kind = in[12]; r->channel = in[13];
70 if (r->status > PD0_ST_BUDGET_EXHAUSTED || r->kind > 1) return PD0V_BAD_FIELD;
71 r->seq = pd0_get_u64(in + 16); r->episode = pd0_get_u32(in + 24); r->step_in_episode = pd0_get_u32(in + 28);
72 r->time_micro = get_i64(in + 32); r->requested = get_i64(in + 40); r->applied = get_i64(in + 48);
73 size_t o = 56;
74 for (int i = 0; i < r->n_obs; i++, o += 8) r->before[i] = get_i64(in + o);
75 for (int i = 0; i < r->n_obs; i++, o += 8) r->after[i] = get_i64(in + o);
76 memcpy(r->prev_hash, in + o, PD0_HASH); o += PD0_HASH;
77 uint8_t h[PD0_HASH]; sha256_hash(in, o, h);
78 if (memcmp(h, in + o, PD0_HASH) != 0) return PD0V_HASH_MISMATCH;
79 memcpy(r->record_hash, in + o, PD0_HASH);
80 if (used) *used = n;
81 return PD0V_OK;
82}
83
84/* ---------------- relationship block ---------------- */
85size_t pd0_rel_write(const pd0_rel *r, uint8_t *out, size_t cap)
86{
87 size_t o = 0; unsigned ne = r->n_vars + r->n_channels;
88 if (r->n_vars == 0 || r->n_vars > PD0_MAX_VARS || r->n_channels > PD0_MAX_CHAN || r->n_latent >= r->n_vars || r->n_equations > PD0_MAX_EQ) return 0;
89 if (cap < 10) return 0;
90 out[o++] = r->n_vars; out[o++] = r->n_latent; out[o++] = r->n_channels;
91 pd0_put_u32(out + o, r->description_bits); o += 4;
92 pd0_put_u16(out + o, r->n_refutations); o += 2;
93 out[o++] = r->n_equations;
94 for (int e = 0; e < r->n_equations; e++) {
95 const pd0_eq *q = &r->eq[e];
96 if (q->n_terms > PD0_MAX_TERMS) return 0;
97 if (cap < o + 3 + (size_t)q->n_terms * (8 + ne)) return 0;
98 out[o++] = q->target; pd0_put_u16(out + o, q->n_terms); o += 2;
99 for (int t = 0; t < q->n_terms; t++) {
100 put_i64(out + o, q->coef[t]); o += 8;
101 memcpy(out + o, q->expo[t], ne); o += ne;
102 }
103 }
104 return o;
105}
106
107int pd0_rel_parse(const uint8_t *in, size_t len, pd0_rel *r, size_t *used)
108{
109 size_t o = 0;
110 if (len < 10) return PD0V_TRUNCATED;
111 memset(r, 0, sizeof *r);
112 r->n_vars = in[o++]; r->n_latent = in[o++]; r->n_channels = in[o++];
113 r->description_bits = pd0_get_u32(in + o); o += 4;
114 r->n_refutations = pd0_get_u16(in + o); o += 2;
115 r->n_equations = in[o++];
116 unsigned ne = r->n_vars + r->n_channels;
117 if (r->n_vars == 0 || r->n_vars > PD0_MAX_VARS || r->n_channels > PD0_MAX_CHAN || r->n_latent >= r->n_vars || r->n_equations > PD0_MAX_EQ) return PD0V_BAD_FIELD;
118 for (int e = 0; e < r->n_equations; e++) {
119 pd0_eq *q = &r->eq[e];
120 if (len < o + 3) return PD0V_TRUNCATED;
121 q->target = in[o++]; q->n_terms = pd0_get_u16(in + o); o += 2;
122 if (q->target >= r->n_vars) return PD0V_BAD_FIELD;
123 if (q->n_terms > PD0_MAX_TERMS) return PD0V_TOO_LARGE;
124 if (len < o + (size_t)q->n_terms * (8 + ne)) return PD0V_TRUNCATED;
125 for (int t = 0; t < q->n_terms; t++) {
126 q->coef[t] = get_i64(in + o); o += 8;
127 memcpy(q->expo[t], in + o, ne); o += ne;
128 }
129 }
130 if (used) *used = o;
131 return PD0V_OK;
132}
133
134uint32_t pd0_rel_size(const pd0_rel *r)
135{
136 uint32_t s = r->n_latent;
137 for (int e = 0; e < r->n_equations; e++) for (int t = 0; t < r->eq[e].n_terms; t++) if (r->eq[e].coef[t] != 0) s++;
138 return s;
139}
140uint32_t pd0_rel_bits(const pd0_rel *r)
141{
142 uint32_t b = 8u * r->n_latent, ne = r->n_vars + r->n_channels;
143 for (int e = 0; e < r->n_equations; e++) b += (uint32_t)r->eq[e].n_terms * (8u * ne + 24u);
144 return b;
145}
146int pd0_rel_max_degree(const pd0_rel *r)
147{
148 int d = 0; unsigned ne = r->n_vars + r->n_channels;
149 for (int e = 0; e < r->n_equations; e++) for (int t = 0; t < r->eq[e].n_terms; t++) {
150 int s = 0; for (unsigned i = 0; i < ne; i++) s += r->eq[e].expo[t][i];
151 if (s > d) d = s;
152 }
153 return d;
154}
155
156static int64_t ipow_micro(int64_t base, uint8_t e)
157{
158 int64_t acc = PD0_MICRO;
159 for (uint8_t i = 0; i < e; i++) acc = pd0_mul(acc, base);
160 return acc;
161}
162
163void pd0_rel_step(const pd0_rel *r, const int64_t *state, uint8_t chan, int64_t value, int64_t *next)
164{
165 int64_t inputs[PD0_MAX_VARS + PD0_MAX_CHAN]; unsigned ne = r->n_vars + r->n_channels;
166 for (int i = 0; i < r->n_vars; i++) inputs[i] = state[i];
167 for (int c = 0; c < r->n_channels; c++) inputs[r->n_vars + c] = (chan == c) ? value : 0;
168 for (int i = 0; i < r->n_vars; i++) next[i] = state[i];
169 for (int e = 0; e < r->n_equations; e++) {
170 const pd0_eq *q = &r->eq[e]; int64_t d = 0;
171 for (int t = 0; t < q->n_terms; t++) {
172 int64_t m = q->coef[t];
173 for (unsigned i = 0; i < ne; i++) if (q->expo[t][i]) m = pd0_mul(m, ipow_micro(inputs[i], q->expo[t][i]));
174 d += m;
175 }
176 next[q->target] = state[q->target] + d;
177 }
178}
179
180/* ---------------- PDLAW1 ---------------- */
181static const uint8_t LAW_MAGIC[8] = { 'P', 'D', 'L', 'A', 'W', '1', 0, 0 };
182size_t pd0_dom_write(const pd0_domain *d, uint8_t *o)
183{
184 size_t p = 0; o[p++] = d->n_obs; o[p++] = d->n_channels;
185 for (int i = 0; i < d->n_obs; i++) { put_i64(o + p, d->var_min[i]); p += 8; put_i64(o + p, d->var_max[i]); p += 8; }
186 for (int i = 0; i < d->n_channels; i++) { put_i64(o + p, d->chan_min[i]); p += 8; put_i64(o + p, d->chan_max[i]); p += 8; }
187 put_i64(o + p, d->dt_micro); p += 8; pd0_put_u64(o + p, d->n_observations); p += 8; pd0_put_u32(o + p, d->n_episodes); p += 4;
188 put_i64(o + p, d->reset_min); p += 8; put_i64(o + p, d->reset_max); p += 8; pd0_put_u32(o + p, d->episode_len); p += 4; put_i64(o + p, d->latent_reset); p += 8;
189 return p;
190}
191static int dom_parse(const uint8_t *i, size_t n, pd0_domain *d, size_t *used)
192{
193 size_t p = 0; if (n < 2) return PD0V_TRUNCATED;
194 memset(d, 0, sizeof *d); d->n_obs = i[p++]; d->n_channels = i[p++];
195 if (d->n_obs == 0 || d->n_obs > PD0_MAX_OBS || d->n_channels > PD0_MAX_CHAN) return PD0V_BAD_FIELD;
196 size_t need = 2 + 16u * d->n_obs + 16u * d->n_channels + 8 + 8 + 4 + 8 + 8 + 4 + 8;
197 if (n < need) return PD0V_TRUNCATED;
198 for (int k = 0; k < d->n_obs; k++) { d->var_min[k] = get_i64(i + p); p += 8; d->var_max[k] = get_i64(i + p); p += 8; }
199 for (int k = 0; k < d->n_channels; k++) { d->chan_min[k] = get_i64(i + p); p += 8; d->chan_max[k] = get_i64(i + p); p += 8; }
200 d->dt_micro = get_i64(i + p); p += 8; d->n_observations = pd0_get_u64(i + p); p += 8; d->n_episodes = pd0_get_u32(i + p); p += 4;
201 d->reset_min = get_i64(i + p); p += 8; d->reset_max = get_i64(i + p); p += 8; d->episode_len = pd0_get_u32(i + p); p += 4; d->latent_reset = get_i64(i + p); p += 8;
202 *used = p; return PD0V_OK;
203}
204
205size_t pd0_law_write(pd0_law *l, uint8_t *out, size_t cap)
206{
207 size_t o = 0;
208 if (cap < 11 + PD0_HASH || l->n_exceptions > PD0_MAX_EXC || l->n_experiments > PD0_MAX_EXP || l->claim_len > PD0_MAX_CLAIM) return 0;
209 memcpy(out, LAW_MAGIC, 8); o = 8; pd0_put_u16(out + o, 1); o += 2; out[o++] = l->state;
210 size_t id_off = o; memset(out + o, 0, PD0_HASH); o += PD0_HASH;
211 size_t n = pd0_rel_write(&l->rel, out + o, cap - o); if (!n) return 0; o += n;
212 if (cap < o + 400) return 0;
213 o += pd0_dom_write(&l->dom, out + o);
214 pd0_put_u32(out + o, l->confidence_ppm); o += 4;
215 pd0_put_u16(out + o, (uint16_t)l->n_exceptions); o += 2;
216 if (cap < o + (size_t)l->n_exceptions * 64 + (size_t)l->n_experiments * 115 + 40 + l->claim_len) return 0;
217 for (uint32_t k = 0; k < l->n_exceptions; k++) {
218 const pd0_exception *x = &l->exc[k];
219 pd0_put_u64(out + o, x->record_seq); o += 8; memcpy(out + o, x->record_hash, PD0_HASH); o += PD0_HASH;
220 put_i64(out + o, x->predicted); o += 8; put_i64(out + o, x->observed); o += 8; put_i64(out + o, x->error_micro); o += 8;
221 }
222 pd0_put_u16(out + o, (uint16_t)l->n_experiments); o += 2;
223 for (uint32_t k = 0; k < l->n_experiments; k++) {
224 const pd0_experiment *x = &l->exp[k];
225 memcpy(out + o, x->experiment_id, PD0_HASH); o += PD0_HASH; out[o++] = x->kind;
226 memcpy(out + o, x->prereg_hash, PD0_HASH); o += PD0_HASH; memcpy(out + o, x->outcome_hash, PD0_HASH); o += PD0_HASH;
227 out[o++] = x->result; pd0_put_u64(out + o, x->first_seq); o += 8; pd0_put_u64(out + o, x->last_seq); o += 8;
228 }
229 memcpy(out + o, l->chain_root, PD0_HASH); o += PD0_HASH;
230 pd0_put_u16(out + o, (uint16_t)l->claim_len); o += 2; memcpy(out + o, l->claim, l->claim_len); o += l->claim_len;
231 sha256_hash(out, o, l->law_id); memcpy(out + id_off, l->law_id, PD0_HASH);
232 return o;
233}
234
235int pd0_law_parse(const uint8_t *in, size_t len, pd0_law *l)
236{
237 size_t o = 0, u = 0; int rc;
238 if (len < 11 + PD0_HASH) return PD0V_TRUNCATED;
239 if (memcmp(in, LAW_MAGIC, 8) != 0) return PD0V_BAD_MAGIC;
240 if (pd0_get_u16(in + 8) != 1) return PD0V_BAD_VERSION;
241 memset(l, 0, sizeof *l); o = 10; l->state = in[o++];
242 if (l->state > PDLAW_PROVISIONAL_LAW) return PD0V_BAD_FIELD;
243 memcpy(l->law_id, in + o, PD0_HASH); o += PD0_HASH;
244 if ((rc = pd0_rel_parse(in + o, len - o, &l->rel, &u)) != 0) return rc;
245 o += u;
246 if ((rc = dom_parse(in + o, len - o, &l->dom, &u)) != 0) return rc;
247 o += u;
248 if (len < o + 6) return PD0V_TRUNCATED;
249 l->confidence_ppm = pd0_get_u32(in + o); o += 4; l->n_exceptions = pd0_get_u16(in + o); o += 2;
250 if (l->confidence_ppm > 1000000u) return PD0V_BAD_FIELD;
251 if (l->n_exceptions > PD0_MAX_EXC) return PD0V_TOO_LARGE;
252 if (len < o + (size_t)l->n_exceptions * 64 + 2) return PD0V_TRUNCATED;
253 for (uint32_t k = 0; k < l->n_exceptions; k++) {
254 pd0_exception *x = &l->exc[k];
255 x->record_seq = pd0_get_u64(in + o); o += 8; memcpy(x->record_hash, in + o, PD0_HASH); o += PD0_HASH;
256 x->predicted = get_i64(in + o); o += 8; x->observed = get_i64(in + o); o += 8; x->error_micro = get_i64(in + o); o += 8;
257 }
258 l->n_experiments = pd0_get_u16(in + o); o += 2;
259 if (l->n_experiments > PD0_MAX_EXP) return PD0V_TOO_LARGE;
260 if (len < o + (size_t)l->n_experiments * 115 + PD0_HASH + 2) return PD0V_TRUNCATED;
261 for (uint32_t k = 0; k < l->n_experiments; k++) {
262 pd0_experiment *x = &l->exp[k];
263 memcpy(x->experiment_id, in + o, PD0_HASH); o += PD0_HASH; x->kind = in[o++];
264 memcpy(x->prereg_hash, in + o, PD0_HASH); o += PD0_HASH; memcpy(x->outcome_hash, in + o, PD0_HASH); o += PD0_HASH;
265 x->result = in[o++]; x->first_seq = pd0_get_u64(in + o); o += 8; x->last_seq = pd0_get_u64(in + o); o += 8;
266 if (x->kind > 1 || x->result > 1) return PD0V_BAD_FIELD;
267 }
268 memcpy(l->chain_root, in + o, PD0_HASH); o += PD0_HASH;
269 l->claim_len = pd0_get_u16(in + o); o += 2;
270 if (l->claim_len > PD0_MAX_CLAIM) return PD0V_TOO_LARGE;
271 if (len < o + l->claim_len) return PD0V_TRUNCATED;
272 memcpy(l->claim, in + o, l->claim_len); o += l->claim_len;
273 /* verify law_id: hash of canonical bytes with the id zeroed */
274 uint8_t tmp[4096]; if (o > sizeof tmp) return PD0V_TOO_LARGE;
275 memcpy(tmp, in, o); memset(tmp + 11, 0, PD0_HASH);
276 uint8_t h[PD0_HASH]; sha256_hash(tmp, o, h);
277 if (memcmp(h, l->law_id, PD0_HASH) != 0) return PD0V_LAW_ID_MISMATCH;
278 return PD0V_OK;
279}
280
281/* ---------------- PD0EXP1 ---------------- */
282static const uint8_t EXP_MAGIC[8] = { 'P', 'D', '0', 'E', 'X', 'P', '1', 0 };
283void pd0_schedule_hash(uint8_t n_obs, const int64_t *reset, uint8_t n_steps, const pd0_step *steps, uint8_t out[PD0_HASH])
284{
285 uint8_t buf[8 * PD0_MAX_OBS + 9 * PD0_MAX_STEPS]; size_t o = 0;
286 for (int i = 0; i < n_obs; i++, o += 8) put_i64(buf + o, reset[i]);
287 for (int s = 0; s < n_steps; s++) { buf[o++] = steps[s].channel; put_i64(buf + o, steps[s].value); o += 8; }
288 sha256_hash(buf, o, out);
289}
290size_t pd0_exp_write(pd0_exp *e, uint8_t *out, size_t cap)
291{
292 if (e->n_obs == 0 || e->n_obs > PD0_MAX_OBS || e->n_hyp == 0 || e->n_hyp > PD0_MAX_HYP || e->n_steps == 0 || e->n_steps > PD0_MAX_STEPS) return 0;
293 size_t need = 16 + 8u * e->n_obs + 9u * e->n_steps + 8u * e->n_hyp * e->n_steps * e->n_obs + 8 + PD0_HASH;
294 if (cap < need) return 0;
295 size_t o = 0; memcpy(out, EXP_MAGIC, 8); o = 8; pd0_put_u16(out + o, 1); o += 2;
296 out[o++] = e->n_obs; out[o++] = e->n_hyp; out[o++] = e->n_steps; out[o++] = 0; out[o++] = 0; out[o++] = 0;
297 for (int i = 0; i < e->n_obs; i++, o += 8) put_i64(out + o, e->reset[i]);
298 for (int s = 0; s < e->n_steps; s++) { out[o++] = e->steps[s].channel; put_i64(out + o, e->steps[s].value); o += 8; }
299 for (int h = 0; h < e->n_hyp; h++) for (int s = 0; s < e->n_steps; s++) for (int i = 0; i < e->n_obs; i++, o += 8) put_i64(out + o, e->expected[h][s][i]);
300 put_i64(out + o, e->divergence_micro); o += 8;
301 pd0_schedule_hash(e->n_obs, e->reset, e->n_steps, e->steps, e->schedule_hash);
302 memcpy(out + o, e->schedule_hash, PD0_HASH); o += PD0_HASH;
303 return o;
304}
305int pd0_exp_parse(const uint8_t *in, size_t len, pd0_exp *e, size_t *used)
306{
307 if (len < 16) return PD0V_TRUNCATED;
308 if (memcmp(in, EXP_MAGIC, 8) != 0) return PD0V_BAD_MAGIC;
309 if (pd0_get_u16(in + 8) != 1) return PD0V_BAD_VERSION;
310 memset(e, 0, sizeof *e); e->n_obs = in[10]; e->n_hyp = in[11]; e->n_steps = in[12];
311 if (e->n_obs == 0 || e->n_obs > PD0_MAX_OBS || e->n_hyp == 0 || e->n_hyp > PD0_MAX_HYP || e->n_steps == 0 || e->n_steps > PD0_MAX_STEPS) return PD0V_BAD_FIELD;
312 size_t need = 16 + 8u * e->n_obs + 9u * e->n_steps + 8u * e->n_hyp * e->n_steps * e->n_obs + 8 + PD0_HASH;
313 if (len < need) return PD0V_TRUNCATED;
314 size_t o = 16;
315 for (int i = 0; i < e->n_obs; i++, o += 8) e->reset[i] = get_i64(in + o);
316 for (int s = 0; s < e->n_steps; s++) { e->steps[s].channel = in[o++]; e->steps[s].value = get_i64(in + o); o += 8; }
317 for (int h = 0; h < e->n_hyp; h++) for (int s = 0; s < e->n_steps; s++) for (int i = 0; i < e->n_obs; i++, o += 8) e->expected[h][s][i] = get_i64(in + o);
318 e->divergence_micro = get_i64(in + o); o += 8;
319 uint8_t h[PD0_HASH]; pd0_schedule_hash(e->n_obs, e->reset, e->n_steps, e->steps, h);
320 if (memcmp(h, in + o, PD0_HASH) != 0) return PD0V_HASH_MISMATCH;
321 memcpy(e->schedule_hash, h, PD0_HASH); o += PD0_HASH;
322 if (used) *used = o;
323 return PD0V_OK;
324}
325
326/* ---------------- ledger ---------------- */
327static const uint8_t LEDG_MAGIC[8] = { 'P', 'D', '0', 'L', 'E', 'D', 'G', '1' };
328size_t pd0_ledg_append(uint8_t *buf, size_t len, size_t cap, uint8_t kind, const uint8_t *payload, uint32_t plen, uint8_t last_hash[PD0_HASH])
329{
330 size_t need = PD0_LEDG_HDR + plen + PD0_LEDG_TRAILER;
331 if (len + need > cap || kind > LEDG_BATCH) return 0;
332 uint8_t *o = buf + len;
333 memcpy(o, LEDG_MAGIC, 8); pd0_put_u16(o + 8, 1); o[10] = kind; o[11] = 0; pd0_put_u32(o + 12, plen);
334 memcpy(o + PD0_LEDG_HDR, payload, plen);
335 memcpy(o + PD0_LEDG_HDR + plen, last_hash, PD0_HASH);
336 sha256_hash(o, PD0_LEDG_HDR + plen + PD0_HASH, o + PD0_LEDG_HDR + plen + PD0_HASH);
337 memcpy(last_hash, o + PD0_LEDG_HDR + plen + PD0_HASH, PD0_HASH);
338 return len + need;
339}
340int pd0_ledg_next(const uint8_t *buf, size_t len, size_t *off, const uint8_t expect_prev[PD0_HASH], pd0_entry *e)
341{
342 size_t o = *off;
343 if (len - o < PD0_LEDG_HDR) return PD0V_TRUNCATED;
344 if (memcmp(buf + o, LEDG_MAGIC, 8) != 0) return PD0V_BAD_MAGIC;
345 if (pd0_get_u16(buf + o + 8) != 1) return PD0V_BAD_VERSION;
346 e->kind = buf[o + 10]; e->payload_len = pd0_get_u32(buf + o + 12);
347 if (e->kind > LEDG_BATCH) return PD0V_BAD_FIELD;
348 if (len - o < PD0_LEDG_HDR + (size_t)e->payload_len + PD0_LEDG_TRAILER) return PD0V_TRUNCATED;
349 e->payload = buf + o + PD0_LEDG_HDR;
350 const uint8_t *prev = e->payload + e->payload_len, *hash = prev + PD0_HASH;
351 memcpy(e->prev_hash, prev, PD0_HASH); memcpy(e->entry_hash, hash, PD0_HASH);
352 if (memcmp(prev, expect_prev, PD0_HASH) != 0) return PD0V_CHAIN_BROKEN;
353 uint8_t h[PD0_HASH]; sha256_hash(buf + o, PD0_LEDG_HDR + e->payload_len + PD0_HASH, h);
354 if (memcmp(h, hash, PD0_HASH) != 0) return PD0V_HASH_MISMATCH;
355 *off = o + PD0_LEDG_HDR + e->payload_len + PD0_LEDG_TRAILER;
356 return PD0V_OK;
357}
358
359/* payloads */
360size_t pd0_tag_write(const pd0_tag *t, uint8_t *o) { pd0_put_u32(o, t->episode); o[4] = t->tag; pd0_put_u32(o + 5, t->batch_id); o[9] = t->origin; return 10; }
361int pd0_tag_parse(const uint8_t *i, size_t n, pd0_tag *t) { if (n != 10) return PD0V_TRUNCATED; t->episode = pd0_get_u32(i); t->tag = i[4]; t->batch_id = pd0_get_u32(i + 5); t->origin = i[9]; return (t->tag > TAG_REP || t->origin > 1) ? PD0V_BAD_FIELD : 0; }
362size_t pd0_corr_write(const pd0_corr *c, uint8_t *o) { o[0] = c->var_a; o[1] = c->var_b; pd0_put_u32(o + 2, c->n); put_i64(o + 6, c->r_micro); put_i64(o + 14, c->p_micro); pd0_put_u32(o + 22, c->n_pairs); pd0_put_u32(o + 26, c->n_shuffles); pd0_put_u64(o + 30, c->shuffle_seed); return 38; }
363int pd0_corr_parse(const uint8_t *i, size_t n, pd0_corr *c) { if (n != 38) return PD0V_TRUNCATED; c->var_a = i[0]; c->var_b = i[1]; c->n = pd0_get_u32(i + 2); c->r_micro = get_i64(i + 6); c->p_micro = get_i64(i + 14); c->n_pairs = pd0_get_u32(i + 22); c->n_shuffles = pd0_get_u32(i + 26); c->shuffle_seed = pd0_get_u64(i + 30); return 0; }
364size_t pd0_fals_write(const pd0_falsifier *f, uint8_t *o) { size_t p = 0; memcpy(o, f->candidate, PD0_HASH); p = PD0_HASH; o[p++] = f->n_rivals; for (int k = 0; k < f->n_rivals; k++) { memcpy(o + p, f->rival[k], PD0_HASH); p += PD0_HASH; } put_i64(o + p, f->eps_micro); p += 8; pd0_put_u32(o + p, f->min_trials); p += 4; return p; }
365int pd0_fals_parse(const uint8_t *i, size_t n, pd0_falsifier *f) { if (n < PD0_HASH + 1) return PD0V_TRUNCATED; memcpy(f->candidate, i, PD0_HASH); f->n_rivals = i[PD0_HASH]; if (f->n_rivals > PD0_MAX_HYP - 1) return PD0V_BAD_FIELD; size_t p = PD0_HASH + 1; if (n != p + (size_t)f->n_rivals * PD0_HASH + 12) return PD0V_TRUNCATED; for (int k = 0; k < f->n_rivals; k++) { memcpy(f->rival[k], i + p, PD0_HASH); p += PD0_HASH; } f->eps_micro = get_i64(i + p); p += 8; f->min_trials = pd0_get_u32(i + p); return 0; }
366size_t pd0_batch_write(const pd0_batch *b, uint8_t *o) { pd0_put_u32(o, b->batch_id); pd0_put_u64(o + 4, b->stream_seed); pd0_put_u32(o + 12, b->n_episodes); return 16; }
367int pd0_batch_parse(const uint8_t *i, size_t n, pd0_batch *b) { if (n != 16) return PD0V_TRUNCATED; b->batch_id = pd0_get_u32(i); b->stream_seed = pd0_get_u64(i + 4); b->n_episodes = pd0_get_u32(i + 12); return 0; }