spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / om / governance / receipt_v2.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note core/rsi/om/governance/receipt_v2.om |
| 3 | note Pure seeded Omega (OM-1) implementation of spark-rsi-mv Version 2 signed evaluation |
| 4 | note receipt digest (docs/RECEIPT-V2.md), holdout-set v1 digest, ECDSA P-256 verification |
| 5 | note (via core/rsi/om/p256/p256.om), and fail-closed promotion gate (src/promotion_gate.rs). |
| 6 | note Compose after: core/rsi/om/p256/sha256.om core/rsi/om/p256/p256.om |
| 7 | note Library unit: no main. Uses decimal literals (OM-1 compatible with p256.om). |
| 8 | |
| 9 | note Promotion gate verdicts (0 = PROMOTE; nonzero = fail-closed refusal) |
| 10 | const RV2_OK ulong 0 |
| 11 | const RV2_ERR_DISK_HASH_MISMATCH ulong 1 |
| 12 | const RV2_ERR_BAD_JUDGE_KEY ulong 2 |
| 13 | const RV2_ERR_SAME_KEY ulong 3 |
| 14 | const RV2_ERR_NOT_V2 ulong 4 |
| 15 | const RV2_ERR_BAD_DIGEST ulong 5 |
| 16 | const RV2_ERR_BAD_SIGNATURE ulong 6 |
| 17 | const RV2_ERR_SUBJECT_HASH ulong 7 |
| 18 | const RV2_ERR_POLICY_HASH ulong 8 |
| 19 | const RV2_ERR_HOLDOUT_HASH ulong 9 |
| 20 | const RV2_ERR_NOT_ADMITTED ulong 10 |
| 21 | const RV2_ERR_PARENT_MISMATCH ulong 11 |
| 22 | const RV2_ERR_SUBJECT_PATH ulong 12 |
| 23 | const RV2_ERR_PROTECTED_PATH ulong 13 |
| 24 | const RV2_ERR_NOT_ALLOWED_TARGET ulong 14 |
| 25 | const RV2_ERR_HOLDOUT_THRESHOLD ulong 15 |
| 26 | const RV2_ERR_BAD_POLICY ulong 16 |
| 27 | |
| 28 | note Domain prefixes (docs/RECEIPT-V2.md) |
| 29 | text rv2_dom_receipt "spark-rsi.evaluation-receipt.v2" |
| 30 | text rv2_dom_holdout "spark-rsi.holdout-set.v1" |
| 31 | text rv2_hex_tbl "0123456789abcdef" |
| 32 | |
| 33 | note Global V2 receipt state (one active receipt under verification) |
| 34 | note rv2_meta: |
| 35 | note [0]=format_version, [1]=holdouts_total, [2]=holdouts_passed, |
| 36 | note [3]=admitted(0/1), [4]=hard_inv(0/1), [5]=stat_gates(0/1), |
| 37 | note [6]=layer_cnt (0..8), [7]=has_metrics(0/1), [8]=cand_res_mb, |
| 38 | note [9]=cycle_len, [10]=cand_len, [11]=parent_len, [12]=eval_at_len, [13]=eval_ver_len, |
| 39 | note [14]=subj_path_len |
| 40 | global rv2_meta ulong 16 |
| 41 | global rv2_cycle uchar 64 |
| 42 | global rv2_cand uchar 64 |
| 43 | global rv2_parent uchar 64 |
| 44 | global rv2_eval_at uchar 64 |
| 45 | global rv2_eval_ver uchar 64 |
| 46 | global rv2_eval_bin_hex uchar 64 |
| 47 | global rv2_subj_path uchar 128 |
| 48 | global rv2_subj_hex uchar 64 |
| 49 | global rv2_holdout_hex uchar 64 |
| 50 | global rv2_policy_hex uchar 64 |
| 51 | global rv2_claimed_dig uchar 32 |
| 52 | global rv2_sig uchar 64 |
| 53 | |
| 54 | note Layer records (up to 8 layers) |
| 55 | global rv2_lr_name uchar 512 |
| 56 | global rv2_lr_nlen ulong 8 |
| 57 | global rv2_lr_hard ulong 8 |
| 58 | global rv2_lr_pass ulong 8 |
| 59 | global rv2_lr_score_bits ulong 8 |
| 60 | global rv2_lr_sum uchar 1024 |
| 61 | global rv2_lr_slen ulong 8 |
| 62 | |
| 63 | note Metrics summary (5 f64 IEEE-754 bit patterns as u64) |
| 64 | global rv2_metrics_bits ulong 5 |
| 65 | |
| 66 | note Pinned policy and promotion subject state |
| 67 | note rv2_pol_meta: |
| 68 | note [0]=require_admitted(0/1), [1]=min_ratio_bps(1..10000), |
| 69 | note [2]=allowed_cnt(0..8), [3]=protected_cnt(0..8), |
| 70 | note [4]=sub_parent_len, [5]=sub_path_len |
| 71 | global rv2_pol_meta ulong 8 |
| 72 | global rv2_pol_sha_hex uchar 64 |
| 73 | global rv2_pol_holdout_hex uchar 64 |
| 74 | global rv2_pol_allowed uchar 1024 |
| 75 | global rv2_pol_alen ulong 8 |
| 76 | global rv2_pol_prot uchar 1024 |
| 77 | global rv2_pol_plen ulong 8 |
| 78 | global rv2_judge_pub uchar 65 |
| 79 | global rv2_own_pub uchar 65 |
| 80 | global rv2_sub_parent uchar 64 |
| 81 | global rv2_sub_path uchar 128 |
| 82 | global rv2_sub_content_hex uchar 64 |
| 83 | global rv2_sub_disk_hex uchar 64 |
| 84 | |
| 85 | note Computed digest output |
| 86 | global rv2_comp_dig uchar 32 |
| 87 | global rv2_comp_hex uchar 64 |
| 88 | |
| 89 | note Reset all V2 receipt, policy, and subject state (fail-closed defaults). |
| 90 | fn rv2_reset to void |
| 91 | for i ulong 16 |
| 92 | put rv2_meta i 0 |
| 93 | end |
| 94 | for i ulong 8 |
| 95 | put rv2_pol_meta i 0 |
| 96 | put rv2_lr_nlen i 0 |
| 97 | put rv2_lr_hard i 0 |
| 98 | put rv2_lr_pass i 0 |
| 99 | put rv2_lr_score_bits i 0 |
| 100 | put rv2_lr_slen i 0 |
| 101 | put rv2_pol_alen i 0 |
| 102 | put rv2_pol_plen i 0 |
| 103 | end |
| 104 | for i ulong 5 |
| 105 | put rv2_metrics_bits i 0 |
| 106 | end |
| 107 | for i ulong 65 |
| 108 | put rv2_judge_pub i uchar 0 |
| 109 | put rv2_own_pub i uchar 0 |
| 110 | end |
| 111 | for i ulong 64 |
| 112 | put rv2_sig i uchar 0 |
| 113 | put rv2_comp_hex i uchar 0 |
| 114 | end |
| 115 | for i ulong 32 |
| 116 | put rv2_claimed_dig i uchar 0 |
| 117 | put rv2_comp_dig i uchar 0 |
| 118 | end |
| 119 | end |
| 120 | |
| 121 | note Feed a small buffer buf[0..n) (n <= 4096) into streaming SHA-256 context c. |
| 122 | fn rv2_sha_feed c edit uchar 104 w edit ulong 64 buf view uchar 4096 n ulong to void |
| 123 | if gt n 0 |
| 124 | absorb c buf n w |
| 125 | end |
| 126 | end |
| 127 | |
| 128 | note Feed u64 little-endian length prefix into SHA-256 context. |
| 129 | fn rv2_sha_u64le c edit uchar 104 w edit ulong 64 v ulong to void |
| 130 | tmp = new uchar 4096 0 |
| 131 | for i ulong 8 |
| 132 | sh = mul i 8 |
| 133 | put tmp i uchar and shr v sh 255 |
| 134 | end |
| 135 | absorb c tmp 8 w |
| 136 | end |
| 137 | |
| 138 | note Feed length-prefixed field (u64_le(n) || bytes[0..n)) into SHA-256 context. |
| 139 | fn rv2_sha_field c edit uchar 104 w edit ulong 64 buf view uchar 4096 n ulong to void |
| 140 | rv2_sha_u64le c w n |
| 141 | rv2_sha_feed c w buf n |
| 142 | end |
| 143 | |
| 144 | note Feed length-prefixed 8-byte u64 little-endian field (len=8, 8 bytes). |
| 145 | fn rv2_sha_field_u64 c edit uchar 104 w edit ulong 64 v ulong to void |
| 146 | rv2_sha_u64le c w 8 |
| 147 | rv2_sha_u64le c w v |
| 148 | end |
| 149 | |
| 150 | note Feed length-prefixed 1-byte flag field (len=1, 0 or 1). |
| 151 | fn rv2_sha_field_flag c edit uchar 104 w edit ulong 64 flag ulong to void |
| 152 | rv2_sha_u64le c w 1 |
| 153 | tmp = new uchar 4096 0 |
| 154 | if ne flag 0 |
| 155 | put tmp 0 uchar 1 |
| 156 | end |
| 157 | absorb c tmp 1 w |
| 158 | end |
| 159 | |
| 160 | note Feed domain text rv2_dom_receipt. |
| 161 | fn rv2_sha_dom_receipt c edit uchar 104 w edit ulong 64 to void |
| 162 | n = len rv2_dom_receipt |
| 163 | tmp = new uchar 4096 0 |
| 164 | for i ulong n bound 64 |
| 165 | put tmp i get rv2_dom_receipt i |
| 166 | end |
| 167 | rv2_sha_field c w tmp n |
| 168 | end |
| 169 | |
| 170 | note Feed a 64-byte global string slice of length n. |
| 171 | fn rv2_feed_g64 c edit uchar 104 w edit ulong 64 which ulong n ulong to void |
| 172 | tmp = new uchar 4096 0 |
| 173 | for i ulong n bound 64 |
| 174 | if eq which 0 |
| 175 | put tmp i get rv2_cycle i |
| 176 | else |
| 177 | if eq which 1 |
| 178 | put tmp i get rv2_cand i |
| 179 | else |
| 180 | if eq which 2 |
| 181 | put tmp i get rv2_parent i |
| 182 | else |
| 183 | if eq which 3 |
| 184 | put tmp i get rv2_eval_at i |
| 185 | else |
| 186 | if eq which 4 |
| 187 | put tmp i get rv2_eval_ver i |
| 188 | else |
| 189 | if eq which 5 |
| 190 | put tmp i get rv2_eval_bin_hex i |
| 191 | else |
| 192 | if eq which 6 |
| 193 | put tmp i get rv2_subj_hex i |
| 194 | else |
| 195 | if eq which 7 |
| 196 | put tmp i get rv2_holdout_hex i |
| 197 | else |
| 198 | put tmp i get rv2_policy_hex i |
| 199 | end |
| 200 | end |
| 201 | end |
| 202 | end |
| 203 | end |
| 204 | end |
| 205 | end |
| 206 | end |
| 207 | end |
| 208 | rv2_sha_field c w tmp n |
| 209 | end |
| 210 | |
| 211 | note Feed rv2_subj_path[0..n). |
| 212 | fn rv2_feed_subj_path c edit uchar 104 w edit ulong 64 n ulong to void |
| 213 | tmp = new uchar 4096 0 |
| 214 | for i ulong n bound 128 |
| 215 | put tmp i get rv2_subj_path i |
| 216 | end |
| 217 | rv2_sha_field c w tmp n |
| 218 | end |
| 219 | |
| 220 | note Feed layer idx into SHA-256 context (violations count = 0). |
| 221 | fn rv2_feed_layer c edit uchar 104 w edit ulong 64 idx ulong to void |
| 222 | tmp = new uchar 4096 0 |
| 223 | nlen = get rv2_lr_nlen idx |
| 224 | nbase = mul idx 64 |
| 225 | for i ulong nlen bound 64 |
| 226 | put tmp i get rv2_lr_name add nbase i |
| 227 | end |
| 228 | rv2_sha_field c w tmp nlen |
| 229 | rv2_sha_field_flag c w get rv2_lr_hard idx |
| 230 | rv2_sha_field_flag c w get rv2_lr_pass idx |
| 231 | rv2_sha_field_u64 c w get rv2_lr_score_bits idx |
| 232 | slen = get rv2_lr_slen idx |
| 233 | sbase = mul idx 128 |
| 234 | for i ulong slen bound 128 |
| 235 | put tmp i get rv2_lr_sum add sbase i |
| 236 | end |
| 237 | rv2_sha_field c w tmp slen |
| 238 | rv2_sha_field_u64 c w 0 |
| 239 | end |
| 240 | |
| 241 | note Encode 32 raw bytes from SHA-256 final output packed in w[0..4) (byte k = (w[k/8] >> ((k&7)*8)) & 255) |
| 242 | note into rv2_comp_dig and rv2_comp_hex (exact match with p256.om p2dig). |
| 243 | fn rv2_store_digest w view ulong 64 to void |
| 244 | for i ulong 32 |
| 245 | bv = and shr get w shr i 3 mul and i 7 8 255 |
| 246 | put rv2_comp_dig i uchar bv |
| 247 | hi = shr bv 4 |
| 248 | lo = and bv 15 |
| 249 | put rv2_comp_hex mul i 2 get rv2_hex_tbl hi |
| 250 | put rv2_comp_hex add mul i 2 1 get rv2_hex_tbl lo |
| 251 | end |
| 252 | end |
| 253 | |
| 254 | note Compute exact V2 receipt digest (docs/RECEIPT-V2.md) into rv2_comp_dig / rv2_comp_hex. |
| 255 | fn rv2_compute_digest to void |
| 256 | c = new uchar 104 0 |
| 257 | w = new ulong 64 0 |
| 258 | init c |
| 259 | rv2_sha_dom_receipt c w |
| 260 | rv2_feed_g64 c w 0 get rv2_meta 9 |
| 261 | rv2_feed_g64 c w 1 get rv2_meta 10 |
| 262 | rv2_feed_g64 c w 2 get rv2_meta 11 |
| 263 | rv2_feed_g64 c w 3 get rv2_meta 12 |
| 264 | rv2_feed_g64 c w 4 get rv2_meta 13 |
| 265 | rv2_feed_g64 c w 5 64 |
| 266 | rv2_feed_subj_path c w get rv2_meta 14 |
| 267 | rv2_feed_g64 c w 6 64 |
| 268 | rv2_feed_g64 c w 7 64 |
| 269 | rv2_feed_g64 c w 8 64 |
| 270 | rv2_sha_field_u64 c w get rv2_meta 1 |
| 271 | rv2_sha_field_u64 c w get rv2_meta 2 |
| 272 | rv2_sha_field_flag c w get rv2_meta 3 |
| 273 | rv2_sha_field_flag c w get rv2_meta 4 |
| 274 | rv2_sha_field_flag c w get rv2_meta 5 |
| 275 | lcnt = get rv2_meta 6 |
| 276 | rv2_sha_field_u64 c w lcnt |
| 277 | for i ulong lcnt bound 8 |
| 278 | rv2_feed_layer c w i |
| 279 | end |
| 280 | if eq get rv2_meta 7 0 |
| 281 | rv2_sha_field_flag c w 0 |
| 282 | else |
| 283 | rv2_sha_field_flag c w 1 |
| 284 | for m ulong 5 |
| 285 | rv2_sha_field_u64 c w get rv2_metrics_bits m |
| 286 | end |
| 287 | rv2_sha_field_u64 c w get rv2_meta 8 |
| 288 | end |
| 289 | final c w |
| 290 | rv2_store_digest w |
| 291 | end |
| 292 | |
| 293 | note Unpack 32 SHA-256 bytes packed in w[0..4) into out32. |
| 294 | fn rv2_unpack_w w view ulong 64 out32 edit uchar 32 to void |
| 295 | for i ulong 32 |
| 296 | v = get w shr i 3 |
| 297 | sh = mul and i 7 8 |
| 298 | put out32 i uchar and shr v sh 255 |
| 299 | end |
| 300 | end |
| 301 | |
| 302 | note Compute SHA-256 of buf[0..n) (n <= 4096) into out32 (raw 32 bytes). |
| 303 | fn rv2_sha256_raw buf view uchar 4096 n ulong out32 edit uchar 32 to void |
| 304 | c = new uchar 104 0 |
| 305 | w = new ulong 64 0 |
| 306 | init c |
| 307 | rv2_sha_feed c w buf n |
| 308 | final c w |
| 309 | rv2_unpack_w w out32 |
| 310 | end |
| 311 | |
| 312 | note Compute exact spark-rsi.holdout-set.v1 digest for one sorted holdout file (name, file_sha256_32). |
| 313 | fn rv2_holdout_digest_1 fname view uchar 64 flen ulong fhash view uchar 32 out32 edit uchar 32 to void |
| 314 | c = new uchar 104 0 |
| 315 | w = new ulong 64 0 |
| 316 | tmp = new uchar 4096 0 |
| 317 | init c |
| 318 | dn = len rv2_dom_holdout |
| 319 | for i ulong dn bound 64 |
| 320 | put tmp i get rv2_dom_holdout i |
| 321 | end |
| 322 | rv2_sha_field c w tmp dn |
| 323 | rv2_sha_field_u64 c w 1 |
| 324 | for i ulong flen bound 64 |
| 325 | put tmp i get fname i |
| 326 | end |
| 327 | rv2_sha_field c w tmp flen |
| 328 | for i ulong 32 |
| 329 | put tmp i get fhash i |
| 330 | end |
| 331 | rv2_sha_field c w tmp 32 |
| 332 | final c w |
| 333 | rv2_unpack_w w out32 |
| 334 | end |
| 335 | |
| 336 | note Derive 65-byte uncompressed SEC1 public key from 32-byte P-256 private scalar d. |
| 337 | fn rv2_p256_pubkey priv32 view uchar 32 pub65 edit uchar 65 to ulong |
| 338 | pw = new ulong 1024 0 |
| 339 | pb = new uchar 4096 0 |
| 340 | p2init pw |
| 341 | for i ulong 32 |
| 342 | put pb add PBKEY i get priv32 i |
| 343 | end |
| 344 | rc = p2pub pw pb |
| 345 | if ne rc 0 |
| 346 | ret rc |
| 347 | end |
| 348 | for i ulong 65 |
| 349 | put pub65 i get pb add PBPUB i |
| 350 | end |
| 351 | ret 0 |
| 352 | end |
| 353 | |
| 354 | note Sign the 32-byte raw V2 digest dig32 with 32-byte P-256 private key priv32 into sig64. |
| 355 | note Matches spark-rsi-mv src/evaluator/mod.rs (signing_key.sign(&digest_bytes)) byte-for-byte. |
| 356 | fn rv2_p256_sign priv32 view uchar 32 dig32 view uchar 32 sig64 edit uchar 64 to ulong |
| 357 | pw = new ulong 1024 0 |
| 358 | pc = new uchar 104 0 |
| 359 | pb = new uchar 4096 0 |
| 360 | ps = new ulong 64 0 |
| 361 | p2init pw |
| 362 | for i ulong 32 |
| 363 | put pb add PBKEY i get priv32 i |
| 364 | put pb add PBMSG i get dig32 i |
| 365 | end |
| 366 | rc = p2sign pw pc pb ps 32 |
| 367 | if ne rc 0 |
| 368 | ret rc |
| 369 | end |
| 370 | for i ulong 64 |
| 371 | put sig64 i get pb add PBSIG i |
| 372 | end |
| 373 | ret 0 |
| 374 | end |
| 375 | |
| 376 | note Verify 64-byte P-256 signature sig64 over 32-byte raw V2 digest dig32 under pub65. |
| 377 | note Returns 1 if valid, 0 if invalid. |
| 378 | fn rv2_p256_verify pub65 view uchar 65 dig32 view uchar 32 sig64 view uchar 64 to ulong |
| 379 | pw = new ulong 1024 0 |
| 380 | pc = new uchar 104 0 |
| 381 | pb = new uchar 4096 0 |
| 382 | ps = new ulong 64 0 |
| 383 | p2init pw |
| 384 | for i ulong 65 |
| 385 | put pb add PBPUB i get pub65 i |
| 386 | end |
| 387 | for i ulong 32 |
| 388 | put pb add PBMSG i get dig32 i |
| 389 | end |
| 390 | for i ulong 64 |
| 391 | put pb add PBSIG i get sig64 i |
| 392 | end |
| 393 | ret p2vrfy pw pc pb ps 32 |
| 394 | end |
| 395 | |
| 396 | note Check uncompressed SEC1 key validity (0x04 prefix and point on NIST P-256 curve). |
| 397 | fn rv2_key_on_curve pub65 view uchar 65 to bool |
| 398 | if ne get pub65 0 uchar 4 |
| 399 | ret false |
| 400 | end |
| 401 | pw = new ulong 1024 0 |
| 402 | pb = new uchar 4096 0 |
| 403 | p2init pw |
| 404 | for i ulong 65 |
| 405 | put pb add PBPUB i get pub65 i |
| 406 | end |
| 407 | p2ld pw PX pb add PBPUB 1 |
| 408 | p2ld pw PY pb add PBPUB 33 |
| 409 | ret p2oncv pw |
| 410 | end |
| 411 | |
| 412 | note Validate clean relative path: non-empty, no leading/trailing '/', no '//', no '.' or '..' segment. |
| 413 | fn rv2_clean_rel_path buf view uchar 128 plen ulong to bool |
| 414 | if or eq plen 0 gt plen 128 |
| 415 | ret false |
| 416 | end |
| 417 | if or eq get buf 0 uchar 47 eq get buf sub plen 1 uchar 47 |
| 418 | ret false |
| 419 | end |
| 420 | seg_start = ulong 0 |
| 421 | i = ulong 0 |
| 422 | while le i plen bound 130 |
| 423 | at_sep = false |
| 424 | if eq i plen |
| 425 | at_sep = true |
| 426 | else |
| 427 | b = get buf i |
| 428 | if or eq b uchar 0 eq b uchar 92 |
| 429 | ret false |
| 430 | end |
| 431 | if eq b uchar 47 |
| 432 | at_sep = true |
| 433 | end |
| 434 | end |
| 435 | if at_sep |
| 436 | slen = sub i seg_start |
| 437 | if eq slen 0 |
| 438 | ret false |
| 439 | end |
| 440 | if eq slen 1 |
| 441 | if eq get buf seg_start uchar 46 |
| 442 | ret false |
| 443 | end |
| 444 | end |
| 445 | if eq slen 2 |
| 446 | if eq get buf seg_start uchar 46 |
| 447 | if eq get buf add seg_start 1 uchar 46 |
| 448 | ret false |
| 449 | end |
| 450 | end |
| 451 | end |
| 452 | seg_start = add i 1 |
| 453 | end |
| 454 | i = add i 1 |
| 455 | end |
| 456 | ret true |
| 457 | end |
| 458 | |
| 459 | note Check if path[0..plen) equals prefix[0..xlen) or starts with prefix + '/'. |
| 460 | fn rv2_under_prefix path view uchar 128 plen ulong pref view uchar 128 xlen ulong to bool |
| 461 | if or eq xlen 0 lt plen xlen |
| 462 | ret false |
| 463 | end |
| 464 | for i ulong xlen bound 128 |
| 465 | if ne get path i get pref i |
| 466 | ret false |
| 467 | end |
| 468 | end |
| 469 | if eq plen xlen |
| 470 | ret true |
| 471 | end |
| 472 | ret eq get path xlen uchar 47 |
| 473 | end |
| 474 | |
| 475 | note Compare two 64-byte global hex digests. |
| 476 | fn rv2_hex64_eq_g a_which ulong b_which ulong to bool |
| 477 | for i ulong 64 |
| 478 | va = uchar 0 |
| 479 | vb = uchar 0 |
| 480 | if eq a_which 0 |
| 481 | va = get rv2_sub_content_hex i |
| 482 | else |
| 483 | if eq a_which 1 |
| 484 | va = get rv2_subj_hex i |
| 485 | else |
| 486 | if eq a_which 2 |
| 487 | va = get rv2_policy_hex i |
| 488 | else |
| 489 | va = get rv2_holdout_hex i |
| 490 | end |
| 491 | end |
| 492 | end |
| 493 | if eq b_which 0 |
| 494 | vb = get rv2_sub_disk_hex i |
| 495 | else |
| 496 | if eq b_which 1 |
| 497 | vb = get rv2_sub_content_hex i |
| 498 | else |
| 499 | if eq b_which 2 |
| 500 | vb = get rv2_pol_sha_hex i |
| 501 | else |
| 502 | vb = get rv2_pol_holdout_hex i |
| 503 | end |
| 504 | end |
| 505 | end |
| 506 | if ne va vb |
| 507 | ret false |
| 508 | end |
| 509 | end |
| 510 | ret true |
| 511 | end |
| 512 | |
| 513 | note Check if rv2_subj_path is in rv2_pol_allowed[0..allowed_cnt). |
| 514 | fn rv2_in_allowed to bool |
| 515 | acnt = get rv2_pol_meta 2 |
| 516 | slen = get rv2_meta 14 |
| 517 | for k ulong acnt bound 8 |
| 518 | alen = get rv2_pol_alen k |
| 519 | if eq alen slen |
| 520 | base = mul k 128 |
| 521 | same = true |
| 522 | for i ulong slen bound 128 |
| 523 | if ne get rv2_subj_path i get rv2_pol_allowed add base i |
| 524 | same = false |
| 525 | break |
| 526 | end |
| 527 | end |
| 528 | if same |
| 529 | ret true |
| 530 | end |
| 531 | end |
| 532 | end |
| 533 | ret false |
| 534 | end |
| 535 | |
| 536 | note Check if rv2_subj_path lies under any rv2_pol_prot[0..protected_cnt). |
| 537 | fn rv2_in_protected to bool |
| 538 | pcnt = get rv2_pol_meta 3 |
| 539 | slen = get rv2_meta 14 |
| 540 | sp = new uchar 128 0 |
| 541 | for i ulong slen bound 128 |
| 542 | put sp i get rv2_subj_path i |
| 543 | end |
| 544 | pr = new uchar 128 0 |
| 545 | for k ulong pcnt bound 8 |
| 546 | xlen = get rv2_pol_plen k |
| 547 | base = mul k 128 |
| 548 | for j ulong xlen bound 128 |
| 549 | put pr j get rv2_pol_prot add base j |
| 550 | end |
| 551 | if rv2_under_prefix sp slen pr xlen |
| 552 | ret true |
| 553 | end |
| 554 | end |
| 555 | ret false |
| 556 | end |
| 557 | |
| 558 | note Verify judge/own key separation and P-256 signature over rv2_comp_dig. |
| 559 | fn rv2_check_keys_and_sig to ulong |
| 560 | jpub = new uchar 65 0 |
| 561 | opub = new uchar 65 0 |
| 562 | dig = new uchar 32 0 |
| 563 | sig = new uchar 64 0 |
| 564 | same_key = true |
| 565 | for i ulong 65 |
| 566 | jb = get rv2_judge_pub i |
| 567 | ob = get rv2_own_pub i |
| 568 | put jpub i jb |
| 569 | put opub i ob |
| 570 | if ne jb ob |
| 571 | same_key = false |
| 572 | end |
| 573 | end |
| 574 | if not rv2_key_on_curve jpub |
| 575 | ret RV2_ERR_BAD_JUDGE_KEY |
| 576 | end |
| 577 | if same_key |
| 578 | ret RV2_ERR_SAME_KEY |
| 579 | end |
| 580 | for i ulong 32 |
| 581 | put dig i get rv2_comp_dig i |
| 582 | end |
| 583 | for i ulong 64 |
| 584 | put sig i get rv2_sig i |
| 585 | end |
| 586 | if ne rv2_p256_verify jpub dig sig 1 |
| 587 | ret RV2_ERR_BAD_SIGNATURE |
| 588 | end |
| 589 | ret RV2_OK |
| 590 | end |
| 591 | |
| 592 | note Fail-closed Version 2 Promotion Gate (exact mirror of spark-rsi-mv/src/promotion_gate.rs). |
| 593 | fn rv2_check_promotion to ulong |
| 594 | note 1. Staged disk hash must equal change content hash |
| 595 | if not rv2_hex64_eq_g 0 0 |
| 596 | ret RV2_ERR_DISK_HASH_MISMATCH |
| 597 | end |
| 598 | note 2. Policy invariants: require_admitted=1, min_ratio_bps in 1..10000, allowed_cnt >= 1 |
| 599 | if eq get rv2_pol_meta 0 0 |
| 600 | ret RV2_ERR_BAD_POLICY |
| 601 | end |
| 602 | min_bps = get rv2_pol_meta 1 |
| 603 | if or eq min_bps 0 gt min_bps 10000 |
| 604 | ret RV2_ERR_BAD_POLICY |
| 605 | end |
| 606 | if eq get rv2_pol_meta 2 0 |
| 607 | ret RV2_ERR_BAD_POLICY |
| 608 | end |
| 609 | note 3. Format version must be strictly 2 (v1 never promotes) |
| 610 | if ne get rv2_meta 0 2 |
| 611 | ret RV2_ERR_NOT_V2 |
| 612 | end |
| 613 | note 4. Recompute V2 digest and verify equality with claimed digest |
| 614 | rv2_compute_digest |
| 615 | for i ulong 32 |
| 616 | if ne get rv2_comp_dig i get rv2_claimed_dig i |
| 617 | ret RV2_ERR_BAD_DIGEST |
| 618 | end |
| 619 | end |
| 620 | note 5. Verify pinned judge P-256 key validity, judge != own key, and ECDSA P-256 signature |
| 621 | krc = rv2_check_keys_and_sig |
| 622 | if ne krc RV2_OK |
| 623 | ret krc |
| 624 | end |
| 625 | note 6. Subject hash, policy hash, and holdout set hash must match pins |
| 626 | if not rv2_hex64_eq_g 1 1 |
| 627 | ret RV2_ERR_SUBJECT_HASH |
| 628 | end |
| 629 | if not rv2_hex64_eq_g 2 2 |
| 630 | ret RV2_ERR_POLICY_HASH |
| 631 | end |
| 632 | if not rv2_hex64_eq_g 3 3 |
| 633 | ret RV2_ERR_HOLDOUT_HASH |
| 634 | end |
| 635 | note 7. Admission and hard/statistical gate flags must all be true |
| 636 | if or eq get rv2_meta 3 0 or eq get rv2_meta 4 0 eq get rv2_meta 5 0 |
| 637 | ret RV2_ERR_NOT_ADMITTED |
| 638 | end |
| 639 | note 8. Parent id must match subject parent id |
| 640 | plen = get rv2_meta 11 |
| 641 | if ne plen get rv2_pol_meta 4 |
| 642 | ret RV2_ERR_PARENT_MISMATCH |
| 643 | end |
| 644 | for i ulong plen bound 64 |
| 645 | if ne get rv2_parent i get rv2_sub_parent i |
| 646 | ret RV2_ERR_PARENT_MISMATCH |
| 647 | end |
| 648 | end |
| 649 | note 9. Subject path must be clean relative path and match subject |
| 650 | slen = get rv2_meta 14 |
| 651 | if ne slen get rv2_pol_meta 5 |
| 652 | ret RV2_ERR_SUBJECT_PATH |
| 653 | end |
| 654 | sp = new uchar 128 0 |
| 655 | for i ulong slen bound 128 |
| 656 | b = get rv2_subj_path i |
| 657 | if ne b get rv2_sub_path i |
| 658 | ret RV2_ERR_SUBJECT_PATH |
| 659 | end |
| 660 | put sp i b |
| 661 | end |
| 662 | if not rv2_clean_rel_path sp slen |
| 663 | ret RV2_ERR_SUBJECT_PATH |
| 664 | end |
| 665 | note 10. Protected paths refused; must appear in allowed_targets |
| 666 | if rv2_in_protected |
| 667 | ret RV2_ERR_PROTECTED_PATH |
| 668 | end |
| 669 | if not rv2_in_allowed |
| 670 | ret RV2_ERR_NOT_ALLOWED_TARGET |
| 671 | end |
| 672 | note 11. Holdout pass ratio check (exact integer: passed * 10000 >= min_ratio_bps * total) |
| 673 | htot = get rv2_meta 1 |
| 674 | hpas = get rv2_meta 2 |
| 675 | if or eq htot 0 gt hpas htot |
| 676 | ret RV2_ERR_HOLDOUT_THRESHOLD |
| 677 | end |
| 678 | if lt mul hpas 10000 mul min_bps htot |
| 679 | ret RV2_ERR_HOLDOUT_THRESHOLD |
| 680 | end |
| 681 | ret RV2_OK |
| 682 | end |