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 / gpu / physics0 / test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note gpu/physics0/test.om: comprehensive positive, negative & mutant-control test suite for gpu/physics0. |
| 3 | note Compose: si/directives/om2.om gpu/physics0/types.om gpu/physics0/rng.om gpu/physics0/wire.om gpu/physics0/world.om gpu/physics0/test.om |
| 4 | |
| 5 | text p0_ok1 "PASS physics0 FIPS 180-4 SHA-256 vectors (empty + abc)\n" |
| 6 | text p0_ok2 "PASS physics0 SplitMix64 + FNV-1a64 + exact micro-unit arithmetic\n" |
| 7 | text p0_ok3 "PASS physics0 PD0DESC2 + PD0REC1 wire framing, SHA-256 chain & STAND_IN guards\n" |
| 8 | text p0_ok4 "PASS physics0 world invariants (timing, episode ceiling, budget ceiling, OOB non-leak, STAND_IN charge)\n" |
| 9 | text p0_ok5 "PASS physics0 mutant controls (M1 L1 spring sign flip, M2 tampered record hash)\n" |
| 10 | text p0_all "ALL PASS gpu-physics0\n" |
| 11 | text p0_fail "FAIL gpu-physics0\n" |
| 12 | |
| 13 | text p0_abc "abc" |
| 14 | text p0_empty_src "x" |
| 15 | |
| 16 | fn p0_puts s view uchar * to void |
| 17 | k = write 1 s 0 len s |
| 18 | if lt k 0 |
| 19 | exit 2 |
| 20 | end |
| 21 | ret |
| 22 | end |
| 23 | |
| 24 | fn test_sha256_vectors to int |
| 25 | h = new uchar 32 0 |
| 26 | note 1. SHA-256("") = e3b0c44298fc1c14...7852b855 |
| 27 | pd0_sha256_hash part p0_empty_src 0 0 ulong 0 edit h |
| 28 | if or ne get h 0 uchar 0xe3 ne get h 1 uchar 0xb0 |
| 29 | ret 1 |
| 30 | end |
| 31 | if or ne get h 2 uchar 0xc4 ne get h 3 uchar 0x42 |
| 32 | ret 2 |
| 33 | end |
| 34 | if or ne get h 30 uchar 0xb8 ne get h 31 uchar 0x55 |
| 35 | ret 3 |
| 36 | end |
| 37 | note 2. SHA-256("abc") = ba7816bf8f01cfea...f20015ad |
| 38 | pd0_sha256_hash p0_abc ulong 3 edit h |
| 39 | if or ne get h 0 uchar 0xba ne get h 1 uchar 0x78 |
| 40 | ret 4 |
| 41 | end |
| 42 | if or ne get h 2 uchar 0x16 ne get h 3 uchar 0xbf |
| 43 | ret 5 |
| 44 | end |
| 45 | if or ne get h 30 uchar 0x15 ne get h 31 uchar 0xad |
| 46 | ret 6 |
| 47 | end |
| 48 | ret 0 |
| 49 | end |
| 50 | |
| 51 | fn test_rng_and_math to int |
| 52 | r1 = new ulong 1 0 |
| 53 | r2 = new ulong 1 0 |
| 54 | pd0_stream edit r1 ulong 42 PD0_TAG_CONST |
| 55 | pd0_stream edit r2 ulong 42 PD0_TAG_CONST |
| 56 | v1 = pd0_splitmix64 edit r1 |
| 57 | v2 = pd0_splitmix64 edit r2 |
| 58 | if ne v1 v2 |
| 59 | ret 10 |
| 60 | end |
| 61 | note Different tags produce distinct streams |
| 62 | pd0_stream edit r2 ulong 42 PD0_TAG_NOISE |
| 63 | v3 = pd0_splitmix64 edit r2 |
| 64 | if eq v1 v3 |
| 65 | ret 11 |
| 66 | end |
| 67 | note Uniform draw in [0, 1000000] |
| 68 | u = pd0_uniform edit r1 |
| 69 | if or lt u long 0 gt u long 1000000 |
| 70 | ret 12 |
| 71 | end |
| 72 | note Exact micro-unit multiply (positive, negative, large) |
| 73 | if ne pd0_mul long 1500000 long 2000000 long 3000000 |
| 74 | ret 13 |
| 75 | end |
| 76 | if ne pd0_mul long 10000000 long -10000000 long -100000000 |
| 77 | ret 14 |
| 78 | end |
| 79 | if ne pd0_mul long -500000 long -500000 long 250000 |
| 80 | ret 15 |
| 81 | end |
| 82 | ret 0 |
| 83 | end |
| 84 | |
| 85 | fn test_wire_and_guards to int |
| 86 | d = new Pd0Desc 0 0 0 0 0 0 0 0 0 0 |
| 87 | d2 = new Pd0Desc 0 0 0 0 0 0 0 0 0 0 |
| 88 | if ne pd0_gen_desc PD0_L1 edit d 0 |
| 89 | ret 20 |
| 90 | end |
| 91 | buf = new uchar 256 0 |
| 92 | n = pd0_desc_encode view d edit buf |
| 93 | if ne n ulong 78 |
| 94 | ret 21 |
| 95 | end |
| 96 | if ne pd0_desc_decode part buf 0 n edit d2 0 |
| 97 | ret 22 |
| 98 | end |
| 99 | if or ne get d2 n_obs uchar 2 ne get d2 dt_micro long 50000 |
| 100 | ret 23 |
| 101 | end |
| 102 | if or ne get d2 budget_steps uint 3000 ne get d2 episode_max_steps uint 100 |
| 103 | ret 24 |
| 104 | end |
| 105 | note Negative control: PD0DESC1 explicitly refused with PD0_DESC_REFUSED_V1 (-2) |
| 106 | put buf 7 uchar 49 |
| 107 | if ne pd0_desc_decode part buf 0 n edit d2 PD0_DESC_REFUSED_V1 |
| 108 | ret 25 |
| 109 | end |
| 110 | note Negative control: bad magic refused (-1) |
| 111 | put buf 0 uchar 88 |
| 112 | if ne pd0_desc_decode part buf 0 n edit d2 -1 |
| 113 | ret 26 |
| 114 | end |
| 115 | note Negative control: truncated buffer refused (-1) |
| 116 | if ne pd0_desc_decode part buf 0 5 edit d2 -1 |
| 117 | ret 27 |
| 118 | end |
| 119 | note STAND_IN range guard positive & negative controls |
| 120 | if ne pd0_guard_step_ok view d PD0_CH_NONE long 999999999 1 |
| 121 | ret 28 |
| 122 | end |
| 123 | if ne pd0_guard_step_ok view d uchar 0 long 2000000 1 |
| 124 | ret 29 |
| 125 | end |
| 126 | if ne pd0_guard_step_ok view d uchar 0 long 2000001 0 |
| 127 | ret 30 |
| 128 | end |
| 129 | if ne pd0_guard_step_ok view d uchar 1 long 0 0 |
| 130 | ret 31 |
| 131 | end |
| 132 | rv = new long 4 0 |
| 133 | put rv 0 long 1000000 |
| 134 | put rv 1 long -2000000 |
| 135 | if ne pd0_guard_reset_ok view d view rv uchar 2 1 |
| 136 | ret 32 |
| 137 | end |
| 138 | put rv 1 long -2000001 |
| 139 | if ne pd0_guard_reset_ok view d view rv uchar 2 0 |
| 140 | ret 33 |
| 141 | end |
| 142 | if ne pd0_guard_in_box long 10000000 1 |
| 143 | ret 34 |
| 144 | end |
| 145 | if ne pd0_guard_in_box long 10000001 0 |
| 146 | ret 35 |
| 147 | end |
| 148 | if ne pd0_guard_in_box long -10000001 0 |
| 149 | ret 36 |
| 150 | end |
| 151 | ret 0 |
| 152 | end |
| 153 | |
| 154 | fn test_world_oob_budget to int |
| 155 | w = new Pd0World 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 156 | d = new Pd0Desc 0 0 0 0 0 0 0 0 0 0 |
| 157 | g = new Pd0Gen 0 0 0 0 0 |
| 158 | r0 = new Pd0Rec 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 159 | cur_r = new Pd0Rec 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 160 | rv = new long 4 0 |
| 161 | if ne pd0_world_init edit w edit d edit g PD0_L0 ulong 12345 0 |
| 162 | ret 52 |
| 163 | end |
| 164 | note 6. Out-of-bounds invariant: push state past PD0_BOUND -> PD0_OUT_OF_BOUNDS, after == before (no leak) |
| 165 | put rv 0 long 2000000 |
| 166 | put rv 1 long 200000 |
| 167 | pd0_world_reset edit w view d view g view rv uchar 2 edit r0 |
| 168 | if ne get r0 status PD0_OK |
| 169 | ret 52 |
| 170 | end |
| 171 | oob_hit = int 0 |
| 172 | for st ulong 30 |
| 173 | pd0_world_step edit w view d view g uchar 0 long 100000 edit cur_r |
| 174 | if eq get cur_r status PD0_OUT_OF_BOUNDS |
| 175 | if ne get cur_r after 0 get cur_r before 0 |
| 176 | ret 53 |
| 177 | end |
| 178 | if ne get cur_r after 1 get cur_r before 1 |
| 179 | ret 53 |
| 180 | end |
| 181 | if ne get w in_episode 0 |
| 182 | ret 54 |
| 183 | end |
| 184 | oob_hit = 1 |
| 185 | break |
| 186 | end |
| 187 | end |
| 188 | if ne oob_hit 1 |
| 189 | ret 55 |
| 190 | end |
| 191 | note 7. Budget ceiling enforcement |
| 192 | put d budget_steps get w steps_used |
| 193 | put rv 0 long 0 |
| 194 | put rv 1 long 0 |
| 195 | pd0_world_reset edit w view d view g view rv uchar 2 edit cur_r |
| 196 | if ne get cur_r status PD0_BUDGET_EXHAUSTED |
| 197 | ret 56 |
| 198 | end |
| 199 | ret 0 |
| 200 | end |
| 201 | |
| 202 | fn test_world_invariants to int |
| 203 | w = new Pd0World 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 204 | d = new Pd0Desc 0 0 0 0 0 0 0 0 0 0 |
| 205 | g = new Pd0Gen 0 0 0 0 0 |
| 206 | r0 = new Pd0Rec 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 207 | r1 = new Pd0Rec 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 208 | r2 = new Pd0Rec 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 209 | rv = new long 4 0 |
| 210 | if ne pd0_world_init edit w edit d edit g PD0_L0 ulong 12345 0 |
| 211 | ret 40 |
| 212 | end |
| 213 | note 1. Out-of-range reset refused (PD0_REFUSED_RANGE) AND charged to budget + sealed at seq 0 |
| 214 | put rv 0 long 5000000 |
| 215 | put rv 1 long 0 |
| 216 | pd0_world_reset edit w view d view g view rv uchar 2 edit r0 |
| 217 | if or ne get r0 status PD0_REFUSED_RANGE ne get w episodes_used uint 1 |
| 218 | ret 41 |
| 219 | end |
| 220 | if ne pd0_rec_chained view r0 0 view r1 1 |
| 221 | ret 42 |
| 222 | end |
| 223 | note 2. Valid reset succeeds at seq 1, chained to r0 |
| 224 | put rv 0 long 0 |
| 225 | put rv 1 long 100000 |
| 226 | pd0_world_reset edit w view d view g view rv uchar 2 edit r1 |
| 227 | if or ne get r1 status PD0_OK ne get r1 seq ulong 1 |
| 228 | ret 43 |
| 229 | end |
| 230 | if ne pd0_rec_chained view r1 1 view r0 1 |
| 231 | ret 44 |
| 232 | end |
| 233 | note 3. Out-of-range step refused (PD0_REFUSED_RANGE), still charged, chained to r1 |
| 234 | pd0_world_step edit w view d view g uchar 0 long 500000 edit r2 |
| 235 | if or ne get r2 status PD0_REFUSED_RANGE ne get w steps_used uint 1 |
| 236 | ret 45 |
| 237 | end |
| 238 | if ne pd0_rec_chained view r2 1 view r1 1 |
| 239 | ret 46 |
| 240 | end |
| 241 | note 4. Run 20 valid steps on L0 -> step 20 hits episode_max_steps -> PD0_EPISODE_END |
| 242 | for st ulong 20 |
| 243 | pd0_world_step edit w view d view g PD0_CH_NONE long 0 edit r0 |
| 244 | if ne pd0_rec_chained view r0 1 view r2 1 |
| 245 | ret 47 |
| 246 | end |
| 247 | if lt st ulong 19 |
| 248 | if ne get r0 status PD0_OK |
| 249 | ret 49 |
| 250 | end |
| 251 | else |
| 252 | if ne get r0 status PD0_EPISODE_END |
| 253 | ret 50 |
| 254 | end |
| 255 | end |
| 256 | put r2 seq get r0 seq |
| 257 | for j ulong 8 |
| 258 | put r2 hash j get r0 hash j |
| 259 | end |
| 260 | end |
| 261 | note 5. Step after episode end returns PD0_BUDGET_EXHAUSTED |
| 262 | pd0_world_step edit w view d view g PD0_CH_NONE long 0 edit r0 |
| 263 | if ne get r0 status PD0_BUDGET_EXHAUSTED |
| 264 | ret 51 |
| 265 | end |
| 266 | ret test_world_oob_budget |
| 267 | end |
| 268 | |
| 269 | fn test_mutant_controls to int |
| 270 | w = new Pd0World 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 271 | d = new Pd0Desc 0 0 0 0 0 0 0 0 0 0 |
| 272 | g = new Pd0Gen 0 0 0 0 0 |
| 273 | if ne pd0_world_init edit w edit d edit g PD0_L1 ulong 77 0 |
| 274 | ret 60 |
| 275 | end |
| 276 | put w s 0 long 1000000 |
| 277 | put w s 1 long 0 |
| 278 | u = new long 2 0 |
| 279 | ns_true = new long 4 0 |
| 280 | ns_mut = new long 4 0 |
| 281 | note Mutant M1: L1 spring sign flip (+k*s0 vs -k*s0). True L1 pulls velocity negative; mutant pushes positive! |
| 282 | b1 = pd0_gen_step_dyn view g edit w view u get d dt_micro edit ns_true 0 |
| 283 | b2 = pd0_gen_step_dyn view g edit w view u get d dt_micro edit ns_mut 1 |
| 284 | if or ne b1 0 ne b2 0 |
| 285 | ret 61 |
| 286 | end |
| 287 | if ge get ns_true 1 long 0 |
| 288 | ret 62 |
| 289 | end |
| 290 | if le get ns_mut 1 long 0 |
| 291 | ret 63 |
| 292 | end |
| 293 | if eq get ns_true 1 get ns_mut 1 |
| 294 | ret 64 |
| 295 | end |
| 296 | note Mutant M2: tampered PD0REC1 record byte refused by SHA-256 verification |
| 297 | rec = new Pd0Rec 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 298 | rec_out = new Pd0Rec 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 299 | put rec n_obs uchar 2 |
| 300 | put rec seq ulong 0 |
| 301 | put rec before 0 long 1000000 |
| 302 | put rec after 0 long 950000 |
| 303 | buf = new uchar 256 0 |
| 304 | n = pd0_rec_encode edit rec edit buf |
| 305 | if ne n ulong 152 |
| 306 | ret 65 |
| 307 | end |
| 308 | if ne pd0_rec_decode part buf 0 n edit rec_out 0 |
| 309 | ret 66 |
| 310 | end |
| 311 | note Flip one bit in payload (seq byte) -> must fail decode |
| 312 | put buf 16 xor get buf 16 uchar 1 |
| 313 | if ne pd0_rec_decode part buf 0 n edit rec_out -1 |
| 314 | ret 67 |
| 315 | end |
| 316 | note Restore payload, flip one bit in pad byte 14 -> must fail decode |
| 317 | put buf 16 xor get buf 16 uchar 1 |
| 318 | put buf 14 uchar 1 |
| 319 | if ne pd0_rec_decode part buf 0 n edit rec_out -1 |
| 320 | ret 68 |
| 321 | end |
| 322 | ret 0 |
| 323 | end |
| 324 | |
| 325 | fn main to int |
| 326 | rc = test_sha256_vectors |
| 327 | if ne rc 0 |
| 328 | p0_puts p0_fail |
| 329 | ret rc |
| 330 | end |
| 331 | p0_puts p0_ok1 |
| 332 | |
| 333 | rc = test_rng_and_math |
| 334 | if ne rc 0 |
| 335 | p0_puts p0_fail |
| 336 | ret rc |
| 337 | end |
| 338 | p0_puts p0_ok2 |
| 339 | |
| 340 | rc = test_wire_and_guards |
| 341 | if ne rc 0 |
| 342 | p0_puts p0_fail |
| 343 | ret rc |
| 344 | end |
| 345 | p0_puts p0_ok3 |
| 346 | |
| 347 | rc = test_world_invariants |
| 348 | if ne rc 0 |
| 349 | p0_puts p0_fail |
| 350 | ret rc |
| 351 | end |
| 352 | p0_puts p0_ok4 |
| 353 | |
| 354 | rc = test_mutant_controls |
| 355 | if ne rc 0 |
| 356 | p0_puts p0_fail |
| 357 | ret rc |
| 358 | end |
| 359 | p0_puts p0_ok5 |
| 360 | |
| 361 | p0_puts p0_all |
| 362 | ret 0 |
| 363 | end |