]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_grasshopper_math.h
Grasshopper && CMake
[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     for (int i = 0; i < GRASSHOPPER_BIT_PARTS; i++) {
61         GRASSHOPPER_ACCESS_128_VALUE(*x, i) = 0;
62     }
63 #endif
64 }
65
66 static GRASSHOPPER_INLINE void grasshopper_copy128(grasshopper_w128_t* to, const grasshopper_w128_t* from) {
67 #if(GRASSHOPPER_BITS == 8 || GRASSHOPPER_BITS == 16)
68     __builtin_memcpy(&to, &from, sizeof(w128_t));
69 #else
70     for (int i = 0; i < GRASSHOPPER_BIT_PARTS; i++) {
71         GRASSHOPPER_ACCESS_128_VALUE(*to, i) = GRASSHOPPER_ACCESS_128_VALUE(*from, i);
72     }
73 #endif
74 }
75
76 static GRASSHOPPER_INLINE void grasshopper_append128(grasshopper_w128_t* x, const grasshopper_w128_t* y) {
77     for (int i = 0; i < GRASSHOPPER_BIT_PARTS; i++) {
78         GRASSHOPPER_ACCESS_128_VALUE(*x, i) ^= GRASSHOPPER_ACCESS_128_VALUE(*y, i);
79     }
80 }
81
82 static GRASSHOPPER_INLINE void grasshopper_plus128(grasshopper_w128_t* result, const grasshopper_w128_t* x,
83                                                const grasshopper_w128_t* y) {
84     grasshopper_copy128(result, x);
85     grasshopper_append128(result, y);
86 }
87
88 // result & x must be different
89 static GRASSHOPPER_INLINE void grasshopper_plus128multi(grasshopper_w128_t* result, const grasshopper_w128_t* x,
90                                                     const grasshopper_w128_t array[][256]) {
91     grasshopper_zero128(result);
92     for (int i = 0; i < GRASSHOPPER_MAX_BIT_PARTS; i++) {
93         grasshopper_append128(result, &array[i][GRASSHOPPER_ACCESS_128_VALUE_8(*x, i)]);
94     }
95 }
96
97 static GRASSHOPPER_INLINE void grasshopper_append128multi(grasshopper_w128_t* result, grasshopper_w128_t* x,
98                                                       const grasshopper_w128_t array[][256]) {
99     grasshopper_plus128multi(result, x, array);
100     grasshopper_copy128(x, result);
101 }
102
103 static GRASSHOPPER_INLINE void grasshopper_convert128(grasshopper_w128_t* x, const uint8_t* array) {
104     for (int i = 0; i < GRASSHOPPER_MAX_BIT_PARTS; i++) {
105         GRASSHOPPER_ACCESS_128_VALUE_8(*x, i) = array[GRASSHOPPER_ACCESS_128_VALUE_8(*x, i)];
106     }
107 }
108
109 #define GRASSHOPPER_GALOIS_POWER 8
110
111 #define GRASSHOPPER_GALOIS_FIELD_SIZE ((1 << GRASSHOPPER_GALOIS_POWER) - 1)
112
113 extern uint8_t grasshopper_galois_alpha_to[256];
114 extern uint8_t grasshopper_galois_index_of[256];
115
116 static GRASSHOPPER_INLINE uint8_t grasshopper_galois_mul(uint8_t x, uint8_t y) {
117     if (__builtin_expect(x != 0 && y != 0, 1)) {
118         return grasshopper_galois_alpha_to[(grasshopper_galois_index_of[x] + grasshopper_galois_index_of[y]) %
119                                          GRASSHOPPER_GALOIS_FIELD_SIZE];
120     } else {
121         return 0;
122     }
123 }
124
125 #if defined(__cplusplus)
126 }
127 #endif
128
129 #endif