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 / protocol / omnet / om / tka / tkapool.om
| 1 | note SPDX-License-Identifier: BSD-3-Clause |
| 2 | note Copyright (c) Tailscale Inc & contributors. See tailscale.LICENSE. |
| 3 | note Two independent authorities, each 128 immutable nodes. Scratch staging is unreachable until commit. |
| 4 | global tkap_a uchar 16777216 |
| 5 | global tkap_s uchar 16777216 |
| 6 | global tkap_h uchar 8192 |
| 7 | global tkap_m ulong 1536 |
| 8 | global tkap_c ulong 16 |
| 9 | global tkap_l ulong 1 |
| 10 | |
| 11 | fn tkaselect lane ulong to ulong |
| 12 | if gt lane 1 |
| 13 | ret 65538 |
| 14 | end |
| 15 | put tkap_l 0 lane |
| 16 | ret 0 |
| 17 | end |
| 18 | |
| 19 | fn tkareset to void |
| 20 | for i ulong 8 |
| 21 | put tkap_c add mul get tkap_l 0 8 i 0 |
| 22 | end |
| 23 | end |
| 24 | |
| 25 | fn tkacget f ulong to ulong |
| 26 | ret get tkap_c add mul get tkap_l 0 8 f |
| 27 | end |
| 28 | |
| 29 | fn tkacset f ulong v ulong to void |
| 30 | put tkap_c add mul get tkap_l 0 8 f v |
| 31 | end |
| 32 | |
| 33 | fn tkaidx i ulong to ulong |
| 34 | ret add mul get tkap_l 0 128 i |
| 35 | end |
| 36 | |
| 37 | fn tkamget i ulong f ulong to ulong |
| 38 | ret get tkap_m add mul tkaidx i 6 f |
| 39 | end |
| 40 | |
| 41 | fn tkamset i ulong f ulong v ulong to void |
| 42 | put tkap_m add mul tkaidx i 6 f v |
| 43 | end |
| 44 | |
| 45 | fn tkaget i ulong state ulong o edit uchar 65536 to ulong |
| 46 | if ge i tkacget 0 |
| 47 | ret 65541 |
| 48 | end |
| 49 | n = tkamget i state |
| 50 | p = mul tkaidx i 65536 |
| 51 | for j ulong n |
| 52 | if eq state 0 |
| 53 | put o j get tkap_a add p j |
| 54 | else |
| 55 | put o j get tkap_s add p j |
| 56 | end |
| 57 | end |
| 58 | ret n |
| 59 | end |
| 60 | |
| 61 | fn tkahget i ulong o edit uchar 32 to void |
| 62 | for j ulong 32 |
| 63 | put o j get tkap_h add mul tkaidx i 32 j |
| 64 | end |
| 65 | end |
| 66 | |
| 67 | fn tkahead o edit uchar 32 to ulong |
| 68 | if eq tkacget 0 0 |
| 69 | ret 65542 |
| 70 | end |
| 71 | tkahget tkacget 1 o |
| 72 | ret 0 |
| 73 | end |
| 74 | |
| 75 | fn tkafind h view uchar 32 to ulong |
| 76 | for i ulong tkacget 0 |
| 77 | same = ulong 1 |
| 78 | for j ulong 32 |
| 79 | if ne get h j get tkap_h add mul tkaidx i 32 j |
| 80 | same = 0 |
| 81 | end |
| 82 | end |
| 83 | if ne same 0 |
| 84 | ret i |
| 85 | end |
| 86 | end |
| 87 | ret 65541 |
| 88 | end |
| 89 | |
| 90 | fn tkastate h view uchar 32 o edit uchar 65536 to ulong |
| 91 | i = tkafind h |
| 92 | if ge i 65537 |
| 93 | ret i |
| 94 | end |
| 95 | ret tkaget i 1 o |
| 96 | end |
| 97 | |
| 98 | fn tkacurrent o edit uchar 65536 to ulong |
| 99 | if eq tkacget 0 0 |
| 100 | ret 65542 |
| 101 | end |
| 102 | ret tkaget tkacget 1 1 o |
| 103 | end |
| 104 | |
| 105 | fn tkaexport s view uchar 65536 p ulong o edit uchar 16384 n ulong to void |
| 106 | for i ulong 32 |
| 107 | put o add n i get s add p i |
| 108 | end |
| 109 | end |
| 110 | |
| 111 | fn tkakeys o edit uchar 16384 to ulong |
| 112 | s = new uchar 65536 0 |
| 113 | n = tkacurrent s |
| 114 | if gt n 65536 |
| 115 | ret n |
| 116 | end |
| 117 | t = new ulong 131074 0 |
| 118 | n = cbrdec s n t |
| 119 | r = cbrmfind t 0 3 |
| 120 | j = add r 1 |
| 121 | cnt = ulong 0 |
| 122 | for i ulong cbrval t r |
| 123 | p = cbrmfind t j 3 |
| 124 | if eq cbrval t p 32 |
| 125 | p = cbrpoff t p |
| 126 | n = mul cnt 32 |
| 127 | tkaexport s p o n |
| 128 | cnt = add cnt 1 |
| 129 | end |
| 130 | j = cbrnext t j |
| 131 | end |
| 132 | ret cnt |
| 133 | end |
| 134 | |
| 135 | fn tkastore a view uchar 65536 n ulong s view uchar 65536 sn ulong parent ulong to ulong |
| 136 | i = tkacget 0 |
| 137 | if ge i 128 |
| 138 | ret 65538 |
| 139 | end |
| 140 | h = new uchar 32 0 |
| 141 | z = tkahash a n 0 h |
| 142 | if ne z 0 |
| 143 | ret z |
| 144 | end |
| 145 | p = mul tkaidx i 65536 |
| 146 | for j ulong n |
| 147 | put tkap_a add p j get a j |
| 148 | end |
| 149 | for j ulong sn |
| 150 | put tkap_s add p j get s j |
| 151 | end |
| 152 | p = mul tkaidx i 32 |
| 153 | for j ulong 32 |
| 154 | put tkap_h add p j get h j |
| 155 | end |
| 156 | tkamset i 0 n |
| 157 | tkamset i 1 sn |
| 158 | tkamset i 2 parent |
| 159 | t = new ulong 131074 0 |
| 160 | z = cbrdec a n t |
| 161 | tkamset i 4 tkalen t 0 1 |
| 162 | tkacset 0 add i 1 |
| 163 | ret i |
| 164 | end |
| 165 | |
| 166 | fn tkaboot a view uchar 65536 n ulong to ulong |
| 167 | if ne tkacget 0 0 |
| 168 | ret 65541 |
| 169 | end |
| 170 | b = new uchar 65536 0 |
| 171 | z = tkamodel a n 1 b 1 |
| 172 | if gt z 65536 |
| 173 | ret z |
| 174 | end |
| 175 | t = new ulong 131074 0 |
| 176 | x = cbrdec b z t |
| 177 | if ne tkalen t 0 1 5 |
| 178 | ret 65539 |
| 179 | end |
| 180 | r = cbrmfind t 0 5 |
| 181 | if eq r 0 |
| 182 | ret 65539 |
| 183 | end |
| 184 | s = new uchar 65536 0 |
| 185 | w = new ulong 4 0 |
| 186 | tkaraw b t r s w |
| 187 | o = new uchar 65536 0 |
| 188 | sn = tkatrusted b z s get w 0 o 1 |
| 189 | if gt sn 65536 |
| 190 | ret sn |
| 191 | end |
| 192 | x = tkastore b z o sn 65541 |
| 193 | if ge x 65537 |
| 194 | ret x |
| 195 | end |
| 196 | tkacset 1 0 |
| 197 | ret 0 |
| 198 | end |
| 199 | |
| 200 | fn tkabegin to ulong |
| 201 | if or eq tkacget 0 0 ne tkacget 3 0 |
| 202 | ret 65542 |
| 203 | end |
| 204 | tkacset 2 tkacget 0 |
| 205 | tkacset 3 1 |
| 206 | ret 0 |
| 207 | end |
| 208 | |
| 209 | fn tkaabort to void |
| 210 | if ne tkacget 3 0 |
| 211 | tkacset 0 tkacget 2 |
| 212 | tkacset 3 0 |
| 213 | end |
| 214 | end |
| 215 | |
| 216 | fn tkastageone a view uchar 65536 n ulong to ulong |
| 217 | if eq tkacget 3 0 |
| 218 | ret 65542 |
| 219 | end |
| 220 | b = new uchar 65536 0 |
| 221 | z = tkamodel a n 1 b 1 |
| 222 | if gt z 65536 |
| 223 | ret z |
| 224 | end |
| 225 | h = new uchar 32 0 |
| 226 | x = tkahash b z 0 h |
| 227 | if ne x 0 |
| 228 | ret x |
| 229 | end |
| 230 | if lt tkafind h 65537 |
| 231 | ret 0 |
| 232 | end |
| 233 | t = new ulong 131074 0 |
| 234 | x = cbrdec b z t |
| 235 | r = cbrmfind t 0 2 |
| 236 | if eq tkalen t 0 2 0 |
| 237 | ret 65541 |
| 238 | end |
| 239 | note Parent() copies up to32 bytes, but applyVerifiedAUM ultimately requires exact32. |
| 240 | if ne cbrval t r 32 |
| 241 | ret 65541 |
| 242 | end |
| 243 | for i ulong 32 |
| 244 | put h i get b add cbrpoff t r i |
| 245 | end |
| 246 | parent = tkafind h |
| 247 | if ge parent 65537 |
| 248 | ret parent |
| 249 | end |
| 250 | s = new uchar 65536 0 |
| 251 | sn = tkaget parent 1 s |
| 252 | o = new uchar 65536 0 |
| 253 | on = tkatrusted b z s sn o 0 |
| 254 | if gt on 65536 |
| 255 | ret on |
| 256 | end |
| 257 | x = tkastore b z o on parent |
| 258 | if ge x 65537 |
| 259 | ret x |
| 260 | end |
| 261 | parent = tkaweightbuf b z s sn |
| 262 | tkamset x 3 parent |
| 263 | ret 0 |
| 264 | end |
| 265 | |
| 266 | fn tkastage a view uchar 65536 n ulong to ulong |
| 267 | z = tkastageone a n |
| 268 | if ne z 0 |
| 269 | tkaabort |
| 270 | end |
| 271 | ret z |
| 272 | end |
| 273 | |
| 274 | fn tkahless a ulong b ulong to ulong |
| 275 | for j ulong 32 |
| 276 | x = get tkap_h add mul tkaidx a 32 j |
| 277 | y = get tkap_h add mul tkaidx b 32 j |
| 278 | if ne x y |
| 279 | ret tkabool lt x y |
| 280 | end |
| 281 | end |
| 282 | ret 0 |
| 283 | end |
| 284 | |
| 285 | fn tkanext parent ulong to ulong |
| 286 | best = ulong 65541 |
| 287 | for i ulong tkacget 0 |
| 288 | if eq tkamget i 2 parent |
| 289 | if eq best 65541 |
| 290 | best = i |
| 291 | else |
| 292 | x = tkamget i 3 |
| 293 | y = tkamget best 3 |
| 294 | if gt x y |
| 295 | best = i |
| 296 | else |
| 297 | if eq x y |
| 298 | x = tkabool eq tkamget i 4 2 |
| 299 | y = tkabool eq tkamget best 4 2 |
| 300 | if gt x y |
| 301 | best = i |
| 302 | else |
| 303 | if and eq x y ne tkahless i best 0 |
| 304 | best = i |
| 305 | end |
| 306 | end |
| 307 | end |
| 308 | end |
| 309 | end |
| 310 | end |
| 311 | end |
| 312 | ret best |
| 313 | end |
| 314 | |
| 315 | fn tkacommit to ulong |
| 316 | if eq tkacget 3 0 |
| 317 | ret 65542 |
| 318 | end |
| 319 | head = ulong 0 |
| 320 | for i ulong 128 |
| 321 | next = tkanext head |
| 322 | if eq next 65541 |
| 323 | tkacset 1 head |
| 324 | tkacset 3 0 |
| 325 | ret 0 |
| 326 | end |
| 327 | head = next |
| 328 | end |
| 329 | tkaabort |
| 330 | ret 65538 |
| 331 | end |
| 332 | |
| 333 | fn tkainform d view uchar 65536 spans view ulong 256 count ulong to ulong |
| 334 | if or eq count 0 gt count 128 |
| 335 | ret 65538 |
| 336 | end |
| 337 | z = tkabegin |
| 338 | if ne z 0 |
| 339 | ret z |
| 340 | end |
| 341 | b = new uchar 65536 0 |
| 342 | for i ulong count |
| 343 | p = get spans mul i 2 |
| 344 | n = get spans add mul i 2 1 |
| 345 | if or gt p 65536 gt n sub 65536 p |
| 346 | tkaabort |
| 347 | ret 65538 |
| 348 | end |
| 349 | for j ulong n |
| 350 | put b j get d add p j |
| 351 | end |
| 352 | z = tkastage b n |
| 353 | if ne z 0 |
| 354 | tkaabort |
| 355 | ret z |
| 356 | end |
| 357 | end |
| 358 | ret tkacommit |
| 359 | end |
| 360 | |
| 361 | note Offer buffer consists of head then ancestor hashes; return total hash count. |
| 362 | fn tkaoffer o edit uchar 65536 to ulong |
| 363 | if eq tkacget 0 0 |
| 364 | ret 65542 |
| 365 | end |
| 366 | cur = tkacget 1 |
| 367 | h = new uchar 32 0 |
| 368 | tkahget cur h |
| 369 | for j ulong 32 |
| 370 | put o j get h j |
| 371 | end |
| 372 | cnt = ulong 1 |
| 373 | skip = ulong 4 |
| 374 | for i ulong 128 |
| 375 | if and gt i 0 eq rem i skip 0 |
| 376 | tkahget cur h |
| 377 | for j ulong 32 |
| 378 | put o add mul cnt 32 j get h j |
| 379 | end |
| 380 | cnt = add cnt 1 |
| 381 | skip = mul skip 4 |
| 382 | end |
| 383 | if eq cur 0 |
| 384 | break |
| 385 | end |
| 386 | cur = tkamget cur 2 |
| 387 | end |
| 388 | tkahget 0 h |
| 389 | for j ulong 32 |
| 390 | put o add mul cnt 32 j get h j |
| 391 | end |
| 392 | ret add cnt 1 |
| 393 | end |
| 394 | |
| 395 | fn tkaonpath node ulong to ulong |
| 396 | cur = tkacget 1 |
| 397 | for i ulong 128 |
| 398 | if eq cur node |
| 399 | ret 1 |
| 400 | end |
| 401 | if eq cur 0 |
| 402 | ret 0 |
| 403 | end |
| 404 | cur = tkamget cur 2 |
| 405 | end |
| 406 | ret 0 |
| 407 | end |
| 408 | |
| 409 | note Intersection returns node index; mode[0]=0 up-to-date,1 head,2 tail. |
| 410 | fn tkainter offer view uchar 65536 count ulong mode edit ulong 1 to ulong |
| 411 | if or eq count 0 gt count 2048 |
| 412 | ret 65538 |
| 413 | end |
| 414 | if eq tkacget 0 0 |
| 415 | ret 65542 |
| 416 | end |
| 417 | h = new uchar 32 0 |
| 418 | for i ulong count |
| 419 | for j ulong 32 |
| 420 | put h j get offer add mul i 32 j |
| 421 | end |
| 422 | node = tkafind h |
| 423 | if lt node 65537 |
| 424 | if ne tkaonpath node 0 |
| 425 | put mode 0 2 |
| 426 | if eq i 0 |
| 427 | put mode 0 1 |
| 428 | if eq node tkacget 1 |
| 429 | put mode 0 0 |
| 430 | end |
| 431 | end |
| 432 | ret node |
| 433 | end |
| 434 | end |
| 435 | end |
| 436 | ret 65542 |
| 437 | end |
| 438 | |
| 439 | note Missing result is ordered node indices; retrieve bytes with tkaget(index,0,out). |
| 440 | fn tkamissing offer view uchar 65536 count ulong nodes edit ulong 128 to ulong |
| 441 | mode = new ulong 1 0 |
| 442 | cur = tkainter offer count mode |
| 443 | if ge cur 65537 |
| 444 | ret cur |
| 445 | end |
| 446 | if eq get mode 0 0 |
| 447 | ret 0 |
| 448 | end |
| 449 | n = ulong 0 |
| 450 | for i ulong 128 |
| 451 | cur = tkanext cur |
| 452 | if eq cur 65541 |
| 453 | ret n |
| 454 | end |
| 455 | put nodes n cur |
| 456 | n = add n 1 |
| 457 | end |
| 458 | ret 65538 |
| 459 | end |