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 / wire.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note gpu/physics0/wire.om: PD-0 wire framing (PD0DESC2, PD0REC1 with SHA-256 chain) and STAND_IN range guard. |
| 3 | note Converted from src/physics0/{pd0_wire.c,pd0_guard.c}. |
| 4 | |
| 5 | text PD0_MAGIC_DESC2 "PD0DESC2" |
| 6 | text PD0_MAGIC_DESC1 "PD0DESC1" |
| 7 | text PD0_MAGIC_REC1 "PD0REC1" |
| 8 | |
| 9 | fn pd0_put_u16 buf edit uchar * off ulong v ushort to void |
| 10 | put buf off uchar and v ushort 255 |
| 11 | put buf add off 1 uchar and shr v 8 ushort 255 |
| 12 | ret |
| 13 | end |
| 14 | |
| 15 | fn pd0_put_u32 buf edit uchar * off ulong v uint to void |
| 16 | for i ulong 4 |
| 17 | sh = mul uint i uint 8 |
| 18 | put buf add off i uchar and shr v sh uint 255 |
| 19 | end |
| 20 | ret |
| 21 | end |
| 22 | |
| 23 | fn pd0_put_u64 buf edit uchar * off ulong v ulong to void |
| 24 | for i ulong 8 |
| 25 | sh = mul i ulong 8 |
| 26 | put buf add off i uchar and shr v sh ulong 255 |
| 27 | end |
| 28 | ret |
| 29 | end |
| 30 | |
| 31 | fn pd0_get_u16 buf view uchar * off ulong to ushort |
| 32 | b0 = ushort get buf off |
| 33 | b1 = shl ushort get buf add off 1 8 |
| 34 | ret or b0 b1 |
| 35 | end |
| 36 | |
| 37 | fn pd0_get_u32 buf view uchar * off ulong to uint |
| 38 | v = uint 0 |
| 39 | for i ulong 4 |
| 40 | sh = mul uint i uint 8 |
| 41 | b = shl uint get buf add off i sh |
| 42 | v = or v b |
| 43 | end |
| 44 | ret v |
| 45 | end |
| 46 | |
| 47 | fn pd0_get_u64 buf view uchar * off ulong to ulong |
| 48 | v = ulong 0 |
| 49 | for i ulong 8 |
| 50 | sh = mul i ulong 8 |
| 51 | b = shl ulong get buf add off i sh |
| 52 | v = or v b |
| 53 | end |
| 54 | ret v |
| 55 | end |
| 56 | |
| 57 | fn pd0_eq_bytes a view uchar * aoff ulong b view uchar * n ulong to int |
| 58 | for i ulong n bound 64 |
| 59 | if ne get a add aoff i get b i |
| 60 | ret 0 |
| 61 | end |
| 62 | end |
| 63 | ret 1 |
| 64 | end |
| 65 | |
| 66 | fn pd0_rec_size n_obs uchar to ulong |
| 67 | ret add ulong 120 mul ulong 16 ulong n_obs |
| 68 | end |
| 69 | |
| 70 | fn pd0_desc_size n_obs uchar n_ch uchar to ulong |
| 71 | ret add add ulong 30 mul ulong 16 ulong n_ch mul ulong 16 ulong n_obs |
| 72 | end |
| 73 | |
| 74 | note ---- STAND_IN range guard (pd0_guard.c) ---- |
| 75 | |
| 76 | fn pd0_guard_step_ok d view Pd0Desc channel uchar val long to int |
| 77 | if eq channel PD0_CH_NONE |
| 78 | ret 1 |
| 79 | end |
| 80 | if ge channel get d n_channels |
| 81 | ret 0 |
| 82 | end |
| 83 | idx = ulong channel |
| 84 | if lt val get d chan_min idx |
| 85 | ret 0 |
| 86 | end |
| 87 | if gt val get d chan_max idx |
| 88 | ret 0 |
| 89 | end |
| 90 | ret 1 |
| 91 | end |
| 92 | |
| 93 | fn pd0_guard_reset_ok d view Pd0Desc vals view long 4 n_vals uchar to int |
| 94 | if ne n_vals get d n_obs |
| 95 | ret 0 |
| 96 | end |
| 97 | n = ulong n_vals |
| 98 | for i ulong n bound 4 |
| 99 | v = get vals i |
| 100 | if lt v get d reset_min i |
| 101 | ret 0 |
| 102 | end |
| 103 | if gt v get d reset_max i |
| 104 | ret 0 |
| 105 | end |
| 106 | end |
| 107 | ret 1 |
| 108 | end |
| 109 | |
| 110 | fn pd0_guard_in_box v long to int |
| 111 | if gt v PD0_BOUND |
| 112 | ret 0 |
| 113 | end |
| 114 | if lt v neg PD0_BOUND |
| 115 | ret 0 |
| 116 | end |
| 117 | ret 1 |
| 118 | end |
| 119 | |
| 120 | note ---- PD0DESC2 wire codec ---- |
| 121 | |
| 122 | fn pd0_desc_encode d view Pd0Desc dst edit uchar * to ulong |
| 123 | nobs = get d n_obs |
| 124 | nch = get d n_channels |
| 125 | if or lt nobs uchar 1 gt nobs uchar 4 |
| 126 | ret ulong 0 |
| 127 | end |
| 128 | if or lt nch uchar 1 gt nch uchar 2 |
| 129 | ret ulong 0 |
| 130 | end |
| 131 | need = pd0_desc_size nobs nch |
| 132 | if lt len dst need |
| 133 | ret ulong 0 |
| 134 | end |
| 135 | for i ulong 8 |
| 136 | put dst i get PD0_MAGIC_DESC2 i |
| 137 | end |
| 138 | put dst 8 nobs |
| 139 | put dst 9 nch |
| 140 | pos = ulong 10 |
| 141 | pd0_put_u64 dst pos bitcast ulong get d dt_micro |
| 142 | pos = add pos 8 |
| 143 | for i ulong ulong nch bound 2 |
| 144 | pd0_put_u64 dst pos bitcast ulong get d chan_min i |
| 145 | pos = add pos 8 |
| 146 | end |
| 147 | for i ulong ulong nch bound 2 |
| 148 | pd0_put_u64 dst pos bitcast ulong get d chan_max i |
| 149 | pos = add pos 8 |
| 150 | end |
| 151 | for i ulong ulong nobs bound 4 |
| 152 | pd0_put_u64 dst pos bitcast ulong get d reset_min i |
| 153 | pos = add pos 8 |
| 154 | end |
| 155 | for i ulong ulong nobs bound 4 |
| 156 | pd0_put_u64 dst pos bitcast ulong get d reset_max i |
| 157 | pos = add pos 8 |
| 158 | end |
| 159 | pd0_put_u32 dst pos get d episode_max_steps |
| 160 | pos = add pos 4 |
| 161 | pd0_put_u32 dst pos get d budget_steps |
| 162 | pos = add pos 4 |
| 163 | pd0_put_u32 dst pos get d budget_episodes |
| 164 | pos = add pos 4 |
| 165 | ret pos |
| 166 | end |
| 167 | |
| 168 | fn pd0_desc_decode src view uchar * d edit Pd0Desc to int |
| 169 | slen = len src |
| 170 | if ge slen ulong 8 |
| 171 | if eq pd0_eq_bytes src ulong 0 PD0_MAGIC_DESC1 ulong 8 1 |
| 172 | ret PD0_DESC_REFUSED_V1 |
| 173 | end |
| 174 | end |
| 175 | if lt slen ulong 10 |
| 176 | ret -1 |
| 177 | end |
| 178 | if ne pd0_eq_bytes src ulong 0 PD0_MAGIC_DESC2 ulong 8 1 |
| 179 | ret -1 |
| 180 | end |
| 181 | nobs = get src 8 |
| 182 | nch = get src 9 |
| 183 | if or lt nobs uchar 1 gt nobs uchar 4 |
| 184 | ret -1 |
| 185 | end |
| 186 | if or lt nch uchar 1 gt nch uchar 2 |
| 187 | ret -1 |
| 188 | end |
| 189 | if ne slen pd0_desc_size nobs nch |
| 190 | ret -1 |
| 191 | end |
| 192 | put d n_obs nobs |
| 193 | put d n_channels nch |
| 194 | pos = ulong 10 |
| 195 | put d dt_micro bitcast long pd0_get_u64 src pos |
| 196 | pos = add pos 8 |
| 197 | for i ulong 2 |
| 198 | put d chan_min i long 0 |
| 199 | put d chan_max i long 0 |
| 200 | end |
| 201 | for i ulong 4 |
| 202 | put d reset_min i long 0 |
| 203 | put d reset_max i long 0 |
| 204 | end |
| 205 | for i ulong ulong nch bound 2 |
| 206 | put d chan_min i bitcast long pd0_get_u64 src pos |
| 207 | pos = add pos 8 |
| 208 | end |
| 209 | for i ulong ulong nch bound 2 |
| 210 | put d chan_max i bitcast long pd0_get_u64 src pos |
| 211 | pos = add pos 8 |
| 212 | end |
| 213 | for i ulong ulong nobs bound 4 |
| 214 | put d reset_min i bitcast long pd0_get_u64 src pos |
| 215 | pos = add pos 8 |
| 216 | end |
| 217 | for i ulong ulong nobs bound 4 |
| 218 | put d reset_max i bitcast long pd0_get_u64 src pos |
| 219 | pos = add pos 8 |
| 220 | end |
| 221 | put d episode_max_steps pd0_get_u32 src pos |
| 222 | pos = add pos 4 |
| 223 | put d budget_steps pd0_get_u32 src pos |
| 224 | pos = add pos 4 |
| 225 | put d budget_episodes pd0_get_u32 src pos |
| 226 | ret 0 |
| 227 | end |
| 228 | |
| 229 | note ---- PD0REC1 sealed evidence record codec ---- |
| 230 | |
| 231 | fn pd0_rec_body r view Pd0Rec dst edit uchar * to ulong |
| 232 | for i ulong 7 |
| 233 | put dst i get PD0_MAGIC_REC1 i |
| 234 | end |
| 235 | put dst 7 uchar 0 |
| 236 | pd0_put_u16 dst ulong 8 ushort 1 |
| 237 | nobs = get r n_obs |
| 238 | put dst 10 nobs |
| 239 | put dst 11 get r status |
| 240 | put dst 12 get r kind |
| 241 | put dst 13 get r channel |
| 242 | put dst 14 uchar 0 |
| 243 | put dst 15 uchar 0 |
| 244 | pd0_put_u64 dst ulong 16 get r seq |
| 245 | pd0_put_u32 dst ulong 24 get r episode |
| 246 | pd0_put_u32 dst ulong 28 get r step_in_episode |
| 247 | pd0_put_u64 dst ulong 32 bitcast ulong get r time_micro |
| 248 | pd0_put_u64 dst ulong 40 bitcast ulong get r requested |
| 249 | pd0_put_u64 dst ulong 48 bitcast ulong get r applied |
| 250 | pos = ulong 56 |
| 251 | for i ulong ulong nobs bound 4 |
| 252 | pd0_put_u64 dst pos bitcast ulong get r before i |
| 253 | pos = add pos 8 |
| 254 | end |
| 255 | for i ulong ulong nobs bound 4 |
| 256 | pd0_put_u64 dst pos bitcast ulong get r after i |
| 257 | pos = add pos 8 |
| 258 | end |
| 259 | for i ulong 8 |
| 260 | w = get r prev_hash i |
| 261 | put dst pos uchar and shr w 24 uint 255 |
| 262 | put dst add pos 1 uchar and shr w 16 uint 255 |
| 263 | put dst add pos 2 uchar and shr w 8 uint 255 |
| 264 | put dst add pos 3 uchar and w uint 255 |
| 265 | pos = add pos 4 |
| 266 | end |
| 267 | ret pos |
| 268 | end |
| 269 | |
| 270 | fn pd0_get_be32 buf view uchar * off ulong to uint |
| 271 | b0 = shl uint get buf off 24 |
| 272 | b1 = shl uint get buf add off 1 16 |
| 273 | b2 = shl uint get buf add off 2 8 |
| 274 | b3 = uint get buf add off 3 |
| 275 | ret or or b0 b1 or b2 b3 |
| 276 | end |
| 277 | |
| 278 | fn pd0_rec_encode r edit Pd0Rec dst edit uchar * to ulong |
| 279 | nobs = get r n_obs |
| 280 | if or lt nobs uchar 1 gt nobs uchar 4 |
| 281 | ret ulong 0 |
| 282 | end |
| 283 | need = pd0_rec_size nobs |
| 284 | if lt len dst need |
| 285 | ret ulong 0 |
| 286 | end |
| 287 | body_n = pd0_rec_body view r dst |
| 288 | h = new uchar 32 0 |
| 289 | pd0_sha256_hash view dst body_n edit h |
| 290 | for i ulong 8 |
| 291 | put r hash i pd0_get_be32 view h mul i 4 |
| 292 | end |
| 293 | for i ulong 32 |
| 294 | put dst add body_n i get h i |
| 295 | end |
| 296 | ret add body_n ulong 32 |
| 297 | end |
| 298 | |
| 299 | fn pd0_rec_decode src view uchar * r edit Pd0Rec to int |
| 300 | slen = len src |
| 301 | if lt slen ulong 12 |
| 302 | ret -1 |
| 303 | end |
| 304 | if ne pd0_eq_bytes src ulong 0 PD0_MAGIC_REC1 ulong 7 1 |
| 305 | ret -1 |
| 306 | end |
| 307 | if ne get src 7 uchar 0 |
| 308 | ret -1 |
| 309 | end |
| 310 | if ne pd0_get_u16 src ulong 8 ushort 1 |
| 311 | ret -1 |
| 312 | end |
| 313 | nobs = get src 10 |
| 314 | if or lt nobs uchar 1 gt nobs uchar 4 |
| 315 | ret -1 |
| 316 | end |
| 317 | if ne slen pd0_rec_size nobs |
| 318 | ret -1 |
| 319 | end |
| 320 | if or ne get src 14 uchar 0 ne get src 15 uchar 0 |
| 321 | ret -1 |
| 322 | end |
| 323 | body_n = sub slen ulong 32 |
| 324 | h = new uchar 32 0 |
| 325 | pd0_sha256_hash src body_n edit h |
| 326 | for i ulong 32 |
| 327 | if ne get h i get src add body_n i |
| 328 | ret -1 |
| 329 | end |
| 330 | end |
| 331 | put r n_obs nobs |
| 332 | put r status get src 11 |
| 333 | put r kind get src 12 |
| 334 | put r channel get src 13 |
| 335 | put r seq pd0_get_u64 src ulong 16 |
| 336 | put r episode pd0_get_u32 src ulong 24 |
| 337 | put r step_in_episode pd0_get_u32 src ulong 28 |
| 338 | put r time_micro bitcast long pd0_get_u64 src ulong 32 |
| 339 | put r requested bitcast long pd0_get_u64 src ulong 40 |
| 340 | put r applied bitcast long pd0_get_u64 src ulong 48 |
| 341 | pos = ulong 56 |
| 342 | for i ulong 4 |
| 343 | put r before i long 0 |
| 344 | put r after i long 0 |
| 345 | end |
| 346 | for i ulong ulong nobs bound 4 |
| 347 | put r before i bitcast long pd0_get_u64 src pos |
| 348 | pos = add pos 8 |
| 349 | end |
| 350 | for i ulong ulong nobs bound 4 |
| 351 | put r after i bitcast long pd0_get_u64 src pos |
| 352 | pos = add pos 8 |
| 353 | end |
| 354 | for i ulong 8 |
| 355 | put r prev_hash i pd0_get_be32 src pos |
| 356 | pos = add pos 4 |
| 357 | end |
| 358 | for i ulong 8 |
| 359 | put r hash i pd0_get_be32 view h mul i 4 |
| 360 | end |
| 361 | ret 0 |
| 362 | end |
| 363 | |
| 364 | fn pd0_rec_chained r view Pd0Rec has_prev int prev view Pd0Rec to int |
| 365 | if eq has_prev 0 |
| 366 | if ne get r seq ulong 0 |
| 367 | ret 0 |
| 368 | end |
| 369 | for i ulong 8 |
| 370 | if ne get r prev_hash i uint 0 |
| 371 | ret 0 |
| 372 | end |
| 373 | end |
| 374 | ret 1 |
| 375 | end |
| 376 | if ne get r seq add get prev seq ulong 1 |
| 377 | ret 0 |
| 378 | end |
| 379 | for i ulong 8 |
| 380 | if ne get r prev_hash i get prev hash i |
| 381 | ret 0 |
| 382 | end |
| 383 | end |
| 384 | ret 1 |
| 385 | end |