4320909f3300cd5d5534f261dc55a6975f30c647
[openssl.git] / crypto / ec / curve448 / word.h
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 #ifndef __WORD_H__
14 # define __WORD_H__
15
16 # include <string.h>
17
18 # include <assert.h>
19 # include <openssl/e_os2.h>
20 # include "arch_intrinsics.h"
21
22 # include "curve448utils.h"
23
24 # ifndef _BSD_SOURCE
25 #  define _BSD_SOURCE 1
26 # endif
27
28 # ifndef _DEFAULT_SOURCE
29 #  define _DEFAULT_SOURCE 1
30 # endif
31
32 # include <stdlib.h>
33
34 # if defined(__ARM_NEON__)
35 #  include <arm_neon.h>
36 # elif defined(__SSE2__)
37 #  if !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 5 || (__GNUC__==4 && __GNUC_MINOR__ >= 4)
38 #   include <immintrin.h>
39 #  else
40 #   include <emmintrin.h>
41 #  endif
42 # endif
43
44 # if (ARCH_WORD_BITS == 64)
45 typedef uint64_t word_t, mask_t;
46 typedef __uint128_t dword_t;
47 typedef int32_t hsword_t;
48 typedef int64_t sword_t;
49 typedef __int128_t dsword_t;
50 # elif (ARCH_WORD_BITS == 32)
51 typedef uint32_t word_t, mask_t;
52 typedef uint64_t dword_t;
53 typedef int16_t hsword_t;
54 typedef int32_t sword_t;
55 typedef int64_t dsword_t;
56 # else
57 #  error "For now, we only support 32- and 64-bit architectures."
58 # endif
59
60 /*
61  * Scalar limbs are keyed off of the API word size instead of the arch word
62  * size.
63  */
64 # if C448_WORD_BITS == 64
65 #  define SC_LIMB(x) (x)
66 # elif C448_WORD_BITS == 32
67 #  define SC_LIMB(x) ((uint32_t)x),(x>>32)
68 # else
69 #  error "For now we only support 32- and 64-bit architectures."
70 # endif
71
72 # ifdef __ARM_NEON__
73 typedef uint32x4_t vecmask_t;
74 # elif defined(__clang__)
75 typedef uint64_t uint64x2_t __attribute__ ((ext_vector_type(2)));
76 typedef int64_t int64x2_t __attribute__ ((ext_vector_type(2)));
77 typedef uint64_t uint64x4_t __attribute__ ((ext_vector_type(4)));
78 typedef int64_t int64x4_t __attribute__ ((ext_vector_type(4)));
79 typedef uint32_t uint32x4_t __attribute__ ((ext_vector_type(4)));
80 typedef int32_t int32x4_t __attribute__ ((ext_vector_type(4)));
81 typedef uint32_t uint32x2_t __attribute__ ((ext_vector_type(2)));
82 typedef int32_t int32x2_t __attribute__ ((ext_vector_type(2)));
83 typedef uint32_t uint32x8_t __attribute__ ((ext_vector_type(8)));
84 typedef int32_t int32x8_t __attribute__ ((ext_vector_type(8)));
85 typedef word_t vecmask_t __attribute__ ((ext_vector_type(4)));
86 # else                          /* GCC, hopefully? */
87 typedef uint64_t uint64x2_t __attribute__ ((vector_size(16)));
88 typedef int64_t int64x2_t __attribute__ ((vector_size(16)));
89 typedef uint64_t uint64x4_t __attribute__ ((vector_size(32)));
90 typedef int64_t int64x4_t __attribute__ ((vector_size(32)));
91 typedef uint32_t uint32x4_t __attribute__ ((vector_size(16)));
92 typedef int32_t int32x4_t __attribute__ ((vector_size(16)));
93 typedef uint32_t uint32x2_t __attribute__ ((vector_size(8)));
94 typedef int32_t int32x2_t __attribute__ ((vector_size(8)));
95 typedef uint32_t uint32x8_t __attribute__ ((vector_size(32)));
96 typedef int32_t int32x8_t __attribute__ ((vector_size(32)));
97 typedef word_t vecmask_t __attribute__ ((vector_size(32)));
98 # endif
99
100 # if defined(__AVX2__)
101 #  define VECTOR_ALIGNED __attribute__((aligned(32)))
102 typedef uint32x8_t big_register_t;
103 typedef uint64x4_t uint64xn_t;
104 typedef uint32x8_t uint32xn_t;
105
106 static ossl_inline big_register_t br_set_to_mask(mask_t x)
107 {
108     uint32_t y = (uint32_t)x;
109     big_register_t ret = { y, y, y, y, y, y, y, y };
110     return ret;
111 }
112 # elif defined(__SSE2__)
113 #  define VECTOR_ALIGNED __attribute__((aligned(16)))
114 typedef uint32x4_t big_register_t;
115 typedef uint64x2_t uint64xn_t;
116 typedef uint32x4_t uint32xn_t;
117
118 static ossl_inline big_register_t br_set_to_mask(mask_t x)
119 {
120     uint32_t y = x;
121     big_register_t ret = { y, y, y, y };
122     return ret;
123 }
124 # elif defined(__ARM_NEON__)
125 #  define VECTOR_ALIGNED __attribute__((aligned(16)))
126 typedef uint32x4_t big_register_t;
127 typedef uint64x2_t uint64xn_t;
128 typedef uint32x4_t uint32xn_t;
129
130 static ossl_inline big_register_t br_set_to_mask(mask_t x)
131 {
132     return vdupq_n_u32(x);
133 }
134 # elif defined(_WIN64) || defined(__amd64__) || defined(__X86_64__) \
135       || defined(__aarch64__)
136 #  define VECTOR_ALIGNED __attribute__((aligned(8)))
137 typedef uint64_t big_register_t, uint64xn_t;
138
139 typedef uint32_t uint32xn_t;
140 static ossl_inline big_register_t br_set_to_mask(mask_t x)
141 {
142     return (big_register_t) x;
143 }
144 # else
145 #  define VECTOR_ALIGNED __attribute__((aligned(4)))
146 typedef uint64_t uint64xn_t;
147 typedef uint32_t uint32xn_t;
148 typedef uint32_t big_register_t;
149
150 static ossl_inline big_register_t br_set_to_mask(mask_t x)
151 {
152     return (big_register_t) x;
153 }
154 # endif
155
156 # if defined(__AVX2__)
157 static ossl_inline big_register_t br_is_zero(big_register_t x)
158 {
159     return (big_register_t) (x == br_set_to_mask(0));
160 }
161 # elif defined(__SSE2__)
162 static ossl_inline big_register_t br_is_zero(big_register_t x)
163 {
164     return (big_register_t) _mm_cmpeq_epi32((__m128i) x, _mm_setzero_si128());
165 }
166 # elif defined(__ARM_NEON__)
167 static ossl_inline big_register_t br_is_zero(big_register_t x)
168 {
169     return vceqq_u32(x, x ^ x);
170 }
171 # else
172 #  define br_is_zero word_is_zero
173 # endif
174
175 /* PERF: vectorize vs unroll */
176 # ifdef __clang__
177 #  if 100*__clang_major__ + __clang_minor__ > 305
178 #   define UNROLL _Pragma("clang loop unroll(full)")
179 #  endif
180 # endif
181
182 # ifndef UNROLL
183 #  define UNROLL
184 # endif
185
186 /*
187  * The plan on booleans: The external interface uses c448_bool_t, but this
188  * might be a different size than our particular arch's word_t (and thus
189  * mask_t).  Also, the caller isn't guaranteed to pass it as nonzero.  So
190  * bool_to_mask converts word sizes and checks nonzero. On the flip side,
191  * mask_t is always -1 or 0, but it might be a different size than
192  * c448_bool_t. On the third hand, we have success vs boolean types, but
193  * that's handled in common.h: it converts between c448_bool_t and
194  * c448_error_t.
195  */
196 static ossl_inline c448_bool_t mask_to_bool(mask_t m)
197 {
198     return (c448_sword_t)(sword_t)m;
199 }
200
201 static ossl_inline mask_t bool_to_mask(c448_bool_t m)
202 {
203     /* On most arches this will be optimized to a simple cast. */
204     mask_t ret = 0;
205     unsigned int i;
206     unsigned int limit = sizeof(c448_bool_t) / sizeof(mask_t);
207
208     if (limit < 1)
209         limit = 1;
210     for (i = 0; i < limit; i++)
211         ret |= ~word_is_zero(m >> (i * 8 * sizeof(word_t)));
212
213     return ret;
214 }
215
216 static ossl_inline void ignore_result(c448_bool_t boo)
217 {
218     (void)boo;
219 }
220
221 #endif                          /* __WORD_H__ */