/rust/registry/src/index.crates.io-6f17d22bba15001f/ring-0.17.8/crypto/curve25519/curve25519.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (c) 2020, Google Inc. |
2 | | * |
3 | | * Permission to use, copy, modify, and/or distribute this software for any |
4 | | * purpose with or without fee is hereby granted, provided that the above |
5 | | * copyright notice and this permission notice appear in all copies. |
6 | | * |
7 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
8 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
9 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
10 | | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
11 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
12 | | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
13 | | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
14 | | |
15 | | // Some of this code is taken from the ref10 version of Ed25519 in SUPERCOP |
16 | | // 20141124 (http://bench.cr.yp.to/supercop.html). That code is released as |
17 | | // public domain. Other parts have been replaced to call into code generated by |
18 | | // Fiat (https://github.com/mit-plv/fiat-crypto) in //third_party/fiat. |
19 | | // |
20 | | // The field functions are shared by Ed25519 and X25519 where possible. |
21 | | |
22 | | #include <ring-core/mem.h> |
23 | | |
24 | | #include "internal.h" |
25 | | #include "../internal.h" |
26 | | |
27 | | #if defined(_MSC_VER) && !defined(__clang__) |
28 | | // '=': conversion from 'int64_t' to 'int32_t', possible loss of data |
29 | | #pragma warning(disable: 4242) |
30 | | // '=': conversion from 'int32_t' to 'uint8_t', possible loss of data |
31 | | #pragma warning(disable: 4244) |
32 | | #endif |
33 | | |
34 | | #if defined(__GNUC__) || defined(__clang__) |
35 | | #pragma GCC diagnostic ignored "-Wconversion" |
36 | | #pragma GCC diagnostic ignored "-Wsign-conversion" |
37 | | #endif |
38 | | |
39 | | #if defined(__GNUC__) && !defined(__clang__) |
40 | | #pragma GCC diagnostic ignored "-Winline" |
41 | | #endif |
42 | | |
43 | | // Various pre-computed constants. |
44 | | #include "./curve25519_tables.h" |
45 | | |
46 | | #if defined(BORINGSSL_HAS_UINT128) |
47 | | #if defined(__GNUC__) |
48 | | #pragma GCC diagnostic ignored "-Wpedantic" |
49 | | #endif |
50 | | #include "../../third_party/fiat/curve25519_64.h" |
51 | | #elif defined(OPENSSL_64_BIT) |
52 | | #include "../../third_party/fiat/curve25519_64_msvc.h" |
53 | | #else |
54 | | #include "../../third_party/fiat/curve25519_32.h" |
55 | | #endif |
56 | | |
57 | | |
58 | | // Low-level intrinsic operations |
59 | | |
60 | 0 | static uint64_t load_3(const uint8_t *in) { |
61 | 0 | uint64_t result; |
62 | 0 | result = (uint64_t)in[0]; |
63 | 0 | result |= ((uint64_t)in[1]) << 8; |
64 | 0 | result |= ((uint64_t)in[2]) << 16; |
65 | 0 | return result; |
66 | 0 | } |
67 | | |
68 | 0 | static uint64_t load_4(const uint8_t *in) { |
69 | 0 | uint64_t result; |
70 | 0 | result = (uint64_t)in[0]; |
71 | 0 | result |= ((uint64_t)in[1]) << 8; |
72 | 0 | result |= ((uint64_t)in[2]) << 16; |
73 | 0 | result |= ((uint64_t)in[3]) << 24; |
74 | 0 | return result; |
75 | 0 | } |
76 | | |
77 | | |
78 | | // Field operations. |
79 | | |
80 | | #if defined(OPENSSL_64_BIT) |
81 | | |
82 | | // assert_fe asserts that |f| satisfies bounds: |
83 | | // |
84 | | // [[0x0 ~> 0x8cccccccccccc], |
85 | | // [0x0 ~> 0x8cccccccccccc], |
86 | | // [0x0 ~> 0x8cccccccccccc], |
87 | | // [0x0 ~> 0x8cccccccccccc], |
88 | | // [0x0 ~> 0x8cccccccccccc]] |
89 | | // |
90 | | // See comments in curve25519_64.h for which functions use these bounds for |
91 | | // inputs or outputs. |
92 | | #define assert_fe(f) \ |
93 | 0 | do { \ |
94 | 0 | for (unsigned _assert_fe_i = 0; _assert_fe_i < 5; _assert_fe_i++) { \ |
95 | 0 | dev_assert_secret(f[_assert_fe_i] <= UINT64_C(0x8cccccccccccc)); \ |
96 | 0 | } \ |
97 | 0 | } while (0) |
98 | | |
99 | | // assert_fe_loose asserts that |f| satisfies bounds: |
100 | | // |
101 | | // [[0x0 ~> 0x1a666666666664], |
102 | | // [0x0 ~> 0x1a666666666664], |
103 | | // [0x0 ~> 0x1a666666666664], |
104 | | // [0x0 ~> 0x1a666666666664], |
105 | | // [0x0 ~> 0x1a666666666664]] |
106 | | // |
107 | | // See comments in curve25519_64.h for which functions use these bounds for |
108 | | // inputs or outputs. |
109 | | #define assert_fe_loose(f) \ |
110 | 0 | do { \ |
111 | 0 | for (unsigned _assert_fe_i = 0; _assert_fe_i < 5; _assert_fe_i++) { \ |
112 | 0 | dev_assert_secret(f[_assert_fe_i] <= UINT64_C(0x1a666666666664)); \ |
113 | 0 | } \ |
114 | 0 | } while (0) |
115 | | |
116 | | #else |
117 | | |
118 | | // assert_fe asserts that |f| satisfies bounds: |
119 | | // |
120 | | // [[0x0 ~> 0x4666666], [0x0 ~> 0x2333333], |
121 | | // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333], |
122 | | // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333], |
123 | | // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333], |
124 | | // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333]] |
125 | | // |
126 | | // See comments in curve25519_32.h for which functions use these bounds for |
127 | | // inputs or outputs. |
128 | | #define assert_fe(f) \ |
129 | | do { \ |
130 | | for (unsigned _assert_fe_i = 0; _assert_fe_i < 10; _assert_fe_i++) { \ |
131 | | dev_assert_secret(f[_assert_fe_i] <= \ |
132 | | ((_assert_fe_i & 1) ? 0x2333333u : 0x4666666u)); \ |
133 | | } \ |
134 | | } while (0) |
135 | | |
136 | | // assert_fe_loose asserts that |f| satisfies bounds: |
137 | | // |
138 | | // [[0x0 ~> 0xd333332], [0x0 ~> 0x6999999], |
139 | | // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999], |
140 | | // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999], |
141 | | // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999], |
142 | | // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999]] |
143 | | // |
144 | | // See comments in curve25519_32.h for which functions use these bounds for |
145 | | // inputs or outputs. |
146 | | #define assert_fe_loose(f) \ |
147 | | do { \ |
148 | | for (unsigned _assert_fe_i = 0; _assert_fe_i < 10; _assert_fe_i++) { \ |
149 | | dev_assert_secret(f[_assert_fe_i] <= \ |
150 | | ((_assert_fe_i & 1) ? 0x6999999u : 0xd333332u)); \ |
151 | | } \ |
152 | | } while (0) |
153 | | |
154 | | #endif // OPENSSL_64_BIT |
155 | | |
156 | | OPENSSL_STATIC_ASSERT(sizeof(fe) == sizeof(fe_limb_t) * FE_NUM_LIMBS, |
157 | | "fe_limb_t[FE_NUM_LIMBS] is inconsistent with fe"); |
158 | | |
159 | 0 | static void fe_frombytes_strict(fe *h, const uint8_t s[32]) { |
160 | | // |fiat_25519_from_bytes| requires the top-most bit be clear. |
161 | 0 | dev_assert_secret((s[31] & 0x80) == 0); |
162 | 0 | fiat_25519_from_bytes(h->v, s); |
163 | 0 | assert_fe(h->v); |
164 | 0 | } |
165 | | |
166 | 0 | static void fe_frombytes(fe *h, const uint8_t s[32]) { |
167 | 0 | uint8_t s_copy[32]; |
168 | 0 | OPENSSL_memcpy(s_copy, s, 32); |
169 | 0 | s_copy[31] &= 0x7f; |
170 | 0 | fe_frombytes_strict(h, s_copy); |
171 | 0 | } |
172 | | |
173 | 0 | static void fe_tobytes(uint8_t s[32], const fe *f) { |
174 | 0 | assert_fe(f->v); |
175 | 0 | fiat_25519_to_bytes(s, f->v); |
176 | 0 | } |
177 | | |
178 | | // h = 0 |
179 | 0 | static void fe_0(fe *h) { |
180 | 0 | OPENSSL_memset(h, 0, sizeof(fe)); |
181 | 0 | } |
182 | | |
183 | | #if defined(OPENSSL_SMALL) |
184 | | |
185 | | static void fe_loose_0(fe_loose *h) { |
186 | | OPENSSL_memset(h, 0, sizeof(fe_loose)); |
187 | | } |
188 | | |
189 | | #endif |
190 | | |
191 | | // h = 1 |
192 | 0 | static void fe_1(fe *h) { |
193 | 0 | OPENSSL_memset(h, 0, sizeof(fe)); |
194 | 0 | h->v[0] = 1; |
195 | 0 | } |
196 | | |
197 | | #if defined(OPENSSL_SMALL) |
198 | | |
199 | | static void fe_loose_1(fe_loose *h) { |
200 | | OPENSSL_memset(h, 0, sizeof(fe_loose)); |
201 | | h->v[0] = 1; |
202 | | } |
203 | | |
204 | | #endif |
205 | | |
206 | | // h = f + g |
207 | | // Can overlap h with f or g. |
208 | 0 | static void fe_add(fe_loose *h, const fe *f, const fe *g) { |
209 | 0 | assert_fe(f->v); |
210 | 0 | assert_fe(g->v); |
211 | 0 | fiat_25519_add(h->v, f->v, g->v); |
212 | 0 | assert_fe_loose(h->v); |
213 | 0 | } |
214 | | |
215 | | // h = f - g |
216 | | // Can overlap h with f or g. |
217 | 0 | static void fe_sub(fe_loose *h, const fe *f, const fe *g) { |
218 | 0 | assert_fe(f->v); |
219 | 0 | assert_fe(g->v); |
220 | 0 | fiat_25519_sub(h->v, f->v, g->v); |
221 | 0 | assert_fe_loose(h->v); |
222 | 0 | } |
223 | | |
224 | 0 | static void fe_carry(fe *h, const fe_loose* f) { |
225 | 0 | assert_fe_loose(f->v); |
226 | 0 | fiat_25519_carry(h->v, f->v); |
227 | 0 | assert_fe(h->v); |
228 | 0 | } |
229 | | |
230 | | static void fe_mul_impl(fe_limb_t out[FE_NUM_LIMBS], |
231 | | const fe_limb_t in1[FE_NUM_LIMBS], |
232 | 0 | const fe_limb_t in2[FE_NUM_LIMBS]) { |
233 | 0 | assert_fe_loose(in1); |
234 | 0 | assert_fe_loose(in2); |
235 | 0 | fiat_25519_carry_mul(out, in1, in2); |
236 | 0 | assert_fe(out); |
237 | 0 | } |
238 | | |
239 | 0 | static void fe_mul_ltt(fe_loose *h, const fe *f, const fe *g) { |
240 | 0 | fe_mul_impl(h->v, f->v, g->v); |
241 | 0 | } |
242 | | |
243 | | #if defined(OPENSSL_SMALL) |
244 | | static void fe_mul_llt(fe_loose *h, const fe_loose *f, const fe *g) { |
245 | | fe_mul_impl(h->v, f->v, g->v); |
246 | | } |
247 | | #endif |
248 | | |
249 | 0 | static void fe_mul_ttt(fe *h, const fe *f, const fe *g) { |
250 | 0 | fe_mul_impl(h->v, f->v, g->v); |
251 | 0 | } |
252 | | |
253 | 0 | static void fe_mul_tlt(fe *h, const fe_loose *f, const fe *g) { |
254 | 0 | fe_mul_impl(h->v, f->v, g->v); |
255 | 0 | } |
256 | | |
257 | 0 | static void fe_mul_ttl(fe *h, const fe *f, const fe_loose *g) { |
258 | 0 | fe_mul_impl(h->v, f->v, g->v); |
259 | 0 | } |
260 | | |
261 | 0 | static void fe_mul_tll(fe *h, const fe_loose *f, const fe_loose *g) { |
262 | 0 | fe_mul_impl(h->v, f->v, g->v); |
263 | 0 | } |
264 | | |
265 | 0 | static void fe_sq_tl(fe *h, const fe_loose *f) { |
266 | 0 | assert_fe_loose(f->v); |
267 | 0 | fiat_25519_carry_square(h->v, f->v); |
268 | 0 | assert_fe(h->v); |
269 | 0 | } |
270 | | |
271 | 0 | static void fe_sq_tt(fe *h, const fe *f) { |
272 | 0 | assert_fe_loose(f->v); |
273 | 0 | fiat_25519_carry_square(h->v, f->v); |
274 | 0 | assert_fe(h->v); |
275 | 0 | } |
276 | | |
277 | | // Replace (f,g) with (g,f) if b == 1; |
278 | | // replace (f,g) with (f,g) if b == 0. |
279 | | // |
280 | | // Preconditions: b in {0,1}. |
281 | 0 | static void fe_cswap(fe *f, fe *g, fe_limb_t b) { |
282 | 0 | b = 0-b; |
283 | 0 | for (unsigned i = 0; i < FE_NUM_LIMBS; i++) { |
284 | 0 | fe_limb_t x = f->v[i] ^ g->v[i]; |
285 | 0 | x &= b; |
286 | 0 | f->v[i] ^= x; |
287 | 0 | g->v[i] ^= x; |
288 | 0 | } |
289 | 0 | } |
290 | | |
291 | 0 | static void fe_mul121666(fe *h, const fe_loose *f) { |
292 | 0 | assert_fe_loose(f->v); |
293 | 0 | fiat_25519_carry_scmul_121666(h->v, f->v); |
294 | 0 | assert_fe(h->v); |
295 | 0 | } |
296 | | |
297 | | // h = -f |
298 | 0 | static void fe_neg(fe_loose *h, const fe *f) { |
299 | 0 | assert_fe(f->v); |
300 | 0 | fiat_25519_opp(h->v, f->v); |
301 | 0 | assert_fe_loose(h->v); |
302 | 0 | } |
303 | | |
304 | | // Replace (f,g) with (g,g) if b == 1; |
305 | | // replace (f,g) with (f,g) if b == 0. |
306 | | // |
307 | | // Preconditions: b in {0,1}. |
308 | 0 | static void fe_cmov(fe_loose *f, const fe_loose *g, fe_limb_t b) { |
309 | | // Silence an unused function warning. |fiat_25519_selectznz| isn't quite the |
310 | | // calling convention the rest of this code wants, so implement it by hand. |
311 | | // |
312 | | // TODO(davidben): Switch to fiat's calling convention, or ask fiat to emit a |
313 | | // different one. |
314 | 0 | (void)fiat_25519_selectznz; |
315 | |
|
316 | 0 | b = 0-b; |
317 | 0 | for (unsigned i = 0; i < FE_NUM_LIMBS; i++) { |
318 | 0 | fe_limb_t x = f->v[i] ^ g->v[i]; |
319 | 0 | x &= b; |
320 | 0 | f->v[i] ^= x; |
321 | 0 | } |
322 | 0 | } |
323 | | |
324 | | // h = f |
325 | 0 | static void fe_copy(fe *h, const fe *f) { |
326 | 0 | fe_limbs_copy(h->v, f->v); |
327 | 0 | } |
328 | | |
329 | 0 | static void fe_copy_lt(fe_loose *h, const fe *f) { |
330 | 0 | OPENSSL_STATIC_ASSERT(sizeof(fe_loose) == sizeof(fe), "fe and fe_loose mismatch"); |
331 | 0 | fe_limbs_copy(h->v, f->v); |
332 | 0 | } |
333 | | |
334 | 0 | static void fe_loose_invert(fe *out, const fe_loose *z) { |
335 | 0 | fe t0; |
336 | 0 | fe t1; |
337 | 0 | fe t2; |
338 | 0 | fe t3; |
339 | 0 | int i; |
340 | |
|
341 | 0 | fe_sq_tl(&t0, z); |
342 | 0 | fe_sq_tt(&t1, &t0); |
343 | 0 | for (i = 1; i < 2; ++i) { |
344 | 0 | fe_sq_tt(&t1, &t1); |
345 | 0 | } |
346 | 0 | fe_mul_tlt(&t1, z, &t1); |
347 | 0 | fe_mul_ttt(&t0, &t0, &t1); |
348 | 0 | fe_sq_tt(&t2, &t0); |
349 | 0 | fe_mul_ttt(&t1, &t1, &t2); |
350 | 0 | fe_sq_tt(&t2, &t1); |
351 | 0 | for (i = 1; i < 5; ++i) { |
352 | 0 | fe_sq_tt(&t2, &t2); |
353 | 0 | } |
354 | 0 | fe_mul_ttt(&t1, &t2, &t1); |
355 | 0 | fe_sq_tt(&t2, &t1); |
356 | 0 | for (i = 1; i < 10; ++i) { |
357 | 0 | fe_sq_tt(&t2, &t2); |
358 | 0 | } |
359 | 0 | fe_mul_ttt(&t2, &t2, &t1); |
360 | 0 | fe_sq_tt(&t3, &t2); |
361 | 0 | for (i = 1; i < 20; ++i) { |
362 | 0 | fe_sq_tt(&t3, &t3); |
363 | 0 | } |
364 | 0 | fe_mul_ttt(&t2, &t3, &t2); |
365 | 0 | fe_sq_tt(&t2, &t2); |
366 | 0 | for (i = 1; i < 10; ++i) { |
367 | 0 | fe_sq_tt(&t2, &t2); |
368 | 0 | } |
369 | 0 | fe_mul_ttt(&t1, &t2, &t1); |
370 | 0 | fe_sq_tt(&t2, &t1); |
371 | 0 | for (i = 1; i < 50; ++i) { |
372 | 0 | fe_sq_tt(&t2, &t2); |
373 | 0 | } |
374 | 0 | fe_mul_ttt(&t2, &t2, &t1); |
375 | 0 | fe_sq_tt(&t3, &t2); |
376 | 0 | for (i = 1; i < 100; ++i) { |
377 | 0 | fe_sq_tt(&t3, &t3); |
378 | 0 | } |
379 | 0 | fe_mul_ttt(&t2, &t3, &t2); |
380 | 0 | fe_sq_tt(&t2, &t2); |
381 | 0 | for (i = 1; i < 50; ++i) { |
382 | 0 | fe_sq_tt(&t2, &t2); |
383 | 0 | } |
384 | 0 | fe_mul_ttt(&t1, &t2, &t1); |
385 | 0 | fe_sq_tt(&t1, &t1); |
386 | 0 | for (i = 1; i < 5; ++i) { |
387 | 0 | fe_sq_tt(&t1, &t1); |
388 | 0 | } |
389 | 0 | fe_mul_ttt(out, &t1, &t0); |
390 | 0 | } |
391 | | |
392 | 0 | static void fe_invert(fe *out, const fe *z) { |
393 | 0 | fe_loose l; |
394 | 0 | fe_copy_lt(&l, z); |
395 | 0 | fe_loose_invert(out, &l); |
396 | 0 | } |
397 | | |
398 | | // return 0 if f == 0 |
399 | | // return 1 if f != 0 |
400 | 0 | static int fe_isnonzero(const fe_loose *f) { |
401 | 0 | fe tight; |
402 | 0 | fe_carry(&tight, f); |
403 | 0 | uint8_t s[32]; |
404 | 0 | fe_tobytes(s, &tight); |
405 | |
|
406 | 0 | static const uint8_t zero[32] = {0}; |
407 | 0 | return CRYPTO_memcmp(s, zero, sizeof(zero)) != 0; |
408 | 0 | } |
409 | | |
410 | | // return 1 if f is in {1,3,5,...,q-2} |
411 | | // return 0 if f is in {0,2,4,...,q-1} |
412 | 0 | static int fe_isnegative(const fe *f) { |
413 | 0 | uint8_t s[32]; |
414 | 0 | fe_tobytes(s, f); |
415 | 0 | return s[0] & 1; |
416 | 0 | } |
417 | | |
418 | 0 | static void fe_sq2_tt(fe *h, const fe *f) { |
419 | | // h = f^2 |
420 | 0 | fe_sq_tt(h, f); |
421 | | |
422 | | // h = h + h |
423 | 0 | fe_loose tmp; |
424 | 0 | fe_add(&tmp, h, h); |
425 | 0 | fe_carry(h, &tmp); |
426 | 0 | } |
427 | | |
428 | 0 | static void fe_pow22523(fe *out, const fe *z) { |
429 | 0 | fe t0; |
430 | 0 | fe t1; |
431 | 0 | fe t2; |
432 | 0 | int i; |
433 | |
|
434 | 0 | fe_sq_tt(&t0, z); |
435 | 0 | fe_sq_tt(&t1, &t0); |
436 | 0 | for (i = 1; i < 2; ++i) { |
437 | 0 | fe_sq_tt(&t1, &t1); |
438 | 0 | } |
439 | 0 | fe_mul_ttt(&t1, z, &t1); |
440 | 0 | fe_mul_ttt(&t0, &t0, &t1); |
441 | 0 | fe_sq_tt(&t0, &t0); |
442 | 0 | fe_mul_ttt(&t0, &t1, &t0); |
443 | 0 | fe_sq_tt(&t1, &t0); |
444 | 0 | for (i = 1; i < 5; ++i) { |
445 | 0 | fe_sq_tt(&t1, &t1); |
446 | 0 | } |
447 | 0 | fe_mul_ttt(&t0, &t1, &t0); |
448 | 0 | fe_sq_tt(&t1, &t0); |
449 | 0 | for (i = 1; i < 10; ++i) { |
450 | 0 | fe_sq_tt(&t1, &t1); |
451 | 0 | } |
452 | 0 | fe_mul_ttt(&t1, &t1, &t0); |
453 | 0 | fe_sq_tt(&t2, &t1); |
454 | 0 | for (i = 1; i < 20; ++i) { |
455 | 0 | fe_sq_tt(&t2, &t2); |
456 | 0 | } |
457 | 0 | fe_mul_ttt(&t1, &t2, &t1); |
458 | 0 | fe_sq_tt(&t1, &t1); |
459 | 0 | for (i = 1; i < 10; ++i) { |
460 | 0 | fe_sq_tt(&t1, &t1); |
461 | 0 | } |
462 | 0 | fe_mul_ttt(&t0, &t1, &t0); |
463 | 0 | fe_sq_tt(&t1, &t0); |
464 | 0 | for (i = 1; i < 50; ++i) { |
465 | 0 | fe_sq_tt(&t1, &t1); |
466 | 0 | } |
467 | 0 | fe_mul_ttt(&t1, &t1, &t0); |
468 | 0 | fe_sq_tt(&t2, &t1); |
469 | 0 | for (i = 1; i < 100; ++i) { |
470 | 0 | fe_sq_tt(&t2, &t2); |
471 | 0 | } |
472 | 0 | fe_mul_ttt(&t1, &t2, &t1); |
473 | 0 | fe_sq_tt(&t1, &t1); |
474 | 0 | for (i = 1; i < 50; ++i) { |
475 | 0 | fe_sq_tt(&t1, &t1); |
476 | 0 | } |
477 | 0 | fe_mul_ttt(&t0, &t1, &t0); |
478 | 0 | fe_sq_tt(&t0, &t0); |
479 | 0 | for (i = 1; i < 2; ++i) { |
480 | 0 | fe_sq_tt(&t0, &t0); |
481 | 0 | } |
482 | 0 | fe_mul_ttt(out, &t0, z); |
483 | 0 | } |
484 | | |
485 | | |
486 | | // Group operations. |
487 | | |
488 | 0 | int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t s[32]) { |
489 | 0 | fe u; |
490 | 0 | fe_loose v; |
491 | 0 | fe w; |
492 | 0 | fe vxx; |
493 | 0 | fe_loose check; |
494 | |
|
495 | 0 | fe_frombytes(&h->Y, s); |
496 | 0 | fe_1(&h->Z); |
497 | 0 | fe_sq_tt(&w, &h->Y); |
498 | 0 | fe_mul_ttt(&vxx, &w, &d); |
499 | 0 | fe_sub(&v, &w, &h->Z); // u = y^2-1 |
500 | 0 | fe_carry(&u, &v); |
501 | 0 | fe_add(&v, &vxx, &h->Z); // v = dy^2+1 |
502 | |
|
503 | 0 | fe_mul_ttl(&w, &u, &v); // w = u*v |
504 | 0 | fe_pow22523(&h->X, &w); // x = w^((q-5)/8) |
505 | 0 | fe_mul_ttt(&h->X, &h->X, &u); // x = u*w^((q-5)/8) |
506 | |
|
507 | 0 | fe_sq_tt(&vxx, &h->X); |
508 | 0 | fe_mul_ttl(&vxx, &vxx, &v); |
509 | 0 | fe_sub(&check, &vxx, &u); |
510 | 0 | if (fe_isnonzero(&check)) { |
511 | 0 | fe_add(&check, &vxx, &u); |
512 | 0 | if (fe_isnonzero(&check)) { |
513 | 0 | return 0; |
514 | 0 | } |
515 | 0 | fe_mul_ttt(&h->X, &h->X, &sqrtm1); |
516 | 0 | } |
517 | | |
518 | 0 | if (fe_isnegative(&h->X) != (s[31] >> 7)) { |
519 | 0 | fe_loose t; |
520 | 0 | fe_neg(&t, &h->X); |
521 | 0 | fe_carry(&h->X, &t); |
522 | 0 | } |
523 | |
|
524 | 0 | fe_mul_ttt(&h->T, &h->X, &h->Y); |
525 | 0 | return 1; |
526 | 0 | } |
527 | | |
528 | 0 | static void ge_p2_0(ge_p2 *h) { |
529 | 0 | fe_0(&h->X); |
530 | 0 | fe_1(&h->Y); |
531 | 0 | fe_1(&h->Z); |
532 | 0 | } |
533 | | |
534 | 0 | static void ge_p3_0(ge_p3 *h) { |
535 | 0 | fe_0(&h->X); |
536 | 0 | fe_1(&h->Y); |
537 | 0 | fe_1(&h->Z); |
538 | 0 | fe_0(&h->T); |
539 | 0 | } |
540 | | |
541 | | #if defined(OPENSSL_SMALL) |
542 | | |
543 | | static void ge_precomp_0(ge_precomp *h) { |
544 | | fe_loose_1(&h->yplusx); |
545 | | fe_loose_1(&h->yminusx); |
546 | | fe_loose_0(&h->xy2d); |
547 | | } |
548 | | |
549 | | #endif |
550 | | |
551 | | // r = p |
552 | 0 | static void ge_p3_to_p2(ge_p2 *r, const ge_p3 *p) { |
553 | 0 | fe_copy(&r->X, &p->X); |
554 | 0 | fe_copy(&r->Y, &p->Y); |
555 | 0 | fe_copy(&r->Z, &p->Z); |
556 | 0 | } |
557 | | |
558 | | // r = p |
559 | 0 | static void x25519_ge_p3_to_cached(ge_cached *r, const ge_p3 *p) { |
560 | 0 | fe_add(&r->YplusX, &p->Y, &p->X); |
561 | 0 | fe_sub(&r->YminusX, &p->Y, &p->X); |
562 | 0 | fe_copy_lt(&r->Z, &p->Z); |
563 | 0 | fe_mul_ltt(&r->T2d, &p->T, &d2); |
564 | 0 | } |
565 | | |
566 | | // r = p |
567 | 0 | static void x25519_ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p) { |
568 | 0 | fe_mul_tll(&r->X, &p->X, &p->T); |
569 | 0 | fe_mul_tll(&r->Y, &p->Y, &p->Z); |
570 | 0 | fe_mul_tll(&r->Z, &p->Z, &p->T); |
571 | 0 | } |
572 | | |
573 | | // r = p |
574 | 0 | static void x25519_ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p) { |
575 | 0 | fe_mul_tll(&r->X, &p->X, &p->T); |
576 | 0 | fe_mul_tll(&r->Y, &p->Y, &p->Z); |
577 | 0 | fe_mul_tll(&r->Z, &p->Z, &p->T); |
578 | 0 | fe_mul_tll(&r->T, &p->X, &p->Y); |
579 | 0 | } |
580 | | |
581 | | // r = 2 * p |
582 | 0 | static void ge_p2_dbl(ge_p1p1 *r, const ge_p2 *p) { |
583 | 0 | fe trX, trZ, trT; |
584 | 0 | fe t0; |
585 | |
|
586 | 0 | fe_sq_tt(&trX, &p->X); |
587 | 0 | fe_sq_tt(&trZ, &p->Y); |
588 | 0 | fe_sq2_tt(&trT, &p->Z); |
589 | 0 | fe_add(&r->Y, &p->X, &p->Y); |
590 | 0 | fe_sq_tl(&t0, &r->Y); |
591 | |
|
592 | 0 | fe_add(&r->Y, &trZ, &trX); |
593 | 0 | fe_sub(&r->Z, &trZ, &trX); |
594 | 0 | fe_carry(&trZ, &r->Y); |
595 | 0 | fe_sub(&r->X, &t0, &trZ); |
596 | 0 | fe_carry(&trZ, &r->Z); |
597 | 0 | fe_sub(&r->T, &trT, &trZ); |
598 | 0 | } |
599 | | |
600 | | // r = 2 * p |
601 | 0 | static void ge_p3_dbl(ge_p1p1 *r, const ge_p3 *p) { |
602 | 0 | ge_p2 q; |
603 | 0 | ge_p3_to_p2(&q, p); |
604 | 0 | ge_p2_dbl(r, &q); |
605 | 0 | } |
606 | | |
607 | | // r = p + q |
608 | 0 | static void ge_madd(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q) { |
609 | 0 | fe trY, trZ, trT; |
610 | |
|
611 | 0 | fe_add(&r->X, &p->Y, &p->X); |
612 | 0 | fe_sub(&r->Y, &p->Y, &p->X); |
613 | 0 | fe_mul_tll(&trZ, &r->X, &q->yplusx); |
614 | 0 | fe_mul_tll(&trY, &r->Y, &q->yminusx); |
615 | 0 | fe_mul_tlt(&trT, &q->xy2d, &p->T); |
616 | 0 | fe_add(&r->T, &p->Z, &p->Z); |
617 | 0 | fe_sub(&r->X, &trZ, &trY); |
618 | 0 | fe_add(&r->Y, &trZ, &trY); |
619 | 0 | fe_carry(&trZ, &r->T); |
620 | 0 | fe_add(&r->Z, &trZ, &trT); |
621 | 0 | fe_sub(&r->T, &trZ, &trT); |
622 | 0 | } |
623 | | |
624 | | // r = p - q |
625 | 0 | static void ge_msub(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q) { |
626 | 0 | fe trY, trZ, trT; |
627 | |
|
628 | 0 | fe_add(&r->X, &p->Y, &p->X); |
629 | 0 | fe_sub(&r->Y, &p->Y, &p->X); |
630 | 0 | fe_mul_tll(&trZ, &r->X, &q->yminusx); |
631 | 0 | fe_mul_tll(&trY, &r->Y, &q->yplusx); |
632 | 0 | fe_mul_tlt(&trT, &q->xy2d, &p->T); |
633 | 0 | fe_add(&r->T, &p->Z, &p->Z); |
634 | 0 | fe_sub(&r->X, &trZ, &trY); |
635 | 0 | fe_add(&r->Y, &trZ, &trY); |
636 | 0 | fe_carry(&trZ, &r->T); |
637 | 0 | fe_sub(&r->Z, &trZ, &trT); |
638 | 0 | fe_add(&r->T, &trZ, &trT); |
639 | 0 | } |
640 | | |
641 | | // r = p + q |
642 | 0 | static void x25519_ge_add(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q) { |
643 | 0 | fe trX, trY, trZ, trT; |
644 | |
|
645 | 0 | fe_add(&r->X, &p->Y, &p->X); |
646 | 0 | fe_sub(&r->Y, &p->Y, &p->X); |
647 | 0 | fe_mul_tll(&trZ, &r->X, &q->YplusX); |
648 | 0 | fe_mul_tll(&trY, &r->Y, &q->YminusX); |
649 | 0 | fe_mul_tlt(&trT, &q->T2d, &p->T); |
650 | 0 | fe_mul_ttl(&trX, &p->Z, &q->Z); |
651 | 0 | fe_add(&r->T, &trX, &trX); |
652 | 0 | fe_sub(&r->X, &trZ, &trY); |
653 | 0 | fe_add(&r->Y, &trZ, &trY); |
654 | 0 | fe_carry(&trZ, &r->T); |
655 | 0 | fe_add(&r->Z, &trZ, &trT); |
656 | 0 | fe_sub(&r->T, &trZ, &trT); |
657 | 0 | } |
658 | | |
659 | | // r = p - q |
660 | 0 | static void x25519_ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q) { |
661 | 0 | fe trX, trY, trZ, trT; |
662 | |
|
663 | 0 | fe_add(&r->X, &p->Y, &p->X); |
664 | 0 | fe_sub(&r->Y, &p->Y, &p->X); |
665 | 0 | fe_mul_tll(&trZ, &r->X, &q->YminusX); |
666 | 0 | fe_mul_tll(&trY, &r->Y, &q->YplusX); |
667 | 0 | fe_mul_tlt(&trT, &q->T2d, &p->T); |
668 | 0 | fe_mul_ttl(&trX, &p->Z, &q->Z); |
669 | 0 | fe_add(&r->T, &trX, &trX); |
670 | 0 | fe_sub(&r->X, &trZ, &trY); |
671 | 0 | fe_add(&r->Y, &trZ, &trY); |
672 | 0 | fe_carry(&trZ, &r->T); |
673 | 0 | fe_sub(&r->Z, &trZ, &trT); |
674 | 0 | fe_add(&r->T, &trZ, &trT); |
675 | 0 | } |
676 | | |
677 | 0 | static void cmov(ge_precomp *t, const ge_precomp *u, uint8_t b) { |
678 | 0 | fe_cmov(&t->yplusx, &u->yplusx, b); |
679 | 0 | fe_cmov(&t->yminusx, &u->yminusx, b); |
680 | 0 | fe_cmov(&t->xy2d, &u->xy2d, b); |
681 | 0 | } |
682 | | |
683 | | #if defined(OPENSSL_SMALL) |
684 | | |
685 | | static void x25519_ge_scalarmult_small_precomp( |
686 | | ge_p3 *h, const uint8_t a[32], const uint8_t precomp_table[15 * 2 * 32]) { |
687 | | // precomp_table is first expanded into matching |ge_precomp| |
688 | | // elements. |
689 | | ge_precomp multiples[15]; |
690 | | |
691 | | unsigned i; |
692 | | for (i = 0; i < 15; i++) { |
693 | | // The precomputed table is assumed to already clear the top bit, so |
694 | | // |fe_frombytes_strict| may be used directly. |
695 | | const uint8_t *bytes = &precomp_table[i*(2 * 32)]; |
696 | | fe x, y; |
697 | | fe_frombytes_strict(&x, bytes); |
698 | | fe_frombytes_strict(&y, bytes + 32); |
699 | | |
700 | | ge_precomp *out = &multiples[i]; |
701 | | fe_add(&out->yplusx, &y, &x); |
702 | | fe_sub(&out->yminusx, &y, &x); |
703 | | fe_mul_ltt(&out->xy2d, &x, &y); |
704 | | fe_mul_llt(&out->xy2d, &out->xy2d, &d2); |
705 | | } |
706 | | |
707 | | // See the comment above |k25519SmallPrecomp| about the structure of the |
708 | | // precomputed elements. This loop does 64 additions and 64 doublings to |
709 | | // calculate the result. |
710 | | ge_p3_0(h); |
711 | | |
712 | | for (i = 63; i < 64; i--) { |
713 | | unsigned j; |
714 | | signed char index = 0; |
715 | | |
716 | | for (j = 0; j < 4; j++) { |
717 | | const uint8_t bit = 1 & (a[(8 * j) + (i / 8)] >> (i & 7)); |
718 | | index |= (bit << j); |
719 | | } |
720 | | |
721 | | ge_precomp e; |
722 | | ge_precomp_0(&e); |
723 | | |
724 | | for (j = 1; j < 16; j++) { |
725 | | cmov(&e, &multiples[j-1], 1&constant_time_eq_w(index, j)); |
726 | | } |
727 | | |
728 | | ge_cached cached; |
729 | | ge_p1p1 r; |
730 | | x25519_ge_p3_to_cached(&cached, h); |
731 | | x25519_ge_add(&r, h, &cached); |
732 | | x25519_ge_p1p1_to_p3(h, &r); |
733 | | |
734 | | ge_madd(&r, h, &e); |
735 | | x25519_ge_p1p1_to_p3(h, &r); |
736 | | } |
737 | | } |
738 | | |
739 | | void x25519_ge_scalarmult_base(ge_p3 *h, const uint8_t a[32], int use_adx) { |
740 | | (void)use_adx; |
741 | | x25519_ge_scalarmult_small_precomp(h, a, k25519SmallPrecomp); |
742 | | } |
743 | | |
744 | | #else |
745 | | |
746 | 0 | static void table_select(ge_precomp *t, const int pos, const signed char b) { |
747 | 0 | uint8_t bnegative = constant_time_msb_w(b); |
748 | 0 | uint8_t babs = b - ((bnegative & b) << 1); |
749 | |
|
750 | 0 | uint8_t t_bytes[3][32] = { |
751 | 0 | {constant_time_is_zero_w(b) & 1}, {constant_time_is_zero_w(b) & 1}, {0}}; |
752 | 0 | #if defined(__clang__) // materialize for vectorization, 6% speedup |
753 | 0 | __asm__("" : "+m" (t_bytes) : /*no inputs*/); |
754 | 0 | #endif |
755 | 0 | OPENSSL_STATIC_ASSERT(sizeof(t_bytes) == sizeof(k25519Precomp[pos][0]), ""); |
756 | 0 | for (int i = 0; i < 8; i++) { |
757 | 0 | constant_time_conditional_memxor(t_bytes, k25519Precomp[pos][i], |
758 | 0 | sizeof(t_bytes), |
759 | 0 | constant_time_eq_w(babs, 1 + i)); |
760 | 0 | } |
761 | |
|
762 | 0 | fe yplusx, yminusx, xy2d; |
763 | 0 | fe_frombytes_strict(&yplusx, t_bytes[0]); |
764 | 0 | fe_frombytes_strict(&yminusx, t_bytes[1]); |
765 | 0 | fe_frombytes_strict(&xy2d, t_bytes[2]); |
766 | |
|
767 | 0 | fe_copy_lt(&t->yplusx, &yplusx); |
768 | 0 | fe_copy_lt(&t->yminusx, &yminusx); |
769 | 0 | fe_copy_lt(&t->xy2d, &xy2d); |
770 | |
|
771 | 0 | ge_precomp minust; |
772 | 0 | fe_copy_lt(&minust.yplusx, &yminusx); |
773 | 0 | fe_copy_lt(&minust.yminusx, &yplusx); |
774 | 0 | fe_neg(&minust.xy2d, &xy2d); |
775 | 0 | cmov(t, &minust, bnegative>>7); |
776 | 0 | } |
777 | | |
778 | | // h = a * B |
779 | | // where a = a[0]+256*a[1]+...+256^31 a[31] |
780 | | // B is the Ed25519 base point (x,4/5) with x positive. |
781 | | // |
782 | | // Preconditions: |
783 | | // a[31] <= 127 |
784 | 0 | void x25519_ge_scalarmult_base(ge_p3 *h, const uint8_t a[32], int use_adx) { |
785 | 0 | #if defined(BORINGSSL_FE25519_ADX) |
786 | 0 | if (use_adx) { |
787 | 0 | uint8_t t[4][32]; |
788 | 0 | x25519_ge_scalarmult_base_adx(t, a); |
789 | 0 | fiat_25519_from_bytes(h->X.v, t[0]); |
790 | 0 | fiat_25519_from_bytes(h->Y.v, t[1]); |
791 | 0 | fiat_25519_from_bytes(h->Z.v, t[2]); |
792 | 0 | fiat_25519_from_bytes(h->T.v, t[3]); |
793 | 0 | return; |
794 | 0 | } |
795 | | #else |
796 | | (void)use_adx; |
797 | | #endif |
798 | 0 | signed char e[64]; |
799 | 0 | signed char carry; |
800 | 0 | ge_p1p1 r; |
801 | 0 | ge_p2 s; |
802 | 0 | ge_precomp t; |
803 | 0 | int i; |
804 | |
|
805 | 0 | for (i = 0; i < 32; ++i) { |
806 | 0 | e[2 * i + 0] = (a[i] >> 0) & 15; |
807 | 0 | e[2 * i + 1] = (a[i] >> 4) & 15; |
808 | 0 | } |
809 | | // each e[i] is between 0 and 15 |
810 | | // e[63] is between 0 and 7 |
811 | |
|
812 | 0 | carry = 0; |
813 | 0 | for (i = 0; i < 63; ++i) { |
814 | 0 | e[i] += carry; |
815 | 0 | carry = e[i] + 8; |
816 | 0 | carry >>= 4; |
817 | 0 | e[i] -= carry << 4; |
818 | 0 | } |
819 | 0 | e[63] += carry; |
820 | | // each e[i] is between -8 and 8 |
821 | |
|
822 | 0 | ge_p3_0(h); |
823 | 0 | for (i = 1; i < 64; i += 2) { |
824 | 0 | table_select(&t, i / 2, e[i]); |
825 | 0 | ge_madd(&r, h, &t); |
826 | 0 | x25519_ge_p1p1_to_p3(h, &r); |
827 | 0 | } |
828 | |
|
829 | 0 | ge_p3_dbl(&r, h); |
830 | 0 | x25519_ge_p1p1_to_p2(&s, &r); |
831 | 0 | ge_p2_dbl(&r, &s); |
832 | 0 | x25519_ge_p1p1_to_p2(&s, &r); |
833 | 0 | ge_p2_dbl(&r, &s); |
834 | 0 | x25519_ge_p1p1_to_p2(&s, &r); |
835 | 0 | ge_p2_dbl(&r, &s); |
836 | 0 | x25519_ge_p1p1_to_p3(h, &r); |
837 | |
|
838 | 0 | for (i = 0; i < 64; i += 2) { |
839 | 0 | table_select(&t, i / 2, e[i]); |
840 | 0 | ge_madd(&r, h, &t); |
841 | 0 | x25519_ge_p1p1_to_p3(h, &r); |
842 | 0 | } |
843 | 0 | } |
844 | | |
845 | | #endif |
846 | | |
847 | 0 | static void slide(signed char *r, const uint8_t *a) { |
848 | 0 | int i; |
849 | 0 | int b; |
850 | 0 | int k; |
851 | |
|
852 | 0 | for (i = 0; i < 256; ++i) { |
853 | 0 | r[i] = 1 & (a[i >> 3] >> (i & 7)); |
854 | 0 | } |
855 | |
|
856 | 0 | for (i = 0; i < 256; ++i) { |
857 | 0 | if (r[i]) { |
858 | 0 | for (b = 1; b <= 6 && i + b < 256; ++b) { |
859 | 0 | if (r[i + b]) { |
860 | 0 | if (r[i] + (r[i + b] << b) <= 15) { |
861 | 0 | r[i] += r[i + b] << b; |
862 | 0 | r[i + b] = 0; |
863 | 0 | } else if (r[i] - (r[i + b] << b) >= -15) { |
864 | 0 | r[i] -= r[i + b] << b; |
865 | 0 | for (k = i + b; k < 256; ++k) { |
866 | 0 | if (!r[k]) { |
867 | 0 | r[k] = 1; |
868 | 0 | break; |
869 | 0 | } |
870 | 0 | r[k] = 0; |
871 | 0 | } |
872 | 0 | } else { |
873 | 0 | break; |
874 | 0 | } |
875 | 0 | } |
876 | 0 | } |
877 | 0 | } |
878 | 0 | } |
879 | 0 | } |
880 | | |
881 | | // r = a * A + b * B |
882 | | // where a = a[0]+256*a[1]+...+256^31 a[31]. |
883 | | // and b = b[0]+256*b[1]+...+256^31 b[31]. |
884 | | // B is the Ed25519 base point (x,4/5) with x positive. |
885 | | static void ge_double_scalarmult_vartime(ge_p2 *r, const uint8_t *a, |
886 | 0 | const ge_p3 *A, const uint8_t *b) { |
887 | 0 | signed char aslide[256]; |
888 | 0 | signed char bslide[256]; |
889 | 0 | ge_cached Ai[8]; // A,3A,5A,7A,9A,11A,13A,15A |
890 | 0 | ge_p1p1 t; |
891 | 0 | ge_p3 u; |
892 | 0 | ge_p3 A2; |
893 | 0 | int i; |
894 | |
|
895 | 0 | slide(aslide, a); |
896 | 0 | slide(bslide, b); |
897 | |
|
898 | 0 | x25519_ge_p3_to_cached(&Ai[0], A); |
899 | 0 | ge_p3_dbl(&t, A); |
900 | 0 | x25519_ge_p1p1_to_p3(&A2, &t); |
901 | 0 | x25519_ge_add(&t, &A2, &Ai[0]); |
902 | 0 | x25519_ge_p1p1_to_p3(&u, &t); |
903 | 0 | x25519_ge_p3_to_cached(&Ai[1], &u); |
904 | 0 | x25519_ge_add(&t, &A2, &Ai[1]); |
905 | 0 | x25519_ge_p1p1_to_p3(&u, &t); |
906 | 0 | x25519_ge_p3_to_cached(&Ai[2], &u); |
907 | 0 | x25519_ge_add(&t, &A2, &Ai[2]); |
908 | 0 | x25519_ge_p1p1_to_p3(&u, &t); |
909 | 0 | x25519_ge_p3_to_cached(&Ai[3], &u); |
910 | 0 | x25519_ge_add(&t, &A2, &Ai[3]); |
911 | 0 | x25519_ge_p1p1_to_p3(&u, &t); |
912 | 0 | x25519_ge_p3_to_cached(&Ai[4], &u); |
913 | 0 | x25519_ge_add(&t, &A2, &Ai[4]); |
914 | 0 | x25519_ge_p1p1_to_p3(&u, &t); |
915 | 0 | x25519_ge_p3_to_cached(&Ai[5], &u); |
916 | 0 | x25519_ge_add(&t, &A2, &Ai[5]); |
917 | 0 | x25519_ge_p1p1_to_p3(&u, &t); |
918 | 0 | x25519_ge_p3_to_cached(&Ai[6], &u); |
919 | 0 | x25519_ge_add(&t, &A2, &Ai[6]); |
920 | 0 | x25519_ge_p1p1_to_p3(&u, &t); |
921 | 0 | x25519_ge_p3_to_cached(&Ai[7], &u); |
922 | |
|
923 | 0 | ge_p2_0(r); |
924 | |
|
925 | 0 | for (i = 255; i >= 0; --i) { |
926 | 0 | if (aslide[i] || bslide[i]) { |
927 | 0 | break; |
928 | 0 | } |
929 | 0 | } |
930 | |
|
931 | 0 | for (; i >= 0; --i) { |
932 | 0 | ge_p2_dbl(&t, r); |
933 | |
|
934 | 0 | if (aslide[i] > 0) { |
935 | 0 | x25519_ge_p1p1_to_p3(&u, &t); |
936 | 0 | x25519_ge_add(&t, &u, &Ai[aslide[i] / 2]); |
937 | 0 | } else if (aslide[i] < 0) { |
938 | 0 | x25519_ge_p1p1_to_p3(&u, &t); |
939 | 0 | x25519_ge_sub(&t, &u, &Ai[(-aslide[i]) / 2]); |
940 | 0 | } |
941 | |
|
942 | 0 | if (bslide[i] > 0) { |
943 | 0 | x25519_ge_p1p1_to_p3(&u, &t); |
944 | 0 | ge_madd(&t, &u, &Bi[bslide[i] / 2]); |
945 | 0 | } else if (bslide[i] < 0) { |
946 | 0 | x25519_ge_p1p1_to_p3(&u, &t); |
947 | 0 | ge_msub(&t, &u, &Bi[(-bslide[i]) / 2]); |
948 | 0 | } |
949 | |
|
950 | 0 | x25519_ge_p1p1_to_p2(r, &t); |
951 | 0 | } |
952 | 0 | } |
953 | | |
954 | | // int64_lshift21 returns |a << 21| but is defined when shifting bits into the |
955 | | // sign bit. This works around a language flaw in C. |
956 | 0 | static inline int64_t int64_lshift21(int64_t a) { |
957 | 0 | return (int64_t)((uint64_t)a << 21); |
958 | 0 | } |
959 | | |
960 | | // The set of scalars is \Z/l |
961 | | // where l = 2^252 + 27742317777372353535851937790883648493. |
962 | | |
963 | | // Input: |
964 | | // s[0]+256*s[1]+...+256^63*s[63] = s |
965 | | // |
966 | | // Output: |
967 | | // s[0]+256*s[1]+...+256^31*s[31] = s mod l |
968 | | // where l = 2^252 + 27742317777372353535851937790883648493. |
969 | | // Overwrites s in place. |
970 | 0 | void x25519_sc_reduce(uint8_t s[64]) { |
971 | 0 | int64_t s0 = 2097151 & load_3(s); |
972 | 0 | int64_t s1 = 2097151 & (load_4(s + 2) >> 5); |
973 | 0 | int64_t s2 = 2097151 & (load_3(s + 5) >> 2); |
974 | 0 | int64_t s3 = 2097151 & (load_4(s + 7) >> 7); |
975 | 0 | int64_t s4 = 2097151 & (load_4(s + 10) >> 4); |
976 | 0 | int64_t s5 = 2097151 & (load_3(s + 13) >> 1); |
977 | 0 | int64_t s6 = 2097151 & (load_4(s + 15) >> 6); |
978 | 0 | int64_t s7 = 2097151 & (load_3(s + 18) >> 3); |
979 | 0 | int64_t s8 = 2097151 & load_3(s + 21); |
980 | 0 | int64_t s9 = 2097151 & (load_4(s + 23) >> 5); |
981 | 0 | int64_t s10 = 2097151 & (load_3(s + 26) >> 2); |
982 | 0 | int64_t s11 = 2097151 & (load_4(s + 28) >> 7); |
983 | 0 | int64_t s12 = 2097151 & (load_4(s + 31) >> 4); |
984 | 0 | int64_t s13 = 2097151 & (load_3(s + 34) >> 1); |
985 | 0 | int64_t s14 = 2097151 & (load_4(s + 36) >> 6); |
986 | 0 | int64_t s15 = 2097151 & (load_3(s + 39) >> 3); |
987 | 0 | int64_t s16 = 2097151 & load_3(s + 42); |
988 | 0 | int64_t s17 = 2097151 & (load_4(s + 44) >> 5); |
989 | 0 | int64_t s18 = 2097151 & (load_3(s + 47) >> 2); |
990 | 0 | int64_t s19 = 2097151 & (load_4(s + 49) >> 7); |
991 | 0 | int64_t s20 = 2097151 & (load_4(s + 52) >> 4); |
992 | 0 | int64_t s21 = 2097151 & (load_3(s + 55) >> 1); |
993 | 0 | int64_t s22 = 2097151 & (load_4(s + 57) >> 6); |
994 | 0 | int64_t s23 = (load_4(s + 60) >> 3); |
995 | 0 | int64_t carry0; |
996 | 0 | int64_t carry1; |
997 | 0 | int64_t carry2; |
998 | 0 | int64_t carry3; |
999 | 0 | int64_t carry4; |
1000 | 0 | int64_t carry5; |
1001 | 0 | int64_t carry6; |
1002 | 0 | int64_t carry7; |
1003 | 0 | int64_t carry8; |
1004 | 0 | int64_t carry9; |
1005 | 0 | int64_t carry10; |
1006 | 0 | int64_t carry11; |
1007 | 0 | int64_t carry12; |
1008 | 0 | int64_t carry13; |
1009 | 0 | int64_t carry14; |
1010 | 0 | int64_t carry15; |
1011 | 0 | int64_t carry16; |
1012 | |
|
1013 | 0 | s11 += s23 * 666643; |
1014 | 0 | s12 += s23 * 470296; |
1015 | 0 | s13 += s23 * 654183; |
1016 | 0 | s14 -= s23 * 997805; |
1017 | 0 | s15 += s23 * 136657; |
1018 | 0 | s16 -= s23 * 683901; |
1019 | 0 | s23 = 0; |
1020 | |
|
1021 | 0 | s10 += s22 * 666643; |
1022 | 0 | s11 += s22 * 470296; |
1023 | 0 | s12 += s22 * 654183; |
1024 | 0 | s13 -= s22 * 997805; |
1025 | 0 | s14 += s22 * 136657; |
1026 | 0 | s15 -= s22 * 683901; |
1027 | 0 | s22 = 0; |
1028 | |
|
1029 | 0 | s9 += s21 * 666643; |
1030 | 0 | s10 += s21 * 470296; |
1031 | 0 | s11 += s21 * 654183; |
1032 | 0 | s12 -= s21 * 997805; |
1033 | 0 | s13 += s21 * 136657; |
1034 | 0 | s14 -= s21 * 683901; |
1035 | 0 | s21 = 0; |
1036 | |
|
1037 | 0 | s8 += s20 * 666643; |
1038 | 0 | s9 += s20 * 470296; |
1039 | 0 | s10 += s20 * 654183; |
1040 | 0 | s11 -= s20 * 997805; |
1041 | 0 | s12 += s20 * 136657; |
1042 | 0 | s13 -= s20 * 683901; |
1043 | 0 | s20 = 0; |
1044 | |
|
1045 | 0 | s7 += s19 * 666643; |
1046 | 0 | s8 += s19 * 470296; |
1047 | 0 | s9 += s19 * 654183; |
1048 | 0 | s10 -= s19 * 997805; |
1049 | 0 | s11 += s19 * 136657; |
1050 | 0 | s12 -= s19 * 683901; |
1051 | 0 | s19 = 0; |
1052 | |
|
1053 | 0 | s6 += s18 * 666643; |
1054 | 0 | s7 += s18 * 470296; |
1055 | 0 | s8 += s18 * 654183; |
1056 | 0 | s9 -= s18 * 997805; |
1057 | 0 | s10 += s18 * 136657; |
1058 | 0 | s11 -= s18 * 683901; |
1059 | 0 | s18 = 0; |
1060 | |
|
1061 | 0 | carry6 = (s6 + (1 << 20)) >> 21; |
1062 | 0 | s7 += carry6; |
1063 | 0 | s6 -= int64_lshift21(carry6); |
1064 | 0 | carry8 = (s8 + (1 << 20)) >> 21; |
1065 | 0 | s9 += carry8; |
1066 | 0 | s8 -= int64_lshift21(carry8); |
1067 | 0 | carry10 = (s10 + (1 << 20)) >> 21; |
1068 | 0 | s11 += carry10; |
1069 | 0 | s10 -= int64_lshift21(carry10); |
1070 | 0 | carry12 = (s12 + (1 << 20)) >> 21; |
1071 | 0 | s13 += carry12; |
1072 | 0 | s12 -= int64_lshift21(carry12); |
1073 | 0 | carry14 = (s14 + (1 << 20)) >> 21; |
1074 | 0 | s15 += carry14; |
1075 | 0 | s14 -= int64_lshift21(carry14); |
1076 | 0 | carry16 = (s16 + (1 << 20)) >> 21; |
1077 | 0 | s17 += carry16; |
1078 | 0 | s16 -= int64_lshift21(carry16); |
1079 | |
|
1080 | 0 | carry7 = (s7 + (1 << 20)) >> 21; |
1081 | 0 | s8 += carry7; |
1082 | 0 | s7 -= int64_lshift21(carry7); |
1083 | 0 | carry9 = (s9 + (1 << 20)) >> 21; |
1084 | 0 | s10 += carry9; |
1085 | 0 | s9 -= int64_lshift21(carry9); |
1086 | 0 | carry11 = (s11 + (1 << 20)) >> 21; |
1087 | 0 | s12 += carry11; |
1088 | 0 | s11 -= int64_lshift21(carry11); |
1089 | 0 | carry13 = (s13 + (1 << 20)) >> 21; |
1090 | 0 | s14 += carry13; |
1091 | 0 | s13 -= int64_lshift21(carry13); |
1092 | 0 | carry15 = (s15 + (1 << 20)) >> 21; |
1093 | 0 | s16 += carry15; |
1094 | 0 | s15 -= int64_lshift21(carry15); |
1095 | |
|
1096 | 0 | s5 += s17 * 666643; |
1097 | 0 | s6 += s17 * 470296; |
1098 | 0 | s7 += s17 * 654183; |
1099 | 0 | s8 -= s17 * 997805; |
1100 | 0 | s9 += s17 * 136657; |
1101 | 0 | s10 -= s17 * 683901; |
1102 | 0 | s17 = 0; |
1103 | |
|
1104 | 0 | s4 += s16 * 666643; |
1105 | 0 | s5 += s16 * 470296; |
1106 | 0 | s6 += s16 * 654183; |
1107 | 0 | s7 -= s16 * 997805; |
1108 | 0 | s8 += s16 * 136657; |
1109 | 0 | s9 -= s16 * 683901; |
1110 | 0 | s16 = 0; |
1111 | |
|
1112 | 0 | s3 += s15 * 666643; |
1113 | 0 | s4 += s15 * 470296; |
1114 | 0 | s5 += s15 * 654183; |
1115 | 0 | s6 -= s15 * 997805; |
1116 | 0 | s7 += s15 * 136657; |
1117 | 0 | s8 -= s15 * 683901; |
1118 | 0 | s15 = 0; |
1119 | |
|
1120 | 0 | s2 += s14 * 666643; |
1121 | 0 | s3 += s14 * 470296; |
1122 | 0 | s4 += s14 * 654183; |
1123 | 0 | s5 -= s14 * 997805; |
1124 | 0 | s6 += s14 * 136657; |
1125 | 0 | s7 -= s14 * 683901; |
1126 | 0 | s14 = 0; |
1127 | |
|
1128 | 0 | s1 += s13 * 666643; |
1129 | 0 | s2 += s13 * 470296; |
1130 | 0 | s3 += s13 * 654183; |
1131 | 0 | s4 -= s13 * 997805; |
1132 | 0 | s5 += s13 * 136657; |
1133 | 0 | s6 -= s13 * 683901; |
1134 | 0 | s13 = 0; |
1135 | |
|
1136 | 0 | s0 += s12 * 666643; |
1137 | 0 | s1 += s12 * 470296; |
1138 | 0 | s2 += s12 * 654183; |
1139 | 0 | s3 -= s12 * 997805; |
1140 | 0 | s4 += s12 * 136657; |
1141 | 0 | s5 -= s12 * 683901; |
1142 | 0 | s12 = 0; |
1143 | |
|
1144 | 0 | carry0 = (s0 + (1 << 20)) >> 21; |
1145 | 0 | s1 += carry0; |
1146 | 0 | s0 -= int64_lshift21(carry0); |
1147 | 0 | carry2 = (s2 + (1 << 20)) >> 21; |
1148 | 0 | s3 += carry2; |
1149 | 0 | s2 -= int64_lshift21(carry2); |
1150 | 0 | carry4 = (s4 + (1 << 20)) >> 21; |
1151 | 0 | s5 += carry4; |
1152 | 0 | s4 -= int64_lshift21(carry4); |
1153 | 0 | carry6 = (s6 + (1 << 20)) >> 21; |
1154 | 0 | s7 += carry6; |
1155 | 0 | s6 -= int64_lshift21(carry6); |
1156 | 0 | carry8 = (s8 + (1 << 20)) >> 21; |
1157 | 0 | s9 += carry8; |
1158 | 0 | s8 -= int64_lshift21(carry8); |
1159 | 0 | carry10 = (s10 + (1 << 20)) >> 21; |
1160 | 0 | s11 += carry10; |
1161 | 0 | s10 -= int64_lshift21(carry10); |
1162 | |
|
1163 | 0 | carry1 = (s1 + (1 << 20)) >> 21; |
1164 | 0 | s2 += carry1; |
1165 | 0 | s1 -= int64_lshift21(carry1); |
1166 | 0 | carry3 = (s3 + (1 << 20)) >> 21; |
1167 | 0 | s4 += carry3; |
1168 | 0 | s3 -= int64_lshift21(carry3); |
1169 | 0 | carry5 = (s5 + (1 << 20)) >> 21; |
1170 | 0 | s6 += carry5; |
1171 | 0 | s5 -= int64_lshift21(carry5); |
1172 | 0 | carry7 = (s7 + (1 << 20)) >> 21; |
1173 | 0 | s8 += carry7; |
1174 | 0 | s7 -= int64_lshift21(carry7); |
1175 | 0 | carry9 = (s9 + (1 << 20)) >> 21; |
1176 | 0 | s10 += carry9; |
1177 | 0 | s9 -= int64_lshift21(carry9); |
1178 | 0 | carry11 = (s11 + (1 << 20)) >> 21; |
1179 | 0 | s12 += carry11; |
1180 | 0 | s11 -= int64_lshift21(carry11); |
1181 | |
|
1182 | 0 | s0 += s12 * 666643; |
1183 | 0 | s1 += s12 * 470296; |
1184 | 0 | s2 += s12 * 654183; |
1185 | 0 | s3 -= s12 * 997805; |
1186 | 0 | s4 += s12 * 136657; |
1187 | 0 | s5 -= s12 * 683901; |
1188 | 0 | s12 = 0; |
1189 | |
|
1190 | 0 | carry0 = s0 >> 21; |
1191 | 0 | s1 += carry0; |
1192 | 0 | s0 -= int64_lshift21(carry0); |
1193 | 0 | carry1 = s1 >> 21; |
1194 | 0 | s2 += carry1; |
1195 | 0 | s1 -= int64_lshift21(carry1); |
1196 | 0 | carry2 = s2 >> 21; |
1197 | 0 | s3 += carry2; |
1198 | 0 | s2 -= int64_lshift21(carry2); |
1199 | 0 | carry3 = s3 >> 21; |
1200 | 0 | s4 += carry3; |
1201 | 0 | s3 -= int64_lshift21(carry3); |
1202 | 0 | carry4 = s4 >> 21; |
1203 | 0 | s5 += carry4; |
1204 | 0 | s4 -= int64_lshift21(carry4); |
1205 | 0 | carry5 = s5 >> 21; |
1206 | 0 | s6 += carry5; |
1207 | 0 | s5 -= int64_lshift21(carry5); |
1208 | 0 | carry6 = s6 >> 21; |
1209 | 0 | s7 += carry6; |
1210 | 0 | s6 -= int64_lshift21(carry6); |
1211 | 0 | carry7 = s7 >> 21; |
1212 | 0 | s8 += carry7; |
1213 | 0 | s7 -= int64_lshift21(carry7); |
1214 | 0 | carry8 = s8 >> 21; |
1215 | 0 | s9 += carry8; |
1216 | 0 | s8 -= int64_lshift21(carry8); |
1217 | 0 | carry9 = s9 >> 21; |
1218 | 0 | s10 += carry9; |
1219 | 0 | s9 -= int64_lshift21(carry9); |
1220 | 0 | carry10 = s10 >> 21; |
1221 | 0 | s11 += carry10; |
1222 | 0 | s10 -= int64_lshift21(carry10); |
1223 | 0 | carry11 = s11 >> 21; |
1224 | 0 | s12 += carry11; |
1225 | 0 | s11 -= int64_lshift21(carry11); |
1226 | |
|
1227 | 0 | s0 += s12 * 666643; |
1228 | 0 | s1 += s12 * 470296; |
1229 | 0 | s2 += s12 * 654183; |
1230 | 0 | s3 -= s12 * 997805; |
1231 | 0 | s4 += s12 * 136657; |
1232 | 0 | s5 -= s12 * 683901; |
1233 | 0 | s12 = 0; |
1234 | |
|
1235 | 0 | carry0 = s0 >> 21; |
1236 | 0 | s1 += carry0; |
1237 | 0 | s0 -= int64_lshift21(carry0); |
1238 | 0 | carry1 = s1 >> 21; |
1239 | 0 | s2 += carry1; |
1240 | 0 | s1 -= int64_lshift21(carry1); |
1241 | 0 | carry2 = s2 >> 21; |
1242 | 0 | s3 += carry2; |
1243 | 0 | s2 -= int64_lshift21(carry2); |
1244 | 0 | carry3 = s3 >> 21; |
1245 | 0 | s4 += carry3; |
1246 | 0 | s3 -= int64_lshift21(carry3); |
1247 | 0 | carry4 = s4 >> 21; |
1248 | 0 | s5 += carry4; |
1249 | 0 | s4 -= int64_lshift21(carry4); |
1250 | 0 | carry5 = s5 >> 21; |
1251 | 0 | s6 += carry5; |
1252 | 0 | s5 -= int64_lshift21(carry5); |
1253 | 0 | carry6 = s6 >> 21; |
1254 | 0 | s7 += carry6; |
1255 | 0 | s6 -= int64_lshift21(carry6); |
1256 | 0 | carry7 = s7 >> 21; |
1257 | 0 | s8 += carry7; |
1258 | 0 | s7 -= int64_lshift21(carry7); |
1259 | 0 | carry8 = s8 >> 21; |
1260 | 0 | s9 += carry8; |
1261 | 0 | s8 -= int64_lshift21(carry8); |
1262 | 0 | carry9 = s9 >> 21; |
1263 | 0 | s10 += carry9; |
1264 | 0 | s9 -= int64_lshift21(carry9); |
1265 | 0 | carry10 = s10 >> 21; |
1266 | 0 | s11 += carry10; |
1267 | 0 | s10 -= int64_lshift21(carry10); |
1268 | |
|
1269 | 0 | s[0] = s0 >> 0; |
1270 | 0 | s[1] = s0 >> 8; |
1271 | 0 | s[2] = (s0 >> 16) | (s1 << 5); |
1272 | 0 | s[3] = s1 >> 3; |
1273 | 0 | s[4] = s1 >> 11; |
1274 | 0 | s[5] = (s1 >> 19) | (s2 << 2); |
1275 | 0 | s[6] = s2 >> 6; |
1276 | 0 | s[7] = (s2 >> 14) | (s3 << 7); |
1277 | 0 | s[8] = s3 >> 1; |
1278 | 0 | s[9] = s3 >> 9; |
1279 | 0 | s[10] = (s3 >> 17) | (s4 << 4); |
1280 | 0 | s[11] = s4 >> 4; |
1281 | 0 | s[12] = s4 >> 12; |
1282 | 0 | s[13] = (s4 >> 20) | (s5 << 1); |
1283 | 0 | s[14] = s5 >> 7; |
1284 | 0 | s[15] = (s5 >> 15) | (s6 << 6); |
1285 | 0 | s[16] = s6 >> 2; |
1286 | 0 | s[17] = s6 >> 10; |
1287 | 0 | s[18] = (s6 >> 18) | (s7 << 3); |
1288 | 0 | s[19] = s7 >> 5; |
1289 | 0 | s[20] = s7 >> 13; |
1290 | 0 | s[21] = s8 >> 0; |
1291 | 0 | s[22] = s8 >> 8; |
1292 | 0 | s[23] = (s8 >> 16) | (s9 << 5); |
1293 | 0 | s[24] = s9 >> 3; |
1294 | 0 | s[25] = s9 >> 11; |
1295 | 0 | s[26] = (s9 >> 19) | (s10 << 2); |
1296 | 0 | s[27] = s10 >> 6; |
1297 | 0 | s[28] = (s10 >> 14) | (s11 << 7); |
1298 | 0 | s[29] = s11 >> 1; |
1299 | 0 | s[30] = s11 >> 9; |
1300 | 0 | s[31] = s11 >> 17; |
1301 | 0 | } |
1302 | | |
1303 | | // Input: |
1304 | | // a[0]+256*a[1]+...+256^31*a[31] = a |
1305 | | // b[0]+256*b[1]+...+256^31*b[31] = b |
1306 | | // c[0]+256*c[1]+...+256^31*c[31] = c |
1307 | | // |
1308 | | // Output: |
1309 | | // s[0]+256*s[1]+...+256^31*s[31] = (ab+c) mod l |
1310 | | // where l = 2^252 + 27742317777372353535851937790883648493. |
1311 | | static void sc_muladd(uint8_t *s, const uint8_t *a, const uint8_t *b, |
1312 | 0 | const uint8_t *c) { |
1313 | 0 | int64_t a0 = 2097151 & load_3(a); |
1314 | 0 | int64_t a1 = 2097151 & (load_4(a + 2) >> 5); |
1315 | 0 | int64_t a2 = 2097151 & (load_3(a + 5) >> 2); |
1316 | 0 | int64_t a3 = 2097151 & (load_4(a + 7) >> 7); |
1317 | 0 | int64_t a4 = 2097151 & (load_4(a + 10) >> 4); |
1318 | 0 | int64_t a5 = 2097151 & (load_3(a + 13) >> 1); |
1319 | 0 | int64_t a6 = 2097151 & (load_4(a + 15) >> 6); |
1320 | 0 | int64_t a7 = 2097151 & (load_3(a + 18) >> 3); |
1321 | 0 | int64_t a8 = 2097151 & load_3(a + 21); |
1322 | 0 | int64_t a9 = 2097151 & (load_4(a + 23) >> 5); |
1323 | 0 | int64_t a10 = 2097151 & (load_3(a + 26) >> 2); |
1324 | 0 | int64_t a11 = (load_4(a + 28) >> 7); |
1325 | 0 | int64_t b0 = 2097151 & load_3(b); |
1326 | 0 | int64_t b1 = 2097151 & (load_4(b + 2) >> 5); |
1327 | 0 | int64_t b2 = 2097151 & (load_3(b + 5) >> 2); |
1328 | 0 | int64_t b3 = 2097151 & (load_4(b + 7) >> 7); |
1329 | 0 | int64_t b4 = 2097151 & (load_4(b + 10) >> 4); |
1330 | 0 | int64_t b5 = 2097151 & (load_3(b + 13) >> 1); |
1331 | 0 | int64_t b6 = 2097151 & (load_4(b + 15) >> 6); |
1332 | 0 | int64_t b7 = 2097151 & (load_3(b + 18) >> 3); |
1333 | 0 | int64_t b8 = 2097151 & load_3(b + 21); |
1334 | 0 | int64_t b9 = 2097151 & (load_4(b + 23) >> 5); |
1335 | 0 | int64_t b10 = 2097151 & (load_3(b + 26) >> 2); |
1336 | 0 | int64_t b11 = (load_4(b + 28) >> 7); |
1337 | 0 | int64_t c0 = 2097151 & load_3(c); |
1338 | 0 | int64_t c1 = 2097151 & (load_4(c + 2) >> 5); |
1339 | 0 | int64_t c2 = 2097151 & (load_3(c + 5) >> 2); |
1340 | 0 | int64_t c3 = 2097151 & (load_4(c + 7) >> 7); |
1341 | 0 | int64_t c4 = 2097151 & (load_4(c + 10) >> 4); |
1342 | 0 | int64_t c5 = 2097151 & (load_3(c + 13) >> 1); |
1343 | 0 | int64_t c6 = 2097151 & (load_4(c + 15) >> 6); |
1344 | 0 | int64_t c7 = 2097151 & (load_3(c + 18) >> 3); |
1345 | 0 | int64_t c8 = 2097151 & load_3(c + 21); |
1346 | 0 | int64_t c9 = 2097151 & (load_4(c + 23) >> 5); |
1347 | 0 | int64_t c10 = 2097151 & (load_3(c + 26) >> 2); |
1348 | 0 | int64_t c11 = (load_4(c + 28) >> 7); |
1349 | 0 | int64_t s0; |
1350 | 0 | int64_t s1; |
1351 | 0 | int64_t s2; |
1352 | 0 | int64_t s3; |
1353 | 0 | int64_t s4; |
1354 | 0 | int64_t s5; |
1355 | 0 | int64_t s6; |
1356 | 0 | int64_t s7; |
1357 | 0 | int64_t s8; |
1358 | 0 | int64_t s9; |
1359 | 0 | int64_t s10; |
1360 | 0 | int64_t s11; |
1361 | 0 | int64_t s12; |
1362 | 0 | int64_t s13; |
1363 | 0 | int64_t s14; |
1364 | 0 | int64_t s15; |
1365 | 0 | int64_t s16; |
1366 | 0 | int64_t s17; |
1367 | 0 | int64_t s18; |
1368 | 0 | int64_t s19; |
1369 | 0 | int64_t s20; |
1370 | 0 | int64_t s21; |
1371 | 0 | int64_t s22; |
1372 | 0 | int64_t s23; |
1373 | 0 | int64_t carry0; |
1374 | 0 | int64_t carry1; |
1375 | 0 | int64_t carry2; |
1376 | 0 | int64_t carry3; |
1377 | 0 | int64_t carry4; |
1378 | 0 | int64_t carry5; |
1379 | 0 | int64_t carry6; |
1380 | 0 | int64_t carry7; |
1381 | 0 | int64_t carry8; |
1382 | 0 | int64_t carry9; |
1383 | 0 | int64_t carry10; |
1384 | 0 | int64_t carry11; |
1385 | 0 | int64_t carry12; |
1386 | 0 | int64_t carry13; |
1387 | 0 | int64_t carry14; |
1388 | 0 | int64_t carry15; |
1389 | 0 | int64_t carry16; |
1390 | 0 | int64_t carry17; |
1391 | 0 | int64_t carry18; |
1392 | 0 | int64_t carry19; |
1393 | 0 | int64_t carry20; |
1394 | 0 | int64_t carry21; |
1395 | 0 | int64_t carry22; |
1396 | |
|
1397 | 0 | s0 = c0 + a0 * b0; |
1398 | 0 | s1 = c1 + a0 * b1 + a1 * b0; |
1399 | 0 | s2 = c2 + a0 * b2 + a1 * b1 + a2 * b0; |
1400 | 0 | s3 = c3 + a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0; |
1401 | 0 | s4 = c4 + a0 * b4 + a1 * b3 + a2 * b2 + a3 * b1 + a4 * b0; |
1402 | 0 | s5 = c5 + a0 * b5 + a1 * b4 + a2 * b3 + a3 * b2 + a4 * b1 + a5 * b0; |
1403 | 0 | s6 = c6 + a0 * b6 + a1 * b5 + a2 * b4 + a3 * b3 + a4 * b2 + a5 * b1 + a6 * b0; |
1404 | 0 | s7 = c7 + a0 * b7 + a1 * b6 + a2 * b5 + a3 * b4 + a4 * b3 + a5 * b2 + |
1405 | 0 | a6 * b1 + a7 * b0; |
1406 | 0 | s8 = c8 + a0 * b8 + a1 * b7 + a2 * b6 + a3 * b5 + a4 * b4 + a5 * b3 + |
1407 | 0 | a6 * b2 + a7 * b1 + a8 * b0; |
1408 | 0 | s9 = c9 + a0 * b9 + a1 * b8 + a2 * b7 + a3 * b6 + a4 * b5 + a5 * b4 + |
1409 | 0 | a6 * b3 + a7 * b2 + a8 * b1 + a9 * b0; |
1410 | 0 | s10 = c10 + a0 * b10 + a1 * b9 + a2 * b8 + a3 * b7 + a4 * b6 + a5 * b5 + |
1411 | 0 | a6 * b4 + a7 * b3 + a8 * b2 + a9 * b1 + a10 * b0; |
1412 | 0 | s11 = c11 + a0 * b11 + a1 * b10 + a2 * b9 + a3 * b8 + a4 * b7 + a5 * b6 + |
1413 | 0 | a6 * b5 + a7 * b4 + a8 * b3 + a9 * b2 + a10 * b1 + a11 * b0; |
1414 | 0 | s12 = a1 * b11 + a2 * b10 + a3 * b9 + a4 * b8 + a5 * b7 + a6 * b6 + a7 * b5 + |
1415 | 0 | a8 * b4 + a9 * b3 + a10 * b2 + a11 * b1; |
1416 | 0 | s13 = a2 * b11 + a3 * b10 + a4 * b9 + a5 * b8 + a6 * b7 + a7 * b6 + a8 * b5 + |
1417 | 0 | a9 * b4 + a10 * b3 + a11 * b2; |
1418 | 0 | s14 = a3 * b11 + a4 * b10 + a5 * b9 + a6 * b8 + a7 * b7 + a8 * b6 + a9 * b5 + |
1419 | 0 | a10 * b4 + a11 * b3; |
1420 | 0 | s15 = a4 * b11 + a5 * b10 + a6 * b9 + a7 * b8 + a8 * b7 + a9 * b6 + a10 * b5 + |
1421 | 0 | a11 * b4; |
1422 | 0 | s16 = a5 * b11 + a6 * b10 + a7 * b9 + a8 * b8 + a9 * b7 + a10 * b6 + a11 * b5; |
1423 | 0 | s17 = a6 * b11 + a7 * b10 + a8 * b9 + a9 * b8 + a10 * b7 + a11 * b6; |
1424 | 0 | s18 = a7 * b11 + a8 * b10 + a9 * b9 + a10 * b8 + a11 * b7; |
1425 | 0 | s19 = a8 * b11 + a9 * b10 + a10 * b9 + a11 * b8; |
1426 | 0 | s20 = a9 * b11 + a10 * b10 + a11 * b9; |
1427 | 0 | s21 = a10 * b11 + a11 * b10; |
1428 | 0 | s22 = a11 * b11; |
1429 | 0 | s23 = 0; |
1430 | |
|
1431 | 0 | carry0 = (s0 + (1 << 20)) >> 21; |
1432 | 0 | s1 += carry0; |
1433 | 0 | s0 -= int64_lshift21(carry0); |
1434 | 0 | carry2 = (s2 + (1 << 20)) >> 21; |
1435 | 0 | s3 += carry2; |
1436 | 0 | s2 -= int64_lshift21(carry2); |
1437 | 0 | carry4 = (s4 + (1 << 20)) >> 21; |
1438 | 0 | s5 += carry4; |
1439 | 0 | s4 -= int64_lshift21(carry4); |
1440 | 0 | carry6 = (s6 + (1 << 20)) >> 21; |
1441 | 0 | s7 += carry6; |
1442 | 0 | s6 -= int64_lshift21(carry6); |
1443 | 0 | carry8 = (s8 + (1 << 20)) >> 21; |
1444 | 0 | s9 += carry8; |
1445 | 0 | s8 -= int64_lshift21(carry8); |
1446 | 0 | carry10 = (s10 + (1 << 20)) >> 21; |
1447 | 0 | s11 += carry10; |
1448 | 0 | s10 -= int64_lshift21(carry10); |
1449 | 0 | carry12 = (s12 + (1 << 20)) >> 21; |
1450 | 0 | s13 += carry12; |
1451 | 0 | s12 -= int64_lshift21(carry12); |
1452 | 0 | carry14 = (s14 + (1 << 20)) >> 21; |
1453 | 0 | s15 += carry14; |
1454 | 0 | s14 -= int64_lshift21(carry14); |
1455 | 0 | carry16 = (s16 + (1 << 20)) >> 21; |
1456 | 0 | s17 += carry16; |
1457 | 0 | s16 -= int64_lshift21(carry16); |
1458 | 0 | carry18 = (s18 + (1 << 20)) >> 21; |
1459 | 0 | s19 += carry18; |
1460 | 0 | s18 -= int64_lshift21(carry18); |
1461 | 0 | carry20 = (s20 + (1 << 20)) >> 21; |
1462 | 0 | s21 += carry20; |
1463 | 0 | s20 -= int64_lshift21(carry20); |
1464 | 0 | carry22 = (s22 + (1 << 20)) >> 21; |
1465 | 0 | s23 += carry22; |
1466 | 0 | s22 -= int64_lshift21(carry22); |
1467 | |
|
1468 | 0 | carry1 = (s1 + (1 << 20)) >> 21; |
1469 | 0 | s2 += carry1; |
1470 | 0 | s1 -= int64_lshift21(carry1); |
1471 | 0 | carry3 = (s3 + (1 << 20)) >> 21; |
1472 | 0 | s4 += carry3; |
1473 | 0 | s3 -= int64_lshift21(carry3); |
1474 | 0 | carry5 = (s5 + (1 << 20)) >> 21; |
1475 | 0 | s6 += carry5; |
1476 | 0 | s5 -= int64_lshift21(carry5); |
1477 | 0 | carry7 = (s7 + (1 << 20)) >> 21; |
1478 | 0 | s8 += carry7; |
1479 | 0 | s7 -= int64_lshift21(carry7); |
1480 | 0 | carry9 = (s9 + (1 << 20)) >> 21; |
1481 | 0 | s10 += carry9; |
1482 | 0 | s9 -= int64_lshift21(carry9); |
1483 | 0 | carry11 = (s11 + (1 << 20)) >> 21; |
1484 | 0 | s12 += carry11; |
1485 | 0 | s11 -= int64_lshift21(carry11); |
1486 | 0 | carry13 = (s13 + (1 << 20)) >> 21; |
1487 | 0 | s14 += carry13; |
1488 | 0 | s13 -= int64_lshift21(carry13); |
1489 | 0 | carry15 = (s15 + (1 << 20)) >> 21; |
1490 | 0 | s16 += carry15; |
1491 | 0 | s15 -= int64_lshift21(carry15); |
1492 | 0 | carry17 = (s17 + (1 << 20)) >> 21; |
1493 | 0 | s18 += carry17; |
1494 | 0 | s17 -= int64_lshift21(carry17); |
1495 | 0 | carry19 = (s19 + (1 << 20)) >> 21; |
1496 | 0 | s20 += carry19; |
1497 | 0 | s19 -= int64_lshift21(carry19); |
1498 | 0 | carry21 = (s21 + (1 << 20)) >> 21; |
1499 | 0 | s22 += carry21; |
1500 | 0 | s21 -= int64_lshift21(carry21); |
1501 | |
|
1502 | 0 | s11 += s23 * 666643; |
1503 | 0 | s12 += s23 * 470296; |
1504 | 0 | s13 += s23 * 654183; |
1505 | 0 | s14 -= s23 * 997805; |
1506 | 0 | s15 += s23 * 136657; |
1507 | 0 | s16 -= s23 * 683901; |
1508 | 0 | s23 = 0; |
1509 | |
|
1510 | 0 | s10 += s22 * 666643; |
1511 | 0 | s11 += s22 * 470296; |
1512 | 0 | s12 += s22 * 654183; |
1513 | 0 | s13 -= s22 * 997805; |
1514 | 0 | s14 += s22 * 136657; |
1515 | 0 | s15 -= s22 * 683901; |
1516 | 0 | s22 = 0; |
1517 | |
|
1518 | 0 | s9 += s21 * 666643; |
1519 | 0 | s10 += s21 * 470296; |
1520 | 0 | s11 += s21 * 654183; |
1521 | 0 | s12 -= s21 * 997805; |
1522 | 0 | s13 += s21 * 136657; |
1523 | 0 | s14 -= s21 * 683901; |
1524 | 0 | s21 = 0; |
1525 | |
|
1526 | 0 | s8 += s20 * 666643; |
1527 | 0 | s9 += s20 * 470296; |
1528 | 0 | s10 += s20 * 654183; |
1529 | 0 | s11 -= s20 * 997805; |
1530 | 0 | s12 += s20 * 136657; |
1531 | 0 | s13 -= s20 * 683901; |
1532 | 0 | s20 = 0; |
1533 | |
|
1534 | 0 | s7 += s19 * 666643; |
1535 | 0 | s8 += s19 * 470296; |
1536 | 0 | s9 += s19 * 654183; |
1537 | 0 | s10 -= s19 * 997805; |
1538 | 0 | s11 += s19 * 136657; |
1539 | 0 | s12 -= s19 * 683901; |
1540 | 0 | s19 = 0; |
1541 | |
|
1542 | 0 | s6 += s18 * 666643; |
1543 | 0 | s7 += s18 * 470296; |
1544 | 0 | s8 += s18 * 654183; |
1545 | 0 | s9 -= s18 * 997805; |
1546 | 0 | s10 += s18 * 136657; |
1547 | 0 | s11 -= s18 * 683901; |
1548 | 0 | s18 = 0; |
1549 | |
|
1550 | 0 | carry6 = (s6 + (1 << 20)) >> 21; |
1551 | 0 | s7 += carry6; |
1552 | 0 | s6 -= int64_lshift21(carry6); |
1553 | 0 | carry8 = (s8 + (1 << 20)) >> 21; |
1554 | 0 | s9 += carry8; |
1555 | 0 | s8 -= int64_lshift21(carry8); |
1556 | 0 | carry10 = (s10 + (1 << 20)) >> 21; |
1557 | 0 | s11 += carry10; |
1558 | 0 | s10 -= int64_lshift21(carry10); |
1559 | 0 | carry12 = (s12 + (1 << 20)) >> 21; |
1560 | 0 | s13 += carry12; |
1561 | 0 | s12 -= int64_lshift21(carry12); |
1562 | 0 | carry14 = (s14 + (1 << 20)) >> 21; |
1563 | 0 | s15 += carry14; |
1564 | 0 | s14 -= int64_lshift21(carry14); |
1565 | 0 | carry16 = (s16 + (1 << 20)) >> 21; |
1566 | 0 | s17 += carry16; |
1567 | 0 | s16 -= int64_lshift21(carry16); |
1568 | |
|
1569 | 0 | carry7 = (s7 + (1 << 20)) >> 21; |
1570 | 0 | s8 += carry7; |
1571 | 0 | s7 -= int64_lshift21(carry7); |
1572 | 0 | carry9 = (s9 + (1 << 20)) >> 21; |
1573 | 0 | s10 += carry9; |
1574 | 0 | s9 -= int64_lshift21(carry9); |
1575 | 0 | carry11 = (s11 + (1 << 20)) >> 21; |
1576 | 0 | s12 += carry11; |
1577 | 0 | s11 -= int64_lshift21(carry11); |
1578 | 0 | carry13 = (s13 + (1 << 20)) >> 21; |
1579 | 0 | s14 += carry13; |
1580 | 0 | s13 -= int64_lshift21(carry13); |
1581 | 0 | carry15 = (s15 + (1 << 20)) >> 21; |
1582 | 0 | s16 += carry15; |
1583 | 0 | s15 -= int64_lshift21(carry15); |
1584 | |
|
1585 | 0 | s5 += s17 * 666643; |
1586 | 0 | s6 += s17 * 470296; |
1587 | 0 | s7 += s17 * 654183; |
1588 | 0 | s8 -= s17 * 997805; |
1589 | 0 | s9 += s17 * 136657; |
1590 | 0 | s10 -= s17 * 683901; |
1591 | 0 | s17 = 0; |
1592 | |
|
1593 | 0 | s4 += s16 * 666643; |
1594 | 0 | s5 += s16 * 470296; |
1595 | 0 | s6 += s16 * 654183; |
1596 | 0 | s7 -= s16 * 997805; |
1597 | 0 | s8 += s16 * 136657; |
1598 | 0 | s9 -= s16 * 683901; |
1599 | 0 | s16 = 0; |
1600 | |
|
1601 | 0 | s3 += s15 * 666643; |
1602 | 0 | s4 += s15 * 470296; |
1603 | 0 | s5 += s15 * 654183; |
1604 | 0 | s6 -= s15 * 997805; |
1605 | 0 | s7 += s15 * 136657; |
1606 | 0 | s8 -= s15 * 683901; |
1607 | 0 | s15 = 0; |
1608 | |
|
1609 | 0 | s2 += s14 * 666643; |
1610 | 0 | s3 += s14 * 470296; |
1611 | 0 | s4 += s14 * 654183; |
1612 | 0 | s5 -= s14 * 997805; |
1613 | 0 | s6 += s14 * 136657; |
1614 | 0 | s7 -= s14 * 683901; |
1615 | 0 | s14 = 0; |
1616 | |
|
1617 | 0 | s1 += s13 * 666643; |
1618 | 0 | s2 += s13 * 470296; |
1619 | 0 | s3 += s13 * 654183; |
1620 | 0 | s4 -= s13 * 997805; |
1621 | 0 | s5 += s13 * 136657; |
1622 | 0 | s6 -= s13 * 683901; |
1623 | 0 | s13 = 0; |
1624 | |
|
1625 | 0 | s0 += s12 * 666643; |
1626 | 0 | s1 += s12 * 470296; |
1627 | 0 | s2 += s12 * 654183; |
1628 | 0 | s3 -= s12 * 997805; |
1629 | 0 | s4 += s12 * 136657; |
1630 | 0 | s5 -= s12 * 683901; |
1631 | 0 | s12 = 0; |
1632 | |
|
1633 | 0 | carry0 = (s0 + (1 << 20)) >> 21; |
1634 | 0 | s1 += carry0; |
1635 | 0 | s0 -= int64_lshift21(carry0); |
1636 | 0 | carry2 = (s2 + (1 << 20)) >> 21; |
1637 | 0 | s3 += carry2; |
1638 | 0 | s2 -= int64_lshift21(carry2); |
1639 | 0 | carry4 = (s4 + (1 << 20)) >> 21; |
1640 | 0 | s5 += carry4; |
1641 | 0 | s4 -= int64_lshift21(carry4); |
1642 | 0 | carry6 = (s6 + (1 << 20)) >> 21; |
1643 | 0 | s7 += carry6; |
1644 | 0 | s6 -= int64_lshift21(carry6); |
1645 | 0 | carry8 = (s8 + (1 << 20)) >> 21; |
1646 | 0 | s9 += carry8; |
1647 | 0 | s8 -= int64_lshift21(carry8); |
1648 | 0 | carry10 = (s10 + (1 << 20)) >> 21; |
1649 | 0 | s11 += carry10; |
1650 | 0 | s10 -= int64_lshift21(carry10); |
1651 | |
|
1652 | 0 | carry1 = (s1 + (1 << 20)) >> 21; |
1653 | 0 | s2 += carry1; |
1654 | 0 | s1 -= int64_lshift21(carry1); |
1655 | 0 | carry3 = (s3 + (1 << 20)) >> 21; |
1656 | 0 | s4 += carry3; |
1657 | 0 | s3 -= int64_lshift21(carry3); |
1658 | 0 | carry5 = (s5 + (1 << 20)) >> 21; |
1659 | 0 | s6 += carry5; |
1660 | 0 | s5 -= int64_lshift21(carry5); |
1661 | 0 | carry7 = (s7 + (1 << 20)) >> 21; |
1662 | 0 | s8 += carry7; |
1663 | 0 | s7 -= int64_lshift21(carry7); |
1664 | 0 | carry9 = (s9 + (1 << 20)) >> 21; |
1665 | 0 | s10 += carry9; |
1666 | 0 | s9 -= int64_lshift21(carry9); |
1667 | 0 | carry11 = (s11 + (1 << 20)) >> 21; |
1668 | 0 | s12 += carry11; |
1669 | 0 | s11 -= int64_lshift21(carry11); |
1670 | |
|
1671 | 0 | s0 += s12 * 666643; |
1672 | 0 | s1 += s12 * 470296; |
1673 | 0 | s2 += s12 * 654183; |
1674 | 0 | s3 -= s12 * 997805; |
1675 | 0 | s4 += s12 * 136657; |
1676 | 0 | s5 -= s12 * 683901; |
1677 | 0 | s12 = 0; |
1678 | |
|
1679 | 0 | carry0 = s0 >> 21; |
1680 | 0 | s1 += carry0; |
1681 | 0 | s0 -= int64_lshift21(carry0); |
1682 | 0 | carry1 = s1 >> 21; |
1683 | 0 | s2 += carry1; |
1684 | 0 | s1 -= int64_lshift21(carry1); |
1685 | 0 | carry2 = s2 >> 21; |
1686 | 0 | s3 += carry2; |
1687 | 0 | s2 -= int64_lshift21(carry2); |
1688 | 0 | carry3 = s3 >> 21; |
1689 | 0 | s4 += carry3; |
1690 | 0 | s3 -= int64_lshift21(carry3); |
1691 | 0 | carry4 = s4 >> 21; |
1692 | 0 | s5 += carry4; |
1693 | 0 | s4 -= int64_lshift21(carry4); |
1694 | 0 | carry5 = s5 >> 21; |
1695 | 0 | s6 += carry5; |
1696 | 0 | s5 -= int64_lshift21(carry5); |
1697 | 0 | carry6 = s6 >> 21; |
1698 | 0 | s7 += carry6; |
1699 | 0 | s6 -= int64_lshift21(carry6); |
1700 | 0 | carry7 = s7 >> 21; |
1701 | 0 | s8 += carry7; |
1702 | 0 | s7 -= int64_lshift21(carry7); |
1703 | 0 | carry8 = s8 >> 21; |
1704 | 0 | s9 += carry8; |
1705 | 0 | s8 -= int64_lshift21(carry8); |
1706 | 0 | carry9 = s9 >> 21; |
1707 | 0 | s10 += carry9; |
1708 | 0 | s9 -= int64_lshift21(carry9); |
1709 | 0 | carry10 = s10 >> 21; |
1710 | 0 | s11 += carry10; |
1711 | 0 | s10 -= int64_lshift21(carry10); |
1712 | 0 | carry11 = s11 >> 21; |
1713 | 0 | s12 += carry11; |
1714 | 0 | s11 -= int64_lshift21(carry11); |
1715 | |
|
1716 | 0 | s0 += s12 * 666643; |
1717 | 0 | s1 += s12 * 470296; |
1718 | 0 | s2 += s12 * 654183; |
1719 | 0 | s3 -= s12 * 997805; |
1720 | 0 | s4 += s12 * 136657; |
1721 | 0 | s5 -= s12 * 683901; |
1722 | 0 | s12 = 0; |
1723 | |
|
1724 | 0 | carry0 = s0 >> 21; |
1725 | 0 | s1 += carry0; |
1726 | 0 | s0 -= int64_lshift21(carry0); |
1727 | 0 | carry1 = s1 >> 21; |
1728 | 0 | s2 += carry1; |
1729 | 0 | s1 -= int64_lshift21(carry1); |
1730 | 0 | carry2 = s2 >> 21; |
1731 | 0 | s3 += carry2; |
1732 | 0 | s2 -= int64_lshift21(carry2); |
1733 | 0 | carry3 = s3 >> 21; |
1734 | 0 | s4 += carry3; |
1735 | 0 | s3 -= int64_lshift21(carry3); |
1736 | 0 | carry4 = s4 >> 21; |
1737 | 0 | s5 += carry4; |
1738 | 0 | s4 -= int64_lshift21(carry4); |
1739 | 0 | carry5 = s5 >> 21; |
1740 | 0 | s6 += carry5; |
1741 | 0 | s5 -= int64_lshift21(carry5); |
1742 | 0 | carry6 = s6 >> 21; |
1743 | 0 | s7 += carry6; |
1744 | 0 | s6 -= int64_lshift21(carry6); |
1745 | 0 | carry7 = s7 >> 21; |
1746 | 0 | s8 += carry7; |
1747 | 0 | s7 -= int64_lshift21(carry7); |
1748 | 0 | carry8 = s8 >> 21; |
1749 | 0 | s9 += carry8; |
1750 | 0 | s8 -= int64_lshift21(carry8); |
1751 | 0 | carry9 = s9 >> 21; |
1752 | 0 | s10 += carry9; |
1753 | 0 | s9 -= int64_lshift21(carry9); |
1754 | 0 | carry10 = s10 >> 21; |
1755 | 0 | s11 += carry10; |
1756 | 0 | s10 -= int64_lshift21(carry10); |
1757 | |
|
1758 | 0 | s[0] = s0 >> 0; |
1759 | 0 | s[1] = s0 >> 8; |
1760 | 0 | s[2] = (s0 >> 16) | (s1 << 5); |
1761 | 0 | s[3] = s1 >> 3; |
1762 | 0 | s[4] = s1 >> 11; |
1763 | 0 | s[5] = (s1 >> 19) | (s2 << 2); |
1764 | 0 | s[6] = s2 >> 6; |
1765 | 0 | s[7] = (s2 >> 14) | (s3 << 7); |
1766 | 0 | s[8] = s3 >> 1; |
1767 | 0 | s[9] = s3 >> 9; |
1768 | 0 | s[10] = (s3 >> 17) | (s4 << 4); |
1769 | 0 | s[11] = s4 >> 4; |
1770 | 0 | s[12] = s4 >> 12; |
1771 | 0 | s[13] = (s4 >> 20) | (s5 << 1); |
1772 | 0 | s[14] = s5 >> 7; |
1773 | 0 | s[15] = (s5 >> 15) | (s6 << 6); |
1774 | 0 | s[16] = s6 >> 2; |
1775 | 0 | s[17] = s6 >> 10; |
1776 | 0 | s[18] = (s6 >> 18) | (s7 << 3); |
1777 | 0 | s[19] = s7 >> 5; |
1778 | 0 | s[20] = s7 >> 13; |
1779 | 0 | s[21] = s8 >> 0; |
1780 | 0 | s[22] = s8 >> 8; |
1781 | 0 | s[23] = (s8 >> 16) | (s9 << 5); |
1782 | 0 | s[24] = s9 >> 3; |
1783 | 0 | s[25] = s9 >> 11; |
1784 | 0 | s[26] = (s9 >> 19) | (s10 << 2); |
1785 | 0 | s[27] = s10 >> 6; |
1786 | 0 | s[28] = (s10 >> 14) | (s11 << 7); |
1787 | 0 | s[29] = s11 >> 1; |
1788 | 0 | s[30] = s11 >> 9; |
1789 | 0 | s[31] = s11 >> 17; |
1790 | 0 | } |
1791 | | |
1792 | | |
1793 | | void x25519_scalar_mult_generic_masked(uint8_t out[32], |
1794 | | const uint8_t scalar_masked[32], |
1795 | 0 | const uint8_t point[32]) { |
1796 | 0 | fe x1, x2, z2, x3, z3, tmp0, tmp1; |
1797 | 0 | fe_loose x2l, z2l, x3l, tmp0l, tmp1l; |
1798 | |
|
1799 | 0 | uint8_t e[32]; |
1800 | 0 | OPENSSL_memcpy(e, scalar_masked, 32); |
1801 | | // The following implementation was transcribed to Coq and proven to |
1802 | | // correspond to unary scalar multiplication in affine coordinates given that |
1803 | | // x1 != 0 is the x coordinate of some point on the curve. It was also checked |
1804 | | // in Coq that doing a ladderstep with x1 = x3 = 0 gives z2' = z3' = 0, and z2 |
1805 | | // = z3 = 0 gives z2' = z3' = 0. The statement was quantified over the |
1806 | | // underlying field, so it applies to Curve25519 itself and the quadratic |
1807 | | // twist of Curve25519. It was not proven in Coq that prime-field arithmetic |
1808 | | // correctly simulates extension-field arithmetic on prime-field values. |
1809 | | // The decoding of the byte array representation of e was not considered. |
1810 | | // Specification of Montgomery curves in affine coordinates: |
1811 | | // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Spec/MontgomeryCurve.v#L27> |
1812 | | // Proof that these form a group that is isomorphic to a Weierstrass curve: |
1813 | | // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/AffineProofs.v#L35> |
1814 | | // Coq transcription and correctness proof of the loop (where scalarbits=255): |
1815 | | // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZ.v#L118> |
1816 | | // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L278> |
1817 | | // preconditions: 0 <= e < 2^255 (not necessarily e < order), fe_invert(0) = 0 |
1818 | 0 | fe_frombytes(&x1, point); |
1819 | 0 | fe_1(&x2); |
1820 | 0 | fe_0(&z2); |
1821 | 0 | fe_copy(&x3, &x1); |
1822 | 0 | fe_1(&z3); |
1823 | |
|
1824 | 0 | unsigned swap = 0; |
1825 | 0 | int pos; |
1826 | 0 | for (pos = 254; pos >= 0; --pos) { |
1827 | | // loop invariant as of right before the test, for the case where x1 != 0: |
1828 | | // pos >= -1; if z2 = 0 then x2 is nonzero; if z3 = 0 then x3 is nonzero |
1829 | | // let r := e >> (pos+1) in the following equalities of projective points: |
1830 | | // to_xz (r*P) === if swap then (x3, z3) else (x2, z2) |
1831 | | // to_xz ((r+1)*P) === if swap then (x2, z2) else (x3, z3) |
1832 | | // x1 is the nonzero x coordinate of the nonzero point (r*P-(r+1)*P) |
1833 | 0 | unsigned b = 1 & (e[pos / 8] >> (pos & 7)); |
1834 | 0 | swap ^= b; |
1835 | 0 | fe_cswap(&x2, &x3, swap); |
1836 | 0 | fe_cswap(&z2, &z3, swap); |
1837 | 0 | swap = b; |
1838 | | // Coq transcription of ladderstep formula (called from transcribed loop): |
1839 | | // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZ.v#L89> |
1840 | | // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L131> |
1841 | | // x1 != 0 <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L217> |
1842 | | // x1 = 0 <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L147> |
1843 | 0 | fe_sub(&tmp0l, &x3, &z3); |
1844 | 0 | fe_sub(&tmp1l, &x2, &z2); |
1845 | 0 | fe_add(&x2l, &x2, &z2); |
1846 | 0 | fe_add(&z2l, &x3, &z3); |
1847 | 0 | fe_mul_tll(&z3, &tmp0l, &x2l); |
1848 | 0 | fe_mul_tll(&z2, &z2l, &tmp1l); |
1849 | 0 | fe_sq_tl(&tmp0, &tmp1l); |
1850 | 0 | fe_sq_tl(&tmp1, &x2l); |
1851 | 0 | fe_add(&x3l, &z3, &z2); |
1852 | 0 | fe_sub(&z2l, &z3, &z2); |
1853 | 0 | fe_mul_ttt(&x2, &tmp1, &tmp0); |
1854 | 0 | fe_sub(&tmp1l, &tmp1, &tmp0); |
1855 | 0 | fe_sq_tl(&z2, &z2l); |
1856 | 0 | fe_mul121666(&z3, &tmp1l); |
1857 | 0 | fe_sq_tl(&x3, &x3l); |
1858 | 0 | fe_add(&tmp0l, &tmp0, &z3); |
1859 | 0 | fe_mul_ttt(&z3, &x1, &z2); |
1860 | 0 | fe_mul_tll(&z2, &tmp1l, &tmp0l); |
1861 | 0 | } |
1862 | | // here pos=-1, so r=e, so to_xz (e*P) === if swap then (x3, z3) else (x2, z2) |
1863 | 0 | fe_cswap(&x2, &x3, swap); |
1864 | 0 | fe_cswap(&z2, &z3, swap); |
1865 | |
|
1866 | 0 | fe_invert(&z2, &z2); |
1867 | 0 | fe_mul_ttt(&x2, &x2, &z2); |
1868 | 0 | fe_tobytes(out, &x2); |
1869 | 0 | } |
1870 | | |
1871 | | void x25519_public_from_private_generic_masked(uint8_t out_public_value[32], |
1872 | | const uint8_t private_key_masked[32], |
1873 | 0 | int use_adx) { |
1874 | 0 | uint8_t e[32]; |
1875 | 0 | OPENSSL_memcpy(e, private_key_masked, 32); |
1876 | |
|
1877 | 0 | ge_p3 A; |
1878 | 0 | x25519_ge_scalarmult_base(&A, e, use_adx); |
1879 | | |
1880 | | // We only need the u-coordinate of the curve25519 point. The map is |
1881 | | // u=(y+1)/(1-y). Since y=Y/Z, this gives u=(Z+Y)/(Z-Y). |
1882 | 0 | fe_loose zplusy, zminusy; |
1883 | 0 | fe zminusy_inv; |
1884 | 0 | fe_add(&zplusy, &A.Z, &A.Y); |
1885 | 0 | fe_sub(&zminusy, &A.Z, &A.Y); |
1886 | 0 | fe_loose_invert(&zminusy_inv, &zminusy); |
1887 | 0 | fe_mul_tlt(&zminusy_inv, &zplusy, &zminusy_inv); |
1888 | 0 | fe_tobytes(out_public_value, &zminusy_inv); |
1889 | 0 | CONSTTIME_DECLASSIFY(out_public_value, 32); |
1890 | 0 | } |
1891 | | |
1892 | 0 | void x25519_fe_invert(fe *out, const fe *z) { |
1893 | 0 | fe_invert(out, z); |
1894 | 0 | } |
1895 | | |
1896 | 0 | uint8_t x25519_fe_isnegative(const fe *f) { |
1897 | 0 | return (uint8_t)fe_isnegative(f); |
1898 | 0 | } |
1899 | | |
1900 | 0 | void x25519_fe_mul_ttt(fe *h, const fe *f, const fe *g) { |
1901 | 0 | fe_mul_ttt(h, f, g); |
1902 | 0 | } |
1903 | | |
1904 | 0 | void x25519_fe_neg(fe *f) { |
1905 | 0 | fe_loose t; |
1906 | 0 | fe_neg(&t, f); |
1907 | 0 | fe_carry(f, &t); |
1908 | 0 | } |
1909 | | |
1910 | 0 | void x25519_fe_tobytes(uint8_t s[32], const fe *h) { |
1911 | 0 | fe_tobytes(s, h); |
1912 | 0 | } |
1913 | | |
1914 | | void x25519_ge_double_scalarmult_vartime(ge_p2 *r, const uint8_t *a, |
1915 | 0 | const ge_p3 *A, const uint8_t *b) { |
1916 | 0 | ge_double_scalarmult_vartime(r, a, A, b); |
1917 | 0 | } |
1918 | | |
1919 | 0 | void x25519_sc_mask(uint8_t a[32]) { |
1920 | 0 | a[0] &= 248; |
1921 | 0 | a[31] &= 127; |
1922 | 0 | a[31] |= 64; |
1923 | 0 | } |
1924 | | |
1925 | | void x25519_sc_muladd(uint8_t *s, const uint8_t *a, const uint8_t *b, |
1926 | 0 | const uint8_t *c) { |
1927 | 0 | sc_muladd(s, a, b, c); |
1928 | 0 | } |