Double Spending of Cryptocurrency

Learn how double spending is avoided in cryptocurrency through transaction confirmations and UTXOs. Discover how these mechanisms ensure secure and unique transactions.

Crypto_Currency__Block_Chain

In my last blog I had discussed miners, why they do mining, what is unconfirmed transaction pool and the block reward change. In this blog, I will discuss the concept of double spending, Unspent Transaction Output (UTXO), and other concepts related to transactions in the blockchain.

What is Double Spending?

Imagine you have physical cash in your wallet, and you go to a store to buy groceries, clothes, or a device. After the cashier gives you the total amount, you pay in cash and take your purchases with you. Now, can you use the same cash, that you already spent on your purchases, to buy something else like Coke or chips from another store? No, unless you somehow steal the money back from the cashier and trick them. But that's illegal, and you would surely be punished for committing such a crime. Therefore, you can't spend the same money twice.

This scenario explains the concept of double spending, where a person attempts to use the same money to pay multiple parties. If this problem existed in the banking system, it would undermine the value of money, disrupt transactions, and render banking, account balances, and the concept of money itself meaningless.

In cryptocurrency, since the money is digital, it would seem easier for a person to duplicate a transaction and engage in double-spending. However, Satoshi Nakamoto, the creator of Bitcoin, anticipated this problem and resolved it by introducing a verification and confirmation process for transactions, including timestamping. In Bitcoin's consensus mechanism (a system of agreement followed by each node in the blockchain), a transaction requires at least six confirmations to be considered trustworthy and legitimate.

For example, if you have 3 BTC and attempt to spend 2.5 BTC in two separate transactions to different wallet addresses (a wallet address functions like a beneficiary's account number), both transactions will first enter the unconfirmed transaction pool. When a miner confirms the first transaction, it will be added to a block. Now, this transaction just needs six more confirmations to become official. Meanwhile, the second transaction, which is still in the unconfirmed pool, may be picked up by a second miner. However, it will be found invalid during verification.

Why is the second transaction invalid? This is the key point. Once Bitcoin is transferred from one wallet to another, the ownership of that Bitcoin is assigned to the receiver's wallet. Each cryptocurrency in the blockchain maintains a chain of ownership. So when the second transaction is checked, it will be found invalid because the ownership of the Bitcoin has already been transferred to someone else during the first transaction.

Another possible scenario is if both transactions are confirmed at the same time from the unconfirmed transaction pool. In this case, two blocks will be formed simultaneously, causing a temporary split in the blockchain. To resolve this, the longest chain rule is applied. This rule states that the longest chain, where more blocks continue to be added, will become the permanent part of the blockchain, while the shorter branch will be excluded. The longest branch needs to reach six blocks to be considered permanent. It’s important to note that it is highly unlikely for both branches to have the same length. Once the shorter branch is removed, its transactions may either go back to the unconfirmed pool or be handled according to the consensus rules.

In a previous blog, I mentioned that as soon as a miner confirms a transaction and adds it to a block, the transaction is completed from the sender’s wallet to the receiver’s wallet. However, the actual transaction is not considered fully successful until it receives at least six confirmations—meaning six additional blocks must be added after the block containing the transaction. This is necessary to ensure a secure and legitimate transaction. Some wallets may require a different number of confirmations to finalize the transaction.

Below is a snapshot of block numbers and their discovery times in the Bitcoin blockchain. Block 689462 was found before block 689467. Hence, block 689462 has received six confirmations, while block 689467, being the latest block, has only one confirmation.

Latest_Blocks

The below snapshot shows block 689462 which has got 6 confirmations and is more trustworthy than 689467.

success.png

The below snapshot shows the block 689467. So it is not trustful, once it gets 6 more blocks added, the transactions of the block will be confirmed. It depends on the wallets, some wallets need 3 confirmations, some need 2 confirmations, and the transactions of large amounts sometimes need 15 confirmations.

unconfirmed.png

What is UTXO?

UTXO stands for Unspent Transaction Output, a fundamental concept in blockchain technology. To understand it, let's use an analogy. Imagine you go to the supermarket to buy some snacks, and you have cash in your physical wallet. After shopping, your bill totals $30. When you pay the cashier, you hand over a $50 bill. In return, the cashier gives you $20 as a change, which you put back in your wallet.

In this scenario, you can think of it as an equation:
$50 = $30 + $20

You initially had $50 in your wallet, which became two parts: $30 (the amount you spent) and $20 (your change). The left-hand side (LHS) equals the right-hand side (RHS)—the total amount remains the same. The $20 is unspent, meaning you can still use it for future purchases, while the $30 is spent and no longer available to you. In this case, the $50 was your input to the transaction, which produced two outputs: $30 and $20. The $20, being unspent, is now available as another input for future transactions.

In the world of cryptocurrency, UTXO works similarly. When a transaction occurs, the change you receive back is referred to as Unspent Transaction Output (UTXO). For example, when you create a Bitcoin transaction, let's say you have 0.6 BTC, 0.5 BTC, 1.8 BTC, and 1.5 BTC in your wallet (simplified values—Bitcoin is divisible up to 8 decimal places). While your wallet shows the total amount, behind the scenes, these Bitcoins from different transactions are not combined because each has its chain of ownership. Your wallet hides these details and presents you with a single balance.

Now, suppose you want to send 2 BTC to someone. In the background, your wallet automatically selects the appropriate UTXOs that add up to slightly more than 2 BTC because the transaction fee also needs to be covered (this happens automatically—you simply enter the amount to send and the recipient's address). Your wallet might select 1.8 BTC and 0.5 BTC, which totals 2.3 BTC. This transaction then goes to the unconfirmed transaction pool, where it will be added to a block. After six confirmations, the transaction becomes valid.

In this example, the recipient receives 2 BTC, and if the miner's fee is 0.1 BTC, it is deducted from the transaction automatically. The remaining 0.2 BTC becomes your change—your UTXO—which is returned to your wallet. You don't need to manually claim this change; your wallet handles it for you. Now, your wallet would look like this: 0.6 BTC, 0.2 BTC, and 1.5 BTC, with 0.2 BTC being the unspent transaction output that you can use in future transactions.

In summary, UTXO refers to the unspent portion of a transaction that is returned to the sender and is available for future use. Each transaction creates new UTXOs, ensuring that Bitcoin maintains a chain of ownership and preventing double-spending.

tx.png

In the above snapshot, the transaction of 2.37361032 BTC is initiated by the sender on LHS. The transaction is split into 5 outputs, out of which the green boundaries addresses are the UTXO, which are reverted to account, and the red boundaries are the actual spent, which the receiver will receive. Now have a look at the blue boundary, in LHS and RHS, you will see that the actual input is not equal to the output because the transaction fee (0.00061184) is not subtracted in LHS. But when you subtract the transaction fee (0.00061184) from LHS 2.37361032 BTC, then LHS will become equal to RHS. This concept I already discussed above, to keep track that the BTCs are not lost, LHS should be equal to RHS.

Will Bitcoin Addresses Run Out?

Every time you make a Bitcoin transaction, a new wallet address for both the sender and receiver is generated. This process helps maintain anonymity, making it difficult to track transactions. Although transactions can be traced, the complex mapping involved makes it very time-consuming. This is why your wallet address changes after every transaction, as it is a measure to enhance privacy.

For example, when sending Bitcoin, you might notice that different addresses are being used, even though they belong to the same sender. This happens because each new transaction generates a new address. However, this doesn't prevent you from using other Bitcoins stored in your wallet, since all the addresses ultimately belong to you. While the wallet manages multiple addresses in the background, it simplifies things by showing you only the most recent address. Importantly, even if someone sends Bitcoin to an older address, you will still receive it because you are the owner of that address.

Given this system, it raises some important questions:

  • Will Bitcoin addresses run out?
  • What if I receive an address that's already been taken?
  • Is it possible for someone to duplicate my wallet address and access my coins?

Let's dive into the math behind Bitcoin's blockchain to answer these questions.

Each wallet address is linked to a private key, which is a unique, random 256-bit number. Every time you complete a transaction, a new private key is generated and mapped in the background by your wallet. The private key provides cryptographic security to your Bitcoin holdings.

There are 2^256 possible private key combinations.

This is like flipping a coin 256 times to generate a completely random private key. The randomness ensures that each private key is unique and unconnected to others.

  • A private key might look something like this: 010101000100101000100101001001001111010101101010101010101001010000101010...

Now, each private key generates a corresponding public key, which is 160 bits long. Public keys are derived from private keys and are used to generate wallet addresses.

There are 2^160 possible public key combinations.

  • A public key might look like this: 010101000100101000100101001001001111010101101010101010101001010000101010...

Next, let's consider the total number of possible Bitcoin addresses. Since each Bitcoin can be broken down into 100,000,000 satoshis (the smallest unit of Bitcoin, equal to 0.00000001 BTC), and there will only ever be 21 million Bitcoins created, that equates to a maximum of 2,100,000,000,000,000 satoshis in circulation.

However, we have 2^160 possible public keys, which equals approximately 1.46 x 10^48 possible addresses.

Probability of Address Collision

The probability of two users accidentally generating the same Bitcoin address is incredibly small. We can calculate the probability using the formula:

formula.png

This probability is astronomically low—so low that you are more likely to be killed by a meteor (with a probability of 1.428×10−61.428 \times 10^{-6}1.428×10−6) than to encounter a duplicate Bitcoin address.

Conclusion

In conclusion, cryptocurrency keeps transactions secure by tackling issues like double-spending, where someone might try to spend the same money twice. It prevents this by requiring several confirmations to make sure the transaction is valid. Additionally, the system uses Unspent Transaction Outputs (UTXOs) to keep track of which parts of a transaction are still available to spend. With an enormous number of possible Bitcoin addresses, the chance of address collisions is almost zero, ensuring that each wallet remains unique and secure. This combination of methods makes blockchain technology reliable and trustworthy for digital transactions.