Coverage Report

Created: 2025-07-11 06:21

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