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 signed __int128 signed_double_limb_t;
26 typedef uint64_t limb_t;
27 typedef int64_t signed_limb_t;
28 static constexpr int LIMBS = 48;
29 static constexpr int SIGNED_LIMBS = 50;
30 static constexpr int LIMB_SIZE = 64;
31 static constexpr int SIGNED_LIMB_SIZE = 62;
32#else
33 typedef uint64_t double_limb_t;
34 typedef int64_t signed_double_limb_t;
35 typedef uint32_t limb_t;
36 typedef int32_t signed_limb_t;
37 static constexpr int LIMBS = 96;
38 static constexpr int SIGNED_LIMBS = 103;
39 static constexpr int LIMB_SIZE = 32;
40 static constexpr int SIGNED_LIMB_SIZE = 30;
41#endif
43
44 // Sanity check for Num3072 constants
45 static_assert(LIMB_SIZE * LIMBS == 3072, "Num3072 isn't 3072 bits");
46 static_assert(sizeof(double_limb_t) == sizeof(limb_t) * 2, "bad size for double_limb_t");
47 static_assert(sizeof(limb_t) * 8 == LIMB_SIZE, "LIMB_SIZE is incorrect");
48 static_assert(SIGNED_LIMB_SIZE * SIGNED_LIMBS > 3072, "SIGNED_LIMBS * SIGNED_LIMB_SIZE is too small");
49 static_assert(3072 / SIGNED_LIMB_SIZE == SIGNED_LIMBS - 1, "Bit 3072 must land in top signed limb");
50
51 // Hard coded values in MuHash3072 constructor and Finalize
52 static_assert(sizeof(limb_t) == 4 || sizeof(limb_t) == 8, "bad size for limb_t");
53
54 void Multiply(const Num3072& a);
55 void Divide(const Num3072& a);
56 void SetToOne();
57 void ToBytes(unsigned char (&out)[BYTE_SIZE]);
58
59 Num3072() { this->SetToOne(); };
60 Num3072(const unsigned char (&data)[BYTE_SIZE]);
61
63 {
64 for (auto& limb : obj.limbs) {
65 READWRITE(limb);
66 }
67 }
68};
69
100{
101private:
104
106
107public:
108 /* The empty set. */
109 MuHash3072() noexcept = default;
110
111 /* A singleton with variable sized data in it. */
112 explicit MuHash3072(Span<const unsigned char> in) noexcept;
113
114 /* Insert a single piece of data into the set. */
115 MuHash3072& Insert(Span<const unsigned char> in) noexcept;
116
117 /* Remove a single piece of data from the set. */
118 MuHash3072& Remove(Span<const unsigned char> in) noexcept;
119
120 /* Multiply (resulting in a hash for the union of the sets) */
121 MuHash3072& operator*=(const MuHash3072& mul) noexcept;
122
123 /* Divide (resulting in a hash for the difference of the sets) */
124 MuHash3072& operator/=(const MuHash3072& div) noexcept;
125
126 /* Finalize into a 32-byte hash. Does not change this object's value. */
127 void Finalize(uint256& out) noexcept;
128
130 {
131 READWRITE(obj.m_numerator);
132 READWRITE(obj.m_denominator);
133 }
134};
135
136#endif // BITCOIN_CRYPTO_MUHASH_H
A class representing MuHash sets.
Definition: muhash.h:100
Num3072 ToNum3072(Span< const unsigned char > in)
Definition: muhash.cpp:535
Num3072 m_numerator
Definition: muhash.h:102
SERIALIZE_METHODS(MuHash3072, obj)
Definition: muhash.h:129
MuHash3072() noexcept=default
void Finalize(uint256 &out) noexcept
Definition: muhash.cpp:551
MuHash3072 & Remove(Span< const unsigned char > in) noexcept
Definition: muhash.cpp:581
MuHash3072 & Insert(Span< const unsigned char > in) noexcept
Definition: muhash.cpp:576
Num3072 m_denominator
Definition: muhash.h:103
Definition: muhash.h:14
Num3072 GetInverse() const
Definition: muhash.cpp:383
static constexpr int LIMBS
Definition: muhash.h:37
int64_t signed_double_limb_t
Definition: muhash.h:34
int32_t signed_limb_t
Definition: muhash.h:36
static constexpr size_t BYTE_SIZE
Definition: muhash.h:21
bool IsOverflow() const
Indicates whether d is larger than the modulus.
Definition: muhash.cpp:115
void ToBytes(unsigned char(&out)[BYTE_SIZE])
Definition: muhash.cpp:525
limb_t limbs[LIMBS]
Definition: muhash.h:42
static constexpr int SIGNED_LIMBS
Definition: muhash.h:38
void SetToOne()
Definition: muhash.cpp:492
static constexpr int LIMB_SIZE
Definition: muhash.h:39
void Divide(const Num3072 &a)
Definition: muhash.cpp:498
static constexpr int SIGNED_LIMB_SIZE
Definition: muhash.h:40
void FullReduce()
Definition: muhash.cpp:124
uint64_t double_limb_t
Definition: muhash.h:33
SERIALIZE_METHODS(Num3072, obj)
Definition: muhash.h:62
uint32_t limb_t
Definition: muhash.h:35
Num3072()
Definition: muhash.h:59
void Multiply(const Num3072 &a)
Definition: muhash.cpp:455
256-bit opaque blob.
Definition: uint256.h:201
#define READWRITE(...)
Definition: serialize.h:156