Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

Commit ad6bfac

Browse files
poppyseedDevyuxizamajatZama
authored
docs: fix due to change in hardhat-templates README trivial encrypt casting (#642)
* docs: update README and MockZamaFHEVMConfig removed * docs: update typescript sample templates * docs: gas added * docs: resolve webpack error structure * docs: readme upgrade and tests * docs: mini change * docs: review * docs: added immutable * docs: types removed Declaring encrypted state variables * docs: add asxxoperation * docs: added asXXoperation * docs: mini change * docs: apply suggestions from code review Co-authored-by: yuxizama <157474013+yuxizama@users.noreply.github.com> * docs: mini changes * docs: mini changes * docs: mini change * docs: mini change Co-authored-by: yuxizama <157474013+yuxizama@users.noreply.github.com> * Update README.md Co-authored-by: Aurora Poppyseed <30662672+poppyseedDev@users.noreply.github.com> * Update README.md Co-authored-by: Aurora Poppyseed <30662672+poppyseedDev@users.noreply.github.com> * Update README.md Co-authored-by: Aurora Poppyseed <30662672+poppyseedDev@users.noreply.github.com> * Update README.md Co-authored-by: Aurora Poppyseed <30662672+poppyseedDev@users.noreply.github.com> * Update docs/fundamentals/asXXoperation.md Co-authored-by: Aurora Poppyseed <30662672+poppyseedDev@users.noreply.github.com> * Update docs/fundamentals/asXXoperation.md Co-authored-by: Aurora Poppyseed <30662672+poppyseedDev@users.noreply.github.com> * Update docs/fundamentals/asXXoperation.md Co-authored-by: Aurora Poppyseed <30662672+poppyseedDev@users.noreply.github.com> * Update docs/fundamentals/asXXoperation.md Co-authored-by: Aurora Poppyseed <30662672+poppyseedDev@users.noreply.github.com> * Update docs/fundamentals/asXXoperation.md Co-authored-by: Aurora Poppyseed <30662672+poppyseedDev@users.noreply.github.com> * docs: mini changes * docs: gitbook problems * docs: mini change * docs: change * docs: remove changes from tests * docs: mini update --------- Co-authored-by: yuxizama <157474013+yuxizama@users.noreply.github.com> Co-authored-by: jat <153528475+jatZama@users.noreply.github.com>
1 parent 8e326db commit ad6bfac

File tree

20 files changed

+614
-310
lines changed

20 files changed

+614
-310
lines changed

README.md

Lines changed: 56 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ _Learn more use cases in the [list of examples](https://docs.zama.ai/fhevm/tutor
8888

8989
### Installation
9090

91-
For now, fhEVM is implemented on evmos.
92-
9391
```bash
9492
# Using npm
9593
npm install fhevm
@@ -101,9 +99,6 @@ yarn add fhevm
10199
pnpm add fhevm
102100
```
103101

104-
_Find more details on implementation instructions in [this repository](https://github.com/zama-ai/fhevm-evmos)._
105-
<br></br>
106-
107102
### A Simple Example
108103

109104
```solidity
@@ -112,19 +107,25 @@ _Find more details on implementation instructions in [this repository](https://g
112107
pragma solidity ^0.8.24;
113108
114109
import "fhevm/lib/TFHE.sol";
110+
import "fhevm/config/ZamaFHEVMConfig.sol";
115111
116-
contract Counter {
117-
euint32 counter;
112+
contract Counter is SepoliaZamaFHEVMConfig {
113+
euint8 internal counter;
114+
115+
constructor() {
116+
counter = TFHE.asEuint8(0);
117+
TFHE.allowThis(counter);
118+
}
118119
119120
function add(einput valueInput, bytes calldata inputProof) public {
120-
euint32 value = TFHE.asEuint32(valueInput, inputProof);
121+
euint8 value = TFHE.asEuint8(valueInput, inputProof);
121122
counter = TFHE.add(counter, value);
122-
TFHE.allow(counter, address(this));
123+
TFHE.allowThis(counter);
123124
}
124125
}
125126
```
126127

127-
_More examples are available [here](https://github.com/zama-ai/fhevm/tree/main/examples)._
128+
_More examples are available [here](https://docs.zama.ai/fhevm/tutorials/see-all-tutorials)._
128129

129130
<p align="right">
130131
<a href="#about" > ↑ Back to top </a>
@@ -136,27 +137,61 @@ _More examples are available [here](https://github.com/zama-ai/fhevm/tree/main/e
136137
137138
## Resources
138139

139-
### White paper
140+
### **White Paper**
140141

141-
- [Confidential EVM Smart Contracts using Fully Homomorphic Encryption](https://github.com/zama-ai/fhevm/blob/main/fhevm-whitepaper-v2.pdf)
142-
<br></br>
142+
Gain insights into the technology powering fhEVM with our in-depth white paper:
143+
👉 [**Confidential EVM Smart Contracts using Fully Homomorphic Encryption**](https://github.com/zama-ai/fhevm/blob/main/fhevm-whitepaper-v2.pdf)
144+
145+
---
146+
147+
### **Demos and Tutorials**
148+
149+
Access a curated collection of demos and step-by-step tutorials to guide your development journey:
150+
🔗 [**Visit the Tutorials Page**](https://docs.zama.ai/fhevm/tutorials/see-all-tutorials)
151+
152+
---
153+
154+
### **Documentation**
155+
156+
Master `fhEVM` and build smart contracts using these resources:
143157

144-
### Demos and Tutorials
158+
- 📘 [**Comprehensive fhEVM Documentation**](https://docs.zama.ai/fhevm)
159+
Dive deep into Zama's detailed guide for utilizing the full potential of fhEVM.
145160

146-
For a comprehensive list of demos and tutorials, visit our [tutorials page](https://docs.zama.ai/fhevm/tutorials/see-all-tutorials).
161+
- 🤖 [**ZAMA Solidity Developer (Modified ChatGPT Model)**](https://chatgpt.com/g/g-67518aee3c708191b9f08d077a7d6fa1-zama-solidity-developer)
162+
Accelerate your smart contract development with AI-powered assistance.
147163

148-
### Documentation
164+
### **Development templates**
149165

150-
Full, comprehensive documentation is available here: [https://docs.zama.ai/fhevm](https://docs.zama.ai/fhevm).
166+
Start building faster with pre-configured templates tailored for various frameworks:
167+
168+
#### **Smart Contracts**
169+
170+
- 🔧 [**Hardhat Template**](https://github.com/zama-ai/fhevm-hardhat-template)
171+
Smart contracts testing and development <- **_primary entry point for developers wanting to develop smart contracts on fhEVM_**
172+
- 💻 [**fhEVM Contracts**](https://github.com/zama-ai/fhevm-contracts)
173+
Library of standardized fhEVM contracts.
174+
175+
#### **Frontend frameworks**
176+
177+
- 🌐 [**React.js Template**](https://github.com/zama-ai/fhevm-react-template)
178+
Simplify your FHE dApp development with a clean and optimized React.js template.
179+
-[**Next.js Template**](https://github.com/zama-ai/fhevm-next-template)
180+
Build server-rendered, scalable dApps with FHE support using this Next.js template.
181+
- 🖼️ [**Vue.js Template**](https://github.com/zama-ai/fhevm-vue-template)
182+
Create modular, responsive dApps with FHE capabilities using Vue.js.
183+
184+
---
185+
186+
### 🚀 **Kickstart Your Project Today!**
187+
188+
Leverage these templates to accelerate your development process and bring your ideas to life faster.
151189

152190
<p align="right">
153191
<a href="#about" > ↑ Back to top </a>
154192
</p>
155193

156-
### Blockchain Implementation
157-
158-
To support fhEVM in an EVM-based blockchain, the **fhevm-go** library can be used as it implements all the needed FHE functionalities.
159-
It is available here: [fhevm-go](https://github.com/zama-ai/fhevm-go)
194+
## Blockchain Implementation
160195

161196
To integrate fhevm-go into any EVM-based blockchain, you can follow the [Integration Guide](https://docs.zama.ai/fhevm-go/getting_started/integration).
162197

@@ -187,8 +222,6 @@ Files that are generated now (can be seen inside `codegen/main.ts`)
187222
```
188223
lib/Impl.sol
189224
lib/TFHE.sol
190-
mocks/Impl.sol
191-
mocks/TFHE.sol
192225
contracts/tests/TFHETestSuiteX.sol
193226
test/tfheOperations/tfheOperations.ts
194227
```
@@ -197,57 +230,11 @@ test/tfheOperations/tfheOperations.ts
197230
<a href="#about" > ↑ Back to top </a>
198231
</p>
199232

200-
#### Tests
201-
202-
The easiest way to understand how to write/dev smart contract and interact with them using **fhevmjs** is to read and explore the available tests in this repository.
203-
204-
##### Fast start
205-
206-
```bash
207-
# in one terminal
208-
npm run fhevm:start
209-
# in another terminal
210-
npm i
211-
cp .env.example .env
212-
npm run test:mock
213-
```
214-
215-
</details>
216-
217-
##### Run test on a real fhEVM
218-
219-
```bash
220-
npm run test -- --network sepolia
221-
```
222-
223233
#### Adding new operators
224234

225235
Operators can be defined as data inside `codegen/common.ts` file and code automatically generates solidity overloads.
226236
Test for overloads must be added (or the build doesn't pass) inside `codegen/overloadsTests.ts` file.
227237

228-
#### Mocked mode
229-
230-
The mocked mode allows faster testing and the ability to analyze coverage of the tests. In this mocked version, encrypted types are not really encrypted, and the tests are run on the original version of the EVM, on a local hardhat network instance. To run the tests in mocked mode, you can use directly the following command:
231-
232-
```bash
233-
npm run test:mock
234-
```
235-
236-
To analyze the coverage of the tests (in mocked mode necessarily, as this cannot be done on the real fhEVM node), you can use this command :
237-
238-
```bash
239-
npm run coverage:mock
240-
```
241-
242-
Then open the file `coverage/index.html`. You can see there which line or branch for each contract which has been covered or missed by your test suite. This allows increased security by pointing out missing branches not covered yet by the current tests.
243-
244-
> [!Note]
245-
> Due to intrinsic limitations of the original EVM, the mocked version differ in few corner cases from the real fhEVM, the main difference is the difference in gas prices for the FHE operations. This means that before deploying to production, developers still need to run the tests with the original fhEVM node, as a final check in non-mocked mode, with `npm run test`.
246-
247-
<p align="right">
248-
<a href="#about" > ↑ Back to top </a>
249-
</p>
250-
251238
### Citations
252239

253240
To cite fhEVM or the whitepaper in academic papers, please use the following entries:

docs/SUMMARY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [Configuration](fundamentals/configure.md)
2323
- [Supported types](fundamentals/types.md)
2424
- [Operations on encrypted types](fundamentals/operations.md)
25+
- [asEbool, asEuintXX, asEaddress and asEbytesXX operations](fundamentals/asEXXoperators.md)
2526
- [Access Control List](fundamentals/acl/README.md)
2627
- [ACL examples](fundamentals/acl/acl_examples.md)
2728
- [Encrypted Inputs](fundamentals/inputs.md)
@@ -38,6 +39,8 @@
3839
- [Branching in FHE](guides/conditions.md)
3940
- [Generate random numbers](guides/random.md)
4041
- [Error handling](guides/error_handling.md)
42+
- [Gas estimation](guides/gas.md)
43+
- [Debug decrypt](guides/debug_decrypt.md)
4144
- [Frontend](guides/frontend/README.md)
4245
- [Build a web application](guides/frontend/webapp.md)
4346
- [Build with Node](guides/frontend/node.md)

docs/fundamentals/acl/acl_examples.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ The ACL system allows you to define two types of permissions for accessing ciphe
3333

3434
```solidity
3535
import "fhevm/lib/TFHE.sol";
36-
import { MockZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
36+
import { SepoliaZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
3737
38-
contract SecretGiver is MockZamaFHEVMConfig {
38+
contract SecretGiver is SepoliaZamaFHEVMConfig {
3939
SecretStore public secretStore;
4040
4141
constructor() {
@@ -56,7 +56,7 @@ contract SecretGiver is MockZamaFHEVMConfig {
5656
```
5757

5858
```
59-
contract SecretStore is MockZamaFHEVMConfig {
59+
contract SecretStore is SepoliaZamaFHEVMConfig {
6060
euint16 public secretResult;
6161
6262
function storeSecret(euint16 callerSecret) public {
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# asEbool, asEuintXX, asEaddress and asEbytesXX operations
2+
3+
This documentation covers the `asEbool`, `asEuintXX`, `asEaddress` and `asEbytesXX` operations provided by the TFHE library for working with encrypted data in the fhEVM. These operations are essential for converting between plaintext and encrypted types, as well as handling encrypted inputs.
4+
5+
The operations can be categorized into three main use cases:
6+
7+
1. **Trivial encryption**: Converting plaintext values to encrypted types
8+
2. **Type casting**: Converting between different encrypted types
9+
3. **Input handling**: Processing encrypted inputs with proofs
10+
11+
## 1. Trivial encryption
12+
13+
Trivial encryption simply put is a plain text in a format of a ciphertext.
14+
15+
### Overview
16+
17+
Trivial encryption is the process of converting plaintext values into encrypted types (ciphertexts) compatible with TFHE operators. Although the data is in ciphertext format, it remains publicly visible on-chain, making it useful for operations between public and private values.
18+
19+
This type of casting involves converting plaintext (unencrypted) values into their encrypted equivalents, such as:
20+
21+
- `bool``ebool`
22+
- `uint``euintXX`
23+
- `bytes``ebytesXX`
24+
- `address``eaddress`
25+
26+
> **Note**: When doing trivial encryption, the data is made compatible with FHE operations but remains publicly visible on-chain unless explicitly encrypted.
27+
28+
#### **Example**
29+
30+
```solidity
31+
euint64 value64 = TFHE.asEuint64(7262); // Trivial encrypt a uint64
32+
ebool valueBool = TFHE.asEbool(true); // Trivial encrypt a boolean
33+
```
34+
35+
### Trivial encryption of `ebytesXX` types
36+
37+
The `TFHE.padToBytesXX` functions facilitate this trivial encryption process for byte arrays, ensuring compatibility with `ebytesXX` types. These functions:
38+
39+
- Pad the provided byte array to the appropriate length (`64`, `128`, or `256` bytes).
40+
- Prevent runtime errors caused by improperly sized input data.
41+
- Work seamlessly with `TFHE.asEbytesXX` for trivial encryption.
42+
43+
> **Important**: Trivial encryption does NOT provide any privacy guarantees. The input data remains fully visible on the blockchain. Only use trivial encryption when working with public values that need to interact with actual encrypted data.
44+
45+
#### Workflow
46+
47+
1. **Pad Input Data**:
48+
Use the `padToBytesXX` functions to ensure your byte array matches the size requirements.
49+
2. **Encrypt the Padded Data**:
50+
Use `TFHE.asEbytesXX` to encrypt the padded byte array into the corresponding encrypted type.
51+
3. **Grant Access**:
52+
Use `TFHE.allowThis` and `TFHE.allow`optionally, if you want to persist allowance for those variables for later use.
53+
54+
### Example: Trivial Encryption with `ebytesXX`
55+
56+
Below is an example demonstrating how to encrypt and manage `ebytes64`, `ebytes128`, and `ebytes256` types:
57+
58+
```solidity
59+
function trivialEncrypt() public {
60+
// Encrypt a 64-byte array
61+
ebytes64 yBytes64 = TFHE.asEbytes64(
62+
TFHE.padToBytes64(
63+
hex"19d179e0cc7e816dc944582ed4f5652f5951900098fc2e0a15a7ea4dc8cfa4e3b6c54beea5ee95e56b728762f659347ce1d4aa1b05fcc5"
64+
)
65+
);
66+
TFHE.allowThis(yBytes64);
67+
TFHE.allow(yBytes64, msg.sender);
68+
69+
// Encrypt a 128-byte array
70+
ebytes128 yBytes128 = TFHE.asEbytes128(
71+
TFHE.padToBytes128(
72+
hex"13e7819123de6e2870c7e83bb764508e22d7c3ab8a5aee6bdfb26355ef0d3f1977d651b83bf5f78634fa360aa14debdc3daa6a587b5c2fb1710ab4d6677e62a8577f2d9fecc190ad8b11c9f0a5ec3138b27da1f055437af8c90a9495dad230"
73+
)
74+
);
75+
TFHE.allowThis(yBytes128);
76+
TFHE.allow(yBytes128, msg.sender);
77+
78+
// Encrypt a 256-byte array
79+
ebytes256 yBytes256 = TFHE.asEbytes256(
80+
TFHE.padToBytes256(
81+
hex"d179e0cc7e816dc944582ed4f5652f5951900098fc2e0a15a7ea4dc8cfa4e3b6c54beea5ee95e56b728762f659347ce1d4aa1b05fcc513e7819123de6e2870c7e83bb764508e22d7c3ab8a5aee6bdfb26355ef0d3f1977d651b83bf5f78634fa360aa14debdc3daa6a587b5c2fb1710ab4d6677e62a8577f2d9fecc190ad8b11c9f0a5ec3138b27da1f055437af8c90a9495dad230"
82+
)
83+
);
84+
TFHE.allowThis(yBytes256);
85+
TFHE.allow(yBytes256, msg.sender);
86+
}
87+
```
88+
89+
## 2. Casting between encrypted types
90+
91+
This type of casting is used to reinterpret or convert one encrypted type into another. For example:
92+
93+
- `euint32``euint64`
94+
- `ebytes128``ebytes256`
95+
96+
Casting between encrypted types is often required when working with operations that demand specific sizes or precisions.
97+
98+
> **Important**: When casting between encrypted types:
99+
>
100+
> - Casting from smaller types to larger types (e.g. `euint32``euint64`) preserves all information
101+
> - Casting from larger types to smaller types (e.g. `euint64``euint32`) will truncate and lose information
102+
103+
The table below summarizes the available casting functions:
104+
105+
| From type | To type | Function |
106+
| --------- | -------- | ---------------- |
107+
| `euintX` | `euintX` | `TFHE.asEuintXX` |
108+
| `ebool` | `euintX` | `TFHE.asEuintXX` |
109+
| `euintX` | `ebool` | `TFHE.asEboolXX` |
110+
111+
{% hint style="info" %}
112+
Casting between encrypted types is efficient and often necessary when handling data with differing precision requirements.
113+
{% endhint %}
114+
115+
### **Workflow for encrypted types**
116+
117+
```solidity
118+
// Casting between encrypted types
119+
euint32 value32 = TFHE.asEuint32(value64); // Cast to euint32
120+
ebool valueBool = TFHE.asEbool(value32); // Cast to ebool
121+
```
122+
123+
## 3. Encrypted input
124+
125+
### Overview
126+
127+
Encrypted input casting is the process of interpreting a handle (ciphertext reference) and its proof as a specific encrypted type. This ensures the validity of the input before it is used in computations.
128+
129+
Encrypted inputs is in depth explained in the following section: [encrypted inputs](./inputs.md)
130+
131+
#### Example
132+
133+
```solidity
134+
euint64 encryptedValue = TFHE.asEuint64(einputHandle, inputProof); // Interpret einputHandle as euint64
135+
```
136+
137+
#### Details
138+
139+
Encrypted input casting validates:
140+
141+
1. The input handle references a valid ciphertext.
142+
2. The accompanying proof matches the expected type.
143+
144+
For more information, see the [Encrypetd inputs documentation](./inputs.md)
145+
146+
## Overall operation summary
147+
148+
| Casting Type | Function | Input Type | Output Type |
149+
| ------------------------ | ----------------------- | ----------------------- | ----------- |
150+
| Trivial encryption | `TFHE.asEuintXX(x)` | `uintX` | `euintX` |
151+
| | `TFHE.asEbool(x)` | `bool` | `ebool` |
152+
| | `TFHE.asEbytesXX(x)` | `bytesXX` | `ebytesXX` |
153+
| | `TFHE.asEaddress(x)` | `address` | `eaddress` |
154+
| Conversion between types | `TFHE.asEuintXX(x)` | `euintXX`/`ebool` | `euintYY` |
155+
| | `TFHE.asEbool(x)` | `euintXX` | `ebool` |
156+
| Encrypted input | `TFHE.asEuintXX(x, y)` | `einput`, `bytes` proof | `euintX` |
157+
| | `TFHE.asEbool(x, y)` | `einput`,`bytes` proof | `ebool` |
158+
| | `TFHE.asEbytesXX(x, y)` | `einput`,`bytes` proof | `ebytesXX` |
159+
| | `TFHE.asEaddress(x, y)` | `einput`, `bytes` proof | `eaddress` |

0 commit comments

Comments
 (0)