Back to Fhevm

Debugging with `debug.decrypt[XX]`

docs/solidity-guides/debug_decrypt.md

0.12.33.7 KB
Original Source

Debugging with debug.decrypt[XX]

This guide explains how to use the debug.decrypt[XX] functions for debugging encrypted data in mocked environments during development with FHEVM.

{% hint style="warning" %} The debug.decrypt[XX] functions should not be used in production as they rely on private keys. {% endhint %}

Overview

The debug.decrypt[XX] functions allow you to decrypt encrypted handles into plaintext values. This feature is useful for debugging encrypted operations such as transfers, balance checks, and other computations involving FHE-encrypted data.

Key points

  • Environment: The debug.decrypt[XX] functions work only in mocked environments (e.g., hardhat network).
  • Production limitation: In production, decryption is performed asynchronously via the relayer and requires an authorized onchain request.
  • Encrypted types: The debug.decrypt[XX] functions supports various encrypted types, including integers, and booleans.
  • Bypass ACL authorization: The debug.decrypt[XX] functions allow decryption without ACL authorization, useful for verifying encrypted operations during development and testing.

Supported functions

Integer decryption

Decrypts encrypted integers of different bit-widths (euint8, euint16, ..., euint256).

Function NameReturnsEncrypted Type
decrypt8biginteuint8
decrypt16biginteuint16
decrypt32biginteuint32
decrypt64biginteuint64
decrypt128biginteuint128
decrypt256biginteuint256

Boolean decryption

Decrypts encrypted booleans (ebool).

Function NameReturnsEncrypted Type
decryptBoolbooleanebool

Address decryption

Decrypts encrypted addresses.

Function NameReturnsEncrypted Type
decryptAddressstringeaddress

Function usage

Example: decrypting encrypted values

typescript
import { debug } from "../utils";

// Decrypt a 64-bit encrypted integer
const handle64: bigint = await this.erc20.balanceOf(this.signers.alice);
const plaintextValue: bigint = await debug.decrypt64(handle64);
console.log("Decrypted Balance:", plaintextValue);

{% hint style="info" %} To utilize the debug functions, import the utils.ts file. {% endhint %}

For a more complete example, refer to the ConfidentialERC20 test file.

How it works

Verifying types

Each decryption function includes a type verification step to ensure the provided handle matches the expected encrypted type. If the type is mismatched, an error is thrown.

typescript
function verifyType(handle: bigint, expectedType: number) {
  const typeCt = handle >> 8n;
  if (Number(typeCt % 256n) !== expectedType) {
    throw "Wrong encrypted type for the handle";
  }
}

Environment checks

{% hint style="danger" %} The functions only work in the hardhat network. Attempting to use them in a production environment will result in an error. {% endhint %}

typescript
if (network.name !== "hardhat") {
  throw Error("This function can only be called in mocked mode");
}

Best practices

  • Use only for debugging: These functions require access to private keys and are meant exclusively for local testing and debugging.
  • Production decryption: For production, always use the asynchronous relayer-based decryption.