Home | History | Annotate | Line # | Download | only in dist
      1  1.2  christos /*	$NetBSD: sntrup761.c,v 1.4 2024/09/24 21:32:18 christos Exp $	*/
      2  1.4  christos /*  $OpenBSD: sntrup761.c,v 1.8 2024/09/16 05:37:05 djm Exp $ */
      3  1.1  christos 
      4  1.1  christos /*
      5  1.1  christos  * Public Domain, Authors:
      6  1.1  christos  * - Daniel J. Bernstein
      7  1.1  christos  * - Chitchanok Chuengsatiansup
      8  1.1  christos  * - Tanja Lange
      9  1.1  christos  * - Christine van Vredendaal
     10  1.1  christos  */
     11  1.2  christos #include "includes.h"
     12  1.2  christos __RCSID("$NetBSD: sntrup761.c,v 1.4 2024/09/24 21:32:18 christos Exp $");
     13  1.1  christos 
     14  1.1  christos #include <string.h>
     15  1.1  christos #include "crypto_api.h"
     16  1.1  christos 
     17  1.4  christos #define crypto_declassify(x, y) do {} while (0)
     18  1.4  christos 
     19  1.1  christos #define int8 crypto_int8
     20  1.1  christos #define uint8 crypto_uint8
     21  1.1  christos #define int16 crypto_int16
     22  1.1  christos #define uint16 crypto_uint16
     23  1.1  christos #define int32 crypto_int32
     24  1.1  christos #define uint32 crypto_uint32
     25  1.1  christos #define int64 crypto_int64
     26  1.1  christos #define uint64 crypto_uint64
     27  1.4  christos extern volatile crypto_int16 crypto_int16_optblocker;
     28  1.4  christos extern volatile crypto_int32 crypto_int32_optblocker;
     29  1.4  christos extern volatile crypto_int64 crypto_int64_optblocker;
     30  1.4  christos 
     31  1.4  christos /* from supercop-20240808/cryptoint/crypto_int16.h */
     32  1.4  christos /* auto-generated: cd cryptoint; ./autogen */
     33  1.4  christos /* cryptoint 20240806 */
     34  1.4  christos 
     35  1.4  christos #ifndef crypto_int16_h
     36  1.4  christos #define crypto_int16_h
     37  1.4  christos 
     38  1.4  christos #define crypto_int16 int16_t
     39  1.4  christos #define crypto_int16_unsigned uint16_t
     40  1.1  christos 
     41  1.1  christos 
     42  1.1  christos 
     43  1.4  christos __attribute__((unused))
     44  1.4  christos static inline
     45  1.4  christos crypto_int16 crypto_int16_load(const unsigned char *crypto_int16_s) {
     46  1.4  christos   crypto_int16 crypto_int16_z = 0;
     47  1.4  christos   crypto_int16_z |= ((crypto_int16) (*crypto_int16_s++)) << 0;
     48  1.4  christos   crypto_int16_z |= ((crypto_int16) (*crypto_int16_s++)) << 8;
     49  1.4  christos   return crypto_int16_z;
     50  1.4  christos }
     51  1.4  christos 
     52  1.4  christos __attribute__((unused))
     53  1.4  christos static inline
     54  1.4  christos void crypto_int16_store(unsigned char *crypto_int16_s,crypto_int16 crypto_int16_x) {
     55  1.4  christos   *crypto_int16_s++ = crypto_int16_x >> 0;
     56  1.4  christos   *crypto_int16_s++ = crypto_int16_x >> 8;
     57  1.4  christos }
     58  1.4  christos 
     59  1.4  christos __attribute__((unused))
     60  1.4  christos static inline
     61  1.4  christos crypto_int16 crypto_int16_negative_mask(crypto_int16 crypto_int16_x) {
     62  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
     63  1.4  christos   __asm__ ("sarw $15,%0" : "+r"(crypto_int16_x) : : "cc");
     64  1.4  christos   return crypto_int16_x;
     65  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
     66  1.4  christos   crypto_int16 crypto_int16_y;
     67  1.4  christos   __asm__ ("sbfx %w0,%w1,15,1" : "=r"(crypto_int16_y) : "r"(crypto_int16_x) : );
     68  1.4  christos   return crypto_int16_y;
     69  1.4  christos #else
     70  1.4  christos   crypto_int16_x >>= 16-6;
     71  1.4  christos   crypto_int16_x ^= crypto_int16_optblocker;
     72  1.4  christos   crypto_int16_x >>= 5;
     73  1.4  christos   return crypto_int16_x;
     74  1.4  christos #endif
     75  1.4  christos }
     76  1.1  christos 
     77  1.4  christos __attribute__((unused))
     78  1.4  christos static inline
     79  1.4  christos crypto_int16_unsigned crypto_int16_unsigned_topbit_01(crypto_int16_unsigned crypto_int16_x) {
     80  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
     81  1.4  christos   __asm__ ("shrw $15,%0" : "+r"(crypto_int16_x) : : "cc");
     82  1.4  christos   return crypto_int16_x;
     83  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
     84  1.4  christos   crypto_int16 crypto_int16_y;
     85  1.4  christos   __asm__ ("ubfx %w0,%w1,15,1" : "=r"(crypto_int16_y) : "r"(crypto_int16_x) : );
     86  1.4  christos   return crypto_int16_y;
     87  1.4  christos #else
     88  1.4  christos   crypto_int16_x >>= 16-6;
     89  1.4  christos   crypto_int16_x ^= crypto_int16_optblocker;
     90  1.4  christos   crypto_int16_x >>= 5;
     91  1.4  christos   return crypto_int16_x;
     92  1.4  christos #endif
     93  1.4  christos }
     94  1.1  christos 
     95  1.4  christos __attribute__((unused))
     96  1.4  christos static inline
     97  1.4  christos crypto_int16 crypto_int16_negative_01(crypto_int16 crypto_int16_x) {
     98  1.4  christos   return crypto_int16_unsigned_topbit_01(crypto_int16_x);
     99  1.4  christos }
    100  1.1  christos 
    101  1.4  christos __attribute__((unused))
    102  1.4  christos static inline
    103  1.4  christos crypto_int16 crypto_int16_topbit_mask(crypto_int16 crypto_int16_x) {
    104  1.4  christos   return crypto_int16_negative_mask(crypto_int16_x);
    105  1.4  christos }
    106  1.1  christos 
    107  1.4  christos __attribute__((unused))
    108  1.4  christos static inline
    109  1.4  christos crypto_int16 crypto_int16_topbit_01(crypto_int16 crypto_int16_x) {
    110  1.4  christos   return crypto_int16_unsigned_topbit_01(crypto_int16_x);
    111  1.4  christos }
    112  1.1  christos 
    113  1.4  christos __attribute__((unused))
    114  1.4  christos static inline
    115  1.4  christos crypto_int16 crypto_int16_bottombit_mask(crypto_int16 crypto_int16_x) {
    116  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    117  1.4  christos   __asm__ ("andw $1,%0" : "+r"(crypto_int16_x) : : "cc");
    118  1.4  christos   return -crypto_int16_x;
    119  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    120  1.4  christos   crypto_int16 crypto_int16_y;
    121  1.4  christos   __asm__ ("sbfx %w0,%w1,0,1" : "=r"(crypto_int16_y) : "r"(crypto_int16_x) : );
    122  1.4  christos   return crypto_int16_y;
    123  1.4  christos #else
    124  1.4  christos   crypto_int16_x &= 1 ^ crypto_int16_optblocker;
    125  1.4  christos   return -crypto_int16_x;
    126  1.4  christos #endif
    127  1.1  christos }
    128  1.1  christos 
    129  1.4  christos __attribute__((unused))
    130  1.4  christos static inline
    131  1.4  christos crypto_int16 crypto_int16_bottombit_01(crypto_int16 crypto_int16_x) {
    132  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    133  1.4  christos   __asm__ ("andw $1,%0" : "+r"(crypto_int16_x) : : "cc");
    134  1.4  christos   return crypto_int16_x;
    135  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    136  1.4  christos   crypto_int16 crypto_int16_y;
    137  1.4  christos   __asm__ ("ubfx %w0,%w1,0,1" : "=r"(crypto_int16_y) : "r"(crypto_int16_x) : );
    138  1.4  christos   return crypto_int16_y;
    139  1.4  christos #else
    140  1.4  christos   crypto_int16_x &= 1 ^ crypto_int16_optblocker;
    141  1.4  christos   return crypto_int16_x;
    142  1.4  christos #endif
    143  1.4  christos }
    144  1.1  christos 
    145  1.4  christos __attribute__((unused))
    146  1.4  christos static inline
    147  1.4  christos crypto_int16 crypto_int16_bitinrangepublicpos_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) {
    148  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    149  1.4  christos   __asm__ ("sarw %%cl,%0" : "+r"(crypto_int16_x) : "c"(crypto_int16_s) : "cc");
    150  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    151  1.4  christos   __asm__ ("sxth %w0,%w0\n asr %w0,%w0,%w1" : "+&r"(crypto_int16_x) : "r"(crypto_int16_s) : );
    152  1.4  christos #else
    153  1.4  christos   crypto_int16_x >>= crypto_int16_s ^ crypto_int16_optblocker;
    154  1.4  christos #endif
    155  1.4  christos   return crypto_int16_bottombit_mask(crypto_int16_x);
    156  1.4  christos }
    157  1.1  christos 
    158  1.4  christos __attribute__((unused))
    159  1.4  christos static inline
    160  1.4  christos crypto_int16 crypto_int16_bitinrangepublicpos_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) {
    161  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    162  1.4  christos   __asm__ ("sarw %%cl,%0" : "+r"(crypto_int16_x) : "c"(crypto_int16_s) : "cc");
    163  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    164  1.4  christos   __asm__ ("sxth %w0,%w0\n asr %w0,%w0,%w1" : "+&r"(crypto_int16_x) : "r"(crypto_int16_s) : );
    165  1.4  christos #else
    166  1.4  christos   crypto_int16_x >>= crypto_int16_s ^ crypto_int16_optblocker;
    167  1.4  christos #endif
    168  1.4  christos   return crypto_int16_bottombit_01(crypto_int16_x);
    169  1.1  christos }
    170  1.1  christos 
    171  1.4  christos __attribute__((unused))
    172  1.4  christos static inline
    173  1.4  christos crypto_int16 crypto_int16_shlmod(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) {
    174  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    175  1.4  christos   crypto_int16_s &= 15;
    176  1.4  christos   __asm__ ("shlw %%cl,%0" : "+r"(crypto_int16_x) : "c"(crypto_int16_s) : "cc");
    177  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    178  1.4  christos   __asm__ ("and %w0,%w0,15\n and %w1,%w1,65535\n lsl %w1,%w1,%w0" : "+&r"(crypto_int16_s), "+r"(crypto_int16_x) : : );
    179  1.4  christos #else
    180  1.4  christos   int crypto_int16_k, crypto_int16_l;
    181  1.4  christos   for (crypto_int16_l = 0,crypto_int16_k = 1;crypto_int16_k < 16;++crypto_int16_l,crypto_int16_k *= 2)
    182  1.4  christos     crypto_int16_x ^= (crypto_int16_x ^ (crypto_int16_x << crypto_int16_k)) & crypto_int16_bitinrangepublicpos_mask(crypto_int16_s,crypto_int16_l);
    183  1.4  christos #endif
    184  1.4  christos   return crypto_int16_x;
    185  1.4  christos }
    186  1.1  christos 
    187  1.4  christos __attribute__((unused))
    188  1.4  christos static inline
    189  1.4  christos crypto_int16 crypto_int16_shrmod(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) {
    190  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    191  1.4  christos   crypto_int16_s &= 15;
    192  1.4  christos   __asm__ ("sarw %%cl,%0" : "+r"(crypto_int16_x) : "c"(crypto_int16_s) : "cc");
    193  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    194  1.4  christos   __asm__ ("and %w0,%w0,15\n sxth %w1,%w1\n asr %w1,%w1,%w0" : "+&r"(crypto_int16_s), "+r"(crypto_int16_x) : : );
    195  1.4  christos #else
    196  1.4  christos   int crypto_int16_k, crypto_int16_l;
    197  1.4  christos   for (crypto_int16_l = 0,crypto_int16_k = 1;crypto_int16_k < 16;++crypto_int16_l,crypto_int16_k *= 2)
    198  1.4  christos     crypto_int16_x ^= (crypto_int16_x ^ (crypto_int16_x >> crypto_int16_k)) & crypto_int16_bitinrangepublicpos_mask(crypto_int16_s,crypto_int16_l);
    199  1.4  christos #endif
    200  1.4  christos   return crypto_int16_x;
    201  1.4  christos }
    202  1.4  christos 
    203  1.4  christos __attribute__((unused))
    204  1.4  christos static inline
    205  1.4  christos crypto_int16 crypto_int16_bitmod_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) {
    206  1.4  christos   crypto_int16_x = crypto_int16_shrmod(crypto_int16_x,crypto_int16_s);
    207  1.4  christos   return crypto_int16_bottombit_mask(crypto_int16_x);
    208  1.4  christos }
    209  1.4  christos 
    210  1.4  christos __attribute__((unused))
    211  1.4  christos static inline
    212  1.4  christos crypto_int16 crypto_int16_bitmod_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) {
    213  1.4  christos   crypto_int16_x = crypto_int16_shrmod(crypto_int16_x,crypto_int16_s);
    214  1.4  christos   return crypto_int16_bottombit_01(crypto_int16_x);
    215  1.4  christos }
    216  1.4  christos 
    217  1.4  christos __attribute__((unused))
    218  1.4  christos static inline
    219  1.4  christos crypto_int16 crypto_int16_nonzero_mask(crypto_int16 crypto_int16_x) {
    220  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    221  1.4  christos   crypto_int16 crypto_int16_q,crypto_int16_z;
    222  1.4  christos   __asm__ ("xorw %0,%0\n movw $-1,%1\n testw %2,%2\n cmovnew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x) : "cc");
    223  1.4  christos   return crypto_int16_z;
    224  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    225  1.4  christos   crypto_int16 crypto_int16_z;
    226  1.4  christos   __asm__ ("tst %w1,65535\n csetm %w0,ne" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc");
    227  1.4  christos   return crypto_int16_z;
    228  1.4  christos #else
    229  1.4  christos   crypto_int16_x |= -crypto_int16_x;
    230  1.4  christos   return crypto_int16_negative_mask(crypto_int16_x);
    231  1.4  christos #endif
    232  1.4  christos }
    233  1.1  christos 
    234  1.4  christos __attribute__((unused))
    235  1.4  christos static inline
    236  1.4  christos crypto_int16 crypto_int16_nonzero_01(crypto_int16 crypto_int16_x) {
    237  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    238  1.4  christos   crypto_int16 crypto_int16_q,crypto_int16_z;
    239  1.4  christos   __asm__ ("xorw %0,%0\n movw $1,%1\n testw %2,%2\n cmovnew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x) : "cc");
    240  1.4  christos   return crypto_int16_z;
    241  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    242  1.4  christos   crypto_int16 crypto_int16_z;
    243  1.4  christos   __asm__ ("tst %w1,65535\n cset %w0,ne" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc");
    244  1.4  christos   return crypto_int16_z;
    245  1.4  christos #else
    246  1.4  christos   crypto_int16_x |= -crypto_int16_x;
    247  1.4  christos   return crypto_int16_unsigned_topbit_01(crypto_int16_x);
    248  1.4  christos #endif
    249  1.4  christos }
    250  1.1  christos 
    251  1.4  christos __attribute__((unused))
    252  1.4  christos static inline
    253  1.4  christos crypto_int16 crypto_int16_positive_mask(crypto_int16 crypto_int16_x) {
    254  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    255  1.4  christos   crypto_int16 crypto_int16_q,crypto_int16_z;
    256  1.4  christos   __asm__ ("xorw %0,%0\n movw $-1,%1\n testw %2,%2\n cmovgw %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x) : "cc");
    257  1.4  christos   return crypto_int16_z;
    258  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    259  1.4  christos   crypto_int16 crypto_int16_z;
    260  1.4  christos   __asm__ ("sxth %w0,%w1\n cmp %w0,0\n csetm %w0,gt" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc");
    261  1.4  christos   return crypto_int16_z;
    262  1.4  christos #else
    263  1.4  christos   crypto_int16 crypto_int16_z = -crypto_int16_x;
    264  1.4  christos   crypto_int16_z ^= crypto_int16_x & crypto_int16_z;
    265  1.4  christos   return crypto_int16_negative_mask(crypto_int16_z);
    266  1.4  christos #endif
    267  1.1  christos }
    268  1.1  christos 
    269  1.4  christos __attribute__((unused))
    270  1.4  christos static inline
    271  1.4  christos crypto_int16 crypto_int16_positive_01(crypto_int16 crypto_int16_x) {
    272  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    273  1.4  christos   crypto_int16 crypto_int16_q,crypto_int16_z;
    274  1.4  christos   __asm__ ("xorw %0,%0\n movw $1,%1\n testw %2,%2\n cmovgw %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x) : "cc");
    275  1.4  christos   return crypto_int16_z;
    276  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    277  1.4  christos   crypto_int16 crypto_int16_z;
    278  1.4  christos   __asm__ ("sxth %w0,%w1\n cmp %w0,0\n cset %w0,gt" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc");
    279  1.4  christos   return crypto_int16_z;
    280  1.4  christos #else
    281  1.4  christos   crypto_int16 crypto_int16_z = -crypto_int16_x;
    282  1.4  christos   crypto_int16_z ^= crypto_int16_x & crypto_int16_z;
    283  1.4  christos   return crypto_int16_unsigned_topbit_01(crypto_int16_z);
    284  1.4  christos #endif
    285  1.4  christos }
    286  1.1  christos 
    287  1.4  christos __attribute__((unused))
    288  1.4  christos static inline
    289  1.4  christos crypto_int16 crypto_int16_zero_mask(crypto_int16 crypto_int16_x) {
    290  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    291  1.4  christos   crypto_int16 crypto_int16_q,crypto_int16_z;
    292  1.4  christos   __asm__ ("xorw %0,%0\n movw $-1,%1\n testw %2,%2\n cmovew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x) : "cc");
    293  1.4  christos   return crypto_int16_z;
    294  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    295  1.4  christos   crypto_int16 crypto_int16_z;
    296  1.4  christos   __asm__ ("tst %w1,65535\n csetm %w0,eq" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc");
    297  1.4  christos   return crypto_int16_z;
    298  1.4  christos #else
    299  1.4  christos   return ~crypto_int16_nonzero_mask(crypto_int16_x);
    300  1.4  christos #endif
    301  1.1  christos }
    302  1.1  christos 
    303  1.4  christos __attribute__((unused))
    304  1.4  christos static inline
    305  1.4  christos crypto_int16 crypto_int16_zero_01(crypto_int16 crypto_int16_x) {
    306  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    307  1.4  christos   crypto_int16 crypto_int16_q,crypto_int16_z;
    308  1.4  christos   __asm__ ("xorw %0,%0\n movw $1,%1\n testw %2,%2\n cmovew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x) : "cc");
    309  1.4  christos   return crypto_int16_z;
    310  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    311  1.4  christos   crypto_int16 crypto_int16_z;
    312  1.4  christos   __asm__ ("tst %w1,65535\n cset %w0,eq" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc");
    313  1.4  christos   return crypto_int16_z;
    314  1.4  christos #else
    315  1.4  christos   return 1-crypto_int16_nonzero_01(crypto_int16_x);
    316  1.4  christos #endif
    317  1.4  christos }
    318  1.1  christos 
    319  1.4  christos __attribute__((unused))
    320  1.4  christos static inline
    321  1.4  christos crypto_int16 crypto_int16_unequal_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
    322  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    323  1.4  christos   crypto_int16 crypto_int16_q,crypto_int16_z;
    324  1.4  christos   __asm__ ("xorw %0,%0\n movw $-1,%1\n cmpw %3,%2\n cmovnew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc");
    325  1.4  christos   return crypto_int16_z;
    326  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    327  1.4  christos   crypto_int16 crypto_int16_z;
    328  1.4  christos   __asm__ ("and %w0,%w1,65535\n cmp %w0,%w2,uxth\n csetm %w0,ne" : "=&r"(crypto_int16_z) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc");
    329  1.4  christos   return crypto_int16_z;
    330  1.4  christos #else
    331  1.4  christos   return crypto_int16_nonzero_mask(crypto_int16_x ^ crypto_int16_y);
    332  1.4  christos #endif
    333  1.4  christos }
    334  1.1  christos 
    335  1.4  christos __attribute__((unused))
    336  1.4  christos static inline
    337  1.4  christos crypto_int16 crypto_int16_unequal_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
    338  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    339  1.4  christos   crypto_int16 crypto_int16_q,crypto_int16_z;
    340  1.4  christos   __asm__ ("xorw %0,%0\n movw $1,%1\n cmpw %3,%2\n cmovnew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc");
    341  1.4  christos   return crypto_int16_z;
    342  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    343  1.4  christos   crypto_int16 crypto_int16_z;
    344  1.4  christos   __asm__ ("and %w0,%w1,65535\n cmp %w0,%w2,uxth\n cset %w0,ne" : "=&r"(crypto_int16_z) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc");
    345  1.4  christos   return crypto_int16_z;
    346  1.4  christos #else
    347  1.4  christos   return crypto_int16_nonzero_01(crypto_int16_x ^ crypto_int16_y);
    348  1.4  christos #endif
    349  1.1  christos }
    350  1.1  christos 
    351  1.4  christos __attribute__((unused))
    352  1.4  christos static inline
    353  1.4  christos crypto_int16 crypto_int16_equal_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
    354  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    355  1.4  christos   crypto_int16 crypto_int16_q,crypto_int16_z;
    356  1.4  christos   __asm__ ("xorw %0,%0\n movw $-1,%1\n cmpw %3,%2\n cmovew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc");
    357  1.4  christos   return crypto_int16_z;
    358  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    359  1.4  christos   crypto_int16 crypto_int16_z;
    360  1.4  christos   __asm__ ("and %w0,%w1,65535\n cmp %w0,%w2,uxth\n csetm %w0,eq" : "=&r"(crypto_int16_z) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc");
    361  1.4  christos   return crypto_int16_z;
    362  1.4  christos #else
    363  1.4  christos   return ~crypto_int16_unequal_mask(crypto_int16_x,crypto_int16_y);
    364  1.4  christos #endif
    365  1.4  christos }
    366  1.1  christos 
    367  1.4  christos __attribute__((unused))
    368  1.4  christos static inline
    369  1.4  christos crypto_int16 crypto_int16_equal_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
    370  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    371  1.4  christos   crypto_int16 crypto_int16_q,crypto_int16_z;
    372  1.4  christos   __asm__ ("xorw %0,%0\n movw $1,%1\n cmpw %3,%2\n cmovew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc");
    373  1.4  christos   return crypto_int16_z;
    374  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    375  1.4  christos   crypto_int16 crypto_int16_z;
    376  1.4  christos   __asm__ ("and %w0,%w1,65535\n cmp %w0,%w2,uxth\n cset %w0,eq" : "=&r"(crypto_int16_z) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc");
    377  1.4  christos   return crypto_int16_z;
    378  1.4  christos #else
    379  1.4  christos   return 1-crypto_int16_unequal_01(crypto_int16_x,crypto_int16_y);
    380  1.4  christos #endif
    381  1.1  christos }
    382  1.1  christos 
    383  1.4  christos __attribute__((unused))
    384  1.4  christos static inline
    385  1.4  christos crypto_int16 crypto_int16_min(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
    386  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    387  1.4  christos   __asm__ ("cmpw %1,%0\n cmovgw %1,%0" : "+r"(crypto_int16_x) : "r"(crypto_int16_y) : "cc");
    388  1.4  christos   return crypto_int16_x;
    389  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    390  1.4  christos   __asm__ ("sxth %w0,%w0\n cmp %w0,%w1,sxth\n csel %w0,%w0,%w1,lt" : "+&r"(crypto_int16_x) : "r"(crypto_int16_y) : "cc");
    391  1.4  christos   return crypto_int16_x;
    392  1.4  christos #else
    393  1.4  christos   crypto_int16 crypto_int16_r = crypto_int16_y ^ crypto_int16_x;
    394  1.4  christos   crypto_int16 crypto_int16_z = crypto_int16_y - crypto_int16_x;
    395  1.4  christos   crypto_int16_z ^= crypto_int16_r & (crypto_int16_z ^ crypto_int16_y);
    396  1.4  christos   crypto_int16_z = crypto_int16_negative_mask(crypto_int16_z);
    397  1.4  christos   crypto_int16_z &= crypto_int16_r;
    398  1.4  christos   return crypto_int16_x ^ crypto_int16_z;
    399  1.4  christos #endif
    400  1.4  christos }
    401  1.1  christos 
    402  1.4  christos __attribute__((unused))
    403  1.4  christos static inline
    404  1.4  christos crypto_int16 crypto_int16_max(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
    405  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    406  1.4  christos   __asm__ ("cmpw %1,%0\n cmovlw %1,%0" : "+r"(crypto_int16_x) : "r"(crypto_int16_y) : "cc");
    407  1.4  christos   return crypto_int16_x;
    408  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    409  1.4  christos   __asm__ ("sxth %w0,%w0\n cmp %w0,%w1,sxth\n csel %w0,%w1,%w0,lt" : "+&r"(crypto_int16_x) : "r"(crypto_int16_y) : "cc");
    410  1.4  christos   return crypto_int16_x;
    411  1.4  christos #else
    412  1.4  christos   crypto_int16 crypto_int16_r = crypto_int16_y ^ crypto_int16_x;
    413  1.4  christos   crypto_int16 crypto_int16_z = crypto_int16_y - crypto_int16_x;
    414  1.4  christos   crypto_int16_z ^= crypto_int16_r & (crypto_int16_z ^ crypto_int16_y);
    415  1.4  christos   crypto_int16_z = crypto_int16_negative_mask(crypto_int16_z);
    416  1.4  christos   crypto_int16_z &= crypto_int16_r;
    417  1.4  christos   return crypto_int16_y ^ crypto_int16_z;
    418  1.4  christos #endif
    419  1.4  christos }
    420  1.1  christos 
    421  1.4  christos __attribute__((unused))
    422  1.4  christos static inline
    423  1.4  christos void crypto_int16_minmax(crypto_int16 *crypto_int16_p,crypto_int16 *crypto_int16_q) {
    424  1.4  christos   crypto_int16 crypto_int16_x = *crypto_int16_p;
    425  1.4  christos   crypto_int16 crypto_int16_y = *crypto_int16_q;
    426  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    427  1.4  christos   crypto_int16 crypto_int16_z;
    428  1.4  christos   __asm__ ("cmpw %2,%1\n movw %1,%0\n cmovgw %2,%1\n cmovgw %0,%2" : "=&r"(crypto_int16_z), "+&r"(crypto_int16_x), "+r"(crypto_int16_y) : : "cc");
    429  1.4  christos   *crypto_int16_p = crypto_int16_x;
    430  1.4  christos   *crypto_int16_q = crypto_int16_y;
    431  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    432  1.4  christos   crypto_int16 crypto_int16_r, crypto_int16_s;
    433  1.4  christos   __asm__ ("sxth %w0,%w0\n cmp %w0,%w3,sxth\n csel %w1,%w0,%w3,lt\n csel %w2,%w3,%w0,lt" : "+&r"(crypto_int16_x), "=&r"(crypto_int16_r), "=r"(crypto_int16_s) : "r"(crypto_int16_y) : "cc");
    434  1.4  christos   *crypto_int16_p = crypto_int16_r;
    435  1.4  christos   *crypto_int16_q = crypto_int16_s;
    436  1.4  christos #else
    437  1.4  christos   crypto_int16 crypto_int16_r = crypto_int16_y ^ crypto_int16_x;
    438  1.4  christos   crypto_int16 crypto_int16_z = crypto_int16_y - crypto_int16_x;
    439  1.4  christos   crypto_int16_z ^= crypto_int16_r & (crypto_int16_z ^ crypto_int16_y);
    440  1.4  christos   crypto_int16_z = crypto_int16_negative_mask(crypto_int16_z);
    441  1.4  christos   crypto_int16_z &= crypto_int16_r;
    442  1.4  christos   crypto_int16_x ^= crypto_int16_z;
    443  1.4  christos   crypto_int16_y ^= crypto_int16_z;
    444  1.4  christos   *crypto_int16_p = crypto_int16_x;
    445  1.4  christos   *crypto_int16_q = crypto_int16_y;
    446  1.4  christos #endif
    447  1.4  christos }
    448  1.1  christos 
    449  1.4  christos __attribute__((unused))
    450  1.4  christos static inline
    451  1.4  christos crypto_int16 crypto_int16_smaller_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
    452  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    453  1.4  christos   crypto_int16 crypto_int16_q,crypto_int16_z;
    454  1.4  christos   __asm__ ("xorw %0,%0\n movw $-1,%1\n cmpw %3,%2\n cmovlw %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc");
    455  1.4  christos   return crypto_int16_z;
    456  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    457  1.4  christos   crypto_int16 crypto_int16_z;
    458  1.4  christos   __asm__ ("sxth %w0,%w1\n cmp %w0,%w2,sxth\n csetm %w0,lt" : "=&r"(crypto_int16_z) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc");
    459  1.4  christos   return crypto_int16_z;
    460  1.1  christos #else
    461  1.4  christos   crypto_int16 crypto_int16_r = crypto_int16_x ^ crypto_int16_y;
    462  1.4  christos   crypto_int16 crypto_int16_z = crypto_int16_x - crypto_int16_y;
    463  1.4  christos   crypto_int16_z ^= crypto_int16_r & (crypto_int16_z ^ crypto_int16_x);
    464  1.4  christos   return crypto_int16_negative_mask(crypto_int16_z);
    465  1.1  christos #endif
    466  1.4  christos }
    467  1.1  christos 
    468  1.4  christos __attribute__((unused))
    469  1.4  christos static inline
    470  1.4  christos crypto_int16 crypto_int16_smaller_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
    471  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    472  1.4  christos   crypto_int16 crypto_int16_q,crypto_int16_z;
    473  1.4  christos   __asm__ ("xorw %0,%0\n movw $1,%1\n cmpw %3,%2\n cmovlw %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc");
    474  1.4  christos   return crypto_int16_z;
    475  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    476  1.4  christos   crypto_int16 crypto_int16_z;
    477  1.4  christos   __asm__ ("sxth %w0,%w1\n cmp %w0,%w2,sxth\n cset %w0,lt" : "=&r"(crypto_int16_z) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc");
    478  1.4  christos   return crypto_int16_z;
    479  1.1  christos #else
    480  1.4  christos   crypto_int16 crypto_int16_r = crypto_int16_x ^ crypto_int16_y;
    481  1.4  christos   crypto_int16 crypto_int16_z = crypto_int16_x - crypto_int16_y;
    482  1.4  christos   crypto_int16_z ^= crypto_int16_r & (crypto_int16_z ^ crypto_int16_x);
    483  1.4  christos   return crypto_int16_unsigned_topbit_01(crypto_int16_z);
    484  1.1  christos #endif
    485  1.4  christos }
    486  1.1  christos 
    487  1.4  christos __attribute__((unused))
    488  1.4  christos static inline
    489  1.4  christos crypto_int16 crypto_int16_leq_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
    490  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    491  1.4  christos   crypto_int16 crypto_int16_q,crypto_int16_z;
    492  1.4  christos   __asm__ ("xorw %0,%0\n movw $-1,%1\n cmpw %3,%2\n cmovlew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc");
    493  1.4  christos   return crypto_int16_z;
    494  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    495  1.4  christos   crypto_int16 crypto_int16_z;
    496  1.4  christos   __asm__ ("sxth %w0,%w1\n cmp %w0,%w2,sxth\n csetm %w0,le" : "=&r"(crypto_int16_z) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc");
    497  1.4  christos   return crypto_int16_z;
    498  1.1  christos #else
    499  1.4  christos   return ~crypto_int16_smaller_mask(crypto_int16_y,crypto_int16_x);
    500  1.1  christos #endif
    501  1.4  christos }
    502  1.1  christos 
    503  1.4  christos __attribute__((unused))
    504  1.4  christos static inline
    505  1.4  christos crypto_int16 crypto_int16_leq_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
    506  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    507  1.4  christos   crypto_int16 crypto_int16_q,crypto_int16_z;
    508  1.4  christos   __asm__ ("xorw %0,%0\n movw $1,%1\n cmpw %3,%2\n cmovlew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc");
    509  1.4  christos   return crypto_int16_z;
    510  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    511  1.4  christos   crypto_int16 crypto_int16_z;
    512  1.4  christos   __asm__ ("sxth %w0,%w1\n cmp %w0,%w2,sxth\n cset %w0,le" : "=&r"(crypto_int16_z) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc");
    513  1.4  christos   return crypto_int16_z;
    514  1.1  christos #else
    515  1.4  christos   return 1-crypto_int16_smaller_01(crypto_int16_y,crypto_int16_x);
    516  1.1  christos #endif
    517  1.4  christos }
    518  1.1  christos 
    519  1.4  christos __attribute__((unused))
    520  1.4  christos static inline
    521  1.4  christos int crypto_int16_ones_num(crypto_int16 crypto_int16_x) {
    522  1.4  christos   crypto_int16_unsigned crypto_int16_y = crypto_int16_x;
    523  1.4  christos   const crypto_int16 C0 = 0x5555;
    524  1.4  christos   const crypto_int16 C1 = 0x3333;
    525  1.4  christos   const crypto_int16 C2 = 0x0f0f;
    526  1.4  christos   crypto_int16_y -= ((crypto_int16_y >> 1) & C0);
    527  1.4  christos   crypto_int16_y = (crypto_int16_y & C1) + ((crypto_int16_y >> 2) & C1);
    528  1.4  christos   crypto_int16_y = (crypto_int16_y + (crypto_int16_y >> 4)) & C2;
    529  1.4  christos   crypto_int16_y = (crypto_int16_y + (crypto_int16_y >> 8)) & 0xff;
    530  1.4  christos   return crypto_int16_y;
    531  1.4  christos }
    532  1.4  christos 
    533  1.4  christos __attribute__((unused))
    534  1.4  christos static inline
    535  1.4  christos int crypto_int16_bottomzeros_num(crypto_int16 crypto_int16_x) {
    536  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    537  1.4  christos   crypto_int16 fallback = 16;
    538  1.4  christos   __asm__ ("bsfw %0,%0\n cmovew %1,%0" : "+&r"(crypto_int16_x) : "r"(fallback) : "cc");
    539  1.4  christos   return crypto_int16_x;
    540  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    541  1.4  christos   int64_t crypto_int16_y;
    542  1.4  christos   __asm__ ("orr %w0,%w1,-65536\n rbit %w0,%w0\n clz %w0,%w0" : "=r"(crypto_int16_y) : "r"(crypto_int16_x) : );
    543  1.4  christos   return crypto_int16_y;
    544  1.4  christos #else
    545  1.4  christos   crypto_int16 crypto_int16_y = crypto_int16_x ^ (crypto_int16_x-1);
    546  1.4  christos   crypto_int16_y = ((crypto_int16) crypto_int16_y) >> 1;
    547  1.4  christos   crypto_int16_y &= ~(crypto_int16_x & (((crypto_int16) 1) << (16-1)));
    548  1.4  christos   return crypto_int16_ones_num(crypto_int16_y);
    549  1.1  christos #endif
    550  1.4  christos }
    551  1.1  christos 
    552  1.1  christos #endif
    553  1.1  christos 
    554  1.4  christos /* from supercop-20240808/cryptoint/crypto_int32.h */
    555  1.4  christos /* auto-generated: cd cryptoint; ./autogen */
    556  1.4  christos /* cryptoint 20240806 */
    557  1.1  christos 
    558  1.4  christos #ifndef crypto_int32_h
    559  1.4  christos #define crypto_int32_h
    560  1.1  christos 
    561  1.4  christos #define crypto_int32 int32_t
    562  1.4  christos #define crypto_int32_unsigned uint32_t
    563  1.1  christos 
    564  1.1  christos 
    565  1.1  christos 
    566  1.4  christos __attribute__((unused))
    567  1.4  christos static inline
    568  1.4  christos crypto_int32 crypto_int32_load(const unsigned char *crypto_int32_s) {
    569  1.4  christos   crypto_int32 crypto_int32_z = 0;
    570  1.4  christos   crypto_int32_z |= ((crypto_int32) (*crypto_int32_s++)) << 0;
    571  1.4  christos   crypto_int32_z |= ((crypto_int32) (*crypto_int32_s++)) << 8;
    572  1.4  christos   crypto_int32_z |= ((crypto_int32) (*crypto_int32_s++)) << 16;
    573  1.4  christos   crypto_int32_z |= ((crypto_int32) (*crypto_int32_s++)) << 24;
    574  1.4  christos   return crypto_int32_z;
    575  1.1  christos }
    576  1.1  christos 
    577  1.4  christos __attribute__((unused))
    578  1.4  christos static inline
    579  1.4  christos void crypto_int32_store(unsigned char *crypto_int32_s,crypto_int32 crypto_int32_x) {
    580  1.4  christos   *crypto_int32_s++ = crypto_int32_x >> 0;
    581  1.4  christos   *crypto_int32_s++ = crypto_int32_x >> 8;
    582  1.4  christos   *crypto_int32_s++ = crypto_int32_x >> 16;
    583  1.4  christos   *crypto_int32_s++ = crypto_int32_x >> 24;
    584  1.4  christos }
    585  1.1  christos 
    586  1.4  christos __attribute__((unused))
    587  1.4  christos static inline
    588  1.4  christos crypto_int32 crypto_int32_negative_mask(crypto_int32 crypto_int32_x) {
    589  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    590  1.4  christos   __asm__ ("sarl $31,%0" : "+r"(crypto_int32_x) : : "cc");
    591  1.4  christos   return crypto_int32_x;
    592  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    593  1.4  christos   crypto_int32 crypto_int32_y;
    594  1.4  christos   __asm__ ("asr %w0,%w1,31" : "=r"(crypto_int32_y) : "r"(crypto_int32_x) : );
    595  1.4  christos   return crypto_int32_y;
    596  1.4  christos #else
    597  1.4  christos   crypto_int32_x >>= 32-6;
    598  1.4  christos   crypto_int32_x ^= crypto_int32_optblocker;
    599  1.4  christos   crypto_int32_x >>= 5;
    600  1.4  christos   return crypto_int32_x;
    601  1.4  christos #endif
    602  1.4  christos }
    603  1.1  christos 
    604  1.4  christos __attribute__((unused))
    605  1.4  christos static inline
    606  1.4  christos crypto_int32_unsigned crypto_int32_unsigned_topbit_01(crypto_int32_unsigned crypto_int32_x) {
    607  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    608  1.4  christos   __asm__ ("shrl $31,%0" : "+r"(crypto_int32_x) : : "cc");
    609  1.4  christos   return crypto_int32_x;
    610  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    611  1.4  christos   crypto_int32 crypto_int32_y;
    612  1.4  christos   __asm__ ("lsr %w0,%w1,31" : "=r"(crypto_int32_y) : "r"(crypto_int32_x) : );
    613  1.4  christos   return crypto_int32_y;
    614  1.4  christos #else
    615  1.4  christos   crypto_int32_x >>= 32-6;
    616  1.4  christos   crypto_int32_x ^= crypto_int32_optblocker;
    617  1.4  christos   crypto_int32_x >>= 5;
    618  1.4  christos   return crypto_int32_x;
    619  1.1  christos #endif
    620  1.4  christos }
    621  1.1  christos 
    622  1.4  christos __attribute__((unused))
    623  1.4  christos static inline
    624  1.4  christos crypto_int32 crypto_int32_negative_01(crypto_int32 crypto_int32_x) {
    625  1.4  christos   return crypto_int32_unsigned_topbit_01(crypto_int32_x);
    626  1.4  christos }
    627  1.1  christos 
    628  1.4  christos __attribute__((unused))
    629  1.4  christos static inline
    630  1.4  christos crypto_int32 crypto_int32_topbit_mask(crypto_int32 crypto_int32_x) {
    631  1.4  christos   return crypto_int32_negative_mask(crypto_int32_x);
    632  1.1  christos }
    633  1.1  christos 
    634  1.4  christos __attribute__((unused))
    635  1.4  christos static inline
    636  1.4  christos crypto_int32 crypto_int32_topbit_01(crypto_int32 crypto_int32_x) {
    637  1.4  christos   return crypto_int32_unsigned_topbit_01(crypto_int32_x);
    638  1.4  christos }
    639  1.1  christos 
    640  1.4  christos __attribute__((unused))
    641  1.4  christos static inline
    642  1.4  christos crypto_int32 crypto_int32_bottombit_mask(crypto_int32 crypto_int32_x) {
    643  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    644  1.4  christos   __asm__ ("andl $1,%0" : "+r"(crypto_int32_x) : : "cc");
    645  1.4  christos   return -crypto_int32_x;
    646  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    647  1.4  christos   crypto_int32 crypto_int32_y;
    648  1.4  christos   __asm__ ("sbfx %w0,%w1,0,1" : "=r"(crypto_int32_y) : "r"(crypto_int32_x) : );
    649  1.4  christos   return crypto_int32_y;
    650  1.4  christos #else
    651  1.4  christos   crypto_int32_x &= 1 ^ crypto_int32_optblocker;
    652  1.4  christos   return -crypto_int32_x;
    653  1.1  christos #endif
    654  1.4  christos }
    655  1.1  christos 
    656  1.4  christos __attribute__((unused))
    657  1.4  christos static inline
    658  1.4  christos crypto_int32 crypto_int32_bottombit_01(crypto_int32 crypto_int32_x) {
    659  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    660  1.4  christos   __asm__ ("andl $1,%0" : "+r"(crypto_int32_x) : : "cc");
    661  1.4  christos   return crypto_int32_x;
    662  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    663  1.4  christos   crypto_int32 crypto_int32_y;
    664  1.4  christos   __asm__ ("ubfx %w0,%w1,0,1" : "=r"(crypto_int32_y) : "r"(crypto_int32_x) : );
    665  1.4  christos   return crypto_int32_y;
    666  1.4  christos #else
    667  1.4  christos   crypto_int32_x &= 1 ^ crypto_int32_optblocker;
    668  1.4  christos   return crypto_int32_x;
    669  1.4  christos #endif
    670  1.4  christos }
    671  1.1  christos 
    672  1.4  christos __attribute__((unused))
    673  1.4  christos static inline
    674  1.4  christos crypto_int32 crypto_int32_bitinrangepublicpos_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) {
    675  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    676  1.4  christos   __asm__ ("sarl %%cl,%0" : "+r"(crypto_int32_x) : "c"(crypto_int32_s) : "cc");
    677  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    678  1.4  christos   __asm__ ("asr %w0,%w0,%w1" : "+r"(crypto_int32_x) : "r"(crypto_int32_s) : );
    679  1.4  christos #else
    680  1.4  christos   crypto_int32_x >>= crypto_int32_s ^ crypto_int32_optblocker;
    681  1.4  christos #endif
    682  1.4  christos   return crypto_int32_bottombit_mask(crypto_int32_x);
    683  1.4  christos }
    684  1.1  christos 
    685  1.4  christos __attribute__((unused))
    686  1.4  christos static inline
    687  1.4  christos crypto_int32 crypto_int32_bitinrangepublicpos_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) {
    688  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    689  1.4  christos   __asm__ ("sarl %%cl,%0" : "+r"(crypto_int32_x) : "c"(crypto_int32_s) : "cc");
    690  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    691  1.4  christos   __asm__ ("asr %w0,%w0,%w1" : "+r"(crypto_int32_x) : "r"(crypto_int32_s) : );
    692  1.4  christos #else
    693  1.4  christos   crypto_int32_x >>= crypto_int32_s ^ crypto_int32_optblocker;
    694  1.4  christos #endif
    695  1.4  christos   return crypto_int32_bottombit_01(crypto_int32_x);
    696  1.4  christos }
    697  1.1  christos 
    698  1.4  christos __attribute__((unused))
    699  1.4  christos static inline
    700  1.4  christos crypto_int32 crypto_int32_shlmod(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) {
    701  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    702  1.4  christos   __asm__ ("shll %%cl,%0" : "+r"(crypto_int32_x) : "c"(crypto_int32_s) : "cc");
    703  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    704  1.4  christos   __asm__ ("lsl %w0,%w0,%w1" : "+r"(crypto_int32_x) : "r"(crypto_int32_s) : );
    705  1.4  christos #else
    706  1.4  christos   int crypto_int32_k, crypto_int32_l;
    707  1.4  christos   for (crypto_int32_l = 0,crypto_int32_k = 1;crypto_int32_k < 32;++crypto_int32_l,crypto_int32_k *= 2)
    708  1.4  christos     crypto_int32_x ^= (crypto_int32_x ^ (crypto_int32_x << crypto_int32_k)) & crypto_int32_bitinrangepublicpos_mask(crypto_int32_s,crypto_int32_l);
    709  1.4  christos #endif
    710  1.4  christos   return crypto_int32_x;
    711  1.1  christos }
    712  1.1  christos 
    713  1.4  christos __attribute__((unused))
    714  1.4  christos static inline
    715  1.4  christos crypto_int32 crypto_int32_shrmod(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) {
    716  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    717  1.4  christos   __asm__ ("sarl %%cl,%0" : "+r"(crypto_int32_x) : "c"(crypto_int32_s) : "cc");
    718  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    719  1.4  christos   __asm__ ("asr %w0,%w0,%w1" : "+r"(crypto_int32_x) : "r"(crypto_int32_s) : );
    720  1.4  christos #else
    721  1.4  christos   int crypto_int32_k, crypto_int32_l;
    722  1.4  christos   for (crypto_int32_l = 0,crypto_int32_k = 1;crypto_int32_k < 32;++crypto_int32_l,crypto_int32_k *= 2)
    723  1.4  christos     crypto_int32_x ^= (crypto_int32_x ^ (crypto_int32_x >> crypto_int32_k)) & crypto_int32_bitinrangepublicpos_mask(crypto_int32_s,crypto_int32_l);
    724  1.4  christos #endif
    725  1.4  christos   return crypto_int32_x;
    726  1.4  christos }
    727  1.4  christos 
    728  1.4  christos __attribute__((unused))
    729  1.4  christos static inline
    730  1.4  christos crypto_int32 crypto_int32_bitmod_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) {
    731  1.4  christos   crypto_int32_x = crypto_int32_shrmod(crypto_int32_x,crypto_int32_s);
    732  1.4  christos   return crypto_int32_bottombit_mask(crypto_int32_x);
    733  1.4  christos }
    734  1.4  christos 
    735  1.4  christos __attribute__((unused))
    736  1.4  christos static inline
    737  1.4  christos crypto_int32 crypto_int32_bitmod_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) {
    738  1.4  christos   crypto_int32_x = crypto_int32_shrmod(crypto_int32_x,crypto_int32_s);
    739  1.4  christos   return crypto_int32_bottombit_01(crypto_int32_x);
    740  1.4  christos }
    741  1.4  christos 
    742  1.4  christos __attribute__((unused))
    743  1.4  christos static inline
    744  1.4  christos crypto_int32 crypto_int32_nonzero_mask(crypto_int32 crypto_int32_x) {
    745  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    746  1.4  christos   crypto_int32 crypto_int32_q,crypto_int32_z;
    747  1.4  christos   __asm__ ("xorl %0,%0\n movl $-1,%1\n testl %2,%2\n cmovnel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x) : "cc");
    748  1.4  christos   return crypto_int32_z;
    749  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    750  1.4  christos   crypto_int32 crypto_int32_z;
    751  1.4  christos   __asm__ ("cmp %w1,0\n csetm %w0,ne" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc");
    752  1.4  christos   return crypto_int32_z;
    753  1.4  christos #else
    754  1.4  christos   crypto_int32_x |= -crypto_int32_x;
    755  1.4  christos   return crypto_int32_negative_mask(crypto_int32_x);
    756  1.1  christos #endif
    757  1.4  christos }
    758  1.1  christos 
    759  1.4  christos __attribute__((unused))
    760  1.4  christos static inline
    761  1.4  christos crypto_int32 crypto_int32_nonzero_01(crypto_int32 crypto_int32_x) {
    762  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    763  1.4  christos   crypto_int32 crypto_int32_q,crypto_int32_z;
    764  1.4  christos   __asm__ ("xorl %0,%0\n movl $1,%1\n testl %2,%2\n cmovnel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x) : "cc");
    765  1.4  christos   return crypto_int32_z;
    766  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    767  1.4  christos   crypto_int32 crypto_int32_z;
    768  1.4  christos   __asm__ ("cmp %w1,0\n cset %w0,ne" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc");
    769  1.4  christos   return crypto_int32_z;
    770  1.4  christos #else
    771  1.4  christos   crypto_int32_x |= -crypto_int32_x;
    772  1.4  christos   return crypto_int32_unsigned_topbit_01(crypto_int32_x);
    773  1.4  christos #endif
    774  1.1  christos }
    775  1.1  christos 
    776  1.4  christos __attribute__((unused))
    777  1.4  christos static inline
    778  1.4  christos crypto_int32 crypto_int32_positive_mask(crypto_int32 crypto_int32_x) {
    779  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    780  1.4  christos   crypto_int32 crypto_int32_q,crypto_int32_z;
    781  1.4  christos   __asm__ ("xorl %0,%0\n movl $-1,%1\n testl %2,%2\n cmovgl %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x) : "cc");
    782  1.4  christos   return crypto_int32_z;
    783  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    784  1.4  christos   crypto_int32 crypto_int32_z;
    785  1.4  christos   __asm__ ("cmp %w1,0\n csetm %w0,gt" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc");
    786  1.4  christos   return crypto_int32_z;
    787  1.4  christos #else
    788  1.4  christos   crypto_int32 crypto_int32_z = -crypto_int32_x;
    789  1.4  christos   crypto_int32_z ^= crypto_int32_x & crypto_int32_z;
    790  1.4  christos   return crypto_int32_negative_mask(crypto_int32_z);
    791  1.4  christos #endif
    792  1.4  christos }
    793  1.1  christos 
    794  1.4  christos __attribute__((unused))
    795  1.4  christos static inline
    796  1.4  christos crypto_int32 crypto_int32_positive_01(crypto_int32 crypto_int32_x) {
    797  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    798  1.4  christos   crypto_int32 crypto_int32_q,crypto_int32_z;
    799  1.4  christos   __asm__ ("xorl %0,%0\n movl $1,%1\n testl %2,%2\n cmovgl %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x) : "cc");
    800  1.4  christos   return crypto_int32_z;
    801  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    802  1.4  christos   crypto_int32 crypto_int32_z;
    803  1.4  christos   __asm__ ("cmp %w1,0\n cset %w0,gt" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc");
    804  1.4  christos   return crypto_int32_z;
    805  1.4  christos #else
    806  1.4  christos   crypto_int32 crypto_int32_z = -crypto_int32_x;
    807  1.4  christos   crypto_int32_z ^= crypto_int32_x & crypto_int32_z;
    808  1.4  christos   return crypto_int32_unsigned_topbit_01(crypto_int32_z);
    809  1.4  christos #endif
    810  1.4  christos }
    811  1.1  christos 
    812  1.4  christos __attribute__((unused))
    813  1.4  christos static inline
    814  1.4  christos crypto_int32 crypto_int32_zero_mask(crypto_int32 crypto_int32_x) {
    815  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    816  1.4  christos   crypto_int32 crypto_int32_q,crypto_int32_z;
    817  1.4  christos   __asm__ ("xorl %0,%0\n movl $-1,%1\n testl %2,%2\n cmovel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x) : "cc");
    818  1.4  christos   return crypto_int32_z;
    819  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    820  1.4  christos   crypto_int32 crypto_int32_z;
    821  1.4  christos   __asm__ ("cmp %w1,0\n csetm %w0,eq" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc");
    822  1.4  christos   return crypto_int32_z;
    823  1.4  christos #else
    824  1.4  christos   return ~crypto_int32_nonzero_mask(crypto_int32_x);
    825  1.4  christos #endif
    826  1.4  christos }
    827  1.1  christos 
    828  1.4  christos __attribute__((unused))
    829  1.4  christos static inline
    830  1.4  christos crypto_int32 crypto_int32_zero_01(crypto_int32 crypto_int32_x) {
    831  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    832  1.4  christos   crypto_int32 crypto_int32_q,crypto_int32_z;
    833  1.4  christos   __asm__ ("xorl %0,%0\n movl $1,%1\n testl %2,%2\n cmovel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x) : "cc");
    834  1.4  christos   return crypto_int32_z;
    835  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    836  1.4  christos   crypto_int32 crypto_int32_z;
    837  1.4  christos   __asm__ ("cmp %w1,0\n cset %w0,eq" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc");
    838  1.4  christos   return crypto_int32_z;
    839  1.4  christos #else
    840  1.4  christos   return 1-crypto_int32_nonzero_01(crypto_int32_x);
    841  1.4  christos #endif
    842  1.1  christos }
    843  1.1  christos 
    844  1.4  christos __attribute__((unused))
    845  1.4  christos static inline
    846  1.4  christos crypto_int32 crypto_int32_unequal_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
    847  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    848  1.4  christos   crypto_int32 crypto_int32_q,crypto_int32_z;
    849  1.4  christos   __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpl %3,%2\n cmovnel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
    850  1.4  christos   return crypto_int32_z;
    851  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    852  1.4  christos   crypto_int32 crypto_int32_z;
    853  1.4  christos   __asm__ ("cmp %w1,%w2\n csetm %w0,ne" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
    854  1.4  christos   return crypto_int32_z;
    855  1.4  christos #else
    856  1.4  christos   return crypto_int32_nonzero_mask(crypto_int32_x ^ crypto_int32_y);
    857  1.4  christos #endif
    858  1.4  christos }
    859  1.1  christos 
    860  1.4  christos __attribute__((unused))
    861  1.4  christos static inline
    862  1.4  christos crypto_int32 crypto_int32_unequal_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
    863  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    864  1.4  christos   crypto_int32 crypto_int32_q,crypto_int32_z;
    865  1.4  christos   __asm__ ("xorl %0,%0\n movl $1,%1\n cmpl %3,%2\n cmovnel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
    866  1.4  christos   return crypto_int32_z;
    867  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    868  1.4  christos   crypto_int32 crypto_int32_z;
    869  1.4  christos   __asm__ ("cmp %w1,%w2\n cset %w0,ne" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
    870  1.4  christos   return crypto_int32_z;
    871  1.4  christos #else
    872  1.4  christos   return crypto_int32_nonzero_01(crypto_int32_x ^ crypto_int32_y);
    873  1.4  christos #endif
    874  1.4  christos }
    875  1.1  christos 
    876  1.4  christos __attribute__((unused))
    877  1.4  christos static inline
    878  1.4  christos crypto_int32 crypto_int32_equal_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
    879  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    880  1.4  christos   crypto_int32 crypto_int32_q,crypto_int32_z;
    881  1.4  christos   __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpl %3,%2\n cmovel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
    882  1.4  christos   return crypto_int32_z;
    883  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    884  1.4  christos   crypto_int32 crypto_int32_z;
    885  1.4  christos   __asm__ ("cmp %w1,%w2\n csetm %w0,eq" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
    886  1.4  christos   return crypto_int32_z;
    887  1.4  christos #else
    888  1.4  christos   return ~crypto_int32_unequal_mask(crypto_int32_x,crypto_int32_y);
    889  1.4  christos #endif
    890  1.1  christos }
    891  1.1  christos 
    892  1.4  christos __attribute__((unused))
    893  1.4  christos static inline
    894  1.4  christos crypto_int32 crypto_int32_equal_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
    895  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    896  1.4  christos   crypto_int32 crypto_int32_q,crypto_int32_z;
    897  1.4  christos   __asm__ ("xorl %0,%0\n movl $1,%1\n cmpl %3,%2\n cmovel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
    898  1.4  christos   return crypto_int32_z;
    899  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    900  1.4  christos   crypto_int32 crypto_int32_z;
    901  1.4  christos   __asm__ ("cmp %w1,%w2\n cset %w0,eq" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
    902  1.4  christos   return crypto_int32_z;
    903  1.4  christos #else
    904  1.4  christos   return 1-crypto_int32_unequal_01(crypto_int32_x,crypto_int32_y);
    905  1.4  christos #endif
    906  1.4  christos }
    907  1.1  christos 
    908  1.4  christos __attribute__((unused))
    909  1.4  christos static inline
    910  1.4  christos crypto_int32 crypto_int32_min(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
    911  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    912  1.4  christos   __asm__ ("cmpl %1,%0\n cmovgl %1,%0" : "+r"(crypto_int32_x) : "r"(crypto_int32_y) : "cc");
    913  1.4  christos   return crypto_int32_x;
    914  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    915  1.4  christos   __asm__ ("cmp %w0,%w1\n csel %w0,%w0,%w1,lt" : "+r"(crypto_int32_x) : "r"(crypto_int32_y) : "cc");
    916  1.4  christos   return crypto_int32_x;
    917  1.4  christos #else
    918  1.4  christos   crypto_int64 crypto_int32_r = (crypto_int64)crypto_int32_y ^ (crypto_int64)crypto_int32_x;
    919  1.4  christos   crypto_int64 crypto_int32_z = (crypto_int64)crypto_int32_y - (crypto_int64)crypto_int32_x;
    920  1.4  christos   crypto_int32_z ^= crypto_int32_r & (crypto_int32_z ^ crypto_int32_y);
    921  1.4  christos   crypto_int32_z = crypto_int32_negative_mask(crypto_int32_z);
    922  1.4  christos   crypto_int32_z &= crypto_int32_r;
    923  1.4  christos   return crypto_int32_x ^ crypto_int32_z;
    924  1.4  christos #endif
    925  1.4  christos }
    926  1.1  christos 
    927  1.4  christos __attribute__((unused))
    928  1.4  christos static inline
    929  1.4  christos crypto_int32 crypto_int32_max(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
    930  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    931  1.4  christos   __asm__ ("cmpl %1,%0\n cmovll %1,%0" : "+r"(crypto_int32_x) : "r"(crypto_int32_y) : "cc");
    932  1.4  christos   return crypto_int32_x;
    933  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    934  1.4  christos   __asm__ ("cmp %w0,%w1\n csel %w0,%w1,%w0,lt" : "+r"(crypto_int32_x) : "r"(crypto_int32_y) : "cc");
    935  1.4  christos   return crypto_int32_x;
    936  1.4  christos #else
    937  1.4  christos   crypto_int64 crypto_int32_r = (crypto_int64)crypto_int32_y ^ (crypto_int64)crypto_int32_x;
    938  1.4  christos   crypto_int64 crypto_int32_z = (crypto_int64)crypto_int32_y - (crypto_int64)crypto_int32_x;
    939  1.4  christos   crypto_int32_z ^= crypto_int32_r & (crypto_int32_z ^ crypto_int32_y);
    940  1.4  christos   crypto_int32_z = crypto_int32_negative_mask(crypto_int32_z);
    941  1.4  christos   crypto_int32_z &= crypto_int32_r;
    942  1.4  christos   return crypto_int32_y ^ crypto_int32_z;
    943  1.4  christos #endif
    944  1.3  christos }
    945  1.1  christos 
    946  1.4  christos __attribute__((unused))
    947  1.4  christos static inline
    948  1.4  christos void crypto_int32_minmax(crypto_int32 *crypto_int32_p,crypto_int32 *crypto_int32_q) {
    949  1.4  christos   crypto_int32 crypto_int32_x = *crypto_int32_p;
    950  1.4  christos   crypto_int32 crypto_int32_y = *crypto_int32_q;
    951  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    952  1.4  christos   crypto_int32 crypto_int32_z;
    953  1.4  christos   __asm__ ("cmpl %2,%1\n movl %1,%0\n cmovgl %2,%1\n cmovgl %0,%2" : "=&r"(crypto_int32_z), "+&r"(crypto_int32_x), "+r"(crypto_int32_y) : : "cc");
    954  1.4  christos   *crypto_int32_p = crypto_int32_x;
    955  1.4  christos   *crypto_int32_q = crypto_int32_y;
    956  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    957  1.4  christos   crypto_int32 crypto_int32_r, crypto_int32_s;
    958  1.4  christos   __asm__ ("cmp %w2,%w3\n csel %w0,%w2,%w3,lt\n csel %w1,%w3,%w2,lt" : "=&r"(crypto_int32_r), "=r"(crypto_int32_s) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
    959  1.4  christos   *crypto_int32_p = crypto_int32_r;
    960  1.4  christos   *crypto_int32_q = crypto_int32_s;
    961  1.4  christos #else
    962  1.4  christos   crypto_int64 crypto_int32_r = (crypto_int64)crypto_int32_y ^ (crypto_int64)crypto_int32_x;
    963  1.4  christos   crypto_int64 crypto_int32_z = (crypto_int64)crypto_int32_y - (crypto_int64)crypto_int32_x;
    964  1.4  christos   crypto_int32_z ^= crypto_int32_r & (crypto_int32_z ^ crypto_int32_y);
    965  1.4  christos   crypto_int32_z = crypto_int32_negative_mask(crypto_int32_z);
    966  1.4  christos   crypto_int32_z &= crypto_int32_r;
    967  1.4  christos   crypto_int32_x ^= crypto_int32_z;
    968  1.4  christos   crypto_int32_y ^= crypto_int32_z;
    969  1.4  christos   *crypto_int32_p = crypto_int32_x;
    970  1.4  christos   *crypto_int32_q = crypto_int32_y;
    971  1.1  christos #endif
    972  1.4  christos }
    973  1.1  christos 
    974  1.4  christos __attribute__((unused))
    975  1.4  christos static inline
    976  1.4  christos crypto_int32 crypto_int32_smaller_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
    977  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    978  1.4  christos   crypto_int32 crypto_int32_q,crypto_int32_z;
    979  1.4  christos   __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpl %3,%2\n cmovll %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
    980  1.4  christos   return crypto_int32_z;
    981  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
    982  1.4  christos   crypto_int32 crypto_int32_z;
    983  1.4  christos   __asm__ ("cmp %w1,%w2\n csetm %w0,lt" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
    984  1.4  christos   return crypto_int32_z;
    985  1.4  christos #else
    986  1.4  christos   crypto_int32 crypto_int32_r = crypto_int32_x ^ crypto_int32_y;
    987  1.4  christos   crypto_int32 crypto_int32_z = crypto_int32_x - crypto_int32_y;
    988  1.4  christos   crypto_int32_z ^= crypto_int32_r & (crypto_int32_z ^ crypto_int32_x);
    989  1.4  christos   return crypto_int32_negative_mask(crypto_int32_z);
    990  1.4  christos #endif
    991  1.4  christos }
    992  1.1  christos 
    993  1.4  christos __attribute__((unused))
    994  1.4  christos static inline
    995  1.4  christos crypto_int32 crypto_int32_smaller_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
    996  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
    997  1.4  christos   crypto_int32 crypto_int32_q,crypto_int32_z;
    998  1.4  christos   __asm__ ("xorl %0,%0\n movl $1,%1\n cmpl %3,%2\n cmovll %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
    999  1.4  christos   return crypto_int32_z;
   1000  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1001  1.4  christos   crypto_int32 crypto_int32_z;
   1002  1.4  christos   __asm__ ("cmp %w1,%w2\n cset %w0,lt" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
   1003  1.4  christos   return crypto_int32_z;
   1004  1.4  christos #else
   1005  1.4  christos   crypto_int32 crypto_int32_r = crypto_int32_x ^ crypto_int32_y;
   1006  1.4  christos   crypto_int32 crypto_int32_z = crypto_int32_x - crypto_int32_y;
   1007  1.4  christos   crypto_int32_z ^= crypto_int32_r & (crypto_int32_z ^ crypto_int32_x);
   1008  1.4  christos   return crypto_int32_unsigned_topbit_01(crypto_int32_z);
   1009  1.4  christos #endif
   1010  1.4  christos }
   1011  1.1  christos 
   1012  1.4  christos __attribute__((unused))
   1013  1.4  christos static inline
   1014  1.4  christos crypto_int32 crypto_int32_leq_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
   1015  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1016  1.4  christos   crypto_int32 crypto_int32_q,crypto_int32_z;
   1017  1.4  christos   __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpl %3,%2\n cmovlel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
   1018  1.4  christos   return crypto_int32_z;
   1019  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1020  1.4  christos   crypto_int32 crypto_int32_z;
   1021  1.4  christos   __asm__ ("cmp %w1,%w2\n csetm %w0,le" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
   1022  1.4  christos   return crypto_int32_z;
   1023  1.4  christos #else
   1024  1.4  christos   return ~crypto_int32_smaller_mask(crypto_int32_y,crypto_int32_x);
   1025  1.4  christos #endif
   1026  1.1  christos }
   1027  1.1  christos 
   1028  1.4  christos __attribute__((unused))
   1029  1.4  christos static inline
   1030  1.4  christos crypto_int32 crypto_int32_leq_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
   1031  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1032  1.4  christos   crypto_int32 crypto_int32_q,crypto_int32_z;
   1033  1.4  christos   __asm__ ("xorl %0,%0\n movl $1,%1\n cmpl %3,%2\n cmovlel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
   1034  1.4  christos   return crypto_int32_z;
   1035  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1036  1.4  christos   crypto_int32 crypto_int32_z;
   1037  1.4  christos   __asm__ ("cmp %w1,%w2\n cset %w0,le" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
   1038  1.4  christos   return crypto_int32_z;
   1039  1.4  christos #else
   1040  1.4  christos   return 1-crypto_int32_smaller_01(crypto_int32_y,crypto_int32_x);
   1041  1.4  christos #endif
   1042  1.1  christos }
   1043  1.4  christos 
   1044  1.4  christos __attribute__((unused))
   1045  1.4  christos static inline
   1046  1.4  christos int crypto_int32_ones_num(crypto_int32 crypto_int32_x) {
   1047  1.4  christos   crypto_int32_unsigned crypto_int32_y = crypto_int32_x;
   1048  1.4  christos   const crypto_int32 C0 = 0x55555555;
   1049  1.4  christos   const crypto_int32 C1 = 0x33333333;
   1050  1.4  christos   const crypto_int32 C2 = 0x0f0f0f0f;
   1051  1.4  christos   crypto_int32_y -= ((crypto_int32_y >> 1) & C0);
   1052  1.4  christos   crypto_int32_y = (crypto_int32_y & C1) + ((crypto_int32_y >> 2) & C1);
   1053  1.4  christos   crypto_int32_y = (crypto_int32_y + (crypto_int32_y >> 4)) & C2;
   1054  1.4  christos   crypto_int32_y += crypto_int32_y >> 8;
   1055  1.4  christos   crypto_int32_y = (crypto_int32_y + (crypto_int32_y >> 16)) & 0xff;
   1056  1.4  christos   return crypto_int32_y;
   1057  1.4  christos }
   1058  1.4  christos 
   1059  1.4  christos __attribute__((unused))
   1060  1.4  christos static inline
   1061  1.4  christos int crypto_int32_bottomzeros_num(crypto_int32 crypto_int32_x) {
   1062  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1063  1.4  christos   crypto_int32 fallback = 32;
   1064  1.4  christos   __asm__ ("bsfl %0,%0\n cmovel %1,%0" : "+&r"(crypto_int32_x) : "r"(fallback) : "cc");
   1065  1.4  christos   return crypto_int32_x;
   1066  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1067  1.4  christos   int64_t crypto_int32_y;
   1068  1.4  christos   __asm__ ("rbit %w0,%w1\n clz %w0,%w0" : "=r"(crypto_int32_y) : "r"(crypto_int32_x) : );
   1069  1.4  christos   return crypto_int32_y;
   1070  1.4  christos #else
   1071  1.4  christos   crypto_int32 crypto_int32_y = crypto_int32_x ^ (crypto_int32_x-1);
   1072  1.4  christos   crypto_int32_y = ((crypto_int32) crypto_int32_y) >> 1;
   1073  1.4  christos   crypto_int32_y &= ~(crypto_int32_x & (((crypto_int32) 1) << (32-1)));
   1074  1.4  christos   return crypto_int32_ones_num(crypto_int32_y);
   1075  1.1  christos #endif
   1076  1.4  christos }
   1077  1.1  christos 
   1078  1.4  christos #endif
   1079  1.4  christos 
   1080  1.4  christos /* from supercop-20240808/cryptoint/crypto_int64.h */
   1081  1.4  christos /* auto-generated: cd cryptoint; ./autogen */
   1082  1.4  christos /* cryptoint 20240806 */
   1083  1.4  christos 
   1084  1.4  christos #ifndef crypto_int64_h
   1085  1.4  christos #define crypto_int64_h
   1086  1.4  christos 
   1087  1.4  christos #define crypto_int64 int64_t
   1088  1.4  christos #define crypto_int64_unsigned uint64_t
   1089  1.1  christos 
   1090  1.1  christos 
   1091  1.1  christos 
   1092  1.4  christos __attribute__((unused))
   1093  1.4  christos static inline
   1094  1.4  christos crypto_int64 crypto_int64_load(const unsigned char *crypto_int64_s) {
   1095  1.4  christos   crypto_int64 crypto_int64_z = 0;
   1096  1.4  christos   crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 0;
   1097  1.4  christos   crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 8;
   1098  1.4  christos   crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 16;
   1099  1.4  christos   crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 24;
   1100  1.4  christos   crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 32;
   1101  1.4  christos   crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 40;
   1102  1.4  christos   crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 48;
   1103  1.4  christos   crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 56;
   1104  1.4  christos   return crypto_int64_z;
   1105  1.4  christos }
   1106  1.4  christos 
   1107  1.4  christos __attribute__((unused))
   1108  1.4  christos static inline
   1109  1.4  christos void crypto_int64_store(unsigned char *crypto_int64_s,crypto_int64 crypto_int64_x) {
   1110  1.4  christos   *crypto_int64_s++ = crypto_int64_x >> 0;
   1111  1.4  christos   *crypto_int64_s++ = crypto_int64_x >> 8;
   1112  1.4  christos   *crypto_int64_s++ = crypto_int64_x >> 16;
   1113  1.4  christos   *crypto_int64_s++ = crypto_int64_x >> 24;
   1114  1.4  christos   *crypto_int64_s++ = crypto_int64_x >> 32;
   1115  1.4  christos   *crypto_int64_s++ = crypto_int64_x >> 40;
   1116  1.4  christos   *crypto_int64_s++ = crypto_int64_x >> 48;
   1117  1.4  christos   *crypto_int64_s++ = crypto_int64_x >> 56;
   1118  1.4  christos }
   1119  1.4  christos 
   1120  1.4  christos __attribute__((unused))
   1121  1.4  christos static inline
   1122  1.4  christos crypto_int64 crypto_int64_negative_mask(crypto_int64 crypto_int64_x) {
   1123  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1124  1.4  christos   __asm__ ("sarq $63,%0" : "+r"(crypto_int64_x) : : "cc");
   1125  1.4  christos   return crypto_int64_x;
   1126  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1127  1.4  christos   crypto_int64 crypto_int64_y;
   1128  1.4  christos   __asm__ ("asr %0,%1,63" : "=r"(crypto_int64_y) : "r"(crypto_int64_x) : );
   1129  1.4  christos   return crypto_int64_y;
   1130  1.4  christos #else
   1131  1.4  christos   crypto_int64_x >>= 64-6;
   1132  1.4  christos   crypto_int64_x ^= crypto_int64_optblocker;
   1133  1.4  christos   crypto_int64_x >>= 5;
   1134  1.4  christos   return crypto_int64_x;
   1135  1.4  christos #endif
   1136  1.1  christos }
   1137  1.1  christos 
   1138  1.4  christos __attribute__((unused))
   1139  1.4  christos static inline
   1140  1.4  christos crypto_int64_unsigned crypto_int64_unsigned_topbit_01(crypto_int64_unsigned crypto_int64_x) {
   1141  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1142  1.4  christos   __asm__ ("shrq $63,%0" : "+r"(crypto_int64_x) : : "cc");
   1143  1.4  christos   return crypto_int64_x;
   1144  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1145  1.4  christos   crypto_int64 crypto_int64_y;
   1146  1.4  christos   __asm__ ("lsr %0,%1,63" : "=r"(crypto_int64_y) : "r"(crypto_int64_x) : );
   1147  1.4  christos   return crypto_int64_y;
   1148  1.4  christos #else
   1149  1.4  christos   crypto_int64_x >>= 64-6;
   1150  1.4  christos   crypto_int64_x ^= crypto_int64_optblocker;
   1151  1.4  christos   crypto_int64_x >>= 5;
   1152  1.4  christos   return crypto_int64_x;
   1153  1.4  christos #endif
   1154  1.1  christos }
   1155  1.1  christos 
   1156  1.4  christos __attribute__((unused))
   1157  1.4  christos static inline
   1158  1.4  christos crypto_int64 crypto_int64_negative_01(crypto_int64 crypto_int64_x) {
   1159  1.4  christos   return crypto_int64_unsigned_topbit_01(crypto_int64_x);
   1160  1.4  christos }
   1161  1.1  christos 
   1162  1.4  christos __attribute__((unused))
   1163  1.4  christos static inline
   1164  1.4  christos crypto_int64 crypto_int64_topbit_mask(crypto_int64 crypto_int64_x) {
   1165  1.4  christos   return crypto_int64_negative_mask(crypto_int64_x);
   1166  1.4  christos }
   1167  1.1  christos 
   1168  1.4  christos __attribute__((unused))
   1169  1.4  christos static inline
   1170  1.4  christos crypto_int64 crypto_int64_topbit_01(crypto_int64 crypto_int64_x) {
   1171  1.4  christos   return crypto_int64_unsigned_topbit_01(crypto_int64_x);
   1172  1.1  christos }
   1173  1.1  christos 
   1174  1.4  christos __attribute__((unused))
   1175  1.4  christos static inline
   1176  1.4  christos crypto_int64 crypto_int64_bottombit_mask(crypto_int64 crypto_int64_x) {
   1177  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1178  1.4  christos   __asm__ ("andq $1,%0" : "+r"(crypto_int64_x) : : "cc");
   1179  1.4  christos   return -crypto_int64_x;
   1180  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1181  1.4  christos   crypto_int64 crypto_int64_y;
   1182  1.4  christos   __asm__ ("sbfx %0,%1,0,1" : "=r"(crypto_int64_y) : "r"(crypto_int64_x) : );
   1183  1.4  christos   return crypto_int64_y;
   1184  1.4  christos #else
   1185  1.4  christos   crypto_int64_x &= 1 ^ crypto_int64_optblocker;
   1186  1.4  christos   return -crypto_int64_x;
   1187  1.4  christos #endif
   1188  1.4  christos }
   1189  1.3  christos 
   1190  1.4  christos __attribute__((unused))
   1191  1.4  christos static inline
   1192  1.4  christos crypto_int64 crypto_int64_bottombit_01(crypto_int64 crypto_int64_x) {
   1193  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1194  1.4  christos   __asm__ ("andq $1,%0" : "+r"(crypto_int64_x) : : "cc");
   1195  1.4  christos   return crypto_int64_x;
   1196  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1197  1.4  christos   crypto_int64 crypto_int64_y;
   1198  1.4  christos   __asm__ ("ubfx %0,%1,0,1" : "=r"(crypto_int64_y) : "r"(crypto_int64_x) : );
   1199  1.4  christos   return crypto_int64_y;
   1200  1.4  christos #else
   1201  1.4  christos   crypto_int64_x &= 1 ^ crypto_int64_optblocker;
   1202  1.4  christos   return crypto_int64_x;
   1203  1.4  christos #endif
   1204  1.4  christos }
   1205  1.3  christos 
   1206  1.4  christos __attribute__((unused))
   1207  1.4  christos static inline
   1208  1.4  christos crypto_int64 crypto_int64_bitinrangepublicpos_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) {
   1209  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1210  1.4  christos   __asm__ ("sarq %%cl,%0" : "+r"(crypto_int64_x) : "c"(crypto_int64_s) : "cc");
   1211  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1212  1.4  christos   __asm__ ("asr %0,%0,%1" : "+r"(crypto_int64_x) : "r"(crypto_int64_s) : );
   1213  1.4  christos #else
   1214  1.4  christos   crypto_int64_x >>= crypto_int64_s ^ crypto_int64_optblocker;
   1215  1.4  christos #endif
   1216  1.4  christos   return crypto_int64_bottombit_mask(crypto_int64_x);
   1217  1.4  christos }
   1218  1.1  christos 
   1219  1.4  christos __attribute__((unused))
   1220  1.4  christos static inline
   1221  1.4  christos crypto_int64 crypto_int64_bitinrangepublicpos_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) {
   1222  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1223  1.4  christos   __asm__ ("sarq %%cl,%0" : "+r"(crypto_int64_x) : "c"(crypto_int64_s) : "cc");
   1224  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1225  1.4  christos   __asm__ ("asr %0,%0,%1" : "+r"(crypto_int64_x) : "r"(crypto_int64_s) : );
   1226  1.4  christos #else
   1227  1.4  christos   crypto_int64_x >>= crypto_int64_s ^ crypto_int64_optblocker;
   1228  1.4  christos #endif
   1229  1.4  christos   return crypto_int64_bottombit_01(crypto_int64_x);
   1230  1.4  christos }
   1231  1.3  christos 
   1232  1.4  christos __attribute__((unused))
   1233  1.4  christos static inline
   1234  1.4  christos crypto_int64 crypto_int64_shlmod(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) {
   1235  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1236  1.4  christos   __asm__ ("shlq %%cl,%0" : "+r"(crypto_int64_x) : "c"(crypto_int64_s) : "cc");
   1237  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1238  1.4  christos   __asm__ ("lsl %0,%0,%1" : "+r"(crypto_int64_x) : "r"(crypto_int64_s) : );
   1239  1.4  christos #else
   1240  1.4  christos   int crypto_int64_k, crypto_int64_l;
   1241  1.4  christos   for (crypto_int64_l = 0,crypto_int64_k = 1;crypto_int64_k < 64;++crypto_int64_l,crypto_int64_k *= 2)
   1242  1.4  christos     crypto_int64_x ^= (crypto_int64_x ^ (crypto_int64_x << crypto_int64_k)) & crypto_int64_bitinrangepublicpos_mask(crypto_int64_s,crypto_int64_l);
   1243  1.4  christos #endif
   1244  1.4  christos   return crypto_int64_x;
   1245  1.4  christos }
   1246  1.3  christos 
   1247  1.4  christos __attribute__((unused))
   1248  1.4  christos static inline
   1249  1.4  christos crypto_int64 crypto_int64_shrmod(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) {
   1250  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1251  1.4  christos   __asm__ ("sarq %%cl,%0" : "+r"(crypto_int64_x) : "c"(crypto_int64_s) : "cc");
   1252  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1253  1.4  christos   __asm__ ("asr %0,%0,%1" : "+r"(crypto_int64_x) : "r"(crypto_int64_s) : );
   1254  1.4  christos #else
   1255  1.4  christos   int crypto_int64_k, crypto_int64_l;
   1256  1.4  christos   for (crypto_int64_l = 0,crypto_int64_k = 1;crypto_int64_k < 64;++crypto_int64_l,crypto_int64_k *= 2)
   1257  1.4  christos     crypto_int64_x ^= (crypto_int64_x ^ (crypto_int64_x >> crypto_int64_k)) & crypto_int64_bitinrangepublicpos_mask(crypto_int64_s,crypto_int64_l);
   1258  1.4  christos #endif
   1259  1.4  christos   return crypto_int64_x;
   1260  1.4  christos }
   1261  1.4  christos 
   1262  1.4  christos __attribute__((unused))
   1263  1.4  christos static inline
   1264  1.4  christos crypto_int64 crypto_int64_bitmod_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) {
   1265  1.4  christos   crypto_int64_x = crypto_int64_shrmod(crypto_int64_x,crypto_int64_s);
   1266  1.4  christos   return crypto_int64_bottombit_mask(crypto_int64_x);
   1267  1.4  christos }
   1268  1.4  christos 
   1269  1.4  christos __attribute__((unused))
   1270  1.4  christos static inline
   1271  1.4  christos crypto_int64 crypto_int64_bitmod_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) {
   1272  1.4  christos   crypto_int64_x = crypto_int64_shrmod(crypto_int64_x,crypto_int64_s);
   1273  1.4  christos   return crypto_int64_bottombit_01(crypto_int64_x);
   1274  1.4  christos }
   1275  1.4  christos 
   1276  1.4  christos __attribute__((unused))
   1277  1.4  christos static inline
   1278  1.4  christos crypto_int64 crypto_int64_nonzero_mask(crypto_int64 crypto_int64_x) {
   1279  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1280  1.4  christos   crypto_int64 crypto_int64_q,crypto_int64_z;
   1281  1.4  christos   __asm__ ("xorq %0,%0\n movq $-1,%1\n testq %2,%2\n cmovneq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x) : "cc");
   1282  1.4  christos   return crypto_int64_z;
   1283  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1284  1.4  christos   crypto_int64 crypto_int64_z;
   1285  1.4  christos   __asm__ ("cmp %1,0\n csetm %0,ne" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc");
   1286  1.4  christos   return crypto_int64_z;
   1287  1.4  christos #else
   1288  1.4  christos   crypto_int64_x |= -crypto_int64_x;
   1289  1.4  christos   return crypto_int64_negative_mask(crypto_int64_x);
   1290  1.4  christos #endif
   1291  1.4  christos }
   1292  1.3  christos 
   1293  1.4  christos __attribute__((unused))
   1294  1.4  christos static inline
   1295  1.4  christos crypto_int64 crypto_int64_nonzero_01(crypto_int64 crypto_int64_x) {
   1296  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1297  1.4  christos   crypto_int64 crypto_int64_q,crypto_int64_z;
   1298  1.4  christos   __asm__ ("xorq %0,%0\n movq $1,%1\n testq %2,%2\n cmovneq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x) : "cc");
   1299  1.4  christos   return crypto_int64_z;
   1300  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1301  1.4  christos   crypto_int64 crypto_int64_z;
   1302  1.4  christos   __asm__ ("cmp %1,0\n cset %0,ne" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc");
   1303  1.4  christos   return crypto_int64_z;
   1304  1.4  christos #else
   1305  1.4  christos   crypto_int64_x |= -crypto_int64_x;
   1306  1.4  christos   return crypto_int64_unsigned_topbit_01(crypto_int64_x);
   1307  1.4  christos #endif
   1308  1.4  christos }
   1309  1.1  christos 
   1310  1.4  christos __attribute__((unused))
   1311  1.4  christos static inline
   1312  1.4  christos crypto_int64 crypto_int64_positive_mask(crypto_int64 crypto_int64_x) {
   1313  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1314  1.4  christos   crypto_int64 crypto_int64_q,crypto_int64_z;
   1315  1.4  christos   __asm__ ("xorq %0,%0\n movq $-1,%1\n testq %2,%2\n cmovgq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x) : "cc");
   1316  1.4  christos   return crypto_int64_z;
   1317  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1318  1.4  christos   crypto_int64 crypto_int64_z;
   1319  1.4  christos   __asm__ ("cmp %1,0\n csetm %0,gt" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc");
   1320  1.4  christos   return crypto_int64_z;
   1321  1.4  christos #else
   1322  1.4  christos   crypto_int64 crypto_int64_z = -crypto_int64_x;
   1323  1.4  christos   crypto_int64_z ^= crypto_int64_x & crypto_int64_z;
   1324  1.4  christos   return crypto_int64_negative_mask(crypto_int64_z);
   1325  1.4  christos #endif
   1326  1.3  christos }
   1327  1.1  christos 
   1328  1.4  christos __attribute__((unused))
   1329  1.4  christos static inline
   1330  1.4  christos crypto_int64 crypto_int64_positive_01(crypto_int64 crypto_int64_x) {
   1331  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1332  1.4  christos   crypto_int64 crypto_int64_q,crypto_int64_z;
   1333  1.4  christos   __asm__ ("xorq %0,%0\n movq $1,%1\n testq %2,%2\n cmovgq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x) : "cc");
   1334  1.4  christos   return crypto_int64_z;
   1335  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1336  1.4  christos   crypto_int64 crypto_int64_z;
   1337  1.4  christos   __asm__ ("cmp %1,0\n cset %0,gt" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc");
   1338  1.4  christos   return crypto_int64_z;
   1339  1.4  christos #else
   1340  1.4  christos   crypto_int64 crypto_int64_z = -crypto_int64_x;
   1341  1.4  christos   crypto_int64_z ^= crypto_int64_x & crypto_int64_z;
   1342  1.4  christos   return crypto_int64_unsigned_topbit_01(crypto_int64_z);
   1343  1.1  christos #endif
   1344  1.1  christos }
   1345  1.1  christos 
   1346  1.4  christos __attribute__((unused))
   1347  1.4  christos static inline
   1348  1.4  christos crypto_int64 crypto_int64_zero_mask(crypto_int64 crypto_int64_x) {
   1349  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1350  1.4  christos   crypto_int64 crypto_int64_q,crypto_int64_z;
   1351  1.4  christos   __asm__ ("xorq %0,%0\n movq $-1,%1\n testq %2,%2\n cmoveq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x) : "cc");
   1352  1.4  christos   return crypto_int64_z;
   1353  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1354  1.4  christos   crypto_int64 crypto_int64_z;
   1355  1.4  christos   __asm__ ("cmp %1,0\n csetm %0,eq" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc");
   1356  1.4  christos   return crypto_int64_z;
   1357  1.4  christos #else
   1358  1.4  christos   return ~crypto_int64_nonzero_mask(crypto_int64_x);
   1359  1.4  christos #endif
   1360  1.1  christos }
   1361  1.1  christos 
   1362  1.4  christos __attribute__((unused))
   1363  1.4  christos static inline
   1364  1.4  christos crypto_int64 crypto_int64_zero_01(crypto_int64 crypto_int64_x) {
   1365  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1366  1.4  christos   crypto_int64 crypto_int64_q,crypto_int64_z;
   1367  1.4  christos   __asm__ ("xorq %0,%0\n movq $1,%1\n testq %2,%2\n cmoveq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x) : "cc");
   1368  1.4  christos   return crypto_int64_z;
   1369  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1370  1.4  christos   crypto_int64 crypto_int64_z;
   1371  1.4  christos   __asm__ ("cmp %1,0\n cset %0,eq" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc");
   1372  1.4  christos   return crypto_int64_z;
   1373  1.4  christos #else
   1374  1.4  christos   return 1-crypto_int64_nonzero_01(crypto_int64_x);
   1375  1.4  christos #endif
   1376  1.1  christos }
   1377  1.1  christos 
   1378  1.4  christos __attribute__((unused))
   1379  1.4  christos static inline
   1380  1.4  christos crypto_int64 crypto_int64_unequal_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
   1381  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1382  1.4  christos   crypto_int64 crypto_int64_q,crypto_int64_z;
   1383  1.4  christos   __asm__ ("xorq %0,%0\n movq $-1,%1\n cmpq %3,%2\n cmovneq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1384  1.4  christos   return crypto_int64_z;
   1385  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1386  1.4  christos   crypto_int64 crypto_int64_z;
   1387  1.4  christos   __asm__ ("cmp %1,%2\n csetm %0,ne" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1388  1.4  christos   return crypto_int64_z;
   1389  1.4  christos #else
   1390  1.4  christos   return crypto_int64_nonzero_mask(crypto_int64_x ^ crypto_int64_y);
   1391  1.1  christos #endif
   1392  1.1  christos }
   1393  1.1  christos 
   1394  1.4  christos __attribute__((unused))
   1395  1.4  christos static inline
   1396  1.4  christos crypto_int64 crypto_int64_unequal_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
   1397  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1398  1.4  christos   crypto_int64 crypto_int64_q,crypto_int64_z;
   1399  1.4  christos   __asm__ ("xorq %0,%0\n movq $1,%1\n cmpq %3,%2\n cmovneq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1400  1.4  christos   return crypto_int64_z;
   1401  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1402  1.4  christos   crypto_int64 crypto_int64_z;
   1403  1.4  christos   __asm__ ("cmp %1,%2\n cset %0,ne" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1404  1.4  christos   return crypto_int64_z;
   1405  1.4  christos #else
   1406  1.4  christos   return crypto_int64_nonzero_01(crypto_int64_x ^ crypto_int64_y);
   1407  1.4  christos #endif
   1408  1.1  christos }
   1409  1.1  christos 
   1410  1.4  christos __attribute__((unused))
   1411  1.4  christos static inline
   1412  1.4  christos crypto_int64 crypto_int64_equal_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
   1413  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1414  1.4  christos   crypto_int64 crypto_int64_q,crypto_int64_z;
   1415  1.4  christos   __asm__ ("xorq %0,%0\n movq $-1,%1\n cmpq %3,%2\n cmoveq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1416  1.4  christos   return crypto_int64_z;
   1417  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1418  1.4  christos   crypto_int64 crypto_int64_z;
   1419  1.4  christos   __asm__ ("cmp %1,%2\n csetm %0,eq" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1420  1.4  christos   return crypto_int64_z;
   1421  1.4  christos #else
   1422  1.4  christos   return ~crypto_int64_unequal_mask(crypto_int64_x,crypto_int64_y);
   1423  1.4  christos #endif
   1424  1.1  christos }
   1425  1.1  christos 
   1426  1.4  christos __attribute__((unused))
   1427  1.4  christos static inline
   1428  1.4  christos crypto_int64 crypto_int64_equal_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
   1429  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1430  1.4  christos   crypto_int64 crypto_int64_q,crypto_int64_z;
   1431  1.4  christos   __asm__ ("xorq %0,%0\n movq $1,%1\n cmpq %3,%2\n cmoveq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1432  1.4  christos   return crypto_int64_z;
   1433  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1434  1.4  christos   crypto_int64 crypto_int64_z;
   1435  1.4  christos   __asm__ ("cmp %1,%2\n cset %0,eq" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1436  1.4  christos   return crypto_int64_z;
   1437  1.4  christos #else
   1438  1.4  christos   return 1-crypto_int64_unequal_01(crypto_int64_x,crypto_int64_y);
   1439  1.4  christos #endif
   1440  1.1  christos }
   1441  1.1  christos 
   1442  1.4  christos __attribute__((unused))
   1443  1.4  christos static inline
   1444  1.4  christos crypto_int64 crypto_int64_min(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
   1445  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1446  1.4  christos   __asm__ ("cmpq %1,%0\n cmovgq %1,%0" : "+r"(crypto_int64_x) : "r"(crypto_int64_y) : "cc");
   1447  1.4  christos   return crypto_int64_x;
   1448  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1449  1.4  christos   __asm__ ("cmp %0,%1\n csel %0,%0,%1,lt" : "+r"(crypto_int64_x) : "r"(crypto_int64_y) : "cc");
   1450  1.4  christos   return crypto_int64_x;
   1451  1.4  christos #else
   1452  1.4  christos   crypto_int64 crypto_int64_r = crypto_int64_y ^ crypto_int64_x;
   1453  1.4  christos   crypto_int64 crypto_int64_z = crypto_int64_y - crypto_int64_x;
   1454  1.4  christos   crypto_int64_z ^= crypto_int64_r & (crypto_int64_z ^ crypto_int64_y);
   1455  1.4  christos   crypto_int64_z = crypto_int64_negative_mask(crypto_int64_z);
   1456  1.4  christos   crypto_int64_z &= crypto_int64_r;
   1457  1.4  christos   return crypto_int64_x ^ crypto_int64_z;
   1458  1.4  christos #endif
   1459  1.1  christos }
   1460  1.1  christos 
   1461  1.4  christos __attribute__((unused))
   1462  1.4  christos static inline
   1463  1.4  christos crypto_int64 crypto_int64_max(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
   1464  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1465  1.4  christos   __asm__ ("cmpq %1,%0\n cmovlq %1,%0" : "+r"(crypto_int64_x) : "r"(crypto_int64_y) : "cc");
   1466  1.4  christos   return crypto_int64_x;
   1467  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1468  1.4  christos   __asm__ ("cmp %0,%1\n csel %0,%1,%0,lt" : "+r"(crypto_int64_x) : "r"(crypto_int64_y) : "cc");
   1469  1.4  christos   return crypto_int64_x;
   1470  1.4  christos #else
   1471  1.4  christos   crypto_int64 crypto_int64_r = crypto_int64_y ^ crypto_int64_x;
   1472  1.4  christos   crypto_int64 crypto_int64_z = crypto_int64_y - crypto_int64_x;
   1473  1.4  christos   crypto_int64_z ^= crypto_int64_r & (crypto_int64_z ^ crypto_int64_y);
   1474  1.4  christos   crypto_int64_z = crypto_int64_negative_mask(crypto_int64_z);
   1475  1.4  christos   crypto_int64_z &= crypto_int64_r;
   1476  1.4  christos   return crypto_int64_y ^ crypto_int64_z;
   1477  1.4  christos #endif
   1478  1.1  christos }
   1479  1.1  christos 
   1480  1.4  christos __attribute__((unused))
   1481  1.4  christos static inline
   1482  1.4  christos void crypto_int64_minmax(crypto_int64 *crypto_int64_p,crypto_int64 *crypto_int64_q) {
   1483  1.4  christos   crypto_int64 crypto_int64_x = *crypto_int64_p;
   1484  1.4  christos   crypto_int64 crypto_int64_y = *crypto_int64_q;
   1485  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1486  1.4  christos   crypto_int64 crypto_int64_z;
   1487  1.4  christos   __asm__ ("cmpq %2,%1\n movq %1,%0\n cmovgq %2,%1\n cmovgq %0,%2" : "=&r"(crypto_int64_z), "+&r"(crypto_int64_x), "+r"(crypto_int64_y) : : "cc");
   1488  1.4  christos   *crypto_int64_p = crypto_int64_x;
   1489  1.4  christos   *crypto_int64_q = crypto_int64_y;
   1490  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1491  1.4  christos   crypto_int64 crypto_int64_r, crypto_int64_s;
   1492  1.4  christos   __asm__ ("cmp %2,%3\n csel %0,%2,%3,lt\n csel %1,%3,%2,lt" : "=&r"(crypto_int64_r), "=r"(crypto_int64_s) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1493  1.4  christos   *crypto_int64_p = crypto_int64_r;
   1494  1.4  christos   *crypto_int64_q = crypto_int64_s;
   1495  1.4  christos #else
   1496  1.4  christos   crypto_int64 crypto_int64_r = crypto_int64_y ^ crypto_int64_x;
   1497  1.4  christos   crypto_int64 crypto_int64_z = crypto_int64_y - crypto_int64_x;
   1498  1.4  christos   crypto_int64_z ^= crypto_int64_r & (crypto_int64_z ^ crypto_int64_y);
   1499  1.4  christos   crypto_int64_z = crypto_int64_negative_mask(crypto_int64_z);
   1500  1.4  christos   crypto_int64_z &= crypto_int64_r;
   1501  1.4  christos   crypto_int64_x ^= crypto_int64_z;
   1502  1.4  christos   crypto_int64_y ^= crypto_int64_z;
   1503  1.4  christos   *crypto_int64_p = crypto_int64_x;
   1504  1.4  christos   *crypto_int64_q = crypto_int64_y;
   1505  1.1  christos #endif
   1506  1.1  christos }
   1507  1.1  christos 
   1508  1.4  christos __attribute__((unused))
   1509  1.4  christos static inline
   1510  1.4  christos crypto_int64 crypto_int64_smaller_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
   1511  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1512  1.4  christos   crypto_int64 crypto_int64_q,crypto_int64_z;
   1513  1.4  christos   __asm__ ("xorq %0,%0\n movq $-1,%1\n cmpq %3,%2\n cmovlq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1514  1.4  christos   return crypto_int64_z;
   1515  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1516  1.4  christos   crypto_int64 crypto_int64_z;
   1517  1.4  christos   __asm__ ("cmp %1,%2\n csetm %0,lt" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1518  1.4  christos   return crypto_int64_z;
   1519  1.4  christos #else
   1520  1.4  christos   crypto_int64 crypto_int64_r = crypto_int64_x ^ crypto_int64_y;
   1521  1.4  christos   crypto_int64 crypto_int64_z = crypto_int64_x - crypto_int64_y;
   1522  1.4  christos   crypto_int64_z ^= crypto_int64_r & (crypto_int64_z ^ crypto_int64_x);
   1523  1.4  christos   return crypto_int64_negative_mask(crypto_int64_z);
   1524  1.4  christos #endif
   1525  1.1  christos }
   1526  1.1  christos 
   1527  1.4  christos __attribute__((unused))
   1528  1.4  christos static inline
   1529  1.4  christos crypto_int64 crypto_int64_smaller_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
   1530  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1531  1.4  christos   crypto_int64 crypto_int64_q,crypto_int64_z;
   1532  1.4  christos   __asm__ ("xorq %0,%0\n movq $1,%1\n cmpq %3,%2\n cmovlq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1533  1.4  christos   return crypto_int64_z;
   1534  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1535  1.4  christos   crypto_int64 crypto_int64_z;
   1536  1.4  christos   __asm__ ("cmp %1,%2\n cset %0,lt" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1537  1.4  christos   return crypto_int64_z;
   1538  1.4  christos #else
   1539  1.4  christos   crypto_int64 crypto_int64_r = crypto_int64_x ^ crypto_int64_y;
   1540  1.4  christos   crypto_int64 crypto_int64_z = crypto_int64_x - crypto_int64_y;
   1541  1.4  christos   crypto_int64_z ^= crypto_int64_r & (crypto_int64_z ^ crypto_int64_x);
   1542  1.4  christos   return crypto_int64_unsigned_topbit_01(crypto_int64_z);
   1543  1.4  christos #endif
   1544  1.1  christos }
   1545  1.3  christos 
   1546  1.4  christos __attribute__((unused))
   1547  1.4  christos static inline
   1548  1.4  christos crypto_int64 crypto_int64_leq_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
   1549  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1550  1.4  christos   crypto_int64 crypto_int64_q,crypto_int64_z;
   1551  1.4  christos   __asm__ ("xorq %0,%0\n movq $-1,%1\n cmpq %3,%2\n cmovleq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1552  1.4  christos   return crypto_int64_z;
   1553  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1554  1.4  christos   crypto_int64 crypto_int64_z;
   1555  1.4  christos   __asm__ ("cmp %1,%2\n csetm %0,le" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1556  1.4  christos   return crypto_int64_z;
   1557  1.4  christos #else
   1558  1.4  christos   return ~crypto_int64_smaller_mask(crypto_int64_y,crypto_int64_x);
   1559  1.1  christos #endif
   1560  1.1  christos }
   1561  1.1  christos 
   1562  1.4  christos __attribute__((unused))
   1563  1.4  christos static inline
   1564  1.4  christos crypto_int64 crypto_int64_leq_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
   1565  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1566  1.4  christos   crypto_int64 crypto_int64_q,crypto_int64_z;
   1567  1.4  christos   __asm__ ("xorq %0,%0\n movq $1,%1\n cmpq %3,%2\n cmovleq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1568  1.4  christos   return crypto_int64_z;
   1569  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1570  1.4  christos   crypto_int64 crypto_int64_z;
   1571  1.4  christos   __asm__ ("cmp %1,%2\n cset %0,le" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
   1572  1.4  christos   return crypto_int64_z;
   1573  1.4  christos #else
   1574  1.4  christos   return 1-crypto_int64_smaller_01(crypto_int64_y,crypto_int64_x);
   1575  1.4  christos #endif
   1576  1.1  christos }
   1577  1.1  christos 
   1578  1.4  christos __attribute__((unused))
   1579  1.4  christos static inline
   1580  1.4  christos int crypto_int64_ones_num(crypto_int64 crypto_int64_x) {
   1581  1.4  christos   crypto_int64_unsigned crypto_int64_y = crypto_int64_x;
   1582  1.4  christos   const crypto_int64 C0 = 0x5555555555555555;
   1583  1.4  christos   const crypto_int64 C1 = 0x3333333333333333;
   1584  1.4  christos   const crypto_int64 C2 = 0x0f0f0f0f0f0f0f0f;
   1585  1.4  christos   crypto_int64_y -= ((crypto_int64_y >> 1) & C0);
   1586  1.4  christos   crypto_int64_y = (crypto_int64_y & C1) + ((crypto_int64_y >> 2) & C1);
   1587  1.4  christos   crypto_int64_y = (crypto_int64_y + (crypto_int64_y >> 4)) & C2;
   1588  1.4  christos   crypto_int64_y += crypto_int64_y >> 8;
   1589  1.4  christos   crypto_int64_y += crypto_int64_y >> 16;
   1590  1.4  christos   crypto_int64_y = (crypto_int64_y + (crypto_int64_y >> 32)) & 0xff;
   1591  1.4  christos   return crypto_int64_y;
   1592  1.4  christos }
   1593  1.4  christos 
   1594  1.4  christos __attribute__((unused))
   1595  1.4  christos static inline
   1596  1.4  christos int crypto_int64_bottomzeros_num(crypto_int64 crypto_int64_x) {
   1597  1.4  christos #if defined(__GNUC__) && defined(__x86_64__)
   1598  1.4  christos   crypto_int64 fallback = 64;
   1599  1.4  christos   __asm__ ("bsfq %0,%0\n cmoveq %1,%0" : "+&r"(crypto_int64_x) : "r"(fallback) : "cc");
   1600  1.4  christos   return crypto_int64_x;
   1601  1.4  christos #elif defined(__GNUC__) && defined(__aarch64__)
   1602  1.4  christos   int64_t crypto_int64_y;
   1603  1.4  christos   __asm__ ("rbit %0,%1\n clz %0,%0" : "=r"(crypto_int64_y) : "r"(crypto_int64_x) : );
   1604  1.4  christos   return crypto_int64_y;
   1605  1.4  christos #else
   1606  1.4  christos   crypto_int64 crypto_int64_y = crypto_int64_x ^ (crypto_int64_x-1);
   1607  1.4  christos   crypto_int64_y = ((crypto_int64) crypto_int64_y) >> 1;
   1608  1.4  christos   crypto_int64_y &= ~(crypto_int64_x & (((crypto_int64) 1) << (64-1)));
   1609  1.4  christos   return crypto_int64_ones_num(crypto_int64_y);
   1610  1.4  christos #endif
   1611  1.1  christos }
   1612  1.3  christos 
   1613  1.1  christos #endif
   1614  1.1  christos 
   1615  1.4  christos /* from supercop-20240808/crypto_sort/int32/portable4/sort.c */
   1616  1.4  christos #define int32_MINMAX(a,b) crypto_int32_minmax(&a,&b)
   1617  1.1  christos 
   1618  1.4  christos static void crypto_sort_int32(void *array,long long n)
   1619  1.1  christos {
   1620  1.4  christos   long long top,p,q,r,i,j;
   1621  1.4  christos   int32 *x = array;
   1622  1.1  christos 
   1623  1.4  christos   if (n < 2) return;
   1624  1.4  christos   top = 1;
   1625  1.4  christos   while (top < n - top) top += top;
   1626  1.1  christos 
   1627  1.4  christos   for (p = top;p >= 1;p >>= 1) {
   1628  1.4  christos     i = 0;
   1629  1.4  christos     while (i + 2 * p <= n) {
   1630  1.4  christos       for (j = i;j < i + p;++j)
   1631  1.4  christos         int32_MINMAX(x[j],x[j+p]);
   1632  1.4  christos       i += 2 * p;
   1633  1.4  christos     }
   1634  1.4  christos     for (j = i;j < n - p;++j)
   1635  1.4  christos       int32_MINMAX(x[j],x[j+p]);
   1636  1.1  christos 
   1637  1.4  christos     i = 0;
   1638  1.4  christos     j = 0;
   1639  1.4  christos     for (q = top;q > p;q >>= 1) {
   1640  1.4  christos       if (j != i) for (;;) {
   1641  1.4  christos         if (j == n - q) goto done;
   1642  1.4  christos         int32 a = x[j + p];
   1643  1.4  christos         for (r = q;r > p;r >>= 1)
   1644  1.4  christos           int32_MINMAX(a,x[j + r]);
   1645  1.4  christos         x[j + p] = a;
   1646  1.4  christos         ++j;
   1647  1.4  christos         if (j == i + p) {
   1648  1.4  christos           i += 2 * p;
   1649  1.4  christos           break;
   1650  1.4  christos         }
   1651  1.4  christos       }
   1652  1.4  christos       while (i + p <= n - q) {
   1653  1.4  christos         for (j = i;j < i + p;++j) {
   1654  1.4  christos           int32 a = x[j + p];
   1655  1.4  christos           for (r = q;r > p;r >>= 1)
   1656  1.4  christos             int32_MINMAX(a,x[j+r]);
   1657  1.4  christos           x[j + p] = a;
   1658  1.4  christos         }
   1659  1.4  christos         i += 2 * p;
   1660  1.4  christos       }
   1661  1.4  christos       /* now i + p > n - q */
   1662  1.4  christos       j = i;
   1663  1.4  christos       while (j < n - q) {
   1664  1.4  christos         int32 a = x[j + p];
   1665  1.4  christos         for (r = q;r > p;r >>= 1)
   1666  1.4  christos           int32_MINMAX(a,x[j+r]);
   1667  1.4  christos         x[j + p] = a;
   1668  1.4  christos         ++j;
   1669  1.4  christos       }
   1670  1.1  christos 
   1671  1.4  christos       done: ;
   1672  1.4  christos     }
   1673  1.1  christos   }
   1674  1.1  christos }
   1675  1.1  christos 
   1676  1.4  christos /* from supercop-20240808/crypto_sort/uint32/useint32/sort.c */
   1677  1.1  christos 
   1678  1.4  christos /* can save time by vectorizing xor loops */
   1679  1.4  christos /* can save time by integrating xor loops with int32_sort */
   1680  1.1  christos 
   1681  1.4  christos static void crypto_sort_uint32(void *array,long long n)
   1682  1.1  christos {
   1683  1.4  christos   crypto_uint32 *x = array;
   1684  1.4  christos   long long j;
   1685  1.4  christos   for (j = 0;j < n;++j) x[j] ^= 0x80000000;
   1686  1.4  christos   crypto_sort_int32(array,n);
   1687  1.4  christos   for (j = 0;j < n;++j) x[j] ^= 0x80000000;
   1688  1.1  christos }
   1689  1.1  christos 
   1690  1.4  christos /* from supercop-20240808/crypto_kem/sntrup761/compact/kem.c */
   1691  1.4  christos // 20240806 djb: some automated conversion to cryptoint
   1692  1.1  christos 
   1693  1.4  christos #define p 761
   1694  1.4  christos #define q 4591
   1695  1.4  christos #define w 286
   1696  1.4  christos #define q12 ((q - 1) / 2)
   1697  1.4  christos typedef int8_t small;
   1698  1.4  christos typedef int16_t Fq;
   1699  1.4  christos #define Hash_bytes 32
   1700  1.4  christos #define Small_bytes ((p + 3) / 4)
   1701  1.4  christos typedef small Inputs[p];
   1702  1.4  christos #define SecretKeys_bytes (2 * Small_bytes)
   1703  1.4  christos #define Confirm_bytes 32
   1704  1.1  christos 
   1705  1.4  christos static small F3_freeze(int16_t x) { return x - 3 * ((10923 * x + 16384) >> 15); }
   1706  1.1  christos 
   1707  1.4  christos static Fq Fq_freeze(int32_t x) {
   1708  1.4  christos   const int32_t q16 = (0x10000 + q / 2) / q;
   1709  1.4  christos   const int32_t q20 = (0x100000 + q / 2) / q;
   1710  1.4  christos   const int32_t q28 = (0x10000000 + q / 2) / q;
   1711  1.4  christos   x -= q * ((q16 * x) >> 16);
   1712  1.4  christos   x -= q * ((q20 * x) >> 20);
   1713  1.4  christos   return x - q * ((q28 * x + 0x8000000) >> 28);
   1714  1.4  christos }
   1715  1.4  christos 
   1716  1.4  christos static int Weightw_mask(small *r) {
   1717  1.4  christos   int i, weight = 0;
   1718  1.4  christos   for (i = 0; i < p; ++i) weight += crypto_int64_bottombit_01(r[i]);
   1719  1.4  christos   return crypto_int16_nonzero_mask(weight - w);
   1720  1.4  christos }
   1721  1.4  christos 
   1722  1.4  christos static void uint32_divmod_uint14(uint32_t *Q, uint16_t *r, uint32_t x, uint16_t m) {
   1723  1.4  christos   uint32_t qpart, mask, v = 0x80000000 / m;
   1724  1.4  christos   qpart = (x * (uint64_t)v) >> 31;
   1725  1.4  christos   x -= qpart * m;
   1726  1.4  christos   *Q = qpart;
   1727  1.4  christos   qpart = (x * (uint64_t)v) >> 31;
   1728  1.4  christos   x -= qpart * m;
   1729  1.4  christos   *Q += qpart;
   1730  1.4  christos   x -= m;
   1731  1.4  christos   *Q += 1;
   1732  1.4  christos   mask = crypto_int32_negative_mask(x);
   1733  1.4  christos   x += mask & (uint32_t)m;
   1734  1.4  christos   *Q += mask;
   1735  1.4  christos   *r = x;
   1736  1.4  christos }
   1737  1.1  christos 
   1738  1.4  christos static uint16_t uint32_mod_uint14(uint32_t x, uint16_t m) {
   1739  1.4  christos   uint32_t Q;
   1740  1.4  christos   uint16_t r;
   1741  1.4  christos   uint32_divmod_uint14(&Q, &r, x, m);
   1742  1.4  christos   return r;
   1743  1.1  christos }
   1744  1.1  christos 
   1745  1.4  christos static void Encode(unsigned char *out, const uint16_t *R, const uint16_t *M, long long len) {
   1746  1.4  christos   if (len == 1) {
   1747  1.4  christos     uint16_t r = R[0], m = M[0];
   1748  1.4  christos     while (m > 1) {
   1749  1.4  christos       *out++ = r;
   1750  1.4  christos       r >>= 8;
   1751  1.4  christos       m = (m + 255) >> 8;
   1752  1.4  christos     }
   1753  1.4  christos   }
   1754  1.4  christos   if (len > 1) {
   1755  1.4  christos     uint16_t R2[(len + 1) / 2], M2[(len + 1) / 2];
   1756  1.4  christos     long long i;
   1757  1.4  christos     for (i = 0; i < len - 1; i += 2) {
   1758  1.4  christos       uint32_t m0 = M[i];
   1759  1.4  christos       uint32_t r = R[i] + R[i + 1] * m0;
   1760  1.4  christos       uint32_t m = M[i + 1] * m0;
   1761  1.4  christos       while (m >= 16384) {
   1762  1.4  christos         *out++ = r;
   1763  1.4  christos         r >>= 8;
   1764  1.4  christos         m = (m + 255) >> 8;
   1765  1.4  christos       }
   1766  1.4  christos       R2[i / 2] = r;
   1767  1.4  christos       M2[i / 2] = m;
   1768  1.4  christos     }
   1769  1.4  christos     if (i < len) {
   1770  1.4  christos       R2[i / 2] = R[i];
   1771  1.4  christos       M2[i / 2] = M[i];
   1772  1.4  christos     }
   1773  1.4  christos     Encode(out, R2, M2, (len + 1) / 2);
   1774  1.4  christos   }
   1775  1.4  christos }
   1776  1.1  christos 
   1777  1.4  christos static void Decode(uint16_t *out, const unsigned char *S, const uint16_t *M, long long len) {
   1778  1.4  christos   if (len == 1) {
   1779  1.4  christos     if (M[0] == 1)
   1780  1.4  christos       *out = 0;
   1781  1.4  christos     else if (M[0] <= 256)
   1782  1.4  christos       *out = uint32_mod_uint14(S[0], M[0]);
   1783  1.4  christos     else
   1784  1.4  christos       *out = uint32_mod_uint14(S[0] + (((uint16_t)S[1]) << 8), M[0]);
   1785  1.4  christos   }
   1786  1.4  christos   if (len > 1) {
   1787  1.4  christos     uint16_t R2[(len + 1) / 2], M2[(len + 1) / 2], bottomr[len / 2];
   1788  1.4  christos     uint32_t bottomt[len / 2];
   1789  1.4  christos     long long i;
   1790  1.4  christos     for (i = 0; i < len - 1; i += 2) {
   1791  1.4  christos       uint32_t m = M[i] * (uint32_t)M[i + 1];
   1792  1.4  christos       if (m > 256 * 16383) {
   1793  1.4  christos         bottomt[i / 2] = 256 * 256;
   1794  1.4  christos         bottomr[i / 2] = S[0] + 256 * S[1];
   1795  1.4  christos         S += 2;
   1796  1.4  christos         M2[i / 2] = (((m + 255) >> 8) + 255) >> 8;
   1797  1.4  christos       } else if (m >= 16384) {
   1798  1.4  christos         bottomt[i / 2] = 256;
   1799  1.4  christos         bottomr[i / 2] = S[0];
   1800  1.4  christos         S += 1;
   1801  1.4  christos         M2[i / 2] = (m + 255) >> 8;
   1802  1.4  christos       } else {
   1803  1.4  christos         bottomt[i / 2] = 1;
   1804  1.4  christos         bottomr[i / 2] = 0;
   1805  1.4  christos         M2[i / 2] = m;
   1806  1.4  christos       }
   1807  1.4  christos     }
   1808  1.4  christos     if (i < len) M2[i / 2] = M[i];
   1809  1.4  christos     Decode(R2, S, M2, (len + 1) / 2);
   1810  1.4  christos     for (i = 0; i < len - 1; i += 2) {
   1811  1.4  christos       uint32_t r1, r = bottomr[i / 2];
   1812  1.4  christos       uint16_t r0;
   1813  1.4  christos       r += bottomt[i / 2] * R2[i / 2];
   1814  1.4  christos       uint32_divmod_uint14(&r1, &r0, r, M[i]);
   1815  1.4  christos       r1 = uint32_mod_uint14(r1, M[i + 1]);
   1816  1.4  christos       *out++ = r0;
   1817  1.4  christos       *out++ = r1;
   1818  1.4  christos     }
   1819  1.4  christos     if (i < len) *out++ = R2[i / 2];
   1820  1.4  christos   }
   1821  1.1  christos }
   1822  1.1  christos 
   1823  1.4  christos static void R3_fromRq(small *out, const Fq *r) {
   1824  1.4  christos   int i;
   1825  1.4  christos   for (i = 0; i < p; ++i) out[i] = F3_freeze(r[i]);
   1826  1.4  christos }
   1827  1.3  christos 
   1828  1.4  christos static void R3_mult(small *h, const small *f, const small *g) {
   1829  1.4  christos   int16_t fg[p + p - 1];
   1830  1.4  christos   int i, j;
   1831  1.4  christos   for (i = 0; i < p + p - 1; ++i) fg[i] = 0;
   1832  1.4  christos   for (i = 0; i < p; ++i)
   1833  1.4  christos     for (j = 0; j < p; ++j) fg[i + j] += f[i] * (int16_t)g[j];
   1834  1.4  christos   for (i = p; i < p + p - 1; ++i) fg[i - p] += fg[i];
   1835  1.4  christos   for (i = p; i < p + p - 1; ++i) fg[i - p + 1] += fg[i];
   1836  1.4  christos   for (i = 0; i < p; ++i) h[i] = F3_freeze(fg[i]);
   1837  1.4  christos }
   1838  1.4  christos 
   1839  1.4  christos static int R3_recip(small *out, const small *in) {
   1840  1.4  christos   small f[p + 1], g[p + 1], v[p + 1], r[p + 1];
   1841  1.4  christos   int sign, swap, t, i, loop, delta = 1;
   1842  1.4  christos   for (i = 0; i < p + 1; ++i) v[i] = 0;
   1843  1.4  christos   for (i = 0; i < p + 1; ++i) r[i] = 0;
   1844  1.4  christos   r[0] = 1;
   1845  1.4  christos   for (i = 0; i < p; ++i) f[i] = 0;
   1846  1.4  christos   f[0] = 1;
   1847  1.4  christos   f[p - 1] = f[p] = -1;
   1848  1.4  christos   for (i = 0; i < p; ++i) g[p - 1 - i] = in[i];
   1849  1.4  christos   g[p] = 0;
   1850  1.4  christos   for (loop = 0; loop < 2 * p - 1; ++loop) {
   1851  1.4  christos     for (i = p; i > 0; --i) v[i] = v[i - 1];
   1852  1.4  christos     v[0] = 0;
   1853  1.4  christos     sign = -g[0] * f[0];
   1854  1.4  christos     swap = crypto_int16_negative_mask(-delta) & crypto_int16_nonzero_mask(g[0]);
   1855  1.4  christos     delta ^= swap & (delta ^ -delta);
   1856  1.4  christos     delta += 1;
   1857  1.4  christos     for (i = 0; i < p + 1; ++i) {
   1858  1.4  christos       t = swap & (f[i] ^ g[i]);
   1859  1.4  christos       f[i] ^= t;
   1860  1.4  christos       g[i] ^= t;
   1861  1.4  christos       t = swap & (v[i] ^ r[i]);
   1862  1.4  christos       v[i] ^= t;
   1863  1.4  christos       r[i] ^= t;
   1864  1.4  christos     }
   1865  1.4  christos     for (i = 0; i < p + 1; ++i) g[i] = F3_freeze(g[i] + sign * f[i]);
   1866  1.4  christos     for (i = 0; i < p + 1; ++i) r[i] = F3_freeze(r[i] + sign * v[i]);
   1867  1.4  christos     for (i = 0; i < p; ++i) g[i] = g[i + 1];
   1868  1.4  christos     g[p] = 0;
   1869  1.4  christos   }
   1870  1.4  christos   sign = f[0];
   1871  1.4  christos   for (i = 0; i < p; ++i) out[i] = sign * v[p - 1 - i];
   1872  1.4  christos   return crypto_int16_nonzero_mask(delta);
   1873  1.1  christos }
   1874  1.1  christos 
   1875  1.4  christos static void Rq_mult_small(Fq *h, const Fq *f, const small *g) {
   1876  1.4  christos   int32_t fg[p + p - 1];
   1877  1.4  christos   int i, j;
   1878  1.4  christos   for (i = 0; i < p + p - 1; ++i) fg[i] = 0;
   1879  1.4  christos   for (i = 0; i < p; ++i)
   1880  1.4  christos     for (j = 0; j < p; ++j) fg[i + j] += f[i] * (int32_t)g[j];
   1881  1.4  christos   for (i = p; i < p + p - 1; ++i) fg[i - p] += fg[i];
   1882  1.4  christos   for (i = p; i < p + p - 1; ++i) fg[i - p + 1] += fg[i];
   1883  1.4  christos   for (i = 0; i < p; ++i) h[i] = Fq_freeze(fg[i]);
   1884  1.1  christos }
   1885  1.1  christos 
   1886  1.4  christos static void Rq_mult3(Fq *h, const Fq *f) {
   1887  1.1  christos   int i;
   1888  1.4  christos   for (i = 0; i < p; ++i) h[i] = Fq_freeze(3 * f[i]);
   1889  1.4  christos }
   1890  1.1  christos 
   1891  1.4  christos static Fq Fq_recip(Fq a1) {
   1892  1.4  christos   int i = 1;
   1893  1.4  christos   Fq ai = a1;
   1894  1.4  christos   while (i < q - 2) {
   1895  1.4  christos     ai = Fq_freeze(a1 * (int32_t)ai);
   1896  1.4  christos     i += 1;
   1897  1.1  christos   }
   1898  1.4  christos   return ai;
   1899  1.1  christos }
   1900  1.1  christos 
   1901  1.4  christos static int Rq_recip3(Fq *out, const small *in) {
   1902  1.4  christos   Fq f[p + 1], g[p + 1], v[p + 1], r[p + 1], scale;
   1903  1.4  christos   int swap, t, i, loop, delta = 1;
   1904  1.4  christos   int32_t f0, g0;
   1905  1.4  christos   for (i = 0; i < p + 1; ++i) v[i] = 0;
   1906  1.4  christos   for (i = 0; i < p + 1; ++i) r[i] = 0;
   1907  1.4  christos   r[0] = Fq_recip(3);
   1908  1.4  christos   for (i = 0; i < p; ++i) f[i] = 0;
   1909  1.4  christos   f[0] = 1;
   1910  1.4  christos   f[p - 1] = f[p] = -1;
   1911  1.4  christos   for (i = 0; i < p; ++i) g[p - 1 - i] = in[i];
   1912  1.4  christos   g[p] = 0;
   1913  1.4  christos   for (loop = 0; loop < 2 * p - 1; ++loop) {
   1914  1.4  christos     for (i = p; i > 0; --i) v[i] = v[i - 1];
   1915  1.4  christos     v[0] = 0;
   1916  1.4  christos     swap = crypto_int16_negative_mask(-delta) & crypto_int16_nonzero_mask(g[0]);
   1917  1.4  christos     delta ^= swap & (delta ^ -delta);
   1918  1.4  christos     delta += 1;
   1919  1.4  christos     for (i = 0; i < p + 1; ++i) {
   1920  1.4  christos       t = swap & (f[i] ^ g[i]);
   1921  1.4  christos       f[i] ^= t;
   1922  1.4  christos       g[i] ^= t;
   1923  1.4  christos       t = swap & (v[i] ^ r[i]);
   1924  1.4  christos       v[i] ^= t;
   1925  1.4  christos       r[i] ^= t;
   1926  1.4  christos     }
   1927  1.4  christos     f0 = f[0];
   1928  1.4  christos     g0 = g[0];
   1929  1.4  christos     for (i = 0; i < p + 1; ++i) g[i] = Fq_freeze(f0 * g[i] - g0 * f[i]);
   1930  1.4  christos     for (i = 0; i < p + 1; ++i) r[i] = Fq_freeze(f0 * r[i] - g0 * v[i]);
   1931  1.4  christos     for (i = 0; i < p; ++i) g[i] = g[i + 1];
   1932  1.4  christos     g[p] = 0;
   1933  1.1  christos   }
   1934  1.4  christos   scale = Fq_recip(f[0]);
   1935  1.4  christos   for (i = 0; i < p; ++i) out[i] = Fq_freeze(scale * (int32_t)v[p - 1 - i]);
   1936  1.4  christos   return crypto_int16_nonzero_mask(delta);
   1937  1.1  christos }
   1938  1.1  christos 
   1939  1.4  christos static void Round(Fq *out, const Fq *a) {
   1940  1.4  christos   int i;
   1941  1.4  christos   for (i = 0; i < p; ++i) out[i] = a[i] - F3_freeze(a[i]);
   1942  1.4  christos }
   1943  1.1  christos 
   1944  1.4  christos static void Short_fromlist(small *out, const uint32_t *in) {
   1945  1.4  christos   uint32_t L[p];
   1946  1.1  christos   int i;
   1947  1.4  christos   for (i = 0; i < w; ++i) L[i] = in[i] & (uint32_t)-2;
   1948  1.4  christos   for (i = w; i < p; ++i) L[i] = (in[i] & (uint32_t)-3) | 1;
   1949  1.4  christos   crypto_sort_uint32(L, p);
   1950  1.4  christos   for (i = 0; i < p; ++i) out[i] = (L[i] & 3) - 1;
   1951  1.1  christos }
   1952  1.1  christos 
   1953  1.4  christos static void Hash_prefix(unsigned char *out, int b, const unsigned char *in, int inlen) {
   1954  1.4  christos   unsigned char x[inlen + 1], h[64];
   1955  1.1  christos   int i;
   1956  1.4  christos   x[0] = b;
   1957  1.4  christos   for (i = 0; i < inlen; ++i) x[i + 1] = in[i];
   1958  1.4  christos   crypto_hash_sha512(h, x, inlen + 1);
   1959  1.4  christos   for (i = 0; i < 32; ++i) out[i] = h[i];
   1960  1.1  christos }
   1961  1.3  christos 
   1962  1.4  christos static uint32_t urandom32(void) {
   1963  1.4  christos   unsigned char c[4];
   1964  1.4  christos   uint32_t result = 0;
   1965  1.1  christos   int i;
   1966  1.4  christos   randombytes(c, 4);
   1967  1.4  christos   for (i = 0; i < 4; ++i) result += ((uint32_t)c[i]) << (8 * i);
   1968  1.4  christos   return result;
   1969  1.1  christos }
   1970  1.1  christos 
   1971  1.4  christos static void Short_random(small *out) {
   1972  1.4  christos   uint32_t L[p];
   1973  1.1  christos   int i;
   1974  1.4  christos   for (i = 0; i < p; ++i) L[i] = urandom32();
   1975  1.4  christos   Short_fromlist(out, L);
   1976  1.1  christos }
   1977  1.1  christos 
   1978  1.4  christos static void Small_random(small *out) {
   1979  1.1  christos   int i;
   1980  1.4  christos   for (i = 0; i < p; ++i) out[i] = (((urandom32() & 0x3fffffff) * 3) >> 30) - 1;
   1981  1.1  christos }
   1982  1.1  christos 
   1983  1.4  christos static void KeyGen(Fq *h, small *f, small *ginv) {
   1984  1.4  christos   small g[p];
   1985  1.4  christos   Fq finv[p];
   1986  1.4  christos   for (;;) {
   1987  1.4  christos     int result;
   1988  1.4  christos     Small_random(g);
   1989  1.4  christos     result = R3_recip(ginv, g);
   1990  1.4  christos     crypto_declassify(&result, sizeof result);
   1991  1.4  christos     if (result == 0) break;
   1992  1.1  christos   }
   1993  1.4  christos   Short_random(f);
   1994  1.4  christos   Rq_recip3(finv, f);
   1995  1.4  christos   Rq_mult_small(h, finv, g);
   1996  1.1  christos }
   1997  1.1  christos 
   1998  1.4  christos static void Encrypt(Fq *c, const small *r, const Fq *h) {
   1999  1.4  christos   Fq hr[p];
   2000  1.4  christos   Rq_mult_small(hr, h, r);
   2001  1.4  christos   Round(c, hr);
   2002  1.4  christos }
   2003  1.1  christos 
   2004  1.4  christos static void Decrypt(small *r, const Fq *c, const small *f, const small *ginv) {
   2005  1.4  christos   Fq cf[p], cf3[p];
   2006  1.4  christos   small e[p], ev[p];
   2007  1.4  christos   int mask, i;
   2008  1.4  christos   Rq_mult_small(cf, c, f);
   2009  1.4  christos   Rq_mult3(cf3, cf);
   2010  1.4  christos   R3_fromRq(e, cf3);
   2011  1.4  christos   R3_mult(ev, e, ginv);
   2012  1.4  christos   mask = Weightw_mask(ev);
   2013  1.4  christos   for (i = 0; i < w; ++i) r[i] = ((ev[i] ^ 1) & ~mask) ^ 1;
   2014  1.4  christos   for (i = w; i < p; ++i) r[i] = ev[i] & ~mask;
   2015  1.4  christos }
   2016  1.4  christos 
   2017  1.4  christos static void Small_encode(unsigned char *s, const small *f) {
   2018  1.4  christos   int i, j;
   2019  1.4  christos   for (i = 0; i < p / 4; ++i) {
   2020  1.4  christos     small x = 0;
   2021  1.4  christos     for (j = 0;j < 4;++j) x += (*f++ + 1) << (2 * j);
   2022  1.4  christos     *s++ = x;
   2023  1.4  christos   }
   2024  1.4  christos   *s = *f++ + 1;
   2025  1.4  christos }
   2026  1.1  christos 
   2027  1.4  christos static void Small_decode(small *f, const unsigned char *s) {
   2028  1.4  christos   int i, j;
   2029  1.4  christos   for (i = 0; i < p / 4; ++i) {
   2030  1.4  christos     unsigned char x = *s++;
   2031  1.4  christos     for (j = 0;j < 4;++j) *f++ = ((small)((x >> (2 * j)) & 3)) - 1;
   2032  1.4  christos   }
   2033  1.4  christos   *f++ = ((small)(*s & 3)) - 1;
   2034  1.1  christos }
   2035  1.1  christos 
   2036  1.4  christos static void Rq_encode(unsigned char *s, const Fq *r) {
   2037  1.4  christos   uint16_t R[p], M[p];
   2038  1.4  christos   int i;
   2039  1.4  christos   for (i = 0; i < p; ++i) R[i] = r[i] + q12;
   2040  1.4  christos   for (i = 0; i < p; ++i) M[i] = q;
   2041  1.4  christos   Encode(s, R, M, p);
   2042  1.1  christos }
   2043  1.1  christos 
   2044  1.4  christos static void Rq_decode(Fq *r, const unsigned char *s) {
   2045  1.4  christos   uint16_t R[p], M[p];
   2046  1.4  christos   int i;
   2047  1.4  christos   for (i = 0; i < p; ++i) M[i] = q;
   2048  1.4  christos   Decode(R, s, M, p);
   2049  1.4  christos   for (i = 0; i < p; ++i) r[i] = ((Fq)R[i]) - q12;
   2050  1.1  christos }
   2051  1.1  christos 
   2052  1.4  christos static void Rounded_encode(unsigned char *s, const Fq *r) {
   2053  1.4  christos   uint16_t R[p], M[p];
   2054  1.1  christos   int i;
   2055  1.4  christos   for (i = 0; i < p; ++i) R[i] = ((r[i] + q12) * 10923) >> 15;
   2056  1.4  christos   for (i = 0; i < p; ++i) M[i] = (q + 2) / 3;
   2057  1.4  christos   Encode(s, R, M, p);
   2058  1.1  christos }
   2059  1.1  christos 
   2060  1.4  christos static void Rounded_decode(Fq *r, const unsigned char *s) {
   2061  1.4  christos   uint16_t R[p], M[p];
   2062  1.4  christos   int i;
   2063  1.4  christos   for (i = 0; i < p; ++i) M[i] = (q + 2) / 3;
   2064  1.4  christos   Decode(R, s, M, p);
   2065  1.4  christos   for (i = 0; i < p; ++i) r[i] = R[i] * 3 - q12;
   2066  1.1  christos }
   2067  1.1  christos 
   2068  1.4  christos static void ZKeyGen(unsigned char *pk, unsigned char *sk) {
   2069  1.4  christos   Fq h[p];
   2070  1.4  christos   small f[p], v[p];
   2071  1.4  christos   KeyGen(h, f, v);
   2072  1.4  christos   Rq_encode(pk, h);
   2073  1.4  christos   Small_encode(sk, f);
   2074  1.4  christos   Small_encode(sk + Small_bytes, v);
   2075  1.4  christos }
   2076  1.4  christos 
   2077  1.4  christos static void ZEncrypt(unsigned char *C, const Inputs r, const unsigned char *pk) {
   2078  1.4  christos   Fq h[p], c[p];
   2079  1.4  christos   Rq_decode(h, pk);
   2080  1.4  christos   Encrypt(c, r, h);
   2081  1.4  christos   Rounded_encode(C, c);
   2082  1.1  christos }
   2083  1.1  christos 
   2084  1.4  christos static void ZDecrypt(Inputs r, const unsigned char *C, const unsigned char *sk) {
   2085  1.4  christos   small f[p], v[p];
   2086  1.4  christos   Fq c[p];
   2087  1.4  christos   Small_decode(f, sk);
   2088  1.4  christos   Small_decode(v, sk + Small_bytes);
   2089  1.4  christos   Rounded_decode(c, C);
   2090  1.4  christos   Decrypt(r, c, f, v);
   2091  1.1  christos }
   2092  1.1  christos 
   2093  1.4  christos static void HashConfirm(unsigned char *h, const unsigned char *r, const unsigned char *cache) {
   2094  1.4  christos   unsigned char x[Hash_bytes * 2];
   2095  1.1  christos   int i;
   2096  1.4  christos   Hash_prefix(x, 3, r, Small_bytes);
   2097  1.4  christos   for (i = 0; i < Hash_bytes; ++i) x[Hash_bytes + i] = cache[i];
   2098  1.4  christos   Hash_prefix(h, 2, x, sizeof x);
   2099  1.1  christos }
   2100  1.1  christos 
   2101  1.4  christos static void HashSession(unsigned char *k, int b, const unsigned char *y, const unsigned char *z) {
   2102  1.4  christos   unsigned char x[Hash_bytes + crypto_kem_sntrup761_CIPHERTEXTBYTES];
   2103  1.1  christos   int i;
   2104  1.4  christos   Hash_prefix(x, 3, y, Small_bytes);
   2105  1.4  christos   for (i = 0; i < crypto_kem_sntrup761_CIPHERTEXTBYTES; ++i) x[Hash_bytes + i] = z[i];
   2106  1.4  christos   Hash_prefix(k, b, x, sizeof x);
   2107  1.1  christos }
   2108  1.1  christos 
   2109  1.4  christos int crypto_kem_sntrup761_keypair(unsigned char *pk, unsigned char *sk) {
   2110  1.1  christos   int i;
   2111  1.4  christos   ZKeyGen(pk, sk);
   2112  1.4  christos   sk += SecretKeys_bytes;
   2113  1.4  christos   for (i = 0; i < crypto_kem_sntrup761_PUBLICKEYBYTES; ++i) *sk++ = pk[i];
   2114  1.4  christos   randombytes(sk, Small_bytes);
   2115  1.4  christos   Hash_prefix(sk + Small_bytes, 4, pk, crypto_kem_sntrup761_PUBLICKEYBYTES);
   2116  1.4  christos   return 0;
   2117  1.1  christos }
   2118  1.1  christos 
   2119  1.4  christos static void Hide(unsigned char *c, unsigned char *r_enc, const Inputs r, const unsigned char *pk, const unsigned char *cache) {
   2120  1.4  christos   Small_encode(r_enc, r);
   2121  1.4  christos   ZEncrypt(c, r, pk);
   2122  1.4  christos   HashConfirm(c + crypto_kem_sntrup761_CIPHERTEXTBYTES - Confirm_bytes, r_enc, cache);
   2123  1.1  christos }
   2124  1.1  christos 
   2125  1.4  christos int crypto_kem_sntrup761_enc(unsigned char *c, unsigned char *k, const unsigned char *pk) {
   2126  1.1  christos   Inputs r;
   2127  1.4  christos   unsigned char r_enc[Small_bytes], cache[Hash_bytes];
   2128  1.4  christos   Hash_prefix(cache, 4, pk, crypto_kem_sntrup761_PUBLICKEYBYTES);
   2129  1.4  christos   Short_random(r);
   2130  1.4  christos   Hide(c, r_enc, r, pk, cache);
   2131  1.4  christos   HashSession(k, 1, r_enc, c);
   2132  1.4  christos   return 0;
   2133  1.1  christos }
   2134  1.1  christos 
   2135  1.4  christos static int Ciphertexts_diff_mask(const unsigned char *c, const unsigned char *c2) {
   2136  1.4  christos   uint16_t differentbits = 0;
   2137  1.4  christos   int len = crypto_kem_sntrup761_CIPHERTEXTBYTES;
   2138  1.4  christos   while (len-- > 0) differentbits |= (*c++) ^ (*c2++);
   2139  1.4  christos   return (crypto_int64_bitmod_01((differentbits - 1),8)) - 1;
   2140  1.1  christos }
   2141  1.1  christos 
   2142  1.4  christos int crypto_kem_sntrup761_dec(unsigned char *k, const unsigned char *c, const unsigned char *sk) {
   2143  1.1  christos   const unsigned char *pk = sk + SecretKeys_bytes;
   2144  1.4  christos   const unsigned char *rho = pk + crypto_kem_sntrup761_PUBLICKEYBYTES;
   2145  1.4  christos   const unsigned char *cache = rho + Small_bytes;
   2146  1.1  christos   Inputs r;
   2147  1.4  christos   unsigned char r_enc[Small_bytes], cnew[crypto_kem_sntrup761_CIPHERTEXTBYTES];
   2148  1.4  christos   int mask, i;
   2149  1.4  christos   ZDecrypt(r, c, sk);
   2150  1.4  christos   Hide(cnew, r_enc, r, pk, cache);
   2151  1.4  christos   mask = Ciphertexts_diff_mask(c, cnew);
   2152  1.4  christos   for (i = 0; i < Small_bytes; ++i) r_enc[i] ^= mask & (r_enc[i] ^ rho[i]);
   2153  1.4  christos   HashSession(k, 1 + mask, r_enc, c);
   2154  1.1  christos   return 0;
   2155  1.1  christos }
   2156  1.1  christos 
   2157