interplane.Get Omega
Read in your language

omtls

omtls: a TLS 1.3 client (RFC 8446) written fresh in seeded Omega, for mailhand. Not audited by a cryptographer. Free (Apache-2.0).

Brought by drakestapleton

branch main Branches Tags History

omtls / src / ec.om

646 lines · 17191 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note ec.om: ECDSA signature VERIFICATION on P-256 and P-384 (FIPS 186-4 6.4, SEC 1 4.1.4), prefix ot_ec_.
3note Public data only (certificates, CertificateVerify): variable time is acceptable; strictness is not optional.
4note Representation: a workspace w (ulong 1024) of 64 slots of 16 words; slot s holds a number as little-endian
5note 32-bit limbs in w[16s .. 16s+12). A modulus slot also keeps its limb count in word 15 and -m^-1 mod 2^32 in
6note word 14. Field and scalar arithmetic is Montgomery multiplication (CIOS) modulo p or n; inverses by Fermat.
7note Points are Jacobian (X, Y, Z) in three slots, Montgomery form mod p, infinity = Z zero; a = -3 doubling;
8note general addition that falls back to doubling for P == Q and gives infinity for P == -Q.
9note Curve constants (SEC 2 / FIPS 186-4 D.1.2):
10note wolfram (WolframLanguageEvaluator, 2026-11-11, TlsPk2 and OmegaTls): for each curve, with a = -3 and affine
11note double-and-add, p == 2^256-2^224+2^192+2^96-1 (resp. 2^384-2^128-2^96+2^32-1), PrimeQ[p], PrimeQ[n],
12note Gy^2 == Gx^3 - 3 Gx + b mod p, n*G == infinity, BitLength[p] == BitLength[n] == 256 (384), n < p < 2n,
13note -4a^3-27b^2 != 0 mod p: all True for both curves. p < 2n: x(R) mod n needs at most one subtraction.
14note Limb bound: one CIOS step adds t_j + a_j b_i + carry <= (2^32-1) + (2^32-1)^2 + (2^32-1) = 2^64 - 1.
15global ot_ec_v ulong 8
16global ot_ec_pub uchar 97
17text ot_ec_p256p "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff"
18text ot_ec_p256b "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b"
19text ot_ec_p256x "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296"
20text ot_ec_p256y "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5"
21text ot_ec_p256n "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551"
22text ot_ec_p384p "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff"
23text ot_ec_p384b "b3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef"
24text ot_ec_p384x "aa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7"
25text ot_ec_p384y "3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f"
26text ot_ec_p384n "ffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973"
27note slots
28const OT_EC_P ulong 0
29const OT_EC_N ulong 1
30const OT_EC_R2P ulong 2
31const OT_EC_R2N ulong 3
32const OT_EC_ONE ulong 4
33const OT_EC_PM2 ulong 5
34const OT_EC_NM2 ulong 6
35const OT_EC_B ulong 7
36const OT_EC_RP ulong 8
37const OT_EC_RN ulong 9
38const OT_EC_GX ulong 10
39const OT_EC_QX ulong 13
40const OT_EC_TX ulong 16
41const OT_EC_AX ulong 19
42const OT_EC_S1 ulong 22
43const OT_EC_S2 ulong 23
44const OT_EC_E ulong 24
45const OT_EC_W ulong 25
46const OT_EC_U1 ulong 26
47const OT_EC_U2 ulong 27
48const OT_EC_X ulong 28
49note 30..63: scratch for the point formulas
50
51note ======== limbs ========
52fn ot_ec_k w view ulong 1024 m ulong to ulong
53ret get w add mul m 16 15
54end
55fn ot_ec_zero w edit ulong 1024 s ulong to void
56for i ulong 16
57put w add mul s 16 i 0
58end
59end
60fn ot_ec_copy w edit ulong 1024 d ulong s ulong to void
61for i ulong 12
62put w add mul d 16 i get w add mul s 16 i
63end
64end
65fn ot_ec_iszero w view ulong 1024 s ulong to bool
66v = ulong 0
67for i ulong 12
68v = or v get w add mul s 16 i
69end
70ret eq v 0
71end
72fn ot_ec_eq w view ulong 1024 a ulong b ulong to bool
73for i ulong 12
74if ne get w add mul a 16 i get w add mul b 16 i
75ret false
76end
77end
78ret true
79end
80note a < modulus m (as integers, k limbs)
81fn ot_ec_lt w view ulong 1024 a ulong m ulong to bool
82k = ot_ec_k w m
83for j ulong k
84i = sub sub k 1 j
85x = get w add mul a 16 i
86y = get w add mul m 16 i
87if lt x y
88ret true
89end
90if gt x y
91ret false
92end
93end
94ret false
95end
96note big-endian bytes (at most 4k) into slot s; false if there are more bytes than the slot holds
97fn ot_ec_import w edit ulong 1024 s ulong b view uchar * to bool
98ot_ec_zero w s
99n = len b
100if gt n 48
101ret false
102end
103for i ulong n
104j = shr i 2
105sh = mul and i 3 8
106v = shl ulong get b sub sub n 1 i sh
107o = add mul s 16 j
108put w o or get w o v
109end
110ret true
111end
112fn ot_ec_export w view ulong 1024 s ulong o edit uchar * to void
113n = len o
114for i ulong n
115v = get w add mul s 16 shr i 2
116sh = mul and i 3 8
117by = uchar and shr v sh 255
118put o sub sub n 1 i by
119end
120end
121fn ot_ec_hexv c uchar to ulong
122if and ge c 48 le c 57
123ret ulong sub c 48
124end
125ret add 10 ulong sub c 97
126end
127note a constant from hex text into slot s
128fn ot_ec_hex w edit ulong 1024 s ulong t view uchar * to void
129b = new uchar 48 0
130n = div len t 2
131for i ulong n
132hi = ot_ec_hexv get t mul i 2
133lo = ot_ec_hexv get t add mul i 2 1
134put b i uchar add mul hi 16 lo
135end
136ok = ot_ec_import w s part b 0 n
137end
138
139note ======== modular arithmetic modulo the modulus slot m ========
140fn ot_ec_g w view ulong 1024 s ulong i ulong to ulong
141ret get w add mul s 16 i
142end
143fn ot_ec_s w edit ulong 1024 s ulong i ulong v ulong to void
144put w add mul s 16 i v
145end
146note a - m in place when a >= m or a has a carry word (c)
147fn ot_ec_cond w edit ulong 1024 a ulong m ulong c ulong to void
148if eq c 0
149if ot_ec_lt w a m
150ret
151end
152end
153k = ot_ec_k w m
154br = ulong 0
155for i ulong k
156x = sub ot_ec_g w a i ot_ec_g w m i
157x = sub x br
158ot_ec_s w a i and x 4294967295
159br = shr x 63
160end
161end
162fn ot_ec_add w edit ulong 1024 d ulong a ulong b ulong m ulong to void
163k = ot_ec_k w m
164c = ulong 0
165for i ulong k
166x = add ot_ec_g w a i ot_ec_g w b i
167x = add x c
168ot_ec_s w d i and x 4294967295
169c = shr x 32
170end
171ot_ec_cond w d m c
172end
173fn ot_ec_sub w edit ulong 1024 d ulong a ulong b ulong m ulong to void
174k = ot_ec_k w m
175br = ulong 0
176for i ulong k
177x = sub ot_ec_g w a i ot_ec_g w b i
178x = sub x br
179ot_ec_s w d i and x 4294967295
180br = shr x 63
181end
182if ne br 0
183ot_ec_addm w d m
184end
185end
186fn ot_ec_addm w edit ulong 1024 d ulong m ulong to void
187k = ot_ec_k w m
188c = ulong 0
189for i ulong k
190x = add ot_ec_g w d i ot_ec_g w m i
191x = add x c
192ot_ec_s w d i and x 4294967295
193c = shr x 32
194end
195end
196note t += a * bi over k limbs (t has k + 2 words)
197fn ot_ec_row1 t edit ulong 16 w view ulong 1024 a ulong bi ulong k ulong to void
198c = ulong 0
199for j ulong k
200x = mul ot_ec_g w a j bi
201x = add x get t j
202x = add x c
203put t j and x 4294967295
204c = shr x 32
205end
206x = add get t k c
207put t k and x 4294967295
208put t add k 1 add get t add k 1 shr x 32
209end
210note t = (t + q m) / 2^32 with q = t0 * (-m^-1) mod 2^32
211fn ot_ec_row2 t edit ulong 16 w view ulong 1024 m ulong k ulong to void
212q = and mul get t 0 ot_ec_g w m 14 4294967295
213x = add get t 0 mul q ot_ec_g w m 0
214c = shr x 32
215for j ulong sub k 1
216x = mul q ot_ec_g w m add j 1
217x = add x get t add j 1
218x = add x c
219put t j and x 4294967295
220c = shr x 32
221end
222x = add get t k c
223put t sub k 1 and x 4294967295
224put t k add get t add k 1 shr x 32
225put t add k 1 0
226end
227note d = a * b / R mod m (Montgomery, R = 2^(32k)); d may equal a or b
228fn ot_ec_mul w edit ulong 1024 d ulong a ulong b ulong m ulong to void
229t = new ulong 16 0
230k = ot_ec_k w m
231for i ulong k
232ot_ec_row1 t w a get w add mul b 16 i k
233ot_ec_row2 t w m k
234end
235for i ulong 12
236v = ulong 0
237if lt i k
238v = get t i
239end
240put w add mul d 16 i v
241end
242ot_ec_cond w d m get t k
243end
244note d = a ^ e (Montgomery form in and out; e an ordinary number in slot e, bits from the top)
245fn ot_ec_pow w edit ulong 1024 d ulong a ulong e ulong m ulong r ulong to void
246note r: slot holding R mod m (Montgomery one); uses slot 62 as the accumulator
247ot_ec_copy w 62 r
248k = ot_ec_k w m
249for j ulong mul k 32
250i = sub sub mul k 32 1 j
251ot_ec_mul w 62 62 62 m
252if ot_ec_bit w e i
253ot_ec_mul w 62 62 a m
254end
255end
256ot_ec_copy w d 62
257end
258
259note ======== curve setup ========
260note modulus slot m from hex: limbs, k, -m^-1 mod 2^32 (Newton: v = v (2 - m0 v), 5 steps double the bits each time)
261fn ot_ec_modulus w edit ulong 1024 m ulong t view uchar * k ulong to void
262ot_ec_hex w m t
263o = mul m 16
264put w add o 15 k
265m0 = get w o
266v = ulong 1
267for i ulong 5
268v = and mul v sub 2 mul m0 v 4294967295
269end
270put w add o 14 and neg v 4294967295
271end
272note slot d = 2^(32k e) mod m by doubling 1 (public, small): e = 1 gives R, e = 2 gives R^2
273fn ot_ec_rpow w edit ulong 1024 d ulong m ulong e ulong to void
274ot_ec_zero w d
275put w mul d 16 1
276k = ot_ec_k w m
277for i ulong mul mul k 32 e
278ot_ec_add w d d d m
279end
280end
281note slot d = m - 2
282fn ot_ec_m2 w edit ulong 1024 d ulong m ulong to void
283ot_ec_copy w d m
284o = mul d 16
285x = sub get w o 2
286put w o x
287note m is odd and > 2: no borrow beyond limb 0 for these moduli (low limb of p and n is >= 2)
288end
289fn ot_ec_setup w edit ulong 1024 curve ulong to bool
290for i ulong 1024
291put w i 0
292end
293if eq curve 1
294ot_ec_modulus w OT_EC_P ot_ec_p256p 8
295ot_ec_modulus w OT_EC_N ot_ec_p256n 8
296ot_ec_hex w OT_EC_B ot_ec_p256b
297ot_ec_hex w OT_EC_GX ot_ec_p256x
298ot_ec_hex w add OT_EC_GX 1 ot_ec_p256y
299else
300if ne curve 2
301ret false
302end
303ot_ec_modulus w OT_EC_P ot_ec_p384p 12
304ot_ec_modulus w OT_EC_N ot_ec_p384n 12
305ot_ec_hex w OT_EC_B ot_ec_p384b
306ot_ec_hex w OT_EC_GX ot_ec_p384x
307ot_ec_hex w add OT_EC_GX 1 ot_ec_p384y
308end
309ot_ec_rpow w OT_EC_RP OT_EC_P 1
310ot_ec_rpow w OT_EC_R2P OT_EC_P 2
311ot_ec_rpow w OT_EC_RN OT_EC_N 1
312ot_ec_rpow w OT_EC_R2N OT_EC_N 2
313ot_ec_zero w OT_EC_ONE
314put w mul OT_EC_ONE 16 1
315ot_ec_m2 w OT_EC_PM2 OT_EC_P
316ot_ec_m2 w OT_EC_NM2 OT_EC_N
317ot_ec_mul w OT_EC_B OT_EC_B OT_EC_R2P OT_EC_P
318ot_ec_tomont w OT_EC_GX
319ret true
320end
321note the affine point at slots (s, s+1) into Montgomery Jacobian form (s, s+1, s+2), Z = 1
322fn ot_ec_tomont w edit ulong 1024 s ulong to void
323ot_ec_mul w s s OT_EC_R2P OT_EC_P
324ot_ec_mul w add s 1 add s 1 OT_EC_R2P OT_EC_P
325ot_ec_copy w add s 2 OT_EC_RP
326end
327note y^2 == x^3 - 3x + b for the affine Montgomery point (s, s+1)
328fn ot_ec_oncurve w edit ulong 1024 s ulong to bool
329ot_ec_mul w 30 add s 1 add s 1 OT_EC_P
330ot_ec_mul w 31 s s OT_EC_P
331ot_ec_mul w 31 31 s OT_EC_P
332ot_ec_sub w 31 31 s OT_EC_P
333ot_ec_sub w 31 31 s OT_EC_P
334ot_ec_sub w 31 31 s OT_EC_P
335ot_ec_add w 31 31 OT_EC_B OT_EC_P
336ret ot_ec_eq w 30 31
337end
338
339note ======== points (Jacobian, a = -3) ========
340note d = 2 s (d may be s). Formulas: delta = Z^2, gamma = Y^2, beta = X gamma, alpha = 3 (X - delta)(X + delta),
341note X3 = alpha^2 - 8 beta, Z3 = (Y + Z)^2 - gamma - delta, Y3 = alpha (4 beta - X3) - 8 gamma^2
342fn ot_ec_dbl w edit ulong 1024 d ulong s ulong to void
343p = OT_EC_P
344if ot_ec_iszero w add s 2
345ot_ec_pcopy w d s
346ret
347end
348ot_ec_mul w 40 add s 2 add s 2 p
349ot_ec_mul w 41 add s 1 add s 1 p
350ot_ec_mul w 42 s 41 p
351ot_ec_sub w 43 s 40 p
352ot_ec_add w 44 s 40 p
353ot_ec_mul w 43 43 44 p
354ot_ec_add w 44 43 43 p
355ot_ec_add w 43 44 43 p
356ot_ec_add w 45 add s 1 add s 2 p
357ot_ec_mul w 45 45 45 p
358ot_ec_sub w 45 45 41 p
359ot_ec_sub w add d 2 45 40 p
360ot_ec_add w 46 42 42 p
361ot_ec_add w 46 46 46 p
362ot_ec_add w 47 46 46 p
363ot_ec_mul w 48 43 43 p
364ot_ec_sub w d 48 47 p
365ot_ec_sub w 46 46 d p
366ot_ec_mul w 46 43 46 p
367ot_ec_mul w 41 41 41 p
368ot_ec_add w 41 41 41 p
369ot_ec_add w 41 41 41 p
370ot_ec_add w 41 41 41 p
371ot_ec_sub w add d 1 46 41 p
372end
373fn ot_ec_pcopy w edit ulong 1024 d ulong s ulong to void
374if ne d s
375ot_ec_copy w d s
376ot_ec_copy w add d 1 add s 1
377ot_ec_copy w add d 2 add s 2
378end
379end
380note d = a + b (d may be a). U1 = X1 Z2^2, U2 = X2 Z1^2, S1 = Y1 Z2^3, S2 = Y2 Z1^3, H = U2 - U1, R = S2 - S1;
381note H = 0: R = 0 -> doubling, else infinity. X3 = R^2 - H^3 - 2 U1 H^2, Y3 = R (U1 H^2 - X3) - S1 H^3, Z3 = H Z1 Z2
382fn ot_ec_padd w edit ulong 1024 d ulong a ulong b ulong to void
383p = OT_EC_P
384if ot_ec_iszero w add a 2
385ot_ec_pcopy w d b
386ret
387end
388if ot_ec_iszero w add b 2
389ot_ec_pcopy w d a
390ret
391end
392ot_ec_mul w 50 add b 2 add b 2 p
393ot_ec_mul w 51 add a 2 add a 2 p
394ot_ec_mul w 52 a 50 p
395ot_ec_mul w 53 b 51 p
396ot_ec_mul w 54 add a 1 add b 2 p
397ot_ec_mul w 54 54 50 p
398ot_ec_mul w 55 add b 1 add a 2 p
399ot_ec_mul w 55 55 51 p
400ot_ec_sub w 56 53 52 p
401ot_ec_sub w 57 55 54 p
402if ot_ec_iszero w 56
403if ot_ec_iszero w 57
404ot_ec_dbl w d a
405ret
406end
407ot_ec_zero w add d 2
408ret
409end
410ot_ec_padd2 w d a b
411end
412fn ot_ec_padd2 w edit ulong 1024 d ulong a ulong b ulong to void
413p = OT_EC_P
414ot_ec_mul w 58 56 56 p
415ot_ec_mul w 59 58 56 p
416ot_ec_mul w 52 52 58 p
417ot_ec_mul w 60 add a 2 add b 2 p
418ot_ec_mul w add d 2 60 56 p
419ot_ec_mul w 61 57 57 p
420ot_ec_sub w 61 61 59 p
421ot_ec_sub w 61 61 52 p
422ot_ec_sub w d 61 52 p
423ot_ec_sub w 52 52 d p
424ot_ec_mul w 52 57 52 p
425ot_ec_mul w 54 54 59 p
426ot_ec_sub w add d 1 52 54 p
427end
428
429note ======== the API ========
430note load the public key: uncompressed SEC 1 point 04 || X || Y for the curve (1 P-256, 2 P-384); coordinates < p,
431note on the curve (which also excludes infinity, which has no affine form here)
432fn ot_ec_key curve ulong pub view uchar * to bool
433put ot_ec_v 0 0
434w = new ulong 1024 0
435if not ot_ec_setup w curve
436ret false
437end
438c = ulong 32
439if eq curve 2
440c = 48
441end
442note the length first, then the first byte: `or` evaluates both sides, so they are two tests
443if ne len pub add mul c 2 1
444ret false
445end
446if ne get pub 0 4
447ret false
448end
449if not ot_ec_import w OT_EC_QX part pub 1 c
450ret false
451end
452if not ot_ec_import w add OT_EC_QX 1 part pub add 1 c c
453ret false
454end
455if or not ot_ec_lt w OT_EC_QX OT_EC_P not ot_ec_lt w add OT_EC_QX 1 OT_EC_P
456ret false
457end
458ot_ec_tomont w OT_EC_QX
459if not ot_ec_oncurve w OT_EC_QX
460ret false
461end
462for i ulong len pub
463put ot_ec_pub i get pub i
464end
465put ot_ec_v 0 curve
466ret true
467end
468note strict DER ECDSA-Sig-Value: SEQUENCE { INTEGER r, INTEGER s }, short-form lengths only (the largest legal
469note value for these curves is below 128 bytes), minimal positive integers, nothing after the SEQUENCE
470fn ot_ec_verify alg ulong digest view uchar 64 sig view uchar * to bool
471n = len sig
472if lt n 8
473ret false
474end
475if or ne get sig 0 48 ne add 2 ulong get sig 1 n
476ret false
477end
478rs = new ulong 4 0
479if not ot_ec_derint sig 2 rs 0
480ret false
481end
482if not ot_ec_derint sig get rs 1 rs 2
483ret false
484end
485if ne get rs 3 n
486ret false
487end
488raw = new uchar 96 0
489c = ulong 32
490if eq get ot_ec_v 0 2
491c = 48
492end
493if not ot_ec_place sig get rs 0 sub get rs 1 get rs 0 raw c
494ret false
495end
496if not ot_ec_place sig get rs 2 sub get rs 3 get rs 2 raw add shl c 8 c
497ret false
498end
499ret ot_ec_verraw alg digest part raw 0 mul c 2
500end
501note one INTEGER at sig[p]: rs[at] = content start, rs[at+1] = position after it; false when not strict DER
502fn ot_ec_derint sig view uchar * p ulong rs edit ulong 4 o ulong to bool
503n = len sig
504if gt add p 2 n
505ret false
506end
507if ne get sig p 2
508ret false
509end
510l = ulong get sig add p 1
511if or eq l 0 ge l 128
512ret false
513end
514s = add p 2
515if gt add s l n
516ret false
517end
518if ne and get sig s 128 0
519ret false
520end
521if gt l 1
522if and eq get sig s 0 eq and get sig add s 1 128 0
523ret false
524end
525end
526put rs o s
527put rs add o 1 add s l
528ret true
529end
530note copy the integer content sig[s, s+l) right-aligned into raw[o, o+c), dropping one leading zero;
531note oc = 256 o + c (the parameter-slot limit)
532fn ot_ec_place sig view uchar * s ulong l ulong raw edit uchar 96 oc ulong to bool
533c = and oc 255
534a = s
535m = l
536if and gt m 1 eq get sig a 0
537a = add a 1
538m = sub m 1
539end
540if gt m c
541ret false
542end
543q = add shr oc 8 sub c m
544for i ulong m
545put raw add q i get sig add a i
546end
547ret true
548end
549note verify r || s (each the curve's byte length) against the loaded key
550fn ot_ec_verraw alg ulong digest view uchar 64 rs view uchar * to bool
551curve = get ot_ec_v 0
552if eq curve 0
553ret false
554end
555c = ulong 32
556if eq curve 2
557c = 48
558end
559if ne len rs mul c 2
560ret false
561end
562hl = ulong 0
563if eq alg 0
564hl = 32
565end
566if eq alg 1
567hl = 48
568end
569if eq alg 2
570hl = 64
571end
572if eq hl 0
573ret false
574end
575w = new ulong 1024 0
576ok = ot_ec_setup w curve
577if not ot_ec_loadq w c
578ret false
579end
580ok = ot_ec_import w OT_EC_S1 part rs 0 c
581ok = ot_ec_import w OT_EC_S2 part rs c c
582if or ot_ec_iszero w OT_EC_S1 ot_ec_iszero w OT_EC_S2
583ret false
584end
585if or not ot_ec_lt w OT_EC_S1 OT_EC_N not ot_ec_lt w OT_EC_S2 OT_EC_N
586ret false
587end
588note e: the leftmost min(hash bits, order bits) bits of the digest; order bits are 8c here
589m = hl
590if gt m c
591m = c
592end
593ok = ot_ec_import w OT_EC_E part digest 0 m
594ot_ec_cond w OT_EC_E OT_EC_N 0
595ret ot_ec_check w
596end
597fn ot_ec_loadq w edit ulong 1024 c ulong to bool
598q = new uchar 97 0
599for i ulong add mul c 2 1
600put q i get ot_ec_pub i
601end
602ok = ot_ec_import w OT_EC_QX part q 1 c
603ok = ot_ec_import w add OT_EC_QX 1 part q add 1 c c
604ot_ec_tomont w OT_EC_QX
605ret true
606end
607note w = s^-1, u1 = e w, u2 = r w (mod n); R = u1 G + u2 Q; accept iff R is finite and x(R) mod n == r
608fn ot_ec_check w edit ulong 1024 to bool
609n = OT_EC_N
610ot_ec_mul w OT_EC_W OT_EC_S2 OT_EC_R2N n
611ot_ec_pow w OT_EC_W OT_EC_W OT_EC_NM2 n OT_EC_RN
612ot_ec_mul w OT_EC_U1 OT_EC_E OT_EC_W n
613ot_ec_mul w OT_EC_U2 OT_EC_S1 OT_EC_W n
614ot_ec_padd w OT_EC_TX OT_EC_GX OT_EC_QX
615ot_ec_zero w add OT_EC_AX 2
616k = ot_ec_k w n
617for j ulong mul k 32
618i = sub sub mul k 32 1 j
619ot_ec_dbl w OT_EC_AX OT_EC_AX
620b1 = ot_ec_bit w OT_EC_U1 i
621b2 = ot_ec_bit w OT_EC_U2 i
622if and b1 b2
623ot_ec_padd w OT_EC_AX OT_EC_AX OT_EC_TX
624else
625if b1
626ot_ec_padd w OT_EC_AX OT_EC_AX OT_EC_GX
627end
628if b2
629ot_ec_padd w OT_EC_AX OT_EC_AX OT_EC_QX
630end
631end
632end
633if ot_ec_iszero w add OT_EC_AX 2
634ret false
635end
636p = OT_EC_P
637ot_ec_pow w OT_EC_X add OT_EC_AX 2 OT_EC_PM2 p OT_EC_RP
638ot_ec_mul w OT_EC_X OT_EC_X OT_EC_X p
639ot_ec_mul w OT_EC_X OT_EC_X OT_EC_AX p
640ot_ec_mul w OT_EC_X OT_EC_X OT_EC_ONE p
641ot_ec_cond w OT_EC_X n 0
642ret ot_ec_eq w OT_EC_X OT_EC_S1
643end
644fn ot_ec_bit w view ulong 1024 s ulong i ulong to bool
645ret eq and shr get w add mul s 16 shr i 5 and i 31 1 1
646end