Coverage Report

Created: 2025-07-18 06:13

/src/WasmEdge/lib/common/hash.cpp
Line
Count
Source (jump to first uncovered line)
1
#include "common/hash.h"
2
3
namespace {
4
5
48.0k
inline uint64_t mulMod(uint64_t A, uint64_t B, uint64_t M) noexcept {
6
48.0k
  uint64_t R = 0;
7
2.80M
  while (B) {
8
2.75M
    if (B & 1) {
9
1.37M
      uint64_t R2 = R + A;
10
1.37M
      if (R2 < R) {
11
117k
        R2 -= M;
12
117k
      }
13
1.37M
      R = R2 % M;
14
1.37M
    }
15
2.75M
    B >>= 1;
16
2.75M
    if (B) {
17
2.70M
      uint64_t A2 = A + A;
18
2.70M
      if (A2 < A) {
19
361k
        A2 -= M;
20
361k
      }
21
2.70M
      A = A2 % M;
22
2.70M
    }
23
2.75M
  }
24
48.0k
  return R;
25
48.0k
}
26
27
522
inline uint64_t powMod(uint64_t A, uint64_t B, uint64_t M) noexcept {
28
522
  uint64_t R = 1;
29
32.4k
  while (B) {
30
31.9k
    if (B & 1) {
31
16.1k
      R = mulMod(R, A, M);
32
16.1k
    }
33
31.9k
    B >>= 1;
34
31.9k
    if (B) {
35
31.3k
      A = mulMod(A, A, M);
36
31.3k
    }
37
31.9k
  }
38
522
  return R;
39
522
}
40
41
522
inline bool sprp(uint64_t N, uint64_t A) noexcept {
42
522
  uint64_t D = N - 1;
43
522
  uint8_t S = 0;
44
522
  while (!(D & 0xff)) {
45
0
    D >>= 8;
46
0
    S += 8;
47
0
  }
48
522
  if (!(D & 0xf)) {
49
63
    D >>= 4;
50
63
    S += 4;
51
63
  }
52
522
  if (!(D & 0x3)) {
53
246
    D >>= 2;
54
246
    S += 2;
55
246
  }
56
522
  if (!(D & 0x1)) {
57
336
    D >>= 1;
58
336
    S += 1;
59
336
  }
60
522
  uint64_t B = powMod(A, D, N);
61
522
  if ((B == 1) || (B == (N - 1))) {
62
111
    return true;
63
111
  }
64
411
  uint8_t R;
65
800
  for (R = 1; R < S; R++) {
66
470
    B = mulMod(B, B, N);
67
470
    if (B <= 1) {
68
0
      return false;
69
0
    }
70
470
    if (B == (N - 1)) {
71
81
      return true;
72
81
    }
73
470
  }
74
330
  return false;
75
411
}
76
77
346
inline bool isPrime(uint64_t N) noexcept {
78
346
  if (N < 2 || !(N & 1)) {
79
0
    return false;
80
0
  }
81
346
  if (N < 4) {
82
0
    return true;
83
0
  }
84
346
  if (!sprp(N, 2)) {
85
330
    return false;
86
330
  }
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
15.2k
inline int popcount(uint64_t X) noexcept {
127
15.2k
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
128
15.2k
  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
15.2k
}
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
25.4k
    do {
158
25.4k
      Ok = true;
159
25.4k
      Secret[I] = 0;
160
229k
      for (size_t J = 0; J < 64; J += 8) {
161
203k
        Secret[I] |= static_cast<uint64_t>(C[Dist(WasmEdge::Hash::RandEngine)])
162
203k
                     << J;
163
203k
      }
164
25.4k
      if (Secret[I] % 2 == 0) {
165
12.6k
        Ok = false;
166
12.6k
        continue;
167
12.6k
      }
168
15.5k
      for (size_t J = 0; J < I; J++) {
169
15.2k
        if (popcount(Secret[J] ^ Secret[I]) != 32) {
170
12.4k
          Ok = false;
171
12.4k
          break;
172
12.4k
        }
173
15.2k
      }
174
12.8k
      if (Ok && !isPrime(Secret[I]))
175
330
        Ok = false;
176
25.4k
    } while (!Ok);
177
16
  }
178
4
  return Secret;
179
4
}
180
181
#if WASMEDGE_ENDIAN_LITTLE_BYTE
182
3.33M
inline uint64_t read(WasmEdge::Span<const std::byte, 8> Data) noexcept {
183
3.33M
  uint64_t V;
184
3.33M
  std::memcpy(&V, Data.data(), 8);
185
3.33M
  return V;
186
3.33M
}
187
389k
inline uint64_t read(WasmEdge::Span<const std::byte, 4> Data) noexcept {
188
389k
  uint32_t V;
189
389k
  std::memcpy(&V, Data.data(), 4);
190
389k
  return V;
191
389k
}
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.37M
inline uint64_t read_small(WasmEdge::Span<const std::byte> Data) noexcept {
228
1.37M
  return (static_cast<uint64_t>(Data[0]) << 56) |
229
1.37M
         (static_cast<uint64_t>(Data[Data.size() >> 1]) << 32) |
230
1.37M
         static_cast<uint64_t>(Data[Data.size() - 1]);
231
1.37M
}
232
233
static const std::array<uint64_t, 4> Secret = generate();
234
235
} // namespace
236
237
namespace WasmEdge::Hash {
238
239
1.50M
WASMEDGE_EXPORT uint64_t Hash::rapidHash(Span<const std::byte> Data) noexcept {
240
1.50M
  const auto Size = Data.size();
241
1.50M
  uint64_t Seed = Secret[3];
242
1.50M
  Seed ^= rapidMix(Seed ^ Secret[0], Secret[1]) ^ Size;
243
1.50M
  uint64_t A, B;
244
1.50M
  if (likely(Data.size() <= 16)) {
245
1.47M
    if (likely(Data.size() >= 4)) {
246
97.3k
      A = (read(Data.first<4>()) << 32) | read(Data.last<4>());
247
97.3k
      const uint64_t delta = ((Data.size() & 24) >> (Data.size() >> 3));
248
97.3k
      B = (read(Data.subspan(delta).first<4>()) << 32) |
249
97.3k
          read(Data.last(4 + delta).first<4>());
250
1.37M
    } else if (likely(Data.size() > 0)) {
251
1.37M
      A = read_small(Data);
252
1.37M
      B = 0;
253
1.37M
    } else {
254
57
      A = B = 0;
255
57
    }
256
1.47M
  } else {
257
31.8k
    if (unlikely(Data.size() > 48)) {
258
241
      uint64_t See1 = Seed, See2 = Seed;
259
266k
      while (likely(Data.size() >= 96)) {
260
266k
        Seed = rapidMix(read(Data.first<8>()) ^ Secret[0],
261
266k
                        read(Data.subspan<8>().first<8>()) ^ Seed);
262
266k
        See1 = rapidMix(read(Data.subspan<16>().first<8>()) ^ Secret[1],
263
266k
                        read(Data.subspan<24>().first<8>()) ^ See1);
264
266k
        See2 = rapidMix(read(Data.subspan<32>().first<8>()) ^ Secret[2],
265
266k
                        read(Data.subspan<40>().first<8>()) ^ See2);
266
266k
        Seed = rapidMix(read(Data.subspan<48>().first<8>()) ^ Secret[0],
267
266k
                        read(Data.subspan<56>().first<8>()) ^ Seed);
268
266k
        See1 = rapidMix(read(Data.subspan<64>().first<8>()) ^ Secret[1],
269
266k
                        read(Data.subspan<72>().first<8>()) ^ See1);
270
266k
        See2 = rapidMix(read(Data.subspan<80>().first<8>()) ^ Secret[2],
271
266k
                        read(Data.subspan<88>().first<8>()) ^ See2);
272
266k
        Data = Data.subspan<96>();
273
266k
      }
274
241
      if (unlikely(Data.size() >= 48)) {
275
102
        Seed = rapidMix(read(Data.first<8>()) ^ Secret[0],
276
102
                        read(Data.subspan<8>().first<8>()) ^ Seed);
277
102
        See1 = rapidMix(read(Data.subspan<16>().first<8>()) ^ Secret[1],
278
102
                        read(Data.subspan<24>().first<8>()) ^ See1);
279
102
        See2 = rapidMix(read(Data.subspan<32>().first<8>()) ^ Secret[2],
280
102
                        read(Data.subspan<40>().first<8>()) ^ See2);
281
102
        Data = Data.subspan<48>();
282
102
      }
283
284
241
      Seed ^= See1 ^ See2;
285
241
    }
286
31.8k
    if (Data.size() > 16) {
287
31.7k
      Seed = rapidMix(read(Data.first<8>()) ^ Secret[2],
288
31.7k
                      read(Data.subspan<8>().first<8>()) ^ Seed ^ Secret[1]);
289
31.7k
      if (Data.size() > 32)
290
2.49k
        Seed = rapidMix(read(Data.subspan<16>().first<8>()) ^ Secret[2],
291
2.49k
                        read(Data.subspan<24>().first<8>()) ^ Seed);
292
31.7k
    }
293
31.8k
    A = read(Data.last<16>().first<8>());
294
31.8k
    B = read(Data.last<8>());
295
31.8k
  }
296
1.50M
  A ^= Secret[1];
297
1.50M
  B ^= Seed;
298
1.50M
  rapidMum(A, B);
299
1.50M
  return rapidMix(A ^ Secret[0] ^ Size, B ^ Secret[1]);
300
1.50M
}
301
302
} // namespace WasmEdge::Hash