@@ -2,71 +2,84 @@ 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 ) )
14
27
}
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
65
78
/**
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 ( )
0 commit comments