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 / codec / codec.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note codec.om: the text encodings of the Tailnet Lock JSON wire format, matching Go go1.26.0 |
| 3 | note encoding/base64, encoding/base32 and encoding/hex and tailscale.com v1.102.0 byte for byte. |
| 4 | note Library unit: functions only, no main, no globals. Every name starts with cd. |
| 5 | note |
| 6 | note Byte strings are fixed-capacity arrays (view/edit uchar 65536) plus an explicit length. |
| 7 | note Decoders read untrusted text: a malformed input returns cdbad (65537) or 0, never traps. |
| 8 | note A length beyond the stated capacity is a caller contract violation: trap 3 before any write. |
| 9 | note |
| 10 | note fn cdb64enc s view uchar 65536 n ulong o edit uchar 65536 to ulong |
| 11 | note base64.StdEncoding.Encode (RFC 4648 4, padded) of s[0..n), n 0..49152; returns the length. |
| 12 | note fn cdb64dec s view uchar 65536 n ulong o edit uchar 65536 to ulong |
| 13 | note base64.StdEncoding.Decode of s[0..n), n 0..65536, the decoder encoding/json uses for []byte. |
| 14 | note Returns the byte count (o[0..count) holds the bytes) or cdbad. |
| 15 | note fn cdb32enc s view uchar 65536 n ulong o edit uchar 65536 to ulong |
| 16 | note base32.StdEncoding.WithPadding(NoPadding) encode (RFC 4648 6, no padding), n 0..40960. |
| 17 | note fn cdb32dec s view uchar 65536 n ulong o edit uchar 65536 to ulong |
| 18 | note base32.StdEncoding.WithPadding(NoPadding).DecodeString of s[0..n), n 0..65536. |
| 19 | note fn cdhexenc s view uchar 65536 n ulong o edit uchar 65536 to ulong |
| 20 | note hex.Encode (lowercase, RFC 4648 8), n 0..32768. |
| 21 | note fn cdhexdec s view uchar 65536 n ulong o edit uchar 65536 to ulong |
| 22 | note hex.DecodeString of s[0..n), n 0..65536; returns n/2 or cdbad. |
| 23 | note fn cdaumenc h view uchar 32 o edit uchar 65536 to ulong |
| 24 | note tka.AUMHash.MarshalText: 52 base32 characters; returns 52. |
| 25 | note fn cdaumdec s view uchar 65536 n ulong h edit uchar 32 to ulong |
| 26 | note tka.AUMHash.UnmarshalText into a zero AUMHash: 1 accepted, 0 rejected (h all zero). |
| 27 | note fn cdnkenc k view uchar 32 o edit uchar 65536 to ulong |
| 28 | note key.NodePublic.MarshalText: "nodekey:" and 64 lowercase hex digits; returns 72. |
| 29 | note fn cdnkdec s view uchar 65536 n ulong k edit uchar 32 to ulong |
| 30 | note key.NodePublic.UnmarshalText: 1 accepted, 0 rejected (k all zero). |
| 31 | note |
| 32 | note On cdbad the output bytes written so far are not cleared; callers discard o. |
| 33 | note NOT constant-time-audited: Omega gives no timing guarantee and none was measured. |
| 34 | |
| 35 | const cdbad ulong 65537 |
| 36 | note "nodekey:" (types/key/node.go nodePublicHexPrefix) as 8 big-endian bytes 6e 6f 64 65 6b 65 79 3a |
| 37 | const cdnkpfx ulong 7957689453343242554 |
| 38 | |
| 39 | note base64 value of one character (RFC 4648 table 1), 255 when outside the alphabet |
| 40 | fn cdb64v c ulong to ulong |
| 41 | if and ge c 65 le c 90 |
| 42 | ret sub c 65 |
| 43 | end |
| 44 | if and ge c 97 le c 122 |
| 45 | ret sub c 71 |
| 46 | end |
| 47 | if and ge c 48 le c 57 |
| 48 | ret add c 4 |
| 49 | end |
| 50 | if eq c 43 |
| 51 | ret 62 |
| 52 | end |
| 53 | if eq c 47 |
| 54 | ret 63 |
| 55 | end |
| 56 | ret 255 |
| 57 | end |
| 58 | |
| 59 | note base64 character of a value 0..63 (RFC 4648 table 1) |
| 60 | fn cdb64c v ulong to uchar |
| 61 | if lt v 26 |
| 62 | ret uchar add v 65 |
| 63 | end |
| 64 | if lt v 52 |
| 65 | ret uchar add v 71 |
| 66 | end |
| 67 | if lt v 62 |
| 68 | ret uchar sub v 4 |
| 69 | end |
| 70 | if eq v 62 |
| 71 | ret 43 |
| 72 | end |
| 73 | ret 47 |
| 74 | end |
| 75 | |
| 76 | note base32 value of one character (RFC 4648 table 3), 255 outside A-Z 2-7 (lowercase too) |
| 77 | fn cdb32v c ulong to ulong |
| 78 | if and ge c 65 le c 90 |
| 79 | ret sub c 65 |
| 80 | end |
| 81 | if and ge c 50 le c 55 |
| 82 | ret sub c 24 |
| 83 | end |
| 84 | ret 255 |
| 85 | end |
| 86 | |
| 87 | note base32 character of a value 0..31 (RFC 4648 table 3) |
| 88 | fn cdb32c v ulong to uchar |
| 89 | if lt v 26 |
| 90 | ret uchar add v 65 |
| 91 | end |
| 92 | ret uchar add v 24 |
| 93 | end |
| 94 | |
| 95 | note hex digit value, either case (hex.go reverseHexTable), 255 otherwise |
| 96 | fn cdhexv c ulong to ulong |
| 97 | if and ge c 48 le c 57 |
| 98 | ret sub c 48 |
| 99 | end |
| 100 | if and ge c 97 le c 102 |
| 101 | ret sub c 87 |
| 102 | end |
| 103 | if and ge c 65 le c 70 |
| 104 | ret sub c 55 |
| 105 | end |
| 106 | ret 255 |
| 107 | end |
| 108 | |
| 109 | note lowercase hex digit of a value 0..15 (hex.go hextable "0123456789abcdef") |
| 110 | fn cdhexc v ulong to uchar |
| 111 | if lt v 10 |
| 112 | ret uchar add v 48 |
| 113 | end |
| 114 | ret uchar add v 87 |
| 115 | end |
| 116 | |
| 117 | note the byte of the two hex digits at s[i], s[i+1], or 256 when either is not a hex digit |
| 118 | fn cdhexb s view uchar 65536 i ulong to ulong |
| 119 | a = cdhexv ulong get s i |
| 120 | b = cdhexv ulong get s add i 1 |
| 121 | if or gt a 15 gt b 15 |
| 122 | ret 256 |
| 123 | end |
| 124 | ret or shl a 4 b |
| 125 | end |
| 126 | |
| 127 | fn cdzero32 k edit uchar 32 to void |
| 128 | for i ulong 32 |
| 129 | put k i 0 |
| 130 | end |
| 131 | end |
| 132 | |
| 133 | note m bytes of s from i (m 1..w) as a big-endian w-byte number, missing bytes 0 |
| 134 | fn cdgetbe s view uchar 65536 i ulong m ulong w ulong to ulong |
| 135 | x = ulong 0 |
| 136 | for t ulong w |
| 137 | x = shl x 8 |
| 138 | if lt t m |
| 139 | x = or x ulong get s add i t |
| 140 | end |
| 141 | end |
| 142 | ret x |
| 143 | end |
| 144 | |
| 145 | note the first m bytes of the w-byte big-endian number x at o[q..q+m) |
| 146 | fn cdputbe o edit uchar 65536 q ulong x ulong m ulong w ulong to void |
| 147 | for t ulong m |
| 148 | sh = mul sub sub w 1 t 8 |
| 149 | v = and shr x sh 255 |
| 150 | put o add q t uchar v |
| 151 | end |
| 152 | end |
| 153 | |
| 154 | note four base64 characters at o[p..p+4) of the 24-bit group x: m sextets, then pad '=' |
| 155 | fn cdb64put o edit uchar 65536 p ulong x ulong m ulong to void |
| 156 | for t ulong 4 |
| 157 | if lt t m |
| 158 | put o add p t cdb64c and shr x sub 18 mul t 6 63 |
| 159 | else |
| 160 | put o add p t 61 |
| 161 | end |
| 162 | end |
| 163 | end |
| 164 | |
| 165 | note RFC 4648 4: 3 bytes -> 4 characters; a final 1 or 2 bytes -> 2 or 3 characters and "==" or "=" |
| 166 | fn cdb64enc s view uchar 65536 n ulong o edit uchar 65536 to ulong |
| 167 | if gt n 49152 |
| 168 | trap 3 |
| 169 | end |
| 170 | f = div n 3 |
| 171 | for k ulong f |
| 172 | cdb64put o mul k 4 cdgetbe s mul k 3 3 3 4 |
| 173 | end |
| 174 | r = sub n mul f 3 |
| 175 | p = mul f 4 |
| 176 | if eq r 0 |
| 177 | ret p |
| 178 | end |
| 179 | cdb64put o p cdgetbe s mul f 3 r 3 add r 1 |
| 180 | ret add p 4 |
| 181 | end |
| 182 | |
| 183 | note base64.StdEncoding.Decode (base64.go 312-407, 518-584). CR and LF are skipped everywhere |
| 184 | note (base64.go 340, 356-359, 372-375), so the rest must be whole quanta of 4; only the last may end in |
| 185 | note "xx==" or "xxx=", and after the padding only CR/LF may follow. Non-zero trailing bits are |
| 186 | note accepted (no Strict, base64.go 394, 401). pd: 0 no '=', 1 one '=' after 2 symbols, 2 done. |
| 187 | fn cdb64dec s view uchar 65536 n ulong o edit uchar 65536 to ulong |
| 188 | if gt n 65536 |
| 189 | trap 3 |
| 190 | end |
| 191 | acc = ulong 0 |
| 192 | j = ulong 0 |
| 193 | q = ulong 0 |
| 194 | pd = ulong 0 |
| 195 | for i ulong n |
| 196 | c = ulong get s i |
| 197 | if or eq c 10 eq c 13 |
| 198 | continue |
| 199 | end |
| 200 | if eq pd 2 |
| 201 | ret cdbad |
| 202 | end |
| 203 | if eq pd 1 |
| 204 | if ne c 61 |
| 205 | ret cdbad |
| 206 | end |
| 207 | pd = 2 |
| 208 | continue |
| 209 | end |
| 210 | if eq c 61 |
| 211 | if eq j 2 |
| 212 | pd = 1 |
| 213 | continue |
| 214 | end |
| 215 | if eq j 3 |
| 216 | pd = 2 |
| 217 | continue |
| 218 | end |
| 219 | ret cdbad |
| 220 | end |
| 221 | v = cdb64v c |
| 222 | if gt v 63 |
| 223 | ret cdbad |
| 224 | end |
| 225 | acc = or shl acc 6 v |
| 226 | j = add j 1 |
| 227 | if eq j 4 |
| 228 | cdputbe o q acc 3 3 |
| 229 | q = add q 3 |
| 230 | acc = 0 |
| 231 | j = 0 |
| 232 | end |
| 233 | end |
| 234 | if eq pd 1 |
| 235 | ret cdbad |
| 236 | end |
| 237 | if eq pd 0 |
| 238 | if ne j 0 |
| 239 | ret cdbad |
| 240 | end |
| 241 | ret q |
| 242 | end |
| 243 | note pd 2: j is 2 or 3; the j symbols, left-aligned in 24 bits, give j - 1 bytes |
| 244 | cdputbe o q shl acc mul sub 4 j 6 sub j 1 3 |
| 245 | ret add q sub j 1 |
| 246 | end |
| 247 | |
| 248 | note m base32 characters at o[p..) of the 40-bit group x |
| 249 | fn cdb32put o edit uchar 65536 p ulong x ulong m ulong to void |
| 250 | for t ulong m |
| 251 | put o add p t cdb32c and shr x sub 35 mul t 5 31 |
| 252 | end |
| 253 | end |
| 254 | |
| 255 | note RFC 4648 6 without padding (base32.go 121-187, EncodedLen 284-289): 5 bytes -> 8 characters, |
| 256 | note a final 1, 2, 3 or 4 bytes -> 2, 4, 5 or 7 characters |
| 257 | fn cdb32enc s view uchar 65536 n ulong o edit uchar 65536 to ulong |
| 258 | if gt n 40960 |
| 259 | trap 3 |
| 260 | end |
| 261 | f = div n 5 |
| 262 | for k ulong f |
| 263 | cdb32put o mul k 8 cdgetbe s mul k 5 5 5 8 |
| 264 | end |
| 265 | r = sub n mul f 5 |
| 266 | p = mul f 8 |
| 267 | if eq r 0 |
| 268 | ret p |
| 269 | end |
| 270 | c = div add mul r 8 4 5 |
| 271 | cdb32put o p cdgetbe s mul f 5 r 5 c |
| 272 | ret add p c |
| 273 | end |
| 274 | |
| 275 | note base32.go stripNewlines 545-555: copy s[0..n) without CR and LF into t, return the count |
| 276 | fn cdstrip s view uchar 65536 n ulong t edit uchar 65536 to ulong |
| 277 | l = ulong 0 |
| 278 | for i ulong n |
| 279 | c = get s i |
| 280 | if and ne c 10 ne c 13 |
| 281 | put t l c |
| 282 | l = add l 1 |
| 283 | end |
| 284 | end |
| 285 | ret l |
| 286 | end |
| 287 | |
| 288 | note base32.go 330-349, the padding branch, entered when the byte just read (t[p-1]) is the pad |
| 289 | note byte, j >= 2 symbols are held and fewer than 8 bytes remain. With NoPadding the pad byte is |
| 290 | note byte(rune(-1)) = 255. Returns 1 when Go accepts the padding, 0 when it reports an error. |
| 291 | note Bytes past the 7 - j checked ones are not looked at, as in Go. |
| 292 | fn cdb32pad t view uchar 65536 p ulong l ulong j ulong to ulong |
| 293 | rm = sub l p |
| 294 | if lt add rm j 7 |
| 295 | ret 0 |
| 296 | end |
| 297 | for k ulong sub 7 j |
| 298 | if gt rm k |
| 299 | if ne get t add p k 255 |
| 300 | ret 0 |
| 301 | end |
| 302 | end |
| 303 | end |
| 304 | if or eq j 3 eq j 6 |
| 305 | ret 0 |
| 306 | end |
| 307 | ret 1 |
| 308 | end |
| 309 | |
| 310 | note output bytes of a quantum of dl symbols (base32.go 362-382): 8 7 5 4 2 -> 5 4 3 2 1; |
| 311 | note 1, 3 and 6 symbols at the end of the input give no byte and no error |
| 312 | fn cdb32nb dl ulong to ulong |
| 313 | if eq dl 8 |
| 314 | ret 5 |
| 315 | end |
| 316 | if eq dl 7 |
| 317 | ret 4 |
| 318 | end |
| 319 | if eq dl 5 |
| 320 | ret 3 |
| 321 | end |
| 322 | if eq dl 4 |
| 323 | ret 2 |
| 324 | end |
| 325 | if eq dl 2 |
| 326 | ret 1 |
| 327 | end |
| 328 | ret 0 |
| 329 | end |
| 330 | |
| 331 | note write the bytes of the quantum d[0..dl) at o[q..); returns the count |
| 332 | fn cdb32out d view ulong 8 dl ulong o edit uchar 65536 q ulong to ulong |
| 333 | x = ulong 0 |
| 334 | for i ulong 8 |
| 335 | x = or shl x 5 get d i |
| 336 | end |
| 337 | nb = cdb32nb dl |
| 338 | cdputbe o q x nb 5 |
| 339 | ret nb |
| 340 | end |
| 341 | |
| 342 | note one quantum of base32.go decode (305-358) from t[p..l), p < l, into d[0..dl). Returns |
| 343 | note p' * 16 + dl, with p' the next read position, or 0 when Go reports an error. Go ends the |
| 344 | note input (end = true) exactly when dl < 8: at the end of t, or in the padding branch. |
| 345 | fn cdb32q t view uchar 65536 p ulong l ulong d edit ulong 8 to ulong |
| 346 | for i ulong 8 |
| 347 | put d i 0 |
| 348 | end |
| 349 | j = ulong 0 |
| 350 | while lt j 8 bound 9 |
| 351 | if eq p l |
| 352 | ret add mul p 16 j |
| 353 | end |
| 354 | c = ulong get t p |
| 355 | p = add p 1 |
| 356 | if and eq c 255 and ge j 2 lt sub l p 8 |
| 357 | if eq cdb32pad t p l j 0 |
| 358 | ret 0 |
| 359 | end |
| 360 | ret add mul p 16 j |
| 361 | end |
| 362 | v = cdb32v c |
| 363 | if gt v 31 |
| 364 | ret 0 |
| 365 | end |
| 366 | put d j v |
| 367 | j = add j 1 |
| 368 | end |
| 369 | ret add mul p 16 8 |
| 370 | end |
| 371 | |
| 372 | note base32.go decode 305-386 on the newline-free t[0..l) with padChar = NoPadding |
| 373 | fn cdb32raw t view uchar 65536 l ulong o edit uchar 65536 to ulong |
| 374 | d = new ulong 8 0 |
| 375 | p = ulong 0 |
| 376 | q = ulong 0 |
| 377 | while lt p l bound 999999 |
| 378 | r = cdb32q t p l d |
| 379 | if eq r 0 |
| 380 | ret cdbad |
| 381 | end |
| 382 | p = shr r 4 |
| 383 | dl = and r 15 |
| 384 | q = add q cdb32out d dl o q |
| 385 | if lt dl 8 |
| 386 | ret q |
| 387 | end |
| 388 | end |
| 389 | ret q |
| 390 | end |
| 391 | |
| 392 | note base32.go DecodeString 421-426: strip CR and LF, then decode |
| 393 | fn cdb32dec s view uchar 65536 n ulong o edit uchar 65536 to ulong |
| 394 | if gt n 65536 |
| 395 | trap 3 |
| 396 | end |
| 397 | t = new uchar 65536 0 |
| 398 | l = cdstrip s n t |
| 399 | ret cdb32raw t l o |
| 400 | end |
| 401 | |
| 402 | note hex.go Encode 45-53, lowercase |
| 403 | fn cdhexenc s view uchar 65536 n ulong o edit uchar 65536 to ulong |
| 404 | if gt n 32768 |
| 405 | trap 3 |
| 406 | end |
| 407 | for i ulong n |
| 408 | v = ulong get s i |
| 409 | put o mul i 2 cdhexc shr v 4 |
| 410 | put o add mul i 2 1 cdhexc and v 15 |
| 411 | end |
| 412 | ret mul n 2 |
| 413 | end |
| 414 | |
| 415 | note hex.go DecodeString 138-142 and Decode 87-113: odd length or a non-hex byte is an error |
| 416 | fn cdhexdec s view uchar 65536 n ulong o edit uchar 65536 to ulong |
| 417 | if gt n 65536 |
| 418 | trap 3 |
| 419 | end |
| 420 | if ne and n 1 0 |
| 421 | ret cdbad |
| 422 | end |
| 423 | for i ulong div n 2 |
| 424 | v = cdhexb s mul i 2 |
| 425 | if gt v 255 |
| 426 | ret cdbad |
| 427 | end |
| 428 | put o i uchar v |
| 429 | end |
| 430 | ret div n 2 |
| 431 | end |
| 432 | |
| 433 | note tka/aum.go AppendText 44-46: base32StdNoPad of the 32 hash bytes |
| 434 | fn cdaumenc h view uchar 32 o edit uchar 65536 to ulong |
| 435 | t = new uchar 65536 0 |
| 436 | for i ulong 32 |
| 437 | put t i get h i |
| 438 | end |
| 439 | ret cdb32enc t 32 o |
| 440 | end |
| 441 | |
| 442 | note tka/aum.go UnmarshalText 33-41: DecodedLen(len) must be 32, which holds for len 52 only |
| 443 | note (52/8*5 + 52%8*5/8 = 32; 51 gives 31, 53 gives 33), counted before CR/LF are stripped. |
| 444 | note Then Decode into the 32 bytes. Stripping leaves at most 52 symbols, at most 32 bytes. |
| 445 | fn cdaumdec s view uchar 65536 n ulong h edit uchar 32 to ulong |
| 446 | cdzero32 h |
| 447 | if ne n 52 |
| 448 | ret 0 |
| 449 | end |
| 450 | o = new uchar 65536 0 |
| 451 | r = cdb32dec s n o |
| 452 | if eq r cdbad |
| 453 | ret 0 |
| 454 | end |
| 455 | for i ulong r |
| 456 | put h i get o i |
| 457 | end |
| 458 | ret 1 |
| 459 | end |
| 460 | |
| 461 | note types/key/node.go AppendText 324-326 and util.go appendHexKey 56-61 |
| 462 | fn cdnkenc k view uchar 32 o edit uchar 65536 to ulong |
| 463 | for i ulong 8 |
| 464 | put o i uchar and shr cdnkpfx sub 56 mul i 8 255 |
| 465 | end |
| 466 | for i ulong 32 |
| 467 | v = ulong get k i |
| 468 | put o add 8 mul i 2 cdhexc shr v 4 |
| 469 | put o add 9 mul i 2 cdhexc and v 15 |
| 470 | end |
| 471 | ret 72 |
| 472 | end |
| 473 | |
| 474 | note types/key/node.go UnmarshalText 336-338 and util.go parseHex 70-87: exact prefix "nodekey:", |
| 475 | note then exactly 64 hex digits of either case (fromHexChar 90-101) |
| 476 | fn cdnkdec s view uchar 65536 n ulong k edit uchar 32 to ulong |
| 477 | cdzero32 k |
| 478 | if ne n 72 |
| 479 | ret 0 |
| 480 | end |
| 481 | x = cdgetbe s 0 8 8 |
| 482 | if ne x cdnkpfx |
| 483 | ret 0 |
| 484 | end |
| 485 | for i ulong 32 |
| 486 | v = cdhexb s add 8 mul i 2 |
| 487 | if gt v 255 |
| 488 | cdzero32 k |
| 489 | ret 0 |
| 490 | end |
| 491 | put k i uchar v |
| 492 | end |
| 493 | ret 1 |
| 494 | end |