Skip to content

Commit 70b9bb0

Browse files
authored
Merge pull request #363 from semaphore-protocol/chore/snarkjs-circomlibjs-types
New Snarkjs/Circomlibjs type definitions Former-commit-id: f212684
2 parents 8cc9d7f + a6bb9bb commit 70b9bb0

File tree

16 files changed

+62
-1037
lines changed

16 files changed

+62
-1037
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@
5050
"@commitlint/cli": "^16.0.2",
5151
"@commitlint/config-conventional": "^16.0.0",
5252
"@rollup/plugin-typescript": "^8.3.0",
53+
"@types/circomlibjs": "^0.1.1",
5354
"@types/download": "^8.0.1",
5455
"@types/glob": "^7.2.0",
5556
"@types/jest": "^27.4.0",
5657
"@types/node": "^17.0.9",
5758
"@types/rimraf": "^3.0.2",
59+
"@types/snarkjs": "^0.7.2",
5860
"@typescript-eslint/eslint-plugin": "^5.9.1",
5961
"@typescript-eslint/parser": "^5.9.1",
6062
"babel-jest": "^27.4.6",

packages/contracts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"@types/rimraf": "^3.0.2",
3131
"chai": "^4.3.5",
3232
"circomlib": "^2.0.2",
33-
"circomlibjs": "^0.0.8",
33+
"circomlibjs": "^0.1.7",
3434
"download": "^8.0.0",
3535
"ethers": "^5.6.8",
3636
"hardhat": "^2.9.7",

packages/contracts/tasks/deploy-semaphore-voting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { poseidon_gencontract as poseidonContract } from "circomlibjs"
1+
import { poseidonContract } from "circomlibjs"
22
import { task, types } from "hardhat/config"
33

44
task("deploy:semaphore-voting", "Deploy a SemaphoreVoting contract")

packages/contracts/tasks/deploy-semaphore-whistleblowing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { poseidon_gencontract as poseidonContract } from "circomlibjs"
1+
import { poseidonContract } from "circomlibjs"
22
import { task, types } from "hardhat/config"
33

44
task("deploy:semaphore-whistleblowing", "Deploy a SemaphoreWhistleblowing contract")

packages/contracts/tasks/deploy-semaphore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { poseidon_gencontract as poseidonContract } from "circomlibjs"
1+
import { poseidonContract } from "circomlibjs"
22
import { task, types } from "hardhat/config"
33
import { saveDeployedContracts } from "../scripts/utils"
44

packages/hardhat/src/tasks/deploy-semaphore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { poseidon_gencontract as poseidonContract } from "circomlibjs"
1+
import { poseidonContract } from "circomlibjs"
22
import { task, types } from "hardhat/config"
33

44
task("deploy:semaphore", "Deploy a Semaphore contract")

packages/proof/src/generateProof.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { BytesLike, Hexable } from "@ethersproject/bytes"
33
import { Group } from "@semaphore-protocol/group"
44
import type { Identity } from "@semaphore-protocol/identity"
55
import { MerkleProof } from "@zk-kit/incremental-merkle-tree"
6-
import { groth16 } from "snarkjs"
6+
import { groth16, NumericString } from "snarkjs"
77
import hash from "./hash"
88
import packProof from "./packProof"
9-
import { FullProof, SnarkArtifacts } from "./types"
9+
import { SemaphoreProof, SnarkArtifacts } from "./types"
1010

1111
/**
1212
* Generates a Semaphore proof.
@@ -23,7 +23,7 @@ export default async function generateProof(
2323
externalNullifier: BytesLike | Hexable | number | bigint,
2424
signal: BytesLike | Hexable | number | bigint,
2525
snarkArtifacts?: SnarkArtifacts
26-
): Promise<FullProof> {
26+
): Promise<SemaphoreProof> {
2727
let merkleProof: MerkleProof
2828

2929
if ("depth" in groupOrMerkleProof) {
@@ -61,8 +61,8 @@ export default async function generateProof(
6161
return {
6262
merkleTreeRoot: publicSignals[0],
6363
nullifierHash: publicSignals[1],
64-
signal: BigNumber.from(signal).toString(),
65-
externalNullifier: BigNumber.from(externalNullifier).toString(),
64+
signal: BigNumber.from(signal).toString() as NumericString,
65+
externalNullifier: BigNumber.from(externalNullifier).toString() as NumericString,
6666
proof: packProof(proof)
6767
}
6868
}

packages/proof/src/hash.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { BigNumber } from "@ethersproject/bignumber"
22
import { BytesLike, Hexable, zeroPad } from "@ethersproject/bytes"
33
import { keccak256 } from "@ethersproject/keccak256"
4+
import { NumericString } from "snarkjs"
45

56
/**
67
* Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
78
* @param message The message to be hashed.
89
* @returns The message digest.
910
*/
10-
export default function hash(message: BytesLike | Hexable | number | bigint): bigint {
11+
export default function hash(message: BytesLike | Hexable | number | bigint): NumericString {
1112
message = BigNumber.from(message).toTwos(256).toHexString()
1213
message = zeroPad(message, 32)
1314

14-
return BigInt(keccak256(message)) >> BigInt(8)
15+
return (BigInt(keccak256(message)) >> BigInt(8)).toString() as NumericString
1516
}

packages/proof/src/index.test.ts

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import calculateNullifierHash from "./calculateNullifierHash"
66
import generateProof from "./generateProof"
77
import hash from "./hash"
88
import packProof from "./packProof"
9-
import { FullProof } from "./types"
9+
import { SemaphoreProof } from "./types"
1010
import unpackProof from "./unpackProof"
1111
import verifyProof from "./verifyProof"
1212

@@ -21,7 +21,7 @@ describe("Proof", () => {
2121

2222
const identity = new Identity()
2323

24-
let fullProof: FullProof
24+
let fullProof: SemaphoreProof
2525
let curve: any
2626

2727
beforeAll(async () => {
@@ -34,9 +34,7 @@ describe("Proof", () => {
3434

3535
describe("# generateProof", () => {
3636
it("Should not generate Semaphore proofs if the identity is not part of the group", async () => {
37-
const group = new Group(treeDepth)
38-
39-
group.addMembers([BigInt(1), BigInt(2)])
37+
const group = new Group(treeDepth, 20, [BigInt(1), BigInt(2)])
4038

4139
const fun = () =>
4240
generateProof(identity, group, externalNullifier, signal, {
@@ -48,19 +46,15 @@ describe("Proof", () => {
4846
})
4947

5048
it("Should not generate a Semaphore proof with default snark artifacts with Node.js", async () => {
51-
const group = new Group(treeDepth)
52-
53-
group.addMembers([BigInt(1), BigInt(2), identity.commitment])
49+
const group = new Group(treeDepth, 20, [BigInt(1), BigInt(2), identity.commitment])
5450

5551
const fun = () => generateProof(identity, group, externalNullifier, signal)
5652

5753
await expect(fun).rejects.toThrow("ENOENT: no such file or directory")
5854
})
5955

6056
it("Should generate a Semaphore proof passing a group as parameter", async () => {
61-
const group = new Group(treeDepth)
62-
63-
group.addMembers([BigInt(1), BigInt(2), identity.commitment])
57+
const group = new Group(treeDepth, 20, [BigInt(1), BigInt(2), identity.commitment])
6458

6559
fullProof = await generateProof(identity, group, externalNullifier, signal, {
6660
wasmFilePath,
@@ -72,9 +66,7 @@ describe("Proof", () => {
7266
}, 20000)
7367

7468
it("Should generate a Semaphore proof passing a Merkle proof as parameter", async () => {
75-
const group = new Group(treeDepth)
76-
77-
group.addMembers([BigInt(1), BigInt(2), identity.commitment])
69+
const group = new Group(treeDepth, 20, [BigInt(1), BigInt(2), identity.commitment])
7870

7971
fullProof = await generateProof(identity, group.generateMerkleProof(2), externalNullifier, signal, {
8072
wasmFilePath,
@@ -104,47 +96,35 @@ describe("Proof", () => {
10496
it("Should hash the signal value correctly", async () => {
10597
const signalHash = hash(signal)
10698

107-
expect(signalHash.toString()).toBe(
108-
"8665846418922331996225934941481656421248110469944536651334918563951783029"
109-
)
99+
expect(signalHash).toBe("8665846418922331996225934941481656421248110469944536651334918563951783029")
110100
})
111101

112102
it("Should hash the external nullifier value correctly", async () => {
113103
const externalNullifierHash = hash(externalNullifier)
114104

115-
expect(externalNullifierHash.toString()).toBe(
105+
expect(externalNullifierHash).toBe(
116106
"244178201824278269437519042830883072613014992408751798420801126401127326826"
117107
)
118108
})
119109

120110
it("Should hash a number", async () => {
121-
expect(hash(2).toString()).toBe(
122-
"113682330006535319932160121224458771213356533826860247409332700812532759386"
123-
)
111+
expect(hash(2)).toBe("113682330006535319932160121224458771213356533826860247409332700812532759386")
124112
})
125113

126114
it("Should hash a big number", async () => {
127-
expect(hash(BigInt(2)).toString()).toBe(
128-
"113682330006535319932160121224458771213356533826860247409332700812532759386"
129-
)
115+
expect(hash(BigInt(2))).toBe("113682330006535319932160121224458771213356533826860247409332700812532759386")
130116
})
131117

132118
it("Should hash an hex number", async () => {
133-
expect(hash("0x2").toString()).toBe(
134-
"113682330006535319932160121224458771213356533826860247409332700812532759386"
135-
)
119+
expect(hash("0x2")).toBe("113682330006535319932160121224458771213356533826860247409332700812532759386")
136120
})
137121

138122
it("Should hash an string number", async () => {
139-
expect(hash("2").toString()).toBe(
140-
"113682330006535319932160121224458771213356533826860247409332700812532759386"
141-
)
123+
expect(hash("2")).toBe("113682330006535319932160121224458771213356533826860247409332700812532759386")
142124
})
143125

144126
it("Should hash an array", async () => {
145-
expect(hash([2]).toString()).toBe(
146-
"113682330006535319932160121224458771213356533826860247409332700812532759386"
147-
)
127+
expect(hash([2])).toBe("113682330006535319932160121224458771213356533826860247409332700812532759386")
148128
})
149129
})
150130

packages/proof/src/packProof.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
import { SnarkJSProof, Proof } from "./types"
1+
import { Groth16Proof } from "snarkjs"
2+
import { PackedProof } from "./types"
23

34
/**
45
* Packs a proof into a format compatible with Semaphore.
5-
* @param originalProof The proof generated with SnarkJS.
6+
* @param proof The Groth16 proof generated with SnarkJS.
67
* @returns The proof compatible with Semaphore.
78
*/
8-
export default function packProof(originalProof: SnarkJSProof): Proof {
9+
export default function packProof(proof: Groth16Proof): PackedProof {
910
return [
10-
originalProof.pi_a[0],
11-
originalProof.pi_a[1],
12-
originalProof.pi_b[0][1],
13-
originalProof.pi_b[0][0],
14-
originalProof.pi_b[1][1],
15-
originalProof.pi_b[1][0],
16-
originalProof.pi_c[0],
17-
originalProof.pi_c[1]
11+
proof.pi_a[0],
12+
proof.pi_a[1],
13+
proof.pi_b[0][1],
14+
proof.pi_b[0][0],
15+
proof.pi_b[1][1],
16+
proof.pi_b[1][0],
17+
proof.pi_c[0],
18+
proof.pi_c[1]
1819
]
1920
}

0 commit comments

Comments
 (0)