Skip to content

Commit a1fccea

Browse files
committed
refactor: Improve encapsulation between MuHash3072 and Num3072
Also fixes a typo.
1 parent cb2c578 commit a1fccea

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

src/crypto/muhash.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace {
1717
using limb_t = Num3072::limb_t;
1818
using double_limb_t = Num3072::double_limb_t;
1919
constexpr int LIMB_SIZE = Num3072::LIMB_SIZE;
20-
constexpr int LIMBS = Num3072::LIMBS;
2120
/** 2^3072 - 1103717, the largest 3072-bit safe prime number, is used as the modulus. */
2221
constexpr limb_t MAX_PRIME_DIFF = 1103717;
2322

@@ -123,7 +122,7 @@ inline void square_n_mul(Num3072& in_out, const int sq, const Num3072& mul)
123122

124123
} // namespace
125124

126-
/** Indicates wether d is larger than the modulus. */
125+
/** Indicates whether d is larger than the modulus. */
127126
bool Num3072::IsOverflow() const
128127
{
129128
if (this->limbs[0] <= std::numeric_limits<limb_t>::max() - MAX_PRIME_DIFF) return false;
@@ -276,18 +275,33 @@ void Num3072::Divide(const Num3072& a)
276275
if (this->IsOverflow()) this->FullReduce();
277276
}
278277

279-
Num3072 MuHash3072::ToNum3072(Span<const unsigned char> in) {
280-
Num3072 out{};
281-
uint256 hashed_in = (CHashWriter(SER_DISK, 0) << in).GetSHA256();
282-
unsigned char tmp[BYTE_SIZE];
283-
ChaCha20(hashed_in.data(), hashed_in.size()).Keystream(tmp, BYTE_SIZE);
278+
Num3072::Num3072(const unsigned char (&data)[BYTE_SIZE]) {
279+
for (int i = 0; i < LIMBS; ++i) {
280+
if (sizeof(limb_t) == 4) {
281+
this->limbs[i] = ReadLE32(data + 4 * i);
282+
} else if (sizeof(limb_t) == 8) {
283+
this->limbs[i] = ReadLE64(data + 8 * i);
284+
}
285+
}
286+
}
287+
288+
void Num3072::ToBytes(unsigned char (&out)[BYTE_SIZE]) {
284289
for (int i = 0; i < LIMBS; ++i) {
285290
if (sizeof(limb_t) == 4) {
286-
out.limbs[i] = ReadLE32(tmp + 4 * i);
291+
WriteLE32(out + i * 4, this->limbs[i]);
287292
} else if (sizeof(limb_t) == 8) {
288-
out.limbs[i] = ReadLE64(tmp + 8 * i);
293+
WriteLE64(out + i * 8, this->limbs[i]);
289294
}
290295
}
296+
}
297+
298+
Num3072 MuHash3072::ToNum3072(Span<const unsigned char> in) {
299+
unsigned char tmp[Num3072::BYTE_SIZE];
300+
301+
uint256 hashed_in = (CHashWriter(SER_DISK, 0) << in).GetSHA256();
302+
ChaCha20(hashed_in.data(), hashed_in.size()).Keystream(tmp, Num3072::BYTE_SIZE);
303+
Num3072 out{tmp};
304+
291305
return out;
292306
}
293307

@@ -301,14 +315,8 @@ void MuHash3072::Finalize(uint256& out) noexcept
301315
m_numerator.Divide(m_denominator);
302316
m_denominator.SetToOne(); // Needed to keep the MuHash object valid
303317

304-
unsigned char data[384];
305-
for (int i = 0; i < LIMBS; ++i) {
306-
if (sizeof(limb_t) == 4) {
307-
WriteLE32(data + i * 4, m_numerator.limbs[i]);
308-
} else if (sizeof(limb_t) == 8) {
309-
WriteLE64(data + i * 8, m_numerator.limbs[i]);
310-
}
311-
}
318+
unsigned char data[Num3072::BYTE_SIZE];
319+
m_numerator.ToBytes(data);
312320

313321
out = (CHashWriter(SER_DISK, 0) << data).GetSHA256();
314322
}

src/crypto/muhash.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Num3072
2222
Num3072 GetInverse() const;
2323

2424
public:
25+
static constexpr size_t BYTE_SIZE = 384;
2526

2627
#ifdef HAVE___INT128
2728
typedef unsigned __int128 double_limb_t;
@@ -48,8 +49,10 @@ class Num3072
4849
void Divide(const Num3072& a);
4950
void SetToOne();
5051
void Square();
52+
void ToBytes(unsigned char (&out)[BYTE_SIZE]);
5153

5254
Num3072() { this->SetToOne(); };
55+
Num3072(const unsigned char (&data)[BYTE_SIZE]);
5356

5457
SERIALIZE_METHODS(Num3072, obj)
5558
{
@@ -78,7 +81,7 @@ class Num3072
7881
* arbitrary subset of the update operations, allowing them to be
7982
* efficiently combined later.
8083
*
81-
* Muhash does not support checking if an element is already part of the
84+
* MuHash does not support checking if an element is already part of the
8285
* set. That is why this class does not enforce the use of a set as the
8386
* data it represents because there is no efficient way to do so.
8487
* It is possible to add elements more than once and also to remove
@@ -91,8 +94,6 @@ class Num3072
9194
class MuHash3072
9295
{
9396
private:
94-
static constexpr size_t BYTE_SIZE = 384;
95-
9697
Num3072 m_numerator;
9798
Num3072 m_denominator;
9899

0 commit comments

Comments
 (0)