Bitcoin ABC 0.32.4
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 signed __int128 signed_double_limb_t;
30 typedef uint64_t limb_t;
31 typedef int64_t signed_limb_t;
32 static constexpr int LIMBS = 48;
33 static constexpr int SIGNED_LIMBS = 50;
34 static constexpr int LIMB_SIZE = 64;
35 static constexpr int SIGNED_LIMB_SIZE = 62;
36#else
37 typedef uint64_t double_limb_t;
38 typedef int64_t signed_double_limb_t;
39 typedef uint32_t limb_t;
40 typedef int32_t signed_limb_t;
41 static constexpr int LIMBS = 96;
42 static constexpr int SIGNED_LIMBS = 103;
43 static constexpr int LIMB_SIZE = 32;
44 static constexpr int SIGNED_LIMB_SIZE = 30;
45#endif
47
48 // Sanity check for Num3072 constants
49 static_assert(LIMB_SIZE * LIMBS == 3072, "Num3072 isn't 3072 bits");
50 static_assert(sizeof(double_limb_t) == sizeof(limb_t) * 2,
51 "bad size for double_limb_t");
52 static_assert(sizeof(limb_t) * 8 == LIMB_SIZE, "LIMB_SIZE is incorrect");
53 static_assert(SIGNED_LIMB_SIZE * SIGNED_LIMBS > 3072,
54 "SIGNED_LIMBS * SIGNED_LIMB_SIZE is too small");
55 static_assert(3072 / SIGNED_LIMB_SIZE == SIGNED_LIMBS - 1,
56 "Bit 3072 must land in top signed limb");
57
58 // Hard coded values in MuHash3072 constructor and Finalize
59 static_assert(sizeof(limb_t) == 4 || sizeof(limb_t) == 8,
60 "bad size for limb_t");
61
62 void Multiply(const Num3072 &a);
63 void Divide(const Num3072 &a);
64 void SetToOne();
65 void ToBytes(uint8_t (&out)[BYTE_SIZE]);
66
67 Num3072() { this->SetToOne(); };
68 Num3072(const uint8_t (&data)[BYTE_SIZE]);
69
71 for (auto &limb : obj.limbs) {
72 READWRITE(limb);
73 }
74 }
75};
76
108private:
111
113
114public:
115 /* The empty set. */
116 MuHash3072() noexcept {};
117
118 /* A singleton with variable sized data in it. */
119 explicit MuHash3072(Span<const uint8_t> in) noexcept;
120
121 /* Insert a single piece of data into the set. */
123
124 /* Remove a single piece of data from the set. */
126
127 /* Multiply (resulting in a hash for the union of the sets) */
128 MuHash3072 &operator*=(const MuHash3072 &mul) noexcept;
129
130 /* Divide (resulting in a hash for the difference of the sets) */
131 MuHash3072 &operator/=(const MuHash3072 &div) noexcept;
132
133 /* Finalize into a 32-byte hash. Does not change this object's value. */
134 void Finalize(uint256 &out) noexcept;
135
137 READWRITE(obj.m_numerator);
138 READWRITE(obj.m_denominator);
139 }
140};
141
142#endif // BITCOIN_CRYPTO_MUHASH_H
A class representing MuHash sets.
Definition: muhash.h:107
Num3072 ToNum3072(Span< const uint8_t > in)
Definition: muhash.cpp:667
Num3072 m_numerator
Definition: muhash.h:109
SERIALIZE_METHODS(MuHash3072, obj)
Definition: muhash.h:136
MuHash3072 & Remove(Span< const uint8_t > in) noexcept
Definition: muhash.cpp:711
void Finalize(uint256 &out) noexcept
Definition: muhash.cpp:683
MuHash3072() noexcept
Definition: muhash.h:116
MuHash3072 & operator/=(const MuHash3072 &div) noexcept
Definition: muhash.cpp:700
MuHash3072 & Insert(Span< const uint8_t > in) noexcept
Definition: muhash.cpp:706
MuHash3072 & operator*=(const MuHash3072 &mul) noexcept
Definition: muhash.cpp:694
Num3072 m_denominator
Definition: muhash.h:110
Definition: muhash.h:18
Num3072 GetInverse() const
Definition: muhash.cpp:494
static constexpr int LIMBS
Definition: muhash.h:41
int64_t signed_double_limb_t
Definition: muhash.h:38
int32_t signed_limb_t
Definition: muhash.h:40
static constexpr size_t BYTE_SIZE
Definition: muhash.h:25
bool IsOverflow() const
Indicates whether d is larger than the modulus.
Definition: muhash.cpp:119
void ToBytes(uint8_t(&out)[BYTE_SIZE])
Definition: muhash.cpp:657
limb_t limbs[LIMBS]
Definition: muhash.h:46
static constexpr int SIGNED_LIMBS
Definition: muhash.h:42
void SetToOne()
Definition: muhash.cpp:620
static constexpr int LIMB_SIZE
Definition: muhash.h:43
void Divide(const Num3072 &a)
Definition: muhash.cpp:627
static constexpr int SIGNED_LIMB_SIZE
Definition: muhash.h:44
void FullReduce()
Definition: muhash.cpp:131
uint64_t double_limb_t
Definition: muhash.h:37
SERIALIZE_METHODS(Num3072, obj)
Definition: muhash.h:70
uint32_t limb_t
Definition: muhash.h:39
Num3072()
Definition: muhash.h:67
void Multiply(const Num3072 &a)
Definition: muhash.cpp:573
256-bit opaque blob.
Definition: uint256.h:129
#define READWRITE(...)
Definition: serialize.h:168