Skip to content

Commit

Permalink
refactor(identity): add identity commitment attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Jan 12, 2024
1 parent fee7f10 commit 202e4be
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages/identity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion packages/identity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
12 changes: 5 additions & 7 deletions packages/identity/src/identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
})

Expand Down Expand Up @@ -51,15 +50,14 @@ 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", () => {
const identity = new Identity(privateKey)

const signature = identity.signMessage("message")

expect(Identity.verifySignature("message", signature, identity.unpackedPublicKey)).toBeTruthy()
expect(Identity.verifySignature("message", signature, identity.publicKey)).toBeTruthy()
})
})
})
30 changes: 12 additions & 18 deletions packages/identity/src/identity.ts
Original file line number Diff line number Diff line change
@@ -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<string>
private _publicKey: string
private _publicKey: Point<string>
private _identityCommitment: string

/**
* Initializes the class attributes based on the parameters.
Expand All @@ -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()
}

/**
Expand All @@ -51,31 +49,27 @@ export default class Identity {
* Returns the public key.
* @returns The public key.
*/
public get publicKey(): string {
public get publicKey(): Point<string> {
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<string> {
return this._unpackedPublicKey
public get identityCommitment(): string {
return this._identityCommitment
}

public signMessage(message: BigNumberish): Signature<string> {
return signMessage(this.privateKey, message)
}

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)
}
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 202e4be

Please sign in to comment.