CompactSize Varint
CompactSize Varint
Bitcoin uses a compact variable-length integer encoding ("CompactSize") for counts and script lengths.
Encoding
- If
n < 0xfd, encode as a single byten. - If
n <= 0xffff, encode as0xfdfollowed by little-endian uint16. - If
n <= 0xffffffff, encode as0xfefollowed by little-endian uint32. - Otherwise, encode as
0xfffollowed by little-endian uint64.
Canonical decoding
Decoding must reject non-canonical encodings:
0xfdprefix is invalid if the value< 0xfd.0xfeprefix is invalid if the value< 0x10000.0xffprefix is invalid if the value< 0x100000000.
Functions
func EncodeCompactSize(n uint64) []byte
func DecodeCompactSize(data []byte) (value uint64, size int, ok bool)
Notes
DecodeCompactSizereturns(0, 0, false)on insufficient data or non-canonical encodings.sizeis the number of bytes consumed.
Run tests to see results
No issues detected