rng
Generate random bytes on-chain
Generate cryptographically secure random bytes on-chain using Mercury EVM's RNG precompile.
Overview
The RNG precompile at address 0x64 generates random bytes directly on the Seismic node using the Strobe128 construction. The random value is returned as raw bytes (padded to 32 bytes).
Precompile Address
0x0000000000000000000000000000000000000064Input Encoding
num_bytes
4 bytes (big-endian u32)
Number of random bytes to generate (1--32)
personalization
Variable (optional)
Optional personalization bytes to seed the RNG
The input is the concatenation of num_bytes followed by the optional personalization bytes.
Output Format
random_bytes
32 bytes
Random output (padded to 32 bytes, big-endian)
Parameters
num_bytes
u32
Yes
Number of random bytes to generate (1--32)
personalization
&[u8]
No
Optional personalization string to domain-separate random values
Examples
Basic Usage
With Personalization
Generate Multiple Random Values
Convert to Fixed-Size Array
How It Works
Encode parameters —
num_bytesis encoded as a 4-byte big-endian integer, followed by optionalpersonalizationbytesCall precompile — Issues an
eth_callto address0x64with estimated gasGenerate randomness — The precompile uses Strobe128 internally for cryptographic security
Decode result — Result bytes are padded to 32 bytes and returned as big-endian
Gas Cost
The gas cost is calculated as:
The base cost is 3500 gas, with 5 gas per 136-byte block for personalization and output.
Notes
num_bytesmust be between 1 and 32 (inclusive)The RNG uses Strobe128 internally for cryptographic security
Each call generates independent random values
The personalization string can be used to domain-separate random values
The result is padded to 32 bytes and returned as a big-endian value
Warnings
Range validation —
num_bytesoutside 1--32 will cause the precompile to revertNode connectivity — Requires a working connection to a Seismic node
Not for consensus — Results may differ across nodes due to timing
See Also
Precompiles Overview — All precompile reference
ecdh — On-chain ECDH key exchange
hkdf — On-chain HKDF key derivation
Last updated

