Skip to content

Commit

Permalink
docs: fix due to change in hardhat-templates README trivial encrypt c…
Browse files Browse the repository at this point in the history
…asting (#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 <[email protected]>

* docs: mini changes

* docs: mini changes

* docs: mini change

* docs: mini change

Co-authored-by: yuxizama <[email protected]>

* Update README.md

Co-authored-by: Aurora Poppyseed <[email protected]>

* Update README.md

Co-authored-by: Aurora Poppyseed <[email protected]>

* Update README.md

Co-authored-by: Aurora Poppyseed <[email protected]>

* Update README.md

Co-authored-by: Aurora Poppyseed <[email protected]>

* Update docs/fundamentals/asXXoperation.md

Co-authored-by: Aurora Poppyseed <[email protected]>

* Update docs/fundamentals/asXXoperation.md

Co-authored-by: Aurora Poppyseed <[email protected]>

* Update docs/fundamentals/asXXoperation.md

Co-authored-by: Aurora Poppyseed <[email protected]>

* Update docs/fundamentals/asXXoperation.md

Co-authored-by: Aurora Poppyseed <[email protected]>

* Update docs/fundamentals/asXXoperation.md

Co-authored-by: Aurora Poppyseed <[email protected]>

* docs: mini changes

* docs: gitbook problems

* docs: mini change

* docs: change

* docs: remove changes from tests

* docs: mini update

---------

Co-authored-by: yuxizama <[email protected]>
Co-authored-by: jat <[email protected]>
  • Loading branch information
3 people authored Dec 11, 2024
1 parent 8e326db commit ad6bfac
Show file tree
Hide file tree
Showing 20 changed files with 614 additions and 310 deletions.
125 changes: 56 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ _Learn more use cases in the [list of examples](https://docs.zama.ai/fhevm/tutor

### Installation

For now, fhEVM is implemented on evmos.

```bash
# Using npm
npm install fhevm
Expand All @@ -101,9 +99,6 @@ yarn add fhevm
pnpm add fhevm
```

_Find more details on implementation instructions in [this repository](https://github.com/zama-ai/fhevm-evmos)._
<br></br>

### A Simple Example

```solidity
Expand All @@ -112,19 +107,25 @@ _Find more details on implementation instructions in [this repository](https://g
pragma solidity ^0.8.24;
import "fhevm/lib/TFHE.sol";
import "fhevm/config/ZamaFHEVMConfig.sol";
contract Counter {
euint32 counter;
contract Counter is SepoliaZamaFHEVMConfig {
euint8 internal counter;
constructor() {
counter = TFHE.asEuint8(0);
TFHE.allowThis(counter);
}
function add(einput valueInput, bytes calldata inputProof) public {
euint32 value = TFHE.asEuint32(valueInput, inputProof);
euint8 value = TFHE.asEuint8(valueInput, inputProof);
counter = TFHE.add(counter, value);
TFHE.allow(counter, address(this));
TFHE.allowThis(counter);
}
}
```

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

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

### White paper
### **White Paper**

- [Confidential EVM Smart Contracts using Fully Homomorphic Encryption](https://github.com/zama-ai/fhevm/blob/main/fhevm-whitepaper-v2.pdf)
<br></br>
Gain insights into the technology powering fhEVM with our in-depth white paper:
👉 [**Confidential EVM Smart Contracts using Fully Homomorphic Encryption**](https://github.com/zama-ai/fhevm/blob/main/fhevm-whitepaper-v2.pdf)

---

### **Demos and Tutorials**

Access a curated collection of demos and step-by-step tutorials to guide your development journey:
🔗 [**Visit the Tutorials Page**](https://docs.zama.ai/fhevm/tutorials/see-all-tutorials)

---

### **Documentation**

Master `fhEVM` and build smart contracts using these resources:

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

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

### Documentation
### **Development templates**

Full, comprehensive documentation is available here: [https://docs.zama.ai/fhevm](https://docs.zama.ai/fhevm).
Start building faster with pre-configured templates tailored for various frameworks:

#### **Smart Contracts**

- 🔧 [**Hardhat Template**](https://github.com/zama-ai/fhevm-hardhat-template)
Smart contracts testing and development <- **_primary entry point for developers wanting to develop smart contracts on fhEVM_**
- 💻 [**fhEVM Contracts**](https://github.com/zama-ai/fhevm-contracts)
Library of standardized fhEVM contracts.

#### **Frontend frameworks**

- 🌐 [**React.js Template**](https://github.com/zama-ai/fhevm-react-template)
Simplify your FHE dApp development with a clean and optimized React.js template.
-[**Next.js Template**](https://github.com/zama-ai/fhevm-next-template)
Build server-rendered, scalable dApps with FHE support using this Next.js template.
- 🖼️ [**Vue.js Template**](https://github.com/zama-ai/fhevm-vue-template)
Create modular, responsive dApps with FHE capabilities using Vue.js.

---

### 🚀 **Kickstart Your Project Today!**

Leverage these templates to accelerate your development process and bring your ideas to life faster.

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

### Blockchain Implementation

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

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

Expand Down Expand Up @@ -187,8 +222,6 @@ Files that are generated now (can be seen inside `codegen/main.ts`)
```
lib/Impl.sol
lib/TFHE.sol
mocks/Impl.sol
mocks/TFHE.sol
contracts/tests/TFHETestSuiteX.sol
test/tfheOperations/tfheOperations.ts
```
Expand All @@ -197,57 +230,11 @@ test/tfheOperations/tfheOperations.ts
<a href="#about" > ↑ Back to top </a>
</p>

#### Tests

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.

##### Fast start

```bash
# in one terminal
npm run fhevm:start
# in another terminal
npm i
cp .env.example .env
npm run test:mock
```

</details>

##### Run test on a real fhEVM

```bash
npm run test -- --network sepolia
```

#### Adding new operators

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

#### Mocked mode

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:

```bash
npm run test:mock
```

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 :

```bash
npm run coverage:mock
```

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.

> [!Note]
> 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`.
<p align="right">
<a href="#about" > ↑ Back to top </a>
</p>

### Citations

To cite fhEVM or the whitepaper in academic papers, please use the following entries:
Expand Down
3 changes: 3 additions & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- [Configuration](fundamentals/configure.md)
- [Supported types](fundamentals/types.md)
- [Operations on encrypted types](fundamentals/operations.md)
- [asEbool, asEuintXX, asEaddress and asEbytesXX operations](fundamentals/asEXXoperators.md)
- [Access Control List](fundamentals/acl/README.md)
- [ACL examples](fundamentals/acl/acl_examples.md)
- [Encrypted Inputs](fundamentals/inputs.md)
Expand All @@ -38,6 +39,8 @@
- [Branching in FHE](guides/conditions.md)
- [Generate random numbers](guides/random.md)
- [Error handling](guides/error_handling.md)
- [Gas estimation](guides/gas.md)
- [Debug decrypt](guides/debug_decrypt.md)
- [Frontend](guides/frontend/README.md)
- [Build a web application](guides/frontend/webapp.md)
- [Build with Node](guides/frontend/node.md)
Expand Down
6 changes: 3 additions & 3 deletions docs/fundamentals/acl/acl_examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ The ACL system allows you to define two types of permissions for accessing ciphe

```solidity
import "fhevm/lib/TFHE.sol";
import { MockZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
import { SepoliaZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
contract SecretGiver is MockZamaFHEVMConfig {
contract SecretGiver is SepoliaZamaFHEVMConfig {
SecretStore public secretStore;
constructor() {
Expand All @@ -56,7 +56,7 @@ contract SecretGiver is MockZamaFHEVMConfig {
```

```
contract SecretStore is MockZamaFHEVMConfig {
contract SecretStore is SepoliaZamaFHEVMConfig {
euint16 public secretResult;
function storeSecret(euint16 callerSecret) public {
Expand Down
159 changes: 159 additions & 0 deletions docs/fundamentals/asEXXoperators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# asEbool, asEuintXX, asEaddress and asEbytesXX operations

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.

The operations can be categorized into three main use cases:

1. **Trivial encryption**: Converting plaintext values to encrypted types
2. **Type casting**: Converting between different encrypted types
3. **Input handling**: Processing encrypted inputs with proofs

## 1. Trivial encryption

Trivial encryption simply put is a plain text in a format of a ciphertext.

### Overview

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.

This type of casting involves converting plaintext (unencrypted) values into their encrypted equivalents, such as:

- `bool``ebool`
- `uint``euintXX`
- `bytes``ebytesXX`
- `address``eaddress`

> **Note**: When doing trivial encryption, the data is made compatible with FHE operations but remains publicly visible on-chain unless explicitly encrypted.
#### **Example**

```solidity
euint64 value64 = TFHE.asEuint64(7262); // Trivial encrypt a uint64
ebool valueBool = TFHE.asEbool(true); // Trivial encrypt a boolean
```

### Trivial encryption of `ebytesXX` types

The `TFHE.padToBytesXX` functions facilitate this trivial encryption process for byte arrays, ensuring compatibility with `ebytesXX` types. These functions:

- Pad the provided byte array to the appropriate length (`64`, `128`, or `256` bytes).
- Prevent runtime errors caused by improperly sized input data.
- Work seamlessly with `TFHE.asEbytesXX` for trivial encryption.

> **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.
#### Workflow

1. **Pad Input Data**:
Use the `padToBytesXX` functions to ensure your byte array matches the size requirements.
2. **Encrypt the Padded Data**:
Use `TFHE.asEbytesXX` to encrypt the padded byte array into the corresponding encrypted type.
3. **Grant Access**:
Use `TFHE.allowThis` and `TFHE.allow`optionally, if you want to persist allowance for those variables for later use.

### Example: Trivial Encryption with `ebytesXX`

Below is an example demonstrating how to encrypt and manage `ebytes64`, `ebytes128`, and `ebytes256` types:

```solidity
function trivialEncrypt() public {
// Encrypt a 64-byte array
ebytes64 yBytes64 = TFHE.asEbytes64(
TFHE.padToBytes64(
hex"19d179e0cc7e816dc944582ed4f5652f5951900098fc2e0a15a7ea4dc8cfa4e3b6c54beea5ee95e56b728762f659347ce1d4aa1b05fcc5"
)
);
TFHE.allowThis(yBytes64);
TFHE.allow(yBytes64, msg.sender);
// Encrypt a 128-byte array
ebytes128 yBytes128 = TFHE.asEbytes128(
TFHE.padToBytes128(
hex"13e7819123de6e2870c7e83bb764508e22d7c3ab8a5aee6bdfb26355ef0d3f1977d651b83bf5f78634fa360aa14debdc3daa6a587b5c2fb1710ab4d6677e62a8577f2d9fecc190ad8b11c9f0a5ec3138b27da1f055437af8c90a9495dad230"
)
);
TFHE.allowThis(yBytes128);
TFHE.allow(yBytes128, msg.sender);
// Encrypt a 256-byte array
ebytes256 yBytes256 = TFHE.asEbytes256(
TFHE.padToBytes256(
hex"d179e0cc7e816dc944582ed4f5652f5951900098fc2e0a15a7ea4dc8cfa4e3b6c54beea5ee95e56b728762f659347ce1d4aa1b05fcc513e7819123de6e2870c7e83bb764508e22d7c3ab8a5aee6bdfb26355ef0d3f1977d651b83bf5f78634fa360aa14debdc3daa6a587b5c2fb1710ab4d6677e62a8577f2d9fecc190ad8b11c9f0a5ec3138b27da1f055437af8c90a9495dad230"
)
);
TFHE.allowThis(yBytes256);
TFHE.allow(yBytes256, msg.sender);
}
```

## 2. Casting between encrypted types

This type of casting is used to reinterpret or convert one encrypted type into another. For example:

- `euint32``euint64`
- `ebytes128``ebytes256`

Casting between encrypted types is often required when working with operations that demand specific sizes or precisions.

> **Important**: When casting between encrypted types:
>
> - Casting from smaller types to larger types (e.g. `euint32``euint64`) preserves all information
> - Casting from larger types to smaller types (e.g. `euint64``euint32`) will truncate and lose information
The table below summarizes the available casting functions:

| From type | To type | Function |
| --------- | -------- | ---------------- |
| `euintX` | `euintX` | `TFHE.asEuintXX` |
| `ebool` | `euintX` | `TFHE.asEuintXX` |
| `euintX` | `ebool` | `TFHE.asEboolXX` |

{% hint style="info" %}
Casting between encrypted types is efficient and often necessary when handling data with differing precision requirements.
{% endhint %}

### **Workflow for encrypted types**

```solidity
// Casting between encrypted types
euint32 value32 = TFHE.asEuint32(value64); // Cast to euint32
ebool valueBool = TFHE.asEbool(value32); // Cast to ebool
```

## 3. Encrypted input

### Overview

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.

Encrypted inputs is in depth explained in the following section: [encrypted inputs](./inputs.md)

#### Example

```solidity
euint64 encryptedValue = TFHE.asEuint64(einputHandle, inputProof); // Interpret einputHandle as euint64
```

#### Details

Encrypted input casting validates:

1. The input handle references a valid ciphertext.
2. The accompanying proof matches the expected type.

For more information, see the [Encrypetd inputs documentation](./inputs.md)

## Overall operation summary

| Casting Type | Function | Input Type | Output Type |
| ------------------------ | ----------------------- | ----------------------- | ----------- |
| Trivial encryption | `TFHE.asEuintXX(x)` | `uintX` | `euintX` |
| | `TFHE.asEbool(x)` | `bool` | `ebool` |
| | `TFHE.asEbytesXX(x)` | `bytesXX` | `ebytesXX` |
| | `TFHE.asEaddress(x)` | `address` | `eaddress` |
| Conversion between types | `TFHE.asEuintXX(x)` | `euintXX`/`ebool` | `euintYY` |
| | `TFHE.asEbool(x)` | `euintXX` | `ebool` |
| Encrypted input | `TFHE.asEuintXX(x, y)` | `einput`, `bytes` proof | `euintX` |
| | `TFHE.asEbool(x, y)` | `einput`,`bytes` proof | `ebool` |
| | `TFHE.asEbytesXX(x, y)` | `einput`,`bytes` proof | `ebytesXX` |
| | `TFHE.asEaddress(x, y)` | `einput`, `bytes` proof | `eaddress` |
Loading

0 comments on commit ad6bfac

Please sign in to comment.