mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 23:09:44 -04:00
Make CHash256/CHash160 output to Span
This commit is contained in:
parent
0ef97b1b10
commit
02c4cc5c5d
13 changed files with 28 additions and 26 deletions
|
@ -34,7 +34,7 @@ static void VerifyScriptBench(benchmark::Bench& bench)
|
||||||
key.Set(vchKey.begin(), vchKey.end(), false);
|
key.Set(vchKey.begin(), vchKey.end(), false);
|
||||||
CPubKey pubkey = key.GetPubKey();
|
CPubKey pubkey = key.GetPubKey();
|
||||||
uint160 pubkeyHash;
|
uint160 pubkeyHash;
|
||||||
CHash160().Write(pubkey).Finalize(pubkeyHash.begin());
|
CHash160().Write(pubkey).Finalize(pubkeyHash);
|
||||||
|
|
||||||
// Script.
|
// Script.
|
||||||
CScript scriptPubKey = CScript() << witnessversion << ToByteVector(pubkeyHash);
|
CScript scriptPubKey = CScript() << witnessversion << ToByteVector(pubkeyHash);
|
||||||
|
|
|
@ -291,7 +291,7 @@ uint256 BlockFilter::GetHash() const
|
||||||
const std::vector<unsigned char>& data = GetEncodedFilter();
|
const std::vector<unsigned char>& data = GetEncodedFilter();
|
||||||
|
|
||||||
uint256 result;
|
uint256 result;
|
||||||
CHash256().Write(data).Finalize(result.begin());
|
CHash256().Write(data).Finalize(result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,6 +303,6 @@ uint256 BlockFilter::ComputeHeader(const uint256& prev_header) const
|
||||||
CHash256()
|
CHash256()
|
||||||
.Write(filter_hash)
|
.Write(filter_hash)
|
||||||
.Write(prev_header)
|
.Write(prev_header)
|
||||||
.Finalize(result.begin());
|
.Finalize(result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
18
src/hash.h
18
src/hash.h
|
@ -25,10 +25,11 @@ private:
|
||||||
public:
|
public:
|
||||||
static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
|
static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
|
||||||
|
|
||||||
void Finalize(unsigned char hash[OUTPUT_SIZE]) {
|
void Finalize(Span<unsigned char> output) {
|
||||||
|
assert(output.size() == OUTPUT_SIZE);
|
||||||
unsigned char buf[CSHA256::OUTPUT_SIZE];
|
unsigned char buf[CSHA256::OUTPUT_SIZE];
|
||||||
sha.Finalize(buf);
|
sha.Finalize(buf);
|
||||||
sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash);
|
sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
CHash256& Write(Span<const unsigned char> input) {
|
CHash256& Write(Span<const unsigned char> input) {
|
||||||
|
@ -49,10 +50,11 @@ private:
|
||||||
public:
|
public:
|
||||||
static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
|
static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
|
||||||
|
|
||||||
void Finalize(unsigned char hash[OUTPUT_SIZE]) {
|
void Finalize(Span<unsigned char> output) {
|
||||||
|
assert(output.size() == OUTPUT_SIZE);
|
||||||
unsigned char buf[CSHA256::OUTPUT_SIZE];
|
unsigned char buf[CSHA256::OUTPUT_SIZE];
|
||||||
sha.Finalize(buf);
|
sha.Finalize(buf);
|
||||||
CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash);
|
CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
CHash160& Write(Span<const unsigned char> input) {
|
CHash160& Write(Span<const unsigned char> input) {
|
||||||
|
@ -73,7 +75,7 @@ inline uint256 Hash(const T1 pbegin, const T1 pend)
|
||||||
static const unsigned char pblank[1] = {};
|
static const unsigned char pblank[1] = {};
|
||||||
uint256 result;
|
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);
|
.Finalize(result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,7 +87,7 @@ inline uint256 Hash(const T1 p1begin, const T1 p1end,
|
||||||
uint256 result;
|
uint256 result;
|
||||||
CHash256().Write({p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[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])})
|
.Write({p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0])})
|
||||||
.Finalize((unsigned char*)&result);
|
.Finalize(result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,7 +98,7 @@ inline uint160 Hash160(const T1 pbegin, const T1 pend)
|
||||||
static unsigned char pblank[1] = {};
|
static unsigned char pblank[1] = {};
|
||||||
uint160 result;
|
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);
|
.Finalize(result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,7 +137,7 @@ public:
|
||||||
// invalidates the object
|
// invalidates the object
|
||||||
uint256 GetHash() {
|
uint256 GetHash() {
|
||||||
uint256 result;
|
uint256 result;
|
||||||
ctx.Finalize((unsigned char*)&result);
|
ctx.Finalize(result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -237,7 +237,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
|
||||||
std::string str = "Bitcoin key verification\n";
|
std::string str = "Bitcoin key verification\n";
|
||||||
GetRandBytes(rnd, sizeof(rnd));
|
GetRandBytes(rnd, sizeof(rnd));
|
||||||
uint256 hash;
|
uint256 hash;
|
||||||
CHash256().Write(MakeUCharSpan(str)).Write(rnd).Finalize(hash.begin());
|
CHash256().Write(MakeUCharSpan(str)).Write(rnd).Finalize(hash);
|
||||||
std::vector<unsigned char> vchSig;
|
std::vector<unsigned char> vchSig;
|
||||||
Sign(hash, vchSig);
|
Sign(hash, vchSig);
|
||||||
return pubkey.Verify(hash, vchSig);
|
return pubkey.Verify(hash, vchSig);
|
||||||
|
|
|
@ -696,7 +696,7 @@ const uint256& V1TransportDeserializer::GetMessageHash() const
|
||||||
{
|
{
|
||||||
assert(Complete());
|
assert(Complete());
|
||||||
if (data_hash.IsNull())
|
if (data_hash.IsNull())
|
||||||
hasher.Finalize(data_hash.begin());
|
hasher.Finalize(data_hash);
|
||||||
return data_hash;
|
return data_hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -986,9 +986,9 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
|
||||||
else if (opcode == OP_SHA256)
|
else if (opcode == OP_SHA256)
|
||||||
CSHA256().Write(vch.data(), vch.size()).Finalize(vchHash.data());
|
CSHA256().Write(vch.data(), vch.size()).Finalize(vchHash.data());
|
||||||
else if (opcode == OP_HASH160)
|
else if (opcode == OP_HASH160)
|
||||||
CHash160().Write(vch).Finalize(vchHash.data());
|
CHash160().Write(vch).Finalize(vchHash);
|
||||||
else if (opcode == OP_HASH256)
|
else if (opcode == OP_HASH256)
|
||||||
CHash256().Write(vch).Finalize(vchHash.data());
|
CHash256().Write(vch).Finalize(vchHash);
|
||||||
popstack(stack);
|
popstack(stack);
|
||||||
stack.push_back(vchHash);
|
stack.push_back(vchHash);
|
||||||
}
|
}
|
||||||
|
|
|
@ -743,7 +743,7 @@ BOOST_AUTO_TEST_CASE(sha256d64)
|
||||||
in[j] = InsecureRandBits(8);
|
in[j] = InsecureRandBits(8);
|
||||||
}
|
}
|
||||||
for (int j = 0; j < i; ++j) {
|
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, 32});
|
||||||
}
|
}
|
||||||
SHA256D64(out2, in, i);
|
SHA256D64(out2, in, i);
|
||||||
BOOST_CHECK(memcmp(out1, out2, 32 * i) == 0);
|
BOOST_CHECK(memcmp(out1, out2, 32 * i) == 0);
|
||||||
|
|
|
@ -73,12 +73,12 @@ void test_one_input(const std::vector<uint8_t>& buffer)
|
||||||
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 8)) {
|
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 8)) {
|
||||||
case 0: {
|
case 0: {
|
||||||
data.resize(CHash160::OUTPUT_SIZE);
|
data.resize(CHash160::OUTPUT_SIZE);
|
||||||
hash160.Finalize(data.data());
|
hash160.Finalize(data);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 1: {
|
case 1: {
|
||||||
data.resize(CHash256::OUTPUT_SIZE);
|
data.resize(CHash256::OUTPUT_SIZE);
|
||||||
hash256.Finalize(data.data());
|
hash256.Finalize(data);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 2: {
|
case 2: {
|
||||||
|
|
|
@ -196,7 +196,7 @@ BOOST_AUTO_TEST_CASE(key_key_negation)
|
||||||
std::string str = "Bitcoin key verification\n";
|
std::string str = "Bitcoin key verification\n";
|
||||||
GetRandBytes(rnd, sizeof(rnd));
|
GetRandBytes(rnd, sizeof(rnd));
|
||||||
uint256 hash;
|
uint256 hash;
|
||||||
CHash256().Write(MakeUCharSpan(str)).Write(rnd).Finalize(hash.begin());
|
CHash256().Write(MakeUCharSpan(str)).Write(rnd).Finalize(hash);
|
||||||
|
|
||||||
// import the static test key
|
// import the static test key
|
||||||
CKey key = DecodeSecret(strSecret1C);
|
CKey key = DecodeSecret(strSecret1C);
|
||||||
|
|
|
@ -60,7 +60,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mutated |= (inner[level] == h);
|
mutated |= (inner[level] == h);
|
||||||
CHash256().Write(inner[level]).Write(h).Finalize(h.begin());
|
CHash256().Write(inner[level]).Write(h).Finalize(h);
|
||||||
}
|
}
|
||||||
// Store the resulting hash at inner position level.
|
// Store the resulting hash at inner position level.
|
||||||
inner[level] = h;
|
inner[level] = h;
|
||||||
|
@ -86,7 +86,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
|
||||||
if (pbranch && matchh) {
|
if (pbranch && matchh) {
|
||||||
pbranch->push_back(h);
|
pbranch->push_back(h);
|
||||||
}
|
}
|
||||||
CHash256().Write(h).Write(h).Finalize(h.begin());
|
CHash256().Write(h).Write(h).Finalize(h);
|
||||||
// Increment count to the value it would have if two entries at this
|
// Increment count to the value it would have if two entries at this
|
||||||
// level had existed.
|
// level had existed.
|
||||||
count += (((uint32_t)1) << level);
|
count += (((uint32_t)1) << level);
|
||||||
|
@ -101,7 +101,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
|
||||||
matchh = true;
|
matchh = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CHash256().Write(inner[level]).Write(h).Finalize(h.begin());
|
CHash256().Write(inner[level]).Write(h).Finalize(h);
|
||||||
level++;
|
level++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -216,7 +216,7 @@ BOOST_AUTO_TEST_CASE(script_standard_ExtractDestination)
|
||||||
s << OP_0 << ToByteVector(pubkey.GetID());
|
s << OP_0 << ToByteVector(pubkey.GetID());
|
||||||
BOOST_CHECK(ExtractDestination(s, address));
|
BOOST_CHECK(ExtractDestination(s, address));
|
||||||
WitnessV0KeyHash keyhash;
|
WitnessV0KeyHash keyhash;
|
||||||
CHash160().Write(pubkey).Finalize(keyhash.begin());
|
CHash160().Write(pubkey).Finalize(keyhash);
|
||||||
BOOST_CHECK(boost::get<WitnessV0KeyHash>(&address) && *boost::get<WitnessV0KeyHash>(&address) == keyhash);
|
BOOST_CHECK(boost::get<WitnessV0KeyHash>(&address) && *boost::get<WitnessV0KeyHash>(&address) == keyhash);
|
||||||
|
|
||||||
// TxoutType::WITNESS_V0_SCRIPTHASH
|
// TxoutType::WITNESS_V0_SCRIPTHASH
|
||||||
|
|
|
@ -282,7 +282,7 @@ public:
|
||||||
CScript scriptPubKey = script;
|
CScript scriptPubKey = script;
|
||||||
if (wm == WitnessMode::PKH) {
|
if (wm == WitnessMode::PKH) {
|
||||||
uint160 hash;
|
uint160 hash;
|
||||||
CHash160().Write(MakeSpan(script).subspan(1)).Finalize(hash.begin());
|
CHash160().Write(MakeSpan(script).subspan(1)).Finalize(hash);
|
||||||
script = CScript() << OP_DUP << OP_HASH160 << ToByteVector(hash) << OP_EQUALVERIFY << OP_CHECKSIG;
|
script = CScript() << OP_DUP << OP_HASH160 << ToByteVector(hash) << OP_EQUALVERIFY << OP_CHECKSIG;
|
||||||
scriptPubKey = CScript() << witnessversion << ToByteVector(hash);
|
scriptPubKey = CScript() << witnessversion << ToByteVector(hash);
|
||||||
} else if (wm == WitnessMode::SH) {
|
} else if (wm == WitnessMode::SH) {
|
||||||
|
|
|
@ -3434,7 +3434,7 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc
|
||||||
if (consensusParams.SegwitHeight != std::numeric_limits<int>::max()) {
|
if (consensusParams.SegwitHeight != std::numeric_limits<int>::max()) {
|
||||||
if (commitpos == -1) {
|
if (commitpos == -1) {
|
||||||
uint256 witnessroot = BlockWitnessMerkleRoot(block, nullptr);
|
uint256 witnessroot = BlockWitnessMerkleRoot(block, nullptr);
|
||||||
CHash256().Write(witnessroot).Write(ret).Finalize(witnessroot.begin());
|
CHash256().Write(witnessroot).Write(ret).Finalize(witnessroot);
|
||||||
CTxOut out;
|
CTxOut out;
|
||||||
out.nValue = 0;
|
out.nValue = 0;
|
||||||
out.scriptPubKey.resize(MINIMUM_WITNESS_COMMITMENT);
|
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) {
|
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__));
|
return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "bad-witness-nonce-size", strprintf("%s : invalid witness reserved value size", __func__));
|
||||||
}
|
}
|
||||||
CHash256().Write(hashWitness).Write(block.vtx[0]->vin[0].scriptWitness.stack[0]).Finalize(hashWitness.begin());
|
CHash256().Write(hashWitness).Write(block.vtx[0]->vin[0].scriptWitness.stack[0]).Finalize(hashWitness);
|
||||||
if (memcmp(hashWitness.begin(), &block.vtx[0]->vout[commitpos].scriptPubKey[6], 32)) {
|
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__));
|
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