open-humanity
Privacy-first peer assistance network for sovereign autonomous agents
Brought by drakestapleton
open-humanity / om / trace_contract_test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note protocol/open-humanity/trace_contract_test.om |
| 3 | note Positive, negative, and cryptographic tamper tests for the exact-byte Open Humanity |
| 4 | note UDP wire codec (beacon-core parity) and AITR v1 consent/export + trace-receiver contract. |
| 5 | note Compose with: |
| 6 | note si/directives/om2.om om/blake3/b3unit.om protocol/omnet/om/sha512/sha512.om |
| 7 | note protocol/omnet/om/ed25519/ed25519.om protocol/open-humanity/lib.om protocol/open-humanity/trace_contract.om |
| 8 | note protocol/open-humanity/trace_contract_test.om |
| 9 | |
| 10 | text tc_pass "PASS " |
| 11 | text tc_fail "FAIL " |
| 12 | text tc_nl "\n" |
| 13 | text tc_ok_all "OPEN-HUMANITY EXACT-BYTE WIRE & TRACE-RECEIVER TEST SUITE PASS\n" |
| 14 | text tc_bad_all "OPEN-HUMANITY EXACT-BYTE WIRE & TRACE-RECEIVER TEST SUITE FAIL\n" |
| 15 | |
| 16 | text tc_n1 "wire_challenge_ack_register_and_keyed_cookie" |
| 17 | text tc_n2 "wire_negative_tamper_drift_and_wrong_key" |
| 18 | text tc_n3 "error_fingerprint_path_scrub_and_ws_parity" |
| 19 | text tc_n4 "trace_consent_export_and_receive_aitr_v1" |
| 20 | text tc_n5 "trace_negative_no_consent_zero_auth_firewall_secret" |
| 21 | text tc_n6 "trace_negative_replay_wrong_receiver_crc_and_tamper" |
| 22 | |
| 23 | text tc_addr_a "192.0.2.1:4000" |
| 24 | text tc_addr_b "192.0.2.1:4001" |
| 25 | text tc_err_a "error at /home/alice/proj/src/lib.rs: cannot compile" |
| 26 | text tc_err_b "error at /Users/bob/proj/src/lib.rs: cannot compile\n" |
| 27 | text tc_pay_ok0 "kernel=sm121 lat_us=412 ok" |
| 28 | text tc_pay_ok1 "kv_hits=128 miss=2" |
| 29 | text tc_pay_skip "internal_debug_type_5_excluded" |
| 30 | text tc_pay_sec "leak AKIAIOSFODNN7EXAMPLE here" |
| 31 | text tc_pay_path "trace at /home/drake/secret/file" |
| 32 | text tc_pay_esc "run sudo rm now" |
| 33 | |
| 34 | global tc_fails ulong 1 |
| 35 | |
| 36 | fn tc_w s view uchar * to void |
| 37 | ok = oh_write_all 1 s |
| 38 | if ok |
| 39 | ret |
| 40 | end |
| 41 | end |
| 42 | |
| 43 | fn tc_rep name view uchar * ok bool to void |
| 44 | if ok |
| 45 | tc_w tc_pass |
| 46 | else |
| 47 | tc_w tc_fail |
| 48 | put tc_fails 0 add get tc_fails 0 1 |
| 49 | end |
| 50 | tc_w name |
| 51 | tc_w tc_nl |
| 52 | end |
| 53 | |
| 54 | fn tc_check_register ck view uchar 16 now ulong to bool |
| 55 | seed = new uchar 32 17 |
| 56 | reg_wire = new uchar 133 0 |
| 57 | if ne ohw_encode_register seed now uchar 5 uchar 1 ck reg_wire 133 |
| 58 | ret false |
| 59 | end |
| 60 | out_pk = new uchar 32 0 |
| 61 | rmeta = new ulong 3 0 |
| 62 | if ne ohw_verify_register reg_wire now out_pk rmeta OHTX_OK |
| 63 | ret false |
| 64 | end |
| 65 | if ne get rmeta 0 now |
| 66 | ret false |
| 67 | end |
| 68 | if ne get rmeta 1 5 |
| 69 | ret false |
| 70 | end |
| 71 | ret eq get rmeta 2 1 |
| 72 | end |
| 73 | |
| 74 | note Test 1: Challenge, Ack, signed Register (133B bincode parity) + keyed BLAKE3 cookie windows. |
| 75 | fn test_tc_wire_pos to bool |
| 76 | secret = new uchar 32 9 |
| 77 | now = ulong 1000000 |
| 78 | ck = new uchar 16 0 |
| 79 | ohw_relay_cookie secret tc_addr_a div now OHW_COOKIE_WINDOW ck |
| 80 | if not ohw_cookie_valid secret tc_addr_a now ck |
| 81 | ret false |
| 82 | end |
| 83 | if not ohw_cookie_valid secret tc_addr_a add now OHW_COOKIE_WINDOW ck |
| 84 | ret false |
| 85 | end |
| 86 | if ohw_cookie_valid secret tc_addr_a add now mul 2 OHW_COOKIE_WINDOW ck |
| 87 | ret false |
| 88 | end |
| 89 | if ohw_cookie_valid secret tc_addr_b now ck |
| 90 | ret false |
| 91 | end |
| 92 | ch_wire = new uchar 19 0 |
| 93 | if ne ohw_encode_challenge ck ch_wire 19 |
| 94 | ret false |
| 95 | end |
| 96 | ck2 = new uchar 16 0 |
| 97 | if ne ohw_decode_challenge ch_wire ck2 OHTX_OK |
| 98 | ret false |
| 99 | end |
| 100 | for i ulong 16 |
| 101 | if ne get ck i get ck2 i |
| 102 | ret false |
| 103 | end |
| 104 | end |
| 105 | ack_wire = new uchar 8 0 |
| 106 | if ne ohw_encode_ack uchar 3 120 ack_wire 8 |
| 107 | ret false |
| 108 | end |
| 109 | ameta = new ulong 2 0 |
| 110 | if ne ohw_decode_ack ack_wire ameta OHTX_OK |
| 111 | ret false |
| 112 | end |
| 113 | if or ne get ameta 0 3 ne get ameta 1 120 |
| 114 | ret false |
| 115 | end |
| 116 | ret tc_check_register ck now |
| 117 | end |
| 118 | |
| 119 | note Test 2 (NEGATIVE): Register tamper, drift > 300s, bad magic/version refused. |
| 120 | fn test_tc_wire_neg to bool |
| 121 | seed = new uchar 32 17 |
| 122 | ck = new uchar 16 4 |
| 123 | now = ulong 1000000 |
| 124 | reg_wire = new uchar 133 0 |
| 125 | n = ohw_encode_register seed now uchar 1 uchar 0 ck reg_wire |
| 126 | if ne n 133 |
| 127 | ret false |
| 128 | end |
| 129 | out_pk = new uchar 32 0 |
| 130 | rmeta = new ulong 3 0 |
| 131 | note Drift > 300s |
| 132 | if ne ohw_verify_register reg_wire add now 305 out_pk rmeta OHTX_ERR_DRIFT |
| 133 | ret false |
| 134 | end |
| 135 | note Flip 1 bit in topics byte -> Ed25519 signature failure |
| 136 | put reg_wire 43 xor get reg_wire 43 uchar 1 |
| 137 | if ne ohw_verify_register reg_wire now out_pk rmeta OHTX_ERR_BAD_SIG |
| 138 | ret false |
| 139 | end |
| 140 | put reg_wire 43 xor get reg_wire 43 uchar 1 |
| 141 | note Bad version |
| 142 | put reg_wire 2 uchar 9 |
| 143 | if ne ohw_verify_register reg_wire now out_pk rmeta OHTX_ERR_BAD_VERSION |
| 144 | ret false |
| 145 | end |
| 146 | ret true |
| 147 | end |
| 148 | |
| 149 | note Test 3: Two different user home folders & whitespace yield identical BLAKE3 error fingerprint. |
| 150 | fn test_tc_fingerprint to bool |
| 151 | h1 = new uchar 32 0 |
| 152 | h2 = new uchar 32 0 |
| 153 | ohw_fingerprint_error tc_err_a h1 |
| 154 | ohw_fingerprint_error tc_err_b h2 |
| 155 | for i ulong 32 |
| 156 | if ne get h1 i get h2 i |
| 157 | ret false |
| 158 | end |
| 159 | end |
| 160 | ret true |
| 161 | end |
| 162 | |
| 163 | note Build a 3-record AITR v1 segment (types 1, 5, 2) -> total 16 + 3*64 = 208 bytes. |
| 164 | fn tc_build_clean_seg dst edit uchar 208 to bool |
| 165 | ohtx_aitr_write_hdr dst |
| 166 | if ne ohtx_aitr_write_rec dst 16 or ulong 1 shl ulong 0 16 tc_pay_ok0 OHTX_OK |
| 167 | ret false |
| 168 | end |
| 169 | if ne ohtx_aitr_write_rec dst 80 or ulong 5 shl ulong 1 16 tc_pay_skip OHTX_OK |
| 170 | ret false |
| 171 | end |
| 172 | if ne ohtx_aitr_write_rec dst 144 or ulong 2 shl ulong 2 16 tc_pay_ok1 OHTX_OK |
| 173 | ret false |
| 174 | end |
| 175 | ret true |
| 176 | end |
| 177 | |
| 178 | fn tc_check_out_seg seg_out view uchar 512 to bool |
| 179 | rcnt_box = new ulong 1 0 |
| 180 | if ne ohtx_aitr_validate_hdr part seg_out 0 144 rcnt_box OHTX_OK |
| 181 | ret false |
| 182 | end |
| 183 | if ne get rcnt_box 0 2 |
| 184 | ret false |
| 185 | end |
| 186 | rt = new ulong 1 0 |
| 187 | pl = new ulong 1 0 |
| 188 | if ne ohtx_aitr_validate_rec part seg_out 0 144 0 rt pl OHTX_OK |
| 189 | ret false |
| 190 | end |
| 191 | if or ne get rt 0 1 ne get pl 0 len tc_pay_ok0 |
| 192 | ret false |
| 193 | end |
| 194 | if ne ohtx_aitr_validate_rec part seg_out 0 144 1 rt pl OHTX_OK |
| 195 | ret false |
| 196 | end |
| 197 | ret and eq get rt 0 2 eq get pl 0 len tc_pay_ok1 |
| 198 | end |
| 199 | |
| 200 | note Test 4 (POSITIVE): Exact-byte consent/export filters types {1,2}, drops type 5, receiver verifies & outputs 2-rec AITR v1. |
| 201 | fn test_tc_trace_pos to bool |
| 202 | ohtx_reset_replay |
| 203 | node_seed = new uchar 32 21 |
| 204 | recv_seed = new uchar 32 22 |
| 205 | recv_pk = new uchar 32 0 |
| 206 | ohtx_ed_pubkey recv_seed recv_pk |
| 207 | now = ulong 1700000000 |
| 208 | consent = new uchar 160 0 |
| 209 | ohtx_sign_consent node_seed recv_pk 6 add now 3600 0 consent |
| 210 | seg_in = new uchar 208 0 |
| 211 | if not tc_build_clean_seg seg_in |
| 212 | ret false |
| 213 | end |
| 214 | bid = new uchar 16 77 |
| 215 | ohtx_set_bundle_id bid |
| 216 | bundle = new uchar 4408 0 |
| 217 | if ne oh_trace_export node_seed consent seg_in now bundle OHTX_OK |
| 218 | ret false |
| 219 | end |
| 220 | if ne ohtx_export_len 440 |
| 221 | ret false |
| 222 | end |
| 223 | seg_out = new uchar 512 0 |
| 224 | if ne oh_trace_receive part bundle 0 440 recv_pk now seg_out OHTX_OK |
| 225 | ret false |
| 226 | end |
| 227 | if ne ohtx_recv_len 144 |
| 228 | ret false |
| 229 | end |
| 230 | ret tc_check_out_seg seg_out |
| 231 | end |
| 232 | |
| 233 | fn tc_check_fw_neg node_seed view uchar 32 recv_pk view uchar 32 now ulong to bool |
| 234 | c_ok = new uchar 160 0 |
| 235 | ohtx_sign_consent node_seed recv_pk 6 add now 3600 0 c_ok |
| 236 | bad_seg = new uchar 80 0 |
| 237 | bundle = new uchar 4408 0 |
| 238 | ohtx_aitr_write_hdr bad_seg |
| 239 | if ne ohtx_aitr_write_rec bad_seg 16 or ulong 1 shl ulong 0 16 tc_pay_sec OHTX_OK |
| 240 | ret false |
| 241 | end |
| 242 | if ne oh_trace_export node_seed c_ok bad_seg now bundle OHTX_ERR_FIREWALL |
| 243 | ret false |
| 244 | end |
| 245 | if ne ohtx_export_len 0 |
| 246 | ret false |
| 247 | end |
| 248 | if ne ohtx_aitr_write_rec bad_seg 16 or ulong 1 shl ulong 0 16 tc_pay_path OHTX_OK |
| 249 | ret false |
| 250 | end |
| 251 | if ne oh_trace_export node_seed c_ok bad_seg now bundle OHTX_ERR_FIREWALL |
| 252 | ret false |
| 253 | end |
| 254 | if ne ohtx_aitr_write_rec bad_seg 16 or ulong 1 shl ulong 0 16 tc_pay_esc OHTX_OK |
| 255 | ret false |
| 256 | end |
| 257 | ret eq oh_trace_export node_seed c_ok bad_seg now bundle OHTX_ERR_ESCALATION |
| 258 | end |
| 259 | |
| 260 | note Test 5 (NEGATIVE): Missing opt-in, nonzero cap_flags, expired consent, and Personal Data Firewall secret/path/escalation refusals. |
| 261 | fn test_tc_trace_neg_consent_fw to bool |
| 262 | node_seed = new uchar 32 21 |
| 263 | recv_seed = new uchar 32 22 |
| 264 | recv_pk = new uchar 32 0 |
| 265 | ohtx_ed_pubkey recv_seed recv_pk |
| 266 | now = ulong 1700000000 |
| 267 | bid = new uchar 16 78 |
| 268 | ohtx_set_bundle_id bid |
| 269 | seg_in = new uchar 208 0 |
| 270 | if not tc_build_clean_seg seg_in |
| 271 | ret false |
| 272 | end |
| 273 | bundle = new uchar 4408 0 |
| 274 | c_buf = new uchar 160 0 |
| 275 | ohtx_sign_consent node_seed recv_pk 6 add now 3600 1 c_buf |
| 276 | if ne oh_trace_export node_seed c_buf seg_in now bundle OHTX_ERR_CONSENT_AUTH |
| 277 | ret false |
| 278 | end |
| 279 | ohtx_sign_consent node_seed recv_pk 6 add now 3600 0 c_buf |
| 280 | put c_buf 5 uchar 0 |
| 281 | if ne oh_trace_export node_seed c_buf seg_in now bundle OHTX_ERR_NO_CONSENT |
| 282 | ret false |
| 283 | end |
| 284 | ohtx_sign_consent node_seed recv_pk 6 sub now 10 0 c_buf |
| 285 | if ne oh_trace_export node_seed c_buf seg_in now bundle OHTX_ERR_CONSENT_EXPIRED |
| 286 | ret false |
| 287 | end |
| 288 | ret tc_check_fw_neg node_seed recv_pk now |
| 289 | end |
| 290 | |
| 291 | fn tc_check_recv_mutants bundle edit uchar 4408 blen ulong recv_pk view uchar 32 now ulong to bool |
| 292 | seg_out = new uchar 512 0 |
| 293 | if ne oh_trace_receive part bundle 0 blen recv_pk now seg_out OHTX_OK |
| 294 | ret false |
| 295 | end |
| 296 | if ne oh_trace_receive part bundle 0 blen recv_pk now seg_out OHTX_ERR_REPLAY |
| 297 | ret false |
| 298 | end |
| 299 | ohtx_reset_replay |
| 300 | put bundle add OHTX_HDR_LEN 24 xor get bundle add OHTX_HDR_LEN 24 uchar 1 |
| 301 | ret eq oh_trace_receive part bundle 0 blen recv_pk now seg_out OHTX_ERR_BAD_DIGEST |
| 302 | end |
| 303 | |
| 304 | note Test 6 (NEGATIVE): Replay, wrong receiver key, tampered segment CRC, torn segment, and bundle tamper all refused. |
| 305 | fn test_tc_trace_neg_recv to bool |
| 306 | ohtx_reset_replay |
| 307 | node_seed = new uchar 32 21 |
| 308 | recv_seed = new uchar 32 22 |
| 309 | other_seed = new uchar 32 23 |
| 310 | recv_pk = new uchar 32 0 |
| 311 | other_pk = new uchar 32 0 |
| 312 | ohtx_ed_pubkey recv_seed recv_pk |
| 313 | ohtx_ed_pubkey other_seed other_pk |
| 314 | now = ulong 1700000000 |
| 315 | consent = new uchar 160 0 |
| 316 | ohtx_sign_consent node_seed recv_pk 6 add now 3600 0 consent |
| 317 | seg_in = new uchar 208 0 |
| 318 | if not tc_build_clean_seg seg_in |
| 319 | ret false |
| 320 | end |
| 321 | bid = new uchar 16 88 |
| 322 | ohtx_set_bundle_id bid |
| 323 | bundle = new uchar 4408 0 |
| 324 | if ne oh_trace_export node_seed consent seg_in now bundle OHTX_OK |
| 325 | ret false |
| 326 | end |
| 327 | blen = ohtx_export_len |
| 328 | seg_out = new uchar 512 0 |
| 329 | if ne oh_trace_receive part bundle 0 blen other_pk now seg_out OHTX_ERR_WRONG_RECEIVER |
| 330 | ret false |
| 331 | end |
| 332 | if not tc_check_recv_mutants bundle blen recv_pk now |
| 333 | ret false |
| 334 | end |
| 335 | if ne oh_trace_export node_seed consent part seg_in 0 180 now bundle OHTX_ERR_AITR_TORN |
| 336 | ret false |
| 337 | end |
| 338 | put seg_in 30 xor get seg_in 30 uchar 1 |
| 339 | ret eq oh_trace_export node_seed consent seg_in now bundle OHTX_ERR_AITR_CRC |
| 340 | end |
| 341 | |
| 342 | fn main to int |
| 343 | put tc_fails 0 0 |
| 344 | tc_rep tc_n1 test_tc_wire_pos |
| 345 | tc_rep tc_n2 test_tc_wire_neg |
| 346 | tc_rep tc_n3 test_tc_fingerprint |
| 347 | tc_rep tc_n4 test_tc_trace_pos |
| 348 | tc_rep tc_n5 test_tc_trace_neg_consent_fw |
| 349 | tc_rep tc_n6 test_tc_trace_neg_recv |
| 350 | if eq get tc_fails 0 0 |
| 351 | tc_w tc_ok_all |
| 352 | ret 0 |
| 353 | end |
| 354 | tc_w tc_bad_all |
| 355 | ret 1 |
| 356 | end |