Hello!

© 2024 Kishan Kumar. All rights reserved.

SegWit: What Is It and How Does It Make Bitcoin Faster and Cheaper?

Segwit is derived from Segregated Witness and is an upgrade to the Bitcoin network that aims to solve several issues, such as scalability, transaction malleability, and block size.

June 04, 2023

Hero

If you have been transacting Bitcoin for a while, you might be confused with two networks that often pop up while sending or receiving the coins.

  1. BITCOIN NETWORK (addresses start with 1 or 3)
  2. BTC SEGWIT (addresses start with bc1)

You might be wondering what are the implications of using the SegWit network over the BTC Network. Let's start the discussion by first understanding what SegWit is and what challenges it solved that used to occur in our vanilla network.

Segwit is derived from Segregated Witness and is an upgrade to the Bitcoin network that aims to solve several issues, such as scalability, transaction malleability, and block size.

It was implemented as a soft fork in August 2017 and was first introduced by the Developer Pieter Wiulle at the Scaling Bitcoin conference in December 2015.

Let's dive deep into how it solves the above-mentioned issues:

Scalability

SegWit increases the number of transactions that can fit into a single block by separating the transaction signatures (witness data) from the transaction data. This allows more transactions to be processed in each block, improving the overall transaction throughput of the network.

BCH

BCH

You might ask, why not simply increase the block size? That would be convenient, wouldn't it?

Yes, and you would be correct to think that way, and you might also be surprised to know that there was a hard fork of the Bitcoin network that increased the block size limit to 8 megabytes (MB) from the 4 MB. This hard fork resulted in the creation of a new cryptocurrency called Bitcoin Cash (BCH), which was launched on August 1, 2017.

However, the hard fork was controversial, and not all members of the Bitcoin community supported it. Some argued that increasing the block size limit would compromise the decentralization and security of the network, as larger blocks would require more storage and processing power, making it more difficult for individual users to participate in the network.

Block Comparison

Block Comparison

So, what should one do? We have a hard limit on the block size; the only way we can achieve scalability is to somehow play with how the transactions are stored in the block.

The maximum block size of Bitcoin is currently 4 megabytes (MB) in principle, but in practice, it is limited to 2 MB due to the way the block size is defined. This limit was introduced as a temporary measure by Satoshi Nakamoto, the creator of Bitcoin, to prevent spam attacks on the network.

Okay, but how does SegWit separate the signature or witness data?

Alright, first, let's understand what the signature data is.

  • The signature is a part of the transaction that proves that the sender has the private key of the address that the coins are being spent from. The signature data is created using the Elliptic Curve Digital Signature Algorithm (ECDSA), which is a cryptographic algorithm used to generate digital signatures. When a user signs a transaction using their private key, the ECDSA algorithm generates a digital signature that is unique to that transaction and can be used to verify the authenticity of the transaction.
  • Earlier, the signature and the transaction details were stored in the same block, but after the SegWit was implemented, the witness data was separated from the transaction data and stored in a separate data structure called the witness block.

Here is how the whole thing sits in the big picture. When a miner validates a transaction, they first check the transaction data to ensure that it is valid and meets the network's rules. If the transaction is valid, the miner then checks the witness block to verify the digital signatures contained in the witness data.

Transaction Malleability

Transaction malleability is a term used to describe the ability of a third party to modify the transaction ID of a Bitcoin transaction without changing the underlying transaction data before it is confirmed by the network. This can happen because the transaction ID is calculated based on the transaction data and the digital signatures contained in the witness data.

TXID = some_hash(TXN_DATA, TXN_SIGNATURE)

For example, let's say Alice sends 1 BTC to Bob and gets a TX ID of 123456. An attacker can intercept this transaction and change the signature data slightly, creating a new TX ID of 654321. The attacker then broadcasts this modified transaction to the network. If the modified transaction gets confirmed before the original one, Alice's wallet will show that her transaction is unconfirmed, even though Bob has received the payment. This can confuse Alice and make her think that she needs to resend the payment.

123456 = some_hash(TXN_DATA, signed_by_alice)
654321 = some_hash(TXN_DATA, signed_by_attacker)

Note: The signature data is not immutable because it can be encoded differently without changing its meaning. For example, the signature data consists of two values, r, and s, which are numbers. However, these numbers can be represented in different formats, such as hexadecimal or decimal. The signature data also has a SIGHASH flag, which can be appended or prepended to the signature. These variations do not affect the validity of the signature, but they do change the TX ID that is generated by hashing the transaction data.

Another example of how transaction malleability can be exploited is by double-spending. Let's say Charlie sends 1 BTC to Dave and gets a TX ID of 789012. Charlie then creates another transaction that spends the same input as the first one but sends it to himself instead of Dave. He modifies the signature data of this second transaction to get a different TX ID of 210987. He then broadcasts both transactions to different parts of the network, hoping that one of them will get confirmed and the other will be rejected. If he succeeds, he can trick Dave into thinking that he has received the payment while actually keeping the coins for himself.

You'll be surprised to know that the Transaction malleability was one of the reasons behind the collapse of Mt Gox, a famous Bitcoin exchange that lost hundreds of thousands of bitcoins in 2014. The attackers used transaction malleability to withdraw bitcoins from Mt Gox multiple times while making it appear that their withdrawals were unsuccessful.

How does SegWit address it?

The Segregated Witness (SegWit) upgrade to the Bitcoin protocol addressed this issue by removing the digital signatures from the transaction ID calculation. This means that the transaction ID is now calculated based only on the transaction data, which makes it much harder to modify the transaction ID without changing the underlying transaction data.

TXID = some_hash(TXN_DATA)
  • By separating the witness data from the transaction data, SegWit also made it easier to implement other improvements to the Bitcoin protocol, such as the Lightning Network, which enables instant, low-cost transactions by creating off-chain payment channels between users.
  • Before SegWit, the transaction data included a lot of redundant information, such as the scriptSig and scriptPubKey fields, which were used to verify the transaction. This redundant information made transactions larger and less efficient, which contributed to the scalability issues of the Bitcoin network.
  • With SegWit, the transaction data is compressed by removing the scriptSig and scriptPubKey fields from the transaction data and moving them to the witness data. This reduces the size of the transaction data and makes transactions more efficient.

In addition to compressing the transaction data, SegWit also includes other features that improve the efficiency and scalability of the Bitcoin network, such as the ability to process more transactions per block and the ability to implement second-layer solutions like the Lightning Network.

With Good things often comes Bad things.

Let's see what those are:

  • Complexity: SegWit introduces additional complexity to the Bitcoin protocol, as it requires changes to the transaction format and how transactions are processed. This can make it more difficult for developers to understand and work with the protocol, and it may increase the risk of bugs or unintended consequences.
  • Complexity: SegWit introduces additional complexity to the Bitcoin protocol, as it requires changes to the transaction format and how transactions are processed. This can make it more difficult for developers to understand and work with the protocol, and it may increase the risk of bugs or unintended consequences.
  • Partial Adoption:Due to the changes in transaction format and the introduction of new address types, such as bech32 addresses, there can be compatibility issues between SegWit-enabled wallets and services and those that have not yet adopted SegWit. This can lead to confusion and inconvenience for users who may not be able to send or receive transactions between different types of addresses.
  • Controversy and Community Division:: The implementation of SegWit was a contentious issue within the Bitcoin community, leading to debates and disagreements over the best approach to scaling the network. This controversy contributed to the hard fork that created Bitcoin Cash (BCH) and has led to ongoing debates about the future direction of the Bitcoin protocol.

SegWit Address and Legacy Address

SegWit addresses use a different format compared to normal addresses. The most common SegWit address format is the bech32format that starts with "bc1". For example:

bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq

Whereas the legacy addresses use the base58 encoding and can start with either "1" (Pay-to-Pubkey Hash or P2PKH) or "3" (Pay-to-Script Hash or P2SH). For example:

1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa (P2PKH)
3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy (P2SH)

In conclusion, SegWit has had a significant impact on the Bitcoin network by addressing scalability issues, fixing transaction malleability, and enabling the development of second-layer solutions like the Lightning Network. While there are some disadvantages, such as complexity and partial adoption, the overall benefits of SegWit have made it an essential upgrade for the Bitcoin ecosystem.

References:

    .   .   .

    The 0xkishan Newsletter

    Subscribe to the newsletter to learn more about the decentralized web, AI and technology.

    © 2024 Kishan Kumar. All rights reserved.