LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
test_hash.c
浏览该文件的文档.
1/**
2 * Copyright (C) 2026 HJimmyK (Jericho Knox)
3 *
4 * This file is part of LMMP.
5 *
6 * LMMP is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <https://www.gnu.org/licenses/>.
19 *
20 * SPDX-License-Identifier: LGPL-3.0-or-later
21 */
22
23#include "lmmp/lmmpn.h"
24#include "lmmp/mprand.h"
25#include "lmmp/secret.h"
26#include "lmmp/version.h"
27#include "cases.h"
29
30#include <string.h>
31
32TEST_CASE("hash/sanity", siphash_xxhash) {
33 mp_limb_t in[8];
34 u64 seed = 0x1234567890abcdefull;
35 for (size_t x_index = 0; x_index < ARRAY_SIZE(in); ++x_index) {
36 in[x_index] = xorshift64(&seed);
37 }
38
39 u64 key2[2] = {0x1111111111111111ull, 0x2222222222222222ull};
40 u64 key1[1] = {0x3333333333333333ull};
41
42 u64 h1 = lmmp_siphash24_(in, 8, key2);
43 u64 h2 = lmmp_siphash24_(in, 8, key2);
44 TEST_CHECK_EQ(h1, h2);
45
46 u64 h3 = lmmp_siphash24_(in, 8, NULL);
47 TEST_CHECK_MSG(h3 != h1, "siphash key changes hash");
48
49 u64 x1 = lmmp_xxhash_(in, 8, key1);
50 u64 x2 = lmmp_xxhash_(in, 8, key1);
51 TEST_CHECK_EQ(x1, x2);
52
53 u64 x3 = lmmp_xxhash_(in, 8, NULL);
54 TEST_CHECK_MSG(x3 != x1, "xxhash key changes hash");
55
56 // 空输入与 NULL key
57 TEST_CHECK_MSG(lmmp_siphash24_(NULL, 0, NULL) != 0, "siphash empty nonzero");
58 TEST_CHECK_MSG(lmmp_xxhash_(NULL, 0, NULL) != 0, "xxhash empty nonzero");
59
60 // 变更输入应改变哈希
61 in[0] ^= 1;
62 TEST_CHECK_MSG(lmmp_xxhash_(in, 8, key1) != x1, "xxhash input change");
63}
64
65void test_hash(void) {
66 TEST_RUN("hash/sanity", siphash_xxhash);
67}
uint64_t mp_limb_t
定义 lmmp.h:92
mp_limb_t u64
Copyright (C) 2026 HJimmyK (Jericho Knox)
#define ARRAY_SIZE(a)
Copyright (C) 2026 HJimmyK (Jericho Knox)
定义 perf.h:40
void test_hash(void)
Copyright (C) 2026 HJimmyK (Jericho Knox)
uint64_t lmmp_siphash24_(mp_srcptr in, mp_size_t inlen, srckey128_t key)
SipHash-2-4 函数(非标准处理任意字节流的 SipHash-2-4)
uint64_t lmmp_xxhash_(mp_srcptr in, mp_size_t inlen, srckey64_t key)
xxhash 函数(非标准处理任意字节流的 xxhash)
#define TEST_CHECK_MSG(cond, msg)
定义 test.h:38
#define TEST_CASE(category, name)
定义 test.h:35
#define TEST_CHECK_EQ(a, b)
定义 test.h:39
#define TEST_RUN(category, name)
定义 test.h:36