Skip to content

Commit 410052c

Browse files
committed
feat: rewrite fhevm-foundry-template using forge-fhevm
Replace the old FFI-based template with a new FHECounter example that uses forge-fhevm for native Foundry testing. Mirrors the fhevm-hardhat-template structure and test coverage.
1 parent e419e3e commit 410052c

23 files changed

Lines changed: 309 additions & 2264 deletions

.env.example

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
export PRIVATE_KEY_ALICE="0x3cf7bdaece87a1f56473ef4c6ba039da2beb78c43ea5051f7ad97982f21b5521"
2-
export PRIVATE_KEY_BOB="0xa5ee0b7fac5c148658c77af611e0f7cbed47ae8278a24097b1aca104bdc3aa97"
3-
export SEPOLIA_RPC_URL="https://sepolia.infura.io/v3/xxxxxxxxxxxxxxxxxxxxxxxxxxx"
1+
# Deployer
2+
DEPLOYER_PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 # Anvil first account
3+
4+
# RPC
5+
RPC_URL=http://localhost:8545
6+
7+
# Etherscan (optional, for contract verification)
8+
# ETHERSCAN_API_KEY=

.gitignore

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
cache/
33
out/
44

5-
# Ignores development broadcast logs
6-
!/broadcast
7-
/broadcast/*/31337/
8-
/broadcast/*/11155111/
9-
/broadcast/**/dry-run/
5+
# Dependencies
6+
dependencies/
107

11-
# Docs
12-
docs/
13-
14-
# Dotenv file
8+
# Env
159
.env
1610

17-
node_modules/
11+
# Docs
12+
docs/book/
13+
14+
# Misc
15+
.DS_Store

.gitmodules

Lines changed: 0 additions & 3 deletions
This file was deleted.

LICENSE

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ Redistribution and use in source and binary forms, with or without modification,
77
are permitted provided that the following conditions are met:
88

99
1. Redistributions of source code must retain the above copyright notice, this
10-
list of conditions and the following disclaimer.
10+
list of conditions and the following disclaimer.
1111

1212
2. Redistributions in binary form must reproduce the above copyright notice, this
13-
list of conditions and the following disclaimer in the documentation and/or other
14-
materials provided with the distribution.
13+
list of conditions and the following disclaimer in the documentation and/or other
14+
materials provided with the distribution.
1515

1616
3. Neither the name of ZAMA nor the names of its contributors may be used to endorse
17-
or promote products derived from this software without specific prior written permission.
17+
or promote products derived from this software without specific prior written permission.
1818

1919
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
2020
THIS SOFTWARE IS PROVIDED BY THE ZAMA AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR

README.md

Lines changed: 86 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,97 @@
1-
# fhevm-foundry-template
1+
# FHEVM Foundry Template
22

3-
This repo is a POC showing how to use fhevmjs functions inside Solidity Foundry scripts. There are two scripts that you can run on Sepolia test network, after setting up your `.env` file (please reuse same keys but different values than the ones given inside `.env.example` and install all npm packages):
4-
The fist one shows how NOT to use reencrypt in a script:
3+
A Foundry-based template for developing Fully Homomorphic Encryption (FHE) enabled Solidity smart contracts using the
4+
FHEVM protocol by Zama.
55

6-
```
7-
forge script script/MyConfidentialERC20Reencrypt.s.sol --rpc-url $SEPOLIA_RPC_URL --ffi --broadcast
8-
```
6+
## Quick Start
97

10-
This script will run but it won't work "as expected" and will log a `0` for the minted balance by Alice instead of the correct value of `1000` which corresponds to the minted amount. This is unavoidable, since in Foundry FFI calls (and console logs) are always happening during the first simulation step, and will never happen during the real transaction broadcasting phase, so getting a 0 value is unavoidable, since the reencrypt function from fhevmjs depends on the real onchain state of Sepolia, after the mint transaction has been validated (see foundry team explanation [here](https://github.com/foundry-rs/foundry/issues/5776#issuecomment-1867287499)). You can still get the correct reencrypted value after the script is done by running:
8+
For detailed instructions see:
9+
[FHEVM Foundry Quick Start Tutorial](https://docs.zama.ai/protocol/solidity-guides/getting-started/quick-start-tutorial)
1110

12-
```
13-
ts-node --transpile-only utils/reencrypt.ts [HANDLE] [PRIVATE_KEY] [CONTRACT_ADDRESS]
14-
```
11+
### Prerequisites
1512

16-
All of the [HANDLE], [PRIVATE_KEY] and [CONTRACT_ADDRESS] values will be correctly logged by the previously run script.
13+
- **Foundry**: [Installation guide](https://book.getfoundry.sh/getting-started/installation)
1714

18-
On the other hand, we have a second script doing user input encryption (using fhevmjs via FFI) + mint + transfer of a confidential erc20, this script runs successfully as expected, because for encryption, contrarily to reencryption/decryption, data does NOT depend on the onchain Sepolia state. You can run it via:
15+
### Installation
1916

20-
```
21-
forge script script/MyConfidentialERC20Encrypt.s.sol --rpc-url $SEPOLIA_RPC_URL --ffi --broadcast
22-
```
17+
1. **Install dependencies**
18+
19+
```bash
20+
forge soldeer install
21+
```
22+
23+
2. **Compile and test**
24+
25+
```bash
26+
forge build
27+
forge test -vvv
28+
```
29+
30+
3. **Deploy to local network**
31+
32+
Start an Anvil node with the FHEVM host contracts deployed (requires [forge-fhevm](../forge-fhevm)):
33+
34+
```bash
35+
# From the forge-fhevm directory
36+
./deploy-local.sh
37+
```
38+
39+
Then deploy to the local network:
2340

24-
And you can still check that Alice's and Bob's balances are correct after Alice transferred 42 encrypted tokens to Bob via:
41+
```bash
42+
forge script script/DeployFHECounter.s.sol --rpc-url http://localhost:8545 --broadcast
43+
```
44+
45+
4. **Deploy to Sepolia Testnet**
46+
47+
```bash
48+
cp .env.example .env
49+
# Edit .env with your deployer key and RPC URL
50+
51+
source .env
52+
forge script script/DeployFHECounter.s.sol \
53+
--rpc-url $RPC_URL \
54+
--private-key $DEPLOYER_PRIVATE_KEY \
55+
--broadcast --verify
56+
```
57+
58+
## Project Structure
2559

2660
```
27-
ts-node --transpile-only utils/reencrypt.ts [HANDLE] [PRIVATE_KEY] [CONTRACT_ADDRESS]
61+
fhevm-foundry-template/
62+
├── src/ # Smart contract source files
63+
│ └── FHECounter.sol # Example FHE counter contract
64+
├── test/ # Test files
65+
│ └── FHECounter.t.sol # Tests using forge-fhevm
66+
├── script/ # Deployment scripts
67+
│ └── DeployFHECounter.s.sol
68+
├── foundry.toml # Foundry configuration
69+
└── remappings.txt # Dependency remappings
2870
```
71+
72+
## Available Scripts
73+
74+
| Script | Description |
75+
| ------------------------------------------ | ------------------------ |
76+
| `forge build` | Compile all contracts |
77+
| `forge test -vvv` | Run all tests |
78+
| `forge test --match-test test_name -vvv` | Run a single test |
79+
| `forge fmt` | Format code |
80+
| `forge fmt --check` | Check formatting |
81+
82+
## Documentation
83+
84+
- [FHEVM Documentation](https://docs.zama.ai/fhevm)
85+
- [FHEVM Foundry Setup Guide](https://docs.zama.ai/protocol/solidity-guides/getting-started/setup)
86+
- [FHEVM Testing Guide](https://docs.zama.ai/protocol/solidity-guides/development-guide/foundry/write_test)
87+
- [forge-fhevm Documentation](../forge-fhevm/docs/)
88+
89+
## License
90+
91+
This project is licensed under the BSD-3-Clause-Clear License. See the [LICENSE](LICENSE) file for details.
92+
93+
## Support
94+
95+
- **GitHub Issues**: [Report bugs or request features](https://github.com/zama-ai/fhevm/issues)
96+
- **Documentation**: [FHEVM Docs](https://docs.zama.ai)
97+
- **Community**: [Zama Discord](https://discord.gg/zama)

foundry.toml

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
[profile.default]
22
src = "src"
33
out = "out"
4-
libs = ["lib"]
4+
libs = ["dependencies"]
5+
allow_paths = ["../forge-fhevm"]
6+
test = "test"
7+
script = "script"
8+
solc = "0.8.27"
9+
evm_version = "cancun"
10+
optimizer = true
11+
optimizer_runs = 800
12+
cbor_metadata = false
13+
bytecode_hash = "none"
514

6-
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
15+
[fuzz]
16+
runs = 256
17+
18+
[fmt]
19+
line_length = 120
20+
21+
[dependencies]
22+
"@openzeppelin-contracts" = "5.1.0"
23+
"@openzeppelin-contracts-upgradeable" = "5.1.0"
24+
forge-std = "1.14.0"
25+
"@openzeppelin-confidential-contracts" = { version = "6edd293", git = "https://github.com/OpenZeppelin/openzeppelin-confidential-contracts.git", rev = "6edd293" }
26+
"@encrypted-types" = "0.0.4"
27+
"@fhevm-solidity" = "0.11.1"
28+
29+
[soldeer]
30+
remappings_version = false
31+
recursive_deps = true

lib/forge-std

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)