Skip to content

Commit

Permalink
refactor!: use maybeGetSemaphoreSnarkArtifacts from `@zk-kit/utils (#…
Browse files Browse the repository at this point in the history
…747)

* chore(proof): bump `@zk-kit/utils` dep

* refactor(proof): use `maybeGetSemaphoreSnarkArtifacts` from `@zk-kit/utils`

Delete logic related to fetching snark artifacts (wasm and zkey files) that was moved to
`@zk-kit/utils`

* revert(proof): add back `requireObject(snarkArtifacts)` check

* chore(proof): update rollup.browser.config.ts

* docs(proof): update README

* chore(proof): remove unused import in rollup.browser.config.ts

* Update packages/proof/package.json

Co-authored-by: Cedoor <me@cedoor.dev>

* docs(proof): add links to other repos in proof README

* chore: bump `yarn.lock`

* docs(proof): add punctuation

---------

Co-authored-by: Cedoor <me@cedoor.dev>
  • Loading branch information
sripwoud and cedoor committed Apr 24, 2024
1 parent 41a85e0 commit cf1cffd
Show file tree
Hide file tree
Showing 8 changed files with 1,391 additions and 1,406 deletions.
10 changes: 6 additions & 4 deletions packages/proof/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ group: _Group_,
message: _BigNumberish_ | _Uint8Array_ | string,
scope: _BigNumberish_ | _Uint8Array_ | string,
merkleTreeDepth: _number_,
snarkArtifacts?: _SnarkArtifacts_
snarkArtifacts?: [_SnarkArtifacts_](https://github.com/privacy-scaling-explorations/zk-kit/blob/88acdc6d8fa5f3f2a8ecd1e1a0140244b970c551/packages/utils/src/types/index.ts#L46)
): Promise\<_SemaphoreProof_>

```typescript
Expand All @@ -95,15 +95,17 @@ const group = new Group([identity1.commitment, identity2.commitment, identity3.c
const message = "Hello world"
const scope = "Semaphore"

// snarkArtifacts are not provided.
// So they will be automatically downloaded (see https://github.com/privacy-scaling-explorations/snark-artifacts).
const proof1 = await generateProof(identity1, group, message, scope)

// You can also specify the maximum tree depth supported by the proof.
const proof2 = await generateProof(identity2, group, message, scope, 20)

// You can also specify the default zkey/wasm files.
// You can also override our default zkey/wasm files.
const proof3 = await generateProof(identity3, group, message, scope, 20, {
wasmFilePath: "./semaphore.wasm",
zkeyFilePath: "./semaphore.zkey"
wasm: "./semaphore.wasm",
zkey: "./semaphore.zkey"
})
```

Expand Down
2 changes: 1 addition & 1 deletion packages/proof/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
},
"dependencies": {
"@semaphore-protocol/utils": "4.0.0-beta.7",
"@zk-kit/utils": "0.8.1",
"@zk-kit/utils": "1.0.0-beta.4",
"ethers": "6.10.0",
"snarkjs": "0.7.3"
}
Expand Down
5 changes: 1 addition & 4 deletions packages/proof/rollup.browser.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import alias from "@rollup/plugin-alias"
import json from "@rollup/plugin-json"
import typescript from "@rollup/plugin-typescript"
import * as fs from "fs"
Expand Down Expand Up @@ -34,14 +33,12 @@ export default {
"ethers/crypto",
"ethers/utils",
"ethers/abi",
"@zk-kit/utils",
"@zk-kit/utils/error-handlers",
"@zk-kit/utils/proof-packing",
"@semaphore-protocol/utils/constants"
],
plugins: [
alias({
entries: [{ find: "./get-snark-artifacts.node", replacement: "./get-snark-artifacts.browser" }]
}),
typescript({
tsconfig: "./build.tsconfig.json"
}),
Expand Down
17 changes: 8 additions & 9 deletions packages/proof/src/generate-proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import type { Identity } from "@semaphore-protocol/identity"
import { MAX_DEPTH, MIN_DEPTH } from "@semaphore-protocol/utils/constants"
import { requireDefined, requireNumber, requireObject, requireTypes } from "@zk-kit/utils/error-handlers"
import { packGroth16Proof } from "@zk-kit/utils/proof-packing"
import { maybeGetSemaphoreSnarkArtifacts, type SnarkArtifacts } from "@zk-kit/utils"
import type { BigNumberish } from "ethers"
import { NumericString, groth16 } from "snarkjs"
import getSnarkArtifacts from "./get-snark-artifacts.node"
import { type NumericString, groth16 } from "snarkjs"
import hash from "./hash"
import toBigInt from "./to-bigint"
import { SemaphoreProof, SnarkArtifacts } from "./types"
import type { SemaphoreProof } from "./types"

/**
* It generates a Semaphore proof, i.e. a zero-knowledge proof that an identity that
Expand All @@ -27,7 +27,7 @@ import { SemaphoreProof, SnarkArtifacts } from "./types"
* @param message The Semaphore message.
* @param scope The Semaphore scope.
* @param merkleTreeDepth The depth of the tree with which the circuit was compiled.
* @param snarkArtifacts The SNARK artifacts.
* @param snarkArtifacts See {@link https://zkkit.pse.dev/interfaces/_zk_kit_utils.SnarkArtifacts.html | SnarkArtifacts}.
* @returns The Semaphore proof ready to be verified.
*/
export default async function generateProof(
Expand Down Expand Up @@ -83,9 +83,8 @@ export default async function generateProof(
}

// If the Snark artifacts are not defined they will be automatically downloaded.
if (!snarkArtifacts) {
snarkArtifacts = await getSnarkArtifacts(merkleTreeDepth)
}
snarkArtifacts ??= await maybeGetSemaphoreSnarkArtifacts(merkleTreeDepth)
const { wasm, zkey } = snarkArtifacts

// The index must be converted to a list of indices, 1 for each tree level.
// The missing siblings can be set to 0, as they won't be used in the circuit.
Expand All @@ -109,8 +108,8 @@ export default async function generateProof(
scope: hash(scope),
message: hash(message)
},
snarkArtifacts.wasmFilePath,
snarkArtifacts.zkeyFilePath
wasm,
zkey
)

return {
Expand Down
18 changes: 0 additions & 18 deletions packages/proof/src/get-snark-artifacts.browser.ts

This file was deleted.

59 changes: 0 additions & 59 deletions packages/proof/src/get-snark-artifacts.node.ts

This file was deleted.

5 changes: 0 additions & 5 deletions packages/proof/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import type { NumericString } from "snarkjs"
import type { PackedGroth16Proof } from "@zk-kit/utils"

export type SnarkArtifacts = {
wasmFilePath: string
zkeyFilePath: string
}

export type SemaphoreProof = {
merkleTreeDepth: number
merkleTreeRoot: NumericString
Expand Down

0 comments on commit cf1cffd

Please sign in to comment.