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

Course Content

Introduction to Blockchain

Overview of HashingOverview of Hashing

We have already mentioned hashing and block hashes a few times, but didn't really pay much attention to that, so it's time to discuss what hashing actually is.

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:

Property Description
Deterministic The same input always results in the same hash.
Fast Computation It should be quick to compute the hash for any given data.
Pre-Image Resistance Given a hash, it should be infeasible to find the original input.
Small Change, Big Difference Even a tiny change in input should produce a significantly different hash.
Collision-Resistant It should be infeasible to find two different inputs that produce the same hash.

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

As we have mentioned before, Merkle root is a single hash that represents all the transactions included in the block. Computation of a Merkle root starts with the hashes of each transaction in a block. Double SHA-256 is used here as well.

Each hash is then paired with another and they are hashed together to form a new hash. This process of pairing and hashing the new hashes continues until only one hash remains. The final hash is the Merkle root, representing all transactions in the block and ensuring their integrity by encoding the entire set of transactions into a single hash. Such process forms a Merkle tree.

Let's take a look at an example of a Merkle tree:

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.

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.

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

Select the correct answer

Everything was clear?

Section 2. Chapter 4
course content

Course Content

Introduction to Blockchain

Overview of HashingOverview of Hashing

We have already mentioned hashing and block hashes a few times, but didn't really pay much attention to that, so it's time to discuss what hashing actually is.

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:

Property Description
Deterministic The same input always results in the same hash.
Fast Computation It should be quick to compute the hash for any given data.
Pre-Image Resistance Given a hash, it should be infeasible to find the original input.
Small Change, Big Difference Even a tiny change in input should produce a significantly different hash.
Collision-Resistant It should be infeasible to find two different inputs that produce the same hash.

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

As we have mentioned before, Merkle root is a single hash that represents all the transactions included in the block. Computation of a Merkle root starts with the hashes of each transaction in a block. Double SHA-256 is used here as well.

Each hash is then paired with another and they are hashed together to form a new hash. This process of pairing and hashing the new hashes continues until only one hash remains. The final hash is the Merkle root, representing all transactions in the block and ensuring their integrity by encoding the entire set of transactions into a single hash. Such process forms a Merkle tree.

Let's take a look at an example of a Merkle tree:

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.

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.

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

Select the correct answer

Everything was clear?

Section 2. Chapter 4
some-alt