Introduction
The FHE library (@fhevm/solidity/lib/FHE.sol) provides Fully Homomorphic Encryption (FHE) capabilities for Solidity smart contracts. It enables computation on encrypted data without revealing the underlying values, allowing developers to build privacy-preserving applications on Ethereum.
Quick Start
Core Concepts
Encrypted Types
The FHE library provides encrypted equivalents for standard Solidity types:- Unsigned Integers:
euint8,euint16,euint32,euint64,euint128,euint256 - Boolean:
ebool - Address:
eaddress
Operations
All standard operations are available on encrypted types:- Arithmetic:
add,sub,mul,div,rem,min,max,neg - Comparison:
eq,ne,gt,ge,lt,le - Bitwise:
and,or,xor,not,shl,shr,rotl,rotr - Conditional:
select(encrypted ternary operator)
Access Control
Every encrypted value has an Access Control List (ACL). You must grant permissions for any address to use an encrypted value:Input Conversion
Convert external encrypted inputs with zero-knowledge proofs:Library Structure
Namespace: FHE
All operations are accessed through the FHE library:
Configuration
All contracts using FHE must inherit fromZamaEthereumConfig:
Key Design Patterns
1. Conditional Logic with select
Never use if statements on encrypted values. Use FHE.select() instead:
2. Always Set ACL Permissions
After every operation that creates a new ciphertext:3. Silent Failure for Privacy
To avoid information leakage, useselect instead of require:
4. Use Smallest Type Possible
Gas costs scale with bit width. Use the smallest type that fits your data:Complete Example: Confidential Token Transfer
API Reference
Encrypted Types
euint8-256, ebool, eaddress
Arithmetic Operations
add, sub, mul, div, rem, min, max, neg
Bitwise Operations
and, or, xor, not, shl, shr, rotl, rotr
Comparison
eq, ne, gt, ge, lt, le
Conditional Logic
select (encrypted ternary)
Access Control
allow, allowThis, allowTransient
Input Conversion
fromExternal with ZK proofs
Decryption
User decryption and public reveal
Randomness
randEuint8-256, randEbool
Type Casting
asEuint conversions
Gas Costs
Operation costs and optimization
Best Practices
Security
- Never decrypt on-chain unless necessary - Use client-side re-encryption for user-specific data
- Avoid information leakage - Use silent failures instead of
requirestatements - Always validate ACL permissions - Check
FHE.isSenderAllowed()before operations - Use transient permissions carefully -
allowTransientonly works within the same transaction
Gas Optimization
- Use smallest types -
euint8is 3-4x cheaper thaneuint256 - Minimize FHE operations - Each operation costs 50-500k gas
- Cache intermediate results - Avoid recomputing the same encrypted value
- Use plaintext operands when possible - Mixed operations are 30-40% cheaper
Important Notes
Overflow wraps silently in FHE operations. There is no revert on overflow, just like unchecked arithmetic.
Support
For issues, questions, or feature requests:- GitHub: zama-ai/fhevm
- Discord: Zama Community
- Documentation: docs.zama.ai