Skip to content

Commit

Permalink
Merge pull request #894 from hltj/dev
Browse files Browse the repository at this point in the history
Fix a typo that cause inconsistent hash between streaming and stateless way for XXH3 128-bit variant with custom secret and seed 0
  • Loading branch information
Cyan4973 authored May 7, 2024
2 parents 5930eb4 + fcde207 commit a9b2f18
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
64 changes: 52 additions & 12 deletions tests/sanity_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ static void SANITY_TEST_XXH3_randomUpdate(
/**/
static void testXXH3(
const void* data,
const void* secret,
size_t secretSize,
const XSUM_testdata64_t* testData,
XSUM_U64* pRandSeed,
const char* testName,
Expand Down Expand Up @@ -346,12 +348,19 @@ static void testXXH3(
* XXH3_generateSecret_fromSeed() and XXH3_64bits_withSecretandSeed()
* results in exactly the same hash generation as XXH3_64bits_withSeed() */
{ char secretBuffer[XXH3_SECRET_DEFAULT_SIZE+1];
char* const secret = secretBuffer + 1; /* intentional unalignment */
XXH3_generateSecret_fromSeed(secret, seed);
{ XSUM_U64 const Dresult = XXH3_64bits_withSecretandSeed(data, len, secret, XXH3_SECRET_DEFAULT_SIZE, seed);
char* const secretFromSeed = secretBuffer + 1; /* intentional unalignment */
XXH3_generateSecret_fromSeed(secretFromSeed, seed);
{ XSUM_U64 const Dresult = XXH3_64bits_withSecretandSeed(data, len, secretFromSeed, XXH3_SECRET_DEFAULT_SIZE, seed);
checkResult64(Dresult, Nresult, testName, testNb, __LINE__);
} }

/* check that XXH3_64bits_withSecretandSeed()
* results in exactly the same return value as XXH3_64bits_withSeed() */
if (len <= XXH3_MIDSIZE_MAX) {
XSUM_U64 const Dresult = XXH3_64bits_withSecretandSeed(data, len, secret, secretSize, seed);
checkResult64(Dresult, Nresult, testName, testNb, __LINE__);
}

/* streaming API test */
{ XXH3_state_t* const state = XXH3_createState();
assert(state != NULL);
Expand All @@ -377,10 +386,19 @@ static void testXXH3(
* XXH3_generateSecret_fromSeed() and XXH3_64bits_reset_withSecretandSeed()
* results in exactly the same hash generation as XXH3_64bits_reset_withSeed() */
{ char secretBuffer[XXH3_SECRET_DEFAULT_SIZE+1];
char* const secret = secretBuffer + 1; /* intentional unalignment */
XXH3_generateSecret_fromSeed(secret, seed);
char* const secretFromSeed = secretBuffer + 1; /* intentional unalignment */
XXH3_generateSecret_fromSeed(secretFromSeed, seed);
/* single ingestion */
(void)XXH3_64bits_reset_withSecretandSeed(state, secretFromSeed, XXH3_SECRET_DEFAULT_SIZE, seed);
(void)XXH3_64bits_update(state, data, len);
checkResult64(XXH3_64bits_digest(state), Nresult, testName, testNb, __LINE__);
}

/* check that XXH3_64bits_withSecretandSeed()
* results in exactly the same return value as XXH3_64bits_withSeed() */
if (len <= XXH3_MIDSIZE_MAX) {
/* single ingestion */
(void)XXH3_64bits_reset_withSecretandSeed(state, secret, XXH3_SECRET_DEFAULT_SIZE, seed);
(void)XXH3_64bits_reset_withSecretandSeed(state, secret, secretSize, seed);
(void)XXH3_64bits_update(state, data, len);
checkResult64(XXH3_64bits_digest(state), Nresult, testName, testNb, __LINE__);
}
Expand Down Expand Up @@ -459,6 +477,8 @@ static void testXXH3_withSecret(
/**/
static void testXXH128(
const void* data,
const void* secret,
size_t secretSize,
const XSUM_testdata128_t* testData,
XSUM_U64* pRandSeed,
const char* testName,
Expand Down Expand Up @@ -493,12 +513,19 @@ static void testXXH128(
* XXH3_generateSecret_fromSeed() and XXH3_128bits_withSecretandSeed()
* results in exactly the same hash generation as XXH3_64bits_withSeed() */
{ char secretBuffer[XXH3_SECRET_DEFAULT_SIZE+1];
char* const secret = secretBuffer + 1; /* intentional unalignment */
XXH3_generateSecret_fromSeed(secret, seed);
{ XXH128_hash_t const Dresult = XXH3_128bits_withSecretandSeed(data, len, secret, XXH3_SECRET_DEFAULT_SIZE, seed);
char* const secretFromSeed = secretBuffer + 1; /* intentional unalignment */
XXH3_generateSecret_fromSeed(secretFromSeed, seed);
{ XXH128_hash_t const Dresult = XXH3_128bits_withSecretandSeed(data, len, secretFromSeed, XXH3_SECRET_DEFAULT_SIZE, seed);
checkResult128(Dresult, Nresult, testName, testNb, __LINE__);
} }

/* check that XXH3_128bits_withSecretandSeed()
* results in exactly the same return value as XXH3_128bits_withSeed() */
if (len <= XXH3_MIDSIZE_MAX) {
XXH128_hash_t const Dresult = XXH3_128bits_withSecretandSeed(data, len, secret, secretSize, seed);
checkResult128(Dresult, Nresult, testName, testNb, __LINE__);
}

/* streaming API test */
{ XXH3_state_t * const state = XXH3_createState();
assert(state != NULL);
Expand All @@ -525,10 +552,19 @@ static void testXXH128(
* XXH3_generateSecret_fromSeed() and XXH3_128bits_reset_withSecretandSeed()
* results in exactly the same hash generation as XXH3_128bits_reset_withSeed() */
{ char secretBuffer[XXH3_SECRET_DEFAULT_SIZE+1];
char* const secret = secretBuffer + 1; /* intentional unalignment */
XXH3_generateSecret_fromSeed(secret, seed);
char* const secretFromSeed = secretBuffer + 1; /* intentional unalignment */
XXH3_generateSecret_fromSeed(secretFromSeed, seed);
/* single ingestion */
(void)XXH3_128bits_reset_withSecretandSeed(state, secret, XXH3_SECRET_DEFAULT_SIZE, seed);
(void)XXH3_128bits_reset_withSecretandSeed(state, secretFromSeed, XXH3_SECRET_DEFAULT_SIZE, seed);
(void)XXH3_128bits_update(state, data, len);
checkResult128(XXH3_128bits_digest(state), Nresult, testName, testNb, __LINE__);
}

/* check that XXH3_128bits_reset_withSecretandSeed()
* results in exactly the same return value as XXH3_128bits_reset_withSeed() */
if (len <= XXH3_MIDSIZE_MAX) {
/* single ingestion */
(void)XXH3_128bits_reset_withSecretandSeed(state, secret, secretSize, seed);
(void)XXH3_128bits_update(state, data, len);
checkResult128(XXH3_128bits_digest(state), Nresult, testName, testNb, __LINE__);
}
Expand Down Expand Up @@ -676,6 +712,8 @@ int main(int argc, const char* argv[])
for (i = 0; i < n; ++i, ++testCount) {
testXXH3(
sanityBuffer,
secret,
secretSize,
&XSUM_XXH3_testdata[i],
&randSeed,
"XSUM_XXH3_testdata",
Expand Down Expand Up @@ -713,6 +751,8 @@ int main(int argc, const char* argv[])
for (i = 0; i < n; ++i, ++testCount) {
testXXH128(
sanityBuffer,
secret,
secretSize,
&XSUM_XXH128_testdata[i],
&randSeed,
"XSUM_XXH128_testdata",
Expand Down
2 changes: 1 addition & 1 deletion xxhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -6926,7 +6926,7 @@ XXH_PUBLIC_API XXH128_hash_t XXH3_128bits_digest (XXH_NOESCAPE const XXH3_state_
}
}
/* len <= XXH3_MIDSIZE_MAX : short code */
if (state->seed)
if (state->useSeed)
return XXH3_128bits_withSeed(state->buffer, (size_t)state->totalLen, state->seed);
return XXH3_128bits_withSecret(state->buffer, (size_t)(state->totalLen),
secret, state->secretLimit + XXH_STRIPE_LEN);
Expand Down

0 comments on commit a9b2f18

Please sign in to comment.