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