>> (p.1)
    Author Topic: Did you know bitcoin uses 6 different ways to represent integers  (Read 422 times)
    Coding Enthusiast (OP)
    Legendary
    *
    Offline

    Activity: 1051
    Merit: 2867


    Bitcoin and C♯ Enthusiast


    View Profile WWW
    October 05, 2019, 09:52:53 AM
    Merited by ABCbits (19), xandry (8), Welsh (8), suchmoon (4), bones261 (4), hugeblack (3), malevolent (1)
     #1

    As you may know there are many numeric values used in the bitcoin blockchain, each representing a different thing: version, script lengths, locktime,... and since a block is just a sequence of octets (bytes) that is transferred between nodes and stored on disk, we have to convert these integer values into octet strings (byte arrays) and back.
    But what you may not have ever noticed is that in bitcoin, depending on what that integer represents a different approach is chosen for its conversion to bytes, resulting in 6 different ways of encoding integers!

    The following is an example transaction from BIP-143 containing 5 out of 6 methods listed below with each one highlighted according their type:

    01000000000102fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f
    00000000494830450221008b9d1dc26ba6a9cb62127b02742fa9d754cd3bebf337f7a55d114c8e
    5cdd30be
    022040529b194ba3f9281a99f2b1c0a19c0489bc22ede944ccf4ecbab4cc618ef3ed01
    eeffffffef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a010000
    00
    00ffffffff02202cb206000000001976a9148280b37df378db99f66f85c95a783a76ac7a6d59
    88ac9093510d000000001976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac000247
    304402203609e17b84f6a7d30c80bfa610b5b4542f32a8a0d5447a12fb1366d7f01cc44a022057
    3a954c4518331561406f90300e8f3358f51928d43c212a8caed02de67eebee
    0121025476c2e831
    88368da1ff3e292e7acafcdb3566bb0ad253f62fc70f07aeee6357
    11000000





    1. Fixed length little endian
    This is the easiest and most common way. It is used for block/transaction version, block time, block target, block nonce, TxIn.Outpoint.Index, TxIn.Sequence and TxOut.Amount and locktime.
    The integer will be converted to a little endian byte array of fixed 4 bytes length, with the exception of TxOut.Amount which is 8 bytes.

    2. Variable length big endian
    This is only used for bigger values in signatures (R and S) using signed notation (most significant bit indicates sign) and public keys (X and Y coordinate). They are always preceded by their length using StackInt (scripts) or CompactInt (witnesses) format.

    3. CompactInt
    This is a special format used in bitcoin only that can encode from 0 to 264-1 values. It is used for script lengths, input/output count, witness item count and witness item length.

    4. DerInt
    (Not an official name) This method is used in DER encoding and can encode from 0 to 21008 length integers. This is only used for encoding signatures (only uses up to 33 bytes lengths). It indicates the length part in DER's Tag-Length-Value encoding scheme.

    5. StackInt
    (Not an official name) This method is used in bitcoin only to indicate length of the data that is supposed to be pushed onto the stack. It can encode from 0 to 232-1 values.

    6. Short form integers inside scripts
    In bitcoin script language, the stack is an array of bytes. Sometimes these bytes could be interpreted as integers, or an integer could be pushed to the stack. To do that a special format is used:
    if there is an OP code for the value (OP_0, OP_NegativeOne,...) that single byte is used.
    if there is no OP code, integer is converted to byte array in little endian order in shortest form (no extra zeros) and sign is determined based on most significant bit.

    Example: How does 254 (0b11111110) look like in each encoding?
    Spoiler (select/highlight to see text):
    1. 0xfe000000
    3. 0xfdfe00
    4. 0x81fe
    5. 0x4cfe
    6. 0xfe00

    Projects List+Suggestion box
    Donate: 1Q9s or bc1q
    |
    |
    |
    FinderOuter(0.21.0)Ann-git
    Denovo(0.8.0)Ann-git
    Bitcoin.Net(0.28.0)Ann-git
    |
    |
    |
    BitcoinTransactionTool(0.11.0)Ann-git
    WatchOnlyBitcoinWallet(4.1.0)Ann-git
    SharpPusher(0.14.0)Ann-git
Page 1
Viewing Page: 1