interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / agora / tls / ec.om

685 lines · 17650 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 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
608note finite and x(R) mod n == r. digest = hs[0..hlen).
609fn ec_core w edit ulong ECWN hs view uchar * hlen ulong to ulong
610if eq ec_cmp w ECSR ECZ 0
611ret 4
612end
613if ne ec_cmp w ECSR ECCN 1
614ret 4
615end
616if eq ec_cmp w ECSS ECZ 0
617ret 4
618end
619if ne ec_cmp w ECSS ECCN 1
620ret 4
621end
622k = mul get w 0 4
623if lt hlen k
624k = hlen
625end
626ec_load w ECE part hs 0 k
627ec_add w ECCN ECE ECE ECZ
628ec_mul w ECCN ECSI ECSS add ECCN 28
629ec_inv w ECCN ECSI ECSI
630ec_mul w ECCN ECU1 ECE ECSI
631ec_mul w ECCN ECU2 ECSR ECSI
632ec_smul w
633if eq ec_cmp w add ECR 24 ECZ 0
634ret 1
635end
636ec_inv w ECCP ECA8 add ECR 24
637ec_mul w ECCP ECA9 ECA8 ECA8
638ec_mul w ECCP ECA9 ECA9 ECR
639ec_mul w ECCP ECA9 ECA9 ECON
640ec_add w ECCN ECA9 ECA9 ECZ
641if eq ec_cmp w ECA9 ECSR 0
642ret 0
643end
644ret 1
645end
646
647fn ecdsa_verify crv ulong pub view uchar * hs view uchar * hlen ulong to ulong
648w = new ulong ECWN 0
649rc = ec_setup w crv
650if ne rc 0
651ret rc
652end
653rc = ec_pub w pub
654if ne rc 0
655ret rc
656end
657if gt hlen len hs
658ret 2
659end
660rc = ec_der w hs hlen
661if ne rc 0
662ret rc
663end
664ret ec_core w hs hlen
665end
666
667fn ecdsa_verify_raw crv ulong pub view uchar * hs view uchar * hlen ulong to ulong
668w = new ulong ECWN 0
669rc = ec_setup w crv
670if ne rc 0
671ret rc
672end
673rc = ec_pub w pub
674if ne rc 0
675ret rc
676end
677if gt hlen len hs
678ret 2
679end
680rc = ec_raw w hs hlen
681if ne rc 0
682ret rc
683end
684ret ec_core w hs hlen
685end