Merge bitcoin/bitcoin#28085: refactor: use Span for SipHash::Write

7d92b1430a refactor: use Span for SipHash::Write (Sebastian Falbesoner)

Pull request description:

  This simple refactoring PR changes the interface for the `SipHash` arbitrary-data `Write` method to take a `Span<unsigned char>` instead of having to pass data and length. (`Span<std::byte>` seems to be more modern, but vectors of `unsigned char` are still used prety much everywhere where SipHash is called, and I didn't find it very appealing having to clutter the code with `Make(Writable)ByteSpan` helpers).

ACKs for top commit:
  sipa:
    utACK 7d92b1430a
  MarcoFalke:
    lgtm ACK 7d92b1430a
  achow101:
    ACK 7d92b1430a

Tree-SHA512: f17a27013c942aead4b09f5a64e0c3ff8dbc7e83fe63eb9a2e3ace8be9921c9cbba3ec67e3e83fbe3332ca941c42370efd059e702c060f9b508307e9657c66f2
This commit is contained in:
Andrew Chow
2023-07-19 16:19:54 -04:00
12 changed files with 25 additions and 23 deletions

View File

@@ -45,14 +45,14 @@ CSipHasher& CSipHasher::Write(uint64_t data)
return *this;
}
CSipHasher& CSipHasher::Write(const unsigned char* data, size_t size)
CSipHasher& CSipHasher::Write(Span<const unsigned char> data)
{
uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3];
uint64_t t = tmp;
uint8_t c = count;
while (size--) {
t |= ((uint64_t)(*(data++))) << (8 * (c % 8));
while (data.size() > 0) {
t |= uint64_t{data.front()} << (8 * (c % 8));
c++;
if ((c & 7) == 0) {
v3 ^= t;
@@ -61,6 +61,7 @@ CSipHasher& CSipHasher::Write(const unsigned char* data, size_t size)
v0 ^= t;
t = 0;
}
data = data.subspan(1);
}
v[0] = v0;

View File

@@ -7,6 +7,7 @@
#include <stdint.h>
#include <span.h>
#include <uint256.h>
/** SipHash-2-4 */
@@ -26,7 +27,7 @@ public:
*/
CSipHasher& Write(uint64_t data);
/** Hash arbitrary bytes. */
CSipHasher& Write(const unsigned char* data, size_t size);
CSipHasher& Write(Span<const unsigned char> data);
/** Compute the 64-bit SipHash-2-4 of the data written so far. The object remains untouched. */
uint64_t Finalize() const;
};