<h4>In consensus rules:</h4>
<ul>
<li>There are script opcodes that perform hash operations on the stack:
<ul>
<li><code>OP_SHA256</code>: SHA256</li>
<li><code>OP_RIPEMD160</code>: RIPEMD160</li>
<li><code>OP_SHA1</code>: SHA1</li>
<li><code>OP_HASH160</code>: SHA256 followed by RIPEMD160 (very commonly used, in P2PKH and <a href="https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki" rel="noreferrer">P2SH</a> scripts)</li>
<li><code>OP_HASH256</code>: double SHA256</li>
</ul>
</li>
<li>The signature verification opcodes (<code>OP_CHECKSIG(VERIFY)</code>, <code>OP_CHECKMULTISIG(VERIFY)</code>, <code>OP_CHECKSIGADD</code>) compute a &quot;sighash&quot; operation on the spending transaction and output being spent.
<ul>
<li>In pre-segwit scripts, this is a double-SHA256 hash of a modified version of the spending transaction.</li>
<li>In <a href="https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki" rel="noreferrer">witness v0 scripts</a>, this is a double-SHA256 of a concatenation of specific data, including certain precomputed transaction-wide values (which themselves are double-SHA256 hashes).</li>
<li>In <a href="https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki" rel="noreferrer">witness v1 (taproot) scripts</a>, this is a tagged single SHA256 hash of a different concatenation of specific data, including certain precomputed transaction-wide values (which themselves are single SHA256 hashes).</li>
</ul>
</li>
<li>In taproot spends, the signature check opcodes perform <a href="https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki" rel="noreferrer">BIP340</a> (Schnorr) signature checking, which internally involves a tagged single SHA256 operation.</li>
<li><a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki" rel="noreferrer">P2WPKH</a> outputs are a SHA256+RIPEMD160 hash of the public key.</li>
<li><a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki" rel="noreferrer">P2WSH</a> outputs are a single SHA256 hash of the script.</li>
<li>In <a href="https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki" rel="noreferrer">taproot script path spends</a>:
<ul>
<li>a tagged single SHA256 operation is used to compute the tweak.</li>
<li>a tagged single SHA256 operation is used in each level of the script tree.</li>
<li>a tagged single SHA256 operation is used to compute the script leaf hash.</li>
</ul>
</li>
<li>The transaction id (txid) and <a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki" rel="noreferrer">witness transaction id</a> (wtxid) are computed as double-SHA256 hashes of the serialized transaction, without and with witness respectively.</li>
<li>The transaction merkle root of a transaction is computed by repeatedly applying double-SHA256 on groups of two txids.</li>
<li>The transaction <a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki" rel="noreferrer">witness merkle root</a> in the coinbase is computed by repeatedly applying double-SHA256 on groups of two wtxids (excluding the coinbase transaction).</li>
<li>The block hash is the double SHA256 hash of the block header.</li>
</ul>
<h4>In the peer to peer protocol:</h4>
<ul>
<li>Every v1 P2P message has a checksum, which is a truncated double-SHA256 hash of the message's payload.</li>
<li>The encrypted v2 transport protocol introduced in <a href="https://github.com/bitcoin/bips/blob/master/bip-0324.mediawiki" rel="noreferrer">BIP324</a> has no checksums anymore, but does involve several SHA256 hashes in its handshake and key setup.</li>
<li>The deprecated <a href="https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki" rel="noreferrer">BIP37</a> (server side filtering) protocol uses Bloom filters, insertions to which use MurmurHash3.</li>
<li>The <a href="https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki" rel="noreferrer">BIP158</a> (client side filtering) filters use a Golomb-coded set (GCS), insertions to which are done using SipHash-2-4.</li>
<li><a href="https://github.com/bitcoin/bips/blob/master/bip-0152.mediawiki" rel="noreferrer">BIP152</a> (compact blocks) uses 48-bit short ids for transactions, which are computed using SipHash-2-4, with a key that is computed using truncated SHA256 of the block hash plus a nonce.</li>
<li>Probably several more.</li>
</ul>
<h4>In wallets:</h4>
<ul>
<li>P2PKH and <a href="https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki" rel="noreferrer">P2SH</a> addresses contain a truncated double-SHA256 checksum, in addition to a SHA256+RIPEMD160 hash of the public key or script, matching the consensus ones.</li>
<li>P2WPKH, P2WSH, and P2TR addresses do not use a hash in their <a href="https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki" rel="noreferrer">checksum</a>, but do typically involve hashes matching the consensus side (SHA256 of script for P2WSH, SHA256+RIPEMD160 for P2WPKH, tweaks/leafs/script tree for P2TR, though it is possible to construct an untweaked P2TR output without any hashes).</li>
<li>The <a href="https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki" rel="noreferrer">BIP32</a> standard for deterministic key derivation involves an HMAC-SHA512 step per tree level, and an HMAC-SHA512 step to go from seed to master key. Its xpub and xprv formats also involve a truncated SHA256+RIPEMD160 fingerprint, and a truncated SHA256 checksum.</li>
<li>The <a href="https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki" rel="noreferrer">BIP39</a> standard involves PBKDF2 strengthening, using HMAC-SHA512 as hashing step, to go from mnemonic to seed. A truncated single SHA256 hash is also used as checksum.</li>
<li>Most ECDSA signing implementations use <a href="https://datatracker.ietf.org/doc/html/rfc6979" rel="noreferrer">RFC6979</a> to generate the nonce, which involves several invocations of HMAC-SHA256.</li>
<li>The <a href="https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki" rel="noreferrer">BIP340</a> default signing algorithm involves a tagged SHA256 hash to generate the nonce.</li>
<li>Signing involves computing all the hashes that will be relevant at verification time (leaf hashes, script trees, signature hashes, tweaks, script/pubkey hashes, hashes inside signature, ...; see above under consensus rules).</li>
</ul>
<h4>In the Lightning network</h4>
<p>I'm not an expert on this matter, so I'll leave this for another answer (or, suggestions welcome).</p>
<h4>In node implementations</h4>
<p>The details are up to individual implementations, and not part of any protocol. I'm just listing things off the top of my head that are used in Bitcoin Core.</p>
<ul>
<li>Secure random number generation often involves hashes to maintain an entropy pool, and/or to combine multiple entropy sources. Bitcoin Core uses its own SHA512-based implementation here.</li>
<li>Bloom filters are extensively used to remember what information peers already have, which involve hash functions. Bitcoin Core uses MurmurHash3 here.</li>
<li>Hash tables for various data structures inherently involve hash functions. Bitcoin Core uses SipHash-2-4 for most non-trivial hash tables.</li>
</ul>

CC BY-SA 4.0
2023-11-12T22:52:26.567Z
Thanks for the detailed insight on how hash functions are integral to Bitcoin. Their versatility is truly impressive, from securing transactions to enabling the mining process. It’s remarkable how they ensure integrity and security in Bitcoin’s network, making the system robust and trustworthy. Hash functions are indeed a cornerstone of Bitcoin’s innovative architecture. Need to read more about it.
BitcoinFanatic
CC BY-SA 4.0
2023-11-11T03:01:35.173Z
2023-11-11T17:03:35.943Z
2023-11-11T17:03:35.943Z
Pieter Wuille
<p>What are hash functions used for in bitcoin?</p>
<p>I am looking for a comprehensive list.</p>

<hash>
What are hash functions used for in bitcoin?
BitcoinFanatic
# What are hash functions used for in bitcoin?

(Asked by: username_0 on 2023-11-10)

What are hash functions used for in bitcoin?

I am looking for a comprehensive list.

**Tags:** hash


---

## 1 answers

### Answer by username_1 on 2023-11-11. Score: 8 | Accepted answer

#### In consensus rules:

* There are script opcodes that perform hash operations on the stack:
  + `OP_SHA256`: SHA256
  + `OP_RIPEMD160`: RIPEMD160
  + `OP_SHA1`: SHA1
  + `OP_HASH160`: SHA256 followed by RIPEMD160 (very commonly used, in P2PKH and [P2SH](https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki) scripts)
  + `OP_HASH256`: double SHA256
* The signature verification opcodes (`OP_CHECKSIG(VERIFY)`, `OP_CHECKMULTISIG(VERIFY)`, `OP_CHECKSIGADD`) compute a "sighash" operation on the spending transaction and output being spent.
  + In pre-segwit scripts, this is a double-SHA256 hash of a modified version of the spending transaction.
  + In [witness v0 scripts](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki), this is a double-SHA256 of a concatenation of specific data, including certain precomputed transaction-wide values (which themselves are double-SHA256 hashes).
  + In [witness v1 (taproot) scripts](https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki), this is a tagged single SHA256 hash of a different concatenation of specific data, including certain precomputed transaction-wide values (which themselves are single SHA256 hashes).
* In taproot spends, the signature check opcodes perform [BIP340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki) (Schnorr) signature checking, which internally involves a tagged single SHA256 operation.
* [P2WPKH](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki) outputs are a SHA256+RIPEMD160 hash of the public key.
* [P2WSH](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki) outputs are a single SHA256 hash of the script.
* In [taproot script path spends](https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki):
  + a tagged single SHA256 operation is used to compute the tweak.
  + a tagged single SHA256 operation is used in each level of the script tree.
  + a tagged single SHA256 operation is used to compute the script leaf hash.
* The transaction id (txid) and [witness transaction id](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki) (wtxid) are computed as double-SHA256 hashes of the serialized transaction, without and with witness respectively.
* The transaction merkle root of a transaction is computed by repeatedly applying double-SHA256 on groups of two txids.
* The transaction [witness merkle root](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki) in the coinbase is computed by repeatedly applying double-SHA256 on groups of two wtxids (excluding the coinbase transaction).
* The block hash is the double SHA256 hash of the block header.

#### In the peer to peer protocol:

* Every v1 P2P message has a checksum, which is a truncated double-SHA256 hash of the message's payload.
* The encrypted v2 transport protocol introduced in [BIP324](https://github.com/bitcoin/bips/blob/master/bip-0324.mediawiki) has no checksums anymore, but does involve several SHA256 hashes in its handshake and key setup.
* The deprecated [BIP37](https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki) (server side filtering) protocol uses Bloom filters, insertions to which use MurmurHash3.
* The [BIP158](https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki) (client side filtering) filters use a Golomb-coded set (GCS), insertions to which are done using SipHash-2-4.
* [BIP152](https://github.com/bitcoin/bips/blob/master/bip-0152.mediawiki) (compact blocks) uses 48-bit short ids for transactions, which are computed using SipHash-2-4, with a key that is computed using truncated SHA256 of the block hash plus a nonce.
* Probably several more.

#### In wallets:

* P2PKH and [P2SH](https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki) addresses contain a truncated double-SHA256 checksum, in addition to a SHA256+RIPEMD160 hash of the public key or script, matching the consensus ones.
* P2WPKH, P2WSH, and P2TR addresses do not use a hash in their [checksum](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki), but do typically involve hashes matching the consensus side (SHA256 of script for P2WSH, SHA256+RIPEMD160 for P2WPKH, tweaks/leafs/script tree for P2TR, though it is possible to construct an untweaked P2TR output without any hashes).
* The [BIP32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki) standard for deterministic key derivation involves an HMAC-SHA512 step per tree level, and an HMAC-SHA512 step to go from seed to master key. Its xpub and xprv formats also involve a truncated SHA256+RIPEMD160 fingerprint, and a truncated SHA256 checksum.
* The [BIP39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) standard involves PBKDF2 strengthening, using HMAC-SHA512 as hashing step, to go from mnemonic to seed. A truncated single SHA256 hash is also used as checksum.
* Most ECDSA signing implementations use [RFC6979](https://datatracker.ietf.org/doc/html/rfc6979) to generate the nonce, which involves several invocations of HMAC-SHA256.
* The [BIP340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki) default signing algorithm involves a tagged SHA256 hash to generate the nonce.
* Signing involves computing all the hashes that will be relevant at verification time (leaf hashes, script trees, signature hashes, tweaks, script/pubkey hashes, hashes inside signature, ...; see above under consensus rules).

#### In the Lightning network

I'm not an expert on this matter, so I'll leave this for another answer (or, suggestions welcome).

#### In node implementations

The details are up to individual implementations, and not part of any protocol. I'm just listing things off the top of my head that are used in Bitcoin Core.

* Secure random number generation often involves hashes to maintain an entropy pool, and/or to combine multiple entropy sources. Bitcoin Core uses its own SHA512-based implementation here.
* Bloom filters are extensively used to remember what information peers already have, which involve hash functions. Bitcoin Core uses MurmurHash3 here.
* Hash tables for various data structures inherently involve hash functions. Bitcoin Core uses SipHash-2-4 for most non-trivial hash tables.

> **Comments:**
> **username_0**: Thanks for the detailed insight on how hash functions are integral to Bitcoin. Their versatility is truly impressive, from securing transactions to enabling the mining process. It’s remarkable how they ensure integrity and security in Bitcoin’s network, making the system robust and trustworthy. Hash functions are indeed a cornerstone of Bitcoin’s innovative architecture. Need to read more about it.  

---

