]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/if_wg/module/curve25519.c
Import kernel WireGuard support
[FreeBSD/FreeBSD.git] / sys / dev / if_wg / module / curve25519.c
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright (C) 2015-2016 The fiat-crypto Authors.
4  * Copyright (C) 2018-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
5  *
6  * This is a machine-generated formally verified implementation of Curve25519
7  * ECDH from: <https://github.com/mit-plv/fiat-crypto>. Though originally
8  * machine generated, it has been tweaked to be suitable for use in the kernel.
9  * It is optimized for 32-bit machines and machines that cannot work efficiently
10  * with 128-bit integer types.
11  */
12
13
14 /* Added for compatibility */
15 #include <sys/types.h>
16 #include <sys/systm.h>
17 #include <sys/support.h>
18 #include <crypto/curve25519.h>
19
20 /* fe means field element. Here the field is \Z/(2^255-19). An element t,
21  * entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77
22  * t[3]+2^102 t[4]+...+2^230 t[9].
23  * fe limbs are bounded by 1.125*2^26,1.125*2^25,1.125*2^26,1.125*2^25,etc.
24  * Multiplication and carrying produce fe from fe_loose.
25  */
26 typedef struct fe { u32 v[10]; } fe;
27
28 /* fe_loose limbs are bounded by 3.375*2^26,3.375*2^25,3.375*2^26,3.375*2^25,etc
29  * Addition and subtraction produce fe_loose from (fe, fe).
30  */
31 typedef struct fe_loose { u32 v[10]; } fe_loose;
32
33 static __always_inline void fe_frombytes_impl(u32 h[10], const u8 *s)
34 {
35         /* Ignores top bit of s. */
36         u32 a0 = get_unaligned_le32(s);
37         u32 a1 = get_unaligned_le32(s+4);
38         u32 a2 = get_unaligned_le32(s+8);
39         u32 a3 = get_unaligned_le32(s+12);
40         u32 a4 = get_unaligned_le32(s+16);
41         u32 a5 = get_unaligned_le32(s+20);
42         u32 a6 = get_unaligned_le32(s+24);
43         u32 a7 = get_unaligned_le32(s+28);
44         h[0] = a0&((1<<26)-1);                    /* 26 used, 32-26 left.   26 */
45         h[1] = (a0>>26) | ((a1&((1<<19)-1))<< 6); /* (32-26) + 19 =  6+19 = 25 */
46         h[2] = (a1>>19) | ((a2&((1<<13)-1))<<13); /* (32-19) + 13 = 13+13 = 26 */
47         h[3] = (a2>>13) | ((a3&((1<< 6)-1))<<19); /* (32-13) +  6 = 19+ 6 = 25 */
48         h[4] = (a3>> 6);                          /* (32- 6)              = 26 */
49         h[5] = a4&((1<<25)-1);                    /*                        25 */
50         h[6] = (a4>>25) | ((a5&((1<<19)-1))<< 7); /* (32-25) + 19 =  7+19 = 26 */
51         h[7] = (a5>>19) | ((a6&((1<<12)-1))<<13); /* (32-19) + 12 = 13+12 = 25 */
52         h[8] = (a6>>12) | ((a7&((1<< 6)-1))<<20); /* (32-12) +  6 = 20+ 6 = 26 */
53         h[9] = (a7>> 6)&((1<<25)-1); /*                                     25 */
54 }
55
56 static __always_inline void fe_frombytes(fe *h, const u8 *s)
57 {
58         fe_frombytes_impl(h->v, s);
59 }
60
61 static __always_inline u8 /*bool*/
62 addcarryx_u25(u8 /*bool*/ c, u32 a, u32 b, u32 *low)
63 {
64         /* This function extracts 25 bits of result and 1 bit of carry
65          * (26 total), so a 32-bit intermediate is sufficient.
66          */
67         u32 x = a + b + c;
68         *low = x & ((1 << 25) - 1);
69         return (x >> 25) & 1;
70 }
71
72 static __always_inline u8 /*bool*/
73 addcarryx_u26(u8 /*bool*/ c, u32 a, u32 b, u32 *low)
74 {
75         /* This function extracts 26 bits of result and 1 bit of carry
76          * (27 total), so a 32-bit intermediate is sufficient.
77          */
78         u32 x = a + b + c;
79         *low = x & ((1 << 26) - 1);
80         return (x >> 26) & 1;
81 }
82
83 static __always_inline u8 /*bool*/
84 subborrow_u25(u8 /*bool*/ c, u32 a, u32 b, u32 *low)
85 {
86         /* This function extracts 25 bits of result and 1 bit of borrow
87          * (26 total), so a 32-bit intermediate is sufficient.
88          */
89         u32 x = a - b - c;
90         *low = x & ((1 << 25) - 1);
91         return x >> 31;
92 }
93
94 static __always_inline u8 /*bool*/
95 subborrow_u26(u8 /*bool*/ c, u32 a, u32 b, u32 *low)
96 {
97         /* This function extracts 26 bits of result and 1 bit of borrow
98          *(27 total), so a 32-bit intermediate is sufficient.
99          */
100         u32 x = a - b - c;
101         *low = x & ((1 << 26) - 1);
102         return x >> 31;
103 }
104
105 static __always_inline u32 cmovznz32(u32 t, u32 z, u32 nz)
106 {
107         t = -!!t; /* all set if nonzero, 0 if 0 */
108         return (t&nz) | ((~t)&z);
109 }
110
111 static __always_inline void fe_freeze(u32 out[10], const u32 in1[10])
112 {
113         { const u32 x17 = in1[9];
114         { const u32 x18 = in1[8];
115         { const u32 x16 = in1[7];
116         { const u32 x14 = in1[6];
117         { const u32 x12 = in1[5];
118         { const u32 x10 = in1[4];
119         { const u32 x8 = in1[3];
120         { const u32 x6 = in1[2];
121         { const u32 x4 = in1[1];
122         { const u32 x2 = in1[0];
123         { u32 x20; u8/*bool*/ x21 = subborrow_u26(0x0, x2, 0x3ffffed, &x20);
124         { u32 x23; u8/*bool*/ x24 = subborrow_u25(x21, x4, 0x1ffffff, &x23);
125         { u32 x26; u8/*bool*/ x27 = subborrow_u26(x24, x6, 0x3ffffff, &x26);
126         { u32 x29; u8/*bool*/ x30 = subborrow_u25(x27, x8, 0x1ffffff, &x29);
127         { u32 x32; u8/*bool*/ x33 = subborrow_u26(x30, x10, 0x3ffffff, &x32);
128         { u32 x35; u8/*bool*/ x36 = subborrow_u25(x33, x12, 0x1ffffff, &x35);
129         { u32 x38; u8/*bool*/ x39 = subborrow_u26(x36, x14, 0x3ffffff, &x38);
130         { u32 x41; u8/*bool*/ x42 = subborrow_u25(x39, x16, 0x1ffffff, &x41);
131         { u32 x44; u8/*bool*/ x45 = subborrow_u26(x42, x18, 0x3ffffff, &x44);
132         { u32 x47; u8/*bool*/ x48 = subborrow_u25(x45, x17, 0x1ffffff, &x47);
133         { u32 x49 = cmovznz32(x48, 0x0, 0xffffffff);
134         { u32 x50 = (x49 & 0x3ffffed);
135         { u32 x52; u8/*bool*/ x53 = addcarryx_u26(0x0, x20, x50, &x52);
136         { u32 x54 = (x49 & 0x1ffffff);
137         { u32 x56; u8/*bool*/ x57 = addcarryx_u25(x53, x23, x54, &x56);
138         { u32 x58 = (x49 & 0x3ffffff);
139         { u32 x60; u8/*bool*/ x61 = addcarryx_u26(x57, x26, x58, &x60);
140         { u32 x62 = (x49 & 0x1ffffff);
141         { u32 x64; u8/*bool*/ x65 = addcarryx_u25(x61, x29, x62, &x64);
142         { u32 x66 = (x49 & 0x3ffffff);
143         { u32 x68; u8/*bool*/ x69 = addcarryx_u26(x65, x32, x66, &x68);
144         { u32 x70 = (x49 & 0x1ffffff);
145         { u32 x72; u8/*bool*/ x73 = addcarryx_u25(x69, x35, x70, &x72);
146         { u32 x74 = (x49 & 0x3ffffff);
147         { u32 x76; u8/*bool*/ x77 = addcarryx_u26(x73, x38, x74, &x76);
148         { u32 x78 = (x49 & 0x1ffffff);
149         { u32 x80; u8/*bool*/ x81 = addcarryx_u25(x77, x41, x78, &x80);
150         { u32 x82 = (x49 & 0x3ffffff);
151         { u32 x84; u8/*bool*/ x85 = addcarryx_u26(x81, x44, x82, &x84);
152         { u32 x86 = (x49 & 0x1ffffff);
153         { u32 x88; addcarryx_u25(x85, x47, x86, &x88);
154         out[0] = x52;
155         out[1] = x56;
156         out[2] = x60;
157         out[3] = x64;
158         out[4] = x68;
159         out[5] = x72;
160         out[6] = x76;
161         out[7] = x80;
162         out[8] = x84;
163         out[9] = x88;
164         }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
165 }
166
167 static __always_inline void fe_tobytes(u8 s[32], const fe *f)
168 {
169         u32 h[10];
170         fe_freeze(h, f->v);
171         s[0] = h[0] >> 0;
172         s[1] = h[0] >> 8;
173         s[2] = h[0] >> 16;
174         s[3] = (h[0] >> 24) | (h[1] << 2);
175         s[4] = h[1] >> 6;
176         s[5] = h[1] >> 14;
177         s[6] = (h[1] >> 22) | (h[2] << 3);
178         s[7] = h[2] >> 5;
179         s[8] = h[2] >> 13;
180         s[9] = (h[2] >> 21) | (h[3] << 5);
181         s[10] = h[3] >> 3;
182         s[11] = h[3] >> 11;
183         s[12] = (h[3] >> 19) | (h[4] << 6);
184         s[13] = h[4] >> 2;
185         s[14] = h[4] >> 10;
186         s[15] = h[4] >> 18;
187         s[16] = h[5] >> 0;
188         s[17] = h[5] >> 8;
189         s[18] = h[5] >> 16;
190         s[19] = (h[5] >> 24) | (h[6] << 1);
191         s[20] = h[6] >> 7;
192         s[21] = h[6] >> 15;
193         s[22] = (h[6] >> 23) | (h[7] << 3);
194         s[23] = h[7] >> 5;
195         s[24] = h[7] >> 13;
196         s[25] = (h[7] >> 21) | (h[8] << 4);
197         s[26] = h[8] >> 4;
198         s[27] = h[8] >> 12;
199         s[28] = (h[8] >> 20) | (h[9] << 6);
200         s[29] = h[9] >> 2;
201         s[30] = h[9] >> 10;
202         s[31] = h[9] >> 18;
203 }
204
205 /* h = f */
206 static __always_inline void fe_copy(fe *h, const fe *f)
207 {
208         memmove(h, f, sizeof(u32) * 10);
209 }
210
211 static __always_inline void fe_copy_lt(fe_loose *h, const fe *f)
212 {
213         memmove(h, f, sizeof(u32) * 10);
214 }
215
216 /* h = 0 */
217 static __always_inline void fe_0(fe *h)
218 {
219         memset(h, 0, sizeof(u32) * 10);
220 }
221
222 /* h = 1 */
223 static __always_inline void fe_1(fe *h)
224 {
225         memset(h, 0, sizeof(u32) * 10);
226         h->v[0] = 1;
227 }
228
229 static void fe_add_impl(u32 out[10], const u32 in1[10], const u32 in2[10])
230 {
231         { const u32 x20 = in1[9];
232         { const u32 x21 = in1[8];
233         { const u32 x19 = in1[7];
234         { const u32 x17 = in1[6];
235         { const u32 x15 = in1[5];
236         { const u32 x13 = in1[4];
237         { const u32 x11 = in1[3];
238         { const u32 x9 = in1[2];
239         { const u32 x7 = in1[1];
240         { const u32 x5 = in1[0];
241         { const u32 x38 = in2[9];
242         { const u32 x39 = in2[8];
243         { const u32 x37 = in2[7];
244         { const u32 x35 = in2[6];
245         { const u32 x33 = in2[5];
246         { const u32 x31 = in2[4];
247         { const u32 x29 = in2[3];
248         { const u32 x27 = in2[2];
249         { const u32 x25 = in2[1];
250         { const u32 x23 = in2[0];
251         out[0] = (x5 + x23);
252         out[1] = (x7 + x25);
253         out[2] = (x9 + x27);
254         out[3] = (x11 + x29);
255         out[4] = (x13 + x31);
256         out[5] = (x15 + x33);
257         out[6] = (x17 + x35);
258         out[7] = (x19 + x37);
259         out[8] = (x21 + x39);
260         out[9] = (x20 + x38);
261         }}}}}}}}}}}}}}}}}}}}
262 }
263
264 /* h = f + g
265  * Can overlap h with f or g.
266  */
267 static __always_inline void fe_add(fe_loose *h, const fe *f, const fe *g)
268 {
269         fe_add_impl(h->v, f->v, g->v);
270 }
271
272 static void fe_sub_impl(u32 out[10], const u32 in1[10], const u32 in2[10])
273 {
274         { const u32 x20 = in1[9];
275         { const u32 x21 = in1[8];
276         { const u32 x19 = in1[7];
277         { const u32 x17 = in1[6];
278         { const u32 x15 = in1[5];
279         { const u32 x13 = in1[4];
280         { const u32 x11 = in1[3];
281         { const u32 x9 = in1[2];
282         { const u32 x7 = in1[1];
283         { const u32 x5 = in1[0];
284         { const u32 x38 = in2[9];
285         { const u32 x39 = in2[8];
286         { const u32 x37 = in2[7];
287         { const u32 x35 = in2[6];
288         { const u32 x33 = in2[5];
289         { const u32 x31 = in2[4];
290         { const u32 x29 = in2[3];
291         { const u32 x27 = in2[2];
292         { const u32 x25 = in2[1];
293         { const u32 x23 = in2[0];
294         out[0] = ((0x7ffffda + x5) - x23);
295         out[1] = ((0x3fffffe + x7) - x25);
296         out[2] = ((0x7fffffe + x9) - x27);
297         out[3] = ((0x3fffffe + x11) - x29);
298         out[4] = ((0x7fffffe + x13) - x31);
299         out[5] = ((0x3fffffe + x15) - x33);
300         out[6] = ((0x7fffffe + x17) - x35);
301         out[7] = ((0x3fffffe + x19) - x37);
302         out[8] = ((0x7fffffe + x21) - x39);
303         out[9] = ((0x3fffffe + x20) - x38);
304         }}}}}}}}}}}}}}}}}}}}
305 }
306
307 /* h = f - g
308  * Can overlap h with f or g.
309  */
310 static __always_inline void fe_sub(fe_loose *h, const fe *f, const fe *g)
311 {
312         fe_sub_impl(h->v, f->v, g->v);
313 }
314
315 static void fe_mul_impl(u32 out[10], const u32 in1[10], const u32 in2[10])
316 {
317         { const u32 x20 = in1[9];
318         { const u32 x21 = in1[8];
319         { const u32 x19 = in1[7];
320         { const u32 x17 = in1[6];
321         { const u32 x15 = in1[5];
322         { const u32 x13 = in1[4];
323         { const u32 x11 = in1[3];
324         { const u32 x9 = in1[2];
325         { const u32 x7 = in1[1];
326         { const u32 x5 = in1[0];
327         { const u32 x38 = in2[9];
328         { const u32 x39 = in2[8];
329         { const u32 x37 = in2[7];
330         { const u32 x35 = in2[6];
331         { const u32 x33 = in2[5];
332         { const u32 x31 = in2[4];
333         { const u32 x29 = in2[3];
334         { const u32 x27 = in2[2];
335         { const u32 x25 = in2[1];
336         { const u32 x23 = in2[0];
337         { u64 x40 = ((u64)x23 * x5);
338         { u64 x41 = (((u64)x23 * x7) + ((u64)x25 * x5));
339         { u64 x42 = ((((u64)(0x2 * x25) * x7) + ((u64)x23 * x9)) + ((u64)x27 * x5));
340         { u64 x43 = (((((u64)x25 * x9) + ((u64)x27 * x7)) + ((u64)x23 * x11)) + ((u64)x29 * x5));
341         { u64 x44 = (((((u64)x27 * x9) + (0x2 * (((u64)x25 * x11) + ((u64)x29 * x7)))) + ((u64)x23 * x13)) + ((u64)x31 * x5));
342         { u64 x45 = (((((((u64)x27 * x11) + ((u64)x29 * x9)) + ((u64)x25 * x13)) + ((u64)x31 * x7)) + ((u64)x23 * x15)) + ((u64)x33 * x5));
343         { u64 x46 = (((((0x2 * ((((u64)x29 * x11) + ((u64)x25 * x15)) + ((u64)x33 * x7))) + ((u64)x27 * x13)) + ((u64)x31 * x9)) + ((u64)x23 * x17)) + ((u64)x35 * x5));
344         { u64 x47 = (((((((((u64)x29 * x13) + ((u64)x31 * x11)) + ((u64)x27 * x15)) + ((u64)x33 * x9)) + ((u64)x25 * x17)) + ((u64)x35 * x7)) + ((u64)x23 * x19)) + ((u64)x37 * x5));
345         { u64 x48 = (((((((u64)x31 * x13) + (0x2 * (((((u64)x29 * x15) + ((u64)x33 * x11)) + ((u64)x25 * x19)) + ((u64)x37 * x7)))) + ((u64)x27 * x17)) + ((u64)x35 * x9)) + ((u64)x23 * x21)) + ((u64)x39 * x5));
346         { u64 x49 = (((((((((((u64)x31 * x15) + ((u64)x33 * x13)) + ((u64)x29 * x17)) + ((u64)x35 * x11)) + ((u64)x27 * x19)) + ((u64)x37 * x9)) + ((u64)x25 * x21)) + ((u64)x39 * x7)) + ((u64)x23 * x20)) + ((u64)x38 * x5));
347         { u64 x50 = (((((0x2 * ((((((u64)x33 * x15) + ((u64)x29 * x19)) + ((u64)x37 * x11)) + ((u64)x25 * x20)) + ((u64)x38 * x7))) + ((u64)x31 * x17)) + ((u64)x35 * x13)) + ((u64)x27 * x21)) + ((u64)x39 * x9));
348         { u64 x51 = (((((((((u64)x33 * x17) + ((u64)x35 * x15)) + ((u64)x31 * x19)) + ((u64)x37 * x13)) + ((u64)x29 * x21)) + ((u64)x39 * x11)) + ((u64)x27 * x20)) + ((u64)x38 * x9));
349         { u64 x52 = (((((u64)x35 * x17) + (0x2 * (((((u64)x33 * x19) + ((u64)x37 * x15)) + ((u64)x29 * x20)) + ((u64)x38 * x11)))) + ((u64)x31 * x21)) + ((u64)x39 * x13));
350         { u64 x53 = (((((((u64)x35 * x19) + ((u64)x37 * x17)) + ((u64)x33 * x21)) + ((u64)x39 * x15)) + ((u64)x31 * x20)) + ((u64)x38 * x13));
351         { u64 x54 = (((0x2 * ((((u64)x37 * x19) + ((u64)x33 * x20)) + ((u64)x38 * x15))) + ((u64)x35 * x21)) + ((u64)x39 * x17));
352         { u64 x55 = (((((u64)x37 * x21) + ((u64)x39 * x19)) + ((u64)x35 * x20)) + ((u64)x38 * x17));
353         { u64 x56 = (((u64)x39 * x21) + (0x2 * (((u64)x37 * x20) + ((u64)x38 * x19))));
354         { u64 x57 = (((u64)x39 * x20) + ((u64)x38 * x21));
355         { u64 x58 = ((u64)(0x2 * x38) * x20);
356         { u64 x59 = (x48 + (x58 << 0x4));
357         { u64 x60 = (x59 + (x58 << 0x1));
358         { u64 x61 = (x60 + x58);
359         { u64 x62 = (x47 + (x57 << 0x4));
360         { u64 x63 = (x62 + (x57 << 0x1));
361         { u64 x64 = (x63 + x57);
362         { u64 x65 = (x46 + (x56 << 0x4));
363         { u64 x66 = (x65 + (x56 << 0x1));
364         { u64 x67 = (x66 + x56);
365         { u64 x68 = (x45 + (x55 << 0x4));
366         { u64 x69 = (x68 + (x55 << 0x1));
367         { u64 x70 = (x69 + x55);
368         { u64 x71 = (x44 + (x54 << 0x4));
369         { u64 x72 = (x71 + (x54 << 0x1));
370         { u64 x73 = (x72 + x54);
371         { u64 x74 = (x43 + (x53 << 0x4));
372         { u64 x75 = (x74 + (x53 << 0x1));
373         { u64 x76 = (x75 + x53);
374         { u64 x77 = (x42 + (x52 << 0x4));
375         { u64 x78 = (x77 + (x52 << 0x1));
376         { u64 x79 = (x78 + x52);
377         { u64 x80 = (x41 + (x51 << 0x4));
378         { u64 x81 = (x80 + (x51 << 0x1));
379         { u64 x82 = (x81 + x51);
380         { u64 x83 = (x40 + (x50 << 0x4));
381         { u64 x84 = (x83 + (x50 << 0x1));
382         { u64 x85 = (x84 + x50);
383         { u64 x86 = (x85 >> 0x1a);
384         { u32 x87 = ((u32)x85 & 0x3ffffff);
385         { u64 x88 = (x86 + x82);
386         { u64 x89 = (x88 >> 0x19);
387         { u32 x90 = ((u32)x88 & 0x1ffffff);
388         { u64 x91 = (x89 + x79);
389         { u64 x92 = (x91 >> 0x1a);
390         { u32 x93 = ((u32)x91 & 0x3ffffff);
391         { u64 x94 = (x92 + x76);
392         { u64 x95 = (x94 >> 0x19);
393         { u32 x96 = ((u32)x94 & 0x1ffffff);
394         { u64 x97 = (x95 + x73);
395         { u64 x98 = (x97 >> 0x1a);
396         { u32 x99 = ((u32)x97 & 0x3ffffff);
397         { u64 x100 = (x98 + x70);
398         { u64 x101 = (x100 >> 0x19);
399         { u32 x102 = ((u32)x100 & 0x1ffffff);
400         { u64 x103 = (x101 + x67);
401         { u64 x104 = (x103 >> 0x1a);
402         { u32 x105 = ((u32)x103 & 0x3ffffff);
403         { u64 x106 = (x104 + x64);
404         { u64 x107 = (x106 >> 0x19);
405         { u32 x108 = ((u32)x106 & 0x1ffffff);
406         { u64 x109 = (x107 + x61);
407         { u64 x110 = (x109 >> 0x1a);
408         { u32 x111 = ((u32)x109 & 0x3ffffff);
409         { u64 x112 = (x110 + x49);
410         { u64 x113 = (x112 >> 0x19);
411         { u32 x114 = ((u32)x112 & 0x1ffffff);
412         { u64 x115 = (x87 + (0x13 * x113));
413         { u32 x116 = (u32) (x115 >> 0x1a);
414         { u32 x117 = ((u32)x115 & 0x3ffffff);
415         { u32 x118 = (x116 + x90);
416         { u32 x119 = (x118 >> 0x19);
417         { u32 x120 = (x118 & 0x1ffffff);
418         out[0] = x117;
419         out[1] = x120;
420         out[2] = (x119 + x93);
421         out[3] = x96;
422         out[4] = x99;
423         out[5] = x102;
424         out[6] = x105;
425         out[7] = x108;
426         out[8] = x111;
427         out[9] = x114;
428         }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
429 }
430
431 static __always_inline void fe_mul_ttt(fe *h, const fe *f, const fe *g)
432 {
433         fe_mul_impl(h->v, f->v, g->v);
434 }
435
436 static __always_inline void fe_mul_tlt(fe *h, const fe_loose *f, const fe *g)
437 {
438         fe_mul_impl(h->v, f->v, g->v);
439 }
440
441 static __always_inline void
442 fe_mul_tll(fe *h, const fe_loose *f, const fe_loose *g)
443 {
444         fe_mul_impl(h->v, f->v, g->v);
445 }
446
447 static void fe_sqr_impl(u32 out[10], const u32 in1[10])
448 {
449         { const u32 x17 = in1[9];
450         { const u32 x18 = in1[8];
451         { const u32 x16 = in1[7];
452         { const u32 x14 = in1[6];
453         { const u32 x12 = in1[5];
454         { const u32 x10 = in1[4];
455         { const u32 x8 = in1[3];
456         { const u32 x6 = in1[2];
457         { const u32 x4 = in1[1];
458         { const u32 x2 = in1[0];
459         { u64 x19 = ((u64)x2 * x2);
460         { u64 x20 = ((u64)(0x2 * x2) * x4);
461         { u64 x21 = (0x2 * (((u64)x4 * x4) + ((u64)x2 * x6)));
462         { u64 x22 = (0x2 * (((u64)x4 * x6) + ((u64)x2 * x8)));
463         { u64 x23 = ((((u64)x6 * x6) + ((u64)(0x4 * x4) * x8)) + ((u64)(0x2 * x2) * x10));
464         { u64 x24 = (0x2 * ((((u64)x6 * x8) + ((u64)x4 * x10)) + ((u64)x2 * x12)));
465         { u64 x25 = (0x2 * (((((u64)x8 * x8) + ((u64)x6 * x10)) + ((u64)x2 * x14)) + ((u64)(0x2 * x4) * x12)));
466         { u64 x26 = (0x2 * (((((u64)x8 * x10) + ((u64)x6 * x12)) + ((u64)x4 * x14)) + ((u64)x2 * x16)));
467         { u64 x27 = (((u64)x10 * x10) + (0x2 * ((((u64)x6 * x14) + ((u64)x2 * x18)) + (0x2 * (((u64)x4 * x16) + ((u64)x8 * x12))))));
468         { u64 x28 = (0x2 * ((((((u64)x10 * x12) + ((u64)x8 * x14)) + ((u64)x6 * x16)) + ((u64)x4 * x18)) + ((u64)x2 * x17)));
469         { u64 x29 = (0x2 * (((((u64)x12 * x12) + ((u64)x10 * x14)) + ((u64)x6 * x18)) + (0x2 * (((u64)x8 * x16) + ((u64)x4 * x17)))));
470         { u64 x30 = (0x2 * (((((u64)x12 * x14) + ((u64)x10 * x16)) + ((u64)x8 * x18)) + ((u64)x6 * x17)));
471         { u64 x31 = (((u64)x14 * x14) + (0x2 * (((u64)x10 * x18) + (0x2 * (((u64)x12 * x16) + ((u64)x8 * x17))))));
472         { u64 x32 = (0x2 * ((((u64)x14 * x16) + ((u64)x12 * x18)) + ((u64)x10 * x17)));
473         { u64 x33 = (0x2 * ((((u64)x16 * x16) + ((u64)x14 * x18)) + ((u64)(0x2 * x12) * x17)));
474         { u64 x34 = (0x2 * (((u64)x16 * x18) + ((u64)x14 * x17)));
475         { u64 x35 = (((u64)x18 * x18) + ((u64)(0x4 * x16) * x17));
476         { u64 x36 = ((u64)(0x2 * x18) * x17);
477         { u64 x37 = ((u64)(0x2 * x17) * x17);
478         { u64 x38 = (x27 + (x37 << 0x4));
479         { u64 x39 = (x38 + (x37 << 0x1));
480         { u64 x40 = (x39 + x37);
481         { u64 x41 = (x26 + (x36 << 0x4));
482         { u64 x42 = (x41 + (x36 << 0x1));
483         { u64 x43 = (x42 + x36);
484         { u64 x44 = (x25 + (x35 << 0x4));
485         { u64 x45 = (x44 + (x35 << 0x1));
486         { u64 x46 = (x45 + x35);
487         { u64 x47 = (x24 + (x34 << 0x4));
488         { u64 x48 = (x47 + (x34 << 0x1));
489         { u64 x49 = (x48 + x34);
490         { u64 x50 = (x23 + (x33 << 0x4));
491         { u64 x51 = (x50 + (x33 << 0x1));
492         { u64 x52 = (x51 + x33);
493         { u64 x53 = (x22 + (x32 << 0x4));
494         { u64 x54 = (x53 + (x32 << 0x1));
495         { u64 x55 = (x54 + x32);
496         { u64 x56 = (x21 + (x31 << 0x4));
497         { u64 x57 = (x56 + (x31 << 0x1));
498         { u64 x58 = (x57 + x31);
499         { u64 x59 = (x20 + (x30 << 0x4));
500         { u64 x60 = (x59 + (x30 << 0x1));
501         { u64 x61 = (x60 + x30);
502         { u64 x62 = (x19 + (x29 << 0x4));
503         { u64 x63 = (x62 + (x29 << 0x1));
504         { u64 x64 = (x63 + x29);
505         { u64 x65 = (x64 >> 0x1a);
506         { u32 x66 = ((u32)x64 & 0x3ffffff);
507         { u64 x67 = (x65 + x61);
508         { u64 x68 = (x67 >> 0x19);
509         { u32 x69 = ((u32)x67 & 0x1ffffff);
510         { u64 x70 = (x68 + x58);
511         { u64 x71 = (x70 >> 0x1a);
512         { u32 x72 = ((u32)x70 & 0x3ffffff);
513         { u64 x73 = (x71 + x55);
514         { u64 x74 = (x73 >> 0x19);
515         { u32 x75 = ((u32)x73 & 0x1ffffff);
516         { u64 x76 = (x74 + x52);
517         { u64 x77 = (x76 >> 0x1a);
518         { u32 x78 = ((u32)x76 & 0x3ffffff);
519         { u64 x79 = (x77 + x49);
520         { u64 x80 = (x79 >> 0x19);
521         { u32 x81 = ((u32)x79 & 0x1ffffff);
522         { u64 x82 = (x80 + x46);
523         { u64 x83 = (x82 >> 0x1a);
524         { u32 x84 = ((u32)x82 & 0x3ffffff);
525         { u64 x85 = (x83 + x43);
526         { u64 x86 = (x85 >> 0x19);
527         { u32 x87 = ((u32)x85 & 0x1ffffff);
528         { u64 x88 = (x86 + x40);
529         { u64 x89 = (x88 >> 0x1a);
530         { u32 x90 = ((u32)x88 & 0x3ffffff);
531         { u64 x91 = (x89 + x28);
532         { u64 x92 = (x91 >> 0x19);
533         { u32 x93 = ((u32)x91 & 0x1ffffff);
534         { u64 x94 = (x66 + (0x13 * x92));
535         { u32 x95 = (u32) (x94 >> 0x1a);
536         { u32 x96 = ((u32)x94 & 0x3ffffff);
537         { u32 x97 = (x95 + x69);
538         { u32 x98 = (x97 >> 0x19);
539         { u32 x99 = (x97 & 0x1ffffff);
540         out[0] = x96;
541         out[1] = x99;
542         out[2] = (x98 + x72);
543         out[3] = x75;
544         out[4] = x78;
545         out[5] = x81;
546         out[6] = x84;
547         out[7] = x87;
548         out[8] = x90;
549         out[9] = x93;
550         }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
551 }
552
553 static __always_inline void fe_sq_tl(fe *h, const fe_loose *f)
554 {
555         fe_sqr_impl(h->v, f->v);
556 }
557
558 static __always_inline void fe_sq_tt(fe *h, const fe *f)
559 {
560         fe_sqr_impl(h->v, f->v);
561 }
562
563 static __always_inline void fe_loose_invert(fe *out, const fe_loose *z)
564 {
565         fe t0;
566         fe t1;
567         fe t2;
568         fe t3;
569         int i;
570
571         fe_sq_tl(&t0, z);
572         fe_sq_tt(&t1, &t0);
573         for (i = 1; i < 2; ++i)
574                 fe_sq_tt(&t1, &t1);
575         fe_mul_tlt(&t1, z, &t1);
576         fe_mul_ttt(&t0, &t0, &t1);
577         fe_sq_tt(&t2, &t0);
578         fe_mul_ttt(&t1, &t1, &t2);
579         fe_sq_tt(&t2, &t1);
580         for (i = 1; i < 5; ++i)
581                 fe_sq_tt(&t2, &t2);
582         fe_mul_ttt(&t1, &t2, &t1);
583         fe_sq_tt(&t2, &t1);
584         for (i = 1; i < 10; ++i)
585                 fe_sq_tt(&t2, &t2);
586         fe_mul_ttt(&t2, &t2, &t1);
587         fe_sq_tt(&t3, &t2);
588         for (i = 1; i < 20; ++i)
589                 fe_sq_tt(&t3, &t3);
590         fe_mul_ttt(&t2, &t3, &t2);
591         fe_sq_tt(&t2, &t2);
592         for (i = 1; i < 10; ++i)
593                 fe_sq_tt(&t2, &t2);
594         fe_mul_ttt(&t1, &t2, &t1);
595         fe_sq_tt(&t2, &t1);
596         for (i = 1; i < 50; ++i)
597                 fe_sq_tt(&t2, &t2);
598         fe_mul_ttt(&t2, &t2, &t1);
599         fe_sq_tt(&t3, &t2);
600         for (i = 1; i < 100; ++i)
601                 fe_sq_tt(&t3, &t3);
602         fe_mul_ttt(&t2, &t3, &t2);
603         fe_sq_tt(&t2, &t2);
604         for (i = 1; i < 50; ++i)
605                 fe_sq_tt(&t2, &t2);
606         fe_mul_ttt(&t1, &t2, &t1);
607         fe_sq_tt(&t1, &t1);
608         for (i = 1; i < 5; ++i)
609                 fe_sq_tt(&t1, &t1);
610         fe_mul_ttt(out, &t1, &t0);
611 }
612
613 static __always_inline void fe_invert(fe *out, const fe *z)
614 {
615         fe_loose l;
616         fe_copy_lt(&l, z);
617         fe_loose_invert(out, &l);
618 }
619
620 /* Replace (f,g) with (g,f) if b == 1;
621  * replace (f,g) with (f,g) if b == 0.
622  *
623  * Preconditions: b in {0,1}
624  */
625 static __always_inline void fe_cswap(fe *f, fe *g, unsigned int b)
626 {
627         unsigned i;
628         b = 0 - b;
629         for (i = 0; i < 10; i++) {
630                 u32 x = f->v[i] ^ g->v[i];
631                 x &= b;
632                 f->v[i] ^= x;
633                 g->v[i] ^= x;
634         }
635 }
636
637 /* NOTE: based on fiat-crypto fe_mul, edited for in2=121666, 0, 0.*/
638 static __always_inline void fe_mul_121666_impl(u32 out[10], const u32 in1[10])
639 {
640         { const u32 x20 = in1[9];
641         { const u32 x21 = in1[8];
642         { const u32 x19 = in1[7];
643         { const u32 x17 = in1[6];
644         { const u32 x15 = in1[5];
645         { const u32 x13 = in1[4];
646         { const u32 x11 = in1[3];
647         { const u32 x9 = in1[2];
648         { const u32 x7 = in1[1];
649         { const u32 x5 = in1[0];
650         { const u32 x38 = 0;
651         { const u32 x39 = 0;
652         { const u32 x37 = 0;
653         { const u32 x35 = 0;
654         { const u32 x33 = 0;
655         { const u32 x31 = 0;
656         { const u32 x29 = 0;
657         { const u32 x27 = 0;
658         { const u32 x25 = 0;
659         { const u32 x23 = 121666;
660         { u64 x40 = ((u64)x23 * x5);
661         { u64 x41 = (((u64)x23 * x7) + ((u64)x25 * x5));
662         { u64 x42 = ((((u64)(0x2 * x25) * x7) + ((u64)x23 * x9)) + ((u64)x27 * x5));
663         { u64 x43 = (((((u64)x25 * x9) + ((u64)x27 * x7)) + ((u64)x23 * x11)) + ((u64)x29 * x5));
664         { u64 x44 = (((((u64)x27 * x9) + (0x2 * (((u64)x25 * x11) + ((u64)x29 * x7)))) + ((u64)x23 * x13)) + ((u64)x31 * x5));
665         { u64 x45 = (((((((u64)x27 * x11) + ((u64)x29 * x9)) + ((u64)x25 * x13)) + ((u64)x31 * x7)) + ((u64)x23 * x15)) + ((u64)x33 * x5));
666         { u64 x46 = (((((0x2 * ((((u64)x29 * x11) + ((u64)x25 * x15)) + ((u64)x33 * x7))) + ((u64)x27 * x13)) + ((u64)x31 * x9)) + ((u64)x23 * x17)) + ((u64)x35 * x5));
667         { u64 x47 = (((((((((u64)x29 * x13) + ((u64)x31 * x11)) + ((u64)x27 * x15)) + ((u64)x33 * x9)) + ((u64)x25 * x17)) + ((u64)x35 * x7)) + ((u64)x23 * x19)) + ((u64)x37 * x5));
668         { u64 x48 = (((((((u64)x31 * x13) + (0x2 * (((((u64)x29 * x15) + ((u64)x33 * x11)) + ((u64)x25 * x19)) + ((u64)x37 * x7)))) + ((u64)x27 * x17)) + ((u64)x35 * x9)) + ((u64)x23 * x21)) + ((u64)x39 * x5));
669         { u64 x49 = (((((((((((u64)x31 * x15) + ((u64)x33 * x13)) + ((u64)x29 * x17)) + ((u64)x35 * x11)) + ((u64)x27 * x19)) + ((u64)x37 * x9)) + ((u64)x25 * x21)) + ((u64)x39 * x7)) + ((u64)x23 * x20)) + ((u64)x38 * x5));
670         { u64 x50 = (((((0x2 * ((((((u64)x33 * x15) + ((u64)x29 * x19)) + ((u64)x37 * x11)) + ((u64)x25 * x20)) + ((u64)x38 * x7))) + ((u64)x31 * x17)) + ((u64)x35 * x13)) + ((u64)x27 * x21)) + ((u64)x39 * x9));
671         { u64 x51 = (((((((((u64)x33 * x17) + ((u64)x35 * x15)) + ((u64)x31 * x19)) + ((u64)x37 * x13)) + ((u64)x29 * x21)) + ((u64)x39 * x11)) + ((u64)x27 * x20)) + ((u64)x38 * x9));
672         { u64 x52 = (((((u64)x35 * x17) + (0x2 * (((((u64)x33 * x19) + ((u64)x37 * x15)) + ((u64)x29 * x20)) + ((u64)x38 * x11)))) + ((u64)x31 * x21)) + ((u64)x39 * x13));
673         { u64 x53 = (((((((u64)x35 * x19) + ((u64)x37 * x17)) + ((u64)x33 * x21)) + ((u64)x39 * x15)) + ((u64)x31 * x20)) + ((u64)x38 * x13));
674         { u64 x54 = (((0x2 * ((((u64)x37 * x19) + ((u64)x33 * x20)) + ((u64)x38 * x15))) + ((u64)x35 * x21)) + ((u64)x39 * x17));
675         { u64 x55 = (((((u64)x37 * x21) + ((u64)x39 * x19)) + ((u64)x35 * x20)) + ((u64)x38 * x17));
676         { u64 x56 = (((u64)x39 * x21) + (0x2 * (((u64)x37 * x20) + ((u64)x38 * x19))));
677         { u64 x57 = (((u64)x39 * x20) + ((u64)x38 * x21));
678         { u64 x58 = ((u64)(0x2 * x38) * x20);
679         { u64 x59 = (x48 + (x58 << 0x4));
680         { u64 x60 = (x59 + (x58 << 0x1));
681         { u64 x61 = (x60 + x58);
682         { u64 x62 = (x47 + (x57 << 0x4));
683         { u64 x63 = (x62 + (x57 << 0x1));
684         { u64 x64 = (x63 + x57);
685         { u64 x65 = (x46 + (x56 << 0x4));
686         { u64 x66 = (x65 + (x56 << 0x1));
687         { u64 x67 = (x66 + x56);
688         { u64 x68 = (x45 + (x55 << 0x4));
689         { u64 x69 = (x68 + (x55 << 0x1));
690         { u64 x70 = (x69 + x55);
691         { u64 x71 = (x44 + (x54 << 0x4));
692         { u64 x72 = (x71 + (x54 << 0x1));
693         { u64 x73 = (x72 + x54);
694         { u64 x74 = (x43 + (x53 << 0x4));
695         { u64 x75 = (x74 + (x53 << 0x1));
696         { u64 x76 = (x75 + x53);
697         { u64 x77 = (x42 + (x52 << 0x4));
698         { u64 x78 = (x77 + (x52 << 0x1));
699         { u64 x79 = (x78 + x52);
700         { u64 x80 = (x41 + (x51 << 0x4));
701         { u64 x81 = (x80 + (x51 << 0x1));
702         { u64 x82 = (x81 + x51);
703         { u64 x83 = (x40 + (x50 << 0x4));
704         { u64 x84 = (x83 + (x50 << 0x1));
705         { u64 x85 = (x84 + x50);
706         { u64 x86 = (x85 >> 0x1a);
707         { u32 x87 = ((u32)x85 & 0x3ffffff);
708         { u64 x88 = (x86 + x82);
709         { u64 x89 = (x88 >> 0x19);
710         { u32 x90 = ((u32)x88 & 0x1ffffff);
711         { u64 x91 = (x89 + x79);
712         { u64 x92 = (x91 >> 0x1a);
713         { u32 x93 = ((u32)x91 & 0x3ffffff);
714         { u64 x94 = (x92 + x76);
715         { u64 x95 = (x94 >> 0x19);
716         { u32 x96 = ((u32)x94 & 0x1ffffff);
717         { u64 x97 = (x95 + x73);
718         { u64 x98 = (x97 >> 0x1a);
719         { u32 x99 = ((u32)x97 & 0x3ffffff);
720         { u64 x100 = (x98 + x70);
721         { u64 x101 = (x100 >> 0x19);
722         { u32 x102 = ((u32)x100 & 0x1ffffff);
723         { u64 x103 = (x101 + x67);
724         { u64 x104 = (x103 >> 0x1a);
725         { u32 x105 = ((u32)x103 & 0x3ffffff);
726         { u64 x106 = (x104 + x64);
727         { u64 x107 = (x106 >> 0x19);
728         { u32 x108 = ((u32)x106 & 0x1ffffff);
729         { u64 x109 = (x107 + x61);
730         { u64 x110 = (x109 >> 0x1a);
731         { u32 x111 = ((u32)x109 & 0x3ffffff);
732         { u64 x112 = (x110 + x49);
733         { u64 x113 = (x112 >> 0x19);
734         { u32 x114 = ((u32)x112 & 0x1ffffff);
735         { u64 x115 = (x87 + (0x13 * x113));
736         { u32 x116 = (u32) (x115 >> 0x1a);
737         { u32 x117 = ((u32)x115 & 0x3ffffff);
738         { u32 x118 = (x116 + x90);
739         { u32 x119 = (x118 >> 0x19);
740         { u32 x120 = (x118 & 0x1ffffff);
741         out[0] = x117;
742         out[1] = x120;
743         out[2] = (x119 + x93);
744         out[3] = x96;
745         out[4] = x99;
746         out[5] = x102;
747         out[6] = x105;
748         out[7] = x108;
749         out[8] = x111;
750         out[9] = x114;
751         }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
752 }
753
754 static __always_inline void fe_mul121666(fe *h, const fe_loose *f)
755 {
756         fe_mul_121666_impl(h->v, f->v);
757 }
758
759 void curve25519_generic(u8 out[CURVE25519_KEY_SIZE],
760                                const u8 scalar[CURVE25519_KEY_SIZE],
761                                const u8 point[CURVE25519_KEY_SIZE])
762 {
763         fe x1, x2, z2, x3, z3;
764         fe_loose x2l, z2l, x3l;
765         unsigned swap = 0;
766         int pos;
767         u8 e[32];
768
769         memcpy(e, scalar, 32);
770
771         /* The following implementation was transcribed to Coq and proven to
772          * correspond to unary scalar multiplication in affine coordinates given
773          * that x1 != 0 is the x coordinate of some point on the curve. It was
774          * also checked in Coq that doing a ladderstep with x1 = x3 = 0 gives
775          * z2' = z3' = 0, and z2 = z3 = 0 gives z2' = z3' = 0. The statement was
776          * quantified over the underlying field, so it applies to Curve25519
777          * itself and the quadratic twist of Curve25519. It was not proven in
778          * Coq that prime-field arithmetic correctly simulates extension-field
779          * arithmetic on prime-field values. The decoding of the byte array
780          * representation of e was not considered.
781          *
782          * Specification of Montgomery curves in affine coordinates:
783          * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Spec/MontgomeryCurve.v#L27>
784          *
785          * Proof that these form a group that is isomorphic to a Weierstrass
786          * curve:
787          * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/AffineProofs.v#L35>
788          *
789          * Coq transcription and correctness proof of the loop
790          * (where scalarbits=255):
791          * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZ.v#L118>
792          * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L278>
793          * preconditions: 0 <= e < 2^255 (not necessarily e < order),
794          * fe_invert(0) = 0
795          */
796         fe_frombytes(&x1, point);
797         fe_1(&x2);
798         fe_0(&z2);
799         fe_copy(&x3, &x1);
800         fe_1(&z3);
801
802         for (pos = 254; pos >= 0; --pos) {
803                 fe tmp0, tmp1;
804                 fe_loose tmp0l, tmp1l;
805                 /* loop invariant as of right before the test, for the case
806                  * where x1 != 0:
807                  *   pos >= -1; if z2 = 0 then x2 is nonzero; if z3 = 0 then x3
808                  *   is nonzero
809                  *   let r := e >> (pos+1) in the following equalities of
810                  *   projective points:
811                  *   to_xz (r*P)     === if swap then (x3, z3) else (x2, z2)
812                  *   to_xz ((r+1)*P) === if swap then (x2, z2) else (x3, z3)
813                  *   x1 is the nonzero x coordinate of the nonzero
814                  *   point (r*P-(r+1)*P)
815                  */
816                 unsigned b = 1 & (e[pos / 8] >> (pos & 7));
817                 swap ^= b;
818                 fe_cswap(&x2, &x3, swap);
819                 fe_cswap(&z2, &z3, swap);
820                 swap = b;
821                 /* Coq transcription of ladderstep formula (called from
822                  * transcribed loop):
823                  * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZ.v#L89>
824                  * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L131>
825                  * x1 != 0 <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L217>
826                  * x1  = 0 <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L147>
827                  */
828                 fe_sub(&tmp0l, &x3, &z3);
829                 fe_sub(&tmp1l, &x2, &z2);
830                 fe_add(&x2l, &x2, &z2);
831                 fe_add(&z2l, &x3, &z3);
832                 fe_mul_tll(&z3, &tmp0l, &x2l);
833                 fe_mul_tll(&z2, &z2l, &tmp1l);
834                 fe_sq_tl(&tmp0, &tmp1l);
835                 fe_sq_tl(&tmp1, &x2l);
836                 fe_add(&x3l, &z3, &z2);
837                 fe_sub(&z2l, &z3, &z2);
838                 fe_mul_ttt(&x2, &tmp1, &tmp0);
839                 fe_sub(&tmp1l, &tmp1, &tmp0);
840                 fe_sq_tl(&z2, &z2l);
841                 fe_mul121666(&z3, &tmp1l);
842                 fe_sq_tl(&x3, &x3l);
843                 fe_add(&tmp0l, &tmp0, &z3);
844                 fe_mul_ttt(&z3, &x1, &z2);
845                 fe_mul_tll(&z2, &tmp1l, &tmp0l);
846         }
847         /* here pos=-1, so r=e, so to_xz (e*P) === if swap then (x3, z3)
848          * else (x2, z2)
849          */
850         fe_cswap(&x2, &x3, swap);
851         fe_cswap(&z2, &z3, swap);
852
853         fe_invert(&z2, &z2);
854         fe_mul_ttt(&x2, &x2, &z2);
855         fe_tobytes(out, &x2);
856
857         memzero_explicit(&x1, sizeof(x1));
858         memzero_explicit(&x2, sizeof(x2));
859         memzero_explicit(&z2, sizeof(z2));
860         memzero_explicit(&x3, sizeof(x3));
861         memzero_explicit(&z3, sizeof(z3));
862         memzero_explicit(&x2l, sizeof(x2l));
863         memzero_explicit(&z2l, sizeof(z2l));
864         memzero_explicit(&x3l, sizeof(x3l));
865         memzero_explicit(&e, sizeof(e));
866 }