Coverage Report

Created: 2025-08-29 06:29

/src/WasmEdge/lib/common/hash.cpp
Line
Count
Source (jump to first uncovered line)
1
#include "common/hash.h"
2
#include "common/endian.h"
3
4
namespace {
5
6
37.8k
inline uint64_t mulMod(uint64_t A, uint64_t B, uint64_t M) noexcept {
7
37.8k
  uint64_t R = 0;
8
2.20M
  while (B) {
9
2.16M
    if (B & 1) {
10
1.08M
      uint64_t R2 = R + A;
11
1.08M
      if (R2 < R) {
12
93.4k
        R2 -= M;
13
93.4k
      }
14
1.08M
      R = R2 % M;
15
1.08M
    }
16
2.16M
    B >>= 1;
17
2.16M
    if (B) {
18
2.13M
      uint64_t A2 = A + A;
19
2.13M
      if (A2 < A) {
20
283k
        A2 -= M;
21
283k
      }
22
2.13M
      A = A2 % M;
23
2.13M
    }
24
2.16M
  }
25
37.8k
  return R;
26
37.8k
}
27
28
412
inline uint64_t powMod(uint64_t A, uint64_t B, uint64_t M) noexcept {
29
412
  uint64_t R = 1;
30
25.4k
  while (B) {
31
25.0k
    if (B & 1) {
32
12.7k
      R = mulMod(R, A, M);
33
12.7k
    }
34
25.0k
    B >>= 1;
35
25.0k
    if (B) {
36
24.6k
      A = mulMod(A, A, M);
37
24.6k
    }
38
25.0k
  }
39
412
  return R;
40
412
}
41
42
412
inline bool sprp(uint64_t N, uint64_t A) noexcept {
43
412
  uint64_t D = N - 1;
44
412
  uint8_t S = 0;
45
412
  while (!(D & 0xff)) {
46
0
    D >>= 8;
47
0
    S += 8;
48
0
  }
49
412
  if (!(D & 0xf)) {
50
94
    D >>= 4;
51
94
    S += 4;
52
94
  }
53
412
  if (!(D & 0x3)) {
54
190
    D >>= 2;
55
190
    S += 2;
56
190
  }
57
412
  if (!(D & 0x1)) {
58
209
    D >>= 1;
59
209
    S += 1;
60
209
  }
61
412
  uint64_t B = powMod(A, D, N);
62
412
  if ((B == 1) || (B == (N - 1))) {
63
84
    return true;
64
84
  }
65
328
  uint8_t R;
66
689
  for (R = 1; R < S; R++) {
67
469
    B = mulMod(B, B, N);
68
469
    if (B <= 1) {
69
0
      return false;
70
0
    }
71
469
    if (B == (N - 1)) {
72
108
      return true;
73
108
    }
74
469
  }
75
220
  return false;
76
328
}
77
78
236
inline bool isPrime(uint64_t N) noexcept {
79
236
  if (N < 2 || !(N & 1)) {
80
0
    return false;
81
0
  }
82
236
  if (N < 4) {
83
0
    return true;
84
0
  }
85
236
  if (!sprp(N, 2)) {
86
220
    return false;
87
220
  }
88
16
  if (N < 2047) {
89
0
    return true;
90
0
  }
91
16
  if (!sprp(N, 3)) {
92
0
    return false;
93
0
  }
94
16
  if (!sprp(N, 5)) {
95
0
    return false;
96
0
  }
97
16
  if (!sprp(N, 7)) {
98
0
    return false;
99
0
  }
100
16
  if (!sprp(N, 11)) {
101
0
    return false;
102
0
  }
103
16
  if (!sprp(N, 13)) {
104
0
    return false;
105
0
  }
106
16
  if (!sprp(N, 17)) {
107
0
    return false;
108
0
  }
109
16
  if (!sprp(N, 19)) {
110
0
    return false;
111
0
  }
112
16
  if (!sprp(N, 23)) {
113
0
    return false;
114
0
  }
115
16
  if (!sprp(N, 29)) {
116
0
    return false;
117
0
  }
118
16
  if (!sprp(N, 31)) {
119
0
    return false;
120
0
  }
121
16
  if (!sprp(N, 37)) {
122
0
    return false;
123
0
  }
124
16
  return true;
125
16
}
126
127
14.8k
inline int popcount(uint64_t X) noexcept {
128
14.8k
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
129
14.8k
  return __builtin_popcountll(X);
130
#elif defined(_MSC_VER) && defined(_WIN64)
131
#if defined(_M_X64)
132
  return static_cast<int>(_mm_popcnt_u64(X));
133
#else
134
  return static_cast<int>(_CountOneBits64(X));
135
#endif
136
#else
137
  X -= (X >> 1) & 0x5555555555555555;
138
  X = (X & 0x3333333333333333) + ((X >> 2) & 0x3333333333333333);
139
  X = (X + (X >> 4)) & 0x0f0f0f0f0f0f0f0f;
140
  X = (X * 0x0101010101010101) >> 56;
141
  return static_cast<int>(X);
142
#endif
143
14.8k
}
144
145
4
std::array<uint64_t, 4> generate() noexcept {
146
4
  std::array<uint64_t, 4> Secret;
147
4
  const std::array<uint8_t, 70> C = {
148
4
      0x0f, 0x17, 0x1b, 0x1d, 0x1e, 0x27, 0x2b, 0x2d, 0x2e, 0x33, 0x35, 0x36,
149
4
      0x39, 0x3a, 0x3c, 0x47, 0x4b, 0x4d, 0x4e, 0x53, 0x55, 0x56, 0x59, 0x5a,
150
4
      0x5c, 0x63, 0x65, 0x66, 0x69, 0x6a, 0x6c, 0x71, 0x72, 0x74, 0x78, 0x87,
151
4
      0x8b, 0x8d, 0x8e, 0x93, 0x95, 0x96, 0x99, 0x9a, 0x9c, 0xa3, 0xa5, 0xa6,
152
4
      0xa9, 0xaa, 0xac, 0xb1, 0xb2, 0xb4, 0xb8, 0xc3, 0xc5, 0xc6, 0xc9, 0xca,
153
4
      0xcc, 0xd1, 0xd2, 0xd4, 0xd8, 0xe1, 0xe2, 0xe4, 0xe8, 0xf0};
154
4
  std::uniform_int_distribution<uint64_t> Dist(
155
4
      UINT64_C(0), static_cast<uint64_t>(C.size() - 1));
156
20
  for (size_t I = 0; I < 4; I++) {
157
16
    bool Ok;
158
24.7k
    do {
159
24.7k
      Ok = true;
160
24.7k
      Secret[I] = 0;
161
222k
      for (size_t J = 0; J < 64; J += 8) {
162
197k
        Secret[I] |= static_cast<uint64_t>(C[Dist(WasmEdge::Hash::RandEngine)])
163
197k
                     << J;
164
197k
      }
165
24.7k
      if (Secret[I] % 2 == 0) {
166
12.3k
        Ok = false;
167
12.3k
        continue;
168
12.3k
      }
169
15.0k
      for (size_t J = 0; J < I; J++) {
170
14.8k
        if (popcount(Secret[J] ^ Secret[I]) != 32) {
171
12.1k
          Ok = false;
172
12.1k
          break;
173
12.1k
        }
174
14.8k
      }
175
12.3k
      if (Ok && !isPrime(Secret[I]))
176
220
        Ok = false;
177
24.7k
    } while (!Ok);
178
16
  }
179
4
  return Secret;
180
4
}
181
182
3.15M
inline uint64_t read(WasmEdge::Span<const std::byte, 8> Data) noexcept {
183
3.15M
  uint64_t V;
184
3.15M
  std::memcpy(&V, Data.data(), 8);
185
3.15M
  if constexpr (WasmEdge::Endian::native == WasmEdge::Endian::little) {
186
3.15M
    return V;
187
  } else {
188
    return WasmEdge::byteswap(V);
189
  }
190
3.15M
}
191
387k
inline uint64_t read(WasmEdge::Span<const std::byte, 4> Data) noexcept {
192
387k
  uint32_t V;
193
387k
  std::memcpy(&V, Data.data(), 4);
194
387k
  if constexpr (WasmEdge::Endian::native == WasmEdge::Endian::little) {
195
387k
    return V;
196
  } else {
197
    return WasmEdge::byteswap(V);
198
  }
199
387k
}
200
201
501k
inline uint64_t read_small(WasmEdge::Span<const std::byte> Data) noexcept {
202
501k
  return (static_cast<uint64_t>(Data[0]) << 56) |
203
501k
         (static_cast<uint64_t>(Data[Data.size() >> 1]) << 32) |
204
501k
         static_cast<uint64_t>(Data[Data.size() - 1]);
205
501k
}
206
207
static const std::array<uint64_t, 4> Secret = generate();
208
209
} // namespace
210
211
namespace WasmEdge::Hash {
212
213
630k
WASMEDGE_EXPORT uint64_t Hash::rapidHash(Span<const std::byte> Data) noexcept {
214
630k
  const auto Size = Data.size();
215
630k
  uint64_t Seed = Secret[3];
216
630k
  Seed ^= rapidMix(Seed ^ Secret[0], Secret[1]) ^ Size;
217
630k
  uint64_t A, B;
218
630k
  if (likely(Data.size() <= 16)) {
219
598k
    if (likely(Data.size() >= 4)) {
220
96.7k
      A = (read(Data.first<4>()) << 32) | read(Data.last<4>());
221
96.7k
      const uint64_t delta = ((Data.size() & 24) >> (Data.size() >> 3));
222
96.7k
      B = (read(Data.subspan(delta).first<4>()) << 32) |
223
96.7k
          read(Data.last(4 + delta).first<4>());
224
501k
    } else if (likely(Data.size() > 0)) {
225
501k
      A = read_small(Data);
226
501k
      B = 0;
227
501k
    } else {
228
56
      A = B = 0;
229
56
    }
230
598k
  } else {
231
32.2k
    if (unlikely(Data.size() > 48)) {
232
253
      uint64_t See1 = Seed, See2 = Seed;
233
251k
      while (likely(Data.size() >= 96)) {
234
251k
        Seed = rapidMix(read(Data.first<8>()) ^ Secret[0],
235
251k
                        read(Data.subspan<8>().first<8>()) ^ Seed);
236
251k
        See1 = rapidMix(read(Data.subspan<16>().first<8>()) ^ Secret[1],
237
251k
                        read(Data.subspan<24>().first<8>()) ^ See1);
238
251k
        See2 = rapidMix(read(Data.subspan<32>().first<8>()) ^ Secret[2],
239
251k
                        read(Data.subspan<40>().first<8>()) ^ See2);
240
251k
        Seed = rapidMix(read(Data.subspan<48>().first<8>()) ^ Secret[0],
241
251k
                        read(Data.subspan<56>().first<8>()) ^ Seed);
242
251k
        See1 = rapidMix(read(Data.subspan<64>().first<8>()) ^ Secret[1],
243
251k
                        read(Data.subspan<72>().first<8>()) ^ See1);
244
251k
        See2 = rapidMix(read(Data.subspan<80>().first<8>()) ^ Secret[2],
245
251k
                        read(Data.subspan<88>().first<8>()) ^ See2);
246
251k
        Data = Data.subspan<96>();
247
251k
      }
248
253
      if (unlikely(Data.size() >= 48)) {
249
102
        Seed = rapidMix(read(Data.first<8>()) ^ Secret[0],
250
102
                        read(Data.subspan<8>().first<8>()) ^ Seed);
251
102
        See1 = rapidMix(read(Data.subspan<16>().first<8>()) ^ Secret[1],
252
102
                        read(Data.subspan<24>().first<8>()) ^ See1);
253
102
        See2 = rapidMix(read(Data.subspan<32>().first<8>()) ^ Secret[2],
254
102
                        read(Data.subspan<40>().first<8>()) ^ See2);
255
102
        Data = Data.subspan<48>();
256
102
      }
257
258
253
      Seed ^= See1 ^ See2;
259
253
    }
260
32.2k
    if (Data.size() > 16) {
261
32.1k
      Seed = rapidMix(read(Data.first<8>()) ^ Secret[2],
262
32.1k
                      read(Data.subspan<8>().first<8>()) ^ Seed ^ Secret[1]);
263
32.1k
      if (Data.size() > 32)
264
2.59k
        Seed = rapidMix(read(Data.subspan<16>().first<8>()) ^ Secret[2],
265
2.59k
                        read(Data.subspan<24>().first<8>()) ^ Seed);
266
32.1k
    }
267
32.2k
    A = read(Data.last<16>().first<8>());
268
32.2k
    B = read(Data.last<8>());
269
32.2k
  }
270
630k
  A ^= Secret[1];
271
630k
  B ^= Seed;
272
630k
  rapidMum(A, B);
273
630k
  return rapidMix(A ^ Secret[0] ^ Size, B ^ Secret[1]);
274
630k
}
275
276
} // namespace WasmEdge::Hash