@fhevm/hardhat-plugin-v3

中文文档
0.13.1发布于 2026/10/24BSD-3-Clause-ClearTypeScriptES Module构建来源已验证近 30 天下载 3176

FHEVM plugin for Hardhat 3: a cleartext FHEVM stack on every development connection, encryption, public and user decryption, error diagnostics, HCU accounting and the fhevm tasks.

原始使用文档
文档目录12 节
  1. List of peer dependencies
  2. Installation
  3. 1. Install
  4. 2. Create contracts/APlusB.sol
  5. 3. Create hardhat.config.ts
  6. 4. Create test/APlusB.ts
  7. 5. Run
  8. fhevm.helpers — encrypt
  9. fhevm.helpers — decrypt (user decryption, requires an ACL a…
  10. fhevm.helpers — decryptPublic (requires FHE.makePubliclyDec…
  11. fhevm.cleartextDb — read cleartexts directly (tests only, b…
  12. fhevm.client — the raw @fhevm/sdk client

FHEVM


📃 Read white paper | 📒 Documentation | 💛 Community support | 📚 FHE resources by Zama

FHEVM Hardhat 3 Plugin

NPM Version hardhat

Hardhat 3 plugin for developing and testing FHEVM contracts.

For documentation and resources:

List of peer dependencies

This Hardhat plugin relies on the following peer dependencies to function correctly.

  • @fhevm/sdk
  • hardhat
  • viem

If your package manager does not install peer dependencies automatically (pnpm or npm <7), add them to your package manually.

Installation

npm install --save-dev @fhevm/hardhat-plugin-v3

And register the plugin in your hardhat.config.ts:

import fhevmPlugin from '@fhevm/hardhat-plugin-v3';
import { defineConfig } from 'hardhat/config';

export default defineConfig({
  plugins: [fhevmPlugin],
});

🚀 Quick Start: Hello World with FHEVM

A minimal project: a contract that stores two encrypted values, adds them, and a test that decrypts the sum.

1. Install

npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox-mocha-ethers @fhevm/hardhat-plugin-v3 @fhevm/sdk viem
npm install @fhevm/solidity

2. Create contracts/APlusB.sol

// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.24;

import { FHE, euint8, externalEuint8 } from "@fhevm/solidity/lib/FHE.sol";
import { ZamaEthereumConfig } from "@fhevm/solidity/config/ZamaConfig.sol";

contract APlusB is ZamaEthereumConfig {
  euint8 private _a;
  euint8 private _b;
  euint8 private _aplusb;

  function setA(externalEuint8 inputA, bytes calldata inputProof) external {
    _a = FHE.fromExternal(inputA, inputProof);
    FHE.allowThis(_a);
  }

  function setB(externalEuint8 inputB, bytes calldata inputProof) external {
    _b = FHE.fromExternal(inputB, inputProof);
    FHE.allowThis(_b);
  }

  function computeAPlusB() external {
    _aplusb = FHE.add(_a, _b);
    FHE.allowThis(_aplusb);
    FHE.allow(_aplusb, msg.sender);
  }

  function aplusb() public view returns (euint8) {
    return _aplusb;
  }
}

3. Create hardhat.config.ts

See the Hardhat 3 configuration guide for the full reference.

import fhevmPlugin from '@fhevm/hardhat-plugin-v3';
import hardhatToolboxMochaEthers from '@nomicfoundation/hardhat-toolbox-mocha-ethers';
import { defineConfig } from 'hardhat/config';

export default defineConfig({
  plugins: [hardhatToolboxMochaEthers, fhevmPlugin],
  solidity: {
    version: '0.8.27',
    // ⚠️ FHEVM requires at least the "cancun" EVM version
    settings: { evmVersion: 'cancun' },
  },
});

4. Create test/APlusB.ts

In Hardhat 3 the FHEVM API lives on the network connection, not on hre.

import { expect } from 'chai';
import { network } from 'hardhat';

const { ethers, fhevm } = await network.connect();

describe('APlusB', function () {
  it('uint8: add 80 to 123 should equal 203', async function () {
    const [alice] = await ethers.getSigners();
    const contract = await ethers.deployContract('APlusB');
    const contractAddress = await contract.getAddress();

    // 1. Encrypt and store `a`
    const a = await fhevm.helpers.encryptUint8({ value: 80, contractAddress, userAddress: alice.address });
    await (await contract.setA(a.externalEuint8, a.inputProof)).wait();

    // 2. Encrypt and store `b`
    const b = await fhevm.helpers.encryptUint8({ value: 123, contractAddress, userAddress: alice.address });
    await (await contract.setB(b.externalEuint8, b.inputProof)).wait();

    // 3. Compute the FHE sum on chain
    await (await contract.computeAPlusB()).wait();

    // 4. Decrypt the result as alice
    const clear = await fhevm.helpers.decryptUint8({
      euint8: await contract.aplusb(),
      contractAddress,
      userAddress: alice.address,
    });

    expect(clear).to.eq(203n);
  });
});

5. Run

npx hardhat test

🧰 Plugin API cheat sheet

All helpers live on the connection's fhevm object: const { fhevm } = await network.connect();

fhevm.helpers — encrypt

Each takes { value, contractAddress, userAddress } and returns { external<Type>, inputProof }.

Functionvalue
encryptBoolboolean
encryptUint8number | bigint
encryptUint16number | bigint
encryptUint32number | bigint
encryptUint64number | bigint
encryptUint128number | bigint
encryptUint256number | bigint
encryptAddressstring

fhevm.helpers — decrypt (user decryption, requires an ACL allow + a permit)

Each takes { <handle>, contractAddress, userAddress, options? }.

FunctionHandle argReturns
decryptBooleboolboolean
decryptUint8euint8number
decryptUint16euint16number
decryptUint32euint32number
decryptUint64euint64bigint
decryptUint128euint128bigint
decryptUint256euint256bigint
decryptAddresseaddress`0x${string}`

fhevm.helpers — decryptPublic (requires FHE.makePubliclyDecryptable)

Each takes { <handle> } only.

FunctionHandle argReturns
decryptPublicBooleboolboolean
decryptPublicUint8euint8number
decryptPublicUint16euint16number
decryptPublicUint32euint32number
decryptPublicUint64euint64bigint
decryptPublicUint128euint128bigint
decryptPublicUint256euint256bigint
decryptPublicAddresseaddress`0x${string}`

Every decryptPublic* has a decryptPublic*WithSignatures twin returning { clearValue, checkSignaturesArgs }, for testing on-chain signature verification.

fhevm.cleartextDb — read cleartexts directly (tests only, bypasses the ACL)

Each takes { <handle> } only and answers even when nobody is allowed to see the value.

FunctionHandle argReturns
readBooleboolboolean
readUint8euint8number
readUint16euint16number
readUint32euint32number
readUint64euint64bigint
readUint128euint128bigint
readUint256euint256bigint
readAddresseaddress`0x${string}`

fhevm.client — the raw @fhevm/sdk client

The helpers above are convenience wrappers. When you need something they do not cover — or when you want to test the @fhevm/sdk API itself — reach for fhevm.client, the underlying FhevmClient from @fhevm/sdk, and call its actions directly.

const { fhevm } = await network.connect();
const { encryptedValues, inputProof } = await fhevm.client.encryptValues({
  values: [{ type: 'euint8', value: 80 }],
  contractAddress,
  userAddress,
});

📘 FHEVM Documentation and Examples

For more FHEVM examples and detailed documentation please go here

版本与发布记录

@fhevm/hardhat-plugin-v3 共发布 5 个版本,首次发布于 2026/09/09;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.13.1latest2026/10/2422 天前
0.13.02026/10/071 个月前
0.13.0-1alpha2026/09/261 个月前
0.13.0-02026/09/171 个月前
0.0.02026/09/092 个月前

关于 @fhevm/hardhat-plugin-v3 的常见问题

@fhevm/hardhat-plugin-v3 是什么?

FHEVM plugin for Hardhat 3: a cleartext FHEVM stack on every development connection, encryption, public and user decryption, error diagnostics, HCU accounting and the fhevm tasks.

如何安装 @fhevm/hardhat-plugin-v3?

在项目目录运行 npm install @fhevm/hardhat-plugin-v3,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:viem(^2.55.19)、hardhat(^3.0.0)、@fhevm/sdk(^0.13.8)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fhevm/hardhat-plugin-v3 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fhevm/hardhat-plugin-v3 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@fhevm/hardhat-plugin-v3 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.23.4。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@fhevm/hardhat-plugin-v3 可以商用吗?

@fhevm/hardhat-plugin-v3 使用 BSD-3-Clause-Clear 许可。声明的许可为 BSD-3-Clause-Clear,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@fhevm/hardhat-plugin-v3 的下载量有多少?

根据 npm 官方下载统计,@fhevm/hardhat-plugin-v3 在 2026/10/15 – 2026/11/13 这 30 天内被下载 3,176 次,日均约 106 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fhevm/hardhat-plugin-v3 还在维护吗?

最新版本 0.13.1 发布于 2026/10/24(22 天前);至今共发布 5 个版本,其中过去 12 个月发布了 5 个;首次发布于 2026/09/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。