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 / test.om

363 lines · 8756 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note gpu/physics0/test.om: comprehensive positive, negative & mutant-control test suite for gpu/physics0.
3note Compose: si/directives/om2.om gpu/physics0/types.om gpu/physics0/rng.om gpu/physics0/wire.om gpu/physics0/world.om gpu/physics0/test.om
4
5text p0_ok1 "PASS physics0 FIPS 180-4 SHA-256 vectors (empty + abc)\n"
6text p0_ok2 "PASS physics0 SplitMix64 + FNV-1a64 + exact micro-unit arithmetic\n"
7text p0_ok3 "PASS physics0 PD0DESC2 + PD0REC1 wire framing, SHA-256 chain & STAND_IN guards\n"
8text p0_ok4 "PASS physics0 world invariants (timing, episode ceiling, budget ceiling, OOB non-leak, STAND_IN charge)\n"
9text p0_ok5 "PASS physics0 mutant controls (M1 L1 spring sign flip, M2 tampered record hash)\n"
10text p0_all "ALL PASS gpu-physics0\n"
11text p0_fail "FAIL gpu-physics0\n"
12
13text p0_abc "abc"
14text p0_empty_src "x"
15
16fn p0_puts s view uchar * to void
17k = write 1 s 0 len s
18if lt k 0
19exit 2
20end
21ret
22end
23
24fn test_sha256_vectors to int
25h = new uchar 32 0
26note 1. SHA-256("") = e3b0c44298fc1c14...7852b855
27pd0_sha256_hash part p0_empty_src 0 0 ulong 0 edit h
28if or ne get h 0 uchar 0xe3 ne get h 1 uchar 0xb0
29ret 1
30end
31if or ne get h 2 uchar 0xc4 ne get h 3 uchar 0x42
32ret 2
33end
34if or ne get h 30 uchar 0xb8 ne get h 31 uchar 0x55
35ret 3
36end
37note 2. SHA-256("abc") = ba7816bf8f01cfea...f20015ad
38pd0_sha256_hash p0_abc ulong 3 edit h
39if or ne get h 0 uchar 0xba ne get h 1 uchar 0x78
40ret 4
41end
42if or ne get h 2 uchar 0x16 ne get h 3 uchar 0xbf
43ret 5
44end
45if or ne get h 30 uchar 0x15 ne get h 31 uchar 0xad
46ret 6
47end
48ret 0
49end
50
51fn test_rng_and_math to int
52r1 = new ulong 1 0
53r2 = new ulong 1 0
54pd0_stream edit r1 ulong 42 PD0_TAG_CONST
55pd0_stream edit r2 ulong 42 PD0_TAG_CONST
56v1 = pd0_splitmix64 edit r1
57v2 = pd0_splitmix64 edit r2
58if ne v1 v2
59ret 10
60end
61note Different tags produce distinct streams
62pd0_stream edit r2 ulong 42 PD0_TAG_NOISE
63v3 = pd0_splitmix64 edit r2
64if eq v1 v3
65ret 11
66end
67note Uniform draw in [0, 1000000]
68u = pd0_uniform edit r1
69if or lt u long 0 gt u long 1000000
70ret 12
71end
72note Exact micro-unit multiply (positive, negative, large)
73if ne pd0_mul long 1500000 long 2000000 long 3000000
74ret 13
75end
76if ne pd0_mul long 10000000 long -10000000 long -100000000
77ret 14
78end
79if ne pd0_mul long -500000 long -500000 long 250000
80ret 15
81end
82ret 0
83end
84
85fn test_wire_and_guards to int
86d = new Pd0Desc 0 0 0 0 0 0 0 0 0 0
87d2 = new Pd0Desc 0 0 0 0 0 0 0 0 0 0
88if ne pd0_gen_desc PD0_L1 edit d 0
89ret 20
90end
91buf = new uchar 256 0
92n = pd0_desc_encode view d edit buf
93if ne n ulong 78
94ret 21
95end
96if ne pd0_desc_decode part buf 0 n edit d2 0
97ret 22
98end
99if or ne get d2 n_obs uchar 2 ne get d2 dt_micro long 50000
100ret 23
101end
102if or ne get d2 budget_steps uint 3000 ne get d2 episode_max_steps uint 100
103ret 24
104end
105note Negative control: PD0DESC1 explicitly refused with PD0_DESC_REFUSED_V1 (-2)
106put buf 7 uchar 49
107if ne pd0_desc_decode part buf 0 n edit d2 PD0_DESC_REFUSED_V1
108ret 25
109end
110note Negative control: bad magic refused (-1)
111put buf 0 uchar 88
112if ne pd0_desc_decode part buf 0 n edit d2 -1
113ret 26
114end
115note Negative control: truncated buffer refused (-1)
116if ne pd0_desc_decode part buf 0 5 edit d2 -1
117ret 27
118end
119note STAND_IN range guard positive & negative controls
120if ne pd0_guard_step_ok view d PD0_CH_NONE long 999999999 1
121ret 28
122end
123if ne pd0_guard_step_ok view d uchar 0 long 2000000 1
124ret 29
125end
126if ne pd0_guard_step_ok view d uchar 0 long 2000001 0
127ret 30
128end
129if ne pd0_guard_step_ok view d uchar 1 long 0 0
130ret 31
131end
132rv = new long 4 0
133put rv 0 long 1000000
134put rv 1 long -2000000
135if ne pd0_guard_reset_ok view d view rv uchar 2 1
136ret 32
137end
138put rv 1 long -2000001
139if ne pd0_guard_reset_ok view d view rv uchar 2 0
140ret 33
141end
142if ne pd0_guard_in_box long 10000000 1
143ret 34
144end
145if ne pd0_guard_in_box long 10000001 0
146ret 35
147end
148if ne pd0_guard_in_box long -10000001 0
149ret 36
150end
151ret 0
152end
153
154fn test_world_oob_budget to int
155w = new Pd0World 0 0 0 0 0 0 0 0 0 0 0 0 0
156d = new Pd0Desc 0 0 0 0 0 0 0 0 0 0
157g = new Pd0Gen 0 0 0 0 0
158r0 = new Pd0Rec 0 0 0 0 0 0 0 0 0 0 0 0 0 0
159cur_r = new Pd0Rec 0 0 0 0 0 0 0 0 0 0 0 0 0 0
160rv = new long 4 0
161if ne pd0_world_init edit w edit d edit g PD0_L0 ulong 12345 0
162ret 52
163end
164note 6. Out-of-bounds invariant: push state past PD0_BOUND -> PD0_OUT_OF_BOUNDS, after == before (no leak)
165put rv 0 long 2000000
166put rv 1 long 200000
167pd0_world_reset edit w view d view g view rv uchar 2 edit r0
168if ne get r0 status PD0_OK
169ret 52
170end
171oob_hit = int 0
172for st ulong 30
173pd0_world_step edit w view d view g uchar 0 long 100000 edit cur_r
174if eq get cur_r status PD0_OUT_OF_BOUNDS
175if ne get cur_r after 0 get cur_r before 0
176ret 53
177end
178if ne get cur_r after 1 get cur_r before 1
179ret 53
180end
181if ne get w in_episode 0
182ret 54
183end
184oob_hit = 1
185break
186end
187end
188if ne oob_hit 1
189ret 55
190end
191note 7. Budget ceiling enforcement
192put d budget_steps get w steps_used
193put rv 0 long 0
194put rv 1 long 0
195pd0_world_reset edit w view d view g view rv uchar 2 edit cur_r
196if ne get cur_r status PD0_BUDGET_EXHAUSTED
197ret 56
198end
199ret 0
200end
201
202fn test_world_invariants to int
203w = new Pd0World 0 0 0 0 0 0 0 0 0 0 0 0 0
204d = new Pd0Desc 0 0 0 0 0 0 0 0 0 0
205g = new Pd0Gen 0 0 0 0 0
206r0 = new Pd0Rec 0 0 0 0 0 0 0 0 0 0 0 0 0 0
207r1 = new Pd0Rec 0 0 0 0 0 0 0 0 0 0 0 0 0 0
208r2 = new Pd0Rec 0 0 0 0 0 0 0 0 0 0 0 0 0 0
209rv = new long 4 0
210if ne pd0_world_init edit w edit d edit g PD0_L0 ulong 12345 0
211ret 40
212end
213note 1. Out-of-range reset refused (PD0_REFUSED_RANGE) AND charged to budget + sealed at seq 0
214put rv 0 long 5000000
215put rv 1 long 0
216pd0_world_reset edit w view d view g view rv uchar 2 edit r0
217if or ne get r0 status PD0_REFUSED_RANGE ne get w episodes_used uint 1
218ret 41
219end
220if ne pd0_rec_chained view r0 0 view r1 1
221ret 42
222end
223note 2. Valid reset succeeds at seq 1, chained to r0
224put rv 0 long 0
225put rv 1 long 100000
226pd0_world_reset edit w view d view g view rv uchar 2 edit r1
227if or ne get r1 status PD0_OK ne get r1 seq ulong 1
228ret 43
229end
230if ne pd0_rec_chained view r1 1 view r0 1
231ret 44
232end
233note 3. Out-of-range step refused (PD0_REFUSED_RANGE), still charged, chained to r1
234pd0_world_step edit w view d view g uchar 0 long 500000 edit r2
235if or ne get r2 status PD0_REFUSED_RANGE ne get w steps_used uint 1
236ret 45
237end
238if ne pd0_rec_chained view r2 1 view r1 1
239ret 46
240end
241note 4. Run 20 valid steps on L0 -> step 20 hits episode_max_steps -> PD0_EPISODE_END
242for st ulong 20
243pd0_world_step edit w view d view g PD0_CH_NONE long 0 edit r0
244if ne pd0_rec_chained view r0 1 view r2 1
245ret 47
246end
247if lt st ulong 19
248if ne get r0 status PD0_OK
249ret 49
250end
251else
252if ne get r0 status PD0_EPISODE_END
253ret 50
254end
255end
256put r2 seq get r0 seq
257for j ulong 8
258put r2 hash j get r0 hash j
259end
260end
261note 5. Step after episode end returns PD0_BUDGET_EXHAUSTED
262pd0_world_step edit w view d view g PD0_CH_NONE long 0 edit r0
263if ne get r0 status PD0_BUDGET_EXHAUSTED
264ret 51
265end
266ret test_world_oob_budget
267end
268
269fn test_mutant_controls to int
270w = new Pd0World 0 0 0 0 0 0 0 0 0 0 0 0 0
271d = new Pd0Desc 0 0 0 0 0 0 0 0 0 0
272g = new Pd0Gen 0 0 0 0 0
273if ne pd0_world_init edit w edit d edit g PD0_L1 ulong 77 0
274ret 60
275end
276put w s 0 long 1000000
277put w s 1 long 0
278u = new long 2 0
279ns_true = new long 4 0
280ns_mut = new long 4 0
281note Mutant M1: L1 spring sign flip (+k*s0 vs -k*s0). True L1 pulls velocity negative; mutant pushes positive!
282b1 = pd0_gen_step_dyn view g edit w view u get d dt_micro edit ns_true 0
283b2 = pd0_gen_step_dyn view g edit w view u get d dt_micro edit ns_mut 1
284if or ne b1 0 ne b2 0
285ret 61
286end
287if ge get ns_true 1 long 0
288ret 62
289end
290if le get ns_mut 1 long 0
291ret 63
292end
293if eq get ns_true 1 get ns_mut 1
294ret 64
295end
296note Mutant M2: tampered PD0REC1 record byte refused by SHA-256 verification
297rec = new Pd0Rec 0 0 0 0 0 0 0 0 0 0 0 0 0 0
298rec_out = new Pd0Rec 0 0 0 0 0 0 0 0 0 0 0 0 0 0
299put rec n_obs uchar 2
300put rec seq ulong 0
301put rec before 0 long 1000000
302put rec after 0 long 950000
303buf = new uchar 256 0
304n = pd0_rec_encode edit rec edit buf
305if ne n ulong 152
306ret 65
307end
308if ne pd0_rec_decode part buf 0 n edit rec_out 0
309ret 66
310end
311note Flip one bit in payload (seq byte) -> must fail decode
312put buf 16 xor get buf 16 uchar 1
313if ne pd0_rec_decode part buf 0 n edit rec_out -1
314ret 67
315end
316note Restore payload, flip one bit in pad byte 14 -> must fail decode
317put buf 16 xor get buf 16 uchar 1
318put buf 14 uchar 1
319if ne pd0_rec_decode part buf 0 n edit rec_out -1
320ret 68
321end
322ret 0
323end
324
325fn main to int
326rc = test_sha256_vectors
327if ne rc 0
328p0_puts p0_fail
329ret rc
330end
331p0_puts p0_ok1
332
333rc = test_rng_and_math
334if ne rc 0
335p0_puts p0_fail
336ret rc
337end
338p0_puts p0_ok2
339
340rc = test_wire_and_guards
341if ne rc 0
342p0_puts p0_fail
343ret rc
344end
345p0_puts p0_ok3
346
347rc = test_world_invariants
348if ne rc 0
349p0_puts p0_fail
350ret rc
351end
352p0_puts p0_ok4
353
354rc = test_mutant_controls
355if ne rc 0
356p0_puts p0_fail
357ret rc
358end
359p0_puts p0_ok5
360
361p0_puts p0_all
362ret 0
363end