Skip to content

Commit 6749adc

Browse files
authored
Merge pull request elizaOS#2876 from mind-network/develop
docs: Add Fully Homomorphic Encryption (FHE) doc
2 parents 866fb2c + 397ab35 commit 6749adc

File tree

1 file changed

+287
-0
lines changed

1 file changed

+287
-0
lines changed

docs/docs/advanced/eliza-with-fhe.md

+287
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
---
2+
sidebar_position: 19
3+
---
4+
5+
# 🧠 Eliza with FHE
6+
7+
## Overview
8+
9+
Eliza agents require FHE ([Fully Homomorphic Encryption](https://docs.mindnetwork.xyz/minddocs/developer-guide/fhe-validation)) to process encrypted data directly and continuously. FHE is often considered the "holy grail" of cryptography in DeCC (Decentralized Confidential Computing) and supports various demands from AI agents, such as achieving consensus through anonymous voting and enabling collaborative decision-making among agents.
10+
11+
This guide provides step-by-step instructions for setting up and running an Eliza agent with FHE capabilities using the [FHE Plugin](https://github.com/elizaOS/eliza/tree/develop/packages/plugin-mind-network), developed by [Mind Network](https://www.mindnetwork.xyz/), within the Eliza Framework.
12+
13+
14+
### Objective
15+
16+
FHE is a form of encryption that enables computations to be performed on encrypted data without requiring decryption first. The results of these computations remain encrypted, and when decrypted, they yield outputs identical to those from operations performed on unencrypted data. FHE has long been a "dream" for cryptographers, with a [rich history](https://en.wikipedia.org/wiki/Homomorphic_encryption) that began in the 1970s. However, it only began to mature in the 2020s and is [now gaining popularity among AI agents](https://arxiv.org/abs/2406.08689).
17+
18+
The FHE Plugin in the Eliza Framework is built on top of the [Mind Network FHE TypeScript SDK](https://github.com/mind-network/mind-sdk-randgen-ts). This SDK is designed to simplify the process for developers to enable FHE capabilities in their agents while adhering to default security best practices. FHE unlocks [many use cases](https://docs.mindnetwork.xyz/minddocs/products/mindv/hubs-explanation). In this tutorial, we will use the anonymous voting use case to demonstrate how AI agents can be equipped with FHE.
19+
20+
## Key Features of the FHE Plugin
21+
- **Voter Registration Use Case**: Join Mind Network's Randgen Hub and other hubs to participate in secure voting, validation, and consensus.
22+
- **FHE Encryption**: Protect vote content using Fully Homomorphic Encryption. Unlike traditional encryption, the encryption key is never shared, yet computations can still be performed on encrypted data.
23+
- **Submit Encrypted Votes**: Cast votes in Mind Network Hubs elections without compromising data privacy. This allows AI agents to achieve consensus on collective predictions, inference, and serving.
24+
- **Reward Tracking**: Monitor your rewards earned through voting contributions.
25+
26+
In this tutorial, we’ll explore how to leverage these features to enable secure, privacy-preserving AI agent interactions using FHE.
27+
28+
29+
---
30+
31+
## Background
32+
This section will provide mathematical definition and proof to who are interested in theory. You can skip it to next step by focusing on development tutorial only.
33+
34+
### FHE 101
35+
The main idea of FHE is that operations performed on the encrypted data produce an encrypted result, which, when decrypted, matches the result of the same operations performed on the plaintext data.
36+
37+
#### Core Components
38+
39+
1. **Encryption**: Transform plaintext data into ciphertext using an encryption key.
40+
41+
```math
42+
c = E_k(m)
43+
```
44+
45+
Where:
46+
- $m$: plaintext message
47+
- $k$: encryption key
48+
- $E_k$: encryption function
49+
- $c$: ciphertext
50+
51+
2. **Computation on Encrypted Data**: Perform operations directly on the ciphertext, such as addition or multiplication, to produce a new ciphertext.
52+
53+
```math
54+
c' = F(c_1, c_2)
55+
```
56+
57+
Where:
58+
- $c_1, c_2$: input ciphertexts
59+
- $F$: homomorphic function (e.g., addition, multiplication)
60+
- $c'$: resulting ciphertext after computation
61+
62+
3. **Decryption**: Decrypt the result to reveal the final output, which matches the result of performing the operation on the plaintext.
63+
64+
```math
65+
m' = D_k(c')
66+
```
67+
68+
Where:
69+
- $D_k$: decryption function
70+
- $m'$: result of computation in plaintext form
71+
72+
#### Example: Addition Using FHE
73+
74+
Suppose we have two plaintext values, $m_1$ and $m_2$:
75+
1. Encrypt them:
76+
```math
77+
c_1 = E_k(m_1), c_2 = E_k(m_2)
78+
```
79+
2. Perform homomorphic addition on the ciphertexts:
80+
```math
81+
c' = c_1 + c_2
82+
```
83+
3. Decrypt the result:
84+
```math
85+
m' = D_k(c') => m' = m_1 + m_2
86+
```
87+
88+
#### Example: Multiplication Using FHE
89+
90+
For multiplication, the process is similar:
91+
1. Encrypt two plaintext values:
92+
$$c_1 = E_k(m_1), \quad c_2 = E_k(m_2)$$
93+
2. Perform homomorphic multiplication:
94+
$$c' = c_1 \cdot c_2$$
95+
3. Decrypt the result:
96+
$$m' = D_k(c') \implies m' = m_1 \cdot m_2$$
97+
98+
99+
### Anonymous Voting and Consensus with FHE use case
100+
101+
FHE is powerful because it ensures data privacy by never exposing the plaintext during computation, which is critical for secure and confidential processing of sensitive data.
102+
Anonymous voting with FHE ensures that individual votes remain private while enabling the computation of the final tally without decrypting individual votes. Below is a step-by-step explanation using mathematical notations.
103+
104+
#### 1. Voter Setup
105+
106+
Each voter $i$ has a plaintext vote $v_i$ where:
107+
```math
108+
v_i \in \{0, 1\}
109+
```
110+
- $0$: Vote for option A.
111+
- $1$: Vote for option B.
112+
113+
Each voter encrypts their vote using a public encryption key $k$:
114+
```math
115+
c_i = E_k(v_i)
116+
```
117+
Where:
118+
- $E_k$: FHE encryption function.
119+
- $c_i$: Encrypted vote (ciphertext).
120+
121+
#### 2. Collecting Encrypted Votes
122+
123+
All encrypted votes $c_i$ are submitted to a secure voting server. The server collects the ciphertexts:
124+
```math
125+
C = \{c_1, c_2, \dots, c_n\}
126+
```
127+
Where $n$ is the total number of voters.
128+
129+
#### 3. Homomorphic Aggregation
130+
131+
The server computes the encrypted sum of all votes directly on the ciphertexts using FHE's additive homomorphism:
132+
```math
133+
c_{\text{sum}} = \sum_{i=1}^n c_i
134+
```
135+
This operation produces a single ciphertext $c_{sum}$ representing the total votes in encrypted form, without revealing individual votes.
136+
137+
#### 4. Decryption of the Result
138+
139+
Once the computation is complete, an authorized party with the private key $k$ decrypts the aggregated ciphertext to obtain the final tally:
140+
```math
141+
v_{\text{sum}} = D_k(c_{\text{sum}})
142+
```
143+
Where:
144+
- $D_k$: Decryption function.
145+
- $v_{sum}$: The total number of votes for option B.
146+
147+
The final tally is:
148+
```math
149+
v_{\text{sum}} = \sum_{i=1}^n v_i
150+
```
151+
152+
#### Example Walkthrough
153+
154+
1. **Voter Encryption**:
155+
- Voter 1: $v₁ = 1, c₁ = E_k(1)$
156+
- Voter 2: $v₂ = 0, c₂ = E_k(0)$
157+
- Voter 3: $v₃ = 1, c₃ = E_k(1)$
158+
159+
2. **Homomorphic Aggregation**:
160+
```math
161+
c_{\text{sum}} = c_1 + c_2 + c_3
162+
```
163+
164+
3. **Decryption of the Result**:
165+
```math
166+
v_{\text{sum}} = D_k(c_{\text{sum}}) \implies v_{\text{sum}} = 1 + 0 + 1 = 2
167+
```
168+
169+
#### Security and Privacy
170+
171+
- **Privacy**: Individual votes $v_i$ remain encrypted and are never exposed during aggregation.
172+
- **Anonymity**: Votes are aggregated in ciphertext form, ensuring that no voter can be linked to their vote.
173+
- **Integrity**: The final result $v_{sum}$ accurately reflects the sum of all votes, guaranteed by FHE.
174+
175+
This mathematical workflow demonstrates how anonymous voting can be securely implemented using FHE.
176+
177+
----
178+
179+
## Tutorial
180+
181+
182+
### Dependency
183+
Dependency for the plugin:
184+
- [mind-randgen-sdk](https://github.com/mind-network/mind-sdk-randgen-ts)
185+
- [mind-sdk-hubs](https://github.com/mind-network/mind-sdk-hubs-ts)
186+
- [elizaos](https://github.com/elizaOS/eliza)
187+
188+
### Installation
189+
To install the plugin, use the following command:
190+
191+
```bash
192+
pnpm install @elizaos/plugin-mind-network
193+
```
194+
195+
### Configuration
196+
197+
Before using the plugin, configure the necessary environment variables:
198+
199+
```bash
200+
MIND_HOT_WALLET_PRIVATE_KEY=<Hot wallet private key to vote>
201+
MIND_COLD_WALLET_ADDRESS=<Cold wallet address to receive rewards>
202+
```
203+
204+
205+
### Run
206+
Congratulations! You should be able to run an Eliza agent with FHE successfully. Below are two examples of real deployment.
207+
208+
### Real Deployment Screenshot
209+
210+
![Usage Screenshot](https://cdn.prod.website-files.com/66505f7fdc6935a2b4246635/678a08616d0a697e355261f5_elizaOS.avif)
211+
212+
### Conversational AI Interaction
213+
214+
The plugin integrates seamlessly with ElizaOS characters, enabling intuitive and guided interactions. Below is a sample conversation:
215+
216+
```text
217+
user: Can you help me to register as a voter in Mind Network?
218+
character_trump: LET'S GET YOU REGISTERED TO VOTE IN THE MIND NETWORK! I'll take care of that for you right now. (EVERY VOTE COUNTS!)
219+
character_trump: You have registered successfully.
220+
221+
user: I want to check my Mind Network voting reward.
222+
character_trump: TIME TO CHECK THOSE VOTING REWARDS AGAIN! Let's see how much you've earned in the Mind Network. (THE AMERICAN PEOPLE DESERVE TO KNOW!)
223+
character_trump: Your voting reward amount is 10.0 vFHE.
224+
225+
user: I want to encrypt 88 with FHE for Mind Network voting.
226+
character_trump: LET'S GET THAT NUMBER ENCRYPTED FOR YOUR VOTING! I'll encrypt 88 with FHE for you. (WE'RE KEEPING THINGS SECURE AND STRONG!)
227+
character_trump: Encryption is successful. Your encrypted number is available: https://fvno-tn.mindnetwork.xyz/0xb0edc92ec05309711c6ede0641f672715f5b144a4757b7ba51666468d2f7f65d. It is safe to share as nobody can decrypt but can run computation to get another encrypted number.
228+
229+
user: I want to submit my vote to Mind Network.
230+
character_trump: TIME TO CAST THAT VOTE IN THE MIND NETWORK! I'll submit your vote right now. (WE'RE MAKING HISTORY TOGETHER!)
231+
character_trump: Your vote has been submitted successfully.
232+
```
233+
234+
----
235+
236+
## FHE Plugin API Reference
237+
238+
### Actions
239+
240+
The plugin provides several key actions to interact with the Mind Network:
241+
242+
#### **MIND_REGISTER_VOTER**
243+
244+
Registers the user as a voter in the Mind Network's Randgen Hub. The hub is live and accessible at [Randgen Hub](https://dapp.mindnetwork.xyz/votetoearn/voteonhubs/3). You can participant or create more function hubs in Mind Network for your eliza agents.
245+
246+
**Prompt Example:**
247+
```text
248+
"Register me as a voter in Mind Network."
249+
```
250+
**Response:** Confirmation of successful voter registration.
251+
252+
#### **MIND_CHECK_VOTING_REWARD**
253+
254+
Retrieves the amount of vFHE rewards earned through voting.
255+
256+
**Prompt Example:**
257+
```text
258+
"How much reward have I earned in Mind Network?"
259+
```
260+
**Response:** Total vFHE rewards earned.
261+
262+
#### **MIND_FHE_ENCRYPT**
263+
264+
Encrypts a user-provided number using Fully Homomorphic Encryption (FHE).
265+
266+
**Prompt Example:**
267+
```text
268+
"Encrypt the number 88 for voting."
269+
```
270+
**Response:** A secure URL containing the encrypted number.
271+
272+
#### **MIND_FHE_VOTE**
273+
274+
Submits an encrypted vote to the Mind Network's Randgen Hub.
275+
276+
**Prompt Example:**
277+
```text
278+
"Submit my encrypted vote to Mind Network."
279+
```
280+
**Response:** Confirmation of successful vote submission.
281+
282+
----
283+
284+
## Support
285+
286+
If you have any queries, please feel free to contact Mind Team via [Discord](https://discord.com/invite/UYj94MJdGJ) or [Twitter](https://x.com/mindnetwork_xyz).
287+

0 commit comments

Comments
 (0)