Overview
The FHE library provides a comprehensive set of functions for working with Fully Homomorphic Encryption (FHE) in Solidity smart contracts. This library enables you to perform computations on encrypted data without decrypting it, ensuring privacy throughout your contract’s execution.All functions in the FHE library are prefixed with
FHE. when called. For example: FHE.add(a, b) or FHE.allowPublic(value).Encrypted Data Types
The library supports the following encrypted data types:Type Conversion
From Plaintext to Encrypted Types
asEbool
bool
required
Plaintext boolean value to encrypt
int32
Optional security zone identifier for isolating encrypted computations
ebool
Encrypted boolean value
asEuint8
uint256
required
Plaintext value to encrypt (must fit in 8 bits)
int32
Optional security zone identifier
euint8
Encrypted 8-bit unsigned integer
asEuint16
uint256
required
Plaintext value to encrypt (must fit in 16 bits)
int32
Optional security zone identifier
euint16
Encrypted 16-bit unsigned integer
asEuint32
uint256
required
Plaintext value to encrypt (must fit in 32 bits)
int32
Optional security zone identifier
euint32
Encrypted 32-bit unsigned integer
asEuint64
uint256
required
Plaintext value to encrypt (must fit in 64 bits)
int32
Optional security zone identifier
euint64
Encrypted 64-bit unsigned integer
asEuint128
uint256
required
Plaintext value to encrypt (must fit in 128 bits)
int32
Optional security zone identifier
euint128
Encrypted 128-bit unsigned integer
asEaddress
address
required
Plaintext Ethereum address to encrypt
int32
Optional security zone identifier
eaddress
Encrypted Ethereum address
From Encrypted Input Structures
asEbool (from InEbool)
InEbool
required
Encrypted input structure containing boolean data
ebool
Encrypted boolean value
asEuint8 (from InEuint8)
InEuint8
required
Encrypted input structure containing 8-bit integer data
euint8
Encrypted 8-bit unsigned integer
asEuint16 (from InEuint16)
InEuint16
required
Encrypted input structure containing 16-bit integer data
euint16
Encrypted 16-bit unsigned integer
asEuint32 (from InEuint32)
InEuint32
required
Encrypted input structure containing 32-bit integer data
euint32
Encrypted 32-bit unsigned integer
asEuint64 (from InEuint64)
InEuint64
required
Encrypted input structure containing 64-bit integer data
euint64
Encrypted 64-bit unsigned integer
asEuint128 (from InEuint128)
InEuint128
required
Encrypted input structure containing 128-bit integer data
euint128
Encrypted 128-bit unsigned integer
asEaddress (from InEaddress)
InEaddress
required
Encrypted input structure containing address data
eaddress
Encrypted Ethereum address
Type Conversion Between Encrypted Types
asEbool (from euint)
Converts various encrypted integer types to an encrypted boolean.asEuint8 (from other encrypted types)
Converts various encrypted types to an encrypted 8-bit unsigned integer.asEuint16 (from other encrypted types)
Converts various encrypted types to an encrypted 16-bit unsigned integer.asEuint32 (from other encrypted types)
Converts various encrypted types to an encrypted 32-bit unsigned integer.asEuint64 (from other encrypted types)
Converts various encrypted types to an encrypted 64-bit unsigned integer.asEuint128 (from other encrypted types)
Converts various encrypted types to an encrypted 128-bit unsigned integer.asEaddress (from euint128)
Converts an encrypted 128-bit unsigned integer to an encrypted address.Arithmetic Operations
add
Performs addition of two encrypted unsigned integers.euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
euint8 | euint16 | euint32 | euint64 | euint128
Sum of the two encrypted values
sub
Performs subtraction of two encrypted unsigned integers.euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
euint8 | euint16 | euint32 | euint64 | euint128
Difference of the two encrypted values
mul
Performs multiplication of two encrypted unsigned integers.euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
euint8 | euint16 | euint32 | euint64 | euint128
Product of the two encrypted values
div
Performs division of two encrypted unsigned integers.euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value (dividend)
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (divisor, must match lhs type)
euint8 | euint16 | euint32 | euint64 | euint128
Quotient of the division
rem
Calculates the remainder when dividing two encrypted unsigned integers.euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value (dividend)
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (divisor, must match lhs type)
euint8 | euint16 | euint32 | euint64 | euint128
Remainder of the division
square
Calculates the square of an encrypted unsigned integer.euint8 | euint16 | euint32 | euint64 | euint128
required
Encrypted value to square
euint8 | euint16 | euint32 | euint64 | euint128
Square of the encrypted value
Bitwise Operations
and
Performs a bitwise AND operation on two encrypted values.ebool | euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
ebool | euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
ebool | euint8 | euint16 | euint32 | euint64 | euint128
Result of the bitwise AND operation
or
Performs a bitwise OR operation on two encrypted values.ebool | euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
ebool | euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
ebool | euint8 | euint16 | euint32 | euint64 | euint128
Result of the bitwise OR operation
xor
Performs a bitwise XOR operation on two encrypted values.ebool | euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
ebool | euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
ebool | euint8 | euint16 | euint32 | euint64 | euint128
Result of the bitwise XOR operation
not
Performs a bitwise NOT operation on an encrypted value.ebool | euint8 | euint16 | euint32 | euint64 | euint128
required
Encrypted value to negate
ebool | euint8 | euint16 | euint32 | euint64 | euint128
Result of the bitwise NOT operation
shl
Performs a shift left operation on an encrypted unsigned integer.euint8 | euint16 | euint32 | euint64 | euint128
required
Encrypted value to shift
euint8 | euint16 | euint32 | euint64 | euint128
required
Number of bits to shift left (must match lhs type)
euint8 | euint16 | euint32 | euint64 | euint128
Result of the shift left operation
shr
Performs a shift right operation on an encrypted unsigned integer.euint8 | euint16 | euint32 | euint64 | euint128
required
Encrypted value to shift
euint8 | euint16 | euint32 | euint64 | euint128
required
Number of bits to shift right (must match lhs type)
euint8 | euint16 | euint32 | euint64 | euint128
Result of the shift right operation
rol
Performs a rotate left operation on an encrypted unsigned integer.euint8 | euint16 | euint32 | euint64 | euint128
required
Encrypted value to rotate
euint8 | euint16 | euint32 | euint64 | euint128
required
Number of bits to rotate left (must match lhs type)
euint8 | euint16 | euint32 | euint64 | euint128
Result of the rotate left operation
ror
Performs a rotate right operation on an encrypted unsigned integer.euint8 | euint16 | euint32 | euint64 | euint128
required
Encrypted value to rotate
euint8 | euint16 | euint32 | euint64 | euint128
required
Number of bits to rotate right (must match lhs type)
euint8 | euint16 | euint32 | euint64 | euint128
Result of the rotate right operation
Comparison Operations
eq
Checks if two encrypted values are equal and returns an encrypted boolean.ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Left-hand side encrypted value
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Right-hand side encrypted value (must match lhs type)
ebool
Encrypted boolean indicating equality
ne
Checks if two encrypted values are not equal and returns an encrypted boolean.ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Left-hand side encrypted value
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Right-hand side encrypted value (must match lhs type)
ebool
Encrypted boolean indicating inequality
lt
Checks if the first encrypted unsigned integer is less than the second and returns an encrypted boolean.euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
ebool
Encrypted boolean indicating if lhs is less than rhs
lte
Checks if the first encrypted unsigned integer is less than or equal to the second and returns an encrypted boolean.euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
ebool
Encrypted boolean indicating if lhs is less than or equal to rhs
gt
Checks if the first encrypted unsigned integer is greater than the second and returns an encrypted boolean.euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
ebool
Encrypted boolean indicating if lhs is greater than rhs
gte
Checks if the first encrypted unsigned integer is greater than or equal to the second and returns an encrypted boolean.euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
ebool
Encrypted boolean indicating if lhs is greater than or equal to rhs
Min/Max Functions
min
Returns the smaller of two encrypted unsigned integers.euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
euint8 | euint16 | euint32 | euint64 | euint128
The smaller of the two encrypted values
max
Returns the larger of two encrypted unsigned integers.euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
euint8 | euint16 | euint32 | euint64 | euint128
The larger of the two encrypted values
Control Flow
select
Conditionally selects between two encrypted values based on an encrypted boolean condition.ebool
required
Encrypted boolean condition
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Value to return if condition is true
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Value to return if condition is false (must match ifTrue type)
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
Selected value based on condition
Decryption Results
getDecryptResult
Retrieves a published decryption result. This function should be called after a result has been published viapublishDecryptResult.
uint256
required
Hash of the encrypted value that was previously decrypted
uint256
Decrypted result value
getDecryptResultSafe
Safely retrieves the decrypted result of a previously decrypted value. UnlikegetDecryptResult, this function returns a boolean flag indicating whether the decryption is complete.
uint256
required
Hash of the encrypted value that was previously decrypted
uint256
Decrypted result value (valid only if decrypted is true)
bool
Boolean indicating whether the decryption has completed successfully
Decrypt Result Publishing & Verification
publishDecryptResult
Publishes a signed decrypt result from the Threshold Network to the chain. The TaskManager verifies the ECDSA signature before storing the result. Anyone holding a valid signature can call this.ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress | uint256
required
The ciphertext hash to publish a result for
bool | uint8 | uint16 | uint32 | uint64 | uint128 | address | uint256
required
The decrypted plaintext value (type matches the ctHash type)
bytes
required
The ECDSA signature from the Threshold Network’s Dispatcher
verifyDecryptResult
Verifies a decrypt result signature without publishing. Reverts if the signature is invalid.ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress | uint256
required
The ciphertext hash
bool | uint8 | uint16 | uint32 | uint64 | uint128 | address | uint256
required
The decrypted plaintext value
bytes
required
The ECDSA signature to verify
bool
Returns
true if the signature is valid. Reverts otherwise.publishDecryptResultBatch
Publishes multiple signed decrypt results in a single transaction for gas efficiency.uint256[]
required
Array of ciphertext hashes
uint256[]
required
Array of decrypted plaintext values
bytes[]
required
Array of ECDSA signatures (one per result)
verifyDecryptResultSafe
Same asverifyDecryptResult, but returns false instead of reverting on invalid signatures.
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress | uint256
required
The ciphertext hash
bool | uint8 | uint16 | uint32 | uint64 | uint128 | address | uint256
required
The decrypted plaintext value
bytes
required
The ECDSA signature to verify
bool
Returns
true if valid, false if invalid (never reverts on bad signature)verifyDecryptResultSafe
LikeverifyDecryptResult, but returns false instead of reverting if the signature is invalid.
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Ciphertext handle of the encrypted value
bool | uint8 | uint16 | uint32 | uint64 | uint128 | address
required
The decrypted plaintext value
bytes
required
The Threshold Network signature
bool
True if the signature is valid, false otherwise (does not revert)
publishDecryptResultBatch
Publishes multiple decrypted results on-chain in a single call. Each element is verified independently.ebool[] | euint8[] | euint16[] | euint32[] | euint64[] | euint128[] | eaddress[]
required
Array of ciphertext handles
bool[] | uint8[] | uint16[] | uint32[] | uint64[] | uint128[] | address[]
required
Array of decrypted plaintext values (must match ctHashes length and type)
bytes[]
required
Array of Threshold Network signatures (must match ctHashes length)
verifyDecryptResultBatch
Verifies multiple Threshold Network signatures in a single call without storing results. Reverts if any signature is invalid.ebool[] | euint8[] | euint16[] | euint32[] | euint64[] | euint128[] | eaddress[]
required
Array of ciphertext handles
bool[] | uint8[] | uint16[] | uint32[] | uint64[] | uint128[] | address[]
required
Array of decrypted plaintext values
bytes[]
required
Array of Threshold Network signatures
bool
True if all signatures are valid (reverts otherwise)
verifyDecryptResultBatchSafe
LikeverifyDecryptResultBatch, but returns a bool[] indicating which entries are valid instead of reverting.
ebool[] | euint8[] | euint16[] | euint32[] | euint64[] | euint128[] | eaddress[]
required
Array of ciphertext handles
bool[] | uint8[] | uint16[] | uint32[] | uint64[] | uint128[] | address[]
required
Array of decrypted plaintext values
bytes[]
required
Array of Threshold Network signatures
bool[]
Array of booleans —
true for each valid signature, false for invalidAccess Control
allow
Grants permission to the specified account to access the encrypted value.ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to grant access to
address
required
Address of the account to grant access to
allowThis
Grants permission to the current contract to access the encrypted value.ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to grant access to
allowPublic
Grants public permission to access the encrypted value. Once called, anyone can request decryption of this value off-chain viadecryptForTx without needing a permit.
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to grant public access to
allowSender
Grants permission to the message sender to access the encrypted value.ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to grant access to
allowTransient
Grants temporary permission to the specified account to access the encrypted value.ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to grant access to
address
required
Address of the account to grant temporary access to
isAllowed
Checks if the specified account has permission to access the encrypted value.ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to check access for
address
required
Address of the account to check
bool
True if the account has permission, false otherwise
isPubliclyAllowed
Checks if the encrypted value has been granted public access viaallowPublic.
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to check
bool
True if the value is publicly accessible, false otherwise
Utility Functions
isInitialized
Checks whether an encrypted value has been initialized (i.e., is not a zero/empty handle).ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to check
bool
True if the value has been initialized, false otherwise
unwrap
Extracts the rawbytes32 handle from a typed encrypted value.
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to unwrap
bytes32
The underlying ciphertext handle
wrapEbool / wrapEuint8 / wrapEuint16 / wrapEuint32 / wrapEuint64 / wrapEuint128 / wrapEaddress
Wraps a rawbytes32 handle into a typed encrypted value. Use this when you have a raw handle (e.g., from storage or an event) and need to convert it to a typed encrypted value.
bytes32
required
Raw ciphertext handle to wrap
Bindings
The FHE library provides binding libraries that enable syntactic sugar for working with encrypted types. These bindings allow for more intuitive and object-oriented usage patterns.Using Bindings
With bindings, you can use encrypted types with dot notation instead of callingFHE.* functions:
Example with Bindings
Security Considerations
- Initialization: All FHE functions check if their inputs are initialized and set them to 0 if not.
-
Decryption: Decryption is a two-phase process — mark values with
allowPublicon-chain, decrypt off-chain via the Client SDK, then publish/verify the result withpublishDecryptResultorverifyDecryptResult. Only reveal values when absolutely necessary. -
Security Zones: Some functions accept a
securityZoneparameter to isolate different encrypted computations. FHE operations can only be performed between ciphertexts that share the same security zone. -
Access Control: The library provides fine-grained access control through the
allow*functions. Always set proper permissions before accessing encrypted values. - Type Safety: Ensure encrypted values use compatible types when performing operations. Type mismatches will cause errors.
Example Usage
Basic Example
This example shows how to perform private computations while keeping all intermediate values encrypted:Example with Bindings
Next Steps
- Learn about encrypted data types and their use cases
- Explore encrypted operations in detail
- Review access control mechanisms for managing permissions
- Check out best practices for secure FHE development