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 / open-humanity / trace_contract.om

1298 lines · 30695 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note protocol/open-humanity/trace_contract.om
3note Pure seeded OM-2 implementation of:
4note 1. Exact-byte Open Humanity UDP wire codec (crates/beacon-core/src/{wire,schema}.rs):
5note - Frames: Signal (0x4f 0x48 0x01), Register (0x4f 0x52 0x01), Challenge (0x4f 0x51 0x01),
6note Ack (0x4f 0x41 0x01), Capsule (0x4f 0x43 0x01), exact bincode 1.3 LE layout
7note - Keyed BLAKE3 relay cookies (16 bytes, 120s window + previous window)
8note - Ed25519 signing and verification over exact signable_bytes
9note - Normalized BLAKE3 error fingerprinting (user home paths scrubbed to ~/, ws collapsed)
10note 2. Exact-byte Consent/Export and Trace-Receiver Contract (OHTX v1 over AITR v1):
11note - Strict AITR v1 trace segment reader/writer (16B header + 64B IEEE CRC-32 records,
12note matching seed/trace/segment.om byte-for-byte)
13note - Signed OHCN v1 consent manifest (160B, Ed25519, opt_in=1, cap_flags=0, allowed_type_mask)
14note - Fail-closed exporter (oh_trace_export) and receiver (oh_trace_receive) enforcing
15note consent, Personal Data Firewall, zero-authority boundary, BLAKE3 integrity, Ed25519
16note authentication, timestamp drift (<= 300s), and replay protection.
17note Compose after:
18note si/directives/om2.om protocol/open-humanity/lib.om
19note protocol/omnet/om/sha512/sha512.om protocol/omnet/om/ed25519/ed25519.om
20
21note ---- Self-contained RFC 8032 Ed25519 key derivation & signing (reusing ed25519.om + sha512.om) ----
22
23fn ohtx_ed_red x64 view uchar 64 dst edit uchar 32 to void
24x = new long 64 0
25for i ulong 64
26put x i long get x64 i
27end
28edmodl x
29for i ulong 32
30put dst i uchar get x i
31end
32end
33
34fn ohtx_ed_expand seed view uchar 32 ar edit uchar 32 pfx edit uchar 64 to void
35m = new uchar 65536 0
36hd = new uchar 64 0
37t = new uchar 64 0
38for i ulong 32
39put m i get seed i
40end
41s512hash m 32 hd
42for i ulong 32
43put t i get hd i
44put pfx i get hd add i 32
45end
46put t 0 and get t 0 uchar 248
47put t 31 and get t 31 uchar 127
48put t 31 or get t 31 uchar 64
49ohtx_ed_red t ar
50end
51
52fn ohtx_ed_basemul s view uchar 32 dst edit uchar 32 to void
53w = new ulong 1024 0
54z = new uchar 32 0
55edinit w
56edsmul w s z
57edpenc w edq dst
58end
59
60fn ohtx_ed_pubkey seed view uchar 32 pk edit uchar 32 to void
61ar = new uchar 32 0
62pfx = new uchar 64 0
63ohtx_ed_expand seed ar pfx
64ohtx_ed_basemul ar pk
65end
66
67fn ohtx_ed_muladd r view uchar 32 k view uchar 32 a view uchar 32 sg edit uchar 64 to void
68x = new long 64 0
69for i ulong 32
70put x i long get r i
71end
72for i ulong 32
73ki = long get k i
74for j ulong 32
75p = add i j
76put x p add get x p mul ki long get a j
77end
78end
79edmodl x
80for i ulong 32
81put sg add 32 i uchar get x i
82end
83end
84
85fn ohtx_ed_hprefix pfx view uchar 64 m view uchar 65536 n ulong hd edit uchar 64 to void
86if gt n 65504
87trap 3
88end
89pm = new uchar 65536 0
90for i ulong 32
91put pm i get pfx i
92end
93for i ulong n bound 65504
94put pm add 32 i get m i
95end
96s512hash pm add n 32 hd
97end
98
99fn ohtx_ed_sign seed view uchar 32 m view uchar 65536 n ulong sg edit uchar 64 to void
100ar = new uchar 32 0
101pfx = new uchar 64 0
102pk = new uchar 32 0
103hd = new uchar 64 0
104r = new uchar 32 0
105rb = new uchar 32 0
106k = new uchar 32 0
107ohtx_ed_expand seed ar pfx
108ohtx_ed_basemul ar pk
109ohtx_ed_hprefix pfx m n hd
110ohtx_ed_red hd r
111ohtx_ed_basemul r rb
112for i ulong 32
113put sg i get rb i
114end
115edhram sg pk m n k
116ohtx_ed_muladd r k ar sg
117end
118
119const OHW_MAGIC_0 uchar 0x4f
120const OHW_VERSION uchar 1
121const OHW_KIND_SIGNAL uchar 0x48
122const OHW_KIND_REGISTER uchar 0x52
123const OHW_KIND_CHALLENGE uchar 0x51
124const OHW_KIND_ACK uchar 0x41
125const OHW_KIND_CAPSULE uchar 0x43
126
127const OHW_MAX_DATAGRAM ulong 1200
128const OHW_MAX_CAPSULE ulong 8192
129const OHW_COOKIE_LEN ulong 16
130const OHW_COOKIE_WINDOW ulong 120
131const OHW_MAX_DRIFT_SECS ulong 300
132
133note Exact-byte AITR v1 & OHTX v1 constants
134const OHTX_AITR_HDR ulong 16
135const OHTX_AITR_REC ulong 64
136const OHTX_AITR_MAX_PAY ulong 52
137const OHTX_CONSENT_LEN ulong 160
138const OHTX_HDR_LEN ulong 296
139const OHTX_REPLAY_CAP ulong 64
140
141note Error codes for wire / trace-receiver contract
142const OHTX_OK ulong 0
143const OHTX_ERR_BAD_MAGIC ulong 101
144const OHTX_ERR_BAD_VERSION ulong 102
145const OHTX_ERR_TRUNCATED ulong 103
146const OHTX_ERR_TOO_LARGE ulong 104
147const OHTX_ERR_BAD_SIG ulong 105
148const OHTX_ERR_DRIFT ulong 106
149const OHTX_ERR_REPLAY ulong 107
150const OHTX_ERR_NO_CONSENT ulong 108
151const OHTX_ERR_CONSENT_EXPIRED ulong 109
152const OHTX_ERR_CONSENT_AUTH ulong 110
153const OHTX_ERR_WRONG_RECEIVER ulong 111
154const OHTX_ERR_BAD_DIGEST ulong 112
155const OHTX_ERR_AITR_HEADER ulong 113
156const OHTX_ERR_AITR_TORN ulong 114
157const OHTX_ERR_AITR_CRC ulong 115
158const OHTX_ERR_AITR_SEQ ulong 116
159const OHTX_ERR_AITR_PAYLEN ulong 117
160const OHTX_ERR_TYPE_NOT_CONSENTED ulong 118
161const OHTX_ERR_FIREWALL ulong 119
162const OHTX_ERR_ESCALATION ulong 120
163const OHTX_ERR_BAD_FIELD ulong 121
164
165text ohtx_dom_consent "open-humanity.trace-consent.v1"
166text ohtx_dom_export "open-humanity.trace-export.v1"
167
168note Receiver replay cache (up to 64 16-byte bundle_ids)
169global ohtx_seen_ids uchar 1024
170global ohtx_seen_cnt ulong 1
171
172fn ohtx_reset_replay to void
173put ohtx_seen_cnt 0 0
174for i ulong 1024
175put ohtx_seen_ids i uchar 0
176end
177end
178
179fn ohtx_put_u16_le dst edit uchar * off ulong v ulong to void
180put dst off uchar and v 255
181put dst add off 1 uchar and shr v 8 255
182end
183
184fn ohtx_get_u16_le src view uchar * off ulong to ulong
185ret or ulong get src off shl ulong get src add off 1 8
186end
187
188fn ohtx_put_u32_le dst edit uchar * off ulong v ulong to void
189for i ulong 4
190put dst add off i uchar and shr v mul i 8 255
191end
192end
193
194fn ohtx_get_u32_le src view uchar * off ulong to ulong
195acc = ulong 0
196for i ulong 4
197acc = or acc shl ulong get src add off i mul i 8
198end
199ret acc
200end
201
202note ---- Self-contained BLAKE3 unkeyed & keyed (exact BLAKE3 spec parity with blake3 1.5) ----
203
204fn ohtx_b3_initw w edit ulong 128 to void
205put w 83 1779033703
206put w 84 3144134277
207put w 85 1013904242
208put w 86 2773480762
209put w 87 1359893119
210put w 88 2600822924
211put w 89 528734635
212put w 90 1541459225
213put w 91 2
214put w 92 6
215put w 93 3
216put w 94 10
217put w 95 7
218put w 96 0
219put w 97 4
220put w 98 13
221put w 99 1
222put w 100 11
223put w 101 12
224put w 102 5
225put w 103 9
226put w 104 14
227put w 105 15
228put w 106 8
229end
230
231fn ohtx_b3_mix w edit ulong 128 a ulong b ulong c ulong d ulong j ulong to void
232x = get w add 16 mul j 2
233t = add get w a get w b
234put w a and add t x 4294967295
235t = xor get w d get w a
236put w d and or shr t 16 shl t 16 4294967295
237put w c and add get w c get w d 4294967295
238t = xor get w b get w c
239put w b and or shr t 12 shl t 20 4294967295
240x = get w add 17 mul j 2
241t = add get w a get w b
242put w a and add t x 4294967295
243t = xor get w d get w a
244put w d and or shr t 8 shl t 24 4294967295
245put w c and add get w c get w d 4294967295
246t = xor get w b get w c
247put w b and or shr t 7 shl t 25 4294967295
248end
249
250fn ohtx_b3_round w edit ulong 128 to void
251ohtx_b3_mix w 0 4 8 12 0
252ohtx_b3_mix w 1 5 9 13 1
253ohtx_b3_mix w 2 6 10 14 2
254ohtx_b3_mix w 3 7 11 15 3
255ohtx_b3_mix w 0 5 10 15 4
256ohtx_b3_mix w 1 6 11 12 5
257ohtx_b3_mix w 2 7 8 13 6
258ohtx_b3_mix w 3 4 9 14 7
259end
260
261fn ohtx_b3_perm w edit ulong 128 to void
262for i ulong 16
263put w add 32 i get w add 16 get w add 91 i
264end
265for i ulong 16
266put w add 16 i get w add 32 i
267end
268end
269
270fn ohtx_b3_compress w edit ulong 128 blk view uchar 64 to void
271for i ulong 16
272o = mul i 4
273v = ulong get blk add o 3
274v = or shl v 8 ulong get blk add o 2
275v = or shl v 8 ulong get blk add o 1
276v = or shl v 8 ulong get blk o
277put w add 16 i v
278end
279for i ulong 8
280put w i get w add 48 i
281put w add i 8 get w add 83 i
282end
283put w 12 and get w 73 4294967295
284put w 13 and shr get w 73 32 4294967295
285put w 14 get w 72
286put w 15 get w 74
287for r ulong 7
288ohtx_b3_round w
289ohtx_b3_perm w
290end
291for i ulong 8
292put w add 56 i xor get w i get w add i 8
293put w add 64 i xor get w add i 8 get w add 48 i
294end
295end
296
297fn ohtx_b3_outle w view ulong 128 dst edit uchar 32 to void
298for i ulong 8
299v = get w add 56 i
300for k ulong 4
301put dst add mul i 4 k uchar and shr v mul k 8 255
302end
303end
304end
305
306fn ohtx_b3_copy_cv w edit ulong 128 to void
307for i ulong 8
308put w add 48 i get w add 56 i
309end
310end
311
312fn ohtx_b3_load_iv w edit ulong 128 to void
313for i ulong 8
314put w add 48 i get w add 83 i
315end
316end
317
318fn ohtx_b3_fill_blk blk edit uchar 64 msg view uchar * pos ulong blen ulong to void
319for i ulong 64
320put blk i uchar 0
321end
322for i ulong blen bound 64
323put blk i get msg add pos i
324end
325end
326
327note Process one BLAKE3 chunk of length len(msg) (0..1024) with chunk counter ctr.
328note Leaves CV in w[48..56) and if is_root=1 sets ROOT flag (8) on the last block.
329fn ohtx_b3_chunk w edit ulong 128 msg view uchar * ctr ulong is_root ulong to void
330blk = new uchar 64 0
331n = len msg
332ohtx_b3_load_iv w
333if eq n 0
334put w 72 0
335put w 73 ctr
336fl = ulong 3
337if ne is_root 0
338fl = ulong 11
339end
340put w 74 fl
341ohtx_b3_compress w blk
342ohtx_b3_copy_cv w
343ret
344end
345pos = ulong 0
346while lt pos n bound 16
347blen = sub n pos
348if gt blen 64
349blen = ulong 64
350end
351ohtx_b3_fill_blk blk msg pos blen
352fl = ulong 0
353if eq pos 0
354fl = or fl 1
355end
356if eq add pos blen n
357fl = or fl 2
358if ne is_root 0
359fl = or fl 8
360end
361end
362put w 72 blen
363put w 73 ctr
364put w 74 fl
365ohtx_b3_compress w blk
366ohtx_b3_copy_cv w
367pos = add pos blen
368end
369end
370
371fn ohtx_b3_put4 blk edit uchar 64 off ulong v ulong to void
372put blk off uchar and v 255
373put blk add off 1 uchar and shr v 8 255
374put blk add off 2 uchar and shr v 16 255
375put blk add off 3 uchar and shr v 24 255
376end
377
378fn ohtx_b3_parent w edit ulong 128 st view ulong 128 lidx ulong r_off ulong is_root ulong to void
379blk = new uchar 64 0
380for i ulong 8
381o = mul i 4
382ohtx_b3_put4 blk o get st add mul lidx 8 i
383ohtx_b3_put4 blk add 32 o get w add r_off i
384end
385ohtx_b3_load_iv w
386put w 72 64
387put w 73 0
388fl = ulong 4
389if ne is_root 0
390fl = ulong 12
391end
392put w 74 fl
393ohtx_b3_compress w blk
394ohtx_b3_copy_cv w
395end
396
397fn ohtx_b3_push w view ulong 128 st edit ulong 128 depth ulong to void
398base = mul depth 8
399for i ulong 8
400put st add base i get w add 48 i
401end
402end
403
404note Full multi-chunk unkeyed BLAKE3 hash (up to 64 KiB) matching blake3::hash byte-for-byte.
405fn ohtx_b3_hash msg view uchar * out32 edit uchar 32 to void
406w = new ulong 128 0
407st = new ulong 128 0
408ohtx_b3_initw w
409n = len msg
410if le n 1024
411ohtx_b3_chunk w msg 0 1
412ohtx_b3_outle w out32
413ret
414end
415depth = ulong 0
416ctr = ulong 0
417off = ulong 0
418while gt sub n off 1024 bound 64
419ohtx_b3_chunk w part msg off 1024 ctr 0
420tot = add ctr 1
421while eq and tot 1 0 bound 8
422depth = sub depth 1
423ohtx_b3_parent w st depth 48 0
424tot = shr tot 1
425end
426ohtx_b3_push w st depth
427depth = add depth 1
428ctr = add ctr 1
429off = add off 1024
430end
431ohtx_b3_chunk w part msg off sub n off ctr 0
432while gt depth 1 bound 8
433depth = sub depth 1
434ohtx_b3_parent w st depth 48 0
435end
436ohtx_b3_parent w st 0 48 1
437ohtx_b3_outle w out32
438end
439
440fn ohtx_b3_load_key w edit ulong 128 secret view uchar 32 to void
441for i ulong 8
442o = mul i 4
443b0 = ulong get secret o
444b1 = ulong get secret add o 1
445b2 = ulong get secret add o 2
446b3 = ulong get secret add o 3
447kv = or b0 shl b1 8
448kv = or kv shl b2 16
449kv = or kv shl b3 24
450put w add 48 i kv
451end
452end
453
454note Keyed BLAKE3 single-chunk (n <= 64 bytes), matching blake3::Hasher::new_keyed(secret).
455note Uses key words as input CV and sets KEYED_HASH (16) | CHUNK_START (1) | CHUNK_END (2) | ROOT (8) = 27.
456fn ohtx_b3_keyed_64 secret view uchar 32 msg view uchar * out32 edit uchar 32 to void
457n = len msg
458if gt n 64
459trap 3
460end
461w = new ulong 128 0
462blk = new uchar 64 0
463ohtx_b3_initw w
464ohtx_b3_load_key w secret
465for i ulong n bound 64
466put blk i get msg i
467end
468put w 72 n
469put w 73 0
470put w 74 27
471ohtx_b3_compress w blk
472ohtx_b3_outle w out32
473end
474
475note Exact relay_cookie(secret, addr_ascii, window) -> 16 bytes (crates/beacon-core/src/wire.rs).
476fn ohw_relay_cookie secret view uchar 32 addr view uchar * window ulong cookie edit uchar 16 to void
477alen = len addr
478if gt alen 56
479trap 3
480end
481inp = new uchar 64 0
482for i ulong alen bound 56
483put inp i get addr i
484end
485for i ulong 8
486put inp add alen i uchar and shr window mul i 8 255
487end
488h32 = new uchar 32 0
489ohtx_b3_keyed_64 secret part inp 0 add alen 8 h32
490for i ulong 16
491put cookie i get h32 i
492end
493end
494
495note True iff cookie matches current window or previous window (120s windows).
496fn ohw_cookie_valid secret view uchar 32 addr view uchar * now_secs ulong cookie view uchar 16 to bool
497win = div now_secs OHW_COOKIE_WINDOW
498c0 = new uchar 16 0
499ohw_relay_cookie secret addr win c0
500ok0 = true
501for i ulong 16
502if ne get c0 i get cookie i
503ok0 = false
504break
505end
506end
507if ok0
508ret true
509end
510if gt win 0
511ohw_relay_cookie secret addr sub win 1 c0
512ok1 = true
513for i ulong 16
514if ne get c0 i get cookie i
515ok1 = false
516break
517end
518end
519ret ok1
520end
521ret false
522end
523
524fn ohtx_is_ws b uchar to bool
525if eq b uchar 32
526ret true
527end
528if eq b uchar 9
529ret true
530end
531if eq b uchar 10
532ret true
533end
534ret eq b uchar 13
535end
536
537note Normalized error fingerprint: scrub user paths to ~/, collapse whitespace, BLAKE3-256.
538fn ohw_fingerprint_error err_text view uchar * out32 edit uchar 32 to void
539scbuf = new uchar 512 0
540sn = oh_scrub_paths err_text scbuf
541norm = new uchar 512 0
542nn = ulong 0
543prev_ws = true
544for i ulong sn bound 512
545b = get scbuf i
546if ohtx_is_ws b
547if not prev_ws
548if lt nn 512
549put norm nn uchar 32
550nn = add nn 1
551end
552prev_ws = true
553end
554else
555if lt nn 512
556put norm nn b
557nn = add nn 1
558end
559prev_ws = false
560end
561end
562if gt nn 0
563if eq get norm sub nn 1 uchar 32
564nn = sub nn 1
565end
566end
567ohtx_b3_hash part norm 0 nn out32
568end
569
570note ---- Exact-byte Open Humanity UDP Wire Codec (bincode 1.3 LE parity with wire.rs) ----
571
572note Encode Challenge frame (19 bytes): [0x4f, 0x51, 0x01] || cookie[16].
573fn ohw_encode_challenge cookie view uchar 16 dst edit uchar 19 to ulong
574put dst 0 OHW_MAGIC_0
575put dst 1 OHW_KIND_CHALLENGE
576put dst 2 OHW_VERSION
577for i ulong 16
578put dst add 3 i get cookie i
579end
580ret 19
581end
582
583note Decode Challenge frame.
584fn ohw_decode_challenge wire view uchar * cookie edit uchar 16 to ulong
585if ne len wire 19
586ret OHTX_ERR_TRUNCATED
587end
588if or ne get wire 0 OHW_MAGIC_0 ne get wire 1 OHW_KIND_CHALLENGE
589ret OHTX_ERR_BAD_MAGIC
590end
591if ne get wire 2 OHW_VERSION
592ret OHTX_ERR_BAD_VERSION
593end
594for i ulong 16
595put cookie i get wire add 3 i
596end
597ret OHTX_OK
598end
599
600note Encode Ack frame (8 bytes): [0x4f, 0x41, 0x01] || topics(u8) || ttl_secs(u32 LE).
601fn ohw_encode_ack topics uchar ttl_secs ulong dst edit uchar 8 to ulong
602put dst 0 OHW_MAGIC_0
603put dst 1 OHW_KIND_ACK
604put dst 2 OHW_VERSION
605put dst 3 topics
606ohtx_put_u32_le dst 4 ttl_secs
607ret 8
608end
609
610note Decode Ack frame into meta[0]=topics, meta[1]=ttl_secs.
611fn ohw_decode_ack wire view uchar * meta edit ulong 2 to ulong
612if ne len wire 8
613ret OHTX_ERR_TRUNCATED
614end
615if or ne get wire 0 OHW_MAGIC_0 ne get wire 1 OHW_KIND_ACK
616ret OHTX_ERR_BAD_MAGIC
617end
618if ne get wire 2 OHW_VERSION
619ret OHTX_ERR_BAD_VERSION
620end
621put meta 0 ulong get wire 3
622put meta 1 ohtx_get_u32_le wire 4
623ret OHTX_OK
624end
625
626note Encode and Ed25519-sign Register frame (133 bytes) matching wire.rs::Frame::Register byte-for-byte.
627note Layout: [0..3) 4f 52 01, [3..35) pubkey, [35..43) ts_u64_le, [43] topics, [44] replay,
628note [45..61) cookie, [61..69) u64_le(64), [69..133) ed25519_sig.
629fn ohw_encode_register seed view uchar 32 ts ulong topics uchar replay uchar cookie view uchar 16 dst edit uchar 133 to ulong
630pk = new uchar 32 0
631ohtx_ed_pubkey seed pk
632put dst 0 OHW_MAGIC_0
633put dst 1 OHW_KIND_REGISTER
634put dst 2 OHW_VERSION
635for i ulong 32
636put dst add 3 i get pk i
637end
638oh_put_u64_le dst 35 ts
639put dst 43 topics
640put dst 44 replay
641for i ulong 16
642put dst add 45 i get cookie i
643end
644oh_put_u64_le dst 61 64
645for i ulong 64
646put dst add 69 i uchar 0
647end
648note signable_bytes is dst[3..133) (130 bytes, signature zeroed)
649sbuf = new uchar 65536 0
650for i ulong 130
651put sbuf i get dst add 3 i
652end
653sig = new uchar 64 0
654ohtx_ed_sign seed sbuf 130 sig
655for i ulong 64
656put dst add 69 i get sig i
657end
658ret 133
659end
660
661note Verify Register frame: magic, version, exact length 133, sig_len==64, timestamp drift <= 300s, Ed25519.
662fn ohw_verify_register wire view uchar * now_secs ulong out_pk edit uchar 32 out_meta edit ulong 3 to ulong
663if ne len wire 133
664ret OHTX_ERR_TRUNCATED
665end
666if or ne get wire 0 OHW_MAGIC_0 ne get wire 1 OHW_KIND_REGISTER
667ret OHTX_ERR_BAD_MAGIC
668end
669if ne get wire 2 OHW_VERSION
670ret OHTX_ERR_BAD_VERSION
671end
672if ne oh_get_u64_le wire 61 64
673ret OHTX_ERR_BAD_FIELD
674end
675ts = oh_get_u64_le wire 35
676diff = ulong 0
677if ge now_secs ts
678diff = sub now_secs ts
679else
680diff = sub ts now_secs
681end
682if gt diff OHW_MAX_DRIFT_SECS
683ret OHTX_ERR_DRIFT
684end
685pk = new uchar 32 0
686sig = new uchar 64 0
687sbuf = new uchar 65536 0
688for i ulong 32
689b = get wire add 3 i
690put pk i b
691put out_pk i b
692end
693for i ulong 64
694put sig i get wire add 69 i
695end
696for i ulong 66
697put sbuf i get wire add 3 i
698end
699note sbuf[66..130) stays zero (signable_bytes has zeroed signature)
700if ne edverify pk sbuf 130 sig 0 1
701ret OHTX_ERR_BAD_SIG
702end
703put out_meta 0 ts
704put out_meta 1 ulong get wire 43
705put out_meta 2 ulong get wire 44
706ret OHTX_OK
707end
708
709note ---- Exact-byte AITR v1 Trace Segment Helpers (matches seed/trace/segment.om) ----
710
711fn ohtx_crc32 buf view uchar * off ulong n ulong to ulong
712c = ulong 4294967295
713for i ulong n bound 64
714c = xor c ulong get buf add off i
715for k ulong 8
716if eq and c 1 1
717c = xor shr c 1 3988292384
718else
719c = shr c 1
720end
721end
722end
723ret xor c 4294967295
724end
725
726note Write 16-byte AITR v1 header at dst[0..16).
727fn ohtx_aitr_write_hdr dst edit uchar * to void
728put dst 0 uchar 65
729put dst 1 uchar 73
730put dst 2 uchar 84
731put dst 3 uchar 82
732ohtx_put_u16_le dst 4 1
733ohtx_put_u16_le dst 6 64
734ohtx_put_u32_le dst 8 16
735ohtx_put_u32_le dst 12 0
736end
737
738note Write one 64-byte AITR v1 record at dst[off..off+64) with IEEE CRC-32.
739note rinfo packs rtype (low 16 bits) and seq (bits 16..47): rinfo = or rtype shl seq 16.
740fn ohtx_aitr_write_rec dst edit uchar * off ulong rinfo ulong pay view uchar * to ulong
741rtype = and rinfo 65535
742seq = and shr rinfo 16 4294967295
743plen = len pay
744if gt plen OHTX_AITR_MAX_PAY
745ret OHTX_ERR_AITR_PAYLEN
746end
747for i ulong 64
748put dst add off i uchar 0
749end
750ohtx_put_u16_le dst off rtype
751ohtx_put_u16_le dst add off 2 plen
752ohtx_put_u32_le dst add off 4 seq
753pbase = add off 8
754for i ulong plen bound 52
755put dst add pbase i get pay i
756end
757crc = ohtx_crc32 dst off 60
758ohtx_put_u32_le dst add off 60 crc
759ret OHTX_OK
760end
761
762note Validate AITR v1 header and exact record alignment; sets out_rcnt[0] = record count.
763fn ohtx_aitr_validate_hdr seg view uchar * out_rcnt edit ulong 1 to ulong
764n = len seg
765if lt n OHTX_AITR_HDR
766ret OHTX_ERR_AITR_HEADER
767end
768if ne get seg 0 uchar 65
769ret OHTX_ERR_AITR_HEADER
770end
771if ne get seg 1 uchar 73
772ret OHTX_ERR_AITR_HEADER
773end
774if ne get seg 2 uchar 84
775ret OHTX_ERR_AITR_HEADER
776end
777if ne get seg 3 uchar 82
778ret OHTX_ERR_AITR_HEADER
779end
780if ne ohtx_get_u16_le seg 4 1
781ret OHTX_ERR_AITR_HEADER
782end
783if ne ohtx_get_u16_le seg 6 64
784ret OHTX_ERR_AITR_HEADER
785end
786if ne ohtx_get_u32_le seg 8 16
787ret OHTX_ERR_AITR_HEADER
788end
789if ne ohtx_get_u32_le seg 12 0
790ret OHTX_ERR_AITR_HEADER
791end
792body = sub n OHTX_AITR_HDR
793if ne rem body OHTX_AITR_REC 0
794ret OHTX_ERR_AITR_TORN
795end
796put out_rcnt 0 div body OHTX_AITR_REC
797ret OHTX_OK
798end
799
800note Validate one 64-byte AITR v1 record at index idx (seq, paylen <= 52, zero padding, IEEE CRC-32).
801fn ohtx_aitr_validate_rec seg view uchar * idx ulong out_type edit ulong 1 out_plen edit ulong 1 to ulong
802off = add OHTX_AITR_HDR mul idx OHTX_AITR_REC
803rtype = ohtx_get_u16_le seg off
804plen = ohtx_get_u16_le seg add off 2
805seq = ohtx_get_u32_le seg add off 4
806if gt plen OHTX_AITR_MAX_PAY
807ret OHTX_ERR_AITR_PAYLEN
808end
809if ne seq idx
810ret OHTX_ERR_AITR_SEQ
811end
812pbase = add add off 8 plen
813zcnt = sub OHTX_AITR_MAX_PAY plen
814for j ulong zcnt bound 52
815if ne get seg add pbase j uchar 0
816ret OHTX_ERR_AITR_PAYLEN
817end
818end
819want_crc = ohtx_get_u32_le seg add off 60
820got_crc = ohtx_crc32 seg off 60
821if ne want_crc got_crc
822ret OHTX_ERR_AITR_CRC
823end
824put out_type 0 rtype
825put out_plen 0 plen
826ret OHTX_OK
827end
828
829note ---- Exact-byte Consent Manifest (OHCN v1, 160 bytes, Ed25519 signed) ----
830
831fn ohtx_sign_consent seed view uchar 32 recv_pk view uchar 32 type_mask ulong not_after ulong cap_flags ulong dst edit uchar 160 to void
832npk = new uchar 32 0
833ohtx_ed_pubkey seed npk
834put dst 0 uchar 79
835put dst 1 uchar 72
836put dst 2 uchar 67
837put dst 3 uchar 78
838put dst 4 uchar 1
839put dst 5 uchar 1
840put dst 6 uchar 0
841put dst 7 uchar 0
842for i ulong 32
843put dst add 8 i get npk i
844put dst add 40 i get recv_pk i
845end
846oh_put_u64_le dst 72 type_mask
847oh_put_u64_le dst 80 not_after
848oh_put_u64_le dst 88 cap_flags
849sbuf = new uchar 65536 0
850dlen = len ohtx_dom_consent
851for i ulong dlen bound 64
852put sbuf i get ohtx_dom_consent i
853end
854for i ulong 96
855put sbuf add dlen i get dst i
856end
857sig = new uchar 64 0
858ohtx_ed_sign seed sbuf add dlen 96 sig
859for i ulong 64
860put dst add 96 i get sig i
861end
862end
863
864fn ohtx_verify_consent cbuf view uchar * now_secs ulong expect_recv view uchar 32 out_mask edit ulong 1 to ulong
865if ne len cbuf OHTX_CONSENT_LEN
866ret OHTX_ERR_TRUNCATED
867end
868if ne get cbuf 0 uchar 79
869ret OHTX_ERR_BAD_MAGIC
870end
871if ne get cbuf 1 uchar 72
872ret OHTX_ERR_BAD_MAGIC
873end
874if ne get cbuf 2 uchar 67
875ret OHTX_ERR_BAD_MAGIC
876end
877if ne get cbuf 3 uchar 78
878ret OHTX_ERR_BAD_MAGIC
879end
880if ne get cbuf 4 uchar 1
881ret OHTX_ERR_BAD_VERSION
882end
883if ne get cbuf 5 uchar 1
884ret OHTX_ERR_NO_CONSENT
885end
886if or ne get cbuf 6 uchar 0 ne get cbuf 7 uchar 0
887ret OHTX_ERR_BAD_FIELD
888end
889note Zero-authority boundary: cap_flags MUST be 0
890if ne oh_get_u64_le cbuf 88 0
891ret OHTX_ERR_CONSENT_AUTH
892end
893not_after = oh_get_u64_le cbuf 80
894if gt now_secs not_after
895ret OHTX_ERR_CONSENT_EXPIRED
896end
897note Receiver binding check (and non-zero receiver pubkey)
898nz = false
899for i ulong 32
900rb = get cbuf add 40 i
901if ne rb uchar 0
902nz = true
903end
904if ne rb get expect_recv i
905ret OHTX_ERR_WRONG_RECEIVER
906end
907end
908if not nz
909ret OHTX_ERR_WRONG_RECEIVER
910end
911npk = new uchar 32 0
912sig = new uchar 64 0
913for i ulong 32
914put npk i get cbuf add 8 i
915end
916for i ulong 64
917put sig i get cbuf add 96 i
918end
919sbuf = new uchar 65536 0
920dlen = len ohtx_dom_consent
921for i ulong dlen bound 64
922put sbuf i get ohtx_dom_consent i
923end
924for i ulong 96
925put sbuf add dlen i get cbuf i
926end
927if ne edverify npk sbuf add dlen 96 sig 0 1
928ret OHTX_ERR_BAD_SIG
929end
930put out_mask 0 oh_get_u64_le cbuf 72
931ret OHTX_OK
932end
933
934note Check a single trace record payload against Personal Data Firewall + escalation detector.
935fn ohtx_check_payload_clean pay view uchar * to ulong
936if eq len pay 0
937ret OHTX_OK
938end
939prc = oh_privacy_check pay
940if ne prc OH_OK
941ret OHTX_ERR_FIREWALL
942end
943if oh_scan_escalation_raw pay
944ret OHTX_ERR_ESCALATION
945end
946ret OHTX_OK
947end
948
949note ---- Exact-byte Trace Exporter (AITR v1 + OHCN v1 -> signed OHTX v1 bundle) ----
950
951global ohtx_last_export_len ulong 1
952global ohtx_last_recv_len ulong 1
953global ohtx_export_bid uchar 16
954
955fn ohtx_set_bundle_id bid view uchar 16 to void
956for i ulong 16
957put ohtx_export_bid i get bid i
958end
959end
960
961fn ohtx_export_len to ulong
962ret get ohtx_last_export_len 0
963end
964
965fn ohtx_recv_len to ulong
966ret get ohtx_last_recv_len 0
967end
968
969fn ohtx_type_allowed tmask ulong rtype ulong to bool
970if ge rtype 64
971ret false
972end
973ret ne and shr tmask rtype 1 0
974end
975
976fn ohtx_append_rec out_seg edit uchar 4112 out_cnt edit ulong 1 rtype ulong pay view uchar * to ulong
977oc = get out_cnt 0
978rinfo = or rtype shl oc 16
979doff = add OHTX_AITR_HDR mul oc OHTX_AITR_REC
980rc = ohtx_aitr_write_rec out_seg doff rinfo pay
981if ne rc OHTX_OK
982ret rc
983end
984put out_cnt 0 add oc 1
985ret OHTX_OK
986end
987
988fn ohtx_filter_one in_seg view uchar * idx ulong tmask ulong out_seg edit uchar 4112 out_cnt edit ulong 1 to ulong
989rt_box = new ulong 1 0
990pl_box = new ulong 1 0
991rrc = ohtx_aitr_validate_rec in_seg idx rt_box pl_box
992if ne rrc OHTX_OK
993ret rrc
994end
995if not ohtx_type_allowed tmask get rt_box 0
996ret OHTX_OK
997end
998poff = add add OHTX_AITR_HDR mul idx OHTX_AITR_REC 8
999rrc = ohtx_check_payload_clean part in_seg poff get pl_box 0
1000if ne rrc OHTX_OK
1001ret rrc
1002end
1003ret ohtx_append_rec out_seg out_cnt get rt_box 0 part in_seg poff get pl_box 0
1004end
1005
1006note Helper: filter consented AITR v1 records and check Personal Data Firewall on every byte.
1007fn ohtx_filter_aitr in_seg view uchar * tmask ulong out_seg edit uchar 4112 out_seglen edit ulong 1 to ulong
1008rcnt_box = new ulong 1 0
1009out_cnt = new ulong 1 0
1010hrc = ohtx_aitr_validate_hdr in_seg rcnt_box
1011if ne hrc OHTX_OK
1012ret hrc
1013end
1014rcnt = get rcnt_box 0
1015if gt rcnt 64
1016ret OHTX_ERR_TOO_LARGE
1017end
1018ohtx_aitr_write_hdr out_seg
1019for i ulong rcnt bound 64
1020frc = ohtx_filter_one in_seg i tmask out_seg out_cnt
1021if ne frc OHTX_OK
1022ret frc
1023end
1024end
1025put out_seglen 0 add OHTX_AITR_HDR mul get out_cnt 0 OHTX_AITR_REC
1026ret OHTX_OK
1027end
1028
1029note Helper: sign OHTX v1 header[0..232) with Ed25519 into dst[232..296).
1030fn ohtx_sign_bundle_hdr seed view uchar 32 dst edit uchar 4408 to void
1031sbuf = new uchar 65536 0
1032dlen = len ohtx_dom_export
1033for i ulong dlen bound 64
1034put sbuf i get ohtx_dom_export i
1035end
1036for i ulong 232
1037put sbuf add dlen i get dst i
1038end
1039sig = new uchar 64 0
1040ohtx_ed_sign seed sbuf add dlen 232 sig
1041for i ulong 64
1042put dst add 232 i get sig i
1043end
1044end
1045
1046fn ohtx_verify_consent_for_export seed view uchar 32 consent view uchar 160 now_secs ulong mask_box edit ulong 1 to ulong
1047recv_pk = new uchar 32 0
1048for i ulong 32
1049put recv_pk i get consent add 40 i
1050end
1051crc = ohtx_verify_consent consent now_secs recv_pk mask_box
1052if ne crc OHTX_OK
1053ret crc
1054end
1055my_pk = new uchar 32 0
1056ohtx_ed_pubkey seed my_pk
1057for i ulong 32
1058if ne get my_pk i get consent add 8 i
1059ret OHTX_ERR_BAD_SIG
1060end
1061end
1062ret OHTX_OK
1063end
1064
1065fn ohtx_pack_bundle seed view uchar 32 consent view uchar 160 out_seg view uchar 4112 seg_len ulong now_secs ulong dst edit uchar 4408 to void
1066put dst 0 uchar 79
1067put dst 1 uchar 72
1068put dst 2 uchar 84
1069put dst 3 uchar 88
1070put dst 4 uchar 1
1071put dst 5 uchar 0
1072put dst 6 uchar 0
1073put dst 7 uchar 0
1074oh_put_u64_le dst 8 now_secs
1075for i ulong 16
1076put dst add 16 i get ohtx_export_bid i
1077end
1078for i ulong 160
1079put dst add 32 i get consent i
1080end
1081dig = new uchar 32 0
1082ohtx_b3_hash part out_seg 0 seg_len dig
1083for i ulong 32
1084put dst add 192 i get dig i
1085end
1086oh_put_u64_le dst 224 seg_len
1087ohtx_sign_bundle_hdr seed dst
1088for i ulong seg_len bound 4112
1089put dst add OHTX_HDR_LEN i get out_seg i
1090end
1091end
1092
1093note Export consented, firewall-clean AITR v1 records into signed OHTX v1 bundle dst.
1094note Caller sets bundle_id via ohtx_set_bundle_id beforehand (keeps parameter count <= 6 slots).
1095note Returns OHTX_OK (0) and sets ohtx_last_export_len, or returns nonzero refusal and writes 0 bytes.
1096fn oh_trace_export seed view uchar 32 consent view uchar 160 in_seg view uchar * now_secs ulong dst edit uchar 4408 to ulong
1097put ohtx_last_export_len 0 0
1098mask_box = new ulong 1 0
1099rc = ohtx_verify_consent_for_export seed consent now_secs mask_box
1100if ne rc OHTX_OK
1101ret rc
1102end
1103out_seg = new uchar 4112 0
1104seglen_box = new ulong 1 0
1105rc = ohtx_filter_aitr in_seg get mask_box 0 out_seg seglen_box
1106if ne rc OHTX_OK
1107ret rc
1108end
1109seg_len = get seglen_box 0
1110total_len = add OHTX_HDR_LEN seg_len
1111if gt total_len 4408
1112ret OHTX_ERR_TOO_LARGE
1113end
1114ohtx_pack_bundle seed consent out_seg seg_len now_secs dst
1115put ohtx_last_export_len 0 total_len
1116ret OHTX_OK
1117end
1118
1119note Replay check/record helper for 16-byte bundle_id.
1120fn ohtx_check_replay bid view uchar 16 to bool
1121cnt = get ohtx_seen_cnt 0
1122for k ulong cnt bound 64
1123base = mul k 16
1124same = true
1125for j ulong 16
1126if ne get ohtx_seen_ids add base j get bid j
1127same = false
1128break
1129end
1130end
1131if same
1132ret false
1133end
1134end
1135ret true
1136end
1137
1138fn ohtx_record_replay bid view uchar 16 to void
1139cnt = get ohtx_seen_cnt 0
1140idx = rem cnt OHTX_REPLAY_CAP
1141base = mul idx 16
1142for j ulong 16
1143put ohtx_seen_ids add base j get bid j
1144end
1145if lt cnt OHTX_REPLAY_CAP
1146put ohtx_seen_cnt 0 add cnt 1
1147end
1148end
1149
1150note Helper: verify Ed25519 exporter signature on bundle[0..232) and BLAKE3 digest on segment.
1151fn ohtx_verify_bundle_crypto bundle view uchar * seg_len ulong to ulong
1152npk = new uchar 32 0
1153sig = new uchar 64 0
1154for i ulong 32
1155put npk i get bundle add 40 i
1156end
1157for i ulong 64
1158put sig i get bundle add 232 i
1159end
1160sbuf = new uchar 65536 0
1161dlen = len ohtx_dom_export
1162for i ulong dlen bound 64
1163put sbuf i get ohtx_dom_export i
1164end
1165for i ulong 232
1166put sbuf add dlen i get bundle i
1167end
1168if ne edverify npk sbuf add dlen 232 sig 0 1
1169ret OHTX_ERR_BAD_SIG
1170end
1171dig = new uchar 32 0
1172ohtx_b3_hash part bundle OHTX_HDR_LEN seg_len dig
1173for i ulong 32
1174if ne get dig i get bundle add 192 i
1175ret OHTX_ERR_BAD_DIGEST
1176end
1177end
1178ret OHTX_OK
1179end
1180
1181fn ohtx_check_incoming_one seg view uchar * idx ulong tmask ulong to ulong
1182rt_box = new ulong 1 0
1183pl_box = new ulong 1 0
1184rrc = ohtx_aitr_validate_rec seg idx rt_box pl_box
1185if ne rrc OHTX_OK
1186ret rrc
1187end
1188if not ohtx_type_allowed tmask get rt_box 0
1189ret OHTX_ERR_TYPE_NOT_CONSENTED
1190end
1191poff = add add OHTX_AITR_HDR mul idx OHTX_AITR_REC 8
1192ret ohtx_check_payload_clean part seg poff get pl_box 0
1193end
1194
1195note Helper: validate incoming AITR v1 segment against consented mask and Personal Data Firewall.
1196fn ohtx_validate_incoming_seg seg view uchar * tmask ulong to ulong
1197rcnt_box = new ulong 1 0
1198hrc = ohtx_aitr_validate_hdr seg rcnt_box
1199if ne hrc OHTX_OK
1200ret hrc
1201end
1202rcnt = get rcnt_box 0
1203if gt rcnt 64
1204ret OHTX_ERR_TOO_LARGE
1205end
1206for i ulong rcnt bound 64
1207rrc = ohtx_check_incoming_one seg i tmask
1208if ne rrc OHTX_OK
1209ret rrc
1210end
1211end
1212ret OHTX_OK
1213end
1214
1215fn ohtx_check_bundle_hdr bundle view uchar * now_secs ulong bid edit uchar 16 to ulong
1216blen = len bundle
1217if lt blen add OHTX_HDR_LEN OHTX_AITR_HDR
1218ret OHTX_ERR_TRUNCATED
1219end
1220if ne get bundle 0 uchar 79
1221ret OHTX_ERR_BAD_MAGIC
1222end
1223if ne get bundle 1 uchar 72
1224ret OHTX_ERR_BAD_MAGIC
1225end
1226if ne get bundle 2 uchar 84
1227ret OHTX_ERR_BAD_MAGIC
1228end
1229if ne get bundle 3 uchar 88
1230ret OHTX_ERR_BAD_MAGIC
1231end
1232if ne get bundle 4 uchar 1
1233ret OHTX_ERR_BAD_VERSION
1234end
1235if ne get bundle 5 uchar 0
1236ret OHTX_ERR_BAD_FIELD
1237end
1238if ne get bundle 6 uchar 0
1239ret OHTX_ERR_BAD_FIELD
1240end
1241if ne get bundle 7 uchar 0
1242ret OHTX_ERR_BAD_FIELD
1243end
1244ts = oh_get_u64_le bundle 8
1245diff = ulong 0
1246if ge now_secs ts
1247diff = sub now_secs ts
1248else
1249diff = sub ts now_secs
1250end
1251if gt diff OHW_MAX_DRIFT_SECS
1252ret OHTX_ERR_DRIFT
1253end
1254for i ulong 16
1255put bid i get bundle add 16 i
1256end
1257if not ohtx_check_replay bid
1258ret OHTX_ERR_REPLAY
1259end
1260ret OHTX_OK
1261end
1262
1263note ---- Exact-byte Trace Receiver (OHTX v1 -> verified AITR v1 segment, fail-closed) ----
1264
1265fn oh_trace_receive bundle view uchar * recv_pk view uchar 32 now_secs ulong dst_seg edit uchar * to ulong
1266put ohtx_last_recv_len 0 0
1267bid = new uchar 16 0
1268rc = ohtx_check_bundle_hdr bundle now_secs bid
1269if ne rc OHTX_OK
1270ret rc
1271end
1272mask_box = new ulong 1 0
1273rc = ohtx_verify_consent part bundle 32 OHTX_CONSENT_LEN now_secs recv_pk mask_box
1274if ne rc OHTX_OK
1275ret rc
1276end
1277seg_len = oh_get_u64_le bundle 224
1278if ne add OHTX_HDR_LEN seg_len len bundle
1279ret OHTX_ERR_TRUNCATED
1280end
1281if gt seg_len len dst_seg
1282ret OHTX_ERR_TOO_LARGE
1283end
1284rc = ohtx_verify_bundle_crypto bundle seg_len
1285if ne rc OHTX_OK
1286ret rc
1287end
1288rc = ohtx_validate_incoming_seg part bundle OHTX_HDR_LEN seg_len get mask_box 0
1289if ne rc OHTX_OK
1290ret rc
1291end
1292ohtx_record_replay bid
1293for i ulong seg_len bound 4112
1294put dst_seg i get bundle add OHTX_HDR_LEN i
1295end
1296put ohtx_last_recv_len 0 seg_len
1297ret OHTX_OK
1298end