blob: 728ef932a1accbfe6b2fc33754fdf2a660e00bee [file] [log] [blame]
Christian Heimes985ecdc2013-11-20 11:46:18 +01001#ifndef Py_HASH_H
2
3#define Py_HASH_H
4#ifdef __cplusplus
5extern "C" {
6#endif
7
8/* Helpers for hash functions */
9#ifndef Py_LIMITED_API
Raymond Hettingera07da092021-04-22 08:34:57 -070010PyAPI_FUNC(Py_hash_t) _Py_HashDouble(PyObject *, double);
Andy Lester3d069532020-02-05 15:09:57 -060011PyAPI_FUNC(Py_hash_t) _Py_HashPointer(const void*);
Victor Stinnerf4532212020-05-12 18:46:20 +020012// Similar to _Py_HashPointer(), but don't replace -1 with -2
13PyAPI_FUNC(Py_hash_t) _Py_HashPointerRaw(const void*);
Christian Heimes985ecdc2013-11-20 11:46:18 +010014PyAPI_FUNC(Py_hash_t) _Py_HashBytes(const void*, Py_ssize_t);
15#endif
16
17/* Prime multiplier used in string and various other hashes. */
18#define _PyHASH_MULTIPLIER 1000003UL /* 0xf4243 */
19
20/* Parameters used for the numeric hash implementation. See notes for
Ned Batchelder01ae58d2017-11-09 11:55:34 -050021 _Py_HashDouble in Python/pyhash.c. Numeric hashes are based on
Christian Heimes985ecdc2013-11-20 11:46:18 +010022 reduction modulo the prime 2**_PyHASH_BITS - 1. */
23
24#if SIZEOF_VOID_P >= 8
25# define _PyHASH_BITS 61
26#else
27# define _PyHASH_BITS 31
28#endif
29
30#define _PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
31#define _PyHASH_INF 314159
Christian Heimes985ecdc2013-11-20 11:46:18 +010032#define _PyHASH_IMAG _PyHASH_MULTIPLIER
33
34
35/* hash secret
36 *
37 * memory layout on 64 bit systems
38 * cccccccc cccccccc cccccccc uc -- unsigned char[24]
39 * pppppppp ssssssss ........ fnv -- two Py_hash_t
Benjamin Peterson9b3d7702016-09-06 13:24:00 -070040 * k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t
Christian Heimes985ecdc2013-11-20 11:46:18 +010041 * ........ ........ ssssssss djbx33a -- 16 bytes padding + one Py_hash_t
42 * ........ ........ eeeeeeee pyexpat XML hash salt
43 *
44 * memory layout on 32 bit systems
45 * cccccccc cccccccc cccccccc uc
46 * ppppssss ........ ........ fnv -- two Py_hash_t
Benjamin Peterson9b3d7702016-09-06 13:24:00 -070047 * k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t (*)
Christian Heimes985ecdc2013-11-20 11:46:18 +010048 * ........ ........ ssss.... djbx33a -- 16 bytes padding + one Py_hash_t
49 * ........ ........ eeee.... pyexpat XML hash salt
50 *
51 * (*) The siphash member may not be available on 32 bit platforms without
52 * an unsigned int64 data type.
53 */
Martin v. Löwis1c0689c2014-01-03 21:36:49 +010054#ifndef Py_LIMITED_API
Christian Heimes985ecdc2013-11-20 11:46:18 +010055typedef union {
56 /* ensure 24 bytes */
57 unsigned char uc[24];
58 /* two Py_hash_t for FNV */
59 struct {
60 Py_hash_t prefix;
61 Py_hash_t suffix;
62 } fnv;
Christian Heimes985ecdc2013-11-20 11:46:18 +010063 /* two uint64 for SipHash24 */
64 struct {
Benjamin Peterson9b3d7702016-09-06 13:24:00 -070065 uint64_t k0;
66 uint64_t k1;
Christian Heimes985ecdc2013-11-20 11:46:18 +010067 } siphash;
Christian Heimes985ecdc2013-11-20 11:46:18 +010068 /* a different (!) Py_hash_t for small string optimization */
69 struct {
70 unsigned char padding[16];
71 Py_hash_t suffix;
72 } djbx33a;
73 struct {
74 unsigned char padding[16];
75 Py_hash_t hashsalt;
76 } expat;
77} _Py_HashSecret_t;
78PyAPI_DATA(_Py_HashSecret_t) _Py_HashSecret;
Martin v. Löwis1c0689c2014-01-03 21:36:49 +010079#endif
Christian Heimes985ecdc2013-11-20 11:46:18 +010080
81#ifdef Py_DEBUG
82PyAPI_DATA(int) _Py_HashSecret_Initialized;
83#endif
84
85
86/* hash function definition */
87#ifndef Py_LIMITED_API
88typedef struct {
89 Py_hash_t (*const hash)(const void *, Py_ssize_t);
90 const char *name;
91 const int hash_bits;
92 const int seed_bits;
93} PyHash_FuncDef;
94
95PyAPI_FUNC(PyHash_FuncDef*) PyHash_GetFuncDef(void);
96#endif
97
98
99/* cutoff for small string DJBX33A optimization in range [1, cutoff).
100 *
101 * About 50% of the strings in a typical Python application are smaller than
102 * 6 to 7 chars. However DJBX33A is vulnerable to hash collision attacks.
103 * NEVER use DJBX33A for long strings!
104 *
105 * A Py_HASH_CUTOFF of 0 disables small string optimization. 32 bit platforms
106 * should use a smaller cutoff because it is easier to create colliding
107 * strings. A cutoff of 7 on 64bit platforms and 5 on 32bit platforms should
108 * provide a decent safety margin.
109 */
110#ifndef Py_HASH_CUTOFF
111# define Py_HASH_CUTOFF 0
112#elif (Py_HASH_CUTOFF > 7 || Py_HASH_CUTOFF < 0)
113# error Py_HASH_CUTOFF must in range 0...7.
114#endif /* Py_HASH_CUTOFF */
115
116
117/* hash algorithm selection
118 *
119 * The values for Py_HASH_SIPHASH24 and Py_HASH_FNV are hard-coded in the
120 * configure script.
121 *
122 * - FNV is available on all platforms and architectures.
Min ho Kim39d87b52019-08-31 06:21:19 +1000123 * - SIPHASH24 only works on platforms that don't require aligned memory for integers.
Christian Heimes985ecdc2013-11-20 11:46:18 +0100124 * - With EXTERNAL embedders can provide an alternative implementation with::
125 *
126 * PyHash_FuncDef PyHash_Func = {...};
127 *
128 * XXX: Figure out __declspec() for extern PyHash_FuncDef.
129 */
130#define Py_HASH_EXTERNAL 0
131#define Py_HASH_SIPHASH24 1
132#define Py_HASH_FNV 2
133
134#ifndef Py_HASH_ALGORITHM
Benjamin Peterson9b3d7702016-09-06 13:24:00 -0700135# ifndef HAVE_ALIGNED_REQUIRED
Christian Heimes985ecdc2013-11-20 11:46:18 +0100136# define Py_HASH_ALGORITHM Py_HASH_SIPHASH24
137# else
138# define Py_HASH_ALGORITHM Py_HASH_FNV
139# endif /* uint64_t && uint32_t && aligned */
140#endif /* Py_HASH_ALGORITHM */
141
142#ifdef __cplusplus
143}
144#endif
145
146#endif /* !Py_HASH_H */