Skip to content

Commit 7d92b14

Browse files
theStackMarcoFalke
andcommitted
refactor: use Span for SipHash::Write
Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^[email protected]>
1 parent 57b8336 commit 7d92b14

File tree

12 files changed

+25
-23
lines changed

12 files changed

+25
-23
lines changed

src/blockfilter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static const std::map<BlockFilterType, std::string> g_filter_types = {
2727
uint64_t GCSFilter::HashToRange(const Element& element) const
2828
{
2929
uint64_t hash = CSipHasher(m_params.m_siphash_k0, m_params.m_siphash_k1)
30-
.Write(element.data(), element.size())
30+
.Write(element)
3131
.Finalize();
3232
return FastRange64(hash, m_F);
3333
}

src/crypto/siphash.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ CSipHasher& CSipHasher::Write(uint64_t data)
4545
return *this;
4646
}
4747

48-
CSipHasher& CSipHasher::Write(const unsigned char* data, size_t size)
48+
CSipHasher& CSipHasher::Write(Span<const unsigned char> data)
4949
{
5050
uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3];
5151
uint64_t t = tmp;
5252
uint8_t c = count;
5353

54-
while (size--) {
55-
t |= ((uint64_t)(*(data++))) << (8 * (c % 8));
54+
while (data.size() > 0) {
55+
t |= uint64_t{data.front()} << (8 * (c % 8));
5656
c++;
5757
if ((c & 7) == 0) {
5858
v3 ^= t;
@@ -61,6 +61,7 @@ CSipHasher& CSipHasher::Write(const unsigned char* data, size_t size)
6161
v0 ^= t;
6262
t = 0;
6363
}
64+
data = data.subspan(1);
6465
}
6566

6667
v[0] = v0;

src/crypto/siphash.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <stdint.h>
99

10+
#include <span.h>
1011
#include <uint256.h>
1112

1213
/** SipHash-2-4 */
@@ -26,7 +27,7 @@ class CSipHasher
2627
*/
2728
CSipHasher& Write(uint64_t data);
2829
/** Hash arbitrary bytes. */
29-
CSipHasher& Write(const unsigned char* data, size_t size);
30+
CSipHasher& Write(Span<const unsigned char> data);
3031
/** Compute the 64-bit SipHash-2-4 of the data written so far. The object remains untouched. */
3132
uint64_t Finalize() const;
3233
};

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,7 +2529,7 @@ std::vector<CAddress> CConnman::GetAddresses(CNode& requestor, size_t max_addres
25292529
auto local_socket_bytes = requestor.addrBind.GetAddrBytes();
25302530
uint64_t cache_id = GetDeterministicRandomizer(RANDOMIZER_ID_ADDRCACHE)
25312531
.Write(requestor.ConnectedThroughNetwork())
2532-
.Write(local_socket_bytes.data(), local_socket_bytes.size())
2532+
.Write(local_socket_bytes)
25332533
// For outbound connections, the port of the bound address is randomly
25342534
// assigned by the OS and would therefore not be useful for seeding.
25352535
.Write(requestor.IsInboundConn() ? requestor.addrBind.GetPort() : 0)
@@ -2912,7 +2912,7 @@ uint64_t CConnman::CalculateKeyedNetGroup(const CAddress& address) const
29122912
{
29132913
std::vector<unsigned char> vchNetGroup(m_netgroupman.GetGroup(address));
29142914

2915-
return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP).Write(vchNetGroup.data(), vchNetGroup.size()).Finalize();
2915+
return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP).Write(vchNetGroup).Finalize();
29162916
}
29172917

29182918
void CaptureMessageToFile(const CAddress& addr,

src/netaddress.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ class CServiceHash
564564
CSipHasher hasher(m_salt_k0, m_salt_k1);
565565
hasher.Write(a.m_net);
566566
hasher.Write(a.port);
567-
hasher.Write(a.m_addr.data(), a.m_addr.size());
567+
hasher.Write(a.m_addr);
568568
return static_cast<size_t>(hasher.Finalize());
569569
}
570570

src/test/fuzz/addrman.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ class AddrManDeterministic : public AddrMan
171171
hasher.Write(a.source.GetNetwork());
172172
hasher.Write(addr_key.size());
173173
hasher.Write(source_key.size());
174-
hasher.Write(addr_key.data(), addr_key.size());
175-
hasher.Write(source_key.data(), source_key.size());
174+
hasher.Write(addr_key);
175+
hasher.Write(source_key);
176176
return (size_t)hasher.Finalize();
177177
};
178178

src/test/fuzz/crypto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ FUZZ_TARGET(crypto)
5757
(void)sha256.Write(data.data(), data.size());
5858
(void)sha3.Write(data);
5959
(void)sha512.Write(data.data(), data.size());
60-
(void)sip_hasher.Write(data.data(), data.size());
60+
(void)sip_hasher.Write(data);
6161

6262
(void)Hash(data);
6363
(void)Hash160(data);

src/test/fuzz/golomb_rice.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace {
2323
uint64_t HashToRange(const std::vector<uint8_t>& element, const uint64_t f)
2424
{
2525
const uint64_t hash = CSipHasher(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL)
26-
.Write(element.data(), element.size())
26+
.Write(element)
2727
.Finalize();
2828
return FastRange64(hash, f);
2929
}

src/test/hash_tests.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,21 @@ BOOST_AUTO_TEST_CASE(siphash)
8383
CSipHasher hasher(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL);
8484
BOOST_CHECK_EQUAL(hasher.Finalize(), 0x726fdb47dd0e0e31ull);
8585
static const unsigned char t0[1] = {0};
86-
hasher.Write(t0, 1);
86+
hasher.Write(t0);
8787
BOOST_CHECK_EQUAL(hasher.Finalize(), 0x74f839c593dc67fdull);
8888
static const unsigned char t1[7] = {1,2,3,4,5,6,7};
89-
hasher.Write(t1, 7);
89+
hasher.Write(t1);
9090
BOOST_CHECK_EQUAL(hasher.Finalize(), 0x93f5f5799a932462ull);
9191
hasher.Write(0x0F0E0D0C0B0A0908ULL);
9292
BOOST_CHECK_EQUAL(hasher.Finalize(), 0x3f2acc7f57c29bdbull);
9393
static const unsigned char t2[2] = {16,17};
94-
hasher.Write(t2, 2);
94+
hasher.Write(t2);
9595
BOOST_CHECK_EQUAL(hasher.Finalize(), 0x4bc1b3f0968dd39cull);
9696
static const unsigned char t3[9] = {18,19,20,21,22,23,24,25,26};
97-
hasher.Write(t3, 9);
97+
hasher.Write(t3);
9898
BOOST_CHECK_EQUAL(hasher.Finalize(), 0x2f2e6163076bcfadull);
9999
static const unsigned char t4[5] = {27,28,29,30,31};
100-
hasher.Write(t4, 5);
100+
hasher.Write(t4);
101101
BOOST_CHECK_EQUAL(hasher.Finalize(), 0x7127512f72f27cceull);
102102
hasher.Write(0x2726252423222120ULL);
103103
BOOST_CHECK_EQUAL(hasher.Finalize(), 0x0e3ea96b5304a7d0ull);
@@ -111,7 +111,7 @@ BOOST_AUTO_TEST_CASE(siphash)
111111
for (uint8_t x=0; x<std::size(siphash_4_2_testvec); ++x)
112112
{
113113
BOOST_CHECK_EQUAL(hasher2.Finalize(), siphash_4_2_testvec[x]);
114-
hasher2.Write(&x, 1);
114+
hasher2.Write(Span{&x, 1});
115115
}
116116
// Check test vectors from spec, eight bytes at a time
117117
CSipHasher hasher3(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL);
@@ -140,9 +140,9 @@ BOOST_AUTO_TEST_CASE(siphash)
140140
uint8_t nb[4];
141141
WriteLE32(nb, n);
142142
CSipHasher sip256(k1, k2);
143-
sip256.Write(x.begin(), 32);
143+
sip256.Write(x);
144144
CSipHasher sip288 = sip256;
145-
sip288.Write(nb, 4);
145+
sip288.Write(nb);
146146
BOOST_CHECK_EQUAL(SipHashUint256(k1, k2, x), sip256.Finalize());
147147
BOOST_CHECK_EQUAL(SipHashUint256Extra(k1, k2, x, n), sip288.Finalize());
148148
}

src/txrequest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class PriorityComputer {
124124

125125
Priority operator()(const uint256& txhash, NodeId peer, bool preferred) const
126126
{
127-
uint64_t low_bits = CSipHasher(m_k0, m_k1).Write(txhash.begin(), txhash.size()).Write(peer).Finalize() >> 1;
127+
uint64_t low_bits = CSipHasher(m_k0, m_k1).Write(txhash).Write(peer).Finalize() >> 1;
128128
return low_bits | uint64_t{preferred} << 63;
129129
}
130130

0 commit comments

Comments
 (0)