
Sequentia Explores: The Mathematics of Crypto
Part III: Historical Ciphers & the Dawn of Cryptanalysis
Lecture 34: SHA-256: The Hashing Algorithm at the Heart of Bitcoin
In our last few lectures, we’ve explored the essential properties of cryptographic hash functions: they are one-way (pre-image resistant) and it’s impossible to find two inputs that produce the same output (collision resistant).
Today, we’re going to put a name and a face to this concept by looking at arguably the most famous and influential hash function in the world: SHA-256.
SHA-256 stands for Secure Hash Algorithm, 256-bit. It’s part of the SHA-2 family of algorithms, which were designed by the U.S. National Security Agency (NSA) and published by the National Institute of Standards and Technology (NIST) in 2001. While many different hash functions exist, SHA-256 has become a global workhorse, renowned for its security and efficiency.
Its most famous application? It’s the cryptographic engine that powers nearly every aspect of the Bitcoin network.
The Anatomy of a SHA-256 Hash
As its name implies, SHA-256 always produces a 256-bit output. This 256-bit binary number is typically represented as a 64-character hexadecimal string.
For example, the SHA-256 hash of the simple text “Hello Sequentia!” is:
3a4f80816e872c6114131a742c8d2d6c1f108253a63b827e6518a4a5b470377f
This long string is the unique, 256-bit “digital fingerprint” of that specific message. As we know, changing even a single character in the input will result in a completely different and unpredictable 64-character output.
How Does SHA-256 Work? (A High-Level View)
The inner workings of SHA-256 are incredibly complex, involving multiple rounds of intricate bitwise operations (like XOR, AND, NOT), bit-shifting, and modular addition. We won’t go through the full mathematical specification, but we can understand its structure conceptually.
SHA-256 is built using a design principle called the Merkle–Damgård construction. This sounds complicated, but the idea is simple: it provides a way to hash a message of any length using a function that can only process a fixed-size block of data.
This core, fixed-size function is called the compression function.
Here’s a simplified step-by-step of the process:
- Padding: The input message is first “padded” by adding specific bits to the end. This is done to ensure that the total length of the message is an exact multiple of the block size, which for SHA-256 is 512 bits. The original length of the message is also appended as part of this padding.
- Initialization: The algorithm starts with a fixed, initial hash value (called the Initialization Vector or IV). These are 8 specific 32-bit numbers, often called the “nothing-up-my-sleeve numbers,” as they are derived from the square roots of the first eight prime numbers.
- Processing in Blocks (The Compression Loop):Â The padded message is now processed one 512-bit block at a time.
- The compression function takes two inputs: the current hash value and the current 512-bit message block.
- It performs a complex series of 64 “rounds” of scrambling operations on these inputs.
- It produces a new 256-bit hash value as its output.
- Chaining: This new hash value then becomes the input hash value for the next 512-bit message block. The process repeats: the output of one block’s compression becomes the input for the next.
- Final Hash:Â After the very last message block has been processed, the final 256-bit output from the compression function is the resulting SHA-256 hash for the entire message.
This “chaining” structure is brilliant. It means that the final hash is dependent on every single block that came before it. A change in the very first block of the message will cause its output hash to change, which will then cause the next block’s output hash to change, and so on, creating an avalanche effect that completely alters the final hash digest.
SHA-256 in Bitcoin: The Unsung Hero
SHA-256 is used everywhere in the Bitcoin protocol. Its properties of pre-image resistance and collision resistance are essential for the network’s integrity and security.
- Proof-of-Work Mining: As we discussed in the last lecture, this is the most famous use. Miners are in a race to find a “nonce” (a random number) such that when they hash a block’s header data (which includes their nonce), the result is a SHA-256 hash that starts with a specific number of zeroes. Because of SHA-256’s one-way nature, this is a pure brute-force guessing game that requires immense computational “work.” In fact, Bitcoin’s proof-of-work actually applies SHA-256 twice (a process called HASH256 or SHA256d).
- Transaction Hashes (TXIDs): Every single transaction on the Bitcoin network is given a unique ID, which is the HASH256 of its data. This provides a unique, unforgeable fingerprint for every transaction.
- Merkle Trees: To efficiently summarize all the transactions in a block, their individual hashes are repeatedly hashed together in pairs, forming a structure called a Merkle tree. The final “root” of this tree—the Merkle Root—is a single hash that acts as a fingerprint for every transaction in the entire block. If even one transaction were altered, the Merkle Root would change completely.
- Generating Bitcoin Addresses:Â While not a direct use, SHA-256 is used in combination with another hash function (RIPEMD-160) as part of the process of generating a public Bitcoin address from a public key.
SHA-256 is the cryptographic glue that holds the Bitcoin blockchain together. Its one-way, deterministic, and collision-resistant properties provide the mathematical certainty needed to maintain an immutable, trustworthy ledger without a central authority.
In our next lecture, we’ll explore Salting and Key Stretching, techniques that build upon basic hashing to create highly secure systems for storing user passwords.