xsum_sanity_check.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * xxhsum - Command line interface for xxhash algorithms
  3. * Copyright (C) 2013-2020 Yann Collet
  4. *
  5. * GPL v2 License
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * You can contact the author at:
  22. * - xxHash homepage: https://www.xxhash.com
  23. * - xxHash source repository: https://github.com/Cyan4973/xxHash
  24. */
  25. #include "xsum_config.h"
  26. #include "xsum_sanity_check.h"
  27. #include "xsum_output.h"
  28. #include <stdlib.h>
  29. #include <assert.h>
  30. #include <string.h>
  31. #ifndef XXH_STATIC_LINKING_ONLY
  32. # define XXH_STATIC_LINKING_ONLY
  33. #endif
  34. #include "../xxhash.h"
  35. /* use #define to make them constant, required for initialization */
  36. #define PRIME32 2654435761U
  37. #define PRIME64 11400714785074694797ULL
  38. /*
  39. * Fills a test buffer with pseudorandom data.
  40. *
  41. * This is used in the sanity check - its values must not be changed.
  42. */
  43. XSUM_API void XSUM_fillTestBuffer(XSUM_U8* buffer, size_t len)
  44. {
  45. XSUM_U64 byteGen = PRIME32;
  46. size_t i;
  47. assert(buffer != NULL);
  48. for (i=0; i<len; i++) {
  49. buffer[i] = (XSUM_U8)(byteGen>>56);
  50. byteGen *= PRIME64;
  51. }
  52. }
  53. /* ************************************************
  54. * Self-test:
  55. * ensure results consistency accross platforms
  56. *********************************************** */
  57. #if XSUM_NO_TESTS
  58. XSUM_API void XSUM_sanityCheck(void)
  59. {
  60. XSUM_log("This version of xxhsum is not verified.\n");
  61. }
  62. #else
  63. /*
  64. * Test data vectors
  65. */
  66. typedef struct {
  67. XSUM_U32 len;
  68. XSUM_U32 seed;
  69. XSUM_U32 Nresult;
  70. } XSUM_testdata32_t;
  71. typedef struct {
  72. XSUM_U32 len;
  73. XSUM_U64 seed;
  74. XSUM_U64 Nresult;
  75. } XSUM_testdata64_t;
  76. typedef struct {
  77. XSUM_U32 len;
  78. XSUM_U64 seed;
  79. XXH128_hash_t Nresult;
  80. } XSUM_testdata128_t;
  81. #define SECRET_SAMPLE_NBBYTES 4
  82. typedef struct {
  83. XSUM_U32 len;
  84. XSUM_U8 byte[SECRET_SAMPLE_NBBYTES];
  85. } XSUM_testdata_sample_t;
  86. /* XXH32 */
  87. static const XSUM_testdata32_t XSUM_XXH32_testdata[] = {
  88. { 0, 0, 0x02CC5D05U },
  89. { 0, PRIME32, 0x36B78AE7U },
  90. { 1, 0, 0xCF65B03EU },
  91. { 1, PRIME32, 0xB4545AA4U },
  92. { 14, 0, 0x1208E7E2U },
  93. { 14, PRIME32, 0x6AF1D1FEU },
  94. { 222, 0, 0x5BD11DBDU },
  95. { 222, PRIME32, 0x58803C5FU }
  96. };
  97. /* XXH64 */
  98. static const XSUM_testdata64_t XSUM_XXH64_testdata[] = {
  99. { 0, 0, 0xEF46DB3751D8E999ULL },
  100. { 0, PRIME32, 0xAC75FDA2929B17EFULL },
  101. { 1, 0, 0xE934A84ADB052768ULL },
  102. { 1, PRIME32, 0x5014607643A9B4C3ULL },
  103. { 4, 0, 0x9136A0DCA57457EEULL },
  104. { 14, 0, 0x8282DCC4994E35C8ULL },
  105. { 14, PRIME32, 0xC3BD6BF63DEB6DF0ULL },
  106. { 222, 0, 0xB641AE8CB691C174ULL },
  107. { 222, PRIME32, 0x20CB8AB7AE10C14AULL }
  108. };
  109. /*
  110. * XXH3:
  111. * Due to being a more complex hash function with specializations for certain
  112. * lengths, a more extensive test is used for XXH3.
  113. */
  114. /* XXH3_64bits, seeded */
  115. static const XSUM_testdata64_t XSUM_XXH3_testdata[] = {
  116. { 0, 0, 0x2D06800538D394C2ULL }, /* empty string */
  117. { 0, PRIME64, 0xA8A6B918B2F0364AULL }, /* empty string */
  118. { 1, 0, 0xC44BDFF4074EECDBULL }, /* 1 - 3 */
  119. { 1, PRIME64, 0x032BE332DD766EF8ULL }, /* 1 - 3 */
  120. { 6, 0, 0x27B56A84CD2D7325ULL }, /* 4 - 8 */
  121. { 6, PRIME64, 0x84589C116AB59AB9ULL }, /* 4 - 8 */
  122. { 12, 0, 0xA713DAF0DFBB77E7ULL }, /* 9 - 16 */
  123. { 12, PRIME64, 0xE7303E1B2336DE0EULL }, /* 9 - 16 */
  124. { 24, 0, 0xA3FE70BF9D3510EBULL }, /* 17 - 32 */
  125. { 24, PRIME64, 0x850E80FC35BDD690ULL }, /* 17 - 32 */
  126. { 48, 0, 0x397DA259ECBA1F11ULL }, /* 33 - 64 */
  127. { 48, PRIME64, 0xADC2CBAA44ACC616ULL }, /* 33 - 64 */
  128. { 80, 0, 0xBCDEFBBB2C47C90AULL }, /* 65 - 96 */
  129. { 80, PRIME64, 0xC6DD0CB699532E73ULL }, /* 65 - 96 */
  130. { 195, 0, 0xCD94217EE362EC3AULL }, /* 129-240 */
  131. { 195, PRIME64, 0xBA68003D370CB3D9ULL }, /* 129-240 */
  132. { 403, 0, 0xCDEB804D65C6DEA4ULL }, /* one block, last stripe is overlapping */
  133. { 403, PRIME64, 0x6259F6ECFD6443FDULL }, /* one block, last stripe is overlapping */
  134. { 512, 0, 0x617E49599013CB6BULL }, /* one block, finishing at stripe boundary */
  135. { 512, PRIME64, 0x3CE457DE14C27708ULL }, /* one block, finishing at stripe boundary */
  136. { 2048, 0, 0xDD59E2C3A5F038E0ULL }, /* 2 blocks, finishing at block boundary */
  137. { 2048, PRIME64, 0x66F81670669ABABCULL }, /* 2 blocks, finishing at block boundary */
  138. { 2240, 0, 0x6E73A90539CF2948ULL }, /* 3 blocks, finishing at stripe boundary */
  139. { 2240, PRIME64, 0x757BA8487D1B5247ULL }, /* 3 blocks, finishing at stripe boundary */
  140. { 2367, 0, 0xCB37AEB9E5D361EDULL }, /* 3 blocks, last stripe is overlapping */
  141. { 2367, PRIME64, 0xD2DB3415B942B42AULL } /* 3 blocks, last stripe is overlapping */
  142. };
  143. /* XXH3_64bits, custom secret */
  144. static const XSUM_testdata64_t XSUM_XXH3_withSecret_testdata[] = {
  145. { 0, 0, 0x3559D64878C5C66CULL }, /* empty string */
  146. { 1, 0, 0x8A52451418B2DA4DULL }, /* 1 - 3 */
  147. { 6, 0, 0x82C90AB0519369ADULL }, /* 4 - 8 */
  148. { 12, 0, 0x14631E773B78EC57ULL }, /* 9 - 16 */
  149. { 24, 0, 0xCDD5542E4A9D9FE8ULL }, /* 17 - 32 */
  150. { 48, 0, 0x33ABD54D094B2534ULL }, /* 33 - 64 */
  151. { 80, 0, 0xE687BA1684965297ULL }, /* 65 - 96 */
  152. { 195, 0, 0xA057273F5EECFB20ULL }, /* 129-240 */
  153. { 403, 0, 0x14546019124D43B8ULL }, /* one block, last stripe is overlapping */
  154. { 512, 0, 0x7564693DD526E28DULL }, /* one block, finishing at stripe boundary */
  155. { 2048, 0, 0xD32E975821D6519FULL }, /* >= 2 blodcks, at least one scrambling */
  156. { 2367, 0, 0x293FA8E5173BB5E7ULL }, /* >= 2 blocks, at least one scrambling, last stripe unaligned */
  157. { 64*10*3, 0, 0x751D2EC54BC6038BULL } /* exactly 3 full blocks, not a multiple of 256 */
  158. };
  159. /* XXH3_128bits, seeded */
  160. static const XSUM_testdata128_t XSUM_XXH128_testdata[] = {
  161. { 0, 0, { 0x6001C324468D497FULL, 0x99AA06D3014798D8ULL } }, /* empty string */
  162. { 0, PRIME32, { 0x5444F7869C671AB0ULL, 0x92220AE55E14AB50ULL } }, /* empty string */
  163. { 1, 0, { 0xC44BDFF4074EECDBULL, 0xA6CD5E9392000F6AULL } }, /* 1 - 3 */
  164. { 1, PRIME32, { 0xB53D5557E7F76F8DULL, 0x89B99554BA22467CULL } }, /* 1 - 3 */
  165. { 6, 0, { 0x3E7039BDDA43CFC6ULL, 0x082AFE0B8162D12AULL } }, /* 4 - 8 */
  166. { 6, PRIME32, { 0x269D8F70BE98856EULL, 0x5A865B5389ABD2B1ULL } }, /* 4 - 8 */
  167. { 12, 0, { 0x061A192713F69AD9ULL, 0x6E3EFD8FC7802B18ULL } }, /* 9 - 16 */
  168. { 12, PRIME32, { 0x9BE9F9A67F3C7DFBULL, 0xD7E09D518A3405D3ULL } }, /* 9 - 16 */
  169. { 24, 0, { 0x1E7044D28B1B901DULL, 0x0CE966E4678D3761ULL } }, /* 17 - 32 */
  170. { 24, PRIME32, { 0xD7304C54EBAD40A9ULL, 0x3162026714A6A243ULL } }, /* 17 - 32 */
  171. { 48, 0, { 0xF942219AED80F67BULL, 0xA002AC4E5478227EULL } }, /* 33 - 64 */
  172. { 48, PRIME32, { 0x7BA3C3E453A1934EULL, 0x163ADDE36C072295ULL } }, /* 33 - 64 */
  173. { 81, 0, { 0x5E8BAFB9F95FB803ULL, 0x4952F58181AB0042ULL } }, /* 65 - 96 */
  174. { 81, PRIME32, { 0x703FBB3D7A5F755CULL, 0x2724EC7ADC750FB6ULL } }, /* 65 - 96 */
  175. { 222, 0, { 0xF1AEBD597CEC6B3AULL, 0x337E09641B948717ULL } }, /* 129-240 */
  176. { 222, PRIME32, { 0xAE995BB8AF917A8DULL, 0x91820016621E97F1ULL } }, /* 129-240 */
  177. { 403, 0, { 0xCDEB804D65C6DEA4ULL, 0x1B6DE21E332DD73DULL } }, /* one block, last stripe is overlapping */
  178. { 403, PRIME64, { 0x6259F6ECFD6443FDULL, 0xBED311971E0BE8F2ULL } }, /* one block, last stripe is overlapping */
  179. { 512, 0, { 0x617E49599013CB6BULL, 0x18D2D110DCC9BCA1ULL } }, /* one block, finishing at stripe boundary */
  180. { 512, PRIME64, { 0x3CE457DE14C27708ULL, 0x925D06B8EC5B8040ULL } }, /* one block, finishing at stripe boundary */
  181. { 2048, 0, { 0xDD59E2C3A5F038E0ULL, 0xF736557FD47073A5ULL } }, /* 2 blocks, finishing at block boundary */
  182. { 2048, PRIME32, { 0x230D43F30206260BULL, 0x7FB03F7E7186C3EAULL } }, /* 2 blocks, finishing at block boundary */
  183. { 2240, 0, { 0x6E73A90539CF2948ULL, 0xCCB134FBFA7CE49DULL } }, /* 3 blocks, finishing at stripe boundary */
  184. { 2240, PRIME32, { 0xED385111126FBA6FULL, 0x50A1FE17B338995FULL } }, /* 3 blocks, finishing at stripe boundary */
  185. { 2367, 0, { 0xCB37AEB9E5D361EDULL, 0xE89C0F6FF369B427ULL } }, /* 3 blocks, last stripe is overlapping */
  186. { 2367, PRIME32, { 0x6F5360AE69C2F406ULL, 0xD23AAE4B76C31ECBULL } } /* 3 blocks, last stripe is overlapping */
  187. };
  188. /* XXH128, custom secret */
  189. static const XSUM_testdata128_t XSUM_XXH128_withSecret_testdata[] = {
  190. { 0, 0, { 0x005923CCEECBE8AEULL, 0x5F70F4EA232F1D38ULL } }, /* empty string */
  191. { 1, 0, { 0x8A52451418B2DA4DULL, 0x3A66AF5A9819198EULL } }, /* 1 - 3 */
  192. { 6, 0, { 0x0B61C8ACA7D4778FULL, 0x376BD91B6432F36DULL } }, /* 4 - 8 */
  193. { 12, 0, { 0xAF82F6EBA263D7D8ULL, 0x90A3C2D839F57D0FULL } } /* 9 - 16 */
  194. };
  195. static const XSUM_testdata_sample_t XSUM_XXH3_generateSecret_testdata[] = {
  196. { 0, { 0xB8, 0x26, 0x83, 0x7E } },
  197. { 1, { 0xA6, 0x16, 0x06, 0x7B } },
  198. { XXH3_SECRET_SIZE_MIN - 1, { 0xDA, 0x2A, 0x12, 0x11 } },
  199. { XXH3_SECRET_DEFAULT_SIZE + 500, { 0x7E, 0x48, 0x0C, 0xA7 } }
  200. };
  201. static void XSUM_checkResult32(XXH32_hash_t r1, XXH32_hash_t r2)
  202. {
  203. static int nbTests = 1;
  204. if (r1!=r2) {
  205. XSUM_log("\rError: 32-bit hash test %i: Internal sanity check failed!\n", nbTests);
  206. XSUM_log("\rGot 0x%08X, expected 0x%08X.\n", (unsigned)r1, (unsigned)r2);
  207. XSUM_log("\rNote: If you modified the hash functions, make sure to either update the values\n"
  208. "or temporarily recompile with XSUM_NO_TESTS=1.\n");
  209. exit(1);
  210. }
  211. nbTests++;
  212. }
  213. static void XSUM_checkResult64(XXH64_hash_t r1, XXH64_hash_t r2)
  214. {
  215. static int nbTests = 1;
  216. if (r1!=r2) {
  217. XSUM_log("\rError: 64-bit hash test %i: Internal sanity check failed!\n", nbTests);
  218. XSUM_log("\rGot 0x%08X%08XULL, expected 0x%08X%08XULL.\n",
  219. (unsigned)(r1>>32), (unsigned)r1, (unsigned)(r2>>32), (unsigned)r2);
  220. XSUM_log("\rNote: If you modified the hash functions, make sure to either update the values\n"
  221. "or temporarily recompile with XSUM_NO_TESTS=1.\n");
  222. exit(1);
  223. }
  224. nbTests++;
  225. }
  226. static void XSUM_checkResult128(XXH128_hash_t r1, XXH128_hash_t r2)
  227. {
  228. static int nbTests = 1;
  229. if ((r1.low64 != r2.low64) || (r1.high64 != r2.high64)) {
  230. XSUM_log("\rError: 128-bit hash test %i: Internal sanity check failed.\n", nbTests);
  231. XSUM_log("\rGot { 0x%08X%08XULL, 0x%08X%08XULL }, expected { 0x%08X%08XULL, 0x%08X%08XULL } \n",
  232. (unsigned)(r1.low64>>32), (unsigned)r1.low64, (unsigned)(r1.high64>>32), (unsigned)r1.high64,
  233. (unsigned)(r2.low64>>32), (unsigned)r2.low64, (unsigned)(r2.high64>>32), (unsigned)r2.high64 );
  234. XSUM_log("\rNote: If you modified the hash functions, make sure to either update the values\n"
  235. "or temporarily recompile with XSUM_NO_TESTS=1.\n");
  236. exit(1);
  237. }
  238. nbTests++;
  239. }
  240. static void XSUM_testXXH32(const void* data, const XSUM_testdata32_t* testData)
  241. {
  242. XXH32_state_t *state = XXH32_createState();
  243. size_t pos;
  244. size_t len = testData->len;
  245. XSUM_U32 seed = testData->seed;
  246. XSUM_U32 Nresult = testData->Nresult;
  247. if (len == 0) {
  248. data = NULL;
  249. } else {
  250. assert(data != NULL);
  251. }
  252. assert(state != NULL);
  253. XSUM_checkResult32(XXH32(data, len, seed), Nresult);
  254. (void)XXH32_reset(state, seed);
  255. (void)XXH32_update(state, data, len);
  256. XSUM_checkResult32(XXH32_digest(state), Nresult);
  257. (void)XXH32_reset(state, seed);
  258. for (pos=0; pos<len; pos++)
  259. (void)XXH32_update(state, ((const char*)data)+pos, 1);
  260. XSUM_checkResult32(XXH32_digest(state), Nresult);
  261. XXH32_freeState(state);
  262. }
  263. static void XSUM_testXXH64(const void* data, const XSUM_testdata64_t* testData)
  264. {
  265. XXH64_state_t *state = XXH64_createState();
  266. size_t pos;
  267. size_t len = (size_t)testData->len;
  268. XSUM_U64 seed = testData->seed;
  269. XSUM_U64 Nresult = testData->Nresult;
  270. if (len == 0) {
  271. data = NULL;
  272. } else {
  273. assert(data != NULL);
  274. }
  275. assert(state != NULL);
  276. XSUM_checkResult64(XXH64(data, len, seed), Nresult);
  277. (void)XXH64_reset(state, seed);
  278. (void)XXH64_update(state, data, len);
  279. XSUM_checkResult64(XXH64_digest(state), Nresult);
  280. (void)XXH64_reset(state, seed);
  281. for (pos=0; pos<len; pos++)
  282. (void)XXH64_update(state, ((const char*)data)+pos, 1);
  283. XSUM_checkResult64(XXH64_digest(state), Nresult);
  284. XXH64_freeState(state);
  285. }
  286. /*
  287. * Used to get "random" (but actually 100% reproducible) lengths for
  288. * XSUM_XXH3_randomUpdate.
  289. */
  290. static XSUM_U32 XSUM_rand(void)
  291. {
  292. static XSUM_U64 seed = PRIME32;
  293. seed *= PRIME64;
  294. return (XSUM_U32)(seed >> 40);
  295. }
  296. /*
  297. * Technically, XXH3_64bits_update is identical to XXH3_128bits_update as of
  298. * v0.8.0, but we treat them as separate.
  299. */
  300. typedef XXH_errorcode (*XSUM_XXH3_update_t)(XXH3_state_t* state, const void* input, size_t length);
  301. /*
  302. * Runs the passed XXH3_update variant on random lengths. This is to test the
  303. * more complex logic of the update function, catching bugs like this one:
  304. * https://github.com/Cyan4973/xxHash/issues/378
  305. */
  306. static void XSUM_XXH3_randomUpdate(XXH3_state_t* state, const void* data,
  307. size_t len, XSUM_XXH3_update_t update_fn)
  308. {
  309. size_t p = 0;
  310. while (p < len) {
  311. size_t const modulo = len > 2 ? len : 2;
  312. size_t l = (size_t)(XSUM_rand()) % modulo;
  313. if (p + l > len) l = len - p;
  314. (void)update_fn(state, (const char*)data+p, l);
  315. p += l;
  316. }
  317. }
  318. static void XSUM_testXXH3(const void* data, const XSUM_testdata64_t* testData)
  319. {
  320. size_t len = testData->len;
  321. XSUM_U64 seed = testData->seed;
  322. XSUM_U64 Nresult = testData->Nresult;
  323. if (len == 0) {
  324. data = NULL;
  325. } else {
  326. assert(data != NULL);
  327. }
  328. { XSUM_U64 const Dresult = XXH3_64bits_withSeed(data, len, seed);
  329. XSUM_checkResult64(Dresult, Nresult);
  330. }
  331. /* check that the no-seed variant produces same result as seed==0 */
  332. if (seed == 0) {
  333. XSUM_U64 const Dresult = XXH3_64bits(data, len);
  334. XSUM_checkResult64(Dresult, Nresult);
  335. }
  336. /* streaming API test */
  337. { XXH3_state_t* const state = XXH3_createState();
  338. assert(state != NULL);
  339. /* single ingestion */
  340. (void)XXH3_64bits_reset_withSeed(state, seed);
  341. (void)XXH3_64bits_update(state, data, len);
  342. XSUM_checkResult64(XXH3_64bits_digest(state), Nresult);
  343. /* random ingestion */
  344. (void)XXH3_64bits_reset_withSeed(state, seed);
  345. XSUM_XXH3_randomUpdate(state, data, len, &XXH3_64bits_update);
  346. XSUM_checkResult64(XXH3_64bits_digest(state), Nresult);
  347. /* byte by byte ingestion */
  348. { size_t pos;
  349. (void)XXH3_64bits_reset_withSeed(state, seed);
  350. for (pos=0; pos<len; pos++)
  351. (void)XXH3_64bits_update(state, ((const char*)data)+pos, 1);
  352. XSUM_checkResult64(XXH3_64bits_digest(state), Nresult);
  353. }
  354. XXH3_freeState(state);
  355. }
  356. }
  357. static void XSUM_testXXH3_withSecret(const void* data, const void* secret,
  358. size_t secretSize, const XSUM_testdata64_t* testData)
  359. {
  360. size_t len = (size_t)testData->len;
  361. XSUM_U64 Nresult = testData->Nresult;
  362. if (len == 0) {
  363. data = NULL;
  364. } else {
  365. assert(data != NULL);
  366. }
  367. { XSUM_U64 const Dresult = XXH3_64bits_withSecret(data, len, secret, secretSize);
  368. XSUM_checkResult64(Dresult, Nresult);
  369. }
  370. /* streaming API test */
  371. { XXH3_state_t *state = XXH3_createState();
  372. assert(state != NULL);
  373. (void)XXH3_64bits_reset_withSecret(state, secret, secretSize);
  374. (void)XXH3_64bits_update(state, data, len);
  375. XSUM_checkResult64(XXH3_64bits_digest(state), Nresult);
  376. /* random ingestion */
  377. (void)XXH3_64bits_reset_withSecret(state, secret, secretSize);
  378. XSUM_XXH3_randomUpdate(state, data, len, &XXH3_64bits_update);
  379. XSUM_checkResult64(XXH3_64bits_digest(state), Nresult);
  380. /* byte by byte ingestion */
  381. { size_t pos;
  382. (void)XXH3_64bits_reset_withSecret(state, secret, secretSize);
  383. for (pos=0; pos<len; pos++)
  384. (void)XXH3_64bits_update(state, ((const char*)data)+pos, 1);
  385. XSUM_checkResult64(XXH3_64bits_digest(state), Nresult);
  386. }
  387. XXH3_freeState(state);
  388. }
  389. }
  390. static void XSUM_testXXH128(const void* data, const XSUM_testdata128_t* testData)
  391. {
  392. size_t len = (size_t)testData->len;
  393. XSUM_U64 seed = testData->seed;
  394. XXH128_hash_t const Nresult = testData->Nresult;
  395. if (len == 0) {
  396. data = NULL;
  397. } else {
  398. assert(data != NULL);
  399. }
  400. { XXH128_hash_t const Dresult = XXH3_128bits_withSeed(data, len, seed);
  401. XSUM_checkResult128(Dresult, Nresult);
  402. }
  403. /* check that XXH128() is identical to XXH3_128bits_withSeed() */
  404. { XXH128_hash_t const Dresult2 = XXH128(data, len, seed);
  405. XSUM_checkResult128(Dresult2, Nresult);
  406. }
  407. /* check that the no-seed variant produces same result as seed==0 */
  408. if (seed == 0) {
  409. XXH128_hash_t const Dresult = XXH3_128bits(data, len);
  410. XSUM_checkResult128(Dresult, Nresult);
  411. }
  412. /* streaming API test */
  413. { XXH3_state_t *state = XXH3_createState();
  414. assert(state != NULL);
  415. /* single ingestion */
  416. (void)XXH3_128bits_reset_withSeed(state, seed);
  417. (void)XXH3_128bits_update(state, data, len);
  418. XSUM_checkResult128(XXH3_128bits_digest(state), Nresult);
  419. /* random ingestion */
  420. (void)XXH3_128bits_reset_withSeed(state, seed);
  421. XSUM_XXH3_randomUpdate(state, data, len, &XXH3_128bits_update);
  422. XSUM_checkResult128(XXH3_128bits_digest(state), Nresult);
  423. /* byte by byte ingestion */
  424. { size_t pos;
  425. (void)XXH3_128bits_reset_withSeed(state, seed);
  426. for (pos=0; pos<len; pos++)
  427. (void)XXH3_128bits_update(state, ((const char*)data)+pos, 1);
  428. XSUM_checkResult128(XXH3_128bits_digest(state), Nresult);
  429. }
  430. XXH3_freeState(state);
  431. }
  432. }
  433. static void XSUM_testXXH128_withSecret(const void* data, const void* secret, size_t secretSize, const XSUM_testdata128_t* testData)
  434. {
  435. size_t len = testData->len;
  436. XXH128_hash_t Nresult = testData->Nresult;
  437. if (len == 0) {
  438. data = NULL;
  439. } else if (len>0) {
  440. assert(data != NULL);
  441. }
  442. { XXH128_hash_t const Dresult = XXH3_128bits_withSecret(data, len, secret, secretSize);
  443. XSUM_checkResult128(Dresult, Nresult);
  444. }
  445. /* streaming API test */
  446. { XXH3_state_t* const state = XXH3_createState();
  447. assert(state != NULL);
  448. (void)XXH3_128bits_reset_withSecret(state, secret, secretSize);
  449. (void)XXH3_128bits_update(state, data, len);
  450. XSUM_checkResult128(XXH3_128bits_digest(state), Nresult);
  451. /* random ingestion */
  452. (void)XXH3_128bits_reset_withSecret(state, secret, secretSize);
  453. XSUM_XXH3_randomUpdate(state, data, len, &XXH3_128bits_update);
  454. XSUM_checkResult128(XXH3_128bits_digest(state), Nresult);
  455. /* byte by byte ingestion */
  456. { size_t pos;
  457. (void)XXH3_128bits_reset_withSecret(state, secret, secretSize);
  458. for (pos=0; pos<len; pos++)
  459. (void)XXH3_128bits_update(state, ((const char*)data)+pos, 1);
  460. XSUM_checkResult128(XXH3_128bits_digest(state), Nresult);
  461. }
  462. XXH3_freeState(state);
  463. }
  464. }
  465. static void XSUM_testSecretGenerator(const void* customSeed, const XSUM_testdata_sample_t* testData)
  466. {
  467. static int nbTests = 1;
  468. const int sampleIndex[SECRET_SAMPLE_NBBYTES] = { 0, 62, 131, 191};
  469. XSUM_U8 secretBuffer[XXH3_SECRET_DEFAULT_SIZE] = {0};
  470. XSUM_U8 samples[SECRET_SAMPLE_NBBYTES];
  471. int i;
  472. XXH3_generateSecret(secretBuffer, customSeed, testData->len);
  473. for (i=0; i<SECRET_SAMPLE_NBBYTES; i++) {
  474. samples[i] = secretBuffer[sampleIndex[i]];
  475. }
  476. if (memcmp(samples, testData->byte, sizeof(testData->byte))) {
  477. XSUM_log("\rError: Secret generation test %i: Internal sanity check failed. \n", nbTests);
  478. XSUM_log("\rGot { 0x%02X, 0x%02X, 0x%02X, 0x%02X }, expected { 0x%02X, 0x%02X, 0x%02X, 0x%02X } \n",
  479. samples[0], samples[1], samples[2], samples[3],
  480. testData->byte[0], testData->byte[1], testData->byte[2], testData->byte[3] );
  481. exit(1);
  482. }
  483. nbTests++;
  484. }
  485. /*!
  486. * XSUM_sanityCheck():
  487. * Runs a sanity check before the benchmark.
  488. *
  489. * Exits on an incorrect output.
  490. */
  491. XSUM_API void XSUM_sanityCheck(void)
  492. {
  493. size_t i;
  494. #define SANITY_BUFFER_SIZE 2367
  495. XSUM_U8 sanityBuffer[SANITY_BUFFER_SIZE];
  496. const void* const secret = sanityBuffer + 7;
  497. const size_t secretSize = XXH3_SECRET_SIZE_MIN + 11;
  498. assert(sizeof(sanityBuffer) >= 7 + secretSize);
  499. XSUM_fillTestBuffer(sanityBuffer, sizeof(sanityBuffer));
  500. /* XXH32 */
  501. for (i = 0; i < (sizeof(XSUM_XXH32_testdata)/sizeof(XSUM_XXH32_testdata[0])); i++) {
  502. XSUM_testXXH32(sanityBuffer, &XSUM_XXH32_testdata[i]);
  503. }
  504. /* XXH64 */
  505. for (i = 0; i < (sizeof(XSUM_XXH64_testdata)/sizeof(XSUM_XXH64_testdata[0])); i++) {
  506. XSUM_testXXH64(sanityBuffer, &XSUM_XXH64_testdata[i]);
  507. }
  508. /* XXH3_64bits, seeded */
  509. for (i = 0; i < (sizeof(XSUM_XXH3_testdata)/sizeof(XSUM_XXH3_testdata[0])); i++) {
  510. XSUM_testXXH3(sanityBuffer, &XSUM_XXH3_testdata[i]);
  511. }
  512. /* XXH3_64bits, custom secret */
  513. for (i = 0; i < (sizeof(XSUM_XXH3_withSecret_testdata)/sizeof(XSUM_XXH3_withSecret_testdata[0])); i++) {
  514. XSUM_testXXH3_withSecret(sanityBuffer, secret, secretSize, &XSUM_XXH3_withSecret_testdata[i]);
  515. }
  516. /* XXH128 */
  517. for (i = 0; i < (sizeof(XSUM_XXH128_testdata)/sizeof(XSUM_XXH128_testdata[0])); i++) {
  518. XSUM_testXXH128(sanityBuffer, &XSUM_XXH128_testdata[i]);
  519. }
  520. /* XXH128 with custom Secret */
  521. for (i = 0; i < (sizeof(XSUM_XXH128_withSecret_testdata)/sizeof(XSUM_XXH128_withSecret_testdata[0])); i++) {
  522. XSUM_testXXH128_withSecret(sanityBuffer, secret, secretSize, &XSUM_XXH128_withSecret_testdata[i]);
  523. }
  524. /* secret generator */
  525. for (i = 0; i < (sizeof(XSUM_XXH3_generateSecret_testdata)/sizeof(XSUM_XXH3_generateSecret_testdata[0])); i++) {
  526. XSUM_testSecretGenerator(sanityBuffer, &XSUM_XXH3_generateSecret_testdata[i]);
  527. }
  528. XSUM_logVerbose(3, "\r%70s\r", ""); /* Clean display line */
  529. XSUM_logVerbose(3, "Sanity check -- all tests ok\n");
  530. }
  531. #endif /* !XSUM_NO_TESTS */