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