|
| 1 | +--- |
| 2 | +id: index |
| 3 | +title: Hands-on fhEVM |
| 4 | +description: -- |
| 5 | +slug: /develop/exercise/hands-on-fhevm |
| 6 | +hide_table_of_contents: false |
| 7 | +sidebar_position: 1 |
| 8 | +--- |
| 9 | + |
| 10 | +# Hands-on fhEVM |
| 11 | + |
| 12 | +This hands-on guide will walk you through **understanding, setting up, and working with fhEVM** to develop encrypted smart contracts and test them on fhEVM network. |
| 13 | + |
| 14 | +### What You’ll Learn |
| 15 | + |
| 16 | +- Basics of fhEVM and its architecture. |
| 17 | +- Setting up a local fhEVM development environment. |
| 18 | +- Writing, compiling, and deploying FHE-enabled Solidity contracts. |
| 19 | +- Testing encrypted contract interactions. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## **1. Understanding fhEVM** |
| 24 | + |
| 25 | +fhEVM is a **privacy-first smart contract platform** that allows computations on encrypted data **without decryption**. This ensures **on-chain privacy** while maintaining **verifiability and interoperability**. |
| 26 | + |
| 27 | +### **Core Principles** |
| 28 | + |
| 29 | +1. **Security**: No compromise on blockchain security. |
| 30 | +2. **Public Verifiability**: Confidential yet verifiable computations. |
| 31 | +3. **Developer-Friendly**: Familiar Solidity tools. |
| 32 | +4. **Composability**: Interoperable confidential and public contracts. |
| 33 | + |
| 34 | +### **Key Features** |
| 35 | + |
| 36 | +- **Encrypted Data Types**: `ebool`, `euintX`, `eaddress`, `ebytesX`, etc. |
| 37 | +- **Homomorphic Computation**: Arithmetic on encrypted numbers. |
| 38 | +- **Confidential Transactions**: Data remains encrypted even on-chain. |
| 39 | +- **EVM-Compatible**: Supports Solidity with added FHE functionalities. |
| 40 | + |
| 41 | +### **Encrypted Data Types** |
| 42 | + |
| 43 | +fhEVM introduces special encrypted types for secure computations: |
| 44 | + |
| 45 | +- **Booleans**: `ebool` |
| 46 | +- **Integers**: `euint4`, `euint8`, ... `euint256` |
| 47 | +- **Addresses**: `eaddress` |
| 48 | +- **Bytes**: `ebytes64`, `ebytes128`, `ebytes256` |
| 49 | +- **Inputs**: `einput` |
| 50 | + |
| 51 | +### **Type Casting** |
| 52 | + |
| 53 | +fhEVM provides utility functions to convert between encrypted types: |
| 54 | + |
| 55 | +- `TFHE.asEbool(value)`: Converts a boolean to an encrypted boolean. |
| 56 | +- `TFHE.asEuintX(value)`: Converts an integer to an encrypted integer. |
| 57 | +- `TFHE.asEaddress(value)`: Converts an address to an encrypted address. |
| 58 | + |
| 59 | +fhEVM empowers secure and efficient computation on encrypted data while preserving blockchain functionality. |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## 2. **fhEVM Architecture** |
| 64 | + |
| 65 | +### **Core Components** |
| 66 | + |
| 67 | +fhEVM consists of several **interdependent components**: |
| 68 | + |
| 69 | +- **Smart Contracts**: Interact with encrypted data to execute logic securely on the blockchain. |
| 70 | +- **fhEVM-native**: On-chain processing that handles homomorphic computations on ciphertexts using evaluation keys. |
| 71 | +- **Key Management System (KMS)**: Safeguards private keys for decryption and re-encryption processes. |
| 72 | +- **Gateway**: Acts as a bridge between the blockchain and the KMS, enabling secure operations like decryption and re-encryption. |
| 73 | +- **Connector**: A service that connects the gateway to the KMS. |
| 74 | +- **Gateway Store**: A service that stores ciphertexts. |
| 75 | +- **fhEVM.js Library**: Facilitates client-side interactions such as encryption, decryption, and re-encryption. |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +### **End-to-End Confidentiality** |
| 80 | + |
| 81 | +fhEVM ensures that: |
| 82 | + |
| 83 | +- **Plaintext data is never exposed** during processing or transmission. |
| 84 | +- **Homomorphic computations** enable complex operations on encrypted data. |
| 85 | +- **Secure re-encryption** allows sharing and interoperability without compromising privacy. |
| 86 | + |
| 87 | +This **tightly integrated architecture** supports **decentralized applications (dApps)** requiring privacy-preserving operations, paving the way for **more secure and trustless systems**. |
| 88 | + |
| 89 | +For more detailed insights, you can refer to the **[official documentation by Zama](https://docs.zama.ai/fhevm)**. |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +### **What's Next?** |
| 94 | + |
| 95 | +Now that you understand the **fhEVM architecture**, let's set up an **fhEVM network**. |
| 96 | +➡ Move on to **[Network Setup](./setting-up-network.md)** to learn how to proceed. |
| 97 | + |
| 98 | + |
| 99 | +<!-- |
| 100 | +## **3. Setting Up Your Development Environment** |
| 101 | +
|
| 102 | +Before writing encrypted contracts, install the necessary tools. |
| 103 | +
|
| 104 | +### **Prerequisites** |
| 105 | +
|
| 106 | +✔ **Go** (`v1.23.x` or later) |
| 107 | +✔ **Docker** (`v26.x.x` or later) |
| 108 | +✔ **Node.js** (`v20.x` or later) |
| 109 | +
|
| 110 | +📖 **[Full Setup Guide](./preparation.md)** |
| 111 | +
|
| 112 | +--- |
| 113 | +
|
| 114 | +## **4. Deploying fhEVM Network** |
| 115 | +
|
| 116 | +We will **set up a local fhEVM test network** using Docker. |
| 117 | +
|
| 118 | +### **Start the Network** |
| 119 | +
|
| 120 | +```bash |
| 121 | +make run-full |
| 122 | +``` |
| 123 | +
|
| 124 | +### **Fund Accounts** |
| 125 | +
|
| 126 | +```bash |
| 127 | +npm run fund |
| 128 | +``` |
| 129 | +
|
| 130 | +### **Deploy Core Contracts** |
| 131 | +
|
| 132 | +```bash |
| 133 | +npm run core |
| 134 | +``` |
| 135 | +
|
| 136 | +📖 **[Full Network Setup Guide](./setting-up-network.md)** |
| 137 | +
|
| 138 | +--- |
| 139 | +
|
| 140 | +## **5. Writing and Deploying Encrypted Smart Contracts** |
| 141 | +
|
| 142 | +fhEVM supports **Fully Homomorphic Encryption (FHE)** within Solidity contracts. |
| 143 | +
|
| 144 | +### **Key Contract Operations** |
| 145 | +
|
| 146 | +✔ **EncryptedERC20**: Secure token minting, transfer, and approvals. |
| 147 | +✔ **Homomorphic Computation**: Arithmetic on encrypted balances. |
| 148 | +✔ **Decryption via Gateway**: Interact with KMS securely. |
| 149 | +
|
| 150 | +📖 **[Writing Smart Contracts](./writing-smart-contract.md)** |
| 151 | +📖 **[Deploying Smart Contracts](./compiling-and-deploying.md)** |
| 152 | +
|
| 153 | +--- |
| 154 | +
|
| 155 | +## **6. Testing Encrypted Contracts** |
| 156 | +
|
| 157 | +To validate your contracts, write **JavaScript-based tests** using `ethers.js`. |
| 158 | +
|
| 159 | +### **Example Test Cases** |
| 160 | +
|
| 161 | +✔ **Minting Tokens** |
| 162 | +✔ **Encrypted Transfers** |
| 163 | +✔ **Encrypted Approvals** |
| 164 | +✔ **Balance Decryption** |
| 165 | +
|
| 166 | +📖 **[Writing Test Files](./writing-contract-test-files.md)** |
| 167 | +
|
| 168 | +--- |
| 169 | +
|
| 170 | +## **7. Conclusion** |
| 171 | +
|
| 172 | +By completing this hands-on guide, you now understand: |
| 173 | +
|
| 174 | +- **How fhEVM works** and how it enables privacy in smart contracts. |
| 175 | +- **How to set up and deploy an encrypted EVM-compatible network**. |
| 176 | +- **How to write and test FHE-enabled Solidity contracts**. |
| 177 | +
|
| 178 | +### **Next Steps** |
| 179 | +
|
| 180 | +🔹 Explore **advanced encrypted contract interactions**. |
| 181 | +🔹 Contribute to **fhEVM projects and research**. |
| 182 | +🔹 Learn more from **[Zama’s Official Documentation](https://docs.zama.ai/fhevm)**. |
| 183 | +
|
| 184 | +📖 **[Final Thoughts & Summary](./08-conclusion.md)** |
| 185 | +
|
| 186 | +--- |
| 187 | +
|
| 188 | +## **🚀 Ready to Build?** |
| 189 | +
|
| 190 | +You’re now equipped with the **fundamentals of fhEVM**. Start experimenting with your own encrypted smart contracts and push the boundaries of **on-chain privacy**! --> |
0 commit comments