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 / ec / p256_driver.om

306 lines · 6193 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note p256_driver.om: test driver for the P-256 p2_* section of om/tls/ec.om (program mode). run.sh
3note writes the line om2, then ec.om, then this file. Reads a vector file on stdin, one case per line; H is 64 lowercase hex
4note digits (a 256-bit number, big-endian), fields separated by one space:
5note M A B R p2_mul: A * B mod p must be R
6note S A R p2_sqr: A^2 mod p must be R
7note A A B R p2_add: A + B mod p must be R (A, B < p)
8note U A B R p2_sub: A - B mod p must be R (A, B < p)
9note I A R p2_inv: A^(p-2) mod p must be R
10note K k X Y RX RY F p2_smul: k * (X, Y) must be (RX, RY) with finite flag F (0 or 1)
11note D u v X Y RX RY F p2_smul2: u * G + v * (X, Y) must be (RX, RY) with flag F
12note T N k X Y timing: N calls of p2_smul, prints TIME smul n N ns T
13note W N u v X Y timing: N calls of p2_smul2, prints TIME smul2 n N ns T
14note F N A B timing: N calls of p2_mul, prints TIME mul n N ns T
15note Lines starting with # and empty lines are skipped. Prints one MISMATCH line per wrong result
16note and the summary line: cases C calls V mismatches X. Exit 0 only when at least one checked call
17note ran and none mismatched; 1 otherwise; 65 bad input.
18
19text P2TMIS "MISMATCH line "
20text P2TCASE "cases "
21text P2TCALL " calls "
22text P2TMM " mismatches "
23text P2TTIME "TIME "
24text P2TSM "smul"
25text P2TSM2 "smul2"
26text P2TMUL "mul"
27text P2TN " n "
28text P2TNS " ns "
29text P2TNL "\n"
30text P2TBAD "driver: bad input\n"
31
32global P2IN uchar 16777216
33global P2LEN ulong 1
34global P2OUT uchar 32
35global P2CNT ulong 8
36
37fn p2t_num v ulong to void
38k = ulong 0
39x = v
40while true bound 30
41put P2OUT sub 31 k uchar add rem x 10 48
42k = add k 1
43x = div x 10
44if eq x 0
45break
46end
47end
48r = write 1 P2OUT sub 32 k k
49end
50
51fn p2t_bad to void
52r = write 2 P2TBAD 0 len P2TBAD
53exit 65
54end
55
56fn p2t_nib c ulong to ulong
57if and ge c 48 le c 57
58ret sub c 48
59end
60if and ge c 97 le c 102
61ret sub c 87
62end
63p2t_bad
64ret 0
65end
66
67note field f (0-based) of the line at p: 64 hex digits at p + 2 + 65 f, then a space or newline,
68note into 8 little-endian 32-bit limbs at a[o..]
69fn p2t_hex a edit ulong * o ulong p ulong f ulong to void
70s = add add p 2 mul f 65
71for j ulong 8
72v = ulong 0
73q = add s mul sub 7 j 8
74for d ulong 8
75c = ulong get P2IN add q d
76v = or shl v 4 p2t_nib c
77end
78put a add o j v
79end
80c = get P2IN add s 64
81if and ne c 32 ne c 10
82p2t_bad
83end
84end
85
86note decimal number at P2IN[o..] up to a space; returns it, position after the space in P2CNT 7
87fn p2t_dec o ulong to ulong
88v = ulong 0
89p = o
90while and ge get P2IN p 48 le get P2IN p 57 bound 20
91v = add mul v 10 sub ulong get P2IN p 48
92p = add p 1
93end
94if or eq p o ne get P2IN p 32
95p2t_bad
96end
97put P2CNT 7 add p 1
98ret v
99end
100
101fn p2t_bump i ulong v ulong to void
102put P2CNT i add get P2CNT i v
103end
104
105note compare n limbs of the workspace at o with x[0..n) and the flags got, want
106fn p2t_check w view ulong P2WN x view ulong 24 o ulong n ulong got ulong want ulong to void
107p2t_bump 2 1
108d = xor got want
109for j ulong n
110d = or d xor get w add o j get x j
111end
112if ne d 0
113p2t_bump 3 1
114r = write 1 P2TMIS 0 len P2TMIS
115p2t_num get P2CNT 0
116r = write 1 P2TNL 0 1
117end
118end
119
120fn p2t_time what view uchar * n ulong t ulong to void
121r = write 1 P2TTIME 0 len P2TTIME
122r = write 1 what 0 len what
123r = write 1 P2TN 0 len P2TN
124p2t_num n
125r = write 1 P2TNS 0 len P2TNS
126p2t_num t
127r = write 1 P2TNL 0 1
128end
129
130note expected width of a line with k hex fields after the op letter
131fn p2t_need p ulong e ulong k ulong to void
132if ne sub e p add 1 mul k 65
133p2t_bad
134end
135end
136
137fn p2t_line p ulong e ulong w edit ulong P2WN x edit ulong 24 to void
138c = get P2IN p
139if or eq e p eq c 35
140ret
141end
142p2t_bump 1 1
143if eq c 77
144p2t_need p e 3
145p2t_hex w 0 p 0
146p2t_hex w 8 p 1
147p2t_hex x 0 p 2
148p2_mul w 16 0 8
149p2t_check w x 16 8 0 0
150ret
151end
152if eq c 83
153p2t_need p e 2
154p2t_hex w 0 p 0
155p2t_hex x 0 p 1
156p2_sqr w 16 0
157p2t_check w x 16 8 0 0
158ret
159end
160if eq c 65
161p2t_need p e 3
162p2t_hex w 0 p 0
163p2t_hex w 8 p 1
164p2t_hex x 0 p 2
165p2_add w 16 0 8
166p2t_check w x 16 8 0 0
167ret
168end
169if eq c 85
170p2t_need p e 3
171p2t_hex w 0 p 0
172p2t_hex w 8 p 1
173p2t_hex x 0 p 2
174p2_sub w 16 0 8
175p2t_check w x 16 8 0 0
176ret
177end
178if eq c 73
179p2t_need p e 2
180p2t_hex w 0 p 0
181p2t_hex x 0 p 1
182p2_inv w 16 0
183p2t_check w x 16 8 0 0
184ret
185end
186if eq c 75
187if ne sub e p 328
188p2t_bad
189end
190p2t_hex w P2K p 0
191p2t_hex w P2P p 1
192p2t_hex w add P2P 8 p 2
193p2t_hex x 0 p 3
194p2t_hex x 8 p 4
195want = p2t_nib ulong get P2IN add p 327
196got = p2_smul w
197p2t_check w x P2O 16 got want
198ret
199end
200if eq c 68
201if ne sub e p 393
202p2t_bad
203end
204p2t_hex w P2K p 0
205p2t_hex w P2U p 1
206p2t_hex w P2Q p 2
207p2t_hex w add P2Q 8 p 3
208p2t_hex x 0 p 4
209p2t_hex x 8 p 5
210want = p2t_nib ulong get P2IN add p 392
211got = p2_smul2 w
212p2t_check w x P2O 16 got want
213ret
214end
215if or or eq c 84 eq c 87 eq c 70
216p2t_bump 1 0
217n = p2t_dec add p 2
218q = sub sub get P2CNT 7 p 2
219if eq c 84
220p2t_need add p q e 3
221p2t_hex w P2K add p q 0
222p2t_hex w P2P add p q 1
223p2t_hex w add P2P 8 add p q 2
224t0 = monons
225for i ulong n
226r = p2_smul w
227end
228p2t_time P2TSM n ulong sub monons t0
229ret
230end
231if eq c 87
232p2t_need add p q e 4
233p2t_hex w P2K add p q 0
234p2t_hex w P2U add p q 1
235p2t_hex w P2Q add p q 2
236p2t_hex w add P2Q 8 add p q 3
237t0 = monons
238for i ulong n
239r = p2_smul2 w
240end
241p2t_time P2TSM2 n ulong sub monons t0
242ret
243end
244p2t_need add p q e 2
245p2t_hex w 0 add p q 0
246p2t_hex w 8 add p q 1
247t0 = monons
248for i ulong n
249p2_mul w 16 0 8
250end
251p2t_time P2TMUL n ulong sub monons t0
252ret
253end
254p2t_bad
255end
256
257fn p2t_slurp to void
258t = ulong 0
259while true bound 999999
260room = sub len P2IN t
261if eq room 0
262p2t_bad
263end
264r = read 0 P2IN t room
265if lt r 0
266p2t_bad
267end
268if eq r 0
269break
270end
271t = add t ulong r
272end
273put P2LEN 0 t
274end
275
276fn main to int
277w = new ulong P2WN 0
278x = new ulong 24 0
279p2_init w
280p2t_slurp
281tot = get P2LEN 0
282if or eq tot 0 ne get P2IN sub tot 1 10
283p2t_bad
284end
285p = ulong 0
286while lt p tot bound 999999
287p2t_bump 0 1
288e = p
289while ne get P2IN e 10 bound 999999
290e = add e 1
291end
292p2t_line p e w x
293p = add e 1
294end
295r = write 1 P2TCASE 0 len P2TCASE
296p2t_num get P2CNT 1
297r = write 1 P2TCALL 0 len P2TCALL
298p2t_num get P2CNT 2
299r = write 1 P2TMM 0 len P2TMM
300p2t_num get P2CNT 3
301r = write 1 P2TNL 0 1
302if or gt get P2CNT 3 0 eq get P2CNT 2 0
303ret 1
304end
305ret 0
306end