bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 44쪽
there is a contract named `HCULimit`, which monitors HCU consumption for each transaction and enforces two key limits:
* **Sequential homomorphic operations depth limit per transaction**: Controls HCU usage for operations that must be processed in order.
* **Global homomorphic operations complexity per transaction**: Controls HCU usage for operations that can be processed in parallel.
If either limit is exceeded, the transaction will revert.
## HCU limit
The current devnet has an HCU limit of **20,000,000** per transaction and an HCU depth limit of **5,000,000** per transaction. If either HCU limit is exceeded, the transaction will revert.
To resolve this, you must do one of the following:
* Refactor your code to reduce the number of FHE operations in your transaction.
* Split your FHE operations across multiple independent transactions.
## HCU costs for common operations
### Boolean operations (`ebool`)
| Function name | HCU (scalar) | HCU (non-scalar) |
| ------------- | ------------ | ---------------- |
| `and` | 22,000 | 25,000 |
| `or` | 22,000 | 24,000 |
| `xor` | 2,000 | 22,000 |
| `not` | - | 2 |
| `select` | - | 55,000 |
| `randEbool` | - | 19,000 |
***
### Unsigned integer operations
HCU increase with the bit-width of the encrypted integer type. Below are the detailed costs for various operations on encrypted types.
#### **8-bit Encrypted integers (`euint8`)**
| Function name | HCU (scalar) | HCU (non-scalar) |
| ------------- | ------------ | ---------------- |
| `add` | 84,000 | 88,000 |
| `sub` | 84,000 | 91,000 |
| `mul` | 122,000 | 150,000 |
| `div` | 210,000 | - |
| `rem` | 440,000 | - |
| `and` | 31,000 | 31,000 |
| `or` | 30,000 | 30,000 |
| `xor` | 31,000 | 31,000 |
| `shr` | 32,000 | 91,000 |
| `shl` | 32,000 | 92,000 |
| `rotr` | 31,000 | 93,000 |
| `rotl` | 31,000 | 91,000 |
| `eq` | 55,000 | 55,000 |
| `ne` | 55,000 | 55,000 |
| `ge` | 52,000 | 63,000 |
| `gt` | 52,000 | 59,000 |
| `le` | 58,000 | 58,000 |
| `lt` | 52,000 | 59,000 |
| `min` | 84,000 | 119,000 |
| `max` | 89,000 | 121,000 |
| `neg` | - | 79,000 |
| `not` | - | 9 |
| `select` | - | 55,000 |
| `randEuint8` | - | 23,000 |
#### **16-bit Encrypted integers (`euint16`)**
| Function name | HCU (scalar) | HCU (non-scalar) |
| ------------- | ------------ | ---------------- |
| `add` | 93,000 | 93,000 |
| `sub` | 93,000 | 93,000 |
| `mul` | 193,000 | 222,000 |
| `div` | 302,000 | - |
| `rem` | 580,000 | - |
| `and` | 31,000 | 31,000 |
| `or` | 30,000 | 31,000 |
| `xor` | 31,000 | 31,000 |
| `shr` | 32,000 | 123,000 |
| `shl` | 32,000 | 125,000 |
| `rotr` | 31,000 | 125,000 |
| `rotl` | 31,000 | 125,000 |
| `eq` | 55,000 | 83,000 |
| `ne` | 55,000 | 83,000 |
| `ge` | 55,000 | 84,000 |
| `gt` | 55,000 | 84,000 |
| `le` | 58,000 | 83,000 |
| `lt` | 58,000 | 84,000 |
| `min` | 88,000 | 146,000 |
| `max` | 89,000 | 145,000 |
| `neg` | - | 93,000 |
| `not` | - | 16 |
| `select` | - | 55,000 |
| `randEuint16` | - | 23,000 |
#### **32-bit Encrypted Integers (`euint32`)**
| Function name | HCU (scalar) | HCU (non-scalar) |
| ------------- | ------------ | ---------------- |
| `add` | 95,000 | 125,000 |
| `sub` | 95,000 | 125,000 |
| `mul` | 265,000 | 328,000 |
| `div` | 438,000 | - |
| `rem` | 792,000 | - |
| `and` | 32,000 | 32,000 |
| `or` | 32,000 | 32,000 |
| `xor` | 32,000 | 32,000 |
| `shr` | 32,000 | 163,000 |
| `shl` | 32,000 | 162,000 |
| `rotr` | 32,000 | 160,000 |
| `rotl` | 32,000 | 163,000 |
| `eq` | 82,000 | 86,000 |
| `ne` | 83,000 | 85,000 |
| `ge` | 84,000 | 118,000 |
| `gt` | 84,000 | 118,000 |
| `le` | 84,000 | 117,000 |
| `lt` | 83,000 | 117,000 |
| `min` | 117,000 | 182,000 |
| `max` | 117,000 | 180,000 |
| `neg` | - | 131,000 |
| `not` | - | 32 |
| `select` | - | 55,000 |
| `randEuint32` | - | 24,000 |
#### **64-bit Encrypted integers (`euint64`)**
| Function name | HCU (scalar) | HCU (non-scalar) |
| ------------- | ------------ | ---------------- |
| `add` | 133,000 | 162,000 |
| `sub` | 133,000 | 162,000 |
| `mul` | 365,000 | 596,000 |
| `div` | 715,000 | - |
| `rem` | 1,153,000 | - |
| `and` | 34,000 | 34,000 |
| `or` | 34,000 | 34,000 |
| `xor` | 34,000 | 34,000 |
| `shr` | 34,000 | 209,000 |
| `shl` | 34,000 | 208,000 |
| `rotr` | 34,000 | 209,000 |
| `rotl` | 34,000 | 209,000 |
| `eq` | 83,000 | 120,000 |
| `ne` | 84,000 | 118,000 |
| `ge` | 116,000 | 152,000 |
| `gt` | 117,000 | 152,000 |
| `le` | 119,000 | 149,000 |
| `lt` | 118,000 | 146,000 |
| `min` | 150,000 | 219,000 |
| `max` | 149,000 | 218,000 |
| `neg` | - | 131,000 |
| `not` | - | 63 |
| `select` | - | 55,000 |
| `randEuint64` | - | 24,000 |
#### **128-bit Encrypted integers (`euint128`)**
| Function name | HCU (scalar) | HCU (non-scalar) |
| -------------- | ------------ | ---------------- |
| `add` | 172,000 | 259,000 |
| `sub` | 172,000 | 260,000 |
| `mul` | 696,000 | 1,686,000 |
| `div` | 1,225,000 | - |
| `rem` | 1,943,000 | - |
| `and` | 37,000 | 37,000 |
| `or` | 37,000 | 37,000 |
| `xor` | 37,000 | 37,000 |
| `shr` | 37,000 | 272,000 |
| `shl` | 37,000 | 272,000 |
| `rotr` | 37,000 | 283,000 |
| `rotl` | 37,000 | 278,000 |
| `eq` | 117,000 | 122,000 |
| `ne` | 117,000 | 122,000 |
| `ge` | 149,000 | 210,000 |
| `gt` | 150,000 | 218,000 |
| `le` | 150,000 | 218,000 |
| `lt` | 149,000 | 215,000 |
| `min` | 186,000 | 289,000 |
| `max` | 180,000 | 290,000 |
| `neg` | - | 168,000 |
| `not` | - | 130 |
| `select` | - | 57,000 |
| `randEuint128` | - | 25,000 |
#### **256-bit Encrypted integers (`euint256`)**
| Function name | HCU (scalar) | HCU (non-scalar) |
| -------------- | ------------ | ---------------- |
| `and` | 38,000 | 38,000 |
| `or` | 38,000 | 38,000 |
| `xor` | 39,000 | 39,000 |
| `shr` | 38,000 | 369,000 |
| `shl` | 39,000 | 378,000 |
| `rotr` | 40,000 | 375,000 |
| `rotl` | 38,000 | 378,000 |
| `eq` | 118,000 | 152,000 |
| `ne` | 117,000 | 150,000 |
| `neg` | - | 269,000 |
| `not` | - | 130 |
| `select` | - | 108,000 |
| `randEuint256` | - | 30,000 |
#### **Encrypted addresses (`euint160`)**
When using `eaddress` (internally represented as `euint160`), the HCU costs for equality and inequality checks and select are as follows:
| Function name | HCU (scalar) | HCU (non-scalar) |
| ------------- | ------------ | ---------------- |
| `eq` | 115,000 | 125,000 |
| `ne` | 115,000 | 124,000 |
| `select` | - | 83,000 |
## Additional Operations
| Function name | HCU |
| ---------------- | ------------- |
| `cast` | 32 |
| `trivialEncrypt` | 32 |
| `randBounded` | 23,000-30,000 |
# Encrypted Inputs
This document introduces the concept of encrypted inputs in the FHEVM, explaining their role, structure, validation process, and how developers can integrate them into smart contracts and applications.
Encrypted inputs are a core feature of FHEVM, enabling users to push encrypted data onto the blockchain while ensuring data confidentiality and integrity.
## What are encrypted inputs?
Encrypted inputs are data values submitted by users in ciphertext form. These inputs allow sensitive information to remain confidential while still being processed by smart contracts. They are accompanied by **Zero-Knowledge Proofs of Knowledge (ZKPoKs)** to ensure the validity of the encrypted data without revealing the plaintext.
### Key characteristics of encrypted inputs:
1. **Confidentiality**: Data is encrypted using the public FHE key, ensuring that only authorized parties can decrypt or process the values.
2. **Validation via ZKPoKs**: Each encrypted input is accompanied by a proof verifying that the user knows the plaintext value of the ciphertext, preventing replay attacks or misuse.
3. **Efficient packing**: All inputs for a transaction are packed into a single ciphertext in a user-defined order, optimizing the size and generation of the zero-knowledge proof.
## Parameters in encrypted functions
When a function in a smart contract is called, it may accept two types of parameters for encrypted inputs:
1. **`externalEbool`, `externalEaddress`,`externalEuintXX`**: Refers to the index of the encrypted parameter within the proof, representing a specific encrypted input handle.
2. **`bytes`**: Contains the ciphertext and the associated zero-knowledge proof used for validation.
Here’s an example of a Solidity function accepting multiple encrypted parameters:
```solidity
function exampleFunction(
externalEbool param1,
externalEuint64 param2,
externalEuint8 param3,
bytes calldata inputProof
) public {
// Function logic here
}
```
In this example, `param1`, `param2`, and `param3` are encrypted inputs for `ebool`, `euint64`, and `euint8` while `inputProof` contains the corresponding ZKPoK to validate their authenticity.
### Input Generation using Hardhat
In the below example, we use Alice's address to create the encrypted inputs and submits the transaction.
```typescript
import { fhevm } from "hardhat";
const input = fhevm.createEncryptedInput(contract.address, signers.alice.address);
input.addBool(canTransfer); // at index 0
input.add64(transferAmount); // at index 1
input.add8(transferType); // at index 2
const encryptedInput = await input.encrypt();
const externalEboolParam1 = encryptedInput.handles[0];
const externalEuint64Param2 = encryptedInput.handles[1];
const externalEuint8Param3 = encryptedInput.handles[2];
const inputProof = encryptedInput.inputProof;
tx = await myContract
.connect(signers.alice)
[
"exampleFunction(bytes32,bytes32,bytes32,bytes)"
](signers.bob.address, externalEboolParam1, externalEuint64Param2, externalEuint8Param3, inputProof);
await tx.wait();
```
### Input Order
Developers are free to design the function parameters in any order. There is no required correspondence between the order in which encrypted inputs are constructed in TypeScript and the order of arguments in the Solidity function.
## Validating encrypted inputs
Smart contracts process encrypted inputs by verifying them against the associated zero-knowledge proof. This is done using the `FHE.asEuintXX`, `FHE.asEbool`, or `FHE.asEaddress` functions, which validate the input and convert it into the appropriate encrypted type.
### Example validation
This example demonstrates a function that performs multiple encrypted operations, such as updating a user's encrypted balance and toggling an encrypted boolean flag:
```solidity
function myExample(externalEuint64 encryptedAmount, externalEbool encryptedToggle, bytes calldata inputProof) public {
// Validate and convert the encrypted inputs
euint64 amount = FHE.fromExternal(encryptedAmount, inputProof);
ebool toggleFlag = FHE.fromExternal(encryptedToggle, inputProof);
// Update the user's encrypted balance
balances[msg.sender] = FHE.add(balances[msg.sender], amount);
// Toggle the user's encrypted flag
userFlags[msg.sender] = FHE.not(toggleFlag);
// FHE permissions and function logic here
...
}
// Function to retrieve a user's encrypted balance
function getEncryptedBalance() public view returns (euint64) {
return balances[msg.sender];
}
// Function to retrieve a user's encrypted flag
function getEncryptedFlag() public view returns (ebool) {
return userFlags[msg.sender];
}
```
### Example validation in the `ConfidentialERC20.sol` smart contract
Here’s an example of a smart contract function that verifies an encrypted input before proceeding:
```solidity
function transfer(
address to,
externalEuint64 encryptedAmount,
bytes calldata inputProof
) public {
// Verify the provided encrypted amount and convert it into an encrypted uint64
euint64 amount = FHE.fromExternal(encryptedAmount, inputProof);
// Function logic here, such as transferring funds
...
}
```
### How validation works
1. **Input verification**:\
The `FHE.fromExternal` function ensures that the input is a valid ciphertext with a corresponding ZKPoK.
2. **Type conversion**:\
The function transforms `externalEbool`, `externalEaddress`, `externalEuintXX` into the appropriate encrypted type (`ebool`, `eaddress`, `euintXX`) for further operations within the contract.
## Best Practices
* **Input packing**: Minimize the size and complexity of zero-knowledge proofs by packing all encrypted inputs into a single ciphertext.
* **Frontend encryption**: Always encrypt inputs using the FHE public key on the client side to ensure data confidentiality.
* **Proof management**: Ensure that the correct zero-knowledge proof is associated with each encrypted input to avoid validation errors.
Encrypted inputs and their validation form the backbone of secure and private interactions in the FHEVM. By leveraging these tools, developers can create robust, privacy-preserving smart contracts without compromising functionality or scalability.
# Key features
This document provides an overview of key features of the FHEVM smart contract library.
### Configuration and initialization
Smart contracts using FHEVM require proper configuration and initialization:
* **Environment setup**: Import and inherit from environment-specific configuration contracts
* **Relayer configuration**: Configure secure relayer access for cryptographic operations
* **Initialization checks**: Validate encrypted variables are properly initialized before use
For more information see [Configuration](/protocol/solidity-guides/v0.10/docs/solidity-guides/configure).
### Encrypted data types
fhevm introduces encrypted data types compatible with Solidity:
* **Booleans**: `ebool`
* **Unsigned Integers**: `euint8`, `euint16`, `euint32`, `euint64`, `euint128`, `euint256`
* **Addresses**: `eaddress`
* **Input**: `externalEbool`, `externalEaddress`, `externalEuintXX` for handling encrypted input data
Encrypted data is represented as ciphertext handles, ensuring secure computation and interaction.
For more information see [use of encrypted types](/protocol/solidity-guides/v0.10/docs/solidity-guides/types).
### Casting types
fhevm provides functions to cast between encrypted types:
* **Casting between encrypted types**: `FHE.asEbool` converts encrypted integers to encrypted booleans
* **Casting to encrypted types**: `FHE.asEuintX` converts plaintext values to encrypted types
* **Casting to encrypted addresses**: `FHE.asEaddress` converts plaintext addresses to encrypted addresses
For more information see [use of encrypted types](/protocol/solidity-guides/v0.10/docs/solidity-guides/types).
### Confidential computation
fhevm enables symbolic execution of encrypted operations, supporting:
* **Arithmetic:** `FHE.add`, `FHE.sub`, `FHE.mul`, `FHE.min`, `FHE.max`, `FHE.neg`, `FHE.div`, `FHE.rem`
* Note: `div` and `rem` operations are supported only with plaintext divisors
* **Bitwise:** `FHE.and`, `FHE.or`, `FHE.xor`, `FHE.not`, `FHE.shl`, `FHE.shr`, `FHE.rotl`, `FHE.rotr`
* **Comparison:** `FHE.eq`, `FHE.ne`, `FHE.lt`, `FHE.le`, `FHE.gt`, `FHE.ge`
* **Advanced:** `FHE.select` for branching on encrypted conditions, `FHE.randEuintX` for on-chain randomness.
For more information on operations, see [Operations on encrypted types](https://github.com/zama-ai/fhevm/blob/release/0.10.x/docs/solidity-guides/operations.md).
For more information on conditional branching, see [Conditional logic in FHE](https://github.com/zama-ai/fhevm/blob/release/0.10.x/docs/solidity-guides/conditions.md).
For more information on random number generation, see [Generate Random Encrypted Numbers](https://github.com/zama-ai/fhevm/blob/release/0.10.x/docs/solidity-guides/random.md).
### Access control mechanism
fhevm enforces access control with a blockchain-based Access Control List (ACL):
* **Persistent access**: `FHE.allow`, `FHE.allowThis` grants permanent permissions for ciphertexts.
* **Transient access**: `FHE.allowTransient` provides temporary access for specific transactions.
* **Validation**: `FHE.isSenderAllowed` ensures that only authorized entities can interact with ciphertexts.
For more information see [ACL](/protocol/solidity-guides/v0.10/docs/solidity-guides/acl).
# migration
FHEVM v0.9 introduces major architectural changes, including:
* Removal of the Zama Oracle
* Introduction of a self-relaying public decryption workflow
* Unified `ZamaEthereumConfig` replacing `SepoliaConfig`
This guide explains what changed and how to migrate your project smoothly.
## What Changed in FHEVM v0.9?
Before diving into migration steps, it’s important to understand the main breaking change: public decryption is no longer handled by a Zama Oracle, but by your dApp’s off-chain logic.
### FHEVM v0.8 Oracle-Based Decryption
In FHEVM v0.8, the decryption process relies on a trusted **Oracle** to relay the decryption request and proof between the dApp and the Zama Key Management System (KMS). This approach abstracts the complexity but introduces an external dependency.
**Decryption Steps:**
| Step | Component | Action |
| ------ | ---------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **1.** | **dApp (Solidity)** | Calls `FHE.requestDecryption()` to signal a need for clear data. |
| **2.** | **Oracle** | Listens for the on-chain decryption request event. |
| **3.** | **Oracle (Off-chain)** | Performs the `publicDecryption` with the Zama KMS, retrieving the **clear values** and the **decryption proof**. |
| **4.** | **Oracle** | Calls the user-specified dApp **callback Solidity function** with the clear values and the associated proof. |
| **5.** | **dApp (Solidity Callback)** | Calls `FHE.verifySignatures()` to verify the authenticity of the clear values using the provided proof. |
> **Key takeaway for v0.8:** The Oracle is the trusted intermediary responsible for performing the off-chain decryption and submitting the result back to the dApp contract.
### FHEVM v0.9 Self-Relaying Decryption & dApp Responsibility
The FHEVM v0.9 architecture shifts to a **self-relaying model**, empowering the dApp client (the user) to execute the off-chain decryption and re-submission. This decentralizes the process and removes the dependency on a general-purpose Oracle.
**Example Scenario: Checking a Player's Encrypted Score**
Consider a **Game contract** where Alice's final score is stored encrypted on-chain. Alice needs to prove her clear score to claim a reward.
| Step | Component | Action |
| ------ | ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **1.** | **Game Contract (Solidity)** | An on-chain function is called to make Alice's encrypted score **publicly decryptable**. |
| **2.** | **Alice (Client/Off-chain)** | Alice fetches the publicly decryptable encrypted score from the Game contract. |
| **3.** | **Alice (Client/Off-chain)** | Alice or any third-party service uses the **`@zama-fhe/relayer-sdk`** to call the off-chain `publicDecrypt` function. This returns the clear score value and a **proof of decryption**. |
| **4.** | **Alice (Client/On-chain)** | Alice calls a function on the **Game contract** with the decrypted clear score and the proof. |
| **5.** | **Game Contract (Solidity)** | The contract calls `FHE.verifySignatures()` to **verify the score's validity** using the provided proof. |
| **6.** | **Game Contract (Solidity)** | If the score is valid, the contract executes the game logic (e.g., distributing Alice's prize). |
> **Key takeaway for FHEVM v0.9:** Decryption is a **user-driven, off-chain process**. The dApp client is responsible for off-chain decryption, fetching the proof, and relaying the result back on-chain for verification.
**Why this matters:** If your dApp previously relied on the Oracle, you must rewrite your decryption flow. The migration steps below guide you through this change.
## Migration Checklist
Here is a brief, ordered list of the steps required to successfully migrate your project to FHEVM v0.9:
1. **Update Dependencies:** Upgrade all key Zama FHE packages to their **프로젝트로 돌아가기