# Hello (CIFAR) world!

We have deployed an application capable of classifying encrypted images from the CIFAR-10 dataset. This page provides instructions on setting up the environment, preparing inputs, running the application, and obtaining the results.

### Prerequisites

* `fairmathd` :&#x20;
  * [Mac OS darwin-amd64 ](https://fairmath-releases.s3.eu-central-1.amazonaws.com/faimrathd/v0.0.20/fairmathd-darwin-amd64.zip)
  * [Mac OS darwin-arm64 ](https://fairmath-releases.s3.eu-central-1.amazonaws.com/faimrathd/v0.0.20/fairmathd-darwin-arm64.zip)
  * [Linux amd64](https://fairmath-releases.s3.eu-central-1.amazonaws.com/faimrathd/v0.0.20/fairmathd-linux-amd64.zip)
  * [Linux arm64](https://fairmath-releases.s3.eu-central-1.amazonaws.com/faimrathd/v0.0.20/fairmathd-linux-arm64.zip)
* `fairmath-cli`  latest release can be downloaded from the [github page](https://github.com/fairmath/fairmath-cli/actions/runs/11970928596)
* FHE Computer web frontend - <https://computer.fairmath.xyz/dashboard>

### Setup

* Unpack and install downloaded `fairmathd` and `fairmath-cli` zip files

### Registration

To run any application on the FHE computer, you need an address on the Fair Math network. If you don’t have one yet, please follow these steps:

1. Run init command:

   ```bash
   $ ./fairmathd init <username> --chain-id fairmath-tn1
   ```

   It will generate config file, by default located at `.fairnet/config/client.toml.`
2. Now we need update the `config` file with the following fields:

   ```bash
   node = "<https://testnet.computer.fairmath.xyz>"
   chain-id = "fairmath-tn1"
   keyring-backend = "test"
   ```
3. Getting a FairMath address:

   ```bash
   $ ./fairmathd keys add <accountname> --keyring-backend=test
   ```

   Note that keyring backend test will store your private key in the open file, never use this option for mainnet
4. You can request tokens from someone or use our [Faucet](https://faucet.fairmath.xyz) to obtain them. Tokens can be transferred between accounts using the following command:

   ```bash
   $ ./fairmathd tx bank send <from address> <to address> 100stake --from <from account> --chain-id testnet --keyring-backend test 
   ```

   You can find the address using the `keys list` command:

   ```bash
   $ ./fairmathd keys list --keyring-backend=test
   - address: fairmath19wjlxxzef8nvlfr63xr9c8vkefmf54awtp8mk0
     name: <accountname>
     pubkey: '{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A2akCmQrGqdJWMjQw7xqR8I188pI+KVb5z/O+1GTXYbN"}'
     type: local
   ```

   Once you receive tokens you can run an application on FHE computer

### Running Application

1. Each application comes with a configuration file that contains all the necessary information for preparing encrypted inputs. Ath te first step you have to get this config. The name of CIFAR application is `cifar-split.`  To download an  config use the  following command:

   ```bash
   $ ./fairmathd actor app-config cifar-split --grpc-addr=testnet.computer.fairmath.xyz:9090
   ```

   If everything is set up correctly, you will see similar logs:: <br>

   ```
   024/11/25 10:23:27 Download config for the following apps:
   2024/11/25 10:23:27 Name: cifar-split	ID: 12	Author: fairmath19wjlxxzef8nvlfr63xr9c8vkefmf54awtp8mk0
   You can see a message about downloaded configs:
   ```

   ```bash
   Name: cifar-split	ID: 12	Author: fairmath19wjlxxzef8nvlfr63xr9c8vkefmf54awtp8mk0
   ```

2. From the message above, we will need the ID later to run the application. Once this command is executed, an FHE configuration file named `cifar-split-12-fhecfg.json` will be generated with all parameters preconfigured.

3. Based on the configuration file, the `fairmath-cli` will generate all the required keys, cryptocontext and ciphertexts. At this stage, we only need to provide the plaintext value in the `plaintext_value` field. Images from the CIFAR dataset can be represented as a vector of numbers. We have prepared [scripts](https://github.com/fairmath/cifar10-image-convert) that generate the corresponding vector from an image. Now we can put input for our applicaiton in config file in open form to generate cipheretx.\
   In this config you must provide just `plaintext_value` for the `input` object.&#x20;

   Example of input section:

   ```bash
    ...
    "input": {
       "cryptocontext": "ctx",
       "plaintext_value": [132,125,130,143,142,145,148,147,148,136,122,133,142,126,123,134,132,114,120,123,118,136,122,116,107,98,97,104,98,102,97,98,146,156,154,145,139,146,145,143,146,132,120,125,125,120,113,127,134,122,129,135,126,126,117,131,122,100,98,105,103,104,88,85,151,160,165,159,151,160,148,138,146,140,124,137,128,128,118,129,134,127,124,138,134,128,110,108,112,101,104,115,120,115,94,91,148,142,161,160,146,156,152,140,143,148,143,124,131,126,123,125,127,119,107,115,127,123,110,97,100,104,114,122,124,117,107,102,160,159,168,159,131,137,146,143,141,149,125,95,121,121,103,97,106,116,137,142,136,112,105,100,101,109,116,106,92,86,96,99,165,171,173,158,153,163,134,141,142,127,116,121,128,111,107,103,98,105,132,172,190,173,147,116,100,116,118,99,86,77,85,96,156,154,170,156,173,173,145,150,155,140,134,137,134,133,129,126,112,114,97,101,143,189,194,178,141,115,117,100,90,75,81,96,146,141,155,149,167,168,160,153,147,151,156,157,142,144,133,145,152,145,137,123,98,115,123,137,150,111,110,101,89,88,76,85,145,144,155,165,172,170,157,147,140,132,135,140,132,140,147,143,135,127,127,123,108,103,88,71,73,57,62,81,73,81,78,81,154,148,148,176,180,168,156,141,161,156,136,145,138,147,153,144,134,135,130,107,73,88,93,91,81,34,27,60,70,76,82,76,158,161,154,186,197,134,117,141,182,175,140,145,151,160,155,149,127,130,122,115,91,70,64,56,69,61,52,66,79,84,87,79,142,164,186,201,216,108,46,117,187,182,141,130,102,137,159,150,126,118,108,108,98,81,83,58,49,58,68,69,83,90,86,80,134,137,166,191,209,153,99,145,160,153,148,125,122,127,165,142,99,104,130,83,67,63,68,67,52,38,37,74,83,76,75,83,136,125,132,156,177,182,180,190,144,100,130,110,142,162,160,144,131,101,126,105,80,60,53,79,63,44,56,100,77,66,78,92,142,134,123,128,145,163,179,186,154,95,125,120,132,170,164,147,148,123,118,104,86,69,72,108,73,65,105,100,62,61,82,96,143,135,130,120,128,143,149,156,167,146,152,129,137,171,190,163,142,129,119,95,76,49,93,114,68,99,126,88,54,33,55,82,142,132,121,120,127,101,108,120,133,147,149,102,127,175,188,182,157,134,114,109,101,73,119,107,73,130,113,73,43,18,35,69,160,136,109,123,130,93,66,91,98,90,103,92,116,147,154,152,139,128,124,124,104,105,131,99,99,135,94,63,34,17,39,72,181,154,120,123,120,68,40,79,111,105,109,122,129,119,134,133,125,113,111,99,93,129,135,112,135,127,87,64,26,18,48,74,186,174,151,137,119,63,14,41,130,159,127,163,182,133,117,122,118,104,85,71,96,138,136,120,149,113,79,55,17,28,73,72,177,181,160,140,130,91,29,6,43,103,116,156,197,171,109,99,82,69,60,69,118,150,126,121,146,97,67,41,20,56,101,87,172,169,154,128,139,138,84,33,5,27,77,106,157,170,122,83,79,71,68,72,115,167,129,141,140,83,57,30,32,87,112,94,152,142,148,139,152,163,140,114,85,31,25,70,126,128,116,76,87,75,83,67,100,178,150,162,120,72,42,22,51,101,106,95,138,143,152,142,140,160,160,160,182,130,53,93,152,111,90,64,77,77,70,27,92,188,158,153,93,60,27,26,79,110,107,92,134,155,151,132,133,150,158,156,168,210,179,169,148,104,53,34,53,56,41,45,141,186,136,108,77,43,26,62,96,101,112,90,147,152,138,118,139,164,176,182,178,189,173,146,115,65,28,17,31,75,95,144,177,163,113,90,58,29,46,103,111,92,98,97,144,145,130,111,123,131,136,133,165,181,133,99,73,30,18,45,85,133,160,166,159,136,109,76,37,51,95,116,120,102,88,98,140,139,121,114,119,132,125,123,154,155,106,58,36,24,36,99,130,134,148,143,111,98,83,46,45,98,125,135,127,113,94,98,135,128,116,116,122,148,153,180,143,114,107,92,94,91,117,150,142,133,84,92,115,109,53,37,96,131,131,144,120,109,113,102,130,132,114,110,121,141,132,139,150,146,142,136,138,116,139,165,167,165,113,127,130,106,90,84,123,137,132,121,110,119,125,106,130,139,118,122,130,125,125,139,147,134,133,132,138,117,129,164,167,161,146,128,130,127,138,140,134,135,132,119,117,121,116,102,129,136,123,130,123,122,137,132,125,125,126,122,131,124,135,151,146,146,141,139,143,148,134,120,129,127,132,127,120,112,113,106,142,130,133,147,148,151,155,153,154,142,129,137,147,135,133,144,142,126,131,133,125,143,128,128,125,115,114,121,116,120,116,113,155,161,157,150,143,149,151,149,151,139,127,130,130,129,125,137,144,133,139,142,133,135,126,145,140,118,116,122,122,122,107,101,160,165,168,163,154,162,153,142,151,144,129,140,131,136,129,138,142,136,132,143,141,139,122,123,130,119,121,132,138,133,112,107,157,147,163,162,150,161,157,145,149,152,147,126,135,134,134,134,135,128,115,120,136,137,126,115,118,122,132,139,141,134,125,118,169,164,171,162,140,150,157,153,152,157,130,100,128,133,119,107,115,126,147,151,148,132,128,121,119,127,133,123,109,103,113,115,173,175,176,162,168,186,156,160,161,145,128,131,140,128,128,122,116,122,149,188,204,183,155,125,111,130,134,118,104,94,103,113,162,159,175,164,192,202,176,176,183,169,154,153,152,155,153,155,141,141,121,122,153,185,181,172,144,123,131,119,108,93,99,115,147,147,166,162,188,194,189,181,178,184,182,180,165,167,156,169,179,171,163,145,111,117,121,136,154,119,123,119,107,106,94,104,143,154,175,186,193,188,178,171,167,163,165,168,157,163,169,164,159,153,155,147,128,118,100,79,81,67,75,98,91,99,96,100,154,168,181,205,198,175,166,158,180,179,165,171,160,171,178,166,157,163,163,134,97,113,116,106,92,45,39,76,88,94,100,95,165,195,198,219,209,128,114,146,189,188,164,163,165,184,186,176,156,165,162,149,122,101,85,70,83,73,65,83,97,102,104,98,154,204,237,235,226,95,34,117,187,185,158,143,112,160,192,177,151,156,155,145,129,112,101,68,64,71,79,87,103,110,105,99,138,164,207,230,233,158,95,162,171,146,151,145,140,143,189,146,91,127,178,111,83,81,85,74,71,52,36,94,111,101,99,104,135,137,158,194,213,207,196,219,160,95,134,128,160,183,195,169,136,125,160,125,98,76,71,94,89,55,57,126,107,90,101,112,139,136,132,151,175,191,207,214,169,101,142,135,144,193,203,197,185,159,147,127,115,93,94,139,103,70,118,135,88,80,102,117,139,131,127,121,135,156,167,177,182,161,176,147,148,182,197,194,177,170,160,128,109,79,122,155,96,106,154,127,76,45,70,104,138,130,117,109,118,99,112,130,146,162,170,121,143,181,174,187,172,164,155,140,129,103,155,144,89,147,154,108,60,22,46,90,157,138,113,121,128,93,68,95,105,99,111,102,135,168,161,163,145,140,136,134,121,133,173,126,100,165,142,91,46,14,45,92,180,157,127,128,124,73,43,80,117,115,115,135,156,150,156,156,142,126,117,106,113,162,178,128,137,171,135,83,35,19,54,92,184,174,153,140,123,65,14,39,137,177,142,185,214,162,133,143,144,127,106,93,128,177,170,127,165,166,117,69,27,37,83,87,175,179,158,142,134,95,31,3,48,118,133,177,226,202,128,114,100,85,74,89,149,182,143,127,173,145,95,49,30,67,112,100,169,165,148,128,141,141,86,29,4,32,86,116,180,204,145,94,88,79,74,87,138,187,133,151,174,122,75,35,42,98,123,105,149,134,137,132,148,159,136,107,75,23,22,69,145,164,139,83,91,78,85,74,112,190,155,180,155,101,54,28,62,112,117,105,135,133,136,127,127,147,146,145,162,110,40,89,174,148,105,65,79,79,72,25,90,194,175,183,126,80,37,34,90,121,119,103,130,143,133,114,115,131,137,135,144,188,166,171,174,137,57,30,56,58,43,35,125,189,164,146,103,56,36,73,107,112,124,103,137,138,127,108,129,152,162,164,168,191,176,161,137,81,31,18,37,78,92,123,149,155,134,117,71,35,52,115,126,106,112,112,135,134,126,110,120,126,129,125,169,198,149,116,87,36,19,44,88,132,150,143,133,125,119,91,44,55,99,126,134,117,102,113,138,135,123,118,121,131,123,121,160,169,120,68,43,25,34,91,122,126,132,124,94,88,83,50,48,101,128,142,141,126,107,112,136,128,120,122,126,149,152,174,136,110,105,94,96,89,113,136,127,120,65,74,104,102,45,34,97,135,133,150,131,121,124,114,129,130,118,115,122,139,127,129,135,130,129,132,137,113,134,152,154,156,100,112,122,103,83,79,124,139,133,126,119,128,134,116,127,134,119,122,126,118,117,134,139,121,126,128,133,112,121,154,163,162,144,116,121,126,133,137,134,134,131,122,125,129,123,110,126,131,120,125,116,113,126,130,125,120,120,115,123,116,126,144,143,146,140,130,132,143,130,118,127,123,129,129,125,118,118,112,118,108,112,124,125,125,123,126,129,113,97,106,116,104,102,120,114,88,95,102,97,115,98,100,100,93,96,102,91,90,89,93,131,140,135,123,125,135,124,123,127,110,94,99,100,97,91,109,114,98,111,117,105,106,100,120,115,95,97,103,98,95,82,80,136,143,147,143,145,156,135,124,134,126,108,120,110,110,100,107,107,99,103,122,117,113,103,103,105,97,103,112,115,111,90,85,133,125,144,151,134,137,133,120,125,134,132,109,112,105,99,95,89,79,71,89,102,100,102,94,93,99,113,119,121,117,105,96,145,142,152,148,94,80,104,101,100,120,105,68,84,81,60,55,60,61,83,94,85,64,76,88,94,104,114,102,90,90,95,93,146,155,159,135,92,82,66,69,72,72,67,67,72,50,43,44,38,43,74,111,116,94,85,78,72,102,114,95,86,81,86,92,132,137,153,116,95,83,59,54,65,62,55,59,67,56,51,55,40,50,41,50,72,93,98,105,85,88,111,96,92,78,82,96,125,116,115,89,74,67,64,54,55,66,71,74,67,57,44,67,76,74,70,65,33,32,44,70,92,84,108,99,90,90,78,86,123,104,94,91,70,65,59,51,45,40,48,54,52,47,50,61,59,52,50,55,45,39,36,23,26,36,65,82,75,83,80,82,128,94,84,106,82,72,72,54,66,57,47,58,57,56,59,63,57,59,53,48,29,39,43,43,39,18,31,61,72,78,84,77,126,95,104,135,115,63,60,66,86,71,49,59,73,76,72,74,52,60,56,52,38,28,25,20,36,49,54,65,81,86,89,80,105,95,151,167,150,57,11,52,94,80,51,47,30,62,87,81,54,56,52,51,46,41,38,21,24,49,66,63,83,93,89,81,102,96,126,151,150,86,34,66,83,81,72,49,47,50,91,75,31,45,73,38,30,35,34,26,26,24,26,49,65,80,83,84,108,97,90,112,121,107,96,107,79,45,66,36,64,88,95,82,62,44,72,59,41,35,29,40,34,22,31,60,50,67,85,91,113,107,88,90,94,91,98,105,87,36,60,46,55,97,103,87,79,66,66,55,43,39,36,67,40,30,61,52,33,57,84,94,113,109,102,89,85,82,81,80,93,77,78,60,65,91,111,98,78,72,66,38,32,14,47,71,31,51,70,39,26,27,53,80,110,107,98,95,92,58,63,62,67,77,74,40,62,92,97,114,96,76,57,46,57,31,64,60,30,71,58,33,21,13,35,70,127,110,88,99,100,62,41,61,56,36,40,37,57,74,78,90,76,69,66,57,61,59,69,49,45,70,45,36,18,14,41,76,152,128,99,97,91,47,30,64,84,68,60,77,79,56,68,71,61,56,59,45,56,85,75,50,67,63,46,47,19,18,49,77,160,150,128,112,94,46,10,31,109,126,76,116,133,71,54,62,61,56,44,38,64,93,76,44,71,56,43,42,19,28,70,71,152,156,135,119,108,71,19,3,33,71,59,91,130,108,59,52,37,31,30,51,85,87,59,46,70,44,34,29,21,55,97,84,146,142,127,108,116,113,64,24,0,11,42,47,85,104,72,40,37,36,38,60,78,86,56,72,71,37,31,20,31,84,106,89,126,113,118,113,122,129,109,87,62,16,13,28,58,62,61,32,46,39,49,52,60,92,79,102,63,39,26,16,49,96,99,88,112,112,118,106,99,116,121,115,133,90,28,45,81,49,49,35,47,50,45,13,51,108,96,104,51,40,23,24,75,104,99,85,108,123,115,91,85,101,115,102,101,142,114,91,68,52,35,26,42,45,30,24,86,113,82,66,46,35,28,61,91,94,103,83,118,121,109,88,105,123,131,125,112,119,98,70,54,33,17,11,25,62,67,91,100,85,61,50,34,33,46,95,107,88,93,95,117,116,106,89,96,97,94,84,112,125,75,48,39,16,15,29,63,103,109,101,84,63,62,46,20,52,91,108,116,98,83,97,117,115,101,93,93,100,88,82,109,111,67,30,21,16,33,73,92,93,90,82,52,43,46,29,34,87,116,129,123,108,88,95,114,107,97,94,96,118,119,140,98,70,73,74,80,73,97,118,107,98,42,44,68,69,24,27,88,112,118,138,114,103,107,98,108,109,95,85,91,109,99,103,106,101,106,116,116,83,103,133,144,144,82,87,94,77,65,68,109,117,114,110,102,112,117,101,106,114,97,93,96,90,91,111,113,92,100,114,114,81,90,132,146,145,120,93,101,105,113,112,110,117,110,102,108,113,107,95,106,110,97,98,88,89,105,107,98,93,96,103,109,91,100,119,121,124,115,108,115,124,111,92,103,107,109,108,110,102,103,98],
       "public_key": "pk",
       "source": "",
       "type": "ciphertext"
     },
     ...
   ```

4. Now we can execute a command to prepare the data(encrypt input, generate keys etc.), upload it to the Data Layer, and run the application:

   ```bash
   $ ./fairmathd actor start-app 12 ./cifar-split-12-fhecfg.json --keys-dir=/path/to/store/keys --key-generator=/path/to/fairmath-cli --from <accountname> --ipfs 167.235.197.94:5001 --keyring-backend=test
   2024/11/25 10:35:38 application with id <12> has been started under process ID: <38>

   ```

If the execution is successful, you will receive a process ID for your run (e.g., `38` in this example). Additionally, all necessary keys and the cryptocontext will be generated and stored in the directory specified by the `--keys-dir` flag. The secret key and cryptocontext will be required later to decrypt the result.

Once the command completes, an updated configuration file named `./cifar-split-12-fhecfg.updated.json` will be created. For future runs, you can use this updated configuration to avoid re-generating keys. Simply modify the input section in the updated config, and the FHE computer will use the existing keys.

To prevent saving an updated configuration, pass the flag `--save-config=false`. This is especially useful if you are running the application with an already updated config to avoid generating redundant files like `cifar-split-12-fhecfg.updated.updated.json`.

To observe the execution of your app you can go to the [FHE computer terminal](https://computer.fairmath.xyz/dashboard) and run `orders` command

```bash
3os> orders 
```

<figure><img src="https://90165124-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmHu1qRdgJeHeQup4pe3R%2Fuploads%2Fk29TvumHQPLFppaCLaSC%2Fimage-2.png?alt=media&#x26;token=457ac124-d044-40ca-a187-31519e913740" alt=""><figcaption></figcaption></figure>

There is 3 orders related to the process `38`. Once all of related orders is in `done` status the process considered as finished and you are able to see result.

To see result of the execution you need your account address(you discovered it during step 2 of registration). Run `process` command in the [FHE computer termial](https://computer.fairmath.xyz/dashboard) and observe the

```bash
3os> process user=fairmath19wjlxxzef8nvlfr63xr9c8vkefmf54awtp8mk0
```

<figure><img src="https://90165124-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmHu1qRdgJeHeQup4pe3R%2Fuploads%2F9QDSEV7dcvWq58yUPBEj%2Fimage-3.png?alt=media&#x26;token=d47c3baf-0f81-4fe3-9a67-12b8f191e81a" alt=""><figcaption></figcaption></figure>

If everything is ok then Args column will contain ipfs link to the result of computation. In this case it is - `/ipfs/QmQ1LHHuu4TheWvKz1UBy5VXsLWr3EWPUgSsk4xHQRHLcu`. To download it paste the following address in your browser changing ipfs link to yours

`http://167.235.197.94:8080/ipfs/QmQ1LHHuu4TheWvKz1UBy5VXsLWr3EWPUgSsk4xHQRHLcu`

```json
{
    "vars": {
        "%arg0": {
            "basetype": "",
            "is_array": false,
            "is_secret": false,
            "storage": "ipfs",
            "value": "/home/gsm/.fairmath/fhevm-store/actor-1/.ipfs.QmXR5FDSupU6ZKxxkU95WcywhmAvykLZbNEqtcu2P3M43a"
        },
        "%arg2": {
            "basetype": "i32",
            "is_array": true,
            "is_secret": false,
            "storage": "ipfs",
            "value": "/home/gsm/.fairmath/fhevm-store/actor-1/.ipfs.QmfZhqPFDZmyK4rAxwDH5FTHeQGrJjDFbRJ1WDzW4qpF1P"
        },
        "%mul_key": {
            "basetype": "",
            "is_array": false,
            "is_secret": false,
            "storage": "ipfs",
            "value": "/home/gsm/.fairmath/fhevm-store/actor-1/.ipfs.QmT4xPQnAkZXR6PGDySaVZ4kBPbHdkVi2TqZgUZdREhfa7"
        },
        "pk": {
            "basetype": "",
            "is_array": false,
            "is_secret": false,
            "storage": "ipfs",
            "value": "/home/gsm/.fairmath/fhevm-store/actor-1/.ipfs.QmeA4jDooaoR4G2amHtnQKGpRD4eEdfVZSLjARDFF5HJq4"
        }
    }
}
```

Now you have a result, to decrypt it you can use `fairmath-cli`   with secret key and crypto context.

```
fairmath-cli decrypt --cc ./cryptocontext_name --key ./private_key_name --output ./decrypted.txt ./ciphertext_file
```
