1f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// 2f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// SpookyHash: a 128-bit noncryptographic hash function 3f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// By Bob Jenkins, public domain 4f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// Oct 31 2010: alpha, framework + SpookyHash::Mix appears right 5f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// Oct 31 2011: alpha again, Mix only good to 2^^69 but rest appears right 6f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// Dec 31 2011: beta, improved Mix, tested it for 2-bit deltas 7f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// Feb 2 2012: production, same bits as beta 8f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// Feb 5 2012: adjusted definitions of uint* to be more portable 9f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// 10f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// Up to 4 bytes/cycle for long messages. Reasonably fast for short messages. 11f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// All 1 or 2 bit deltas achieve avalanche within 1% bias per output bit. 12f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// 13f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// This was developed for and tested on 64-bit x86-compatible processors. 14f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// It assumes the processor is little-endian. There is a macro 15f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// controlling whether unaligned reads are allowed (by default they are). 16f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// This should be an equally good hash on big-endian machines, but it will 17f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// compute different results on them than on little-endian machines. 18f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// 19f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// Google's CityHash has similar specs to SpookyHash, and CityHash is faster 20f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// on some platforms. MD4 and MD5 also have similar specs, but they are orders 21f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// of magnitude slower. CRCs are two or more times slower, but unlike 22f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// SpookyHash, they have nice math for combining the CRCs of pieces to form 23f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// the CRCs of wholes. There are also cryptographic hashes, but those are even 24f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// slower than MD5. 25f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com// 26f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 27dd462f2c6817d4e09a6ecd99f35e3a04c342afb2tanjent@gmail.com#include "Platform.h" 28f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com#include <stddef.h> 29f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 30f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com#ifdef _MSC_VER 31f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com# define INLINE __forceinline 32f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com typedef unsigned __int64 uint64; 33f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com typedef unsigned __int32 uint32; 34f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com typedef unsigned __int16 uint16; 35f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com typedef unsigned __int8 uint8; 36f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com#else 37f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com# include <stdint.h> 38f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com# define INLINE inline 39f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com typedef uint64_t uint64; 40f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com typedef uint32_t uint32; 41f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com typedef uint16_t uint16; 42f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com typedef uint8_t uint8; 43f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com#endif 44f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 45f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 46f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.comclass SpookyHash 47f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com{ 48f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.compublic: 49f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 50f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // SpookyHash: hash a single message in one call, produce 128-bit output 51f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 52f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com static void Hash128( 53f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com const void *message, // message to hash 54f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com size_t length, // length of message in bytes 55f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 *hash1, // in/out: in seed 1, out hash value 1 56f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 *hash2); // in/out: in seed 2, out hash value 2 57f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 58f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 59f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // Hash64: hash a single message in one call, return 64-bit output 60f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 61f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com static uint64 Hash64( 62f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com const void *message, // message to hash 63f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com size_t length, // length of message in bytes 64f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 seed) // seed 65f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com { 66f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 hash1 = seed; 67f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com Hash128(message, length, &hash1, &seed); 68f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com return hash1; 69f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com } 70f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 71f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 72f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // Hash32: hash a single message in one call, produce 32-bit output 73f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 74f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com static uint32 Hash32( 75f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com const void *message, // message to hash 76f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com size_t length, // length of message in bytes 77f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint32 seed) // seed 78f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com { 79f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 hash1 = seed, hash2 = seed; 80f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com Hash128(message, length, &hash1, &hash2); 81f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com return (uint32)hash1; 82f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com } 83f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 84f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 85f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // Init: initialize the context of a SpookyHash 86f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 87f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com void Init( 88f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 seed1, // any 64-bit value will do, including 0 89f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 seed2); // different seeds produce independent hashes 90f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 91f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 92f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // Update: add a piece of a message to a SpookyHash state 93f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 94f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com void Update( 95f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com const void *message, // message fragment 96f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com size_t length); // length of message fragment in bytes 97f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 98f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 99f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 100f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // Final: compute the hash for the current SpookyHash state 101f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 102f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // This does not modify the state; you can keep updating it afterward 103f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 104f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // The result is the same as if SpookyHash() had been called with 105f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // all the pieces concatenated into one message. 106f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 107f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com void Final( 108f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 *hash1, // out only: first 64 bits of hash value. 109f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 *hash2); // out only: second 64 bits of hash value. 110f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 111f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 112f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // left rotate a 64-bit value by k bytes 113f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 114f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com static INLINE uint64 Rot64(uint64 x, int k) 115f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com { 116f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com return (x << k) | (x >> (64 - k)); 117f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com } 118f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 119f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 120f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // This is used if the input is 96 bytes long or longer. 121f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 122f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // The internal state is fully overwritten every 96 bytes. 123f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // Every input bit appears to cause at least 128 bits of entropy 124f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // before 96 other bytes are combined, when run forward or backward 125f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // For every input bit, 126f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // Two inputs differing in just that input bit 127f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // Where "differ" means xor or subtraction 128f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // And the base value is random 129f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // When run forward or backwards one Mix 130f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // I tried 3 pairs of each; they all differed by at least 212 bits. 131f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 132f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com static INLINE void Mix( 133f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com const uint64 *data, 134f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 &s0, uint64 &s1, uint64 &s2, uint64 &s3, 135f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 &s4, uint64 &s5, uint64 &s6, uint64 &s7, 136f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 &s8, uint64 &s9, uint64 &s10,uint64 &s11) 137f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com { 138f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com s0 += data[0]; s2 ^= s10; s11 ^= s0; s0 = Rot64(s0,11); s11 += s1; 139f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com s1 += data[1]; s3 ^= s11; s0 ^= s1; s1 = Rot64(s1,32); s0 += s2; 140f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com s2 += data[2]; s4 ^= s0; s1 ^= s2; s2 = Rot64(s2,43); s1 += s3; 141f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com s3 += data[3]; s5 ^= s1; s2 ^= s3; s3 = Rot64(s3,31); s2 += s4; 142f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com s4 += data[4]; s6 ^= s2; s3 ^= s4; s4 = Rot64(s4,17); s3 += s5; 143f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com s5 += data[5]; s7 ^= s3; s4 ^= s5; s5 = Rot64(s5,28); s4 += s6; 144f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com s6 += data[6]; s8 ^= s4; s5 ^= s6; s6 = Rot64(s6,39); s5 += s7; 145f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com s7 += data[7]; s9 ^= s5; s6 ^= s7; s7 = Rot64(s7,57); s6 += s8; 146f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com s8 += data[8]; s10 ^= s6; s7 ^= s8; s8 = Rot64(s8,55); s7 += s9; 147f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com s9 += data[9]; s11 ^= s7; s8 ^= s9; s9 = Rot64(s9,54); s8 += s10; 148f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com s10 += data[10]; s0 ^= s8; s9 ^= s10; s10 = Rot64(s10,22); s9 += s11; 149f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com s11 += data[11]; s1 ^= s9; s10 ^= s11; s11 = Rot64(s11,46); s10 += s0; 150f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com } 151f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 152f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 153f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // Mix all 12 inputs together so that h0, h1 are a hash of them all. 154f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 155f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // For two inputs differing in just the input bits 156f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // Where "differ" means xor or subtraction 157f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // And the base value is random, or a counting value starting at that bit 158f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // The final result will have each bit of h0, h1 flip 159f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // For every input bit, 160f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // with probability 50 +- .3% 161f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // For every pair of input bits, 162f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // with probability 50 +- 3% 163f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 164f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // This does not rely on the last Mix() call having already mixed some. 165f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // Two iterations was almost good enough for a 64-bit result, but a 166f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 128-bit result is reported, so End() does three iterations. 167f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 168f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com static INLINE void EndPartial( 169f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3, 170f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 &h4, uint64 &h5, uint64 &h6, uint64 &h7, 171f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 &h8, uint64 &h9, uint64 &h10,uint64 &h11) 172f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com { 173f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h11+= h1; h2 ^= h11; h1 = Rot64(h1,44); 174f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h0 += h2; h3 ^= h0; h2 = Rot64(h2,15); 175f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h1 += h3; h4 ^= h1; h3 = Rot64(h3,34); 176f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h2 += h4; h5 ^= h2; h4 = Rot64(h4,21); 177f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h3 += h5; h6 ^= h3; h5 = Rot64(h5,38); 178f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h4 += h6; h7 ^= h4; h6 = Rot64(h6,33); 179f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h5 += h7; h8 ^= h5; h7 = Rot64(h7,10); 180f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h6 += h8; h9 ^= h6; h8 = Rot64(h8,13); 181f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h7 += h9; h10^= h7; h9 = Rot64(h9,38); 182f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h8 += h10; h11^= h8; h10= Rot64(h10,53); 183f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h9 += h11; h0 ^= h9; h11= Rot64(h11,42); 184f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h10+= h0; h1 ^= h10; h0 = Rot64(h0,54); 185f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com } 186f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 187f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com static INLINE void End( 188f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3, 189f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 &h4, uint64 &h5, uint64 &h6, uint64 &h7, 190f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 &h8, uint64 &h9, uint64 &h10,uint64 &h11) 191f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com { 192f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); 193f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); 194f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); 195f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com } 196f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 197f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 198f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // The goal is for each bit of the input to expand into 128 bits of 199f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // apparent entropy before it is fully overwritten. 200f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // n trials both set and cleared at least m bits of h0 h1 h2 h3 201f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // n: 2 m: 29 202f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // n: 3 m: 46 203f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // n: 4 m: 57 204f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // n: 5 m: 107 205f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // n: 6 m: 146 206f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // n: 7 m: 152 207f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // when run forwards or backwards 208f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // for all 1-bit and 2-bit diffs 209f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // with diffs defined by either xor or subtraction 210f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // with a base of all zeros plus a counter, or plus another bit, or random 211f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 212f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com static INLINE void ShortMix(uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3) 213f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com { 214f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h2 = Rot64(h2,50); h2 += h3; h0 ^= h2; 215f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h3 = Rot64(h3,52); h3 += h0; h1 ^= h3; 216f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h0 = Rot64(h0,30); h0 += h1; h2 ^= h0; 217f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h1 = Rot64(h1,41); h1 += h2; h3 ^= h1; 218f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h2 = Rot64(h2,54); h2 += h3; h0 ^= h2; 219f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h3 = Rot64(h3,48); h3 += h0; h1 ^= h3; 220f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h0 = Rot64(h0,38); h0 += h1; h2 ^= h0; 221f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h1 = Rot64(h1,37); h1 += h2; h3 ^= h1; 222f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h2 = Rot64(h2,62); h2 += h3; h0 ^= h2; 223f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h3 = Rot64(h3,34); h3 += h0; h1 ^= h3; 224f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h0 = Rot64(h0,5); h0 += h1; h2 ^= h0; 225f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h1 = Rot64(h1,36); h1 += h2; h3 ^= h1; 226f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com } 227f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 228f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 229f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // Mix all 4 inputs together so that h0, h1 are a hash of them all. 230f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 231f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // For two inputs differing in just the input bits 232f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // Where "differ" means xor or subtraction 233f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // And the base value is random, or a counting value starting at that bit 234f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // The final result will have each bit of h0, h1 flip 235f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // For every input bit, 236f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // with probability 50 +- .3% (it is probably better than that) 237f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // For every pair of input bits, 238f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // with probability 50 +- .75% (the worst case is approximately that) 239f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 240f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com static INLINE void ShortEnd(uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3) 241f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com { 242f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h3 ^= h2; h2 = Rot64(h2,15); h3 += h2; 243f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h0 ^= h3; h3 = Rot64(h3,52); h0 += h3; 244f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h1 ^= h0; h0 = Rot64(h0,26); h1 += h0; 245f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h2 ^= h1; h1 = Rot64(h1,51); h2 += h1; 246f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h3 ^= h2; h2 = Rot64(h2,28); h3 += h2; 247f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h0 ^= h3; h3 = Rot64(h3,9); h0 += h3; 248f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h1 ^= h0; h0 = Rot64(h0,47); h1 += h0; 249f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h2 ^= h1; h1 = Rot64(h1,54); h2 += h1; 250f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h3 ^= h2; h2 = Rot64(h2,32); h3 += h2; 251f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h0 ^= h3; h3 = Rot64(h3,25); h0 += h3; 252f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com h1 ^= h0; h0 = Rot64(h0,63); h1 += h0; 253f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com } 254f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 255f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.comprivate: 256f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 257f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 258f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // Short is used for messages under 192 bytes in length 259f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // Short has a low startup cost, the normal mode is good for long 260f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // keys, the cost crossover is at about 192 bytes. The two modes were 261f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // held to the same quality bar. 262f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 263f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com static void Short( 264f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com const void *message, 265f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com size_t length, 266f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 *hash1, 267f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 *hash2); 268f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 269f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // number of uint64's in internal state 270f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com static const size_t sc_numVars = 12; 271f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 272f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // size of the internal state 273f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com static const size_t sc_blockSize = sc_numVars*8; 274f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 275f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // size of buffer of unhashed data, in bytes 276f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com static const size_t sc_bufSize = 2*sc_blockSize; 277f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 278f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 279f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // sc_const: a constant which: 280f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // * is not zero 281f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // * is odd 282f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // * is a not-very-regular mix of 1's and 0's 283f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // * does not need any other special mathematical properties 284f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com // 285dd462f2c6817d4e09a6ecd99f35e3a04c342afb2tanjent@gmail.com static const uint64 sc_const = 0xdeadbeefdeadbeefULL; 286f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 287f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 m_data[2*sc_numVars]; // unhashed data, for partial messages 288f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint64 m_state[sc_numVars]; // internal state of the hash 289f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com size_t m_length; // total length of the input so far 290f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com uint8 m_remainder; // length of unhashed data stashed in m_data 291f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com}; 292f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 293f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 294f3b789787b93945c974e2cc517b7dc352b28354etanjent@gmail.com 295