Skip to content

High-performance RLP encoder/decoder for PHP, written in C as a native extension. Fully compatible with Ethereum’s Recursive Length Prefix (RLP) specification. Offers 10–30x performance gains over pure PHP libraries like kornrunner/rlp.

License

Notifications You must be signed in to change notification settings

RandomCoderTinker/php-rlp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

php-rlp

PHP Version License Version

A high-performance PHP extension for RLP (Recursive Length Prefix) encoding and decoding, compatible with Ethereum’s transaction and contract formats. Built natively in C for maximum speed, fully interoperable with KornRunner’s RLP implementation and EVM tooling.


Functions

Function Purpose
rlp_encode() Encode a string, integer, or array to RLP
rlp_decode() Decode an RLP string into PHP types

EVM Compatibility

This extension matches Ethereum’s canonical RLP format used for:

  • Transactions (nonce, gasPrice, to, value, data, v, r, s)
  • Smart contract deployments and calls
  • EIP-155 chain-ID encoding
  • Event logs and receipts

The encoder supports:

  • Raw binary and text strings
  • Hex strings prefixed with 0x (converted to binary before encoding)
  • Integers (int, long)
  • Arrays (any nested structure)

The decoder returns:

  • PHP string for raw values
  • PHP array for RLP lists

Installation

Prerequisites

sudo apt install php-dev autoconf make gcc

Build & Install

git clone https://github.com/RandomCoderTinker/php-rlp.git
cd php-rlp
phpize
./configure
make
sudo make install

Enable in php.ini:

extension=php_rlp.so

Restart PHP-FPM or your web server.


Usage

<?php

// RLP encode/decode strings
echo bin2hex(rlp_encode("Dog"));   // 83446f67
echo rlp_decode(hex2bin("83446f67")); // Dog

// RLP encode an Ethereum transaction
$tx = [
    'nonce'    => '0x00',
    'gasPrice' => '0x09184e72a000',
    'gasLimit' => '0x2710',
    'to'       => '0x0000000000000000000000000000000000000000',
    'value'    => '0x00',
    'data'     => '',
    'v'        => '0x1b',
    'r'        => '0x00',
    's'        => '0x00'
];

$encoded = rlp_encode($tx);
echo bin2hex($encoded);

// Decode
$decoded = rlp_decode($encoded);
print_r($decoded);

Output Format

Type PHP Input RLP Output (hex)
String "Dog" 83446f67
Hex "0xdeadbeef" 84deadbeef
Int 15 0f
Array ["cat", "dog"] c88363617483646f67
Tx Ethereum Tx array Matches EVM / web3 compatible form

Performance Benchmarks

Operation C Extension KornRunner (PHP) Speedup
Encode 6.667 ms 88.314 ms ~13.2×
Decode 2.874 ms 73.849 ms ~25.7×

Benchmarked over 10,000 iterations on identical hardware.


Real-world Use Cases

This extension is ideal for:

  • Ethereum & L2 Transaction Builders
  • EVM-compatible VM integration
  • Smart contract encode/decode
  • Custom chains and wallets
  • High-performance RPC & Bridge Layers

License

MIT © RandomCoderTinker


Built with ❤️ using PHP 8.1+

About

High-performance RLP encoder/decoder for PHP, written in C as a native extension. Fully compatible with Ethereum’s Recursive Length Prefix (RLP) specification. Offers 10–30x performance gains over pure PHP libraries like kornrunner/rlp.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published