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
18{
19private:
20 void FullReduce();
21 bool IsOverflow() const;
22 Num3072 GetInverse() const;
23
24public:
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{
96private:
99
101
102public:
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
A class representing MuHash sets.
Definition: muhash.h:95
Num3072 ToNum3072(Span< const unsigned char > in)
Definition: muhash.cpp:298
Num3072 m_numerator
Definition: muhash.h:97
SERIALIZE_METHODS(MuHash3072, obj)
Definition: muhash.h:124
void Finalize(uint256 &out) noexcept
Definition: muhash.cpp:313
MuHash3072() noexcept
Definition: muhash.h:104
MuHash3072 & Remove(Span< const unsigned char > in) noexcept
Definition: muhash.cpp:343
MuHash3072 & operator/=(const MuHash3072 &div) noexcept
Definition: muhash.cpp:331
MuHash3072 & Insert(Span< const unsigned char > in) noexcept
Definition: muhash.cpp:338
MuHash3072 & operator*=(const MuHash3072 &mul) noexcept
Definition: muhash.cpp:324
Num3072 m_denominator
Definition: muhash.h:98
Definition: muhash.h:18
Num3072 GetInverse() const
Definition: muhash.cpp:144
void Square()
Definition: muhash.cpp:218
static constexpr int LIMBS
Definition: muhash.h:35
static constexpr size_t BYTE_SIZE
Definition: muhash.h:25
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:38
void SetToOne()
Definition: muhash.cpp:255
static constexpr int LIMB_SIZE
Definition: muhash.h:36
void Divide(const Num3072 &a)
Definition: muhash.cpp:261
void FullReduce()
Definition: muhash.cpp:135
uint64_t double_limb_t
Definition: muhash.h:33
SERIALIZE_METHODS(Num3072, obj)
Definition: muhash.h:57
uint32_t limb_t
Definition: muhash.h:34
Num3072()
Definition: muhash.h:54
void Multiply(const Num3072 &a)
Definition: muhash.cpp:181
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
256-bit opaque blob.
Definition: uint256.h:124
#define READWRITE(...)
Definition: serialize.h:147