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_types.om

416 lines · 9330 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note src/physics0/pd0_types.om: PD-0 core constants, structs, FIPS 180-4 SHA-256, and math/format helpers.
3note Learner-side safe: no generator, no world, no file I/O.
4
5const PD0_MAX_OBS ulong 4
6const PD0_MAX_CH ulong 2
7const PD0_MAX_LATENT ulong 2
8const PD0_MAX_VARS ulong 6
9const PD0_MAX_EX ulong 8
10const PD0_MAX_TERMS ulong 12
11const PD0_MAX_DEG ulong 3
12const PD0_SCHED_STEPS ulong 20
13const PD0_HASH ulong 32
14const PD0_CH_NONE uchar 255
15const PD0_MICRO long 1000000
16const PD0_IH_SD long 577350
17const PD0_BOUND long 10000000
18const PD0_EPISODE_STEPS uint 100
19const PD0_L6_REDRAW_CAP int 1000
20
21const PD0_OK uchar 0
22const PD0_REFUSED_RANGE uchar 1
23const PD0_OUT_OF_BOUNDS uchar 2
24const PD0_EPISODE_END uchar 3
25const PD0_BUDGET_EXHAUSTED uchar 4
26
27const PD0_KIND_RESET uchar 0
28const PD0_KIND_STEP uchar 1
29
30const PD0_OP_DESCRIBE uchar 0
31const PD0_OP_RESET uchar 1
32const PD0_OP_STEP uchar 2
33const PD0_OP_SCORE uchar 3
34const PD0_OP_SHAPE uchar 4
35const PD0_OP_FINAL uchar 5
36const PD0_OP_AUDIT uchar 6
37
38const PD0_AUDIT_RESP_SIZE ulong 57
39const PD0_SCORE_STEPS_MAX ulong 100
40const PD0_DESC_REFUSED_V1 int -2
41
42const PD0_L0 int 0
43const PD0_L1 int 1
44const PD0_L2 int 2
45const PD0_L3 int 3
46const PD0_L4 int 4
47const PD0_L5 int 5
48const PD0_L6 int 6
49const PD0_LEVEL_NULL int 7
50const PD0_LEVELS int 8
51
52const PD0_LAW_REJECTED uchar 0
53const PD0_LAW_REFUTED uchar 1
54const PD0_LAW_HYPOTHESIS uchar 2
55const PD0_LAW_PROVISIONAL uchar 3
56
57struct Pd0Desc n_obs uchar n_channels uchar dt_micro long chan_min long 2 chan_max long 2 reset_min long 4 reset_max long 4 episode_max_steps uint budget_steps uint budget_episodes uint end
58
59struct Pd0Rec n_obs uchar status uchar kind uchar channel uchar seq ulong episode uint step_in_episode uint time_micro long requested long applied long before long 4 after long 4 prev_hash uint 8 hash uint 8 end
60
61struct Pd0Gen level int k long 4 sigma_obs long sigma_null long n_hidden int redraws int invalid int end
62
63struct Pd0World level int seed ulong noise_s ulong null_s ulong s long 4 h long in_episode int step_in_episode uint episodes_used uint steps_used uint episode uint seq ulong last_hash uint 8 end
64
65struct Pd0LawMeta state uchar n_refutations ushort eps_micro uint confidence_ppm uint n_obs_dom ulong n_ep_dom uint reset_min long reset_max long ep_len uint dt_micro long latent_reset long n_exc ushort n_exp ushort end
66
67note ---- Math helpers (f64) ----
68
69fn pd0_llround x f64 to long
70if ge x 0.0f64
71ret long add x 0.5f64
72end
73ret long sub x 0.5f64
74end
75
76fn pd0_log2 x f64 to f64
77if le x 0.0f64
78ret -1000.0f64
79end
80bits = bitcast ulong x
81exp_raw = long and shr bits 52 0x7ff
82e = sub exp_raw long 1023
83mant_bits = or and bits 0x000fffffffffffff 0x3ff0000000000000
84m = bitcast f64 mant_bits
85if ge m 1.4142135623730951f64
86m = mul m 0.5f64
87e = add e long 1
88end
89t = div sub m 1.0f64 add m 1.0f64
90t2 = mul t t
91term = t
92s = term
93term = mul term t2
94s = add s div term 3.0f64
95term = mul term t2
96s = add s div term 5.0f64
97term = mul term t2
98s = add s div term 7.0f64
99term = mul term t2
100s = add s div term 9.0f64
101term = mul term t2
102s = add s div term 11.0f64
103term = mul term t2
104s = add s div term 13.0f64
105ln_m = mul 2.0f64 s
106ret add f64 e mul ln_m 1.4426950408889634f64
107end
108
109note ---- Buffer append helpers ----
110
111fn pd0_buf_ch dst edit uchar * pos ulong cap ulong ch uchar to ulong
112if lt pos cap
113put dst pos ch
114ret add pos 1
115end
116ret pos
117end
118
119fn pd0_buf_str dst edit uchar * pos ulong cap ulong s view uchar * to ulong
120n = len s
121for i ulong n bound 4096
122if lt pos cap
123put dst pos get s i
124pos = add pos 1
125end
126end
127ret pos
128end
129
130fn pd0_buf_u64 dst edit uchar * pos ulong cap ulong v ulong to ulong
131if eq v ulong 0
132ret pd0_buf_ch dst pos cap uchar 48
133end
134tmp = new uchar 24 0
135k = ulong 0
136cur = v
137while gt cur ulong 0 bound 24
138d = rem cur ulong 10
139put tmp k uchar add ulong 48 d
140k = add k 1
141cur = div cur ulong 10
142end
143for i ulong k bound 24
144idx = sub sub k 1 i
145if lt pos cap
146put dst pos get tmp idx
147pos = add pos 1
148end
149end
150ret pos
151end
152
153fn pd0_buf_i64 dst edit uchar * pos ulong cap ulong v long to ulong
154if lt v long 0
155pos = pd0_buf_ch dst pos cap uchar 45
156if eq v long -9223372036854775807
157ret pd0_buf_u64 dst pos cap ulong 9223372036854775807
158end
159ret pd0_buf_u64 dst pos cap ulong neg v
160end
161ret pd0_buf_u64 dst pos cap ulong v
162end
163
164fn pd0_buf_u64_pad6 dst edit uchar * pos ulong cap ulong v ulong to ulong
165tmp = new uchar 6 48
166cur = v
167for i ulong 6
168idx = sub ulong 5 i
169d = rem cur ulong 10
170put tmp idx uchar add ulong 48 d
171cur = div cur ulong 10
172end
173for i ulong 6
174if lt pos cap
175put dst pos get tmp i
176pos = add pos 1
177end
178end
179ret pos
180end
181
182fn pd0_buf_f64_6 dst edit uchar * pos ulong cap ulong x f64 to ulong
183if lt x 0.0f64
184pos = pd0_buf_ch dst pos cap uchar 45
185x = neg x
186end
187sc = ulong pd0_llround mul x 1000000.0f64
188ip = div sc ulong 1000000
189fp = rem sc ulong 1000000
190pos = pd0_buf_u64 dst pos cap ip
191pos = pd0_buf_ch dst pos cap uchar 46
192ret pd0_buf_u64_pad6 dst pos cap fp
193end
194
195fn pd0_puts fd int s view uchar * to void
196k = write fd s 0 len s
197if lt k 0
198ret
199end
200ret
201end
202
203fn pd0_put_bytes fd int s view uchar * n ulong to void
204if eq n ulong 0
205ret
206end
207k = write fd s 0 n
208if lt k 0
209ret
210end
211ret
212end
213
214note ---- FIPS 180-4 SHA-256 ----
215
216fn pd0_sha_kinit k edit uint 64 to void
217put k 0 0x428a2f98
218put k 1 0x71374491
219put k 2 0xb5c0fbcf
220put k 3 0xe9b5dba5
221put k 4 0x3956c25b
222put k 5 0x59f111f1
223put k 6 0x923f82a4
224put k 7 0xab1c5ed5
225put k 8 0xd807aa98
226put k 9 0x12835b01
227put k 10 0x243185be
228put k 11 0x550c7dc3
229put k 12 0x72be5d74
230put k 13 0x80deb1fe
231put k 14 0x9bdc06a7
232put k 15 0xc19bf174
233put k 16 0xe49b69c1
234put k 17 0xefbe4786
235put k 18 0x0fc19dc6
236put k 19 0x240ca1cc
237put k 20 0x2de92c6f
238put k 21 0x4a7484aa
239put k 22 0x5cb0a9dc
240put k 23 0x76f988da
241put k 24 0x983e5152
242put k 25 0xa831c66d
243put k 26 0xb00327c8
244put k 27 0xbf597fc7
245put k 28 0xc6e00bf3
246put k 29 0xd5a79147
247put k 30 0x06ca6351
248put k 31 0x14292967
249put k 32 0x27b70a85
250put k 33 0x2e1b2138
251put k 34 0x4d2c6dfc
252put k 35 0x53380d13
253put k 36 0x650a7354
254put k 37 0x766a0abb
255put k 38 0x81c2c92e
256put k 39 0x92722c85
257put k 40 0xa2bfe8a1
258put k 41 0xa81a664b
259put k 42 0xc24b8b70
260put k 43 0xc76c51a3
261put k 44 0xd192e819
262put k 45 0xd6990624
263put k 46 0xf40e3585
264put k 47 0x106aa070
265put k 48 0x19a4c116
266put k 49 0x1e376c08
267put k 50 0x2748774c
268put k 51 0x34b0bcb5
269put k 52 0x391c0cb3
270put k 53 0x4ed8aa4a
271put k 54 0x5b9cca4f
272put k 55 0x682e6ff3
273put k 56 0x748f82ee
274put k 57 0x78a5636f
275put k 58 0x84c87814
276put k 59 0x8cc70208
277put k 60 0x90befffa
278put k 61 0xa4506ceb
279put k 62 0xbef9a3f7
280put k 63 0xc67178f2
281ret
282end
283
284fn pd0_sha_ch x uint y uint z uint to uint
285a = and x y
286b = and not x z
287ret xor a b
288end
289
290fn pd0_sha_maj x uint y uint z uint to uint
291a = and x y
292b = and x z
293c = and y z
294ret xor xor a b c
295end
296
297fn pd0_sha_bs0 x uint to uint
298ret xor xor rotr x 2 rotr x 13 rotr x 22
299end
300
301fn pd0_sha_bs1 x uint to uint
302ret xor xor rotr x 6 rotr x 11 rotr x 25
303end
304
305fn pd0_sha_ss0 x uint to uint
306ret xor xor rotr x 7 rotr x 18 shr x 3
307end
308
309fn pd0_sha_ss1 x uint to uint
310ret xor xor rotr x 17 rotr x 19 shr x 10
311end
312
313fn pd0_sha_be32 buf view uchar 64 off ulong to uint
314b0 = shl uint get buf off 24
315b1 = shl uint get buf add off 1 16
316b2 = shl uint get buf add off 2 8
317b3 = uint get buf add off 3
318ret or or b0 b1 or b2 b3
319end
320
321fn pd0_sha_compress st edit uint 8 blk view uchar 64 k view uint 64 to void
322w = new uint 64 0
323v = new uint 8 0
324for i ulong 16
325put w i pd0_sha_be32 blk mul i 4
326end
327for i ulong 48
328j = add i 16
329s0 = pd0_sha_ss0 get w sub j 15
330s1 = pd0_sha_ss1 get w sub j 2
331val = add add get w sub j 16 s0 add get w sub j 7 s1
332put w j val
333end
334for i ulong 8
335put v i get st i
336end
337for r ulong 64
338t1 = add get v 7 pd0_sha_bs1 get v 4
339t1 = add t1 pd0_sha_ch get v 4 get v 5 get v 6
340t1 = add t1 add get k r get w r
341t2 = add pd0_sha_bs0 get v 0 pd0_sha_maj get v 0 get v 1 get v 2
342put v 7 get v 6
343put v 6 get v 5
344put v 5 get v 4
345put v 4 add get v 3 t1
346put v 3 get v 2
347put v 2 get v 1
348put v 1 get v 0
349put v 0 add t1 t2
350end
351for i ulong 8
352put st i add get st i get v i
353end
354ret
355end
356
357fn pd0_sha_emit st view uint 8 dst edit uchar 32 to void
358for i ulong 8
359word = get st i
360off = mul i 4
361put dst off uchar and shr word 24 0xff
362put dst add off 1 uchar and shr word 16 0xff
363put dst add off 2 uchar and shr word 8 0xff
364put dst add off 3 uchar and word 0xff
365end
366ret
367end
368
369fn pd0_sha256_hash msg view uchar * mlen ulong dst edit uchar 32 to void
370st = new uint 8 0
371k = new uint 64 0
372blk = new uchar 64 0
373pd0_sha_kinit edit k
374put st 0 0x6a09e667
375put st 1 0xbb67ae85
376put st 2 0x3c6ef372
377put st 3 0xa54ff53a
378put st 4 0x510e527f
379put st 5 0x9b05688c
380put st 6 0x1f83d9ab
381put st 7 0x5be0cd19
382pos = ulong 0
383while ge sub mlen pos 64 bound 262144
384for i ulong 64
385put blk i get msg add pos i
386end
387pd0_sha_compress edit st view blk view k
388pos = add pos 64
389end
390rem_len = sub mlen pos
391for i ulong 64
392put blk i uchar 0
393end
394for i ulong rem_len bound 64
395put blk i get msg add pos i
396end
397put blk rem_len uchar 0x80
398if gt rem_len 55
399pd0_sha_compress edit st view blk view k
400for i ulong 64
401put blk i uchar 0
402end
403end
404bits = mul mlen 8
405put blk 56 uchar and shr bits 56 0xff
406put blk 57 uchar and shr bits 48 0xff
407put blk 58 uchar and shr bits 40 0xff
408put blk 59 uchar and shr bits 32 0xff
409put blk 60 uchar and shr bits 24 0xff
410put blk 61 uchar and shr bits 16 0xff
411put blk 62 uchar and shr bits 8 0xff
412put blk 63 uchar and bits 0xff
413pd0_sha_compress edit st view blk view k
414pd0_sha_emit view st dst
415ret
416end