RECURSIVE-LENGTH PREFIX (RLP) SERIALIZATION

Recursive Length Prefix (RLP) serialization is widely used in Hash Ahead's execution clients. Data is transferred between nodes in a space-efficient format, and recursive length prefixing standardizes this process. The purpose of the recursive length prefix is to encode arbitrary nested binary data arrays, and the recursive length prefix is the main encoding method used to serialize objects in the Hash Ahead execution layer. The sole purpose of a recursive length-prefix is to encode structures; the work of encoding specific data types (e.g. strings, floats) is left to higher-order protocols; however, positive recursive length-prefixed integers MUST start with no leading zeros Represented in big-endian binary form (so that the integer value zero is equivalent to an empty byte array). Deserialized positive integers with leading zeros are considered invalid. The integer representation of the string length must also be encoded in this way, as do the integers in the payload.

To encode a dictionary with a recursive length prefix, the two suggested canonical forms are:

  • Use [[k1,v1],[k2,v2]...] plus the keys sorted lexicographically

  • Use higher level prefix tree encoding like Hash Ahead

Definition

The recursive length-prefix encoding function accepts an item. The item is defined as follows:

  • A string (i.e. byte array) is an item

  • A list of items is also an item

For example, all of the following are projects:

  • Empty string;

  • A string containing the word "cat";

  • A list containing any number of strings;

  • And more complex data structures such as ["cat", ["puppy", "cow"], "horse", [[]], "pig", [""], "sheep"]

Note that in the context of the rest of this page, "string" means "a certain number of bytes of binary data"; no special encoding is used, and nothing is implied about the contents of the string.

The recursive length-prefix encoding is defined as follows:

  • For a single byte whose value is in the [0x00, 0x7f] (decimal [0, 127]) range, that byte is its own RLP encoding.

  • Otherwise, if a string is 0-55 bytes long, the RLP encoding consists of a single byte with value 0x80 (dec. 128) plus the length of the string followed by the string. The range of the first byte is thus [0x80, 0xb7] (dec. [128, 183]).

  • If a string is more than 55 bytes long, the RLP encoding consists of a single byte with value 0xb7 (dec. 183) plus the length in bytes of the length of the string in binary form, followed by the length of the string, followed by the string. For example, a 1024 byte long string would be encoded as \xb9\x04\x00 (dec. 185, 4, 0) followed by the string. Here, 0xb9 (183 + 2 = 185) as the first byte, followed by the 2 bytes 0x0400 (dec. 1024) that denote the length of the actual string. The range of the first byte is thus [0xb8, 0xbf] (dec. [184, 191]).

  • If the total payload of a list (i.e. the combined length of all its items being RLP encoded) is 0-55 bytes long, the RLP encoding consists of a single byte with value 0xc0 plus the length of the list followed by the concatenation of the RLP encodings of the items. The range of the first byte is thus [0xc0, 0xf7] (dec. [192, 247]).

  • If the total payload of a list is more than 55 bytes long, the RLP encoding consists of a single byte with value 0xf7 plus the length in bytes of the length of the payload in binary form, followed by the length of the payload, followed by the concatenation of the RLP encodings of the items. The range of the first byte is thus [0xf8, 0xff] (dec. [248, 255]).

In code, this is:

EXAMPLES

  • the string "dog" = [ 0x83, 'd', 'o', 'g' ]

  • the list [ "cat", "dog" ] = [ 0xc8, 0x83, 'c', 'a', 't', 0x83, 'd', 'o', 'g' ]

  • the empty string ('null') = [ 0x80 ]

  • the empty list = [ 0xc0 ]

  • the integer 0 = [ 0x80 ]

  • the encoded integer 0 ('\x00') = [ 0x00 ]

  • the encoded integer 15 ('\x0f') = [ 0x0f ]

  • the encoded integer 1024 ('\x04\x00') = [ 0x82, 0x04, 0x00 ]

  • the set theoretical representation(opens in a new tab)↗ of three, [ [], [[]], [ [], [[]] ] ] = [ 0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0 ]

  • the string "Lorem ipsum dolor sit amet, consectetur adipisicing elit" = [ 0xb8, 0x38, 'L', 'o', 'r', 'e', 'm', ' ', ... , 'e', 'l', 'i', 't' ]

RLP DECODING

According to the rules and process of RLP encoding, the input of RLP decode is regarded as an array of binary data. The RLP decoding process is as follows:

  1. according to the first byte (i.e. prefix) of input data and decoding the data type, the length of the actual data and offset;

  2. according to the type and offset of data, decode the data correspondingly;

  3. continue to decode the rest of the input;

Among them, the rules of decoding data types and offset is as follows:

  1. the data is a string if the range of the first byte (i.e. prefix) is [0x00, 0x7f], and the string is the first byte itself exactly;

  2. the data is a string if the range of the first byte is [0x80, 0xb7], and the string whose length is equal to the first byte minus 0x80 follows the first byte;

  3. the data is a string if the range of the first byte is [0xb8, 0xbf], and the length of the string whose length in bytes is equal to the first byte minus 0xb7 follows the first byte, and the string follows the length of the string;

  4. the data is a list if the range of the first byte is [0xc0, 0xf7], and the concatenation of the RLP encodings of all items of the list which the total payload is equal to the first byte minus 0xc0 follows the first byte;

  5. the data is a list if the range of the first byte is [0xf8, 0xff], and the total payload of the list whose length is equal to the first byte minus 0xf7 follows the first byte, and the concatenation of the RLP encodings of all items of the list follows the total payload of the list;

In code, this is:

Last updated