wand-magic-sparkleshkdf

On-chain HKDF-SHA256 key derivation

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

Overview

The HKDF precompile at address 0x68 performs HKDF-SHA256 key derivation on input key material (IKM). It produces a uniformly distributed 32-byte derived key suitable for use as an AES-256 key or other cryptographic purpose.

circle-info

The SeismicSignedProvider uses this precompile internally to derive AES keys from ECDH shared secrets during the TEE key exchange. You can also call it directly for custom key derivation workflows.

Precompile Address

0x0000000000000000000000000000000000000068

Input Encoding

Field
Size
Description

ikm

Variable

Input key material (arbitrary bytes)

The input is the raw IKM bytes -- no additional encoding is needed.

Output Format

Field
Size
Description

derived_key

32 bytes

HKDF-SHA256 derived key

Parameters

Parameter
Type
Required
Description

ikm

&[u8]

Yes

Input key material (arbitrary bytes)

Examples

Basic Usage

Derive Multiple Keys by Context

Use as AES Key

Derive from ECDH Output

Deterministic Key Derivation

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:

IKM Size
Gas Cost

32 bytes

~6144

64 bytes

~6168

128 bytes

~6216

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 the 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 the same output (no salt or randomness is added)

See Also

Last updated