dicerng

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

0x0000000000000000000000000000000000000064

Input Encoding

Field
Size
Description

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

Field
Size
Description

random_bytes

32 bytes

Random output (padded to 32 bytes, big-endian)

Parameters

Parameter
Type
Required
Description

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

  1. Encode parameters -- num_bytes is encoded as a 4-byte big-endian integer, followed by optional personalization bytes

  2. Call precompile -- Issues an eth_call to address 0x64 with estimated gas

  3. Generate randomness -- The precompile uses Strobe128 internally for cryptographic security

  4. 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_bytes must 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_bytes outside 1--32 will cause the precompile to revert

  • Node connectivity -- Requires a working connection to a Seismic node

  • Not for consensus -- Results may differ across nodes due to timing

See Also

Last updated