Coverage Report

Created: 2025-08-08 06:44

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