Skip to content

Commit

Permalink
feat(identity): add generate commitment function (#877)
Browse files Browse the repository at this point in the history
This static method is particularly useful after signature verification, as it allows retrieval of
the corresponding commitment associated with the public key.

re #873
  • Loading branch information
vplasencia authored Oct 17, 2024
1 parent 06e11d5 commit b3f8629
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/identity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,20 @@ export class Identity {
static verifySignature(message: BigNumberish, signature: Signature, publicKey: Point): boolean {
return verifySignature(message, signature, publicKey)
}

/**
* Generates the commitment from the given public key.
* This static method is particularly useful after signature verification,
* as it allows retrieval of the corresponding commitment associated with the public key.
*
* @example
* const identity = new Identity()
* Identity.generateCommitment(identity.publicKey)
*
* @param publicKey The public key to generate the commitment.
* @returns The Semaphore identity commitment.
*/
static generateCommitment(publicKey: Point): bigint {
return poseidon2(publicKey)
}
}
10 changes: 10 additions & 0 deletions packages/identity/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,14 @@ describe("Identity", () => {
expect(Identity.verifySignature("message", signature, identity.publicKey)).toBeTruthy()
})
})

describe("# generateCommitment", () => {
it("Should generate the identity commitment from the public key", () => {
const identity = new Identity(privateKeyText)

const commitment = Identity.generateCommitment(identity.publicKey)

expect(identity.commitment).toBe(commitment)
})
})
})

0 comments on commit b3f8629

Please sign in to comment.