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 / ladder / pd0_fmt.h
| 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 | /* PD-0 verifier: minimal byte layouts (PD0REC1, PDLAW1, PD0EXP1) and the |
| 18 | * verifier's evidence ledger (PD0LEDG1). Written from the spec on physics |
| 19 | * main (docs/PD0_HIDDEN_EQUATION_BENCHMARK.md rev 2, sections 2.5, 7, 8). |
| 20 | * Little-endian, fixed-width, no floating point in any canonical byte. |
| 21 | * To be deduplicated against pd0-wire when the substrate lane merges. */ |
| 22 | #ifndef PD0_FMT_H |
| 23 | #define PD0_FMT_H |
| 24 | #include <stddef.h> |
| 25 | #include <stdint.h> |
| 26 | |
| 27 | #define PD0_MICRO 1000000LL |
| 28 | #define PD0_MAX_OBS 8 |
| 29 | #define PD0_MAX_CHAN 4 |
| 30 | #define PD0_MAX_VARS 12 /* observed + latent */ |
| 31 | #define PD0_MAX_TERMS 64 |
| 32 | #define PD0_MAX_EQ 12 |
| 33 | #define PD0_MAX_STEPS 20 |
| 34 | #define PD0_MAX_HYP 8 |
| 35 | #define PD0_HASH 32 |
| 36 | |
| 37 | enum { PD0_ST_OK = 0, PD0_ST_REFUSED_RANGE = 1, PD0_ST_OUT_OF_BOUNDS = 2, |
| 38 | PD0_ST_EPISODE_END = 3, PD0_ST_BUDGET_EXHAUSTED = 4 }; |
| 39 | enum { PD0_KIND_RESET = 0, PD0_KIND_STEP = 1 }; |
| 40 | #define PD0_CHAN_NONE 255 |
| 41 | |
| 42 | /* fixed-point product per spec 2.1: 128-bit, truncate toward zero */ |
| 43 | int64_t pd0_mul(int64_t a, int64_t b); |
| 44 | |
| 45 | /* ---- PD0REC1 observation record ---- */ |
| 46 | typedef struct { |
| 47 | uint8_t n_obs, status, kind, channel; |
| 48 | uint64_t seq; |
| 49 | uint32_t episode, step_in_episode; |
| 50 | int64_t time_micro, requested, applied; |
| 51 | int64_t before[PD0_MAX_OBS], after[PD0_MAX_OBS]; |
| 52 | uint8_t prev_hash[PD0_HASH], record_hash[PD0_HASH]; |
| 53 | } pd0_rec; |
| 54 | size_t pd0_rec_size(uint8_t n_obs); |
| 55 | /* write: fills record_hash; prev_hash must be set by caller. returns bytes */ |
| 56 | size_t pd0_rec_write(const pd0_rec *r, uint8_t *out, size_t cap); |
| 57 | /* parse: 0 ok, else PD0V_* code (bad magic/version/truncated/hash) */ |
| 58 | int pd0_rec_parse(const uint8_t *in, size_t len, pd0_rec *r, size_t *used); |
| 59 | |
| 60 | /* ---- relationship block (shared by PDLAW1 and RELATION ledger entries) ---- */ |
| 61 | typedef struct { |
| 62 | uint8_t target; |
| 63 | uint16_t n_terms; |
| 64 | int64_t coef[PD0_MAX_TERMS]; |
| 65 | uint8_t expo[PD0_MAX_TERMS][PD0_MAX_VARS + PD0_MAX_CHAN]; |
| 66 | } pd0_eq; |
| 67 | typedef struct { |
| 68 | uint8_t n_vars, n_latent, n_channels, n_equations; |
| 69 | uint32_t description_bits; |
| 70 | uint16_t n_refutations; |
| 71 | pd0_eq eq[PD0_MAX_EQ]; |
| 72 | } pd0_rel; |
| 73 | size_t pd0_rel_write(const pd0_rel *r, uint8_t *out, size_t cap); |
| 74 | int pd0_rel_parse(const uint8_t *in, size_t len, pd0_rel *r, size_t *used); |
| 75 | uint32_t pd0_rel_size(const pd0_rel *r); /* spec 6.2 |R| */ |
| 76 | uint32_t pd0_rel_bits(const pd0_rel *r); /* spec 6.2 description_bits */ |
| 77 | int pd0_rel_max_degree(const pd0_rel *r); |
| 78 | /* one tick: state has n_vars entries (observed then latent); chan/value applied */ |
| 79 | void pd0_rel_step(const pd0_rel *r, const int64_t *state, uint8_t chan, int64_t value, int64_t *next); |
| 80 | |
| 81 | /* ---- PDLAW1 (list counts and claim length are u16, per the substrate lane amendment in omega PR #243) ---- */ |
| 82 | enum { PDLAW_REJECTED = 0, PDLAW_REFUTED = 1, PDLAW_HYPOTHESIS = 2, PDLAW_PROVISIONAL_LAW = 3 }; |
| 83 | typedef struct { uint64_t record_seq; uint8_t record_hash[PD0_HASH]; int64_t predicted, observed, error_micro; } pd0_exception; |
| 84 | typedef struct { uint8_t experiment_id[PD0_HASH]; uint8_t kind; uint8_t prereg_hash[PD0_HASH]; uint8_t outcome_hash[PD0_HASH]; uint8_t result; uint64_t first_seq, last_seq; } pd0_experiment; |
| 85 | #define PD0_MAX_EXC 64 |
| 86 | #define PD0_MAX_EXP 64 |
| 87 | #define PD0_MAX_CLAIM 512 |
| 88 | typedef struct { |
| 89 | uint8_t n_obs, n_channels; |
| 90 | int64_t var_min[PD0_MAX_OBS], var_max[PD0_MAX_OBS]; |
| 91 | int64_t chan_min[PD0_MAX_CHAN], chan_max[PD0_MAX_CHAN]; |
| 92 | int64_t dt_micro; |
| 93 | uint64_t n_observations; |
| 94 | uint32_t n_episodes; |
| 95 | int64_t reset_min, reset_max; |
| 96 | uint32_t episode_len; |
| 97 | int64_t latent_reset; |
| 98 | } pd0_domain; |
| 99 | typedef struct { |
| 100 | uint8_t state; |
| 101 | uint8_t law_id[PD0_HASH]; |
| 102 | pd0_rel rel; |
| 103 | pd0_domain dom; |
| 104 | uint32_t confidence_ppm; |
| 105 | uint32_t n_exceptions; pd0_exception exc[PD0_MAX_EXC]; |
| 106 | uint32_t n_experiments; pd0_experiment exp[PD0_MAX_EXP]; |
| 107 | uint8_t chain_root[PD0_HASH]; |
| 108 | uint32_t claim_len; uint8_t claim[PD0_MAX_CLAIM]; |
| 109 | } pd0_law; |
| 110 | size_t pd0_law_write(pd0_law *l, uint8_t *out, size_t cap); /* computes law_id */ |
| 111 | size_t pd0_dom_write(const pd0_domain *d, uint8_t *o); /* canonical bytes of the observed_domain block */ |
| 112 | int pd0_law_parse(const uint8_t *in, size_t len, pd0_law *l); /* verifies law_id */ |
| 113 | |
| 114 | /* ---- PD0EXP1 preregistration ---- */ |
| 115 | typedef struct { uint8_t channel; int64_t value; } pd0_step; |
| 116 | typedef struct { |
| 117 | uint8_t n_obs, n_hyp, n_steps; |
| 118 | int64_t reset[PD0_MAX_OBS]; |
| 119 | pd0_step steps[PD0_MAX_STEPS]; |
| 120 | int64_t expected[PD0_MAX_HYP][PD0_MAX_STEPS][PD0_MAX_OBS]; |
| 121 | int64_t divergence_micro; |
| 122 | uint8_t schedule_hash[PD0_HASH]; |
| 123 | } pd0_exp; |
| 124 | size_t pd0_exp_write(pd0_exp *e, uint8_t *out, size_t cap); /* computes schedule_hash */ |
| 125 | int pd0_exp_parse(const uint8_t *in, size_t len, pd0_exp *e, size_t *used); |
| 126 | /* schedule hash = SHA-256(reset[n_obs] i64 LE || per step: channel u8, value i64 LE) */ |
| 127 | void pd0_schedule_hash(uint8_t n_obs, const int64_t *reset, uint8_t n_steps, const pd0_step *steps, uint8_t out[PD0_HASH]); |
| 128 | |
| 129 | /* ---- PD0LEDG1 evidence ledger (verifier-owned format) ---- */ |
| 130 | enum { LEDG_OBS = 0, LEDG_TAG = 1, LEDG_CORR = 2, LEDG_RELATION = 3, LEDG_FALSIFIER = 4, LEDG_PREREG = 5, LEDG_BATCH = 6 }; |
| 131 | enum { TAG_FIT = 0, TAG_SELECT = 1, TAG_HOLDOUT = 2, TAG_TRIAL = 3, TAG_REP = 4 }; |
| 132 | enum { ORIGIN_PLANNER = 0, ORIGIN_RANDOM = 1 }; |
| 133 | enum { REL_CANDIDATE = 0, REL_RIVAL = 1, REL_NULL = 2 }; |
| 134 | typedef struct { |
| 135 | uint8_t kind; |
| 136 | const uint8_t *payload; uint32_t payload_len; |
| 137 | uint8_t prev_hash[PD0_HASH], entry_hash[PD0_HASH]; |
| 138 | } pd0_entry; |
| 139 | #define PD0_LEDG_HDR 16 |
| 140 | #define PD0_LEDG_TRAILER (2 * PD0_HASH) |
| 141 | /* append an entry to a byte buffer; chains prev_hash from last_hash (zero for first). */ |
| 142 | size_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]); |
| 143 | /* parse next entry at offset; verifies hash + chain. returns 0 or code */ |
| 144 | int pd0_ledg_next(const uint8_t *buf, size_t len, size_t *off, const uint8_t expect_prev[PD0_HASH], pd0_entry *e); |
| 145 | |
| 146 | /* payload helpers */ |
| 147 | typedef struct { uint32_t episode; uint8_t tag; uint32_t batch_id; uint8_t origin; } pd0_tag; |
| 148 | typedef struct { uint8_t var_a, var_b; uint32_t n; int64_t r_micro, p_micro; uint32_t n_pairs, n_shuffles; uint64_t shuffle_seed; } pd0_corr; |
| 149 | typedef struct { uint8_t candidate[PD0_HASH]; uint8_t n_rivals; uint8_t rival[PD0_MAX_HYP][PD0_HASH]; int64_t eps_micro; uint32_t min_trials; } pd0_falsifier; |
| 150 | typedef struct { uint32_t batch_id; uint64_t stream_seed; uint32_t n_episodes; } pd0_batch; |
| 151 | size_t pd0_tag_write(const pd0_tag *t, uint8_t *o); int pd0_tag_parse(const uint8_t *i, size_t n, pd0_tag *t); |
| 152 | size_t pd0_corr_write(const pd0_corr *c, uint8_t *o); int pd0_corr_parse(const uint8_t *i, size_t n, pd0_corr *c); |
| 153 | size_t pd0_fals_write(const pd0_falsifier *f, uint8_t *o); int pd0_fals_parse(const uint8_t *i, size_t n, pd0_falsifier *f); |
| 154 | size_t pd0_batch_write(const pd0_batch *b, uint8_t *o); int pd0_batch_parse(const uint8_t *i, size_t n, pd0_batch *b); |
| 155 | |
| 156 | /* LE helpers (exported for tests) */ |
| 157 | void pd0_put_u16(uint8_t *p, uint16_t v); void pd0_put_u32(uint8_t *p, uint32_t v); void pd0_put_u64(uint8_t *p, uint64_t v); |
| 158 | uint16_t pd0_get_u16(const uint8_t *p); uint32_t pd0_get_u32(const uint8_t *p); uint64_t pd0_get_u64(const uint8_t *p); |
| 159 | #endif |