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 / src / physics0 / pd0_types.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note src/physics0/pd0_types.om: PD-0 core constants, structs, FIPS 180-4 SHA-256, and math/format helpers. |
| 3 | note Learner-side safe: no generator, no world, no file I/O. |
| 4 | |
| 5 | const PD0_MAX_OBS ulong 4 |
| 6 | const PD0_MAX_CH ulong 2 |
| 7 | const PD0_MAX_LATENT ulong 2 |
| 8 | const PD0_MAX_VARS ulong 6 |
| 9 | const PD0_MAX_EX ulong 8 |
| 10 | const PD0_MAX_TERMS ulong 12 |
| 11 | const PD0_MAX_DEG ulong 3 |
| 12 | const PD0_SCHED_STEPS ulong 20 |
| 13 | const PD0_HASH ulong 32 |
| 14 | const PD0_CH_NONE uchar 255 |
| 15 | const PD0_MICRO long 1000000 |
| 16 | const PD0_IH_SD long 577350 |
| 17 | const PD0_BOUND long 10000000 |
| 18 | const PD0_EPISODE_STEPS uint 100 |
| 19 | const PD0_L6_REDRAW_CAP int 1000 |
| 20 | |
| 21 | const PD0_OK uchar 0 |
| 22 | const PD0_REFUSED_RANGE uchar 1 |
| 23 | const PD0_OUT_OF_BOUNDS uchar 2 |
| 24 | const PD0_EPISODE_END uchar 3 |
| 25 | const PD0_BUDGET_EXHAUSTED uchar 4 |
| 26 | |
| 27 | const PD0_KIND_RESET uchar 0 |
| 28 | const PD0_KIND_STEP uchar 1 |
| 29 | |
| 30 | const PD0_OP_DESCRIBE uchar 0 |
| 31 | const PD0_OP_RESET uchar 1 |
| 32 | const PD0_OP_STEP uchar 2 |
| 33 | const PD0_OP_SCORE uchar 3 |
| 34 | const PD0_OP_SHAPE uchar 4 |
| 35 | const PD0_OP_FINAL uchar 5 |
| 36 | const PD0_OP_AUDIT uchar 6 |
| 37 | |
| 38 | const PD0_AUDIT_RESP_SIZE ulong 57 |
| 39 | const PD0_SCORE_STEPS_MAX ulong 100 |
| 40 | const PD0_DESC_REFUSED_V1 int -2 |
| 41 | |
| 42 | const PD0_L0 int 0 |
| 43 | const PD0_L1 int 1 |
| 44 | const PD0_L2 int 2 |
| 45 | const PD0_L3 int 3 |
| 46 | const PD0_L4 int 4 |
| 47 | const PD0_L5 int 5 |
| 48 | const PD0_L6 int 6 |
| 49 | const PD0_LEVEL_NULL int 7 |
| 50 | const PD0_LEVELS int 8 |
| 51 | |
| 52 | const PD0_LAW_REJECTED uchar 0 |
| 53 | const PD0_LAW_REFUTED uchar 1 |
| 54 | const PD0_LAW_HYPOTHESIS uchar 2 |
| 55 | const PD0_LAW_PROVISIONAL uchar 3 |
| 56 | |
| 57 | struct Pd0Desc n_obs uchar n_channels uchar dt_micro long chan_min long 2 chan_max long 2 reset_min long 4 reset_max long 4 episode_max_steps uint budget_steps uint budget_episodes uint end |
| 58 | |
| 59 | struct Pd0Rec n_obs uchar status uchar kind uchar channel uchar seq ulong episode uint step_in_episode uint time_micro long requested long applied long before long 4 after long 4 prev_hash uint 8 hash uint 8 end |
| 60 | |
| 61 | struct Pd0Gen level int k long 4 sigma_obs long sigma_null long n_hidden int redraws int invalid int end |
| 62 | |
| 63 | struct Pd0World level int seed ulong noise_s ulong null_s ulong s long 4 h long in_episode int step_in_episode uint episodes_used uint steps_used uint episode uint seq ulong last_hash uint 8 end |
| 64 | |
| 65 | struct Pd0LawMeta state uchar n_refutations ushort eps_micro uint confidence_ppm uint n_obs_dom ulong n_ep_dom uint reset_min long reset_max long ep_len uint dt_micro long latent_reset long n_exc ushort n_exp ushort end |
| 66 | |
| 67 | note ---- Math helpers (f64) ---- |
| 68 | |
| 69 | fn pd0_llround x f64 to long |
| 70 | if ge x 0.0f64 |
| 71 | ret long add x 0.5f64 |
| 72 | end |
| 73 | ret long sub x 0.5f64 |
| 74 | end |
| 75 | |
| 76 | fn pd0_log2 x f64 to f64 |
| 77 | if le x 0.0f64 |
| 78 | ret -1000.0f64 |
| 79 | end |
| 80 | bits = bitcast ulong x |
| 81 | exp_raw = long and shr bits 52 0x7ff |
| 82 | e = sub exp_raw long 1023 |
| 83 | mant_bits = or and bits 0x000fffffffffffff 0x3ff0000000000000 |
| 84 | m = bitcast f64 mant_bits |
| 85 | if ge m 1.4142135623730951f64 |
| 86 | m = mul m 0.5f64 |
| 87 | e = add e long 1 |
| 88 | end |
| 89 | t = div sub m 1.0f64 add m 1.0f64 |
| 90 | t2 = mul t t |
| 91 | term = t |
| 92 | s = term |
| 93 | term = mul term t2 |
| 94 | s = add s div term 3.0f64 |
| 95 | term = mul term t2 |
| 96 | s = add s div term 5.0f64 |
| 97 | term = mul term t2 |
| 98 | s = add s div term 7.0f64 |
| 99 | term = mul term t2 |
| 100 | s = add s div term 9.0f64 |
| 101 | term = mul term t2 |
| 102 | s = add s div term 11.0f64 |
| 103 | term = mul term t2 |
| 104 | s = add s div term 13.0f64 |
| 105 | ln_m = mul 2.0f64 s |
| 106 | ret add f64 e mul ln_m 1.4426950408889634f64 |
| 107 | end |
| 108 | |
| 109 | note ---- Buffer append helpers ---- |
| 110 | |
| 111 | fn pd0_buf_ch dst edit uchar * pos ulong cap ulong ch uchar to ulong |
| 112 | if lt pos cap |
| 113 | put dst pos ch |
| 114 | ret add pos 1 |
| 115 | end |
| 116 | ret pos |
| 117 | end |
| 118 | |
| 119 | fn pd0_buf_str dst edit uchar * pos ulong cap ulong s view uchar * to ulong |
| 120 | n = len s |
| 121 | for i ulong n bound 4096 |
| 122 | if lt pos cap |
| 123 | put dst pos get s i |
| 124 | pos = add pos 1 |
| 125 | end |
| 126 | end |
| 127 | ret pos |
| 128 | end |
| 129 | |
| 130 | fn pd0_buf_u64 dst edit uchar * pos ulong cap ulong v ulong to ulong |
| 131 | if eq v ulong 0 |
| 132 | ret pd0_buf_ch dst pos cap uchar 48 |
| 133 | end |
| 134 | tmp = new uchar 24 0 |
| 135 | k = ulong 0 |
| 136 | cur = v |
| 137 | while gt cur ulong 0 bound 24 |
| 138 | d = rem cur ulong 10 |
| 139 | put tmp k uchar add ulong 48 d |
| 140 | k = add k 1 |
| 141 | cur = div cur ulong 10 |
| 142 | end |
| 143 | for i ulong k bound 24 |
| 144 | idx = sub sub k 1 i |
| 145 | if lt pos cap |
| 146 | put dst pos get tmp idx |
| 147 | pos = add pos 1 |
| 148 | end |
| 149 | end |
| 150 | ret pos |
| 151 | end |
| 152 | |
| 153 | fn pd0_buf_i64 dst edit uchar * pos ulong cap ulong v long to ulong |
| 154 | if lt v long 0 |
| 155 | pos = pd0_buf_ch dst pos cap uchar 45 |
| 156 | if eq v long -9223372036854775807 |
| 157 | ret pd0_buf_u64 dst pos cap ulong 9223372036854775807 |
| 158 | end |
| 159 | ret pd0_buf_u64 dst pos cap ulong neg v |
| 160 | end |
| 161 | ret pd0_buf_u64 dst pos cap ulong v |
| 162 | end |
| 163 | |
| 164 | fn pd0_buf_u64_pad6 dst edit uchar * pos ulong cap ulong v ulong to ulong |
| 165 | tmp = new uchar 6 48 |
| 166 | cur = v |
| 167 | for i ulong 6 |
| 168 | idx = sub ulong 5 i |
| 169 | d = rem cur ulong 10 |
| 170 | put tmp idx uchar add ulong 48 d |
| 171 | cur = div cur ulong 10 |
| 172 | end |
| 173 | for i ulong 6 |
| 174 | if lt pos cap |
| 175 | put dst pos get tmp i |
| 176 | pos = add pos 1 |
| 177 | end |
| 178 | end |
| 179 | ret pos |
| 180 | end |
| 181 | |
| 182 | fn pd0_buf_f64_6 dst edit uchar * pos ulong cap ulong x f64 to ulong |
| 183 | if lt x 0.0f64 |
| 184 | pos = pd0_buf_ch dst pos cap uchar 45 |
| 185 | x = neg x |
| 186 | end |
| 187 | sc = ulong pd0_llround mul x 1000000.0f64 |
| 188 | ip = div sc ulong 1000000 |
| 189 | fp = rem sc ulong 1000000 |
| 190 | pos = pd0_buf_u64 dst pos cap ip |
| 191 | pos = pd0_buf_ch dst pos cap uchar 46 |
| 192 | ret pd0_buf_u64_pad6 dst pos cap fp |
| 193 | end |
| 194 | |
| 195 | fn pd0_puts fd int s view uchar * to void |
| 196 | k = write fd s 0 len s |
| 197 | if lt k 0 |
| 198 | ret |
| 199 | end |
| 200 | ret |
| 201 | end |
| 202 | |
| 203 | fn pd0_put_bytes fd int s view uchar * n ulong to void |
| 204 | if eq n ulong 0 |
| 205 | ret |
| 206 | end |
| 207 | k = write fd s 0 n |
| 208 | if lt k 0 |
| 209 | ret |
| 210 | end |
| 211 | ret |
| 212 | end |
| 213 | |
| 214 | note ---- FIPS 180-4 SHA-256 ---- |
| 215 | |
| 216 | fn pd0_sha_kinit k edit uint 64 to void |
| 217 | put k 0 0x428a2f98 |
| 218 | put k 1 0x71374491 |
| 219 | put k 2 0xb5c0fbcf |
| 220 | put k 3 0xe9b5dba5 |
| 221 | put k 4 0x3956c25b |
| 222 | put k 5 0x59f111f1 |
| 223 | put k 6 0x923f82a4 |
| 224 | put k 7 0xab1c5ed5 |
| 225 | put k 8 0xd807aa98 |
| 226 | put k 9 0x12835b01 |
| 227 | put k 10 0x243185be |
| 228 | put k 11 0x550c7dc3 |
| 229 | put k 12 0x72be5d74 |
| 230 | put k 13 0x80deb1fe |
| 231 | put k 14 0x9bdc06a7 |
| 232 | put k 15 0xc19bf174 |
| 233 | put k 16 0xe49b69c1 |
| 234 | put k 17 0xefbe4786 |
| 235 | put k 18 0x0fc19dc6 |
| 236 | put k 19 0x240ca1cc |
| 237 | put k 20 0x2de92c6f |
| 238 | put k 21 0x4a7484aa |
| 239 | put k 22 0x5cb0a9dc |
| 240 | put k 23 0x76f988da |
| 241 | put k 24 0x983e5152 |
| 242 | put k 25 0xa831c66d |
| 243 | put k 26 0xb00327c8 |
| 244 | put k 27 0xbf597fc7 |
| 245 | put k 28 0xc6e00bf3 |
| 246 | put k 29 0xd5a79147 |
| 247 | put k 30 0x06ca6351 |
| 248 | put k 31 0x14292967 |
| 249 | put k 32 0x27b70a85 |
| 250 | put k 33 0x2e1b2138 |
| 251 | put k 34 0x4d2c6dfc |
| 252 | put k 35 0x53380d13 |
| 253 | put k 36 0x650a7354 |
| 254 | put k 37 0x766a0abb |
| 255 | put k 38 0x81c2c92e |
| 256 | put k 39 0x92722c85 |
| 257 | put k 40 0xa2bfe8a1 |
| 258 | put k 41 0xa81a664b |
| 259 | put k 42 0xc24b8b70 |
| 260 | put k 43 0xc76c51a3 |
| 261 | put k 44 0xd192e819 |
| 262 | put k 45 0xd6990624 |
| 263 | put k 46 0xf40e3585 |
| 264 | put k 47 0x106aa070 |
| 265 | put k 48 0x19a4c116 |
| 266 | put k 49 0x1e376c08 |
| 267 | put k 50 0x2748774c |
| 268 | put k 51 0x34b0bcb5 |
| 269 | put k 52 0x391c0cb3 |
| 270 | put k 53 0x4ed8aa4a |
| 271 | put k 54 0x5b9cca4f |
| 272 | put k 55 0x682e6ff3 |
| 273 | put k 56 0x748f82ee |
| 274 | put k 57 0x78a5636f |
| 275 | put k 58 0x84c87814 |
| 276 | put k 59 0x8cc70208 |
| 277 | put k 60 0x90befffa |
| 278 | put k 61 0xa4506ceb |
| 279 | put k 62 0xbef9a3f7 |
| 280 | put k 63 0xc67178f2 |
| 281 | ret |
| 282 | end |
| 283 | |
| 284 | fn pd0_sha_ch x uint y uint z uint to uint |
| 285 | a = and x y |
| 286 | b = and not x z |
| 287 | ret xor a b |
| 288 | end |
| 289 | |
| 290 | fn pd0_sha_maj x uint y uint z uint to uint |
| 291 | a = and x y |
| 292 | b = and x z |
| 293 | c = and y z |
| 294 | ret xor xor a b c |
| 295 | end |
| 296 | |
| 297 | fn pd0_sha_bs0 x uint to uint |
| 298 | ret xor xor rotr x 2 rotr x 13 rotr x 22 |
| 299 | end |
| 300 | |
| 301 | fn pd0_sha_bs1 x uint to uint |
| 302 | ret xor xor rotr x 6 rotr x 11 rotr x 25 |
| 303 | end |
| 304 | |
| 305 | fn pd0_sha_ss0 x uint to uint |
| 306 | ret xor xor rotr x 7 rotr x 18 shr x 3 |
| 307 | end |
| 308 | |
| 309 | fn pd0_sha_ss1 x uint to uint |
| 310 | ret xor xor rotr x 17 rotr x 19 shr x 10 |
| 311 | end |
| 312 | |
| 313 | fn pd0_sha_be32 buf view uchar 64 off ulong to uint |
| 314 | b0 = shl uint get buf off 24 |
| 315 | b1 = shl uint get buf add off 1 16 |
| 316 | b2 = shl uint get buf add off 2 8 |
| 317 | b3 = uint get buf add off 3 |
| 318 | ret or or b0 b1 or b2 b3 |
| 319 | end |
| 320 | |
| 321 | fn pd0_sha_compress st edit uint 8 blk view uchar 64 k view uint 64 to void |
| 322 | w = new uint 64 0 |
| 323 | v = new uint 8 0 |
| 324 | for i ulong 16 |
| 325 | put w i pd0_sha_be32 blk mul i 4 |
| 326 | end |
| 327 | for i ulong 48 |
| 328 | j = add i 16 |
| 329 | s0 = pd0_sha_ss0 get w sub j 15 |
| 330 | s1 = pd0_sha_ss1 get w sub j 2 |
| 331 | val = add add get w sub j 16 s0 add get w sub j 7 s1 |
| 332 | put w j val |
| 333 | end |
| 334 | for i ulong 8 |
| 335 | put v i get st i |
| 336 | end |
| 337 | for r ulong 64 |
| 338 | t1 = add get v 7 pd0_sha_bs1 get v 4 |
| 339 | t1 = add t1 pd0_sha_ch get v 4 get v 5 get v 6 |
| 340 | t1 = add t1 add get k r get w r |
| 341 | t2 = add pd0_sha_bs0 get v 0 pd0_sha_maj get v 0 get v 1 get v 2 |
| 342 | put v 7 get v 6 |
| 343 | put v 6 get v 5 |
| 344 | put v 5 get v 4 |
| 345 | put v 4 add get v 3 t1 |
| 346 | put v 3 get v 2 |
| 347 | put v 2 get v 1 |
| 348 | put v 1 get v 0 |
| 349 | put v 0 add t1 t2 |
| 350 | end |
| 351 | for i ulong 8 |
| 352 | put st i add get st i get v i |
| 353 | end |
| 354 | ret |
| 355 | end |
| 356 | |
| 357 | fn pd0_sha_emit st view uint 8 dst edit uchar 32 to void |
| 358 | for i ulong 8 |
| 359 | word = get st i |
| 360 | off = mul i 4 |
| 361 | put dst off uchar and shr word 24 0xff |
| 362 | put dst add off 1 uchar and shr word 16 0xff |
| 363 | put dst add off 2 uchar and shr word 8 0xff |
| 364 | put dst add off 3 uchar and word 0xff |
| 365 | end |
| 366 | ret |
| 367 | end |
| 368 | |
| 369 | fn pd0_sha256_hash msg view uchar * mlen ulong dst edit uchar 32 to void |
| 370 | st = new uint 8 0 |
| 371 | k = new uint 64 0 |
| 372 | blk = new uchar 64 0 |
| 373 | pd0_sha_kinit edit k |
| 374 | put st 0 0x6a09e667 |
| 375 | put st 1 0xbb67ae85 |
| 376 | put st 2 0x3c6ef372 |
| 377 | put st 3 0xa54ff53a |
| 378 | put st 4 0x510e527f |
| 379 | put st 5 0x9b05688c |
| 380 | put st 6 0x1f83d9ab |
| 381 | put st 7 0x5be0cd19 |
| 382 | pos = ulong 0 |
| 383 | while ge sub mlen pos 64 bound 262144 |
| 384 | for i ulong 64 |
| 385 | put blk i get msg add pos i |
| 386 | end |
| 387 | pd0_sha_compress edit st view blk view k |
| 388 | pos = add pos 64 |
| 389 | end |
| 390 | rem_len = sub mlen pos |
| 391 | for i ulong 64 |
| 392 | put blk i uchar 0 |
| 393 | end |
| 394 | for i ulong rem_len bound 64 |
| 395 | put blk i get msg add pos i |
| 396 | end |
| 397 | put blk rem_len uchar 0x80 |
| 398 | if gt rem_len 55 |
| 399 | pd0_sha_compress edit st view blk view k |
| 400 | for i ulong 64 |
| 401 | put blk i uchar 0 |
| 402 | end |
| 403 | end |
| 404 | bits = mul mlen 8 |
| 405 | put blk 56 uchar and shr bits 56 0xff |
| 406 | put blk 57 uchar and shr bits 48 0xff |
| 407 | put blk 58 uchar and shr bits 40 0xff |
| 408 | put blk 59 uchar and shr bits 32 0xff |
| 409 | put blk 60 uchar and shr bits 24 0xff |
| 410 | put blk 61 uchar and shr bits 16 0xff |
| 411 | put blk 62 uchar and shr bits 8 0xff |
| 412 | put blk 63 uchar and bits 0xff |
| 413 | pd0_sha_compress edit st view blk view k |
| 414 | pd0_sha_emit view st dst |
| 415 | ret |
| 416 | end |