Skip to content

Commit

Permalink
docs: add review
Browse files Browse the repository at this point in the history
  • Loading branch information
poppyseedDev committed Nov 28, 2024
1 parent 5668f1d commit 713a5e1
Show file tree
Hide file tree
Showing 17 changed files with 506 additions and 307 deletions.
31 changes: 3 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ _Learn more use cases in the [list of examples](https://docs.zama.ai/fhevm/tutor
- [A Simple Example](#a-simple-example)
- **[Resources](#resources)**
- [White paper](#white-paper)
- [Demos](#demos)
[Demos](#demos-and-tutorials)
- [Tutorials](#tutorials)
- [Documentation](#documentation)
- [Blockchain Implementation](#blockchain-implementation)
Expand Down Expand Up @@ -142,34 +142,9 @@ _More examples are available [here](https://github.com/zama-ai/fhevm/tree/main/e
- [Confidential EVM Smart Contracts using Fully Homomorphic Encryption](https://github.com/zama-ai/fhevm/blob/main/fhevm-whitepaper.pdf)
<br></br>

### Demos
### Demos and Tutorials

#### Finance

- [ERC-20](https://github.com/zama-ai/fhevm/blob/main/examples/EncryptedERC20.sol): A variation of the standard ERC20 smart contract that incorporates encrypted balances, providing additional privacy for token holders.
- [Darkpool](https://github.com/omurovec/fhe-darkpools): A smart contract that enables anonymous trading of cryptocurrencies or assets, typically used to execute large orders without affecting the market price. - by [Owen Murovec](https://github.com/omurovec)

#### Games:

- [Cipherbomb](https://github.com/immortal-tofu/cipherbomb): A Hardhat-based template for developing Solidity smart contracts, with sensible defaults. - by Clément Danjou
- [Battleship](https://github.com/battleship-fhevm/battleship-hardhat): A smart contract that replicates the classic Battleship game on a blockchain in a transparent manner. - by [Owen Murovec](https://github.com/omurovec)

#### Others

- [Blind auction](https://github.com/zama-ai/fhevm/blob/main/examples/BlindAuction.sol): A smart contract for conducting blind auctions where bids are encrypted and the winning bid remains private.

_If you have built awesome projects using fhEVM, please let us know and we will be happy to showcase them here!_
<br></br>

### Tutorials

- [[Video tutorial] How to Write Confidential Smart Contracts Using Zama's fhEVM](https://www.zama.ai/post/video-tutorial-how-to-write-confidential-smart-contracts-using-zamas-fhevm)
- [Confidential ERC-20 Tokens Using Homomorphic Encryption and the fhEVM](https://www.zama.ai/post/confidential-erc-20-tokens-using-homomorphic-encryption)
- [On-chain Blind Auctions Using Homomorphic Encryption and the fhEVM](https://www.zama.ai/post/on-chain-blind-auctions-using-homomorphic-encryption)
- [Programmable Privacy and Onchain Compliance using Homomorphic Encryption](https://www.zama.ai/post/programmable-privacy-and-onchain-compliance-using-homomorphic-encryption)

_Explore more useful resources in [fhEVM tutorials](https://docs.zama.ai/fhevm/tutorials/see-all-tutorials) and [Awesome Zama repo](https://github.com/zama-ai/awesome-zama)._
<br></br>
For a comprehensive list of demos and tutorials, visit our [tutorials page](https://docs.zama.ai/fhevm/tutorials/see-all-tutorials).

### Documentation

Expand Down
9 changes: 5 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ layout:

Learn the basics of fhEVM, set it up, and make it run with ease.

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th data-hidden data-card-cover data-type="files"></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><strong>What is fhEVM</strong></td><td>Understand the basic concepts of fhEVM library.</td><td><a href=".gitbook/assets/start1.png">start1.png</a></td><td><a href="getting_started/overview.md">overview.md</a></td></tr><tr><td><strong>Write contract</strong></td><td>Start writing fhEVM smart contract using Hardhat template</td><td><a href=".gitbook/assets/start4.png">start4.png</a></td><td><a href="getting_started/write_contract/hardhat.md">hardhat.md</a></td></tr><tr><td><strong>Deploy on fhEVM native</strong></td><td>Get 10 Zama token to start working with fhEVM native</td><td><a href=".gitbook/assets/start2.png">start2.png</a></td><td><a href="getting_started/devnet.md">devnet.md</a></td></tr><tr><td><strong>Deploy on Ethereum</strong></td><td>Coming soon!</td><td><a href=".gitbook/assets/start5.png">start5.png</a></td><td><a href="getting_started/ethereum.md">ethereum.md</a></td></tr></tbody></table>
<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th data-hidden data-card-cover data-type="files"></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><strong>What is fhEVM</strong></td><td>Understand the basic concepts of fhEVM library.</td><td><a href=".gitbook/assets/start1.png">start1.png</a></td><td><a href="getting_started/overview.md">overview.md</a></td></tr><tr><td><strong>Write contract</strong></td><td>Start writing fhEVM smart contract using Hardhat template</td><td><a href=".gitbook/assets/start4.png">start4.png</a></td><td><a href="getting_started/write_contract/hardhat.md">hardhat.md</a></td></tr><tr><td><strong>Learn to write FHE contracts</strong></td><td>Learn how to write your first confidential smart contract</td><td><a href=".gitbook/assets/start2.png">start2.png</a></td><td><a href="fundamentals/first_step/start.md">start.md</a></td></tr><tr><td><strong>Deploy on Ethereum</strong></td><td>Be one of the first to deploy confidential smart contracts on Ethereum!</td><td><a href=".gitbook/assets/start5.png">start5.png</a></td><td><a href="getting_started/ethereum.md">ethereum.md</a></td></tr></tbody></table>

## Develop a fhEVM smart contract

Start developing fhEVM smart contracts in Solidity by exploring its core features, discovering essential guides, and learning more with user-friendly tutorials.

<table data-view="cards"><thead><tr><th></th><th></th><th></th><th data-hidden data-card-cover data-type="files"></th></tr></thead><tbody><tr><td><strong>Fundamentals</strong></td><td>Explore core features.</td><td><ul><li><a href="getting_started/write_contract/">Write contract</a></li><li><a href="fundamentals/first_step/types.md">Use encrypted types</a></li></ul></td><td><a href=".gitbook/assets/build1.png">build1.png</a></td></tr><tr><td><strong>Guides</strong></td><td>Deploy your project.</td><td><ul><li><a href="guides/gas.md">Gas estimation</a></li><li><a href="guides/pitfalls.md">Common pitfalls and best practices</a></li></ul></td><td><a href=".gitbook/assets/build2.png">build2.png</a></td></tr><tr><td><strong>Tutorials</strong></td><td>Learn more with tutorials.</td><td><ul><li><a href="tutorials/see-all-tutorials.md#start-here">Start here</a></li><li><a href="tutorials/see-all-tutorials.md#go-further">Go further</a></li></ul></td><td><a href=".gitbook/assets/build3.png">build3.png</a></td></tr></tbody></table>
<table data-view="cards"><thead><tr><th></th><th></th><th></th><th data-hidden data-card-cover data-type="files"></th></tr></thead><tbody><tr><td><strong>Fundamentals</strong></td><td>Explore core features.</td><td><ul><li><a href="getting_started/write_contract/hardhat.md">Write contract with Hardhat</a></li><li><a href="fundamentals/first_step/types.md">Use encrypted types</a></li></ul></td><td><a href=".gitbook/assets/build1.png">build1.png</a></td></tr><tr><td><strong>Guides</strong></td><td>Learn further.</td><td><a href=".gitbook/assets/build2.png">build2.png</a></td></tr><tr><td><strong>Tutorials</strong></td><td>Learn more with tutorials.</td><td><ul><li><a href="tutorials/see-all-tutorials.md">See all tutorials</a></li></ul></td><td><a href=".gitbook/assets/build3.png">build3.png</a></td></tr></tbody></table>

## Explore more

Expand Down Expand Up @@ -55,8 +55,9 @@ Collaborate with us to advance the FHE spaces and drive innovation together.
- [Contribute to fhEVM](developer/contribute.md)
- [Follow the development roadmap](developer/roadmap.md)
- [See the latest test release note](https://github.com/zama-ai/fhevm/releases)
- [Request a feature](https://github.com/zama-ai/fhevm/issues/new)
- [Report a bug](https://github.com/zama-ai/fhevm/issues/new)
- [Request a feature](https://github.com/zama-ai/fhevm/issues/new?assignees=&labels=enhancement&projects=&template=feature-request.md&title=)
- [Report a bug](https://github.com/zama-ai/fhevm/issues/new?assignees=&labels=bug&projects=&template=bug_report_fhevm.md&title=)


---

Expand Down
6 changes: 3 additions & 3 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
- [Architecture overview](fundamentals/architecture_overview.md)
- [Encryption, decryption, re-encryption, and computation](fundamentals/d_re_ecrypt_compute.md)
- [Access control list](fundamentals/acl.md)
- [Configuration](fundamentals/first_step/configure.md)
- [Workflow for SC development](fundamentals/first_step/index.md)
- [Configuration](fundamentals/first_step/configure.md)
- [First smart contract](fundamentals/first_step/start.md)
- [Supported types](fundamentals/first_step/types.md)
- [Operations on encrypted types](fundamentals/first_step/operations.md)
Expand All @@ -32,11 +33,10 @@
## Guides

- Smart Contracts
- [Common pitfalls and best practises](guides/pitfalls.md)
- [fhevm-contracts](guides/contracts.md)
- [If sentances](guides/loop.md)
- [Branching in FHE](guides/conditions.md)
- [Generate random numbers](guides/random.md)
- [Gas estimation on devnet](guides/gas.md)
- [Error handling](guides/error_handling.md)
- Frontend
- [Build a web application](guides/frontend/webapp.md)
Expand Down
10 changes: 6 additions & 4 deletions docs/fundamentals/first_step/acl_examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ 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";
contract SecretGiver {
contract SecretGiver is MockZamaFHEVMConfig {
SecretStore public secretStore;
constructor() {
TFHE.setFHEVM(FHEVMConfig.defaultConfig());
secretStore = new SecretStore();
}
Expand All @@ -53,8 +53,10 @@ contract SecretGiver {
secretStore.storeSecret(mySecret);
}
}
```

contract SecretStore {
```
contract SecretStore is MockZamaFHEVMConfig {
euint16 public secretResult;
function storeSecret(euint16 callerSecret) public {
Expand Down Expand Up @@ -100,7 +102,7 @@ function randomize() public {

---

## Security best practices
## 🔧 Best practices

### Verifying sender access

Expand Down
136 changes: 78 additions & 58 deletions docs/fundamentals/first_step/configure.md
Original file line number Diff line number Diff line change
@@ -1,96 +1,116 @@
# Configuring your smart contract for encrypted computations
# Configuring Your Smart Contract for Encrypted Computations

This document describes how to enable encrypted computations in your smart contract by setting up the `fhEVM` environment. This involves configuring essential functions and integrating specific libraries to handle encrypted data securely and efficiently.
This document explains how to enable encrypted computations in your smart contract by setting up the `fhEVM` environment. Learn how to integrate essential libraries, configure encryption, and add secure computation logic to your contracts.

---

## Workflow for writing confidential smart contracts
## Core Configuration Setup

1. **Use the Hardhat Template**:
Start with our custom [`fhevm-hardhat-template` repository](https://github.com/zama-ai/fhevm-hardhat-template). Hardhat is a powerful development environment for Solidity, allowing you to write, test, and deploy contracts to the `fhEVM` using TypeScript.
To utilize encrypted computations in Solidity contracts, you must configure the **TFHE library** and **Gateway addresses**. The `fhevm` package simplifies this process with prebuilt configuration contracts, allowing you to focus on developing your contract’s logic without handling the underlying cryptographic setup.

2. **Begin with Unencrypted Logic**:
Develop your contract as if it were intended for a traditional EVM chain. Use cleartext variables and basic logic, which simplifies reasoning about the contract’s functionality.
### Key Components Configured Automatically:
1. **TFHE Library**: Sets up encryption parameters and cryptographic keys.
2. **Gateway**: Manages secure cryptographic operations, including reencryption and decryption.
3. **Network-Specific Settings**: Adapts to local testing, testnets (e.g., Sepolia), or mainnet deployment.

3. **Add Encryption**:
Integrate the `TFHE` Solidity library to enable encryption. Convert sensitive variables (e.g., `uintX`) into encrypted types (e.g., `euintX`) to ensure confidentiality. Refer to the [pitfalls to avoid and best practices](../../guides/pitfalls.md) guide for detailed advice.

4. **Leverage Example Templates**:
The [fhevm-contracts repository](https://github.com/zama-ai/fhevm-contracts) provides a collection of example smart contracts demonstrating common FHE patterns and best practices. Use these templates to:
- Study proven implementation patterns
- Understand gas optimization techniques
- Learn how to properly handle encrypted operations
- See real-world examples of confidential smart contracts
By inheriting these configuration contracts, you ensure seamless initialization and functionality across environments.

---

## Core configuration functions

Below are the key configuration functions needed to enable encrypted operations in your contract.
### ZamaFHEVMConfig.sol

### `setFHEVM`

This function initializes the FHEVM environment with the required configuration.
This configuration contract initializes the **FHEVM environment** with required encryption parameters.

#### Import Based on Your Environment:
```solidity
function setFHEVM(FHEVMConfig.FHEVMConfigStruct memory fhevmConfig) public
```
// For Mock testnet
import { MockZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
// For Ethereum Sepolia
import { SepoliaZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
- **Purpose**: Sets encryption parameters, including cryptographic keys and supported ciphertext types.
- **Usage**: Call this function in the constructor to ensure proper initialization.
- **Default Configuration**: Use `FHEVMConfig.defaultConfig()` for a standard encryption setup.
// For Ethereum Mainnet (when ready)
import { EthereumZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
```

#### Example
#### Purpose:
- Sets encryption parameters such as cryptographic keys and supported ciphertext types.
- Ensures proper initialization of the FHEVM environment.

#### Example: Using Mock Configuration
```solidity
constructor() {
// Initialize the FHEVM environment with default settings
TFHE.setFHEVM(FHEVMConfig.defaultConfig());
// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.24;
import { MockZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
contract MyERC20 is MockZamaFHEVMConfig {
constructor() {
// Additional initialization logic if needed
}
}
```
```

---

### `isInitialized`
### ZamaGatewayConfig.sol

This utility function verifies whether a given encrypted variable is initialized.
To perform decryption or reencryption, your contract must interact with the **Gateway**, which acts as a secure bridge between the blockchain, coprocessor, and Key Management System (KMS).

#### Import Based on Your Environment:
```solidity
function isInitialized(T v) internal pure returns (bool)
```
// For Mock testnet
import { MockZamaGatewayConfig } from "fhevm/config/ZamaGatewayConfig.sol";
// For Ethereum Sepolia
import { SepoliaZamaGatewayConfig } from "fhevm/config/ZamaGatewayConfig.sol";
- **Purpose**: Prevents uninitialized encrypted variables from being used, avoiding unexpected behavior.
- **Usage**: Use this check in critical functions or constructors.
// For Ethereum Mainnet (when ready)
import { EthereumZamaGatewayConfig } from "fhevm/config/ZamaGatewayConfig.sol";
```

#### Example
#### Purpose:
- Configures the Gateway for secure cryptographic operations.
- Facilitates reencryption and decryption requests.

#### Example: Configuring the Gateway with Mock Settings
```solidity
require(TFHE.isInitialized(counter), "Counter not initialized!");
```
import "fhevm/lib/TFHE.sol";
import { MockZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
import { MockZamaGatewayConfig } from "fhevm/config/ZamaGatewayConfig.sol";
import "fhevm/gateway/GatewayCaller.sol";
contract Test is MockZamaFHEVMConfig, MockZamaGatewayConfig, GatewayCaller {
constructor() {
// Gateway and FHEVM environment initialized automatically
}
}
```

---

### `setGateway`
### Using `isInitialized`

When performing decryption or re-encryption, you must configure the contract to interact with the `Gateway`.
The `isInitialized` utility function checks whether an encrypted variable has been properly initialized, preventing unexpected behavior due to uninitialized values.

#### Function Signature:
```solidity
Gateway.setGateway(Gateway.defaultGatewayAddress());
```

The Gateway serves as the bridge between the blockchain, coprocessor, and KMS for secure cryptographic operations.
function isInitialized(T v) internal pure returns (bool)
```

#### Example
#### Purpose:
- Ensures encrypted variables are initialized before use.
- Prevents potential logic errors in contract execution.

#### Example: Initialization Check for Encrypted Counter
```solidity
import "fhevm/lib/TFHE.sol";
import "fhevm/gateway/GatewayCaller.sol";
require(TFHE.isInitialized(counter), "Counter not initialized!");
```

contract Test is GatewayCaller {
constructor() {
// Set up the FHEVM and Gateway configurations
TFHE.setFHEVM(FHEVMConfig.defaultConfig());
Gateway.setGateway(Gateway.defaultGatewayAddress());
}
}
```
---

## Summary

By leveraging prebuilt configuration contracts like `ZamaFHEVMConfig.sol` and `ZamaGatewayConfig.sol`, you can efficiently set up your smart contract for encrypted computations. These tools abstract the complexity of cryptographic initialization, allowing you to focus on building secure, confidential smart contracts.

For a complete guide on contract development and integration, refer to our [Workflow for Writing Confidential Smart Contracts](../../guides/workflow.md).
Loading

0 comments on commit 713a5e1

Please sign in to comment.