]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_grasshopper_math.h
Fix MSVS2017 build errors.
[openssl-gost/engine.git] / gost_grasshopper_math.h
1 /*
2  * Maxim Tishkov 2016
3  * This file is distributed under the same license as OpenSSL
4  */
5
6 #ifndef GOST_GRASSHOPPER_MATH_H
7 #define GOST_GRASSHOPPER_MATH_H
8
9 #if defined(__cplusplus)
10 extern "C" {
11 #endif
12
13 #include "gost_grasshopper_defines.h"
14
15 #if defined(__SSE__) || defined(__SSE2__) || defined(__SSE2_MATH__) || defined(__SSE3__) || defined(__SSE_MATH__) \
16  || defined(__SSE4_1__)|| defined(__SSE4_2__)|| defined(__SSSE3__)
17 #define GRASSHOPPER_SSE_SUPPORTED
18 #endif
19
20 #define GRASSHOPPER_MIN_BITS 8
21 #define GRASSHOPPER_MAX_BITS 128
22
23 #if UINTPTR_MAX == 0xff
24 #define GRASSHOPPER_BITS 8
25 #elif UINTPTR_MAX == 0xffff
26 #define GRASSHOPPER_BITS 16
27 #elif UINTPTR_MAX == 0xffffffff
28 #define GRASSHOPPER_BITS 32
29 #elif UINTPTR_MAX == 0xffffffffffffffff
30 #define GRASSHOPPER_BITS 64
31 #endif
32
33 #define GRASSHOPPER_BIT_PARTS_8 (GRASSHOPPER_MAX_BITS / 8)
34 #define GRASSHOPPER_BIT_PARTS_16 (GRASSHOPPER_MAX_BITS / 16)
35 #define GRASSHOPPER_BIT_PARTS_32 (GRASSHOPPER_MAX_BITS / 32)
36 #define GRASSHOPPER_BIT_PARTS_64 (GRASSHOPPER_MAX_BITS / 64)
37
38 #define GRASSHOPPER_BIT_PARTS (GRASSHOPPER_MAX_BITS / GRASSHOPPER_BITS)
39 #define GRASSHOPPER_MAX_BIT_PARTS (GRASSHOPPER_MAX_BITS / GRASSHOPPER_MIN_BITS)
40
41 #define GRASSHOPPER_ACCESS_128_VALUE_8(key, part) ((key).b[(part)])
42 #define GRASSHOPPER_ACCESS_128_VALUE_16(key, part) ((key).w[(part)])
43 #define GRASSHOPPER_ACCESS_128_VALUE_32(key, part) ((key).d[(part)])
44 #define GRASSHOPPER_ACCESS_128_VALUE_64(key, part) ((key).q[(part)])
45
46 #if(GRASSHOPPER_BITS == 8)
47 #define GRASSHOPPER_ACCESS_128_VALUE GRASSHOPPER_ACCESS_128_VALUE_8
48 #elif(GRASSHOPPER_BITS == 16)
49 #define GRASSHOPPER_ACCESS_128_VALUE GRASSHOPPER_ACCESS_128_VALUE_16
50 #elif(GRASSHOPPER_BITS == 32)
51 #define GRASSHOPPER_ACCESS_128_VALUE GRASSHOPPER_ACCESS_128_VALUE_32
52 #elif(GRASSHOPPER_BITS == 64)
53 #define GRASSHOPPER_ACCESS_128_VALUE GRASSHOPPER_ACCESS_128_VALUE_64
54 #endif
55
56 static GRASSHOPPER_INLINE void grasshopper_zero128(grasshopper_w128_t* x) {
57 #if(GRASSHOPPER_BITS == 8 || GRASSHOPPER_BITS == 16)
58     memset(&x, 0, sizeof(x));
59 #else
60                 int i;
61     for (i = 0; i < GRASSHOPPER_BIT_PARTS; i++) {
62         GRASSHOPPER_ACCESS_128_VALUE(*x, i) = 0;
63     }
64 #endif
65 }
66
67 static GRASSHOPPER_INLINE void grasshopper_copy128(grasshopper_w128_t* to, const grasshopper_w128_t* from) {
68 #if(GRASSHOPPER_BITS == 8 || GRASSHOPPER_BITS == 16)
69     __builtin_memcpy(&to, &from, sizeof(w128_t));
70 #else
71                 int i;
72     for (i = 0; i < GRASSHOPPER_BIT_PARTS; i++) {
73         GRASSHOPPER_ACCESS_128_VALUE(*to, i) = GRASSHOPPER_ACCESS_128_VALUE(*from, i);
74     }
75 #endif
76 }
77
78 static GRASSHOPPER_INLINE void grasshopper_append128(grasshopper_w128_t* x, const grasshopper_w128_t* y) {
79                 int i;
80     for (i = 0; i < GRASSHOPPER_BIT_PARTS; i++) {
81         GRASSHOPPER_ACCESS_128_VALUE(*x, i) ^= GRASSHOPPER_ACCESS_128_VALUE(*y, i);
82     }
83 }
84
85 static GRASSHOPPER_INLINE void grasshopper_plus128(grasshopper_w128_t* result, const grasshopper_w128_t* x,
86                                                const grasshopper_w128_t* y) {
87     grasshopper_copy128(result, x);
88     grasshopper_append128(result, y);
89 }
90
91 // result & x must be different
92 static GRASSHOPPER_INLINE void grasshopper_plus128multi(grasshopper_w128_t* result, const grasshopper_w128_t* x,
93                                                     const grasshopper_w128_t array[][256]) {
94                 int i;
95     grasshopper_zero128(result);
96     for (i = 0; i < GRASSHOPPER_MAX_BIT_PARTS; i++) {
97         grasshopper_append128(result, &array[i][GRASSHOPPER_ACCESS_128_VALUE_8(*x, i)]);
98     }
99 }
100
101 static GRASSHOPPER_INLINE void grasshopper_append128multi(grasshopper_w128_t* result, grasshopper_w128_t* x,
102                                                       const grasshopper_w128_t array[][256]) {
103     grasshopper_plus128multi(result, x, array);
104     grasshopper_copy128(x, result);
105 }
106
107 static GRASSHOPPER_INLINE void grasshopper_convert128(grasshopper_w128_t* x, const uint8_t* array) {
108                 int i;
109     for (i = 0; i < GRASSHOPPER_MAX_BIT_PARTS; i++) {
110         GRASSHOPPER_ACCESS_128_VALUE_8(*x, i) = array[GRASSHOPPER_ACCESS_128_VALUE_8(*x, i)];
111     }
112 }
113
114 #define GRASSHOPPER_GALOIS_POWER 8
115
116 #define GRASSHOPPER_GALOIS_FIELD_SIZE ((1 << GRASSHOPPER_GALOIS_POWER) - 1)
117
118 extern uint8_t grasshopper_galois_alpha_to[256];
119 extern uint8_t grasshopper_galois_index_of[256];
120
121 static GRASSHOPPER_INLINE uint8_t grasshopper_galois_mul(uint8_t x, uint8_t y) {
122     if (likely(x != 0 && y != 0)) {
123         return grasshopper_galois_alpha_to[(grasshopper_galois_index_of[x] + grasshopper_galois_index_of[y]) %
124                                          GRASSHOPPER_GALOIS_FIELD_SIZE];
125     } else {
126         return 0;
127     }
128 }
129
130 #if defined(__cplusplus)
131 }
132 #endif
133
134 #endif