bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 48쪽
const countAfterInc = await counterContract.getCount();
// expect(countAfterInc).to.eq(countBeforeInc + 1n);
// });
// it("decrement the counter by 1", async function () {
// // First increment, count becomes 1
// let tx = await counterContract.connect(signers.alice).increment();
// await tx.wait();
// // Then decrement, count goes back to 0
// tx = await counterContract.connect(signers.alice).decrement(1);
// await tx.wait();
// const count = await counterContract.getCount();
// expect(count).to.eq(0);
// });
});
```
#### What’s Different from `Counter.ts`?
* This test file is structurally similar to the original `Counter.ts`, but it uses the FHEVM-compatible smart contract `FHECounter` instead of the regular `Counter`.
– For clarity, the `Counter` unit tests are included as comments, allowing you to better understand how each part is adapted during the migration to FHEVM.
* While the test logic remains the same, this version is now set up to support encrypted computations via the FHEVM library — enabling tests that manipulate confidential values directly on-chain.
{% endstep %}
{% step %}
### Run the test `test/FHECounter.ts`
From your project's root directory, run:
```sh
npx hardhat test
```
Output:
```sh
FHECounter
FHECounter has been deployed at address 0x7553CB9124f974Ee475E5cE45482F90d5B6076BC
✔ should be deployed
1 passing (1ms)
```
Great! Your Hardhat FHEVM test environment is properly setup.
{% endstep %}
{% endstepper %}
## Test functions
Now everything is up and running, you can start testing your contract functions.
{% stepper %}
{% step %}
### Call the contract `getCount()` view function
Replace the commented‐out test for the legacy `Counter` contract:
```ts
// it("count should be zero after deployment", async function () {
// const count = await counterContract.getCount();
// console.log(`Counter.getCount() === ${count}`);
// // Expect initial count to be 0 after deployment
// expect(count).to.eq(0);
// });
```
with its FHEVM equivalent:
```ts
it("encrypted count should be uninitialized after deployment", async function () {
const encryptedCount = await fheCounterContract.getCount();
// Expect initial count to be bytes32(0) after deployment,
// (meaning the encrypted count value is uninitialized)
expect(encryptedCount).to.eq(ethers.ZeroHash);
});
```
**What’s different?**
– `encryptedCount` is no longer a plain TypeScript number. It is now a hexadecimal string representing a Solidity `bytes32` value, known as an **FHEVM handle**. This handle points to an encrypted FHEVM primitive of type `euint32`, which internally represents an encrypted Solidity `uint32` primitive type.
* `encryptedCount` is equal to `0x0000000000000000000000000000000000000000000000000000000000000000` which means that `encryptedCount` is uninitialized, and does not reference to any encrypted value at this point.
**Run the test**
From your project's root directory, run:
```sh
npx hardhat test
```
**Expected Output**
```sh
Counter
Counter has been deployed at address 0x7553CB9124f974Ee475E5cE45482F90d5B6076BC
✔ should be deployed
✔ encrypted count should be uninitialized after deployment
2 passing (7ms)
```
{% endstep %}
{% step %}
### Setup the `increment()` function unit test
We’ll migrate the `increment()` unit test to FHEVM step by step. To start, let’s handle the value of the counter before the first increment. As explained above, the counter is initially a `bytes32` value equal to zero, meaning the FHEVM `euint32` variable is uninitialized.
We’ll interpret this as if the underlying clear value is 0.
Replace the commented‐out test for the legacy `Counter` contract:
```ts
// it("increment the counter by 1", async function () {
// const countBeforeInc = await counterContract.getCount();
// const tx = await counterContract.connect(signers.alice).increment(1);
// await tx.wait();
// const countAfterInc = await counterContract.getCount();
// expect(countAfterInc).to.eq(countBeforeInc + 1n);
// });
```
with the following:
```ts
it("increment the counter by 1", async function () {
const encryptedCountBeforeInc = await fheCounterContract.getCount();
expect(encryptedCountBeforeInc).to.eq(ethers.ZeroHash);
const clearCountBeforeInc = 0;
// const tx = await counterContract.connect(signers.alice).increment(1);
// await tx.wait();
// const countAfterInc = await counterContract.getCount();
// expect(countAfterInc).to.eq(countBeforeInc + 1n);
});
```
{% endstep %}
{% step %}
### Encrypt the `increment()` function argument
The `increment()` function takes a single argument: the value by which the counter should be incremented. In the initial version of `Counter.sol`, this value is a clear `uint32`.
We’ll switch to passing an encrypted value instead, using FHEVM `externalEuint32` primitive type. This allows us to securely increment the counter without revealing the input value on-chain.
{% hint style="info" %}
We are using an `externalEuint32` instead of a regular `euint32`. This tells the FHEVM that the encrypted `uint32` was provided externally (e.g., by a user) and must be verified for integrity and authenticity before it can be used within the contract.
{% endhint %}
Replace :
```ts
it("increment the counter by 1", async function () {
const encryptedCountBeforeInc = await fheCounterContract.getCount();
expect(encryptedCountBeforeInc).to.eq(ethers.ZeroHash);
const clearCountBeforeInc = 0;
// const tx = await counterContract.connect(signers.alice).increment(1);
// await tx.wait();
// const countAfterInc = await counterContract.getCount();
// expect(countAfterInc).to.eq(countBeforeInc + 1n);
});
```
with the following:
```ts
it("increment the counter by 1", async function () {
const encryptedCountBeforeInc = await fheCounterContract.getCount();
expect(encryptedCountBeforeInc).to.eq(ethers.ZeroHash);
const clearCountBeforeInc = 0;
// Encrypt constant 1 as a euint32
const clearOne = 1;
const encryptedOne = await fhevm
.createEncryptedInput(fheCounterContractAddress, signers.alice.address)
.add32(clearOne)
.encrypt();
// const tx = await counterContract.connect(signers.alice).increment(1);
// await tx.wait();
// const countAfterInc = await counterContract.getCount();
// expect(countAfterInc).to.eq(countBeforeInc + 1n);
});
```
{% hint style="info" %}
`fhevm.createEncryptedInput(fheCounterContractAddress, signers.alice.address)` creates an encrypted value that is bound to both the contract (`fheCounterContractAddress`) and the user (`signers.alice.address`). This means only Alice can use this encrypted value, and only within the `FHECounter.sol` contract at that specific address. **It cannot be reused by another user or in a different contract, ensuring data confidentiality and binding context-specific encryption.**
{% endhint %}
{% endstep %}
{% step %}
### Call the `increment()` function with the encrypted argument
Now that we have an encrypted argument, we can call the `increment()` function with it.
Below, you’ll notice that the updated `increment()` function now takes **two arguments instead of one.**
This is because the FHEVM requires both:
1. The `externalEuint32` — the encrypted value itself
2. An accompanying **Zero-Knowledge Proof of Knowledge** (`inputProof`) — which verifies that the encrypted input is securely bound to:
* the caller (Alice, the transaction signer), and
* the target smart contract (where `increment()` is being executed)
This ensures that the encrypted value cannot be reused in a different context or by a different user, preserving **confidentiality and integrity.**
Replace :
```ts
// const tx = await counterContract.connect(signers.alice).increment(1);
// await tx.wait();
```
with the following:
```ts
const tx = await fheCounterContract.connect(signers.alice).increment(encryptedOne.handles[0], encryptedOne.inputProof);
await tx.wait();
```
At this point the counter has been successfully incremented by 1 using a **Fully Homomorphic Encryption (FHE)**. In the next step, we will retrieve the updated encrypted counter value and decrypt it locally. But before we move on, let’s quickly run the tests to make sure everything is working correctly.
***
**Run the test**
From your project's root directory, run:
```sh
npx hardhat test
```
**Expected Output**
```sh
FHECounter
FHECounter has been deployed at address 0x7553CB9124f974Ee475E5cE45482F90d5B6076BC
✔ should be deployed
✔ encrypted count should be uninitialized after deployment
✔ increment the counter by 1
3 passing (7ms)
```
{% endstep %}
{% step %}
### Call the `getCount()` function and Decrypt the value
Now that the counter has been incremented using an encrypted input, it's time to **read the updated encrypted value** from the smart contract and **decrypt it** using the `userDecryptEuint` function provided by the FHEVM Hardhat Plugin.
The `userDecryptEuint` function takes four parameters:
1. **FhevmType**: The integer type of the FHE-encrypted value. In this case, we're using `FhevmType.euint32` because the counter is a `uint32`.
2. **Encrypted handle**: A 32-byte FHEVM handle representing the encrypted value you want to decrypt.
3. **Smart contract address**: The address of the contract that has permission to access the encrypted handle.
4. **User signer**: The signer (e.g., signers.alice) who has permission to access the handle.
{% hint style="info" %}
Note: Permissions to access the FHEVM handle are set on-chain using the `FHE.allow()` Solidity function (see FHECounter.sol).
{% endhint %}
Replace :
```ts
// const countAfterInc = await counterContract.getCount();
// expect(countAfterInc).to.eq(countBeforeInc + 1n);
```
with the following:
```ts
const encryptedCountAfterInc = await fheCounterContract.getCount();
const clearCountAfterInc = await fhevm.userDecryptEuint(
FhevmType.euint32,
encryptedCountAfterInc,
fheCounterContractAddress,
signers.alice,
);
expect(clearCountAfterInc).to.eq(clearCountBeforeInc + clearOne);
```
***
**Run the test**
From your project's root directory, run:
```sh
npx hardhat test
```
**Expected Output**
```sh
FHECounter
FHECounter has been deployed at address 0x7553CB9124f974Ee475E5cE45482F90d5B6076BC
✔ should be deployed
✔ encrypted count should be uninitialized after deployment
✔ increment the counter by 1
3 passing (7ms)
```
{% endstep %}
{% step %}
### Call the contract `decrement()` function
Similarly to the previous test, we’ll now call the `decrement()` function using an encrypted input.
Replace :
```ts
// it("decrement the counter by 1", async function () {
// // First increment, count becomes 1
// let tx = await counterContract.connect(signers.alice).increment();
// await tx.wait();
// // Then decrement, count goes back to 0
// tx = await counterContract.connect(signers.alice).decrement(1);
// await tx.wait();
// const count = await counterContract.getCount();
// expect(count).to.eq(0);
// });
```
with the following:
```ts
it("decrement the counter by 1", async function () {
// Encrypt constant 1 as a euint32
const clearOne = 1;
const encryptedOne = await fhevm
.createEncryptedInput(fheCounterContractAddress, signers.alice.address)
.add32(clearOne)
.encrypt();
// First increment by 1, count becomes 1
let tx = await fheCounterContract.connect(signers.alice).increment(encryptedOne.handles[0], encryptedOne.inputProof);
await tx.wait();
// Then decrement by 1, count goes back to 0
tx = await fheCounterContract.connect(signers.alice).decrement(encryptedOne.handles[0], encryptedOne.inputProof);
await tx.wait();
const encryptedCountAfterDec = await fheCounterContract.getCount();
const clearCountAfterDec = await fhevm.userDecryptEuint(
FhevmType.euint32,
encryptedCountAfterDec,
fheCounterContractAddress,
signers.alice,
);
expect(clearCountAfterDec).to.eq(0);
});
```
***
**Run the test**
From your project's root directory, run:
```sh
npx hardhat test
```
**Expected Output**
```sh
FHECounter
FHECounter has been deployed at address 0x7553CB9124f974Ee475E5cE45482F90d5B6076BC
✔ should be deployed
✔ encrypted count should be uninitialized after deployment
✔ increment the counter by 1
✔ decrement the counter by 1
4 passing (7ms)
```
{% endstep %}
{% endstepper %}
## Congratulations! You've completed the full tutorial.
You have successfully written and tested your FHEVM-based counter smart contract. By now, your project should include the following files:
* [`contracts/FHECounter.sol`](https://docs.zama.ai/protocol/examples#tab-fhecounter.sol) — your Solidity smart contract
* [`test/FHECounter.ts`](https://docs.zama.ai/protocol/examples#tab-fhecounter.ts) — your Hardhat test suite written in TypeScript
## Next step
If you would like to deploy your project on the testnet, or learn more about using FHEVM Hardhat Plugin, head to [Deploy contracts and run tests](/protocol/solidity-guides/v0.10/docs/solidity-guides/hardhat/run_test).
# Turn it into FHEVM
In this tutorial, you'll learn how to take a basic Solidity smart contract and progressively upgrade it to support Fully Homomorphic Encryption using the FHEVM library by Zama.
Starting with the plain `Counter.sol` contract that you built from the ["Write a simple contract" tutorial](/protocol/solidity-guides/v0.10/docs/solidity-guides/getting-started/quick-start-tutorial/write_a_simple_contract), and step-by-step, you’ll learn how to:
* Replace standard types with encrypted equivalents
* Integrate zero-knowledge proof validation
* Enable encrypted on-chain computation
* Grant permissions for secure off-chain decryption
By the end, you'll have a fully functional smart contract that supports FHE computation.
## Initiate the contract
{% stepper %}
{% step %}
### Create the `FHECounter.sol` file
Navigate to your project’s `contracts` directory:
```sh
cd <your-project-root-directory>/contracts
```
From there, create a new file named `FHECounter.sol`, and copy the following Solidity code into it:
```solidity
// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.24;
/// @title A simple counter contract
contract Counter {
uint32 private _count;
/// @notice Returns the current count
function getCount() external view returns (uint32) {
return _count;
}
/// @notice Increments the counter by a specific value
function increment(uint32 value) external {
_count += value;
}
/// @notice Decrements the counter by a specific value
function decrement(uint32 value) external {
require(_count >= value, "Counter: cannot decrement below zero");
_count -= value;
}
}
```
This is a plain `Counter` contract that we’ll use as the starting point for adding FHEVM functionality. We will modify this contract step-by-step to progressively integrate FHEVM capabilities.
{% endstep %}
{% step %}
### Turn `Counter` into `FHECounter`
To begin integrating FHEVM features into your contract, we first need to import the required FHEVM libraries.
**Replace the current header**
```solidity
// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.24;
```
**With this updated header:**
```solidity
// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.24;
import { FHE, euint32, externalEuint32 } from "@fhevm/solidity/lib/FHE.sol";
import { ZamaEthereumConfig } from "@fhevm/solidity/config/ZamaConfig.sol";
```
These imports:
* **FHE** — the core library to work with FHEVM encrypted types
* **euint32** and **externalEuint32** — encrypted uint32 types used in FHEVM
* **ZamaEthereumConfig** — provides the FHEVM configuration for the Ethereum mainnet or Ethereum Sepolia testnet networks.\
Inheriting from it enables your contract to use the FHE library
**Replace the current contract declaration:**
```solidity
/// @title A simple counter contract
contract Counter {
```
**With the updated declaration :**
```solidity
/// @title A simple FHE counter contract
contract FHECounter is ZamaEthereumConfig {
```
This change:
* Renames the contract to `FHECounter`
* Inherits from `ZamaEthereumConfig` to enable FHEVM support
{% hint style="warning" %}
This contract must inherit from the `ZamaEthereumConfig` abstract contract; otherwise, it will not be able to execute any FHEVM-related functionality on Sepolia or Hardhat.
{% endhint %}
From your project's root directory, run:
```sh
npx hardhat compile
```
Great! Your smart contract is now compiled and ready to use **FHEVM features.**
{% endstep %}
{% endstepper %}
## Apply FHE functions and types
{% stepper %}
{% step %}
### Comment out the `increment()` and `decrement()` Functions
Before we move forward, let’s comment out the `increment()` and `decrement()` functions in `FHECounter`. We'll replace them later with updated versions that support FHE-encrypted operations.
```solidity
/// @notice Increments the counter by a specific value
// function increment(uint32 value) external {
// _count += value;
// }
/// @notice Decrements the counter by a specific value
// function decrement(uint32 value) external {
// require(_count >= value, "Counter: cannot decrement below zero");
// _count -= value;
// }
```
{% endstep %}
{% step %}
### Replace `uint32` with the FHEVM `euint32` Type
We’ll now switch from the standard Solidity `uint32` type to the encrypted FHEVM type `euint32`.
This enables private, homomorphic computation on encrypted integers.
**Replace**
```solidity
uint32 _count;
```
and
```solidity
function getCount() external view returns (uint32) {
```
**With :**
```solidity
euint32 _count;
```
and
```solidity
function getCount() external view returns (euint32) {
```
{% endstep %}
{% step %}
### Replace `increment(uint32 value)` with the FHEVM version `increment(externalEuint32 value)`
To support encrypted input, we will update the increment function to accept a value encrypted off-chain.
Instead of using a `uint32`, the new version will accept an `externalEuint32`, which is an encrypted integer produced off-chain and sent to the smart contract.
To ensure the validity of this encrypted value, we also include a second argument:`inputProof`, a bytes array containing a Zero-Knowledge Proof of Knowledge (ZKPoK) that proves two things:
1. The `externalEuint32` was encrypted off-chain by the function caller (`msg.sender`)
2. The `externalEuint32` is bound to the contract (`address(this)`) and can only be processed by it.
**Replace**
```solidity
/// @notice Increments the counter by a specific value
// function increment(uint32 value) external {
// _count += value;
// }
```
**With :**
```solidity
/// @notice Increments the counter by a specific value
function increment(externalEuint32 inputEuint32, bytes calldata inputProof) external {
// _count += value;
}
```
{% endstep %}
{% step %}
### Convert `externalEuint32` to `euint32`
You cannot directly use `externalEuint32` in FHE operations. To manipulate it with the FHEVM library, you first need to convert it into the native FHE type `euint32`.
This conversion is done using:
```solidity
FHE.fromExternal(inputEuint32, inputProof);
```
This method verifies the zero-knowledge proof and returns a usable encrypted value within the contract.
**Replace**
```solidity
/// @notice Increments the counter by a specific value
function increment(externalEuint32 inputEuint32, bytes calldata inputProof) external {
// _count += value;
}
```
**With :**
```solidity
/// @notice Increments the counter by a specific value
function increment(externalEuint32 inputEuint32, bytes calldata inputProof) external {
euint32 evalue = FHE.fromExternal(inputEuint32, inputProof);
// _count += value;
}
```
{% endstep %}
{% step %}
### Convert `_count += value` into its FHEVM equivalent
To perform the update `_count += value` in a Fully Homomorphic way, we use the `FHE.add()` operator. This function allows us to compute the FHE sum of 2 encrypted integers.
**Replace**
```solidity
/// @notice Increments the counter by a specific value
function increment(externalEuint32 inp프로젝트로 돌아가기