Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Overview of Hashing | Blocks and Transactions
Introduction to Blockchain

bookOverview of Hashing

Hashing and block hashes have been referenced several times, but the concept itself has not yet been examined in detail. It is now time to understand what hashing actually is.

Note
Study More

Hashing is a process that takes an input and produces a fixed-size string of bytes, typically expressed as a hexadecimal number. The output, known as a hash, appears random and changes completely with any alteration to the input, a property known as the "avalanche effect".

Properties of Hash Functions

In blockchain, hash functions are used to:

  • Secure transactions by creating a unique fingerprint for each one;
  • Generate addresses from public keys;
  • Create the links in the blockchain through block hashes.

With this in mind, a good hash function for blockchain has several key properties:

Hashing, especially using a good hash function, can be compared to making a smoothie. Let's take a look at the following illustration:

Well, in fact, it is possible to find the original input given a hash, however, it would take too much time to actually be somewhat practical.

Block Hashing

In order to get the hash of a certain block, Bitcoin uses the SHA-256 hash function, which outputs a 256-bit (32-byte) hash. It's a part of the SHA-2 family designed by the National Security Agency (NSA) and known for its strong security properties, making it resistant to collisions, preimage attacks, and other cryptographic vulnerabilities.

The hash of a Bitcoin block is computed by taking the block header data. This data is input into the SHA-256 hash function twice in a process known as double SHA-256.

Here is an image to make things clear:

The resulting 256-bit hash must meet certain criteria defined by the network's difficulty target. If the hash is not below the target, the nonce is adjusted and the hash is recomputed until a qualifying hash is found. This final hash is the block's unique identifier.

Merkle Root

The Merkle root is a single hash that represents all transactions included in a block. Its computation begins with the hashes of each individual transaction, using double SHA-256.

Each transaction hash is then paired with another, and the pair is hashed together to produce a new hash. This process of pairing and hashing continues layer by layer until only one hash remains. The final hash is the Merkle root, which encapsulates the entire set of transactions and ensures their integrity by encoding them into a single value. This hierarchical structure is known as a Merkle tree.

The following example illustrates how a Merkle tree is formed:

As you can see, our block contains 6 transactions (each labeled as Tx) and via hashing and pairing new hashes a Merkle root is computed.

Note
Note

Whenever the rightmost hash does not have its pair, as with Hash56 in our example, this last hash is duplicated to create a pair. This technique maintains the integrity and efficiency of the Merkle tree structure even with odd numbers of transactions or hashes at some point.

question mark

What is the output size of the SHA-256 hash function used in Bitcoin's blockchain?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 6.25

bookOverview of Hashing

Swipe to show menu

Hashing and block hashes have been referenced several times, but the concept itself has not yet been examined in detail. It is now time to understand what hashing actually is.

Note
Study More

Hashing is a process that takes an input and produces a fixed-size string of bytes, typically expressed as a hexadecimal number. The output, known as a hash, appears random and changes completely with any alteration to the input, a property known as the "avalanche effect".

Properties of Hash Functions

In blockchain, hash functions are used to:

  • Secure transactions by creating a unique fingerprint for each one;
  • Generate addresses from public keys;
  • Create the links in the blockchain through block hashes.

With this in mind, a good hash function for blockchain has several key properties:

Hashing, especially using a good hash function, can be compared to making a smoothie. Let's take a look at the following illustration:

Well, in fact, it is possible to find the original input given a hash, however, it would take too much time to actually be somewhat practical.

Block Hashing

In order to get the hash of a certain block, Bitcoin uses the SHA-256 hash function, which outputs a 256-bit (32-byte) hash. It's a part of the SHA-2 family designed by the National Security Agency (NSA) and known for its strong security properties, making it resistant to collisions, preimage attacks, and other cryptographic vulnerabilities.

The hash of a Bitcoin block is computed by taking the block header data. This data is input into the SHA-256 hash function twice in a process known as double SHA-256.

Here is an image to make things clear:

The resulting 256-bit hash must meet certain criteria defined by the network's difficulty target. If the hash is not below the target, the nonce is adjusted and the hash is recomputed until a qualifying hash is found. This final hash is the block's unique identifier.

Merkle Root

The Merkle root is a single hash that represents all transactions included in a block. Its computation begins with the hashes of each individual transaction, using double SHA-256.

Each transaction hash is then paired with another, and the pair is hashed together to produce a new hash. This process of pairing and hashing continues layer by layer until only one hash remains. The final hash is the Merkle root, which encapsulates the entire set of transactions and ensures their integrity by encoding them into a single value. This hierarchical structure is known as a Merkle tree.

The following example illustrates how a Merkle tree is formed:

As you can see, our block contains 6 transactions (each labeled as Tx) and via hashing and pairing new hashes a Merkle root is computed.

Note
Note

Whenever the rightmost hash does not have its pair, as with Hash56 in our example, this last hash is duplicated to create a pair. This technique maintains the integrity and efficiency of the Merkle tree structure even with odd numbers of transactions or hashes at some point.

question mark

What is the output size of the SHA-256 hash function used in Bitcoin's blockchain?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4
some-alt