ecdh
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.
Precompile Address
0x0000000000000000000000000000000000000065Input Encoding
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
shared_secret
32 bytes
HKDF-derived shared secret (x-coordinate of ECDH point, post-processed)
Parameters
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
Encode parameters — Concatenates 32-byte private key and 33-byte compressed public key
Call precompile — Issues an
eth_callto address0x65with 3120 gasCompute ECDH — Precompile performs scalar multiplication on the secp256k1 curve
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
0x02or0x03)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
Precompiles Overview — All precompile reference
aes-gcm-encrypt — Encrypt with derived key
aes-gcm-decrypt — Decrypt with derived key
hkdf — Key derivation function
Encryption — How the provider uses ECDH internally
Last updated

