# Fair Math VM

<figure><img src="/files/Djt6tJ6e8uDqWD29vo0y" alt=""><figcaption></figcaption></figure>

**FairMath Virtual Machine**

The FairMath Virtual Machine (fairmath-vm) is  running on the actor side, responsible for executing instructions received from the [Orchestration Layer](https://docs.fairmath.xyz/) through [Order Book](/fhe-computer/operating-system/order-book.md).

We have implemented the virtual machine based on **OpenFHE**, but the specification allows actors to implement it using other libraries, providing flexibility to developers.

FMVM executes compiled FHE applications or their parts and returns the execution results. It operates solely with local files, with all necessary data prepared in advance.

#### Required Input Data:

1. **Instructions**: A file containing the instructions to execute.
2. **Execution Context**: A file with precomputed variables (possibly generated on another actor). The execution context allows splitting the application into blocks and running these blocks across multiple actors.

This approach is explained in detail in our Whitepaper.

FMVM requires no additional configuration, making it simple and efficient to use.

### How To Build

* Install CMake 3.22(or above), gcc or clang
* Install OpenMP(this is not necessary but highly recommended)
* Clone vcpkg repo and install binary

```
$ git clone https://github.com/microsoft/vcpkg.git
$ cd vcpkg && ./bootstrap-vcpkg.sh
```

* Add the following env vars

```
$ export VCPKG_ROOT=/path/to/cloned/vcpkg/repo
$ export PATH=$VCPKG_ROOT:$PATH
```

You may also to add these variables to your `.bashrc` file for convenience.

* Clone `fairmath-vm` repository

```
$ git clone https://github.com/fairmath/fairmath-vm
```

* Configure cmake and build the project. You should specify vcpkg path, build type and directory for cmake generated file.

```
$ VCPKG_ROOT=/path/to/cloned/vcpkg/repo BUILD_TYPE=Release BIN_DIR=rel cmake --preset=base && cmake --build BUILD/rel
```

All files will be located in `BUILD` directory. You can omit VCPKG\_ROOT variable if the one was defined previously(e.g. in the `.bashrc` file) Also it is possible to create user defined presets for CMake. Just create the following file in the root of the repository CMakeUserPresets.json:

```
{
    "version": 2,
    "configurePresets": [
      {
        "name": "dbg",
        "inherits": "base",
        "environment": {
          "VCPKG_ROOT": "/path/to/cloned/vcpkg/repo",
          "BUILD_TYPE": "Debug",
          "BIN_DIR": "dbg"
        }
      },
      {
        "name": "rel",
        "inherits": "base",
        "environment": {
          "VCPKG_ROOT": "/path/to/cloned/vcpkg/repo",
          "BUILD_TYPE": "Release",
          "BIN_DIR": "rel"
        }
      }
    ]
}
```

Since that you are able to run the following command to build release configuration

```
$ cmake --preset=rel && cmake --build BUILD/rel
```

Change rel to dbg to build a debug configuration.

The very first build could take a time because of vcpkg will build 3rd party libraries for this project. NExt builds will be mush faster since 3rd party libraries will be already built and cached.

* Installation Run cmake install to gather all binary files related to the build.

```
$ cmake --install BUILD/rel
```

If you are using provided `CMakeUserPresets.json` then build will be installed to the `BUILD/install` directory.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.fairmath.xyz/fhe-computer/fair-math-actor/fair-math-vm.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
