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 / protocol / uor-gpu / emit_kernels.om

612 lines · 16678 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note protocol/uor-gpu/emit_kernels.om: Pure seeded OM-2 single-source GPU kernel compiler,
3note QMD v5 emitter driver, real source/ML-BOM hashing, and local trust-anchor builder.
4note Compiles SM121 payloads directly from om/gpu/bw_vecadd.om + om/gpu/bw_qmd.om and SPIR-V
5note payloads directly from gpu/spirv/emit.om using the SHA-256-verified seed/out/omc binary.
6note Zero duplicated SASS/QMD/SPIR-V fixtures. Zero CUDApp, zero C/Python/shell.
7note Compose after canonical.om, payload_vm.om, verify.om, gate.om.
8
9text ug_omc_hex "708fdc0f81866594ed6df2f492b7a8a6bc7456f2d733a838b7f840549ebba6c3"
10text ug_lean_hex "4242bcf69741fc3064f67083694e0e09f969eb4cc9ed4369d96154dc02a7238a"
11text ug_vecadd_sass_hex "39f3dfdfc529a75a8274a27900acf4e7382b7f280a211e0412c51609f44b62a1"
12text ug_default_omc "seed/out/omc"
13text ug_src_vecadd "om/gpu/bw_vecadd.om"
14text ug_src_qmd "om/gpu/bw_qmd.om"
15text ug_src_spirv "gpu/spirv/emit.om"
16text ug_tmp_pfx "/tmp/ug-"
17text ug_suf_om ".om"
18text ug_suf_bin ".bin"
19text ug_suf_out ".out"
20text ug_suf_err ".err"
21text ug_dev_null "/dev/null"
22text ug_line_om2 "om2\n"
23text ug_kw_export "export "
24text ug_arg_vadd "vadd"
25text ug_arg_cl "cl"
26text ug_arg_mut "mutant"
27text ug_mb_sm121 "mlbom:v2:sm121:u32:8:"
28text ug_mb_spirv "mlbom:v2:spirv:f32:8:"
29
30note Pure-Omega main driver appended to stripped om/gpu/bw_vecadd.om + om/gpu/bw_qmd.om.
31note Calls bw_encode_vecadd (512B SASS) + bw_qmd1 + bw_qmd_verify (384B QMD v5) -> 896B stdout.
32text ug_sm121_drv "fn ug_drv_put32 b edit uchar 384 o ulong v uint to void\nput b o uchar and v 255\nput b add o 1 uchar and shr v 8 255\nput b add o 2 uchar and shr v 16 255\nput b add o 3 uchar and shr v 24 255\nend\nfn main to int\ncode = new uchar 512 0\nm = new ulong 16 0\nq1 = new uint 96 0\nqb = new uchar 384 0\nn = bw_encode_vecadd code\nif ne n 512\nret 1\nend\nput m 0 0x100000000\nput m 1 0x100010000\nput m 2 0x100020000\nput m 3 0x100030000\nput m 4 0x100040000\nput m 5 256\nput m 6 256\nput m 7 1\nput m 8 256\nput m 9 1\nput m 10 1\nput m 11 1\nput m 12 16\nput m 13 0\nif ne bw_qmd1 m q1 0\nret 2\nend\nif ne bw_qmd_verify q1 0\nret 3\nend\nfor i ulong 96\nug_drv_put32 qb mul i 4 get q1 i\nend\nw1 = write 1 code 0 512\nw2 = write 1 qb 0 384\nif or ne w1 512 ne w2 384\nret 4\nend\nret 0\nend\n"
33
34const UG_O_RDONLY int 0
35const UG_O_WR_EXCL int 705
36const UG_MODE_600 int 384
37const UG_MODE_700 int 448
38
39fn ug_hex_nib c uchar to ulong
40 v = ulong c
41 if and ge v 48 le v 57
42 ret sub v 48
43 end
44 ret add 10 sub v 97
45end
46
47fn ug_parse_hex32 hx view uchar 64 dst edit uchar 32 to void
48 for i ulong 32
49 hi = ug_hex_nib get hx mul i 2
50 lo = ug_hex_nib get hx add mul i 2 1
51 put dst i uchar or shl hi 4 lo
52 end
53end
54
55fn ug_parse_hex64 hx view uchar 128 dst edit uchar 64 to void
56 for i ulong 64
57 hi = ug_hex_nib get hx mul i 2
58 lo = ug_hex_nib get hx add mul i 2 1
59 put dst i uchar or shl hi 4 lo
60 end
61end
62
63note Streaming SHA-256 of any file into an initialized sha256 context. Returns 1 on success, 0 on error.
64fn ug_sha256_file_into ctx edit ulong 40 path view uchar * pn ulong to ulong
65 fd = fopen path 0 pn UG_O_RDONLY 0
66 if lt fd 0
67 ret 0
68 end
69 buf = new uchar 4096 0
70 ok = ulong 1
71 while eq ok 1 bound 65536
72 k = read int fd buf 0 4096
73 if lt k 0
74 ok = ulong 0
75 break
76 end
77 if eq k 0
78 break
79 end
80 sha256_update ctx part buf 0 ulong k
81 end
82 cl = fclose int fd
83 ret ok
84end
85
86note Verify that omc_path's streaming SHA-256 matches ug_omc_hex. Returns 1 iff exact match.
87fn ug_check_omc_pin omc_path view uchar * pn ulong to ulong
88 ctx = new ulong 40 0
89 dig = new uchar 32 0
90 pin = new uchar 32 0
91 sha256_init ctx
92 if eq ug_sha256_file_into ctx omc_path pn 0
93 ret 0
94 end
95 sha256_final ctx dig
96 ug_parse_hex32 ug_omc_hex pin
97 ret uor_eq32 dig 0 pin 0
98end
99
100note Copy bytes into dst[off..off+n); returns new off.
101fn ug_pcat dst edit uchar 128 off ulong src view uchar * n ulong to ulong
102 for i ulong n bound 128
103 put dst add off i get src i
104 end
105 ret add off n
106end
107
108note Build unique /tmp/ug-<16hex><suf> path; returns path length.
109fn ug_mkpath base view uchar 24 suf view uchar * dst edit uchar 128 to ulong
110 o = ug_pcat dst 0 base 24
111 ret ug_pcat dst o suf len suf
112end
113
114note Generate 24-byte unique prefix "/tmp/ug-<16 random hex chars>". Returns 1 on success.
115fn ug_rand_prefix base edit uchar 24 to ulong
116 rb = new uchar 8 0
117 if ne random rb 0 8 0 8
118 ret 0
119 end
120 for i ulong 8
121 put base i get ug_tmp_pfx i
122 end
123 for i ulong 8
124 b = ulong get rb i
125 put base add 8 mul i 2 get uor_hex_tbl shr b 4
126 put base add 9 mul i 2 get uor_hex_tbl and b 15
127 end
128 ret 1
129end
130
131note Append one arg to packed execprog argv packet av[0..512); pos[0]=bytes, pos[1]=argc.
132fn ug_av_add av edit uchar 512 pos edit ulong 2 s view uchar * n ulong to void
133 p = get pos 0
134 put av p uchar and n 255
135 put av add p 1 uchar and shr n 8 255
136 for i ulong n bound 256
137 put av add add p 2 i get s i
138 end
139 put pos 0 add p add n 2
140 c = add get pos 1 1
141 put pos 1 c
142 put av 0 uchar and c 255
143 put av 1 uchar and shr c 8 255
144end
145
146fn ug_close_all fi int fo int fe int to void
147 if ge fi 0
148 c1 = fclose fi
149 end
150 if ge fo 0
151 c2 = fclose fo
152 end
153 if ge fe 0
154 c3 = fclose fe
155 end
156end
157
158note Spawn execprog(av[0..avlen)) with stdin=in_fd, stdout=out_fd, stderr=err_fd (closes fds in parent).
159note Returns 1 iff child exited cleanly with status 0.
160fn ug_spawn av view uchar 512 avlen ulong fi int fo int fe int to ulong
161 stb = new uchar 4 0
162 if lt fi 0
163 ug_close_all fi fo fe
164 ret 0
165 end
166 if lt fo 0
167 ug_close_all fi fo fe
168 ret 0
169 end
170 if lt fe 0
171 ug_close_all fi fo fe
172 ret 0
173 end
174 pid = fork
175 if lt pid 0
176 ug_close_all fi fo fe
177 ret 0
178 end
179 if eq pid 0
180 if ne dupfd fi 0 0 0
181 exit 127
182 end
183 if ne dupfd fo 1 0 1
184 exit 127
185 end
186 if ne dupfd fe 2 0 2
187 exit 127
188 end
189 er = execprog av 0 avlen
190 exit 127
191 end
192 ug_close_all fi fo fe
193 r = waitpid int pid stb 0 4 0
194 if ne r pid
195 ret 0
196 end
197 if ne get stb 0 uchar 0
198 ret 0
199 end
200 if ne get stb 1 uchar 0
201 ret 0
202 end
203 ret 1
204end
205
206note Returns 1 iff file at path[0..pn) exists and has byte length 0.
207fn ug_file_empty path view uchar * pn ulong to ulong
208 fd = fopen path 0 pn UG_O_RDONLY 0
209 if lt fd 0
210 ret 0
211 end
212 b = new uchar 1 0
213 k = read int fd b 0 1
214 cl = fclose int fd
215 if eq k 0
216 ret 1
217 end
218 ret 0
219end
220
221note Read whole file into dst (at most len dst bytes); returns byte count or -1 on error/overflow.
222fn ug_read_all path view uchar * pn ulong dst edit uchar * to long
223 fd = fopen path 0 pn UG_O_RDONLY 0
224 if lt fd 0
225 ret -1
226 end
227 cap = len dst
228 got = ulong 0
229 while lt got cap bound 65536
230 k = read int fd dst got sub cap got
231 if lt k 0
232 cl = fclose int fd
233 ret -1
234 end
235 if eq k 0
236 break
237 end
238 got = add got ulong k
239 end
240 ex = new uchar 1 0
241 ek = read int fd ex 0 1
242 cl = fclose int fd
243 if gt ek 0
244 ret -1
245 end
246 ret long got
247end
248
249note True iff line[0..ln) is "om2" (with optional trailing LF/CR).
250fn ug_is_om2_line line view uchar 1024 ln ulong to ulong
251 n = ln
252 if gt n 0
253 if eq get line sub n 1 uchar 10
254 n = sub n 1
255 end
256 end
257 if gt n 0
258 if eq get line sub n 1 uchar 13
259 n = sub n 1
260 end
261 end
262 if ne n 3
263 ret 0
264 end
265 if ne get line 0 uchar 111
266 ret 0
267 end
268 if ne get line 1 uchar 109
269 ret 0
270 end
271 if ne get line 2 uchar 50
272 ret 0
273 end
274 ret 1
275end
276
277note True iff line[0..ln) starts with "export ".
278fn ug_starts_export line view uchar 1024 ln ulong to ulong
279 if lt ln 7
280 ret 0
281 end
282 for i ulong 7
283 if ne get line i get ug_kw_export i
284 ret 0
285 end
286 end
287 ret 1
288end
289
290note Write one filtered line to fd (skipping bare "om2" lines and stripping leading "export ").
291fn ug_emit_line fd int line view uchar 1024 ln ulong to ulong
292 if eq ln 0
293 ret 1
294 end
295 if ne ug_is_om2_line line ln 0
296 ret 1
297 end
298 st = ulong 0
299 if ne ug_starts_export line ln 0
300 st = ulong 7
301 end
302 remn = sub ln st
303 w = write fd line st remn
304 if ne w long remn
305 ret 0
306 end
307 ret 1
308end
309
310fn ug_feed_lines buf view uchar 4096 ku ulong out_fd int line edit uchar 1024 st edit ulong 2 to void
311 for i ulong ku bound 4096
312 ch = get buf i
313 ln = get st 0
314 if ge ln 1024
315 put st 1 0
316 ret
317 end
318 put line ln ch
319 ln = add ln 1
320 if eq ch uchar 10
321 if eq ug_emit_line out_fd line ln 0
322 put st 1 0
323 ret
324 end
325 ln = ulong 0
326 end
327 put st 0 ln
328 end
329end
330
331note Stream src file: hash exact raw bytes into sha_ctx, and emit filtered OM-2 lines into out_fd.
332fn ug_stream_source src_path view uchar * out_fd int ctx edit ulong 40 to ulong
333 buf = new uchar 4096 0
334 line = new uchar 1024 0
335 nl = new uchar 1 10
336 st = new ulong 2 0
337 fd = fopen src_path 0 len src_path UG_O_RDONLY 0
338 if lt fd 0
339 ret 0
340 end
341 put st 1 1
342 while eq get st 1 1 bound 65536
343 k = read int fd buf 0 4096
344 if lt k 0
345 put st 1 0
346 break
347 end
348 if eq k 0
349 break
350 end
351 sha256_update ctx part buf 0 ulong k
352 ug_feed_lines buf ulong k out_fd line st
353 end
354 cl = fclose int fd
355 if eq get st 1 0
356 ret 0
357 end
358 if gt get st 0 0
359 if eq ug_emit_line out_fd line get st 0 0
360 ret 0
361 end
362 if ne write out_fd nl 0 1 1
363 ret 0
364 end
365 end
366 ret 1
367end
368
369note Compile composed OM-2 file in_path using verified omc_path -> executable <base>.bin (stderr <base>.err).
370fn ug_run_omc omc_path view uchar * in_path view uchar * base view uchar 24 to ulong
371 av = new uchar 512 0
372 pos = new ulong 2 0
373 pbin = new uchar 128 0
374 perr = new uchar 128 0
375 put pos 0 2
376 put pos 1 0
377 ug_av_add av pos omc_path len omc_path
378 lbin = ug_mkpath base ug_suf_bin pbin
379 lerr = ug_mkpath base ug_suf_err perr
380 fi = fopen in_path 0 len in_path UG_O_RDONLY 0
381 fo = fopen pbin 0 lbin UG_O_WR_EXCL UG_MODE_700
382 fe = fopen perr 0 lerr UG_O_WR_EXCL UG_MODE_600
383 if eq ug_spawn av get pos 0 int fi int fo int fe 0
384 ret 0
385 end
386 ret ug_file_empty perr lerr
387end
388
389note Write composed SM121 emitter source into <base>.om and compute real source SHA-256 into src_dig32.
390fn ug_write_sm121_om base view uchar 24 src_dig32 edit uchar 32 to ulong
391 pom = new uchar 128 0
392 ctx = new ulong 40 0
393 lom = ug_mkpath base ug_suf_om pom
394 fd = fopen pom 0 lom UG_O_WR_EXCL UG_MODE_600
395 if lt fd 0
396 ret 0
397 end
398 sha256_init ctx
399 ok = ulong 1
400 if ne write int fd ug_line_om2 0 len ug_line_om2 long len ug_line_om2
401 ok = ulong 0
402 end
403 if eq ok 1
404 if eq ug_stream_source ug_src_vecadd int fd ctx 0
405 ok = ulong 0
406 end
407 end
408 if eq ok 1
409 if eq ug_stream_source ug_src_qmd int fd ctx 0
410 ok = ulong 0
411 end
412 end
413 if eq ok 1
414 if ne write int fd ug_sm121_drv 0 len ug_sm121_drv long len ug_sm121_drv
415 ok = ulong 0
416 end
417 end
418 cl = fclose int fd
419 sha256_final ctx src_dig32
420 ret ok
421end
422
423note Run <base>.bin with packed argv (av[0..avlen)), capture stdout into <base>.out -> pay_out.
424note Returns byte count >= 0, or -1 on error/non-empty stderr.
425fn ug_exec_capture base view uchar 24 av view uchar 512 avlen ulong pay_out edit uchar 65536 to long
426 pout = new uchar 128 0
427 perr = new uchar 128 0
428 lout = ug_mkpath base ug_suf_out pout
429 lerr = ug_mkpath base ug_suf_err perr
430 u = unlink perr 0 lerr
431 fi = fopen ug_dev_null 0 len ug_dev_null UG_O_RDONLY 0
432 fo = fopen pout 0 lout UG_O_WR_EXCL UG_MODE_600
433 fe = fopen perr 0 lerr UG_O_WR_EXCL UG_MODE_600
434 ok = ug_spawn av avlen int fi int fo int fe
435 if eq ok 1
436 ok = ug_file_empty perr lerr
437 end
438 nb = long -1
439 if eq ok 1
440 nb = ug_read_all pout lout pay_out
441 end
442 u = unlink pout 0 lout
443 u = unlink perr 0 lerr
444 ret nb
445end
446
447fn ug_cleanup_base base view uchar 24 to void
448 p = new uchar 128 0
449 l = ug_mkpath base ug_suf_om p
450 u = unlink p 0 l
451 l = ug_mkpath base ug_suf_bin p
452 u = unlink p 0 l
453 l = ug_mkpath base ug_suf_out p
454 u = unlink p 0 l
455 l = ug_mkpath base ug_suf_err p
456 u = unlink p 0 l
457end
458
459note Compile SM121 payload directly from om/gpu/bw_vecadd.om + om/gpu/bw_qmd.om with verified omc.
460note Writes exact 896B (512B SASS || 384B full QMD v5) into pay_out and real source SHA-256 into src_dig32.
461fn ug_compile_sm121 omc_path view uchar * pay_out edit uchar 65536 out_plen edit ulong 1 src_dig32 edit uchar 32 to ulong
462 base = new uchar 24 0
463 p = new uchar 128 0
464 av = new uchar 512 0
465 pos = new ulong 2 0
466 if eq ug_check_omc_pin omc_path len omc_path 0
467 ret 0
468 end
469 if eq ug_rand_prefix base 0
470 ret 0
471 end
472 ok = ug_write_sm121_om base src_dig32
473 if eq ok 1
474 lom = ug_mkpath base ug_suf_om p
475 ok = ug_run_omc omc_path part p 0 lom base
476 end
477 if eq ok 1
478 lbin = ug_mkpath base ug_suf_bin p
479 put pos 0 2
480 put pos 1 0
481 ug_av_add av pos p lbin
482 nb = ug_exec_capture base av get pos 0 pay_out
483 if ne nb long UOR_SM121_PAYLOAD_BYTES
484 ok = ulong 0
485 else
486 put out_plen 0 UOR_SM121_PAYLOAD_BYTES
487 end
488 end
489 ug_cleanup_base base
490 ret ok
491end
492
493fn ug_hash_file path view uchar * dst32 edit uchar 32 to ulong
494 ctx = new ulong 40 0
495 sha256_init ctx
496 if eq ug_sha256_file_into ctx path len path 0
497 ret 0
498 end
499 sha256_final ctx dst32
500 ret 1
501end
502
503note Compile SPIR-V 1.0 OpenCL Kernel payload directly from gpu/spirv/emit.om with verified omc.
504note is_mutant == 0: `vadd cl` (OpFAdd); is_mutant == 1: `vadd cl mutant` (OpFSub, for negative control).
505fn ug_compile_spirv omc_path view uchar * is_mutant ulong pay_out edit uchar 65536 out_plen edit ulong 1 src_dig32 edit uchar 32 to ulong
506 base = new uchar 24 0
507 pbin = new uchar 128 0
508 av = new uchar 512 0
509 pos = new ulong 2 0
510 if eq ug_check_omc_pin omc_path len omc_path 0
511 ret 0
512 end
513 if eq ug_hash_file ug_src_spirv src_dig32 0
514 ret 0
515 end
516 if eq ug_rand_prefix base 0
517 ret 0
518 end
519 ok = ug_run_omc omc_path ug_src_spirv base
520 if eq ok 1
521 lbin = ug_mkpath base ug_suf_bin pbin
522 put pos 0 2
523 put pos 1 0
524 ug_av_add av pos pbin lbin
525 ug_av_add av pos ug_arg_vadd len ug_arg_vadd
526 ug_av_add av pos ug_arg_cl len ug_arg_cl
527 if ne is_mutant 0
528 ug_av_add av pos ug_arg_mut len ug_arg_mut
529 end
530 nb = ug_exec_capture base av get pos 0 pay_out
531 if le nb 20
532 ok = ulong 0
533 end
534 if gt nb 65536
535 ok = ulong 0
536 end
537 if eq ok 1
538 put out_plen 0 ulong nb
539 end
540 end
541 ug_cleanup_base base
542 ret ok
543end
544
545note Compute real ML-BOM digest over structured manifest tag || golden input slab A[0..8) || B[0..8).
546fn ug_mlbom_digest isa ulong dst32 edit uchar 32 to void
547 ctx = new ulong 40 0
548 slab = new uchar 64 0
549 sha256_init ctx
550 if eq isa UOR_GPU_ISA_SM121
551 sha256_update ctx ug_mb_sm121
552 for i ulong UOR_REPLAY_N
553 uor_put_u32 slab mul i 4 add mul i 3 10
554 uor_put_u32 slab add 32 mul i 4 add mul i 5 20
555 end
556 else
557 sha256_update ctx ug_mb_spirv
558 for i ulong UOR_REPLAY_N
559 uor_put_u32 slab mul i 4 ulong bitcast uint f32 add mul i 3 10
560 uor_put_u32 slab add 32 mul i 4 ulong bitcast uint f32 add mul i 5 20
561 end
562 end
563 sha256_update ctx slab
564 sha256_final ctx dst32
565end
566
567note Populate P3 header using real source SHA-256, pinned omc/Lean digests, real ML-BOM slab digest,
568note and real kernel replay executed directly over pay[0..len pay). Returns UOR_VERDICT_PASS or error.
569fn ug_fill_p3_hdr_real hdr edit uchar 384 isa ulong pay view uchar * src_dig view uchar 32 to ulong
570 omc_pin = new uchar 32 0
571 lean_dig = new uchar 32 0
572 rep_dig = new uchar 32 0
573 mb_dig = new uchar 32 0
574 meta = new ulong 5 0
575 for i ulong 384
576 put hdr i uchar 0
577 end
578 ug_parse_hex32 ug_omc_hex omc_pin
579 ug_parse_hex32 ug_lean_hex lean_dig
580 ug_mlbom_digest isa mb_dig
581 put meta 0 isa
582 put meta 1 UOR_GPU_MATH_RN
583 put meta 2 UOR_LVL_P3
584 put meta 3 len pay
585 put meta 4 256
586 uor_hdr_write_meta hdr meta
587 rc = uvm_replay_payload hdr pay rep_dig
588 if ne rc UOR_VERDICT_PASS
589 ret rc
590 end
591 uor_copy32 hdr 32 omc_pin 0
592 uor_copy32 hdr 64 src_dig 0
593 uor_copy32 hdr 128 lean_dig 0
594 uor_copy32 hdr 160 rep_dig 0
595 uor_copy32 hdr 192 mb_dig 0
596 ret UOR_VERDICT_PASS
597end
598
599note Build 136-byte local trust anchors (one signer) with this build's omc and Lean proof pins:
600note magic | omc pin | lean pin | authority key apk | signer key spk. More signers can be appended.
601fn ug_build_anchors anc edit uchar 136 apk view uchar 32 spk view uchar 32 to void
602 pin = new uchar 32 0
603 for i ulong 8
604 put anc i get uor_anch_magic i
605 end
606 ug_parse_hex32 ug_omc_hex pin
607 uor_copy32 anc UOR_ANCH_OMC pin 0
608 ug_parse_hex32 ug_lean_hex pin
609 uor_copy32 anc UOR_ANCH_LEAN pin 0
610 uor_copy32 anc UOR_ANCH_AUTH apk 0
611 uor_copy32 anc UOR_ANCH_KEYS spk 0
612end