refactor: extract `SipHash` C0-C3 constants to class scope
What changed, and why it matters
This commit is a simple code cleanup: it replaces four hard-coded magic numbers in Bitcoin Core's SipHash implementation with named constants. The actual numeric values and how they are used remain exactly the same, so there is no security change.
No security action needed. Review as normal code-quality refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change extracts the standard SipHash initialization constants (0x736f6d6570736575, 0x646f72616e646f6d, 0x6c7967656e657261, 0x7465646279746573) into static constexpr members C0-C3 of CSipHasher. The constants are referenced in the constructor and in the SipHashUint256/SipHashUint256Extra specialized functions. Values and algorithm behavior are unchanged; this is purely a readability/maintainability refactor.
Changed components
src/crypto/siphash.cppsrc/crypto/siphash.hInspect captured patch +19 / −12
diff --git a/src/crypto/siphash.cpp b/src/crypto/siphash.cpp
index 1a9eb771..37ed9189 100644
--- a/src/crypto/siphash.cpp
+++ b/src/crypto/siphash.cpp
@@ -21,10 +21,10 @@
CSipHasher::CSipHasher(uint64_t k0, uint64_t k1)
{
- v[0] = 0x736f6d6570736575ULL ^ k0;
- v[1] = 0x646f72616e646f6dULL ^ k1;
- v[2] = 0x6c7967656e657261ULL ^ k0;
- v[3] = 0x7465646279746573ULL ^ k1;
+ v[0] = C0 ^ k0;
+ v[1] = C1 ^ k1;
+ v[2] = C2 ^ k0;
+ v[3] = C3 ^ k1;
count = 0;
tmp = 0;
}
@@ -101,10 +101,11 @@ uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256& val)
/* Specialized implementation for efficiency */
uint64_t d = val.GetUint64(0);
- uint64_t v0 = 0x736f6d6570736575ULL ^ k0;
- uint64_t v1 = 0x646f72616e646f6dULL ^ k1;
- uint64_t v2 = 0x6c7967656e657261ULL ^ k0;
- uint64_t v3 = 0x7465646279746573ULL ^ k1 ^ d;
+ // TODO moved in followup commit
+ uint64_t v0 = CSipHasher::C0 ^ k0;
+ uint64_t v1 = CSipHasher::C1 ^ k1;
+ uint64_t v2 = CSipHasher::C2 ^ k0;
+ uint64_t v3 = CSipHasher::C3 ^ k1 ^ d;
SIPROUND;
SIPROUND;
@@ -141,10 +142,11 @@ uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256& val, uint3
/* Specialized implementation for efficiency */
uint64_t d = val.GetUint64(0);
- uint64_t v0 = 0x736f6d6570736575ULL ^ k0;
- uint64_t v1 = 0x646f72616e646f6dULL ^ k1;
- uint64_t v2 = 0x6c7967656e657261ULL ^ k0;
- uint64_t v3 = 0x7465646279746573ULL ^ k1 ^ d;
+ // TODO moved in followup commit
+ uint64_t v0 = CSipHasher::C0 ^ k0;
+ uint64_t v1 = CSipHasher::C1 ^ k1;
+ uint64_t v2 = CSipHasher::C2 ^ k0;
+ uint64_t v3 = CSipHasher::C3 ^ k1 ^ d;
SIPROUND;
SIPROUND;
diff --git a/src/crypto/siphash.h b/src/crypto/siphash.h
index 8d41a083..4293fdf9 100644
--- a/src/crypto/siphash.h
+++ b/src/crypto/siphash.h
@@ -19,6 +19,11 @@ private:
uint8_t count; // Only the low 8 bits of the input size matter.
public:
+ static constexpr uint64_t C0{0x736f6d6570736575ULL};
+ static constexpr uint64_t C1{0x646f72616e646f6dULL};
+ static constexpr uint64_t C2{0x6c7967656e657261ULL};
+ static constexpr uint64_t C3{0x7465646279746573ULL};
+
/** Construct a SipHash calculator initialized with 128-bit key (k0, k1) */
CSipHasher(uint64_t k0, uint64_t k1);
/** Hash a 64-bit integer worth of data
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.