code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(125 lines)
1 // This file holds the internals for the SipHash implementation. For a few
2 // cases, we want to include this for incrementally computing hashes.
3 // Otherwise, it suffices to just use the siphash24() function from siphash.h
5 #pragma once
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <string.h>
11 #include "siphash.h"
13 /* <MIT License>
14 Copyright (c) 2013 Marek Majkowski <marek@popcount.org>
15 Copyright (c) 2018 Samantha McVey <samantham@posteo.net>
16 Copyright (c) 2024 Bruce Hill <bruce@bruce-hill.com>
18 Permission is hereby granted, free of charge, to any person obtaining a copy
19 of this software and associated documentation files (the "Software"), to deal
20 in the Software without restriction, including without limitation the rights
21 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22 copies of the Software, and to permit persons to whom the Software is
23 furnished to do so, subject to the following conditions:
25 The above copyright notice and this permission notice shall be included in
26 all copies or substantial portions of the Software.
28 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 THE SOFTWARE.
35 </MIT License>
37 Original location:
38 https://github.com/majek/csiphash/
40 Original solution inspired by code from:
41 Samuel Neves (supercop/crypto_auth/siphash24/little)
42 djb (supercop/crypto_auth/siphash24/little2)
43 Jean-Philippe Aumasson (https://131002.net/siphash/siphash24.c)
45 Extensive modifications for MoarVM by Samantha McVey
47 Further modifications for Tomo by Bruce Hill
48 */
49 struct siphash {
50 uint64_t v0;
51 uint64_t v1;
52 uint64_t v2;
53 uint64_t v3;
54 uint64_t b;
55 };
56 typedef struct siphash siphash;
57 #define ROTATE(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
59 #define HALF_ROUND(a, b, c, d, s, t) \
60 a += b; \
61 c += d; \
62 b = ROTATE(b, s) ^ a; \
63 d = ROTATE(d, t) ^ c; \
64 a = ROTATE(a, 32);
66 #define DOUBLE_ROUND(v0, v1, v2, v3) \
67 HALF_ROUND(v0, v1, v2, v3, 13, 16); \
68 HALF_ROUND(v2, v1, v0, v3, 17, 21); \
69 HALF_ROUND(v0, v1, v2, v3, 13, 16); \
70 HALF_ROUND(v2, v1, v0, v3, 17, 21);
72 MACROLIKE void siphashinit(siphash *sh, size_t src_sz) {
73 const uint64_t k0 = TOMO_HASH_KEY[0];
74 const uint64_t k1 = TOMO_HASH_KEY[1];
75 sh->b = (uint64_t)src_sz << 56;
76 sh->v0 = k0 ^ 0x736f6d6570736575ULL;
77 sh->v1 = k1 ^ 0x646f72616e646f6dULL;
78 sh->v2 = k0 ^ 0x6c7967656e657261ULL;
79 sh->v3 = k1 ^ 0x7465646279746573ULL;
81 MACROLIKE void siphashadd64bits(siphash *sh, const uint64_t in) {
82 const uint64_t mi = in;
83 sh->v3 ^= mi;
84 DOUBLE_ROUND(sh->v0, sh->v1, sh->v2, sh->v3);
85 sh->v0 ^= mi;
87 MACROLIKE uint64_t siphashfinish_last_part(siphash *sh, uint64_t t) {
88 sh->b |= t;
89 sh->v3 ^= sh->b;
90 DOUBLE_ROUND(sh->v0, sh->v1, sh->v2, sh->v3);
91 sh->v0 ^= sh->b;
92 sh->v2 ^= 0xff;
93 DOUBLE_ROUND(sh->v0, sh->v1, sh->v2, sh->v3);
94 DOUBLE_ROUND(sh->v0, sh->v1, sh->v2, sh->v3);
95 return (sh->v0 ^ sh->v1) ^ (sh->v2 ^ sh->v3);
97 /* This union helps us avoid doing weird things with pointers that can cause old
98 * compilers like GCC 4 to generate bad code. In addition it is nicely more C
99 * standards compliant to keep type punning to a minimum. */
100 union SipHash64_union {
101 uint64_t u64;
102 uint32_t u32;
103 uint8_t u8[8];
105 MACROLIKE uint64_t siphashfinish(siphash *sh, const uint8_t *src, size_t src_sz) {
106 union SipHash64_union t = {0};
107 switch (src_sz) {
108 /* Falls through */
109 case 7: t.u8[6] = src[6];
110 /* Falls through */
111 case 6: t.u8[5] = src[5];
112 /* Falls through */
113 case 5: t.u8[4] = src[4];
114 /* Falls through */
115 case 4: t.u8[3] = src[3];
116 /* Falls through */
117 case 3: t.u8[2] = src[2];
118 /* Falls through */
119 case 2: t.u8[1] = src[1];
120 /* Falls through */
121 case 1: t.u8[0] = src[0];
122 default: break;
124 return siphashfinish_last_part(sh, t.u64);