@semaphore-protocol/identity 2.5.0 → 2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -46,8 +46,8 @@
46
46
  </h4>
47
47
  </div>
48
48
 
49
- | This library provides a class that can be used to create identities compatible with the Semaphore [circuits](https://github.com/semaphore-protocol/semaphore/tree/main/circuits). Each identity contains two private values (_trapdoor_ and _nullifier_), and the Poseidon hash of these values (_commitment_) is used as the public identifier of the Semaphore identity. |
50
- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
49
+ | This library provides a class that can be used to create identities compatible with the Semaphore [circuits](https://github.com/semaphore-protocol/semaphore/tree/main/circuits). Each identity contains two secret values: _trapdoor_ and _nullifier_, and one public value: _commitment_. The Poseidon hash of the secret values is the identity secret, and its hash is the identity commitment. |
50
+ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
51
51
 
52
52
  ## 🛠 Install
53
53
 
@@ -72,30 +72,15 @@ yarn add @semaphore-protocol/identity
72
72
  ```typescript
73
73
  import { Identity } from "@semaphore-protocol/identity"
74
74
 
75
- // Trapdoor and nullifier are generated randomly.
75
+ // The identity can be generated randomly.
76
76
  const identity1 = new Identity()
77
77
 
78
- // Trapdoor and nullifier are generated deterministically from a secret message.
78
+ // Deterministically from a secret message.
79
79
  const identity2 = new Identity("secret-message")
80
80
 
81
- // Trapdoor and nullifier are generated from an existing identity.
81
+ // Or it can be retrieved from an existing identity.
82
82
  const identity3 = new Identity(identity1.toString())
83
- ```
84
-
85
- \# **getTrapdoor**(): _bigint_
86
-
87
- ```typescript
88
- const trapdoor = identity.getTrapdoor()
89
- ```
90
-
91
- \# **getNullifier**(): _bigint_
92
83
 
93
- ```typescript
94
- const nullifier = identity.getNullifier()
95
- ```
96
-
97
- \# **generateCommitment**(): _bigint_
98
-
99
- ```typescript
100
- const commitment = identity.generateCommitment()
84
+ // Trapdoor, nullifier and commitment are the attributes (e.g. JS getters).
85
+ const { trapdoor, nullifier, commitment } = identity1
101
86
  ```
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/identity
3
- * @version 2.5.0
3
+ * @version 2.6.1
4
4
  * @file A library to create Semaphore identities.
5
5
  * @copyright Ethereum Foundation 2022
6
6
  * @license MIT
@@ -36,6 +36,15 @@ function genRandomNumber(numberOfBytes) {
36
36
  if (numberOfBytes === void 0) { numberOfBytes = 31; }
37
37
  return BigNumber.from(randomBytes(numberOfBytes)).toBigInt();
38
38
  }
39
+ /**
40
+ * Generates the identity commitment from trapdoor and nullifier.
41
+ * @param nullifier The identity nullifier.
42
+ * @param trapdoor The identity trapdoor.
43
+ * @returns identity commitment
44
+ */
45
+ function generateCommitment(nullifier, trapdoor) {
46
+ return poseidon([poseidon([nullifier, trapdoor])]);
47
+ }
39
48
  /**
40
49
  * Checks if a string is a JSON.
41
50
  * @param jsonString The JSON string.
@@ -59,6 +68,7 @@ var Identity = /** @class */ (function () {
59
68
  if (identityOrMessage === undefined) {
60
69
  this._trapdoor = genRandomNumber();
61
70
  this._nullifier = genRandomNumber();
71
+ this._commitment = generateCommitment(this._nullifier, this._trapdoor);
62
72
  return;
63
73
  }
64
74
  checkParameter(identityOrMessage, "identityOrMessage", "string");
@@ -66,12 +76,25 @@ var Identity = /** @class */ (function () {
66
76
  var messageHash = sha256(identityOrMessage).slice(2);
67
77
  this._trapdoor = BigNumber.from(sha256("".concat(messageHash, "identity_trapdoor"))).toBigInt();
68
78
  this._nullifier = BigNumber.from(sha256("".concat(messageHash, "identity_nullifier"))).toBigInt();
79
+ this._commitment = generateCommitment(this._nullifier, this._trapdoor);
69
80
  return;
70
81
  }
71
82
  var _a = JSON.parse(identityOrMessage), trapdoor = _a[0], nullifier = _a[1];
72
83
  this._trapdoor = BigNumber.from("0x".concat(trapdoor)).toBigInt();
73
84
  this._nullifier = BigNumber.from("0x".concat(nullifier)).toBigInt();
85
+ this._commitment = generateCommitment(this._nullifier, this._trapdoor);
74
86
  }
87
+ Object.defineProperty(Identity.prototype, "trapdoor", {
88
+ /**
89
+ * Returns the identity trapdoor.
90
+ * @returns The identity trapdoor.
91
+ */
92
+ get: function () {
93
+ return this._trapdoor;
94
+ },
95
+ enumerable: false,
96
+ configurable: true
97
+ });
75
98
  /**
76
99
  * Returns the identity trapdoor.
77
100
  * @returns The identity trapdoor.
@@ -79,6 +102,17 @@ var Identity = /** @class */ (function () {
79
102
  Identity.prototype.getTrapdoor = function () {
80
103
  return this._trapdoor;
81
104
  };
105
+ Object.defineProperty(Identity.prototype, "nullifier", {
106
+ /**
107
+ * Returns the identity nullifier.
108
+ * @returns The identity nullifier.
109
+ */
110
+ get: function () {
111
+ return this._nullifier;
112
+ },
113
+ enumerable: false,
114
+ configurable: true
115
+ });
82
116
  /**
83
117
  * Returns the identity nullifier.
84
118
  * @returns The identity nullifier.
@@ -86,7 +120,26 @@ var Identity = /** @class */ (function () {
86
120
  Identity.prototype.getNullifier = function () {
87
121
  return this._nullifier;
88
122
  };
123
+ Object.defineProperty(Identity.prototype, "commitment", {
124
+ /**
125
+ * Returns the identity commitment.
126
+ * @returns The identity commitment.
127
+ */
128
+ get: function () {
129
+ return this._commitment;
130
+ },
131
+ enumerable: false,
132
+ configurable: true
133
+ });
134
+ /**
135
+ * Returns the identity commitment.
136
+ * @returns The identity commitment.
137
+ */
138
+ Identity.prototype.getCommitment = function () {
139
+ return this._commitment;
140
+ };
89
141
  /**
142
+ * @deprecated since version 2.6.0
90
143
  * Generates the identity commitment from trapdoor and nullifier.
91
144
  * @returns identity commitment
92
145
  */
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/identity
3
- * @version 2.5.0
3
+ * @version 2.6.1
4
4
  * @file A library to create Semaphore identities.
5
5
  * @copyright Ethereum Foundation 2022
6
6
  * @license MIT
@@ -40,6 +40,15 @@ function genRandomNumber(numberOfBytes) {
40
40
  if (numberOfBytes === void 0) { numberOfBytes = 31; }
41
41
  return bignumber.BigNumber.from(random.randomBytes(numberOfBytes)).toBigInt();
42
42
  }
43
+ /**
44
+ * Generates the identity commitment from trapdoor and nullifier.
45
+ * @param nullifier The identity nullifier.
46
+ * @param trapdoor The identity trapdoor.
47
+ * @returns identity commitment
48
+ */
49
+ function generateCommitment(nullifier, trapdoor) {
50
+ return circomlibjs.poseidon([circomlibjs.poseidon([nullifier, trapdoor])]);
51
+ }
43
52
  /**
44
53
  * Checks if a string is a JSON.
45
54
  * @param jsonString The JSON string.
@@ -63,6 +72,7 @@ var Identity = /** @class */ (function () {
63
72
  if (identityOrMessage === undefined) {
64
73
  this._trapdoor = genRandomNumber();
65
74
  this._nullifier = genRandomNumber();
75
+ this._commitment = generateCommitment(this._nullifier, this._trapdoor);
66
76
  return;
67
77
  }
68
78
  checkParameter(identityOrMessage, "identityOrMessage", "string");
@@ -70,12 +80,25 @@ var Identity = /** @class */ (function () {
70
80
  var messageHash = sha256(identityOrMessage).slice(2);
71
81
  this._trapdoor = bignumber.BigNumber.from(sha256("".concat(messageHash, "identity_trapdoor"))).toBigInt();
72
82
  this._nullifier = bignumber.BigNumber.from(sha256("".concat(messageHash, "identity_nullifier"))).toBigInt();
83
+ this._commitment = generateCommitment(this._nullifier, this._trapdoor);
73
84
  return;
74
85
  }
75
86
  var _a = JSON.parse(identityOrMessage), trapdoor = _a[0], nullifier = _a[1];
76
87
  this._trapdoor = bignumber.BigNumber.from("0x".concat(trapdoor)).toBigInt();
77
88
  this._nullifier = bignumber.BigNumber.from("0x".concat(nullifier)).toBigInt();
89
+ this._commitment = generateCommitment(this._nullifier, this._trapdoor);
78
90
  }
91
+ Object.defineProperty(Identity.prototype, "trapdoor", {
92
+ /**
93
+ * Returns the identity trapdoor.
94
+ * @returns The identity trapdoor.
95
+ */
96
+ get: function () {
97
+ return this._trapdoor;
98
+ },
99
+ enumerable: false,
100
+ configurable: true
101
+ });
79
102
  /**
80
103
  * Returns the identity trapdoor.
81
104
  * @returns The identity trapdoor.
@@ -83,6 +106,17 @@ var Identity = /** @class */ (function () {
83
106
  Identity.prototype.getTrapdoor = function () {
84
107
  return this._trapdoor;
85
108
  };
109
+ Object.defineProperty(Identity.prototype, "nullifier", {
110
+ /**
111
+ * Returns the identity nullifier.
112
+ * @returns The identity nullifier.
113
+ */
114
+ get: function () {
115
+ return this._nullifier;
116
+ },
117
+ enumerable: false,
118
+ configurable: true
119
+ });
86
120
  /**
87
121
  * Returns the identity nullifier.
88
122
  * @returns The identity nullifier.
@@ -90,7 +124,26 @@ var Identity = /** @class */ (function () {
90
124
  Identity.prototype.getNullifier = function () {
91
125
  return this._nullifier;
92
126
  };
127
+ Object.defineProperty(Identity.prototype, "commitment", {
128
+ /**
129
+ * Returns the identity commitment.
130
+ * @returns The identity commitment.
131
+ */
132
+ get: function () {
133
+ return this._commitment;
134
+ },
135
+ enumerable: false,
136
+ configurable: true
137
+ });
138
+ /**
139
+ * Returns the identity commitment.
140
+ * @returns The identity commitment.
141
+ */
142
+ Identity.prototype.getCommitment = function () {
143
+ return this._commitment;
144
+ };
93
145
  /**
146
+ * @deprecated since version 2.6.0
94
147
  * Generates the identity commitment from trapdoor and nullifier.
95
148
  * @returns identity commitment
96
149
  */
@@ -1,22 +1,44 @@
1
1
  export default class Identity {
2
2
  private _trapdoor;
3
3
  private _nullifier;
4
+ private _commitment;
4
5
  /**
5
6
  * Initializes the class attributes based on the strategy passed as parameter.
6
7
  * @param identityOrMessage Additional data needed to create identity for given strategy.
7
8
  */
8
9
  constructor(identityOrMessage?: string);
10
+ /**
11
+ * Returns the identity trapdoor.
12
+ * @returns The identity trapdoor.
13
+ */
14
+ get trapdoor(): bigint;
9
15
  /**
10
16
  * Returns the identity trapdoor.
11
17
  * @returns The identity trapdoor.
12
18
  */
13
19
  getTrapdoor(): bigint;
20
+ /**
21
+ * Returns the identity nullifier.
22
+ * @returns The identity nullifier.
23
+ */
24
+ get nullifier(): bigint;
14
25
  /**
15
26
  * Returns the identity nullifier.
16
27
  * @returns The identity nullifier.
17
28
  */
18
29
  getNullifier(): bigint;
19
30
  /**
31
+ * Returns the identity commitment.
32
+ * @returns The identity commitment.
33
+ */
34
+ get commitment(): bigint;
35
+ /**
36
+ * Returns the identity commitment.
37
+ * @returns The identity commitment.
38
+ */
39
+ getCommitment(): bigint;
40
+ /**
41
+ * @deprecated since version 2.6.0
20
42
  * Generates the identity commitment from trapdoor and nullifier.
21
43
  * @returns identity commitment
22
44
  */
@@ -10,6 +10,13 @@ export declare function sha256(message: string): string;
10
10
  * @returns The generated random number.
11
11
  */
12
12
  export declare function genRandomNumber(numberOfBytes?: number): bigint;
13
+ /**
14
+ * Generates the identity commitment from trapdoor and nullifier.
15
+ * @param nullifier The identity nullifier.
16
+ * @param trapdoor The identity trapdoor.
17
+ * @returns identity commitment
18
+ */
19
+ export declare function generateCommitment(nullifier: bigint, trapdoor: bigint): bigint;
13
20
  /**
14
21
  * Checks if a string is a JSON.
15
22
  * @param jsonString The JSON string.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@semaphore-protocol/identity",
3
- "version": "2.5.0",
3
+ "version": "2.6.1",
4
4
  "description": "A library to create Semaphore identities.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.node.js",
@@ -17,16 +17,17 @@ describe("Identity", () => {
17
17
  const identity1 = new Identity()
18
18
  const identity2 = new Identity()
19
19
 
20
- expect(identity1.getTrapdoor()).not.toBe(identity2.getTrapdoor())
21
- expect(identity1.getNullifier()).not.toBe(identity2.getNullifier())
20
+ expect(identity1.trapdoor).not.toBe(identity2.getTrapdoor())
21
+ expect(identity1.nullifier).not.toBe(identity2.getNullifier())
22
+ expect(identity1.commitment).not.toBe(identity2.getCommitment())
22
23
  })
23
24
 
24
25
  it("Should create deterministic identities from a message", () => {
25
26
  const identity1 = new Identity("message")
26
27
  const identity2 = new Identity("message")
27
28
 
28
- expect(identity1.getTrapdoor()).toBe(identity2.getTrapdoor())
29
- expect(identity1.getNullifier()).toBe(identity2.getNullifier())
29
+ expect(identity1.trapdoor).toBe(identity2.getTrapdoor())
30
+ expect(identity1.nullifier).toBe(identity2.getNullifier())
30
31
  })
31
32
 
32
33
  it("Should create deterministic identities from number/boolean messages", () => {
@@ -35,10 +36,10 @@ describe("Identity", () => {
35
36
  const identity3 = new Identity("7")
36
37
  const identity4 = new Identity("7")
37
38
 
38
- expect(identity1.getTrapdoor()).toBe(identity2.getTrapdoor())
39
- expect(identity1.getNullifier()).toBe(identity2.getNullifier())
40
- expect(identity3.getTrapdoor()).toBe(identity4.getTrapdoor())
41
- expect(identity3.getNullifier()).toBe(identity4.getNullifier())
39
+ expect(identity1.trapdoor).toBe(identity2.getTrapdoor())
40
+ expect(identity1.nullifier).toBe(identity2.getNullifier())
41
+ expect(identity3.trapdoor).toBe(identity4.getTrapdoor())
42
+ expect(identity3.nullifier).toBe(identity4.getNullifier())
42
43
  })
43
44
 
44
45
  it("Should not recreate an existing invalid identity", () => {
@@ -52,8 +53,8 @@ describe("Identity", () => {
52
53
 
53
54
  const identity2 = new Identity(identity1.toString())
54
55
 
55
- expect(identity1.getTrapdoor()).toBe(identity2.getTrapdoor())
56
- expect(identity1.getNullifier()).toBe(identity2.getNullifier())
56
+ expect(identity1.trapdoor).toBe(identity2.getTrapdoor())
57
+ expect(identity1.nullifier).toBe(identity2.getNullifier())
57
58
  })
58
59
  })
59
60
 
package/src/identity.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import { BigNumber } from "@ethersproject/bignumber"
2
2
  import { poseidon } from "circomlibjs"
3
3
  import checkParameter from "./checkParameter"
4
- import { genRandomNumber, isJsonArray, sha256 } from "./utils"
4
+ import { generateCommitment, genRandomNumber, isJsonArray, sha256 } from "./utils"
5
5
 
6
6
  export default class Identity {
7
7
  private _trapdoor: bigint
8
8
  private _nullifier: bigint
9
+ private _commitment: bigint
9
10
 
10
11
  /**
11
12
  * Initializes the class attributes based on the strategy passed as parameter.
@@ -15,6 +16,7 @@ export default class Identity {
15
16
  if (identityOrMessage === undefined) {
16
17
  this._trapdoor = genRandomNumber()
17
18
  this._nullifier = genRandomNumber()
19
+ this._commitment = generateCommitment(this._nullifier, this._trapdoor)
18
20
 
19
21
  return
20
22
  }
@@ -26,6 +28,7 @@ export default class Identity {
26
28
 
27
29
  this._trapdoor = BigNumber.from(sha256(`${messageHash}identity_trapdoor`)).toBigInt()
28
30
  this._nullifier = BigNumber.from(sha256(`${messageHash}identity_nullifier`)).toBigInt()
31
+ this._commitment = generateCommitment(this._nullifier, this._trapdoor)
29
32
 
30
33
  return
31
34
  }
@@ -34,6 +37,15 @@ export default class Identity {
34
37
 
35
38
  this._trapdoor = BigNumber.from(`0x${trapdoor}`).toBigInt()
36
39
  this._nullifier = BigNumber.from(`0x${nullifier}`).toBigInt()
40
+ this._commitment = generateCommitment(this._nullifier, this._trapdoor)
41
+ }
42
+
43
+ /**
44
+ * Returns the identity trapdoor.
45
+ * @returns The identity trapdoor.
46
+ */
47
+ public get trapdoor(): bigint {
48
+ return this._trapdoor
37
49
  }
38
50
 
39
51
  /**
@@ -44,6 +56,14 @@ export default class Identity {
44
56
  return this._trapdoor
45
57
  }
46
58
 
59
+ /**
60
+ * Returns the identity nullifier.
61
+ * @returns The identity nullifier.
62
+ */
63
+ public get nullifier(): bigint {
64
+ return this._nullifier
65
+ }
66
+
47
67
  /**
48
68
  * Returns the identity nullifier.
49
69
  * @returns The identity nullifier.
@@ -53,6 +73,23 @@ export default class Identity {
53
73
  }
54
74
 
55
75
  /**
76
+ * Returns the identity commitment.
77
+ * @returns The identity commitment.
78
+ */
79
+ public get commitment(): bigint {
80
+ return this._commitment
81
+ }
82
+
83
+ /**
84
+ * Returns the identity commitment.
85
+ * @returns The identity commitment.
86
+ */
87
+ public getCommitment(): bigint {
88
+ return this._commitment
89
+ }
90
+
91
+ /**
92
+ * @deprecated since version 2.6.0
56
93
  * Generates the identity commitment from trapdoor and nullifier.
57
94
  * @returns identity commitment
58
95
  */
package/src/utils.ts CHANGED
@@ -2,6 +2,7 @@ import { BigNumber } from "@ethersproject/bignumber"
2
2
  import { randomBytes } from "@ethersproject/random"
3
3
  import { sha256 as _sha256 } from "@ethersproject/sha2"
4
4
  import { toUtf8Bytes } from "@ethersproject/strings"
5
+ import { poseidon } from "circomlibjs"
5
6
 
6
7
  /**
7
8
  * Returns an hexadecimal sha256 hash of the message passed as parameter.
@@ -23,6 +24,16 @@ export function genRandomNumber(numberOfBytes = 31): bigint {
23
24
  return BigNumber.from(randomBytes(numberOfBytes)).toBigInt()
24
25
  }
25
26
 
27
+ /**
28
+ * Generates the identity commitment from trapdoor and nullifier.
29
+ * @param nullifier The identity nullifier.
30
+ * @param trapdoor The identity trapdoor.
31
+ * @returns identity commitment
32
+ */
33
+ export function generateCommitment(nullifier: bigint, trapdoor: bigint): bigint {
34
+ return poseidon([poseidon([nullifier, trapdoor])])
35
+ }
36
+
26
37
  /**
27
38
  * Checks if a string is a JSON.
28
39
  * @param jsonString The JSON string.