Skip to main content

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:
TypeDescription
eboolEncrypted boolean value
euint8Encrypted 8-bit unsigned integer
euint16Encrypted 16-bit unsigned integer
euint32Encrypted 32-bit unsigned integer
euint64Encrypted 64-bit unsigned integer
euint128Encrypted 128-bit unsigned integer
eaddressEncrypted Ethereum address

Type Conversion

From Plaintext to Encrypted Types

asEbool

value
bool
required
Plaintext boolean value to encrypt
securityZone
int32
Optional security zone identifier for isolating encrypted computations
result
ebool
Encrypted boolean value
Converts a plaintext boolean value to an encrypted boolean.

asEuint8

value
uint256
required
Plaintext value to encrypt (must fit in 8 bits)
securityZone
int32
Optional security zone identifier
result
euint8
Encrypted 8-bit unsigned integer
Converts a plaintext value to an encrypted 8-bit unsigned integer.

asEuint16

value
uint256
required
Plaintext value to encrypt (must fit in 16 bits)
securityZone
int32
Optional security zone identifier
result
euint16
Encrypted 16-bit unsigned integer
Converts a plaintext value to an encrypted 16-bit unsigned integer.

asEuint32

value
uint256
required
Plaintext value to encrypt (must fit in 32 bits)
securityZone
int32
Optional security zone identifier
result
euint32
Encrypted 32-bit unsigned integer
Converts a plaintext value to an encrypted 32-bit unsigned integer.

asEuint64

value
uint256
required
Plaintext value to encrypt (must fit in 64 bits)
securityZone
int32
Optional security zone identifier
result
euint64
Encrypted 64-bit unsigned integer
Converts a plaintext value to an encrypted 64-bit unsigned integer.

asEuint128

value
uint256
required
Plaintext value to encrypt (must fit in 128 bits)
securityZone
int32
Optional security zone identifier
result
euint128
Encrypted 128-bit unsigned integer
Converts a plaintext value to an encrypted 128-bit unsigned integer.

asEaddress

value
address
required
Plaintext Ethereum address to encrypt
securityZone
int32
Optional security zone identifier
result
eaddress
Encrypted Ethereum address
Converts a plaintext address value to an encrypted address.

From Encrypted Input Structures

asEbool (from InEbool)

value
InEbool
required
Encrypted input structure containing boolean data
result
ebool
Encrypted boolean value
Converts an encrypted input structure to an encrypted boolean.

asEuint8 (from InEuint8)

value
InEuint8
required
Encrypted input structure containing 8-bit integer data
result
euint8
Encrypted 8-bit unsigned integer
Converts an encrypted input structure to an encrypted 8-bit unsigned integer.

asEuint16 (from InEuint16)

value
InEuint16
required
Encrypted input structure containing 16-bit integer data
result
euint16
Encrypted 16-bit unsigned integer
Converts an encrypted input structure to an encrypted 16-bit unsigned integer.

asEuint32 (from InEuint32)

value
InEuint32
required
Encrypted input structure containing 32-bit integer data
result
euint32
Encrypted 32-bit unsigned integer
Converts an encrypted input structure to an encrypted 32-bit unsigned integer.

asEuint64 (from InEuint64)

value
InEuint64
required
Encrypted input structure containing 64-bit integer data
result
euint64
Encrypted 64-bit unsigned integer
Converts an encrypted input structure to an encrypted 64-bit unsigned integer.

asEuint128 (from InEuint128)

value
InEuint128
required
Encrypted input structure containing 128-bit integer data
result
euint128
Encrypted 128-bit unsigned integer
Converts an encrypted input structure to an encrypted 128-bit unsigned integer.

asEaddress (from InEaddress)

value
InEaddress
required
Encrypted input structure containing address data
result
eaddress
Encrypted Ethereum address
Converts an encrypted input structure to an encrypted 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.
lhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
rhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
result
euint8 | euint16 | euint32 | euint64 | euint128
Sum of the two encrypted values

sub

Performs subtraction of two encrypted unsigned integers.
lhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
rhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
result
euint8 | euint16 | euint32 | euint64 | euint128
Difference of the two encrypted values

mul

Performs multiplication of two encrypted unsigned integers.
lhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
rhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
result
euint8 | euint16 | euint32 | euint64 | euint128
Product of the two encrypted values

div

Performs division of two encrypted unsigned integers.
lhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value (dividend)
rhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (divisor, must match lhs type)
result
euint8 | euint16 | euint32 | euint64 | euint128
Quotient of the division
Division by zero will cause the operation to revert. Ensure the divisor is non-zero.

rem

Calculates the remainder when dividing two encrypted unsigned integers.
lhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value (dividend)
rhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (divisor, must match lhs type)
result
euint8 | euint16 | euint32 | euint64 | euint128
Remainder of the division

square

Calculates the square of an encrypted unsigned integer.
value
euint8 | euint16 | euint32 | euint64 | euint128
required
Encrypted value to square
result
euint8 | euint16 | euint32 | euint64 | euint128
Square of the encrypted value

Bitwise Operations

and

Performs a bitwise AND operation on two encrypted values.
lhs
ebool | euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
rhs
ebool | euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
result
ebool | euint8 | euint16 | euint32 | euint64 | euint128
Result of the bitwise AND operation

or

Performs a bitwise OR operation on two encrypted values.
lhs
ebool | euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
rhs
ebool | euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
result
ebool | euint8 | euint16 | euint32 | euint64 | euint128
Result of the bitwise OR operation

xor

Performs a bitwise XOR operation on two encrypted values.
lhs
ebool | euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
rhs
ebool | euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
result
ebool | euint8 | euint16 | euint32 | euint64 | euint128
Result of the bitwise XOR operation

not

Performs a bitwise NOT operation on an encrypted value.
value
ebool | euint8 | euint16 | euint32 | euint64 | euint128
required
Encrypted value to negate
result
ebool | euint8 | euint16 | euint32 | euint64 | euint128
Result of the bitwise NOT operation

shl

Performs a shift left operation on an encrypted unsigned integer.
lhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Encrypted value to shift
rhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Number of bits to shift left (must match lhs type)
result
euint8 | euint16 | euint32 | euint64 | euint128
Result of the shift left operation

shr

Performs a shift right operation on an encrypted unsigned integer.
lhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Encrypted value to shift
rhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Number of bits to shift right (must match lhs type)
result
euint8 | euint16 | euint32 | euint64 | euint128
Result of the shift right operation

rol

Performs a rotate left operation on an encrypted unsigned integer.
lhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Encrypted value to rotate
rhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Number of bits to rotate left (must match lhs type)
result
euint8 | euint16 | euint32 | euint64 | euint128
Result of the rotate left operation

ror

Performs a rotate right operation on an encrypted unsigned integer.
lhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Encrypted value to rotate
rhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Number of bits to rotate right (must match lhs type)
result
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.
lhs
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Left-hand side encrypted value
rhs
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Right-hand side encrypted value (must match lhs type)
result
ebool
Encrypted boolean indicating equality

ne

Checks if two encrypted values are not equal and returns an encrypted boolean.
lhs
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Left-hand side encrypted value
rhs
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Right-hand side encrypted value (must match lhs type)
result
ebool
Encrypted boolean indicating inequality

lt

Checks if the first encrypted unsigned integer is less than the second and returns an encrypted boolean.
lhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
rhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
result
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.
lhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
rhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
result
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.
lhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
rhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
result
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.
lhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
rhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
result
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.
lhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
rhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
result
euint8 | euint16 | euint32 | euint64 | euint128
The smaller of the two encrypted values

max

Returns the larger of two encrypted unsigned integers.
lhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Left-hand side encrypted value
rhs
euint8 | euint16 | euint32 | euint64 | euint128
required
Right-hand side encrypted value (must match lhs type)
result
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.
condition
ebool
required
Encrypted boolean condition
ifTrue
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Value to return if condition is true
ifFalse
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Value to return if condition is false (must match ifTrue type)
result
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
Selected value based on condition
Use select instead of if/else statements when working with encrypted values. Conditional branching doesn’t work with encrypted data.

Decryption Results

getDecryptResult

Retrieves a published decryption result. This function should be called after a result has been published via publishDecryptResult.
input
uint256
required
Hash of the encrypted value that was previously decrypted
result
uint256
Decrypted result value
This function will revert if the decryption result is not available yet. Use getDecryptResultSafe for non-reverting behavior.

getDecryptResultSafe

Safely retrieves the decrypted result of a previously decrypted value. Unlike getDecryptResult, this function returns a boolean flag indicating whether the decryption is complete.
input
uint256
required
Hash of the encrypted value that was previously decrypted
result
uint256
Decrypted result value (valid only if decrypted is true)
decrypted
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.
ctHash
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress | uint256
required
The ciphertext hash to publish a result for
result
bool | uint8 | uint16 | uint32 | uint64 | uint128 | address | uint256
required
The decrypted plaintext value (type matches the ctHash type)
signature
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.
ctHash
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress | uint256
required
The ciphertext hash
result
bool | uint8 | uint16 | uint32 | uint64 | uint128 | address | uint256
required
The decrypted plaintext value
signature
bytes
required
The ECDSA signature to verify
valid
bool
Returns true if the signature is valid. Reverts otherwise.

publishDecryptResultBatch

Publishes multiple signed decrypt results in a single transaction for gas efficiency.
ctHashes
uint256[]
required
Array of ciphertext hashes
results
uint256[]
required
Array of decrypted plaintext values
signatures
bytes[]
required
Array of ECDSA signatures (one per result)

verifyDecryptResultSafe

Same as verifyDecryptResult, but returns false instead of reverting on invalid signatures.
ctHash
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress | uint256
required
The ciphertext hash
plaintext
bool | uint8 | uint16 | uint32 | uint64 | uint128 | address | uint256
required
The decrypted plaintext value
signature
bytes
required
The ECDSA signature to verify
valid
bool
Returns true if valid, false if invalid (never reverts on bad signature)

verifyDecryptResultSafe

Like verifyDecryptResult, but returns false instead of reverting if the signature is invalid.
ctHash
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Ciphertext handle of the encrypted value
plaintext
bool | uint8 | uint16 | uint32 | uint64 | uint128 | address
required
The decrypted plaintext value
signature
bytes
required
The Threshold Network signature
result
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.
ctHashes
ebool[] | euint8[] | euint16[] | euint32[] | euint64[] | euint128[] | eaddress[]
required
Array of ciphertext handles
plaintexts
bool[] | uint8[] | uint16[] | uint32[] | uint64[] | uint128[] | address[]
required
Array of decrypted plaintext values (must match ctHashes length and type)
signatures
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.
ctHashes
ebool[] | euint8[] | euint16[] | euint32[] | euint64[] | euint128[] | eaddress[]
required
Array of ciphertext handles
plaintexts
bool[] | uint8[] | uint16[] | uint32[] | uint64[] | uint128[] | address[]
required
Array of decrypted plaintext values
signatures
bytes[]
required
Array of Threshold Network signatures
result
bool
True if all signatures are valid (reverts otherwise)

verifyDecryptResultBatchSafe

Like verifyDecryptResultBatch, but returns a bool[] indicating which entries are valid instead of reverting.
ctHashes
ebool[] | euint8[] | euint16[] | euint32[] | euint64[] | euint128[] | eaddress[]
required
Array of ciphertext handles
plaintexts
bool[] | uint8[] | uint16[] | uint32[] | uint64[] | uint128[] | address[]
required
Array of decrypted plaintext values
signatures
bytes[]
required
Array of Threshold Network signatures
results
bool[]
Array of booleans — true for each valid signature, false for invalid

Access Control

allow

Grants permission to the specified account to access the encrypted value.
value
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to grant access to
account
address
required
Address of the account to grant access to

allowThis

Grants permission to the current contract to access the encrypted value.
value
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to grant access to
Call allowThis after modifying encrypted values if you need to access them later in the same contract.

allowPublic

Grants public permission to access the encrypted value. Once called, anyone can request decryption of this value off-chain via decryptForTx without needing a permit.
value
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to grant public access to
Once allowPublic is called, the value can be decrypted by anyone. Only use this when you intend to reveal the value publicly (e.g., after an auction closes or when unwrapping tokens).

allowSender

Grants permission to the message sender to access the encrypted value.
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.
value
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to grant access to
account
address
required
Address of the account to grant temporary access to

isAllowed

Checks if the specified account has permission to access the encrypted value.
value
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to check access for
account
address
required
Address of the account to check
result
bool
True if the account has permission, false otherwise

isPubliclyAllowed

Checks if the encrypted value has been granted public access via allowPublic.
value
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to check
result
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).
value
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to check
result
bool
True if the value has been initialized, false otherwise

unwrap

Extracts the raw bytes32 handle from a typed encrypted value.
value
ebool | euint8 | euint16 | euint32 | euint64 | euint128 | eaddress
required
Encrypted value to unwrap
result
bytes32
The underlying ciphertext handle

wrapEbool / wrapEuint8 / wrapEuint16 / wrapEuint32 / wrapEuint64 / wrapEuint128 / wrapEaddress

Wraps a raw bytes32 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.
handle
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 calling FHE.* functions:
Each encrypted type has a corresponding binding library that includes all the operations available for that type.

Example with Bindings

Security Considerations

Always consider security implications when working with encrypted data.
  1. Initialization: All FHE functions check if their inputs are initialized and set them to 0 if not.
  2. Decryption: Decryption is a two-phase process — mark values with allowPublic on-chain, decrypt off-chain via the Client SDK, then publish/verify the result with publishDecryptResult or verifyDecryptResult. Only reveal values when absolutely necessary.
  3. Security Zones: Some functions accept a securityZone parameter to isolate different encrypted computations. FHE operations can only be performed between ciphertexts that share the same security zone.
  4. Access Control: The library provides fine-grained access control through the allow* functions. Always set proper permissions before accessing encrypted values.
  5. 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