fhEVM
WebsiteLibrariesProduct & ServicesDevelopersSupport
0.6
0.6
  • Welcome to fhEVM
  • White paper
  • Getting Started
    • Overview
    • Quick Start
      • Remix
        • 1. Setting up Remix
        • 2. Connect your wallet to Remix
        • 3. Deploying ConfidentialERC20
        • 4. Interacting with the contract
      • Hardhat
        • Prerequisites
        • 1. Setting up Hardhat
        • 2. Writing contracts
        • 3. Testing in mocked mode
        • 4. Deploying the contract
        • 5. Interacting with the contract
  • Tutorials
    • See all tutorials
  • Smart contract
    • Key features
    • Configuration
    • FhEVM contracts
    • Supported types
    • Operations on encrypted types
    • Access Control List
      • ACL examples
    • Encrypted Inputs
    • Decryption
      • Decryption
      • Decryption in depth
      • Re-encryption
    • If sentences
    • Branching in FHE
    • AsEbool, asEuintXX, asEaddress and asEbytesXX operations
    • Generate random numbers
    • Error handling
    • Gas estimation
    • Debug decrypt
    • Using Foundry
  • Frontend
    • Setup
    • Build a web application
    • Using React.js
    • Using Next.js
    • Using Vue.js
    • Using Node or Typescript
    • Using the CLI
    • Common webpack errors
  • Explanations
    • Architectural overview
    • FHE on blockchain
    • fhEVM components
    • Encryption, decryption, re-encryption, and computation
  • References
    • Table of all addresses
    • Smart contracts - fhEVM API
    • Frontend - fhevmjs lib
    • Repositories
  • Developer
    • Contributing
    • Development roadmap
    • Release note
    • Feature request
    • Bug report
    • Status
Powered by GitBook

Libraries

  • TFHE-rs
  • Concrete
  • Concrete ML
  • fhEVM

Developers

  • Blog
  • Documentation
  • Github
  • FHE resources

Company

  • About
  • Introduction to FHE
  • Media
  • Careers
On this page
  • Overview
  • Using TFHE.select for conditional logic
  • Example: Auction Bidding Logic
  • How It Works
  • Key Considerations
  • Summary

Was this helpful?

Export as PDF
  1. Smart contract

Branching in FHE

PreviousIf sentencesNextAsEbool, asEuintXX, asEaddress and asEbytesXX operations

Last updated 1 month ago

Was this helpful?

This document explains how to implement conditional logic (if/else branching) when working with encrypted values in fhEVM. Unlike typical Solidity programming, working with Fully Homomorphic Encryption (FHE) requires specialized methods to handle conditions on encrypted data.

Overview

In fhEVM, when you perform , the result is an encrypted boolean (ebool). Since encrypted booleans do not support standard boolean operations like if statements or logical operators, conditional logic must be implemented using specialized methods.

To facilitate conditional assignments, fhEVM provides the TFHE.select function, which acts as a ternary operator for encrypted values.

Using TFHE.select for conditional logic

The TFHE.select function enables branching logic by selecting one of two encrypted values based on an encrypted condition (ebool). It works as follows:

TFHE.select(condition, valueIfTrue, valueIfFalse);
  • condition: An encrypted boolean (ebool) resulting from a comparison.

  • valueIfTrue: The encrypted value to return if the condition is true.

  • valueIfFalse: The encrypted value to return if the condition is false.

Example: Auction Bidding Logic

Here's an example of using conditional logic to update the highest winning number in a guessing game:

function bid(einput encryptedValue, bytes calldata inputProof) external onlyBeforeEnd {
  // Convert the encrypted input to an encrypted 64-bit integer
  euint64 bid = TFHE.asEuint64(encryptedValue, inputProof);

  // Compare the current highest bid with the new bid
  ebool isAbove = TFHE.lt(highestBid, bid);

  // Update the highest bid if the new bid is greater
  highestBid = TFHE.select(isAbove, bid, highestBid);

  // Allow the contract to use the updated highest bid ciphertext
  TFHE.allowThis(highestBid);
}

How It Works

  • Comparison:

    • The TFHE.lt function compares highestBid and bid, returning an ebool (isAbove) that indicates whether the new bid is higher.

  • Selection:

    • The TFHE.select function updates highestBid to either the new bid or the previous highest bid, based on the encrypted condition isAbove.

  • Permission Handling:

    • After updating highestBid, the contract reauthorizes itself to manipulate the updated ciphertext using TFHE.allowThis.

Key Considerations

  • Value change behavior: Each time TFHE.select assigns a value, a new ciphertext is created, even if the underlying plaintext value remains unchanged. This behavior is inherent to FHE and ensures data confidentiality, but developers should account for it when designing their smart contracts.

  • Gas consumption: Using TFHE.select and other encrypted operations incurs additional gas costs compared to traditional Solidity logic. Optimize your code to minimize unnecessary operations.

  • Access control: Always use appropriate ACL functions (e.g., TFHE.allowThis, TFHE.allow) to ensure the updated ciphertexts are authorized for use in future computations or transactions.


Summary

  • TFHE.select is a powerful tool for conditional logic on encrypted values.

  • Encrypted booleans (ebool) and values maintain confidentiality, enabling privacy-preserving logic.

  • Developers should account for gas costs and ciphertext behavior when designing conditional operations.

This is a simplified example to demonstrate the functionality. For a complete implementation with proper error handling and additional features, see the .

For more information on the supported operations, see the .

comparison operations
Blind Auction contract example
fhEVM API documentation