hardhat-circom

Overview of the hardhat-circom npm package

Circom can be a lot to grasp

If you are like most individuals starting their ZK journey then it can be a bit overwhelming. In the case of circom you have to learn an entirely new language with a unique syntactical structure. On top of this there are many repetitive commands used for the trusted setup ceremony, key generation, creating proofs, and verifying proofs. An initial read of the circom documentation is not sufficient for digesting all the steps in the process and one may find themselves returning back to the documentation over and over again.

hardhat-circom to the rescue

Luckily hardhat-circom provides a solution to all the repetition. It is an npm package that integrates abstracts snarkjs and circom so that it can be used to effortless create proofs. The hefty commands required in the proof setup are replaced with a configuration file where one can specify where the output files will be written to, proving scheme, and much more

Setup

Quoted from hardhat-cirom repository

Installation

npm install hardhat-circom

Import the plugin in your hardhat.config.js:

require("hardhat-circom");

Or if you are using TypeScript, in your hardhat.config.ts:

import "hardhat-circom";

Tasks

This plugin adds the circom task to build circuit(s) into wasm and zkey file and template them to seperate Verifier contracts saved to the Hardhat sources directory (usually contracts/).

Usage: hardhat [GLOBAL OPTIONS] circom --circuit <STRING> [--debug] [--deterministic]

OPTIONS:

  --circuit      	limit your circom task to a single circuit name
  --debug        	output intermediate files to artifacts directory, generally for debug
  --deterministic	enable deterministic builds for groth16 protocol circuits (except for .wasm)

circom: compile circom circuits and template Verifier

For global options help run: hardhat help

You must run hardhat circom at least once to build the assets before compiling or deploying your contracts. Additionally, you can hook Hardhat's compile task to build your circuits before every compile, see Hooking compile below.

Basic configuration

Set up your project (we'll use best_dapp_ever/) with the following minimal hardhat.config.js at the root. The two required properties are ptau (see Powers of Tau) and circuits.

module.exports = {
  solidity: "0.6.7",
  circom: {
    // (optional) Base path for input files, defaults to `./circuits/`
    inputBasePath: "./circuits",
    // (required) The final ptau file, relative to inputBasePath, from a Phase 1 ceremony
    ptau: "pot15_final.ptau",
    // (required) Each object in this array refers to a separate circuit
    circuits: [{ name: "init" }],
  },
};

Your project structure should look like this:

j:~/best_dapp_ever/ $ tree
└── circuits
    ├── init.circom
    ├── init.json
    └── pot15_final.ptau

Now, you can use npx hardhat circom --verbose to compile the circuits and output InitVerifier.sol, init.zkey, and init.wasm files into their respective directories:

j:~/best_dapp_ever/ $ tree
├── circuits
│   ├── init.circom
│   ├── init.json
│   ├── init.wasm
│   ├── init.zkey
│   └── pot15_final.ptau
└── contracts
    └── InitVerifier.sol

Source code: https://github.com/projectsophon/hardhat-circom

Last updated