Skip to main content

Try it Live

Run RLP examples in the interactive playground

    Decoding Algorithm

    RLP decoder uses the first byte (prefix) to determine data type and length:

    Prefix Ranges

    Single Byte (0x00-0x7f)

    Bytes with value < 0x80 encode as themselves:

    Short String (0x80-0xb7)

    Length encoded in prefix: length = prefix - 0x80

    Long String (0xb8-0xbf)

    Length-of-length encoding: lengthOfLength = prefix - 0xb7

    Short List (0xc0-0xf7)

    Total payload length in prefix: length = prefix - 0xc0

    Long List (0xf8-0xff)

    Length-of-length encoding: lengthOfLength = prefix - 0xf7

    Usage Patterns

    Extract Transaction Data

    Decode transaction bytes and extract fields:

    Stream Decoding

    Decode multiple RLP values from a stream:

    Validate and Extract

    Decode with validation and type checking:

    Recursive Flattening

    Flatten nested lists to extract all byte values:

    Canonical Validation

    RLP decoder enforces canonical encoding rules to prevent malleability:

    Non-canonical Single Byte

    Single bytes < 0x80 must not have a length prefix:

    Non-canonical Short Form

    Strings < 56 bytes must use short form:

    Leading Zeros

    Length encodings must not have leading zeros:

    Length Mismatches

    Declared length must match actual data:

    Error Handling

    Comprehensive error handling for malformed input:

    Error Types Reference

    ErrorCauseFix
    InputTooShortNot enough bytes for declared lengthProvide complete data
    InvalidRemainderExtra bytes after value (non-stream)Use stream=true or trim input
    NonCanonicalSizeNon-minimal length encodingUse canonical encoding
    LeadingZerosLength has leading zero bytesRemove leading zeros from length
    InvalidLengthList payload length mismatchFix list item encodings
    RecursionDepthExceededNested > 32 levels deepReduce nesting depth
    UnexpectedInputInvalid prefix or formatCheck input is valid RLP

    Performance

    Depth Limiting

    Maximum recursion depth is 32 to prevent stack overflow:

    Stream Mode Efficiency

    Use stream mode to avoid re-parsing when decoding multiple values:

    Round-trip Encoding

    Decode and re-encode produces identical bytes (for canonical input):
    Validation ensures malformed RLP can’t cause issues downstream. The performance cost is negligible compared to security benefits.

    See Also