TFHE-rs
WebsiteLibrariesProduct & ServicesDevelopersSupport
1.0
1.0
  • Welcome to TFHE-rs
  • Get Started
    • What is TFHE-rs?
    • Installation
    • Quick start
    • Benchmarks
      • CPU Benchmarks
        • Integer
        • Programmable bootstrapping
      • GPU Benchmarks
        • Integer
        • Programmable bootstrapping
      • Zero-knowledge proof benchmarks
    • Security and cryptography
  • FHE Computation
    • Types
      • Integer
      • Strings
      • Array
    • Operations
      • Arithmetic operations
      • Bitwise operations
      • Comparison operations
      • Min/Max operations
      • Ternary conditional operations
      • Casting operations
      • Boolean Operations
      • String Operations
    • Core workflow
      • Configuration and key generation
      • Server key
      • Encryption
      • Decryption
      • Parameters
    • Data handling
      • Compressing ciphertexts/keys
      • Serialization/deserialization
      • Data versioning
    • Advanced features
      • Encrypted pseudo random values
      • Overflow detection
      • Public key encryption
      • Trivial ciphertexts
      • Zero-knowledge proofs
      • Multi-threading with Rayon crate
    • Tooling
      • PBS statistics
      • Generic trait bounds
      • Debugging
  • Configuration
    • Advanced Rust setup
    • GPU acceleration
    • Parallelized PBS
  • Integration
    • JS on WASM API
    • High-level API in C
  • Tutorials
    • Homomorphic parity bit
    • Homomorphic case changing on Ascii string
    • SHA256 with Boolean API
    • All tutorials
  • References
    • API references
    • Fine-grained APIs
      • Quick start
      • Boolean
        • Operations
        • Cryptographic parameters
        • Serialization/Deserialization
      • Shortint
        • Operations
        • Cryptographic parameters
        • Serialization/Deserialization
      • Integer
        • Operations
        • Cryptographic parameters
        • Serialization/Deserialization
    • Core crypto API
      • Quick start
      • Tutorial
  • Explanations
    • TFHE deep dive
  • Developers
    • Contributing
    • Release note
    • Feature request
    • Bug report
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

Was this helpful?

Export as PDF
  1. FHE Computation
  2. Operations

Ternary conditional operations

This document details the ternary operations supported by TFHE-rs.

The ternary conditional operator execute conditional instructions in the form if cond { choice_if_true } else { choice_if_false }.

name
symbol
type

Ternary operator

select

Ternary

The syntax is encrypted_condition.select(encrypted_choice_if_true, encrypted_choice_if_false). The valid encrypted_condition must be an encryption of 0 or 1.

The following example shows how to perform ternary conditional operations:

use tfhe::prelude::*;
use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheInt32};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Basic configuration to use homomorphic integers
    let config = ConfigBuilder::default().build();

    // Key generation
    let (client_key, server_keys) = generate_keys(config);
    
    let clear_a = 32i32;
    let clear_b = -45i32;
    
    // Encrypting the input data using the (private) client_key
    // FheInt32: Encrypted equivalent to i32
    let encrypted_a = FheInt32::try_encrypt(clear_a, &client_key)?;
    let encrypted_b = FheInt32::try_encrypt(clear_b, &client_key)?;
    
    // On the server side:
    set_server_key(server_keys);
    
    // Clear equivalent computations: 32 > -45
    let encrypted_comp = &encrypted_a.gt(&encrypted_b);
    let clear_res = encrypted_comp.decrypt(&client_key);
    assert_eq!(clear_res, clear_a > clear_b);
    
    // `encrypted_comp` is a FheBool, thus it encrypts a boolean value.
    // This acts as a condition on which the
    // `select` function can be applied on.
    // Clear equivalent computations:
    // if 32 > -45 {result = 32} else {result = -45}
    let encrypted_res = &encrypted_comp.select(&encrypted_a, &encrypted_b);
    
    let clear_res: i32 = encrypted_res.decrypt(&client_key);
    assert_eq!(clear_res, clear_a);
    
    Ok(())
}
PreviousMin/Max operationsNextCasting operations

Last updated 2 months ago

Was this helpful?