From 202e4bead456262910fae4a9274a39bf5a301a1d Mon Sep 17 00:00:00 2001 From: cedoor Date: Wed, 6 Dec 2023 19:58:13 +0000 Subject: [PATCH] refactor(identity): add identity commitment attribute --- packages/identity/README.md | 4 ++-- packages/identity/package.json | 3 ++- packages/identity/src/identity.test.ts | 12 +++++------ packages/identity/src/identity.ts | 30 +++++++++++--------------- yarn.lock | 1 + 5 files changed, 22 insertions(+), 28 deletions(-) diff --git a/packages/identity/README.md b/packages/identity/README.md index ab5fb6ab6..7064f48f4 100644 --- a/packages/identity/README.md +++ b/packages/identity/README.md @@ -76,7 +76,7 @@ yarn add @semaphore-protocol/identity import { Identity } from "@semaphore-protocol/identity" // The identity will be generated randomly. -const { privateKey, publicKey } = new Identity() +const { privateKey, publicKey, identityCommitment } = new Identity() // Alternatively, you can pass your private key. const identity = new Identity("your-private-key") @@ -106,7 +106,7 @@ const signature = identity.signMessage(message) identity.verifySignature(message, signature) ``` -\# **Identity.verifySignature**(message: _BigNumberish_, signature: _Signature_, publicKey: _BigNumber_ | _Point_): _boolean_ +\# **Identity.verifySignature**(message: _BigNumberish_, signature: _Signature_, publicKey: _Point_): _boolean_ ```typescript import { Identity } from "@semaphore-protocol/identity" diff --git a/packages/identity/package.json b/packages/identity/package.json index 7637f7369..1df750c77 100644 --- a/packages/identity/package.json +++ b/packages/identity/package.json @@ -36,6 +36,7 @@ "rollup-plugin-typescript2": "^0.31.2" }, "dependencies": { - "@zk-kit/eddsa-poseidon": "0.4.1" + "@zk-kit/eddsa-poseidon": "0.4.1", + "poseidon-lite": "^0.2.0" } } diff --git a/packages/identity/src/identity.test.ts b/packages/identity/src/identity.test.ts index 5cd699579..f0d12a416 100644 --- a/packages/identity/src/identity.test.ts +++ b/packages/identity/src/identity.test.ts @@ -8,19 +8,18 @@ describe("Identity", () => { const identity = new Identity() expect(Buffer.isBuffer(identity.privateKey)).toBeTruthy() - expect(typeof identity.publicKey).toBe("string") expect(typeof identity.secretScalar).toBe("string") - expect(identity.unpackedPublicKey).toHaveLength(2) + expect(identity.publicKey).toHaveLength(2) + expect(typeof identity.identityCommitment).toBe("string") }) it("Should create deterministic identities from a secret (private key)", () => { const identity = new Identity(privateKey) expect(typeof identity.privateKey).toBe("string") - expect(typeof identity.publicKey).toBe("string") expect(typeof identity.secretScalar).toBe("string") - expect(identity.unpackedPublicKey).toHaveLength(2) - expect(typeof identity.unpackedPublicKey[0]).toBe("string") + expect(identity.publicKey).toHaveLength(2) + expect(typeof identity.identityCommitment).toBe("string") }) }) @@ -51,7 +50,6 @@ describe("Identity", () => { const signature = identity.signMessage("message") expect(Identity.verifySignature("message", signature, identity.publicKey)).toBeTruthy() - expect(Identity.verifySignature("message", signature, BigInt(identity.publicKey))).toBeTruthy() }) it("Should verify an external signature with an unpacked public key", () => { @@ -59,7 +57,7 @@ describe("Identity", () => { const signature = identity.signMessage("message") - expect(Identity.verifySignature("message", signature, identity.unpackedPublicKey)).toBeTruthy() + expect(Identity.verifySignature("message", signature, identity.publicKey)).toBeTruthy() }) }) }) diff --git a/packages/identity/src/identity.ts b/packages/identity/src/identity.ts index 0721e5d6d..507f50f95 100644 --- a/packages/identity/src/identity.ts +++ b/packages/identity/src/identity.ts @@ -1,22 +1,20 @@ import { - BigNumber, BigNumberish, Point, Signature, derivePublicKey, deriveSecretScalar, - packPublicKey, signMessage, - unpackPublicKey, verifySignature } from "@zk-kit/eddsa-poseidon" import { randomBytes } from "crypto" +import { poseidon2 } from "poseidon-lite/poseidon2" export default class Identity { private _privateKey: BigNumberish private _secretScalar: string - private _unpackedPublicKey: Point - private _publicKey: string + private _publicKey: Point + private _identityCommitment: string /** * Initializes the class attributes based on the parameters. @@ -26,9 +24,9 @@ export default class Identity { this._privateKey = privateKey this._secretScalar = deriveSecretScalar(privateKey) - this._unpackedPublicKey = derivePublicKey(privateKey) + this._publicKey = derivePublicKey(privateKey) - this._publicKey = packPublicKey(this._unpackedPublicKey) as string + this._identityCommitment = poseidon2(this._publicKey).toString() } /** @@ -51,16 +49,16 @@ export default class Identity { * Returns the public key. * @returns The public key. */ - public get publicKey(): string { + public get publicKey(): Point { return this._publicKey } /** - * Returns the unpacked public key. - * @returns The unpacked public key. + * Returns the identity commitment. + * @returns The identity commitment. */ - public get unpackedPublicKey(): Point { - return this._unpackedPublicKey + public get identityCommitment(): string { + return this._identityCommitment } public signMessage(message: BigNumberish): Signature { @@ -68,14 +66,10 @@ export default class Identity { } public verifySignature(message: BigNumberish, signature: Signature): boolean { - return verifySignature(message, signature, this._unpackedPublicKey) + return verifySignature(message, signature, this._publicKey) } - static verifySignature(message: BigNumberish, signature: Signature, publicKey: BigNumber | Point): boolean { - if (typeof publicKey === "string" || typeof publicKey === "bigint") { - publicKey = unpackPublicKey(publicKey) - } - + static verifySignature(message: BigNumberish, signature: Signature, publicKey: Point): boolean { return verifySignature(message, signature, publicKey) } } diff --git a/yarn.lock b/yarn.lock index 1dd028ee3..f7f1b2278 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8492,6 +8492,7 @@ __metadata: "@rollup/plugin-commonjs": ^24.0.1 "@rollup/plugin-node-resolve": ^15.0.1 "@zk-kit/eddsa-poseidon": 0.4.1 + poseidon-lite: ^0.2.0 rollup-plugin-cleanup: ^3.2.1 rollup-plugin-typescript2: ^0.31.2 languageName: unknown