# FHE Compute Execution Virtual Machine

In Sight Oracle we use a simple FHE Compute Execution VM as a conceptual framework for defining, managing, and executing complex sequences of operations on encrypted data using Fully Homomorphic Encryption (FHE). This VM allows secure computation over encrypted values, ensuring data privacy while performing meaningful computations.

### Key Characteristics of a FHE Compute Execution VM

#### 1. Instruction Set

The VM has an instruction set defined by the `opcode` values in the `Operation` structure. Each `opcode` represents a specific operation, such as:

* **Loading Encrypted Values**: Instructions to load encrypted boolean (`get_ebool`) or integer (`get_euint64`) values.
* **Arithmetic Operations**: Instructions to perform operations like addition (`add_euint64_euint64`) and subtraction (`sub_euint64_euint64`).
* **Random Value Generation**: Instructions to generate random encrypted values (`rand_euint64`).

#### 2. Memory Management

The VM uses the `operands` array within each `Operation` and the `ops` array within the `Request` structure to act as memory:

* **Operations Array**: Stores all the operations within a request, allowing them to reference each other by index.
* **Operands Array**: Holds indices or direct encrypted values required for each operation.

This setup allows operations to reference the results of previous operations, much like a traditional VM's memory management.

#### 3. Execution Context

The `Request` structure serves as the execution context of the VM. It maintains:

* **Current State**: Tracks the current state of execution using `opsCursor`.
* **Operations**: Holds the list of operations to be performed.
* **Metadata**: Includes information about the requester, callback details, and additional payload data.

#### 4. Program Execution

The VM processes the request in a manner similar to compiling and executing a program:

1. **Initialization**: Creating a new request initializes the execution context.
2. **Adding Operations**: Operations are added to the request, each representing an instruction to be executed.
3. **Finalization**: Finalizing the request prepares it for execution by generating a unique ID and ensuring all operations are set up correctly.
4. **Execution**: The VM executes each operation in sequence, interpreting the `opcode` and performing the corresponding action.
5. **Callback**: After execution, the VM invokes a callback function to notify the requester of the results.

### Example

Below is the structure used in Sight Oracle Solidity Contract:

```solidity
library RequestBuilder {
    struct Operation {
        uint8 opcode;
        uint256[] operands; // Indices of the operands or an encrypted value.
        uint64 value; // Direct value if applicable
    }

    struct Request {
        bytes32 id;
        address requester;
        Operation[] ops;
        uint256 opsCursor;
        address callbackAddr;
        bytes4 callbackFunc;
        bytes payload;
    }
}
```
