Skip to content

Commit c9e565a

Browse files
authored
Merge pull request #295 from semaphore-protocol/feat/identity-getter
New identity secret attribute and getter Former-commit-id: 2af0af9
2 parents 5bd7cd9 + 6cea8c5 commit c9e565a

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

packages/identity/src/identity.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe("Identity", () => {
1919

2020
expect(identity1.trapdoor).not.toBe(identity2.getTrapdoor())
2121
expect(identity1.nullifier).not.toBe(identity2.getNullifier())
22+
expect(identity1.secret).not.toBe(identity2.getSecret())
2223
expect(identity1.commitment).not.toBe(identity2.getCommitment())
2324
})
2425

@@ -82,8 +83,18 @@ describe("Identity", () => {
8283
})
8384
})
8485

85-
describe("# generateCommitment", () => {
86-
it("Should generate an identity commitment", () => {
86+
describe("# getSecret", () => {
87+
it("Should return an identity secret", () => {
88+
const { secret } = new Identity("message")
89+
90+
expect(secret.toString()).toBe(
91+
"17452394798940441025978193762953691632066258438336130543532009665042636950194"
92+
)
93+
})
94+
})
95+
96+
describe("# getCommitment", () => {
97+
it("Should return an identity commitment", () => {
8798
const { commitment } = new Identity("message")
8899

89100
expect(commitment.toString()).toBe(

packages/identity/src/identity.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { BigNumber } from "@ethersproject/bignumber"
22
import hash from "js-sha512"
3+
import { poseidon1 } from "poseidon-lite/poseidon1"
4+
import { poseidon2 } from "poseidon-lite/poseidon2"
35
import checkParameter from "./checkParameter"
4-
import { generateCommitment, genRandomNumber, isJsonArray } from "./utils"
6+
import { genRandomNumber, isJsonArray } from "./utils"
57

68
export default class Identity {
79
private _trapdoor: bigint
810
private _nullifier: bigint
11+
private _secret: bigint
912
private _commitment: bigint
1013

1114
/**
@@ -16,7 +19,8 @@ export default class Identity {
1619
if (identityOrMessage === undefined) {
1720
this._trapdoor = genRandomNumber()
1821
this._nullifier = genRandomNumber()
19-
this._commitment = generateCommitment(this._nullifier, this._trapdoor)
22+
this._secret = poseidon2([this._nullifier, this._trapdoor])
23+
this._commitment = poseidon1([this._secret])
2024

2125
return
2226
}
@@ -25,10 +29,11 @@ export default class Identity {
2529

2630
if (!isJsonArray(identityOrMessage)) {
2731
const h = hash.sha512(identityOrMessage).padStart(128, "0")
28-
// alt_bn128 is 253.6 bits, so we can safely use 253 bits
32+
// alt_bn128 is 253.6 bits, so we can safely use 253 bits.
2933
this._trapdoor = BigInt(`0x${h.slice(64)}`) >> BigInt(3)
3034
this._nullifier = BigInt(`0x${h.slice(0, 64)}`) >> BigInt(3)
31-
this._commitment = generateCommitment(this._nullifier, this._trapdoor)
35+
this._secret = poseidon2([this._nullifier, this._trapdoor])
36+
this._commitment = poseidon1([this._secret])
3237

3338
return
3439
}
@@ -37,7 +42,8 @@ export default class Identity {
3742

3843
this._trapdoor = BigNumber.from(trapdoor).toBigInt()
3944
this._nullifier = BigNumber.from(nullifier).toBigInt()
40-
this._commitment = generateCommitment(this._nullifier, this._trapdoor)
45+
this._secret = poseidon2([this._nullifier, this._trapdoor])
46+
this._commitment = poseidon1([this._secret])
4147
}
4248

4349
/**
@@ -72,6 +78,22 @@ export default class Identity {
7278
return this._nullifier
7379
}
7480

81+
/**
82+
* Returns the identity secret.
83+
* @returns The identity secret.
84+
*/
85+
public get secret(): bigint {
86+
return this._secret
87+
}
88+
89+
/**
90+
* Returns the identity secret.
91+
* @returns The identity secret.
92+
*/
93+
public getSecret(): bigint {
94+
return this._secret
95+
}
96+
7597
/**
7698
* Returns the identity commitment.
7799
* @returns The identity commitment.

packages/identity/src/utils.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { BigNumber } from "@ethersproject/bignumber"
22
import { randomBytes } from "@ethersproject/random"
3-
import { poseidon1 } from "poseidon-lite/poseidon1"
4-
import { poseidon2 } from "poseidon-lite/poseidon2"
53

64
/**
75
* Generates a random big number.
@@ -12,16 +10,6 @@ export function genRandomNumber(numberOfBytes = 31): bigint {
1210
return BigNumber.from(randomBytes(numberOfBytes)).toBigInt()
1311
}
1412

15-
/**
16-
* Generates the identity commitment from trapdoor and nullifier.
17-
* @param nullifier The identity nullifier.
18-
* @param trapdoor The identity trapdoor.
19-
* @returns identity commitment
20-
*/
21-
export function generateCommitment(nullifier: bigint, trapdoor: bigint): bigint {
22-
return poseidon1([poseidon2([nullifier, trapdoor])])
23-
}
24-
2513
/**
2614
* Checks if a string is a JSON.
2715
* @param jsonString The JSON string.

0 commit comments

Comments
 (0)