xref: /openbsd/sys/crypto/curve25519.c (revision 6a126883)
1*6a126883Stobhe /*	$OpenBSD: curve25519.c,v 1.2 2020/07/22 13:54:30 tobhe Exp $	*/
22df0987dSdlg /*
32df0987dSdlg  * Copyright (C) 2018-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
42df0987dSdlg  * Copyright (C) 2015-2016 The fiat-crypto Authors.
52df0987dSdlg  *
62df0987dSdlg  * Permission to use, copy, modify, and distribute this software for any
72df0987dSdlg  * purpose with or without fee is hereby granted, provided that the above
82df0987dSdlg  * copyright notice and this permission notice appear in all copies.
92df0987dSdlg  *
102df0987dSdlg  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
112df0987dSdlg  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
122df0987dSdlg  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
132df0987dSdlg  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
142df0987dSdlg  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
152df0987dSdlg  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
162df0987dSdlg  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
172df0987dSdlg  *
182df0987dSdlg  * This contains two implementation: a machine-generated formally verified
192df0987dSdlg  * implementation of Curve25519 ECDH from:
202df0987dSdlg  * <https://github.com/mit-plv/fiat-crypto>. Though originally machine
212df0987dSdlg  * generated, it has been tweaked to be suitable for use in the kernel.  It is
222df0987dSdlg  * optimized for 32-bit machines and machines that cannot work efficiently with
232df0987dSdlg  * 128-bit integer types.
242df0987dSdlg  */
252df0987dSdlg 
262df0987dSdlg #include <sys/types.h>
272df0987dSdlg #include <sys/systm.h>
282df0987dSdlg #include <crypto/curve25519.h>
292df0987dSdlg 
302df0987dSdlg #define __always_inline __inline __attribute__((__always_inline__))
312df0987dSdlg static const uint8_t null_point[CURVE25519_KEY_SIZE];
322df0987dSdlg static const uint8_t base_point[CURVE25519_KEY_SIZE] = { 9 };
332df0987dSdlg 
curve25519_generate_public(uint8_t pub[CURVE25519_KEY_SIZE],const uint8_t secret[CURVE25519_KEY_SIZE])342df0987dSdlg int curve25519_generate_public(uint8_t pub[CURVE25519_KEY_SIZE],
352df0987dSdlg 			       const uint8_t secret[CURVE25519_KEY_SIZE])
362df0987dSdlg {
372df0987dSdlg 	if (timingsafe_bcmp(secret, null_point, CURVE25519_KEY_SIZE) == 0)
382df0987dSdlg 		return 0;
392df0987dSdlg 	return curve25519(pub, secret, base_point);
402df0987dSdlg }
412df0987dSdlg 
get_unaligned_le32(const uint8_t * a)422df0987dSdlg static __always_inline uint32_t get_unaligned_le32(const uint8_t *a)
432df0987dSdlg {
442df0987dSdlg 	uint32_t l;
452df0987dSdlg 	__builtin_memcpy(&l, a, sizeof(l));
462df0987dSdlg 	return letoh32(l);
472df0987dSdlg }
482df0987dSdlg 
492df0987dSdlg /* fe means field element. Here the field is \Z/(2^255-19). An element t,
502df0987dSdlg  * entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77
512df0987dSdlg  * t[3]+2^102 t[4]+...+2^230 t[9].
522df0987dSdlg  * fe limbs are bounded by 1.125*2^26,1.125*2^25,1.125*2^26,1.125*2^25,etc.
532df0987dSdlg  * Multiplication and carrying produce fe from fe_loose.
542df0987dSdlg  */
552df0987dSdlg typedef struct fe { uint32_t v[10]; } fe;
562df0987dSdlg 
572df0987dSdlg /* fe_loose limbs are bounded by 3.375*2^26,3.375*2^25,3.375*2^26,3.375*2^25,etc
582df0987dSdlg  * Addition and subtraction produce fe_loose from (fe, fe).
592df0987dSdlg  */
602df0987dSdlg typedef struct fe_loose { uint32_t v[10]; } fe_loose;
612df0987dSdlg 
fe_frombytes_impl(uint32_t h[10],const uint8_t * s)622df0987dSdlg static __always_inline void fe_frombytes_impl(uint32_t h[10], const uint8_t *s)
632df0987dSdlg {
642df0987dSdlg 	/* Ignores top bit of s. */
652df0987dSdlg 	uint32_t a0 = get_unaligned_le32(s);
662df0987dSdlg 	uint32_t a1 = get_unaligned_le32(s+4);
672df0987dSdlg 	uint32_t a2 = get_unaligned_le32(s+8);
682df0987dSdlg 	uint32_t a3 = get_unaligned_le32(s+12);
692df0987dSdlg 	uint32_t a4 = get_unaligned_le32(s+16);
702df0987dSdlg 	uint32_t a5 = get_unaligned_le32(s+20);
712df0987dSdlg 	uint32_t a6 = get_unaligned_le32(s+24);
722df0987dSdlg 	uint32_t a7 = get_unaligned_le32(s+28);
732df0987dSdlg 	h[0] = a0&((1<<26)-1);                    /* 26 used, 32-26 left.   26 */
742df0987dSdlg 	h[1] = (a0>>26) | ((a1&((1<<19)-1))<< 6); /* (32-26) + 19 =  6+19 = 25 */
752df0987dSdlg 	h[2] = (a1>>19) | ((a2&((1<<13)-1))<<13); /* (32-19) + 13 = 13+13 = 26 */
762df0987dSdlg 	h[3] = (a2>>13) | ((a3&((1<< 6)-1))<<19); /* (32-13) +  6 = 19+ 6 = 25 */
772df0987dSdlg 	h[4] = (a3>> 6);                          /* (32- 6)              = 26 */
782df0987dSdlg 	h[5] = a4&((1<<25)-1);                    /*                        25 */
792df0987dSdlg 	h[6] = (a4>>25) | ((a5&((1<<19)-1))<< 7); /* (32-25) + 19 =  7+19 = 26 */
802df0987dSdlg 	h[7] = (a5>>19) | ((a6&((1<<12)-1))<<13); /* (32-19) + 12 = 13+12 = 25 */
812df0987dSdlg 	h[8] = (a6>>12) | ((a7&((1<< 6)-1))<<20); /* (32-12) +  6 = 20+ 6 = 26 */
822df0987dSdlg 	h[9] = (a7>> 6)&((1<<25)-1); /*                                     25 */
832df0987dSdlg }
842df0987dSdlg 
fe_frombytes(fe * h,const uint8_t * s)852df0987dSdlg static __always_inline void fe_frombytes(fe *h, const uint8_t *s)
862df0987dSdlg {
872df0987dSdlg 	fe_frombytes_impl(h->v, s);
882df0987dSdlg }
892df0987dSdlg 
902df0987dSdlg static __always_inline uint8_t /*bool*/
addcarryx_u25(uint8_t c,uint32_t a,uint32_t b,uint32_t * low)912df0987dSdlg addcarryx_u25(uint8_t /*bool*/ c, uint32_t a, uint32_t b, uint32_t *low)
922df0987dSdlg {
932df0987dSdlg 	/* This function extracts 25 bits of result and 1 bit of carry
942df0987dSdlg 	 * (26 total), so a 32-bit intermediate is sufficient.
952df0987dSdlg 	 */
962df0987dSdlg 	uint32_t x = a + b + c;
972df0987dSdlg 	*low = x & ((1 << 25) - 1);
982df0987dSdlg 	return (x >> 25) & 1;
992df0987dSdlg }
1002df0987dSdlg 
1012df0987dSdlg static __always_inline uint8_t /*bool*/
addcarryx_u26(uint8_t c,uint32_t a,uint32_t b,uint32_t * low)1022df0987dSdlg addcarryx_u26(uint8_t /*bool*/ c, uint32_t a, uint32_t b, uint32_t *low)
1032df0987dSdlg {
1042df0987dSdlg 	/* This function extracts 26 bits of result and 1 bit of carry
1052df0987dSdlg 	 * (27 total), so a 32-bit intermediate is sufficient.
1062df0987dSdlg 	 */
1072df0987dSdlg 	uint32_t x = a + b + c;
1082df0987dSdlg 	*low = x & ((1 << 26) - 1);
1092df0987dSdlg 	return (x >> 26) & 1;
1102df0987dSdlg }
1112df0987dSdlg 
1122df0987dSdlg static __always_inline uint8_t /*bool*/
subborrow_u25(uint8_t c,uint32_t a,uint32_t b,uint32_t * low)1132df0987dSdlg subborrow_u25(uint8_t /*bool*/ c, uint32_t a, uint32_t b, uint32_t *low)
1142df0987dSdlg {
1152df0987dSdlg 	/* This function extracts 25 bits of result and 1 bit of borrow
1162df0987dSdlg 	 * (26 total), so a 32-bit intermediate is sufficient.
1172df0987dSdlg 	 */
1182df0987dSdlg 	uint32_t x = a - b - c;
1192df0987dSdlg 	*low = x & ((1 << 25) - 1);
1202df0987dSdlg 	return x >> 31;
1212df0987dSdlg }
1222df0987dSdlg 
1232df0987dSdlg static __always_inline uint8_t /*bool*/
subborrow_u26(uint8_t c,uint32_t a,uint32_t b,uint32_t * low)1242df0987dSdlg subborrow_u26(uint8_t /*bool*/ c, uint32_t a, uint32_t b, uint32_t *low)
1252df0987dSdlg {
1262df0987dSdlg 	/* This function extracts 26 bits of result and 1 bit of borrow
1272df0987dSdlg 	 *(27 total), so a 32-bit intermediate is sufficient.
1282df0987dSdlg 	 */
1292df0987dSdlg 	uint32_t x = a - b - c;
1302df0987dSdlg 	*low = x & ((1 << 26) - 1);
1312df0987dSdlg 	return x >> 31;
1322df0987dSdlg }
1332df0987dSdlg 
cmovznz32(uint32_t t,uint32_t z,uint32_t nz)1342df0987dSdlg static __always_inline uint32_t cmovznz32(uint32_t t, uint32_t z, uint32_t nz)
1352df0987dSdlg {
1362df0987dSdlg 	t = -!!t; /* all set if nonzero, 0 if 0 */
1372df0987dSdlg 	return (t&nz) | ((~t)&z);
1382df0987dSdlg }
1392df0987dSdlg 
fe_freeze(uint32_t out[10],const uint32_t in1[10])1402df0987dSdlg static __always_inline void fe_freeze(uint32_t out[10], const uint32_t in1[10])
1412df0987dSdlg {
1422df0987dSdlg 	const uint32_t x17 = in1[9];
1432df0987dSdlg 	const uint32_t x18 = in1[8];
1442df0987dSdlg 	const uint32_t x16 = in1[7];
1452df0987dSdlg 	const uint32_t x14 = in1[6];
1462df0987dSdlg 	const uint32_t x12 = in1[5];
1472df0987dSdlg 	const uint32_t x10 = in1[4];
1482df0987dSdlg 	const uint32_t x8 = in1[3];
1492df0987dSdlg 	const uint32_t x6 = in1[2];
1502df0987dSdlg 	const uint32_t x4 = in1[1];
1512df0987dSdlg 	const uint32_t x2 = in1[0];
1522df0987dSdlg 	uint32_t x20; uint8_t/*bool*/ x21 = subborrow_u26(0x0, x2, 0x3ffffed, &x20);
1532df0987dSdlg 	uint32_t x23; uint8_t/*bool*/ x24 = subborrow_u25(x21, x4, 0x1ffffff, &x23);
1542df0987dSdlg 	uint32_t x26; uint8_t/*bool*/ x27 = subborrow_u26(x24, x6, 0x3ffffff, &x26);
1552df0987dSdlg 	uint32_t x29; uint8_t/*bool*/ x30 = subborrow_u25(x27, x8, 0x1ffffff, &x29);
1562df0987dSdlg 	uint32_t x32; uint8_t/*bool*/ x33 = subborrow_u26(x30, x10, 0x3ffffff, &x32);
1572df0987dSdlg 	uint32_t x35; uint8_t/*bool*/ x36 = subborrow_u25(x33, x12, 0x1ffffff, &x35);
1582df0987dSdlg 	uint32_t x38; uint8_t/*bool*/ x39 = subborrow_u26(x36, x14, 0x3ffffff, &x38);
1592df0987dSdlg 	uint32_t x41; uint8_t/*bool*/ x42 = subborrow_u25(x39, x16, 0x1ffffff, &x41);
1602df0987dSdlg 	uint32_t x44; uint8_t/*bool*/ x45 = subborrow_u26(x42, x18, 0x3ffffff, &x44);
1612df0987dSdlg 	uint32_t x47; uint8_t/*bool*/ x48 = subborrow_u25(x45, x17, 0x1ffffff, &x47);
1622df0987dSdlg 	uint32_t x49 = cmovznz32(x48, 0x0, 0xffffffff);
1632df0987dSdlg 	uint32_t x50 = (x49 & 0x3ffffed);
1642df0987dSdlg 	uint32_t x52; uint8_t/*bool*/ x53 = addcarryx_u26(0x0, x20, x50, &x52);
1652df0987dSdlg 	uint32_t x54 = (x49 & 0x1ffffff);
1662df0987dSdlg 	uint32_t x56; uint8_t/*bool*/ x57 = addcarryx_u25(x53, x23, x54, &x56);
1672df0987dSdlg 	uint32_t x58 = (x49 & 0x3ffffff);
1682df0987dSdlg 	uint32_t x60; uint8_t/*bool*/ x61 = addcarryx_u26(x57, x26, x58, &x60);
1692df0987dSdlg 	uint32_t x62 = (x49 & 0x1ffffff);
1702df0987dSdlg 	uint32_t x64; uint8_t/*bool*/ x65 = addcarryx_u25(x61, x29, x62, &x64);
1712df0987dSdlg 	uint32_t x66 = (x49 & 0x3ffffff);
1722df0987dSdlg 	uint32_t x68; uint8_t/*bool*/ x69 = addcarryx_u26(x65, x32, x66, &x68);
1732df0987dSdlg 	uint32_t x70 = (x49 & 0x1ffffff);
1742df0987dSdlg 	uint32_t x72; uint8_t/*bool*/ x73 = addcarryx_u25(x69, x35, x70, &x72);
1752df0987dSdlg 	uint32_t x74 = (x49 & 0x3ffffff);
1762df0987dSdlg 	uint32_t x76; uint8_t/*bool*/ x77 = addcarryx_u26(x73, x38, x74, &x76);
1772df0987dSdlg 	uint32_t x78 = (x49 & 0x1ffffff);
1782df0987dSdlg 	uint32_t x80; uint8_t/*bool*/ x81 = addcarryx_u25(x77, x41, x78, &x80);
1792df0987dSdlg 	uint32_t x82 = (x49 & 0x3ffffff);
1802df0987dSdlg 	uint32_t x84; uint8_t/*bool*/ x85 = addcarryx_u26(x81, x44, x82, &x84);
1812df0987dSdlg 	uint32_t x86 = (x49 & 0x1ffffff);
1822df0987dSdlg 	uint32_t x88; addcarryx_u25(x85, x47, x86, &x88);
1832df0987dSdlg 	out[0] = x52;
1842df0987dSdlg 	out[1] = x56;
1852df0987dSdlg 	out[2] = x60;
1862df0987dSdlg 	out[3] = x64;
1872df0987dSdlg 	out[4] = x68;
1882df0987dSdlg 	out[5] = x72;
1892df0987dSdlg 	out[6] = x76;
1902df0987dSdlg 	out[7] = x80;
1912df0987dSdlg 	out[8] = x84;
1922df0987dSdlg 	out[9] = x88;
1932df0987dSdlg }
1942df0987dSdlg 
fe_tobytes(uint8_t s[32],const fe * f)1952df0987dSdlg static __always_inline void fe_tobytes(uint8_t s[32], const fe *f)
1962df0987dSdlg {
1972df0987dSdlg 	uint32_t h[10];
1982df0987dSdlg 	fe_freeze(h, f->v);
1992df0987dSdlg 	s[0] = h[0] >> 0;
2002df0987dSdlg 	s[1] = h[0] >> 8;
2012df0987dSdlg 	s[2] = h[0] >> 16;
2022df0987dSdlg 	s[3] = (h[0] >> 24) | (h[1] << 2);
2032df0987dSdlg 	s[4] = h[1] >> 6;
2042df0987dSdlg 	s[5] = h[1] >> 14;
2052df0987dSdlg 	s[6] = (h[1] >> 22) | (h[2] << 3);
2062df0987dSdlg 	s[7] = h[2] >> 5;
2072df0987dSdlg 	s[8] = h[2] >> 13;
2082df0987dSdlg 	s[9] = (h[2] >> 21) | (h[3] << 5);
2092df0987dSdlg 	s[10] = h[3] >> 3;
2102df0987dSdlg 	s[11] = h[3] >> 11;
2112df0987dSdlg 	s[12] = (h[3] >> 19) | (h[4] << 6);
2122df0987dSdlg 	s[13] = h[4] >> 2;
2132df0987dSdlg 	s[14] = h[4] >> 10;
2142df0987dSdlg 	s[15] = h[4] >> 18;
2152df0987dSdlg 	s[16] = h[5] >> 0;
2162df0987dSdlg 	s[17] = h[5] >> 8;
2172df0987dSdlg 	s[18] = h[5] >> 16;
2182df0987dSdlg 	s[19] = (h[5] >> 24) | (h[6] << 1);
2192df0987dSdlg 	s[20] = h[6] >> 7;
2202df0987dSdlg 	s[21] = h[6] >> 15;
2212df0987dSdlg 	s[22] = (h[6] >> 23) | (h[7] << 3);
2222df0987dSdlg 	s[23] = h[7] >> 5;
2232df0987dSdlg 	s[24] = h[7] >> 13;
2242df0987dSdlg 	s[25] = (h[7] >> 21) | (h[8] << 4);
2252df0987dSdlg 	s[26] = h[8] >> 4;
2262df0987dSdlg 	s[27] = h[8] >> 12;
2272df0987dSdlg 	s[28] = (h[8] >> 20) | (h[9] << 6);
2282df0987dSdlg 	s[29] = h[9] >> 2;
2292df0987dSdlg 	s[30] = h[9] >> 10;
2302df0987dSdlg 	s[31] = h[9] >> 18;
2312df0987dSdlg }
2322df0987dSdlg 
2332df0987dSdlg /* h = f */
fe_copy(fe * h,const fe * f)2342df0987dSdlg static __always_inline void fe_copy(fe *h, const fe *f)
2352df0987dSdlg {
2362df0987dSdlg 	memmove(h, f, sizeof(uint32_t) * 10);
2372df0987dSdlg }
2382df0987dSdlg 
fe_copy_lt(fe_loose * h,const fe * f)2392df0987dSdlg static __always_inline void fe_copy_lt(fe_loose *h, const fe *f)
2402df0987dSdlg {
2412df0987dSdlg 	memmove(h, f, sizeof(uint32_t) * 10);
2422df0987dSdlg }
2432df0987dSdlg 
2442df0987dSdlg /* h = 0 */
fe_0(fe * h)2452df0987dSdlg static __always_inline void fe_0(fe *h)
2462df0987dSdlg {
2472df0987dSdlg 	memset(h, 0, sizeof(uint32_t) * 10);
2482df0987dSdlg }
2492df0987dSdlg 
2502df0987dSdlg /* h = 1 */
fe_1(fe * h)2512df0987dSdlg static __always_inline void fe_1(fe *h)
2522df0987dSdlg {
2532df0987dSdlg 	memset(h, 0, sizeof(uint32_t) * 10);
2542df0987dSdlg 	h->v[0] = 1;
2552df0987dSdlg }
2562df0987dSdlg 
fe_add_impl(uint32_t out[10],const uint32_t in1[10],const uint32_t in2[10])2572df0987dSdlg static void fe_add_impl(uint32_t out[10], const uint32_t in1[10], const uint32_t in2[10])
2582df0987dSdlg {
2592df0987dSdlg 	const uint32_t x20 = in1[9];
2602df0987dSdlg 	const uint32_t x21 = in1[8];
2612df0987dSdlg 	const uint32_t x19 = in1[7];
2622df0987dSdlg 	const uint32_t x17 = in1[6];
2632df0987dSdlg 	const uint32_t x15 = in1[5];
2642df0987dSdlg 	const uint32_t x13 = in1[4];
2652df0987dSdlg 	const uint32_t x11 = in1[3];
2662df0987dSdlg 	const uint32_t x9 = in1[2];
2672df0987dSdlg 	const uint32_t x7 = in1[1];
2682df0987dSdlg 	const uint32_t x5 = in1[0];
2692df0987dSdlg 	const uint32_t x38 = in2[9];
2702df0987dSdlg 	const uint32_t x39 = in2[8];
2712df0987dSdlg 	const uint32_t x37 = in2[7];
2722df0987dSdlg 	const uint32_t x35 = in2[6];
2732df0987dSdlg 	const uint32_t x33 = in2[5];
2742df0987dSdlg 	const uint32_t x31 = in2[4];
2752df0987dSdlg 	const uint32_t x29 = in2[3];
2762df0987dSdlg 	const uint32_t x27 = in2[2];
2772df0987dSdlg 	const uint32_t x25 = in2[1];
2782df0987dSdlg 	const uint32_t x23 = in2[0];
2792df0987dSdlg 	out[0] = (x5 + x23);
2802df0987dSdlg 	out[1] = (x7 + x25);
2812df0987dSdlg 	out[2] = (x9 + x27);
2822df0987dSdlg 	out[3] = (x11 + x29);
2832df0987dSdlg 	out[4] = (x13 + x31);
2842df0987dSdlg 	out[5] = (x15 + x33);
2852df0987dSdlg 	out[6] = (x17 + x35);
2862df0987dSdlg 	out[7] = (x19 + x37);
2872df0987dSdlg 	out[8] = (x21 + x39);
2882df0987dSdlg 	out[9] = (x20 + x38);
2892df0987dSdlg }
2902df0987dSdlg 
2912df0987dSdlg /* h = f + g
2922df0987dSdlg  * Can overlap h with f or g.
2932df0987dSdlg  */
fe_add(fe_loose * h,const fe * f,const fe * g)2942df0987dSdlg static __always_inline void fe_add(fe_loose *h, const fe *f, const fe *g)
2952df0987dSdlg {
2962df0987dSdlg 	fe_add_impl(h->v, f->v, g->v);
2972df0987dSdlg }
2982df0987dSdlg 
fe_sub_impl(uint32_t out[10],const uint32_t in1[10],const uint32_t in2[10])2992df0987dSdlg static void fe_sub_impl(uint32_t out[10], const uint32_t in1[10], const uint32_t in2[10])
3002df0987dSdlg {
3012df0987dSdlg 	const uint32_t x20 = in1[9];
3022df0987dSdlg 	const uint32_t x21 = in1[8];
3032df0987dSdlg 	const uint32_t x19 = in1[7];
3042df0987dSdlg 	const uint32_t x17 = in1[6];
3052df0987dSdlg 	const uint32_t x15 = in1[5];
3062df0987dSdlg 	const uint32_t x13 = in1[4];
3072df0987dSdlg 	const uint32_t x11 = in1[3];
3082df0987dSdlg 	const uint32_t x9 = in1[2];
3092df0987dSdlg 	const uint32_t x7 = in1[1];
3102df0987dSdlg 	const uint32_t x5 = in1[0];
3112df0987dSdlg 	const uint32_t x38 = in2[9];
3122df0987dSdlg 	const uint32_t x39 = in2[8];
3132df0987dSdlg 	const uint32_t x37 = in2[7];
3142df0987dSdlg 	const uint32_t x35 = in2[6];
3152df0987dSdlg 	const uint32_t x33 = in2[5];
3162df0987dSdlg 	const uint32_t x31 = in2[4];
3172df0987dSdlg 	const uint32_t x29 = in2[3];
3182df0987dSdlg 	const uint32_t x27 = in2[2];
3192df0987dSdlg 	const uint32_t x25 = in2[1];
3202df0987dSdlg 	const uint32_t x23 = in2[0];
3212df0987dSdlg 	out[0] = ((0x7ffffda + x5) - x23);
3222df0987dSdlg 	out[1] = ((0x3fffffe + x7) - x25);
3232df0987dSdlg 	out[2] = ((0x7fffffe + x9) - x27);
3242df0987dSdlg 	out[3] = ((0x3fffffe + x11) - x29);
3252df0987dSdlg 	out[4] = ((0x7fffffe + x13) - x31);
3262df0987dSdlg 	out[5] = ((0x3fffffe + x15) - x33);
3272df0987dSdlg 	out[6] = ((0x7fffffe + x17) - x35);
3282df0987dSdlg 	out[7] = ((0x3fffffe + x19) - x37);
3292df0987dSdlg 	out[8] = ((0x7fffffe + x21) - x39);
3302df0987dSdlg 	out[9] = ((0x3fffffe + x20) - x38);
3312df0987dSdlg }
3322df0987dSdlg 
3332df0987dSdlg /* h = f - g
3342df0987dSdlg  * Can overlap h with f or g.
3352df0987dSdlg  */
fe_sub(fe_loose * h,const fe * f,const fe * g)3362df0987dSdlg static __always_inline void fe_sub(fe_loose *h, const fe *f, const fe *g)
3372df0987dSdlg {
3382df0987dSdlg 	fe_sub_impl(h->v, f->v, g->v);
3392df0987dSdlg }
3402df0987dSdlg 
fe_mul_impl(uint32_t out[10],const uint32_t in1[10],const uint32_t in2[10])3412df0987dSdlg static void fe_mul_impl(uint32_t out[10], const uint32_t in1[10], const uint32_t in2[10])
3422df0987dSdlg {
3432df0987dSdlg 	const uint32_t x20 = in1[9];
3442df0987dSdlg 	const uint32_t x21 = in1[8];
3452df0987dSdlg 	const uint32_t x19 = in1[7];
3462df0987dSdlg 	const uint32_t x17 = in1[6];
3472df0987dSdlg 	const uint32_t x15 = in1[5];
3482df0987dSdlg 	const uint32_t x13 = in1[4];
3492df0987dSdlg 	const uint32_t x11 = in1[3];
3502df0987dSdlg 	const uint32_t x9 = in1[2];
3512df0987dSdlg 	const uint32_t x7 = in1[1];
3522df0987dSdlg 	const uint32_t x5 = in1[0];
3532df0987dSdlg 	const uint32_t x38 = in2[9];
3542df0987dSdlg 	const uint32_t x39 = in2[8];
3552df0987dSdlg 	const uint32_t x37 = in2[7];
3562df0987dSdlg 	const uint32_t x35 = in2[6];
3572df0987dSdlg 	const uint32_t x33 = in2[5];
3582df0987dSdlg 	const uint32_t x31 = in2[4];
3592df0987dSdlg 	const uint32_t x29 = in2[3];
3602df0987dSdlg 	const uint32_t x27 = in2[2];
3612df0987dSdlg 	const uint32_t x25 = in2[1];
3622df0987dSdlg 	const uint32_t x23 = in2[0];
3632df0987dSdlg 	uint64_t x40 = ((uint64_t)x23 * x5);
3642df0987dSdlg 	uint64_t x41 = (((uint64_t)x23 * x7) + ((uint64_t)x25 * x5));
3652df0987dSdlg 	uint64_t x42 = ((((uint64_t)(0x2 * x25) * x7) + ((uint64_t)x23 * x9)) + ((uint64_t)x27 * x5));
3662df0987dSdlg 	uint64_t x43 = (((((uint64_t)x25 * x9) + ((uint64_t)x27 * x7)) + ((uint64_t)x23 * x11)) + ((uint64_t)x29 * x5));
3672df0987dSdlg 	uint64_t x44 = (((((uint64_t)x27 * x9) + (0x2 * (((uint64_t)x25 * x11) + ((uint64_t)x29 * x7)))) + ((uint64_t)x23 * x13)) + ((uint64_t)x31 * x5));
3682df0987dSdlg 	uint64_t x45 = (((((((uint64_t)x27 * x11) + ((uint64_t)x29 * x9)) + ((uint64_t)x25 * x13)) + ((uint64_t)x31 * x7)) + ((uint64_t)x23 * x15)) + ((uint64_t)x33 * x5));
3692df0987dSdlg 	uint64_t x46 = (((((0x2 * ((((uint64_t)x29 * x11) + ((uint64_t)x25 * x15)) + ((uint64_t)x33 * x7))) + ((uint64_t)x27 * x13)) + ((uint64_t)x31 * x9)) + ((uint64_t)x23 * x17)) + ((uint64_t)x35 * x5));
3702df0987dSdlg 	uint64_t x47 = (((((((((uint64_t)x29 * x13) + ((uint64_t)x31 * x11)) + ((uint64_t)x27 * x15)) + ((uint64_t)x33 * x9)) + ((uint64_t)x25 * x17)) + ((uint64_t)x35 * x7)) + ((uint64_t)x23 * x19)) + ((uint64_t)x37 * x5));
3712df0987dSdlg 	uint64_t x48 = (((((((uint64_t)x31 * x13) + (0x2 * (((((uint64_t)x29 * x15) + ((uint64_t)x33 * x11)) + ((uint64_t)x25 * x19)) + ((uint64_t)x37 * x7)))) + ((uint64_t)x27 * x17)) + ((uint64_t)x35 * x9)) + ((uint64_t)x23 * x21)) + ((uint64_t)x39 * x5));
3722df0987dSdlg 	uint64_t x49 = (((((((((((uint64_t)x31 * x15) + ((uint64_t)x33 * x13)) + ((uint64_t)x29 * x17)) + ((uint64_t)x35 * x11)) + ((uint64_t)x27 * x19)) + ((uint64_t)x37 * x9)) + ((uint64_t)x25 * x21)) + ((uint64_t)x39 * x7)) + ((uint64_t)x23 * x20)) + ((uint64_t)x38 * x5));
3732df0987dSdlg 	uint64_t x50 = (((((0x2 * ((((((uint64_t)x33 * x15) + ((uint64_t)x29 * x19)) + ((uint64_t)x37 * x11)) + ((uint64_t)x25 * x20)) + ((uint64_t)x38 * x7))) + ((uint64_t)x31 * x17)) + ((uint64_t)x35 * x13)) + ((uint64_t)x27 * x21)) + ((uint64_t)x39 * x9));
3742df0987dSdlg 	uint64_t x51 = (((((((((uint64_t)x33 * x17) + ((uint64_t)x35 * x15)) + ((uint64_t)x31 * x19)) + ((uint64_t)x37 * x13)) + ((uint64_t)x29 * x21)) + ((uint64_t)x39 * x11)) + ((uint64_t)x27 * x20)) + ((uint64_t)x38 * x9));
3752df0987dSdlg 	uint64_t x52 = (((((uint64_t)x35 * x17) + (0x2 * (((((uint64_t)x33 * x19) + ((uint64_t)x37 * x15)) + ((uint64_t)x29 * x20)) + ((uint64_t)x38 * x11)))) + ((uint64_t)x31 * x21)) + ((uint64_t)x39 * x13));
3762df0987dSdlg 	uint64_t x53 = (((((((uint64_t)x35 * x19) + ((uint64_t)x37 * x17)) + ((uint64_t)x33 * x21)) + ((uint64_t)x39 * x15)) + ((uint64_t)x31 * x20)) + ((uint64_t)x38 * x13));
3772df0987dSdlg 	uint64_t x54 = (((0x2 * ((((uint64_t)x37 * x19) + ((uint64_t)x33 * x20)) + ((uint64_t)x38 * x15))) + ((uint64_t)x35 * x21)) + ((uint64_t)x39 * x17));
3782df0987dSdlg 	uint64_t x55 = (((((uint64_t)x37 * x21) + ((uint64_t)x39 * x19)) + ((uint64_t)x35 * x20)) + ((uint64_t)x38 * x17));
3792df0987dSdlg 	uint64_t x56 = (((uint64_t)x39 * x21) + (0x2 * (((uint64_t)x37 * x20) + ((uint64_t)x38 * x19))));
3802df0987dSdlg 	uint64_t x57 = (((uint64_t)x39 * x20) + ((uint64_t)x38 * x21));
3812df0987dSdlg 	uint64_t x58 = ((uint64_t)(0x2 * x38) * x20);
3822df0987dSdlg 	uint64_t x59 = (x48 + (x58 << 0x4));
3832df0987dSdlg 	uint64_t x60 = (x59 + (x58 << 0x1));
3842df0987dSdlg 	uint64_t x61 = (x60 + x58);
3852df0987dSdlg 	uint64_t x62 = (x47 + (x57 << 0x4));
3862df0987dSdlg 	uint64_t x63 = (x62 + (x57 << 0x1));
3872df0987dSdlg 	uint64_t x64 = (x63 + x57);
3882df0987dSdlg 	uint64_t x65 = (x46 + (x56 << 0x4));
3892df0987dSdlg 	uint64_t x66 = (x65 + (x56 << 0x1));
3902df0987dSdlg 	uint64_t x67 = (x66 + x56);
3912df0987dSdlg 	uint64_t x68 = (x45 + (x55 << 0x4));
3922df0987dSdlg 	uint64_t x69 = (x68 + (x55 << 0x1));
3932df0987dSdlg 	uint64_t x70 = (x69 + x55);
3942df0987dSdlg 	uint64_t x71 = (x44 + (x54 << 0x4));
3952df0987dSdlg 	uint64_t x72 = (x71 + (x54 << 0x1));
3962df0987dSdlg 	uint64_t x73 = (x72 + x54);
3972df0987dSdlg 	uint64_t x74 = (x43 + (x53 << 0x4));
3982df0987dSdlg 	uint64_t x75 = (x74 + (x53 << 0x1));
3992df0987dSdlg 	uint64_t x76 = (x75 + x53);
4002df0987dSdlg 	uint64_t x77 = (x42 + (x52 << 0x4));
4012df0987dSdlg 	uint64_t x78 = (x77 + (x52 << 0x1));
4022df0987dSdlg 	uint64_t x79 = (x78 + x52);
4032df0987dSdlg 	uint64_t x80 = (x41 + (x51 << 0x4));
4042df0987dSdlg 	uint64_t x81 = (x80 + (x51 << 0x1));
4052df0987dSdlg 	uint64_t x82 = (x81 + x51);
4062df0987dSdlg 	uint64_t x83 = (x40 + (x50 << 0x4));
4072df0987dSdlg 	uint64_t x84 = (x83 + (x50 << 0x1));
4082df0987dSdlg 	uint64_t x85 = (x84 + x50);
4092df0987dSdlg 	uint64_t x86 = (x85 >> 0x1a);
4102df0987dSdlg 	uint32_t x87 = ((uint32_t)x85 & 0x3ffffff);
4112df0987dSdlg 	uint64_t x88 = (x86 + x82);
4122df0987dSdlg 	uint64_t x89 = (x88 >> 0x19);
4132df0987dSdlg 	uint32_t x90 = ((uint32_t)x88 & 0x1ffffff);
4142df0987dSdlg 	uint64_t x91 = (x89 + x79);
4152df0987dSdlg 	uint64_t x92 = (x91 >> 0x1a);
4162df0987dSdlg 	uint32_t x93 = ((uint32_t)x91 & 0x3ffffff);
4172df0987dSdlg 	uint64_t x94 = (x92 + x76);
4182df0987dSdlg 	uint64_t x95 = (x94 >> 0x19);
4192df0987dSdlg 	uint32_t x96 = ((uint32_t)x94 & 0x1ffffff);
4202df0987dSdlg 	uint64_t x97 = (x95 + x73);
4212df0987dSdlg 	uint64_t x98 = (x97 >> 0x1a);
4222df0987dSdlg 	uint32_t x99 = ((uint32_t)x97 & 0x3ffffff);
4232df0987dSdlg 	uint64_t x100 = (x98 + x70);
4242df0987dSdlg 	uint64_t x101 = (x100 >> 0x19);
4252df0987dSdlg 	uint32_t x102 = ((uint32_t)x100 & 0x1ffffff);
4262df0987dSdlg 	uint64_t x103 = (x101 + x67);
4272df0987dSdlg 	uint64_t x104 = (x103 >> 0x1a);
4282df0987dSdlg 	uint32_t x105 = ((uint32_t)x103 & 0x3ffffff);
4292df0987dSdlg 	uint64_t x106 = (x104 + x64);
4302df0987dSdlg 	uint64_t x107 = (x106 >> 0x19);
4312df0987dSdlg 	uint32_t x108 = ((uint32_t)x106 & 0x1ffffff);
4322df0987dSdlg 	uint64_t x109 = (x107 + x61);
4332df0987dSdlg 	uint64_t x110 = (x109 >> 0x1a);
4342df0987dSdlg 	uint32_t x111 = ((uint32_t)x109 & 0x3ffffff);
4352df0987dSdlg 	uint64_t x112 = (x110 + x49);
4362df0987dSdlg 	uint64_t x113 = (x112 >> 0x19);
4372df0987dSdlg 	uint32_t x114 = ((uint32_t)x112 & 0x1ffffff);
4382df0987dSdlg 	uint64_t x115 = (x87 + (0x13 * x113));
4392df0987dSdlg 	uint32_t x116 = (uint32_t) (x115 >> 0x1a);
4402df0987dSdlg 	uint32_t x117 = ((uint32_t)x115 & 0x3ffffff);
4412df0987dSdlg 	uint32_t x118 = (x116 + x90);
4422df0987dSdlg 	uint32_t x119 = (x118 >> 0x19);
4432df0987dSdlg 	uint32_t x120 = (x118 & 0x1ffffff);
4442df0987dSdlg 	out[0] = x117;
4452df0987dSdlg 	out[1] = x120;
4462df0987dSdlg 	out[2] = (x119 + x93);
4472df0987dSdlg 	out[3] = x96;
4482df0987dSdlg 	out[4] = x99;
4492df0987dSdlg 	out[5] = x102;
4502df0987dSdlg 	out[6] = x105;
4512df0987dSdlg 	out[7] = x108;
4522df0987dSdlg 	out[8] = x111;
4532df0987dSdlg 	out[9] = x114;
4542df0987dSdlg }
4552df0987dSdlg 
fe_mul_ttt(fe * h,const fe * f,const fe * g)4562df0987dSdlg static __always_inline void fe_mul_ttt(fe *h, const fe *f, const fe *g)
4572df0987dSdlg {
4582df0987dSdlg 	fe_mul_impl(h->v, f->v, g->v);
4592df0987dSdlg }
4602df0987dSdlg 
fe_mul_tlt(fe * h,const fe_loose * f,const fe * g)4612df0987dSdlg static __always_inline void fe_mul_tlt(fe *h, const fe_loose *f, const fe *g)
4622df0987dSdlg {
4632df0987dSdlg 	fe_mul_impl(h->v, f->v, g->v);
4642df0987dSdlg }
4652df0987dSdlg 
4662df0987dSdlg static __always_inline void
fe_mul_tll(fe * h,const fe_loose * f,const fe_loose * g)4672df0987dSdlg fe_mul_tll(fe *h, const fe_loose *f, const fe_loose *g)
4682df0987dSdlg {
4692df0987dSdlg 	fe_mul_impl(h->v, f->v, g->v);
4702df0987dSdlg }
4712df0987dSdlg 
fe_sqr_impl(uint32_t out[10],const uint32_t in1[10])4722df0987dSdlg static void fe_sqr_impl(uint32_t out[10], const uint32_t in1[10])
4732df0987dSdlg {
4742df0987dSdlg 	const uint32_t x17 = in1[9];
4752df0987dSdlg 	const uint32_t x18 = in1[8];
4762df0987dSdlg 	const uint32_t x16 = in1[7];
4772df0987dSdlg 	const uint32_t x14 = in1[6];
4782df0987dSdlg 	const uint32_t x12 = in1[5];
4792df0987dSdlg 	const uint32_t x10 = in1[4];
4802df0987dSdlg 	const uint32_t x8 = in1[3];
4812df0987dSdlg 	const uint32_t x6 = in1[2];
4822df0987dSdlg 	const uint32_t x4 = in1[1];
4832df0987dSdlg 	const uint32_t x2 = in1[0];
4842df0987dSdlg 	uint64_t x19 = ((uint64_t)x2 * x2);
4852df0987dSdlg 	uint64_t x20 = ((uint64_t)(0x2 * x2) * x4);
4862df0987dSdlg 	uint64_t x21 = (0x2 * (((uint64_t)x4 * x4) + ((uint64_t)x2 * x6)));
4872df0987dSdlg 	uint64_t x22 = (0x2 * (((uint64_t)x4 * x6) + ((uint64_t)x2 * x8)));
4882df0987dSdlg 	uint64_t x23 = ((((uint64_t)x6 * x6) + ((uint64_t)(0x4 * x4) * x8)) + ((uint64_t)(0x2 * x2) * x10));
4892df0987dSdlg 	uint64_t x24 = (0x2 * ((((uint64_t)x6 * x8) + ((uint64_t)x4 * x10)) + ((uint64_t)x2 * x12)));
4902df0987dSdlg 	uint64_t x25 = (0x2 * (((((uint64_t)x8 * x8) + ((uint64_t)x6 * x10)) + ((uint64_t)x2 * x14)) + ((uint64_t)(0x2 * x4) * x12)));
4912df0987dSdlg 	uint64_t x26 = (0x2 * (((((uint64_t)x8 * x10) + ((uint64_t)x6 * x12)) + ((uint64_t)x4 * x14)) + ((uint64_t)x2 * x16)));
4922df0987dSdlg 	uint64_t x27 = (((uint64_t)x10 * x10) + (0x2 * ((((uint64_t)x6 * x14) + ((uint64_t)x2 * x18)) + (0x2 * (((uint64_t)x4 * x16) + ((uint64_t)x8 * x12))))));
4932df0987dSdlg 	uint64_t x28 = (0x2 * ((((((uint64_t)x10 * x12) + ((uint64_t)x8 * x14)) + ((uint64_t)x6 * x16)) + ((uint64_t)x4 * x18)) + ((uint64_t)x2 * x17)));
4942df0987dSdlg 	uint64_t x29 = (0x2 * (((((uint64_t)x12 * x12) + ((uint64_t)x10 * x14)) + ((uint64_t)x6 * x18)) + (0x2 * (((uint64_t)x8 * x16) + ((uint64_t)x4 * x17)))));
4952df0987dSdlg 	uint64_t x30 = (0x2 * (((((uint64_t)x12 * x14) + ((uint64_t)x10 * x16)) + ((uint64_t)x8 * x18)) + ((uint64_t)x6 * x17)));
4962df0987dSdlg 	uint64_t x31 = (((uint64_t)x14 * x14) + (0x2 * (((uint64_t)x10 * x18) + (0x2 * (((uint64_t)x12 * x16) + ((uint64_t)x8 * x17))))));
4972df0987dSdlg 	uint64_t x32 = (0x2 * ((((uint64_t)x14 * x16) + ((uint64_t)x12 * x18)) + ((uint64_t)x10 * x17)));
4982df0987dSdlg 	uint64_t x33 = (0x2 * ((((uint64_t)x16 * x16) + ((uint64_t)x14 * x18)) + ((uint64_t)(0x2 * x12) * x17)));
4992df0987dSdlg 	uint64_t x34 = (0x2 * (((uint64_t)x16 * x18) + ((uint64_t)x14 * x17)));
5002df0987dSdlg 	uint64_t x35 = (((uint64_t)x18 * x18) + ((uint64_t)(0x4 * x16) * x17));
5012df0987dSdlg 	uint64_t x36 = ((uint64_t)(0x2 * x18) * x17);
5022df0987dSdlg 	uint64_t x37 = ((uint64_t)(0x2 * x17) * x17);
5032df0987dSdlg 	uint64_t x38 = (x27 + (x37 << 0x4));
5042df0987dSdlg 	uint64_t x39 = (x38 + (x37 << 0x1));
5052df0987dSdlg 	uint64_t x40 = (x39 + x37);
5062df0987dSdlg 	uint64_t x41 = (x26 + (x36 << 0x4));
5072df0987dSdlg 	uint64_t x42 = (x41 + (x36 << 0x1));
5082df0987dSdlg 	uint64_t x43 = (x42 + x36);
5092df0987dSdlg 	uint64_t x44 = (x25 + (x35 << 0x4));
5102df0987dSdlg 	uint64_t x45 = (x44 + (x35 << 0x1));
5112df0987dSdlg 	uint64_t x46 = (x45 + x35);
5122df0987dSdlg 	uint64_t x47 = (x24 + (x34 << 0x4));
5132df0987dSdlg 	uint64_t x48 = (x47 + (x34 << 0x1));
5142df0987dSdlg 	uint64_t x49 = (x48 + x34);
5152df0987dSdlg 	uint64_t x50 = (x23 + (x33 << 0x4));
5162df0987dSdlg 	uint64_t x51 = (x50 + (x33 << 0x1));
5172df0987dSdlg 	uint64_t x52 = (x51 + x33);
5182df0987dSdlg 	uint64_t x53 = (x22 + (x32 << 0x4));
5192df0987dSdlg 	uint64_t x54 = (x53 + (x32 << 0x1));
5202df0987dSdlg 	uint64_t x55 = (x54 + x32);
5212df0987dSdlg 	uint64_t x56 = (x21 + (x31 << 0x4));
5222df0987dSdlg 	uint64_t x57 = (x56 + (x31 << 0x1));
5232df0987dSdlg 	uint64_t x58 = (x57 + x31);
5242df0987dSdlg 	uint64_t x59 = (x20 + (x30 << 0x4));
5252df0987dSdlg 	uint64_t x60 = (x59 + (x30 << 0x1));
5262df0987dSdlg 	uint64_t x61 = (x60 + x30);
5272df0987dSdlg 	uint64_t x62 = (x19 + (x29 << 0x4));
5282df0987dSdlg 	uint64_t x63 = (x62 + (x29 << 0x1));
5292df0987dSdlg 	uint64_t x64 = (x63 + x29);
5302df0987dSdlg 	uint64_t x65 = (x64 >> 0x1a);
5312df0987dSdlg 	uint32_t x66 = ((uint32_t)x64 & 0x3ffffff);
5322df0987dSdlg 	uint64_t x67 = (x65 + x61);
5332df0987dSdlg 	uint64_t x68 = (x67 >> 0x19);
5342df0987dSdlg 	uint32_t x69 = ((uint32_t)x67 & 0x1ffffff);
5352df0987dSdlg 	uint64_t x70 = (x68 + x58);
5362df0987dSdlg 	uint64_t x71 = (x70 >> 0x1a);
5372df0987dSdlg 	uint32_t x72 = ((uint32_t)x70 & 0x3ffffff);
5382df0987dSdlg 	uint64_t x73 = (x71 + x55);
5392df0987dSdlg 	uint64_t x74 = (x73 >> 0x19);
5402df0987dSdlg 	uint32_t x75 = ((uint32_t)x73 & 0x1ffffff);
5412df0987dSdlg 	uint64_t x76 = (x74 + x52);
5422df0987dSdlg 	uint64_t x77 = (x76 >> 0x1a);
5432df0987dSdlg 	uint32_t x78 = ((uint32_t)x76 & 0x3ffffff);
5442df0987dSdlg 	uint64_t x79 = (x77 + x49);
5452df0987dSdlg 	uint64_t x80 = (x79 >> 0x19);
5462df0987dSdlg 	uint32_t x81 = ((uint32_t)x79 & 0x1ffffff);
5472df0987dSdlg 	uint64_t x82 = (x80 + x46);
5482df0987dSdlg 	uint64_t x83 = (x82 >> 0x1a);
5492df0987dSdlg 	uint32_t x84 = ((uint32_t)x82 & 0x3ffffff);
5502df0987dSdlg 	uint64_t x85 = (x83 + x43);
5512df0987dSdlg 	uint64_t x86 = (x85 >> 0x19);
5522df0987dSdlg 	uint32_t x87 = ((uint32_t)x85 & 0x1ffffff);
5532df0987dSdlg 	uint64_t x88 = (x86 + x40);
5542df0987dSdlg 	uint64_t x89 = (x88 >> 0x1a);
5552df0987dSdlg 	uint32_t x90 = ((uint32_t)x88 & 0x3ffffff);
5562df0987dSdlg 	uint64_t x91 = (x89 + x28);
5572df0987dSdlg 	uint64_t x92 = (x91 >> 0x19);
5582df0987dSdlg 	uint32_t x93 = ((uint32_t)x91 & 0x1ffffff);
5592df0987dSdlg 	uint64_t x94 = (x66 + (0x13 * x92));
5602df0987dSdlg 	uint32_t x95 = (uint32_t) (x94 >> 0x1a);
5612df0987dSdlg 	uint32_t x96 = ((uint32_t)x94 & 0x3ffffff);
5622df0987dSdlg 	uint32_t x97 = (x95 + x69);
5632df0987dSdlg 	uint32_t x98 = (x97 >> 0x19);
5642df0987dSdlg 	uint32_t x99 = (x97 & 0x1ffffff);
5652df0987dSdlg 	out[0] = x96;
5662df0987dSdlg 	out[1] = x99;
5672df0987dSdlg 	out[2] = (x98 + x72);
5682df0987dSdlg 	out[3] = x75;
5692df0987dSdlg 	out[4] = x78;
5702df0987dSdlg 	out[5] = x81;
5712df0987dSdlg 	out[6] = x84;
5722df0987dSdlg 	out[7] = x87;
5732df0987dSdlg 	out[8] = x90;
5742df0987dSdlg 	out[9] = x93;
5752df0987dSdlg }
5762df0987dSdlg 
fe_sq_tl(fe * h,const fe_loose * f)5772df0987dSdlg static __always_inline void fe_sq_tl(fe *h, const fe_loose *f)
5782df0987dSdlg {
5792df0987dSdlg 	fe_sqr_impl(h->v, f->v);
5802df0987dSdlg }
5812df0987dSdlg 
fe_sq_tt(fe * h,const fe * f)5822df0987dSdlg static __always_inline void fe_sq_tt(fe *h, const fe *f)
5832df0987dSdlg {
5842df0987dSdlg 	fe_sqr_impl(h->v, f->v);
5852df0987dSdlg }
5862df0987dSdlg 
fe_loose_invert(fe * out,const fe_loose * z)5872df0987dSdlg static __always_inline void fe_loose_invert(fe *out, const fe_loose *z)
5882df0987dSdlg {
5892df0987dSdlg 	fe t0;
5902df0987dSdlg 	fe t1;
5912df0987dSdlg 	fe t2;
5922df0987dSdlg 	fe t3;
5932df0987dSdlg 	int i;
5942df0987dSdlg 
5952df0987dSdlg 	fe_sq_tl(&t0, z);
5962df0987dSdlg 	fe_sq_tt(&t1, &t0);
5972df0987dSdlg 	for (i = 1; i < 2; ++i)
5982df0987dSdlg 		fe_sq_tt(&t1, &t1);
5992df0987dSdlg 	fe_mul_tlt(&t1, z, &t1);
6002df0987dSdlg 	fe_mul_ttt(&t0, &t0, &t1);
6012df0987dSdlg 	fe_sq_tt(&t2, &t0);
6022df0987dSdlg 	fe_mul_ttt(&t1, &t1, &t2);
6032df0987dSdlg 	fe_sq_tt(&t2, &t1);
6042df0987dSdlg 	for (i = 1; i < 5; ++i)
6052df0987dSdlg 		fe_sq_tt(&t2, &t2);
6062df0987dSdlg 	fe_mul_ttt(&t1, &t2, &t1);
6072df0987dSdlg 	fe_sq_tt(&t2, &t1);
6082df0987dSdlg 	for (i = 1; i < 10; ++i)
6092df0987dSdlg 		fe_sq_tt(&t2, &t2);
6102df0987dSdlg 	fe_mul_ttt(&t2, &t2, &t1);
6112df0987dSdlg 	fe_sq_tt(&t3, &t2);
6122df0987dSdlg 	for (i = 1; i < 20; ++i)
6132df0987dSdlg 		fe_sq_tt(&t3, &t3);
6142df0987dSdlg 	fe_mul_ttt(&t2, &t3, &t2);
6152df0987dSdlg 	fe_sq_tt(&t2, &t2);
6162df0987dSdlg 	for (i = 1; i < 10; ++i)
6172df0987dSdlg 		fe_sq_tt(&t2, &t2);
6182df0987dSdlg 	fe_mul_ttt(&t1, &t2, &t1);
6192df0987dSdlg 	fe_sq_tt(&t2, &t1);
6202df0987dSdlg 	for (i = 1; i < 50; ++i)
6212df0987dSdlg 		fe_sq_tt(&t2, &t2);
6222df0987dSdlg 	fe_mul_ttt(&t2, &t2, &t1);
6232df0987dSdlg 	fe_sq_tt(&t3, &t2);
6242df0987dSdlg 	for (i = 1; i < 100; ++i)
6252df0987dSdlg 		fe_sq_tt(&t3, &t3);
6262df0987dSdlg 	fe_mul_ttt(&t2, &t3, &t2);
6272df0987dSdlg 	fe_sq_tt(&t2, &t2);
6282df0987dSdlg 	for (i = 1; i < 50; ++i)
6292df0987dSdlg 		fe_sq_tt(&t2, &t2);
6302df0987dSdlg 	fe_mul_ttt(&t1, &t2, &t1);
6312df0987dSdlg 	fe_sq_tt(&t1, &t1);
6322df0987dSdlg 	for (i = 1; i < 5; ++i)
6332df0987dSdlg 		fe_sq_tt(&t1, &t1);
6342df0987dSdlg 	fe_mul_ttt(out, &t1, &t0);
6352df0987dSdlg }
6362df0987dSdlg 
fe_invert(fe * out,const fe * z)6372df0987dSdlg static __always_inline void fe_invert(fe *out, const fe *z)
6382df0987dSdlg {
6392df0987dSdlg 	fe_loose l;
6402df0987dSdlg 	fe_copy_lt(&l, z);
6412df0987dSdlg 	fe_loose_invert(out, &l);
6422df0987dSdlg }
6432df0987dSdlg 
6442df0987dSdlg /* Replace (f,g) with (g,f) if b == 1;
6452df0987dSdlg  * replace (f,g) with (f,g) if b == 0.
6462df0987dSdlg  *
6472df0987dSdlg  * Preconditions: b in {0,1}
6482df0987dSdlg  */
fe_cswap(fe * f,fe * g,unsigned int b)6492df0987dSdlg static __always_inline void fe_cswap(fe *f, fe *g, unsigned int b)
6502df0987dSdlg {
6512df0987dSdlg 	unsigned i;
6522df0987dSdlg 	b = 0 - b;
6532df0987dSdlg 	for (i = 0; i < 10; i++) {
6542df0987dSdlg 		uint32_t x = f->v[i] ^ g->v[i];
6552df0987dSdlg 		x &= b;
6562df0987dSdlg 		f->v[i] ^= x;
6572df0987dSdlg 		g->v[i] ^= x;
6582df0987dSdlg 	}
6592df0987dSdlg }
6602df0987dSdlg 
6612df0987dSdlg /* NOTE: based on fiat-crypto fe_mul, edited for in2=121666, 0, 0.*/
fe_mul_121666_impl(uint32_t out[10],const uint32_t in1[10])6622df0987dSdlg static __always_inline void fe_mul_121666_impl(uint32_t out[10], const uint32_t in1[10])
6632df0987dSdlg {
6642df0987dSdlg 	const uint32_t x20 = in1[9];
6652df0987dSdlg 	const uint32_t x21 = in1[8];
6662df0987dSdlg 	const uint32_t x19 = in1[7];
6672df0987dSdlg 	const uint32_t x17 = in1[6];
6682df0987dSdlg 	const uint32_t x15 = in1[5];
6692df0987dSdlg 	const uint32_t x13 = in1[4];
6702df0987dSdlg 	const uint32_t x11 = in1[3];
6712df0987dSdlg 	const uint32_t x9 = in1[2];
6722df0987dSdlg 	const uint32_t x7 = in1[1];
6732df0987dSdlg 	const uint32_t x5 = in1[0];
6742df0987dSdlg 	const uint32_t x38 = 0;
6752df0987dSdlg 	const uint32_t x39 = 0;
6762df0987dSdlg 	const uint32_t x37 = 0;
6772df0987dSdlg 	const uint32_t x35 = 0;
6782df0987dSdlg 	const uint32_t x33 = 0;
6792df0987dSdlg 	const uint32_t x31 = 0;
6802df0987dSdlg 	const uint32_t x29 = 0;
6812df0987dSdlg 	const uint32_t x27 = 0;
6822df0987dSdlg 	const uint32_t x25 = 0;
6832df0987dSdlg 	const uint32_t x23 = 121666;
6842df0987dSdlg 	uint64_t x40 = ((uint64_t)x23 * x5);
6852df0987dSdlg 	uint64_t x41 = (((uint64_t)x23 * x7) + ((uint64_t)x25 * x5));
6862df0987dSdlg 	uint64_t x42 = ((((uint64_t)(0x2 * x25) * x7) + ((uint64_t)x23 * x9)) + ((uint64_t)x27 * x5));
6872df0987dSdlg 	uint64_t x43 = (((((uint64_t)x25 * x9) + ((uint64_t)x27 * x7)) + ((uint64_t)x23 * x11)) + ((uint64_t)x29 * x5));
6882df0987dSdlg 	uint64_t x44 = (((((uint64_t)x27 * x9) + (0x2 * (((uint64_t)x25 * x11) + ((uint64_t)x29 * x7)))) + ((uint64_t)x23 * x13)) + ((uint64_t)x31 * x5));
6892df0987dSdlg 	uint64_t x45 = (((((((uint64_t)x27 * x11) + ((uint64_t)x29 * x9)) + ((uint64_t)x25 * x13)) + ((uint64_t)x31 * x7)) + ((uint64_t)x23 * x15)) + ((uint64_t)x33 * x5));
6902df0987dSdlg 	uint64_t x46 = (((((0x2 * ((((uint64_t)x29 * x11) + ((uint64_t)x25 * x15)) + ((uint64_t)x33 * x7))) + ((uint64_t)x27 * x13)) + ((uint64_t)x31 * x9)) + ((uint64_t)x23 * x17)) + ((uint64_t)x35 * x5));
6912df0987dSdlg 	uint64_t x47 = (((((((((uint64_t)x29 * x13) + ((uint64_t)x31 * x11)) + ((uint64_t)x27 * x15)) + ((uint64_t)x33 * x9)) + ((uint64_t)x25 * x17)) + ((uint64_t)x35 * x7)) + ((uint64_t)x23 * x19)) + ((uint64_t)x37 * x5));
6922df0987dSdlg 	uint64_t x48 = (((((((uint64_t)x31 * x13) + (0x2 * (((((uint64_t)x29 * x15) + ((uint64_t)x33 * x11)) + ((uint64_t)x25 * x19)) + ((uint64_t)x37 * x7)))) + ((uint64_t)x27 * x17)) + ((uint64_t)x35 * x9)) + ((uint64_t)x23 * x21)) + ((uint64_t)x39 * x5));
6932df0987dSdlg 	uint64_t x49 = (((((((((((uint64_t)x31 * x15) + ((uint64_t)x33 * x13)) + ((uint64_t)x29 * x17)) + ((uint64_t)x35 * x11)) + ((uint64_t)x27 * x19)) + ((uint64_t)x37 * x9)) + ((uint64_t)x25 * x21)) + ((uint64_t)x39 * x7)) + ((uint64_t)x23 * x20)) + ((uint64_t)x38 * x5));
6942df0987dSdlg 	uint64_t x50 = (((((0x2 * ((((((uint64_t)x33 * x15) + ((uint64_t)x29 * x19)) + ((uint64_t)x37 * x11)) + ((uint64_t)x25 * x20)) + ((uint64_t)x38 * x7))) + ((uint64_t)x31 * x17)) + ((uint64_t)x35 * x13)) + ((uint64_t)x27 * x21)) + ((uint64_t)x39 * x9));
6952df0987dSdlg 	uint64_t x51 = (((((((((uint64_t)x33 * x17) + ((uint64_t)x35 * x15)) + ((uint64_t)x31 * x19)) + ((uint64_t)x37 * x13)) + ((uint64_t)x29 * x21)) + ((uint64_t)x39 * x11)) + ((uint64_t)x27 * x20)) + ((uint64_t)x38 * x9));
6962df0987dSdlg 	uint64_t x52 = (((((uint64_t)x35 * x17) + (0x2 * (((((uint64_t)x33 * x19) + ((uint64_t)x37 * x15)) + ((uint64_t)x29 * x20)) + ((uint64_t)x38 * x11)))) + ((uint64_t)x31 * x21)) + ((uint64_t)x39 * x13));
6972df0987dSdlg 	uint64_t x53 = (((((((uint64_t)x35 * x19) + ((uint64_t)x37 * x17)) + ((uint64_t)x33 * x21)) + ((uint64_t)x39 * x15)) + ((uint64_t)x31 * x20)) + ((uint64_t)x38 * x13));
6982df0987dSdlg 	uint64_t x54 = (((0x2 * ((((uint64_t)x37 * x19) + ((uint64_t)x33 * x20)) + ((uint64_t)x38 * x15))) + ((uint64_t)x35 * x21)) + ((uint64_t)x39 * x17));
6992df0987dSdlg 	uint64_t x55 = (((((uint64_t)x37 * x21) + ((uint64_t)x39 * x19)) + ((uint64_t)x35 * x20)) + ((uint64_t)x38 * x17));
7002df0987dSdlg 	uint64_t x56 = (((uint64_t)x39 * x21) + (0x2 * (((uint64_t)x37 * x20) + ((uint64_t)x38 * x19))));
7012df0987dSdlg 	uint64_t x57 = (((uint64_t)x39 * x20) + ((uint64_t)x38 * x21));
7022df0987dSdlg 	uint64_t x58 = ((uint64_t)(0x2 * x38) * x20);
7032df0987dSdlg 	uint64_t x59 = (x48 + (x58 << 0x4));
7042df0987dSdlg 	uint64_t x60 = (x59 + (x58 << 0x1));
7052df0987dSdlg 	uint64_t x61 = (x60 + x58);
7062df0987dSdlg 	uint64_t x62 = (x47 + (x57 << 0x4));
7072df0987dSdlg 	uint64_t x63 = (x62 + (x57 << 0x1));
7082df0987dSdlg 	uint64_t x64 = (x63 + x57);
7092df0987dSdlg 	uint64_t x65 = (x46 + (x56 << 0x4));
7102df0987dSdlg 	uint64_t x66 = (x65 + (x56 << 0x1));
7112df0987dSdlg 	uint64_t x67 = (x66 + x56);
7122df0987dSdlg 	uint64_t x68 = (x45 + (x55 << 0x4));
7132df0987dSdlg 	uint64_t x69 = (x68 + (x55 << 0x1));
7142df0987dSdlg 	uint64_t x70 = (x69 + x55);
7152df0987dSdlg 	uint64_t x71 = (x44 + (x54 << 0x4));
7162df0987dSdlg 	uint64_t x72 = (x71 + (x54 << 0x1));
7172df0987dSdlg 	uint64_t x73 = (x72 + x54);
7182df0987dSdlg 	uint64_t x74 = (x43 + (x53 << 0x4));
7192df0987dSdlg 	uint64_t x75 = (x74 + (x53 << 0x1));
7202df0987dSdlg 	uint64_t x76 = (x75 + x53);
7212df0987dSdlg 	uint64_t x77 = (x42 + (x52 << 0x4));
7222df0987dSdlg 	uint64_t x78 = (x77 + (x52 << 0x1));
7232df0987dSdlg 	uint64_t x79 = (x78 + x52);
7242df0987dSdlg 	uint64_t x80 = (x41 + (x51 << 0x4));
7252df0987dSdlg 	uint64_t x81 = (x80 + (x51 << 0x1));
7262df0987dSdlg 	uint64_t x82 = (x81 + x51);
7272df0987dSdlg 	uint64_t x83 = (x40 + (x50 << 0x4));
7282df0987dSdlg 	uint64_t x84 = (x83 + (x50 << 0x1));
7292df0987dSdlg 	uint64_t x85 = (x84 + x50);
7302df0987dSdlg 	uint64_t x86 = (x85 >> 0x1a);
7312df0987dSdlg 	uint32_t x87 = ((uint32_t)x85 & 0x3ffffff);
7322df0987dSdlg 	uint64_t x88 = (x86 + x82);
7332df0987dSdlg 	uint64_t x89 = (x88 >> 0x19);
7342df0987dSdlg 	uint32_t x90 = ((uint32_t)x88 & 0x1ffffff);
7352df0987dSdlg 	uint64_t x91 = (x89 + x79);
7362df0987dSdlg 	uint64_t x92 = (x91 >> 0x1a);
7372df0987dSdlg 	uint32_t x93 = ((uint32_t)x91 & 0x3ffffff);
7382df0987dSdlg 	uint64_t x94 = (x92 + x76);
7392df0987dSdlg 	uint64_t x95 = (x94 >> 0x19);
7402df0987dSdlg 	uint32_t x96 = ((uint32_t)x94 & 0x1ffffff);
7412df0987dSdlg 	uint64_t x97 = (x95 + x73);
7422df0987dSdlg 	uint64_t x98 = (x97 >> 0x1a);
7432df0987dSdlg 	uint32_t x99 = ((uint32_t)x97 & 0x3ffffff);
7442df0987dSdlg 	uint64_t x100 = (x98 + x70);
7452df0987dSdlg 	uint64_t x101 = (x100 >> 0x19);
7462df0987dSdlg 	uint32_t x102 = ((uint32_t)x100 & 0x1ffffff);
7472df0987dSdlg 	uint64_t x103 = (x101 + x67);
7482df0987dSdlg 	uint64_t x104 = (x103 >> 0x1a);
7492df0987dSdlg 	uint32_t x105 = ((uint32_t)x103 & 0x3ffffff);
7502df0987dSdlg 	uint64_t x106 = (x104 + x64);
7512df0987dSdlg 	uint64_t x107 = (x106 >> 0x19);
7522df0987dSdlg 	uint32_t x108 = ((uint32_t)x106 & 0x1ffffff);
7532df0987dSdlg 	uint64_t x109 = (x107 + x61);
7542df0987dSdlg 	uint64_t x110 = (x109 >> 0x1a);
7552df0987dSdlg 	uint32_t x111 = ((uint32_t)x109 & 0x3ffffff);
7562df0987dSdlg 	uint64_t x112 = (x110 + x49);
7572df0987dSdlg 	uint64_t x113 = (x112 >> 0x19);
7582df0987dSdlg 	uint32_t x114 = ((uint32_t)x112 & 0x1ffffff);
7592df0987dSdlg 	uint64_t x115 = (x87 + (0x13 * x113));
7602df0987dSdlg 	uint32_t x116 = (uint32_t) (x115 >> 0x1a);
7612df0987dSdlg 	uint32_t x117 = ((uint32_t)x115 & 0x3ffffff);
7622df0987dSdlg 	uint32_t x118 = (x116 + x90);
7632df0987dSdlg 	uint32_t x119 = (x118 >> 0x19);
7642df0987dSdlg 	uint32_t x120 = (x118 & 0x1ffffff);
7652df0987dSdlg 	out[0] = x117;
7662df0987dSdlg 	out[1] = x120;
7672df0987dSdlg 	out[2] = (x119 + x93);
7682df0987dSdlg 	out[3] = x96;
7692df0987dSdlg 	out[4] = x99;
7702df0987dSdlg 	out[5] = x102;
7712df0987dSdlg 	out[6] = x105;
7722df0987dSdlg 	out[7] = x108;
7732df0987dSdlg 	out[8] = x111;
7742df0987dSdlg 	out[9] = x114;
7752df0987dSdlg }
7762df0987dSdlg 
fe_mul121666(fe * h,const fe_loose * f)7772df0987dSdlg static __always_inline void fe_mul121666(fe *h, const fe_loose *f)
7782df0987dSdlg {
7792df0987dSdlg 	fe_mul_121666_impl(h->v, f->v);
7802df0987dSdlg }
7812df0987dSdlg 
curve25519(uint8_t out[CURVE25519_KEY_SIZE],const uint8_t scalar[CURVE25519_KEY_SIZE],const uint8_t point[CURVE25519_KEY_SIZE])7822df0987dSdlg int curve25519(uint8_t out[CURVE25519_KEY_SIZE],
7832df0987dSdlg 	       const uint8_t scalar[CURVE25519_KEY_SIZE],
7842df0987dSdlg 	       const uint8_t point[CURVE25519_KEY_SIZE])
7852df0987dSdlg {
7862df0987dSdlg 	fe x1, x2, z2, x3, z3;
7872df0987dSdlg 	fe_loose x2l, z2l, x3l;
7882df0987dSdlg 	unsigned swap = 0;
7892df0987dSdlg 	int pos;
7902df0987dSdlg 	uint8_t e[32];
7912df0987dSdlg 
7922df0987dSdlg 	memcpy(e, scalar, 32);
7932df0987dSdlg 	curve25519_clamp_secret(e);
7942df0987dSdlg 
7952df0987dSdlg 	/* The following implementation was transcribed to Coq and proven to
7962df0987dSdlg 	 * correspond to unary scalar multiplication in affine coordinates given
7972df0987dSdlg 	 * that x1 != 0 is the x coordinate of some point on the curve. It was
7982df0987dSdlg 	 * also checked in Coq that doing a ladderstep with x1 = x3 = 0 gives
7992df0987dSdlg 	 * z2' = z3' = 0, and z2 = z3 = 0 gives z2' = z3' = 0. The statement was
8002df0987dSdlg 	 * quantified over the underlying field, so it applies to Curve25519
8012df0987dSdlg 	 * itself and the quadratic twist of Curve25519. It was not proven in
8022df0987dSdlg 	 * Coq that prime-field arithmetic correctly simulates extension-field
8032df0987dSdlg 	 * arithmetic on prime-field values. The decoding of the byte array
8042df0987dSdlg 	 * representation of e was not considered.
8052df0987dSdlg 	 *
8062df0987dSdlg 	 * Specification of Montgomery curves in affine coordinates:
8072df0987dSdlg 	 * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Spec/MontgomeryCurve.v#L27>
8082df0987dSdlg 	 *
8092df0987dSdlg 	 * Proof that these form a group that is isomorphic to a Weierstrass
8102df0987dSdlg 	 * curve:
8112df0987dSdlg 	 * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/AffineProofs.v#L35>
8122df0987dSdlg 	 *
8132df0987dSdlg 	 * Coq transcription and correctness proof of the loop
8142df0987dSdlg 	 * (where scalarbits=255):
8152df0987dSdlg 	 * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZ.v#L118>
8162df0987dSdlg 	 * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L278>
8172df0987dSdlg 	 * preconditions: 0 <= e < 2^255 (not necessarily e < order),
8182df0987dSdlg 	 * fe_invert(0) = 0
8192df0987dSdlg 	 */
8202df0987dSdlg 	fe_frombytes(&x1, point);
8212df0987dSdlg 	fe_1(&x2);
8222df0987dSdlg 	fe_0(&z2);
8232df0987dSdlg 	fe_copy(&x3, &x1);
8242df0987dSdlg 	fe_1(&z3);
8252df0987dSdlg 
8262df0987dSdlg 	for (pos = 254; pos >= 0; --pos) {
8272df0987dSdlg 		fe tmp0, tmp1;
8282df0987dSdlg 		fe_loose tmp0l, tmp1l;
8292df0987dSdlg 		/* loop invariant as of right before the test, for the case
8302df0987dSdlg 		 * where x1 != 0:
8312df0987dSdlg 		 *   pos >= -1; if z2 = 0 then x2 is nonzero; if z3 = 0 then x3
8322df0987dSdlg 		 *   is nonzero
8332df0987dSdlg 		 *   let r := e >> (pos+1) in the following equalities of
8342df0987dSdlg 		 *   projective points:
8352df0987dSdlg 		 *   to_xz (r*P)     === if swap then (x3, z3) else (x2, z2)
8362df0987dSdlg 		 *   to_xz ((r+1)*P) === if swap then (x2, z2) else (x3, z3)
8372df0987dSdlg 		 *   x1 is the nonzero x coordinate of the nonzero
8382df0987dSdlg 		 *   point (r*P-(r+1)*P)
8392df0987dSdlg 		 */
8402df0987dSdlg 		unsigned b = 1 & (e[pos / 8] >> (pos & 7));
8412df0987dSdlg 		swap ^= b;
8422df0987dSdlg 		fe_cswap(&x2, &x3, swap);
8432df0987dSdlg 		fe_cswap(&z2, &z3, swap);
8442df0987dSdlg 		swap = b;
8452df0987dSdlg 		/* Coq transcription of ladderstep formula (called from
8462df0987dSdlg 		 * transcribed loop):
8472df0987dSdlg 		 * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZ.v#L89>
8482df0987dSdlg 		 * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L131>
8492df0987dSdlg 		 * x1 != 0 <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L217>
8502df0987dSdlg 		 * x1  = 0 <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L147>
8512df0987dSdlg 		 */
8522df0987dSdlg 		fe_sub(&tmp0l, &x3, &z3);
8532df0987dSdlg 		fe_sub(&tmp1l, &x2, &z2);
8542df0987dSdlg 		fe_add(&x2l, &x2, &z2);
8552df0987dSdlg 		fe_add(&z2l, &x3, &z3);
8562df0987dSdlg 		fe_mul_tll(&z3, &tmp0l, &x2l);
8572df0987dSdlg 		fe_mul_tll(&z2, &z2l, &tmp1l);
8582df0987dSdlg 		fe_sq_tl(&tmp0, &tmp1l);
8592df0987dSdlg 		fe_sq_tl(&tmp1, &x2l);
8602df0987dSdlg 		fe_add(&x3l, &z3, &z2);
8612df0987dSdlg 		fe_sub(&z2l, &z3, &z2);
8622df0987dSdlg 		fe_mul_ttt(&x2, &tmp1, &tmp0);
8632df0987dSdlg 		fe_sub(&tmp1l, &tmp1, &tmp0);
8642df0987dSdlg 		fe_sq_tl(&z2, &z2l);
8652df0987dSdlg 		fe_mul121666(&z3, &tmp1l);
8662df0987dSdlg 		fe_sq_tl(&x3, &x3l);
8672df0987dSdlg 		fe_add(&tmp0l, &tmp0, &z3);
8682df0987dSdlg 		fe_mul_ttt(&z3, &x1, &z2);
8692df0987dSdlg 		fe_mul_tll(&z2, &tmp1l, &tmp0l);
8702df0987dSdlg 	}
8712df0987dSdlg 	/* here pos=-1, so r=e, so to_xz (e*P) === if swap then (x3, z3)
8722df0987dSdlg 	 * else (x2, z2)
8732df0987dSdlg 	 */
8742df0987dSdlg 	fe_cswap(&x2, &x3, swap);
8752df0987dSdlg 	fe_cswap(&z2, &z3, swap);
8762df0987dSdlg 
8772df0987dSdlg 	fe_invert(&z2, &z2);
8782df0987dSdlg 	fe_mul_ttt(&x2, &x2, &z2);
8792df0987dSdlg 	fe_tobytes(out, &x2);
8802df0987dSdlg 
8812df0987dSdlg 	explicit_bzero(&x1, sizeof(x1));
8822df0987dSdlg 	explicit_bzero(&x2, sizeof(x2));
8832df0987dSdlg 	explicit_bzero(&z2, sizeof(z2));
8842df0987dSdlg 	explicit_bzero(&x3, sizeof(x3));
8852df0987dSdlg 	explicit_bzero(&z3, sizeof(z3));
8862df0987dSdlg 	explicit_bzero(&x2l, sizeof(x2l));
8872df0987dSdlg 	explicit_bzero(&z2l, sizeof(z2l));
8882df0987dSdlg 	explicit_bzero(&x3l, sizeof(x3l));
8892df0987dSdlg 	explicit_bzero(&e, sizeof(e));
8902df0987dSdlg 	return timingsafe_bcmp(out, null_point, CURVE25519_KEY_SIZE);
8912df0987dSdlg }
892