Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/classfile/test_AltHashing.cpp
41145 views
1
/*
2
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
#include "precompiled.hpp"
24
#include "classfile/altHashing.hpp"
25
#include "utilities/debug.hpp"
26
#include "utilities/formatBuffer.hpp"
27
#include "unittest.hpp"
28
29
class AltHashingTest : public ::testing::Test {
30
31
public:
32
33
static void testHalfsiphash_32_ByteArray() {
34
const int factor = 4;
35
36
uint8_t vector[256];
37
uint8_t hashes[factor * 256];
38
39
for (int i = 0; i < 256; i++) {
40
vector[i] = (uint8_t) i;
41
}
42
43
// Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}
44
for (int i = 0; i < 256; i++) {
45
uint32_t hash = AltHashing::halfsiphash_32(256 - i, vector, i);
46
hashes[i * factor] = (uint8_t) hash;
47
hashes[i * factor + 1] = (uint8_t)(hash >> 8);
48
hashes[i * factor + 2] = (uint8_t)(hash >> 16);
49
hashes[i * factor + 3] = (uint8_t)(hash >> 24);
50
}
51
52
// hash to get const result.
53
uint32_t final_hash = AltHashing::halfsiphash_32(0, hashes, factor*256);
54
55
// Value found using reference implementation for the hashes array.
56
//uint64_t k = 0; // seed
57
//uint32_t reference;
58
//halfsiphash((const uint8_t*)hashes, factor*256, (const uint8_t *)&k, (uint8_t*)&reference, 4);
59
//printf("0x%x", reference);
60
61
static const uint32_t HALFSIPHASH_32_BYTE_CHECK_VALUE = 0xd2be7fd8;
62
63
ASSERT_EQ(HALFSIPHASH_32_BYTE_CHECK_VALUE, final_hash) <<
64
err_msg(
65
"Calculated hash result not as expected. Expected " UINT32_FORMAT " got " UINT32_FORMAT,
66
HALFSIPHASH_32_BYTE_CHECK_VALUE,
67
final_hash);
68
}
69
70
static void testHalfsiphash_32_CharArray() {
71
const int factor = 2;
72
73
uint16_t vector[256];
74
uint16_t hashes[factor * 256];
75
76
for (int i = 0; i < 256; i++) {
77
vector[i] = (uint16_t) i;
78
}
79
80
// Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}
81
for (int i = 0; i < 256; i++) {
82
uint32_t hash = AltHashing::halfsiphash_32(256 - i, vector, i);
83
hashes[i * factor] = (uint16_t) hash;
84
hashes[i * factor + 1] = (uint16_t)(hash >> 16);
85
}
86
87
// hash to get const result.
88
uint32_t final_hash = AltHashing::halfsiphash_32(0, hashes, factor*256);
89
90
// Value found using reference implementation for the hashes array.
91
//uint64_t k = 0; // seed
92
//uint32_t reference;
93
//halfsiphash((const uint8_t*)hashes, 2*factor*256, (const uint8_t *)&k, (uint8_t*)&reference, 4);
94
//printf("0x%x", reference);
95
96
static const uint32_t HALFSIPHASH_32_CHAR_CHECK_VALUE = 0x428bf8a5;
97
98
ASSERT_EQ(HALFSIPHASH_32_CHAR_CHECK_VALUE, final_hash) <<
99
err_msg(
100
"Calculated hash result not as expected. Expected " UINT32_FORMAT " got " UINT32_FORMAT,
101
HALFSIPHASH_32_CHAR_CHECK_VALUE,
102
final_hash);
103
}
104
105
// Test against sample hashes published with the reference implementation:
106
// https://github.com/veorq/SipHash
107
static void testHalfsiphash_64_FromReference() {
108
109
const uint64_t seed = 0x0706050403020100;
110
const uint64_t results[16] = {
111
0xc83cb8b9591f8d21, 0xa12ee55b178ae7d5,
112
0x8c85e4bc20e8feed, 0x99c7f5ae9f1fc77b,
113
0xb5f37b5fd2aa3673, 0xdba7ee6f0a2bf51b,
114
0xf1a63fae45107470, 0xb516001efb5f922d,
115
0x6c6211d8469d7028, 0xdc7642ec407ad686,
116
0x4caec8671cc8385b, 0x5ab1dc27adf3301e,
117
0x3e3ea94bc0a8eaa9, 0xe150f598795a4402,
118
0x1d5ff142f992a4a1, 0x60e426bf902876d6
119
};
120
uint32_t vector[16];
121
122
for (int i = 0; i < 16; i++)
123
vector[i] = 0x03020100 + i * 0x04040404;
124
125
for (int i = 0; i < 16; i++) {
126
uint64_t hash = AltHashing::halfsiphash_64(seed, vector, i);
127
ASSERT_EQ(results[i], hash) <<
128
err_msg(
129
"Calculated hash result not as expected. Round %d: "
130
"Expected " UINT64_FORMAT_X " got " UINT64_FORMAT_X "\n",
131
i,
132
results[i],
133
hash);
134
}
135
}
136
};
137
138
TEST_F(AltHashingTest, halfsiphash_test_ByteArray) {
139
AltHashingTest::testHalfsiphash_32_ByteArray();
140
}
141
TEST_F(AltHashingTest, halfsiphash_test_CharArray) {
142
AltHashingTest::testHalfsiphash_32_CharArray();
143
}
144
TEST_F(AltHashingTest, halfsiphash_test_FromReference) {
145
AltHashingTest::testHalfsiphash_64_FromReference();
146
}
147
148