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