> ## Documentation Index
> Fetch the complete documentation index at: https://cofhe-docs.fhenix.zone/llms.txt
> Use this file to discover all available pages before exploring further.

# FHE Operation Request Flow

> Complete flow of an FHE operation request in the CoFHE ecosystem through smart contracts

# FHE Operation Request Flow

## Overview

This document outlines the complete flow of an FHE (Fully Homomorphic Encryption) operation request in the CoFHE ecosystem through Smart Contracts. Understanding this process is essential for developers integrating private computation capabilities into their smart contracts.

## Key Components

| Component             | Description                                                             |
| --------------------- | ----------------------------------------------------------------------- |
| **dApp**              | The decentralized application that requests FHE operations              |
| **FHE.sol**           | The library providing FHE operation functions                           |
| **Task Manager**      | Verifies and forwards operation requests                                |
| **Slim Listener**     | Monitors blockchain events and forwards requests to the execution layer |
| **Result Processor**  | Handles operation results and publishes them back to the blockchain     |
| **fheOS Server**      | Executes the actual FHE operations                                      |
| **Threshold Network** | (When applicable) Handles secure decryption operations                  |

## Flow Diagram

The following diagram illustrates the complete flow of an FHE operation request in the CoFHE ecosystem:

<Frame>
  <img src="https://mintcdn.com/fhenix/QsDx0SV0x2gd-xtZ/images/Transactions.svg?fit=max&auto=format&n=QsDx0SV0x2gd-xtZ&q=85&s=ffa819f31d8179f364c912556bc7f386" alt="End-to-end flow of an FHE operation request through the CoFHE system components" width="2784" height="1320" data-path="images/Transactions.svg" />
</Frame>

*Figure 1: End-to-end flow of an FHE operation request through the CoFHE system components*

## Step-by-Step Flow

<Steps>
  <Step title="Integration with the Client SDK">
    The decentralized application (dApp) integrates with CoFHE by utilizing the **Client SDK** (`@cofhe/sdk`) for encryption.

    [See in GitHub](https://github.com/FhenixProtocol/cofhesdk) 1️⃣

    [Encrypt request](/deep-dive/data-flows/encryption-request-flow) using the Client SDK, returns `InEuint` structure.

    <Note>
      This step happens on the client side before blockchain interaction.
    </Note>
  </Step>

  <Step title="Requesting an FHE Operation">
    When the dApp needs to perform an encrypted operation within the smart contract: 2️⃣

    **Import the FHE library in Solidity:**

    ```solidity theme={null}
    import "@fhenixprotocol/cofhe-contracts/FHE.sol";
    ```

    **Call the appropriate FHE function** from the imported library:

    ```solidity theme={null}
    // using trivial encrypt or the returned structures from the previous step.
    function addExample(InEuint32 encryptedInput)  {
        euint32 lhs = FHE.asEuint32(encryptedInput);
        euint32 rhs = FHE.asEuint32(10);
        
        // Request an operation (addition in this example)
        euint32 result = FHE.add(lhs, rhs);
    }
    ```

    **FHE.sol forwards the request** to the Task Manager contract
  </Step>

  <Step title="Task Manager Processing">
    The Task Manager serves as the gateway for all FHE operation requests:

    1. **Validate request structure** to ensure all inputs are properly formatted
    2. **Verify access permissions** by checking if the caller has proper access to the encrypted inputs (using ACL.sol) 3️⃣
    3. **Generate a unique handle** that will be used to reference the future ciphertext result
    4. **Return the handle** to the calling dApp contract
    5. **Emit an event** containing the operation details for the Slim Listener to process 4️⃣
  </Step>

  <Step title="Slim Listener Processing">
    The Slim Listener monitors and forwards FHE operation requests:

    1. **Listen for events** from the Task Manager 5️⃣
    2. **Forward request details** to the fheOS server 6️⃣
  </Step>

  <Step title="FheOS server - FHE Operation Execution">
    The FheOS server handles requests:

    1. **Create execution thread** on the fheOS server
    2. **Execute the requested operation** on encrypted data
    3. **Generate result ciphertext** containing the encrypted result
    4. **Map the handle** to the actual ciphertext hash in the private storage
    5. **Make result available** for subsequent operations
    6. **Notify the Result Processor** of operation completion
  </Step>

  <Step title="Result Handling (For Standard Operations)">
    For standard FHE operations (not decryption):

    1. **Update ciphertext registry** with the new encrypted result 7️⃣

    At this point **the operation cycle is completed**, preserving the confidentiality of all encrypted values.
  </Step>
</Steps>
