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 / core / rsi / om / governance / receipt_v2.om

682 lines · 16390 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note core/rsi/om/governance/receipt_v2.om
3note Pure seeded Omega (OM-1) implementation of spark-rsi-mv Version 2 signed evaluation
4note receipt digest (docs/RECEIPT-V2.md), holdout-set v1 digest, ECDSA P-256 verification
5note (via core/rsi/om/p256/p256.om), and fail-closed promotion gate (src/promotion_gate.rs).
6note Compose after: core/rsi/om/p256/sha256.om core/rsi/om/p256/p256.om
7note Library unit: no main. Uses decimal literals (OM-1 compatible with p256.om).
8
9note Promotion gate verdicts (0 = PROMOTE; nonzero = fail-closed refusal)
10const RV2_OK ulong 0
11const RV2_ERR_DISK_HASH_MISMATCH ulong 1
12const RV2_ERR_BAD_JUDGE_KEY ulong 2
13const RV2_ERR_SAME_KEY ulong 3
14const RV2_ERR_NOT_V2 ulong 4
15const RV2_ERR_BAD_DIGEST ulong 5
16const RV2_ERR_BAD_SIGNATURE ulong 6
17const RV2_ERR_SUBJECT_HASH ulong 7
18const RV2_ERR_POLICY_HASH ulong 8
19const RV2_ERR_HOLDOUT_HASH ulong 9
20const RV2_ERR_NOT_ADMITTED ulong 10
21const RV2_ERR_PARENT_MISMATCH ulong 11
22const RV2_ERR_SUBJECT_PATH ulong 12
23const RV2_ERR_PROTECTED_PATH ulong 13
24const RV2_ERR_NOT_ALLOWED_TARGET ulong 14
25const RV2_ERR_HOLDOUT_THRESHOLD ulong 15
26const RV2_ERR_BAD_POLICY ulong 16
27
28note Domain prefixes (docs/RECEIPT-V2.md)
29text rv2_dom_receipt "spark-rsi.evaluation-receipt.v2"
30text rv2_dom_holdout "spark-rsi.holdout-set.v1"
31text rv2_hex_tbl "0123456789abcdef"
32
33note Global V2 receipt state (one active receipt under verification)
34note rv2_meta:
35note [0]=format_version, [1]=holdouts_total, [2]=holdouts_passed,
36note [3]=admitted(0/1), [4]=hard_inv(0/1), [5]=stat_gates(0/1),
37note [6]=layer_cnt (0..8), [7]=has_metrics(0/1), [8]=cand_res_mb,
38note [9]=cycle_len, [10]=cand_len, [11]=parent_len, [12]=eval_at_len, [13]=eval_ver_len,
39note [14]=subj_path_len
40global rv2_meta ulong 16
41global rv2_cycle uchar 64
42global rv2_cand uchar 64
43global rv2_parent uchar 64
44global rv2_eval_at uchar 64
45global rv2_eval_ver uchar 64
46global rv2_eval_bin_hex uchar 64
47global rv2_subj_path uchar 128
48global rv2_subj_hex uchar 64
49global rv2_holdout_hex uchar 64
50global rv2_policy_hex uchar 64
51global rv2_claimed_dig uchar 32
52global rv2_sig uchar 64
53
54note Layer records (up to 8 layers)
55global rv2_lr_name uchar 512
56global rv2_lr_nlen ulong 8
57global rv2_lr_hard ulong 8
58global rv2_lr_pass ulong 8
59global rv2_lr_score_bits ulong 8
60global rv2_lr_sum uchar 1024
61global rv2_lr_slen ulong 8
62
63note Metrics summary (5 f64 IEEE-754 bit patterns as u64)
64global rv2_metrics_bits ulong 5
65
66note Pinned policy and promotion subject state
67note rv2_pol_meta:
68note [0]=require_admitted(0/1), [1]=min_ratio_bps(1..10000),
69note [2]=allowed_cnt(0..8), [3]=protected_cnt(0..8),
70note [4]=sub_parent_len, [5]=sub_path_len
71global rv2_pol_meta ulong 8
72global rv2_pol_sha_hex uchar 64
73global rv2_pol_holdout_hex uchar 64
74global rv2_pol_allowed uchar 1024
75global rv2_pol_alen ulong 8
76global rv2_pol_prot uchar 1024
77global rv2_pol_plen ulong 8
78global rv2_judge_pub uchar 65
79global rv2_own_pub uchar 65
80global rv2_sub_parent uchar 64
81global rv2_sub_path uchar 128
82global rv2_sub_content_hex uchar 64
83global rv2_sub_disk_hex uchar 64
84
85note Computed digest output
86global rv2_comp_dig uchar 32
87global rv2_comp_hex uchar 64
88
89note Reset all V2 receipt, policy, and subject state (fail-closed defaults).
90fn rv2_reset to void
91for i ulong 16
92put rv2_meta i 0
93end
94for i ulong 8
95put rv2_pol_meta i 0
96put rv2_lr_nlen i 0
97put rv2_lr_hard i 0
98put rv2_lr_pass i 0
99put rv2_lr_score_bits i 0
100put rv2_lr_slen i 0
101put rv2_pol_alen i 0
102put rv2_pol_plen i 0
103end
104for i ulong 5
105put rv2_metrics_bits i 0
106end
107for i ulong 65
108put rv2_judge_pub i uchar 0
109put rv2_own_pub i uchar 0
110end
111for i ulong 64
112put rv2_sig i uchar 0
113put rv2_comp_hex i uchar 0
114end
115for i ulong 32
116put rv2_claimed_dig i uchar 0
117put rv2_comp_dig i uchar 0
118end
119end
120
121note Feed a small buffer buf[0..n) (n <= 4096) into streaming SHA-256 context c.
122fn rv2_sha_feed c edit uchar 104 w edit ulong 64 buf view uchar 4096 n ulong to void
123if gt n 0
124absorb c buf n w
125end
126end
127
128note Feed u64 little-endian length prefix into SHA-256 context.
129fn rv2_sha_u64le c edit uchar 104 w edit ulong 64 v ulong to void
130tmp = new uchar 4096 0
131for i ulong 8
132sh = mul i 8
133put tmp i uchar and shr v sh 255
134end
135absorb c tmp 8 w
136end
137
138note Feed length-prefixed field (u64_le(n) || bytes[0..n)) into SHA-256 context.
139fn rv2_sha_field c edit uchar 104 w edit ulong 64 buf view uchar 4096 n ulong to void
140rv2_sha_u64le c w n
141rv2_sha_feed c w buf n
142end
143
144note Feed length-prefixed 8-byte u64 little-endian field (len=8, 8 bytes).
145fn rv2_sha_field_u64 c edit uchar 104 w edit ulong 64 v ulong to void
146rv2_sha_u64le c w 8
147rv2_sha_u64le c w v
148end
149
150note Feed length-prefixed 1-byte flag field (len=1, 0 or 1).
151fn rv2_sha_field_flag c edit uchar 104 w edit ulong 64 flag ulong to void
152rv2_sha_u64le c w 1
153tmp = new uchar 4096 0
154if ne flag 0
155put tmp 0 uchar 1
156end
157absorb c tmp 1 w
158end
159
160note Feed domain text rv2_dom_receipt.
161fn rv2_sha_dom_receipt c edit uchar 104 w edit ulong 64 to void
162n = len rv2_dom_receipt
163tmp = new uchar 4096 0
164for i ulong n bound 64
165put tmp i get rv2_dom_receipt i
166end
167rv2_sha_field c w tmp n
168end
169
170note Feed a 64-byte global string slice of length n.
171fn rv2_feed_g64 c edit uchar 104 w edit ulong 64 which ulong n ulong to void
172tmp = new uchar 4096 0
173for i ulong n bound 64
174if eq which 0
175put tmp i get rv2_cycle i
176else
177if eq which 1
178put tmp i get rv2_cand i
179else
180if eq which 2
181put tmp i get rv2_parent i
182else
183if eq which 3
184put tmp i get rv2_eval_at i
185else
186if eq which 4
187put tmp i get rv2_eval_ver i
188else
189if eq which 5
190put tmp i get rv2_eval_bin_hex i
191else
192if eq which 6
193put tmp i get rv2_subj_hex i
194else
195if eq which 7
196put tmp i get rv2_holdout_hex i
197else
198put tmp i get rv2_policy_hex i
199end
200end
201end
202end
203end
204end
205end
206end
207end
208rv2_sha_field c w tmp n
209end
210
211note Feed rv2_subj_path[0..n).
212fn rv2_feed_subj_path c edit uchar 104 w edit ulong 64 n ulong to void
213tmp = new uchar 4096 0
214for i ulong n bound 128
215put tmp i get rv2_subj_path i
216end
217rv2_sha_field c w tmp n
218end
219
220note Feed layer idx into SHA-256 context (violations count = 0).
221fn rv2_feed_layer c edit uchar 104 w edit ulong 64 idx ulong to void
222tmp = new uchar 4096 0
223nlen = get rv2_lr_nlen idx
224nbase = mul idx 64
225for i ulong nlen bound 64
226put tmp i get rv2_lr_name add nbase i
227end
228rv2_sha_field c w tmp nlen
229rv2_sha_field_flag c w get rv2_lr_hard idx
230rv2_sha_field_flag c w get rv2_lr_pass idx
231rv2_sha_field_u64 c w get rv2_lr_score_bits idx
232slen = get rv2_lr_slen idx
233sbase = mul idx 128
234for i ulong slen bound 128
235put tmp i get rv2_lr_sum add sbase i
236end
237rv2_sha_field c w tmp slen
238rv2_sha_field_u64 c w 0
239end
240
241note Encode 32 raw bytes from SHA-256 final output packed in w[0..4) (byte k = (w[k/8] >> ((k&7)*8)) & 255)
242note into rv2_comp_dig and rv2_comp_hex (exact match with p256.om p2dig).
243fn rv2_store_digest w view ulong 64 to void
244for i ulong 32
245bv = and shr get w shr i 3 mul and i 7 8 255
246put rv2_comp_dig i uchar bv
247hi = shr bv 4
248lo = and bv 15
249put rv2_comp_hex mul i 2 get rv2_hex_tbl hi
250put rv2_comp_hex add mul i 2 1 get rv2_hex_tbl lo
251end
252end
253
254note Compute exact V2 receipt digest (docs/RECEIPT-V2.md) into rv2_comp_dig / rv2_comp_hex.
255fn rv2_compute_digest to void
256c = new uchar 104 0
257w = new ulong 64 0
258init c
259rv2_sha_dom_receipt c w
260rv2_feed_g64 c w 0 get rv2_meta 9
261rv2_feed_g64 c w 1 get rv2_meta 10
262rv2_feed_g64 c w 2 get rv2_meta 11
263rv2_feed_g64 c w 3 get rv2_meta 12
264rv2_feed_g64 c w 4 get rv2_meta 13
265rv2_feed_g64 c w 5 64
266rv2_feed_subj_path c w get rv2_meta 14
267rv2_feed_g64 c w 6 64
268rv2_feed_g64 c w 7 64
269rv2_feed_g64 c w 8 64
270rv2_sha_field_u64 c w get rv2_meta 1
271rv2_sha_field_u64 c w get rv2_meta 2
272rv2_sha_field_flag c w get rv2_meta 3
273rv2_sha_field_flag c w get rv2_meta 4
274rv2_sha_field_flag c w get rv2_meta 5
275lcnt = get rv2_meta 6
276rv2_sha_field_u64 c w lcnt
277for i ulong lcnt bound 8
278rv2_feed_layer c w i
279end
280if eq get rv2_meta 7 0
281rv2_sha_field_flag c w 0
282else
283rv2_sha_field_flag c w 1
284for m ulong 5
285rv2_sha_field_u64 c w get rv2_metrics_bits m
286end
287rv2_sha_field_u64 c w get rv2_meta 8
288end
289final c w
290rv2_store_digest w
291end
292
293note Unpack 32 SHA-256 bytes packed in w[0..4) into out32.
294fn rv2_unpack_w w view ulong 64 out32 edit uchar 32 to void
295for i ulong 32
296v = get w shr i 3
297sh = mul and i 7 8
298put out32 i uchar and shr v sh 255
299end
300end
301
302note Compute SHA-256 of buf[0..n) (n <= 4096) into out32 (raw 32 bytes).
303fn rv2_sha256_raw buf view uchar 4096 n ulong out32 edit uchar 32 to void
304c = new uchar 104 0
305w = new ulong 64 0
306init c
307rv2_sha_feed c w buf n
308final c w
309rv2_unpack_w w out32
310end
311
312note Compute exact spark-rsi.holdout-set.v1 digest for one sorted holdout file (name, file_sha256_32).
313fn rv2_holdout_digest_1 fname view uchar 64 flen ulong fhash view uchar 32 out32 edit uchar 32 to void
314c = new uchar 104 0
315w = new ulong 64 0
316tmp = new uchar 4096 0
317init c
318dn = len rv2_dom_holdout
319for i ulong dn bound 64
320put tmp i get rv2_dom_holdout i
321end
322rv2_sha_field c w tmp dn
323rv2_sha_field_u64 c w 1
324for i ulong flen bound 64
325put tmp i get fname i
326end
327rv2_sha_field c w tmp flen
328for i ulong 32
329put tmp i get fhash i
330end
331rv2_sha_field c w tmp 32
332final c w
333rv2_unpack_w w out32
334end
335
336note Derive 65-byte uncompressed SEC1 public key from 32-byte P-256 private scalar d.
337fn rv2_p256_pubkey priv32 view uchar 32 pub65 edit uchar 65 to ulong
338pw = new ulong 1024 0
339pb = new uchar 4096 0
340p2init pw
341for i ulong 32
342put pb add PBKEY i get priv32 i
343end
344rc = p2pub pw pb
345if ne rc 0
346ret rc
347end
348for i ulong 65
349put pub65 i get pb add PBPUB i
350end
351ret 0
352end
353
354note Sign the 32-byte raw V2 digest dig32 with 32-byte P-256 private key priv32 into sig64.
355note Matches spark-rsi-mv src/evaluator/mod.rs (signing_key.sign(&digest_bytes)) byte-for-byte.
356fn rv2_p256_sign priv32 view uchar 32 dig32 view uchar 32 sig64 edit uchar 64 to ulong
357pw = new ulong 1024 0
358pc = new uchar 104 0
359pb = new uchar 4096 0
360ps = new ulong 64 0
361p2init pw
362for i ulong 32
363put pb add PBKEY i get priv32 i
364put pb add PBMSG i get dig32 i
365end
366rc = p2sign pw pc pb ps 32
367if ne rc 0
368ret rc
369end
370for i ulong 64
371put sig64 i get pb add PBSIG i
372end
373ret 0
374end
375
376note Verify 64-byte P-256 signature sig64 over 32-byte raw V2 digest dig32 under pub65.
377note Returns 1 if valid, 0 if invalid.
378fn rv2_p256_verify pub65 view uchar 65 dig32 view uchar 32 sig64 view uchar 64 to ulong
379pw = new ulong 1024 0
380pc = new uchar 104 0
381pb = new uchar 4096 0
382ps = new ulong 64 0
383p2init pw
384for i ulong 65
385put pb add PBPUB i get pub65 i
386end
387for i ulong 32
388put pb add PBMSG i get dig32 i
389end
390for i ulong 64
391put pb add PBSIG i get sig64 i
392end
393ret p2vrfy pw pc pb ps 32
394end
395
396note Check uncompressed SEC1 key validity (0x04 prefix and point on NIST P-256 curve).
397fn rv2_key_on_curve pub65 view uchar 65 to bool
398if ne get pub65 0 uchar 4
399ret false
400end
401pw = new ulong 1024 0
402pb = new uchar 4096 0
403p2init pw
404for i ulong 65
405put pb add PBPUB i get pub65 i
406end
407p2ld pw PX pb add PBPUB 1
408p2ld pw PY pb add PBPUB 33
409ret p2oncv pw
410end
411
412note Validate clean relative path: non-empty, no leading/trailing '/', no '//', no '.' or '..' segment.
413fn rv2_clean_rel_path buf view uchar 128 plen ulong to bool
414if or eq plen 0 gt plen 128
415ret false
416end
417if or eq get buf 0 uchar 47 eq get buf sub plen 1 uchar 47
418ret false
419end
420seg_start = ulong 0
421i = ulong 0
422while le i plen bound 130
423at_sep = false
424if eq i plen
425at_sep = true
426else
427b = get buf i
428if or eq b uchar 0 eq b uchar 92
429ret false
430end
431if eq b uchar 47
432at_sep = true
433end
434end
435if at_sep
436slen = sub i seg_start
437if eq slen 0
438ret false
439end
440if eq slen 1
441if eq get buf seg_start uchar 46
442ret false
443end
444end
445if eq slen 2
446if eq get buf seg_start uchar 46
447if eq get buf add seg_start 1 uchar 46
448ret false
449end
450end
451end
452seg_start = add i 1
453end
454i = add i 1
455end
456ret true
457end
458
459note Check if path[0..plen) equals prefix[0..xlen) or starts with prefix + '/'.
460fn rv2_under_prefix path view uchar 128 plen ulong pref view uchar 128 xlen ulong to bool
461if or eq xlen 0 lt plen xlen
462ret false
463end
464for i ulong xlen bound 128
465if ne get path i get pref i
466ret false
467end
468end
469if eq plen xlen
470ret true
471end
472ret eq get path xlen uchar 47
473end
474
475note Compare two 64-byte global hex digests.
476fn rv2_hex64_eq_g a_which ulong b_which ulong to bool
477for i ulong 64
478va = uchar 0
479vb = uchar 0
480if eq a_which 0
481va = get rv2_sub_content_hex i
482else
483if eq a_which 1
484va = get rv2_subj_hex i
485else
486if eq a_which 2
487va = get rv2_policy_hex i
488else
489va = get rv2_holdout_hex i
490end
491end
492end
493if eq b_which 0
494vb = get rv2_sub_disk_hex i
495else
496if eq b_which 1
497vb = get rv2_sub_content_hex i
498else
499if eq b_which 2
500vb = get rv2_pol_sha_hex i
501else
502vb = get rv2_pol_holdout_hex i
503end
504end
505end
506if ne va vb
507ret false
508end
509end
510ret true
511end
512
513note Check if rv2_subj_path is in rv2_pol_allowed[0..allowed_cnt).
514fn rv2_in_allowed to bool
515acnt = get rv2_pol_meta 2
516slen = get rv2_meta 14
517for k ulong acnt bound 8
518alen = get rv2_pol_alen k
519if eq alen slen
520base = mul k 128
521same = true
522for i ulong slen bound 128
523if ne get rv2_subj_path i get rv2_pol_allowed add base i
524same = false
525break
526end
527end
528if same
529ret true
530end
531end
532end
533ret false
534end
535
536note Check if rv2_subj_path lies under any rv2_pol_prot[0..protected_cnt).
537fn rv2_in_protected to bool
538pcnt = get rv2_pol_meta 3
539slen = get rv2_meta 14
540sp = new uchar 128 0
541for i ulong slen bound 128
542put sp i get rv2_subj_path i
543end
544pr = new uchar 128 0
545for k ulong pcnt bound 8
546xlen = get rv2_pol_plen k
547base = mul k 128
548for j ulong xlen bound 128
549put pr j get rv2_pol_prot add base j
550end
551if rv2_under_prefix sp slen pr xlen
552ret true
553end
554end
555ret false
556end
557
558note Verify judge/own key separation and P-256 signature over rv2_comp_dig.
559fn rv2_check_keys_and_sig to ulong
560jpub = new uchar 65 0
561opub = new uchar 65 0
562dig = new uchar 32 0
563sig = new uchar 64 0
564same_key = true
565for i ulong 65
566jb = get rv2_judge_pub i
567ob = get rv2_own_pub i
568put jpub i jb
569put opub i ob
570if ne jb ob
571same_key = false
572end
573end
574if not rv2_key_on_curve jpub
575ret RV2_ERR_BAD_JUDGE_KEY
576end
577if same_key
578ret RV2_ERR_SAME_KEY
579end
580for i ulong 32
581put dig i get rv2_comp_dig i
582end
583for i ulong 64
584put sig i get rv2_sig i
585end
586if ne rv2_p256_verify jpub dig sig 1
587ret RV2_ERR_BAD_SIGNATURE
588end
589ret RV2_OK
590end
591
592note Fail-closed Version 2 Promotion Gate (exact mirror of spark-rsi-mv/src/promotion_gate.rs).
593fn rv2_check_promotion to ulong
594note 1. Staged disk hash must equal change content hash
595if not rv2_hex64_eq_g 0 0
596ret RV2_ERR_DISK_HASH_MISMATCH
597end
598note 2. Policy invariants: require_admitted=1, min_ratio_bps in 1..10000, allowed_cnt >= 1
599if eq get rv2_pol_meta 0 0
600ret RV2_ERR_BAD_POLICY
601end
602min_bps = get rv2_pol_meta 1
603if or eq min_bps 0 gt min_bps 10000
604ret RV2_ERR_BAD_POLICY
605end
606if eq get rv2_pol_meta 2 0
607ret RV2_ERR_BAD_POLICY
608end
609note 3. Format version must be strictly 2 (v1 never promotes)
610if ne get rv2_meta 0 2
611ret RV2_ERR_NOT_V2
612end
613note 4. Recompute V2 digest and verify equality with claimed digest
614rv2_compute_digest
615for i ulong 32
616if ne get rv2_comp_dig i get rv2_claimed_dig i
617ret RV2_ERR_BAD_DIGEST
618end
619end
620note 5. Verify pinned judge P-256 key validity, judge != own key, and ECDSA P-256 signature
621krc = rv2_check_keys_and_sig
622if ne krc RV2_OK
623ret krc
624end
625note 6. Subject hash, policy hash, and holdout set hash must match pins
626if not rv2_hex64_eq_g 1 1
627ret RV2_ERR_SUBJECT_HASH
628end
629if not rv2_hex64_eq_g 2 2
630ret RV2_ERR_POLICY_HASH
631end
632if not rv2_hex64_eq_g 3 3
633ret RV2_ERR_HOLDOUT_HASH
634end
635note 7. Admission and hard/statistical gate flags must all be true
636if or eq get rv2_meta 3 0 or eq get rv2_meta 4 0 eq get rv2_meta 5 0
637ret RV2_ERR_NOT_ADMITTED
638end
639note 8. Parent id must match subject parent id
640plen = get rv2_meta 11
641if ne plen get rv2_pol_meta 4
642ret RV2_ERR_PARENT_MISMATCH
643end
644for i ulong plen bound 64
645if ne get rv2_parent i get rv2_sub_parent i
646ret RV2_ERR_PARENT_MISMATCH
647end
648end
649note 9. Subject path must be clean relative path and match subject
650slen = get rv2_meta 14
651if ne slen get rv2_pol_meta 5
652ret RV2_ERR_SUBJECT_PATH
653end
654sp = new uchar 128 0
655for i ulong slen bound 128
656b = get rv2_subj_path i
657if ne b get rv2_sub_path i
658ret RV2_ERR_SUBJECT_PATH
659end
660put sp i b
661end
662if not rv2_clean_rel_path sp slen
663ret RV2_ERR_SUBJECT_PATH
664end
665note 10. Protected paths refused; must appear in allowed_targets
666if rv2_in_protected
667ret RV2_ERR_PROTECTED_PATH
668end
669if not rv2_in_allowed
670ret RV2_ERR_NOT_ALLOWED_TARGET
671end
672note 11. Holdout pass ratio check (exact integer: passed * 10000 >= min_ratio_bps * total)
673htot = get rv2_meta 1
674hpas = get rv2_meta 2
675if or eq htot 0 gt hpas htot
676ret RV2_ERR_HOLDOUT_THRESHOLD
677end
678if lt mul hpas 10000 mul min_bps htot
679ret RV2_ERR_HOLDOUT_THRESHOLD
680end
681ret RV2_OK
682end