Skip to content

Commit

Permalink
Merge pull request #34 from erhant/erhant/instantiate-custom-dir
Browse files Browse the repository at this point in the history
`instantiate` bugfix & refactors, NPM workflow, slight refactors
  • Loading branch information
erhant authored Nov 29, 2023
2 parents 0473d88 + c39a015 commit 2e7ae21
Show file tree
Hide file tree
Showing 20 changed files with 474 additions and 909 deletions.
8 changes: 0 additions & 8 deletions .editorconfig

This file was deleted.

54 changes: 51 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,55 @@
{
"extends": "./node_modules/gts/",
"extends": ["eslint:recommended", "plugin:node/recommended", "prettier"],
"plugins": ["node", "prettier"],
"rules": {
"node/no-unpublished-import": ["off"],
"node/no-unpublished-require": ["off"]
"prettier/prettier": "error",
"block-scoped-var": "error",
"eqeqeq": "error",
"no-var": "error",
"prefer-const": "error",
"eol-last": "error",
"prefer-arrow-callback": "error",
"no-trailing-spaces": "error",
"quotes": ["warn", "single", {"avoidEscape": true}],
"no-restricted-properties": [
"error",
{
"object": "describe",
"property": "only"
},
{
"object": "it",
"property": "only"
}
]
},
"overrides": [
{
"files": ["**/*.ts", "**/*.tsx"],
"parser": "@typescript-eslint/parser",
"extends": ["plugin:@typescript-eslint/recommended"],
"rules": {
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-warning-comments": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/camelcase": "off",
"node/no-missing-import": "off",
"node/no-empty-function": "off",
"node/no-unsupported-features/es-syntax": "off",
"node/no-missing-require": "off",
"node/shebang": "off",
"no-dupe-class-members": "off",
"require-atomic-updates": "off"
}
}
],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
}
}
24 changes: 24 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Publish to NPM

on:
release:
types: [published]

jobs:
publish:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v3
with:
node-version: '20'
registry-url: 'https://registry.npmjs.org'

- run: yarn
- run: yarn build
- uses: JS-DevTools/npm-publish@v3
id: publish
with:
token: ${{ secrets.NPM_TOKEN }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ dist
.DS_Store

# ignore auto generated test circuits
circuits/test
**/test/*.circom

# for init tests
tmptest
Expand Down
5 changes: 4 additions & 1 deletion .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
...require('gts/.prettierrc.json'),
bracketSpacing: false,
singleQuote: true,
trailingComma: 'es5',
arrowParens: 'avoid',
printWidth: 120,
};
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Changelog

## [0.0.18] - TBD
## [0.0.19] - 2023-XX-XX

- Fixes [#33](https://github.com/erhant/circomkit/issues/33), now `instantiate` respect the custom `dirCircuits` configuration. The function is moved within the Circomkit class, instead of being at its own file under `utils/instantiate.ts`.
- Fixes [#31](https://github.com/erhant/circomkit/issues/31).
- Slight refactor on the CLI switch-case condition.
- Update typescript to `^5.3.2` (note that TS doesnt use semver).
- Removed `gts` and downloaded `eslint` & `prettier` manually.
- `circomkit.instantiate` now respects existing `dir` and `version` in circuit config.
- Added NPM publish workflow.

## [0.0.18] - 2023-08-30

- Removed linting & styling from tests, GTS broke something regarding prettier perhaps? Lints pass locally but not at the workflow.
- Changed `init` logic to be a git clone instead.
Expand Down
64 changes: 34 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,14 @@
</p>

<p align="center">
<a href="https://opensource.org/licenses/MIT" target="_blank">
<img src="https://img.shields.io/badge/license-MIT-yellow.svg">
</a>
<a href="https://www.npmjs.com/package/circomkit" target="_blank">
<a href="https://www.npmjs.com/package/circomkit" target="_blank">
<img alt="NPM" src="https://img.shields.io/npm/v/circomkit?logo=npm&color=CB3837">
</a>
<a href="./.github/workflows/tests.yml" target="_blank">
<img alt="Workflow: Tests" src="https://github.com/erhant/circomkit/actions/workflows/tests.yml/badge.svg?branch=main">
</a>
<a href="https://github.com/iden3/snarkjs" target="_blank">
<img alt="GitHub: SnarkJS" src="https://img.shields.io/badge/github-snarkjs-lightgray?logo=github">
</a>
<a href="https://github.com/iden3/circom" target="_blank">
<img alt="GitHub: Circom" src="https://img.shields.io/badge/github-circom-lightgray?logo=github">
<a href="https://opensource.org/licenses/MIT" target="_blank">
<img src="https://img.shields.io/badge/license-MIT-blue.svg">
</a>
</p>

Expand Down Expand Up @@ -99,7 +93,11 @@ Everything used by Circomkit can be optionally overridden by providing the selec
npx circomkit config
```

You can edit any of the fields there to fit your needs. Most importantly, you can change the protocol to be `groth16`, `plonk` or `fflonk`; and you can change the underlying prime field to `bn128`, `bls12381` and `goldilocks`. Note that using a prime other than `bn128` makes things a bit harder in circuit-specific setup, as you will have to find the PTAU files yourself, whereas in `bn128` we can use [Perpetual Powers of Tau](https://github.com/privacy-scaling-explorations/perpetualpowersoftau).
You can edit any of the fields there to fit your needs. Most importantly, you can change the protocol to be `groth16`, `plonk` or `fflonk`; and you can change the underlying prime field to `bn128`, `bls12381` and `goldilocks`.

> [!NOTE]
>
> Using a prime other than `bn128` makes things a bit harder in circuit-specific setup, as you will have to find the PTAU files yourself, whereas in `bn128` we can use [Perpetual Powers of Tau](https://github.com/privacy-scaling-explorations/perpetualpowersoftau).
### Circuit Configurations

Expand All @@ -111,10 +109,12 @@ sudoku_9x9: {
template: 'Sudoku',
pubs: ['puzzle'],
params: [3], // sqrt(9)
},
}
```

You can omit `pubs` and `params` options, they default to `[]`.
> [!TIP]
>
> The `pubs` and `params` options can be omitted, in which case they will default to `[]`.
### Using Circomkit in Code

Expand Down Expand Up @@ -207,24 +207,28 @@ Finally, you can run tests on the witnesses too. This is most useful when you wo
- `expectConstraintPass(witness)` checks if constraints are passing for a witness
- `expectConstraintFail(witness)` checks if constraints are failing

You can compute the witness via the `calculateWitness(input)` function. To test for soundness errors, you may edit the witness and see if constraints are failing. Circomkit provides a nice utility for this purpose, called `editWitness(witness, symbols)`. You simply provide a dictionary of symbols to their new values, and it will edit the witness accordingly. See the example below:

```ts
it('should pass on correct witness', async () => {
const witness = await circuit.calculateWitness(INPUT);
await circuit.expectConstraintPass(witness);
});

it('should fail on fake witness', async () => {
const witness = await circuit.calculateWitness(INPUT);
const badWitness = await circuit.editWitness(witness, {
'main.signal': BigInt(1234),
'main.component.signal': BigInt('0xCAFE'),
'main.foo.bar[0]': BigInt('0b0101'),
});
await circuit.expectConstraintFail(badWitness);
});
```
You can compute the witness via the `calculateWitness(input)` function. To test for soundness errors, you may edit the witness and see if constraints are failing.

> [!TIP]
>
> Circomkit provides a nice utility for this purpose, called `editWitness(witness, symbols)`. You simply provide a dictionary of symbols to their new values, and it will edit the witness accordingly. See the example below:
>
> ```ts
> it('should pass on correct witness', async () => {
> const witness = await circuit.calculateWitness(INPUT);
> await circuit.expectConstraintPass(witness);
> });
>
> it('should fail on fake witness', async () => {
> const witness = await circuit.calculateWitness(INPUT);
> const badWitness = await circuit.editWitness(witness, {
> 'main.signal': BigInt(1234),
> 'main.component.signal': BigInt('0xCAFE'),
> 'main.foo.bar[0]': BigInt('0b0101'),
> });
> await circuit.expectConstraintFail(badWitness);
> });
> ```
### Proof Tester
Expand Down
15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "circomkit",
"version": "0.0.18",
"version": "0.0.19",
"description": "A Circom development environment",
"author": "erhant",
"license": "MIT",
Expand Down Expand Up @@ -31,21 +31,28 @@
"cli": "yarn build && node ./dist/bin",
"test": "npx mocha",
"pretest": "rimraf build/",
"check": "npx tsc --noEmit",
"style": "yarn format && yarn lint",
"lint": "npx gts lint",
"lint": "npx eslint '**/*.ts'",
"format": "npx prettier --check ./**/*.ts"
},
"devDependencies": {
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
"@types/mocha-each": "^2.0.0",
"@types/node": "^18.11.18",
"gts": "^5.0.1",
"@typescript-eslint/eslint-plugin": "^6.13.1",
"@typescript-eslint/parser": "^6.13.1",
"eslint": "^8.54.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^5.0.1",
"mocha": "^10.2.0",
"mocha-each": "^2.0.1",
"prettier": "^3.1.0",
"rimraf": "^5.0.1",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
"typescript": "^5.3.2"
},
"dependencies": {
"chai": "^4.3.7",
Expand Down
5 changes: 3 additions & 2 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ async function cli(): Promise<number> {
const titleLog = (title: string) => circomkit.log(`===| ${title} |===`, 'title');

// execute command
type Commands = keyof Circomkit | 'init' | 'config';
switch (process.argv[2] as unknown as Commands) {
const command = process.argv[2] as keyof Circomkit | 'init' | 'config';
switch (command) {
case 'compile': {
titleLog('Compiling the circuit');
const path = await circomkit.compile(process.argv[3]);
Expand Down Expand Up @@ -154,6 +154,7 @@ async function cli(): Promise<number> {
}

default:
// command satisfies never; // CLI cases are not that well-typed yet
console.log(usageString);
return 1;
}
Expand Down
Loading

0 comments on commit 2e7ae21

Please sign in to comment.