A blockchain is a public, distributed ledger of all transactions ever to take place on that blockchain. Although Bitcoin seems to be synonymous with blockchain, blockchain technology is not just Bitcoin. Bitcoin is the largest and most well-known implementation of the technology, but some of the other large open source implementations are Ethereum and Litecoin.

Very simply, the blockchain is software which runs on millions of computers. These computers (or nodes) communicate with each other to ensure that data stays in sync.

But how does it work, and why was it invented?

**The Double Spending Problem**

One of the primary responsibilities of a central, trusted authority in a financial transaction, such as a bank, is to prevent what is known as the “double spending” problem. Banks act as the trusted intermediaries to ensure that money is not spent digitally twice. The banks ensure that this exchange of money happens within a *transaction*.

A transaction, by definition, is atomic, i.e. either it happens or it does not, there is no in-between state. Transactions are also sequential. Which means a transaction always follows and/or precedes another transaction. Transactions do not occur simultaneously.

As an example, let’s say Hannah has $10, and she has to pay Alice $3 and Liam $5. The logical, successful end to these transactions would result in Hannah having $2, Alice with $3 and Liam with $5. Hannah cannot send the same money to both Alice and Liam at the exact same time. If she did that, Hannah would end up with $5, Alice would get her $3 and Liam would get his $5. Hannah would have magically created $3 extra. She would have spent her money twice.

Without a trusted, central authority like a bank keeping records of each of these transactions, the financial system would quickly fall apart.

Therefore, although the idea of a decentralized ledger and digital currency has been around since the 1990s, the double spending problem was a huge stumbling block for its practical implementation.

**How Bitcoin/Blockchain Solves It**

In 2009, Satoshi Nakamoto, a yet-to-be-identified individual, published a white paper in which he claimed that he had solved the double spending problem without a central agency acting as an arbiter, via digital cryptography, game theory and computer science.

Nick Szabo had introduced Bitgold in 2005. Bitcoin is so similar to Bitgold and shares so many of its concepts that some people suspect Szabo to be Satoshi Nakamoto.

#### Public Key Cryptography

In order to understand how blockchain technology works, one needs to understand public-key cryptography. The full technical details of this topic is beyond the scope of this article. More details are available here.

At a very high level, public-key cryptography relies on two keys: a public key and a private key. These two keys are linked but separate as below:

- Only the public key can decrypt data which was encrypted by the corresponding private key
- The public key can verify data signed by the corresponding private key

The public key can be safely shared with others, hence “public”. The private key must be kept secret for security reasons. The public key can be derived from the private key but not vice versa.

As an example, suppose Bob wants to transmit a secret message to Alice. Bob will need to know Alice’s public key to encrypt the message before sending it to her. Since the public key is not a secret, Alice is free to send it to Bob. When Alice receives the encrypted message, she can decrypt it using her private key. Anyone in the middle who intercepts the message cannot decrypt it without Alice’s private key.

For reference, this is what a public key looks like:

305C300D06092A864886F70D01010105 00034B003048024100A251C92CCA3E7B 6599146DDB10044807B3AA1EFA8BE627 4D329765E0CC5A0EA03CA8F010524A89 2484A7C96498660A20DBD3AA3E92D2CB 5037DD95B7F69BBA630203010001

An encrypted message could look something like this:

```
-----BEGIN PGP MESSAGE-----
hIwCMavvb4t6z90BA/42UOAdWvnzfhRG2xXyYe203CISLsn2O39vM/y640hNbSl7
U29aNGZFfLMRGn7eLZG43SWwBz4cHjphG6iAzeLftRgHkLggxXA9VpGki5PyNID9
B0rk4TpRVE3qzgTbdio69aMlK6BdAQ4zWkyxSCi0oR3Vpnh+VVZyOVyaX8etlYRM
AvUTsuDYCkr1AQH+OlA4ntqhxoPP/SJpKm5ugMLYiiij8ak8V90a8IYMkYB0CzMr
liOJ6ZZxQm1x8orgjL/6Bm5EoSvN4eCCeA/xXKYAAAHXLhG47kVhJkjlPrI/U/sr
2aQEm6r+aU1s0ziU1LxF2c5DAW6cD5b4xH+EbvYrnQQJClNMh9y03SjviXvnqFDC
O4M70u3iLC50+em4PouqM1DZdoW8O5pb
=vhFx
-----END PGP MESSAGE-----
```

#### Hash Functions – SHA-256

Encryption ensures that messages between two parties is secure and reliable. However, the recipient also needs to verify that the message that they’ve received has not been tampered with. In order to do that, every transmitted message is digitally signed using its hash value.

A hash is like a digital signature of a message. Numerous algorithms, known as hash functions, have been developed to generate a hash, such as MD5 and SHA-1, but the one most commonly used now is called SHA-256. The details of hashing and the math involved in the various hash functions are unnecessary for the understanding of blockchain.

A hash function is a one way function. This means that the hash of a message is always the same when using the same function. However, the message cannot be decrypted from the hash.

SHA-256 always produces a 256 bit hash value. For example:

[table id=1 /]

As you can see, a single change to the data (upper case L in this case) completely changes the hash.

The data input to the hash function can be as simple as a single word, or it could be the entire dictionary and it will always produce a 256 bit hash value similar to the ones above.

#### What Are Chains

As mentioned above, any dataset can be passed through a hash function to generate its corresponding hash. In a blockchain, this dataset is the transactions taking place on it. Every transaction in a blockchain is digitally signed using its hash, currently using the SHA-256 function. All subsequent transactions will include the digital signature of its previous transaction. This forms a *chain* of transactions such that if one were to modify any transaction in the chain, it will change the hash value of all subsequent transactions. This makes it easy to identify any chain where a previous transaction has been modified just by comparing hash values.

Example: Say Bob needs to transfer $10 to Alice and another $5 to Charlie. Since transactions are atomic, Bob’s transfer of $10 to Alice will occur first. This transaction will be digitally signed using its SHA-256 hash. The next transaction of Bob transferring $5 to Charlie will also be digitally signed, but this time it will also include the hash of the previous transaction.

[table id=2 /]

Of course, transactions on a blockchain will not look like this, but this gives a general representation of it.

#### What Is Mining

As previously mentioned, the blockchain is a network of nodes running the same software which communicate with each other to ensure data is in sync. Some of these are specialized nodes called miners.

The purpose of miners is to verify the transactions that are being done on the blockchain. When a number of transaction chains have been verified, they are combined to form a *block* of transactions.

As a reward for correctly and honestly verifying the transactions, the miners are awarded the cryptocurrency of the corresponding blockchain. So for example miners on the Bitcoin blockchain will be rewarded with Bitcoin. This amount is fixed but can vary from time to time.

Miners must show their *proof of work* to earn their reward. The *work* to be performed is essentially to find a 32-bit field which when hashed with the hash value of the chain of transactions generates another hash value with a set number of leading zeros. This field is called a nonce. The number of leading zeros to be generated is set by the difficulty which changes after a set number of blocks have been mined. The more number of leading zeros, the more difficult it becomes to find the nonce.

As an example, the nonce of Bitcoin block #493257 was found to be *1488602745* which when hashed with the transactions in that block generated the hash for the block of *00000000000000000034bd56aea0c6db865e8dff1fe5d2023170b568ad5e0968*. Please note the leading zeros in the block’s hash value.

It is highly infeasible to derive a nonce using a formula. The only way to find the nonce is through guesswork. This means that miners have to spend their own computing power to run through a potentially large number of 32-bit values before essentially stumbling upon the one which meets the requirements for the leading zeros. The fact that they found the nonce proves that they have done the work to find it.

#### Putting The Blockchain Together

Public-key cryptography ensures that transactions between two accounts are secure.

Hash functions ensure that transactions are valid, atomic and virtually impossible to modify.

Mining through proof of work ensures that transactions are confirmed and verified by a third party.

All these methods working together make the distributed ledger, and hence an application like digital currency, possible on the blockchain.

Mahesh Sashital is cofounder of Smartereum.

He was previously Vice President at J.P. Morgan. He has over 15 years of experience in software engineering at various Fortune 20 companies. He has a Bachelor's in Engineering from the University of Mumbai and an MBA from the University of Texas-Austin.