Skip to content

Commit de12ceb

Browse files
committed
Use SipHash-1-3 instead of SipHash-2-4.
1 parent 09796f2 commit de12ceb

3 files changed

Lines changed: 19 additions & 20 deletions

File tree

Include/pyhash.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,24 @@ PyAPI_FUNC(PyHash_FuncDef*) PyHash_GetFuncDef(void);
114114

115115
/* hash algorithm selection
116116
*
117-
* The values for Py_HASH_SIPHASH24 and Py_HASH_FNV are hard-coded in the
117+
* The values for Py_HASH_SIPHASH13 and Py_HASH_FNV are hard-coded in the
118118
* configure script.
119119
*
120120
* - FNV is available on all platforms and architectures.
121-
* - SIPHASH24 only works on platforms that don't require aligned memory for integers.
121+
* - SIPHASH only works on platforms that don't require aligned memory for integers.
122122
* - With EXTERNAL embedders can provide an alternative implementation with::
123123
*
124124
* PyHash_FuncDef PyHash_Func = {...};
125125
*
126126
* XXX: Figure out __declspec() for extern PyHash_FuncDef.
127127
*/
128128
#define Py_HASH_EXTERNAL 0
129-
#define Py_HASH_SIPHASH24 1
129+
#define Py_HASH_SIPHASH 1
130130
#define Py_HASH_FNV 2
131131

132132
#ifndef Py_HASH_ALGORITHM
133133
# ifndef HAVE_ALIGNED_REQUIRED
134-
# define Py_HASH_ALGORITHM Py_HASH_SIPHASH24
134+
# define Py_HASH_ALGORITHM Py_HASH_SIPHASH
135135
# else
136136
# define Py_HASH_ALGORITHM Py_HASH_FNV
137137
# endif /* uint64_t && uint32_t && aligned */

Python/pyhash.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,13 @@ static PyHash_FuncDef PyHash_Func = {fnv, "fnv", 8 * SIZEOF_PY_HASH_T,
364364
d = ROTATE(d, t) ^ c; \
365365
a = ROTATE(a, 32);
366366

367-
#define DOUBLE_ROUND(v0,v1,v2,v3) \
368-
HALF_ROUND(v0,v1,v2,v3,13,16); \
369-
HALF_ROUND(v2,v1,v0,v3,17,21); \
367+
#define SINGLE_ROUND(v0,v1,v2,v3) \
370368
HALF_ROUND(v0,v1,v2,v3,13,16); \
371369
HALF_ROUND(v2,v1,v0,v3,17,21);
372370

373371

374372
static uint64_t
375-
siphash24(uint64_t k0, uint64_t k1, const void *src, Py_ssize_t src_sz) {
373+
siphash13(uint64_t k0, uint64_t k1, const void *src, Py_ssize_t src_sz) {
376374
uint64_t b = (uint64_t)src_sz << 56;
377375
const uint8_t *in = (const uint8_t*)src;
378376

@@ -391,7 +389,7 @@ siphash24(uint64_t k0, uint64_t k1, const void *src, Py_ssize_t src_sz) {
391389
in += sizeof(mi);
392390
src_sz -= sizeof(mi);
393391
v3 ^= mi;
394-
DOUBLE_ROUND(v0,v1,v2,v3);
392+
SINGLE_ROUND(v0,v1,v2,v3);
395393
v0 ^= mi;
396394
}
397395

@@ -409,11 +407,12 @@ siphash24(uint64_t k0, uint64_t k1, const void *src, Py_ssize_t src_sz) {
409407
b |= _le64toh(t);
410408

411409
v3 ^= b;
412-
DOUBLE_ROUND(v0,v1,v2,v3);
410+
SINGLE_ROUND(v0,v1,v2,v3);
413411
v0 ^= b;
414412
v2 ^= 0xff;
415-
DOUBLE_ROUND(v0,v1,v2,v3);
416-
DOUBLE_ROUND(v0,v1,v2,v3);
413+
SINGLE_ROUND(v0,v1,v2,v3);
414+
SINGLE_ROUND(v0,v1,v2,v3);
415+
SINGLE_ROUND(v0,v1,v2,v3);
417416

418417
/* modified */
419418
t = (v0 ^ v1) ^ (v2 ^ v3);
@@ -423,19 +422,19 @@ siphash24(uint64_t k0, uint64_t k1, const void *src, Py_ssize_t src_sz) {
423422
uint64_t
424423
_Py_KeyedHash(uint64_t key, const void *src, Py_ssize_t src_sz)
425424
{
426-
return siphash24(key, 0, src, src_sz);
425+
return siphash13(key, 0, src, src_sz);
427426
}
428427

429428

430-
#if Py_HASH_ALGORITHM == Py_HASH_SIPHASH24
429+
#if Py_HASH_ALGORITHM == Py_HASH_SIPHASH
431430
static Py_hash_t
432431
pysiphash(const void *src, Py_ssize_t src_sz) {
433-
return (Py_hash_t)siphash24(
432+
return (Py_hash_t)siphash13(
434433
_le64toh(_Py_HashSecret.siphash.k0), _le64toh(_Py_HashSecret.siphash.k1),
435434
src, src_sz);
436435
}
437436

438-
static PyHash_FuncDef PyHash_Func = {pysiphash, "siphash24", 64, 128};
437+
static PyHash_FuncDef PyHash_Func = {pysiphash, "siphash13", 64, 128};
439438
#endif
440439

441440
#ifdef __cplusplus

configure.ac

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3036,17 +3036,17 @@ fi
30363036
# str, bytes and memoryview hash algorithm
30373037
AH_TEMPLATE(Py_HASH_ALGORITHM,
30383038
[Define hash algorithm for str, bytes and memoryview.
3039-
SipHash24: 1, FNV: 2, externally defined: 0])
3039+
SipHash: 1, FNV: 2, externally defined: 0])
30403040

30413041
AC_MSG_CHECKING(for --with-hash-algorithm)
30423042
dnl quadrigraphs "@<:@" and "@:>@" produce "[" and "]" in the output
30433043
AC_ARG_WITH(hash_algorithm,
3044-
AS_HELP_STRING([--with-hash-algorithm=@<:@fnv|siphash24@:>@],
3045-
[select hash algorithm for use in Python/pyhash.c (default is SipHash24)]),
3044+
AS_HELP_STRING([--with-hash-algorithm=@<:@fnv|siphash@:>@],
3045+
[select hash algorithm for use in Python/pyhash.c (default is SipHash)]),
30463046
[
30473047
AC_MSG_RESULT($withval)
30483048
case "$withval" in
3049-
siphash24)
3049+
siphash)
30503050
AC_DEFINE(Py_HASH_ALGORITHM, 1)
30513051
;;
30523052
fnv)

0 commit comments

Comments
 (0)