Bitcoin Core  22.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  static constexpr size_t BYTE_SIZE = 384;
26 
27 #ifdef HAVE___INT128
28  typedef unsigned __int128 double_limb_t;
29  typedef uint64_t limb_t;
30  static constexpr int LIMBS = 48;
31  static constexpr int LIMB_SIZE = 64;
32 #else
33  typedef uint64_t double_limb_t;
34  typedef uint32_t limb_t;
35  static constexpr int LIMBS = 96;
36  static constexpr int LIMB_SIZE = 32;
37 #endif
39 
40  // Sanity check for Num3072 constants
41  static_assert(LIMB_SIZE * LIMBS == 3072, "Num3072 isn't 3072 bits");
42  static_assert(sizeof(double_limb_t) == sizeof(limb_t) * 2, "bad size for double_limb_t");
43  static_assert(sizeof(limb_t) * 8 == LIMB_SIZE, "LIMB_SIZE is incorrect");
44 
45  // Hard coded values in MuHash3072 constructor and Finalize
46  static_assert(sizeof(limb_t) == 4 || sizeof(limb_t) == 8, "bad size for limb_t");
47 
48  void Multiply(const Num3072& a);
49  void Divide(const Num3072& a);
50  void SetToOne();
51  void Square();
52  void ToBytes(unsigned char (&out)[BYTE_SIZE]);
53 
54  Num3072() { this->SetToOne(); };
55  Num3072(const unsigned char (&data)[BYTE_SIZE]);
56 
58  {
59  for (auto& limb : obj.limbs) {
60  READWRITE(limb);
61  }
62  }
63 };
64 
95 {
96 private:
99 
101 
102 public:
103  /* The empty set. */
104  MuHash3072() noexcept {};
105 
106  /* A singleton with variable sized data in it. */
107  explicit MuHash3072(Span<const unsigned char> in) noexcept;
108 
109  /* Insert a single piece of data into the set. */
111 
112  /* Remove a single piece of data from the set. */
114 
115  /* Multiply (resulting in a hash for the union of the sets) */
116  MuHash3072& operator*=(const MuHash3072& mul) noexcept;
117 
118  /* Divide (resulting in a hash for the difference of the sets) */
119  MuHash3072& operator/=(const MuHash3072& div) noexcept;
120 
121  /* Finalize into a 32-byte hash. Does not change this object's value. */
122  void Finalize(uint256& out) noexcept;
123 
125  {
126  READWRITE(obj.m_numerator);
127  READWRITE(obj.m_denominator);
128  }
129 };
130 
131 #endif // BITCOIN_CRYPTO_MUHASH_H
MuHash3072::MuHash3072
MuHash3072() noexcept
Definition: muhash.h:104
Num3072::GetInverse
Num3072 GetInverse() const
Definition: muhash.cpp:144
MuHash3072::operator*=
MuHash3072 & operator*=(const MuHash3072 &mul) noexcept
Definition: muhash.cpp:324
Num3072::Num3072
Num3072()
Definition: muhash.h:54
uint256.h
MuHash3072::Insert
MuHash3072 & Insert(Span< const unsigned char > in) noexcept
Definition: muhash.cpp:338
Num3072::IsOverflow
bool IsOverflow() const
Indicates whether d is larger than the modulus.
Definition: muhash.cpp:126
bitcoin-config.h
Num3072
Definition: muhash.h:17
Num3072::LIMB_SIZE
static constexpr int LIMB_SIZE
Definition: muhash.h:36
Num3072::BYTE_SIZE
static constexpr size_t BYTE_SIZE
Definition: muhash.h:25
MuHash3072::Remove
MuHash3072 & Remove(Span< const unsigned char > in) noexcept
Definition: muhash.cpp:343
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
Num3072::SERIALIZE_METHODS
SERIALIZE_METHODS(Num3072, obj)
Definition: muhash.h:57
Num3072::SetToOne
void SetToOne()
Definition: muhash.cpp:255
uint256
256-bit opaque blob.
Definition: uint256.h:124
READWRITE
#define READWRITE(...)
Definition: serialize.h:147
MuHash3072::Finalize
void Finalize(uint256 &out) noexcept
Definition: muhash.cpp:313
MuHash3072::m_numerator
Num3072 m_numerator
Definition: muhash.h:97
MuHash3072::SERIALIZE_METHODS
SERIALIZE_METHODS(MuHash3072, obj)
Definition: muhash.h:124
Num3072::limbs
limb_t limbs[LIMBS]
Definition: muhash.h:38
MuHash3072::operator/=
MuHash3072 & operator/=(const MuHash3072 &div) noexcept
Definition: muhash.cpp:331
Num3072::limb_t
uint32_t limb_t
Definition: muhash.h:34
Num3072::Square
void Square()
Definition: muhash.cpp:218
MuHash3072::ToNum3072
Num3072 ToNum3072(Span< const unsigned char > in)
Definition: muhash.cpp:298
Num3072::FullReduce
void FullReduce()
Definition: muhash.cpp:135
Num3072::LIMBS
static constexpr int LIMBS
Definition: muhash.h:35
Num3072::ToBytes
void ToBytes(unsigned char(&out)[BYTE_SIZE])
Definition: muhash.cpp:288
serialize.h
Num3072::Multiply
void Multiply(const Num3072 &a)
Definition: muhash.cpp:181
MuHash3072::m_denominator
Num3072 m_denominator
Definition: muhash.h:98
Num3072::Divide
void Divide(const Num3072 &a)
Definition: muhash.cpp:261
MuHash3072
A class representing MuHash sets.
Definition: muhash.h:94
Num3072::double_limb_t
uint64_t double_limb_t
Definition: muhash.h:33