From 5ee185a4896e43c67b6d299becfa616da78fb9f4 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 21 Mar 2025 21:48:53 -0400 Subject: Move stdlib into src/ --- src/stdlib/siphash.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/stdlib/siphash.c (limited to 'src/stdlib/siphash.c') diff --git a/src/stdlib/siphash.c b/src/stdlib/siphash.c new file mode 100644 index 00000000..44e8b6eb --- /dev/null +++ b/src/stdlib/siphash.c @@ -0,0 +1,79 @@ +#include +#include +#include + +#include "siphash.h" +#include "util.h" + +public uint64_t TOMO_HASH_KEY[2] = {23, 42}; // Randomized in tomo_init() + +/* + Copyright (c) 2013 Marek Majkowski + Copyright (c) 2018 Samantha McVey + Copyright (c) 2024 Bruce Hill + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + + + Original location: + https://github.com/majek/csiphash/ + + Original solution inspired by code from: + Samuel Neves (supercop/crypto_auth/siphash24/little) + djb (supercop/crypto_auth/siphash24/little2) + Jean-Philippe Aumasson (https://131002.net/siphash/siphash24.c) + + Extensive modifications for MoarVM by Samantha McVey + + Further modifications for Tomo by Bruce Hill +*/ + +#include "siphash-internals.h" + +PUREFUNC public uint64_t siphash24(const uint8_t *src, size_t src_sz) { + siphash sh; + if ((uint64_t)src % __alignof__(uint64_t) == 0) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" + const uint64_t *in = (uint64_t*)src; + /* Find largest src_sz evenly divisible by 8 bytes. */ + const ptrdiff_t src_sz_nearest_8bits = ((ptrdiff_t)src_sz >> 3) << 3; + const uint64_t *goal = (uint64_t*)(src + src_sz_nearest_8bits); +#pragma GCC diagnostic pop + siphashinit(&sh, src_sz); + src_sz -= (size_t)src_sz_nearest_8bits; + while (in < goal) { + siphashadd64bits(&sh, *in); + in++; + } + return siphashfinish(&sh, (uint8_t *)in, src_sz); + } else { + const uint8_t *in = src; + siphashinit(&sh, src_sz); + while (src_sz >= 8) { + uint64_t in_64; + memcpy(&in_64, in, sizeof(uint64_t)); + siphashadd64bits(&sh, in_64); + in += 8; src_sz -= 8; + } + return siphashfinish(&sh, (uint8_t *)in, src_sz); + } +} + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 -- cgit v1.2.3