2 SipHash reference C implementation
4 Copyright (c) 2012-2021 Jean-Philippe Aumasson
5 <jeanphilippe.aumasson@gmail.com>
6 Copyright (c) 2012-2014 Daniel J. Bernstein <djb@cr.yp.to>
7 Slightly tweaked by the git author.
9 To the extent possible under law, the author(s) have dedicated all copyright
10 and related and neighboring rights to this software to the public domain
11 worldwide. This software is distributed without any warranty.
13 You should have received a copy of the CC0 Public Domain Dedication along
15 this software. If not, see
16 <http://creativecommons.org/publicdomain/zero/1.0/>.
22 /* default: SipHash-2-4 */
30 #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
32 #define U32TO8_LE(p, v) \
33 (p)[0] = (uint8_t)((v)); \
34 (p)[1] = (uint8_t)((v) >> 8); \
35 (p)[2] = (uint8_t)((v) >> 16); \
36 (p)[3] = (uint8_t)((v) >> 24);
38 #define U8TO64_LE(p) \
39 (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \
40 ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \
41 ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \
42 ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
66 printf("(%3zu) v0 %016" PRIx64 "\n", inlen, v0); \
67 printf("(%3zu) v1 %016" PRIx64 "\n", inlen, v1); \
68 printf("(%3zu) v2 %016" PRIx64 "\n", inlen, v2); \
69 printf("(%3zu) v3 %016" PRIx64 "\n", inlen, v3); \
75 __attribute__((always_inline))
76 static inline uint64_t siphash(const void *in, const size_t inlen, const uint8_t k[16]) {
77 const unsigned char *ni = (const unsigned char *)in;
78 const unsigned char *kk = (const unsigned char *)k;
80 uint64_t v0 = UINT64_C(0x736f6d6570736575);
81 uint64_t v1 = UINT64_C(0x646f72616e646f6d);
82 uint64_t v2 = UINT64_C(0x6c7967656e657261);
83 uint64_t v3 = UINT64_C(0x7465646279746573);
84 uint64_t k0 = U8TO64_LE(kk);
85 uint64_t k1 = U8TO64_LE(kk + 8);
88 const unsigned char *end = ni + inlen - (inlen % sizeof(uint64_t));
89 const int left = inlen & 7;
90 uint64_t b = ((uint64_t)inlen) << 56;
96 for (; ni != end; ni += 8) {
101 for (i = 0; i < cROUNDS; ++i)
109 b |= ((uint64_t)ni[6]) << 48;
111 b |= ((uint64_t)ni[5]) << 40;
113 b |= ((uint64_t)ni[4]) << 32;
115 b |= ((uint64_t)ni[3]) << 24;
117 b |= ((uint64_t)ni[2]) << 16;
119 b |= ((uint64_t)ni[1]) << 8;
121 b |= ((uint64_t)ni[0]);
130 for (i = 0; i < cROUNDS; ++i)
137 for (i = 0; i < dROUNDS; ++i)
140 b = v0 ^ v1 ^ v2 ^ v3;