• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

semaphore-protocol / extensions / 15018959907

14 May 2025 10:59AM UTC coverage: 98.039%. Remained the same
15018959907

push

github

web-flow
chore: update semaphore version (#35)

5 of 5 branches covered (100.0%)

Branch coverage included in aggregate %.

45 of 46 relevant lines covered (97.83%)

2.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/packages/identity-proof/src/generate.ts
1
import { maybeGetSnarkArtifacts, Project, SnarkArtifacts } from "@zk-kit/artifacts"
2✔
2
import { deriveSecretScalar } from "@zk-kit/eddsa-poseidon"
2✔
3
import { packGroth16Proof } from "@zk-kit/utils"
2✔
4
import type { BigNumberish } from "ethers"
5
import { groth16, NumericString } from "snarkjs"
2✔
6
import hash from "./hash"
2✔
7
import toBigInt from "./to-bigint"
2✔
8
import { IdentityProof } from "./types"
9

10
/**
11
 * Creates a zero-knowledge proof to prove that you have the pre-image of a Semaphore commitment,
12
 * without disclosing the actual preimage itself.
13
 * The use of a scope parameter along with a nullifier helps ensure the uniqueness
14
 * and non-reusability of the proofs, enhancing security in applications like
15
 * blockchain transactions or private data verification.
16
 * If, for example, this package were used with Semaphore to demonstrate possession
17
 * of a Semaphore identity of a group of voters, the scope could be the poll's ID.
18
 * @param privateKey The private key of the commitment.
19
 * @param scope A public value used to contextualize the cryptographic proof
20
 * and calculate the nullifier.
21
 * @param snarkArtifacts The Snark artifacts (wasm and zkey files) generated in
22
 * a trusted setup of the circuit are necessary to generate valid proofs
23
 * @returns The Poseidon zero-knowledge proof.
24
 */
25
export default async function generate(
2✔
26
    privateKey: Buffer | Uint8Array | string,
27
    scope: BigNumberish | Uint8Array | string,
28
    snarkArtifacts?: SnarkArtifacts
29
): Promise<IdentityProof> {
30
    scope = toBigInt(scope)
2✔
31

32
    // allow user to override our artifacts
33
    // otherwise they'll be downloaded if not already in local tmp folder
34
    snarkArtifacts ??= await maybeGetSnarkArtifacts(Project.SEMAPHORE_IDENTITY)
2✔
35
    const { wasm, zkey } = snarkArtifacts
2✔
36
    const secretScalar = deriveSecretScalar(privateKey)
2✔
37

38
    const { proof, publicSignals } = await groth16.fullProve(
2✔
39
        {
40
            secret: secretScalar,
41
            scope: hash(scope)
42
        },
43
        wasm,
44
        zkey
45
    )
46

47
    return {
2✔
48
        commitment: publicSignals[0],
49
        scope: scope.toString() as NumericString,
50
        proof: packGroth16Proof(proof)
51
    }
52
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc