This article brought to my attention that SIGHASH flags are 4 bytes long when signed, but only the last byte is actually included in the transaction. Then OP_CHECKSIG adds 3 bytes 0x000000
back on before verifying the signature. The article describes a clever way to introduce a fork using those three truncated bytes -- so why are the SIGHASH flags 4 bytes long in the first place? Is that a design flaw or feature?
1 Answer
I assume it is simply the result of lazyness.
In the original client source code (and still today), the sighash type is represented as an int. The serialization framework by default serializes ints as 4 little-endian bytes.
I assume Bitcoin's creator did not bother converting it to a single byte before serializing.
If there is any other reason, I'm afraid you'll need to ask him.
-
Thanks Pieter that makes sense... but then when (in the code) is that int truncated to 1 byte? Is that just to reduce transaction size?– pinheadAug 18, 2016 at 21:15
-
Is it this line right here, casting
nHashType
as anunsigned char
(1 byte): github.com/bitcoin/bitcoin/blob/…– pinheadAug 19, 2016 at 0:28 -
2Yes, my assumption is that Bitcoin's creator cared about signature size (although he didn't realize that using DER encoding added 6 bytes for no reason), and used 1 byte for the sighash type there, but didn't care enough to do the same for the sighash calculation. Aug 19, 2016 at 8:29