@semaphore-protocol/proof 3.15.1 → 4.0.0-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +18 -28
- package/dist/index.browser.mjs +800 -0
- package/dist/index.node.js +272 -1065
- package/dist/index.node.mjs +270 -1065
- package/dist/types/generate-proof.d.ts +15 -0
- package/dist/types/get-snark-artifacts.browser.d.ts +2 -0
- package/dist/types/get-snark-artifacts.node.d.ts +2 -0
- package/dist/types/hash.d.ts +1 -1
- package/dist/types/index.d.ts +6 -5
- package/dist/types/{packProof.d.ts → pack-proof.d.ts} +1 -1
- package/dist/types/types/index.d.ts +6 -4
- package/dist/types/{unpackProof.d.ts → unpack-proof.d.ts} +1 -1
- package/dist/types/verify-proof.d.ts +8 -0
- package/package.json +20 -13
- package/src/generate-proof.ts +81 -0
- package/src/get-snark-artifacts.browser.ts +9 -0
- package/src/get-snark-artifacts.node.ts +24 -0
- package/src/hash.ts +1 -1
- package/src/index.ts +6 -5
- package/src/{packProof.ts → pack-proof.ts} +1 -1
- package/src/types/index.ts +7 -4
- package/src/{unpackProof.ts → unpack-proof.ts} +1 -1
- package/src/verification-keys.json +522 -0
- package/src/verify-proof.ts +36 -0
- package/dist/types/calculateNullifierHash.d.ts +0 -8
- package/dist/types/generateProof.d.ts +0 -15
- package/dist/types/index.test.d.ts +0 -1
- package/dist/types/verifyProof.d.ts +0 -8
- package/src/calculateNullifierHash.ts +0 -16
- package/src/generateProof.ts +0 -69
- package/src/index.test.ts +0 -147
- package/src/verificationKeys.json +0 -712
- package/src/verifyProof.ts +0 -31
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { BytesLike, Hexable } from "@ethersproject/bytes";
|
|
2
|
+
import { Group } from "@semaphore-protocol/group";
|
|
3
|
+
import type { Identity } from "@semaphore-protocol/identity";
|
|
4
|
+
import { SemaphoreProof, SnarkArtifacts } from "./types";
|
|
5
|
+
/**
|
|
6
|
+
* Generates a Semaphore proof.
|
|
7
|
+
* @param identity The Semaphore identity.
|
|
8
|
+
* @param group The Semaphore group or its Merkle proof.
|
|
9
|
+
* @param scope The external nullifier.
|
|
10
|
+
* @param message The Semaphore signal.
|
|
11
|
+
* @param merkleTreeDepth The depth of the tree with which the circuit was compiled.
|
|
12
|
+
* @param snarkArtifacts The SNARK artifacts.
|
|
13
|
+
* @returns The Semaphore proof ready to be verified.
|
|
14
|
+
*/
|
|
15
|
+
export default function generateProof(identity: Identity, group: Group, message: BytesLike | Hexable | number | bigint, scope: BytesLike | Hexable | number | bigint, merkleTreeDepth?: number, snarkArtifacts?: SnarkArtifacts): Promise<SemaphoreProof>;
|
package/dist/types/hash.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BytesLike, Hexable } from "@ethersproject/bytes";
|
|
2
|
-
import { NumericString } from "
|
|
2
|
+
import { NumericString } from "@zk-kit/groth16";
|
|
3
3
|
/**
|
|
4
4
|
* Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
|
|
5
5
|
* @param message The message to be hashed.
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
1
|
+
import generateProof from "./generate-proof";
|
|
2
|
+
import getSnarkArtifacts from "./get-snark-artifacts.node";
|
|
3
|
+
import packProof from "./pack-proof";
|
|
4
|
+
import unpackProof from "./unpack-proof";
|
|
5
|
+
import verifyProof from "./verify-proof";
|
|
5
6
|
export * from "./types";
|
|
6
|
-
export { generateProof,
|
|
7
|
+
export { generateProof, getSnarkArtifacts, packProof, unpackProof, verifyProof };
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import type { NumericString } from "
|
|
1
|
+
import type { NumericString } from "@zk-kit/groth16";
|
|
2
|
+
export type BigNumberish = string | number | bigint;
|
|
2
3
|
export type SnarkArtifacts = {
|
|
3
4
|
wasmFilePath: string;
|
|
4
5
|
zkeyFilePath: string;
|
|
5
6
|
};
|
|
6
7
|
export type SemaphoreProof = {
|
|
8
|
+
merkleTreeDepth: number;
|
|
7
9
|
merkleTreeRoot: NumericString;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
message: NumericString;
|
|
11
|
+
nullifier: NumericString;
|
|
12
|
+
scope: NumericString;
|
|
11
13
|
proof: PackedProof;
|
|
12
14
|
};
|
|
13
15
|
export type PackedProof = [
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { SemaphoreProof } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Verifies a Semaphore proof.
|
|
4
|
+
* @param fullProof The SnarkJS Semaphore proof.
|
|
5
|
+
* @param treeDepth The depth of the tree.
|
|
6
|
+
* @returns True if the proof is valid, false otherwise.
|
|
7
|
+
*/
|
|
8
|
+
export default function verifyProof({ merkleTreeDepth, merkleTreeRoot, nullifier, message, scope, proof }: SemaphoreProof): Promise<boolean>;
|
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@semaphore-protocol/proof",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-alpha",
|
|
4
4
|
"description": "A library to generate and verify Semaphore proofs.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.node.js",
|
|
7
7
|
"exports": {
|
|
8
|
-
"
|
|
9
|
-
|
|
8
|
+
"node": {
|
|
9
|
+
"import": "./dist/index.node.mjs",
|
|
10
|
+
"require": "./dist/index.node.js"
|
|
11
|
+
},
|
|
12
|
+
"browser": "./dist/index.browser.mjs",
|
|
13
|
+
"default": "./dist/index.browser.mjs",
|
|
10
14
|
"types": "./dist/types/index.d.ts"
|
|
11
15
|
},
|
|
12
16
|
"types": "dist/types/index.d.ts",
|
|
@@ -22,17 +26,20 @@
|
|
|
22
26
|
"url": "https://github.com/semaphore-protocol/semaphore.git/issues"
|
|
23
27
|
},
|
|
24
28
|
"scripts": {
|
|
25
|
-
"build
|
|
26
|
-
"build": "
|
|
29
|
+
"build": "rimraf dist && yarn build:browser && yarn build:node",
|
|
30
|
+
"build:browser": "rollup -c rollup.browser.config.ts --configPlugin typescript",
|
|
31
|
+
"build:node": "rollup -c rollup.node.config.ts --configPlugin typescript",
|
|
27
32
|
"prepublishOnly": "yarn build"
|
|
28
33
|
},
|
|
29
34
|
"publishConfig": {
|
|
30
35
|
"access": "public"
|
|
31
36
|
},
|
|
32
37
|
"devDependencies": {
|
|
33
|
-
"@
|
|
38
|
+
"@ethersproject/strings": "^5.7.0",
|
|
39
|
+
"@rollup/plugin-alias": "^5.1.0",
|
|
34
40
|
"@rollup/plugin-json": "^5.0.1",
|
|
35
|
-
"@
|
|
41
|
+
"@types/download": "^8.0.5",
|
|
42
|
+
"@types/tmp": "^0.2.6",
|
|
36
43
|
"poseidon-lite": "^0.2.0",
|
|
37
44
|
"rimraf": "^5.0.5",
|
|
38
45
|
"rollup": "^4.0.2",
|
|
@@ -40,15 +47,15 @@
|
|
|
40
47
|
"rollup-plugin-typescript2": "^0.31.2"
|
|
41
48
|
},
|
|
42
49
|
"peerDependencies": {
|
|
43
|
-
"@semaphore-protocol/group": "
|
|
44
|
-
"@semaphore-protocol/identity": "
|
|
50
|
+
"@semaphore-protocol/group": "4.0.0-alpha",
|
|
51
|
+
"@semaphore-protocol/identity": "4.0.0-alpha"
|
|
45
52
|
},
|
|
46
53
|
"dependencies": {
|
|
47
|
-
"@ethersproject/bignumber": "^5.
|
|
54
|
+
"@ethersproject/bignumber": "^5.7.0",
|
|
48
55
|
"@ethersproject/bytes": "^5.7.0",
|
|
49
56
|
"@ethersproject/keccak256": "^5.7.0",
|
|
50
|
-
"@
|
|
51
|
-
"
|
|
52
|
-
"
|
|
57
|
+
"@zk-kit/groth16": "0.5.0",
|
|
58
|
+
"download": "^8.0.0",
|
|
59
|
+
"tmp": "^0.2.1"
|
|
53
60
|
}
|
|
54
61
|
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { BigNumber } from "@ethersproject/bignumber"
|
|
2
|
+
import { BytesLike, Hexable } from "@ethersproject/bytes"
|
|
3
|
+
import { Group } from "@semaphore-protocol/group"
|
|
4
|
+
import type { Identity } from "@semaphore-protocol/identity"
|
|
5
|
+
import { NumericString, prove } from "@zk-kit/groth16"
|
|
6
|
+
import getSnarkArtifacts from "./get-snark-artifacts.node"
|
|
7
|
+
import hash from "./hash"
|
|
8
|
+
import packProof from "./pack-proof"
|
|
9
|
+
import { SemaphoreProof, SnarkArtifacts } from "./types"
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Generates a Semaphore proof.
|
|
13
|
+
* @param identity The Semaphore identity.
|
|
14
|
+
* @param group The Semaphore group or its Merkle proof.
|
|
15
|
+
* @param scope The external nullifier.
|
|
16
|
+
* @param message The Semaphore signal.
|
|
17
|
+
* @param merkleTreeDepth The depth of the tree with which the circuit was compiled.
|
|
18
|
+
* @param snarkArtifacts The SNARK artifacts.
|
|
19
|
+
* @returns The Semaphore proof ready to be verified.
|
|
20
|
+
*/
|
|
21
|
+
export default async function generateProof(
|
|
22
|
+
identity: Identity,
|
|
23
|
+
group: Group,
|
|
24
|
+
message: BytesLike | Hexable | number | bigint,
|
|
25
|
+
scope: BytesLike | Hexable | number | bigint,
|
|
26
|
+
merkleTreeDepth?: number,
|
|
27
|
+
snarkArtifacts?: SnarkArtifacts
|
|
28
|
+
): Promise<SemaphoreProof> {
|
|
29
|
+
const leafIndex = group.indexOf(identity.commitment)
|
|
30
|
+
const merkleProof = group.generateMerkleProof(leafIndex)
|
|
31
|
+
const merkleProofLength = merkleProof.siblings.length
|
|
32
|
+
|
|
33
|
+
if (merkleTreeDepth !== undefined) {
|
|
34
|
+
if (merkleTreeDepth < 1 || merkleTreeDepth > 12) {
|
|
35
|
+
throw new TypeError("The tree depth must be a number between 1 and 12")
|
|
36
|
+
}
|
|
37
|
+
} else {
|
|
38
|
+
merkleTreeDepth = merkleProofLength
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// If the Snark artifacts are not defined they will be automatically downloaded.
|
|
42
|
+
/* istanbul ignore next */
|
|
43
|
+
if (!snarkArtifacts) {
|
|
44
|
+
snarkArtifacts = await getSnarkArtifacts(merkleTreeDepth)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// The index must be converted to a list of indices, 1 for each tree level.
|
|
48
|
+
// The missing siblings can be set to 0, as they won't be used in the circuit.
|
|
49
|
+
const merkleProofIndices = []
|
|
50
|
+
const merkleProofSiblings = merkleProof.siblings
|
|
51
|
+
|
|
52
|
+
for (let i = 0; i < merkleTreeDepth; i += 1) {
|
|
53
|
+
merkleProofIndices.push((merkleProof.index >> i) & 1)
|
|
54
|
+
|
|
55
|
+
if (merkleProofSiblings[i] === undefined) {
|
|
56
|
+
merkleProofSiblings[i] = "0"
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const { proof, publicSignals } = await prove(
|
|
61
|
+
{
|
|
62
|
+
secret: identity.secretScalar,
|
|
63
|
+
merkleProofLength,
|
|
64
|
+
merkleProofIndices,
|
|
65
|
+
merkleProofSiblings,
|
|
66
|
+
scope: hash(scope),
|
|
67
|
+
message: hash(message)
|
|
68
|
+
},
|
|
69
|
+
snarkArtifacts.wasmFilePath,
|
|
70
|
+
snarkArtifacts.zkeyFilePath
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
merkleTreeDepth,
|
|
75
|
+
merkleTreeRoot: publicSignals[0],
|
|
76
|
+
nullifier: publicSignals[1],
|
|
77
|
+
message: BigNumber.from(message).toString() as NumericString,
|
|
78
|
+
scope: BigNumber.from(scope).toString() as NumericString,
|
|
79
|
+
proof: packProof(proof)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/* istanbul ignore file */
|
|
2
|
+
import { SnarkArtifacts } from "./types"
|
|
3
|
+
|
|
4
|
+
export default async function getSnarkArtifacts(treeDepth: number): Promise<SnarkArtifacts> {
|
|
5
|
+
return {
|
|
6
|
+
wasmFilePath: `https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.wasm`,
|
|
7
|
+
zkeyFilePath: `https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.zkey`
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/* istanbul ignore file */
|
|
2
|
+
import download from "download"
|
|
3
|
+
import fs from "fs"
|
|
4
|
+
import tmp from "tmp"
|
|
5
|
+
import { SnarkArtifacts } from "./types"
|
|
6
|
+
|
|
7
|
+
export default async function getSnarkArtifacts(treeDepth: number): Promise<SnarkArtifacts> {
|
|
8
|
+
const tmpDir = "semaphore-proof"
|
|
9
|
+
const tmpPath = `${tmp.tmpdir}/${tmpDir}-${treeDepth}`
|
|
10
|
+
|
|
11
|
+
if (!fs.existsSync(tmpPath)) {
|
|
12
|
+
tmp.dirSync({ name: `${tmpDir}-${treeDepth}` })
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (fs.readdirSync(tmpPath).length !== 2) {
|
|
16
|
+
await download(`https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.wasm`, tmpPath)
|
|
17
|
+
await download(`https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.zkey`, tmpPath)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
wasmFilePath: `${tmpPath}/semaphore.wasm`,
|
|
22
|
+
zkeyFilePath: `${tmpPath}/semaphore.zkey`
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/hash.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BigNumber } from "@ethersproject/bignumber"
|
|
2
2
|
import { BytesLike, Hexable, zeroPad } from "@ethersproject/bytes"
|
|
3
3
|
import { keccak256 } from "@ethersproject/keccak256"
|
|
4
|
-
import { NumericString } from "
|
|
4
|
+
import { NumericString } from "@zk-kit/groth16"
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import generateProof from "./generate-proof"
|
|
2
|
+
import getSnarkArtifacts from "./get-snark-artifacts.node"
|
|
3
|
+
import packProof from "./pack-proof"
|
|
4
|
+
import unpackProof from "./unpack-proof"
|
|
5
|
+
import verifyProof from "./verify-proof"
|
|
4
6
|
|
|
5
|
-
export { MerkleProof } from "@zk-kit/incremental-merkle-tree"
|
|
6
7
|
export * from "./types"
|
|
7
|
-
export { generateProof,
|
|
8
|
+
export { generateProof, getSnarkArtifacts, packProof, unpackProof, verifyProof }
|
package/src/types/index.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import type { NumericString } from "
|
|
1
|
+
import type { NumericString } from "@zk-kit/groth16"
|
|
2
|
+
|
|
3
|
+
export type BigNumberish = string | number | bigint
|
|
2
4
|
|
|
3
5
|
export type SnarkArtifacts = {
|
|
4
6
|
wasmFilePath: string
|
|
@@ -6,10 +8,11 @@ export type SnarkArtifacts = {
|
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
export type SemaphoreProof = {
|
|
11
|
+
merkleTreeDepth: number
|
|
9
12
|
merkleTreeRoot: NumericString
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
message: NumericString
|
|
14
|
+
nullifier: NumericString
|
|
15
|
+
scope: NumericString
|
|
13
16
|
proof: PackedProof
|
|
14
17
|
}
|
|
15
18
|
|