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.
Function | Purpose |
---|---|
rlp_encode() |
Encode a string, integer, or array to RLP |
rlp_decode() |
Decode an RLP string into PHP types |
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
sudo apt install php-dev autoconf make gcc
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.
<?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);
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 |
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.
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
MIT © RandomCoderTinker
Built with ❤️ using PHP 8.1+