From: Matt Corallo Date: Mon, 17 Jun 2024 18:10:21 +0000 (+0000) Subject: Simplify siphash as we always have a round number of 64-bit words X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=2348cb3aa6b126ebfdcc0bc38178999ba9507d4e;p=flowspec-xdp Simplify siphash as we always have a round number of 64-bit words --- diff --git a/siphash.h b/siphash.h index 1d748b9..f6d5fb3 100644 --- a/siphash.h +++ b/siphash.h @@ -18,6 +18,7 @@ #include #include +#include /* default: SipHash-2-4 */ #ifndef cROUNDS @@ -73,8 +74,7 @@ #endif __attribute__((always_inline)) -static inline uint64_t siphash(const void *in, const size_t inlen, const uint8_t k[16]) { - const unsigned char *ni = (const unsigned char *)in; +static inline uint64_t siphash(const uint64_t *in, const size_t inwords, const uint8_t k[16]) { const unsigned char *kk = (const unsigned char *)k; uint64_t v0 = UINT64_C(0x736f6d6570736575); @@ -85,16 +85,16 @@ static inline uint64_t siphash(const void *in, const size_t inlen, const uint8_t uint64_t k1 = U8TO64_LE(kk + 8); uint64_t m; int i; - const unsigned char *end = ni + inlen - (inlen % sizeof(uint64_t)); - const int left = inlen & 7; - uint64_t b = ((uint64_t)inlen) << 56; + size_t j; + uint64_t b = ((uint64_t)inwords) << (56 + 3); v3 ^= k1; v2 ^= k0; v1 ^= k1; v0 ^= k0; - for (; ni != end; ni += 8) { - m = U8TO64_LE(ni); + for (j = 0; j < inwords; ++j) { + m = *in; + in += 1; v3 ^= m; TRACE; @@ -104,26 +104,6 @@ static inline uint64_t siphash(const void *in, const size_t inlen, const uint8_t v0 ^= m; } - switch (left) { - case 7: - b |= ((uint64_t)ni[6]) << 48; - case 6: - b |= ((uint64_t)ni[5]) << 40; - case 5: - b |= ((uint64_t)ni[4]) << 32; - case 4: - b |= ((uint64_t)ni[3]) << 24; - case 3: - b |= ((uint64_t)ni[2]) << 16; - case 2: - b |= ((uint64_t)ni[1]) << 8; - case 1: - b |= ((uint64_t)ni[0]); - break; - case 0: - break; - } - v3 ^= b; TRACE; @@ -143,8 +123,10 @@ static inline uint64_t siphash(const void *in, const size_t inlen, const uint8_t #include "rand.h" static uint64_t siphash_uint64_t(const uint64_t in) { - return siphash(&in, sizeof(uint64_t), COMPILE_TIME_RAND); + return siphash(&in, 1, COMPILE_TIME_RAND); } static uint64_t siphash_uint128_t(const __uint128_t in) { - return siphash(&in, sizeof(__uint128_t), COMPILE_TIME_RAND); + uint64_t words[2]; + memcpy(words, &in, sizeof(__uint128_t)); + return siphash(words, 2, COMPILE_TIME_RAND); }