Run util/openssl-format-source on the Curve448 code
[openssl.git] / crypto / ec / curve448 / arch_32 / f_impl.c
1 /*
2  * Copyright 2017 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 "f_field.h"
14
15 #if (defined(__OPTIMIZE__) && !defined(__OPTIMIZE_SIZE__) && !I_HATE_UNROLLED_LOOPS) \
16      || defined(DECAF_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
28     uint64_t accum0 = 0, accum1 = 0, accum2 = 0;
29     uint32_t mask = (1 << 28) - 1;
30
31     uint32_t aa[8], bb[8];
32
33     int i, j;
34     for (i = 0; i < 8; i++) {
35         aa[i] = a[i] + a[i + 8];
36         bb[i] = b[i] + b[i + 8];
37     }
38
39     FOR_LIMB(j, 0, 8, {
40              accum2 = 0;
41              FOR_LIMB(i, 0, j + 1, {
42                       accum2 += widemul(a[j - i], b[i]);
43                       accum1 += widemul(aa[j - i], bb[i]);
44                       accum0 += widemul(a[8 + j - i], b[8 + i]);
45                       }
46              ); accum1 -= accum2; accum0 += accum2;
47              accum2 = 0;
48              FOR_LIMB(i, j + 1, 8, {
49                       accum0 -=
50                       widemul(a[8 + j - i], b[i]);
51                       accum2 +=
52                       widemul(aa[8 + j - i],
53                               bb[i]);
54                       accum1 += widemul(a[16 + j - i], b[8 + i]);
55                       }
56              );
57              accum1 += accum2;
58              accum0 += accum2;
59              c[j] = ((uint32_t)(accum0)) & mask;
60              c[j + 8] = ((uint32_t)(accum1)) & mask;
61              accum0 >>= 28; accum1 >>= 28;
62              });
63
64     accum0 += accum1;
65     accum0 += c[8];
66     accum1 += c[0];
67     c[8] = ((uint32_t)(accum0)) & mask;
68     c[0] = ((uint32_t)(accum1)) & mask;
69
70     accum0 >>= 28;
71     accum1 >>= 28;
72     c[9] += ((uint32_t)(accum0));
73     c[1] += ((uint32_t)(accum1));
74 }
75
76 void gf_mulw_unsigned(gf_s * __restrict__ cs, const gf as, uint32_t b)
77 {
78     const uint32_t *a = as->limb;
79     uint32_t *c = cs->limb;
80     uint64_t accum0 = 0, accum8 = 0;
81     uint32_t mask = (1 << 28) - 1;
82     int i;
83
84     assert(b < 1 << 28);
85
86     FOR_LIMB(i, 0, 8, {
87              accum0 += widemul(b, a[i]); accum8 += widemul(b, a[i + 8]);
88              c[i] = accum0 & mask; accum0 >>= 28;
89              c[i + 8] = accum8 & mask; accum8 >>= 28;
90              });
91
92     accum0 += accum8 + c[8];
93     c[8] = accum0 & mask;
94     c[9] += accum0 >> 28;
95
96     accum8 += c[0];
97     c[0] = accum8 & mask;
98     c[1] += accum8 >> 28;
99 }
100
101 void gf_sqr(gf_s * __restrict__ cs, const gf as)
102 {
103     gf_mul(cs, as, as);         /* Performs better with a dedicated square */
104 }