Skip to content

Commit

Permalink
refactor(proof): add functions to check types
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Feb 19, 2024
1 parent eb2fba2 commit 3ef0553
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 13 deletions.
3 changes: 2 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const projects: any = fs
displayName: name,
setupFiles: ["dotenv/config"],
moduleNameMapper: {
"@semaphore-protocol/(.*)": "<rootDir>/../$1/src/index.ts" // Interdependency packages.
"@semaphore-protocol/(.*)/(.*)": "<rootDir>/../$1/src/$2.ts",
"@semaphore-protocol/(.*)": "<rootDir>/../$1/src/index.ts"
}
}))

Expand Down
3 changes: 2 additions & 1 deletion packages/proof/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@rollup/plugin-alias": "^5.1.0",
"@rollup/plugin-json": "^6.1.0",
"@types/download": "^8.0.5",
"@types/snarkjs": "0.7.8",
"@types/tmp": "^0.2.6",
"poseidon-lite": "^0.2.0",
"rimraf": "^5.0.5",
Expand All @@ -53,7 +54,7 @@
"@semaphore-protocol/identity": "4.0.0-alpha.8"
},
"dependencies": {
"@types/snarkjs": "0.7.8",
"@semaphore-protocol/utils": "4.0.0-alpha.8",
"ethers": "6.10.0",
"snarkjs": "0.7.3"
}
Expand Down
3 changes: 2 additions & 1 deletion packages/proof/rollup.browser.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export default {
"node:stream/promises",
"ethers/crypto",
"ethers/utils",
"ethers/abi"
"ethers/abi",
"@semaphore-protocol/utils/errors"
],
plugins: [
alias({
Expand Down
3 changes: 2 additions & 1 deletion packages/proof/rollup.node.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export default {
"node:stream/promises",
"ethers/crypto",
"ethers/utils",
"ethers/abi"
"ethers/abi",
"@semaphore-protocol/utils/errors"
],
plugins: [
typescript({
Expand Down
21 changes: 20 additions & 1 deletion packages/proof/src/generate-proof.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { Group, MerkleProof } from "@semaphore-protocol/group"
import type { Identity } from "@semaphore-protocol/identity"
import { requireDefined, requireNumber, requireObject, requireTypes } from "@semaphore-protocol/utils/errors"
import { NumericString, groth16 } from "snarkjs"
import getSnarkArtifacts from "./get-snark-artifacts.node"
import hash from "./hash"
import packPoints from "./pack-points"
import { BigNumberish, SemaphoreProof, SnarkArtifacts } from "./types"
import toBigInt from "./to-bigint"
import { BigNumberish, SemaphoreProof, SnarkArtifacts } from "./types"

/**
* Generates a Semaphore proof.
Expand All @@ -25,6 +26,24 @@ export default async function generateProof(
merkleTreeDepth?: number,
snarkArtifacts?: SnarkArtifacts
): Promise<SemaphoreProof> {
requireDefined(identity, "identity")
requireDefined(groupOrMerkleProof, "groupOrMerkleProof")
requireDefined(message, "message")
requireDefined(scope, "scope")

requireObject(identity, "identity")
requireObject(identity, "groupOrMerkleProof")
requireTypes(message, "message", ["string", "bigint", "number", "uint8array"])
requireTypes(scope, "scope", ["string", "bigint", "number", "uint8array"])

if (merkleTreeDepth) {
requireNumber(merkleTreeDepth, "merkleTreeDepth")
}

if (snarkArtifacts) {
requireObject(snarkArtifacts, "snarkArtifacts")
}

message = toBigInt(message)
scope = toBigInt(scope)

Expand Down
3 changes: 3 additions & 0 deletions packages/proof/src/get-snark-artifacts.browser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* istanbul ignore file */
import { requireNumber } from "@semaphore-protocol/utils/errors"
import { SnarkArtifacts } from "./types"

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`
Expand Down
3 changes: 3 additions & 0 deletions packages/proof/src/get-snark-artifacts.node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* istanbul ignore file */
import { requireNumber } from "@semaphore-protocol/utils/errors"
import { createWriteStream, existsSync, readdirSync } from "node:fs"
import { mkdir } from "node:fs/promises"
import os from "node:os"
Expand All @@ -23,6 +24,8 @@ async function download(url: string, outputPath: string) {
}

export default async function getSnarkArtifacts(treeDepth: number): Promise<SnarkArtifacts> {
requireNumber(treeDepth, "treeDepth")

const tmpDir = "semaphore-proof"
const tmpPath = `${os.tmpdir()}/${tmpDir}-${treeDepth}`

Expand Down
28 changes: 20 additions & 8 deletions packages/proof/src/verify-proof.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import {
requireArray,
requireDefined,
requireNumber,
requireObject,
requireString
} from "@semaphore-protocol/utils/errors"
import { groth16 } from "snarkjs"
import hash from "./hash"
import { SemaphoreProof } from "./types"
Expand All @@ -9,14 +16,19 @@ import verificationKeys from "./verification-keys.json"
* @param proof The Semaphore proof.
* @returns True if the proof is valid, false otherwise.
*/
export default async function verifyProof({
merkleTreeDepth,
merkleTreeRoot,
nullifier,
message,
scope,
points
}: SemaphoreProof): Promise<boolean> {
export default async function verifyProof(proof: SemaphoreProof): Promise<boolean> {
requireDefined(proof, "proof")
requireObject(proof, "proof")

const { merkleTreeDepth, merkleTreeRoot, nullifier, message, scope, points } = proof

requireNumber(merkleTreeDepth, "proof.merkleTreeDepth")
requireString(merkleTreeRoot, "proof.merkleTreeRoot")
requireString(nullifier, "proof.nullifier")
requireString(message, "proof.message")
requireString(scope, "proof.scope")
requireArray(points, "proof.points")

// TODO: support all tree depths after trusted-setup.
if (merkleTreeDepth < 1 || merkleTreeDepth > 12) {
throw new TypeError("The tree depth must be a number between 1 and 12")
Expand Down

0 comments on commit 3ef0553

Please sign in to comment.