Bitcoin ABC 0.30.5
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)
9#include <config/bitcoin-config.h>
10#endif
11
12#include <serialize.h>
13#include <span.h>
14#include <uint256.h>
15
16#include <cstdint>
17
18class Num3072 {
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,
43 "bad size for double_limb_t");
44 static_assert(sizeof(limb_t) * 8 == LIMB_SIZE, "LIMB_SIZE is incorrect");
45
46 // Hard coded values in MuHash3072 constructor and Finalize
47 static_assert(sizeof(limb_t) == 4 || sizeof(limb_t) == 8,
48 "bad size for limb_t");
49
50 void Multiply(const Num3072 &a);
51 void Divide(const Num3072 &a);
52 void SetToOne();
53 void Square();
54 void ToBytes(uint8_t (&out)[BYTE_SIZE]);
55
56 Num3072() { this->SetToOne(); };
57 Num3072(const uint8_t (&data)[BYTE_SIZE]);
58
60 for (auto &limb : obj.limbs) {
61 READWRITE(limb);
62 }
63 }
64};
65
97private:
100
102
103public:
104 /* The empty set. */
105 MuHash3072() noexcept {};
106
107 /* A singleton with variable sized data in it. */
108 explicit MuHash3072(Span<const uint8_t> in) noexcept;
109
110 /* Insert a single piece of data into the set. */
112
113 /* Remove a single piece of data from the set. */
115
116 /* Multiply (resulting in a hash for the union of the sets) */
117 MuHash3072 &operator*=(const MuHash3072 &mul) noexcept;
118
119 /* Divide (resulting in a hash for the difference of the sets) */
120 MuHash3072 &operator/=(const MuHash3072 &div) noexcept;
121
122 /* Finalize into a 32-byte hash. Does not change this object's value. */
123 void Finalize(uint256 &out) noexcept;
124
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:96
Num3072 ToNum3072(Span< const uint8_t > in)
Definition: muhash.cpp:338
Num3072 m_numerator
Definition: muhash.h:98
SERIALIZE_METHODS(MuHash3072, obj)
Definition: muhash.h:125
MuHash3072 & Remove(Span< const uint8_t > in) noexcept
Definition: muhash.cpp:381
void Finalize(uint256 &out) noexcept
Definition: muhash.cpp:353
MuHash3072() noexcept
Definition: muhash.h:105
MuHash3072 & operator/=(const MuHash3072 &div) noexcept
Definition: muhash.cpp:370
MuHash3072 & Insert(Span< const uint8_t > in) noexcept
Definition: muhash.cpp:376
MuHash3072 & operator*=(const MuHash3072 &mul) noexcept
Definition: muhash.cpp:364
Num3072 m_denominator
Definition: muhash.h:99
Definition: muhash.h:18
Num3072 GetInverse() const
Definition: muhash.cpp:150
void Square()
Definition: muhash.cpp:237
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:130
void ToBytes(uint8_t(&out)[BYTE_SIZE])
Definition: muhash.cpp:328
limb_t limbs[LIMBS]
Definition: muhash.h:38
void SetToOne()
Definition: muhash.cpp:291
static constexpr int LIMB_SIZE
Definition: muhash.h:36
void Divide(const Num3072 &a)
Definition: muhash.cpp:298
void FullReduce()
Definition: muhash.cpp:142
uint64_t double_limb_t
Definition: muhash.h:33
SERIALIZE_METHODS(Num3072, obj)
Definition: muhash.h:59
uint32_t limb_t
Definition: muhash.h:34
Num3072()
Definition: muhash.h:56
void Multiply(const Num3072 &a)
Definition: muhash.cpp:190
256-bit opaque blob.
Definition: uint256.h:129
#define READWRITE(...)
Definition: serialize.h:166