mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-03 09:56:38 -05:00
refactor: Extract RIPEMD160
To directly return a CRIPEMD160 hash from data. Incidentally, decoding this acronym: * RIPEMD -> RIPE Message Digest * RIPE -> RACE Integrity Primitives Evaluation * RACE -> Research and Development in Advanced Communications Technologies in Europe
This commit is contained in:
parent
7386da7a0b
commit
6879be691b
8 changed files with 27 additions and 20 deletions
|
@ -18,7 +18,7 @@
|
|||
/* Number of bytes to hash per iteration */
|
||||
static const uint64_t BUFFER_SIZE = 1000*1000;
|
||||
|
||||
static void RIPEMD160(benchmark::Bench& bench)
|
||||
static void BenchRIPEMD160(benchmark::Bench& bench)
|
||||
{
|
||||
uint8_t hash[CRIPEMD160::OUTPUT_SIZE];
|
||||
std::vector<uint8_t> in(BUFFER_SIZE,0);
|
||||
|
@ -150,7 +150,7 @@ static void MuHashPrecompute(benchmark::Bench& bench)
|
|||
});
|
||||
}
|
||||
|
||||
BENCHMARK(RIPEMD160, benchmark::PriorityLevel::HIGH);
|
||||
BENCHMARK(BenchRIPEMD160, benchmark::PriorityLevel::HIGH);
|
||||
BENCHMARK(SHA1, benchmark::PriorityLevel::HIGH);
|
||||
BENCHMARK(SHA256, benchmark::PriorityLevel::HIGH);
|
||||
BENCHMARK(SHA512, benchmark::PriorityLevel::HIGH);
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <crypto/sha256.h>
|
||||
#include <prevector.h>
|
||||
#include <serialize.h>
|
||||
#include <span.h>
|
||||
#include <uint256.h>
|
||||
#include <version.h>
|
||||
|
||||
|
@ -223,4 +224,12 @@ void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char he
|
|||
*/
|
||||
HashWriter TaggedHash(const std::string& tag);
|
||||
|
||||
/** Compute the 160-bit RIPEMD-160 hash of an array. */
|
||||
inline uint160 RIPEMD160(Span<const unsigned char> data)
|
||||
{
|
||||
uint160 result;
|
||||
CRIPEMD160().Write(data.data(), data.size()).Finalize(result.begin());
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif // BITCOIN_HASH_H
|
||||
|
|
|
@ -4,11 +4,13 @@
|
|||
|
||||
#include <script/descriptor.h>
|
||||
|
||||
#include <hash.h>
|
||||
#include <key_io.h>
|
||||
#include <pubkey.h>
|
||||
#include <script/miniscript.h>
|
||||
#include <script/script.h>
|
||||
#include <script/standard.h>
|
||||
#include <uint256.h>
|
||||
|
||||
#include <span.h>
|
||||
#include <util/bip32.h>
|
||||
|
@ -1618,8 +1620,7 @@ std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptCo
|
|||
}
|
||||
}
|
||||
if (txntype == TxoutType::WITNESS_V0_SCRIPTHASH && (ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH)) {
|
||||
CScriptID scriptid;
|
||||
CRIPEMD160().Write(data[0].data(), data[0].size()).Finalize(scriptid.begin());
|
||||
CScriptID scriptid{RIPEMD160(data[0])};
|
||||
CScript subscript;
|
||||
if (provider.GetCScript(scriptid, subscript)) {
|
||||
auto sub = InferScript(subscript, ParseScriptContext::P2WSH, provider);
|
||||
|
|
|
@ -285,7 +285,6 @@ static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator
|
|||
std::vector<valtype>& ret, TxoutType& whichTypeRet, SigVersion sigversion, SignatureData& sigdata)
|
||||
{
|
||||
CScript scriptRet;
|
||||
uint160 h160;
|
||||
ret.clear();
|
||||
std::vector<unsigned char> sig;
|
||||
|
||||
|
@ -314,8 +313,8 @@ static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator
|
|||
ret.push_back(ToByteVector(pubkey));
|
||||
return true;
|
||||
}
|
||||
case TxoutType::SCRIPTHASH:
|
||||
h160 = uint160(vSolutions[0]);
|
||||
case TxoutType::SCRIPTHASH: {
|
||||
uint160 h160{vSolutions[0]};
|
||||
if (GetCScript(provider, sigdata, CScriptID{h160}, scriptRet)) {
|
||||
ret.push_back(std::vector<unsigned char>(scriptRet.begin(), scriptRet.end()));
|
||||
return true;
|
||||
|
@ -323,7 +322,7 @@ static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator
|
|||
// Could not find redeemScript, add to missing
|
||||
sigdata.missing_redeem_script = h160;
|
||||
return false;
|
||||
|
||||
}
|
||||
case TxoutType::MULTISIG: {
|
||||
size_t required = vSolutions.front()[0];
|
||||
ret.push_back(valtype()); // workaround CHECKMULTISIG bug
|
||||
|
@ -349,8 +348,7 @@ static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator
|
|||
return true;
|
||||
|
||||
case TxoutType::WITNESS_V0_SCRIPTHASH:
|
||||
CRIPEMD160().Write(vSolutions[0].data(), vSolutions[0].size()).Finalize(h160.begin());
|
||||
if (GetCScript(provider, sigdata, CScriptID{h160}, scriptRet)) {
|
||||
if (GetCScript(provider, sigdata, CScriptID{RIPEMD160(vSolutions[0])}, scriptRet)) {
|
||||
ret.push_back(std::vector<unsigned char>(scriptRet.begin(), scriptRet.end()));
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include <script/interpreter.h>
|
||||
#include <script/keyorigin.h>
|
||||
#include <script/standard.h>
|
||||
#include <uint256.h>
|
||||
|
||||
class CKey;
|
||||
class CKeyID;
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include <clientversion.h>
|
||||
#include <core_io.h>
|
||||
#include <fs.h>
|
||||
#include <hash.h>
|
||||
#include <interfaces/chain.h>
|
||||
#include <key_io.h>
|
||||
#include <merkleblock.h>
|
||||
|
@ -14,6 +15,7 @@
|
|||
#include <script/script.h>
|
||||
#include <script/standard.h>
|
||||
#include <sync.h>
|
||||
#include <uint256.h>
|
||||
#include <util/bip32.h>
|
||||
#include <util/system.h>
|
||||
#include <util/time.h>
|
||||
|
@ -885,9 +887,7 @@ static std::string RecurseImportData(const CScript& script, ImportData& import_d
|
|||
}
|
||||
case TxoutType::WITNESS_V0_SCRIPTHASH: {
|
||||
if (script_ctx == ScriptContext::WITNESS_V0) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2WSH inside another P2WSH");
|
||||
uint256 fullid(solverdata[0]);
|
||||
CScriptID id;
|
||||
CRIPEMD160().Write(fullid.begin(), fullid.size()).Finalize(id.begin());
|
||||
CScriptID id{RIPEMD160(solverdata[0])};
|
||||
auto subscript = std::move(import_data.witnessscript); // Remove redeemscript from import_data to check for superfluous script later.
|
||||
if (!subscript) return "missing witnessscript";
|
||||
if (CScriptID(*subscript) != id) return "witnessScript does not match the scriptPubKey or redeemScript";
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <core_io.h>
|
||||
#include <hash.h>
|
||||
#include <key_io.h>
|
||||
#include <rpc/util.h>
|
||||
#include <util/moneystr.h>
|
||||
|
@ -679,8 +680,7 @@ RPCHelpMan listunspent()
|
|||
CHECK_NONFATAL(extracted);
|
||||
// Also return the witness script
|
||||
const WitnessV0ScriptHash& whash = std::get<WitnessV0ScriptHash>(witness_destination);
|
||||
CScriptID id;
|
||||
CRIPEMD160().Write(whash.begin(), whash.size()).Finalize(id.begin());
|
||||
CScriptID id{RIPEMD160(whash)};
|
||||
CScript witnessScript;
|
||||
if (provider->GetCScript(id, witnessScript)) {
|
||||
entry.pushKV("witnessScript", HexStr(witnessScript));
|
||||
|
@ -689,8 +689,7 @@ RPCHelpMan listunspent()
|
|||
}
|
||||
} else if (scriptPubKey.IsPayToWitnessScriptHash()) {
|
||||
const WitnessV0ScriptHash& whash = std::get<WitnessV0ScriptHash>(address);
|
||||
CScriptID id;
|
||||
CRIPEMD160().Write(whash.begin(), whash.size()).Finalize(id.begin());
|
||||
CScriptID id{RIPEMD160(whash)};
|
||||
CScript witnessScript;
|
||||
if (provider->GetCScript(id, witnessScript)) {
|
||||
entry.pushKV("witnessScript", HexStr(witnessScript));
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <hash.h>
|
||||
#include <key_io.h>
|
||||
#include <logging.h>
|
||||
#include <outputtype.h>
|
||||
|
@ -166,9 +167,7 @@ IsMineResult IsMineInner(const LegacyScriptPubKeyMan& keystore, const CScript& s
|
|||
if (sigversion == IsMineSigVersion::TOP && !keystore.HaveCScript(CScriptID(CScript() << OP_0 << vSolutions[0]))) {
|
||||
break;
|
||||
}
|
||||
uint160 hash;
|
||||
CRIPEMD160().Write(vSolutions[0].data(), vSolutions[0].size()).Finalize(hash.begin());
|
||||
CScriptID scriptID = CScriptID(hash);
|
||||
CScriptID scriptID{RIPEMD160(vSolutions[0])};
|
||||
CScript subscript;
|
||||
if (keystore.GetCScript(scriptID, subscript)) {
|
||||
ret = std::max(ret, recurse_scripthash ? IsMineInner(keystore, subscript, IsMineSigVersion::WITNESS_V0) : IsMineResult::SPENDABLE);
|
||||
|
|
Loading…
Add table
Reference in a new issue