physics-mv
FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.
Brought by drakestapleton
physics-mv / forge / form / forge_form.h
| 1 | /* |
| 2 | * forge/form/forge_form.h -- FORGE REALIZATION FORM V1 (host-only reference). |
| 3 | * |
| 4 | * Architecture: extends ARCH-0018 (substrate-neutral physical realization) |
| 5 | * and ARCH-0019 (mixed-algebra realization); proposed as ADR 0034 |
| 6 | * "Physical-Form Realization IR" (PROPOSED, not accepted). |
| 7 | * |
| 8 | * A realization form is a REALIZATION-SIDE artifact: the mathematical |
| 9 | * structure that one or more physical substrates can embody. It sits below |
| 10 | * an Omega semantic operation and its SemanticResultContract, and beside |
| 11 | * the substrate-specific lowering. It is not an Omega program, not a new |
| 12 | * layer, not an authority, and it carries no substrate, vendor, device, |
| 13 | * calibration, address, clock or temperature field. |
| 14 | * |
| 15 | * Omega semantic operation + SemanticResultContract (semantic identity) |
| 16 | * | |
| 17 | * v forge_form_from_contract |
| 18 | * ForgeRealizationForm (form digest) |
| 19 | * | |
| 20 | * +-------------+--------------+ |
| 21 | * v v forge_form_lower_* |
| 22 | * digital reference lowering simulated analog lowering (lowering digest |
| 23 | * | | = H(form, code)) |
| 24 | * v v forge_form_realize |
| 25 | * V2 realization identity V2 realization identity (realization identity |
| 26 | * V2 evidence + record V2 evidence + record and evidence identity) |
| 27 | * |
| 28 | * Identity rules (the point of this module): |
| 29 | * - The form digest NEVER enters the semantic contract digest. |
| 30 | * - The form digest enters realization identity only through the |
| 31 | * code_digest slot of forge_v2_realization_identity (FORGE V2 bytes and |
| 32 | * digests are untouched). |
| 33 | * - Every realization of one form under one contract carries the SAME |
| 34 | * semantic_contract_digest and DIFFERENT realization identities. |
| 35 | * |
| 36 | * Provenance: every evidence record and realization record produced here is |
| 37 | * SIMULATED_DEVELOPMENT (host fixture; no provisioned Machine measured |
| 38 | * anything). There is no API to label a result PHYSICAL. |
| 39 | * |
| 40 | * Wire form (same discipline as FORGE V2, separate magic so the two |
| 41 | * families never collide): |
| 42 | * header : magic "FGFM" | kind u16 | version u16 (=1) | body_len u32 |
| 43 | * body : TLV fields, tag u32 | len u32 | value[len], strictly ascending |
| 44 | * tags, every field required, fixed widths, little-endian. |
| 45 | * digest : SHA-256 over header+body. |
| 46 | * |
| 47 | * Integers only, no heap, no hardware, no I/O. |
| 48 | */ |
| 49 | #ifndef FORGE_FORM_H |
| 50 | #define FORGE_FORM_H |
| 51 | |
| 52 | #include <stddef.h> |
| 53 | #include <stdint.h> |
| 54 | |
| 55 | #include "forge_substrate_v2.h" |
| 56 | #include "forge_analog_sim.h" |
| 57 | |
| 58 | #define FORGE_FORM_MAGIC0 'F' |
| 59 | #define FORGE_FORM_MAGIC1 'G' |
| 60 | #define FORGE_FORM_MAGIC2 'F' |
| 61 | #define FORGE_FORM_MAGIC3 'M' |
| 62 | #define FORGE_FORM_HEADER_LEN 12u |
| 63 | #define FORGE_FORM_TLV_HEAD_LEN 8u |
| 64 | #define FORGE_FORM_WIRE_VERSION 1u |
| 65 | #define FORGE_FORM_MAX_OBJECT_LEN 4096u |
| 66 | #define FORGE_FORM_MAX_DIM 65536u |
| 67 | |
| 68 | /* ---- object kinds ---- */ |
| 69 | enum { |
| 70 | FORGE_FORM_KIND_FORM = 0x0001, |
| 71 | FORGE_FORM_KIND_REALIZATION_RECORD = 0x0002 |
| 72 | }; |
| 73 | |
| 74 | /* ---- form kinds (numeric ids frozen; only LINEAR_OPERATOR is realizable) ---- */ |
| 75 | enum { |
| 76 | FORGE_FORM_INVALID = 0, |
| 77 | FORGE_FORM_LINEAR_OPERATOR = 1, /* y = A x over a declared scalar domain */ |
| 78 | FORGE_FORM_ENERGY_FUNCTIONAL = 2, /* SPECIFIED in ADR 0034, NOT IMPLEMENTED: decode refuses */ |
| 79 | FORGE_FORM_STOCHASTIC_DISTRIBUTION = 3 /* RESERVED, NOT IMPLEMENTED: decode refuses */ |
| 80 | }; |
| 81 | |
| 82 | /* ---- scalar domains (ARCH-0019: the algebraic domain a realization computes in) ---- */ |
| 83 | enum { |
| 84 | FORGE_FORM_DOMAIN_INVALID = 0, |
| 85 | FORGE_FORM_DOMAIN_INT_WRAP_U64 = 1, /* integers mod 2^64, wrapping (omega_matvec_reference) */ |
| 86 | FORGE_FORM_DOMAIN_INT_WRAP_U32 = 2 /* integers mod 2^32, wrapping (omega_matmul_cpu_oracle_i32, n = 1) */ |
| 87 | }; |
| 88 | |
| 89 | /* ---- accumulation semantics ---- */ |
| 90 | enum { |
| 91 | FORGE_FORM_ACCUM_INVALID = 0, |
| 92 | FORGE_FORM_ACCUM_EXACT_MODULAR = 1 /* exact modular sum; order-free in a wrapping integer ring */ |
| 93 | }; |
| 94 | |
| 95 | /* ---- lowering backends (realization side; NOT a form field) ---- */ |
| 96 | enum { |
| 97 | FORGE_FORM_BACKEND_DIGITAL_REFERENCE = 1, |
| 98 | FORGE_FORM_BACKEND_ANALOG_SIM = 2 |
| 99 | }; |
| 100 | |
| 101 | /* ---- realization record decision ---- */ |
| 102 | enum { |
| 103 | FORGE_FORM_DECISION_PUBLISHED = 1, |
| 104 | FORGE_FORM_DECISION_REFUSED = 2 |
| 105 | }; |
| 106 | |
| 107 | /* ---- errors / statuses ---- */ |
| 108 | enum { |
| 109 | FORGE_FORM_OK = 0, |
| 110 | FORGE_FORM_PUBLISHED = 0, /* forge_form_realize: result satisfied the contract */ |
| 111 | FORGE_FORM_REFUSED = 1, /* forge_form_realize: nothing published, evidence kept */ |
| 112 | FORGE_FORM_ERR_ARG = -1, |
| 113 | FORGE_FORM_ERR_SPACE = -2, |
| 114 | FORGE_FORM_ERR_TRUNCATED = -3, |
| 115 | FORGE_FORM_ERR_MAGIC = -4, |
| 116 | FORGE_FORM_ERR_KIND = -5, |
| 117 | FORGE_FORM_ERR_VERSION = -6, |
| 118 | FORGE_FORM_ERR_LENGTH = -7, |
| 119 | FORGE_FORM_ERR_ORDER = -8, |
| 120 | FORGE_FORM_ERR_UNKNOWN_TAG = -9, |
| 121 | FORGE_FORM_ERR_WIDTH = -10, |
| 122 | FORGE_FORM_ERR_MISSING = -11, |
| 123 | FORGE_FORM_ERR_VALUE = -12, |
| 124 | FORGE_FORM_ERR_NONCANONICAL = -13, |
| 125 | FORGE_FORM_ERR_UNSUPPORTED_FORM = -14, /* specified or reserved form kind with no realization here */ |
| 126 | FORGE_FORM_ERR_CONTRACT = -15, /* contract cannot be mapped to / does not match the form */ |
| 127 | FORGE_FORM_ERR_NO_FALLBACK = -16, /* eligible set holds no digital realization */ |
| 128 | FORGE_FORM_ERR_EVIDENCE = -17, /* evidence or record failed validation */ |
| 129 | FORGE_FORM_ERR_INPUT_RANGE = -18 /* inputs outside what the lowering can take */ |
| 130 | }; |
| 131 | |
| 132 | /* ---- objects ---- */ |
| 133 | |
| 134 | /* The form: mathematical structure only. */ |
| 135 | typedef struct { |
| 136 | uint32_t form_kind; /* FORGE_FORM_LINEAR_OPERATOR */ |
| 137 | uint32_t scalar_domain; /* FORGE_FORM_DOMAIN_* */ |
| 138 | uint32_t operator_rows; /* M, 1..FORGE_FORM_MAX_DIM */ |
| 139 | uint32_t operator_cols; /* N, 1..FORGE_FORM_MAX_DIM */ |
| 140 | uint32_t accumulation; /* FORGE_FORM_ACCUM_* */ |
| 141 | } ForgeRealizationForm; |
| 142 | |
| 143 | /* Evidence-side binding of one realization of one form under one contract. */ |
| 144 | typedef struct { |
| 145 | uint8_t form_digest[32]; |
| 146 | uint8_t contract_digest[32]; |
| 147 | uint8_t substrate_digest[32]; |
| 148 | uint8_t lowering_digest[32]; |
| 149 | uint8_t realization_identity[32]; |
| 150 | uint8_t evidence_digest[32]; |
| 151 | uint32_t provenance_class; /* always SIMULATED_DEVELOPMENT here */ |
| 152 | uint32_t decision; /* FORGE_FORM_DECISION_* */ |
| 153 | } ForgeFormRealizationRecord; |
| 154 | |
| 155 | /* A lowering: one way to embody the form on one substrate. */ |
| 156 | typedef struct { |
| 157 | uint32_t backend; /* FORGE_FORM_BACKEND_* */ |
| 158 | ForgeSubstrateDescriptor substrate; |
| 159 | uint8_t substrate_digest[32]; |
| 160 | uint8_t code_digest[32]; /* identity of the lowering code (label digest) */ |
| 161 | uint8_t form_digest[32]; |
| 162 | uint8_t lowering_digest[32]; /* H("FORGE-FORM-LOWER-V1" | form_digest | code_digest) */ |
| 163 | uint8_t has_calibration; |
| 164 | ForgeCalibrationArtifact cal; |
| 165 | uint8_t cal_digest[32]; |
| 166 | /* analog-sim backend only */ |
| 167 | ForgeAnalogSimModel declared, actual; |
| 168 | uint32_t wrong_row; |
| 169 | int64_t wrong_delta; |
| 170 | } ForgeFormLowering; |
| 171 | |
| 172 | typedef struct { |
| 173 | const uint64_t *A; /* M x N row-major */ |
| 174 | const uint64_t *x; /* N */ |
| 175 | uint32_t M, N; /* must equal the form's rows / cols */ |
| 176 | uint8_t machine_identity[32]; |
| 177 | ForgeV2Conditions now; |
| 178 | } ForgeFormInput; |
| 179 | |
| 180 | typedef struct { |
| 181 | int status; /* FORGE_FORM_PUBLISHED / FORGE_FORM_REFUSED / < 0 */ |
| 182 | uint32_t refuse_reason; /* FORGE_ASIM_REFUSE_* vocabulary */ |
| 183 | int eligibility_rc; |
| 184 | uint32_t ran, runs, runs_within_bound; |
| 185 | uint64_t error_abs; /* worst element error vs the contract's oracle */ |
| 186 | uint32_t published; |
| 187 | uint64_t y[FORGE_ASIM_MAX_ROWS]; /* zeroed unless published */ |
| 188 | ForgeExecutionEvidenceV2 evidence; /* valid when ran */ |
| 189 | uint8_t evidence_digest[32]; |
| 190 | ForgeFormRealizationRecord record; /* valid when ran */ |
| 191 | uint8_t record_digest[32]; |
| 192 | } ForgeFormOutcome; |
| 193 | |
| 194 | /* ---- encoding / identity ---- */ |
| 195 | int forge_form_encode(const ForgeRealizationForm *f, uint8_t *buf, size_t cap, size_t *out_len); |
| 196 | int forge_form_decode(const uint8_t *buf, size_t len, int strict, ForgeRealizationForm *f); |
| 197 | int forge_form_digest(const ForgeRealizationForm *f, uint8_t out[32]); |
| 198 | int forge_form_validate(const ForgeRealizationForm *f); |
| 199 | |
| 200 | int forge_form_encode_record(const ForgeFormRealizationRecord *r, uint8_t *buf, size_t cap, size_t *out_len); |
| 201 | int forge_form_decode_record(const uint8_t *buf, size_t len, int strict, ForgeFormRealizationRecord *r); |
| 202 | int forge_form_digest_record(const ForgeFormRealizationRecord *r, uint8_t out[32]); |
| 203 | |
| 204 | /* Lowering digest: SHA-256("FORGE-FORM-LOWER-V1" | form_digest | code_digest). */ |
| 205 | void forge_form_lowering_digest(const uint8_t form_digest[32], const uint8_t code_digest[32], uint8_t out[32]); |
| 206 | |
| 207 | /* ---- contract mapping (Omega / V2 contract -> form) ---- */ |
| 208 | /* MATVEC, rank 2, known oracle -> LINEAR_OPERATOR; anything else FORGE_FORM_ERR_CONTRACT. */ |
| 209 | int forge_form_from_contract(const ForgeSemanticResultContract *c, ForgeRealizationForm *out); |
| 210 | |
| 211 | /* ---- lowerings ---- */ |
| 212 | int forge_form_lower_digital(const ForgeRealizationForm *f, ForgeFormLowering *out); |
| 213 | int forge_form_lower_analog_sim(const ForgeRealizationForm *f, const ForgeAnalogSimModel *declared, |
| 214 | const ForgeAnalogSimModel *actual, const ForgeCalibrationArtifact *cal, |
| 215 | ForgeFormLowering *out); |
| 216 | |
| 217 | /* ---- admission and realization ---- */ |
| 218 | /* Eligible set over the given lowerings; FORGE_FORM_OK when it holds a |
| 219 | * digital realization of this contract (forge_v2_admit_plan), otherwise |
| 220 | * FORGE_FORM_ERR_NO_FALLBACK. *n_eligible receives the eligible count. */ |
| 221 | int forge_form_admit(const ForgeFormLowering *const *lw, size_t n, const ForgeSemanticResultContract *c, |
| 222 | const ForgeFormInput *in, size_t *n_eligible); |
| 223 | |
| 224 | /* Realize the form under the contract through one lowering: eligibility, |
| 225 | * contract.min_samples runs, check against the oracle the contract names, |
| 226 | * V2 evidence + realization record sealed whether published or refused. */ |
| 227 | int forge_form_realize(const ForgeRealizationForm *f, const ForgeSemanticResultContract *c, |
| 228 | const ForgeFormLowering *lw, const ForgeFormInput *in, ForgeFormOutcome *out); |
| 229 | |
| 230 | /* Fixed provenance of everything this module seals. */ |
| 231 | uint32_t forge_form_provenance(void); |
| 232 | |
| 233 | const char *forge_form_strerror(int err); |
| 234 | |
| 235 | #endif |