This document explains how to enable encrypted computations in your smart contract by setting up the fhevm environment. Learn how to integrate essential libraries, configure encryption, and add secure computation logic to your contracts.
To utilize encrypted computations in Solidity contracts, you must configure the FHE library and Relayer addresses. The fhevm package simplifies this process with prebuilt configuration contracts, allowing you to focus on developing your contract’s logic without handling the underlying cryptographic setup.
- FHE library: Sets up encryption parameters and cryptographic keys.
- Relayer: Manages secure cryptographic operations, including user decryption and public decryption.
- Network-specific settings: Adapts to local testing, testnets (Sepolia for example), or mainnet deployment.
By inheriting these configuration contracts, you ensure seamless initialization and functionality across environments.
This configuration contract initializes the fhevm environment with required encryption parameters.
Import based on your environment:
// For Ethereum Sepolia
import { SepoliaConfig } from "@fhevm/solidity/config/ZamaConfig.sol";Purpose:
- Sets encryption parameters such as cryptographic keys and supported ciphertext types.
- Ensures proper initialization of the FHEVM environment.
Example: using Sepolia configuration
// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.24;
import { SepoliaConfig } from "@fhevm/solidity/config/ZamaConfig.sol";
contract MyERC20 is SepoliaConfig {
constructor() {
// Additional initialization logic if needed
}
}To perform public decryption or user decryption, your contract must interact with the Relayer, which acts as a secure bridge between the blockchain, coprocessor, and Key Management System (KMS).
Import based on your environment
// For Sepolia
import { SepoliaConfig } from "@fhevm/solidity/config/ZamaConfig.sol";Purpose
- Configures the relayer for secure cryptographic operations.
- Facilitates public and user decryption requests.
Example: Configuring the relayer with Sepolia settings
import "@fhevm/solidity/lib/FHE.sol";
import { SepoliaConfig } from "@fhevm/solidity/config/ZamaConfig.sol";
contract Test is SepoliaConfig {
constructor() {
// Relayer and FHEVM environment initialized automatically
}
}The isInitialized utility function checks whether an encrypted variable has been properly initialized, preventing unexpected behavior due to uninitialized values.
Function signature
function isInitialized(T v) internal pure returns (bool)Purpose
- Ensures encrypted variables are initialized before use.
- Prevents potential logic errors in contract execution.
Example: Initialization Check for Encrypted Counter
require(FHE.isInitialized(counter), "Counter not initialized!");By leveraging prebuilt a configuration contract like ZamaConfig.sol, you can efficiently set up your smart contract for encrypted computations. These tools abstract the complexity of cryptographic initialization, allowing you to focus on building secure, confidential smart contracts.