How Blockchain Works | Bitcoin's Algorithm Explained
3 min read · · Korean original

At the core of every blockchain is its mining algorithm, so let's use Bitcoin's algorithm as an example. SHA-256, short for "Secure Hash Algorithm 256 bits," accepts input of any length: text, numbers, or even a computer file of any size. The output is called a "hash," and it always has a fixed length equal to 256 bits in machine language.
The same input produces the same output every time; the output is not random. But if the input changes even slightly, the output will be completely different.
The algorithm is also a one-way function, so even if you know the output, you can't work backward to calculate the input. Even if you try to guess the input at random, your chance of getting it right is only 1 in 2 to the 256th power, which makes it practically impossible. Put simply, it's secure.
Now that you know what the algorithm does, let me show you how a blockchain works using a simple transaction example.
Here we have Alice and Bob, who both hold bitcoin. Let's say Alice owes Bob 2 bitcoins. To send Bob the 2 bitcoins, Alice broadcasts the transaction she wants to share to all the miners on the Bitcoin network. This broadcast Alice sends to the miners includes the number of bitcoins Bob will receive and his address, a digital signature, and Alice's public key. The digital signature is created with Alice's private key, and through it the miners confirm that Alice is the owner of the bitcoin and the one making the transaction.
After checking that all of this is valid, the miners bundle this transaction together with many other transactions into a single block and try to mine it. Mining is done by feeding this block into the SHA-256 algorithm. For the output to be considered valid, it has to start with a certain number of zeros. How many zeros the output has to start with depends on the "mining difficulty," and here "mining difficulty" refers to the computing power that exists on the Bitcoin network.
To get an output that starts with a certain number of zeros, the miner has to add a "nonce" to the input. Because even a small change to the input produces a completely different output, miners keep trying nonces at random until they get a suitable output.
Once a block has been mined, the miner broadcasts it to the network. The miners who receive it check whether the block is genuine and add it to their own copies of the blockchain, and the transaction between Alice and Bob is complete. Apart from the transactions, though, each block also contains the output hash of the block right before it, which ties all the blocks together. That's also why it's called a blockchain. This is an important part of how trust works in a blockchain's structure.
Every miner keeps a copy of the blockchain on their own computer, and they trust the longest blockchain, the one with the most computational work put into it. If a miner tries to change a transaction in an earlier block, that block's output hash changes, and so do the hashes of every block created after it. That's because every block is linked to the block before it by its hash. So to get everyone else to accept one miner's blockchain as the correct one, that miner would have to recalculate every block after the one they changed. That means the miner would need more than 50% of the network's computing power, which is nearly impossible. This kind of attack on the network is called a 51% attack.
The computational work done to mine a block is called Proof-of-Work. There are also other mining models, such as Proof-of-Stake, which doesn't require as much computing power as Proof-of-Work, uses far less electricity, and lets more people take part.
And that's the basic process of how a blockchain works!