aes_gcm_encrypt
On-chain AES-256-GCM encryption
Encrypt data on-chain using Mercury EVM's AES-GCM encryption precompile.
Overview
The AES-GCM encryption precompile at address 0x66 performs AES-256-GCM authenticated encryption. The ciphertext includes a 16-byte authentication tag that ensures integrity and authenticity.
Precompile Address
0x0000000000000000000000000000000000000066Input Encoding
key
32 bytes
AES-256 encryption key
nonce
12 bytes
Unique nonce (must never be reused with the same key)
plaintext
Variable
Data to encrypt
The input is the concatenation of key (32 bytes) + nonce (12 bytes) + plaintext (variable length).
Output Format
ciphertext
len(plaintext) bytes
Encrypted data
tag
16 bytes
Authentication tag (appended to ciphertext)
Total output length = len(plaintext) + 16.
Parameters
key
[u8; 32]
Yes
32-byte AES-256 encryption key
nonce
[u8; 12]
Yes
12-byte nonce (must be unique per key)
plaintext
&[u8]
Yes
Data to encrypt
Examples
Basic Usage
Encrypt with Integer Nonce
Encrypt-Decrypt Round Trip
With ECDH-Derived Key
How It Works
Encode parameters — Concatenates 32-byte key + 12-byte nonce + plaintext
Call precompile — Issues an
eth_callto address0x66with estimated gasEncrypt data — Precompile performs AES-256-GCM encryption
Return ciphertext — Returns encrypted data with 16-byte authentication tag appended
Gas Cost
Gas cost is calculated as:
For example:
16 bytes
1030
64 bytes
1120
256 bytes
1480
Notes
Uses AES-256-GCM authenticated encryption
Nonce must be unique for each encryption with the same key
Ciphertext length = plaintext length + 16 bytes (authentication tag)
The authentication tag ensures ciphertext integrity and authenticity
Reusing a nonce with the same key breaks security
Warnings
Nonce reuse — NEVER reuse the same nonce with the same key. This breaks confidentiality and can leak the plaintext.
Key security — Keep AES keys secure and never expose them in logs or error messages
Authentication tag — The 16-byte tag is appended to the ciphertext and must be included when decrypting
Counter management — When using integer nonces, ensure they are sequential and never repeated
See Also
Precompiles Overview — All precompile reference
aes-gcm-decrypt — Decrypt AES-GCM ciphertext
ecdh — Derive shared encryption keys
Encryption — How the provider uses AES-GCM internally
Last updated

