mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-05 14:06:27 -05:00
Make CHash256 and CHash160 consume Spans
This commit is contained in:
parent
2a2182c387
commit
e549bf8a9a
16 changed files with 32 additions and 32 deletions
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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
src/hash.h
18
src/hash.h
|
@ -31,8 +31,8 @@ public:
|
|||
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;
|
||||
}
|
||||
|
||||
|
@ -55,8 +55,8 @@ public:
|
|||
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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -129,7 +129,7 @@ public:
|
|||
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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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++;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -282,7 +282,7 @@ public:
|
|||
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) {
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
@ -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__));
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue