unlockaes_gcm_decrypt

On-chain AES-256-GCM decryption

Decrypt data on-chain using Mercury EVM's AES-GCM decryption precompile.

Overview

The AES-GCM decryption precompile at address 0x67 performs AES-256-GCM authenticated decryption. The ciphertext must include the 16-byte authentication tag appended during encryption. If the tag does not verify, the precompile reverts.

Precompile Address

0x0000000000000000000000000000000000000067

Input Encoding

Field
Size
Description

key

32 bytes

AES-256 decryption key (must match encryption key)

nonce

12 bytes

Nonce (must match the nonce used during encryption)

ciphertext

Variable

Encrypted data including the 16-byte authentication tag

The input is the concatenation of key (32 bytes) + nonce (12 bytes) + ciphertext (variable length, includes 16-byte tag).

Output Format

Field
Size
Description

plaintext

len(ciphertext) - 16 bytes

Decrypted data

Parameters

Parameter
Type
Required
Description

key

[u8; 32]

Yes

32-byte AES-256 decryption key

nonce

[u8; 12]

Yes

12-byte nonce (must match encryption nonce)

ciphertext

&[u8]

Yes

Ciphertext including 16-byte authentication tag

Examples

Basic Usage

Decrypt Multiple Messages

Handle Decryption Failure

With ECDH-Derived Key (Bob's Side)

How It Works

  1. Encode parameters -- Concatenates 32-byte key + 12-byte nonce + ciphertext (with tag)

  2. Call precompile -- Issues an eth_call to address 0x67 with estimated gas

  3. Decrypt and verify -- Precompile performs AES-256-GCM decryption and verifies the authentication tag

  4. Return plaintext -- Returns decrypted data if tag verification succeeds; reverts otherwise

Gas Cost

Gas cost is calculated as:

The gas cost is proportional to ciphertext length (including the 16-byte tag).

Notes

  • Uses AES-256-GCM authenticated decryption

  • Nonce must exactly match the nonce used during encryption

  • Ciphertext must include the 16-byte authentication tag (appended by encryption)

  • Decryption fails (reverts) if the authentication tag does not verify

  • Plaintext length = ciphertext length - 16 bytes (authentication tag)

Warnings

  • Authentication failure -- If the tag does not verify, the precompile reverts. This can happen with a wrong key, wrong nonce, or tampered ciphertext.

  • Nonce mismatch -- Using a different nonce than the one used for encryption will cause decryption to fail

  • Key mismatch -- Using a different key than the one used for encryption will cause authentication failure

  • Ciphertext integrity -- Any modification to the ciphertext (including the tag) causes authentication failure

See Also

Last updated