The first thing that comes to mind when you hear the word “hash” is probably one of two things. First would undoubtedly be hash browns. The glorious melding of potatoes, butter, and spices in a skillet to produce a crunchy, savory treat. Or, having a purposeful discussion with someone. Hashing out some problems. Although, did you know that there’s another prolific use for hashing as well called cryptographic hashing?
Before we dive in, let’s quickly recap where we’ve been on our journey of investigating Bitcoin’s relationship with cryptography. We discussed a brief history of cryptography and laid out three fundamental branches: symmetric-key cryptography, asymmetric-key (or “public-key”) cryptography, and hashing. We discussed the role of keys, their importance in Bitcoin transactions, and where they fit into the overall scheme of the blockchain. In this post, we’re going to take a look at hashing, a branch of cryptography involving one-way functions, and why they are necessary in maintaining Bitcoin’s overall infrastructure from a logical perspective.
First off, what is cryptographic hashing in the first place? A robust hash has four properties:
- quick (or efficient) to compute.
- essentially impossible to find two different messages that produce the same output.
- It is practically impossible to find any identifying characteristics from the original message.
- The hash of an input looks like a completely random output.
We don’t need to spend much more time on these other than to say that each of these forms an important part of defining a secure hash. So we know a few things about hashes; what exactly is a hash? A hashing algorithm produces a fixed-length, random output (called a “hash”) from an arbitrary-length message. Not only that, but the output makes it essentially impossible to recompute the original message – this is why hashes are sometimes referred to as “one-way” functions.
(A quick aside: you might be wondering why we’re using the term “essentially impossible”. In the realm of computational cryptography, there are always ways to eventually get a key – it might take you a billion years, but the possibility exists. That’s why we’re talking about something that is right next to impossible but not quite 100% impossible. Alright, back to the action.)
Types of Hashes
One more thing to discuss – types of hashes. There are a large number of hash types available to use, but we’ll stick to the top two families of hashing algorithms – Message Digest (MDx) and Secure Hash Algorithms (SHA-x). The MD family of hashes began in the 1980’s and has been through several iterations (MD2, MD4, MD5, and MD6). These hashing algorithms are not trusted anymore due to their inherent weaknesses.
SHA algorithms began their development in the 1990’s. These are referred to as SHA-0, SHA-1, SHA-2, and SHA-3. Each of the algorithms got progressively stronger and used progressively longer key lengths. SHA algorithms are the gold standard in industry – most hashes that you will see in any cryptographic operation nowadays are going to use SHA. Bitcoin specifically uses the SHA-2 algorithm with a 256-bit hash length. This means that any output that comes from an operation involving hashing in Bitcoin will be 256 bits long (although you’ll typically see this as a hexadecimal number that’s 64 digits long. Hexadecimal numbers use the digits 0-9 and the letters A-F).
How Does Bitcoin use Hashing
Now that we’ve laid the groundwork, let’s talk specifics. Bitcoin uses cryptographic hashing in two different places: transactions and providing a proof-of-work. Let’s discuss transactions first.
Trying to explain this part of hashing is difficult without a diagram, so feast your eyes upon the figure below.
Starting at transaction two, the current owner digitally signs a hash of transaction 1 plus the new owner’s public key. The first owner’s public key can be used to verify that the signature is valid – meaning that anyone can at any time validate the chain of transactions to ensure that everything is valid. By combining the previous transaction with the new owner’s public key, the resulting hash makes it essentially impossible to deny the new owner of his bitcoin. This aids in preventing fraud and ensures that malicious attackers can’t pilfer coins that rightfully belong to another owner by crafting hashes or public/private key pairs. When the new owner decides to spend this bitcoin, the process starts anew and continues in this deterministic and verifiable path.
Proof of Work Algorithm
We mentioned that Bitcoin also uses hashing in delivering proof-of-work. Bitcoin uses an algorithm known as “hashcash” to present a proof-of-work to other nodes on the network. Essentially, a “block” in the block chain is a collection of transactions that needs to be verified as legitimate. Each transaction isn’t verified in this process, only the block is, so block verification will take the same amount of time regardless of the total number of transactions in the block. Any node attempting to verify a block receives a “target” number. All Bitcoin nodes share this target number and attempt to provide a SHA-256 hash of a block’s header that is less than or equal to the current target number. Over time, the target decreases, increasing the difficulty of generating a new block.
Remember how we mentioned that a hash produces a random output number? This means that any node attempting to provide a hash less than the target number has to attempt millions and millions of hashes before stumbling upon a hash that is less than the target. The first one to verify a block and publish it to the network wins!
In this post, we were only able to briefly touch on the uses of cryptographic hashing in Bitcoin, but hopefully it was informative and useful for you. Want to do some more reading? Check out these links: