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 / si / mcp / gates / pincheck.om
| 1 | om2 |
| 2 | note pincheck: the pin is written in one place, PIN. build.sh compiles this unit with the pinned omc and runs |
| 3 | note it in the repository root as `git ls-files | build/pincheck`; a finding fails the build. |
| 4 | note It reads PIN (exactly `omega-mv <40 hex>` and `omc-sha256 <64 hex>`, one per line) and a list of tracked |
| 5 | note paths on stdin, one per line, and reports two kinds of word (a word is a run of 0-9 A-Z a-z _ between |
| 6 | note other characters). A word of 7 to 64 characters that, with A-F read as a-f, equals that many first |
| 7 | note characters of the PIN revision or of the omc sha256 restates the pin: this compares against the values |
| 8 | note in PIN and asks nothing else of the word, so a short revision made only of digits is found like any |
| 9 | note other, and a number that is not a prefix of the pin (a date, a count) is not. Any other hash-like word, |
| 10 | note a run of 7 or more of 0-9 a-f holding a digit and a letter, is a hash nothing derives from PIN. |
| 11 | note Not read: PIN; records (evidence/, .crumb, server.json, the v0.1.0 registry record); gates/fixtures/, |
| 12 | note data the gates compare against (the older compiler the pin case must refuse); data/, which |
| 13 | note tools/gen-reference.py writes from PIN and tools/check-reference.py checks against it; README.zh-CN.md |
| 14 | note (translated copies are retired and not maintained). The hex alphabet `0123456789abcdef` is not a hash. |
| 15 | note Exit 0 no findings, 1 findings, 2 PIN malformed or a listed file unreadable. |
| 16 | |
| 17 | global pin uchar 128 |
| 18 | global ib uchar 65536 |
| 19 | global buf uchar 65536 |
| 20 | global tok uchar 64 |
| 21 | global pth uchar 4096 |
| 22 | global ob uchar 32 |
| 23 | global st ulong 16 |
| 24 | |
| 25 | note st cells: word length, hex only, has digit, has letter, line, findings, path length, |
| 26 | note files read, unreadable or overlong paths, hex only after reading A-F as a-f |
| 27 | const q_len ulong 0 |
| 28 | const q_hex ulong 1 |
| 29 | const q_dig ulong 2 |
| 30 | const q_let ulong 3 |
| 31 | const q_line ulong 4 |
| 32 | const q_bad ulong 5 |
| 33 | const q_pl ulong 6 |
| 34 | const q_nf ulong 7 |
| 35 | const q_err ulong 8 |
| 36 | const q_hexf ulong 9 |
| 37 | |
| 38 | text kpinf "PIN" |
| 39 | text khex "0123456789abcdef" |
| 40 | text kmvw "omega-mv " |
| 41 | text kshaw "omc-sha256 " |
| 42 | text m_pin "pincheck: PIN must be exactly two lines, `omega-mv <40 hex>` and `omc-sha256 <64 hex>`\n" |
| 43 | text m_open "pincheck: cannot read " |
| 44 | text m_long "pincheck: path longer than 4095 bytes\n" |
| 45 | text m_rest ": restates PIN: " |
| 46 | text m_hash ": names a hash that nothing derives from PIN: " |
| 47 | text m_hint "pincheck: the pin is written only in PIN; point at PIN (or derive the value from it) instead\n" |
| 48 | text m_sum "pincheck: files " |
| 49 | text m_fnd " findings " |
| 50 | text nl "\n" |
| 51 | text colon ":" |
| 52 | text x_pin "PIN" |
| 53 | text x_ev "evidence/" |
| 54 | text x_data "data/" |
| 55 | text x_crumb ".crumb" |
| 56 | text x_zh "README.zh-CN.md" |
| 57 | text x_fix "gates/fixtures/" |
| 58 | text x_srv "server.json" |
| 59 | |
| 60 | note wt: all of t on stdout; a failed write exits 2 |
| 61 | fn wt t view uchar * to void |
| 62 | n = len t |
| 63 | p = ulong 0 |
| 64 | while lt p n |
| 65 | k = write 1 t p sub n p |
| 66 | if le k 0 |
| 67 | exit 2 |
| 68 | end |
| 69 | p = add p ulong k |
| 70 | end |
| 71 | end |
| 72 | |
| 73 | note wpth: the current path on stdout |
| 74 | fn wpth to void |
| 75 | n = get st q_pl |
| 76 | p = ulong 0 |
| 77 | while lt p n |
| 78 | k = write 1 pth p sub n p |
| 79 | if le k 0 |
| 80 | exit 2 |
| 81 | end |
| 82 | p = add p ulong k |
| 83 | end |
| 84 | end |
| 85 | |
| 86 | note wtok: the current word on stdout (its first 64 bytes) |
| 87 | fn wtok to void |
| 88 | n = get st q_len |
| 89 | if gt n 64 |
| 90 | n = ulong 64 |
| 91 | end |
| 92 | p = ulong 0 |
| 93 | while lt p n |
| 94 | k = write 1 tok p sub n p |
| 95 | if le k 0 |
| 96 | exit 2 |
| 97 | end |
| 98 | p = add p ulong k |
| 99 | end |
| 100 | end |
| 101 | |
| 102 | note putn: v in decimal on stdout; a ulong has at most 20 digits |
| 103 | fn putn v ulong to void |
| 104 | i = ulong 32 |
| 105 | n = v |
| 106 | while true bound 20 |
| 107 | i = sub i 1 |
| 108 | put ob i uchar add rem n 10 48 |
| 109 | n = div n 10 |
| 110 | if eq n 0 |
| 111 | break |
| 112 | end |
| 113 | end |
| 114 | p = i |
| 115 | while lt p 32 |
| 116 | k = write 1 ob p sub 32 p |
| 117 | if le k 0 |
| 118 | exit 2 |
| 119 | end |
| 120 | p = add p ulong k |
| 121 | end |
| 122 | end |
| 123 | |
| 124 | fn ishex c uchar to bool |
| 125 | if and ge c 48 le c 57 |
| 126 | ret true |
| 127 | end |
| 128 | ret and ge c 97 le c 102 |
| 129 | end |
| 130 | |
| 131 | fn isw c uchar to bool |
| 132 | if and ge c 48 le c 57 |
| 133 | ret true |
| 134 | end |
| 135 | if and ge c 65 le c 90 |
| 136 | ret true |
| 137 | end |
| 138 | if and ge c 97 le c 122 |
| 139 | ret true |
| 140 | end |
| 141 | ret eq c 95 |
| 142 | end |
| 143 | |
| 144 | note ldpin: PIN into pin; true when it has exactly the two lines |
| 145 | fn ldpin to bool |
| 146 | f = fopen kpinf 0 len kpinf 0 0 |
| 147 | if lt f 0 |
| 148 | ret false |
| 149 | end |
| 150 | n = read int f pin 0 128 |
| 151 | z = fclose int f |
| 152 | if ne n 126 |
| 153 | ret false |
| 154 | end |
| 155 | for i ulong 9 |
| 156 | if ne get pin i get kmvw i |
| 157 | ret false |
| 158 | end |
| 159 | end |
| 160 | for i ulong 11 |
| 161 | if ne get pin add 50 i get kshaw i |
| 162 | ret false |
| 163 | end |
| 164 | end |
| 165 | if or ne get pin 49 10 ne get pin 125 10 |
| 166 | ret false |
| 167 | end |
| 168 | for i ulong 40 |
| 169 | if not ishex get pin add 9 i |
| 170 | ret false |
| 171 | end |
| 172 | end |
| 173 | for i ulong 64 |
| 174 | if not ishex get pin add 61 i |
| 175 | ret false |
| 176 | end |
| 177 | end |
| 178 | ret true |
| 179 | end |
| 180 | |
| 181 | note pre: the current path starts with t; eqp: it is t |
| 182 | fn pre t view uchar * to bool |
| 183 | n = len t |
| 184 | if lt get st q_pl n |
| 185 | ret false |
| 186 | end |
| 187 | for i ulong n |
| 188 | if ne get pth i get t i |
| 189 | ret false |
| 190 | end |
| 191 | end |
| 192 | ret true |
| 193 | end |
| 194 | |
| 195 | fn eqp t view uchar * to bool |
| 196 | if ne get st q_pl len t |
| 197 | ret false |
| 198 | end |
| 199 | ret pre t |
| 200 | end |
| 201 | |
| 202 | note tokat: the current word (n <= 64 bytes) equals pin[o .. o+n) |
| 203 | fn tokat o ulong n ulong to bool |
| 204 | for i ulong n |
| 205 | if ne get tok i get pin add o i |
| 206 | ret false |
| 207 | end |
| 208 | end |
| 209 | ret true |
| 210 | end |
| 211 | |
| 212 | fn ishexab to bool |
| 213 | if ne get st q_len 16 |
| 214 | ret false |
| 215 | end |
| 216 | for i ulong 16 |
| 217 | if ne get tok i get khex i |
| 218 | ret false |
| 219 | end |
| 220 | end |
| 221 | ret true |
| 222 | end |
| 223 | |
| 224 | fn rset to void |
| 225 | put st q_len 0 |
| 226 | put st q_hex 1 |
| 227 | put st q_hexf 1 |
| 228 | put st q_dig 0 |
| 229 | put st q_let 0 |
| 230 | end |
| 231 | |
| 232 | note judge: report the word that just ended, if it restates the pin or is hash-like |
| 233 | fn judge to void |
| 234 | n = get st q_len |
| 235 | if lt n 7 |
| 236 | ret |
| 237 | end |
| 238 | if eq get st q_hexf 0 |
| 239 | ret |
| 240 | end |
| 241 | r = false |
| 242 | if le n 40 |
| 243 | r = tokat 9 n |
| 244 | end |
| 245 | if and not r le n 64 |
| 246 | r = tokat 61 n |
| 247 | end |
| 248 | if not r |
| 249 | if or eq get st q_hex 0 or eq get st q_dig 0 eq get st q_let 0 |
| 250 | ret |
| 251 | end |
| 252 | if ishexab |
| 253 | ret |
| 254 | end |
| 255 | end |
| 256 | wpth |
| 257 | wt colon |
| 258 | putn get st q_line |
| 259 | if r |
| 260 | wt m_rest |
| 261 | else |
| 262 | wt m_hash |
| 263 | end |
| 264 | wtok |
| 265 | wt nl |
| 266 | put st q_bad add get st q_bad 1 |
| 267 | end |
| 268 | |
| 269 | fn eat c uchar to void |
| 270 | if isw c |
| 271 | n = get st q_len |
| 272 | d = c |
| 273 | if and ge c 65 le c 70 |
| 274 | d = add c 32 |
| 275 | end |
| 276 | if lt n 64 |
| 277 | put tok n d |
| 278 | end |
| 279 | put st q_len add n 1 |
| 280 | if not ishex c |
| 281 | put st q_hex 0 |
| 282 | end |
| 283 | if not ishex d |
| 284 | put st q_hexf 0 |
| 285 | end |
| 286 | if and ge c 48 le c 57 |
| 287 | put st q_dig 1 |
| 288 | end |
| 289 | if and ge c 97 le c 102 |
| 290 | put st q_let 1 |
| 291 | end |
| 292 | ret |
| 293 | end |
| 294 | judge |
| 295 | rset |
| 296 | if eq c 10 |
| 297 | put st q_line add get st q_line 1 |
| 298 | end |
| 299 | end |
| 300 | |
| 301 | note scan: every word of the open file f; false when a read fails |
| 302 | fn scan f int to bool |
| 303 | rset |
| 304 | put st q_line 1 |
| 305 | while true |
| 306 | k = read f buf 0 65536 |
| 307 | if lt k 0 |
| 308 | ret false |
| 309 | end |
| 310 | if eq k 0 |
| 311 | break |
| 312 | end |
| 313 | m = ulong k |
| 314 | for i ulong m |
| 315 | eat get buf i |
| 316 | end |
| 317 | end |
| 318 | judge |
| 319 | ret true |
| 320 | end |
| 321 | |
| 322 | fn skip to bool |
| 323 | if eqp x_pin |
| 324 | ret true |
| 325 | end |
| 326 | if pre x_ev |
| 327 | ret true |
| 328 | end |
| 329 | if pre x_data |
| 330 | ret true |
| 331 | end |
| 332 | if eqp x_crumb |
| 333 | ret true |
| 334 | end |
| 335 | if pre x_fix |
| 336 | ret true |
| 337 | end |
| 338 | if eqp x_srv |
| 339 | ret true |
| 340 | end |
| 341 | ret eqp x_zh |
| 342 | end |
| 343 | |
| 344 | note dopath: check the path in pth, then forget it |
| 345 | fn dopath to void |
| 346 | n = get st q_pl |
| 347 | if eq n 0 |
| 348 | ret |
| 349 | end |
| 350 | if gt n 4095 |
| 351 | wt m_long |
| 352 | put st q_err 1 |
| 353 | put st q_pl 0 |
| 354 | ret |
| 355 | end |
| 356 | if skip |
| 357 | put st q_pl 0 |
| 358 | ret |
| 359 | end |
| 360 | f = fopen pth 0 n 0 0 |
| 361 | ok = false |
| 362 | if ge f 0 |
| 363 | ok = scan int f |
| 364 | z = fclose int f |
| 365 | end |
| 366 | if not ok |
| 367 | wt m_open |
| 368 | wpth |
| 369 | wt nl |
| 370 | put st q_err 1 |
| 371 | end |
| 372 | put st q_nf add get st q_nf 1 |
| 373 | put st q_pl 0 |
| 374 | end |
| 375 | |
| 376 | fn main to int |
| 377 | if not ldpin |
| 378 | wt m_pin |
| 379 | ret 2 |
| 380 | end |
| 381 | put st q_bad 0 |
| 382 | put st q_nf 0 |
| 383 | put st q_pl 0 |
| 384 | put st q_err 0 |
| 385 | while true |
| 386 | k = read 0 ib 0 65536 |
| 387 | if lt k 0 |
| 388 | exit 2 |
| 389 | end |
| 390 | if eq k 0 |
| 391 | break |
| 392 | end |
| 393 | m = ulong k |
| 394 | for i ulong m |
| 395 | c = get ib i |
| 396 | if eq c 10 |
| 397 | dopath |
| 398 | else |
| 399 | n = get st q_pl |
| 400 | if lt n 4096 |
| 401 | put pth n c |
| 402 | end |
| 403 | put st q_pl add n 1 |
| 404 | end |
| 405 | end |
| 406 | end |
| 407 | dopath |
| 408 | wt m_sum |
| 409 | putn get st q_nf |
| 410 | wt m_fnd |
| 411 | putn get st q_bad |
| 412 | wt nl |
| 413 | if ne get st q_err 0 |
| 414 | ret 2 |
| 415 | end |
| 416 | if ne get st q_bad 0 |
| 417 | wt m_hint |
| 418 | ret 1 |
| 419 | end |
| 420 | ret 0 |
| 421 | end |