mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 15:04:44 +01:00
refactor: Improve encapsulation between MuHash3072 and Num3072
Also fixes a typo.
This commit is contained in:
parent
cb2c578451
commit
a1fcceac69
2 changed files with 29 additions and 20 deletions
|
@ -17,7 +17,6 @@ namespace {
|
||||||
using limb_t = Num3072::limb_t;
|
using limb_t = Num3072::limb_t;
|
||||||
using double_limb_t = Num3072::double_limb_t;
|
using double_limb_t = Num3072::double_limb_t;
|
||||||
constexpr int LIMB_SIZE = Num3072::LIMB_SIZE;
|
constexpr int LIMB_SIZE = Num3072::LIMB_SIZE;
|
||||||
constexpr int LIMBS = Num3072::LIMBS;
|
|
||||||
/** 2^3072 - 1103717, the largest 3072-bit safe prime number, is used as the modulus. */
|
/** 2^3072 - 1103717, the largest 3072-bit safe prime number, is used as the modulus. */
|
||||||
constexpr limb_t MAX_PRIME_DIFF = 1103717;
|
constexpr limb_t MAX_PRIME_DIFF = 1103717;
|
||||||
|
|
||||||
|
@ -123,7 +122,7 @@ inline void square_n_mul(Num3072& in_out, const int sq, const Num3072& mul)
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
/** Indicates wether d is larger than the modulus. */
|
/** Indicates whether d is larger than the modulus. */
|
||||||
bool Num3072::IsOverflow() const
|
bool Num3072::IsOverflow() const
|
||||||
{
|
{
|
||||||
if (this->limbs[0] <= std::numeric_limits<limb_t>::max() - MAX_PRIME_DIFF) return false;
|
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)
|
||||||
if (this->IsOverflow()) this->FullReduce();
|
if (this->IsOverflow()) this->FullReduce();
|
||||||
}
|
}
|
||||||
|
|
||||||
Num3072 MuHash3072::ToNum3072(Span<const unsigned char> in) {
|
Num3072::Num3072(const unsigned char (&data)[BYTE_SIZE]) {
|
||||||
Num3072 out{};
|
|
||||||
uint256 hashed_in = (CHashWriter(SER_DISK, 0) << in).GetSHA256();
|
|
||||||
unsigned char tmp[BYTE_SIZE];
|
|
||||||
ChaCha20(hashed_in.data(), hashed_in.size()).Keystream(tmp, BYTE_SIZE);
|
|
||||||
for (int i = 0; i < LIMBS; ++i) {
|
for (int i = 0; i < LIMBS; ++i) {
|
||||||
if (sizeof(limb_t) == 4) {
|
if (sizeof(limb_t) == 4) {
|
||||||
out.limbs[i] = ReadLE32(tmp + 4 * i);
|
this->limbs[i] = ReadLE32(data + 4 * i);
|
||||||
} else if (sizeof(limb_t) == 8) {
|
} else if (sizeof(limb_t) == 8) {
|
||||||
out.limbs[i] = ReadLE64(tmp + 8 * i);
|
this->limbs[i] = ReadLE64(data + 8 * i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Num3072::ToBytes(unsigned char (&out)[BYTE_SIZE]) {
|
||||||
|
for (int i = 0; i < LIMBS; ++i) {
|
||||||
|
if (sizeof(limb_t) == 4) {
|
||||||
|
WriteLE32(out + i * 4, this->limbs[i]);
|
||||||
|
} else if (sizeof(limb_t) == 8) {
|
||||||
|
WriteLE64(out + i * 8, this->limbs[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Num3072 MuHash3072::ToNum3072(Span<const unsigned char> in) {
|
||||||
|
unsigned char tmp[Num3072::BYTE_SIZE];
|
||||||
|
|
||||||
|
uint256 hashed_in = (CHashWriter(SER_DISK, 0) << in).GetSHA256();
|
||||||
|
ChaCha20(hashed_in.data(), hashed_in.size()).Keystream(tmp, Num3072::BYTE_SIZE);
|
||||||
|
Num3072 out{tmp};
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -301,14 +315,8 @@ void MuHash3072::Finalize(uint256& out) noexcept
|
||||||
m_numerator.Divide(m_denominator);
|
m_numerator.Divide(m_denominator);
|
||||||
m_denominator.SetToOne(); // Needed to keep the MuHash object valid
|
m_denominator.SetToOne(); // Needed to keep the MuHash object valid
|
||||||
|
|
||||||
unsigned char data[384];
|
unsigned char data[Num3072::BYTE_SIZE];
|
||||||
for (int i = 0; i < LIMBS; ++i) {
|
m_numerator.ToBytes(data);
|
||||||
if (sizeof(limb_t) == 4) {
|
|
||||||
WriteLE32(data + i * 4, m_numerator.limbs[i]);
|
|
||||||
} else if (sizeof(limb_t) == 8) {
|
|
||||||
WriteLE64(data + i * 8, m_numerator.limbs[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
out = (CHashWriter(SER_DISK, 0) << data).GetSHA256();
|
out = (CHashWriter(SER_DISK, 0) << data).GetSHA256();
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ private:
|
||||||
Num3072 GetInverse() const;
|
Num3072 GetInverse() const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
static constexpr size_t BYTE_SIZE = 384;
|
||||||
|
|
||||||
#ifdef HAVE___INT128
|
#ifdef HAVE___INT128
|
||||||
typedef unsigned __int128 double_limb_t;
|
typedef unsigned __int128 double_limb_t;
|
||||||
|
@ -48,8 +49,10 @@ public:
|
||||||
void Divide(const Num3072& a);
|
void Divide(const Num3072& a);
|
||||||
void SetToOne();
|
void SetToOne();
|
||||||
void Square();
|
void Square();
|
||||||
|
void ToBytes(unsigned char (&out)[BYTE_SIZE]);
|
||||||
|
|
||||||
Num3072() { this->SetToOne(); };
|
Num3072() { this->SetToOne(); };
|
||||||
|
Num3072(const unsigned char (&data)[BYTE_SIZE]);
|
||||||
|
|
||||||
SERIALIZE_METHODS(Num3072, obj)
|
SERIALIZE_METHODS(Num3072, obj)
|
||||||
{
|
{
|
||||||
|
@ -78,7 +81,7 @@ public:
|
||||||
* arbitrary subset of the update operations, allowing them to be
|
* arbitrary subset of the update operations, allowing them to be
|
||||||
* efficiently combined later.
|
* efficiently combined later.
|
||||||
*
|
*
|
||||||
* Muhash does not support checking if an element is already part of the
|
* MuHash does not support checking if an element is already part of the
|
||||||
* set. That is why this class does not enforce the use of a set as the
|
* set. That is why this class does not enforce the use of a set as the
|
||||||
* data it represents because there is no efficient way to do so.
|
* data it represents because there is no efficient way to do so.
|
||||||
* It is possible to add elements more than once and also to remove
|
* It is possible to add elements more than once and also to remove
|
||||||
|
@ -91,8 +94,6 @@ public:
|
||||||
class MuHash3072
|
class MuHash3072
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
static constexpr size_t BYTE_SIZE = 384;
|
|
||||||
|
|
||||||
Num3072 m_numerator;
|
Num3072 m_numerator;
|
||||||
Num3072 m_denominator;
|
Num3072 m_denominator;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue