@semaphore-protocol/group 4.0.0-alpha.7 → 4.0.0-beta

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
@@ -18,7 +18,7 @@
18
18
  <a href="https://npmjs.org/package/@semaphore-protocol/group">
19
19
  <img alt="Downloads" src="https://img.shields.io/npm/dm/@semaphore-protocol/group.svg?style=flat-square" />
20
20
  </a>
21
- <a href="https://js.semaphore.pse.dev/group">
21
+ <a href="https://js.semaphore.pse.dev/modules/_semaphore_protocol_group">
22
22
  <img alt="Documentation typedoc" src="https://img.shields.io/badge/docs-typedoc-744C7C?style=flat-square">
23
23
  </a>
24
24
  <a href="https://eslint.org/">
@@ -70,6 +70,8 @@ yarn add @semaphore-protocol/group
70
70
 
71
71
  ## 📜 Usage
72
72
 
73
+ For more information on the functions provided by `@semaphore-protocol/group`, please refer to the [TypeDoc documentation](https://js.semaphore.pse.dev/modules/_semaphore_protocol_group).
74
+
73
75
  \# **new Group**(members: _BigNumberish[]_ = []): _Group_
74
76
 
75
77
  ```typescript
package/dist/index.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/group
3
- * @version 4.0.0-alpha.7
3
+ * @version 4.0.0-beta
4
4
  * @file A library to create and manage Semaphore groups.
5
5
  * @copyright Ethereum Foundation 2024
6
6
  * @license MIT
@@ -111,10 +111,22 @@ function poseidon2(inputs) {
111
111
  return (0, _poseidon.default)(inputs, c);
112
112
  }
113
113
 
114
+ /**
115
+ * The Semaphore group is a {@link https://zkkit.pse.dev/classes/_zk_kit_imt.LeanIMT.html | LeanIMT}
116
+ * (Lean Incremental Merkle Tree), i.e. an optimized version of the incremental binary Merkle tree
117
+ * used by Semaphore V3. The new tree does not use zero hashes, and its depth is dynamic.
118
+ * The members of a Semaphore group, or the leaves of a tree, are the identity commitments.
119
+ * Thanks to the properties of Merkle trees, it can be efficiently demonstrated that a group
120
+ * member belongs to the group.
121
+ * This class supports operations such as member addition, update, removal and Merkle proof
122
+ * generation and verification. Groups can also be exported or imported.
123
+ */
114
124
  var Group = /** @class */ (function () {
115
125
  /**
116
- * Initializes the group with the group id and the tree depth.
117
- * @param members List of group members.
126
+ * Creates a new instance of the Group. Optionally, a list of identity commitments can
127
+ * be passed as a parameter. Adding members in chunks is more efficient than adding
128
+ * them one by one with the `addMember` function.
129
+ * @param members A list of identity commitments.
118
130
  */
119
131
  function Group(members) {
120
132
  if (members === void 0) { members = []; }
@@ -123,7 +135,7 @@ var Group = /** @class */ (function () {
123
135
  Object.defineProperty(Group.prototype, "root", {
124
136
  /**
125
137
  * Returns the root hash of the tree.
126
- * @returns Root hash.
138
+ * @returns The root hash as a string.
127
139
  */
128
140
  get: function () {
129
141
  return this.leanIMT.root ? this.leanIMT.root.toString() : "0";
@@ -134,7 +146,7 @@ var Group = /** @class */ (function () {
134
146
  Object.defineProperty(Group.prototype, "depth", {
135
147
  /**
136
148
  * Returns the depth of the tree.
137
- * @returns Tree depth.
149
+ * @returns The tree depth as a number.
138
150
  */
139
151
  get: function () {
140
152
  return this.leanIMT.depth;
@@ -145,7 +157,7 @@ var Group = /** @class */ (function () {
145
157
  Object.defineProperty(Group.prototype, "size", {
146
158
  /**
147
159
  * Returns the size of the tree (i.e. number of leaves).
148
- * @returns Tree depth.
160
+ * @returns The tree size as a number.
149
161
  */
150
162
  get: function () {
151
163
  return this.leanIMT.size;
@@ -156,7 +168,7 @@ var Group = /** @class */ (function () {
156
168
  Object.defineProperty(Group.prototype, "members", {
157
169
  /**
158
170
  * Returns the members (i.e. identity commitments) of the group.
159
- * @returns List of members.
171
+ * @returns The list of members of the group.
160
172
  */
161
173
  get: function () {
162
174
  return this.leanIMT.leaves.map(String);
@@ -166,15 +178,15 @@ var Group = /** @class */ (function () {
166
178
  });
167
179
  /**
168
180
  * Returns the index of a member. If the member does not exist it returns -1.
169
- * @param member Group member.
170
- * @returns Index of the member.
181
+ * @param member A member of the group.
182
+ * @returns The index of the member, or -1 if it does not exist.
171
183
  */
172
184
  Group.prototype.indexOf = function (member) {
173
185
  return this.leanIMT.indexOf(BigInt(member));
174
186
  };
175
187
  /**
176
188
  * Adds a new member to the group.
177
- * @param member New member.
189
+ * @param member The new member to be added.
178
190
  */
179
191
  Group.prototype.addMember = function (member) {
180
192
  this.leanIMT.insert(BigInt(member));
@@ -196,15 +208,15 @@ var Group = /** @class */ (function () {
196
208
  };
197
209
  /**
198
210
  * Removes a member from the group.
199
- * @param index Index of the member to be removed.
211
+ * @param index The index of the member to be removed.
200
212
  */
201
213
  Group.prototype.removeMember = function (index) {
202
214
  this.leanIMT.update(index, BigInt(0));
203
215
  };
204
216
  /**
205
- * Creates a proof of membership.
206
- * @param index Index of the proof's member.
207
- * @returns Proof object.
217
+ * Creates a proof of membership for a member of the group.
218
+ * @param index The index of the member.
219
+ * @returns The {@link MerkleProof} object.
208
220
  */
209
221
  Group.prototype.generateMerkleProof = function (_index) {
210
222
  var _a = this.leanIMT.generateProof(_index), index = _a.index, leaf = _a.leaf, root = _a.root, siblings = _a.siblings;
@@ -216,7 +228,7 @@ var Group = /** @class */ (function () {
216
228
  };
217
229
  };
218
230
  /**
219
- * It enables the conversion of the group into a JSON string that
231
+ * Enables the conversion of the group into a JSON string that
220
232
  * can be re-used for future imports. This approach is beneficial for
221
233
  * large groups, as it avoids re-calculating the tree hashes.
222
234
  * @returns The stringified JSON of the group.
@@ -225,11 +237,11 @@ var Group = /** @class */ (function () {
225
237
  return this.leanIMT.export();
226
238
  };
227
239
  /**
228
- * It imports an entire group by initializing the tree without calculating
240
+ * Imports an entire group by initializing the tree without calculating
229
241
  * any hashes. Note that it is crucial to ensure the integrity of the
230
242
  * exported group.
231
243
  * @param nodes The stringified JSON of the group.
232
- * @returns The group instance.
244
+ * @returns The {@link Group} instance.
233
245
  */
234
246
  Group.import = function (exportedGroup) {
235
247
  var group = new Group();
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/group
3
- * @version 4.0.0-alpha.7
3
+ * @version 4.0.0-beta
4
4
  * @file A library to create and manage Semaphore groups.
5
5
  * @copyright Ethereum Foundation 2024
6
6
  * @license MIT
@@ -109,10 +109,22 @@ function poseidon2(inputs) {
109
109
  return (0, _poseidon.default)(inputs, c);
110
110
  }
111
111
 
112
+ /**
113
+ * The Semaphore group is a {@link https://zkkit.pse.dev/classes/_zk_kit_imt.LeanIMT.html | LeanIMT}
114
+ * (Lean Incremental Merkle Tree), i.e. an optimized version of the incremental binary Merkle tree
115
+ * used by Semaphore V3. The new tree does not use zero hashes, and its depth is dynamic.
116
+ * The members of a Semaphore group, or the leaves of a tree, are the identity commitments.
117
+ * Thanks to the properties of Merkle trees, it can be efficiently demonstrated that a group
118
+ * member belongs to the group.
119
+ * This class supports operations such as member addition, update, removal and Merkle proof
120
+ * generation and verification. Groups can also be exported or imported.
121
+ */
112
122
  var Group = /** @class */ (function () {
113
123
  /**
114
- * Initializes the group with the group id and the tree depth.
115
- * @param members List of group members.
124
+ * Creates a new instance of the Group. Optionally, a list of identity commitments can
125
+ * be passed as a parameter. Adding members in chunks is more efficient than adding
126
+ * them one by one with the `addMember` function.
127
+ * @param members A list of identity commitments.
116
128
  */
117
129
  function Group(members) {
118
130
  if (members === void 0) { members = []; }
@@ -121,7 +133,7 @@ var Group = /** @class */ (function () {
121
133
  Object.defineProperty(Group.prototype, "root", {
122
134
  /**
123
135
  * Returns the root hash of the tree.
124
- * @returns Root hash.
136
+ * @returns The root hash as a string.
125
137
  */
126
138
  get: function () {
127
139
  return this.leanIMT.root ? this.leanIMT.root.toString() : "0";
@@ -132,7 +144,7 @@ var Group = /** @class */ (function () {
132
144
  Object.defineProperty(Group.prototype, "depth", {
133
145
  /**
134
146
  * Returns the depth of the tree.
135
- * @returns Tree depth.
147
+ * @returns The tree depth as a number.
136
148
  */
137
149
  get: function () {
138
150
  return this.leanIMT.depth;
@@ -143,7 +155,7 @@ var Group = /** @class */ (function () {
143
155
  Object.defineProperty(Group.prototype, "size", {
144
156
  /**
145
157
  * Returns the size of the tree (i.e. number of leaves).
146
- * @returns Tree depth.
158
+ * @returns The tree size as a number.
147
159
  */
148
160
  get: function () {
149
161
  return this.leanIMT.size;
@@ -154,7 +166,7 @@ var Group = /** @class */ (function () {
154
166
  Object.defineProperty(Group.prototype, "members", {
155
167
  /**
156
168
  * Returns the members (i.e. identity commitments) of the group.
157
- * @returns List of members.
169
+ * @returns The list of members of the group.
158
170
  */
159
171
  get: function () {
160
172
  return this.leanIMT.leaves.map(String);
@@ -164,15 +176,15 @@ var Group = /** @class */ (function () {
164
176
  });
165
177
  /**
166
178
  * Returns the index of a member. If the member does not exist it returns -1.
167
- * @param member Group member.
168
- * @returns Index of the member.
179
+ * @param member A member of the group.
180
+ * @returns The index of the member, or -1 if it does not exist.
169
181
  */
170
182
  Group.prototype.indexOf = function (member) {
171
183
  return this.leanIMT.indexOf(BigInt(member));
172
184
  };
173
185
  /**
174
186
  * Adds a new member to the group.
175
- * @param member New member.
187
+ * @param member The new member to be added.
176
188
  */
177
189
  Group.prototype.addMember = function (member) {
178
190
  this.leanIMT.insert(BigInt(member));
@@ -194,15 +206,15 @@ var Group = /** @class */ (function () {
194
206
  };
195
207
  /**
196
208
  * Removes a member from the group.
197
- * @param index Index of the member to be removed.
209
+ * @param index The index of the member to be removed.
198
210
  */
199
211
  Group.prototype.removeMember = function (index) {
200
212
  this.leanIMT.update(index, BigInt(0));
201
213
  };
202
214
  /**
203
- * Creates a proof of membership.
204
- * @param index Index of the proof's member.
205
- * @returns Proof object.
215
+ * Creates a proof of membership for a member of the group.
216
+ * @param index The index of the member.
217
+ * @returns The {@link MerkleProof} object.
206
218
  */
207
219
  Group.prototype.generateMerkleProof = function (_index) {
208
220
  var _a = this.leanIMT.generateProof(_index), index = _a.index, leaf = _a.leaf, root = _a.root, siblings = _a.siblings;
@@ -214,7 +226,7 @@ var Group = /** @class */ (function () {
214
226
  };
215
227
  };
216
228
  /**
217
- * It enables the conversion of the group into a JSON string that
229
+ * Enables the conversion of the group into a JSON string that
218
230
  * can be re-used for future imports. This approach is beneficial for
219
231
  * large groups, as it avoids re-calculating the tree hashes.
220
232
  * @returns The stringified JSON of the group.
@@ -223,11 +235,11 @@ var Group = /** @class */ (function () {
223
235
  return this.leanIMT.export();
224
236
  };
225
237
  /**
226
- * It imports an entire group by initializing the tree without calculating
238
+ * Imports an entire group by initializing the tree without calculating
227
239
  * any hashes. Note that it is crucial to ensure the integrity of the
228
240
  * exported group.
229
241
  * @param nodes The stringified JSON of the group.
230
- * @returns The group instance.
242
+ * @returns The {@link Group} instance.
231
243
  */
232
244
  Group.import = function (exportedGroup) {
233
245
  var group = new Group();
@@ -1,41 +1,53 @@
1
1
  import { LeanIMT } from "@zk-kit/imt";
2
2
  import { BigNumberish, MerkleProof } from "./types";
3
+ /**
4
+ * The Semaphore group is a {@link https://zkkit.pse.dev/classes/_zk_kit_imt.LeanIMT.html | LeanIMT}
5
+ * (Lean Incremental Merkle Tree), i.e. an optimized version of the incremental binary Merkle tree
6
+ * used by Semaphore V3. The new tree does not use zero hashes, and its depth is dynamic.
7
+ * The members of a Semaphore group, or the leaves of a tree, are the identity commitments.
8
+ * Thanks to the properties of Merkle trees, it can be efficiently demonstrated that a group
9
+ * member belongs to the group.
10
+ * This class supports operations such as member addition, update, removal and Merkle proof
11
+ * generation and verification. Groups can also be exported or imported.
12
+ */
3
13
  export default class Group {
4
14
  leanIMT: LeanIMT;
5
15
  /**
6
- * Initializes the group with the group id and the tree depth.
7
- * @param members List of group members.
16
+ * Creates a new instance of the Group. Optionally, a list of identity commitments can
17
+ * be passed as a parameter. Adding members in chunks is more efficient than adding
18
+ * them one by one with the `addMember` function.
19
+ * @param members A list of identity commitments.
8
20
  */
9
21
  constructor(members?: BigNumberish[]);
10
22
  /**
11
23
  * Returns the root hash of the tree.
12
- * @returns Root hash.
24
+ * @returns The root hash as a string.
13
25
  */
14
26
  get root(): string;
15
27
  /**
16
28
  * Returns the depth of the tree.
17
- * @returns Tree depth.
29
+ * @returns The tree depth as a number.
18
30
  */
19
31
  get depth(): number;
20
32
  /**
21
33
  * Returns the size of the tree (i.e. number of leaves).
22
- * @returns Tree depth.
34
+ * @returns The tree size as a number.
23
35
  */
24
36
  get size(): number;
25
37
  /**
26
38
  * Returns the members (i.e. identity commitments) of the group.
27
- * @returns List of members.
39
+ * @returns The list of members of the group.
28
40
  */
29
41
  get members(): string[];
30
42
  /**
31
43
  * Returns the index of a member. If the member does not exist it returns -1.
32
- * @param member Group member.
33
- * @returns Index of the member.
44
+ * @param member A member of the group.
45
+ * @returns The index of the member, or -1 if it does not exist.
34
46
  */
35
47
  indexOf(member: BigNumberish): number;
36
48
  /**
37
49
  * Adds a new member to the group.
38
- * @param member New member.
50
+ * @param member The new member to be added.
39
51
  */
40
52
  addMember(member: BigNumberish): void;
41
53
  /**
@@ -51,28 +63,28 @@ export default class Group {
51
63
  updateMember(index: number, member: BigNumberish): void;
52
64
  /**
53
65
  * Removes a member from the group.
54
- * @param index Index of the member to be removed.
66
+ * @param index The index of the member to be removed.
55
67
  */
56
68
  removeMember(index: number): void;
57
69
  /**
58
- * Creates a proof of membership.
59
- * @param index Index of the proof's member.
60
- * @returns Proof object.
70
+ * Creates a proof of membership for a member of the group.
71
+ * @param index The index of the member.
72
+ * @returns The {@link MerkleProof} object.
61
73
  */
62
74
  generateMerkleProof(_index: number): MerkleProof;
63
75
  /**
64
- * It enables the conversion of the group into a JSON string that
76
+ * Enables the conversion of the group into a JSON string that
65
77
  * can be re-used for future imports. This approach is beneficial for
66
78
  * large groups, as it avoids re-calculating the tree hashes.
67
79
  * @returns The stringified JSON of the group.
68
80
  */
69
81
  export(): string;
70
82
  /**
71
- * It imports an entire group by initializing the tree without calculating
83
+ * Imports an entire group by initializing the tree without calculating
72
84
  * any hashes. Note that it is crucial to ensure the integrity of the
73
85
  * exported group.
74
86
  * @param nodes The stringified JSON of the group.
75
- * @returns The group instance.
87
+ * @returns The {@link Group} instance.
76
88
  */
77
89
  static import(exportedGroup: string): Group;
78
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@semaphore-protocol/group",
3
- "version": "4.0.0-alpha.7",
3
+ "version": "4.0.0-beta",
4
4
  "description": "A library to create and manage Semaphore groups.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/group.ts CHANGED
@@ -2,12 +2,25 @@ import { LeanIMT } from "@zk-kit/imt"
2
2
  import { poseidon2 } from "poseidon-lite/poseidon2"
3
3
  import { BigNumberish, MerkleProof } from "./types"
4
4
 
5
+ /**
6
+ * The Semaphore group is a {@link https://zkkit.pse.dev/classes/_zk_kit_imt.LeanIMT.html | LeanIMT}
7
+ * (Lean Incremental Merkle Tree), i.e. an optimized version of the incremental binary Merkle tree
8
+ * used by Semaphore V3. The new tree does not use zero hashes, and its depth is dynamic.
9
+ * The members of a Semaphore group, or the leaves of a tree, are the identity commitments.
10
+ * Thanks to the properties of Merkle trees, it can be efficiently demonstrated that a group
11
+ * member belongs to the group.
12
+ * This class supports operations such as member addition, update, removal and Merkle proof
13
+ * generation and verification. Groups can also be exported or imported.
14
+ */
5
15
  export default class Group {
6
- leanIMT: LeanIMT
16
+ // The {@link https://zkkit.pse.dev/classes/_zk_kit_imt.LeanIMT.html | LeanIMT} instance.
17
+ public leanIMT: LeanIMT
7
18
 
8
19
  /**
9
- * Initializes the group with the group id and the tree depth.
10
- * @param members List of group members.
20
+ * Creates a new instance of the Group. Optionally, a list of identity commitments can
21
+ * be passed as a parameter. Adding members in chunks is more efficient than adding
22
+ * them one by one with the `addMember` function.
23
+ * @param members A list of identity commitments.
11
24
  */
12
25
  constructor(members: BigNumberish[] = []) {
13
26
  this.leanIMT = new LeanIMT((a, b) => poseidon2([a, b]), members.map(BigInt))
@@ -15,50 +28,50 @@ export default class Group {
15
28
 
16
29
  /**
17
30
  * Returns the root hash of the tree.
18
- * @returns Root hash.
31
+ * @returns The root hash as a string.
19
32
  */
20
- get root(): string {
33
+ public get root(): string {
21
34
  return this.leanIMT.root ? this.leanIMT.root.toString() : "0"
22
35
  }
23
36
 
24
37
  /**
25
38
  * Returns the depth of the tree.
26
- * @returns Tree depth.
39
+ * @returns The tree depth as a number.
27
40
  */
28
- get depth(): number {
41
+ public get depth(): number {
29
42
  return this.leanIMT.depth
30
43
  }
31
44
 
32
45
  /**
33
46
  * Returns the size of the tree (i.e. number of leaves).
34
- * @returns Tree depth.
47
+ * @returns The tree size as a number.
35
48
  */
36
- get size(): number {
49
+ public get size(): number {
37
50
  return this.leanIMT.size
38
51
  }
39
52
 
40
53
  /**
41
54
  * Returns the members (i.e. identity commitments) of the group.
42
- * @returns List of members.
55
+ * @returns The list of members of the group.
43
56
  */
44
- get members(): string[] {
57
+ public get members(): string[] {
45
58
  return this.leanIMT.leaves.map(String)
46
59
  }
47
60
 
48
61
  /**
49
62
  * Returns the index of a member. If the member does not exist it returns -1.
50
- * @param member Group member.
51
- * @returns Index of the member.
63
+ * @param member A member of the group.
64
+ * @returns The index of the member, or -1 if it does not exist.
52
65
  */
53
- indexOf(member: BigNumberish): number {
66
+ public indexOf(member: BigNumberish): number {
54
67
  return this.leanIMT.indexOf(BigInt(member))
55
68
  }
56
69
 
57
70
  /**
58
71
  * Adds a new member to the group.
59
- * @param member New member.
72
+ * @param member The new member to be added.
60
73
  */
61
- addMember(member: BigNumberish) {
74
+ public addMember(member: BigNumberish) {
62
75
  this.leanIMT.insert(BigInt(member))
63
76
  }
64
77
 
@@ -66,7 +79,7 @@ export default class Group {
66
79
  * Adds new members to the group.
67
80
  * @param members New members.
68
81
  */
69
- addMembers(members: BigNumberish[]) {
82
+ public addMembers(members: BigNumberish[]) {
70
83
  this.leanIMT.insertMany(members.map(BigInt))
71
84
  }
72
85
 
@@ -75,24 +88,24 @@ export default class Group {
75
88
  * @param index Index of the member to be updated.
76
89
  * @param member New member value.
77
90
  */
78
- updateMember(index: number, member: BigNumberish) {
91
+ public updateMember(index: number, member: BigNumberish) {
79
92
  this.leanIMT.update(index, BigInt(member))
80
93
  }
81
94
 
82
95
  /**
83
96
  * Removes a member from the group.
84
- * @param index Index of the member to be removed.
97
+ * @param index The index of the member to be removed.
85
98
  */
86
- removeMember(index: number) {
99
+ public removeMember(index: number) {
87
100
  this.leanIMT.update(index, BigInt(0))
88
101
  }
89
102
 
90
103
  /**
91
- * Creates a proof of membership.
92
- * @param index Index of the proof's member.
93
- * @returns Proof object.
104
+ * Creates a proof of membership for a member of the group.
105
+ * @param index The index of the member.
106
+ * @returns The {@link MerkleProof} object.
94
107
  */
95
- generateMerkleProof(_index: number): MerkleProof {
108
+ public generateMerkleProof(_index: number): MerkleProof {
96
109
  const { index, leaf, root, siblings } = this.leanIMT.generateProof(_index)
97
110
 
98
111
  return {
@@ -104,21 +117,21 @@ export default class Group {
104
117
  }
105
118
 
106
119
  /**
107
- * It enables the conversion of the group into a JSON string that
120
+ * Enables the conversion of the group into a JSON string that
108
121
  * can be re-used for future imports. This approach is beneficial for
109
122
  * large groups, as it avoids re-calculating the tree hashes.
110
123
  * @returns The stringified JSON of the group.
111
124
  */
112
- export(): string {
125
+ public export(): string {
113
126
  return this.leanIMT.export()
114
127
  }
115
128
 
116
129
  /**
117
- * It imports an entire group by initializing the tree without calculating
130
+ * Imports an entire group by initializing the tree without calculating
118
131
  * any hashes. Note that it is crucial to ensure the integrity of the
119
132
  * exported group.
120
133
  * @param nodes The stringified JSON of the group.
121
- * @returns The group instance.
134
+ * @returns The {@link Group} instance.
122
135
  */
123
136
  static import(exportedGroup: string): Group {
124
137
  const group = new Group()