Ok..let me make sure I understood correctly.
Say this is an example..
483045022100a3658e7cedeab2800add38516aa711de2f98259df55319a92a2fa73cbaf15d94022 049ed36b8c83b386e2ae4bef82328848d06e8f0a783a25203b573942f7a5c8c48012102296038d0 cba420126d7dc75fe7edc8f5604a5ef6874b034ac65b761a73faf503
~~
~
My understanding is correct, isn't it?
Thanks,
Yes! That's correct.
~~
I also have seen someone starts 0x47.....Any difference?
BTW, Is there any technical detailed doc about this signature encoding/decoding so I can take look at?
Thanks,
ECDSA algorithm takes two inputs, digest (double-SHA256 hash) of the transaction serialization and private key. It then produces 2 values as output - r and s. r and s are usually 32-byte values. Depending upon the values of R and S, we need to follow certain rules while encoding the raw transaction.
BIP-66 has defined the rules for strict DER-encoding of signatures. Due to these rules, the length and bytes of the signature may differ from transaction to transaction. Some of the rules are:
1. Value of S - The value of s as yielded by ECDSA algorithm can be very large. But as per the current standard, its value should be less than: 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0. If it is higher than that then it is required to be converted into low s (s'). In order to find low s, we have to subtract s from n (parameter in secp256k1 curve). Hence,
s' = n - s
s' = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s
2. Null byte if R as 32-byte signed integer is negative - This rule directly effects the length of signature. If the value of r as 32-byte signed integer is negative, we would append null byte i.e. 0x00 in front of it. This would make the length of r equals to 33 bytes.
The format of DER-encoding (in hexadecimal) is: 30 + length of signature + 02 + length of R + R + 02 + length of S + S
If R as 32-byte signed int is negative, signature will look like this:
3045022100XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX0220YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
where 0x45 (69 in dec) is the length of signature
0x21 (33 in dec) is the length of R
XX...denotes 32 bytes of R
0x20 (32 in dec) is the length of S
YY...denotes 32 bytes of S
If R as 32-byte signed int is positive, signature will look like this:
30440220XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX0220YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
where 0x44 (68 in dec) is the length of signature
0x20 (32 in dec) is the length of R
XX...denotes 32 bytes of R
0x20 (32 in dec) is the length of S
YY...denotes 32 bytes of S
Signature is then followed by an additional byte known as sighash flag denoting which part of the transaction is signed. The most common flag is 0x01 which depicts that all the inputs/outputs are signed. This is then followed by one more byte which denotes the length of public key. If the public key is compressed then its length will be 33 bytes which is denoted by 0x21 followed by public key.
The whole signature and public key serialization is preceded by a byte which denotes the length of the serialization. It can be either 0x48 or 0x47 depending upon the value of R. Hence, whole serialization will look like this:
If R as 32-byte signed integer is negative:
483045022100XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX0220YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY0121ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
If R as 32-byte signed integer is positive:
4730440220XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX0220YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY0121ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
where ZZ.. denotes 33 bytes of compressed public key (note: public key can be uncompressed as well)