@dorafactory/maci-sdk 0.0.52 → 0.0.53
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/dist/index.js +14 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +14 -10
- package/dist/index.mjs.map +1 -1
- package/dist/libs/crypto/tree.d.ts +13 -14
- package/package.json +1 -1
- package/src/libs/crypto/keys.ts +1 -1
- package/src/libs/crypto/tree.ts +33 -30
|
@@ -1,23 +1,22 @@
|
|
|
1
1
|
export declare class Tree {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
readonly DEPTH: number;
|
|
3
|
+
readonly HEIGHT: number;
|
|
4
|
+
readonly DEGREE: number;
|
|
5
|
+
readonly LEAVES_COUNT: number;
|
|
6
|
+
readonly LEAVES_IDX_0: number;
|
|
7
|
+
readonly NODES_COUNT: number;
|
|
8
|
+
protected nodes: bigint[];
|
|
9
|
+
protected zeros: bigint[];
|
|
10
10
|
constructor(degree: number, depth: number, zero: bigint);
|
|
11
11
|
get root(): bigint;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
initLeaves(leaves:
|
|
12
|
+
initZero(zero: bigint): void;
|
|
13
|
+
initNodes(): void;
|
|
14
|
+
initLeaves(leaves: bigint[]): void;
|
|
15
15
|
leaf(leafIdx: number): bigint;
|
|
16
16
|
leaves(): bigint[];
|
|
17
17
|
updateLeaf(leafIdx: number, leaf: bigint): void;
|
|
18
|
-
pathIdxOf(leafIdx: number):
|
|
18
|
+
pathIdxOf(leafIdx: number): bigint[];
|
|
19
19
|
pathElementOf(leafIdx: number): bigint[][];
|
|
20
20
|
subTree(length: number): Tree;
|
|
21
|
-
|
|
21
|
+
protected _update(nodeIdx: number): void;
|
|
22
22
|
}
|
|
23
|
-
export default Tree;
|
package/package.json
CHANGED
package/src/libs/crypto/keys.ts
CHANGED
|
@@ -16,7 +16,7 @@ import { packPublicKey, unpackPublicKey } from '@zk-kit/eddsa-poseidon';
|
|
|
16
16
|
|
|
17
17
|
import { EcdhSharedKey, Keypair, PrivKey, PubKey } from './types';
|
|
18
18
|
import { poseidon } from './hashing';
|
|
19
|
-
import Tree from './tree';
|
|
19
|
+
import { Tree } from './tree';
|
|
20
20
|
import { genRandomBabyJubValue } from './babyjub';
|
|
21
21
|
|
|
22
22
|
const SNARK_FIELD_SIZE =
|
package/src/libs/crypto/tree.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { poseidon } from './hashing';
|
|
2
2
|
|
|
3
3
|
export class Tree {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
public readonly DEPTH: number;
|
|
5
|
+
public readonly HEIGHT: number;
|
|
6
|
+
public readonly DEGREE: number;
|
|
7
|
+
public readonly LEAVES_COUNT: number;
|
|
8
|
+
public readonly LEAVES_IDX_0: number;
|
|
9
|
+
public readonly NODES_COUNT: number;
|
|
10
|
+
|
|
11
|
+
protected nodes: bigint[] = [];
|
|
12
|
+
protected zeros: bigint[] = [];
|
|
12
13
|
|
|
13
14
|
constructor(degree: number, depth: number, zero: bigint) {
|
|
14
15
|
this.DEPTH = depth;
|
|
@@ -23,34 +24,38 @@ export class Tree {
|
|
|
23
24
|
this.initNodes();
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
get root()
|
|
27
|
+
get root() {
|
|
27
28
|
return this.nodes[0];
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
for (let i = 1; i <
|
|
34
|
-
|
|
31
|
+
initZero(zero: bigint) {
|
|
32
|
+
const zeros = new Array(this.HEIGHT);
|
|
33
|
+
zeros[0] = zero;
|
|
34
|
+
for (let i = 1; i < zeros.length; i++) {
|
|
35
|
+
const children = new Array(this.DEGREE).fill(zeros[i - 1]);
|
|
36
|
+
zeros[i] = poseidon(children);
|
|
35
37
|
}
|
|
38
|
+
this.zeros = zeros;
|
|
36
39
|
}
|
|
37
40
|
|
|
38
|
-
|
|
41
|
+
initNodes() {
|
|
39
42
|
const DEGREE = this.DEGREE;
|
|
40
43
|
|
|
41
|
-
|
|
44
|
+
const nodes = new Array(this.NODES_COUNT);
|
|
42
45
|
|
|
43
46
|
for (let d = this.DEPTH; d >= 0; d--) {
|
|
44
47
|
const size = DEGREE ** d;
|
|
45
48
|
const idx0 = (DEGREE ** d - 1) / (DEGREE - 1);
|
|
46
49
|
const zero = this.zeros[this.DEPTH - d];
|
|
47
50
|
for (let i = 0; i < size; i++) {
|
|
48
|
-
|
|
51
|
+
nodes[idx0 + i] = zero;
|
|
49
52
|
}
|
|
50
53
|
}
|
|
54
|
+
|
|
55
|
+
this.nodes = nodes;
|
|
51
56
|
}
|
|
52
57
|
|
|
53
|
-
initLeaves(leaves:
|
|
58
|
+
initLeaves(leaves: bigint[]) {
|
|
54
59
|
const DEGREE = this.DEGREE;
|
|
55
60
|
for (let i = 0; i < leaves.length; i++) {
|
|
56
61
|
if (i >= this.LEAVES_COUNT) {
|
|
@@ -71,7 +76,7 @@ export class Tree {
|
|
|
71
76
|
}
|
|
72
77
|
}
|
|
73
78
|
|
|
74
|
-
leaf(leafIdx: number)
|
|
79
|
+
leaf(leafIdx: number) {
|
|
75
80
|
if (leafIdx > this.LEAVES_COUNT || leafIdx < 0) {
|
|
76
81
|
throw new Error('wrong leaf index');
|
|
77
82
|
}
|
|
@@ -79,11 +84,11 @@ export class Tree {
|
|
|
79
84
|
return this.nodes[nodeIdx];
|
|
80
85
|
}
|
|
81
86
|
|
|
82
|
-
leaves()
|
|
87
|
+
leaves() {
|
|
83
88
|
return this.nodes.slice(this.LEAVES_IDX_0);
|
|
84
89
|
}
|
|
85
90
|
|
|
86
|
-
updateLeaf(leafIdx: number, leaf: bigint)
|
|
91
|
+
updateLeaf(leafIdx: number, leaf: bigint) {
|
|
87
92
|
if (leafIdx > this.LEAVES_COUNT || leafIdx < 0) {
|
|
88
93
|
throw new Error('wrong leaf index');
|
|
89
94
|
}
|
|
@@ -93,18 +98,18 @@ export class Tree {
|
|
|
93
98
|
this._update(nodeIdx);
|
|
94
99
|
}
|
|
95
100
|
|
|
96
|
-
pathIdxOf(leafIdx: number)
|
|
101
|
+
pathIdxOf(leafIdx: number) {
|
|
97
102
|
if (leafIdx > this.LEAVES_COUNT || leafIdx < 0) {
|
|
98
103
|
throw new Error('wrong leaf index');
|
|
99
104
|
}
|
|
100
105
|
let idx = this.LEAVES_IDX_0 + leafIdx;
|
|
101
|
-
const pathIdx:
|
|
106
|
+
const pathIdx: bigint[] = [];
|
|
102
107
|
|
|
103
108
|
for (let i = 0; i < this.DEPTH; i++) {
|
|
104
109
|
const parentIdx = Math.floor((idx - 1) / this.DEGREE);
|
|
105
110
|
const childrenIdx0 = parentIdx * this.DEGREE + 1;
|
|
106
111
|
|
|
107
|
-
pathIdx.push(idx - childrenIdx0);
|
|
112
|
+
pathIdx.push(BigInt(idx - childrenIdx0));
|
|
108
113
|
|
|
109
114
|
idx = parentIdx;
|
|
110
115
|
}
|
|
@@ -112,7 +117,7 @@ export class Tree {
|
|
|
112
117
|
return pathIdx;
|
|
113
118
|
}
|
|
114
119
|
|
|
115
|
-
pathElementOf(leafIdx: number)
|
|
120
|
+
pathElementOf(leafIdx: number) {
|
|
116
121
|
if (leafIdx > this.LEAVES_COUNT || leafIdx < 0) {
|
|
117
122
|
throw new Error('wrong leaf index');
|
|
118
123
|
}
|
|
@@ -137,7 +142,7 @@ export class Tree {
|
|
|
137
142
|
return pathElement;
|
|
138
143
|
}
|
|
139
144
|
|
|
140
|
-
subTree(length: number)
|
|
145
|
+
subTree(length: number) {
|
|
141
146
|
const subTree = new Tree(this.DEGREE, this.DEPTH, this.zeros[0]);
|
|
142
147
|
const nodes = [...this.nodes];
|
|
143
148
|
|
|
@@ -159,18 +164,16 @@ export class Tree {
|
|
|
159
164
|
return subTree;
|
|
160
165
|
}
|
|
161
166
|
|
|
162
|
-
|
|
167
|
+
protected _update(nodeIdx: number) {
|
|
163
168
|
let idx = nodeIdx;
|
|
164
169
|
while (idx > 0) {
|
|
165
170
|
const parentIdx = Math.floor((idx - 1) / this.DEGREE);
|
|
166
171
|
const childrenIdx0 = parentIdx * this.DEGREE + 1;
|
|
167
172
|
this.nodes[parentIdx] = poseidon(
|
|
168
|
-
this.nodes.slice(childrenIdx0, childrenIdx0 +
|
|
173
|
+
this.nodes.slice(childrenIdx0, childrenIdx0 + 5)
|
|
169
174
|
);
|
|
170
175
|
|
|
171
176
|
idx = parentIdx;
|
|
172
177
|
}
|
|
173
178
|
}
|
|
174
179
|
}
|
|
175
|
-
|
|
176
|
-
export default Tree;
|