18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 OR MIT 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2015-2016 The fiat-crypto Authors. 48c2ecf20Sopenharmony_ci * Copyright (C) 2018-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * This is a machine-generated formally verified implementation of Curve25519 78c2ecf20Sopenharmony_ci * ECDH from: <https://github.com/mit-plv/fiat-crypto>. Though originally 88c2ecf20Sopenharmony_ci * machine generated, it has been tweaked to be suitable for use in the kernel. 98c2ecf20Sopenharmony_ci * It is optimized for 32-bit machines and machines that cannot work efficiently 108c2ecf20Sopenharmony_ci * with 128-bit integer types. 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <asm/unaligned.h> 148c2ecf20Sopenharmony_ci#include <crypto/curve25519.h> 158c2ecf20Sopenharmony_ci#include <linux/string.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci/* fe means field element. Here the field is \Z/(2^255-19). An element t, 188c2ecf20Sopenharmony_ci * entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77 198c2ecf20Sopenharmony_ci * t[3]+2^102 t[4]+...+2^230 t[9]. 208c2ecf20Sopenharmony_ci * fe limbs are bounded by 1.125*2^26,1.125*2^25,1.125*2^26,1.125*2^25,etc. 218c2ecf20Sopenharmony_ci * Multiplication and carrying produce fe from fe_loose. 228c2ecf20Sopenharmony_ci */ 238c2ecf20Sopenharmony_citypedef struct fe { u32 v[10]; } fe; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci/* fe_loose limbs are bounded by 3.375*2^26,3.375*2^25,3.375*2^26,3.375*2^25,etc 268c2ecf20Sopenharmony_ci * Addition and subtraction produce fe_loose from (fe, fe). 278c2ecf20Sopenharmony_ci */ 288c2ecf20Sopenharmony_citypedef struct fe_loose { u32 v[10]; } fe_loose; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistatic __always_inline void fe_frombytes_impl(u32 h[10], const u8 *s) 318c2ecf20Sopenharmony_ci{ 328c2ecf20Sopenharmony_ci /* Ignores top bit of s. */ 338c2ecf20Sopenharmony_ci u32 a0 = get_unaligned_le32(s); 348c2ecf20Sopenharmony_ci u32 a1 = get_unaligned_le32(s+4); 358c2ecf20Sopenharmony_ci u32 a2 = get_unaligned_le32(s+8); 368c2ecf20Sopenharmony_ci u32 a3 = get_unaligned_le32(s+12); 378c2ecf20Sopenharmony_ci u32 a4 = get_unaligned_le32(s+16); 388c2ecf20Sopenharmony_ci u32 a5 = get_unaligned_le32(s+20); 398c2ecf20Sopenharmony_ci u32 a6 = get_unaligned_le32(s+24); 408c2ecf20Sopenharmony_ci u32 a7 = get_unaligned_le32(s+28); 418c2ecf20Sopenharmony_ci h[0] = a0&((1<<26)-1); /* 26 used, 32-26 left. 26 */ 428c2ecf20Sopenharmony_ci h[1] = (a0>>26) | ((a1&((1<<19)-1))<< 6); /* (32-26) + 19 = 6+19 = 25 */ 438c2ecf20Sopenharmony_ci h[2] = (a1>>19) | ((a2&((1<<13)-1))<<13); /* (32-19) + 13 = 13+13 = 26 */ 448c2ecf20Sopenharmony_ci h[3] = (a2>>13) | ((a3&((1<< 6)-1))<<19); /* (32-13) + 6 = 19+ 6 = 25 */ 458c2ecf20Sopenharmony_ci h[4] = (a3>> 6); /* (32- 6) = 26 */ 468c2ecf20Sopenharmony_ci h[5] = a4&((1<<25)-1); /* 25 */ 478c2ecf20Sopenharmony_ci h[6] = (a4>>25) | ((a5&((1<<19)-1))<< 7); /* (32-25) + 19 = 7+19 = 26 */ 488c2ecf20Sopenharmony_ci h[7] = (a5>>19) | ((a6&((1<<12)-1))<<13); /* (32-19) + 12 = 13+12 = 25 */ 498c2ecf20Sopenharmony_ci h[8] = (a6>>12) | ((a7&((1<< 6)-1))<<20); /* (32-12) + 6 = 20+ 6 = 26 */ 508c2ecf20Sopenharmony_ci h[9] = (a7>> 6)&((1<<25)-1); /* 25 */ 518c2ecf20Sopenharmony_ci} 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistatic __always_inline void fe_frombytes(fe *h, const u8 *s) 548c2ecf20Sopenharmony_ci{ 558c2ecf20Sopenharmony_ci fe_frombytes_impl(h->v, s); 568c2ecf20Sopenharmony_ci} 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistatic __always_inline u8 /*bool*/ 598c2ecf20Sopenharmony_ciaddcarryx_u25(u8 /*bool*/ c, u32 a, u32 b, u32 *low) 608c2ecf20Sopenharmony_ci{ 618c2ecf20Sopenharmony_ci /* This function extracts 25 bits of result and 1 bit of carry 628c2ecf20Sopenharmony_ci * (26 total), so a 32-bit intermediate is sufficient. 638c2ecf20Sopenharmony_ci */ 648c2ecf20Sopenharmony_ci u32 x = a + b + c; 658c2ecf20Sopenharmony_ci *low = x & ((1 << 25) - 1); 668c2ecf20Sopenharmony_ci return (x >> 25) & 1; 678c2ecf20Sopenharmony_ci} 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_cistatic __always_inline u8 /*bool*/ 708c2ecf20Sopenharmony_ciaddcarryx_u26(u8 /*bool*/ c, u32 a, u32 b, u32 *low) 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci /* This function extracts 26 bits of result and 1 bit of carry 738c2ecf20Sopenharmony_ci * (27 total), so a 32-bit intermediate is sufficient. 748c2ecf20Sopenharmony_ci */ 758c2ecf20Sopenharmony_ci u32 x = a + b + c; 768c2ecf20Sopenharmony_ci *low = x & ((1 << 26) - 1); 778c2ecf20Sopenharmony_ci return (x >> 26) & 1; 788c2ecf20Sopenharmony_ci} 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_cistatic __always_inline u8 /*bool*/ 818c2ecf20Sopenharmony_cisubborrow_u25(u8 /*bool*/ c, u32 a, u32 b, u32 *low) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci /* This function extracts 25 bits of result and 1 bit of borrow 848c2ecf20Sopenharmony_ci * (26 total), so a 32-bit intermediate is sufficient. 858c2ecf20Sopenharmony_ci */ 868c2ecf20Sopenharmony_ci u32 x = a - b - c; 878c2ecf20Sopenharmony_ci *low = x & ((1 << 25) - 1); 888c2ecf20Sopenharmony_ci return x >> 31; 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_cistatic __always_inline u8 /*bool*/ 928c2ecf20Sopenharmony_cisubborrow_u26(u8 /*bool*/ c, u32 a, u32 b, u32 *low) 938c2ecf20Sopenharmony_ci{ 948c2ecf20Sopenharmony_ci /* This function extracts 26 bits of result and 1 bit of borrow 958c2ecf20Sopenharmony_ci *(27 total), so a 32-bit intermediate is sufficient. 968c2ecf20Sopenharmony_ci */ 978c2ecf20Sopenharmony_ci u32 x = a - b - c; 988c2ecf20Sopenharmony_ci *low = x & ((1 << 26) - 1); 998c2ecf20Sopenharmony_ci return x >> 31; 1008c2ecf20Sopenharmony_ci} 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_cistatic __always_inline u32 cmovznz32(u32 t, u32 z, u32 nz) 1038c2ecf20Sopenharmony_ci{ 1048c2ecf20Sopenharmony_ci t = -!!t; /* all set if nonzero, 0 if 0 */ 1058c2ecf20Sopenharmony_ci return (t&nz) | ((~t)&z); 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_cistatic __always_inline void fe_freeze(u32 out[10], const u32 in1[10]) 1098c2ecf20Sopenharmony_ci{ 1108c2ecf20Sopenharmony_ci { const u32 x17 = in1[9]; 1118c2ecf20Sopenharmony_ci { const u32 x18 = in1[8]; 1128c2ecf20Sopenharmony_ci { const u32 x16 = in1[7]; 1138c2ecf20Sopenharmony_ci { const u32 x14 = in1[6]; 1148c2ecf20Sopenharmony_ci { const u32 x12 = in1[5]; 1158c2ecf20Sopenharmony_ci { const u32 x10 = in1[4]; 1168c2ecf20Sopenharmony_ci { const u32 x8 = in1[3]; 1178c2ecf20Sopenharmony_ci { const u32 x6 = in1[2]; 1188c2ecf20Sopenharmony_ci { const u32 x4 = in1[1]; 1198c2ecf20Sopenharmony_ci { const u32 x2 = in1[0]; 1208c2ecf20Sopenharmony_ci { u32 x20; u8/*bool*/ x21 = subborrow_u26(0x0, x2, 0x3ffffed, &x20); 1218c2ecf20Sopenharmony_ci { u32 x23; u8/*bool*/ x24 = subborrow_u25(x21, x4, 0x1ffffff, &x23); 1228c2ecf20Sopenharmony_ci { u32 x26; u8/*bool*/ x27 = subborrow_u26(x24, x6, 0x3ffffff, &x26); 1238c2ecf20Sopenharmony_ci { u32 x29; u8/*bool*/ x30 = subborrow_u25(x27, x8, 0x1ffffff, &x29); 1248c2ecf20Sopenharmony_ci { u32 x32; u8/*bool*/ x33 = subborrow_u26(x30, x10, 0x3ffffff, &x32); 1258c2ecf20Sopenharmony_ci { u32 x35; u8/*bool*/ x36 = subborrow_u25(x33, x12, 0x1ffffff, &x35); 1268c2ecf20Sopenharmony_ci { u32 x38; u8/*bool*/ x39 = subborrow_u26(x36, x14, 0x3ffffff, &x38); 1278c2ecf20Sopenharmony_ci { u32 x41; u8/*bool*/ x42 = subborrow_u25(x39, x16, 0x1ffffff, &x41); 1288c2ecf20Sopenharmony_ci { u32 x44; u8/*bool*/ x45 = subborrow_u26(x42, x18, 0x3ffffff, &x44); 1298c2ecf20Sopenharmony_ci { u32 x47; u8/*bool*/ x48 = subborrow_u25(x45, x17, 0x1ffffff, &x47); 1308c2ecf20Sopenharmony_ci { u32 x49 = cmovznz32(x48, 0x0, 0xffffffff); 1318c2ecf20Sopenharmony_ci { u32 x50 = (x49 & 0x3ffffed); 1328c2ecf20Sopenharmony_ci { u32 x52; u8/*bool*/ x53 = addcarryx_u26(0x0, x20, x50, &x52); 1338c2ecf20Sopenharmony_ci { u32 x54 = (x49 & 0x1ffffff); 1348c2ecf20Sopenharmony_ci { u32 x56; u8/*bool*/ x57 = addcarryx_u25(x53, x23, x54, &x56); 1358c2ecf20Sopenharmony_ci { u32 x58 = (x49 & 0x3ffffff); 1368c2ecf20Sopenharmony_ci { u32 x60; u8/*bool*/ x61 = addcarryx_u26(x57, x26, x58, &x60); 1378c2ecf20Sopenharmony_ci { u32 x62 = (x49 & 0x1ffffff); 1388c2ecf20Sopenharmony_ci { u32 x64; u8/*bool*/ x65 = addcarryx_u25(x61, x29, x62, &x64); 1398c2ecf20Sopenharmony_ci { u32 x66 = (x49 & 0x3ffffff); 1408c2ecf20Sopenharmony_ci { u32 x68; u8/*bool*/ x69 = addcarryx_u26(x65, x32, x66, &x68); 1418c2ecf20Sopenharmony_ci { u32 x70 = (x49 & 0x1ffffff); 1428c2ecf20Sopenharmony_ci { u32 x72; u8/*bool*/ x73 = addcarryx_u25(x69, x35, x70, &x72); 1438c2ecf20Sopenharmony_ci { u32 x74 = (x49 & 0x3ffffff); 1448c2ecf20Sopenharmony_ci { u32 x76; u8/*bool*/ x77 = addcarryx_u26(x73, x38, x74, &x76); 1458c2ecf20Sopenharmony_ci { u32 x78 = (x49 & 0x1ffffff); 1468c2ecf20Sopenharmony_ci { u32 x80; u8/*bool*/ x81 = addcarryx_u25(x77, x41, x78, &x80); 1478c2ecf20Sopenharmony_ci { u32 x82 = (x49 & 0x3ffffff); 1488c2ecf20Sopenharmony_ci { u32 x84; u8/*bool*/ x85 = addcarryx_u26(x81, x44, x82, &x84); 1498c2ecf20Sopenharmony_ci { u32 x86 = (x49 & 0x1ffffff); 1508c2ecf20Sopenharmony_ci { u32 x88; addcarryx_u25(x85, x47, x86, &x88); 1518c2ecf20Sopenharmony_ci out[0] = x52; 1528c2ecf20Sopenharmony_ci out[1] = x56; 1538c2ecf20Sopenharmony_ci out[2] = x60; 1548c2ecf20Sopenharmony_ci out[3] = x64; 1558c2ecf20Sopenharmony_ci out[4] = x68; 1568c2ecf20Sopenharmony_ci out[5] = x72; 1578c2ecf20Sopenharmony_ci out[6] = x76; 1588c2ecf20Sopenharmony_ci out[7] = x80; 1598c2ecf20Sopenharmony_ci out[8] = x84; 1608c2ecf20Sopenharmony_ci out[9] = x88; 1618c2ecf20Sopenharmony_ci }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} 1628c2ecf20Sopenharmony_ci} 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_cistatic __always_inline void fe_tobytes(u8 s[32], const fe *f) 1658c2ecf20Sopenharmony_ci{ 1668c2ecf20Sopenharmony_ci u32 h[10]; 1678c2ecf20Sopenharmony_ci fe_freeze(h, f->v); 1688c2ecf20Sopenharmony_ci s[0] = h[0] >> 0; 1698c2ecf20Sopenharmony_ci s[1] = h[0] >> 8; 1708c2ecf20Sopenharmony_ci s[2] = h[0] >> 16; 1718c2ecf20Sopenharmony_ci s[3] = (h[0] >> 24) | (h[1] << 2); 1728c2ecf20Sopenharmony_ci s[4] = h[1] >> 6; 1738c2ecf20Sopenharmony_ci s[5] = h[1] >> 14; 1748c2ecf20Sopenharmony_ci s[6] = (h[1] >> 22) | (h[2] << 3); 1758c2ecf20Sopenharmony_ci s[7] = h[2] >> 5; 1768c2ecf20Sopenharmony_ci s[8] = h[2] >> 13; 1778c2ecf20Sopenharmony_ci s[9] = (h[2] >> 21) | (h[3] << 5); 1788c2ecf20Sopenharmony_ci s[10] = h[3] >> 3; 1798c2ecf20Sopenharmony_ci s[11] = h[3] >> 11; 1808c2ecf20Sopenharmony_ci s[12] = (h[3] >> 19) | (h[4] << 6); 1818c2ecf20Sopenharmony_ci s[13] = h[4] >> 2; 1828c2ecf20Sopenharmony_ci s[14] = h[4] >> 10; 1838c2ecf20Sopenharmony_ci s[15] = h[4] >> 18; 1848c2ecf20Sopenharmony_ci s[16] = h[5] >> 0; 1858c2ecf20Sopenharmony_ci s[17] = h[5] >> 8; 1868c2ecf20Sopenharmony_ci s[18] = h[5] >> 16; 1878c2ecf20Sopenharmony_ci s[19] = (h[5] >> 24) | (h[6] << 1); 1888c2ecf20Sopenharmony_ci s[20] = h[6] >> 7; 1898c2ecf20Sopenharmony_ci s[21] = h[6] >> 15; 1908c2ecf20Sopenharmony_ci s[22] = (h[6] >> 23) | (h[7] << 3); 1918c2ecf20Sopenharmony_ci s[23] = h[7] >> 5; 1928c2ecf20Sopenharmony_ci s[24] = h[7] >> 13; 1938c2ecf20Sopenharmony_ci s[25] = (h[7] >> 21) | (h[8] << 4); 1948c2ecf20Sopenharmony_ci s[26] = h[8] >> 4; 1958c2ecf20Sopenharmony_ci s[27] = h[8] >> 12; 1968c2ecf20Sopenharmony_ci s[28] = (h[8] >> 20) | (h[9] << 6); 1978c2ecf20Sopenharmony_ci s[29] = h[9] >> 2; 1988c2ecf20Sopenharmony_ci s[30] = h[9] >> 10; 1998c2ecf20Sopenharmony_ci s[31] = h[9] >> 18; 2008c2ecf20Sopenharmony_ci} 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci/* h = f */ 2038c2ecf20Sopenharmony_cistatic __always_inline void fe_copy(fe *h, const fe *f) 2048c2ecf20Sopenharmony_ci{ 2058c2ecf20Sopenharmony_ci memmove(h, f, sizeof(u32) * 10); 2068c2ecf20Sopenharmony_ci} 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_cistatic __always_inline void fe_copy_lt(fe_loose *h, const fe *f) 2098c2ecf20Sopenharmony_ci{ 2108c2ecf20Sopenharmony_ci memmove(h, f, sizeof(u32) * 10); 2118c2ecf20Sopenharmony_ci} 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci/* h = 0 */ 2148c2ecf20Sopenharmony_cistatic __always_inline void fe_0(fe *h) 2158c2ecf20Sopenharmony_ci{ 2168c2ecf20Sopenharmony_ci memset(h, 0, sizeof(u32) * 10); 2178c2ecf20Sopenharmony_ci} 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci/* h = 1 */ 2208c2ecf20Sopenharmony_cistatic __always_inline void fe_1(fe *h) 2218c2ecf20Sopenharmony_ci{ 2228c2ecf20Sopenharmony_ci memset(h, 0, sizeof(u32) * 10); 2238c2ecf20Sopenharmony_ci h->v[0] = 1; 2248c2ecf20Sopenharmony_ci} 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_cistatic noinline void fe_add_impl(u32 out[10], const u32 in1[10], const u32 in2[10]) 2278c2ecf20Sopenharmony_ci{ 2288c2ecf20Sopenharmony_ci { const u32 x20 = in1[9]; 2298c2ecf20Sopenharmony_ci { const u32 x21 = in1[8]; 2308c2ecf20Sopenharmony_ci { const u32 x19 = in1[7]; 2318c2ecf20Sopenharmony_ci { const u32 x17 = in1[6]; 2328c2ecf20Sopenharmony_ci { const u32 x15 = in1[5]; 2338c2ecf20Sopenharmony_ci { const u32 x13 = in1[4]; 2348c2ecf20Sopenharmony_ci { const u32 x11 = in1[3]; 2358c2ecf20Sopenharmony_ci { const u32 x9 = in1[2]; 2368c2ecf20Sopenharmony_ci { const u32 x7 = in1[1]; 2378c2ecf20Sopenharmony_ci { const u32 x5 = in1[0]; 2388c2ecf20Sopenharmony_ci { const u32 x38 = in2[9]; 2398c2ecf20Sopenharmony_ci { const u32 x39 = in2[8]; 2408c2ecf20Sopenharmony_ci { const u32 x37 = in2[7]; 2418c2ecf20Sopenharmony_ci { const u32 x35 = in2[6]; 2428c2ecf20Sopenharmony_ci { const u32 x33 = in2[5]; 2438c2ecf20Sopenharmony_ci { const u32 x31 = in2[4]; 2448c2ecf20Sopenharmony_ci { const u32 x29 = in2[3]; 2458c2ecf20Sopenharmony_ci { const u32 x27 = in2[2]; 2468c2ecf20Sopenharmony_ci { const u32 x25 = in2[1]; 2478c2ecf20Sopenharmony_ci { const u32 x23 = in2[0]; 2488c2ecf20Sopenharmony_ci out[0] = (x5 + x23); 2498c2ecf20Sopenharmony_ci out[1] = (x7 + x25); 2508c2ecf20Sopenharmony_ci out[2] = (x9 + x27); 2518c2ecf20Sopenharmony_ci out[3] = (x11 + x29); 2528c2ecf20Sopenharmony_ci out[4] = (x13 + x31); 2538c2ecf20Sopenharmony_ci out[5] = (x15 + x33); 2548c2ecf20Sopenharmony_ci out[6] = (x17 + x35); 2558c2ecf20Sopenharmony_ci out[7] = (x19 + x37); 2568c2ecf20Sopenharmony_ci out[8] = (x21 + x39); 2578c2ecf20Sopenharmony_ci out[9] = (x20 + x38); 2588c2ecf20Sopenharmony_ci }}}}}}}}}}}}}}}}}}}} 2598c2ecf20Sopenharmony_ci} 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci/* h = f + g 2628c2ecf20Sopenharmony_ci * Can overlap h with f or g. 2638c2ecf20Sopenharmony_ci */ 2648c2ecf20Sopenharmony_cistatic __always_inline void fe_add(fe_loose *h, const fe *f, const fe *g) 2658c2ecf20Sopenharmony_ci{ 2668c2ecf20Sopenharmony_ci fe_add_impl(h->v, f->v, g->v); 2678c2ecf20Sopenharmony_ci} 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_cistatic noinline void fe_sub_impl(u32 out[10], const u32 in1[10], const u32 in2[10]) 2708c2ecf20Sopenharmony_ci{ 2718c2ecf20Sopenharmony_ci { const u32 x20 = in1[9]; 2728c2ecf20Sopenharmony_ci { const u32 x21 = in1[8]; 2738c2ecf20Sopenharmony_ci { const u32 x19 = in1[7]; 2748c2ecf20Sopenharmony_ci { const u32 x17 = in1[6]; 2758c2ecf20Sopenharmony_ci { const u32 x15 = in1[5]; 2768c2ecf20Sopenharmony_ci { const u32 x13 = in1[4]; 2778c2ecf20Sopenharmony_ci { const u32 x11 = in1[3]; 2788c2ecf20Sopenharmony_ci { const u32 x9 = in1[2]; 2798c2ecf20Sopenharmony_ci { const u32 x7 = in1[1]; 2808c2ecf20Sopenharmony_ci { const u32 x5 = in1[0]; 2818c2ecf20Sopenharmony_ci { const u32 x38 = in2[9]; 2828c2ecf20Sopenharmony_ci { const u32 x39 = in2[8]; 2838c2ecf20Sopenharmony_ci { const u32 x37 = in2[7]; 2848c2ecf20Sopenharmony_ci { const u32 x35 = in2[6]; 2858c2ecf20Sopenharmony_ci { const u32 x33 = in2[5]; 2868c2ecf20Sopenharmony_ci { const u32 x31 = in2[4]; 2878c2ecf20Sopenharmony_ci { const u32 x29 = in2[3]; 2888c2ecf20Sopenharmony_ci { const u32 x27 = in2[2]; 2898c2ecf20Sopenharmony_ci { const u32 x25 = in2[1]; 2908c2ecf20Sopenharmony_ci { const u32 x23 = in2[0]; 2918c2ecf20Sopenharmony_ci out[0] = ((0x7ffffda + x5) - x23); 2928c2ecf20Sopenharmony_ci out[1] = ((0x3fffffe + x7) - x25); 2938c2ecf20Sopenharmony_ci out[2] = ((0x7fffffe + x9) - x27); 2948c2ecf20Sopenharmony_ci out[3] = ((0x3fffffe + x11) - x29); 2958c2ecf20Sopenharmony_ci out[4] = ((0x7fffffe + x13) - x31); 2968c2ecf20Sopenharmony_ci out[5] = ((0x3fffffe + x15) - x33); 2978c2ecf20Sopenharmony_ci out[6] = ((0x7fffffe + x17) - x35); 2988c2ecf20Sopenharmony_ci out[7] = ((0x3fffffe + x19) - x37); 2998c2ecf20Sopenharmony_ci out[8] = ((0x7fffffe + x21) - x39); 3008c2ecf20Sopenharmony_ci out[9] = ((0x3fffffe + x20) - x38); 3018c2ecf20Sopenharmony_ci }}}}}}}}}}}}}}}}}}}} 3028c2ecf20Sopenharmony_ci} 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ci/* h = f - g 3058c2ecf20Sopenharmony_ci * Can overlap h with f or g. 3068c2ecf20Sopenharmony_ci */ 3078c2ecf20Sopenharmony_cistatic __always_inline void fe_sub(fe_loose *h, const fe *f, const fe *g) 3088c2ecf20Sopenharmony_ci{ 3098c2ecf20Sopenharmony_ci fe_sub_impl(h->v, f->v, g->v); 3108c2ecf20Sopenharmony_ci} 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_cistatic noinline void fe_mul_impl(u32 out[10], const u32 in1[10], const u32 in2[10]) 3138c2ecf20Sopenharmony_ci{ 3148c2ecf20Sopenharmony_ci { const u32 x20 = in1[9]; 3158c2ecf20Sopenharmony_ci { const u32 x21 = in1[8]; 3168c2ecf20Sopenharmony_ci { const u32 x19 = in1[7]; 3178c2ecf20Sopenharmony_ci { const u32 x17 = in1[6]; 3188c2ecf20Sopenharmony_ci { const u32 x15 = in1[5]; 3198c2ecf20Sopenharmony_ci { const u32 x13 = in1[4]; 3208c2ecf20Sopenharmony_ci { const u32 x11 = in1[3]; 3218c2ecf20Sopenharmony_ci { const u32 x9 = in1[2]; 3228c2ecf20Sopenharmony_ci { const u32 x7 = in1[1]; 3238c2ecf20Sopenharmony_ci { const u32 x5 = in1[0]; 3248c2ecf20Sopenharmony_ci { const u32 x38 = in2[9]; 3258c2ecf20Sopenharmony_ci { const u32 x39 = in2[8]; 3268c2ecf20Sopenharmony_ci { const u32 x37 = in2[7]; 3278c2ecf20Sopenharmony_ci { const u32 x35 = in2[6]; 3288c2ecf20Sopenharmony_ci { const u32 x33 = in2[5]; 3298c2ecf20Sopenharmony_ci { const u32 x31 = in2[4]; 3308c2ecf20Sopenharmony_ci { const u32 x29 = in2[3]; 3318c2ecf20Sopenharmony_ci { const u32 x27 = in2[2]; 3328c2ecf20Sopenharmony_ci { const u32 x25 = in2[1]; 3338c2ecf20Sopenharmony_ci { const u32 x23 = in2[0]; 3348c2ecf20Sopenharmony_ci { u64 x40 = ((u64)x23 * x5); 3358c2ecf20Sopenharmony_ci { u64 x41 = (((u64)x23 * x7) + ((u64)x25 * x5)); 3368c2ecf20Sopenharmony_ci { u64 x42 = ((((u64)(0x2 * x25) * x7) + ((u64)x23 * x9)) + ((u64)x27 * x5)); 3378c2ecf20Sopenharmony_ci { u64 x43 = (((((u64)x25 * x9) + ((u64)x27 * x7)) + ((u64)x23 * x11)) + ((u64)x29 * x5)); 3388c2ecf20Sopenharmony_ci { u64 x44 = (((((u64)x27 * x9) + (0x2 * (((u64)x25 * x11) + ((u64)x29 * x7)))) + ((u64)x23 * x13)) + ((u64)x31 * x5)); 3398c2ecf20Sopenharmony_ci { u64 x45 = (((((((u64)x27 * x11) + ((u64)x29 * x9)) + ((u64)x25 * x13)) + ((u64)x31 * x7)) + ((u64)x23 * x15)) + ((u64)x33 * x5)); 3408c2ecf20Sopenharmony_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)); 3418c2ecf20Sopenharmony_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)); 3428c2ecf20Sopenharmony_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)); 3438c2ecf20Sopenharmony_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)); 3448c2ecf20Sopenharmony_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)); 3458c2ecf20Sopenharmony_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)); 3468c2ecf20Sopenharmony_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)); 3478c2ecf20Sopenharmony_ci { u64 x53 = (((((((u64)x35 * x19) + ((u64)x37 * x17)) + ((u64)x33 * x21)) + ((u64)x39 * x15)) + ((u64)x31 * x20)) + ((u64)x38 * x13)); 3488c2ecf20Sopenharmony_ci { u64 x54 = (((0x2 * ((((u64)x37 * x19) + ((u64)x33 * x20)) + ((u64)x38 * x15))) + ((u64)x35 * x21)) + ((u64)x39 * x17)); 3498c2ecf20Sopenharmony_ci { u64 x55 = (((((u64)x37 * x21) + ((u64)x39 * x19)) + ((u64)x35 * x20)) + ((u64)x38 * x17)); 3508c2ecf20Sopenharmony_ci { u64 x56 = (((u64)x39 * x21) + (0x2 * (((u64)x37 * x20) + ((u64)x38 * x19)))); 3518c2ecf20Sopenharmony_ci { u64 x57 = (((u64)x39 * x20) + ((u64)x38 * x21)); 3528c2ecf20Sopenharmony_ci { u64 x58 = ((u64)(0x2 * x38) * x20); 3538c2ecf20Sopenharmony_ci { u64 x59 = (x48 + (x58 << 0x4)); 3548c2ecf20Sopenharmony_ci { u64 x60 = (x59 + (x58 << 0x1)); 3558c2ecf20Sopenharmony_ci { u64 x61 = (x60 + x58); 3568c2ecf20Sopenharmony_ci { u64 x62 = (x47 + (x57 << 0x4)); 3578c2ecf20Sopenharmony_ci { u64 x63 = (x62 + (x57 << 0x1)); 3588c2ecf20Sopenharmony_ci { u64 x64 = (x63 + x57); 3598c2ecf20Sopenharmony_ci { u64 x65 = (x46 + (x56 << 0x4)); 3608c2ecf20Sopenharmony_ci { u64 x66 = (x65 + (x56 << 0x1)); 3618c2ecf20Sopenharmony_ci { u64 x67 = (x66 + x56); 3628c2ecf20Sopenharmony_ci { u64 x68 = (x45 + (x55 << 0x4)); 3638c2ecf20Sopenharmony_ci { u64 x69 = (x68 + (x55 << 0x1)); 3648c2ecf20Sopenharmony_ci { u64 x70 = (x69 + x55); 3658c2ecf20Sopenharmony_ci { u64 x71 = (x44 + (x54 << 0x4)); 3668c2ecf20Sopenharmony_ci { u64 x72 = (x71 + (x54 << 0x1)); 3678c2ecf20Sopenharmony_ci { u64 x73 = (x72 + x54); 3688c2ecf20Sopenharmony_ci { u64 x74 = (x43 + (x53 << 0x4)); 3698c2ecf20Sopenharmony_ci { u64 x75 = (x74 + (x53 << 0x1)); 3708c2ecf20Sopenharmony_ci { u64 x76 = (x75 + x53); 3718c2ecf20Sopenharmony_ci { u64 x77 = (x42 + (x52 << 0x4)); 3728c2ecf20Sopenharmony_ci { u64 x78 = (x77 + (x52 << 0x1)); 3738c2ecf20Sopenharmony_ci { u64 x79 = (x78 + x52); 3748c2ecf20Sopenharmony_ci { u64 x80 = (x41 + (x51 << 0x4)); 3758c2ecf20Sopenharmony_ci { u64 x81 = (x80 + (x51 << 0x1)); 3768c2ecf20Sopenharmony_ci { u64 x82 = (x81 + x51); 3778c2ecf20Sopenharmony_ci { u64 x83 = (x40 + (x50 << 0x4)); 3788c2ecf20Sopenharmony_ci { u64 x84 = (x83 + (x50 << 0x1)); 3798c2ecf20Sopenharmony_ci { u64 x85 = (x84 + x50); 3808c2ecf20Sopenharmony_ci { u64 x86 = (x85 >> 0x1a); 3818c2ecf20Sopenharmony_ci { u32 x87 = ((u32)x85 & 0x3ffffff); 3828c2ecf20Sopenharmony_ci { u64 x88 = (x86 + x82); 3838c2ecf20Sopenharmony_ci { u64 x89 = (x88 >> 0x19); 3848c2ecf20Sopenharmony_ci { u32 x90 = ((u32)x88 & 0x1ffffff); 3858c2ecf20Sopenharmony_ci { u64 x91 = (x89 + x79); 3868c2ecf20Sopenharmony_ci { u64 x92 = (x91 >> 0x1a); 3878c2ecf20Sopenharmony_ci { u32 x93 = ((u32)x91 & 0x3ffffff); 3888c2ecf20Sopenharmony_ci { u64 x94 = (x92 + x76); 3898c2ecf20Sopenharmony_ci { u64 x95 = (x94 >> 0x19); 3908c2ecf20Sopenharmony_ci { u32 x96 = ((u32)x94 & 0x1ffffff); 3918c2ecf20Sopenharmony_ci { u64 x97 = (x95 + x73); 3928c2ecf20Sopenharmony_ci { u64 x98 = (x97 >> 0x1a); 3938c2ecf20Sopenharmony_ci { u32 x99 = ((u32)x97 & 0x3ffffff); 3948c2ecf20Sopenharmony_ci { u64 x100 = (x98 + x70); 3958c2ecf20Sopenharmony_ci { u64 x101 = (x100 >> 0x19); 3968c2ecf20Sopenharmony_ci { u32 x102 = ((u32)x100 & 0x1ffffff); 3978c2ecf20Sopenharmony_ci { u64 x103 = (x101 + x67); 3988c2ecf20Sopenharmony_ci { u64 x104 = (x103 >> 0x1a); 3998c2ecf20Sopenharmony_ci { u32 x105 = ((u32)x103 & 0x3ffffff); 4008c2ecf20Sopenharmony_ci { u64 x106 = (x104 + x64); 4018c2ecf20Sopenharmony_ci { u64 x107 = (x106 >> 0x19); 4028c2ecf20Sopenharmony_ci { u32 x108 = ((u32)x106 & 0x1ffffff); 4038c2ecf20Sopenharmony_ci { u64 x109 = (x107 + x61); 4048c2ecf20Sopenharmony_ci { u64 x110 = (x109 >> 0x1a); 4058c2ecf20Sopenharmony_ci { u32 x111 = ((u32)x109 & 0x3ffffff); 4068c2ecf20Sopenharmony_ci { u64 x112 = (x110 + x49); 4078c2ecf20Sopenharmony_ci { u64 x113 = (x112 >> 0x19); 4088c2ecf20Sopenharmony_ci { u32 x114 = ((u32)x112 & 0x1ffffff); 4098c2ecf20Sopenharmony_ci { u64 x115 = (x87 + (0x13 * x113)); 4108c2ecf20Sopenharmony_ci { u32 x116 = (u32) (x115 >> 0x1a); 4118c2ecf20Sopenharmony_ci { u32 x117 = ((u32)x115 & 0x3ffffff); 4128c2ecf20Sopenharmony_ci { u32 x118 = (x116 + x90); 4138c2ecf20Sopenharmony_ci { u32 x119 = (x118 >> 0x19); 4148c2ecf20Sopenharmony_ci { u32 x120 = (x118 & 0x1ffffff); 4158c2ecf20Sopenharmony_ci out[0] = x117; 4168c2ecf20Sopenharmony_ci out[1] = x120; 4178c2ecf20Sopenharmony_ci out[2] = (x119 + x93); 4188c2ecf20Sopenharmony_ci out[3] = x96; 4198c2ecf20Sopenharmony_ci out[4] = x99; 4208c2ecf20Sopenharmony_ci out[5] = x102; 4218c2ecf20Sopenharmony_ci out[6] = x105; 4228c2ecf20Sopenharmony_ci out[7] = x108; 4238c2ecf20Sopenharmony_ci out[8] = x111; 4248c2ecf20Sopenharmony_ci out[9] = x114; 4258c2ecf20Sopenharmony_ci }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} 4268c2ecf20Sopenharmony_ci} 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_cistatic __always_inline void fe_mul_ttt(fe *h, const fe *f, const fe *g) 4298c2ecf20Sopenharmony_ci{ 4308c2ecf20Sopenharmony_ci fe_mul_impl(h->v, f->v, g->v); 4318c2ecf20Sopenharmony_ci} 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_cistatic __always_inline void fe_mul_tlt(fe *h, const fe_loose *f, const fe *g) 4348c2ecf20Sopenharmony_ci{ 4358c2ecf20Sopenharmony_ci fe_mul_impl(h->v, f->v, g->v); 4368c2ecf20Sopenharmony_ci} 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_cistatic __always_inline void 4398c2ecf20Sopenharmony_cife_mul_tll(fe *h, const fe_loose *f, const fe_loose *g) 4408c2ecf20Sopenharmony_ci{ 4418c2ecf20Sopenharmony_ci fe_mul_impl(h->v, f->v, g->v); 4428c2ecf20Sopenharmony_ci} 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_cistatic noinline void fe_sqr_impl(u32 out[10], const u32 in1[10]) 4458c2ecf20Sopenharmony_ci{ 4468c2ecf20Sopenharmony_ci { const u32 x17 = in1[9]; 4478c2ecf20Sopenharmony_ci { const u32 x18 = in1[8]; 4488c2ecf20Sopenharmony_ci { const u32 x16 = in1[7]; 4498c2ecf20Sopenharmony_ci { const u32 x14 = in1[6]; 4508c2ecf20Sopenharmony_ci { const u32 x12 = in1[5]; 4518c2ecf20Sopenharmony_ci { const u32 x10 = in1[4]; 4528c2ecf20Sopenharmony_ci { const u32 x8 = in1[3]; 4538c2ecf20Sopenharmony_ci { const u32 x6 = in1[2]; 4548c2ecf20Sopenharmony_ci { const u32 x4 = in1[1]; 4558c2ecf20Sopenharmony_ci { const u32 x2 = in1[0]; 4568c2ecf20Sopenharmony_ci { u64 x19 = ((u64)x2 * x2); 4578c2ecf20Sopenharmony_ci { u64 x20 = ((u64)(0x2 * x2) * x4); 4588c2ecf20Sopenharmony_ci { u64 x21 = (0x2 * (((u64)x4 * x4) + ((u64)x2 * x6))); 4598c2ecf20Sopenharmony_ci { u64 x22 = (0x2 * (((u64)x4 * x6) + ((u64)x2 * x8))); 4608c2ecf20Sopenharmony_ci { u64 x23 = ((((u64)x6 * x6) + ((u64)(0x4 * x4) * x8)) + ((u64)(0x2 * x2) * x10)); 4618c2ecf20Sopenharmony_ci { u64 x24 = (0x2 * ((((u64)x6 * x8) + ((u64)x4 * x10)) + ((u64)x2 * x12))); 4628c2ecf20Sopenharmony_ci { u64 x25 = (0x2 * (((((u64)x8 * x8) + ((u64)x6 * x10)) + ((u64)x2 * x14)) + ((u64)(0x2 * x4) * x12))); 4638c2ecf20Sopenharmony_ci { u64 x26 = (0x2 * (((((u64)x8 * x10) + ((u64)x6 * x12)) + ((u64)x4 * x14)) + ((u64)x2 * x16))); 4648c2ecf20Sopenharmony_ci { u64 x27 = (((u64)x10 * x10) + (0x2 * ((((u64)x6 * x14) + ((u64)x2 * x18)) + (0x2 * (((u64)x4 * x16) + ((u64)x8 * x12)))))); 4658c2ecf20Sopenharmony_ci { u64 x28 = (0x2 * ((((((u64)x10 * x12) + ((u64)x8 * x14)) + ((u64)x6 * x16)) + ((u64)x4 * x18)) + ((u64)x2 * x17))); 4668c2ecf20Sopenharmony_ci { u64 x29 = (0x2 * (((((u64)x12 * x12) + ((u64)x10 * x14)) + ((u64)x6 * x18)) + (0x2 * (((u64)x8 * x16) + ((u64)x4 * x17))))); 4678c2ecf20Sopenharmony_ci { u64 x30 = (0x2 * (((((u64)x12 * x14) + ((u64)x10 * x16)) + ((u64)x8 * x18)) + ((u64)x6 * x17))); 4688c2ecf20Sopenharmony_ci { u64 x31 = (((u64)x14 * x14) + (0x2 * (((u64)x10 * x18) + (0x2 * (((u64)x12 * x16) + ((u64)x8 * x17)))))); 4698c2ecf20Sopenharmony_ci { u64 x32 = (0x2 * ((((u64)x14 * x16) + ((u64)x12 * x18)) + ((u64)x10 * x17))); 4708c2ecf20Sopenharmony_ci { u64 x33 = (0x2 * ((((u64)x16 * x16) + ((u64)x14 * x18)) + ((u64)(0x2 * x12) * x17))); 4718c2ecf20Sopenharmony_ci { u64 x34 = (0x2 * (((u64)x16 * x18) + ((u64)x14 * x17))); 4728c2ecf20Sopenharmony_ci { u64 x35 = (((u64)x18 * x18) + ((u64)(0x4 * x16) * x17)); 4738c2ecf20Sopenharmony_ci { u64 x36 = ((u64)(0x2 * x18) * x17); 4748c2ecf20Sopenharmony_ci { u64 x37 = ((u64)(0x2 * x17) * x17); 4758c2ecf20Sopenharmony_ci { u64 x38 = (x27 + (x37 << 0x4)); 4768c2ecf20Sopenharmony_ci { u64 x39 = (x38 + (x37 << 0x1)); 4778c2ecf20Sopenharmony_ci { u64 x40 = (x39 + x37); 4788c2ecf20Sopenharmony_ci { u64 x41 = (x26 + (x36 << 0x4)); 4798c2ecf20Sopenharmony_ci { u64 x42 = (x41 + (x36 << 0x1)); 4808c2ecf20Sopenharmony_ci { u64 x43 = (x42 + x36); 4818c2ecf20Sopenharmony_ci { u64 x44 = (x25 + (x35 << 0x4)); 4828c2ecf20Sopenharmony_ci { u64 x45 = (x44 + (x35 << 0x1)); 4838c2ecf20Sopenharmony_ci { u64 x46 = (x45 + x35); 4848c2ecf20Sopenharmony_ci { u64 x47 = (x24 + (x34 << 0x4)); 4858c2ecf20Sopenharmony_ci { u64 x48 = (x47 + (x34 << 0x1)); 4868c2ecf20Sopenharmony_ci { u64 x49 = (x48 + x34); 4878c2ecf20Sopenharmony_ci { u64 x50 = (x23 + (x33 << 0x4)); 4888c2ecf20Sopenharmony_ci { u64 x51 = (x50 + (x33 << 0x1)); 4898c2ecf20Sopenharmony_ci { u64 x52 = (x51 + x33); 4908c2ecf20Sopenharmony_ci { u64 x53 = (x22 + (x32 << 0x4)); 4918c2ecf20Sopenharmony_ci { u64 x54 = (x53 + (x32 << 0x1)); 4928c2ecf20Sopenharmony_ci { u64 x55 = (x54 + x32); 4938c2ecf20Sopenharmony_ci { u64 x56 = (x21 + (x31 << 0x4)); 4948c2ecf20Sopenharmony_ci { u64 x57 = (x56 + (x31 << 0x1)); 4958c2ecf20Sopenharmony_ci { u64 x58 = (x57 + x31); 4968c2ecf20Sopenharmony_ci { u64 x59 = (x20 + (x30 << 0x4)); 4978c2ecf20Sopenharmony_ci { u64 x60 = (x59 + (x30 << 0x1)); 4988c2ecf20Sopenharmony_ci { u64 x61 = (x60 + x30); 4998c2ecf20Sopenharmony_ci { u64 x62 = (x19 + (x29 << 0x4)); 5008c2ecf20Sopenharmony_ci { u64 x63 = (x62 + (x29 << 0x1)); 5018c2ecf20Sopenharmony_ci { u64 x64 = (x63 + x29); 5028c2ecf20Sopenharmony_ci { u64 x65 = (x64 >> 0x1a); 5038c2ecf20Sopenharmony_ci { u32 x66 = ((u32)x64 & 0x3ffffff); 5048c2ecf20Sopenharmony_ci { u64 x67 = (x65 + x61); 5058c2ecf20Sopenharmony_ci { u64 x68 = (x67 >> 0x19); 5068c2ecf20Sopenharmony_ci { u32 x69 = ((u32)x67 & 0x1ffffff); 5078c2ecf20Sopenharmony_ci { u64 x70 = (x68 + x58); 5088c2ecf20Sopenharmony_ci { u64 x71 = (x70 >> 0x1a); 5098c2ecf20Sopenharmony_ci { u32 x72 = ((u32)x70 & 0x3ffffff); 5108c2ecf20Sopenharmony_ci { u64 x73 = (x71 + x55); 5118c2ecf20Sopenharmony_ci { u64 x74 = (x73 >> 0x19); 5128c2ecf20Sopenharmony_ci { u32 x75 = ((u32)x73 & 0x1ffffff); 5138c2ecf20Sopenharmony_ci { u64 x76 = (x74 + x52); 5148c2ecf20Sopenharmony_ci { u64 x77 = (x76 >> 0x1a); 5158c2ecf20Sopenharmony_ci { u32 x78 = ((u32)x76 & 0x3ffffff); 5168c2ecf20Sopenharmony_ci { u64 x79 = (x77 + x49); 5178c2ecf20Sopenharmony_ci { u64 x80 = (x79 >> 0x19); 5188c2ecf20Sopenharmony_ci { u32 x81 = ((u32)x79 & 0x1ffffff); 5198c2ecf20Sopenharmony_ci { u64 x82 = (x80 + x46); 5208c2ecf20Sopenharmony_ci { u64 x83 = (x82 >> 0x1a); 5218c2ecf20Sopenharmony_ci { u32 x84 = ((u32)x82 & 0x3ffffff); 5228c2ecf20Sopenharmony_ci { u64 x85 = (x83 + x43); 5238c2ecf20Sopenharmony_ci { u64 x86 = (x85 >> 0x19); 5248c2ecf20Sopenharmony_ci { u32 x87 = ((u32)x85 & 0x1ffffff); 5258c2ecf20Sopenharmony_ci { u64 x88 = (x86 + x40); 5268c2ecf20Sopenharmony_ci { u64 x89 = (x88 >> 0x1a); 5278c2ecf20Sopenharmony_ci { u32 x90 = ((u32)x88 & 0x3ffffff); 5288c2ecf20Sopenharmony_ci { u64 x91 = (x89 + x28); 5298c2ecf20Sopenharmony_ci { u64 x92 = (x91 >> 0x19); 5308c2ecf20Sopenharmony_ci { u32 x93 = ((u32)x91 & 0x1ffffff); 5318c2ecf20Sopenharmony_ci { u64 x94 = (x66 + (0x13 * x92)); 5328c2ecf20Sopenharmony_ci { u32 x95 = (u32) (x94 >> 0x1a); 5338c2ecf20Sopenharmony_ci { u32 x96 = ((u32)x94 & 0x3ffffff); 5348c2ecf20Sopenharmony_ci { u32 x97 = (x95 + x69); 5358c2ecf20Sopenharmony_ci { u32 x98 = (x97 >> 0x19); 5368c2ecf20Sopenharmony_ci { u32 x99 = (x97 & 0x1ffffff); 5378c2ecf20Sopenharmony_ci out[0] = x96; 5388c2ecf20Sopenharmony_ci out[1] = x99; 5398c2ecf20Sopenharmony_ci out[2] = (x98 + x72); 5408c2ecf20Sopenharmony_ci out[3] = x75; 5418c2ecf20Sopenharmony_ci out[4] = x78; 5428c2ecf20Sopenharmony_ci out[5] = x81; 5438c2ecf20Sopenharmony_ci out[6] = x84; 5448c2ecf20Sopenharmony_ci out[7] = x87; 5458c2ecf20Sopenharmony_ci out[8] = x90; 5468c2ecf20Sopenharmony_ci out[9] = x93; 5478c2ecf20Sopenharmony_ci }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} 5488c2ecf20Sopenharmony_ci} 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_cistatic __always_inline void fe_sq_tl(fe *h, const fe_loose *f) 5518c2ecf20Sopenharmony_ci{ 5528c2ecf20Sopenharmony_ci fe_sqr_impl(h->v, f->v); 5538c2ecf20Sopenharmony_ci} 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_cistatic __always_inline void fe_sq_tt(fe *h, const fe *f) 5568c2ecf20Sopenharmony_ci{ 5578c2ecf20Sopenharmony_ci fe_sqr_impl(h->v, f->v); 5588c2ecf20Sopenharmony_ci} 5598c2ecf20Sopenharmony_ci 5608c2ecf20Sopenharmony_cistatic __always_inline void fe_loose_invert(fe *out, const fe_loose *z) 5618c2ecf20Sopenharmony_ci{ 5628c2ecf20Sopenharmony_ci fe t0; 5638c2ecf20Sopenharmony_ci fe t1; 5648c2ecf20Sopenharmony_ci fe t2; 5658c2ecf20Sopenharmony_ci fe t3; 5668c2ecf20Sopenharmony_ci int i; 5678c2ecf20Sopenharmony_ci 5688c2ecf20Sopenharmony_ci fe_sq_tl(&t0, z); 5698c2ecf20Sopenharmony_ci fe_sq_tt(&t1, &t0); 5708c2ecf20Sopenharmony_ci for (i = 1; i < 2; ++i) 5718c2ecf20Sopenharmony_ci fe_sq_tt(&t1, &t1); 5728c2ecf20Sopenharmony_ci fe_mul_tlt(&t1, z, &t1); 5738c2ecf20Sopenharmony_ci fe_mul_ttt(&t0, &t0, &t1); 5748c2ecf20Sopenharmony_ci fe_sq_tt(&t2, &t0); 5758c2ecf20Sopenharmony_ci fe_mul_ttt(&t1, &t1, &t2); 5768c2ecf20Sopenharmony_ci fe_sq_tt(&t2, &t1); 5778c2ecf20Sopenharmony_ci for (i = 1; i < 5; ++i) 5788c2ecf20Sopenharmony_ci fe_sq_tt(&t2, &t2); 5798c2ecf20Sopenharmony_ci fe_mul_ttt(&t1, &t2, &t1); 5808c2ecf20Sopenharmony_ci fe_sq_tt(&t2, &t1); 5818c2ecf20Sopenharmony_ci for (i = 1; i < 10; ++i) 5828c2ecf20Sopenharmony_ci fe_sq_tt(&t2, &t2); 5838c2ecf20Sopenharmony_ci fe_mul_ttt(&t2, &t2, &t1); 5848c2ecf20Sopenharmony_ci fe_sq_tt(&t3, &t2); 5858c2ecf20Sopenharmony_ci for (i = 1; i < 20; ++i) 5868c2ecf20Sopenharmony_ci fe_sq_tt(&t3, &t3); 5878c2ecf20Sopenharmony_ci fe_mul_ttt(&t2, &t3, &t2); 5888c2ecf20Sopenharmony_ci fe_sq_tt(&t2, &t2); 5898c2ecf20Sopenharmony_ci for (i = 1; i < 10; ++i) 5908c2ecf20Sopenharmony_ci fe_sq_tt(&t2, &t2); 5918c2ecf20Sopenharmony_ci fe_mul_ttt(&t1, &t2, &t1); 5928c2ecf20Sopenharmony_ci fe_sq_tt(&t2, &t1); 5938c2ecf20Sopenharmony_ci for (i = 1; i < 50; ++i) 5948c2ecf20Sopenharmony_ci fe_sq_tt(&t2, &t2); 5958c2ecf20Sopenharmony_ci fe_mul_ttt(&t2, &t2, &t1); 5968c2ecf20Sopenharmony_ci fe_sq_tt(&t3, &t2); 5978c2ecf20Sopenharmony_ci for (i = 1; i < 100; ++i) 5988c2ecf20Sopenharmony_ci fe_sq_tt(&t3, &t3); 5998c2ecf20Sopenharmony_ci fe_mul_ttt(&t2, &t3, &t2); 6008c2ecf20Sopenharmony_ci fe_sq_tt(&t2, &t2); 6018c2ecf20Sopenharmony_ci for (i = 1; i < 50; ++i) 6028c2ecf20Sopenharmony_ci fe_sq_tt(&t2, &t2); 6038c2ecf20Sopenharmony_ci fe_mul_ttt(&t1, &t2, &t1); 6048c2ecf20Sopenharmony_ci fe_sq_tt(&t1, &t1); 6058c2ecf20Sopenharmony_ci for (i = 1; i < 5; ++i) 6068c2ecf20Sopenharmony_ci fe_sq_tt(&t1, &t1); 6078c2ecf20Sopenharmony_ci fe_mul_ttt(out, &t1, &t0); 6088c2ecf20Sopenharmony_ci} 6098c2ecf20Sopenharmony_ci 6108c2ecf20Sopenharmony_cistatic __always_inline void fe_invert(fe *out, const fe *z) 6118c2ecf20Sopenharmony_ci{ 6128c2ecf20Sopenharmony_ci fe_loose l; 6138c2ecf20Sopenharmony_ci fe_copy_lt(&l, z); 6148c2ecf20Sopenharmony_ci fe_loose_invert(out, &l); 6158c2ecf20Sopenharmony_ci} 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_ci/* Replace (f,g) with (g,f) if b == 1; 6188c2ecf20Sopenharmony_ci * replace (f,g) with (f,g) if b == 0. 6198c2ecf20Sopenharmony_ci * 6208c2ecf20Sopenharmony_ci * Preconditions: b in {0,1} 6218c2ecf20Sopenharmony_ci */ 6228c2ecf20Sopenharmony_cistatic noinline void fe_cswap(fe *f, fe *g, unsigned int b) 6238c2ecf20Sopenharmony_ci{ 6248c2ecf20Sopenharmony_ci unsigned i; 6258c2ecf20Sopenharmony_ci b = 0 - b; 6268c2ecf20Sopenharmony_ci for (i = 0; i < 10; i++) { 6278c2ecf20Sopenharmony_ci u32 x = f->v[i] ^ g->v[i]; 6288c2ecf20Sopenharmony_ci x &= b; 6298c2ecf20Sopenharmony_ci f->v[i] ^= x; 6308c2ecf20Sopenharmony_ci g->v[i] ^= x; 6318c2ecf20Sopenharmony_ci } 6328c2ecf20Sopenharmony_ci} 6338c2ecf20Sopenharmony_ci 6348c2ecf20Sopenharmony_ci/* NOTE: based on fiat-crypto fe_mul, edited for in2=121666, 0, 0.*/ 6358c2ecf20Sopenharmony_cistatic __always_inline void fe_mul_121666_impl(u32 out[10], const u32 in1[10]) 6368c2ecf20Sopenharmony_ci{ 6378c2ecf20Sopenharmony_ci { const u32 x20 = in1[9]; 6388c2ecf20Sopenharmony_ci { const u32 x21 = in1[8]; 6398c2ecf20Sopenharmony_ci { const u32 x19 = in1[7]; 6408c2ecf20Sopenharmony_ci { const u32 x17 = in1[6]; 6418c2ecf20Sopenharmony_ci { const u32 x15 = in1[5]; 6428c2ecf20Sopenharmony_ci { const u32 x13 = in1[4]; 6438c2ecf20Sopenharmony_ci { const u32 x11 = in1[3]; 6448c2ecf20Sopenharmony_ci { const u32 x9 = in1[2]; 6458c2ecf20Sopenharmony_ci { const u32 x7 = in1[1]; 6468c2ecf20Sopenharmony_ci { const u32 x5 = in1[0]; 6478c2ecf20Sopenharmony_ci { const u32 x38 = 0; 6488c2ecf20Sopenharmony_ci { const u32 x39 = 0; 6498c2ecf20Sopenharmony_ci { const u32 x37 = 0; 6508c2ecf20Sopenharmony_ci { const u32 x35 = 0; 6518c2ecf20Sopenharmony_ci { const u32 x33 = 0; 6528c2ecf20Sopenharmony_ci { const u32 x31 = 0; 6538c2ecf20Sopenharmony_ci { const u32 x29 = 0; 6548c2ecf20Sopenharmony_ci { const u32 x27 = 0; 6558c2ecf20Sopenharmony_ci { const u32 x25 = 0; 6568c2ecf20Sopenharmony_ci { const u32 x23 = 121666; 6578c2ecf20Sopenharmony_ci { u64 x40 = ((u64)x23 * x5); 6588c2ecf20Sopenharmony_ci { u64 x41 = (((u64)x23 * x7) + ((u64)x25 * x5)); 6598c2ecf20Sopenharmony_ci { u64 x42 = ((((u64)(0x2 * x25) * x7) + ((u64)x23 * x9)) + ((u64)x27 * x5)); 6608c2ecf20Sopenharmony_ci { u64 x43 = (((((u64)x25 * x9) + ((u64)x27 * x7)) + ((u64)x23 * x11)) + ((u64)x29 * x5)); 6618c2ecf20Sopenharmony_ci { u64 x44 = (((((u64)x27 * x9) + (0x2 * (((u64)x25 * x11) + ((u64)x29 * x7)))) + ((u64)x23 * x13)) + ((u64)x31 * x5)); 6628c2ecf20Sopenharmony_ci { u64 x45 = (((((((u64)x27 * x11) + ((u64)x29 * x9)) + ((u64)x25 * x13)) + ((u64)x31 * x7)) + ((u64)x23 * x15)) + ((u64)x33 * x5)); 6638c2ecf20Sopenharmony_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)); 6648c2ecf20Sopenharmony_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)); 6658c2ecf20Sopenharmony_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)); 6668c2ecf20Sopenharmony_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)); 6678c2ecf20Sopenharmony_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)); 6688c2ecf20Sopenharmony_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)); 6698c2ecf20Sopenharmony_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)); 6708c2ecf20Sopenharmony_ci { u64 x53 = (((((((u64)x35 * x19) + ((u64)x37 * x17)) + ((u64)x33 * x21)) + ((u64)x39 * x15)) + ((u64)x31 * x20)) + ((u64)x38 * x13)); 6718c2ecf20Sopenharmony_ci { u64 x54 = (((0x2 * ((((u64)x37 * x19) + ((u64)x33 * x20)) + ((u64)x38 * x15))) + ((u64)x35 * x21)) + ((u64)x39 * x17)); 6728c2ecf20Sopenharmony_ci { u64 x55 = (((((u64)x37 * x21) + ((u64)x39 * x19)) + ((u64)x35 * x20)) + ((u64)x38 * x17)); 6738c2ecf20Sopenharmony_ci { u64 x56 = (((u64)x39 * x21) + (0x2 * (((u64)x37 * x20) + ((u64)x38 * x19)))); 6748c2ecf20Sopenharmony_ci { u64 x57 = (((u64)x39 * x20) + ((u64)x38 * x21)); 6758c2ecf20Sopenharmony_ci { u64 x58 = ((u64)(0x2 * x38) * x20); 6768c2ecf20Sopenharmony_ci { u64 x59 = (x48 + (x58 << 0x4)); 6778c2ecf20Sopenharmony_ci { u64 x60 = (x59 + (x58 << 0x1)); 6788c2ecf20Sopenharmony_ci { u64 x61 = (x60 + x58); 6798c2ecf20Sopenharmony_ci { u64 x62 = (x47 + (x57 << 0x4)); 6808c2ecf20Sopenharmony_ci { u64 x63 = (x62 + (x57 << 0x1)); 6818c2ecf20Sopenharmony_ci { u64 x64 = (x63 + x57); 6828c2ecf20Sopenharmony_ci { u64 x65 = (x46 + (x56 << 0x4)); 6838c2ecf20Sopenharmony_ci { u64 x66 = (x65 + (x56 << 0x1)); 6848c2ecf20Sopenharmony_ci { u64 x67 = (x66 + x56); 6858c2ecf20Sopenharmony_ci { u64 x68 = (x45 + (x55 << 0x4)); 6868c2ecf20Sopenharmony_ci { u64 x69 = (x68 + (x55 << 0x1)); 6878c2ecf20Sopenharmony_ci { u64 x70 = (x69 + x55); 6888c2ecf20Sopenharmony_ci { u64 x71 = (x44 + (x54 << 0x4)); 6898c2ecf20Sopenharmony_ci { u64 x72 = (x71 + (x54 << 0x1)); 6908c2ecf20Sopenharmony_ci { u64 x73 = (x72 + x54); 6918c2ecf20Sopenharmony_ci { u64 x74 = (x43 + (x53 << 0x4)); 6928c2ecf20Sopenharmony_ci { u64 x75 = (x74 + (x53 << 0x1)); 6938c2ecf20Sopenharmony_ci { u64 x76 = (x75 + x53); 6948c2ecf20Sopenharmony_ci { u64 x77 = (x42 + (x52 << 0x4)); 6958c2ecf20Sopenharmony_ci { u64 x78 = (x77 + (x52 << 0x1)); 6968c2ecf20Sopenharmony_ci { u64 x79 = (x78 + x52); 6978c2ecf20Sopenharmony_ci { u64 x80 = (x41 + (x51 << 0x4)); 6988c2ecf20Sopenharmony_ci { u64 x81 = (x80 + (x51 << 0x1)); 6998c2ecf20Sopenharmony_ci { u64 x82 = (x81 + x51); 7008c2ecf20Sopenharmony_ci { u64 x83 = (x40 + (x50 << 0x4)); 7018c2ecf20Sopenharmony_ci { u64 x84 = (x83 + (x50 << 0x1)); 7028c2ecf20Sopenharmony_ci { u64 x85 = (x84 + x50); 7038c2ecf20Sopenharmony_ci { u64 x86 = (x85 >> 0x1a); 7048c2ecf20Sopenharmony_ci { u32 x87 = ((u32)x85 & 0x3ffffff); 7058c2ecf20Sopenharmony_ci { u64 x88 = (x86 + x82); 7068c2ecf20Sopenharmony_ci { u64 x89 = (x88 >> 0x19); 7078c2ecf20Sopenharmony_ci { u32 x90 = ((u32)x88 & 0x1ffffff); 7088c2ecf20Sopenharmony_ci { u64 x91 = (x89 + x79); 7098c2ecf20Sopenharmony_ci { u64 x92 = (x91 >> 0x1a); 7108c2ecf20Sopenharmony_ci { u32 x93 = ((u32)x91 & 0x3ffffff); 7118c2ecf20Sopenharmony_ci { u64 x94 = (x92 + x76); 7128c2ecf20Sopenharmony_ci { u64 x95 = (x94 >> 0x19); 7138c2ecf20Sopenharmony_ci { u32 x96 = ((u32)x94 & 0x1ffffff); 7148c2ecf20Sopenharmony_ci { u64 x97 = (x95 + x73); 7158c2ecf20Sopenharmony_ci { u64 x98 = (x97 >> 0x1a); 7168c2ecf20Sopenharmony_ci { u32 x99 = ((u32)x97 & 0x3ffffff); 7178c2ecf20Sopenharmony_ci { u64 x100 = (x98 + x70); 7188c2ecf20Sopenharmony_ci { u64 x101 = (x100 >> 0x19); 7198c2ecf20Sopenharmony_ci { u32 x102 = ((u32)x100 & 0x1ffffff); 7208c2ecf20Sopenharmony_ci { u64 x103 = (x101 + x67); 7218c2ecf20Sopenharmony_ci { u64 x104 = (x103 >> 0x1a); 7228c2ecf20Sopenharmony_ci { u32 x105 = ((u32)x103 & 0x3ffffff); 7238c2ecf20Sopenharmony_ci { u64 x106 = (x104 + x64); 7248c2ecf20Sopenharmony_ci { u64 x107 = (x106 >> 0x19); 7258c2ecf20Sopenharmony_ci { u32 x108 = ((u32)x106 & 0x1ffffff); 7268c2ecf20Sopenharmony_ci { u64 x109 = (x107 + x61); 7278c2ecf20Sopenharmony_ci { u64 x110 = (x109 >> 0x1a); 7288c2ecf20Sopenharmony_ci { u32 x111 = ((u32)x109 & 0x3ffffff); 7298c2ecf20Sopenharmony_ci { u64 x112 = (x110 + x49); 7308c2ecf20Sopenharmony_ci { u64 x113 = (x112 >> 0x19); 7318c2ecf20Sopenharmony_ci { u32 x114 = ((u32)x112 & 0x1ffffff); 7328c2ecf20Sopenharmony_ci { u64 x115 = (x87 + (0x13 * x113)); 7338c2ecf20Sopenharmony_ci { u32 x116 = (u32) (x115 >> 0x1a); 7348c2ecf20Sopenharmony_ci { u32 x117 = ((u32)x115 & 0x3ffffff); 7358c2ecf20Sopenharmony_ci { u32 x118 = (x116 + x90); 7368c2ecf20Sopenharmony_ci { u32 x119 = (x118 >> 0x19); 7378c2ecf20Sopenharmony_ci { u32 x120 = (x118 & 0x1ffffff); 7388c2ecf20Sopenharmony_ci out[0] = x117; 7398c2ecf20Sopenharmony_ci out[1] = x120; 7408c2ecf20Sopenharmony_ci out[2] = (x119 + x93); 7418c2ecf20Sopenharmony_ci out[3] = x96; 7428c2ecf20Sopenharmony_ci out[4] = x99; 7438c2ecf20Sopenharmony_ci out[5] = x102; 7448c2ecf20Sopenharmony_ci out[6] = x105; 7458c2ecf20Sopenharmony_ci out[7] = x108; 7468c2ecf20Sopenharmony_ci out[8] = x111; 7478c2ecf20Sopenharmony_ci out[9] = x114; 7488c2ecf20Sopenharmony_ci }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} 7498c2ecf20Sopenharmony_ci} 7508c2ecf20Sopenharmony_ci 7518c2ecf20Sopenharmony_cistatic __always_inline void fe_mul121666(fe *h, const fe_loose *f) 7528c2ecf20Sopenharmony_ci{ 7538c2ecf20Sopenharmony_ci fe_mul_121666_impl(h->v, f->v); 7548c2ecf20Sopenharmony_ci} 7558c2ecf20Sopenharmony_ci 7568c2ecf20Sopenharmony_civoid curve25519_generic(u8 out[CURVE25519_KEY_SIZE], 7578c2ecf20Sopenharmony_ci const u8 scalar[CURVE25519_KEY_SIZE], 7588c2ecf20Sopenharmony_ci const u8 point[CURVE25519_KEY_SIZE]) 7598c2ecf20Sopenharmony_ci{ 7608c2ecf20Sopenharmony_ci fe x1, x2, z2, x3, z3; 7618c2ecf20Sopenharmony_ci fe_loose x2l, z2l, x3l; 7628c2ecf20Sopenharmony_ci unsigned swap = 0; 7638c2ecf20Sopenharmony_ci int pos; 7648c2ecf20Sopenharmony_ci u8 e[32]; 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci memcpy(e, scalar, 32); 7678c2ecf20Sopenharmony_ci curve25519_clamp_secret(e); 7688c2ecf20Sopenharmony_ci 7698c2ecf20Sopenharmony_ci /* The following implementation was transcribed to Coq and proven to 7708c2ecf20Sopenharmony_ci * correspond to unary scalar multiplication in affine coordinates given 7718c2ecf20Sopenharmony_ci * that x1 != 0 is the x coordinate of some point on the curve. It was 7728c2ecf20Sopenharmony_ci * also checked in Coq that doing a ladderstep with x1 = x3 = 0 gives 7738c2ecf20Sopenharmony_ci * z2' = z3' = 0, and z2 = z3 = 0 gives z2' = z3' = 0. The statement was 7748c2ecf20Sopenharmony_ci * quantified over the underlying field, so it applies to Curve25519 7758c2ecf20Sopenharmony_ci * itself and the quadratic twist of Curve25519. It was not proven in 7768c2ecf20Sopenharmony_ci * Coq that prime-field arithmetic correctly simulates extension-field 7778c2ecf20Sopenharmony_ci * arithmetic on prime-field values. The decoding of the byte array 7788c2ecf20Sopenharmony_ci * representation of e was not considered. 7798c2ecf20Sopenharmony_ci * 7808c2ecf20Sopenharmony_ci * Specification of Montgomery curves in affine coordinates: 7818c2ecf20Sopenharmony_ci * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Spec/MontgomeryCurve.v#L27> 7828c2ecf20Sopenharmony_ci * 7838c2ecf20Sopenharmony_ci * Proof that these form a group that is isomorphic to a Weierstrass 7848c2ecf20Sopenharmony_ci * curve: 7858c2ecf20Sopenharmony_ci * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/AffineProofs.v#L35> 7868c2ecf20Sopenharmony_ci * 7878c2ecf20Sopenharmony_ci * Coq transcription and correctness proof of the loop 7888c2ecf20Sopenharmony_ci * (where scalarbits=255): 7898c2ecf20Sopenharmony_ci * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZ.v#L118> 7908c2ecf20Sopenharmony_ci * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L278> 7918c2ecf20Sopenharmony_ci * preconditions: 0 <= e < 2^255 (not necessarily e < order), 7928c2ecf20Sopenharmony_ci * fe_invert(0) = 0 7938c2ecf20Sopenharmony_ci */ 7948c2ecf20Sopenharmony_ci fe_frombytes(&x1, point); 7958c2ecf20Sopenharmony_ci fe_1(&x2); 7968c2ecf20Sopenharmony_ci fe_0(&z2); 7978c2ecf20Sopenharmony_ci fe_copy(&x3, &x1); 7988c2ecf20Sopenharmony_ci fe_1(&z3); 7998c2ecf20Sopenharmony_ci 8008c2ecf20Sopenharmony_ci for (pos = 254; pos >= 0; --pos) { 8018c2ecf20Sopenharmony_ci fe tmp0, tmp1; 8028c2ecf20Sopenharmony_ci fe_loose tmp0l, tmp1l; 8038c2ecf20Sopenharmony_ci /* loop invariant as of right before the test, for the case 8048c2ecf20Sopenharmony_ci * where x1 != 0: 8058c2ecf20Sopenharmony_ci * pos >= -1; if z2 = 0 then x2 is nonzero; if z3 = 0 then x3 8068c2ecf20Sopenharmony_ci * is nonzero 8078c2ecf20Sopenharmony_ci * let r := e >> (pos+1) in the following equalities of 8088c2ecf20Sopenharmony_ci * projective points: 8098c2ecf20Sopenharmony_ci * to_xz (r*P) === if swap then (x3, z3) else (x2, z2) 8108c2ecf20Sopenharmony_ci * to_xz ((r+1)*P) === if swap then (x2, z2) else (x3, z3) 8118c2ecf20Sopenharmony_ci * x1 is the nonzero x coordinate of the nonzero 8128c2ecf20Sopenharmony_ci * point (r*P-(r+1)*P) 8138c2ecf20Sopenharmony_ci */ 8148c2ecf20Sopenharmony_ci unsigned b = 1 & (e[pos / 8] >> (pos & 7)); 8158c2ecf20Sopenharmony_ci swap ^= b; 8168c2ecf20Sopenharmony_ci fe_cswap(&x2, &x3, swap); 8178c2ecf20Sopenharmony_ci fe_cswap(&z2, &z3, swap); 8188c2ecf20Sopenharmony_ci swap = b; 8198c2ecf20Sopenharmony_ci /* Coq transcription of ladderstep formula (called from 8208c2ecf20Sopenharmony_ci * transcribed loop): 8218c2ecf20Sopenharmony_ci * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZ.v#L89> 8228c2ecf20Sopenharmony_ci * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L131> 8238c2ecf20Sopenharmony_ci * x1 != 0 <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L217> 8248c2ecf20Sopenharmony_ci * x1 = 0 <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L147> 8258c2ecf20Sopenharmony_ci */ 8268c2ecf20Sopenharmony_ci fe_sub(&tmp0l, &x3, &z3); 8278c2ecf20Sopenharmony_ci fe_sub(&tmp1l, &x2, &z2); 8288c2ecf20Sopenharmony_ci fe_add(&x2l, &x2, &z2); 8298c2ecf20Sopenharmony_ci fe_add(&z2l, &x3, &z3); 8308c2ecf20Sopenharmony_ci fe_mul_tll(&z3, &tmp0l, &x2l); 8318c2ecf20Sopenharmony_ci fe_mul_tll(&z2, &z2l, &tmp1l); 8328c2ecf20Sopenharmony_ci fe_sq_tl(&tmp0, &tmp1l); 8338c2ecf20Sopenharmony_ci fe_sq_tl(&tmp1, &x2l); 8348c2ecf20Sopenharmony_ci fe_add(&x3l, &z3, &z2); 8358c2ecf20Sopenharmony_ci fe_sub(&z2l, &z3, &z2); 8368c2ecf20Sopenharmony_ci fe_mul_ttt(&x2, &tmp1, &tmp0); 8378c2ecf20Sopenharmony_ci fe_sub(&tmp1l, &tmp1, &tmp0); 8388c2ecf20Sopenharmony_ci fe_sq_tl(&z2, &z2l); 8398c2ecf20Sopenharmony_ci fe_mul121666(&z3, &tmp1l); 8408c2ecf20Sopenharmony_ci fe_sq_tl(&x3, &x3l); 8418c2ecf20Sopenharmony_ci fe_add(&tmp0l, &tmp0, &z3); 8428c2ecf20Sopenharmony_ci fe_mul_ttt(&z3, &x1, &z2); 8438c2ecf20Sopenharmony_ci fe_mul_tll(&z2, &tmp1l, &tmp0l); 8448c2ecf20Sopenharmony_ci } 8458c2ecf20Sopenharmony_ci /* here pos=-1, so r=e, so to_xz (e*P) === if swap then (x3, z3) 8468c2ecf20Sopenharmony_ci * else (x2, z2) 8478c2ecf20Sopenharmony_ci */ 8488c2ecf20Sopenharmony_ci fe_cswap(&x2, &x3, swap); 8498c2ecf20Sopenharmony_ci fe_cswap(&z2, &z3, swap); 8508c2ecf20Sopenharmony_ci 8518c2ecf20Sopenharmony_ci fe_invert(&z2, &z2); 8528c2ecf20Sopenharmony_ci fe_mul_ttt(&x2, &x2, &z2); 8538c2ecf20Sopenharmony_ci fe_tobytes(out, &x2); 8548c2ecf20Sopenharmony_ci 8558c2ecf20Sopenharmony_ci memzero_explicit(&x1, sizeof(x1)); 8568c2ecf20Sopenharmony_ci memzero_explicit(&x2, sizeof(x2)); 8578c2ecf20Sopenharmony_ci memzero_explicit(&z2, sizeof(z2)); 8588c2ecf20Sopenharmony_ci memzero_explicit(&x3, sizeof(x3)); 8598c2ecf20Sopenharmony_ci memzero_explicit(&z3, sizeof(z3)); 8608c2ecf20Sopenharmony_ci memzero_explicit(&x2l, sizeof(x2l)); 8618c2ecf20Sopenharmony_ci memzero_explicit(&z2l, sizeof(z2l)); 8628c2ecf20Sopenharmony_ci memzero_explicit(&x3l, sizeof(x3l)); 8638c2ecf20Sopenharmony_ci memzero_explicit(&e, sizeof(e)); 8648c2ecf20Sopenharmony_ci} 865