3 * This file is distributed under the same license as OpenSSL
6 #ifndef GOST_GRASSHOPPER_MATH_H
7 #define GOST_GRASSHOPPER_MATH_H
9 #if defined(__cplusplus)
13 #include "gost_grasshopper_defines.h"
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
20 #define GRASSHOPPER_MIN_BITS 8
21 #define GRASSHOPPER_MAX_BITS 128
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
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)
38 #define GRASSHOPPER_BIT_PARTS (GRASSHOPPER_MAX_BITS / GRASSHOPPER_BITS)
39 #define GRASSHOPPER_MAX_BIT_PARTS (GRASSHOPPER_MAX_BITS / GRASSHOPPER_MIN_BITS)
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)])
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
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));
61 for (i = 0; i < GRASSHOPPER_BIT_PARTS; i++) {
62 GRASSHOPPER_ACCESS_128_VALUE(*x, i) = 0;
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));
72 for (i = 0; i < GRASSHOPPER_BIT_PARTS; i++) {
73 GRASSHOPPER_ACCESS_128_VALUE(*to, i) = GRASSHOPPER_ACCESS_128_VALUE(*from, i);
78 static GRASSHOPPER_INLINE void grasshopper_append128(grasshopper_w128_t* x, const grasshopper_w128_t* y) {
80 for (i = 0; i < GRASSHOPPER_BIT_PARTS; i++) {
81 GRASSHOPPER_ACCESS_128_VALUE(*x, i) ^= GRASSHOPPER_ACCESS_128_VALUE(*y, i);
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);
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]) {
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)]);
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);
107 static GRASSHOPPER_INLINE void grasshopper_convert128(grasshopper_w128_t* x, const uint8_t* array) {
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)];
114 #define GRASSHOPPER_GALOIS_POWER 8
116 #define GRASSHOPPER_GALOIS_FIELD_SIZE ((1 << GRASSHOPPER_GALOIS_POWER) - 1)
118 extern uint8_t grasshopper_galois_alpha_to[256];
119 extern uint8_t grasshopper_galois_index_of[256];
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];
130 #if defined(__cplusplus)