Skip to content

Commit

Permalink
Make CHash256 and CHash160 consume Spans
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Jul 30, 2020
1 parent 2a2182c commit e549bf8
Show file tree
Hide file tree
Showing 16 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/bench/chacha_poly_aead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static void HASH(benchmark::Bench& bench, size_t buffersize)
uint8_t hash[CHash256::OUTPUT_SIZE];
std::vector<uint8_t> in(buffersize,0);
bench.batch(in.size()).unit("byte").run([&] {
CHash256().Write(in.data(), in.size()).Finalize(hash);
CHash256().Write(in).Finalize(hash);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/bench/verify_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static void VerifyScriptBench(benchmark::Bench& bench)
key.Set(vchKey.begin(), vchKey.end(), false);
CPubKey pubkey = key.GetPubKey();
uint160 pubkeyHash;
CHash160().Write(pubkey.begin(), pubkey.size()).Finalize(pubkeyHash.begin());
CHash160().Write(pubkey).Finalize(pubkeyHash.begin());

// Script.
CScript scriptPubKey = CScript() << witnessversion << ToByteVector(pubkeyHash);
Expand Down
6 changes: 3 additions & 3 deletions src/blockfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ uint256 BlockFilter::GetHash() const
const std::vector<unsigned char>& data = GetEncodedFilter();

uint256 result;
CHash256().Write(data.data(), data.size()).Finalize(result.begin());
CHash256().Write(data).Finalize(result.begin());
return result;
}

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

uint256 result;
CHash256()
.Write(filter_hash.begin(), filter_hash.size())
.Write(prev_header.begin(), prev_header.size())
.Write(filter_hash)
.Write(prev_header)
.Finalize(result.begin());
return result;
}
18 changes: 9 additions & 9 deletions src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class CHash256 {
sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash);
}

CHash256& Write(const unsigned char *data, size_t len) {
sha.Write(data, len);
CHash256& Write(Span<const unsigned char> input) {
sha.Write(input.data(), input.size());
return *this;
}

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

CHash160& Write(const unsigned char *data, size_t len) {
sha.Write(data, len);
CHash160& Write(Span<const unsigned char> input) {
sha.Write(input.data(), input.size());
return *this;
}

Expand All @@ -72,7 +72,7 @@ inline uint256 Hash(const T1 pbegin, const T1 pend)
{
static const unsigned char pblank[1] = {};
uint256 result;
CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
CHash256().Write({pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])})
.Finalize((unsigned char*)&result);
return result;
}
Expand All @@ -83,8 +83,8 @@ inline uint256 Hash(const T1 p1begin, const T1 p1end,
const T2 p2begin, const T2 p2end) {
static const unsigned char pblank[1] = {};
uint256 result;
CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
.Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
CHash256().Write({p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0])})
.Write({p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0])})
.Finalize((unsigned char*)&result);
return result;
}
Expand All @@ -95,7 +95,7 @@ inline uint160 Hash160(const T1 pbegin, const T1 pend)
{
static unsigned char pblank[1] = {};
uint160 result;
CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
CHash160().Write({pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])})
.Finalize((unsigned char*)&result);
return result;
}
Expand Down Expand Up @@ -129,7 +129,7 @@ class CHashWriter
int GetVersion() const { return nVersion; }

void write(const char *pch, size_t size) {
ctx.Write((const unsigned char*)pch, size);
ctx.Write({(const unsigned char*)pch, size});
}

// invalidates the object
Expand Down
2 changes: 1 addition & 1 deletion src/key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
std::string str = "Bitcoin key verification\n";
GetRandBytes(rnd, sizeof(rnd));
uint256 hash;
CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize(hash.begin());
CHash256().Write(MakeUCharSpan(str)).Write(rnd).Finalize(hash.begin());
std::vector<unsigned char> vchSig;
Sign(hash, vchSig);
return pubkey.Verify(hash, vchSig);
Expand Down
2 changes: 1 addition & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ int V1TransportDeserializer::readData(const char *pch, unsigned int nBytes)
vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/script/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,9 +986,9 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
else if (opcode == OP_SHA256)
CSHA256().Write(vch.data(), vch.size()).Finalize(vchHash.data());
else if (opcode == OP_HASH160)
CHash160().Write(vch.data(), vch.size()).Finalize(vchHash.data());
CHash160().Write(vch).Finalize(vchHash.data());
else if (opcode == OP_HASH256)
CHash256().Write(vch.data(), vch.size()).Finalize(vchHash.data());
CHash256().Write(vch).Finalize(vchHash.data());
popstack(stack);
stack.push_back(vchHash);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/crypto_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ BOOST_AUTO_TEST_CASE(sha256d64)
in[j] = InsecureRandBits(8);
}
for (int j = 0; j < i; ++j) {
CHash256().Write(in + 64 * j, 64).Finalize(out1 + 32 * j);
CHash256().Write({in + 64 * j, 64}).Finalize(out1 + 32 * j);
}
SHA256D64(out2, in, i);
BOOST_CHECK(memcmp(out1, out2, 32 * i) == 0);
Expand Down
4 changes: 2 additions & 2 deletions src/test/fuzz/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ void test_one_input(const std::vector<uint8_t>& buffer)
}
}

(void)hash160.Write(data.data(), data.size());
(void)hash256.Write(data.data(), data.size());
(void)hash160.Write(data);
(void)hash256.Write(data);
(void)hmac_sha256.Write(data.data(), data.size());
(void)hmac_sha512.Write(data.data(), data.size());
(void)ripemd160.Write(data.data(), data.size());
Expand Down
2 changes: 1 addition & 1 deletion src/test/key_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ BOOST_AUTO_TEST_CASE(key_key_negation)
std::string str = "Bitcoin key verification\n";
GetRandBytes(rnd, sizeof(rnd));
uint256 hash;
CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize(hash.begin());
CHash256().Write(MakeUCharSpan(str)).Write(rnd).Finalize(hash.begin());

// import the static test key
CKey key = DecodeSecret(strSecret1C);
Expand Down
6 changes: 3 additions & 3 deletions src/test/merkle_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
}
}
mutated |= (inner[level] == h);
CHash256().Write(inner[level].begin(), 32).Write(h.begin(), 32).Finalize(h.begin());
CHash256().Write(inner[level]).Write(h).Finalize(h.begin());
}
// Store the resulting hash at inner position level.
inner[level] = h;
Expand All @@ -86,7 +86,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
if (pbranch && matchh) {
pbranch->push_back(h);
}
CHash256().Write(h.begin(), 32).Write(h.begin(), 32).Finalize(h.begin());
CHash256().Write(h).Write(h).Finalize(h.begin());
// Increment count to the value it would have if two entries at this
// level had existed.
count += (((uint32_t)1) << level);
Expand All @@ -101,7 +101,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
matchh = true;
}
}
CHash256().Write(inner[level].begin(), 32).Write(h.begin(), 32).Finalize(h.begin());
CHash256().Write(inner[level]).Write(h).Finalize(h.begin());
level++;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/script_standard_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ BOOST_AUTO_TEST_CASE(script_standard_ExtractDestination)
s << OP_0 << ToByteVector(pubkey.GetID());
BOOST_CHECK(ExtractDestination(s, address));
WitnessV0KeyHash keyhash;
CHash160().Write(pubkey.begin(), pubkey.size()).Finalize(keyhash.begin());
CHash160().Write(pubkey).Finalize(keyhash.begin());
BOOST_CHECK(boost::get<WitnessV0KeyHash>(&address) && *boost::get<WitnessV0KeyHash>(&address) == keyhash);

// TxoutType::WITNESS_V0_SCRIPTHASH
Expand Down
2 changes: 1 addition & 1 deletion src/test/script_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class TestBuilder
CScript scriptPubKey = script;
if (wm == WitnessMode::PKH) {
uint160 hash;
CHash160().Write(&script[1], script.size() - 1).Finalize(hash.begin());
CHash160().Write(MakeSpan(script).subspan(1)).Finalize(hash.begin());
script = CScript() << OP_DUP << OP_HASH160 << ToByteVector(hash) << OP_EQUALVERIFY << OP_CHECKSIG;
scriptPubKey = CScript() << witnessversion << ToByteVector(hash);
} else if (wm == WitnessMode::SH) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/settings_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ BOOST_FIXTURE_TEST_CASE(Merge, MergeTestingSetup)
if (OnlyHasDefaultSectionSetting(settings, network, name)) desc += " ignored";
desc += "\n";

out_sha.Write((const unsigned char*)desc.data(), desc.size());
out_sha.Write(MakeUCharSpan(desc));
if (out_file) {
BOOST_REQUIRE(fwrite(desc.data(), 1, desc.size(), out_file) == desc.size());
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ BOOST_FIXTURE_TEST_CASE(util_ArgsMerge, ArgsMergeTestingSetup)

desc += "\n";

out_sha.Write((const unsigned char*)desc.data(), desc.size());
out_sha.Write(MakeUCharSpan(desc));
if (out_file) {
BOOST_REQUIRE(fwrite(desc.data(), 1, desc.size(), out_file) == desc.size());
}
Expand Down Expand Up @@ -1112,7 +1112,7 @@ BOOST_FIXTURE_TEST_CASE(util_ChainMerge, ChainMergeTestingSetup)
}
desc += "\n";

out_sha.Write((const unsigned char*)desc.data(), desc.size());
out_sha.Write(MakeUCharSpan(desc));
if (out_file) {
BOOST_REQUIRE(fwrite(desc.data(), 1, desc.size(), out_file) == desc.size());
}
Expand Down
4 changes: 2 additions & 2 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3434,7 +3434,7 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc
if (consensusParams.SegwitHeight != std::numeric_limits<int>::max()) {
if (commitpos == -1) {
uint256 witnessroot = BlockWitnessMerkleRoot(block, nullptr);
CHash256().Write(witnessroot.begin(), 32).Write(ret.data(), 32).Finalize(witnessroot.begin());
CHash256().Write(witnessroot).Write(ret).Finalize(witnessroot.begin());
CTxOut out;
out.nValue = 0;
out.scriptPubKey.resize(MINIMUM_WITNESS_COMMITMENT);
Expand Down Expand Up @@ -3579,7 +3579,7 @@ static bool ContextualCheckBlock(const CBlock& block, BlockValidationState& stat
if (block.vtx[0]->vin[0].scriptWitness.stack.size() != 1 || block.vtx[0]->vin[0].scriptWitness.stack[0].size() != 32) {
return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "bad-witness-nonce-size", strprintf("%s : invalid witness reserved value size", __func__));
}
CHash256().Write(hashWitness.begin(), 32).Write(&block.vtx[0]->vin[0].scriptWitness.stack[0][0], 32).Finalize(hashWitness.begin());
CHash256().Write(hashWitness).Write(block.vtx[0]->vin[0].scriptWitness.stack[0]).Finalize(hashWitness.begin());
if (memcmp(hashWitness.begin(), &block.vtx[0]->vout[commitpos].scriptPubKey[6], 32)) {
return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "bad-witness-merkle-match", strprintf("%s : witness merkle commitment mismatch", __func__));
}
Expand Down

0 comments on commit e549bf8

Please sign in to comment.