@@ -17,7 +17,6 @@ namespace {
17
17
using limb_t = Num3072::limb_t ;
18
18
using double_limb_t = Num3072::double_limb_t ;
19
19
constexpr int LIMB_SIZE = Num3072::LIMB_SIZE;
20
- constexpr int LIMBS = Num3072::LIMBS;
21
20
/* * 2^3072 - 1103717, the largest 3072-bit safe prime number, is used as the modulus. */
22
21
constexpr limb_t MAX_PRIME_DIFF = 1103717 ;
23
22
@@ -123,7 +122,7 @@ inline void square_n_mul(Num3072& in_out, const int sq, const Num3072& mul)
123
122
124
123
} // namespace
125
124
126
- /* * Indicates wether d is larger than the modulus. */
125
+ /* * Indicates whether d is larger than the modulus. */
127
126
bool Num3072::IsOverflow () const
128
127
{
129
128
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)
276
275
if (this ->IsOverflow ()) this ->FullReduce ();
277
276
}
278
277
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]) {
284
289
for (int i = 0 ; i < LIMBS; ++i) {
285
290
if (sizeof (limb_t ) == 4 ) {
286
- out. limbs [i] = ReadLE32 (tmp + 4 * i );
291
+ WriteLE32 ( out + i * 4 , this -> limbs [i] );
287
292
} else if (sizeof (limb_t ) == 8 ) {
288
- out. limbs [i] = ReadLE64 (tmp + 8 * i );
293
+ WriteLE64 ( out + i * 8 , this -> limbs [i] );
289
294
}
290
295
}
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
+
291
305
return out;
292
306
}
293
307
@@ -301,14 +315,8 @@ void MuHash3072::Finalize(uint256& out) noexcept
301
315
m_numerator.Divide (m_denominator);
302
316
m_denominator.SetToOne (); // Needed to keep the MuHash object valid
303
317
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);
312
320
313
321
out = (CHashWriter (SER_DISK, 0 ) << data).GetSHA256 ();
314
322
}
0 commit comments