semaphore-protocol / semaphore

A zero-knowledge protocol for anonymous interactions.
https://semaphore.pse.dev
MIT License
898 stars 194 forks source link

Generating a new identity with a user-provided private key results in inconsistent behavior #799

Closed gubsheep closed 3 months ago

gubsheep commented 3 months ago

Describe the bug Initialize two fresh Semaphore identities: a new Identity() with empty args, and another new Identity(<private key>) passing in the private key generated for the first identity. These two identities will have different public keys and commitments, though they should be identical.

To Reproduce Steps to reproduce the behavior:

  1. Initialize a fresh Semaphore identity: const id1 = new Identity()
  2. Copy the private key of this identity: const privkey = id1.privateKey.
  3. Initialize a second Semaphore identity, passing in the private key as an argument to constructor: const id2 = new Identity(privkey).

Expected behavior

console.log(id1.privateKey === id2.privateKey) // expect true, got true
console.log(id1.commitment === id2.commitment) // expect true, got false

Screenshots id1 in this screenshot was generated as a fresh Semaphore identity with no args passed into constructor. id2 is generated with id1's private key passed as argument to constructor. Both of these Identities appear to have the same privateKey, but have different publicKey and commitment values.

image

Technologies (please complete the following information):

Additional context

Prepending the private key with 0x prefix does not solve the issue: for example, run const id3 = new Identity('0x' + privkey). Now, id3's commitment and publicKey will be different from both the freshly generated Semaphore ID as well as the identity generated by passing in the first privateKey as a constructor argument.

zkfriendly commented 3 months ago

This issue has existed since this commit c88175bb2de96dd39d9954247fbe7a280716fd4e

cedoor commented 3 months ago

@zkfriendly yess, I found the problem and I'm currently working to solve it.

zkfriendly commented 3 months ago

@zkfriendly yess, I found the problem and I'm currently working to solve it.

@cedoor Ow, I was working on a fix at same time too!

cedoor commented 3 months ago

@cedoor Ow, I was working on a fix at same time too!

Ahh please, take a look at the assignees before working on the next task. Sorry for this misunderstanding!