docs/protocol/architecture/library.md
This document offers a high-level overview of the FHEVM library, helping you understand how it fits into the broader Zama Protocol. To learn how to use it in practice, see the Solidity Guides.
The FHEVM library enables developers to build smart contracts that operate on encrypted data—without requiring any knowledge of cryptography.
It extends the standard Solidity development flow with:
This library serves as an abstraction layer over Fully Homomorphic Encryption (FHE) and interacts seamlessly with off-chain components such as the Coprocessors and the Gateway.
The library introduces encrypted variants of common Solidity types, implemented as user-defined value types. Internally, these are represented as bytes32 handles that point to encrypted values stored off-chain.
| Category | Types |
|---|---|
| Booleans | ebool |
| Unsigned integers | euint8, euint16, ..., euint256 |
| Signed integers | eint8, eint16, ..., eint256 |
| Addresses | eaddress |
→ See the full guide of Encrypted data types.
Each encrypted type supports operations similar to its plaintext counterpart:
add, sub, mul, div, rem, negand, or, xor, notlt, gt, le, ge, eq, ne, min, maxshl, shr, rotl, rotrThese operations are symbolically executed on-chain by generating new handles and emitting events for coprocessors to process the actual FHE computation off-chain.
Example:
function compute(euint64 x, euint64 y, euint64 z) public returns (euint64) {
euint64 result = FHE.mul(FHE.add(x, y), z);
return result;
}
→ See the full guide of Operations on encrypted types.
Direct if or require statements are not compatible with encrypted booleans. Instead, the library provides a selectoperator to emulate conditional logic without revealing which branch was taken:
ebool condition = FHE.lte(x, y);
euint64 result = FHE.select(condition, valueIfTrue, valueIfFalse);
This preserves confidentiality even in conditional logic.
→ See the full guide of Branching.
When users want to pass encrypted inputs (e.g., values they’ve encrypted off-chain or bridged from another chain), they provide:
The function fromExternal is used to validate the attestation and extract a usable encrypted handle:
function handleInput(externalEuint64 param1, externalEbool param2, bytes calldata attestation) public {
euint64 val = FHE.fromExternal(param1, attestation);
ebool flag = FHE.fromExternal(param2, attestation);
}
This ensures that only authorized, well-formed ciphertexts are accepted by smart contracts.
→ See the full guide of Encrypted input.
The FHE library also exposes methods for managing access to encrypted values using the ACL maintained by host contracts:
allow(handle, address): Grant persistent accessallowTransient(handle, address): Grant access for the current transaction onlyallowForDecryption(handle): Make handle publicly decryptableisAllowed(handle, address): Check if address has accessisSenderAllowed(handle): Shortcut for checking msg.sender permissionsThese allow methods emit events consumed by the coprocessors to replicate the ACL state in the Gateway.
→ See the full guide of ACL.
The library allows generation of pseudo-random encrypted integers, useful for games, lotteries, or randomized logic:
randEuintXX()randEuintXXBounded(uint bound)These are deterministic across coprocessors and indistinguishable to external observers.
→ See the full guide of Generate random number.