162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 OR MIT
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2015-2016 The fiat-crypto Authors.
462306a36Sopenharmony_ci * Copyright (C) 2018-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * This is a machine-generated formally verified implementation of Curve25519
762306a36Sopenharmony_ci * ECDH from: <https://github.com/mit-plv/fiat-crypto>. Though originally
862306a36Sopenharmony_ci * machine generated, it has been tweaked to be suitable for use in the kernel.
962306a36Sopenharmony_ci * It is optimized for 32-bit machines and machines that cannot work efficiently
1062306a36Sopenharmony_ci * with 128-bit integer types.
1162306a36Sopenharmony_ci */
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <asm/unaligned.h>
1462306a36Sopenharmony_ci#include <crypto/curve25519.h>
1562306a36Sopenharmony_ci#include <linux/string.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci/* fe means field element. Here the field is \Z/(2^255-19). An element t,
1862306a36Sopenharmony_ci * entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77
1962306a36Sopenharmony_ci * t[3]+2^102 t[4]+...+2^230 t[9].
2062306a36Sopenharmony_ci * fe limbs are bounded by 1.125*2^26,1.125*2^25,1.125*2^26,1.125*2^25,etc.
2162306a36Sopenharmony_ci * Multiplication and carrying produce fe from fe_loose.
2262306a36Sopenharmony_ci */
2362306a36Sopenharmony_citypedef struct fe { u32 v[10]; } fe;
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/* fe_loose limbs are bounded by 3.375*2^26,3.375*2^25,3.375*2^26,3.375*2^25,etc
2662306a36Sopenharmony_ci * Addition and subtraction produce fe_loose from (fe, fe).
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_citypedef struct fe_loose { u32 v[10]; } fe_loose;
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_cistatic __always_inline void fe_frombytes_impl(u32 h[10], const u8 *s)
3162306a36Sopenharmony_ci{
3262306a36Sopenharmony_ci	/* Ignores top bit of s. */
3362306a36Sopenharmony_ci	u32 a0 = get_unaligned_le32(s);
3462306a36Sopenharmony_ci	u32 a1 = get_unaligned_le32(s+4);
3562306a36Sopenharmony_ci	u32 a2 = get_unaligned_le32(s+8);
3662306a36Sopenharmony_ci	u32 a3 = get_unaligned_le32(s+12);
3762306a36Sopenharmony_ci	u32 a4 = get_unaligned_le32(s+16);
3862306a36Sopenharmony_ci	u32 a5 = get_unaligned_le32(s+20);
3962306a36Sopenharmony_ci	u32 a6 = get_unaligned_le32(s+24);
4062306a36Sopenharmony_ci	u32 a7 = get_unaligned_le32(s+28);
4162306a36Sopenharmony_ci	h[0] = a0&((1<<26)-1);                    /* 26 used, 32-26 left.   26 */
4262306a36Sopenharmony_ci	h[1] = (a0>>26) | ((a1&((1<<19)-1))<< 6); /* (32-26) + 19 =  6+19 = 25 */
4362306a36Sopenharmony_ci	h[2] = (a1>>19) | ((a2&((1<<13)-1))<<13); /* (32-19) + 13 = 13+13 = 26 */
4462306a36Sopenharmony_ci	h[3] = (a2>>13) | ((a3&((1<< 6)-1))<<19); /* (32-13) +  6 = 19+ 6 = 25 */
4562306a36Sopenharmony_ci	h[4] = (a3>> 6);                          /* (32- 6)              = 26 */
4662306a36Sopenharmony_ci	h[5] = a4&((1<<25)-1);                    /*                        25 */
4762306a36Sopenharmony_ci	h[6] = (a4>>25) | ((a5&((1<<19)-1))<< 7); /* (32-25) + 19 =  7+19 = 26 */
4862306a36Sopenharmony_ci	h[7] = (a5>>19) | ((a6&((1<<12)-1))<<13); /* (32-19) + 12 = 13+12 = 25 */
4962306a36Sopenharmony_ci	h[8] = (a6>>12) | ((a7&((1<< 6)-1))<<20); /* (32-12) +  6 = 20+ 6 = 26 */
5062306a36Sopenharmony_ci	h[9] = (a7>> 6)&((1<<25)-1); /*                                     25 */
5162306a36Sopenharmony_ci}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cistatic __always_inline void fe_frombytes(fe *h, const u8 *s)
5462306a36Sopenharmony_ci{
5562306a36Sopenharmony_ci	fe_frombytes_impl(h->v, s);
5662306a36Sopenharmony_ci}
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_cistatic __always_inline u8 /*bool*/
5962306a36Sopenharmony_ciaddcarryx_u25(u8 /*bool*/ c, u32 a, u32 b, u32 *low)
6062306a36Sopenharmony_ci{
6162306a36Sopenharmony_ci	/* This function extracts 25 bits of result and 1 bit of carry
6262306a36Sopenharmony_ci	 * (26 total), so a 32-bit intermediate is sufficient.
6362306a36Sopenharmony_ci	 */
6462306a36Sopenharmony_ci	u32 x = a + b + c;
6562306a36Sopenharmony_ci	*low = x & ((1 << 25) - 1);
6662306a36Sopenharmony_ci	return (x >> 25) & 1;
6762306a36Sopenharmony_ci}
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_cistatic __always_inline u8 /*bool*/
7062306a36Sopenharmony_ciaddcarryx_u26(u8 /*bool*/ c, u32 a, u32 b, u32 *low)
7162306a36Sopenharmony_ci{
7262306a36Sopenharmony_ci	/* This function extracts 26 bits of result and 1 bit of carry
7362306a36Sopenharmony_ci	 * (27 total), so a 32-bit intermediate is sufficient.
7462306a36Sopenharmony_ci	 */
7562306a36Sopenharmony_ci	u32 x = a + b + c;
7662306a36Sopenharmony_ci	*low = x & ((1 << 26) - 1);
7762306a36Sopenharmony_ci	return (x >> 26) & 1;
7862306a36Sopenharmony_ci}
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_cistatic __always_inline u8 /*bool*/
8162306a36Sopenharmony_cisubborrow_u25(u8 /*bool*/ c, u32 a, u32 b, u32 *low)
8262306a36Sopenharmony_ci{
8362306a36Sopenharmony_ci	/* This function extracts 25 bits of result and 1 bit of borrow
8462306a36Sopenharmony_ci	 * (26 total), so a 32-bit intermediate is sufficient.
8562306a36Sopenharmony_ci	 */
8662306a36Sopenharmony_ci	u32 x = a - b - c;
8762306a36Sopenharmony_ci	*low = x & ((1 << 25) - 1);
8862306a36Sopenharmony_ci	return x >> 31;
8962306a36Sopenharmony_ci}
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_cistatic __always_inline u8 /*bool*/
9262306a36Sopenharmony_cisubborrow_u26(u8 /*bool*/ c, u32 a, u32 b, u32 *low)
9362306a36Sopenharmony_ci{
9462306a36Sopenharmony_ci	/* This function extracts 26 bits of result and 1 bit of borrow
9562306a36Sopenharmony_ci	 *(27 total), so a 32-bit intermediate is sufficient.
9662306a36Sopenharmony_ci	 */
9762306a36Sopenharmony_ci	u32 x = a - b - c;
9862306a36Sopenharmony_ci	*low = x & ((1 << 26) - 1);
9962306a36Sopenharmony_ci	return x >> 31;
10062306a36Sopenharmony_ci}
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_cistatic __always_inline u32 cmovznz32(u32 t, u32 z, u32 nz)
10362306a36Sopenharmony_ci{
10462306a36Sopenharmony_ci	t = -!!t; /* all set if nonzero, 0 if 0 */
10562306a36Sopenharmony_ci	return (t&nz) | ((~t)&z);
10662306a36Sopenharmony_ci}
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_cistatic __always_inline void fe_freeze(u32 out[10], const u32 in1[10])
10962306a36Sopenharmony_ci{
11062306a36Sopenharmony_ci	{ const u32 x17 = in1[9];
11162306a36Sopenharmony_ci	{ const u32 x18 = in1[8];
11262306a36Sopenharmony_ci	{ const u32 x16 = in1[7];
11362306a36Sopenharmony_ci	{ const u32 x14 = in1[6];
11462306a36Sopenharmony_ci	{ const u32 x12 = in1[5];
11562306a36Sopenharmony_ci	{ const u32 x10 = in1[4];
11662306a36Sopenharmony_ci	{ const u32 x8 = in1[3];
11762306a36Sopenharmony_ci	{ const u32 x6 = in1[2];
11862306a36Sopenharmony_ci	{ const u32 x4 = in1[1];
11962306a36Sopenharmony_ci	{ const u32 x2 = in1[0];
12062306a36Sopenharmony_ci	{ u32 x20; u8/*bool*/ x21 = subborrow_u26(0x0, x2, 0x3ffffed, &x20);
12162306a36Sopenharmony_ci	{ u32 x23; u8/*bool*/ x24 = subborrow_u25(x21, x4, 0x1ffffff, &x23);
12262306a36Sopenharmony_ci	{ u32 x26; u8/*bool*/ x27 = subborrow_u26(x24, x6, 0x3ffffff, &x26);
12362306a36Sopenharmony_ci	{ u32 x29; u8/*bool*/ x30 = subborrow_u25(x27, x8, 0x1ffffff, &x29);
12462306a36Sopenharmony_ci	{ u32 x32; u8/*bool*/ x33 = subborrow_u26(x30, x10, 0x3ffffff, &x32);
12562306a36Sopenharmony_ci	{ u32 x35; u8/*bool*/ x36 = subborrow_u25(x33, x12, 0x1ffffff, &x35);
12662306a36Sopenharmony_ci	{ u32 x38; u8/*bool*/ x39 = subborrow_u26(x36, x14, 0x3ffffff, &x38);
12762306a36Sopenharmony_ci	{ u32 x41; u8/*bool*/ x42 = subborrow_u25(x39, x16, 0x1ffffff, &x41);
12862306a36Sopenharmony_ci	{ u32 x44; u8/*bool*/ x45 = subborrow_u26(x42, x18, 0x3ffffff, &x44);
12962306a36Sopenharmony_ci	{ u32 x47; u8/*bool*/ x48 = subborrow_u25(x45, x17, 0x1ffffff, &x47);
13062306a36Sopenharmony_ci	{ u32 x49 = cmovznz32(x48, 0x0, 0xffffffff);
13162306a36Sopenharmony_ci	{ u32 x50 = (x49 & 0x3ffffed);
13262306a36Sopenharmony_ci	{ u32 x52; u8/*bool*/ x53 = addcarryx_u26(0x0, x20, x50, &x52);
13362306a36Sopenharmony_ci	{ u32 x54 = (x49 & 0x1ffffff);
13462306a36Sopenharmony_ci	{ u32 x56; u8/*bool*/ x57 = addcarryx_u25(x53, x23, x54, &x56);
13562306a36Sopenharmony_ci	{ u32 x58 = (x49 & 0x3ffffff);
13662306a36Sopenharmony_ci	{ u32 x60; u8/*bool*/ x61 = addcarryx_u26(x57, x26, x58, &x60);
13762306a36Sopenharmony_ci	{ u32 x62 = (x49 & 0x1ffffff);
13862306a36Sopenharmony_ci	{ u32 x64; u8/*bool*/ x65 = addcarryx_u25(x61, x29, x62, &x64);
13962306a36Sopenharmony_ci	{ u32 x66 = (x49 & 0x3ffffff);
14062306a36Sopenharmony_ci	{ u32 x68; u8/*bool*/ x69 = addcarryx_u26(x65, x32, x66, &x68);
14162306a36Sopenharmony_ci	{ u32 x70 = (x49 & 0x1ffffff);
14262306a36Sopenharmony_ci	{ u32 x72; u8/*bool*/ x73 = addcarryx_u25(x69, x35, x70, &x72);
14362306a36Sopenharmony_ci	{ u32 x74 = (x49 & 0x3ffffff);
14462306a36Sopenharmony_ci	{ u32 x76; u8/*bool*/ x77 = addcarryx_u26(x73, x38, x74, &x76);
14562306a36Sopenharmony_ci	{ u32 x78 = (x49 & 0x1ffffff);
14662306a36Sopenharmony_ci	{ u32 x80; u8/*bool*/ x81 = addcarryx_u25(x77, x41, x78, &x80);
14762306a36Sopenharmony_ci	{ u32 x82 = (x49 & 0x3ffffff);
14862306a36Sopenharmony_ci	{ u32 x84; u8/*bool*/ x85 = addcarryx_u26(x81, x44, x82, &x84);
14962306a36Sopenharmony_ci	{ u32 x86 = (x49 & 0x1ffffff);
15062306a36Sopenharmony_ci	{ u32 x88; addcarryx_u25(x85, x47, x86, &x88);
15162306a36Sopenharmony_ci	out[0] = x52;
15262306a36Sopenharmony_ci	out[1] = x56;
15362306a36Sopenharmony_ci	out[2] = x60;
15462306a36Sopenharmony_ci	out[3] = x64;
15562306a36Sopenharmony_ci	out[4] = x68;
15662306a36Sopenharmony_ci	out[5] = x72;
15762306a36Sopenharmony_ci	out[6] = x76;
15862306a36Sopenharmony_ci	out[7] = x80;
15962306a36Sopenharmony_ci	out[8] = x84;
16062306a36Sopenharmony_ci	out[9] = x88;
16162306a36Sopenharmony_ci	}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
16262306a36Sopenharmony_ci}
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_cistatic __always_inline void fe_tobytes(u8 s[32], const fe *f)
16562306a36Sopenharmony_ci{
16662306a36Sopenharmony_ci	u32 h[10];
16762306a36Sopenharmony_ci	fe_freeze(h, f->v);
16862306a36Sopenharmony_ci	s[0] = h[0] >> 0;
16962306a36Sopenharmony_ci	s[1] = h[0] >> 8;
17062306a36Sopenharmony_ci	s[2] = h[0] >> 16;
17162306a36Sopenharmony_ci	s[3] = (h[0] >> 24) | (h[1] << 2);
17262306a36Sopenharmony_ci	s[4] = h[1] >> 6;
17362306a36Sopenharmony_ci	s[5] = h[1] >> 14;
17462306a36Sopenharmony_ci	s[6] = (h[1] >> 22) | (h[2] << 3);
17562306a36Sopenharmony_ci	s[7] = h[2] >> 5;
17662306a36Sopenharmony_ci	s[8] = h[2] >> 13;
17762306a36Sopenharmony_ci	s[9] = (h[2] >> 21) | (h[3] << 5);
17862306a36Sopenharmony_ci	s[10] = h[3] >> 3;
17962306a36Sopenharmony_ci	s[11] = h[3] >> 11;
18062306a36Sopenharmony_ci	s[12] = (h[3] >> 19) | (h[4] << 6);
18162306a36Sopenharmony_ci	s[13] = h[4] >> 2;
18262306a36Sopenharmony_ci	s[14] = h[4] >> 10;
18362306a36Sopenharmony_ci	s[15] = h[4] >> 18;
18462306a36Sopenharmony_ci	s[16] = h[5] >> 0;
18562306a36Sopenharmony_ci	s[17] = h[5] >> 8;
18662306a36Sopenharmony_ci	s[18] = h[5] >> 16;
18762306a36Sopenharmony_ci	s[19] = (h[5] >> 24) | (h[6] << 1);
18862306a36Sopenharmony_ci	s[20] = h[6] >> 7;
18962306a36Sopenharmony_ci	s[21] = h[6] >> 15;
19062306a36Sopenharmony_ci	s[22] = (h[6] >> 23) | (h[7] << 3);
19162306a36Sopenharmony_ci	s[23] = h[7] >> 5;
19262306a36Sopenharmony_ci	s[24] = h[7] >> 13;
19362306a36Sopenharmony_ci	s[25] = (h[7] >> 21) | (h[8] << 4);
19462306a36Sopenharmony_ci	s[26] = h[8] >> 4;
19562306a36Sopenharmony_ci	s[27] = h[8] >> 12;
19662306a36Sopenharmony_ci	s[28] = (h[8] >> 20) | (h[9] << 6);
19762306a36Sopenharmony_ci	s[29] = h[9] >> 2;
19862306a36Sopenharmony_ci	s[30] = h[9] >> 10;
19962306a36Sopenharmony_ci	s[31] = h[9] >> 18;
20062306a36Sopenharmony_ci}
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci/* h = f */
20362306a36Sopenharmony_cistatic __always_inline void fe_copy(fe *h, const fe *f)
20462306a36Sopenharmony_ci{
20562306a36Sopenharmony_ci	memmove(h, f, sizeof(u32) * 10);
20662306a36Sopenharmony_ci}
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_cistatic __always_inline void fe_copy_lt(fe_loose *h, const fe *f)
20962306a36Sopenharmony_ci{
21062306a36Sopenharmony_ci	memmove(h, f, sizeof(u32) * 10);
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci/* h = 0 */
21462306a36Sopenharmony_cistatic __always_inline void fe_0(fe *h)
21562306a36Sopenharmony_ci{
21662306a36Sopenharmony_ci	memset(h, 0, sizeof(u32) * 10);
21762306a36Sopenharmony_ci}
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci/* h = 1 */
22062306a36Sopenharmony_cistatic __always_inline void fe_1(fe *h)
22162306a36Sopenharmony_ci{
22262306a36Sopenharmony_ci	memset(h, 0, sizeof(u32) * 10);
22362306a36Sopenharmony_ci	h->v[0] = 1;
22462306a36Sopenharmony_ci}
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_cistatic noinline void fe_add_impl(u32 out[10], const u32 in1[10], const u32 in2[10])
22762306a36Sopenharmony_ci{
22862306a36Sopenharmony_ci	{ const u32 x20 = in1[9];
22962306a36Sopenharmony_ci	{ const u32 x21 = in1[8];
23062306a36Sopenharmony_ci	{ const u32 x19 = in1[7];
23162306a36Sopenharmony_ci	{ const u32 x17 = in1[6];
23262306a36Sopenharmony_ci	{ const u32 x15 = in1[5];
23362306a36Sopenharmony_ci	{ const u32 x13 = in1[4];
23462306a36Sopenharmony_ci	{ const u32 x11 = in1[3];
23562306a36Sopenharmony_ci	{ const u32 x9 = in1[2];
23662306a36Sopenharmony_ci	{ const u32 x7 = in1[1];
23762306a36Sopenharmony_ci	{ const u32 x5 = in1[0];
23862306a36Sopenharmony_ci	{ const u32 x38 = in2[9];
23962306a36Sopenharmony_ci	{ const u32 x39 = in2[8];
24062306a36Sopenharmony_ci	{ const u32 x37 = in2[7];
24162306a36Sopenharmony_ci	{ const u32 x35 = in2[6];
24262306a36Sopenharmony_ci	{ const u32 x33 = in2[5];
24362306a36Sopenharmony_ci	{ const u32 x31 = in2[4];
24462306a36Sopenharmony_ci	{ const u32 x29 = in2[3];
24562306a36Sopenharmony_ci	{ const u32 x27 = in2[2];
24662306a36Sopenharmony_ci	{ const u32 x25 = in2[1];
24762306a36Sopenharmony_ci	{ const u32 x23 = in2[0];
24862306a36Sopenharmony_ci	out[0] = (x5 + x23);
24962306a36Sopenharmony_ci	out[1] = (x7 + x25);
25062306a36Sopenharmony_ci	out[2] = (x9 + x27);
25162306a36Sopenharmony_ci	out[3] = (x11 + x29);
25262306a36Sopenharmony_ci	out[4] = (x13 + x31);
25362306a36Sopenharmony_ci	out[5] = (x15 + x33);
25462306a36Sopenharmony_ci	out[6] = (x17 + x35);
25562306a36Sopenharmony_ci	out[7] = (x19 + x37);
25662306a36Sopenharmony_ci	out[8] = (x21 + x39);
25762306a36Sopenharmony_ci	out[9] = (x20 + x38);
25862306a36Sopenharmony_ci	}}}}}}}}}}}}}}}}}}}}
25962306a36Sopenharmony_ci}
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci/* h = f + g
26262306a36Sopenharmony_ci * Can overlap h with f or g.
26362306a36Sopenharmony_ci */
26462306a36Sopenharmony_cistatic __always_inline void fe_add(fe_loose *h, const fe *f, const fe *g)
26562306a36Sopenharmony_ci{
26662306a36Sopenharmony_ci	fe_add_impl(h->v, f->v, g->v);
26762306a36Sopenharmony_ci}
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_cistatic noinline void fe_sub_impl(u32 out[10], const u32 in1[10], const u32 in2[10])
27062306a36Sopenharmony_ci{
27162306a36Sopenharmony_ci	{ const u32 x20 = in1[9];
27262306a36Sopenharmony_ci	{ const u32 x21 = in1[8];
27362306a36Sopenharmony_ci	{ const u32 x19 = in1[7];
27462306a36Sopenharmony_ci	{ const u32 x17 = in1[6];
27562306a36Sopenharmony_ci	{ const u32 x15 = in1[5];
27662306a36Sopenharmony_ci	{ const u32 x13 = in1[4];
27762306a36Sopenharmony_ci	{ const u32 x11 = in1[3];
27862306a36Sopenharmony_ci	{ const u32 x9 = in1[2];
27962306a36Sopenharmony_ci	{ const u32 x7 = in1[1];
28062306a36Sopenharmony_ci	{ const u32 x5 = in1[0];
28162306a36Sopenharmony_ci	{ const u32 x38 = in2[9];
28262306a36Sopenharmony_ci	{ const u32 x39 = in2[8];
28362306a36Sopenharmony_ci	{ const u32 x37 = in2[7];
28462306a36Sopenharmony_ci	{ const u32 x35 = in2[6];
28562306a36Sopenharmony_ci	{ const u32 x33 = in2[5];
28662306a36Sopenharmony_ci	{ const u32 x31 = in2[4];
28762306a36Sopenharmony_ci	{ const u32 x29 = in2[3];
28862306a36Sopenharmony_ci	{ const u32 x27 = in2[2];
28962306a36Sopenharmony_ci	{ const u32 x25 = in2[1];
29062306a36Sopenharmony_ci	{ const u32 x23 = in2[0];
29162306a36Sopenharmony_ci	out[0] = ((0x7ffffda + x5) - x23);
29262306a36Sopenharmony_ci	out[1] = ((0x3fffffe + x7) - x25);
29362306a36Sopenharmony_ci	out[2] = ((0x7fffffe + x9) - x27);
29462306a36Sopenharmony_ci	out[3] = ((0x3fffffe + x11) - x29);
29562306a36Sopenharmony_ci	out[4] = ((0x7fffffe + x13) - x31);
29662306a36Sopenharmony_ci	out[5] = ((0x3fffffe + x15) - x33);
29762306a36Sopenharmony_ci	out[6] = ((0x7fffffe + x17) - x35);
29862306a36Sopenharmony_ci	out[7] = ((0x3fffffe + x19) - x37);
29962306a36Sopenharmony_ci	out[8] = ((0x7fffffe + x21) - x39);
30062306a36Sopenharmony_ci	out[9] = ((0x3fffffe + x20) - x38);
30162306a36Sopenharmony_ci	}}}}}}}}}}}}}}}}}}}}
30262306a36Sopenharmony_ci}
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci/* h = f - g
30562306a36Sopenharmony_ci * Can overlap h with f or g.
30662306a36Sopenharmony_ci */
30762306a36Sopenharmony_cistatic __always_inline void fe_sub(fe_loose *h, const fe *f, const fe *g)
30862306a36Sopenharmony_ci{
30962306a36Sopenharmony_ci	fe_sub_impl(h->v, f->v, g->v);
31062306a36Sopenharmony_ci}
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_cistatic noinline void fe_mul_impl(u32 out[10], const u32 in1[10], const u32 in2[10])
31362306a36Sopenharmony_ci{
31462306a36Sopenharmony_ci	{ const u32 x20 = in1[9];
31562306a36Sopenharmony_ci	{ const u32 x21 = in1[8];
31662306a36Sopenharmony_ci	{ const u32 x19 = in1[7];
31762306a36Sopenharmony_ci	{ const u32 x17 = in1[6];
31862306a36Sopenharmony_ci	{ const u32 x15 = in1[5];
31962306a36Sopenharmony_ci	{ const u32 x13 = in1[4];
32062306a36Sopenharmony_ci	{ const u32 x11 = in1[3];
32162306a36Sopenharmony_ci	{ const u32 x9 = in1[2];
32262306a36Sopenharmony_ci	{ const u32 x7 = in1[1];
32362306a36Sopenharmony_ci	{ const u32 x5 = in1[0];
32462306a36Sopenharmony_ci	{ const u32 x38 = in2[9];
32562306a36Sopenharmony_ci	{ const u32 x39 = in2[8];
32662306a36Sopenharmony_ci	{ const u32 x37 = in2[7];
32762306a36Sopenharmony_ci	{ const u32 x35 = in2[6];
32862306a36Sopenharmony_ci	{ const u32 x33 = in2[5];
32962306a36Sopenharmony_ci	{ const u32 x31 = in2[4];
33062306a36Sopenharmony_ci	{ const u32 x29 = in2[3];
33162306a36Sopenharmony_ci	{ const u32 x27 = in2[2];
33262306a36Sopenharmony_ci	{ const u32 x25 = in2[1];
33362306a36Sopenharmony_ci	{ const u32 x23 = in2[0];
33462306a36Sopenharmony_ci	{ u64 x40 = ((u64)x23 * x5);
33562306a36Sopenharmony_ci	{ u64 x41 = (((u64)x23 * x7) + ((u64)x25 * x5));
33662306a36Sopenharmony_ci	{ u64 x42 = ((((u64)(0x2 * x25) * x7) + ((u64)x23 * x9)) + ((u64)x27 * x5));
33762306a36Sopenharmony_ci	{ u64 x43 = (((((u64)x25 * x9) + ((u64)x27 * x7)) + ((u64)x23 * x11)) + ((u64)x29 * x5));
33862306a36Sopenharmony_ci	{ u64 x44 = (((((u64)x27 * x9) + (0x2 * (((u64)x25 * x11) + ((u64)x29 * x7)))) + ((u64)x23 * x13)) + ((u64)x31 * x5));
33962306a36Sopenharmony_ci	{ u64 x45 = (((((((u64)x27 * x11) + ((u64)x29 * x9)) + ((u64)x25 * x13)) + ((u64)x31 * x7)) + ((u64)x23 * x15)) + ((u64)x33 * x5));
34062306a36Sopenharmony_ci	{ u64 x46 = (((((0x2 * ((((u64)x29 * x11) + ((u64)x25 * x15)) + ((u64)x33 * x7))) + ((u64)x27 * x13)) + ((u64)x31 * x9)) + ((u64)x23 * x17)) + ((u64)x35 * x5));
34162306a36Sopenharmony_ci	{ 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));
34262306a36Sopenharmony_ci	{ 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));
34362306a36Sopenharmony_ci	{ 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));
34462306a36Sopenharmony_ci	{ 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));
34562306a36Sopenharmony_ci	{ 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));
34662306a36Sopenharmony_ci	{ u64 x52 = (((((u64)x35 * x17) + (0x2 * (((((u64)x33 * x19) + ((u64)x37 * x15)) + ((u64)x29 * x20)) + ((u64)x38 * x11)))) + ((u64)x31 * x21)) + ((u64)x39 * x13));
34762306a36Sopenharmony_ci	{ u64 x53 = (((((((u64)x35 * x19) + ((u64)x37 * x17)) + ((u64)x33 * x21)) + ((u64)x39 * x15)) + ((u64)x31 * x20)) + ((u64)x38 * x13));
34862306a36Sopenharmony_ci	{ u64 x54 = (((0x2 * ((((u64)x37 * x19) + ((u64)x33 * x20)) + ((u64)x38 * x15))) + ((u64)x35 * x21)) + ((u64)x39 * x17));
34962306a36Sopenharmony_ci	{ u64 x55 = (((((u64)x37 * x21) + ((u64)x39 * x19)) + ((u64)x35 * x20)) + ((u64)x38 * x17));
35062306a36Sopenharmony_ci	{ u64 x56 = (((u64)x39 * x21) + (0x2 * (((u64)x37 * x20) + ((u64)x38 * x19))));
35162306a36Sopenharmony_ci	{ u64 x57 = (((u64)x39 * x20) + ((u64)x38 * x21));
35262306a36Sopenharmony_ci	{ u64 x58 = ((u64)(0x2 * x38) * x20);
35362306a36Sopenharmony_ci	{ u64 x59 = (x48 + (x58 << 0x4));
35462306a36Sopenharmony_ci	{ u64 x60 = (x59 + (x58 << 0x1));
35562306a36Sopenharmony_ci	{ u64 x61 = (x60 + x58);
35662306a36Sopenharmony_ci	{ u64 x62 = (x47 + (x57 << 0x4));
35762306a36Sopenharmony_ci	{ u64 x63 = (x62 + (x57 << 0x1));
35862306a36Sopenharmony_ci	{ u64 x64 = (x63 + x57);
35962306a36Sopenharmony_ci	{ u64 x65 = (x46 + (x56 << 0x4));
36062306a36Sopenharmony_ci	{ u64 x66 = (x65 + (x56 << 0x1));
36162306a36Sopenharmony_ci	{ u64 x67 = (x66 + x56);
36262306a36Sopenharmony_ci	{ u64 x68 = (x45 + (x55 << 0x4));
36362306a36Sopenharmony_ci	{ u64 x69 = (x68 + (x55 << 0x1));
36462306a36Sopenharmony_ci	{ u64 x70 = (x69 + x55);
36562306a36Sopenharmony_ci	{ u64 x71 = (x44 + (x54 << 0x4));
36662306a36Sopenharmony_ci	{ u64 x72 = (x71 + (x54 << 0x1));
36762306a36Sopenharmony_ci	{ u64 x73 = (x72 + x54);
36862306a36Sopenharmony_ci	{ u64 x74 = (x43 + (x53 << 0x4));
36962306a36Sopenharmony_ci	{ u64 x75 = (x74 + (x53 << 0x1));
37062306a36Sopenharmony_ci	{ u64 x76 = (x75 + x53);
37162306a36Sopenharmony_ci	{ u64 x77 = (x42 + (x52 << 0x4));
37262306a36Sopenharmony_ci	{ u64 x78 = (x77 + (x52 << 0x1));
37362306a36Sopenharmony_ci	{ u64 x79 = (x78 + x52);
37462306a36Sopenharmony_ci	{ u64 x80 = (x41 + (x51 << 0x4));
37562306a36Sopenharmony_ci	{ u64 x81 = (x80 + (x51 << 0x1));
37662306a36Sopenharmony_ci	{ u64 x82 = (x81 + x51);
37762306a36Sopenharmony_ci	{ u64 x83 = (x40 + (x50 << 0x4));
37862306a36Sopenharmony_ci	{ u64 x84 = (x83 + (x50 << 0x1));
37962306a36Sopenharmony_ci	{ u64 x85 = (x84 + x50);
38062306a36Sopenharmony_ci	{ u64 x86 = (x85 >> 0x1a);
38162306a36Sopenharmony_ci	{ u32 x87 = ((u32)x85 & 0x3ffffff);
38262306a36Sopenharmony_ci	{ u64 x88 = (x86 + x82);
38362306a36Sopenharmony_ci	{ u64 x89 = (x88 >> 0x19);
38462306a36Sopenharmony_ci	{ u32 x90 = ((u32)x88 & 0x1ffffff);
38562306a36Sopenharmony_ci	{ u64 x91 = (x89 + x79);
38662306a36Sopenharmony_ci	{ u64 x92 = (x91 >> 0x1a);
38762306a36Sopenharmony_ci	{ u32 x93 = ((u32)x91 & 0x3ffffff);
38862306a36Sopenharmony_ci	{ u64 x94 = (x92 + x76);
38962306a36Sopenharmony_ci	{ u64 x95 = (x94 >> 0x19);
39062306a36Sopenharmony_ci	{ u32 x96 = ((u32)x94 & 0x1ffffff);
39162306a36Sopenharmony_ci	{ u64 x97 = (x95 + x73);
39262306a36Sopenharmony_ci	{ u64 x98 = (x97 >> 0x1a);
39362306a36Sopenharmony_ci	{ u32 x99 = ((u32)x97 & 0x3ffffff);
39462306a36Sopenharmony_ci	{ u64 x100 = (x98 + x70);
39562306a36Sopenharmony_ci	{ u64 x101 = (x100 >> 0x19);
39662306a36Sopenharmony_ci	{ u32 x102 = ((u32)x100 & 0x1ffffff);
39762306a36Sopenharmony_ci	{ u64 x103 = (x101 + x67);
39862306a36Sopenharmony_ci	{ u64 x104 = (x103 >> 0x1a);
39962306a36Sopenharmony_ci	{ u32 x105 = ((u32)x103 & 0x3ffffff);
40062306a36Sopenharmony_ci	{ u64 x106 = (x104 + x64);
40162306a36Sopenharmony_ci	{ u64 x107 = (x106 >> 0x19);
40262306a36Sopenharmony_ci	{ u32 x108 = ((u32)x106 & 0x1ffffff);
40362306a36Sopenharmony_ci	{ u64 x109 = (x107 + x61);
40462306a36Sopenharmony_ci	{ u64 x110 = (x109 >> 0x1a);
40562306a36Sopenharmony_ci	{ u32 x111 = ((u32)x109 & 0x3ffffff);
40662306a36Sopenharmony_ci	{ u64 x112 = (x110 + x49);
40762306a36Sopenharmony_ci	{ u64 x113 = (x112 >> 0x19);
40862306a36Sopenharmony_ci	{ u32 x114 = ((u32)x112 & 0x1ffffff);
40962306a36Sopenharmony_ci	{ u64 x115 = (x87 + (0x13 * x113));
41062306a36Sopenharmony_ci	{ u32 x116 = (u32) (x115 >> 0x1a);
41162306a36Sopenharmony_ci	{ u32 x117 = ((u32)x115 & 0x3ffffff);
41262306a36Sopenharmony_ci	{ u32 x118 = (x116 + x90);
41362306a36Sopenharmony_ci	{ u32 x119 = (x118 >> 0x19);
41462306a36Sopenharmony_ci	{ u32 x120 = (x118 & 0x1ffffff);
41562306a36Sopenharmony_ci	out[0] = x117;
41662306a36Sopenharmony_ci	out[1] = x120;
41762306a36Sopenharmony_ci	out[2] = (x119 + x93);
41862306a36Sopenharmony_ci	out[3] = x96;
41962306a36Sopenharmony_ci	out[4] = x99;
42062306a36Sopenharmony_ci	out[5] = x102;
42162306a36Sopenharmony_ci	out[6] = x105;
42262306a36Sopenharmony_ci	out[7] = x108;
42362306a36Sopenharmony_ci	out[8] = x111;
42462306a36Sopenharmony_ci	out[9] = x114;
42562306a36Sopenharmony_ci	}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
42662306a36Sopenharmony_ci}
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_cistatic __always_inline void fe_mul_ttt(fe *h, const fe *f, const fe *g)
42962306a36Sopenharmony_ci{
43062306a36Sopenharmony_ci	fe_mul_impl(h->v, f->v, g->v);
43162306a36Sopenharmony_ci}
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_cistatic __always_inline void fe_mul_tlt(fe *h, const fe_loose *f, const fe *g)
43462306a36Sopenharmony_ci{
43562306a36Sopenharmony_ci	fe_mul_impl(h->v, f->v, g->v);
43662306a36Sopenharmony_ci}
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_cistatic __always_inline void
43962306a36Sopenharmony_cife_mul_tll(fe *h, const fe_loose *f, const fe_loose *g)
44062306a36Sopenharmony_ci{
44162306a36Sopenharmony_ci	fe_mul_impl(h->v, f->v, g->v);
44262306a36Sopenharmony_ci}
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_cistatic noinline void fe_sqr_impl(u32 out[10], const u32 in1[10])
44562306a36Sopenharmony_ci{
44662306a36Sopenharmony_ci	{ const u32 x17 = in1[9];
44762306a36Sopenharmony_ci	{ const u32 x18 = in1[8];
44862306a36Sopenharmony_ci	{ const u32 x16 = in1[7];
44962306a36Sopenharmony_ci	{ const u32 x14 = in1[6];
45062306a36Sopenharmony_ci	{ const u32 x12 = in1[5];
45162306a36Sopenharmony_ci	{ const u32 x10 = in1[4];
45262306a36Sopenharmony_ci	{ const u32 x8 = in1[3];
45362306a36Sopenharmony_ci	{ const u32 x6 = in1[2];
45462306a36Sopenharmony_ci	{ const u32 x4 = in1[1];
45562306a36Sopenharmony_ci	{ const u32 x2 = in1[0];
45662306a36Sopenharmony_ci	{ u64 x19 = ((u64)x2 * x2);
45762306a36Sopenharmony_ci	{ u64 x20 = ((u64)(0x2 * x2) * x4);
45862306a36Sopenharmony_ci	{ u64 x21 = (0x2 * (((u64)x4 * x4) + ((u64)x2 * x6)));
45962306a36Sopenharmony_ci	{ u64 x22 = (0x2 * (((u64)x4 * x6) + ((u64)x2 * x8)));
46062306a36Sopenharmony_ci	{ u64 x23 = ((((u64)x6 * x6) + ((u64)(0x4 * x4) * x8)) + ((u64)(0x2 * x2) * x10));
46162306a36Sopenharmony_ci	{ u64 x24 = (0x2 * ((((u64)x6 * x8) + ((u64)x4 * x10)) + ((u64)x2 * x12)));
46262306a36Sopenharmony_ci	{ u64 x25 = (0x2 * (((((u64)x8 * x8) + ((u64)x6 * x10)) + ((u64)x2 * x14)) + ((u64)(0x2 * x4) * x12)));
46362306a36Sopenharmony_ci	{ u64 x26 = (0x2 * (((((u64)x8 * x10) + ((u64)x6 * x12)) + ((u64)x4 * x14)) + ((u64)x2 * x16)));
46462306a36Sopenharmony_ci	{ u64 x27 = (((u64)x10 * x10) + (0x2 * ((((u64)x6 * x14) + ((u64)x2 * x18)) + (0x2 * (((u64)x4 * x16) + ((u64)x8 * x12))))));
46562306a36Sopenharmony_ci	{ u64 x28 = (0x2 * ((((((u64)x10 * x12) + ((u64)x8 * x14)) + ((u64)x6 * x16)) + ((u64)x4 * x18)) + ((u64)x2 * x17)));
46662306a36Sopenharmony_ci	{ u64 x29 = (0x2 * (((((u64)x12 * x12) + ((u64)x10 * x14)) + ((u64)x6 * x18)) + (0x2 * (((u64)x8 * x16) + ((u64)x4 * x17)))));
46762306a36Sopenharmony_ci	{ u64 x30 = (0x2 * (((((u64)x12 * x14) + ((u64)x10 * x16)) + ((u64)x8 * x18)) + ((u64)x6 * x17)));
46862306a36Sopenharmony_ci	{ u64 x31 = (((u64)x14 * x14) + (0x2 * (((u64)x10 * x18) + (0x2 * (((u64)x12 * x16) + ((u64)x8 * x17))))));
46962306a36Sopenharmony_ci	{ u64 x32 = (0x2 * ((((u64)x14 * x16) + ((u64)x12 * x18)) + ((u64)x10 * x17)));
47062306a36Sopenharmony_ci	{ u64 x33 = (0x2 * ((((u64)x16 * x16) + ((u64)x14 * x18)) + ((u64)(0x2 * x12) * x17)));
47162306a36Sopenharmony_ci	{ u64 x34 = (0x2 * (((u64)x16 * x18) + ((u64)x14 * x17)));
47262306a36Sopenharmony_ci	{ u64 x35 = (((u64)x18 * x18) + ((u64)(0x4 * x16) * x17));
47362306a36Sopenharmony_ci	{ u64 x36 = ((u64)(0x2 * x18) * x17);
47462306a36Sopenharmony_ci	{ u64 x37 = ((u64)(0x2 * x17) * x17);
47562306a36Sopenharmony_ci	{ u64 x38 = (x27 + (x37 << 0x4));
47662306a36Sopenharmony_ci	{ u64 x39 = (x38 + (x37 << 0x1));
47762306a36Sopenharmony_ci	{ u64 x40 = (x39 + x37);
47862306a36Sopenharmony_ci	{ u64 x41 = (x26 + (x36 << 0x4));
47962306a36Sopenharmony_ci	{ u64 x42 = (x41 + (x36 << 0x1));
48062306a36Sopenharmony_ci	{ u64 x43 = (x42 + x36);
48162306a36Sopenharmony_ci	{ u64 x44 = (x25 + (x35 << 0x4));
48262306a36Sopenharmony_ci	{ u64 x45 = (x44 + (x35 << 0x1));
48362306a36Sopenharmony_ci	{ u64 x46 = (x45 + x35);
48462306a36Sopenharmony_ci	{ u64 x47 = (x24 + (x34 << 0x4));
48562306a36Sopenharmony_ci	{ u64 x48 = (x47 + (x34 << 0x1));
48662306a36Sopenharmony_ci	{ u64 x49 = (x48 + x34);
48762306a36Sopenharmony_ci	{ u64 x50 = (x23 + (x33 << 0x4));
48862306a36Sopenharmony_ci	{ u64 x51 = (x50 + (x33 << 0x1));
48962306a36Sopenharmony_ci	{ u64 x52 = (x51 + x33);
49062306a36Sopenharmony_ci	{ u64 x53 = (x22 + (x32 << 0x4));
49162306a36Sopenharmony_ci	{ u64 x54 = (x53 + (x32 << 0x1));
49262306a36Sopenharmony_ci	{ u64 x55 = (x54 + x32);
49362306a36Sopenharmony_ci	{ u64 x56 = (x21 + (x31 << 0x4));
49462306a36Sopenharmony_ci	{ u64 x57 = (x56 + (x31 << 0x1));
49562306a36Sopenharmony_ci	{ u64 x58 = (x57 + x31);
49662306a36Sopenharmony_ci	{ u64 x59 = (x20 + (x30 << 0x4));
49762306a36Sopenharmony_ci	{ u64 x60 = (x59 + (x30 << 0x1));
49862306a36Sopenharmony_ci	{ u64 x61 = (x60 + x30);
49962306a36Sopenharmony_ci	{ u64 x62 = (x19 + (x29 << 0x4));
50062306a36Sopenharmony_ci	{ u64 x63 = (x62 + (x29 << 0x1));
50162306a36Sopenharmony_ci	{ u64 x64 = (x63 + x29);
50262306a36Sopenharmony_ci	{ u64 x65 = (x64 >> 0x1a);
50362306a36Sopenharmony_ci	{ u32 x66 = ((u32)x64 & 0x3ffffff);
50462306a36Sopenharmony_ci	{ u64 x67 = (x65 + x61);
50562306a36Sopenharmony_ci	{ u64 x68 = (x67 >> 0x19);
50662306a36Sopenharmony_ci	{ u32 x69 = ((u32)x67 & 0x1ffffff);
50762306a36Sopenharmony_ci	{ u64 x70 = (x68 + x58);
50862306a36Sopenharmony_ci	{ u64 x71 = (x70 >> 0x1a);
50962306a36Sopenharmony_ci	{ u32 x72 = ((u32)x70 & 0x3ffffff);
51062306a36Sopenharmony_ci	{ u64 x73 = (x71 + x55);
51162306a36Sopenharmony_ci	{ u64 x74 = (x73 >> 0x19);
51262306a36Sopenharmony_ci	{ u32 x75 = ((u32)x73 & 0x1ffffff);
51362306a36Sopenharmony_ci	{ u64 x76 = (x74 + x52);
51462306a36Sopenharmony_ci	{ u64 x77 = (x76 >> 0x1a);
51562306a36Sopenharmony_ci	{ u32 x78 = ((u32)x76 & 0x3ffffff);
51662306a36Sopenharmony_ci	{ u64 x79 = (x77 + x49);
51762306a36Sopenharmony_ci	{ u64 x80 = (x79 >> 0x19);
51862306a36Sopenharmony_ci	{ u32 x81 = ((u32)x79 & 0x1ffffff);
51962306a36Sopenharmony_ci	{ u64 x82 = (x80 + x46);
52062306a36Sopenharmony_ci	{ u64 x83 = (x82 >> 0x1a);
52162306a36Sopenharmony_ci	{ u32 x84 = ((u32)x82 & 0x3ffffff);
52262306a36Sopenharmony_ci	{ u64 x85 = (x83 + x43);
52362306a36Sopenharmony_ci	{ u64 x86 = (x85 >> 0x19);
52462306a36Sopenharmony_ci	{ u32 x87 = ((u32)x85 & 0x1ffffff);
52562306a36Sopenharmony_ci	{ u64 x88 = (x86 + x40);
52662306a36Sopenharmony_ci	{ u64 x89 = (x88 >> 0x1a);
52762306a36Sopenharmony_ci	{ u32 x90 = ((u32)x88 & 0x3ffffff);
52862306a36Sopenharmony_ci	{ u64 x91 = (x89 + x28);
52962306a36Sopenharmony_ci	{ u64 x92 = (x91 >> 0x19);
53062306a36Sopenharmony_ci	{ u32 x93 = ((u32)x91 & 0x1ffffff);
53162306a36Sopenharmony_ci	{ u64 x94 = (x66 + (0x13 * x92));
53262306a36Sopenharmony_ci	{ u32 x95 = (u32) (x94 >> 0x1a);
53362306a36Sopenharmony_ci	{ u32 x96 = ((u32)x94 & 0x3ffffff);
53462306a36Sopenharmony_ci	{ u32 x97 = (x95 + x69);
53562306a36Sopenharmony_ci	{ u32 x98 = (x97 >> 0x19);
53662306a36Sopenharmony_ci	{ u32 x99 = (x97 & 0x1ffffff);
53762306a36Sopenharmony_ci	out[0] = x96;
53862306a36Sopenharmony_ci	out[1] = x99;
53962306a36Sopenharmony_ci	out[2] = (x98 + x72);
54062306a36Sopenharmony_ci	out[3] = x75;
54162306a36Sopenharmony_ci	out[4] = x78;
54262306a36Sopenharmony_ci	out[5] = x81;
54362306a36Sopenharmony_ci	out[6] = x84;
54462306a36Sopenharmony_ci	out[7] = x87;
54562306a36Sopenharmony_ci	out[8] = x90;
54662306a36Sopenharmony_ci	out[9] = x93;
54762306a36Sopenharmony_ci	}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
54862306a36Sopenharmony_ci}
54962306a36Sopenharmony_ci
55062306a36Sopenharmony_cistatic __always_inline void fe_sq_tl(fe *h, const fe_loose *f)
55162306a36Sopenharmony_ci{
55262306a36Sopenharmony_ci	fe_sqr_impl(h->v, f->v);
55362306a36Sopenharmony_ci}
55462306a36Sopenharmony_ci
55562306a36Sopenharmony_cistatic __always_inline void fe_sq_tt(fe *h, const fe *f)
55662306a36Sopenharmony_ci{
55762306a36Sopenharmony_ci	fe_sqr_impl(h->v, f->v);
55862306a36Sopenharmony_ci}
55962306a36Sopenharmony_ci
56062306a36Sopenharmony_cistatic __always_inline void fe_loose_invert(fe *out, const fe_loose *z)
56162306a36Sopenharmony_ci{
56262306a36Sopenharmony_ci	fe t0;
56362306a36Sopenharmony_ci	fe t1;
56462306a36Sopenharmony_ci	fe t2;
56562306a36Sopenharmony_ci	fe t3;
56662306a36Sopenharmony_ci	int i;
56762306a36Sopenharmony_ci
56862306a36Sopenharmony_ci	fe_sq_tl(&t0, z);
56962306a36Sopenharmony_ci	fe_sq_tt(&t1, &t0);
57062306a36Sopenharmony_ci	for (i = 1; i < 2; ++i)
57162306a36Sopenharmony_ci		fe_sq_tt(&t1, &t1);
57262306a36Sopenharmony_ci	fe_mul_tlt(&t1, z, &t1);
57362306a36Sopenharmony_ci	fe_mul_ttt(&t0, &t0, &t1);
57462306a36Sopenharmony_ci	fe_sq_tt(&t2, &t0);
57562306a36Sopenharmony_ci	fe_mul_ttt(&t1, &t1, &t2);
57662306a36Sopenharmony_ci	fe_sq_tt(&t2, &t1);
57762306a36Sopenharmony_ci	for (i = 1; i < 5; ++i)
57862306a36Sopenharmony_ci		fe_sq_tt(&t2, &t2);
57962306a36Sopenharmony_ci	fe_mul_ttt(&t1, &t2, &t1);
58062306a36Sopenharmony_ci	fe_sq_tt(&t2, &t1);
58162306a36Sopenharmony_ci	for (i = 1; i < 10; ++i)
58262306a36Sopenharmony_ci		fe_sq_tt(&t2, &t2);
58362306a36Sopenharmony_ci	fe_mul_ttt(&t2, &t2, &t1);
58462306a36Sopenharmony_ci	fe_sq_tt(&t3, &t2);
58562306a36Sopenharmony_ci	for (i = 1; i < 20; ++i)
58662306a36Sopenharmony_ci		fe_sq_tt(&t3, &t3);
58762306a36Sopenharmony_ci	fe_mul_ttt(&t2, &t3, &t2);
58862306a36Sopenharmony_ci	fe_sq_tt(&t2, &t2);
58962306a36Sopenharmony_ci	for (i = 1; i < 10; ++i)
59062306a36Sopenharmony_ci		fe_sq_tt(&t2, &t2);
59162306a36Sopenharmony_ci	fe_mul_ttt(&t1, &t2, &t1);
59262306a36Sopenharmony_ci	fe_sq_tt(&t2, &t1);
59362306a36Sopenharmony_ci	for (i = 1; i < 50; ++i)
59462306a36Sopenharmony_ci		fe_sq_tt(&t2, &t2);
59562306a36Sopenharmony_ci	fe_mul_ttt(&t2, &t2, &t1);
59662306a36Sopenharmony_ci	fe_sq_tt(&t3, &t2);
59762306a36Sopenharmony_ci	for (i = 1; i < 100; ++i)
59862306a36Sopenharmony_ci		fe_sq_tt(&t3, &t3);
59962306a36Sopenharmony_ci	fe_mul_ttt(&t2, &t3, &t2);
60062306a36Sopenharmony_ci	fe_sq_tt(&t2, &t2);
60162306a36Sopenharmony_ci	for (i = 1; i < 50; ++i)
60262306a36Sopenharmony_ci		fe_sq_tt(&t2, &t2);
60362306a36Sopenharmony_ci	fe_mul_ttt(&t1, &t2, &t1);
60462306a36Sopenharmony_ci	fe_sq_tt(&t1, &t1);
60562306a36Sopenharmony_ci	for (i = 1; i < 5; ++i)
60662306a36Sopenharmony_ci		fe_sq_tt(&t1, &t1);
60762306a36Sopenharmony_ci	fe_mul_ttt(out, &t1, &t0);
60862306a36Sopenharmony_ci}
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_cistatic __always_inline void fe_invert(fe *out, const fe *z)
61162306a36Sopenharmony_ci{
61262306a36Sopenharmony_ci	fe_loose l;
61362306a36Sopenharmony_ci	fe_copy_lt(&l, z);
61462306a36Sopenharmony_ci	fe_loose_invert(out, &l);
61562306a36Sopenharmony_ci}
61662306a36Sopenharmony_ci
61762306a36Sopenharmony_ci/* Replace (f,g) with (g,f) if b == 1;
61862306a36Sopenharmony_ci * replace (f,g) with (f,g) if b == 0.
61962306a36Sopenharmony_ci *
62062306a36Sopenharmony_ci * Preconditions: b in {0,1}
62162306a36Sopenharmony_ci */
62262306a36Sopenharmony_cistatic noinline void fe_cswap(fe *f, fe *g, unsigned int b)
62362306a36Sopenharmony_ci{
62462306a36Sopenharmony_ci	unsigned i;
62562306a36Sopenharmony_ci	b = 0 - b;
62662306a36Sopenharmony_ci	for (i = 0; i < 10; i++) {
62762306a36Sopenharmony_ci		u32 x = f->v[i] ^ g->v[i];
62862306a36Sopenharmony_ci		x &= b;
62962306a36Sopenharmony_ci		f->v[i] ^= x;
63062306a36Sopenharmony_ci		g->v[i] ^= x;
63162306a36Sopenharmony_ci	}
63262306a36Sopenharmony_ci}
63362306a36Sopenharmony_ci
63462306a36Sopenharmony_ci/* NOTE: based on fiat-crypto fe_mul, edited for in2=121666, 0, 0.*/
63562306a36Sopenharmony_cistatic __always_inline void fe_mul_121666_impl(u32 out[10], const u32 in1[10])
63662306a36Sopenharmony_ci{
63762306a36Sopenharmony_ci	{ const u32 x20 = in1[9];
63862306a36Sopenharmony_ci	{ const u32 x21 = in1[8];
63962306a36Sopenharmony_ci	{ const u32 x19 = in1[7];
64062306a36Sopenharmony_ci	{ const u32 x17 = in1[6];
64162306a36Sopenharmony_ci	{ const u32 x15 = in1[5];
64262306a36Sopenharmony_ci	{ const u32 x13 = in1[4];
64362306a36Sopenharmony_ci	{ const u32 x11 = in1[3];
64462306a36Sopenharmony_ci	{ const u32 x9 = in1[2];
64562306a36Sopenharmony_ci	{ const u32 x7 = in1[1];
64662306a36Sopenharmony_ci	{ const u32 x5 = in1[0];
64762306a36Sopenharmony_ci	{ const u32 x38 = 0;
64862306a36Sopenharmony_ci	{ const u32 x39 = 0;
64962306a36Sopenharmony_ci	{ const u32 x37 = 0;
65062306a36Sopenharmony_ci	{ const u32 x35 = 0;
65162306a36Sopenharmony_ci	{ const u32 x33 = 0;
65262306a36Sopenharmony_ci	{ const u32 x31 = 0;
65362306a36Sopenharmony_ci	{ const u32 x29 = 0;
65462306a36Sopenharmony_ci	{ const u32 x27 = 0;
65562306a36Sopenharmony_ci	{ const u32 x25 = 0;
65662306a36Sopenharmony_ci	{ const u32 x23 = 121666;
65762306a36Sopenharmony_ci	{ u64 x40 = ((u64)x23 * x5);
65862306a36Sopenharmony_ci	{ u64 x41 = (((u64)x23 * x7) + ((u64)x25 * x5));
65962306a36Sopenharmony_ci	{ u64 x42 = ((((u64)(0x2 * x25) * x7) + ((u64)x23 * x9)) + ((u64)x27 * x5));
66062306a36Sopenharmony_ci	{ u64 x43 = (((((u64)x25 * x9) + ((u64)x27 * x7)) + ((u64)x23 * x11)) + ((u64)x29 * x5));
66162306a36Sopenharmony_ci	{ u64 x44 = (((((u64)x27 * x9) + (0x2 * (((u64)x25 * x11) + ((u64)x29 * x7)))) + ((u64)x23 * x13)) + ((u64)x31 * x5));
66262306a36Sopenharmony_ci	{ u64 x45 = (((((((u64)x27 * x11) + ((u64)x29 * x9)) + ((u64)x25 * x13)) + ((u64)x31 * x7)) + ((u64)x23 * x15)) + ((u64)x33 * x5));
66362306a36Sopenharmony_ci	{ u64 x46 = (((((0x2 * ((((u64)x29 * x11) + ((u64)x25 * x15)) + ((u64)x33 * x7))) + ((u64)x27 * x13)) + ((u64)x31 * x9)) + ((u64)x23 * x17)) + ((u64)x35 * x5));
66462306a36Sopenharmony_ci	{ 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));
66562306a36Sopenharmony_ci	{ 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));
66662306a36Sopenharmony_ci	{ 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));
66762306a36Sopenharmony_ci	{ 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));
66862306a36Sopenharmony_ci	{ 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));
66962306a36Sopenharmony_ci	{ u64 x52 = (((((u64)x35 * x17) + (0x2 * (((((u64)x33 * x19) + ((u64)x37 * x15)) + ((u64)x29 * x20)) + ((u64)x38 * x11)))) + ((u64)x31 * x21)) + ((u64)x39 * x13));
67062306a36Sopenharmony_ci	{ u64 x53 = (((((((u64)x35 * x19) + ((u64)x37 * x17)) + ((u64)x33 * x21)) + ((u64)x39 * x15)) + ((u64)x31 * x20)) + ((u64)x38 * x13));
67162306a36Sopenharmony_ci	{ u64 x54 = (((0x2 * ((((u64)x37 * x19) + ((u64)x33 * x20)) + ((u64)x38 * x15))) + ((u64)x35 * x21)) + ((u64)x39 * x17));
67262306a36Sopenharmony_ci	{ u64 x55 = (((((u64)x37 * x21) + ((u64)x39 * x19)) + ((u64)x35 * x20)) + ((u64)x38 * x17));
67362306a36Sopenharmony_ci	{ u64 x56 = (((u64)x39 * x21) + (0x2 * (((u64)x37 * x20) + ((u64)x38 * x19))));
67462306a36Sopenharmony_ci	{ u64 x57 = (((u64)x39 * x20) + ((u64)x38 * x21));
67562306a36Sopenharmony_ci	{ u64 x58 = ((u64)(0x2 * x38) * x20);
67662306a36Sopenharmony_ci	{ u64 x59 = (x48 + (x58 << 0x4));
67762306a36Sopenharmony_ci	{ u64 x60 = (x59 + (x58 << 0x1));
67862306a36Sopenharmony_ci	{ u64 x61 = (x60 + x58);
67962306a36Sopenharmony_ci	{ u64 x62 = (x47 + (x57 << 0x4));
68062306a36Sopenharmony_ci	{ u64 x63 = (x62 + (x57 << 0x1));
68162306a36Sopenharmony_ci	{ u64 x64 = (x63 + x57);
68262306a36Sopenharmony_ci	{ u64 x65 = (x46 + (x56 << 0x4));
68362306a36Sopenharmony_ci	{ u64 x66 = (x65 + (x56 << 0x1));
68462306a36Sopenharmony_ci	{ u64 x67 = (x66 + x56);
68562306a36Sopenharmony_ci	{ u64 x68 = (x45 + (x55 << 0x4));
68662306a36Sopenharmony_ci	{ u64 x69 = (x68 + (x55 << 0x1));
68762306a36Sopenharmony_ci	{ u64 x70 = (x69 + x55);
68862306a36Sopenharmony_ci	{ u64 x71 = (x44 + (x54 << 0x4));
68962306a36Sopenharmony_ci	{ u64 x72 = (x71 + (x54 << 0x1));
69062306a36Sopenharmony_ci	{ u64 x73 = (x72 + x54);
69162306a36Sopenharmony_ci	{ u64 x74 = (x43 + (x53 << 0x4));
69262306a36Sopenharmony_ci	{ u64 x75 = (x74 + (x53 << 0x1));
69362306a36Sopenharmony_ci	{ u64 x76 = (x75 + x53);
69462306a36Sopenharmony_ci	{ u64 x77 = (x42 + (x52 << 0x4));
69562306a36Sopenharmony_ci	{ u64 x78 = (x77 + (x52 << 0x1));
69662306a36Sopenharmony_ci	{ u64 x79 = (x78 + x52);
69762306a36Sopenharmony_ci	{ u64 x80 = (x41 + (x51 << 0x4));
69862306a36Sopenharmony_ci	{ u64 x81 = (x80 + (x51 << 0x1));
69962306a36Sopenharmony_ci	{ u64 x82 = (x81 + x51);
70062306a36Sopenharmony_ci	{ u64 x83 = (x40 + (x50 << 0x4));
70162306a36Sopenharmony_ci	{ u64 x84 = (x83 + (x50 << 0x1));
70262306a36Sopenharmony_ci	{ u64 x85 = (x84 + x50);
70362306a36Sopenharmony_ci	{ u64 x86 = (x85 >> 0x1a);
70462306a36Sopenharmony_ci	{ u32 x87 = ((u32)x85 & 0x3ffffff);
70562306a36Sopenharmony_ci	{ u64 x88 = (x86 + x82);
70662306a36Sopenharmony_ci	{ u64 x89 = (x88 >> 0x19);
70762306a36Sopenharmony_ci	{ u32 x90 = ((u32)x88 & 0x1ffffff);
70862306a36Sopenharmony_ci	{ u64 x91 = (x89 + x79);
70962306a36Sopenharmony_ci	{ u64 x92 = (x91 >> 0x1a);
71062306a36Sopenharmony_ci	{ u32 x93 = ((u32)x91 & 0x3ffffff);
71162306a36Sopenharmony_ci	{ u64 x94 = (x92 + x76);
71262306a36Sopenharmony_ci	{ u64 x95 = (x94 >> 0x19);
71362306a36Sopenharmony_ci	{ u32 x96 = ((u32)x94 & 0x1ffffff);
71462306a36Sopenharmony_ci	{ u64 x97 = (x95 + x73);
71562306a36Sopenharmony_ci	{ u64 x98 = (x97 >> 0x1a);
71662306a36Sopenharmony_ci	{ u32 x99 = ((u32)x97 & 0x3ffffff);
71762306a36Sopenharmony_ci	{ u64 x100 = (x98 + x70);
71862306a36Sopenharmony_ci	{ u64 x101 = (x100 >> 0x19);
71962306a36Sopenharmony_ci	{ u32 x102 = ((u32)x100 & 0x1ffffff);
72062306a36Sopenharmony_ci	{ u64 x103 = (x101 + x67);
72162306a36Sopenharmony_ci	{ u64 x104 = (x103 >> 0x1a);
72262306a36Sopenharmony_ci	{ u32 x105 = ((u32)x103 & 0x3ffffff);
72362306a36Sopenharmony_ci	{ u64 x106 = (x104 + x64);
72462306a36Sopenharmony_ci	{ u64 x107 = (x106 >> 0x19);
72562306a36Sopenharmony_ci	{ u32 x108 = ((u32)x106 & 0x1ffffff);
72662306a36Sopenharmony_ci	{ u64 x109 = (x107 + x61);
72762306a36Sopenharmony_ci	{ u64 x110 = (x109 >> 0x1a);
72862306a36Sopenharmony_ci	{ u32 x111 = ((u32)x109 & 0x3ffffff);
72962306a36Sopenharmony_ci	{ u64 x112 = (x110 + x49);
73062306a36Sopenharmony_ci	{ u64 x113 = (x112 >> 0x19);
73162306a36Sopenharmony_ci	{ u32 x114 = ((u32)x112 & 0x1ffffff);
73262306a36Sopenharmony_ci	{ u64 x115 = (x87 + (0x13 * x113));
73362306a36Sopenharmony_ci	{ u32 x116 = (u32) (x115 >> 0x1a);
73462306a36Sopenharmony_ci	{ u32 x117 = ((u32)x115 & 0x3ffffff);
73562306a36Sopenharmony_ci	{ u32 x118 = (x116 + x90);
73662306a36Sopenharmony_ci	{ u32 x119 = (x118 >> 0x19);
73762306a36Sopenharmony_ci	{ u32 x120 = (x118 & 0x1ffffff);
73862306a36Sopenharmony_ci	out[0] = x117;
73962306a36Sopenharmony_ci	out[1] = x120;
74062306a36Sopenharmony_ci	out[2] = (x119 + x93);
74162306a36Sopenharmony_ci	out[3] = x96;
74262306a36Sopenharmony_ci	out[4] = x99;
74362306a36Sopenharmony_ci	out[5] = x102;
74462306a36Sopenharmony_ci	out[6] = x105;
74562306a36Sopenharmony_ci	out[7] = x108;
74662306a36Sopenharmony_ci	out[8] = x111;
74762306a36Sopenharmony_ci	out[9] = x114;
74862306a36Sopenharmony_ci	}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
74962306a36Sopenharmony_ci}
75062306a36Sopenharmony_ci
75162306a36Sopenharmony_cistatic __always_inline void fe_mul121666(fe *h, const fe_loose *f)
75262306a36Sopenharmony_ci{
75362306a36Sopenharmony_ci	fe_mul_121666_impl(h->v, f->v);
75462306a36Sopenharmony_ci}
75562306a36Sopenharmony_ci
75662306a36Sopenharmony_civoid curve25519_generic(u8 out[CURVE25519_KEY_SIZE],
75762306a36Sopenharmony_ci			const u8 scalar[CURVE25519_KEY_SIZE],
75862306a36Sopenharmony_ci			const u8 point[CURVE25519_KEY_SIZE])
75962306a36Sopenharmony_ci{
76062306a36Sopenharmony_ci	fe x1, x2, z2, x3, z3;
76162306a36Sopenharmony_ci	fe_loose x2l, z2l, x3l;
76262306a36Sopenharmony_ci	unsigned swap = 0;
76362306a36Sopenharmony_ci	int pos;
76462306a36Sopenharmony_ci	u8 e[32];
76562306a36Sopenharmony_ci
76662306a36Sopenharmony_ci	memcpy(e, scalar, 32);
76762306a36Sopenharmony_ci	curve25519_clamp_secret(e);
76862306a36Sopenharmony_ci
76962306a36Sopenharmony_ci	/* The following implementation was transcribed to Coq and proven to
77062306a36Sopenharmony_ci	 * correspond to unary scalar multiplication in affine coordinates given
77162306a36Sopenharmony_ci	 * that x1 != 0 is the x coordinate of some point on the curve. It was
77262306a36Sopenharmony_ci	 * also checked in Coq that doing a ladderstep with x1 = x3 = 0 gives
77362306a36Sopenharmony_ci	 * z2' = z3' = 0, and z2 = z3 = 0 gives z2' = z3' = 0. The statement was
77462306a36Sopenharmony_ci	 * quantified over the underlying field, so it applies to Curve25519
77562306a36Sopenharmony_ci	 * itself and the quadratic twist of Curve25519. It was not proven in
77662306a36Sopenharmony_ci	 * Coq that prime-field arithmetic correctly simulates extension-field
77762306a36Sopenharmony_ci	 * arithmetic on prime-field values. The decoding of the byte array
77862306a36Sopenharmony_ci	 * representation of e was not considered.
77962306a36Sopenharmony_ci	 *
78062306a36Sopenharmony_ci	 * Specification of Montgomery curves in affine coordinates:
78162306a36Sopenharmony_ci	 * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Spec/MontgomeryCurve.v#L27>
78262306a36Sopenharmony_ci	 *
78362306a36Sopenharmony_ci	 * Proof that these form a group that is isomorphic to a Weierstrass
78462306a36Sopenharmony_ci	 * curve:
78562306a36Sopenharmony_ci	 * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/AffineProofs.v#L35>
78662306a36Sopenharmony_ci	 *
78762306a36Sopenharmony_ci	 * Coq transcription and correctness proof of the loop
78862306a36Sopenharmony_ci	 * (where scalarbits=255):
78962306a36Sopenharmony_ci	 * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZ.v#L118>
79062306a36Sopenharmony_ci	 * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L278>
79162306a36Sopenharmony_ci	 * preconditions: 0 <= e < 2^255 (not necessarily e < order),
79262306a36Sopenharmony_ci	 * fe_invert(0) = 0
79362306a36Sopenharmony_ci	 */
79462306a36Sopenharmony_ci	fe_frombytes(&x1, point);
79562306a36Sopenharmony_ci	fe_1(&x2);
79662306a36Sopenharmony_ci	fe_0(&z2);
79762306a36Sopenharmony_ci	fe_copy(&x3, &x1);
79862306a36Sopenharmony_ci	fe_1(&z3);
79962306a36Sopenharmony_ci
80062306a36Sopenharmony_ci	for (pos = 254; pos >= 0; --pos) {
80162306a36Sopenharmony_ci		fe tmp0, tmp1;
80262306a36Sopenharmony_ci		fe_loose tmp0l, tmp1l;
80362306a36Sopenharmony_ci		/* loop invariant as of right before the test, for the case
80462306a36Sopenharmony_ci		 * where x1 != 0:
80562306a36Sopenharmony_ci		 *   pos >= -1; if z2 = 0 then x2 is nonzero; if z3 = 0 then x3
80662306a36Sopenharmony_ci		 *   is nonzero
80762306a36Sopenharmony_ci		 *   let r := e >> (pos+1) in the following equalities of
80862306a36Sopenharmony_ci		 *   projective points:
80962306a36Sopenharmony_ci		 *   to_xz (r*P)     === if swap then (x3, z3) else (x2, z2)
81062306a36Sopenharmony_ci		 *   to_xz ((r+1)*P) === if swap then (x2, z2) else (x3, z3)
81162306a36Sopenharmony_ci		 *   x1 is the nonzero x coordinate of the nonzero
81262306a36Sopenharmony_ci		 *   point (r*P-(r+1)*P)
81362306a36Sopenharmony_ci		 */
81462306a36Sopenharmony_ci		unsigned b = 1 & (e[pos / 8] >> (pos & 7));
81562306a36Sopenharmony_ci		swap ^= b;
81662306a36Sopenharmony_ci		fe_cswap(&x2, &x3, swap);
81762306a36Sopenharmony_ci		fe_cswap(&z2, &z3, swap);
81862306a36Sopenharmony_ci		swap = b;
81962306a36Sopenharmony_ci		/* Coq transcription of ladderstep formula (called from
82062306a36Sopenharmony_ci		 * transcribed loop):
82162306a36Sopenharmony_ci		 * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZ.v#L89>
82262306a36Sopenharmony_ci		 * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L131>
82362306a36Sopenharmony_ci		 * x1 != 0 <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L217>
82462306a36Sopenharmony_ci		 * x1  = 0 <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L147>
82562306a36Sopenharmony_ci		 */
82662306a36Sopenharmony_ci		fe_sub(&tmp0l, &x3, &z3);
82762306a36Sopenharmony_ci		fe_sub(&tmp1l, &x2, &z2);
82862306a36Sopenharmony_ci		fe_add(&x2l, &x2, &z2);
82962306a36Sopenharmony_ci		fe_add(&z2l, &x3, &z3);
83062306a36Sopenharmony_ci		fe_mul_tll(&z3, &tmp0l, &x2l);
83162306a36Sopenharmony_ci		fe_mul_tll(&z2, &z2l, &tmp1l);
83262306a36Sopenharmony_ci		fe_sq_tl(&tmp0, &tmp1l);
83362306a36Sopenharmony_ci		fe_sq_tl(&tmp1, &x2l);
83462306a36Sopenharmony_ci		fe_add(&x3l, &z3, &z2);
83562306a36Sopenharmony_ci		fe_sub(&z2l, &z3, &z2);
83662306a36Sopenharmony_ci		fe_mul_ttt(&x2, &tmp1, &tmp0);
83762306a36Sopenharmony_ci		fe_sub(&tmp1l, &tmp1, &tmp0);
83862306a36Sopenharmony_ci		fe_sq_tl(&z2, &z2l);
83962306a36Sopenharmony_ci		fe_mul121666(&z3, &tmp1l);
84062306a36Sopenharmony_ci		fe_sq_tl(&x3, &x3l);
84162306a36Sopenharmony_ci		fe_add(&tmp0l, &tmp0, &z3);
84262306a36Sopenharmony_ci		fe_mul_ttt(&z3, &x1, &z2);
84362306a36Sopenharmony_ci		fe_mul_tll(&z2, &tmp1l, &tmp0l);
84462306a36Sopenharmony_ci	}
84562306a36Sopenharmony_ci	/* here pos=-1, so r=e, so to_xz (e*P) === if swap then (x3, z3)
84662306a36Sopenharmony_ci	 * else (x2, z2)
84762306a36Sopenharmony_ci	 */
84862306a36Sopenharmony_ci	fe_cswap(&x2, &x3, swap);
84962306a36Sopenharmony_ci	fe_cswap(&z2, &z3, swap);
85062306a36Sopenharmony_ci
85162306a36Sopenharmony_ci	fe_invert(&z2, &z2);
85262306a36Sopenharmony_ci	fe_mul_ttt(&x2, &x2, &z2);
85362306a36Sopenharmony_ci	fe_tobytes(out, &x2);
85462306a36Sopenharmony_ci
85562306a36Sopenharmony_ci	memzero_explicit(&x1, sizeof(x1));
85662306a36Sopenharmony_ci	memzero_explicit(&x2, sizeof(x2));
85762306a36Sopenharmony_ci	memzero_explicit(&z2, sizeof(z2));
85862306a36Sopenharmony_ci	memzero_explicit(&x3, sizeof(x3));
85962306a36Sopenharmony_ci	memzero_explicit(&z3, sizeof(z3));
86062306a36Sopenharmony_ci	memzero_explicit(&x2l, sizeof(x2l));
86162306a36Sopenharmony_ci	memzero_explicit(&z2l, sizeof(z2l));
86262306a36Sopenharmony_ci	memzero_explicit(&x3l, sizeof(x3l));
86362306a36Sopenharmony_ci	memzero_explicit(&e, sizeof(e));
86462306a36Sopenharmony_ci}
865