78f788b5b1efb9ae79dade2ca1e38b1ac5a60ad7
[openssl.git] / crypto / ec / curve448 / arch_32 / f_impl.c
1 /*
2  * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright 2014 Cryptography Research, Inc.
4  *
5  * Licensed under the OpenSSL license (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  *
10  * Originally written by Mike Hamburg
11  */
12
13 #include "field.h"
14
15 #if (defined(__OPTIMIZE__) && !defined(__OPTIMIZE_SIZE__) && !I_HATE_UNROLLED_LOOPS) \
16      || defined(C448_FORCE_UNROLL)
17 # define REPEAT8(_x) _x _x _x _x _x _x _x _x
18 # define FOR_LIMB(_i,_start,_end,_x) do { _i=_start; REPEAT8( if (_i<_end) { _x; } _i++;) } while (0)
19 #else
20 # define FOR_LIMB(_i,_start,_end,_x) do { for (_i=_start; _i<_end; _i++) _x; } while (0)
21 #endif
22
23 void gf_mul(gf_s * RESTRICT cs, const gf as, const gf bs)
24 {
25     const uint32_t *a = as->limb, *b = bs->limb;
26     uint32_t *c = cs->limb;
27     uint64_t accum0 = 0, accum1 = 0, accum2 = 0;
28     uint32_t mask = (1 << 28) - 1;
29     uint32_t aa[8], bb[8];
30     int i, j;
31
32     for (i = 0; i < 8; i++) {
33         aa[i] = a[i] + a[i + 8];
34         bb[i] = b[i] + b[i + 8];
35     }
36
37     FOR_LIMB(j, 0, 8, {
38              accum2 = 0;
39              FOR_LIMB(i, 0, j + 1, {
40                       accum2 += widemul(a[j - i], b[i]);
41                       accum1 += widemul(aa[j - i], bb[i]);
42                       accum0 += widemul(a[8 + j - i], b[8 + i]);
43                       }
44              ); accum1 -= accum2; accum0 += accum2;
45              accum2 = 0;
46              FOR_LIMB(i, j + 1, 8, {
47                       accum0 -=
48                       widemul(a[8 + j - i], b[i]);
49                       accum2 +=
50                       widemul(aa[8 + j - i],
51                               bb[i]);
52                       accum1 += widemul(a[16 + j - i], b[8 + i]);
53                       }
54              );
55              accum1 += accum2;
56              accum0 += accum2;
57              c[j] = ((uint32_t)(accum0)) & mask;
58              c[j + 8] = ((uint32_t)(accum1)) & mask;
59              accum0 >>= 28; accum1 >>= 28;
60              });
61
62     accum0 += accum1;
63     accum0 += c[8];
64     accum1 += c[0];
65     c[8] = ((uint32_t)(accum0)) & mask;
66     c[0] = ((uint32_t)(accum1)) & mask;
67
68     accum0 >>= 28;
69     accum1 >>= 28;
70     c[9] += ((uint32_t)(accum0));
71     c[1] += ((uint32_t)(accum1));
72 }
73
74 void gf_mulw_unsigned(gf_s * RESTRICT cs, const gf as, uint32_t b)
75 {
76     const uint32_t *a = as->limb;
77     uint32_t *c = cs->limb;
78     uint64_t accum0 = 0, accum8 = 0;
79     uint32_t mask = (1 << 28) - 1;
80     int i;
81
82     assert(b < 1 << 28);
83
84     FOR_LIMB(i, 0, 8, {
85              accum0 += widemul(b, a[i]); accum8 += widemul(b, a[i + 8]);
86              c[i] = accum0 & mask; accum0 >>= 28;
87              c[i + 8] = accum8 & mask; accum8 >>= 28;
88              });
89
90     accum0 += accum8 + c[8];
91     c[8] = ((uint32_t)accum0) & mask;
92     c[9] += (uint32_t)(accum0 >> 28);
93
94     accum8 += c[0];
95     c[0] = ((uint32_t)accum8) & mask;
96     c[1] += (uint32_t)(accum8 >> 28);
97 }
98
99 void gf_sqr(gf_s * RESTRICT cs, const gf as)
100 {
101     gf_mul(cs, as, as);         /* Performs better with a dedicated square */
102 }