Overview
This guide helps you set up your local development environment for building FHE (Fully Homomorphic Encryption) smart contracts with CoFHE. The Fhenix development starter kit provides everything you need to develop, test, and deploy FHE contracts both locally and on test networks.Supported Networks
Fhenix CoFHE currently supports the following testnet networks:- Ethereum Sepolia - Main Ethereum testnet for FHE development
- Arbitrum Sepolia - Layer 2 testnet with lower gas costs
- Base Sepolia - Coinbase’s Layer 2 testnet
You can develop and test locally using mock contracts, then deploy to any of these supported testnets when ready. Production mainnet support is coming soon.
- cofhe-hardhat-starter: Template Hardhat project to clone and use as a starting point
- cofhe-hardhat-plugin: Hardhat plugin that deploys mock contracts and exposes utilities
- cofhejs: JavaScript library for interacting with FHE contracts and the CoFHE coprocessor
- cofhe-mock-contracts: Mock contracts that mimic CoFHE coprocessor behavior for local testing
- cofhe-contracts: Solidity libraries and smart contracts for FHE operations
Prerequisites
Before starting, ensure you have:- Node.js (v20 or later)
- pnpm (recommended package manager)
- Basic familiarity with Hardhat and Solidity
If you don’t have pnpm installed, you can install it globally with
npm install -g pnpm.Installation
1
Clone the starter repository
Clone the
cofhe-hardhat-starter repository:You should now have the starter project in your local directory.
2
Install dependencies
Install all required dependencies using pnpm:
All dependencies are installed and ready to use.
Project Structure
The starter kit provides a well-organized directory structure to help you get started quickly:contracts/: Contains all your Solidity smart contract source filesCounter.sol: An example FHE-enabled counter contract demonstrating basic FHE operations
test/: Houses tests that utilize cofhejs and utilities to interact with FHE-enabled contractstasks/: Tasks to deploy and interact with the Counter contract on Arbitrum Sepoliahardhat.config.ts: Imports the CoFHE Hardhat plugin to deploy mock contracts
Local Development Workflow
Writing FHE Smart Contracts
FHE contracts use special encrypted types and operations from the FHE library. Here’s a basic example:contracts/Counter.sol
Testing with Mock Environment
For rapid development, use the mock environment to test your contracts:Deploying to Testnet
When ready for more realistic testing, deploy to a Sepolia testnet:1
Configure environment variables
Create a
.env file with your private key and RPC URLs:2
Deploy your contract
Deploy your contract to the testnet:
3
Interact with your contract
Use tasks to interact with your deployed contract:
Key Components
cofhe-hardhat-plugin
This plugin provides essential tools for developing FHE contracts:- Network Configuration: Automatically configures supported networks
- Testing Utilities: Helpers for testing FHE contracts
- Mock Integration: Sets up mock contracts for local testing
Cofhejs
The JavaScript library for working with FHE contracts:- Encrypt/Decrypt: Encrypt data to send to contracts and decrypt results
- Querying: Fetch encrypted values from FHE contracts
- Authentication: Uses Permits to authenticate connected users when requesting confidential data
cofhe-mock-contracts
These contracts provide mock implementations for FHE functionality:- Allows testing without actual FHE operations
- Simulates the behavior of the real FHE environment
- Stores plaintext values on-chain for testing purposes
In the mock environment, gas costs are higher than in production due to the additional operations needed to simulate FHE behavior. This is especially noticeable when logging is enabled.
cofhe-contracts
Package of Solidity libraries and smart contracts for FHE operations:- FHE.sol Library: The only import you need to start using FHE functionality in your contracts
- Complete API reference for all available functions and types
Development Environments
CoFHE supports multiple development environments:MOCK Environment
- Fastest development cycle
- No external dependencies
- Uses mock contracts to simulate FHE operations
Sepolia Testnet
- Public testnet for real FHE operations
- Requires ETH from the Sepolia faucet
- Available on Ethereum Sepolia and Arbitrum Sepolia
Environment detection happens automatically in most cases. When testing on the Hardhat network with mocks deployed (e.g., in unit tests),
cofhejs_initializeWithHardhatSigner will detect the Hardhat network and set environment: "MOCK" in the underlying cofhejs.initialize(...) call. When using tasks that connect to networks like arb-sepolia, the environment will automatically be set to environment: "TESTNET". This environment setting is crucial as it determines how cofhejs handles encryption and unsealing operations.Creating Custom Tasks
You can create custom Hardhat tasks for your contracts in thetasks/ directory:
tasks/increment-counter.ts
Development Guidelines
Start with Mock Environment
Begin development using mock contracts for faster iteration and debugging. This approach eliminates external dependencies and provides immediate feedback.Test Thoroughly
Write comprehensive tests covering both mock and testnet environments. Ensure your application behaves consistently across environments and handles edge cases properly.Permission Management
Always set proper permissions withFHE.allowThis() and FHE.allowSender() to control which contracts and addresses can access encrypted data. Proper permission management is crucial for maintaining privacy and security in FHE applications.
Error Handling
Implement robust error handling for FHE operations. Be prepared for potential decryption delays and use appropriate retry mechanisms.Gas Optimization
Be aware that FHE operations cost more gas than standard operations. Mock environments will simulate higher gas consumption than actual production environments. For accurate gas estimation, always test on the testnet before deployment.Next Steps
Now that you have your development environment set up, you can:- Explore the FHE Library documentation to learn about encrypted data types and operations
- Review the Counter contract example to see FHE in action
- Check out Cofhejs documentation for client-side integration
- Learn about access control mechanisms for managing encrypted data permissions