|
|
@ -19,15 +19,9 @@ |
|
|
|
// taken from https://github.com/Logan007/pearson
|
|
|
|
// This is free and unencumbered software released into the public domain.
|
|
|
|
|
|
|
|
#include <stddef.h> |
|
|
|
#include <stdint.h> |
|
|
|
|
|
|
|
#include "pearson.h" |
|
|
|
|
|
|
|
// compile with 'LOW_MEM_FOOTPRINT' defined to make use of 256 byte look-up tabe only
|
|
|
|
// otherwise, a 16-bit look-up table is used which allows considerably faster hashing
|
|
|
|
// however, it needs to be generated by once calling pearson_hash_init() upfront
|
|
|
|
// #define LOW_MEM_FOOTPRINT
|
|
|
|
|
|
|
|
// AES S-Box table -- allows for eventually supported hardware accelerated look-up
|
|
|
|
static const uint8_t t[256] ={ |
|
|
@ -71,14 +65,125 @@ static const uint8_t t[256] ={ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
#ifndef LOW_MEM_FOOTPRINT |
|
|
|
static uint16_t t16[65536]; // 16-bit look-up table
|
|
|
|
#endif |
|
|
|
|
|
|
|
#define ROR64(x,r) (((x)>>(r))|((x)<<(64-(r)))) |
|
|
|
#define ROR32(x,r) (((x)>>(r))|((x)<<(32-(r)))) |
|
|
|
|
|
|
|
|
|
|
|
#if defined (__AES__) && defined (__SSSE3__) // AES-NI & SSSE3 ----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
void pearson_hash_256 (uint8_t *out, const uint8_t *in, size_t len) { |
|
|
|
|
|
|
|
size_t i; |
|
|
|
|
|
|
|
uint8_t upper[8] = { 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08 }; |
|
|
|
uint8_t lower[8] = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 }; |
|
|
|
|
|
|
|
uint64_t upper_hash_mask = *(uint64_t*)&upper; |
|
|
|
uint64_t lower_hash_mask = *(uint64_t*)&lower; |
|
|
|
|
|
|
|
__m128i tmp = _mm_set1_epi8(0x10); |
|
|
|
|
|
|
|
__m128i hash_mask = _mm_set_epi64 ((__m64)lower_hash_mask, (__m64)upper_hash_mask); |
|
|
|
__m128i high_hash_mask = _mm_xor_si128 (tmp, hash_mask); |
|
|
|
__m128i hash= _mm_setzero_si128(); |
|
|
|
__m128i high_hash= _mm_setzero_si128(); hash; |
|
|
|
|
|
|
|
__m128i ZERO = _mm_setzero_si128(); |
|
|
|
__m128i ISOLATE_SBOX_MASK = _mm_set_epi32(0x0306090C, 0x0F020508, 0x0B0E0104, 0x070A0D00); |
|
|
|
|
|
|
|
for (i = 0; i < len; i++) { |
|
|
|
// broadcast the character, xor into hash, make them different permutations
|
|
|
|
__m128i cc = _mm_set1_epi8 (in[i]); |
|
|
|
hash = _mm_xor_si128 (hash, cc); |
|
|
|
high_hash = _mm_xor_si128 (high_hash, cc); |
|
|
|
hash = _mm_xor_si128 (hash, hash_mask); |
|
|
|
high_hash = _mm_xor_si128 (high_hash, high_hash_mask); |
|
|
|
// table lookup
|
|
|
|
hash = _mm_shuffle_epi8(hash, ISOLATE_SBOX_MASK); // re-order along AES round
|
|
|
|
high_hash = _mm_shuffle_epi8(high_hash, ISOLATE_SBOX_MASK); // re-order along AES round
|
|
|
|
hash = _mm_aesenclast_si128(hash, ZERO); |
|
|
|
high_hash = _mm_aesenclast_si128(high_hash, ZERO); |
|
|
|
} |
|
|
|
// store output
|
|
|
|
_mm_store_si128 ((__m128i*)out , high_hash); |
|
|
|
_mm_store_si128 ((__m128i*)&out[16] , hash); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void pearson_hash_128 (uint8_t *out, const uint8_t *in, size_t len) { |
|
|
|
|
|
|
|
size_t i; |
|
|
|
|
|
|
|
uint8_t upper[8] = { 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08 }; |
|
|
|
uint8_t lower[8] = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 }; |
|
|
|
|
|
|
|
uint64_t upper_hash_mask = *(uint64_t*)&upper; |
|
|
|
uint64_t lower_hash_mask = *(uint64_t*)&lower; |
|
|
|
|
|
|
|
__m128i hash_mask = _mm_set_epi64 ((__m64)lower_hash_mask, (__m64)upper_hash_mask); |
|
|
|
__m128i hash = _mm_setzero_si128(); // _mm_xor_si128 (hash, hash);
|
|
|
|
|
|
|
|
__m128i ZERO = _mm_setzero_si128(); |
|
|
|
__m128i ISOLATE_SBOX_MASK = _mm_set_epi32(0x0306090C, 0x0F020508, 0x0B0E0104, 0x070A0D00); |
|
|
|
|
|
|
|
for (i = 0; i < len; i++) { |
|
|
|
// broadcast the character, xor into hash, make them different permutations
|
|
|
|
__m128i cc = _mm_set1_epi8 (in[i]); |
|
|
|
hash = _mm_xor_si128 (hash, cc); |
|
|
|
hash = _mm_xor_si128 (hash, hash_mask); |
|
|
|
// table lookup
|
|
|
|
hash = _mm_shuffle_epi8(hash, ISOLATE_SBOX_MASK); // re-order along AES round
|
|
|
|
hash = _mm_aesenclast_si128(hash, ZERO); |
|
|
|
} |
|
|
|
// store output
|
|
|
|
_mm_store_si128 ((__m128i*)out , hash); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// so far, only used internally
|
|
|
|
static uint64_t pearson_hash_64 (const uint8_t *in, size_t len) { |
|
|
|
|
|
|
|
size_t i; |
|
|
|
|
|
|
|
__m128i hash_mask = _mm_cvtsi64_si128(0x0706050403020100); |
|
|
|
|
|
|
|
__m128i hash = _mm_setzero_si128(); |
|
|
|
|
|
|
|
__m128i ZERO = _mm_setzero_si128(); |
|
|
|
__m128i ISOLATE_SBOX_MASK = _mm_set_epi32(0x0306090C, 0x0F020508, 0x0B0E0104, 0x070A0D00); |
|
|
|
|
|
|
|
for (i = 0; i < len; i++) { |
|
|
|
// broadcast the character, xor into hash, make them different permutations
|
|
|
|
__m128i cc = _mm_set1_epi8 (in[i]); |
|
|
|
hash = _mm_xor_si128 (hash, cc); |
|
|
|
hash = _mm_xor_si128 (hash, hash_mask); |
|
|
|
// table lookup
|
|
|
|
hash = _mm_shuffle_epi8(hash, ISOLATE_SBOX_MASK); // re-order along AES round
|
|
|
|
hash = _mm_aesenclast_si128(hash, ZERO); |
|
|
|
} |
|
|
|
// store output
|
|
|
|
return _mm_cvtsi128_si64 (hash); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint32_t pearson_hash_32 (const uint8_t *in, size_t len) { |
|
|
|
|
|
|
|
return pearson_hash_64(in, len); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint16_t pearson_hash_16 (const uint8_t *in, size_t len) { |
|
|
|
|
|
|
|
return pearson_hash_64(in, len); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#else // plain C --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
void pearson_hash_256 (uint8_t *out, const uint8_t *in, size_t len) { |
|
|
|
|
|
|
|
size_t i; |
|
|
@ -112,51 +217,6 @@ void pearson_hash_256 (uint8_t *out, const uint8_t *in, size_t len) { |
|
|
|
|
|
|
|
// table lookup
|
|
|
|
uint64_t h = 0; |
|
|
|
#ifdef LOW_MEM_FOOTPRINT // 256 byte look-up table ----------
|
|
|
|
uint8_t x; |
|
|
|
x = upper_hash; x = t[x]; upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = upper_hash; x = t[x]; upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = upper_hash; x = t[x]; upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = upper_hash; x = t[x]; upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = upper_hash; x = t[x]; upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = upper_hash; x = t[x]; upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = upper_hash; x = t[x]; upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = upper_hash; x = t[x]; upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
upper_hash = h; |
|
|
|
|
|
|
|
h = 0; |
|
|
|
x = lower_hash; x = t[x]; lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = lower_hash; x = t[x]; lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = lower_hash; x = t[x]; lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = lower_hash; x = t[x]; lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = lower_hash; x = t[x]; lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = lower_hash; x = t[x]; lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = lower_hash; x = t[x]; lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = lower_hash; x = t[x]; lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
lower_hash = h; |
|
|
|
|
|
|
|
h = 0; |
|
|
|
x = high_upper_hash; x = t[x]; high_upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = high_upper_hash; x = t[x]; high_upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = high_upper_hash; x = t[x]; high_upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = high_upper_hash; x = t[x]; high_upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = high_upper_hash; x = t[x]; high_upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = high_upper_hash; x = t[x]; high_upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = high_upper_hash; x = t[x]; high_upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = high_upper_hash; x = t[x]; high_upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
high_upper_hash = h; |
|
|
|
|
|
|
|
h = 0; |
|
|
|
x = high_lower_hash; x = t[x]; high_lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = high_lower_hash; x = t[x]; high_lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = high_lower_hash; x = t[x]; high_lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = high_lower_hash; x = t[x]; high_lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = high_lower_hash; x = t[x]; high_lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = high_lower_hash; x = t[x]; high_lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = high_lower_hash; x = t[x]; high_lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = high_lower_hash; x = t[x]; high_lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
high_lower_hash = h; |
|
|
|
#else // 16-bit look-up table -------------------------------
|
|
|
|
uint16_t x; |
|
|
|
x = upper_hash; x = t16[x]; upper_hash >>= 16; h |= x; h=ROR64(h,16); |
|
|
|
x = upper_hash; x = t16[x]; upper_hash >>= 16; h |= x; h=ROR64(h,16); |
|
|
@ -184,7 +244,6 @@ void pearson_hash_256 (uint8_t *out, const uint8_t *in, size_t len) { |
|
|
|
x = high_lower_hash; x = t16[x]; high_lower_hash >>= 16; h |= x; h=ROR64(h,16); |
|
|
|
x = high_lower_hash; x = t16[x]; high_lower_hash >>= 16; h |= x; h=ROR64(h,16); |
|
|
|
high_lower_hash = h; |
|
|
|
#endif // LOW_MEM_FOOTPRINT ------
|
|
|
|
} |
|
|
|
// store output
|
|
|
|
uint64_t *o; |
|
|
@ -225,29 +284,6 @@ void pearson_hash_128 (uint8_t *out, const uint8_t *in, size_t len) { |
|
|
|
lower_hash ^= c ^ lower_hash_mask; |
|
|
|
// table lookup
|
|
|
|
uint64_t h = 0; |
|
|
|
#ifdef LOW_MEM_FOOTPRINT // 256 byte look-up table ----------
|
|
|
|
uint8_t x; |
|
|
|
x = upper_hash; x = t[x]; upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = upper_hash; x = t[x]; upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = upper_hash; x = t[x]; upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = upper_hash; x = t[x]; upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = upper_hash; x = t[x]; upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = upper_hash; x = t[x]; upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = upper_hash; x = t[x]; upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = upper_hash; x = t[x]; upper_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
upper_hash = h; |
|
|
|
|
|
|
|
h = 0; |
|
|
|
x = lower_hash; x = t[x]; lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = lower_hash; x = t[x]; lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = lower_hash; x = t[x]; lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = lower_hash; x = t[x]; lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = lower_hash; x = t[x]; lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = lower_hash; x = t[x]; lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = lower_hash; x = t[x]; lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
x = lower_hash; x = t[x]; lower_hash >>= 8; h |= x; h=ROR64(h,8); |
|
|
|
lower_hash= h; |
|
|
|
#else // 16-bit look-up table -------------------------------
|
|
|
|
uint16_t x; |
|
|
|
x = upper_hash; x = t16[x]; upper_hash >>= 16; h |= x; h=ROR64(h,16); |
|
|
|
x = upper_hash; x = t16[x]; upper_hash >>= 16; h |= x; h=ROR64(h,16); |
|
|
@ -261,7 +297,6 @@ void pearson_hash_128 (uint8_t *out, const uint8_t *in, size_t len) { |
|
|
|
x = lower_hash; x = t16[x]; lower_hash >>= 16; h |= x; h=ROR64(h,16); |
|
|
|
x = lower_hash; x = t16[x]; lower_hash >>= 16; h |= x; h=ROR64(h,16); |
|
|
|
lower_hash = h; |
|
|
|
#endif // LOW_MEM_FOOTPRINT ------
|
|
|
|
} |
|
|
|
// store output
|
|
|
|
uint64_t *o; |
|
|
@ -287,7 +322,6 @@ uint32_t pearson_hash_32 (const uint8_t *in, size_t len) { |
|
|
|
c |= c << 16; |
|
|
|
hash ^= c ^ hash_mask; |
|
|
|
// table lookup
|
|
|
|
#ifdef LOW_MEM_FOOTPRINT |
|
|
|
uint32_t h = 0; |
|
|
|
uint8_t x; |
|
|
|
x = hash; x = t[x]; hash >>= 8; h |= x; h=ROR32(h,8); |
|
|
@ -295,9 +329,6 @@ uint32_t pearson_hash_32 (const uint8_t *in, size_t len) { |
|
|
|
x = hash; x = t[x]; hash >>= 8; h |= x; h=ROR32(h,8); |
|
|
|
x = hash; x = t[x]; hash >>= 8; h |= x; h=ROR32(h,8); |
|
|
|
hash = h; |
|
|
|
#else |
|
|
|
hash = (t16[hash >> 16] << 16) + t16[(uint16_t)hash]; |
|
|
|
#endif |
|
|
|
} |
|
|
|
// output
|
|
|
|
return hash; |
|
|
@ -317,23 +348,20 @@ uint16_t pearson_hash_16 (const uint8_t *in, size_t len) { |
|
|
|
c |= c << 8; |
|
|
|
hash ^= c ^ hash_mask; |
|
|
|
// table lookup
|
|
|
|
#ifdef LOW_MEM_FOOTPRINT |
|
|
|
hash = t[(uint8_t)hash] + (t[hash >> 8] << 8); |
|
|
|
#else |
|
|
|
hash = t16[hash]; |
|
|
|
#endif |
|
|
|
} |
|
|
|
// output
|
|
|
|
return hash; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#endif // AES-NI & SSSE3, plain C ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
void pearson_hash_init () { |
|
|
|
|
|
|
|
#ifndef LOW_MEM_FOOTPRINT |
|
|
|
size_t i; |
|
|
|
|
|
|
|
for (i = 0; i < 65536; i++) |
|
|
|
t16[i] = (t[i >> 8] << 8) + t[(uint8_t)i]; |
|
|
|
#endif |
|
|
|
} |
|
|
|