Back to Fhevm

README

docs/solidity-guides/foundry/README.md

0.14.0-02.2 KB
Original Source

This section will guide you through writing and testing FHEVM smart contracts in Solidity using Foundry.

The forge-fhevm testing library

To write FHEVM smart contracts in Foundry, the recommended approach is to use forge-fhevm — a Foundry-native testing library for FHEVM confidential smart contracts.

forge-fhevm deploys the FHEVM host contracts (FHEVMExecutor, ACL, InputVerifier, KMSVerifier) into Foundry's test EVM, so your contract runs the same on-chain code paths as production — input verification, ACL enforcement, and handle lifecycle all execute exactly as they would on mainnet. The FHE coprocessor computation itself is simulated, with plaintext values tracked locally so you can assertEq on decrypted results directly.

It gives you, out of the box:

  • Encryption helpers for every FHE type (encryptBool, encryptUint8encryptUint256, encryptAddress)
  • Three decryption modes: low-level decrypt(), publicDecrypt(), and userDecrypt()
  • EIP-712 proof helpers (signUserDecrypt, buildDecryptionProof)
  • A ready-to-use FhevmTest base contract with all infrastructure deployed in setUp()

{% hint style="info" %} The only deviation from mainnet is the use of mock private keys for the input signer and KMS signer, enabling deterministic EIP-712 proof generation in tests. {% endhint %}

The FHEVM Foundry template

The fastest way to start is the FHEVM Foundry Template — a ready-to-clone Foundry project preconfigured with forge-fhevm, an example FHECounter contract, deployment scripts for local Anvil and Sepolia, and tests demonstrating the full encrypt → execute → decrypt flow.

Where to go next

🟨 Go to Setup Foundry to create a Foundry project from the template.

🟨 Go to Write FHEVM tests in Foundry for details on writing tests with forge-fhevm.

🟨 Go to Deploy FHEVM contracts with Foundry to learn how to deploy to a local Anvil node or to Sepolia.

🟨 Go to forge-fhevm API reference for the full list of FhevmTest helpers.