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 / tools / turing_verify_indep / src / selftest.om

598 lines · 13647 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Self tests from the documents' own test vectors, plus the log2 rounding audit (SPEC_GAPS G2).
3
4global g_st_fails ulong 1
5global g_bw_buf uchar 4096
6global g_bw_pos ulong 1
7global g_arg1 uchar 1024
8global g_arg2 uchar 1024
9global g_path uchar 2048
10global g_sidecar uchar 64
11
12text ts_ok "ok "
13text ts_fail "FAIL "
14text ts_abc "abc"
15text ts_abc_exp "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
16text ts_sha_lbl "sha256(abc)\n"
17text ts_b0_path "/tmp/is_b0.tym"
18text ts_b0_exp "83c04bdfc10614febd634092355ef03997ed255fde5bdec9a80fe2d8964998ce"
19text ts_b0_lbl "B0 test vector: L=232, digest "
20text ts_b3_path "/tmp/is_b3.tym"
21text ts_b3_exp "e729818b8289263e6819b21fc55f944f9d5113e7cfc4419d0badcc27f589f2ae"
22text ts_b3_lbl1 "B3 rebuilt from prereg section 0: L="
23text ts_b3_lbl2 " bytes="
24text ts_b3_lbl3 " digest "
25text ts_b3_bytes "B3 bytes 29..31 = 02 49 22 (MDE 7)\n"
26text ts_t_path "/tmp/is_t.tym"
27text ts_r2 "refuse version 1\n"
28text ts_r3 "refuse qbits 8\n"
29text ts_r5 "refuse trailing byte\n"
30text ts_r6 "refuse zero entry\n"
31text ts_r4 "refuse keybits mismatch (mask 2)\n"
32text ts_prof_a "/profiles/Turing-profile-v1.0.toml"
33text ts_prof_b "/profiles/Turing-profile-v1.1.toml"
34text ts_side_a "/profiles/Turing-profile-v1.0.sha256"
35text ts_side_b "/profiles/Turing-profile-v1.1.sha256"
36text ts_prof_lbl "profile sha256 = sidecar\n"
37text ts_mmem_suf "/M_mem.tym"
38text ts_mmem1_suf "/M_mem_seed1.tym"
39text ts_mmem_nm "M_mem"
40text ts_mmem1_nm "M_mem_seed1"
41text ts_mmem_fs "5a2ca3f457cc75b4f49fee99f054a0c32d15e3bfd442886c40e4a5f4945bab81"
42text ts_mmem1_fs "34df83a19756d1bd7e73f2b218576cefedad40ac2fc0e7a7a3da10a2a7fd206f"
43text ts_mmem_md "cbd6944c6618f508e5377e4698b0d472042cdf3b4592cb80e5427767e232c140"
44text ts_mmem1_md "5dfeff7b643178807b547fbd62388319b9a407109e345614679e4bf20ccfd652"
45text ts_sm_lbl "splitmix64 reference output\n"
46text ts_near_file "near_tie_q.txt"
47text ts_diff1 " table differs from exact rounding at q="
48text ts_diff2 ": "
49text ts_diff3 " vs "
50text ts_diff4 "\n"
51text ts_ub_chk "ub table (literal 32-step method) differs from correctly rounded exact value at exactly q=43481,46819 (G2: the written integer rule is the definition; differences "
52text ts_ub_chk2 ")\n"
53text ts_near1 " q whose exact ub fraction is within 5e-4 of .5: "
54text ts_near2 " (listed in near_tie_q.txt)\n"
55text ts_ub1 " ub(1)="
56text ts_ub2 " ub(7282)="
57text ts_ub3 " ub(32768)="
58text ts_ub4 " ub(65536)="
59text ts_pass "SELFTEST PASS ("
60text ts_fail_end "SELFTEST FAIL ("
61text ts_fend " failures)\n"
62text ts_sp " "
63text ts_dot "."
64text ts_colon ": r="
65text ts_l " L="
66text ts_rows " rows="
67text ts_kb " keybits="
68
69fn st_check ok bool to void
70 if ok
71 wr_str int 1 ts_ok
72 else
73 wr_str int 1 ts_fail
74 put g_st_fails 0 add get g_st_fails 0 1
75 end
76end
77
78fn bw_reset to void
79 i = ulong 0
80 while lt i 4096 bound 4096
81 put g_bw_buf i uchar 0
82 i = add i 1
83 end
84 put g_bw_pos 0 ulong 0
85end
86
87fn bw_put v ulong n ulong to void
88 i = n
89 while gt i 0 bound 64
90 i = sub i 1
91 p = get g_bw_pos 0
92 if ne and shr v i ulong 1 0
93 bi = shr p 3
94 bit = uchar shr ulong 128 and p 7
95 put g_bw_buf bi or get g_bw_buf bi bit
96 end
97 put g_bw_pos 0 add p 1
98 end
99end
100
101fn write_file_bytes path view uchar * buf view uchar * n ulong to int
102 fd = fopen path ulong 0 len path int 577 int 420
103 if lt fd long 0
104 ret int -1
105 end
106 _w = wr_all int fd buf ulong 0 n
107 _fc = fclose int fd
108 ret int 0
109end
110
111fn write_g_bw path view uchar * n ulong to int
112 fd = fopen path ulong 0 len path int 577 int 420
113 if lt fd long 0
114 ret int -1
115 end
116 p = ulong 0
117 while lt p n bound 4096
118 k = write int fd g_bw_buf p sub n p
119 if le k long 0
120 _fc = fclose int fd
121 ret int -1
122 end
123 p = add p ulong k
124 end
125 _fc = fclose int fd
126 ret int 0
127end
128
129fn build_b0 b edit uchar 29 to void
130 put b 0 uchar 0x54
131 put b 1 uchar 0x59
132 put b 2 uchar 0x4d
133 put b 3 uchar 0x30
134 put b 4 uchar 0x00
135 put b 5 uchar 0x09
136 put b 6 uchar 0x00
137 put b 7 uchar 0x10
138 put b 8 uchar 0x00
139 put b 9 uchar 0x00
140 put b 10 uchar 0x00
141 put b 11 uchar 0x00
142 put b 12 uchar 0x00
143 i = ulong 0
144 while lt i 7 bound 8
145 put b add 13 mul i 2 uchar 0x1c
146 put b add 14 mul i 2 uchar 0x72
147 i = add i 1
148 end
149 put b 27 uchar 0x1c
150 put b 28 uchar 0x71
151end
152
153fn test_refusals to void
154 b0 = new uchar 29 0
155 build_b0 b0
156 t = new uchar 30 0
157 for i ulong 29
158 put t i get b0 i
159 end
160 put t 4 uchar 1
161 _w1 = write_file_bytes ts_t_path t ulong 29
162 st_check eq is_model_load ts_t_path len ts_t_path int -2
163 wr_str int 1 ts_r2
164
165 for i ulong 29
166 put t i get b0 i
167 end
168 put t 7 uchar 8
169 _w2 = write_file_bytes ts_t_path t ulong 29
170 st_check eq is_model_load ts_t_path len ts_t_path int -3
171 wr_str int 1 ts_r3
172
173 for i ulong 29
174 put t i get b0 i
175 end
176 put t 29 uchar 0
177 _w3 = write_file_bytes ts_t_path t ulong 30
178 st_check eq is_model_load ts_t_path len ts_t_path int -5
179 wr_str int 1 ts_r5
180
181 for i ulong 29
182 put t i get b0 i
183 end
184 put t 13 uchar 0
185 put t 14 uchar 0
186 _w4 = write_file_bytes ts_t_path t ulong 29
187 st_check eq is_model_load ts_t_path len ts_t_path int -6
188 wr_str int 1 ts_r6
189
190 for i ulong 29
191 put t i get b0 i
192 end
193 put t 6 uchar 2
194 _w5 = write_file_bytes ts_t_path t ulong 29
195 st_check eq is_model_load ts_t_path len ts_t_path int -4
196 wr_str int 1 ts_r4
197end
198
199fn cat_path_arg1 suf view uchar * a1len ulong to ulong
200 i = ulong 0
201 while lt i a1len bound 1024
202 put g_path i get g_arg1 i
203 i = add i 1
204 end
205 j = ulong 0
206 sn = len suf
207 while lt j sn bound 256
208 put g_path add a1len j get suf j
209 j = add j 1
210 end
211 ret add a1len sn
212end
213
214fn cat_path_arg2 suf view uchar * a2len ulong to ulong
215 i = ulong 0
216 while lt i a2len bound 1024
217 put g_path i get g_arg2 i
218 i = add i 1
219 end
220 j = ulong 0
221 sn = len suf
222 while lt j sn bound 256
223 put g_path add a2len j get suf j
224 j = add j 1
225 end
226 ret add a2len sn
227end
228
229fn sha_gpath plen ulong hx edit uchar 64 to void
230 pbuf = new uchar 2048 0
231 d = new uchar 32 0
232 i = ulong 0
233 while lt i plen bound 2048
234 put pbuf i get g_path i
235 i = add i 1
236 end
237 _sf = sha_file pbuf plen d
238 sha_hex d hx
239end
240
241fn test_docs a1len ulong to void
242 hx = new uchar 64 0
243 sc = new uchar 64 0
244 pbuf = new uchar 2048 0
245 pv = is_profile_version
246 plen = ulong 0
247 if eq pv int 1
248 plen = cat_path_arg1 ts_prof_b a1len
249 else
250 plen = cat_path_arg1 ts_prof_a a1len
251 end
252 sha_gpath plen hx
253 if eq pv int 1
254 plen = cat_path_arg1 ts_side_b a1len
255 else
256 plen = cat_path_arg1 ts_side_a a1len
257 end
258 i = ulong 0
259 while lt i plen bound 2048
260 put pbuf i get g_path i
261 i = add i 1
262 end
263 fd = fopen pbuf ulong 0 plen int 0 int 0
264 ok = false
265 if ge fd long 0
266 got = read int fd sc ulong 0 ulong 64
267 _fc = fclose int fd
268 if eq got long 64
269 ok = buf_eq hx sc ulong 64
270 end
271 end
272 st_check ok
273 wr_str int 1 ts_prof_lbl
274end
275
276fn cand_check_hashes which ulong to bool
277 fsha = new uchar 32 0
278 hx = new uchar 64 0
279 for z ulong 32
280 put fsha z get g_m_fsha z
281 end
282 sha_hex fsha hx
283 ok = false
284 if eq which 0
285 ok = buf_eq hx ts_mmem_fs ulong 64
286 else
287 ok = buf_eq hx ts_mmem1_fs ulong 64
288 end
289 if not ok
290 ret false
291 end
292 for z ulong 32
293 put fsha z get g_m_mdig z
294 end
295 sha_hex fsha hx
296 if eq which 0
297 if not buf_eq hx ts_mmem_md ulong 64
298 ret false
299 end
300 if ne get g_m_meta 6 ulong 607838516
301 ret false
302 end
303 if ne get g_m_meta 4 ulong 3706331
304 ret false
305 end
306 ret eq get g_m_meta 3 ulong 36
307 end
308 if not buf_eq hx ts_mmem1_md ulong 64
309 ret false
310 end
311 if ne get g_m_meta 6 ulong 356335496
312 ret false
313 end
314 if ne get g_m_meta 4 ulong 2172776
315 ret false
316 end
317 ret eq get g_m_meta 3 ulong 36
318end
319
320fn test_one_cand which ulong a2len ulong to void
321 pbuf = new uchar 2048 0
322 plen = ulong 0
323 if eq which 0
324 plen = cat_path_arg2 ts_mmem_suf a2len
325 else
326 plen = cat_path_arg2 ts_mmem1_suf a2len
327 end
328 i = ulong 0
329 while lt i plen bound 2048
330 put pbuf i get g_path i
331 i = add i 1
332 end
333 r = is_model_load pbuf plen
334 ok = eq r int 0
335 if ok
336 ok = cand_check_hashes which
337 end
338 st_check ok
339 if eq which 0
340 wr_str int 1 ts_mmem_nm
341 else
342 wr_str int 1 ts_mmem1_nm
343 end
344 wr_str int 1 ts_colon
345 wr_i64 int 1 long r
346 wr_str int 1 ts_l
347 wr_u64 int 1 get g_m_meta 6
348 wr_str int 1 ts_rows
349 wr_u64 int 1 get g_m_meta 4
350 wr_str int 1 ts_kb
351 wr_u64 int 1 get g_m_meta 3
352 wr_str int 1 t_nl
353end
354
355note 52-bit exact Q62 squaring for log2(q) rounding audit (replaces long double log2l)
356fn log2_q52 q uint to ulong
357 ip = clz32_pos q
358 m = shl ulong q sub ulong 62 ip
359 frac = ulong 0
360 msb = shl ulong 1 63
361 i = ulong 1
362 while le i 52 bound 52
363 m = sq_shr62 m
364 if ge m msb
365 frac = or frac shl ulong 1 sub ulong 52 i
366 m = shr m 1
367 end
368 i = add i 1
369 end
370 ret or shl ip 52 frac
371end
372
373fn ub_audit_one q uint nf long to ulong
374 x52 = sub shl ulong 16 52 log2_q52 q
375 xhi = mul shr x52 20 ulong 1000000
376 xlo = mul and x52 ulong 0xfffff ulong 1000000
377 tot_hi = add xhi shr xlo 20
378 fr52 = or shl and tot_hi ulong 0xffffffff 20 and xlo ulong 0xfffff
379 rn = long shr tot_hi 32
380 half52 = shl ulong 1 51
381 if ge fr52 half52
382 rn = add rn 1
383 end
384 res = ulong 0
385 tbl = get g_ub_q16 ulong q
386 if ne rn tbl
387 res = or res ulong 1
388 wr_str int 1 ts_diff1
389 wr_u64 int 1 ulong q
390 wr_str int 1 ts_diff2
391 wr_i64 int 1 tbl
392 wr_str int 1 ts_diff3
393 wr_i64 int 1 rn
394 wr_str int 1 ts_diff4
395 end
396 dist = ulong 0
397 if ge fr52 half52
398 dist = sub fr52 half52
399 else
400 dist = sub half52 fr52
401 end
402 if lt dist ulong 2251799813685
403 res = or res ulong 2
404 if ge nf long 0
405 wr_u64 int nf ulong q
406 wr_str int nf t_nl
407 end
408 end
409 ret res
410end
411
412fn test_ub_audit to void
413 is_ub_table_init
414 ndiff = ulong 0
415 nnear = ulong 0
416 nf = fopen ts_near_file ulong 0 len ts_near_file int 577 int 420
417 q = uint 1
418 while le q uint 65536 bound 65537
419 res = ub_audit_one q nf
420 if ne and res 1 0
421 ndiff = add ndiff 1
422 end
423 if ne and res 2 0
424 nnear = add nnear 1
425 end
426 q = add q 1
427 end
428 if ge nf long 0
429 _fc = fclose int nf
430 end
431 ok = eq ndiff 2
432 if ok
433 ok = eq get g_ub_q16 43481 long 591903
434 end
435 if ok
436 ok = eq get g_ub_q16 46819 long 485194
437 end
438 st_check ok
439 wr_str int 1 ts_ub_chk
440 wr_u64 int 1 ndiff
441 wr_str int 1 ts_ub_chk2
442 wr_str int 1 ts_near1
443 wr_u64 int 1 nnear
444 wr_str int 1 ts_near2
445 wr_str int 1 ts_ub1
446 wr_i64 int 1 get g_ub_q16 1
447 wr_str int 1 ts_ub2
448 wr_i64 int 1 get g_ub_q16 7282
449 wr_str int 1 ts_ub3
450 wr_i64 int 1 get g_ub_q16 32768
451 wr_str int 1 ts_ub4
452 wr_i64 int 1 get g_ub_q16 65536
453 wr_str int 1 t_nl
454end
455
456fn test_b0_b3 to void
457 b0 = new uchar 29 0
458 build_b0 b0
459 _w0 = write_file_bytes ts_b0_path b0 ulong 29
460 r = is_model_load ts_b0_path len ts_b0_path
461 md = new uchar 32 0
462 for z ulong 32
463 put md z get g_m_mdig z
464 end
465 hx = new uchar 64 0
466 sha_hex md hx
467 ok = eq r int 0
468 if ok
469 ok = eq get g_m_meta 6 ulong 232
470 end
471 if ok
472 ok = eq get g_m_def 8 ushort 7281
473 end
474 if ok
475 ok = buf_eq hx ts_b0_exp ulong 64
476 end
477 st_check ok
478 wr_str int 1 ts_b0_lbl
479 _wh = wr_all int 1 hx ulong 0 ulong 64
480 wr_str int 1 t_nl
481
482 bw_reset
483 bw_put ulong 0x54594d30 ulong 32
484 bw_put ulong 0 ulong 8
485 bw_put ulong 9 ulong 8
486 bw_put ulong 1 ulong 8
487 bw_put ulong 16 ulong 8
488 bw_put ulong 4 ulong 8
489 bw_put ulong 16 ulong 32
490 for j ulong 7
491 bw_put ulong 7282 ulong 16
492 end
493 bw_put ulong 7281 ulong 16
494 k = ulong 0
495 while lt k 16 bound 16
496 bw_put k ulong 4
497 j = ulong 0
498 while lt j 8 bound 8
499 v = ulong 1
500 if lt k 15
501 if lt j 7
502 v = ulong 9362
503 end
504 else
505 if ge j 7
506 v = ulong 32765
507 end
508 end
509 bw_put v ulong 16
510 j = add j 1
511 end
512 k = add k 1
513 end
514 b3bytes = div add get g_bw_pos 0 7 8
515 _w3 = write_g_bw ts_b3_path b3bytes
516 r = is_model_load ts_b3_path len ts_b3_path
517 for z ulong 32
518 put md z get g_m_mdig z
519 end
520 sha_hex md hx
521 ok3 = eq r int 0
522 if ok3
523 ok3 = eq get g_m_meta 6 ulong 2344
524 end
525 if ok3
526 ok3 = eq b3bytes ulong 293
527 end
528 if ok3
529 ok3 = buf_eq hx ts_b3_exp ulong 64
530 end
531 st_check ok3
532 wr_str int 1 ts_b3_lbl1
533 wr_u64 int 1 get g_m_meta 6
534 wr_str int 1 ts_b3_lbl2
535 wr_u64 int 1 b3bytes
536 wr_str int 1 ts_b3_lbl3
537 _wh3 = wr_all int 1 hx ulong 0 ulong 64
538 wr_str int 1 t_nl
539 ok3b = eq get g_bw_buf 29 uchar 0x02
540 if ok3b
541 ok3b = eq get g_bw_buf 30 uchar 0x49
542 end
543 if ok3b
544 ok3b = eq get g_bw_buf 31 uchar 0x22
545 end
546 st_check ok3b
547 wr_str int 1 ts_b3_bytes
548end
549
550fn main to int
551 put g_st_fails 0 ulong 0
552 sha_init ulong 0
553 sha_upd ulong 0 ts_abc ulong 0 ulong 3
554 d = new uchar 32 0
555 hx = new uchar 64 0
556 sha_final ulong 0 d
557 sha_hex d hx
558 st_check buf_eq hx ts_abc_exp ulong 64
559 wr_str int 1 ts_sha_lbl
560
561 test_b0_b3
562 test_refusals
563
564 ac = argcnt
565 if gt ac long 1
566 a1 = argread ulong 1 g_arg1 ulong 0 ulong 1024
567 if gt a1 long 0
568 test_docs ulong a1
569 end
570 end
571 if gt ac long 2
572 a2 = argread ulong 2 g_arg2 ulong 0 ulong 1024
573 if gt a2 long 0
574 test_one_cand ulong 0 ulong a2
575 test_one_cand ulong 1 ulong a2
576 end
577 end
578
579 put g_rng_st 0 ulong 0
580 sm = is_splitmix64
581 smexp = or shl ulong 0xe220a839 32 ulong 0x7b1dcdaf
582 st_check eq sm smexp
583 wr_str int 1 ts_sm_lbl
584
585 test_ub_audit
586
587 f = get g_st_fails 0
588 if ne f 0
589 wr_str int 1 ts_fail_end
590 wr_u64 int 1 f
591 wr_str int 1 ts_fend
592 ret int 1
593 end
594 wr_str int 1 ts_pass
595 wr_u64 int 1 f
596 wr_str int 1 ts_fend
597 ret int 0
598end