Write FHEVM tests in Hardhat

In this section, you'll find everything you need to set up a new Hardhatarrow-up-right project and start developing FHEVM smart contracts from scratch using the FHEVM Hardhat Pluginarrow-up-right

Enabling the FHEVM Hardhat Plugin in your Hardhat project

Like any Hardhat plugin, the FHEVM Hardhat Pluginarrow-up-right must be enabled by adding the following import statement to your hardhat.config.ts file:

import "@fhevm/hardhat-plugin";
circle-exclamation

Accessing the Hardhat FHEVM API

The plugin extends the standard Hardhat Runtime Environmentarrow-up-right (or hre in short) with the new fhevm Hardhat module.

You can access it in either of the following ways:

import { fhevm } from "hardhat";

or

import * as hre from "hardhat";

// Then access: hre.fhevm

Encrypting Values Using the Hardhat FHEVM API

Suppose the FHEVM smart contract you want to test has a function called foo that takes an encrypted uint32 value as input. The Solidity function foo should be declared as follows:

function foo(externalEunit32 value, bytes calldata memory inputProof);

Where:

  • externalEunit32 value : is a bytes32 representing the encrypted uint32

  • bytes calldata memory inputProof : is a bytes array representing the zero-knowledge proof of knowledge that validates the encryption

To compute these arguments in TypeScript, you need:

  • The address of the target smart contract

  • The signer’s address (i.e., the account sending the transaction)

1

Create a new encryted input

2

Add the value you want to encrypt.

3

Perform local encryption.

4

Call the Solidity function

Encryption examples

Decrypting values using the Hardhat FHEVM API

Suppose user Alice wants to decrypt a euint32 value that is stored in a smart contract exposing the following Solidity view function:

circle-exclamation
1

Retrieve the encrypted value (a bytes32 handle) from the smart contract:

2

Perform the decryption using the FHEVM API:

circle-exclamation

Supported Decryption Types

Use the appropriate function for each encrypted data type:

Type
Function

euintXXX

fhevm.userDecryptEuint(...)

ebool

fhevm.userDecryptEbool(...)

eaddress

fhevm.userDecryptEaddress(...)

Decryption examples

Last updated