Overview
Writing smart contracts with Fully Homomorphic Encryption (FHE) changes how you handle conditionals. Since all data is encrypted, you can’t use traditionalif...else statementsthere’s no way to view the values being compared.
Moreover, conditionals in FHE must evaluate both branches simultaneously. This is similar to constant-time cryptographic programming, where branching can leak information through timing attacksfor example, if one path takes longer to execute, an observer could infer which condition was true.
The Select Function
To handle encrypted conditionals, Fhenix uses a concept called a selectora function that takes an encrypted condition and two possible values, returning one based on the encrypted result. In practice, this is done with theselect function. It behaves like a ternary operator (condition ? a : b) but works entirely on encrypted data.
How It Works
FHE.select takes the encrypted ebool returned by comparison operations like gt. If the condition represents encrypted true, it returns the first value; otherwise, it returns the second valueall without revealing which path was taken.
Quick Start
Key Points to Remember
Encrypted Operations Only
All operations take place on encrypted data, so the actual values and comparison results stay concealed from observers.
No Traditional Branching
Traditional
if...else statements on encrypted data leak information through execution paths and timing.Select is Your Friend
The
select function is the only way to handle conditional execution in FHE without leaking information.Both Paths Execute
Both branches are evaluated, then the correct result is selected based on the encrypted condition.
Common Use Cases
Here are some common scenarios where you’ll useselect:
1. Maximum/Minimum Operations
Find the larger or smaller of two encrypted values:2. Conditional Updates
Update a value only when a condition is met:3. Threshold Checks
Cap values at a certain threshold:4. Conditional Access Control
Grant different permissions based on encrypted conditions:5. Fee Calculations
Apply different rates based on encrypted criteria:Best Practices
Always Use Select for Conditionals
Always Use Select for Conditionals
Never try to implement branching logic with traditional
if...else statements on encrypted data. Always use select to ensure constant-time execution and prevent information leakage.Keep Conditional Logic Simple and Linear
Keep Conditional Logic Simple and Linear
Complex nested conditions should be broken down into simpler operations. Each
select can only choose between two values, so chain them carefully.Remember All Operations Are on Encrypted Data
Remember All Operations Are on Encrypted Data
Every value, comparison, and result remains encrypted throughout the entire process. The blockchain never sees plaintext values.
Consider Performance Implications
Consider Performance Implications
Since both branches of a
select are always evaluated, complex operations in both paths will always execute. Structure your code to minimize unnecessary computations.Complete Example: Auction Bid
Here’s a practical example showing how to handle encrypted bids in an auction:In the example above, the actual bid amounts remain encrypted throughout the auction. Only the final winner can be revealed through controlled decryption.
Related Topics
- Learn more about comparison operations in FHE Encrypted Operations
- Understand how to manage access in Access Control