PHPackages                             amashukov/rlp-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. amashukov/rlp-php

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

amashukov/rlp-php
=================

RLP (Recursive Length Prefix) encoder and decoder in pure PHP — Ethereum's canonical serialization for transactions, blocks, and state trie.

v0.1.0(2w ago)0213↑118.2%2MITPHPPHP &gt;=8.3CI passing

Since May 24Pushed 2w agoCompare

[ Source](https://github.com/AndreyMashukov/rlp-php)[ Packagist](https://packagist.org/packages/amashukov/rlp-php)[ RSS](/packages/amashukov-rlp-php/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (5)Versions (2)Used By (2)

amashukov/rlp-php
=================

[](#amashukovrlp-php)

Pure-PHP RLP (Recursive Length Prefix) encoder and decoder — Ethereum's canonical serialization for transactions, blocks and the state trie.

[![CI](https://camo.githubusercontent.com/cb64c4d80f3a029f41a26cfb8f5c1299ec155a563b30be432915850704ea590f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f416e647265794d617368756b6f762f726c702d7068702f63692e796d6c3f6272616e63683d6d61696e266c6162656c3d4349)](https://github.com/AndreyMashukov/rlp-php/actions)[![PHPStan L9](https://camo.githubusercontent.com/33d9da454d70d18954316f8bc6b686aa9c654bae500cea184ca412c5054c224a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f416e647265794d617368756b6f762f726c702d7068702f7374616e2e796d6c3f6272616e63683d6d61696e266c6162656c3d5048505374616e2532304c39)](https://github.com/AndreyMashukov/rlp-php/actions)[![Latest Version](https://camo.githubusercontent.com/c427e616a5dbea61fdf8c4207f4de655b0ff5bf1feea4ffc669099ef137dd689/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616d617368756b6f762f726c702d706870)](https://packagist.org/packages/amashukov/rlp-php)[![Downloads](https://camo.githubusercontent.com/e741e8081fca9b1bcc218d6ef1a04b763448690f4a113b4cd3ef3a413f88d4bf/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616d617368756b6f762f726c702d706870)](https://packagist.org/packages/amashukov/rlp-php)[![PHP](https://camo.githubusercontent.com/8ddabdac88a51064bd026209f68414cd26c8684225e7650c98e8537a35ed296e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f616d617368756b6f762f726c702d7068702f706870)](https://packagist.org/packages/amashukov/rlp-php)[![License](https://camo.githubusercontent.com/5d76042891bf237cf84e6d3ed380937797dcf812bcb0042fe1ed4bf27b94565c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f616d617368756b6f762f726c702d706870)](LICENSE)[![Stars](https://camo.githubusercontent.com/4d710532f1be28f899d11de70383dc1cafd95cca83d74d2aace468ff26c2d50e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f416e647265794d617368756b6f762f726c702d7068703f7374796c653d736f6369616c)](https://github.com/AndreyMashukov/rlp-php)

`amashukov/rlp-php` is a pure-PHP implementation of RLP (Recursive Length Prefix), the canonical serialization format used throughout Ethereum and the EVM for transactions, blocks and the Merkle-Patricia state trie. It encodes and decodes nested byte-string structures and minimal big-endian integers, and enforces strict RLP canonicality on decode to defend against malleable encodings.

The package is a leaf primitive — zero composer dependencies, just `ext-gmp` for big-integer encoding helpers.

Features
--------

[](#features)

- **Encode &amp; decode** arbitrarily nested lists of byte-strings.
- **Integer encoding** in Ethereum's minimal big-endian convention, including big integers via decimal strings.
- **Strict canonical decode** — rejects non-canonical length prefixes, leading-zero lengths, trailing bytes and truncated input.
- **Stream decoding** for parsing concatenated RLP items (e.g. a sequence of transactions).
- **Zero composer dependencies** — `ext-gmp` only.
- PHPStan level 9 clean, `@PER-CS` formatted, CI-tested.

Installation
------------

[](#installation)

```
composer require amashukov/rlp-php
```

Usage
-----

[](#usage)

### Encode

[](#encode)

```
use Amashukov\Rlp\Rlp;

// Empty string.
Rlp::encode('');                              // 0x80

// Single byte < 0x80 is its own encoding.
Rlp::encode("\x7f");                          // 0x7f

// Short string (≤ 55 bytes).
Rlp::encode('dog');                           // 0x83 'd' 'o' 'g'

// Long string (> 55 bytes).
Rlp::encode(str_repeat('a', 56));             // 0xb8 0x38 'a' × 56

// Empty list.
Rlp::encode([]);                              // 0xc0

// Flat list of byte-strings.
Rlp::encode(['cat', 'dog']);                  // 0xc8 0x83 'cat' 0x83 'dog'

// Nested list.
Rlp::encode([[], [[]], [[], [[]]]]);          // 0xc7 0xc0 0xc1 0xc0 0xc3 0xc0 0xc1 0xc0
```

### Encode integers (Ethereum convention: minimal big-endian)

[](#encode-integers-ethereum-convention-minimal-big-endian)

```
Rlp::encodeInt(0);                            // 0x80  (empty bytes)
Rlp::encodeInt(1);                            // 0x01
Rlp::encodeInt(127);                          // 0x7f
Rlp::encodeInt(128);                          // 0x81 0x80
Rlp::encodeInt(1024);                         // 0x82 0x04 0x00
Rlp::encodeInt('1000000000000000000');        // big-int via decimal string (1 ETH in wei)
```

### Decode

[](#decode)

```
Rlp::decode("\x80");                          // ''
Rlp::decode("\x83dog");                       // 'dog'
Rlp::decode("\xc8\x83cat\x83dog");            // ['cat', 'dog']
Rlp::decode("\xc7\xc0\xc1\xc0\xc3\xc0\xc1\xc0"); // [[], [[]], [[], [[]]]]
```

Leaves are always returned as raw byte strings; integers are the caller's interpretation step.

### Stream decoding

[](#stream-decoding)

For parsing concatenated RLP items (e.g. a stream of transactions), use `decodeStream`:

```
$stream    = Rlp::encode('first') . Rlp::encode('second');
[$first,  $consumed]  = Rlp::decodeStream($stream, 0);
[$second, $consumed2] = Rlp::decodeStream($stream, $consumed);
// $first = 'first', $second = 'second'
```

Canonical decoding
------------------

[](#canonical-decoding)

`decode()` enforces RLP canonicality. The following inputs are rejected with `InvalidArgumentException`:

- A single byte &lt; 0x80 wrapped in a long-form prefix (`0x81 0x00`).
- A short string encoded as a long string (length &lt; 56 with `0xb8…` prefix).
- A short list encoded as a long list (payload length &lt; 56 with `0xf8…` prefix).
- A long-form length with a leading zero byte (`0xb9 0x00 0x38 …`).
- Trailing bytes after the root item.
- Truncated input where the declared length exceeds the available bytes.

These checks defend downstream consumers against malleable transaction encodings and trie tampering.

Requirements
------------

[](#requirements)

- PHP 8.3+
- `ext-gmp`

No composer dependencies.

Related packages
----------------

[](#related-packages)

Part of a modular pure-PHP blockchain toolkit:

PackagePurpose[amashukov/keccak-php](https://github.com/AndreyMashukov/keccak-php)Keccak-256 / SHA-3 / SHAKE hashing[amashukov/secp256k1-php](https://github.com/AndreyMashukov/secp256k1-php)secp256k1 ECDSA sign / verify / recover[amashukov/rlp-php](https://github.com/AndreyMashukov/rlp-php)Ethereum RLP encode / decode[amashukov/ton-cell-php](https://github.com/AndreyMashukov/ton-cell-php)TON TLB Cell / Builder / Slice / BOC[amashukov/eip1559-tx-signer-php](https://github.com/AndreyMashukov/eip1559-tx-signer-php)EIP-1559 transaction signer[amashukov/abi-encoder-php](https://github.com/AndreyMashukov/abi-encoder-php)Ethereum ABI encoder[amashukov/eth-rpc-client-php](https://github.com/AndreyMashukov/eth-rpc-client-php)Ethereum JSON-RPC client[amashukov/eth-php](https://github.com/AndreyMashukov/eth-php)EVM umbrella packageQuality
-------

[](#quality)

- PHPStan level 9.
- php-cs-fixer with the `@PER-CS` ruleset.
- GitHub Actions CI on every push.
- Test vectors validated against the Ethereum RLP / Yellow Paper reference outputs.

Reference
---------

[](#reference)

- Ethereum RLP specification:
- Yellow Paper §B (Recursive Length Prefix):

License
-------

[](#license)

MIT License.

###  Health Score

42

—

FairBetter than 88% of packages

Maintenance97

Actively maintained with recent releases

Popularity16

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

17d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4b3e86ca79a82515b23e0239b1d3616c747e59b64c40e169d538d5c759c9bbc4?d=identicon)[Andrey Mashukov](/maintainers/Andrey%20Mashukov)

---

Top Contributors

[![AndreyMashukov](https://avatars.githubusercontent.com/u/24682497?v=4)](https://github.com/AndreyMashukov "AndreyMashukov (3 commits)")

---

Tags

blockchainencodingethereumevmphprlpserializationencodingserializationethereumrlp

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/amashukov-rlp-php/health.svg)

```
[![Health](https://phpackages.com/badges/amashukov-rlp-php/health.svg)](https://phpackages.com/packages/amashukov-rlp-php)
```

###  Alternatives

[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k233.3M312](/packages/opis-closure)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k139.8M905](/packages/jms-serializer)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.9k91.4M662](/packages/jms-serializer-bundle)[google/flatbuffers

FlatBuffers for PHP

26.0k144.0k5](/packages/google-flatbuffers)[apache/avro

Apache Avro™ is a data serialization system.

3.3k50.6k3](/packages/apache-avro)[flix-tech/avro-serde-php

A library to serialize and deserialize Avro records making use of the confluent schema registry

684.1M19](/packages/flix-tech-avro-serde-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
