Skip to content

Commit

Permalink
chore: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
olehmisar committed Oct 23, 2024
0 parents commit a535a97
Show file tree
Hide file tree
Showing 24 changed files with 3,679 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# EditorConfig is awesome: https://EditorConfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = false
31 changes: 31 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Lint Test

on: [push]

jobs:
build:
runs-on: ubuntu-20.04
strategy:
matrix:
node-version: [20]
steps:
- uses: actions/checkout@v3

- uses: pnpm/action-setup@v4
with:
version: 9

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "pnpm"

- name: Install dependencies
run: pnpm install

- name: Lint
run: pnpm lint

- name: Test
run: pnpm test
68 changes: 68 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/dist

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# next.js build output
.next

# environment variables
.env

cache
target
artifacts
.vscode
5 changes: 5 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": "ts-node/register/files",
"ignore": ["test/fixture-projects/**/*"],
"timeout": 60000
}
58 changes: 58 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Hardhat TypeScript plugin boilerplate

This is a sample Hardhat plugin written in TypeScript. Creating a Hardhat plugin
can be as easy as extracting a part of your config into a different file and
publishing it to npm.

This sample project contains an example on how to do that, but also comes with
many more features:

- A mocha test suite ready to use
- TravisCI already setup
- A package.json with scripts and publishing info
- Examples on how to do different things

## Installation

To start working on your project, just run

```bash
npm install
```

## Plugin development

Make sure to read our [Plugin Development Guide](https://hardhat.org/advanced/building-plugins.html) to learn how to build a plugin.

## Testing

Running `npm run test` will run every test located in the `test/` folder. They
use [mocha](https://mochajs.org) and [chai](https://www.chaijs.com/),
but you can customize them.

We recommend creating unit tests for your own modules, and integration tests for
the interaction of the plugin with Hardhat and its dependencies.

## Linting and autoformat

All of Hardhat projects use [prettier](https://prettier.io/) and
[tslint](https://palantir.github.io/tslint/).

You can check if your code style is correct by running `npm run lint`, and fix
it with `npm run lint:fix`.

## Building the project

Just run `npm run build` ️👷

## README file

This README describes this boilerplate project, but won't be very useful to your
plugin users.

Take a look at `README-TEMPLATE.md` for an example of what a Hardhat plugin's
README should look like.

## Migrating from Buidler?

Take a look at [the migration guide](MIGRATION.md)!
164 changes: 164 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<img align="right" width="150" height="150" top="100" src="./assets/banner.jpg">

# Hardhat Noir

Develop [Noir](https://noir-lang.org) with [Hardhat](https://hardhat.org) without hassle.

## What

Write programs in Noir, generate Solidity verifiers and run tests.

This plugin automatically manages `nargo` and `bb` versions and compiles Noir on demand.

## Installation

Install the plugin and Noir dependencies:

```bash
npm install hardhat-plugin-noir @noir-lang/noir_js @noir-lang/backend_barretenberg
```

Import the plugin in your `hardhat.config.js`:

```js
require("hardhat-plugin-noir");
```

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

```ts
import "hardhat-plugin-noir";
```

**You must enable Solidity optimizer in order to be able to deploy Solidity verifier contracts.**

```js
const config: HardhatUserConfig = {
solidity: {
version: "0.8.27",
settings: {
optimizer: {
enabled: true,
runs: 100000000,
},
},
},
};
```

## Usage

To get started, create a Noir circuit in `noir` folder:

```bash
npx hardhat noir-new my_noir
```

It will create `noir/my_noir` folder with the following `src/main.nr`:

```rs
fn main(x: Field, y: pub Field) {
assert(x != y);
}
```

This circuit will prove that the private input `x` is not equal to the public input `y` using a zero-knowledge proof.

Compile Noir(it will also generate a Solidity verifier):

```bash
npx hardhat compile
```

Use the verifier contract in Solidity:

```solidity
// contracts/MyContract.sol
import {UltraVerifier} from "../noir/target/my_noir.sol";
contract MyContract {
UltraVerifier public verifier = new UltraVerifier();
function verify(bytes calldata proof, uint256 y) external view returns (bool) {
bytes32[] memory publicInputs = new bytes32[](1);
publicInputs[0] = bytes32(y);
bool result = verifier.verify(proof, publicInputs);
return result;
}
}
```

Generate a proof in TypeScript and verify it on chain:

```js
// test/MyContract.test.ts
import { expect } from "chai";
import hre, { ethers } from "hardhat";

it("proves and verifies on-chain", async () => {
// Deploy a verifier contract
const contractFactory = await ethers.getContractFactory("MyContract");
const contract = await contractFactory.deploy();
await contract.waitForDeployment();

// Generate a proof
const { noir, backend } = await hre.noir.getCircuit("my_noir");
const input = { x: 1, y: 2 };
const { witness } = await noir.execute(input);
const { proof, publicInputs } = await backend.generateProof(witness);
// it matches because we marked y as `pub` in `main.nr`
expect(BigInt(publicInputs[0])).to.eq(BigInt(input.y));

// Verify the proof on-chain
const result = await contract.verify(proof, input.y);
expect(result).to.eq(true);

// You can also verify in JavaScript.
const resultJs = await backend.verifyProof({
proof,
publicInputs: [String(input.y)],
});
expect(resultJs).to.eq(true);
});
```

## Tasks

This plugin creates no additional tasks. Run `hardhat compile` to compile Noir.

<!-- This plugin adds the _example_ task to Hardhat:
```
output of `npx hardhat help example`
``` -->

## Environment extensions

This plugin extends the Hardhat Runtime Environment by adding a `noir` field.

You can call `hre.noir.getCircuit(name)` to get a compiled circuit JSON.

## Configuration

Configure Noir and Barretenberg (bb) versions in `hardhat.config.ts`:

```js
export default {
noir: {
// Noir version, optional, will use the latest known Noir version by default
version: "0.35.0",
// bb version, optional, will be inferred if possible
bbVersion: "0.57.0",
},
};
```

The default folder where Noir is located is `noir`. You can change it in `hardhat.config.js`:

```js
export default {
paths: {
noir: "circuits",
},
};
```
Binary file added assets/banner.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "hardhat-plugin-noir",
"version": "0.0.1",
"description": "Hardhat plugin for Noir language",
"repository": "github:olehmisar/hardhat-noir",
"author": "Oleh Misarosh <olehmisar@gmail.com>",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"keywords": [
"ethereum",
"smart-contracts",
"hardhat",
"hardhat-plugin",
"noir",
"aztec",
"zero knowledge",
"zk"
],
"scripts": {
"lint:fix": "prettier --write 'src/**/*.{js,ts}' 'test/**/*.{js,ts}'",
"lint": "tsc --noEmit && prettier --check 'src/**/*.{js,ts}' 'test/**/*.{js,ts}'",
"test": "mocha --exit --recursive 'test/**/*.test.ts'",
"build": "rm -rf dist && tsc",
"watch": "tsc -w",
"prepublishOnly": "npm run build"
},
"files": [
"dist/src/",
"src/",
"LICENSE",
"README.md"
],
"devDependencies": {
"@noir-lang/backend_barretenberg": "^0.36.0",
"@noir-lang/noir_js": "^0.36.0",
"@noir-lang/types": "^0.36.0",
"@types/chai": "^5.0.0",
"@types/mocha": "^10.0.9",
"@types/node": "^22.7.9",
"chai": "^4.2.0",
"hardhat": "^2.0.0",
"mocha": "^10.7.3",
"prettier": "3.3.3",
"ts-node": "^10.9.2",
"typescript": "^5.6.3"
},
"peerDependencies": {
"hardhat": "^2.0.0"
},
"dependencies": {
"glob": "^11.0.0",
"zod": "^3.23.8"
}
}
Loading

0 comments on commit a535a97

Please sign in to comment.