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 / src / physics0 / pd0_wire.om

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