From a80064036c33096ed1b6d5eff1e54027a767b480 Mon Sep 17 00:00:00 2001 From: hltj Date: Sun, 5 Nov 2023 00:45:59 +0800 Subject: [PATCH 1/2] add test cases for that `_withSecretandSeed` is the same as `_withSeed()` when `len <= XXH3_MIDSIZE_MAX` --- tests/sanity_test.c | 64 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/tests/sanity_test.c b/tests/sanity_test.c index 4c59bf3a..3e3d4f16 100644 --- a/tests/sanity_test.c +++ b/tests/sanity_test.c @@ -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, @@ -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); @@ -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__); } @@ -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, @@ -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); @@ -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__); } @@ -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", @@ -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", From fcde20738b45393b2b2ccc5bd18962012dae9a28 Mon Sep 17 00:00:00 2001 From: hltj Date: Sun, 5 Nov 2023 00:46:49 +0800 Subject: [PATCH 2/2] fix a typo --- xxhash.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xxhash.h b/xxhash.h index d11f0f63..744caca2 100644 --- a/xxhash.h +++ b/xxhash.h @@ -6897,7 +6897,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);