Skip to main content

Overview

Writing smart contracts with Fully Homomorphic Encryption (FHE) changes how you handle conditionals. Since all data is encrypted, you can’t use traditional if...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.
Using traditional if...else on encrypted data might result in unexpected behavior and leak information about your encrypted values.

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 the select 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 use select:

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

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.
Complex nested conditions should be broken down into simpler operations. Each select can only choose between two values, so chain them carefully.
Every value, comparison, and result remains encrypted throughout the entire process. The blockchain never sees plaintext values.
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. To reveal the winner, a client calls decryptForTx off-chain to obtain the plaintext and a threshold signature, then submits them on-chain via revealWinner for verification.