hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
compile-time_murmur2.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_MURMUR_2_HPP
7 #define IROHA_MURMUR_2_HPP
8 
9 namespace iroha::ct_hash {
10 
11  class Hasher {
12  static constexpr /* h */ uint32_t __init__(uint32_t len) {
13  return 0 ^ len;
14  }
15 
16  template <typename __T>
17  static constexpr uint32_t __load__(__T &data, uint32_t offset) {
18  return data[offset + 0] | (data[offset + 1] << 8)
19  | (data[offset + 2] << 16) | (data[offset + 3] << 24);
20  }
21 
22  static constexpr uint32_t __mul__(uint32_t val1, uint32_t val2) {
23  return val1 * val2;
24  }
25 
26  static constexpr uint32_t __sl__(uint32_t value, uint32_t count) {
27  return (value << count);
28  }
29 
30  static constexpr uint32_t __sr__(uint32_t value, uint32_t count) {
31  return (value >> count);
32  }
33 
34  static constexpr uint32_t __xor__(uint32_t h, uint32_t k) {
35  return h ^ k;
36  }
37 
38  static constexpr uint32_t __xor_with_sr__(uint32_t k, uint32_t r) {
39  return __xor__(k, __sr__(k, r));
40  }
41 
42  template <typename __Type>
43  static constexpr /* h */ uint32_t __proc__(__Type &data,
44  uint32_t len,
45  uint32_t offset,
46  uint32_t h,
47  uint32_t m,
48  uint32_t r) {
49  return len >= 4
50  ? __proc__(data,
51  len - 4,
52  offset + 4,
53  __xor__(__mul__(h, m),
54  __mul__(__xor_with_sr__(
55  __mul__(__load__(data, offset), m), r),
56  m)),
57  m,
58  r)
59  : len == 3
60  ? __proc__(data,
61  len - 1,
62  offset,
63  __xor__(h, __sl__(data[offset + 2], 16)),
64  m,
65  r)
66  : len == 2 ? __proc__(data,
67  len - 1,
68  offset,
69  __xor__(h, __sl__(data[offset + 1], 8)),
70  m,
71  r)
72  : len == 1
73  ? __proc__(data,
74  len - 1,
75  offset,
76  __xor__(h, data[offset]) * m,
77  m,
78  r)
79  : __xor__(__mul__(__xor_with_sr__(h, 13), m),
80  __sr__(__mul__(__xor_with_sr__(h, 13), m), 15));
81  }
82 
83  public:
84  template <typename __Type>
85  static constexpr uint32_t murmur2(__Type &data, uint32_t len) {
86  return __proc__(data, len, 0, __init__(len), 0x5bd1e995, 24);
87  }
88  };
89 
90 } // namespace iroha::ct_hash
91 
92 #ifndef CT_MURMUR2
93 #define CT_MURMUR2(x) \
94  ::iroha::ct_hash::Hasher::murmur2(x, (sizeof(x) / sizeof(x[0])) - 1)
95 #endif // CT_MURMUR2
96 
97 static_assert(CT_MURMUR2("Called the One Ring, or the Ruling Ring.")
98  == 1333588607);
99 static_assert(
100  CT_MURMUR2("Fashioned by Sauron a decade after the making of the Elven "
101  "rings in the fires of Mount Doom in Mordor and which")
102  == 1319897327);
103 static_assert(CT_MURMUR2("could only be destroyed in that same fire.")
104  == 702138758);
105 
106 #endif // IROHA_MURMUR_2_HPP
#define CT_MURMUR2(x)
Definition: compile-time_murmur2.hpp:93
static constexpr uint32_t murmur2(__Type &data, uint32_t len)
Definition: compile-time_murmur2.hpp:85
Definition: compile-time_murmur2.hpp:9
Definition: compile-time_murmur2.hpp:11