Skip to content

Commit c2ae512

Browse files
committed
perl_siphash.h - add documentation
Scott Baker asked for documentation for these functions. This adds documentation sections so that autodoc can include docs for these functions. Includes usage examples.
1 parent eb5a17c commit c2ae512

File tree

1 file changed

+190
-5
lines changed

1 file changed

+190
-5
lines changed

perl_siphash.h

+190-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,197 @@
55
*
66
* https://www.131002.net/siphash/
77
*
8-
* This implementation seems to perform slightly slower than one-at-a-time for
9-
* short keys, but degrades slower for longer keys. Murmur Hash outperforms it
10-
* regardless of keys size.
8+
* Naming convention:
119
*
12-
* It is 64 bit only.
13-
*/
10+
* S_perl_hash_siphash_N_M: the N refers to how many rounds are performed per
11+
* block. The M refers to how many rounds are performed as part of the
12+
* finalizer. Increased values of either improve security, but decrease
13+
* performance.
14+
*
15+
* _with_state: these functions take a 32 bit state vector prepared by
16+
* S_perl_siphash_seed_state(). Functions without 'with_state' take a 16
17+
* byte seed vector and call S_perl_siphash_seed_state() implicitly. If
18+
* you are hashing many things with the same seed, the _with_state
19+
* variants are faster.
20+
*
21+
* _64: returns a 64 bit hash
22+
*
23+
* no-suffix: returns a 32 bit hash.
24+
*
25+
* This file defines 9 functions related to implementing 2 variants of
26+
* the Siphash family of hash functions, Siphash-2-4, and Siphash-1-3.
27+
28+
=for apidoc_section $numeric
29+
=for apidoc eST|void|S_perl_siphash_seed_state \
30+
|const unsigned char * const seed_buf \
31+
|unsigned char * state_buf
32+
33+
Takes a 16 byte seed and converts it into a 32 byte state buffer. The
34+
contents of state_buf will be overwritten.
35+
36+
If you need to hash a lot of things, then you can use this to process
37+
the seed once, and then reuse the state over and over.
38+
39+
The siphash functions which take a seed argument will call this function
40+
implicitly every time they are used. Those which take a state argument
41+
require the seed to be converted into a state before they are used.
42+
43+
See the various _with_state siphash functions for a usage example.
44+
45+
=for apidoc eSTP|U64|S_perl_hash_siphash_1_3_with_state_64\
46+
|const unsigned char * const state \
47+
|const unsigned char *in|const STRLEN inlen
48+
49+
Implements the variant of Siphash which performs 1 round function
50+
per block, and 3 as part of the finalizer.
51+
52+
Takes a 32 byte 'state' vector prepared by S_perl_siphash_seed_state()
53+
and uses it to hash C<inlen> bytes from the buffer pointed to by C<in>,
54+
returns a 64 bit hash.
55+
56+
The following code should return 0xB70339FD9E758A5C
57+
58+
U8 state[32];
59+
char seed[] = "Call me Ishmael.";
60+
S_perl_siphash_seed_state((const U8*)seed, state);
61+
62+
char in[] = "It is not down on any map; true places never are.";
63+
U64 hash = S_perl_hash_siphash_1_3_with_state_64(
64+
state, (const U8*)in, sizeof(in)-1);
65+
66+
=for apidoc eSTP|U32|S_perl_hash_siphash_1_3_with_state\
67+
|const unsigned char * const state \
68+
|const unsigned char *in|const STRLEN inlen
69+
70+
Implements the variant of Siphash which performs 1 round function
71+
per block, and 3 as part of the finalizer.
72+
73+
Takes a 32 byte 'state' vector prepared by S_perl_siphash_seed_state()
74+
and uses it to hash C<inlen> bytes from the buffer pointed to by C<in>,
75+
returns a 32 bit hash.
76+
77+
The following code should return 0x2976B3A1
78+
79+
U8 state[32];
80+
char seed[] = "Call me Ishmael.";
81+
S_perl_siphash_seed_state((const U8*)seed, state);
82+
83+
char in[] = "It is not down on any map; true places never are.";
84+
U32 hash = S_perl_hash_siphash_1_3_with_state(
85+
state, (const U8*)in, sizeof(in)-1);
86+
87+
=for apidoc eSTP|U64|S_perl_hash_siphash_1_3_64\
88+
|const unsigned char * const seed \
89+
|const unsigned char *in|const STRLEN inlen
90+
91+
Implements the variant of Siphash which performs 1 round function
92+
per block, and 3 as part of the finalizer.
93+
94+
Takes a 16 byte C<seed> vector, and uses it to hash C<inlen> bytes
95+
from the buffer pointed to by C<in>, returns a 64 bit hash.
96+
97+
The following code should return 0xB70339FD9E758A5C
98+
99+
char seed[] = "Call me Ishmael.";
100+
char in[] = "It is not down on any map; true places never are.";
101+
U64 hash = S_perl_hash_siphash_1_3_64(
102+
(const U8*)seed, (const U8*)in, sizeof(in)-1);
103+
104+
=for apidoc eSTP|U64|S_perl_hash_siphash_1_3\
105+
|const unsigned char * const seed \
106+
|const unsigned char *in|const STRLEN inlen
107+
108+
Implements the variant of Siphash which performs 1 round function
109+
per block, and 3 as part of the finalizer.
110+
111+
Takes a 16 byte C<seed> vector, and uses it to hash C<inlen> bytes
112+
from the buffer pointed to by C<in>, returns a 32 bit hash.
113+
114+
The following code should return 0x2976B3A1
115+
116+
char seed[] = "Call me Ishmael.";
117+
char in[] = "It is not down on any map; true places never are.";
118+
U32 hash = S_perl_hash_siphash_1_3(
119+
(const U8*)seed, (const U8*)in, sizeof(in)-1);
120+
121+
=for apidoc eSTP|U64|S_perl_hash_siphash_2_4_with_state_64\
122+
|const unsigned char * const state \
123+
|const unsigned char *in|const STRLEN inlen
124+
125+
Implements the variant of Siphash which performs 2 round functions
126+
per block, and 4 as part of the finalizer.
127+
128+
Takes a 32 byte 'state' vector prepared by S_perl_siphash_seed_state()
129+
and uses it to hash C<inlen> bytes from the buffer pointed to by C<in>,
130+
returns a 64 bit hash.
131+
132+
The following code should return 0x1E84CF1D7AA516B7
133+
134+
U8 state[32];
135+
char seed[] = "Call me Ishmael.";
136+
S_perl_siphash_seed_state((const U8*)seed, state);
137+
138+
char in[] = "It is not down on any map; true places never are.";
139+
U64 hash = S_perl_hash_siphash_2_4_with_state_64(
140+
state, (const U8*)in, sizeof(in)-1);
141+
142+
=for apidoc eSTP|U32|S_perl_hash_siphash_2_4_with_state\
143+
|const unsigned char * const state \
144+
|const unsigned char *in|const STRLEN inlen
145+
146+
Implements the variant of Siphash which performs 2 round function
147+
per block, and 4 as part of the finalizer.
148+
149+
Takes a 32 byte 'state' vector prepared by S_perl_siphash_seed_state()
150+
and uses it to hash C<inlen> bytes from the buffer pointed to by C<in>,
151+
returns a 32 bit hash.
152+
153+
The following code should return 0x6421D9AA
154+
155+
U8 state[32];
156+
char seed[] = "Call me Ishmael.";
157+
S_perl_siphash_seed_state((const U8*)seed, state);
158+
159+
char in[] = "It is not down on any map; true places never are.";
160+
U32 hash = S_perl_hash_siphash_2_4_with_state(
161+
state, (const U8*)in, sizeof(in)-1);
162+
163+
=for apidoc eSTP|U64|S_perl_hash_siphash_2_4_64\
164+
|const unsigned char * const seed \
165+
|const unsigned char *in|const STRLEN inlen
166+
167+
Implements the variant of Siphash which performs 2 round functions
168+
per block, and 4 as part of the finalizer.
169+
170+
Takes a 16 byte C<seed> vector, and uses it to hash C<inlen> bytes
171+
from the buffer pointed to by C<in>, returns a 64 bit hash.
172+
173+
The following code should return 0x1E84CF1D7AA516B7
174+
175+
char seed[] = "Call me Ishmael.";
176+
char in[] = "It is not down on any map; true places never are.";
177+
U64 hash = S_perl_hash_siphash_2_4_64(
178+
(const U8*)seed, (const U8*)in, sizeof(in)-1);
179+
180+
=for apidoc eSTP|U32|S_perl_hash_siphash_2_4\
181+
|const unsigned char * const seed \
182+
|const unsigned char *in|const STRLEN inlen
183+
184+
Implements the variant of Siphash which performs 2 round functions
185+
per block, and 4 as part of the finalizer.
186+
187+
Takes a 16 byte C<seed> vector, and uses it to hash C<inlen> bytes
188+
from the buffer pointed to by C<in>, returns a 32 bit hash.
189+
190+
The following code should return 0x6421D9AA
191+
192+
char seed[] = "Call me Ishmael.";
193+
char in[] = "It is not down on any map; true places never are.";
194+
U32 hash = S_perl_hash_siphash_2_4(
195+
(const U8*)seed, (const U8*)in, sizeof(in)-1);
196+
197+
=cut
198+
*/
14199

15200
#ifdef CAN64BITHASH
16201

0 commit comments

Comments
 (0)