code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(85 lines)
1 // This file has an implementation of the SipHash hashing function.
3 #include <stddef.h>
4 #include <stdint.h>
5 #include <string.h>
7 #include "siphash.h"
8 #include "util.h"
10 public
11 uint64_t TOMO_HASH_KEY[2] = {23, 42}; // Randomized in tomo_init()
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 */
50 #include "siphash-internals.h"
52 PUREFUNC public uint64_t siphash24(const uint8_t *src, size_t src_sz) {
53 siphash sh;
54 if ((uint64_t)src % __alignof__(uint64_t) == 0) {
55 #ifdef __GNUC__
56 #pragma GCC diagnostic push
57 #pragma GCC diagnostic ignored "-Wcast-align"
58 #endif
59 const uint64_t *in = (uint64_t *)src;
60 /* Find largest src_sz evenly divisible by 8 bytes. */
61 const ptrdiff_t src_sz_nearest_8bits = ((ptrdiff_t)src_sz >> 3) << 3;
62 const uint64_t *goal = (uint64_t *)(src + src_sz_nearest_8bits);
63 #ifdef __GNUC__
64 #pragma GCC diagnostic pop
65 #endif
66 siphashinit(&sh, src_sz);
67 src_sz -= (size_t)src_sz_nearest_8bits;
68 while (in < goal) {
69 siphashadd64bits(&sh, *in);
70 in++;
72 return siphashfinish(&sh, (uint8_t *)in, src_sz);
73 } else {
74 const uint8_t *in = src;
75 siphashinit(&sh, src_sz);
76 while (src_sz >= 8) {
77 uint64_t in_64;
78 memcpy(&in_64, in, sizeof(uint64_t));
79 siphashadd64bits(&sh, in_64);
80 in += 8;
81 src_sz -= 8;
83 return siphashfinish(&sh, (uint8_t *)in, src_sz);