| 1 |
/* |
| 2 |
* xxhsum - Command line interface for xxhash algorithms |
| 3 |
* Copyright (C) 2013-2021 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 |
|
| 26 |
#include "xsum_sanity_check.h" |
| 27 |
#include "xsum_output.h" /* XSUM_log */ |
| 28 |
#ifndef XXH_STATIC_LINKING_ONLY |
| 29 |
# define XXH_STATIC_LINKING_ONLY |
| 30 |
#endif |
| 31 |
#include "../xxhash.h" |
| 32 |
|
| 33 |
#include <stdlib.h> /* exit */ |
| 34 |
#include <assert.h> |
| 35 |
#include <string.h> /* memcmp */ |
| 36 |
|
| 37 |
/* use #define to make them constant, required for initialization */ |
| 38 |
#define PRIME32 2654435761U |
| 39 |
#define PRIME64 11400714785074694797ULL |
| 40 |
|
| 41 |
/* |
| 42 |
* Fills a test buffer with pseudorandom data. |
| 43 |
* |
| 44 |
* This is used in the sanity check - its values must not be changed. |
| 45 |
*/ |
| 46 |
XSUM_API void XSUM_fillTestBuffer(XSUM_U8* buffer, size_t len) |
| 47 |
{ |
| 48 |
XSUM_U64 byteGen = PRIME32; |
| 49 |
size_t i; |
| 50 |
|
| 51 |
assert(buffer != NULL); |
| 52 |
|
| 53 |
for (i=0; i<len; i++) { |
| 54 |
buffer[i] = (XSUM_U8)(byteGen>>56); |
| 55 |
byteGen *= PRIME64; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
/* ************************************************ |
| 62 |
* Self-test: |
| 63 |
* ensure results consistency across platforms |
| 64 |
*********************************************** */ |
| 65 |
#if XSUM_NO_TESTS |
| 66 |
XSUM_API void XSUM_sanityCheck(void) |
| 67 |
{ |
| 68 |
XSUM_log("This version of xxhsum is not verified.\n"); |
| 69 |
} |
| 70 |
#else |
| 71 |
|
| 72 |
/* |
| 73 |
* Test data vectors |
| 74 |
*/ |
| 75 |
typedef struct { |
| 76 |
XSUM_U32 len; |
| 77 |
XSUM_U32 seed; |
| 78 |
XSUM_U32 Nresult; |
| 79 |
} XSUM_testdata32_t; |
| 80 |
|
| 81 |
typedef struct { |
| 82 |
XSUM_U32 len; |
| 83 |
XSUM_U64 seed; |
| 84 |
XSUM_U64 Nresult; |
| 85 |
} XSUM_testdata64_t; |
| 86 |
|
| 87 |
typedef struct { |
| 88 |
XSUM_U32 len; |
| 89 |
XSUM_U64 seed; |
| 90 |
XXH128_hash_t Nresult; |
| 91 |
} XSUM_testdata128_t; |
| 92 |
|
| 93 |
#define SECRET_SAMPLE_NBBYTES 5 |
| 94 |
typedef struct { |
| 95 |
XSUM_U32 seedLen; |
| 96 |
XSUM_U32 secretLen; |
| 97 |
XSUM_U8 byte[SECRET_SAMPLE_NBBYTES]; |
| 98 |
} XSUM_testdata_sample_t; |
| 99 |
|
| 100 |
/* XXH32 */ |
| 101 |
static const XSUM_testdata32_t XSUM_XXH32_testdata[] = { |
| 102 |
{ 0, 0, 0x02CC5D05U }, |
| 103 |
{ 0, PRIME32, 0x36B78AE7U }, |
| 104 |
{ 1, 0, 0xCF65B03EU }, |
| 105 |
{ 1, PRIME32, 0xB4545AA4U }, |
| 106 |
{ 14, 0, 0x1208E7E2U }, |
| 107 |
{ 14, PRIME32, 0x6AF1D1FEU }, |
| 108 |
{ 222, 0, 0x5BD11DBDU }, |
| 109 |
{ 222, PRIME32, 0x58803C5FU } |
| 110 |
}; |
| 111 |
|
| 112 |
/* XXH64 */ |
| 113 |
static const XSUM_testdata64_t XSUM_XXH64_testdata[] = { |
| 114 |
{ 0, 0, 0xEF46DB3751D8E999ULL }, |
| 115 |
{ 0, PRIME32, 0xAC75FDA2929B17EFULL }, |
| 116 |
{ 1, 0, 0xE934A84ADB052768ULL }, |
| 117 |
{ 1, PRIME32, 0x5014607643A9B4C3ULL }, |
| 118 |
{ 4, 0, 0x9136A0DCA57457EEULL }, |
| 119 |
{ 14, 0, 0x8282DCC4994E35C8ULL }, |
| 120 |
{ 14, PRIME32, 0xC3BD6BF63DEB6DF0ULL }, |
| 121 |
{ 222, 0, 0xB641AE8CB691C174ULL }, |
| 122 |
{ 222, PRIME32, 0x20CB8AB7AE10C14AULL } |
| 123 |
}; |
| 124 |
/* |
| 125 |
* XXH3: |
| 126 |
* Due to being a more complex hash function with specializations for certain |
| 127 |
* lengths, a more extensive test is used for XXH3. |
| 128 |
*/ |
| 129 |
|
| 130 |
/* XXH3_64bits, seeded */ |
| 131 |
static const XSUM_testdata64_t XSUM_XXH3_testdata[] = { |
| 132 |
{ 0, 0, 0x2D06800538D394C2ULL }, /* empty string */ |
| 133 |
{ 0, PRIME64, 0xA8A6B918B2F0364AULL }, |
| 134 |
{ 1, 0, 0xC44BDFF4074EECDBULL }, /* 1 - 3 */ |
| 135 |
{ 1, PRIME64, 0x032BE332DD766EF8ULL }, |
| 136 |
{ 6, 0, 0x27B56A84CD2D7325ULL }, /* 4 - 8 */ |
| 137 |
{ 6, PRIME64, 0x84589C116AB59AB9ULL }, |
| 138 |
{ 12, 0, 0xA713DAF0DFBB77E7ULL }, /* 9 - 16 */ |
| 139 |
{ 12, PRIME64, 0xE7303E1B2336DE0EULL }, |
| 140 |
{ 24, 0, 0xA3FE70BF9D3510EBULL }, /* 17 - 32 */ |
| 141 |
{ 24, PRIME64, 0x850E80FC35BDD690ULL }, |
| 142 |
{ 48, 0, 0x397DA259ECBA1F11ULL }, /* 33 - 64 */ |
| 143 |
{ 48, PRIME64, 0xADC2CBAA44ACC616ULL }, |
| 144 |
{ 80, 0, 0xBCDEFBBB2C47C90AULL }, /* 65 - 96 */ |
| 145 |
{ 80, PRIME64, 0xC6DD0CB699532E73ULL }, |
| 146 |
{ 195, 0, 0xCD94217EE362EC3AULL }, /* 129-240 */ |
| 147 |
{ 195, PRIME64, 0xBA68003D370CB3D9ULL }, |
| 148 |
|
| 149 |
{ 403, 0, 0xCDEB804D65C6DEA4ULL }, /* one block, last stripe is overlapping */ |
| 150 |
{ 403, PRIME64, 0x6259F6ECFD6443FDULL }, |
| 151 |
{ 512, 0, 0x617E49599013CB6BULL }, /* one block, finishing at stripe boundary */ |
| 152 |
{ 512, PRIME64, 0x3CE457DE14C27708ULL }, |
| 153 |
{ 2048, 0, 0xDD59E2C3A5F038E0ULL }, /* 2 blocks, finishing at block boundary */ |
| 154 |
{ 2048, PRIME64, 0x66F81670669ABABCULL }, |
| 155 |
{ 2099, 0, 0xC6B9D9B3FC9AC765ULL }, /* 2 blocks + 1 partial block, to detect off-by-one scrambling issues, like #816 */ |
| 156 |
{ 2099, PRIME64, 0x184F316843663974ULL }, |
| 157 |
{ 2240, 0, 0x6E73A90539CF2948ULL }, /* 3 blocks, finishing at stripe boundary */ |
| 158 |
{ 2240, PRIME64, 0x757BA8487D1B5247ULL }, |
| 159 |
{ 2367, 0, 0xCB37AEB9E5D361EDULL }, /* 3 blocks, last stripe is overlapping */ |
| 160 |
{ 2367, PRIME64, 0xD2DB3415B942B42AULL } |
| 161 |
}; |
| 162 |
/* XXH3_64bits, custom secret */ |
| 163 |
static const XSUM_testdata64_t XSUM_XXH3_withSecret_testdata[] = { |
| 164 |
{ 0, 0, 0x3559D64878C5C66CULL }, /* empty string */ |
| 165 |
{ 1, 0, 0x8A52451418B2DA4DULL }, /* 1 - 3 */ |
| 166 |
{ 6, 0, 0x82C90AB0519369ADULL }, /* 4 - 8 */ |
| 167 |
{ 12, 0, 0x14631E773B78EC57ULL }, /* 9 - 16 */ |
| 168 |
{ 24, 0, 0xCDD5542E4A9D9FE8ULL }, /* 17 - 32 */ |
| 169 |
{ 48, 0, 0x33ABD54D094B2534ULL }, /* 33 - 64 */ |
| 170 |
{ 80, 0, 0xE687BA1684965297ULL }, /* 65 - 96 */ |
| 171 |
{ 195, 0, 0xA057273F5EECFB20ULL }, /* 129-240 */ |
| 172 |
|
| 173 |
{ 403, 0, 0x14546019124D43B8ULL }, /* one block, last stripe is overlapping */ |
| 174 |
{ 512, 0, 0x7564693DD526E28DULL }, /* one block, finishing at stripe boundary */ |
| 175 |
{ 2048, 0, 0xD32E975821D6519FULL }, /* >= 2 blodcks, at least one scrambling */ |
| 176 |
{ 2367, 0, 0x293FA8E5173BB5E7ULL }, /* >= 2 blocks, at least one scrambling, last stripe unaligned */ |
| 177 |
|
| 178 |
{ 64*10*3, 0, 0x751D2EC54BC6038BULL } /* exactly 3 full blocks, not a multiple of 256 */ |
| 179 |
}; |
| 180 |
/* XXH3_128bits, seeded */ |
| 181 |
static const XSUM_testdata128_t XSUM_XXH128_testdata[] = { |
| 182 |
{ 0, 0, { 0x6001C324468D497FULL, 0x99AA06D3014798D8ULL } }, /* empty string */ |
| 183 |
{ 0, PRIME32, { 0x5444F7869C671AB0ULL, 0x92220AE55E14AB50ULL } }, |
| 184 |
{ 1, 0, { 0xC44BDFF4074EECDBULL, 0xA6CD5E9392000F6AULL } }, /* 1 - 3 */ |
| 185 |
{ 1, PRIME32, { 0xB53D5557E7F76F8DULL, 0x89B99554BA22467CULL } }, |
| 186 |
{ 6, 0, { 0x3E7039BDDA43CFC6ULL, 0x082AFE0B8162D12AULL } }, /* 4 - 8 */ |
| 187 |
{ 6, PRIME32, { 0x269D8F70BE98856EULL, 0x5A865B5389ABD2B1ULL } }, |
| 188 |
{ 12, 0, { 0x061A192713F69AD9ULL, 0x6E3EFD8FC7802B18ULL } }, /* 9 - 16 */ |
| 189 |
{ 12, PRIME32, { 0x9BE9F9A67F3C7DFBULL, 0xD7E09D518A3405D3ULL } }, |
| 190 |
{ 24, 0, { 0x1E7044D28B1B901DULL, 0x0CE966E4678D3761ULL } }, /* 17 - 32 */ |
| 191 |
{ 24, PRIME32, { 0xD7304C54EBAD40A9ULL, 0x3162026714A6A243ULL } }, |
| 192 |
{ 48, 0, { 0xF942219AED80F67BULL, 0xA002AC4E5478227EULL } }, /* 33 - 64 */ |
| 193 |
{ 48, PRIME32, { 0x7BA3C3E453A1934EULL, 0x163ADDE36C072295ULL } }, |
| 194 |
{ 81, 0, { 0x5E8BAFB9F95FB803ULL, 0x4952F58181AB0042ULL } }, /* 65 - 96 */ |
| 195 |
{ 81, PRIME32, { 0x703FBB3D7A5F755CULL, 0x2724EC7ADC750FB6ULL } }, |
| 196 |
{ 222, 0, { 0xF1AEBD597CEC6B3AULL, 0x337E09641B948717ULL } }, /* 129-240 */ |
| 197 |
{ 222, PRIME32, { 0xAE995BB8AF917A8DULL, 0x91820016621E97F1ULL } }, |
| 198 |
|
| 199 |
{ 403, 0, { 0xCDEB804D65C6DEA4ULL, 0x1B6DE21E332DD73DULL } }, /* one block, last stripe is overlapping */ |
| 200 |
{ 403, PRIME64, { 0x6259F6ECFD6443FDULL, 0xBED311971E0BE8F2ULL } }, |
| 201 |
{ 512, 0, { 0x617E49599013CB6BULL, 0x18D2D110DCC9BCA1ULL } }, /* one block, finishing at stripe boundary */ |
| 202 |
{ 512, PRIME64, { 0x3CE457DE14C27708ULL, 0x925D06B8EC5B8040ULL } }, |
| 203 |
{ 2048, 0, { 0xDD59E2C3A5F038E0ULL, 0xF736557FD47073A5ULL } }, /* 2 blocks, finishing at block boundary */ |
| 204 |
{ 2048, PRIME32, { 0x230D43F30206260BULL, 0x7FB03F7E7186C3EAULL } }, |
| 205 |
{ 2240, 0, { 0x6E73A90539CF2948ULL, 0xCCB134FBFA7CE49DULL } }, /* 3 blocks, finishing at stripe boundary */ |
| 206 |
{ 2240, PRIME32, { 0xED385111126FBA6FULL, 0x50A1FE17B338995FULL } }, |
| 207 |
{ 2367, 0, { 0xCB37AEB9E5D361EDULL, 0xE89C0F6FF369B427ULL } }, /* 3 blocks, last stripe is overlapping */ |
| 208 |
{ 2367, PRIME32, { 0x6F5360AE69C2F406ULL, 0xD23AAE4B76C31ECBULL } } |
| 209 |
}; |
| 210 |
|
| 211 |
/* XXH128, custom secret */ |
| 212 |
static const XSUM_testdata128_t XSUM_XXH128_withSecret_testdata[] = { |
| 213 |
{ 0, 0, { 0x005923CCEECBE8AEULL, 0x5F70F4EA232F1D38ULL } }, /* empty string */ |
| 214 |
{ 1, 0, { 0x8A52451418B2DA4DULL, 0x3A66AF5A9819198EULL } }, /* 1 - 3 */ |
| 215 |
{ 6, 0, { 0x0B61C8ACA7D4778FULL, 0x376BD91B6432F36DULL } }, /* 4 - 8 */ |
| 216 |
{ 12, 0, { 0xAF82F6EBA263D7D8ULL, 0x90A3C2D839F57D0FULL } } /* 9 - 16 */ |
| 217 |
}; |
| 218 |
|
| 219 |
#define SECRET_SIZE_MAX 9867 |
| 220 |
static const XSUM_testdata_sample_t XSUM_XXH3_generateSecret_testdata[] = { |
| 221 |
{ 0, 192, { 0xE7, 0x8C, 0x77, 0x77, 0x00 } }, |
| 222 |
{ 1, 240, { 0x2B, 0x3E, 0xDE, 0xC1, 0x00 } }, |
| 223 |
{ XXH3_SECRET_SIZE_MIN - 1, 277, { 0xE8, 0x39, 0x6C, 0xCC, 0x7B } }, |
| 224 |
{ XXH3_SECRET_DEFAULT_SIZE + 500, SECRET_SIZE_MAX, { 0xD6, 0x1C, 0x41, 0x17, 0xB3 } } |
| 225 |
}; |
| 226 |
|
| 227 |
static void XSUM_checkResult32(XXH32_hash_t r1, XXH32_hash_t r2) |
| 228 |
{ |
| 229 |
static int nbTests = 1; |
| 230 |
if (r1!=r2) { |
| 231 |
XSUM_log("\rError: 32-bit hash test %i: Internal sanity check failed!\n", nbTests); |
| 232 |
XSUM_log("\rGot 0x%08X, expected 0x%08X.\n", (unsigned)r1, (unsigned)r2); |
| 233 |
XSUM_log("\rNote: If you modified the hash functions, make sure to either update the values\n" |
| 234 |
"or temporarily recompile with XSUM_NO_TESTS=1.\n"); |
| 235 |
exit(1); |
| 236 |
} |
| 237 |
nbTests++; |
| 238 |
} |
| 239 |
|
| 240 |
static void XSUM_checkResult64(XXH64_hash_t r1, XXH64_hash_t r2) |
| 241 |
{ |
| 242 |
static int nbTests = 1; |
| 243 |
if (r1!=r2) { |
| 244 |
XSUM_log("\rError: 64-bit hash test %i: Internal sanity check failed!\n", nbTests); |
| 245 |
XSUM_log("\rGot 0x%08X%08XULL, expected 0x%08X%08XULL.\n", |
| 246 |
(unsigned)(r1>>32), (unsigned)r1, (unsigned)(r2>>32), (unsigned)r2); |
| 247 |
XSUM_log("\rNote: If you modified the hash functions, make sure to either update the values\n" |
| 248 |
"or temporarily recompile with XSUM_NO_TESTS=1.\n"); |
| 249 |
exit(1); |
| 250 |
} |
| 251 |
nbTests++; |
| 252 |
} |
| 253 |
|
| 254 |
static void XSUM_checkResult128(XXH128_hash_t r1, XXH128_hash_t r2) |
| 255 |
{ |
| 256 |
static int nbTests = 1; |
| 257 |
if ((r1.low64 != r2.low64) || (r1.high64 != r2.high64)) { |
| 258 |
XSUM_log("\rError: 128-bit hash test %i: Internal sanity check failed.\n", nbTests); |
| 259 |
XSUM_log("\rGot { 0x%08X%08XULL, 0x%08X%08XULL }, expected { 0x%08X%08XULL, 0x%08X%08XULL } \n", |
| 260 |
(unsigned)(r1.low64>>32), (unsigned)r1.low64, (unsigned)(r1.high64>>32), (unsigned)r1.high64, |
| 261 |
(unsigned)(r2.low64>>32), (unsigned)r2.low64, (unsigned)(r2.high64>>32), (unsigned)r2.high64 ); |
| 262 |
XSUM_log("\rNote: If you modified the hash functions, make sure to either update the values\n" |
| 263 |
"or temporarily recompile with XSUM_NO_TESTS=1.\n"); |
| 264 |
exit(1); |
| 265 |
} |
| 266 |
nbTests++; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
static void XSUM_testXXH32(const void* data, const XSUM_testdata32_t* testData) |
| 271 |
{ |
| 272 |
XXH32_state_t *state = XXH32_createState(); |
| 273 |
size_t pos; |
| 274 |
|
| 275 |
size_t len = testData->len; |
| 276 |
XSUM_U32 seed = testData->seed; |
| 277 |
XSUM_U32 Nresult = testData->Nresult; |
| 278 |
|
| 279 |
if (len == 0) { |
| 280 |
data = NULL; |
| 281 |
} else { |
| 282 |
assert(data != NULL); |
| 283 |
} |
| 284 |
|
| 285 |
assert(state != NULL); |
| 286 |
|
| 287 |
XSUM_checkResult32(XXH32(data, len, seed), Nresult); |
| 288 |
|
| 289 |
(void)XXH32_reset(state, seed); |
| 290 |
(void)XXH32_update(state, data, len); |
| 291 |
XSUM_checkResult32(XXH32_digest(state), Nresult); |
| 292 |
|
| 293 |
(void)XXH32_reset(state, seed); |
| 294 |
for (pos=0; pos<len; pos++) |
| 295 |
(void)XXH32_update(state, ((const char*)data)+pos, 1); |
| 296 |
XSUM_checkResult32(XXH32_digest(state), Nresult); |
| 297 |
XXH32_freeState(state); |
| 298 |
} |
| 299 |
|
| 300 |
static void XSUM_testXXH64(const void* data, const XSUM_testdata64_t* testData) |
| 301 |
{ |
| 302 |
XXH64_state_t *state = XXH64_createState(); |
| 303 |
size_t pos; |
| 304 |
|
| 305 |
size_t len = (size_t)testData->len; |
| 306 |
XSUM_U64 seed = testData->seed; |
| 307 |
XSUM_U64 Nresult = testData->Nresult; |
| 308 |
|
| 309 |
if (len == 0) { |
| 310 |
data = NULL; |
| 311 |
} else { |
| 312 |
assert(data != NULL); |
| 313 |
} |
| 314 |
|
| 315 |
assert(state != NULL); |
| 316 |
|
| 317 |
XSUM_checkResult64(XXH64(data, len, seed), Nresult); |
| 318 |
|
| 319 |
(void)XXH64_reset(state, seed); |
| 320 |
(void)XXH64_update(state, data, len); |
| 321 |
XSUM_checkResult64(XXH64_digest(state), Nresult); |
| 322 |
|
| 323 |
(void)XXH64_reset(state, seed); |
| 324 |
for (pos=0; pos<len; pos++) |
| 325 |
(void)XXH64_update(state, ((const char*)data)+pos, 1); |
| 326 |
XSUM_checkResult64(XXH64_digest(state), Nresult); |
| 327 |
XXH64_freeState(state); |
| 328 |
} |
| 329 |
|
| 330 |
/* |
| 331 |
* Used to get "random" (but actually 100% reproducible) lengths for |
| 332 |
* XSUM_XXH3_randomUpdate. |
| 333 |
*/ |
| 334 |
static XSUM_U32 XSUM_rand(void) |
| 335 |
{ |
| 336 |
static XSUM_U64 seed = PRIME32; |
| 337 |
seed *= PRIME64; |
| 338 |
return (XSUM_U32)(seed >> 40); |
| 339 |
} |
| 340 |
|
| 341 |
/* |
| 342 |
* Technically, XXH3_64bits_update is identical to XXH3_128bits_update as of |
| 343 |
* v0.8.0, but we treat them as separate. |
| 344 |
*/ |
| 345 |
typedef XXH_errorcode (*XSUM_XXH3_update_t)(XXH3_state_t* state, const void* input, size_t length); |
| 346 |
|
| 347 |
/* |
| 348 |
* Runs the passed XXH3_update variant on random lengths. This is to test the |
| 349 |
* more complex logic of the update function, catching bugs like this one: |
| 350 |
* https://github.com/Cyan4973/xxHash/issues/378 |
| 351 |
*/ |
| 352 |
static void XSUM_XXH3_randomUpdate(XXH3_state_t* state, const void* data, |
| 353 |
size_t len, XSUM_XXH3_update_t update_fn) |
| 354 |
{ |
| 355 |
size_t p = 0; |
| 356 |
while (p < len) { |
| 357 |
size_t const modulo = len > 2 ? len : 2; |
| 358 |
size_t l = (size_t)(XSUM_rand()) % modulo; |
| 359 |
if (p + l > len) l = len - p; |
| 360 |
(void)update_fn(state, (const char*)data+p, l); |
| 361 |
p += l; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
static void XSUM_testXXH3(const void* data, const XSUM_testdata64_t* testData) |
| 366 |
{ |
| 367 |
size_t len = testData->len; |
| 368 |
XSUM_U64 seed = testData->seed; |
| 369 |
XSUM_U64 Nresult = testData->Nresult; |
| 370 |
if (len == 0) { |
| 371 |
data = NULL; |
| 372 |
} else { |
| 373 |
assert(data != NULL); |
| 374 |
} |
| 375 |
{ XSUM_U64 const Dresult = XXH3_64bits_withSeed(data, len, seed); |
| 376 |
XSUM_checkResult64(Dresult, Nresult); |
| 377 |
} |
| 378 |
|
| 379 |
/* check that the no-seed variant produces same result as seed==0 */ |
| 380 |
if (seed == 0) { |
| 381 |
XSUM_U64 const Dresult = XXH3_64bits(data, len); |
| 382 |
XSUM_checkResult64(Dresult, Nresult); |
| 383 |
} |
| 384 |
|
| 385 |
/* check that the combination of |
| 386 |
* XXH3_generateSecret_fromSeed() and XXH3_64bits_withSecretandSeed() |
| 387 |
* results in exactly the same hash generation as XXH3_64bits_withSeed() */ |
| 388 |
{ char secretBuffer[XXH3_SECRET_DEFAULT_SIZE+1]; |
| 389 |
char* const secret = secretBuffer + 1; /* intentional unalignment */ |
| 390 |
XXH3_generateSecret_fromSeed(secret, seed); |
| 391 |
{ XSUM_U64 const Dresult = XXH3_64bits_withSecretandSeed(data, len, secret, XXH3_SECRET_DEFAULT_SIZE, seed); |
| 392 |
XSUM_checkResult64(Dresult, Nresult); |
| 393 |
} |
| 394 |
|
| 395 |
/* check that XXH3_64bits_withSecretandSeed() |
| 396 |
* results in exactly the same return value as XXH3_64bits_withSeed() |
| 397 |
* when len <= XXH3_MIDSIZE_MAX, whatever the content of @secret */ |
| 398 |
memset(secret, 0x99, 9); |
| 399 |
if (len <= XXH3_MIDSIZE_MAX) { |
| 400 |
XSUM_U64 const Dresult = XXH3_64bits_withSecretandSeed(data, len, secret, XXH3_SECRET_DEFAULT_SIZE, seed); |
| 401 |
XSUM_checkResult64(Dresult, Nresult); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
/* streaming API test */ |
| 406 |
{ XXH3_state_t* const state = XXH3_createState(); |
| 407 |
assert(state != NULL); |
| 408 |
/* single ingestion */ |
| 409 |
(void)XXH3_64bits_reset_withSeed(state, seed); |
| 410 |
(void)XXH3_64bits_update(state, data, len); |
| 411 |
XSUM_checkResult64(XXH3_64bits_digest(state), Nresult); |
| 412 |
|
| 413 |
/* random ingestion */ |
| 414 |
(void)XXH3_64bits_reset_withSeed(state, seed); |
| 415 |
XSUM_XXH3_randomUpdate(state, data, len, &XXH3_64bits_update); |
| 416 |
XSUM_checkResult64(XXH3_64bits_digest(state), Nresult); |
| 417 |
|
| 418 |
/* byte by byte ingestion */ |
| 419 |
{ size_t pos; |
| 420 |
(void)XXH3_64bits_reset_withSeed(state, seed); |
| 421 |
for (pos=0; pos<len; pos++) |
| 422 |
(void)XXH3_64bits_update(state, ((const char*)data)+pos, 1); |
| 423 |
XSUM_checkResult64(XXH3_64bits_digest(state), Nresult); |
| 424 |
} |
| 425 |
|
| 426 |
/* check that streaming with a combination of |
| 427 |
* XXH3_generateSecret_fromSeed() and XXH3_64bits_reset_withSecretandSeed() |
| 428 |
* results in exactly the same hash generation as XXH3_64bits_reset_withSeed() */ |
| 429 |
{ char secretBuffer[XXH3_SECRET_DEFAULT_SIZE+1]; |
| 430 |
char* const secret = secretBuffer + 1; /* intentional unalignment */ |
| 431 |
XXH3_generateSecret_fromSeed(secret, seed); |
| 432 |
/* single ingestion */ |
| 433 |
(void)XXH3_64bits_reset_withSecretandSeed(state, secret, XXH3_SECRET_DEFAULT_SIZE, seed); |
| 434 |
(void)XXH3_64bits_update(state, data, len); |
| 435 |
XSUM_checkResult64(XXH3_64bits_digest(state), Nresult); |
| 436 |
|
| 437 |
/* check that XXH3_64bits_withSecretandSeed() |
| 438 |
* results in exactly the same return value as XXH3_64bits_withSeed() |
| 439 |
* when len <= XXH3_MIDSIZE_MAX, whatever the content of @secret */ |
| 440 |
if (len <= XXH3_MIDSIZE_MAX) { |
| 441 |
/* single ingestion */ |
| 442 |
memset(secret, 0x99, 9); |
| 443 |
(void)XXH3_64bits_reset_withSecretandSeed(state, secret, XXH3_SECRET_DEFAULT_SIZE, seed); |
| 444 |
(void)XXH3_64bits_update(state, data, len); |
| 445 |
XSUM_checkResult64(XXH3_64bits_digest(state), Nresult); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
XXH3_freeState(state); |
| 450 |
} |
| 451 |
|
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
static void XSUM_testXXH3_withSecret(const void* data, const void* secret, |
| 456 |
size_t secretSize, const XSUM_testdata64_t* testData) |
| 457 |
{ |
| 458 |
size_t len = (size_t)testData->len; |
| 459 |
XSUM_U64 Nresult = testData->Nresult; |
| 460 |
|
| 461 |
if (len == 0) { |
| 462 |
data = NULL; |
| 463 |
} else { |
| 464 |
assert(data != NULL); |
| 465 |
} |
| 466 |
{ XSUM_U64 const Dresult = XXH3_64bits_withSecret(data, len, secret, secretSize); |
| 467 |
XSUM_checkResult64(Dresult, Nresult); |
| 468 |
} |
| 469 |
|
| 470 |
/* check that XXH3_64bits_withSecretandSeed() |
| 471 |
* results in exactly the same return value as XXH3_64bits_withSecret() */ |
| 472 |
if (len > XXH3_MIDSIZE_MAX) |
| 473 |
{ XSUM_U64 const Dresult = XXH3_64bits_withSecretandSeed(data, len, secret, secretSize, 0); |
| 474 |
XSUM_checkResult64(Dresult, Nresult); |
| 475 |
} |
| 476 |
|
| 477 |
/* streaming API test */ |
| 478 |
{ XXH3_state_t *state = XXH3_createState(); |
| 479 |
assert(state != NULL); |
| 480 |
(void)XXH3_64bits_reset_withSecret(state, secret, secretSize); |
| 481 |
(void)XXH3_64bits_update(state, data, len); |
| 482 |
XSUM_checkResult64(XXH3_64bits_digest(state), Nresult); |
| 483 |
|
| 484 |
/* random ingestion */ |
| 485 |
(void)XXH3_64bits_reset_withSecret(state, secret, secretSize); |
| 486 |
XSUM_XXH3_randomUpdate(state, data, len, &XXH3_64bits_update); |
| 487 |
XSUM_checkResult64(XXH3_64bits_digest(state), Nresult); |
| 488 |
|
| 489 |
/* byte by byte ingestion */ |
| 490 |
{ size_t pos; |
| 491 |
(void)XXH3_64bits_reset_withSecret(state, secret, secretSize); |
| 492 |
for (pos=0; pos<len; pos++) |
| 493 |
(void)XXH3_64bits_update(state, ((const char*)data)+pos, 1); |
| 494 |
XSUM_checkResult64(XXH3_64bits_digest(state), Nresult); |
| 495 |
} |
| 496 |
|
| 497 |
/* check that XXH3_64bits_reset_withSecretandSeed() |
| 498 |
* results in exactly the same return value as XXH3_64bits_reset_withSecret() |
| 499 |
* when len > XXH3_MIDSIZE_MAX, whatever the value of @seed */ |
| 500 |
if (len > XXH3_MIDSIZE_MAX) { |
| 501 |
/* single ingestion */ |
| 502 |
(void)XXH3_64bits_reset_withSecretandSeed(state, secret, secretSize, 17); |
| 503 |
(void)XXH3_64bits_update(state, data, len); |
| 504 |
XSUM_checkResult64(XXH3_64bits_digest(state), Nresult); |
| 505 |
} |
| 506 |
|
| 507 |
XXH3_freeState(state); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
static void XSUM_testXXH128(const void* data, const XSUM_testdata128_t* testData) |
| 512 |
{ |
| 513 |
size_t len = (size_t)testData->len; |
| 514 |
XSUM_U64 seed = testData->seed; |
| 515 |
XXH128_hash_t const Nresult = testData->Nresult; |
| 516 |
if (len == 0) { |
| 517 |
data = NULL; |
| 518 |
} else { |
| 519 |
assert(data != NULL); |
| 520 |
} |
| 521 |
|
| 522 |
{ XXH128_hash_t const Dresult = XXH3_128bits_withSeed(data, len, seed); |
| 523 |
XSUM_checkResult128(Dresult, Nresult); |
| 524 |
} |
| 525 |
|
| 526 |
/* check that XXH128() is identical to XXH3_128bits_withSeed() */ |
| 527 |
{ XXH128_hash_t const Dresult2 = XXH128(data, len, seed); |
| 528 |
XSUM_checkResult128(Dresult2, Nresult); |
| 529 |
} |
| 530 |
|
| 531 |
/* check that the no-seed variant produces same result as seed==0 */ |
| 532 |
if (seed == 0) { |
| 533 |
XXH128_hash_t const Dresult = XXH3_128bits(data, len); |
| 534 |
XSUM_checkResult128(Dresult, Nresult); |
| 535 |
} |
| 536 |
|
| 537 |
/* check that the combination of |
| 538 |
* XXH3_generateSecret_fromSeed() and XXH3_128bits_withSecretandSeed() |
| 539 |
* results in exactly the same hash generation as XXH3_64bits_withSeed() */ |
| 540 |
{ char secretBuffer[XXH3_SECRET_DEFAULT_SIZE+1]; |
| 541 |
char* const secret = secretBuffer + 1; /* intentional unalignment */ |
| 542 |
XXH3_generateSecret_fromSeed(secret, seed); |
| 543 |
{ XXH128_hash_t const Dresult = XXH3_128bits_withSecretandSeed(data, len, secret, XXH3_SECRET_DEFAULT_SIZE, seed); |
| 544 |
XSUM_checkResult128(Dresult, Nresult); |
| 545 |
} |
| 546 |
|
| 547 |
/* check that XXH3_128bits_withSecretandSeed() |
| 548 |
* results in exactly the same return value as XXH3_128bits_withSeed() |
| 549 |
* if len <= XXH3_MIDSIZE_MAX, whatever the content of @secret */ |
| 550 |
memset(secret, 0x99, 9); |
| 551 |
if (len <= XXH3_MIDSIZE_MAX) { |
| 552 |
XXH128_hash_t const Dresult = XXH3_128bits_withSecretandSeed(data, len, secret, XXH3_SECRET_DEFAULT_SIZE, seed); |
| 553 |
XSUM_checkResult128(Dresult, Nresult); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
/* streaming API test */ |
| 558 |
{ XXH3_state_t *state = XXH3_createState(); |
| 559 |
assert(state != NULL); |
| 560 |
|
| 561 |
/* single ingestion */ |
| 562 |
(void)XXH3_128bits_reset_withSeed(state, seed); |
| 563 |
(void)XXH3_128bits_update(state, data, len); |
| 564 |
XSUM_checkResult128(XXH3_128bits_digest(state), Nresult); |
| 565 |
|
| 566 |
/* random ingestion */ |
| 567 |
(void)XXH3_128bits_reset_withSeed(state, seed); |
| 568 |
XSUM_XXH3_randomUpdate(state, data, len, &XXH3_128bits_update); |
| 569 |
XSUM_checkResult128(XXH3_128bits_digest(state), Nresult); |
| 570 |
|
| 571 |
/* byte by byte ingestion */ |
| 572 |
{ size_t pos; |
| 573 |
(void)XXH3_128bits_reset_withSeed(state, seed); |
| 574 |
for (pos=0; pos<len; pos++) |
| 575 |
(void)XXH3_128bits_update(state, ((const char*)data)+pos, 1); |
| 576 |
XSUM_checkResult128(XXH3_128bits_digest(state), Nresult); |
| 577 |
} |
| 578 |
|
| 579 |
/* check that streaming with a combination of |
| 580 |
* XXH3_generateSecret_fromSeed() and XXH3_128bits_reset_withSecretandSeed() |
| 581 |
* results in exactly the same hash generation as XXH3_128bits_reset_withSeed() */ |
| 582 |
{ char secretBuffer[XXH3_SECRET_DEFAULT_SIZE+1]; |
| 583 |
char* const secret = secretBuffer + 1; /* intentional unalignment */ |
| 584 |
XXH3_generateSecret_fromSeed(secret, seed); |
| 585 |
/* single ingestion */ |
| 586 |
(void)XXH3_128bits_reset_withSecretandSeed(state, secret, XXH3_SECRET_DEFAULT_SIZE, seed); |
| 587 |
(void)XXH3_128bits_update(state, data, len); |
| 588 |
XSUM_checkResult128(XXH3_128bits_digest(state), Nresult); |
| 589 |
|
| 590 |
/* check that XXH3_128bits_reset_withSecretandSeed() |
| 591 |
* results in exactly the same return value as XXH3_128bits_reset_withSeed() |
| 592 |
* if len <= XXH3_MIDSIZE_MAX, whatever the content of @secret */ |
| 593 |
if (len <= XXH3_MIDSIZE_MAX) { |
| 594 |
/* single ingestion */ |
| 595 |
memset(secret, 0x99, 9); |
| 596 |
(void)XXH3_128bits_reset_withSecretandSeed(state, secret, XXH3_SECRET_DEFAULT_SIZE, seed); |
| 597 |
(void)XXH3_128bits_update(state, data, len); |
| 598 |
XSUM_checkResult128(XXH3_128bits_digest(state), Nresult); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
XXH3_freeState(state); |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
static void XSUM_testXXH128_withSecret(const void* data, const void* secret, size_t secretSize, const XSUM_testdata128_t* testData) |
| 607 |
{ |
| 608 |
size_t len = testData->len; |
| 609 |
XXH128_hash_t Nresult = testData->Nresult; |
| 610 |
if (len == 0) { |
| 611 |
data = NULL; |
| 612 |
} else { |
| 613 |
assert(data != NULL); |
| 614 |
} |
| 615 |
{ XXH128_hash_t const Dresult = XXH3_128bits_withSecret(data, len, secret, secretSize); |
| 616 |
XSUM_checkResult128(Dresult, Nresult); |
| 617 |
} |
| 618 |
|
| 619 |
/* check that XXH3_128bits_withSecretandSeed() |
| 620 |
* results in exactly the same return value as XXH3_128bits_withSecret() */ |
| 621 |
if (len > XXH3_MIDSIZE_MAX) |
| 622 |
{ XXH128_hash_t const Dresult = XXH3_128bits_withSecretandSeed(data, len, secret, secretSize, 0); |
| 623 |
XSUM_checkResult128(Dresult, Nresult); |
| 624 |
} |
| 625 |
|
| 626 |
/* streaming API test */ |
| 627 |
{ XXH3_state_t* const state = XXH3_createState(); |
| 628 |
assert(state != NULL); |
| 629 |
(void)XXH3_128bits_reset_withSecret(state, secret, secretSize); |
| 630 |
(void)XXH3_128bits_update(state, data, len); |
| 631 |
XSUM_checkResult128(XXH3_128bits_digest(state), Nresult); |
| 632 |
|
| 633 |
/* random ingestion */ |
| 634 |
(void)XXH3_128bits_reset_withSecret(state, secret, secretSize); |
| 635 |
XSUM_XXH3_randomUpdate(state, data, len, &XXH3_128bits_update); |
| 636 |
XSUM_checkResult128(XXH3_128bits_digest(state), Nresult); |
| 637 |
|
| 638 |
/* byte by byte ingestion */ |
| 639 |
{ size_t pos; |
| 640 |
(void)XXH3_128bits_reset_withSecret(state, secret, secretSize); |
| 641 |
for (pos=0; pos<len; pos++) |
| 642 |
(void)XXH3_128bits_update(state, ((const char*)data)+pos, 1); |
| 643 |
XSUM_checkResult128(XXH3_128bits_digest(state), Nresult); |
| 644 |
} |
| 645 |
|
| 646 |
/* check that XXH3_128bits_reset_withSecretandSeed() |
| 647 |
* results in exactly the same return value as XXH3_128bits_reset_withSecret() */ |
| 648 |
if (len > XXH3_MIDSIZE_MAX) { |
| 649 |
/* single ingestion */ |
| 650 |
(void)XXH3_128bits_reset_withSecretandSeed(state, secret, secretSize, 0); |
| 651 |
(void)XXH3_128bits_update(state, data, len); |
| 652 |
XSUM_checkResult128(XXH3_128bits_digest(state), Nresult); |
| 653 |
} |
| 654 |
|
| 655 |
XXH3_freeState(state); |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
static void XSUM_testSecretGenerator(const void* customSeed, const XSUM_testdata_sample_t* testData) |
| 660 |
{ |
| 661 |
static int nbTests = 1; |
| 662 |
const int sampleIndex[SECRET_SAMPLE_NBBYTES] = { 0, 62, 131, 191, 241 }; /* position of sampled bytes */ |
| 663 |
XSUM_U8 secretBuffer[SECRET_SIZE_MAX] = {0}; |
| 664 |
XSUM_U8 samples[SECRET_SAMPLE_NBBYTES]; |
| 665 |
int i; |
| 666 |
|
| 667 |
assert(testData->secretLen <= SECRET_SIZE_MAX); |
| 668 |
XXH3_generateSecret(secretBuffer, testData->secretLen, customSeed, testData->seedLen); |
| 669 |
for (i=0; i<SECRET_SAMPLE_NBBYTES; i++) { |
| 670 |
samples[i] = secretBuffer[sampleIndex[i]]; |
| 671 |
} |
| 672 |
if (memcmp(samples, testData->byte, sizeof(testData->byte))) { |
| 673 |
XSUM_log("\rError: Secret generation test %i: Internal sanity check failed. \n", nbTests); |
| 674 |
XSUM_log("\rGot { 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X }, expected { 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X } \n", |
| 675 |
samples[0], samples[1], samples[2], samples[3], samples[4], |
| 676 |
testData->byte[0], testData->byte[1], testData->byte[2], testData->byte[3], testData->byte[4] ); |
| 677 |
exit(1); |
| 678 |
} |
| 679 |
nbTests++; |
| 680 |
} |
| 681 |
|
| 682 |
/*! |
| 683 |
* XSUM_sanityCheck(): |
| 684 |
* Runs a sanity check before the benchmark. |
| 685 |
* |
| 686 |
* Exits on an incorrect output. |
| 687 |
*/ |
| 688 |
XSUM_API void XSUM_sanityCheck(void) |
| 689 |
{ |
| 690 |
size_t i; |
| 691 |
#define SANITY_BUFFER_SIZE 2367 |
| 692 |
XSUM_U8 sanityBuffer[SANITY_BUFFER_SIZE]; |
| 693 |
const void* const secret = sanityBuffer + 7; |
| 694 |
const size_t secretSize = XXH3_SECRET_SIZE_MIN + 11; |
| 695 |
assert(sizeof(sanityBuffer) >= 7 + secretSize); |
| 696 |
|
| 697 |
XSUM_fillTestBuffer(sanityBuffer, sizeof(sanityBuffer)); |
| 698 |
|
| 699 |
/* XXH32 */ |
| 700 |
for (i = 0; i < (sizeof(XSUM_XXH32_testdata)/sizeof(XSUM_XXH32_testdata[0])); i++) { |
| 701 |
XSUM_testXXH32(sanityBuffer, &XSUM_XXH32_testdata[i]); |
| 702 |
} |
| 703 |
/* XXH64 */ |
| 704 |
for (i = 0; i < (sizeof(XSUM_XXH64_testdata)/sizeof(XSUM_XXH64_testdata[0])); i++) { |
| 705 |
XSUM_testXXH64(sanityBuffer, &XSUM_XXH64_testdata[i]); |
| 706 |
} |
| 707 |
/* XXH3_64bits, seeded */ |
| 708 |
for (i = 0; i < (sizeof(XSUM_XXH3_testdata)/sizeof(XSUM_XXH3_testdata[0])); i++) { |
| 709 |
XSUM_testXXH3(sanityBuffer, &XSUM_XXH3_testdata[i]); |
| 710 |
} |
| 711 |
/* XXH3_64bits, custom secret */ |
| 712 |
for (i = 0; i < (sizeof(XSUM_XXH3_withSecret_testdata)/sizeof(XSUM_XXH3_withSecret_testdata[0])); i++) { |
| 713 |
XSUM_testXXH3_withSecret(sanityBuffer, secret, secretSize, &XSUM_XXH3_withSecret_testdata[i]); |
| 714 |
} |
| 715 |
/* XXH128 */ |
| 716 |
for (i = 0; i < (sizeof(XSUM_XXH128_testdata)/sizeof(XSUM_XXH128_testdata[0])); i++) { |
| 717 |
XSUM_testXXH128(sanityBuffer, &XSUM_XXH128_testdata[i]); |
| 718 |
} |
| 719 |
/* XXH128 with custom Secret */ |
| 720 |
for (i = 0; i < (sizeof(XSUM_XXH128_withSecret_testdata)/sizeof(XSUM_XXH128_withSecret_testdata[0])); i++) { |
| 721 |
XSUM_testXXH128_withSecret(sanityBuffer, secret, secretSize, &XSUM_XXH128_withSecret_testdata[i]); |
| 722 |
} |
| 723 |
/* secret generator */ |
| 724 |
for (i = 0; i < (sizeof(XSUM_XXH3_generateSecret_testdata)/sizeof(XSUM_XXH3_generateSecret_testdata[0])); i++) { |
| 725 |
assert(XSUM_XXH3_generateSecret_testdata[i].seedLen <= SANITY_BUFFER_SIZE); |
| 726 |
XSUM_testSecretGenerator(sanityBuffer, &XSUM_XXH3_generateSecret_testdata[i]); |
| 727 |
} |
| 728 |
|
| 729 |
XSUM_logVerbose(3, "\r%70s\r", ""); /* Clean display line */ |
| 730 |
XSUM_logVerbose(3, "Sanity check -- all tests ok\n"); |
| 731 |
} |
| 732 |
|
| 733 |
#endif /* !XSUM_NO_TESTS */ |