-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(circuits): create new package for circuits
- Loading branch information
Showing
13 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,8 @@ | |
"circom" | ||
], | ||
"workspaces": [ | ||
"packages/*" | ||
"packages/*", | ||
"packages/circuits/templates" | ||
], | ||
"packageManager": "[email protected]", | ||
"devDependencies": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.r1cs | ||
*.wasm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
templates/LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
templates/README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pragma circom 2.1.5; | ||
|
||
include "../templates/binary-merkle-root.circom"; | ||
|
||
component main {public [depth]} = BinaryMerkleRoot(16); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pragma circom 2.1.5; | ||
|
||
include "../templates/poseidon-proof.circom"; | ||
|
||
component main {public [scope]} = PoseidonProof(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "circuits", | ||
"private": true, | ||
"description": "A comprehensive library of general-purpose Circom circuits.", | ||
"license": "MIT", | ||
"scripts": { | ||
"compile": "circom --r1cs -l ../../node_modules/circomlib/circuits" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<p align="center"> | ||
<h1 align="center"> | ||
ZK-kit circuits | ||
</h1> | ||
<p align="center">A comprehensive library of general-purpose Circom 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> | ||
<a href="https://bundlephobia.com/package/@zk-kit/circuits"> | ||
<img alt="npm bundle size (scoped)" src="https://img.shields.io/bundlephobia/minzip/@zk-kit/circuits" /> | ||
</a> | ||
</p> | ||
|
||
<div align="center"> | ||
<h4> | ||
<a href="https://appliedzkp.org/discord"> | ||
🗣️ Chat & Support | ||
</a> | ||
</h4> | ||
</div> | ||
|
||
--- | ||
|
||
## 🛠 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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "@zk-kit/circuits", | ||
"version": "0.1.0", | ||
"description": "A comprehensive library of general-purpose Circom circuits.", | ||
"license": "MIT", | ||
"files": [ | ||
"**/*.circom", | ||
"LICENSE", | ||
"README.md" | ||
], | ||
"keywords": [ | ||
"zk-kit", | ||
"circom", | ||
"circuits" | ||
], | ||
"repository": "[email protected]:privacy-scaling-explorations/zk-kit.git", | ||
"homepage": "https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/circuits.sol", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"circomlib": "^2.0.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters