Skip to content

Commit

Permalink
Update prvhash to 2.5 (GH rurban#133)
Browse files Browse the repository at this point in the history
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
rurban committed Aug 14, 2020
1 parent 073358b commit d768ea3
Show file tree
Hide file tree
Showing 10 changed files with 1,604 additions and 3,191 deletions.
13 changes: 4 additions & 9 deletions Hashes.h
Original file line number Diff line number Diff line change
Expand Up @@ -990,29 +990,24 @@ inline void aesnihash_test ( const void * key, int len, unsigned seed, void * ou
}
#endif

#include "prvhash42.h"
inline void prvhash_test ( const void * key, int len, unsigned seed, void * out )
{
uint8_t hash[8] = {0};
prvhash42 ((const uint8_t *)key, len, hash, 8, (uint64_t)seed, 0, 0);
memcpy (out, hash, 8);
}
// https://github.com/avaneev/prvhash
#include "prvhash42opt.h"
inline void prvhash42_32test ( const void * key, int len, unsigned seed, void * out )
{
uint8_t hash[4] = {0};
prvhash42_32 ((const uint8_t *)key, len, hash, (uint64_t)seed);
memcpy (out, hash, 4);
}
#include "prvhash42.h"
inline void prvhash42_64test ( const void * key, int len, unsigned seed, void * out )
{
uint8_t hash[8] = {0};
prvhash42_64 ((const uint8_t *)key, len, hash, (uint64_t)seed);
prvhash42 ((const uint8_t *)key, len, hash, 8, (uint64_t)seed, 0, 0);
memcpy (out, hash, 8);
}
inline void prvhash42_128test ( const void * key, int len, unsigned seed, void * out )
{
uint8_t hash[16] = {0};
prvhash42_128 ((const uint8_t *)key, len, hash, (uint64_t)seed);
prvhash42 ((const uint8_t *)key, len, hash, 16, (uint64_t)seed, 0, 0);
memcpy (out, hash, 16);
}
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ SMhasher
| -------------------------------------- | | | | | |
| [tifuhash_64](doc/tifuhash_64.txt) | 35.60 | 1679.52 |1212.75 (15)| 276 | |
| [floppsyhash_64](doc/floppsyhash.txt) | 191.95 | 450.93 | 821.11 (152)| 623 | |
| [prvhash](doc/prvhash.txt) | 302.97 | 180.76 | 221.50 (2) | 157 | |
| [prvhash42_32](doc/prvhash42_32.txt) | 475.30 | 108.44 | 205.53 (1) | 157 | |
| [prvhash42_64](doc/prvhash42_64.txt) | 255.64 | 193.90 | 220.84 (4) | 157 | |
| [prvhash42_128](doc/prvhash42_128.txt) | 141.42 | 334.96 | 301.14 (3) | 157 | |
| [prvhash42_32](doc/prvhash42_32.txt) | 604.80 | 89.19 | 483.92 (15)| 157 | |
| [prvhash42_64](doc/prvhash42_64.txt) | 596.86 | 114.96 | 525.22 (1) | 157 | |
| [prvhash42_128](doc/prvhash42_128.txt) | 582.64 | 119.15 | 538.29 (4) | 157 | |
| [chaskey](doc/chaskey.txt) | 735.85 | 168.05 | 336.07 (4) |1609 | |
| [SipHash](doc/SipHash.txt) | 958.78 | 141.84 | 278.15 (3) |1071 | |
| [HalfSipHash](doc/HalfSipHash.txt) | 741.59 | 122.25 | 256.22 (20)| 700 | zeroes |
Expand Down
1,479 changes: 0 additions & 1,479 deletions doc/prvhash.txt

This file was deleted.

1,044 changes: 522 additions & 522 deletions doc/prvhash42_128.txt

Large diffs are not rendered by default.

600 changes: 300 additions & 300 deletions doc/prvhash42_32.txt

Large diffs are not rendered by default.

1,108 changes: 554 additions & 554 deletions doc/prvhash42_64.txt

Large diffs are not rendered by default.

25 changes: 9 additions & 16 deletions doc/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -869,31 +869,24 @@ <h1 id="smhasher">SMhasher</h1>
<td align="left"></td>
</tr>
<tr class="good">
<td align="left"><a href="prvhash.txt">prvhash</a></td>
<td align="right">302.97</td>
<td align="right">180.76</td>
<td align="right">221.50 (2) </td>
<td align="right">157</td>
<td align="left"></td>
<tr class="good">
<td align="left"><a href="prvhash42_32.txt">prvhash42_32</a></td>
<td align="right">475.30</td>
<td align="right">108.44</td>
<td align="right">205.53 (1) </td>
<td align="right">604.80</td>
<td align="right">89.19</td>
<td align="right">483.92 (15)</td>
<td align="right">157</td>
<td align="left"></td>
<tr class="good">
<td align="left"><a href="prvhash42_64.txt">prvhash42_64</a></td>
<td align="right">255.64</td>
<td align="right">193.90</td>
<td align="right">220.84 (4) </td>
<td align="right">596.86</td>
<td align="right">114.96</td>
<td align="right">525.22 (1) </td>
<td align="right">157</td>
<td align="left"></td>
<tr class="good">
<td align="left"><a href="prvhash42_128.txt">prvhash42_128</a></td>
<td align="right">141.42</td>
<td align="right">334.96</td>
<td align="right">301.14 (3) </td>
<td align="right">582.64</td>
<td align="right">119.15</td>
<td align="right">538.29 (4) </td>
<td align="right">157</td>
<td align="left"></td>
</tr>
Expand Down
7 changes: 3 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,9 @@ HashInfo g_hashes[] =
{ floppsyhash_64, 64, 0x0, "floppsyhash", "slow hash designed for floating point hardware", GOOD },
{ chaskey_test, 64, 0x81A90131, "chaskey", "mouha.be/chaskey/ with added seed support", GOOD },
#ifdef HAVE_INT64
{ prvhash_test, 64, 0x84468B63, "prvhash", "Generic prvhash", GOOD },
{ prvhash42_32test, 32, 0xD93CEC64, "prvhash42_32", "prvhash42 32bit", GOOD },
{ prvhash42_64test, 64, 0x84468B63, "prvhash42_64", "prvhash42 64bit", GOOD },
{ prvhash42_128test, 128, 0x119602B0, "prvhash42_128","prvhash42 128bit", GOOD },
{ prvhash42_32test, 32, 0x6588C6C3, "prvhash42_32", "prvhash42opt 32bit", GOOD },
{ prvhash42_64test, 64, 0xFDFEDA1E, "prvhash42_64", "prvhash42 64bit", GOOD },
{ prvhash42_128test, 128, 0xFB4CE766, "prvhash42_128","prvhash42 128bit", GOOD },
#endif
{ siphash_test, 64, 0xC58D7F9C, "SipHash", "SipHash 2-4 - SSSE3 optimized", GOOD },
{ halfsiphash_test, 32, 0xA7A05F72, "HalfSipHash", "HalfSipHash 2-4, 32bit", GOOD },
Expand Down
224 changes: 119 additions & 105 deletions prvhash42.h
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
Loading

0 comments on commit d768ea3

Please sign in to comment.