omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / core / rsi / om / eval / canary.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note core/rsi/om/eval/canary.om |
| 3 | note Pure OM-2 conversion of spark-rsi-mv/src/canary/{admission,identity,observation,workload}.rs. |
| 4 | note Canary identity binding (SHA-256), non-synthetic provenance enforcement, |
| 5 | note observation window accumulation, and fail-closed canary admission gate. |
| 6 | note Compose after om/tls/sha2.om. |
| 7 | |
| 8 | note Canary provenance |
| 9 | const CNR_PROV_SYNTHETIC ulong 0 |
| 10 | const CNR_PROV_CANARY_HOST ulong 1 |
| 11 | const CNR_PROV_SILICON ulong 2 |
| 12 | |
| 13 | note Canary verdicts |
| 14 | const CNR_PASS ulong 0 |
| 15 | const CNR_HOLD_INSUFFICIENT_SAMPLES ulong 1 |
| 16 | const CNR_DENY_UNINIT ulong 10 |
| 17 | const CNR_DENY_UNBOUND_ID ulong 11 |
| 18 | const CNR_DENY_SYNTHETIC ulong 12 |
| 19 | const CNR_DENY_CRASH ulong 13 |
| 20 | const CNR_DENY_ERROR_RATE ulong 14 |
| 21 | const CNR_DENY_LATENCY ulong 15 |
| 22 | const CNR_DENY_ID_MISMATCH ulong 16 |
| 23 | |
| 24 | text cnr_id_dom "RSI-CANARY-ID-V1" |
| 25 | |
| 26 | note Config: [0]=init, [1]=min_samples, [2]=max_err_bps, [3]=max_p99_reg_bps |
| 27 | global cnr_cfg ulong 8 |
| 28 | |
| 29 | note Active canary session state: |
| 30 | note [0]=bound(0/1), [1]=cand_id, [2]=provenance, [3]=samples, [4]=errors, [5]=crashes, [6]=base_p99_ns, [7]=max_obs_p99_ns |
| 31 | global cnr_state ulong 8 |
| 32 | global cnr_id_hash uchar 32 |
| 33 | |
| 34 | fn cnr_init min_samples ulong max_err_bps ulong max_p99_reg_bps ulong to void |
| 35 | put cnr_cfg 0 1 |
| 36 | put cnr_cfg 1 min_samples |
| 37 | put cnr_cfg 2 max_err_bps |
| 38 | put cnr_cfg 3 max_p99_reg_bps |
| 39 | for i ulong 8 |
| 40 | put cnr_state i 0 |
| 41 | end |
| 42 | for i ulong 32 |
| 43 | put cnr_id_hash i uchar 0 |
| 44 | end |
| 45 | end |
| 46 | |
| 47 | fn cnr_is_zero_hash h view uchar 32 to bool |
| 48 | acc = uchar 0 |
| 49 | for i ulong 32 |
| 50 | acc = or acc get h i |
| 51 | end |
| 52 | ret eq acc uchar 0 |
| 53 | end |
| 54 | |
| 55 | fn cnr_hash_eq a view uchar 32 b view uchar 32 to bool |
| 56 | diff = uchar 0 |
| 57 | for i ulong 32 |
| 58 | diff = or diff xor get a i get b i |
| 59 | end |
| 60 | ret eq diff uchar 0 |
| 61 | end |
| 62 | |
| 63 | fn cnr_put_u64 buf edit uchar * off ulong v ulong to void |
| 64 | for i ulong 8 |
| 65 | sh = mul i 8 |
| 66 | put buf add off i uchar and shr v sh ulong 255 |
| 67 | end |
| 68 | end |
| 69 | |
| 70 | note Compute canonical SHA-256 canary identity binding from cand_id and artifact digest. |
| 71 | fn cnr_compute_identity cand_id ulong art_digest view uchar 32 out_id edit uchar 32 to void |
| 72 | ctx = new ulong 40 0 |
| 73 | sha256_init ctx |
| 74 | sha256_update ctx cnr_id_dom |
| 75 | hdr = new uchar 8 0 |
| 76 | cnr_put_u64 hdr 0 cand_id |
| 77 | sha256_update ctx hdr |
| 78 | sha256_update ctx art_digest |
| 79 | sha256_final ctx out_id |
| 80 | end |
| 81 | |
| 82 | note Admit a candidate into the canary observation window with cryptographic identity binding. |
| 83 | fn cnr_begin cand_id ulong provenance ulong base_p99_ns ulong art_digest view uchar 32 to ulong |
| 84 | if eq get cnr_cfg 0 0 |
| 85 | ret CNR_DENY_UNINIT |
| 86 | end |
| 87 | if or eq provenance CNR_PROV_SYNTHETIC gt provenance CNR_PROV_SILICON |
| 88 | ret CNR_DENY_SYNTHETIC |
| 89 | end |
| 90 | if or eq base_p99_ns 0 cnr_is_zero_hash art_digest |
| 91 | ret CNR_DENY_UNBOUND_ID |
| 92 | end |
| 93 | for i ulong 8 |
| 94 | put cnr_state i 0 |
| 95 | end |
| 96 | id_h = new uchar 32 0 |
| 97 | cnr_compute_identity cand_id art_digest id_h |
| 98 | for i ulong 32 |
| 99 | put cnr_id_hash i get id_h i |
| 100 | end |
| 101 | put cnr_state 0 1 |
| 102 | put cnr_state 1 cand_id |
| 103 | put cnr_state 2 provenance |
| 104 | put cnr_state 6 base_p99_ns |
| 105 | ret CNR_PASS |
| 106 | end |
| 107 | |
| 108 | note Record one canary workload observation batch. |
| 109 | fn cnr_observe reqs ulong errs ulong crashes ulong obs_p99_ns ulong to ulong |
| 110 | if eq get cnr_state 0 0 |
| 111 | ret CNR_DENY_UNBOUND_ID |
| 112 | end |
| 113 | put cnr_state 3 add get cnr_state 3 reqs |
| 114 | put cnr_state 4 add get cnr_state 4 errs |
| 115 | put cnr_state 5 add get cnr_state 5 crashes |
| 116 | if gt obs_p99_ns get cnr_state 7 |
| 117 | put cnr_state 7 obs_p99_ns |
| 118 | end |
| 119 | ret CNR_PASS |
| 120 | end |
| 121 | |
| 122 | note Verify canary identity binding and evaluate all canary admission criteria. |
| 123 | fn cnr_evaluate cand_id ulong art_digest view uchar 32 to ulong |
| 124 | if eq get cnr_cfg 0 0 |
| 125 | ret CNR_DENY_UNINIT |
| 126 | end |
| 127 | if eq get cnr_state 0 0 |
| 128 | ret CNR_DENY_UNBOUND_ID |
| 129 | end |
| 130 | if ne get cnr_state 1 cand_id |
| 131 | ret CNR_DENY_ID_MISMATCH |
| 132 | end |
| 133 | exp_id = new uchar 32 0 |
| 134 | cnr_compute_identity cand_id art_digest exp_id |
| 135 | cur_id = new uchar 32 0 |
| 136 | for i ulong 32 |
| 137 | put cur_id i get cnr_id_hash i |
| 138 | end |
| 139 | if not cnr_hash_eq exp_id cur_id |
| 140 | ret CNR_DENY_ID_MISMATCH |
| 141 | end |
| 142 | prov = get cnr_state 2 |
| 143 | if or eq prov CNR_PROV_SYNTHETIC gt prov CNR_PROV_SILICON |
| 144 | ret CNR_DENY_SYNTHETIC |
| 145 | end |
| 146 | if gt get cnr_state 5 0 |
| 147 | ret CNR_DENY_CRASH |
| 148 | end |
| 149 | samples = get cnr_state 3 |
| 150 | if lt samples get cnr_cfg 1 |
| 151 | ret CNR_HOLD_INSUFFICIENT_SAMPLES |
| 152 | end |
| 153 | errs = get cnr_state 4 |
| 154 | if gt errs samples |
| 155 | ret CNR_DENY_ERROR_RATE |
| 156 | end |
| 157 | err_bps = div mul errs 10000 samples |
| 158 | if gt err_bps get cnr_cfg 2 |
| 159 | ret CNR_DENY_ERROR_RATE |
| 160 | end |
| 161 | base_p99 = get cnr_state 6 |
| 162 | obs_p99 = get cnr_state 7 |
| 163 | if gt obs_p99 base_p99 |
| 164 | reg_bps = div mul sub obs_p99 base_p99 10000 base_p99 |
| 165 | if gt reg_bps get cnr_cfg 3 |
| 166 | ret CNR_DENY_LATENCY |
| 167 | end |
| 168 | end |
| 169 | ret CNR_PASS |
| 170 | end |