Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/spark-rsi-mv/blob/main/evidence/dual/gen.c # spark-rsi-mv Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `spark-rsi-mv / evidence / dual / gen.c` 153 lines ยท 8914 bytes Raw Blame History 1 /* Throwaway golden-vector generator. Links omega dual/0a-records rx_dual.c + sha256.c. 2 * Prints "name hex" lines; "# " lines are comments. Status lines print the C 3 * checker's RxDualStatus for hostile inputs so the Rust port can be compared. */ 4 #include "rx_dual.h" 5 #include "sha256.h" 6 #include 7 #include 8 #include 9 10 static void hex(const char *name, const uint8_t *b, size_t n) { 11 printf("%s ", name); 12 for (size_t i = 0; i < n; i++) printf("%02x", b[i]); 13 printf("\n"); 14 } 15 static RxDualDigest tag(const char *s) { 16 RxDualDigest d; sha256_hash((const uint8_t *)s, strlen(s), d.b); return d; 17 } 18 static RxDualDigest zero(void) { RxDualDigest d; memset(&d, 0, sizeof d); return d; } 19 20 static RxDualConstraintState cs(uint32_t id, RxDualUnit unit, RxDualClass cls, double lambda, 21 RxDualLambdaState st, int calibrated, uint64_t gen, uint64_t tick) { 22 RxDualConstraintState s; memset(&s, 0, sizeof s); 23 s.resource_id = id; s.unit = unit; s.cls = cls; 24 s.budget = 4096.0; 25 s.budget_contract = tag("budget-contract"); 26 s.observation_ref = tag("observation"); 27 s.estimate_ref = tag("estimate"); 28 s.estimate_kind = RX_DUAL_EST_MEASURED; 29 s.estimate = 3900.5; s.uncertainty = 12.25; 30 s.calibration_ref = calibrated ? tag("est3-calibration-receipt") : zero(); 31 s.lambda = lambda; s.lambda_state = st; 32 s.controller_id = tag("controller"); 33 s.generation = gen; s.tick = tick; 34 s.evidence_root = tag("evidence-root"); 35 s.parent = tick ? tag("parent-state") : zero(); 36 return s; 37 } 38 39 #define ENC(name, encfn, digfn, dom, obj) do { \ 40 uint8_t buf[RX_DUAL_ENCODED_MAX]; size_t len = 0; RxDualDigest d; \ 41 RxDualStatus st = encfn(&(obj), buf, sizeof buf, &len); \ 42 if (st != RX_DUAL_OK) { printf("# ENCODE FAIL %s st=%d\n", name, (int)st); return 1; } \ 43 st = digfn(&(obj), &d); if (st != RX_DUAL_OK) { printf("# DIGEST FAIL %s\n", name); return 1; } \ 44 hex(name, buf, len); hex(name ".digest", d.b, 32); } while (0) 45 46 int main(void) { 47 printf("# generated by gen.c against omega dual/0a-records src/dual/rx_dual.c + src/sha256.c\n"); 48 printf("# tag(x) = SHA-256(x) used as placeholder digests\n"); 49 50 RxDualResource res; memset(&res, 0, sizeof res); 51 res.resource_id = 5; res.unit = RX_DUAL_UNIT_KV_BLOCKS; res.scale = 1024.0; res.contract = tag("contract-kv"); 52 ENC("resource_kv", rx_dual_encode_resource, rx_dual_digest_resource, RX_DUAL_DOMAIN_RESOURCE, res); 53 54 RxDualConstraintState c_ns = cs(2, RX_DUAL_UNIT_NS, RX_DUAL_CLASS_SOFT, 0.42, RX_DUAL_LAMBDA_FRESH, 1, 7, 3); 55 RxDualConstraintState c_kv = cs(5, RX_DUAL_UNIT_KV_BLOCKS, RX_DUAL_CLASS_SOFT, 0.42, RX_DUAL_LAMBDA_FRESH, 1, 7, 3); 56 RxDualConstraintState c_cap = cs(9, RX_DUAL_UNIT_BYTES, RX_DUAL_CLASS_CAPACITY, 0.1, RX_DUAL_LAMBDA_FRESH, 1, 7, 0); 57 RxDualConstraintState c_st = cs(5, RX_DUAL_UNIT_KV_BLOCKS, RX_DUAL_CLASS_SOFT, 0.42, RX_DUAL_LAMBDA_STALE, 0, 7, 4); 58 RxDualConstraintState c_unc = cs(7, RX_DUAL_UNIT_PJ, RX_DUAL_CLASS_SOFT, 3.0, RX_DUAL_LAMBDA_UNCALIBRATED, 0, 7, 0); 59 RxDualConstraintState c_fro = cs(11, RX_DUAL_UNIT_WATT, RX_DUAL_CLASS_SOFT, 0.7, RX_DUAL_LAMBDA_FROZEN, 1, 7, 2); 60 /* negative zero lambda: must encode as +0.0 */ 61 RxDualConstraintState c_nz = cs(3, RX_DUAL_UNIT_NS, RX_DUAL_CLASS_SOFT, -0.0, RX_DUAL_LAMBDA_FRESH, 1, 7, 0); 62 63 ENC("constraint_fresh_soft_ns", rx_dual_encode_constraint, rx_dual_digest_constraint, RX_DUAL_DOMAIN_CONSTRAINT, c_ns); 64 ENC("constraint_fresh_soft_kv", rx_dual_encode_constraint, rx_dual_digest_constraint, RX_DUAL_DOMAIN_CONSTRAINT, c_kv); 65 ENC("constraint_fresh_capacity", rx_dual_encode_constraint, rx_dual_digest_constraint, RX_DUAL_DOMAIN_CONSTRAINT, c_cap); 66 ENC("constraint_stale", rx_dual_encode_constraint, rx_dual_digest_constraint, RX_DUAL_DOMAIN_CONSTRAINT, c_st); 67 ENC("constraint_uncalibrated", rx_dual_encode_constraint, rx_dual_digest_constraint, RX_DUAL_DOMAIN_CONSTRAINT, c_unc); 68 ENC("constraint_frozen", rx_dual_encode_constraint, rx_dual_digest_constraint, RX_DUAL_DOMAIN_CONSTRAINT, c_fro); 69 ENC("constraint_negzero_lambda", rx_dual_encode_constraint, rx_dual_digest_constraint, RX_DUAL_DOMAIN_CONSTRAINT, c_nz); 70 71 RxDualController ctl; memset(&ctl, 0, sizeof ctl); 72 ctl.eta = 0.05; ctl.rho = 0.01; ctl.k_sigma = 2.0; ctl.max_age = 16; ctl.cadence = 1; ctl.n = 3; 73 ctl.resource_id[0] = 2; ctl.lambda_max[0] = 10.0; 74 ctl.resource_id[1] = 5; ctl.lambda_max[1] = 10.0; 75 ctl.resource_id[2] = 9; ctl.lambda_max[2] = 1.0; 76 ENC("controller", rx_dual_encode_controller, rx_dual_digest_controller, RX_DUAL_DOMAIN_CONTROLLER, ctl); 77 78 RxDualDigest ctx = tag("decision-context"); 79 RxDualPriceVector pv; 80 const RxDualConstraintState *a[3] = { &c_ns, &c_kv, &c_cap }; 81 if (rx_dual_price_vector_build(a, 3, &ctx, &pv) != RX_DUAL_OK) { printf("# PV_A build fail\n"); return 1; } 82 ENC("pricevec_a", rx_dual_encode_price_vector, rx_dual_digest_price_vector, RX_DUAL_DOMAIN_PRICE_VECTOR, pv); 83 const RxDualConstraintState *b[2] = { &c_st, &c_unc }; 84 if (rx_dual_price_vector_build(b, 2, &ctx, &pv) != RX_DUAL_OK) { printf("# PV_B build fail\n"); return 1; } 85 ENC("pricevec_b", rx_dual_encode_price_vector, rx_dual_digest_price_vector, RX_DUAL_DOMAIN_PRICE_VECTOR, pv); 86 const RxDualConstraintState *c[2] = { &c_unc, &c_cap }; 87 if (rx_dual_price_vector_build(c, 2, &ctx, &pv) != RX_DUAL_OK) { printf("# PV_C build fail\n"); return 1; } 88 ENC("pricevec_c", rx_dual_encode_price_vector, rx_dual_digest_price_vector, RX_DUAL_DOMAIN_PRICE_VECTOR, pv); 89 const RxDualConstraintState *d[2] = { &c_fro, &c_unc }; 90 /* unordered on purpose: must be refused by the builder */ 91 printf("# builder unordered (11,7) status %d\n", (int)rx_dual_price_vector_build(d, 2, &ctx, &pv)); 92 93 /* ---- hostile cases: C checker status for mutated encodings ---- */ 94 uint8_t buf[RX_DUAL_ENCODED_MAX]; size_t len = 0; RxDualConstraintState out; 95 rx_dual_encode_constraint(&c_kv, buf, sizeof buf, &len); 96 printf("# constraint encoded length %zu\n", len); 97 uint8_t m[RX_DUAL_ENCODED_MAX]; 98 #define MUT(name, body) do { memcpy(m, buf, len); size_t ml = len; body; \ 99 printf("status %s %d\n", name, (int)rx_dual_decode_constraint(m, ml, &out)); } while (0) 100 MUT("wrong_kind", m[0] = 4); 101 MUT("wrong_version", m[1] = 2); 102 MUT("truncated", ml = len - 1); 103 MUT("trailing", m[len] = 0; ml = len + 1); 104 MUT("class_invariant", m[10] = 1); 105 MUT("class_undeclared", m[10] = 0); 106 MUT("unit_none", m[6] = 0); 107 MUT("unit_unknown", m[6] = 16); 108 /* lambda at offset 2+4+4+4+8+32+32+32+4+8+8+32 = 170 */ 109 MUT("lambda_negative", m[177] |= 0x80); 110 MUT("lambda_nan", { double nan = NAN; memcpy(m + 170, &nan, 8); }); 111 MUT("lambda_inf", { double inf = INFINITY; memcpy(m + 170, &inf, 8); }); 112 /* uncertainty at offset 2+4+4+4+8+32+32+32+4+8 = 130 */ 113 MUT("uncertainty_negative", m[137] |= 0x80); 114 /* calibration_ref at 138..169: zero it while FRESH */ 115 MUT("fresh_without_calibration", memset(m + 138, 0, 32)); 116 /* evidence_root at 170+8+4+32+8+8 = 230..261 */ 117 MUT("evidence_root_zero", memset(m + 230, 0, 32)); 118 /* parent at 262..293: zero while tick=3 */ 119 MUT("parent_zero_with_tick", memset(m + 262, 0, 32)); 120 /* tick at 222..229: zero while parent nonzero */ 121 MUT("tick_zero_with_parent", memset(m + 222, 0, 8)); 122 /* estimate_kind at 118: 0 and 4 */ 123 MUT("estimate_kind_zero", m[118] = 0); 124 MUT("estimate_kind_four", m[118] = 4); 125 /* lambda_state at 178: 0 and 6 */ 126 MUT("lambda_state_zero", m[178] = 0); 127 MUT("lambda_state_six", m[178] = 6); 128 /* observation_ref at 54..85 zero while MEASURED */ 129 MUT("measured_without_observation", memset(m + 54, 0, 32)); 130 /* budget_contract at 22..53 */ 131 MUT("budget_contract_zero", memset(m + 22, 0, 32)); 132 133 /* price vector hostile */ 134 RxDualPriceVector pvo; size_t plen = 0; 135 const RxDualConstraintState *aa[3] = { &c_ns, &c_kv, &c_cap }; 136 rx_dual_price_vector_build(aa, 3, &ctx, &pv); 137 rx_dual_encode_price_vector(&pv, buf, sizeof buf, &plen); 138 printf("# pricevec_a encoded length %zu\n", plen); 139 #define PMUT(name, body) do { memcpy(m, buf, plen); size_t ml = plen; body; \ 140 printf("status %s %d\n", name, (int)rx_dual_decode_price_vector(m, ml, &pvo)); } while (0) 141 PMUT("pv_wrong_kind", m[0] = 2); 142 PMUT("pv_wrong_version", m[1] = 0); 143 PMUT("pv_truncated", ml = plen - 5); 144 PMUT("pv_trailing", m[plen] = 7; ml = plen + 1); 145 /* ids at 2+8+32+4 = 46: (46,id0)(50..81 dig)(82,id1)(86..117)(118,id2) */ 146 PMUT("pv_unordered_ids", { m[46] = 5; m[82] = 2; }); 147 PMUT("pv_duplicate_ids", m[82] = 2); 148 PMUT("pv_zero_state_digest", memset(m + 50, 0, 32)); 149 PMUT("pv_zero_context", memset(m + 10, 0, 32)); 150 PMUT("pv_n_zero", { m[42] = 0; ml = 46; }); 151 PMUT("pv_n_too_large", m[42] = 33); 152 return 0; 153 }