I was doing some research about the ways to cut out transaction fees when I came across SegWit. I discovered that SegWit was introduced to fix transaction malleability and improve block space efficiency, but I’m still a bit unclear and confused on how it works under the hood.
SegWit which is Segregated Witness in full actually changes Bitcoin transactions by separating witness data (which are actually signatures and public keys) from the main transaction data which makes it easier to work with and also save block space and improve efficiency smaller space occupied means smaller fees paid.
However In legacy transactions, the entire transaction including witness data is hashed to create the TXID, making it far less malleable since minor witness alterations would change the TXID. SegWit actually solves this by excluding witness data from the TXID calculation. instead it's moved to a separate "witness" field, ensuring the TXID remains fixed even if witness data is modified.
This separation actually leads to lower transaction fees because witness data in SegWit transactions is "discounted" when calculating transaction size meaning each witness byte is weighted as 0.25 bytes for fee purposes which results in a smaller effective "virtual size" known as (vbytes).