*

RLP

Recursive Length Prefix (RLP) serialization is used extensively in Ethereum's execution clients. RLP standardizes the transfer of data between nodes in a space-efficient format.

The purpose of RLP is to encode arbitrarily nested arrays of binary data, and RLP is the primary encoding method used to serialize objects in Ethereum's execution layer.

The only purpose of RLP is to encode structure; encoding specific data types (e.g. strings, floats) is left up to higher-order protocols;

You can read more of the definitions in Ethereum Yellow Paper.

In this guide, we'll use a interactive way to show how RLP works.

Let's take a first look at the RLP encoding.

HEX("abcdefghijklm")=0x6162636465666768696a6b6c6d
PBS("abcdefghijklm")=
0x0d6162636465666768696a6b6c6d
RLP("abcdefghijklm")=
0x8d6162636465666768696a6b6c6d

Try to change the value of the range input, and see how the output changes.

Keep in mind that although the output is a hex string, it is actually a byte array. The hex string is just a way to display the byte array. We can also use decimals to represent the byte array.

RLP only care about the byte array

If we want to encode a string, we need to convert it to a byte array first. Character a for example, it's a unicode character which is assigned to 0x61 in UTF-8 encoding. 0x61 is the representation of byte array in hexadecimal, which is 97 in decimal, which is less than 128. So the RLP encoding of a is also 0x61.

To make it easier to understand, we'll use a square block to represent the original character and its RLP encode.

61
a

RLP encoding is defined as follows:

Single Byte

  • If the byte array contains solely a single byte and that single byte is <128, then the input is exactly equal to the output.

  • If the byte array contains solely a single byte and that single byte is ≧128, then the output is equal to the input prefixed. by the byte equal to the length of the byte array plus 128.

00
0
Binary
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Decimal
0
Hex
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
ASCII
NUL
Action

Multiple Bytes

If the byte array contains more than one byte but fewer than 56 bytes, then the output is equal to the input prefixed. by the the length of the bytes plus 128 (or 0x80 in hex).

If the byte array contains more than 55 bytes, but fewer than 264 bytes, the output will have two parts of prefix:

  1. The first prefix is 183 plus the byte length of the second prefix.

  2. The second prefix by a minimal-length byte array that, when interpreted as a big-endian integer, represents the length of the input.

Drag the slider in the demo below to see how the RLP encoding changes.

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
87
61
a
62
b
63
c
64
d
65
e
66
f
67
g
264

Byte arrays containing 264 or more bytes cannot be encoded. This restriction ensures that the first byte of the encoding of a byte array is always below 192, and thus it can be readily distinguished from the encodings of sequences in L.

Sequences

If the concatenated serialisations of each contained item is less than 56 bytes in length, then the output is equal to that concatenation prefixed by the byte equal to the length of this byte array plus 192.

c3
61
a
62
b
63
c

Otherwise, the output is equal to the concatenated serialisations, provided that they contain fewer than 264 bytes, prefixed by the minimal-length byte array which when interpreted as a big-endian integer is equal to the length of the concatenated serialisations byte array, which is itself prefixedby the number of bytes required to faithfully encode this length value plus 247

f8
42
61
a
62
b
b8
3e
61
a
62
b
63
c
64
d
65
e
66
f
67
g
68
h
69
i
6a
j
6b
k
6c
l
6d
m
6e
n
6f
o
70
p
71
q
72
r
73
s
74
t
75
u
76
v
77
w
78
x
79
y
7a
z
41
A
42
B
43
C
44
D
45
E
46
F
47
G
48
H
49
I
4a
J
4b
K
4c
L
4d
M
4e
N
4f
O
50
P
51
Q
52
R
53
S
54
T
55
U
56
V
57
W
58
X
59
Y
5a
Z
31
1
32
2
33
3
34
4
35
5
36
6
37
7
38
8
39
9
30
0
Concatenated Serialisations

Sequences whose concatenated serialized items contain 264 or more bytes cannot be encoded. This restriction ensures that the first byte of the encoding does not exceed 255 (otherwise it would not be a byte)