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 / synapse / claims / src / 50_event.om

322 lines · 5660 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note Event serialisation, hashing, batched signing through synapse-claims-sig, and the locked append.
3
4text mkid "AIEN_CLAIMS_EVENT_V1\n"
5text tbrace "}"
6text msig "signing failed (synapse-claims-sig)"
7text mapp "ledger append failed"
8text mlock "lock failed"
9text mhome "home not initialised; run init"
10text tsign "sign"
11text tver "verify"
12text tkeyg "keygen"
13
14note gev cells: the event being built
15const eop ulong 0
16const eid ulong 1
17const eag ulong 2
18const eagl ulong 3
19const ese ulong 4
20const esel ulong 5
21const ekind ulong 6
22const etg ulong 7
23const etgl ulong 8
24const emode ulong 9
25const ettl ulong 10
26const eexp ulong 11
27const est ulong 12
28const ein ulong 13
29const einl ulong 14
30const eev ulong 15
31const eevl ulong 16
32const eor ulong 17
33const eorl ulong 18
34const eidp ulong 19
35
36note ======== line writer: s[gx xlp ..] ========
37
38fn l_b s edit uchar * c uchar to void
39p = get gx xlp
40put s p c
41put gx xlp add p 1
42end
43
44fn l_t s edit uchar * t view uchar * to void
45for i ulong len t
46l_b s get t i
47end
48end
49
50fn l_ar s edit uchar * off ulong n ulong to void
51for i ulong n
52l_b s ar_g add off i
53end
54end
55
56fn l_arq s edit uchar * off ulong n ulong to void
57l_b s 34
58l_ar s off n
59l_b s 34
60end
61
62fn l_u s edit uchar * v ulong to void
63x = v
64k = ulong 0
65for i ulong 20
66put gst i uchar add 48 rem x 10
67x = div x 10
68k = add k 1
69if eq x 0
70break
71end
72end
73for j ulong k
74l_b s get gst sub sub k j 1
75end
76end
77
78fn l_ts s edit uchar * v ulong to void
79ts_fmt v
80l_b s 34
81for i ulong 20
82l_b s get gtb i
83end
84l_b s 34
85end
86
87fn l_wd s edit uchar * w ulong to void
88l_b s 34
89p = tw_off wall w
90for i ulong tw_len wall w
91l_b s get wall add p i
92end
93l_b s 34
94end
95
96fn l_id s edit uchar * v ulong to void
97for i ulong 16
98l_b s hexd and shr v sub 60 mul i 4 15
99end
100end
101
102note ev_hash: sha256(tag ++ s[0..hp) ++ "}") as lowercase hex into s[hp+9 .. hp+73)
103fn ev_hash s edit uchar * hp ulong to void
104c = new ulong 40 0
105d = new uchar 32 0
106sha256_init c
107sha256_update c mkid
108sha256_update c view part s 0 hp
109sha256_update c tbrace
110sha256_final c d
111q = add hp 9
112for i ulong 32
113put s add q mul i 2 hexd ulong shr get d i 4
114put s add add q mul i 2 1 hexd ulong and get d i 15
115end
116end
117
118note ev_body: writes the event body (through prev) at s[0..); returns hp. seq = xseq + 1.
119fn ev_body s edit uchar * to ulong
120put gx xlp 0
121l_t s kv
122l_u s add get gx xseq 1
123l_t s kts
124l_ts s get gx xnow
125l_t s kop
126l_wd s get gev eop
127l_t s kid
128l_b s 34
129put gev eidp get gx xlp
130l_id s get gev eid
131l_b s 34
132l_t s kag
133l_arq s get gev eag get gev eagl
134l_t s kse
135l_arq s get gev ese get gev esel
136l_t s kkind
137l_wd s add get gev ekind 13
138l_t s ktg
139l_arq s get gev etg get gev etgl
140l_t s kmode
141l_wd s add get gev emode 18
142l_t s kttl
143l_u s get gev ettl
144l_t s kexp
145l_ts s get gev eexp
146l_t s kst
147l_wd s add get gev est 5
148l_t s kin
149l_arq s get gev ein get gev einl
150l_t s kev
151l_ar s get gev eev get gev eevl
152l_b s 93
153l_t s kor
154l_arq s get gev eor get gev eorl
155l_t s kprev
156l_b s 34
157for i ulong 64
158l_b s get glh i
159end
160l_b s 34
161ret get gx xlp
162end
163
164note ev_build: complete line (sig = 128 zeros, patched at flush); for op=claim the id is the first 16
165note hex of the hash of the body with id 0000000000000000. Returns the line length.
166fn ev_build s edit uchar * to ulong
167if eq get gev eop 0
168put gev eid 0
169end
170hp = ev_body s
171put gx xlp hp
172l_t s khash
173l_b s 34
174for i ulong 64
175l_b s 48
176end
177l_b s 34
178l_t s ksig
179l_b s 34
180for j ulong 128
181l_b s 48
182end
183l_b s 34
184l_b s 125
185n = get gx xlp
186if eq get gev eop 0
187ev_hash s hp
188put gev eid hx_u s add hp 9
189p = get gev eidp
190for k ulong 16
191put s add p k get s add add hp 9 k
192end
193end
194ev_hash s hp
195ret n
196end
197
198note ======== pending batch (gpend, ends in gpe) ========
199
200fn pend_add s edit uchar * n ulong to void
201p = get gx xpen
202k = get gx xpec
203if or gt add add p n 1 pecap ge k 8192
204die 70 mapp
205end
206for i ulong n
207put gpend add p i get s i
208end
209put gpend add p n 10
210e = add add p n 1
211put gpe k e
212put gx xpen e
213put gx xpec add k 1
214h = sub e 204
215for j ulong 64
216put glh j get gpend add h j
217end
218put gx xseq add get gx xseq 1
219end
220
221fn pe_room to bool
222if ge get gx xpec 8192
223ret false
224end
225ret le add get gx xpen 70000 pecap
226end
227
228fn pk_g off ulong n ulong to void
229p = pk_rec n
230for i ulong n
231put gpk add p i get gpth add off i
232end
233end
234
235note pe_flush: sign every pending hash in one helper run, patch the signatures, then append each line
236note with a single write() and fdsync. echo true also prints each appended line on stdout.
237fn pe_flush echo bool to void
238k = get gx xpec
239if eq k 0
240ret
241end
242si_open
243for i ulong k
244h = sub get gpe i 204
245for j ulong 64
246si_b get gpend add h j
247end
248si_b 10
249end
250pk_reset
251pk_g get gx xsig get gx xsigl
252pk_t tsign
253pk_g get gx xpkey get gx xpkeyl
254r = si_run
255if ne r 0
256die 70 msig
257end
258if ne get gx xson mul k 129
259die 70 msig
260end
261for a ulong k
262sp = sub get gpe a 131
263for b ulong 128
264c = get gso add mul a 129 b
265if eq hexv c 16
266die 70 msig
267end
268put gpend add sp b c
269end
270if ne get gso add mul a 129 128 10
271die 70 msig
272end
273end
274fd = fopen gpth get gx xpled get gx xpledl 525377 420
275if lt fd 0
276die 70 mapp
277end
278st = ulong 0
279for e ulong k
280en = get gpe e
281p = st
282while lt p en bound 999999
283w = write int fd gpend p sub en p
284if le w 0
285die 70 mapp
286end
287p = add p ulong w
288end
289if echo
290for x ulong sub en st
291o_b get gpend add st x
292end
293end
294st = en
295end
296y = fdsync int fd
297if ne y 0
298die 70 mapp
299end
300z = fclose int fd
301put gx xpen 0
302put gx xpec 0
303end
304
305note lk_take: open claims.lock and flock it (1 shared, 2 exclusive); held until exit
306fn lk_take op int to void
307fd = fopen gpth get gx xplk get gx xplkl 524354 420
308if eq fd -2
309if eq op 1
310ret
311end
312die 70 mhome
313end
314if lt fd 0
315die 70 mlock
316end
317r = flock int fd op
318if ne r 0
319die 70 mlock
320end
321put gx xlkfd ulong fd
322end