diff --git a/packages/identity/package.json b/packages/identity/package.json index 5f6b47b..d88c545 100644 --- a/packages/identity/package.json +++ b/packages/identity/package.json @@ -1,6 +1,6 @@ { "name": "@libsem/identity", - "version": "1.0.6", + "version": "1.0.7", "description": "Library for managing identites for Semaphore and Rln protocols", "main": "dist/index.node.js", "types": "dist/types/index.d.ts", diff --git a/packages/identity/src/identity.ts b/packages/identity/src/identity.ts index 1a1bb5f..9ba9e0e 100644 --- a/packages/identity/src/identity.ts +++ b/packages/identity/src/identity.ts @@ -14,7 +14,7 @@ class ZkIdentity { * @param metadata additional data needed to create identity for given strategy * @returns Identity */ - genIdentity(strategy = "random", metadata: any = {}): Identity { + genIdentity(strategy: "random" | "signedMessage" = "random", metadata: any = {}): Identity { if (strategy === "random") return genRandomIdentity(); else if (strategy === "signedMessage") return genIdentityFromSignedMessage(metadata); @@ -48,11 +48,21 @@ class ZkIdentity { * @param secret identity secret * @returns identity commitment */ - genIdentityCommitment(secret: bigint[]): bigint { + genIdentityCommitmentFromSecret(secret: bigint[]): bigint { const secretHash = poseidonHash(secret); return poseidonHash([secretHash]); } + /** + * Generate commitment from identity + * @param identity identity + * @returns identity commitment + */ + genIdentityCommitment(identity: Identity): bigint { + const secretHash = poseidonHash([identity.identityNullifier, identity.identityTrapdoor]); + return poseidonHash([secretHash]); + } + /** * Serializes identity * @param identity to serialize diff --git a/packages/identity/tests/identity.test.ts b/packages/identity/tests/identity.test.ts index 6cdec9e..4a28b8f 100644 --- a/packages/identity/tests/identity.test.ts +++ b/packages/identity/tests/identity.test.ts @@ -23,17 +23,16 @@ describe("Semaphore identity", () => { expect(typeof identitySecret).toEqual("object") }) - it("Should generate identity commitment from identity secret", async () => { + it("Should generate identity commitment from identity", async () => { const identity: Identity = ZkIdentity.genIdentity(); - const identitySecret: bigint[] = ZkIdentity.genSecretFromIdentity(identity); - const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identitySecret); + const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identity); expect(typeof identityCommitment).toEqual("bigint") }) it("Should generate identity commitment from random secret", async () => { const secretParts = 5; const identitySecret: bigint[] = ZkIdentity.genRandomSecret(secretParts); - const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identitySecret); + const identityCommitment: bigint = ZkIdentity.genIdentityCommitmentFromSecret(identitySecret); expect(typeof identityCommitment).toEqual("bigint") }) @@ -49,4 +48,4 @@ describe("Semaphore identity", () => { expect(unserialized).toStrictEqual(identity) }) }) -}) \ No newline at end of file +}) diff --git a/packages/protocols/package.json b/packages/protocols/package.json index 95fbcf4..1664f67 100644 --- a/packages/protocols/package.json +++ b/packages/protocols/package.json @@ -1,6 +1,6 @@ { "name": "@libsem/protocols", - "version": "1.0.7", + "version": "1.0.8", "description": "Client library for generating and verifying Semaphore & Rln ZK proofs", "main": "dist/index.node.js", "types": "dist/types/index.d.ts", diff --git a/packages/protocols/tests/rln.test.ts b/packages/protocols/tests/rln.test.ts index a0047c9..8a84f1f 100644 --- a/packages/protocols/tests/rln.test.ts +++ b/packages/protocols/tests/rln.test.ts @@ -12,8 +12,7 @@ beforeAll(() => { for (let i=0; i { it("Generate rln witness", () => { const identity: Identity = ZkIdentity.genIdentity(); const identitySecret: bigint[] = ZkIdentity.genSecretFromIdentity(identity); - const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identitySecret); + const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identity); const secretHash: bigint = poseidonHash(identitySecret); const commitments: Array = Object.assign([], identityCommitments); @@ -46,7 +45,7 @@ describe("Rln", () => { const identitySecret: bigint[] = ZkIdentity.genSecretFromIdentity(identity); const secretHash: bigint = poseidonHash(identitySecret); - const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identitySecret); + const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identity); const commitments: Array = Object.assign([], identityCommitments); commitments.push(identityCommitment); diff --git a/packages/protocols/tests/semaphore.test.ts b/packages/protocols/tests/semaphore.test.ts index 87bbee1..6681d14 100644 --- a/packages/protocols/tests/semaphore.test.ts +++ b/packages/protocols/tests/semaphore.test.ts @@ -15,8 +15,7 @@ beforeAll(() => { for (let i=0; i { const identity: Identity = ZkIdentity.genIdentity(); const externalNullifier: string = genExternalNullifier("voting_1"); const signal = '0x111'; - const identitySecret: bigint[] = ZkIdentity.genSecretFromIdentity(identity); - const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identitySecret); + const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identity); const commitments: Array = Object.assign([], identityCommitments); commitments.push(identityCommitment); @@ -46,8 +44,7 @@ describe("Semaphore", () => { const externalNullifier: string = genExternalNullifier("voting_1"); const signal = '0x111'; const nullifierHash: bigint = Semaphore.genNullifierHash(externalNullifier, identity.identityNullifier, 20); - const identitySecret: bigint[] = ZkIdentity.genSecretFromIdentity(identity); - const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identitySecret); + const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identity); const commitments: Array = Object.assign([], identityCommitments); commitments.push(identityCommitment);