| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <stdint.h> |
| 22 |
#include <string.h> |
| 23 |
#include <caml/alloc.h> |
| 24 |
|
| 25 |
|
| 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 |
|
| 78 |
|
| 79 |
|
| 80 |
struct hash_state |
| 81 |
{ |
| 82 |
uint64_t v0, v1, v2, v3; |
| 83 |
}; |
| 84 |
|
| 85 |
|
| 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 |
|
| 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 |
|
| 122 |
u.d = d; |
| 123 |
h = u.i.h; l = u.i.l; |
| 124 |
|
| 125 |
if ((h & 0x7FF00000) == 0x7FF00000 && (l | (h & 0xFFFFF)) != 0) { |
| 126 |
h = 0x7FF00000; |
| 127 |
l = 0x00000001; |
| 128 |
} |
| 129 |
|
| 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 |
|
| 153 |
siphash_fold_uint64(st, ((uint64_t)len)); |
| 154 |
|
| 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 |
|
| 165 |
case 6: |
| 166 |
w |= ((uint64_t)in[5]) << 40; |
| 167 |
|
| 168 |
case 5: |
| 169 |
w |= ((uint64_t)in[4]) << 32; |
| 170 |
|
| 171 |
case 4: |
| 172 |
w |= ((uint64_t)in[3]) << 24; |
| 173 |
|
| 174 |
case 3: |
| 175 |
w |= ((uint64_t)in[2]) << 16; |
| 176 |
|
| 177 |
case 2: |
| 178 |
w |= ((uint64_t)in[1]) << 8; |
| 179 |
|
| 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 |
|
| 203 |
memset(buffer, 0, 16); |
| 204 |
|
| 205 |
for (i = 0; i < (key_len > 16 ? 16 : key_len); i++) |
| 206 |
buffer[i] = Byte_u(key,i); |
| 207 |
|
| 208 |
k0 = U8TO64_LE(buffer); |
| 209 |
k1 = U8TO64_LE(buffer + 8); |
| 210 |
|
| 211 |
|
| 212 |
h->v0 = 0x736f6d6570736575ULL ^ k0; |
| 213 |
h->v1 = 0x646f72616e646f6dULL ^ k1; |
| 214 |
h->v2 = 0x6c7967656e657261ULL ^ k0; |
| 215 |
h->v3 = 0x7465646279746573ULL ^ k1; |
| 216 |
|
| 217 |
|
| 218 |
return st; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 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 |
|
| 230 |
|
| 231 |
|
| 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 |
|
| 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 |
} |