11import { BigNumber } from "@ethersproject/bignumber"
22import hash from "js-sha512"
3+ import { poseidon1 } from "poseidon-lite/poseidon1"
4+ import { poseidon2 } from "poseidon-lite/poseidon2"
35import checkParameter from "./checkParameter"
4- import { generateCommitment , genRandomNumber , isJsonArray } from "./utils"
6+ import { genRandomNumber , isJsonArray } from "./utils"
57
68export 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.
0 commit comments