Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ build
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# types
types
26 changes: 13 additions & 13 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ jobs:
node-version: 20
cache: yarn

# https://github.com/iden3/circuits/blob/8fffb6609ecad0b7bcda19bb908bdb544bdb3cf7/.github/workflows/main.yml#L18-L22
# https://stackoverflow.com/a/78377916
- name: Setup Circom deps
run: |
sudo rm /etc/apt/sources.list.d/microsoft-prod.list
sudo apt-get update && sudo apt-get install -y wget nlohmann-json3-dev libgmp-dev nasm g++ build-essential

- name: Setup Circom
run: wget https://github.com/iden3/circom/releases/latest/download/circom-linux-amd64 && sudo mv ./circom-linux-amd64 /usr/bin/circom && sudo chmod +x /usr/bin/circom

- name: Install dependencies
run: yarn

Expand All @@ -48,20 +58,10 @@ jobs:
- name: Test libraries
run: yarn test:libraries

- name: Coveralls
uses: coverallsapp/github-action@v2.2.3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
flag-name: run-libraries
parallel: true

coverage:
runs-on: ubuntu-latest
needs: test
- name: Test circuits
run: yarn test:circuits

steps:
- name: Coveralls Finished
- name: Coveralls
uses: coverallsapp/github-action@v2.2.3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true
13 changes: 13 additions & 0 deletions .github/workflows/pull-requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ jobs:
node-version: 20
cache: yarn

# https://github.com/iden3/circuits/blob/8fffb6609ecad0b7bcda19bb908bdb544bdb3cf7/.github/workflows/main.yml#L18-L22
# https://stackoverflow.com/a/78377916
- name: Setup Circom deps
run: |
sudo rm /etc/apt/sources.list.d/microsoft-prod.list
sudo apt-get update && sudo apt-get install -y wget nlohmann-json3-dev libgmp-dev nasm g++ build-essential

- name: Setup Circom
run: wget https://github.com/iden3/circom/releases/latest/download/circom-linux-amd64 && sudo mv ./circom-linux-amd64 /usr/bin/circom && sudo chmod +x /usr/bin/circom

- name: Install dependencies
run: yarn

Expand All @@ -45,3 +55,6 @@ jobs:

- name: Test libraries
run: yarn test

- name: Test circuits
run: yarn test:circuits
3 changes: 2 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"semi": false,
"arrowParens": "always",
"trailingComma": "none"
"trailingComma": "none",
"plugins": ["prettier-plugin-solidity"]
}
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@
</a>
</td>
</tr>
<tr>
<td>
<a href="https://github.com/semaphore-protocol/extensions/tree/main/packages/identity-proof.circom">
@semaphore-extensions/identity-proof.circom
</a>
</td>
<td>
<!-- NPM version -->
<a href="https://www.npmjs.com/package/@semaphore-extensions/identity-proof.circom">
<img alt="NPM Version" src="https://img.shields.io/npm/v/%40semaphore-extensions%2Fidentity-proof.circom">
</a>
</td>
<td>
<!-- Downloads -->
<a href="https://npmjs.org/package/@semaphore-extensions/identity-proof.circom">
<img alt="NPM Downloads" src="https://img.shields.io/npm/dm/%40semaphore-extensions%2Fidentity-proof.circom">
</a>
</td>
</tr>
<tbody>
</table>

Expand Down
4 changes: 2 additions & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs"
import type { Config } from "jest"

const exclude = ["circuits", "contracts"]
const exclude = ["identity-proof.circom"]

const projects: any = fs
.readdirSync("./packages", { withFileTypes: true })
Expand All @@ -21,7 +21,7 @@ const projects: any = fs
const config: Config = {
projects,
verbose: true,
coverageDirectory: "./coverage/libraries",
coverageDirectory: "./coverage",
coverageThreshold: {
global: {
branches: 90,
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"scripts": {
"build": "yarn build:libraries",
"build:libraries": "yarn workspaces foreach -A -t --no-private run build",
"test": "yarn test:libraries",
"test": "yarn test:libraries && yarn test:circuits",
"test:libraries": "jest --coverage",
"test:library": "jest packages/${0}",
"test:circuits": "yarn workspace @semaphore-extensions/identity-proof.circom test",
"lint:eslint": "eslint . --ext .js,.ts,.tsx",
"lint": "yarn lint:eslint",
"lint:fix": "yarn lint:eslint --fix",
Expand Down Expand Up @@ -35,6 +36,7 @@
"@commitlint/config-conventional": "^19.1.0",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.4",
"@types/snarkjs": "^0",
"@typescript-eslint/eslint-plugin": "^7.5.0",
"@typescript-eslint/parser": "^7.5.0",
"changelogithub": "patch:changelogithub@npm%3A0.13.5#~/.yarn/patches/changelogithub-npm-0.13.5-9884734389.patch",
Expand All @@ -52,7 +54,9 @@
"jest": "^29.7.0",
"lint-staged": "^15.2.2",
"prettier": "^3.2.5",
"prettier-plugin-solidity": "^1.3.1",
"rimraf": "^5.0.5",
"snarkjs": "^0.7.4",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typedoc": "^0.25.13",
Expand Down
3 changes: 3 additions & 0 deletions packages/identity-proof.circom/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ptau
main
test
7 changes: 7 additions & 0 deletions packages/identity-proof.circom/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extension": ["ts"],
"require": "ts-node/register",
"spec": "./tests/*.test.ts",
"timeout": 100000,
"exit": true
}
21 changes: 21 additions & 0 deletions packages/identity-proof.circom/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 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.
58 changes: 58 additions & 0 deletions packages/identity-proof.circom/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<p align="center">
<h1 align="center">
Semaphore Identity Proof (circuit)
</h1>
<p align="center">A circuit to prove the ownership of a Semaphore identity without revealing the private key.</p>
</p>

<p align="center">
<a href="https://github.com/semaphore-protocol">
<img src="https://img.shields.io/badge/project-Semaphore-blue.svg?style=flat-square">
</a>
<a href="https://github.com/semaphore-protocol/extensions/tree/main/packages/identity-proof.circom/LICENSE">
<img alt="NPM license" src="https://img.shields.io/npm/l/%40semaphore-extensions%2Fidentity-proof.circom?style=flat-square">
</a>
<a href="https://www.npmjs.com/package/@semaphore-extensions/identity-proof.circom">
<img alt="NPM version" src="https://img.shields.io/npm/v/@semaphore-extensions/identity-proof.circom?style=flat-square" />
</a>
<a href="https://npmjs.org/package/@semaphore-extensions/identity-proof.circom">
<img alt="Downloads" src="https://img.shields.io/npm/dm/@semaphore-extensions/identity-proof.circom.svg?style=flat-square" />
</a>
</p>

<div align="center">
<h4>
<a href="https://github.com/semaphore-protocol/extensions/blob/main/CONTRIBUTING.md">
👥 Contributing
</a>
<span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
<a href="https://github.com/semaphore-protocol/extensions/blob/main/CODE_OF_CONDUCT.md">
🤝 Code of conduct
</a>
<span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
<a href="https://github.com/semaphore-protocol/extensions/contribute">
🔎 Issues
</a>
<span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
<a href="https://semaphore.pse.dev/discord">
🗣️ Chat &amp; Support
</a>
</h4>
</div>

> [!WARNING]
> This library has **not** been audited.

## 🛠 Install

Install the `@semaphore-extensions/identity-proof.circom` package with npm:

```bash
npm i @semaphore-extensions/identity-proof.circom --save
```

or yarn:

```bash
yarn add @semaphore-extensions/identity-proof.circom
```
17 changes: 17 additions & 0 deletions packages/identity-proof.circom/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": "./circuits.json",
"dirPtau": "./ptau",
"dirCircuits": "./src",
"dirInputs": "./inputs",
"dirBuild": "./build",
"optimization": 2,
"inspect": true,
"include": ["../../node_modules/circomlib/circuits"],
"groth16numContributions": 1,
"groth16askForEntropy": false,
"logLevel": "INFO",
"verbose": true
}
7 changes: 7 additions & 0 deletions packages/identity-proof.circom/circuits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"identity-proof": {
"file": "identity-proof",
"template": "IdentityProof",
"pubs": ["scope"]
}
}
36 changes: 36 additions & 0 deletions packages/identity-proof.circom/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@semaphore-extensions/identity-proof.circom",
"version": "1.0.0-beta",
"description": "A circuit to prove the ownership of a Semaphore identity without revealing the private key.",
"license": "MIT",
"files": [
"**/*.circom",
"!main",
"!test",
"LICENSE",
"README.md"
],
"repository": "https://github.com/semaphore-protocol/extensions",
"homepage": "https://github.com/semaphore-protocol/extensions/tree/main/packages/identity-proof.circom",
"bugs": {
"url": "https://github.com/semaphore-protocol/extensions.git/issues"
},
"scripts": {
"compile": "circomkit compile identity-proof",
"setup": "circomkit setup identity-proof",
"test": "mocha"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"circomlib": "2.0.5"
},
"devDependencies": {
"@types/mocha": "^10.0.6",
"@zk-kit/eddsa-poseidon": "^1.0.0-beta",
"circomkit": "^0.0.19",
"mocha": "^10.2.0",
"poseidon-lite": "^0.2.0"
}
}
37 changes: 37 additions & 0 deletions packages/identity-proof.circom/src/identity-proof.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pragma circom 2.1.5;

include "babyjub.circom";
include "poseidon.circom";
include "comparators.circom";

// This circuit can be used to prove the possession of a Semaphore private key
// without revealing the private key itself. It utilizes
// the {@link https://eips.ethereum.org/EIPS/eip-2494|Baby Jubjub} elliptic curve, the Poseidon hash function which 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 IdentityProof() {
// The circuit takes two inputs: the private key and an additional scope parameter.
signal input secret;
signal input scope;

signal output commitment;

// The secret scalar must be in the prime subgroup order 'l'.
var l = 2736030358979909402780800718157159386076813972158567259200215660948447373041;

component isLessThan = LessThan(251);
isLessThan.in <== [secret, l];
isLessThan.out === 1;

var Ax, Ay;

// Get the two Baby Jubjub points using the private key.
(Ax, Ay) = BabyPbk()(secret);

// It applies the Poseidon hash function to the to Baby Jubjub points to produce the commitment.
commitment <== Poseidon(2)([Ax, Ay]);

// Dummy constraint to prevent compiler from optimizing it.
signal dummySquare <== scope * scope;
}
11 changes: 11 additions & 0 deletions packages/identity-proof.circom/tests/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Circomkit } from "circomkit"
import { readFileSync } from "fs"
import path from "path"

const configFilePath = path.join(__dirname, "../circomkit.json")
const config = JSON.parse(readFileSync(configFilePath, "utf-8"))

export const circomkit = new Circomkit({
...config,
verbose: false
})
36 changes: 36 additions & 0 deletions packages/identity-proof.circom/tests/identity-proof.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { WitnessTester } from "circomkit"
import { poseidon2 } from "poseidon-lite"
import { deriveSecretScalar, derivePublicKey } from "@zk-kit/eddsa-poseidon"
import { circomkit } from "./common"

describe("identity-proof", () => {
let circuit: WitnessTester<["secret", "scope"], ["commitment"]>

const privateKey = Buffer.from("secret")
const scope = 2

const publicKey = derivePublicKey(privateKey)
const commitment = poseidon2(publicKey)

const INPUT = {
secret: deriveSecretScalar(privateKey),
scope
}

const OUTPUT = {
commitment
}

before(async () => {
circuit = await circomkit.WitnessTester("identity-proof", {
file: "identity-proof",
template: "IdentityProof",
pubs: ["scope"]
})
})

// eslint-disable-next-line jest/expect-expect
it("Should compute the commitment correctly", async () => {
await circuit.expectPass(INPUT, OUTPUT)
})
})
Loading