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

1213 lines · 33927 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note om/tls/ec.om: ECDSA signature verification on NIST P-256 and P-384 (FIPS 186-4 section 6.4.2).
3note Library unit (no om2 line, no main). Module code EC: functions ec_* and ecdsa_*, consts EC*.
4note
5note API (all inputs are public; this module is variable time by design, see "Timing" below):
6note ecdsa_verify crv pub hs hlen crv 256 or 384; pub = 0x04 || X || Y (SEC1 uncompressed);
7note hs = digest (hlen bytes) || DER Ecdsa-Sig-Value
8note ecdsa_verify_raw crv pub hs hlen same, signature r || s, each 32 (P-256) or 48 (P-384) bytes
9note Return codes: 0 valid, 1 signature mismatch (computed point at infinity included),
10note 2 bad DER or encoding (also hlen > len hs), 3 bad public key (format, coordinate >= p,
11note not on the curve), 4 r or s not in [1, n-1], 5 unsupported crv.
12note The digest may have any length: e = its leftmost min(hlen, 32|48) bytes (bits2int; the bit
13note length of n is 256 or 384, byte aligned), reduced mod n.
14note
15note Arithmetic: 32-bit limbs held in ulong cells, little-endian, L = 8 (P-256) or 12 (P-384).
16note Generic Montgomery multiplication (CIOS) for p and for n; n0' = -m^-1 mod 2^32 (Newton
17note iteration), R mod m and R^2 mod m (R = 2^(32L), by repeated doubling) are computed at run
18note time. Points in Jacobian coordinates (a = -3), Z = 0 is the point at infinity. Shamir's
19note joint double-scalar multiplication u1*G + u2*Q with the table G, Q, G+Q.
20note
21note Timing: signature verification handles only public values (key, digest, signature), so this
22note module is NOT constant time and does not try to be: DER parsing, range checks, the Shamir
23note loop (branches on the bits of u1 and u2), point addition (branches on Z = 0, H = 0, r = 0),
24note ec_csub (branch on the borrow), ec_cmp (early exit) and the Fermat inversions (branch on the
25note exponent bits of the public moduli) all take data-dependent time. Do not reuse these helpers
26note on secret data (e.g. ECDH or signing) without a constant-time rewrite. Compiler caveat: omc
27note emits plain AArch64 and no DIT bit is set. No secret is held, so nothing is wiped.
28note Untrusted input never traps: every length and offset is checked before indexing (fuzzed by
29note test/ec/gen_fuzz.py).
30note
31note Workspace w (ECWN ulong cells) layout, slot stride 12 cells:
32note 0 L; 2..15 Montgomery accumulator T (L+2 cells)
33note ECCP 16 / ECCN 56: modulus context: +0 m, +12 n0', +16 R mod m, +28 R^2 mod m
34note ECZ zero, ECON plain 1, ECB curve b (Montgomery), points ECG ECQ ECGQ ECR (X,Y,Z),
35note scalars ECSR r, ECSS s, ECE e, ECU1, ECU2, ECSI s^-1, temporaries ECA0..ECA9, ECAC.
36
37const ECWN ulong 480
38const ECCP ulong 16
39const ECCN ulong 56
40const ECZ ulong 96
41const ECON ulong 108
42const ECB ulong 120
43const ECG ulong 132
44const ECQ ulong 168
45const ECGQ ulong 204
46const ECR ulong 240
47const ECSR ulong 276
48const ECSS ulong 288
49const ECE ulong 300
50const ECU1 ulong 312
51const ECU2 ulong 324
52const ECSI ulong 336
53const ECA0 ulong 348
54const ECA1 ulong 360
55const ECA2 ulong 372
56const ECA3 ulong 384
57const ECA4 ulong 396
58const ECA5 ulong 408
59const ECA6 ulong 420
60const ECA7 ulong 432
61const ECA8 ulong 444
62const ECA9 ulong 456
63const ECAC ulong 468
64
65note Curve constants, verbatim from FIPS 186-4 Appendix D.1.2.3 (P-256) and D.1.2.4 (P-384)
66note (same values as SEC 2 v2 sections 2.4.2 secp256r1 and 2.5.1 secp384r1). a = -3 for both.
67note Checked with Wolfram (WolframLanguageEvaluator, 2026-10-26; exact query and output in
68note test/ec/wolfram.txt): PrimeQ[p], PrimeQ[n], Mod[Gy^2-(Gx^3-3Gx+b),p]==0, n*G == O (affine
69note double-and-add mod p), BitLength[p]==BitLength[n] (256, 384), n<p<2n: all True for both curves.
70fn ec_p256 w edit ulong ECWN to void
71put w 0 8
72note p = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff (limbs little-endian)
73put w 16 0xffffffff
74put w 17 0xffffffff
75put w 18 0xffffffff
76put w 19 0x0
77put w 20 0x0
78put w 21 0x0
79put w 22 0x1
80put w 23 0xffffffff
81note n = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551 (limbs little-endian)
82put w 56 0xfc632551
83put w 57 0xf3b9cac2
84put w 58 0xa7179e84
85put w 59 0xbce6faad
86put w 60 0xffffffff
87put w 61 0xffffffff
88put w 62 0x0
89put w 63 0xffffffff
90note b = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b (limbs little-endian)
91put w 120 0x27d2604b
92put w 121 0x3bce3c3e
93put w 122 0xcc53b0f6
94put w 123 0x651d06b0
95put w 124 0x769886bc
96put w 125 0xb3ebbd55
97put w 126 0xaa3a93e7
98put w 127 0x5ac635d8
99note gx = 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296 (limbs little-endian)
100put w 132 0xd898c296
101put w 133 0xf4a13945
102put w 134 0x2deb33a0
103put w 135 0x77037d81
104put w 136 0x63a440f2
105put w 137 0xf8bce6e5
106put w 138 0xe12c4247
107put w 139 0x6b17d1f2
108note gy = 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5 (limbs little-endian)
109put w 144 0x37bf51f5
110put w 145 0xcbb64068
111put w 146 0x6b315ece
112put w 147 0x2bce3357
113put w 148 0x7c0f9e16
114put w 149 0x8ee7eb4a
115put w 150 0xfe1a7f9b
116put w 151 0x4fe342e2
117end
118
119fn ec_p384 w edit ulong ECWN to void
120put w 0 12
121note p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff (limbs little-endian)
122put w 16 0xffffffff
123put w 17 0x0
124put w 18 0x0
125put w 19 0xffffffff
126put w 20 0xfffffffe
127put w 21 0xffffffff
128put w 22 0xffffffff
129put w 23 0xffffffff
130put w 24 0xffffffff
131put w 25 0xffffffff
132put w 26 0xffffffff
133put w 27 0xffffffff
134note n = 0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973 (limbs little-endian)
135put w 56 0xccc52973
136put w 57 0xecec196a
137put w 58 0x48b0a77a
138put w 59 0x581a0db2
139put w 60 0xf4372ddf
140put w 61 0xc7634d81
141put w 62 0xffffffff
142put w 63 0xffffffff
143put w 64 0xffffffff
144put w 65 0xffffffff
145put w 66 0xffffffff
146put w 67 0xffffffff
147note b = 0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef (limbs little-endian)
148put w 120 0xd3ec2aef
149put w 121 0x2a85c8ed
150put w 122 0x8a2ed19d
151put w 123 0xc656398d
152put w 124 0x5013875a
153put w 125 0x314088f
154put w 126 0xfe814112
155put w 127 0x181d9c6e
156put w 128 0xe3f82d19
157put w 129 0x988e056b
158put w 130 0xe23ee7e4
159put w 131 0xb3312fa7
160note gx = 0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7 (limbs little-endian)
161put w 132 0x72760ab7
162put w 133 0x3a545e38
163put w 134 0xbf55296c
164put w 135 0x5502f25d
165put w 136 0x82542a38
166put w 137 0x59f741e0
167put w 138 0x8ba79b98
168put w 139 0x6e1d3b62
169put w 140 0xf320ad74
170put w 141 0x8eb1c71e
171put w 142 0xbe8b0537
172put w 143 0xaa87ca22
173note gy = 0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f (limbs little-endian)
174put w 144 0x90ea0e5f
175put w 145 0x7a431d7c
176put w 146 0x1d7e819d
177put w 147 0xa60b1ce
178put w 148 0xb5f0b8c0
179put w 149 0xe9da3113
180put w 150 0x289a147c
181put w 151 0xf8f41dbd
182put w 152 0x9292dc29
183put w 153 0x5d9e98bf
184put w 154 0x96262c6f
185put w 155 0x3617de4a
186end
187
188note T[0..L+1] += a[0..L-1] * y (a at w offset a, y < 2^32); each step fits: (2^32-1) +
189note (2^32-1)^2 + (2^32-1) = 2^64 - 1
190fn ec_row w edit ulong ECWN a ulong y ulong to void
191n = get w 0
192c = ulong 0
193x = ulong 0
194for j ulong n
195x = add add get w add 2 j mul get w add a j y c
196put w add 2 j and x 0xffffffff
197c = shr x 32
198end
199x = add get w add 2 n c
200put w add 2 n and x 0xffffffff
201put w add 3 n add get w add 3 n shr x 32
202end
203
204note o = T - m if T >= m else T (T has L+1 limbs, T < 2m); modulus m at offset c
205fn ec_csub w edit ulong ECWN c ulong o ulong to void
206n = get w 0
207br = ulong 0
208x = ulong 0
209for j ulong n
210x = sub sub get w add 2 j get w add c j br
211br = shr x 63
212end
213x = sub get w add 2 n br
214if eq shr x 63 0
215br = 0
216for j ulong n
217x = sub sub get w add 2 j get w add c j br
218put w add o j and x 0xffffffff
219br = shr x 63
220end
221ret
222end
223for j ulong n
224put w add o j get w add 2 j
225end
226end
227
228note T = a + b as L+1 limbs
229fn ec_sum w edit ulong ECWN a ulong b ulong to void
230n = get w 0
231cy = ulong 0
232x = ulong 0
233for j ulong n
234x = add add get w add a j get w add b j cy
235put w add 2 j and x 0xffffffff
236cy = shr x 32
237end
238put w add 2 n cy
239end
240
241note o = a + b mod m (a, b < m; also a < 2m with b = 0 reduces a)
242fn ec_add w edit ulong ECWN c ulong o ulong a ulong b ulong to void
243ec_sum w a b
244ec_csub w c o
245end
246
247note o = a - b mod m (a, b < m): T = a + m - b, in (0, 2m), then one conditional subtraction
248fn ec_sub w edit ulong ECWN c ulong o ulong a ulong b ulong to void
249ec_sum w a c
250n = get w 0
251br = ulong 0
252x = ulong 0
253for j ulong n
254x = sub sub get w add 2 j get w add b j br
255put w add 2 j and x 0xffffffff
256br = shr x 63
257end
258put w add 2 n sub get w add 2 n br
259ec_csub w c o
260end
261
262note one CIOS step: T = (T + a*y + q*m) / 2^32 with q = (T + a*y)[0] * n0' mod 2^32
263fn ec_step w edit ulong ECWN c ulong a ulong y ulong to void
264ec_row w a y
265q = and mul get w 2 get w add c 12 0xffffffff
266ec_row w c q
267n = get w 0
268for j ulong add n 1
269put w add 2 j get w add 3 j
270end
271put w add 3 n 0
272end
273
274note o = a * b * R^-1 mod m (Montgomery, CIOS; a, b < m). o may alias a or b.
275fn ec_mul w edit ulong ECWN c ulong o ulong a ulong b ulong to void
276n = get w 0
277for j ulong add n 2
278put w add 2 j 0
279end
280for i ulong n
281ec_step w c a get w add b i
282end
283ec_csub w c o
284end
285
286note compare L-limb numbers: 0 equal, 1 a < b, 2 a > b
287fn ec_cmp w edit ulong ECWN a ulong b ulong to ulong
288n = get w 0
289for k ulong n
290j = sub sub n 1 k
291x = get w add a j
292y = get w add b j
293if lt x y
294ret 1
295end
296if gt x y
297ret 2
298end
299end
300ret 0
301end
302
303fn ec_copy w edit ulong ECWN o ulong a ulong to void
304for j ulong get w 0
305put w add o j get w add a j
306end
307end
308
309note copy a point (three slots)
310fn ec_pcopy w edit ulong ECWN o ulong a ulong to void
311for j ulong 36
312put w add o j get w add a j
313end
314end
315
316note bit i of the L-limb number at a
317fn ec_bit w edit ulong ECWN a ulong i ulong to ulong
318ret and shr get w add a shr i 5 and i 31 1
319end
320
321note modulus context at c (m already stored): n0' = -m^-1 mod 2^32 by Newton iteration
322note (x = m0 is right to 3 bits; each step doubles the bits: 3, 6, 12, 24, 48 >= 32),
323note R mod m = 2^(32L) mod m and R^2 mod m by repeated modular doubling from 1.
324fn ec_ctx w edit ulong ECWN c ulong to void
325n = get w 0
326m0 = get w c
327x = m0
328for k ulong 5
329x = and mul x sub 2 mul m0 x 0xffffffff
330end
331put w add c 12 and sub 0 x 0xffffffff
332for j ulong n
333put w add add c 16 j 0
334end
335put w add c 16 1
336for k ulong mul n 32
337ec_add w c add c 16 add c 16 add c 16
338end
339ec_copy w add c 28 add c 16
340for k ulong mul n 32
341ec_add w c add c 28 add c 28 add c 28
342end
343end
344
345note o = a^(m-2) (Montgomery domain; m prime, so a^-1 for a != 0). m - 2: m0 >= 2 for all four
346note moduli, so only limb 0 changes.
347fn ec_inv w edit ulong ECWN c ulong o ulong a ulong to void
348ec_copy w ECAC add c 16
349nb = mul get w 0 32
350for k ulong nb
351i = sub sub nb 1 k
352ec_mul w c ECAC ECAC ECAC
353v = get w add c shr i 5
354if eq shr i 5 0
355v = sub v 2
356end
357if eq and shr v and i 31 1 1
358ec_mul w c ECAC ECAC a
359end
360end
361ec_copy w o ECAC
362end
363
364note load the big-endian bytes of b (len b <= 4L) as an L-limb number at o
365fn ec_load w edit ulong ECWN o ulong b view uchar * to void
366for j ulong get w 0
367put w add o j 0
368end
369k = len b
370for i ulong k
371bv = shl ulong get b sub sub k i 1 mul and i 3 8
372li = add o shr i 2
373put w li or get w li bv
374end
375end
376
377note P = 2P in place, a = -3 (dbl-2001-b). Infinity (Z = 0) stays infinity.
378fn ec_dbl w edit ulong ECWN p ulong to void
379y = add p 12
380z = add p 24
381ec_mul w ECCP ECA0 z z
382ec_mul w ECCP ECA1 y y
383ec_mul w ECCP ECA2 p ECA1
384ec_sub w ECCP ECA4 p ECA0
385ec_add w ECCP ECA5 p ECA0
386ec_mul w ECCP ECA3 ECA4 ECA5
387ec_add w ECCP ECA4 ECA3 ECA3
388ec_add w ECCP ECA3 ECA4 ECA3
389ec_add w ECCP ECA4 y z
390ec_mul w ECCP ECA4 ECA4 ECA4
391ec_sub w ECCP ECA4 ECA4 ECA1
392ec_sub w ECCP z ECA4 ECA0
393ec_add w ECCP ECA2 ECA2 ECA2
394ec_add w ECCP ECA2 ECA2 ECA2
395ec_add w ECCP ECA5 ECA2 ECA2
396ec_mul w ECCP p ECA3 ECA3
397ec_sub w ECCP p p ECA5
398ec_sub w ECCP ECA2 ECA2 p
399ec_mul w ECCP ECA2 ECA3 ECA2
400ec_mul w ECCP ECA1 ECA1 ECA1
401ec_add w ECCP ECA1 ECA1 ECA1
402ec_add w ECCP ECA1 ECA1 ECA1
403ec_add w ECCP ECA1 ECA1 ECA1
404ec_sub w ECCP y ECA2 ECA1
405end
406
407note P = P + Q (add-1998-cmo-2). Q infinity: P unchanged; P infinity: P = Q; H = 0 and R = 0
408note (P == Q): doubling; H = 0, R != 0 (P == -Q): infinity.
409fn ec_padd w edit ulong ECWN p ulong q ulong to void
410if eq ec_cmp w add q 24 ECZ 0
411ret
412end
413if eq ec_cmp w add p 24 ECZ 0
414ec_pcopy w p q
415ret
416end
417ec_mul w ECCP ECA0 add p 24 add p 24
418ec_mul w ECCP ECA1 add q 24 add q 24
419ec_mul w ECCP ECA2 p ECA1
420ec_mul w ECCP ECA3 q ECA0
421ec_mul w ECCP ECA4 add p 12 add q 24
422ec_mul w ECCP ECA4 ECA4 ECA1
423ec_mul w ECCP ECA5 add q 12 add p 24
424ec_mul w ECCP ECA5 ECA5 ECA0
425ec_sub w ECCP ECA3 ECA3 ECA2
426ec_sub w ECCP ECA5 ECA5 ECA4
427if eq ec_cmp w ECA3 ECZ 0
428if eq ec_cmp w ECA5 ECZ 0
429ec_dbl w p
430ret
431end
432ec_copy w add p 24 ECZ
433ret
434end
435ec_mul w ECCP ECA6 ECA3 ECA3
436ec_mul w ECCP ECA7 ECA3 ECA6
437ec_mul w ECCP ECA2 ECA2 ECA6
438ec_mul w ECCP p ECA5 ECA5
439ec_sub w ECCP p p ECA7
440ec_sub w ECCP p p ECA2
441ec_sub w ECCP p p ECA2
442ec_sub w ECCP ECA2 ECA2 p
443ec_mul w ECCP ECA2 ECA5 ECA2
444ec_mul w ECCP ECA4 ECA4 ECA7
445ec_sub w ECCP add p 12 ECA2 ECA4
446ec_mul w ECCP add p 24 add p 24 add q 24
447ec_mul w ECCP add p 24 add p 24 ECA3
448end
449
450note ECR = u1*G + u2*Q (Shamir, table G, Q, G+Q; variable time)
451fn ec_smul w edit ulong ECWN to void
452ec_pcopy w ECGQ ECG
453ec_padd w ECGQ ECQ
454ec_pcopy w ECR ECG
455ec_copy w add ECR 24 ECZ
456nb = mul get w 0 32
457for k ulong nb
458i = sub sub nb 1 k
459ec_dbl w ECR
460t = or ec_bit w ECU1 i shl ec_bit w ECU2 i 1
461if eq t 1
462ec_padd w ECR ECG
463end
464if eq t 2
465ec_padd w ECR ECQ
466end
467if eq t 3
468ec_padd w ECR ECGQ
469end
470end
471end
472
473note curve setup: constants, both Montgomery contexts, b and G into the Montgomery domain
474fn ec_setup w edit ulong ECWN crv ulong to ulong
475if eq crv 256
476ec_p256 w
477else
478if eq crv 384
479ec_p384 w
480else
481ret 5
482end
483end
484ec_ctx w ECCP
485ec_ctx w ECCN
486put w ECON 1
487ec_mul w ECCP ECB ECB add ECCP 28
488ec_mul w ECCP ECG ECG add ECCP 28
489ec_mul w ECCP add ECG 12 add ECG 12 add ECCP 28
490ec_copy w add ECG 24 add ECCP 16
491ret 0
492end
493
494note public key: exactly 0x04 || X || Y, X < p, Y < p, Y^2 = X^3 - 3X + b; Q in Montgomery form
495fn ec_pub w edit ulong ECWN pub view uchar * to ulong
496nb = mul get w 0 4
497if ne len pub add 1 mul nb 2
498ret 3
499end
500if ne get pub 0 4
501ret 3
502end
503ec_load w ECQ part pub 1 nb
504ec_load w add ECQ 12 part pub add 1 nb nb
505if ne ec_cmp w ECQ ECCP 1
506ret 3
507end
508if ne ec_cmp w add ECQ 12 ECCP 1
509ret 3
510end
511ec_mul w ECCP ECQ ECQ add ECCP 28
512ec_mul w ECCP add ECQ 12 add ECQ 12 add ECCP 28
513ec_copy w add ECQ 24 add ECCP 16
514ec_mul w ECCP ECA0 add ECQ 12 add ECQ 12
515ec_mul w ECCP ECA1 ECQ ECQ
516ec_add w ECCP ECA2 add ECCP 16 add ECCP 16
517ec_add w ECCP ECA2 ECA2 add ECCP 16
518ec_sub w ECCP ECA1 ECA1 ECA2
519ec_mul w ECCP ECA1 ECA1 ECQ
520ec_add w ECCP ECA1 ECA1 ECB
521if ne ec_cmp w ECA0 ECA1 0
522ret 3
523end
524ret 0
525end
526
527note one DER INTEGER at hs[p..e): tag 2, short-form length 1..127 inside e, non-negative,
528note minimal (a leading 0x00 only before a byte >= 0x80). Writes the value to o; a value longer
529note than 4L bytes is replaced by n so the range check rejects it. Returns the bytes used, 0 bad.
530fn ec_int w edit ulong ECWN o ulong hs view uchar * p ulong e ulong to ulong
531if lt sub e p 3
532ret 0
533end
534if ne get hs p 2
535ret 0
536end
537il = ulong get hs add p 1
538if or ge il 128 eq il 0
539ret 0
540end
541if gt il sub e add p 2
542ret 0
543end
544v = add p 2
545if ge get hs v 128
546ret 0
547end
548k = il
549if and eq get hs v 0 gt il 1
550if lt get hs add v 1 128
551ret 0
552end
553v = add v 1
554k = sub k 1
555end
556if gt k mul get w 0 4
557ec_copy w o ECCN
558else
559ec_load w o part hs v k
560end
561ret add il 2
562end
563
564note strict DER Ecdsa-Sig-Value at hs[h..]: SEQUENCE (short-form length; every valid signature
565note on these curves is < 128 bytes, so a long form is never minimal) of two INTEGERs, and no
566note byte after it.
567fn ec_der w edit ulong ECWN hs view uchar * h ulong to ulong
568k = len hs
569if lt sub k h 2
570ret 2
571end
572if ne get hs h 0x30
573ret 2
574end
575sl = ulong get hs add h 1
576if ge sl 128
577ret 2
578end
579if ne add add h 2 sl k
580ret 2
581end
582c = ec_int w ECSR hs add h 2 k
583if eq c 0
584ret 2
585end
586d = ec_int w ECSS hs add add h 2 c k
587if eq d 0
588ret 2
589end
590if ne add add add h 2 c d k
591ret 2
592end
593ret 0
594end
595
596note raw r || s at hs[h..], each 4L bytes, nothing after
597fn ec_raw w edit ulong ECWN hs view uchar * h ulong to ulong
598nb = mul get w 0 4
599if ne sub len hs h mul nb 2
600ret 2
601end
602ec_load w ECSR part hs h nb
603ec_load w ECSS part hs add h nb nb
604ret 0
605end
606
607note ---- P-256 specialised field and group arithmetic (module code P2: functions p2_*, consts P2*),
608note used by ec_core for P-256 verification and by ecdh.om for P-256 ECDH. Pure: no I/O, no randomness.
609note
610note API (all values in the workspace w, P2WN ulong cells, 32-bit limbs little-endian, 8 per element):
611note p2_init w load p, b and G; call once per fresh workspace
612note p2_smul w P2O = k*P for the secret scalar k at P2K (k < 2^256) and the affine point
613note P at P2P (X, Y); constant time; returns 1 when the result is finite, else 0
614note p2_smul2 w P2O = u1*G + u2*Q for the public scalars u1 at P2K, u2 at P2U and the
615note affine point Q at P2Q; not constant time (public data only); returns 1 when
616note the result is finite, else 0
617note p2_wipe w overwrite the whole workspace with 0
618note The output P2O is the affine (x, y), plain (not Montgomery) and fully reduced, both 0 when the
619note result is the point at infinity. Inputs must be on the curve with coordinates < p (the callers
620note validate public keys in ec.om, ec_pub); scalars are not reduced here.
621note
622note Field: p = 2^256 - 2^224 + 2^192 + 2^96 - 1. Elements are 8 limbs < 2^32, value in [0, p).
623note Multiplication: the 16-limb product by operand scanning, each 64-bit partial product split into
624note its low and high 32-bit halves accumulated in their own columns (a column takes at most 16 halves,
625note < 2^36), one carry pass to 32-bit words c0..c15, then the fast reduction of FIPS 186-4 Appendix
626note D.2.3 (Solinas): with c = (c15, ..., c0),
627note r = T + 2 S1 + 2 S2 + S3 + S4 - D1 - D2 - D3 - D4 (mod p), per 32-bit word j:
628note w0 = c0 + c8 + c9 - c11 - c12 - c13 - c14 w4 = c4 + 2 c12 + 2 c13 + c14 - c9 - c10
629note w1 = c1 + c9 + c10 - c12 - c13 - c14 - c15 w5 = c5 + 2 c13 + 2 c14 + c15 - c10 - c11
630note w2 = c2 + c10 + c11 - c13 - c14 - c15 w6 = c6 + c13 + 3 c14 + 2 c15 - c8 - c9
631note w3 = c3 + 2 c11 + 2 c12 + c13 - c8 - c9 - c15 w7 = c7 + c8 + 3 c15 - c10 - c11 - c12 - c13
632note The words are formed in wrapping ulong arithmetic (two's complement); |w_j| < 2^35, so a signed
633note carry (arithmetic shift right by 32 through bitcast long) is exact. The carry f out of word 7 is
634note in [-4, 6] and folds back as f 2^256 = f (2^224 - 2^192 - 2^96 + 1) mod p (+f at word 0, -f at
635note words 3 and 6, +f at word 7). Two carry-and-fold rounds leave a value in [0, 2^256) (the second
636note carry is -1, 0 or 1 and its fold cannot carry again); 2^256 < 2p, so one masked subtraction of p
637note gives [0, p). Addition and subtraction: carry or borrow chains with a masked correction by p.
638note Inversion: Fermat a^(p-2) by a fixed addition chain (255 squarings, 12 multiplications).
639note Checked with the Wolfram Language evaluator (2026-10-26, queries in test/ec/wolfram.txt): the
640note Solinas word identity, the fold identity, 2^256 < 2p, the inversion chain exponent.
641note
642note Group: projective (X : Y : Z), the point at infinity is (0 : 1 : 0). Addition and doubling use
643note the complete formula for a = -3 of Renes, Costello and Batina, "Complete addition formulas for
644note prime order elliptic curves" (EUROCRYPT 2016), Algorithm 4 (also used by ecdh.om): no exceptional
645note case, so no branch. Scalar multiplication: fixed 4-bit window, the table 0P .. 15P (0P the point
646note at infinity), 63 rounds of 4 doublings and one addition after the top window; every table read
647note for a secret scalar goes through p2_sel, which reads all 16 entries and keeps one by an AND mask
648note computed without a branch ((j xor v) - 1) >> 63. Window digits come from fixed bit positions.
649note
650note Constant time (p2_smul): no branch, loop count or memory index depends on the scalar, the point
651note or any intermediate value; all carries are shifts. The verification path p2_smul2 picks table
652note entries by direct index (public scalars only). Caveats as in ecdh.om: omc emits plain AArch64
653note (MUL, ADD, SUB, EOR, AND, ORR, LSL/LSR/ASR); PSTATE.DIT is not set by this unit; register and
654note spill contents are not scrubbed. Local scratch arrays holding limbs are zeroed before return and
655note p2_wipe clears the workspace.
656
657note workspace layout (ulong offsets): two 16-entry point tables, points, field temporaries, constants
658const P2WN ulong 1072
659const P2TA ulong 0
660const P2TB ulong 384
661const P2R ulong 768
662const P2S ulong 792
663const P2P ulong 816
664const P2Q ulong 840
665const P2T0 ulong 864
666const P2T1 ulong 872
667const P2T2 ulong 880
668const P2T3 ulong 888
669const P2T4 ulong 896
670const P2X3 ulong 904
671const P2Y3 ulong 912
672const P2Z3 ulong 920
673const P2I0 ulong 928
674const P2I1 ulong 936
675const P2I2 ulong 944
676const P2I3 ulong 952
677const P2I4 ulong 960
678const P2I5 ulong 968
679const P2K ulong 976
680const P2U ulong 984
681const P2PM ulong 992
682const P2B ulong 1000
683const P2GX ulong 1008
684const P2GY ulong 1016
685const P2O ulong 1024
686const P2ZI ulong 1040
687const P2M32 ulong 0xffffffff
688
689note p, b, G (FIPS 186-4 D.1.2.3, the values ec_p256 in ec.om loads), limbs little-endian
690fn p2_init w edit ulong P2WN to void
691note p = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff
692put w 992 0xffffffff
693put w 993 0xffffffff
694put w 994 0xffffffff
695put w 995 0x0
696put w 996 0x0
697put w 997 0x0
698put w 998 0x1
699put w 999 0xffffffff
700note b = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b
701put w 1000 0x27d2604b
702put w 1001 0x3bce3c3e
703put w 1002 0xcc53b0f6
704put w 1003 0x651d06b0
705put w 1004 0x769886bc
706put w 1005 0xb3ebbd55
707put w 1006 0xaa3a93e7
708put w 1007 0x5ac635d8
709note gx = 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296
710put w 1008 0xd898c296
711put w 1009 0xf4a13945
712put w 1010 0x2deb33a0
713put w 1011 0x77037d81
714put w 1012 0x63a440f2
715put w 1013 0xf8bce6e5
716put w 1014 0xe12c4247
717put w 1015 0x6b17d1f2
718note gy = 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5
719put w 1016 0x37bf51f5
720put w 1017 0xcbb64068
721put w 1018 0x6b315ece
722put w 1019 0x2bce3357
723put w 1020 0x7c0f9e16
724put w 1021 0x8ee7eb4a
725put w 1022 0xfe1a7f9b
726put w 1023 0x4fe342e2
727end
728
729note r (8 words, each < 2^32 + 7 as wrapping two's complement; value in (-2^259, 2^259)) to the
730note words in [0, 2^32) and the signed carry out of word 7 (arithmetic shift, no branch)
731fn p2_carry r edit ulong 8 to ulong inline
732cr = ulong 0
733for k ulong 8 unroll
734z = add get r k cr
735put r k and z P2M32
736cr = bitcast ulong shr bitcast long z 32
737end
738ret cr
739end
740
741note fold the signed carry f: f 2^256 = f (2^224 - 2^192 - 2^96 + 1) mod p
742fn p2_fold r edit ulong 8 f ulong to void inline
743put r 0 add get r 0 f
744put r 3 sub get r 3 f
745put r 6 sub get r 6 f
746put r 7 add get r 7 f
747end
748
749note w[o] = r mod p for r in [0, 2^256) (2^256 < 2p): one masked subtraction of p, no branch;
750note r and the scratch are zeroed
751fn p2_csub w edit ulong P2WN o ulong r edit ulong 8 to void
752d = new ulong 8 0
753br = ulong 0
754for k ulong 8 unroll
755z = sub sub get r k get w add P2PM k br
756put d k and z P2M32
757br = shr z 63
758end
759mk = sub br 1
760for k ulong 8 unroll
761x = get r k
762put w add o k xor x and mk xor x get d k
763put d k 0
764put r k 0
765end
766end
767
768note w[o] = the 16 product columns t (t[k] < 2^37) mod p: carry pass, Solinas words, two
769note carry-and-fold rounds, the masked subtraction. t and the scratch are zeroed.
770fn p2_red w edit ulong P2WN o ulong t edit ulong 16 to void
771cr = ulong 0
772for k ulong 16 unroll
773z = add get t k cr
774put t k and z P2M32
775cr = shr z 32
776end
777r = new ulong 8 0
778s = add add get t 0 get t 8 get t 9
779put r 0 sub sub sub sub s get t 11 get t 12 get t 13 get t 14
780s = add add get t 1 get t 9 get t 10
781put r 1 sub sub sub sub s get t 12 get t 13 get t 14 get t 15
782s = add add get t 2 get t 10 get t 11
783put r 2 sub sub sub s get t 13 get t 14 get t 15
784s = add add get t 3 shl add get t 11 get t 12 1 get t 13
785put r 3 sub sub sub s get t 8 get t 9 get t 15
786s = add add get t 4 shl add get t 12 get t 13 1 get t 14
787put r 4 sub sub s get t 9 get t 10
788s = add add get t 5 shl add get t 13 get t 14 1 get t 15
789put r 5 sub sub s get t 10 get t 11
790s = add add add get t 6 get t 13 mul get t 14 3 shl get t 15 1
791put r 6 sub sub s get t 8 get t 9
792s = add add get t 7 get t 8 mul get t 15 3
793put r 7 sub sub sub sub s get t 10 get t 11 get t 12 get t 13
794for k ulong 16 unroll
795put t k 0
796end
797f = p2_carry r
798p2_fold r f
799f = p2_carry r
800p2_fold r f
801f = p2_carry r
802p2_csub w o r
803end
804
805note w[o] = w[a] * w[b] mod p (o may equal a or b)
806fn p2_mul w edit ulong P2WN o ulong a ulong b ulong to void
807x = new ulong 8 0
808y = new ulong 8 0
809t = new ulong 16 0
810for i ulong 8 unroll
811put x i get w add a i
812put y i get w add b i
813end
814for i ulong 8 unroll
815xi = get x i
816for j ulong 8 unroll
817p = mul xi get y j
818put t add i j add get t add i j and p P2M32
819put t add add i j 1 add get t add add i j 1 shr p 32
820end
821end
822for i ulong 8 unroll
823put x i 0
824put y i 0
825end
826p2_red w o t
827end
828
829note w[o] = w[a]^2 mod p: the 28 cross products once and doubled, the 8 squares (o may equal a)
830fn p2_sqr w edit ulong P2WN o ulong a ulong to void
831x = new ulong 8 0
832t = new ulong 16 0
833for i ulong 8 unroll
834put x i get w add a i
835end
836for i ulong 8 unroll
837xi = get x i
838for j ulong 8 unroll
839if lt i j
840p = mul xi get x j
841put t add i j add get t add i j and p P2M32
842put t add add i j 1 add get t add add i j 1 shr p 32
843end
844end
845end
846for k ulong 16 unroll
847put t k shl get t k 1
848end
849for i ulong 8 unroll
850xi = get x i
851p = mul xi xi
852put t add i i add get t add i i and p P2M32
853put t add add i i 1 add get t add add i i 1 shr p 32
854put x i 0
855end
856p2_red w o t
857end
858
859note w[o] = w[a] + w[b] mod p (a, b < p; o may equal a or b)
860fn p2_add w edit ulong P2WN o ulong a ulong b ulong to void
861s = new ulong 8 0
862c = ulong 0
863for k ulong 8 unroll
864z = add add get w add a k get w add b k c
865put s k and z P2M32
866c = shr z 32
867end
868br = ulong 0
869d = new ulong 8 0
870for k ulong 8 unroll
871z = sub sub get s k get w add P2PM k br
872put d k and z P2M32
873br = shr z 63
874end
875z = sub c br
876br = shr z 63
877mk = sub br 1
878for k ulong 8 unroll
879x = get s k
880put w add o k xor x and mk xor x get d k
881put s k 0
882put d k 0
883end
884end
885
886note w[o] = w[a] - w[b] mod p (a, b < p; o may equal a or b): borrow chain, then + (p AND mask)
887fn p2_sub w edit ulong P2WN o ulong a ulong b ulong to void
888d = new ulong 8 0
889br = ulong 0
890for k ulong 8 unroll
891z = sub sub get w add a k get w add b k br
892put d k and z P2M32
893br = shr z 63
894end
895mk = sub 0 br
896c = ulong 0
897for k ulong 8 unroll
898z = add add get d k and mk get w add P2PM k c
899put w add o k and z P2M32
900c = shr z 32
901put d k 0
902end
903end
904
905fn p2_copy w edit ulong P2WN o ulong a ulong to void
906for k ulong 8 unroll
907put w add o k get w add a k
908end
909end
910
911note w[o] = w[a]^(2^n), n >= 1 (n public)
912fn p2_sqn w edit ulong P2WN o ulong a ulong n ulong to void
913p2_sqr w o a
914for i ulong sub n 1
915p2_sqr w o o
916end
917end
918
919note w[o] = w[a]^(p - 2) = 1 / w[a] (0 for 0). p - 2 = 0xffffffff 00000001 0^96 ffffffff ffffffff
920note fffffffd: x2 = a^(2^2-1), x3, x6, x12, x15, x30, x32 (xk = a^(2^k - 1)), then
921note (((x32 2^32 + 1) 2^128 + x32) 2^32 + x32) 2^30 + x30) 2^2 + 1 in the exponent. Uses P2I0..P2I5;
922note o and a must be other slots.
923fn p2_inv w edit ulong P2WN o ulong a ulong to void
924p2_sqr w P2I0 a
925p2_mul w P2I0 P2I0 a
926p2_sqr w P2I1 P2I0
927p2_mul w P2I1 P2I1 a
928p2_sqn w P2I2 P2I1 3
929p2_mul w P2I2 P2I2 P2I1
930p2_sqn w P2I3 P2I2 6
931p2_mul w P2I3 P2I3 P2I2
932p2_sqn w P2I3 P2I3 3
933p2_mul w P2I3 P2I3 P2I1
934p2_sqn w P2I4 P2I3 15
935p2_mul w P2I4 P2I4 P2I3
936p2_sqn w P2I5 P2I4 2
937p2_mul w P2I5 P2I5 P2I0
938p2_sqn w P2I3 P2I5 32
939p2_mul w P2I3 P2I3 a
940p2_sqn w P2I3 P2I3 128
941p2_mul w P2I3 P2I3 P2I5
942p2_sqn w P2I3 P2I3 32
943p2_mul w P2I3 P2I3 P2I5
944p2_sqn w P2I3 P2I3 30
945p2_mul w P2I3 P2I3 P2I4
946p2_sqn w P2I3 P2I3 2
947p2_mul w o P2I3 a
948end
949
950note o = a + b, complete projective addition for a = -3 (RCB 2016 Algorithm 4, 12M + 2 mb + 29a,
951note the sequence of ecdh.om ecd_padd). a == b gives the doubling; o may alias a or b (the result is
952note formed in P2X3, P2Y3, P2Z3 after the last read of the inputs).
953fn p2_padd w edit ulong P2WN o ulong a ulong b ulong to void
954p2_mul w P2T0 a b
955p2_mul w P2T1 add a 8 add b 8
956p2_mul w P2T2 add a 16 add b 16
957p2_add w P2T3 a add a 8
958p2_add w P2T4 b add b 8
959p2_mul w P2T3 P2T3 P2T4
960p2_add w P2T4 P2T0 P2T1
961p2_sub w P2T3 P2T3 P2T4
962p2_add w P2T4 add a 8 add a 16
963p2_add w P2X3 add b 8 add b 16
964p2_mul w P2T4 P2T4 P2X3
965p2_add w P2X3 P2T1 P2T2
966p2_sub w P2T4 P2T4 P2X3
967p2_add w P2X3 a add a 16
968p2_add w P2Y3 b add b 16
969p2_mul w P2X3 P2X3 P2Y3
970p2_add w P2Y3 P2T0 P2T2
971p2_sub w P2Y3 P2X3 P2Y3
972p2_mul w P2Z3 P2B P2T2
973p2_sub w P2X3 P2Y3 P2Z3
974p2_add w P2Z3 P2X3 P2X3
975p2_add w P2X3 P2X3 P2Z3
976p2_sub w P2Z3 P2T1 P2X3
977p2_add w P2X3 P2T1 P2X3
978p2_mul w P2Y3 P2B P2Y3
979p2_add w P2T1 P2T2 P2T2
980p2_add w P2T2 P2T1 P2T2
981p2_sub w P2Y3 P2Y3 P2T2
982p2_sub w P2Y3 P2Y3 P2T0
983p2_add w P2T1 P2Y3 P2Y3
984p2_add w P2Y3 P2T1 P2Y3
985p2_add w P2T1 P2T0 P2T0
986p2_add w P2T0 P2T1 P2T0
987p2_sub w P2T0 P2T0 P2T2
988p2_mul w P2T1 P2T4 P2Y3
989p2_mul w P2T2 P2T0 P2Y3
990p2_mul w P2Y3 P2X3 P2Z3
991p2_add w P2Y3 P2Y3 P2T2
992p2_mul w P2X3 P2T3 P2X3
993p2_sub w P2X3 P2X3 P2T1
994p2_mul w P2Z3 P2T4 P2Z3
995p2_mul w P2T1 P2T3 P2T0
996p2_add w P2Z3 P2Z3 P2T1
997p2_copy w o P2X3
998p2_copy w add o 8 P2Y3
999p2_copy w add o 16 P2Z3
1000end
1001
1002note copy the 24-cell point at a to o
1003fn p2_pcopy w edit ulong P2WN o ulong a ulong to void
1004for k ulong 24 unroll
1005put w add o k get w add a k
1006end
1007end
1008
1009note the table t[j] = j P (j = 0..15) for the projective point at p; t[0] = (0 : 1 : 0)
1010fn p2_tab w edit ulong P2WN t ulong p ulong to void
1011for k ulong 24 unroll
1012put w add t k 0
1013end
1014put w add t 8 1
1015p2_pcopy w add t 24 p
1016for j ulong 14
1017p2_padd w add t mul add j 2 24 add t mul add j 1 24 p
1018end
1019end
1020
1021note o = t[v] for the secret digit v in 0..15: all 16 entries are read, each AND a mask that is all
1022note ones only for j = v (m = 0 - (((j xor v) - 1) >> 63)), and ORed together; no branch, no index
1023note from v
1024fn p2_sel w edit ulong P2WN o ulong t ulong v ulong to void
1025s = new ulong 24 0
1026for j ulong 16
1027m = sub 0 shr sub xor j v 1 63
1028e = add t mul j 24
1029for k ulong 24 unroll
1030put s k or get s k and m get w add e k
1031end
1032end
1033for k ulong 24 unroll
1034put w add o k get s k
1035put s k 0
1036end
1037end
1038
1039note digit i (0..63, public) of the 4-bit windows of the scalar at k: bits 4i .. 4i + 3
1040fn p2_dig w edit ulong P2WN k ulong i ulong to ulong
1041ret and shr get w add k shr i 3 mul and i 7 4 15
1042end
1043
1044note the affine point at a (X, Y; 16 cells) to projective (X : Y : 1) at o
1045fn p2_proj w edit ulong P2WN o ulong a ulong to void
1046for k ulong 16 unroll
1047put w add o k get w add a k
1048end
1049for k ulong 8 unroll
1050put w add add o 16 k 0
1051end
1052put w add o 16 1
1053end
1054
1055note P2O = P2R in affine coordinates; returns 1 when Z != 0, else 0 (and P2O = (0, 0) since
1056note Z^(p-2) = 0); the Z test is an OR of the limbs and ((z | -z) >> 63), no branch
1057fn p2_aff w edit ulong P2WN to ulong
1058z = ulong 0
1059for k ulong 8 unroll
1060z = or z get w add add P2R 16 k
1061end
1062p2_inv w P2ZI add P2R 16
1063p2_mul w P2O P2R P2ZI
1064p2_mul w add P2O 8 add P2R 8 P2ZI
1065ret shr or z sub 0 z 63
1066end
1067
1068fn p2_smul w edit ulong P2WN to ulong
1069p2_proj w P2Q P2P
1070p2_tab w P2TA P2Q
1071p2_sel w P2R P2TA p2_dig w P2K 63
1072for n ulong 63
1073i = sub 62 n
1074p2_padd w P2R P2R P2R
1075p2_padd w P2R P2R P2R
1076p2_padd w P2R P2R P2R
1077p2_padd w P2R P2R P2R
1078p2_sel w P2S P2TA p2_dig w P2K i
1079p2_padd w P2R P2R P2S
1080end
1081ret p2_aff w
1082end
1083
1084fn p2_smul2 w edit ulong P2WN to ulong
1085p2_proj w P2S P2GX
1086p2_tab w P2TA P2S
1087p2_proj w P2S P2Q
1088p2_tab w P2TB P2S
1089p2_pcopy w P2R add P2TA mul p2_dig w P2K 63 24
1090p2_padd w P2R P2R add P2TB mul p2_dig w P2U 63 24
1091for n ulong 63
1092i = sub 62 n
1093p2_padd w P2R P2R P2R
1094p2_padd w P2R P2R P2R
1095p2_padd w P2R P2R P2R
1096p2_padd w P2R P2R P2R
1097p2_padd w P2R P2R add P2TA mul p2_dig w P2K i 24
1098p2_padd w P2R P2R add P2TB mul p2_dig w P2U i 24
1099end
1100ret p2_aff w
1101end
1102
1103fn p2_wipe w edit ulong P2WN to void
1104for j ulong P2WN
1105put w j 0
1106end
1107end
1108
1109note P-256: ECA9 = x(u1 G + u2 Q) plain, by p2_smul2 in its own workspace; returns 1 when the
1110note point is finite, else 0. Q is taken out of the Montgomery domain (times plain 1).
1111fn ec_smul256 w edit ulong ECWN to ulong
1112v = new ulong P2WN 0
1113p2_init v
1114ec_mul w ECCP ECA0 ECQ ECON
1115ec_mul w ECCP ECA1 add ECQ 12 ECON
1116for j ulong 8 unroll
1117put v add P2K j get w add ECU1 j
1118put v add P2U j get w add ECU2 j
1119put v add P2Q j get w add ECA0 j
1120put v add add P2Q 8 j get w add ECA1 j
1121end
1122f = p2_smul2 v
1123for j ulong 8 unroll
1124put w add ECA9 j get v add P2O j
1125end
1126ret f
1127end
1128
1129note r, s in [1, n-1], then u1 = e s^-1, u2 = r s^-1 mod n, R = u1 G + u2 Q, accept iff R is
1130note finite and x(R) mod n == r. digest = hs[0..hlen).
1131fn ec_core w edit ulong ECWN hs view uchar * hlen ulong to ulong
1132if eq ec_cmp w ECSR ECZ 0
1133ret 4
1134end
1135if ne ec_cmp w ECSR ECCN 1
1136ret 4
1137end
1138if eq ec_cmp w ECSS ECZ 0
1139ret 4
1140end
1141if ne ec_cmp w ECSS ECCN 1
1142ret 4
1143end
1144k = mul get w 0 4
1145if lt hlen k
1146k = hlen
1147end
1148ec_load w ECE part hs 0 k
1149ec_add w ECCN ECE ECE ECZ
1150ec_mul w ECCN ECSI ECSS add ECCN 28
1151ec_inv w ECCN ECSI ECSI
1152ec_mul w ECCN ECU1 ECE ECSI
1153ec_mul w ECCN ECU2 ECSR ECSI
1154if eq get w 0 8
1155if eq ec_smul256 w 0
1156ret 1
1157end
1158else
1159ec_smul w
1160if eq ec_cmp w add ECR 24 ECZ 0
1161ret 1
1162end
1163ec_inv w ECCP ECA8 add ECR 24
1164ec_mul w ECCP ECA9 ECA8 ECA8
1165ec_mul w ECCP ECA9 ECA9 ECR
1166ec_mul w ECCP ECA9 ECA9 ECON
1167end
1168ec_add w ECCN ECA9 ECA9 ECZ
1169if eq ec_cmp w ECA9 ECSR 0
1170ret 0
1171end
1172ret 1
1173end
1174
1175fn ecdsa_verify crv ulong pub view uchar * hs view uchar * hlen ulong to ulong
1176w = new ulong ECWN 0
1177rc = ec_setup w crv
1178if ne rc 0
1179ret rc
1180end
1181rc = ec_pub w pub
1182if ne rc 0
1183ret rc
1184end
1185if gt hlen len hs
1186ret 2
1187end
1188rc = ec_der w hs hlen
1189if ne rc 0
1190ret rc
1191end
1192ret ec_core w hs hlen
1193end
1194
1195fn ecdsa_verify_raw crv ulong pub view uchar * hs view uchar * hlen ulong to ulong
1196w = new ulong ECWN 0
1197rc = ec_setup w crv
1198if ne rc 0
1199ret rc
1200end
1201rc = ec_pub w pub
1202if ne rc 0
1203ret rc
1204end
1205if gt hlen len hs
1206ret 2
1207end
1208rc = ec_raw w hs hlen
1209if ne rc 0
1210ret rc
1211end
1212ret ec_core w hs hlen
1213end