How Bitcoin’s hash functions work
Hash functions are a crucial part in a cryptocurrency protocol and in security in general.
A hash function is a mathematical process that intakes data of any size, process the data and returns an output of a fixed size. The input data can be virtually anything – a single letter, a word or the entire collection of the British Library. After being processed, that data, no matter its size, will produce a same length of letters and numbers, an alphanumeric string, that is unique for that input. The alphanumeric string is called a digest.
To give you an idea on how hashing works, we ran a few phrases through the SHA256 function (the same one the Bitcoin network uses)
As you can see, all we did was capitalize a single letter in our input and got radically different results.
Here is the hash digest for this entire article and how it compares to the single letter I.
Hash(“I”) – a83dd0ccbffe39d071cc317ddf6e97f5c6b1c87af91919271f9fa140b0508c6c
Of course, as mentioned before, the digest has the exact same lenght, even though the lenght of the input is very different.
Hash functions are used to store passwords. When you create a password, the server never stores the actual password on their servers. How then can they tell if the password you introduced is the correct one, without comparing the two? The server will store the hash digest. When you login, your password goes through a hashing process, generates an output, and that output is compared to the hash digest stored on the server. If they match, you are granted access to your account.
This ensures that if a hacker manages to access the database that contains all the hashes from the users of a service, he will only get the digests, not the actual passwords. Therefore, he will not be able to compromise the account of the users, because there is no easy way to find the password that produces a certain hash.
Bitcoin’s hash functions
Hashing algorithms are part of the bitcoin protocol. They are used to write new transactions in the blockchain.
The input for Bitcoin’s hash function is represented by all the recent transactions that have not yet been confirmed, along with an extra input tied to a time stamp and to the reference to a previous block. As you can see, the results of a hash changes with the slightest change to the input.
In order to be granted a block reward, miners have to run these inputs with their own, different input, in such a way that the resulting hash contains a certain numbers of zeros. It is a repetitive task made automatically by miners. Once they find that input, they practically mined a block. This takes processing power from the part of the miners and an expenditure of electricity.
To give you an idea of how mining works, we will try to hash a mockup of a transaction (the phrase ICONetworkTransactions123), along with today’s date (26th of April 2017), and a mockup reference to a previous block (xyz). To this, we will add a random extra letter, until we obtain an even number at the beginning of the hash function.
hash(“ICONetworkTransactions123 26th of April 2017 xyzA”)
hash(“ICONetworkTransactions123 26th of April 2017 xyzB”)
hash(“ICONetworkTransactions123 26th of April 2017 xyzF”)
With a small challenge of obtaining an even number at the beginning of the digest, it still took us six guesses.
Of course, solving the hash for a bitcoin block requires great processing power. The combined power of all the devices connected to the network takes aproximately 10 minutes to solve a block. This ensures that newly generated bitcoins are minted over a longer period of time, instead of all at once.
The numbers of zeros required at the beginning of the hash digest depends on the mining difficulty. The more processing power the network has, the more the difficulty raises. This is a clever mechanism that ensures that, the more miners join, the harder it becomes for any one miner to find the block. This aspect contributes to the deflationary value of bitcoin. At the moment of writing this article, the hash digest is required to have 19 zeros at the beginning.
After a miner finds the hash of a block, it simply adds it to a permanent register, listing all of the hash digests of blocks mined previously. This way, the blockchain is extended. In return, according to the protocol, the miner is granted a bitcoin reward (after the 2016 halving, the reward for every solved block is 12 Bitcoins). New Bitcoins enter the market through this mining process.
How Bitcoin’s hash functions work