Bitcoin Core 28.99.0
P2P Digital Currency
muhash.h
Go to the documentation of this file.
1// Copyright (c) 2017-2021 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#include <serialize.h>
9#include <uint256.h>
10
11#include <stdint.h>
12
14{
15private:
16 void FullReduce();
17 bool IsOverflow() const;
18 Num3072 GetInverse() const;
19
20public:
21 static constexpr size_t BYTE_SIZE = 384;
22
23#ifdef __SIZEOF_INT128__
24 typedef unsigned __int128 double_limb_t;
25 typedef uint64_t limb_t;
26 static constexpr int LIMBS = 48;
27 static constexpr int LIMB_SIZE = 64;
28#else
29 typedef uint64_t double_limb_t;
30 typedef uint32_t limb_t;
31 static constexpr int LIMBS = 96;
32 static constexpr int LIMB_SIZE = 32;
33#endif
35
36 // Sanity check for Num3072 constants
37 static_assert(LIMB_SIZE * LIMBS == 3072, "Num3072 isn't 3072 bits");
38 static_assert(sizeof(double_limb_t) == sizeof(limb_t) * 2, "bad size for double_limb_t");
39 static_assert(sizeof(limb_t) * 8 == LIMB_SIZE, "LIMB_SIZE is incorrect");
40
41 // Hard coded values in MuHash3072 constructor and Finalize
42 static_assert(sizeof(limb_t) == 4 || sizeof(limb_t) == 8, "bad size for limb_t");
43
44 void Multiply(const Num3072& a);
45 void Divide(const Num3072& a);
46 void SetToOne();
47 void Square();
48 void ToBytes(unsigned char (&out)[BYTE_SIZE]);
49
50 Num3072() { this->SetToOne(); };
51 Num3072(const unsigned char (&data)[BYTE_SIZE]);
52
54 {
55 for (auto& limb : obj.limbs) {
56 READWRITE(limb);
57 }
58 }
59};
60
91{
92private:
95
97
98public:
99 /* The empty set. */
100 MuHash3072() noexcept = default;
101
102 /* A singleton with variable sized data in it. */
103 explicit MuHash3072(Span<const unsigned char> in) noexcept;
104
105 /* Insert a single piece of data into the set. */
106 MuHash3072& Insert(Span<const unsigned char> in) noexcept;
107
108 /* Remove a single piece of data from the set. */
109 MuHash3072& Remove(Span<const unsigned char> in) noexcept;
110
111 /* Multiply (resulting in a hash for the union of the sets) */
112 MuHash3072& operator*=(const MuHash3072& mul) noexcept;
113
114 /* Divide (resulting in a hash for the difference of the sets) */
115 MuHash3072& operator/=(const MuHash3072& div) noexcept;
116
117 /* Finalize into a 32-byte hash. Does not change this object's value. */
118 void Finalize(uint256& out) noexcept;
119
121 {
122 READWRITE(obj.m_numerator);
123 READWRITE(obj.m_denominator);
124 }
125};
126
127#endif // BITCOIN_CRYPTO_MUHASH_H
A class representing MuHash sets.
Definition: muhash.h:91
Num3072 ToNum3072(Span< const unsigned char > in)
Definition: muhash.cpp:298
Num3072 m_numerator
Definition: muhash.h:93
SERIALIZE_METHODS(MuHash3072, obj)
Definition: muhash.h:120
MuHash3072() noexcept=default
void Finalize(uint256 &out) noexcept
Definition: muhash.cpp:314
MuHash3072 & Remove(Span< const unsigned char > in) noexcept
Definition: muhash.cpp:344
MuHash3072 & Insert(Span< const unsigned char > in) noexcept
Definition: muhash.cpp:339
Num3072 m_denominator
Definition: muhash.h:94
Definition: muhash.h:14
Num3072 GetInverse() const
Definition: muhash.cpp:144
void Square()
Definition: muhash.cpp:218
static constexpr int LIMBS
Definition: muhash.h:31
static constexpr size_t BYTE_SIZE
Definition: muhash.h:21
bool IsOverflow() const
Indicates whether d is larger than the modulus.
Definition: muhash.cpp:126
void ToBytes(unsigned char(&out)[BYTE_SIZE])
Definition: muhash.cpp:288
limb_t limbs[LIMBS]
Definition: muhash.h:34
void SetToOne()
Definition: muhash.cpp:255
static constexpr int LIMB_SIZE
Definition: muhash.h:32
void Divide(const Num3072 &a)
Definition: muhash.cpp:261
void FullReduce()
Definition: muhash.cpp:135
uint64_t double_limb_t
Definition: muhash.h:29
SERIALIZE_METHODS(Num3072, obj)
Definition: muhash.h:53
uint32_t limb_t
Definition: muhash.h:30
Num3072()
Definition: muhash.h:50
void Multiply(const Num3072 &a)
Definition: muhash.cpp:181
256-bit opaque blob.
Definition: uint256.h:190
#define READWRITE(...)
Definition: serialize.h:156