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 / om / tls / test / ecdh / driver.om

382 lines · 7792 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note driver.om: test driver for om/tls/ecdh.om (program mode), concatenated after ec.om and ecdh.om.
3note Reads a vector file on stdin (see gen_vectors.py), one case per line:
4note ID OP RC DHEX PHEX XHEX ('-' = empty hex field, '#' comment)
5note OP S: ecdh_shared(d, p, z) with len z = len x; must return exactly RC and write x
6note OP P: ecdh_pub(d, q) with len q = len x; must return exactly RC and write x (zeros when RC != 0)
7note OP C: ecdh_pct(d, p); must return exactly RC
8note Controls for every RC 0 case. S: the last byte of p flipped must give exactly 3 (off-curve),
9note d with its last byte xor 1 must not give x, a 1-byte-shorter d must give 5 and a 1-byte-longer
10note z must give 2. P: ecdh_pct(d, x) must give 0 and with the last byte of x flipped exactly 6.
11note Optional argument: repeat count R (timing; each call runs R times). Prints one MISMATCH line
12note per wrong result and the summary line:
13note cases C calls V controls K mismatches X ns T codes c0 c1 c2 c3 c4 c5 c6
14note Exit 0 only when at least one call ran and none mismatched; 1 otherwise; 65 bad input.
15
16text txmis "MISMATCH line "
17text txgot " got "
18text txwhy " "
19text txctl "control"
20text txver "call"
21text txcase "cases "
22text txvers " calls "
23text txctls " controls "
24text txmm " mismatches "
25text txns " ns "
26text txcod " codes"
27text txpct "pct"
28text txz "bytes"
29text txsp " "
30text txnl "\n"
31text txbad "driver: bad input\n"
32
33global etin uchar 16777216
34global etout uchar 32
35global etarg uchar 32
36note etc: 0 line, 1 cases, 2 calls, 3 mismatches, 4 ns, 5 repeats, 6 pos, 7 controls,
37note 8..14 code counts 0..6, 17 op (83 S, 80 P, 67 C), 18 expected code, 20 d len, 21 p len,
38note 22 x len, 24 id start, 25 id end, 26 input length
39global etc ulong 32
40
41fn etnum v ulong to void
42k = ulong 0
43x = v
44while true bound 30
45put etout sub 31 k uchar add rem x 10 48
46k = add k 1
47x = div x 10
48if eq x 0
49break
50end
51end
52r = write 1 etout sub 32 k k
53end
54
55fn etdie to void
56r = write 2 txbad 0 len txbad
57exit 65
58end
59
60fn etnib c ulong to ulong
61if and ge c 48 le c 57
62ret sub c 48
63end
64if and ge c 97 le c 102
65ret sub c 87
66end
67etdie
68ret 0
69end
70
71fn etslurp to void
72t = ulong 0
73while true bound 999999
74room = sub len etin t
75if eq room 0
76etdie
77end
78r = read 0 etin t room
79if lt r 0
80etdie
81end
82if eq r 0
83break
84end
85t = add t ulong r
86end
87put etc 26 t
88end
89
90fn etreps to ulong
91if lt argcnt 2
92ret 1
93end
94l = argread 1 etarg 0 20
95v = ulong 0
96for i ulong ulong l
97v = add mul v 10 sub ulong get etarg i 48
98end
99ret v
100end
101
102note end of the token at the current position (stops at a space or newline); the token must be
103note non-empty
104fn etend to ulong
105p = get etc 6
106e = p
107while lt e get etc 26 bound 999999
108c = get etin e
109if or eq c 32 eq c 10
110break
111end
112e = add e 1
113end
114if eq e p
115etdie
116end
117ret e
118end
119
120note step past the token ending at e and one following space (a newline is left in place)
121fn etnext e ulong to void
122if lt e get etc 26
123if eq get etin e 32
124e = add e 1
125end
126end
127put etc 6 e
128end
129
130fn etdec to ulong
131e = etend
132v = ulong 0
133for i ulong sub e get etc 6
134c = ulong get etin add get etc 6 i
135if or lt c 48 gt c 57
136etdie
137end
138v = add mul v 10 sub c 48
139end
140etnext e
141ret v
142end
143
144fn etbyte p ulong to uchar
145hi = etnib ulong get etin p
146lo = etnib ulong get etin add p 1
147ret uchar or shl hi 4 lo
148end
149
150note hex token into a[0..); returns the byte count ('-' is empty)
151fn ethex a edit uchar * to ulong
152e = etend
153p = get etc 6
154if and eq sub e p 1 eq get etin p 45
155etnext e
156ret 0
157end
158h = sub e p
159if ne and h 1 0
160etdie
161end
162c = div h 2
163if gt c len a
164etdie
165end
166for i ulong c
167put a i etbyte add p mul i 2
168end
169etnext e
170ret c
171end
172
173fn etbump i ulong v ulong to void
174put etc i add get etc i v
175end
176
177fn etmis why view uchar * got ulong to void
178r = write 1 txmis 0 len txmis
179etnum get etc 0
180r = write 1 txsp 0 1
181r = write 1 etin get etc 24 sub get etc 25 get etc 24
182r = write 1 txwhy 0 1
183r = write 1 why 0 len why
184r = write 1 txgot 0 len txgot
185etnum got
186r = write 1 txnl 0 1
187etbump 3 1
188end
189
190
191note one library call for the case's OP (etc 17); the output is z[0..etc 22)
192fn etcall d view uchar * p view uchar * z edit uchar * to ulong
193op = get etc 17
194if eq op 83
195ret ecdh_shared d p part z 0 get etc 22
196end
197if eq op 80
198ret ecdh_pub d part z 0 get etc 22
199end
200ret ecdh_pct d p
201end
202
203note parse one case line into d, p and the expected bytes x
204fn etparse d edit uchar * p edit uchar * x edit uchar * to void
205put etc 24 get etc 6
206e = etend
207put etc 25 e
208etnext e
209e = etend
210if ne sub e get etc 6 1
211etdie
212end
213c = ulong get etin get etc 6
214if and and ne c 83 ne c 80 ne c 67
215etdie
216end
217put etc 17 c
218etnext e
219put etc 18 etdec
220put etc 20 ethex d
221put etc 21 ethex p
222put etc 22 ethex x
223if ne get etin get etc 6 10
224etdie
225end
226end
227
228fn etzero o edit uchar * to void
229for i ulong len o
230put o i 0
231end
232end
233
234note bytes o[0..n) against x[0..n): 0 equal, else 1
235fn etdiff o view uchar * x view uchar * to ulong
236k = ulong 0
237for i ulong len x
238k = or k ulong xor get o i get x i
239end
240if eq k 0
241ret 0
242end
243ret 1
244end
245
246fn etctl got ulong want ulong to void
247etbump 7 1
248if ne got want
249etmis txctl got
250end
251end
252
253note S controls (expected code 0), on views of exact length: peer flipped must give 3, scalar
254note changed must not give x, a short d must give 5, a long z must give 2
255fn etcs1 d view uchar * p edit uchar * to void
256o = new uchar 64 0
257np = len p
258put p sub np 1 xor get p sub np 1 1
259got = ecdh_shared d p part o 0 len d
260put p sub np 1 xor get p sub np 1 1
261etctl got 3
262end
263
264fn etcs2 d edit uchar * p view uchar * x view uchar * to void
265o = new uchar 64 0
266nd = len d
267put d sub nd 1 xor get d sub nd 1 1
268got = ecdh_shared d p part o 0 nd
269put d sub nd 1 xor get d sub nd 1 1
270etbump 7 1
271if eq got 0
272if eq etdiff part o 0 nd x 0
273etmis txctl 0
274end
275end
276end
277
278fn etcs3 d view uchar * p view uchar * to void
279o = new uchar 64 0
280nd = len d
281got = ecdh_shared part d 0 sub nd 1 p part o 0 nd
282etctl got 5
283got = ecdh_shared d p part o 0 add nd 1
284etctl got 2
285end
286
287note P controls (expected code 0): pct of the expected key passes, a flipped key fails with 6
288fn etcp d view uchar * x edit uchar * to void
289nx = len x
290got = ecdh_pct d x
291etctl got 0
292put x sub nx 1 xor get x sub nx 1 1
293got = ecdh_pct d x
294put x sub nx 1 xor get x sub nx 1 1
295etctl got 6
296end
297
298note run the case R times (etc 5), keeping the last code in etc 19
299fn etrun d view uchar * p view uchar * o edit uchar * to void
300for k ulong get etc 5
301etzero o
302t0 = monons
303put etc 19 etcall d p o
304etbump 4 ulong sub monons t0
305etbump 2 1
306end
307end
308
309fn etchk o view uchar * x view uchar * to void
310got = get etc 19
311if le got 6
312etbump add 8 got 1
313end
314if ne got get etc 18
315etmis txver got
316end
317if eq etdiff o x 1
318etmis txz got
319end
320end
321
322fn etcase d edit uchar * p edit uchar * x edit uchar * to void
323o = new uchar 128 0
324etbump 1 1
325etparse d p x
326etrun part d 0 get etc 20 part p 0 get etc 21 o
327etchk part o 0 get etc 22 part x 0 get etc 22
328if eq get etc 18 0
329nd = get etc 20
330np = get etc 21
331nx = get etc 22
332if eq get etc 17 83
333etcs1 part d 0 nd part p 0 np
334etcs2 part d 0 nd part p 0 np part x 0 nx
335etcs3 part d 0 nd part p 0 np
336end
337if eq get etc 17 80
338etcp part d 0 nd part x 0 nx
339end
340end
341end
342
343fn main to int
344d = new uchar 64 0
345p = new uchar 256 0
346x = new uchar 128 0
347etslurp
348put etc 5 etreps
349tot = get etc 26
350while lt get etc 6 tot bound 999999
351etbump 0 1
352q = get etc 6
353if ne get etin q 35
354etcase d p x
355end
356e = get etc 6
357while and lt e tot ne get etin e 10 bound 999999
358e = add e 1
359end
360put etc 6 add e 1
361end
362r = write 1 txcase 0 len txcase
363etnum get etc 1
364r = write 1 txvers 0 len txvers
365etnum get etc 2
366r = write 1 txctls 0 len txctls
367etnum get etc 7
368r = write 1 txmm 0 len txmm
369etnum get etc 3
370r = write 1 txns 0 len txns
371etnum get etc 4
372r = write 1 txcod 0 len txcod
373for i ulong 7
374r = write 1 txsp 0 1
375etnum get etc add 8 i
376end
377r = write 1 txnl 0 1
378if or gt get etc 3 0 eq get etc 2 0
379ret 1
380end
381ret 0
382end