asEbool, asEuintXX, asEaddress and asEbytesXX operations
This documentation covers the asEbool
, asEuintXX
, asEaddress
and asEbytesXX
operations provided by the TFHE library for working with encrypted data in the fhEVM. These operations are essential for converting between plaintext and encrypted types, as well as handling encrypted inputs.
The operations can be categorized into three main use cases:
Trivial encryption: Converting plaintext values to encrypted types
Type casting: Converting between different encrypted types
Input handling: Processing encrypted inputs with proofs
1. Trivial encryption
Trivial encryption simply put is a plain text in a format of a ciphertext.
Overview
Trivial encryption is the process of converting plaintext values into encrypted types (ciphertexts) compatible with TFHE operators. Although the data is in ciphertext format, it remains publicly visible on-chain, making it useful for operations between public and private values.
This type of casting involves converting plaintext (unencrypted) values into their encrypted equivalents, such as:
bool
→ebool
uint
→euintXX
bytes
→ebytesXX
address
→eaddress
Note: When doing trivial encryption, the data is made compatible with FHE operations but remains publicly visible on-chain unless explicitly encrypted.
Example
Trivial encryption of ebytesXX
types
ebytesXX
typesThe TFHE.padToBytesXX
functions facilitate this trivial encryption process for byte arrays, ensuring compatibility with ebytesXX
types. These functions:
Pad the provided byte array to the appropriate length (
64
,128
, or256
bytes).Prevent runtime errors caused by improperly sized input data.
Work seamlessly with
TFHE.asEbytesXX
for trivial encryption.
Important: Trivial encryption does NOT provide any privacy guarantees. The input data remains fully visible on the blockchain. Only use trivial encryption when working with public values that need to interact with actual encrypted data.
Workflow
Pad Input Data: Use the
padToBytesXX
functions to ensure your byte array matches the size requirements.Encrypt the Padded Data: Use
TFHE.asEbytesXX
to encrypt the padded byte array into the corresponding encrypted type.Grant Access: Use
TFHE.allowThis
andTFHE.allow
optionally, if you want to persist allowance for those variables for later use.
Example: Trivial Encryption with ebytesXX
ebytesXX
Below is an example demonstrating how to encrypt and manage ebytes64
, ebytes128
, and ebytes256
types:
2. Casting between encrypted types
This type of casting is used to reinterpret or convert one encrypted type into another. For example:
euint32
→euint64
ebytes128
→ebytes256
Casting between encrypted types is often required when working with operations that demand specific sizes or precisions.
Important: When casting between encrypted types:
Casting from smaller types to larger types (e.g.
euint32
→euint64
) preserves all informationCasting from larger types to smaller types (e.g.
euint64
→euint32
) will truncate and lose information
The table below summarizes the available casting functions:
euintX
euintX
TFHE.asEuintXX
ebool
euintX
TFHE.asEuintXX
euintX
ebool
TFHE.asEboolXX
Casting between encrypted types is efficient and often necessary when handling data with differing precision requirements.
Workflow for encrypted types
3. Encrypted input
Overview
Encrypted input casting is the process of interpreting a handle (ciphertext reference) and its proof as a specific encrypted type. This ensures the validity of the input before it is used in computations.
Encrypted inputs is in depth explained in the following section: encrypted inputs
Example
Details
Encrypted input casting validates:
The input handle references a valid ciphertext.
The accompanying proof matches the expected type.
For more information, see the Encrypetd inputs documentation
Overall operation summary
Trivial encryption
TFHE.asEuintXX(x)
uintX
euintX
TFHE.asEbool(x)
bool
ebool
TFHE.asEbytesXX(x)
bytesXX
ebytesXX
TFHE.asEaddress(x)
address
eaddress
Conversion between types
TFHE.asEuintXX(x)
euintXX
/ebool
euintYY
TFHE.asEbool(x)
euintXX
ebool
Encrypted input
TFHE.asEuintXX(x, y)
einput
, bytes
proof
euintX
TFHE.asEbool(x, y)
einput
,bytes
proof
ebool
TFHE.asEbytesXX(x, y)
einput
,bytes
proof
ebytesXX
TFHE.asEaddress(x, y)
einput
, bytes
proof
eaddress
Last updated