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.c
| 1 | /* |
| 2 | * forge/form/forge_form.c -- FORGE REALIZATION FORM V1 reference: canonical |
| 3 | * encoding, digests, contract mapping, two lowerings (digital reference, |
| 4 | * simulated analog) and realization with V2 evidence. Host-only: no |
| 5 | * hardware, no heap, no I/O. See forge_form.h. |
| 6 | */ |
| 7 | #include "forge_form.h" |
| 8 | |
| 9 | #include <string.h> |
| 10 | |
| 11 | /* ------------------------------------------------------------------ */ |
| 12 | /* primitives */ |
| 13 | /* ------------------------------------------------------------------ */ |
| 14 | |
| 15 | static void put_u16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); } |
| 16 | static void put_u32(uint8_t *p, uint32_t v) |
| 17 | { |
| 18 | p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24); |
| 19 | } |
| 20 | static void put_u64(uint8_t *p, uint64_t v) { put_u32(p, (uint32_t)v); put_u32(p + 4, (uint32_t)(v >> 32)); } |
| 21 | static uint16_t get_u16(const uint8_t *p) { return (uint16_t)(p[0] | (p[1] << 8)); } |
| 22 | static uint32_t get_u32(const uint8_t *p) |
| 23 | { |
| 24 | return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); |
| 25 | } |
| 26 | static uint64_t sat_add(uint64_t a, uint64_t b) { return a > UINT64_MAX - b ? UINT64_MAX : a + b; } |
| 27 | static uint64_t mul_div_ceil(uint64_t a, uint64_t b, uint64_t d) |
| 28 | { |
| 29 | unsigned __int128 p = (unsigned __int128)a * b; |
| 30 | unsigned __int128 q = (p + d - 1) / d; |
| 31 | return q > UINT64_MAX ? UINT64_MAX : (uint64_t)q; |
| 32 | } |
| 33 | static void label_digest(const char *label, uint8_t out[32]) |
| 34 | { |
| 35 | forge_v2_sha256((const uint8_t *)label, strlen(label), out); |
| 36 | } |
| 37 | |
| 38 | /* ------------------------------------------------------------------ */ |
| 39 | /* schema: every field required, fixed width, ascending tags */ |
| 40 | /* ------------------------------------------------------------------ */ |
| 41 | |
| 42 | enum { T_U32 = 1, T_D32 = 2 }; |
| 43 | typedef struct { uint32_t tag; uint8_t type; size_t off; } Field; |
| 44 | |
| 45 | #define FORM_NFIELDS 5u |
| 46 | static const Field FORM_FIELDS[FORM_NFIELDS] = { |
| 47 | { 1, T_U32, offsetof(ForgeRealizationForm, form_kind) }, |
| 48 | { 2, T_U32, offsetof(ForgeRealizationForm, scalar_domain) }, |
| 49 | { 3, T_U32, offsetof(ForgeRealizationForm, operator_rows) }, |
| 50 | { 4, T_U32, offsetof(ForgeRealizationForm, operator_cols) }, |
| 51 | { 5, T_U32, offsetof(ForgeRealizationForm, accumulation) }, |
| 52 | }; |
| 53 | |
| 54 | #define RECORD_NFIELDS 8u |
| 55 | static const Field RECORD_FIELDS[RECORD_NFIELDS] = { |
| 56 | { 1, T_D32, offsetof(ForgeFormRealizationRecord, form_digest) }, |
| 57 | { 2, T_D32, offsetof(ForgeFormRealizationRecord, contract_digest) }, |
| 58 | { 3, T_D32, offsetof(ForgeFormRealizationRecord, substrate_digest) }, |
| 59 | { 4, T_D32, offsetof(ForgeFormRealizationRecord, lowering_digest) }, |
| 60 | { 5, T_D32, offsetof(ForgeFormRealizationRecord, realization_identity) }, |
| 61 | { 6, T_D32, offsetof(ForgeFormRealizationRecord, evidence_digest) }, |
| 62 | { 7, T_U32, offsetof(ForgeFormRealizationRecord, provenance_class) }, |
| 63 | { 8, T_U32, offsetof(ForgeFormRealizationRecord, decision) }, |
| 64 | }; |
| 65 | |
| 66 | static size_t field_width(uint8_t type) { return type == T_U32 ? 4u : 32u; } |
| 67 | |
| 68 | static int is_zero32(const uint8_t *p) |
| 69 | { |
| 70 | uint8_t acc = 0; |
| 71 | for (size_t i = 0; i < 32; i++) acc |= p[i]; |
| 72 | return acc == 0; |
| 73 | } |
| 74 | |
| 75 | /* ---- validation ---- */ |
| 76 | |
| 77 | int forge_form_validate(const ForgeRealizationForm *f) |
| 78 | { |
| 79 | if (!f) return FORGE_FORM_ERR_ARG; |
| 80 | switch (f->form_kind) { |
| 81 | case FORGE_FORM_LINEAR_OPERATOR: break; |
| 82 | case FORGE_FORM_ENERGY_FUNCTIONAL: |
| 83 | case FORGE_FORM_STOCHASTIC_DISTRIBUTION: return FORGE_FORM_ERR_UNSUPPORTED_FORM; |
| 84 | default: return FORGE_FORM_ERR_VALUE; |
| 85 | } |
| 86 | if (f->scalar_domain != FORGE_FORM_DOMAIN_INT_WRAP_U64 && f->scalar_domain != FORGE_FORM_DOMAIN_INT_WRAP_U32) |
| 87 | return FORGE_FORM_ERR_VALUE; |
| 88 | if (f->operator_rows == 0 || f->operator_rows > FORGE_FORM_MAX_DIM) return FORGE_FORM_ERR_VALUE; |
| 89 | if (f->operator_cols == 0 || f->operator_cols > FORGE_FORM_MAX_DIM) return FORGE_FORM_ERR_VALUE; |
| 90 | if (f->accumulation != FORGE_FORM_ACCUM_EXACT_MODULAR) return FORGE_FORM_ERR_VALUE; |
| 91 | return FORGE_FORM_OK; |
| 92 | } |
| 93 | |
| 94 | static int record_validate(const ForgeFormRealizationRecord *r) |
| 95 | { |
| 96 | if (!r) return FORGE_FORM_ERR_ARG; |
| 97 | if (is_zero32(r->form_digest) || is_zero32(r->contract_digest) || is_zero32(r->substrate_digest) || |
| 98 | is_zero32(r->lowering_digest) || is_zero32(r->realization_identity) || is_zero32(r->evidence_digest)) |
| 99 | return FORGE_FORM_ERR_VALUE; |
| 100 | if (r->provenance_class != FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT) return FORGE_FORM_ERR_VALUE; |
| 101 | if (r->decision != FORGE_FORM_DECISION_PUBLISHED && r->decision != FORGE_FORM_DECISION_REFUSED) |
| 102 | return FORGE_FORM_ERR_VALUE; |
| 103 | return FORGE_FORM_OK; |
| 104 | } |
| 105 | |
| 106 | /* ---- generic encode / decode ---- */ |
| 107 | |
| 108 | static int enc_obj(uint16_t kind, const Field *fields, size_t nf, const void *obj, uint8_t *buf, size_t cap, |
| 109 | size_t *out_len) |
| 110 | { |
| 111 | size_t n = FORGE_FORM_HEADER_LEN; |
| 112 | const uint8_t *base = (const uint8_t *)obj; |
| 113 | if (!buf || !out_len) return FORGE_FORM_ERR_ARG; |
| 114 | for (size_t i = 0; i < nf; i++) { |
| 115 | size_t w = field_width(fields[i].type); |
| 116 | if (n + FORGE_FORM_TLV_HEAD_LEN + w > cap) return FORGE_FORM_ERR_SPACE; |
| 117 | put_u32(buf + n, fields[i].tag); |
| 118 | put_u32(buf + n + 4, (uint32_t)w); |
| 119 | if (fields[i].type == T_U32) { |
| 120 | uint32_t v; |
| 121 | memcpy(&v, base + fields[i].off, sizeof v); |
| 122 | put_u32(buf + n + 8, v); |
| 123 | } else { |
| 124 | memcpy(buf + n + 8, base + fields[i].off, 32); |
| 125 | } |
| 126 | n += FORGE_FORM_TLV_HEAD_LEN + w; |
| 127 | } |
| 128 | if (cap < FORGE_FORM_HEADER_LEN) return FORGE_FORM_ERR_SPACE; |
| 129 | buf[0] = FORGE_FORM_MAGIC0; buf[1] = FORGE_FORM_MAGIC1; buf[2] = FORGE_FORM_MAGIC2; buf[3] = FORGE_FORM_MAGIC3; |
| 130 | put_u16(buf + 4, kind); |
| 131 | put_u16(buf + 6, FORGE_FORM_WIRE_VERSION); |
| 132 | put_u32(buf + 8, (uint32_t)(n - FORGE_FORM_HEADER_LEN)); |
| 133 | *out_len = n; |
| 134 | return FORGE_FORM_OK; |
| 135 | } |
| 136 | |
| 137 | static int dec_obj(uint16_t kind, const Field *fields, size_t nf, const uint8_t *buf, size_t len, int strict, |
| 138 | void *obj, size_t obj_size) |
| 139 | { |
| 140 | uint8_t *base = (uint8_t *)obj; |
| 141 | uint32_t prev_tag = 0; |
| 142 | uint8_t present[16] = { 0 }; |
| 143 | size_t n = FORGE_FORM_HEADER_LEN; |
| 144 | if (!buf || !obj) return FORGE_FORM_ERR_ARG; |
| 145 | memset(obj, 0, obj_size); |
| 146 | if (len < FORGE_FORM_HEADER_LEN) return FORGE_FORM_ERR_TRUNCATED; |
| 147 | if (len > FORGE_FORM_MAX_OBJECT_LEN) return FORGE_FORM_ERR_LENGTH; |
| 148 | if (buf[0] != FORGE_FORM_MAGIC0 || buf[1] != FORGE_FORM_MAGIC1 || buf[2] != FORGE_FORM_MAGIC2 || |
| 149 | buf[3] != FORGE_FORM_MAGIC3) |
| 150 | return FORGE_FORM_ERR_MAGIC; |
| 151 | if (get_u16(buf + 4) != kind) return FORGE_FORM_ERR_KIND; |
| 152 | if (get_u16(buf + 6) != FORGE_FORM_WIRE_VERSION) return FORGE_FORM_ERR_VERSION; |
| 153 | if ((size_t)get_u32(buf + 8) != len - FORGE_FORM_HEADER_LEN) return FORGE_FORM_ERR_LENGTH; |
| 154 | while (n < len) { |
| 155 | uint32_t tag, flen; |
| 156 | size_t i; |
| 157 | if (len - n < FORGE_FORM_TLV_HEAD_LEN) return FORGE_FORM_ERR_TRUNCATED; |
| 158 | tag = get_u32(buf + n); |
| 159 | flen = get_u32(buf + n + 4); |
| 160 | if (tag <= prev_tag) return FORGE_FORM_ERR_ORDER; /* duplicate or descending */ |
| 161 | prev_tag = tag; |
| 162 | n += FORGE_FORM_TLV_HEAD_LEN; |
| 163 | if (len - n < flen) return FORGE_FORM_ERR_TRUNCATED; |
| 164 | for (i = 0; i < nf; i++) if (fields[i].tag == tag) break; |
| 165 | if (i == nf) { |
| 166 | if (strict) return FORGE_FORM_ERR_UNKNOWN_TAG; |
| 167 | n += flen; |
| 168 | continue; |
| 169 | } |
| 170 | if (flen != field_width(fields[i].type)) return FORGE_FORM_ERR_WIDTH; |
| 171 | if (fields[i].type == T_U32) { |
| 172 | uint32_t v = get_u32(buf + n); |
| 173 | memcpy(base + fields[i].off, &v, sizeof v); |
| 174 | } else { |
| 175 | memcpy(base + fields[i].off, buf + n, 32); |
| 176 | } |
| 177 | present[i] = 1; |
| 178 | n += flen; |
| 179 | } |
| 180 | for (size_t i = 0; i < nf; i++) if (!present[i]) return FORGE_FORM_ERR_MISSING; |
| 181 | return FORGE_FORM_OK; |
| 182 | } |
| 183 | |
| 184 | static int dec_public(uint16_t kind, const Field *fields, size_t nf, const uint8_t *buf, size_t len, int strict, |
| 185 | void *obj, size_t obj_size, int (*validate)(const void *)) |
| 186 | { |
| 187 | int rc = dec_obj(kind, fields, nf, buf, len, strict, obj, obj_size); |
| 188 | if (rc != FORGE_FORM_OK) { if (obj) memset(obj, 0, obj_size); return rc; } |
| 189 | rc = validate(obj); |
| 190 | if (rc != FORGE_FORM_OK) { memset(obj, 0, obj_size); return rc; } |
| 191 | if (strict) { |
| 192 | uint8_t again[FORGE_FORM_MAX_OBJECT_LEN]; |
| 193 | size_t m = 0; |
| 194 | rc = enc_obj(kind, fields, nf, obj, again, sizeof again, &m); |
| 195 | if (rc != FORGE_FORM_OK || m != len || memcmp(again, buf, len) != 0) { |
| 196 | memset(obj, 0, obj_size); |
| 197 | return FORGE_FORM_ERR_NONCANONICAL; |
| 198 | } |
| 199 | } |
| 200 | return FORGE_FORM_OK; |
| 201 | } |
| 202 | |
| 203 | static int validate_form_v(const void *o) { return forge_form_validate((const ForgeRealizationForm *)o); } |
| 204 | static int validate_record_v(const void *o) { return record_validate((const ForgeFormRealizationRecord *)o); } |
| 205 | |
| 206 | int forge_form_encode(const ForgeRealizationForm *f, uint8_t *buf, size_t cap, size_t *out_len) |
| 207 | { |
| 208 | int rc = forge_form_validate(f); |
| 209 | if (rc != FORGE_FORM_OK) return rc; |
| 210 | return enc_obj(FORGE_FORM_KIND_FORM, FORM_FIELDS, FORM_NFIELDS, f, buf, cap, out_len); |
| 211 | } |
| 212 | |
| 213 | int forge_form_decode(const uint8_t *buf, size_t len, int strict, ForgeRealizationForm *f) |
| 214 | { |
| 215 | return dec_public(FORGE_FORM_KIND_FORM, FORM_FIELDS, FORM_NFIELDS, buf, len, strict, f, sizeof *f, |
| 216 | validate_form_v); |
| 217 | } |
| 218 | |
| 219 | int forge_form_digest(const ForgeRealizationForm *f, uint8_t out[32]) |
| 220 | { |
| 221 | uint8_t buf[256]; |
| 222 | size_t n = 0; |
| 223 | int rc = forge_form_encode(f, buf, sizeof buf, &n); |
| 224 | if (rc != FORGE_FORM_OK) return rc; |
| 225 | forge_v2_sha256(buf, n, out); |
| 226 | return FORGE_FORM_OK; |
| 227 | } |
| 228 | |
| 229 | int forge_form_encode_record(const ForgeFormRealizationRecord *r, uint8_t *buf, size_t cap, size_t *out_len) |
| 230 | { |
| 231 | int rc = record_validate(r); |
| 232 | if (rc != FORGE_FORM_OK) return rc; |
| 233 | return enc_obj(FORGE_FORM_KIND_REALIZATION_RECORD, RECORD_FIELDS, RECORD_NFIELDS, r, buf, cap, out_len); |
| 234 | } |
| 235 | |
| 236 | int forge_form_decode_record(const uint8_t *buf, size_t len, int strict, ForgeFormRealizationRecord *r) |
| 237 | { |
| 238 | return dec_public(FORGE_FORM_KIND_REALIZATION_RECORD, RECORD_FIELDS, RECORD_NFIELDS, buf, len, strict, r, |
| 239 | sizeof *r, validate_record_v); |
| 240 | } |
| 241 | |
| 242 | int forge_form_digest_record(const ForgeFormRealizationRecord *r, uint8_t out[32]) |
| 243 | { |
| 244 | uint8_t buf[512]; |
| 245 | size_t n = 0; |
| 246 | int rc = forge_form_encode_record(r, buf, sizeof buf, &n); |
| 247 | if (rc != FORGE_FORM_OK) return rc; |
| 248 | forge_v2_sha256(buf, n, out); |
| 249 | return FORGE_FORM_OK; |
| 250 | } |
| 251 | |
| 252 | void forge_form_lowering_digest(const uint8_t form_digest[32], const uint8_t code_digest[32], uint8_t out[32]) |
| 253 | { |
| 254 | static const char domain[19] = { 'F','O','R','G','E','-','F','O','R','M','-','L','O','W','E','R','-','V','1' }; |
| 255 | uint8_t in[19 + 64]; |
| 256 | memcpy(in, domain, 19); |
| 257 | memcpy(in + 19, form_digest, 32); |
| 258 | memcpy(in + 51, code_digest, 32); |
| 259 | forge_v2_sha256(in, sizeof in, out); |
| 260 | } |
| 261 | |
| 262 | /* ------------------------------------------------------------------ */ |
| 263 | /* contract mapping */ |
| 264 | /* ------------------------------------------------------------------ */ |
| 265 | |
| 266 | static uint32_t oracle_of(const uint8_t digest[32]) |
| 267 | { |
| 268 | uint8_t d[32]; |
| 269 | if (forge_asim_oracle_digest(FORGE_ASIM_ORACLE_EXACT_U64, d) == FORGE_V2_OK && memcmp(d, digest, 32) == 0) |
| 270 | return FORGE_ASIM_ORACLE_EXACT_U64; |
| 271 | if (forge_asim_oracle_digest(FORGE_ASIM_ORACLE_I32, d) == FORGE_V2_OK && memcmp(d, digest, 32) == 0) |
| 272 | return FORGE_ASIM_ORACLE_I32; |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | int forge_form_from_contract(const ForgeSemanticResultContract *c, ForgeRealizationForm *out) |
| 277 | { |
| 278 | uint32_t oracle; |
| 279 | if (!c || !out) return FORGE_FORM_ERR_ARG; |
| 280 | memset(out, 0, sizeof *out); |
| 281 | if (c->operation_family != FORGE_V2_OP_MATVEC) return FORGE_FORM_ERR_CONTRACT; |
| 282 | if (c->shape_rank != 2 || c->shape_dim[0] == 0 || c->shape_dim[1] == 0 || c->shape_dim[2] || c->shape_dim[3]) |
| 283 | return FORGE_FORM_ERR_CONTRACT; |
| 284 | oracle = oracle_of(c->reference_oracle_digest); |
| 285 | if (!oracle) return FORGE_FORM_ERR_CONTRACT; |
| 286 | out->form_kind = FORGE_FORM_LINEAR_OPERATOR; |
| 287 | out->scalar_domain = oracle == FORGE_ASIM_ORACLE_EXACT_U64 ? FORGE_FORM_DOMAIN_INT_WRAP_U64 |
| 288 | : FORGE_FORM_DOMAIN_INT_WRAP_U32; |
| 289 | out->operator_rows = c->shape_dim[0]; |
| 290 | out->operator_cols = c->shape_dim[1]; |
| 291 | out->accumulation = FORGE_FORM_ACCUM_EXACT_MODULAR; |
| 292 | return forge_form_validate(out); |
| 293 | } |
| 294 | |
| 295 | /* ------------------------------------------------------------------ */ |
| 296 | /* lowerings */ |
| 297 | /* ------------------------------------------------------------------ */ |
| 298 | |
| 299 | static const char *const DIGITAL_CODE_LABEL = "FORGE-FORM/lowering/linear-operator/digital-reference/v1"; |
| 300 | static const char *const ANALOG_CODE_LABEL = "FORGE-FORM/lowering/linear-operator/analog-sim/v1"; |
| 301 | |
| 302 | static int finish_lowering(const ForgeRealizationForm *f, ForgeFormLowering *out) |
| 303 | { |
| 304 | if (forge_form_digest(f, out->form_digest) != FORGE_FORM_OK) return FORGE_FORM_ERR_ARG; |
| 305 | if (forge_v2_digest_substrate(&out->substrate, out->substrate_digest) != FORGE_V2_OK) |
| 306 | return FORGE_FORM_ERR_EVIDENCE; |
| 307 | forge_form_lowering_digest(out->form_digest, out->code_digest, out->lowering_digest); |
| 308 | return FORGE_FORM_OK; |
| 309 | } |
| 310 | |
| 311 | int forge_form_lower_digital(const ForgeRealizationForm *f, ForgeFormLowering *out) |
| 312 | { |
| 313 | int rc; |
| 314 | if (!out) return FORGE_FORM_ERR_ARG; |
| 315 | memset(out, 0, sizeof *out); |
| 316 | if ((rc = forge_form_validate(f)) != FORGE_FORM_OK) return rc; |
| 317 | out->backend = FORGE_FORM_BACKEND_DIGITAL_REFERENCE; |
| 318 | forge_asim_digital_descriptor(&out->substrate); |
| 319 | label_digest(DIGITAL_CODE_LABEL, out->code_digest); |
| 320 | return finish_lowering(f, out); |
| 321 | } |
| 322 | |
| 323 | int forge_form_lower_analog_sim(const ForgeRealizationForm *f, const ForgeAnalogSimModel *declared, |
| 324 | const ForgeAnalogSimModel *actual, const ForgeCalibrationArtifact *cal, |
| 325 | ForgeFormLowering *out) |
| 326 | { |
| 327 | int rc; |
| 328 | if (!out) return FORGE_FORM_ERR_ARG; |
| 329 | memset(out, 0, sizeof *out); |
| 330 | if (!declared || !actual || !cal) return FORGE_FORM_ERR_ARG; |
| 331 | if ((rc = forge_form_validate(f)) != FORGE_FORM_OK) return rc; |
| 332 | out->backend = FORGE_FORM_BACKEND_ANALOG_SIM; |
| 333 | out->declared = *declared; |
| 334 | out->actual = *actual; |
| 335 | out->cal = *cal; |
| 336 | out->has_calibration = 1; |
| 337 | if (forge_asim_describe(declared, cal, &out->substrate) != FORGE_V2_OK) return FORGE_FORM_ERR_ARG; |
| 338 | if (forge_v2_digest_calibration(cal, out->cal_digest) != FORGE_V2_OK) return FORGE_FORM_ERR_ARG; |
| 339 | label_digest(ANALOG_CODE_LABEL, out->code_digest); |
| 340 | return finish_lowering(f, out); |
| 341 | } |
| 342 | |
| 343 | /* ------------------------------------------------------------------ */ |
| 344 | /* admission and realization */ |
| 345 | /* ------------------------------------------------------------------ */ |
| 346 | |
| 347 | uint32_t forge_form_provenance(void) { return FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT; } |
| 348 | |
| 349 | static int eligible(const ForgeFormLowering *lw, const ForgeSemanticResultContract *c, const ForgeV2Conditions *now) |
| 350 | { |
| 351 | return forge_v2_substrate_eligible(&lw->substrate, c, lw->has_calibration ? &lw->cal : NULL, |
| 352 | FORGE_V2_FAULT_NONE, now); |
| 353 | } |
| 354 | |
| 355 | int forge_form_admit(const ForgeFormLowering *const *lw, size_t n, const ForgeSemanticResultContract *c, |
| 356 | const ForgeFormInput *in, size_t *n_eligible) |
| 357 | { |
| 358 | ForgeV2Candidate set[8]; |
| 359 | uint8_t contract_digest[32]; |
| 360 | size_t nset = 0; |
| 361 | if (!lw || !c || !in || n > 8) return FORGE_FORM_ERR_ARG; |
| 362 | if (forge_v2_digest_contract(c, contract_digest) != FORGE_V2_OK) return FORGE_FORM_ERR_CONTRACT; |
| 363 | for (size_t i = 0; i < n; i++) { |
| 364 | if (!lw[i]) return FORGE_FORM_ERR_ARG; |
| 365 | if (eligible(lw[i], c, &in->now) != FORGE_V2_OK) continue; |
| 366 | memcpy(set[nset].contract_digest, contract_digest, 32); |
| 367 | memcpy(set[nset].machine_identity, in->machine_identity, 32); |
| 368 | memcpy(set[nset].substrate_digest, lw[i]->substrate_digest, 32); |
| 369 | set[nset].substrate_class = lw[i]->substrate.substrate_class; |
| 370 | nset++; |
| 371 | } |
| 372 | if (n_eligible) *n_eligible = nset; |
| 373 | return forge_v2_admit_plan(set, nset, contract_digest) == FORGE_V2_OK ? FORGE_FORM_OK |
| 374 | : FORGE_FORM_ERR_NO_FALLBACK; |
| 375 | } |
| 376 | |
| 377 | static size_t put_u64_at(uint8_t *p, uint64_t v) { put_u64(p, v); return 8; } |
| 378 | |
| 379 | static void vec_digest(const char *domain, const uint64_t *v, size_t n, uint8_t out[32]) |
| 380 | { |
| 381 | /* SHA-256(domain | n (u64) | v[0..n) ) via a fixed stack buffer */ |
| 382 | uint8_t buf[64 + 8 + 8 * FORGE_ASIM_MAX_COLS]; |
| 383 | size_t k = strlen(domain); |
| 384 | memcpy(buf, domain, k); |
| 385 | k += put_u64_at(buf + k, (uint64_t)n); |
| 386 | for (size_t i = 0; i < n; i++) k += put_u64_at(buf + k, v[i]); |
| 387 | forge_v2_sha256(buf, k, out); |
| 388 | } |
| 389 | |
| 390 | static void input_digest(const uint64_t *A, const uint64_t *x, uint32_t M, uint32_t N, uint8_t out[32]) |
| 391 | { |
| 392 | uint8_t da[32], dx[32], in[19 + 8 + 64]; |
| 393 | /* A is hashed row by row to stay inside the fixed buffer */ |
| 394 | uint8_t rows[32 * FORGE_ASIM_MAX_ROWS]; |
| 395 | for (uint32_t i = 0; i < M; i++) vec_digest("FORGE-FORM/input/row/v1", A + (size_t)i * N, N, rows + 32 * i); |
| 396 | forge_v2_sha256(rows, 32 * (size_t)M, da); |
| 397 | vec_digest("FORGE-FORM/input/x/v1", x, N, dx); |
| 398 | memcpy(in, "FORGE-FORM/input/v1", 19); |
| 399 | put_u32(in + 19, M); put_u32(in + 23, N); |
| 400 | memcpy(in + 27, da, 32); memcpy(in + 59, dx, 32); |
| 401 | forge_v2_sha256(in, 91, out); |
| 402 | } |
| 403 | |
| 404 | static int model_ideal(const ForgeAnalogSimModel *m) |
| 405 | { |
| 406 | return m->quant_step_lsb <= 1 && m->bias_lsb == 0 && m->noise_peak_lsb == 0 && m->drift_lsb_per_hour == 0 && |
| 407 | m->temp_lsb_per_k == 0 && m->saturation_max == 0; |
| 408 | } |
| 409 | |
| 410 | static int shape_ok(const ForgeFormInput *in) |
| 411 | { |
| 412 | if (!in->A || !in->x || in->M == 0 || in->N == 0 || in->M > FORGE_ASIM_MAX_ROWS || in->N > FORGE_ASIM_MAX_COLS) |
| 413 | return 0; |
| 414 | for (size_t k = 0; k < (size_t)in->M * in->N; k++) if (in->A[k] >> FORGE_ASIM_INPUT_BITS) return 0; |
| 415 | for (uint32_t j = 0; j < in->N; j++) if (in->x[j] >> FORGE_ASIM_INPUT_BITS) return 0; |
| 416 | return 1; |
| 417 | } |
| 418 | |
| 419 | static void compute_ref(uint32_t oracle, const ForgeFormInput *in, uint64_t *ref) |
| 420 | { |
| 421 | if (oracle == FORGE_ASIM_ORACLE_EXACT_U64) { |
| 422 | forge_asim_oracle_exact_u64(in->A, in->x, ref, in->M, in->N); |
| 423 | } else { |
| 424 | uint32_t a32[FORGE_ASIM_MAX_ROWS * FORGE_ASIM_MAX_COLS], x32[FORGE_ASIM_MAX_COLS], y32[FORGE_ASIM_MAX_ROWS]; |
| 425 | for (size_t k = 0; k < (size_t)in->M * in->N; k++) a32[k] = (uint32_t)in->A[k]; |
| 426 | for (uint32_t j = 0; j < in->N; j++) x32[j] = (uint32_t)in->x[j]; |
| 427 | forge_asim_oracle_i32(a32, x32, y32, in->M, in->N); |
| 428 | for (uint32_t i = 0; i < in->M; i++) ref[i] = y32[i]; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /* One run of the lowering. Never consults the oracle. */ |
| 433 | static int run_once(const ForgeFormLowering *lw, const ForgeFormInput *in, uint32_t r, uint32_t oracle, |
| 434 | uint64_t *y, uint64_t *latency_ns) |
| 435 | { |
| 436 | if (lw->backend == FORGE_FORM_BACKEND_DIGITAL_REFERENCE) { |
| 437 | compute_ref(oracle, in, y); /* the digital reference IS the oracle's arithmetic */ |
| 438 | *latency_ns = 0; |
| 439 | return FORGE_FORM_OK; |
| 440 | } |
| 441 | if (lw->backend == FORGE_FORM_BACKEND_ANALOG_SIM) { |
| 442 | uint64_t cal_age = in->now.execution_epoch_s - lw->cal.epoch_s; /* eligible => not negative */ |
| 443 | int32_t mid = (int32_t)(((int64_t)lw->cal.env_temp_min_mc + lw->cal.env_temp_max_mc) / 2); |
| 444 | int32_t dtemp = in->now.temperature_mc - mid; |
| 445 | int rc = forge_asim_realize(&lw->actual, lw->wrong_row, lw->wrong_delta, in->A, in->x, in->M, in->N, r, |
| 446 | cal_age, dtemp, y, latency_ns); |
| 447 | return rc == FORGE_ASIM_PUBLISHED_ANALOG ? FORGE_FORM_OK : FORGE_FORM_ERR_INPUT_RANGE; |
| 448 | } |
| 449 | return FORGE_FORM_ERR_ARG; |
| 450 | } |
| 451 | |
| 452 | static int seal(ForgeFormOutcome *out, const ForgeFormLowering *lw, const uint8_t contract_digest[32], |
| 453 | uint32_t decision) |
| 454 | { |
| 455 | uint8_t buf[4096]; |
| 456 | size_t n; |
| 457 | ForgeExecutionEvidenceV2 *e = &out->evidence; |
| 458 | ForgeFormRealizationRecord *rec = &out->record; |
| 459 | e->provenance_class = forge_form_provenance(); |
| 460 | e->energy_source = FORGE_V2_ENERGY_NOT_MEASURED; |
| 461 | e->energy_nj = 0; |
| 462 | if (forge_v2_encode_evidence(e, buf, sizeof buf, &n) != FORGE_V2_OK) return FORGE_FORM_ERR_EVIDENCE; |
| 463 | forge_v2_sha256(buf, n, out->evidence_digest); |
| 464 | memset(rec, 0, sizeof *rec); |
| 465 | memcpy(rec->form_digest, lw->form_digest, 32); |
| 466 | memcpy(rec->contract_digest, contract_digest, 32); |
| 467 | memcpy(rec->substrate_digest, lw->substrate_digest, 32); |
| 468 | memcpy(rec->lowering_digest, lw->lowering_digest, 32); |
| 469 | memcpy(rec->realization_identity, e->realization_identity, 32); |
| 470 | memcpy(rec->evidence_digest, out->evidence_digest, 32); |
| 471 | rec->provenance_class = forge_form_provenance(); |
| 472 | rec->decision = decision; |
| 473 | if (forge_form_digest_record(rec, out->record_digest) != FORGE_FORM_OK) return FORGE_FORM_ERR_EVIDENCE; |
| 474 | return FORGE_FORM_OK; |
| 475 | } |
| 476 | |
| 477 | int forge_form_realize(const ForgeRealizationForm *f, const ForgeSemanticResultContract *c, |
| 478 | const ForgeFormLowering *lw, const ForgeFormInput *in, ForgeFormOutcome *out) |
| 479 | { |
| 480 | ForgeRealizationForm derived; |
| 481 | uint8_t contract_digest[32], form_digest[32], input_id[32], ref_id[32]; |
| 482 | uint64_t ref[FORGE_ASIM_MAX_ROWS], first[FORGE_ASIM_MAX_ROWS], run_y[FORGE_ASIM_MAX_ROWS], |
| 483 | chosen[FORGE_ASIM_MAX_ROWS]; |
| 484 | uint64_t worst = 0, worst_rel = 0, lat = 0, lat_total = 0; |
| 485 | uint32_t oracle, runs, within_runs = 0, have_chosen = 0, differs = 0, exact_bad = 0; |
| 486 | int rc; |
| 487 | |
| 488 | if (!out) return FORGE_FORM_ERR_ARG; |
| 489 | memset(out, 0, sizeof *out); |
| 490 | out->status = FORGE_FORM_ERR_ARG; |
| 491 | if (!f || !c || !lw || !in) return out->status; |
| 492 | if (forge_form_validate(f) != FORGE_FORM_OK) return out->status; |
| 493 | /* the form must be exactly the one this contract maps to */ |
| 494 | if (forge_form_from_contract(c, &derived) != FORGE_FORM_OK || memcmp(&derived, f, sizeof derived) != 0) |
| 495 | return out->status = FORGE_FORM_ERR_CONTRACT; |
| 496 | if (forge_form_digest(f, form_digest) != FORGE_FORM_OK || memcmp(form_digest, lw->form_digest, 32) != 0) |
| 497 | return out->status = FORGE_FORM_ERR_CONTRACT; |
| 498 | if (in->M != f->operator_rows || in->N != f->operator_cols) return out->status = FORGE_FORM_ERR_CONTRACT; |
| 499 | oracle = oracle_of(c->reference_oracle_digest); |
| 500 | runs = c->min_samples; |
| 501 | if (!oracle || runs == 0 || runs > FORGE_ASIM_MAX_SAMPLES) return out->status; |
| 502 | if (!shape_ok(in)) return out->status = FORGE_FORM_ERR_INPUT_RANGE; |
| 503 | if (forge_v2_digest_contract(c, contract_digest) != FORGE_V2_OK) return out->status = FORGE_FORM_ERR_CONTRACT; |
| 504 | |
| 505 | out->eligibility_rc = eligible(lw, c, &in->now); |
| 506 | if (out->eligibility_rc != FORGE_V2_OK) { |
| 507 | out->refuse_reason = FORGE_ASIM_REFUSE_INELIGIBLE; |
| 508 | return out->status = FORGE_FORM_REFUSED; |
| 509 | } |
| 510 | if (c->error_norm != FORGE_V2_NORM_ELEMENTWISE_MAX || c->error_kind == FORGE_V2_ERROR_MEASURED_DISTRIBUTION) { |
| 511 | out->refuse_reason = FORGE_ASIM_REFUSE_UNSUPPORTED; |
| 512 | return out->status = FORGE_FORM_REFUSED; |
| 513 | } |
| 514 | |
| 515 | compute_ref(oracle, in, ref); |
| 516 | input_digest(in->A, in->x, in->M, in->N, input_id); |
| 517 | vec_digest("FORGE-FORM/output/linear-operator/v1", ref, in->M, ref_id); |
| 518 | |
| 519 | for (uint32_t r = 0; r < runs; r++) { |
| 520 | rc = run_once(lw, in, r, oracle, run_y, &lat); |
| 521 | if (rc != FORGE_FORM_OK) return out->status = rc; |
| 522 | lat_total = sat_add(lat_total, lat); |
| 523 | if (r == 0) memcpy(first, run_y, sizeof first); |
| 524 | else if (memcmp(first, run_y, (size_t)in->M * sizeof run_y[0]) != 0) differs = 1; |
| 525 | if (c->error_kind == FORGE_V2_ERROR_EXACT) { |
| 526 | uint8_t od[32]; |
| 527 | vec_digest("FORGE-FORM/output/linear-operator/v1", run_y, in->M, od); |
| 528 | if (memcmp(od, ref_id, 32) != 0) exact_bad = 1; |
| 529 | } |
| 530 | uint32_t ok = 1; |
| 531 | for (uint32_t i = 0; i < in->M; i++) { |
| 532 | uint64_t err = run_y[i] > ref[i] ? run_y[i] - ref[i] : ref[i] - run_y[i]; |
| 533 | uint64_t allow = sat_add(c->error_abs, (uint64_t)c->error_rel_ppb * ref[i] / 1000000000ull); |
| 534 | if (c->error_kind == FORGE_V2_ERROR_EXACT) allow = 0; |
| 535 | if (err > allow) ok = 0; |
| 536 | if (err > worst) worst = err; |
| 537 | uint64_t rel = ref[i] ? mul_div_ceil(err, 1000000000ull, ref[i]) : (err ? UINT32_MAX : 0); |
| 538 | if (rel > worst_rel) worst_rel = rel; |
| 539 | } |
| 540 | if (ok) { |
| 541 | within_runs++; |
| 542 | if (!have_chosen) { memcpy(chosen, run_y, sizeof chosen); have_chosen = 1; } |
| 543 | } |
| 544 | } |
| 545 | out->ran = 1; |
| 546 | out->runs = runs; |
| 547 | out->runs_within_bound = within_runs; |
| 548 | out->error_abs = worst; |
| 549 | |
| 550 | if (c->error_kind == FORGE_V2_ERROR_EXACT && |
| 551 | (exact_bad || (lw->backend == FORGE_FORM_BACKEND_ANALOG_SIM && (!model_ideal(&lw->actual) || lw->wrong_row)))) |
| 552 | out->refuse_reason = FORGE_ASIM_REFUSE_EXACT_MISMATCH; |
| 553 | else if (c->error_kind != FORGE_V2_ERROR_BOUNDED_STOCHASTIC && differs) |
| 554 | out->refuse_reason = FORGE_ASIM_REFUSE_NONDETERMINISM; |
| 555 | else if (c->error_kind != FORGE_V2_ERROR_BOUNDED_STOCHASTIC && within_runs != runs) |
| 556 | out->refuse_reason = FORGE_ASIM_REFUSE_ERROR_BOUND; |
| 557 | else if (c->error_kind == FORGE_V2_ERROR_BOUNDED_STOCHASTIC && |
| 558 | (uint64_t)within_runs * 1000000ull < (uint64_t)c->confidence_ppm * runs) |
| 559 | out->refuse_reason = FORGE_ASIM_REFUSE_CONFIDENCE; |
| 560 | |
| 561 | /* evidence, published or refused; the form digest enters only via the lowering digest */ |
| 562 | ForgeExecutionEvidenceV2 *e = &out->evidence; |
| 563 | forge_v2_realization_identity(contract_digest, in->machine_identity, lw->substrate_digest, lw->lowering_digest, |
| 564 | lw->has_calibration ? lw->cal_digest : NULL, e->realization_identity); |
| 565 | memcpy(e->machine_identity, in->machine_identity, 32); |
| 566 | memcpy(e->substrate_identity, lw->substrate.substrate_identity, 32); |
| 567 | memcpy(e->semantic_contract_digest, contract_digest, 32); |
| 568 | e->has_calibration = lw->has_calibration; |
| 569 | if (lw->has_calibration) memcpy(e->calibration_digest, lw->cal_digest, 32); |
| 570 | memcpy(e->input_identity, input_id, 32); |
| 571 | vec_digest("FORGE-FORM/output/linear-operator/v1", have_chosen ? chosen : first, in->M, e->output_identity); |
| 572 | e->execution_epoch_s = in->now.execution_epoch_s; |
| 573 | e->execution_duration_ns = lat_total; |
| 574 | e->conversion_duration_ns = lat; |
| 575 | e->env_temperature_mc = in->now.temperature_mc; |
| 576 | e->env_supply_uv = in->now.supply_uv; |
| 577 | e->measured_error_abs = worst; /* V2 field name; here a simulated / host error vs the oracle */ |
| 578 | e->measured_error_rel_ppb = worst_rel > UINT32_MAX ? UINT32_MAX : (uint32_t)worst_rel; |
| 579 | e->confidence_ppm = (uint32_t)((uint64_t)within_runs * 1000000ull / runs); |
| 580 | e->repeat_runs = runs; |
| 581 | e->repeat_passes = within_runs; |
| 582 | e->repeat_mean_ns = runs ? lat_total / runs : 0; |
| 583 | e->fault_state = FORGE_V2_FAULT_NONE; |
| 584 | |
| 585 | if (out->refuse_reason == FORGE_ASIM_REFUSE_NONE) { |
| 586 | if ((rc = seal(out, lw, contract_digest, FORGE_FORM_DECISION_PUBLISHED)) != FORGE_FORM_OK) |
| 587 | return out->status = rc; |
| 588 | memcpy(out->y, chosen, (size_t)in->M * sizeof chosen[0]); |
| 589 | out->published = 1; |
| 590 | return out->status = FORGE_FORM_PUBLISHED; |
| 591 | } |
| 592 | if ((rc = seal(out, lw, contract_digest, FORGE_FORM_DECISION_REFUSED)) != FORGE_FORM_OK) |
| 593 | return out->status = rc; |
| 594 | return out->status = FORGE_FORM_REFUSED; |
| 595 | } |
| 596 | |
| 597 | const char *forge_form_strerror(int err) |
| 598 | { |
| 599 | switch (err) { |
| 600 | case FORGE_FORM_OK: return "ok"; |
| 601 | case FORGE_FORM_REFUSED: return "refused"; |
| 602 | case FORGE_FORM_ERR_ARG: return "bad argument"; |
| 603 | case FORGE_FORM_ERR_SPACE: return "output buffer too small"; |
| 604 | case FORGE_FORM_ERR_TRUNCATED: return "truncated"; |
| 605 | case FORGE_FORM_ERR_MAGIC: return "bad magic"; |
| 606 | case FORGE_FORM_ERR_KIND: return "wrong object kind"; |
| 607 | case FORGE_FORM_ERR_VERSION: return "unsupported wire version"; |
| 608 | case FORGE_FORM_ERR_LENGTH: return "body length mismatch"; |
| 609 | case FORGE_FORM_ERR_ORDER: return "tags not strictly ascending"; |
| 610 | case FORGE_FORM_ERR_UNKNOWN_TAG: return "unknown tag (strict)"; |
| 611 | case FORGE_FORM_ERR_WIDTH: return "field width wrong"; |
| 612 | case FORGE_FORM_ERR_MISSING: return "required field missing"; |
| 613 | case FORGE_FORM_ERR_VALUE: return "value out of range"; |
| 614 | case FORGE_FORM_ERR_NONCANONICAL: return "non-canonical encoding"; |
| 615 | case FORGE_FORM_ERR_UNSUPPORTED_FORM: return "form kind specified but not realizable here"; |
| 616 | case FORGE_FORM_ERR_CONTRACT: return "contract does not map to this form"; |
| 617 | case FORGE_FORM_ERR_NO_FALLBACK: return "eligible set holds no digital realization"; |
| 618 | case FORGE_FORM_ERR_EVIDENCE: return "evidence or record failed validation"; |
| 619 | case FORGE_FORM_ERR_INPUT_RANGE: return "input outside lowering limits"; |
| 620 | default: return "unknown"; |
| 621 | } |
| 622 | } |