Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New package for general-purpose zero-knowledge circuits #76

Merged
merged 17 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions .yarn/patches/circomkit-npm-0.0.18-e33b63f27f.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
diff --git a/dist/utils/instantiate.js b/dist/utils/instantiate.js
index 33cdff152e8b23d6853eb475097bda8c02712e01..418f57072c9c0ebf3778c8a9a50d379728128e5f 100644
--- a/dist/utils/instantiate.js
+++ b/dist/utils/instantiate.js
@@ -1,15 +1,17 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-exports.instantiate = void 0;
-const fs_1 = require("fs");
+"use strict"
+Object.defineProperty(exports, "__esModule", { value: true })
+exports.instantiate = void 0
+const fs_1 = require("fs")
/** Circuit builder, kinda like `ejs.render`. **Be very careful when editing this file.** */
const makeCircuit = (config) => `// auto-generated by circomkit
pragma circom ${config.version};

include "../${config.file}.circom";

-component main${config.pubs.length === 0 ? '' : ' {public[' + config.pubs.join(', ') + ']}'} = ${config.template}(${config.params.join(', ')});
-`;
+component main${config.pubs.length === 0 ? "" : " {public[" + config.pubs.join(", ") + "]}"} = ${
+ config.template
+}(${config.params.join(", ")});
+`
/**
* Programmatically generate the `main` component of a circuit
* @param name name of the circuit to be generated
@@ -18,31 +20,32 @@ component main${config.pubs.length === 0 ? '' : ' {public[' + config.pubs.join('
*/
function instantiate(name, circuitConfig) {
// directory to output the file
- const directory = circuitConfig.dir || 'test';
+ const directory = circuitConfig.dir || "test"
// add "../" to the filename in include, one for each "/" in directory name
// if none, the prefix becomes empty string
- const filePrefixMatches = directory.match(/\//g);
- let file = circuitConfig.file;
+ const filePrefixMatches = directory.match(/\//g)
+ let file = circuitConfig.file
if (filePrefixMatches !== null) {
- file = '../'.repeat(filePrefixMatches.length) + file;
+ file = "../".repeat(filePrefixMatches.length) + file
}
const circuitCode = makeCircuit({
file: file,
template: circuitConfig.template,
- version: circuitConfig.version || '2.0.0',
+ version: circuitConfig.version || "2.0.0",
dir: directory,
pubs: circuitConfig.pubs || [],
- params: circuitConfig.params || [],
- });
- const targetDir = `./circuits/${directory}`;
+ params: circuitConfig.params || []
+ })
+ const targetDir = `./circom/${directory}`
if (!(0, fs_1.existsSync)(targetDir)) {
- (0, fs_1.mkdirSync)(targetDir, {
- recursive: true,
- });
+ ;(0, fs_1.mkdirSync)(targetDir, {
+ recursive: true
+ })
}
- const targetPath = `${targetDir}/${name}.circom`;
- (0, fs_1.writeFileSync)(targetPath, circuitCode);
- return targetPath;
+ const targetPath = `${targetDir}/${name}.circom`
+ ;(0, fs_1.writeFileSync)(targetPath, circuitCode)
+ return targetPath
}
-exports.instantiate = instantiate;
+exports.instantiate = instantiate
//# sourceMappingURL=instantiate.js.map
+
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,8 @@
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"resolutions": {
"[email protected]": "patch:circomkit@npm:0.0.18#.yarn/patches/circomkit-npm-0.0.18-e33b63f27f.patch"
}
}
4 changes: 4 additions & 0 deletions packages/circuits/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ptau
main
*.r1cs
*.wasm
21 changes: 21 additions & 0 deletions packages/circuits/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Ethereum Foundation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
55 changes: 55 additions & 0 deletions packages/circuits/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<p align="center">
<h1 align="center">
ZK-kit circuits
</h1>
<p align="center">A comprehensive library of general-purpose zero-knowledge circuits.</p>
</p>

<p align="center">
<a href="https://github.com/privacy-scaling-explorations/zk-kit">
<img src="https://img.shields.io/badge/project-zk--kit-blue.svg?style=flat-square">
</a>
<a href="https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/circuits.sol/LICENSE">
<img alt="Github license" src="https://img.shields.io/github/license/privacy-scaling-explorations/zk-kit.svg?style=flat-square">
</a>
<a href="https://www.npmjs.com/package/@zk-kit/circuits">
<img alt="NPM version" src="https://img.shields.io/npm/v/@zk-kit/circuits?style=flat-square" />
</a>
<a href="https://npmjs.org/package/@zk-kit/circuits">
<img alt="Downloads" src="https://img.shields.io/npm/dm/@zk-kit/circuits.svg?style=flat-square" />
</a>
</p>

<div align="center">
<h4>
<a href="https://appliedzkp.org/discord">
🗣️ Chat &amp; Support
</a>
</h4>
</div>


| This package offers a collection of reusable circuits designed for integration into other projects or protocols, promoting code modularization within the zero-knowledge ecosystem. |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

## Circuits

* Circom:
* [PoseidonProof](./circom/poseidon-proof.circom): It proves the possession of a Posidon pre-image without revealing the pre-image itself.
cedoor marked this conversation as resolved.
Show resolved Hide resolved
* [BinaryMerkleRoot](./circom/binary-merkle-root.circom): It calculates the root of a binary Merkle tree using a provided proof-of-membership.

## 🛠 Install

### npm or yarn

Install the `@zk-kit/circuits` package with npm:

```bash
npm i @zk-kit/circuits --save
```

or yarn:

```bash
yarn add @zk-kit/circuits
cedoor marked this conversation as resolved.
Show resolved Hide resolved
```
32 changes: 32 additions & 0 deletions packages/circuits/circom/binary-merkle-root.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
pragma circom 2.1.5;

include "poseidon.circom";
include "mux1.circom";
include "comparators.circom";

template BinaryMerkleRoot(MAX_DEPTH) {
signal input leaf, depth, indices[MAX_DEPTH], siblings[MAX_DEPTH];

signal output out;

signal nodes[MAX_DEPTH + 1];
nodes[0] <== leaf;

signal roots[MAX_DEPTH];
var root = 0;

for (var i = 0; i < MAX_DEPTH; i++) {
var a = IsEqual()([depth, i]);

roots[i] <== a * nodes[i];

root += roots[i];

var c[2][2] = [ [nodes[i], siblings[i]], [siblings[i], nodes[i]] ];
var childNodes[2] = MultiMux1(2)(c, indices[i]);

nodes[i + 1] <== Poseidon(2)(childNodes);
}

out <== root;
}
11 changes: 11 additions & 0 deletions packages/circuits/circom/circuits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"poseidon-proof": {
"file": "poseidon-proof",
"template": "PoseidonProof"
},
"binary-merkle-root": {
"file": "binary-merkle-root",
"template": "BinaryMerkleRoot",
"params": [20]
}
}
24 changes: 24 additions & 0 deletions packages/circuits/circom/poseidon-proof.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pragma circom 2.1.5;

include "poseidon.circom";

// This circuit can be used to prove the possession of a pre-image of a
// hash without revealing the pre-image itself. It utilizes the Poseidon
// hash function, a highly efficient and secure hash function suited
// for zero-knowledge proof contexts.
// A scope value can be used to define a nullifier to prevent the same
// proof from being re-used twice.
template PoseidonProof() {
// The circuit takes two inputs: the pre-image (in) and an additional scope parameter (scope).
signal input in;
signal input scope;

// It applies the Poseidon hash function to the pre-image to produce a hash output (out).
signal output out;
out <== Poseidon(1)([in]);

// A nullifier is also computed using both the pre-image and the scope, providing a value
// to prevent the same proof from being reused twice.
signal output nullifier;
nullifier <== Poseidon(2)([scope, in]);
}
17 changes: 17 additions & 0 deletions packages/circuits/circomkit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"protocol": "groth16",
"prime": "bn128",
"version": "2.1.5",
"circuits": "./circom/circuits.json",
"dirPtau": "./ptau",
"dirCircuits": "./circom",
"dirInputs": "./inputs",
"dirBuild": "./build",
"optimization": 1,
"inspect": true,
"include": ["../../node_modules/circomlib/circuits"],
"groth16numContributions": 1,
"groth16askForEntropy": false,
"logLevel": "INFO",
"verbose": true
}
cedoor marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
27 changes: 27 additions & 0 deletions packages/circuits/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@zk-kit/circuits",
"version": "0.2.0",
"description": "A comprehensive library of general-purpose zero-knowledge circuits.",
"license": "MIT",
"files": [
"circom",
"noir",
"LICENSE",
"README.md"
],
"repository": "[email protected]:privacy-scaling-explorations/zk-kit.git",
"homepage": "https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/circuits.sol",
"scripts": {
"circom:compile": "circomkit compile",
"circom:setup": "circomkit setup"
},
"dependencies": {
"circomlib": "^2.0.5"
},
"devDependencies": {
"circomkit": "^0.0.18"
},
"publishConfig": {
"access": "public"
}
}
66 changes: 64 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4399,6 +4399,15 @@ __metadata:
languageName: node
linkType: hard

"@zk-kit/circuits@workspace:packages/circuits":
version: 0.0.0-use.local
resolution: "@zk-kit/circuits@workspace:packages/circuits"
dependencies:
circomkit: ^0.0.18
circomlib: ^2.0.5
languageName: unknown
linkType: soft

"@zk-kit/[email protected], @zk-kit/groth16@workspace:packages/groth16":
version: 0.0.0-use.local
resolution: "@zk-kit/groth16@workspace:packages/groth16"
Expand Down Expand Up @@ -6072,7 +6081,7 @@ __metadata:
languageName: node
linkType: hard

"chai@npm:^4.2.0, chai@npm:^4.3.6":
"chai@npm:^4.2.0, chai@npm:^4.3.6, chai@npm:^4.3.7":
version: 4.3.10
resolution: "chai@npm:4.3.10"
dependencies:
Expand Down Expand Up @@ -6401,6 +6410,20 @@ __metadata:
languageName: node
linkType: hard

"circomkit@npm:^0.0.18":
version: 0.0.18
resolution: "circomkit@npm:0.0.18"
dependencies:
chai: ^4.3.7
circom_tester: ^0.0.19
loglevel: ^1.8.1
snarkjs: ^0.7.0
bin:
circomkit: dist/bin/index.js
checksum: 4120fdf8523f8d595a85e673bb74b8d683a302a401bf049ecb1a0ff4ad178e8f77ed7d12a50856c1be5f56bb8666f861a240d432f2ada78faa13dec161780cf1
languageName: node
linkType: hard

"circomlib@npm:0.5.1":
version: 0.5.1
resolution: "circomlib@npm:0.5.1"
Expand Down Expand Up @@ -9109,7 +9132,7 @@ __metadata:
languageName: node
linkType: hard

"ffjavascript@npm:^0.2.30, ffjavascript@npm:^0.2.35, ffjavascript@npm:^0.2.38":
"ffjavascript@npm:0.2.62, ffjavascript@npm:^0.2.30, ffjavascript@npm:^0.2.35, ffjavascript@npm:^0.2.38":
version: 0.2.62
resolution: "ffjavascript@npm:0.2.62"
dependencies:
Expand Down Expand Up @@ -13337,6 +13360,13 @@ __metadata:
languageName: node
linkType: hard

"loglevel@npm:^1.8.1":
version: 1.8.1
resolution: "loglevel@npm:1.8.1"
checksum: a1a62db40291aaeaef2f612334c49e531bff71cc1d01a2acab689ab80d59e092f852ab164a5aedc1a752fdc46b7b162cb097d8a9eb2cf0b299511106c29af61d
languageName: node
linkType: hard

"logplease@npm:^1.2.15":
version: 1.2.15
resolution: "logplease@npm:1.2.15"
Expand Down Expand Up @@ -15487,6 +15517,18 @@ __metadata:
languageName: node
linkType: hard

"r1csfile@npm:0.0.47":
version: 0.0.47
resolution: "r1csfile@npm:0.0.47"
dependencies:
"@iden3/bigarray": 0.0.2
"@iden3/binfileutils": 0.0.11
fastfile: 0.0.20
ffjavascript: 0.2.60
checksum: edeb325b83851a71cbca2e5de56eb622ee5347ecae921b526a5fc484c4825b6b30c73b6fde40e9bc5112b9d21e046af885bf212ed9cee2efbc6de93b8454ec06
languageName: node
linkType: hard

"randombytes@npm:^2.1.0":
version: 2.1.0
resolution: "randombytes@npm:2.1.0"
Expand Down Expand Up @@ -16825,6 +16867,26 @@ __metadata:
languageName: node
linkType: hard

"snarkjs@npm:^0.7.0":
version: 0.7.2
resolution: "snarkjs@npm:0.7.2"
dependencies:
"@iden3/binfileutils": 0.0.11
bfj: ^7.0.2
blake2b-wasm: ^2.4.0
circom_runtime: 0.1.24
ejs: ^3.1.6
fastfile: 0.0.20
ffjavascript: 0.2.62
js-sha3: ^0.8.0
logplease: ^1.2.15
r1csfile: 0.0.47
bin:
snarkjs: build/cli.cjs
checksum: c784e2171278403b2356ddc42fac47093e7cf4c48c0ef46ac3c269c308795d2da63a00dd6b92521b166f3d0349d0b8301454f5a9633b5db447755b4568c4b5e7
languageName: node
linkType: hard

"socks-proxy-agent@npm:^8.0.1":
version: 8.0.2
resolution: "socks-proxy-agent@npm:8.0.2"
Expand Down
Loading