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 / v2 / forge_substrate_v2.h
| 1 | /* forge/v2/forge_substrate_v2.h -- FORGE SUBSTRATE V2 reference model. |
| 2 | * |
| 3 | * Specification: docs/FORGE_SUBSTRATE_V2_SPEC.md |
| 4 | * Architecture decision: ARCH-0018 "Substrate-Neutral Physical Realization" |
| 5 | * (PROPOSED, aien-spark/aien-architecture docs/adr/0018-...). |
| 6 | * |
| 7 | * This is a host-only reference: canonical serializer, SHA-256 digest and |
| 8 | * strict decoder/validator. It touches no hardware and allocates no memory. |
| 9 | * |
| 10 | * Wire form of every top-level object (all integers little-endian): |
| 11 | * header : magic "FGV2" (4 bytes) | kind u16 | version u16 | body_len u32 |
| 12 | * body : TLV fields, each tag u32 | len u32 | value[len] |
| 13 | * Fields appear in ascending tag order; a repeatable field repeats its tag |
| 14 | * with strictly ascending element sort key. Each value is exactly one |
| 15 | * primitive (u32, u64, i32, i64, 32-byte digest) or one nested object. |
| 16 | * The object digest is SHA-256 over header+body. A digest is derived, never |
| 17 | * stored inside the bytes it covers. |
| 18 | * |
| 19 | * The FORGE MACHINE DESCRIPTOR V1 (208-byte GB10 record) is never |
| 20 | * re-serialized here: a V2 substrate refers to it only by its 32-byte digest |
| 21 | * (legacy_v1_descriptor_digest). |
| 22 | */ |
| 23 | #ifndef FORGE_SUBSTRATE_V2_H |
| 24 | #define FORGE_SUBSTRATE_V2_H |
| 25 | |
| 26 | #include <stddef.h> |
| 27 | #include <stdint.h> |
| 28 | |
| 29 | #define FORGE_V2_MAGIC0 'F' |
| 30 | #define FORGE_V2_MAGIC1 'G' |
| 31 | #define FORGE_V2_MAGIC2 'V' |
| 32 | #define FORGE_V2_MAGIC3 '2' |
| 33 | #define FORGE_V2_HEADER_LEN 12u |
| 34 | #define FORGE_V2_TLV_HEAD_LEN 8u |
| 35 | #define FORGE_V2_WIRE_VERSION 2u /* every V2 object carries version 2 */ |
| 36 | #define FORGE_V2_LEGACY_VERSION 1u /* refused: legacy / 32-bit generation era */ |
| 37 | #define FORGE_V2_MAX_OBJECT_LEN 65536u |
| 38 | #define FORGE_V2_MAX_SUBSTRATES 8u |
| 39 | #define FORGE_V2_DIGEST_LEN 32u |
| 40 | #define FORGE_V1_DESCRIPTOR_LEN 208u |
| 41 | |
| 42 | /* ---- object kinds (header kind field) ---- */ |
| 43 | enum { |
| 44 | FORGE_V2_KIND_MACHINE_DESCRIPTOR = 0x0001, |
| 45 | FORGE_V2_KIND_SUBSTRATE = 0x0002, |
| 46 | FORGE_V2_KIND_RESULT_CONTRACT = 0x0003, |
| 47 | FORGE_V2_KIND_NUMERICAL_PROFILE = 0x0004, |
| 48 | FORGE_V2_KIND_CALIBRATION = 0x0005, |
| 49 | FORGE_V2_KIND_EXECUTION_EVIDENCE = 0x0006, |
| 50 | FORGE_V2_KIND_CROSS_MACHINE_REF = 0x0007 |
| 51 | }; |
| 52 | |
| 53 | /* ---- errors ---- */ |
| 54 | enum { |
| 55 | FORGE_V2_OK = 0, |
| 56 | FORGE_V2_ERR_ARG = -1, |
| 57 | FORGE_V2_ERR_SPACE = -2, /* output buffer too small */ |
| 58 | FORGE_V2_ERR_TRUNCATED = -3, |
| 59 | FORGE_V2_ERR_MAGIC = -4, |
| 60 | FORGE_V2_ERR_KIND = -5, |
| 61 | FORGE_V2_ERR_VERSION = -6, /* unknown future version */ |
| 62 | FORGE_V2_ERR_LEGACY_VERSION = -7, /* version 1 object refused */ |
| 63 | FORGE_V2_ERR_LENGTH = -8, /* body_len / object size mismatch */ |
| 64 | FORGE_V2_ERR_ORDER = -9, /* tags not ascending / duplicate */ |
| 65 | FORGE_V2_ERR_UNKNOWN_TAG = -10, /* strict mode */ |
| 66 | FORGE_V2_ERR_WIDTH = -11, /* field length wrong for its type */ |
| 67 | FORGE_V2_ERR_MISSING = -12, /* required field absent */ |
| 68 | FORGE_V2_ERR_COUNT = -13, /* repeat count out of range */ |
| 69 | FORGE_V2_ERR_VALUE = -14, /* enum / range / cross-field rule */ |
| 70 | FORGE_V2_ERR_NONCANONICAL = -15, /* decode+encode does not reproduce input */ |
| 71 | FORGE_V2_ERR_GENERATION_32 = -16, /* 32-bit capability generation refused */ |
| 72 | FORGE_V2_ERR_PROVENANCE = -17, /* provenance_class missing or zero */ |
| 73 | FORGE_V2_ERR_CAL_STALE = -18, /* calibration window expired */ |
| 74 | FORGE_V2_ERR_CAL_MISSING = -19, /* substrate needs calibration, none bound */ |
| 75 | FORGE_V2_ERR_CAL_MISMATCH = -20, /* calibration names another substrate */ |
| 76 | FORGE_V2_ERR_FAULTED = -21, /* substrate faulted or unavailable */ |
| 77 | FORGE_V2_ERR_STALE_GENERATION = -22, |
| 78 | FORGE_V2_ERR_RIGHTS = -23, |
| 79 | FORGE_V2_ERR_RANGE = -24, /* offset/length outside referenced object */ |
| 80 | FORGE_V2_ERR_DUPLICATE_ID = -25, |
| 81 | FORGE_V2_ERR_OUT_OF_ENVELOPE = -26, /* conditions outside the calibration envelope */ |
| 82 | FORGE_V2_ERR_NO_DIGITAL_FALLBACK = -27 /* eligible set holds no digital realization */ |
| 83 | }; |
| 84 | |
| 85 | /* ---- substrate classes (vendor-neutral; numeric ids are frozen) ---- */ |
| 86 | enum { |
| 87 | FORGE_V2_SUBSTRATE_INVALID = 0x0000, |
| 88 | FORGE_V2_SUBSTRATE_DIGITAL_CPU = 0x0001, |
| 89 | FORGE_V2_SUBSTRATE_DIGITAL_GPU = 0x0002, |
| 90 | FORGE_V2_SUBSTRATE_ANALOG_IN_MEMORY = 0x0010, |
| 91 | FORGE_V2_SUBSTRATE_NEUROMORPHIC_SPIKING = 0x0020, |
| 92 | FORGE_V2_SUBSTRATE_FPGA_CGRA_DATAFLOW = 0x0030, |
| 93 | FORGE_V2_SUBSTRATE_OPTICAL_PHOTONIC = 0x0040, |
| 94 | FORGE_V2_SUBSTRATE_FUTURE_RESERVED_FIRST = 0x8000, |
| 95 | FORGE_V2_SUBSTRATE_FUTURE_RESERVED_LAST = 0xFFFF |
| 96 | }; |
| 97 | |
| 98 | /* ---- operation families (bit mask in a substrate, single bit in a contract) ---- */ |
| 99 | enum { |
| 100 | FORGE_V2_OP_MATVEC = 1u << 0, |
| 101 | FORGE_V2_OP_MATMUL = 1u << 1, |
| 102 | FORGE_V2_OP_CONV = 1u << 2, |
| 103 | FORGE_V2_OP_ELEMENTWISE = 1u << 3, |
| 104 | FORGE_V2_OP_REDUCTION = 1u << 4, |
| 105 | FORGE_V2_OP_SPIKE_INTEGRATE = 1u << 5, |
| 106 | FORGE_V2_OP_TRANSFORM = 1u << 6, /* FFT-like linear transforms */ |
| 107 | FORGE_V2_OP_SAMPLE = 1u << 7, /* draw from a distribution */ |
| 108 | FORGE_V2_OP_GENERAL = 1u << 8, /* general-purpose program */ |
| 109 | FORGE_V2_OP_KNOWN_MASK = 0x1FFu |
| 110 | }; |
| 111 | |
| 112 | /* ---- input / output representation ---- */ |
| 113 | enum { |
| 114 | FORGE_V2_REPR_DIGITAL_INT = 1, FORGE_V2_REPR_DIGITAL_FLOAT = 2, |
| 115 | FORGE_V2_REPR_FIXED_POINT = 3, FORGE_V2_REPR_ANALOG_VOLTAGE = 4, |
| 116 | FORGE_V2_REPR_ANALOG_CURRENT = 5, FORGE_V2_REPR_SPIKE_TRAIN = 6, |
| 117 | FORGE_V2_REPR_OPTICAL_INTENSITY = 7, FORGE_V2_REPR_OPTICAL_PHASE = 8, |
| 118 | FORGE_V2_REPR_LAST = 8 |
| 119 | }; |
| 120 | |
| 121 | enum { /* state model */ |
| 122 | FORGE_V2_STATE_STATELESS = 1, FORGE_V2_STATE_VOLATILE = 2, |
| 123 | FORGE_V2_STATE_PERSISTENT_WEIGHTS = 3, FORGE_V2_STATE_DYNAMICAL = 4, |
| 124 | FORGE_V2_STATE_LAST = 4 |
| 125 | }; |
| 126 | |
| 127 | enum { /* synchronization model */ |
| 128 | FORGE_V2_SYNC_CLOCKED = 1, FORGE_V2_SYNC_ASYNC_EVENT = 2, |
| 129 | FORGE_V2_SYNC_BATCH_BARRIER = 3, FORGE_V2_SYNC_FREE_RUNNING = 4, |
| 130 | FORGE_V2_SYNC_LAST = 4 |
| 131 | }; |
| 132 | |
| 133 | enum { /* memory model */ |
| 134 | FORGE_V2_MEM_SHARED_COHERENT = 1, FORGE_V2_MEM_DEVICE_LOCAL = 2, |
| 135 | FORGE_V2_MEM_IN_MEMORY_COMPUTE = 3, FORGE_V2_MEM_NONE = 4, |
| 136 | FORGE_V2_MEM_LAST = 4 |
| 137 | }; |
| 138 | |
| 139 | enum { /* machine memory coherency */ |
| 140 | FORGE_V2_COHERENCY_UNIFIED = 1, FORGE_V2_COHERENCY_NONCOHERENT = 2, |
| 141 | FORGE_V2_COHERENCY_DISCRETE = 3, FORGE_V2_COHERENCY_LAST = 3 |
| 142 | }; |
| 143 | |
| 144 | enum { /* interconnect topology */ |
| 145 | FORGE_V2_TOPO_SINGLE_PACKAGE = 1, FORGE_V2_TOPO_SHARED_BUS = 2, |
| 146 | FORGE_V2_TOPO_SWITCHED = 3, FORGE_V2_TOPO_MESH = 4, |
| 147 | FORGE_V2_TOPO_REMOTE = 5, FORGE_V2_TOPO_LAST = 5 |
| 148 | }; |
| 149 | |
| 150 | enum { /* transport class: cost metadata only, never semantic identity */ |
| 151 | FORGE_V2_TRANSPORT_ON_PACKAGE = 1, FORGE_V2_TRANSPORT_PCIE = 2, |
| 152 | FORGE_V2_TRANSPORT_USB = 3, FORGE_V2_TRANSPORT_ETHERNET = 4, |
| 153 | FORGE_V2_TRANSPORT_RDMA_ETHERNET = 5, FORGE_V2_TRANSPORT_BOARD_TO_BOARD = 6, |
| 154 | FORGE_V2_TRANSPORT_REMOTE_MACHINE = 7, FORGE_V2_TRANSPORT_LAST = 7 |
| 155 | }; |
| 156 | |
| 157 | enum { /* acceptable error kind */ |
| 158 | FORGE_V2_ERROR_EXACT = 1, |
| 159 | FORGE_V2_ERROR_BOUNDED_DETERMINISTIC = 2, |
| 160 | FORGE_V2_ERROR_BOUNDED_STOCHASTIC = 3, |
| 161 | FORGE_V2_ERROR_MEASURED_DISTRIBUTION = 4, |
| 162 | FORGE_V2_ERROR_LAST = 4 |
| 163 | }; |
| 164 | |
| 165 | enum { /* norm the error bound is measured in */ |
| 166 | FORGE_V2_NORM_ELEMENTWISE_MAX = 1, FORGE_V2_NORM_L2_RELATIVE = 2, |
| 167 | FORGE_V2_NORM_LAST = 2 |
| 168 | }; |
| 169 | |
| 170 | enum { /* allowed nondeterminism (mask) */ |
| 171 | FORGE_V2_NONDET_NONE = 0, |
| 172 | FORGE_V2_NONDET_REDUCTION_ORDER = 1u << 0, |
| 173 | FORGE_V2_NONDET_ROUNDING = 1u << 1, |
| 174 | FORGE_V2_NONDET_STOCHASTIC_NOISE = 1u << 2, |
| 175 | FORGE_V2_NONDET_TIMING = 1u << 3, |
| 176 | FORGE_V2_NONDET_KNOWN_MASK = 0xFu |
| 177 | }; |
| 178 | |
| 179 | enum { /* evidence capabilities (mask) */ |
| 180 | FORGE_V2_EVCAP_DURATION = 1u << 0, FORGE_V2_EVCAP_ENERGY = 1u << 1, |
| 181 | FORGE_V2_EVCAP_TEMPERATURE = 1u << 2, FORGE_V2_EVCAP_MEASURED_ERROR = 1u << 3, |
| 182 | FORGE_V2_EVCAP_REPEATS = 1u << 4, FORGE_V2_EVCAP_HW_STATUS = 1u << 5, |
| 183 | FORGE_V2_EVCAP_FAULT = 1u << 6, FORGE_V2_EVCAP_KNOWN_MASK = 0x7Fu |
| 184 | }; |
| 185 | |
| 186 | enum { /* calibration method */ |
| 187 | FORGE_V2_CAL_REFERENCE_FIXTURE_SWEEP = 1, FORGE_V2_CAL_SELF_TEST = 2, |
| 188 | FORGE_V2_CAL_FACTORY_TABLE = 3, FORGE_V2_CAL_SIMULATED_MODEL = 4, |
| 189 | FORGE_V2_CAL_LAST = 4 |
| 190 | }; |
| 191 | |
| 192 | enum { /* energy source in evidence */ |
| 193 | FORGE_V2_ENERGY_NOT_MEASURED = 0, FORGE_V2_ENERGY_MEASURED = 1, |
| 194 | FORGE_V2_ENERGY_ESTIMATED = 2, FORGE_V2_ENERGY_LAST = 2 |
| 195 | }; |
| 196 | |
| 197 | enum { /* fault state */ |
| 198 | FORGE_V2_FAULT_NONE = 0, FORGE_V2_FAULT_TRANSIENT = 1, |
| 199 | FORGE_V2_FAULT_PERSISTENT = 2, FORGE_V2_FAULT_UNAVAILABLE = 3, |
| 200 | FORGE_V2_FAULT_LAST = 3 |
| 201 | }; |
| 202 | |
| 203 | enum { /* provenance class: mandatory, zero is invalid */ |
| 204 | FORGE_V2_PROVENANCE_INVALID = 0, |
| 205 | FORGE_V2_PROVENANCE_PHYSICAL = 1, |
| 206 | FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT = 2, |
| 207 | FORGE_V2_PROVENANCE_LAST = 2 |
| 208 | }; |
| 209 | |
| 210 | enum { /* cross-machine reference target kind */ |
| 211 | FORGE_V2_TARGET_MACHINE = 1, FORGE_V2_TARGET_SUBSTRATE = 2 |
| 212 | }; |
| 213 | |
| 214 | /* Effect classes: same bit meaning as omega CQ_FX_* (rx_capq.h). */ |
| 215 | enum { |
| 216 | FORGE_V2_FX_PURE = 0x001, |
| 217 | FORGE_V2_FX_READ = 0x002, |
| 218 | FORGE_V2_FX_EXTERNAL_WRITE = 0x040, |
| 219 | FORGE_V2_FX_EXTERNAL_IRREVERSIBLE = 0x080 |
| 220 | }; |
| 221 | |
| 222 | enum { /* rights carried by a cross-machine reference (mask) */ |
| 223 | FORGE_V2_RIGHT_READ = 1u << 0, |
| 224 | FORGE_V2_RIGHT_EXECUTE_PURE = 1u << 1, |
| 225 | FORGE_V2_RIGHT_CONFIGURE_REVERSIBLE = 1u << 2, |
| 226 | FORGE_V2_RIGHT_CONFIGURE_IRREVERSIBLE = 1u << 3, |
| 227 | FORGE_V2_RIGHT_KNOWN_MASK = 0xFu |
| 228 | }; |
| 229 | |
| 230 | /* ---- objects ---- */ |
| 231 | |
| 232 | typedef struct { |
| 233 | uint32_t dynamic_range_mdb; /* dB x 1000 */ |
| 234 | uint32_t resolution_bits; |
| 235 | uint32_t repeatability_ppb; |
| 236 | uint32_t drift_ppb_per_hour; |
| 237 | uint32_t temperature_ppb_per_k; |
| 238 | uint64_t settling_ns; |
| 239 | uint64_t sample_rate_hz; |
| 240 | uint64_t calibration_validity_s; /* 0 = no calibration required */ |
| 241 | } ForgeNumericalProfile; |
| 242 | |
| 243 | typedef struct { |
| 244 | uint8_t substrate_identity[32]; |
| 245 | uint32_t substrate_class; |
| 246 | uint64_t operation_families; |
| 247 | uint32_t input_repr, input_bits; |
| 248 | uint32_t output_repr, output_bits; |
| 249 | uint32_t state_model; |
| 250 | uint32_t sync_model; |
| 251 | uint32_t memory_model; |
| 252 | uint64_t memory_capacity_bytes; /* declared nominal, not measured */ |
| 253 | ForgeNumericalProfile numerical; |
| 254 | uint32_t calibration_required; /* 0 or 1 */ |
| 255 | uint32_t calibration_method_mask; /* bit (1 << method) */ |
| 256 | uint64_t calibration_max_window_s; |
| 257 | uint32_t calibration_min_fixtures; |
| 258 | uint32_t error_kind; /* best achievable FORGE_V2_ERROR_* */ |
| 259 | uint32_t error_rel_ppb; |
| 260 | uint64_t error_abs; |
| 261 | uint32_t fault_detect_mask; |
| 262 | uint64_t timing_min_latency_ns; |
| 263 | uint64_t timing_setup_ns; |
| 264 | uint64_t timing_conversion_ns; |
| 265 | uint32_t timing_deterministic; /* 0 or 1 */ |
| 266 | uint32_t limit_max_rows; |
| 267 | uint32_t limit_max_cols; |
| 268 | uint64_t limit_max_state_bytes; |
| 269 | uint32_t limit_max_concurrent; |
| 270 | uint32_t evidence_capabilities; |
| 271 | uint8_t has_legacy_v1; /* not serialized: presence of the tag */ |
| 272 | uint8_t legacy_v1_descriptor_digest[32]; |
| 273 | } ForgeSubstrateDescriptor; |
| 274 | |
| 275 | typedef struct { |
| 276 | /* The canonical AIEN MachineId bytes (omega src/runtime/aien_machine_id.h, M20): |
| 277 | * SHA-256("AIENOS-MACHINE-ID-V1\0" || provisioned or owner-key root). KAT |
| 278 | * fixtures keep their label digests; FORGE checks only "non-zero". */ |
| 279 | uint8_t machine_identity[32]; |
| 280 | uint8_t authority_domain[32]; |
| 281 | uint64_t memory_nominal_bytes; |
| 282 | uint32_t memory_numa_domains; |
| 283 | uint32_t memory_coherency; |
| 284 | uint32_t interconnect_topology; |
| 285 | uint32_t interconnect_links; |
| 286 | uint32_t transport_class; |
| 287 | uint32_t transport_bandwidth_mbps; |
| 288 | uint32_t transport_latency_ns; |
| 289 | uint32_t substrate_count; /* 1..FORGE_V2_MAX_SUBSTRATES */ |
| 290 | ForgeSubstrateDescriptor substrates[FORGE_V2_MAX_SUBSTRATES]; |
| 291 | } ForgeMachineDescriptorV2; |
| 292 | |
| 293 | typedef struct { |
| 294 | uint32_t operation_family; /* exactly one FORGE_V2_OP_* bit */ |
| 295 | uint8_t reference_oracle_digest[32]; /* identity of the oracle that defines "correct" */ |
| 296 | uint32_t shape_rank; /* 1..4 */ |
| 297 | uint32_t shape_dim[4]; /* unused dims must be 0 */ |
| 298 | uint32_t error_kind; |
| 299 | uint32_t error_rel_ppb; |
| 300 | uint64_t error_abs; |
| 301 | uint32_t error_norm; /* FORGE_V2_NORM_* */ |
| 302 | uint32_t confidence_ppm; /* 1..1000000 */ |
| 303 | uint32_t min_samples; /* >= 1; stochastic kinds: runs needed for acceptance */ |
| 304 | uint32_t allowed_nondeterminism; |
| 305 | } ForgeSemanticResultContract; |
| 306 | |
| 307 | typedef struct { |
| 308 | uint8_t substrate_id[32]; |
| 309 | uint8_t calibration_id[32]; |
| 310 | uint32_t method; |
| 311 | uint64_t epoch_s; /* calibration time on the authority clock */ |
| 312 | uint64_t validity_window_s; /* > 0 */ |
| 313 | int32_t env_temp_min_mc; /* milli-degC */ |
| 314 | int32_t env_temp_max_mc; |
| 315 | uint32_t env_supply_min_uv; |
| 316 | uint32_t env_supply_max_uv; |
| 317 | int64_t transfer_gain_ppb; /* deviation from ideal gain */ |
| 318 | int64_t transfer_offset_lsb; |
| 319 | uint32_t transfer_linearity_ppb; |
| 320 | uint32_t noise_rms_ppb; |
| 321 | uint32_t noise_peak_ppb; |
| 322 | uint32_t noise_samples; |
| 323 | uint32_t drift_ppb_per_hour; |
| 324 | uint32_t drift_temp_ppb_per_k; |
| 325 | uint64_t drift_observed_span_s; |
| 326 | uint32_t fixture_count; |
| 327 | uint8_t fixtures_digest[32]; |
| 328 | uint8_t evidence_root[32]; |
| 329 | } ForgeCalibrationArtifact; |
| 330 | |
| 331 | typedef struct { |
| 332 | uint8_t realization_identity[32]; |
| 333 | uint8_t machine_identity[32]; |
| 334 | uint8_t substrate_identity[32]; |
| 335 | uint8_t semantic_contract_digest[32]; |
| 336 | uint8_t has_calibration; /* presence of the calibration tag */ |
| 337 | uint8_t calibration_digest[32]; |
| 338 | uint8_t input_identity[32]; |
| 339 | uint8_t output_identity[32]; |
| 340 | uint64_t execution_epoch_s; |
| 341 | uint64_t execution_duration_ns; |
| 342 | uint64_t conversion_duration_ns; |
| 343 | uint64_t energy_nj; |
| 344 | uint32_t energy_source; |
| 345 | int32_t env_temperature_mc; |
| 346 | uint32_t env_supply_uv; |
| 347 | uint32_t measured_error_rel_ppb; |
| 348 | uint64_t measured_error_abs; |
| 349 | uint32_t confidence_ppm; |
| 350 | uint32_t repeat_runs; |
| 351 | uint32_t repeat_passes; |
| 352 | uint64_t repeat_mean_ns; |
| 353 | uint64_t repeat_spread_ns; |
| 354 | uint32_t repeat_error_spread_ppb; |
| 355 | uint32_t hardware_status; |
| 356 | uint32_t fault_state; |
| 357 | uint32_t provenance_class; /* mandatory */ |
| 358 | } ForgeExecutionEvidenceV2; |
| 359 | |
| 360 | typedef struct { |
| 361 | uint8_t program_digest[32]; |
| 362 | uint8_t semantic_object_id[32]; |
| 363 | uint8_t ir_reference[32]; /* content digest of the IR bytes */ |
| 364 | uint64_t ir_size; |
| 365 | uint64_t range_offset; |
| 366 | uint64_t range_length; |
| 367 | uint32_t target_kind; /* FORGE_V2_TARGET_* */ |
| 368 | uint8_t target_digest[32]; |
| 369 | uint64_t generation; /* full 64-bit capability generation */ |
| 370 | uint64_t rights; |
| 371 | } ForgeCrossMachineRef; |
| 372 | |
| 373 | /* ---- API ---- */ |
| 374 | |
| 375 | /* SHA-256 of bytes, standard 32-byte big-endian digest form. */ |
| 376 | void forge_v2_sha256(const uint8_t *data, size_t len, uint8_t out[32]); |
| 377 | |
| 378 | /* Encode: writes canonical bytes into buf (cap bytes); *out_len = size. |
| 379 | * Refuses objects that fail validation. */ |
| 380 | int forge_v2_encode_machine(const ForgeMachineDescriptorV2 *m, uint8_t *buf, size_t cap, size_t *out_len); |
| 381 | int forge_v2_encode_substrate(const ForgeSubstrateDescriptor *s, uint8_t *buf, size_t cap, size_t *out_len); |
| 382 | int forge_v2_encode_contract(const ForgeSemanticResultContract *c, uint8_t *buf, size_t cap, size_t *out_len); |
| 383 | int forge_v2_encode_profile(const ForgeNumericalProfile *p, uint8_t *buf, size_t cap, size_t *out_len); |
| 384 | int forge_v2_encode_calibration(const ForgeCalibrationArtifact *a, uint8_t *buf, size_t cap, size_t *out_len); |
| 385 | int forge_v2_encode_evidence(const ForgeExecutionEvidenceV2 *e, uint8_t *buf, size_t cap, size_t *out_len); |
| 386 | int forge_v2_encode_ref(const ForgeCrossMachineRef *r, uint8_t *buf, size_t cap, size_t *out_len); |
| 387 | |
| 388 | /* Decode + validate. strict != 0: unknown tags refused and the input must |
| 389 | * be byte-identical to the canonical re-encoding. strict == 0: unknown tags |
| 390 | * (still in ascending order) are skipped; everything else is enforced. */ |
| 391 | int forge_v2_decode_machine(const uint8_t *buf, size_t len, int strict, ForgeMachineDescriptorV2 *m); |
| 392 | int forge_v2_decode_substrate(const uint8_t *buf, size_t len, int strict, ForgeSubstrateDescriptor *s); |
| 393 | int forge_v2_decode_contract(const uint8_t *buf, size_t len, int strict, ForgeSemanticResultContract *c); |
| 394 | int forge_v2_decode_profile(const uint8_t *buf, size_t len, int strict, ForgeNumericalProfile *p); |
| 395 | int forge_v2_decode_calibration(const uint8_t *buf, size_t len, int strict, ForgeCalibrationArtifact *a); |
| 396 | int forge_v2_decode_evidence(const uint8_t *buf, size_t len, int strict, ForgeExecutionEvidenceV2 *e); |
| 397 | int forge_v2_decode_ref(const uint8_t *buf, size_t len, int strict, ForgeCrossMachineRef *r); |
| 398 | |
| 399 | /* Digest of the canonical encoding (encode + SHA-256). */ |
| 400 | int forge_v2_digest_machine(const ForgeMachineDescriptorV2 *m, uint8_t out[32]); |
| 401 | int forge_v2_digest_substrate(const ForgeSubstrateDescriptor *s, uint8_t out[32]); |
| 402 | int forge_v2_digest_contract(const ForgeSemanticResultContract *c, uint8_t out[32]); |
| 403 | int forge_v2_digest_calibration(const ForgeCalibrationArtifact *a, uint8_t out[32]); |
| 404 | int forge_v2_digest_evidence(const ForgeExecutionEvidenceV2 *e, uint8_t out[32]); |
| 405 | int forge_v2_digest_ref(const ForgeCrossMachineRef *r, uint8_t out[32]); |
| 406 | |
| 407 | /* Realization identity. calibration_digest may be NULL only when the |
| 408 | * substrate needs no calibration. Domain-separated SHA-256 over |
| 409 | * "FORGE-V2-REALIZE" | contract | machine_identity | substrate descriptor |
| 410 | * digest | code | has_cal | cal. machine_identity (not the machine |
| 411 | * descriptor digest) is used so transport metadata never moves identity. */ |
| 412 | void forge_v2_realization_identity(const uint8_t contract_digest[32], |
| 413 | const uint8_t machine_identity[32], |
| 414 | const uint8_t substrate_digest[32], |
| 415 | const uint8_t code_digest[32], |
| 416 | const uint8_t *calibration_digest, |
| 417 | uint8_t out[32]); |
| 418 | |
| 419 | /* Measured operating conditions at execution time (evidence, not identity). */ |
| 420 | typedef struct { |
| 421 | uint64_t execution_epoch_s; |
| 422 | int32_t temperature_mc; /* milli-degC */ |
| 423 | uint32_t supply_uv; |
| 424 | } ForgeV2Conditions; |
| 425 | |
| 426 | /* Calibration freshness: FORGE_V2_OK when the artifact names this substrate, |
| 427 | * satisfies its requirements, epoch_s <= now < epoch_s + window (window |
| 428 | * capped by the substrate), and the conditions lie inside the calibrated |
| 429 | * envelope. Stale => FORGE_V2_ERR_CAL_STALE; outside envelope => |
| 430 | * FORGE_V2_ERR_OUT_OF_ENVELOPE. Both mean "not eligible", never "degraded". */ |
| 431 | int forge_v2_calibration_check(const ForgeSubstrateDescriptor *s, |
| 432 | const ForgeCalibrationArtifact *a, |
| 433 | const ForgeV2Conditions *now); |
| 434 | |
| 435 | /* Eligibility of one substrate for one contract (the check AEGIS runs). |
| 436 | * cal may be NULL when the substrate needs no calibration. */ |
| 437 | int forge_v2_substrate_eligible(const ForgeSubstrateDescriptor *s, |
| 438 | const ForgeSemanticResultContract *c, |
| 439 | const ForgeCalibrationArtifact *cal, |
| 440 | uint32_t fault_state, |
| 441 | const ForgeV2Conditions *now); |
| 442 | |
| 443 | /* One eligible realization candidate, on any Machine of the Fabric. */ |
| 444 | typedef struct { |
| 445 | uint8_t contract_digest[32]; /* SemanticResultContract digest it satisfies */ |
| 446 | uint8_t machine_identity[32]; /* Machine that hosts the substrate */ |
| 447 | uint8_t substrate_digest[32]; |
| 448 | uint32_t substrate_class; |
| 449 | } ForgeV2Candidate; |
| 450 | |
| 451 | /* Digital fallback rule. Returns 1 when some candidate in the eligible set |
| 452 | * (on any Machine) is on a DIGITAL_CPU or DIGITAL_GPU substrate and carries |
| 453 | * the same contract digest; 0 otherwise (also for NULL or empty input). |
| 454 | * Pure. A single Machine need not hold a digital substrate. */ |
| 455 | int forge_v2_eligible_set_has_digital_fallback(const ForgeV2Candidate *set, size_t n, |
| 456 | const uint8_t contract_digest[32]); |
| 457 | |
| 458 | /* Admission of a realization plan for one contract: FORGE_V2_OK when the |
| 459 | * eligible set holds a digital fallback for that contract, otherwise |
| 460 | * FORGE_V2_ERR_NO_DIGITAL_FALLBACK (AEGIS refuses the plan). */ |
| 461 | int forge_v2_admit_plan(const ForgeV2Candidate *set, size_t n, const uint8_t contract_digest[32]); |
| 462 | |
| 463 | /* Authority for a use of a cross-machine reference. effect_class is one |
| 464 | * FORGE_V2_FX_* value. Hardware-state-changing effects need the matching |
| 465 | * CONFIGURE right and a generation equal to current_generation. */ |
| 466 | int forge_v2_validate_capability_ref(const ForgeCrossMachineRef *r, uint32_t effect_class, |
| 467 | uint64_t current_generation); |
| 468 | |
| 469 | const char *forge_v2_strerror(int err); |
| 470 | |
| 471 | #endif |