0

Hashing within Bitcoin is done in several different areas of the code. EG: We have the SHA-256 mining algorithm, the Merkle tree hashing, and I believe double hashing within certain areas (SHA-256D).

Hashing is all over the place in Bitcoin and I'm confused as to where the code hashes something for a particular reason.

Is there a list somewhere of all features/processes of Bitcoin that use a hash function in order to do something.

1 Answer 1

2

In Bitcoin's consensus rules (block and transaction validity rules):

  • Transactions hashes (txids and wtxids) are computed as double-SHA256 of their serialization (without and with witness data, respectively).
  • Block Merkle roots are computed by repeatedly pairwise double-SHA256 hashing, starting from the list of txids in a block, until only a single element remains. The same design is used for the witness commitment added in BIP141.
  • Block hashes are computed using double-SHA256 of the block header (which includes the Merkle root listed above). This block hash also functions as proof-of-work, so this hashing is the operation performed by miners at scale.
  • The Bitcoin scripting languages has opcodes OP_SHA256, OP_RIPEMD160, OP_HASH256 (= double-SHA256), and OP_HASH160 (SHA256 followed by RIPEMD16).
  • To compute the message being ECDSA signed in transaction inputs, double-SHA256 of a modified transaction serialization is used in both legacy and witness v0 inputs (see BIP143 for the latter).
  • In the upcoming "Taproot" protocol upgrade, single-SHA256 hashes are used for a variety of purposes: tagged hashes, script leaf hashes, script Merkle trees, and key tweaks.

In wallet standards:

  • P2PKH, P2WPKH, and P2SH addresses are computed as the RIPEMD160 hash of the SHA256 hash of the public key (resp. script) being paid to. They are verified by the OP_HASH160 opcode in the Bitcoin scripting language.
  • P2WSH addresses contain a single-SHA256 hash of the script being sent to.
  • P2PKH and P2SH addresses include a truncated (4 bytes) double-SHA256 based checksum to detect typos.
  • In the BIP32 deterministic key derivation algorithm, (an HMAC based on) SHA512 is used to compute child key tweaks and chaincodes.
  • In the BIP39 seed phrase standard single-SHA256 is used as checksum, and repeated HMAC-SHA512 is used for strengthening.

In the P2P protocol:

  • In the Bitcoin P2P protocol, a 4-byte checksum is added to every message, which is a truncated double-SHA256 hash.
  • The deprecated BIP37 Bloom filtering protocol used 32-bit MurmurHash3 to determine the bits set in Bloom filters.
  • In the BIP152 compact block protocol extension SipHash is used to compute 48-bit compact transaction hashes.
  • In the BIP158 client-side filtering protocol extension, SipHash is used to derive filter bit positions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.