Categories
Cryptocurrencies & NFTs Financial talks at dinner table

Merkle Tree & Double Spending

The family today will wrap up the technical issues and move on to big picture issues associated with cryptocurrencies and blockchain.

Greg: Okay, last time we talked about Merkle Tree but did not find the time to talk more about it. To begin, I think the key is to understand different hash values. We have transaction level hashes, meaning each transaction has its own hash, although we barely talk about that.

Emily: Yeah, why is that?

Greg: Because the “block hash” steals the show. In fact, we talk about block hash so much that we may even believe there is only one hash in each block. But if you think about it, that can’t be true because we want to make sure every transaction is immutable, not just immutability for the entire block.

Jason: That’s true. I remember watching that video you recommended, and it shows every time the guy typed something in the data column, the hash changes. It’s not like the hash only changes once at the end when all data are entered.

Greg: It has to be that way. If anyone can mess around each and every transaction inside a block without any change in hash, what does it mean to have a secure block? It means nothing. A secure block must make sure all the records it contains to be secure. We can think of a block as a movie theater, which is not safe unless all movie-goers are safe. If one were to be kidnaped in it, nobody could claim the theater safe.

Lily: The key is to link record level security to block level security I think.

Greg: Exactly! And that’s where Merkle tree fits in. Let me show you a picture of that tree.

Merkle Tree, Merkle Root & Block Headers

Emily: It looks complicated.  

Greg: That’s because it shows more than a Merkle Tree. It has three blocks, or more accurately three block headers. Merkle Tree is shown for the middle block. Let me show you another picture from Investopedia that contains only Merkle Tree, no block headers:

Transactions (T), Hashes (H), Merkle Tree & Merkle Root

Emily: What’s the basic idea of the picture?

Greg: The tree keeps pairing up transaction’s hashes, called “leaves hashes,” to combine them all into one single “root” for all transactions. In the pictures the root is called “Tx_Root” or Habcdefgh where “Tx” or “T” stands for transactions. Most people call it Merkle root to honor the author who invented it. “H” is always for hash.

Kimberly: It’s funny that you call it a tree, but the root is at the top.

Greg: I was gonna point that out. Yes, the Merkle Tree is always upside down with the “leaves” at the bottom and “root” on the top. Furthermore, it is the same fingerprint machine, SHA256, that is used to build the tree. It starts by creating one hash for each transaction and then retains the hash, no need to keep transaction details, which saves space.

Lily: Let me guess: The fingerprint machine keeps pairing up hashes until there is just one hash, which will be the Merkle Root hash, right?

Kimberly: Also, once we have linked all records in a block, we then link blocks. This is done by each block header always containing the block hash of the preceding block, like its parent, which has its own parent block. That way once a block is added to the blockchain, it cannot be easily changed.

Greg: Both of you guessed right! A couple details. First, if the number of hashes is an odd number, we will simply duplicate one hash to ensure all hashes will be paired up. Secondly, the Merkle root hash is always 32 bytes, whether it’s for one transaction or one thousand. Finally, SHA256 actually hashes block header twice, which is why sometimes you will see notions like SHA256-2 or SHA256 Squared (SHA2562).

Emily: Why is that?

Greg: I don’t think we need to worry too much about it, but the most popular theory is that hashing twice helps protect against the so called “length extension attacks.”

Emily: What’s that?

Greg: According to this Wikipedia page, it’s a risk where a hacker can use the output hash and the length of input messageto calculate another hash controlled by hacker, without needing to know the content of input message. By the way all SHA-2 algorithms, which include SHA256, are vulnerable to this but the future generation like SHA-3 won’t have the problem.

Joy: We probably should only remember that the reason behind hashing twice is to increase the security of the hash.

Greg: That’s right. Now to wrap up, here is a pretty good overall picture from Tutorialspoint.com:

Overview of Bitcoin Block Hashing

Emily: This one has all the six fields in a block header.

Greg: Yes, it does. One terminology issue: The level of difficulty is sometimes called “Bits” like in this picture. Just so you know.

Kimberly: I have had a question for a while but did not get to ask: What exactly is the “double spending” problem. Almost all discussions on mining mentioned this.

Joy: There is an article by Investopedia that offers a good explanation. It says the problem is mostly for digital money, not paper money. Spending the same $20 bill twice is virtually impossible, just like you can’t have a cake and eat it. After you hand your $20 bill out to the cashier for a 16’’ pizza, you can’t get that bill back, unless the pizza had the wrong topping, like you ordered mushroom, but you got meatball. You can demand for a refund and get the $20 back — but then you didn’t get the pizza.

Jason: It is possible though that the store manager may let you keep the meatball pizza for free and give your $20 back. A friend of mine told me a story like that before.

Greg: Good try, Jason! But that is not a “double spending” case. Instead, it’s “no spending” because you did not spend a penny in the transaction.

Kimberly: But why do cashiers in supermarkets or stores constantly check every dollar bill from the customers?

Joy: I was thinking of the same thing. Supermarkets are looking for fake money, but fake money is one feasible way to double or even multiple spending. It uses one real dollar bill to produce thousands or millions of fake bills. To be sure, fake paper money is only possible with the right printing equipment and paper. With central bank’s monopoly power, it is impossible to produce fully authentic paper money. Cashiers with a simple device can quickly tell the difference.

Kimberly: So is it true that digital money is more vulnerable to double spending than paper money?

Greg: Let me first make a quick comment that we need to separate digital money from cryptocurrency.  The former is centrally issued just like the paper money but only exists in digital format, while the latter is decentralized, encrypted and distributed.

Kimberly: Wow, up to this point I thought “cryptocurrency” and “digital money” were different names of the same thing!

Greg: I recommend this article of Investopedia entitled “Digital Currency.” The authors correctly point out that all cryptocurrencies are digital currency, but not all digital currencies are cryptocurrency. Digital money has three types. The first is cryptocurrencies like Bitcoin and Ethereum; but we also have the so called “virtual currency” like casino tokens; and finally Central Bank Digital Currencies or CBDC, issued by central banks.

Joy: I remember reading this article from Times of India that says one of the striking facts of life is that an estimated 92% of the world’s currency is digital. I believe that estimate is right because all transactions between banks, between banks and firms, between firms and firms, they happen more often in digital form than in paper money. The bigger the transaction size, the more likely it is in digit form.

Greg: That’s right. If you expand the definition of digital currency to those frequently but not necessarily exclusively existing in digital form, then we can totally believe more than 90% of money today belong to that group. They are simply digital tokens. Yes, they can be printed out if needed, although I have a hard time imaging why we would want to do that.

Kimberly: Perhaps not printing out but cash out from an ATM machine. Like someone receives her monthly salary in digital form to her bank account but may need to take out a $5 bill to pay the toll if she does not have a FasTrack device.

Greg: That’s true.

Kimberly: So what does it mean for the double spending problem?

Greg: Is digital money more vulnerable to double spending than paper money? I would say no. In fact, I would say the double spending problem for digital money is overstated. There is an important technical reason for that: Digital money is much easier to be copied than paper money. However, just because it’s technically feasible does not mean it’s inevitable, because we already have powerful means to control and prevent the problem.

Kimberly: What are the means you have in mind?

Greg: Because digital currency is centrally controlled, the same central authority becomes the means to fight double spending. It will have detailed ledger to track down every digital dollar and nobody can escape from that monitoring and central auditing — unless the kind of double spending approved by the central authority.

Kimberly: We can have legitimate way of double spending?

Greg: Oh yeah, it happens all the time. All commercial banks are required to have certain amount of money in reserve but can make commercial loans above its limit. This is a legitimate way of double spending the reserved money. Say a bank only has $1 million in cash but can make $1.5 million loans. We just don’t call it “double spending” but “leveraging.”

Joy: On the other hand, like we talked about earlier, paper money is not any safer than digital money. Fake paper money can happen to every one of us on a daily basis. Just because one cannot spend the same dollar bill twice does not mean double spending is impossible for paper money. It is possible, mostly through fake dollar bills.

Lily: What about cryptocurrency? Are they more vulnerable to double spending?

Greg: Once again, the possibility of double spending for cryptocurrency is exaggerated. If anything, the blockchain has made double spending very difficult — even without a central controlling authority. There is an article in Investopedia that says that the “likelihood of a secret block being inserted into the blockchain is very slim because it has to be accepted and verified by the network of miners.” In fact, “it is more likely that a cryptocurrency is stolen from a wallet that wasn’t adequately protected and secured.” In conclusion, it says “(t)here isn’t actually any recorded instance of double-spending. The cryptocurrency community believes that all double-spending has been thwarted.”

Emily: We still have not touched on the comparison between Proof of Work and Proof of Stake yet. Looks like we will get to that another day.