Skip to content

Commit e549bf8

Browse files
committed
Make CHash256 and CHash160 consume Spans
1 parent 2a2182c commit e549bf8

16 files changed

+32
-32
lines changed

src/bench/chacha_poly_aead.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static void HASH(benchmark::Bench& bench, size_t buffersize)
9393
uint8_t hash[CHash256::OUTPUT_SIZE];
9494
std::vector<uint8_t> in(buffersize,0);
9595
bench.batch(in.size()).unit("byte").run([&] {
96-
CHash256().Write(in.data(), in.size()).Finalize(hash);
96+
CHash256().Write(in).Finalize(hash);
9797
});
9898
}
9999

src/bench/verify_script.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static void VerifyScriptBench(benchmark::Bench& bench)
3434
key.Set(vchKey.begin(), vchKey.end(), false);
3535
CPubKey pubkey = key.GetPubKey();
3636
uint160 pubkeyHash;
37-
CHash160().Write(pubkey.begin(), pubkey.size()).Finalize(pubkeyHash.begin());
37+
CHash160().Write(pubkey).Finalize(pubkeyHash.begin());
3838

3939
// Script.
4040
CScript scriptPubKey = CScript() << witnessversion << ToByteVector(pubkeyHash);

src/blockfilter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ uint256 BlockFilter::GetHash() const
291291
const std::vector<unsigned char>& data = GetEncodedFilter();
292292

293293
uint256 result;
294-
CHash256().Write(data.data(), data.size()).Finalize(result.begin());
294+
CHash256().Write(data).Finalize(result.begin());
295295
return result;
296296
}
297297

@@ -301,8 +301,8 @@ uint256 BlockFilter::ComputeHeader(const uint256& prev_header) const
301301

302302
uint256 result;
303303
CHash256()
304-
.Write(filter_hash.begin(), filter_hash.size())
305-
.Write(prev_header.begin(), prev_header.size())
304+
.Write(filter_hash)
305+
.Write(prev_header)
306306
.Finalize(result.begin());
307307
return result;
308308
}

src/hash.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class CHash256 {
3131
sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash);
3232
}
3333

34-
CHash256& Write(const unsigned char *data, size_t len) {
35-
sha.Write(data, len);
34+
CHash256& Write(Span<const unsigned char> input) {
35+
sha.Write(input.data(), input.size());
3636
return *this;
3737
}
3838

@@ -55,8 +55,8 @@ class CHash160 {
5555
CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash);
5656
}
5757

58-
CHash160& Write(const unsigned char *data, size_t len) {
59-
sha.Write(data, len);
58+
CHash160& Write(Span<const unsigned char> input) {
59+
sha.Write(input.data(), input.size());
6060
return *this;
6161
}
6262

@@ -72,7 +72,7 @@ inline uint256 Hash(const T1 pbegin, const T1 pend)
7272
{
7373
static const unsigned char pblank[1] = {};
7474
uint256 result;
75-
CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
75+
CHash256().Write({pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])})
7676
.Finalize((unsigned char*)&result);
7777
return result;
7878
}
@@ -83,8 +83,8 @@ inline uint256 Hash(const T1 p1begin, const T1 p1end,
8383
const T2 p2begin, const T2 p2end) {
8484
static const unsigned char pblank[1] = {};
8585
uint256 result;
86-
CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
87-
.Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
86+
CHash256().Write({p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0])})
87+
.Write({p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0])})
8888
.Finalize((unsigned char*)&result);
8989
return result;
9090
}
@@ -95,7 +95,7 @@ inline uint160 Hash160(const T1 pbegin, const T1 pend)
9595
{
9696
static unsigned char pblank[1] = {};
9797
uint160 result;
98-
CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
98+
CHash160().Write({pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])})
9999
.Finalize((unsigned char*)&result);
100100
return result;
101101
}
@@ -129,7 +129,7 @@ class CHashWriter
129129
int GetVersion() const { return nVersion; }
130130

131131
void write(const char *pch, size_t size) {
132-
ctx.Write((const unsigned char*)pch, size);
132+
ctx.Write({(const unsigned char*)pch, size});
133133
}
134134

135135
// invalidates the object

src/key.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
237237
std::string str = "Bitcoin key verification\n";
238238
GetRandBytes(rnd, sizeof(rnd));
239239
uint256 hash;
240-
CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize(hash.begin());
240+
CHash256().Write(MakeUCharSpan(str)).Write(rnd).Finalize(hash.begin());
241241
std::vector<unsigned char> vchSig;
242242
Sign(hash, vchSig);
243243
return pubkey.Verify(hash, vchSig);

src/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ int V1TransportDeserializer::readData(const char *pch, unsigned int nBytes)
685685
vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
686686
}
687687

688-
hasher.Write((const unsigned char*)pch, nCopy);
688+
hasher.Write({(const unsigned char*)pch, nCopy});
689689
memcpy(&vRecv[nDataPos], pch, nCopy);
690690
nDataPos += nCopy;
691691

src/script/interpreter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,9 +986,9 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
986986
else if (opcode == OP_SHA256)
987987
CSHA256().Write(vch.data(), vch.size()).Finalize(vchHash.data());
988988
else if (opcode == OP_HASH160)
989-
CHash160().Write(vch.data(), vch.size()).Finalize(vchHash.data());
989+
CHash160().Write(vch).Finalize(vchHash.data());
990990
else if (opcode == OP_HASH256)
991-
CHash256().Write(vch.data(), vch.size()).Finalize(vchHash.data());
991+
CHash256().Write(vch).Finalize(vchHash.data());
992992
popstack(stack);
993993
stack.push_back(vchHash);
994994
}

src/test/crypto_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ BOOST_AUTO_TEST_CASE(sha256d64)
743743
in[j] = InsecureRandBits(8);
744744
}
745745
for (int j = 0; j < i; ++j) {
746-
CHash256().Write(in + 64 * j, 64).Finalize(out1 + 32 * j);
746+
CHash256().Write({in + 64 * j, 64}).Finalize(out1 + 32 * j);
747747
}
748748
SHA256D64(out2, in, i);
749749
BOOST_CHECK(memcmp(out1, out2, 32 * i) == 0);

src/test/fuzz/crypto.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ void test_one_input(const std::vector<uint8_t>& buffer)
4444
}
4545
}
4646

47-
(void)hash160.Write(data.data(), data.size());
48-
(void)hash256.Write(data.data(), data.size());
47+
(void)hash160.Write(data);
48+
(void)hash256.Write(data);
4949
(void)hmac_sha256.Write(data.data(), data.size());
5050
(void)hmac_sha512.Write(data.data(), data.size());
5151
(void)ripemd160.Write(data.data(), data.size());

src/test/key_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ BOOST_AUTO_TEST_CASE(key_key_negation)
196196
std::string str = "Bitcoin key verification\n";
197197
GetRandBytes(rnd, sizeof(rnd));
198198
uint256 hash;
199-
CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize(hash.begin());
199+
CHash256().Write(MakeUCharSpan(str)).Write(rnd).Finalize(hash.begin());
200200

201201
// import the static test key
202202
CKey key = DecodeSecret(strSecret1C);

0 commit comments

Comments
 (0)