> For the complete documentation index, see [llms.txt](https://ciferai.gitbook.io/fhe/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ciferai.gitbook.io/fhe/6.-federated-learning-with-encrypted-assets.md).

# 6. Federated Learning with Encrypted Assets

This section demonstrates typical privacy-preserving model training scenarios using Fully Homomorphic Encryption (FHE) via the `cifer securetrain` CLI.

Here are example workflows between two parties, Alice and Bob:

* Alice is the **Data Owner**
* Bob is the **Project Owner** who trains and tunes the model using Alice’s encrypted data

## Scenario 1: Compute on Encrypted Data on Project Owner’s Device <a href="#scenario-1-compute-on-encrypted-data-on-project-ow" id="scenario-1-compute-on-encrypted-data-on-project-ow"></a>

In this workflow, Bob (Project Owner) trains a model directly on Alice’s encrypted data without accessing the plaintext.

{% stepper %}
{% step %}

### Data Owner Encrypts Dataset and Generates Key Pair

{% code title="bash" overflow="wrap" %}

```bash
cifer securetrain encrypt-dataset \
  --dataset ./alice/prj1_alice_data.npz \
  --output ./alice/encrypted_prj1_alice_data.enc
```

{% endcode %}

This creates:

* **Encrypted dataset:** `./alice/encrypted_prj1_alice_data.enc`
* **Public key:** `./keys/public.key` (to share with Bob)
* **Private key:** `./keys/private.key` (kept securely by Alice)

> **Note:** As described in [**Step 5: Encryption**](/fhe/5.-encryption.md), keys are stored by default in the `./keys/` directory.

{% hint style="info" %}
**Recommendation:** Rename keys to reflect data owner or project ID for clarity in collaboration, e.g.:

```
public.key → prj1_alice_public.key
private.key → prj1_alice_private.key
```

{% endhint %}
{% endstep %}

{% step %}

### Data Owner Shares Encrypted Data and Public Key with Project Owner

Alice provides Bob with:

* `./alice/encrypted_prj1_alice_data.enc`
* `./keys/prj1_alice_public.key`
  {% endstep %}

{% step %}

### Project Owner Stores Encrypted Data and Public Key

Bob places:

* **Encrypted dataset** in `./alice/`
* **Public key** in `./keys/`
  {% endstep %}

{% step %}

### Project Owner Trains Model on Encrypted Data

{% code title="bash" overflow="wrap" %}

```bash
cifer securetrain train \
  --encrypted-data ./alice/encrypted_prj1_alice_data.enc \
  --public-key ./keys/prj1_alice_public.key \
  --output-model ./bob/prj1_trained_model.h5
```

{% endcode %}

Output:

* **Trained model:** `./bob/prj1_trained_model.h5`

Training accuracy example: 99.93% (0.9993)
{% endstep %}
{% endstepper %}

## Scenario 2: Federated Learning with Encrypted Model on Data Owner’s Device <a href="#scenario-2-federated-learning-with-encrypted-model" id="scenario-2-federated-learning-with-encrypted-model"></a>

In this scenario, Bob encrypts a model and sends it to Alice, who retrains the model on her encrypted data using Federated Learning principles.

{% stepper %}
{% step %}

### Project Owner Encrypts Model and Generates Key Pair

{% code title="bash" overflow="wrap" %}

```bash
cifer securetrain encrypt-model \
  --model ./bob/prj2_model.h5 \
  --output ./bob/encrypted_prj2_model.enc
```

{% endcode %}

This creates:

* **Encrypted model:** `./bob/encrypted_prj2_model.enc`
* **Public key:** `./keys/public.key` (to share with Alice)
* **Private key:** `./keys/private.key` (kept securely by Bob)

> **Note:** As described in [**Step 5: Encryption**](/fhe/5.-encryption.md), keys are stored by default in the `./keys/` directory.

{% hint style="info" %}
**Recommendation:** Rename keys to reflect model owner or project ID for clarity in collaboration, e.g.:

```
public.key → prj2_bob_public.key
private.key → prj2_bob_private.key
```

{% endhint %}
{% endstep %}

{% step %}

### Project Owner Shares Encrypted Model and Public Key with Data Owner

Bob provides Alice with:

* `./bob/encrypted_prj2_model.enc`
* `./keys/prj2_bob_public.key`
  {% endstep %}

{% step %}

### Data Owner Stores Encrypted Model and Public Key

Alice places:

* **Encrypted model** in `./bob/`
* **Public key** in `./keys/`
  {% endstep %}

{% step %}

### Data Owner Encrypts Own Dataset and Generates Key Pair

{% code title="bash" overflow="wrap" %}

```bash
cifer securetrain encrypt-dataset \
  --dataset ./alice/prj2_alice_data.npz \
  --output ./alice/encrypted_prj2_alice_data.enc
```

{% endcode %}
{% endstep %}

{% step %}

### Data Owner Trains Model on Encrypted Dataset Using Public Key from Project Owner

{% code title="bash" overflow="wrap" %}

```bash
cifer securetrain train \
  --encrypted-data ./alice/encrypted_prj2_alice_data.enc \
  --public-key ./keys/prj2_bob_public.key \
  --output-model ./alice/prj2_trained_model.h5
```

{% endcode %}

Output:

* **Retrained model:** `./alice/prj2_trained_model.h5`

Training accuracy example: 99.93% (0.9993)

Alice may send this updated model back to Bob for further refinement.
{% endstep %}

{% step %}

### Data Owner Encrypts the Updated Model Before Returning

For additional security, Alice encrypts the updated model:

{% code title="bash" overflow="wrap" %}

```bash
cifer securetrain encrypt-model \
  --model ./alice/prj2_trained_model.h5 \
  --output ./alice/encrypted_prj2_trained_model.enc
```

{% endcode %}

Alice sends:

* Encrypted updated model: `encrypted_prj2_trained_model.enc`
* Her private key securely via a separate channel<br>
  {% endstep %}

{% step %}

### Project Owner Decrypts the Final Model

{% code title="bash" overflow="wrap" %}

```bash
cifer securetrain decrypt-model \
  --input-model ./bob/encrypted_prj2_trained_model.enc \
  --private-key ./keys/alice_private.key \
  --output-model ./bob/final_model_decrypted.h5
```

{% endcode %}

The decrypted model is saved as: `./bob/final_model_decrypted.h5` and ready for deployment or further use.
{% endstep %}
{% endstepper %}

This workflow highlights how **Fully Homomorphic Encryption (FHE)** enables secure federated learning by allowing computation on encrypted datasets and models without exposing sensitive data. By carefully managing encryption keys and workflow roles, Data Owners can protect privacy while Project Owners can train and tune models effectively.

Using the `cifer securetrain` CLI, teams can implement these privacy-preserving training workflows in a practical and scalable way. The separation of encrypted data, keys, and model files maintains strong security guarantees while providing flexibility for collaborative machine learning projects.
