xref: /freebsd/crypto/openssh/sntrup761.c (revision 3d9fd9fcb432750f3716b28f6ccb0104cd9d351a)
1*3d9fd9fcSEd Maste 
2*3d9fd9fcSEd Maste /*  $OpenBSD: sntrup761.c,v 1.8 2024/09/16 05:37:05 djm Exp $ */
319261079SEd Maste 
419261079SEd Maste /*
519261079SEd Maste  * Public Domain, Authors:
619261079SEd Maste  * - Daniel J. Bernstein
719261079SEd Maste  * - Chitchanok Chuengsatiansup
819261079SEd Maste  * - Tanja Lange
919261079SEd Maste  * - Christine van Vredendaal
1019261079SEd Maste  */
1119261079SEd Maste 
1219261079SEd Maste #include "includes.h"
1319261079SEd Maste 
1419261079SEd Maste #ifdef USE_SNTRUP761X25519
1519261079SEd Maste 
1619261079SEd Maste #include <string.h>
1719261079SEd Maste #include "crypto_api.h"
1819261079SEd Maste 
19*3d9fd9fcSEd Maste #define crypto_declassify(x, y) do {} while (0)
20*3d9fd9fcSEd Maste 
2119261079SEd Maste #define int8 crypto_int8
2219261079SEd Maste #define uint8 crypto_uint8
2319261079SEd Maste #define int16 crypto_int16
2419261079SEd Maste #define uint16 crypto_uint16
2519261079SEd Maste #define int32 crypto_int32
2619261079SEd Maste #define uint32 crypto_uint32
2719261079SEd Maste #define int64 crypto_int64
2819261079SEd Maste #define uint64 crypto_uint64
29*3d9fd9fcSEd Maste extern volatile crypto_int16 crypto_int16_optblocker;
30*3d9fd9fcSEd Maste extern volatile crypto_int32 crypto_int32_optblocker;
31*3d9fd9fcSEd Maste extern volatile crypto_int64 crypto_int64_optblocker;
3219261079SEd Maste 
33*3d9fd9fcSEd Maste /* from supercop-20240808/cryptoint/crypto_int16.h */
34*3d9fd9fcSEd Maste /* auto-generated: cd cryptoint; ./autogen */
35*3d9fd9fcSEd Maste /* cryptoint 20240806 */
3619261079SEd Maste 
37*3d9fd9fcSEd Maste #ifndef crypto_int16_h
38*3d9fd9fcSEd Maste #define crypto_int16_h
3919261079SEd Maste 
40*3d9fd9fcSEd Maste #define crypto_int16 int16_t
41*3d9fd9fcSEd Maste #define crypto_int16_unsigned uint16_t
42*3d9fd9fcSEd Maste 
43*3d9fd9fcSEd Maste 
44*3d9fd9fcSEd Maste 
45*3d9fd9fcSEd Maste __attribute__((unused))
46*3d9fd9fcSEd Maste static inline
crypto_int16_load(const unsigned char * crypto_int16_s)47*3d9fd9fcSEd Maste crypto_int16 crypto_int16_load(const unsigned char *crypto_int16_s) {
48*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z = 0;
49*3d9fd9fcSEd Maste   crypto_int16_z |= ((crypto_int16) (*crypto_int16_s++)) << 0;
50*3d9fd9fcSEd Maste   crypto_int16_z |= ((crypto_int16) (*crypto_int16_s++)) << 8;
51*3d9fd9fcSEd Maste   return crypto_int16_z;
52*3d9fd9fcSEd Maste }
53*3d9fd9fcSEd Maste 
54*3d9fd9fcSEd Maste __attribute__((unused))
55*3d9fd9fcSEd Maste static inline
crypto_int16_store(unsigned char * crypto_int16_s,crypto_int16 crypto_int16_x)56*3d9fd9fcSEd Maste void crypto_int16_store(unsigned char *crypto_int16_s,crypto_int16 crypto_int16_x) {
57*3d9fd9fcSEd Maste   *crypto_int16_s++ = crypto_int16_x >> 0;
58*3d9fd9fcSEd Maste   *crypto_int16_s++ = crypto_int16_x >> 8;
59*3d9fd9fcSEd Maste }
60*3d9fd9fcSEd Maste 
61*3d9fd9fcSEd Maste __attribute__((unused))
62*3d9fd9fcSEd Maste static inline
crypto_int16_negative_mask(crypto_int16 crypto_int16_x)63*3d9fd9fcSEd Maste crypto_int16 crypto_int16_negative_mask(crypto_int16 crypto_int16_x) {
64*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
65*3d9fd9fcSEd Maste   __asm__ ("sarw $15,%0" : "+r"(crypto_int16_x) : : "cc");
66*3d9fd9fcSEd Maste   return crypto_int16_x;
67*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
68*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_y;
69*3d9fd9fcSEd Maste   __asm__ ("sbfx %w0,%w1,15,1" : "=r"(crypto_int16_y) : "r"(crypto_int16_x) : );
70*3d9fd9fcSEd Maste   return crypto_int16_y;
71*3d9fd9fcSEd Maste #else
72*3d9fd9fcSEd Maste   crypto_int16_x >>= 16-6;
73*3d9fd9fcSEd Maste   crypto_int16_x ^= crypto_int16_optblocker;
74*3d9fd9fcSEd Maste   crypto_int16_x >>= 5;
75*3d9fd9fcSEd Maste   return crypto_int16_x;
76*3d9fd9fcSEd Maste #endif
77*3d9fd9fcSEd Maste }
78*3d9fd9fcSEd Maste 
79*3d9fd9fcSEd Maste __attribute__((unused))
80*3d9fd9fcSEd Maste static inline
crypto_int16_unsigned_topbit_01(crypto_int16_unsigned crypto_int16_x)81*3d9fd9fcSEd Maste crypto_int16_unsigned crypto_int16_unsigned_topbit_01(crypto_int16_unsigned crypto_int16_x) {
82*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
83*3d9fd9fcSEd Maste   __asm__ ("shrw $15,%0" : "+r"(crypto_int16_x) : : "cc");
84*3d9fd9fcSEd Maste   return crypto_int16_x;
85*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
86*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_y;
87*3d9fd9fcSEd Maste   __asm__ ("ubfx %w0,%w1,15,1" : "=r"(crypto_int16_y) : "r"(crypto_int16_x) : );
88*3d9fd9fcSEd Maste   return crypto_int16_y;
89*3d9fd9fcSEd Maste #else
90*3d9fd9fcSEd Maste   crypto_int16_x >>= 16-6;
91*3d9fd9fcSEd Maste   crypto_int16_x ^= crypto_int16_optblocker;
92*3d9fd9fcSEd Maste   crypto_int16_x >>= 5;
93*3d9fd9fcSEd Maste   return crypto_int16_x;
94*3d9fd9fcSEd Maste #endif
95*3d9fd9fcSEd Maste }
96*3d9fd9fcSEd Maste 
97*3d9fd9fcSEd Maste __attribute__((unused))
98*3d9fd9fcSEd Maste static inline
crypto_int16_negative_01(crypto_int16 crypto_int16_x)99*3d9fd9fcSEd Maste crypto_int16 crypto_int16_negative_01(crypto_int16 crypto_int16_x) {
100*3d9fd9fcSEd Maste   return crypto_int16_unsigned_topbit_01(crypto_int16_x);
101*3d9fd9fcSEd Maste }
102*3d9fd9fcSEd Maste 
103*3d9fd9fcSEd Maste __attribute__((unused))
104*3d9fd9fcSEd Maste static inline
crypto_int16_topbit_mask(crypto_int16 crypto_int16_x)105*3d9fd9fcSEd Maste crypto_int16 crypto_int16_topbit_mask(crypto_int16 crypto_int16_x) {
106*3d9fd9fcSEd Maste   return crypto_int16_negative_mask(crypto_int16_x);
107*3d9fd9fcSEd Maste }
108*3d9fd9fcSEd Maste 
109*3d9fd9fcSEd Maste __attribute__((unused))
110*3d9fd9fcSEd Maste static inline
crypto_int16_topbit_01(crypto_int16 crypto_int16_x)111*3d9fd9fcSEd Maste crypto_int16 crypto_int16_topbit_01(crypto_int16 crypto_int16_x) {
112*3d9fd9fcSEd Maste   return crypto_int16_unsigned_topbit_01(crypto_int16_x);
113*3d9fd9fcSEd Maste }
114*3d9fd9fcSEd Maste 
115*3d9fd9fcSEd Maste __attribute__((unused))
116*3d9fd9fcSEd Maste static inline
crypto_int16_bottombit_mask(crypto_int16 crypto_int16_x)117*3d9fd9fcSEd Maste crypto_int16 crypto_int16_bottombit_mask(crypto_int16 crypto_int16_x) {
118*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
119*3d9fd9fcSEd Maste   __asm__ ("andw $1,%0" : "+r"(crypto_int16_x) : : "cc");
120*3d9fd9fcSEd Maste   return -crypto_int16_x;
121*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
122*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_y;
123*3d9fd9fcSEd Maste   __asm__ ("sbfx %w0,%w1,0,1" : "=r"(crypto_int16_y) : "r"(crypto_int16_x) : );
124*3d9fd9fcSEd Maste   return crypto_int16_y;
125*3d9fd9fcSEd Maste #else
126*3d9fd9fcSEd Maste   crypto_int16_x &= 1 ^ crypto_int16_optblocker;
127*3d9fd9fcSEd Maste   return -crypto_int16_x;
128*3d9fd9fcSEd Maste #endif
129*3d9fd9fcSEd Maste }
130*3d9fd9fcSEd Maste 
131*3d9fd9fcSEd Maste __attribute__((unused))
132*3d9fd9fcSEd Maste static inline
crypto_int16_bottombit_01(crypto_int16 crypto_int16_x)133*3d9fd9fcSEd Maste crypto_int16 crypto_int16_bottombit_01(crypto_int16 crypto_int16_x) {
134*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
135*3d9fd9fcSEd Maste   __asm__ ("andw $1,%0" : "+r"(crypto_int16_x) : : "cc");
136*3d9fd9fcSEd Maste   return crypto_int16_x;
137*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
138*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_y;
139*3d9fd9fcSEd Maste   __asm__ ("ubfx %w0,%w1,0,1" : "=r"(crypto_int16_y) : "r"(crypto_int16_x) : );
140*3d9fd9fcSEd Maste   return crypto_int16_y;
141*3d9fd9fcSEd Maste #else
142*3d9fd9fcSEd Maste   crypto_int16_x &= 1 ^ crypto_int16_optblocker;
143*3d9fd9fcSEd Maste   return crypto_int16_x;
144*3d9fd9fcSEd Maste #endif
145*3d9fd9fcSEd Maste }
146*3d9fd9fcSEd Maste 
147*3d9fd9fcSEd Maste __attribute__((unused))
148*3d9fd9fcSEd Maste static inline
crypto_int16_bitinrangepublicpos_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s)149*3d9fd9fcSEd Maste crypto_int16 crypto_int16_bitinrangepublicpos_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) {
150*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
151*3d9fd9fcSEd Maste   __asm__ ("sarw %%cl,%0" : "+r"(crypto_int16_x) : "c"(crypto_int16_s) : "cc");
152*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
153*3d9fd9fcSEd Maste   __asm__ ("sxth %w0,%w0\n asr %w0,%w0,%w1" : "+&r"(crypto_int16_x) : "r"(crypto_int16_s) : );
154*3d9fd9fcSEd Maste #else
155*3d9fd9fcSEd Maste   crypto_int16_x >>= crypto_int16_s ^ crypto_int16_optblocker;
156*3d9fd9fcSEd Maste #endif
157*3d9fd9fcSEd Maste   return crypto_int16_bottombit_mask(crypto_int16_x);
158*3d9fd9fcSEd Maste }
159*3d9fd9fcSEd Maste 
160*3d9fd9fcSEd Maste __attribute__((unused))
161*3d9fd9fcSEd Maste static inline
crypto_int16_bitinrangepublicpos_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s)162*3d9fd9fcSEd Maste crypto_int16 crypto_int16_bitinrangepublicpos_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) {
163*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
164*3d9fd9fcSEd Maste   __asm__ ("sarw %%cl,%0" : "+r"(crypto_int16_x) : "c"(crypto_int16_s) : "cc");
165*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
166*3d9fd9fcSEd Maste   __asm__ ("sxth %w0,%w0\n asr %w0,%w0,%w1" : "+&r"(crypto_int16_x) : "r"(crypto_int16_s) : );
167*3d9fd9fcSEd Maste #else
168*3d9fd9fcSEd Maste   crypto_int16_x >>= crypto_int16_s ^ crypto_int16_optblocker;
169*3d9fd9fcSEd Maste #endif
170*3d9fd9fcSEd Maste   return crypto_int16_bottombit_01(crypto_int16_x);
171*3d9fd9fcSEd Maste }
172*3d9fd9fcSEd Maste 
173*3d9fd9fcSEd Maste __attribute__((unused))
174*3d9fd9fcSEd Maste static inline
crypto_int16_shlmod(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s)175*3d9fd9fcSEd Maste crypto_int16 crypto_int16_shlmod(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) {
176*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
177*3d9fd9fcSEd Maste   crypto_int16_s &= 15;
178*3d9fd9fcSEd Maste   __asm__ ("shlw %%cl,%0" : "+r"(crypto_int16_x) : "c"(crypto_int16_s) : "cc");
179*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
180*3d9fd9fcSEd Maste   __asm__ ("and %w0,%w0,15\n and %w1,%w1,65535\n lsl %w1,%w1,%w0" : "+&r"(crypto_int16_s), "+r"(crypto_int16_x) : : );
181*3d9fd9fcSEd Maste #else
182*3d9fd9fcSEd Maste   int crypto_int16_k, crypto_int16_l;
183*3d9fd9fcSEd Maste   for (crypto_int16_l = 0,crypto_int16_k = 1;crypto_int16_k < 16;++crypto_int16_l,crypto_int16_k *= 2)
184*3d9fd9fcSEd Maste     crypto_int16_x ^= (crypto_int16_x ^ (crypto_int16_x << crypto_int16_k)) & crypto_int16_bitinrangepublicpos_mask(crypto_int16_s,crypto_int16_l);
185*3d9fd9fcSEd Maste #endif
186*3d9fd9fcSEd Maste   return crypto_int16_x;
187*3d9fd9fcSEd Maste }
188*3d9fd9fcSEd Maste 
189*3d9fd9fcSEd Maste __attribute__((unused))
190*3d9fd9fcSEd Maste static inline
crypto_int16_shrmod(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s)191*3d9fd9fcSEd Maste crypto_int16 crypto_int16_shrmod(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) {
192*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
193*3d9fd9fcSEd Maste   crypto_int16_s &= 15;
194*3d9fd9fcSEd Maste   __asm__ ("sarw %%cl,%0" : "+r"(crypto_int16_x) : "c"(crypto_int16_s) : "cc");
195*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
196*3d9fd9fcSEd Maste   __asm__ ("and %w0,%w0,15\n sxth %w1,%w1\n asr %w1,%w1,%w0" : "+&r"(crypto_int16_s), "+r"(crypto_int16_x) : : );
197*3d9fd9fcSEd Maste #else
198*3d9fd9fcSEd Maste   int crypto_int16_k, crypto_int16_l;
199*3d9fd9fcSEd Maste   for (crypto_int16_l = 0,crypto_int16_k = 1;crypto_int16_k < 16;++crypto_int16_l,crypto_int16_k *= 2)
200*3d9fd9fcSEd Maste     crypto_int16_x ^= (crypto_int16_x ^ (crypto_int16_x >> crypto_int16_k)) & crypto_int16_bitinrangepublicpos_mask(crypto_int16_s,crypto_int16_l);
201*3d9fd9fcSEd Maste #endif
202*3d9fd9fcSEd Maste   return crypto_int16_x;
203*3d9fd9fcSEd Maste }
204*3d9fd9fcSEd Maste 
205*3d9fd9fcSEd Maste __attribute__((unused))
206*3d9fd9fcSEd Maste static inline
crypto_int16_bitmod_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s)207*3d9fd9fcSEd Maste crypto_int16 crypto_int16_bitmod_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) {
208*3d9fd9fcSEd Maste   crypto_int16_x = crypto_int16_shrmod(crypto_int16_x,crypto_int16_s);
209*3d9fd9fcSEd Maste   return crypto_int16_bottombit_mask(crypto_int16_x);
210*3d9fd9fcSEd Maste }
211*3d9fd9fcSEd Maste 
212*3d9fd9fcSEd Maste __attribute__((unused))
213*3d9fd9fcSEd Maste static inline
crypto_int16_bitmod_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s)214*3d9fd9fcSEd Maste crypto_int16 crypto_int16_bitmod_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) {
215*3d9fd9fcSEd Maste   crypto_int16_x = crypto_int16_shrmod(crypto_int16_x,crypto_int16_s);
216*3d9fd9fcSEd Maste   return crypto_int16_bottombit_01(crypto_int16_x);
217*3d9fd9fcSEd Maste }
218*3d9fd9fcSEd Maste 
219*3d9fd9fcSEd Maste __attribute__((unused))
220*3d9fd9fcSEd Maste static inline
crypto_int16_nonzero_mask(crypto_int16 crypto_int16_x)221*3d9fd9fcSEd Maste crypto_int16 crypto_int16_nonzero_mask(crypto_int16 crypto_int16_x) {
222*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
223*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_q,crypto_int16_z;
224*3d9fd9fcSEd Maste   __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");
225*3d9fd9fcSEd Maste   return crypto_int16_z;
226*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
227*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z;
228*3d9fd9fcSEd Maste   __asm__ ("tst %w1,65535\n csetm %w0,ne" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc");
229*3d9fd9fcSEd Maste   return crypto_int16_z;
230*3d9fd9fcSEd Maste #else
231*3d9fd9fcSEd Maste   crypto_int16_x |= -crypto_int16_x;
232*3d9fd9fcSEd Maste   return crypto_int16_negative_mask(crypto_int16_x);
233*3d9fd9fcSEd Maste #endif
234*3d9fd9fcSEd Maste }
235*3d9fd9fcSEd Maste 
236*3d9fd9fcSEd Maste __attribute__((unused))
237*3d9fd9fcSEd Maste static inline
crypto_int16_nonzero_01(crypto_int16 crypto_int16_x)238*3d9fd9fcSEd Maste crypto_int16 crypto_int16_nonzero_01(crypto_int16 crypto_int16_x) {
239*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
240*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_q,crypto_int16_z;
241*3d9fd9fcSEd Maste   __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");
242*3d9fd9fcSEd Maste   return crypto_int16_z;
243*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
244*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z;
245*3d9fd9fcSEd Maste   __asm__ ("tst %w1,65535\n cset %w0,ne" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc");
246*3d9fd9fcSEd Maste   return crypto_int16_z;
247*3d9fd9fcSEd Maste #else
248*3d9fd9fcSEd Maste   crypto_int16_x |= -crypto_int16_x;
249*3d9fd9fcSEd Maste   return crypto_int16_unsigned_topbit_01(crypto_int16_x);
250*3d9fd9fcSEd Maste #endif
251*3d9fd9fcSEd Maste }
252*3d9fd9fcSEd Maste 
253*3d9fd9fcSEd Maste __attribute__((unused))
254*3d9fd9fcSEd Maste static inline
crypto_int16_positive_mask(crypto_int16 crypto_int16_x)255*3d9fd9fcSEd Maste crypto_int16 crypto_int16_positive_mask(crypto_int16 crypto_int16_x) {
256*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
257*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_q,crypto_int16_z;
258*3d9fd9fcSEd Maste   __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");
259*3d9fd9fcSEd Maste   return crypto_int16_z;
260*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
261*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z;
262*3d9fd9fcSEd Maste   __asm__ ("sxth %w0,%w1\n cmp %w0,0\n csetm %w0,gt" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc");
263*3d9fd9fcSEd Maste   return crypto_int16_z;
264*3d9fd9fcSEd Maste #else
265*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z = -crypto_int16_x;
266*3d9fd9fcSEd Maste   crypto_int16_z ^= crypto_int16_x & crypto_int16_z;
267*3d9fd9fcSEd Maste   return crypto_int16_negative_mask(crypto_int16_z);
268*3d9fd9fcSEd Maste #endif
269*3d9fd9fcSEd Maste }
270*3d9fd9fcSEd Maste 
271*3d9fd9fcSEd Maste __attribute__((unused))
272*3d9fd9fcSEd Maste static inline
crypto_int16_positive_01(crypto_int16 crypto_int16_x)273*3d9fd9fcSEd Maste crypto_int16 crypto_int16_positive_01(crypto_int16 crypto_int16_x) {
274*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
275*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_q,crypto_int16_z;
276*3d9fd9fcSEd Maste   __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");
277*3d9fd9fcSEd Maste   return crypto_int16_z;
278*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
279*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z;
280*3d9fd9fcSEd Maste   __asm__ ("sxth %w0,%w1\n cmp %w0,0\n cset %w0,gt" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc");
281*3d9fd9fcSEd Maste   return crypto_int16_z;
282*3d9fd9fcSEd Maste #else
283*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z = -crypto_int16_x;
284*3d9fd9fcSEd Maste   crypto_int16_z ^= crypto_int16_x & crypto_int16_z;
285*3d9fd9fcSEd Maste   return crypto_int16_unsigned_topbit_01(crypto_int16_z);
286*3d9fd9fcSEd Maste #endif
287*3d9fd9fcSEd Maste }
288*3d9fd9fcSEd Maste 
289*3d9fd9fcSEd Maste __attribute__((unused))
290*3d9fd9fcSEd Maste static inline
crypto_int16_zero_mask(crypto_int16 crypto_int16_x)291*3d9fd9fcSEd Maste crypto_int16 crypto_int16_zero_mask(crypto_int16 crypto_int16_x) {
292*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
293*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_q,crypto_int16_z;
294*3d9fd9fcSEd Maste   __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");
295*3d9fd9fcSEd Maste   return crypto_int16_z;
296*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
297*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z;
298*3d9fd9fcSEd Maste   __asm__ ("tst %w1,65535\n csetm %w0,eq" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc");
299*3d9fd9fcSEd Maste   return crypto_int16_z;
300*3d9fd9fcSEd Maste #else
301*3d9fd9fcSEd Maste   return ~crypto_int16_nonzero_mask(crypto_int16_x);
302*3d9fd9fcSEd Maste #endif
303*3d9fd9fcSEd Maste }
304*3d9fd9fcSEd Maste 
305*3d9fd9fcSEd Maste __attribute__((unused))
306*3d9fd9fcSEd Maste static inline
crypto_int16_zero_01(crypto_int16 crypto_int16_x)307*3d9fd9fcSEd Maste crypto_int16 crypto_int16_zero_01(crypto_int16 crypto_int16_x) {
308*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
309*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_q,crypto_int16_z;
310*3d9fd9fcSEd Maste   __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");
311*3d9fd9fcSEd Maste   return crypto_int16_z;
312*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
313*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z;
314*3d9fd9fcSEd Maste   __asm__ ("tst %w1,65535\n cset %w0,eq" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc");
315*3d9fd9fcSEd Maste   return crypto_int16_z;
316*3d9fd9fcSEd Maste #else
317*3d9fd9fcSEd Maste   return 1-crypto_int16_nonzero_01(crypto_int16_x);
318*3d9fd9fcSEd Maste #endif
319*3d9fd9fcSEd Maste }
320*3d9fd9fcSEd Maste 
321*3d9fd9fcSEd Maste __attribute__((unused))
322*3d9fd9fcSEd Maste static inline
crypto_int16_unequal_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y)323*3d9fd9fcSEd Maste crypto_int16 crypto_int16_unequal_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
324*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
325*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_q,crypto_int16_z;
326*3d9fd9fcSEd Maste   __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");
327*3d9fd9fcSEd Maste   return crypto_int16_z;
328*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
329*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z;
330*3d9fd9fcSEd Maste   __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");
331*3d9fd9fcSEd Maste   return crypto_int16_z;
332*3d9fd9fcSEd Maste #else
333*3d9fd9fcSEd Maste   return crypto_int16_nonzero_mask(crypto_int16_x ^ crypto_int16_y);
334*3d9fd9fcSEd Maste #endif
335*3d9fd9fcSEd Maste }
336*3d9fd9fcSEd Maste 
337*3d9fd9fcSEd Maste __attribute__((unused))
338*3d9fd9fcSEd Maste static inline
crypto_int16_unequal_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y)339*3d9fd9fcSEd Maste crypto_int16 crypto_int16_unequal_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
340*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
341*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_q,crypto_int16_z;
342*3d9fd9fcSEd Maste   __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");
343*3d9fd9fcSEd Maste   return crypto_int16_z;
344*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
345*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z;
346*3d9fd9fcSEd Maste   __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");
347*3d9fd9fcSEd Maste   return crypto_int16_z;
348*3d9fd9fcSEd Maste #else
349*3d9fd9fcSEd Maste   return crypto_int16_nonzero_01(crypto_int16_x ^ crypto_int16_y);
350*3d9fd9fcSEd Maste #endif
351*3d9fd9fcSEd Maste }
352*3d9fd9fcSEd Maste 
353*3d9fd9fcSEd Maste __attribute__((unused))
354*3d9fd9fcSEd Maste static inline
crypto_int16_equal_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y)355*3d9fd9fcSEd Maste crypto_int16 crypto_int16_equal_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
356*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
357*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_q,crypto_int16_z;
358*3d9fd9fcSEd Maste   __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");
359*3d9fd9fcSEd Maste   return crypto_int16_z;
360*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
361*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z;
362*3d9fd9fcSEd Maste   __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");
363*3d9fd9fcSEd Maste   return crypto_int16_z;
364*3d9fd9fcSEd Maste #else
365*3d9fd9fcSEd Maste   return ~crypto_int16_unequal_mask(crypto_int16_x,crypto_int16_y);
366*3d9fd9fcSEd Maste #endif
367*3d9fd9fcSEd Maste }
368*3d9fd9fcSEd Maste 
369*3d9fd9fcSEd Maste __attribute__((unused))
370*3d9fd9fcSEd Maste static inline
crypto_int16_equal_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y)371*3d9fd9fcSEd Maste crypto_int16 crypto_int16_equal_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
372*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
373*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_q,crypto_int16_z;
374*3d9fd9fcSEd Maste   __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");
375*3d9fd9fcSEd Maste   return crypto_int16_z;
376*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
377*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z;
378*3d9fd9fcSEd Maste   __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");
379*3d9fd9fcSEd Maste   return crypto_int16_z;
380*3d9fd9fcSEd Maste #else
381*3d9fd9fcSEd Maste   return 1-crypto_int16_unequal_01(crypto_int16_x,crypto_int16_y);
382*3d9fd9fcSEd Maste #endif
383*3d9fd9fcSEd Maste }
384*3d9fd9fcSEd Maste 
385*3d9fd9fcSEd Maste __attribute__((unused))
386*3d9fd9fcSEd Maste static inline
crypto_int16_min(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y)387*3d9fd9fcSEd Maste crypto_int16 crypto_int16_min(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
388*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
389*3d9fd9fcSEd Maste   __asm__ ("cmpw %1,%0\n cmovgw %1,%0" : "+r"(crypto_int16_x) : "r"(crypto_int16_y) : "cc");
390*3d9fd9fcSEd Maste   return crypto_int16_x;
391*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
392*3d9fd9fcSEd Maste   __asm__ ("sxth %w0,%w0\n cmp %w0,%w1,sxth\n csel %w0,%w0,%w1,lt" : "+&r"(crypto_int16_x) : "r"(crypto_int16_y) : "cc");
393*3d9fd9fcSEd Maste   return crypto_int16_x;
394*3d9fd9fcSEd Maste #else
395*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_r = crypto_int16_y ^ crypto_int16_x;
396*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z = crypto_int16_y - crypto_int16_x;
397*3d9fd9fcSEd Maste   crypto_int16_z ^= crypto_int16_r & (crypto_int16_z ^ crypto_int16_y);
398*3d9fd9fcSEd Maste   crypto_int16_z = crypto_int16_negative_mask(crypto_int16_z);
399*3d9fd9fcSEd Maste   crypto_int16_z &= crypto_int16_r;
400*3d9fd9fcSEd Maste   return crypto_int16_x ^ crypto_int16_z;
401*3d9fd9fcSEd Maste #endif
402*3d9fd9fcSEd Maste }
403*3d9fd9fcSEd Maste 
404*3d9fd9fcSEd Maste __attribute__((unused))
405*3d9fd9fcSEd Maste static inline
crypto_int16_max(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y)406*3d9fd9fcSEd Maste crypto_int16 crypto_int16_max(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
407*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
408*3d9fd9fcSEd Maste   __asm__ ("cmpw %1,%0\n cmovlw %1,%0" : "+r"(crypto_int16_x) : "r"(crypto_int16_y) : "cc");
409*3d9fd9fcSEd Maste   return crypto_int16_x;
410*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
411*3d9fd9fcSEd Maste   __asm__ ("sxth %w0,%w0\n cmp %w0,%w1,sxth\n csel %w0,%w1,%w0,lt" : "+&r"(crypto_int16_x) : "r"(crypto_int16_y) : "cc");
412*3d9fd9fcSEd Maste   return crypto_int16_x;
413*3d9fd9fcSEd Maste #else
414*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_r = crypto_int16_y ^ crypto_int16_x;
415*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z = crypto_int16_y - crypto_int16_x;
416*3d9fd9fcSEd Maste   crypto_int16_z ^= crypto_int16_r & (crypto_int16_z ^ crypto_int16_y);
417*3d9fd9fcSEd Maste   crypto_int16_z = crypto_int16_negative_mask(crypto_int16_z);
418*3d9fd9fcSEd Maste   crypto_int16_z &= crypto_int16_r;
419*3d9fd9fcSEd Maste   return crypto_int16_y ^ crypto_int16_z;
420*3d9fd9fcSEd Maste #endif
421*3d9fd9fcSEd Maste }
422*3d9fd9fcSEd Maste 
423*3d9fd9fcSEd Maste __attribute__((unused))
424*3d9fd9fcSEd Maste static inline
crypto_int16_minmax(crypto_int16 * crypto_int16_p,crypto_int16 * crypto_int16_q)425*3d9fd9fcSEd Maste void crypto_int16_minmax(crypto_int16 *crypto_int16_p,crypto_int16 *crypto_int16_q) {
426*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_x = *crypto_int16_p;
427*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_y = *crypto_int16_q;
428*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
429*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z;
430*3d9fd9fcSEd Maste   __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");
431*3d9fd9fcSEd Maste   *crypto_int16_p = crypto_int16_x;
432*3d9fd9fcSEd Maste   *crypto_int16_q = crypto_int16_y;
433*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
434*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_r, crypto_int16_s;
435*3d9fd9fcSEd Maste   __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");
436*3d9fd9fcSEd Maste   *crypto_int16_p = crypto_int16_r;
437*3d9fd9fcSEd Maste   *crypto_int16_q = crypto_int16_s;
438*3d9fd9fcSEd Maste #else
439*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_r = crypto_int16_y ^ crypto_int16_x;
440*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z = crypto_int16_y - crypto_int16_x;
441*3d9fd9fcSEd Maste   crypto_int16_z ^= crypto_int16_r & (crypto_int16_z ^ crypto_int16_y);
442*3d9fd9fcSEd Maste   crypto_int16_z = crypto_int16_negative_mask(crypto_int16_z);
443*3d9fd9fcSEd Maste   crypto_int16_z &= crypto_int16_r;
444*3d9fd9fcSEd Maste   crypto_int16_x ^= crypto_int16_z;
445*3d9fd9fcSEd Maste   crypto_int16_y ^= crypto_int16_z;
446*3d9fd9fcSEd Maste   *crypto_int16_p = crypto_int16_x;
447*3d9fd9fcSEd Maste   *crypto_int16_q = crypto_int16_y;
448*3d9fd9fcSEd Maste #endif
449*3d9fd9fcSEd Maste }
450*3d9fd9fcSEd Maste 
451*3d9fd9fcSEd Maste __attribute__((unused))
452*3d9fd9fcSEd Maste static inline
crypto_int16_smaller_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y)453*3d9fd9fcSEd Maste crypto_int16 crypto_int16_smaller_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
454*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
455*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_q,crypto_int16_z;
456*3d9fd9fcSEd Maste   __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");
457*3d9fd9fcSEd Maste   return crypto_int16_z;
458*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
459*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z;
460*3d9fd9fcSEd Maste   __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");
461*3d9fd9fcSEd Maste   return crypto_int16_z;
462*3d9fd9fcSEd Maste #else
463*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_r = crypto_int16_x ^ crypto_int16_y;
464*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z = crypto_int16_x - crypto_int16_y;
465*3d9fd9fcSEd Maste   crypto_int16_z ^= crypto_int16_r & (crypto_int16_z ^ crypto_int16_x);
466*3d9fd9fcSEd Maste   return crypto_int16_negative_mask(crypto_int16_z);
467*3d9fd9fcSEd Maste #endif
468*3d9fd9fcSEd Maste }
469*3d9fd9fcSEd Maste 
470*3d9fd9fcSEd Maste __attribute__((unused))
471*3d9fd9fcSEd Maste static inline
crypto_int16_smaller_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y)472*3d9fd9fcSEd Maste crypto_int16 crypto_int16_smaller_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
473*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
474*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_q,crypto_int16_z;
475*3d9fd9fcSEd Maste   __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");
476*3d9fd9fcSEd Maste   return crypto_int16_z;
477*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
478*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z;
479*3d9fd9fcSEd Maste   __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");
480*3d9fd9fcSEd Maste   return crypto_int16_z;
481*3d9fd9fcSEd Maste #else
482*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_r = crypto_int16_x ^ crypto_int16_y;
483*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z = crypto_int16_x - crypto_int16_y;
484*3d9fd9fcSEd Maste   crypto_int16_z ^= crypto_int16_r & (crypto_int16_z ^ crypto_int16_x);
485*3d9fd9fcSEd Maste   return crypto_int16_unsigned_topbit_01(crypto_int16_z);
486*3d9fd9fcSEd Maste #endif
487*3d9fd9fcSEd Maste }
488*3d9fd9fcSEd Maste 
489*3d9fd9fcSEd Maste __attribute__((unused))
490*3d9fd9fcSEd Maste static inline
crypto_int16_leq_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y)491*3d9fd9fcSEd Maste crypto_int16 crypto_int16_leq_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
492*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
493*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_q,crypto_int16_z;
494*3d9fd9fcSEd Maste   __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");
495*3d9fd9fcSEd Maste   return crypto_int16_z;
496*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
497*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z;
498*3d9fd9fcSEd Maste   __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");
499*3d9fd9fcSEd Maste   return crypto_int16_z;
500*3d9fd9fcSEd Maste #else
501*3d9fd9fcSEd Maste   return ~crypto_int16_smaller_mask(crypto_int16_y,crypto_int16_x);
502*3d9fd9fcSEd Maste #endif
503*3d9fd9fcSEd Maste }
504*3d9fd9fcSEd Maste 
505*3d9fd9fcSEd Maste __attribute__((unused))
506*3d9fd9fcSEd Maste static inline
crypto_int16_leq_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y)507*3d9fd9fcSEd Maste crypto_int16 crypto_int16_leq_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) {
508*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
509*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_q,crypto_int16_z;
510*3d9fd9fcSEd Maste   __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");
511*3d9fd9fcSEd Maste   return crypto_int16_z;
512*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
513*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_z;
514*3d9fd9fcSEd Maste   __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");
515*3d9fd9fcSEd Maste   return crypto_int16_z;
516*3d9fd9fcSEd Maste #else
517*3d9fd9fcSEd Maste   return 1-crypto_int16_smaller_01(crypto_int16_y,crypto_int16_x);
518*3d9fd9fcSEd Maste #endif
519*3d9fd9fcSEd Maste }
520*3d9fd9fcSEd Maste 
521*3d9fd9fcSEd Maste __attribute__((unused))
522*3d9fd9fcSEd Maste static inline
crypto_int16_ones_num(crypto_int16 crypto_int16_x)523*3d9fd9fcSEd Maste int crypto_int16_ones_num(crypto_int16 crypto_int16_x) {
524*3d9fd9fcSEd Maste   crypto_int16_unsigned crypto_int16_y = crypto_int16_x;
525*3d9fd9fcSEd Maste   const crypto_int16 C0 = 0x5555;
526*3d9fd9fcSEd Maste   const crypto_int16 C1 = 0x3333;
527*3d9fd9fcSEd Maste   const crypto_int16 C2 = 0x0f0f;
528*3d9fd9fcSEd Maste   crypto_int16_y -= ((crypto_int16_y >> 1) & C0);
529*3d9fd9fcSEd Maste   crypto_int16_y = (crypto_int16_y & C1) + ((crypto_int16_y >> 2) & C1);
530*3d9fd9fcSEd Maste   crypto_int16_y = (crypto_int16_y + (crypto_int16_y >> 4)) & C2;
531*3d9fd9fcSEd Maste   crypto_int16_y = (crypto_int16_y + (crypto_int16_y >> 8)) & 0xff;
532*3d9fd9fcSEd Maste   return crypto_int16_y;
533*3d9fd9fcSEd Maste }
534*3d9fd9fcSEd Maste 
535*3d9fd9fcSEd Maste __attribute__((unused))
536*3d9fd9fcSEd Maste static inline
crypto_int16_bottomzeros_num(crypto_int16 crypto_int16_x)537*3d9fd9fcSEd Maste int crypto_int16_bottomzeros_num(crypto_int16 crypto_int16_x) {
538*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
539*3d9fd9fcSEd Maste   crypto_int16 fallback = 16;
540*3d9fd9fcSEd Maste   __asm__ ("bsfw %0,%0\n cmovew %1,%0" : "+&r"(crypto_int16_x) : "r"(fallback) : "cc");
541*3d9fd9fcSEd Maste   return crypto_int16_x;
542*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
543*3d9fd9fcSEd Maste   int64_t crypto_int16_y;
544*3d9fd9fcSEd Maste   __asm__ ("orr %w0,%w1,-65536\n rbit %w0,%w0\n clz %w0,%w0" : "=r"(crypto_int16_y) : "r"(crypto_int16_x) : );
545*3d9fd9fcSEd Maste   return crypto_int16_y;
546*3d9fd9fcSEd Maste #else
547*3d9fd9fcSEd Maste   crypto_int16 crypto_int16_y = crypto_int16_x ^ (crypto_int16_x-1);
548*3d9fd9fcSEd Maste   crypto_int16_y = ((crypto_int16) crypto_int16_y) >> 1;
549*3d9fd9fcSEd Maste   crypto_int16_y &= ~(crypto_int16_x & (((crypto_int16) 1) << (16-1)));
550*3d9fd9fcSEd Maste   return crypto_int16_ones_num(crypto_int16_y);
551*3d9fd9fcSEd Maste #endif
552*3d9fd9fcSEd Maste }
553*3d9fd9fcSEd Maste 
554*3d9fd9fcSEd Maste #endif
555*3d9fd9fcSEd Maste 
556*3d9fd9fcSEd Maste /* from supercop-20240808/cryptoint/crypto_int32.h */
557*3d9fd9fcSEd Maste /* auto-generated: cd cryptoint; ./autogen */
558*3d9fd9fcSEd Maste /* cryptoint 20240806 */
559*3d9fd9fcSEd Maste 
560*3d9fd9fcSEd Maste #ifndef crypto_int32_h
561*3d9fd9fcSEd Maste #define crypto_int32_h
562*3d9fd9fcSEd Maste 
563*3d9fd9fcSEd Maste #define crypto_int32 int32_t
564*3d9fd9fcSEd Maste #define crypto_int32_unsigned uint32_t
565*3d9fd9fcSEd Maste 
566*3d9fd9fcSEd Maste 
567*3d9fd9fcSEd Maste 
568*3d9fd9fcSEd Maste __attribute__((unused))
569*3d9fd9fcSEd Maste static inline
crypto_int32_load(const unsigned char * crypto_int32_s)570*3d9fd9fcSEd Maste crypto_int32 crypto_int32_load(const unsigned char *crypto_int32_s) {
571*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z = 0;
572*3d9fd9fcSEd Maste   crypto_int32_z |= ((crypto_int32) (*crypto_int32_s++)) << 0;
573*3d9fd9fcSEd Maste   crypto_int32_z |= ((crypto_int32) (*crypto_int32_s++)) << 8;
574*3d9fd9fcSEd Maste   crypto_int32_z |= ((crypto_int32) (*crypto_int32_s++)) << 16;
575*3d9fd9fcSEd Maste   crypto_int32_z |= ((crypto_int32) (*crypto_int32_s++)) << 24;
576*3d9fd9fcSEd Maste   return crypto_int32_z;
577*3d9fd9fcSEd Maste }
578*3d9fd9fcSEd Maste 
579*3d9fd9fcSEd Maste __attribute__((unused))
580*3d9fd9fcSEd Maste static inline
crypto_int32_store(unsigned char * crypto_int32_s,crypto_int32 crypto_int32_x)581*3d9fd9fcSEd Maste void crypto_int32_store(unsigned char *crypto_int32_s,crypto_int32 crypto_int32_x) {
582*3d9fd9fcSEd Maste   *crypto_int32_s++ = crypto_int32_x >> 0;
583*3d9fd9fcSEd Maste   *crypto_int32_s++ = crypto_int32_x >> 8;
584*3d9fd9fcSEd Maste   *crypto_int32_s++ = crypto_int32_x >> 16;
585*3d9fd9fcSEd Maste   *crypto_int32_s++ = crypto_int32_x >> 24;
586*3d9fd9fcSEd Maste }
587*3d9fd9fcSEd Maste 
588*3d9fd9fcSEd Maste __attribute__((unused))
589*3d9fd9fcSEd Maste static inline
crypto_int32_negative_mask(crypto_int32 crypto_int32_x)590*3d9fd9fcSEd Maste crypto_int32 crypto_int32_negative_mask(crypto_int32 crypto_int32_x) {
591*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
592*3d9fd9fcSEd Maste   __asm__ ("sarl $31,%0" : "+r"(crypto_int32_x) : : "cc");
593*3d9fd9fcSEd Maste   return crypto_int32_x;
594*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
595*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_y;
596*3d9fd9fcSEd Maste   __asm__ ("asr %w0,%w1,31" : "=r"(crypto_int32_y) : "r"(crypto_int32_x) : );
597*3d9fd9fcSEd Maste   return crypto_int32_y;
598*3d9fd9fcSEd Maste #else
599*3d9fd9fcSEd Maste   crypto_int32_x >>= 32-6;
600*3d9fd9fcSEd Maste   crypto_int32_x ^= crypto_int32_optblocker;
601*3d9fd9fcSEd Maste   crypto_int32_x >>= 5;
602*3d9fd9fcSEd Maste   return crypto_int32_x;
603*3d9fd9fcSEd Maste #endif
604*3d9fd9fcSEd Maste }
605*3d9fd9fcSEd Maste 
606*3d9fd9fcSEd Maste __attribute__((unused))
607*3d9fd9fcSEd Maste static inline
crypto_int32_unsigned_topbit_01(crypto_int32_unsigned crypto_int32_x)608*3d9fd9fcSEd Maste crypto_int32_unsigned crypto_int32_unsigned_topbit_01(crypto_int32_unsigned crypto_int32_x) {
609*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
610*3d9fd9fcSEd Maste   __asm__ ("shrl $31,%0" : "+r"(crypto_int32_x) : : "cc");
611*3d9fd9fcSEd Maste   return crypto_int32_x;
612*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
613*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_y;
614*3d9fd9fcSEd Maste   __asm__ ("lsr %w0,%w1,31" : "=r"(crypto_int32_y) : "r"(crypto_int32_x) : );
615*3d9fd9fcSEd Maste   return crypto_int32_y;
616*3d9fd9fcSEd Maste #else
617*3d9fd9fcSEd Maste   crypto_int32_x >>= 32-6;
618*3d9fd9fcSEd Maste   crypto_int32_x ^= crypto_int32_optblocker;
619*3d9fd9fcSEd Maste   crypto_int32_x >>= 5;
620*3d9fd9fcSEd Maste   return crypto_int32_x;
621*3d9fd9fcSEd Maste #endif
622*3d9fd9fcSEd Maste }
623*3d9fd9fcSEd Maste 
624*3d9fd9fcSEd Maste __attribute__((unused))
625*3d9fd9fcSEd Maste static inline
crypto_int32_negative_01(crypto_int32 crypto_int32_x)626*3d9fd9fcSEd Maste crypto_int32 crypto_int32_negative_01(crypto_int32 crypto_int32_x) {
627*3d9fd9fcSEd Maste   return crypto_int32_unsigned_topbit_01(crypto_int32_x);
628*3d9fd9fcSEd Maste }
629*3d9fd9fcSEd Maste 
630*3d9fd9fcSEd Maste __attribute__((unused))
631*3d9fd9fcSEd Maste static inline
crypto_int32_topbit_mask(crypto_int32 crypto_int32_x)632*3d9fd9fcSEd Maste crypto_int32 crypto_int32_topbit_mask(crypto_int32 crypto_int32_x) {
633*3d9fd9fcSEd Maste   return crypto_int32_negative_mask(crypto_int32_x);
634*3d9fd9fcSEd Maste }
635*3d9fd9fcSEd Maste 
636*3d9fd9fcSEd Maste __attribute__((unused))
637*3d9fd9fcSEd Maste static inline
crypto_int32_topbit_01(crypto_int32 crypto_int32_x)638*3d9fd9fcSEd Maste crypto_int32 crypto_int32_topbit_01(crypto_int32 crypto_int32_x) {
639*3d9fd9fcSEd Maste   return crypto_int32_unsigned_topbit_01(crypto_int32_x);
640*3d9fd9fcSEd Maste }
641*3d9fd9fcSEd Maste 
642*3d9fd9fcSEd Maste __attribute__((unused))
643*3d9fd9fcSEd Maste static inline
crypto_int32_bottombit_mask(crypto_int32 crypto_int32_x)644*3d9fd9fcSEd Maste crypto_int32 crypto_int32_bottombit_mask(crypto_int32 crypto_int32_x) {
645*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
646*3d9fd9fcSEd Maste   __asm__ ("andl $1,%0" : "+r"(crypto_int32_x) : : "cc");
647*3d9fd9fcSEd Maste   return -crypto_int32_x;
648*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
649*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_y;
650*3d9fd9fcSEd Maste   __asm__ ("sbfx %w0,%w1,0,1" : "=r"(crypto_int32_y) : "r"(crypto_int32_x) : );
651*3d9fd9fcSEd Maste   return crypto_int32_y;
652*3d9fd9fcSEd Maste #else
653*3d9fd9fcSEd Maste   crypto_int32_x &= 1 ^ crypto_int32_optblocker;
654*3d9fd9fcSEd Maste   return -crypto_int32_x;
655*3d9fd9fcSEd Maste #endif
656*3d9fd9fcSEd Maste }
657*3d9fd9fcSEd Maste 
658*3d9fd9fcSEd Maste __attribute__((unused))
659*3d9fd9fcSEd Maste static inline
crypto_int32_bottombit_01(crypto_int32 crypto_int32_x)660*3d9fd9fcSEd Maste crypto_int32 crypto_int32_bottombit_01(crypto_int32 crypto_int32_x) {
661*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
662*3d9fd9fcSEd Maste   __asm__ ("andl $1,%0" : "+r"(crypto_int32_x) : : "cc");
663*3d9fd9fcSEd Maste   return crypto_int32_x;
664*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
665*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_y;
666*3d9fd9fcSEd Maste   __asm__ ("ubfx %w0,%w1,0,1" : "=r"(crypto_int32_y) : "r"(crypto_int32_x) : );
667*3d9fd9fcSEd Maste   return crypto_int32_y;
668*3d9fd9fcSEd Maste #else
669*3d9fd9fcSEd Maste   crypto_int32_x &= 1 ^ crypto_int32_optblocker;
670*3d9fd9fcSEd Maste   return crypto_int32_x;
671*3d9fd9fcSEd Maste #endif
672*3d9fd9fcSEd Maste }
673*3d9fd9fcSEd Maste 
674*3d9fd9fcSEd Maste __attribute__((unused))
675*3d9fd9fcSEd Maste static inline
crypto_int32_bitinrangepublicpos_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s)676*3d9fd9fcSEd Maste crypto_int32 crypto_int32_bitinrangepublicpos_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) {
677*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
678*3d9fd9fcSEd Maste   __asm__ ("sarl %%cl,%0" : "+r"(crypto_int32_x) : "c"(crypto_int32_s) : "cc");
679*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
680*3d9fd9fcSEd Maste   __asm__ ("asr %w0,%w0,%w1" : "+r"(crypto_int32_x) : "r"(crypto_int32_s) : );
681*3d9fd9fcSEd Maste #else
682*3d9fd9fcSEd Maste   crypto_int32_x >>= crypto_int32_s ^ crypto_int32_optblocker;
683*3d9fd9fcSEd Maste #endif
684*3d9fd9fcSEd Maste   return crypto_int32_bottombit_mask(crypto_int32_x);
685*3d9fd9fcSEd Maste }
686*3d9fd9fcSEd Maste 
687*3d9fd9fcSEd Maste __attribute__((unused))
688*3d9fd9fcSEd Maste static inline
crypto_int32_bitinrangepublicpos_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s)689*3d9fd9fcSEd Maste crypto_int32 crypto_int32_bitinrangepublicpos_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) {
690*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
691*3d9fd9fcSEd Maste   __asm__ ("sarl %%cl,%0" : "+r"(crypto_int32_x) : "c"(crypto_int32_s) : "cc");
692*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
693*3d9fd9fcSEd Maste   __asm__ ("asr %w0,%w0,%w1" : "+r"(crypto_int32_x) : "r"(crypto_int32_s) : );
694*3d9fd9fcSEd Maste #else
695*3d9fd9fcSEd Maste   crypto_int32_x >>= crypto_int32_s ^ crypto_int32_optblocker;
696*3d9fd9fcSEd Maste #endif
697*3d9fd9fcSEd Maste   return crypto_int32_bottombit_01(crypto_int32_x);
698*3d9fd9fcSEd Maste }
699*3d9fd9fcSEd Maste 
700*3d9fd9fcSEd Maste __attribute__((unused))
701*3d9fd9fcSEd Maste static inline
crypto_int32_shlmod(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s)702*3d9fd9fcSEd Maste crypto_int32 crypto_int32_shlmod(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) {
703*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
704*3d9fd9fcSEd Maste   __asm__ ("shll %%cl,%0" : "+r"(crypto_int32_x) : "c"(crypto_int32_s) : "cc");
705*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
706*3d9fd9fcSEd Maste   __asm__ ("lsl %w0,%w0,%w1" : "+r"(crypto_int32_x) : "r"(crypto_int32_s) : );
707*3d9fd9fcSEd Maste #else
708*3d9fd9fcSEd Maste   int crypto_int32_k, crypto_int32_l;
709*3d9fd9fcSEd Maste   for (crypto_int32_l = 0,crypto_int32_k = 1;crypto_int32_k < 32;++crypto_int32_l,crypto_int32_k *= 2)
710*3d9fd9fcSEd Maste     crypto_int32_x ^= (crypto_int32_x ^ (crypto_int32_x << crypto_int32_k)) & crypto_int32_bitinrangepublicpos_mask(crypto_int32_s,crypto_int32_l);
711*3d9fd9fcSEd Maste #endif
712*3d9fd9fcSEd Maste   return crypto_int32_x;
713*3d9fd9fcSEd Maste }
714*3d9fd9fcSEd Maste 
715*3d9fd9fcSEd Maste __attribute__((unused))
716*3d9fd9fcSEd Maste static inline
crypto_int32_shrmod(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s)717*3d9fd9fcSEd Maste crypto_int32 crypto_int32_shrmod(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) {
718*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
719*3d9fd9fcSEd Maste   __asm__ ("sarl %%cl,%0" : "+r"(crypto_int32_x) : "c"(crypto_int32_s) : "cc");
720*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
721*3d9fd9fcSEd Maste   __asm__ ("asr %w0,%w0,%w1" : "+r"(crypto_int32_x) : "r"(crypto_int32_s) : );
722*3d9fd9fcSEd Maste #else
723*3d9fd9fcSEd Maste   int crypto_int32_k, crypto_int32_l;
724*3d9fd9fcSEd Maste   for (crypto_int32_l = 0,crypto_int32_k = 1;crypto_int32_k < 32;++crypto_int32_l,crypto_int32_k *= 2)
725*3d9fd9fcSEd Maste     crypto_int32_x ^= (crypto_int32_x ^ (crypto_int32_x >> crypto_int32_k)) & crypto_int32_bitinrangepublicpos_mask(crypto_int32_s,crypto_int32_l);
726*3d9fd9fcSEd Maste #endif
727*3d9fd9fcSEd Maste   return crypto_int32_x;
728*3d9fd9fcSEd Maste }
729*3d9fd9fcSEd Maste 
730*3d9fd9fcSEd Maste __attribute__((unused))
731*3d9fd9fcSEd Maste static inline
crypto_int32_bitmod_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s)732*3d9fd9fcSEd Maste crypto_int32 crypto_int32_bitmod_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) {
733*3d9fd9fcSEd Maste   crypto_int32_x = crypto_int32_shrmod(crypto_int32_x,crypto_int32_s);
734*3d9fd9fcSEd Maste   return crypto_int32_bottombit_mask(crypto_int32_x);
735*3d9fd9fcSEd Maste }
736*3d9fd9fcSEd Maste 
737*3d9fd9fcSEd Maste __attribute__((unused))
738*3d9fd9fcSEd Maste static inline
crypto_int32_bitmod_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s)739*3d9fd9fcSEd Maste crypto_int32 crypto_int32_bitmod_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) {
740*3d9fd9fcSEd Maste   crypto_int32_x = crypto_int32_shrmod(crypto_int32_x,crypto_int32_s);
741*3d9fd9fcSEd Maste   return crypto_int32_bottombit_01(crypto_int32_x);
742*3d9fd9fcSEd Maste }
743*3d9fd9fcSEd Maste 
744*3d9fd9fcSEd Maste __attribute__((unused))
745*3d9fd9fcSEd Maste static inline
crypto_int32_nonzero_mask(crypto_int32 crypto_int32_x)746*3d9fd9fcSEd Maste crypto_int32 crypto_int32_nonzero_mask(crypto_int32 crypto_int32_x) {
747*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
748*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_q,crypto_int32_z;
749*3d9fd9fcSEd Maste   __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");
750*3d9fd9fcSEd Maste   return crypto_int32_z;
751*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
752*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z;
753*3d9fd9fcSEd Maste   __asm__ ("cmp %w1,0\n csetm %w0,ne" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc");
754*3d9fd9fcSEd Maste   return crypto_int32_z;
755*3d9fd9fcSEd Maste #else
756*3d9fd9fcSEd Maste   crypto_int32_x |= -crypto_int32_x;
757*3d9fd9fcSEd Maste   return crypto_int32_negative_mask(crypto_int32_x);
758*3d9fd9fcSEd Maste #endif
759*3d9fd9fcSEd Maste }
760*3d9fd9fcSEd Maste 
761*3d9fd9fcSEd Maste __attribute__((unused))
762*3d9fd9fcSEd Maste static inline
crypto_int32_nonzero_01(crypto_int32 crypto_int32_x)763*3d9fd9fcSEd Maste crypto_int32 crypto_int32_nonzero_01(crypto_int32 crypto_int32_x) {
764*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
765*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_q,crypto_int32_z;
766*3d9fd9fcSEd Maste   __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");
767*3d9fd9fcSEd Maste   return crypto_int32_z;
768*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
769*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z;
770*3d9fd9fcSEd Maste   __asm__ ("cmp %w1,0\n cset %w0,ne" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc");
771*3d9fd9fcSEd Maste   return crypto_int32_z;
772*3d9fd9fcSEd Maste #else
773*3d9fd9fcSEd Maste   crypto_int32_x |= -crypto_int32_x;
774*3d9fd9fcSEd Maste   return crypto_int32_unsigned_topbit_01(crypto_int32_x);
775*3d9fd9fcSEd Maste #endif
776*3d9fd9fcSEd Maste }
777*3d9fd9fcSEd Maste 
778*3d9fd9fcSEd Maste __attribute__((unused))
779*3d9fd9fcSEd Maste static inline
crypto_int32_positive_mask(crypto_int32 crypto_int32_x)780*3d9fd9fcSEd Maste crypto_int32 crypto_int32_positive_mask(crypto_int32 crypto_int32_x) {
781*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
782*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_q,crypto_int32_z;
783*3d9fd9fcSEd Maste   __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");
784*3d9fd9fcSEd Maste   return crypto_int32_z;
785*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
786*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z;
787*3d9fd9fcSEd Maste   __asm__ ("cmp %w1,0\n csetm %w0,gt" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc");
788*3d9fd9fcSEd Maste   return crypto_int32_z;
789*3d9fd9fcSEd Maste #else
790*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z = -crypto_int32_x;
791*3d9fd9fcSEd Maste   crypto_int32_z ^= crypto_int32_x & crypto_int32_z;
792*3d9fd9fcSEd Maste   return crypto_int32_negative_mask(crypto_int32_z);
793*3d9fd9fcSEd Maste #endif
794*3d9fd9fcSEd Maste }
795*3d9fd9fcSEd Maste 
796*3d9fd9fcSEd Maste __attribute__((unused))
797*3d9fd9fcSEd Maste static inline
crypto_int32_positive_01(crypto_int32 crypto_int32_x)798*3d9fd9fcSEd Maste crypto_int32 crypto_int32_positive_01(crypto_int32 crypto_int32_x) {
799*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
800*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_q,crypto_int32_z;
801*3d9fd9fcSEd Maste   __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");
802*3d9fd9fcSEd Maste   return crypto_int32_z;
803*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
804*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z;
805*3d9fd9fcSEd Maste   __asm__ ("cmp %w1,0\n cset %w0,gt" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc");
806*3d9fd9fcSEd Maste   return crypto_int32_z;
807*3d9fd9fcSEd Maste #else
808*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z = -crypto_int32_x;
809*3d9fd9fcSEd Maste   crypto_int32_z ^= crypto_int32_x & crypto_int32_z;
810*3d9fd9fcSEd Maste   return crypto_int32_unsigned_topbit_01(crypto_int32_z);
811*3d9fd9fcSEd Maste #endif
812*3d9fd9fcSEd Maste }
813*3d9fd9fcSEd Maste 
814*3d9fd9fcSEd Maste __attribute__((unused))
815*3d9fd9fcSEd Maste static inline
crypto_int32_zero_mask(crypto_int32 crypto_int32_x)816*3d9fd9fcSEd Maste crypto_int32 crypto_int32_zero_mask(crypto_int32 crypto_int32_x) {
817*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
818*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_q,crypto_int32_z;
819*3d9fd9fcSEd Maste   __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");
820*3d9fd9fcSEd Maste   return crypto_int32_z;
821*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
822*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z;
823*3d9fd9fcSEd Maste   __asm__ ("cmp %w1,0\n csetm %w0,eq" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc");
824*3d9fd9fcSEd Maste   return crypto_int32_z;
825*3d9fd9fcSEd Maste #else
826*3d9fd9fcSEd Maste   return ~crypto_int32_nonzero_mask(crypto_int32_x);
827*3d9fd9fcSEd Maste #endif
828*3d9fd9fcSEd Maste }
829*3d9fd9fcSEd Maste 
830*3d9fd9fcSEd Maste __attribute__((unused))
831*3d9fd9fcSEd Maste static inline
crypto_int32_zero_01(crypto_int32 crypto_int32_x)832*3d9fd9fcSEd Maste crypto_int32 crypto_int32_zero_01(crypto_int32 crypto_int32_x) {
833*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
834*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_q,crypto_int32_z;
835*3d9fd9fcSEd Maste   __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");
836*3d9fd9fcSEd Maste   return crypto_int32_z;
837*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
838*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z;
839*3d9fd9fcSEd Maste   __asm__ ("cmp %w1,0\n cset %w0,eq" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc");
840*3d9fd9fcSEd Maste   return crypto_int32_z;
841*3d9fd9fcSEd Maste #else
842*3d9fd9fcSEd Maste   return 1-crypto_int32_nonzero_01(crypto_int32_x);
843*3d9fd9fcSEd Maste #endif
844*3d9fd9fcSEd Maste }
845*3d9fd9fcSEd Maste 
846*3d9fd9fcSEd Maste __attribute__((unused))
847*3d9fd9fcSEd Maste static inline
crypto_int32_unequal_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y)848*3d9fd9fcSEd Maste crypto_int32 crypto_int32_unequal_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
849*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
850*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_q,crypto_int32_z;
851*3d9fd9fcSEd Maste   __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");
852*3d9fd9fcSEd Maste   return crypto_int32_z;
853*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
854*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z;
855*3d9fd9fcSEd Maste   __asm__ ("cmp %w1,%w2\n csetm %w0,ne" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
856*3d9fd9fcSEd Maste   return crypto_int32_z;
857*3d9fd9fcSEd Maste #else
858*3d9fd9fcSEd Maste   return crypto_int32_nonzero_mask(crypto_int32_x ^ crypto_int32_y);
859*3d9fd9fcSEd Maste #endif
860*3d9fd9fcSEd Maste }
861*3d9fd9fcSEd Maste 
862*3d9fd9fcSEd Maste __attribute__((unused))
863*3d9fd9fcSEd Maste static inline
crypto_int32_unequal_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y)864*3d9fd9fcSEd Maste crypto_int32 crypto_int32_unequal_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
865*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
866*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_q,crypto_int32_z;
867*3d9fd9fcSEd Maste   __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");
868*3d9fd9fcSEd Maste   return crypto_int32_z;
869*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
870*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z;
871*3d9fd9fcSEd Maste   __asm__ ("cmp %w1,%w2\n cset %w0,ne" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
872*3d9fd9fcSEd Maste   return crypto_int32_z;
873*3d9fd9fcSEd Maste #else
874*3d9fd9fcSEd Maste   return crypto_int32_nonzero_01(crypto_int32_x ^ crypto_int32_y);
875*3d9fd9fcSEd Maste #endif
876*3d9fd9fcSEd Maste }
877*3d9fd9fcSEd Maste 
878*3d9fd9fcSEd Maste __attribute__((unused))
879*3d9fd9fcSEd Maste static inline
crypto_int32_equal_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y)880*3d9fd9fcSEd Maste crypto_int32 crypto_int32_equal_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
881*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
882*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_q,crypto_int32_z;
883*3d9fd9fcSEd Maste   __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");
884*3d9fd9fcSEd Maste   return crypto_int32_z;
885*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
886*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z;
887*3d9fd9fcSEd Maste   __asm__ ("cmp %w1,%w2\n csetm %w0,eq" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
888*3d9fd9fcSEd Maste   return crypto_int32_z;
889*3d9fd9fcSEd Maste #else
890*3d9fd9fcSEd Maste   return ~crypto_int32_unequal_mask(crypto_int32_x,crypto_int32_y);
891*3d9fd9fcSEd Maste #endif
892*3d9fd9fcSEd Maste }
893*3d9fd9fcSEd Maste 
894*3d9fd9fcSEd Maste __attribute__((unused))
895*3d9fd9fcSEd Maste static inline
crypto_int32_equal_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y)896*3d9fd9fcSEd Maste crypto_int32 crypto_int32_equal_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
897*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
898*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_q,crypto_int32_z;
899*3d9fd9fcSEd Maste   __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");
900*3d9fd9fcSEd Maste   return crypto_int32_z;
901*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
902*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z;
903*3d9fd9fcSEd Maste   __asm__ ("cmp %w1,%w2\n cset %w0,eq" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
904*3d9fd9fcSEd Maste   return crypto_int32_z;
905*3d9fd9fcSEd Maste #else
906*3d9fd9fcSEd Maste   return 1-crypto_int32_unequal_01(crypto_int32_x,crypto_int32_y);
907*3d9fd9fcSEd Maste #endif
908*3d9fd9fcSEd Maste }
909*3d9fd9fcSEd Maste 
910*3d9fd9fcSEd Maste __attribute__((unused))
911*3d9fd9fcSEd Maste static inline
crypto_int32_min(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y)912*3d9fd9fcSEd Maste crypto_int32 crypto_int32_min(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
913*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
914*3d9fd9fcSEd Maste   __asm__ ("cmpl %1,%0\n cmovgl %1,%0" : "+r"(crypto_int32_x) : "r"(crypto_int32_y) : "cc");
915*3d9fd9fcSEd Maste   return crypto_int32_x;
916*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
917*3d9fd9fcSEd Maste   __asm__ ("cmp %w0,%w1\n csel %w0,%w0,%w1,lt" : "+r"(crypto_int32_x) : "r"(crypto_int32_y) : "cc");
918*3d9fd9fcSEd Maste   return crypto_int32_x;
919*3d9fd9fcSEd Maste #else
920*3d9fd9fcSEd Maste   crypto_int64 crypto_int32_r = (crypto_int64)crypto_int32_y ^ (crypto_int64)crypto_int32_x;
921*3d9fd9fcSEd Maste   crypto_int64 crypto_int32_z = (crypto_int64)crypto_int32_y - (crypto_int64)crypto_int32_x;
922*3d9fd9fcSEd Maste   crypto_int32_z ^= crypto_int32_r & (crypto_int32_z ^ crypto_int32_y);
923*3d9fd9fcSEd Maste   crypto_int32_z = crypto_int32_negative_mask(crypto_int32_z);
924*3d9fd9fcSEd Maste   crypto_int32_z &= crypto_int32_r;
925*3d9fd9fcSEd Maste   return crypto_int32_x ^ crypto_int32_z;
926*3d9fd9fcSEd Maste #endif
927*3d9fd9fcSEd Maste }
928*3d9fd9fcSEd Maste 
929*3d9fd9fcSEd Maste __attribute__((unused))
930*3d9fd9fcSEd Maste static inline
crypto_int32_max(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y)931*3d9fd9fcSEd Maste crypto_int32 crypto_int32_max(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
932*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
933*3d9fd9fcSEd Maste   __asm__ ("cmpl %1,%0\n cmovll %1,%0" : "+r"(crypto_int32_x) : "r"(crypto_int32_y) : "cc");
934*3d9fd9fcSEd Maste   return crypto_int32_x;
935*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
936*3d9fd9fcSEd Maste   __asm__ ("cmp %w0,%w1\n csel %w0,%w1,%w0,lt" : "+r"(crypto_int32_x) : "r"(crypto_int32_y) : "cc");
937*3d9fd9fcSEd Maste   return crypto_int32_x;
938*3d9fd9fcSEd Maste #else
939*3d9fd9fcSEd Maste   crypto_int64 crypto_int32_r = (crypto_int64)crypto_int32_y ^ (crypto_int64)crypto_int32_x;
940*3d9fd9fcSEd Maste   crypto_int64 crypto_int32_z = (crypto_int64)crypto_int32_y - (crypto_int64)crypto_int32_x;
941*3d9fd9fcSEd Maste   crypto_int32_z ^= crypto_int32_r & (crypto_int32_z ^ crypto_int32_y);
942*3d9fd9fcSEd Maste   crypto_int32_z = crypto_int32_negative_mask(crypto_int32_z);
943*3d9fd9fcSEd Maste   crypto_int32_z &= crypto_int32_r;
944*3d9fd9fcSEd Maste   return crypto_int32_y ^ crypto_int32_z;
945*3d9fd9fcSEd Maste #endif
946*3d9fd9fcSEd Maste }
947*3d9fd9fcSEd Maste 
948*3d9fd9fcSEd Maste __attribute__((unused))
949*3d9fd9fcSEd Maste static inline
crypto_int32_minmax(crypto_int32 * crypto_int32_p,crypto_int32 * crypto_int32_q)950*3d9fd9fcSEd Maste void crypto_int32_minmax(crypto_int32 *crypto_int32_p,crypto_int32 *crypto_int32_q) {
951*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_x = *crypto_int32_p;
952*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_y = *crypto_int32_q;
953*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
954*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z;
955*3d9fd9fcSEd Maste   __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");
956*3d9fd9fcSEd Maste   *crypto_int32_p = crypto_int32_x;
957*3d9fd9fcSEd Maste   *crypto_int32_q = crypto_int32_y;
958*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
959*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_r, crypto_int32_s;
960*3d9fd9fcSEd Maste   __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");
961*3d9fd9fcSEd Maste   *crypto_int32_p = crypto_int32_r;
962*3d9fd9fcSEd Maste   *crypto_int32_q = crypto_int32_s;
963*3d9fd9fcSEd Maste #else
964*3d9fd9fcSEd Maste   crypto_int64 crypto_int32_r = (crypto_int64)crypto_int32_y ^ (crypto_int64)crypto_int32_x;
965*3d9fd9fcSEd Maste   crypto_int64 crypto_int32_z = (crypto_int64)crypto_int32_y - (crypto_int64)crypto_int32_x;
966*3d9fd9fcSEd Maste   crypto_int32_z ^= crypto_int32_r & (crypto_int32_z ^ crypto_int32_y);
967*3d9fd9fcSEd Maste   crypto_int32_z = crypto_int32_negative_mask(crypto_int32_z);
968*3d9fd9fcSEd Maste   crypto_int32_z &= crypto_int32_r;
969*3d9fd9fcSEd Maste   crypto_int32_x ^= crypto_int32_z;
970*3d9fd9fcSEd Maste   crypto_int32_y ^= crypto_int32_z;
971*3d9fd9fcSEd Maste   *crypto_int32_p = crypto_int32_x;
972*3d9fd9fcSEd Maste   *crypto_int32_q = crypto_int32_y;
973*3d9fd9fcSEd Maste #endif
974*3d9fd9fcSEd Maste }
975*3d9fd9fcSEd Maste 
976*3d9fd9fcSEd Maste __attribute__((unused))
977*3d9fd9fcSEd Maste static inline
crypto_int32_smaller_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y)978*3d9fd9fcSEd Maste crypto_int32 crypto_int32_smaller_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
979*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
980*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_q,crypto_int32_z;
981*3d9fd9fcSEd Maste   __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");
982*3d9fd9fcSEd Maste   return crypto_int32_z;
983*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
984*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z;
985*3d9fd9fcSEd Maste   __asm__ ("cmp %w1,%w2\n csetm %w0,lt" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
986*3d9fd9fcSEd Maste   return crypto_int32_z;
987*3d9fd9fcSEd Maste #else
988*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_r = crypto_int32_x ^ crypto_int32_y;
989*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z = crypto_int32_x - crypto_int32_y;
990*3d9fd9fcSEd Maste   crypto_int32_z ^= crypto_int32_r & (crypto_int32_z ^ crypto_int32_x);
991*3d9fd9fcSEd Maste   return crypto_int32_negative_mask(crypto_int32_z);
992*3d9fd9fcSEd Maste #endif
993*3d9fd9fcSEd Maste }
994*3d9fd9fcSEd Maste 
995*3d9fd9fcSEd Maste __attribute__((unused))
996*3d9fd9fcSEd Maste static inline
crypto_int32_smaller_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y)997*3d9fd9fcSEd Maste crypto_int32 crypto_int32_smaller_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
998*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
999*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_q,crypto_int32_z;
1000*3d9fd9fcSEd Maste   __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");
1001*3d9fd9fcSEd Maste   return crypto_int32_z;
1002*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1003*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z;
1004*3d9fd9fcSEd Maste   __asm__ ("cmp %w1,%w2\n cset %w0,lt" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
1005*3d9fd9fcSEd Maste   return crypto_int32_z;
1006*3d9fd9fcSEd Maste #else
1007*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_r = crypto_int32_x ^ crypto_int32_y;
1008*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z = crypto_int32_x - crypto_int32_y;
1009*3d9fd9fcSEd Maste   crypto_int32_z ^= crypto_int32_r & (crypto_int32_z ^ crypto_int32_x);
1010*3d9fd9fcSEd Maste   return crypto_int32_unsigned_topbit_01(crypto_int32_z);
1011*3d9fd9fcSEd Maste #endif
1012*3d9fd9fcSEd Maste }
1013*3d9fd9fcSEd Maste 
1014*3d9fd9fcSEd Maste __attribute__((unused))
1015*3d9fd9fcSEd Maste static inline
crypto_int32_leq_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y)1016*3d9fd9fcSEd Maste crypto_int32 crypto_int32_leq_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
1017*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1018*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_q,crypto_int32_z;
1019*3d9fd9fcSEd Maste   __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");
1020*3d9fd9fcSEd Maste   return crypto_int32_z;
1021*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1022*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z;
1023*3d9fd9fcSEd Maste   __asm__ ("cmp %w1,%w2\n csetm %w0,le" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
1024*3d9fd9fcSEd Maste   return crypto_int32_z;
1025*3d9fd9fcSEd Maste #else
1026*3d9fd9fcSEd Maste   return ~crypto_int32_smaller_mask(crypto_int32_y,crypto_int32_x);
1027*3d9fd9fcSEd Maste #endif
1028*3d9fd9fcSEd Maste }
1029*3d9fd9fcSEd Maste 
1030*3d9fd9fcSEd Maste __attribute__((unused))
1031*3d9fd9fcSEd Maste static inline
crypto_int32_leq_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y)1032*3d9fd9fcSEd Maste crypto_int32 crypto_int32_leq_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) {
1033*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1034*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_q,crypto_int32_z;
1035*3d9fd9fcSEd Maste   __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");
1036*3d9fd9fcSEd Maste   return crypto_int32_z;
1037*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1038*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_z;
1039*3d9fd9fcSEd Maste   __asm__ ("cmp %w1,%w2\n cset %w0,le" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc");
1040*3d9fd9fcSEd Maste   return crypto_int32_z;
1041*3d9fd9fcSEd Maste #else
1042*3d9fd9fcSEd Maste   return 1-crypto_int32_smaller_01(crypto_int32_y,crypto_int32_x);
1043*3d9fd9fcSEd Maste #endif
1044*3d9fd9fcSEd Maste }
1045*3d9fd9fcSEd Maste 
1046*3d9fd9fcSEd Maste __attribute__((unused))
1047*3d9fd9fcSEd Maste static inline
crypto_int32_ones_num(crypto_int32 crypto_int32_x)1048*3d9fd9fcSEd Maste int crypto_int32_ones_num(crypto_int32 crypto_int32_x) {
1049*3d9fd9fcSEd Maste   crypto_int32_unsigned crypto_int32_y = crypto_int32_x;
1050*3d9fd9fcSEd Maste   const crypto_int32 C0 = 0x55555555;
1051*3d9fd9fcSEd Maste   const crypto_int32 C1 = 0x33333333;
1052*3d9fd9fcSEd Maste   const crypto_int32 C2 = 0x0f0f0f0f;
1053*3d9fd9fcSEd Maste   crypto_int32_y -= ((crypto_int32_y >> 1) & C0);
1054*3d9fd9fcSEd Maste   crypto_int32_y = (crypto_int32_y & C1) + ((crypto_int32_y >> 2) & C1);
1055*3d9fd9fcSEd Maste   crypto_int32_y = (crypto_int32_y + (crypto_int32_y >> 4)) & C2;
1056*3d9fd9fcSEd Maste   crypto_int32_y += crypto_int32_y >> 8;
1057*3d9fd9fcSEd Maste   crypto_int32_y = (crypto_int32_y + (crypto_int32_y >> 16)) & 0xff;
1058*3d9fd9fcSEd Maste   return crypto_int32_y;
1059*3d9fd9fcSEd Maste }
1060*3d9fd9fcSEd Maste 
1061*3d9fd9fcSEd Maste __attribute__((unused))
1062*3d9fd9fcSEd Maste static inline
crypto_int32_bottomzeros_num(crypto_int32 crypto_int32_x)1063*3d9fd9fcSEd Maste int crypto_int32_bottomzeros_num(crypto_int32 crypto_int32_x) {
1064*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1065*3d9fd9fcSEd Maste   crypto_int32 fallback = 32;
1066*3d9fd9fcSEd Maste   __asm__ ("bsfl %0,%0\n cmovel %1,%0" : "+&r"(crypto_int32_x) : "r"(fallback) : "cc");
1067*3d9fd9fcSEd Maste   return crypto_int32_x;
1068*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1069*3d9fd9fcSEd Maste   int64_t crypto_int32_y;
1070*3d9fd9fcSEd Maste   __asm__ ("rbit %w0,%w1\n clz %w0,%w0" : "=r"(crypto_int32_y) : "r"(crypto_int32_x) : );
1071*3d9fd9fcSEd Maste   return crypto_int32_y;
1072*3d9fd9fcSEd Maste #else
1073*3d9fd9fcSEd Maste   crypto_int32 crypto_int32_y = crypto_int32_x ^ (crypto_int32_x-1);
1074*3d9fd9fcSEd Maste   crypto_int32_y = ((crypto_int32) crypto_int32_y) >> 1;
1075*3d9fd9fcSEd Maste   crypto_int32_y &= ~(crypto_int32_x & (((crypto_int32) 1) << (32-1)));
1076*3d9fd9fcSEd Maste   return crypto_int32_ones_num(crypto_int32_y);
1077*3d9fd9fcSEd Maste #endif
1078*3d9fd9fcSEd Maste }
1079*3d9fd9fcSEd Maste 
1080*3d9fd9fcSEd Maste #endif
1081*3d9fd9fcSEd Maste 
1082*3d9fd9fcSEd Maste /* from supercop-20240808/cryptoint/crypto_int64.h */
1083*3d9fd9fcSEd Maste /* auto-generated: cd cryptoint; ./autogen */
1084*3d9fd9fcSEd Maste /* cryptoint 20240806 */
1085*3d9fd9fcSEd Maste 
1086*3d9fd9fcSEd Maste #ifndef crypto_int64_h
1087*3d9fd9fcSEd Maste #define crypto_int64_h
1088*3d9fd9fcSEd Maste 
1089*3d9fd9fcSEd Maste #define crypto_int64 int64_t
1090*3d9fd9fcSEd Maste #define crypto_int64_unsigned uint64_t
1091*3d9fd9fcSEd Maste 
1092*3d9fd9fcSEd Maste 
1093*3d9fd9fcSEd Maste 
1094*3d9fd9fcSEd Maste __attribute__((unused))
1095*3d9fd9fcSEd Maste static inline
crypto_int64_load(const unsigned char * crypto_int64_s)1096*3d9fd9fcSEd Maste crypto_int64 crypto_int64_load(const unsigned char *crypto_int64_s) {
1097*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z = 0;
1098*3d9fd9fcSEd Maste   crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 0;
1099*3d9fd9fcSEd Maste   crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 8;
1100*3d9fd9fcSEd Maste   crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 16;
1101*3d9fd9fcSEd Maste   crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 24;
1102*3d9fd9fcSEd Maste   crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 32;
1103*3d9fd9fcSEd Maste   crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 40;
1104*3d9fd9fcSEd Maste   crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 48;
1105*3d9fd9fcSEd Maste   crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 56;
1106*3d9fd9fcSEd Maste   return crypto_int64_z;
1107*3d9fd9fcSEd Maste }
1108*3d9fd9fcSEd Maste 
1109*3d9fd9fcSEd Maste __attribute__((unused))
1110*3d9fd9fcSEd Maste static inline
crypto_int64_store(unsigned char * crypto_int64_s,crypto_int64 crypto_int64_x)1111*3d9fd9fcSEd Maste void crypto_int64_store(unsigned char *crypto_int64_s,crypto_int64 crypto_int64_x) {
1112*3d9fd9fcSEd Maste   *crypto_int64_s++ = crypto_int64_x >> 0;
1113*3d9fd9fcSEd Maste   *crypto_int64_s++ = crypto_int64_x >> 8;
1114*3d9fd9fcSEd Maste   *crypto_int64_s++ = crypto_int64_x >> 16;
1115*3d9fd9fcSEd Maste   *crypto_int64_s++ = crypto_int64_x >> 24;
1116*3d9fd9fcSEd Maste   *crypto_int64_s++ = crypto_int64_x >> 32;
1117*3d9fd9fcSEd Maste   *crypto_int64_s++ = crypto_int64_x >> 40;
1118*3d9fd9fcSEd Maste   *crypto_int64_s++ = crypto_int64_x >> 48;
1119*3d9fd9fcSEd Maste   *crypto_int64_s++ = crypto_int64_x >> 56;
1120*3d9fd9fcSEd Maste }
1121*3d9fd9fcSEd Maste 
1122*3d9fd9fcSEd Maste __attribute__((unused))
1123*3d9fd9fcSEd Maste static inline
crypto_int64_negative_mask(crypto_int64 crypto_int64_x)1124*3d9fd9fcSEd Maste crypto_int64 crypto_int64_negative_mask(crypto_int64 crypto_int64_x) {
1125*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1126*3d9fd9fcSEd Maste   __asm__ ("sarq $63,%0" : "+r"(crypto_int64_x) : : "cc");
1127*3d9fd9fcSEd Maste   return crypto_int64_x;
1128*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1129*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_y;
1130*3d9fd9fcSEd Maste   __asm__ ("asr %0,%1,63" : "=r"(crypto_int64_y) : "r"(crypto_int64_x) : );
1131*3d9fd9fcSEd Maste   return crypto_int64_y;
1132*3d9fd9fcSEd Maste #else
1133*3d9fd9fcSEd Maste   crypto_int64_x >>= 64-6;
1134*3d9fd9fcSEd Maste   crypto_int64_x ^= crypto_int64_optblocker;
1135*3d9fd9fcSEd Maste   crypto_int64_x >>= 5;
1136*3d9fd9fcSEd Maste   return crypto_int64_x;
1137*3d9fd9fcSEd Maste #endif
1138*3d9fd9fcSEd Maste }
1139*3d9fd9fcSEd Maste 
1140*3d9fd9fcSEd Maste __attribute__((unused))
1141*3d9fd9fcSEd Maste static inline
crypto_int64_unsigned_topbit_01(crypto_int64_unsigned crypto_int64_x)1142*3d9fd9fcSEd Maste crypto_int64_unsigned crypto_int64_unsigned_topbit_01(crypto_int64_unsigned crypto_int64_x) {
1143*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1144*3d9fd9fcSEd Maste   __asm__ ("shrq $63,%0" : "+r"(crypto_int64_x) : : "cc");
1145*3d9fd9fcSEd Maste   return crypto_int64_x;
1146*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1147*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_y;
1148*3d9fd9fcSEd Maste   __asm__ ("lsr %0,%1,63" : "=r"(crypto_int64_y) : "r"(crypto_int64_x) : );
1149*3d9fd9fcSEd Maste   return crypto_int64_y;
1150*3d9fd9fcSEd Maste #else
1151*3d9fd9fcSEd Maste   crypto_int64_x >>= 64-6;
1152*3d9fd9fcSEd Maste   crypto_int64_x ^= crypto_int64_optblocker;
1153*3d9fd9fcSEd Maste   crypto_int64_x >>= 5;
1154*3d9fd9fcSEd Maste   return crypto_int64_x;
1155*3d9fd9fcSEd Maste #endif
1156*3d9fd9fcSEd Maste }
1157*3d9fd9fcSEd Maste 
1158*3d9fd9fcSEd Maste __attribute__((unused))
1159*3d9fd9fcSEd Maste static inline
crypto_int64_negative_01(crypto_int64 crypto_int64_x)1160*3d9fd9fcSEd Maste crypto_int64 crypto_int64_negative_01(crypto_int64 crypto_int64_x) {
1161*3d9fd9fcSEd Maste   return crypto_int64_unsigned_topbit_01(crypto_int64_x);
1162*3d9fd9fcSEd Maste }
1163*3d9fd9fcSEd Maste 
1164*3d9fd9fcSEd Maste __attribute__((unused))
1165*3d9fd9fcSEd Maste static inline
crypto_int64_topbit_mask(crypto_int64 crypto_int64_x)1166*3d9fd9fcSEd Maste crypto_int64 crypto_int64_topbit_mask(crypto_int64 crypto_int64_x) {
1167*3d9fd9fcSEd Maste   return crypto_int64_negative_mask(crypto_int64_x);
1168*3d9fd9fcSEd Maste }
1169*3d9fd9fcSEd Maste 
1170*3d9fd9fcSEd Maste __attribute__((unused))
1171*3d9fd9fcSEd Maste static inline
crypto_int64_topbit_01(crypto_int64 crypto_int64_x)1172*3d9fd9fcSEd Maste crypto_int64 crypto_int64_topbit_01(crypto_int64 crypto_int64_x) {
1173*3d9fd9fcSEd Maste   return crypto_int64_unsigned_topbit_01(crypto_int64_x);
1174*3d9fd9fcSEd Maste }
1175*3d9fd9fcSEd Maste 
1176*3d9fd9fcSEd Maste __attribute__((unused))
1177*3d9fd9fcSEd Maste static inline
crypto_int64_bottombit_mask(crypto_int64 crypto_int64_x)1178*3d9fd9fcSEd Maste crypto_int64 crypto_int64_bottombit_mask(crypto_int64 crypto_int64_x) {
1179*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1180*3d9fd9fcSEd Maste   __asm__ ("andq $1,%0" : "+r"(crypto_int64_x) : : "cc");
1181*3d9fd9fcSEd Maste   return -crypto_int64_x;
1182*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1183*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_y;
1184*3d9fd9fcSEd Maste   __asm__ ("sbfx %0,%1,0,1" : "=r"(crypto_int64_y) : "r"(crypto_int64_x) : );
1185*3d9fd9fcSEd Maste   return crypto_int64_y;
1186*3d9fd9fcSEd Maste #else
1187*3d9fd9fcSEd Maste   crypto_int64_x &= 1 ^ crypto_int64_optblocker;
1188*3d9fd9fcSEd Maste   return -crypto_int64_x;
1189*3d9fd9fcSEd Maste #endif
1190*3d9fd9fcSEd Maste }
1191*3d9fd9fcSEd Maste 
1192*3d9fd9fcSEd Maste __attribute__((unused))
1193*3d9fd9fcSEd Maste static inline
crypto_int64_bottombit_01(crypto_int64 crypto_int64_x)1194*3d9fd9fcSEd Maste crypto_int64 crypto_int64_bottombit_01(crypto_int64 crypto_int64_x) {
1195*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1196*3d9fd9fcSEd Maste   __asm__ ("andq $1,%0" : "+r"(crypto_int64_x) : : "cc");
1197*3d9fd9fcSEd Maste   return crypto_int64_x;
1198*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1199*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_y;
1200*3d9fd9fcSEd Maste   __asm__ ("ubfx %0,%1,0,1" : "=r"(crypto_int64_y) : "r"(crypto_int64_x) : );
1201*3d9fd9fcSEd Maste   return crypto_int64_y;
1202*3d9fd9fcSEd Maste #else
1203*3d9fd9fcSEd Maste   crypto_int64_x &= 1 ^ crypto_int64_optblocker;
1204*3d9fd9fcSEd Maste   return crypto_int64_x;
1205*3d9fd9fcSEd Maste #endif
1206*3d9fd9fcSEd Maste }
1207*3d9fd9fcSEd Maste 
1208*3d9fd9fcSEd Maste __attribute__((unused))
1209*3d9fd9fcSEd Maste static inline
crypto_int64_bitinrangepublicpos_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s)1210*3d9fd9fcSEd Maste crypto_int64 crypto_int64_bitinrangepublicpos_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) {
1211*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1212*3d9fd9fcSEd Maste   __asm__ ("sarq %%cl,%0" : "+r"(crypto_int64_x) : "c"(crypto_int64_s) : "cc");
1213*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1214*3d9fd9fcSEd Maste   __asm__ ("asr %0,%0,%1" : "+r"(crypto_int64_x) : "r"(crypto_int64_s) : );
1215*3d9fd9fcSEd Maste #else
1216*3d9fd9fcSEd Maste   crypto_int64_x >>= crypto_int64_s ^ crypto_int64_optblocker;
1217*3d9fd9fcSEd Maste #endif
1218*3d9fd9fcSEd Maste   return crypto_int64_bottombit_mask(crypto_int64_x);
1219*3d9fd9fcSEd Maste }
1220*3d9fd9fcSEd Maste 
1221*3d9fd9fcSEd Maste __attribute__((unused))
1222*3d9fd9fcSEd Maste static inline
crypto_int64_bitinrangepublicpos_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s)1223*3d9fd9fcSEd Maste crypto_int64 crypto_int64_bitinrangepublicpos_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) {
1224*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1225*3d9fd9fcSEd Maste   __asm__ ("sarq %%cl,%0" : "+r"(crypto_int64_x) : "c"(crypto_int64_s) : "cc");
1226*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1227*3d9fd9fcSEd Maste   __asm__ ("asr %0,%0,%1" : "+r"(crypto_int64_x) : "r"(crypto_int64_s) : );
1228*3d9fd9fcSEd Maste #else
1229*3d9fd9fcSEd Maste   crypto_int64_x >>= crypto_int64_s ^ crypto_int64_optblocker;
1230*3d9fd9fcSEd Maste #endif
1231*3d9fd9fcSEd Maste   return crypto_int64_bottombit_01(crypto_int64_x);
1232*3d9fd9fcSEd Maste }
1233*3d9fd9fcSEd Maste 
1234*3d9fd9fcSEd Maste __attribute__((unused))
1235*3d9fd9fcSEd Maste static inline
crypto_int64_shlmod(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s)1236*3d9fd9fcSEd Maste crypto_int64 crypto_int64_shlmod(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) {
1237*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1238*3d9fd9fcSEd Maste   __asm__ ("shlq %%cl,%0" : "+r"(crypto_int64_x) : "c"(crypto_int64_s) : "cc");
1239*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1240*3d9fd9fcSEd Maste   __asm__ ("lsl %0,%0,%1" : "+r"(crypto_int64_x) : "r"(crypto_int64_s) : );
1241*3d9fd9fcSEd Maste #else
1242*3d9fd9fcSEd Maste   int crypto_int64_k, crypto_int64_l;
1243*3d9fd9fcSEd Maste   for (crypto_int64_l = 0,crypto_int64_k = 1;crypto_int64_k < 64;++crypto_int64_l,crypto_int64_k *= 2)
1244*3d9fd9fcSEd Maste     crypto_int64_x ^= (crypto_int64_x ^ (crypto_int64_x << crypto_int64_k)) & crypto_int64_bitinrangepublicpos_mask(crypto_int64_s,crypto_int64_l);
1245*3d9fd9fcSEd Maste #endif
1246*3d9fd9fcSEd Maste   return crypto_int64_x;
1247*3d9fd9fcSEd Maste }
1248*3d9fd9fcSEd Maste 
1249*3d9fd9fcSEd Maste __attribute__((unused))
1250*3d9fd9fcSEd Maste static inline
crypto_int64_shrmod(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s)1251*3d9fd9fcSEd Maste crypto_int64 crypto_int64_shrmod(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) {
1252*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1253*3d9fd9fcSEd Maste   __asm__ ("sarq %%cl,%0" : "+r"(crypto_int64_x) : "c"(crypto_int64_s) : "cc");
1254*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1255*3d9fd9fcSEd Maste   __asm__ ("asr %0,%0,%1" : "+r"(crypto_int64_x) : "r"(crypto_int64_s) : );
1256*3d9fd9fcSEd Maste #else
1257*3d9fd9fcSEd Maste   int crypto_int64_k, crypto_int64_l;
1258*3d9fd9fcSEd Maste   for (crypto_int64_l = 0,crypto_int64_k = 1;crypto_int64_k < 64;++crypto_int64_l,crypto_int64_k *= 2)
1259*3d9fd9fcSEd Maste     crypto_int64_x ^= (crypto_int64_x ^ (crypto_int64_x >> crypto_int64_k)) & crypto_int64_bitinrangepublicpos_mask(crypto_int64_s,crypto_int64_l);
1260*3d9fd9fcSEd Maste #endif
1261*3d9fd9fcSEd Maste   return crypto_int64_x;
1262*3d9fd9fcSEd Maste }
1263*3d9fd9fcSEd Maste 
1264*3d9fd9fcSEd Maste __attribute__((unused))
1265*3d9fd9fcSEd Maste static inline
crypto_int64_bitmod_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s)1266*3d9fd9fcSEd Maste crypto_int64 crypto_int64_bitmod_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) {
1267*3d9fd9fcSEd Maste   crypto_int64_x = crypto_int64_shrmod(crypto_int64_x,crypto_int64_s);
1268*3d9fd9fcSEd Maste   return crypto_int64_bottombit_mask(crypto_int64_x);
1269*3d9fd9fcSEd Maste }
1270*3d9fd9fcSEd Maste 
1271*3d9fd9fcSEd Maste __attribute__((unused))
1272*3d9fd9fcSEd Maste static inline
crypto_int64_bitmod_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s)1273*3d9fd9fcSEd Maste crypto_int64 crypto_int64_bitmod_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) {
1274*3d9fd9fcSEd Maste   crypto_int64_x = crypto_int64_shrmod(crypto_int64_x,crypto_int64_s);
1275*3d9fd9fcSEd Maste   return crypto_int64_bottombit_01(crypto_int64_x);
1276*3d9fd9fcSEd Maste }
1277*3d9fd9fcSEd Maste 
1278*3d9fd9fcSEd Maste __attribute__((unused))
1279*3d9fd9fcSEd Maste static inline
crypto_int64_nonzero_mask(crypto_int64 crypto_int64_x)1280*3d9fd9fcSEd Maste crypto_int64 crypto_int64_nonzero_mask(crypto_int64 crypto_int64_x) {
1281*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1282*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_q,crypto_int64_z;
1283*3d9fd9fcSEd Maste   __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");
1284*3d9fd9fcSEd Maste   return crypto_int64_z;
1285*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1286*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z;
1287*3d9fd9fcSEd Maste   __asm__ ("cmp %1,0\n csetm %0,ne" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc");
1288*3d9fd9fcSEd Maste   return crypto_int64_z;
1289*3d9fd9fcSEd Maste #else
1290*3d9fd9fcSEd Maste   crypto_int64_x |= -crypto_int64_x;
1291*3d9fd9fcSEd Maste   return crypto_int64_negative_mask(crypto_int64_x);
1292*3d9fd9fcSEd Maste #endif
1293*3d9fd9fcSEd Maste }
1294*3d9fd9fcSEd Maste 
1295*3d9fd9fcSEd Maste __attribute__((unused))
1296*3d9fd9fcSEd Maste static inline
crypto_int64_nonzero_01(crypto_int64 crypto_int64_x)1297*3d9fd9fcSEd Maste crypto_int64 crypto_int64_nonzero_01(crypto_int64 crypto_int64_x) {
1298*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1299*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_q,crypto_int64_z;
1300*3d9fd9fcSEd Maste   __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");
1301*3d9fd9fcSEd Maste   return crypto_int64_z;
1302*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1303*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z;
1304*3d9fd9fcSEd Maste   __asm__ ("cmp %1,0\n cset %0,ne" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc");
1305*3d9fd9fcSEd Maste   return crypto_int64_z;
1306*3d9fd9fcSEd Maste #else
1307*3d9fd9fcSEd Maste   crypto_int64_x |= -crypto_int64_x;
1308*3d9fd9fcSEd Maste   return crypto_int64_unsigned_topbit_01(crypto_int64_x);
1309*3d9fd9fcSEd Maste #endif
1310*3d9fd9fcSEd Maste }
1311*3d9fd9fcSEd Maste 
1312*3d9fd9fcSEd Maste __attribute__((unused))
1313*3d9fd9fcSEd Maste static inline
crypto_int64_positive_mask(crypto_int64 crypto_int64_x)1314*3d9fd9fcSEd Maste crypto_int64 crypto_int64_positive_mask(crypto_int64 crypto_int64_x) {
1315*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1316*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_q,crypto_int64_z;
1317*3d9fd9fcSEd Maste   __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");
1318*3d9fd9fcSEd Maste   return crypto_int64_z;
1319*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1320*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z;
1321*3d9fd9fcSEd Maste   __asm__ ("cmp %1,0\n csetm %0,gt" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc");
1322*3d9fd9fcSEd Maste   return crypto_int64_z;
1323*3d9fd9fcSEd Maste #else
1324*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z = -crypto_int64_x;
1325*3d9fd9fcSEd Maste   crypto_int64_z ^= crypto_int64_x & crypto_int64_z;
1326*3d9fd9fcSEd Maste   return crypto_int64_negative_mask(crypto_int64_z);
1327*3d9fd9fcSEd Maste #endif
1328*3d9fd9fcSEd Maste }
1329*3d9fd9fcSEd Maste 
1330*3d9fd9fcSEd Maste __attribute__((unused))
1331*3d9fd9fcSEd Maste static inline
crypto_int64_positive_01(crypto_int64 crypto_int64_x)1332*3d9fd9fcSEd Maste crypto_int64 crypto_int64_positive_01(crypto_int64 crypto_int64_x) {
1333*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1334*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_q,crypto_int64_z;
1335*3d9fd9fcSEd Maste   __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");
1336*3d9fd9fcSEd Maste   return crypto_int64_z;
1337*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1338*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z;
1339*3d9fd9fcSEd Maste   __asm__ ("cmp %1,0\n cset %0,gt" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc");
1340*3d9fd9fcSEd Maste   return crypto_int64_z;
1341*3d9fd9fcSEd Maste #else
1342*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z = -crypto_int64_x;
1343*3d9fd9fcSEd Maste   crypto_int64_z ^= crypto_int64_x & crypto_int64_z;
1344*3d9fd9fcSEd Maste   return crypto_int64_unsigned_topbit_01(crypto_int64_z);
1345*3d9fd9fcSEd Maste #endif
1346*3d9fd9fcSEd Maste }
1347*3d9fd9fcSEd Maste 
1348*3d9fd9fcSEd Maste __attribute__((unused))
1349*3d9fd9fcSEd Maste static inline
crypto_int64_zero_mask(crypto_int64 crypto_int64_x)1350*3d9fd9fcSEd Maste crypto_int64 crypto_int64_zero_mask(crypto_int64 crypto_int64_x) {
1351*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1352*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_q,crypto_int64_z;
1353*3d9fd9fcSEd Maste   __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");
1354*3d9fd9fcSEd Maste   return crypto_int64_z;
1355*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1356*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z;
1357*3d9fd9fcSEd Maste   __asm__ ("cmp %1,0\n csetm %0,eq" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc");
1358*3d9fd9fcSEd Maste   return crypto_int64_z;
1359*3d9fd9fcSEd Maste #else
1360*3d9fd9fcSEd Maste   return ~crypto_int64_nonzero_mask(crypto_int64_x);
1361*3d9fd9fcSEd Maste #endif
1362*3d9fd9fcSEd Maste }
1363*3d9fd9fcSEd Maste 
1364*3d9fd9fcSEd Maste __attribute__((unused))
1365*3d9fd9fcSEd Maste static inline
crypto_int64_zero_01(crypto_int64 crypto_int64_x)1366*3d9fd9fcSEd Maste crypto_int64 crypto_int64_zero_01(crypto_int64 crypto_int64_x) {
1367*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1368*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_q,crypto_int64_z;
1369*3d9fd9fcSEd Maste   __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");
1370*3d9fd9fcSEd Maste   return crypto_int64_z;
1371*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1372*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z;
1373*3d9fd9fcSEd Maste   __asm__ ("cmp %1,0\n cset %0,eq" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc");
1374*3d9fd9fcSEd Maste   return crypto_int64_z;
1375*3d9fd9fcSEd Maste #else
1376*3d9fd9fcSEd Maste   return 1-crypto_int64_nonzero_01(crypto_int64_x);
1377*3d9fd9fcSEd Maste #endif
1378*3d9fd9fcSEd Maste }
1379*3d9fd9fcSEd Maste 
1380*3d9fd9fcSEd Maste __attribute__((unused))
1381*3d9fd9fcSEd Maste static inline
crypto_int64_unequal_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y)1382*3d9fd9fcSEd Maste crypto_int64 crypto_int64_unequal_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
1383*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1384*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_q,crypto_int64_z;
1385*3d9fd9fcSEd Maste   __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");
1386*3d9fd9fcSEd Maste   return crypto_int64_z;
1387*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1388*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z;
1389*3d9fd9fcSEd Maste   __asm__ ("cmp %1,%2\n csetm %0,ne" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
1390*3d9fd9fcSEd Maste   return crypto_int64_z;
1391*3d9fd9fcSEd Maste #else
1392*3d9fd9fcSEd Maste   return crypto_int64_nonzero_mask(crypto_int64_x ^ crypto_int64_y);
1393*3d9fd9fcSEd Maste #endif
1394*3d9fd9fcSEd Maste }
1395*3d9fd9fcSEd Maste 
1396*3d9fd9fcSEd Maste __attribute__((unused))
1397*3d9fd9fcSEd Maste static inline
crypto_int64_unequal_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y)1398*3d9fd9fcSEd Maste crypto_int64 crypto_int64_unequal_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
1399*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1400*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_q,crypto_int64_z;
1401*3d9fd9fcSEd Maste   __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");
1402*3d9fd9fcSEd Maste   return crypto_int64_z;
1403*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1404*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z;
1405*3d9fd9fcSEd Maste   __asm__ ("cmp %1,%2\n cset %0,ne" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
1406*3d9fd9fcSEd Maste   return crypto_int64_z;
1407*3d9fd9fcSEd Maste #else
1408*3d9fd9fcSEd Maste   return crypto_int64_nonzero_01(crypto_int64_x ^ crypto_int64_y);
1409*3d9fd9fcSEd Maste #endif
1410*3d9fd9fcSEd Maste }
1411*3d9fd9fcSEd Maste 
1412*3d9fd9fcSEd Maste __attribute__((unused))
1413*3d9fd9fcSEd Maste static inline
crypto_int64_equal_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y)1414*3d9fd9fcSEd Maste crypto_int64 crypto_int64_equal_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
1415*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1416*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_q,crypto_int64_z;
1417*3d9fd9fcSEd Maste   __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");
1418*3d9fd9fcSEd Maste   return crypto_int64_z;
1419*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1420*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z;
1421*3d9fd9fcSEd Maste   __asm__ ("cmp %1,%2\n csetm %0,eq" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
1422*3d9fd9fcSEd Maste   return crypto_int64_z;
1423*3d9fd9fcSEd Maste #else
1424*3d9fd9fcSEd Maste   return ~crypto_int64_unequal_mask(crypto_int64_x,crypto_int64_y);
1425*3d9fd9fcSEd Maste #endif
1426*3d9fd9fcSEd Maste }
1427*3d9fd9fcSEd Maste 
1428*3d9fd9fcSEd Maste __attribute__((unused))
1429*3d9fd9fcSEd Maste static inline
crypto_int64_equal_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y)1430*3d9fd9fcSEd Maste crypto_int64 crypto_int64_equal_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
1431*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1432*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_q,crypto_int64_z;
1433*3d9fd9fcSEd Maste   __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");
1434*3d9fd9fcSEd Maste   return crypto_int64_z;
1435*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1436*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z;
1437*3d9fd9fcSEd Maste   __asm__ ("cmp %1,%2\n cset %0,eq" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
1438*3d9fd9fcSEd Maste   return crypto_int64_z;
1439*3d9fd9fcSEd Maste #else
1440*3d9fd9fcSEd Maste   return 1-crypto_int64_unequal_01(crypto_int64_x,crypto_int64_y);
1441*3d9fd9fcSEd Maste #endif
1442*3d9fd9fcSEd Maste }
1443*3d9fd9fcSEd Maste 
1444*3d9fd9fcSEd Maste __attribute__((unused))
1445*3d9fd9fcSEd Maste static inline
crypto_int64_min(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y)1446*3d9fd9fcSEd Maste crypto_int64 crypto_int64_min(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
1447*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1448*3d9fd9fcSEd Maste   __asm__ ("cmpq %1,%0\n cmovgq %1,%0" : "+r"(crypto_int64_x) : "r"(crypto_int64_y) : "cc");
1449*3d9fd9fcSEd Maste   return crypto_int64_x;
1450*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1451*3d9fd9fcSEd Maste   __asm__ ("cmp %0,%1\n csel %0,%0,%1,lt" : "+r"(crypto_int64_x) : "r"(crypto_int64_y) : "cc");
1452*3d9fd9fcSEd Maste   return crypto_int64_x;
1453*3d9fd9fcSEd Maste #else
1454*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_r = crypto_int64_y ^ crypto_int64_x;
1455*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z = crypto_int64_y - crypto_int64_x;
1456*3d9fd9fcSEd Maste   crypto_int64_z ^= crypto_int64_r & (crypto_int64_z ^ crypto_int64_y);
1457*3d9fd9fcSEd Maste   crypto_int64_z = crypto_int64_negative_mask(crypto_int64_z);
1458*3d9fd9fcSEd Maste   crypto_int64_z &= crypto_int64_r;
1459*3d9fd9fcSEd Maste   return crypto_int64_x ^ crypto_int64_z;
1460*3d9fd9fcSEd Maste #endif
1461*3d9fd9fcSEd Maste }
1462*3d9fd9fcSEd Maste 
1463*3d9fd9fcSEd Maste __attribute__((unused))
1464*3d9fd9fcSEd Maste static inline
crypto_int64_max(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y)1465*3d9fd9fcSEd Maste crypto_int64 crypto_int64_max(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
1466*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1467*3d9fd9fcSEd Maste   __asm__ ("cmpq %1,%0\n cmovlq %1,%0" : "+r"(crypto_int64_x) : "r"(crypto_int64_y) : "cc");
1468*3d9fd9fcSEd Maste   return crypto_int64_x;
1469*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1470*3d9fd9fcSEd Maste   __asm__ ("cmp %0,%1\n csel %0,%1,%0,lt" : "+r"(crypto_int64_x) : "r"(crypto_int64_y) : "cc");
1471*3d9fd9fcSEd Maste   return crypto_int64_x;
1472*3d9fd9fcSEd Maste #else
1473*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_r = crypto_int64_y ^ crypto_int64_x;
1474*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z = crypto_int64_y - crypto_int64_x;
1475*3d9fd9fcSEd Maste   crypto_int64_z ^= crypto_int64_r & (crypto_int64_z ^ crypto_int64_y);
1476*3d9fd9fcSEd Maste   crypto_int64_z = crypto_int64_negative_mask(crypto_int64_z);
1477*3d9fd9fcSEd Maste   crypto_int64_z &= crypto_int64_r;
1478*3d9fd9fcSEd Maste   return crypto_int64_y ^ crypto_int64_z;
1479*3d9fd9fcSEd Maste #endif
1480*3d9fd9fcSEd Maste }
1481*3d9fd9fcSEd Maste 
1482*3d9fd9fcSEd Maste __attribute__((unused))
1483*3d9fd9fcSEd Maste static inline
crypto_int64_minmax(crypto_int64 * crypto_int64_p,crypto_int64 * crypto_int64_q)1484*3d9fd9fcSEd Maste void crypto_int64_minmax(crypto_int64 *crypto_int64_p,crypto_int64 *crypto_int64_q) {
1485*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_x = *crypto_int64_p;
1486*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_y = *crypto_int64_q;
1487*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1488*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z;
1489*3d9fd9fcSEd Maste   __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");
1490*3d9fd9fcSEd Maste   *crypto_int64_p = crypto_int64_x;
1491*3d9fd9fcSEd Maste   *crypto_int64_q = crypto_int64_y;
1492*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1493*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_r, crypto_int64_s;
1494*3d9fd9fcSEd Maste   __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");
1495*3d9fd9fcSEd Maste   *crypto_int64_p = crypto_int64_r;
1496*3d9fd9fcSEd Maste   *crypto_int64_q = crypto_int64_s;
1497*3d9fd9fcSEd Maste #else
1498*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_r = crypto_int64_y ^ crypto_int64_x;
1499*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z = crypto_int64_y - crypto_int64_x;
1500*3d9fd9fcSEd Maste   crypto_int64_z ^= crypto_int64_r & (crypto_int64_z ^ crypto_int64_y);
1501*3d9fd9fcSEd Maste   crypto_int64_z = crypto_int64_negative_mask(crypto_int64_z);
1502*3d9fd9fcSEd Maste   crypto_int64_z &= crypto_int64_r;
1503*3d9fd9fcSEd Maste   crypto_int64_x ^= crypto_int64_z;
1504*3d9fd9fcSEd Maste   crypto_int64_y ^= crypto_int64_z;
1505*3d9fd9fcSEd Maste   *crypto_int64_p = crypto_int64_x;
1506*3d9fd9fcSEd Maste   *crypto_int64_q = crypto_int64_y;
1507*3d9fd9fcSEd Maste #endif
1508*3d9fd9fcSEd Maste }
1509*3d9fd9fcSEd Maste 
1510*3d9fd9fcSEd Maste __attribute__((unused))
1511*3d9fd9fcSEd Maste static inline
crypto_int64_smaller_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y)1512*3d9fd9fcSEd Maste crypto_int64 crypto_int64_smaller_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
1513*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1514*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_q,crypto_int64_z;
1515*3d9fd9fcSEd Maste   __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");
1516*3d9fd9fcSEd Maste   return crypto_int64_z;
1517*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1518*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z;
1519*3d9fd9fcSEd Maste   __asm__ ("cmp %1,%2\n csetm %0,lt" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
1520*3d9fd9fcSEd Maste   return crypto_int64_z;
1521*3d9fd9fcSEd Maste #else
1522*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_r = crypto_int64_x ^ crypto_int64_y;
1523*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z = crypto_int64_x - crypto_int64_y;
1524*3d9fd9fcSEd Maste   crypto_int64_z ^= crypto_int64_r & (crypto_int64_z ^ crypto_int64_x);
1525*3d9fd9fcSEd Maste   return crypto_int64_negative_mask(crypto_int64_z);
1526*3d9fd9fcSEd Maste #endif
1527*3d9fd9fcSEd Maste }
1528*3d9fd9fcSEd Maste 
1529*3d9fd9fcSEd Maste __attribute__((unused))
1530*3d9fd9fcSEd Maste static inline
crypto_int64_smaller_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y)1531*3d9fd9fcSEd Maste crypto_int64 crypto_int64_smaller_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
1532*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1533*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_q,crypto_int64_z;
1534*3d9fd9fcSEd Maste   __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");
1535*3d9fd9fcSEd Maste   return crypto_int64_z;
1536*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1537*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z;
1538*3d9fd9fcSEd Maste   __asm__ ("cmp %1,%2\n cset %0,lt" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
1539*3d9fd9fcSEd Maste   return crypto_int64_z;
1540*3d9fd9fcSEd Maste #else
1541*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_r = crypto_int64_x ^ crypto_int64_y;
1542*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z = crypto_int64_x - crypto_int64_y;
1543*3d9fd9fcSEd Maste   crypto_int64_z ^= crypto_int64_r & (crypto_int64_z ^ crypto_int64_x);
1544*3d9fd9fcSEd Maste   return crypto_int64_unsigned_topbit_01(crypto_int64_z);
1545*3d9fd9fcSEd Maste #endif
1546*3d9fd9fcSEd Maste }
1547*3d9fd9fcSEd Maste 
1548*3d9fd9fcSEd Maste __attribute__((unused))
1549*3d9fd9fcSEd Maste static inline
crypto_int64_leq_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y)1550*3d9fd9fcSEd Maste crypto_int64 crypto_int64_leq_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
1551*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1552*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_q,crypto_int64_z;
1553*3d9fd9fcSEd Maste   __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");
1554*3d9fd9fcSEd Maste   return crypto_int64_z;
1555*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1556*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z;
1557*3d9fd9fcSEd Maste   __asm__ ("cmp %1,%2\n csetm %0,le" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
1558*3d9fd9fcSEd Maste   return crypto_int64_z;
1559*3d9fd9fcSEd Maste #else
1560*3d9fd9fcSEd Maste   return ~crypto_int64_smaller_mask(crypto_int64_y,crypto_int64_x);
1561*3d9fd9fcSEd Maste #endif
1562*3d9fd9fcSEd Maste }
1563*3d9fd9fcSEd Maste 
1564*3d9fd9fcSEd Maste __attribute__((unused))
1565*3d9fd9fcSEd Maste static inline
crypto_int64_leq_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y)1566*3d9fd9fcSEd Maste crypto_int64 crypto_int64_leq_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) {
1567*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1568*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_q,crypto_int64_z;
1569*3d9fd9fcSEd Maste   __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");
1570*3d9fd9fcSEd Maste   return crypto_int64_z;
1571*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1572*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_z;
1573*3d9fd9fcSEd Maste   __asm__ ("cmp %1,%2\n cset %0,le" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc");
1574*3d9fd9fcSEd Maste   return crypto_int64_z;
1575*3d9fd9fcSEd Maste #else
1576*3d9fd9fcSEd Maste   return 1-crypto_int64_smaller_01(crypto_int64_y,crypto_int64_x);
1577*3d9fd9fcSEd Maste #endif
1578*3d9fd9fcSEd Maste }
1579*3d9fd9fcSEd Maste 
1580*3d9fd9fcSEd Maste __attribute__((unused))
1581*3d9fd9fcSEd Maste static inline
crypto_int64_ones_num(crypto_int64 crypto_int64_x)1582*3d9fd9fcSEd Maste int crypto_int64_ones_num(crypto_int64 crypto_int64_x) {
1583*3d9fd9fcSEd Maste   crypto_int64_unsigned crypto_int64_y = crypto_int64_x;
1584*3d9fd9fcSEd Maste   const crypto_int64 C0 = 0x5555555555555555;
1585*3d9fd9fcSEd Maste   const crypto_int64 C1 = 0x3333333333333333;
1586*3d9fd9fcSEd Maste   const crypto_int64 C2 = 0x0f0f0f0f0f0f0f0f;
1587*3d9fd9fcSEd Maste   crypto_int64_y -= ((crypto_int64_y >> 1) & C0);
1588*3d9fd9fcSEd Maste   crypto_int64_y = (crypto_int64_y & C1) + ((crypto_int64_y >> 2) & C1);
1589*3d9fd9fcSEd Maste   crypto_int64_y = (crypto_int64_y + (crypto_int64_y >> 4)) & C2;
1590*3d9fd9fcSEd Maste   crypto_int64_y += crypto_int64_y >> 8;
1591*3d9fd9fcSEd Maste   crypto_int64_y += crypto_int64_y >> 16;
1592*3d9fd9fcSEd Maste   crypto_int64_y = (crypto_int64_y + (crypto_int64_y >> 32)) & 0xff;
1593*3d9fd9fcSEd Maste   return crypto_int64_y;
1594*3d9fd9fcSEd Maste }
1595*3d9fd9fcSEd Maste 
1596*3d9fd9fcSEd Maste __attribute__((unused))
1597*3d9fd9fcSEd Maste static inline
crypto_int64_bottomzeros_num(crypto_int64 crypto_int64_x)1598*3d9fd9fcSEd Maste int crypto_int64_bottomzeros_num(crypto_int64 crypto_int64_x) {
1599*3d9fd9fcSEd Maste #if defined(__GNUC__) && defined(__x86_64__)
1600*3d9fd9fcSEd Maste   crypto_int64 fallback = 64;
1601*3d9fd9fcSEd Maste   __asm__ ("bsfq %0,%0\n cmoveq %1,%0" : "+&r"(crypto_int64_x) : "r"(fallback) : "cc");
1602*3d9fd9fcSEd Maste   return crypto_int64_x;
1603*3d9fd9fcSEd Maste #elif defined(__GNUC__) && defined(__aarch64__)
1604*3d9fd9fcSEd Maste   int64_t crypto_int64_y;
1605*3d9fd9fcSEd Maste   __asm__ ("rbit %0,%1\n clz %0,%0" : "=r"(crypto_int64_y) : "r"(crypto_int64_x) : );
1606*3d9fd9fcSEd Maste   return crypto_int64_y;
1607*3d9fd9fcSEd Maste #else
1608*3d9fd9fcSEd Maste   crypto_int64 crypto_int64_y = crypto_int64_x ^ (crypto_int64_x-1);
1609*3d9fd9fcSEd Maste   crypto_int64_y = ((crypto_int64) crypto_int64_y) >> 1;
1610*3d9fd9fcSEd Maste   crypto_int64_y &= ~(crypto_int64_x & (((crypto_int64) 1) << (64-1)));
1611*3d9fd9fcSEd Maste   return crypto_int64_ones_num(crypto_int64_y);
1612*3d9fd9fcSEd Maste #endif
1613*3d9fd9fcSEd Maste }
1614*3d9fd9fcSEd Maste 
1615*3d9fd9fcSEd Maste #endif
1616*3d9fd9fcSEd Maste 
1617*3d9fd9fcSEd Maste /* from supercop-20240808/crypto_sort/int32/portable4/sort.c */
1618*3d9fd9fcSEd Maste #define int32_MINMAX(a,b) crypto_int32_minmax(&a,&b)
161919261079SEd Maste 
crypto_sort_int32(void * array,long long n)162019261079SEd Maste static void crypto_sort_int32(void *array,long long n)
162119261079SEd Maste {
162219261079SEd Maste   long long top,p,q,r,i,j;
162319261079SEd Maste   int32 *x = array;
162419261079SEd Maste 
162519261079SEd Maste   if (n < 2) return;
162619261079SEd Maste   top = 1;
162719261079SEd Maste   while (top < n - top) top += top;
162819261079SEd Maste 
162919261079SEd Maste   for (p = top;p >= 1;p >>= 1) {
163019261079SEd Maste     i = 0;
163119261079SEd Maste     while (i + 2 * p <= n) {
163219261079SEd Maste       for (j = i;j < i + p;++j)
163319261079SEd Maste         int32_MINMAX(x[j],x[j+p]);
163419261079SEd Maste       i += 2 * p;
163519261079SEd Maste     }
163619261079SEd Maste     for (j = i;j < n - p;++j)
163719261079SEd Maste       int32_MINMAX(x[j],x[j+p]);
163819261079SEd Maste 
163919261079SEd Maste     i = 0;
164019261079SEd Maste     j = 0;
164119261079SEd Maste     for (q = top;q > p;q >>= 1) {
164219261079SEd Maste       if (j != i) for (;;) {
164319261079SEd Maste         if (j == n - q) goto done;
164419261079SEd Maste         int32 a = x[j + p];
164519261079SEd Maste         for (r = q;r > p;r >>= 1)
164619261079SEd Maste           int32_MINMAX(a,x[j + r]);
164719261079SEd Maste         x[j + p] = a;
164819261079SEd Maste         ++j;
164919261079SEd Maste         if (j == i + p) {
165019261079SEd Maste           i += 2 * p;
165119261079SEd Maste           break;
165219261079SEd Maste         }
165319261079SEd Maste       }
165419261079SEd Maste       while (i + p <= n - q) {
165519261079SEd Maste         for (j = i;j < i + p;++j) {
165619261079SEd Maste           int32 a = x[j + p];
165719261079SEd Maste           for (r = q;r > p;r >>= 1)
165819261079SEd Maste             int32_MINMAX(a,x[j+r]);
165919261079SEd Maste           x[j + p] = a;
166019261079SEd Maste         }
166119261079SEd Maste         i += 2 * p;
166219261079SEd Maste       }
166319261079SEd Maste       /* now i + p > n - q */
166419261079SEd Maste       j = i;
166519261079SEd Maste       while (j < n - q) {
166619261079SEd Maste         int32 a = x[j + p];
166719261079SEd Maste         for (r = q;r > p;r >>= 1)
166819261079SEd Maste           int32_MINMAX(a,x[j+r]);
166919261079SEd Maste         x[j + p] = a;
167019261079SEd Maste         ++j;
167119261079SEd Maste       }
167219261079SEd Maste 
167319261079SEd Maste       done: ;
167419261079SEd Maste     }
167519261079SEd Maste   }
167619261079SEd Maste }
167719261079SEd Maste 
1678*3d9fd9fcSEd Maste /* from supercop-20240808/crypto_sort/uint32/useint32/sort.c */
167919261079SEd Maste 
168019261079SEd Maste /* can save time by vectorizing xor loops */
168119261079SEd Maste /* can save time by integrating xor loops with int32_sort */
168219261079SEd Maste 
crypto_sort_uint32(void * array,long long n)168319261079SEd Maste static void crypto_sort_uint32(void *array,long long n)
168419261079SEd Maste {
168519261079SEd Maste   crypto_uint32 *x = array;
168619261079SEd Maste   long long j;
168719261079SEd Maste   for (j = 0;j < n;++j) x[j] ^= 0x80000000;
168819261079SEd Maste   crypto_sort_int32(array,n);
168919261079SEd Maste   for (j = 0;j < n;++j) x[j] ^= 0x80000000;
169019261079SEd Maste }
169119261079SEd Maste 
1692*3d9fd9fcSEd Maste /* from supercop-20240808/crypto_kem/sntrup761/compact/kem.c */
1693*3d9fd9fcSEd Maste // 20240806 djb: some automated conversion to cryptoint
169419261079SEd Maste 
1695*3d9fd9fcSEd Maste #define p 761
1696*3d9fd9fcSEd Maste #define q 4591
1697*3d9fd9fcSEd Maste #define w 286
1698*3d9fd9fcSEd Maste #define q12 ((q - 1) / 2)
1699*3d9fd9fcSEd Maste typedef int8_t small;
1700*3d9fd9fcSEd Maste typedef int16_t Fq;
1701*3d9fd9fcSEd Maste #define Hash_bytes 32
1702*3d9fd9fcSEd Maste #define Small_bytes ((p + 3) / 4)
1703*3d9fd9fcSEd Maste typedef small Inputs[p];
1704*3d9fd9fcSEd Maste #define SecretKeys_bytes (2 * Small_bytes)
1705*3d9fd9fcSEd Maste #define Confirm_bytes 32
170619261079SEd Maste 
F3_freeze(int16_t x)1707*3d9fd9fcSEd Maste static small F3_freeze(int16_t x) { return x - 3 * ((10923 * x + 16384) >> 15); }
170819261079SEd Maste 
Fq_freeze(int32_t x)1709*3d9fd9fcSEd Maste static Fq Fq_freeze(int32_t x) {
1710*3d9fd9fcSEd Maste   const int32_t q16 = (0x10000 + q / 2) / q;
1711*3d9fd9fcSEd Maste   const int32_t q20 = (0x100000 + q / 2) / q;
1712*3d9fd9fcSEd Maste   const int32_t q28 = (0x10000000 + q / 2) / q;
1713*3d9fd9fcSEd Maste   x -= q * ((q16 * x) >> 16);
1714*3d9fd9fcSEd Maste   x -= q * ((q20 * x) >> 20);
1715*3d9fd9fcSEd Maste   return x - q * ((q28 * x + 0x8000000) >> 28);
1716*3d9fd9fcSEd Maste }
171719261079SEd Maste 
Weightw_mask(small * r)1718*3d9fd9fcSEd Maste static int Weightw_mask(small *r) {
1719*3d9fd9fcSEd Maste   int i, weight = 0;
1720*3d9fd9fcSEd Maste   for (i = 0; i < p; ++i) weight += crypto_int64_bottombit_01(r[i]);
1721*3d9fd9fcSEd Maste   return crypto_int16_nonzero_mask(weight - w);
1722*3d9fd9fcSEd Maste }
172319261079SEd Maste 
uint32_divmod_uint14(uint32_t * Q,uint16_t * r,uint32_t x,uint16_t m)1724*3d9fd9fcSEd Maste static void uint32_divmod_uint14(uint32_t *Q, uint16_t *r, uint32_t x, uint16_t m) {
1725*3d9fd9fcSEd Maste   uint32_t qpart, mask, v = 0x80000000 / m;
1726*3d9fd9fcSEd Maste   qpart = (x * (uint64_t)v) >> 31;
1727*3d9fd9fcSEd Maste   x -= qpart * m;
1728*3d9fd9fcSEd Maste   *Q = qpart;
1729*3d9fd9fcSEd Maste   qpart = (x * (uint64_t)v) >> 31;
1730*3d9fd9fcSEd Maste   x -= qpart * m;
1731*3d9fd9fcSEd Maste   *Q += qpart;
1732*3d9fd9fcSEd Maste   x -= m;
1733*3d9fd9fcSEd Maste   *Q += 1;
1734*3d9fd9fcSEd Maste   mask = crypto_int32_negative_mask(x);
1735*3d9fd9fcSEd Maste   x += mask & (uint32_t)m;
1736*3d9fd9fcSEd Maste   *Q += mask;
173719261079SEd Maste   *r = x;
173819261079SEd Maste }
173919261079SEd Maste 
uint32_mod_uint14(uint32_t x,uint16_t m)1740*3d9fd9fcSEd Maste static uint16_t uint32_mod_uint14(uint32_t x, uint16_t m) {
1741*3d9fd9fcSEd Maste   uint32_t Q;
1742*3d9fd9fcSEd Maste   uint16_t r;
1743*3d9fd9fcSEd Maste   uint32_divmod_uint14(&Q, &r, x, m);
174419261079SEd Maste   return r;
174519261079SEd Maste }
174619261079SEd Maste 
Encode(unsigned char * out,const uint16_t * R,const uint16_t * M,long long len)1747*3d9fd9fcSEd Maste static void Encode(unsigned char *out, const uint16_t *R, const uint16_t *M, long long len) {
174819261079SEd Maste   if (len == 1) {
1749*3d9fd9fcSEd Maste     uint16_t r = R[0], m = M[0];
175019261079SEd Maste     while (m > 1) {
175119261079SEd Maste       *out++ = r;
175219261079SEd Maste       r >>= 8;
175319261079SEd Maste       m = (m + 255) >> 8;
175419261079SEd Maste     }
175519261079SEd Maste   }
175619261079SEd Maste   if (len > 1) {
1757*3d9fd9fcSEd Maste     uint16_t R2[(len + 1) / 2], M2[(len + 1) / 2];
175819261079SEd Maste     long long i;
175919261079SEd Maste     for (i = 0; i < len - 1; i += 2) {
1760*3d9fd9fcSEd Maste       uint32_t m0 = M[i];
1761*3d9fd9fcSEd Maste       uint32_t r = R[i] + R[i + 1] * m0;
1762*3d9fd9fcSEd Maste       uint32_t m = M[i + 1] * m0;
176319261079SEd Maste       while (m >= 16384) {
176419261079SEd Maste         *out++ = r;
176519261079SEd Maste         r >>= 8;
176619261079SEd Maste         m = (m + 255) >> 8;
176719261079SEd Maste       }
176819261079SEd Maste       R2[i / 2] = r;
176919261079SEd Maste       M2[i / 2] = m;
177019261079SEd Maste     }
177119261079SEd Maste     if (i < len) {
177219261079SEd Maste       R2[i / 2] = R[i];
177319261079SEd Maste       M2[i / 2] = M[i];
177419261079SEd Maste     }
177519261079SEd Maste     Encode(out, R2, M2, (len + 1) / 2);
177619261079SEd Maste   }
177719261079SEd Maste }
177819261079SEd Maste 
Decode(uint16_t * out,const unsigned char * S,const uint16_t * M,long long len)1779*3d9fd9fcSEd Maste static void Decode(uint16_t *out, const unsigned char *S, const uint16_t *M, long long len) {
1780*3d9fd9fcSEd Maste   if (len == 1) {
1781*3d9fd9fcSEd Maste     if (M[0] == 1)
1782*3d9fd9fcSEd Maste       *out = 0;
1783*3d9fd9fcSEd Maste     else if (M[0] <= 256)
1784*3d9fd9fcSEd Maste       *out = uint32_mod_uint14(S[0], M[0]);
1785*3d9fd9fcSEd Maste     else
1786*3d9fd9fcSEd Maste       *out = uint32_mod_uint14(S[0] + (((uint16_t)S[1]) << 8), M[0]);
1787*3d9fd9fcSEd Maste   }
1788*3d9fd9fcSEd Maste   if (len > 1) {
1789*3d9fd9fcSEd Maste     uint16_t R2[(len + 1) / 2], M2[(len + 1) / 2], bottomr[len / 2];
1790*3d9fd9fcSEd Maste     uint32_t bottomt[len / 2];
1791*3d9fd9fcSEd Maste     long long i;
1792*3d9fd9fcSEd Maste     for (i = 0; i < len - 1; i += 2) {
1793*3d9fd9fcSEd Maste       uint32_t m = M[i] * (uint32_t)M[i + 1];
1794*3d9fd9fcSEd Maste       if (m > 256 * 16383) {
1795*3d9fd9fcSEd Maste         bottomt[i / 2] = 256 * 256;
1796*3d9fd9fcSEd Maste         bottomr[i / 2] = S[0] + 256 * S[1];
1797*3d9fd9fcSEd Maste         S += 2;
1798*3d9fd9fcSEd Maste         M2[i / 2] = (((m + 255) >> 8) + 255) >> 8;
1799*3d9fd9fcSEd Maste       } else if (m >= 16384) {
1800*3d9fd9fcSEd Maste         bottomt[i / 2] = 256;
1801*3d9fd9fcSEd Maste         bottomr[i / 2] = S[0];
1802*3d9fd9fcSEd Maste         S += 1;
1803*3d9fd9fcSEd Maste         M2[i / 2] = (m + 255) >> 8;
1804*3d9fd9fcSEd Maste       } else {
1805*3d9fd9fcSEd Maste         bottomt[i / 2] = 1;
1806*3d9fd9fcSEd Maste         bottomr[i / 2] = 0;
1807*3d9fd9fcSEd Maste         M2[i / 2] = m;
1808*3d9fd9fcSEd Maste       }
1809*3d9fd9fcSEd Maste     }
1810*3d9fd9fcSEd Maste     if (i < len) M2[i / 2] = M[i];
1811*3d9fd9fcSEd Maste     Decode(R2, S, M2, (len + 1) / 2);
1812*3d9fd9fcSEd Maste     for (i = 0; i < len - 1; i += 2) {
1813*3d9fd9fcSEd Maste       uint32_t r1, r = bottomr[i / 2];
1814*3d9fd9fcSEd Maste       uint16_t r0;
1815*3d9fd9fcSEd Maste       r += bottomt[i / 2] * R2[i / 2];
1816*3d9fd9fcSEd Maste       uint32_divmod_uint14(&r1, &r0, r, M[i]);
1817*3d9fd9fcSEd Maste       r1 = uint32_mod_uint14(r1, M[i + 1]);
1818*3d9fd9fcSEd Maste       *out++ = r0;
1819*3d9fd9fcSEd Maste       *out++ = r1;
1820*3d9fd9fcSEd Maste     }
1821*3d9fd9fcSEd Maste     if (i < len) *out++ = R2[i / 2];
1822*3d9fd9fcSEd Maste   }
182319261079SEd Maste }
182419261079SEd Maste 
R3_fromRq(small * out,const Fq * r)1825*3d9fd9fcSEd Maste static void R3_fromRq(small *out, const Fq *r) {
1826*3d9fd9fcSEd Maste   int i;
1827*3d9fd9fcSEd Maste   for (i = 0; i < p; ++i) out[i] = F3_freeze(r[i]);
182819261079SEd Maste }
182919261079SEd Maste 
R3_mult(small * h,const small * f,const small * g)1830*3d9fd9fcSEd Maste static void R3_mult(small *h, const small *f, const small *g) {
1831*3d9fd9fcSEd Maste   int16_t fg[p + p - 1];
1832*3d9fd9fcSEd Maste   int i, j;
1833*3d9fd9fcSEd Maste   for (i = 0; i < p + p - 1; ++i) fg[i] = 0;
1834*3d9fd9fcSEd Maste   for (i = 0; i < p; ++i)
1835*3d9fd9fcSEd Maste     for (j = 0; j < p; ++j) fg[i + j] += f[i] * (int16_t)g[j];
1836*3d9fd9fcSEd Maste   for (i = p; i < p + p - 1; ++i) fg[i - p] += fg[i];
1837*3d9fd9fcSEd Maste   for (i = p; i < p + p - 1; ++i) fg[i - p + 1] += fg[i];
1838*3d9fd9fcSEd Maste   for (i = 0; i < p; ++i) h[i] = F3_freeze(fg[i]);
183919261079SEd Maste }
184019261079SEd Maste 
R3_recip(small * out,const small * in)1841*3d9fd9fcSEd Maste static int R3_recip(small *out, const small *in) {
1842*3d9fd9fcSEd Maste   small f[p + 1], g[p + 1], v[p + 1], r[p + 1];
1843*3d9fd9fcSEd Maste   int sign, swap, t, i, loop, delta = 1;
1844*3d9fd9fcSEd Maste   for (i = 0; i < p + 1; ++i) v[i] = 0;
1845*3d9fd9fcSEd Maste   for (i = 0; i < p + 1; ++i) r[i] = 0;
1846*3d9fd9fcSEd Maste   r[0] = 1;
1847*3d9fd9fcSEd Maste   for (i = 0; i < p; ++i) f[i] = 0;
1848*3d9fd9fcSEd Maste   f[0] = 1;
1849*3d9fd9fcSEd Maste   f[p - 1] = f[p] = -1;
1850*3d9fd9fcSEd Maste   for (i = 0; i < p; ++i) g[p - 1 - i] = in[i];
1851*3d9fd9fcSEd Maste   g[p] = 0;
1852*3d9fd9fcSEd Maste   for (loop = 0; loop < 2 * p - 1; ++loop) {
1853*3d9fd9fcSEd Maste     for (i = p; i > 0; --i) v[i] = v[i - 1];
1854*3d9fd9fcSEd Maste     v[0] = 0;
1855*3d9fd9fcSEd Maste     sign = -g[0] * f[0];
1856*3d9fd9fcSEd Maste     swap = crypto_int16_negative_mask(-delta) & crypto_int16_nonzero_mask(g[0]);
1857*3d9fd9fcSEd Maste     delta ^= swap & (delta ^ -delta);
1858*3d9fd9fcSEd Maste     delta += 1;
1859*3d9fd9fcSEd Maste     for (i = 0; i < p + 1; ++i) {
1860*3d9fd9fcSEd Maste       t = swap & (f[i] ^ g[i]);
1861*3d9fd9fcSEd Maste       f[i] ^= t;
1862*3d9fd9fcSEd Maste       g[i] ^= t;
1863*3d9fd9fcSEd Maste       t = swap & (v[i] ^ r[i]);
1864*3d9fd9fcSEd Maste       v[i] ^= t;
1865*3d9fd9fcSEd Maste       r[i] ^= t;
1866*3d9fd9fcSEd Maste     }
1867*3d9fd9fcSEd Maste     for (i = 0; i < p + 1; ++i) g[i] = F3_freeze(g[i] + sign * f[i]);
1868*3d9fd9fcSEd Maste     for (i = 0; i < p + 1; ++i) r[i] = F3_freeze(r[i] + sign * v[i]);
1869*3d9fd9fcSEd Maste     for (i = 0; i < p; ++i) g[i] = g[i + 1];
1870*3d9fd9fcSEd Maste     g[p] = 0;
1871*3d9fd9fcSEd Maste   }
1872*3d9fd9fcSEd Maste   sign = f[0];
1873*3d9fd9fcSEd Maste   for (i = 0; i < p; ++i) out[i] = sign * v[p - 1 - i];
1874*3d9fd9fcSEd Maste   return crypto_int16_nonzero_mask(delta);
187519261079SEd Maste }
187619261079SEd Maste 
Rq_mult_small(Fq * h,const Fq * f,const small * g)1877*3d9fd9fcSEd Maste static void Rq_mult_small(Fq *h, const Fq *f, const small *g) {
1878*3d9fd9fcSEd Maste   int32_t fg[p + p - 1];
1879*3d9fd9fcSEd Maste   int i, j;
1880*3d9fd9fcSEd Maste   for (i = 0; i < p + p - 1; ++i) fg[i] = 0;
1881*3d9fd9fcSEd Maste   for (i = 0; i < p; ++i)
1882*3d9fd9fcSEd Maste     for (j = 0; j < p; ++j) fg[i + j] += f[i] * (int32_t)g[j];
1883*3d9fd9fcSEd Maste   for (i = p; i < p + p - 1; ++i) fg[i - p] += fg[i];
1884*3d9fd9fcSEd Maste   for (i = p; i < p + p - 1; ++i) fg[i - p + 1] += fg[i];
1885*3d9fd9fcSEd Maste   for (i = 0; i < p; ++i) h[i] = Fq_freeze(fg[i]);
1886*3d9fd9fcSEd Maste }
188719261079SEd Maste 
Rq_mult3(Fq * h,const Fq * f)1888*3d9fd9fcSEd Maste static void Rq_mult3(Fq *h, const Fq *f) {
1889*3d9fd9fcSEd Maste   int i;
1890*3d9fd9fcSEd Maste   for (i = 0; i < p; ++i) h[i] = Fq_freeze(3 * f[i]);
1891*3d9fd9fcSEd Maste }
1892*3d9fd9fcSEd Maste 
Fq_recip(Fq a1)1893*3d9fd9fcSEd Maste static Fq Fq_recip(Fq a1) {
189419261079SEd Maste   int i = 1;
189519261079SEd Maste   Fq ai = a1;
189619261079SEd Maste   while (i < q - 2) {
1897*3d9fd9fcSEd Maste     ai = Fq_freeze(a1 * (int32_t)ai);
189819261079SEd Maste     i += 1;
189919261079SEd Maste   }
190019261079SEd Maste   return ai;
190119261079SEd Maste }
190219261079SEd Maste 
Rq_recip3(Fq * out,const small * in)1903*3d9fd9fcSEd Maste static int Rq_recip3(Fq *out, const small *in) {
1904*3d9fd9fcSEd Maste   Fq f[p + 1], g[p + 1], v[p + 1], r[p + 1], scale;
1905*3d9fd9fcSEd Maste   int swap, t, i, loop, delta = 1;
1906*3d9fd9fcSEd Maste   int32_t f0, g0;
190719261079SEd Maste   for (i = 0; i < p + 1; ++i) v[i] = 0;
190819261079SEd Maste   for (i = 0; i < p + 1; ++i) r[i] = 0;
190919261079SEd Maste   r[0] = Fq_recip(3);
191019261079SEd Maste   for (i = 0; i < p; ++i) f[i] = 0;
1911*3d9fd9fcSEd Maste   f[0] = 1;
1912*3d9fd9fcSEd Maste   f[p - 1] = f[p] = -1;
191319261079SEd Maste   for (i = 0; i < p; ++i) g[p - 1 - i] = in[i];
191419261079SEd Maste   g[p] = 0;
191519261079SEd Maste   for (loop = 0; loop < 2 * p - 1; ++loop) {
191619261079SEd Maste     for (i = p; i > 0; --i) v[i] = v[i - 1];
191719261079SEd Maste     v[0] = 0;
1918*3d9fd9fcSEd Maste     swap = crypto_int16_negative_mask(-delta) & crypto_int16_nonzero_mask(g[0]);
191919261079SEd Maste     delta ^= swap & (delta ^ -delta);
192019261079SEd Maste     delta += 1;
192119261079SEd Maste     for (i = 0; i < p + 1; ++i) {
1922*3d9fd9fcSEd Maste       t = swap & (f[i] ^ g[i]);
1923*3d9fd9fcSEd Maste       f[i] ^= t;
1924*3d9fd9fcSEd Maste       g[i] ^= t;
1925*3d9fd9fcSEd Maste       t = swap & (v[i] ^ r[i]);
1926*3d9fd9fcSEd Maste       v[i] ^= t;
1927*3d9fd9fcSEd Maste       r[i] ^= t;
192819261079SEd Maste     }
192919261079SEd Maste     f0 = f[0];
193019261079SEd Maste     g0 = g[0];
193119261079SEd Maste     for (i = 0; i < p + 1; ++i) g[i] = Fq_freeze(f0 * g[i] - g0 * f[i]);
193219261079SEd Maste     for (i = 0; i < p + 1; ++i) r[i] = Fq_freeze(f0 * r[i] - g0 * v[i]);
193319261079SEd Maste     for (i = 0; i < p; ++i) g[i] = g[i + 1];
193419261079SEd Maste     g[p] = 0;
193519261079SEd Maste   }
193619261079SEd Maste   scale = Fq_recip(f[0]);
1937*3d9fd9fcSEd Maste   for (i = 0; i < p; ++i) out[i] = Fq_freeze(scale * (int32_t)v[p - 1 - i]);
1938*3d9fd9fcSEd Maste   return crypto_int16_nonzero_mask(delta);
193919261079SEd Maste }
194019261079SEd Maste 
Round(Fq * out,const Fq * a)1941*3d9fd9fcSEd Maste static void Round(Fq *out, const Fq *a) {
194219261079SEd Maste   int i;
194319261079SEd Maste   for (i = 0; i < p; ++i) out[i] = a[i] - F3_freeze(a[i]);
194419261079SEd Maste }
194519261079SEd Maste 
Short_fromlist(small * out,const uint32_t * in)1946*3d9fd9fcSEd Maste static void Short_fromlist(small *out, const uint32_t *in) {
1947*3d9fd9fcSEd Maste   uint32_t L[p];
194819261079SEd Maste   int i;
1949*3d9fd9fcSEd Maste   for (i = 0; i < w; ++i) L[i] = in[i] & (uint32_t)-2;
1950*3d9fd9fcSEd Maste   for (i = w; i < p; ++i) L[i] = (in[i] & (uint32_t)-3) | 1;
195119261079SEd Maste   crypto_sort_uint32(L, p);
195219261079SEd Maste   for (i = 0; i < p; ++i) out[i] = (L[i] & 3) - 1;
195319261079SEd Maste }
195419261079SEd Maste 
Hash_prefix(unsigned char * out,int b,const unsigned char * in,int inlen)1955*3d9fd9fcSEd Maste static void Hash_prefix(unsigned char *out, int b, const unsigned char *in, int inlen) {
1956*3d9fd9fcSEd Maste   unsigned char x[inlen + 1], h[64];
195719261079SEd Maste   int i;
195819261079SEd Maste   x[0] = b;
195919261079SEd Maste   for (i = 0; i < inlen; ++i) x[i + 1] = in[i];
196019261079SEd Maste   crypto_hash_sha512(h, x, inlen + 1);
196119261079SEd Maste   for (i = 0; i < 32; ++i) out[i] = h[i];
196219261079SEd Maste }
196319261079SEd Maste 
urandom32(void)1964*3d9fd9fcSEd Maste static uint32_t urandom32(void) {
196519261079SEd Maste   unsigned char c[4];
1966*3d9fd9fcSEd Maste   uint32_t result = 0;
1967*3d9fd9fcSEd Maste   int i;
196819261079SEd Maste   randombytes(c, 4);
1969*3d9fd9fcSEd Maste   for (i = 0; i < 4; ++i) result += ((uint32_t)c[i]) << (8 * i);
1970*3d9fd9fcSEd Maste   return result;
197119261079SEd Maste }
197219261079SEd Maste 
Short_random(small * out)1973*3d9fd9fcSEd Maste static void Short_random(small *out) {
1974*3d9fd9fcSEd Maste   uint32_t L[p];
197519261079SEd Maste   int i;
197619261079SEd Maste   for (i = 0; i < p; ++i) L[i] = urandom32();
197719261079SEd Maste   Short_fromlist(out, L);
197819261079SEd Maste }
197919261079SEd Maste 
Small_random(small * out)1980*3d9fd9fcSEd Maste static void Small_random(small *out) {
198119261079SEd Maste   int i;
198219261079SEd Maste   for (i = 0; i < p; ++i) out[i] = (((urandom32() & 0x3fffffff) * 3) >> 30) - 1;
198319261079SEd Maste }
198419261079SEd Maste 
KeyGen(Fq * h,small * f,small * ginv)1985*3d9fd9fcSEd Maste static void KeyGen(Fq *h, small *f, small *ginv) {
198619261079SEd Maste   small g[p];
198719261079SEd Maste   Fq finv[p];
198819261079SEd Maste   for (;;) {
1989*3d9fd9fcSEd Maste     int result;
199019261079SEd Maste     Small_random(g);
1991*3d9fd9fcSEd Maste     result = R3_recip(ginv, g);
1992*3d9fd9fcSEd Maste     crypto_declassify(&result, sizeof result);
1993*3d9fd9fcSEd Maste     if (result == 0) break;
199419261079SEd Maste   }
199519261079SEd Maste   Short_random(f);
1996*3d9fd9fcSEd Maste   Rq_recip3(finv, f);
199719261079SEd Maste   Rq_mult_small(h, finv, g);
199819261079SEd Maste }
199919261079SEd Maste 
Encrypt(Fq * c,const small * r,const Fq * h)2000*3d9fd9fcSEd Maste static void Encrypt(Fq *c, const small *r, const Fq *h) {
200119261079SEd Maste   Fq hr[p];
200219261079SEd Maste   Rq_mult_small(hr, h, r);
200319261079SEd Maste   Round(c, hr);
200419261079SEd Maste }
200519261079SEd Maste 
Decrypt(small * r,const Fq * c,const small * f,const small * ginv)2006*3d9fd9fcSEd Maste static void Decrypt(small *r, const Fq *c, const small *f, const small *ginv) {
2007*3d9fd9fcSEd Maste   Fq cf[p], cf3[p];
2008*3d9fd9fcSEd Maste   small e[p], ev[p];
2009*3d9fd9fcSEd Maste   int mask, i;
201019261079SEd Maste   Rq_mult_small(cf, c, f);
201119261079SEd Maste   Rq_mult3(cf3, cf);
201219261079SEd Maste   R3_fromRq(e, cf3);
201319261079SEd Maste   R3_mult(ev, e, ginv);
2014*3d9fd9fcSEd Maste   mask = Weightw_mask(ev);
201519261079SEd Maste   for (i = 0; i < w; ++i) r[i] = ((ev[i] ^ 1) & ~mask) ^ 1;
201619261079SEd Maste   for (i = w; i < p; ++i) r[i] = ev[i] & ~mask;
201719261079SEd Maste }
201819261079SEd Maste 
Small_encode(unsigned char * s,const small * f)2019*3d9fd9fcSEd Maste static void Small_encode(unsigned char *s, const small *f) {
2020*3d9fd9fcSEd Maste   int i, j;
202119261079SEd Maste   for (i = 0; i < p / 4; ++i) {
2022*3d9fd9fcSEd Maste     small x = 0;
2023*3d9fd9fcSEd Maste     for (j = 0;j < 4;++j) x += (*f++ + 1) << (2 * j);
202419261079SEd Maste     *s++ = x;
202519261079SEd Maste   }
2026*3d9fd9fcSEd Maste   *s = *f++ + 1;
202719261079SEd Maste }
202819261079SEd Maste 
Small_decode(small * f,const unsigned char * s)2029*3d9fd9fcSEd Maste static void Small_decode(small *f, const unsigned char *s) {
2030*3d9fd9fcSEd Maste   int i, j;
203119261079SEd Maste   for (i = 0; i < p / 4; ++i) {
2032*3d9fd9fcSEd Maste     unsigned char x = *s++;
2033*3d9fd9fcSEd Maste     for (j = 0;j < 4;++j) *f++ = ((small)((x >> (2 * j)) & 3)) - 1;
203419261079SEd Maste   }
2035*3d9fd9fcSEd Maste   *f++ = ((small)(*s & 3)) - 1;
203619261079SEd Maste }
203719261079SEd Maste 
Rq_encode(unsigned char * s,const Fq * r)2038*3d9fd9fcSEd Maste static void Rq_encode(unsigned char *s, const Fq *r) {
2039*3d9fd9fcSEd Maste   uint16_t R[p], M[p];
204019261079SEd Maste   int i;
204119261079SEd Maste   for (i = 0; i < p; ++i) R[i] = r[i] + q12;
204219261079SEd Maste   for (i = 0; i < p; ++i) M[i] = q;
204319261079SEd Maste   Encode(s, R, M, p);
204419261079SEd Maste }
204519261079SEd Maste 
Rq_decode(Fq * r,const unsigned char * s)2046*3d9fd9fcSEd Maste static void Rq_decode(Fq *r, const unsigned char *s) {
2047*3d9fd9fcSEd Maste   uint16_t R[p], M[p];
204819261079SEd Maste   int i;
204919261079SEd Maste   for (i = 0; i < p; ++i) M[i] = q;
205019261079SEd Maste   Decode(R, s, M, p);
205119261079SEd Maste   for (i = 0; i < p; ++i) r[i] = ((Fq)R[i]) - q12;
205219261079SEd Maste }
205319261079SEd Maste 
Rounded_encode(unsigned char * s,const Fq * r)2054*3d9fd9fcSEd Maste static void Rounded_encode(unsigned char *s, const Fq *r) {
2055*3d9fd9fcSEd Maste   uint16_t R[p], M[p];
205619261079SEd Maste   int i;
205719261079SEd Maste   for (i = 0; i < p; ++i) R[i] = ((r[i] + q12) * 10923) >> 15;
205819261079SEd Maste   for (i = 0; i < p; ++i) M[i] = (q + 2) / 3;
205919261079SEd Maste   Encode(s, R, M, p);
206019261079SEd Maste }
206119261079SEd Maste 
Rounded_decode(Fq * r,const unsigned char * s)2062*3d9fd9fcSEd Maste static void Rounded_decode(Fq *r, const unsigned char *s) {
2063*3d9fd9fcSEd Maste   uint16_t R[p], M[p];
206419261079SEd Maste   int i;
206519261079SEd Maste   for (i = 0; i < p; ++i) M[i] = (q + 2) / 3;
206619261079SEd Maste   Decode(R, s, M, p);
206719261079SEd Maste   for (i = 0; i < p; ++i) r[i] = R[i] * 3 - q12;
206819261079SEd Maste }
206919261079SEd Maste 
ZKeyGen(unsigned char * pk,unsigned char * sk)2070*3d9fd9fcSEd Maste static void ZKeyGen(unsigned char *pk, unsigned char *sk) {
207119261079SEd Maste   Fq h[p];
207219261079SEd Maste   small f[p], v[p];
207319261079SEd Maste   KeyGen(h, f, v);
207419261079SEd Maste   Rq_encode(pk, h);
2075*3d9fd9fcSEd Maste   Small_encode(sk, f);
2076*3d9fd9fcSEd Maste   Small_encode(sk + Small_bytes, v);
207719261079SEd Maste }
207819261079SEd Maste 
ZEncrypt(unsigned char * C,const Inputs r,const unsigned char * pk)2079*3d9fd9fcSEd Maste static void ZEncrypt(unsigned char *C, const Inputs r, const unsigned char *pk) {
2080*3d9fd9fcSEd Maste   Fq h[p], c[p];
208119261079SEd Maste   Rq_decode(h, pk);
208219261079SEd Maste   Encrypt(c, r, h);
208319261079SEd Maste   Rounded_encode(C, c);
208419261079SEd Maste }
208519261079SEd Maste 
ZDecrypt(Inputs r,const unsigned char * C,const unsigned char * sk)2086*3d9fd9fcSEd Maste static void ZDecrypt(Inputs r, const unsigned char *C, const unsigned char *sk) {
208719261079SEd Maste   small f[p], v[p];
208819261079SEd Maste   Fq c[p];
2089*3d9fd9fcSEd Maste   Small_decode(f, sk);
2090*3d9fd9fcSEd Maste   Small_decode(v, sk + Small_bytes);
209119261079SEd Maste   Rounded_decode(c, C);
209219261079SEd Maste   Decrypt(r, c, f, v);
209319261079SEd Maste }
209419261079SEd Maste 
HashConfirm(unsigned char * h,const unsigned char * r,const unsigned char * cache)2095*3d9fd9fcSEd Maste static void HashConfirm(unsigned char *h, const unsigned char *r, const unsigned char *cache) {
209619261079SEd Maste   unsigned char x[Hash_bytes * 2];
209719261079SEd Maste   int i;
2098*3d9fd9fcSEd Maste   Hash_prefix(x, 3, r, Small_bytes);
209919261079SEd Maste   for (i = 0; i < Hash_bytes; ++i) x[Hash_bytes + i] = cache[i];
210019261079SEd Maste   Hash_prefix(h, 2, x, sizeof x);
210119261079SEd Maste }
210219261079SEd Maste 
HashSession(unsigned char * k,int b,const unsigned char * y,const unsigned char * z)2103*3d9fd9fcSEd Maste static void HashSession(unsigned char *k, int b, const unsigned char *y, const unsigned char *z) {
2104*3d9fd9fcSEd Maste   unsigned char x[Hash_bytes + crypto_kem_sntrup761_CIPHERTEXTBYTES];
210519261079SEd Maste   int i;
2106*3d9fd9fcSEd Maste   Hash_prefix(x, 3, y, Small_bytes);
2107*3d9fd9fcSEd Maste   for (i = 0; i < crypto_kem_sntrup761_CIPHERTEXTBYTES; ++i) x[Hash_bytes + i] = z[i];
210819261079SEd Maste   Hash_prefix(k, b, x, sizeof x);
210919261079SEd Maste }
211019261079SEd Maste 
crypto_kem_sntrup761_keypair(unsigned char * pk,unsigned char * sk)2111*3d9fd9fcSEd Maste int crypto_kem_sntrup761_keypair(unsigned char *pk, unsigned char *sk) {
211219261079SEd Maste   int i;
2113*3d9fd9fcSEd Maste   ZKeyGen(pk, sk);
2114*3d9fd9fcSEd Maste   sk += SecretKeys_bytes;
2115*3d9fd9fcSEd Maste   for (i = 0; i < crypto_kem_sntrup761_PUBLICKEYBYTES; ++i) *sk++ = pk[i];
2116*3d9fd9fcSEd Maste   randombytes(sk, Small_bytes);
2117*3d9fd9fcSEd Maste   Hash_prefix(sk + Small_bytes, 4, pk, crypto_kem_sntrup761_PUBLICKEYBYTES);
2118*3d9fd9fcSEd Maste   return 0;
211919261079SEd Maste }
212019261079SEd Maste 
Hide(unsigned char * c,unsigned char * r_enc,const Inputs r,const unsigned char * pk,const unsigned char * cache)2121*3d9fd9fcSEd Maste static void Hide(unsigned char *c, unsigned char *r_enc, const Inputs r, const unsigned char *pk, const unsigned char *cache) {
2122*3d9fd9fcSEd Maste   Small_encode(r_enc, r);
2123*3d9fd9fcSEd Maste   ZEncrypt(c, r, pk);
2124*3d9fd9fcSEd Maste   HashConfirm(c + crypto_kem_sntrup761_CIPHERTEXTBYTES - Confirm_bytes, r_enc, cache);
212519261079SEd Maste }
212619261079SEd Maste 
crypto_kem_sntrup761_enc(unsigned char * c,unsigned char * k,const unsigned char * pk)2127*3d9fd9fcSEd Maste int crypto_kem_sntrup761_enc(unsigned char *c, unsigned char *k, const unsigned char *pk) {
212819261079SEd Maste   Inputs r;
2129*3d9fd9fcSEd Maste   unsigned char r_enc[Small_bytes], cache[Hash_bytes];
2130*3d9fd9fcSEd Maste   Hash_prefix(cache, 4, pk, crypto_kem_sntrup761_PUBLICKEYBYTES);
2131*3d9fd9fcSEd Maste   Short_random(r);
213219261079SEd Maste   Hide(c, r_enc, r, pk, cache);
213319261079SEd Maste   HashSession(k, 1, r_enc, c);
2134*3d9fd9fcSEd Maste   return 0;
213519261079SEd Maste }
213619261079SEd Maste 
Ciphertexts_diff_mask(const unsigned char * c,const unsigned char * c2)2137*3d9fd9fcSEd Maste static int Ciphertexts_diff_mask(const unsigned char *c, const unsigned char *c2) {
2138*3d9fd9fcSEd Maste   uint16_t differentbits = 0;
2139*3d9fd9fcSEd Maste   int len = crypto_kem_sntrup761_CIPHERTEXTBYTES;
214019261079SEd Maste   while (len-- > 0) differentbits |= (*c++) ^ (*c2++);
2141*3d9fd9fcSEd Maste   return (crypto_int64_bitmod_01((differentbits - 1),8)) - 1;
214219261079SEd Maste }
214319261079SEd Maste 
crypto_kem_sntrup761_dec(unsigned char * k,const unsigned char * c,const unsigned char * sk)2144*3d9fd9fcSEd Maste int crypto_kem_sntrup761_dec(unsigned char *k, const unsigned char *c, const unsigned char *sk) {
214519261079SEd Maste   const unsigned char *pk = sk + SecretKeys_bytes;
2146*3d9fd9fcSEd Maste   const unsigned char *rho = pk + crypto_kem_sntrup761_PUBLICKEYBYTES;
2147*3d9fd9fcSEd Maste   const unsigned char *cache = rho + Small_bytes;
214819261079SEd Maste   Inputs r;
2149*3d9fd9fcSEd Maste   unsigned char r_enc[Small_bytes], cnew[crypto_kem_sntrup761_CIPHERTEXTBYTES];
2150*3d9fd9fcSEd Maste   int mask, i;
215119261079SEd Maste   ZDecrypt(r, c, sk);
215219261079SEd Maste   Hide(cnew, r_enc, r, pk, cache);
215319261079SEd Maste   mask = Ciphertexts_diff_mask(c, cnew);
2154*3d9fd9fcSEd Maste   for (i = 0; i < Small_bytes; ++i) r_enc[i] ^= mask & (r_enc[i] ^ rho[i]);
215519261079SEd Maste   HashSession(k, 1 + mask, r_enc, c);
215619261079SEd Maste   return 0;
215719261079SEd Maste }
215819261079SEd Maste 
215919261079SEd Maste #endif /* USE_SNTRUP761X25519 */
2160