├── README.md ├── Makefile ├── spooky.h ├── COPYING ├── testspooky.cpp └── spooky.c /README.md: -------------------------------------------------------------------------------- 1 | 2 | SpookyHash in C 3 | =============== 4 | 5 | [SpookyHash][] is a 128-bit noncryptographic hash, written by Bob Jenkins and 6 | placed in the public domain. 7 | 8 | The original implementation is in C++, and this is my conversion to C99. It 9 | should be thread-safe and endian-neutral (but has only been tested on x86). 10 | 11 | It is made available under [CC0][]. 12 | 13 | Some similar efforts: 14 | 15 | - http://github.com/uxcn/spookyhash-c 16 | - http://github.com/centaurean/spookyhash 17 | 18 | [SpookyHash]: http://burtleburtle.net/bob/hash/spooky.html 19 | [CC0]: http://creativecommons.org/publicdomain/zero/1.0/ 20 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ## 2 | ## SpookyHash - 128-bit noncryptographic hash function 3 | ## 4 | ## GCC Makefile 5 | ## 6 | 7 | .SUFFIXES: 8 | 9 | .PHONY: clean all 10 | 11 | CFLAGS = -std=c99 -Wall -Wextra -pedantic -Ofast -flto 12 | CXXFLAGS = -std=c++11 -Wall -Wextra -pedantic -Ofast -flto 13 | LDFLAGS = 14 | 15 | ifeq ($(OS),Windows_NT) 16 | LDFLAGS += -static 17 | ifeq ($(CC),cc) 18 | CC = gcc 19 | endif 20 | endif 21 | 22 | objs = testspooky.o spooky.o 23 | 24 | target = testspooky 25 | 26 | all: $(target) 27 | 28 | %.o : %.c 29 | $(CC) $(CFLAGS) -o $@ -c $< 30 | 31 | %.o : %.cpp 32 | $(CXX) $(CXXFLAGS) -o $@ -c $< 33 | 34 | $(target): $(objs) 35 | $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) 36 | 37 | clean: 38 | $(RM) $(objs) $(target) 39 | -------------------------------------------------------------------------------- /spooky.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SpookyHash - 128-bit noncryptographic hash function 3 | * 4 | * Written in 2012 by Bob Jenkins 5 | * 6 | * Converted to C in 2015 by Joergen Ibsen 7 | * 8 | * To the extent possible under law, the author(s) have dedicated all 9 | * copyright and related and neighboring rights to this software to the 10 | * public domain worldwide. This software is distributed without any 11 | * warranty. 12 | * 13 | * Original comment from SpookyV2.h by Bob Jenkins: 14 | * 15 | * SpookyHash: a 128-bit noncryptographic hash function 16 | * By Bob Jenkins, public domain 17 | * Oct 31 2010: alpha, framework + SpookyHash::Mix appears right 18 | * Oct 31 2011: alpha again, Mix only good to 2^^69 but rest appears right 19 | * Dec 31 2011: beta, improved Mix, tested it for 2-bit deltas 20 | * Feb 2 2012: production, same bits as beta 21 | * Feb 5 2012: adjusted definitions of uint* to be more portable 22 | * Mar 30 2012: 3 bytes/cycle, not 4. Alpha was 4 but wasn't thorough enough. 23 | * August 5 2012: SpookyV2 (different results) 24 | * 25 | * Up to 3 bytes/cycle for long messages. Reasonably fast for short messages. 26 | * All 1 or 2 bit deltas achieve avalanche within 1% bias per output bit. 27 | * 28 | * This was developed for and tested on 64-bit x86-compatible processors. 29 | * It assumes the processor is little-endian. There is a macro 30 | * controlling whether unaligned reads are allowed (by default they are). 31 | * This should be an equally good hash on big-endian machines, but it will 32 | * compute different results on them than on little-endian machines. 33 | * 34 | * Google's CityHash has similar specs to SpookyHash, and CityHash is faster 35 | * on new Intel boxes. MD4 and MD5 also have similar specs, but they are orders 36 | * of magnitude slower. CRCs are two or more times slower, but unlike 37 | * SpookyHash, they have nice math for combining the CRCs of pieces to form 38 | * the CRCs of wholes. There are also cryptographic hashes, but those are even 39 | * slower than MD5. 40 | */ 41 | 42 | #ifndef SPOOKY_H_INCLUDED 43 | #define SPOOKY_H_INCLUDED 44 | 45 | #include 46 | #include 47 | 48 | #ifdef __cplusplus 49 | extern "C" { 50 | #endif 51 | 52 | // number of uint64_t's in internal state 53 | #define SC_NUMVARS 12U 54 | 55 | // size of the internal state 56 | #define SC_BLOCKSIZE (SC_NUMVARS * 8U) 57 | 58 | // size of buffer of unhashed data, in bytes 59 | #define SC_BUFSIZE (2U * SC_BLOCKSIZE) 60 | 61 | struct spooky_state { 62 | uint64_t data[2 * SC_NUMVARS]; // unhashed data, for partial messages 63 | uint64_t state[SC_NUMVARS]; // internal state of the hash 64 | size_t length; // total length of the input so far 65 | uint8_t left; // length of unhashed data stashed in data 66 | }; 67 | 68 | void 69 | spooky_hash128(const void *message, size_t length, uint64_t *hash1, uint64_t *hash2); 70 | 71 | uint64_t 72 | spooky_hash64(const void *message, size_t length, uint64_t seed); 73 | 74 | uint32_t 75 | spooky_hash32(const void *message, size_t length, uint32_t seed); 76 | 77 | void 78 | spooky_init(struct spooky_state *state, uint64_t seed1, uint64_t seed2); 79 | 80 | void 81 | spooky_update(struct spooky_state *state, const void *message, size_t length); 82 | 83 | void 84 | spooky_final(struct spooky_state *state, uint64_t *hash1, uint64_t *hash2); 85 | 86 | #ifdef __cplusplus 87 | } /* extern "C" */ 88 | #endif 89 | 90 | #endif /* SPOOKY_H_INCLUDED */ 91 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | CC0 1.0 Universal 2 | 3 | Statement of Purpose 4 | 5 | The laws of most jurisdictions throughout the world automatically confer 6 | exclusive Copyright and Related Rights (defined below) upon the creator 7 | and subsequent owner(s) (each and all, an "owner") of an original work of 8 | authorship and/or a database (each, a "Work"). 9 | 10 | Certain owners wish to permanently relinquish those rights to a Work for 11 | the purpose of contributing to a commons of creative, cultural and 12 | scientific works ("Commons") that the public can reliably and without fear 13 | of later claims of infringement build upon, modify, incorporate in other 14 | works, reuse and redistribute as freely as possible in any form whatsoever 15 | and for any purposes, including without limitation commercial purposes. 16 | These owners may contribute to the Commons to promote the ideal of a free 17 | culture and the further production of creative, cultural and scientific 18 | works, or to gain reputation or greater distribution for their Work in 19 | part through the use and efforts of others. 20 | 21 | For these and/or other purposes and motivations, and without any 22 | expectation of additional consideration or compensation, the person 23 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 24 | is an owner of Copyright and Related Rights in the Work, voluntarily 25 | elects to apply CC0 to the Work and publicly distribute the Work under its 26 | terms, with knowledge of his or her Copyright and Related Rights in the 27 | Work and the meaning and intended legal effect of CC0 on those rights. 28 | 29 | 1. Copyright and Related Rights. A Work made available under CC0 may be 30 | protected by copyright and related or neighboring rights ("Copyright and 31 | Related Rights"). Copyright and Related Rights include, but are not 32 | limited to, the following: 33 | 34 | i. the right to reproduce, adapt, distribute, perform, display, 35 | communicate, and translate a Work; 36 | ii. moral rights retained by the original author(s) and/or performer(s); 37 | iii. publicity and privacy rights pertaining to a person's image or 38 | likeness depicted in a Work; 39 | iv. rights protecting against unfair competition in regards to a Work, 40 | subject to the limitations in paragraph 4(a), below; 41 | v. rights protecting the extraction, dissemination, use and reuse of data 42 | in a Work; 43 | vi. database rights (such as those arising under Directive 96/9/EC of the 44 | European Parliament and of the Council of 11 March 1996 on the legal 45 | protection of databases, and under any national implementation 46 | thereof, including any amended or successor version of such 47 | directive); and 48 | vii. other similar, equivalent or corresponding rights throughout the 49 | world based on applicable law or treaty, and any national 50 | implementations thereof. 51 | 52 | 2. Waiver. To the greatest extent permitted by, but not in contravention 53 | of, applicable law, Affirmer hereby overtly, fully, permanently, 54 | irrevocably and unconditionally waives, abandons, and surrenders all of 55 | Affirmer's Copyright and Related Rights and associated claims and causes 56 | of action, whether now known or unknown (including existing as well as 57 | future claims and causes of action), in the Work (i) in all territories 58 | worldwide, (ii) for the maximum duration provided by applicable law or 59 | treaty (including future time extensions), (iii) in any current or future 60 | medium and for any number of copies, and (iv) for any purpose whatsoever, 61 | including without limitation commercial, advertising or promotional 62 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 63 | member of the public at large and to the detriment of Affirmer's heirs and 64 | successors, fully intending that such Waiver shall not be subject to 65 | revocation, rescission, cancellation, termination, or any other legal or 66 | equitable action to disrupt the quiet enjoyment of the Work by the public 67 | as contemplated by Affirmer's express Statement of Purpose. 68 | 69 | 3. Public License Fallback. Should any part of the Waiver for any reason 70 | be judged legally invalid or ineffective under applicable law, then the 71 | Waiver shall be preserved to the maximum extent permitted taking into 72 | account Affirmer's express Statement of Purpose. In addition, to the 73 | extent the Waiver is so judged Affirmer hereby grants to each affected 74 | person a royalty-free, non transferable, non sublicensable, non exclusive, 75 | irrevocable and unconditional license to exercise Affirmer's Copyright and 76 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 77 | maximum duration provided by applicable law or treaty (including future 78 | time extensions), (iii) in any current or future medium and for any number 79 | of copies, and (iv) for any purpose whatsoever, including without 80 | limitation commercial, advertising or promotional purposes (the 81 | "License"). The License shall be deemed effective as of the date CC0 was 82 | applied by Affirmer to the Work. Should any part of the License for any 83 | reason be judged legally invalid or ineffective under applicable law, such 84 | partial invalidity or ineffectiveness shall not invalidate the remainder 85 | of the License, and in such case Affirmer hereby affirms that he or she 86 | will not (i) exercise any of his or her remaining Copyright and Related 87 | Rights in the Work or (ii) assert any associated claims and causes of 88 | action with respect to the Work, in either case contrary to Affirmer's 89 | express Statement of Purpose. 90 | 91 | 4. Limitations and Disclaimers. 92 | 93 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 94 | surrendered, licensed or otherwise affected by this document. 95 | b. Affirmer offers the Work as-is and makes no representations or 96 | warranties of any kind concerning the Work, express, implied, 97 | statutory or otherwise, including without limitation warranties of 98 | title, merchantability, fitness for a particular purpose, non 99 | infringement, or the absence of latent or other defects, accuracy, or 100 | the present or absence of errors, whether or not discoverable, all to 101 | the greatest extent permissible under applicable law. 102 | c. Affirmer disclaims responsibility for clearing rights of other persons 103 | that may apply to the Work or any use thereof, including without 104 | limitation any person's Copyright and Related Rights in the Work. 105 | Further, Affirmer disclaims responsibility for obtaining any necessary 106 | consents, permissions or other rights required for any use of the 107 | Work. 108 | d. Affirmer understands and acknowledges that Creative Commons is not a 109 | party to this document and has no duty or obligation with respect to 110 | this CC0 or use of the Work. 111 | 112 | For more information, please see 113 | 114 | -------------------------------------------------------------------------------- /testspooky.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Workaround for issue with inttypes.h 3 | #ifdef __MINGW32__ 4 | # define __USE_MINGW_ANSI_STDIO 1 5 | #endif 6 | 7 | #include "spooky.h" 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #ifdef _WIN32 15 | 16 | #include 17 | 18 | uint64_t get_ticks(void) 19 | { 20 | return GetTickCount(); 21 | } 22 | 23 | #else // _WIN32 24 | 25 | #include 26 | 27 | uint64_t get_ticks(void) 28 | { 29 | struct timespec ts; 30 | int res = clock_gettime(CLOCK_MONOTONIC, &ts); 31 | return res ? (uint64_t)-1 : (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000; 32 | } 33 | 34 | #endif // _WIN32 35 | 36 | class Random 37 | { 38 | public: 39 | inline uint64_t Value() 40 | { 41 | uint64_t e = m_a - Rot64(m_b, 23); 42 | m_a = m_b ^ Rot64(m_c, 16); 43 | m_b = m_c + Rot64(m_d, 11); 44 | m_c = m_d + e; 45 | m_d = e + m_a; 46 | return m_d; 47 | } 48 | 49 | inline void Init( uint64_t seed) 50 | { 51 | m_a = 0xdeadbeef; 52 | m_b = m_c = m_d = seed; 53 | for (int i=0; i<20; ++i) 54 | (void)Value(); 55 | } 56 | 57 | private: 58 | static inline uint64_t Rot64(uint64_t x, int k) 59 | { 60 | return (x << k) | (x >> (64-(k))); 61 | } 62 | 63 | uint64_t m_a; 64 | uint64_t m_b; 65 | uint64_t m_c; 66 | uint64_t m_d; 67 | }; 68 | 69 | // fastest conceivable hash function (for comparison) 70 | static void Add(const void *data, size_t length, uint64_t *hash1, uint64_t *hash2) 71 | { 72 | uint64_t *p64 = (uint64_t *)data; 73 | uint64_t *end = p64 + length/8; 74 | uint64_t hash = *hash1 + *hash2; 75 | while (p64 < end) 76 | { 77 | hash += *p64; 78 | ++p64; 79 | } 80 | *hash1 = hash; 81 | *hash2 = hash; 82 | } 83 | 84 | #define BUFSIZE (512) 85 | void TestResults() 86 | { 87 | printf("\ntesting results ...\n"); 88 | static const uint64_t expected[BUFSIZE] = { 89 | 0x6bf50919,0x70de1d26,0xa2b37298,0x35bc5fbf,0x8223b279,0x5bcb315e,0x53fe88a1,0xf9f1a233, 90 | 0xee193982,0x54f86f29,0xc8772d36,0x9ed60886,0x5f23d1da,0x1ed9f474,0xf2ef0c89,0x83ec01f9, 91 | 0xf274736c,0x7e9ac0df,0xc7aed250,0xb1015811,0xe23470f5,0x48ac20c4,0xe2ab3cd5,0x608f8363, 92 | 0xd0639e68,0xc4e8e7ab,0x863c7c5b,0x4ea63579,0x99ae8622,0x170c658b,0x149ba493,0x027bca7c, 93 | 0xe5cfc8b6,0xce01d9d7,0x11103330,0x5d1f5ed4,0xca720ecb,0xef408aec,0x733b90ec,0x855737a6, 94 | 0x9856c65f,0x647411f7,0x50777c74,0xf0f1a8b7,0x9d7e55a5,0xc68dd371,0xfc1af2cc,0x75728d0a, 95 | 0x390e5fdc,0xf389b84c,0xfb0ccf23,0xc95bad0e,0x5b1cb85a,0x6bdae14f,0x6deb4626,0x93047034, 96 | 0x6f3266c6,0xf529c3bd,0x396322e7,0x3777d042,0x1cd6a5a2,0x197b402e,0xc28d0d2b,0x09c1afb4, 97 | 98 | 0x069c8bb7,0x6f9d4e1e,0xd2621b5c,0xea68108d,0x8660cb8f,0xd61e6de6,0x7fba15c7,0xaacfaa97, 99 | 0xdb381902,0x4ea22649,0x5d414a1e,0xc3fc5984,0xa0fc9e10,0x347dc51c,0x37545fb6,0x8c84b26b, 100 | 0xf57efa5d,0x56afaf16,0xb6e1eb94,0x9218536a,0xe3cc4967,0xd3275ef4,0xea63536e,0x6086e499, 101 | 0xaccadce7,0xb0290d82,0x4ebfd0d6,0x46ccc185,0x2eeb10d3,0x474e3c8c,0x23c84aee,0x3abae1cb, 102 | 0x1499b81a,0xa2993951,0xeed176ad,0xdfcfe84c,0xde4a961f,0x4af13fe6,0xe0069c42,0xc14de8f5, 103 | 0x6e02ce8f,0x90d19f7f,0xbca4a484,0xd4efdd63,0x780fd504,0xe80310e3,0x03abbc12,0x90023849, 104 | 0xd6f6fb84,0xd6b354c5,0x5b8575f0,0x758f14e4,0x450de862,0x90704afb,0x47209a33,0xf226b726, 105 | 0xf858dab8,0x7c0d6de9,0xb05ce777,0xee5ff2d4,0x7acb6d5c,0x2d663f85,0x41c72a91,0x82356bf2, 106 | 107 | 0x94e948ec,0xd358d448,0xeca7814d,0x78cd7950,0xd6097277,0x97782a5d,0xf43fc6f4,0x105f0a38, 108 | 0x9e170082,0x4bfe566b,0x4371d25f,0xef25a364,0x698eb672,0x74f850e4,0x4678ff99,0x4a290dc6, 109 | 0x3918f07c,0x32c7d9cd,0x9f28e0af,0x0d3c5a86,0x7bfc8a45,0xddf0c7e1,0xdeacb86b,0x970b3c5c, 110 | 0x5e29e199,0xea28346d,0x6b59e71b,0xf8a8a46a,0x862f6ce4,0x3ccb740b,0x08761e9e,0xbfa01e5f, 111 | 0xf17cfa14,0x2dbf99fb,0x7a0be420,0x06137517,0xe020b266,0xd25bfc61,0xff10ed00,0x42e6be8b, 112 | 0x029ef587,0x683b26e0,0xb08afc70,0x7c1fd59e,0xbaae9a70,0x98c8c801,0xb6e35a26,0x57083971, 113 | 0x90a6a680,0x1b44169e,0x1dce237c,0x518e0a59,0xccb11358,0x7b8175fb,0xb8fe701a,0x10d259bb, 114 | 0xe806ce10,0x9212be79,0x4604ae7b,0x7fa22a84,0xe715b13a,0x0394c3b2,0x11efbbae,0xe13d9e19, 115 | 116 | 0x77e012bd,0x2d05114c,0xaecf2ddd,0xb2a2b4aa,0xb9429546,0x55dce815,0xc89138f8,0x46dcae20, 117 | 0x1f6f7162,0x0c557ebc,0x5b996932,0xafbbe7e2,0xd2bd5f62,0xff475b9f,0x9cec7108,0xeaddcffb, 118 | 0x5d751aef,0xf68f7bdf,0xf3f4e246,0x00983fcd,0x00bc82bb,0xbf5fd3e7,0xe80c7e2c,0x187d8b1f, 119 | 0xefafb9a7,0x8f27a148,0x5c9606a9,0xf2d2be3e,0xe992d13a,0xe4bcd152,0xce40b436,0x63d6a1fc, 120 | 0xdc1455c4,0x64641e39,0xd83010c9,0x2d535ae0,0x5b748f3e,0xf9a9146b,0x80f10294,0x2859acd4, 121 | 0x5fc846da,0x56d190e9,0x82167225,0x98e4daba,0xbf7865f3,0x00da7ae4,0x9b7cd126,0x644172f8, 122 | 0xde40c78f,0xe8803efc,0xdd331a2b,0x48485c3c,0x4ed01ddc,0x9c0b2d9e,0xb1c6e9d7,0xd797d43c, 123 | 0x274101ff,0x3bf7e127,0x91ebbc56,0x7ffeb321,0x4d42096f,0xd6e9456a,0x0bade318,0x2f40ee0b, 124 | 125 | 0x38cebf03,0x0cbc2e72,0xbf03e704,0x7b3e7a9a,0x8e985acd,0x90917617,0x413895f8,0xf11dde04, 126 | 0xc66f8244,0xe5648174,0x6c420271,0x2469d463,0x2540b033,0xdc788e7b,0xe4140ded,0x0990630a, 127 | 0xa54abed4,0x6e124829,0xd940155a,0x1c8836f6,0x38fda06c,0x5207ab69,0xf8be9342,0x774882a8, 128 | 0x56fc0d7e,0x53a99d6e,0x8241f634,0x9490954d,0x447130aa,0x8cc4a81f,0x0868ec83,0xc22c642d, 129 | 0x47880140,0xfbff3bec,0x0f531f41,0xf845a667,0x08c15fb7,0x1996cd81,0x86579103,0xe21dd863, 130 | 0x513d7f97,0x3984a1f1,0xdfcdc5f4,0x97766a5e,0x37e2b1da,0x41441f3f,0xabd9ddba,0x23b755a9, 131 | 0xda937945,0x103e650e,0x3eef7c8f,0x2760ff8d,0x2493a4cd,0x1d671225,0x3bf4bd4c,0xed6e1728, 132 | 0xc70e9e30,0x4e05e529,0x928d5aa6,0x164d0220,0xb5184306,0x4bd7efb3,0x63830f11,0xf3a1526c, 133 | 134 | 0xf1545450,0xd41d5df5,0x25a5060d,0x77b368da,0x4fe33c7e,0xeae09021,0xfdb053c4,0x2930f18d, 135 | 0xd37109ff,0x8511a781,0xc7e7cdd7,0x6aeabc45,0xebbeaeaa,0x9a0c4f11,0xda252cbb,0x5b248f41, 136 | 0x5223b5eb,0xe32ab782,0x8e6a1c97,0x11d3f454,0x3e05bd16,0x0059001d,0xce13ac97,0xf83b2b4c, 137 | 0x71db5c9a,0xdc8655a6,0x9e98597b,0x3fcae0a2,0x75e63ccd,0x076c72df,0x4754c6ad,0x26b5627b, 138 | 0xd818c697,0x998d5f3d,0xe94fc7b2,0x1f49ad1a,0xca7ff4ea,0x9fe72c05,0xfbd0cbbf,0xb0388ceb, 139 | 0xb76031e3,0xd0f53973,0xfb17907c,0xa4c4c10f,0x9f2d8af9,0xca0e56b0,0xb0d9b689,0xfcbf37a3, 140 | 0xfede8f7d,0xf836511c,0x744003fc,0x89eba576,0xcfdcf6a6,0xc2007f52,0xaaaf683f,0x62d2f9ca, 141 | 0xc996f77f,0x77a7b5b3,0x8ba7d0a4,0xef6a0819,0xa0d903c0,0x01b27431,0x58fffd4c,0x4827f45c, 142 | 143 | 0x44eb5634,0xae70edfc,0x591c740b,0x478bf338,0x2f3b513b,0x67bf518e,0x6fef4a0c,0x1e0b6917, 144 | 0x5ac0edc5,0x2e328498,0x077de7d5,0x5726020b,0x2aeda888,0x45b637ca,0xcf60858d,0x3dc91ae2, 145 | 0x3e6d5294,0xe6900d39,0x0f634c71,0x827a5fa4,0xc713994b,0x1c363494,0x3d43b615,0xe5fe7d15, 146 | 0xf6ada4f2,0x472099d5,0x04360d39,0x7f2a71d0,0x88a4f5ff,0x2c28fac5,0x4cd64801,0xfd78dd33, 147 | 0xc9bdd233,0x21e266cc,0x9bbf419d,0xcbf7d81d,0x80f15f96,0x04242657,0x53fb0f66,0xded11e46, 148 | 0xf2fdba97,0x8d45c9f1,0x4eeae802,0x17003659,0xb9db81a7,0xe734b1b2,0x9503c54e,0xb7c77c3e, 149 | 0x271dd0ab,0xd8b906b5,0x0d540ec6,0xf03b86e0,0x0fdb7d18,0x95e261af,0xad9ec04e,0x381f4a64, 150 | 0xfec798d7,0x09ea20be,0x0ef4ca57,0x1e6195bb,0xfd0da78b,0xcea1653b,0x157d9777,0xf04af50f, 151 | 152 | 0xad7baa23,0xd181714a,0x9bbdab78,0x6c7d1577,0x645eb1e7,0xa0648264,0x35839ca6,0x2287ef45, 153 | 0x32a64ca3,0x26111f6f,0x64814946,0xb0cddaf1,0x4351c59e,0x1b30471c,0xb970788a,0x30e9f597, 154 | 0xd7e58df1,0xc6d2b953,0xf5f37cf4,0x3d7c419e,0xf91ecb2d,0x9c87fd5d,0xb22384ce,0x8c7ac51c, 155 | 0x62c96801,0x57e54091,0x964536fe,0x13d3b189,0x4afd1580,0xeba62239,0xb82ea667,0xae18d43a, 156 | 0xbef04402,0x1942534f,0xc54bf260,0x3c8267f5,0xa1020ddd,0x112fcc8a,0xde596266,0xe91d0856, 157 | 0xf300c914,0xed84478e,0x5b65009e,0x4764da16,0xaf8e07a2,0x4088dc2c,0x9a0cad41,0x2c3f179b, 158 | 0xa67b83f7,0xf27eab09,0xdbe10e28,0xf04c911f,0xd1169f87,0x8e1e4976,0x17f57744,0xe4f5a33f, 159 | 0x27c2e04b,0x0b7523bd,0x07305776,0xc6be7503,0x918fa7c9,0xaf2e2cd9,0x82046f8e,0xcc1c8250 160 | }; 161 | 162 | uint8_t buf[BUFSIZE]; 163 | uint32_t saw[BUFSIZE]; 164 | for (int i=0; i>1); 347 | measure[5][l] = measure[0][l] + measure[1][l]; 348 | measure[5][l] ^= (measure[4][l]>>1); 349 | } 350 | for (int l=0; l<2; ++l) 351 | { 352 | for (int m=0; m maxk) 365 | { 366 | maxk = k; 367 | } 368 | } 369 | } 370 | printf("passed for buffer size %d max %d\n", h, maxk); 371 | } 372 | } 373 | #undef BUFSIZE 374 | #undef TRIES 375 | #undef MEASURES 376 | 377 | 378 | // test that hashing pieces has the same behavior as hashing the whole 379 | #define BUFSIZE 1024 380 | void TestPieces() 381 | { 382 | printf("\ntesting pieces ...\n"); 383 | char buf[BUFSIZE]; 384 | for (int i=0; i 12 | // 13 | // Original comment from SpookyV2.cpp by Bob Jenkins: 14 | // 15 | // Spooky Hash 16 | // A 128-bit noncryptographic hash, for checksums and table lookup 17 | // By Bob Jenkins. Public domain. 18 | // Oct 31 2010: published framework, disclaimer ShortHash isn't right 19 | // Nov 7 2010: disabled ShortHash 20 | // Oct 31 2011: replace End, ShortMix, ShortEnd, enable ShortHash again 21 | // April 10 2012: buffer overflow on platforms without unaligned reads 22 | // July 12 2012: was passing out variables in final to in/out in short 23 | // July 30 2012: I reintroduced the buffer overflow 24 | // August 5 2012: SpookyV2: d = should be d += in short hash, and remove extra mix from long hash 25 | 26 | #include "spooky.h" 27 | 28 | #include 29 | #include 30 | 31 | #define ALLOW_UNALIGNED_READS 1 32 | 33 | // 34 | // SC_CONST: a constant which: 35 | // - is not zero 36 | // - is odd 37 | // - is a not-very-regular mix of 1's and 0's 38 | // - does not need any other special mathematical properties 39 | // 40 | #define SC_CONST 0xDEADBEEFDEADBEEFULL 41 | 42 | #define ROTL64(x, k) (((x) << (k)) | ((x) >> (64 - (k)))) 43 | 44 | #ifdef _MSC_VER 45 | # define restrict __restrict 46 | # define inline __forceinline 47 | #endif 48 | 49 | static bool 50 | spooky_is_aligned(const void *p, size_t size) 51 | { 52 | return (uintptr_t) p % size == 0; 53 | } 54 | 55 | static bool 56 | spooky_is_little_endian(void) 57 | { 58 | const union { 59 | uint32_t i; 60 | uint8_t c[sizeof(uint32_t)]; 61 | } x = { 1 }; 62 | 63 | return x.c[0]; 64 | } 65 | 66 | // 67 | // Read uint64_t in little-endian order. 68 | // 69 | static inline uint64_t 70 | spooky_read_le64(const uint64_t *s) 71 | { 72 | if (spooky_is_little_endian()) { 73 | uint64_t v; 74 | memcpy(&v, s, sizeof(v)); 75 | return v; 76 | } 77 | else { 78 | const uint8_t *p = (const uint8_t *) s; 79 | return (uint64_t) p[0] 80 | | ((uint64_t) p[1] << 8) 81 | | ((uint64_t) p[2] << 16) 82 | | ((uint64_t) p[3] << 24) 83 | | ((uint64_t) p[4] << 32) 84 | | ((uint64_t) p[5] << 40) 85 | | ((uint64_t) p[6] << 48) 86 | | ((uint64_t) p[7] << 56); 87 | } 88 | } 89 | 90 | // 91 | // This is used if the input is 96 bytes long or longer. 92 | // 93 | // The internal state is fully overwritten every 96 bytes. 94 | // Every input bit appears to cause at least 128 bits of entropy 95 | // before 96 other bytes are combined, when run forward or backward 96 | // For every input bit, 97 | // Two inputs differing in just that input bit 98 | // Where "differ" means xor or subtraction 99 | // And the base value is random 100 | // When run forward or backwards one Mix 101 | // I tried 3 pairs of each; they all differed by at least 212 bits. 102 | // 103 | static inline void 104 | spooky_mix(const uint64_t *restrict data, uint64_t *restrict s) 105 | { 106 | s[0] += spooky_read_le64(&data[0]); s[2] ^= s[10]; 107 | s[11] ^= s[0]; s[0] = ROTL64(s[0], 11); s[11] += s[1]; 108 | s[1] += spooky_read_le64(&data[1]); s[3] ^= s[11]; 109 | s[0] ^= s[1]; s[1] = ROTL64(s[1], 32); s[0] += s[2]; 110 | s[2] += spooky_read_le64(&data[2]); s[4] ^= s[0]; 111 | s[1] ^= s[2]; s[2] = ROTL64(s[2], 43); s[1] += s[3]; 112 | s[3] += spooky_read_le64(&data[3]); s[5] ^= s[1]; 113 | s[2] ^= s[3]; s[3] = ROTL64(s[3], 31); s[2] += s[4]; 114 | s[4] += spooky_read_le64(&data[4]); s[6] ^= s[2]; 115 | s[3] ^= s[4]; s[4] = ROTL64(s[4], 17); s[3] += s[5]; 116 | s[5] += spooky_read_le64(&data[5]); s[7] ^= s[3]; 117 | s[4] ^= s[5]; s[5] = ROTL64(s[5], 28); s[4] += s[6]; 118 | s[6] += spooky_read_le64(&data[6]); s[8] ^= s[4]; 119 | s[5] ^= s[6]; s[6] = ROTL64(s[6], 39); s[5] += s[7]; 120 | s[7] += spooky_read_le64(&data[7]); s[9] ^= s[5]; 121 | s[6] ^= s[7]; s[7] = ROTL64(s[7], 57); s[6] += s[8]; 122 | s[8] += spooky_read_le64(&data[8]); s[10] ^= s[6]; 123 | s[7] ^= s[8]; s[8] = ROTL64(s[8], 55); s[7] += s[9]; 124 | s[9] += spooky_read_le64(&data[9]); s[11] ^= s[7]; 125 | s[8] ^= s[9]; s[9] = ROTL64(s[9], 54); s[8] += s[10]; 126 | s[10] += spooky_read_le64(&data[10]); s[0] ^= s[8]; 127 | s[9] ^= s[10]; s[10] = ROTL64(s[10], 22); s[9] += s[11]; 128 | s[11] += spooky_read_le64(&data[11]); s[1] ^= s[9]; 129 | s[10] ^= s[11]; s[11] = ROTL64(s[11], 46); s[10] += s[0]; 130 | } 131 | 132 | // 133 | // Mix all 12 inputs together so that h0, h1 are a hash of them all. 134 | // 135 | // For two inputs differing in just the input bits 136 | // Where "differ" means xor or subtraction 137 | // And the base value is random, or a counting value starting at that bit 138 | // The final result will have each bit of h0, h1 flip 139 | // For every input bit, 140 | // with probability 50 +- .3% 141 | // For every pair of input bits, 142 | // with probability 50 +- 3% 143 | // 144 | // This does not rely on the last Mix() call having already mixed some. 145 | // Two iterations was almost good enough for a 64-bit result, but a 146 | // 128-bit result is reported, so End() does three iterations. 147 | // 148 | static inline void 149 | spooky_end_partial(uint64_t *h) 150 | { 151 | h[11] += h[1]; h[2] ^= h[11]; h[1] = ROTL64(h[1], 44); 152 | h[0] += h[2]; h[3] ^= h[0]; h[2] = ROTL64(h[2], 15); 153 | h[1] += h[3]; h[4] ^= h[1]; h[3] = ROTL64(h[3], 34); 154 | h[2] += h[4]; h[5] ^= h[2]; h[4] = ROTL64(h[4], 21); 155 | h[3] += h[5]; h[6] ^= h[3]; h[5] = ROTL64(h[5], 38); 156 | h[4] += h[6]; h[7] ^= h[4]; h[6] = ROTL64(h[6], 33); 157 | h[5] += h[7]; h[8] ^= h[5]; h[7] = ROTL64(h[7], 10); 158 | h[6] += h[8]; h[9] ^= h[6]; h[8] = ROTL64(h[8], 13); 159 | h[7] += h[9]; h[10] ^= h[7]; h[9] = ROTL64(h[9], 38); 160 | h[8] += h[10]; h[11] ^= h[8]; h[10] = ROTL64(h[10], 53); 161 | h[9] += h[11]; h[0] ^= h[9]; h[11] = ROTL64(h[11], 42); 162 | h[10] += h[0]; h[1] ^= h[10]; h[0] = ROTL64(h[0], 54); 163 | } 164 | 165 | static inline void 166 | spooky_end(const uint64_t *restrict data, uint64_t *restrict h) 167 | { 168 | h[0] += spooky_read_le64(&data[0]); 169 | h[1] += spooky_read_le64(&data[1]); 170 | h[2] += spooky_read_le64(&data[2]); 171 | h[3] += spooky_read_le64(&data[3]); 172 | h[4] += spooky_read_le64(&data[4]); 173 | h[5] += spooky_read_le64(&data[5]); 174 | h[6] += spooky_read_le64(&data[6]); 175 | h[7] += spooky_read_le64(&data[7]); 176 | h[8] += spooky_read_le64(&data[8]); 177 | h[9] += spooky_read_le64(&data[9]); 178 | h[10] += spooky_read_le64(&data[10]); 179 | h[11] += spooky_read_le64(&data[11]); 180 | spooky_end_partial(h); 181 | spooky_end_partial(h); 182 | spooky_end_partial(h); 183 | } 184 | 185 | // 186 | // The goal is for each bit of the input to expand into 128 bits of 187 | // apparent entropy before it is fully overwritten. 188 | // n trials both set and cleared at least m bits of h0 h1 h2 h3 189 | // n: 2 m: 29 190 | // n: 3 m: 46 191 | // n: 4 m: 57 192 | // n: 5 m: 107 193 | // n: 6 m: 146 194 | // n: 7 m: 152 195 | // when run forwards or backwards 196 | // for all 1-bit and 2-bit diffs 197 | // with diffs defined by either xor or subtraction 198 | // with a base of all zeros plus a counter, or plus another bit, or random 199 | // 200 | static inline void 201 | spooky_short_mix(uint64_t *h) 202 | { 203 | h[2] = ROTL64(h[2], 50); h[2] += h[3]; h[0] ^= h[2]; 204 | h[3] = ROTL64(h[3], 52); h[3] += h[0]; h[1] ^= h[3]; 205 | h[0] = ROTL64(h[0], 30); h[0] += h[1]; h[2] ^= h[0]; 206 | h[1] = ROTL64(h[1], 41); h[1] += h[2]; h[3] ^= h[1]; 207 | h[2] = ROTL64(h[2], 54); h[2] += h[3]; h[0] ^= h[2]; 208 | h[3] = ROTL64(h[3], 48); h[3] += h[0]; h[1] ^= h[3]; 209 | h[0] = ROTL64(h[0], 38); h[0] += h[1]; h[2] ^= h[0]; 210 | h[1] = ROTL64(h[1], 37); h[1] += h[2]; h[3] ^= h[1]; 211 | h[2] = ROTL64(h[2], 62); h[2] += h[3]; h[0] ^= h[2]; 212 | h[3] = ROTL64(h[3], 34); h[3] += h[0]; h[1] ^= h[3]; 213 | h[0] = ROTL64(h[0], 5); h[0] += h[1]; h[2] ^= h[0]; 214 | h[1] = ROTL64(h[1], 36); h[1] += h[2]; h[3] ^= h[1]; 215 | } 216 | 217 | // 218 | // Mix all 4 inputs together so that h0, h1 are a hash of them all. 219 | // 220 | // For two inputs differing in just the input bits 221 | // Where "differ" means xor or subtraction 222 | // And the base value is random, or a counting value starting at that bit 223 | // The final result will have each bit of h0, h1 flip 224 | // For every input bit, 225 | // with probability 50 +- .3% (it is probably better than that) 226 | // For every pair of input bits, 227 | // with probability 50 +- .75% (the worst case is approximately that) 228 | // 229 | static inline void 230 | spooky_short_end(uint64_t *h) 231 | { 232 | h[3] ^= h[2]; h[2] = ROTL64(h[2], 15); h[3] += h[2]; 233 | h[0] ^= h[3]; h[3] = ROTL64(h[3], 52); h[0] += h[3]; 234 | h[1] ^= h[0]; h[0] = ROTL64(h[0], 26); h[1] += h[0]; 235 | h[2] ^= h[1]; h[1] = ROTL64(h[1], 51); h[2] += h[1]; 236 | h[3] ^= h[2]; h[2] = ROTL64(h[2], 28); h[3] += h[2]; 237 | h[0] ^= h[3]; h[3] = ROTL64(h[3], 9); h[0] += h[3]; 238 | h[1] ^= h[0]; h[0] = ROTL64(h[0], 47); h[1] += h[0]; 239 | h[2] ^= h[1]; h[1] = ROTL64(h[1], 54); h[2] += h[1]; 240 | h[3] ^= h[2]; h[2] = ROTL64(h[2], 32); h[3] += h[2]; 241 | h[0] ^= h[3]; h[3] = ROTL64(h[3], 25); h[0] += h[3]; 242 | h[1] ^= h[0]; h[0] = ROTL64(h[0], 63); h[1] += h[0]; 243 | } 244 | 245 | // 246 | // short hash ... it could be used on any message, 247 | // but it's used by Spooky just for short messages. 248 | // 249 | static void 250 | spooky_short(const void *restrict message, size_t length, 251 | uint64_t *restrict hash1, uint64_t *restrict hash2) 252 | { 253 | uint64_t buf[2 * SC_NUMVARS]; 254 | union { 255 | const uint8_t *p8; 256 | uint64_t *p64; 257 | } u; 258 | 259 | u.p8 = (const uint8_t *) message; 260 | 261 | if (ALLOW_UNALIGNED_READS == 0 && !spooky_is_aligned(u.p8, 8)) { 262 | memcpy(buf, message, length); 263 | u.p64 = buf; 264 | } 265 | 266 | size_t left = length % 32; 267 | uint64_t h[4]; 268 | h[0] = *hash1; 269 | h[1] = *hash2; 270 | h[2] = SC_CONST; 271 | h[3] = SC_CONST; 272 | 273 | if (length > 15) { 274 | const uint64_t *end = u.p64 + (length / 32) * 4; 275 | 276 | // handle all complete sets of 32 bytes 277 | for (; u.p64 < end; u.p64 += 4) { 278 | h[2] += spooky_read_le64(&u.p64[0]); 279 | h[3] += spooky_read_le64(&u.p64[1]); 280 | spooky_short_mix(h); 281 | h[0] += spooky_read_le64(&u.p64[2]); 282 | h[1] += spooky_read_le64(&u.p64[3]); 283 | } 284 | 285 | //Handle the case of 16+ remaining bytes. 286 | if (left >= 16) { 287 | h[2] += spooky_read_le64(&u.p64[0]); 288 | h[3] += spooky_read_le64(&u.p64[1]); 289 | spooky_short_mix(h); 290 | u.p64 += 2; 291 | left -= 16; 292 | } 293 | } 294 | 295 | // Handle the last 0..15 bytes, and its length 296 | h[3] += ((uint64_t) length) << 56; 297 | switch (left) { 298 | case 15: 299 | h[3] += ((uint64_t) u.p8[14]) << 48; 300 | case 14: 301 | h[3] += ((uint64_t) u.p8[13]) << 40; 302 | case 13: 303 | h[3] += ((uint64_t) u.p8[12]) << 32; 304 | case 12: 305 | h[3] += ((uint64_t) u.p8[11]) << 24; 306 | case 11: 307 | h[3] += ((uint64_t) u.p8[10]) << 16; 308 | case 10: 309 | h[3] += ((uint64_t) u.p8[9]) << 8; 310 | case 9: 311 | h[3] += (uint64_t) u.p8[8]; 312 | case 8: 313 | h[2] += spooky_read_le64(&u.p64[0]); 314 | break; 315 | case 7: 316 | h[2] += ((uint64_t) u.p8[6]) << 48; 317 | case 6: 318 | h[2] += ((uint64_t) u.p8[5]) << 40; 319 | case 5: 320 | h[2] += ((uint64_t) u.p8[4]) << 32; 321 | case 4: 322 | h[2] += ((uint64_t) u.p8[3]) << 24; 323 | case 3: 324 | h[2] += ((uint64_t) u.p8[2]) << 16; 325 | case 2: 326 | h[2] += ((uint64_t) u.p8[1]) << 8; 327 | case 1: 328 | h[2] += (uint64_t) u.p8[0]; 329 | break; 330 | case 0: 331 | h[2] += SC_CONST; 332 | h[3] += SC_CONST; 333 | } 334 | spooky_short_end(h); 335 | *hash1 = h[0]; 336 | *hash2 = h[1]; 337 | } 338 | 339 | uint64_t 340 | spooky_hash64(const void *message, size_t length, uint64_t seed) 341 | { 342 | uint64_t hash1 = seed; 343 | spooky_hash128(message, length, &hash1, &seed); 344 | return hash1; 345 | } 346 | 347 | uint32_t 348 | spooky_hash32(const void *message, size_t length, uint32_t seed) 349 | { 350 | uint64_t hash1 = seed, hash2 = seed; 351 | spooky_hash128(message, length, &hash1, &hash2); 352 | return (uint32_t) hash1; 353 | } 354 | 355 | // do the whole hash in one call 356 | void 357 | spooky_hash128(const void *restrict message, size_t length, 358 | uint64_t *restrict hash1, uint64_t *restrict hash2) 359 | { 360 | if (length < SC_BUFSIZE) { 361 | spooky_short(message, length, hash1, hash2); 362 | return; 363 | } 364 | 365 | uint64_t h[SC_NUMVARS]; 366 | uint64_t buf[SC_NUMVARS]; 367 | uint64_t *end; 368 | union { 369 | const uint8_t *p8; 370 | uint64_t *p64; 371 | } u; 372 | size_t left; 373 | 374 | h[0] = h[3] = h[6] = h[9] = *hash1; 375 | h[1] = h[4] = h[7] = h[10] = *hash2; 376 | h[2] = h[5] = h[8] = h[11] = SC_CONST; 377 | 378 | u.p8 = (const uint8_t *) message; 379 | end = u.p64 + (length / SC_BLOCKSIZE) * SC_NUMVARS; 380 | 381 | // handle all whole SC_BLOCKSIZE blocks of bytes 382 | if (ALLOW_UNALIGNED_READS || spooky_is_aligned(u.p8, 8)) { 383 | do { 384 | spooky_mix(u.p64, h); 385 | u.p64 += SC_NUMVARS; 386 | } while (u.p64 < end); 387 | } 388 | else { 389 | do { 390 | memcpy(buf, u.p64, SC_BLOCKSIZE); 391 | spooky_mix(buf, h); 392 | u.p64 += SC_NUMVARS; 393 | } while (u.p64 < end); 394 | } 395 | 396 | // handle the last partial block of SC_BLOCKSIZE bytes 397 | left = length - ((const uint8_t *) end - (const uint8_t *) message); 398 | memcpy(buf, end, left); 399 | memset(((uint8_t *) buf) + left, 0, SC_BLOCKSIZE - left); 400 | ((uint8_t *) buf)[SC_BLOCKSIZE - 1] = (uint8_t) left; 401 | 402 | // do some final mixing 403 | spooky_end(buf, h); 404 | *hash1 = h[0]; 405 | *hash2 = h[1]; 406 | } 407 | 408 | // init spooky state 409 | void 410 | spooky_init(struct spooky_state *state, uint64_t seed1, uint64_t seed2) 411 | { 412 | state->length = 0; 413 | state->left = 0; 414 | state->state[0] = seed1; 415 | state->state[1] = seed2; 416 | } 417 | 418 | // add a message fragment to the state 419 | void 420 | spooky_update(struct spooky_state *restrict state, 421 | const void *restrict message, size_t length) 422 | { 423 | uint64_t h[SC_NUMVARS]; 424 | size_t newLength = length + state->left; 425 | uint8_t left; 426 | union { 427 | const uint8_t *p8; 428 | uint64_t *p64; 429 | } u; 430 | const uint64_t *end; 431 | 432 | // Is this message fragment too short? If it is, stuff it away. 433 | if (newLength < SC_BUFSIZE) { 434 | memcpy(&((uint8_t *) state->data)[state->left], message, length); 435 | state->length = length + state->length; 436 | state->left = (uint8_t) newLength; 437 | return; 438 | } 439 | 440 | // init the variables 441 | if (state->length < SC_BUFSIZE) { 442 | h[0] = h[3] = h[6] = h[9] = state->state[0]; 443 | h[1] = h[4] = h[7] = h[10] = state->state[1]; 444 | h[2] = h[5] = h[8] = h[11] = SC_CONST; 445 | } 446 | else { 447 | memcpy(h, state->state, sizeof(state->state)); 448 | } 449 | state->length = length + state->length; 450 | 451 | // if we've got anything stuffed away, use it now 452 | if (state->left) { 453 | uint8_t prefix = SC_BUFSIZE - state->left; 454 | memcpy(&(((uint8_t *) state->data)[state->left]), message, prefix); 455 | u.p64 = state->data; 456 | spooky_mix(u.p64, h); 457 | spooky_mix(&u.p64[SC_NUMVARS], h); 458 | u.p8 = ((const uint8_t *) message) + prefix; 459 | length -= prefix; 460 | } 461 | else { 462 | u.p8 = (const uint8_t *) message; 463 | } 464 | 465 | // handle all whole blocks of SC_BLOCKSIZE bytes 466 | end = u.p64 + (length / SC_BLOCKSIZE) * SC_NUMVARS; 467 | left = (uint8_t) (length - ((const uint8_t *) end - u.p8)); 468 | if (ALLOW_UNALIGNED_READS || spooky_is_aligned(u.p8, 8)) { 469 | while (u.p64 < end) { 470 | spooky_mix(u.p64, h); 471 | u.p64 += SC_NUMVARS; 472 | } 473 | } 474 | else { 475 | while (u.p64 < end) { 476 | memcpy(state->data, u.p8, SC_BLOCKSIZE); 477 | spooky_mix(state->data, h); 478 | u.p64 += SC_NUMVARS; 479 | } 480 | } 481 | 482 | // stuff away the last few bytes 483 | state->left = left; 484 | memcpy(state->data, end, left); 485 | 486 | // stuff away the variables 487 | memcpy(state->state, h, sizeof(state->state)); 488 | } 489 | 490 | // report the hash for the concatenation of all message fragments so far 491 | void 492 | spooky_final(struct spooky_state *restrict state, 493 | uint64_t *restrict hash1, uint64_t *restrict hash2) 494 | { 495 | // init the variables 496 | if (state->length < SC_BUFSIZE) { 497 | *hash1 = state->state[0]; 498 | *hash2 = state->state[1]; 499 | spooky_short(state->data, state->length, hash1, hash2); 500 | return; 501 | } 502 | 503 | const uint64_t *data = (const uint64_t *) state->data; 504 | uint8_t left = state->left; 505 | 506 | uint64_t h[SC_NUMVARS]; 507 | memcpy(h, state->state, sizeof(state->state)); 508 | 509 | if (left >= SC_BLOCKSIZE) { 510 | // m_data can contain two blocks; handle any whole first block 511 | spooky_mix(data, h); 512 | data += SC_NUMVARS; 513 | left -= SC_BLOCKSIZE; 514 | } 515 | 516 | // mix in the last partial block, and the length mod SC_BLOCKSIZE 517 | memset(&((uint8_t *) data)[left], 0, (SC_BLOCKSIZE - left)); 518 | 519 | ((uint8_t *) data)[SC_BLOCKSIZE - 1] = left; 520 | 521 | // do some final mixing 522 | spooky_end(data, h); 523 | 524 | *hash1 = h[0]; 525 | *hash2 = h[1]; 526 | } 527 | --------------------------------------------------------------------------------