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 | Description |
|---|---|
ebool | Encrypted boolean value |
euint8 | Encrypted 8-bit unsigned integer |
euint16 | Encrypted 16-bit unsigned integer |
euint32 | Encrypted 32-bit unsigned integer |
euint64 | Encrypted 64-bit unsigned integer |
euint128 | Encrypted 128-bit unsigned integer |
eaddress | Encrypted Ethereum address |
Type Conversion
From Plaintext to Encrypted Types
asEbool
Plaintext boolean value to encrypt
Optional security zone identifier for isolating encrypted computations
Encrypted boolean value
asEuint8
Plaintext value to encrypt (must fit in 8 bits)
Optional security zone identifier
Encrypted 8-bit unsigned integer
asEuint16
Plaintext value to encrypt (must fit in 16 bits)
Optional security zone identifier
Encrypted 16-bit unsigned integer
asEuint32
Plaintext value to encrypt (must fit in 32 bits)
Optional security zone identifier
Encrypted 32-bit unsigned integer
asEuint64
Plaintext value to encrypt (must fit in 64 bits)
Optional security zone identifier
Encrypted 64-bit unsigned integer
asEuint128
Plaintext value to encrypt (must fit in 128 bits)
Optional security zone identifier
Encrypted 128-bit unsigned integer
asEaddress
Plaintext Ethereum address to encrypt
Optional security zone identifier
Encrypted Ethereum address
From Encrypted Input Structures
asEbool (from InEbool)
Encrypted input structure containing boolean data
Encrypted boolean value
asEuint8 (from InEuint8)
Encrypted input structure containing 8-bit integer data
Encrypted 8-bit unsigned integer
asEuint16 (from InEuint16)
Encrypted input structure containing 16-bit integer data
Encrypted 16-bit unsigned integer
asEuint32 (from InEuint32)
Encrypted input structure containing 32-bit integer data
Encrypted 32-bit unsigned integer
asEuint64 (from InEuint64)
Encrypted input structure containing 64-bit integer data
Encrypted 64-bit unsigned integer
asEuint128 (from InEuint128)
Encrypted input structure containing 128-bit integer data
Encrypted 128-bit unsigned integer
asEaddress (from InEaddress)
Encrypted input structure containing address data
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.Left-hand side encrypted value
Right-hand side encrypted value (must match lhs type)
Sum of the two encrypted values
sub
Performs subtraction of two encrypted unsigned integers.Left-hand side encrypted value
Right-hand side encrypted value (must match lhs type)
Difference of the two encrypted values
mul
Performs multiplication of two encrypted unsigned integers.Left-hand side encrypted value
Right-hand side encrypted value (must match lhs type)
Product of the two encrypted values
div
Performs division of two encrypted unsigned integers.Left-hand side encrypted value (dividend)
Right-hand side encrypted value (divisor, must match lhs type)
Quotient of the division
rem
Calculates the remainder when dividing two encrypted unsigned integers.Left-hand side encrypted value (dividend)
Right-hand side encrypted value (divisor, must match lhs type)
Remainder of the division
square
Calculates the square of an encrypted unsigned integer.Encrypted value to square
Square of the encrypted value
Bitwise Operations
and
Performs a bitwise AND operation on two encrypted values.Left-hand side encrypted value
Right-hand side encrypted value (must match lhs type)
Result of the bitwise AND operation
or
Performs a bitwise OR operation on two encrypted values.Left-hand side encrypted value
Right-hand side encrypted value (must match lhs type)
Result of the bitwise OR operation
xor
Performs a bitwise XOR operation on two encrypted values.Left-hand side encrypted value
Right-hand side encrypted value (must match lhs type)
Result of the bitwise XOR operation
not
Performs a bitwise NOT operation on an encrypted value.Encrypted value to negate
Result of the bitwise NOT operation
shl
Performs a shift left operation on an encrypted unsigned integer.Encrypted value to shift
Number of bits to shift left (must match lhs type)
Result of the shift left operation
shr
Performs a shift right operation on an encrypted unsigned integer.Encrypted value to shift
Number of bits to shift right (must match lhs type)
Result of the shift right operation
rol
Performs a rotate left operation on an encrypted unsigned integer.Encrypted value to rotate
Number of bits to rotate left (must match lhs type)
Result of the rotate left operation
ror
Performs a rotate right operation on an encrypted unsigned integer.Encrypted value to rotate
Number of bits to rotate right (must match lhs type)
Result of the rotate right operation
Comparison Operations
eq
Checks if two encrypted values are equal and returns an encrypted boolean.Left-hand side encrypted value
Right-hand side encrypted value (must match lhs type)
Encrypted boolean indicating equality
ne
Checks if two encrypted values are not equal and returns an encrypted boolean.Left-hand side encrypted value
Right-hand side encrypted value (must match lhs type)
Encrypted boolean indicating inequality
lt
Checks if the first encrypted unsigned integer is less than the second and returns an encrypted boolean.Left-hand side encrypted value
Right-hand side encrypted value (must match lhs type)
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.Left-hand side encrypted value
Right-hand side encrypted value (must match lhs type)
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.Left-hand side encrypted value
Right-hand side encrypted value (must match lhs type)
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.Left-hand side encrypted value
Right-hand side encrypted value (must match lhs type)
Encrypted boolean indicating if lhs is greater than or equal to rhs
Min/Max Functions
min
Returns the smaller of two encrypted unsigned integers.Left-hand side encrypted value
Right-hand side encrypted value (must match lhs type)
The smaller of the two encrypted values
max
Returns the larger of two encrypted unsigned integers.Left-hand side encrypted value
Right-hand side encrypted value (must match lhs type)
The larger of the two encrypted values
Control Flow
select
Conditionally selects between two encrypted values based on an encrypted boolean condition.Encrypted boolean condition
Value to return if condition is true
Value to return if condition is false (must match ifTrue type)
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.
Hash of the encrypted value that was previously decrypted
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.
Hash of the encrypted value that was previously decrypted
Decrypted result value (valid only if decrypted is true)
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.The ciphertext hash to publish a result for
The decrypted plaintext value (type matches the ctHash type)
The ECDSA signature from the Threshold Network’s Dispatcher
verifyDecryptResult
Verifies a decrypt result signature without publishing. Reverts if the signature is invalid.The ciphertext hash
The decrypted plaintext value
The ECDSA signature to verify
Returns
true if the signature is valid. Reverts otherwise.publishDecryptResultBatch
Publishes multiple signed decrypt results in a single transaction for gas efficiency.Array of ciphertext hashes
Array of decrypted plaintext values
Array of ECDSA signatures (one per result)
verifyDecryptResultSafe
Same asverifyDecryptResult, but returns false instead of reverting on invalid signatures.
The ciphertext hash
The decrypted plaintext value
The ECDSA signature to verify
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.
Ciphertext handle of the encrypted value
The decrypted plaintext value
The Threshold Network signature
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.Array of ciphertext handles
Array of decrypted plaintext values (must match ctHashes length and type)
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.Array of ciphertext handles
Array of decrypted plaintext values
Array of Threshold Network signatures
True if all signatures are valid (reverts otherwise)
verifyDecryptResultBatchSafe
LikeverifyDecryptResultBatch, but returns a bool[] indicating which entries are valid instead of reverting.
Array of ciphertext handles
Array of decrypted plaintext values
Array of Threshold Network signatures
Array of booleans —
true for each valid signature, false for invalidAccess Control
allow
Grants permission to the specified account to access the encrypted value.Encrypted value to grant access to
Address of the account to grant access to
allowThis
Grants permission to the current contract to access the encrypted value.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.
Encrypted value to grant public access to
allowSender
Grants permission to the message sender to access the encrypted value.Encrypted value to grant access to
allowTransient
Grants temporary permission to the specified account to access the encrypted value.Encrypted value to grant access to
Address of the account to grant temporary access to
isAllowed
Checks if the specified account has permission to access the encrypted value.Encrypted value to check access for
Address of the account to check
True if the account has permission, false otherwise
isPubliclyAllowed
Checks if the encrypted value has been granted public access viaallowPublic.
Encrypted value to check
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).Encrypted value to check
True if the value has been initialized, false otherwise
unwrap
Extracts the rawbytes32 handle from a typed encrypted value.
Encrypted value to unwrap
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.
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