Skip to content

Commit

Permalink
docs: docs
Browse files Browse the repository at this point in the history
  • Loading branch information
poppyseedDev committed Jan 11, 2025
2 parents 3658c74 + 34dbe13 commit 4ff03cf
Show file tree
Hide file tree
Showing 17 changed files with 246 additions and 176 deletions.
52 changes: 43 additions & 9 deletions codegen/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ ${commonSolLib()}
* that interact with TFHE.
*/
library TFHE {
/// @notice Returned if the input's length is greater than 64 bytes.
error InputLengthAbove64Bytes(uint256 inputLength);
/// @notice Returned if the input's length is greater than 128 bytes.
error InputLengthAbove128Bytes(uint256 inputLength);
/// @notice Returned if the input's length is greater than 256 bytes.
error InputLengthAbove256Bytes(uint256 inputLength);
function setFHEVM(FHEVMConfigStruct memory fhevmConfig) internal {
Impl.setFHEVM(fhevmConfig);
}
Expand Down Expand Up @@ -877,6 +887,14 @@ function tfheAclMethods(supportedBits: number[]): string {
Impl.allowTransient(eaddress.unwrap(value), account);
}
function allowTransient(ebytes64 value, address account) internal {
Impl.allowTransient(ebytes64.unwrap(value), account);
}
function allowTransient(ebytes128 value, address account) internal {
Impl.allowTransient(ebytes128.unwrap(value), account);
}
function allowTransient(ebytes256 value, address account) internal {
Impl.allowTransient(ebytes256.unwrap(value), account);
}
Expand Down Expand Up @@ -1003,13 +1021,19 @@ function tfheCustomMethods(): string {
// Left-pad a bytes array with zeros such that it becomes of length 64.
function padToBytes64(bytes memory input) internal pure returns (bytes memory) {
require(input.length <= 64, "Input exceeds 64 bytes");
uint256 inputLength = input.length;
if (inputLength > 64) {
revert InputLengthAbove64Bytes(inputLength);
}
bytes memory result = new bytes(64);
uint256 paddingLength = 64 - input.length;
uint256 paddingLength = 64 - inputLength;
for (uint256 i = 0; i < paddingLength; i++) {
result[i] = 0;
}
for (uint256 i = 0; i < input.length; i++) {
for (uint256 i = 0; i < inputLength; i++) {
result[paddingLength + i] = input[i];
}
return result;
Expand All @@ -1027,13 +1051,18 @@ function tfheCustomMethods(): string {
// Left-pad a bytes array with zeros such that it becomes of length 128.
function padToBytes128(bytes memory input) internal pure returns (bytes memory) {
require(input.length <= 128, "Input exceeds 128 bytes");
uint256 inputLength = input.length;
if (inputLength > 128) {
revert InputLengthAbove128Bytes(inputLength);
}
bytes memory result = new bytes(128);
uint256 paddingLength = 128 - input.length;
uint256 paddingLength = 128 - inputLength;
for (uint256 i = 0; i < paddingLength; i++) {
result[i] = 0;
}
for (uint256 i = 0; i < input.length; i++) {
for (uint256 i = 0; i < inputLength; i++) {
result[paddingLength + i] = input[i];
}
return result;
Expand All @@ -1051,13 +1080,18 @@ function tfheCustomMethods(): string {
// Left-pad a bytes array with zeros such that it becomes of length 256.
function padToBytes256(bytes memory input) internal pure returns (bytes memory) {
require(input.length <= 256, "Input exceeds 256 bytes");
uint256 inputLength = input.length;
if (inputLength > 256) {
revert InputLengthAbove256Bytes(inputLength);
}
bytes memory result = new bytes(256);
uint256 paddingLength = 256 - input.length;
uint256 paddingLength = 256 - inputLength;
for (uint256 i = 0; i < paddingLength; i++) {
result[i] = 0;
}
for (uint256 i = 0; i < input.length; i++) {
for (uint256 i = 0; i < inputLength; i++) {
result[paddingLength + i] = input[i];
}
return result;
Expand Down
Binary file modified docs/.gitbook/assets/useGateway.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions docs/fundamentals/decryption/reencryption.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ Here, `balanceOf` allows retrieval of the user’s encrypted balance stored on t

## Step 2: re-encrypt the ciphertext

Re-encryption is performed client-side using the `fhevmjs` library. Below is an example of how to implement this in a dApp:
Re-encryption is performed client-side using the `fhevmjs` library. [Refer to the guide](../../guides/frontend/webapp.md) to learn how to include `fhevmjs` in your project.
Below is an example of how to implement reencryption in a dApp:

```ts
import { createInstances } from "../instance";
import { getSigners, initSigners } from "../signers";
import abi from "./abi.json";
import { Contract, BrowserProvider } from "ethers";
import { createInstance } from "fhevmjs";
import { createInstance } from "fhevmjs/bundle";

const CONTRACT_ADDRESS = "";

Expand Down
77 changes: 0 additions & 77 deletions docs/getting_started/devnet.md

This file was deleted.

12 changes: 6 additions & 6 deletions docs/getting_started/ethereum.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ Save this in your `.env` file:

| Contract/Service | Address/Value |
| ---------------------- | ------------------------------------------ |
| TFHE_EXECUTOR_CONTRACT | 0x199fB61DFdfE46f9F90C9773769c28D9623Bb90e |
| ACL_CONTRACT | 0x9479B455904dCccCf8Bc4f7dF8e9A1105cBa2A8e |
| PAYMENT_CONTRACT | 0x25FE5d92Ae6f89AF37D177cF818bF27EDFe37F7c |
| KMS_VERIFIER_CONTRACT | 0x904Af2B61068f686838bD6257E385C2cE7a09195 |
| GATEWAY_CONTRACT | 0x7455c89669cdE1f7Cb6D026DFB87263422D821ca |
| PUBLIC_KEY_ID | 55729ddea48547ea837137d122e1c90043e94c41 |
| TFHE_EXECUTOR_CONTRACT | 0x687408aB54661ba0b4aeF3a44156c616c6955E07 |
| ACL_CONTRACT | 0xFee8407e2f5e3Ee68ad77cAE98c434e637f516e5 |
| PAYMENT_CONTRACT | 0xFb03BE574d14C256D56F09a198B586bdfc0A9de2 |
| KMS_VERIFIER_CONTRACT | 0x9D6891A6240D6130c54ae243d8005063D05fE14b |
| GATEWAY_CONTRACT | 0x33347831500F1e73f0ccCBb95c9f86B94d7b1123 |
| PUBLIC_KEY_ID | 0301c5dd3e2702992b7c12930b7d4defeaaa52cf |
| GATEWAY_URL | `https://gateway.sepolia.zama.ai/` |
32 changes: 0 additions & 32 deletions docs/getting_started/first_smart_contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,38 +164,6 @@ The test file demonstrates key concepts for testing fhEVM smart contracts:
- Handle asynchronous operations properly with async/await
- Set up proper encryption instances for testing encrypted values

### No constant nor immutable encrypted state variables

Never use encrypted types for constant or immutable state variables, even if they should actually stay constants, or else any transaction involving those will fail. This is because ciphertexts should always be stored in the privileged storage of the contract (see paragraph 4.4 of [whitepaper](../../fhevm-whitepaper.pdf)) while constant and immutable variables are just appended to the bytecode of the deployed contract at construction time.

❌ So, even if `a` and `b` should never change after construction, the following example :

```solidity
contract C {
euint32 internal constant a = TFHE.asEuint32(42);
euint32 internal immutable b;
constructor(uint32 _b) {
b = TFHE.asEuint32(_b);
TFHE.allowThis(b);
}
}
```

✅ Should be replaced by this snippet:

```solidity
contract C {
euint32 internal a = TFHE.asEuint32(42);
euint32 internal b;
constructor(uint32 _b) {
b = TFHE.asEuint32(_b);
TFHE.allowThis(b);
}
}
```

## **Next steps**

Congratulations! You’ve configured and written your first confidential smart contract. Here are some ideas to expand your knowledge:
Expand Down
6 changes: 2 additions & 4 deletions docs/getting_started/write_contract/remix.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ This document provides guidance on using the new Zama plugin for the [the offici

## Configuring the Zama plugin

After connecting to the Zama Plugin, follow the steps to configure it:
After connecting to the Zama plugin, follow the steps to configure it:

1. Click on the plugin button located on the left of the screen
2. Add a Gateway URL to be able to request reencryption of ciphertexts, as shown in the picture below.

The default recommended Gateway URL is: `https://gateway.devnet.zama.ai`.
2. Use the **Zama Coprocessor - Sepolia** settings or use a custom gateway and contracts settings.

<figure><img src="../../.gitbook/assets/useGateway.png" alt="How to install Remix IDE plugin" width="300"><figcaption></figcaption></figure>

Expand Down
4 changes: 2 additions & 2 deletions docs/guides/frontend/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const { createInstance } = require("fhevmjs");
const createFhevmInstance = async () => {
return createInstance({
chainId: 11155111, // Sepolia chain ID
networkUrl: "https://rpc.sepolia.org/", // Sepolia RPC URL
gatewayUrl: "https://gateway.zama.ai",
networkUrl: "https://eth-sepolia.public.blastapi.io", // Sepolia RPC URL
gatewayUrl: "https://gateway.sepolia.zama.ai",
});
};
createFhevmInstance().then((instance) => {
Expand Down
60 changes: 52 additions & 8 deletions docs/guides/frontend/webapp.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,49 @@ You can also use [this template](https://github.com/zama-ai/fhevmjs-vue-template

You can also use [this template](https://github.com/zama-ai/fhevmjs-next-template) to start an application with fhevmjs, using Next + TypeScript.

## Using the mocked coprocessor for front end

As an alternative to use the real coprocessor deployed on Sepolia to help you develop your dApp faster and without needing testnet tokens, you can use a mocked fhevm. Currently, we recommend you to use the `ConfidentialERC20` dApp example available on the `mockedFrontend` branch of the [React template](https://github.com/zama-ai/fhevm-react-template/tree/mockedFrontend). Follow the README on this branch, and you will be able to deploy exactly the same dApp both on Sepolia as well as on the mocked coprocessor seamlessly.

## Using directly the library

### Step 1: Install the library
### Step 1: Setup the library

`fhevmjs` consists of multiple files, including WASM files and WebWorkers, which can make packaging these components correctly in your setup cumbersome. To simplify this process, especially if you're developing a dApp with server-side rendering (SSR), we recommend using our CDN.

#### Using UMD CDN

Include this line at the top of your project.

```html
<script src="https://cdn.zama.ai/fhevmjs/0.6.2/fhevmjs.umd.cjs" type="text/javascript"></script>
```

In your project, you can use the bundle import if you install `fhevmjs` package:

```javascript
import { initFhevm, createInstance } from "fhevmjs/bundle";
```

#### Using ESM CDN

If you prefer You can also use the `fhevmjs` as a ES module:

```html
<script type="module">
import { initFhevm, createInstance } from "https://cdn.zama.ai/fhevmjs/0.6.2/fhevmjs.js";
await initFhevm();
const instance = await createInstance({
network: window.ethereum,
kmsContractAddress: "0x9D6891A6240D6130c54ae243d8005063D05fE14b",
aclContractAddress: "0xFee8407e2f5e3Ee68ad77cAE98c434e637f516e5",
gatewayUrl: "https://gateway.sepolia.zama.ai",
});
</script>
```

#### Using npm package

Install the `fhevmjs` library to your project:

Expand All @@ -35,14 +75,18 @@ yarn add fhevmjs
pnpm add fhevmjs
```

### Step 2: Initialize your project

`fhevmjs` uses ESM format. You need to set the [type to "module" in your package.json](https://nodejs.org/api/packages.html#type). If your node project use `"type": "commonjs"` or no type, you can force the loading of the web version by using `import { createInstance } from 'fhevmjs/web';`

```javascript
import { initFhevm, createInstance } from "fhevmjs";
```

### Step 2: Initialize your project

To use the library in your project, you need to load the WASM of [TFHE](https://www.npmjs.com/package/tfhe) first with `initFhevm`.

```javascript
import { initFhevm } from "fhevmjs";
import { initFhevm } from "fhevmjs/bundle";

const init = async () => {
await initFhevm(); // Load needed WASM
Expand All @@ -61,15 +105,15 @@ Once the WASM is loaded, you can now create an instance. An instance receives an
- `coprocessorUrl` (optional): the URL of the coprocessor

```javascript
import { initFhevm, createInstance } from "fhevmjs";
import { initFhevm, createInstance } from "fhevmjs/bundle";

const init = async () => {
await initFhevm(); // Load TFHE
return createInstance({
kmsContractAddress: "0x208De73316E44722e16f6dDFF40881A3e4F86104",
aclContractAddress: "0xc9990FEfE0c27D31D0C2aa36196b085c0c4d456c",
kmsContractAddress: "0x9D6891A6240D6130c54ae243d8005063D05fE14b",
aclContractAddress: "0xFee8407e2f5e3Ee68ad77cAE98c434e637f516e5",
network: window.ethereum,
gatewayUrl: "https://gateway.zama.ai/",
gatewayUrl: "https://gateway.sepolia.zama.ai/",
});
};

Expand Down
11 changes: 8 additions & 3 deletions docs/guides/frontend/webpack.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,22 @@ resolve: {

**Cause:** The library may not bundle correctly with certain frameworks, leading to errors during the build or runtime process.

<<<<<<< HEAD
**Possible solutions:** Use the prebundled version available in `fhevmjs/bundle`. Embed the library with a `<script>` tag and initialize it as shown below:
=======
**Possible solutions:** Use the [prebundled version available](./webapp.md) with `fhevmjs/bundle`. Embed the library with a `<script>` tag and initialize it as shown below:

> > > > > > > 34dbe133566d273b54d87e58b5986c05c44f1d41
```javascript
const start = async () => {
await window.fhevm.initFhevm(); // load wasm needed
const instance = window.fhevm
.createInstance({
kmsContractAddress: "0x208De73316E44722e16f6dDFF40881A3e4F86104",
aclContractAddress: "0xc9990FEfE0c27D31D0C2aa36196b085c0c4d456c",
kmsContractAddress: "0x9D6891A6240D6130c54ae243d8005063D05fE14b",
aclContractAddress: "0xFee8407e2f5e3Ee68ad77cAE98c434e637f516e5",
network: window.ethereum,
gatewayUrl: "https://gateway.zama.ai/",
gatewayUrl: "https://gateway.sepolia.zama.ai/",
})
.then((instance) => {
console.log(instance);
Expand Down
Loading

0 comments on commit 4ff03cf

Please sign in to comment.