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 / tests / turing / test_ty_math.om

1042 lines · 24953 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3note
4note tests/turing/test_ty_math.om -- Pure OM-2 test suite for Turing Yield TY-1/TY-2
5note Composed with src/turing/ty_core_lib.om.
6
7text t_fix_a "tests/turing/fixtures/ctr1_seed1_crumbs05-07.ctr"
8text t_fix_b "tests/turing/fixtures/ctr1_seed1_crumbs10-12.ctr"
9text t_gold_a "777193633a4ff8092659024f18194bd111da5129c6e8bc3f04a292c138a732cc"
10text t_gold_b "c01b91468d5b6cfb4953166d6f161cee2dceeb174675d0f7d90418b2b8967ccc"
11text t_gold_prof "f4292df9748e306165f30b250673a0062391054321ea6dbe68855ecaa0fa8be8"
12text t_gold_man "6efc04b525af60706bd0f3176013fe234d8e8a52e2554cff3add8a4e16171dca"
13text t_gold_gain "acf0b17065f0d4930425e55ad4af9bcfd91a2c0c0d0aa4dc6f086c3457e86cd6"
14text t_def_pfx "build/test_ty_math"
15text t_lbl_a "fixture A"
16text t_lbl_b "fixture B"
17text t_suf_det ".det"
18text t_suf_bad ".bad.ctr"
19text t_suf_tmp ".held_tamper.ctr"
20
21text t_s_pass "test_ty_math: "
22text t_s_p2 " passed, "
23text t_s_f2 " failed\n"
24text t_s_fail "FAIL check "
25text t_s_nl "\n"
26text t_d_ubsum "ub_table_sum="
27text t_d_q0 "quant0="
28text t_d_q1 "quant1="
29text t_d_q2 "quant2="
30text t_d_hist "fixA_hist="
31text t_d_memo1 "memorizer lm="
32text t_d_memo2 " t_held_ub="
33text t_d_memo3 " t_fit_ub="
34text t_d_to1 "train_only t_fit_ub="
35text t_d_to2 " t_held_ub="
36text t_d_fg1 "fixture gain t_ub="
37text t_d_fg2 " lm_b="
38text t_d_fg3 " lm_c="
39text t_d_fg4 " digest="
40
41global g_pass ulong 1
42global g_fail ulong 1
43global g_chk_id ulong 1
44global g_pfx uchar 512
45global g_pfx_len ulong 1
46global g_det_buf uchar 16384
47global g_det_len ulong 1
48global g_path_tmp uchar 600
49global g_path_len ulong 1
50global g_io_buf uchar 8192
51global g_mcode_bak uchar 1048576
52
53fn out_str s view uchar * to void
54 r = write 1 s 0 len s
55end
56
57fn out_u64 v ulong to void
58 tv_clr
59 tv_u64 v
60 n = get g_tvl 0
61 r = write 1 g_tv 0 n
62end
63
64fn det_b c uchar to void
65 n = get g_det_len 0
66 if lt n 16384
67 put g_det_buf n c
68 put g_det_len 0 add n 1
69 end
70end
71
72fn det_txt s view uchar * to void
73 n = len s
74 for i ulong n bound 512
75 det_b get s i
76 end
77end
78
79fn det_u64 v ulong to void
80 tv_clr
81 tv_u64 v
82 n = get g_tvl 0
83 for i ulong n bound 32
84 det_b get g_tv i
85 end
86end
87
88fn det_i64 v long to void
89 tv_clr
90 tv_i64 v
91 n = get g_tvl 0
92 for i ulong n bound 32
93 det_b get g_tv i
94 end
95end
96
97fn det_dig slot ulong to void
98 tv_clr
99 tv_dig slot
100 for i ulong 64
101 det_b get g_tv i
102 end
103end
104
105fn chk cond bool to void
106 id = add get g_chk_id 0 1
107 put g_chk_id 0 id
108 if cond
109 put g_pass 0 add get g_pass 0 1
110 else
111 put g_fail 0 add get g_fail 0 1
112 out_str t_s_fail
113 out_u64 id
114 out_str t_s_nl
115 end
116end
117
118fn make_tmp_path suf view uchar * to void
119 n = get g_pfx_len 0
120 for i ulong n bound 512
121 put g_path_tmp i get g_pfx i
122 put g_path i get g_pfx i
123 end
124 sl = len suf
125 for i ulong sl bound 64
126 put g_path_tmp add n i get suf i
127 put g_path add n i get suf i
128 end
129 put g_path_len 0 add n sl
130 put g_plen 0 add n sl
131end
132
133fn copy_file_to_tmp src view uchar * to bool
134 fd1 = fopen src 0 len src int 0 int 0
135 if lt fd1 0
136 ret false
137 end
138 note O_WRONLY|O_CREAT|O_TRUNC = 1|64|512 = 577
139 fd2 = fopen g_path_tmp 0 get g_path_len 0 int 577 int 420
140 if lt fd2 0
141 dc = fclose int fd1
142 ret false
143 end
144 done = false
145 while not done bound 100000
146 k = read int fd1 g_io_buf 0 8192
147 if lt k 0
148 dc1 = fclose int fd1
149 dc2 = fclose int fd2
150 ret false
151 end
152 if eq k 0
153 done = true
154 else
155 w = write int fd2 g_io_buf 0 ulong k
156 if ne w k
157 dc1 = fclose int fd1
158 dc2 = fclose int fd2
159 ret false
160 end
161 end
162 end
163 dc1 = fclose int fd1
164 dc2 = fclose int fd2
165 ret true
166end
167
168fn patch_tmp off ulong val uchar to bool
169 note O_RDWR = 2
170 fd = fopen g_path_tmp 0 get g_path_len 0 int 2 int 0
171 if lt fd 0
172 ret false
173 end
174 b = new uchar 1 0
175 put b 0 val
176 w = pwrite int fd b 0 1 long off
177 dc = fclose int fd
178 ret eq w 1
179end
180
181fn append_zero_tmp to bool
182 note O_WRONLY|O_APPEND = 1|1024 = 1025
183 fd = fopen g_path_tmp 0 get g_path_len 0 int 1025 int 0
184 if lt fd 0
185 ret false
186 end
187 b = new uchar 1 0
188 w = write int fd b 0 1
189 dc = fclose int fd
190 ret eq w 1
191end
192
193fn dg_eq_txt slot ulong hex view uchar * to bool
194 if ne len hex 64
195 ret false
196 end
197 h = new uchar 64 0
198 dg_to_hex slot h
199 for i ulong 64
200 if ne get h i get hex i
201 ret false
202 end
203 end
204 ret true
205end
206
207note ---- Test 1: Math ----
208fn t_math to void
209 sum = long 0
210 q = ulong 1
211 while le q TY_QONE bound 65536
212 sum = add sum ty_ubits_q16 q
213 q = add q 1
214 end
215 chk eq ty_ubits_q16 TY_QONE 0
216 chk eq ty_ubits_q16 32768 1000000
217 chk eq ty_ubits_q16 1 16000000
218 chk and eq ty_ubits_q16 0 TY_E_RANGE eq ty_ubits_q16 65537 TY_E_RANGE
219 det_txt t_d_ubsum
220 det_i64 sum
221 det_b uchar 10
222
223 chk and eq ty_ubits_ratio 1 2 TY_OK eq get g_ty_ret_i 0 1000000
224 chk and eq ty_ubits_ratio 7 7 TY_OK eq get g_ty_ret_i 0 0
225 chk eq ty_ubits_ratio 0 5 TY_E_ZERO_REALIZED
226 chk eq ty_ubits_ratio 3 2 TY_E_RANGE
227 chk and eq ty_ubits_ratio 1 shl ulong 1 62 TY_OK eq get g_ty_ret_i 0 62000000
228
229 tol = f64 0.000000000001
230 ok = new f64 3 0.0
231 put ok 0 f64 0.5
232 put ok 1 f64 0.25
233 put ok 2 f64 0.25
234 chk eq ty_validate_f64 ok 3 tol TY_OK
235
236 nanv = new f64 3 0.0
237 put nanv 0 f64 0.5
238 put nanv 1 div f64 0.0 f64 0.0
239 put nanv 2 f64 0.5
240 chk eq ty_validate_f64 nanv 3 tol TY_E_NAN
241
242 infv = new f64 3 0.0
243 put infv 0 f64 0.5
244 put infv 1 div f64 1.0 f64 0.0
245 put infv 2 f64 0.0
246 chk eq ty_validate_f64 infv 3 tol TY_E_INF
247
248 negv = new f64 3 0.0
249 put negv 0 f64 0.75
250 put negv 1 f64 -0.25
251 put negv 2 f64 0.5
252 chk eq ty_validate_f64 negv 3 tol TY_E_NEG
253
254 offv = new f64 3 0.0
255 put offv 0 f64 0.5
256 put offv 1 f64 0.25
257 put offv 2 f64 0.2
258 chk eq ty_validate_f64 offv 3 tol TY_E_NORM
259 chk eq ty_validate_f64 offv 3 f64 0.1 TY_OK
260 t_math_f64_2
261end
262
263fn t_math_f64_2 to void
264 tol = f64 0.000000000001
265 ok = new f64 3 0.0
266 put ok 0 f64 0.5
267 put ok 1 f64 0.25
268 put ok 2 f64 0.25
269 negz = new f64 3 0.0
270 put negz 0 f64 0.5
271 put negz 1 bitcast f64 shl ulong 1 63
272 put negz 2 f64 0.5
273 chk eq ty_validate_f64 negz 3 tol TY_OK
274 chk eq ty_validate_f64 ok 0 tol TY_E_ARG
275
276 chk and eq ty_ubits_f64 ok 3 0 tol TY_OK eq get g_ty_ret_i 0 1000000
277 chk and eq ty_ubits_f64 ok 3 1 tol TY_OK eq get g_ty_ret_i 0 2000000
278
279 zv = new f64 3 0.0
280 put zv 0 f64 0.5
281 put zv 1 f64 0.5
282 put zv 2 f64 0.0
283 chk eq ty_ubits_f64 zv 3 2 tol TY_E_ZERO_REALIZED
284
285 note 1e-300 in IEEE-754: 0x01ad7f29abcaf485
286 put zv 2 bitcast f64 or shl ulong 0x01ad7f29 32 ulong 0xabcaf485
287 chk eq ty_ubits_f64 zv 3 2 tol TY_E_UNDERFLOW
288
289 note subnormal 4.9e-324 = 0x1
290 put zv 2 bitcast f64 ulong 1
291 chk eq ty_ubits_f64 zv 3 2 tol TY_E_UNDERFLOW
292
293 note 2^-60 = exp 1023-60=963 -> 0x3c30000000000000
294 put zv 2 bitcast f64 shl ulong 963 52
295 chk and eq ty_ubits_f64 zv 3 2 tol TY_OK eq get g_ty_ret_i 0 60000000
296 chk eq ty_ubits_f64 ok 3 3 tol TY_E_ARG
297
298 p3 = new f64 2 0.0
299 put p3 0 f64 0.3
300 put p3 1 f64 0.7
301 chk eq ty_ubits_f64 p3 2 0 tol TY_OK
302 note -log2(0.3)*1e6 = 1736965.594... -> 1736966
303 v3 = get g_ty_ret_i 0
304 chk and ge v3 1736965 le v3 1736967
305
306 chk and eq ty_dl 1000 5000000 TY_OK eq get g_ty_ret_i 0 1005000000
307 chk eq ty_dl shr ulong 0xffffffffffffffff 1 0 TY_E_OVERFLOW
308 chk eq ty_dl 1 -1 TY_E_RANGE
309 chk and eq ty_gain 10 4 TY_OK eq get g_ty_ret_i 0 6
310 chk and eq ty_gain 4 10 TY_OK eq get g_ty_ret_i 0 -6
311 chk eq ty_add bitcast long or shl ulong 0x7fffffff 32 ulong 0xffffffff 1 TY_E_OVERFLOW
312
313 t_math_quant
314end
315
316fn t_math_quant to void
317 c = new ulong 9 0
318 qv = new uint 9 0
319 for k ulong 3
320 for x ulong 9
321 put c x 0
322 end
323 if eq k 1
324 put c 0 1000000
325 put c 8 1
326 end
327 if eq k 2
328 for x ulong 9
329 put c x 5
330 end
331 end
332 chk eq ty_quantize_kt c 9 qv TY_OK
333 s = ulong 0
334 mn = ulong 999999
335 for x ulong 9
336 v = ulong get qv x
337 s = add s v
338 if lt v mn
339 mn = v
340 end
341 end
342 chk and eq s TY_QONE ge mn 1
343 if eq k 0
344 det_txt t_d_q0
345 else
346 if eq k 1
347 det_txt t_d_q1
348 else
349 det_txt t_d_q2
350 end
351 end
352 for x ulong 9
353 det_u64 ulong get qv x
354 det_b uchar 32
355 end
356 det_b uchar 10
357 if eq k 0
358 chk and eq get qv 0 uint 7282 eq get qv 8 uint 7281
359 end
360 if eq k 1
361 chk and eq get qv 1 uint 1 gt get qv 0 uint 65000
362 end
363 end
364 chk and eq ty_quantize_kt c 1 qv TY_E_ARG eq ty_quantize_kt c 17 qv TY_E_ARG
365end
366
367note ---- Test 2: Reader ----
368fn t_reader to void
369 st_reset_all
370 st_begin 0
371 na = ty_ctr1_read t_fix_a len t_fix_a 0
372 st_begin 1
373 nb = ty_ctr1_read t_fix_b len t_fix_b 1
374 chk and eq na 1418 and eq get g_st_ncr 0 3 eq get g_st_gap 0 0
375 chk and eq nb 1330 eq get g_st_ncr 1 3
376 rc1 = ty_file_sha256 t_fix_a len t_fix_a 0
377 chk and eq rc1 TY_OK dg_eq_txt 0 t_gold_a
378 rc2 = ty_file_sha256 t_fix_b len t_fix_b 1
379 chk and eq rc2 TY_OK dg_eq_txt 1 t_gold_b
380
381 hist = new ulong 9 0
382 so = get g_st_off 0
383 sn = get g_st_n 0
384 for i ulong sn bound 2000
385 sym = ev_sym get g_ev add so i
386 put hist sym add get hist sym 1
387 end
388 det_txt t_d_hist
389 for x ulong 9
390 det_u64 get hist x
391 det_b uchar 32
392 end
393 det_b uchar 10
394 t_reader_hostile
395end
396
397fn t_reader_hostile to void
398 make_tmp_path t_suf_bad
399 offs = new ulong 7 0
400 vals = new uchar 7 0
401 put offs 0 0
402 put vals 0 uchar 88
403 put offs 1 4
404 put vals 1 uchar 2
405 put offs 2 6
406 put vals 2 uchar 3
407 put offs 3 22
408 put vals 3 uchar 1
409 put offs 4 20
410 put vals 4 uchar 15
411 put offs 5 25
412 put vals 5 uchar 3
413 put offs 6 ulong 1243
414 put vals 6 uchar 0
415 for i ulong 7
416 okc = copy_file_to_tmp t_fix_a
417 okp = patch_tmp get offs i get vals i
418 st_reset_all
419 st_begin 0
420 n = ty_ctr1_read_gp 0
421 if eq i 6
422 chk and eq n 1418 eq get g_st_ncr 0 4
423 else
424 chk eq n TY_E_FORMAT
425 end
426 end
427 okc = copy_file_to_tmp t_fix_a
428 okp = patch_tmp ulong 1243 uchar 2
429 st_reset_all
430 st_begin 0
431 chk eq ty_ctr1_read_gp 0 TY_E_FORMAT
432
433 note find pruned record in FIX_A and patch rc=4
434 fd = fopen t_fix_a 0 len t_fix_a int 0 int 0
435 pidx = long -1
436 ri = long 0
437 while lt ri 2000 bound 2000
438 k = read int fd g_io_buf 0 TY_CTR1_BYTES
439 if ne k long TY_CTR1_BYTES
440 break
441 end
442 if and eq get g_io_buf 6 uchar 1 ne get g_io_buf 23 uchar 0
443 pidx = ri
444 break
445 end
446 ri = add ri 1
447 end
448 dc = fclose int fd
449 okc = copy_file_to_tmp t_fix_a
450 if ge pidx 0
451 okp = patch_tmp add mul ulong pidx TY_CTR1_BYTES 7 uchar 4
452 end
453 st_reset_all
454 st_begin 0
455 chk and ge pidx 0 eq ty_ctr1_read_gp 0 TY_E_FORMAT
456
457 note size % 247 != 0
458 okc = copy_file_to_tmp t_fix_a
459 oka = append_zero_tmp
460 st_reset_all
461 st_begin 0
462 chk eq ty_ctr1_read_gp 0 TY_E_FORMAT
463 ru = unlink g_path_tmp 0 get g_path_len 0
464end
465
466note ---- Test 3: Model Code ----
467fn model_01_eq nr ulong to bool
468 for x ulong TY_OUT_K
469 if ne get g_md_def x get g_md_def add 16 x
470 ret false
471 end
472 end
473 for i ulong nr bound 65536
474 if ne get g_md_rkey i get g_md_rkey add TY_MAX_MROWS i
475 ret false
476 end
477 b0 = mul i 16
478 b1 = mul add TY_MAX_MROWS i 16
479 for x ulong TY_OUT_K
480 if ne get g_md_rq add b0 x get g_md_rq add b1 x
481 ret false
482 end
483 end
484 end
485 ret true
486end
487
488fn t_model_code to void
489 st_reset_all
490 st_begin 0
491 chk ge ty_ctr1_read t_fix_a len t_fix_a 0 0
492 put g_fit_sids 0 0
493 chk eq ty_model_fit 0 1 or TY_F_OP TY_F_PREV1 TY_OUT_K TY_FIT_KEEP_ALL TY_OK
494 chk eq ty_model_encode 0 0 TY_OK
495 nb = get g_mclen 0
496 bits = get g_mcbits 0
497 nr = get g_md_nr 0
498 kb = get g_md_kb 0
499 chk eq bits add add TY_MODEL_HEADER_BITS 128 mul nr add kb 128
500 chk and eq ty_model_decode 0 nb 1 TY_OK and eq get g_ty_ret_u 0 bits eq get g_md_nr 1 nr
501 chk model_01_eq nr
502 r1 = ty_model_score 0 0
503 s1 = get g_ty_ret_i 0
504 r2 = ty_model_score 1 0
505 s2 = get g_ty_ret_i 0
506 chk eq s1 s2
507 t_model_code_mut nb kb
508end
509
510fn t_model_code_mut nb ulong kb ulong to void
511 for i ulong nb bound 1048576
512 put g_mcode_bak i get g_mcode i
513 end
514 note mutations
515 put g_mcode 0 uchar 88
516 chk eq ty_model_decode 0 nb 1 TY_E_FORMAT
517 put g_mcode 0 get g_mcode_bak 0
518
519 put g_mcode 4 uchar 1
520 chk eq ty_model_decode 0 nb 1 TY_E_PROFILE
521 put g_mcode 4 get g_mcode_bak 4
522
523 put g_mcode 7 uchar 12
524 chk eq ty_model_decode 0 nb 1 TY_E_PROFILE
525 put g_mcode 7 get g_mcode_bak 7
526
527 put g_mcode 8 uchar add kb 1
528 chk eq ty_model_decode 0 nb 1 TY_E_FORMAT
529 put g_mcode 8 get g_mcode_bak 8
530
531 put g_mcode 13 uchar 0
532 put g_mcode 14 uchar 0
533 chk eq ty_model_decode 0 nb 1 TY_E_PROFILE
534 put g_mcode 13 get g_mcode_bak 13
535 put g_mcode 14 get g_mcode_bak 14
536
537 chk eq ty_model_decode 0 sub nb 1 1 TY_E_FORMAT
538end
539
540fn dl_of fit_sid ulong sc_sid ulong mask ulong rule ulong to long
541 put g_fit_sids 0 fit_sid
542 rc = ty_model_fit 0 1 mask TY_OUT_K rule
543 if ne rc TY_OK
544 ret -1
545 end
546 rc = ty_model_encode 0 0
547 if ne rc TY_OK
548 ret -1
549 end
550 nb = get g_mclen 0
551 lm = get g_mcbits 0
552 put g_ty_ret_u 5 lm
553 rc = ty_model_decode 0 nb 1
554 if ne rc TY_OK
555 ret -1
556 end
557 rc = ty_model_score 1 sc_sid
558 if ne rc TY_OK
559 ret -1
560 end
561 rc = ty_dl lm get g_ty_ret_i 0
562 if ne rc TY_OK
563 ret -1
564 end
565 ret get g_ty_ret_i 0
566end
567
568note ---- Test 4: Memorizer ----
569fn t_memorizer to void
570 st_reset_all
571 st_begin 0
572 na = ty_ctr1_read t_fix_a len t_fix_a 0
573 st_begin 1
574 nb = ty_ctr1_read t_fix_b len t_fix_b 1
575 chk and ge na 0 ge nb 0
576 dlb_h = dl_of 0 1 TY_F_PREV1 TY_FIT_MDL_PRUNE
577 dlm_h = dl_of 0 1 TY_F_POS TY_FIT_KEEP_ALL
578 lm_m = get g_ty_ret_u 5
579 dlb_f = dl_of 0 0 TY_F_PREV1 TY_FIT_MDL_PRUNE
580 dlm_f = dl_of 0 0 TY_F_POS TY_FIT_KEEP_ALL
581 t_h = sub dlb_h dlm_h
582 t_f = sub dlb_f dlm_f
583 chk gt lm_m mul ulong 1418 160
584 chk lt t_h 0
585 chk lt t_f 0
586 det_txt t_d_memo1
587 det_u64 lm_m
588 det_txt t_d_memo2
589 det_i64 t_h
590 det_txt t_d_memo3
591 det_i64 t_f
592 det_b uchar 10
593end
594
595note ---- Test 5: Train-only gain ----
596fn sm64 s ulong to ulong
597 z = mul xor s shr s 30 or shl ulong 0xbf58476d 32 ulong 0x1ce4e5b9
598 z = mul xor z shr z 27 or shl ulong 0x94d049bb 32 ulong 0x133111eb
599 ret xor z shr z 31
600end
601
602fn t_train_only to void
603 st_reset_all
604 st_begin 0
605 seed = ulong 12345
606 for c ulong 20
607 for i ulong 500
608 f = ulong 0
609 if eq i 0
610 f = 1
611 end
612 rc = st_push 0 ev_pack rem i TY_OUT_K rem i 15 0 f c i
613 end
614 end
615 st_begin 1
616 for c ulong 20
617 for i ulong 500
618 f = ulong 0
619 if eq i 0
620 f = 1
621 end
622 seed = add seed or shl ulong 0x9e3779b9 32 ulong 0x7f4a7c15
623 sym = rem sm64 seed TY_OUT_K
624 rc = st_push 1 ev_pack sym rem i 15 0 f c i
625 end
626 end
627 bf = dl_of 0 0 0 TY_FIT_MDL_PRUNE
628 cf = dl_of 0 0 TY_F_PREV1 TY_FIT_MDL_PRUNE
629 bh = dl_of 0 1 0 TY_FIT_MDL_PRUNE
630 ch = dl_of 0 1 TY_F_PREV1 TY_FIT_MDL_PRUNE
631 tf = sub bf cf
632 th = sub bh ch
633 chk gt tf 0
634 chk lt th 0
635 det_txt t_d_to1
636 det_i64 tf
637 det_txt t_d_to2
638 det_i64 th
639 det_b uchar 10
640end
641
642note ---- Test 6: Records & Hostile Cases ----
643fn set_sp_lbl which ulong idx ulong s view uchar * to void
644 lo = mul idx 64
645 n = len s
646 for i ulong n bound 64
647 if eq which 0
648 put g_sp_flbl add lo i get s i
649 else
650 put g_sp_hlbl add lo i get s i
651 end
652 end
653 if eq which 0
654 put g_sp_flen idx n
655 else
656 put g_sp_hlen idx n
657 end
658end
659
660fn fit_fixture_model fi ulong cs ulong role ulong mask ulong prof_slot ulong split_slot ulong to long
661 st_reset_all
662 st_begin 0
663 rc = ty_file_sha256_gp add 20 fi
664 if ne rc TY_OK
665 ret rc
666 end
667 if lt ty_ctr1_read_gp 0 0
668 ret TY_E_FORMAT
669 end
670 put g_fit_sids 0 0
671 rc = ty_model_fit fi 1 mask TY_OUT_K TY_FIT_MDL_PRUNE
672 if ne rc TY_OK
673 ret rc
674 end
675 rc = ty_model_encode fi cs
676 if ne rc TY_OK
677 ret rc
678 end
679 ty_model_digest cs get g_mclen cs add 30 fi
680 put g_fr_prof fi prof_slot
681 put g_fr_split fi split_slot
682 put g_fr_role fi role
683 put g_fr_mask fi mask
684 put g_fr_rule fi TY_FIT_MDL_PRUNE
685 put g_fr_nf fi 1
686 put g_fr_fdig mul fi 12 add 20 fi
687 put g_fr_mod fi add 30 fi
688 put g_fr_mbits fi get g_mcbits cs
689 ret TY_OK
690end
691
692note Check one fit record against profile (slot 10), split (slot 11), floor_q
693fn check_fit_rec fi ulong cs ulong prof_slot ulong split_slot ulong to long
694 if not dg_eq get g_fr_prof fi prof_slot
695 ret TY_E_PROFILE
696 end
697 if not dg_eq get g_fr_split fi split_slot
698 ret TY_E_PROVENANCE
699 end
700 nf = get g_fr_nf fi
701 bo = mul fi 12
702 nh = get g_sp_nh 0
703 for i ulong nf bound 12
704 fd = get g_fr_fdig add bo i
705 for j ulong nh bound 12
706 if dg_eq fd get g_sp_hdig j
707 ret TY_E_LEAK
708 end
709 end
710 end
711 if ne nf get g_sp_nf 0
712 ret TY_E_PROVENANCE
713 end
714 for i ulong nf bound 12
715 if not dg_eq get g_fr_fdig add bo i get g_sp_fdig i
716 ret TY_E_PROVENANCE
717 end
718 end
719 if ne get g_fr_rule fi TY_FIT_MDL_PRUNE
720 ret TY_E_PROFILE
721 end
722 ty_model_digest cs get g_mclen cs 45
723 if not dg_eq 45 get g_fr_mod fi
724 ret TY_E_DIGEST
725 end
726 ms = add 2 fi
727 rc = ty_model_decode cs get g_mclen cs ms
728 if ne rc TY_OK
729 ret rc
730 end
731 if ne get g_md_k ms TY_OUT_K
732 ret TY_E_PROFILE
733 end
734 if ne get g_ty_ret_u 0 get g_fr_mbits fi
735 ret TY_E_PROFILE
736 end
737 if ne get g_md_mask ms get g_fr_mask fi
738 ret TY_E_PROFILE
739 end
740 ret TY_OK
741end
742
743fn gain_compute_fix prof_slot ulong split_slot ulong floor_q ulong to long
744 if ne floor_q 1
745 ret TY_E_PROFILE
746 end
747 if or ne get g_fr_role 0 TY_ROLE_BASELINE ne get g_fr_mask 0 TY_V0_BASELINE_MASK
748 ret TY_E_BASELINE
749 end
750 if ne get g_fr_role 1 TY_ROLE_CANDIDATE
751 ret TY_E_ARG
752 end
753 rc = check_fit_rec 0 0 prof_slot split_slot
754 if ne rc TY_OK
755 ret rc
756 end
757 rc = check_fit_rec 1 1 prof_slot split_slot
758 if ne rc TY_OK
759 ret rc
760 end
761 rc = ty_file_sha256_gp 46
762 if ne rc TY_OK
763 ret rc
764 end
765 if not dg_eq 46 get g_sp_hdig 0
766 ret TY_E_DIGEST
767 end
768 st_reset_all
769 st_begin 0
770 if lt ty_ctr1_read_gp 0 0
771 ret TY_E_FORMAT
772 end
773 rc = ty_model_score 2 0
774 if ne rc TY_OK
775 ret rc
776 end
777 put g_gr_i 0 get g_ty_ret_i 0
778 put g_gr_u 0 get g_ty_ret_u 0
779 rc = ty_model_score 3 0
780 if ne rc TY_OK
781 ret rc
782 end
783 put g_gr_i 1 get g_ty_ret_i 0
784 rc = ty_dl get g_fr_mbits 0 get g_gr_i 0
785 put g_gr_i 2 get g_ty_ret_i 0
786 rc = ty_dl get g_fr_mbits 1 get g_gr_i 1
787 put g_gr_i 3 get g_ty_ret_i 0
788 rc = ty_gain get g_gr_i 2 get g_gr_i 3
789 put g_gr_i 4 get g_ty_ret_i 0
790 rc = ty_fit_digest 0 40
791 rc = ty_fit_digest 1 41
792 put g_gr_dig 0 prof_slot
793 put g_gr_dig 1 split_slot
794 put g_gr_dig 2 40
795 put g_gr_dig 3 41
796 put g_gr_dig 4 get g_fr_mod 0
797 put g_gr_dig 5 get g_fr_mod 1
798 put g_gr_u 1 get g_fr_mbits 0
799 put g_gr_u 2 get g_fr_mbits 1
800 put g_gr_u 3 1
801 ns = get g_gr_u 0
802 qnum = add mul mul 2 ns TY_QERR_MILLI_UB 999
803 qerr = long div qnum 1000
804 put g_gr_i 5 qerr
805 put g_gr_i 6 TY_V0_MARGIN_UB
806 put g_gr_fnsym 0 ns
807 t_ub = get g_gr_i 4
808 put g_gr_ftub 0 t_ub
809 pass = ulong 0
810 diff = sub t_ub qerr
811 if and gt diff TY_V0_MARGIN_UB gt diff 0
812 pass = 1
813 end
814 put g_gr_pass 0 pass
815 ret TY_OK
816end
817
818fn t_records to void
819 note real V0 profile digest golden
820 rc = dg_parse_hex t_gold_man len t_gold_man 5
821 rc = ty_profile_v0_digest 5 1 6
822 chk dg_eq_txt 6 t_gold_prof
823 rc = ty_profile_v0_digest 5 2 7
824 chk not dg_eq_txt 7 t_gold_prof
825
826 note fixture profile + split
827 rc = ty_file_sha256 t_fix_a len t_fix_a 0
828 rc = ty_file_sha256 t_fix_b len t_fix_b 1
829 sh_init
830 for i ulong 32
831 sh_byte get g_dig i
832 end
833 for i ulong 32
834 sh_byte get g_dig add 32 i
835 end
836 sh_final 2
837 rc = ty_profile_v0_digest 2 1 10
838 put g_sp_nf 0 1
839 put g_sp_nh 0 1
840 put g_sp_fdig 0 0
841 put g_sp_hdig 0 1
842 set_sp_lbl 0 0 t_lbl_a
843 set_sp_lbl 1 0 t_lbl_b
844 rc = ty_split_digest 11
845
846 gp_set t_fix_a len t_fix_a
847 chk eq fit_fixture_model 0 0 TY_ROLE_BASELINE TY_V0_BASELINE_MASK 10 11 TY_OK
848 gp_set t_fix_a len t_fix_a
849 chk eq fit_fixture_model 1 1 TY_ROLE_CANDIDATE TY_V0_CANDIDATE_MASK 10 11 TY_OK
850 gp_set t_fix_b len t_fix_b
851 chk eq gain_compute_fix 10 11 1 TY_OK
852 rc = ty_gain_digest 50
853 chk and eq get g_gr_u 0 1330 eq sub get g_gr_i 2 get g_gr_i 3 get g_gr_i 4
854 chk eq get g_gr_ftub 0 get g_gr_i 4
855 chk eq get g_gr_i 5 long div add mul mul ulong 2 1330 501 999 1000
856 chk eq get g_gr_pass 0 0
857 chk dg_eq_txt 50 t_gold_gain
858 det_txt t_d_fg1
859 det_i64 get g_gr_i 4
860 det_txt t_d_fg2
861 det_u64 get g_gr_u 1
862 det_txt t_d_fg3
863 det_u64 get g_gr_u 2
864 det_txt t_d_fg4
865 det_dig 50
866 det_b uchar 10
867
868 t_records_hostile
869end
870
871fn t_records_hostile to void
872 note 1. leakage: candidate fit on held-out file B
873 gp_set t_fix_b len t_fix_b
874 chk eq fit_fixture_model 1 1 TY_ROLE_CANDIDATE TY_V0_CANDIDATE_MASK 10 11 TY_OK
875 gp_set t_fix_b len t_fix_b
876 chk eq gain_compute_fix 10 11 1 TY_E_LEAK
877 note hidden leak: claim fit digest A, compute succeeds, refit disagrees with digest 50
878 put g_fr_fdig 12 0
879 gp_set t_fix_b len t_fix_b
880 chk eq gain_compute_fix 10 11 1 TY_OK
881 rc = ty_gain_digest 51
882 chk not dg_eq 51 50
883
884 note restore clean candidate
885 gp_set t_fix_a len t_fix_a
886 rc = fit_fixture_model 1 1 TY_ROLE_CANDIDATE TY_V0_CANDIDATE_MASK 10 11
887
888 note 2. foreign fit file -> TY_E_PROVENANCE
889 dg_cp 25 21
890 put g_dig 800 xor get g_dig 800 uchar 1
891 put g_fr_fdig 12 25
892 gp_set t_fix_b len t_fix_b
893 chk eq gain_compute_fix 10 11 1 TY_E_PROVENANCE
894 put g_fr_fdig 12 21
895
896 note 3. changed floor profile
897 rc = ty_profile_v0_digest 2 2 12
898 put g_fr_prof 1 12
899 gp_set t_fix_b len t_fix_b
900 chk eq gain_compute_fix 10 11 1 TY_E_PROFILE
901 put g_fr_prof 1 10
902 gp_set t_fix_b len t_fix_b
903 chk eq gain_compute_fix 12 11 2 TY_E_PROFILE
904
905 note 4. candidate mutating version/qbits/floor with consistent digest
906 cbo = TY_MCODE_SLOT
907 nc = get g_mclen 1
908 b4 = get g_mcode add cbo 4
909 put g_mcode add cbo 4 uchar 1
910 ty_model_digest 1 nc 31
911 gp_set t_fix_b len t_fix_b
912 chk eq gain_compute_fix 10 11 1 TY_E_PROFILE
913 put g_mcode add cbo 4 b4
914
915 b7 = get g_mcode add cbo 7
916 put g_mcode add cbo 7 uchar 12
917 ty_model_digest 1 nc 31
918 gp_set t_fix_b len t_fix_b
919 chk eq gain_compute_fix 10 11 1 TY_E_PROFILE
920 put g_mcode add cbo 7 b7
921
922 b13 = get g_mcode add cbo 13
923 b14 = get g_mcode add cbo 14
924 put g_mcode add cbo 13 uchar 0
925 put g_mcode add cbo 14 uchar 0
926 ty_model_digest 1 nc 31
927 gp_set t_fix_b len t_fix_b
928 chk eq gain_compute_fix 10 11 1 TY_E_PROFILE
929 put g_mcode add cbo 13 b13
930 put g_mcode add cbo 14 b14
931 ty_model_digest 1 nc 31
932 t_records_hostile2
933end
934
935fn t_records_hostile2 to void
936 cbo = TY_MCODE_SLOT
937 note 5. weaker baseline (order-0)
938 gp_set t_fix_a len t_fix_a
939 rc = fit_fixture_model 0 0 TY_ROLE_BASELINE 0 10 11
940 gp_set t_fix_b len t_fix_b
941 chk eq gain_compute_fix 10 11 1 TY_E_BASELINE
942 gp_set t_fix_a len t_fix_a
943 rc = fit_fixture_model 0 0 TY_ROLE_BASELINE TY_V0_BASELINE_MASK 10 11
944
945 note 6. tampered baseline & candidate code
946 nb = get g_mclen 0
947 put g_mcode sub nb 1 xor get g_mcode sub nb 1 uchar 128
948 gp_set t_fix_b len t_fix_b
949 chk eq gain_compute_fix 10 11 1 TY_E_DIGEST
950 put g_mcode sub nb 1 xor get g_mcode sub nb 1 uchar 128
951
952 put g_mcode add cbo 20 xor get g_mcode add cbo 20 uchar 1
953 gp_set t_fix_b len t_fix_b
954 chk eq gain_compute_fix 10 11 1 TY_E_DIGEST
955 put g_mcode add cbo 20 xor get g_mcode add cbo 20 uchar 1
956
957 note 7. tampered held-out dataset
958 t_records_hostile3
959end
960
961fn t_records_hostile3 to void
962 make_tmp_path t_suf_tmp
963 okc = copy_file_to_tmp t_fix_b
964 fd = fopen t_fix_b 0 len t_fix_b int 0 int 0
965 fidx = long -1
966 ri = long 0
967 while lt ri 2000 bound 2000
968 k = read int fd g_io_buf 0 TY_CTR1_BYTES
969 if ne k long TY_CTR1_BYTES
970 break
971 end
972 if eq get g_io_buf 6 uchar 1
973 if eq get g_io_buf 23 uchar 0
974 if eq get g_io_buf 7 uchar 2
975 fidx = ri
976 break
977 end
978 end
979 end
980 ri = add ri 1
981 end
982 dc = fclose int fd
983 if ge fidx 0
984 okp = patch_tmp add mul ulong fidx TY_CTR1_BYTES 7 uchar 4
985 end
986 st_reset_all
987 st_begin 0
988 chk and ge fidx 0 ge ty_ctr1_read_gp 0 0
989 chk eq gain_compute_fix 10 11 1 TY_E_DIGEST
990 ru = unlink g_path_tmp 0 get g_path_len 0
991
992 note 8. tampered receipt (edited T, forged PASS)
993 gp_set t_fix_b len t_fix_b
994 rc = gain_compute_fix 10 11 1
995 orig_t = get g_gr_i 4
996 put g_gr_i 4 add orig_t 1000000
997 rc = ty_gain_digest 52
998 chk not dg_eq 52 50
999 put g_gr_i 4 orig_t
1000 put g_gr_pass 0 1
1001 rc = ty_gain_digest 53
1002 chk not dg_eq 53 50
1003end
1004
1005fn main to int
1006 ac = argcnt
1007 if gt ac 1
1008 k = argread 1 g_pfx 0 512
1009 if gt k 0
1010 put g_pfx_len 0 ulong k
1011 end
1012 else
1013 n = len t_def_pfx
1014 for i ulong n bound 512
1015 put g_pfx i get t_def_pfx i
1016 end
1017 put g_pfx_len 0 n
1018 end
1019 t_math
1020 t_reader
1021 t_model_code
1022 t_memorizer
1023 t_train_only
1024 t_records
1025
1026 make_tmp_path t_suf_det
1027 fd = fopen g_path_tmp 0 get g_path_len 0 int 577 int 420
1028 if ge fd 0
1029 w = write int fd g_det_buf 0 get g_det_len 0
1030 dc = fclose int fd
1031 end
1032
1033 out_str t_s_pass
1034 out_u64 get g_pass 0
1035 out_str t_s_p2
1036 out_u64 get g_fail 0
1037 out_str t_s_f2
1038 if ne get g_fail 0 0
1039 ret 1
1040 end
1041 ret 0
1042end