unlockaes_gcm_decrypt

On-chain AES-256-GCM decryption

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

Overview

aes_gcm_decrypt() and async_aes_gcm_decrypt() call the AES-GCM decryption precompile at address 0x67 to decrypt ciphertext using AES-256-GCM. The ciphertext must include the 16-byte authentication tag.

Signature

def aes_gcm_decrypt(
    w3: Web3,
    *,
    aes_key: Bytes32,
    nonce: int | EncryptionNonce,
    ciphertext: bytes,
) -> HexBytes

async def async_aes_gcm_decrypt(
    w3: AsyncWeb3,
    *,
    aes_key: Bytes32,
    nonce: int | EncryptionNonce,
    ciphertext: bytes,
) -> HexBytes

Parameters

Parameter
Type
Required
Description

w3

Web3 or AsyncWeb3

Yes

Web3 instance connected to a Seismic node

aes_key

Yes

32-byte AES-256 decryption key

nonce

Yes

12-byte nonce (must match encryption nonce)

ciphertext

bytes

Yes

Data to decrypt (includes 16-byte authentication tag)

Returns

Type
Description

HexBytes

Decrypted plaintext bytes

Examples

Basic Usage

With Integer Nonce

With EncryptionNonce

Async Usage

Decrypt Multiple Messages

Handle Decryption Failure

With ECDH-Derived Key

Convert Result to String

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 authentication tag

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

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 if the authentication tag doesn't verify

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

Warnings

  • Authentication failure - If the tag doesn't verify, the precompile reverts (wrong key/nonce/tampered data)

  • Nonce mismatch - Using a different nonce than encryption will cause decryption to fail

  • Key mismatch - Using a different key than encryption will cause authentication failure

  • Ciphertext integrity - Any modification to ciphertext causes authentication failure

See Also

Last updated