interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / si / mcp / gates / pincheck.om

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