Bitcoin Core  21.99.0
P2P Digital Currency
muhash.h
Go to the documentation of this file.
1 // Copyright (c) 2017-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_CRYPTO_MUHASH_H
6 #define BITCOIN_CRYPTO_MUHASH_H
7 
8 #if defined(HAVE_CONFIG_H)
10 #endif
11 
12 #include <serialize.h>
13 #include <uint256.h>
14 
15 #include <stdint.h>
16 
17 class Num3072
18 {
19 private:
20  void FullReduce();
21  bool IsOverflow() const;
22  Num3072 GetInverse() const;
23 
24 public:
25 
26 #ifdef HAVE___INT128
27  typedef unsigned __int128 double_limb_t;
28  typedef uint64_t limb_t;
29  static constexpr int LIMBS = 48;
30  static constexpr int LIMB_SIZE = 64;
31 #else
32  typedef uint64_t double_limb_t;
33  typedef uint32_t limb_t;
34  static constexpr int LIMBS = 96;
35  static constexpr int LIMB_SIZE = 32;
36 #endif
37  limb_t limbs[LIMBS];
38 
39  // Sanity check for Num3072 constants
40  static_assert(LIMB_SIZE * LIMBS == 3072, "Num3072 isn't 3072 bits");
41  static_assert(sizeof(double_limb_t) == sizeof(limb_t) * 2, "bad size for double_limb_t");
42  static_assert(sizeof(limb_t) * 8 == LIMB_SIZE, "LIMB_SIZE is incorrect");
43 
44  // Hard coded values in MuHash3072 constructor and Finalize
45  static_assert(sizeof(limb_t) == 4 || sizeof(limb_t) == 8, "bad size for limb_t");
46 
47  void Multiply(const Num3072& a);
48  void Divide(const Num3072& a);
49  void SetToOne();
50  void Square();
51 
52  Num3072() { this->SetToOne(); };
53 
55  {
56  for (auto& limb : obj.limbs) {
57  READWRITE(limb);
58  }
59  }
60 };
61 
92 {
93 private:
94  static constexpr size_t BYTE_SIZE = 384;
95 
98 
99  Num3072 ToNum3072(Span<const unsigned char> in);
100 
101 public:
102  /* The empty set. */
103  MuHash3072() noexcept {};
104 
105  /* A singleton with variable sized data in it. */
106  explicit MuHash3072(Span<const unsigned char> in) noexcept;
107 
108  /* Insert a single piece of data into the set. */
109  MuHash3072& Insert(Span<const unsigned char> in) noexcept;
110 
111  /* Remove a single piece of data from the set. */
112  MuHash3072& Remove(Span<const unsigned char> in) noexcept;
113 
114  /* Multiply (resulting in a hash for the union of the sets) */
115  MuHash3072& operator*=(const MuHash3072& mul) noexcept;
116 
117  /* Divide (resulting in a hash for the difference of the sets) */
118  MuHash3072& operator/=(const MuHash3072& div) noexcept;
119 
120  /* Finalize into a 32-byte hash. Does not change this object's value. */
121  void Finalize(uint256& out) noexcept;
122 
124  {
125  READWRITE(obj.m_numerator);
126  READWRITE(obj.m_denominator);
127  }
128 };
129 
130 #endif // BITCOIN_CRYPTO_MUHASH_H
Definition: muhash.h:17
limb_t limbs[LIMBS]
Definition: muhash.h:37
uint64_t double_limb_t
Definition: muhash.h:32
uint32_t limb_t
Definition: muhash.h:33
Num3072()
Definition: muhash.h:52
bool IsOverflow() const
Indicates wether d is larger than the modulus.
Definition: muhash.cpp:127
SERIALIZE_METHODS(Num3072, obj)
Definition: muhash.h:54
Num3072 m_denominator
Definition: muhash.h:97
void Divide(const Num3072 &a)
Definition: muhash.cpp:262
static constexpr int LIMB_SIZE
Definition: muhash.h:35
static constexpr int LIMBS
Definition: muhash.h:34
SERIALIZE_METHODS(MuHash3072, obj)
Definition: muhash.h:123
Num3072 m_numerator
Definition: muhash.h:96
void Multiply(const Num3072 &a)
Definition: muhash.cpp:182
MuHash3072() noexcept
Definition: muhash.h:103
Num3072 GetInverse() const
Definition: muhash.cpp:145
256-bit opaque blob.
Definition: uint256.h:124
A class representing MuHash sets.
Definition: muhash.h:91
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
void SetToOne()
Definition: muhash.cpp:256
void Square()
Definition: muhash.cpp:219
#define READWRITE(...)
Definition: serialize.h:175
void FullReduce()
Definition: muhash.cpp:136