Skip to main content

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:
  1. String - Byte sequences (including empty)
  2. 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.
Rule: if length == 1 && byte < 0x80: output = byte

2. Short String [0-55 bytes]

For strings of 0-55 bytes, prepend 0x80 + length.
Rule: 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:
  1. Encode length as big-endian integer (minimal bytes)
  2. Prepend 0xb7 + length_of_length
  3. Append actual bytes
Rule: 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:
  1. RLP-encode each item
  2. Concatenate encoded items
  3. Prepend 0xc0 + total_length
Rule: if total_length < 56: output = [0xc0 + total_length, ...encoded_items]

2. Long List [56+ bytes total]

For lists where total encoded payload >= 56 bytes:
  1. RLP-encode each item
  2. Calculate total length
  3. Encode length as big-endian (minimal)
  4. Prepend 0xf7 + length_of_length
Rule: 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:

Length Encoding

Big-endian encoding with no leading zeros:

Decoding Algorithm

Decoding reverses the encoding process:
  1. Read prefix byte
  2. Determine type and length based on prefix range
  3. Extract data according to type
  4. 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 function RLP: 𝕋 → 𝔹 Where:
  • 𝕋 is the set of all trees of byte sequences
  • 𝔹 is the set of byte sequences (RLP output)
Rules:
Notation:
  • |x| = length of x in bytes
  • ||x|| = number of bytes needed to encode |x|
  • BE(n) = big-endian encoding of n
  • · = concatenation

Security Considerations

DOS Prevention

Recursion Depth Limit: Maximum depth of 32 prevents stack overflow. Length Validation: All declared lengths must match actual data. Canonical Enforcement: Rejects non-minimal encodings to prevent malleability.

Malleability

RLP canonical encoding prevents transaction malleability:

Implementation Notes

Efficiency

Minimal Allocations: Pre-calculate sizes to allocate once. Buffer Reuse: Reuse buffers for repeated encoding. Stream Processing: Decode incrementally for large data.

Correctness

Canonical Validation: Enforce all canonical rules during decode. Length Checks: Validate sufficient data before reading. Type Safety: Use tagged unions for RLP data structures.
  • Encoding - Encoding implementation
  • Decoding - Decoding implementation
  • Types - Type system
  • WASM - High-performance implementation