Debian Codegraph @

runtime-lib/siphash/siphash.c

raw symbols SCIP
1 /* This file has been modified to be used as one of our hash-folding algorithm.
2 The reference implementation of siphash is kept in siphash.c.txt */
3 /*
4 SipHash reference C implementation
5
6 Copyright (c) 2012-2014 Jean-Philippe Aumasson
7 <[email protected]>
8 Copyright (c) 2012-2014 Daniel J. Bernstein <[email protected]>
9
10 To the extent possible under law, the author(s) have dedicated all copyright
11 and related and neighboring rights to this software to the public domain
12 worldwide. This software is distributed without any warranty.
13
14 You should have received a copy of the CC0 Public Domain Dedication along
15 with
16 this software. If not, see
17 <http://creativecommons.org/publicdomain/zero/1.0/>.
18
19 */
20
21 #include <stdint.h>
22 #include <string.h>
23 #include <caml/alloc.h>
24
25 /* default: SipHash-2-4 */
26 #define cROUNDS 1
27 #define dROUNDS 3
28
29 #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
30
31 #define U32TO8_LE(p, v) \
32 (p)[0] = (uint8_t)((v)); \
33 (p)[1] = (uint8_t)((v) >> 8); \
34 (p)[2] = (uint8_t)((v) >> 16); \
35 (p)[3] = (uint8_t)((v) >> 24);
36
37 #define U64TO8_LE(p, v) \
38 U32TO8_LE((p), (uint32_t)((v))); \
39 U32TO8_LE((p) + 4, (uint32_t)((v) >> 32));
40
41 #define U8TO64_LE(p) \
42 (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \
43 ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \
44 ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \
45 ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
46
47 #define SIPROUND(h) \
48 do { \
49 h->v0 += h->v1; \
50 h->v1 = ROTL(h->v1, 13); \
51 h->v1 ^= h->v0; \
52 h->v0 = ROTL(h->v0, 32); \
53 h->v2 += h->v3; \
54 h->v3 = ROTL(h->v3, 16); \
55 h->v3 ^= h->v2; \
56 h->v0 += h->v3; \
57 h->v3 = ROTL(h->v3, 21); \
58 h->v3 ^= h->v0; \
59 h->v2 += h->v1; \
60 h->v1 = ROTL(h->v1, 17); \
61 h->v1 ^= h->v2; \
62 h->v2 = ROTL(h->v2, 32); \
63 } while (0)
64
65 #define TRACE(h) \
66 do { \
67 printf(" h->v0 %08x %08x\n", (uint32_t)(h->v0 >> 32), \
68 (uint32_t)h->v0); \
69 printf(" h->v1 %08x %08x\n", (uint32_t)(h->v1 >> 32), \
70 (uint32_t)h->v1); \
71 printf(" h->v2 %08x %08x\n", (uint32_t)(h->v2 >> 32), \
72 (uint32_t)h->v2); \
73 printf(" h->v3 %08x %08x\n", (uint32_t)(h->v3 >> 32), \
74 (uint32_t)h->v3); \
75 } while (0)
76
77 /* The code above this line is mostly a copy and paste from siphash reference
78 implementation. The code below has been substantially modified. */
79
80 struct hash_state
81 {
82 uint64_t v0, v1, v2, v3;
83 };
84
85 /* internal */
86 void siphash_fold_uint64(value state, uint64_t i)
87 {
88 struct hash_state * h = (struct hash_state *) state;
89 unsigned round;
90 h->v3 ^= i;
91 for (round = 0; round < cROUNDS; ++round)
92 SIPROUND(h);
93 h->v0 ^= i;
94 }
95
96 CAMLprim value siphash_fold_int64(value st, value i)
97 {
98 siphash_fold_uint64(st, Int64_val(i));
99 return st;
100 }
101
102 CAMLprim value siphash_fold_int(value st, value i)
103 {
104 siphash_fold_uint64(st, Long_val(i));
105 return st;
106 }
107
108 /* The code has been 'borrowed' from byterun/hash.c in ocaml */
109 CAMLexport uint64_t caml_hash_normalize_double_to_int64(double d)
110 {
111 union {
112 double d;
113 uint64_t i64;
114 #if defined(ARCH_BIG_ENDIAN) || (defined(__arm__) && !defined(__ARM_EABI__))
115 struct { uint32_t h; uint32_t l; } i;
116 #else
117 struct { uint32_t l; uint32_t h; } i;
118 #endif
119 } u;
120 uint32_t h, l;
121 /* Convert to two 32-bit halves */
122 u.d = d;
123 h = u.i.h; l = u.i.l;
124 /* Normalize NaNs */
125 if ((h & 0x7FF00000) == 0x7FF00000 && (l | (h & 0xFFFFF)) != 0) {
126 h = 0x7FF00000;
127 l = 0x00000001;
128 }
129 /* Normalize -0 into +0 */
130 else if (h == 0x80000000 && l == 0) {
131 h = 0;
132 }
133 u.i.h = h;
134 u.i.l = l;
135 return u.i64;
136 }
137
138 CAMLprim value siphash_fold_float(value st, value i)
139 {
140 uint64_t x = caml_hash_normalize_double_to_int64(Double_val(i));
141 siphash_fold_uint64(st, x);
142 return st;
143 }
144
145 CAMLprim value siphash_fold_string(value st, value s)
146 {
147 const mlsize_t len = caml_string_length(s);
148 const int left = len & 7;
149 unsigned char * in;
150 mlsize_t i;
151 uint64_t w;
152 /* The length must be mixed in before the elements to avoid a violation of the rule described by Perfect_hash. */
153 siphash_fold_uint64(st, ((uint64_t)len));
154 /* Mix by 64-bit blocks (little-endian) */
155 for (i = 0; i + 8 <= len; i += 8) {
156 w = U8TO64_LE(Bp_val(s)+i);
157 siphash_fold_uint64(st, w);
158 }
159 in = (uint8_t*)Bp_val(s) + i;
160 w = ((uint64_t)len) << 56;
161 switch (left) {
162 case 7:
163 w |= ((uint64_t)in[6]) << 48;
164 /* fall through */
165 case 6:
166 w |= ((uint64_t)in[5]) << 40;
167 /* fall through */
168 case 5:
169 w |= ((uint64_t)in[4]) << 32;
170 /* fall through */
171 case 4:
172 w |= ((uint64_t)in[3]) << 24;
173 /* fall through */
174 case 3:
175 w |= ((uint64_t)in[2]) << 16;
176 /* fall through */
177 case 2:
178 w |= ((uint64_t)in[1]) << 8;
179 /* fall through */
180 case 1:
181 w |= ((uint64_t)in[0]);
182 break;
183 case 0:
184 break;
185 }
186 siphash_fold_uint64(st,w);
187 return st;
188 }
189
190 CAMLprim value siphash_alloc ()
191 {
192 return caml_alloc_small(sizeof(struct hash_state) / sizeof(value), Abstract_tag);
193 }
194
195 CAMLprim value siphash_reset (value st, value key)
196 {
197 struct hash_state * h = (struct hash_state *) (Op_val(st));
198 char buffer[16];
199 uint64_t k0, k1;
200 unsigned i;
201 unsigned key_len = caml_string_length(key);
202 /* initialize the buffer */
203 memset(buffer, 0, 16);
204 /* copy the first 16 chars of the key to the buffer */
205 for (i = 0; i < (key_len > 16 ? 16 : key_len); i++)
206 buffer[i] = Byte_u(key,i);
207 /* initialize k0 and k1 */
208 k0 = U8TO64_LE(buffer);
209 k1 = U8TO64_LE(buffer + 8);
210 /* Those are verbatim from siphash reference implementation.*/
211 /* "somepseudorandomlygeneratedbytes" */
212 h->v0 = 0x736f6d6570736575ULL ^ k0;
213 h->v1 = 0x646f72616e646f6dULL ^ k1;
214 h->v2 = 0x6c7967656e657261ULL ^ k0;
215 h->v3 = 0x7465646279746573ULL ^ k1;
216 /* If we switch to DOUBLE, we need to bring back the rest of siphash init
217 code. */
218 return st;
219 }
220
221 /* This function destroy (that is, mixes) the content of the hash state. That
222 means that it is not possible to stop in the middle of a hash, and ask for
223 the current hash value, and continue to hash the rest of a structure. */
224 CAMLprim value siphash_get_hash_value(value st)
225 {
226 struct hash_state * h = (struct hash_state *) st;
227 uint64_t b;
228 unsigned i;
229 /* This is the final mix step of the reference implementation of siphash, in
230 the "non-DOUBLE" case (that is, where we get a single int64 out of the hash
231 state). */
232 h->v2 ^= 0xff;
233 for (i = 0; i < dROUNDS; ++i)
234 SIPROUND(h);
235 b = h->v0 ^ h->v1 ^ h->v2 ^ h->v3;
236 /* We lose one bit of precision here. */
237 return Val_long(b);
238 }
239
240 CAMLprim value siphash_blit_hash_to_bytes(value st, value bytes)
241 {
242 struct hash_state * h = (struct hash_state *) st;
243 uint8_t *str = (uint8_t *) (Bp_val(bytes));
244 U64TO8_LE(str + 0, h -> v0);
245 U64TO8_LE(str + 8, h -> v1);
246 U64TO8_LE(str + 16, h -> v2);
247 U64TO8_LE(str + 24, h -> v3);
248 return Val_long(0);
249 }