Bitcoin Forum
May 08, 2024, 05:01:34 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Var length integers  (Read 240 times)
jackg (OP)
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


https://bit.ly/387FXHi lightning theory


View Profile
January 25, 2019, 05:32:47 PM
Merited by DaCryptoRaccoon (1)
 #1

I made a thread on the bitcoin wiki section but it seems to have gone ignored. Maybe that board isn't as subscribed to as this one so here it goes:

From this page there's a table: https://en.bitcoin.it/wiki/Protocol_documentation#Message_types

valuestorage lengthformat
<0xFD1uint8_t
<=0xFFFF30xFD followed by the length as uint16_t
<=0xFFFFFFFF50xFE followed by length as uint64_t
-90xFF followed by length as uint64_t

There isn't really any description as to what the first character is supposed to represent? And why the second, third and forth are needed is this just in case the voltage gets misread?
1715187694
Hero Member
*
Offline Offline

Posts: 1715187694

View Profile Personal Message (Offline)

Ignore
1715187694
Reply with quote  #2

1715187694
Report to moderator
Each block is stacked on top of the previous one. Adding another block to the top makes all lower blocks more difficult to remove: there is more "weight" above each block. A transaction in a block 6 blocks deep (6 confirmations) will be very difficult to remove.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715187694
Hero Member
*
Offline Offline

Posts: 1715187694

View Profile Personal Message (Offline)

Ignore
1715187694
Reply with quote  #2

1715187694
Report to moderator
1715187694
Hero Member
*
Offline Offline

Posts: 1715187694

View Profile Personal Message (Offline)

Ignore
1715187694
Reply with quote  #2

1715187694
Report to moderator
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6631


Just writing some code


View Profile WWW
January 25, 2019, 05:52:14 PM
Merited by Foxpup (3), Jet Cash (3), Coding Enthusiast (1)
 #2

voltage?

This is just how you store integers. For integers that have a value less than 0xFD, you just have the integer as a single byte by itself. For an integer between 0xFD and 0xFFFF inclusive, you put the byte 0xFD followed by 2 bytes representing the integer. For integers between 0x010000 and 0xFFFFFFFF inclusive, you put 0xFE followed by 4 bytes representing the integer. Lastly for integers between 0x0100000000 and 0xFFFFFFFFFFFFFFFF, you put 0xFF followed by 8 bytes representing the integer.

jackg (OP)
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


https://bit.ly/387FXHi lightning theory


View Profile
January 25, 2019, 06:06:01 PM
 #3

Aha that's quite simple! Sorry for the hassle.

Voltages as in baud.

Anything bigger than 64 bit uints are sent as strings I know that much then before this...
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
January 26, 2019, 07:13:26 AM
 #4

That part is in serious need of at least 2 or 3 examples, I have had trouble understanding it too. This page has an example which can help.
More:
integer|compact|Base16
_________________|_______________|____________________________
252|{ 252 }|{ 0xfc }
253|{ 253, 253, 0 }|{ 0xfd, 0xfd, 0x00 }
UInt16.MaxValue|{ 253, 255, 255 }|{ 0xfd, 0xff, 0xff }
UInt16.MaxValue + 1|{ 254, 0, 0, 1, 0 }|{ 0xfe, 0x00, 0x00, 0x01, 0x00 }


Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
darosior
Sr. Member
****
Offline Offline

Activity: 279
Merit: 435


View Profile
January 26, 2019, 09:40:06 AM
 #5

Hi,

I had difficulties to understand it too the first time I read it. This page, written by in3rsha provides a good explanation about variable length integers (and on many other Bitcoin-related subjects).
jackg (OP)
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


https://bit.ly/387FXHi lightning theory


View Profile
January 26, 2019, 01:22:10 PM
 #6

Do you pad out the front with zeros?

If you have 0xFC9782 does it become 0xFE00FC9782? I've seen some randomly complicated padding before so I'm just checking.

Eg. Software I was using stored hex in index orders [3][1][4][2][0][5] for some reason...
darosior
Sr. Member
****
Offline Offline

Activity: 279
Merit: 435


View Profile
January 26, 2019, 02:48:42 PM
Last edit: January 26, 2019, 03:06:13 PM by darosior
Merited by ABCbits (1), Coding Enthusiast (1)
 #7

Do you pad out the front with zeros?

If you have 0xFC9782 does it become 0xFE00FC9782? I've seen some randomly complicated padding before so I'm just checking.

Eg. Software I was using stored hex in index orders [3][1][4][2][0][5] for some reason...
No you dont. If you can read Python code here is an implementation which I find easy to understand : https://github.com/Crypto-lyon/genesis-parser/blob/c01167295e01570baf687c323ea6b7b311e5ba23/parser.py#L3. However the comments are in french because I made it for a french workshop, so feel free to ask question if something seems magic.

EDIT : The algorithm
Read the first byte from the file descriptor
If the first byte is less than 0xfd :
     The number is the first byte
Else if the first byte is equal to 0xfd:
     Read the second byte
     The number is the second byte
Else if the first byte is equal to 0xfe:
     Read the second and third bytes
     The number is the second and third bytes
Else if the first byte is equal to 0xff:
     Read the second third and fourth bytes, this is the number
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
January 26, 2019, 03:04:51 PM
Merited by ABCbits (1)
 #8

It is not padding, you are representing an x-byte integer in little endian order. So for example when you have UInt16=253 it is actually 2 bytes in your memory (16-bit) and since it is little endian the least significant bit is at the beginning meaning { 253, 0 } and UInt16=256 would look like { 0, 1 } (256= 0*2560 + 1*2561)

If you have 0xFC9782 does it become 0xFE00FC9782?
The correct form is: fe8297fc00

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
jackg (OP)
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


https://bit.ly/387FXHi lightning theory


View Profile
January 26, 2019, 04:55:51 PM
 #9

Silly me for not looking into little edian...

Do strings of numbers (like the public key) get represented in big or little edian?
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
January 26, 2019, 06:17:33 PM
 #10

Do strings of numbers (like the public key) get represented in big or little edian?
Public key's X and Y coordinates are represented as big endian.

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!