6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
8
9
- #include " siphash.h"
10
- #include < assert.h>
11
- #include < stddef.h>
12
- #include < stdint.h>
9
+ #include " llvm/Support/Compiler.h"
10
+ #include < cstdint>
13
11
14
12
// Lightly adapted from the SipHash reference C implementation by
15
13
// Jean-Philippe Aumasson and Daniel J. Bernstein.
16
14
17
- /* default: SipHash-2-4 */
18
- #ifndef cROUNDS
19
- #define cROUNDS 2
20
- #endif
21
- #ifndef dROUNDS
22
- #define dROUNDS 4
23
- #endif
24
-
25
15
#define ROTL (x, b ) (uint64_t )(((x) << (b)) | ((x) >> (64 - (b))))
26
16
27
- #define U32TO8_LE (p, v ) \
28
- (p)[0 ] = (uint8_t )((v)); \
29
- (p)[1 ] = (uint8_t )((v) >> 8 ); \
30
- (p)[2 ] = (uint8_t )((v) >> 16 ); \
31
- (p)[3 ] = (uint8_t )((v) >> 24 );
32
-
33
- #define U64TO8_LE (p, v ) \
34
- U32TO8_LE ((p), (uint32_t )((v))); \
35
- U32TO8_LE ((p) + 4, (uint32_t )((v) >> 32));
36
-
37
17
#define U8TO64_LE (p ) \
38
18
(((uint64_t )((p)[0 ])) | ((uint64_t )((p)[1 ]) << 8 ) | \
39
19
((uint64_t )((p)[2 ]) << 16 ) | ((uint64_t )((p)[3 ]) << 24 ) | \
58
38
v2 = ROTL (v2, 32 ); \
59
39
} while (0 )
60
40
61
- /*
62
- Computes a SipHash value
63
- *in: pointer to input data (read-only)
64
- inlen: input data length in bytes (any size_t value)
65
- *k: pointer to the key data (read-only), must be 16 bytes
66
- *out: pointer to output data (write-only), outlen bytes must be allocated
67
- outlen: length of the output in bytes, must be 8 or 16
68
- */
69
- int siphash (const void *in, const size_t inlen, const void *k, uint8_t *out,
70
- const size_t outlen) {
41
+ template <int cROUNDS, int dROUNDS, class ResultTy >
42
+ static inline ResultTy siphash (const unsigned char *in, uint64_t inlen,
43
+ const unsigned char (&k)[16]) {
71
44
72
45
const unsigned char *ni = (const unsigned char *)in;
73
46
const unsigned char *kk = (const unsigned char *)k;
74
47
75
- assert ((outlen == 8 ) || (outlen == 16 ));
48
+ static_assert (sizeof (ResultTy) == 8 || sizeof (ResultTy) == 16 ,
49
+ " result type should be uint64_t or uint128_t" );
76
50
uint64_t v0 = UINT64_C (0x736f6d6570736575 );
77
51
uint64_t v1 = UINT64_C (0x646f72616e646f6d );
78
52
uint64_t v2 = UINT64_C (0x6c7967656e657261 );
@@ -89,7 +63,7 @@ int siphash(const void *in, const size_t inlen, const void *k, uint8_t *out,
89
63
v1 ^= k1;
90
64
v0 ^= k0;
91
65
92
- if (outlen == 16 )
66
+ if (sizeof (ResultTy) == 16 )
93
67
v1 ^= 0xee ;
94
68
95
69
for (; ni != end; ni += 8 ) {
@@ -105,22 +79,22 @@ int siphash(const void *in, const size_t inlen, const void *k, uint8_t *out,
105
79
switch (left) {
106
80
case 7 :
107
81
b |= ((uint64_t )ni[6 ]) << 48 ;
108
- /* FALLTHRU */
82
+ LLVM_FALLTHROUGH;
109
83
case 6 :
110
84
b |= ((uint64_t )ni[5 ]) << 40 ;
111
- /* FALLTHRU */
85
+ LLVM_FALLTHROUGH;
112
86
case 5 :
113
87
b |= ((uint64_t )ni[4 ]) << 32 ;
114
- /* FALLTHRU */
88
+ LLVM_FALLTHROUGH;
115
89
case 4 :
116
90
b |= ((uint64_t )ni[3 ]) << 24 ;
117
- /* FALLTHRU */
91
+ LLVM_FALLTHROUGH;
118
92
case 3 :
119
93
b |= ((uint64_t )ni[2 ]) << 16 ;
120
- /* FALLTHRU */
94
+ LLVM_FALLTHROUGH;
121
95
case 2 :
122
96
b |= ((uint64_t )ni[1 ]) << 8 ;
123
- /* FALLTHRU */
97
+ LLVM_FALLTHROUGH;
124
98
case 1 :
125
99
b |= ((uint64_t )ni[0 ]);
126
100
break ;
@@ -135,7 +109,7 @@ int siphash(const void *in, const size_t inlen, const void *k, uint8_t *out,
135
109
136
110
v0 ^= b;
137
111
138
- if (outlen == 16 )
112
+ if (sizeof (ResultTy) == 16 )
139
113
v2 ^= 0xee ;
140
114
else
141
115
v2 ^= 0xff ;
@@ -144,18 +118,18 @@ int siphash(const void *in, const size_t inlen, const void *k, uint8_t *out,
144
118
SIPROUND;
145
119
146
120
b = v0 ^ v1 ^ v2 ^ v3;
147
- U64TO8_LE (out, b);
148
121
149
- if (outlen == 8 )
150
- return 0 ;
122
+ uint64_t firstHalf = b;
123
+ if (sizeof (ResultTy) == 8 )
124
+ return firstHalf;
151
125
152
126
v1 ^= 0xdd ;
153
127
154
128
for (i = 0 ; i < dROUNDS; ++i)
155
129
SIPROUND;
156
130
157
131
b = v0 ^ v1 ^ v2 ^ v3;
158
- U64TO8_LE (out + 8 , b) ;
132
+ uint64_t secondHalf = b ;
159
133
160
- return 0 ;
134
+ return firstHalf | ( ResultTy (secondHalf) << ( sizeof (ResultTy) == 8 ? 0 : 64 )) ;
161
135
}
0 commit comments