keyecdh

On-chain elliptic-curve Diffie-Hellman key exchange

Perform on-chain ECDH key exchange using Mercury EVM's ECDH precompile.

Overview

The ECDH precompile at address 0x65 computes a shared secret from a private key and a public key using elliptic-curve Diffie-Hellman on the secp256k1 curve. The result is a 32-byte shared secret derived via HKDF.

circle-info

The SeismicSignedProvider uses this precompile internally during TEE key exchange. You can also call it directly for custom key agreement workflows.

Precompile Address

0x0000000000000000000000000000000000000065

Input Encoding

Field
Size
Description

secret_key

32 bytes

secp256k1 private key

public_key

33 bytes

Compressed secp256k1 public key (starts with 0x02 or 0x03)

The input is the concatenation of secret_key (32 bytes) followed by public_key (33 bytes), for a total of 65 bytes.

Output Format

Field
Size
Description

shared_secret

32 bytes

HKDF-derived shared secret (x-coordinate of ECDH point, post-processed)

Parameters

Parameter
Type
Required
Description

secret_key

[u8; 32]

Yes

32-byte secp256k1 private key

public_key

[u8; 33]

Yes

33-byte compressed secp256k1 public key

Examples

Basic Usage

Two-Party Key Exchange

Use with AES Encryption

How It Works

  1. Encode parameters -- Concatenates 32-byte private key and 33-byte compressed public key

  2. Call precompile -- Issues an eth_call to address 0x65 with 3120 gas

  3. Compute ECDH -- Precompile performs scalar multiplication on the secp256k1 curve

  4. Derive key -- Applies HKDF to the ECDH point to produce a 32-byte uniform secret

Gas Cost

Fixed gas cost: 3120 gas

  • 3000 gas for ECDH scalar multiplication

  • 120 gas for HKDF key derivation

Notes

  • Uses the secp256k1 elliptic curve (same as Ethereum)

  • Public keys must be in compressed format (33 bytes starting with 0x02 or 0x03)

  • The ECDH point is passed through HKDF for key uniformity

  • Both parties compute the same shared secret: ecdh(sk_A, pk_B) == ecdh(sk_B, pk_A)

  • The shared secret can be used directly as an AES-256 key

Warnings

  • Private key security -- Never expose or log private keys

  • Public key validation -- Invalid public keys will cause the precompile to revert

  • Key reuse -- Using the same keypair for multiple sessions reduces forward secrecy

See Also

Last updated