refactor!: use maybeGetSemaphoreSnarkArtifacts from `@zk-kit/utils (#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>
This commit is contained in:
sripwoud
2024-04-24 16:43:19 +02:00
committed by GitHub
parent 41a85e0e45
commit cf1cffdf65
8 changed files with 1400 additions and 1415 deletions

View File

@@ -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
@@ -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"
})
```

View File

@@ -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"
}

View File

@@ -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"
@@ -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"
}),

View File

@@ -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
@@ -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(
@@ -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.
@@ -109,8 +108,8 @@ export default async function generateProof(
scope: hash(scope),
message: hash(message)
},
snarkArtifacts.wasmFilePath,
snarkArtifacts.zkeyFilePath
wasm,
zkey
)
return {

View File

@@ -1,18 +0,0 @@
/* istanbul ignore file */
import { requireNumber } from "@zk-kit/utils/error-handlers"
import { SnarkArtifacts } from "./types"
/**
* Returns the zero-knowledge artifact paths. Artifacts exist for each tree
* depth supported by Semaphore, and they were generated in a trusted-setup.
* @param treeDepth The depth of the tree.
* @returns The zero-knowledge artifacts paths.
*/
export default async function getSnarkArtifacts(treeDepth: number): Promise<SnarkArtifacts> {
requireNumber(treeDepth, "treeDepth")
return {
wasmFilePath: `https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.wasm`,
zkeyFilePath: `https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.zkey`
}
}

View File

@@ -1,59 +0,0 @@
/* istanbul ignore file */
import { requireNumber } from "@zk-kit/utils/error-handlers"
import { createWriteStream, existsSync, readdirSync } from "node:fs"
import { mkdir } from "node:fs/promises"
import os from "node:os"
import { dirname } from "node:path"
import { Readable } from "node:stream"
import { finished } from "node:stream/promises"
import { SnarkArtifacts } from "./types"
/**
* A utility function to download the zero-knowledge artifacts from an external server.
* @param url The URL from which to download the artifacts.
* @param outputPath The path in which to save the artifacts.
*/
async function download(url: string, outputPath: string) {
const response = await fetch(url)
if (!response.ok) {
throw new Error(`Failed to fetch ${url}: ${response.statusText}`)
}
// Ensure the directory exists.
const dir = dirname(outputPath)
await mkdir(dir, { recursive: true })
const fileStream = createWriteStream(outputPath)
await finished(Readable.fromWeb(response.body as any).pipe(fileStream))
}
/**
* Downloads the zero-knowledge artifacts and returns their paths.
* Artifacts exist for each tree depth supported by Semaphore, and
* they were generated in a trusted-setup.
* @param treeDepth The depth of the tree.
* @returns The zero-knowledge artifacts paths.
*/
export default async function getSnarkArtifacts(treeDepth: number): Promise<SnarkArtifacts> {
requireNumber(treeDepth, "treeDepth")
const tmpDir = "semaphore-proof"
const tmpPath = `${os.tmpdir()}/${tmpDir}-${treeDepth}`
if (!existsSync(tmpPath) || readdirSync(tmpPath).length !== 2) {
await download(
`https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.wasm`,
`${tmpPath}/semaphore.wasm`
)
await download(
`https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.zkey`,
`${tmpPath}/semaphore.zkey`
)
}
return {
wasmFilePath: `${tmpPath}/semaphore.wasm`,
zkeyFilePath: `${tmpPath}/semaphore.zkey`
}
}

View File

@@ -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

2699
yarn.lock

File diff suppressed because it is too large Load Diff