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 / rad-id-sync / lib.om

1313 lines · 26261 bytes Raw Blame History
1note protocol/rad-id-sync/lib.om: pure-Omega Radicle identity validation and synchronization engine
2note Faithful port of upstream rad-id-sync (identity anchor, SHA-256 fingerprints, key validation)
3note extended with Radicle RID, delegate did:key:z6Mk, threshold/quorum, and diff/sync policy checks.
4note Compose with si/directives/om2.om ahead of this file.
5
6const RAD_MAX_DEL ulong 16
7const RAD_DID_SLOT ulong 64
8const RAD_DEL_BUF ulong 1024
9const RAD_RID_CAP ulong 64
10const RAD_KEY_CAP ulong 256
11
12const RAD_OK ulong 0
13const RAD_ERR_BAD_RID ulong 1
14const RAD_ERR_BAD_DID ulong 2
15const RAD_ERR_BAD_THRESH ulong 3
16const RAD_ERR_DUP_DEL ulong 4
17const RAD_ERR_TOO_MANY ulong 5
18const RAD_ERR_RID_MISMATCH ulong 6
19const RAD_ERR_UNAUTH_DEL ulong 7
20const RAD_ERR_NO_QUORUM ulong 8
21const RAD_ERR_BAD_KEY ulong 9
22const RAD_ERR_PARSE ulong 10
23
24const SYNC_IN_SYNC ulong 0
25const SYNC_UPDATE_OK ulong 1
26const SYNC_ROTATED ulong 2
27const SYNC_REFUSED ulong 3
28
29const ENTITY_AGENT ulong 0
30const ENTITY_HUMAN ulong 1
31const ENTITY_BRIDGE ulong 2
32
33struct RadDiff code ulong decision ulong added ulong removed ulong common ulong thresh_chg ulong end
34struct AnchorSync code ulong in_sync ulong rotated ulong rad_chg ulong ssh_chg ulong hist_n ulong end
35
36note Global buffers for base and remote Radicle identity documents
37global g_base_rid uchar 64
38global g_base_rid_len ulong 1
39global g_base_dels uchar 1024
40global g_base_del_lens ulong 16
41global g_base_del_n ulong 1
42global g_base_thresh ulong 1
43
44global g_rem_rid uchar 64
45global g_rem_rid_len ulong 1
46global g_rem_dels uchar 1024
47global g_rem_del_lens ulong 16
48global g_rem_del_n ulong 1
49global g_rem_thresh ulong 1
50
51note Global state for IdentityAnchor (upstream identity.rs + keys.rs)
52global g_anc_did uchar 64
53global g_anc_did_len ulong 1
54global g_anc_name uchar 64
55global g_anc_name_len ulong 1
56global g_anc_etype ulong 1
57global g_anc_rad_key uchar 256
58global g_anc_rad_len ulong 1
59global g_anc_ssh_key uchar 256
60global g_anc_ssh_len ulong 1
61global g_anc_rad_fp uchar 64
62global g_anc_ssh_fp uchar 64
63global g_anc_hist_n ulong 1
64
65note Global scratch for SHA-256 state and schedule
66global g_sha_k uint 64
67global g_sha_k_init ulong 1
68global g_sha_h uint 8
69global g_sha_w uint 64
70global g_sha_blk uchar 64
71
72text txt_rad_pfx "rad:z"
73text txt_did_key_pfx "did:key:z6Mk"
74text txt_did_rad_pfx "did:rad:"
75text txt_ssh_pfx "ssh-ed25519 "
76text txt_hex_digits "0123456789abcdef"
77
78text txt_def_did "did:rad:aien:spark-master"
79text txt_def_name "AIEN"
80
81text txt_j_open "{\"anchor_did\":\""
82text txt_j_name "\",\"entity_name\":\""
83text txt_j_insync "\",\"in_sync\":"
84text txt_j_rot ",\"rotated\":"
85text txt_j_hist ",\"key_history_len\":"
86text txt_j_radfp ",\"radicle_fp\":\""
87text txt_j_sshfp "\",\"ssh_fp\":\""
88text txt_j_msg "\",\"message\":\""
89text txt_j_close "\"}\n"
90text txt_true "true"
91text txt_false "false"
92text txt_msg_synced "Keys are already synchronized with Identity Anchor."
93text txt_msg_rad_rot "Detected rotated Radicle public key; updated Identity Anchor."
94text txt_msg_ssh_rot "Detected updated SSH public key; updated Identity Anchor."
95text txt_msg_both_rot "Detected updated Radicle and SSH public keys; updated Identity Anchor."
96text txt_msg_none "No local keys bound to Identity Anchor."
97
98note --- Basic byte and slice helpers ---
99
100fn is_space_b b uchar to bool
101if eq b uchar 32
102ret true
103end
104if eq b uchar 9
105ret true
106end
107if eq b uchar 10
108ret true
109end
110if eq b uchar 13
111ret true
112end
113ret false
114end
115
116fn trim_start s view uchar * to ulong
117n = len s
118i = ulong 0
119note bound 65536 covers max supported input line/key length
120if gt n ulong 65536
121ret n
122end
123while lt i n bound 65536
124b = get s i
125if not is_space_b b
126ret i
127end
128i = add i ulong 1
129end
130ret n
131end
132
133fn trim_end s view uchar * to ulong
134n = len s
135if eq n ulong 0
136ret ulong 0
137end
138if gt n ulong 65536
139ret ulong 0
140end
141e = n
142while gt e ulong 0 bound 65536
143idx = sub e ulong 1
144b = get s idx
145if not is_space_b b
146ret e
147end
148e = idx
149end
150ret ulong 0
151end
152
153fn slice_eq a view uchar * b view uchar * to bool
154na = len a
155nb = len b
156if ne na nb
157ret false
158end
159if gt na ulong 65536
160ret false
161end
162i = ulong 0
163while lt i na bound 65536
164if ne get a i get b i
165ret false
166end
167i = add i ulong 1
168end
169ret true
170end
171
172fn slice_starts_with s view uchar * pfx view uchar * to bool
173ns = len s
174np = len pfx
175if lt ns np
176ret false
177end
178if gt np ulong 4096
179ret false
180end
181i = ulong 0
182while lt i np bound 4096
183if ne get s i get pfx i
184ret false
185end
186i = add i ulong 1
187end
188ret true
189end
190
191fn write_all fd int buf view uchar * to bool
192n = len buf
193if eq n ulong 0
194ret true
195end
196if gt n ulong 1048576
197ret false
198end
199off = ulong 0
200while lt off n bound 1048576
201remn = sub n off
202k = write fd buf off remn
203if le k long 0
204ret false
205end
206off = add off ulong k
207end
208ret true
209end
210
211fn append_bytes dst edit uchar * pos ulong src view uchar * to ulong
212cap = len dst
213ns = len src
214if gt ns ulong 65536
215ret pos
216end
217i = ulong 0
218p = pos
219while lt i ns bound 65536
220if ge p cap
221ret p
222end
223put dst p get src i
224p = add p ulong 1
225i = add i ulong 1
226end
227ret p
228end
229
230fn append_byte dst edit uchar * pos ulong b uchar to ulong
231if ge pos len dst
232ret pos
233end
234put dst pos b
235ret add pos ulong 1
236end
237
238fn append_u64 dst edit uchar * pos ulong v ulong to ulong
239tmp = new uchar 24 0
240n = ulong 0
241cur = v
242if eq cur ulong 0
243put tmp 0 uchar 48
244n = ulong 1
245else
246while gt cur ulong 0 bound 24
247d = rem cur ulong 10
248put tmp n uchar add d ulong 48
249cur = div cur ulong 10
250n = add n ulong 1
251end
252end
253p = pos
254i = n
255while gt i ulong 0 bound 24
256i = sub i ulong 1
257p = append_byte dst p get tmp i
258end
259ret p
260end
261
262note --- SHA-256 (FIPS 180-4) ---
263
264fn sha256_init_k to void
265if eq get g_sha_k_init 0 ulong 1
266ret
267end
268put g_sha_k 0 uint 0x428a2f98
269put g_sha_k 1 uint 0x71374491
270put g_sha_k 2 uint 0xb5c0fbcf
271put g_sha_k 3 uint 0xe9b5dba5
272put g_sha_k 4 uint 0x3956c25b
273put g_sha_k 5 uint 0x59f111f1
274put g_sha_k 6 uint 0x923f82a4
275put g_sha_k 7 uint 0xab1c5ed5
276put g_sha_k 8 uint 0xd807aa98
277put g_sha_k 9 uint 0x12835b01
278put g_sha_k 10 uint 0x243185be
279put g_sha_k 11 uint 0x550c7dc3
280put g_sha_k 12 uint 0x72be5d74
281put g_sha_k 13 uint 0x80deb1fe
282put g_sha_k 14 uint 0x9bdc06a7
283put g_sha_k 15 uint 0xc19bf174
284put g_sha_k 16 uint 0xe49b69c1
285put g_sha_k 17 uint 0xefbe4786
286put g_sha_k 18 uint 0x0fc19dc6
287put g_sha_k 19 uint 0x240ca1cc
288put g_sha_k 20 uint 0x2de92c6f
289put g_sha_k 21 uint 0x4a7484aa
290put g_sha_k 22 uint 0x5cb0a9dc
291put g_sha_k 23 uint 0x76f988da
292put g_sha_k 24 uint 0x983e5152
293put g_sha_k 25 uint 0xa831c66d
294put g_sha_k 26 uint 0xb00327c8
295put g_sha_k 27 uint 0xbf597fc7
296put g_sha_k 28 uint 0xc6e00bf3
297put g_sha_k 29 uint 0xd5a79147
298put g_sha_k 30 uint 0x06ca6351
299put g_sha_k 31 uint 0x14292967
300put g_sha_k 32 uint 0x27b70a85
301put g_sha_k 33 uint 0x2e1b2138
302put g_sha_k 34 uint 0x4d2c6dfc
303put g_sha_k 35 uint 0x53380d13
304put g_sha_k 36 uint 0x650a7354
305put g_sha_k 37 uint 0x766a0abb
306put g_sha_k 38 uint 0x81c2c92e
307put g_sha_k 39 uint 0x92722c85
308put g_sha_k 40 uint 0xa2bfe8a1
309put g_sha_k 41 uint 0xa81a664b
310put g_sha_k 42 uint 0xc24b8b70
311put g_sha_k 43 uint 0xc76c51a3
312put g_sha_k 44 uint 0xd192e819
313put g_sha_k 45 uint 0xd6990624
314put g_sha_k 46 uint 0xf40e3585
315put g_sha_k 47 uint 0x106aa070
316put g_sha_k 48 uint 0x19a4c116
317put g_sha_k 49 uint 0x1e376c08
318put g_sha_k 50 uint 0x2748774c
319put g_sha_k 51 uint 0x34b0bcb5
320put g_sha_k 52 uint 0x391c0cb3
321put g_sha_k 53 uint 0x4ed8aa4a
322put g_sha_k 54 uint 0x5b9cca4f
323put g_sha_k 55 uint 0x682e6ff3
324put g_sha_k 56 uint 0x748f82ee
325put g_sha_k 57 uint 0x78a5636f
326put g_sha_k 58 uint 0x84c87814
327put g_sha_k 59 uint 0x8cc70208
328put g_sha_k 60 uint 0x90befffa
329put g_sha_k 61 uint 0xa4506ceb
330put g_sha_k 62 uint 0xbef9a3f7
331put g_sha_k 63 uint 0xc67178f2
332put g_sha_k_init 0 ulong 1
333end
334
335fn sha256_reset to void
336sha256_init_k
337put g_sha_h 0 uint 0x6a09e667
338put g_sha_h 1 uint 0xbb67ae85
339put g_sha_h 2 uint 0x3c6ef372
340put g_sha_h 3 uint 0xa54ff53a
341put g_sha_h 4 uint 0x510e527f
342put g_sha_h 5 uint 0x9b05688c
343put g_sha_h 6 uint 0x1f83d9ab
344put g_sha_h 7 uint 0x5be0cd19
345end
346
347fn sha256_load_w to void
348for i ulong 16
349o = mul i ulong 4
350b0 = uint get g_sha_blk o
351b1 = uint get g_sha_blk add o ulong 1
352b2 = uint get g_sha_blk add o ulong 2
353b3 = uint get g_sha_blk add o ulong 3
354w = or or shl b0 uint 24 shl b1 uint 16 or shl b2 uint 8 b3
355put g_sha_w i w
356end
357i = ulong 16
358while lt i ulong 64 bound 48
359w15 = get g_sha_w sub i ulong 15
360w2 = get g_sha_w sub i ulong 2
361s0 = xor xor rotr w15 uint 7 rotr w15 uint 18 shr w15 uint 3
362s1 = xor xor rotr w2 uint 17 rotr w2 uint 19 shr w2 uint 10
363w16 = get g_sha_w sub i ulong 16
364w7 = get g_sha_w sub i ulong 7
365put g_sha_w i add add w16 s0 add w7 s1
366i = add i ulong 1
367end
368end
369
370fn sha256_step_t1 e uint f uint g uint h uint idx ulong to uint
371s1 = xor xor rotr e uint 6 rotr e uint 11 rotr e uint 25
372ch = xor and e f and not e g
373kw = add get g_sha_k idx get g_sha_w idx
374ret add add h s1 add ch kw
375end
376
377fn sha256_step_t2 a uint b uint c uint to uint
378s0 = xor xor rotr a uint 2 rotr a uint 13 rotr a uint 22
379maj = xor xor and a b and a c and b c
380ret add s0 maj
381end
382
383fn sha256_compress to void
384sha256_load_w
385a = get g_sha_h 0
386b = get g_sha_h 1
387c = get g_sha_h 2
388d = get g_sha_h 3
389e = get g_sha_h 4
390f = get g_sha_h 5
391g = get g_sha_h 6
392h = get g_sha_h 7
393for i ulong 64
394t1 = sha256_step_t1 e f g h i
395t2 = sha256_step_t2 a b c
396h = g
397g = f
398f = e
399e = add d t1
400d = c
401c = b
402b = a
403a = add t1 t2
404end
405put g_sha_h 0 add get g_sha_h 0 a
406put g_sha_h 1 add get g_sha_h 1 b
407put g_sha_h 2 add get g_sha_h 2 c
408put g_sha_h 3 add get g_sha_h 3 d
409put g_sha_h 4 add get g_sha_h 4 e
410put g_sha_h 5 add get g_sha_h 5 f
411put g_sha_h 6 add get g_sha_h 6 g
412put g_sha_h 7 add get g_sha_h 7 h
413end
414
415fn sha256_blk_zero to void
416for i ulong 64
417put g_sha_blk i uchar 0
418end
419end
420
421fn sha256_put_len bits ulong to void
422for i ulong 8
423sh = mul sub ulong 7 i ulong 8
424v = and shr bits sh ulong 255
425put g_sha_blk add ulong 56 i uchar v
426end
427end
428
429fn sha256_digest msg view uchar * digest edit uchar 32 to void
430sha256_reset
431n = len msg
432if gt n ulong 65536
433n = ulong 65536
434end
435off = ulong 0
436note bound 1025 covers up to 65536 bytes in 64-byte blocks
437while le add off ulong 64 n bound 1025
438for j ulong 64
439put g_sha_blk j get msg add off j
440end
441sha256_compress
442off = add off ulong 64
443end
444sha256_blk_zero
445remn = sub n off
446j = ulong 0
447while lt j remn bound 64
448put g_sha_blk j get msg add off j
449j = add j ulong 1
450end
451put g_sha_blk remn uchar 0x80
452if gt remn ulong 55
453sha256_compress
454sha256_blk_zero
455end
456bits = mul n ulong 8
457sha256_put_len bits
458sha256_compress
459for i ulong 8
460w = get g_sha_h i
461o = mul i ulong 4
462put digest o uchar and shr w uint 24 uint 255
463put digest add o ulong 1 uchar and shr w uint 16 uint 255
464put digest add o ulong 2 uchar and shr w uint 8 uint 255
465put digest add o ulong 3 uchar and w uint 255
466end
467end
468
469fn sha256_hex msg view uchar * hexbuf edit uchar 64 to void
470dig = new uchar 32 0
471sha256_digest msg dig
472for i ulong 32
473b = ulong get dig i
474hi = shr b ulong 4
475lo = and b ulong 15
476put hexbuf mul i ulong 2 get txt_hex_digits hi
477put hexbuf add mul i ulong 2 ulong 1 get txt_hex_digits lo
478end
479end
480
481fn key_fingerprint key_str view uchar * hexbuf edit uchar 64 to void
482s = trim_start key_str
483e = trim_end key_str
484if ge s e
485sha256_hex part key_str 0 0 hexbuf
486ret
487end
488sha256_hex part key_str s sub e s hexbuf
489end
490
491note --- Base58BTC, RID, DID, and Public Key validation ---
492
493fn is_base58btc b uchar to bool
494if and ge b uchar 49 le b uchar 57
495ret true
496end
497if and ge b uchar 65 le b uchar 72
498ret true
499end
500if and ge b uchar 74 le b uchar 78
501ret true
502end
503if and ge b uchar 80 le b uchar 90
504ret true
505end
506if and ge b uchar 97 le b uchar 107
507ret true
508end
509if and ge b uchar 109 le b uchar 122
510ret true
511end
512ret false
513end
514
515fn is_base64_ch b uchar to bool
516if and ge b uchar 65 le b uchar 90
517ret true
518end
519if and ge b uchar 97 le b uchar 122
520ret true
521end
522if and ge b uchar 48 le b uchar 57
523ret true
524end
525if eq b uchar 43
526ret true
527end
528if eq b uchar 47
529ret true
530end
531if eq b uchar 61
532ret true
533end
534ret false
535end
536
537fn validate_rid rid view uchar * to ulong
538n = len rid
539note Real Radicle RID is rad:z + 27..58 base58btc characters (total 32..63)
540if lt n ulong 32
541ret RAD_ERR_BAD_RID
542end
543if gt n ulong 63
544ret RAD_ERR_BAD_RID
545end
546if not slice_starts_with rid txt_rad_pfx
547ret RAD_ERR_BAD_RID
548end
549i = ulong 5
550while lt i n bound 64
551if not is_base58btc get rid i
552ret RAD_ERR_BAD_RID
553end
554i = add i ulong 1
555end
556ret RAD_OK
557end
558
559fn validate_delegate_did did view uchar * to ulong
560n = len did
561note Ed25519 did:key:z6Mk... has 12-byte prefix + 42..46 base58btc chars (total 54..58)
562if lt n ulong 54
563ret RAD_ERR_BAD_DID
564end
565if gt n ulong 58
566ret RAD_ERR_BAD_DID
567end
568if not slice_starts_with did txt_did_key_pfx
569ret RAD_ERR_BAD_DID
570end
571i = ulong 12
572while lt i n bound 64
573if not is_base58btc get did i
574ret RAD_ERR_BAD_DID
575end
576i = add i ulong 1
577end
578ret RAD_OK
579end
580
581fn validate_anchor_did did view uchar * to ulong
582n = len did
583if lt n ulong 10
584ret RAD_ERR_BAD_DID
585end
586if gt n ulong 63
587ret RAD_ERR_BAD_DID
588end
589if slice_starts_with did txt_did_key_pfx
590ret validate_delegate_did did
591end
592if not slice_starts_with did txt_did_rad_pfx
593ret RAD_ERR_BAD_DID
594end
595i = ulong 8
596while lt i n bound 64
597b = get did i
598ok = false
599if and ge b uchar 97 le b uchar 122
600ok = true
601end
602if and ge b uchar 65 le b uchar 90
603ok = true
604end
605if and ge b uchar 48 le b uchar 57
606ok = true
607end
608if or eq b uchar 58 or eq b uchar 45 eq b uchar 95
609ok = true
610end
611if not ok
612ret RAD_ERR_BAD_DID
613end
614i = add i ulong 1
615end
616ret RAD_OK
617end
618
619fn validate_ssh_ed25519 key_str view uchar * to ulong
620s = trim_start key_str
621e = trim_end key_str
622if ge s e
623ret RAD_ERR_BAD_KEY
624end
625tn = sub e s
626if gt tn ulong 1024
627ret RAD_ERR_BAD_KEY
628end
629if not slice_starts_with part key_str s tn txt_ssh_pfx
630ret RAD_ERR_BAD_KEY
631end
632p = add s ulong 12
633while lt p e bound 1024
634if not is_space_b get key_str p
635break
636end
637p = add p ulong 1
638end
639tok_start = p
640while lt p e bound 1024
641if is_space_b get key_str p
642break
643end
644if not is_base64_ch get key_str p
645ret RAD_ERR_BAD_KEY
646end
647p = add p ulong 1
648end
649tok_len = sub p tok_start
650if lt tok_len ulong 32
651ret RAD_ERR_BAD_KEY
652end
653ret RAD_OK
654end
655
656fn validate_radicle_key key_str view uchar * to ulong
657s = trim_start key_str
658e = trim_end key_str
659if ge s e
660ret RAD_ERR_BAD_KEY
661end
662tn = sub e s
663if gt tn ulong 1024
664ret RAD_ERR_BAD_KEY
665end
666if slice_starts_with part key_str s tn txt_ssh_pfx
667ret validate_ssh_ed25519 part key_str s tn
668end
669if slice_starts_with part key_str s tn txt_rad_pfx
670ret validate_rid part key_str s tn
671end
672if slice_starts_with part key_str s tn txt_did_key_pfx
673ret validate_delegate_did part key_str s tn
674end
675if lt tn ulong 32
676ret RAD_ERR_BAD_KEY
677end
678i = s
679while lt i e bound 1024
680if is_space_b get key_str i
681ret RAD_ERR_BAD_KEY
682end
683i = add i ulong 1
684end
685ret RAD_OK
686end
687
688note --- Threshold and quorum validation ---
689
690fn check_quorum del_n ulong thresh ulong approvals ulong to ulong
691if eq del_n ulong 0
692ret RAD_ERR_BAD_THRESH
693end
694if gt del_n RAD_MAX_DEL
695ret RAD_ERR_TOO_MANY
696end
697if eq thresh ulong 0
698ret RAD_ERR_BAD_THRESH
699end
700if gt thresh del_n
701ret RAD_ERR_BAD_THRESH
702end
703if lt approvals thresh
704ret RAD_ERR_NO_QUORUM
705end
706ret RAD_OK
707end
708
709note --- Base & Remote Identity Document management ---
710
711fn base_doc_reset to void
712put g_base_rid_len 0 ulong 0
713put g_base_del_n 0 ulong 0
714put g_base_thresh 0 ulong 0
715end
716
717fn remote_doc_reset to void
718put g_rem_rid_len 0 ulong 0
719put g_rem_del_n 0 ulong 0
720put g_rem_thresh 0 ulong 0
721end
722
723fn base_doc_set_rid rid view uchar * to ulong
724n = len rid
725if gt n RAD_RID_CAP
726ret RAD_ERR_BAD_RID
727end
728i = ulong 0
729while lt i n bound 64
730put g_base_rid i get rid i
731i = add i ulong 1
732end
733put g_base_rid_len 0 n
734ret RAD_OK
735end
736
737fn remote_doc_set_rid rid view uchar * to ulong
738n = len rid
739if gt n RAD_RID_CAP
740ret RAD_ERR_BAD_RID
741end
742i = ulong 0
743while lt i n bound 64
744put g_rem_rid i get rid i
745i = add i ulong 1
746end
747put g_rem_rid_len 0 n
748ret RAD_OK
749end
750
751fn base_doc_set_threshold t ulong to void
752put g_base_thresh 0 t
753end
754
755fn remote_doc_set_threshold t ulong to void
756put g_rem_thresh 0 t
757end
758
759fn base_doc_add_delegate did view uchar * to ulong
760idx = get g_base_del_n 0
761if ge idx RAD_MAX_DEL
762ret RAD_ERR_TOO_MANY
763end
764n = len did
765if gt n RAD_DID_SLOT
766ret RAD_ERR_BAD_DID
767end
768base = mul idx RAD_DID_SLOT
769i = ulong 0
770while lt i n bound 64
771put g_base_dels add base i get did i
772i = add i ulong 1
773end
774put g_base_del_lens idx n
775put g_base_del_n 0 add idx ulong 1
776ret RAD_OK
777end
778
779fn remote_doc_add_delegate did view uchar * to ulong
780idx = get g_rem_del_n 0
781if ge idx RAD_MAX_DEL
782ret RAD_ERR_TOO_MANY
783end
784n = len did
785if gt n RAD_DID_SLOT
786ret RAD_ERR_BAD_DID
787end
788base = mul idx RAD_DID_SLOT
789i = ulong 0
790while lt i n bound 64
791put g_rem_dels add base i get did i
792i = add i ulong 1
793end
794put g_rem_del_lens idx n
795put g_rem_del_n 0 add idx ulong 1
796ret RAD_OK
797end
798
799fn base_del_eq_idx a ulong b ulong to bool
800la = get g_base_del_lens a
801lb = get g_base_del_lens b
802if ne la lb
803ret false
804end
805oa = mul a RAD_DID_SLOT
806ob = mul b RAD_DID_SLOT
807i = ulong 0
808while lt i la bound 64
809if ne get g_base_dels add oa i get g_base_dels add ob i
810ret false
811end
812i = add i ulong 1
813end
814ret true
815end
816
817fn rem_del_eq_idx a ulong b ulong to bool
818la = get g_rem_del_lens a
819lb = get g_rem_del_lens b
820if ne la lb
821ret false
822end
823oa = mul a RAD_DID_SLOT
824ob = mul b RAD_DID_SLOT
825i = ulong 0
826while lt i la bound 64
827if ne get g_rem_dels add oa i get g_rem_dels add ob i
828ret false
829end
830i = add i ulong 1
831end
832ret true
833end
834
835fn base_rem_del_eq bi ulong ri ulong to bool
836lb = get g_base_del_lens bi
837lr = get g_rem_del_lens ri
838if ne lb lr
839ret false
840end
841ob = mul bi RAD_DID_SLOT
842or_off = mul ri RAD_DID_SLOT
843i = ulong 0
844while lt i lb bound 64
845if ne get g_base_dels add ob i get g_rem_dels add or_off i
846ret false
847end
848i = add i ulong 1
849end
850ret true
851end
852
853fn rids_match to bool
854lb = get g_base_rid_len 0
855lr = get g_rem_rid_len 0
856if ne lb lr
857ret false
858end
859i = ulong 0
860while lt i lb bound 64
861if ne get g_base_rid i get g_rem_rid i
862ret false
863end
864i = add i ulong 1
865end
866ret true
867end
868
869fn validate_base_doc to ulong
870rl = get g_base_rid_len 0
871if eq rl ulong 0
872ret RAD_ERR_BAD_RID
873end
874rbuf = new uchar 64 0
875i = ulong 0
876while lt i rl bound 64
877put rbuf i get g_base_rid i
878i = add i ulong 1
879end
880rc = validate_rid part rbuf 0 rl
881if ne rc RAD_OK
882ret rc
883end
884dn = get g_base_del_n 0
885th = get g_base_thresh 0
886if eq dn ulong 0
887ret RAD_ERR_BAD_THRESH
888end
889if gt dn RAD_MAX_DEL
890ret RAD_ERR_TOO_MANY
891end
892if or eq th ulong 0 gt th dn
893ret RAD_ERR_BAD_THRESH
894end
895dbuf = new uchar 64 0
896d = ulong 0
897while lt d dn bound 16
898dl = get g_base_del_lens d
899off = mul d RAD_DID_SLOT
900j = ulong 0
901while lt j dl bound 64
902put dbuf j get g_base_dels add off j
903j = add j ulong 1
904end
905dc = validate_delegate_did part dbuf 0 dl
906if ne dc RAD_OK
907ret dc
908end
909k = add d ulong 1
910while lt k dn bound 16
911if base_del_eq_idx d k
912ret RAD_ERR_DUP_DEL
913end
914k = add k ulong 1
915end
916d = add d ulong 1
917end
918ret RAD_OK
919end
920
921fn validate_remote_doc to ulong
922rl = get g_rem_rid_len 0
923if eq rl ulong 0
924ret RAD_ERR_BAD_RID
925end
926rbuf = new uchar 64 0
927i = ulong 0
928while lt i rl bound 64
929put rbuf i get g_rem_rid i
930i = add i ulong 1
931end
932rc = validate_rid part rbuf 0 rl
933if ne rc RAD_OK
934ret rc
935end
936dn = get g_rem_del_n 0
937th = get g_rem_thresh 0
938if eq dn ulong 0
939ret RAD_ERR_BAD_THRESH
940end
941if gt dn RAD_MAX_DEL
942ret RAD_ERR_TOO_MANY
943end
944if or eq th ulong 0 gt th dn
945ret RAD_ERR_BAD_THRESH
946end
947dbuf = new uchar 64 0
948d = ulong 0
949while lt d dn bound 16
950dl = get g_rem_del_lens d
951off = mul d RAD_DID_SLOT
952j = ulong 0
953while lt j dl bound 64
954put dbuf j get g_rem_dels add off j
955j = add j ulong 1
956end
957dc = validate_delegate_did part dbuf 0 dl
958if ne dc RAD_OK
959ret dc
960end
961k = add d ulong 1
962while lt k dn bound 16
963if rem_del_eq_idx d k
964ret RAD_ERR_DUP_DEL
965end
966k = add k ulong 1
967end
968d = add d ulong 1
969end
970ret RAD_OK
971end
972
973fn rad_doc_count_common to ulong
974bn = get g_base_del_n 0
975rn = get g_rem_del_n 0
976common_n = ulong 0
977bi = ulong 0
978while lt bi bn bound 16
979found = false
980ri = ulong 0
981while lt ri rn bound 16
982if base_rem_del_eq bi ri
983found = true
984break
985end
986ri = add ri ulong 1
987end
988if found
989common_n = add common_n ulong 1
990end
991bi = add bi ulong 1
992end
993ret common_n
994end
995
996fn rad_doc_diff_sync approvals ulong allow_add ulong diff edit RadDiff to void
997put diff code RAD_OK
998put diff decision SYNC_REFUSED
999put diff added ulong 0
1000put diff removed ulong 0
1001put diff common ulong 0
1002put diff thresh_chg ulong 0
1003vc = validate_base_doc
1004if ne vc RAD_OK
1005put diff code vc
1006ret
1007end
1008vc = validate_remote_doc
1009if ne vc RAD_OK
1010put diff code vc
1011ret
1012end
1013if not rids_match
1014put diff code RAD_ERR_RID_MISMATCH
1015ret
1016end
1017bn = get g_base_del_n 0
1018rn = get g_rem_del_n 0
1019common_n = rad_doc_count_common
1020rem_n = sub bn common_n
1021add_n = sub rn common_n
1022tchg = ulong 0
1023if ne get g_base_thresh 0 get g_rem_thresh 0
1024tchg = ulong 1
1025end
1026put diff added add_n
1027put diff removed rem_n
1028put diff common common_n
1029put diff thresh_chg tchg
1030any_chg = add add add_n rem_n tchg
1031if eq any_chg ulong 0
1032put diff code RAD_OK
1033put diff decision SYNC_IN_SYNC
1034ret
1035end
1036if and gt add_n ulong 0 eq allow_add ulong 0
1037put diff code RAD_ERR_UNAUTH_DEL
1038put diff decision SYNC_REFUSED
1039ret
1040end
1041vc = check_quorum bn get g_base_thresh 0 approvals
1042if ne vc RAD_OK
1043put diff code vc
1044put diff decision SYNC_REFUSED
1045ret
1046end
1047put diff code RAD_OK
1048if or gt add_n ulong 0 gt rem_n ulong 0
1049put diff decision SYNC_ROTATED
1050else
1051put diff decision SYNC_UPDATE_OK
1052end
1053end
1054
1055note --- Identity Anchor (upstream identity.rs + keys.rs) ---
1056
1057fn anchor_init_default to void
1058dn = len txt_def_did
1059i = ulong 0
1060while lt i dn bound 64
1061put g_anc_did i get txt_def_did i
1062i = add i ulong 1
1063end
1064put g_anc_did_len 0 dn
1065nn = len txt_def_name
1066j = ulong 0
1067while lt j nn bound 64
1068put g_anc_name j get txt_def_name j
1069j = add j ulong 1
1070end
1071put g_anc_name_len 0 nn
1072put g_anc_etype 0 ENTITY_AGENT
1073put g_anc_rad_len 0 ulong 0
1074put g_anc_ssh_len 0 ulong 0
1075put g_anc_hist_n 0 ulong 0
1076for k ulong 64
1077put g_anc_rad_fp k uchar 0
1078put g_anc_ssh_fp k uchar 0
1079end
1080end
1081
1082fn anchor_set_identity did view uchar * name view uchar * etype ulong to ulong
1083dc = validate_anchor_did did
1084if ne dc RAD_OK
1085ret dc
1086end
1087dn = len did
1088nn = len name
1089if or eq nn ulong 0 gt nn ulong 63
1090ret RAD_ERR_PARSE
1091end
1092i = ulong 0
1093while lt i dn bound 64
1094put g_anc_did i get did i
1095i = add i ulong 1
1096end
1097put g_anc_did_len 0 dn
1098j = ulong 0
1099while lt j nn bound 64
1100put g_anc_name j get name j
1101j = add j ulong 1
1102end
1103put g_anc_name_len 0 nn
1104put g_anc_etype 0 etype
1105ret RAD_OK
1106end
1107
1108fn anchor_rad_matches key_str view uchar * to bool
1109cur = get g_anc_rad_len 0
1110if ne cur len key_str
1111ret false
1112end
1113i = ulong 0
1114while lt i cur bound 256
1115if ne get g_anc_rad_key i get key_str i
1116ret false
1117end
1118i = add i ulong 1
1119end
1120ret true
1121end
1122
1123fn anchor_ssh_matches key_str view uchar * to bool
1124cur = get g_anc_ssh_len 0
1125if ne cur len key_str
1126ret false
1127end
1128i = ulong 0
1129while lt i cur bound 256
1130if ne get g_anc_ssh_key i get key_str i
1131ret false
1132end
1133i = add i ulong 1
1134end
1135ret true
1136end
1137
1138fn anchor_sync_rad_key rad_key view uchar * st edit AnchorSync to ulong
1139fp = new uchar 64 0
1140rs = trim_start rad_key
1141re = trim_end rad_key
1142if ge rs re
1143ret RAD_OK
1144end
1145rlen = sub re rs
1146if gt rlen RAD_KEY_CAP
1147ret RAD_ERR_BAD_KEY
1148end
1149rc = validate_radicle_key part rad_key rs rlen
1150if ne rc RAD_OK
1151ret rc
1152end
1153if not anchor_rad_matches part rad_key rs rlen
1154if gt get g_anc_rad_len 0 ulong 0
1155put st rotated ulong 1
1156end
1157put st rad_chg ulong 1
1158i = ulong 0
1159while lt i rlen bound 256
1160put g_anc_rad_key i get rad_key add rs i
1161i = add i ulong 1
1162end
1163put g_anc_rad_len 0 rlen
1164key_fingerprint part rad_key rs rlen fp
1165for f ulong 64
1166put g_anc_rad_fp f get fp f
1167end
1168put g_anc_hist_n 0 add get g_anc_hist_n 0 ulong 1
1169end
1170ret RAD_OK
1171end
1172
1173fn anchor_sync_ssh_key ssh_key view uchar * st edit AnchorSync to ulong
1174fp = new uchar 64 0
1175ss = trim_start ssh_key
1176se = trim_end ssh_key
1177if ge ss se
1178ret RAD_OK
1179end
1180slen = sub se ss
1181if gt slen RAD_KEY_CAP
1182ret RAD_ERR_BAD_KEY
1183end
1184sc = validate_ssh_ed25519 part ssh_key ss slen
1185if ne sc RAD_OK
1186ret sc
1187end
1188if not anchor_ssh_matches part ssh_key ss slen
1189if gt get g_anc_ssh_len 0 ulong 0
1190put st rotated ulong 1
1191end
1192put st ssh_chg ulong 1
1193j = ulong 0
1194while lt j slen bound 256
1195put g_anc_ssh_key j get ssh_key add ss j
1196j = add j ulong 1
1197end
1198put g_anc_ssh_len 0 slen
1199key_fingerprint part ssh_key ss slen fp
1200for f ulong 64
1201put g_anc_ssh_fp f get fp f
1202end
1203put g_anc_hist_n 0 add get g_anc_hist_n 0 ulong 1
1204end
1205ret RAD_OK
1206end
1207
1208fn anchor_sync_keys rad_key view uchar * ssh_key view uchar * st edit AnchorSync to void
1209put st code RAD_OK
1210put st in_sync ulong 0
1211put st rotated ulong 0
1212put st rad_chg ulong 0
1213put st ssh_chg ulong 0
1214put st hist_n get g_anc_hist_n 0
1215rc = anchor_sync_rad_key rad_key st
1216if ne rc RAD_OK
1217put st code rc
1218ret
1219end
1220sc = anchor_sync_ssh_key ssh_key st
1221if ne sc RAD_OK
1222put st code sc
1223ret
1224end
1225if or gt get g_anc_rad_len 0 ulong 0 gt get g_anc_ssh_len 0 ulong 0
1226put st in_sync ulong 1
1227end
1228put st hist_n get g_anc_hist_n 0
1229end
1230
1231fn anchor_render_json st view AnchorSync dst edit uchar * to ulong
1232p = ulong 0
1233p = append_bytes dst p txt_j_open
1234dl = get g_anc_did_len 0
1235i = ulong 0
1236while lt i dl bound 64
1237p = append_byte dst p get g_anc_did i
1238i = add i ulong 1
1239end
1240p = append_bytes dst p txt_j_name
1241nl = get g_anc_name_len 0
1242j = ulong 0
1243while lt j nl bound 64
1244p = append_byte dst p get g_anc_name j
1245j = add j ulong 1
1246end
1247p = append_bytes dst p txt_j_insync
1248if eq get st in_sync ulong 1
1249p = append_bytes dst p txt_true
1250else
1251p = append_bytes dst p txt_false
1252end
1253p = append_bytes dst p txt_j_rot
1254if eq get st rotated ulong 1
1255p = append_bytes dst p txt_true
1256else
1257p = append_bytes dst p txt_false
1258end
1259p = append_bytes dst p txt_j_hist
1260p = append_u64 dst p get st hist_n
1261p = append_bytes dst p txt_j_radfp
1262if gt get g_anc_rad_len 0 ulong 0
1263for k ulong 64
1264p = append_byte dst p get g_anc_rad_fp k
1265end
1266end
1267p = append_bytes dst p txt_j_sshfp
1268if gt get g_anc_ssh_len 0 ulong 0
1269for k2 ulong 64
1270p = append_byte dst p get g_anc_ssh_fp k2
1271end
1272end
1273p = append_bytes dst p txt_j_msg
1274if eq get st in_sync ulong 0
1275p = append_bytes dst p txt_msg_none
1276else
1277if eq get st rotated ulong 1
1278if and eq get st rad_chg ulong 1 eq get st ssh_chg ulong 1
1279p = append_bytes dst p txt_msg_both_rot
1280else
1281if eq get st rad_chg ulong 1
1282p = append_bytes dst p txt_msg_rad_rot
1283else
1284p = append_bytes dst p txt_msg_ssh_rot
1285end
1286end
1287else
1288p = append_bytes dst p txt_msg_synced
1289end
1290end
1291p = append_bytes dst p txt_j_close
1292ret p
1293end
1294
1295fn parse_u64_strict s view uchar * ok edit ulong 1 to ulong
1296put ok 0 ulong 0
1297n = len s
1298if or eq n ulong 0 gt n ulong 18
1299ret ulong 0
1300end
1301acc = ulong 0
1302i = ulong 0
1303while lt i n bound 20
1304b = get s i
1305if or lt b uchar 48 gt b uchar 57
1306ret ulong 0
1307end
1308acc = add mul acc ulong 10 ulong sub b uchar 48
1309i = add i ulong 1
1310end
1311put ok 0 ulong 1
1312ret acc
1313end