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 / seed / ng / pack / sem_pack.om

832 lines · 17243 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/pack/sem_pack.om: Compile-on-Arrive Semantic Package (OMPK v1) wire codec,
3note fail-closed SHA-256 verifier, content-addressed package store with DFS cycle detection,
4note and TargetDesc-gated Realization Cache integration.
5note
6note Library unit for program mode (no om2 line, no fn main; prefix pk_ / PK_).
7note Expects seed/ng/sha256.om and seed/ng/target/target_desc.om linked before it.
8note
9note OMPK v1 Binary Wire Layout:
10note [0..3] magic "OMPK" (79, 77, 80, 75)
11note [4..7] version u32 LE (1)
12note [8..39] sem_id (32-byte SHA-256 of canonical OIR payload)
13note [40..71] func_id (32-byte canonical semantic function digest; equals sem_id when unset)
14note [72..119] 6x u64 LE physical silicon contract:
15note [72..79] req_exec
16note [80..87] req_smem
17note [88..95] req_regs
18note [96..103] req_subgroup
19note [104..111] req_mma
20note [112..119] req_feat
21note [120..123] import_count u32 LE (max 16)
22note [124..127] ir_len u32 LE (max 4096)
23note [128 .. 128 + import_count*32) digest-addressed import table (import_sem_ids)
24note [128 + import_count*32 .. end) raw canonical OIR payload bytes (ir_len bytes)
25
26const PK_MAGIC_0 ulong 79
27const PK_MAGIC_1 ulong 77
28const PK_MAGIC_2 ulong 80
29const PK_MAGIC_3 ulong 75
30const PK_VERSION ulong 1
31const PK_VERSION_2 ulong 2
32const PK_HDR_LEN ulong 128
33const PK_V2_HDR_LEN ulong 160
34const PK_V2_EXT_TAG ulong 844516176
35const PK_DIGEST_LEN ulong 32
36const PK_MAX_IMPORTS ulong 16
37const PK_MAX_IR_LEN ulong 4096
38const PK_MAX_PKG_LEN ulong 4768
39const PK_BUF_CAP ulong 8192
40
41const PK_STORE_CAP ulong 32
42const PK_STORE_STRIDE ulong 8192
43const PK_NONE ulong 999999
44
45note Status and refusal codes (1..6 mirror TDK_ERR_EXEC..TDK_ERR_FEAT from target_desc.om)
46const PK_OK ulong 0
47const PK_ERR_MAGIC ulong 10
48const PK_ERR_VERSION ulong 11
49const PK_ERR_LENGTH ulong 12
50const PK_ERR_DIGEST ulong 13
51const PK_ERR_IMPORTS ulong 14
52const PK_ERR_STORE_FULL ulong 15
53const PK_ERR_MISSING_IMPORT ulong 16
54const PK_ERR_CYCLE ulong 17
55const PK_ERR_PROOF ulong 18
56const PK_ERR_CACHE_FULL ulong 19
57const PK_ERR_BAD_TARGET ulong 20
58const PK_ERR_NOT_FOUND ulong 21
59const PK_ERR_CONTRACT ulong 22
60
61note Active OMPK wire buffer and exact byte length
62global pk_buf uchar 8192
63global pk_len ulong 1
64
65note Builder staging state for pk_pack
66global pk_ir_in uchar 4096
67global pk_ir_in_len ulong 1
68global pk_fid_in uchar 32
69global pk_req_in ulong 6
70global pk_imp_in uchar 512
71global pk_imp_cnt ulong 1
72note OMPK v2 contract staging: 0=active, 1=purity, 2=precision, 3=hard_dl, 4=err_bound, 5=deadline_us, 6=power_mw
73global pk_v2_in ulong 8
74
75note Digest query and last-packed / last-stored metadata
76global pk_query_id uchar 32
77global pk_last_sem uchar 32
78global pk_last_fid uchar 32
79global pk_last_slot ulong 1
80
81note Content-addressed in-memory package store (32 slots)
82global pk_store_data uchar 262144
83global pk_store_used ulong 32
84global pk_store_len ulong 32
85global pk_store_sem uchar 1024
86global pk_store_fid uchar 1024
87global pk_store_icnt ulong 32
88global pk_store_imps uchar 16384
89global pk_store_count ulong 1
90
91note 3-color DFS state (0=white, 1=gray, 2=black) and topological order output
92global pk_dfs_state ulong 32
93global pk_topo_order ulong 32
94global pk_topo_len ulong 1
95
96note Compile-on-Arrive realization telemetry:
97note [0]=status, [1]=cache_slot, [2]=cache_hit, [3]=cache_key,
98note [4]=code_ptr, [5]=codegen_count, [6]=dedup_hits, [7]=used_sem_fallback
99global pk_real_out ulong 8
100
101text pk_tag_fn "omega.function.v1:"
102
103note ---- Little-endian wire encoding helpers on pk_buf ----
104
105fn pk_put_u8 off ulong v ulong to void
106put pk_buf off uchar and v 255
107end
108
109fn pk_put_u32 off ulong v ulong to void
110pk_put_u8 off v
111pk_put_u8 add off 1 shr v 8
112pk_put_u8 add off 2 shr v 16
113pk_put_u8 add off 3 shr v 24
114end
115
116fn pk_put_u64 off ulong v ulong to void
117pk_put_u32 off and v 4294967295
118pk_put_u32 add off 4 shr v 32
119end
120
121fn pk_get_u32 off ulong to ulong
122b0 = ulong get pk_buf off
123b1 = shl ulong get pk_buf add off 1 8
124b2 = shl ulong get pk_buf add off 2 16
125b3 = shl ulong get pk_buf add off 3 24
126ret or or b0 b1 or b2 b3
127end
128
129fn pk_get_u64 off ulong to ulong
130lo = pk_get_u32 off
131hi = pk_get_u32 add off 4
132ret or lo shl hi 32
133end
134
135note ---- Reset and builder staging helpers ----
136
137fn pk_clear_func_id to void
138for i ulong 32
139put pk_fid_in i 0
140end
141end
142
143fn pk_ir_reset to void
144put pk_ir_in_len 0 0
145end
146
147fn pk_ir_push ch uchar to ulong
148cur = get pk_ir_in_len 0
149if ge cur PK_MAX_IR_LEN
150ret PK_ERR_LENGTH
151end
152put pk_ir_in cur ch
153put pk_ir_in_len 0 add cur 1
154ret PK_OK
155end
156
157fn pk_set_req reqExec ulong reqSmem ulong reqRegs ulong reqSubg ulong reqMma ulong reqFeat ulong to void
158put pk_req_in 0 reqExec
159put pk_req_in 1 reqSmem
160put pk_req_in 2 reqRegs
161put pk_req_in 3 reqSubg
162put pk_req_in 4 reqMma
163put pk_req_in 5 reqFeat
164end
165
166fn pk_clear_contract to void
167for i ulong 8
168put pk_v2_in i 0
169end
170end
171
172fn pk_v2_contract_ok pur ulong prc ulong hd ulong rsv ulong err ulong dl ulong to bool
173if or gt pur 1 gt prc 2
174ret false
175end
176if or gt hd 1 ne rsv 0
177ret false
178end
179if and ne prc 2 ne err 0
180ret false
181end
182if and eq hd 1 eq dl 0
183ret false
184end
185ret true
186end
187
188fn pk_set_contract pur ulong prc ulong hd ulong err ulong dl ulong pwr ulong to void
189put pk_v2_in 0 1
190put pk_v2_in 1 pur
191put pk_v2_in 2 prc
192put pk_v2_in 3 hd
193put pk_v2_in 4 err
194put pk_v2_in 5 dl
195put pk_v2_in 6 pwr
196end
197
198fn pk_set_func_id_from_sh to void
199for i ulong 32
200put pk_fid_in i get sh_dig i
201end
202end
203
204fn pk_hdr_len_cur to ulong
205if eq pk_get_u32 4 PK_VERSION_2
206ret PK_V2_HDR_LEN
207end
208ret PK_HDR_LEN
209end
210
211fn pk_get_ver to ulong
212ret pk_get_u32 4
213end
214
215fn pk_get_purity to ulong
216if ne pk_get_u32 4 PK_VERSION_2
217ret 0
218end
219ret ulong get pk_buf 128
220end
221
222fn pk_get_precision to ulong
223if ne pk_get_u32 4 PK_VERSION_2
224ret 0
225end
226ret ulong get pk_buf 129
227end
228
229fn pk_get_hard_dl to ulong
230if ne pk_get_u32 4 PK_VERSION_2
231ret 0
232end
233ret ulong get pk_buf 130
234end
235
236fn pk_get_err_bound to ulong
237if ne pk_get_u32 4 PK_VERSION_2
238ret 0
239end
240ret pk_get_u64 136
241end
242
243fn pk_get_deadline_us to ulong
244if ne pk_get_u32 4 PK_VERSION_2
245ret 0
246end
247ret pk_get_u64 144
248end
249
250fn pk_get_power_mw to ulong
251if ne pk_get_u32 4 PK_VERSION_2
252ret 0
253end
254ret pk_get_u64 152
255end
256
257fn pk_reset_builder to void
258put pk_ir_in_len 0 0
259put pk_imp_cnt 0 0
260pk_clear_func_id
261pk_clear_contract
262for i ulong 6
263put pk_req_in i 0
264end
265end
266
267fn pk_store_reset to void
268put pk_store_count 0 0
269put pk_topo_len 0 0
270put pk_last_slot 0 PK_NONE
271for i ulong 32
272put pk_store_used i 0
273put pk_store_len i 0
274put pk_store_icnt i 0
275put pk_dfs_state i 0
276put pk_topo_order i 0
277end
278end
279
280fn pk_reset_all to void
281pk_reset_builder
282pk_store_reset
283put pk_len 0 0
284for i ulong 8
285put pk_real_out i 0
286end
287put pk_real_out 1 PK_NONE
288end
289
290note Compute canonical polynomial FUNCTION_ID via SHA-256("omega.function.v1:" || w || c0 || c1 || c2)
291fn pk_sh_put_u64 off ulong v ulong to void
292for j ulong 8
293s = mul j 8
294put sh_dat add off j uchar and shr v s 255
295end
296end
297
298fn pk_compute_poly_func_id width ulong c0 ulong c1 ulong c2 ulong to void
299tlen = len pk_tag_fn
300for i ulong tlen bound 64
301put sh_dat i get pk_tag_fn i
302end
303pk_sh_put_u64 tlen width
304pk_sh_put_u64 add tlen 8 c0
305pk_sh_put_u64 add tlen 16 c1
306pk_sh_put_u64 add tlen 24 c2
307sh_all add tlen 32
308for i ulong 32
309put pk_fid_in i get sh_dig i
310end
311end
312
313fn pk_add_import_from_query to ulong
314ic = get pk_imp_cnt 0
315if ge ic PK_MAX_IMPORTS
316ret PK_ERR_IMPORTS
317end
318base = mul ic PK_DIGEST_LEN
319for i ulong 32
320put pk_imp_in add base i get pk_query_id i
321end
322put pk_imp_cnt 0 add ic 1
323ret PK_OK
324end
325
326fn pk_add_import_from_store slot ulong to ulong
327if ge slot PK_STORE_CAP
328ret PK_ERR_NOT_FOUND
329end
330if eq get pk_store_used slot 0
331ret PK_ERR_NOT_FOUND
332end
333ic = get pk_imp_cnt 0
334if ge ic PK_MAX_IMPORTS
335ret PK_ERR_IMPORTS
336end
337dstBase = mul ic PK_DIGEST_LEN
338srcBase = mul slot PK_DIGEST_LEN
339for i ulong 32
340put pk_imp_in add dstBase i get pk_store_sem add srcBase i
341end
342put pk_imp_cnt 0 add ic 1
343ret PK_OK
344end
345
346note ---- SHA-256 helpers reusing seed/ng/sha256.om ----
347
348fn pk_sha256_ir_in nbytes ulong to void
349for i ulong nbytes bound 4096
350put sh_dat i get pk_ir_in i
351end
352sh_all nbytes
353end
354
355fn pk_sha256_buf_slice start ulong nbytes ulong to void
356for i ulong nbytes bound 4096
357put sh_dat i get pk_buf add start i
358end
359sh_all nbytes
360end
361
362fn pk_fid_in_is_zero to bool
363acc = ulong 0
364for i ulong 32
365acc = or acc ulong get pk_fid_in i
366end
367ret eq acc 0
368end
369
370note ---- OMPK v1/v2 Packing (pk_pack) ----
371
372fn pk_write_hdr ic ulong irl ulong to void
373pk_put_u8 0 PK_MAGIC_0
374pk_put_u8 1 PK_MAGIC_1
375pk_put_u8 2 PK_MAGIC_2
376pk_put_u8 3 PK_MAGIC_3
377isv2 = ne get pk_v2_in 0 0
378if isv2
379pk_put_u32 4 PK_VERSION_2
380else
381pk_put_u32 4 PK_VERSION
382end
383useSem = pk_fid_in_is_zero
384for i ulong 32
385b = get sh_dig i
386put pk_buf add 8 i b
387put pk_last_sem i b
388if useSem
389put pk_buf add 40 i b
390put pk_last_fid i b
391else
392fb = get pk_fid_in i
393put pk_buf add 40 i fb
394put pk_last_fid i fb
395end
396end
397for k ulong 6
398pk_put_u64 add 72 mul k 8 get pk_req_in k
399end
400pk_put_u32 120 ic
401pk_put_u32 124 irl
402if isv2
403pk_put_u8 128 get pk_v2_in 1
404pk_put_u8 129 get pk_v2_in 2
405pk_put_u8 130 get pk_v2_in 3
406pk_put_u8 131 0
407pk_put_u32 132 PK_V2_EXT_TAG
408pk_put_u64 136 get pk_v2_in 4
409pk_put_u64 144 get pk_v2_in 5
410pk_put_u64 152 get pk_v2_in 6
411end
412end
413
414fn pk_write_body ic ulong irl ulong to void
415hdrLen = PK_HDR_LEN
416if ne get pk_v2_in 0 0
417hdrLen = PK_V2_HDR_LEN
418end
419impBytes = mul ic PK_DIGEST_LEN
420for i ulong impBytes bound 512
421put pk_buf add hdrLen i get pk_imp_in i
422end
423irOff = add hdrLen impBytes
424for i ulong irl bound 4096
425put pk_buf add irOff i get pk_ir_in i
426end
427put pk_len 0 add irOff irl
428end
429
430fn pk_in_contract_ok to bool
431pur = get pk_v2_in 1
432prc = get pk_v2_in 2
433hd = get pk_v2_in 3
434ev = get pk_v2_in 4
435dl = get pk_v2_in 5
436ret pk_v2_contract_ok pur prc hd 0 ev dl
437end
438
439fn pk_buf_contract_ok to bool
440pur = ulong get pk_buf 128
441prc = ulong get pk_buf 129
442hd = ulong get pk_buf 130
443rsv = ulong get pk_buf 131
444ev = pk_get_u64 136
445dl = pk_get_u64 144
446ret pk_v2_contract_ok pur prc hd rsv ev dl
447end
448
449fn pk_pack to ulong
450ic = get pk_imp_cnt 0
451irl = get pk_ir_in_len 0
452if gt ic PK_MAX_IMPORTS
453ret PK_ERR_IMPORTS
454end
455if gt irl PK_MAX_IR_LEN
456ret PK_ERR_LENGTH
457end
458if ne get pk_v2_in 0 0
459if not pk_in_contract_ok
460ret PK_ERR_CONTRACT
461end
462end
463pk_sha256_ir_in irl
464pk_write_hdr ic irl
465pk_write_body ic irl
466ret PK_OK
467end
468
469note ---- Fail-Closed Verifier (pk_verify) ----
470
471fn pk_check_magic to bool
472if ne ulong get pk_buf 0 PK_MAGIC_0
473ret false
474end
475if ne ulong get pk_buf 1 PK_MAGIC_1
476ret false
477end
478if ne ulong get pk_buf 2 PK_MAGIC_2
479ret false
480end
481if ne ulong get pk_buf 3 PK_MAGIC_3
482ret false
483end
484ret true
485end
486
487fn pk_verify_digest to bool
488diff = ulong 0
489for i ulong 32
490if ne get sh_dig i get pk_buf add 8 i
491diff = add diff 1
492end
493end
494ret eq diff 0
495end
496
497fn pk_verify to ulong
498blen = get pk_len 0
499if lt blen PK_HDR_LEN
500ret PK_ERR_LENGTH
501end
502if gt blen PK_BUF_CAP
503ret PK_ERR_LENGTH
504end
505if not pk_check_magic
506ret PK_ERR_MAGIC
507end
508ver = pk_get_u32 4
509hdrLen = PK_HDR_LEN
510if eq ver PK_VERSION_2
511if lt blen PK_V2_HDR_LEN
512ret PK_ERR_VERSION
513end
514if ne pk_get_u32 132 PK_V2_EXT_TAG
515ret PK_ERR_VERSION
516end
517if not pk_buf_contract_ok
518ret PK_ERR_CONTRACT
519end
520hdrLen = PK_V2_HDR_LEN
521else
522if ne ver PK_VERSION
523ret PK_ERR_VERSION
524end
525end
526ic = pk_get_u32 120
527if gt ic PK_MAX_IMPORTS
528ret PK_ERR_IMPORTS
529end
530irl = pk_get_u32 124
531irOff = add hdrLen mul ic PK_DIGEST_LEN
532if ne blen add irOff irl
533ret PK_ERR_LENGTH
534end
535if gt irl PK_MAX_IR_LEN
536ret PK_ERR_LENGTH
537end
538pk_sha256_buf_slice irOff irl
539if not pk_verify_digest
540ret PK_ERR_DIGEST
541end
542ret PK_OK
543end
544
545note ---- Content-Addressed Package Store + DFS Dependency Resolver ----
546
547fn pk_copy_buf_sem_to_query to void
548for i ulong 32
549put pk_query_id i get pk_buf add 8 i
550end
551end
552
553fn pk_copy_store_sem_to_query slot ulong to void
554base = mul slot PK_DIGEST_LEN
555for i ulong 32
556put pk_query_id i get pk_store_sem add base i
557end
558end
559
560fn pk_copy_store_imp_to_query slot ulong impIdx ulong to void
561base = mul add mul slot PK_MAX_IMPORTS impIdx PK_DIGEST_LEN
562for i ulong 32
563put pk_query_id i get pk_store_imps add base i
564end
565end
566
567fn pk_store_match_slot slot ulong to bool
568if eq get pk_store_used slot 0
569ret false
570end
571base = mul slot PK_DIGEST_LEN
572for i ulong 32
573if ne get pk_store_sem add base i get pk_query_id i
574ret false
575end
576end
577ret true
578end
579
580fn pk_store_find_query to ulong
581for s ulong 32
582if pk_store_match_slot s
583ret s
584end
585end
586ret PK_NONE
587end
588
589fn pk_store_has to bool
590ret ne pk_store_find_query PK_NONE
591end
592
593fn pk_store_copy_bytes slot ulong blen ulong to void
594dataBase = mul slot PK_STORE_STRIDE
595for i ulong blen bound 4768
596put pk_store_data add dataBase i get pk_buf i
597end
598put pk_store_len slot blen
599end
600
601fn pk_store_copy_digests slot ulong to void
602digBase = mul slot PK_DIGEST_LEN
603for i ulong 32
604put pk_store_sem add digBase i get pk_buf add 8 i
605put pk_store_fid add digBase i get pk_buf add 40 i
606end
607end
608
609fn pk_store_copy_imps slot ulong to void
610hdrLen = pk_hdr_len_cur
611ic = pk_get_u32 120
612put pk_store_icnt slot ic
613impBytes = mul ic PK_DIGEST_LEN
614impBase = mul slot 512
615for i ulong impBytes bound 512
616put pk_store_imps add impBase i get pk_buf add hdrLen i
617end
618end
619
620fn pk_store_write_slot slot ulong blen ulong to void
621pk_store_copy_bytes slot blen
622pk_store_copy_digests slot
623pk_store_copy_imps slot
624put pk_store_used slot 1
625end
626
627fn pk_store_put to ulong
628vrc = pk_verify
629if ne vrc PK_OK
630ret vrc
631end
632pk_copy_buf_sem_to_query
633ex = pk_store_find_query
634if ne ex PK_NONE
635put pk_last_slot 0 ex
636ret PK_OK
637end
638freeSlot = PK_NONE
639for s ulong 32
640if eq get pk_store_used s 0
641freeSlot = s
642break
643end
644end
645if eq freeSlot PK_NONE
646ret PK_ERR_STORE_FULL
647end
648blen = get pk_len 0
649pk_store_write_slot freeSlot blen
650put pk_store_count 0 add get pk_store_count 0 1
651put pk_last_slot 0 freeSlot
652ret PK_OK
653end
654
655fn pk_store_load slot ulong to ulong
656if ge slot PK_STORE_CAP
657ret PK_ERR_NOT_FOUND
658end
659if eq get pk_store_used slot 0
660ret PK_ERR_NOT_FOUND
661end
662slen = get pk_store_len slot
663if gt slen PK_MAX_PKG_LEN
664ret PK_ERR_LENGTH
665end
666dataBase = mul slot PK_STORE_STRIDE
667for i ulong slen bound 4768
668put pk_buf i get pk_store_data add dataBase i
669end
670put pk_len 0 slen
671ret PK_OK
672end
673
674fn pk_dfs_visit slot ulong to ulong
675st = get pk_dfs_state slot
676if eq st 1
677ret PK_ERR_CYCLE
678end
679if eq st 2
680ret PK_OK
681end
682put pk_dfs_state slot 1
683ic = get pk_store_icnt slot
684for i ulong ic bound 16
685pk_copy_store_imp_to_query slot i
686dep = pk_store_find_query
687if eq dep PK_NONE
688ret PK_ERR_MISSING_IMPORT
689end
690rc = pk_dfs_visit dep
691if ne rc PK_OK
692ret rc
693end
694end
695put pk_dfs_state slot 2
696tlen = get pk_topo_len 0
697if lt tlen PK_STORE_CAP
698put pk_topo_order tlen slot
699put pk_topo_len 0 add tlen 1
700end
701ret PK_OK
702end
703
704fn pk_resolve_deps to ulong
705root = pk_store_find_query
706if eq root PK_NONE
707ret PK_ERR_NOT_FOUND
708end
709for i ulong 32
710put pk_dfs_state i 0
711put pk_topo_order i 0
712end
713put pk_topo_len 0 0
714ret pk_dfs_visit root
715end
716
717note ---- Compile-on-Arrive Realization (TargetDesc Contract + Proof-Gated Cache) ----
718
719fn pk_rotl64 x ulong r ulong to ulong
720ret or shl x r shr x sub 64 r
721end
722
723fn pk_digest_key64 off ulong to ulong
724w0 = pk_get_u64 off
725w1 = pk_rotl64 pk_get_u64 add off 8 17
726w2 = pk_rotl64 pk_get_u64 add off 16 31
727w3 = pk_rotl64 pk_get_u64 add off 24 47
728k = xor xor w0 w1 xor w2 w3
729if eq k 0
730k = ulong 1
731end
732ret k
733end
734
735fn pk_buf_is_zero32 off ulong to bool
736acc = ulong 0
737for i ulong 32
738acc = or acc ulong get pk_buf add off i
739end
740ret eq acc 0
741end
742
743fn pk_select_cache_key to ulong
744if pk_buf_is_zero32 40
745put pk_real_out 7 1
746ret pk_digest_key64 8
747end
748put pk_real_out 7 0
749ret pk_digest_key64 40
750end
751
752fn pk_load_contract_to_td to void
753r0 = pk_get_u64 72
754r1 = pk_get_u64 80
755r2 = pk_get_u64 88
756r3 = pk_get_u64 96
757r4 = pk_get_u64 104
758r5 = pk_get_u64 112
759tdSetReq r0 r1 r2 r3 r4 r5
760end
761
762fn pk_check_contract tid ulong feat ulong to ulong
763pk_load_contract_to_td
764crc = tdCheckKernelContract tid
765if ne crc TDK_OK
766ret crc
767end
768rf = pk_get_u64 112
769if ne and rf feat rf
770ret TDK_ERR_FEAT
771end
772ret TDK_OK
773end
774
775fn pk_make_code_ptr tid ulong cg ulong to ulong
776ret add 65536 add mul tid 4096 mul cg 64
777end
778
779fn pk_install_realization key ulong tid ulong feat ulong shapeSig ulong proofMask ulong to ulong
780cg = add get pk_real_out 5 1
781codePtr = pk_make_code_ptr tid cg
782ins = tdCacheInstall key tid feat shapeSig codePtr proofMask
783if eq ins PK_NONE
784if ne and proofMask 7 7
785put pk_real_out 0 PK_ERR_PROOF
786ret PK_ERR_PROOF
787end
788put pk_real_out 0 PK_ERR_CACHE_FULL
789ret PK_ERR_CACHE_FULL
790end
791put pk_real_out 5 cg
792put pk_real_out 0 PK_OK
793put pk_real_out 1 ins
794put pk_real_out 2 0
795put pk_real_out 4 codePtr
796ret PK_OK
797end
798
799fn pk_realize_on_arrive tid ulong feat ulong shapeSig ulong proofMask ulong to ulong
800put pk_real_out 0 PK_OK
801put pk_real_out 1 PK_NONE
802put pk_real_out 2 0
803put pk_real_out 3 0
804put pk_real_out 4 0
805put pk_real_out 7 0
806if ge tid get tdCount 0
807put pk_real_out 0 PK_ERR_BAD_TARGET
808ret PK_ERR_BAD_TARGET
809end
810rc = pk_verify
811if ne rc PK_OK
812put pk_real_out 0 rc
813ret rc
814end
815rc = pk_check_contract tid feat
816if ne rc TDK_OK
817put pk_real_out 0 rc
818ret rc
819end
820key = pk_select_cache_key
821put pk_real_out 3 key
822hitSlot = tdCacheLookup key tid feat shapeSig
823if ne hitSlot PK_NONE
824put pk_real_out 0 PK_OK
825put pk_real_out 1 hitSlot
826put pk_real_out 2 1
827put pk_real_out 4 get rcPtr hitSlot
828put pk_real_out 6 add get pk_real_out 6 1
829ret PK_OK
830end
831ret pk_install_realization key tid feat shapeSig proofMask
832end