fingerprinthkdf

On-chain HKDF-SHA256 key derivation

Derive cryptographic keys on-chain using Mercury EVM's HKDF precompile.

Overview

hkdf() and async_hkdf() call the HKDF precompile at address 0x68 to perform HKDF-SHA256 key derivation on input key material (IKM). The result is a uniformly distributed 32-byte derived key.

Signature

def hkdf(
    w3: Web3,
    ikm: bytes,
) -> Bytes32

async def async_hkdf(
    w3: AsyncWeb3,
    ikm: bytes,
) -> Bytes32

Parameters

Parameter
Type
Required
Description

w3

Web3 or AsyncWeb3

Yes

Web3 instance connected to a Seismic node

ikm

bytes

Yes

Input key material (arbitrary bytes)

Returns

Type
Description

32-byte derived key

Examples

Basic Usage

Derive from Password

Async Usage

Derive Multiple Keys

Use as AES Key

Derive from ECDH Output

Deterministic Key Derivation

Variable-Length Input

Key Separation by Context

How It Works

  1. Encode parameters - Passes input key material as-is

  2. Call precompile - Issues an eth_call to address 0x68 with gas proportional to input length

  3. HKDF derivation - Precompile performs HKDF-SHA256 extract and expand phases

  4. Return key - Returns first 32 bytes of derived key material

Gas Cost

Gas cost is calculated as:

For example:

  • 32 bytes IKM: ~6144 gas

  • 64 bytes IKM: ~6168 gas

  • 128 bytes IKM: ~6216 gas

Notes

  • Uses HKDF-SHA256 from RFC 5869

  • Always returns exactly 32 bytes regardless of input length

  • Input key material can be any length

  • The derivation is deterministic: same IKM always produces same output

  • Internally performs both HKDF-Extract and HKDF-Expand phases

  • The derived key has uniform distribution suitable for cryptographic use

Use Cases

  • Derive encryption keys from shared secrets

  • Convert non-uniform entropy into uniform keys

  • Key separation: derive multiple keys from one master secret

  • Post-process ECDH output for additional security

Warnings

  • Not for password hashing - Use proper password hashing algorithms (bcrypt, argon2) for passwords

  • Input entropy - Output security depends entirely on input entropy

  • Deterministic - Same input always yields same output (no salt/randomness added)

See Also

  • ecdh - Often used before HKDF to derive keys

  • aes-gcm-encrypt - Use derived keys for encryption

  • rng - Generate random input material

  • Bytes32 - 32-byte derived key type

Last updated