0% found this document useful (0 votes)
5 views

Hash function

A hash function transforms input data of any length into a fixed-length output, known as a hash, which is crucial for blockchain technology, particularly in Bitcoin's Proof of Work consensus algorithm. The document outlines the properties and applications of hash functions, including their role in ensuring data integrity, digital signatures, and the structure of blockchain. It specifically discusses the SHA-256 algorithm, detailing its development, features, and the hashing process involved.

Uploaded by

chipo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Hash function

A hash function transforms input data of any length into a fixed-length output, known as a hash, which is crucial for blockchain technology, particularly in Bitcoin's Proof of Work consensus algorithm. The document outlines the properties and applications of hash functions, including their role in ensuring data integrity, digital signatures, and the structure of blockchain. It specifically discusses the SHA-256 algorithm, detailing its development, features, and the hashing process involved.

Uploaded by

chipo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Hash function

Hash pointer
One way function
What is a hash function?

• The Bitcoin network depends on a set of rules called the Proof of


Work consensus algorithm. This consensus algorithm is a set of rules that
governs a blockchain network. Outside of cryptocurrencies, the most
common usage of hash functions is in the storage of passwords.

• A hash function takes an input string (numbers, alphabets, media files) of


any length and transforms it into a fixed length.
• The fixed bit length can vary (like 32-bit or 64-bit or 128-bit or 256-bit)
depending on the hash function which is being used.
• The fixed-length output is called a hash. This hash is also the cryptographic
byproduct of a hash algorithm.
• We can understand it from the following diagram.
• Let’s go back to the very basics.
• A mathematical function is used to illustrate an expression or a
relationship involving one or more variables or sets. Consequently, a
function relates an input to an output.
• An example of this would be the size and price of burgers at a restaurant.
• The cost of a burger is determined by its size.
• Therefore, in this case, the cost is a function of the size.
• Let’s say you can buy small, medium and large burgers and their respective
costs are $1.50, $2.50 and $3.50.
• The input is the size of the burger.
• The output is the cost of the burger.
• A hash function turns an input (for example text) into a string of bytes
with a fixed length and structure. The output or value created is
called a ‘hash value’ or ‘checksum.’ Any hash value created from data
using a specific hashing algorithm is always the same length and
one-way - it cannot be reversed.
• The term "hash function" is derived from the French word “hacher”
which means to “chop into small pieces”, indicating how a hash
function is designed to “chop” the data.
• Another type of data structure, a hash table, is frequently used for
rapid detection of any two identical hashes (hash values).
History…. Of Has function
• After Diffie and Hellman first identified the need for a one-way hash
function in their 1976 seminal paper on public-key cryptography, over
the next two decades developments in cryptography progressed
rapidly.
• In 1990, cryptographer and MIT professor, Ronald Rivest, invented
the MD4 hash function and later the MD5 and MD6 functions.
• In 1995, the NSA (National Security Agency) designed the SHA-1
(Secure Hash Algorithm 1) based on Rivest’s design, followed by the
SHA-2 update in 2001.
• SHA-2 is the standard that inspired SHA-256, which served as the
baseline for Bitcoin’s consensus algorithm.
Applications of SHA algorithm
Uses of Hash Functions in Blockchain

• The blockchain has a number of different


uses for hash functions. Some of the most
common uses of the hash function in
blockchain are:
• Merkle Tree: This uses hash functions to
make sure that it is infeasible to find two
Merkle trees with the same root hash.
• This helps to protect the integrity of the
block header by storing the root hash within
the block header and thus protecting the
integrity of the transactions.
Proof of Work Consensus:
• This algorithm defines a valid block as the one whose block header
has a hash value less than the threshold value.
What Is Proof of Work?

• For example, the text “hello”, using the hash function SHA-256,
produces the hash:

• 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e730433629
38b9824

• The proof of work process persent in a network like Bitcoin starts


with a group of 4 numbers, then hashing them by iterating a nonce
multiple times until a target “difficulty” is hit. Then, the miner who
hits the target first wins the rewards.
Digital signatures:

• Hash functions are the vital part of digital signatures that ensures
data integrity and are used for authentication for blockchain
transactions.
Bitcoin transaction --- Address
• Now that you have created your public/private key pair, you are
ready to join the network and receive bitcoin. Your friend, Tom, said
he is willing to give you 10 bitcoin if you provide him your address.
• In order to create an address your public key is hashed into a
pubkeyhash and converted to a base58check format, which generates
the addresses you see today that start with a ‘1’ or ‘3’. Note again,
you can always use your private key to create a public key and you
can always use your public key to create the pubkeyhash, but you
can’t work backwards. It is a one-way street.
• You can, however, go back and forth between a pubkeyhash and a
Bitcoin address.
The chain of blocks:
• Each block header in a block in
the blockchain contains the hash
of the previous block header.
• This ensures that it is not
possible to change even a single
block in a blockchain without
being detected.
• As modifying one block requires
generating new versions of
every following block, thus
increasing the difficulty.
Properties of hash functions

• A cryptographic hash function should be computationally efficient, meaning that


it must be quick in performance to create the hash value.
• It has to be deterministic - every time you put in a certain input, it has to produce
the same output and to be pre-image resistant which means it cannot reveal any
information about the input in the output.
• Finally, a hash function has to be collision-resistant which ensures that it has to be
impossible for two different inputs to produce the same output.
• It is the deterministic feature, pre-image resistance and collision-resistance that
constitute the three most important properties of hash functions in the Bitcoin
mining process.
• The hash algorithm has certain unique properties:
• It produces a unique output (or hash).
• It is a one-way function
In Detail …
• Collision resistant: Give two messages m1 and m2, it is difficult to find a hash value such that
hash(k, m1) = hash(k, m2) where k is the key value.
• Preimage resistance: Given a hash value h, it is difficult to find a message m such that h = hash(k,
m).
• Second preimage resistance: Given a message m1, it is difficult to find another message m2 such
that hash(k, m1) = hash(k, m2).
• Large output space: The only way to find a hash collision is via a brute force search, which requires
checking as many inputs as the hash function has possible outputs.
• Deterministic: A hash function must be deterministic, which means that for any given input a hash
function must always give the same result.
• Avalanche Effect: This means for a small change in the input, the output will change significantly.
• Puzzle Friendliness: This means even if one gets to know the first 200 bytes, one cannot guess or
determine the next 56 bytes.
• Fixed-length Mapping: For any input of fixed length, the hash function will always generate the
output of the same length.
• In the context of cryptocurrencies like Bitcoin, the blockchain uses
this cryptographic hash function's properties in its consensus
mechanism.
• A cryptographic hash is a digest or digital fingerprints of a certain
amount of data.
• In cryptographic hash functions, the transactions are taken as an
input and run through a hashing algorithm which gives an output of a
fixed size
A hash function has to be collision-resistant which ensures
that it has to be impossible for two different inputs to produce
the same output.
Hash functions in the mining process

• Quickly running through the validation process in the Bitcoin network


again, a block is bundled and contains several transactions as well as
information on the previous block. This means if someone wanted to
change the ledger or double-spend a transaction, he or she would have to
change the hash in all previous blocks.

• In order for the bundled block to be added to the blockchain, miners need
to find a hash that meets the target difficulty. Each block contains a
blockheader with the number of the block, the hash of the previous block
and a “nonce”, which includes a timestamp. The purpose of a nonce is
varying the input to a cryptographic hash function, that is increased
randomness, in computation during the mining process.
SHA-256

• A Bitcoin's blockchain uses SHA-256 (Secure Hash Algorithm) hashing


algorithm. In 2001, SHA-256 Hashing algorithm was developed by the
National Security Agency (NSA) in the USA.

• Among the many advancements seen in network


security, encryption and hashing have been the core principles of
additional security modules.
• The secure hash algorithm with a digest size of 256 bits, or the SHA
256 algorithm, is one of the most widely used hash algorithms. While
there are other variants, SHA 256 has been at the forefront of
real-world applications.
What is the SHA-256 Algorithm?

• SHA 256 is a part of the SHA 2 family of algorithms, where SHA stands
for Secure Hash Algorithm. Published in 2001, it was a joint effort
between the NSA and NIST to introduce a successor to the SHA 1
family, which was slowly losing strength against brute force attacks.

• The significance of the 256 in the name stands for the final hash
digest value, i.e. irrespective of the size of plaintext/cleartext, the
hash value will always be 256 bits.
Some of the standout features of the SHA
algorithm are as follows:
• Message Length: The length of the cleartext should be less than 264
bits. The size needs to be in the comparison area to keep the digest
as random as possible.
• Digest Length: The length of the hash digest should be 256 bits in SHA
256 algorithm, 512 bits in SHA-512, and so on.
• Bigger digests usually suggest significantly more calculations at the
cost of speed and space.
• Irreversible: By design, all hash functions such as the SHA 256 are
irreversible. You should neither get a plaintext when you have the
digest beforehand nor should the digest provide its original value
when you pass it through the hash function again.
Steps in SHA-256 Algorithm

•Padding Bits
•Padding Length

•Initialising the Buffers


•Compression Functions

•Output
Process of hasing SHA256
You can divide the complete process into five different segments, as mentioned
below:
Padding Bits

• It adds some extra bits to the message, such that the length is exactly 64 bits short of a
multiple of 512.
• During the addition, the first bit should be one, and the rest of it should be filled with
zeroes.

Padding Length

• You can add 64 bits of data now to make the final plaintext a multiple
of 512.
• You can calculate these 64 bits of characters by applying the modulus
to your original cleartext without the padding.
Initialising the Buffers:

• You need to initialize the default values for eight buffers to be used in the rounds as follows:

• You also need to store 64 different keys in an array, ranging from K[0] to K[63].
They are initialized as follows:

Compression Functions

• The entire message gets broken down into multiple blocks of 512 bits each.
• It puts each block through 64 rounds of operation, with the output of each
block serving as the input for the following block.
• The entire process is as follows:

https://sha256algorithm.com/
• While the value of K[i] in all those rounds is pre-initialized, W[i] is
another input that is calculated individually for each block, depending
on the number of iterations being processed at the moment.

• With each iteration, the final output of the block serves as the input
for the next block. The entire cycle keeps repeating until you reach
the last 512-bit block, and you then consider its output the final hash
digest. This digest will be of the length 256-bit, as per the name of
this algorithm.

You might also like