Try it Live
Run RLP examples in the interactive playground
RLP Algorithm
Complete specification of RLP (Recursive Length Prefix) encoding algorithm as defined in the Ethereum Yellow Paper.Overview
RLP is a serialization method that encodes arbitrarily nested arrays of binary data. The encoding is deterministic, space-efficient, and simple to implement. Design Goals:- Deterministic - Same input always produces identical output
- Minimal - No type information, only structure
- Efficient - Minimal overhead for encoding
- Simple - Straightforward rules, easy to implement
Specification
RLP defines encoding for two data types:- String - Byte sequences (including empty)
- List - Arrays of RLP-encodable items (including nested lists)
String Encoding
Three cases based on byte length:1. Single Byte [0x00, 0x7f]
For a single byte with value in range [0x00, 0x7f], the byte encodes as itself.if length == 1 && byte < 0x80: output = byte
2. Short String [0-55 bytes]
For strings of 0-55 bytes, prepend0x80 + length.
if 0 <= length <= 55: output = [0x80 + length, ...bytes]
Note: Single byte >= 0x80 uses this encoding, not the single-byte rule.
3. Long String [56+ bytes]
For strings of 56 or more bytes:- Encode length as big-endian integer (minimal bytes)
- Prepend
0xb7 + length_of_length - Append actual bytes
if length >= 56: output = [0xb7 + len(BE(length)), BE(length), ...bytes]
Where BE(length) is big-endian encoding with no leading zeros.
List Encoding
Two cases based on total payload length:1. Short List [0-55 bytes total]
For lists where total encoded payload < 56 bytes:- RLP-encode each item
- Concatenate encoded items
- Prepend
0xc0 + total_length
if total_length < 56: output = [0xc0 + total_length, ...encoded_items]
2. Long List [56+ bytes total]
For lists where total encoded payload >= 56 bytes:- RLP-encode each item
- Calculate total length
- Encode length as big-endian (minimal)
- Prepend
0xf7 + length_of_length
if total_length >= 56: output = [0xf7 + len(BE(length)), BE(length), ...encoded_items]
Canonical Encoding
RLP enforces canonical encoding to ensure deterministic serialization:Rule 1: Single Byte
Single bytes < 0x80 must not have a length prefix.Rule 2: Minimal Length Encoding
Lengths must use minimum number of bytes (no leading zeros).Rule 3: Correct Form Selection
Must use short form for lengths < 56.Prefix Byte Ranges
Summary of all prefix byte meanings:| Range | Type | Meaning |
|---|---|---|
0x00-0x7f | Single byte | Byte value itself |
0x80-0xb7 | Short string | length = prefix - 0x80 (0-55 bytes) |
0xb8-0xbf | Long string | len_of_len = prefix - 0xb7 (56+ bytes) |
0xc0-0xf7 | Short list | length = prefix - 0xc0 (0-55 bytes total) |
0xf8-0xff | Long list | len_of_len = prefix - 0xf7 (56+ bytes total) |
Length Encoding
Big-endian encoding with no leading zeros:Decoding Algorithm
Decoding reverses the encoding process:- Read prefix byte
- Determine type and length based on prefix range
- Extract data according to type
- Recursively decode list items
Examples
Example 1: Simple String
Example 2: Empty String
Example 3: List of Strings
Example 4: Empty List
Example 5: Integer (as big-endian bytes)
Ethereum Yellow Paper Reference
RLP is formally specified in Appendix B of the Ethereum Yellow Paper: Definition: RLP functionRLP: 𝕋 → 𝔹
Where:
𝕋is the set of all trees of byte sequences𝔹is the set of byte sequences (RLP output)
|x|= length of x in bytes||x||= number of bytes needed to encode |x|BE(n)= big-endian encoding of n·= concatenation

