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 / x25519 / driver.om

315 lines · 6503 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note driver.om: test driver for om/tls/x25519.om (program mode). run.sh writes the line om2, then
3note x25519.om, then this file. Reads a vector file on stdin, one case per line:
4note X R KHEX UHEX OHEX x25519_scalarmult(k, u) must return R (0 or 1) and write OHEX
5note B KHEX OHEX x25519_base(k) must return 0 and write OHEX
6note I N OHEX RFC 7748 section 5.2 iteration from k = u = 9, N >= 1 steps
7note L length checks: five calls with a 31- or 33-byte o, k or u must return 2
8note T N KHEX UHEX timing: N calls of x25519_scalarmult, prints TIME line L n N ns T
9note Lines starting with # and empty lines are skipped. Prints one MISMATCH line per wrong result
10note and the summary line: cases C calls V mismatches X ns T (T = ns spent in checked calls).
11note Exit 0 only when at least one checked call ran and none mismatched; 1 otherwise; 65 bad input.
12
13text XCTMIS "MISMATCH line "
14text XCTGOT " got rc "
15text XCTWANT " want rc "
16text XCTCASE "cases "
17text XCTCALL " calls "
18text XCTMM " mismatches "
19text XCTNS " ns "
20text XCTTIME "TIME line "
21text XCTN " n "
22text XCTNL "\n"
23text XCTBAD "driver: bad input\n"
24
25global XCIN uchar 16777216
26global XCLEN ulong 1
27global XCOUT uchar 32
28global XCCNT ulong 8
29
30fn x25519t_num v ulong to void
31k = ulong 0
32x = v
33while true bound 30
34put XCOUT sub 31 k uchar add rem x 10 48
35k = add k 1
36x = div x 10
37if eq x 0
38break
39end
40end
41r = write 1 XCOUT sub 32 k k
42end
43
44fn x25519t_bad to void
45r = write 2 XCTBAD 0 len XCTBAD
46exit 65
47end
48
49fn x25519t_nib c ulong to ulong
50if and ge c 48 le c 57
51ret sub c 48
52end
53if and ge c 97 le c 102
54ret sub c 87
55end
56x25519t_bad
57ret 0
58end
59
60note 32 bytes from 64 hex digits at XCIN[o..], followed by a space or the newline
61fn x25519t_hex a edit uchar 32 o ulong to void
62for i ulong 32
63hi = x25519t_nib ulong get XCIN add o mul i 2
64lo = x25519t_nib ulong get XCIN add add o mul i 2 1
65put a i uchar or shl hi 4 lo
66end
67c = get XCIN add o 64
68if and ne c 32 ne c 10
69x25519t_bad
70end
71end
72
73note decimal number at XCIN[o..] up to a space or newline
74fn x25519t_dec o ulong to ulong
75v = ulong 0
76p = o
77while and ge get XCIN p 48 le get XCIN p 57 bound 20
78v = add mul v 10 sub ulong get XCIN p 48
79p = add p 1
80end
81if or eq p o and ne get XCIN p 32 ne get XCIN p 10
82x25519t_bad
83end
84ret v
85end
86
87note position after the next space at or after o (before e)
88fn x25519t_fld o ulong e ulong to ulong
89p = o
90while ne get XCIN p 32 bound 30
91if ge p e
92x25519t_bad
93end
94p = add p 1
95end
96ret add p 1
97end
98
99fn x25519t_bump i ulong v ulong to void
100put XCCNT i add get XCCNT i v
101end
102
103note counters in XCCNT: 0 line, 1 cases, 2 calls, 3 mismatches, 4 ns
104fn x25519t_mis got ulong want ulong to void
105x25519t_bump 3 1
106r = write 1 XCTMIS 0 len XCTMIS
107x25519t_num get XCCNT 0
108r = write 1 XCTGOT 0 len XCTGOT
109x25519t_num got
110r = write 1 XCTWANT 0 len XCTWANT
111x25519t_num want
112r = write 1 XCTNL 0 1
113end
114
115note one checked result: rc got against want, bytes o against x
116fn x25519t_check got ulong want ulong o view uchar * x view uchar 32 to void
117x25519t_bump 2 1
118d = ulong 0
119for i ulong 32
120d = or d ulong xor get o i get x i
121end
122if or ne got want ne d 0
123x25519t_mis got want
124end
125end
126
127fn x25519t_x p ulong e ulong to void
128k = new uchar 32 0
129u = new uchar 32 0
130o = new uchar 32 0
131x = new uchar 32 0
132if ne sub e p 198
133x25519t_bad
134end
135want = x25519t_nib ulong get XCIN add p 2
136x25519t_hex k add p 4
137x25519t_hex u add p 69
138x25519t_hex x add p 134
139t0 = monons
140got = x25519_scalarmult o k u
141x25519t_bump 4 ulong sub monons t0
142x25519t_check got want o x
143end
144
145fn x25519t_b p ulong e ulong to void
146k = new uchar 32 0
147o = new uchar 32 0
148x = new uchar 32 0
149if ne sub e p 131
150x25519t_bad
151end
152x25519t_hex k add p 2
153x25519t_hex x add p 67
154t0 = monons
155got = x25519_base o k
156x25519t_bump 4 ulong sub monons t0
157x25519t_check got 0 o x
158end
159
160note one iteration step: o = X25519(k, u), u = k, k = o; returns the rc
161fn x25519t_step k edit uchar 32 u edit uchar 32 o edit uchar 32 to ulong
162r = x25519_scalarmult o k u
163for i ulong 32
164put u i get k i
165put k i get o i
166end
167ret r
168end
169
170fn x25519t_i p ulong e ulong to void
171k = new uchar 32 0
172u = new uchar 32 0
173o = new uchar 32 0
174x = new uchar 32 0
175put k 0 9
176put u 0 9
177n = x25519t_dec add p 2
178q = x25519t_fld add p 2 e
179if or eq n 0 ne sub e q 64
180x25519t_bad
181end
182x25519t_hex x q
183bad = ulong 0
184t0 = monons
185for a ulong div n 1000
186for b ulong 1000
187bad = or bad x25519t_step k u o
188end
189end
190for b ulong rem n 1000
191bad = or bad x25519t_step k u o
192end
193x25519t_bump 4 ulong sub monons t0
194x25519t_check bad 0 k x
195end
196
197fn x25519t_l to void
198o = new uchar 32 0
199k = new uchar 32 7
200u = new uchar 32 9
201s = new uchar 31 7
202g = new uchar 33 7
203x = new uchar 32 0
204x25519t_check x25519_scalarmult s k u 2 o x
205x25519t_check x25519_scalarmult o s u 2 o x
206x25519t_check x25519_scalarmult o k g 2 o x
207x25519t_check x25519_base g k 2 o x
208x25519t_check x25519_base o s 2 o x
209end
210
211fn x25519t_t p ulong e ulong to void
212k = new uchar 32 0
213u = new uchar 32 0
214o = new uchar 32 0
215n = x25519t_dec add p 2
216q = x25519t_fld add p 2 e
217if ne sub e q 129
218x25519t_bad
219end
220x25519t_hex k q
221x25519t_hex u add q 65
222t0 = monons
223for i ulong n
224r = x25519_scalarmult o k u
225end
226dt = ulong sub monons t0
227r = write 1 XCTTIME 0 len XCTTIME
228x25519t_num get XCCNT 0
229r = write 1 XCTN 0 len XCTN
230x25519t_num n
231r = write 1 XCTNS 0 len XCTNS
232x25519t_num dt
233r = write 1 XCTNL 0 1
234end
235
236fn x25519t_slurp to void
237t = ulong 0
238while true bound 999999
239room = sub len XCIN t
240if eq room 0
241x25519t_bad
242end
243r = read 0 XCIN t room
244if lt r 0
245x25519t_bad
246end
247if eq r 0
248break
249end
250t = add t ulong r
251end
252put XCLEN 0 t
253end
254
255note the case on XCIN[p..e), e at its newline
256fn x25519t_line p ulong e ulong to void
257c = get XCIN p
258if or eq e p eq c 35
259ret
260end
261x25519t_bump 1 1
262if eq c 88
263x25519t_x p e
264ret
265end
266if eq c 66
267x25519t_b p e
268ret
269end
270if eq c 73
271x25519t_i p e
272ret
273end
274if and eq c 76 eq sub e p 1
275x25519t_l
276ret
277end
278if eq c 84
279x25519t_bump 1 0
280x25519t_t p e
281ret
282end
283x25519t_bad
284end
285
286fn main to int
287x25519t_slurp
288tot = get XCLEN 0
289if or eq tot 0 ne get XCIN sub tot 1 10
290x25519t_bad
291end
292p = ulong 0
293while lt p tot bound 999999
294x25519t_bump 0 1
295e = p
296while ne get XCIN e 10 bound 999999
297e = add e 1
298end
299x25519t_line p e
300p = add e 1
301end
302r = write 1 XCTCASE 0 len XCTCASE
303x25519t_num get XCCNT 1
304r = write 1 XCTCALL 0 len XCTCALL
305x25519t_num get XCCNT 2
306r = write 1 XCTMM 0 len XCTMM
307x25519t_num get XCCNT 3
308r = write 1 XCTNS 0 len XCTNS
309x25519t_num get XCCNT 4
310r = write 1 XCTNL 0 1
311if or gt get XCCNT 3 0 eq get XCCNT 2 0
312ret 1
313end
314ret 0
315end