Skip to content

Commit

Permalink
Appear to have fixed rurban#191
Browse files Browse the repository at this point in the history
- Change FORCE_INLINE to static inline
- Make windows happy? Blargh...need to use dynamic memory instead of VLA...
- Correct verif value
- Delete unused
- Add line for 256 for info
- Name: DISCoHAsH
  • Loading branch information
o0101 committed Jun 24, 2023
1 parent 58f7726 commit 250b1b2
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 116 deletions.
2 changes: 1 addition & 1 deletion Hashes.h
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ inline void blake3_64 ( const void * key, int len, unsigned seed, void * out )
// objsize: 452520-45358b: 4203
#include "beamsplitter.h"

// objsize: 452010-45251e: 1294 (BEBB4185)
// objsize: 452010-45251e: 1294 (DISCoHAsH)
#include "discohash.h"

#if defined(HAVE_SSE2) && defined(HAVE_AESNI) && !defined(_MSC_VER)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ SMhasher
| [tifuhash_64](doc/tifuhash_64.txt) | 35.60 | 1679.52 |1212.75 (15)| 276 | Cyclic low32 |
| [floppsyhash](doc/floppsyhash.txt) | 35.72 | 1868.92 |1411.07 (7) | 623 | |
| [beamsplitter](doc/beamsplitter.txt) | 789.22 | 682.45 |1150.33 (26)|4203 | UB |
| [BEBB4185](doc/BEBB4185.txt) | 3959.08 | 199.92 | 413.43 (6) |1294 | UB, msvc-specific |
| [DISCoHAsH](doc/DISCoHAsH.txt) | 3959.08 | 199.92 | 413.43 (6) |1294 | UB, msvc-specific |
| [chaskey](doc/chaskey.txt) | 1150.69 | 113.09 | 308.01 (5) |1609 | PerlinNoise |
| [SipHash](doc/SipHash.txt) | 945.64 | 146.95 | 346.53 (5) |1071 | |
| [HalfSipHash](doc/HalfSipHash.txt) | 1108.77 | 82.02 | 285.78 (5) | 700 | zeroes |
Expand Down
107 changes: 37 additions & 70 deletions discohash.cpp
Original file line number Diff line number Diff line change
@@ -1,92 +1,58 @@
// also known as BEBB4185
// Copyright 2020 Cris Stringfellow
// Licensed under GPL-3.0
// https://github.com/cris691/discohash
// https://github.com/dosyago/discohash
#include <cstdio>
#include <inttypes.h>
#include <cstring>
#include "discohash.h"

#if defined(_MSC_VER)

#define FORCE_INLINE __forceinline

// Other compilers

#else // defined(_MSC_VER)

#define FORCE_INLINE inline __attribute__((always_inline))

#endif // !defined(_MSC_VER)

const int STATE = 32;
const int STATE64 = STATE >> 3;
const int STATEM = STATE-1;
const int HSTATE64M = (STATE64 >> 1)-1;
const int STATE64M = STATE64-1;
uint8_t disco_buf[STATE] = {0};
uint64_t P = 0xFFFFFFFFFFFFFFFF - 58;
uint64_t Q = 13166748625691186689U;
uint8_t *ds8 = (uint8_t *)disco_buf;
uint32_t *ds32 = (uint32_t *)disco_buf;
constexpr int STATE = 32;
constexpr int STATE64 = STATE >> 3;
constexpr int STATEM = STATE-1;
constexpr int HSTATE64M = (STATE64 >> 1)-1;
constexpr int STATE64M = STATE64-1;
alignas(uint64_t) uint8_t disco_buf[STATE] = {0};
constexpr uint64_t P = 0xFFFFFFFFFFFFFFFF - 58;
constexpr uint64_t Q = 13166748625691186689U;
alignas(uint64_t) uint8_t *ds8 = (uint8_t *)disco_buf;
uint64_t *ds = (uint64_t *)disco_buf;

//--------
// State mix function

FORCE_INLINE uint64_t rot( uint64_t v, int n)
static inline uint64_t rot( uint64_t v, int n)
{
n = n & 63U;
if (n)
v = (v >> n) | (v << (64-n));
return v;
}

FORCE_INLINE uint64_t rot32( uint64_t v, int n)
{
n = n & 31U;
if (n)
v = (v >> n) | (v << (64-n));
return v;
}

FORCE_INLINE uint8_t rot8( uint8_t v, int n)
static inline uint8_t rot8( uint8_t v, int n)
{
n = n & 7U;
if (n)
v = (v >> n) | (v << (8-n));
return v;
}

FORCE_INLINE void mixA()
{
int i = ds32[0] & 1;
int j = ds32[3] & 3;

ds[0] = rot(ds[0], ds[i]);
ds[i] *= (P % (ds32[j] + 1) + 1);
ds[1] += ds32[j];
}

FORCE_INLINE void mix(const int A)
static inline void mix(const int A)
{
const int B = A+1;
ds[A] *= P;
ds[A] = rot(ds[A], 23);
ds[A] *= Q;
//ds[A] = rot(ds[A], 23);

ds[B] ^= ds[A];

ds[B] *= P;
ds[B] = rot(ds[B], 23);
ds[B] *= Q;
//ds[B] = rot(ds[B], 23);
}

//---------
// Hash round function

FORCE_INLINE void round( const uint64_t * m64, const uint8_t * m8, int len )
static inline void round( const uint64_t * m64, const uint8_t * m8, int len )
{
int index = 0;
int sindex = 0;
Expand All @@ -98,11 +64,13 @@ uint64_t *ds = (uint64_t *)disco_buf;
counter += ~m64[index] + 1;
if ( sindex == HSTATE64M ) {
mix(0);
sindex++;
} else if ( sindex == STATE64M ) {
mix(2);
sindex = -1;
sindex = 0;
} else {
sindex++;
}
sindex++;
}

mix(1);
Expand All @@ -114,9 +82,10 @@ uint64_t *ds = (uint64_t *)disco_buf;
counter8 += ~m8[sindex] + 1;
mix(index%STATE64M);
if ( sindex >= STATEM ) {
sindex = -1;
sindex = 0;
} else {
sindex++;
}
sindex++;
}

mix(0);
Expand All @@ -127,20 +96,23 @@ uint64_t *ds = (uint64_t *)disco_buf;
//---------
// main hash function

void BEBB4185_64 ( const void * key, int len, unsigned seed, void * out )
void DISCoHAsH_64 ( const void * key, int len, unsigned seed, void * out )
{
int tempLen = len;
if ( tempLen == 0 ) {
tempLen = 1;
}
alignas(uint64_t) uint8_t* tempBuf = new uint8_t[tempLen];
const uint8_t *key8Arr = (uint8_t *)key;
const uint64_t *key64Arr = (uint64_t *)key;

const uint8_t seedbuf[16] = {0};
alignas(uint64_t) const uint8_t seedbuf[16] = {0};
const uint8_t *seed8Arr = (uint8_t *)seedbuf;
const uint64_t *seed64Arr = (uint64_t *)seedbuf;
uint32_t *seed32Arr = (uint32_t *)seedbuf;

// the cali number from the Matrix (1999)
seed32Arr[0] = 0xc5550690;
seed32Arr[0] -= seed;
// if seed mod doesn't work let's try reverse order of seed/key round calls
seed32Arr[1] = 1 + seed;
seed32Arr[2] = ~(1 - seed);
seed32Arr[3] = (1+seed) * 0xf00dacca;
Expand All @@ -151,23 +123,18 @@ uint64_t *ds = (uint64_t *)disco_buf;
ds[2] = 0xaccadacca80081e5;
ds[3] = 0xf00baaf00f00baaa;

round( key64Arr, key8Arr, len );
memcpy(tempBuf, key, len);
uint64_t* temp64 = reinterpret_cast<uint64_t*>(tempBuf);

round( temp64, key8Arr, len );
round( seed64Arr, seed8Arr, 16 );
round( ds, ds8, STATE );

/**
printf("ds = %#018" PRIx64 " %#018" PRIx64 " %#018" PRIx64 " %#018" PRIx64 "\n",
ds[0], ds[1], ds[2], ds[3] );
**/

const uint8_t output[STATE] = {0};
uint64_t *h = (uint64_t *)output;
//uint64_t h[4] = {ds[2] + ds[3], ds[3] ^ ds[2], ds[0] + ds[1], ds[1] ^ ds[0]}; // full 256-bit output

//h[0] = ds[1];
h[0] = ds[2];
h[1] = ds[3];
uint64_t h[1] = {ds[2] + ds[3]}; // 64-bit output

h[0] += h[1];
memcpy(out, h, sizeof(h));

((uint64_t *)out)[0] = h[0];
delete[] tempBuf;
}
25 changes: 5 additions & 20 deletions discohash.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
#include <stdint.h>

#ifndef _DISCOHASH_H_
#define _DISCOHASH_H_

// gotten from random.org
// as hex bytes that I formatted into 64-bit values

#if defined(_MSC_VER) && (_MSC_VER < 1600)
#pragma once

typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
typedef unsigned __int64 uint64_t;

// Other compilers

#else // defined(_MSC_VER)
#ifndef DISCOHASH_H
#define DISCOHASH_H

#include <stdint.h>

#endif // !defined(_MSC_VER)

void BEBB4185_64 ( const void * key, int len, uint32_t seed, void * out );
void DISCoHAsH_64(const void* key, int len, uint32_t seed, void* out);

#endif // _DISCOHASH_H
#endif // DISCOHASH_H

4 changes: 2 additions & 2 deletions doc/BEBB4185.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
-------------------------------------------------------------------------------
--- Testing BEBB4185 "BEBB4185 64" GOOD
--- Testing DISCoHAsH "DISCoHAsH 64" GOOD

[[[ Sanity Tests ]]]

Verification value 0xBEBB4185 ....... PASS
Verification value 0xDISCoHAsH ....... PASS
Running sanity check 1 .......... PASS
Running AppendedZeroesTest .......... PASS

Expand Down
2 changes: 1 addition & 1 deletion doc/air.html
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ <h1 id="smhasher">SMhasher</h1>
<td align="left"><a href="../#problems">UB</a>, too many bad seeds</td>
</tr>
<tr class="poor">
<td align="left"><a href="BEBB4185.txt">BEBB4185</a></td>
<td align="left"><a href="DISCoHAsH.txt">DISCoHAsH</a></td>
<td align="right">2951.01</td>
<td align="right">213.86</td>
<td align="right">346.87 (4)</td>
Expand Down
2 changes: 1 addition & 1 deletion doc/badseeds.log.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ Testing 0 internal secrets:

Input vcode 0x00000001, Output vcode 0x00000001, Result vcode 0x00000001
Verification value is 0x00000001 - Testing took 0.000207 seconds
--- Testing BEBB4185 "BEBB4185 64" GOOD
--- Testing DISCoHAsH "DISCoHAsH 64" GOOD

[[[ BadSeeds Tests ]]]

Expand Down
2 changes: 1 addition & 1 deletion doc/i686.html
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ <h1 id="smhasher">SMhasher</h1>
<td align="left"><a href="../#problems">UB</a>, too many bad seeds,</td>
</tr>
<tr class="poor">
<td align="left"><a href="BEBB4185.txt">BEBB4185</a></td>
<td align="left"><a href="DISCoHAsH.txt">DISCoHAsH</a></td>
<td align="right">2951.62</td>
<td align="right">222.03</td>
<td align="right">343.63 (4)</td>
Expand Down
2 changes: 1 addition & 1 deletion doc/intel.html
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ <h1 id="smhasher">SMhasher</h1>
<td align="left"><a href="../#problems">UB</a>, too many bad seeds</td>
</tr>
<tr class="poor">
<td align="left"><a href="BEBB4185.txt">BEBB4185</a></td>
<td align="left"><a href="DISCoHAsH.txt">DISCoHAsH</a></td>
<td align="right">2951.62</td>
<td align="right">222.03</td>
<td align="right">343.63 (4)</td>
Expand Down
2 changes: 1 addition & 1 deletion doc/phone.html
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ <h1 id="smhasher">SMhasher</h1>
<td align="left"><a href="../#problems">UB</a>, too many bad seeds</td>
</tr>
<tr class="poor">
<td align="left"><a href="BEBB4185.txt">BEBB4185</a></td>
<td align="left"><a href="DISCoHAsH.txt">DISCoHAsH</a></td>
<td align="right">3160.29</td>
<td align="right">164.25</td>
<td align="right">343.63 (4)</td>
Expand Down
2 changes: 1 addition & 1 deletion doc/ryzen3.html
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ <h1 id="smhasher">SMhasher</h1>
<td align="left">0.9% bias</td>
</tr>
<tr class="good">
<td align="left"><a href="BEBB4185.txt">BEBB4185</a></td>
<td align="left"><a href="DISCoHAsH.txt">DISCoHAsH</a></td>
<td align="right">4854.70</td>
<td align="right">165.51</td>
<td align="right">641.75 (1)</td>
Expand Down
2 changes: 1 addition & 1 deletion doc/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ <h1 id="smhasher">SMhasher</h1>
<td align="left">0.9% bias</td>
</tr>
<tr class="good">
<td align="left"><a href="BEBB4185.txt">BEBB4185</a></td>
<td align="left"><a href="DISCoHAsH.txt">DISCoHAsH</a></td>
<td align="right">3959.08</td>
<td align="right">199.92</td>
<td align="right">413.43 (6)</td>
Expand Down
8 changes: 4 additions & 4 deletions log.sanity
Original file line number Diff line number Diff line change
Expand Up @@ -1439,11 +1439,11 @@ Running AppendedZeroesTest .......... PASS

Input vcode 0x00000001, Output vcode 0x00000001, Result vcode 0x00000001
Verification value is 0x00000001 - Testing took 69.875176 seconds
--- Testing BEBB4185 "BEBB4185 64" GOOD
--- Testing DISCoHAsH "DISCoHAsH 64" GOOD

[[[ Sanity Tests ]]]

Verification value 0xBEBB4185 ....... PASS
Verification value 0xDISCoHAsH ....... PASS
Running sanity check 1 .......... PASS
Running AppendedZeroesTest .......... PASS

Expand Down Expand Up @@ -3920,11 +3920,11 @@ Running AppendedZeroesTest .......... PASS

Input vcode 0x00000001, Output vcode 0x00000001, Result vcode 0x00000001
Verification value is 0x00000001 - Testing took 88.364050 seconds
--- Testing BEBB4185 "BEBB4185 64" GOOD
--- Testing DISCoHAsH "DISCoHAsH 64" GOOD

[[[ Sanity Tests ]]]

Verification value 0xBEBB4185 ....... PASS
Verification value 0xDISCoHAsH ....... PASS
Running sanity check 1 .......... PASS
Running AppendedZeroesTest .......... PASS

Expand Down
2 changes: 1 addition & 1 deletion log.speed
Original file line number Diff line number Diff line change
Expand Up @@ -7764,7 +7764,7 @@ Running fast HashMapTest: 944.435 cycles/op (19.1 stdv) ....... PASS

Input vcode 0x00000001, Output vcode 0x00000001, Result vcode 0x00000001
Verification value is 0x00000001 - Testing took 156.710037 seconds
--- Testing BEBB4185 "BEBB4185 64" GOOD
--- Testing DISCoHAsH "DISCoHAsH 64" GOOD

[[[ Speed Tests ]]]

Expand Down
4 changes: 2 additions & 2 deletions log.speed-phone
Original file line number Diff line number Diff line change
Expand Up @@ -5337,7 +5337,7 @@ Unable to open words dict file /usr/share/dict/words

Input vcode 0x00000001, Output vcode 0x00000001, Result vcode 0x00000001
Verification value is 0x00000001 - Testing took 40.532609 seconds
--- Testing BEBB4185 "BEBB4185 64" GOOD
--- Testing DISCoHAsH "DISCoHAsH 64" GOOD

[[[ Speed Tests ]]]

Expand Down Expand Up @@ -10614,7 +10614,7 @@ Small key speed test - 31-byte keys - 45.00 cycles/hash
Average 35.981 cycles/hash


--- Testing BEBB4185 "BEBB4185 64" GOOD
--- Testing DISCoHAsH "DISCoHAsH 64" GOOD

[[[ Speed Tests ]]]

Expand Down
9 changes: 1 addition & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,7 @@ HashInfo g_hashes[] =
// and now the quality hash funcs, slowest first
{ tifuhash_64, 64, TIFU_VERIF, "tifuhash_64", "Tiny Floatingpoint Unique Hash with continued egyptian fractions", GOOD, {} },
{ beamsplitter_64, 64, 0x1BDF358B, "beamsplitter","A possibly universal hash made with a 10x64 s-box.", GOOD, {} },
#if defined(_MSC_VER) && defined(LTO)
# define BEBB4185_VERIF 0xB7013C8F
#else
# define BEBB4185_VERIF 0xBEBB4185
#endif
#ifndef HAVE_ALIGNED_ACCESS_REQUIRED
{ BEBB4185_64, 64, BEBB4185_VERIF, "BEBB4185", "BEBB4185 64", GOOD, { } },
#endif
{ DISCoHAsH_64, 64, 0xDISCoHAsH, "DISCoHAsH", "DISCoHAsH 64", GOOD, { } },
{ fasthash32_test, 32, 0xE9481AFC, "fasthash32", "fast-hash 32bit", GOOD, {0x880355f21e6d1965ULL} },
{ fasthash64_test, 64, 0xA16231A7, "fasthash64", "fast-hash 64bit", GOOD, {0x880355f21e6d1965ULL} },
// different verif on gcc vs clang
Expand Down

0 comments on commit 250b1b2

Please sign in to comment.