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 / gpu / physics0 / wire.om

385 lines · 7950 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note gpu/physics0/wire.om: PD-0 wire framing (PD0DESC2, PD0REC1 with SHA-256 chain) and STAND_IN range guard.
3note Converted from src/physics0/{pd0_wire.c,pd0_guard.c}.
4
5text PD0_MAGIC_DESC2 "PD0DESC2"
6text PD0_MAGIC_DESC1 "PD0DESC1"
7text PD0_MAGIC_REC1 "PD0REC1"
8
9fn pd0_put_u16 buf edit uchar * off ulong v ushort to void
10put buf off uchar and v ushort 255
11put buf add off 1 uchar and shr v 8 ushort 255
12ret
13end
14
15fn pd0_put_u32 buf edit uchar * off ulong v uint to void
16for i ulong 4
17sh = mul uint i uint 8
18put buf add off i uchar and shr v sh uint 255
19end
20ret
21end
22
23fn pd0_put_u64 buf edit uchar * off ulong v ulong to void
24for i ulong 8
25sh = mul i ulong 8
26put buf add off i uchar and shr v sh ulong 255
27end
28ret
29end
30
31fn pd0_get_u16 buf view uchar * off ulong to ushort
32b0 = ushort get buf off
33b1 = shl ushort get buf add off 1 8
34ret or b0 b1
35end
36
37fn pd0_get_u32 buf view uchar * off ulong to uint
38v = uint 0
39for i ulong 4
40sh = mul uint i uint 8
41b = shl uint get buf add off i sh
42v = or v b
43end
44ret v
45end
46
47fn pd0_get_u64 buf view uchar * off ulong to ulong
48v = ulong 0
49for i ulong 8
50sh = mul i ulong 8
51b = shl ulong get buf add off i sh
52v = or v b
53end
54ret v
55end
56
57fn pd0_eq_bytes a view uchar * aoff ulong b view uchar * n ulong to int
58for i ulong n bound 64
59if ne get a add aoff i get b i
60ret 0
61end
62end
63ret 1
64end
65
66fn pd0_rec_size n_obs uchar to ulong
67ret add ulong 120 mul ulong 16 ulong n_obs
68end
69
70fn pd0_desc_size n_obs uchar n_ch uchar to ulong
71ret add add ulong 30 mul ulong 16 ulong n_ch mul ulong 16 ulong n_obs
72end
73
74note ---- STAND_IN range guard (pd0_guard.c) ----
75
76fn pd0_guard_step_ok d view Pd0Desc channel uchar val long to int
77if eq channel PD0_CH_NONE
78ret 1
79end
80if ge channel get d n_channels
81ret 0
82end
83idx = ulong channel
84if lt val get d chan_min idx
85ret 0
86end
87if gt val get d chan_max idx
88ret 0
89end
90ret 1
91end
92
93fn pd0_guard_reset_ok d view Pd0Desc vals view long 4 n_vals uchar to int
94if ne n_vals get d n_obs
95ret 0
96end
97n = ulong n_vals
98for i ulong n bound 4
99v = get vals i
100if lt v get d reset_min i
101ret 0
102end
103if gt v get d reset_max i
104ret 0
105end
106end
107ret 1
108end
109
110fn pd0_guard_in_box v long to int
111if gt v PD0_BOUND
112ret 0
113end
114if lt v neg PD0_BOUND
115ret 0
116end
117ret 1
118end
119
120note ---- PD0DESC2 wire codec ----
121
122fn pd0_desc_encode d view Pd0Desc dst edit uchar * to ulong
123nobs = get d n_obs
124nch = get d n_channels
125if or lt nobs uchar 1 gt nobs uchar 4
126ret ulong 0
127end
128if or lt nch uchar 1 gt nch uchar 2
129ret ulong 0
130end
131need = pd0_desc_size nobs nch
132if lt len dst need
133ret ulong 0
134end
135for i ulong 8
136put dst i get PD0_MAGIC_DESC2 i
137end
138put dst 8 nobs
139put dst 9 nch
140pos = ulong 10
141pd0_put_u64 dst pos bitcast ulong get d dt_micro
142pos = add pos 8
143for i ulong ulong nch bound 2
144pd0_put_u64 dst pos bitcast ulong get d chan_min i
145pos = add pos 8
146end
147for i ulong ulong nch bound 2
148pd0_put_u64 dst pos bitcast ulong get d chan_max i
149pos = add pos 8
150end
151for i ulong ulong nobs bound 4
152pd0_put_u64 dst pos bitcast ulong get d reset_min i
153pos = add pos 8
154end
155for i ulong ulong nobs bound 4
156pd0_put_u64 dst pos bitcast ulong get d reset_max i
157pos = add pos 8
158end
159pd0_put_u32 dst pos get d episode_max_steps
160pos = add pos 4
161pd0_put_u32 dst pos get d budget_steps
162pos = add pos 4
163pd0_put_u32 dst pos get d budget_episodes
164pos = add pos 4
165ret pos
166end
167
168fn pd0_desc_decode src view uchar * d edit Pd0Desc to int
169slen = len src
170if ge slen ulong 8
171if eq pd0_eq_bytes src ulong 0 PD0_MAGIC_DESC1 ulong 8 1
172ret PD0_DESC_REFUSED_V1
173end
174end
175if lt slen ulong 10
176ret -1
177end
178if ne pd0_eq_bytes src ulong 0 PD0_MAGIC_DESC2 ulong 8 1
179ret -1
180end
181nobs = get src 8
182nch = get src 9
183if or lt nobs uchar 1 gt nobs uchar 4
184ret -1
185end
186if or lt nch uchar 1 gt nch uchar 2
187ret -1
188end
189if ne slen pd0_desc_size nobs nch
190ret -1
191end
192put d n_obs nobs
193put d n_channels nch
194pos = ulong 10
195put d dt_micro bitcast long pd0_get_u64 src pos
196pos = add pos 8
197for i ulong 2
198put d chan_min i long 0
199put d chan_max i long 0
200end
201for i ulong 4
202put d reset_min i long 0
203put d reset_max i long 0
204end
205for i ulong ulong nch bound 2
206put d chan_min i bitcast long pd0_get_u64 src pos
207pos = add pos 8
208end
209for i ulong ulong nch bound 2
210put d chan_max i bitcast long pd0_get_u64 src pos
211pos = add pos 8
212end
213for i ulong ulong nobs bound 4
214put d reset_min i bitcast long pd0_get_u64 src pos
215pos = add pos 8
216end
217for i ulong ulong nobs bound 4
218put d reset_max i bitcast long pd0_get_u64 src pos
219pos = add pos 8
220end
221put d episode_max_steps pd0_get_u32 src pos
222pos = add pos 4
223put d budget_steps pd0_get_u32 src pos
224pos = add pos 4
225put d budget_episodes pd0_get_u32 src pos
226ret 0
227end
228
229note ---- PD0REC1 sealed evidence record codec ----
230
231fn pd0_rec_body r view Pd0Rec dst edit uchar * to ulong
232for i ulong 7
233put dst i get PD0_MAGIC_REC1 i
234end
235put dst 7 uchar 0
236pd0_put_u16 dst ulong 8 ushort 1
237nobs = get r n_obs
238put dst 10 nobs
239put dst 11 get r status
240put dst 12 get r kind
241put dst 13 get r channel
242put dst 14 uchar 0
243put dst 15 uchar 0
244pd0_put_u64 dst ulong 16 get r seq
245pd0_put_u32 dst ulong 24 get r episode
246pd0_put_u32 dst ulong 28 get r step_in_episode
247pd0_put_u64 dst ulong 32 bitcast ulong get r time_micro
248pd0_put_u64 dst ulong 40 bitcast ulong get r requested
249pd0_put_u64 dst ulong 48 bitcast ulong get r applied
250pos = ulong 56
251for i ulong ulong nobs bound 4
252pd0_put_u64 dst pos bitcast ulong get r before i
253pos = add pos 8
254end
255for i ulong ulong nobs bound 4
256pd0_put_u64 dst pos bitcast ulong get r after i
257pos = add pos 8
258end
259for i ulong 8
260w = get r prev_hash i
261put dst pos uchar and shr w 24 uint 255
262put dst add pos 1 uchar and shr w 16 uint 255
263put dst add pos 2 uchar and shr w 8 uint 255
264put dst add pos 3 uchar and w uint 255
265pos = add pos 4
266end
267ret pos
268end
269
270fn pd0_get_be32 buf view uchar * off ulong to uint
271b0 = shl uint get buf off 24
272b1 = shl uint get buf add off 1 16
273b2 = shl uint get buf add off 2 8
274b3 = uint get buf add off 3
275ret or or b0 b1 or b2 b3
276end
277
278fn pd0_rec_encode r edit Pd0Rec dst edit uchar * to ulong
279nobs = get r n_obs
280if or lt nobs uchar 1 gt nobs uchar 4
281ret ulong 0
282end
283need = pd0_rec_size nobs
284if lt len dst need
285ret ulong 0
286end
287body_n = pd0_rec_body view r dst
288h = new uchar 32 0
289pd0_sha256_hash view dst body_n edit h
290for i ulong 8
291put r hash i pd0_get_be32 view h mul i 4
292end
293for i ulong 32
294put dst add body_n i get h i
295end
296ret add body_n ulong 32
297end
298
299fn pd0_rec_decode src view uchar * r edit Pd0Rec to int
300slen = len src
301if lt slen ulong 12
302ret -1
303end
304if ne pd0_eq_bytes src ulong 0 PD0_MAGIC_REC1 ulong 7 1
305ret -1
306end
307if ne get src 7 uchar 0
308ret -1
309end
310if ne pd0_get_u16 src ulong 8 ushort 1
311ret -1
312end
313nobs = get src 10
314if or lt nobs uchar 1 gt nobs uchar 4
315ret -1
316end
317if ne slen pd0_rec_size nobs
318ret -1
319end
320if or ne get src 14 uchar 0 ne get src 15 uchar 0
321ret -1
322end
323body_n = sub slen ulong 32
324h = new uchar 32 0
325pd0_sha256_hash src body_n edit h
326for i ulong 32
327if ne get h i get src add body_n i
328ret -1
329end
330end
331put r n_obs nobs
332put r status get src 11
333put r kind get src 12
334put r channel get src 13
335put r seq pd0_get_u64 src ulong 16
336put r episode pd0_get_u32 src ulong 24
337put r step_in_episode pd0_get_u32 src ulong 28
338put r time_micro bitcast long pd0_get_u64 src ulong 32
339put r requested bitcast long pd0_get_u64 src ulong 40
340put r applied bitcast long pd0_get_u64 src ulong 48
341pos = ulong 56
342for i ulong 4
343put r before i long 0
344put r after i long 0
345end
346for i ulong ulong nobs bound 4
347put r before i bitcast long pd0_get_u64 src pos
348pos = add pos 8
349end
350for i ulong ulong nobs bound 4
351put r after i bitcast long pd0_get_u64 src pos
352pos = add pos 8
353end
354for i ulong 8
355put r prev_hash i pd0_get_be32 src pos
356pos = add pos 4
357end
358for i ulong 8
359put r hash i pd0_get_be32 view h mul i 4
360end
361ret 0
362end
363
364fn pd0_rec_chained r view Pd0Rec has_prev int prev view Pd0Rec to int
365if eq has_prev 0
366if ne get r seq ulong 0
367ret 0
368end
369for i ulong 8
370if ne get r prev_hash i uint 0
371ret 0
372end
373end
374ret 1
375end
376if ne get r seq add get prev seq ulong 1
377ret 0
378end
379for i ulong 8
380if ne get r prev_hash i get prev hash i
381ret 0
382end
383end
384ret 1
385end