What Is A Cryptographic Hash?
June 9, 2017
Cryptographic hashes play a fundamental role in the structure of blockchains, which is what Bitcoin is built on.
Essentially, hashes act as digital fingerprints for data. If you have a person and a fingerprint, it is easy to verify that the fingerprint belongs to the person, but a fingerprint alone tells you very little about someone. We will later see that this property is useful for verifying the authenticity of a blockchain.
Let’s explore this more formally in this pre-introductory post to Bitcoin.
- What’s A Hash Function?
- What Is A Cryptographic Hash Function
What’s A Hash Function?
A hash is a function which satisfies the three following properties.
The input space X is the set of strings of arbitrary length.
This means that ‘helloworld’ and ‘helloworldhelloworldhelloworld’ are both perfectly fine inputs.
The output space Y is a set of strings fixed length.
Regardless of their different lengths, both inputs would map to something of the same length, say and .
Bitcoin uses SHA256 as its hash function. The 256 part stands for the number of bits in its fixed-sized output.
The hash function $H$ is efficient to compute.
Given a string of length , the complexity to produce is .
This ease of computation is what give blockchains the property of being easily verifiable to prevent tampering. As we will see later, we can verify if a block is “authentic” in a blockchain by simply computing the hash of a block, which is a cost-effective operation (computation-wise).
What Is A Cryptographic Hash Function
In addition to the three properties of hash functionslisted above, cryptographic hash functions have the following additional properties.
A collision happens when a hash function maps two different inputs to the same output.So collisions happen on points where the function isn’t injective.
Since the target space is of a fixed sized (say 256 bits) while the input size is infinite, a hash function cannot be truly injective by the Pigeonhole principleThe principle that if I throw 4 balls in 3 holes, one hole is going to have more than 1 ball. . A collision resistant function is one where it is computationally infeasible to find a collision. What is infeasible, of course, changes with technological progress.
For a “good” hash function with 256-bit output (which is what Bitcoin uses), it takes computations on average to produce a collision. If a computer calculates 10,000 hashes per second, it would take one octillion () years to calculate hashes in order to produce a collision. Source.
A example of a hash function where it’s feasible to find a collision is . This function satisfies our fixed length property, but it is easy to find a collision as is simply , so each number which differs by an integer multiple of collide once they are mapped by this hash function.
As of now, no function has been proven to be collision resistant yetIn fact, we have been wrong in the past. The MD5 function was assumed to be collision resistant but after a couple years people have found collisions. What is computationally costly in the past won’t be as much in the future. , so the cryptographic functions we are using nowadays are functions for which we have yet to find collisions for.
Application: Collision resistance is what gives cryptographic hash the property of acting like fingerprints/identifiers of data. Although we cannot usually deduce from the hash what the data looks like (as we will see below), the data is (until proven otherwise) uniquely identified to its hash function, which is of fixed length. As we will see later, this allows us to identify blocks in the blockchain of Bitcoin by hashing them. It also makes the blocks resistant to tampering since changing the data changes the hash.
Intuitively, we want the hash functions to “hide” the space of practical inputs. Given the hash , an attacker should have to generate the hash of every in the input space randomly in order to be able to find which generated . This makes it infeasible to predict which input will result in the right hash.
For example, if the information we are hashing is of the form “Alice pays Bob”, “Bob pays Eve”, then the input space is easily guessable to be of the form “Person1 pays Person2”.
This is a situation we want to avoid, because it makes computing inverse images becomes easy: generate all the hashes of the form (assuming strings of these form are finite), and we now have a lookup table of every input-output pair.
To remedy the situation, each time we want to input to our hash function, we must pick random fixed length string (say a 256-bit one) from a “spread out” (high min-entropy) probability distribution. In Bitcoin, this random string is called the nonce, for number used once. We then concatenate our string with . From this concatenation, we have spread our space of practical input which was low entropy (high information) to a space with high entropy (low information).
Given a hash function , we will now write when we really mean with the symbol denoting concatenation. Now given , it is infeasible to find because there are too many combinations to try out. Constructing a lookup table is now unfeasible.
Application: The hiding property allows us to make a commitment scheme. Given a message , we may send a hash of the message to announce a commitment to this message without revealing what it is. In other words, given we cannot figure out what is by the hiding property. Then, once we are ready to reveal the message, we can declare it along with the nonce, meaning we are declaring the tuple . Then, someone can compute the hash of the nonce and the message in linear time, confirming that this message was indeed the one committed. By the collision-free property, we may not have edited our message because it infeasible to find a tuple which hash to the same thing.
We have just seen that given an output, we want to make it difficult to find the input.
Puzzle friendliness is the property that given part of the input which is picked from a high min-entropy distribution and an -bit output , it is infeasible to find so that in time significantly less than .
Basically, even given part of the input and the output, it is very difficult for us to find the rest of the input.
Application: As we will see later, this property is central to Bitcoin mining, which is essentially a race to solve a cryptographic puzzle by essentially trying out solutions uniformly randomly. Bitcoin achieves its distributed consensus by allowing the first person to solve the puzzle to decide the next block. This why having access to more computational power increases your leverage in Bitcoin.
As computational power increases over time, it gets easier and easier to solve Bitcoin’s cryptographic puzzle. Moore’s law states that computational power increases as , where is time. Puzzle friendliness allows us to keep the required time to solve Bitcoin’s cryptographic puzzle roughly constant by increasing the length of , the hash output we want to find in the puzzle. By definition of puzzle friendliness, the difficulty of the puzzle evolves as where is the length of . This cancels the effect of Moore’s law, as well as bigger players entering the Bitcoin ecosystem. In fact, Bitcoin keeps the average time to mine a new block to 10 minutes.
That’s all for now! In a next post, we will see how these properties of the crytographic hash make the hash pointer in a blockchain resistant to tempering, a property that is fundamental to the Bitcoin system. Stay tuned!
In summary, the hash function has these three properties.
- Takes as input a string of arbitrary length,
- Returns as output a string of fixed length,
- The output of the function is easy to compute relative to the length of the input.
A cryptographic hash has these three additional properties.
- Collision resistance: we are very unlikely to find two inputs which give the same output, no matter how hard we try.
- Hiding: given a hash, it is infeasible to find its associated input and the optimal way to do so is to try every possibility uniformly randomly.
- Puzzle friendliness: given a hash and part of the input that has been chosen in a suitably random way, the optimal way to find the rest of the input is still to try every possibility randomly. The difficulty of that task is exponential in the length of the hash, making it easy to adjust the difficulty of the puzzle by varying the restriction on the desired hash output.
Bitcoin and Cryptocurrency Technologies great theoretical textbook on Bitcoin with free lectures available.
This post was written from the perspective of a mathy beginner.
Special thanks to Jake Hickey for guiding through my cryptocurrency learning and providing valuable feedback for this blog post.