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 / tests / test_forge_v2_kat.c
| 1 | /* tests/test_forge_v2_kat.c -- FORGE SUBSTRATE V2 host-only known-answer test. |
| 2 | * |
| 3 | * No hardware, no driver, no GPU. Links forge/v2/forge_substrate_v2.c and |
| 4 | * the pinned root sha256_clean.c only. |
| 5 | * |
| 6 | * test_forge_v2_kat run every check, print PASS/FAIL, exit 0 on all-pass |
| 7 | * test_forge_v2_kat --dump-vectors print docs/forge-v2-golden-vectors.md to stdout |
| 8 | * |
| 9 | * Every vector here is a synthetic test fixture. None of them is a receipt of |
| 10 | * a physical run, and no analog / neuromorphic / optical device exists on |
| 11 | * this host. |
| 12 | */ |
| 13 | #include "forge_substrate_v2.h" |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 | |
| 18 | /* ---- FORGE MACHINE DESCRIPTOR V1 KAT stream (208 bytes) ---- |
| 19 | * The v1 serialization KAT reference record (forge_descriptor.c KAT values, |
| 20 | * physics PR #13), as its canonical byte stream. V2 never re-serializes v1 |
| 21 | * fields: it only hashes this literal and carries the digest. */ |
| 22 | static const uint8_t V1_KAT_STREAM[208] = { |
| 23 | 0xde,0x10,0x12,0x2e,0xde,0x10,0x00,0x00,0x30,0x30,0x30,0x66,0x3a,0x30,0x31,0x3a, |
| 24 | 0x30,0x30,0x2e,0x30,0x00,0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08, |
| 25 | 0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0xc0,0xce,0x00,0x00,0x04,0x0a,0x00,0x00, |
| 26 | 0x20,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x02,0x00,0x00,0x00, |
| 27 | 0x35,0x38,0x30,0x2e,0x31,0x37,0x33,0x2e,0x30,0x32,0x00,0x00,0x00,0x00,0x00,0x00, |
| 28 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 29 | 0x39,0x61,0x2e,0x30,0x62,0x2e,0x32,0x64,0x2e,0x30,0x30,0x2e,0x30,0x30,0x00,0x00, |
| 30 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 31 | 0x35,0x38,0x30,0x2e,0x31,0x37,0x33,0x2e,0x30,0x32,0x00,0x00,0x00,0x00,0x00,0x00, |
| 32 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 33 | 0x00,0xf0,0xfe,0x6b,0x1e,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00, |
| 34 | 0x00,0x10,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0xc0,0x00,0x00,0x1f,0x00,0x00,0x00, |
| 35 | 0xff,0xff,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 36 | }; |
| 37 | _Static_assert(sizeof V1_KAT_STREAM == FORGE_V1_DESCRIPTOR_LEN, "v1 KAT stream must be 208 bytes"); |
| 38 | |
| 39 | static const char V1_KAT_DIGEST[] = "10d63d05f888eaba4fe473c5f17febe22f462f543ae0c2b005a9a8e5417e09fd"; |
| 40 | |
| 41 | /* ---- frozen V2 digests (machine-produced by --dump-vectors, then frozen) ---- */ |
| 42 | static const char FROZEN_MACHINE[] = "89cb5ba667a75ab2871a6ba0d2f812c84d95c376cbfdf3b9b987ecacda1106c0"; |
| 43 | static const char FROZEN_CONTRACT[] = "b32a3577b720087e5877997824123f1177cb7a4e72d31da92313f3e650335421"; |
| 44 | static const char FROZEN_CALIBRATION[] = "f7fd8996c6818081ee918681bf9cf9a0be3e1c23fde8ac522eeb55b3a8565413"; |
| 45 | static const char FROZEN_EVIDENCE[] = "e1fbcb958b1e7060ba7ee2d366c3688a00bcf6e27f3f9ca71fb8d73c88f150f9"; |
| 46 | static const char FROZEN_REF[] = "a20384da09d1f46090e553651ba81d1355533d3becbe59c1a798f1321da4b7b5"; |
| 47 | static const char FROZEN_ANALOG_MACHINE[] = "a1c00a5f917889455972a204ec7e83c44cce3ea2042e06efeb87981c67d88d2c"; |
| 48 | |
| 49 | #define KAT_EPOCH 1000000u /* calibration epoch of the fixture, seconds */ |
| 50 | #define KAT_WINDOW 3600u /* one hour validity */ |
| 51 | #define KAT_GENERATION 0x0000000500000007ull |
| 52 | /* measured conditions at time t: inside the fixture envelope */ |
| 53 | #define AT(t) (&(ForgeV2Conditions){ (t), 25000, 800000 }) |
| 54 | |
| 55 | static int g_pass, g_fail; |
| 56 | |
| 57 | static void check(const char *name, int ok) |
| 58 | { |
| 59 | printf("%s %s\n", ok ? "PASS" : "FAIL", name); |
| 60 | if (ok) g_pass++; else g_fail++; |
| 61 | } |
| 62 | |
| 63 | static void to_hex(const uint8_t *p, size_t n, char *out) |
| 64 | { |
| 65 | static const char hx[] = "0123456789abcdef"; |
| 66 | for (size_t i = 0; i < n; i++) { out[2 * i] = hx[p[i] >> 4]; out[2 * i + 1] = hx[p[i] & 15]; } |
| 67 | out[2 * n] = 0; |
| 68 | } |
| 69 | |
| 70 | static int digest_is(const uint8_t d[32], const char *hex) |
| 71 | { |
| 72 | char h[65]; |
| 73 | to_hex(d, 32, h); |
| 74 | return strcmp(h, hex) == 0; |
| 75 | } |
| 76 | |
| 77 | /* label -> 32-byte fixture identity (SHA-256 of an ASCII label) */ |
| 78 | static void label_id(const char *label, uint8_t out[32]) |
| 79 | { |
| 80 | forge_v2_sha256((const uint8_t *)label, strlen(label), out); |
| 81 | } |
| 82 | |
| 83 | /* ---- fixtures ---- */ |
| 84 | |
| 85 | static void cpu_substrate(ForgeSubstrateDescriptor *s) |
| 86 | { |
| 87 | memset(s, 0, sizeof *s); |
| 88 | label_id("FORGE-V2-KAT/substrate/digital-cpu", s->substrate_identity); |
| 89 | s->substrate_class = FORGE_V2_SUBSTRATE_DIGITAL_CPU; |
| 90 | s->operation_families = FORGE_V2_OP_MATVEC | FORGE_V2_OP_MATMUL | FORGE_V2_OP_ELEMENTWISE | |
| 91 | FORGE_V2_OP_REDUCTION | FORGE_V2_OP_GENERAL; |
| 92 | s->input_repr = FORGE_V2_REPR_DIGITAL_INT; s->input_bits = 64; |
| 93 | s->output_repr = FORGE_V2_REPR_DIGITAL_INT; s->output_bits = 64; |
| 94 | s->state_model = FORGE_V2_STATE_VOLATILE; |
| 95 | s->sync_model = FORGE_V2_SYNC_CLOCKED; |
| 96 | s->memory_model = FORGE_V2_MEM_SHARED_COHERENT; |
| 97 | s->memory_capacity_bytes = 128ull << 30; |
| 98 | s->numerical.dynamic_range_mdb = 385000; /* 64-bit integer, ~385 dB */ |
| 99 | s->numerical.resolution_bits = 64; |
| 100 | s->error_kind = FORGE_V2_ERROR_EXACT; |
| 101 | s->fault_detect_mask = 0; |
| 102 | s->timing_min_latency_ns = 1; |
| 103 | s->timing_deterministic = 0; |
| 104 | s->limit_max_rows = 1u << 20; s->limit_max_cols = 1u << 20; |
| 105 | s->limit_max_state_bytes = 64ull << 30; |
| 106 | s->limit_max_concurrent = 20; |
| 107 | s->evidence_capabilities = FORGE_V2_EVCAP_DURATION | FORGE_V2_EVCAP_ENERGY | |
| 108 | FORGE_V2_EVCAP_TEMPERATURE | FORGE_V2_EVCAP_REPEATS; |
| 109 | } |
| 110 | |
| 111 | static void gpu_substrate(ForgeSubstrateDescriptor *s, const uint8_t v1_digest[32]) |
| 112 | { |
| 113 | memset(s, 0, sizeof *s); |
| 114 | label_id("FORGE-V2-KAT/substrate/digital-gpu", s->substrate_identity); |
| 115 | s->substrate_class = FORGE_V2_SUBSTRATE_DIGITAL_GPU; |
| 116 | s->operation_families = FORGE_V2_OP_MATVEC | FORGE_V2_OP_MATMUL | FORGE_V2_OP_ELEMENTWISE | |
| 117 | FORGE_V2_OP_REDUCTION | FORGE_V2_OP_CONV; |
| 118 | s->input_repr = FORGE_V2_REPR_DIGITAL_INT; s->input_bits = 32; |
| 119 | s->output_repr = FORGE_V2_REPR_DIGITAL_INT; s->output_bits = 32; |
| 120 | s->state_model = FORGE_V2_STATE_VOLATILE; |
| 121 | s->sync_model = FORGE_V2_SYNC_BATCH_BARRIER; |
| 122 | s->memory_model = FORGE_V2_MEM_SHARED_COHERENT; |
| 123 | s->memory_capacity_bytes = 128ull << 30; |
| 124 | s->numerical.dynamic_range_mdb = 192000; /* 32-bit integer, ~192 dB */ |
| 125 | s->numerical.resolution_bits = 32; |
| 126 | s->error_kind = FORGE_V2_ERROR_EXACT; |
| 127 | s->timing_min_latency_ns = 1000; |
| 128 | s->timing_setup_ns = 10000; |
| 129 | s->limit_max_rows = 1u << 16; s->limit_max_cols = 1u << 16; |
| 130 | s->limit_max_state_bytes = 64ull << 30; |
| 131 | s->limit_max_concurrent = 1; |
| 132 | s->evidence_capabilities = FORGE_V2_EVCAP_DURATION | FORGE_V2_EVCAP_HW_STATUS | FORGE_V2_EVCAP_FAULT; |
| 133 | s->has_legacy_v1 = 1; |
| 134 | memcpy(s->legacy_v1_descriptor_digest, v1_digest, 32); |
| 135 | } |
| 136 | |
| 137 | /* Development fixture only: no such device exists on this host. */ |
| 138 | static void analog_fixture_substrate(ForgeSubstrateDescriptor *s) |
| 139 | { |
| 140 | memset(s, 0, sizeof *s); |
| 141 | label_id("FORGE-V2-KAT/fixture/analog-in-memory-simulated", s->substrate_identity); |
| 142 | s->substrate_class = FORGE_V2_SUBSTRATE_ANALOG_IN_MEMORY; |
| 143 | s->operation_families = FORGE_V2_OP_MATVEC; |
| 144 | s->input_repr = FORGE_V2_REPR_ANALOG_VOLTAGE; s->input_bits = 8; |
| 145 | s->output_repr = FORGE_V2_REPR_ANALOG_CURRENT; s->output_bits = 8; |
| 146 | s->state_model = FORGE_V2_STATE_PERSISTENT_WEIGHTS; |
| 147 | s->sync_model = FORGE_V2_SYNC_BATCH_BARRIER; |
| 148 | s->memory_model = FORGE_V2_MEM_IN_MEMORY_COMPUTE; |
| 149 | s->memory_capacity_bytes = 1ull << 20; |
| 150 | s->numerical.dynamic_range_mdb = 48000; |
| 151 | s->numerical.resolution_bits = 8; |
| 152 | s->numerical.repeatability_ppb = 2000000; /* 0.2 % */ |
| 153 | s->numerical.drift_ppb_per_hour = 500000; |
| 154 | s->numerical.temperature_ppb_per_k = 100000; |
| 155 | s->numerical.settling_ns = 200; |
| 156 | s->numerical.sample_rate_hz = 10000000; |
| 157 | s->numerical.calibration_validity_s = KAT_WINDOW; |
| 158 | s->calibration_required = 1; |
| 159 | s->calibration_method_mask = (1u << FORGE_V2_CAL_REFERENCE_FIXTURE_SWEEP) | (1u << FORGE_V2_CAL_SIMULATED_MODEL); |
| 160 | s->calibration_max_window_s = KAT_WINDOW; |
| 161 | s->calibration_min_fixtures = 4; |
| 162 | s->error_kind = FORGE_V2_ERROR_BOUNDED_STOCHASTIC; |
| 163 | s->error_rel_ppb = 10000000; /* 1 % */ |
| 164 | s->error_abs = 2; |
| 165 | s->fault_detect_mask = 1; |
| 166 | s->timing_min_latency_ns = 500; |
| 167 | s->timing_conversion_ns = 300; |
| 168 | s->limit_max_rows = 256; s->limit_max_cols = 256; |
| 169 | s->limit_max_state_bytes = 65536; |
| 170 | s->limit_max_concurrent = 1; |
| 171 | s->evidence_capabilities = FORGE_V2_EVCAP_KNOWN_MASK; |
| 172 | } |
| 173 | |
| 174 | static void sort2(ForgeSubstrateDescriptor *a, ForgeSubstrateDescriptor *b) |
| 175 | { |
| 176 | if (memcmp(a->substrate_identity, b->substrate_identity, 32) > 0) { |
| 177 | ForgeSubstrateDescriptor t = *a; *a = *b; *b = t; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static void reference_machine(ForgeMachineDescriptorV2 *m, const uint8_t v1_digest[32]) |
| 182 | { |
| 183 | memset(m, 0, sizeof *m); |
| 184 | label_id("FORGE-V2-KAT/machine/first-qualified-machine", m->machine_identity); |
| 185 | label_id("FORGE-V2-KAT/authority-domain/owner", m->authority_domain); |
| 186 | m->memory_nominal_bytes = 128ull << 30; /* declared nominal, not measured MemTotal */ |
| 187 | m->memory_numa_domains = 1; |
| 188 | m->memory_coherency = FORGE_V2_COHERENCY_UNIFIED; |
| 189 | m->interconnect_topology = FORGE_V2_TOPO_SINGLE_PACKAGE; |
| 190 | m->interconnect_links = 1; |
| 191 | m->transport_class = FORGE_V2_TRANSPORT_ON_PACKAGE; |
| 192 | m->transport_bandwidth_mbps = 600000; |
| 193 | m->transport_latency_ns = 1000; |
| 194 | m->substrate_count = 2; |
| 195 | cpu_substrate(&m->substrates[0]); |
| 196 | gpu_substrate(&m->substrates[1], v1_digest); |
| 197 | sort2(&m->substrates[0], &m->substrates[1]); |
| 198 | } |
| 199 | |
| 200 | static void bounded_matvec_contract(ForgeSemanticResultContract *c) |
| 201 | { |
| 202 | memset(c, 0, sizeof *c); |
| 203 | c->operation_family = FORGE_V2_OP_MATVEC; |
| 204 | label_id("FORGE-V2-KAT/oracle/matvec-exact-integer-reference", c->reference_oracle_digest); |
| 205 | c->shape_rank = 1; c->shape_dim[0] = 256; |
| 206 | c->error_kind = FORGE_V2_ERROR_BOUNDED_STOCHASTIC; |
| 207 | c->error_rel_ppb = 20000000; /* 2 % */ |
| 208 | c->error_abs = 4; |
| 209 | c->error_norm = FORGE_V2_NORM_ELEMENTWISE_MAX; |
| 210 | c->confidence_ppm = 999000; |
| 211 | c->min_samples = 32; |
| 212 | c->allowed_nondeterminism = FORGE_V2_NONDET_STOCHASTIC_NOISE | FORGE_V2_NONDET_ROUNDING; |
| 213 | } |
| 214 | |
| 215 | static void exact_matvec_contract(ForgeSemanticResultContract *c) |
| 216 | { |
| 217 | memset(c, 0, sizeof *c); |
| 218 | c->operation_family = FORGE_V2_OP_MATVEC; |
| 219 | label_id("FORGE-V2-KAT/oracle/matvec-exact-integer-reference", c->reference_oracle_digest); |
| 220 | c->shape_rank = 1; c->shape_dim[0] = 256; |
| 221 | c->error_kind = FORGE_V2_ERROR_EXACT; |
| 222 | c->error_norm = FORGE_V2_NORM_ELEMENTWISE_MAX; |
| 223 | c->confidence_ppm = 1000000; |
| 224 | c->min_samples = 1; |
| 225 | } |
| 226 | |
| 227 | static void fixture_calibration(ForgeCalibrationArtifact *a, const ForgeSubstrateDescriptor *s) |
| 228 | { |
| 229 | memset(a, 0, sizeof *a); |
| 230 | memcpy(a->substrate_id, s->substrate_identity, 32); |
| 231 | label_id("FORGE-V2-KAT/calibration/0001", a->calibration_id); |
| 232 | a->method = FORGE_V2_CAL_SIMULATED_MODEL; |
| 233 | a->epoch_s = KAT_EPOCH; |
| 234 | a->validity_window_s = KAT_WINDOW; |
| 235 | a->env_temp_min_mc = 15000; a->env_temp_max_mc = 45000; |
| 236 | a->env_supply_min_uv = 790000; a->env_supply_max_uv = 810000; |
| 237 | a->transfer_gain_ppb = -1500000; |
| 238 | a->transfer_offset_lsb = -1; |
| 239 | a->transfer_linearity_ppb = 800000; |
| 240 | a->noise_rms_ppb = 1200000; a->noise_peak_ppb = 4000000; a->noise_samples = 4096; |
| 241 | a->drift_ppb_per_hour = 400000; a->drift_temp_ppb_per_k = 90000; a->drift_observed_span_s = 7200; |
| 242 | a->fixture_count = 8; |
| 243 | label_id("FORGE-V2-KAT/calibration/fixtures", a->fixtures_digest); |
| 244 | label_id("FORGE-V2-KAT/calibration/evidence-root", a->evidence_root); |
| 245 | } |
| 246 | |
| 247 | static void fixture_evidence(ForgeExecutionEvidenceV2 *e, const uint8_t rid[32], const uint8_t machine_id[32], |
| 248 | const uint8_t substrate_id[32], const uint8_t contract_digest[32], |
| 249 | const uint8_t cal_digest[32]) |
| 250 | { |
| 251 | memset(e, 0, sizeof *e); |
| 252 | memcpy(e->realization_identity, rid, 32); |
| 253 | memcpy(e->machine_identity, machine_id, 32); |
| 254 | memcpy(e->substrate_identity, substrate_id, 32); |
| 255 | memcpy(e->semantic_contract_digest, contract_digest, 32); |
| 256 | e->has_calibration = 1; |
| 257 | memcpy(e->calibration_digest, cal_digest, 32); |
| 258 | label_id("FORGE-V2-KAT/evidence/input", e->input_identity); |
| 259 | label_id("FORGE-V2-KAT/evidence/output", e->output_identity); |
| 260 | e->execution_epoch_s = KAT_EPOCH + 60; |
| 261 | e->execution_duration_ns = 1800; |
| 262 | e->conversion_duration_ns = 300; |
| 263 | e->energy_nj = 0; |
| 264 | e->energy_source = FORGE_V2_ENERGY_NOT_MEASURED; |
| 265 | e->env_temperature_mc = 25000; |
| 266 | e->env_supply_uv = 800000; |
| 267 | e->measured_error_rel_ppb = 6000000; |
| 268 | e->measured_error_abs = 1; |
| 269 | e->confidence_ppm = 999500; |
| 270 | e->repeat_runs = 32; e->repeat_passes = 32; |
| 271 | e->repeat_mean_ns = 1800; e->repeat_spread_ns = 40; e->repeat_error_spread_ppb = 900000; |
| 272 | e->hardware_status = 0; |
| 273 | e->fault_state = FORGE_V2_FAULT_NONE; |
| 274 | e->provenance_class = FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT; |
| 275 | } |
| 276 | |
| 277 | static void fixture_ref(ForgeCrossMachineRef *r, const uint8_t target[32]) |
| 278 | { |
| 279 | memset(r, 0, sizeof *r); |
| 280 | label_id("FORGE-V2-KAT/ref/program", r->program_digest); |
| 281 | label_id("FORGE-V2-KAT/ref/semantic-object", r->semantic_object_id); |
| 282 | label_id("FORGE-V2-KAT/ref/ir", r->ir_reference); |
| 283 | r->ir_size = 4096; r->range_offset = 512; r->range_length = 1024; |
| 284 | r->target_kind = FORGE_V2_TARGET_SUBSTRATE; |
| 285 | memcpy(r->target_digest, target, 32); |
| 286 | r->generation = KAT_GENERATION; |
| 287 | r->rights = FORGE_V2_RIGHT_READ | FORGE_V2_RIGHT_EXECUTE_PURE; |
| 288 | } |
| 289 | |
| 290 | /* ---- all fixtures in one place ---- */ |
| 291 | |
| 292 | typedef struct { |
| 293 | uint8_t v1_digest[32]; |
| 294 | ForgeMachineDescriptorV2 machine; |
| 295 | uint8_t machine_digest[32]; |
| 296 | ForgeSubstrateDescriptor analog; |
| 297 | uint8_t analog_digest[32]; |
| 298 | ForgeSemanticResultContract contract; |
| 299 | uint8_t contract_digest[32]; |
| 300 | ForgeCalibrationArtifact cal; |
| 301 | uint8_t cal_digest[32]; |
| 302 | uint8_t code_digest[32]; |
| 303 | uint8_t rid[32]; |
| 304 | ForgeExecutionEvidenceV2 ev; |
| 305 | uint8_t ev_digest[32]; |
| 306 | ForgeCrossMachineRef ref; |
| 307 | uint8_t ref_digest[32]; |
| 308 | ForgeMachineDescriptorV2 analog_machine; /* Fabric Machine with only an analog substrate */ |
| 309 | uint8_t analog_machine_digest[32]; |
| 310 | } Kat; |
| 311 | |
| 312 | /* Development fixture: an external analog compute node reached over the |
| 313 | * Fabric, whose only substrate is the simulated analog fixture. */ |
| 314 | static void analog_only_machine(ForgeMachineDescriptorV2 *m, const ForgeSubstrateDescriptor *analog) |
| 315 | { |
| 316 | memset(m, 0, sizeof *m); |
| 317 | label_id("FORGE-V2-KAT/machine/external-analog-node-fixture", m->machine_identity); |
| 318 | label_id("FORGE-V2-KAT/authority-domain/owner", m->authority_domain); |
| 319 | m->memory_nominal_bytes = 1ull << 20; |
| 320 | m->memory_numa_domains = 1; |
| 321 | m->memory_coherency = FORGE_V2_COHERENCY_DISCRETE; |
| 322 | m->interconnect_topology = FORGE_V2_TOPO_REMOTE; |
| 323 | m->interconnect_links = 1; |
| 324 | m->transport_class = FORGE_V2_TRANSPORT_ETHERNET; |
| 325 | m->transport_bandwidth_mbps = 10000; |
| 326 | m->transport_latency_ns = 50000; |
| 327 | m->substrate_count = 1; |
| 328 | m->substrates[0] = *analog; |
| 329 | } |
| 330 | |
| 331 | static int build_kat(Kat *k) |
| 332 | { |
| 333 | memset(k, 0, sizeof *k); |
| 334 | forge_v2_sha256(V1_KAT_STREAM, sizeof V1_KAT_STREAM, k->v1_digest); |
| 335 | reference_machine(&k->machine, k->v1_digest); |
| 336 | if (forge_v2_digest_machine(&k->machine, k->machine_digest)) return -1; |
| 337 | analog_fixture_substrate(&k->analog); |
| 338 | if (forge_v2_digest_substrate(&k->analog, k->analog_digest)) return -1; |
| 339 | bounded_matvec_contract(&k->contract); |
| 340 | if (forge_v2_digest_contract(&k->contract, k->contract_digest)) return -1; |
| 341 | fixture_calibration(&k->cal, &k->analog); |
| 342 | if (forge_v2_digest_calibration(&k->cal, k->cal_digest)) return -1; |
| 343 | label_id("FORGE-V2-KAT/code/analog-matvec-256", k->code_digest); |
| 344 | forge_v2_realization_identity(k->contract_digest, k->machine.machine_identity, k->analog_digest, |
| 345 | k->code_digest, k->cal_digest, k->rid); |
| 346 | fixture_evidence(&k->ev, k->rid, k->machine.machine_identity, k->analog.substrate_identity, |
| 347 | k->contract_digest, k->cal_digest); |
| 348 | if (forge_v2_digest_evidence(&k->ev, k->ev_digest)) return -1; |
| 349 | fixture_ref(&k->ref, k->analog_digest); |
| 350 | if (forge_v2_digest_ref(&k->ref, k->ref_digest)) return -1; |
| 351 | analog_only_machine(&k->analog_machine, &k->analog); |
| 352 | if (forge_v2_digest_machine(&k->analog_machine, k->analog_machine_digest)) return -1; |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | /* ---- byte-level helpers for negative tests ---- */ |
| 357 | |
| 358 | static uint32_t rd32(const uint8_t *p) |
| 359 | { |
| 360 | return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); |
| 361 | } |
| 362 | static void wr32(uint8_t *p, uint32_t v) |
| 363 | { |
| 364 | p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24); |
| 365 | } |
| 366 | |
| 367 | /* offset of the TLV with this tag, or 0 */ |
| 368 | static size_t find_tlv(const uint8_t *b, size_t n, uint32_t tag) |
| 369 | { |
| 370 | size_t pos = FORGE_V2_HEADER_LEN; |
| 371 | while (pos + 8 <= n) { |
| 372 | if (rd32(b + pos) == tag) return pos; |
| 373 | pos += 8 + rd32(b + pos + 4); |
| 374 | } |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | /* remove the TLV at off (length 8+len), fixing body_len */ |
| 379 | static size_t cut_tlv(uint8_t *b, size_t n, size_t off) |
| 380 | { |
| 381 | size_t w = 8 + rd32(b + off + 4); |
| 382 | memmove(b + off, b + off + w, n - off - w); |
| 383 | n -= w; |
| 384 | wr32(b + 8, (uint32_t)(n - FORGE_V2_HEADER_LEN)); |
| 385 | return n; |
| 386 | } |
| 387 | |
| 388 | /* insert a TLV at off, fixing body_len */ |
| 389 | static size_t put_tlv(uint8_t *b, size_t n, size_t off, uint32_t tag, const uint8_t *v, uint32_t vlen) |
| 390 | { |
| 391 | memmove(b + off + 8 + vlen, b + off, n - off); |
| 392 | wr32(b + off, tag); |
| 393 | wr32(b + off + 4, vlen); |
| 394 | memcpy(b + off + 8, v, vlen); |
| 395 | n += 8 + vlen; |
| 396 | wr32(b + 8, (uint32_t)(n - FORGE_V2_HEADER_LEN)); |
| 397 | return n; |
| 398 | } |
| 399 | |
| 400 | /* ---- checks ---- */ |
| 401 | |
| 402 | static uint8_t g_buf[FORGE_V2_MAX_OBJECT_LEN]; |
| 403 | static uint8_t g_tmp[FORGE_V2_MAX_OBJECT_LEN]; |
| 404 | |
| 405 | static int run_checks(void) |
| 406 | { |
| 407 | Kat k; |
| 408 | size_t n = 0, n2 = 0; |
| 409 | uint8_t d[32]; |
| 410 | int rc; |
| 411 | |
| 412 | /* V1 wrap */ |
| 413 | uint8_t v1[32]; |
| 414 | forge_v2_sha256(V1_KAT_STREAM, sizeof V1_KAT_STREAM, v1); |
| 415 | check("v1_kat_stream_digest_reproduced (208-byte literal -> 10d63d05...)", digest_is(v1, V1_KAT_DIGEST)); |
| 416 | |
| 417 | check("kat_fixtures_build", build_kat(&k) == 0); |
| 418 | { |
| 419 | const ForgeSubstrateDescriptor *gpu = 0; |
| 420 | for (uint32_t i = 0; i < k.machine.substrate_count; i++) |
| 421 | if (k.machine.substrates[i].substrate_class == FORGE_V2_SUBSTRATE_DIGITAL_GPU) gpu = &k.machine.substrates[i]; |
| 422 | check("v1_digest_wrapped_in_digital_gpu_substrate", |
| 423 | gpu && gpu->has_legacy_v1 && digest_is(gpu->legacy_v1_descriptor_digest, V1_KAT_DIGEST)); |
| 424 | /* wrap, not re-serialize: the v1 stream bytes never appear in V2 bytes */ |
| 425 | rc = forge_v2_encode_machine(&k.machine, g_buf, sizeof g_buf, &n); |
| 426 | int found = 0; |
| 427 | for (size_t i = 0; rc == 0 && i + 16 <= n; i++) |
| 428 | if (memcmp(g_buf + i, V1_KAT_STREAM + 8, 16) == 0) found = 1; /* v1 location field */ |
| 429 | check("v2_bytes_carry_no_v1_fields (no bus address in identity)", rc == 0 && !found); |
| 430 | } |
| 431 | |
| 432 | /* frozen digests */ |
| 433 | check("frozen_v2_machine_descriptor_digest", digest_is(k.machine_digest, FROZEN_MACHINE)); |
| 434 | check("frozen_semantic_result_contract_digest", digest_is(k.contract_digest, FROZEN_CONTRACT)); |
| 435 | check("frozen_calibration_artifact_digest", digest_is(k.cal_digest, FROZEN_CALIBRATION)); |
| 436 | check("frozen_execution_evidence_v2_digest", digest_is(k.ev_digest, FROZEN_EVIDENCE)); |
| 437 | check("frozen_cross_machine_ref_digest", digest_is(k.ref_digest, FROZEN_REF)); |
| 438 | |
| 439 | /* round trips (strict) */ |
| 440 | { |
| 441 | ForgeMachineDescriptorV2 m2; |
| 442 | rc = forge_v2_encode_machine(&k.machine, g_buf, sizeof g_buf, &n); |
| 443 | int ok = rc == 0 && forge_v2_decode_machine(g_buf, n, 1, &m2) == 0 && |
| 444 | forge_v2_encode_machine(&m2, g_tmp, sizeof g_tmp, &n2) == 0 && n2 == n && |
| 445 | memcmp(g_buf, g_tmp, n) == 0 && forge_v2_digest_machine(&m2, d) == 0 && |
| 446 | memcmp(d, k.machine_digest, 32) == 0 && m2.substrate_count == 2; |
| 447 | check("roundtrip_machine_descriptor_strict", ok); |
| 448 | } |
| 449 | { |
| 450 | ForgeSemanticResultContract c2; ForgeCalibrationArtifact a2; ForgeExecutionEvidenceV2 e2; |
| 451 | ForgeCrossMachineRef r2; ForgeSubstrateDescriptor s2; |
| 452 | int ok = 1; |
| 453 | ok &= forge_v2_encode_contract(&k.contract, g_buf, sizeof g_buf, &n) == 0 && |
| 454 | forge_v2_decode_contract(g_buf, n, 1, &c2) == 0 && forge_v2_digest_contract(&c2, d) == 0 && |
| 455 | memcmp(d, k.contract_digest, 32) == 0; |
| 456 | ok &= forge_v2_encode_calibration(&k.cal, g_buf, sizeof g_buf, &n) == 0 && |
| 457 | forge_v2_decode_calibration(g_buf, n, 1, &a2) == 0 && forge_v2_digest_calibration(&a2, d) == 0 && |
| 458 | memcmp(d, k.cal_digest, 32) == 0 && a2.transfer_gain_ppb == -1500000; |
| 459 | ok &= forge_v2_encode_evidence(&k.ev, g_buf, sizeof g_buf, &n) == 0 && |
| 460 | forge_v2_decode_evidence(g_buf, n, 1, &e2) == 0 && forge_v2_digest_evidence(&e2, d) == 0 && |
| 461 | memcmp(d, k.ev_digest, 32) == 0; |
| 462 | ok &= forge_v2_encode_ref(&k.ref, g_buf, sizeof g_buf, &n) == 0 && |
| 463 | forge_v2_decode_ref(g_buf, n, 1, &r2) == 0 && forge_v2_digest_ref(&r2, d) == 0 && |
| 464 | memcmp(d, k.ref_digest, 32) == 0 && r2.generation == KAT_GENERATION; |
| 465 | ok &= forge_v2_encode_substrate(&k.analog, g_buf, sizeof g_buf, &n) == 0 && |
| 466 | forge_v2_decode_substrate(g_buf, n, 1, &s2) == 0 && forge_v2_digest_substrate(&s2, d) == 0 && |
| 467 | memcmp(d, k.analog_digest, 32) == 0; |
| 468 | check("roundtrip_contract_calibration_evidence_ref_substrate_strict", ok); |
| 469 | } |
| 470 | |
| 471 | /* unknown tag: strict refuses, lenient skips and yields the same object */ |
| 472 | { |
| 473 | ForgeMachineDescriptorV2 m2; |
| 474 | uint8_t v[4] = { 1, 2, 3, 4 }; |
| 475 | forge_v2_encode_machine(&k.machine, g_buf, sizeof g_buf, &n); |
| 476 | n2 = put_tlv(g_buf, n, n, 0x01F0, v, 4); /* after the last (0x0140) field */ |
| 477 | check("unknown_tag_rejected_strict", forge_v2_decode_machine(g_buf, n2, 1, &m2) == FORGE_V2_ERR_UNKNOWN_TAG); |
| 478 | rc = forge_v2_decode_machine(g_buf, n2, 0, &m2); |
| 479 | check("unknown_tag_skipped_lenient_same_digest", |
| 480 | rc == 0 && forge_v2_digest_machine(&m2, d) == 0 && memcmp(d, k.machine_digest, 32) == 0); |
| 481 | } |
| 482 | |
| 483 | /* canonical order and structure */ |
| 484 | { |
| 485 | ForgeSemanticResultContract c2; |
| 486 | forge_v2_encode_contract(&k.contract, g_buf, sizeof g_buf, &n); |
| 487 | /* swap two adjacent 4-byte TLVs (0x0310 shape_rank, 0x0311 shape_dim0) */ |
| 488 | size_t o = find_tlv(g_buf, n, 0x0310); |
| 489 | uint8_t a[12], b[12]; |
| 490 | memcpy(a, g_buf + o, 12); memcpy(b, g_buf + o + 12, 12); |
| 491 | memcpy(g_buf + o, b, 12); memcpy(g_buf + o + 12, a, 12); |
| 492 | check("out_of_order_tags_rejected", forge_v2_decode_contract(g_buf, n, 1, &c2) == FORGE_V2_ERR_ORDER); |
| 493 | forge_v2_encode_contract(&k.contract, g_buf, sizeof g_buf, &n); |
| 494 | n2 = cut_tlv(g_buf, n, find_tlv(g_buf, n, 0x0330)); |
| 495 | check("missing_required_field_rejected", forge_v2_decode_contract(g_buf, n2, 1, &c2) == FORGE_V2_ERR_MISSING); |
| 496 | forge_v2_encode_contract(&k.contract, g_buf, sizeof g_buf, &n); |
| 497 | g_buf[n - 1] ^= 0xff; /* nondeterminism mask gets unknown bits */ |
| 498 | check("unknown_enum_bits_rejected", forge_v2_decode_contract(g_buf, n, 1, &c2) == FORGE_V2_ERR_VALUE); |
| 499 | forge_v2_encode_contract(&k.contract, g_buf, sizeof g_buf, &n); |
| 500 | check("truncated_object_rejected", forge_v2_decode_contract(g_buf, n - 1, 1, &c2) == FORGE_V2_ERR_LENGTH); |
| 501 | } |
| 502 | { |
| 503 | ForgeMachineDescriptorV2 m = k.machine; |
| 504 | m.substrates[1] = m.substrates[0]; |
| 505 | check("duplicate_substrate_identity_rejected", |
| 506 | forge_v2_encode_machine(&m, g_buf, sizeof g_buf, &n) == FORGE_V2_ERR_DUPLICATE_ID); |
| 507 | m = k.machine; |
| 508 | ForgeSubstrateDescriptor t = m.substrates[0]; m.substrates[0] = m.substrates[1]; m.substrates[1] = t; |
| 509 | check("substrates_out_of_identity_order_rejected", |
| 510 | forge_v2_encode_machine(&m, g_buf, sizeof g_buf, &n) == FORGE_V2_ERR_ORDER); |
| 511 | m = k.machine; m.substrate_count = 0; |
| 512 | check("zero_substrates_rejected", forge_v2_encode_machine(&m, g_buf, sizeof g_buf, &n) == FORGE_V2_ERR_COUNT); |
| 513 | { |
| 514 | /* (a) a Fabric Machine whose only substrate is analog is valid */ |
| 515 | ForgeMachineDescriptorV2 am; |
| 516 | rc = forge_v2_encode_machine(&k.analog_machine, g_buf, sizeof g_buf, &n); |
| 517 | check("analog_only_machine_roundtrip_strict", |
| 518 | rc == 0 && forge_v2_decode_machine(g_buf, n, 1, &am) == 0 && am.substrate_count == 1 && |
| 519 | am.substrates[0].substrate_class == FORGE_V2_SUBSTRATE_ANALOG_IN_MEMORY && |
| 520 | forge_v2_digest_machine(&am, d) == 0 && memcmp(d, k.analog_machine_digest, 32) == 0); |
| 521 | check("frozen_analog_only_machine_digest", digest_is(k.analog_machine_digest, FROZEN_ANALOG_MACHINE)); |
| 522 | } |
| 523 | { |
| 524 | /* (b)/(c) the digital fallback is a property of the eligible set */ |
| 525 | ForgeV2Candidate set[3]; |
| 526 | memset(set, 0, sizeof set); |
| 527 | memcpy(set[0].contract_digest, k.contract_digest, 32); |
| 528 | memcpy(set[0].machine_identity, k.analog_machine.machine_identity, 32); |
| 529 | memcpy(set[0].substrate_digest, k.analog_digest, 32); |
| 530 | set[0].substrate_class = FORGE_V2_SUBSTRATE_ANALOG_IN_MEMORY; |
| 531 | set[1] = set[0]; |
| 532 | set[1].substrate_digest[0] ^= 1; /* a second analog candidate */ |
| 533 | check("eligible_set_analog_only_has_no_digital_fallback", |
| 534 | forge_v2_eligible_set_has_digital_fallback(set, 2, k.contract_digest) == 0 && |
| 535 | forge_v2_admit_plan(set, 2, k.contract_digest) == FORGE_V2_ERR_NO_DIGITAL_FALLBACK); |
| 536 | /* a digital candidate for a different contract does not count */ |
| 537 | const ForgeSubstrateDescriptor *cpu = &k.machine.substrates[0]; |
| 538 | if (cpu->substrate_class != FORGE_V2_SUBSTRATE_DIGITAL_CPU) cpu = &k.machine.substrates[1]; |
| 539 | memcpy(set[2].contract_digest, k.contract_digest, 32); |
| 540 | set[2].contract_digest[0] ^= 1; |
| 541 | memcpy(set[2].machine_identity, k.machine.machine_identity, 32); |
| 542 | forge_v2_digest_substrate(cpu, set[2].substrate_digest); |
| 543 | set[2].substrate_class = cpu->substrate_class; |
| 544 | check("digital_candidate_for_other_contract_not_a_fallback", |
| 545 | forge_v2_eligible_set_has_digital_fallback(set, 3, k.contract_digest) == 0); |
| 546 | /* digital realization of the same contract on another Machine */ |
| 547 | memcpy(set[2].contract_digest, k.contract_digest, 32); |
| 548 | check("eligible_set_digital_on_other_machine_is_fallback", |
| 549 | memcmp(set[2].machine_identity, set[0].machine_identity, 32) != 0 && |
| 550 | forge_v2_eligible_set_has_digital_fallback(set, 3, k.contract_digest) == 1 && |
| 551 | forge_v2_admit_plan(set, 3, k.contract_digest) == FORGE_V2_OK); |
| 552 | } |
| 553 | ForgeSubstrateDescriptor s = k.analog; |
| 554 | s.substrate_class = FORGE_V2_SUBSTRATE_FUTURE_RESERVED_FIRST; |
| 555 | check("reserved_substrate_class_rejected", |
| 556 | forge_v2_encode_substrate(&s, g_buf, sizeof g_buf, &n) == FORGE_V2_ERR_VALUE); |
| 557 | s = k.analog; s.has_legacy_v1 = 1; memcpy(s.legacy_v1_descriptor_digest, k.v1_digest, 32); |
| 558 | check("legacy_v1_digest_only_on_digital_substrate", |
| 559 | forge_v2_encode_substrate(&s, g_buf, sizeof g_buf, &n) == FORGE_V2_ERR_VALUE); |
| 560 | } |
| 561 | |
| 562 | /* transport is cost metadata: descriptor digest moves, identity does not */ |
| 563 | { |
| 564 | ForgeMachineDescriptorV2 m = k.machine; |
| 565 | m.transport_class = FORGE_V2_TRANSPORT_RDMA_ETHERNET; |
| 566 | uint8_t rid2[32]; |
| 567 | forge_v2_digest_machine(&m, d); |
| 568 | forge_v2_realization_identity(k.contract_digest, m.machine_identity, k.analog_digest, k.code_digest, |
| 569 | k.cal_digest, rid2); |
| 570 | check("transport_changes_descriptor_digest_not_realization_identity", |
| 571 | memcmp(d, k.machine_digest, 32) != 0 && memcmp(rid2, k.rid, 32) == 0); |
| 572 | } |
| 573 | |
| 574 | /* calibration: fresh ok, stale / missing / future / foreign refused, digest bound into identity */ |
| 575 | { |
| 576 | uint64_t fresh = KAT_EPOCH + 60, stale = KAT_EPOCH + KAT_WINDOW, future = KAT_EPOCH - 1; |
| 577 | check("fresh_calibration_eligible", |
| 578 | forge_v2_substrate_eligible(&k.analog, &k.contract, &k.cal, FORGE_V2_FAULT_NONE, AT(fresh)) == 0); |
| 579 | check("stale_calibration_rejected", |
| 580 | forge_v2_substrate_eligible(&k.analog, &k.contract, &k.cal, FORGE_V2_FAULT_NONE, AT(stale)) == |
| 581 | FORGE_V2_ERR_CAL_STALE); |
| 582 | check("missing_calibration_rejected", |
| 583 | forge_v2_substrate_eligible(&k.analog, &k.contract, 0, FORGE_V2_FAULT_NONE, AT(fresh)) == |
| 584 | FORGE_V2_ERR_CAL_MISSING); |
| 585 | check("calibration_from_future_rejected", |
| 586 | forge_v2_calibration_check(&k.analog, &k.cal, AT(future)) == FORGE_V2_ERR_CAL_STALE); |
| 587 | ForgeCalibrationArtifact a = k.cal; |
| 588 | a.validity_window_s = KAT_WINDOW * 10; /* artifact over-claims its window */ |
| 589 | check("calibration_window_capped_by_substrate", |
| 590 | forge_v2_calibration_check(&k.analog, &a, AT(KAT_EPOCH + KAT_WINDOW + 1)) == FORGE_V2_ERR_CAL_STALE); |
| 591 | a = k.cal; a.substrate_id[0] ^= 1; |
| 592 | check("calibration_for_other_substrate_rejected", |
| 593 | forge_v2_calibration_check(&k.analog, &a, AT(fresh)) == FORGE_V2_ERR_CAL_MISMATCH); |
| 594 | ForgeV2Conditions hot = { fresh, 50000, 800000 }; /* 50 C; envelope ends at 45 C */ |
| 595 | check("out_of_envelope_conditions_rejected", |
| 596 | forge_v2_substrate_eligible(&k.analog, &k.contract, &k.cal, FORGE_V2_FAULT_NONE, &hot) == |
| 597 | FORGE_V2_ERR_OUT_OF_ENVELOPE); |
| 598 | a = k.cal; a.epoch_s += 1; |
| 599 | uint8_t cd[32], rid2[32]; |
| 600 | forge_v2_digest_calibration(&a, cd); |
| 601 | forge_v2_realization_identity(k.contract_digest, k.machine.machine_identity, k.analog_digest, |
| 602 | k.code_digest, cd, rid2); |
| 603 | check("recalibration_changes_realization_identity", memcmp(rid2, k.rid, 32) != 0); |
| 604 | } |
| 605 | |
| 606 | /* failure behavior: faulted substrate excluded, digital fallback remains */ |
| 607 | { |
| 608 | ForgeSemanticResultContract exact; |
| 609 | exact_matvec_contract(&exact); |
| 610 | const ForgeSubstrateDescriptor *cpu = 0; |
| 611 | for (uint32_t i = 0; i < k.machine.substrate_count; i++) |
| 612 | if (k.machine.substrates[i].substrate_class == FORGE_V2_SUBSTRATE_DIGITAL_CPU) cpu = &k.machine.substrates[i]; |
| 613 | check("faulted_substrate_excluded", |
| 614 | forge_v2_substrate_eligible(&k.analog, &k.contract, &k.cal, FORGE_V2_FAULT_PERSISTENT, AT(KAT_EPOCH + 60)) == |
| 615 | FORGE_V2_ERR_FAULTED); |
| 616 | check("digital_fallback_eligible_for_bounded_and_exact", |
| 617 | cpu && forge_v2_substrate_eligible(cpu, &k.contract, 0, FORGE_V2_FAULT_NONE, AT(KAT_EPOCH + 60)) == 0 && |
| 618 | forge_v2_substrate_eligible(cpu, &exact, 0, FORGE_V2_FAULT_NONE, AT(KAT_EPOCH + 60)) == 0); |
| 619 | check("stochastic_substrate_not_eligible_for_exact_contract", |
| 620 | forge_v2_substrate_eligible(&k.analog, &exact, &k.cal, FORGE_V2_FAULT_NONE, AT(KAT_EPOCH + 60)) == |
| 621 | FORGE_V2_ERR_VALUE); |
| 622 | ForgeSemanticResultContract tight = k.contract; |
| 623 | tight.error_rel_ppb = 1000000; /* 0.1 %, tighter than the substrate's 1 % */ |
| 624 | check("substrate_bound_looser_than_contract_rejected", |
| 625 | forge_v2_substrate_eligible(&k.analog, &tight, &k.cal, FORGE_V2_FAULT_NONE, AT(KAT_EPOCH + 60)) == |
| 626 | FORGE_V2_ERR_VALUE); |
| 627 | } |
| 628 | |
| 629 | /* generation: 32-bit refused, legacy version refused, full 64 bits compared */ |
| 630 | { |
| 631 | ForgeCrossMachineRef r2; |
| 632 | forge_v2_encode_ref(&k.ref, g_buf, sizeof g_buf, &n); |
| 633 | size_t off = find_tlv(g_buf, n, 0x0730); |
| 634 | uint8_t g32[4] = { 7, 0, 0, 0 }; |
| 635 | n2 = cut_tlv(g_buf, n, off); |
| 636 | n2 = put_tlv(g_buf, n2, off, 0x0730, g32, 4); |
| 637 | check("v1_32bit_generation_refused", forge_v2_decode_ref(g_buf, n2, 1, &r2) == FORGE_V2_ERR_GENERATION_32); |
| 638 | forge_v2_encode_ref(&k.ref, g_buf, sizeof g_buf, &n); |
| 639 | g_buf[6] = 1; g_buf[7] = 0; |
| 640 | check("legacy_version_1_object_refused", forge_v2_decode_ref(g_buf, n, 1, &r2) == FORGE_V2_ERR_LEGACY_VERSION); |
| 641 | check("authorize_pure_current_generation", |
| 642 | forge_v2_validate_capability_ref(&k.ref, FORGE_V2_FX_PURE, KAT_GENERATION) == 0); |
| 643 | check("stale_generation_high_bits_refused", |
| 644 | forge_v2_validate_capability_ref(&k.ref, FORGE_V2_FX_PURE, KAT_GENERATION + (1ull << 32)) == |
| 645 | FORGE_V2_ERR_STALE_GENERATION); |
| 646 | check("irreversible_config_without_right_refused", |
| 647 | forge_v2_validate_capability_ref(&k.ref, FORGE_V2_FX_EXTERNAL_IRREVERSIBLE, KAT_GENERATION) == FORGE_V2_ERR_RIGHTS); |
| 648 | ForgeCrossMachineRef r = k.ref; |
| 649 | r.rights |= FORGE_V2_RIGHT_CONFIGURE_IRREVERSIBLE; |
| 650 | check("irreversible_config_with_right_and_generation_allowed", |
| 651 | forge_v2_validate_capability_ref(&r, FORGE_V2_FX_EXTERNAL_IRREVERSIBLE, KAT_GENERATION) == 0); |
| 652 | r = k.ref; r.range_offset = UINT64_MAX - 10; |
| 653 | check("reference_range_overflow_refused", forge_v2_encode_ref(&r, g_buf, sizeof g_buf, &n) == FORGE_V2_ERR_RANGE); |
| 654 | } |
| 655 | |
| 656 | /* provenance must be explicit */ |
| 657 | { |
| 658 | ForgeExecutionEvidenceV2 e = k.ev, e2; |
| 659 | e.provenance_class = FORGE_V2_PROVENANCE_INVALID; |
| 660 | check("evidence_encode_without_provenance_refused", |
| 661 | forge_v2_encode_evidence(&e, g_buf, sizeof g_buf, &n) == FORGE_V2_ERR_PROVENANCE); |
| 662 | forge_v2_encode_evidence(&k.ev, g_buf, sizeof g_buf, &n); |
| 663 | n2 = cut_tlv(g_buf, n, find_tlv(g_buf, n, 0x0670)); |
| 664 | check("evidence_decode_missing_provenance_refused", |
| 665 | forge_v2_decode_evidence(g_buf, n2, 1, &e2) == FORGE_V2_ERR_PROVENANCE); |
| 666 | forge_v2_encode_evidence(&k.ev, g_buf, sizeof g_buf, &n); |
| 667 | wr32(g_buf + find_tlv(g_buf, n, 0x0670) + 8, 0); |
| 668 | check("evidence_decode_zero_provenance_refused", |
| 669 | forge_v2_decode_evidence(g_buf, n, 1, &e2) == FORGE_V2_ERR_PROVENANCE); |
| 670 | forge_v2_encode_evidence(&k.ev, g_buf, sizeof g_buf, &n); |
| 671 | check("simulated_provenance_explicit_in_kat_evidence", |
| 672 | forge_v2_decode_evidence(g_buf, n, 1, &e2) == 0 && |
| 673 | e2.provenance_class == FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT); |
| 674 | } |
| 675 | |
| 676 | printf("RESULT %d passed, %d failed\n", g_pass, g_fail); |
| 677 | return g_fail == 0 ? 0 : 1; |
| 678 | } |
| 679 | |
| 680 | /* ---- --dump-vectors ---- */ |
| 681 | |
| 682 | static void dump_bytes(const uint8_t *b, size_t n) |
| 683 | { |
| 684 | char h[65]; |
| 685 | printf("```\n"); |
| 686 | for (size_t i = 0; i < n; i += 32) { |
| 687 | size_t w = n - i < 32 ? n - i : 32; |
| 688 | to_hex(b + i, w, h); |
| 689 | printf("%s\n", h); |
| 690 | } |
| 691 | printf("```\n"); |
| 692 | } |
| 693 | |
| 694 | static void dump_digest(const char *label, const uint8_t d[32]) |
| 695 | { |
| 696 | char h[65]; |
| 697 | to_hex(d, 32, h); |
| 698 | printf("%s: `%s`\n\n", label, h); |
| 699 | } |
| 700 | |
| 701 | static int dump_vectors(void) |
| 702 | { |
| 703 | Kat k; |
| 704 | size_t n = 0; |
| 705 | if (build_kat(&k) != 0) return 1; |
| 706 | printf("# FORGE SUBSTRATE V2 golden vectors\n\n"); |
| 707 | printf("Generated by `build/test_forge_v2_kat --dump-vectors` (see tests/run_forge_v2_gates.sh).\n"); |
| 708 | printf("Do not edit by hand: the gate regenerates this file and fails on any difference.\n\n"); |
| 709 | printf("All vectors are synthetic test fixtures. None is a receipt of a physical run. The\n"); |
| 710 | printf("ANALOG_IN_MEMORY substrate below is a development fixture; no such device exists and\n"); |
| 711 | printf("no physical analog result is claimed. Canonical bytes are shown as hex, 32 bytes per line.\n\n"); |
| 712 | |
| 713 | printf("## 1. FORGE MACHINE DESCRIPTOR V1 KAT stream (wrapped, not re-serialized)\n\n"); |
| 714 | printf("208-byte v1 canonical stream (the v1 KAT record):\n\n"); |
| 715 | dump_bytes(V1_KAT_STREAM, sizeof V1_KAT_STREAM); |
| 716 | printf("\n"); |
| 717 | dump_digest("v1 descriptor digest (must equal the frozen v1 KAT digest)", k.v1_digest); |
| 718 | |
| 719 | printf("## 2. Reference ForgeMachineDescriptorV2 (first qualified machine, frozen)\n\n"); |
| 720 | printf("Two substrates: DIGITAL_CPU and DIGITAL_GPU; the DIGITAL_GPU substrate carries\n"); |
| 721 | printf("`legacy_v1_descriptor_digest` = the v1 digest above.\n\n"); |
| 722 | forge_v2_encode_machine(&k.machine, g_buf, sizeof g_buf, &n); |
| 723 | printf("Canonical bytes (%zu bytes):\n\n", n); |
| 724 | dump_bytes(g_buf, n); |
| 725 | printf("\n"); |
| 726 | dump_digest("V2 machine descriptor digest (frozen)", k.machine_digest); |
| 727 | for (uint32_t i = 0; i < k.machine.substrate_count; i++) { |
| 728 | uint8_t d[32]; |
| 729 | forge_v2_digest_substrate(&k.machine.substrates[i], d); |
| 730 | char lbl[64]; |
| 731 | snprintf(lbl, sizeof lbl, "Substrate %u (%s) descriptor digest", i, |
| 732 | k.machine.substrates[i].substrate_class == FORGE_V2_SUBSTRATE_DIGITAL_GPU ? "DIGITAL_GPU" : "DIGITAL_CPU"); |
| 733 | dump_digest(lbl, d); |
| 734 | } |
| 735 | |
| 736 | printf("## 3. Development fixture substrate: ANALOG_IN_MEMORY (simulated)\n\n"); |
| 737 | forge_v2_encode_substrate(&k.analog, g_buf, sizeof g_buf, &n); |
| 738 | printf("Canonical bytes (%zu bytes):\n\n", n); |
| 739 | dump_bytes(g_buf, n); |
| 740 | printf("\n"); |
| 741 | dump_digest("Fixture substrate descriptor digest", k.analog_digest); |
| 742 | |
| 743 | printf("## 4. SemanticResultContract (bounded stochastic matvec, 256 outputs)\n\n"); |
| 744 | forge_v2_encode_contract(&k.contract, g_buf, sizeof g_buf, &n); |
| 745 | printf("Canonical bytes (%zu bytes):\n\n", n); |
| 746 | dump_bytes(g_buf, n); |
| 747 | printf("\n"); |
| 748 | dump_digest("Contract digest (frozen)", k.contract_digest); |
| 749 | |
| 750 | printf("## 5. CalibrationArtifact (fixture substrate, method SIMULATED_MODEL)\n\n"); |
| 751 | forge_v2_encode_calibration(&k.cal, g_buf, sizeof g_buf, &n); |
| 752 | printf("Canonical bytes (%zu bytes):\n\n", n); |
| 753 | dump_bytes(g_buf, n); |
| 754 | printf("\n"); |
| 755 | dump_digest("Calibration digest (frozen)", k.cal_digest); |
| 756 | |
| 757 | printf("## 6. Realization identity (calibration digest bound in)\n\n"); |
| 758 | dump_digest("Code digest input (SHA-256 of a fixture label)", k.code_digest); |
| 759 | dump_digest("Realization identity", k.rid); |
| 760 | |
| 761 | printf("## 7. ForgeExecutionEvidenceV2 (provenance_class = SIMULATED_DEVELOPMENT)\n\n"); |
| 762 | forge_v2_encode_evidence(&k.ev, g_buf, sizeof g_buf, &n); |
| 763 | printf("Canonical bytes (%zu bytes):\n\n", n); |
| 764 | dump_bytes(g_buf, n); |
| 765 | printf("\n"); |
| 766 | dump_digest("Evidence digest (frozen)", k.ev_digest); |
| 767 | |
| 768 | printf("## 8. Cross-machine reference (64-bit generation 0x%016llx)\n\n", |
| 769 | (unsigned long long)k.ref.generation); |
| 770 | forge_v2_encode_ref(&k.ref, g_buf, sizeof g_buf, &n); |
| 771 | printf("Canonical bytes (%zu bytes):\n\n", n); |
| 772 | dump_bytes(g_buf, n); |
| 773 | printf("\n"); |
| 774 | dump_digest("Reference digest (frozen)", k.ref_digest); |
| 775 | |
| 776 | printf("## 9. Analog-only Fabric Machine (development fixture, frozen)\n\n"); |
| 777 | printf("An external analog compute node whose only substrate is the simulated ANALOG_IN_MEMORY\n"); |
| 778 | printf("fixture of section 3. Valid: the digital fallback is required of the eligible set for a\n"); |
| 779 | printf("contract (any Machine), not of each Machine descriptor.\n\n"); |
| 780 | forge_v2_encode_machine(&k.analog_machine, g_buf, sizeof g_buf, &n); |
| 781 | printf("Canonical bytes (%zu bytes):\n\n", n); |
| 782 | dump_bytes(g_buf, n); |
| 783 | printf("\n"); |
| 784 | dump_digest("Analog-only machine descriptor digest (frozen)", k.analog_machine_digest); |
| 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | int main(int argc, char **argv) |
| 789 | { |
| 790 | if (argc == 2 && strcmp(argv[1], "--dump-vectors") == 0) return dump_vectors(); |
| 791 | if (argc != 1) { |
| 792 | fprintf(stderr, "usage: %s [--dump-vectors]\n", argv[0]); |
| 793 | return 2; |
| 794 | } |
| 795 | return run_checks(); |
| 796 | } |