forked from rurban/smhasher
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update prvhash to 2.5 (GH rurban#133)
removed the generic 64bit test, it was the same as _64. opt is now only for _32, the rest is generic. WIP speeds
- Loading branch information
Showing
10 changed files
with
1,604 additions
and
3,191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,119 @@ | ||
/** | ||
* @file prvhash42.h | ||
* | ||
* @brief The inclusion file for the "prvhash42" hash function. | ||
* | ||
* @mainpage | ||
* | ||
* @section intro_sec Introduction | ||
* | ||
* Description is available at https://github.com/avaneev/prvhash | ||
* | ||
* @section license License | ||
* | ||
* Copyright (c) 2020 Aleksey Vaneev | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a | ||
* copy of this software and associated documentation files (the "Software"), | ||
* to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the | ||
* Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
* DEALINGS IN THE SOFTWARE. | ||
* | ||
* @version 2.0 | ||
*/ | ||
|
||
//$ nocpp | ||
|
||
#ifndef PRVHASH42_INCLUDED | ||
#define PRVHASH42_INCLUDED | ||
|
||
#include <stdint.h> | ||
#include <string.h> | ||
|
||
/** | ||
* PRVHASH hash function (64-bit with 32-bit hash word). Produces hash of the | ||
* specified Message. | ||
* | ||
* @param Message Message to produce hash from. | ||
* @param MessageLen Message length, in bytes. | ||
* @param[out] Hash The resulting hash. | ||
* @param HashLen The required hash length, in bytes, should be >= 4, in | ||
* increments of 4. | ||
* @param SeedXOR Optional value, to XOR the default seed with. To use the | ||
* default seed, set to 0. | ||
* @param InitLCG For development purposes, should be set to 0. If != 0, "lcg" | ||
* value to use. | ||
* @param InitSeed For development purposes, should be set to 0. If != 0, | ||
* "Seed" value to use. | ||
*/ | ||
|
||
inline void prvhash42( const uint8_t* const Message, const int MessageLen, | ||
uint8_t* const Hash, const int HashLen, const uint64_t SeedXOR, | ||
const uint64_t InitLCG, const uint64_t InitSeed ) | ||
{ | ||
// Initialize the hash. | ||
|
||
memset( Hash, 0, HashLen ); | ||
|
||
uint64_t lcg = ( InitLCG == 0 ? 15267459991392010589ULL : InitLCG ); | ||
// Multiplier inspired by LCG. This is not a prime number. It is a | ||
// random sequence of bits. This value can be regenerated at will, | ||
// possibly using various statistical search methods. The best | ||
// strategies: 1) Compose both this and seed numbers of 8-bit values | ||
// that have 4 random bits set; 2) Compose the 64-bit value that has | ||
// 32 random bits set; same for seed. An important consideration here | ||
// is to pass the 16-bit Sparse test. | ||
|
||
uint64_t Seed = ( InitSeed == 0 ? 7928988912013905173ULL : InitSeed ); | ||
// Generated similarly to "lcg". | ||
|
||
Seed ^= SeedXOR; | ||
int k; | ||
|
||
for( k = 0; k < MessageLen; k++ ) | ||
{ | ||
const uint64_t m = Message[ k ]; | ||
|
||
Seed ^= m; | ||
int i; | ||
|
||
for( i = 0; i < HashLen; i += 4 ) | ||
{ | ||
Seed *= lcg; | ||
uint32_t* const hc = (uint32_t*) &Hash[ i ]; | ||
const uint64_t ph = *hc; | ||
*hc ^= (uint32_t) ( Seed >> 32 ); | ||
Seed ^= ph ^ m; | ||
} | ||
|
||
lcg += Seed; | ||
} | ||
} | ||
|
||
#endif // PRVHASH42_INCLUDED | ||
/** | ||
* @file prvhash42.h | ||
* | ||
* @brief The inclusion file for the "prvhash42" hash function. | ||
* | ||
* @mainpage | ||
* | ||
* @section intro_sec Introduction | ||
* | ||
* Description is available at https://github.com/avaneev/prvhash | ||
* | ||
* @section license License | ||
* | ||
* Copyright (c) 2020 Aleksey Vaneev | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a | ||
* copy of this software and associated documentation files (the "Software"), | ||
* to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the | ||
* Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
* DEALINGS IN THE SOFTWARE. | ||
* | ||
* @version 2.5 | ||
*/ | ||
|
||
//$ nocpp | ||
|
||
#ifndef PRVHASH42_INCLUDED | ||
#define PRVHASH42_INCLUDED | ||
|
||
#include <stdint.h> | ||
#include <string.h> | ||
|
||
/** | ||
* PRVHASH hash function (64-bit variables with 32-bit hash word). Produces | ||
* hash of the specified Message. | ||
* | ||
* @param Message Message to produce hash from. | ||
* @param MessageLen Message length, in bytes. | ||
* @param[out] Hash The resulting hash. If both InitLCG and InitSeed are | ||
* non-zero, the hash will not be initially reset to 0, otherwise the hash | ||
* should be pre-initialized with random bytes. | ||
* @param HashLen The required hash length, in bytes, should be >= 4, in | ||
* increments of 4. | ||
* @param SeedXOR Optional value, to XOR the default seed with. To use the | ||
* default seed, set to 0. If both InitLCG and InitSeed are non-zero, this | ||
* SeedXOR is ignored and should be set to 0. Otherwise, the SeedXOR value | ||
* can have any bit length, and is used only as an additional entropy source. | ||
* @param InitLCG If both InitLCG and InitSeed are non-zero, both values | ||
* will be used as initial state of the hash function. Full 64-bit random | ||
* value should be supplied in this case. See the considerations below. | ||
* @param InitSeed If both InitLCG and InitSeed are non-zero, both values | ||
* will be used as initial state of the hash function. Full 64-bit random | ||
* value should be supplied in this case. | ||
*/ | ||
|
||
inline void prvhash42( const uint8_t* const Message, const int MessageLen, | ||
uint8_t* const Hash, const int HashLen, const uint64_t SeedXOR, | ||
const uint64_t InitLCG, const uint64_t InitSeed ) | ||
{ | ||
uint64_t lcg; // Multiplier inspired by LCG. This is not a prime number. | ||
// It is a random sequence of bits. This value can be regenerated at | ||
// will, possibly using various statistical search methods. The best | ||
// strategies: 1) Compose this number from 16-bit random values that | ||
// have 6 to 10 random bits set; 2) Use a 64-bit random value that has | ||
// 30-34 random bits set. An important consideration here is to pass | ||
// the 16-bit Sparse test by default. | ||
uint64_t Seed; // Generated similarly to "lcg". | ||
|
||
if( InitLCG == 0 && InitSeed == 0 ) | ||
{ | ||
lcg = 15267459991392010589ULL; | ||
Seed = 7928988912013905173ULL ^ SeedXOR; | ||
memset( Hash, 0, HashLen ); | ||
} | ||
else | ||
{ | ||
lcg = InitLCG; | ||
Seed = InitSeed; | ||
} | ||
|
||
const int hl4 = ( HashLen >> 2 ); | ||
const uint64_t lmsg = ( MessageLen == 0 ? 0 : ~Message[ MessageLen - 1 ]); | ||
int c = MessageLen + hl4 + hl4 - MessageLen % hl4; | ||
int hpos = 0; | ||
int k; | ||
|
||
for( k = 0; k < c; k++ ) | ||
{ | ||
const uint64_t msg = ( k < MessageLen ? Message[ k ] : lmsg ); | ||
|
||
Seed *= lcg; | ||
uint32_t* const hc = (uint32_t*) &Hash[ hpos ]; | ||
const uint64_t ph = *hc; | ||
*hc ^= (uint32_t) ( Seed >> 32 ); | ||
Seed ^= ph ^ msg; | ||
lcg += Seed; | ||
|
||
hpos += 4; | ||
|
||
if( hpos == HashLen ) | ||
{ | ||
hpos = 0; | ||
} | ||
} | ||
} | ||
|
||
#endif // PRVHASH42_INCLUDED |
Oops, something went wrong.