Skip to content

Commit

Permalink
Merge pull request #168 from semaphore-protocol/refactor/hardcode-ver…
Browse files Browse the repository at this point in the history
…ification-keys

Hardcoded snark verification keys
  • Loading branch information
cedoor committed Nov 21, 2022
2 parents 583311f + 26d1c51 commit 901531b
Show file tree
Hide file tree
Showing 7 changed files with 789 additions and 25 deletions.
14 changes: 6 additions & 8 deletions packages/proof/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ yarn add @semaphore-protocol/identity @semaphore-protocol/group @semaphore-proto

## 📜 Usage

\# **generateProof**(identity: _Identity_, group: _Group_, externalNullifier: _BigNumberish_, signal: _string_, snarkArtifacts?: _SnarkArtifacts_): Promise\<_SemaphoreFullProof_>
\# **generateProof**(identity: _Identity_, group: _Group_ | _MerkleProof_, externalNullifier: _BigNumberish_, signal: _string_, snarkArtifacts?: _SnarkArtifacts_): Promise\<_SemaphoreFullProof_>

```typescript
import { Identity } from "@semaphore-protocol/identity"
Expand All @@ -81,23 +81,21 @@ const signal = "Hello world"

group.addMembers([...identityCommitments, identity.generateCommitment()])

const fullProof = await generateProof(identity, merkleProof, externalNullifier, signal, {
const fullProof = await generateProof(identity, group, externalNullifier, signal, {
zkeyFilePath: "./semaphore.zkey",
wasmFilePath: "./semaphore.wasm"
})

// You can also use the default zkey/wasm files (only for browsers!).
// const fullProof = await generateProof(identity, merkleProof, externalNullifier, signal)
// You can also use the default zkey/wasm files (it only works from browsers!).
// const fullProof = await generateProof(identity, group, externalNullifier, signal)
```

\# **verifyProof**(verificationKey: _any_, fullProof: _FullProof_): Promise\<_boolean_>
\# **verifyProof**(fullProof: _FullProof_, treeDepth: _number_): Promise\<_boolean_>

```typescript
import { verifyProof } from "@semaphore-protocol/proof"

const verificationKey = JSON.parse(fs.readFileSync("/semaphore.json", "utf-8"))

await verifyProof(verificationKey, fullProof)
await verifyProof(fullProof, 20)
```

\# **packToSolidityProof**(proof: _Proof_): _SolidityProof_
Expand Down
1 change: 1 addition & 0 deletions packages/proof/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"access": "public"
},
"devDependencies": {
"@rollup/plugin-json": "^5.0.1",
"ffjavascript": "^0.2.54",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-typescript2": "^0.31.2",
Expand Down
4 changes: 3 additions & 1 deletion packages/proof/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import typescript from "rollup-plugin-typescript2"
import * as fs from "fs"
import cleanup from "rollup-plugin-cleanup"
import json from "@rollup/plugin-json"

const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8"))
const banner = `/**
Expand All @@ -24,6 +25,7 @@ export default {
tsconfig: "./build.tsconfig.json",
useTsconfigDeclarationDir: true
}),
cleanup({ comments: "jsdoc" })
cleanup({ comments: "jsdoc" }),
json()
]
}
26 changes: 14 additions & 12 deletions packages/proof/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { formatBytes32String } from "@ethersproject/strings"
import { Group } from "@semaphore-protocol/group"
import { Identity } from "@semaphore-protocol/identity"
import { getCurveFromName } from "ffjavascript"
import fs from "fs"
import generateNullifierHash from "./generateNullifierHash"
import generateProof from "./generateProof"
import generateSignalHash from "./generateSignalHash"
Expand All @@ -18,7 +17,6 @@ describe("Proof", () => {

const wasmFilePath = `./snark-artifacts/${treeDepth}/semaphore.wasm`
const zkeyFilePath = `./snark-artifacts/${treeDepth}/semaphore.zkey`
const verificationKeyPath = `./snark-artifacts/${treeDepth}/semaphore.json`

const identity = new Identity()

Expand Down Expand Up @@ -89,6 +87,20 @@ describe("Proof", () => {
}, 20000)
})

describe("# verifyProof", () => {
it("Should not verify a proof if the tree depth is wrong", () => {
const fun = () => verifyProof(fullProof, 3)

expect(fun).toThrow("The tree depth must be a number between 16 and 32")
})

it("Should verify a Semaphore proof", async () => {
const response = await verifyProof(fullProof, treeDepth)

expect(response).toBe(true)
})
})

describe("# generateSignalHash", () => {
it("Should generate a valid signal hash", async () => {
const signalHash = generateSignalHash(signal)
Expand Down Expand Up @@ -118,14 +130,4 @@ describe("Proof", () => {
expect(solidityProof).toHaveLength(8)
})
})

describe("# verifyProof", () => {
it("Should generate and verify a Semaphore proof", async () => {
const verificationKey = JSON.parse(fs.readFileSync(verificationKeyPath, "utf-8"))

const response = await verifyProof(verificationKey, fullProof)

expect(response).toBe(true)
})
})
})
Loading

0 comments on commit 901531b

Please sign in to comment.